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;
41 Demuxer* Demuxer::getInstance()
46 int Demuxer::init(Callback* tcallback)
50 if ( !videostream.init(demuxMemoryV) ||
51 !audiostream.init(demuxMemoryA) ||
52 !(local_frame = (UCHAR *) malloc(0x10000)))
54 // printf("failed to initialize demuxer\n");
69 video_current = audio_current = -1;
70 horizontal_size = vertical_size = 0;
71 aspect_ratio = (enum AspectRatio) 0;
72 frame_rate = bit_rate = 0;
75 int Demuxer::shutdown()
77 videostream.shutdown();
78 audiostream.shutdown();
88 state_frametype = state_framepos = 0;
92 void Demuxer::flushAudio()
102 void Demuxer::setAudioStream(int id)
107 void Demuxer::setVideoStream(int id)
112 void Demuxer::setAspectRatio(enum AspectRatio ar)
114 if (aspect_ratio != ar)
117 callback->call(this);
121 int Demuxer::writeAudio(int fd)
123 return audiostream.drain(fd);
126 int Demuxer::writeVideo(int fd)
128 return videostream.drain(fd);
131 int Demuxer::scan(UCHAR *buf, int len)
133 // Temporarily, just look for the lowest audio stream and return it
139 // We are searching for a string of bytes (0,0,1).
140 byte = *(buf++); --len;
146 if (zeros < 2 || byte != 1)
151 // We have found the pattern (0,0,1).
152 // Check the next byte for the sub-frame type.
153 byte = *(buf++); --len;
154 if (byte >= 0xc0 && byte <= 0xdf) // Audio
155 if (ret == 0 || ret > byte) ret = byte;
160 int Demuxer::put(UCHAR* buf, int len)
162 int ret = 0; // return number of bytes consumed
163 int parsed = 0; // number of bytes parsed by sub-function
164 int full; // sub-function sets this to tell us to exit early
165 inbuf = buf; // Initialize buffer pointer
170 switch (state_frametype)
172 case 0: // Search for frame
173 parsed = parse_find_frame(len);
175 case FRAMETYPE_VID0 ... FRAMETYPE_VIDMAX:
176 parsed = parse_video_frame(len, &full);
178 case FRAMETYPE_AUD0 ... FRAMETYPE_AUDMAX:
179 parsed = parse_audio_frame(len, &full);
181 case FRAMETYPE_PRIVATE_1:
182 parsed = parse_private1_frame(len, &full);
185 ret += parsed; len -= parsed;
186 if (full) // We have to exit early.
187 break; // out of while loop
189 //Log::getInstance()->log("Demuxer", Log::DEBUG, "Put %d; took %d", ret + len, ret);
193 int Demuxer::parse_find_frame(int len)
195 int ret = 0; // return number of bytes parsed
198 // In this function, state_framepos represents
199 // the number of fixed header bytes found so far.
200 if (state_framepos > 3 || state_framepos < 0)
210 switch (state_framepos)
226 state_framepos = 0; // Set initial state for the new frame
230 state_framepos = 1; // Count this as a first header byte!
232 case FRAMETYPE_VID0 ... FRAMETYPE_VIDMAX:
233 case FRAMETYPE_AUD0 ... FRAMETYPE_AUDMAX:
234 case FRAMETYPE_PRIVATE_1:
235 state_frametype = byte;
238 // Not a recognised frame type. Go back to Old Kent Road.
245 int Demuxer::parse_video_frame(int len, int* full)
247 int ret = 0; // return number of bytes consumed
250 switch(state_framepos)
252 case 0: // Brand new video frame. Set initial states.
253 state_stream_fill = 0; state_vid_parsed = 0;
254 // Create a local copy of the frame header
255 local_frame[0] = local_frame[1] = 0; local_frame[2] = 1;
256 local_frame[3] = state_frametype;
257 // If no video stream has been set, use this one.
258 if (video_current == -1) video_current = state_frametype;
259 // Get MSB of frame length and copy to local frame.
260 frame_length = *inbuf << 8;
261 local_frame[4] = *inbuf;
262 ++inbuf; ++state_framepos; ++ret; --len;
263 if (len == 0) return ret;
264 // FALL THROUGH TO NEXT BYTE IN STREAM
265 case 1: // Get LSB of frame length and copy to local frame.
266 frame_length += *inbuf;
267 local_frame[5] = *inbuf;
268 ++inbuf; ++state_framepos; ++ret; --len;
269 if (len == 0) return ret;
271 // We are in the frame data
272 bytes_remaining = 2 + frame_length - state_framepos;
273 if (video_current != state_frametype)
275 // We don't want this frame. Throw it away.
276 if (len >= bytes_remaining)
278 inbuf += bytes_remaining;
279 ret += bytes_remaining;
280 state_frametype = state_framepos = 0;
285 inbuf += len; ret += len;
286 state_framepos += len;
289 } // No fall through here
291 if (bytes_remaining) // There is data yet to copy to local_frame
293 if (len > bytes_remaining) len = bytes_remaining;
294 memcpy(local_frame + state_framepos + 4, inbuf, len);
295 inbuf += len; ret += len; state_framepos += len;
296 if (len < bytes_remaining) // Not all arrived yet
298 parse_video_details(local_frame+6, frame_length);
303 // We have the whole frame in local_frame. Send it to the stream.
304 // We still support streams that might not consume all the data.
305 state_stream_fill += videostream.put(local_frame,
306 6 + frame_length - state_stream_fill);
307 if (state_stream_fill < frame_length + 6) // stream is full!
309 *full = 1; return ret;
312 state_frametype = state_framepos = 0;
316 int Demuxer::parse_audio_frame(int len, int* full)
318 int ret = 0; // return number of bytes consumed
321 switch(state_framepos)
323 case 0: // Brand new audio frame. Set initial states.
324 state_stream_fill = 0;
325 // Create a local copy of the frame header
326 local_frame[0] = local_frame[1] = 0; local_frame[2] = 1;
327 local_frame[3] = state_frametype;
328 // If no audio stream has been set, use this one.
329 if (audio_current == -1) audio_current = state_frametype;
330 // Get MSB of frame length and copy to local frame.
331 frame_length = *inbuf << 8;
332 local_frame[4] = *inbuf;
333 ++inbuf; ++state_framepos; ++ret; --len;
334 if (len == 0) return ret;
335 // FALL THROUGH TO NEXT BYTE IN STREAM
336 case 1: // Get LSB of frame length and copy to local frame.
337 frame_length += *inbuf;
338 local_frame[5] = *inbuf;
339 ++inbuf; ++state_framepos; ++ret; --len;
340 if (len == 0) return ret;
342 // We are in the frame data
343 bytes_remaining = 2 + frame_length - state_framepos;
344 if (audio_current != state_frametype)
346 // We don't want this frame. Throw it away.
347 if (len >= bytes_remaining)
349 inbuf += bytes_remaining;
350 ret += bytes_remaining;
351 state_frametype = state_framepos = 0;
356 inbuf += len; ret += len;
357 state_framepos += len;
360 } // No fall through is allowed here
362 if (bytes_remaining) // There is data yet to copy to local_frame
364 if (len > bytes_remaining) len = bytes_remaining;
365 memcpy(local_frame + state_framepos + 4, inbuf, len);
366 inbuf += len; ret += len; state_framepos += len;
367 if (len < bytes_remaining) // Not all arrived yet
371 // We have the whole frame in local_frame. Send it to the stream.
372 // We still support streams that might not consume all the data.
373 state_stream_fill += audiostream.put(local_frame,
374 6 + frame_length - state_stream_fill);
375 if (state_stream_fill < frame_length + 6) // stream is full!
377 *full = 1; return ret;
379 state_frametype = state_framepos = 0;
383 int Demuxer::parse_private1_frame(int len, int* full)
385 int ret = 0; // return number of bytes consumed
388 switch(state_framepos)
390 case 0: // Brand new frame. Set initial states.
391 // Get MSB of frame length and copy to local frame.
392 frame_length = *inbuf << 8;
393 ++inbuf; ++state_framepos; ++ret; --len;
394 if (len == 0) return ret;
395 // FALL THROUGH TO NEXT BYTE IN STREAM
396 case 1: // Get LSB of frame length and copy to local frame.
397 frame_length += *inbuf;
398 local_frame[5] = *inbuf;
399 ++inbuf; ++state_framepos; ++ret; --len;
400 if (len == 0) return ret;
402 // We are in the frame data
403 bytes_remaining = 2 + frame_length - state_framepos;
404 // Temporary - just discard the frame.
405 if (len >= bytes_remaining)
407 inbuf += bytes_remaining;
408 ret += bytes_remaining;
409 state_frametype = state_framepos = 0;
414 inbuf += len; ret += len;
415 state_framepos += len;
420 void Demuxer::parse_video_details(UCHAR* buf, int len)
422 // Extract video PTS if it exists
423 if ( buf[1] & 0x80 ) // PTS_DTS_flags indicate that PTS is present
425 video_pts = ( (ULLONG)(buf[3] & 0x0E) << 29 ) |
426 ( (ULLONG)(buf[4]) << 22 ) |
427 ( (ULLONG)(buf[5] & 0xFE) << 14 ) |
428 ( (ULLONG)(buf[6]) << 7 ) |
429 ( (ULLONG)(buf[7] & 0xFE) >> 1 );
431 // Now, scan for a GOP header and extract video information
434 while (len >= 8) // 8 is length of a GOP header
436 // We are searching for a string of bytes (0,0,1).
437 byte = *(buf++); --len;
443 if (zeros < 2 || byte != 1)
448 // We have found the pattern (0,0,1).
449 // Check the next byte for the sub-frame type.
450 byte = *(buf++); --len;
453 case 0x00: // Picture header
454 // 10 bits: temporal reference
455 // 3 bits: coding type (I/P/B)
458 if ( (buf[1] & 0x38) == 0x08 ) // I-frame
460 buf += 4; // Minimum length of picture header
463 case 0xb3: // Sequence header
464 // 12 bits: Horizontal size
465 // 12 bits: Vertical size
466 // 4 bits: Aspect ratio
467 // 4 bits: Frame rate code
468 // 18 bits: Bit rate value
471 horizontal_size = ((int)buf[0] << 4) | ((int)buf[1] >> 4);
472 vertical_size = (((int)buf[1] & 0xf) << 8) | (int)buf[2];
473 setAspectRatio((enum AspectRatio)(buf[3] >> 4));
474 frame_rate = buf[3] & 0x0f;
475 if (frame_rate >= 1 && frame_rate <= 8)
476 frame_rate = FrameRates[frame_rate];
479 bit_rate = ((int)buf[4] << 10) |
483 buf += 8; // Minimum length of sequence header
486 case 0xb8: // Group header
487 // We're not going to bother parsing anything.
489 buf += 4; // Minimum length of group header
496 int Demuxer::findVideoPTS(UCHAR* buf, int len, ULLONG* dest)
498 UINT LoPattern = 0x100 | FRAMETYPE_VID0,
499 HiPattern = 0x100 | FRAMETYPE_VIDMAX;
503 UINT pattern = *(UINT*)buf;
505 if (pattern < LoPattern || pattern > HiPattern) continue;
507 UINT framelength = ((UINT)buf[3] << 8) | buf[4];
510 if ( buf[1] & 0x80 ) // PTS_DTS_flags indicate that PTS is present
512 *dest = ( (ULLONG)(buf[3] & 0x0E) << 29 ) |
513 ( (ULLONG)(buf[4]) << 22 ) |
514 ( (ULLONG)(buf[5] & 0xFE) << 14 ) |
515 ( (ULLONG)(buf[6]) << 7 ) |
516 ( (ULLONG)(buf[7] & 0xFE) >> 1 );
520 buf += framelength; len -= framelength;