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
45 static const char* TAG = "Demuxer";
48 const int Demuxer::FrameRates[9] = { 0, 23, 24, 25, 29, 30, 50, 59, 60 };
49 Demuxer* Demuxer::instance = NULL;
53 NALUUnit(const u1* buf,u4 length_buf);
56 inline u4 getBits(u4 num_bits);
59 bool isEonalu() {return eonalu;};
60 int getPos(){return pos;};
72 NALUUnit::NALUUnit(const u1 *buf, u4 length_buf)
84 u4 pattern =(((u4)buf[ 0] << 16) |
88 while (pattern != 0x000001)
90 if (++nalu_start >= length_buf) return;
91 pattern = ((pattern << 8) | buf[nalu_start])&0x00FFFFFF;
93 nalu_end=nalu_start+1;
94 if (nalu_end >= length_buf) return; // input buffer too small. corrupt data? ignore.
95 pattern = ((pattern << 8) | buf[nalu_end])&0x00FFFFFF;
97 while (pattern != 0x000001 && pattern != 0x000000)
99 if (++nalu_end >= length_buf) { nalu_end+=3;break;};
100 pattern = ((pattern << 8) | buf[nalu_end])&0x00FFFFFF;
103 nalu_end=std::min(length_buf-1,nalu_end);
104 if (nalu_end <= nalu_start) return; // input buffer too small. corrupt data? ignore.
105 nalu_length=nalu_end-nalu_start;
106 nalu_buf=(u1*)malloc(nalu_length);
107 memcpy(nalu_buf,buf+nalu_start,nalu_length);
111 NALUUnit::~NALUUnit()
113 if (nalu_buf) free(nalu_buf);
116 inline u4 NALUUnit::getBits(u4 num_bits)
118 if (num_bits==0) return 0; //???
119 u4 remain_bits=num_bits;
121 //May be slow, but should work!
122 while (remain_bits>0) {
126 last_bytes=(last_bytes<<8) & nalu_buf[pos];
127 if ((last_bytes & 0x00FFFFFF) == 0x000003) pos++; //emulation prevention byte
130 working_byte=nalu_buf[pos];
146 u4 fetch_bits=std::min(remain_bits, static_cast<u4>(8-bit_pos));
147 work=work <<fetch_bits;
148 //work|=((working_byte>>bit_pos) & (0xFF>>(8-fetch_bits)));
149 work|=(working_byte &(0xFF>>(bit_pos)))>>(8-fetch_bits-bit_pos);
150 remain_bits-=fetch_bits;
151 bit_pos=(bit_pos+fetch_bits)%8;
160 for( bit = 0; !bit && !eonalu; leadbits++ )
162 if (eonalu) return 0;
163 return ((1 << leadbits)-1)+getBits(leadbits);
166 int NALUUnit::getSe()
169 if (input==0) return 0;
170 int output=((input+1)>>1);
171 if (input & 0x1) output*=-1;
177 static const int PESPacket_initial_size = 2000;
180 PESPacket::PESPacket()
182 data_size = PESPacket_initial_size;
183 data = (u1*)malloc(data_size);
190 PESPacket::PESPacket(const PESPacket& packet)
195 PESPacket& PESPacket::operator=(const PESPacket& packet)
199 if (data) free(data);
205 PESPacket::~PESPacket()
207 if (data) free(data);
210 void PESPacket::copyFrom(const PESPacket& packet)
212 length = packet.length;
214 packetType = packet.packetType;
215 substream = packet.substream;
216 seq_header = packet.seq_header;
218 data = (u1*)malloc(data_size);
219 memcpy(data, packet.data, data_size);
222 void PESPacket::init(u1 type, u1 sub)
226 data[4] = data[5] = 0;
230 seq_header = 1; // Unknown seq_header status
234 void PESPacket::truncate()
236 init(packetType,substream);
241 int PESPacket::write(const u1 *buf, int len)
245 if (size + len > 0x10000) return 0;
246 if (size + len > data_size)
248 u4 new_data_size = std::max(data_size + data_size / 2, data_size + len);
249 if (new_data_size > 0x10000) new_data_size = 0x10000;
250 data_size = new_data_size;
251 data = (u1*)realloc(data, data_size);
253 memcpy(data + size, buf, len);
256 data[4] = (length >> 8);
257 data[5] = (length & 0xFF);
258 // We have added data - reset seq_header indicator if necessary
259 if (seq_header == 0) seq_header = 1; // Reset to 'unknown'
263 u8 PESPacket::getPTS() const
265 if ( ( (packetType >= Demuxer::PESTYPE_AUD0 &&
266 packetType <= Demuxer::PESTYPE_AUDMAX)
268 (packetType >= Demuxer::PESTYPE_VID0 &&
269 packetType <= Demuxer::PESTYPE_VIDMAX)
271 packetType == Demuxer::PESTYPE_PRIVATE_1
273 && size >= 14 && data[7] & 0x80)
275 return ( (u8)(data[ 9] & 0x0E) << 29) |
276 ( (u8)(data[10]) << 22 ) |
277 ( (u8)(data[11] & 0xFE) << 14 ) |
278 ( (u8)(data[12]) << 7 ) |
279 ( (u8)(data[13] & 0xFE) >> 1 );
281 else return PTS_INVALID;
284 u1 PESPacket::operator[] (u4 index) const
292 u4 PESPacket::findPictureHeader(bool h264) const
294 if (size < 12) return 0;
295 u4 pattern = ( ((u4)data[ 8] << 24) |
296 ((u4)data[ 9] << 16) |
297 ((u4)data[10] << 8) |
302 while (pattern != DEMUXER_H264_ACCESS_UNIT)
304 if (++pos >= size) return 0;
305 pattern = (pattern << 8) | data[pos];
309 while (pattern != DEMUXER_PIC_HEAD)
311 if (++pos >= size) return 0;
312 pattern = (pattern << 8) | data[pos];
318 u4 PESPacket::countPictureHeaders(bool h264, struct PictCountInfo& pinfo) const
320 if (size < 12) return 0;
321 u4 pattern = ( ((u4)data[ 8] << 24) |
322 ((u4)data[ 9] << 16) |
323 ((u4)data[10] << 8) |
328 //inspired by vdr algorithm for frame couting by Klaus Schmidinger
332 pattern = (pattern << 8) | data[pos];
333 if ((pattern &0xFFFFFF00)==0x00000100) {
334 u4 testpattern=(pattern& 0xFFFFFF1f);
335 if (testpattern==DEMUXER_H264_ACCESS_UNIT) pinfo.hasaccessunit=true;
336 else if (testpattern==DEMUXER_H264_SEQ_PARAMETER_SET ) {
338 NALUUnit nalu(data+pos-3,getSize()-pos+3);
339 int profile=nalu.getBits(8);
340 nalu.getBits(8); //constraints
341 nalu.getBits(8); //level_idc
342 nalu.getUe(); //seq_parameter_set_id
344 pinfo.separate_color_plane_flag=0;
345 if (profile==100 || profile==110 || profile==122 || profile==144)
350 pinfo.separate_color_plane_flag=nalu.getBits(1);
352 nalu.getUe(); //bit depth lume
353 nalu.getUe(); //bit depth chrome
357 for (int i=0;i<8;i++){
364 for (int j=0;j<16;j++) {
366 u4 delta=nalu.getSe();
367 nextscale=(lastscale+delta+256)%256;
369 lastscale=(nextscale==0)?lastscale:nextscale;
376 for (int j=0;j<64;j++) {
378 u4 delta=nalu.getSe();
379 nextscale=(lastscale+delta+256)%256;
381 lastscale=(nextscale==0)?lastscale:nextscale;
389 u4 checkMaxFrameNum = nalu.getUe() + 4; //log2framenum
391 if (checkMaxFrameNum < 13) {
392 pinfo.log2_max_frame_num = checkMaxFrameNum;
400 u4 temp=nalu.getUe();
401 if (temp==0) //pict order
407 u4 temp2=nalu.getUe();
408 for (u4 i=0;i<temp2;i++)
411 nalu.getUe(); //Num refframes
415 pinfo.frame_mbs_only_flag=nalu.getBits(1);
419 } if (testpattern==DEMUXER_H264_CODED_SLICE_IDR || testpattern==DEMUXER_H264_CODED_SLICE_NON_IDR ) {
420 /* LogNT::getInstance()->error(TAG,
421 "Has slice %d %d %d %d %d",pinfo.hasaccessunit, pinfo.hassps,pinfo.frame_mbs_only_flag,pinfo.separate_color_plane_flag,
422 pinfo.log2_max_frame_num);*/
423 if (pinfo.hasaccessunit && pinfo.hassps) {
425 NALUUnit nalu(data+pos-3,getSize()-pos+3);
426 nalu.getUe();// first_mb_in_slice
427 nalu.getUe();//sliectype
428 if (!pinfo.frame_mbs_only_flag) {
429 nalu.getUe(); //pic_paramter_set_id
430 if (pinfo.separate_color_plane_flag) nalu.getBits(2);
431 nalu.getBits(pinfo.log2_max_frame_num);
432 //if (!frame_mbs_only_flag)
433 if (nalu.getBits(1)) { //field_picture
434 if (!nalu.getBits(1)) { //bottom_field
442 pinfo.hasaccessunit=false;
452 pattern = (pattern << 8) | data[pos];
453 if (pattern==DEMUXER_PIC_HEAD) count++;
459 u4 PESPacket::findSeqHeader(bool h264) const
461 if (seq_header != 1) return seq_header;
462 if (size < 12) return 0;
463 u4 pattern = ( ((u4)data[ 8] << 24) |
464 ((u4)data[ 9] << 16) |
465 ((u4)data[10] << 8) |
469 while ((pattern & 0xFFFFFF1F) != DEMUXER_H264_SEQ_PARAMETER_SET)
476 pattern = (pattern << 8) | data[pos];
478 seq_header = pos - 3;
482 while (pattern != DEMUXER_SEQ_HEAD)
489 pattern = (pattern << 8) | data[pos];
491 seq_header = pos - 3;
496 u4 PESPacket::findSeqExtHeader(bool h264) const
498 if (seq_header != 1) return seq_header;
499 if (size < 12) return 0;
500 u4 pattern = ( ((u4)data[ 8] << 24) |
501 ((u4)data[ 9] << 16) |
502 ((u4)data[10] << 8) |
506 while ((pattern & 0xFFFFFF1F) != DEMUXER_H264_SUB_ENHANCEMENT_INF)
513 pattern = (pattern << 8) | data[pos];
515 seq_header = pos - 3;
519 while (pattern != DEMUXER_SEQ_EXT_HEAD && (data[pos+1]&0xf0)!=0x10)
521 if (++pos >= (size-1))
526 pattern = (pattern << 8) | data[pos];
528 seq_header = pos - 3;
536 if (instance) return;
541 vid_seeking = aud_seeking = false;
542 video_pts = audio_pts = 0;
543 ispre_1_3_19 = false;
557 Demuxer* Demuxer::getInstance()
562 int Demuxer::init(Callback* tcallback, DrainTarget* audio, DrainTarget* video, DrainTarget* teletext,
563 u4 demuxMemoryV, u4 demuxMemoryA, u4 demuxMemoryT,double infps, DVBSubtitles* tsubtitles)
567 if ( !videostream.init(video, demuxMemoryV) ||
568 !audiostream.init(audio, demuxMemoryA) ||
569 !teletextstream.init(teletext, demuxMemoryT))
571 LogNT::getInstance()->crit(TAG, "Failed to initialize demuxer");
577 isteletextdecoded = true;
579 isteletextdecoded = false;
584 subtitles = tsubtitles;
585 callback = tcallback;
589 void Demuxer::reset()
591 LogNT::getInstance()->debug(TAG, "Reset called");
593 video_current = audio_current = teletext_current = subtitle_current = -1;
594 horizontal_size = vertical_size = 0;
595 interlaced=true; // default is true
596 aspect_ratio = (enum AspectRatio) 0;
599 frame_rate = bit_rate = 0;
600 ispre_1_3_19 = false;
606 for (int i = 0; i <= (PESTYPE_AUDMAX - PESTYPE_AUD0); i++)
608 avail_mpaudchan[i] = false;
610 for (int i = 0; i <= (PESTYPE_SUBSTREAM_AC3MAX - PESTYPE_SUBSTREAM_AC30); i++)
612 avail_ac3audchan[i] = false;
614 for (int i = 0; i <= (PESTYPE_SUBSTREAM_DVBSUBTITLEMAX - PESTYPE_SUBSTREAM_DVBSUBTITLE0); i++)
616 avail_dvbsubtitlechan[i] = false;
620 int Demuxer::shutdown()
622 videostream.shutdown();
623 audiostream.shutdown();
624 teletextstream.shutdown();
629 void Demuxer::flush()
631 LogNT::getInstance()->debug(TAG, "Flush called");
635 teletextstream.flush();
638 void Demuxer::flushAudio()
645 vid_seeking = aud_seeking = true;
646 video_pts = audio_pts = teletext_pts = 0;
649 void Demuxer::setAudioStream(int id)
654 void Demuxer::setVideoStream(int id)
659 void Demuxer::setTeletextStream(int id)
661 teletext_current = id;
664 void Demuxer::setDVBSubtitleStream(int id)
666 subtitle_current = id;
669 void Demuxer::setAspectRatio(enum AspectRatio ar, int taspectx, int taspecty)
671 if ((aspect_ratio != ar) || (parx != taspectx) || (pary != taspecty) )
673 LogNT::getInstance()->debug(TAG, "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 u1 packet_type = packet.getPacketType();
706 const u1* 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 u1 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 LogNT::getInstance()->debug(TAG, "Leaving audio sync: Audio PTS = {}", audio_pts);
817 else if (packet.getPacketType() == PESTYPE_PRIVATE_1) // Private stream
820 //Inspired by vdr's device.c
821 int payload_begin = packet[8]+9;
822 unsigned char substream_id = packet[payload_begin];
823 unsigned char substream_type = substream_id & 0xF0;
824 unsigned char substream_index = substream_id & 0x1F;
825 pre_1_3_19_Recording: //This is for old recordings stuff and live TV
828 int old_substream=packet.getSubstream();
829 if (old_substream){ //someone else already set it, this is live tv
830 substream_id = old_substream;
831 substream_type = substream_id & 0xF0;
832 substream_index = substream_id & 0x1F;
834 substream_id = PESTYPE_PRIVATE_1;
835 substream_type = 0x80;
840 switch (substream_type)
844 packet.setSubstream(substream_id);
846 case 0xA0: //LPCM //not supported yet, is there any LPCM transmissio out there?
848 case 0x80: //ac3, currently only one ac3 track per recording supported
849 packet.setSubstream(substream_type+substream_index);
851 // Extract audio PTS if it exists
854 audio_pts = packet.getPTS();
855 // We continue to seek on the audio if the video PTS that we
856 // are trying to match is ahead of the audio PTS by at most
857 // SEEK_THRESHOLD. We consider the possibility of PTS wrap.
858 if (aud_seeking && !vid_seeking &&
859 !( (video_pts_seek > audio_pts &&
860 video_pts_seek - audio_pts < SEEK_THRESHOLD)
862 (video_pts_seek < audio_pts &&
863 video_pts_seek + (1LL<<33) - audio_pts < SEEK_THRESHOLD) ))
866 LogNT::getInstance()->debug(TAG, "Leaving audio sync: Audio PTS = {}", audio_pts);
870 case 0x10: //Teletext Is this correct?
871 packet.setSubstream(substream_id);
872 // Extract teletxt PTS if it exists
875 teletext_pts = packet.getPTS();
881 ispre_1_3_19=true; //switching to compat mode and live tv mode
882 goto pre_1_3_19_Recording;
886 packet.setSubstream(0);
892 else if (packet.getPacketType() >= PESTYPE_VID0 &&
893 packet.getPacketType() <= PESTYPE_VIDMAX)
895 // Extract video PTS if it exists
896 if (packet.hasPTS()) video_pts = packet.getPTS();
898 // If there is a sequence header, extract information
899 u4 pos = packet.findSeqHeader(h264);
904 if (pos+6 >= packet.getSize()) return;
905 horizontal_size = ((int)packet[pos] << 4) | ((int)packet[pos+1] >> 4);
907 vertical_size = (((int)packet[pos+1] & 0xf) << 8) | (int)packet[pos+2];
909 enum AspectRatio aspect=(enum AspectRatio)(packet[pos+3] >> 4);
912 const int aspectDAR[]={0,1,1,1,4,3,16,9,221,100};
913 int aspectDARx=aspectDAR[aspect*2];
914 int aspectDARy=aspectDAR[aspect*2+1];
916 aspectx=aspectDARx*vertical_size;
917 aspecty=aspectDARy*horizontal_size;
921 // LogNT::getInstance()->debug(TAG, "PAR test1 {} {} {} {}", aspectx, aspecty, commona, commonb);
927 commonb=commona%commonb;
930 aspectx=aspectx/commona;
931 aspecty=aspecty/commona;
932 //LogNT::getInstance()->debug(TAG, "PAR test2 {} {} {} {} {} {} {}", aspectx,aspecty,aspectDARx,aspectDARy,horizontal_size,vertical_size,commona);
934 setAspectRatio(aspect,aspectx,aspecty);
935 frame_rate = packet[pos+3] & 0x0f;
936 if (frame_rate >= 1 && frame_rate <= 8)
937 frame_rate = FrameRates[frame_rate];
940 bit_rate = ((int)packet[pos+4] << 10) |
941 ((int)packet[pos+5] << 2) |
942 ((int)packet[pos+6] >> 6);
946 /* Chris and Mark I know this is ugly, should we move this to a method of PESPacket or to NALUUnit what would be better?
947 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*/
948 NALUUnit nalu(packet.getData()+pos,packet.getSize()-pos);
949 profile=nalu.getBits(8);
950 nalu.getBits(8); //constraints
951 nalu.getBits(8); //level_idc
952 nalu.getUe(); //seq_parameter_set_id
954 if (profile==100 || profile==110 || profile==122 || profile==144)
961 nalu.getUe(); //bit depth lume
962 nalu.getUe(); //bit depth chrome
966 for (int i=0;i<8;i++){
973 for (int j=0;j<16;j++) {
975 u4 delta=nalu.getSe();
976 nextscale=(lastscale+delta+256)%256;
978 lastscale=(nextscale==0)?lastscale:nextscale;
985 for (int j=0;j<64;j++) {
987 u4 delta=nalu.getSe();
988 nextscale=(lastscale+delta+256)%256;
990 lastscale=(nextscale==0)?lastscale:nextscale;
1001 chromunitx=chromunity=1; break;
1003 chromunitx=chromunity=2; break;
1005 chromunitx=2;chromunity=1; break;
1007 chromunitx=chromunity=1; break;
1010 nalu.getUe(); //log2framenum
1011 u4 temp=nalu.getUe();
1012 if (temp==0) //pict order
1018 u4 temp2=nalu.getUe();
1019 for (u4 i=0;i<temp2;i++)
1022 nalu.getUe(); //Num refframes
1024 horizontal_size=(nalu.getUe()+1)*16;
1026 vertical_size=(nalu.getUe()+1)*16;
1027 int tinterlaced=nalu.getBits(1);
1028 vertical_size*=(2-tinterlaced);
1029 interlaced=!tinterlaced;
1031 if (!tinterlaced) nalu.getBits(1);
1033 if (nalu.getBits(1))
1035 horizontal_size-=nalu.getUe()*chromunitx;
1036 horizontal_size-=nalu.getUe()*chromunitx;
1037 vertical_size-=nalu.getUe()*(2-tinterlaced)*chromunity;
1038 vertical_size-=nalu.getUe()*(2-tinterlaced)*chromunity;
1040 if (nalu.getBits(1))
1042 if (nalu.getBits(1))
1044 u4 aspectratioidc=nalu.getBits(8);
1045 bool hasaspect=false;
1046 int aspectx,aspecty;
1047 const float aspects[]={1., 1./1.,12./11.,10./11.,16./11.,40./33.,
1048 24./11.,20./11.,32./11.,80./33.,18./11.,15./11.,64./33.,160./99.,4./3.,3./2.,2./1.};
1049 const int aspectsx[]={1, 1,12,10,16,40,
1050 24,20,32,80,18,15,64,160,4,3,2};
1051 const int aspectsy[]={1, 1,11,11,11,33,11,11,11,33,11,11,33,99,3,2,1};
1053 float aspectratio=((float) horizontal_size)/((float) vertical_size);
1054 if (aspectratioidc<=16)
1057 aspectratio*=aspects[aspectratioidc];
1058 aspectx=aspectsx[aspectratioidc];
1059 aspecty=aspectsy[aspectratioidc];
1062 else if (aspectratioidc==255)
1064 int t_sar_width=nalu.getBits(16);
1065 int t_sar_height=nalu.getBits(16);
1066 if (t_sar_width!=0 && t_sar_height!=0)
1069 aspectratio*=((float)t_sar_width)/((float)t_sar_height);
1070 aspectx=t_sar_width;
1071 aspecty=t_sar_height;
1076 if (fabs(aspectratio-16./9.)<0.1) setAspectRatio(ASPECT_16_9,aspectx,aspecty);
1077 else if (fabs(aspectratio-4./3.)<0.1) setAspectRatio(ASPECT_4_3,aspectx,aspecty);
1078 else setAspectRatio(ASPECT_1_1,aspectx,aspecty);
1085 u4 posext = packet.findSeqExtHeader(h264);
1088 interlaced=!(packet[pos+1] & 0x08); //really simple
1089 // if more than full hd is coming we need to add additional parsers here
1091 /* NALUUnit nalu(packet.getData()+posext,packet.getSize()-posext);
1092 while (!nalu.isEonalu()) {
1093 unsigned int payloadtype=0;
1094 unsigned int payloadadd=0xFF;
1095 while (payloadadd==0xFF && !nalu.isEonalu()) {
1096 payloadadd=nalu.getBits(8);
1097 payloadtype+=payloadadd;
1099 unsigned int payloadsize=0;
1101 while (payloadadd==0xFF && !nalu.isEonalu()) {
1102 payloadadd=nalu.getBits(8);
1103 payloadsize+=payloadadd;
1105 switch (payloadtype) {
1106 case 1: { // picture timing SEI
1110 while (payloadsize) { // not handled skip
1128 video_pts_seek = video_pts;
1129 LogNT::getInstance()->debug(TAG, "Entering audio sync: Video PTS = {}", video_pts);
1130 LogNT::getInstance()->debug(TAG, "Entering audio sync: Audio PTS = {}", audio_pts);
1137 u4 Demuxer::stripAudio(u1* buf, u4 len)
1139 u4 read_pos = 0, write_pos = 0;
1140 u4 pattern, packet_length;
1141 if (len < 4) return 0;
1142 pattern = (buf[0] << 16) | (buf[1] << 8) | (buf[2]);
1143 while (read_pos + 7 <= len)
1145 pattern = ((pattern & 0xFFFFFF) << 8) | buf[read_pos+3];
1146 if (pattern < (0x100|PESTYPE_VID0) || pattern > (0x100|PESTYPE_VIDMAX))
1150 packet_length = ((buf[read_pos+4] << 8) | (buf[read_pos+5])) + 6;
1151 if (read_pos + packet_length > len)
1155 if (read_pos != write_pos)
1156 memmove(buf+write_pos, buf+read_pos, packet_length);
1157 read_pos += packet_length;
1158 write_pos += packet_length;
1159 pattern = (buf[read_pos] << 16) | (buf[read_pos+1] << 8)
1160 | (buf[read_pos+2]);
1167 void Demuxer::changeTimes(u1* buf, u4 len,u4 playtime)
1169 u4 pattern, packet_length;
1171 if (len < 4) return;
1172 pattern = (buf[0] << 16) | (buf[1] << 8) | (buf[2]);
1173 while (read_pos + 7 <= len)
1175 pattern = ((pattern & 0xFFFFFF) << 8) | buf[read_pos+3];
1176 if (pattern < (0x100|PESTYPE_VID0) || pattern > (0x100|PESTYPE_VIDMAX))
1180 packet_length = ((buf[read_pos+4] << 8) | (buf[read_pos+5])) + 6;
1181 // ok we have a packet figure out if pts and dts are present and replace them
1182 if (read_pos + 19 > len) return;
1183 u8 new_ts=playtime*90; //play time is on ms so multiply it by 90
1184 if (buf[read_pos+7] & 0x80) { // pts is here, replace it
1185 buf[read_pos+9]=0x21 | (( new_ts>>29)& 0xde );
1186 buf[read_pos+10]=0x00 |(( new_ts>>22)& 0xff );
1187 buf[read_pos+11]=0x01 | (( new_ts>>14)& 0xfe );
1188 buf[read_pos+12]=0x00 | (( new_ts>>7)& 0xff );
1189 buf[read_pos+13]=0x01 | (( new_ts<<1)& 0xfe );
1192 if (buf[read_pos+7] & 0x40) { // pts is here, replace it
1193 buf[read_pos+14]=0x21 | (( new_ts>>29)& 0xde );
1194 buf[read_pos+15]=0x00 | (( new_ts>>22)& 0xff );
1195 buf[read_pos+16]=0x01 | (( new_ts>>14)& 0xfe );
1196 buf[read_pos+17]=0x00 | (( new_ts>>7)& 0xff );
1197 buf[read_pos+18]=0x01 | (( new_ts<<1)& 0xfe );
1199 read_pos += packet_length;
1200 pattern = (buf[read_pos] << 16) | (buf[read_pos+1] << 8)
1201 | (buf[read_pos+2]);
1207 bool Demuxer::scanForVideo(u1* buf, u4 len, bool &ish264)
1212 if (len < 4) return false;
1213 pattern = (buf[0] << 16) | (buf[1] << 8) | (buf[2]);
1216 pattern = ((pattern & 0xFFFFFF) << 8) | buf[pos++];
1217 if (pattern >= (0x100|PESTYPE_VID0) && pattern <= (0x100|PESTYPE_VIDMAX))
1223 bool* Demuxer::getmpAudioChannels()
1225 return avail_mpaudchan;
1228 bool* Demuxer::getac3AudioChannels()
1230 return avail_ac3audchan;
1233 bool* Demuxer::getSubtitleChannels()
1235 return avail_dvbsubtitlechan;
1238 int Demuxer::getselSubtitleChannel()
1240 return subtitle_current;
1243 int Demuxer::getselAudioChannel()
1245 return audio_current;