diff --git a/libavcodec/aaccoder.c b/libavcodec/aaccoder.c index e8a61cef6cdad7e36567ea5e4e07f69f1a0fc17b..14862ec3c1b980bd8ba7bf927634792aa2b23ed0 100644 --- a/libavcodec/aaccoder.c +++ b/libavcodec/aaccoder.c @@ -261,9 +261,9 @@ static void search_for_quantizers_anmr(AVCodecContext *avctx, AACEncContext *s, } //minimum scalefactor index is when minimum nonzero coefficient after quantizing is not clipped - q0 = coef2minsf(q0f); + q0 = av_clip(coef2minsf(q0f), 0, SCALE_MAX_POS-1); //maximum scalefactor index is when maximum coefficient after quantizing is still not zero - q1 = coef2maxsf(q1f); + q1 = av_clip(coef2maxsf(q1f), 1, SCALE_MAX_POS); if (q1 - q0 > 60) { int q0low = q0; int q1high = q1; @@ -279,6 +279,12 @@ static void search_for_quantizers_anmr(AVCodecContext *avctx, AACEncContext *s, q1 = q1high; } } + // q0 == q1 isn't really a legal situation + if (q0 == q1) { + // the following is indirect but guarantees q1 != q0 && q1 near q0 + q1 = av_clip(q0+1, 1, SCALE_MAX_POS); + q0 = av_clip(q1-1, 0, SCALE_MAX_POS - 1); + } for (i = 0; i < TRELLIS_STATES; i++) { paths[0][i].cost = 0.0f; @@ -327,6 +333,10 @@ static void search_for_quantizers_anmr(AVCodecContext *avctx, AACEncContext *s, maxscale = coef2maxsf(qmax); minscale = av_clip(minscale - q0, 0, TRELLIS_STATES - 1); maxscale = av_clip(maxscale - q0, 0, TRELLIS_STATES); + if (minscale == maxscale) { + maxscale = av_clip(minscale+1, 1, TRELLIS_STATES); + minscale = av_clip(maxscale-1, 0, TRELLIS_STATES - 1); + } maxval = find_max_val(sce->ics.group_len[w], sce->ics.swb_sizes[g], s->scoefs+start); for (q = minscale; q < maxscale; q++) { float dist = 0;