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;
40 Demuxer* Demuxer::getInstance()
49 if ( videostream.init(demuxMemory) ||
50 audiostream.init(demuxMemory) ||
51 !(local_frame = (UCHAR *) malloc(0x10000)))
53 printf("failed to initialize demuxer\n");
67 video_current = audio_current = -1;
68 horizontal_size = vertical_size = 0;
69 aspect_ratio = (enum AspectRatio) 0;
70 frame_rate = bit_rate = 0;
73 int Demuxer::shutdown()
75 videostream.shutdown();
76 audiostream.shutdown();
86 state_frametype = state_framepos = 0;
90 void Demuxer::flushAudio()
100 void Demuxer::setAudioStream(int id)
105 void Demuxer::setVideoStream(int id)
110 int Demuxer::scan(UCHAR *buf, int len)
112 // Temporarily, just look for the lowest audio stream and return it
118 // We are searching for a string of bytes (0,0,1).
119 byte = *(buf++); --len;
125 if (zeros < 2 || byte != 1)
130 // We have found the pattern (0,0,1).
131 // Check the next byte for the sub-frame type.
132 byte = *(buf++); --len;
133 if (byte >= 0xc0 && byte <= 0xdf) // Audio
134 if (ret == 0 || ret > byte) ret = byte;
139 int Demuxer::put(UCHAR* buf, int len)
141 int ret = 0; // return number of bytes consumed
142 int parsed = 0; // number of bytes parsed by sub-function
143 int full; // sub-function sets this to tell us to exit early
144 inbuf = buf; // Initialize buffer pointer
149 switch (state_frametype)
151 case 0: // Search for frame
152 parsed = parse_find_frame(len);
154 case FRAMETYPE_VID0 ... FRAMETYPE_VIDMAX:
155 parsed = parse_video_frame(len, &full);
157 case FRAMETYPE_AUD0 ... FRAMETYPE_AUDMAX:
158 parsed = parse_audio_frame(len, &full);
161 ret += parsed; len -= parsed;
162 if (full) // We have to exit early.
163 break; // out of while loop
165 Log::getInstance()->log(
166 "Demuxer", Log::DEBUG, "Put %d; took %d", ret + len, ret);
170 int Demuxer::parse_find_frame(int len)
172 int ret = 0; // return number of bytes parsed
175 // In this function, state_framepos represents
176 // the number of fixed header bytes found so far.
177 if (state_framepos > 3 || state_framepos < 0)
187 switch (state_framepos)
203 state_framepos = 0; // Set initial state for the new frame
207 state_framepos = 1; // Count this as a first header byte!
209 case FRAMETYPE_VID0 ... FRAMETYPE_VIDMAX:
210 case FRAMETYPE_AUD0 ... FRAMETYPE_AUDMAX:
211 state_frametype = byte;
214 // Not a recognised frame type. Go back to Old Kent Road.
221 int Demuxer::parse_video_frame(int len, int* full)
223 int ret = 0; // return number of bytes consumed
224 int stream_sent, bytes_remaining;
226 switch(state_framepos)
228 case 0: // Brand new video frame. Set initial states.
229 state_stream_fill = 0; state_vid_parsed = 0;
230 // Create a local copy of the frame header
231 local_frame[0] = local_frame[1] = 0; local_frame[2] = 1;
232 local_frame[3] = state_frametype;
233 // If no video stream has been set, use this one.
234 if (video_current == -1) video_current = state_frametype;
235 // Get MSB of frame length and copy to local frame.
236 frame_length = *inbuf << 8;
237 local_frame[4] = *inbuf;
238 ++inbuf; ++state_framepos; ++ret; --len;
239 if (len == 0) return ret;
240 // FALL THROUGH TO NEXT BYTE IN STREAM
241 case 1: // Get LSB of frame length and copy to local frame.
242 frame_length += *inbuf;
243 local_frame[5] = *inbuf;
244 ++inbuf; ++state_framepos; ++ret; --len;
245 if (len == 0) return ret;
246 // FALL THROUGH TO NEXT BYTE IN STREAM
247 case 2: // First data byte
248 if (len >= frame_length // If we have the entire frame
249 && !state_vid_parsed) // and haven't parsed it yet
251 if (video_current == state_frametype) // and we're interested
252 parse_video_details(inbuf, frame_length); // then parse it
253 state_vid_parsed = 1;
256 // We are in the frame data
257 bytes_remaining = 2 + frame_length - state_framepos;
258 if (video_current != state_frametype)
260 // We don't want this frame. Throw it away.
261 if (len >= bytes_remaining)
263 inbuf += bytes_remaining;
264 ret += bytes_remaining;
265 state_frametype = state_framepos = 0;
270 inbuf += len; ret += len;
271 state_framepos += len;
274 } // No fall through here
276 if (state_vid_parsed)
278 // We have already parsed the whole frame
279 if (seeking) // Still not found a sync point. Throw this frame away.
281 if (len >= bytes_remaining)
283 inbuf += bytes_remaining;
284 ret += bytes_remaining;
285 state_frametype = state_framepos = 0;
290 inbuf += len; ret += len; return ret;
292 } // No fall through is allowed here
293 // Send frame header to stream
294 if (state_stream_fill < 6)
296 state_stream_fill += videostream.put(local_frame,
297 6 - state_stream_fill);
298 if (state_stream_fill < 6) // stream is full!
300 *full = 1; return ret;
303 // Send all frame data we have to stream
304 if (len >= bytes_remaining) len = bytes_remaining;
305 stream_sent = videostream.put(inbuf, len);
306 inbuf += stream_sent; ret += stream_sent;
307 state_framepos += stream_sent;
308 state_stream_fill += stream_sent;
309 if (stream_sent != len) // stream is full!
311 *full = 1; return ret;
313 if (state_framepos == frame_length + 2) // frame done
315 state_frametype = state_framepos = 0;
318 } // No fall through is allowed here
320 // We haven't parsed the frame yet. It's arriving in pieces.
321 if (bytes_remaining) // There is data yet to copy to local_frame
323 if (len >= bytes_remaining) len = bytes_remaining;
324 memcpy(local_frame + state_framepos + 4, inbuf, len);
325 inbuf += len; ret += len; state_framepos += len;
326 if (len < bytes_remaining) // Not all arrived yet
328 parse_video_details(local_frame+6, frame_length);
329 if (seeking) // Still not found a sync point. Ignore this frame.
332 // We have the whole frame in local_frame. Send it to the stream.
333 state_stream_fill += videostream.put(local_frame,
334 6 + frame_length - state_stream_fill);
335 if (state_stream_fill < frame_length + 6) // stream is full!
337 *full = 1; return ret;
339 state_frametype = state_framepos = 0;
343 int Demuxer::parse_audio_frame(int len, int* full)
345 int ret = 0; // return number of bytes consumed
346 int stream_sent, bytes_remaining;
348 switch(state_framepos)
350 case 0: // Brand new audio frame. Set initial states.
351 state_stream_fill = 0;
352 // Create a local copy of the frame header
353 local_frame[0] = local_frame[1] = 0; local_frame[2] = 1;
354 local_frame[3] = state_frametype;
355 // If no audio stream has been set, use this one.
356 if (audio_current == -1) audio_current = state_frametype;
357 // Get MSB of frame length and copy to local frame.
358 frame_length = *inbuf << 8;
359 local_frame[4] = *inbuf;
360 ++inbuf; ++state_framepos; ++ret; --len;
361 if (len == 0) return ret;
362 // FALL THROUGH TO NEXT BYTE IN STREAM
363 case 1: // Get LSB of frame length and copy to local frame.
364 frame_length += *inbuf;
365 local_frame[5] = *inbuf;
366 ++inbuf; ++state_framepos; ++ret; --len;
367 if (len == 0) return ret;
369 // We are in the frame data
370 bytes_remaining = 2 + frame_length - state_framepos;
371 if (audio_current != state_frametype)
373 // We don't want this frame. Throw it away.
374 if (len >= bytes_remaining)
376 inbuf += bytes_remaining;
377 ret += bytes_remaining;
378 state_frametype = state_framepos = 0;
383 inbuf += len; ret += len;
384 state_framepos += len;
387 } // No fall through is allowed here
389 // Send frame header to stream
390 if (state_stream_fill < 6)
392 state_stream_fill += audiostream.put(local_frame,
393 6 - state_stream_fill);
394 if (state_stream_fill < 6) // stream is full!
396 *full = 1; return ret;
399 // Send all frame data we have to stream
400 if (len >= bytes_remaining) len = bytes_remaining;
401 stream_sent = audiostream.put(inbuf, len);
402 inbuf += stream_sent; ret += stream_sent;
403 state_framepos += stream_sent;
404 state_stream_fill += stream_sent;
405 if (stream_sent != len) // stream is full!
407 *full = 1; return ret;
409 if (state_framepos == frame_length + 2) // frame done
411 state_frametype = state_framepos = 0;
416 void Demuxer::parse_video_details(UCHAR* buf, int len)
420 while (len >= 8) // 8 is length of a GOP header
422 // We are searching for a string of bytes (0,0,1).
423 byte = *(buf++); --len;
429 if (zeros < 2 || byte != 1)
434 // We have found the pattern (0,0,1).
435 // Check the next byte for the sub-frame type.
436 byte = *(buf++); --len;
439 case 0x00: // Picture header
440 // 10 bits: temporal reference
441 // 3 bits: coding type (I/P/B)
444 if ( (buf[1] & 0x38) == 0x08 ) // I-frame
446 buf += 4; // Minimum length of picture header
449 case 0xb3: // Sequence header
450 // 12 bits: Horizontal size
451 // 12 bits: Vertical size
452 // 4 bits: Aspect ratio
453 // 4 bits: Frame rate code
454 // 18 bits: Bit rate value
457 horizontal_size = ((int)buf[0] << 4) | ((int)buf[1] >> 4);
458 vertical_size = (((int)buf[1] & 0xf) << 8) | (int)buf[2];
459 aspect_ratio = (enum AspectRatio)(buf[3] >> 4);
460 frame_rate = buf[3] & 0x0f;
461 if (frame_rate >= 1 && frame_rate <= 8)
462 frame_rate = FrameRates[frame_rate];
465 bit_rate = ((int)buf[4] << 10) |
469 buf += 8; // Minimum length of sequence header
472 case 0xb8: // Group header
473 // We're not going to bother parsing anything.
475 buf += 4; // Minimum length of group header