[FFmpeg-devel] [FFmpeg-cvslog] lavc/aac_ac3_parser: improve the raw AAC file bit rate calculation

Alexander Strasser eclipse7 at gmx.net
Sun Jun 28 00:30:39 EEST 2020


On 2020-06-26 01:56 +0000, Jun Zhao wrote:
> ffmpeg | branch: master | Jun Zhao <barryjzhao at tencent.com> | Sun May 17 12:10:05 2020 +0800| [60d79b1df9d4c6030010ccb0c134ede9e33158c2] | committer: Jun Zhao
>
> lavc/aac_ac3_parser: improve the raw AAC file bit rate calculation
>
> Now we just use one ADTS raw frame to calculate the bit rate, it's
> lead to a larger error when get the duration from bit rate, the
> improvement cumulate Nth ADTS frames to get the average bit rate.
>
> e,g used the command get the duration like:
> ffprobe -show_entries format=duration -i fate-suite/aac/foo.aac
>
> before this improvement dump the duration=2.173935
> after this improvement  dump the duration=1.979267
>
> in fact, the real duration can be get by command like:
> ffmpeg -i fate-suite/aac/foo.aac -f null /dev/null with time=00:00:01.97
>
> Also update the fate-adtstoasc_ticket3715.
>
> Signed-off-by: Jun Zhao <barryjzhao at tencent.com>
>
> > http://git.videolan.org/gitweb.cgi/ffmpeg.git/?a=commit;h=60d79b1df9d4c6030010ccb0c134ede9e33158c2
> ---
>
>  libavcodec/aac_ac3_parser.c         | 9 +++++++--
>  libavcodec/aac_ac3_parser.h         | 2 ++
>  tests/ref/fate/adtstoasc_ticket3715 | 2 +-
>  3 files changed, 10 insertions(+), 3 deletions(-)
>
> diff --git a/libavcodec/aac_ac3_parser.c b/libavcodec/aac_ac3_parser.c
> index 54e459844f..0746798dab 100644
> --- a/libavcodec/aac_ac3_parser.c
> +++ b/libavcodec/aac_ac3_parser.c
> @@ -97,8 +97,13 @@ get_next:
>              avctx->audio_service_type = s->service_type;
>          }
>
> -        if (avctx->codec_id != AV_CODEC_ID_EAC3)
> -            avctx->bit_rate = s->bit_rate;
> +        /* Calculate the average bit rate */
> +        s->frame_number++;
> +        if (avctx->codec_id != AV_CODEC_ID_EAC3) {
> +            avctx->bit_rate =
> +                (s->last_bit_rate * (s->frame_number -1) + s->bit_rate)/s->frame_number;
> +            s->last_bit_rate = avctx->bit_rate;
> +        }
>      }

Wouldn't it be better to sum up the individual bit_rate values in
a private context variable and divide by number of frames?

This way or the other, it might be useful to think about maximum
values of the total bits? I suspect the current int calculation
might overflow.


  Alexander

>      return i;
> diff --git a/libavcodec/aac_ac3_parser.h b/libavcodec/aac_ac3_parser.h
> index c2506a5bfd..b04041f69d 100644
> --- a/libavcodec/aac_ac3_parser.h
> +++ b/libavcodec/aac_ac3_parser.h
> @@ -55,6 +55,8 @@ typedef struct AACAC3ParseContext {
>      uint64_t state;
>
>      int need_next_header;
> +    int frame_number;
> +    int last_bit_rate;
>      enum AVCodecID codec_id;
>  } AACAC3ParseContext;


More information about the ffmpeg-devel mailing list