3 * Common code for Vorbis I encoder and decoder
4 * @author Denes Balatoni ( dbalatoni programozo hu )
6 * This file is part of FFmpeg.
8 * FFmpeg 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.
13 * FFmpeg 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.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with FFmpeg; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25 * Common code for Vorbis I encoder and decoder
26 * @author Denes Balatoni ( dbalatoni programozo hu )
29 #include "libavutil/common.h"
35 /* Helper functions */
38 unsigned int ff_vorbis_nth_root(unsigned int x, unsigned int n)
40 unsigned int ret = 0, i, j;
44 for (i = 0, j = ret; i < n - 1; i++)
51 // Generate vlc codes from vorbis huffman code lengths
53 // the two bits[p] > 32 checks should be redundant, all calling code should
54 // already ensure that, but since it allows overwriting the stack it seems
55 // reasonable to check redundantly.
56 int ff_vorbis_len2vlc(uint8_t *bits, uint32_t *codes, unsigned num)
58 uint32_t exit_at_level[33] = { 404 };
59 unsigned i, j, p, code;
61 for (p = 0; (bits[p] == 0) && (p < num); ++p)
68 return AVERROR_INVALIDDATA;
69 for (i = 0; i < bits[p]; ++i)
70 exit_at_level[i+1] = 1 << i;
74 for (i = p; (bits[i] == 0) && (i < num); ++i)
79 for (; p < num; ++p) {
81 return AVERROR_INVALIDDATA;
84 // find corresponding exit(node which the tree can grow further from)
85 for (i = bits[p]; i > 0; --i)
88 if (!i) // overspecified tree
89 return AVERROR_INVALIDDATA;
90 code = exit_at_level[i];
92 // construct code (append 0s to end) and introduce new exits
93 for (j = i + 1 ;j <= bits[p]; ++j)
94 exit_at_level[j] = code + (1 << (j - 1));
98 //no exits should be left (underspecified tree - ie. unused valid vlcs - not allowed by SPEC)
99 for (p = 1; p < 33; p++)
100 if (exit_at_level[p])
101 return AVERROR_INVALIDDATA;
106 int ff_vorbis_ready_floor1_list(AVCodecContext *avctx,
107 vorbis_floor1_entry *list, int values)
112 for (i = 2; i < values; i++) {
117 for (j = 2; j < i; j++) {
119 if (tmp < list[i].x) {
120 if (tmp > list[list[i].low].x)
123 if (tmp < list[list[i].high].x)
128 for (i = 0; i < values - 1; i++) {
130 for (j = i + 1; j < values; j++) {
131 if (list[i].x == list[j].x) {
132 av_log(avctx, AV_LOG_ERROR,
133 "Duplicate value found in floor 1 X coordinates\n");
134 return AVERROR_INVALIDDATA;
136 if (list[list[i].sort].x > list[list[j].sort].x) {
137 int tmp = list[i].sort;
138 list[i].sort = list[j].sort;
146 static inline void render_line_unrolled(intptr_t x, int y, int x1,
147 intptr_t sy, int ady, int adx,
158 buf[x++] = ff_vorbis_floor1_inverse_db_table[av_clip_uint8(y)];
160 buf[x] = ff_vorbis_floor1_inverse_db_table[av_clip_uint8(y)];
165 buf[x] = ff_vorbis_floor1_inverse_db_table[av_clip_uint8(y)];
169 static void render_line(int x0, int y0, int x1, int y1, float *buf)
174 int sy = dy < 0 ? -1 : 1;
175 buf[x0] = ff_vorbis_floor1_inverse_db_table[av_clip_uint8(y0)];
176 if (ady*2 <= adx) { // optimized common case
177 render_line_unrolled(x0, y0, x1, sy, ady, adx, buf);
183 ady -= FFABS(base) * adx;
191 buf[x] = ff_vorbis_floor1_inverse_db_table[av_clip_uint8(y)];
196 void ff_vorbis_floor1_render_list(vorbis_floor1_entry * list, int values,
197 uint16_t *y_list, int *flag,
198 int multiplier, float *out, int samples)
202 ly = y_list[0] * multiplier;
203 for (i = 1; i < values; i++) {
204 int pos = list[i].sort;
206 int x1 = list[pos].x;
207 int y1 = y_list[pos] * multiplier;
209 render_line(lx, ly, FFMIN(x1,samples), y1, out);
217 render_line(lx, ly, samples, ly, out);