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 DEMUXER_PIC_HEAD 0x00000101
38 #define SEEK_THRESHOLD 150000 // About 1.5 seconds
40 const int Demuxer::FrameRates[9] = { 0, 23, 24, 25, 29, 30, 50, 59, 60 };
41 const ULLONG Demuxer::PESPacket::PTS_INVALID = (1LL << 33);
43 Demuxer* Demuxer::instance = NULL;
52 vid_seeking = aud_seeking = false;
53 video_pts = audio_pts = 0;
62 Demuxer* Demuxer::getInstance()
67 int Demuxer::init(Callback* tcallback, DrainTarget* audio, DrainTarget* video)
71 if ( !videostream.init(video, demuxMemoryV) ||
72 !audiostream.init(audio, demuxMemoryA))
74 Log::getInstance()->log("Demuxer", Log::CRIT,
75 "Failed to initialize demuxer");
90 video_current = audio_current = -1;
91 horizontal_size = vertical_size = 0;
92 aspect_ratio = (enum AspectRatio) 0;
93 frame_rate = bit_rate = 0;
96 int Demuxer::shutdown()
98 videostream.shutdown();
99 audiostream.shutdown();
104 void Demuxer::flush()
110 void Demuxer::flushAudio()
117 vid_seeking = aud_seeking = true;
118 video_pts = audio_pts = 0;
121 void Demuxer::setAudioStream(int id)
126 void Demuxer::setVideoStream(int id)
131 void Demuxer::setAspectRatio(enum AspectRatio ar)
133 if (aspect_ratio != ar)
135 Log::getInstance()->log("Demux", Log::DEBUG,
136 "Aspect ratio difference signalled");
137 if (++arcnt > 3) // avoid changing aspect ratio if glitch in signal
141 if (callback) callback->call(this);
148 bool Demuxer::writeAudio()
150 return audiostream.drain();
153 bool Demuxer::writeVideo()
155 return videostream.drain();
158 Demuxer::PESPacket::PESPacket()
166 void Demuxer::PESPacket::init(UCHAR type)
168 length = submitted = 0;
169 size = 6; closed = false;
171 data[4] = data[5] = 0;
177 int Demuxer::PESPacket::write(UCHAR *buf, int len)
179 if (closed) return 0;
180 if (length + len > 0xFFFA) return 0;
181 memcpy(data+length+6, buf, len);
184 data[4] = (length >> 8);
185 data[5] = (length & 0xFF);
189 int Demuxer::PESPacket::submit()
191 if (submitted >= size) return 0;
192 if (!closed) parseDetails();
195 Demuxer* dx = Demuxer::getInstance();
197 if (packetType >= PESTYPE_VID0 && packetType <= PESTYPE_VIDMAX)
199 if (dx->video_current == -1) dx->video_current = packetType;
200 if (dx->video_current == packetType && !dx->vid_seeking)
201 sent = dx->videostream.put(data+submitted, size-submitted);
203 sent = size-submitted;
205 else if (packetType >= PESTYPE_AUD0 && packetType <= PESTYPE_AUDMAX)
207 if (dx->audio_current == -1) dx->audio_current = packetType;
208 if (dx->audio_current == packetType && !dx->aud_seeking)
209 sent = dx->audiostream.put(data+submitted, size-submitted);
211 sent = size-submitted;
215 sent = size-submitted;
219 if (submitted < size) // Stream is full.
225 void Demuxer::PESPacket::parseDetails()
227 Demuxer* dx = Demuxer::getInstance();
228 if (packetType >= PESTYPE_AUD0 && packetType <= PESTYPE_AUDMAX)
230 // Extract audio PTS if it exists
231 if ( size >= 14 && (data[7] & 0x80) ) // PTS_DTS_flags indicate PTS
233 dx->audio_pts = pts = ( (ULLONG)(data[9] & 0x0E) << 29 ) |
234 ( (ULLONG)(data[10]) << 22 ) |
235 ( (ULLONG)(data[11] & 0xFE) << 14 ) |
236 ( (ULLONG)(data[12]) << 7 ) |
237 ( (ULLONG)(data[13] & 0xFE) >> 1 );
239 // We continue to seek on the audio if the video PTS that we
240 // are trying to match is ahead of the audio PTS by at most
241 // SEEK_THRESHOLD. We consider the possibility of PTS wrap.
242 if (dx->aud_seeking && !dx->vid_seeking &&
243 !( (dx->video_pts_seek > dx->audio_pts &&
244 dx->video_pts_seek - dx->audio_pts < SEEK_THRESHOLD)
246 (dx->video_pts_seek < dx->audio_pts &&
247 dx->video_pts_seek + (1LL<<33) -
248 dx->audio_pts < SEEK_THRESHOLD) ))
251 Log::getInstance()->log("Demuxer", Log::DEBUG,
252 "Leaving audio sync: Audio PTS = %llu", dx->audio_pts);
256 else if (packetType >= PESTYPE_VID0 && packetType <= PESTYPE_VIDMAX)
258 // Extract video PTS if it exists
259 if ( size >= 14 && (data[7] & 0x80) ) // PTS_DTS_flags indicate PTS
261 dx->video_pts = pts = ( (ULLONG)(data[9] & 0x0E) << 29 ) |
262 ( (ULLONG)(data[10]) << 22 ) |
263 ( (ULLONG)(data[11] & 0xFE) << 14 ) |
264 ( (ULLONG)(data[12]) << 7 ) |
265 ( (ULLONG)(data[13] & 0xFE) >> 1 );
268 // Now, scan for a sequence header and extract information
269 UINT pos = 9; // Start searching from byte 9
270 while (pos < size - 5)
272 UINT pattern = *(UINT*)(data+pos);
273 if (pattern == DEMUXER_SEQ_HEAD)
277 if (pos+6 >= size) return;
278 dx->horizontal_size = ((int)data[pos] << 4) | ((int)data[pos+1] >> 4);
279 dx->vertical_size = (((int)data[pos+1] & 0xf) << 8) | (int)data[pos+2];
280 dx->setAspectRatio((enum AspectRatio)(data[pos+3] >> 4));
281 dx->frame_rate = data[pos+3] & 0x0f;
282 if (dx->frame_rate >= 1 && dx->frame_rate <= 8)
283 dx->frame_rate = FrameRates[dx->frame_rate];
286 dx->bit_rate = ((int)data[pos+4] << 10) |
287 ((int)data[pos+5] << 2) |
288 ((int)data[pos+6] >> 6);
292 dx->video_pts_seek = dx->video_pts;
293 Log::getInstance()->log("Demuxer", Log::DEBUG,
294 "Entering audio sync: Video PTS = %llu", dx->video_pts);
295 Log::getInstance()->log("Demuxer", Log::DEBUG,
296 "Entering audio sync: Audio PTS = %llu", dx->audio_pts);
305 UINT Demuxer::PESPacket::findPictureHeader()
307 if (size < 12) return 0;
308 UINT pattern = *(UINT*)(data+8);
310 while (pattern != DEMUXER_PIC_HEAD)
312 if (++pos >= size) return 0;
313 pattern = (pattern << 8) | data[pos];
318 UINT Demuxer::stripAudio(UCHAR* buf, UINT len)
320 UINT read_pos = 0, write_pos = 0;
321 UINT pattern, packet_length;
322 if (len < 4) return 0;
323 pattern = (buf[0] << 16) | (buf[1] << 8) | (buf[2]);
324 while (read_pos + 7 <= len)
326 pattern = ((pattern & 0xFFFFFF) << 8) | buf[read_pos+3];
327 if (pattern < (0x100|PESTYPE_VID0) || pattern > (0x100|PESTYPE_VIDMAX))
331 packet_length = ((buf[read_pos+4] << 8) | (buf[read_pos+5])) + 6;
332 if (read_pos + packet_length > len)
336 if (read_pos != write_pos)
337 memmove(buf+write_pos, buf+read_pos, packet_length);
338 read_pos += packet_length;
339 write_pos += packet_length;
340 pattern = (buf[read_pos] << 16) | (buf[read_pos+1] << 8)