2 * copyright (c) 2006 Oded Shimon <ods15@ods15.dyndns.org>
4 * This file is part of FFmpeg.
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 * Native Vorbis encoder.
24 * @author Oded Shimon <ods15@ods15.dyndns.org>
31 #include "vorbis_enc_data.h"
59 int * partition_to_class
;
61 floor_class_t
* classes
;
65 floor1_entry_t
* list
;
97 int log2_blocksize
[2];
103 float * floor
; // also used for tmp values for mdct
104 float * coeffs
; // also used for residue after floor
108 codebook_t
* codebooks
;
114 residue_t
* residues
;
117 mapping_t
* mappings
;
120 vorbis_mode_t
* modes
;
130 static inline void init_put_bits(PutBitContext
* pb
, uint8_t * buf
, int buffer_len
) {
131 pb
->total
= buffer_len
* 8;
137 static void put_bits(PutBitContext
* pb
, int bits
, uint64_t val
) {
138 if ((pb
->total_pos
+= bits
) >= pb
->total
) return;
141 if (pb
->pos
> bits
) {
142 *pb
->buf_ptr
|= val
<< (8 - pb
->pos
);
146 *pb
->buf_ptr
++ |= (val
<< (8 - pb
->pos
)) & 0xFF;
152 for (; bits
>= 8; bits
-= 8) {
153 *pb
->buf_ptr
++ = val
& 0xFF;
162 static inline void flush_put_bits(PutBitContext
* pb
) {
165 static inline int put_bits_count(PutBitContext
* pb
) {
166 return pb
->total_pos
;
169 static inline void put_codeword(PutBitContext
* pb
, codebook_t
* cb
, int entry
) {
171 assert(entry
< cb
->nentries
);
172 assert(cb
->lens
[entry
]);
173 put_bits(pb
, cb
->lens
[entry
], cb
->codewords
[entry
]);
176 static int cb_lookup_vals(int lookup
, int dimentions
, int entries
) {
177 if (lookup
== 1) return ff_vorbis_nth_root(entries
, dimentions
);
178 else if (lookup
== 2) return dimentions
* entries
;
182 static void ready_codebook(codebook_t
* cb
) {
185 ff_vorbis_len2vlc(cb
->lens
, cb
->codewords
, cb
->nentries
);
188 cb
->pow2
= cb
->dimentions
= NULL
;
190 int vals
= cb_lookup_vals(cb
->lookup
, cb
->ndimentions
, cb
->nentries
);
191 cb
->dimentions
= av_malloc(sizeof(float) * cb
->nentries
* cb
->ndimentions
);
192 cb
->pow2
= av_mallocz(sizeof(float) * cb
->nentries
);
193 for (i
= 0; i
< cb
->nentries
; i
++) {
197 for (j
= 0; j
< cb
->ndimentions
; j
++) {
200 off
= (i
/ div
) % vals
; // lookup type 1
202 off
= i
* cb
->ndimentions
+ j
; // lookup type 2
204 cb
->dimentions
[i
* cb
->ndimentions
+ j
] = last
+ cb
->min
+ cb
->quantlist
[off
] * cb
->delta
;
206 last
= cb
->dimentions
[i
* cb
->ndimentions
+ j
];
207 cb
->pow2
[i
] += cb
->dimentions
[i
* cb
->ndimentions
+ j
]*cb
->dimentions
[i
* cb
->ndimentions
+ j
];
215 static void ready_residue(residue_t
* rc
, venc_context_t
* venc
) {
217 assert(rc
->type
== 2);
218 rc
->maxes
= av_mallocz(sizeof(float[2]) * rc
->classifications
);
219 for (i
= 0; i
< rc
->classifications
; i
++) {
222 for (j
= 0; j
< 8; j
++)
223 if (rc
->books
[i
][j
] != -1) break;
224 if (j
== 8) continue; // zero
225 cb
= &venc
->codebooks
[rc
->books
[i
][j
]];
226 assert(cb
->ndimentions
>= 2);
229 for (j
= 0; j
< cb
->nentries
; j
++) {
231 if (!cb
->lens
[j
]) continue;
232 a
= fabs(cb
->dimentions
[j
* cb
->ndimentions
]);
233 if (a
> rc
->maxes
[i
][0])
235 a
= fabs(cb
->dimentions
[j
* cb
->ndimentions
+ 1]);
236 if (a
> rc
->maxes
[i
][1])
241 for (i
= 0; i
< rc
->classifications
; i
++) {
242 rc
->maxes
[i
][0] += 0.8;
243 rc
->maxes
[i
][1] += 0.8;
247 static void create_vorbis_context(venc_context_t
* venc
, AVCodecContext
* avccontext
) {
253 venc
->channels
= avccontext
->channels
;
254 venc
->sample_rate
= avccontext
->sample_rate
;
255 venc
->log2_blocksize
[0] = venc
->log2_blocksize
[1] = 11;
257 venc
->ncodebooks
= sizeof(cvectors
)/sizeof(cvectors
[0]);
258 venc
->codebooks
= av_malloc(sizeof(codebook_t
) * venc
->ncodebooks
);
260 // codebook 0..14 - floor1 book, values 0..255
261 // codebook 15 residue masterbook
262 // codebook 16..29 residue
263 for (book
= 0; book
< venc
->ncodebooks
; book
++) {
264 codebook_t
* cb
= &venc
->codebooks
[book
];
266 cb
->ndimentions
= cvectors
[book
].dim
;
267 cb
->nentries
= cvectors
[book
].real_len
;
268 cb
->min
= cvectors
[book
].min
;
269 cb
->delta
= cvectors
[book
].delta
;
270 cb
->lookup
= cvectors
[book
].lookup
;
273 cb
->lens
= av_malloc(sizeof(uint8_t) * cb
->nentries
);
274 cb
->codewords
= av_malloc(sizeof(uint32_t) * cb
->nentries
);
275 memcpy(cb
->lens
, cvectors
[book
].clens
, cvectors
[book
].len
);
276 memset(cb
->lens
+ cvectors
[book
].len
, 0, cb
->nentries
- cvectors
[book
].len
);
279 vals
= cb_lookup_vals(cb
->lookup
, cb
->ndimentions
, cb
->nentries
);
280 cb
->quantlist
= av_malloc(sizeof(int) * vals
);
281 for (i
= 0; i
< vals
; i
++)
282 cb
->quantlist
[i
] = cvectors
[book
].quant
[i
];
284 cb
->quantlist
= NULL
;
290 venc
->floors
= av_malloc(sizeof(floor_t
) * venc
->nfloors
);
293 fc
= &venc
->floors
[0];
295 fc
->partition_to_class
= av_malloc(sizeof(int) * fc
->partitions
);
297 for (i
= 0; i
< fc
->partitions
; i
++) {
298 static const int a
[] = {0,1,2,2,3,3,4,4};
299 fc
->partition_to_class
[i
] = a
[i
];
300 fc
->nclasses
= FFMAX(fc
->nclasses
, fc
->partition_to_class
[i
]);
303 fc
->classes
= av_malloc(sizeof(floor_class_t
) * fc
->nclasses
);
304 for (i
= 0; i
< fc
->nclasses
; i
++) {
305 floor_class_t
* c
= &fc
->classes
[i
];
307 c
->dim
= floor_classes
[i
].dim
;
308 c
->subclass
= floor_classes
[i
].subclass
;
309 c
->masterbook
= floor_classes
[i
].masterbook
;
310 books
= (1 << c
->subclass
);
311 c
->books
= av_malloc(sizeof(int) * books
);
312 for (j
= 0; j
< books
; j
++)
313 c
->books
[j
] = floor_classes
[i
].nbooks
[j
];
316 fc
->rangebits
= venc
->log2_blocksize
[0] - 1;
319 for (i
= 0; i
< fc
->partitions
; i
++)
320 fc
->values
+= fc
->classes
[fc
->partition_to_class
[i
]].dim
;
322 fc
->list
= av_malloc(sizeof(floor1_entry_t
) * fc
->values
);
324 fc
->list
[1].x
= 1 << fc
->rangebits
;
325 for (i
= 2; i
< fc
->values
; i
++) {
326 static const int a
[] = {
327 93, 23,372, 6, 46,186,750, 14, 33, 65,
328 130,260,556, 3, 10, 18, 28, 39, 55, 79,
329 111,158,220,312,464,650,850
331 fc
->list
[i
].x
= a
[i
- 2];
333 ff_vorbis_ready_floor1_list(fc
->list
, fc
->values
);
336 venc
->residues
= av_malloc(sizeof(residue_t
) * venc
->nresidues
);
339 rc
= &venc
->residues
[0];
343 rc
->partition_size
= 32;
344 rc
->classifications
= 10;
346 rc
->books
= av_malloc(sizeof(*rc
->books
) * rc
->classifications
);
348 static const int8_t a
[10][8] = {
349 { -1, -1, -1, -1, -1, -1, -1, -1, },
350 { -1, -1, 16, -1, -1, -1, -1, -1, },
351 { -1, -1, 17, -1, -1, -1, -1, -1, },
352 { -1, -1, 18, -1, -1, -1, -1, -1, },
353 { -1, -1, 19, -1, -1, -1, -1, -1, },
354 { -1, -1, 20, -1, -1, -1, -1, -1, },
355 { -1, -1, 21, -1, -1, -1, -1, -1, },
356 { 22, 23, -1, -1, -1, -1, -1, -1, },
357 { 24, 25, -1, -1, -1, -1, -1, -1, },
358 { 26, 27, 28, -1, -1, -1, -1, -1, },
360 memcpy(rc
->books
, a
, sizeof a
);
362 ready_residue(rc
, venc
);
365 venc
->mappings
= av_malloc(sizeof(mapping_t
) * venc
->nmappings
);
368 mc
= &venc
->mappings
[0];
370 mc
->mux
= av_malloc(sizeof(int) * venc
->channels
);
371 for (i
= 0; i
< venc
->channels
; i
++)
373 mc
->floor
= av_malloc(sizeof(int) * mc
->submaps
);
374 mc
->residue
= av_malloc(sizeof(int) * mc
->submaps
);
375 for (i
= 0; i
< mc
->submaps
; i
++) {
379 mc
->coupling_steps
= venc
->channels
== 2 ? 1 : 0;
380 mc
->magnitude
= av_malloc(sizeof(int) * mc
->coupling_steps
);
381 mc
->angle
= av_malloc(sizeof(int) * mc
->coupling_steps
);
382 if (mc
->coupling_steps
) {
383 mc
->magnitude
[0] = 0;
388 venc
->modes
= av_malloc(sizeof(vorbis_mode_t
) * venc
->nmodes
);
391 venc
->modes
[0].blockflag
= 0;
392 venc
->modes
[0].mapping
= 0;
394 venc
->have_saved
= 0;
395 venc
->saved
= av_malloc(sizeof(float) * venc
->channels
* (1 << venc
->log2_blocksize
[1]) / 2);
396 venc
->samples
= av_malloc(sizeof(float) * venc
->channels
* (1 << venc
->log2_blocksize
[1]));
397 venc
->floor
= av_malloc(sizeof(float) * venc
->channels
* (1 << venc
->log2_blocksize
[1]) / 2);
398 venc
->coeffs
= av_malloc(sizeof(float) * venc
->channels
* (1 << venc
->log2_blocksize
[1]) / 2);
400 venc
->win
[0] = ff_vorbis_vwin
[venc
->log2_blocksize
[0] - 6];
401 venc
->win
[1] = ff_vorbis_vwin
[venc
->log2_blocksize
[1] - 6];
403 ff_mdct_init(&venc
->mdct
[0], venc
->log2_blocksize
[0], 0);
404 ff_mdct_init(&venc
->mdct
[1], venc
->log2_blocksize
[1], 0);
407 static void put_float(PutBitContext
* pb
, float f
) {
410 mant
= (int)ldexp(frexp(f
, &exp
), 20);
412 if (mant
< 0) { res
|= (1 << 31); mant
= -mant
; }
413 res
|= mant
| (exp
<< 21);
414 put_bits(pb
, 32, res
);
417 static void put_codebook_header(PutBitContext
* pb
, codebook_t
* cb
) {
421 put_bits(pb
, 24, 0x564342); //magic
422 put_bits(pb
, 16, cb
->ndimentions
);
423 put_bits(pb
, 24, cb
->nentries
);
425 for (i
= 1; i
< cb
->nentries
; i
++)
426 if (cb
->lens
[i
] < cb
->lens
[i
-1]) break;
427 if (i
== cb
->nentries
)
430 put_bits(pb
, 1, ordered
);
432 int len
= cb
->lens
[0];
433 put_bits(pb
, 5, len
- 1);
435 while (i
< cb
->nentries
) {
437 for (j
= 0; j
+i
< cb
->nentries
; j
++)
438 if (cb
->lens
[j
+i
] != len
) break;
439 put_bits(pb
, ilog(cb
->nentries
- i
), j
);
445 for (i
= 0; i
< cb
->nentries
; i
++)
446 if (!cb
->lens
[i
]) break;
447 if (i
!= cb
->nentries
)
449 put_bits(pb
, 1, sparse
);
451 for (i
= 0; i
< cb
->nentries
; i
++) {
452 if (sparse
) put_bits(pb
, 1, !!cb
->lens
[i
]);
453 if (cb
->lens
[i
]) put_bits(pb
, 5, cb
->lens
[i
] - 1);
457 put_bits(pb
, 4, cb
->lookup
);
459 int tmp
= cb_lookup_vals(cb
->lookup
, cb
->ndimentions
, cb
->nentries
);
460 int bits
= ilog(cb
->quantlist
[0]);
462 for (i
= 1; i
< tmp
; i
++)
463 bits
= FFMAX(bits
, ilog(cb
->quantlist
[i
]));
465 put_float(pb
, cb
->min
);
466 put_float(pb
, cb
->delta
);
468 put_bits(pb
, 4, bits
- 1);
469 put_bits(pb
, 1, cb
->seq_p
);
471 for (i
= 0; i
< tmp
; i
++)
472 put_bits(pb
, bits
, cb
->quantlist
[i
]);
476 static void put_floor_header(PutBitContext
* pb
, floor_t
* fc
) {
479 put_bits(pb
, 16, 1); // type, only floor1 is supported
481 put_bits(pb
, 5, fc
->partitions
);
483 for (i
= 0; i
< fc
->partitions
; i
++)
484 put_bits(pb
, 4, fc
->partition_to_class
[i
]);
486 for (i
= 0; i
< fc
->nclasses
; i
++) {
489 put_bits(pb
, 3, fc
->classes
[i
].dim
- 1);
490 put_bits(pb
, 2, fc
->classes
[i
].subclass
);
492 if (fc
->classes
[i
].subclass
)
493 put_bits(pb
, 8, fc
->classes
[i
].masterbook
);
495 books
= (1 << fc
->classes
[i
].subclass
);
497 for (j
= 0; j
< books
; j
++)
498 put_bits(pb
, 8, fc
->classes
[i
].books
[j
] + 1);
501 put_bits(pb
, 2, fc
->multiplier
- 1);
502 put_bits(pb
, 4, fc
->rangebits
);
504 for (i
= 2; i
< fc
->values
; i
++)
505 put_bits(pb
, fc
->rangebits
, fc
->list
[i
].x
);
508 static void put_residue_header(PutBitContext
* pb
, residue_t
* rc
) {
511 put_bits(pb
, 16, rc
->type
);
513 put_bits(pb
, 24, rc
->begin
);
514 put_bits(pb
, 24, rc
->end
);
515 put_bits(pb
, 24, rc
->partition_size
- 1);
516 put_bits(pb
, 6, rc
->classifications
- 1);
517 put_bits(pb
, 8, rc
->classbook
);
519 for (i
= 0; i
< rc
->classifications
; i
++) {
521 for (j
= 0; j
< 8; j
++)
522 tmp
|= (rc
->books
[i
][j
] != -1) << j
;
524 put_bits(pb
, 3, tmp
& 7);
525 put_bits(pb
, 1, tmp
> 7);
528 put_bits(pb
, 5, tmp
>> 3);
531 for (i
= 0; i
< rc
->classifications
; i
++) {
533 for (j
= 0; j
< 8; j
++)
534 if (rc
->books
[i
][j
] != -1)
535 put_bits(pb
, 8, rc
->books
[i
][j
]);
539 static int put_main_header(venc_context_t
* venc
, uint8_t ** out
) {
542 uint8_t buffer
[50000] = {0}, * p
= buffer
;
543 int buffer_len
= sizeof buffer
;
546 // identification header
547 init_put_bits(&pb
, p
, buffer_len
);
548 put_bits(&pb
, 8, 1); //magic
549 for (i
= 0; "vorbis"[i
]; i
++)
550 put_bits(&pb
, 8, "vorbis"[i
]);
551 put_bits(&pb
, 32, 0); // version
552 put_bits(&pb
, 8, venc
->channels
);
553 put_bits(&pb
, 32, venc
->sample_rate
);
554 put_bits(&pb
, 32, 0); // bitrate
555 put_bits(&pb
, 32, 0); // bitrate
556 put_bits(&pb
, 32, 0); // bitrate
557 put_bits(&pb
, 4, venc
->log2_blocksize
[0]);
558 put_bits(&pb
, 4, venc
->log2_blocksize
[1]);
559 put_bits(&pb
, 1, 1); // framing
562 hlens
[0] = (put_bits_count(&pb
) + 7) / 8;
563 buffer_len
-= hlens
[0];
567 init_put_bits(&pb
, p
, buffer_len
);
568 put_bits(&pb
, 8, 3); //magic
569 for (i
= 0; "vorbis"[i
]; i
++)
570 put_bits(&pb
, 8, "vorbis"[i
]);
571 put_bits(&pb
, 32, 0); // vendor length TODO
572 put_bits(&pb
, 32, 0); // amount of comments
573 put_bits(&pb
, 1, 1); // framing
576 hlens
[1] = (put_bits_count(&pb
) + 7) / 8;
577 buffer_len
-= hlens
[1];
581 init_put_bits(&pb
, p
, buffer_len
);
582 put_bits(&pb
, 8, 5); //magic
583 for (i
= 0; "vorbis"[i
]; i
++)
584 put_bits(&pb
, 8, "vorbis"[i
]);
587 put_bits(&pb
, 8, venc
->ncodebooks
- 1);
588 for (i
= 0; i
< venc
->ncodebooks
; i
++)
589 put_codebook_header(&pb
, &venc
->codebooks
[i
]);
591 // time domain, reserved, zero
593 put_bits(&pb
, 16, 0);
596 put_bits(&pb
, 6, venc
->nfloors
- 1);
597 for (i
= 0; i
< venc
->nfloors
; i
++)
598 put_floor_header(&pb
, &venc
->floors
[i
]);
601 put_bits(&pb
, 6, venc
->nresidues
- 1);
602 for (i
= 0; i
< venc
->nresidues
; i
++)
603 put_residue_header(&pb
, &venc
->residues
[i
]);
606 put_bits(&pb
, 6, venc
->nmappings
- 1);
607 for (i
= 0; i
< venc
->nmappings
; i
++) {
608 mapping_t
* mc
= &venc
->mappings
[i
];
610 put_bits(&pb
, 16, 0); // mapping type
612 put_bits(&pb
, 1, mc
->submaps
> 1);
614 put_bits(&pb
, 4, mc
->submaps
- 1);
616 put_bits(&pb
, 1, !!mc
->coupling_steps
);
617 if (mc
->coupling_steps
) {
618 put_bits(&pb
, 8, mc
->coupling_steps
- 1);
619 for (j
= 0; j
< mc
->coupling_steps
; j
++) {
620 put_bits(&pb
, ilog(venc
->channels
- 1), mc
->magnitude
[j
]);
621 put_bits(&pb
, ilog(venc
->channels
- 1), mc
->angle
[j
]);
625 put_bits(&pb
, 2, 0); // reserved
628 for (j
= 0; j
< venc
->channels
; j
++)
629 put_bits(&pb
, 4, mc
->mux
[j
]);
631 for (j
= 0; j
< mc
->submaps
; j
++) {
632 put_bits(&pb
, 8, 0); // reserved time configuration
633 put_bits(&pb
, 8, mc
->floor
[j
]);
634 put_bits(&pb
, 8, mc
->residue
[j
]);
639 put_bits(&pb
, 6, venc
->nmodes
- 1);
640 for (i
= 0; i
< venc
->nmodes
; i
++) {
641 put_bits(&pb
, 1, venc
->modes
[i
].blockflag
);
642 put_bits(&pb
, 16, 0); // reserved window type
643 put_bits(&pb
, 16, 0); // reserved transform type
644 put_bits(&pb
, 8, venc
->modes
[i
].mapping
);
647 put_bits(&pb
, 1, 1); // framing
650 hlens
[2] = (put_bits_count(&pb
) + 7) / 8;
652 len
= hlens
[0] + hlens
[1] + hlens
[2];
653 p
= *out
= av_mallocz(64 + len
+ len
/255);
656 p
+= av_xiphlacing(p
, hlens
[0]);
657 p
+= av_xiphlacing(p
, hlens
[1]);
659 for (i
= 0; i
< 3; i
++) {
660 memcpy(p
, buffer
+ buffer_len
, hlens
[i
]);
662 buffer_len
+= hlens
[i
];
668 static float get_floor_average(floor_t
* fc
, float * coeffs
, int i
) {
669 int begin
= fc
->list
[fc
->list
[FFMAX(i
-1, 0)].sort
].x
;
670 int end
= fc
->list
[fc
->list
[FFMIN(i
+1, fc
->values
- 1)].sort
].x
;
674 for (j
= begin
; j
< end
; j
++)
675 average
+= fabs(coeffs
[j
]);
676 return average
/ (end
- begin
);
679 static void floor_fit(venc_context_t
* venc
, floor_t
* fc
, float * coeffs
, uint_fast16_t * posts
, int samples
) {
680 int range
= 255 / fc
->multiplier
+ 1;
682 float tot_average
= 0.;
683 float averages
[fc
->values
];
684 for (i
= 0; i
< fc
->values
; i
++){
685 averages
[i
] = get_floor_average(fc
, coeffs
, i
);
686 tot_average
+= averages
[i
];
688 tot_average
/= fc
->values
;
689 tot_average
/= venc
->quality
;
691 for (i
= 0; i
< fc
->values
; i
++) {
692 int position
= fc
->list
[fc
->list
[i
].sort
].x
;
693 float average
= averages
[i
];
696 average
*= pow(tot_average
/ average
, 0.5) * pow(1.25, position
/200.); // MAGIC!
697 for (j
= 0; j
< range
- 1; j
++)
698 if (ff_vorbis_floor1_inverse_db_table
[j
* fc
->multiplier
] > average
) break;
699 posts
[fc
->list
[i
].sort
] = j
;
703 static int render_point(int x0
, int y0
, int x1
, int y1
, int x
) {
704 return y0
+ (x
- x0
) * (y1
- y0
) / (x1
- x0
);
707 static void floor_encode(venc_context_t
* venc
, floor_t
* fc
, PutBitContext
* pb
, uint_fast16_t * posts
, float * floor
, int samples
) {
708 int range
= 255 / fc
->multiplier
+ 1;
709 int coded
[fc
->values
]; // first 2 values are unused
712 put_bits(pb
, 1, 1); // non zero
713 put_bits(pb
, ilog(range
- 1), posts
[0]);
714 put_bits(pb
, ilog(range
- 1), posts
[1]);
715 coded
[0] = coded
[1] = 1;
717 for (i
= 2; i
< fc
->values
; i
++) {
718 int predicted
= render_point(fc
->list
[fc
->list
[i
].low
].x
,
719 posts
[fc
->list
[i
].low
],
720 fc
->list
[fc
->list
[i
].high
].x
,
721 posts
[fc
->list
[i
].high
],
723 int highroom
= range
- predicted
;
724 int lowroom
= predicted
;
725 int room
= FFMIN(highroom
, lowroom
);
726 if (predicted
== posts
[i
]) {
727 coded
[i
] = 0; // must be used later as flag!
730 if (!coded
[fc
->list
[i
].low
]) coded
[fc
->list
[i
].low
] = -1;
731 if (!coded
[fc
->list
[i
].high
]) coded
[fc
->list
[i
].high
] = -1;
733 if (posts
[i
] > predicted
) {
734 if (posts
[i
] - predicted
> room
)
735 coded
[i
] = posts
[i
] - predicted
+ lowroom
;
737 coded
[i
] = (posts
[i
] - predicted
) << 1;
739 if (predicted
- posts
[i
] > room
)
740 coded
[i
] = predicted
- posts
[i
] + highroom
- 1;
742 coded
[i
] = ((predicted
- posts
[i
]) << 1) - 1;
747 for (i
= 0; i
< fc
->partitions
; i
++) {
748 floor_class_t
* c
= &fc
->classes
[fc
->partition_to_class
[i
]];
749 int k
, cval
= 0, csub
= 1<<c
->subclass
;
751 codebook_t
* book
= &venc
->codebooks
[c
->masterbook
];
753 for (k
= 0; k
< c
->dim
; k
++) {
755 for (l
= 0; l
< csub
; l
++) {
757 if (c
->books
[l
] != -1)
758 maxval
= venc
->codebooks
[c
->books
[l
]].nentries
;
759 // coded could be -1, but this still works, cause that is 0
760 if (coded
[counter
+ k
] < maxval
) break;
764 cshift
+= c
->subclass
;
766 put_codeword(pb
, book
, cval
);
768 for (k
= 0; k
< c
->dim
; k
++) {
769 int book
= c
->books
[cval
& (csub
-1)];
770 int entry
= coded
[counter
++];
771 cval
>>= c
->subclass
;
772 if (book
== -1) continue;
773 if (entry
== -1) entry
= 0;
774 put_codeword(pb
, &venc
->codebooks
[book
], entry
);
778 ff_vorbis_floor1_render_list(fc
->list
, fc
->values
, posts
, coded
, fc
->multiplier
, floor
, samples
);
781 static float * put_vector(codebook_t
* book
, PutBitContext
* pb
, float * num
) {
783 float distance
= FLT_MAX
;
784 assert(book
->dimentions
);
785 for (i
= 0; i
< book
->nentries
; i
++) {
786 float * vec
= book
->dimentions
+ i
* book
->ndimentions
, d
= book
->pow2
[i
];
788 if (!book
->lens
[i
]) continue;
789 for (j
= 0; j
< book
->ndimentions
; j
++)
790 d
-= vec
[j
] * num
[j
];
796 put_codeword(pb
, book
, entry
);
797 return &book
->dimentions
[entry
* book
->ndimentions
];
800 static void residue_encode(venc_context_t
* venc
, residue_t
* rc
, PutBitContext
* pb
, float * coeffs
, int samples
, int real_ch
) {
801 int pass
, i
, j
, p
, k
;
802 int psize
= rc
->partition_size
;
803 int partitions
= (rc
->end
- rc
->begin
) / psize
;
804 int channels
= (rc
->type
== 2) ? 1 : real_ch
;
805 int classes
[channels
][partitions
];
806 int classwords
= venc
->codebooks
[rc
->classbook
].ndimentions
;
808 assert(rc
->type
== 2);
809 assert(real_ch
== 2);
810 for (p
= 0; p
< partitions
; p
++) {
811 float max1
= 0., max2
= 0.;
812 int s
= rc
->begin
+ p
* psize
;
813 for (k
= s
; k
< s
+ psize
; k
+= 2) {
814 max1
= FFMAX(max1
, fabs(coeffs
[ k
/ real_ch
]));
815 max2
= FFMAX(max2
, fabs(coeffs
[samples
+ k
/ real_ch
]));
818 for (i
= 0; i
< rc
->classifications
- 1; i
++) {
819 if (max1
< rc
->maxes
[i
][0] && max2
< rc
->maxes
[i
][1]) break;
824 for (pass
= 0; pass
< 8; pass
++) {
826 while (p
< partitions
) {
828 for (j
= 0; j
< channels
; j
++) {
829 codebook_t
* book
= &venc
->codebooks
[rc
->classbook
];
831 for (i
= 0; i
< classwords
; i
++) {
832 entry
*= rc
->classifications
;
833 entry
+= classes
[j
][p
+ i
];
835 put_codeword(pb
, book
, entry
);
837 for (i
= 0; i
< classwords
&& p
< partitions
; i
++, p
++) {
838 for (j
= 0; j
< channels
; j
++) {
839 int nbook
= rc
->books
[classes
[j
][p
]][pass
];
840 codebook_t
* book
= &venc
->codebooks
[nbook
];
841 float * buf
= coeffs
+ samples
*j
+ rc
->begin
+ p
*psize
;
842 if (nbook
== -1) continue;
844 assert(rc
->type
== 0 || rc
->type
== 2);
845 assert(!(psize
% book
->ndimentions
));
848 for (k
= 0; k
< psize
; k
+= book
->ndimentions
) {
849 float * a
= put_vector(book
, pb
, &buf
[k
]);
851 for (l
= 0; l
< book
->ndimentions
; l
++)
855 int s
= rc
->begin
+ p
* psize
, a1
, b1
;
856 a1
= (s
% real_ch
) * samples
;
858 s
= real_ch
* samples
;
859 for (k
= 0; k
< psize
; k
+= book
->ndimentions
) {
860 int dim
, a2
= a1
, b2
= b1
;
861 float vec
[book
->ndimentions
], * pv
= vec
;
862 for (dim
= book
->ndimentions
; dim
--; ) {
863 *pv
++ = coeffs
[a2
+ b2
];
864 if ((a2
+= samples
) == s
) {
869 pv
= put_vector(book
, pb
, vec
);
870 for (dim
= book
->ndimentions
; dim
--; ) {
871 coeffs
[a1
+ b1
] -= *pv
++;
872 if ((a1
+= samples
) == s
) {
885 static int apply_window_and_mdct(venc_context_t
* venc
, signed short * audio
, int samples
) {
887 const float * win
= venc
->win
[0];
888 int window_len
= 1 << (venc
->log2_blocksize
[0] - 1);
889 float n
= (float)(1 << venc
->log2_blocksize
[0]) / 4.;
892 if (!venc
->have_saved
&& !samples
) return 0;
894 if (venc
->have_saved
) {
895 for (channel
= 0; channel
< venc
->channels
; channel
++) {
896 memcpy(venc
->samples
+ channel
*window_len
*2, venc
->saved
+ channel
*window_len
, sizeof(float)*window_len
);
899 for (channel
= 0; channel
< venc
->channels
; channel
++) {
900 memset(venc
->samples
+ channel
*window_len
*2, 0, sizeof(float)*window_len
);
905 for (channel
= 0; channel
< venc
->channels
; channel
++) {
906 float * offset
= venc
->samples
+ channel
*window_len
*2 + window_len
;
908 for (i
= 0; i
< samples
; i
++, j
+= venc
->channels
)
909 offset
[i
] = -audio
[j
] / 32768. / n
* win
[window_len
- i
- 1]; //FIXME find out why the sign has to be fliped
912 for (channel
= 0; channel
< venc
->channels
; channel
++) {
913 memset(venc
->samples
+ channel
*window_len
*2 + window_len
, 0, sizeof(float)*window_len
);
917 for (channel
= 0; channel
< venc
->channels
; channel
++) {
918 ff_mdct_calc(&venc
->mdct
[0], venc
->coeffs
+ channel
*window_len
, venc
->samples
+ channel
*window_len
*2, venc
->floor
/*tmp*/);
922 for (channel
= 0; channel
< venc
->channels
; channel
++) {
923 float * offset
= venc
->saved
+ channel
*window_len
;
925 for (i
= 0; i
< samples
; i
++, j
+= venc
->channels
)
926 offset
[i
] = -audio
[j
] / 32768. / n
* win
[i
]; //FIXME find out why the sign has to be fliped
928 venc
->have_saved
= 1;
930 venc
->have_saved
= 0;
935 static av_cold
int vorbis_encode_init(AVCodecContext
* avccontext
)
937 venc_context_t
* venc
= avccontext
->priv_data
;
939 if (avccontext
->channels
!= 2) {
940 av_log(avccontext
, AV_LOG_ERROR
, "Current FFmpeg Vorbis encoder only supports 2 channels.\n");
944 create_vorbis_context(venc
, avccontext
);
946 if (avccontext
->flags
& CODEC_FLAG_QSCALE
)
947 venc
->quality
= avccontext
->global_quality
/ (float)FF_QP2LAMBDA
/ 10.;
950 venc
->quality
*= venc
->quality
;
952 avccontext
->extradata_size
= put_main_header(venc
, (uint8_t**)&avccontext
->extradata
);
954 avccontext
->frame_size
= 1 << (venc
->log2_blocksize
[0] - 1);
956 avccontext
->coded_frame
= avcodec_alloc_frame();
957 avccontext
->coded_frame
->key_frame
= 1;
962 static int vorbis_encode_frame(AVCodecContext
* avccontext
, unsigned char * packets
, int buf_size
, void *data
)
964 venc_context_t
* venc
= avccontext
->priv_data
;
965 signed short * audio
= data
;
966 int samples
= data
? avccontext
->frame_size
: 0;
967 vorbis_mode_t
* mode
;
972 if (!apply_window_and_mdct(venc
, audio
, samples
)) return 0;
973 samples
= 1 << (venc
->log2_blocksize
[0] - 1);
975 init_put_bits(&pb
, packets
, buf_size
);
977 put_bits(&pb
, 1, 0); // magic bit
979 put_bits(&pb
, ilog(venc
->nmodes
- 1), 0); // 0 bits, the mode
981 mode
= &venc
->modes
[0];
982 mapping
= &venc
->mappings
[mode
->mapping
];
983 if (mode
->blockflag
) {
988 for (i
= 0; i
< venc
->channels
; i
++) {
989 floor_t
* fc
= &venc
->floors
[mapping
->floor
[mapping
->mux
[i
]]];
990 uint_fast16_t posts
[fc
->values
];
991 floor_fit(venc
, fc
, &venc
->coeffs
[i
* samples
], posts
, samples
);
992 floor_encode(venc
, fc
, &pb
, posts
, &venc
->floor
[i
* samples
], samples
);
995 for (i
= 0; i
< venc
->channels
* samples
; i
++) {
996 venc
->coeffs
[i
] /= venc
->floor
[i
];
999 for (i
= 0; i
< mapping
->coupling_steps
; i
++) {
1000 float * mag
= venc
->coeffs
+ mapping
->magnitude
[i
] * samples
;
1001 float * ang
= venc
->coeffs
+ mapping
->angle
[i
] * samples
;
1003 for (j
= 0; j
< samples
; j
++) {
1006 if (mag
[j
] > 0) ang
[j
] = -ang
[j
];
1007 if (ang
[j
] < 0) mag
[j
] = a
;
1011 residue_encode(venc
, &venc
->residues
[mapping
->residue
[mapping
->mux
[0]]], &pb
, venc
->coeffs
, samples
, venc
->channels
);
1013 flush_put_bits(&pb
);
1014 return (put_bits_count(&pb
) + 7) / 8;
1018 static av_cold
int vorbis_encode_close(AVCodecContext
* avccontext
)
1020 venc_context_t
* venc
= avccontext
->priv_data
;
1023 if (venc
->codebooks
)
1024 for (i
= 0; i
< venc
->ncodebooks
; i
++) {
1025 av_freep(&venc
->codebooks
[i
].lens
);
1026 av_freep(&venc
->codebooks
[i
].codewords
);
1027 av_freep(&venc
->codebooks
[i
].quantlist
);
1028 av_freep(&venc
->codebooks
[i
].dimentions
);
1029 av_freep(&venc
->codebooks
[i
].pow2
);
1031 av_freep(&venc
->codebooks
);
1034 for (i
= 0; i
< venc
->nfloors
; i
++) {
1036 if (venc
->floors
[i
].classes
)
1037 for (j
= 0; j
< venc
->floors
[i
].nclasses
; j
++)
1038 av_freep(&venc
->floors
[i
].classes
[j
].books
);
1039 av_freep(&venc
->floors
[i
].classes
);
1040 av_freep(&venc
->floors
[i
].partition_to_class
);
1041 av_freep(&venc
->floors
[i
].list
);
1043 av_freep(&venc
->floors
);
1046 for (i
= 0; i
< venc
->nresidues
; i
++) {
1047 av_freep(&venc
->residues
[i
].books
);
1048 av_freep(&venc
->residues
[i
].maxes
);
1050 av_freep(&venc
->residues
);
1053 for (i
= 0; i
< venc
->nmappings
; i
++) {
1054 av_freep(&venc
->mappings
[i
].mux
);
1055 av_freep(&venc
->mappings
[i
].floor
);
1056 av_freep(&venc
->mappings
[i
].residue
);
1057 av_freep(&venc
->mappings
[i
].magnitude
);
1058 av_freep(&venc
->mappings
[i
].angle
);
1060 av_freep(&venc
->mappings
);
1062 av_freep(&venc
->modes
);
1064 av_freep(&venc
->saved
);
1065 av_freep(&venc
->samples
);
1066 av_freep(&venc
->floor
);
1067 av_freep(&venc
->coeffs
);
1069 ff_mdct_end(&venc
->mdct
[0]);
1070 ff_mdct_end(&venc
->mdct
[1]);
1072 av_freep(&avccontext
->coded_frame
);
1073 av_freep(&avccontext
->extradata
);
1078 AVCodec vorbis_encoder
= {
1082 sizeof(venc_context_t
),
1084 vorbis_encode_frame
,
1085 vorbis_encode_close
,
1086 .capabilities
= CODEC_CAP_DELAY
,
1087 .long_name
= NULL_IF_CONFIG_SMALL("Vorbis"),