X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=modules%2Fpacketizer%2Fmpegvideo.c;h=ad0f7da1c0a4c09b8189c0e70bcc7d3b3a6b0f98;hb=000076e7dd4055484a3b1f3df6fe18d2c4d64676;hp=f612389773e6e664ae47bacdd855f9ab9f26f200;hpb=59dfb394720537f4f8091f4d80fccd469551210f;p=vlc diff --git a/modules/packetizer/mpegvideo.c b/modules/packetizer/mpegvideo.c index f612389773..ad0f7da1c0 100644 --- a/modules/packetizer/mpegvideo.c +++ b/modules/packetizer/mpegvideo.c @@ -1,12 +1,13 @@ /***************************************************************************** * mpegvideo.c: parse and packetize an MPEG1/2 video stream ***************************************************************************** - * Copyright (C) 2001, 2002 VideoLAN - * $Id: mpegvideo.c,v 1.24 2003/12/06 23:25:23 gbazin Exp $ + * Copyright (C) 2001-2006 the VideoLAN team + * $Id$ * * Authors: Laurent Aimar * Eric Petit - * Gildas Bazin + * Gildas Bazin + * Jean-Paul Saman * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -20,7 +21,7 @@ * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA. + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA. *****************************************************************************/ /***************************************************************************** @@ -40,13 +41,22 @@ /***************************************************************************** * Preamble *****************************************************************************/ -#include /* malloc(), free() */ -#include -#include -#include +#ifdef HAVE_CONFIG_H +# include "config.h" +#endif + +#include +#include +#include +#include +#include +#include "../codec/cc.h" -#include "vlc_block_helper.h" +#define SYNC_INTRAFRAME_TEXT N_("Sync on Intra Frame") +#define SYNC_INTRAFRAME_LONGTEXT N_("Normally the packetizer would " \ + "sync on the next full frame. This flags instructs the packetizer " \ + "to sync on the first Intra Frame found.") /***************************************************************************** * Module descriptor @@ -54,17 +64,24 @@ static int Open ( vlc_object_t * ); static void Close( vlc_object_t * ); -vlc_module_begin(); - set_description( _("MPEG-I/II video packetizer") ); - set_capability( "packetizer", 50 ); - set_callbacks( Open, Close ); -vlc_module_end(); +vlc_module_begin () + set_category( CAT_SOUT ) + set_subcategory( SUBCAT_SOUT_PACKETIZER ) + set_description( N_("MPEG-I/II video packetizer") ) + set_shortname( N_("MPEG Video") ) + set_capability( "packetizer", 50 ) + set_callbacks( Open, Close ) + + add_bool( "packetizer-mpegvideo-sync-iframe", 0, NULL, SYNC_INTRAFRAME_TEXT, + SYNC_INTRAFRAME_LONGTEXT, true ); +vlc_module_end () /***************************************************************************** * Local prototypes *****************************************************************************/ static block_t *Packetize( decoder_t *, block_t ** ); static block_t *ParseMPEGBlock( decoder_t *, block_t * ); +static block_t *GetCc( decoder_t *p_dec, bool pb_present[4] ); struct decoder_sys_t { @@ -73,25 +90,28 @@ struct decoder_sys_t */ block_bytestream_t bytestream; int i_state; - int i_offset; + size_t i_offset; uint8_t p_startcode[3]; - /* Sequence header and extention */ + /* Sequence header and extension */ block_t *p_seq; block_t *p_ext; /* Current frame being built */ block_t *p_frame; - vlc_bool_t b_frame_slice; + block_t **pp_last; + + bool b_frame_slice; mtime_t i_pts; mtime_t i_dts; /* Sequence properties */ int i_frame_rate; int i_frame_rate_base; - vlc_bool_t b_seq_progressive; - vlc_bool_t b_low_delay; + bool b_seq_progressive; + bool b_low_delay; int i_aspect_ratio_info; + bool b_inited; /* Picture properties */ int i_temporal_ref; @@ -102,12 +122,22 @@ struct decoder_sys_t int i_progressive_frame; mtime_t i_interpolated_dts; - mtime_t i_old_duration; mtime_t i_last_ref_pts; + bool b_second_field; - /* Number of pictues since last sequence header */ + /* Number of pictures since last sequence header */ int i_seq_old; + /* Sync behaviour */ + bool b_sync_on_intra_frame; + bool b_discontinuity; + + /* */ + bool b_cc_reset; + uint32_t i_cc_flags; + mtime_t i_cc_pts; + mtime_t i_cc_dts; + cc_data_t cc; }; enum { @@ -132,12 +162,16 @@ static int Open( vlc_object_t *p_this ) es_format_Init( &p_dec->fmt_out, VIDEO_ES, VLC_FOURCC('m','p','g','v') ); p_dec->pf_packetize = Packetize; + p_dec->pf_get_cc = GetCc; p_dec->p_sys = p_sys = malloc( sizeof( decoder_sys_t ) ); + if( !p_dec->p_sys ) + return VLC_ENOMEM; + memset( p_dec->p_sys, 0, sizeof( decoder_sys_t ) ); /* Misc init */ p_sys->i_state = STATE_NOSYNC; - p_sys->bytestream = block_BytestreamInit( p_dec ); + p_sys->bytestream = block_BytestreamInit(); p_sys->p_startcode[0] = 0; p_sys->p_startcode[1] = 0; p_sys->p_startcode[2] = 1; @@ -146,14 +180,15 @@ static int Open( vlc_object_t *p_this ) p_sys->p_seq = NULL; p_sys->p_ext = NULL; p_sys->p_frame = NULL; - p_sys->b_frame_slice = VLC_FALSE; + p_sys->pp_last = &p_sys->p_frame; + p_sys->b_frame_slice = false; p_sys->i_dts = p_sys->i_pts = 0; p_sys->i_frame_rate = 1; p_sys->i_frame_rate_base = 1; - p_sys->b_seq_progressive = VLC_TRUE; - p_sys->b_low_delay = VLC_TRUE; + p_sys->b_seq_progressive = true; + p_sys->b_low_delay = true; p_sys->i_seq_old = 0; p_sys->i_temporal_ref = 0; @@ -162,10 +197,22 @@ static int Open( vlc_object_t *p_this ) p_sys->i_top_field_first = 0; p_sys->i_repeat_first_field = 0; p_sys->i_progressive_frame = 0; + p_sys->b_inited = 0; p_sys->i_interpolated_dts = 0; - p_sys->i_old_duration = 0; p_sys->i_last_ref_pts = 0; + p_sys->b_second_field = 0; + + p_sys->b_discontinuity = false; + p_sys->b_sync_on_intra_frame = var_CreateGetBool( p_dec, "packetizer-mpegvideo-sync-iframe" ); + if( p_sys->b_sync_on_intra_frame ) + msg_Dbg( p_dec, "syncing on intra frame now" ); + + p_sys->b_cc_reset = false; + p_sys->i_cc_pts = 0; + p_sys->i_cc_dts = 0; + p_sys->i_cc_flags = 0; + cc_Init( &p_sys->cc ); return VLC_SUCCESS; } @@ -193,6 +240,8 @@ static void Close( vlc_object_t *p_this ) block_ChainRelease( p_sys->p_frame ); } + var_Destroy( p_dec, "packetizer-mpegvideo-sync-iframe" ); + free( p_sys ); } @@ -209,14 +258,28 @@ static block_t *Packetize( decoder_t *p_dec, block_t **pp_block ) return NULL; } - if( (*pp_block)->b_discontinuity ) + if( (*pp_block)->i_flags&(BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED) ) { - p_sys->i_state = STATE_NOSYNC; - if( p_sys->p_frame ) block_ChainRelease( p_sys->p_frame ); - p_sys->p_frame = NULL; - p_sys->b_frame_slice = VLC_FALSE; + if( (*pp_block)->i_flags&BLOCK_FLAG_CORRUPTED ) + { + p_sys->i_state = STATE_NOSYNC; + block_BytestreamFlush( &p_sys->bytestream ); + + p_sys->b_discontinuity = true; + if( p_sys->p_frame ) + block_ChainRelease( p_sys->p_frame ); + p_sys->p_frame = NULL; + p_sys->pp_last = &p_sys->p_frame; + p_sys->b_frame_slice = false; + } +// p_sys->i_interpolated_dts = +// p_sys->i_last_ref_pts = 0; + + block_Release( *pp_block ); + return NULL; } + block_BytestreamPush( &p_sys->bytestream, *pp_block ); while( 1 ) @@ -260,6 +323,7 @@ static block_t *Packetize( decoder_t *p_dec, block_t **pp_block ) /* Get the new fragment and set the pts/dts */ p_pic = block_New( p_dec, p_sys->i_offset ); + block_BytestreamFlush( &p_sys->bytestream ); p_pic->i_pts = p_sys->bytestream.p_block->i_pts; p_pic->i_dts = p_sys->bytestream.p_block->i_dts; @@ -283,6 +347,26 @@ static block_t *Packetize( decoder_t *p_dec, block_t **pp_block ) break; } + /* If a discontinuity has been encountered, then wait till + * the next Intra frame before continuing with packetizing */ + if( p_sys->b_discontinuity && + p_sys->b_sync_on_intra_frame ) + { + if( p_pic->i_flags & BLOCK_FLAG_TYPE_I ) + { + msg_Dbg( p_dec, "synced on intra frame" ); + p_sys->b_discontinuity = false; + p_pic->i_flags |= BLOCK_FLAG_DISCONTINUITY; + } + else + { + msg_Dbg( p_dec, "waiting on intra frame" ); + p_sys->i_state = STATE_NOSYNC; + block_Release( p_pic ); + break; + } + } + /* We've just started the stream, wait for the first PTS. * We discard here so we can still get the sequence header. */ if( p_sys->i_dts <= 0 && p_sys->i_pts <= 0 && @@ -294,6 +378,10 @@ static block_t *Packetize( decoder_t *p_dec, block_t **pp_block ) break; } + /* When starting the stream we can have the first frame with + * a null DTS (i_interpolated_pts is initialized to 0) */ + if( !p_pic->i_dts ) p_pic->i_dts = p_pic->i_pts; + /* So p_block doesn't get re-added several times */ *pp_block = block_BytestreamPop( &p_sys->bytestream ); @@ -304,6 +392,33 @@ static block_t *Packetize( decoder_t *p_dec, block_t **pp_block ) } } +/***************************************************************************** + * GetCc: + *****************************************************************************/ +static block_t *GetCc( decoder_t *p_dec, bool pb_present[4] ) +{ + decoder_sys_t *p_sys = p_dec->p_sys; + block_t *p_cc; + int i; + + for( i = 0; i < 4; i++ ) + pb_present[i] = p_sys->cc.pb_present[i]; + + if( p_sys->cc.i_data <= 0 ) + return NULL; + + p_cc = block_New( p_dec, p_sys->cc.i_data); + if( p_cc ) + { + memcpy( p_cc->p_buffer, p_sys->cc.p_data, p_sys->cc.i_data ); + p_cc->i_dts = + p_cc->i_pts = p_sys->cc.b_reorder ? p_sys->i_cc_pts : p_sys->i_cc_dts; + 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); + } + cc_Flush( &p_sys->cc ); + return p_cc; +} + /***************************************************************************** * ParseMPEGBlock: Re-assemble fragments into a block containing a picture *****************************************************************************/ @@ -324,16 +439,28 @@ static block_t *ParseMPEGBlock( decoder_t *p_dec, block_t *p_frag ) msg_Dbg( p_dec, "waiting for sequence start" ); if( p_sys->p_frame ) block_ChainRelease( p_sys->p_frame ); p_sys->p_frame = NULL; - p_sys->b_frame_slice = VLC_FALSE; + p_sys->pp_last = &p_sys->p_frame; + p_sys->b_frame_slice = false; } else if( p_sys->b_frame_slice && (p_frag->p_buffer[3] == 0x00 || p_frag->p_buffer[3] > 0xaf) ) { + const bool b_eos = p_frag->p_buffer[3] == 0xb7; + mtime_t i_duration; + if( b_eos ) + { + block_ChainLastAppend( &p_sys->pp_last, p_frag ); + p_frag = NULL; + } + p_pic = block_ChainGather( p_sys->p_frame ); + if( b_eos ) + p_pic->i_flags |= BLOCK_FLAG_END_OF_SEQUENCE; + i_duration = (mtime_t)( 1000000 * p_sys->i_frame_rate_base / p_sys->i_frame_rate ); @@ -376,14 +503,16 @@ static block_t *ParseMPEGBlock( decoder_t *p_dec, block_t *p_frag ) else { /* Correct interpolated dts when we receive a new pts/dts */ - if( p_sys->i_last_ref_pts > 0 ) + if( p_sys->i_last_ref_pts > 0 && !p_sys->b_second_field ) p_sys->i_interpolated_dts = p_sys->i_last_ref_pts; if( p_sys->i_dts > 0 ) p_sys->i_interpolated_dts = p_sys->i_dts; - p_sys->i_last_ref_pts = p_sys->i_pts; + if( !p_sys->b_second_field ) + p_sys->i_last_ref_pts = p_sys->i_pts; } p_pic->i_dts = p_sys->i_interpolated_dts; + p_sys->i_interpolated_dts += i_duration; /* Set PTS only if we have a B frame or if it comes from the stream */ if( p_sys->i_pts > 0 ) @@ -399,29 +528,55 @@ static block_t *ParseMPEGBlock( decoder_t *p_dec, block_t *p_frag ) p_pic->i_pts = 0; } - if( p_sys->b_low_delay || p_sys->i_picture_type == 0x03 ) - { - /* Trivial case (DTS == PTS) */ - p_sys->i_interpolated_dts += i_duration; - } - else + switch ( p_sys->i_picture_type ) { - p_sys->i_interpolated_dts += p_sys->i_old_duration; - p_sys->i_old_duration = i_duration; + case 0x01: + p_pic->i_flags |= BLOCK_FLAG_TYPE_I; + break; + case 0x02: + p_pic->i_flags |= BLOCK_FLAG_TYPE_P; + break; + case 0x03: + p_pic->i_flags |= BLOCK_FLAG_TYPE_B; + break; } p_pic->i_length = p_sys->i_interpolated_dts - p_pic->i_dts; #if 0 - msg_Dbg( p_dec, "pic: type=%d dts="I64Fd" pts-dts="I64Fd, + msg_Dbg( p_dec, "pic: type=%d dts=%"PRId64" pts-dts=%"PRId64, p_sys->i_picture_type, p_pic->i_dts, p_pic->i_pts - p_pic->i_dts); #endif /* Reset context */ p_sys->p_frame = NULL; - p_sys->b_frame_slice = VLC_FALSE; + p_sys->pp_last = &p_sys->p_frame; + p_sys->b_frame_slice = false; + + if( p_sys->i_picture_structure != 0x03 ) + { + p_sys->b_second_field = !p_sys->b_second_field; + } + else + { + p_sys->b_second_field = 0; + } + + /* CC */ + p_sys->b_cc_reset = true; + p_sys->i_cc_pts = p_pic->i_pts; + p_sys->i_cc_dts = p_pic->i_dts; + p_sys->i_cc_flags = p_pic->i_flags; } + if( !p_pic && p_sys->b_cc_reset ) + { + p_sys->b_cc_reset = false; + cc_Flush( &p_sys->cc ); + } + + if( !p_frag ) + return p_pic; /* * Check info of current fragment */ @@ -432,12 +587,10 @@ static block_t *ParseMPEGBlock( decoder_t *p_dec, block_t *p_frag ) p_sys->i_seq_old > p_sys->i_frame_rate/p_sys->i_frame_rate_base ) { /* Usefull for mpeg1: repeat sequence header every second */ - block_ChainAppend( &p_sys->p_frame, - block_Duplicate( p_sys->p_seq ) ); + block_ChainLastAppend( &p_sys->pp_last, block_Duplicate( p_sys->p_seq ) ); if( p_sys->p_ext ) { - block_ChainAppend( &p_sys->p_frame, - block_Duplicate( p_sys->p_ext ) ); + block_ChainLastAppend( &p_sys->pp_last, block_Duplicate( p_sys->p_ext ) ); } p_sys->i_seq_old = 0; @@ -477,41 +630,54 @@ static block_t *ParseMPEGBlock( decoder_t *p_dec, block_t *p_frag ) p_sys->i_frame_rate_base = code_to_frame_rate[p_frag->p_buffer[7]&0x0f][1]; - p_sys->b_seq_progressive = VLC_TRUE; - p_sys->b_low_delay = VLC_TRUE; + p_dec->fmt_out.video.i_frame_rate = p_sys->i_frame_rate; + p_dec->fmt_out.video.i_frame_rate_base = p_sys->i_frame_rate_base; -#if 0 - msg_Dbg( p_dec, "Size %dx%d fps=%.3f", + p_sys->b_seq_progressive = true; + p_sys->b_low_delay = true; + + if ( !p_sys->b_inited ) + { + msg_Dbg( p_dec, "size %dx%d fps=%.3f", p_dec->fmt_out.video.i_width, p_dec->fmt_out.video.i_height, p_sys->i_frame_rate / (float)p_sys->i_frame_rate_base ); -#endif + p_sys->b_inited = 1; + } } else if( p_frag->p_buffer[3] == 0xb5 ) { int i_type = p_frag->p_buffer[4] >> 4; - /* Extention start code */ + /* Extension start code */ if( i_type == 0x01 ) { +#if 0 static const int mpeg2_aspect[16][2] = { {0,1}, {1,1}, {4,3}, {16,9}, {221,100}, {0,1}, {0,1}, {0,1}, {0,1}, {0,1}, {0,1}, {0,1}, {0,1}, {0,1}, {0,1}, {0,1} }; +#endif - /* sequence extention */ + /* sequence extension */ if( p_sys->p_ext) block_Release( p_sys->p_ext ); p_sys->p_ext = block_Duplicate( p_frag ); if( p_frag->i_buffer >= 10 ) { p_sys->b_seq_progressive = - p_frag->p_buffer[5]&0x08 ? VLC_TRUE : VLC_FALSE; + p_frag->p_buffer[5]&0x08 ? true : false; p_sys->b_low_delay = - p_frag->p_buffer[9]&0x80 ? VLC_TRUE : VLC_FALSE; + p_frag->p_buffer[9]&0x80 ? true : false; } + /* Do not set aspect ratio : in case we're transcoding, + * transcode will take our fmt_out as a fmt_in to libmpeg2. + * libmpeg2.c will then believe that the user has requested + * a specific aspect ratio, which she hasn't. Thus in case + * of aspect ratio change, we're screwed. --Meuuh + */ #if 0 p_dec->fmt_out.video.i_aspect = mpeg2_aspect[p_sys->i_aspect_ratio_info][0] * @@ -522,13 +688,17 @@ static block_t *ParseMPEGBlock( decoder_t *p_dec, block_t *p_frag ) } else if( i_type == 0x08 ) { - /* picture extention */ + /* picture extension */ p_sys->i_picture_structure = p_frag->p_buffer[6]&0x03; p_sys->i_top_field_first = p_frag->p_buffer[7] >> 7; p_sys->i_repeat_first_field= (p_frag->p_buffer[7]>>1)&0x01; p_sys->i_progressive_frame = p_frag->p_buffer[8] >> 7; } } + else if( p_frag->p_buffer[3] == 0xb2 && p_frag->i_buffer > 4 ) + { + cc_Extract( &p_sys->cc, &p_frag->p_buffer[4], p_frag->i_buffer - 4 ); + } else if( p_frag->p_buffer[3] == 0x00 ) { /* Picture start code */ @@ -547,11 +717,11 @@ static block_t *ParseMPEGBlock( decoder_t *p_dec, block_t *p_frag ) else if( p_frag->p_buffer[3] >= 0x01 && p_frag->p_buffer[3] <= 0xaf ) { /* Slice start code */ - p_sys->b_frame_slice = VLC_TRUE; + p_sys->b_frame_slice = true; } /* Append the block */ - block_ChainAppend( &p_sys->p_frame, p_frag ); + block_ChainLastAppend( &p_sys->pp_last, p_frag ); return p_pic; }