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 *****************************************************************************/
49 #include <vlc_common.h>
50 #include <vlc_plugin.h>
51 #include <vlc_block.h>
52 #include <vlc_codec.h>
53 #include <vlc_block_helper.h>
54 #include "../codec/cc.h"
56 #define SYNC_INTRAFRAME_TEXT N_("Sync on Intra Frame")
57 #define SYNC_INTRAFRAME_LONGTEXT N_("Normally the packetizer would " \
58 "sync on the next full frame. This flags instructs the packetizer " \
59 "to sync on the first Intra Frame found.")
61 /*****************************************************************************
63 *****************************************************************************/
64 static int Open ( vlc_object_t * );
65 static void Close( vlc_object_t * );
68 set_category( CAT_SOUT )
69 set_subcategory( SUBCAT_SOUT_PACKETIZER )
70 set_description( N_("MPEG-I/II video packetizer") )
71 set_shortname( N_("MPEG Video") )
72 set_capability( "packetizer", 50 )
73 set_callbacks( Open, Close )
75 add_bool( "packetizer-mpegvideo-sync-iframe", 0, NULL, SYNC_INTRAFRAME_TEXT,
76 SYNC_INTRAFRAME_LONGTEXT, true )
79 /*****************************************************************************
81 *****************************************************************************/
82 static block_t *Packetize( decoder_t *, block_t ** );
83 static block_t *ParseMPEGBlock( decoder_t *, block_t * );
84 static block_t *GetCc( decoder_t *p_dec, bool pb_present[4] );
91 block_bytestream_t bytestream;
94 uint8_t p_startcode[3];
96 /* Sequence header and extension */
100 /* Current frame being built */
108 /* Sequence properties */
110 int i_frame_rate_base;
111 bool b_seq_progressive;
113 int i_aspect_ratio_info;
116 /* Picture properties */
119 int i_picture_structure;
120 int i_top_field_first;
121 int i_repeat_first_field;
122 int i_progressive_frame;
124 mtime_t i_interpolated_dts;
125 mtime_t i_last_ref_pts;
128 /* Number of pictures since last sequence header */
132 bool b_sync_on_intra_frame;
133 bool b_discontinuity;
148 /*****************************************************************************
150 *****************************************************************************/
151 static int Open( vlc_object_t *p_this )
153 decoder_t *p_dec = (decoder_t*)p_this;
154 decoder_sys_t *p_sys;
156 if( p_dec->fmt_in.i_codec != VLC_FOURCC( 'm', 'p', 'g', '1' ) &&
157 p_dec->fmt_in.i_codec != VLC_FOURCC( 'm', 'p', 'g', '2' ) &&
158 p_dec->fmt_in.i_codec != VLC_FOURCC( 'm', 'p', 'g', 'v' ) )
163 es_format_Init( &p_dec->fmt_out, VIDEO_ES, VLC_FOURCC('m','p','g','v') );
164 p_dec->pf_packetize = Packetize;
165 p_dec->pf_get_cc = GetCc;
167 p_dec->p_sys = p_sys = malloc( sizeof( decoder_sys_t ) );
170 memset( p_dec->p_sys, 0, sizeof( decoder_sys_t ) );
173 p_sys->i_state = STATE_NOSYNC;
174 p_sys->bytestream = block_BytestreamInit();
175 p_sys->p_startcode[0] = 0;
176 p_sys->p_startcode[1] = 0;
177 p_sys->p_startcode[2] = 1;
182 p_sys->p_frame = NULL;
183 p_sys->pp_last = &p_sys->p_frame;
184 p_sys->b_frame_slice = false;
186 p_sys->i_dts = p_sys->i_pts = 0;
188 p_sys->i_frame_rate = 1;
189 p_sys->i_frame_rate_base = 1;
190 p_sys->b_seq_progressive = true;
191 p_sys->b_low_delay = true;
192 p_sys->i_seq_old = 0;
194 p_sys->i_temporal_ref = 0;
195 p_sys->i_picture_type = 0;
196 p_sys->i_picture_structure = 0x03; /* frame */
197 p_sys->i_top_field_first = 0;
198 p_sys->i_repeat_first_field = 0;
199 p_sys->i_progressive_frame = 0;
202 p_sys->i_interpolated_dts = 0;
203 p_sys->i_last_ref_pts = 0;
204 p_sys->b_second_field = 0;
206 p_sys->b_discontinuity = false;
207 p_sys->b_sync_on_intra_frame = var_CreateGetBool( p_dec, "packetizer-mpegvideo-sync-iframe" );
208 if( p_sys->b_sync_on_intra_frame )
209 msg_Dbg( p_dec, "syncing on intra frame now" );
211 p_sys->b_cc_reset = false;
214 p_sys->i_cc_flags = 0;
215 cc_Init( &p_sys->cc );
220 /*****************************************************************************
222 *****************************************************************************/
223 static void Close( vlc_object_t *p_this )
225 decoder_t *p_dec = (decoder_t*)p_this;
226 decoder_sys_t *p_sys = p_dec->p_sys;
228 block_BytestreamRelease( &p_sys->bytestream );
232 block_Release( p_sys->p_seq );
236 block_Release( p_sys->p_ext );
240 block_ChainRelease( p_sys->p_frame );
243 var_Destroy( p_dec, "packetizer-mpegvideo-sync-iframe" );
248 /*****************************************************************************
250 *****************************************************************************/
251 static block_t *Packetize( decoder_t *p_dec, block_t **pp_block )
253 decoder_sys_t *p_sys = p_dec->p_sys;
256 if( pp_block == NULL || *pp_block == NULL )
261 if( (*pp_block)->i_flags&(BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED) )
263 if( (*pp_block)->i_flags&BLOCK_FLAG_CORRUPTED )
265 p_sys->i_state = STATE_NOSYNC;
266 block_BytestreamEmpty( &p_sys->bytestream );
268 p_sys->b_discontinuity = true;
270 block_ChainRelease( p_sys->p_frame );
271 p_sys->p_frame = NULL;
272 p_sys->pp_last = &p_sys->p_frame;
273 p_sys->b_frame_slice = false;
277 p_sys->i_interpolated_dts = 0;
278 p_sys->i_last_ref_pts = 0;
280 block_Release( *pp_block );
285 block_BytestreamPush( &p_sys->bytestream, *pp_block );
289 switch( p_sys->i_state )
293 if( block_FindStartcodeFromOffset( &p_sys->bytestream,
294 &p_sys->i_offset, p_sys->p_startcode, 3 ) == VLC_SUCCESS )
296 p_sys->i_state = STATE_NEXT_SYNC;
299 if( p_sys->i_offset )
301 block_SkipBytes( &p_sys->bytestream, p_sys->i_offset );
303 block_BytestreamFlush( &p_sys->bytestream );
306 if( p_sys->i_state != STATE_NEXT_SYNC )
312 p_sys->i_offset = 1; /* To find next startcode */
314 case STATE_NEXT_SYNC:
315 /* TODO: If p_block == NULL, flush the buffer without checking the
318 /* Find the next startcode */
319 if( block_FindStartcodeFromOffset( &p_sys->bytestream,
320 &p_sys->i_offset, p_sys->p_startcode, 3 ) != VLC_SUCCESS )
326 /* Get the new fragment and set the pts/dts */
327 p_pic = block_New( p_dec, p_sys->i_offset );
328 block_BytestreamFlush( &p_sys->bytestream );
329 p_pic->i_pts = p_sys->bytestream.p_block->i_pts;
330 p_pic->i_dts = p_sys->bytestream.p_block->i_dts;
332 block_GetBytes( &p_sys->bytestream, p_pic->p_buffer,
335 /* don't reuse the same timestamps several times */
336 if( p_pic->i_buffer >= 4 && p_pic->p_buffer[3] == 0x00 )
338 /* We have a picture start code */
339 p_sys->bytestream.p_block->i_pts = 0;
340 p_sys->bytestream.p_block->i_dts = 0;
345 /* Get picture if any */
346 if( !( p_pic = ParseMPEGBlock( p_dec, p_pic ) ) )
348 p_sys->i_state = STATE_NOSYNC;
352 /* If a discontinuity has been encountered, then wait till
353 * the next Intra frame before continuing with packetizing */
354 if( p_sys->b_discontinuity &&
355 p_sys->b_sync_on_intra_frame )
357 if( p_pic->i_flags & BLOCK_FLAG_TYPE_I )
359 msg_Dbg( p_dec, "synced on intra frame" );
360 p_sys->b_discontinuity = false;
361 p_pic->i_flags |= BLOCK_FLAG_DISCONTINUITY;
365 msg_Dbg( p_dec, "waiting on intra frame" );
366 p_sys->i_state = STATE_NOSYNC;
367 block_Release( p_pic );
372 /* We've just started the stream, wait for the first PTS.
373 * We discard here so we can still get the sequence header. */
374 if( p_sys->i_dts <= 0 && p_sys->i_pts <= 0 &&
375 p_sys->i_interpolated_dts <= 0 )
377 msg_Dbg( p_dec, "need a starting pts/dts" );
378 p_sys->i_state = STATE_NOSYNC;
379 block_Release( p_pic );
383 /* When starting the stream we can have the first frame with
384 * a null DTS (i_interpolated_pts is initialized to 0) */
385 if( !p_pic->i_dts ) p_pic->i_dts = p_pic->i_pts;
387 /* So p_block doesn't get re-added several times */
388 *pp_block = block_BytestreamPop( &p_sys->bytestream );
390 p_sys->i_state = STATE_NOSYNC;
397 /*****************************************************************************
399 *****************************************************************************/
400 static block_t *GetCc( decoder_t *p_dec, bool pb_present[4] )
402 decoder_sys_t *p_sys = p_dec->p_sys;
406 for( i = 0; i < 4; i++ )
407 pb_present[i] = p_sys->cc.pb_present[i];
409 if( p_sys->cc.i_data <= 0 )
412 p_cc = block_New( p_dec, p_sys->cc.i_data);
415 memcpy( p_cc->p_buffer, p_sys->cc.p_data, p_sys->cc.i_data );
417 p_cc->i_pts = p_sys->cc.b_reorder ? p_sys->i_cc_pts : p_sys->i_cc_dts;
418 p_cc->i_flags = ( p_sys->cc.b_reorder ? p_sys->i_cc_flags : BLOCK_FLAG_TYPE_P ) & ( BLOCK_FLAG_TYPE_I|BLOCK_FLAG_TYPE_P|BLOCK_FLAG_TYPE_B);
420 cc_Flush( &p_sys->cc );
424 /*****************************************************************************
425 * ParseMPEGBlock: Re-assemble fragments into a block containing a picture
426 *****************************************************************************/
427 static block_t *ParseMPEGBlock( decoder_t *p_dec, block_t *p_frag )
429 decoder_sys_t *p_sys = p_dec->p_sys;
430 block_t *p_pic = NULL;
433 * Check if previous picture is finished
435 if( ( p_sys->b_frame_slice &&
436 (p_frag->p_buffer[3] == 0x00 || p_frag->p_buffer[3] > 0xaf) ) &&
437 p_sys->p_seq == NULL )
439 /* We have a picture but without a sequence header we can't
441 msg_Dbg( p_dec, "waiting for sequence start" );
442 if( p_sys->p_frame ) block_ChainRelease( p_sys->p_frame );
443 p_sys->p_frame = NULL;
444 p_sys->pp_last = &p_sys->p_frame;
445 p_sys->b_frame_slice = false;
448 else if( p_sys->b_frame_slice &&
449 (p_frag->p_buffer[3] == 0x00 || p_frag->p_buffer[3] > 0xaf) )
451 const bool b_eos = p_frag->p_buffer[3] == 0xb7;
457 block_ChainLastAppend( &p_sys->pp_last, p_frag );
461 p_pic = block_ChainGather( p_sys->p_frame );
464 p_pic->i_flags |= BLOCK_FLAG_END_OF_SEQUENCE;
466 i_duration = (mtime_t)( 1000000 * p_sys->i_frame_rate_base /
467 p_sys->i_frame_rate );
469 if( !p_sys->b_seq_progressive && p_sys->i_picture_structure != 0x03 )
474 if( p_sys->b_seq_progressive )
476 if( p_sys->i_top_field_first == 0 &&
477 p_sys->i_repeat_first_field == 1 )
481 else if( p_sys->i_top_field_first == 1 &&
482 p_sys->i_repeat_first_field == 1 )
489 if( p_sys->i_picture_structure == 0x03 )
491 if( p_sys->i_progressive_frame && p_sys->i_repeat_first_field )
493 i_duration += i_duration / 2;
498 if( p_sys->b_low_delay || p_sys->i_picture_type == 0x03 )
500 /* Trivial case (DTS == PTS) */
501 /* Correct interpolated dts when we receive a new pts/dts */
502 if( p_sys->i_pts > 0 ) p_sys->i_interpolated_dts = p_sys->i_pts;
503 if( p_sys->i_dts > 0 ) p_sys->i_interpolated_dts = p_sys->i_dts;
507 /* Correct interpolated dts when we receive a new pts/dts */
508 if( p_sys->i_last_ref_pts > 0 && !p_sys->b_second_field )
509 p_sys->i_interpolated_dts = p_sys->i_last_ref_pts;
510 if( p_sys->i_dts > 0 ) p_sys->i_interpolated_dts = p_sys->i_dts;
512 if( !p_sys->b_second_field )
513 p_sys->i_last_ref_pts = p_sys->i_pts;
516 p_pic->i_dts = p_sys->i_interpolated_dts;
517 p_sys->i_interpolated_dts += i_duration;
519 /* Set PTS only if we have a B frame or if it comes from the stream */
520 if( p_sys->i_pts > 0 )
522 p_pic->i_pts = p_sys->i_pts;
524 else if( p_sys->i_picture_type == 0x03 )
526 p_pic->i_pts = p_pic->i_dts;
533 switch ( p_sys->i_picture_type )
536 p_pic->i_flags |= BLOCK_FLAG_TYPE_I;
539 p_pic->i_flags |= BLOCK_FLAG_TYPE_P;
542 p_pic->i_flags |= BLOCK_FLAG_TYPE_B;
546 p_pic->i_length = p_sys->i_interpolated_dts - p_pic->i_dts;
549 msg_Dbg( p_dec, "pic: type=%d dts=%"PRId64" pts-dts=%"PRId64,
550 p_sys->i_picture_type, p_pic->i_dts, p_pic->i_pts - p_pic->i_dts);
554 p_sys->p_frame = NULL;
555 p_sys->pp_last = &p_sys->p_frame;
556 p_sys->b_frame_slice = false;
558 if( p_sys->i_picture_structure != 0x03 )
560 p_sys->b_second_field = !p_sys->b_second_field;
564 p_sys->b_second_field = 0;
568 p_sys->b_cc_reset = true;
569 p_sys->i_cc_pts = p_pic->i_pts;
570 p_sys->i_cc_dts = p_pic->i_dts;
571 p_sys->i_cc_flags = p_pic->i_flags;
574 if( !p_pic && p_sys->b_cc_reset )
576 p_sys->b_cc_reset = false;
577 cc_Flush( &p_sys->cc );
583 * Check info of current fragment
585 if( p_frag->p_buffer[3] == 0xb8 )
587 /* Group start code */
589 p_sys->i_seq_old > p_sys->i_frame_rate/p_sys->i_frame_rate_base )
591 /* Usefull for mpeg1: repeat sequence header every second */
592 block_ChainLastAppend( &p_sys->pp_last, block_Duplicate( p_sys->p_seq ) );
595 block_ChainLastAppend( &p_sys->pp_last, block_Duplicate( p_sys->p_ext ) );
598 p_sys->i_seq_old = 0;
601 else if( p_frag->p_buffer[3] == 0xb3 && p_frag->i_buffer >= 8 )
603 /* Sequence header code */
604 static const int code_to_frame_rate[16][2] =
606 { 1, 1 }, /* invalid */
607 { 24000, 1001 }, { 24, 1 }, { 25, 1 }, { 30000, 1001 },
608 { 30, 1 }, { 50, 1 }, { 60000, 1001 }, { 60, 1 },
609 /* Unofficial 15fps from Xing*/
611 /* Unofficial economy rates from libmpeg3 */
612 { 5, 1001 }, { 10, 1001 }, { 12, 1001 }, { 15, 1001 },
613 { 1, 1 }, { 1, 1 } /* invalid */
616 if( p_sys->p_seq ) block_Release( p_sys->p_seq );
617 if( p_sys->p_ext ) block_Release( p_sys->p_ext );
619 p_sys->p_seq = block_Duplicate( p_frag );
620 p_sys->i_seq_old = 0;
623 p_dec->fmt_out.video.i_width =
624 ( p_frag->p_buffer[4] << 4)|(p_frag->p_buffer[5] >> 4 );
625 p_dec->fmt_out.video.i_height =
626 ( (p_frag->p_buffer[5]&0x0f) << 8 )|p_frag->p_buffer[6];
627 p_sys->i_aspect_ratio_info = p_frag->p_buffer[7] >> 4;
629 /* TODO: MPEG1 aspect ratio */
631 p_sys->i_frame_rate = code_to_frame_rate[p_frag->p_buffer[7]&0x0f][0];
632 p_sys->i_frame_rate_base =
633 code_to_frame_rate[p_frag->p_buffer[7]&0x0f][1];
635 p_dec->fmt_out.video.i_frame_rate = p_sys->i_frame_rate;
636 p_dec->fmt_out.video.i_frame_rate_base = p_sys->i_frame_rate_base;
638 p_sys->b_seq_progressive = true;
639 p_sys->b_low_delay = true;
641 if ( !p_sys->b_inited )
643 msg_Dbg( p_dec, "size %dx%d fps=%.3f",
644 p_dec->fmt_out.video.i_width, p_dec->fmt_out.video.i_height,
645 p_sys->i_frame_rate / (float)p_sys->i_frame_rate_base );
649 else if( p_frag->p_buffer[3] == 0xb5 )
651 int i_type = p_frag->p_buffer[4] >> 4;
653 /* Extension start code */
657 static const int mpeg2_aspect[16][2] =
659 {0,1}, {1,1}, {4,3}, {16,9}, {221,100},
660 {0,1}, {0,1}, {0,1}, {0,1}, {0,1}, {0,1}, {0,1}, {0,1}, {0,1},
665 /* sequence extension */
666 if( p_sys->p_ext) block_Release( p_sys->p_ext );
667 p_sys->p_ext = block_Duplicate( p_frag );
669 if( p_frag->i_buffer >= 10 )
671 p_sys->b_seq_progressive =
672 p_frag->p_buffer[5]&0x08 ? true : false;
674 p_frag->p_buffer[9]&0x80 ? true : false;
677 /* Do not set aspect ratio : in case we're transcoding,
678 * transcode will take our fmt_out as a fmt_in to libmpeg2.
679 * libmpeg2.c will then believe that the user has requested
680 * a specific aspect ratio, which she hasn't. Thus in case
681 * of aspect ratio change, we're screwed. --Meuuh
684 p_dec->fmt_out.video.i_aspect =
685 mpeg2_aspect[p_sys->i_aspect_ratio_info][0] *
687 mpeg2_aspect[p_sys->i_aspect_ratio_info][1];
691 else if( i_type == 0x08 )
693 /* picture extension */
694 p_sys->i_picture_structure = p_frag->p_buffer[6]&0x03;
695 p_sys->i_top_field_first = p_frag->p_buffer[7] >> 7;
696 p_sys->i_repeat_first_field= (p_frag->p_buffer[7]>>1)&0x01;
697 p_sys->i_progressive_frame = p_frag->p_buffer[8] >> 7;
700 else if( p_frag->p_buffer[3] == 0xb2 && p_frag->i_buffer > 4 )
702 cc_Extract( &p_sys->cc, &p_frag->p_buffer[4], p_frag->i_buffer - 4 );
704 else if( p_frag->p_buffer[3] == 0x00 )
706 /* Picture start code */
709 if( p_frag->i_buffer >= 6 )
711 p_sys->i_temporal_ref =
712 ( p_frag->p_buffer[4] << 2 )|(p_frag->p_buffer[5] >> 6);
713 p_sys->i_picture_type = ( p_frag->p_buffer[5] >> 3 ) & 0x03;
716 p_sys->i_dts = p_frag->i_dts;
717 p_sys->i_pts = p_frag->i_pts;
719 else if( p_frag->p_buffer[3] >= 0x01 && p_frag->p_buffer[3] <= 0xaf )
721 /* Slice start code */
722 p_sys->b_frame_slice = true;
725 /* Append the block */
726 block_ChainLastAppend( &p_sys->pp_last, p_frag );