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"
28 #define DEMUXER_SEQ_HEAD 0x000001B3
29 #define DEMUXER_PIC_HEAD 0x00000101
30 #define SEEK_THRESHOLD 150000 // About 1.5 seconds
33 const int Demuxer::FrameRates[9] = { 0, 23, 24, 25, 29, 30, 50, 59, 60 };
34 Demuxer* Demuxer::instance = NULL;
37 PESPacket::PESPacket()
43 void PESPacket::init(UCHAR type, UCHAR sub)
51 seq_header = 1; // Unknown seq_header status
54 void PESPacket::truncate()
56 init(packetType,substream);
59 int PESPacket::write(const UCHAR *buf, int len)
61 if (length + len > 0xFFFA) return 0;
62 data.insert(data.end(), buf, buf+len);
65 data[4] = (length >> 8);
66 data[5] = (length & 0xFF);
67 // We have added data - reset seq_header indicator if necessary
68 if (seq_header == 0) seq_header = 1; // Reset to 'unknown'
72 ULLONG PESPacket::getPTS() const
74 if ( ( (packetType >= Demuxer::PESTYPE_AUD0 &&
75 packetType <= Demuxer::PESTYPE_AUDMAX)
77 (packetType >= Demuxer::PESTYPE_VID0 &&
78 packetType <= Demuxer::PESTYPE_VIDMAX)
80 packetType == Demuxer::PESTYPE_PRIVATE_1
82 && size >= 14 && data[7] & 0x80)
84 return ( (ULLONG)(data[ 9] & 0x0E) << 29) |
85 ( (ULLONG)(data[10]) << 22 ) |
86 ( (ULLONG)(data[11] & 0xFE) << 14 ) |
87 ( (ULLONG)(data[12]) << 7 ) |
88 ( (ULLONG)(data[13] & 0xFE) >> 1 );
90 else return PTS_INVALID;
93 UCHAR PESPacket::operator[] (UINT index) const
101 UINT PESPacket::findPictureHeader() const
103 if (size < 12) return 0;
104 UINT pattern = ( ((UINT)data[ 8] << 24) |
105 ((UINT)data[ 9] << 16) |
106 ((UINT)data[10] << 8) |
109 while (pattern != DEMUXER_PIC_HEAD)
111 if (++pos >= size) return 0;
112 pattern = (pattern << 8) | data[pos];
117 UINT PESPacket::findSeqHeader() const
119 if (seq_header != 1) return seq_header;
120 if (size < 12) return 0;
121 UINT pattern = ( ((UINT)data[ 8] << 24) |
122 ((UINT)data[ 9] << 16) |
123 ((UINT)data[10] << 8) |
126 while (pattern != DEMUXER_SEQ_HEAD)
133 pattern = (pattern << 8) | data[pos];
135 seq_header = pos - 3;
142 if (instance) return;
147 vid_seeking = aud_seeking = false;
148 video_pts = audio_pts = 0;
149 ispre_1_3_19 = false;
158 Demuxer* Demuxer::getInstance()
163 int Demuxer::init(Callback* tcallback, DrainTarget* audio, DrainTarget* video, ULONG demuxMemoryV, ULONG demuxMemoryA, DVBSubtitles* tsubtitles)
167 if ( !videostream.init(video, demuxMemoryV) ||
168 !audiostream.init(audio, demuxMemoryA))
170 Log::getInstance()->log("Demuxer", Log::CRIT,
171 "Failed to initialize demuxer");
179 subtitles = tsubtitles;
180 callback = tcallback;
184 void Demuxer::reset()
186 Log::getInstance()->log("Demuxer", Log::DEBUG, "Reset called");
188 video_current = audio_current = -1;
189 horizontal_size = vertical_size = 0;
190 aspect_ratio = (enum AspectRatio) 0;
191 frame_rate = bit_rate = 0;
192 ispre_1_3_19 = false;
194 for (int i = 0; i <= (PESTYPE_AUDMAX - PESTYPE_AUD0); i++)
196 avail_mpaudchan[i] = false;
198 for (int i = 0; i <= (PESTYPE_SUBSTREAM_AC3MAX - PESTYPE_SUBSTREAM_AC30); i++)
200 avail_ac3audchan[i] = false;
204 int Demuxer::shutdown()
206 videostream.shutdown();
207 audiostream.shutdown();
212 void Demuxer::flush()
214 Log::getInstance()->log("Demuxer", Log::DEBUG, "Flush called");
220 void Demuxer::flushAudio()
227 vid_seeking = aud_seeking = true;
228 video_pts = audio_pts = 0;
231 void Demuxer::setAudioStream(int id)
236 void Demuxer::setVideoStream(int id)
241 void Demuxer::setAspectRatio(enum AspectRatio ar)
243 if (aspect_ratio != ar)
245 Log::getInstance()->log("Demux", Log::DEBUG,
246 "Aspect ratio difference signalled");
247 if (++arcnt > 3) // avoid changing aspect ratio if glitch in signal
251 if (callback) callback->call(this);
258 bool Demuxer::writeAudio()
260 return audiostream.drain();
263 bool Demuxer::writeVideo()
265 return videostream.drain();
268 bool Demuxer::submitPacket(PESPacket& packet)
271 UCHAR packet_type = packet.getPacketType();
272 const std::vector<UCHAR>& packetdata = packet.getData();
274 if (packet_type >= PESTYPE_VID0 && packet_type <= PESTYPE_VIDMAX)
276 if (video_current == -1) video_current = packet_type;
277 if (video_current == packet_type && !vid_seeking)
278 sent = videostream.put(&packetdata[0], packet.getSize(), MPTYPE_VIDEO);
280 sent = packet.getSize();
282 else if (packet_type >= PESTYPE_AUD0 && packet_type <= PESTYPE_AUDMAX)
284 if (audio_current == -1) audio_current = packet_type;
285 avail_mpaudchan[packet_type - PESTYPE_AUD0] = true;
286 if (audio_current == packet_type && !aud_seeking)
287 sent = audiostream.put(&packetdata[0], packet.getSize(), MPTYPE_MPEG_AUDIO);
289 sent = packet.getSize();
291 else if (packet_type == PESTYPE_PRIVATE_1 &&
292 packet.getSubstream() >= PESTYPE_SUBSTREAM_AC30 &&
293 packet.getSubstream() <= PESTYPE_SUBSTREAM_AC3MAX)
295 avail_ac3audchan[packet.getSubstream() - PESTYPE_SUBSTREAM_AC30] = true;
296 if (packet.getSubstream() == audio_current)
298 sent = audiostream.put(&packetdata[0], packet.getSize(), (ispre_1_3_19)? MPTYPE_AC3_PRE13 : MPTYPE_AC3);
302 sent = packet.getSize();
305 else if (packet_type == PESTYPE_PRIVATE_1 &&
306 packet.getSubstream() == 0x20)
308 if (subtitles) subtitles->put(packet);
309 sent = packet.getSize();
313 sent = packet.getSize();
316 if (sent < packet.getSize()) // Stream is full.
322 void Demuxer::parsePacketDetails(PESPacket& packet)
324 if (packet.getPacketType() >= PESTYPE_AUD0 &&
325 packet.getPacketType() <= PESTYPE_AUDMAX)
327 // Extract audio PTS if it exists
330 audio_pts = packet.getPTS();
331 // We continue to seek on the audio if the video PTS that we
332 // are trying to match is ahead of the audio PTS by at most
333 // SEEK_THRESHOLD. We consider the possibility of PTS wrap.
334 if (aud_seeking && !vid_seeking &&
335 !( (video_pts_seek > audio_pts &&
336 video_pts_seek - audio_pts < SEEK_THRESHOLD)
338 (video_pts_seek < audio_pts &&
339 video_pts_seek + (1LL<<33) - audio_pts < SEEK_THRESHOLD) ))
342 Log::getInstance()->log("Demuxer", Log::DEBUG,
343 "Leaving audio sync: Audio PTS = %llu", audio_pts);
347 else if (packet.getPacketType() == PESTYPE_PRIVATE_1) // Private stream
349 //Inspired by vdr's device.c
350 int payload_begin = packet[8]+9;
351 unsigned char substream_id = packet[payload_begin];
352 unsigned char substream_type = substream_id & 0xF0;
353 unsigned char substream_index = substream_id & 0x1F;
354 pre_1_3_19_Recording: //This is for old recordings stuff and live TV
357 substream_id = PESTYPE_PRIVATE_1;
358 substream_type = 0x80;
361 switch (substream_type)
365 packet.setSubstream(substream_id);
367 case 0xA0: //LPCM //not supported yet, is there any LPCM transmissio out there?
369 case 0x80: //ac3, currently only one ac3 track per recording supported
370 packet.setSubstream(substream_type+substream_index);
372 // Extract audio PTS if it exists
375 audio_pts = packet.getPTS();
376 // We continue to seek on the audio if the video PTS that we
377 // are trying to match is ahead of the audio PTS by at most
378 // SEEK_THRESHOLD. We consider the possibility of PTS wrap.
379 if (aud_seeking && !vid_seeking &&
380 !( (video_pts_seek > audio_pts &&
381 video_pts_seek - audio_pts < SEEK_THRESHOLD)
383 (video_pts_seek < audio_pts &&
384 video_pts_seek + (1LL<<33) - audio_pts < SEEK_THRESHOLD) ))
387 Log::getInstance()->log("Demuxer", Log::DEBUG, "Leaving audio sync: Audio PTS = %llu", audio_pts);
394 ispre_1_3_19=true; //switching to compat mode and live tv mode
395 goto pre_1_3_19_Recording;
399 packet.setSubstream(0);
404 else if (packet.getPacketType() >= PESTYPE_VID0 &&
405 packet.getPacketType() <= PESTYPE_VIDMAX)
407 // Extract video PTS if it exists
408 if (packet.hasPTS()) video_pts = packet.getPTS();
410 // If there is a sequence header, extract information
411 UINT pos = packet.findSeqHeader();
415 if (pos+6 >= packet.getSize()) return;
416 horizontal_size = ((int)packet[pos] << 4) | ((int)packet[pos+1] >> 4);
417 vertical_size = (((int)packet[pos+1] & 0xf) << 8) | (int)packet[pos+2];
418 setAspectRatio((enum AspectRatio)(packet[pos+3] >> 4));
419 frame_rate = packet[pos+3] & 0x0f;
420 if (frame_rate >= 1 && frame_rate <= 8)
421 frame_rate = FrameRates[frame_rate];
424 bit_rate = ((int)packet[pos+4] << 10) |
425 ((int)packet[pos+5] << 2) |
426 ((int)packet[pos+6] >> 6);
430 video_pts_seek = video_pts;
431 Log::getInstance()->log("Demuxer", Log::DEBUG,
432 "Entering audio sync: Video PTS = %llu", video_pts);
433 Log::getInstance()->log("Demuxer", Log::DEBUG,
434 "Entering audio sync: Audio PTS = %llu", audio_pts);
441 UINT Demuxer::stripAudio(UCHAR* buf, UINT len)
443 UINT read_pos = 0, write_pos = 0;
444 UINT pattern, packet_length;
445 if (len < 4) return 0;
446 pattern = (buf[0] << 16) | (buf[1] << 8) | (buf[2]);
447 while (read_pos + 7 <= len)
449 pattern = ((pattern & 0xFFFFFF) << 8) | buf[read_pos+3];
450 if (pattern < (0x100|PESTYPE_VID0) || pattern > (0x100|PESTYPE_VIDMAX))
454 packet_length = ((buf[read_pos+4] << 8) | (buf[read_pos+5])) + 6;
455 if (read_pos + packet_length > len)
459 if (read_pos != write_pos)
460 memmove(buf+write_pos, buf+read_pos, packet_length);
461 read_pos += packet_length;
462 write_pos += packet_length;
463 pattern = (buf[read_pos] << 16) | (buf[read_pos+1] << 8)
471 bool Demuxer::scanForVideo(UCHAR* buf, UINT len)
475 if (len < 4) return false;
476 pattern = (buf[0] << 16) | (buf[1] << 8) | (buf[2]);
479 pattern = ((pattern & 0xFFFFFF) << 8) | buf[pos++];
480 if (pattern >= (0x100|PESTYPE_VID0) && pattern <= (0x100|PESTYPE_VIDMAX))
486 bool* Demuxer::getmpAudioChannels()
488 return avail_mpaudchan;
491 bool* Demuxer::getac3AudioChannels()
493 return avail_ac3audchan;
496 int Demuxer::getselAudioChannel()
498 return audio_current;
501 void Demuxer::setAudioChannel(int aud_channel)
503 audio_current = aud_channel;