[FFmpeg-devel] [PATCH] avformat/utils: improve get duration

Michael Niedermayer michaelni at gmx.at
Fri May 31 12:08:39 CEST 2013


On Fri, May 31, 2013 at 12:46:51AM +0800, Ping Kai wrote:
> improve the accuracy of getting
> duration from bit rate.
> 
> Signed-off-by: Ping Kai <pingkai010 at gmail.com>
> ---
>  libavformat/utils.c |   15 +++++++++++++++
>  1 file changed, 15 insertions(+)
> 
> diff --git a/libavformat/utils.c b/libavformat/utils.c
> index baa7177..9070b85 100644
> --- a/libavformat/utils.c
> +++ b/libavformat/utils.c
> @@ -2775,7 +2775,11 @@ int avformat_find_stream_info(AVFormatContext *ic, AVDictionary **options)
>      int64_t old_offset = avio_tell(ic->pb);
>      int orig_nb_streams = ic->nb_streams;        // new streams might appear, no options for those
>      int flush_codecs = ic->probesize > 0;
> +    int64_t *sample_bit_rate_sum = av_mallocz(ic->nb_streams * sizeof(int64_t));
> +    int *sample_count = av_mallocz(ic->nb_streams * sizeof(int));
>  
> +    if (sample_bit_rate_sum == NULL || sample_count == NULL)
> +        return AVERROR(ENOMEM);
>      if(ic->pb)
>          av_log(ic, AV_LOG_DEBUG, "File position before avformat_find_stream_info() is %"PRId64"\n", avio_tell(ic->pb));
>  
> @@ -2910,6 +2914,11 @@ int avformat_find_stream_info(AVFormatContext *ic, AVDictionary **options)
>              break;
>          }
>  
> +        if (pkt1.stream_index < orig_nb_streams
> +            &&ic->streams[pkt1.stream_index]->codec->bit_rate > 0){
> +            sample_bit_rate_sum += ic->streams[pkt1.stream_index]->codec->bit_rate;
> +            sample_count ++;
> +        }
>          if (ic->flags & AVFMT_FLAG_NOBUFFER) {
>              pkt = &pkt1;
>          } else {
> @@ -3030,6 +3039,10 @@ int avformat_find_stream_info(AVFormatContext *ic, AVDictionary **options)
>          count++;
>      }
>  
> +    for(i=0;i<orig_nb_streams;i++){
> +        if (sample_count[i] > 0)
> +            ic->streams[i]->codec->bit_rate = (int)(sample_bit_rate_sum[i] / sample_count[i]);
> +    }

_if_ a codec or muxer keeps changing and updating bit_rate it will
continue to do so after avformat_find_stream_info() and the average
calculated here will be overwritten
OTOH, if the bit_rate field never changes the average will also be the
same.
also the way you average is wrong, the global bitrate is bits/duration
not neccesarily the average of the indicated bitrates

[...]
-- 
Michael     GnuPG fingerprint: 9FF2128B147EF6730BADF133611EC787040B0FAB

it is not once nor twice but times without number that the same ideas make
their appearance in the world. -- Aristotle
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 198 bytes
Desc: Digital signature
URL: <http://ffmpeg.org/pipermail/ffmpeg-devel/attachments/20130531/a25e69f5/attachment.asc>


More information about the ffmpeg-devel mailing list