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"
32 #define DEMUXER_SEQ_HEAD 0x000001B3
33 #define DEMUXER_PIC_HEAD 0x00000101
34 #define DEMUXER_SEQ_EXT_HEAD 0x000001B5
36 #define DEMUXER_H264_ACCESS_UNIT 0x00000109
37 #define DEMUXER_H264_SEQ_PARAMETER_SET 0x00000107
38 #define DEMUXER_H264_SUB_ENHANCEMENT_INF 0x00000106
39 #define DEMUXER_H264_CODED_SLICE_IDR 0x00000105
40 #define DEMUXER_H264_CODED_SLICE_NON_IDR 0x00000101
42 #define SEEK_THRESHOLD 150000 // About 1.5 seconds
45 const int Demuxer::FrameRates[9] = { 0, 23, 24, 25, 29, 30, 50, 59, 60 };
46 Demuxer* Demuxer::instance = NULL;
50 NALUUnit(const UCHAR* buf,UINT length_buf);
53 inline UINT getBits(UINT num_bits);
56 bool isEonalu() {return eonalu;};
57 int getPos(){return pos;};
69 NALUUnit::NALUUnit(const UCHAR *buf, UINT length_buf)
81 UINT pattern =(((UINT)buf[ 0] << 16) |
85 while (pattern != 0x000001)
87 if (++nalu_start >= length_buf) return;
88 pattern = ((pattern << 8) | buf[nalu_start])&0x00FFFFFF;
90 nalu_end=nalu_start+1;
91 pattern = ((pattern << 8) | buf[nalu_end])&0x00FFFFFF;
93 while (pattern != 0x000001 && pattern != 0x000000)
95 if (++nalu_end >= length_buf) { nalu_end+=3;break;};
96 pattern = ((pattern << 8) | buf[nalu_end])&0x00FFFFFF;
99 nalu_end=min(length_buf-1,nalu_end);
100 nalu_length=nalu_end-nalu_start;
101 nalu_buf=(UCHAR*)malloc(nalu_length);
102 memcpy(nalu_buf,buf+nalu_start,nalu_length);
106 NALUUnit::~NALUUnit()
108 if (nalu_buf) free(nalu_buf);
111 inline UINT NALUUnit::getBits(UINT num_bits)
113 if (num_bits==0) return 0; //???
114 UINT remain_bits=num_bits;
116 //May be slow, but should work!
117 while (remain_bits>0) {
121 last_bytes=(last_bytes<<8) & nalu_buf[pos];
122 if ((last_bytes & 0x00FFFFFF) == 0x000003) pos++; //emulation prevention byte
125 working_byte=nalu_buf[pos];
141 UINT fetch_bits=min(remain_bits,8-bit_pos);
142 work=work <<fetch_bits;
143 //work|=((working_byte>>bit_pos) & (0xFF>>(8-fetch_bits)));
144 work|=(working_byte &(0xFF>>(bit_pos)))>>(8-fetch_bits-bit_pos);
145 remain_bits-=fetch_bits;
146 bit_pos=(bit_pos+fetch_bits)%8;
151 UINT NALUUnit::getUe()
155 for( bit = 0; !bit && !eonalu; leadbits++ )
157 if (eonalu) return true;
158 return ((1 << leadbits)-1)+getBits(leadbits);
161 int NALUUnit::getSe()
164 if (input==0) return 0;
165 int output=((input+1)>>1);
166 if (input & 0x1) output*=-1;
172 static const int PESPacket_initial_size = 2000;
175 PESPacket::PESPacket()
177 data_size = PESPacket_initial_size;
178 data = (UCHAR*)malloc(data_size);
185 PESPacket::PESPacket(const PESPacket& packet)
190 PESPacket& PESPacket::operator=(const PESPacket& packet)
194 if (data) free(data);
200 PESPacket::~PESPacket()
202 if (data) free(data);
205 void PESPacket::copyFrom(const PESPacket& packet)
207 length = packet.length;
209 packetType = packet.packetType;
210 substream = packet.substream;
211 seq_header = packet.seq_header;
213 data = (UCHAR*)malloc(data_size);
214 memcpy(data, packet.data, data_size);
217 void PESPacket::init(UCHAR type, UCHAR sub)
221 data[4] = data[5] = 0;
225 seq_header = 1; // Unknown seq_header status
229 void PESPacket::truncate()
231 init(packetType,substream);
236 int PESPacket::write(const UCHAR *buf, int len)
240 if (size + len > 0x10000) return 0;
241 if (size + len > data_size)
243 UINT new_data_size = max(data_size + data_size / 2, data_size + len);
244 if (new_data_size > 0x10000) new_data_size = 0x10000;
245 data_size = new_data_size;
246 data = (UCHAR*)realloc(data, data_size);
248 memcpy(data + size, buf, len);
251 data[4] = (length >> 8);
252 data[5] = (length & 0xFF);
253 // We have added data - reset seq_header indicator if necessary
254 if (seq_header == 0) seq_header = 1; // Reset to 'unknown'
258 ULLONG PESPacket::getPTS() const
260 if ( ( (packetType >= Demuxer::PESTYPE_AUD0 &&
261 packetType <= Demuxer::PESTYPE_AUDMAX)
263 (packetType >= Demuxer::PESTYPE_VID0 &&
264 packetType <= Demuxer::PESTYPE_VIDMAX)
266 packetType == Demuxer::PESTYPE_PRIVATE_1
268 && size >= 14 && data[7] & 0x80)
270 return ( (ULLONG)(data[ 9] & 0x0E) << 29) |
271 ( (ULLONG)(data[10]) << 22 ) |
272 ( (ULLONG)(data[11] & 0xFE) << 14 ) |
273 ( (ULLONG)(data[12]) << 7 ) |
274 ( (ULLONG)(data[13] & 0xFE) >> 1 );
276 else return PTS_INVALID;
279 UCHAR PESPacket::operator[] (UINT index) const
287 UINT PESPacket::findPictureHeader(bool h264) const
289 if (size < 12) return 0;
290 UINT pattern = ( ((UINT)data[ 8] << 24) |
291 ((UINT)data[ 9] << 16) |
292 ((UINT)data[10] << 8) |
297 while (pattern != DEMUXER_H264_ACCESS_UNIT)
299 if (++pos >= size) return 0;
300 pattern = (pattern << 8) | data[pos];
304 while (pattern != DEMUXER_PIC_HEAD)
306 if (++pos >= size) return 0;
307 pattern = (pattern << 8) | data[pos];
313 UINT PESPacket::countPictureHeaders(bool h264, struct PictCountInfo& pinfo) const
315 if (size < 12) return 0;
316 UINT pattern = ( ((UINT)data[ 8] << 24) |
317 ((UINT)data[ 9] << 16) |
318 ((UINT)data[10] << 8) |
323 //inspired by vdr algorithm for frame couting by Klaus Schmidinger
327 pattern = (pattern << 8) | data[pos];
328 if ((pattern &0xFFFFFF00)==0x00000100) {
329 UINT testpattern=(pattern& 0xFFFFFF1f);
330 if (testpattern==DEMUXER_H264_ACCESS_UNIT) pinfo.hasaccessunit=true;
331 else if (testpattern==DEMUXER_H264_SEQ_PARAMETER_SET ) {
333 NALUUnit nalu(data+pos-3,getSize()-pos+3);
334 int profile=nalu.getBits(8);
335 nalu.getBits(8); //constraints
336 nalu.getBits(8); //level_idc
337 nalu.getUe(); //seq_parameter_set_id
339 pinfo.separate_color_plane_flag=0;
340 if (profile==100 || profile==110 || profile==122 || profile==144)
345 pinfo.separate_color_plane_flag=nalu.getBits(1);
347 nalu.getUe(); //bit depth lume
348 nalu.getUe(); //bit depth chrome
352 for (int i=0;i<8;i++){
359 for (int j=0;j<16;j++) {
361 UINT delta=nalu.getSe();
362 nextscale=(lastscale+delta+256)%256;
364 lastscale=(nextscale==0)?lastscale:nextscale;
371 for (int j=0;j<64;j++) {
373 UINT delta=nalu.getSe();
374 nextscale=(lastscale+delta+256)%256;
376 lastscale=(nextscale==0)?lastscale:nextscale;
384 pinfo.log2_max_frame_num=nalu.getUe()+4; //log2framenum
385 UINT temp=nalu.getUe();
386 if (temp==0) //pict order
392 UINT temp2=nalu.getUe();
393 for (int i=0;i<temp2;i++)
396 nalu.getUe(); //Num refframes
400 pinfo.frame_mbs_only_flag=nalu.getBits(1);
404 } if (testpattern==DEMUXER_H264_CODED_SLICE_IDR || testpattern==DEMUXER_H264_CODED_SLICE_NON_IDR ) {
405 /* Log::getInstance()->log("Demuxer", Log::ERR,
406 "Has slice %d %d %d %d %d",pinfo.hasaccessunit, pinfo.hassps,pinfo.frame_mbs_only_flag,pinfo.separate_color_plane_flag,
407 pinfo.log2_max_frame_num);*/
408 if (pinfo.hasaccessunit && pinfo.hassps) {
410 NALUUnit nalu(data+pos-3,getSize()-pos+3);
411 nalu.getUe();// first_mb_in_slice
412 nalu.getUe();//sliectype
413 if (!pinfo.frame_mbs_only_flag) {
414 nalu.getUe(); //pic_paramter_set_id
415 if (pinfo.separate_color_plane_flag) nalu.getBits(2);
416 nalu.getBits(pinfo.log2_max_frame_num);
417 //if (!frame_mbs_only_flag)
418 if (nalu.getBits(1)) { //field_picture
419 if (!nalu.getBits(1)) { //bottom_field
427 pinfo.hasaccessunit=false;
437 pattern = (pattern << 8) | data[pos];
438 if (pattern==DEMUXER_PIC_HEAD) count++;
444 UINT PESPacket::findSeqHeader(bool h264) const
446 if (seq_header != 1) return seq_header;
447 if (size < 12) return 0;
448 UINT pattern = ( ((UINT)data[ 8] << 24) |
449 ((UINT)data[ 9] << 16) |
450 ((UINT)data[10] << 8) |
454 while ((pattern & 0xFFFFFF1F) != DEMUXER_H264_SEQ_PARAMETER_SET)
461 pattern = (pattern << 8) | data[pos];
463 seq_header = pos - 3;
467 while (pattern != DEMUXER_SEQ_HEAD)
474 pattern = (pattern << 8) | data[pos];
476 seq_header = pos - 3;
481 UINT PESPacket::findSeqExtHeader(bool h264) const
483 if (seq_header != 1) return seq_header;
484 if (size < 12) return 0;
485 UINT pattern = ( ((UINT)data[ 8] << 24) |
486 ((UINT)data[ 9] << 16) |
487 ((UINT)data[10] << 8) |
491 while ((pattern & 0xFFFFFF1F) != DEMUXER_H264_SUB_ENHANCEMENT_INF)
498 pattern = (pattern << 8) | data[pos];
500 seq_header = pos - 3;
504 while (pattern != DEMUXER_SEQ_EXT_HEAD && (data[pos+1]&0xf0)!=0x10)
506 if (++pos >= (size-1))
511 pattern = (pattern << 8) | data[pos];
513 seq_header = pos - 3;
521 if (instance) return;
526 vid_seeking = aud_seeking = false;
527 video_pts = audio_pts = 0;
528 ispre_1_3_19 = false;
542 Demuxer* Demuxer::getInstance()
547 int Demuxer::init(Callback* tcallback, DrainTarget* audio, DrainTarget* video, DrainTarget* teletext,
548 ULONG demuxMemoryV, ULONG demuxMemoryA, ULONG demuxMemoryT,double infps, DVBSubtitles* tsubtitles)
552 if ( !videostream.init(video, demuxMemoryV) ||
553 !audiostream.init(audio, demuxMemoryA) ||
554 !teletextstream.init(teletext, demuxMemoryT))
556 Log::getInstance()->log("Demuxer", Log::CRIT,
557 "Failed to initialize demuxer");
563 isteletextdecoded = true;
565 isteletextdecoded = false;
570 subtitles = tsubtitles;
571 callback = tcallback;
575 void Demuxer::reset()
577 Log::getInstance()->log("Demuxer", Log::DEBUG, "Reset called");
579 video_current = audio_current = teletext_current = subtitle_current = -1;
580 horizontal_size = vertical_size = 0;
581 interlaced=true; // default is true
582 aspect_ratio = (enum AspectRatio) 0;
585 frame_rate = bit_rate = 0;
586 ispre_1_3_19 = false;
592 for (int i = 0; i <= (PESTYPE_AUDMAX - PESTYPE_AUD0); i++)
594 avail_mpaudchan[i] = false;
596 for (int i = 0; i <= (PESTYPE_SUBSTREAM_AC3MAX - PESTYPE_SUBSTREAM_AC30); i++)
598 avail_ac3audchan[i] = false;
600 for (int i = 0; i <= (PESTYPE_SUBSTREAM_DVBSUBTITLEMAX - PESTYPE_SUBSTREAM_DVBSUBTITLE0); i++)
602 avail_dvbsubtitlechan[i] = false;
606 int Demuxer::shutdown()
608 videostream.shutdown();
609 audiostream.shutdown();
610 teletextstream.shutdown();
615 void Demuxer::flush()
617 Log::getInstance()->log("Demuxer", Log::DEBUG, "Flush called");
621 teletextstream.flush();
624 void Demuxer::flushAudio()
631 vid_seeking = aud_seeking = true;
632 video_pts = audio_pts = teletext_pts = 0;
635 void Demuxer::setAudioStream(int id)
640 void Demuxer::setVideoStream(int id)
645 void Demuxer::setTeletextStream(int id)
647 teletext_current = id;
650 void Demuxer::setDVBSubtitleStream(int id)
652 subtitle_current = id;
655 void Demuxer::setAspectRatio(enum AspectRatio ar, int taspectx, int taspecty)
657 if (aspect_ratio != ar)
659 Log::getInstance()->log("Demux", Log::DEBUG,
660 "Aspect ratio difference signalled");
661 if (++arcnt > 3) // avoid changing aspect ratio if glitch in signal
667 if (callback) callback->call(this);
674 bool Demuxer::writeAudio(bool * dataavail)
676 return audiostream.drain(dataavail);
679 bool Demuxer::writeVideo(bool * dataavail)
681 return videostream.drain(dataavail);
684 bool Demuxer::writeTeletext(bool * dataavail)
686 return teletextstream.drain(dataavail);
689 bool Demuxer::submitPacket(PESPacket& packet)
692 UCHAR packet_type = packet.getPacketType();
693 const UCHAR* packetdata = packet.getData();
694 if (packet_type >= PESTYPE_VID0 && packet_type <= PESTYPE_VIDMAX)
696 if (video_current == -1) video_current = packet_type;
697 if (video_current == packet_type && !vid_seeking)
699 sent = videostream.put(&packetdata[0], packet.getSize(), h264?MPTYPE_VIDEO_H264:MPTYPE_VIDEO_MPEG2,packetnum);
700 if (sent) packetnum++;
703 sent = packet.getSize();
705 else if (packet_type >= PESTYPE_AUD0 && packet_type <= PESTYPE_AUDMAX)
708 if (audio_current == -1) audio_current = packet_type;
709 avail_mpaudchan[packet_type - PESTYPE_AUD0] = true;
710 if (audio_current == packet_type && !aud_seeking)
712 UCHAR type=MPTYPE_MPEG_AUDIO;
717 type=MPTYPE_MPEG_AUDIO; break;
719 type=MPTYPE_AAC_LATM; break;
721 sent = audiostream.put(&packetdata[0], packet.getSize(), type,packetnum);
722 if (sent) packetnum++;
725 sent = packet.getSize();
727 else if (packet_type == PESTYPE_PRIVATE_1 &&
728 packet.getSubstream() >= PESTYPE_SUBSTREAM_AC30 &&
729 packet.getSubstream() <= PESTYPE_SUBSTREAM_AC3MAX)
731 avail_ac3audchan[packet.getSubstream() - PESTYPE_SUBSTREAM_AC30] = true;
732 if (packet.getSubstream() == audio_current)
734 sent = audiostream.put(&packetdata[0], packet.getSize(), (ispre_1_3_19 || livetv)? MPTYPE_AC3_PRE13 : MPTYPE_AC3,packetnum);
735 if (sent) packetnum++;
739 sent = packet.getSize();
742 else if (packet_type == PESTYPE_PRIVATE_1 &&
743 packet.getSubstream() >= PESTYPE_SUBSTREAM_DVBSUBTITLE0 &&
744 packet.getSubstream() <= PESTYPE_SUBSTREAM_DVBSUBTITLEMAX)
746 avail_dvbsubtitlechan[packet.getSubstream()-PESTYPE_SUBSTREAM_DVBSUBTITLE0]=true;
747 if (subtitle_current == -1) subtitle_current = packet.getSubstream();
748 if (subtitles && packet.getSubstream()==subtitle_current)
750 subtitles->put(packet);
752 sent = packet.getSize();
754 else if (isteletextdecoded && packet_type == PESTYPE_PRIVATE_1 &&
755 packet.getSubstream() >= PESTYPE_SUBSTREAM_TELETEXT0 &&
756 packet.getSubstream() <= PESTYPE_SUBSTREAM_TELETEXTMAX)
759 if (teletext_current == -1) teletext_current = packet.getSubstream();
760 if (teletext_current == packet.getSubstream())
762 sent = teletextstream.put(&packetdata[0], packet.getSize(), MPTYPE_TELETEXT,packetnum);
766 sent = packet.getSize();
771 sent = packet.getSize();
774 if (sent < packet.getSize()) // Stream is full.
780 void Demuxer::parsePacketDetails(PESPacket& packet)
782 if (packet.getPacketType() >= PESTYPE_AUD0 &&
783 packet.getPacketType() <= PESTYPE_AUDMAX)
785 // Extract audio PTS if it exists
788 audio_pts = packet.getPTS();
789 // We continue to seek on the audio if the video PTS that we
790 // are trying to match is ahead of the audio PTS by at most
791 // SEEK_THRESHOLD. We consider the possibility of PTS wrap.
792 if (aud_seeking && !vid_seeking &&
793 !( (video_pts_seek > audio_pts &&
794 video_pts_seek - audio_pts < SEEK_THRESHOLD)
796 (video_pts_seek < audio_pts &&
797 video_pts_seek + (1LL<<33) - audio_pts < SEEK_THRESHOLD) ))
800 Log::getInstance()->log("Demuxer", Log::DEBUG,
801 "Leaving audio sync: Audio PTS = %llu", audio_pts);
805 else if (packet.getPacketType() == PESTYPE_PRIVATE_1) // Private stream
808 //Inspired by vdr's device.c
809 int payload_begin = packet[8]+9;
810 unsigned char substream_id = packet[payload_begin];
811 unsigned char substream_type = substream_id & 0xF0;
812 unsigned char substream_index = substream_id & 0x1F;
813 pre_1_3_19_Recording: //This is for old recordings stuff and live TV
816 int old_substream=packet.getSubstream();
817 if (old_substream){ //someone else already set it, this is live tv
818 substream_id = old_substream;
819 substream_type = substream_id & 0xF0;
820 substream_index = substream_id & 0x1F;
822 substream_id = PESTYPE_PRIVATE_1;
823 substream_type = 0x80;
828 switch (substream_type)
832 packet.setSubstream(substream_id);
834 case 0xA0: //LPCM //not supported yet, is there any LPCM transmissio out there?
836 case 0x80: //ac3, currently only one ac3 track per recording supported
837 packet.setSubstream(substream_type+substream_index);
839 // Extract audio PTS if it exists
842 audio_pts = packet.getPTS();
843 // We continue to seek on the audio if the video PTS that we
844 // are trying to match is ahead of the audio PTS by at most
845 // SEEK_THRESHOLD. We consider the possibility of PTS wrap.
846 if (aud_seeking && !vid_seeking &&
847 !( (video_pts_seek > audio_pts &&
848 video_pts_seek - audio_pts < SEEK_THRESHOLD)
850 (video_pts_seek < audio_pts &&
851 video_pts_seek + (1LL<<33) - audio_pts < SEEK_THRESHOLD) ))
854 Log::getInstance()->log("Demuxer", Log::DEBUG, "Leaving audio sync: Audio PTS = %llu", audio_pts);
858 case 0x10: //Teletext Is this correct?
859 packet.setSubstream(substream_id);
860 // Extract teletxt PTS if it exists
863 teletext_pts = packet.getPTS();
869 ispre_1_3_19=true; //switching to compat mode and live tv mode
870 goto pre_1_3_19_Recording;
874 packet.setSubstream(0);
880 else if (packet.getPacketType() >= PESTYPE_VID0 &&
881 packet.getPacketType() <= PESTYPE_VIDMAX)
883 // Extract video PTS if it exists
884 if (packet.hasPTS()) video_pts = packet.getPTS();
886 // If there is a sequence header, extract information
887 UINT pos = packet.findSeqHeader(h264);
892 if (pos+6 >= packet.getSize()) return;
893 horizontal_size = ((int)packet[pos] << 4) | ((int)packet[pos+1] >> 4);
895 vertical_size = (((int)packet[pos+1] & 0xf) << 8) | (int)packet[pos+2];
897 enum AspectRatio aspect=(enum AspectRatio)(packet[pos+3] >> 4);
900 const int aspectDAR[]={0,1,1,1,4,3,16,9,221,100};
901 int aspectDARx=aspectDAR[aspect*2];
902 int aspectDARy=aspectDAR[aspect*2+1];
904 aspectx=aspectDARx*vertical_size;
905 aspecty=aspectDARy*horizontal_size;
909 // Log::getInstance()->log("Demuxer", Log::DEBUG, "PAR test1 %d %d %d %d ", aspectx,aspecty,commona,commonb);
915 commonb=commona%commonb;
918 aspectx=aspectx/commona;
919 aspecty=aspecty/commona;
920 //Log::getInstance()->log("Demuxer", Log::DEBUG, "PAR test2 %d %d %d %d %d %d %d", aspectx,aspecty,aspectDARx,aspectDARy,horizontal_size,vertical_size,commona);
922 setAspectRatio(aspect,aspectx,aspecty);
923 frame_rate = packet[pos+3] & 0x0f;
924 if (frame_rate >= 1 && frame_rate <= 8)
925 frame_rate = FrameRates[frame_rate];
928 bit_rate = ((int)packet[pos+4] << 10) |
929 ((int)packet[pos+5] << 2) |
930 ((int)packet[pos+6] >> 6);
934 /* Chris and Mark I know this is ugly, should we move this to a method of PESPacket or to NALUUnit what would be better?
935 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*/
936 NALUUnit nalu(packet.getData()+pos,packet.getSize()-pos);
937 profile=nalu.getBits(8);
938 nalu.getBits(8); //constraints
939 nalu.getBits(8); //level_idc
940 nalu.getUe(); //seq_parameter_set_id
942 if (profile==100 || profile==110 || profile==122 || profile==144)
949 nalu.getUe(); //bit depth lume
950 nalu.getUe(); //bit depth chrome
954 for (int i=0;i<8;i++){
961 for (int j=0;j<16;j++) {
963 UINT delta=nalu.getSe();
964 nextscale=(lastscale+delta+256)%256;
966 lastscale=(nextscale==0)?lastscale:nextscale;
973 for (int j=0;j<64;j++) {
975 UINT delta=nalu.getSe();
976 nextscale=(lastscale+delta+256)%256;
978 lastscale=(nextscale==0)?lastscale:nextscale;
989 chromunitx=chromunity=1; break;
991 chromunitx=chromunity=2; break;
993 chromunitx=2;chromunity=1; break;
995 chromunitx=chromunity=1; break;
998 nalu.getUe(); //log2framenum
999 UINT temp=nalu.getUe();
1000 if (temp==0) //pict order
1006 UINT temp2=nalu.getUe();
1007 for (int i=0;i<temp2;i++)
1010 nalu.getUe(); //Num refframes
1012 horizontal_size=(nalu.getUe()+1)*16;
1014 vertical_size=(nalu.getUe()+1)*16;
1015 int tinterlaced=nalu.getBits(1);
1016 vertical_size*=(2-tinterlaced);
1017 interlaced=!tinterlaced;
1019 if (!tinterlaced) nalu.getBits(1);
1021 if (nalu.getBits(1))
1023 horizontal_size-=nalu.getUe()*chromunitx;
1024 horizontal_size-=nalu.getUe()*chromunitx;
1025 vertical_size-=nalu.getUe()*(2-tinterlaced)*chromunity;
1026 vertical_size-=nalu.getUe()*(2-tinterlaced)*chromunity;
1028 if (nalu.getBits(1))
1030 if (nalu.getBits(1))
1032 UINT aspectratioidc=nalu.getBits(8);
1033 bool hasaspect=false;
1034 int aspectx,aspecty;
1035 const float aspects[]={1., 1./1.,12./11.,10./11.,16./11.,40./33.,
1036 24./11.,20./11.,32./11.,80./33.,18./11.,15./11.,64./33.,160./99.,4./3.,3./2.,2./1.};
1037 const int aspectsx[]={1, 1,12,10,16,40,
1038 24,20,32,80,18,15,64,160,4,3,2};
1039 const int aspectsy[]={1, 1,11,11,11,33,11,11,11,33,11,11,33,99,3,2,1};
1041 float aspectratio=((float) horizontal_size)/((float) vertical_size);
1042 if (aspectratioidc<=16)
1045 aspectratio*=aspects[aspectratioidc];
1046 aspectx=aspectsx[aspectratioidc];
1047 aspecty=aspectsy[aspectratioidc];
1050 else if (aspectratioidc==255)
1052 int t_sar_width=nalu.getBits(16);
1053 int t_sar_height=nalu.getBits(16);
1054 if (t_sar_width!=0 && t_sar_height!=0)
1057 aspectratio*=((float)t_sar_width)/((float)t_sar_height);
1058 aspectx=t_sar_width;
1059 aspecty=t_sar_height;
1064 if (fabs(aspectratio-16./9.)<0.1) setAspectRatio(ASPECT_16_9,aspectx,aspecty);
1065 else if (fabs(aspectratio-4./3.)<0.1) setAspectRatio(ASPECT_4_3,aspectx,aspecty);
1066 else setAspectRatio(ASPECT_1_1,aspectx,aspecty);
1073 UINT posext = packet.findSeqExtHeader(h264);
1076 interlaced=!(packet[pos+1] & 0x08); //really simple
1077 // if more than full hd is coming we need to add additional parsers here
1079 /* NALUUnit nalu(packet.getData()+posext,packet.getSize()-posext);
1080 while (!nalu.isEonalu()) {
1081 unsigned int payloadtype=0;
1082 unsigned int payloadadd=0xFF;
1083 while (payloadadd==0xFF && !nalu.isEonalu()) {
1084 payloadadd=nalu.getBits(8);
1085 payloadtype+=payloadadd;
1087 unsigned int payloadsize=0;
1089 while (payloadadd==0xFF && !nalu.isEonalu()) {
1090 payloadadd=nalu.getBits(8);
1091 payloadsize+=payloadadd;
1093 switch (payloadtype) {
1094 case 1: { // picture timing SEI
1098 while (payloadsize) { // not handled skip
1116 video_pts_seek = video_pts;
1117 Log::getInstance()->log("Demuxer", Log::DEBUG,
1118 "Entering audio sync: Video PTS = %llu", video_pts);
1119 Log::getInstance()->log("Demuxer", Log::DEBUG,
1120 "Entering audio sync: Audio PTS = %llu", audio_pts);
1127 UINT Demuxer::stripAudio(UCHAR* buf, UINT len)
1129 UINT read_pos = 0, write_pos = 0;
1130 UINT pattern, packet_length;
1131 if (len < 4) return 0;
1132 pattern = (buf[0] << 16) | (buf[1] << 8) | (buf[2]);
1133 while (read_pos + 7 <= len)
1135 pattern = ((pattern & 0xFFFFFF) << 8) | buf[read_pos+3];
1136 if (pattern < (0x100|PESTYPE_VID0) || pattern > (0x100|PESTYPE_VIDMAX))
1140 packet_length = ((buf[read_pos+4] << 8) | (buf[read_pos+5])) + 6;
1141 if (read_pos + packet_length > len)
1145 if (read_pos != write_pos)
1146 memmove(buf+write_pos, buf+read_pos, packet_length);
1147 read_pos += packet_length;
1148 write_pos += packet_length;
1149 pattern = (buf[read_pos] << 16) | (buf[read_pos+1] << 8)
1150 | (buf[read_pos+2]);
1157 void Demuxer::changeTimes(UCHAR* buf, UINT len,UINT playtime)
1159 UINT pattern, packet_length;
1161 if (len < 4) return;
1162 pattern = (buf[0] << 16) | (buf[1] << 8) | (buf[2]);
1163 while (read_pos + 7 <= len)
1165 pattern = ((pattern & 0xFFFFFF) << 8) | buf[read_pos+3];
1166 if (pattern < (0x100|PESTYPE_VID0) || pattern > (0x100|PESTYPE_VIDMAX))
1170 packet_length = ((buf[read_pos+4] << 8) | (buf[read_pos+5])) + 6;
1171 // ok we have a packet figure out if pts and dts are present and replace them
1172 if (read_pos + 19 > len) return;
1173 ULLONG new_ts=playtime*90; //play time is on ms so multiply it by 90
1174 if (buf[read_pos+7] & 0x80) { // pts is here, replace it
1175 buf[read_pos+9]=0x21 | (( new_ts>>29)& 0xde );
1176 buf[read_pos+10]=0x00 |(( new_ts>>22)& 0xff );
1177 buf[read_pos+11]=0x01 | (( new_ts>>14)& 0xfe );
1178 buf[read_pos+12]=0x00 | (( new_ts>>7)& 0xff );
1179 buf[read_pos+13]=0x01 | (( new_ts<<1)& 0xfe );
1182 if (buf[read_pos+7] & 0x40) { // pts is here, replace it
1183 buf[read_pos+14]=0x21 | (( new_ts>>29)& 0xde );
1184 buf[read_pos+15]=0x00 | (( new_ts>>22)& 0xff );
1185 buf[read_pos+16]=0x01 | (( new_ts>>14)& 0xfe );
1186 buf[read_pos+17]=0x00 | (( new_ts>>7)& 0xff );
1187 buf[read_pos+18]=0x01 | (( new_ts<<1)& 0xfe );
1189 read_pos += packet_length;
1190 pattern = (buf[read_pos] << 16) | (buf[read_pos+1] << 8)
1191 | (buf[read_pos+2]);
1197 bool Demuxer::scanForVideo(UCHAR* buf, UINT len, bool &ish264)
1202 if (len < 4) return false;
1203 pattern = (buf[0] << 16) | (buf[1] << 8) | (buf[2]);
1206 pattern = ((pattern & 0xFFFFFF) << 8) | buf[pos++];
1207 if (pattern >= (0x100|PESTYPE_VID0) && pattern <= (0x100|PESTYPE_VIDMAX))
1213 bool* Demuxer::getmpAudioChannels()
1215 return avail_mpaudchan;
1218 bool* Demuxer::getac3AudioChannels()
1220 return avail_ac3audchan;
1223 bool* Demuxer::getSubtitleChannels()
1225 return avail_dvbsubtitlechan;
1228 int Demuxer::getselSubtitleChannel()
1230 return subtitle_current;
1233 int Demuxer::getselAudioChannel()
1235 return audio_current;