1 /*****************************************************************************
2 * mpegvideo.c: parse and packetize an MPEG1/2 video stream
3 *****************************************************************************
4 * Copyright (C) 2001-2006 the VideoLAN team
7 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8 * Eric Petit <titer@videolan.org>
9 * Gildas Bazin <gbazin@videolan.org>
10 * Jean-Paul Saman <jpsaman #_at_# m2x dot nl>
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
25 *****************************************************************************/
27 /*****************************************************************************
28 * Problem with this implementation:
30 * Although we should time-stamp each picture with a PTS, this isn't possible
31 * with the current implementation.
32 * The problem comes from the fact that for non-low-delay streams we can't
33 * calculate the PTS of pictures used as backward reference. Even the temporal
34 * reference number doesn't help here because all the pictures don't
35 * necessarily have the same duration (eg. 3:2 pulldown).
37 * However this doesn't really matter as far as the MPEG muxers are concerned
38 * because they allow having empty PTS fields. --gibalou
39 *****************************************************************************/
41 /*****************************************************************************
43 *****************************************************************************/
44 #include <stdlib.h> /* malloc(), free() */
47 #include <vlc_block.h>
48 #include <vlc_codec.h>
49 #include <vlc_block_helper.h>
51 #define SYNC_INTRAFRAME_TEXT N_("Sync on Intra Frame")
52 #define SYNC_INTRAFRAME_LONGTEXT N_("Normally the packetizer would " \
53 "sync on the next full frame. This flags instructs the packetizer " \
54 "to sync on the first Intra Frame found.")
56 /*****************************************************************************
58 *****************************************************************************/
59 static int Open ( vlc_object_t * );
60 static void Close( vlc_object_t * );
63 set_category( CAT_SOUT );
64 set_subcategory( SUBCAT_SOUT_PACKETIZER );
65 set_description( _("MPEG-I/II video packetizer") );
66 set_capability( "packetizer", 50 );
67 set_callbacks( Open, Close );
69 add_bool( "packetizer-mpegvideo-sync-iframe", 0, NULL, SYNC_INTRAFRAME_TEXT,
70 SYNC_INTRAFRAME_LONGTEXT, VLC_TRUE );
73 /*****************************************************************************
75 *****************************************************************************/
76 static block_t *Packetize( decoder_t *, block_t ** );
77 static block_t *ParseMPEGBlock( decoder_t *, block_t * );
84 block_bytestream_t bytestream;
87 uint8_t p_startcode[3];
89 /* Sequence header and extension */
93 /* Current frame being built */
97 vlc_bool_t b_frame_slice;
101 /* Sequence properties */
103 int i_frame_rate_base;
104 vlc_bool_t b_seq_progressive;
105 vlc_bool_t b_low_delay;
106 int i_aspect_ratio_info;
109 /* Picture properties */
112 int i_picture_structure;
113 int i_top_field_first;
114 int i_repeat_first_field;
115 int i_progressive_frame;
117 mtime_t i_interpolated_dts;
118 mtime_t i_old_duration;
119 mtime_t i_last_ref_pts;
120 vlc_bool_t b_second_field;
122 /* Number of pictures since last sequence header */
126 vlc_bool_t b_sync_on_intra_frame;
127 vlc_bool_t b_discontinuity;
135 /*****************************************************************************
137 *****************************************************************************/
138 static int Open( vlc_object_t *p_this )
140 decoder_t *p_dec = (decoder_t*)p_this;
141 decoder_sys_t *p_sys;
143 if( p_dec->fmt_in.i_codec != VLC_FOURCC( 'm', 'p', 'g', '1' ) &&
144 p_dec->fmt_in.i_codec != VLC_FOURCC( 'm', 'p', 'g', '2' ) &&
145 p_dec->fmt_in.i_codec != VLC_FOURCC( 'm', 'p', 'g', 'v' ) )
150 es_format_Init( &p_dec->fmt_out, VIDEO_ES, VLC_FOURCC('m','p','g','v') );
151 p_dec->pf_packetize = Packetize;
153 p_dec->p_sys = p_sys = malloc( sizeof( decoder_sys_t ) );
156 p_sys->i_state = STATE_NOSYNC;
157 p_sys->bytestream = block_BytestreamInit( p_dec );
158 p_sys->p_startcode[0] = 0;
159 p_sys->p_startcode[1] = 0;
160 p_sys->p_startcode[2] = 1;
165 p_sys->p_frame = NULL;
166 p_sys->pp_last = &p_sys->p_frame;
167 p_sys->b_frame_slice = VLC_FALSE;
169 p_sys->i_dts = p_sys->i_pts = 0;
171 p_sys->i_frame_rate = 1;
172 p_sys->i_frame_rate_base = 1;
173 p_sys->b_seq_progressive = VLC_TRUE;
174 p_sys->b_low_delay = VLC_TRUE;
175 p_sys->i_seq_old = 0;
177 p_sys->i_temporal_ref = 0;
178 p_sys->i_picture_type = 0;
179 p_sys->i_picture_structure = 0x03; /* frame */
180 p_sys->i_top_field_first = 0;
181 p_sys->i_repeat_first_field = 0;
182 p_sys->i_progressive_frame = 0;
185 p_sys->i_interpolated_dts = 0;
186 p_sys->i_old_duration = 0;
187 p_sys->i_last_ref_pts = 0;
188 p_sys->b_second_field = 0;
190 p_sys->b_discontinuity = VLC_FALSE;
191 p_sys->b_sync_on_intra_frame = var_CreateGetBool( p_dec, "packetizer-mpegvideo-sync-iframe" );
192 if( p_sys->b_sync_on_intra_frame )
193 msg_Dbg( p_dec, "syncing on intra frame now" );
198 /*****************************************************************************
200 *****************************************************************************/
201 static void Close( vlc_object_t *p_this )
203 decoder_t *p_dec = (decoder_t*)p_this;
204 decoder_sys_t *p_sys = p_dec->p_sys;
206 block_BytestreamRelease( &p_sys->bytestream );
210 block_Release( p_sys->p_seq );
214 block_Release( p_sys->p_ext );
218 block_ChainRelease( p_sys->p_frame );
221 var_Destroy( p_dec, "packetizer-mpegvideo-sync-iframe" );
226 /*****************************************************************************
228 *****************************************************************************/
229 static block_t *Packetize( decoder_t *p_dec, block_t **pp_block )
231 decoder_sys_t *p_sys = p_dec->p_sys;
234 if( pp_block == NULL || *pp_block == NULL )
239 if( (*pp_block)->i_flags & (BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED) )
241 p_sys->i_state = STATE_NOSYNC;
242 p_sys->b_discontinuity = VLC_TRUE;
244 block_ChainRelease( p_sys->p_frame );
245 p_sys->p_frame = NULL;
246 p_sys->pp_last = &p_sys->p_frame;
247 p_sys->b_frame_slice = VLC_FALSE;
248 block_Release( *pp_block );
252 block_BytestreamPush( &p_sys->bytestream, *pp_block );
256 switch( p_sys->i_state )
260 if( block_FindStartcodeFromOffset( &p_sys->bytestream,
261 &p_sys->i_offset, p_sys->p_startcode, 3 ) == VLC_SUCCESS )
263 p_sys->i_state = STATE_NEXT_SYNC;
266 if( p_sys->i_offset )
268 block_SkipBytes( &p_sys->bytestream, p_sys->i_offset );
270 block_BytestreamFlush( &p_sys->bytestream );
273 if( p_sys->i_state != STATE_NEXT_SYNC )
279 p_sys->i_offset = 1; /* To find next startcode */
281 case STATE_NEXT_SYNC:
282 /* TODO: If p_block == NULL, flush the buffer without checking the
285 /* Find the next startcode */
286 if( block_FindStartcodeFromOffset( &p_sys->bytestream,
287 &p_sys->i_offset, p_sys->p_startcode, 3 ) != VLC_SUCCESS )
293 /* Get the new fragment and set the pts/dts */
294 p_pic = block_New( p_dec, p_sys->i_offset );
295 block_BytestreamFlush( &p_sys->bytestream );
296 p_pic->i_pts = p_sys->bytestream.p_block->i_pts;
297 p_pic->i_dts = p_sys->bytestream.p_block->i_dts;
299 block_GetBytes( &p_sys->bytestream, p_pic->p_buffer,
302 /* don't reuse the same timestamps several times */
303 if( p_pic->i_buffer >= 4 && p_pic->p_buffer[3] == 0x00 )
305 /* We have a picture start code */
306 p_sys->bytestream.p_block->i_pts = 0;
307 p_sys->bytestream.p_block->i_dts = 0;
312 /* Get picture if any */
313 if( !( p_pic = ParseMPEGBlock( p_dec, p_pic ) ) )
315 p_sys->i_state = STATE_NOSYNC;
319 /* If a discontinuity has been encountered, then wait till
320 * the next Intra frame before continuing with packetizing */
321 if( p_sys->b_discontinuity &&
322 p_sys->b_sync_on_intra_frame )
324 if( p_pic->i_flags & BLOCK_FLAG_TYPE_I )
326 msg_Dbg( p_dec, "synced on intra frame" );
327 p_sys->b_discontinuity = VLC_FALSE;
328 p_pic->i_flags |= BLOCK_FLAG_DISCONTINUITY;
332 msg_Dbg( p_dec, "waiting on intra frame" );
333 p_sys->i_state = STATE_NOSYNC;
334 block_Release( p_pic );
339 /* We've just started the stream, wait for the first PTS.
340 * We discard here so we can still get the sequence header. */
341 if( p_sys->i_dts <= 0 && p_sys->i_pts <= 0 &&
342 p_sys->i_interpolated_dts <= 0 )
344 msg_Dbg( p_dec, "need a starting pts/dts" );
345 p_sys->i_state = STATE_NOSYNC;
346 block_Release( p_pic );
350 /* When starting the stream we can have the first frame with
351 * a null DTS (i_interpolated_pts is initialized to 0) */
352 if( !p_pic->i_dts ) p_pic->i_dts = p_pic->i_pts;
354 /* So p_block doesn't get re-added several times */
355 *pp_block = block_BytestreamPop( &p_sys->bytestream );
357 p_sys->i_state = STATE_NOSYNC;
364 /*****************************************************************************
365 * ParseMPEGBlock: Re-assemble fragments into a block containing a picture
366 *****************************************************************************/
367 static block_t *ParseMPEGBlock( decoder_t *p_dec, block_t *p_frag )
369 decoder_sys_t *p_sys = p_dec->p_sys;
370 block_t *p_pic = NULL;
373 * Check if previous picture is finished
375 if( ( p_sys->b_frame_slice &&
376 (p_frag->p_buffer[3] == 0x00 || p_frag->p_buffer[3] > 0xaf) ) &&
377 p_sys->p_seq == NULL )
379 /* We have a picture but without a sequence header we can't
381 msg_Dbg( p_dec, "waiting for sequence start" );
382 if( p_sys->p_frame ) block_ChainRelease( p_sys->p_frame );
383 p_sys->p_frame = NULL;
384 p_sys->pp_last = &p_sys->p_frame;
385 p_sys->b_frame_slice = VLC_FALSE;
388 else if( p_sys->b_frame_slice &&
389 (p_frag->p_buffer[3] == 0x00 || p_frag->p_buffer[3] > 0xaf) )
393 p_pic = block_ChainGather( p_sys->p_frame );
395 i_duration = (mtime_t)( 1000000 * p_sys->i_frame_rate_base /
396 p_sys->i_frame_rate );
398 if( !p_sys->b_seq_progressive && p_sys->i_picture_structure != 0x03 )
403 if( p_sys->b_seq_progressive )
405 if( p_sys->i_top_field_first == 0 &&
406 p_sys->i_repeat_first_field == 1 )
410 else if( p_sys->i_top_field_first == 1 &&
411 p_sys->i_repeat_first_field == 1 )
418 if( p_sys->i_picture_structure == 0x03 )
420 if( p_sys->i_progressive_frame && p_sys->i_repeat_first_field )
422 i_duration += i_duration / 2;
427 if( p_sys->b_low_delay || p_sys->i_picture_type == 0x03 )
429 /* Trivial case (DTS == PTS) */
430 /* Correct interpolated dts when we receive a new pts/dts */
431 if( p_sys->i_pts > 0 ) p_sys->i_interpolated_dts = p_sys->i_pts;
432 if( p_sys->i_dts > 0 ) p_sys->i_interpolated_dts = p_sys->i_dts;
436 /* Correct interpolated dts when we receive a new pts/dts */
437 if( p_sys->i_last_ref_pts > 0 && !p_sys->b_second_field )
438 p_sys->i_interpolated_dts = p_sys->i_last_ref_pts;
439 if( p_sys->i_dts > 0 ) p_sys->i_interpolated_dts = p_sys->i_dts;
441 if( !p_sys->b_second_field )
442 p_sys->i_last_ref_pts = p_sys->i_pts;
445 p_pic->i_dts = p_sys->i_interpolated_dts;
446 p_sys->i_interpolated_dts += i_duration;
448 /* Set PTS only if we have a B frame or if it comes from the stream */
449 if( p_sys->i_pts > 0 )
451 p_pic->i_pts = p_sys->i_pts;
453 else if( p_sys->i_picture_type == 0x03 )
455 p_pic->i_pts = p_pic->i_dts;
462 switch ( p_sys->i_picture_type )
465 p_pic->i_flags |= BLOCK_FLAG_TYPE_I;
468 p_pic->i_flags |= BLOCK_FLAG_TYPE_P;
471 p_pic->i_flags |= BLOCK_FLAG_TYPE_B;
475 p_pic->i_length = p_sys->i_interpolated_dts - p_pic->i_dts;
478 msg_Dbg( p_dec, "pic: type=%d dts="I64Fd" pts-dts="I64Fd,
479 p_sys->i_picture_type, p_pic->i_dts, p_pic->i_pts - p_pic->i_dts);
483 p_sys->p_frame = NULL;
484 p_sys->pp_last = &p_sys->p_frame;
485 p_sys->b_frame_slice = VLC_FALSE;
487 if( p_sys->i_picture_structure != 0x03 )
489 p_sys->b_second_field = !p_sys->b_second_field;
493 p_sys->b_second_field = 0;
498 * Check info of current fragment
500 if( p_frag->p_buffer[3] == 0xb8 )
502 /* Group start code */
504 p_sys->i_seq_old > p_sys->i_frame_rate/p_sys->i_frame_rate_base )
506 /* Usefull for mpeg1: repeat sequence header every second */
507 block_ChainLastAppend( &p_sys->pp_last, block_Duplicate( p_sys->p_seq ) );
510 block_ChainLastAppend( &p_sys->pp_last, block_Duplicate( p_sys->p_ext ) );
513 p_sys->i_seq_old = 0;
516 else if( p_frag->p_buffer[3] == 0xb3 && p_frag->i_buffer >= 8 )
518 /* Sequence header code */
519 static const int code_to_frame_rate[16][2] =
521 { 1, 1 }, /* invalid */
522 { 24000, 1001 }, { 24, 1 }, { 25, 1 }, { 30000, 1001 },
523 { 30, 1 }, { 50, 1 }, { 60000, 1001 }, { 60, 1 },
524 /* Unofficial 15fps from Xing*/
526 /* Unofficial economy rates from libmpeg3 */
527 { 5, 1001 }, { 10, 1001 }, { 12, 1001 }, { 15, 1001 },
528 { 1, 1 }, { 1, 1 } /* invalid */
531 if( p_sys->p_seq ) block_Release( p_sys->p_seq );
532 if( p_sys->p_ext ) block_Release( p_sys->p_ext );
534 p_sys->p_seq = block_Duplicate( p_frag );
535 p_sys->i_seq_old = 0;
538 p_dec->fmt_out.video.i_width =
539 ( p_frag->p_buffer[4] << 4)|(p_frag->p_buffer[5] >> 4 );
540 p_dec->fmt_out.video.i_height =
541 ( (p_frag->p_buffer[5]&0x0f) << 8 )|p_frag->p_buffer[6];
542 p_sys->i_aspect_ratio_info = p_frag->p_buffer[7] >> 4;
544 /* TODO: MPEG1 aspect ratio */
546 p_sys->i_frame_rate = code_to_frame_rate[p_frag->p_buffer[7]&0x0f][0];
547 p_sys->i_frame_rate_base =
548 code_to_frame_rate[p_frag->p_buffer[7]&0x0f][1];
550 p_dec->fmt_out.video.i_frame_rate = p_sys->i_frame_rate;
551 p_dec->fmt_out.video.i_frame_rate_base = p_sys->i_frame_rate_base;
553 p_sys->b_seq_progressive = VLC_TRUE;
554 p_sys->b_low_delay = VLC_TRUE;
556 if ( !p_sys->b_inited )
558 msg_Dbg( p_dec, "size %dx%d fps=%.3f",
559 p_dec->fmt_out.video.i_width, p_dec->fmt_out.video.i_height,
560 p_sys->i_frame_rate / (float)p_sys->i_frame_rate_base );
564 else if( p_frag->p_buffer[3] == 0xb5 )
566 int i_type = p_frag->p_buffer[4] >> 4;
568 /* Extension start code */
572 static const int mpeg2_aspect[16][2] =
574 {0,1}, {1,1}, {4,3}, {16,9}, {221,100},
575 {0,1}, {0,1}, {0,1}, {0,1}, {0,1}, {0,1}, {0,1}, {0,1}, {0,1},
580 /* sequence extension */
581 if( p_sys->p_ext) block_Release( p_sys->p_ext );
582 p_sys->p_ext = block_Duplicate( p_frag );
584 if( p_frag->i_buffer >= 10 )
586 p_sys->b_seq_progressive =
587 p_frag->p_buffer[5]&0x08 ? VLC_TRUE : VLC_FALSE;
589 p_frag->p_buffer[9]&0x80 ? VLC_TRUE : VLC_FALSE;
592 /* Do not set aspect ratio : in case we're transcoding,
593 * transcode will take our fmt_out as a fmt_in to libmpeg2.
594 * libmpeg2.c will then believe that the user has requested
595 * a specific aspect ratio, which she hasn't. Thus in case
596 * of aspect ratio change, we're screwed. --Meuuh
599 p_dec->fmt_out.video.i_aspect =
600 mpeg2_aspect[p_sys->i_aspect_ratio_info][0] *
602 mpeg2_aspect[p_sys->i_aspect_ratio_info][1];
606 else if( i_type == 0x08 )
608 /* picture extension */
609 p_sys->i_picture_structure = p_frag->p_buffer[6]&0x03;
610 p_sys->i_top_field_first = p_frag->p_buffer[7] >> 7;
611 p_sys->i_repeat_first_field= (p_frag->p_buffer[7]>>1)&0x01;
612 p_sys->i_progressive_frame = p_frag->p_buffer[8] >> 7;
615 else if( p_frag->p_buffer[3] == 0x00 )
617 /* Picture start code */
620 if( p_frag->i_buffer >= 6 )
622 p_sys->i_temporal_ref =
623 ( p_frag->p_buffer[4] << 2 )|(p_frag->p_buffer[5] >> 6);
624 p_sys->i_picture_type = ( p_frag->p_buffer[5] >> 3 ) & 0x03;
627 p_sys->i_dts = p_frag->i_dts;
628 p_sys->i_pts = p_frag->i_pts;
630 else if( p_frag->p_buffer[3] >= 0x01 && p_frag->p_buffer[3] <= 0xaf )
632 /* Slice start code */
633 p_sys->b_frame_slice = VLC_TRUE;
636 /* Append the block */
637 block_ChainLastAppend( &p_sys->pp_last, p_frag );