2 Copyright 2005-2006 Mark Calderbank
4 This file is part of VOMP.
6 VOMP is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 VOMP is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with VOMP; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 #define __LITTLE_ENDIAN 1234
26 #define __BIG_ENDIAN 4321
27 #define __BYTE_ORDER __LITTLE_ENDIAN
30 #if __BYTE_ORDER == __BIG_ENDIAN
31 #define DEMUXER_SEQ_HEAD 0x000001B3
33 #define DEMUXER_SEQ_HEAD 0xB3010000
36 #define SEEK_THRESHOLD 150000 // About 1.5 seconds
38 const int Demuxer::FrameRates[9] = { 0, 23, 24, 25, 29, 30, 50, 59, 60 };
39 const ULLONG Demuxer::PESPacket::PTS_INVALID = (1LL << 33);
41 Demuxer* Demuxer::instance = NULL;
50 vid_seeking = aud_seeking = false;
51 video_pts = audio_pts = 0;
60 Demuxer* Demuxer::getInstance()
65 int Demuxer::init(Callback* tcallback, DrainTarget* audio, DrainTarget* video)
69 if ( !videostream.init(video, demuxMemoryV) ||
70 !audiostream.init(audio, demuxMemoryA))
72 Log::getInstance()->log("Demuxer", Log::CRIT,
73 "Failed to initialize demuxer");
88 video_current = audio_current = -1;
89 horizontal_size = vertical_size = 0;
90 aspect_ratio = (enum AspectRatio) 0;
91 frame_rate = bit_rate = 0;
94 int Demuxer::shutdown()
96 videostream.shutdown();
97 audiostream.shutdown();
102 void Demuxer::flush()
108 void Demuxer::flushAudio()
115 vid_seeking = aud_seeking = true;
116 video_pts = audio_pts = 0;
119 void Demuxer::setAudioStream(int id)
124 void Demuxer::setVideoStream(int id)
129 void Demuxer::setAspectRatio(enum AspectRatio ar)
131 if (aspect_ratio != ar)
133 Log::getInstance()->log("Demux", Log::DEBUG,
134 "Aspect ratio difference signalled");
135 if (++arcnt > 3) // avoid changing aspect ratio if glitch in signal
139 callback->call(this);
146 int Demuxer::writeAudio()
148 return audiostream.drain();
151 int Demuxer::writeVideo()
153 return videostream.drain();
156 Demuxer::PESPacket::PESPacket()
164 void Demuxer::PESPacket::init(UCHAR type)
166 length = submitted = 0;
167 size = 6; closed = false;
169 data[4] = data[5] = 0;
175 int Demuxer::PESPacket::write(UCHAR *buf, int len)
177 if (closed) return 0;
178 if (length + len > 0xFFFA) return 0;
179 memcpy(data+length+6, buf, len);
182 data[4] = (length >> 8);
183 data[5] = (length & 0xFF);
187 int Demuxer::PESPacket::submit()
189 int Demuxer::PESPacket::submit(ULLONG cur_pos)
192 if (submitted >= size) return 0;
193 if (!closed) parseDetails();
196 Demuxer* dx = Demuxer::getInstance();
198 if (packetType >= PESTYPE_VID0 && packetType <= PESTYPE_VIDMAX)
200 if (dx->video_current == -1) dx->video_current = packetType;
201 if (dx->video_current == packetType && !dx->vid_seeking)
203 sent = dx->videostream.put(data+submitted, size-submitted);
205 sent = dx->videostream.put(data+submitted, size-submitted,cur_pos);
208 sent = size-submitted;
210 else if (packetType >= PESTYPE_AUD0 && packetType <= PESTYPE_AUDMAX)
212 if (dx->audio_current == -1) dx->audio_current = packetType;
213 if (dx->audio_current == packetType && !dx->aud_seeking)
215 sent = dx->audiostream.put(data+submitted, size-submitted);
217 sent = dx->audiostream.put(data+submitted, size-submitted,cur_pos);
220 sent = size-submitted;
224 sent = size-submitted;
228 if (submitted < size) // Stream is full.
234 void Demuxer::PESPacket::parseDetails()
236 Demuxer* dx = Demuxer::getInstance();
237 if (packetType >= PESTYPE_AUD0 && packetType <= PESTYPE_AUDMAX)
239 // Extract audio PTS if it exists
240 if ( data[7] & 0x80 ) // PTS_DTS_flags indicate that PTS is present
242 dx->audio_pts = pts = ( (ULLONG)(data[9] & 0x0E) << 29 ) |
243 ( (ULLONG)(data[10]) << 22 ) |
244 ( (ULLONG)(data[11] & 0xFE) << 14 ) |
245 ( (ULLONG)(data[12]) << 7 ) |
246 ( (ULLONG)(data[13] & 0xFE) >> 1 );
248 // We continue to seek on the audio if the video PTS that we
249 // are trying to match is ahead of the audio PTS by at most
250 // SEEK_THRESHOLD. We consider the possibility of PTS wrap.
251 if (dx->aud_seeking && !dx->vid_seeking &&
252 !( (dx->video_pts_seek > dx->audio_pts &&
253 dx->video_pts_seek - dx->audio_pts < SEEK_THRESHOLD)
255 (dx->video_pts_seek < dx->audio_pts &&
256 dx->video_pts_seek + (1LL<<33) -
257 dx->audio_pts < SEEK_THRESHOLD) ))
260 Log::getInstance()->log("Demuxer", Log::DEBUG,
261 "Leaving audio sync: Audio PTS = %llu", dx->audio_pts);
265 else if (packetType >= PESTYPE_VID0 && packetType <= PESTYPE_VIDMAX)
267 // Extract video PTS if it exists
268 if ( data[7] & 0x80 ) // PTS_DTS_flags indicate that PTS is present
270 dx->video_pts = pts = ( (ULLONG)(data[9] & 0x0E) << 29 ) |
271 ( (ULLONG)(data[10]) << 22 ) |
272 ( (ULLONG)(data[11] & 0xFE) << 14 ) |
273 ( (ULLONG)(data[12]) << 7 ) |
274 ( (ULLONG)(data[13] & 0xFE) >> 1 );
277 // Now, scan for a sequence header and extract information
278 UINT pos = 9; // Start searching from byte 9
279 while (pos < size - 5)
281 UINT pattern = *(UINT*)(data+pos);
282 if (pattern == DEMUXER_SEQ_HEAD)
286 if (pos+6 >= size) return;
287 dx->horizontal_size = ((int)data[pos] << 4) | ((int)data[pos+1] >> 4);
288 dx->vertical_size = (((int)data[pos+1] & 0xf) << 8) | (int)data[pos+2];
289 dx->setAspectRatio((enum AspectRatio)(data[pos+3] >> 4));
290 dx->frame_rate = data[pos+3] & 0x0f;
291 if (dx->frame_rate >= 1 && dx->frame_rate <= 8)
292 dx->frame_rate = FrameRates[dx->frame_rate];
295 dx->bit_rate = ((int)data[pos+4] << 10) |
296 ((int)data[pos+5] << 2) |
297 ((int)data[pos+6] >> 6);
301 dx->video_pts_seek = dx->video_pts;
302 Log::getInstance()->log("Demuxer", Log::DEBUG,
303 "Entering audio sync: Video PTS = %llu", dx->video_pts);
304 Log::getInstance()->log("Demuxer", Log::DEBUG,
305 "Entering audio sync: Audio PTS = %llu", dx->audio_pts);