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
41 #define SEEK_THRESHOLD 150000 // About 1.5 seconds
44 const int Demuxer::FrameRates[9] = { 0, 23, 24, 25, 29, 30, 50, 59, 60 };
45 Demuxer* Demuxer::instance = NULL;
49 NALUUnit(const UCHAR* buf,UINT length_buf);
52 inline UINT getBits(UINT num_bits);
55 bool isEonalu() {return eonalu;};
67 NALUUnit::NALUUnit(const UCHAR *buf, UINT length_buf)
79 UINT pattern =(((UINT)buf[ 0] << 16) |
83 while (pattern != 0x000001)
85 if (++nalu_start >= length_buf) return;
86 pattern = ((pattern << 8) | buf[nalu_start])&0x00FFFFFF;
88 nalu_end=nalu_start+1;
89 pattern = ((pattern << 8) | buf[nalu_end])&0x00FFFFFF;
91 while (pattern != 0x000001 && pattern != 0x000000)
93 if (++nalu_end >= length_buf) { nalu_end+=3;break;};
94 pattern = ((pattern << 8) | buf[nalu_end])&0x00FFFFFF;
97 nalu_end=min(length_buf-1,nalu_end);
98 nalu_length=nalu_end-nalu_start;
99 nalu_buf=(UCHAR*)malloc(nalu_length);
100 memcpy(nalu_buf,buf+nalu_start,nalu_length);
104 NALUUnit::~NALUUnit()
106 if (nalu_buf) free(nalu_buf);
109 inline UINT NALUUnit::getBits(UINT num_bits)
111 if (num_bits==0) return 0; //???
112 UINT remain_bits=num_bits;
114 //May be slow, but should work!
115 while (remain_bits>0) {
119 last_bytes=(last_bytes<<8) & nalu_buf[pos];
120 if ((last_bytes & 0x00FFFFFF) == 0x000003) pos++; //emulation prevention byte
123 working_byte=nalu_buf[pos];
139 UINT fetch_bits=min(remain_bits,8-bit_pos);
140 work=work <<fetch_bits;
141 //work|=((working_byte>>bit_pos) & (0xFF>>(8-fetch_bits)));
142 work|=(working_byte &(0xFF>>(bit_pos)))>>(8-fetch_bits-bit_pos);
143 remain_bits-=fetch_bits;
144 bit_pos=(bit_pos+fetch_bits)%8;
149 UINT NALUUnit::getUe()
153 for( bit = 0; !bit && !eonalu; leadbits++ )
155 if (eonalu) return true;
156 return ((1 << leadbits)-1)+getBits(leadbits);
159 int NALUUnit::getSe()
162 if (input==0) return 0;
163 int output=((input+1)>>1);
164 if (input & 0x1) output*=-1;
170 static const int PESPacket_initial_size = 2000;
173 PESPacket::PESPacket()
175 data_size = PESPacket_initial_size;
176 data = (UCHAR*)malloc(data_size);
183 PESPacket::PESPacket(const PESPacket& packet)
188 PESPacket& PESPacket::operator=(const PESPacket& packet)
192 if (data) free(data);
198 PESPacket::~PESPacket()
200 if (data) free(data);
203 void PESPacket::copyFrom(const PESPacket& packet)
205 length = packet.length;
207 packetType = packet.packetType;
208 substream = packet.substream;
209 seq_header = packet.seq_header;
211 data = (UCHAR*)malloc(data_size);
212 memcpy(data, packet.data, data_size);
215 void PESPacket::init(UCHAR type, UCHAR sub)
219 data[4] = data[5] = 0;
223 seq_header = 1; // Unknown seq_header status
227 void PESPacket::truncate()
229 init(packetType,substream);
234 int PESPacket::write(const UCHAR *buf, int len)
238 if (size + len > 0x10000) return 0;
239 if (size + len > data_size)
241 UINT new_data_size = max(data_size + data_size / 2, data_size + len);
242 if (new_data_size > 0x10000) new_data_size = 0x10000;
243 data_size = new_data_size;
244 data = (UCHAR*)realloc(data, data_size);
246 memcpy(data + size, buf, len);
249 data[4] = (length >> 8);
250 data[5] = (length & 0xFF);
251 // We have added data - reset seq_header indicator if necessary
252 if (seq_header == 0) seq_header = 1; // Reset to 'unknown'
256 ULLONG PESPacket::getPTS() const
258 if ( ( (packetType >= Demuxer::PESTYPE_AUD0 &&
259 packetType <= Demuxer::PESTYPE_AUDMAX)
261 (packetType >= Demuxer::PESTYPE_VID0 &&
262 packetType <= Demuxer::PESTYPE_VIDMAX)
264 packetType == Demuxer::PESTYPE_PRIVATE_1
266 && size >= 14 && data[7] & 0x80)
268 return ( (ULLONG)(data[ 9] & 0x0E) << 29) |
269 ( (ULLONG)(data[10]) << 22 ) |
270 ( (ULLONG)(data[11] & 0xFE) << 14 ) |
271 ( (ULLONG)(data[12]) << 7 ) |
272 ( (ULLONG)(data[13] & 0xFE) >> 1 );
274 else return PTS_INVALID;
277 UCHAR PESPacket::operator[] (UINT index) const
285 UINT PESPacket::findPictureHeader(bool h264) const
287 if (size < 12) return 0;
288 UINT pattern = ( ((UINT)data[ 8] << 24) |
289 ((UINT)data[ 9] << 16) |
290 ((UINT)data[10] << 8) |
295 while (pattern != DEMUXER_H264_ACCESS_UNIT)
297 if (++pos >= size) return 0;
298 pattern = (pattern << 8) | data[pos];
302 while (pattern != DEMUXER_PIC_HEAD)
304 if (++pos >= size) return 0;
305 pattern = (pattern << 8) | data[pos];
311 UINT PESPacket::countPictureHeaders(bool h264) const
313 if (size < 12) return 0;
314 UINT pattern = ( ((UINT)data[ 8] << 24) |
315 ((UINT)data[ 9] << 16) |
316 ((UINT)data[10] << 8) |
325 pattern = (pattern << 8) | data[pos];
326 if (pattern==DEMUXER_H264_ACCESS_UNIT) count++;
333 pattern = (pattern << 8) | data[pos];
334 if (pattern==DEMUXER_PIC_HEAD) count++;
340 UINT PESPacket::findSeqHeader(bool h264) const
342 if (seq_header != 1) return seq_header;
343 if (size < 12) return 0;
344 UINT pattern = ( ((UINT)data[ 8] << 24) |
345 ((UINT)data[ 9] << 16) |
346 ((UINT)data[10] << 8) |
350 while ((pattern & 0xFFFFFF1F) != DEMUXER_H264_SEQ_PARAMETER_SET)
357 pattern = (pattern << 8) | data[pos];
359 seq_header = pos - 3;
363 while (pattern != DEMUXER_SEQ_HEAD)
370 pattern = (pattern << 8) | data[pos];
372 seq_header = pos - 3;
377 UINT PESPacket::findSeqExtHeader(bool h264) const
379 if (seq_header != 1) return seq_header;
380 if (size < 12) return 0;
381 UINT pattern = ( ((UINT)data[ 8] << 24) |
382 ((UINT)data[ 9] << 16) |
383 ((UINT)data[10] << 8) |
387 while ((pattern & 0xFFFFFF1F) != DEMUXER_H264_SUB_ENHANCEMENT_INF)
394 pattern = (pattern << 8) | data[pos];
396 seq_header = pos - 3;
400 while (pattern != DEMUXER_SEQ_EXT_HEAD && (data[pos+1]&0xf0)!=0x10)
402 if (++pos >= (size-1))
407 pattern = (pattern << 8) | data[pos];
409 seq_header = pos - 3;
417 if (instance) return;
422 vid_seeking = aud_seeking = false;
423 video_pts = audio_pts = 0;
424 ispre_1_3_19 = false;
437 Demuxer* Demuxer::getInstance()
442 int Demuxer::init(Callback* tcallback, DrainTarget* audio, DrainTarget* video, DrainTarget* teletext,
443 ULONG demuxMemoryV, ULONG demuxMemoryA, ULONG demuxMemoryT,double infps, DVBSubtitles* tsubtitles)
447 if ( !videostream.init(video, demuxMemoryV) ||
448 !audiostream.init(audio, demuxMemoryA) ||
449 !teletextstream.init(teletext, demuxMemoryT))
451 Log::getInstance()->log("Demuxer", Log::CRIT,
452 "Failed to initialize demuxer");
458 isteletextdecoded = true;
460 isteletextdecoded = false;
465 subtitles = tsubtitles;
466 callback = tcallback;
470 void Demuxer::reset()
472 Log::getInstance()->log("Demuxer", Log::DEBUG, "Reset called");
474 video_current = audio_current = teletext_current = subtitle_current = -1;
475 horizontal_size = vertical_size = 0;
476 interlaced=true; // default is true
477 aspect_ratio = (enum AspectRatio) 0;
480 frame_rate = bit_rate = 0;
481 ispre_1_3_19 = false;
486 for (int i = 0; i <= (PESTYPE_AUDMAX - PESTYPE_AUD0); i++)
488 avail_mpaudchan[i] = false;
490 for (int i = 0; i <= (PESTYPE_SUBSTREAM_AC3MAX - PESTYPE_SUBSTREAM_AC30); i++)
492 avail_ac3audchan[i] = false;
494 for (int i = 0; i <= (PESTYPE_SUBSTREAM_DVBSUBTITLEMAX - PESTYPE_SUBSTREAM_DVBSUBTITLE0); i++)
496 avail_dvbsubtitlechan[i] = false;
500 int Demuxer::shutdown()
502 videostream.shutdown();
503 audiostream.shutdown();
504 teletextstream.shutdown();
509 void Demuxer::flush()
511 Log::getInstance()->log("Demuxer", Log::DEBUG, "Flush called");
515 teletextstream.flush();
518 void Demuxer::flushAudio()
525 vid_seeking = aud_seeking = true;
526 video_pts = audio_pts = teletext_pts = 0;
529 void Demuxer::setAudioStream(int id)
534 void Demuxer::setVideoStream(int id)
539 void Demuxer::setTeletextStream(int id)
541 teletext_current = id;
544 void Demuxer::setDVBSubtitleStream(int id)
546 subtitle_current = id;
549 void Demuxer::setAspectRatio(enum AspectRatio ar, int taspectx, int taspecty)
551 if (aspect_ratio != ar)
553 Log::getInstance()->log("Demux", Log::DEBUG,
554 "Aspect ratio difference signalled");
555 if (++arcnt > 3) // avoid changing aspect ratio if glitch in signal
561 if (callback) callback->call(this);
568 bool Demuxer::writeAudio(bool * dataavail)
570 return audiostream.drain(dataavail);
573 bool Demuxer::writeVideo(bool * dataavail)
575 return videostream.drain(dataavail);
578 bool Demuxer::writeTeletext(bool * dataavail)
580 return teletextstream.drain(dataavail);
583 bool Demuxer::submitPacket(PESPacket& packet)
586 UCHAR packet_type = packet.getPacketType();
587 const UCHAR* packetdata = packet.getData();
588 if (packet_type >= PESTYPE_VID0 && packet_type <= PESTYPE_VIDMAX)
590 if (video_current == -1) video_current = packet_type;
591 if (video_current == packet_type && !vid_seeking)
593 sent = videostream.put(&packetdata[0], packet.getSize(), h264?MPTYPE_VIDEO_H264:MPTYPE_VIDEO_MPEG2,packetnum);
594 if (sent) packetnum++;
597 sent = packet.getSize();
599 else if (packet_type >= PESTYPE_AUD0 && packet_type <= PESTYPE_AUDMAX)
602 if (audio_current == -1) audio_current = packet_type;
603 avail_mpaudchan[packet_type - PESTYPE_AUD0] = true;
604 if (audio_current == packet_type && !aud_seeking)
606 UCHAR type=MPTYPE_MPEG_AUDIO;
611 type=MPTYPE_MPEG_AUDIO; break;
613 type=MPTYPE_AAC_LATM; break;
615 sent = audiostream.put(&packetdata[0], packet.getSize(), type,packetnum);
616 if (sent) packetnum++;
619 sent = packet.getSize();
621 else if (packet_type == PESTYPE_PRIVATE_1 &&
622 packet.getSubstream() >= PESTYPE_SUBSTREAM_AC30 &&
623 packet.getSubstream() <= PESTYPE_SUBSTREAM_AC3MAX)
625 avail_ac3audchan[packet.getSubstream() - PESTYPE_SUBSTREAM_AC30] = true;
626 if (packet.getSubstream() == audio_current)
628 sent = audiostream.put(&packetdata[0], packet.getSize(), (ispre_1_3_19)? MPTYPE_AC3_PRE13 : MPTYPE_AC3,packetnum);
629 if (sent) packetnum++;
633 sent = packet.getSize();
636 else if (packet_type == PESTYPE_PRIVATE_1 &&
637 packet.getSubstream() >= PESTYPE_SUBSTREAM_DVBSUBTITLE0 &&
638 packet.getSubstream() <= PESTYPE_SUBSTREAM_DVBSUBTITLEMAX)
640 avail_dvbsubtitlechan[packet.getSubstream()-PESTYPE_SUBSTREAM_DVBSUBTITLE0]=true;
641 if (subtitle_current == -1) subtitle_current = packet.getSubstream();
642 if (subtitles && packet.getSubstream()==subtitle_current)
644 subtitles->put(packet);
646 sent = packet.getSize();
648 else if (isteletextdecoded && packet_type == PESTYPE_PRIVATE_1 &&
649 packet.getSubstream() >= PESTYPE_SUBSTREAM_TELETEXT0 &&
650 packet.getSubstream() <= PESTYPE_SUBSTREAM_TELETEXTMAX)
653 if (teletext_current == -1) teletext_current = packet.getSubstream();
654 if (teletext_current == packet.getSubstream())
656 sent = teletextstream.put(&packetdata[0], packet.getSize(), MPTYPE_TELETEXT,packetnum);
660 sent = packet.getSize();
665 sent = packet.getSize();
668 if (sent < packet.getSize()) // Stream is full.
674 void Demuxer::parsePacketDetails(PESPacket& packet)
676 if (packet.getPacketType() >= PESTYPE_AUD0 &&
677 packet.getPacketType() <= PESTYPE_AUDMAX)
679 // Extract audio PTS if it exists
682 audio_pts = packet.getPTS();
683 // We continue to seek on the audio if the video PTS that we
684 // are trying to match is ahead of the audio PTS by at most
685 // SEEK_THRESHOLD. We consider the possibility of PTS wrap.
686 if (aud_seeking && !vid_seeking &&
687 !( (video_pts_seek > audio_pts &&
688 video_pts_seek - audio_pts < SEEK_THRESHOLD)
690 (video_pts_seek < audio_pts &&
691 video_pts_seek + (1LL<<33) - audio_pts < SEEK_THRESHOLD) ))
694 Log::getInstance()->log("Demuxer", Log::DEBUG,
695 "Leaving audio sync: Audio PTS = %llu", audio_pts);
699 else if (packet.getPacketType() == PESTYPE_PRIVATE_1) // Private stream
701 //Inspired by vdr's device.c
702 int payload_begin = packet[8]+9;
703 unsigned char substream_id = packet[payload_begin];
704 unsigned char substream_type = substream_id & 0xF0;
705 unsigned char substream_index = substream_id & 0x1F;
706 pre_1_3_19_Recording: //This is for old recordings stuff and live TV
709 int old_substream=packet.getSubstream();
710 if (old_substream){ //someone else already set it, this is live tv
711 substream_id = old_substream;
712 substream_type = substream_id & 0xF0;
713 substream_index = substream_id & 0x1F;
715 substream_id = PESTYPE_PRIVATE_1;
716 substream_type = 0x80;
721 switch (substream_type)
725 packet.setSubstream(substream_id);
727 case 0xA0: //LPCM //not supported yet, is there any LPCM transmissio out there?
729 case 0x80: //ac3, currently only one ac3 track per recording supported
730 packet.setSubstream(substream_type+substream_index);
732 // Extract audio PTS if it exists
735 audio_pts = packet.getPTS();
736 // We continue to seek on the audio if the video PTS that we
737 // are trying to match is ahead of the audio PTS by at most
738 // SEEK_THRESHOLD. We consider the possibility of PTS wrap.
739 if (aud_seeking && !vid_seeking &&
740 !( (video_pts_seek > audio_pts &&
741 video_pts_seek - audio_pts < SEEK_THRESHOLD)
743 (video_pts_seek < audio_pts &&
744 video_pts_seek + (1LL<<33) - audio_pts < SEEK_THRESHOLD) ))
747 Log::getInstance()->log("Demuxer", Log::DEBUG, "Leaving audio sync: Audio PTS = %llu", audio_pts);
751 case 0x10: //Teletext Is this correct?
752 packet.setSubstream(substream_id);
753 // Extract teletxt PTS if it exists
756 teletext_pts = packet.getPTS();
762 ispre_1_3_19=true; //switching to compat mode and live tv mode
763 goto pre_1_3_19_Recording;
767 packet.setSubstream(0);
772 else if (packet.getPacketType() >= PESTYPE_VID0 &&
773 packet.getPacketType() <= PESTYPE_VIDMAX)
775 // Extract video PTS if it exists
776 if (packet.hasPTS()) video_pts = packet.getPTS();
778 // If there is a sequence header, extract information
779 UINT pos = packet.findSeqHeader(h264);
784 if (pos+6 >= packet.getSize()) return;
785 horizontal_size = ((int)packet[pos] << 4) | ((int)packet[pos+1] >> 4);
787 vertical_size = (((int)packet[pos+1] & 0xf) << 8) | (int)packet[pos+2];
789 enum AspectRatio aspect=(enum AspectRatio)(packet[pos+3] >> 4);
792 const int aspectDAR[]={0,1,1,1,4,3,16,9,221,100};
793 int aspectDARx=aspectDAR[aspect*2];
794 int aspectDARy=aspectDAR[aspect*2+1];
796 aspectx=aspectDARx*vertical_size;
797 aspecty=aspectDARy*horizontal_size;
801 // Log::getInstance()->log("Demuxer", Log::DEBUG, "PAR test1 %d %d %d %d ", aspectx,aspecty,commona,commonb);
807 commonb=commona%commonb;
810 aspectx=aspectx/commona;
811 aspecty=aspecty/commona;
812 //Log::getInstance()->log("Demuxer", Log::DEBUG, "PAR test2 %d %d %d %d %d %d %d", aspectx,aspecty,aspectDARx,aspectDARy,horizontal_size,vertical_size,commona);
814 setAspectRatio(aspect,aspectx,aspecty);
815 frame_rate = packet[pos+3] & 0x0f;
816 if (frame_rate >= 1 && frame_rate <= 8)
817 frame_rate = FrameRates[frame_rate];
820 bit_rate = ((int)packet[pos+4] << 10) |
821 ((int)packet[pos+5] << 2) |
822 ((int)packet[pos+6] >> 6);
826 /* Chris and Mark I know this is ugly, should we move this to a method of PESPacket or to NALUUnit what would be better?
827 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*/
828 NALUUnit nalu(packet.getData()+pos,packet.getSize()-pos);
829 profile=nalu.getBits(8);
830 nalu.getBits(8); //constraints
831 nalu.getBits(8); //level_idc
832 nalu.getUe(); //seq_parameter_set_id
834 if (profile==100 || profile==110 || profile==122 || profile==144)
841 nalu.getUe(); //bit depth lume
842 nalu.getUe(); //bit depth chrome
846 for (int i=0;i<8;i++){
853 for (int j=0;j<16;j++) {
855 UINT delta=nalu.getSe();
856 nextscale=(lastscale+delta+256)%256;
858 lastscale=(nextscale==0)?lastscale:nextscale;
865 for (int j=0;j<64;j++) {
867 UINT delta=nalu.getSe();
868 nextscale=(lastscale+delta+256)%256;
870 lastscale=(nextscale==0)?lastscale:nextscale;
881 chromunitx=chromunity=1; break;
883 chromunitx=chromunity=2; break;
885 chromunitx=2;chromunity=1; break;
887 chromunitx=chromunity=1; break;
890 nalu.getUe(); //log2framenum
891 UINT temp=nalu.getUe();
892 if (temp==0) //pict order
898 UINT temp2=nalu.getUe();
899 for (int i=0;i<temp2;i++)
902 nalu.getUe(); //Num refframes
904 horizontal_size=(nalu.getUe()+1)*16;
906 vertical_size=(nalu.getUe()+1)*16;
907 int tinterlaced=nalu.getBits(1);
908 vertical_size*=(2-tinterlaced);
909 interlaced=!tinterlaced;
911 if (!tinterlaced) nalu.getBits(1);
915 horizontal_size-=nalu.getUe()*chromunitx;
916 horizontal_size-=nalu.getUe()*chromunitx;
917 vertical_size-=nalu.getUe()*(2-tinterlaced)*chromunity;
918 vertical_size-=nalu.getUe()*(2-tinterlaced)*chromunity;
924 UINT aspectratioidc=nalu.getBits(8);
925 bool hasaspect=false;
927 const float aspects[]={1., 1./1.,12./11.,10./11.,16./11.,40./33.,
928 24./11.,20./11.,32./11.,80./33.,18./11.,15./11.,64./33.,160./99.,4./3.,3./2.,2./1.};
929 const int aspectsx[]={1, 1,12,10,16,40,
930 24,20,32,80,18,15,64,160,4,3,2};
931 const int aspectsy[]={1, 1,11,11,11,33,11,11,11,33,11,11,33,99,3,2,1};
933 float aspectratio=((float) horizontal_size)/((float) vertical_size);
934 if (aspectratioidc<=16)
937 aspectratio*=aspects[aspectratioidc];
938 aspectx=aspectsx[aspectratioidc];
939 aspecty=aspectsy[aspectratioidc];
942 else if (aspectratioidc==255)
944 int t_sar_width=nalu.getBits(16);
945 int t_sar_height=nalu.getBits(16);
946 if (t_sar_width!=0 && t_sar_height!=0)
949 aspectratio*=((float)t_sar_width)/((float)t_sar_height);
951 aspecty=t_sar_height;
956 if (fabs(aspectratio-16./9.)<0.1) setAspectRatio(ASPECT_16_9,aspectx,aspecty);
957 else if (fabs(aspectratio-4./3.)<0.1) setAspectRatio(ASPECT_4_3,aspectx,aspecty);
958 else setAspectRatio(ASPECT_1_1,aspectx,aspecty);
965 UINT posext = packet.findSeqExtHeader(h264);
968 interlaced=!(packet[pos+1] & 0x08); //really simple
969 // if more than full hd is coming we need to add additional parsers here
971 /* NALUUnit nalu(packet.getData()+posext,packet.getSize()-posext);
972 while (!nalu.isEonalu()) {
973 unsigned int payloadtype=0;
974 unsigned int payloadadd=0xFF;
975 while (payloadadd==0xFF && !nalu.isEonalu()) {
976 payloadadd=nalu.getBits(8);
977 payloadtype+=payloadadd;
979 unsigned int payloadsize=0;
981 while (payloadadd==0xFF && !nalu.isEonalu()) {
982 payloadadd=nalu.getBits(8);
983 payloadsize+=payloadadd;
985 switch (payloadtype) {
986 case 1: { // picture timing SEI
990 while (payloadsize) { // not handled skip
1008 video_pts_seek = video_pts;
1009 Log::getInstance()->log("Demuxer", Log::DEBUG,
1010 "Entering audio sync: Video PTS = %llu", video_pts);
1011 Log::getInstance()->log("Demuxer", Log::DEBUG,
1012 "Entering audio sync: Audio PTS = %llu", audio_pts);
1019 UINT Demuxer::stripAudio(UCHAR* buf, UINT len)
1021 UINT read_pos = 0, write_pos = 0;
1022 UINT pattern, packet_length;
1023 if (len < 4) return 0;
1024 pattern = (buf[0] << 16) | (buf[1] << 8) | (buf[2]);
1025 while (read_pos + 7 <= len)
1027 pattern = ((pattern & 0xFFFFFF) << 8) | buf[read_pos+3];
1028 if (pattern < (0x100|PESTYPE_VID0) || pattern > (0x100|PESTYPE_VIDMAX))
1032 packet_length = ((buf[read_pos+4] << 8) | (buf[read_pos+5])) + 6;
1033 if (read_pos + packet_length > len)
1037 if (read_pos != write_pos)
1038 memmove(buf+write_pos, buf+read_pos, packet_length);
1039 read_pos += packet_length;
1040 write_pos += packet_length;
1041 pattern = (buf[read_pos] << 16) | (buf[read_pos+1] << 8)
1042 | (buf[read_pos+2]);
1049 void Demuxer::changeTimes(UCHAR* buf, UINT len,UINT playtime)
1051 UINT pattern, packet_length;
1053 if (len < 4) return;
1054 pattern = (buf[0] << 16) | (buf[1] << 8) | (buf[2]);
1055 while (read_pos + 7 <= len)
1057 pattern = ((pattern & 0xFFFFFF) << 8) | buf[read_pos+3];
1058 if (pattern < (0x100|PESTYPE_VID0) || pattern > (0x100|PESTYPE_VIDMAX))
1062 packet_length = ((buf[read_pos+4] << 8) | (buf[read_pos+5])) + 6;
1063 // ok we have a packet figure out if pts and dts are present and replace them
1064 if (read_pos + 19 > len) return;
1065 ULLONG new_ts=playtime*90; //play time is on ms so multiply it by 90
1066 if (buf[read_pos+7] & 0x80) { // pts is here, replace it
1067 buf[read_pos+9]=0x21 | (( new_ts>>29)& 0xde );
1068 buf[read_pos+10]=0x00 |(( new_ts>>22)& 0xff );
1069 buf[read_pos+11]=0x01 | (( new_ts>>14)& 0xfe );
1070 buf[read_pos+12]=0x00 | (( new_ts>>7)& 0xff );
1071 buf[read_pos+13]=0x01 | (( new_ts<<1)& 0xfe );
1074 if (buf[read_pos+7] & 0x40) { // pts is here, replace it
1075 buf[read_pos+14]=0x21 | (( new_ts>>29)& 0xde );
1076 buf[read_pos+15]=0x00 | (( new_ts>>22)& 0xff );
1077 buf[read_pos+16]=0x01 | (( new_ts>>14)& 0xfe );
1078 buf[read_pos+17]=0x00 | (( new_ts>>7)& 0xff );
1079 buf[read_pos+18]=0x01 | (( new_ts<<1)& 0xfe );
1081 read_pos += packet_length;
1082 pattern = (buf[read_pos] << 16) | (buf[read_pos+1] << 8)
1083 | (buf[read_pos+2]);
1089 bool Demuxer::scanForVideo(UCHAR* buf, UINT len, bool &ish264)
1094 if (len < 4) return false;
1095 pattern = (buf[0] << 16) | (buf[1] << 8) | (buf[2]);
1098 pattern = ((pattern & 0xFFFFFF) << 8) | buf[pos++];
1099 if (pattern >= (0x100|PESTYPE_VID0) && pattern <= (0x100|PESTYPE_VIDMAX))
1105 bool* Demuxer::getmpAudioChannels()
1107 return avail_mpaudchan;
1110 bool* Demuxer::getac3AudioChannels()
1112 return avail_ac3audchan;
1115 bool* Demuxer::getSubtitleChannels()
1117 return avail_dvbsubtitlechan;
1120 int Demuxer::getselSubtitleChannel()
1122 return subtitle_current;
1125 int Demuxer::getselAudioChannel()
1127 return audio_current;