X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=modules%2Fdemux%2Favi%2Favi.c;h=42eb4cc2b7eacd82bdfd9eaa63341f35c106da3c;hb=00b95848260cd44f140a05da8aa5baf13580e2d3;hp=8bf5dbcb58fcc23390d19c85893967c7585495ff;hpb=806cf5165824be921bf2402ecf11fd3ee6501f9c;p=vlc diff --git a/modules/demux/avi/avi.c b/modules/demux/avi/avi.c index 8bf5dbcb58..42eb4cc2b7 100644 --- a/modules/demux/avi/avi.c +++ b/modules/demux/avi/avi.c @@ -24,7 +24,12 @@ * Preamble *****************************************************************************/ -#include +#ifdef HAVE_CONFIG_H +# include "config.h" +#endif + +#include +#include #include #include @@ -50,24 +55,22 @@ static int Open ( vlc_object_t * ); static void Close( vlc_object_t * ); -static int pi_index[] = {0,1,2}; +static const int pi_index[] = {0,1,2}; -static const char *ppsz_indexes[] = { N_("Ask"), N_("Always fix"), +static const char *const ppsz_indexes[] = { N_("Ask"), N_("Always fix"), N_("Never fix") }; vlc_module_begin(); set_shortname( "AVI" ); - set_description( _("AVI demuxer") ); - set_capability( "demux2", 212 ); + set_description( N_("AVI demuxer") ); + set_capability( "demux", 212 ); set_category( CAT_INPUT ); set_subcategory( SUBCAT_INPUT_DEMUX ); add_bool( "avi-interleaved", 0, NULL, - INTERLEAVE_TEXT, INTERLEAVE_LONGTEXT, VLC_TRUE ); - change_safe(); + INTERLEAVE_TEXT, INTERLEAVE_LONGTEXT, true ); add_integer( "avi-index", 0, NULL, - INDEX_TEXT, INDEX_LONGTEXT, VLC_FALSE ); - change_safe(); + INDEX_TEXT, INDEX_LONGTEXT, false ); change_integer_list( pi_index, ppsz_indexes, 0 ); set_callbacks( Open, Close ); @@ -109,7 +112,7 @@ typedef struct typedef struct { - vlc_bool_t b_activated; + bool b_activated; unsigned int i_cat; /* AUDIO_ES, VIDEO_ES */ vlc_fourcc_t i_codec; @@ -128,6 +131,9 @@ typedef struct unsigned int i_idxposc; /* numero of chunk */ unsigned int i_idxposb; /* byte in the current chunk */ + /* extra information given to the decoder */ + void *p_extra; + /* For VBR audio only */ unsigned int i_blockno; unsigned int i_blocksize; @@ -141,11 +147,11 @@ struct demux_sys_t mtime_t i_time; mtime_t i_length; - vlc_bool_t b_seekable; - vlc_bool_t b_muxed; + bool b_seekable; + bool b_muxed; avi_chunk_t ck_root; - vlc_bool_t b_odml; + bool b_odml; off_t i_movi_begin; off_t i_movi_lastchunk_pos; /* XXX position of last valid chunk */ @@ -216,7 +222,7 @@ static int Open( vlc_object_t * p_this ) demux_t *p_demux = (demux_t *)p_this; demux_sys_t *p_sys; - vlc_bool_t b_index = VLC_FALSE; + bool b_index = false; int i_do_index; avi_chunk_t ck_riff; @@ -251,8 +257,8 @@ static int Open( vlc_object_t * p_this ) p_sys->i_time = 0; p_sys->i_length = 0; p_sys->i_movi_lastchunk_pos = 0; - p_sys->b_odml = VLC_FALSE; - p_sys->b_muxed = VLC_FALSE; + p_sys->b_odml = false; + p_sys->b_muxed = false; p_sys->i_track = 0; p_sys->track = NULL; p_sys->meta = NULL; @@ -293,7 +299,7 @@ static int Open( vlc_object_t * p_this ) if( p_sysx->i_type == AVIFOURCC_AVIX ) { msg_Warn( p_demux, "detected OpenDML file" ); - p_sys->b_odml = VLC_TRUE; + p_sys->b_odml = true; break; } } @@ -349,6 +355,9 @@ static int Open( vlc_object_t * p_this ) for( i = 0 ; i < i_track; i++ ) { avi_track_t *tk = malloc( sizeof( avi_track_t ) ); + if( !tk ) + goto error; + avi_chunk_list_t *p_strl = AVI_ChunkFind( p_hdrl, AVIFOURCC_strl, i ); avi_chunk_strh_t *p_strh = AVI_ChunkFind( p_strl, AVIFOURCC_strh, 0 ); avi_chunk_STRING_t *p_strn = AVI_ChunkFind( p_strl, AVIFOURCC_strn, 0 ); @@ -356,18 +365,7 @@ static int Open( vlc_object_t * p_this ) avi_chunk_strf_vids_t *p_vids = NULL; es_format_t fmt; - tk->b_activated = VLC_FALSE; - tk->p_index = 0; - tk->i_idxnb = 0; - tk->i_idxmax = 0; - tk->i_idxposc = 0; - tk->i_idxposb = 0; - - tk->i_blockno = 0; - tk->i_blocksize = 0; - - tk->p_es = NULL; - tk->p_out_muxed = NULL; + memset( tk, 0, sizeof( avi_track_t ) ); p_vids = (avi_chunk_strf_vids_t*)AVI_ChunkFind( p_strl, AVIFOURCC_strf, 0 ); p_auds = (avi_chunk_strf_auds_t*)AVI_ChunkFind( p_strl, AVIFOURCC_strf, 0 ); @@ -390,7 +388,9 @@ static int Open( vlc_object_t * p_this ) tk->i_cat = AUDIO_ES; tk->i_codec = AVI_FourccGetCodec( AUDIO_ES, p_auds->p_wf->wFormatTag ); - if( ( tk->i_blocksize = p_auds->p_wf->nBlockAlign ) == 0 ) + if( tk->i_codec == VLC_FOURCC( 'v', 'o', 'r', 'b' ) ) + tk->i_blocksize = 0; /* fix vorbis VBR decoding */ + else if( ( tk->i_blocksize = p_auds->p_wf->nBlockAlign ) == 0 ) { if( p_auds->p_wf->wFormatTag == 1 ) { @@ -409,12 +409,85 @@ static int Open( vlc_object_t * p_this ) fmt.audio.i_blockalign = p_auds->p_wf->nBlockAlign; fmt.audio.i_bitspersample = p_auds->p_wf->wBitsPerSample; fmt.b_packetized = !tk->i_blocksize; + + msg_Dbg( p_demux, + "stream[%d] audio(0x%x) %d channels %dHz %dbits", + i, p_auds->p_wf->wFormatTag, p_auds->p_wf->nChannels, + p_auds->p_wf->nSamplesPerSec, + p_auds->p_wf->wBitsPerSample ); + fmt.i_extra = __MIN( p_auds->p_wf->cbSize, p_auds->i_chunk_size - sizeof(WAVEFORMATEX) ); - fmt.p_extra = &p_auds->p_wf[1]; - msg_Dbg( p_demux, "stream[%d] audio(0x%x) %d channels %dHz %dbits", - i, p_auds->p_wf->wFormatTag, p_auds->p_wf->nChannels, - p_auds->p_wf->nSamplesPerSec, p_auds->p_wf->wBitsPerSample); + fmt.p_extra = tk->p_extra = malloc( fmt.i_extra ); + if( !fmt.p_extra ) goto error; + memcpy( fmt.p_extra, &p_auds->p_wf[1], fmt.i_extra ); + + /* Rewrite the vorbis headers from Xiph-like format + * to VLC internal format + * + * Xiph format: + * - 1st byte == N, is the number of packets - 1 + * - Following bytes are the size of the N first packets: + * while( *p == 0xFF ) { size += 0xFF; p++ } size += *p; + * (the size of the last packet is the size of remaining + * data in the buffer) + * - Finally, all the packets concatenated + * + * VLC format: + * - Size of the packet on 16 bits (big endian) FIXME: should be 32 bits to be safe + * - The packet itself + * - Size of the next packet, and so on ... + */ + + if( tk->i_codec == VLC_FOURCC( 'v', 'o', 'r', 'b' ) ) + { + uint8_t *p_extra = fmt.p_extra; + size_t i_extra = fmt.i_extra; + + if( i_extra <= 1 ) break; + if( *p_extra++ != 2 ) break; /* 3 packets - 1 = 2 */ + i_extra--; + + size_t i_identifier_len = 0; + while( *p_extra == 0xFF ) + { + i_identifier_len += 0xFF; + p_extra++; + if( --i_extra <= 1 ) break; + } + i_identifier_len += *p_extra++; + if( i_identifier_len > --i_extra ) break; + + size_t i_comment_len = 0; + while( *p_extra == 0xFF ) + { + i_comment_len += 0xFF; + p_extra++; + if( --i_extra <= 1 ) break; + } + i_comment_len += *p_extra++; + if( i_comment_len > --i_extra ) break; + size_t i_cookbook_len = i_extra; + + size_t i_headers_size = 3 * 2 + i_identifier_len + + i_comment_len + i_cookbook_len; + uint8_t *p_out = malloc( i_headers_size ); + if( !p_out ) goto error; + free( fmt.p_extra ); + fmt.p_extra = tk->p_extra = p_out; + fmt.i_extra = i_headers_size; + #define copy_packet( len ) \ + *p_out++ = len >> 8; \ + *p_out++ = len & 0xFF; \ + memcpy( p_out, p_extra, len ); \ + p_out += len; \ + p_extra += len; + copy_packet( i_identifier_len ); + copy_packet( i_comment_len ); + copy_packet( i_cookbook_len ); + #undef copy_packet + break; + } break; case( AVIFOURCC_vids ): @@ -473,10 +546,10 @@ static int Open( vlc_object_t * p_this ) __MIN( p_vids->p_bih->biSize - sizeof( BITMAPINFOHEADER ), p_vids->i_chunk_size - sizeof(BITMAPINFOHEADER) ); fmt.p_extra = &p_vids->p_bih[1]; - msg_Dbg( p_demux, "stream[%d] video(%4.4s) %dx%d %dbpp %ffps", + msg_Dbg( p_demux, "stream[%d] video(%4.4s) %"PRIu32"x%"PRIu32" %dbpp %ffps", i, (char*)&p_vids->p_bih->biCompression, - p_vids->p_bih->biWidth, - p_vids->p_bih->biHeight, + (uint32_t)p_vids->p_bih->biWidth, + (uint32_t)p_vids->p_bih->biHeight, p_vids->p_bih->biBitCount, (float)tk->i_rate/(float)tk->i_scale ); @@ -518,11 +591,13 @@ static int Open( vlc_object_t * p_this ) case( AVIFOURCC_iavs): case( AVIFOURCC_ivas): - p_sys->b_muxed = VLC_TRUE; + p_sys->b_muxed = true; msg_Dbg( p_demux, "stream[%d] iavs with handler %4.4s", i, (char *)&p_strh->i_handler ); if( p_strh->i_handler == FOURCC_dvsd || p_strh->i_handler == FOURCC_dvhd || - p_strh->i_handler == FOURCC_dvsl ) + p_strh->i_handler == FOURCC_dvsl || + p_strh->i_handler == FOURCC_dv25 || + p_strh->i_handler == FOURCC_dv50 ) { tk->p_out_muxed = stream_DemuxNew( p_demux, (char *)"rawdv", p_demux->out ); if( !tk->p_out_muxed ) @@ -583,9 +658,13 @@ aviindex: (mtime_t)1000000 ) { msg_Warn( p_demux, "broken or missing index, 'seek' will be " - "axproximative or will have strange behaviour" ); + "approximative or will exhibit strange behavior" ); if( i_do_index == 0 && !b_index ) { + if( !p_sys->b_seekable ) { + b_index = true; + goto aviindex; + } int i_create; i_create = intf_UserYesNo( p_demux, _("AVI Index") , _( "This AVI file is broken. Seeking will not " @@ -594,7 +673,7 @@ aviindex: _( "Repair" ), _( "Don't repair" ), _( "Cancel") ); if( i_create == DIALOG_OK_YES ) { - b_index = VLC_TRUE; + b_index = true; msg_Dbg( p_demux, "Fixing AVI index" ); goto aviindex; } @@ -685,11 +764,12 @@ static void Close ( vlc_object_t * p_this ) { if( p_sys->track[i]->p_out_muxed ) stream_DemuxDelete( p_sys->track[i]->p_out_muxed ); - FREENULL( p_sys->track[i]->p_index ); + free( p_sys->track[i]->p_index ); + free( p_sys->track[i]->p_extra ); free( p_sys->track[i] ); } } - FREENULL( p_sys->track ); + free( p_sys->track ); AVI_ChunkFreeRoot( p_demux->s, &p_sys->ck_root ); vlc_meta_Delete( p_sys->meta ); @@ -705,7 +785,7 @@ static void Close ( vlc_object_t * p_this ) *****************************************************************************/ typedef struct { - vlc_bool_t b_ok; + bool b_ok; int i_toread; @@ -720,7 +800,7 @@ static int Demux_Seekable( demux_t *p_demux ) unsigned int i_track_count = 0; unsigned int i_track; - vlc_bool_t b_stream; + bool b_stream; /* cannot be more than 100 stream (dcXX or wbXX) */ avi_track_toread_t toread[100]; @@ -729,12 +809,12 @@ static int Demux_Seekable( demux_t *p_demux ) for( i_track = 0; i_track < p_sys->i_track; i_track++ ) { avi_track_t *tk = p_sys->track[i_track]; - vlc_bool_t b; + bool b; if( p_sys->b_muxed && tk->p_out_muxed ) { i_track_count++; - tk->b_activated = VLC_TRUE; + tk->b_activated = true; continue; } @@ -745,11 +825,11 @@ static int Demux_Seekable( demux_t *p_demux ) { AVI_TrackSeek( p_demux, i_track, p_sys->i_time ); } - tk->b_activated = VLC_TRUE; + tk->b_activated = true; } else if( !b && tk->b_activated ) { - tk->b_activated = VLC_FALSE; + tk->b_activated = false; } if( b ) { @@ -813,19 +893,19 @@ static int Demux_Seekable( demux_t *p_demux ) } } - b_stream = VLC_FALSE; + b_stream = false; for( ;; ) { avi_track_t *tk; - vlc_bool_t b_done; + bool b_done; block_t *p_frame; off_t i_pos; unsigned int i; size_t i_size; /* search for first chunk to be read */ - for( i = 0, b_done = VLC_TRUE, i_pos = -1; i < p_sys->i_track; i++ ) + for( i = 0, b_done = true, i_pos = -1; i < p_sys->i_track; i++ ) { if( !toread[i].b_ok || AVI_GetDPTS( p_sys->track[i], @@ -836,7 +916,7 @@ static int Demux_Seekable( demux_t *p_demux ) if( toread[i].i_toread > 0 ) { - b_done = VLC_FALSE; /* not yet finished */ + b_done = false; /* not yet finished */ } if( toread[i].i_posf > 0 ) { @@ -897,7 +977,7 @@ static int Demux_Seekable( demux_t *p_demux ) * affect the reading speed too much. */ if( !(++i_loop_count % 1024) ) { - if( p_demux->b_die ) return -1; + if( !vlc_object_alive (p_demux) ) return -1; msleep( 10000 ); if( !(i_loop_count % (1024 * 10)) ) @@ -981,8 +1061,8 @@ static int Demux_Seekable( demux_t *p_demux ) if( ( p_frame = stream_Block( p_demux->s, __EVEN( i_size ) ) )==NULL ) { msg_Warn( p_demux, "failed reading data" ); - tk->b_activated = VLC_FALSE; - toread[i_track].b_ok = VLC_FALSE; + tk->b_activated = false; + toread[i_track].b_ok = false; continue; } if( i_size % 2 ) /* read was padded on word boundary */ @@ -1048,7 +1128,7 @@ static int Demux_Seekable( demux_t *p_demux ) toread[i_track].i_posf = -1; } - b_stream = VLC_TRUE; /* at least one read succeed */ + b_stream = true; /* at least one read succeed */ if( tk->i_cat != VIDEO_ES ) p_frame->i_dts = p_frame->i_pts; @@ -1092,7 +1172,7 @@ static int Demux_UnSeekable( demux_t *p_demux ) for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ ) { avi_track_t *tk = p_sys->track[i_stream]; - vlc_bool_t b; + bool b; es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE, tk->p_es, &b ); @@ -1216,7 +1296,7 @@ static int Seek( demux_t *p_demux, mtime_t i_date, int i_percent ) demux_sys_t *p_sys = p_demux->p_sys; unsigned int i_stream; - msg_Dbg( p_demux, "seek requested: "I64Fd" seconds %d%%", + msg_Dbg( p_demux, "seek requested: %"PRId64" seconds %d%%", i_date / 1000000, i_percent ); if( p_sys->b_seekable ) @@ -1276,7 +1356,7 @@ static int Seek( demux_t *p_demux, mtime_t i_date, int i_percent ) i_date = AVI_GetPTS( p_stream ); /* TODO better support for i_samplesize != 0 */ - msg_Dbg( p_demux, "estimate date "I64Fd, i_date ); + msg_Dbg( p_demux, "estimate date %"PRId64, i_date ); } /* */ @@ -1290,7 +1370,7 @@ static int Seek( demux_t *p_demux, mtime_t i_date, int i_percent ) AVI_TrackSeek( p_demux, i_stream, i_date ); } p_sys->i_time = i_date; - msg_Dbg( p_demux, "seek: "I64Fd" seconds", p_sys->i_time /1000000 ); + msg_Dbg( p_demux, "seek: %"PRId64" seconds", p_sys->i_time /1000000 ); return VLC_SUCCESS; } else @@ -1515,7 +1595,7 @@ static int AVI_StreamChunkFind( demux_t *p_demux, unsigned int i_stream ) for( ;; ) { - if( p_demux->b_die ) return VLC_EGENERIC; + if( !vlc_object_alive (p_demux) ) return VLC_EGENERIC; if( AVI_PacketGetHeader( p_demux, &avi_pk ) ) { @@ -1535,7 +1615,7 @@ static int AVI_StreamChunkFind( demux_t *p_demux, unsigned int i_stream ) * affect the reading speed too much. */ if( !(++i_loop_count % 1024) ) { - if( p_demux->b_die ) return VLC_EGENERIC; + if( !vlc_object_alive (p_demux) ) return VLC_EGENERIC; msleep( 10000 ); if( !(i_loop_count % (1024 * 10)) ) @@ -1698,7 +1778,7 @@ static int AVI_TrackSeek( demux_t *p_demux, } msg_Dbg( p_demux, - "old:"I64Fd" %s new "I64Fd, + "old:%"PRId64" %s new %"PRId64, i_oldpts, i_oldpts > i_date ? ">" : "<", i_date ); @@ -1754,7 +1834,7 @@ static int AVI_TrackSeek( demux_t *p_demux, } /**************************************************************************** - * Return VLC_TRUE if it's a key frame + * Return true if it's a key frame ****************************************************************************/ static int AVI_GetKeyFlag( vlc_fourcc_t i_fourcc, uint8_t *p_byte ) { @@ -2018,7 +2098,7 @@ static int AVI_PacketSearch( demux_t *p_demux ) * this code is called only on broken files). */ if( !(++i_count % 1024) ) { - if( p_demux->b_die ) return VLC_EGENERIC; + if( !vlc_object_alive (p_demux) ) return VLC_EGENERIC; msleep( 10000 ); if( !(i_count % (1024 * 10)) ) @@ -2081,7 +2161,7 @@ static int AVI_IndexLoad_idx1( demux_t *p_demux ) off_t i_offset; unsigned int i; - vlc_bool_t b_keyset[100]; + bool b_keyset[100]; p_riff = AVI_ChunkFind( &p_sys->ck_root, AVIFOURCC_RIFF, 0); p_idx1 = AVI_ChunkFind( p_riff, AVIFOURCC_idx1, 0); @@ -2108,7 +2188,7 @@ static int AVI_IndexLoad_idx1( demux_t *p_demux ) /* Reset b_keyset */ for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ ) - b_keyset[i_stream] = VLC_FALSE; + b_keyset[i_stream] = false; for( i_index = 0; i_index < p_idx1->i_entry_count; i_index++ ) { @@ -2129,7 +2209,7 @@ static int AVI_IndexLoad_idx1( demux_t *p_demux ) AVI_IndexAddEntry( p_sys, i_stream, &index ); if( index.i_flags&AVIIF_KEYFRAME ) - b_keyset[i_stream] = VLC_TRUE; + b_keyset[i_stream] = true; } } @@ -2314,7 +2394,7 @@ static void AVI_IndexCreate( demux_t *p_demux ) { avi_packet_t pk; - if( p_demux->b_die ) + if( !vlc_object_alive (p_demux) ) { return; } @@ -2409,19 +2489,19 @@ static int AVI_TrackStopFinishedStreams( demux_t *p_demux ) { demux_sys_t *p_sys = p_demux->p_sys; unsigned int i; - int b_end = VLC_TRUE; + int b_end = true; for( i = 0; i < p_sys->i_track; i++ ) { avi_track_t *tk = p_sys->track[i]; if( tk->i_idxposc >= tk->i_idxnb ) { - tk->b_activated = VLC_FALSE; - if( tk->p_es ) es_out_Control( p_demux->out, ES_OUT_SET_ES_STATE, tk->p_es, VLC_FALSE ); + tk->b_activated = false; + if( tk->p_es ) es_out_Control( p_demux->out, ES_OUT_SET_ES_STATE, tk->p_es, false ); } else { - b_end = VLC_FALSE; + b_end = false; } } return( b_end ); @@ -2460,7 +2540,7 @@ static mtime_t AVI_MovieGetLength( demux_t *p_demux ) i_length /= (mtime_t)1000000; /* in seconds */ msg_Dbg( p_demux, - "stream[%d] length:"I64Fd" (based on index)", + "stream[%d] length:%"PRId64" (based on index)", i, i_length ); i_maxlength = __MAX( i_maxlength, i_length );