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);
160 case FRAMETYPE_PRIVATE_1:
161 parsed = parse_private1_frame(len, &full);
164 ret += parsed; len -= parsed;
165 if (full) // We have to exit early.
166 break; // out of while loop
168 //Log::getInstance()->log("Demuxer", Log::DEBUG, "Put %d; took %d", ret + len, ret);
172 int Demuxer::parse_find_frame(int len)
174 int ret = 0; // return number of bytes parsed
177 // In this function, state_framepos represents
178 // the number of fixed header bytes found so far.
179 if (state_framepos > 3 || state_framepos < 0)
189 switch (state_framepos)
205 state_framepos = 0; // Set initial state for the new frame
209 state_framepos = 1; // Count this as a first header byte!
211 case FRAMETYPE_VID0 ... FRAMETYPE_VIDMAX:
212 case FRAMETYPE_AUD0 ... FRAMETYPE_AUDMAX:
213 case FRAMETYPE_PRIVATE_1:
214 state_frametype = byte;
217 // Not a recognised frame type. Go back to Old Kent Road.
224 int Demuxer::parse_video_frame(int len, int* full)
226 int ret = 0; // return number of bytes consumed
229 switch(state_framepos)
231 case 0: // Brand new video frame. Set initial states.
232 state_stream_fill = 0; state_vid_parsed = 0;
233 // Create a local copy of the frame header
234 local_frame[0] = local_frame[1] = 0; local_frame[2] = 1;
235 local_frame[3] = state_frametype;
236 // If no video stream has been set, use this one.
237 if (video_current == -1) video_current = state_frametype;
238 // Get MSB of frame length and copy to local frame.
239 frame_length = *inbuf << 8;
240 local_frame[4] = *inbuf;
241 ++inbuf; ++state_framepos; ++ret; --len;
242 if (len == 0) return ret;
243 // FALL THROUGH TO NEXT BYTE IN STREAM
244 case 1: // Get LSB of frame length and copy to local frame.
245 frame_length += *inbuf;
246 local_frame[5] = *inbuf;
247 ++inbuf; ++state_framepos; ++ret; --len;
248 if (len == 0) return ret;
250 // We are in the frame data
251 bytes_remaining = 2 + frame_length - state_framepos;
252 if (video_current != state_frametype)
254 // We don't want this frame. Throw it away.
255 if (len >= bytes_remaining)
257 inbuf += bytes_remaining;
258 ret += bytes_remaining;
259 state_frametype = state_framepos = 0;
264 inbuf += len; ret += len;
265 state_framepos += len;
268 } // No fall through here
270 if (bytes_remaining) // There is data yet to copy to local_frame
272 if (len > bytes_remaining) len = bytes_remaining;
273 memcpy(local_frame + state_framepos + 4, inbuf, len);
274 inbuf += len; ret += len; state_framepos += len;
275 if (len < bytes_remaining) // Not all arrived yet
277 parse_video_details(local_frame+6, frame_length);
282 // We have the whole frame in local_frame. Send it to the stream.
283 // We still support streams that might not consume all the data.
284 state_stream_fill += videostream.put(local_frame,
285 6 + frame_length - state_stream_fill);
286 if (state_stream_fill < frame_length + 6) // stream is full!
288 *full = 1; return ret;
291 state_frametype = state_framepos = 0;
295 int Demuxer::parse_audio_frame(int len, int* full)
297 int ret = 0; // return number of bytes consumed
300 switch(state_framepos)
302 case 0: // Brand new audio frame. Set initial states.
303 state_stream_fill = 0;
304 // Create a local copy of the frame header
305 local_frame[0] = local_frame[1] = 0; local_frame[2] = 1;
306 local_frame[3] = state_frametype;
307 // If no audio stream has been set, use this one.
308 if (audio_current == -1) audio_current = state_frametype;
309 // Get MSB of frame length and copy to local frame.
310 frame_length = *inbuf << 8;
311 local_frame[4] = *inbuf;
312 ++inbuf; ++state_framepos; ++ret; --len;
313 if (len == 0) return ret;
314 // FALL THROUGH TO NEXT BYTE IN STREAM
315 case 1: // Get LSB of frame length and copy to local frame.
316 frame_length += *inbuf;
317 local_frame[5] = *inbuf;
318 ++inbuf; ++state_framepos; ++ret; --len;
319 if (len == 0) return ret;
321 // We are in the frame data
322 bytes_remaining = 2 + frame_length - state_framepos;
323 if (audio_current != state_frametype)
325 // We don't want this frame. Throw it away.
326 if (len >= bytes_remaining)
328 inbuf += bytes_remaining;
329 ret += bytes_remaining;
330 state_frametype = state_framepos = 0;
335 inbuf += len; ret += len;
336 state_framepos += len;
339 } // No fall through is allowed here
341 if (bytes_remaining) // There is data yet to copy to local_frame
343 if (len > bytes_remaining) len = bytes_remaining;
344 memcpy(local_frame + state_framepos + 4, inbuf, len);
345 inbuf += len; ret += len; state_framepos += len;
346 if (len < bytes_remaining) // Not all arrived yet
350 // We have the whole frame in local_frame. Send it to the stream.
351 // We still support streams that might not consume all the data.
352 state_stream_fill += audiostream.put(local_frame,
353 6 + frame_length - state_stream_fill);
354 if (state_stream_fill < frame_length + 6) // stream is full!
356 *full = 1; return ret;
358 state_frametype = state_framepos = 0;
362 int Demuxer::parse_private1_frame(int len, int* full)
364 int ret = 0; // return number of bytes consumed
367 switch(state_framepos)
369 case 0: // Brand new frame. Set initial states.
370 // Get MSB of frame length and copy to local frame.
371 frame_length = *inbuf << 8;
372 ++inbuf; ++state_framepos; ++ret; --len;
373 if (len == 0) return ret;
374 // FALL THROUGH TO NEXT BYTE IN STREAM
375 case 1: // Get LSB of frame length and copy to local frame.
376 frame_length += *inbuf;
377 local_frame[5] = *inbuf;
378 ++inbuf; ++state_framepos; ++ret; --len;
379 if (len == 0) return ret;
381 // We are in the frame data
382 bytes_remaining = 2 + frame_length - state_framepos;
383 // Temporary - just discard the frame.
384 if (len >= bytes_remaining)
386 inbuf += bytes_remaining;
387 ret += bytes_remaining;
388 state_frametype = state_framepos = 0;
393 inbuf += len; ret += len;
394 state_framepos += len;
399 void Demuxer::parse_video_details(UCHAR* buf, int len)
403 while (len >= 8) // 8 is length of a GOP header
405 // We are searching for a string of bytes (0,0,1).
406 byte = *(buf++); --len;
412 if (zeros < 2 || byte != 1)
417 // We have found the pattern (0,0,1).
418 // Check the next byte for the sub-frame type.
419 byte = *(buf++); --len;
422 case 0x00: // Picture header
423 // 10 bits: temporal reference
424 // 3 bits: coding type (I/P/B)
427 if ( (buf[1] & 0x38) == 0x08 ) // I-frame
429 buf += 4; // Minimum length of picture header
432 case 0xb3: // Sequence header
433 // 12 bits: Horizontal size
434 // 12 bits: Vertical size
435 // 4 bits: Aspect ratio
436 // 4 bits: Frame rate code
437 // 18 bits: Bit rate value
440 horizontal_size = ((int)buf[0] << 4) | ((int)buf[1] >> 4);
441 vertical_size = (((int)buf[1] & 0xf) << 8) | (int)buf[2];
442 aspect_ratio = (enum AspectRatio)(buf[3] >> 4);
443 frame_rate = buf[3] & 0x0f;
444 if (frame_rate >= 1 && frame_rate <= 8)
445 frame_rate = FrameRates[frame_rate];
448 bit_rate = ((int)buf[4] << 10) |
452 buf += 8; // Minimum length of sequence header
455 case 0xb8: // Group header
456 // We're not going to bother parsing anything.
458 buf += 4; // Minimum length of group header