3 #include "lib/codecs/codecmpg.h"
4 #include <lib/dvb/decoder.h>
5 #include <netinet/in.h>
9 #include <lib/base/eerror.h>
10 #include <lib/system/econfig.h>
12 unsigned long eDemux::getLong()
15 if (input
.read(&c
, 4) != 4)
17 eDebug("read error ! :))");
18 return 0x1b9; // simulate end of program stream.
30 unsigned long eDemux::getBits(unsigned int num
)
41 res
|=(last
>>(remaining
-d
))&~(-1<<d
);
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.
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)
63 memset(&pcmsettings
, 0, sizeof(pcmsettings
));
68 eConfig::getInstance()->setKey("/ezap/audio/prevAudioStreamID", curAudioStreamID
);
72 int eMPEGDemux::decodeMore(int last
, int maxsamples
, Signal1
<void,unsigned int>*newastreamid
)
74 // eDebug("decodeMore");
78 while (written
< maxsamples
)
81 while (1) // search for start code.
83 if (input
.size() < 4096)
104 case 0xb9: // MPEG_program_end_code
106 eDebug("program_end_code");
109 case 0xba: // pack_start_code
112 if ( type
!= mpegtype
)
117 Decoder::SetStreamType(TYPE_PES
);
120 Decoder::SetStreamType(TYPE_MPEG1
);
124 // eDebug("set %s", type == 1 ? "MPEG-2" : "MPEG-1" );
135 int scr_base0
, scr_base
, scr_ext
;
136 scr_base0
=getBits(3);
137 scr_base
=scr_base0
<<30;
141 scr_base
|=getBits(15)<<15;
144 scr_base
|=getBits(15);
150 /* int program_mux_rate= */ getBits(22);
154 int pack_stuffing_length
=getBits(3);
155 while (pack_stuffing_length
--)
156 if (getBits(8) != 0xFF)
158 if (pack_stuffing_length
>= 0)
160 // eDebug("scr: %08x:%02d\n", scr_base, scr_ext);
163 case 0xbb: // system_header_start_code
164 case 0xBE: // Padding Stream
165 case 0xBF: // Private Stream 2 (???)
172 // eDebug("system_header %02x", code);
173 int length
=getBits(16);
174 while ( length
&& remaining
)
182 if ( input
.read(buffer
, length
) != length
)
183 eDebug("read Error in skip");
187 case 0xbc: // program_stream_map
190 int program_stream_map_length
=getBits(16);
191 eDebug("program stream map!\n");
192 int current_next_indicator
=getBits(1);
194 int program_stream_map_version
=getBits(5);
198 int program_stream_info_length
=getBits(16);
199 for (int r
=0; r
<program_stream_info_length
; )
204 eDebug("tag: %02x %02x ", tag
, length
);
206 eDebug("%02lx ", getBits(8));
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
; )
218 int length
=getBits(8);
219 eDebug("elementary: %02x %02x ", tag
, length
);
221 eDebugNoNewLine("%02x ", getBits(8));
224 r
+=elementary_stream_info_length
+4;
230 case 0xC0 ... 0xCF: // Audio Stream
233 int &cnt
= audiostreams
[code
];
239 // eDebug("/*emit*/ (*newastreamid)(%02x)", code);
240 if ( !curAudioStreamID
)
242 Decoder::parms
.audio_type
= DECODE_AUDIO_MPEG
;
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;
260 minFrameLength
+=2048;
261 eDebug("minFrameLength now %d", minFrameLength
);
263 unsigned char buffer
[6+length
];
271 buffer
[p
++]=length
>>8;
272 buffer
[p
++]=length
&0xFF;
274 while ( length
&& remaining
)
276 buffer
[p
++]=getBits(8);
282 int rd
= input
.read(buffer
+p
, length
);
284 { // buffer empty.. put all data back in input buffer
285 input
.write(buffer
, p
+rd
);
289 eDebug("read %04x bytes", length);*/
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...
307 // if ( subid > 0xA7 ) // 0xA0 .. 0xA7 (LPCM)
310 if ( subid
> 0x87 ) // 0x88 .. 0x89 (DTS)
313 for (int i=0; i < 32; ++i )
314 eDebugNoNewLine("%02x ", buffer[i]);
317 // here we have subid 0x80 .. 0x87
318 code
|= (subid
<< 8);
319 int &cnt
= audiostreams
[code
];
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
;
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 :)");
353 if ( it
!= syncbuffer
.end() )
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
)
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...
374 if ( synced
==1 && sheader
)
383 while ( !buffer
[pos
] )
385 if ( buffer
[pos
++] != 1 )
387 if ( buffer
[pos
++] != 0xb8 ) // group start code
389 pos
-=4; // before GROUP START
390 int length
=(buffer
[4]<<8)|buffer
[5];
393 buffer
[5]=length
&0xFF;
394 video
.write(buffer
, pos
);
395 video
.write(sheader
, sheader_len
);
396 video
.write(buffer
+pos
, p
-pos
);
403 video
.write(buffer
, p
);
407 else if ( code
== curAudioStreamID
)
409 // check current audiopacket
412 unsigned int AudioPTS
= 0xFFFFFFFF,
413 VideoPTS
= 0xFFFFFFFF,
415 while( buffer
[++pos
] == 0xFF ); // stuffing überspringen
416 if ( (buffer
[pos
] & 0xC0) == 0x40 ) // buffer scale size
418 if ( ((buffer
[pos
] & 0xF0) == 0x20) || //MPEG1
419 ((buffer
[pos
] & 0xF0) == 0x30) || //MPEG1
420 ((buffer
[pos
] & 0xC0) == 0x80) ) //MPEG2
423 if ((buffer
[pos
] & 0xC0) == 0x80) // we must skip many bytes
426 eDebug("warning encrypted multiplex not handled!!!");
428 if ( ((buffer
[pos
]&0xC0) != 0x80) &&
429 ((buffer
[pos
]&0xC0) != 0xC0) )
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 )
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
)
454 else if ( (AudioPTS
> VideoPTS
) || VideoPTS
== 0xFFFFFFFF )
456 syncAudioPacket pack
;
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) );
467 audio
.write(buffer
, p
);
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
++);
484 eDebug("unhandled code %02x", code
);
493 int ePVADemux::decodeMore(int last
, int maxsamples
, Signal1
<void,unsigned int>*newastreamid
)
495 // eDebug("decodeMore");
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)
527 if ( tmp
[4] != 0x55 )
530 unsigned char backbuff
[input
.size()+4+3];
531 backbuff
[cnt
++]=tmp
[2];
532 backbuff
[cnt
++]=tmp
[3];
533 backbuff
[cnt
++]=tmp
[4];
535 backbuff
[cnt
++]=getBits(8);
536 cnt
+=input
.read(backbuff
+cnt
, input
.size());
537 input
.write(backbuff
, cnt
);
544 int length
=tmp
[6]<<8|tmp
[7];
547 unsigned int code
= tmp
[2];
549 if ( code
== 1 && tmp
[5] & 0x10 )
568 case 0x80 ... 0x87: // AC3
573 case 0xC0 ... 0xCF: // Audio Stream
576 int &cnt
= audiostreams
[code
];
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
;
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
;
602 minFrameLength
+=2048;
603 eDebug("minFrameLength now %d", minFrameLength
);
605 unsigned char buffer
[length
];
607 while ( length
&& remaining
)
609 buffer
[p
++]=getBits(8);
614 int rd
= input
.read(buffer
+p
, 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
624 // eDebug("read %04x bytes", 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 :)");
644 if ( it
!= syncbuffer
.end() )
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
)
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...
665 if ( synced
==1 && sheader
)
667 unsigned int pos
= 0;
674 while ( !buffer
[pos
] )
676 if ( buffer
[pos
++] != 1 )
678 if ( buffer
[pos
++] != 0xb8 ) // group start code
680 pos
-=4; // before GROUP START
687 VideoPTS
=pts
[0]<<24|pts
[1]<<16|pts
[2]<<8|pts
[3];
688 int payload_length
= tmp
[6]<<8 | tmp
[7];
691 payload_length
+=sheader_len
;
692 char buf
[14] = { 0, 0, 1, code
&0xFF,
693 (payload_length
& 0xFF00)>>8,
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);
705 int payload_length
= tmp
[6]<<8 | tmp
[7];
708 payload_length
+=sheader_len
;
709 char buf
[9] = { 0, 0, 1, code
&0xFF,
710 (payload_length
& 0xFF00)>>8,
717 video
.write(buffer
,headerPos
);
718 video
.write(sheader
,sheader_len
);
719 video
.write(buffer
+headerPos
, p
-headerPos
);
723 video
.write(buffer
, p
);
727 else if ( code
== curAudioStreamID
)
729 // check current audiopacket
732 unsigned int AudioPTS
= 0xFFFFFFFF,
733 VideoPTS
= 0xFFFFFFFF,
735 while( buffer
[++pos
] == 0xFF ); // stuffing überspringen
736 if ( (buffer
[pos
] & 0xC0) == 0x40 ) // buffer scale size
738 if ( ((buffer
[pos
] & 0xF0) == 0x20) || //MPEG1
739 ((buffer
[pos
] & 0xF0) == 0x30) || //MPEG1
740 ((buffer
[pos
] & 0xC0) == 0x80) ) //MPEG2
744 if ((buffer
[pos
] & 0xC0) == 0x80) // we must skip many bytes
748 if ( ((buffer
[pos
]&0xC0) != 0x80) &&
749 ((buffer
[pos
]&0xC0) != 0xC0) )
753 if ( type
!= mpegtype
)
756 Decoder::SetStreamType(TYPE_PES
);
758 Decoder::SetStreamType(TYPE_MPEG1
);
760 // eDebug("set %s", type == 1 ? "MPEG-2" : "MPEG-1" );
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 )
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
)
784 else if ( (AudioPTS
> VideoPTS
) || VideoPTS
== 0xFFFFFFFF )
786 syncAudioPacket pack
;
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) );
797 audio
.write(buffer
, p
);
803 /*eDebug("unhandled code %02x", tmp[2])*/;
810 void eDemux::resync()
814 std::list
<syncAudioPacket
>::iterator
it( syncbuffer
.begin() );
815 for (;it
!= syncbuffer
.end(); ++it
)
820 int eDemux::getMinimumFramelength()
822 return minFrameLength
;
825 int eDemux::getAverageBitrate()
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
;
842 // not thread safe !!
843 Decoder::parms
.audio_type
= DECODE_AUDIO_MPEG
;
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;
866 while ( !buffer
[pos
] )
872 if ( buffer
[pos
++] != 1 )
876 if ( buffer
[pos
++] != 0xB3 )
884 if ( buffer
[pos
] & 2 ) // intra matrix avail?
891 if ( buffer
[pos
] & 1 ) // non intra matrix avail?
901 // extended start code
902 if ( !buffer
[pos
] && !buffer
[pos
+1] && buffer
[pos
+2] && buffer
[pos
+3] == 0xB5 )
913 while( buffer
[pos
] || buffer
[pos
+1] || !buffer
[pos
+2] );
917 if ( !buffer
[pos
] && !buffer
[pos
+1] && buffer
[pos
+2] && buffer
[pos
+3] == 0xB2 )
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]);
940 #endif // DISABLE_FILE
944 // frame rate 600Hz (48/96kHz)
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?)
955 // b7-b6: quantization_word_length,
956 // 0: 16bits, 1: 20bits, 2: 24bits, 3: reserved
958 // b4: audio_sampling_frequency,
959 // 0: 48 kHz, 1: 96 kHz
961 // b2-b0: number_of_audio_channels
962 // 0: mono (2ch ? dual-mono: L=R)
970 // [6]: dynamic_range