[FFmpeg-cvslog] ac3enc: choose the closest bit rate to the one requested instead of failing

Justin Ruggles git at videolan.org
Thu Mar 1 03:20:21 CET 2012


ffmpeg | branch: master | Justin Ruggles <justin.ruggles at gmail.com> | Wed Feb 22 21:26:09 2012 -0500| [6aeea1dfb2f21de959701ee7dfaab59b4634e570] | committer: Justin Ruggles

ac3enc: choose the closest bit rate to the one requested instead of failing

> http://git.videolan.org/gitweb.cgi/ffmpeg.git/?a=commit;h=6aeea1dfb2f21de959701ee7dfaab59b4634e570
---

 libavcodec/ac3enc.c |   17 +++++++++++------
 1 files changed, 11 insertions(+), 6 deletions(-)

diff --git a/libavcodec/ac3enc.c b/libavcodec/ac3enc.c
index c577c87..669be23 100644
--- a/libavcodec/ac3enc.c
+++ b/libavcodec/ac3enc.c
@@ -2189,15 +2189,20 @@ static av_cold int validate_options(AC3EncodeContext *s)
             wpf--;
         s->frame_size_min = 2 * wpf;
     } else {
+        int best_br = 0, best_code = 0, best_diff = INT_MAX;
         for (i = 0; i < 19; i++) {
-            if ((ff_ac3_bitrate_tab[i] >> s->bit_alloc.sr_shift)*1000 == avctx->bit_rate)
+            int br   = (ff_ac3_bitrate_tab[i] >> s->bit_alloc.sr_shift) * 1000;
+            int diff = abs(br - avctx->bit_rate);
+            if (diff < best_diff) {
+                best_br   = br;
+                best_code = i;
+                best_diff = diff;
+            }
+            if (!best_diff)
                 break;
         }
-        if (i == 19) {
-            av_log(avctx, AV_LOG_ERROR, "invalid bit rate\n");
-            return AVERROR(EINVAL);
-        }
-        s->frame_size_code = i << 1;
+        avctx->bit_rate    = best_br;
+        s->frame_size_code = best_code << 1;
         s->frame_size_min  = 2 * ff_ac3_frame_size_tab[s->frame_size_code][s->bit_alloc.sr_code];
         s->num_blks_code   = 0x3;
         s->num_blocks      = 6;



More information about the ffmpeg-cvslog mailing list