trunk 20080912
[gitenigma.git] / lib / codecs / codecmpg.cpp
blob13d02a11499a06a14eb1e582c4531c3cda802f66
1 #ifndef DISABLE_FILE
3 #include "lib/codecs/codecmpg.h"
4 #include <lib/dvb/decoder.h>
5 #include <netinet/in.h>
6 #include <unistd.h>
7 #include <fcntl.h>
8 #include <sys/ioctl.h>
9 #include <lib/base/eerror.h>
10 #include <lib/system/econfig.h>
12 unsigned long eDemux::getLong()
14 unsigned long c;
15 if (input.read(&c, 4) != 4)
17 eDebug("read error ! :))");
18 return 0x1b9; // simulate end of program stream.
20 c=htonl(c);
21 return c;
24 void eDemux::refill()
26 last=getLong();
27 remaining=32;
30 unsigned long eDemux::getBits(unsigned int num)
32 unsigned long res=0;
33 while (num)
35 if (!remaining)
36 refill();
37 unsigned int d=num;
38 if (d > remaining)
39 d=remaining;
40 res<<=d;
41 res|=(last>>(remaining-d))&~(-1<<d);
42 remaining-=d;
43 num-=d;
45 return res;
48 void eDemux::syncBits()
50 // round UP. so we re-read the last octet.
51 // that's ok, since syncBits() only does something
52 // when we're out of sync. and in that case, we might have
53 // already read one bit of the startcode.
54 remaining+=7;
55 remaining&=~7;
58 eDemux::eDemux(eIOBuffer &input, eIOBuffer &video, eIOBuffer &audio, int fd)
59 :input(input), video(video), audio(audio), minFrameLength(4096),
60 mpegtype(-1), curAudioStreamID(0), synced(0), fd(fd), sheader(0)
62 remaining=0;
63 memset(&pcmsettings, 0, sizeof(pcmsettings));
66 eDemux::~eDemux()
68 eConfig::getInstance()->setKey("/ezap/audio/prevAudioStreamID", curAudioStreamID);
69 delete [] sheader;
72 int eMPEGDemux::decodeMore(int last, int maxsamples, Signal1<void,unsigned int>*newastreamid )
74 // eDebug("decodeMore");
75 int written=0;
76 (void)last;
78 while (written < maxsamples)
80 unsigned int code=0;
81 while (1) // search for start code.
83 if (input.size() < 4096)
85 maxsamples=0;
86 break;
88 syncBits();
89 if (getBits(8))
90 continue;
91 if (getBits(8))
92 continue;
94 int c=getBits(8);
95 if (!c)
96 goto a;
97 if (c != 1)
98 continue;
99 if (!maxsamples)
100 break;
101 code = getBits(8);
102 switch (code)
104 case 0xb9: // MPEG_program_end_code
106 eDebug("program_end_code");
107 goto finish;
109 case 0xba: // pack_start_code
111 int type=getBits(2);
112 if ( type != mpegtype )
114 switch (type)
116 case 1:
117 Decoder::SetStreamType(TYPE_PES);
118 break;
119 default:
120 Decoder::SetStreamType(TYPE_MPEG1);
121 break;
123 mpegtype=type;
124 // eDebug("set %s", type == 1 ? "MPEG-2" : "MPEG-1" );
126 if (type != 1)
128 getBits(6);
129 getBits(16);
130 getBits(16);
131 getBits(16);
132 getBits(8);
133 continue;
135 int scr_base0, scr_base, scr_ext;
136 scr_base0=getBits(3);
137 scr_base=scr_base0<<30;
138 scr_base0>>=2;
139 if (!getBits(1))
140 continue;
141 scr_base|=getBits(15)<<15;
142 if (!getBits(1))
143 continue;
144 scr_base|=getBits(15);
145 if (!getBits(1))
146 continue;
147 scr_ext=getBits(9);
148 if (!getBits(1))
149 continue;
150 /* int program_mux_rate= */ getBits(22);
151 if (getBits(2) != 3)
152 continue;
153 getBits(5);
154 int pack_stuffing_length=getBits(3);
155 while (pack_stuffing_length--)
156 if (getBits(8) != 0xFF)
157 break;
158 if (pack_stuffing_length >= 0)
159 continue;
160 // eDebug("scr: %08x:%02d\n", scr_base, scr_ext);
161 break;
163 case 0xbb: // system_header_start_code
164 case 0xBE: // Padding Stream
165 case 0xBF: // Private Stream 2 (???)
166 case 0xF0:
167 case 0xF1:
168 case 0xF2:
169 case 0xF3:
170 case 0xFF:
172 // eDebug("system_header %02x", code);
173 int length=getBits(16);
174 while ( length && remaining )
176 getBits(8);
177 --length;
179 if ( length )
181 char buffer[length];
182 if ( input.read(buffer, length) != length )
183 eDebug("read Error in skip");
185 break;
187 case 0xbc: // program_stream_map
189 #if 0
190 int program_stream_map_length=getBits(16);
191 eDebug("program stream map!\n");
192 int current_next_indicator=getBits(1);
193 getBits(2);
194 int program_stream_map_version=getBits(5);
195 getBits(7);
196 if (!getBits(1))
197 continue;
198 int program_stream_info_length=getBits(16);
199 for (int r=0; r<program_stream_info_length; )
201 int tag=getBits(8);
202 length=getBits(8);
203 r+=length+2;
204 eDebug("tag: %02x %02x ", tag, length);
205 while (length--)
206 eDebug("%02lx ", getBits(8));
207 eDebug("\n");
209 int elementary_stream_map_length=getBits(16);
210 for (int r=0; r < elementary_stream_map_length; )
212 int stream_type=getBits(8);
213 int elementary_stream_id=getBits(8);
214 int elementary_stream_info_length=getBits(16);
215 for (int a=0; a < elementary_stream_info_length; )
217 int tag=getBits(8);
218 int length=getBits(8);
219 eDebug("elementary: %02x %02x ", tag, length);
220 while (length--)
221 eDebugNoNewLine("%02x ", getBits(8));
222 eDebug("\n");
224 r+=elementary_stream_info_length+4;
226 getBits(32);
227 #endif
228 break;
230 case 0xC0 ... 0xCF: // Audio Stream
231 case 0xD0 ... 0xDF:
233 int &cnt = audiostreams[code];
234 if ( cnt < 10 )
236 cnt++;
237 if ( cnt == 10 )
239 // eDebug("/*emit*/ (*newastreamid)(%02x)", code);
240 if ( !curAudioStreamID )
242 Decoder::parms.audio_type = DECODE_AUDIO_MPEG;
243 Decoder::Set();
244 curAudioStreamID = code;
246 /*emit*/ (*newastreamid)(code);
250 case 0xBD: // Private Stream 1 (AC3 or ttx)
251 case 0xE0 ... 0xEF: // Video Stream
253 int length=getBits(16);
254 if ( (length+6) > minFrameLength )
257 if ( (minFrameLength+2048) > (length+6) )
258 minFrameLength=length+6;
259 else
260 minFrameLength+=2048;
261 eDebug("minFrameLength now %d", minFrameLength );
263 unsigned char buffer[6+length];
264 int p=0;
266 buffer[p++]=0;
267 buffer[p++]=0;
268 buffer[p++]=1;
269 buffer[p++]=code;
271 buffer[p++]=length>>8;
272 buffer[p++]=length&0xFF;
274 while ( length && remaining )
276 buffer[p++]=getBits(8);
277 --length;
280 if ( length )
282 int rd = input.read(buffer+p, length);
283 if ( rd != length )
284 { // buffer empty.. put all data back in input buffer
285 input.write(buffer, p+rd);
286 return written;
288 /* else
289 eDebug("read %04x bytes", length);*/
290 p+=length;
293 if ( code == 0xBD )
295 int offs = buffer[8];
296 int subid = buffer[8+offs+1];
298 // eDebug("offs = %02x, subid = %02x",offs, subid);
301 // if ( offs == 0x24 && subid == 0x10 ) // TTX stream...
302 // break;
304 if ( subid < 0x80 )
305 break;
307 // if ( subid > 0xA7 ) // 0xA0 .. 0xA7 (LPCM)
308 // break;
310 if ( subid > 0x87 ) // 0x88 .. 0x89 (DTS)
311 break;
313 for (int i=0; i < 32; ++i )
314 eDebugNoNewLine("%02x ", buffer[i]);
315 eDebug("");*/
317 // here we have subid 0x80 .. 0x87
318 code |= (subid << 8);
319 int &cnt = audiostreams[code];
320 if ( cnt < 10 )
322 cnt++;
323 if ( cnt == 10 )
325 // eDebug("found new AC3 stream subid %02x", subid);
326 // eDebug("/*emit*/ (*newastreamid)(%04x)", code);
327 if ( !curAudioStreamID )
329 Decoder::parms.audio_type = DECODE_AUDIO_AC3_VOB;
330 Decoder::Set();
331 curAudioStreamID = code;
333 /*emit*/ (*newastreamid)(code);
337 // check old audiopackets in syncbuffer
338 if ( syncbuffer.size() )
340 unsigned int VideoPTS=0xFFFFFFFF;
341 Decoder::getVideoPTS(VideoPTS);
342 if ( VideoPTS != 0xFFFFFFFF )
344 std::list<syncAudioPacket>::iterator it( syncbuffer.begin() );
345 for (;it != syncbuffer.end(); ++it )
347 if ( abs(VideoPTS - it->pts) <= 0x1000 )
349 eDebug("synced2 :)");
350 break;
353 if ( it != syncbuffer.end() )
355 synced=1;
356 // write data from syncbuffer to audio device
357 for (;it != syncbuffer.end(); ++it )
358 audio.write( it->data, it->len );
359 // cleanup syncbuffer
360 for (it=syncbuffer.begin();it != syncbuffer.end(); ++it )
361 delete [] it->data;
362 syncbuffer.clear();
366 if (code > 0xDF && code < 0xF0)
368 videostreams.insert(code);
369 if ( code != 0xE0 && videostreams.find(240) != videostreams.end() )
370 ; // dont play video streams != 0xE0 when 0xE0 is avail...
371 else
373 bool sent=false;
374 if ( synced==1 && sheader)
376 int pos = 6;
377 while(pos < p)
379 if ( buffer[pos++] )
380 continue;
381 if ( buffer[pos++] )
382 continue;
383 while ( !buffer[pos] )
384 pos++;
385 if ( buffer[pos++] != 1 )
386 continue;
387 if ( buffer[pos++] != 0xb8 ) // group start code
388 continue;
389 pos-=4; // before GROUP START
390 int length=(buffer[4]<<8)|buffer[5];
391 length+=sheader_len;
392 buffer[4]=length>>8;
393 buffer[5]=length&0xFF;
394 video.write(buffer, pos);
395 video.write(sheader, sheader_len);
396 video.write(buffer+pos, p-pos);
397 synced++;
398 sent=true;
399 break;
402 if ( !sent )
403 video.write(buffer, p);
404 written+=p;
407 else if ( code == curAudioStreamID )
409 // check current audiopacket
410 if (!synced)
412 unsigned int AudioPTS = 0xFFFFFFFF,
413 VideoPTS = 0xFFFFFFFF,
414 pos=5;
415 while( buffer[++pos] == 0xFF ); // stuffing überspringen
416 if ( (buffer[pos] & 0xC0) == 0x40 ) // buffer scale size
417 pos+=2;
418 if ( ((buffer[pos] & 0xF0) == 0x20) || //MPEG1
419 ((buffer[pos] & 0xF0) == 0x30) || //MPEG1
420 ((buffer[pos] & 0xC0) == 0x80) ) //MPEG2
422 int readPTS=1;
423 if ((buffer[pos] & 0xC0) == 0x80) // we must skip many bytes
425 if ((c & 0x30) != 0)
426 eDebug("warning encrypted multiplex not handled!!!");
427 ++pos; // flags
428 if ( ((buffer[pos]&0xC0) != 0x80) &&
429 ((buffer[pos]&0xC0) != 0xC0) )
430 readPTS=0;
431 pos+=2;
433 if (readPTS)
435 AudioPTS = (buffer[pos++] >> 1) << 29;
436 AudioPTS |= buffer[pos++] << 21;
437 AudioPTS |=(buffer[pos++] >> 1) << 14;
438 AudioPTS |= buffer[pos++] << 6;
439 AudioPTS |= buffer[pos] >> 2;
440 // eDebug("APTS %08x", AudioPTS);
443 Decoder::getVideoPTS(VideoPTS);
444 if ( VideoPTS != 0xFFFFFFFF && abs(VideoPTS - AudioPTS) <= 0x1000 )
446 synced=1;
447 eDebug("synced1 :)");
448 // cleanup syncbuffer.. we don't need content of it
449 std::list<syncAudioPacket>::iterator it( syncbuffer.begin() );
450 for (;it != syncbuffer.end(); ++it )
451 delete [] it->data;
452 syncbuffer.clear();
454 else if ( (AudioPTS > VideoPTS) || VideoPTS == 0xFFFFFFFF )
456 syncAudioPacket pack;
457 pack.pts = AudioPTS;
458 pack.len = p;
459 pack.data = new __u8[p];
460 memcpy( pack.data, buffer, pack.len );
461 syncbuffer.push_back( pack );
462 // eDebug("PTSA = %08x\nPTSV = %08x\nDIFF = %08x",
463 // AudioPTS, VideoPTS, abs(AudioPTS-VideoPTS) );
466 if ( synced )
467 audio.write(buffer, p);
468 written+=p;
470 break;
472 default:
474 if ( audio.size() || video.size() )
475 eDebug("unhandled code... but already data in buffers!!");
476 for (std::map<int,int>::iterator it(audiostreams.begin());
477 it != audiostreams.end();)
479 if ( it->second < 10 )
480 audiostreams.erase(it++);
481 else
482 ++it;
484 eDebug("unhandled code %02x", code);
489 finish:
490 return written;
493 int ePVADemux::decodeMore(int last, int maxsamples, Signal1<void,unsigned int>*newastreamid )
495 // eDebug("decodeMore");
496 int written=0;
497 (void)last;
499 while (written < maxsamples)
501 bool readedPTS=false;
502 unsigned char tmp[8];
503 unsigned char pts[4];
504 while (1) // search for start code.
506 if (input.size() < 4096)
508 maxsamples=0;
509 break;
511 syncBits();
513 tmp[0]=getBits(8);
515 if (tmp[0] != 0x41)
516 continue;
518 tmp[1]=getBits(8);
520 if (tmp[1] != 0x56)
521 continue;
523 tmp[2]=getBits(8);
524 tmp[3]=getBits(8);
525 tmp[4]=getBits(8);
527 if ( tmp[4] != 0x55 )
529 unsigned int cnt=0;
530 unsigned char backbuff[input.size()+4+3];
531 backbuff[cnt++]=tmp[2];
532 backbuff[cnt++]=tmp[3];
533 backbuff[cnt++]=tmp[4];
534 while (remaining)
535 backbuff[cnt++]=getBits(8);
536 cnt+=input.read(backbuff+cnt, input.size());
537 input.write(backbuff, cnt);
538 continue;
541 tmp[5]=getBits(8);
542 tmp[6]=getBits(8);
543 tmp[7]=getBits(8);
544 int length=tmp[6]<<8|tmp[7];
545 int rd=8;
547 unsigned int code = tmp[2];
549 if ( code == 1 && tmp[5] & 0x10 )
551 pts[0]=getBits(8);
552 pts[1]=getBits(8);
553 pts[2]=getBits(8);
554 pts[3]=getBits(8);
555 length-=4;
556 rd+=4;
557 readedPTS=true;
559 else
560 readedPTS=false;
562 if (code == 2)
563 code = 0xC0;
564 else if (code == 1)
565 code = 0xE0;
566 switch (code)
568 case 0x80 ... 0x87: // AC3
570 code <<= 8;
571 code |= 0xBD;
573 case 0xC0 ... 0xCF: // Audio Stream
574 case 0xD0 ... 0xDF:
576 int &cnt = audiostreams[code];
577 if ( cnt < 10 )
579 cnt++;
580 if ( cnt == 10 )
582 // eDebug("/*emit*/ (*newastreamid)(%02x)", code);
583 if ( !curAudioStreamID )
585 Decoder::parms.audio_type = (code&0xFF==0xBD) ?
586 DECODE_AUDIO_AC3_VOB : DECODE_AUDIO_MPEG;
587 Decoder::Set();
588 curAudioStreamID = code;
589 // eDebug("set %02x", code);
591 /*emit*/ (*newastreamid)(code);
595 case 0xE0 ... 0xEF: // Video Stream
597 if ( (length+rd) > minFrameLength )
599 if ( (minFrameLength+2048) > (length+rd) )
600 minFrameLength=length+rd;
601 else
602 minFrameLength+=2048;
603 eDebug("minFrameLength now %d", minFrameLength );
605 unsigned char buffer[length];
606 unsigned int p=0;
607 while ( length && remaining )
609 buffer[p++]=getBits(8);
610 --length;
612 if ( length )
614 int rd = input.read(buffer+p, length);
615 if ( rd != length )
616 { // buffer empty.. put all data back in input buffer
617 input.write(tmp, 8); // put pva header back into buffer
618 if (readedPTS) // pts read?
619 input.write(pts,4); // put pts bytes back into buffer
620 input.write(buffer, rd); // put all readed bytes back into buffer
621 return written;
623 // else
624 // eDebug("read %04x bytes", length);
625 p+=length;
628 // check old audiopackets in syncbuffer
629 unsigned int VideoPTS=0xFFFFFFFF;
630 Decoder::getVideoPTS(VideoPTS);
631 if ( syncbuffer.size() )
633 if ( VideoPTS != 0xFFFFFFFF )
635 std::list<syncAudioPacket>::iterator it( syncbuffer.begin() );
636 for (;it != syncbuffer.end(); ++it )
638 if ( abs(VideoPTS - it->pts) <= 0x1000 )
640 eDebug("synced2 :)");
641 break;
644 if ( it != syncbuffer.end() )
646 synced=1;
647 // write data from syncbuffer to audio device
648 for (;it != syncbuffer.end(); ++it )
649 audio.write( it->data, it->len );
650 // cleanup syncbuffer
651 for (it=syncbuffer.begin();it != syncbuffer.end(); ++it )
652 delete [] it->data;
653 syncbuffer.clear();
657 if (code > 0xDF && code < 0xF0)
659 videostreams.insert(code);
660 if ( code != 0xE0 && videostreams.find(240) != videostreams.end() )
661 ; // dont play video streams != 1 when 0xE0 is avail...
662 else
664 int headerPos=-1;
665 if ( synced==1 && sheader)
667 unsigned int pos = 0;
668 while(pos < p)
670 if ( buffer[pos++] )
671 continue;
672 if ( buffer[pos++] )
673 continue;
674 while ( !buffer[pos] )
675 pos++;
676 if ( buffer[pos++] != 1 )
677 continue;
678 if ( buffer[pos++] != 0xb8 ) // group start code
679 continue;
680 pos-=4; // before GROUP START
681 headerPos=pos;
682 break;
685 if ( readedPTS )
687 VideoPTS=pts[0]<<24|pts[1]<<16|pts[2]<<8|pts[3];
688 int payload_length = tmp[6]<<8 | tmp[7];
689 payload_length+=3;
690 if (headerPos!=-1)
691 payload_length+=sheader_len;
692 char buf[14] = { 0, 0, 1, code&0xFF,
693 (payload_length & 0xFF00)>>8,
694 payload_length&0xFF,
695 0x80, 0x80, 0x05,
696 0x20|((VideoPTS&0xC0000000)>>29)|1,
697 (VideoPTS&0x3FC00000)>>22,
698 ((VideoPTS&0x3F8000)>>14)|1,
699 (VideoPTS&0x7F80)>>7,
700 ((VideoPTS&0x7F)<<1)|1};
701 video.write(buf, 14);
703 else
705 int payload_length = tmp[6]<<8 | tmp[7];
706 payload_length+=3;
707 if (headerPos!=-1)
708 payload_length+=sheader_len;
709 char buf[9] = { 0, 0, 1, code&0xFF,
710 (payload_length & 0xFF00)>>8,
711 payload_length&0xFF,
712 0x80, 0, 0 };
713 video.write(buf, 9);
715 if (headerPos != -1)
717 video.write(buffer,headerPos);
718 video.write(sheader,sheader_len);
719 video.write(buffer+headerPos, p-headerPos);
720 synced++;
722 else
723 video.write(buffer, p);
724 written+=p;
727 else if ( code == curAudioStreamID )
729 // check current audiopacket
730 if (!synced)
732 unsigned int AudioPTS = 0xFFFFFFFF,
733 VideoPTS = 0xFFFFFFFF,
734 pos=5;
735 while( buffer[++pos] == 0xFF ); // stuffing überspringen
736 if ( (buffer[pos] & 0xC0) == 0x40 ) // buffer scale size
737 pos+=2;
738 if ( ((buffer[pos] & 0xF0) == 0x20) || //MPEG1
739 ((buffer[pos] & 0xF0) == 0x30) || //MPEG1
740 ((buffer[pos] & 0xC0) == 0x80) ) //MPEG2
742 int type=0;
743 int readPTS=1;
744 if ((buffer[pos] & 0xC0) == 0x80) // we must skip many bytes
746 type=1;
747 ++pos; // flags
748 if ( ((buffer[pos]&0xC0) != 0x80) &&
749 ((buffer[pos]&0xC0) != 0xC0) )
750 readPTS=0;
751 pos+=2;
753 if ( type != mpegtype )
755 if ( type == 1 )
756 Decoder::SetStreamType(TYPE_PES);
757 else
758 Decoder::SetStreamType(TYPE_MPEG1);
759 mpegtype=type;
760 // eDebug("set %s", type == 1 ? "MPEG-2" : "MPEG-1" );
762 if (readPTS)
764 AudioPTS = (buffer[pos++] >> 1) << 29;
765 AudioPTS |= buffer[pos++] << 21;
766 AudioPTS |=(buffer[pos++] >> 1) << 14;
767 AudioPTS |= buffer[pos++] << 6;
768 AudioPTS |= buffer[pos] >> 2;
769 // eDebug("APTS %08x", AudioPTS);
772 Decoder::getVideoPTS(VideoPTS);
773 // eDebug("VPTS %08x", VideoPTS);
774 if ( VideoPTS != 0xFFFFFFFF && abs(VideoPTS - AudioPTS) <= 0x1000 )
776 synced=1;
777 eDebug("synced1 :)");
778 // cleanup syncbuffer.. we don't need content of it
779 std::list<syncAudioPacket>::iterator it( syncbuffer.begin() );
780 for (;it != syncbuffer.end(); ++it )
781 delete [] it->data;
782 syncbuffer.clear();
784 else if ( (AudioPTS > VideoPTS) || VideoPTS == 0xFFFFFFFF )
786 syncAudioPacket pack;
787 pack.pts = AudioPTS;
788 pack.len = p;
789 pack.data = new __u8[p];
790 memcpy( pack.data, buffer, pack.len );
791 syncbuffer.push_back( pack );
792 // eDebug("PTSA = %08x\nPTSV = %08x\nDIFF = %08x",
793 // AudioPTS, VideoPTS, abs(AudioPTS-VideoPTS) );
796 if ( synced )
797 audio.write(buffer, p);
798 written+=p;
800 break;
802 default:
803 /*eDebug("unhandled code %02x", tmp[2])*/;
807 return written;
810 void eDemux::resync()
812 remaining=synced=0;
813 // clear syncbuffer
814 std::list<syncAudioPacket>::iterator it( syncbuffer.begin() );
815 for (;it != syncbuffer.end(); ++it )
816 delete [] it->data;
817 syncbuffer.clear();
820 int eDemux::getMinimumFramelength()
822 return minFrameLength;
825 int eDemux::getAverageBitrate()
827 return 3*1024*1024;
830 void eDemux::setAudioStream( unsigned int id )
832 if ( curAudioStreamID != id && audiostreams.find(id) != audiostreams.end() )
834 if ( (id&0xFF) == 0xBD)
836 // not thread safe !!
837 Decoder::parms.audio_type = DECODE_AUDIO_AC3_VOB;
838 Decoder::Set();
840 else
842 // not thread safe !!
843 Decoder::parms.audio_type = DECODE_AUDIO_MPEG;
844 Decoder::Set();
846 curAudioStreamID = id;
850 void eDemux::extractSequenceHeader( unsigned char *buffer, unsigned int len )
852 if (!sheader) // we have no sequence header
854 unsigned int pos = 0;
856 while(pos < len)
858 if ( buffer[pos++] )
859 continue;
860 if ( pos >=len )
861 return;
862 if ( buffer[pos++] )
863 continue;
864 if ( pos >=len )
865 return;
866 while ( !buffer[pos] )
868 pos++;
869 if ( pos >=len )
870 return;
872 if ( buffer[pos++] != 1 )
873 continue;
874 if ( pos >=len )
875 return;
876 if ( buffer[pos++] != 0xB3 )
877 continue;
878 if ( pos >=len )
879 return;
880 pos+=7;
881 if ( pos >=len )
882 return;
883 sheader_len=12;
884 if ( buffer[pos] & 2 ) // intra matrix avail?
886 pos+=64;
887 if ( pos >=len )
888 return;
889 sheader_len+=64;
891 if ( buffer[pos] & 1 ) // non intra matrix avail?
893 pos+=64;
894 if ( pos >=len )
895 return;
896 sheader_len+=64;
898 pos+=1;
899 if ( pos+3 >=len )
900 return;
901 // extended start code
902 if ( !buffer[pos] && !buffer[pos+1] && buffer[pos+2] && buffer[pos+3] == 0xB5 )
904 pos+=3;
905 sheader_len+=3;
908 pos+=1;
909 ++sheader_len;
910 if (pos+2 > len)
911 return;
913 while( buffer[pos] || buffer[pos+1] || !buffer[pos+2] );
915 if ( pos+3 >=len )
916 return;
917 if ( !buffer[pos] && !buffer[pos+1] && buffer[pos+2] && buffer[pos+3] == 0xB2 )
919 pos+=3;
920 sheader_len+=3;
923 pos+=1;
924 ++sheader_len;
925 if (pos+2 > len)
926 return;
928 while( buffer[pos] || buffer[pos+1] || !buffer[pos+2] );
930 sheader = new unsigned char[sheader_len];
931 memcpy(sheader, &buffer[pos-sheader_len], sheader_len);
932 // for (unsigned int i=0; i < sheader_len; ++i)
933 // eDebugNoNewLine("%02x", sheader[i]);
934 // eDebug("");
935 return;
940 #endif // DISABLE_FILE
942 // lpcm
943 // lpcm dvd
944 // frame rate 600Hz (48/96kHz)
945 // 16/20/24 bits
946 // 8ch
947 // max 6.144Mbps
948 // [0] private stream sub type
949 // [1] number_of_frame_headers
950 // [2-3] first_access_unit_pointer (offset from [3])
951 // [4] audio_frame_number
952 // (of first access unit (wraps at 19 ?))
953 // (20 frames at 1/600Hz = 1/30 (24 frames for 25fps?)
954 // [5]:
955 // b7-b6: quantization_word_length,
956 // 0: 16bits, 1: 20bits, 2: 24bits, 3: reserved
957 // b5: reserved
958 // b4: audio_sampling_frequency,
959 // 0: 48 kHz, 1: 96 kHz
960 // b3: reserved
961 // b2-b0: number_of_audio_channels
962 // 0: mono (2ch ? dual-mono: L=R)
963 // 1: 2ch (stereo)
964 // 2: 3 channel
965 // 3: 4 ch
966 // 4: 5 ch
967 // 5: 6 ch
968 // 6: 7 ch
969 // 7: 8 ch
970 // [6]: dynamic_range