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 cbAspectRatio = 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()
114 if (aspect_ratio != cbAspectRatio)
116 cbAspectRatio = aspect_ratio;
117 callback->call(this);
121 int Demuxer::getAspectRatio()
126 int Demuxer::scan(UCHAR *buf, int len)
128 // Temporarily, just look for the lowest audio stream and return it
134 // We are searching for a string of bytes (0,0,1).
135 byte = *(buf++); --len;
141 if (zeros < 2 || byte != 1)
146 // We have found the pattern (0,0,1).
147 // Check the next byte for the sub-frame type.
148 byte = *(buf++); --len;
149 if (byte >= 0xc0 && byte <= 0xdf) // Audio
150 if (ret == 0 || ret > byte) ret = byte;
155 int Demuxer::put(UCHAR* buf, int len)
157 int ret = 0; // return number of bytes consumed
158 int parsed = 0; // number of bytes parsed by sub-function
159 int full; // sub-function sets this to tell us to exit early
160 inbuf = buf; // Initialize buffer pointer
165 switch (state_frametype)
167 case 0: // Search for frame
168 parsed = parse_find_frame(len);
170 case FRAMETYPE_VID0 ... FRAMETYPE_VIDMAX:
171 parsed = parse_video_frame(len, &full);
173 case FRAMETYPE_AUD0 ... FRAMETYPE_AUDMAX:
174 parsed = parse_audio_frame(len, &full);
176 case FRAMETYPE_PRIVATE_1:
177 parsed = parse_private1_frame(len, &full);
180 ret += parsed; len -= parsed;
181 if (full) // We have to exit early.
182 break; // out of while loop
184 //Log::getInstance()->log("Demuxer", Log::DEBUG, "Put %d; took %d", ret + len, ret);
188 int Demuxer::parse_find_frame(int len)
190 int ret = 0; // return number of bytes parsed
193 // In this function, state_framepos represents
194 // the number of fixed header bytes found so far.
195 if (state_framepos > 3 || state_framepos < 0)
205 switch (state_framepos)
221 state_framepos = 0; // Set initial state for the new frame
225 state_framepos = 1; // Count this as a first header byte!
227 case FRAMETYPE_VID0 ... FRAMETYPE_VIDMAX:
228 case FRAMETYPE_AUD0 ... FRAMETYPE_AUDMAX:
229 case FRAMETYPE_PRIVATE_1:
230 state_frametype = byte;
233 // Not a recognised frame type. Go back to Old Kent Road.
240 int Demuxer::parse_video_frame(int len, int* full)
242 int ret = 0; // return number of bytes consumed
245 switch(state_framepos)
247 case 0: // Brand new video frame. Set initial states.
248 state_stream_fill = 0; state_vid_parsed = 0;
249 // Create a local copy of the frame header
250 local_frame[0] = local_frame[1] = 0; local_frame[2] = 1;
251 local_frame[3] = state_frametype;
252 // If no video stream has been set, use this one.
253 if (video_current == -1) video_current = state_frametype;
254 // Get MSB of frame length and copy to local frame.
255 frame_length = *inbuf << 8;
256 local_frame[4] = *inbuf;
257 ++inbuf; ++state_framepos; ++ret; --len;
258 if (len == 0) return ret;
259 // FALL THROUGH TO NEXT BYTE IN STREAM
260 case 1: // Get LSB of frame length and copy to local frame.
261 frame_length += *inbuf;
262 local_frame[5] = *inbuf;
263 ++inbuf; ++state_framepos; ++ret; --len;
264 if (len == 0) return ret;
266 // We are in the frame data
267 bytes_remaining = 2 + frame_length - state_framepos;
268 if (video_current != state_frametype)
270 // We don't want this frame. Throw it away.
271 if (len >= bytes_remaining)
273 inbuf += bytes_remaining;
274 ret += bytes_remaining;
275 state_frametype = state_framepos = 0;
280 inbuf += len; ret += len;
281 state_framepos += len;
284 } // No fall through here
286 if (bytes_remaining) // There is data yet to copy to local_frame
288 if (len > bytes_remaining) len = bytes_remaining;
289 memcpy(local_frame + state_framepos + 4, inbuf, len);
290 inbuf += len; ret += len; state_framepos += len;
291 if (len < bytes_remaining) // Not all arrived yet
293 parse_video_details(local_frame+6, frame_length);
298 // We have the whole frame in local_frame. Send it to the stream.
299 // We still support streams that might not consume all the data.
300 state_stream_fill += videostream.put(local_frame,
301 6 + frame_length - state_stream_fill);
302 if (state_stream_fill < frame_length + 6) // stream is full!
304 *full = 1; return ret;
307 state_frametype = state_framepos = 0;
311 int Demuxer::parse_audio_frame(int len, int* full)
313 int ret = 0; // return number of bytes consumed
316 switch(state_framepos)
318 case 0: // Brand new audio frame. Set initial states.
319 state_stream_fill = 0;
320 // Create a local copy of the frame header
321 local_frame[0] = local_frame[1] = 0; local_frame[2] = 1;
322 local_frame[3] = state_frametype;
323 // If no audio stream has been set, use this one.
324 if (audio_current == -1) audio_current = state_frametype;
325 // Get MSB of frame length and copy to local frame.
326 frame_length = *inbuf << 8;
327 local_frame[4] = *inbuf;
328 ++inbuf; ++state_framepos; ++ret; --len;
329 if (len == 0) return ret;
330 // FALL THROUGH TO NEXT BYTE IN STREAM
331 case 1: // Get LSB of frame length and copy to local frame.
332 frame_length += *inbuf;
333 local_frame[5] = *inbuf;
334 ++inbuf; ++state_framepos; ++ret; --len;
335 if (len == 0) return ret;
337 // We are in the frame data
338 bytes_remaining = 2 + frame_length - state_framepos;
339 if (audio_current != state_frametype)
341 // We don't want this frame. Throw it away.
342 if (len >= bytes_remaining)
344 inbuf += bytes_remaining;
345 ret += bytes_remaining;
346 state_frametype = state_framepos = 0;
351 inbuf += len; ret += len;
352 state_framepos += len;
355 } // No fall through is allowed here
357 if (bytes_remaining) // There is data yet to copy to local_frame
359 if (len > bytes_remaining) len = bytes_remaining;
360 memcpy(local_frame + state_framepos + 4, inbuf, len);
361 inbuf += len; ret += len; state_framepos += len;
362 if (len < bytes_remaining) // Not all arrived yet
366 // We have the whole frame in local_frame. Send it to the stream.
367 // We still support streams that might not consume all the data.
368 state_stream_fill += audiostream.put(local_frame,
369 6 + frame_length - state_stream_fill);
370 if (state_stream_fill < frame_length + 6) // stream is full!
372 *full = 1; return ret;
374 state_frametype = state_framepos = 0;
378 int Demuxer::parse_private1_frame(int len, int* full)
380 int ret = 0; // return number of bytes consumed
383 switch(state_framepos)
385 case 0: // Brand new frame. Set initial states.
386 // Get MSB of frame length and copy to local frame.
387 frame_length = *inbuf << 8;
388 ++inbuf; ++state_framepos; ++ret; --len;
389 if (len == 0) return ret;
390 // FALL THROUGH TO NEXT BYTE IN STREAM
391 case 1: // Get LSB of frame length and copy to local frame.
392 frame_length += *inbuf;
393 local_frame[5] = *inbuf;
394 ++inbuf; ++state_framepos; ++ret; --len;
395 if (len == 0) return ret;
397 // We are in the frame data
398 bytes_remaining = 2 + frame_length - state_framepos;
399 // Temporary - just discard the frame.
400 if (len >= bytes_remaining)
402 inbuf += bytes_remaining;
403 ret += bytes_remaining;
404 state_frametype = state_framepos = 0;
409 inbuf += len; ret += len;
410 state_framepos += len;
415 void Demuxer::parse_video_details(UCHAR* buf, int len)
419 while (len >= 8) // 8 is length of a GOP header
421 // We are searching for a string of bytes (0,0,1).
422 byte = *(buf++); --len;
428 if (zeros < 2 || byte != 1)
433 // We have found the pattern (0,0,1).
434 // Check the next byte for the sub-frame type.
435 byte = *(buf++); --len;
438 case 0x00: // Picture header
439 // 10 bits: temporal reference
440 // 3 bits: coding type (I/P/B)
443 if ( (buf[1] & 0x38) == 0x08 ) // I-frame
445 buf += 4; // Minimum length of picture header
448 case 0xb3: // Sequence header
449 // 12 bits: Horizontal size
450 // 12 bits: Vertical size
451 // 4 bits: Aspect ratio
452 // 4 bits: Frame rate code
453 // 18 bits: Bit rate value
456 horizontal_size = ((int)buf[0] << 4) | ((int)buf[1] >> 4);
457 vertical_size = (((int)buf[1] & 0xf) << 8) | (int)buf[2];
458 aspect_ratio = (enum AspectRatio)(buf[3] >> 4);
459 frame_rate = buf[3] & 0x0f;
460 if (frame_rate >= 1 && frame_rate <= 8)
461 frame_rate = FrameRates[frame_rate];
464 bit_rate = ((int)buf[4] << 10) |
468 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