History

aacenc: remove ANMR coder

If there's ever a rework of the AAC encoder, it won't start from here. The codec, with all its oddities and tweaks needed to acheive good quality has strayed far from the academic work upon which this coder was based on. Its been 20 years since this paper was released, and no known existing implementations, open-source or proprietary that we know of, are based on it.

Lynne committed 1 year ago

Commit b3d73df1 parent 93835333 files · +0 −295Browse files

Showing the changes to libavcodec/aaccoder.c (1 of 3 files). Show all

Unified · Split

libavcodec/aaccoder.c+0−287
@@ -292,120 +292,6 @@ typedef struct BandCodingPath { · 291 unmodified lines
292 int run;292 int run;
293} BandCodingPath;293} BandCodingPath;
294 294
295/**
296 * Encode band info for single window group bands.
297 */
298static void encode_window_bands_info(AACEncContext *s, SingleChannelElement *sce,
299 int win, int group_len, const float lambda)
300{
301 BandCodingPath path[120][CB_TOT_ALL];
302 int w, swb, cb, start, size;
303 int i, j;
304 const int max_sfb = sce->ics.max_sfb;
305 const int run_bits = sce->ics.num_windows == 1 ? 5 : 3;
306 const int run_esc = (1 << run_bits) - 1;
307 int idx, ppos, count;
308 int stackrun[120], stackcb[120], stack_len;
309 float next_minrd = INFINITY;
310 int next_mincb = 0;
311
312 s->aacdsp.abs_pow34(s->scoefs, sce->coeffs, 1024);
313 start = win*128;
314 for (cb = 0; cb < CB_TOT_ALL; cb++) {
315 path[0][cb].cost = 0.0f;
316 path[0][cb].prev_idx = -1;
317 path[0][cb].run = 0;
318 }
319 for (swb = 0; swb < max_sfb; swb++) {
320 size = sce->ics.swb_sizes[swb];
321 if (sce->zeroes[win*16 + swb]) {
322 for (cb = 0; cb < CB_TOT_ALL; cb++) {
323 path[swb+1][cb].prev_idx = cb;
324 path[swb+1][cb].cost = path[swb][cb].cost;
325 path[swb+1][cb].run = path[swb][cb].run + 1;
326 }
327 } else {
328 float minrd = next_minrd;
329 int mincb = next_mincb;
330 next_minrd = INFINITY;
331 next_mincb = 0;
332 for (cb = 0; cb < CB_TOT_ALL; cb++) {
333 float cost_stay_here, cost_get_here;
334 float rd = 0.0f;
335 if (cb >= 12 && sce->band_type[win*16+swb] < aac_cb_out_map[cb] ||
336 cb < aac_cb_in_map[sce->band_type[win*16+swb]] && sce->band_type[win*16+swb] > aac_cb_out_map[cb]) {
337 path[swb+1][cb].prev_idx = -1;
338 path[swb+1][cb].cost = INFINITY;
339 path[swb+1][cb].run = path[swb][cb].run + 1;
340 continue;
341 }
342 for (w = 0; w < group_len; w++) {
343 FFPsyBand *band = &s->psy.ch[s->cur_channel].psy_bands[(win+w)*16+swb];
344 rd += quantize_band_cost(s, &sce->coeffs[start + w*128],
345 &s->scoefs[start + w*128], size,
346 sce->sf_idx[(win+w)*16+swb], aac_cb_out_map[cb],
347 lambda / band->threshold, INFINITY, NULL, NULL);
348 }
349 cost_stay_here = path[swb][cb].cost + rd;
350 cost_get_here = minrd + rd + run_bits + 4;
351 if ( run_value_bits[sce->ics.num_windows == 8][path[swb][cb].run]
352 != run_value_bits[sce->ics.num_windows == 8][path[swb][cb].run+1])
353 cost_stay_here += run_bits;
354 if (cost_get_here < cost_stay_here) {
355 path[swb+1][cb].prev_idx = mincb;
356 path[swb+1][cb].cost = cost_get_here;
357 path[swb+1][cb].run = 1;
358 } else {
359 path[swb+1][cb].prev_idx = cb;
360 path[swb+1][cb].cost = cost_stay_here;
361 path[swb+1][cb].run = path[swb][cb].run + 1;
362 }
363 if (path[swb+1][cb].cost < next_minrd) {
364 next_minrd = path[swb+1][cb].cost;
365 next_mincb = cb;
366 }
367 }
368 }
369 start += sce->ics.swb_sizes[swb];
370 }
371
372 //convert resulting path from backward-linked list
373 stack_len = 0;
374 idx = 0;
375 for (cb = 1; cb < CB_TOT_ALL; cb++)
376 if (path[max_sfb][cb].cost < path[max_sfb][idx].cost)
377 idx = cb;
378 ppos = max_sfb;
379 while (ppos > 0) {
380 av_assert1(idx >= 0);
381 cb = idx;
382 stackrun[stack_len] = path[ppos][cb].run;
383 stackcb [stack_len] = cb;
384 idx = path[ppos-path[ppos][cb].run+1][cb].prev_idx;
385 ppos -= path[ppos][cb].run;
386 stack_len++;
387 }
388 //perform actual band info encoding
389 start = 0;
390 for (i = stack_len - 1; i >= 0; i--) {
391 cb = aac_cb_out_map[stackcb[i]];
392 put_bits(&s->pb, 4, cb);
393 count = stackrun[i];
394 memset(sce->zeroes + win*16 + start, !cb, count);
395 //XXX: memset when band_type is also uint8_t
396 for (j = 0; j < count; j++) {
397 sce->band_type[win*16 + start] = cb;
398 start++;
399 }
400 while (count >= run_esc) {
401 put_bits(&s->pb, run_bits, run_esc);
402 count -= run_esc;
403 }
404 put_bits(&s->pb, run_bits, count);
405 }
406}
407
408
409typedef struct TrellisPath {295typedef struct TrellisPath {
410 float cost;296 float cost;
411 int prev;297 int prev;
@@ -453,166 +339,6 @@ static void set_special_band_scalefactors(AACEncContext *s, SingleChannelElement · 41 unmodified lines
453 }339 }
454}340}
455 341
456static void search_for_quantizers_anmr(AVCodecContext *avctx, AACEncContext *s,
457 SingleChannelElement *sce,
458 const float lambda)
459{
460 int q, w, w2, g, start = 0;
461 int i, j;
462 int idx;
463 TrellisPath paths[TRELLIS_STAGES][TRELLIS_STATES];
464 int bandaddr[TRELLIS_STAGES];
465 int minq;
466 float mincost;
467 float q0f = FLT_MAX, q1f = 0.0f, qnrgf = 0.0f;
468 int q0, q1, qcnt = 0;
469
470 for (i = 0; i < 1024; i++) {
471 float t = fabsf(sce->coeffs[i]);
472 if (t > 0.0f) {
473 q0f = FFMIN(q0f, t);
474 q1f = FFMAX(q1f, t);
475 qnrgf += t*t;
476 qcnt++;
477 }
478 }
479
480 if (!qcnt) {
481 memset(sce->sf_idx, 0, sizeof(sce->sf_idx));
482 memset(sce->zeroes, 1, sizeof(sce->zeroes));
483 return;
484 }
485
486 //minimum scalefactor index is when minimum nonzero coefficient after quantizing is not clipped
487 q0 = av_clip(coef2minsf(q0f), 0, SCALE_MAX_POS-1);
488 //maximum scalefactor index is when maximum coefficient after quantizing is still not zero
489 q1 = av_clip(coef2maxsf(q1f), 1, SCALE_MAX_POS);
490 if (q1 - q0 > 60) {
491 int q0low = q0;
492 int q1high = q1;
493 //minimum scalefactor index is when maximum nonzero coefficient after quantizing is not clipped
494 int qnrg = av_clip_uint8(log2f(sqrtf(qnrgf/qcnt))*4 - 31 + SCALE_ONE_POS - SCALE_DIV_512);
495 q1 = qnrg + 30;
496 q0 = qnrg - 30;
497 if (q0 < q0low) {
498 q1 += q0low - q0;
499 q0 = q0low;
500 } else if (q1 > q1high) {
501 q0 -= q1 - q1high;
502 q1 = q1high;
503 }
504 }
505 // q0 == q1 isn't really a legal situation
506 if (q0 == q1) {
507 // the following is indirect but guarantees q1 != q0 && q1 near q0
508 q1 = av_clip(q0+1, 1, SCALE_MAX_POS);
509 q0 = av_clip(q1-1, 0, SCALE_MAX_POS - 1);
510 }
511
512 for (i = 0; i < TRELLIS_STATES; i++) {
513 paths[0][i].cost = 0.0f;
514 paths[0][i].prev = -1;
515 }
516 for (j = 1; j < TRELLIS_STAGES; j++) {
517 for (i = 0; i < TRELLIS_STATES; i++) {
518 paths[j][i].cost = INFINITY;
519 paths[j][i].prev = -2;
520 }
521 }
522 idx = 1;
523 s->aacdsp.abs_pow34(s->scoefs, sce->coeffs, 1024);
524 for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
525 start = w*128;
526 for (g = 0; g < sce->ics.num_swb; g++) {
527 const float *coefs = &sce->coeffs[start];
528 float qmin, qmax;
529 int nz = 0;
530
531 bandaddr[idx] = w * 16 + g;
532 qmin = INT_MAX;
533 qmax = 0.0f;
534 for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
535 FFPsyBand *band = &s->psy.ch[s->cur_channel].psy_bands[(w+w2)*16+g];
536 if (band->energy <= band->threshold || band->threshold == 0.0f) {
537 sce->zeroes[(w+w2)*16+g] = 1;
538 continue;
539 }
540 sce->zeroes[(w+w2)*16+g] = 0;
541 nz = 1;
542 for (i = 0; i < sce->ics.swb_sizes[g]; i++) {
543 float t = fabsf(coefs[w2*128+i]);
544 if (t > 0.0f)
545 qmin = FFMIN(qmin, t);
546 qmax = FFMAX(qmax, t);
547 }
548 }
549 if (nz) {
550 int minscale, maxscale;
551 float minrd = INFINITY;
552 float maxval;
553 //minimum scalefactor index is when minimum nonzero coefficient after quantizing is not clipped
554 minscale = coef2minsf(qmin);
555 //maximum scalefactor index is when maximum coefficient after quantizing is still not zero
556 maxscale = coef2maxsf(qmax);
557 minscale = av_clip(minscale - q0, 0, TRELLIS_STATES - 1);
558 maxscale = av_clip(maxscale - q0, 0, TRELLIS_STATES);
559 if (minscale == maxscale) {
560 maxscale = av_clip(minscale+1, 1, TRELLIS_STATES);
561 minscale = av_clip(maxscale-1, 0, TRELLIS_STATES - 1);
562 }
563 maxval = find_max_val(sce->ics.group_len[w], sce->ics.swb_sizes[g], s->scoefs+start);
564 for (q = minscale; q < maxscale; q++) {
565 float dist = 0;
566 int cb = find_min_book(maxval, sce->sf_idx[w*16+g]);
567 for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
568 FFPsyBand *band = &s->psy.ch[s->cur_channel].psy_bands[(w+w2)*16+g];
569 dist += quantize_band_cost(s, coefs + w2*128, s->scoefs + start + w2*128, sce->ics.swb_sizes[g],
570 q + q0, cb, lambda / band->threshold, INFINITY, NULL, NULL);
571 }
572 minrd = FFMIN(minrd, dist);
573
574 for (i = 0; i < q1 - q0; i++) {
575 float cost;
576 cost = paths[idx - 1][i].cost + dist
577 + ff_aac_scalefactor_bits[q - i + SCALE_DIFF_ZERO];
578 if (cost < paths[idx][q].cost) {
579 paths[idx][q].cost = cost;
580 paths[idx][q].prev = i;
581 }
582 }
583 }
584 } else {
585 for (q = 0; q < q1 - q0; q++) {
586 paths[idx][q].cost = paths[idx - 1][q].cost + 1;
587 paths[idx][q].prev = q;
588 }
589 }
590 sce->zeroes[w*16+g] = !nz;
591 start += sce->ics.swb_sizes[g];
592 idx++;
593 }
594 }
595 idx--;
596 mincost = paths[idx][0].cost;
597 minq = 0;
598 for (i = 1; i < TRELLIS_STATES; i++) {
599 if (paths[idx][i].cost < mincost) {
600 mincost = paths[idx][i].cost;
601 minq = i;
602 }
603 }
604 while (idx) {
605 sce->sf_idx[bandaddr[idx]] = minq + q0;
606 minq = FFMAX(paths[idx][minq].prev, 0);
607 idx--;
608 }
609 //set the same quantizers inside window groups
610 for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w])
611 for (g = 0; g < sce->ics.num_swb; g++)
612 for (w2 = 1; w2 < sce->ics.group_len[w]; w2++)
613 sce->sf_idx[(w+w2)*16+g] = sce->sf_idx[w*16+g];
614}
615
616static void search_for_quantizers_fast(AVCodecContext *avctx, AACEncContext *s,342static void search_for_quantizers_fast(AVCodecContext *avctx, AACEncContext *s,
617 SingleChannelElement *sce,343 SingleChannelElement *sce,
618 const float lambda)344 const float lambda)
@@ -1115,19 +841,6 @@ static void search_for_ms(AACEncContext *s, ChannelElement *cpe) · 496 unmodified lines
1115}841}
1116 842
1117const AACCoefficientsEncoder ff_aac_coders[AAC_CODER_NB] = {843const AACCoefficientsEncoder ff_aac_coders[AAC_CODER_NB] = {
1118 [AAC_CODER_ANMR] = {
1119 search_for_quantizers_anmr,
1120 encode_window_bands_info,
1121 quantize_and_encode_band,
1122 ff_aac_encode_tns_info,
1123 ff_aac_apply_tns,
1124 set_special_band_scalefactors,
1125 search_for_pns,
1126 mark_pns,
1127 ff_aac_search_for_tns,
1128 search_for_ms,
1129 ff_aac_search_for_is,
1130 },
1131 [AAC_CODER_TWOLOOP] = {844 [AAC_CODER_TWOLOOP] = {
1132 search_for_quantizers_twoloop,845 search_for_quantizers_twoloop,
1133 codebook_trellis_rate,846 codebook_trellis_rate,

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t