2 * AAC encoder trellis codebook selector
3 * Copyright (C) 2008-2009 Konstantin Shishkov
5 * This file is part of FFmpeg.
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 * AAC encoder trellis codebook selector
25 * @author Konstantin Shishkov
29 * This file contains a template for the codebook_trellis_rate selector function.
30 * It needs to be provided, externally, as an already included declaration,
31 * the following functions from aacenc_quantization/util.h. They're not included
32 * explicitly here to make it possible to provide alternative implementations:
33 * - quantize_band_cost_bits
37 #ifndef AVCODEC_AACCODER_TRELLIS_H
38 #define AVCODEC_AACCODER_TRELLIS_H
41 #include "libavutil/mathematics.h"
47 #include "aacenctab.h"
48 #include "aac_tablegen_decl.h"
52 * structure used in optimal codebook search
54 typedef struct TrellisBandCodingPath {
55 int prev_idx; ///< pointer to the previous path point
56 float cost; ///< path cost
58 } TrellisBandCodingPath;
61 static void codebook_trellis_rate(AACEncContext *s, SingleChannelElement *sce,
62 int win, int group_len, const float lambda)
64 TrellisBandCodingPath path[120][CB_TOT_ALL];
65 int w, swb, cb, start, size;
67 const int max_sfb = sce->ics.max_sfb;
68 const int run_bits = sce->ics.num_windows == 1 ? 5 : 3;
69 const int run_esc = (1 << run_bits) - 1;
71 int stackrun[120], stackcb[120], stack_len;
72 float next_minbits = INFINITY;
75 abs_pow34_v(s->scoefs, sce->coeffs, 1024);
77 for (cb = 0; cb < CB_TOT_ALL; cb++) {
78 path[0][cb].cost = run_bits+4;
79 path[0][cb].prev_idx = -1;
82 for (swb = 0; swb < max_sfb; swb++) {
83 size = sce->ics.swb_sizes[swb];
84 if (sce->zeroes[win*16 + swb]) {
85 float cost_stay_here = path[swb][0].cost;
86 float cost_get_here = next_minbits + run_bits + 4;
87 if ( run_value_bits[sce->ics.num_windows == 8][path[swb][0].run]
88 != run_value_bits[sce->ics.num_windows == 8][path[swb][0].run+1])
89 cost_stay_here += run_bits;
90 if (cost_get_here < cost_stay_here) {
91 path[swb+1][0].prev_idx = next_mincb;
92 path[swb+1][0].cost = cost_get_here;
93 path[swb+1][0].run = 1;
95 path[swb+1][0].prev_idx = 0;
96 path[swb+1][0].cost = cost_stay_here;
97 path[swb+1][0].run = path[swb][0].run + 1;
99 next_minbits = path[swb+1][0].cost;
101 for (cb = 1; cb < CB_TOT_ALL; cb++) {
102 path[swb+1][cb].cost = 61450;
103 path[swb+1][cb].prev_idx = -1;
104 path[swb+1][cb].run = 0;
107 float minbits = next_minbits;
108 int mincb = next_mincb;
109 int startcb = sce->band_type[win*16+swb];
110 startcb = aac_cb_in_map[startcb];
111 next_minbits = INFINITY;
113 for (cb = 0; cb < startcb; cb++) {
114 path[swb+1][cb].cost = 61450;
115 path[swb+1][cb].prev_idx = -1;
116 path[swb+1][cb].run = 0;
118 for (cb = startcb; cb < CB_TOT_ALL; cb++) {
119 float cost_stay_here, cost_get_here;
121 if (cb >= 12 && sce->band_type[win*16+swb] != aac_cb_out_map[cb]) {
122 path[swb+1][cb].cost = 61450;
123 path[swb+1][cb].prev_idx = -1;
124 path[swb+1][cb].run = 0;
127 for (w = 0; w < group_len; w++) {
128 bits += quantize_band_cost_bits(s, &sce->coeffs[start + w*128],
129 &s->scoefs[start + w*128], size,
130 sce->sf_idx[win*16+swb],
132 0, INFINITY, NULL, NULL, 0);
134 cost_stay_here = path[swb][cb].cost + bits;
135 cost_get_here = minbits + bits + run_bits + 4;
136 if ( run_value_bits[sce->ics.num_windows == 8][path[swb][cb].run]
137 != run_value_bits[sce->ics.num_windows == 8][path[swb][cb].run+1])
138 cost_stay_here += run_bits;
139 if (cost_get_here < cost_stay_here) {
140 path[swb+1][cb].prev_idx = mincb;
141 path[swb+1][cb].cost = cost_get_here;
142 path[swb+1][cb].run = 1;
144 path[swb+1][cb].prev_idx = cb;
145 path[swb+1][cb].cost = cost_stay_here;
146 path[swb+1][cb].run = path[swb][cb].run + 1;
148 if (path[swb+1][cb].cost < next_minbits) {
149 next_minbits = path[swb+1][cb].cost;
154 start += sce->ics.swb_sizes[swb];
157 //convert resulting path from backward-linked list
160 for (cb = 1; cb < CB_TOT_ALL; cb++)
161 if (path[max_sfb][cb].cost < path[max_sfb][idx].cost)
165 av_assert1(idx >= 0);
167 stackrun[stack_len] = path[ppos][cb].run;
168 stackcb [stack_len] = cb;
169 idx = path[ppos-path[ppos][cb].run+1][cb].prev_idx;
170 ppos -= path[ppos][cb].run;
173 //perform actual band info encoding
175 for (i = stack_len - 1; i >= 0; i--) {
176 cb = aac_cb_out_map[stackcb[i]];
177 put_bits(&s->pb, 4, cb);
179 memset(sce->zeroes + win*16 + start, !cb, count);
180 //XXX: memset when band_type is also uint8_t
181 for (j = 0; j < count; j++) {
182 sce->band_type[win*16 + start] = cb;
185 while (count >= run_esc) {
186 put_bits(&s->pb, run_bits, run_esc);
189 put_bits(&s->pb, run_bits, count);
194 #endif /* AVCODEC_AACCODER_TRELLIS_H */