2 Copyright 2005 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
23 const int Demuxer::FrameRates[9] = { 0, 23, 24, 25, 29, 30, 50, 59, 60 };
25 Demuxer* Demuxer::instance = NULL;
42 Demuxer* Demuxer::getInstance()
47 int Demuxer::init(Callback* tcallback)
51 if ( !videostream.init(demuxMemoryV) ||
52 !audiostream.init(demuxMemoryA) ||
53 !(local_frame = (UCHAR *) malloc(0x10000)))
55 // printf("failed to initialize demuxer\n");
70 video_current = audio_current = -1;
71 horizontal_size = vertical_size = 0;
72 aspect_ratio = (enum AspectRatio) 0;
73 frame_rate = bit_rate = 0;
76 int Demuxer::shutdown()
78 videostream.shutdown();
79 audiostream.shutdown();
89 state_frametype = state_framepos = 0;
93 void Demuxer::flushAudio()
103 void Demuxer::setAudioStream(int id)
108 void Demuxer::setVideoStream(int id)
113 void Demuxer::setAspectRatio(enum AspectRatio ar)
115 if (aspect_ratio != ar)
117 Log::getInstance()->log("Demux", Log::DEBUG, "Aspect ratio difference signalled");
118 if (++arcnt > 3) // avoid changing aspect ratio if glitch in signal
122 callback->call(this);
129 int Demuxer::writeAudio(int fd)
131 return audiostream.drain(fd);
134 int Demuxer::writeVideo(int fd)
136 return videostream.drain(fd);
139 int Demuxer::scan(UCHAR *buf, int len)
141 // Temporarily, just look for the lowest audio stream and return it
147 // We are searching for a string of bytes (0,0,1).
148 byte = *(buf++); --len;
154 if (zeros < 2 || byte != 1)
159 // We have found the pattern (0,0,1).
160 // Check the next byte for the sub-frame type.
161 byte = *(buf++); --len;
162 if (byte >= 0xc0 && byte <= 0xdf) // Audio
163 if (ret == 0 || ret > byte) ret = byte;
168 int Demuxer::put(UCHAR* buf, int len)
170 int ret = 0; // return number of bytes consumed
171 int parsed = 0; // number of bytes parsed by sub-function
172 int full; // sub-function sets this to tell us to exit early
173 inbuf = buf; // Initialize buffer pointer
179 if (state_frametype == 0) // Search for frame
181 parsed = parse_find_frame(len);
183 else if ((state_frametype >= FRAMETYPE_VID0) && (state_frametype <= FRAMETYPE_VIDMAX))
185 parsed = parse_video_frame(len, &full);
187 else if ((state_frametype >= FRAMETYPE_AUD0) && (state_frametype <= FRAMETYPE_AUDMAX))
189 parsed = parse_audio_frame(len, &full);
191 else if (state_frametype == FRAMETYPE_PRIVATE_1)
193 parsed = parse_private1_frame(len, &full);
196 ret += parsed; len -= parsed;
197 if (full) // We have to exit early.
198 break; // out of while loop
200 //Log::getInstance()->log("Demuxer", Log::DEBUG, "Put %d; took %d", ret + len, ret);
204 int Demuxer::parse_find_frame(int len)
206 int ret = 0; // return number of bytes parsed
209 // In this function, state_framepos represents
210 // the number of fixed header bytes found so far.
211 if (state_framepos > 3 || state_framepos < 0)
221 switch (state_framepos)
237 state_framepos = 0; // Set initial state for the new frame
241 state_framepos = 1; // Count this as a first header byte!
243 else if ( ((byte >= FRAMETYPE_VID0) && (byte <= FRAMETYPE_VIDMAX))
244 || ((byte >= FRAMETYPE_AUD0) && (byte <= FRAMETYPE_AUDMAX))
245 || byte==FRAMETYPE_PRIVATE_1 )
247 state_frametype = byte;
251 // Not a recognised frame type. Go back to Old Kent Road.
258 int Demuxer::parse_video_frame(int len, int* full)
260 int ret = 0; // return number of bytes consumed
263 switch(state_framepos)
265 case 0: // Brand new video frame. Set initial states.
266 state_stream_fill = 0; state_vid_parsed = 0;
267 // Create a local copy of the frame header
268 local_frame[0] = local_frame[1] = 0; local_frame[2] = 1;
269 local_frame[3] = state_frametype;
270 // If no video stream has been set, use this one.
271 if (video_current == -1) video_current = state_frametype;
272 // Get MSB of frame length and copy to local frame.
273 frame_length = *inbuf << 8;
274 local_frame[4] = *inbuf;
275 ++inbuf; ++state_framepos; ++ret; --len;
276 if (len == 0) return ret;
277 // FALL THROUGH TO NEXT BYTE IN STREAM
278 case 1: // Get LSB of frame length and copy to local frame.
279 frame_length += *inbuf;
280 local_frame[5] = *inbuf;
281 ++inbuf; ++state_framepos; ++ret; --len;
282 if (len == 0) return ret;
284 // We are in the frame data
285 bytes_remaining = 2 + frame_length - state_framepos;
286 if (video_current != state_frametype)
288 // We don't want this frame. Throw it away.
289 if (len >= bytes_remaining)
291 inbuf += bytes_remaining;
292 ret += bytes_remaining;
293 state_frametype = state_framepos = 0;
298 inbuf += len; ret += len;
299 state_framepos += len;
302 } // No fall through here
304 if (bytes_remaining) // There is data yet to copy to local_frame
306 if (len > bytes_remaining) len = bytes_remaining;
307 memcpy(local_frame + state_framepos + 4, inbuf, len);
308 inbuf += len; ret += len; state_framepos += len;
309 if (len < bytes_remaining) // Not all arrived yet
311 parse_video_details(local_frame+6, frame_length);
316 // We have the whole frame in local_frame. Send it to the stream.
317 // We still support streams that might not consume all the data.
318 state_stream_fill += videostream.put(local_frame,
319 6 + frame_length - state_stream_fill);
320 if (state_stream_fill < frame_length + 6) // stream is full!
322 *full = 1; return ret;
325 state_frametype = state_framepos = 0;
329 int Demuxer::parse_audio_frame(int len, int* full)
331 int ret = 0; // return number of bytes consumed
334 switch(state_framepos)
336 case 0: // Brand new audio frame. Set initial states.
337 state_stream_fill = 0;
338 // Create a local copy of the frame header
339 local_frame[0] = local_frame[1] = 0; local_frame[2] = 1;
340 local_frame[3] = state_frametype;
341 // If no audio stream has been set, use this one.
342 if (audio_current == -1) audio_current = state_frametype;
343 // Get MSB of frame length and copy to local frame.
344 frame_length = *inbuf << 8;
345 local_frame[4] = *inbuf;
346 ++inbuf; ++state_framepos; ++ret; --len;
347 if (len == 0) return ret;
348 // FALL THROUGH TO NEXT BYTE IN STREAM
349 case 1: // Get LSB of frame length and copy to local frame.
350 frame_length += *inbuf;
351 local_frame[5] = *inbuf;
352 ++inbuf; ++state_framepos; ++ret; --len;
353 if (len == 0) return ret;
355 // We are in the frame data
356 bytes_remaining = 2 + frame_length - state_framepos;
357 if (audio_current != state_frametype)
359 // We don't want this frame. Throw it away.
360 if (len >= bytes_remaining)
362 inbuf += bytes_remaining;
363 ret += bytes_remaining;
364 state_frametype = state_framepos = 0;
369 inbuf += len; ret += len;
370 state_framepos += len;
373 } // No fall through is allowed here
375 if (bytes_remaining) // There is data yet to copy to local_frame
377 if (len > bytes_remaining) len = bytes_remaining;
378 memcpy(local_frame + state_framepos + 4, inbuf, len);
379 inbuf += len; ret += len; state_framepos += len;
380 if (len < bytes_remaining) // Not all arrived yet
384 // We have the whole frame in local_frame. Send it to the stream.
385 // We still support streams that might not consume all the data.
386 state_stream_fill += audiostream.put(local_frame,
387 6 + frame_length - state_stream_fill);
388 if (state_stream_fill < frame_length + 6) // stream is full!
390 *full = 1; return ret;
392 state_frametype = state_framepos = 0;
396 int Demuxer::parse_private1_frame(int len, int* full)
398 int ret = 0; // return number of bytes consumed
401 switch(state_framepos)
403 case 0: // Brand new frame. Set initial states.
404 // Get MSB of frame length and copy to local frame.
405 frame_length = *inbuf << 8;
406 ++inbuf; ++state_framepos; ++ret; --len;
407 if (len == 0) return ret;
408 // FALL THROUGH TO NEXT BYTE IN STREAM
409 case 1: // Get LSB of frame length and copy to local frame.
410 frame_length += *inbuf;
411 local_frame[5] = *inbuf;
412 ++inbuf; ++state_framepos; ++ret; --len;
413 if (len == 0) return ret;
415 // We are in the frame data
416 bytes_remaining = 2 + frame_length - state_framepos;
417 // Temporary - just discard the frame.
418 if (len >= bytes_remaining)
420 inbuf += bytes_remaining;
421 ret += bytes_remaining;
422 state_frametype = state_framepos = 0;
427 inbuf += len; ret += len;
428 state_framepos += len;
433 void Demuxer::parse_video_details(UCHAR* buf, int len)
435 // Extract video PTS if it exists
436 if ( buf[1] & 0x80 ) // PTS_DTS_flags indicate that PTS is present
438 video_pts = ( (ULLONG)(buf[3] & 0x0E) << 29 ) |
439 ( (ULLONG)(buf[4]) << 22 ) |
440 ( (ULLONG)(buf[5] & 0xFE) << 14 ) |
441 ( (ULLONG)(buf[6]) << 7 ) |
442 ( (ULLONG)(buf[7] & 0xFE) >> 1 );
444 // Now, scan for a GOP header and extract video information
447 while (len >= 8) // 8 is length of a GOP header
449 // We are searching for a string of bytes (0,0,1).
450 byte = *(buf++); --len;
456 if (zeros < 2 || byte != 1)
461 // We have found the pattern (0,0,1).
462 // Check the next byte for the sub-frame type.
463 byte = *(buf++); --len;
466 case 0x00: // Picture header
467 // 10 bits: temporal reference
468 // 3 bits: coding type (I/P/B)
471 if ( (buf[1] & 0x38) == 0x08 ) // I-frame
473 buf += 4; // Minimum length of picture header
476 case 0xb3: // Sequence header
477 // 12 bits: Horizontal size
478 // 12 bits: Vertical size
479 // 4 bits: Aspect ratio
480 // 4 bits: Frame rate code
481 // 18 bits: Bit rate value
484 horizontal_size = ((int)buf[0] << 4) | ((int)buf[1] >> 4);
485 vertical_size = (((int)buf[1] & 0xf) << 8) | (int)buf[2];
486 setAspectRatio((enum AspectRatio)(buf[3] >> 4));
487 frame_rate = buf[3] & 0x0f;
488 if (frame_rate >= 1 && frame_rate <= 8)
489 frame_rate = FrameRates[frame_rate];
492 bit_rate = ((int)buf[4] << 10) |
496 buf += 8; // Minimum length of sequence header
499 case 0xb8: // Group header
500 // We're not going to bother parsing anything.
502 buf += 4; // Minimum length of group header
509 int Demuxer::findVideoPTS(UCHAR* buf, int len, ULLONG* dest)
511 UINT LoPattern = 0x100 | FRAMETYPE_VID0,
512 HiPattern = 0x100 | FRAMETYPE_VIDMAX;
516 UINT pattern = *(UINT*)buf;
518 if (pattern < LoPattern || pattern > HiPattern) continue;
520 UINT framelength = ((UINT)buf[3] << 8) | buf[4];
523 if ( buf[1] & 0x80 ) // PTS_DTS_flags indicate that PTS is present
525 *dest = ( (ULLONG)(buf[3] & 0x0E) << 29 ) |
526 ( (ULLONG)(buf[4]) << 22 ) |
527 ( (ULLONG)(buf[5] & 0xFE) << 14 ) |
528 ( (ULLONG)(buf[6]) << 7 ) |
529 ( (ULLONG)(buf[7] & 0xFE) >> 1 );
533 buf += framelength; len -= framelength;