📄 avi.c
字号:
{ stream_Seek( p_demux->s, p_sys->i_movi_begin + 12 ); } for( ;; ) { if( !vlc_object_alive (p_demux) ) return VLC_EGENERIC; if( AVI_PacketGetHeader( p_demux, &avi_pk ) ) { msg_Warn( p_demux, "cannot get packet header" ); return VLC_EGENERIC; } if( avi_pk.i_stream >= p_sys->i_track || ( avi_pk.i_cat != AUDIO_ES && avi_pk.i_cat != VIDEO_ES ) ) { if( AVI_PacketNext( p_demux ) ) { return VLC_EGENERIC; } /* Prevents from eating all the CPU with broken files. * This value should be low enough so that it doesn't * affect the reading speed too much. */ if( !(++i_loop_count % 1024) ) { if( !vlc_object_alive (p_demux) ) return VLC_EGENERIC; msleep( 10000 ); if( !(i_loop_count % (1024 * 10)) ) msg_Warn( p_demux, "don't seem to find any data..." ); } } else { /* add this chunk to the index */ avi_entry_t index; index.i_id = avi_pk.i_fourcc; index.i_flags = AVI_GetKeyFlag(p_sys->track[avi_pk.i_stream]->i_codec, avi_pk.i_peek); index.i_pos = avi_pk.i_pos; index.i_length = avi_pk.i_size; AVI_IndexAddEntry( p_sys, avi_pk.i_stream, &index ); if( avi_pk.i_stream == i_stream ) { return VLC_SUCCESS; } if( AVI_PacketNext( p_demux ) ) { return VLC_EGENERIC; } } }}/* be sure that i_ck will be a valid index entry */static int AVI_StreamChunkSet( demux_t *p_demux, unsigned int i_stream, unsigned int i_ck ){ demux_sys_t *p_sys = p_demux->p_sys; avi_track_t *p_stream = p_sys->track[i_stream]; p_stream->i_idxposc = i_ck; p_stream->i_idxposb = 0; if( i_ck >= p_stream->i_idxnb ) { p_stream->i_idxposc = p_stream->i_idxnb - 1; do { p_stream->i_idxposc++; if( AVI_StreamChunkFind( p_demux, i_stream ) ) { return VLC_EGENERIC; } } while( p_stream->i_idxposc < i_ck ); } return VLC_SUCCESS;}/* XXX FIXME up to now, we assume that all chunk are one after one */static int AVI_StreamBytesSet( demux_t *p_demux, unsigned int i_stream, off_t i_byte ){ demux_sys_t *p_sys = p_demux->p_sys; avi_track_t *p_stream = p_sys->track[i_stream]; if( ( p_stream->i_idxnb > 0 ) &&( i_byte < p_stream->p_index[p_stream->i_idxnb - 1].i_lengthtotal + p_stream->p_index[p_stream->i_idxnb - 1].i_length ) ) { /* index is valid to find the ck */ /* uses dichototmie to be fast enougth */ int i_idxposc = __MIN( p_stream->i_idxposc, p_stream->i_idxnb - 1 ); int i_idxmax = p_stream->i_idxnb; int i_idxmin = 0; for( ;; ) { if( p_stream->p_index[i_idxposc].i_lengthtotal > i_byte ) { i_idxmax = i_idxposc ; i_idxposc = ( i_idxmin + i_idxposc ) / 2 ; } else { if( p_stream->p_index[i_idxposc].i_lengthtotal + p_stream->p_index[i_idxposc].i_length <= i_byte) { i_idxmin = i_idxposc ; i_idxposc = (i_idxmax + i_idxposc ) / 2 ; } else { p_stream->i_idxposc = i_idxposc; p_stream->i_idxposb = i_byte - p_stream->p_index[i_idxposc].i_lengthtotal; return VLC_SUCCESS; } } } } else { p_stream->i_idxposc = p_stream->i_idxnb - 1; p_stream->i_idxposb = 0; do { p_stream->i_idxposc++; if( AVI_StreamChunkFind( p_demux, i_stream ) ) { return VLC_EGENERIC; } } while( p_stream->p_index[p_stream->i_idxposc].i_lengthtotal + p_stream->p_index[p_stream->i_idxposc].i_length <= i_byte ); p_stream->i_idxposb = i_byte - p_stream->p_index[p_stream->i_idxposc].i_lengthtotal; return VLC_SUCCESS; }}static int AVI_TrackSeek( demux_t *p_demux, int i_stream, mtime_t i_date ){ demux_sys_t *p_sys = p_demux->p_sys; avi_track_t *tk = p_sys->track[i_stream];#define p_stream p_sys->track[i_stream] mtime_t i_oldpts; i_oldpts = AVI_GetPTS( p_stream ); if( !p_stream->i_samplesize ) { if( AVI_StreamChunkSet( p_demux, i_stream, AVI_PTSToChunk( p_stream, i_date ) ) ) { return VLC_EGENERIC; } if( p_stream->i_cat == AUDIO_ES ) { unsigned int i; tk->i_blockno = 0; for( i = 0; i < tk->i_idxposc; i++ ) { if( tk->i_blocksize > 0 ) { tk->i_blockno += ( tk->p_index[i].i_length + tk->i_blocksize - 1 ) / tk->i_blocksize; } else { tk->i_blockno++; } } } msg_Dbg( p_demux, "old:%"PRId64" %s new %"PRId64, i_oldpts, i_oldpts > i_date ? ">" : "<", i_date ); if( p_stream->i_cat == VIDEO_ES ) { /* search key frame */ //if( i_date < i_oldpts || 1 ) { while( p_stream->i_idxposc > 0 && !( p_stream->p_index[p_stream->i_idxposc].i_flags & AVIIF_KEYFRAME ) ) { if( AVI_StreamChunkSet( p_demux, i_stream, p_stream->i_idxposc - 1 ) ) { return VLC_EGENERIC; } } if( p_stream->p_es ) es_out_Control( p_demux->out, ES_OUT_SET_NEXT_DISPLAY_TIME, p_stream->p_es, i_date ); }#if 0 else { while( p_stream->i_idxposc < p_stream->i_idxnb && !( p_stream->p_index[p_stream->i_idxposc].i_flags & AVIIF_KEYFRAME ) ) { if( AVI_StreamChunkSet( p_demux, i_stream, p_stream->i_idxposc + 1 ) ) { return VLC_EGENERIC; } } }#endif } } else { if( AVI_StreamBytesSet( p_demux, i_stream, AVI_PTSToByte( p_stream, i_date ) ) ) { return VLC_EGENERIC; } } return VLC_SUCCESS;#undef p_stream}/**************************************************************************** * Return true if it's a key frame ****************************************************************************/static int AVI_GetKeyFlag( vlc_fourcc_t i_fourcc, uint8_t *p_byte ){ switch( i_fourcc ) { case FOURCC_DIV1: /* we have: * startcode: 0x00000100 32bits * framenumber ? 5bits * piture type 0(I),1(P) 2bits */ if( GetDWBE( p_byte ) != 0x00000100 ) { /* it's not an msmpegv1 stream, strange...*/ return AVIIF_KEYFRAME; } return p_byte[4] & 0x06 ? 0 : AVIIF_KEYFRAME; case FOURCC_DIV2: case FOURCC_DIV3: /* wmv1 also */ /* we have * picture type 0(I),1(P) 2bits */ return p_byte[0] & 0xC0 ? 0 : AVIIF_KEYFRAME; case FOURCC_mp4v: /* we should find first occurrence of 0x000001b6 (32bits) * startcode: 0x000001b6 32bits * piture type 0(I),1(P) 2bits */ if( GetDWBE( p_byte ) != 0x000001b6 ) { /* not true , need to find the first VOP header */ return AVIIF_KEYFRAME; } return p_byte[4] & 0xC0 ? 0 : AVIIF_KEYFRAME; default: /* I can't do it, so say yes */ return AVIIF_KEYFRAME; }}vlc_fourcc_t AVI_FourccGetCodec( unsigned int i_cat, vlc_fourcc_t i_codec ){ switch( i_cat ) { case AUDIO_ES: wf_tag_to_fourcc( i_codec, &i_codec, NULL ); return i_codec; case VIDEO_ES: /* XXX DIV1 <- msmpeg4v1, DIV2 <- msmpeg4v2, DIV3 <- msmpeg4v3, mp4v for mpeg4 */ switch( i_codec ) { case FOURCC_1: return VLC_FOURCC('m','r','l','e'); case FOURCC_DIV1: case FOURCC_div1: case FOURCC_MPG4: case FOURCC_mpg4: return FOURCC_DIV1; case FOURCC_DIV2: case FOURCC_div2: case FOURCC_MP42: case FOURCC_mp42: case FOURCC_MPG3: case FOURCC_mpg3: return FOURCC_DIV2; case FOURCC_div3: case FOURCC_MP43: case FOURCC_mp43: case FOURCC_DIV3: case FOURCC_DIV4: case FOURCC_div4: case FOURCC_DIV5: case FOURCC_div5: case FOURCC_DIV6: case FOURCC_div6: case FOURCC_AP41: case FOURCC_3IV1: case FOURCC_3iv1: case FOURCC_3IVD: case FOURCC_3ivd: case FOURCC_3VID: case FOURCC_3vid: return FOURCC_DIV3; case FOURCC_DIVX: case FOURCC_divx: case FOURCC_MP4S: case FOURCC_mp4s: case FOURCC_M4S2: case FOURCC_m4s2: case FOURCC_xvid: case FOURCC_XVID: case FOURCC_XviD: case FOURCC_DX50: case FOURCC_dx50: case FOURCC_mp4v: case FOURCC_4: case FOURCC_3IV2: case FOURCC_3iv2: return FOURCC_mp4v; } default: return VLC_FOURCC( 'u', 'n', 'd', 'f' ); }}/**************************************************************************** * ****************************************************************************/static void AVI_ParseStreamHeader( vlc_fourcc_t i_id, unsigned int *pi_number, unsigned int *pi_type ){#define SET_PTR( p, v ) if( p ) *(p) = (v); int c1, c2; c1 = ((uint8_t *)&i_id)[0]; c2 = ((uint8_t *)&i_id)[1]; if( c1 < '0' || c1 > '9' || c2 < '0' || c2 > '9' ) { SET_PTR( pi_number, 100 ); /* > max stream number */ SET_PTR( pi_type, UNKNOWN_ES ); } else { SET_PTR( pi_number, (c1 - '0') * 10 + (c2 - '0' ) ); switch( VLC_TWOCC( ((uint8_t *)&i_id)[2], ((uint8_t *)&i_id)[3] ) ) { case AVITWOCC_wb: SET_PTR( pi_type, AUDIO_ES ); break; case AVITWOCC_dc: case AVITWOCC_db: SET_PTR( pi_type, VIDEO_ES ); break; default: SET_PTR( pi_type, UNKNOWN_ES ); break; } }#undef SET_PTR}/**************************************************************************** * ****************************************************************************/static int AVI_PacketGetHeader( demux_t *p_demux, avi_packet_t *p_pk ){ const uint8_t *p_peek; if( stream_Pee
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -