1 /*****************************************************************************
2 * mpegvideo.c: parse and packetize an MPEG1/2 video stream
3 *****************************************************************************
4 * Copyright (C) 2001, 2002 VideoLAN
5 * $Id: mpegvideo.c,v 1.27 2004/01/25 17:58:30 murray Exp $
7 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8 * Eric Petit <titer@videolan.org>
9 * Gildas Bazin <gbazin@netcourrier.com>
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
24 *****************************************************************************/
26 /*****************************************************************************
27 * Problem with this implementation:
29 * Although we should time-stamp each picture with a PTS, this isn't possible
30 * with the current implementation.
31 * The problem comes from the fact that for non-low-delay streams we can't
32 * calculate the PTS of pictures used as backward reference. Even the temporal
33 * reference number doesn't help here because all the pictures don't
34 * necessarily have the same duration (eg. 3:2 pulldown).
36 * However this doesn't really matter as far as the MPEG muxers are concerned
37 * because they allow having empty PTS fields. --gibalou
38 *****************************************************************************/
40 /*****************************************************************************
42 *****************************************************************************/
43 #include <stdlib.h> /* malloc(), free() */
46 #include <vlc/decoder.h>
47 #include <vlc/input.h>
49 #include "vlc_block_helper.h"
51 /*****************************************************************************
53 *****************************************************************************/
54 static int Open ( vlc_object_t * );
55 static void Close( vlc_object_t * );
58 set_description( _("MPEG-I/II video packetizer") );
59 set_capability( "packetizer", 50 );
60 set_callbacks( Open, Close );
63 /*****************************************************************************
65 *****************************************************************************/
66 static block_t *Packetize( decoder_t *, block_t ** );
67 static block_t *ParseMPEGBlock( decoder_t *, block_t * );
74 block_bytestream_t bytestream;
77 uint8_t p_startcode[3];
79 /* Sequence header and extention */
83 /* Current frame being built */
85 vlc_bool_t b_frame_slice;
89 /* Sequence properties */
91 int i_frame_rate_base;
92 vlc_bool_t b_seq_progressive;
93 vlc_bool_t b_low_delay;
94 int i_aspect_ratio_info;
96 /* Picture properties */
99 int i_picture_structure;
100 int i_top_field_first;
101 int i_repeat_first_field;
102 int i_progressive_frame;
104 mtime_t i_interpolated_dts;
105 mtime_t i_old_duration;
106 mtime_t i_last_ref_pts;
108 /* Number of pictues since last sequence header */
118 /*****************************************************************************
120 *****************************************************************************/
121 static int Open( vlc_object_t *p_this )
123 decoder_t *p_dec = (decoder_t*)p_this;
124 decoder_sys_t *p_sys;
126 if( p_dec->fmt_in.i_codec != VLC_FOURCC( 'm', 'p', 'g', '1' ) &&
127 p_dec->fmt_in.i_codec != VLC_FOURCC( 'm', 'p', 'g', '2' ) &&
128 p_dec->fmt_in.i_codec != VLC_FOURCC( 'm', 'p', 'g', 'v' ) )
133 es_format_Init( &p_dec->fmt_out, VIDEO_ES, VLC_FOURCC('m','p','g','v') );
134 p_dec->pf_packetize = Packetize;
136 p_dec->p_sys = p_sys = malloc( sizeof( decoder_sys_t ) );
139 p_sys->i_state = STATE_NOSYNC;
140 p_sys->bytestream = block_BytestreamInit( p_dec );
141 p_sys->p_startcode[0] = 0;
142 p_sys->p_startcode[1] = 0;
143 p_sys->p_startcode[2] = 1;
148 p_sys->p_frame = NULL;
149 p_sys->b_frame_slice = VLC_FALSE;
151 p_sys->i_dts = p_sys->i_pts = 0;
153 p_sys->i_frame_rate = 1;
154 p_sys->i_frame_rate_base = 1;
155 p_sys->b_seq_progressive = VLC_TRUE;
156 p_sys->b_low_delay = VLC_TRUE;
157 p_sys->i_seq_old = 0;
159 p_sys->i_temporal_ref = 0;
160 p_sys->i_picture_type = 0;
161 p_sys->i_picture_structure = 0x03; /* frame */
162 p_sys->i_top_field_first = 0;
163 p_sys->i_repeat_first_field = 0;
164 p_sys->i_progressive_frame = 0;
166 p_sys->i_interpolated_dts = 0;
167 p_sys->i_old_duration = 0;
168 p_sys->i_last_ref_pts = 0;
173 /*****************************************************************************
175 *****************************************************************************/
176 static void Close( vlc_object_t *p_this )
178 decoder_t *p_dec = (decoder_t*)p_this;
179 decoder_sys_t *p_sys = p_dec->p_sys;
181 block_BytestreamRelease( &p_sys->bytestream );
185 block_Release( p_sys->p_seq );
189 block_Release( p_sys->p_ext );
193 block_ChainRelease( p_sys->p_frame );
199 /*****************************************************************************
201 *****************************************************************************/
202 static block_t *Packetize( decoder_t *p_dec, block_t **pp_block )
204 decoder_sys_t *p_sys = p_dec->p_sys;
207 if( pp_block == NULL || *pp_block == NULL )
212 if( (*pp_block)->b_discontinuity )
214 p_sys->i_state = STATE_NOSYNC;
215 if( p_sys->p_frame ) block_ChainRelease( p_sys->p_frame );
216 p_sys->p_frame = NULL;
217 p_sys->b_frame_slice = VLC_FALSE;
220 block_BytestreamPush( &p_sys->bytestream, *pp_block );
224 switch( p_sys->i_state )
228 if( block_FindStartcodeFromOffset( &p_sys->bytestream,
229 &p_sys->i_offset, p_sys->p_startcode, 3 ) == VLC_SUCCESS )
231 p_sys->i_state = STATE_NEXT_SYNC;
234 if( p_sys->i_offset )
236 block_SkipBytes( &p_sys->bytestream, p_sys->i_offset );
238 block_BytestreamFlush( &p_sys->bytestream );
241 if( p_sys->i_state != STATE_NEXT_SYNC )
247 p_sys->i_offset = 1; /* To find next startcode */
249 case STATE_NEXT_SYNC:
250 /* TODO: If p_block == NULL, flush the buffer without checking the
253 /* Find the next startcode */
254 if( block_FindStartcodeFromOffset( &p_sys->bytestream,
255 &p_sys->i_offset, p_sys->p_startcode, 3 ) != VLC_SUCCESS )
261 /* Get the new fragment and set the pts/dts */
262 p_pic = block_New( p_dec, p_sys->i_offset );
263 p_pic->i_pts = p_sys->bytestream.p_block->i_pts;
264 p_pic->i_dts = p_sys->bytestream.p_block->i_dts;
266 block_GetBytes( &p_sys->bytestream, p_pic->p_buffer,
269 /* don't reuse the same timestamps several times */
270 if( p_pic->i_buffer >= 4 && p_pic->p_buffer[3] == 0x00 )
272 /* We have a picture start code */
273 p_sys->bytestream.p_block->i_pts = 0;
274 p_sys->bytestream.p_block->i_dts = 0;
279 /* Get picture if any */
280 if( !( p_pic = ParseMPEGBlock( p_dec, p_pic ) ) )
282 p_sys->i_state = STATE_NOSYNC;
286 /* We've just started the stream, wait for the first PTS.
287 * We discard here so we can still get the sequence header. */
288 if( p_sys->i_dts <= 0 && p_sys->i_pts <= 0 &&
289 p_sys->i_interpolated_dts <= 0 )
291 msg_Dbg( p_dec, "need a starting pts/dts" );
292 p_sys->i_state = STATE_NOSYNC;
293 block_Release( p_pic );
297 /* So p_block doesn't get re-added several times */
298 *pp_block = block_BytestreamPop( &p_sys->bytestream );
300 p_sys->i_state = STATE_NOSYNC;
307 /*****************************************************************************
308 * ParseMPEGBlock: Re-assemble fragments into a block containing a picture
309 *****************************************************************************/
310 static block_t *ParseMPEGBlock( decoder_t *p_dec, block_t *p_frag )
312 decoder_sys_t *p_sys = p_dec->p_sys;
313 block_t *p_pic = NULL;
316 * Check if previous picture is finished
318 if( ( p_sys->b_frame_slice &&
319 (p_frag->p_buffer[3] == 0x00 || p_frag->p_buffer[3] > 0xaf) ) &&
320 p_sys->p_seq == NULL )
322 /* We have a picture but without a sequence header we can't
324 msg_Dbg( p_dec, "waiting for sequence start" );
325 if( p_sys->p_frame ) block_ChainRelease( p_sys->p_frame );
326 p_sys->p_frame = NULL;
327 p_sys->b_frame_slice = VLC_FALSE;
330 else if( p_sys->b_frame_slice &&
331 (p_frag->p_buffer[3] == 0x00 || p_frag->p_buffer[3] > 0xaf) )
335 p_pic = block_ChainGather( p_sys->p_frame );
337 i_duration = (mtime_t)( 1000000 * p_sys->i_frame_rate_base /
338 p_sys->i_frame_rate );
340 if( !p_sys->b_seq_progressive && p_sys->i_picture_structure != 0x03 )
345 if( p_sys->b_seq_progressive )
347 if( p_sys->i_top_field_first == 0 &&
348 p_sys->i_repeat_first_field == 1 )
352 else if( p_sys->i_top_field_first == 1 &&
353 p_sys->i_repeat_first_field == 1 )
360 if( p_sys->i_picture_structure == 0x03 )
362 if( p_sys->i_progressive_frame && p_sys->i_repeat_first_field )
364 i_duration += i_duration / 2;
369 if( p_sys->b_low_delay || p_sys->i_picture_type == 0x03 )
371 /* Trivial case (DTS == PTS) */
372 /* Correct interpolated dts when we receive a new pts/dts */
373 if( p_sys->i_pts > 0 ) p_sys->i_interpolated_dts = p_sys->i_pts;
374 if( p_sys->i_dts > 0 ) p_sys->i_interpolated_dts = p_sys->i_dts;
378 /* Correct interpolated dts when we receive a new pts/dts */
379 if( p_sys->i_last_ref_pts > 0 )
380 p_sys->i_interpolated_dts = p_sys->i_last_ref_pts;
381 if( p_sys->i_dts > 0 ) p_sys->i_interpolated_dts = p_sys->i_dts;
383 p_sys->i_last_ref_pts = p_sys->i_pts;
386 p_pic->i_dts = p_sys->i_interpolated_dts;
388 /* Set PTS only if we have a B frame or if it comes from the stream */
389 if( p_sys->i_pts > 0 )
391 p_pic->i_pts = p_sys->i_pts;
393 else if( p_sys->i_picture_type == 0x03 )
395 p_pic->i_pts = p_pic->i_dts;
402 if( p_sys->b_low_delay || p_sys->i_picture_type == 0x03 )
404 /* Trivial case (DTS == PTS) */
405 p_sys->i_interpolated_dts += i_duration;
409 p_sys->i_interpolated_dts += p_sys->i_old_duration;
410 p_sys->i_old_duration = i_duration;
413 p_pic->i_length = p_sys->i_interpolated_dts - p_pic->i_dts;
416 msg_Dbg( p_dec, "pic: type=%d dts="I64Fd" pts-dts="I64Fd,
417 p_sys->i_picture_type, p_pic->i_dts, p_pic->i_pts - p_pic->i_dts);
421 p_sys->p_frame = NULL;
422 p_sys->b_frame_slice = VLC_FALSE;
426 * Check info of current fragment
428 if( p_frag->p_buffer[3] == 0xb8 )
430 /* Group start code */
432 p_sys->i_seq_old > p_sys->i_frame_rate/p_sys->i_frame_rate_base )
434 /* Usefull for mpeg1: repeat sequence header every second */
435 block_ChainAppend( &p_sys->p_frame,
436 block_Duplicate( p_sys->p_seq ) );
439 block_ChainAppend( &p_sys->p_frame,
440 block_Duplicate( p_sys->p_ext ) );
443 p_sys->i_seq_old = 0;
446 else if( p_frag->p_buffer[3] == 0xb3 && p_frag->i_buffer >= 8 )
448 /* Sequence header code */
449 static const int code_to_frame_rate[16][2] =
451 { 1, 1 }, /* invalid */
452 { 24000, 1001 }, { 24, 1 }, { 25, 1 }, { 30000, 1001 },
453 { 30, 1 }, { 50, 1 }, { 60000, 1001 }, { 60, 1 },
454 /* Unofficial 15fps from Xing*/
456 /* Unofficial economy rates from libmpeg3 */
457 { 5, 1001 }, { 10, 1001 }, { 12, 1001 }, { 15, 1001 },
458 { 1, 1 }, { 1, 1 } /* invalid */
461 if( p_sys->p_seq ) block_Release( p_sys->p_seq );
462 if( p_sys->p_ext ) block_Release( p_sys->p_ext );
464 p_sys->p_seq = block_Duplicate( p_frag );
465 p_sys->i_seq_old = 0;
468 p_dec->fmt_out.video.i_width =
469 ( p_frag->p_buffer[4] << 4)|(p_frag->p_buffer[5] >> 4 );
470 p_dec->fmt_out.video.i_height =
471 ( (p_frag->p_buffer[5]&0x0f) << 8 )|p_frag->p_buffer[6];
472 p_sys->i_aspect_ratio_info = p_frag->p_buffer[7] >> 4;
474 /* TODO: MPEG1 aspect ratio */
476 p_sys->i_frame_rate = code_to_frame_rate[p_frag->p_buffer[7]&0x0f][0];
477 p_sys->i_frame_rate_base =
478 code_to_frame_rate[p_frag->p_buffer[7]&0x0f][1];
480 p_sys->b_seq_progressive = VLC_TRUE;
481 p_sys->b_low_delay = VLC_TRUE;
484 msg_Dbg( p_dec, "Size %dx%d fps=%.3f",
485 p_dec->fmt_out.video.i_width, p_dec->fmt_out.video.i_height,
486 p_sys->i_frame_rate / (float)p_sys->i_frame_rate_base );
489 else if( p_frag->p_buffer[3] == 0xb5 )
491 int i_type = p_frag->p_buffer[4] >> 4;
493 /* Extention start code */
496 static const int mpeg2_aspect[16][2] =
498 {0,1}, {1,1}, {4,3}, {16,9}, {221,100},
499 {0,1}, {0,1}, {0,1}, {0,1}, {0,1}, {0,1}, {0,1}, {0,1}, {0,1},
503 /* sequence extention */
504 if( p_sys->p_ext) block_Release( p_sys->p_ext );
505 p_sys->p_ext = block_Duplicate( p_frag );
507 if( p_frag->i_buffer >= 10 )
509 p_sys->b_seq_progressive =
510 p_frag->p_buffer[5]&0x08 ? VLC_TRUE : VLC_FALSE;
512 p_frag->p_buffer[9]&0x80 ? VLC_TRUE : VLC_FALSE;
515 p_dec->fmt_out.video.i_aspect =
516 mpeg2_aspect[p_sys->i_aspect_ratio_info][0] *
518 mpeg2_aspect[p_sys->i_aspect_ratio_info][1];
521 else if( i_type == 0x08 )
523 /* picture extention */
524 p_sys->i_picture_structure = p_frag->p_buffer[6]&0x03;
525 p_sys->i_top_field_first = p_frag->p_buffer[7] >> 7;
526 p_sys->i_repeat_first_field= (p_frag->p_buffer[7]>>1)&0x01;
527 p_sys->i_progressive_frame = p_frag->p_buffer[8] >> 7;
530 else if( p_frag->p_buffer[3] == 0x00 )
532 /* Picture start code */
535 if( p_frag->i_buffer >= 6 )
537 p_sys->i_temporal_ref =
538 ( p_frag->p_buffer[4] << 2 )|(p_frag->p_buffer[5] >> 6);
539 p_sys->i_picture_type = ( p_frag->p_buffer[5] >> 3 ) & 0x03;
542 p_sys->i_dts = p_frag->i_dts;
543 p_sys->i_pts = p_frag->i_pts;
545 else if( p_frag->p_buffer[3] >= 0x01 && p_frag->p_buffer[3] <= 0xaf )
547 /* Slice start code */
548 p_sys->b_frame_slice = VLC_TRUE;
551 /* Append the block */
552 block_ChainAppend( &p_sys->p_frame, p_frag );