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"
30 #define DEMUXER_SEQ_HEAD 0x000001B3
31 #define DEMUXER_PIC_HEAD 0x00000101
32 #define SEEK_THRESHOLD 150000 // About 1.5 seconds
35 const int Demuxer::FrameRates[9] = { 0, 23, 24, 25, 29, 30, 50, 59, 60 };
36 Demuxer* Demuxer::instance = NULL;
38 static const int PESPacket_initial_size = 2000;
41 PESPacket::PESPacket()
43 data_size = PESPacket_initial_size;
44 data = (UCHAR*)malloc(data_size);
51 PESPacket::PESPacket(const PESPacket& packet)
56 PESPacket& PESPacket::operator=(const PESPacket& packet)
66 PESPacket::~PESPacket()
71 void PESPacket::copyFrom(const PESPacket& packet)
73 length = packet.length;
75 packetType = packet.packetType;
76 substream = packet.substream;
77 seq_header = packet.seq_header;
79 data = (UCHAR*)malloc(data_size);
80 memcpy(data, packet.data, data_size);
83 void PESPacket::init(UCHAR type, UCHAR sub)
87 data[4] = data[5] = 0;
91 seq_header = 1; // Unknown seq_header status
94 void PESPacket::truncate()
96 init(packetType,substream);
99 int PESPacket::write(const UCHAR *buf, int len)
101 if (size + len > 0x10000) return 0;
102 if (size + len > data_size)
104 UINT new_data_size = max(data_size + data_size / 2, data_size + len);
105 if (new_data_size > 0x10000) new_data_size = 0x10000;
106 data_size = new_data_size;
107 data = (UCHAR*)realloc(data, data_size);
109 memcpy(data + size, buf, len);
112 data[4] = (length >> 8);
113 data[5] = (length & 0xFF);
114 // We have added data - reset seq_header indicator if necessary
115 if (seq_header == 0) seq_header = 1; // Reset to 'unknown'
119 ULLONG PESPacket::getPTS() const
121 if ( ( (packetType >= Demuxer::PESTYPE_AUD0 &&
122 packetType <= Demuxer::PESTYPE_AUDMAX)
124 (packetType >= Demuxer::PESTYPE_VID0 &&
125 packetType <= Demuxer::PESTYPE_VIDMAX)
127 packetType == Demuxer::PESTYPE_PRIVATE_1
129 && size >= 14 && data[7] & 0x80)
131 return ( (ULLONG)(data[ 9] & 0x0E) << 29) |
132 ( (ULLONG)(data[10]) << 22 ) |
133 ( (ULLONG)(data[11] & 0xFE) << 14 ) |
134 ( (ULLONG)(data[12]) << 7 ) |
135 ( (ULLONG)(data[13] & 0xFE) >> 1 );
137 else return PTS_INVALID;
140 UCHAR PESPacket::operator[] (UINT index) const
148 UINT PESPacket::findPictureHeader() const
150 if (size < 12) return 0;
151 UINT pattern = ( ((UINT)data[ 8] << 24) |
152 ((UINT)data[ 9] << 16) |
153 ((UINT)data[10] << 8) |
156 while (pattern != DEMUXER_PIC_HEAD)
158 if (++pos >= size) return 0;
159 pattern = (pattern << 8) | data[pos];
164 UINT PESPacket::findSeqHeader() const
166 if (seq_header != 1) return seq_header;
167 if (size < 12) return 0;
168 UINT pattern = ( ((UINT)data[ 8] << 24) |
169 ((UINT)data[ 9] << 16) |
170 ((UINT)data[10] << 8) |
173 while (pattern != DEMUXER_SEQ_HEAD)
180 pattern = (pattern << 8) | data[pos];
182 seq_header = pos - 3;
189 if (instance) return;
194 vid_seeking = aud_seeking = false;
195 video_pts = audio_pts = 0;
196 ispre_1_3_19 = false;
205 Demuxer* Demuxer::getInstance()
210 int Demuxer::init(Callback* tcallback, DrainTarget* audio, DrainTarget* video, ULONG demuxMemoryV, ULONG demuxMemoryA, DVBSubtitles* tsubtitles)
214 if ( !videostream.init(video, demuxMemoryV) ||
215 !audiostream.init(audio, demuxMemoryA))
217 Log::getInstance()->log("Demuxer", Log::CRIT,
218 "Failed to initialize demuxer");
226 subtitles = tsubtitles;
227 callback = tcallback;
231 void Demuxer::reset()
233 Log::getInstance()->log("Demuxer", Log::DEBUG, "Reset called");
235 video_current = audio_current = -1;
236 horizontal_size = vertical_size = 0;
237 aspect_ratio = (enum AspectRatio) 0;
238 frame_rate = bit_rate = 0;
239 ispre_1_3_19 = false;
241 for (int i = 0; i <= (PESTYPE_AUDMAX - PESTYPE_AUD0); i++)
243 avail_mpaudchan[i] = false;
245 for (int i = 0; i <= (PESTYPE_SUBSTREAM_AC3MAX - PESTYPE_SUBSTREAM_AC30); i++)
247 avail_ac3audchan[i] = false;
251 int Demuxer::shutdown()
253 videostream.shutdown();
254 audiostream.shutdown();
259 void Demuxer::flush()
261 Log::getInstance()->log("Demuxer", Log::DEBUG, "Flush called");
267 void Demuxer::flushAudio()
274 vid_seeking = aud_seeking = true;
275 video_pts = audio_pts = 0;
278 void Demuxer::setAudioStream(int id)
283 void Demuxer::setVideoStream(int id)
288 void Demuxer::setAspectRatio(enum AspectRatio ar)
290 if (aspect_ratio != ar)
292 Log::getInstance()->log("Demux", Log::DEBUG,
293 "Aspect ratio difference signalled");
294 if (++arcnt > 3) // avoid changing aspect ratio if glitch in signal
298 if (callback) callback->call(this);
305 bool Demuxer::writeAudio()
307 return audiostream.drain();
310 bool Demuxer::writeVideo()
312 return videostream.drain();
315 bool Demuxer::submitPacket(PESPacket& packet)
318 UCHAR packet_type = packet.getPacketType();
319 const UCHAR* packetdata = packet.getData();
321 if (packet_type >= PESTYPE_VID0 && packet_type <= PESTYPE_VIDMAX)
323 if (video_current == -1) video_current = packet_type;
324 if (video_current == packet_type && !vid_seeking)
325 sent = videostream.put(&packetdata[0], packet.getSize(), MPTYPE_VIDEO);
327 sent = packet.getSize();
329 else if (packet_type >= PESTYPE_AUD0 && packet_type <= PESTYPE_AUDMAX)
331 if (audio_current == -1) audio_current = packet_type;
332 avail_mpaudchan[packet_type - PESTYPE_AUD0] = true;
333 if (audio_current == packet_type && !aud_seeking)
334 sent = audiostream.put(&packetdata[0], packet.getSize(), MPTYPE_MPEG_AUDIO);
336 sent = packet.getSize();
338 else if (packet_type == PESTYPE_PRIVATE_1 &&
339 packet.getSubstream() >= PESTYPE_SUBSTREAM_AC30 &&
340 packet.getSubstream() <= PESTYPE_SUBSTREAM_AC3MAX)
342 avail_ac3audchan[packet.getSubstream() - PESTYPE_SUBSTREAM_AC30] = true;
343 if (packet.getSubstream() == audio_current)
345 sent = audiostream.put(&packetdata[0], packet.getSize(), (ispre_1_3_19)? MPTYPE_AC3_PRE13 : MPTYPE_AC3);
349 sent = packet.getSize();
352 else if (packet_type == PESTYPE_PRIVATE_1 &&
353 packet.getSubstream() == 0x20)
355 if (subtitles) subtitles->put(packet);
356 sent = packet.getSize();
360 sent = packet.getSize();
363 if (sent < packet.getSize()) // Stream is full.
369 void Demuxer::parsePacketDetails(PESPacket& packet)
371 if (packet.getPacketType() >= PESTYPE_AUD0 &&
372 packet.getPacketType() <= PESTYPE_AUDMAX)
374 // Extract audio PTS if it exists
377 audio_pts = packet.getPTS();
378 // We continue to seek on the audio if the video PTS that we
379 // are trying to match is ahead of the audio PTS by at most
380 // SEEK_THRESHOLD. We consider the possibility of PTS wrap.
381 if (aud_seeking && !vid_seeking &&
382 !( (video_pts_seek > audio_pts &&
383 video_pts_seek - audio_pts < SEEK_THRESHOLD)
385 (video_pts_seek < audio_pts &&
386 video_pts_seek + (1LL<<33) - audio_pts < SEEK_THRESHOLD) ))
389 Log::getInstance()->log("Demuxer", Log::DEBUG,
390 "Leaving audio sync: Audio PTS = %llu", audio_pts);
394 else if (packet.getPacketType() == PESTYPE_PRIVATE_1) // Private stream
396 //Inspired by vdr's device.c
397 int payload_begin = packet[8]+9;
398 unsigned char substream_id = packet[payload_begin];
399 unsigned char substream_type = substream_id & 0xF0;
400 unsigned char substream_index = substream_id & 0x1F;
401 pre_1_3_19_Recording: //This is for old recordings stuff and live TV
404 substream_id = PESTYPE_PRIVATE_1;
405 substream_type = 0x80;
408 switch (substream_type)
412 packet.setSubstream(substream_id);
414 case 0xA0: //LPCM //not supported yet, is there any LPCM transmissio out there?
416 case 0x80: //ac3, currently only one ac3 track per recording supported
417 packet.setSubstream(substream_type+substream_index);
419 // Extract audio PTS if it exists
422 audio_pts = packet.getPTS();
423 // We continue to seek on the audio if the video PTS that we
424 // are trying to match is ahead of the audio PTS by at most
425 // SEEK_THRESHOLD. We consider the possibility of PTS wrap.
426 if (aud_seeking && !vid_seeking &&
427 !( (video_pts_seek > audio_pts &&
428 video_pts_seek - audio_pts < SEEK_THRESHOLD)
430 (video_pts_seek < audio_pts &&
431 video_pts_seek + (1LL<<33) - audio_pts < SEEK_THRESHOLD) ))
434 Log::getInstance()->log("Demuxer", Log::DEBUG, "Leaving audio sync: Audio PTS = %llu", audio_pts);
441 ispre_1_3_19=true; //switching to compat mode and live tv mode
442 goto pre_1_3_19_Recording;
446 packet.setSubstream(0);
451 else if (packet.getPacketType() >= PESTYPE_VID0 &&
452 packet.getPacketType() <= PESTYPE_VIDMAX)
454 // Extract video PTS if it exists
455 if (packet.hasPTS()) video_pts = packet.getPTS();
457 // If there is a sequence header, extract information
458 UINT pos = packet.findSeqHeader();
462 if (pos+6 >= packet.getSize()) return;
463 horizontal_size = ((int)packet[pos] << 4) | ((int)packet[pos+1] >> 4);
464 vertical_size = (((int)packet[pos+1] & 0xf) << 8) | (int)packet[pos+2];
465 setAspectRatio((enum AspectRatio)(packet[pos+3] >> 4));
466 frame_rate = packet[pos+3] & 0x0f;
467 if (frame_rate >= 1 && frame_rate <= 8)
468 frame_rate = FrameRates[frame_rate];
471 bit_rate = ((int)packet[pos+4] << 10) |
472 ((int)packet[pos+5] << 2) |
473 ((int)packet[pos+6] >> 6);
477 video_pts_seek = video_pts;
478 Log::getInstance()->log("Demuxer", Log::DEBUG,
479 "Entering audio sync: Video PTS = %llu", video_pts);
480 Log::getInstance()->log("Demuxer", Log::DEBUG,
481 "Entering audio sync: Audio PTS = %llu", audio_pts);
488 UINT Demuxer::stripAudio(UCHAR* buf, UINT len)
490 UINT read_pos = 0, write_pos = 0;
491 UINT pattern, packet_length;
492 if (len < 4) return 0;
493 pattern = (buf[0] << 16) | (buf[1] << 8) | (buf[2]);
494 while (read_pos + 7 <= len)
496 pattern = ((pattern & 0xFFFFFF) << 8) | buf[read_pos+3];
497 if (pattern < (0x100|PESTYPE_VID0) || pattern > (0x100|PESTYPE_VIDMAX))
501 packet_length = ((buf[read_pos+4] << 8) | (buf[read_pos+5])) + 6;
502 if (read_pos + packet_length > len)
506 if (read_pos != write_pos)
507 memmove(buf+write_pos, buf+read_pos, packet_length);
508 read_pos += packet_length;
509 write_pos += packet_length;
510 pattern = (buf[read_pos] << 16) | (buf[read_pos+1] << 8)
518 bool Demuxer::scanForVideo(UCHAR* buf, UINT len)
522 if (len < 4) return false;
523 pattern = (buf[0] << 16) | (buf[1] << 8) | (buf[2]);
526 pattern = ((pattern & 0xFFFFFF) << 8) | buf[pos++];
527 if (pattern >= (0x100|PESTYPE_VID0) && pattern <= (0x100|PESTYPE_VIDMAX))
533 bool* Demuxer::getmpAudioChannels()
535 return avail_mpaudchan;
538 bool* Demuxer::getac3AudioChannels()
540 return avail_ac3audchan;
543 int Demuxer::getselAudioChannel()
545 return audio_current;
548 void Demuxer::setAudioChannel(int aud_channel)
550 audio_current = aud_channel;