From: Gildas Bazin Date: Sat, 6 Dec 2003 23:25:23 +0000 (+0000) Subject: * include/vlc_block_helper.h: new block_FindStartcodeFromOffset() function. X-Git-Tag: 0.7.0~202 X-Git-Url: https://git.sesse.net/?a=commitdiff_plain;h=59dfb394720537f4f8091f4d80fccd469551210f;p=vlc * include/vlc_block_helper.h: new block_FindStartcodeFromOffset() function. * modules/packetizer/mpegvideo.c: rewrote the packetizer to use the block helper functions. --- diff --git a/include/vlc_block_helper.h b/include/vlc_block_helper.h index bb155023be..f22f7fcf44 100644 --- a/include/vlc_block_helper.h +++ b/include/vlc_block_helper.h @@ -2,7 +2,7 @@ * vlc_block_helper.h: Helper functions for data blocks management. ***************************************************************************** * Copyright (C) 2003 VideoLAN - * $Id: vlc_block_helper.h,v 1.5 2003/11/16 21:07:30 gbazin Exp $ + * $Id: vlc_block_helper.h,v 1.6 2003/12/06 23:25:23 gbazin Exp $ * * Authors: Gildas Bazin * @@ -436,4 +436,72 @@ static inline int block_PeekOffsetBytes( block_bytestream_t *p_bytestream, return VLC_SUCCESS; } +static inline int block_FindStartcodeFromOffset( + block_bytestream_t *p_bytestream, int *pi_offset, + uint8_t *p_startcode, int i_startcode_length ) +{ + block_t *p_block, *p_block_backup = 0; + int i_size, i_offset, i_offset_backup = 0; + int i_caller_offset_backup = 0, i_match; + + /* Find the right place */ + i_size = *pi_offset + p_bytestream->i_offset; + for( p_block = p_bytestream->p_block; + p_block != NULL; p_block = p_block->p_next ) + { + i_size -= p_block->i_buffer; + if( i_size < 0 ) break; + } + + if( i_size >= 0 ) + { + /* Not enough data, bail out */ + return VLC_EGENERIC; + } + + /* Begin the search. + * We first look for an occurence of the 1st startcode byte and + * if found, we do a more thorough check. */ + i_size = p_block->i_buffer + i_size; + *pi_offset -= i_size; + i_match = 0; + for( ; p_block != NULL; p_block = p_block->p_next ) + { + for( i_offset = i_size; i_offset < p_block->i_buffer; i_offset++ ) + { + if( p_block->p_buffer[i_offset] == p_startcode[i_match] ) + { + if( !i_match ) + { + p_block_backup = p_block; + i_offset_backup = i_offset; + i_caller_offset_backup = *pi_offset; + } + + if( i_match + 1 == i_startcode_length ) + { + /* We have it */ + *pi_offset += i_offset - i_match; + return VLC_SUCCESS; + } + + i_match++; + } + else if ( i_match ) + { + /* False positive */ + p_block = p_block_backup; + i_offset = i_offset_backup; + *pi_offset = i_caller_offset_backup; + i_match = 0; + } + + } + i_size = 0; + *pi_offset += i_offset; + } + + return VLC_EGENERIC; +} + #endif /* VLC_BLOCK_HELPER_H */ diff --git a/modules/packetizer/mpegvideo.c b/modules/packetizer/mpegvideo.c index ff3815a8ec..f612389773 100644 --- a/modules/packetizer/mpegvideo.c +++ b/modules/packetizer/mpegvideo.c @@ -1,11 +1,12 @@ /***************************************************************************** - * mpegvideo.c + * mpegvideo.c: parse and packetize an MPEG1/2 video stream ***************************************************************************** * Copyright (C) 2001, 2002 VideoLAN - * $Id: mpegvideo.c,v 1.23 2003/11/27 22:44:50 massiot Exp $ + * $Id: mpegvideo.c,v 1.24 2003/12/06 23:25:23 gbazin Exp $ * * Authors: Laurent Aimar * Eric Petit + * Gildas Bazin * * 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 @@ -45,6 +46,8 @@ #include #include +#include "vlc_block_helper.h" + /***************************************************************************** * Module descriptor *****************************************************************************/ @@ -57,43 +60,40 @@ vlc_module_begin(); set_callbacks( Open, Close ); vlc_module_end(); - /***************************************************************************** * Local prototypes *****************************************************************************/ static block_t *Packetize( decoder_t *, block_t ** ); - -static int mpgv_FindStartCode( uint8_t **pp_start, uint8_t *p_end ); +static block_t *ParseMPEGBlock( decoder_t *, block_t * ); struct decoder_sys_t { - /* sequence header and extention */ + /* + * Input properties + */ + block_bytestream_t bytestream; + int i_state; + int i_offset; + uint8_t p_startcode[3]; + + /* Sequence header and extention */ block_t *p_seq; block_t *p_ext; - /* current frame being building */ + /* Current frame being built */ block_t *p_frame; vlc_bool_t b_frame_slice; - vlc_bool_t b_frame_corrupted; - vlc_bool_t b_gop; - - /* pts of current picture */ mtime_t i_pts; mtime_t i_dts; - /* gathering buffer */ - int i_buffer; - int i_buffer_size; - uint8_t *p_buffer; - uint8_t *p_start, *p_old; - - /* */ + /* Sequence properties */ int i_frame_rate; int i_frame_rate_base; vlc_bool_t b_seq_progressive; vlc_bool_t b_low_delay; + int i_aspect_ratio_info; - /* */ + /* Picture properties */ int i_temporal_ref; int i_picture_type; int i_picture_structure; @@ -101,14 +101,18 @@ struct decoder_sys_t int i_repeat_first_field; int i_progressive_frame; - /* */ - int i_seq_old; /* How many picture from last seq */ - - /* */ - mtime_t i_interpolated_dts; mtime_t i_old_duration; mtime_t i_last_ref_pts; + + /* Number of pictues since last sequence header */ + int i_seq_old; + +}; + +enum { + STATE_NOSYNC, + STATE_NEXT_SYNC }; /***************************************************************************** @@ -126,26 +130,25 @@ static int Open( vlc_object_t *p_this ) return VLC_EGENERIC; } - es_format_Init( &p_dec->fmt_out, VIDEO_ES, VLC_FOURCC( 'm', 'p', 'g', 'v' ) ); + es_format_Init( &p_dec->fmt_out, VIDEO_ES, VLC_FOURCC('m','p','g','v') ); p_dec->pf_packetize = Packetize; p_dec->p_sys = p_sys = malloc( sizeof( decoder_sys_t ) ); + /* Misc init */ + p_sys->i_state = STATE_NOSYNC; + p_sys->bytestream = block_BytestreamInit( p_dec ); + p_sys->p_startcode[0] = 0; + p_sys->p_startcode[1] = 0; + p_sys->p_startcode[2] = 1; + p_sys->i_offset = 0; + p_sys->p_seq = NULL; p_sys->p_ext = NULL; p_sys->p_frame = NULL; p_sys->b_frame_slice = VLC_FALSE; - p_sys->b_frame_corrupted = VLC_FALSE; - p_sys->b_gop = VLC_FALSE; - p_sys->i_buffer = 0; - p_sys->i_buffer_size = 10000; - p_sys->p_buffer = malloc( p_sys->i_buffer_size ); - p_sys->p_start = p_sys->p_buffer; - p_sys->p_old = NULL; - - p_sys->i_dts = 0; - p_sys->i_pts = 0; + p_sys->i_dts = p_sys->i_pts = 0; p_sys->i_frame_rate = 1; p_sys->i_frame_rate_base = 1; @@ -175,6 +178,8 @@ static void Close( vlc_object_t *p_this ) decoder_t *p_dec = (decoder_t*)p_this; decoder_sys_t *p_sys = p_dec->p_sys; + block_BytestreamRelease( &p_sys->bytestream ); + if( p_sys->p_seq ) { block_Release( p_sys->p_seq ); @@ -185,9 +190,9 @@ static void Close( vlc_object_t *p_this ) } if( p_sys->p_frame ) { - block_Release( p_sys->p_frame ); + block_ChainRelease( p_sys->p_frame ); } - free( p_sys->p_buffer ); + free( p_sys ); } @@ -197,323 +202,356 @@ static void Close( vlc_object_t *p_this ) static block_t *Packetize( decoder_t *p_dec, block_t **pp_block ) { decoder_sys_t *p_sys = p_dec->p_sys; - block_t *p_chain_out = NULL; - block_t *p_block; + block_t *p_pic; if( pp_block == NULL || *pp_block == NULL ) { return NULL; } - p_block = *pp_block; - *pp_block = NULL; - if( p_block->b_discontinuity ) + if( (*pp_block)->b_discontinuity ) { - p_sys->b_frame_corrupted = VLC_TRUE; + 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; } - /* Append data */ - if( p_sys->i_buffer + p_block->i_buffer > p_sys->i_buffer_size ) - { - uint8_t *p_buffer = p_sys->p_buffer; - - p_sys->i_buffer_size += p_block->i_buffer + 1024; - p_sys->p_buffer = realloc( p_sys->p_buffer, p_sys->i_buffer_size ); + block_BytestreamPush( &p_sys->bytestream, *pp_block ); - if( p_sys->p_start ) - { - p_sys->p_start = p_sys->p_start - p_buffer + p_sys->p_buffer; - } - if( p_sys->p_old ) + while( 1 ) + { + switch( p_sys->i_state ) { - p_sys->p_old = p_sys->p_old - p_buffer + p_sys->p_buffer; - } - } - memcpy( &p_sys->p_buffer[p_sys->i_buffer], p_block->p_buffer, - p_block->i_buffer ); - p_sys->i_buffer += p_block->i_buffer; + case STATE_NOSYNC: + if( block_FindStartcodeFromOffset( &p_sys->bytestream, + &p_sys->i_offset, p_sys->p_startcode, 3 ) == VLC_SUCCESS ) + { + p_sys->i_state = STATE_NEXT_SYNC; + } + if( p_sys->i_offset ) + { + block_SkipBytes( &p_sys->bytestream, p_sys->i_offset ); + p_sys->i_offset = 0; + block_BytestreamFlush( &p_sys->bytestream ); + } - if( p_sys->i_buffer > 10*1000000 ) - { - msg_Err( p_dec, "mmh reseting context" ); - p_sys->i_buffer = 0; - } + if( p_sys->i_state != STATE_NEXT_SYNC ) + { + /* Need more data */ + return NULL; + } - /* Split data in block */ - for( ;; ) - { - if( mpgv_FindStartCode( &p_sys->p_start, &p_sys->p_buffer[p_sys->i_buffer] ) ) - { - block_Release( p_block ); + p_sys->i_offset = 1; /* To find next startcode */ + + case STATE_NEXT_SYNC: + /* TODO: If p_block == NULL, flush the buffer without checking the + * next sync word */ - if( p_sys->p_seq == NULL ) + /* Find the next startcode */ + if( block_FindStartcodeFromOffset( &p_sys->bytestream, + &p_sys->i_offset, p_sys->p_startcode, 3 ) != VLC_SUCCESS ) { - block_ChainRelease( p_chain_out ); + /* Need more data */ return NULL; } - return p_chain_out; - } - if( p_sys->p_old ) - { - /* Extract the data */ - int i_frag = p_sys->p_start - p_sys->p_old; - block_t *p_frag = block_New( p_dec, i_frag ); + /* Get the new fragment and set the pts/dts */ + p_pic = block_New( p_dec, p_sys->i_offset ); + p_pic->i_pts = p_sys->bytestream.p_block->i_pts; + p_pic->i_dts = p_sys->bytestream.p_block->i_dts; - memcpy( p_frag->p_buffer, p_sys->p_old, i_frag ); - if( i_frag < p_sys->i_buffer ) + block_GetBytes( &p_sys->bytestream, p_pic->p_buffer, + p_pic->i_buffer ); + + /* don't reuse the same timestamps several times */ + if( p_pic->i_buffer >= 4 && p_pic->p_buffer[3] == 0x00 ) { - memmove( p_sys->p_buffer, &p_sys->p_buffer[i_frag], - p_sys->i_buffer - i_frag ); + /* We have a picture start code */ + p_sys->bytestream.p_block->i_pts = 0; + p_sys->bytestream.p_block->i_dts = 0; } - p_sys->i_buffer -= i_frag; - p_sys->p_start -= i_frag; - p_sys->p_old -= i_frag; - if( p_sys->b_frame_slice && ( p_frag->p_buffer[3] == 0x00 || p_frag->p_buffer[3] > 0xaf ) ) - { - /* We have a complete picture output it */ - if( p_sys->p_seq == NULL ) - { - msg_Dbg( p_dec, "waiting sequence start" ); - block_ChainRelease( p_sys->p_frame ); - } - else if( p_sys->i_dts <= 0 && p_sys->i_pts <= 0 && p_sys->i_interpolated_dts <= 0 ) - { - msg_Dbg( p_dec, "need a starting pts/dts" ); - block_ChainRelease( p_sys->p_frame ); - } - else if( p_sys->b_frame_corrupted ) - { - msg_Warn( p_dec, "trashing a corrupted picture" ); - block_ChainRelease( p_sys->p_frame ); - p_sys->b_frame_corrupted = VLC_FALSE; - } - else - { - block_t *p_pic = block_ChainGather( p_sys->p_frame ); - mtime_t i_duration = (mtime_t)( 1000000 * p_sys->i_frame_rate_base / p_sys->i_frame_rate); - - if( !p_sys->b_seq_progressive && p_sys->i_picture_structure != 0x03 ) - { - i_duration /= 2; - } - - if( p_sys->b_seq_progressive ) - { - if( p_sys->i_top_field_first == 0 && p_sys->i_repeat_first_field == 1 ) - { - i_duration *= 2; - } - else if( p_sys->i_top_field_first == 1 && p_sys->i_repeat_first_field == 1 ) - { - i_duration *= 3; - } - } - else - { - if( p_sys->i_picture_structure == 0x03 ) - { - if( p_sys->i_progressive_frame && p_sys->i_repeat_first_field ) - { - i_duration += i_duration / 2; - } - } - } - - if( p_sys->b_low_delay || p_sys->i_picture_type == 0x03 ) - { - /* Trivial case (DTS == PTS) */ - /* Correct interpolated dts when we receive a new pts/dts */ - if( p_sys->i_pts > 0 ) p_sys->i_interpolated_dts = p_sys->i_pts; - if( p_sys->i_dts > 0 ) p_sys->i_interpolated_dts = p_sys->i_dts; - } - else - { - /* Correct interpolated dts when we receive a new pts/dts */ - if( p_sys->i_last_ref_pts > 0 ) - 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; - } - - p_pic->i_dts = p_sys->i_interpolated_dts; - /* Set PTS only if I frame or come from stream */ - if( p_sys->i_pts > 0 ) - { - p_pic->i_pts = p_sys->i_pts; - } - else if( p_sys->i_picture_type == 0x03 ) - { - p_pic->i_pts = p_pic->i_dts; - } - else - { - p_pic->i_pts = -1; - } - - if( p_sys->b_low_delay || p_sys->i_picture_type == 0x03 ) - { - /* Trivial case (DTS == PTS) */ - p_sys->i_interpolated_dts += i_duration; - } - else - { - p_sys->i_interpolated_dts += p_sys->i_old_duration; - p_sys->i_old_duration = i_duration; - } - - p_pic->i_length = p_sys->i_interpolated_dts - p_pic->i_dts; - - //msg_Dbg( p_dec, "pic: type=%d dts=%lld pts-dts=%lld", p_sys->i_picture_type, p_pic->i_dts, p_pic->i_pts - p_pic->i_dts); - - block_ChainAppend( &p_chain_out, p_pic ); + p_sys->i_offset = 0; - } + /* Get picture if any */ + if( !( p_pic = ParseMPEGBlock( p_dec, p_pic ) ) ) + { + p_sys->i_state = STATE_NOSYNC; + break; + } - /* reset context */ - p_sys->p_frame = NULL; - p_sys->b_frame_slice = VLC_FALSE; - p_sys->b_gop = VLC_FALSE; - p_sys->i_pts = 0; - p_sys->i_dts = 0; + /* 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 && + p_sys->i_interpolated_dts <= 0 ) + { + msg_Dbg( p_dec, "need a starting pts/dts" ); + p_sys->i_state = STATE_NOSYNC; + block_Release( p_pic ); + break; } - if( p_frag->p_buffer[3] == 0xb8 ) + /* So p_block doesn't get re-added several times */ + *pp_block = block_BytestreamPop( &p_sys->bytestream ); + + p_sys->i_state = STATE_NOSYNC; + + return p_pic; + } + } +} + +/***************************************************************************** + * ParseMPEGBlock: Re-assemble fragments into a block containing a picture + *****************************************************************************/ +static block_t *ParseMPEGBlock( decoder_t *p_dec, block_t *p_frag ) +{ + decoder_sys_t *p_sys = p_dec->p_sys; + block_t *p_pic = NULL; + + /* + * Check if previous picture is finished + */ + if( ( p_sys->b_frame_slice && + (p_frag->p_buffer[3] == 0x00 || p_frag->p_buffer[3] > 0xaf) ) && + p_sys->p_seq == NULL ) + { + /* We have a picture but without a sequence header we can't + * do anything */ + 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; + + } + else if( p_sys->b_frame_slice && + (p_frag->p_buffer[3] == 0x00 || p_frag->p_buffer[3] > 0xaf) ) + { + mtime_t i_duration; + + p_pic = block_ChainGather( p_sys->p_frame ); + + i_duration = (mtime_t)( 1000000 * p_sys->i_frame_rate_base / + p_sys->i_frame_rate ); + + if( !p_sys->b_seq_progressive && p_sys->i_picture_structure != 0x03 ) + { + i_duration /= 2; + } + + if( p_sys->b_seq_progressive ) + { + if( p_sys->i_top_field_first == 0 && + p_sys->i_repeat_first_field == 1 ) { - if( p_sys->p_seq && - 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 ) ); - if( p_sys->p_ext ) - { - block_ChainAppend( &p_sys->p_frame, - block_Duplicate( p_sys->p_ext ) ); - } - - p_sys->i_seq_old = 0; - } - p_sys->b_gop = VLC_TRUE; + i_duration *= 2; } - else if( p_frag->p_buffer[3] == 0xb3 ) + else if( p_sys->i_top_field_first == 1 && + p_sys->i_repeat_first_field == 1 ) { - static const int code_to_frame_rate[16][2] = - { - { 1, 1 }, /* invalid */ - { 24000, 1001 }, { 24, 1 }, { 25, 1 }, { 30000, 1001 }, - { 30, 1 }, { 50, 1 }, { 60000, 1001 }, { 60, 1 }, - { 1, 1 }, { 1, 1 }, { 1, 1 }, { 1, 1 }, /* invalid */ - { 1, 1 }, { 1, 1 }, { 1, 1 } /* invalid */ - }; - - /* sequence header */ - if( p_sys->p_seq ) - { - block_Release( p_sys->p_seq ); - } - if( p_sys->p_ext ) + i_duration *= 3; + } + } + else + { + if( p_sys->i_picture_structure == 0x03 ) + { + if( p_sys->i_progressive_frame && p_sys->i_repeat_first_field ) { - block_Release( p_sys->p_ext ); - p_sys->p_ext = NULL; + i_duration += i_duration / 2; } - p_sys->p_seq = block_Duplicate( p_frag ); - p_sys->i_seq_old = 0; + } + } - p_dec->fmt_out.video.i_width = ( p_frag->p_buffer[4] << 4)|(p_frag->p_buffer[5] >> 4 ); - p_dec->fmt_out.video.i_height= ( (p_frag->p_buffer[5]&0x0f) << 8 )|p_frag->p_buffer[6]; + if( p_sys->b_low_delay || p_sys->i_picture_type == 0x03 ) + { + /* Trivial case (DTS == PTS) */ + /* Correct interpolated dts when we receive a new pts/dts */ + if( p_sys->i_pts > 0 ) p_sys->i_interpolated_dts = p_sys->i_pts; + if( p_sys->i_dts > 0 ) p_sys->i_interpolated_dts = p_sys->i_dts; + } + else + { + /* Correct interpolated dts when we receive a new pts/dts */ + if( p_sys->i_last_ref_pts > 0 ) + 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_frame_rate = code_to_frame_rate[p_frag->p_buffer[7]&0x0f][0]; - p_sys->i_frame_rate_base = code_to_frame_rate[p_frag->p_buffer[7]&0x0f][1]; + p_sys->i_last_ref_pts = p_sys->i_pts; + } + p_pic->i_dts = p_sys->i_interpolated_dts; - p_sys->b_seq_progressive = VLC_TRUE; - p_sys->b_low_delay = VLC_TRUE; + /* Set PTS only if we have a B frame or if it comes from the stream */ + if( p_sys->i_pts > 0 ) + { + p_pic->i_pts = p_sys->i_pts; + } + else if( p_sys->i_picture_type == 0x03 ) + { + p_pic->i_pts = p_pic->i_dts; + } + else + { + 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 + { + p_sys->i_interpolated_dts += p_sys->i_old_duration; + p_sys->i_old_duration = i_duration; + } + + p_pic->i_length = p_sys->i_interpolated_dts - p_pic->i_dts; #if 0 - msg_Dbg( p_dec, "Size %dx%d fps=%.3f", - p_dec->fmt_out.video.i_width, - p_dec->fmt_out.video.i_height, - (float)p_sys->i_frame_rate / (float)p_sys->i_frame_rate_base ); + msg_Dbg( p_dec, "pic: type=%d dts="I64Fd" pts-dts="I64Fd, + p_sys->i_picture_type, p_pic->i_dts, p_pic->i_pts - p_pic->i_dts); #endif - } - else if( p_frag->p_buffer[3] == 0xb5 ) + + /* Reset context */ + p_sys->p_frame = NULL; + p_sys->b_frame_slice = VLC_FALSE; + } + + /* + * Check info of current fragment + */ + if( p_frag->p_buffer[3] == 0xb8 ) + { + /* Group start code */ + if( p_sys->p_seq && + 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 ) ); + if( p_sys->p_ext ) { - int i_type = p_frag->p_buffer[4] >> 4; - /* extention start code */ - if( i_type == 0x01 ) - { - /* sequence extention */ - 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_sys->b_low_delay = p_frag->p_buffer[9]&0x80 ? VLC_TRUE : VLC_FALSE; - } - } - else if( i_type == 0x08 ) - { - /* picture extention */ - 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; - } + block_ChainAppend( &p_sys->p_frame, + block_Duplicate( p_sys->p_ext ) ); } - else if( p_frag->p_buffer[3] == 0x00 ) + + p_sys->i_seq_old = 0; + } + } + else if( p_frag->p_buffer[3] == 0xb3 && p_frag->i_buffer >= 8 ) + { + /* Sequence header code */ + static const int code_to_frame_rate[16][2] = + { + { 1, 1 }, /* invalid */ + { 24000, 1001 }, { 24, 1 }, { 25, 1 }, { 30000, 1001 }, + { 30, 1 }, { 50, 1 }, { 60000, 1001 }, { 60, 1 }, + /* Unofficial 15fps from Xing*/ + { 15, 1001 }, + /* Unofficial economy rates from libmpeg3 */ + { 5, 1001 }, { 10, 1001 }, { 12, 1001 }, { 15, 1001 }, + { 1, 1 }, { 1, 1 } /* invalid */ + }; + + if( p_sys->p_seq ) block_Release( p_sys->p_seq ); + if( p_sys->p_ext ) block_Release( p_sys->p_ext ); + + p_sys->p_seq = block_Duplicate( p_frag ); + p_sys->i_seq_old = 0; + p_sys->p_ext = NULL; + + p_dec->fmt_out.video.i_width = + ( p_frag->p_buffer[4] << 4)|(p_frag->p_buffer[5] >> 4 ); + p_dec->fmt_out.video.i_height = + ( (p_frag->p_buffer[5]&0x0f) << 8 )|p_frag->p_buffer[6]; + p_sys->i_aspect_ratio_info = p_frag->p_buffer[7] >> 4; + + /* TODO: MPEG1 aspect ratio */ + + p_sys->i_frame_rate = code_to_frame_rate[p_frag->p_buffer[7]&0x0f][0]; + 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; + +#if 0 + 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 + } + else if( p_frag->p_buffer[3] == 0xb5 ) + { + int i_type = p_frag->p_buffer[4] >> 4; + + /* Extention start code */ + if( i_type == 0x01 ) + { + static const int mpeg2_aspect[16][2] = { - /* picture */ - p_sys->i_seq_old++; + {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} + }; - if( p_frag->i_buffer >= 6 ) - { - p_sys->i_temporal_ref = ( p_frag->p_buffer[4] << 2 )|(p_frag->p_buffer[5] >> 6); - p_sys->i_picture_type = ( p_frag->p_buffer[5] >> 3 )&0x03; - } - if( !p_sys->b_frame_slice ) - { - p_sys->i_dts = p_block->i_dts; p_block->i_dts = 0; - p_sys->i_pts = p_block->i_pts; p_block->i_pts = 0; - } - } - else if( p_frag->p_buffer[3] >= 0x01 && p_frag->p_buffer[3] <= 0xaf ) + /* sequence extention */ + 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 ) { - /* Slice */ - p_sys->b_frame_slice = VLC_TRUE; + p_sys->b_seq_progressive = + p_frag->p_buffer[5]&0x08 ? VLC_TRUE : VLC_FALSE; + p_sys->b_low_delay = + p_frag->p_buffer[9]&0x80 ? VLC_TRUE : VLC_FALSE; } - /* Append the block */ - block_ChainAppend( &p_sys->p_frame, p_frag ); +#if 0 + p_dec->fmt_out.video.i_aspect = + mpeg2_aspect[p_sys->i_aspect_ratio_info][0] * + VOUT_ASPECT_FACTOR / + mpeg2_aspect[p_sys->i_aspect_ratio_info][1]; +#endif + + } + else if( i_type == 0x08 ) + { + /* picture extention */ + 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; } - p_sys->p_old = p_sys->p_start; - p_sys->p_start += 4; } -} - -static int mpgv_FindStartCode( uint8_t **pp_start, uint8_t *p_end ) -{ - uint8_t *p = *pp_start; - - for( p = *pp_start; p < p_end - 4; p++ ) + else if( p_frag->p_buffer[3] == 0x00 ) { - if( p[0] == 0 && p[1] == 0 && p[2] == 1 ) + /* Picture start code */ + p_sys->i_seq_old++; + + if( p_frag->i_buffer >= 6 ) { - *pp_start = p; - return VLC_SUCCESS; + p_sys->i_temporal_ref = + ( p_frag->p_buffer[4] << 2 )|(p_frag->p_buffer[5] >> 6); + p_sys->i_picture_type = ( p_frag->p_buffer[5] >> 3 ) & 0x03; } - } - *pp_start = p; - return VLC_EGENERIC; -} + p_sys->i_dts = p_frag->i_dts; + p_sys->i_pts = p_frag->i_pts; + } + else if( p_frag->p_buffer[3] >= 0x01 && p_frag->p_buffer[3] <= 0xaf ) + { + /* Slice start code */ + p_sys->b_frame_slice = VLC_TRUE; + } + /* Append the block */ + block_ChainAppend( &p_sys->p_frame, p_frag ); + return p_pic; +}