]> git.sesse.net Git - ffmpeg/blob - libavcodec/aaccoder_trellis.h
Merge commit 'bfe1cd80ebeab58cbc1c91ac766a96fce8e4ec1e'
[ffmpeg] / libavcodec / aaccoder_trellis.h
1 /*
2  * AAC encoder trellis codebook selector
3  * Copyright (C) 2008-2009 Konstantin Shishkov
4  *
5  * This file is part of FFmpeg.
6  *
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.
11  *
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.
16  *
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
20  */
21
22 /**
23  * @file
24  * AAC encoder trellis codebook selector
25  * @author Konstantin Shishkov
26  */
27
28 /**
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
34  *  - abs_pow34_v
35  */
36
37 #ifndef AVCODEC_AACCODER_TRELLIS_H
38 #define AVCODEC_AACCODER_TRELLIS_H
39
40 #include <float.h>
41 #include "libavutil/mathematics.h"
42 #include "avcodec.h"
43 #include "put_bits.h"
44 #include "aac.h"
45 #include "aacenc.h"
46 #include "aactab.h"
47 #include "aacenctab.h"
48 #include "aac_tablegen_decl.h"
49
50
51 /**
52  * structure used in optimal codebook search
53  */
54 typedef struct TrellisBandCodingPath {
55     int prev_idx; ///< pointer to the previous path point
56     float cost;   ///< path cost
57     int run;
58 } TrellisBandCodingPath;
59
60
61 static void codebook_trellis_rate(AACEncContext *s, SingleChannelElement *sce,
62                                   int win, int group_len, const float lambda)
63 {
64     TrellisBandCodingPath path[120][CB_TOT_ALL];
65     int w, swb, cb, start, size;
66     int i, j;
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;
70     int idx, ppos, count;
71     int stackrun[120], stackcb[120], stack_len;
72     float next_minbits = INFINITY;
73     int next_mincb = 0;
74
75     abs_pow34_v(s->scoefs, sce->coeffs, 1024);
76     start = win*128;
77     for (cb = 0; cb < CB_TOT_ALL; cb++) {
78         path[0][cb].cost     = run_bits+4;
79         path[0][cb].prev_idx = -1;
80         path[0][cb].run      = 0;
81     }
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;
94             } else {
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;
98             }
99             next_minbits = path[swb+1][0].cost;
100             next_mincb = 0;
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;
105             }
106         } else {
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;
112             next_mincb = 0;
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;
117             }
118             for (cb = startcb; cb < CB_TOT_ALL; cb++) {
119                 float cost_stay_here, cost_get_here;
120                 float bits = 0.0f;
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;
125                     continue;
126                 }
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],
131                                                aac_cb_out_map[cb],
132                                                0, INFINITY, NULL, 0);
133                 }
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;
143                 } else {
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;
147                 }
148                 if (path[swb+1][cb].cost < next_minbits) {
149                     next_minbits = path[swb+1][cb].cost;
150                     next_mincb = cb;
151                 }
152             }
153         }
154         start += sce->ics.swb_sizes[swb];
155     }
156
157     //convert resulting path from backward-linked list
158     stack_len = 0;
159     idx       = 0;
160     for (cb = 1; cb < CB_TOT_ALL; cb++)
161         if (path[max_sfb][cb].cost < path[max_sfb][idx].cost)
162             idx = cb;
163     ppos = max_sfb;
164     while (ppos > 0) {
165         av_assert1(idx >= 0);
166         cb = idx;
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;
171         stack_len++;
172     }
173     //perform actual band info encoding
174     start = 0;
175     for (i = stack_len - 1; i >= 0; i--) {
176         cb = aac_cb_out_map[stackcb[i]];
177         put_bits(&s->pb, 4, cb);
178         count = stackrun[i];
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;
183             start++;
184         }
185         while (count >= run_esc) {
186             put_bits(&s->pb, run_bits, run_esc);
187             count -= run_esc;
188         }
189         put_bits(&s->pb, run_bits, count);
190     }
191 }
192
193
194 #endif /* AVCODEC_AACCODER_TRELLIS_H */