]> git.sesse.net Git - ffmpeg/blob - libavcodec/vorbis.c
mpegaudio: clean up #includes
[ffmpeg] / libavcodec / vorbis.c
1 /**
2  * @file
3  * Common code for Vorbis I encoder and decoder
4  * @author Denes Balatoni  ( dbalatoni programozo hu )
5  *
6  * This file is part of Libav.
7  *
8  * Libav is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * Libav is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with Libav; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22
23 #undef V_DEBUG
24 //#define V_DEBUG
25
26 #define ALT_BITSTREAM_READER_LE
27 #include "avcodec.h"
28 #include "get_bits.h"
29
30 #include "vorbis.h"
31
32
33 /* Helper functions */
34
35 // x^(1/n)
36 unsigned int ff_vorbis_nth_root(unsigned int x, unsigned int n)
37 {
38     unsigned int ret = 0, i, j;
39
40     do {
41         ++ret;
42         for (i = 0, j = ret; i < n - 1; i++)
43             j *= ret;
44     } while (j <= x);
45
46     return ret - 1;
47 }
48
49 // Generate vlc codes from vorbis huffman code lengths
50
51 // the two bits[p] > 32 checks should be redundant, all calling code should
52 // already ensure that, but since it allows overwriting the stack it seems
53 // reasonable to check redundantly.
54 int ff_vorbis_len2vlc(uint8_t *bits, uint32_t *codes, unsigned num)
55 {
56     uint32_t exit_at_level[33] = { 404 };
57
58     unsigned i, j, p, code;
59
60 #ifdef V_DEBUG
61     GetBitContext gb;
62 #endif
63
64     for (p = 0; (bits[p] == 0) && (p < num); ++p)
65         ;
66     if (p == num) {
67 //        av_log(vc->avccontext, AV_LOG_INFO, "An empty codebook. Heh?! \n");
68         return 0;
69     }
70
71     codes[p] = 0;
72     if (bits[p] > 32)
73         return 1;
74     for (i = 0; i < bits[p]; ++i)
75         exit_at_level[i+1] = 1 << i;
76
77 #ifdef V_DEBUG
78     av_log(NULL, AV_LOG_INFO, " %u. of %u code len %d code %d - ", p, num, bits[p], codes[p]);
79     init_get_bits(&gb, (uint8_t *)&codes[p], bits[p]);
80     for (i = 0; i < bits[p]; ++i)
81         av_log(NULL, AV_LOG_INFO, "%s", get_bits1(&gb) ? "1" : "0");
82     av_log(NULL, AV_LOG_INFO, "\n");
83 #endif
84
85     ++p;
86
87     for (; p < num; ++p) {
88         if (bits[p] > 32)
89              return 1;
90         if (bits[p] == 0)
91              continue;
92         // find corresponding exit(node which the tree can grow further from)
93         for (i = bits[p]; i > 0; --i)
94             if (exit_at_level[i])
95                 break;
96         if (!i) // overspecified tree
97              return 1;
98         code = exit_at_level[i];
99         exit_at_level[i] = 0;
100         // construct code (append 0s to end) and introduce new exits
101         for (j = i + 1 ;j <= bits[p]; ++j)
102             exit_at_level[j] = code + (1 << (j - 1));
103         codes[p] = code;
104
105 #ifdef V_DEBUG
106         av_log(NULL, AV_LOG_INFO, " %d. code len %d code %d - ", p, bits[p], codes[p]);
107         init_get_bits(&gb, (uint8_t *)&codes[p], bits[p]);
108         for (i = 0; i < bits[p]; ++i)
109             av_log(NULL, AV_LOG_INFO, "%s", get_bits1(&gb) ? "1" : "0");
110         av_log(NULL, AV_LOG_INFO, "\n");
111 #endif
112
113     }
114
115     //no exits should be left (underspecified tree - ie. unused valid vlcs - not allowed by SPEC)
116     for (p = 1; p < 33; p++)
117         if (exit_at_level[p])
118             return 1;
119
120     return 0;
121 }
122
123 void ff_vorbis_ready_floor1_list(vorbis_floor1_entry * list, int values)
124 {
125     int i;
126     list[0].sort = 0;
127     list[1].sort = 1;
128     for (i = 2; i < values; i++) {
129         int j;
130         list[i].low  = 0;
131         list[i].high = 1;
132         list[i].sort = i;
133         for (j = 2; j < i; j++) {
134             int tmp = list[j].x;
135             if (tmp < list[i].x) {
136                 if (tmp > list[list[i].low].x)
137                     list[i].low  =  j;
138             } else {
139                 if (tmp < list[list[i].high].x)
140                     list[i].high = j;
141             }
142         }
143     }
144     for (i = 0; i < values - 1; i++) {
145         int j;
146         for (j = i + 1; j < values; j++) {
147             if (list[list[i].sort].x > list[list[j].sort].x) {
148                 int tmp = list[i].sort;
149                 list[i].sort = list[j].sort;
150                 list[j].sort = tmp;
151             }
152         }
153     }
154 }
155
156 static inline void render_line_unrolled(intptr_t x, intptr_t y, int x1,
157                                         intptr_t sy, int ady, int adx,
158                                         float *buf)
159 {
160     int err = -adx;
161     x -= x1 - 1;
162     buf += x1 - 1;
163     while (++x < 0) {
164         err += ady;
165         if (err >= 0) {
166             err += ady - adx;
167             y   += sy;
168             buf[x++] = ff_vorbis_floor1_inverse_db_table[y];
169         }
170         buf[x] = ff_vorbis_floor1_inverse_db_table[y];
171     }
172     if (x <= 0) {
173         if (err + ady >= 0)
174             y += sy;
175         buf[x] = ff_vorbis_floor1_inverse_db_table[y];
176     }
177 }
178
179 static void render_line(int x0, int y0, int x1, int y1, float *buf)
180 {
181     int dy  = y1 - y0;
182     int adx = x1 - x0;
183     int ady = FFABS(dy);
184     int sy  = dy < 0 ? -1 : 1;
185     buf[x0] = ff_vorbis_floor1_inverse_db_table[y0];
186     if (ady*2 <= adx) { // optimized common case
187         render_line_unrolled(x0, y0, x1, sy, ady, adx, buf);
188     } else {
189         int base = dy / adx;
190         int x    = x0;
191         int y    = y0;
192         int err  = -adx;
193         ady -= FFABS(base) * adx;
194         while (++x < x1) {
195             y += base;
196             err += ady;
197             if (err >= 0) {
198                 err -= adx;
199                 y   += sy;
200             }
201             buf[x] = ff_vorbis_floor1_inverse_db_table[y];
202         }
203     }
204 }
205
206 void ff_vorbis_floor1_render_list(vorbis_floor1_entry * list, int values,
207                                   uint16_t *y_list, int *flag,
208                                   int multiplier, float *out, int samples)
209 {
210     int lx, ly, i;
211     lx = 0;
212     ly = y_list[0] * multiplier;
213     for (i = 1; i < values; i++) {
214         int pos = list[i].sort;
215         if (flag[pos]) {
216             int x1 = list[pos].x;
217             int y1 = y_list[pos] * multiplier;
218             if (lx < samples)
219                 render_line(lx, ly, FFMIN(x1,samples), y1, out);
220             lx = x1;
221             ly = y1;
222         }
223         if (lx >= samples)
224             break;
225     }
226     if (lx < samples)
227         render_line(lx, ly, samples, ly, out);
228 }