2 Copyright 2005-2007 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
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 #define DEMUXER_SEQ_HEAD 0x000001B3
28 #define DEMUXER_PIC_HEAD 0x00000101
29 #define SEEK_THRESHOLD 150000 // About 1.5 seconds
32 const int Demuxer::FrameRates[9] = { 0, 23, 24, 25, 29, 30, 50, 59, 60 };
33 const ULLONG PESPacket::PTS_INVALID = (1LL << 33);
34 Demuxer* Demuxer::instance = NULL;
37 PESPacket::PESPacket()
45 void PESPacket::init(UCHAR type)
49 data[4] = data[5] = 0;
51 seq_header = 1; // Unknown seq_header status
54 void PESPacket::truncate()
59 int PESPacket::write(UCHAR *buf, int len)
61 if (length + len > 0xFFFA) return 0;
62 memcpy(data+length+6, 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()
74 if ( ( (packetType >= Demuxer::PESTYPE_AUD0 &&
75 packetType <= Demuxer::PESTYPE_AUDMAX)
77 (packetType >= Demuxer::PESTYPE_VID0 &&
78 packetType <= Demuxer::PESTYPE_VIDMAX)
79 ) && size >= 14 && data[7] & 0x80)
81 return ( (ULLONG)(data[ 9] & 0x0E) << 29) |
82 ( (ULLONG)(data[10]) << 22 ) |
83 ( (ULLONG)(data[11] & 0xFE) << 14 ) |
84 ( (ULLONG)(data[12]) << 7 ) |
85 ( (ULLONG)(data[13] & 0xFE) >> 1 );
87 else return PTS_INVALID;
90 UCHAR PESPacket::operator[] (UINT index)
98 UINT PESPacket::findPictureHeader()
100 if (size < 12) return 0;
101 UINT pattern = ( ((UINT)data[ 8] << 24) |
102 ((UINT)data[ 9] << 16) |
103 ((UINT)data[10] << 8) |
106 while (pattern != DEMUXER_PIC_HEAD)
108 if (++pos >= size) return 0;
109 pattern = (pattern << 8) | data[pos];
114 UINT PESPacket::findSeqHeader()
116 if (seq_header != 1) return seq_header;
117 if (size < 12) return 0;
118 UINT pattern = ( ((UINT)data[ 8] << 24) |
119 ((UINT)data[ 9] << 16) |
120 ((UINT)data[10] << 8) |
123 while (pattern != DEMUXER_SEQ_HEAD)
130 pattern = (pattern << 8) | data[pos];
132 seq_header = pos - 3;
139 if (instance) return;
144 vid_seeking = aud_seeking = false;
145 video_pts = audio_pts = 0;
146 ispre_1_3_19 = false;
155 Demuxer* Demuxer::getInstance()
160 int Demuxer::init(Callback* tcallback, DrainTarget* audio, DrainTarget* video, ULONG demuxMemoryV, ULONG demuxMemoryA)
164 if ( !videostream.init(video, demuxMemoryV) ||
165 !audiostream.init(audio, demuxMemoryA))
167 Log::getInstance()->log("Demuxer", Log::CRIT,
168 "Failed to initialize demuxer");
176 callback = tcallback;
180 void Demuxer::reset()
182 Log::getInstance()->log("Demuxer", Log::DEBUG, "Reset called");
184 video_current = audio_current = -1;
185 horizontal_size = vertical_size = 0;
186 aspect_ratio = (enum AspectRatio) 0;
187 frame_rate = bit_rate = 0;
188 ispre_1_3_19 = false;
190 for (int i = 0; i <= (PESTYPE_AUDMAX - PESTYPE_AUD0); i++)
192 avail_mpaudchan[i] = false;
194 for (int i = 0; i <= (PESTYPE_SUBSTREAM_AC3MAX - PESTYPE_SUBSTREAM_AC30); i++)
196 avail_ac3audchan[i] = false;
200 int Demuxer::shutdown()
202 videostream.shutdown();
203 audiostream.shutdown();
208 void Demuxer::flush()
210 Log::getInstance()->log("Demuxer", Log::DEBUG, "Flush called");
216 void Demuxer::flushAudio()
223 vid_seeking = aud_seeking = true;
224 video_pts = audio_pts = 0;
227 void Demuxer::setAudioStream(int id)
232 void Demuxer::setVideoStream(int id)
237 void Demuxer::setAspectRatio(enum AspectRatio ar)
239 if (aspect_ratio != ar)
241 Log::getInstance()->log("Demux", Log::DEBUG,
242 "Aspect ratio difference signalled");
243 if (++arcnt > 3) // avoid changing aspect ratio if glitch in signal
247 if (callback) callback->call(this);
254 bool Demuxer::writeAudio()
256 return audiostream.drain();
259 bool Demuxer::writeVideo()
261 return videostream.drain();
264 bool Demuxer::submitPacket(PESPacket& packet)
267 UCHAR packet_type = packet.getPacketType();
269 if (packet_type >= PESTYPE_VID0 && packet_type <= PESTYPE_VIDMAX)
271 if (video_current == -1) video_current = packet_type;
272 if (video_current == packet_type && !vid_seeking)
273 sent = videostream.put(packet.getData(), packet.getSize(), MPTYPE_VIDEO);
275 sent = packet.getSize();
277 else if (packet_type >= PESTYPE_AUD0 && packet_type <= PESTYPE_AUDMAX)
279 if (audio_current == -1) audio_current = packet_type;
280 avail_mpaudchan[packet_type - PESTYPE_AUD0] = true;
281 if (audio_current == packet_type && !aud_seeking)
282 sent = audiostream.put(packet.getData(), packet.getSize(), MPTYPE_MPEG_AUDIO);
284 sent = packet.getSize();
286 else if (packet_type == PESTYPE_PRIVATE_1)
288 if (packet.getSubstream() >= PESTYPE_SUBSTREAM_AC30 &&
289 packet.getSubstream() <= PESTYPE_SUBSTREAM_AC3MAX)
291 avail_ac3audchan[packet.getSubstream() - PESTYPE_SUBSTREAM_AC30] = true;
292 if (packet.getSubstream() == audio_current)
294 sent = audiostream.put(packet.getData(), packet.getSize(), (ispre_1_3_19)? MPTYPE_AC3_PRE13 : MPTYPE_AC3);
298 sent = packet.getSize();
304 sent = packet.getSize();
307 if (sent < packet.getSize()) // Stream is full.
313 void Demuxer::parsePacketDetails(PESPacket& packet)
315 if (packet.getPacketType() >= PESTYPE_AUD0 &&
316 packet.getPacketType() <= PESTYPE_AUDMAX)
318 // Extract audio PTS if it exists
321 audio_pts = packet.getPTS();
322 // We continue to seek on the audio if the video PTS that we
323 // are trying to match is ahead of the audio PTS by at most
324 // SEEK_THRESHOLD. We consider the possibility of PTS wrap.
325 if (aud_seeking && !vid_seeking &&
326 !( (video_pts_seek > audio_pts &&
327 video_pts_seek - audio_pts < SEEK_THRESHOLD)
329 (video_pts_seek < audio_pts &&
330 video_pts_seek + (1LL<<33) - audio_pts < SEEK_THRESHOLD) ))
333 Log::getInstance()->log("Demuxer", Log::DEBUG,
334 "Leaving audio sync: Audio PTS = %llu", audio_pts);
338 else if (packet.getPacketType() == PESTYPE_PRIVATE_1) // Private stream
340 //Inspired by vdr's device.c
341 int payload_begin = packet[8]+9;
342 unsigned char substream_id = packet[payload_begin];
343 unsigned char substream_type = substream_id & 0xF0;
344 unsigned char substream_index = substream_id & 0x1F;
345 pre_1_3_19_Recording: //This is for old recordings stuff
348 substream_id = PESTYPE_PRIVATE_1;
349 substream_type = 0x80;
352 switch (substream_type)
357 case 0xA0: //LPCM //not supported yet, is there any LPCM transmissio out there?
359 case 0x80: //ac3, currently only one ac3 track per recording supported
360 packet.setSubstream(substream_type+substream_index);
362 // Extract audio PTS if it exists
365 audio_pts = packet.getPTS();
366 // We continue to seek on the audio if the video PTS that we
367 // are trying to match is ahead of the audio PTS by at most
368 // SEEK_THRESHOLD. We consider the possibility of PTS wrap.
369 if (aud_seeking && !vid_seeking &&
370 !( (video_pts_seek > audio_pts &&
371 video_pts_seek - audio_pts < SEEK_THRESHOLD)
373 (video_pts_seek < audio_pts &&
374 video_pts_seek + (1LL<<33) - audio_pts < SEEK_THRESHOLD) ))
377 Log::getInstance()->log("Demuxer", Log::DEBUG, "Leaving audio sync: Audio PTS = %llu", audio_pts);
384 ispre_1_3_19=true; //switching to compat mode
385 goto pre_1_3_19_Recording;
389 packet.setSubstream(0);
394 else if (packet.getPacketType() >= PESTYPE_VID0 &&
395 packet.getPacketType() <= PESTYPE_VIDMAX)
397 // Extract video PTS if it exists
398 if (packet.hasPTS()) video_pts = packet.getPTS();
400 // If there is a sequence header, extract information
401 UINT pos = packet.findSeqHeader();
405 if (pos+6 >= packet.getSize()) return;
406 horizontal_size = ((int)packet[pos] << 4) | ((int)packet[pos+1] >> 4);
407 vertical_size = (((int)packet[pos+1] & 0xf) << 8) | (int)packet[pos+2];
408 setAspectRatio((enum AspectRatio)(packet[pos+3] >> 4));
409 frame_rate = packet[pos+3] & 0x0f;
410 if (frame_rate >= 1 && frame_rate <= 8)
411 frame_rate = FrameRates[frame_rate];
414 bit_rate = ((int)packet[pos+4] << 10) |
415 ((int)packet[pos+5] << 2) |
416 ((int)packet[pos+6] >> 6);
420 video_pts_seek = video_pts;
421 Log::getInstance()->log("Demuxer", Log::DEBUG,
422 "Entering audio sync: Video PTS = %llu", video_pts);
423 Log::getInstance()->log("Demuxer", Log::DEBUG,
424 "Entering audio sync: Audio PTS = %llu", audio_pts);
431 UINT Demuxer::stripAudio(UCHAR* buf, UINT len)
433 UINT read_pos = 0, write_pos = 0;
434 UINT pattern, packet_length;
435 if (len < 4) return 0;
436 pattern = (buf[0] << 16) | (buf[1] << 8) | (buf[2]);
437 while (read_pos + 7 <= len)
439 pattern = ((pattern & 0xFFFFFF) << 8) | buf[read_pos+3];
440 if (pattern < (0x100|PESTYPE_VID0) || pattern > (0x100|PESTYPE_VIDMAX))
444 packet_length = ((buf[read_pos+4] << 8) | (buf[read_pos+5])) + 6;
445 if (read_pos + packet_length > len)
449 if (read_pos != write_pos)
450 memmove(buf+write_pos, buf+read_pos, packet_length);
451 read_pos += packet_length;
452 write_pos += packet_length;
453 pattern = (buf[read_pos] << 16) | (buf[read_pos+1] << 8)
461 bool Demuxer::scanForVideo(UCHAR* buf, UINT len)
465 if (len < 4) return false;
466 pattern = (buf[0] << 16) | (buf[1] << 8) | (buf[2]);
469 pattern = ((pattern & 0xFFFFFF) << 8) | buf[pos++];
470 if (pattern >= (0x100|PESTYPE_VID0) && pattern <= (0x100|PESTYPE_VIDMAX))
476 bool* Demuxer::getmpAudioChannels()
478 return avail_mpaudchan;
481 bool* Demuxer::getac3AudioChannels()
483 return avail_ac3audchan;
486 int Demuxer::getselAudioChannel()
488 return audio_current;
491 void Demuxer::setAudioChannel(int aud_channel)
493 audio_current = aud_channel;