📄 demux.c
字号:
psz_type, (char*)&fcc ); TAB_APPEND( p_sys->i_tk, p_sys->tk, es ); } msg_Dbg( p_demux, "AVFormat supported stream" ); msg_Dbg( p_demux, " - format = %s (%s)", p_sys->fmt->name, p_sys->fmt->long_name ); msg_Dbg( p_demux, " - start time = %"PRId64, ( p_sys->ic->start_time != (int64_t)AV_NOPTS_VALUE ) ? p_sys->ic->start_time * 1000000 / AV_TIME_BASE : -1 ); msg_Dbg( p_demux, " - duration = %"PRId64, ( p_sys->ic->duration != (int64_t)AV_NOPTS_VALUE ) ? p_sys->ic->duration * 1000000 / AV_TIME_BASE : -1 ); return VLC_SUCCESS;}/***************************************************************************** * Close *****************************************************************************/void CloseDemux( vlc_object_t *p_this ){ demux_t *p_demux = (demux_t*)p_this; demux_sys_t *p_sys = p_demux->p_sys; bool b_avfmt_nofile; FREENULL( p_sys->tk ); b_avfmt_nofile = p_sys->fmt->flags & AVFMT_NOFILE; p_sys->fmt->flags |= AVFMT_NOFILE; /* libavformat must not fopen/fclose */ if( p_sys->ic ) av_close_input_file( p_sys->ic ); if( !b_avfmt_nofile ) p_sys->fmt->flags ^= AVFMT_NOFILE; free( p_sys->io_buffer ); free( p_sys );}/***************************************************************************** * Demux: *****************************************************************************/static int Demux( demux_t *p_demux ){ demux_sys_t *p_sys = p_demux->p_sys; AVPacket pkt; block_t *p_frame; int64_t i_start_time; /* Read a frame */ if( av_read_frame( p_sys->ic, &pkt ) ) { return 0; } if( pkt.stream_index < 0 || pkt.stream_index >= p_sys->i_tk ) { av_free_packet( &pkt ); return 1; } if( ( p_frame = block_New( p_demux, pkt.size ) ) == NULL ) { return 0; } memcpy( p_frame->p_buffer, pkt.data, pkt.size ); if( pkt.flags & PKT_FLAG_KEY ) p_frame->i_flags |= BLOCK_FLAG_TYPE_I; i_start_time = ( p_sys->ic->start_time != (int64_t)AV_NOPTS_VALUE ) ? ( p_sys->ic->start_time * 1000000 / AV_TIME_BASE ) : 0; p_frame->i_dts = ( pkt.dts == (int64_t)AV_NOPTS_VALUE ) ? 0 : (pkt.dts) * 1000000 * p_sys->ic->streams[pkt.stream_index]->time_base.num / p_sys->ic->streams[pkt.stream_index]->time_base.den - i_start_time; p_frame->i_pts = ( pkt.pts == (int64_t)AV_NOPTS_VALUE ) ? 0 : (pkt.pts) * 1000000 * p_sys->ic->streams[pkt.stream_index]->time_base.num / p_sys->ic->streams[pkt.stream_index]->time_base.den - i_start_time;#ifdef AVFORMAT_DEBUG msg_Dbg( p_demux, "tk[%d] dts=%"PRId64" pts=%"PRId64, pkt.stream_index, p_frame->i_dts, p_frame->i_pts );#endif if( pkt.dts > 0 && ( pkt.stream_index == p_sys->i_pcr_tk || p_sys->i_pcr_tk < 0 ) ) { p_sys->i_pcr_tk = pkt.stream_index; p_sys->i_pcr = p_frame->i_dts; es_out_Control( p_demux->out, ES_OUT_SET_PCR, (int64_t)p_sys->i_pcr ); } es_out_Send( p_demux->out, p_sys->tk[pkt.stream_index], p_frame ); av_free_packet( &pkt ); return 1;}/***************************************************************************** * Control: *****************************************************************************/static int Control( demux_t *p_demux, int i_query, va_list args ){ demux_sys_t *p_sys = p_demux->p_sys; double f, *pf; int64_t i64, *pi64; switch( i_query ) { case DEMUX_GET_POSITION: pf = (double*) va_arg( args, double* ); *pf = 0.0; i64 = stream_Size( p_demux->s ); if( i64 > 0 ) { *pf = (double)stream_Tell( p_demux->s ) / (double)i64; } if( (p_sys->ic->duration != (int64_t)AV_NOPTS_VALUE) && (p_sys->i_pcr > 0) ) { *pf = (double)p_sys->i_pcr / (double)p_sys->ic->duration; } return VLC_SUCCESS; case DEMUX_SET_POSITION: f = (double) va_arg( args, double ); i64 = stream_Tell( p_demux->s ); if( p_sys->i_pcr > 0 ) { i64 = p_sys->ic->duration * f; if( p_sys->ic->start_time != (int64_t)AV_NOPTS_VALUE ) i64 += p_sys->ic->start_time; msg_Warn( p_demux, "DEMUX_SET_POSITION: %"PRId64, i64 ); /* If we have a duration, we prefer to seek by time but if we don't, or if the seek fails, try BYTE seeking */ if( p_sys->ic->duration == (int64_t)AV_NOPTS_VALUE || (av_seek_frame( p_sys->ic, -1, i64, 0 ) < 0) ) { int64_t i_size = stream_Size( p_demux->s ); i64 = (int64_t)i_size * f; msg_Warn( p_demux, "DEMUX_SET_BYTE_POSITION: %"PRId64, i64 ); if( av_seek_frame( p_sys->ic, -1, i64, AVSEEK_FLAG_BYTE ) < 0 ) return VLC_EGENERIC; } es_out_Control( p_demux->out, ES_OUT_RESET_PCR ); p_sys->i_pcr = -1; /* Invalidate time display */ } return VLC_SUCCESS; case DEMUX_GET_LENGTH: pi64 = (int64_t*)va_arg( args, int64_t * ); if( p_sys->ic->duration != (int64_t)AV_NOPTS_VALUE ) { *pi64 = p_sys->ic->duration; } else *pi64 = 0; return VLC_SUCCESS; case DEMUX_GET_TIME: pi64 = (int64_t*)va_arg( args, int64_t * ); *pi64 = p_sys->i_pcr; return VLC_SUCCESS; case DEMUX_SET_TIME: i64 = (int64_t)va_arg( args, int64_t ); i64 = i64 *AV_TIME_BASE / 1000000; if( p_sys->ic->start_time != (int64_t)AV_NOPTS_VALUE ) i64 += p_sys->ic->start_time; msg_Warn( p_demux, "DEMUX_SET_TIME: %"PRId64, i64 ); if( av_seek_frame( p_sys->ic, -1, i64, 0 ) < 0 ) { return VLC_EGENERIC; } es_out_Control( p_demux->out, ES_OUT_RESET_PCR ); p_sys->i_pcr = -1; /* Invalidate time display */ return VLC_SUCCESS; case DEMUX_GET_META: { vlc_meta_t *p_meta = (vlc_meta_t*)va_arg( args, vlc_meta_t* ); if( !p_sys->ic->title[0] || !p_sys->ic->author[0] || !p_sys->ic->copyright[0] || !p_sys->ic->comment[0] || /*!p_sys->ic->album[0] ||*/ !p_sys->ic->genre[0] ) { return VLC_EGENERIC; } if( p_sys->ic->title[0] ) vlc_meta_SetTitle( p_meta, p_sys->ic->title ); if( p_sys->ic->author[0] ) vlc_meta_SetArtist( p_meta, p_sys->ic->author ); if( p_sys->ic->copyright[0] ) vlc_meta_SetCopyright( p_meta, p_sys->ic->copyright ); if( p_sys->ic->comment[0] ) vlc_meta_SetDescription( p_meta, p_sys->ic->comment ); if( p_sys->ic->genre[0] ) vlc_meta_SetGenre( p_meta, p_sys->ic->genre ); return VLC_SUCCESS; } default: return VLC_EGENERIC; }}/***************************************************************************** * I/O wrappers for libavformat *****************************************************************************/static int IORead( void *opaque, uint8_t *buf, int buf_size ){ URLContext *p_url = opaque; demux_t *p_demux = p_url->priv_data; int i_ret = stream_Read( p_demux->s, buf, buf_size ); return i_ret ? i_ret : -1;}static offset_t IOSeek( void *opaque, offset_t offset, int whence ){ URLContext *p_url = opaque; demux_t *p_demux = p_url->priv_data; int64_t i_absolute = (int64_t)offset; int64_t i_size = stream_Size( p_demux->s );#ifdef AVFORMAT_DEBUG msg_Warn( p_demux, "IOSeek offset: %"PRId64", whence: %i", offset, whence );#endif switch( whence ) {#ifdef AVSEEK_SIZE case AVSEEK_SIZE: return i_size;#endif case SEEK_SET: i_absolute = (int64_t)offset; break; case SEEK_CUR: i_absolute = stream_Tell( p_demux->s ) + (int64_t)offset; break; case SEEK_END: i_absolute = i_size + (int64_t)offset; break; default: return -1; } if( i_absolute < 0 ) i_absolute = 0; if( i_size > 0 && i_absolute >= i_size ) { msg_Dbg( p_demux, "Trying to seek too far : EOF?" ); return -1; } if( stream_Seek( p_demux->s, i_absolute ) ) { msg_Warn( p_demux, "we were not allowed to seek, or EOF " ); return -1; } return stream_Tell( p_demux->s );}#endif /* HAVE_LIBAVFORMAT_AVFORMAT_H */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -