1 /*****************************************************************************
2 * mpegvideo.c: parse and packetize an MPEG1/2 video stream
3 *****************************************************************************
4 * Copyright (C) 2001, 2002 VideoLAN
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 extension */
83 /* Current frame being built */
87 vlc_bool_t b_frame_slice;
91 /* Sequence properties */
93 int i_frame_rate_base;
94 vlc_bool_t b_seq_progressive;
95 vlc_bool_t b_low_delay;
96 int i_aspect_ratio_info;
99 /* Picture properties */
102 int i_picture_structure;
103 int i_top_field_first;
104 int i_repeat_first_field;
105 int i_progressive_frame;
107 mtime_t i_interpolated_dts;
108 mtime_t i_old_duration;
109 mtime_t i_last_ref_pts;
111 /* Number of pictures since last sequence header */
121 /*****************************************************************************
123 *****************************************************************************/
124 static int Open( vlc_object_t *p_this )
126 decoder_t *p_dec = (decoder_t*)p_this;
127 decoder_sys_t *p_sys;
129 if( p_dec->fmt_in.i_codec != VLC_FOURCC( 'm', 'p', 'g', '1' ) &&
130 p_dec->fmt_in.i_codec != VLC_FOURCC( 'm', 'p', 'g', '2' ) &&
131 p_dec->fmt_in.i_codec != VLC_FOURCC( 'm', 'p', 'g', 'v' ) )
136 es_format_Init( &p_dec->fmt_out, VIDEO_ES, VLC_FOURCC('m','p','g','v') );
137 p_dec->pf_packetize = Packetize;
139 p_dec->p_sys = p_sys = malloc( sizeof( decoder_sys_t ) );
142 p_sys->i_state = STATE_NOSYNC;
143 p_sys->bytestream = block_BytestreamInit( p_dec );
144 p_sys->p_startcode[0] = 0;
145 p_sys->p_startcode[1] = 0;
146 p_sys->p_startcode[2] = 1;
151 p_sys->p_frame = NULL;
152 p_sys->pp_last = &p_sys->p_frame;
153 p_sys->b_frame_slice = VLC_FALSE;
155 p_sys->i_dts = p_sys->i_pts = 0;
157 p_sys->i_frame_rate = 1;
158 p_sys->i_frame_rate_base = 1;
159 p_sys->b_seq_progressive = VLC_TRUE;
160 p_sys->b_low_delay = VLC_TRUE;
161 p_sys->i_seq_old = 0;
163 p_sys->i_temporal_ref = 0;
164 p_sys->i_picture_type = 0;
165 p_sys->i_picture_structure = 0x03; /* frame */
166 p_sys->i_top_field_first = 0;
167 p_sys->i_repeat_first_field = 0;
168 p_sys->i_progressive_frame = 0;
171 p_sys->i_interpolated_dts = 0;
172 p_sys->i_old_duration = 0;
173 p_sys->i_last_ref_pts = 0;
178 /*****************************************************************************
180 *****************************************************************************/
181 static void Close( vlc_object_t *p_this )
183 decoder_t *p_dec = (decoder_t*)p_this;
184 decoder_sys_t *p_sys = p_dec->p_sys;
186 block_BytestreamRelease( &p_sys->bytestream );
190 block_Release( p_sys->p_seq );
194 block_Release( p_sys->p_ext );
198 block_ChainRelease( p_sys->p_frame );
204 /*****************************************************************************
206 *****************************************************************************/
207 static block_t *Packetize( decoder_t *p_dec, block_t **pp_block )
209 decoder_sys_t *p_sys = p_dec->p_sys;
212 if( pp_block == NULL || *pp_block == NULL )
217 if( (*pp_block)->i_flags & BLOCK_FLAG_DISCONTINUITY )
219 p_sys->i_state = STATE_NOSYNC;
220 if( p_sys->p_frame ) block_ChainRelease( p_sys->p_frame );
221 p_sys->p_frame = NULL;
222 p_sys->pp_last = &p_sys->p_frame;
223 p_sys->b_frame_slice = VLC_FALSE;
224 block_Release( *pp_block );
228 block_BytestreamPush( &p_sys->bytestream, *pp_block );
232 switch( p_sys->i_state )
236 if( block_FindStartcodeFromOffset( &p_sys->bytestream,
237 &p_sys->i_offset, p_sys->p_startcode, 3 ) == VLC_SUCCESS )
239 p_sys->i_state = STATE_NEXT_SYNC;
242 if( p_sys->i_offset )
244 block_SkipBytes( &p_sys->bytestream, p_sys->i_offset );
246 block_BytestreamFlush( &p_sys->bytestream );
249 if( p_sys->i_state != STATE_NEXT_SYNC )
255 p_sys->i_offset = 1; /* To find next startcode */
257 case STATE_NEXT_SYNC:
258 /* TODO: If p_block == NULL, flush the buffer without checking the
261 /* Find the next startcode */
262 if( block_FindStartcodeFromOffset( &p_sys->bytestream,
263 &p_sys->i_offset, p_sys->p_startcode, 3 ) != VLC_SUCCESS )
269 /* Get the new fragment and set the pts/dts */
270 p_pic = block_New( p_dec, p_sys->i_offset );
271 p_pic->i_pts = p_sys->bytestream.p_block->i_pts;
272 p_pic->i_dts = p_sys->bytestream.p_block->i_dts;
274 block_GetBytes( &p_sys->bytestream, p_pic->p_buffer,
277 /* don't reuse the same timestamps several times */
278 if( p_pic->i_buffer >= 4 && p_pic->p_buffer[3] == 0x00 )
280 /* We have a picture start code */
281 p_sys->bytestream.p_block->i_pts = 0;
282 p_sys->bytestream.p_block->i_dts = 0;
287 /* Get picture if any */
288 if( !( p_pic = ParseMPEGBlock( p_dec, p_pic ) ) )
290 p_sys->i_state = STATE_NOSYNC;
294 /* We've just started the stream, wait for the first PTS.
295 * We discard here so we can still get the sequence header. */
296 if( p_sys->i_dts <= 0 && p_sys->i_pts <= 0 &&
297 p_sys->i_interpolated_dts <= 0 )
299 msg_Dbg( p_dec, "need a starting pts/dts" );
300 p_sys->i_state = STATE_NOSYNC;
301 block_Release( p_pic );
305 /* When starting the stream we can have the first frame with
306 * a null DTS (i_interpolated_pts is initialized to 0) */
307 if( !p_pic->i_dts ) p_pic->i_dts = p_pic->i_pts;
309 /* So p_block doesn't get re-added several times */
310 *pp_block = block_BytestreamPop( &p_sys->bytestream );
312 p_sys->i_state = STATE_NOSYNC;
319 /*****************************************************************************
320 * ParseMPEGBlock: Re-assemble fragments into a block containing a picture
321 *****************************************************************************/
322 static block_t *ParseMPEGBlock( decoder_t *p_dec, block_t *p_frag )
324 decoder_sys_t *p_sys = p_dec->p_sys;
325 block_t *p_pic = NULL;
328 * Check if previous picture is finished
330 if( ( p_sys->b_frame_slice &&
331 (p_frag->p_buffer[3] == 0x00 || p_frag->p_buffer[3] > 0xaf) ) &&
332 p_sys->p_seq == NULL )
334 /* We have a picture but without a sequence header we can't
336 msg_Dbg( p_dec, "waiting for sequence start" );
337 if( p_sys->p_frame ) block_ChainRelease( p_sys->p_frame );
338 p_sys->p_frame = NULL;
339 p_sys->pp_last = &p_sys->p_frame;
340 p_sys->b_frame_slice = VLC_FALSE;
343 else if( p_sys->b_frame_slice &&
344 (p_frag->p_buffer[3] == 0x00 || p_frag->p_buffer[3] > 0xaf) )
348 p_pic = block_ChainGather( p_sys->p_frame );
350 i_duration = (mtime_t)( 1000000 * p_sys->i_frame_rate_base /
351 p_sys->i_frame_rate );
353 if( !p_sys->b_seq_progressive && p_sys->i_picture_structure != 0x03 )
358 if( p_sys->b_seq_progressive )
360 if( p_sys->i_top_field_first == 0 &&
361 p_sys->i_repeat_first_field == 1 )
365 else if( p_sys->i_top_field_first == 1 &&
366 p_sys->i_repeat_first_field == 1 )
373 if( p_sys->i_picture_structure == 0x03 )
375 if( p_sys->i_progressive_frame && p_sys->i_repeat_first_field )
377 i_duration += i_duration / 2;
382 if( p_sys->b_low_delay || p_sys->i_picture_type == 0x03 )
384 /* Trivial case (DTS == PTS) */
385 /* Correct interpolated dts when we receive a new pts/dts */
386 if( p_sys->i_pts > 0 ) p_sys->i_interpolated_dts = p_sys->i_pts;
387 if( p_sys->i_dts > 0 ) p_sys->i_interpolated_dts = p_sys->i_dts;
391 /* Correct interpolated dts when we receive a new pts/dts */
392 if( p_sys->i_last_ref_pts > 0 )
393 p_sys->i_interpolated_dts = p_sys->i_last_ref_pts;
394 if( p_sys->i_dts > 0 ) p_sys->i_interpolated_dts = p_sys->i_dts;
396 p_sys->i_last_ref_pts = p_sys->i_pts;
399 p_pic->i_dts = p_sys->i_interpolated_dts;
401 /* Set PTS only if we have a B frame or if it comes from the stream */
402 if( p_sys->i_pts > 0 )
404 p_pic->i_pts = p_sys->i_pts;
406 else if( p_sys->i_picture_type == 0x03 )
408 p_pic->i_pts = p_pic->i_dts;
415 if( p_sys->b_low_delay || p_sys->i_picture_type == 0x03 )
417 /* Trivial case (DTS == PTS) */
418 p_sys->i_interpolated_dts += i_duration;
422 p_sys->i_interpolated_dts += p_sys->i_old_duration;
423 p_sys->i_old_duration = i_duration;
426 switch ( p_sys->i_picture_type )
429 p_pic->i_flags |= BLOCK_FLAG_TYPE_I;
432 p_pic->i_flags |= BLOCK_FLAG_TYPE_P;
435 p_pic->i_flags |= BLOCK_FLAG_TYPE_B;
439 p_pic->i_length = p_sys->i_interpolated_dts - p_pic->i_dts;
442 msg_Dbg( p_dec, "pic: type=%d dts="I64Fd" pts-dts="I64Fd,
443 p_sys->i_picture_type, p_pic->i_dts, p_pic->i_pts - p_pic->i_dts);
447 p_sys->p_frame = NULL;
448 p_sys->pp_last = &p_sys->p_frame;
449 p_sys->b_frame_slice = VLC_FALSE;
453 * Check info of current fragment
455 if( p_frag->p_buffer[3] == 0xb8 )
457 /* Group start code */
459 p_sys->i_seq_old > p_sys->i_frame_rate/p_sys->i_frame_rate_base )
461 /* Usefull for mpeg1: repeat sequence header every second */
462 block_ChainLastAppend( &p_sys->pp_last, block_Duplicate( p_sys->p_seq ) );
465 block_ChainLastAppend( &p_sys->pp_last, block_Duplicate( p_sys->p_ext ) );
468 p_sys->i_seq_old = 0;
471 else if( p_frag->p_buffer[3] == 0xb3 && p_frag->i_buffer >= 8 )
473 /* Sequence header code */
474 static const int code_to_frame_rate[16][2] =
476 { 1, 1 }, /* invalid */
477 { 24000, 1001 }, { 24, 1 }, { 25, 1 }, { 30000, 1001 },
478 { 30, 1 }, { 50, 1 }, { 60000, 1001 }, { 60, 1 },
479 /* Unofficial 15fps from Xing*/
481 /* Unofficial economy rates from libmpeg3 */
482 { 5, 1001 }, { 10, 1001 }, { 12, 1001 }, { 15, 1001 },
483 { 1, 1 }, { 1, 1 } /* invalid */
486 if( p_sys->p_seq ) block_Release( p_sys->p_seq );
487 if( p_sys->p_ext ) block_Release( p_sys->p_ext );
489 p_sys->p_seq = block_Duplicate( p_frag );
490 p_sys->i_seq_old = 0;
493 p_dec->fmt_out.video.i_width =
494 ( p_frag->p_buffer[4] << 4)|(p_frag->p_buffer[5] >> 4 );
495 p_dec->fmt_out.video.i_height =
496 ( (p_frag->p_buffer[5]&0x0f) << 8 )|p_frag->p_buffer[6];
497 p_sys->i_aspect_ratio_info = p_frag->p_buffer[7] >> 4;
499 /* TODO: MPEG1 aspect ratio */
501 p_sys->i_frame_rate = code_to_frame_rate[p_frag->p_buffer[7]&0x0f][0];
502 p_sys->i_frame_rate_base =
503 code_to_frame_rate[p_frag->p_buffer[7]&0x0f][1];
505 p_sys->b_seq_progressive = VLC_TRUE;
506 p_sys->b_low_delay = VLC_TRUE;
508 if ( !p_sys->b_inited )
510 msg_Dbg( p_dec, "Size %dx%d fps=%.3f",
511 p_dec->fmt_out.video.i_width, p_dec->fmt_out.video.i_height,
512 p_sys->i_frame_rate / (float)p_sys->i_frame_rate_base );
516 else if( p_frag->p_buffer[3] == 0xb5 )
518 int i_type = p_frag->p_buffer[4] >> 4;
520 /* Extention start code */
523 static const int mpeg2_aspect[16][2] =
525 {0,1}, {1,1}, {4,3}, {16,9}, {221,100},
526 {0,1}, {0,1}, {0,1}, {0,1}, {0,1}, {0,1}, {0,1}, {0,1}, {0,1},
530 /* sequence extention */
531 if( p_sys->p_ext) block_Release( p_sys->p_ext );
532 p_sys->p_ext = block_Duplicate( p_frag );
534 if( p_frag->i_buffer >= 10 )
536 p_sys->b_seq_progressive =
537 p_frag->p_buffer[5]&0x08 ? VLC_TRUE : VLC_FALSE;
539 p_frag->p_buffer[9]&0x80 ? VLC_TRUE : VLC_FALSE;
542 p_dec->fmt_out.video.i_aspect =
543 mpeg2_aspect[p_sys->i_aspect_ratio_info][0] *
545 mpeg2_aspect[p_sys->i_aspect_ratio_info][1];
548 else if( i_type == 0x08 )
550 /* picture extention */
551 p_sys->i_picture_structure = p_frag->p_buffer[6]&0x03;
552 p_sys->i_top_field_first = p_frag->p_buffer[7] >> 7;
553 p_sys->i_repeat_first_field= (p_frag->p_buffer[7]>>1)&0x01;
554 p_sys->i_progressive_frame = p_frag->p_buffer[8] >> 7;
557 else if( p_frag->p_buffer[3] == 0x00 )
559 /* Picture start code */
562 if( p_frag->i_buffer >= 6 )
564 p_sys->i_temporal_ref =
565 ( p_frag->p_buffer[4] << 2 )|(p_frag->p_buffer[5] >> 6);
566 p_sys->i_picture_type = ( p_frag->p_buffer[5] >> 3 ) & 0x03;
569 p_sys->i_dts = p_frag->i_dts;
570 p_sys->i_pts = p_frag->i_pts;
572 else if( p_frag->p_buffer[3] >= 0x01 && p_frag->p_buffer[3] <= 0xaf )
574 /* Slice start code */
575 p_sys->b_frame_slice = VLC_TRUE;
578 /* Append the block */
579 block_ChainLastAppend( &p_sys->pp_last, p_frag );