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>
27 #define BITSTREAM_H // don't include this
29 typedef int GetBitContext;
35 //#define ALT_BITSTREAM_WRITER
36 //#include "bitstream.h"
71 int * partition_to_class;
73 floor_class_t * classes;
109 int blocksize[2]; // in (1<<n) format
111 const float * win[2];
115 float * floor; // also used for tmp values for mdct
116 float * coeffs; // also used for residue after floor
119 codebook_t * codebooks;
125 residue_t * residues;
128 mapping_t * mappings;
131 vorbis_mode_t * modes;
141 #define ilog(i) av_log2(2*(i))
143 static inline void init_put_bits(PutBitContext * pb, uint8_t * buf, int buffer_len) {
144 pb->total = buffer_len * 8;
150 static void put_bits(PutBitContext * pb, int bits, uint64_t val) {
151 if ((pb->total_pos += bits) >= pb->total) return;
154 if (pb->pos > bits) {
155 *pb->buf_ptr |= val << (8 - pb->pos);
159 *pb->buf_ptr++ |= (val << (8 - pb->pos)) & 0xFF;
165 for (; bits >= 8; bits -= 8) {
166 *pb->buf_ptr++ = val & 0xFF;
175 static inline void flush_put_bits(PutBitContext * pb) {
178 static inline int put_bits_count(PutBitContext * pb) {
179 return pb->total_pos;
182 static int cb_lookup_vals(int lookup, int dimentions, int entries) {
185 for (tmp = 0; ; tmp++) {
187 for (i = 0; i < dimentions; i++) n *= tmp;
188 if (n > entries) break;
191 } else if (lookup == 2) return dimentions * entries;
195 static void ready_codebook(codebook_t * cb) {
196 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 };
199 for (i = 0; i < cb->nentries; i++) {
200 cb_entry_t * e = &cb->entries[i];
202 if (!e->len) continue;
205 for (j = e->len; j; j--)
211 for (j++; j <= e->len; j++) h[j] = e->codeword | (1 << (j - 1));
213 for (i = 0; i < 33; i++) assert(!h[i]);
215 if (!cb->lookup) cb->dimentions = NULL;
217 int vals = cb_lookup_vals(cb->lookup, cb->ndimentions, cb->nentries);
218 cb->dimentions = av_malloc(sizeof(float) * cb->nentries * cb->ndimentions);
219 for (i = 0; i < cb->nentries; i++) {
223 for (j = 0; j < cb->ndimentions; j++) {
225 if (cb->lookup == 1) off = (i / div) % vals; // lookup type 1
226 else off = i * cb->ndimentions + j; // lookup type 2
228 cb->dimentions[i * cb->ndimentions + j] = last + cb->min + cb->quantlist[off] * cb->delta;
229 if (cb->seq_p) last = cb->dimentions[i * cb->ndimentions + j];
237 static void ready_floor(floor_t * fc) {
239 fc->list[0].sort = 0;
240 fc->list[1].sort = 1;
241 for (i = 2; i < fc->values; i++) {
244 fc->list[i].high = 1;
245 fc->list[i].sort = i;
246 for (j = 2; j < i; j++) {
247 int tmp = fc->list[j].x;
248 if (tmp < fc->list[i].x) {
249 if (tmp > fc->list[fc->list[i].low].x) fc->list[i].low = j;
251 if (tmp < fc->list[fc->list[i].high].x) fc->list[i].high = j;
255 for (i = 0; i < fc->values - 1; i++) {
257 for (j = i + 1; j < fc->values; j++) {
258 if (fc->list[fc->list[i].sort].x > fc->list[fc->list[j].sort].x) {
259 int tmp = fc->list[i].sort;
260 fc->list[i].sort = fc->list[j].sort;
261 fc->list[j].sort = tmp;
267 static void ready_residue(residue_t * rc, venc_context_t * venc) {
269 assert(rc->type == 2);
270 rc->maxes = av_mallocz(sizeof(float[2]) * rc->classifications);
271 for (i = 0; i < rc->classifications; i++) {
274 for (j = 0; j < 8; j++) if (rc->books[i][j] != -1) break;
275 if (j == 8) continue; // zero
276 cb = &venc->codebooks[rc->books[i][j]];
277 assert(cb->ndimentions >= 2);
280 for (j = 0; j < cb->nentries; j++) {
282 if (!cb->entries[j].len) continue;
283 a = fabs(cb->dimentions[j * cb->ndimentions]);
284 if (a > rc->maxes[i][0]) rc->maxes[i][0] = a;
285 a = fabs(cb->dimentions[j * cb->ndimentions + 1]);
286 if (a > rc->maxes[i][1]) rc->maxes[i][1] = a;
290 for (i = 0; i < rc->classifications; i++) {
291 rc->maxes[i][0] += 0.8;
292 rc->maxes[i][1] += 0.8;
296 static void create_vorbis_context(venc_context_t * venc, AVCodecContext * avccontext) {
303 venc->channels = avccontext->channels;
304 venc->sample_rate = avccontext->sample_rate;
305 venc->blocksize[0] = venc->blocksize[1] = 11;
307 venc->ncodebooks = 29;
308 venc->codebooks = av_malloc(sizeof(codebook_t) * venc->ncodebooks);
310 int codebook0[] = { 2, 10, 8, 14, 7, 12, 11, 14, 1, 5, 3, 7, 4, 9, 7, 13, };
311 int codebook1[] = { 1, 4, 2, 6, 3, 7, 5, 7, };
312 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, };
313 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, };
314 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, };
315 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, };
316 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, };
317 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, };
318 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, };
319 int codebook9[] = { 4, 5, 4, 5, 3, 5, 3, 5, 3, 5, 4, 4, 4, 4, 5, 5, 5, };
320 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, };
321 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, };
322 int codebook12[] = { 4, 5, 4, 5, 4, 5, 4, 5, 3, 5, 3, 5, 3, 5, 4, 5, 4, };
323 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, };
324 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, };
325 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, };
326 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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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, };
332 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, };
333 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, };
334 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, };
335 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, };
336 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, };
337 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, };
338 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, };
340 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, };
341 int * codebook_lens[] = { codebook0, codebook1, codebook2, codebook3, codebook4, codebook5, codebook6, codebook7,
342 codebook8, codebook9, codebook10, codebook11, codebook12, codebook13, codebook14, codebook15,
343 codebook16, codebook17, codebook18, codebook19, codebook20, codebook21, codebook22, codebook23,
344 codebook24, codebook25, codebook26, codebook27, codebook28, };
354 { 1, 8, -1.0, 1.0, 6561,(int[]){ 1, 0, 2, } },
355 { 1, 4, -2.0, 1.0, 625, (int[]){ 2, 1, 3, 0, 4, } },
356 { 1, 4, -2.0, 1.0, 625, (int[]){ 2, 1, 3, 0, 4, } },
357 { 1, 2, -4.0, 1.0, 81, (int[]){ 4, 3, 5, 2, 6, 1, 7, 0, 8, } },
358 { 1, 2, -4.0, 1.0, 81, (int[]){ 4, 3, 5, 2, 6, 1, 7, 0, 8, } },
359 { 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 { 1, 4, -11.0, 11.0, 81, (int[]){ 1, 0, 2, } },
361 { 1, 2, -5.0, 1.0, 121, (int[]){ 5, 4, 6, 3, 7, 2, 8, 1, 9, 0, 10, } },
362 { 1, 2, -30.0, 5.0, 169, (int[]){ 6, 5, 7, 4, 8, 3, 9, 2, 10, 1, 11, 0, 12, } },
363 { 1, 2, -2.0, 1.0, 25, (int[]){ 2, 1, 3, 0, 4, } },
364 { 1, 2, -1530.0, 255.0, 169, (int[]){ 6, 5, 7, 4, 8, 3, 9, 2, 10, 1, 11, 0, 12, } },
365 { 1, 2, -119.0, 17.0, 225, (int[]){ 7, 6, 8, 5, 9, 4, 10, 3, 11, 2, 12, 1, 13, 0, 14, } },
366 { 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, } },
369 // codebook 0..14 - floor1 book, values 0..255
370 // codebook 15 residue masterbook
371 // codebook 16..29 residue
372 for (book = 0; book < venc->ncodebooks; book++) {
373 cb = &venc->codebooks[book];
374 cb->nentries = codebook_sizes[book];
381 cb->quantlist = NULL;
385 cb->nentries = cvectors[book - 16].real_len;
386 cb->ndimentions = cvectors[book - 16].dim;
387 cb->min = cvectors[book - 16].min;
388 cb->delta = cvectors[book - 16].delta;
389 cb->lookup = cvectors[book - 16].lookup;
390 vals = cb_lookup_vals(cb->lookup, cb->ndimentions, cb->nentries);
391 cb->quantlist = av_malloc(sizeof(int) * vals);
392 for (i = 0; i < vals; i++) cb->quantlist[i] = cvectors[book - 16].quant[i];
394 cb->entries = av_malloc(sizeof(cb_entry_t) * cb->nentries);
395 for (i = 0; i < cb->nentries; i++) {
396 if (i < codebook_sizes[book]) cb->entries[i].len = codebook_lens[book][i];
397 else cb->entries[i].len = 0;
403 venc->floors = av_malloc(sizeof(floor_t) * venc->nfloors);
406 fc = &venc->floors[0];
408 fc->partition_to_class = av_malloc(sizeof(int) * fc->partitions);
410 for (i = 0; i < fc->partitions; i++) {
411 int a[] = {0,1,2,2,3,3,4,4};
412 fc->partition_to_class[i] = a[i];
413 fc->nclasses = FFMAX(fc->nclasses, fc->partition_to_class[i]);
416 fc->classes = av_malloc(sizeof(floor_class_t) * fc->nclasses);
417 for (i = 0; i < fc->nclasses; i++) {
418 floor_class_t * c = &fc->classes[i];
420 int dim[] = {3,4,3,4,3};
421 int subclass[] = {0,1,1,2,2};
422 int masterbook[] = {0/*none*/,0,1,2,3};
427 (int[]){ -1, 9, 10, 11 },
428 (int[]){ -1, 12, 13, 14 },
431 c->subclass = subclass[i];
432 c->masterbook = masterbook[i];
433 books = (1 << c->subclass);
434 c->books = av_malloc(sizeof(int) * books);
435 for (j = 0; j < books; j++) c->books[j] = nbooks[i][j];
438 fc->rangebits = venc->blocksize[0] - 1;
441 for (i = 0; i < fc->partitions; i++)
442 fc->values += fc->classes[fc->partition_to_class[i]].dim;
444 fc->list = av_malloc(sizeof(floor_entry_t) * fc->values);
446 fc->list[1].x = 1 << fc->rangebits;
447 for (i = 2; i < fc->values; i++) {
450 assert(g <= fc->rangebits);
452 g = 1 << (fc->rangebits - g);
453 fc->list[i].x = g + a*2*g;*/
454 //int a[] = {14, 4, 58, 2, 8, 28, 90};
455 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};
456 fc->list[i].x = a[i - 2];
461 venc->residues = av_malloc(sizeof(residue_t) * venc->nresidues);
464 rc = &venc->residues[0];
468 rc->partition_size = 32;
469 rc->classifications = 10;
471 rc->books = av_malloc(sizeof(int[8]) * rc->classifications);
472 for (i = 0; i < rc->classifications; i++) {
474 { -1, -1, -1, -1, -1, -1, -1, -1, },
475 { -1, -1, 16, -1, -1, -1, -1, -1, },
476 { -1, -1, 17, -1, -1, -1, -1, -1, },
477 { -1, -1, 18, -1, -1, -1, -1, -1, },
478 { -1, -1, 19, -1, -1, -1, -1, -1, },
479 { -1, -1, 20, -1, -1, -1, -1, -1, },
480 { -1, -1, 21, -1, -1, -1, -1, -1, },
481 { 22, 23, -1, -1, -1, -1, -1, -1, },
482 { 24, 25, -1, -1, -1, -1, -1, -1, },
483 { 26, 27, 28, -1, -1, -1, -1, -1, },
486 for (j = 0; j < 8; j++) rc->books[i][j] = a[i][j];
488 ready_residue(rc, venc);
491 venc->mappings = av_malloc(sizeof(mapping_t) * venc->nmappings);
494 mc = &venc->mappings[0];
496 mc->mux = av_malloc(sizeof(int) * venc->channels);
497 for (i = 0; i < venc->channels; i++) mc->mux[i] = 0;
498 mc->floor = av_malloc(sizeof(int) * mc->submaps);
499 mc->residue = av_malloc(sizeof(int) * mc->submaps);
500 for (i = 0; i < mc->submaps; i++) {
504 mc->coupling_steps = venc->channels == 2 ? 1 : 0;
505 mc->magnitude = av_malloc(sizeof(int) * mc->coupling_steps);
506 mc->angle = av_malloc(sizeof(int) * mc->coupling_steps);
507 if (mc->coupling_steps) {
508 mc->magnitude[0] = 0;
513 venc->modes = av_malloc(sizeof(vorbis_mode_t) * venc->nmodes);
516 venc->modes[0].blockflag = 0;
517 venc->modes[0].mapping = 0;
519 venc->have_saved = 0;
520 venc->saved = av_malloc(sizeof(float) * venc->channels * (1 << venc->blocksize[1]) / 2);
521 venc->samples = av_malloc(sizeof(float) * venc->channels * (1 << venc->blocksize[1]));
522 venc->floor = av_malloc(sizeof(float) * venc->channels * (1 << venc->blocksize[1]) / 2);
523 venc->coeffs = av_malloc(sizeof(float) * venc->channels * (1 << venc->blocksize[1]) / 2);
526 const float *vwin[8]={ vwin64, vwin128, vwin256, vwin512, vwin1024, vwin2048, vwin4096, vwin8192 };
527 venc->win[0] = vwin[venc->blocksize[0] - 6];
528 venc->win[1] = vwin[venc->blocksize[1] - 6];
531 ff_mdct_init(&venc->mdct[0], venc->blocksize[0], 0);
532 ff_mdct_init(&venc->mdct[1], venc->blocksize[1], 0);
535 static void put_float(PutBitContext * pb, float f) {
538 mant = (int)ldexp(frexp(f, &exp), 20);
540 if (mant < 0) { res |= (1 << 31); mant = -mant; }
541 res |= mant | (exp << 21);
542 put_bits(pb, 32, res);
545 static void put_codebook_header(PutBitContext * pb, codebook_t * cb) {
549 put_bits(pb, 24, 0x564342); //magic
550 put_bits(pb, 16, cb->ndimentions);
551 put_bits(pb, 24, cb->nentries);
553 for (i = 1; i < cb->nentries; i++) if (cb->entries[i].len < cb->entries[i-1].len) break;
554 if (i == cb->nentries) ordered = 1;
556 put_bits(pb, 1, ordered);
558 int len = cb->entries[0].len;
559 put_bits(pb, 5, len - 1);
561 while (i < cb->nentries) {
563 for (j = 0; j+i < cb->nentries; j++) if (cb->entries[j+i].len != len) break;
564 put_bits(pb, ilog(cb->nentries - i), j);
570 for (i = 0; i < cb->nentries; i++) if (!cb->entries[i].len) break;
571 if (i != cb->nentries) sparse = 1;
572 put_bits(pb, 1, sparse);
574 for (i = 0; i < cb->nentries; i++) {
575 if (sparse) put_bits(pb, 1, !!cb->entries[i].len);
576 if (cb->entries[i].len) put_bits(pb, 5, cb->entries[i].len - 1);
580 put_bits(pb, 4, cb->lookup);
582 int tmp = cb_lookup_vals(cb->lookup, cb->ndimentions, cb->nentries);
583 int bits = ilog(cb->quantlist[0]);
585 for (i = 1; i < tmp; i++) bits = FFMAX(bits, ilog(cb->quantlist[i]));
587 put_float(pb, cb->min);
588 put_float(pb, cb->delta);
590 put_bits(pb, 4, bits - 1);
591 put_bits(pb, 1, cb->seq_p);
593 for (i = 0; i < tmp; i++) put_bits(pb, bits, cb->quantlist[i]);
597 static void put_floor_header(PutBitContext * pb, floor_t * fc) {
600 put_bits(pb, 16, 1); // type, only floor1 is supported
602 put_bits(pb, 5, fc->partitions);
604 for (i = 0; i < fc->partitions; i++) put_bits(pb, 4, fc->partition_to_class[i]);
606 for (i = 0; i < fc->nclasses; i++) {
609 put_bits(pb, 3, fc->classes[i].dim - 1);
610 put_bits(pb, 2, fc->classes[i].subclass);
612 if (fc->classes[i].subclass) put_bits(pb, 8, fc->classes[i].masterbook);
614 books = (1 << fc->classes[i].subclass);
616 for (j = 0; j < books; j++) put_bits(pb, 8, fc->classes[i].books[j] + 1);
619 put_bits(pb, 2, fc->multiplier - 1);
620 put_bits(pb, 4, fc->rangebits);
622 for (i = 2; i < fc->values; i++) put_bits(pb, fc->rangebits, fc->list[i].x);
625 static void put_residue_header(PutBitContext * pb, residue_t * rc) {
628 put_bits(pb, 16, rc->type);
630 put_bits(pb, 24, rc->begin);
631 put_bits(pb, 24, rc->end);
632 put_bits(pb, 24, rc->partition_size - 1);
633 put_bits(pb, 6, rc->classifications - 1);
634 put_bits(pb, 8, rc->classbook);
636 for (i = 0; i < rc->classifications; i++) {
638 for (j = 0; j < 8; j++) tmp |= (rc->books[i][j] != -1) << j;
640 put_bits(pb, 3, tmp & 7);
641 put_bits(pb, 1, tmp > 7);
643 if (tmp > 7) put_bits(pb, 5, tmp >> 3);
646 for (i = 0; i < rc->classifications; i++) {
648 for (j = 0; j < 8; j++)
649 if (rc->books[i][j] != -1)
650 put_bits(pb, 8, rc->books[i][j]);
654 static int put_main_header(venc_context_t * venc, uint8_t ** out) {
657 uint8_t buffer[50000] = {0}, * p = buffer;
658 int buffer_len = sizeof buffer;
661 // identification header
662 init_put_bits(&pb, p, buffer_len);
663 put_bits(&pb, 8, 1); //magic
664 for (i = 0; "vorbis"[i]; i++) put_bits(&pb, 8, "vorbis"[i]);
665 put_bits(&pb, 32, 0); // version
666 put_bits(&pb, 8, venc->channels);
667 put_bits(&pb, 32, venc->sample_rate);
668 put_bits(&pb, 32, 0); // bitrate
669 put_bits(&pb, 32, 0); // bitrate
670 put_bits(&pb, 32, 0); // bitrate
671 put_bits(&pb, 4, venc->blocksize[0]);
672 put_bits(&pb, 4, venc->blocksize[1]);
673 put_bits(&pb, 1, 1); // framing
676 hlens[0] = (put_bits_count(&pb) + 7) / 8;
677 buffer_len -= hlens[0];
681 init_put_bits(&pb, p, buffer_len);
682 put_bits(&pb, 8, 3); //magic
683 for (i = 0; "vorbis"[i]; i++) put_bits(&pb, 8, "vorbis"[i]);
684 put_bits(&pb, 32, 0); // vendor length TODO
685 put_bits(&pb, 32, 0); // amount of comments
686 put_bits(&pb, 1, 1); // framing
689 hlens[1] = (put_bits_count(&pb) + 7) / 8;
690 buffer_len -= hlens[1];
694 init_put_bits(&pb, p, buffer_len);
695 put_bits(&pb, 8, 5); //magic
696 for (i = 0; "vorbis"[i]; i++) put_bits(&pb, 8, "vorbis"[i]);
699 put_bits(&pb, 8, venc->ncodebooks - 1);
700 for (i = 0; i < venc->ncodebooks; i++) put_codebook_header(&pb, &venc->codebooks[i]);
702 // time domain, reserved, zero
704 put_bits(&pb, 16, 0);
707 put_bits(&pb, 6, venc->nfloors - 1);
708 for (i = 0; i < venc->nfloors; i++) put_floor_header(&pb, &venc->floors[i]);
711 put_bits(&pb, 6, venc->nresidues - 1);
712 for (i = 0; i < venc->nresidues; i++) put_residue_header(&pb, &venc->residues[i]);
715 put_bits(&pb, 6, venc->nmappings - 1);
716 for (i = 0; i < venc->nmappings; i++) {
717 mapping_t * mc = &venc->mappings[i];
719 put_bits(&pb, 16, 0); // mapping type
721 put_bits(&pb, 1, mc->submaps > 1);
722 if (mc->submaps > 1) put_bits(&pb, 4, mc->submaps - 1);
724 put_bits(&pb, 1, !!mc->coupling_steps);
725 if (mc->coupling_steps) {
726 put_bits(&pb, 8, mc->coupling_steps - 1);
727 for (j = 0; j < mc->coupling_steps; j++) {
728 put_bits(&pb, ilog(venc->channels - 1), mc->magnitude[j]);
729 put_bits(&pb, ilog(venc->channels - 1), mc->angle[j]);
733 put_bits(&pb, 2, 0); // reserved
735 if (mc->submaps > 1) for (j = 0; j < venc->channels; j++) put_bits(&pb, 4, mc->mux[j]);
737 for (j = 0; j < mc->submaps; j++) {
738 put_bits(&pb, 8, 0); // reserved time configuration
739 put_bits(&pb, 8, mc->floor[j]);
740 put_bits(&pb, 8, mc->residue[j]);
745 put_bits(&pb, 6, venc->nmodes - 1);
746 for (i = 0; i < venc->nmodes; i++) {
747 put_bits(&pb, 1, venc->modes[i].blockflag);
748 put_bits(&pb, 16, 0); // reserved window type
749 put_bits(&pb, 16, 0); // reserved transform type
750 put_bits(&pb, 8, venc->modes[i].mapping);
753 put_bits(&pb, 1, 1); // framing
756 hlens[2] = (put_bits_count(&pb) + 7) / 8;
758 len = hlens[0] + hlens[1] + hlens[2];
759 p = *out = av_mallocz(64 + len + len/255);
762 p += av_xiphlacing(p, hlens[0]);
763 p += av_xiphlacing(p, hlens[1]);
765 for (i = 0; i < 3; i++) {
766 memcpy(p, buffer + buffer_len, hlens[i]);
768 buffer_len += hlens[i];
774 static void floor_fit(venc_context_t * venc, floor_t * fc, float * coeffs, int * posts, int samples) {
775 int range = 255 / fc->multiplier + 1;
777 for (i = 0; i < fc->values; i++) {
778 int position = fc->list[fc->list[i].sort].x;
779 int begin = fc->list[fc->list[FFMAX(i-1, 0)].sort].x;
780 int end = fc->list[fc->list[FFMIN(i+1, fc->values - 1)].sort].x;
783 begin = (position + begin) / 2;
784 end = (position + end ) / 2;
786 assert(end <= samples);
787 for (j = begin; j < end; j++) average += fabs(coeffs[j]);
788 average /= end - begin;
789 average /= 32; // MAGIC!
790 for (j = 0; j < range - 1; j++) if (floor1_inverse_db_table[j * fc->multiplier] > average) break;
791 posts[fc->list[i].sort] = j;
795 static int render_point(int x0, int y0, int x1, int y1, int x) {
796 return y0 + (x - x0) * (y1 - y0) / (x1 - x0);
799 static void render_line(int x0, int y0, int x1, int y1, float * buf, int n) {
802 int ady = FFMAX(dy, -dy);
808 if (dy < 0) sy = base - 1;
810 ady = ady - FFMAX(base, -base) * adx;
812 buf[x] = floor1_inverse_db_table[y];
813 for (x = x0 + 1; x < x1; x++) {
822 buf[x] = floor1_inverse_db_table[y];
826 static void floor_encode(venc_context_t * venc, floor_t * fc, PutBitContext * pb, int * posts, float * floor, int samples) {
827 int range = 255 / fc->multiplier + 1;
828 int coded[fc->values]; // first 2 values are unused
832 put_bits(pb, 1, 1); // non zero
833 put_bits(pb, ilog(range - 1), posts[0]);
834 put_bits(pb, ilog(range - 1), posts[1]);
836 for (i = 2; i < fc->values; i++) {
837 int predicted = render_point(fc->list[fc->list[i].low].x,
838 posts[fc->list[i].low],
839 fc->list[fc->list[i].high].x,
840 posts[fc->list[i].high],
842 int highroom = range - predicted;
843 int lowroom = predicted;
844 int room = FFMIN(highroom, lowroom);
845 if (predicted == posts[i]) {
846 coded[i] = 0; // must be used later as flag!
849 if (!coded[fc->list[i].low]) coded[fc->list[i].low] = -1;
850 if (!coded[fc->list[i].high]) coded[fc->list[i].high] = -1;
852 if (posts[i] > predicted) {
853 if (posts[i] - predicted > room) coded[i] = posts[i] - predicted + lowroom;
854 else coded[i] = (posts[i] - predicted) << 1;
856 if (predicted - posts[i] > room) coded[i] = predicted - posts[i] + highroom - 1;
857 else coded[i] = ((predicted - posts[i]) << 1) - 1;
862 for (i = 0; i < fc->partitions; i++) {
863 floor_class_t * c = &fc->classes[fc->partition_to_class[i]];
864 int k, cval = 0, csub = 1<<c->subclass;
866 codebook_t * book = &venc->codebooks[c->masterbook];
868 for (k = 0; k < c->dim; k++) {
870 for (l = 0; l < csub; l++) {
872 if (c->books[l] != -1) maxval = venc->codebooks[c->books[l]].nentries;
873 // coded could be -1, but this still works, cause thats 0
874 if (coded[counter + k] < maxval) break;
878 cshift += c->subclass;
880 assert(cval < book->nentries);
881 put_bits(pb, book->entries[cval].len, book->entries[cval].codeword);
883 for (k = 0; k < c->dim; k++) {
884 int book = c->books[cval & (csub-1)];
885 int entry = coded[counter++];
886 cval >>= c->subclass;
887 if (book == -1) continue;
888 if (entry == -1) entry = 0;
889 assert(entry < venc->codebooks[book].nentries);
891 put_bits(pb, venc->codebooks[book].entries[entry].len, venc->codebooks[book].entries[entry].codeword);
896 ly = posts[0] * fc->multiplier; // sorted 0 is still 0
897 coded[0] = coded[1] = 1;
898 for (i = 1; i < fc->values; i++) {
899 int pos = fc->list[i].sort;
901 render_line(lx, ly, fc->list[pos].x, posts[pos] * fc->multiplier, floor, samples);
902 lx = fc->list[pos].x;
903 ly = posts[pos] * fc->multiplier;
905 if (lx >= samples) break;
907 if (lx < samples) render_line(lx, ly, samples, ly, floor, samples);
910 static float * put_vector(codebook_t * book, PutBitContext * pb, float * num) {
914 assert(book->dimentions);
915 for (i = 0; i < book->nentries; i++) {
918 for (j = 0; j < book->ndimentions; j++) {
919 float a = (book->dimentions[i * book->ndimentions + j] - num[j]);
922 if (entry == -1 || distance > d) {
927 put_bits(pb, book->entries[entry].len, book->entries[entry].codeword);
928 return &book->dimentions[entry * book->ndimentions];
931 static void residue_encode(venc_context_t * venc, residue_t * rc, PutBitContext * pb, float * coeffs, int samples, int real_ch) {
932 int pass, i, j, p, k;
933 int psize = rc->partition_size;
934 int partitions = (rc->end - rc->begin) / psize;
935 int channels = (rc->type == 2) ? 1 : real_ch;
936 int classes[channels][partitions];
937 int classwords = venc->codebooks[rc->classbook].ndimentions;
939 assert(rc->type == 2);
940 assert(real_ch == 2);
941 for (p = 0; p < partitions; p++) {
942 float max1 = 0., max2 = 0.;
943 int s = rc->begin + p * psize;
944 for (k = s; k < s + psize; k += 2) {
945 if (fabs(coeffs[k / real_ch]) > max1) max1 = fabs(coeffs[k / real_ch]);
946 if (fabs(coeffs[samples + k / real_ch]) > max2) max2 = fabs(coeffs[samples + k / real_ch]);
949 for (i = 0; i < rc->classifications - 1; i++) {
950 if (max1 < rc->maxes[i][0] && max2 < rc->maxes[i][1]) break;
955 for (pass = 0; pass < 8; pass++) {
957 while (p < partitions) {
958 if (pass == 0) for (j = 0; j < channels; j++) {
959 codebook_t * book = &venc->codebooks[rc->classbook];
961 for (i = 0; i < classwords; i++) {
962 entry *= rc->classifications;
963 entry += classes[j][p + i];
965 assert(entry < book->nentries);
967 put_bits(pb, book->entries[entry].len, book->entries[entry].codeword);
969 for (i = 0; i < classwords && p < partitions; i++, p++) {
970 for (j = 0; j < channels; j++) {
971 int nbook = rc->books[classes[j][p]][pass];
972 codebook_t * book = &venc->codebooks[nbook];
973 float * buf = coeffs + samples*j + rc->begin + p*psize;
974 if (nbook == -1) continue;
976 assert(rc->type == 0 || rc->type == 2);
977 assert(!(psize % book->ndimentions));
980 for (k = 0; k < psize; k += book->ndimentions) {
981 float * a = put_vector(book, pb, &buf[k]);
983 for (l = 0; l < book->ndimentions; l++) buf[k + l] -= a[l];
986 for (k = 0; k < psize; k += book->ndimentions) {
987 int dim = book->ndimentions, s = rc->begin + p * psize, l;
988 float vec[dim], * a = vec;
989 for (l = s + k; l < s + k + dim; l++)
990 *a++ = coeffs[(l % real_ch) * samples + l / real_ch];
991 a = put_vector(book, pb, vec);
992 for (l = s + k; l < s + k + dim; l++)
993 coeffs[(l % real_ch) * samples + l / real_ch] -= *a++;
1002 static int window(venc_context_t * venc, signed short * audio, int samples) {
1004 const float * win = venc->win[0];
1005 int window_len = 1 << (venc->blocksize[0] - 1);
1006 float n = (float)(1 << venc->blocksize[0]) / 4.;
1009 if (!venc->have_saved && !samples) return 0;
1011 if (venc->have_saved) {
1012 for (channel = 0; channel < venc->channels; channel++) {
1013 memcpy(venc->samples + channel*window_len*2, venc->saved + channel*window_len, sizeof(float)*window_len);
1016 for (channel = 0; channel < venc->channels; channel++) {
1017 memset(venc->samples + channel*window_len*2, 0, sizeof(float)*window_len);
1022 for (channel = 0; channel < venc->channels; channel++) {
1023 float * offset = venc->samples + channel*window_len*2 + window_len;
1025 for (i = 0; i < samples; i++, j += venc->channels)
1026 offset[i] = audio[j] / 32768. / n * win[window_len - i - 1];
1029 for (channel = 0; channel < venc->channels; channel++) {
1030 memset(venc->samples + channel*window_len*2 + window_len, 0, sizeof(float)*window_len);
1034 for (channel = 0; channel < venc->channels; channel++) {
1035 ff_mdct_calc(&venc->mdct[0], venc->coeffs + channel*window_len, venc->samples + channel*window_len*2, venc->floor/*tmp*/);
1039 for (channel = 0; channel < venc->channels; channel++) {
1040 float * offset = venc->saved + channel*window_len;
1042 for (i = 0; i < samples; i++, j += venc->channels)
1043 offset[i] = audio[j] / 32768. / n * win[i];
1045 venc->have_saved = 1;
1047 venc->have_saved = 0;
1052 static int vorbis_encode_init(AVCodecContext * avccontext)
1054 venc_context_t * venc = avccontext->priv_data;
1056 create_vorbis_context(venc, avccontext);
1058 //if (avccontext->flags & CODEC_FLAG_QSCALE) avccontext->global_quality / (float)FF_QP2LAMBDA); else avccontext->bit_rate;
1059 //if(avccontext->cutoff > 0) cfreq = avccontext->cutoff / 1000.0;
1061 avccontext->extradata_size = put_main_header(venc, (uint8_t**)&avccontext->extradata);
1063 avccontext->frame_size = 1 << (venc->blocksize[0] - 1);
1065 avccontext->coded_frame = avcodec_alloc_frame();
1066 avccontext->coded_frame->key_frame = 1;
1071 static int vorbis_encode_frame(AVCodecContext * avccontext, unsigned char * packets, int buf_size, void *data)
1073 venc_context_t * venc = avccontext->priv_data;
1074 signed short * audio = data;
1075 int samples = data ? avccontext->frame_size : 0;
1076 vorbis_mode_t * mode;
1077 mapping_t * mapping;
1081 if (!window(venc, audio, samples)) return 0;
1082 samples = 1 << (venc->blocksize[0] - 1);
1084 init_put_bits(&pb, packets, buf_size);
1086 put_bits(&pb, 1, 0); // magic bit
1088 put_bits(&pb, ilog(venc->nmodes - 1), 0); // 0 bits, the mode
1090 mode = &venc->modes[0];
1091 mapping = &venc->mappings[mode->mapping];
1092 if (mode->blockflag) {
1093 put_bits(&pb, 1, 0);
1094 put_bits(&pb, 1, 0);
1097 for (i = 0; i < venc->channels; i++) {
1098 floor_t * fc = &venc->floors[mapping->floor[mapping->mux[i]]];
1099 int posts[fc->values];
1100 floor_fit(venc, fc, &venc->coeffs[i * samples], posts, samples);
1101 floor_encode(venc, fc, &pb, posts, &venc->floor[i * samples], samples);
1104 for (i = 0; i < venc->channels; i++) {
1106 for (j = 0; j < samples; j++) {
1107 venc->coeffs[i * samples + j] /= venc->floor[i * samples + j];
1111 for (i = 0; i < mapping->coupling_steps; i++) {
1112 float * mag = venc->coeffs + mapping->magnitude[i] * samples;
1113 float * ang = venc->coeffs + mapping->angle[i] * samples;
1115 for (j = 0; j < samples; j++) {
1120 if (a > m) mag[j] = a;
1124 if (a > m) mag[j] = m;
1130 residue_encode(venc, &venc->residues[mapping->residue[mapping->mux[0]]], &pb, venc->coeffs, samples, venc->channels);
1132 return (put_bits_count(&pb) + 7) / 8;
1136 static int vorbis_encode_close(AVCodecContext * avccontext)
1138 venc_context_t * venc = avccontext->priv_data;
1141 if (venc->codebooks) for (i = 0; i < venc->ncodebooks; i++) {
1142 av_freep(&venc->codebooks[i].entries);
1143 av_freep(&venc->codebooks[i].quantlist);
1144 av_freep(&venc->codebooks[i].dimentions);
1146 av_freep(&venc->codebooks);
1148 if (venc->floors) for (i = 0; i < venc->nfloors; i++) {
1150 av_freep(&venc->floors[i].classes);
1151 if (venc->floors[i].classes)
1152 for (j = 0; j < venc->floors[i].nclasses; j++)
1153 av_freep(&venc->floors[i].classes[j].books);
1154 av_freep(&venc->floors[i].partition_to_class);
1155 av_freep(&venc->floors[i].list);
1157 av_freep(&venc->floors);
1159 if (venc->residues) for (i = 0; i < venc->nresidues; i++) {
1160 av_freep(&venc->residues[i].books);
1161 av_freep(&venc->residues[i].maxes);
1163 av_freep(&venc->residues);
1165 if (venc->mappings) for (i = 0; i < venc->nmappings; i++) {
1166 av_freep(&venc->mappings[i].mux);
1167 av_freep(&venc->mappings[i].floor);
1168 av_freep(&venc->mappings[i].residue);
1170 av_freep(&venc->mappings);
1172 av_freep(&venc->modes);
1174 av_freep(&venc->saved);
1175 av_freep(&venc->samples);
1176 av_freep(&venc->floor);
1177 av_freep(&venc->coeffs);
1179 ff_mdct_end(&venc->mdct[0]);
1180 ff_mdct_end(&venc->mdct[1]);
1182 av_freep(&avccontext->coded_frame);
1183 av_freep(&avccontext->extradata);
1188 AVCodec vorbis_encoder = {
1192 sizeof(venc_context_t),
1194 vorbis_encode_frame,
1195 vorbis_encode_close,
1196 .capabilities= CODEC_CAP_DELAY,