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
38 const int Demuxer::FrameRates[9] = { 0, 23, 24, 25, 29, 30, 50, 59, 60 };
40 Demuxer* Demuxer::instance = NULL;
49 vid_seeking = aud_seeking = false;
50 video_pts = audio_pts = 0;
59 Demuxer* Demuxer::getInstance()
64 int Demuxer::init(Callback* tcallback)
68 if ( !videostream.init(demuxMemoryV) ||
69 !audiostream.init(demuxMemoryA))
71 Log::getInstance()->log("Demuxer", Log::CRIT, "Failed to initialize demuxer");
76 videostream.setDrainTarget(Video::getInstance());
77 audiostream.setDrainTarget(Audio::getInstance());
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, "Aspect ratio difference signalled");
136 if (++arcnt > 3) // avoid changing aspect ratio if glitch in signal
140 callback->call(this);
147 int Demuxer::writeAudio(int fd)
149 return audiostream.drain(fd);
152 int Demuxer::writeVideo(int fd)
154 return videostream.drain(fd);
157 int Demuxer::writeAudio(DrainTarget* dt)
159 return audiostream.drain(dt);
162 int Demuxer::writeVideo(DrainTarget* dt)
164 return videostream.drain(dt);
168 Demuxer::PESPacket::PESPacket()
176 void Demuxer::PESPacket::init(UCHAR type)
178 length = submitted = 0;
179 size = 6; closed = false;
181 data[4] = data[5] = 0;
185 int Demuxer::PESPacket::write(UCHAR *buf, int len)
187 if (closed) return 0;
188 if (length + len > 0xFFFA) return 0;
189 memcpy(data+length+6, buf, len);
192 data[4] = (length >> 8);
193 data[5] = (length & 0xFF);
197 int Demuxer::PESPacket::submit()
199 int Demuxer::PESPacket::submit(ULLONG cur_pos)
202 if (submitted >= size) return 0;
203 if (!closed) parseDetails();
206 Demuxer* dx = Demuxer::getInstance();
208 if (packetType >= PESTYPE_VID0 && packetType <= PESTYPE_VIDMAX)
210 if (dx->video_current == -1) dx->video_current = packetType;
211 if (dx->video_current == packetType && !dx->vid_seeking)
213 sent = dx->videostream.put(data+submitted, size-submitted);
215 sent = dx->videostream.put(data+submitted, size-submitted,cur_pos);
218 sent = size-submitted;
220 else if (packetType >= PESTYPE_AUD0 && packetType <= PESTYPE_AUDMAX)
222 if (dx->audio_current == -1) dx->audio_current = packetType;
223 if (dx->audio_current == packetType && !dx->aud_seeking)
225 sent = dx->audiostream.put(data+submitted, size-submitted);
227 sent = dx->audiostream.put(data+submitted, size-submitted,cur_pos);
230 sent = size-submitted;
234 sent = size-submitted;
238 if (submitted < size) // Stream is full.
244 void Demuxer::PESPacket::parseDetails()
246 Demuxer* dx = Demuxer::getInstance();
247 if (packetType >= PESTYPE_AUD0 && packetType <= PESTYPE_AUDMAX)
249 // Extract audio PTS if it exists
250 if ( data[7] & 0x80 ) // PTS_DTS_flags indicate that PTS is present
252 dx->audio_pts = ( (ULLONG)(data[9] & 0x0E) << 29 ) |
253 ( (ULLONG)(data[10]) << 22 ) |
254 ( (ULLONG)(data[11] & 0xFE) << 14 ) |
255 ( (ULLONG)(data[12]) << 7 ) |
256 ( (ULLONG)(data[13] & 0xFE) >> 1 );
257 if (dx->aud_seeking && !dx->vid_seeking &&
258 dx->audio_pts >= dx->video_pts_seek)
261 Log::getInstance()->log("Demuxer", Log::DEBUG,
262 "Leaving audio sync: Audio PTS = %llu", dx->audio_pts);
266 else if (packetType >= PESTYPE_VID0 && packetType <= PESTYPE_VIDMAX)
268 // Extract video PTS if it exists
269 if ( data[7] & 0x80 ) // PTS_DTS_flags indicate that PTS is present
271 dx->video_pts = ( (ULLONG)(data[9] & 0x0E) << 29 ) |
272 ( (ULLONG)(data[10]) << 22 ) |
273 ( (ULLONG)(data[11] & 0xFE) << 14 ) |
274 ( (ULLONG)(data[12]) << 7 ) |
275 ( (ULLONG)(data[13] & 0xFE) >> 1 );
278 // Now, scan for a sequence header and extract information
279 UINT pos = 9; // Start searching from byte 9
280 while (pos < size - 5)
282 UINT pattern = *(UINT*)(data+pos);
283 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);