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

Michael Niedermayer michaelni at gmx.at
Fri May 31 17:56:02 CEST 2013


On Fri, May 31, 2013 at 10:59:31PM +0800, 平凯 wrote:
> My purpose is not to obtain an average bit rate,I know it will be
> overewritten. And it a temporary number. And now there is no duration very
> likely,and it will be used  to calculate the duration next in this
> function. My purose is duration,not the bit rate , As you know, As a player
> the duration is more important than bit rate.
> 
> If not do this, the function will to caculate the duration use a random bit
> rate,lead to produce is wrong duration when the bit rate is changing.

your patch calculates bitrate and duration wrong.
you can also look at libavcodec/mpegaudio_parser.c for a specific
example where it will be totally wrong. (the bitrates you use as
input are in that case already averages summing them up again will
give you a worse estimate)


> 
> I have test a acc file the bit rate is changing from 120K to 180K,and in
> this function, it use a random value 166 (the last value) to calculate the
> duration ,the duration is 2:40,but the actual  value is 3:28,and use my way
> the duration is 3:27.

ive thrown a dice once with my left hand and once with my right
with my left it gave 3 with my right 5. I thus know that throwing a
dice with my left will approximate PI better than it would with my
right


> 
> you can get the file here
> click
> http://pan.baidu.com/share/link?shareid=574906&uk=1677986196
>  and click 下载(3.19M) button to download it
> 
> 
> 
> 
> 2013/5/31 Michael Niedermayer <michaelni at gmx.at>
> 
> > 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
> >
> > _______________________________________________
> > ffmpeg-devel mailing list
> > ffmpeg-devel at ffmpeg.org
> > http://ffmpeg.org/mailman/listinfo/ffmpeg-devel
> >
> >
> 
> 
> -- 
> ————————————————————————
> 平凯
> e-mail:pingkai010 at gmail.com
> msn:pingkai010 at hotmail.com
> tel:15101537133
> qq:306978600
> _______________________________________________
> ffmpeg-devel mailing list
> ffmpeg-devel at ffmpeg.org
> http://ffmpeg.org/mailman/listinfo/ffmpeg-devel

-- 
Michael     GnuPG fingerprint: 9FF2128B147EF6730BADF133611EC787040B0FAB

Opposition brings concord. Out of discord comes the fairest harmony.
-- Heraclitus
-------------- 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/e9553735/attachment.asc>


More information about the ffmpeg-devel mailing list