2 Copyright 2005-2008 Mark Calderbank
3 Copyright 2007 Marten Richter (AC3 support)
5 This file is part of VOMP.
7 VOMP is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 VOMP is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with VOMP; if not, write to the Free Software Foundation, Inc.,
19 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
25 #include "dvbsubtitles.h"
33 #define DEMUXER_SEQ_HEAD 0x000001B3
34 #define DEMUXER_PIC_HEAD 0x00000101
35 #define DEMUXER_SEQ_EXT_HEAD 0x000001B5
37 #define DEMUXER_H264_ACCESS_UNIT 0x00000109
38 #define DEMUXER_H264_SEQ_PARAMETER_SET 0x00000107
39 #define DEMUXER_H264_SUB_ENHANCEMENT_INF 0x00000106
40 #define DEMUXER_H264_CODED_SLICE_IDR 0x00000105
41 #define DEMUXER_H264_CODED_SLICE_NON_IDR 0x00000101
43 #define SEEK_THRESHOLD 150000 // About 1.5 seconds
46 const int Demuxer::FrameRates[9] = { 0, 23, 24, 25, 29, 30, 50, 59, 60 };
47 Demuxer* Demuxer::instance = NULL;
51 NALUUnit(const UCHAR* buf,UINT length_buf);
54 inline UINT getBits(UINT num_bits);
57 bool isEonalu() {return eonalu;};
58 int getPos(){return pos;};
70 NALUUnit::NALUUnit(const UCHAR *buf, UINT length_buf)
82 UINT pattern =(((UINT)buf[ 0] << 16) |
86 while (pattern != 0x000001)
88 if (++nalu_start >= length_buf) return;
89 pattern = ((pattern << 8) | buf[nalu_start])&0x00FFFFFF;
91 nalu_end=nalu_start+1;
92 if (nalu_end >= length_buf) return; // input buffer too small. corrupt data? ignore.
93 pattern = ((pattern << 8) | buf[nalu_end])&0x00FFFFFF;
95 while (pattern != 0x000001 && pattern != 0x000000)
97 if (++nalu_end >= length_buf) { nalu_end+=3;break;};
98 pattern = ((pattern << 8) | buf[nalu_end])&0x00FFFFFF;
101 nalu_end=std::min(length_buf-1,nalu_end);
102 if (nalu_end <= nalu_start) return; // input buffer too small. corrupt data? ignore.
103 nalu_length=nalu_end-nalu_start;
104 nalu_buf=(UCHAR*)malloc(nalu_length);
105 memcpy(nalu_buf,buf+nalu_start,nalu_length);
109 NALUUnit::~NALUUnit()
111 if (nalu_buf) free(nalu_buf);
114 inline UINT NALUUnit::getBits(UINT num_bits)
116 if (num_bits==0) return 0; //???
117 UINT remain_bits=num_bits;
119 //May be slow, but should work!
120 while (remain_bits>0) {
124 last_bytes=(last_bytes<<8) & nalu_buf[pos];
125 if ((last_bytes & 0x00FFFFFF) == 0x000003) pos++; //emulation prevention byte
128 working_byte=nalu_buf[pos];
144 UINT fetch_bits=std::min(remain_bits, static_cast<UINT>(8-bit_pos));
145 work=work <<fetch_bits;
146 //work|=((working_byte>>bit_pos) & (0xFF>>(8-fetch_bits)));
147 work|=(working_byte &(0xFF>>(bit_pos)))>>(8-fetch_bits-bit_pos);
148 remain_bits-=fetch_bits;
149 bit_pos=(bit_pos+fetch_bits)%8;
154 UINT NALUUnit::getUe()
158 for( bit = 0; !bit && !eonalu; leadbits++ )
160 if (eonalu) return 0;
161 return ((1 << leadbits)-1)+getBits(leadbits);
164 int NALUUnit::getSe()
167 if (input==0) return 0;
168 int output=((input+1)>>1);
169 if (input & 0x1) output*=-1;
175 static const int PESPacket_initial_size = 2000;
178 PESPacket::PESPacket()
180 data_size = PESPacket_initial_size;
181 data = (UCHAR*)malloc(data_size);
188 PESPacket::PESPacket(const PESPacket& packet)
193 PESPacket& PESPacket::operator=(const PESPacket& packet)
197 if (data) free(data);
203 PESPacket::~PESPacket()
205 if (data) free(data);
208 void PESPacket::copyFrom(const PESPacket& packet)
210 length = packet.length;
212 packetType = packet.packetType;
213 substream = packet.substream;
214 seq_header = packet.seq_header;
216 data = (UCHAR*)malloc(data_size);
217 memcpy(data, packet.data, data_size);
220 void PESPacket::init(UCHAR type, UCHAR sub)
224 data[4] = data[5] = 0;
228 seq_header = 1; // Unknown seq_header status
232 void PESPacket::truncate()
234 init(packetType,substream);
239 int PESPacket::write(const UCHAR *buf, int len)
243 if (size + len > 0x10000) return 0;
244 if (size + len > data_size)
246 UINT new_data_size = std::max(data_size + data_size / 2, data_size + len);
247 if (new_data_size > 0x10000) new_data_size = 0x10000;
248 data_size = new_data_size;
249 data = (UCHAR*)realloc(data, data_size);
251 memcpy(data + size, buf, len);
254 data[4] = (length >> 8);
255 data[5] = (length & 0xFF);
256 // We have added data - reset seq_header indicator if necessary
257 if (seq_header == 0) seq_header = 1; // Reset to 'unknown'
261 ULLONG PESPacket::getPTS() const
263 if ( ( (packetType >= Demuxer::PESTYPE_AUD0 &&
264 packetType <= Demuxer::PESTYPE_AUDMAX)
266 (packetType >= Demuxer::PESTYPE_VID0 &&
267 packetType <= Demuxer::PESTYPE_VIDMAX)
269 packetType == Demuxer::PESTYPE_PRIVATE_1
271 && size >= 14 && data[7] & 0x80)
273 return ( (ULLONG)(data[ 9] & 0x0E) << 29) |
274 ( (ULLONG)(data[10]) << 22 ) |
275 ( (ULLONG)(data[11] & 0xFE) << 14 ) |
276 ( (ULLONG)(data[12]) << 7 ) |
277 ( (ULLONG)(data[13] & 0xFE) >> 1 );
279 else return PTS_INVALID;
282 UCHAR PESPacket::operator[] (UINT index) const
290 UINT PESPacket::findPictureHeader(bool h264) const
292 if (size < 12) return 0;
293 UINT pattern = ( ((UINT)data[ 8] << 24) |
294 ((UINT)data[ 9] << 16) |
295 ((UINT)data[10] << 8) |
300 while (pattern != DEMUXER_H264_ACCESS_UNIT)
302 if (++pos >= size) return 0;
303 pattern = (pattern << 8) | data[pos];
307 while (pattern != DEMUXER_PIC_HEAD)
309 if (++pos >= size) return 0;
310 pattern = (pattern << 8) | data[pos];
316 UINT PESPacket::countPictureHeaders(bool h264, struct PictCountInfo& pinfo) const
318 if (size < 12) return 0;
319 UINT pattern = ( ((UINT)data[ 8] << 24) |
320 ((UINT)data[ 9] << 16) |
321 ((UINT)data[10] << 8) |
326 //inspired by vdr algorithm for frame couting by Klaus Schmidinger
330 pattern = (pattern << 8) | data[pos];
331 if ((pattern &0xFFFFFF00)==0x00000100) {
332 UINT testpattern=(pattern& 0xFFFFFF1f);
333 if (testpattern==DEMUXER_H264_ACCESS_UNIT) pinfo.hasaccessunit=true;
334 else if (testpattern==DEMUXER_H264_SEQ_PARAMETER_SET ) {
336 NALUUnit nalu(data+pos-3,getSize()-pos+3);
337 int profile=nalu.getBits(8);
338 nalu.getBits(8); //constraints
339 nalu.getBits(8); //level_idc
340 nalu.getUe(); //seq_parameter_set_id
342 pinfo.separate_color_plane_flag=0;
343 if (profile==100 || profile==110 || profile==122 || profile==144)
348 pinfo.separate_color_plane_flag=nalu.getBits(1);
350 nalu.getUe(); //bit depth lume
351 nalu.getUe(); //bit depth chrome
355 for (int i=0;i<8;i++){
362 for (int j=0;j<16;j++) {
364 UINT delta=nalu.getSe();
365 nextscale=(lastscale+delta+256)%256;
367 lastscale=(nextscale==0)?lastscale:nextscale;
374 for (int j=0;j<64;j++) {
376 UINT delta=nalu.getSe();
377 nextscale=(lastscale+delta+256)%256;
379 lastscale=(nextscale==0)?lastscale:nextscale;
387 UINT checkMaxFrameNum = nalu.getUe() + 4; //log2framenum
389 if (checkMaxFrameNum < 13) {
390 pinfo.log2_max_frame_num = checkMaxFrameNum;
398 UINT temp=nalu.getUe();
399 if (temp==0) //pict order
405 UINT temp2=nalu.getUe();
406 for (UINT i=0;i<temp2;i++)
409 nalu.getUe(); //Num refframes
413 pinfo.frame_mbs_only_flag=nalu.getBits(1);
417 } if (testpattern==DEMUXER_H264_CODED_SLICE_IDR || testpattern==DEMUXER_H264_CODED_SLICE_NON_IDR ) {
418 /* Log::getInstance()->log("Demuxer", Log::ERR,
419 "Has slice %d %d %d %d %d",pinfo.hasaccessunit, pinfo.hassps,pinfo.frame_mbs_only_flag,pinfo.separate_color_plane_flag,
420 pinfo.log2_max_frame_num);*/
421 if (pinfo.hasaccessunit && pinfo.hassps) {
423 NALUUnit nalu(data+pos-3,getSize()-pos+3);
424 nalu.getUe();// first_mb_in_slice
425 nalu.getUe();//sliectype
426 if (!pinfo.frame_mbs_only_flag) {
427 nalu.getUe(); //pic_paramter_set_id
428 if (pinfo.separate_color_plane_flag) nalu.getBits(2);
429 nalu.getBits(pinfo.log2_max_frame_num);
430 //if (!frame_mbs_only_flag)
431 if (nalu.getBits(1)) { //field_picture
432 if (!nalu.getBits(1)) { //bottom_field
440 pinfo.hasaccessunit=false;
450 pattern = (pattern << 8) | data[pos];
451 if (pattern==DEMUXER_PIC_HEAD) count++;
457 UINT PESPacket::findSeqHeader(bool h264) const
459 if (seq_header != 1) return seq_header;
460 if (size < 12) return 0;
461 UINT pattern = ( ((UINT)data[ 8] << 24) |
462 ((UINT)data[ 9] << 16) |
463 ((UINT)data[10] << 8) |
467 while ((pattern & 0xFFFFFF1F) != DEMUXER_H264_SEQ_PARAMETER_SET)
474 pattern = (pattern << 8) | data[pos];
476 seq_header = pos - 3;
480 while (pattern != DEMUXER_SEQ_HEAD)
487 pattern = (pattern << 8) | data[pos];
489 seq_header = pos - 3;
494 UINT PESPacket::findSeqExtHeader(bool h264) const
496 if (seq_header != 1) return seq_header;
497 if (size < 12) return 0;
498 UINT pattern = ( ((UINT)data[ 8] << 24) |
499 ((UINT)data[ 9] << 16) |
500 ((UINT)data[10] << 8) |
504 while ((pattern & 0xFFFFFF1F) != DEMUXER_H264_SUB_ENHANCEMENT_INF)
511 pattern = (pattern << 8) | data[pos];
513 seq_header = pos - 3;
517 while (pattern != DEMUXER_SEQ_EXT_HEAD && (data[pos+1]&0xf0)!=0x10)
519 if (++pos >= (size-1))
524 pattern = (pattern << 8) | data[pos];
526 seq_header = pos - 3;
534 if (instance) return;
539 vid_seeking = aud_seeking = false;
540 video_pts = audio_pts = 0;
541 ispre_1_3_19 = false;
555 Demuxer* Demuxer::getInstance()
560 int Demuxer::init(Callback* tcallback, DrainTarget* audio, DrainTarget* video, DrainTarget* teletext,
561 ULONG demuxMemoryV, ULONG demuxMemoryA, ULONG demuxMemoryT,double infps, DVBSubtitles* tsubtitles)
565 if ( !videostream.init(video, demuxMemoryV) ||
566 !audiostream.init(audio, demuxMemoryA) ||
567 !teletextstream.init(teletext, demuxMemoryT))
569 Log::getInstance()->log("Demuxer", Log::CRIT,
570 "Failed to initialize demuxer");
576 isteletextdecoded = true;
578 isteletextdecoded = false;
583 subtitles = tsubtitles;
584 callback = tcallback;
588 void Demuxer::reset()
590 Log::getInstance()->log("Demuxer", Log::DEBUG, "Reset called");
592 video_current = audio_current = teletext_current = subtitle_current = -1;
593 horizontal_size = vertical_size = 0;
594 interlaced=true; // default is true
595 aspect_ratio = (enum AspectRatio) 0;
598 frame_rate = bit_rate = 0;
599 ispre_1_3_19 = false;
605 for (int i = 0; i <= (PESTYPE_AUDMAX - PESTYPE_AUD0); i++)
607 avail_mpaudchan[i] = false;
609 for (int i = 0; i <= (PESTYPE_SUBSTREAM_AC3MAX - PESTYPE_SUBSTREAM_AC30); i++)
611 avail_ac3audchan[i] = false;
613 for (int i = 0; i <= (PESTYPE_SUBSTREAM_DVBSUBTITLEMAX - PESTYPE_SUBSTREAM_DVBSUBTITLE0); i++)
615 avail_dvbsubtitlechan[i] = false;
619 int Demuxer::shutdown()
621 videostream.shutdown();
622 audiostream.shutdown();
623 teletextstream.shutdown();
628 void Demuxer::flush()
630 Log::getInstance()->log("Demuxer", Log::DEBUG, "Flush called");
634 teletextstream.flush();
637 void Demuxer::flushAudio()
644 vid_seeking = aud_seeking = true;
645 video_pts = audio_pts = teletext_pts = 0;
648 void Demuxer::setAudioStream(int id)
653 void Demuxer::setVideoStream(int id)
658 void Demuxer::setTeletextStream(int id)
660 teletext_current = id;
663 void Demuxer::setDVBSubtitleStream(int id)
665 subtitle_current = id;
668 void Demuxer::setAspectRatio(enum AspectRatio ar, int taspectx, int taspecty)
670 if ((aspect_ratio != ar) || (parx != taspectx) || (pary != taspecty) )
672 Log::getInstance()->log("Demux", Log::DEBUG,
673 "Aspect ratio difference signalled");
674 if (++arcnt > 3) // avoid changing aspect ratio if glitch in signal
680 if (callback) callback->call(this);
687 bool Demuxer::writeAudio(bool * dataavail)
689 return audiostream.drain(dataavail);
692 bool Demuxer::writeVideo(bool * dataavail)
694 return videostream.drain(dataavail);
697 bool Demuxer::writeTeletext(bool * dataavail)
699 return teletextstream.drain(dataavail);
702 bool Demuxer::submitPacket(PESPacket& packet)
705 UCHAR packet_type = packet.getPacketType();
706 const UCHAR* packetdata = packet.getData();
707 if (packet_type >= PESTYPE_VID0 && packet_type <= PESTYPE_VIDMAX)
709 if (video_current == -1) video_current = packet_type;
710 if (video_current == packet_type && !vid_seeking)
712 sent = videostream.put(&packetdata[0], packet.getSize(), h264?MPTYPE_VIDEO_H264:MPTYPE_VIDEO_MPEG2,packetnum);
713 if (sent) packetnum++;
716 sent = packet.getSize();
718 else if (packet_type >= PESTYPE_AUD0 && packet_type <= PESTYPE_AUDMAX)
721 if (audio_current == -1) audio_current = packet_type;
722 avail_mpaudchan[packet_type - PESTYPE_AUD0] = true;
723 if (audio_current == packet_type && !aud_seeking)
725 UCHAR type=MPTYPE_MPEG_AUDIO;
730 type=MPTYPE_MPEG_AUDIO; break;
732 type=MPTYPE_AAC_LATM; break;
734 sent = audiostream.put(&packetdata[0], packet.getSize(), type,packetnum);
735 if (sent) packetnum++;
738 sent = packet.getSize();
740 else if (packet_type == PESTYPE_PRIVATE_1 &&
741 packet.getSubstream() >= PESTYPE_SUBSTREAM_AC30 &&
742 packet.getSubstream() <= PESTYPE_SUBSTREAM_AC3MAX)
744 avail_ac3audchan[packet.getSubstream() - PESTYPE_SUBSTREAM_AC30] = true;
745 if (packet.getSubstream() == audio_current)
747 sent = audiostream.put(&packetdata[0], packet.getSize(), (ispre_1_3_19 || livetv)? MPTYPE_AC3_PRE13 : MPTYPE_AC3,packetnum);
748 if (sent) packetnum++;
752 sent = packet.getSize();
755 else if (packet_type == PESTYPE_PRIVATE_1 &&
756 packet.getSubstream() >= PESTYPE_SUBSTREAM_DVBSUBTITLE0 &&
757 packet.getSubstream() <= PESTYPE_SUBSTREAM_DVBSUBTITLEMAX)
759 avail_dvbsubtitlechan[packet.getSubstream()-PESTYPE_SUBSTREAM_DVBSUBTITLE0]=true;
760 if (subtitle_current == -1) subtitle_current = packet.getSubstream();
761 if (subtitles && packet.getSubstream()==subtitle_current)
763 subtitles->put(packet);
765 sent = packet.getSize();
767 else if (isteletextdecoded && packet_type == PESTYPE_PRIVATE_1 &&
768 packet.getSubstream() >= PESTYPE_SUBSTREAM_TELETEXT0 &&
769 packet.getSubstream() <= PESTYPE_SUBSTREAM_TELETEXTMAX)
772 if (teletext_current == -1) teletext_current = packet.getSubstream();
773 if (teletext_current == packet.getSubstream())
775 sent = teletextstream.put(&packetdata[0], packet.getSize(), MPTYPE_TELETEXT,packetnum);
779 sent = packet.getSize();
784 sent = packet.getSize();
787 if (sent < packet.getSize()) // Stream is full.
793 void Demuxer::parsePacketDetails(PESPacket& packet)
795 if (packet.getPacketType() >= PESTYPE_AUD0 &&
796 packet.getPacketType() <= PESTYPE_AUDMAX)
798 // Extract audio PTS if it exists
801 audio_pts = packet.getPTS();
802 // We continue to seek on the audio if the video PTS that we
803 // are trying to match is ahead of the audio PTS by at most
804 // SEEK_THRESHOLD. We consider the possibility of PTS wrap.
805 if (aud_seeking && !vid_seeking &&
806 !( (video_pts_seek > audio_pts &&
807 video_pts_seek - audio_pts < SEEK_THRESHOLD)
809 (video_pts_seek < audio_pts &&
810 video_pts_seek + (1LL<<33) - audio_pts < SEEK_THRESHOLD) ))
813 Log::getInstance()->log("Demuxer", Log::DEBUG,
814 "Leaving audio sync: Audio PTS = %llu", audio_pts);
818 else if (packet.getPacketType() == PESTYPE_PRIVATE_1) // Private stream
821 //Inspired by vdr's device.c
822 int payload_begin = packet[8]+9;
823 unsigned char substream_id = packet[payload_begin];
824 unsigned char substream_type = substream_id & 0xF0;
825 unsigned char substream_index = substream_id & 0x1F;
826 pre_1_3_19_Recording: //This is for old recordings stuff and live TV
829 int old_substream=packet.getSubstream();
830 if (old_substream){ //someone else already set it, this is live tv
831 substream_id = old_substream;
832 substream_type = substream_id & 0xF0;
833 substream_index = substream_id & 0x1F;
835 substream_id = PESTYPE_PRIVATE_1;
836 substream_type = 0x80;
841 switch (substream_type)
845 packet.setSubstream(substream_id);
847 case 0xA0: //LPCM //not supported yet, is there any LPCM transmissio out there?
849 case 0x80: //ac3, currently only one ac3 track per recording supported
850 packet.setSubstream(substream_type+substream_index);
852 // Extract audio PTS if it exists
855 audio_pts = packet.getPTS();
856 // We continue to seek on the audio if the video PTS that we
857 // are trying to match is ahead of the audio PTS by at most
858 // SEEK_THRESHOLD. We consider the possibility of PTS wrap.
859 if (aud_seeking && !vid_seeking &&
860 !( (video_pts_seek > audio_pts &&
861 video_pts_seek - audio_pts < SEEK_THRESHOLD)
863 (video_pts_seek < audio_pts &&
864 video_pts_seek + (1LL<<33) - audio_pts < SEEK_THRESHOLD) ))
867 Log::getInstance()->log("Demuxer", Log::DEBUG, "Leaving audio sync: Audio PTS = %llu", audio_pts);
871 case 0x10: //Teletext Is this correct?
872 packet.setSubstream(substream_id);
873 // Extract teletxt PTS if it exists
876 teletext_pts = packet.getPTS();
882 ispre_1_3_19=true; //switching to compat mode and live tv mode
883 goto pre_1_3_19_Recording;
887 packet.setSubstream(0);
893 else if (packet.getPacketType() >= PESTYPE_VID0 &&
894 packet.getPacketType() <= PESTYPE_VIDMAX)
896 // Extract video PTS if it exists
897 if (packet.hasPTS()) video_pts = packet.getPTS();
899 // If there is a sequence header, extract information
900 UINT pos = packet.findSeqHeader(h264);
905 if (pos+6 >= packet.getSize()) return;
906 horizontal_size = ((int)packet[pos] << 4) | ((int)packet[pos+1] >> 4);
908 vertical_size = (((int)packet[pos+1] & 0xf) << 8) | (int)packet[pos+2];
910 enum AspectRatio aspect=(enum AspectRatio)(packet[pos+3] >> 4);
913 const int aspectDAR[]={0,1,1,1,4,3,16,9,221,100};
914 int aspectDARx=aspectDAR[aspect*2];
915 int aspectDARy=aspectDAR[aspect*2+1];
917 aspectx=aspectDARx*vertical_size;
918 aspecty=aspectDARy*horizontal_size;
922 // Log::getInstance()->log("Demuxer", Log::DEBUG, "PAR test1 %d %d %d %d ", aspectx,aspecty,commona,commonb);
928 commonb=commona%commonb;
931 aspectx=aspectx/commona;
932 aspecty=aspecty/commona;
933 //Log::getInstance()->log("Demuxer", Log::DEBUG, "PAR test2 %d %d %d %d %d %d %d", aspectx,aspecty,aspectDARx,aspectDARy,horizontal_size,vertical_size,commona);
935 setAspectRatio(aspect,aspectx,aspecty);
936 frame_rate = packet[pos+3] & 0x0f;
937 if (frame_rate >= 1 && frame_rate <= 8)
938 frame_rate = FrameRates[frame_rate];
941 bit_rate = ((int)packet[pos+4] << 10) |
942 ((int)packet[pos+5] << 2) |
943 ((int)packet[pos+6] >> 6);
947 /* Chris and Mark I know this is ugly, should we move this to a method of PESPacket or to NALUUnit what would be better?
948 This looks so ugly since the header includes variable length parts and I have to parse through the whole header to get the wanted information*/
949 NALUUnit nalu(packet.getData()+pos,packet.getSize()-pos);
950 profile=nalu.getBits(8);
951 nalu.getBits(8); //constraints
952 nalu.getBits(8); //level_idc
953 nalu.getUe(); //seq_parameter_set_id
955 if (profile==100 || profile==110 || profile==122 || profile==144)
962 nalu.getUe(); //bit depth lume
963 nalu.getUe(); //bit depth chrome
967 for (int i=0;i<8;i++){
974 for (int j=0;j<16;j++) {
976 UINT delta=nalu.getSe();
977 nextscale=(lastscale+delta+256)%256;
979 lastscale=(nextscale==0)?lastscale:nextscale;
986 for (int j=0;j<64;j++) {
988 UINT delta=nalu.getSe();
989 nextscale=(lastscale+delta+256)%256;
991 lastscale=(nextscale==0)?lastscale:nextscale;
1002 chromunitx=chromunity=1; break;
1004 chromunitx=chromunity=2; break;
1006 chromunitx=2;chromunity=1; break;
1008 chromunitx=chromunity=1; break;
1011 nalu.getUe(); //log2framenum
1012 UINT temp=nalu.getUe();
1013 if (temp==0) //pict order
1019 UINT temp2=nalu.getUe();
1020 for (UINT i=0;i<temp2;i++)
1023 nalu.getUe(); //Num refframes
1025 horizontal_size=(nalu.getUe()+1)*16;
1027 vertical_size=(nalu.getUe()+1)*16;
1028 int tinterlaced=nalu.getBits(1);
1029 vertical_size*=(2-tinterlaced);
1030 interlaced=!tinterlaced;
1032 if (!tinterlaced) nalu.getBits(1);
1034 if (nalu.getBits(1))
1036 horizontal_size-=nalu.getUe()*chromunitx;
1037 horizontal_size-=nalu.getUe()*chromunitx;
1038 vertical_size-=nalu.getUe()*(2-tinterlaced)*chromunity;
1039 vertical_size-=nalu.getUe()*(2-tinterlaced)*chromunity;
1041 if (nalu.getBits(1))
1043 if (nalu.getBits(1))
1045 UINT aspectratioidc=nalu.getBits(8);
1046 bool hasaspect=false;
1047 int aspectx,aspecty;
1048 const float aspects[]={1., 1./1.,12./11.,10./11.,16./11.,40./33.,
1049 24./11.,20./11.,32./11.,80./33.,18./11.,15./11.,64./33.,160./99.,4./3.,3./2.,2./1.};
1050 const int aspectsx[]={1, 1,12,10,16,40,
1051 24,20,32,80,18,15,64,160,4,3,2};
1052 const int aspectsy[]={1, 1,11,11,11,33,11,11,11,33,11,11,33,99,3,2,1};
1054 float aspectratio=((float) horizontal_size)/((float) vertical_size);
1055 if (aspectratioidc<=16)
1058 aspectratio*=aspects[aspectratioidc];
1059 aspectx=aspectsx[aspectratioidc];
1060 aspecty=aspectsy[aspectratioidc];
1063 else if (aspectratioidc==255)
1065 int t_sar_width=nalu.getBits(16);
1066 int t_sar_height=nalu.getBits(16);
1067 if (t_sar_width!=0 && t_sar_height!=0)
1070 aspectratio*=((float)t_sar_width)/((float)t_sar_height);
1071 aspectx=t_sar_width;
1072 aspecty=t_sar_height;
1077 if (fabs(aspectratio-16./9.)<0.1) setAspectRatio(ASPECT_16_9,aspectx,aspecty);
1078 else if (fabs(aspectratio-4./3.)<0.1) setAspectRatio(ASPECT_4_3,aspectx,aspecty);
1079 else setAspectRatio(ASPECT_1_1,aspectx,aspecty);
1086 UINT posext = packet.findSeqExtHeader(h264);
1089 interlaced=!(packet[pos+1] & 0x08); //really simple
1090 // if more than full hd is coming we need to add additional parsers here
1092 /* NALUUnit nalu(packet.getData()+posext,packet.getSize()-posext);
1093 while (!nalu.isEonalu()) {
1094 unsigned int payloadtype=0;
1095 unsigned int payloadadd=0xFF;
1096 while (payloadadd==0xFF && !nalu.isEonalu()) {
1097 payloadadd=nalu.getBits(8);
1098 payloadtype+=payloadadd;
1100 unsigned int payloadsize=0;
1102 while (payloadadd==0xFF && !nalu.isEonalu()) {
1103 payloadadd=nalu.getBits(8);
1104 payloadsize+=payloadadd;
1106 switch (payloadtype) {
1107 case 1: { // picture timing SEI
1111 while (payloadsize) { // not handled skip
1129 video_pts_seek = video_pts;
1130 Log::getInstance()->log("Demuxer", Log::DEBUG,
1131 "Entering audio sync: Video PTS = %llu", video_pts);
1132 Log::getInstance()->log("Demuxer", Log::DEBUG,
1133 "Entering audio sync: Audio PTS = %llu", audio_pts);
1140 UINT Demuxer::stripAudio(UCHAR* buf, UINT len)
1142 UINT read_pos = 0, write_pos = 0;
1143 UINT pattern, packet_length;
1144 if (len < 4) return 0;
1145 pattern = (buf[0] << 16) | (buf[1] << 8) | (buf[2]);
1146 while (read_pos + 7 <= len)
1148 pattern = ((pattern & 0xFFFFFF) << 8) | buf[read_pos+3];
1149 if (pattern < (0x100|PESTYPE_VID0) || pattern > (0x100|PESTYPE_VIDMAX))
1153 packet_length = ((buf[read_pos+4] << 8) | (buf[read_pos+5])) + 6;
1154 if (read_pos + packet_length > len)
1158 if (read_pos != write_pos)
1159 memmove(buf+write_pos, buf+read_pos, packet_length);
1160 read_pos += packet_length;
1161 write_pos += packet_length;
1162 pattern = (buf[read_pos] << 16) | (buf[read_pos+1] << 8)
1163 | (buf[read_pos+2]);
1170 void Demuxer::changeTimes(UCHAR* buf, UINT len,UINT playtime)
1172 UINT pattern, packet_length;
1174 if (len < 4) return;
1175 pattern = (buf[0] << 16) | (buf[1] << 8) | (buf[2]);
1176 while (read_pos + 7 <= len)
1178 pattern = ((pattern & 0xFFFFFF) << 8) | buf[read_pos+3];
1179 if (pattern < (0x100|PESTYPE_VID0) || pattern > (0x100|PESTYPE_VIDMAX))
1183 packet_length = ((buf[read_pos+4] << 8) | (buf[read_pos+5])) + 6;
1184 // ok we have a packet figure out if pts and dts are present and replace them
1185 if (read_pos + 19 > len) return;
1186 ULLONG new_ts=playtime*90; //play time is on ms so multiply it by 90
1187 if (buf[read_pos+7] & 0x80) { // pts is here, replace it
1188 buf[read_pos+9]=0x21 | (( new_ts>>29)& 0xde );
1189 buf[read_pos+10]=0x00 |(( new_ts>>22)& 0xff );
1190 buf[read_pos+11]=0x01 | (( new_ts>>14)& 0xfe );
1191 buf[read_pos+12]=0x00 | (( new_ts>>7)& 0xff );
1192 buf[read_pos+13]=0x01 | (( new_ts<<1)& 0xfe );
1195 if (buf[read_pos+7] & 0x40) { // pts is here, replace it
1196 buf[read_pos+14]=0x21 | (( new_ts>>29)& 0xde );
1197 buf[read_pos+15]=0x00 | (( new_ts>>22)& 0xff );
1198 buf[read_pos+16]=0x01 | (( new_ts>>14)& 0xfe );
1199 buf[read_pos+17]=0x00 | (( new_ts>>7)& 0xff );
1200 buf[read_pos+18]=0x01 | (( new_ts<<1)& 0xfe );
1202 read_pos += packet_length;
1203 pattern = (buf[read_pos] << 16) | (buf[read_pos+1] << 8)
1204 | (buf[read_pos+2]);
1210 bool Demuxer::scanForVideo(UCHAR* buf, UINT len, bool &ish264)
1215 if (len < 4) return false;
1216 pattern = (buf[0] << 16) | (buf[1] << 8) | (buf[2]);
1219 pattern = ((pattern & 0xFFFFFF) << 8) | buf[pos++];
1220 if (pattern >= (0x100|PESTYPE_VID0) && pattern <= (0x100|PESTYPE_VIDMAX))
1226 bool* Demuxer::getmpAudioChannels()
1228 return avail_mpaudchan;
1231 bool* Demuxer::getac3AudioChannels()
1233 return avail_ac3audchan;
1236 bool* Demuxer::getSubtitleChannels()
1238 return avail_dvbsubtitlechan;
1241 int Demuxer::getselSubtitleChannel()
1243 return subtitle_current;
1246 int Demuxer::getselAudioChannel()
1248 return audio_current;