2 * copyright (c) 2006 Oded Shimon <ods15@ods15.dyndns.org>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 * Native Vorbis encoder.
22 * @author Oded Shimon <ods15@ods15.dyndns.org>
32 //#define ALT_BITSTREAM_WRITER
33 //#include "bitstream.h"
68 int * partition_to_class;
70 floor_class_t * classes;
106 int blocksize[2]; // in (1<<n) format
108 const float * win[2];
112 float * floor; // also used for tmp values for mdct
113 float * coeffs; // also used for residue after floor
117 codebook_t * codebooks;
123 residue_t * residues;
126 mapping_t * mappings;
129 vorbis_mode_t * modes;
139 #define ilog(i) av_log2(2*(i))
141 static inline void init_put_bits(PutBitContext * pb, uint8_t * buf, int buffer_len) {
142 pb->total = buffer_len * 8;
148 static void put_bits(PutBitContext * pb, int bits, uint64_t val) {
149 if ((pb->total_pos += bits) >= pb->total) return;
152 if (pb->pos > bits) {
153 *pb->buf_ptr |= val << (8 - pb->pos);
157 *pb->buf_ptr++ |= (val << (8 - pb->pos)) & 0xFF;
163 for (; bits >= 8; bits -= 8) {
164 *pb->buf_ptr++ = val & 0xFF;
173 static inline void flush_put_bits(PutBitContext * pb) {
176 static inline int put_bits_count(PutBitContext * pb) {
177 return pb->total_pos;
180 static int cb_lookup_vals(int lookup, int dimentions, int entries) {
183 for (tmp = 0; ; tmp++) {
185 for (i = 0; i < dimentions; i++) n *= tmp;
186 if (n > entries) break;
189 } else if (lookup == 2) return dimentions * entries;
193 static void ready_codebook(codebook_t * cb) {
194 int h[33] = { 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 };
197 for (i = 0; i < cb->nentries; i++) {
198 cb_entry_t * e = &cb->entries[i];
200 if (!e->len) continue;
203 for (j = e->len; j; j--)
209 for (j++; j <= e->len; j++) h[j] = e->codeword | (1 << (j - 1));
211 for (i = 0; i < 33; i++) assert(!h[i]);
213 if (!cb->lookup) cb->dimentions = NULL;
215 int vals = cb_lookup_vals(cb->lookup, cb->ndimentions, cb->nentries);
216 cb->dimentions = av_malloc(sizeof(float) * cb->nentries * cb->ndimentions);
217 for (i = 0; i < cb->nentries; i++) {
221 for (j = 0; j < cb->ndimentions; j++) {
223 if (cb->lookup == 1) off = (i / div) % vals; // lookup type 1
224 else off = i * cb->ndimentions + j; // lookup type 2
226 cb->dimentions[i * cb->ndimentions + j] = last + cb->min + cb->quantlist[off] * cb->delta;
227 if (cb->seq_p) last = cb->dimentions[i * cb->ndimentions + j];
235 static void ready_floor(floor_t * fc) {
237 fc->list[0].sort = 0;
238 fc->list[1].sort = 1;
239 for (i = 2; i < fc->values; i++) {
242 fc->list[i].high = 1;
243 fc->list[i].sort = i;
244 for (j = 2; j < i; j++) {
245 int tmp = fc->list[j].x;
246 if (tmp < fc->list[i].x) {
247 if (tmp > fc->list[fc->list[i].low].x) fc->list[i].low = j;
249 if (tmp < fc->list[fc->list[i].high].x) fc->list[i].high = j;
253 for (i = 0; i < fc->values - 1; i++) {
255 for (j = i + 1; j < fc->values; j++) {
256 if (fc->list[fc->list[i].sort].x > fc->list[fc->list[j].sort].x) {
257 int tmp = fc->list[i].sort;
258 fc->list[i].sort = fc->list[j].sort;
259 fc->list[j].sort = tmp;
265 static void ready_residue(residue_t * rc, venc_context_t * venc) {
267 assert(rc->type == 2);
268 rc->maxes = av_mallocz(sizeof(float[2]) * rc->classifications);
269 for (i = 0; i < rc->classifications; i++) {
272 for (j = 0; j < 8; j++) if (rc->books[i][j] != -1) break;
273 if (j == 8) continue; // zero
274 cb = &venc->codebooks[rc->books[i][j]];
275 assert(cb->ndimentions >= 2);
278 for (j = 0; j < cb->nentries; j++) {
280 if (!cb->entries[j].len) continue;
281 a = fabs(cb->dimentions[j * cb->ndimentions]);
282 if (a > rc->maxes[i][0]) rc->maxes[i][0] = a;
283 a = fabs(cb->dimentions[j * cb->ndimentions + 1]);
284 if (a > rc->maxes[i][1]) rc->maxes[i][1] = a;
288 for (i = 0; i < rc->classifications; i++) {
289 rc->maxes[i][0] += 0.8;
290 rc->maxes[i][1] += 0.8;
294 static void create_vorbis_context(venc_context_t * venc, AVCodecContext * avccontext) {
301 int codebook0[] = { 2, 10, 8, 14, 7, 12, 11, 14, 1, 5, 3, 7, 4, 9, 7, 13, };
302 int codebook1[] = { 1, 4, 2, 6, 3, 7, 5, 7, };
303 int codebook2[] = { 1, 5, 7, 21, 5, 8, 9, 21, 10, 9, 12, 20, 20, 16, 20, 20, 4, 8, 9, 20, 6, 8, 9, 20, 11, 11, 13, 20, 20, 15, 17, 20, 9, 11, 14, 20, 8, 10, 15, 20, 11, 13, 15, 20, 20, 20, 20, 20, 20, 20, 20, 20, 13, 20, 20, 20, 18, 18, 20, 20, 20, 20, 20, 20, 3, 6, 8, 20, 6, 7, 9, 20, 10, 9, 12, 20, 20, 20, 20, 20, 5, 7, 9, 20, 6, 6, 9, 20, 10, 9, 12, 20, 20, 20, 20, 20, 8, 10, 13, 20, 8, 9, 12, 20, 11, 10, 12, 20, 20, 20, 20, 20, 18, 20, 20, 20, 15, 17, 18, 20, 18, 17, 18, 20, 20, 20, 20, 20, 7, 10, 12, 20, 8, 9, 11, 20, 14, 13, 14, 20, 20, 20, 20, 20, 6, 9, 12, 20, 7, 8, 11, 20, 12, 11, 13, 20, 20, 20, 20, 20, 9, 11, 15, 20, 8, 10, 14, 20, 12, 11, 14, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 11, 16, 18, 20, 15, 15, 17, 20, 20, 17, 20, 20, 20, 20, 20, 20, 9, 14, 16, 20, 12, 12, 15, 20, 17, 15, 18, 20, 20, 20, 20, 20, 16, 19, 18, 20, 15, 16, 20, 20, 17, 17, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, };
304 int codebook3[] = { 2, 3, 7, 13, 4, 4, 7, 15, 8, 6, 9, 17, 21, 16, 15, 21, 2, 5, 7, 11, 5, 5, 7, 14, 9, 7, 10, 16, 17, 15, 16, 21, 4, 7, 10, 17, 7, 7, 9, 15, 11, 9, 11, 16, 21, 18, 15, 21, 18, 21, 21, 21, 15, 17, 17, 19, 21, 19, 18, 20, 21, 21, 21, 20, };
305 int codebook4[] = { 5, 5, 5, 5, 6, 5, 6, 5, 6, 5, 6, 5, 6, 5, 6, 5, 6, 5, 6, 5, 6, 5, 6, 5, 7, 5, 7, 5, 7, 5, 7, 5, 8, 6, 8, 6, 8, 6, 9, 6, 9, 6, 10, 6, 10, 6, 11, 6, 11, 7, 11, 7, 12, 7, 12, 7, 12, 7, 12, 7, 12, 7, 12, 7, 12, 7, 12, 8, 13, 8, 12, 8, 12, 8, 13, 8, 13, 9, 13, 9, 13, 9, 13, 9, 12, 10, 12, 10, 13, 10, 14, 11, 14, 12, 14, 13, 14, 13, 14, 14, 15, 16, 15, 15, 15, 14, 15, 17, 21, 22, 22, 21, 22, 22, 22, 22, 22, 22, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, };
306 int codebook5[] = { 2, 5, 5, 4, 5, 4, 5, 4, 5, 4, 6, 5, 6, 5, 6, 5, 6, 5, 7, 5, 7, 6, 8, 6, 8, 6, 8, 6, 9, 6, 9, 6, };
307 int codebook6[] = { 8, 5, 8, 4, 9, 4, 9, 4, 9, 4, 9, 4, 9, 4, 9, 4, 9, 4, 9, 4, 9, 4, 8, 4, 8, 4, 9, 5, 9, 5, 9, 5, 9, 5, 9, 6, 10, 6, 10, 7, 10, 8, 11, 9, 11, 11, 12, 13, 12, 14, 13, 15, 13, 15, 14, 16, 14, 17, 15, 17, 15, 15, 16, 16, 15, 16, 16, 16, 15, 18, 16, 15, 17, 17, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, };
308 int codebook7[] = { 1, 5, 5, 5, 5, 5, 5, 5, 6, 5, 6, 5, 6, 5, 6, 5, 6, 6, 7, 7, 7, 7, 8, 7, 8, 8, 9, 8, 10, 9, 10, 9, };
309 int codebook8[] = { 4, 3, 4, 3, 4, 4, 5, 4, 5, 4, 5, 5, 6, 5, 6, 5, 7, 5, 7, 6, 7, 6, 8, 7, 8, 7, 8, 7, 9, 8, 9, 9, 9, 9, 10, 10, 10, 11, 9, 12, 9, 12, 9, 15, 10, 14, 9, 13, 10, 13, 10, 12, 10, 12, 10, 13, 10, 12, 11, 13, 11, 14, 12, 13, 13, 14, 14, 13, 14, 15, 14, 16, 13, 13, 14, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 15, 15, };
310 int codebook9[] = { 4, 5, 4, 5, 3, 5, 3, 5, 3, 5, 4, 4, 4, 4, 5, 5, 5, };
311 int codebook10[] = { 3, 3, 4, 3, 4, 4, 4, 4, 5, 5, 5, 5, 5, 6, 5, 7, 5, 8, 6, 8, 6, 9, 7, 10, 7, 10, 8, 10, 8, 11, 9, 11, };
312 int codebook11[] = { 3, 7, 3, 8, 3, 10, 3, 8, 3, 9, 3, 8, 4, 9, 4, 9, 5, 9, 6, 10, 6, 9, 7, 11, 7, 12, 9, 13, 10, 13, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, };
313 int codebook12[] = { 4, 5, 4, 5, 4, 5, 4, 5, 3, 5, 3, 5, 3, 5, 4, 5, 4, };
314 int codebook13[] = { 4, 2, 4, 2, 5, 3, 5, 4, 6, 6, 6, 7, 7, 8, 7, 8, 7, 8, 7, 9, 8, 9, 8, 9, 8, 10, 8, 11, 9, 12, 9, 12, };
315 int codebook14[] = { 2, 5, 2, 6, 3, 6, 4, 7, 4, 7, 5, 9, 5, 11, 6, 11, 6, 11, 7, 11, 6, 11, 6, 11, 9, 11, 8, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 10, 10, 10, 10, 10, 10, };
316 int codebook15[] = { 5, 6, 11, 11, 11, 11, 10, 10, 12, 11, 5, 2, 11, 5, 6, 6, 7, 9, 11, 13, 13, 10, 7, 11, 6, 7, 8, 9, 10, 12, 11, 5, 11, 6, 8, 7, 9, 11, 14, 15, 11, 6, 6, 8, 4, 5, 7, 8, 10, 13, 10, 5, 7, 7, 5, 5, 6, 8, 10, 11, 10, 7, 7, 8, 6, 5, 5, 7, 9, 9, 11, 8, 8, 11, 8, 7, 6, 6, 7, 9, 12, 11, 10, 13, 9, 9, 7, 7, 7, 9, 11, 13, 12, 15, 12, 11, 9, 8, 8, 8, };
317 int codebook16[] = { 2, 4, 4, 0, 0, 0, 0, 0, 0, 5, 6, 6, 0, 0, 0, 0, 0, 0, 5, 6, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, 7, 7, 0, 0, 0, 0, 0, 0, 7, 8, 8, 0, 0, 0, 0, 0, 0, 6, 7, 8, 0, 0, 0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 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 6, 6, 6, 9, 9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 6, 6, 7, 9, 9, };
320 int codebook19[] = { 2, 3, 3, 6, 6, 0, 0, 0, 0, 0, 4, 4, 6, 6, 0, 0, 0, 0, 0, 4, 4, 6, 6, 0, 0, 0, 0, 0, 5, 5, 6, 6, 0, 0, 0, 0, 0, 0, 0, 6, 6, 0, 0, 0, 0, 0, 0, 0, 7, 8, 0, 0, 0, 0, 0, 0, 0, 7, 7, 0, 0, 0, 0, 0, 0, 0, 9, 9, };
321 int codebook20[] = { 1, 3, 4, 6, 6, 7, 7, 9, 9, 0, 5, 5, 7, 7, 7, 8, 9, 9, 0, 5, 5, 7, 7, 8, 8, 9, 9, 0, 7, 7, 8, 8, 8, 8, 10, 10, 0, 0, 0, 8, 8, 8, 8, 10, 10, 0, 0, 0, 9, 9, 9, 9, 10, 10, 0, 0, 0, 9, 9, 9, 9, 10, 10, 0, 0, 0, 10, 10, 10, 10, 11, 11, 0, 0, 0, 0, 0, 10, 10, 11, 11, };
322 int codebook21[] = { 2, 3, 3, 6, 6, 7, 7, 8, 8, 8, 8, 9, 9, 10, 10, 11, 10, 0, 5, 5, 7, 7, 8, 8, 9, 9, 9, 9, 10, 10, 10, 10, 11, 11, 0, 5, 5, 7, 7, 8, 8, 9, 9, 9, 9, 10, 10, 10, 10, 11, 11, 0, 6, 6, 7, 7, 8, 8, 9, 9, 9, 9, 10, 10, 11, 11, 11, 11, 0, 0, 0, 7, 7, 8, 8, 9, 9, 9, 9, 10, 10, 11, 11, 11, 12, 0, 0, 0, 8, 8, 8, 8, 9, 9, 9, 9, 10, 10, 11, 11, 12, 12, 0, 0, 0, 8, 8, 8, 8, 9, 9, 9, 9, 10, 10, 11, 11, 12, 12, 0, 0, 0, 9, 9, 9, 9, 10, 10, 10, 10, 11, 10, 11, 11, 12, 12, 0, 0, 0, 0, 0, 9, 9, 10, 10, 10, 10, 11, 11, 11, 11, 12, 12, 0, 0, 0, 0, 0, 9, 8, 9, 9, 10, 10, 11, 11, 12, 12, 12, 12, 0, 0, 0, 0, 0, 8, 8, 9, 9, 10, 10, 11, 11, 12, 11, 12, 12, 0, 0, 0, 0, 0, 9, 10, 10, 10, 11, 11, 11, 11, 12, 12, 13, 13, 0, 0, 0, 0, 0, 0, 0, 10, 10, 10, 10, 11, 11, 12, 12, 13, 13, 0, 0, 0, 0, 0, 0, 0, 11, 11, 11, 11, 12, 12, 12, 12, 13, 13, 0, 0, 0, 0, 0, 0, 0, 11, 11, 11, 11, 12, 12, 12, 12, 13, 13, 0, 0, 0, 0, 0, 0, 0, 11, 11, 12, 12, 12, 12, 13, 13, 13, 13, 0, 0, 0, 0, 0, 0, 0, 0, 0, 12, 12, 12, 12, 13, 13, 13, 13, };
323 int codebook22[] = { 1, 4, 4, 7, 6, 6, 7, 6, 6, 4, 7, 7, 10, 9, 9, 11, 9, 9, 4, 7, 7, 10, 9, 9, 11, 9, 9, 7, 10, 10, 11, 11, 10, 12, 11, 11, 6, 9, 9, 11, 10, 10, 11, 10, 10, 6, 9, 9, 11, 10, 10, 11, 10, 10, 7, 11, 11, 11, 11, 11, 12, 11, 11, 6, 9, 9, 11, 10, 10, 11, 10, 10, 6, 9, 9, 11, 10, 10, 11, 10, 10, };
324 int codebook23[] = { 2, 4, 4, 6, 6, 7, 7, 7, 7, 8, 8, 10, 5, 5, 6, 6, 7, 7, 8, 8, 8, 8, 10, 5, 5, 6, 6, 7, 7, 8, 8, 8, 8, 10, 6, 6, 7, 7, 8, 8, 8, 8, 8, 8, 10, 10, 10, 7, 7, 8, 7, 8, 8, 8, 8, 10, 10, 10, 8, 8, 8, 8, 8, 8, 8, 8, 10, 10, 10, 7, 8, 8, 8, 8, 8, 8, 8, 10, 10, 10, 8, 8, 8, 8, 8, 8, 8, 8, 10, 10, 10, 10, 10, 8, 8, 8, 8, 8, 8, 10, 10, 10, 10, 10, 9, 9, 8, 8, 9, 8, 10, 10, 10, 10, 10, 8, 8, 8, 8, 8, 8, };
325 int codebook24[] = { 1, 4, 4, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10, 6, 5, 5, 7, 7, 8, 8, 8, 8, 9, 9, 10, 10, 7, 5, 5, 7, 7, 8, 8, 8, 8, 9, 9, 11, 10, 0, 8, 8, 8, 8, 9, 9, 9, 9, 10, 10, 11, 11, 0, 8, 8, 8, 8, 9, 9, 9, 9, 10, 10, 11, 11, 0, 12, 12, 9, 9, 10, 10, 10, 10, 11, 11, 11, 12, 0, 13, 13, 9, 9, 10, 10, 10, 10, 11, 11, 12, 12, 0, 0, 0, 10, 10, 10, 10, 11, 11, 12, 12, 12, 12, 0, 0, 0, 10, 10, 10, 10, 11, 11, 12, 12, 12, 12, 0, 0, 0, 14, 14, 11, 11, 11, 11, 12, 12, 13, 13, 0, 0, 0, 14, 14, 11, 11, 11, 11, 12, 12, 13, 13, 0, 0, 0, 0, 0, 12, 12, 12, 12, 13, 13, 14, 13, 0, 0, 0, 0, 0, 13, 13, 12, 12, 13, 12, 14, 13, };
326 int codebook25[] = { 2, 4, 4, 5, 5, 6, 5, 5, 5, 5, 6, 4, 5, 5, 5, 6, 5, 5, 5, 5, 6, 6, 6, 5, 5, };
327 int codebook26[] = { 1, 4, 4, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 4, 9, 8, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 2, 9, 7, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 11, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, };
328 int codebook27[] = { 1, 4, 4, 6, 6, 7, 7, 8, 7, 9, 9, 10, 10, 10, 10, 6, 5, 5, 7, 7, 8, 8, 10, 8, 11, 10, 12, 12, 13, 13, 6, 5, 5, 7, 7, 8, 8, 10, 9, 11, 11, 12, 12, 13, 12, 18, 8, 8, 8, 8, 9, 9, 10, 9, 11, 10, 12, 12, 13, 13, 18, 8, 8, 8, 8, 9, 9, 10, 10, 11, 11, 13, 12, 14, 13, 18, 11, 11, 9, 9, 10, 10, 11, 11, 11, 12, 13, 12, 13, 14, 18, 11, 11, 9, 8, 11, 10, 11, 11, 11, 11, 12, 12, 14, 13, 18, 18, 18, 10, 11, 10, 11, 12, 12, 12, 12, 13, 12, 14, 13, 18, 18, 18, 10, 11, 11, 9, 12, 11, 12, 12, 12, 13, 13, 13, 18, 18, 17, 14, 14, 11, 11, 12, 12, 13, 12, 14, 12, 14, 13, 18, 18, 18, 14, 14, 11, 10, 12, 9, 12, 13, 13, 13, 13, 13, 18, 18, 17, 16, 18, 13, 13, 12, 12, 13, 11, 14, 12, 14, 14, 17, 18, 18, 17, 18, 13, 12, 13, 10, 12, 11, 14, 14, 14, 14, 17, 18, 18, 18, 18, 15, 16, 12, 12, 13, 10, 14, 12, 14, 15, 18, 18, 18, 16, 17, 16, 14, 12, 11, 13, 10, 13, 13, 14, 15, };
329 int codebook28[] = { 2, 5, 5, 6, 6, 7, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8, 8, 10, 6, 6, 7, 7, 8, 7, 8, 8, 8, 8, 8, 9, 9, 9, 9, 9, 10, 6, 6, 7, 7, 7, 7, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 10, 7, 7, 7, 7, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 9, 9, 10, 10, 10, 7, 7, 8, 8, 8, 9, 9, 9, 9, 9, 9, 9, 9, 9, 11, 11, 11, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 10, 10, 10, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 10, 10, 10, 8, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 10, 9, 10, 10, 10, 11, 11, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 11, 10, 11, 11, 11, 9, 9, 9, 9, 9, 9, 10, 10, 9, 9, 10, 9, 11, 10, 11, 11, 11, 9, 9, 9, 9, 9, 9, 9, 9, 10, 10, 10, 9, 11, 11, 11, 11, 11, 9, 9, 9, 9, 10, 10, 9, 9, 9, 9, 10, 9, 11, 11, 11, 11, 11, 11, 11, 9, 9, 9, 9, 9, 9, 10, 10, 10, 10, 11, 11, 11, 11, 11, 11, 11, 10, 9, 10, 10, 9, 10, 9, 9, 10, 9, 11, 10, 10, 11, 11, 11, 11, 9, 10, 9, 9, 9, 9, 10, 10, 10, 10, 11, 11, 11, 11, 11, 11, 10, 10, 10, 9, 9, 10, 9, 10, 9, 10, 10, 10, 10, 11, 11, 11, 11, 11, 11, 11, 9, 9, 9, 9, 9, 10, 10, 10, };
331 int codebook_sizes[] = { 16, 8, 256, 64, 128, 32, 96, 32, 96, 17, 32, 78, 17, 32, 78, 100, 1641, 443, 105, 68, 81, 289, 81, 121, 169, 25, 169, 225, 289, };
332 int * codebook_lens[] = { codebook0, codebook1, codebook2, codebook3, codebook4, codebook5, codebook6, codebook7,
333 codebook8, codebook9, codebook10, codebook11, codebook12, codebook13, codebook14, codebook15,
334 codebook16, codebook17, codebook18, codebook19, codebook20, codebook21, codebook22, codebook23,
335 codebook24, codebook25, codebook26, codebook27, codebook28, };
345 { 1, 8, -1.0, 1.0, 6561,(int[]){ 1, 0, 2, } },
346 { 1, 4, -2.0, 1.0, 625, (int[]){ 2, 1, 3, 0, 4, } },
347 { 1, 4, -2.0, 1.0, 625, (int[]){ 2, 1, 3, 0, 4, } },
348 { 1, 2, -4.0, 1.0, 81, (int[]){ 4, 3, 5, 2, 6, 1, 7, 0, 8, } },
349 { 1, 2, -4.0, 1.0, 81, (int[]){ 4, 3, 5, 2, 6, 1, 7, 0, 8, } },
350 { 1, 2, -8.0, 1.0, 289, (int[]){ 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15, 0, 16, } },
351 { 1, 4, -11.0, 11.0, 81, (int[]){ 1, 0, 2, } },
352 { 1, 2, -5.0, 1.0, 121, (int[]){ 5, 4, 6, 3, 7, 2, 8, 1, 9, 0, 10, } },
353 { 1, 2, -30.0, 5.0, 169, (int[]){ 6, 5, 7, 4, 8, 3, 9, 2, 10, 1, 11, 0, 12, } },
354 { 1, 2, -2.0, 1.0, 25, (int[]){ 2, 1, 3, 0, 4, } },
355 { 1, 2, -1530.0, 255.0, 169, (int[]){ 6, 5, 7, 4, 8, 3, 9, 2, 10, 1, 11, 0, 12, } },
356 { 1, 2, -119.0, 17.0, 225, (int[]){ 7, 6, 8, 5, 9, 4, 10, 3, 11, 2, 12, 1, 13, 0, 14, } },
357 { 1, 2, -8.0, 1.0, 289, (int[]){ 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15, 0, 16, } },
360 venc->channels = avccontext->channels;
361 venc->sample_rate = avccontext->sample_rate;
362 venc->blocksize[0] = venc->blocksize[1] = 11;
364 venc->ncodebooks = 29;
365 venc->codebooks = av_malloc(sizeof(codebook_t) * venc->ncodebooks);
367 // codebook 0..14 - floor1 book, values 0..255
368 // codebook 15 residue masterbook
369 // codebook 16..29 residue
370 for (book = 0; book < venc->ncodebooks; book++) {
371 cb = &venc->codebooks[book];
372 cb->nentries = codebook_sizes[book];
379 cb->quantlist = NULL;
383 cb->nentries = cvectors[book - 16].real_len;
384 cb->ndimentions = cvectors[book - 16].dim;
385 cb->min = cvectors[book - 16].min;
386 cb->delta = cvectors[book - 16].delta;
387 cb->lookup = cvectors[book - 16].lookup;
388 vals = cb_lookup_vals(cb->lookup, cb->ndimentions, cb->nentries);
389 cb->quantlist = av_malloc(sizeof(int) * vals);
390 for (i = 0; i < vals; i++) cb->quantlist[i] = cvectors[book - 16].quant[i];
392 cb->entries = av_malloc(sizeof(cb_entry_t) * cb->nentries);
393 for (i = 0; i < cb->nentries; i++) {
394 if (i < codebook_sizes[book]) cb->entries[i].len = codebook_lens[book][i];
395 else cb->entries[i].len = 0;
401 venc->floors = av_malloc(sizeof(floor_t) * venc->nfloors);
404 fc = &venc->floors[0];
406 fc->partition_to_class = av_malloc(sizeof(int) * fc->partitions);
408 for (i = 0; i < fc->partitions; i++) {
409 int a[] = {0,1,2,2,3,3,4,4};
410 fc->partition_to_class[i] = a[i];
411 fc->nclasses = FFMAX(fc->nclasses, fc->partition_to_class[i]);
414 fc->classes = av_malloc(sizeof(floor_class_t) * fc->nclasses);
415 for (i = 0; i < fc->nclasses; i++) {
416 floor_class_t * c = &fc->classes[i];
418 int dim[] = {3,4,3,4,3};
419 int subclass[] = {0,1,1,2,2};
420 int masterbook[] = {0/*none*/,0,1,2,3};
425 (int[]){ -1, 9, 10, 11 },
426 (int[]){ -1, 12, 13, 14 },
429 c->subclass = subclass[i];
430 c->masterbook = masterbook[i];
431 books = (1 << c->subclass);
432 c->books = av_malloc(sizeof(int) * books);
433 for (j = 0; j < books; j++) c->books[j] = nbooks[i][j];
436 fc->rangebits = venc->blocksize[0] - 1;
439 for (i = 0; i < fc->partitions; i++)
440 fc->values += fc->classes[fc->partition_to_class[i]].dim;
442 fc->list = av_malloc(sizeof(floor_entry_t) * fc->values);
444 fc->list[1].x = 1 << fc->rangebits;
445 for (i = 2; i < fc->values; i++) {
448 assert(g <= fc->rangebits);
450 g = 1 << (fc->rangebits - g);
451 fc->list[i].x = g + a*2*g;*/
452 //int a[] = {14, 4, 58, 2, 8, 28, 90};
453 int a[] = {93,23,372,6,46,186,750,14,33,65,130,260,556,3,10,18,28,39,55,79,111,158,220,312,464,650,850};
454 fc->list[i].x = a[i - 2];
459 venc->residues = av_malloc(sizeof(residue_t) * venc->nresidues);
462 rc = &venc->residues[0];
466 rc->partition_size = 32;
467 rc->classifications = 10;
469 rc->books = av_malloc(sizeof(int[8]) * rc->classifications);
470 for (i = 0; i < rc->classifications; i++) {
472 { -1, -1, -1, -1, -1, -1, -1, -1, },
473 { -1, -1, 16, -1, -1, -1, -1, -1, },
474 { -1, -1, 17, -1, -1, -1, -1, -1, },
475 { -1, -1, 18, -1, -1, -1, -1, -1, },
476 { -1, -1, 19, -1, -1, -1, -1, -1, },
477 { -1, -1, 20, -1, -1, -1, -1, -1, },
478 { -1, -1, 21, -1, -1, -1, -1, -1, },
479 { 22, 23, -1, -1, -1, -1, -1, -1, },
480 { 24, 25, -1, -1, -1, -1, -1, -1, },
481 { 26, 27, 28, -1, -1, -1, -1, -1, },
484 for (j = 0; j < 8; j++) rc->books[i][j] = a[i][j];
486 ready_residue(rc, venc);
489 venc->mappings = av_malloc(sizeof(mapping_t) * venc->nmappings);
492 mc = &venc->mappings[0];
494 mc->mux = av_malloc(sizeof(int) * venc->channels);
495 for (i = 0; i < venc->channels; i++) mc->mux[i] = 0;
496 mc->floor = av_malloc(sizeof(int) * mc->submaps);
497 mc->residue = av_malloc(sizeof(int) * mc->submaps);
498 for (i = 0; i < mc->submaps; i++) {
502 mc->coupling_steps = venc->channels == 2 ? 1 : 0;
503 mc->magnitude = av_malloc(sizeof(int) * mc->coupling_steps);
504 mc->angle = av_malloc(sizeof(int) * mc->coupling_steps);
505 if (mc->coupling_steps) {
506 mc->magnitude[0] = 0;
511 venc->modes = av_malloc(sizeof(vorbis_mode_t) * venc->nmodes);
514 venc->modes[0].blockflag = 0;
515 venc->modes[0].mapping = 0;
517 venc->have_saved = 0;
518 venc->saved = av_malloc(sizeof(float) * venc->channels * (1 << venc->blocksize[1]) / 2);
519 venc->samples = av_malloc(sizeof(float) * venc->channels * (1 << venc->blocksize[1]));
520 venc->floor = av_malloc(sizeof(float) * venc->channels * (1 << venc->blocksize[1]) / 2);
521 venc->coeffs = av_malloc(sizeof(float) * venc->channels * (1 << venc->blocksize[1]) / 2);
523 venc->win[0] = ff_vorbis_vwin[venc->blocksize[0] - 6];
524 venc->win[1] = ff_vorbis_vwin[venc->blocksize[1] - 6];
526 ff_mdct_init(&venc->mdct[0], venc->blocksize[0], 0);
527 ff_mdct_init(&venc->mdct[1], venc->blocksize[1], 0);
530 static void put_float(PutBitContext * pb, float f) {
533 mant = (int)ldexp(frexp(f, &exp), 20);
535 if (mant < 0) { res |= (1 << 31); mant = -mant; }
536 res |= mant | (exp << 21);
537 put_bits(pb, 32, res);
540 static void put_codebook_header(PutBitContext * pb, codebook_t * cb) {
544 put_bits(pb, 24, 0x564342); //magic
545 put_bits(pb, 16, cb->ndimentions);
546 put_bits(pb, 24, cb->nentries);
548 for (i = 1; i < cb->nentries; i++) if (cb->entries[i].len < cb->entries[i-1].len) break;
549 if (i == cb->nentries) ordered = 1;
551 put_bits(pb, 1, ordered);
553 int len = cb->entries[0].len;
554 put_bits(pb, 5, len - 1);
556 while (i < cb->nentries) {
558 for (j = 0; j+i < cb->nentries; j++) if (cb->entries[j+i].len != len) break;
559 put_bits(pb, ilog(cb->nentries - i), j);
565 for (i = 0; i < cb->nentries; i++) if (!cb->entries[i].len) break;
566 if (i != cb->nentries) sparse = 1;
567 put_bits(pb, 1, sparse);
569 for (i = 0; i < cb->nentries; i++) {
570 if (sparse) put_bits(pb, 1, !!cb->entries[i].len);
571 if (cb->entries[i].len) put_bits(pb, 5, cb->entries[i].len - 1);
575 put_bits(pb, 4, cb->lookup);
577 int tmp = cb_lookup_vals(cb->lookup, cb->ndimentions, cb->nentries);
578 int bits = ilog(cb->quantlist[0]);
580 for (i = 1; i < tmp; i++) bits = FFMAX(bits, ilog(cb->quantlist[i]));
582 put_float(pb, cb->min);
583 put_float(pb, cb->delta);
585 put_bits(pb, 4, bits - 1);
586 put_bits(pb, 1, cb->seq_p);
588 for (i = 0; i < tmp; i++) put_bits(pb, bits, cb->quantlist[i]);
592 static void put_floor_header(PutBitContext * pb, floor_t * fc) {
595 put_bits(pb, 16, 1); // type, only floor1 is supported
597 put_bits(pb, 5, fc->partitions);
599 for (i = 0; i < fc->partitions; i++) put_bits(pb, 4, fc->partition_to_class[i]);
601 for (i = 0; i < fc->nclasses; i++) {
604 put_bits(pb, 3, fc->classes[i].dim - 1);
605 put_bits(pb, 2, fc->classes[i].subclass);
607 if (fc->classes[i].subclass) put_bits(pb, 8, fc->classes[i].masterbook);
609 books = (1 << fc->classes[i].subclass);
611 for (j = 0; j < books; j++) put_bits(pb, 8, fc->classes[i].books[j] + 1);
614 put_bits(pb, 2, fc->multiplier - 1);
615 put_bits(pb, 4, fc->rangebits);
617 for (i = 2; i < fc->values; i++) put_bits(pb, fc->rangebits, fc->list[i].x);
620 static void put_residue_header(PutBitContext * pb, residue_t * rc) {
623 put_bits(pb, 16, rc->type);
625 put_bits(pb, 24, rc->begin);
626 put_bits(pb, 24, rc->end);
627 put_bits(pb, 24, rc->partition_size - 1);
628 put_bits(pb, 6, rc->classifications - 1);
629 put_bits(pb, 8, rc->classbook);
631 for (i = 0; i < rc->classifications; i++) {
633 for (j = 0; j < 8; j++) tmp |= (rc->books[i][j] != -1) << j;
635 put_bits(pb, 3, tmp & 7);
636 put_bits(pb, 1, tmp > 7);
638 if (tmp > 7) put_bits(pb, 5, tmp >> 3);
641 for (i = 0; i < rc->classifications; i++) {
643 for (j = 0; j < 8; j++)
644 if (rc->books[i][j] != -1)
645 put_bits(pb, 8, rc->books[i][j]);
649 static int put_main_header(venc_context_t * venc, uint8_t ** out) {
652 uint8_t buffer[50000] = {0}, * p = buffer;
653 int buffer_len = sizeof buffer;
656 // identification header
657 init_put_bits(&pb, p, buffer_len);
658 put_bits(&pb, 8, 1); //magic
659 for (i = 0; "vorbis"[i]; i++) put_bits(&pb, 8, "vorbis"[i]);
660 put_bits(&pb, 32, 0); // version
661 put_bits(&pb, 8, venc->channels);
662 put_bits(&pb, 32, venc->sample_rate);
663 put_bits(&pb, 32, 0); // bitrate
664 put_bits(&pb, 32, 0); // bitrate
665 put_bits(&pb, 32, 0); // bitrate
666 put_bits(&pb, 4, venc->blocksize[0]);
667 put_bits(&pb, 4, venc->blocksize[1]);
668 put_bits(&pb, 1, 1); // framing
671 hlens[0] = (put_bits_count(&pb) + 7) / 8;
672 buffer_len -= hlens[0];
676 init_put_bits(&pb, p, buffer_len);
677 put_bits(&pb, 8, 3); //magic
678 for (i = 0; "vorbis"[i]; i++) put_bits(&pb, 8, "vorbis"[i]);
679 put_bits(&pb, 32, 0); // vendor length TODO
680 put_bits(&pb, 32, 0); // amount of comments
681 put_bits(&pb, 1, 1); // framing
684 hlens[1] = (put_bits_count(&pb) + 7) / 8;
685 buffer_len -= hlens[1];
689 init_put_bits(&pb, p, buffer_len);
690 put_bits(&pb, 8, 5); //magic
691 for (i = 0; "vorbis"[i]; i++) put_bits(&pb, 8, "vorbis"[i]);
694 put_bits(&pb, 8, venc->ncodebooks - 1);
695 for (i = 0; i < venc->ncodebooks; i++) put_codebook_header(&pb, &venc->codebooks[i]);
697 // time domain, reserved, zero
699 put_bits(&pb, 16, 0);
702 put_bits(&pb, 6, venc->nfloors - 1);
703 for (i = 0; i < venc->nfloors; i++) put_floor_header(&pb, &venc->floors[i]);
706 put_bits(&pb, 6, venc->nresidues - 1);
707 for (i = 0; i < venc->nresidues; i++) put_residue_header(&pb, &venc->residues[i]);
710 put_bits(&pb, 6, venc->nmappings - 1);
711 for (i = 0; i < venc->nmappings; i++) {
712 mapping_t * mc = &venc->mappings[i];
714 put_bits(&pb, 16, 0); // mapping type
716 put_bits(&pb, 1, mc->submaps > 1);
717 if (mc->submaps > 1) put_bits(&pb, 4, mc->submaps - 1);
719 put_bits(&pb, 1, !!mc->coupling_steps);
720 if (mc->coupling_steps) {
721 put_bits(&pb, 8, mc->coupling_steps - 1);
722 for (j = 0; j < mc->coupling_steps; j++) {
723 put_bits(&pb, ilog(venc->channels - 1), mc->magnitude[j]);
724 put_bits(&pb, ilog(venc->channels - 1), mc->angle[j]);
728 put_bits(&pb, 2, 0); // reserved
730 if (mc->submaps > 1) for (j = 0; j < venc->channels; j++) put_bits(&pb, 4, mc->mux[j]);
732 for (j = 0; j < mc->submaps; j++) {
733 put_bits(&pb, 8, 0); // reserved time configuration
734 put_bits(&pb, 8, mc->floor[j]);
735 put_bits(&pb, 8, mc->residue[j]);
740 put_bits(&pb, 6, venc->nmodes - 1);
741 for (i = 0; i < venc->nmodes; i++) {
742 put_bits(&pb, 1, venc->modes[i].blockflag);
743 put_bits(&pb, 16, 0); // reserved window type
744 put_bits(&pb, 16, 0); // reserved transform type
745 put_bits(&pb, 8, venc->modes[i].mapping);
748 put_bits(&pb, 1, 1); // framing
751 hlens[2] = (put_bits_count(&pb) + 7) / 8;
753 len = hlens[0] + hlens[1] + hlens[2];
754 p = *out = av_mallocz(64 + len + len/255);
757 p += av_xiphlacing(p, hlens[0]);
758 p += av_xiphlacing(p, hlens[1]);
760 for (i = 0; i < 3; i++) {
761 memcpy(p, buffer + buffer_len, hlens[i]);
763 buffer_len += hlens[i];
769 static float get_floor_average(floor_t * fc, float * coeffs, int i) {
770 int begin = fc->list[fc->list[FFMAX(i-1, 0)].sort].x;
771 int end = fc->list[fc->list[FFMIN(i+1, fc->values - 1)].sort].x;
775 for (j = begin; j < end; j++) average += fabs(coeffs[j]);
776 return average / (end - begin);
779 static void floor_fit(venc_context_t * venc, floor_t * fc, float * coeffs, int * posts, int samples) {
780 int range = 255 / fc->multiplier + 1;
782 float tot_average = 0.;
783 for (i = 0; i < fc->values; i++) tot_average += get_floor_average(fc, coeffs, i);
784 tot_average /= fc->values;
785 tot_average /= venc->quality;
787 for (i = 0; i < fc->values; i++) {
788 int position = fc->list[fc->list[i].sort].x;
789 float average = get_floor_average(fc, coeffs, i);
792 average /= pow(average, 0.5) / tot_average * pow(0.8, position/200.); // MAGIC!
793 for (j = 0; j < range - 1; j++) if (ff_vorbis_floor1_inverse_db_table[j * fc->multiplier] > average) break;
794 posts[fc->list[i].sort] = j;
798 static int render_point(int x0, int y0, int x1, int y1, int x) {
799 return y0 + (x - x0) * (y1 - y0) / (x1 - x0);
802 static void render_line(int x0, int y0, int x1, int y1, float * buf, int n) {
805 int ady = FFMAX(dy, -dy);
811 if (dy < 0) sy = base - 1;
813 ady = ady - FFMAX(base, -base) * adx;
815 buf[x] = ff_vorbis_floor1_inverse_db_table[y];
816 for (x = x0 + 1; x < x1; x++) {
825 buf[x] = ff_vorbis_floor1_inverse_db_table[y];
829 static void floor_encode(venc_context_t * venc, floor_t * fc, PutBitContext * pb, int * posts, float * floor, int samples) {
830 int range = 255 / fc->multiplier + 1;
831 int coded[fc->values]; // first 2 values are unused
835 put_bits(pb, 1, 1); // non zero
836 put_bits(pb, ilog(range - 1), posts[0]);
837 put_bits(pb, ilog(range - 1), posts[1]);
838 coded[0] = coded[1] = 1;
840 for (i = 2; i < fc->values; i++) {
841 int predicted = render_point(fc->list[fc->list[i].low].x,
842 posts[fc->list[i].low],
843 fc->list[fc->list[i].high].x,
844 posts[fc->list[i].high],
846 int highroom = range - predicted;
847 int lowroom = predicted;
848 int room = FFMIN(highroom, lowroom);
849 if (predicted == posts[i]) {
850 coded[i] = 0; // must be used later as flag!
853 if (!coded[fc->list[i].low]) coded[fc->list[i].low] = -1;
854 if (!coded[fc->list[i].high]) coded[fc->list[i].high] = -1;
856 if (posts[i] > predicted) {
857 if (posts[i] - predicted > room) coded[i] = posts[i] - predicted + lowroom;
858 else coded[i] = (posts[i] - predicted) << 1;
860 if (predicted - posts[i] > room) coded[i] = predicted - posts[i] + highroom - 1;
861 else coded[i] = ((predicted - posts[i]) << 1) - 1;
866 for (i = 0; i < fc->partitions; i++) {
867 floor_class_t * c = &fc->classes[fc->partition_to_class[i]];
868 int k, cval = 0, csub = 1<<c->subclass;
870 codebook_t * book = &venc->codebooks[c->masterbook];
872 for (k = 0; k < c->dim; k++) {
874 for (l = 0; l < csub; l++) {
876 if (c->books[l] != -1) maxval = venc->codebooks[c->books[l]].nentries;
877 // coded could be -1, but this still works, cause thats 0
878 if (coded[counter + k] < maxval) break;
882 cshift += c->subclass;
884 assert(cval < book->nentries);
885 put_bits(pb, book->entries[cval].len, book->entries[cval].codeword);
887 for (k = 0; k < c->dim; k++) {
888 int book = c->books[cval & (csub-1)];
889 int entry = coded[counter++];
890 cval >>= c->subclass;
891 if (book == -1) continue;
892 if (entry == -1) entry = 0;
893 assert(entry < venc->codebooks[book].nentries);
895 put_bits(pb, venc->codebooks[book].entries[entry].len, venc->codebooks[book].entries[entry].codeword);
900 ly = posts[0] * fc->multiplier; // sorted 0 is still 0
901 for (i = 1; i < fc->values; i++) {
902 int pos = fc->list[i].sort;
904 render_line(lx, ly, fc->list[pos].x, posts[pos] * fc->multiplier, floor, samples);
905 lx = fc->list[pos].x;
906 ly = posts[pos] * fc->multiplier;
908 if (lx >= samples) break;
910 if (lx < samples) render_line(lx, ly, samples, ly, floor, samples);
913 static float * put_vector(codebook_t * book, PutBitContext * pb, float * num) {
917 assert(book->dimentions);
918 for (i = 0; i < book->nentries; i++) {
921 if (!book->entries[i].len) continue;
922 for (j = 0; j < book->ndimentions; j++) {
923 float a = (book->dimentions[i * book->ndimentions + j] - num[j]);
926 if (entry == -1 || distance > d) {
931 put_bits(pb, book->entries[entry].len, book->entries[entry].codeword);
932 return &book->dimentions[entry * book->ndimentions];
935 static void residue_encode(venc_context_t * venc, residue_t * rc, PutBitContext * pb, float * coeffs, int samples, int real_ch) {
936 int pass, i, j, p, k;
937 int psize = rc->partition_size;
938 int partitions = (rc->end - rc->begin) / psize;
939 int channels = (rc->type == 2) ? 1 : real_ch;
940 int classes[channels][partitions];
941 int classwords = venc->codebooks[rc->classbook].ndimentions;
943 assert(rc->type == 2);
944 assert(real_ch == 2);
945 for (p = 0; p < partitions; p++) {
946 float max1 = 0., max2 = 0.;
947 int s = rc->begin + p * psize;
948 for (k = s; k < s + psize; k += 2) {
949 if (fabs(coeffs[k / real_ch]) > max1) max1 = fabs(coeffs[k / real_ch]);
950 if (fabs(coeffs[samples + k / real_ch]) > max2) max2 = fabs(coeffs[samples + k / real_ch]);
953 for (i = 0; i < rc->classifications - 1; i++) {
954 if (max1 < rc->maxes[i][0] && max2 < rc->maxes[i][1]) break;
959 for (pass = 0; pass < 8; pass++) {
961 while (p < partitions) {
962 if (pass == 0) for (j = 0; j < channels; j++) {
963 codebook_t * book = &venc->codebooks[rc->classbook];
965 for (i = 0; i < classwords; i++) {
966 entry *= rc->classifications;
967 entry += classes[j][p + i];
969 assert(entry < book->nentries);
971 put_bits(pb, book->entries[entry].len, book->entries[entry].codeword);
973 for (i = 0; i < classwords && p < partitions; i++, p++) {
974 for (j = 0; j < channels; j++) {
975 int nbook = rc->books[classes[j][p]][pass];
976 codebook_t * book = &venc->codebooks[nbook];
977 float * buf = coeffs + samples*j + rc->begin + p*psize;
978 if (nbook == -1) continue;
980 assert(rc->type == 0 || rc->type == 2);
981 assert(!(psize % book->ndimentions));
984 for (k = 0; k < psize; k += book->ndimentions) {
985 float * a = put_vector(book, pb, &buf[k]);
987 for (l = 0; l < book->ndimentions; l++) buf[k + l] -= a[l];
990 for (k = 0; k < psize; k += book->ndimentions) {
991 int dim = book->ndimentions, s = rc->begin + p * psize + k, l;
992 float vec[dim], * a = vec;
993 for (l = s; l < s + dim; l++)
994 *a++ = coeffs[(l % real_ch) * samples + l / real_ch];
995 a = put_vector(book, pb, vec);
996 for (l = s; l < s + dim; l++)
997 coeffs[(l % real_ch) * samples + l / real_ch] -= *a++;
1006 static int window(venc_context_t * venc, signed short * audio, int samples) {
1008 const float * win = venc->win[0];
1009 int window_len = 1 << (venc->blocksize[0] - 1);
1010 float n = (float)(1 << venc->blocksize[0]) / 4.;
1013 if (!venc->have_saved && !samples) return 0;
1015 if (venc->have_saved) {
1016 for (channel = 0; channel < venc->channels; channel++) {
1017 memcpy(venc->samples + channel*window_len*2, venc->saved + channel*window_len, sizeof(float)*window_len);
1020 for (channel = 0; channel < venc->channels; channel++) {
1021 memset(venc->samples + channel*window_len*2, 0, sizeof(float)*window_len);
1026 for (channel = 0; channel < venc->channels; channel++) {
1027 float * offset = venc->samples + channel*window_len*2 + window_len;
1029 for (i = 0; i < samples; i++, j += venc->channels)
1030 offset[i] = audio[j] / 32768. / n * win[window_len - i - 1];
1033 for (channel = 0; channel < venc->channels; channel++) {
1034 memset(venc->samples + channel*window_len*2 + window_len, 0, sizeof(float)*window_len);
1038 for (channel = 0; channel < venc->channels; channel++) {
1039 ff_mdct_calc(&venc->mdct[0], venc->coeffs + channel*window_len, venc->samples + channel*window_len*2, venc->floor/*tmp*/);
1043 for (channel = 0; channel < venc->channels; channel++) {
1044 float * offset = venc->saved + channel*window_len;
1046 for (i = 0; i < samples; i++, j += venc->channels)
1047 offset[i] = audio[j] / 32768. / n * win[i];
1049 venc->have_saved = 1;
1051 venc->have_saved = 0;
1056 static int vorbis_encode_init(AVCodecContext * avccontext)
1058 venc_context_t * venc = avccontext->priv_data;
1060 create_vorbis_context(venc, avccontext);
1062 if (avccontext->flags & CODEC_FLAG_QSCALE) venc->quality = avccontext->global_quality / (float)FF_QP2LAMBDA / 100.;
1063 else venc->quality = 0.17;
1064 //if(avccontext->cutoff > 0) cfreq = avccontext->cutoff / 1000.0;
1066 avccontext->extradata_size = put_main_header(venc, (uint8_t**)&avccontext->extradata);
1068 avccontext->frame_size = 1 << (venc->blocksize[0] - 1);
1070 avccontext->coded_frame = avcodec_alloc_frame();
1071 avccontext->coded_frame->key_frame = 1;
1076 static int vorbis_encode_frame(AVCodecContext * avccontext, unsigned char * packets, int buf_size, void *data)
1078 venc_context_t * venc = avccontext->priv_data;
1079 signed short * audio = data;
1080 int samples = data ? avccontext->frame_size : 0;
1081 vorbis_mode_t * mode;
1082 mapping_t * mapping;
1086 if (!window(venc, audio, samples)) return 0;
1087 samples = 1 << (venc->blocksize[0] - 1);
1089 init_put_bits(&pb, packets, buf_size);
1091 put_bits(&pb, 1, 0); // magic bit
1093 put_bits(&pb, ilog(venc->nmodes - 1), 0); // 0 bits, the mode
1095 mode = &venc->modes[0];
1096 mapping = &venc->mappings[mode->mapping];
1097 if (mode->blockflag) {
1098 put_bits(&pb, 1, 0);
1099 put_bits(&pb, 1, 0);
1102 for (i = 0; i < venc->channels; i++) {
1103 floor_t * fc = &venc->floors[mapping->floor[mapping->mux[i]]];
1104 int posts[fc->values];
1105 floor_fit(venc, fc, &venc->coeffs[i * samples], posts, samples);
1106 floor_encode(venc, fc, &pb, posts, &venc->floor[i * samples], samples);
1109 for (i = 0; i < venc->channels; i++) {
1111 for (j = 0; j < samples; j++) {
1112 venc->coeffs[i * samples + j] /= venc->floor[i * samples + j];
1116 for (i = 0; i < mapping->coupling_steps; i++) {
1117 float * mag = venc->coeffs + mapping->magnitude[i] * samples;
1118 float * ang = venc->coeffs + mapping->angle[i] * samples;
1120 for (j = 0; j < samples; j++) {
1125 if (a > m) mag[j] = a;
1129 if (a > m) mag[j] = m;
1135 residue_encode(venc, &venc->residues[mapping->residue[mapping->mux[0]]], &pb, venc->coeffs, samples, venc->channels);
1137 flush_put_bits(&pb);
1138 return (put_bits_count(&pb) + 7) / 8;
1142 static int vorbis_encode_close(AVCodecContext * avccontext)
1144 venc_context_t * venc = avccontext->priv_data;
1147 if (venc->codebooks) for (i = 0; i < venc->ncodebooks; i++) {
1148 av_freep(&venc->codebooks[i].entries);
1149 av_freep(&venc->codebooks[i].quantlist);
1150 av_freep(&venc->codebooks[i].dimentions);
1152 av_freep(&venc->codebooks);
1154 if (venc->floors) for (i = 0; i < venc->nfloors; i++) {
1156 av_freep(&venc->floors[i].classes);
1157 if (venc->floors[i].classes)
1158 for (j = 0; j < venc->floors[i].nclasses; j++)
1159 av_freep(&venc->floors[i].classes[j].books);
1160 av_freep(&venc->floors[i].partition_to_class);
1161 av_freep(&venc->floors[i].list);
1163 av_freep(&venc->floors);
1165 if (venc->residues) for (i = 0; i < venc->nresidues; i++) {
1166 av_freep(&venc->residues[i].books);
1167 av_freep(&venc->residues[i].maxes);
1169 av_freep(&venc->residues);
1171 if (venc->mappings) for (i = 0; i < venc->nmappings; i++) {
1172 av_freep(&venc->mappings[i].mux);
1173 av_freep(&venc->mappings[i].floor);
1174 av_freep(&venc->mappings[i].residue);
1176 av_freep(&venc->mappings);
1178 av_freep(&venc->modes);
1180 av_freep(&venc->saved);
1181 av_freep(&venc->samples);
1182 av_freep(&venc->floor);
1183 av_freep(&venc->coeffs);
1185 ff_mdct_end(&venc->mdct[0]);
1186 ff_mdct_end(&venc->mdct[1]);
1188 av_freep(&avccontext->coded_frame);
1189 av_freep(&avccontext->extradata);
1194 AVCodec vorbis_encoder = {
1198 sizeof(venc_context_t),
1200 vorbis_encode_frame,
1201 vorbis_encode_close,
1202 .capabilities= CODEC_CAP_DELAY,