FFmpeg
Loading...
Searching...
No Matches
cook.c
Go to the documentation of this file.
1/*
2 * COOK compatible decoder
3 * Copyright (c) 2003 Sascha Sommer
4 * Copyright (c) 2005 Benjamin Larsson
5 *
6 * This file is part of FFmpeg.
7 *
8 * FFmpeg is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * FFmpeg is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with FFmpeg; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22
23/**
24 * @file
25 * Cook compatible decoder. Bastardization of the G.722.1 standard.
26 * This decoder handles RealNetworks, RealAudio G2 data.
27 * Cook is identified by the codec name cook in RM files.
28 *
29 * To use this decoder, a calling application must supply the extradata
30 * bytes provided from the RM container; 8+ bytes for mono streams and
31 * 16+ for stereo streams (maybe more).
32 *
33 * Codec technicalities (all this assume a buffer length of 1024):
34 * Cook works with several different techniques to achieve its compression.
35 * In the timedomain the buffer is divided into 8 pieces and quantized. If
36 * two neighboring pieces have different quantization index a smooth
37 * quantization curve is used to get a smooth overlap between the different
38 * pieces.
39 * To get to the transformdomain Cook uses a modulated lapped transform.
40 * The transform domain has 50 subbands with 20 elements each. This
41 * means only a maximum of 50*20=1000 coefficients are used out of the 1024
42 * available.
43 */
44
46#include "libavutil/lfg.h"
47#include "libavutil/mem.h"
49#include "libavutil/thread.h"
50#include "libavutil/tx.h"
51
52#include "audiodsp.h"
53#include "avcodec.h"
54#include "get_bits.h"
55#include "bytestream.h"
56#include "codec_internal.h"
57#include "decode.h"
58#include "sinewin.h"
59#include "unary.h"
60
61#include "cookdata.h"
62
63/* the different Cook versions */
64#define MONO 0x1000001
65#define STEREO 0x1000002
66#define JOINT_STEREO 0x1000003
67#define MC_COOK 0x2000000
68
69#define SUBBAND_SIZE 20
70#define MAX_SUBPACKETS 5
71
72#define QUANT_VLC_BITS 9
73#define COUPLING_VLC_BITS 6
74
75typedef struct cook_gains {
76 int *now;
79
108
109typedef struct cook {
110 /*
111 * The following 5 functions provide the lowlevel arithmetic on
112 * the internal audio buffers.
113 */
114 void (*scalar_dequant)(struct cook *q, int index, int quant_index,
115 int *subband_coef_index, int *subband_coef_sign,
116 float *mlt_p);
117
118 void (*decouple)(struct cook *q,
119 COOKSubpacket *p,
120 int subband,
121 float f1, float f2,
122 float *decode_buffer,
123 float *mlt_buffer1, float *mlt_buffer2);
124
125 void (*imlt_window)(struct cook *q, float *buffer1,
126 cook_gains *gains_ptr, float *previous_buffer);
127
128 void (*interpolate)(struct cook *q, float *buffer,
129 int gain_index, int gain_index_next);
130
131 void (*saturate_output)(struct cook *q, float *out);
132
136 /* stream data */
139 /* states */
142
143 /* transform data */
147
148 /* VLC data */
150 VLC sqvh[7]; // scalar quantization
151
152 /* generate tables and related variables */
154 float gain_table[31];
155
156 /* data buffers */
157
160 float decode_buffer_1[1024];
161 float decode_buffer_2[1024];
162 float decode_buffer_0[1060]; /* static allocation for joint decode */
163
164 const float *cplscales[5];
168
169static float pow2tab[127];
170static float rootpow2tab[127];
171
172/*************** init functions ***************/
173
174/* table generator */
175static av_cold void init_pow2table(void)
176{
177 /* fast way of computing 2^i and 2^(0.5*i) for -63 <= i < 64 */
178 int i;
179 static const float exp2_tab[2] = {1, M_SQRT2};
180 float exp2_val = powf(2, -63);
181 float root_val = powf(2, -32);
182 for (i = -63; i < 64; i++) {
183 if (!(i & 1))
184 root_val *= 2;
185 pow2tab[63 + i] = exp2_val;
186 rootpow2tab[63 + i] = root_val * exp2_tab[i & 1];
187 exp2_val *= 2;
188 }
189}
190
191/* table generator */
193{
194 int i;
196 for (i = 0; i < 31; i++)
197 q->gain_table[i] = pow(pow2tab[i + 48],
198 (1.0 / (double) q->gain_size_factor));
199}
200
201static av_cold int build_vlc(VLC *vlc, int nb_bits, const uint8_t counts[16],
202 const void *syms, int symbol_size, int offset,
203 void *logctx)
204{
205 uint8_t lens[MAX_COOK_VLC_ENTRIES];
206 unsigned num = 0;
207
208 for (int i = 0; i < 16; i++)
209 for (unsigned count = num + counts[i]; num < count; num++)
210 lens[num] = i + 1;
211
212 return ff_vlc_init_from_lengths(vlc, nb_bits, num, lens, 1,
213 syms, symbol_size, symbol_size,
214 offset, 0, logctx);
215}
216
218{
219 int i, result;
220
221 result = 0;
222 for (i = 0; i < 13; i++) {
226 }
227 av_log(q->avctx, AV_LOG_DEBUG, "sqvh VLC init\n");
228 for (i = 0; i < 7; i++) {
229 int sym_size = 1 + (i == 3);
230 result |= build_vlc(&q->sqvh[i], vhvlcsize_tab[i],
232 cvh_huffsyms[i], sym_size, 0, q->avctx);
233 }
234
235 for (i = 0; i < q->num_subpackets; i++) {
236 if (q->subpacket[i].joint_stereo == 1) {
240 0, q->avctx);
241 av_log(q->avctx, AV_LOG_DEBUG, "subpacket %i Joint-stereo VLC used.\n", i);
242 }
243 }
244
245 av_log(q->avctx, AV_LOG_DEBUG, "VLC tables initialized.\n");
246 return result;
247}
248
250{
251 int j, ret;
252 int mlt_size = q->samples_per_channel;
253 const float scale = 1.0 / 32768.0;
254
255 if (!(q->mlt_window = av_malloc_array(mlt_size, sizeof(*q->mlt_window))))
256 return AVERROR(ENOMEM);
257
258 /* Initialize the MLT window: simple sine window. */
259 ff_sine_window_init(q->mlt_window, mlt_size);
260 for (j = 0; j < mlt_size; j++)
261 q->mlt_window[j] *= sqrt(2.0 / q->samples_per_channel);
262
263 /* Initialize the MDCT. */
265 1, mlt_size, &scale, AV_TX_FULL_IMDCT);
266 if (ret < 0)
267 return ret;
268
269 return 0;
270}
271
273{
274 int i;
275 for (i = 0; i < 5; i++)
276 q->cplscales[i] = cplscales[i];
277}
278
279/*************** init functions end ***********/
280
281#define DECODE_BYTES_PAD1(bytes) (3 - ((bytes) + 3) % 4)
282#define DECODE_BYTES_PAD2(bytes) ((bytes) % 4 + DECODE_BYTES_PAD1(2 * (bytes)))
283
284/**
285 * Cook indata decoding, every 32 bits are XORed with 0x37c511f2.
286 * Why? No idea, some checksum/error detection method maybe.
287 *
288 * Out buffer size: extra bytes are needed to cope with
289 * padding/misalignment.
290 * Subpackets passed to the decoder can contain two, consecutive
291 * half-subpackets, of identical but arbitrary size.
292 * 1234 1234 1234 1234 extraA extraB
293 * Case 1: AAAA BBBB 0 0
294 * Case 2: AAAA ABBB BB-- 3 3
295 * Case 3: AAAA AABB BBBB 2 2
296 * Case 4: AAAA AAAB BBBB BB-- 1 5
297 *
298 * Nice way to waste CPU cycles.
299 *
300 * @param inbuffer pointer to byte array of indata
301 * @param out pointer to byte array of outdata
302 * @param bytes number of bytes
303 */
304static inline int decode_bytes(const uint8_t *inbuffer, uint8_t *out, int bytes)
305{
306 static const uint32_t tab[4] = {
307 AV_BE2NE32C(0x37c511f2u), AV_BE2NE32C(0xf237c511u),
308 AV_BE2NE32C(0x11f237c5u), AV_BE2NE32C(0xc511f237u),
309 };
310 int i, off;
311 uint32_t c;
312 const uint32_t *buf;
313 uint32_t *obuf = (uint32_t *) out;
314 /* FIXME: 64 bit platforms would be able to do 64 bits at a time.
315 * I'm too lazy though, should be something like
316 * for (i = 0; i < bitamount / 64; i++)
317 * (int64_t) out[i] = 0x37c511f237c511f2 ^ av_be2ne64(int64_t) in[i]);
318 * Buffer alignment needs to be checked. */
319
320 off = (intptr_t) inbuffer & 3;
321 buf = (const uint32_t *) (inbuffer - off);
322 c = tab[off];
323 bytes += 3 + off;
324 for (i = 0; i < bytes / 4; i++)
325 obuf[i] = c ^ buf[i];
326
327 return off;
328}
329
331{
332 int i;
333 COOKContext *q = avctx->priv_data;
334 av_log(avctx, AV_LOG_DEBUG, "Deallocating memory.\n");
335
336 /* Free allocated memory buffers. */
337 av_freep(&q->mlt_window);
339
340 /* Free the transform. */
342
343 /* Free the VLC tables. */
344 for (i = 0; i < 13; i++)
346 for (i = 0; i < 7; i++)
347 ff_vlc_free(&q->sqvh[i]);
348 for (i = 0; i < q->num_subpackets; i++)
350
351 av_log(avctx, AV_LOG_DEBUG, "Memory deallocated.\n");
352
353 return 0;
354}
355
356/**
357 * Fill the gain array for the timedomain quantization.
358 *
359 * @param gb pointer to the GetBitContext
360 * @param gaininfo array[9] of gain indexes
361 */
362static void decode_gain_info(GetBitContext *gb, int *gaininfo)
363{
364 int i, n;
365
366 n = get_unary(gb, 0, get_bits_left(gb)); // amount of elements*2 to update
367
368 i = 0;
369 while (n--) {
370 int index = get_bits(gb, 3);
371 int gain = get_bits1(gb) ? get_bits(gb, 4) - 7 : -1;
372
373 while (i <= index)
374 gaininfo[i++] = gain;
375 }
376 while (i <= 8)
377 gaininfo[i++] = 0;
378}
379
380/**
381 * Create the quant index table needed for the envelope.
382 *
383 * @param q pointer to the COOKContext
384 * @param quant_index_table pointer to the array
385 */
387 int *quant_index_table)
388{
389 int i, j, vlc_index;
390
391 quant_index_table[0] = get_bits(&q->gb, 6) - 6; // This is used later in categorize
392
393 for (i = 1; i < p->total_subbands; i++) {
394 vlc_index = i;
395 if (i >= p->js_subband_start * 2) {
396 vlc_index -= p->js_subband_start;
397 } else {
398 vlc_index /= 2;
399 if (vlc_index < 1)
400 vlc_index = 1;
401 }
402 if (vlc_index > 13)
403 vlc_index = 13; // the VLC tables >13 are identical to No. 13
404
405 j = get_vlc2(&q->gb, q->envelope_quant_index[vlc_index - 1].table,
406 QUANT_VLC_BITS, 2);
407 quant_index_table[i] = quant_index_table[i - 1] + j; // differential encoding
408 if (quant_index_table[i] > 63 || quant_index_table[i] < -63) {
410 "Invalid quantizer %d at position %d, outside [-63, 63] range\n",
411 quant_index_table[i], i);
412 return AVERROR_INVALIDDATA;
413 }
414 }
415
416 return 0;
417}
418
419/**
420 * Calculate the category and category_index vector.
421 *
422 * @param q pointer to the COOKContext
423 * @param quant_index_table pointer to the array
424 * @param category pointer to the category array
425 * @param category_index pointer to the category_index array
426 */
427static void categorize(COOKContext *q, COOKSubpacket *p, const int *quant_index_table,
428 int *category, int *category_index)
429{
430 int exp_idx, bias, tmpbias1, tmpbias2, bits_left, num_bits, index, v, i, j;
431 int exp_index2[102] = { 0 };
432 int exp_index1[102] = { 0 };
433
434 int tmp_categorize_array[128 * 2] = { 0 };
435 int tmp_categorize_array1_idx = p->numvector_size;
436 int tmp_categorize_array2_idx = p->numvector_size;
437
438 bits_left = p->bits_per_subpacket - get_bits_count(&q->gb);
439
442 ((bits_left - q->samples_per_channel) * 5) / 8;
443
444 bias = -32;
445
446 /* Estimate bias. */
447 for (i = 32; i > 0; i = i / 2) {
448 num_bits = 0;
449 index = 0;
450 for (j = p->total_subbands; j > 0; j--) {
451 exp_idx = av_clip_uintp2((i - quant_index_table[index] + bias) / 2, 3);
452 index++;
453 num_bits += expbits_tab[exp_idx];
454 }
455 if (num_bits >= bits_left - 32)
456 bias += i;
457 }
458
459 /* Calculate total number of bits. */
460 num_bits = 0;
461 for (i = 0; i < p->total_subbands; i++) {
462 exp_idx = av_clip_uintp2((bias - quant_index_table[i]) / 2, 3);
463 num_bits += expbits_tab[exp_idx];
464 exp_index1[i] = exp_idx;
465 exp_index2[i] = exp_idx;
466 }
467 tmpbias1 = tmpbias2 = num_bits;
468
469 for (j = 1; j < p->numvector_size; j++) {
470 if (tmpbias1 + tmpbias2 > 2 * bits_left) { /* ---> */
471 int max = -999999;
472 index = -1;
473 for (i = 0; i < p->total_subbands; i++) {
474 if (exp_index1[i] < 7) {
475 v = (-2 * exp_index1[i]) - quant_index_table[i] + bias;
476 if (v >= max) {
477 max = v;
478 index = i;
479 }
480 }
481 }
482 if (index == -1)
483 break;
484 tmp_categorize_array[tmp_categorize_array1_idx++] = index;
485 tmpbias1 -= expbits_tab[exp_index1[index]] -
486 expbits_tab[exp_index1[index] + 1];
487 ++exp_index1[index];
488 } else { /* <--- */
489 int min = 999999;
490 index = -1;
491 for (i = 0; i < p->total_subbands; i++) {
492 if (exp_index2[i] > 0) {
493 v = (-2 * exp_index2[i]) - quant_index_table[i] + bias;
494 if (v < min) {
495 min = v;
496 index = i;
497 }
498 }
499 }
500 if (index == -1)
501 break;
502 tmp_categorize_array[--tmp_categorize_array2_idx] = index;
503 tmpbias2 -= expbits_tab[exp_index2[index]] -
504 expbits_tab[exp_index2[index] - 1];
505 --exp_index2[index];
506 }
507 }
508
509 for (i = 0; i < p->total_subbands; i++)
510 category[i] = exp_index2[i];
511
512 for (i = 0; i < p->numvector_size - 1; i++)
513 category_index[i] = tmp_categorize_array[tmp_categorize_array2_idx++];
514}
515
516
517/**
518 * Expand the category vector.
519 *
520 * @param q pointer to the COOKContext
521 * @param category pointer to the category array
522 * @param category_index pointer to the category_index array
523 */
524static inline void expand_category(COOKContext *q, int *category,
525 int *category_index)
526{
527 int i;
528 for (i = 0; i < q->num_vectors; i++)
529 {
530 int idx = category_index[i];
531 if (++category[idx] >= FF_ARRAY_ELEMS(dither_tab))
532 --category[idx];
533 }
534}
535
536/**
537 * The real requantization of the mltcoefs
538 *
539 * @param q pointer to the COOKContext
540 * @param index index
541 * @param quant_index quantisation index
542 * @param subband_coef_index array of indexes to quant_centroid_tab
543 * @param subband_coef_sign signs of coefficients
544 * @param mlt_p pointer into the mlt buffer
545 */
546static void scalar_dequant_float(COOKContext *q, int index, int quant_index,
547 int *subband_coef_index, int *subband_coef_sign,
548 float *mlt_p)
549{
550 int i;
551 float f1;
552
553 for (i = 0; i < SUBBAND_SIZE; i++) {
554 if (subband_coef_index[i]) {
555 f1 = quant_centroid_tab[index][subband_coef_index[i]];
556 if (subband_coef_sign[i])
557 f1 = -f1;
558 } else {
559 /* noise coding if subband_coef_index[i] == 0 */
560 f1 = dither_tab[index];
561 if (av_lfg_get(&q->random_state) < 0x80000000)
562 f1 = -f1;
563 }
564 mlt_p[i] = f1 * rootpow2tab[quant_index + 63];
565 }
566}
567/**
568 * Unpack the subband_coef_index and subband_coef_sign vectors.
569 *
570 * @param q pointer to the COOKContext
571 * @param category pointer to the category array
572 * @param subband_coef_index array of indexes to quant_centroid_tab
573 * @param subband_coef_sign signs of coefficients
574 */
576 int *subband_coef_index, int *subband_coef_sign)
577{
578 int i, j;
579 int vlc, vd, tmp, result;
580
581 vd = vd_tab[category];
582 result = 0;
583 for (i = 0; i < vpr_tab[category]; i++) {
584 vlc = get_vlc2(&q->gb, q->sqvh[category].table, q->sqvh[category].bits, 3);
585 if (p->bits_per_subpacket < get_bits_count(&q->gb)) {
586 vlc = 0;
587 result = 1;
588 }
589 for (j = vd - 1; j >= 0; j--) {
590 tmp = (vlc * invradix_tab[category]) / 0x100000;
591 subband_coef_index[vd * i + j] = vlc - tmp * (kmax_tab[category] + 1);
592 vlc = tmp;
593 }
594 for (j = 0; j < vd; j++) {
595 if (subband_coef_index[i * vd + j]) {
596 if (get_bits_count(&q->gb) < p->bits_per_subpacket) {
597 subband_coef_sign[i * vd + j] = get_bits1(&q->gb);
598 } else {
599 result = 1;
600 subband_coef_sign[i * vd + j] = 0;
601 }
602 } else {
603 subband_coef_sign[i * vd + j] = 0;
604 }
605 }
606 }
607 return result;
608}
609
610
611/**
612 * Fill the mlt_buffer with mlt coefficients.
613 *
614 * @param q pointer to the COOKContext
615 * @param category pointer to the category array
616 * @param quant_index_table pointer to the array
617 * @param mlt_buffer pointer to mlt coefficients
618 */
620 int *quant_index_table, float *mlt_buffer)
621{
622 /* A zero in this table means that the subband coefficient is
623 random noise coded. */
624 int subband_coef_index[SUBBAND_SIZE];
625 /* A zero in this table means that the subband coefficient is a
626 positive multiplicator. */
627 int subband_coef_sign[SUBBAND_SIZE];
628 int band, j;
629 int index = 0;
630
631 for (band = 0; band < p->total_subbands; band++) {
632 index = category[band];
633 if (category[band] < 7) {
634 if (unpack_SQVH(q, p, category[band], subband_coef_index, subband_coef_sign)) {
635 index = 7;
636 for (j = 0; j < p->total_subbands; j++)
637 category[band + j] = 7;
638 }
639 }
640 if (index >= 7) {
641 memset(subband_coef_index, 0, sizeof(subband_coef_index));
642 memset(subband_coef_sign, 0, sizeof(subband_coef_sign));
643 }
644 q->scalar_dequant(q, index, quant_index_table[band],
645 subband_coef_index, subband_coef_sign,
646 &mlt_buffer[band * SUBBAND_SIZE]);
647 }
648
649 /* FIXME: should this be removed, or moved into loop above? */
650 if (p->total_subbands * SUBBAND_SIZE >= q->samples_per_channel)
651 return;
652}
653
654
655static int mono_decode(COOKContext *q, COOKSubpacket *p, float *mlt_buffer)
656{
657 int category_index[128] = { 0 };
658 int category[128] = { 0 };
659 int quant_index_table[102];
660 int res, i;
661
662 if ((res = decode_envelope(q, p, quant_index_table)) < 0)
663 return res;
664 q->num_vectors = get_bits(&q->gb, p->log2_numvector_size);
665 categorize(q, p, quant_index_table, category, category_index);
666 expand_category(q, category, category_index);
667 for (i=0; i<p->total_subbands; i++) {
668 if (category[i] > 7)
669 return AVERROR_INVALIDDATA;
670 }
671 decode_vectors(q, p, category, quant_index_table, mlt_buffer);
672
673 return 0;
674}
675
676
677/**
678 * the actual requantization of the timedomain samples
679 *
680 * @param q pointer to the COOKContext
681 * @param buffer pointer to the timedomain buffer
682 * @param gain_index index for the block multiplier
683 * @param gain_index_next index for the next block multiplier
684 */
685static void interpolate_float(COOKContext *q, float *buffer,
686 int gain_index, int gain_index_next)
687{
688 int i;
689 float fc1, fc2;
690 fc1 = pow2tab[gain_index + 63];
691
692 if (gain_index == gain_index_next) { // static gain
693 for (i = 0; i < q->gain_size_factor; i++)
694 buffer[i] *= fc1;
695 } else { // smooth gain
696 fc2 = q->gain_table[15 + (gain_index_next - gain_index)];
697 for (i = 0; i < q->gain_size_factor; i++) {
698 buffer[i] *= fc1;
699 fc1 *= fc2;
700 }
701 }
702}
703
704/**
705 * Apply transform window, overlap buffers.
706 *
707 * @param q pointer to the COOKContext
708 * @param inbuffer pointer to the mltcoefficients
709 * @param gains_ptr current and previous gains
710 * @param previous_buffer pointer to the previous buffer to be used for overlapping
711 */
712static void imlt_window_float(COOKContext *q, float *inbuffer,
713 cook_gains *gains_ptr, float *previous_buffer)
714{
715 const float fc = pow2tab[gains_ptr->previous[0] + 63];
716 int i;
717 /* The weird thing here, is that the two halves of the time domain
718 * buffer are swapped. Also, the newest data, that we save away for
719 * next frame, has the wrong sign. Hence the subtraction below.
720 * Almost sounds like a complex conjugate/reverse data/FFT effect.
721 */
722
723 /* Apply window and overlap */
724 for (i = 0; i < q->samples_per_channel; i++)
725 inbuffer[i] = inbuffer[i] * fc * q->mlt_window[i] -
726 previous_buffer[i] * q->mlt_window[q->samples_per_channel - 1 - i];
727}
728
729/**
730 * The modulated lapped transform, this takes transform coefficients
731 * and transforms them into timedomain samples.
732 * Apply transform window, overlap buffers, apply gain profile
733 * and buffer management.
734 *
735 * @param q pointer to the COOKContext
736 * @param inbuffer pointer to the mltcoefficients
737 * @param gains_ptr current and previous gains
738 * @param previous_buffer pointer to the previous buffer to be used for overlapping
739 */
740static void imlt_gain(COOKContext *q, float *inbuffer,
741 cook_gains *gains_ptr, float *previous_buffer)
742{
743 float *buffer0 = q->mono_mdct_output;
744 float *buffer1 = q->mono_mdct_output + q->samples_per_channel;
745 int i;
746
747 /* Inverse modified discrete cosine transform */
748 q->mdct_fn(q->mdct_ctx, q->mono_mdct_output, inbuffer, sizeof(float));
749
750 q->imlt_window(q, buffer1, gains_ptr, previous_buffer);
751
752 /* Apply gain profile */
753 for (i = 0; i < 8; i++)
754 if (gains_ptr->now[i] || gains_ptr->now[i + 1])
755 q->interpolate(q, &buffer1[q->gain_size_factor * i],
756 gains_ptr->now[i], gains_ptr->now[i + 1]);
757
758 /* Save away the current to be previous block. */
759 memcpy(previous_buffer, buffer0,
760 q->samples_per_channel * sizeof(*previous_buffer));
761}
762
763
764/**
765 * function for getting the jointstereo coupling information
766 *
767 * @param q pointer to the COOKContext
768 * @param decouple_tab decoupling array
769 */
770static int decouple_info(COOKContext *q, COOKSubpacket *p, int *decouple_tab)
771{
772 int i;
773 int vlc = get_bits1(&q->gb);
774 int start = cplband[p->js_subband_start];
775 int end = cplband[p->subbands - 1];
776 int length = end - start + 1;
777
778 if (start > end)
779 return 0;
780
781 if (vlc)
782 for (i = 0; i < length; i++)
783 decouple_tab[start + i] = get_vlc2(&q->gb,
784 p->channel_coupling.table,
786 else
787 for (i = 0; i < length; i++) {
788 int v = get_bits(&q->gb, p->js_vlc_bits);
789 if (v == (1<<p->js_vlc_bits)-1) {
790 av_log(q->avctx, AV_LOG_ERROR, "decouple value too large\n");
791 return AVERROR_INVALIDDATA;
792 }
793 decouple_tab[start + i] = v;
794 }
795 return 0;
796}
797
798/**
799 * function decouples a pair of signals from a single signal via multiplication.
800 *
801 * @param q pointer to the COOKContext
802 * @param subband index of the current subband
803 * @param f1 multiplier for channel 1 extraction
804 * @param f2 multiplier for channel 2 extraction
805 * @param decode_buffer input buffer
806 * @param mlt_buffer1 pointer to left channel mlt coefficients
807 * @param mlt_buffer2 pointer to right channel mlt coefficients
808 */
810 COOKSubpacket *p,
811 int subband,
812 float f1, float f2,
813 float *decode_buffer,
814 float *mlt_buffer1, float *mlt_buffer2)
815{
816 int j, tmp_idx;
817 for (j = 0; j < SUBBAND_SIZE; j++) {
818 tmp_idx = ((p->js_subband_start + subband) * SUBBAND_SIZE) + j;
819 mlt_buffer1[SUBBAND_SIZE * subband + j] = f1 * decode_buffer[tmp_idx];
820 mlt_buffer2[SUBBAND_SIZE * subband + j] = f2 * decode_buffer[tmp_idx];
821 }
822}
823
824/**
825 * function for decoding joint stereo data
826 *
827 * @param q pointer to the COOKContext
828 * @param mlt_buffer1 pointer to left channel mlt coefficients
829 * @param mlt_buffer2 pointer to right channel mlt coefficients
830 */
832 float *mlt_buffer_left, float *mlt_buffer_right)
833{
834 int i, j, res;
835 int decouple_tab[SUBBAND_SIZE] = { 0 };
836 float *decode_buffer = q->decode_buffer_0;
837 int idx, cpl_tmp;
838 float f1, f2;
839 const float *cplscale;
840
841 memset(decode_buffer, 0, sizeof(q->decode_buffer_0));
842
843 /* Make sure the buffers are zeroed out. */
844 memset(mlt_buffer_left, 0, 1024 * sizeof(*mlt_buffer_left));
845 memset(mlt_buffer_right, 0, 1024 * sizeof(*mlt_buffer_right));
846 if ((res = decouple_info(q, p, decouple_tab)) < 0)
847 return res;
848 if ((res = mono_decode(q, p, decode_buffer)) < 0)
849 return res;
850 /* The two channels are stored interleaved in decode_buffer. */
851 for (i = 0; i < p->js_subband_start; i++) {
852 for (j = 0; j < SUBBAND_SIZE; j++) {
853 mlt_buffer_left[i * 20 + j] = decode_buffer[i * 40 + j];
854 mlt_buffer_right[i * 20 + j] = decode_buffer[i * 40 + 20 + j];
855 }
856 }
857
858 /* When we reach js_subband_start (the higher frequencies)
859 the coefficients are stored in a coupling scheme. */
860 idx = (1 << p->js_vlc_bits) - 1;
861 for (i = p->js_subband_start; i < p->subbands; i++) {
862 cpl_tmp = cplband[i];
863 idx -= decouple_tab[cpl_tmp];
864 cplscale = q->cplscales[p->js_vlc_bits - 2]; // choose decoupler table
865 f1 = cplscale[decouple_tab[cpl_tmp] + 1];
866 f2 = cplscale[idx];
867 q->decouple(q, p, i, f1, f2, decode_buffer,
868 mlt_buffer_left, mlt_buffer_right);
869 idx = (1 << p->js_vlc_bits) - 1;
870 }
871
872 return 0;
873}
874
875/**
876 * First part of subpacket decoding:
877 * decode raw stream bytes and read gain info.
878 *
879 * @param q pointer to the COOKContext
880 * @param inbuffer pointer to raw stream data
881 * @param gains_ptr array of current/prev gain pointers
882 */
884 const uint8_t *inbuffer,
885 cook_gains *gains_ptr)
886{
887 int offset;
888
890 p->bits_per_subpacket / 8);
892 p->bits_per_subpacket);
893 decode_gain_info(&q->gb, gains_ptr->now);
894
895 /* Swap current and previous gains */
896 FFSWAP(int *, gains_ptr->now, gains_ptr->previous);
897}
898
899/**
900 * Saturate the output signal and interleave.
901 *
902 * @param q pointer to the COOKContext
903 * @param out pointer to the output vector
904 */
905static void saturate_output_float(COOKContext *q, float *out)
906{
908 FFALIGN(q->samples_per_channel, 8), -1.0f, 1.0f);
909}
910
911
912/**
913 * Final part of subpacket decoding:
914 * Apply modulated lapped transform, gain compensation,
915 * clip and convert to integer.
916 *
917 * @param q pointer to the COOKContext
918 * @param decode_buffer pointer to the mlt coefficients
919 * @param gains_ptr array of current/prev gain pointers
920 * @param previous_buffer pointer to the previous buffer to be used for overlapping
921 * @param out pointer to the output buffer
922 */
923static inline void mlt_compensate_output(COOKContext *q, float *decode_buffer,
924 cook_gains *gains_ptr, float *previous_buffer,
925 float *out)
926{
927 imlt_gain(q, decode_buffer, gains_ptr, previous_buffer);
928 if (out)
929 q->saturate_output(q, out);
930}
931
932
933/**
934 * Cook subpacket decoding. This function returns one decoded subpacket,
935 * usually 1024 samples per channel.
936 *
937 * @param q pointer to the COOKContext
938 * @param inbuffer pointer to the inbuffer
939 * @param outbuffer pointer to the outbuffer
940 */
942 const uint8_t *inbuffer, float **outbuffer)
943{
944 int sub_packet_size = p->size;
945 int res;
946
947 memset(q->decode_buffer_1, 0, sizeof(q->decode_buffer_1));
948 decode_bytes_and_gain(q, p, inbuffer, &p->gains1);
949
950 if (p->joint_stereo) {
951 if ((res = joint_decode(q, p, q->decode_buffer_1, q->decode_buffer_2)) < 0)
952 return res;
953 } else {
954 if ((res = mono_decode(q, p, q->decode_buffer_1)) < 0)
955 return res;
956
957 if (p->num_channels == 2) {
958 decode_bytes_and_gain(q, p, inbuffer + sub_packet_size / 2, &p->gains2);
959 if ((res = mono_decode(q, p, q->decode_buffer_2)) < 0)
960 return res;
961 }
962 }
963
964 mlt_compensate_output(q, q->decode_buffer_1, &p->gains1,
965 p->mono_previous_buffer1,
966 outbuffer ? outbuffer[p->ch_idx] : NULL);
967
968 if (p->num_channels == 2) {
969 if (p->joint_stereo)
970 mlt_compensate_output(q, q->decode_buffer_2, &p->gains1,
971 p->mono_previous_buffer2,
972 outbuffer ? outbuffer[p->ch_idx + 1] : NULL);
973 else
974 mlt_compensate_output(q, q->decode_buffer_2, &p->gains2,
975 p->mono_previous_buffer2,
976 outbuffer ? outbuffer[p->ch_idx + 1] : NULL);
977 }
978
979 return 0;
980}
981
982
984 int *got_frame_ptr, AVPacket *avpkt)
985{
986 const uint8_t *buf = avpkt->data;
987 int buf_size = avpkt->size;
988 COOKContext *q = avctx->priv_data;
989 float **samples = NULL;
990 int i, ret;
991 int offset = 0;
992 int chidx = 0;
993
994 if (buf_size < avctx->block_align)
995 return buf_size;
996
997 /* get output buffer */
998 if (q->discarded_packets >= 2) {
999 frame->nb_samples = q->samples_per_channel;
1000 if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
1001 return ret;
1002 samples = (float **)frame->extended_data;
1003 }
1004
1005 /* estimate subpacket sizes */
1006 q->subpacket[0].size = avctx->block_align;
1007
1008 for (i = 1; i < q->num_subpackets; i++) {
1009 q->subpacket[i].size = 2 * buf[avctx->block_align - q->num_subpackets + i];
1010 q->subpacket[0].size -= q->subpacket[i].size + 1;
1011 if (q->subpacket[0].size < 0) {
1012 av_log(avctx, AV_LOG_DEBUG,
1013 "frame subpacket size total > avctx->block_align!\n");
1014 return AVERROR_INVALIDDATA;
1015 }
1016 }
1017
1018 /* decode supbackets */
1019 for (i = 0; i < q->num_subpackets; i++) {
1020 q->subpacket[i].bits_per_subpacket = (q->subpacket[i].size * 8) >>
1022 q->subpacket[i].ch_idx = chidx;
1023 av_log(avctx, AV_LOG_DEBUG,
1024 "subpacket[%i] size %i js %i %i block_align %i\n",
1026 avctx->block_align);
1027
1028 if ((ret = decode_subpacket(q, &q->subpacket[i], buf + offset, samples)) < 0)
1029 return ret;
1030 offset += q->subpacket[i].size;
1031 chidx += q->subpacket[i].num_channels;
1032 av_log(avctx, AV_LOG_DEBUG, "subpacket[%i] %i %i\n",
1033 i, q->subpacket[i].size * 8, get_bits_count(&q->gb));
1034 }
1035
1036 /* Discard the first two frames: no valid audio. */
1037 if (q->discarded_packets < 2) {
1038 q->discarded_packets++;
1039 *got_frame_ptr = 0;
1040 return avctx->block_align;
1041 }
1042
1043 *got_frame_ptr = 1;
1044
1045 return avctx->block_align;
1046}
1047
1049{
1050 //int i=0;
1051#define PRINT(a, b) ff_dlog(q->avctx, " %s = %d\n", a, b);
1052 ff_dlog(q->avctx, "COOKextradata\n");
1053 ff_dlog(q->avctx, "cookversion=%x\n", q->subpacket[0].cookversion);
1054 if (q->subpacket[0].cookversion > STEREO) {
1055 PRINT("js_subband_start", q->subpacket[0].js_subband_start);
1056 PRINT("js_vlc_bits", q->subpacket[0].js_vlc_bits);
1057 }
1058 ff_dlog(q->avctx, "COOKContext\n");
1059 PRINT("nb_channels", q->avctx->ch_layout.nb_channels);
1060 PRINT("bit_rate", (int)q->avctx->bit_rate);
1061 PRINT("sample_rate", q->avctx->sample_rate);
1062 PRINT("samples_per_channel", q->subpacket[0].samples_per_channel);
1063 PRINT("subbands", q->subpacket[0].subbands);
1064 PRINT("js_subband_start", q->subpacket[0].js_subband_start);
1065 PRINT("log2_numvector_size", q->subpacket[0].log2_numvector_size);
1066 PRINT("numvector_size", q->subpacket[0].numvector_size);
1067 PRINT("total_subbands", q->subpacket[0].total_subbands);
1068}
1069
1070/**
1071 * Cook initialization
1072 *
1073 * @param avctx pointer to the AVCodecContext
1074 */
1076{
1077 static AVOnce init_static_once = AV_ONCE_INIT;
1078 COOKContext *q = avctx->priv_data;
1079 GetByteContext gb;
1080 int s = 0;
1081 unsigned int channel_mask = 0;
1082 int samples_per_frame = 0;
1083 int total_channels = 0;
1084 int ret;
1085 int channels = avctx->ch_layout.nb_channels;
1086
1087 q->avctx = avctx;
1088
1089 /* Take care of the codec specific extradata. */
1090 if (avctx->extradata_size < 8) {
1091 av_log(avctx, AV_LOG_ERROR, "Necessary extradata missing!\n");
1092 return AVERROR_INVALIDDATA;
1093 }
1094 av_log(avctx, AV_LOG_DEBUG, "codecdata_length=%d\n", avctx->extradata_size);
1095
1096 bytestream2_init(&gb, avctx->extradata, avctx->extradata_size);
1097
1098 /* Take data from the AVCodecContext (RM container). */
1099 if (!channels) {
1100 av_log(avctx, AV_LOG_ERROR, "Invalid number of channels\n");
1101 return AVERROR_INVALIDDATA;
1102 }
1103
1104 if (avctx->block_align >= INT_MAX / 8)
1105 return AVERROR(EINVAL);
1106
1107 /* Initialize RNG. */
1108 av_lfg_init(&q->random_state, 0);
1109
1111
1112 while (bytestream2_get_bytes_left(&gb)) {
1113 if (s >= FFMIN(MAX_SUBPACKETS, avctx->block_align)) {
1114 avpriv_request_sample(avctx, "subpackets > %d", FFMIN(MAX_SUBPACKETS, avctx->block_align));
1115 return AVERROR_PATCHWELCOME;
1116 }
1117 /* 8 for mono, 16 for stereo, ? for multichannel
1118 Swap to right endianness so we don't need to care later on. */
1119 q->subpacket[s].cookversion = bytestream2_get_be32(&gb);
1120 samples_per_frame = bytestream2_get_be16(&gb);
1121 q->subpacket[s].subbands = bytestream2_get_be16(&gb);
1122 bytestream2_get_be32(&gb); // Unknown unused
1123 q->subpacket[s].js_subband_start = bytestream2_get_be16(&gb);
1124 if (q->subpacket[s].js_subband_start >= 51) {
1125 av_log(avctx, AV_LOG_ERROR, "js_subband_start %d is too large\n", q->subpacket[s].js_subband_start);
1126 return AVERROR_INVALIDDATA;
1127 }
1128 q->subpacket[s].js_vlc_bits = bytestream2_get_be16(&gb);
1129
1130 /* Initialize extradata related variables. */
1131 q->subpacket[s].samples_per_channel = samples_per_frame / channels;
1132 q->subpacket[s].bits_per_subpacket = avctx->block_align * 8;
1133
1134 /* Initialize default data states. */
1137 q->subpacket[s].num_channels = 1;
1138
1139 /* Initialize version-dependent variables */
1140
1141 av_log(avctx, AV_LOG_DEBUG, "subpacket[%i].cookversion=%x\n", s,
1142 q->subpacket[s].cookversion);
1143 q->subpacket[s].joint_stereo = 0;
1144 switch (q->subpacket[s].cookversion) {
1145 case MONO:
1146 if (channels != 1) {
1147 avpriv_request_sample(avctx, "Container channels != 1");
1148 return AVERROR_PATCHWELCOME;
1149 }
1150 av_log(avctx, AV_LOG_DEBUG, "MONO\n");
1151 break;
1152 case STEREO:
1153 if (channels != 1) {
1155 q->subpacket[s].num_channels = 2;
1156 }
1157 av_log(avctx, AV_LOG_DEBUG, "STEREO\n");
1158 break;
1159 case JOINT_STEREO:
1160 if (channels != 2) {
1161 avpriv_request_sample(avctx, "Container channels != 2");
1162 return AVERROR_PATCHWELCOME;
1163 }
1164 av_log(avctx, AV_LOG_DEBUG, "JOINT_STEREO\n");
1165 if (avctx->extradata_size >= 16) {
1168 q->subpacket[s].joint_stereo = 1;
1169 q->subpacket[s].num_channels = 2;
1170 }
1171 if (q->subpacket[s].samples_per_channel > 256) {
1173 }
1174 if (q->subpacket[s].samples_per_channel > 512) {
1176 }
1177 break;
1178 case MC_COOK:
1179 av_log(avctx, AV_LOG_DEBUG, "MULTI_CHANNEL\n");
1180 channel_mask |= q->subpacket[s].channel_mask = bytestream2_get_be32(&gb);
1181
1182 if (av_popcount64(q->subpacket[s].channel_mask) > 1) {
1185 q->subpacket[s].joint_stereo = 1;
1186 q->subpacket[s].num_channels = 2;
1187 q->subpacket[s].samples_per_channel = samples_per_frame >> 1;
1188
1189 if (q->subpacket[s].samples_per_channel > 256) {
1191 }
1192 if (q->subpacket[s].samples_per_channel > 512) {
1194 }
1195 } else
1196 q->subpacket[s].samples_per_channel = samples_per_frame;
1197
1198 break;
1199 default:
1200 avpriv_request_sample(avctx, "Cook version %d",
1201 q->subpacket[s].cookversion);
1202 return AVERROR_PATCHWELCOME;
1203 }
1204
1205 if (s > 1 && q->subpacket[s].samples_per_channel != q->samples_per_channel) {
1206 av_log(avctx, AV_LOG_ERROR, "different number of samples per channel!\n");
1207 return AVERROR_INVALIDDATA;
1208 } else
1210
1211
1212 /* Initialize variable relations */
1214
1215 /* Try to catch some obviously faulty streams, otherwise it might be exploitable */
1216 if (q->subpacket[s].total_subbands > 53) {
1217 avpriv_request_sample(avctx, "total_subbands > 53");
1218 return AVERROR_PATCHWELCOME;
1219 }
1220
1221 if ((q->subpacket[s].js_vlc_bits > 6) ||
1222 (q->subpacket[s].js_vlc_bits < 2 * q->subpacket[s].joint_stereo)) {
1223 av_log(avctx, AV_LOG_ERROR, "js_vlc_bits = %d, only >= %d and <= 6 allowed!\n",
1225 return AVERROR_INVALIDDATA;
1226 }
1227
1228 if (q->subpacket[s].subbands > 50) {
1229 avpriv_request_sample(avctx, "subbands > 50");
1230 return AVERROR_PATCHWELCOME;
1231 }
1232 if (q->subpacket[s].subbands == 0) {
1233 avpriv_request_sample(avctx, "subbands = 0");
1234 return AVERROR_PATCHWELCOME;
1235 }
1240
1241 if (total_channels + q->subpacket[s].num_channels > channels) {
1242 av_log(avctx, AV_LOG_ERROR, "Too many subpacket channels %d for channels %d\n",
1243 total_channels + q->subpacket[s].num_channels, channels);
1244 return AVERROR_INVALIDDATA;
1245 }
1246 total_channels += q->subpacket[s].num_channels;
1247
1248 q->num_subpackets++;
1249 s++;
1250 }
1251
1252 /* Try to catch some obviously faulty streams, otherwise it might be exploitable */
1253 if (q->samples_per_channel != 256 && q->samples_per_channel != 512 &&
1254 q->samples_per_channel != 1024) {
1255 avpriv_request_sample(avctx, "samples_per_channel = %d",
1257 return AVERROR_PATCHWELCOME;
1258 }
1259
1260 /* Generate tables */
1261 ff_thread_once(&init_static_once, init_pow2table);
1262 init_gain_table(q);
1264
1265 if ((ret = init_cook_vlc_tables(q)))
1266 return ret;
1267
1268 /* Pad the databuffer with:
1269 DECODE_BYTES_PAD1 or DECODE_BYTES_PAD2 for decode_bytes(),
1270 AV_INPUT_BUFFER_PADDING_SIZE, for the bitstreamreader. */
1272 av_mallocz(avctx->block_align
1275 if (!q->decoded_bytes_buffer)
1276 return AVERROR(ENOMEM);
1277
1278 /* Initialize transform. */
1279 if ((ret = init_cook_mlt(q)))
1280 return ret;
1281
1282 /* Initialize COOK signal arithmetic handling */
1283 if (1) {
1289 }
1290
1293 if (channel_mask)
1294 av_channel_layout_from_mask(&avctx->ch_layout, channel_mask);
1295 else
1297
1298
1300
1301 return 0;
1302}
1303
1305 .p.name = "cook",
1306 CODEC_LONG_NAME("Cook / Cooker / Gecko (RealAudio G2)"),
1307 .p.type = AVMEDIA_TYPE_AUDIO,
1308 .p.id = AV_CODEC_ID_COOK,
1309 .priv_data_size = sizeof(COOKContext),
1313 .p.capabilities = AV_CODEC_CAP_DR1,
1314 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
1315};
const FFCodec ff_cook_decoder
Definition cook.c:1304
channels
Definition aptx.h:31
subbands
Definition aptx.h:37
static av_cold void close(AVCodecParserContext *s)
Definition apv_parser.c:197
#define JOINT_STEREO
Definition atrac3.c:59
Libavcodec external API header.
#define bits_left
Definition bitstream.h:116
#define AV_BE2NE32C(x)
Definition bswap.h:99
static av_always_inline int bytestream2_get_bytes_left(const GetByteContext *g)
Definition bytestream.h:158
static av_always_inline void bytestream2_init(GetByteContext *g, const uint8_t *buf, int buf_size)
Definition bytestream.h:137
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define s(width, name)
Definition cbs_vp9.c:198
Public libavutil channel layout APIs header.
#define FF_CODEC_DECODE_CB(func)
#define CODEC_LONG_NAME(str)
#define FF_CODEC_CAP_INIT_CLEANUP
The codec allows calling the close function for deallocation even if the init function returned a fai...
#define av_popcount64
Definition common.h:157
#define av_clip_uintp2
Definition common.h:124
static int decode_bytes(const uint8_t *inbuffer, uint8_t *out, int bytes)
Cook indata decoding, every 32 bits are XORed with 0x37c511f2.
Definition cook.c:304
static av_cold int build_vlc(VLC *vlc, int nb_bits, const uint8_t counts[16], const void *syms, int symbol_size, int offset, void *logctx)
Definition cook.c:201
static void imlt_gain(COOKContext *q, float *inbuffer, cook_gains *gains_ptr, float *previous_buffer)
The modulated lapped transform, this takes transform coefficients and transforms them into timedomain...
Definition cook.c:740
static int decode_envelope(COOKContext *q, COOKSubpacket *p, int *quant_index_table)
Create the quant index table needed for the envelope.
Definition cook.c:386
static void saturate_output_float(COOKContext *q, float *out)
Saturate the output signal and interleave.
Definition cook.c:905
static int joint_decode(COOKContext *q, COOKSubpacket *p, float *mlt_buffer_left, float *mlt_buffer_right)
function for decoding joint stereo data
Definition cook.c:831
static void dump_cook_context(COOKContext *q)
Definition cook.c:1048
#define QUANT_VLC_BITS
Definition cook.c:72
#define SUBBAND_SIZE
Definition cook.c:69
static void categorize(COOKContext *q, COOKSubpacket *p, const int *quant_index_table, int *category, int *category_index)
Calculate the category and category_index vector.
Definition cook.c:427
static void interpolate_float(COOKContext *q, float *buffer, int gain_index, int gain_index_next)
the actual requantization of the timedomain samples
Definition cook.c:685
static int decouple_info(COOKContext *q, COOKSubpacket *p, int *decouple_tab)
function for getting the jointstereo coupling information
Definition cook.c:770
#define STEREO
Definition cook.c:65
static av_cold void init_pow2table(void)
Definition cook.c:175
#define COUPLING_VLC_BITS
Definition cook.c:73
static void decouple_float(COOKContext *q, COOKSubpacket *p, int subband, float f1, float f2, float *decode_buffer, float *mlt_buffer1, float *mlt_buffer2)
function decouples a pair of signals from a single signal via multiplication.
Definition cook.c:809
static void imlt_window_float(COOKContext *q, float *inbuffer, cook_gains *gains_ptr, float *previous_buffer)
Apply transform window, overlap buffers.
Definition cook.c:712
static float rootpow2tab[127]
Definition cook.c:170
static av_cold void init_cplscales_table(COOKContext *q)
Definition cook.c:272
#define PRINT(a, b)
#define MC_COOK
Definition cook.c:67
static int unpack_SQVH(COOKContext *q, COOKSubpacket *p, int category, int *subband_coef_index, int *subband_coef_sign)
Unpack the subband_coef_index and subband_coef_sign vectors.
Definition cook.c:575
static int decode_subpacket(COOKContext *q, COOKSubpacket *p, const uint8_t *inbuffer, float **outbuffer)
Cook subpacket decoding.
Definition cook.c:941
#define DECODE_BYTES_PAD1(bytes)
Definition cook.c:281
static void expand_category(COOKContext *q, int *category, int *category_index)
Expand the category vector.
Definition cook.c:524
static int mono_decode(COOKContext *q, COOKSubpacket *p, float *mlt_buffer)
Definition cook.c:655
#define MONO
Definition cook.c:64
static void scalar_dequant_float(COOKContext *q, int index, int quant_index, int *subband_coef_index, int *subband_coef_sign, float *mlt_p)
The real requantization of the mltcoefs.
Definition cook.c:546
static av_cold void init_gain_table(COOKContext *q)
Definition cook.c:192
static void decode_gain_info(GetBitContext *gb, int *gaininfo)
Fill the gain array for the timedomain quantization.
Definition cook.c:362
static av_cold int init_cook_mlt(COOKContext *q)
Definition cook.c:249
#define MAX_SUBPACKETS
Definition cook.c:70
static void decode_bytes_and_gain(COOKContext *q, COOKSubpacket *p, const uint8_t *inbuffer, cook_gains *gains_ptr)
First part of subpacket decoding: decode raw stream bytes and read gain info.
Definition cook.c:883
static void decode_vectors(COOKContext *q, COOKSubpacket *p, int *category, int *quant_index_table, float *mlt_buffer)
Fill the mlt_buffer with mlt coefficients.
Definition cook.c:619
static void mlt_compensate_output(COOKContext *q, float *decode_buffer, cook_gains *gains_ptr, float *previous_buffer, float *out)
Final part of subpacket decoding: Apply modulated lapped transform, gain compensation,...
Definition cook.c:923
static av_cold int cook_decode_init(AVCodecContext *avctx)
Cook initialization.
Definition cook.c:1075
static av_cold int init_cook_vlc_tables(COOKContext *q)
Definition cook.c:217
static float pow2tab[127]
Definition cook.c:169
static av_cold int cook_decode_close(AVCodecContext *avctx)
Definition cook.c:330
static int cook_decode_frame(AVCodecContext *avctx, AVFrame *frame, int *got_frame_ptr, AVPacket *avpkt)
Definition cook.c:983
Cook AKA RealAudio G2 compatible decoder data.
static const int kmax_tab[7]
Definition cookdata.h:57
static const uint8_t *const ccpl_huffsyms[5]
Definition cookdata.h:278
static const void *const cvh_huffsyms[7]
Definition cookdata.h:241
static const int expbits_tab[8]
Definition cookdata.h:35
static const float quant_centroid_tab[7][14]
Definition cookdata.h:43
static const uint8_t ccpl_huffcounts[5][16]
Definition cookdata.h:270
static const uint8_t cvh_huffcounts[7][16]
Definition cookdata.h:125
static const int vd_tab[7]
Definition cookdata.h:61
static const int vpr_tab[7]
Definition cookdata.h:65
static const uint8_t envelope_quant_index_huffcounts[13][16]
Definition cookdata.h:79
#define MAX_COOK_VLC_ENTRIES
Definition cookdata.h:73
static const float dither_tab[9]
Definition cookdata.h:39
static const uint8_t envelope_quant_index_huffsyms[13][24]
Definition cookdata.h:95
static const float *const cplscales[5]
Definition cookdata.h:357
static const int cplband[51]
Definition cookdata.h:285
static const int invradix_tab[7]
Definition cookdata.h:53
static const int vhvlcsize_tab[7]
Definition cookdata.h:75
#define NULL
Definition coverity.c:32
#define min(a, b)
#define max(a, b)
static const uint16_t fc[]
Definition dcaenc.h:43
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition decode.c:1777
static AVFrame * frame
int(* init)(AVBSFContext *ctx)
Definition dts2pts.c:608
bitstream reader API header.
static av_always_inline int get_vlc2(GetBitContext *s, const VLCElem *table, int bits, int max_depth)
Parse a vlc code.
Definition get_bits.h:645
static int get_bits_left(GetBitContext *gb)
Definition get_bits.h:688
static unsigned int get_bits1(GetBitContext *s)
Definition get_bits.h:391
static int get_bits_count(const GetBitContext *s)
Definition get_bits.h:254
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition get_bits.h:337
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
Definition get_bits.h:517
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
Definition codec.h:49
@ AV_CODEC_ID_COOK
Definition codec_id.h:473
#define AV_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding.
Definition defs.h:40
void av_channel_layout_default(AVChannelLayout *ch_layout, int nb_channels)
Get the default channel layout for a given number of channels.
void av_channel_layout_uninit(AVChannelLayout *channel_layout)
Free any allocated data in the channel layout and reset the channel count to 0.
int av_channel_layout_from_mask(AVChannelLayout *channel_layout, uint64_t mask)
Initialize a native channel layout from a bitmask indicating which channels are present.
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition error.h:64
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition error.h:61
#define AVERROR(e)
Definition error.h:45
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition log.h:231
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
@ AVMEDIA_TYPE_AUDIO
Definition avutil.h:201
@ AV_SAMPLE_FMT_FLTP
float, planar
Definition samplefmt.h:66
int index
Definition gxfenc.c:90
static void scale(int *out, const int *in, const int w, const int h, const int shift)
Definition intra.c:278
av_cold void av_lfg_init(AVLFG *c, unsigned int seed)
Definition lfg.c:32
static unsigned int av_lfg_get(AVLFG *c)
Get the next random unsigned 32-bit number using an ALFG.
Definition lfg.h:53
unsigned offset
Definition libaomenc.c:763
av_cold void ff_audiodsp_init(AudioDSPContext *c)
Definition audiodsp.c:65
#define av_cold
Definition attributes.h:117
#define AVOnce
Definition thread.h:202
static int ff_thread_once(char *control, void(*routine)(void))
Definition thread.h:205
#define AV_ONCE_INIT
Definition thread.h:203
#define powf(x, y)
Definition libm.h:52
#define FFSWAP(type, a, b)
Definition macros.h:52
#define FFMIN(a, b)
Definition macros.h:49
#define FFALIGN(x, a)
Definition macros.h:78
#define M_SQRT2
Memory handling functions.
#define DECLARE_ALIGNED(n, t, v)
Declare a variable that is aligned in memory.
category
Definition openal-dec.c:249
void ff_sine_window_init(float *window, int n)
Generate a sine window.
#define FF_ARRAY_ELEMS(a)
int nb_channels
Number of channels in this layout.
main external API structure.
Definition avcodec.h:443
AVChannelLayout ch_layout
Audio channel layout.
Definition avcodec.h:1055
enum AVSampleFormat sample_fmt
audio sample format
Definition avcodec.h:1047
int64_t bit_rate
the average bitrate
Definition avcodec.h:493
int sample_rate
samples per second
Definition avcodec.h:1040
uint8_t * extradata
Out-of-band global headers that may be used by some codecs.
Definition avcodec.h:526
int extradata_size
Definition avcodec.h:527
int block_align
number of bytes per packet if constant and known or 0 Used by some WAV based audio codecs.
Definition avcodec.h:1075
void * priv_data
Definition avcodec.h:470
This structure describes decoded (raw) audio or video data.
Definition frame.h:472
Context structure for the Lagged Fibonacci PRNG.
Definition lfg.h:33
This structure stores compressed data.
Definition packet.h:580
int size
Definition packet.h:604
uint8_t * data
Definition packet.h:603
void(* vector_clipf)(float *dst, const float *src, int len, float min, float max)
Definition audiodsp.h:49
int samples_per_channel
Definition cook.c:138
void(* imlt_window)(struct cook *q, float *buffer1, cook_gains *gains_ptr, float *previous_buffer)
Definition cook.c:125
AudioDSPContext adsp
Definition cook.c:134
int num_vectors
Definition cook.c:137
VLC envelope_quant_index[13]
Definition cook.c:149
int gain_size_factor
Definition cook.c:153
float gain_table[31]
Definition cook.c:154
float decode_buffer_0[1060]
Definition cook.c:162
AVLFG random_state
Definition cook.c:140
void(* scalar_dequant)(struct cook *q, int index, int quant_index, int *subband_coef_index, int *subband_coef_sign, float *mlt_p)
Definition cook.c:114
uint8_t * decoded_bytes_buffer
Definition cook.c:158
int num_subpackets
Definition cook.c:165
void(* decouple)(struct cook *q, COOKSubpacket *p, int subband, float f1, float f2, float *decode_buffer, float *mlt_buffer1, float *mlt_buffer2)
Definition cook.c:118
float * mlt_window
Definition cook.c:146
int discarded_packets
Definition cook.c:141
GetBitContext gb
Definition cook.c:135
void(* interpolate)(struct cook *q, float *buffer, int gain_index, int gain_index_next)
Definition cook.c:128
av_tx_fn mdct_fn
Definition cook.c:145
float decode_buffer_1[1024]
Definition cook.c:160
float mono_mdct_output[2048]
Definition cook.c:159
AVTXContext * mdct_ctx
Definition cook.c:144
AVCodecContext * avctx
Definition cook.c:133
COOKSubpacket subpacket[MAX_SUBPACKETS]
Definition cook.c:166
VLC sqvh[7]
Definition cook.c:150
const float * cplscales[5]
Definition cook.c:164
float decode_buffer_2[1024]
Definition cook.c:161
void(* saturate_output)(struct cook *q, float *out)
Definition cook.c:131
int gain_4[9]
Definition cook.c:106
float mono_previous_buffer1[1024]
Definition cook.c:98
int subbands
Definition cook.c:85
int total_subbands
Definition cook.c:95
int gain_1[9]
Definition cook.c:103
int size
Definition cook.c:82
int js_subband_start
Definition cook.c:86
int samples_per_channel
Definition cook.c:88
int bits_per_subpdiv
Definition cook.c:94
int numvector_size
Definition cook.c:96
int num_channels
Definition cook.c:83
unsigned int channel_mask
Definition cook.c:90
int log2_numvector_size
Definition cook.c:89
int bits_per_subpacket
Definition cook.c:93
VLC channel_coupling
Definition cook.c:91
int cookversion
Definition cook.c:84
cook_gains gains1
Definition cook.c:101
int joint_stereo
Definition cook.c:92
int gain_3[9]
Definition cook.c:105
int gain_2[9]
Definition cook.c:104
int js_vlc_bits
Definition cook.c:87
float mono_previous_buffer2[1024]
Definition cook.c:99
cook_gains gains2
Definition cook.c:102
int ch_idx
Definition cook.c:81
Definition vlc.h:50
VLCElem * table
Definition vlc.h:52
int bits
Definition vlc.h:51
int * now
Definition cook.c:76
int * previous
Definition cook.c:77
#define av_malloc_array(a, b)
#define av_mallocz(s)
#define ff_dlog(a,...)
#define avpriv_request_sample(...)
#define av_freep(p)
#define av_log(a,...)
static uint8_t tmp[40]
Definition aes_ctr.c:52
static FILE * out
Definition movenc.c:55
static char buffer[20]
Definition seek.c:32
static const struct twinvq_data tab
av_cold void av_tx_uninit(AVTXContext **ctx)
Frees a context and sets *ctx to NULL, does nothing when *ctx == NULL.
Definition tx.c:295
av_cold int av_tx_init(AVTXContext **ctx, av_tx_fn *tx, enum AVTXType type, int inv, int len, const void *scale, uint64_t flags)
Initialize a transform context with the given configuration (i)MDCTs with an odd length are currently...
Definition tx.c:903
@ AV_TX_FULL_IMDCT
Performs a full inverse MDCT rather than leaving out samples that can be derived through symmetry.
Definition tx.h:175
@ AV_TX_FLOAT_MDCT
Standard MDCT with a sample data type of float, double or int32_t, respectively.
Definition tx.h:68
void(* av_tx_fn)(AVTXContext *s, void *out, void *in, ptrdiff_t stride)
Function pointer to a function to perform the transform.
Definition tx.h:151
static int get_unary(GetBitContext *gb, int stop, int len)
Get unary code of limited length.
Definition unary.h:46
int ff_vlc_init_from_lengths(VLC *vlc, int nb_bits, int nb_codes, const int8_t *lens, int lens_wrap, const void *symbols, int symbols_wrap, int symbols_size, int offset, int flags, void *logctx)
Build VLC decoding tables suitable for use with get_vlc2()
Definition vlc.c:306
void ff_vlc_free(VLC *vlc)
Definition vlc.c:580
static int bias(int x, int c)
Definition vqcdec.c:115
static double c[64]