You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1327 lines
45KB

  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. * @file
  24. * Cook compatible decoder. Bastardization of the G.722.1 standard.
  25. * This decoder handles RealNetworks, RealAudio G2 data.
  26. * Cook is identified by the codec name cook in RM files.
  27. *
  28. * To use this decoder, a calling application must supply the extradata
  29. * bytes provided from the RM container; 8+ bytes for mono streams and
  30. * 16+ for stereo streams (maybe more).
  31. *
  32. * Codec technicalities (all this assume a buffer length of 1024):
  33. * Cook works with several different techniques to achieve its compression.
  34. * In the timedomain the buffer is divided into 8 pieces and quantized. If
  35. * two neighboring pieces have different quantization index a smooth
  36. * quantization curve is used to get a smooth overlap between the different
  37. * pieces.
  38. * To get to the transformdomain Cook uses a modulated lapped transform.
  39. * The transform domain has 50 subbands with 20 elements each. This
  40. * means only a maximum of 50*20=1000 coefficients are used out of the 1024
  41. * available.
  42. */
  43. #include "libavutil/lfg.h"
  44. #include "avcodec.h"
  45. #include "get_bits.h"
  46. #include "dsputil.h"
  47. #include "bytestream.h"
  48. #include "fft.h"
  49. #include "libavutil/audioconvert.h"
  50. #include "sinewin.h"
  51. #include "cookdata.h"
  52. /* the different Cook versions */
  53. #define MONO 0x1000001
  54. #define STEREO 0x1000002
  55. #define JOINT_STEREO 0x1000003
  56. #define MC_COOK 0x2000000 // multichannel Cook, not supported
  57. #define SUBBAND_SIZE 20
  58. #define MAX_SUBPACKETS 5
  59. typedef struct {
  60. int *now;
  61. int *previous;
  62. } cook_gains;
  63. typedef struct {
  64. int ch_idx;
  65. int size;
  66. int num_channels;
  67. int cookversion;
  68. int samples_per_frame;
  69. int subbands;
  70. int js_subband_start;
  71. int js_vlc_bits;
  72. int samples_per_channel;
  73. int log2_numvector_size;
  74. unsigned int channel_mask;
  75. VLC ccpl; ///< channel coupling
  76. int joint_stereo;
  77. int bits_per_subpacket;
  78. int bits_per_subpdiv;
  79. int total_subbands;
  80. int numvector_size; ///< 1 << log2_numvector_size;
  81. float mono_previous_buffer1[1024];
  82. float mono_previous_buffer2[1024];
  83. /** gain buffers */
  84. cook_gains gains1;
  85. cook_gains gains2;
  86. int gain_1[9];
  87. int gain_2[9];
  88. int gain_3[9];
  89. int gain_4[9];
  90. } COOKSubpacket;
  91. typedef struct cook {
  92. /*
  93. * The following 5 functions provide the lowlevel arithmetic on
  94. * the internal audio buffers.
  95. */
  96. void (*scalar_dequant)(struct cook *q, int index, int quant_index,
  97. int *subband_coef_index, int *subband_coef_sign,
  98. float *mlt_p);
  99. void (*decouple)(struct cook *q,
  100. COOKSubpacket *p,
  101. int subband,
  102. float f1, float f2,
  103. float *decode_buffer,
  104. float *mlt_buffer1, float *mlt_buffer2);
  105. void (*imlt_window)(struct cook *q, float *buffer1,
  106. cook_gains *gains_ptr, float *previous_buffer);
  107. void (*interpolate)(struct cook *q, float *buffer,
  108. int gain_index, int gain_index_next);
  109. void (*saturate_output)(struct cook *q, int chan, float *out);
  110. AVCodecContext* avctx;
  111. AVFrame frame;
  112. GetBitContext gb;
  113. /* stream data */
  114. int nb_channels;
  115. int bit_rate;
  116. int sample_rate;
  117. int num_vectors;
  118. int samples_per_channel;
  119. /* states */
  120. AVLFG random_state;
  121. int discarded_packets;
  122. /* transform data */
  123. FFTContext mdct_ctx;
  124. float* mlt_window;
  125. /* VLC data */
  126. VLC envelope_quant_index[13];
  127. VLC sqvh[7]; // scalar quantization
  128. /* generatable tables and related variables */
  129. int gain_size_factor;
  130. float gain_table[23];
  131. /* data buffers */
  132. uint8_t* decoded_bytes_buffer;
  133. DECLARE_ALIGNED(32, float, mono_mdct_output)[2048];
  134. float decode_buffer_1[1024];
  135. float decode_buffer_2[1024];
  136. float decode_buffer_0[1060]; /* static allocation for joint decode */
  137. const float *cplscales[5];
  138. int num_subpackets;
  139. COOKSubpacket subpacket[MAX_SUBPACKETS];
  140. } COOKContext;
  141. static float pow2tab[127];
  142. static float rootpow2tab[127];
  143. /*************** init functions ***************/
  144. /* table generator */
  145. static av_cold void init_pow2table(void)
  146. {
  147. int i;
  148. for (i = -63; i < 64; i++) {
  149. pow2tab[63 + i] = pow(2, i);
  150. rootpow2tab[63 + i] = sqrt(pow(2, i));
  151. }
  152. }
  153. /* table generator */
  154. static av_cold void init_gain_table(COOKContext *q)
  155. {
  156. int i;
  157. q->gain_size_factor = q->samples_per_channel / 8;
  158. for (i = 0; i < 23; i++)
  159. q->gain_table[i] = pow(pow2tab[i + 52],
  160. (1.0 / (double) q->gain_size_factor));
  161. }
  162. static av_cold int init_cook_vlc_tables(COOKContext *q)
  163. {
  164. int i, result;
  165. result = 0;
  166. for (i = 0; i < 13; i++) {
  167. result |= init_vlc(&q->envelope_quant_index[i], 9, 24,
  168. envelope_quant_index_huffbits[i], 1, 1,
  169. envelope_quant_index_huffcodes[i], 2, 2, 0);
  170. }
  171. av_log(q->avctx, AV_LOG_DEBUG, "sqvh VLC init\n");
  172. for (i = 0; i < 7; i++) {
  173. result |= init_vlc(&q->sqvh[i], vhvlcsize_tab[i], vhsize_tab[i],
  174. cvh_huffbits[i], 1, 1,
  175. cvh_huffcodes[i], 2, 2, 0);
  176. }
  177. for (i = 0; i < q->num_subpackets; i++) {
  178. if (q->subpacket[i].joint_stereo == 1) {
  179. result |= init_vlc(&q->subpacket[i].ccpl, 6, (1 << q->subpacket[i].js_vlc_bits) - 1,
  180. ccpl_huffbits[q->subpacket[i].js_vlc_bits - 2], 1, 1,
  181. ccpl_huffcodes[q->subpacket[i].js_vlc_bits - 2], 2, 2, 0);
  182. av_log(q->avctx, AV_LOG_DEBUG, "subpacket %i Joint-stereo VLC used.\n", i);
  183. }
  184. }
  185. av_log(q->avctx, AV_LOG_DEBUG, "VLC tables initialized.\n");
  186. return result;
  187. }
  188. static av_cold int init_cook_mlt(COOKContext *q)
  189. {
  190. int j, ret;
  191. int mlt_size = q->samples_per_channel;
  192. if ((q->mlt_window = av_malloc(mlt_size * sizeof(*q->mlt_window))) == 0)
  193. return AVERROR(ENOMEM);
  194. /* Initialize the MLT window: simple sine window. */
  195. ff_sine_window_init(q->mlt_window, mlt_size);
  196. for (j = 0; j < mlt_size; j++)
  197. q->mlt_window[j] *= sqrt(2.0 / q->samples_per_channel);
  198. /* Initialize the MDCT. */
  199. if ((ret = ff_mdct_init(&q->mdct_ctx, av_log2(mlt_size) + 1, 1, 1.0 / 32768.0))) {
  200. av_free(q->mlt_window);
  201. return ret;
  202. }
  203. av_log(q->avctx, AV_LOG_DEBUG, "MDCT initialized, order = %d.\n",
  204. av_log2(mlt_size) + 1);
  205. return 0;
  206. }
  207. static const float *maybe_reformat_buffer32(COOKContext *q, const float *ptr, int n)
  208. {
  209. if (1)
  210. return ptr;
  211. }
  212. static av_cold void init_cplscales_table(COOKContext *q)
  213. {
  214. int i;
  215. for (i = 0; i < 5; i++)
  216. q->cplscales[i] = maybe_reformat_buffer32(q, cplscales[i], (1 << (i + 2)) - 1);
  217. }
  218. /*************** init functions end ***********/
  219. #define DECODE_BYTES_PAD1(bytes) (3 - ((bytes) + 3) % 4)
  220. #define DECODE_BYTES_PAD2(bytes) ((bytes) % 4 + DECODE_BYTES_PAD1(2 * (bytes)))
  221. /**
  222. * Cook indata decoding, every 32 bits are XORed with 0x37c511f2.
  223. * Why? No idea, some checksum/error detection method maybe.
  224. *
  225. * Out buffer size: extra bytes are needed to cope with
  226. * padding/misalignment.
  227. * Subpackets passed to the decoder can contain two, consecutive
  228. * half-subpackets, of identical but arbitrary size.
  229. * 1234 1234 1234 1234 extraA extraB
  230. * Case 1: AAAA BBBB 0 0
  231. * Case 2: AAAA ABBB BB-- 3 3
  232. * Case 3: AAAA AABB BBBB 2 2
  233. * Case 4: AAAA AAAB BBBB BB-- 1 5
  234. *
  235. * Nice way to waste CPU cycles.
  236. *
  237. * @param inbuffer pointer to byte array of indata
  238. * @param out pointer to byte array of outdata
  239. * @param bytes number of bytes
  240. */
  241. static inline int decode_bytes(const uint8_t *inbuffer, uint8_t *out, int bytes)
  242. {
  243. static const uint32_t tab[4] = {
  244. AV_BE2NE32C(0x37c511f2U), AV_BE2NE32C(0xf237c511U),
  245. AV_BE2NE32C(0x11f237c5U), AV_BE2NE32C(0xc511f237U),
  246. };
  247. int i, off;
  248. uint32_t c;
  249. const uint32_t *buf;
  250. uint32_t *obuf = (uint32_t *) out;
  251. /* FIXME: 64 bit platforms would be able to do 64 bits at a time.
  252. * I'm too lazy though, should be something like
  253. * for (i = 0; i < bitamount / 64; i++)
  254. * (int64_t) out[i] = 0x37c511f237c511f2 ^ av_be2ne64(int64_t) in[i]);
  255. * Buffer alignment needs to be checked. */
  256. off = (intptr_t) inbuffer & 3;
  257. buf = (const uint32_t *) (inbuffer - off);
  258. c = tab[off];
  259. bytes += 3 + off;
  260. for (i = 0; i < bytes / 4; i++)
  261. obuf[i] = c ^ buf[i];
  262. return off;
  263. }
  264. /**
  265. * Cook uninit
  266. */
  267. static av_cold int cook_decode_close(AVCodecContext *avctx)
  268. {
  269. int i;
  270. COOKContext *q = avctx->priv_data;
  271. av_log(avctx, AV_LOG_DEBUG, "Deallocating memory.\n");
  272. /* Free allocated memory buffers. */
  273. av_free(q->mlt_window);
  274. av_free(q->decoded_bytes_buffer);
  275. /* Free the transform. */
  276. ff_mdct_end(&q->mdct_ctx);
  277. /* Free the VLC tables. */
  278. for (i = 0; i < 13; i++)
  279. ff_free_vlc(&q->envelope_quant_index[i]);
  280. for (i = 0; i < 7; i++)
  281. ff_free_vlc(&q->sqvh[i]);
  282. for (i = 0; i < q->num_subpackets; i++)
  283. ff_free_vlc(&q->subpacket[i].ccpl);
  284. av_log(avctx, AV_LOG_DEBUG, "Memory deallocated.\n");
  285. return 0;
  286. }
  287. /**
  288. * Fill the gain array for the timedomain quantization.
  289. *
  290. * @param gb pointer to the GetBitContext
  291. * @param gaininfo array[9] of gain indexes
  292. */
  293. static void decode_gain_info(GetBitContext *gb, int *gaininfo)
  294. {
  295. int i, n;
  296. while (get_bits1(gb)) {
  297. /* NOTHING */
  298. }
  299. n = get_bits_count(gb) - 1; // amount of elements*2 to update
  300. i = 0;
  301. while (n--) {
  302. int index = get_bits(gb, 3);
  303. int gain = get_bits1(gb) ? get_bits(gb, 4) - 7 : -1;
  304. while (i <= index)
  305. gaininfo[i++] = gain;
  306. }
  307. while (i <= 8)
  308. gaininfo[i++] = 0;
  309. }
  310. /**
  311. * Create the quant index table needed for the envelope.
  312. *
  313. * @param q pointer to the COOKContext
  314. * @param quant_index_table pointer to the array
  315. */
  316. static int decode_envelope(COOKContext *q, COOKSubpacket *p,
  317. int *quant_index_table)
  318. {
  319. int i, j, vlc_index;
  320. quant_index_table[0] = get_bits(&q->gb, 6) - 6; // This is used later in categorize
  321. for (i = 1; i < p->total_subbands; i++) {
  322. vlc_index = i;
  323. if (i >= p->js_subband_start * 2) {
  324. vlc_index -= p->js_subband_start;
  325. } else {
  326. vlc_index /= 2;
  327. if (vlc_index < 1)
  328. vlc_index = 1;
  329. }
  330. if (vlc_index > 13)
  331. vlc_index = 13; // the VLC tables >13 are identical to No. 13
  332. j = get_vlc2(&q->gb, q->envelope_quant_index[vlc_index - 1].table,
  333. q->envelope_quant_index[vlc_index - 1].bits, 2);
  334. quant_index_table[i] = quant_index_table[i - 1] + j - 12; // differential encoding
  335. if (quant_index_table[i] > 63 || quant_index_table[i] < -63) {
  336. av_log(q->avctx, AV_LOG_ERROR,
  337. "Invalid quantizer %d at position %d, outside [-63, 63] range\n",
  338. quant_index_table[i], i);
  339. return AVERROR_INVALIDDATA;
  340. }
  341. }
  342. return 0;
  343. }
  344. /**
  345. * Calculate the category and category_index vector.
  346. *
  347. * @param q pointer to the COOKContext
  348. * @param quant_index_table pointer to the array
  349. * @param category pointer to the category array
  350. * @param category_index pointer to the category_index array
  351. */
  352. static void categorize(COOKContext *q, COOKSubpacket *p, const int *quant_index_table,
  353. int *category, int *category_index)
  354. {
  355. int exp_idx, bias, tmpbias1, tmpbias2, bits_left, num_bits, index, v, i, j;
  356. int exp_index2[102];
  357. int exp_index1[102];
  358. int tmp_categorize_array[128 * 2];
  359. int tmp_categorize_array1_idx = p->numvector_size;
  360. int tmp_categorize_array2_idx = p->numvector_size;
  361. bits_left = p->bits_per_subpacket - get_bits_count(&q->gb);
  362. if (bits_left > q->samples_per_channel) {
  363. bits_left = q->samples_per_channel +
  364. ((bits_left - q->samples_per_channel) * 5) / 8;
  365. //av_log(q->avctx, AV_LOG_ERROR, "bits_left = %d\n",bits_left);
  366. }
  367. memset(&exp_index1, 0, sizeof(exp_index1));
  368. memset(&exp_index2, 0, sizeof(exp_index2));
  369. memset(&tmp_categorize_array, 0, sizeof(tmp_categorize_array));
  370. bias = -32;
  371. /* Estimate bias. */
  372. for (i = 32; i > 0; i = i / 2) {
  373. num_bits = 0;
  374. index = 0;
  375. for (j = p->total_subbands; j > 0; j--) {
  376. exp_idx = av_clip((i - quant_index_table[index] + bias) / 2, 0, 7);
  377. index++;
  378. num_bits += expbits_tab[exp_idx];
  379. }
  380. if (num_bits >= bits_left - 32)
  381. bias += i;
  382. }
  383. /* Calculate total number of bits. */
  384. num_bits = 0;
  385. for (i = 0; i < p->total_subbands; i++) {
  386. exp_idx = av_clip((bias - quant_index_table[i]) / 2, 0, 7);
  387. num_bits += expbits_tab[exp_idx];
  388. exp_index1[i] = exp_idx;
  389. exp_index2[i] = exp_idx;
  390. }
  391. tmpbias1 = tmpbias2 = num_bits;
  392. for (j = 1; j < p->numvector_size; j++) {
  393. if (tmpbias1 + tmpbias2 > 2 * bits_left) { /* ---> */
  394. int max = -999999;
  395. index = -1;
  396. for (i = 0; i < p->total_subbands; i++) {
  397. if (exp_index1[i] < 7) {
  398. v = (-2 * exp_index1[i]) - quant_index_table[i] + bias;
  399. if (v >= max) {
  400. max = v;
  401. index = i;
  402. }
  403. }
  404. }
  405. if (index == -1)
  406. break;
  407. tmp_categorize_array[tmp_categorize_array1_idx++] = index;
  408. tmpbias1 -= expbits_tab[exp_index1[index]] -
  409. expbits_tab[exp_index1[index] + 1];
  410. ++exp_index1[index];
  411. } else { /* <--- */
  412. int min = 999999;
  413. index = -1;
  414. for (i = 0; i < p->total_subbands; i++) {
  415. if (exp_index2[i] > 0) {
  416. v = (-2 * exp_index2[i]) - quant_index_table[i] + bias;
  417. if (v < min) {
  418. min = v;
  419. index = i;
  420. }
  421. }
  422. }
  423. if (index == -1)
  424. break;
  425. tmp_categorize_array[--tmp_categorize_array2_idx] = index;
  426. tmpbias2 -= expbits_tab[exp_index2[index]] -
  427. expbits_tab[exp_index2[index] - 1];
  428. --exp_index2[index];
  429. }
  430. }
  431. for (i = 0; i < p->total_subbands; i++)
  432. category[i] = exp_index2[i];
  433. for (i = 0; i < p->numvector_size - 1; i++)
  434. category_index[i] = tmp_categorize_array[tmp_categorize_array2_idx++];
  435. }
  436. /**
  437. * Expand the category vector.
  438. *
  439. * @param q pointer to the COOKContext
  440. * @param category pointer to the category array
  441. * @param category_index pointer to the category_index array
  442. */
  443. static inline void expand_category(COOKContext *q, int *category,
  444. int *category_index)
  445. {
  446. int i;
  447. for (i = 0; i < q->num_vectors; i++)
  448. {
  449. int idx = category_index[i];
  450. if (++category[idx] >= FF_ARRAY_ELEMS(dither_tab))
  451. --category[idx];
  452. }
  453. }
  454. /**
  455. * The real requantization of the mltcoefs
  456. *
  457. * @param q pointer to the COOKContext
  458. * @param index index
  459. * @param quant_index quantisation index
  460. * @param subband_coef_index array of indexes to quant_centroid_tab
  461. * @param subband_coef_sign signs of coefficients
  462. * @param mlt_p pointer into the mlt buffer
  463. */
  464. static void scalar_dequant_float(COOKContext *q, int index, int quant_index,
  465. int *subband_coef_index, int *subband_coef_sign,
  466. float *mlt_p)
  467. {
  468. int i;
  469. float f1;
  470. for (i = 0; i < SUBBAND_SIZE; i++) {
  471. if (subband_coef_index[i]) {
  472. f1 = quant_centroid_tab[index][subband_coef_index[i]];
  473. if (subband_coef_sign[i])
  474. f1 = -f1;
  475. } else {
  476. /* noise coding if subband_coef_index[i] == 0 */
  477. f1 = dither_tab[index];
  478. if (av_lfg_get(&q->random_state) < 0x80000000)
  479. f1 = -f1;
  480. }
  481. mlt_p[i] = f1 * rootpow2tab[quant_index + 63];
  482. }
  483. }
  484. /**
  485. * Unpack the subband_coef_index and subband_coef_sign vectors.
  486. *
  487. * @param q pointer to the COOKContext
  488. * @param category pointer to the category array
  489. * @param subband_coef_index array of indexes to quant_centroid_tab
  490. * @param subband_coef_sign signs of coefficients
  491. */
  492. static int unpack_SQVH(COOKContext *q, COOKSubpacket *p, int category,
  493. int *subband_coef_index, int *subband_coef_sign)
  494. {
  495. int i, j;
  496. int vlc, vd, tmp, result;
  497. vd = vd_tab[category];
  498. result = 0;
  499. for (i = 0; i < vpr_tab[category]; i++) {
  500. vlc = get_vlc2(&q->gb, q->sqvh[category].table, q->sqvh[category].bits, 3);
  501. if (p->bits_per_subpacket < get_bits_count(&q->gb)) {
  502. vlc = 0;
  503. result = 1;
  504. }
  505. for (j = vd - 1; j >= 0; j--) {
  506. tmp = (vlc * invradix_tab[category]) / 0x100000;
  507. subband_coef_index[vd * i + j] = vlc - tmp * (kmax_tab[category] + 1);
  508. vlc = tmp;
  509. }
  510. for (j = 0; j < vd; j++) {
  511. if (subband_coef_index[i * vd + j]) {
  512. if (get_bits_count(&q->gb) < p->bits_per_subpacket) {
  513. subband_coef_sign[i * vd + j] = get_bits1(&q->gb);
  514. } else {
  515. result = 1;
  516. subband_coef_sign[i * vd + j] = 0;
  517. }
  518. } else {
  519. subband_coef_sign[i * vd + j] = 0;
  520. }
  521. }
  522. }
  523. return result;
  524. }
  525. /**
  526. * Fill the mlt_buffer with mlt coefficients.
  527. *
  528. * @param q pointer to the COOKContext
  529. * @param category pointer to the category array
  530. * @param quant_index_table pointer to the array
  531. * @param mlt_buffer pointer to mlt coefficients
  532. */
  533. static void decode_vectors(COOKContext *q, COOKSubpacket *p, int *category,
  534. int *quant_index_table, float *mlt_buffer)
  535. {
  536. /* A zero in this table means that the subband coefficient is
  537. random noise coded. */
  538. int subband_coef_index[SUBBAND_SIZE];
  539. /* A zero in this table means that the subband coefficient is a
  540. positive multiplicator. */
  541. int subband_coef_sign[SUBBAND_SIZE];
  542. int band, j;
  543. int index = 0;
  544. for (band = 0; band < p->total_subbands; band++) {
  545. index = category[band];
  546. if (category[band] < 7) {
  547. if (unpack_SQVH(q, p, category[band], subband_coef_index, subband_coef_sign)) {
  548. index = 7;
  549. for (j = 0; j < p->total_subbands; j++)
  550. category[band + j] = 7;
  551. }
  552. }
  553. if (index >= 7) {
  554. memset(subband_coef_index, 0, sizeof(subband_coef_index));
  555. memset(subband_coef_sign, 0, sizeof(subband_coef_sign));
  556. }
  557. q->scalar_dequant(q, index, quant_index_table[band],
  558. subband_coef_index, subband_coef_sign,
  559. &mlt_buffer[band * SUBBAND_SIZE]);
  560. }
  561. /* FIXME: should this be removed, or moved into loop above? */
  562. if (p->total_subbands * SUBBAND_SIZE >= q->samples_per_channel)
  563. return;
  564. }
  565. /**
  566. * function for decoding mono data
  567. *
  568. * @param q pointer to the COOKContext
  569. * @param mlt_buffer pointer to mlt coefficients
  570. */
  571. static int mono_decode(COOKContext *q, COOKSubpacket *p, float *mlt_buffer)
  572. {
  573. int category_index[128];
  574. int quant_index_table[102];
  575. int category[128];
  576. int res, i;
  577. memset(&category, 0, sizeof(category));
  578. memset(&category_index, 0, sizeof(category_index));
  579. if ((res = decode_envelope(q, p, quant_index_table)) < 0)
  580. return res;
  581. q->num_vectors = get_bits(&q->gb, p->log2_numvector_size);
  582. categorize(q, p, quant_index_table, category, category_index);
  583. expand_category(q, category, category_index);
  584. for (i=0; i<p->total_subbands; i++) {
  585. if (category[i] > 7)
  586. return AVERROR_INVALIDDATA;
  587. }
  588. decode_vectors(q, p, category, quant_index_table, mlt_buffer);
  589. return 0;
  590. }
  591. /**
  592. * the actual requantization of the timedomain samples
  593. *
  594. * @param q pointer to the COOKContext
  595. * @param buffer pointer to the timedomain buffer
  596. * @param gain_index index for the block multiplier
  597. * @param gain_index_next index for the next block multiplier
  598. */
  599. static void interpolate_float(COOKContext *q, float *buffer,
  600. int gain_index, int gain_index_next)
  601. {
  602. int i;
  603. float fc1, fc2;
  604. fc1 = pow2tab[gain_index + 63];
  605. if (gain_index == gain_index_next) { // static gain
  606. for (i = 0; i < q->gain_size_factor; i++)
  607. buffer[i] *= fc1;
  608. } else { // smooth gain
  609. fc2 = q->gain_table[11 + (gain_index_next - gain_index)];
  610. for (i = 0; i < q->gain_size_factor; i++) {
  611. buffer[i] *= fc1;
  612. fc1 *= fc2;
  613. }
  614. }
  615. }
  616. /**
  617. * Apply transform window, overlap buffers.
  618. *
  619. * @param q pointer to the COOKContext
  620. * @param inbuffer pointer to the mltcoefficients
  621. * @param gains_ptr current and previous gains
  622. * @param previous_buffer pointer to the previous buffer to be used for overlapping
  623. */
  624. static void imlt_window_float(COOKContext *q, float *inbuffer,
  625. cook_gains *gains_ptr, float *previous_buffer)
  626. {
  627. const float fc = pow2tab[gains_ptr->previous[0] + 63];
  628. int i;
  629. /* The weird thing here, is that the two halves of the time domain
  630. * buffer are swapped. Also, the newest data, that we save away for
  631. * next frame, has the wrong sign. Hence the subtraction below.
  632. * Almost sounds like a complex conjugate/reverse data/FFT effect.
  633. */
  634. /* Apply window and overlap */
  635. for (i = 0; i < q->samples_per_channel; i++)
  636. inbuffer[i] = inbuffer[i] * fc * q->mlt_window[i] -
  637. previous_buffer[i] * q->mlt_window[q->samples_per_channel - 1 - i];
  638. }
  639. /**
  640. * The modulated lapped transform, this takes transform coefficients
  641. * and transforms them into timedomain samples.
  642. * Apply transform window, overlap buffers, apply gain profile
  643. * and buffer management.
  644. *
  645. * @param q pointer to the COOKContext
  646. * @param inbuffer pointer to the mltcoefficients
  647. * @param gains_ptr current and previous gains
  648. * @param previous_buffer pointer to the previous buffer to be used for overlapping
  649. */
  650. static void imlt_gain(COOKContext *q, float *inbuffer,
  651. cook_gains *gains_ptr, float *previous_buffer)
  652. {
  653. float *buffer0 = q->mono_mdct_output;
  654. float *buffer1 = q->mono_mdct_output + q->samples_per_channel;
  655. int i;
  656. /* Inverse modified discrete cosine transform */
  657. q->mdct_ctx.imdct_calc(&q->mdct_ctx, q->mono_mdct_output, inbuffer);
  658. q->imlt_window(q, buffer1, gains_ptr, previous_buffer);
  659. /* Apply gain profile */
  660. for (i = 0; i < 8; i++)
  661. if (gains_ptr->now[i] || gains_ptr->now[i + 1])
  662. q->interpolate(q, &buffer1[q->gain_size_factor * i],
  663. gains_ptr->now[i], gains_ptr->now[i + 1]);
  664. /* Save away the current to be previous block. */
  665. memcpy(previous_buffer, buffer0,
  666. q->samples_per_channel * sizeof(*previous_buffer));
  667. }
  668. /**
  669. * function for getting the jointstereo coupling information
  670. *
  671. * @param q pointer to the COOKContext
  672. * @param decouple_tab decoupling array
  673. *
  674. */
  675. static int decouple_info(COOKContext *q, COOKSubpacket *p, int *decouple_tab)
  676. {
  677. int i;
  678. int vlc = get_bits1(&q->gb);
  679. int start = cplband[p->js_subband_start];
  680. int end = cplband[p->subbands - 1];
  681. int length = end - start + 1;
  682. if (start > end)
  683. return 0;
  684. if (vlc)
  685. for (i = 0; i < length; i++)
  686. decouple_tab[start + i] = get_vlc2(&q->gb, p->ccpl.table, p->ccpl.bits, 2);
  687. else
  688. for (i = 0; i < length; i++) {
  689. int v = get_bits(&q->gb, p->js_vlc_bits);
  690. if (v == (1<<p->js_vlc_bits)-1) {
  691. av_log(q->avctx, AV_LOG_ERROR, "decouple value too large\n");
  692. return AVERROR_INVALIDDATA;
  693. }
  694. decouple_tab[start + i] = v;
  695. }
  696. return 0;
  697. }
  698. /*
  699. * function decouples a pair of signals from a single signal via multiplication.
  700. *
  701. * @param q pointer to the COOKContext
  702. * @param subband index of the current subband
  703. * @param f1 multiplier for channel 1 extraction
  704. * @param f2 multiplier for channel 2 extraction
  705. * @param decode_buffer input buffer
  706. * @param mlt_buffer1 pointer to left channel mlt coefficients
  707. * @param mlt_buffer2 pointer to right channel mlt coefficients
  708. */
  709. static void decouple_float(COOKContext *q,
  710. COOKSubpacket *p,
  711. int subband,
  712. float f1, float f2,
  713. float *decode_buffer,
  714. float *mlt_buffer1, float *mlt_buffer2)
  715. {
  716. int j, tmp_idx;
  717. for (j = 0; j < SUBBAND_SIZE; j++) {
  718. tmp_idx = ((p->js_subband_start + subband) * SUBBAND_SIZE) + j;
  719. mlt_buffer1[SUBBAND_SIZE * subband + j] = f1 * decode_buffer[tmp_idx];
  720. mlt_buffer2[SUBBAND_SIZE * subband + j] = f2 * decode_buffer[tmp_idx];
  721. }
  722. }
  723. /**
  724. * function for decoding joint stereo data
  725. *
  726. * @param q pointer to the COOKContext
  727. * @param mlt_buffer1 pointer to left channel mlt coefficients
  728. * @param mlt_buffer2 pointer to right channel mlt coefficients
  729. */
  730. static int joint_decode(COOKContext *q, COOKSubpacket *p, float *mlt_buffer1,
  731. float *mlt_buffer2)
  732. {
  733. int i, j, res;
  734. int decouple_tab[SUBBAND_SIZE];
  735. float *decode_buffer = q->decode_buffer_0;
  736. int idx, cpl_tmp;
  737. float f1, f2;
  738. const float *cplscale;
  739. memset(decouple_tab, 0, sizeof(decouple_tab));
  740. memset(decode_buffer, 0, sizeof(q->decode_buffer_0));
  741. /* Make sure the buffers are zeroed out. */
  742. memset(mlt_buffer1, 0, 1024 * sizeof(*mlt_buffer1));
  743. memset(mlt_buffer2, 0, 1024 * sizeof(*mlt_buffer2));
  744. if ((res = decouple_info(q, p, decouple_tab)) < 0)
  745. return res;
  746. if ((res = mono_decode(q, p, decode_buffer)) < 0)
  747. return res;
  748. /* The two channels are stored interleaved in decode_buffer. */
  749. for (i = 0; i < p->js_subband_start; i++) {
  750. for (j = 0; j < SUBBAND_SIZE; j++) {
  751. mlt_buffer1[i * 20 + j] = decode_buffer[i * 40 + j];
  752. mlt_buffer2[i * 20 + j] = decode_buffer[i * 40 + 20 + j];
  753. }
  754. }
  755. /* When we reach js_subband_start (the higher frequencies)
  756. the coefficients are stored in a coupling scheme. */
  757. idx = (1 << p->js_vlc_bits) - 1;
  758. for (i = p->js_subband_start; i < p->subbands; i++) {
  759. cpl_tmp = cplband[i];
  760. idx -= decouple_tab[cpl_tmp];
  761. cplscale = q->cplscales[p->js_vlc_bits - 2]; // choose decoupler table
  762. f1 = cplscale[decouple_tab[cpl_tmp] + 1];
  763. f2 = cplscale[idx];
  764. q->decouple(q, p, i, f1, f2, decode_buffer, mlt_buffer1, mlt_buffer2);
  765. idx = (1 << p->js_vlc_bits) - 1;
  766. }
  767. return 0;
  768. }
  769. /**
  770. * First part of subpacket decoding:
  771. * decode raw stream bytes and read gain info.
  772. *
  773. * @param q pointer to the COOKContext
  774. * @param inbuffer pointer to raw stream data
  775. * @param gains_ptr array of current/prev gain pointers
  776. */
  777. static inline void decode_bytes_and_gain(COOKContext *q, COOKSubpacket *p,
  778. const uint8_t *inbuffer,
  779. cook_gains *gains_ptr)
  780. {
  781. int offset;
  782. offset = decode_bytes(inbuffer, q->decoded_bytes_buffer,
  783. p->bits_per_subpacket / 8);
  784. init_get_bits(&q->gb, q->decoded_bytes_buffer + offset,
  785. p->bits_per_subpacket);
  786. decode_gain_info(&q->gb, gains_ptr->now);
  787. /* Swap current and previous gains */
  788. FFSWAP(int *, gains_ptr->now, gains_ptr->previous);
  789. }
  790. /**
  791. * Saturate the output signal and interleave.
  792. *
  793. * @param q pointer to the COOKContext
  794. * @param chan channel to saturate
  795. * @param out pointer to the output vector
  796. */
  797. static void saturate_output_float(COOKContext *q, int chan, float *out)
  798. {
  799. int j;
  800. float *output = q->mono_mdct_output + q->samples_per_channel;
  801. for (j = 0; j < q->samples_per_channel; j++) {
  802. out[chan + q->nb_channels * j] = av_clipf(output[j], -1.0, 1.0);
  803. }
  804. }
  805. /**
  806. * Final part of subpacket decoding:
  807. * Apply modulated lapped transform, gain compensation,
  808. * clip and convert to integer.
  809. *
  810. * @param q pointer to the COOKContext
  811. * @param decode_buffer pointer to the mlt coefficients
  812. * @param gains_ptr array of current/prev gain pointers
  813. * @param previous_buffer pointer to the previous buffer to be used for overlapping
  814. * @param out pointer to the output buffer
  815. * @param chan 0: left or single channel, 1: right channel
  816. */
  817. static inline void mlt_compensate_output(COOKContext *q, float *decode_buffer,
  818. cook_gains *gains_ptr, float *previous_buffer,
  819. float *out, int chan)
  820. {
  821. imlt_gain(q, decode_buffer, gains_ptr, previous_buffer);
  822. if (out)
  823. q->saturate_output(q, chan, out);
  824. }
  825. /**
  826. * Cook subpacket decoding. This function returns one decoded subpacket,
  827. * usually 1024 samples per channel.
  828. *
  829. * @param q pointer to the COOKContext
  830. * @param inbuffer pointer to the inbuffer
  831. * @param outbuffer pointer to the outbuffer
  832. */
  833. static int decode_subpacket(COOKContext *q, COOKSubpacket *p,
  834. const uint8_t *inbuffer, float *outbuffer)
  835. {
  836. int sub_packet_size = p->size;
  837. int res;
  838. /* packet dump */
  839. // for (i = 0; i < sub_packet_size ; i++)
  840. // av_log(q->avctx, AV_LOG_ERROR, "%02x", inbuffer[i]);
  841. // av_log(q->avctx, AV_LOG_ERROR, "\n");
  842. memset(q->decode_buffer_1, 0, sizeof(q->decode_buffer_1));
  843. decode_bytes_and_gain(q, p, inbuffer, &p->gains1);
  844. if (p->joint_stereo) {
  845. if ((res = joint_decode(q, p, q->decode_buffer_1, q->decode_buffer_2)) < 0)
  846. return res;
  847. } else {
  848. if ((res = mono_decode(q, p, q->decode_buffer_1)) < 0)
  849. return res;
  850. if (p->num_channels == 2) {
  851. decode_bytes_and_gain(q, p, inbuffer + sub_packet_size / 2, &p->gains2);
  852. if ((res = mono_decode(q, p, q->decode_buffer_2)) < 0)
  853. return res;
  854. }
  855. }
  856. mlt_compensate_output(q, q->decode_buffer_1, &p->gains1,
  857. p->mono_previous_buffer1, outbuffer, p->ch_idx);
  858. if (p->num_channels == 2)
  859. if (p->joint_stereo)
  860. mlt_compensate_output(q, q->decode_buffer_2, &p->gains1,
  861. p->mono_previous_buffer2, outbuffer, p->ch_idx + 1);
  862. else
  863. mlt_compensate_output(q, q->decode_buffer_2, &p->gains2,
  864. p->mono_previous_buffer2, outbuffer, p->ch_idx + 1);
  865. return 0;
  866. }
  867. /**
  868. * Cook frame decoding
  869. *
  870. * @param avctx pointer to the AVCodecContext
  871. */
  872. static int cook_decode_frame(AVCodecContext *avctx, void *data,
  873. int *got_frame_ptr, AVPacket *avpkt)
  874. {
  875. const uint8_t *buf = avpkt->data;
  876. int buf_size = avpkt->size;
  877. COOKContext *q = avctx->priv_data;
  878. float *samples = NULL;
  879. int i, ret;
  880. int offset = 0;
  881. int chidx = 0;
  882. if (buf_size < avctx->block_align)
  883. return buf_size;
  884. /* get output buffer */
  885. if (q->discarded_packets >= 2) {
  886. q->frame.nb_samples = q->samples_per_channel;
  887. if ((ret = avctx->get_buffer(avctx, &q->frame)) < 0) {
  888. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  889. return ret;
  890. }
  891. samples = (float *) q->frame.data[0];
  892. }
  893. /* estimate subpacket sizes */
  894. q->subpacket[0].size = avctx->block_align;
  895. for (i = 1; i < q->num_subpackets; i++) {
  896. q->subpacket[i].size = 2 * buf[avctx->block_align - q->num_subpackets + i];
  897. q->subpacket[0].size -= q->subpacket[i].size + 1;
  898. if (q->subpacket[0].size < 0) {
  899. av_log(avctx, AV_LOG_DEBUG,
  900. "frame subpacket size total > avctx->block_align!\n");
  901. return AVERROR_INVALIDDATA;
  902. }
  903. }
  904. /* decode supbackets */
  905. for (i = 0; i < q->num_subpackets; i++) {
  906. q->subpacket[i].bits_per_subpacket = (q->subpacket[i].size * 8) >>
  907. q->subpacket[i].bits_per_subpdiv;
  908. q->subpacket[i].ch_idx = chidx;
  909. av_log(avctx, AV_LOG_DEBUG,
  910. "subpacket[%i] size %i js %i %i block_align %i\n",
  911. i, q->subpacket[i].size, q->subpacket[i].joint_stereo, offset,
  912. avctx->block_align);
  913. if ((ret = decode_subpacket(q, &q->subpacket[i], buf + offset, samples)) < 0)
  914. return ret;
  915. offset += q->subpacket[i].size;
  916. chidx += q->subpacket[i].num_channels;
  917. av_log(avctx, AV_LOG_DEBUG, "subpacket[%i] %i %i\n",
  918. i, q->subpacket[i].size * 8, get_bits_count(&q->gb));
  919. }
  920. /* Discard the first two frames: no valid audio. */
  921. if (q->discarded_packets < 2) {
  922. q->discarded_packets++;
  923. *got_frame_ptr = 0;
  924. return avctx->block_align;
  925. }
  926. *got_frame_ptr = 1;
  927. *(AVFrame *) data = q->frame;
  928. return avctx->block_align;
  929. }
  930. #ifdef DEBUG
  931. static void dump_cook_context(COOKContext *q)
  932. {
  933. //int i=0;
  934. #define PRINT(a, b) av_log(q->avctx, AV_LOG_ERROR, " %s = %d\n", a, b);
  935. av_log(q->avctx, AV_LOG_ERROR, "COOKextradata\n");
  936. av_log(q->avctx, AV_LOG_ERROR, "cookversion=%x\n", q->subpacket[0].cookversion);
  937. if (q->subpacket[0].cookversion > STEREO) {
  938. PRINT("js_subband_start", q->subpacket[0].js_subband_start);
  939. PRINT("js_vlc_bits", q->subpacket[0].js_vlc_bits);
  940. }
  941. av_log(q->avctx, AV_LOG_ERROR, "COOKContext\n");
  942. PRINT("nb_channels", q->nb_channels);
  943. PRINT("bit_rate", q->bit_rate);
  944. PRINT("sample_rate", q->sample_rate);
  945. PRINT("samples_per_channel", q->subpacket[0].samples_per_channel);
  946. PRINT("samples_per_frame", q->subpacket[0].samples_per_frame);
  947. PRINT("subbands", q->subpacket[0].subbands);
  948. PRINT("js_subband_start", q->subpacket[0].js_subband_start);
  949. PRINT("log2_numvector_size", q->subpacket[0].log2_numvector_size);
  950. PRINT("numvector_size", q->subpacket[0].numvector_size);
  951. PRINT("total_subbands", q->subpacket[0].total_subbands);
  952. }
  953. #endif
  954. static av_cold int cook_count_channels(unsigned int mask)
  955. {
  956. int i;
  957. int channels = 0;
  958. for (i = 0; i < 32; i++)
  959. if (mask & (1 << i))
  960. ++channels;
  961. return channels;
  962. }
  963. /**
  964. * Cook initialization
  965. *
  966. * @param avctx pointer to the AVCodecContext
  967. */
  968. static av_cold int cook_decode_init(AVCodecContext *avctx)
  969. {
  970. COOKContext *q = avctx->priv_data;
  971. const uint8_t *edata_ptr = avctx->extradata;
  972. const uint8_t *edata_ptr_end = edata_ptr + avctx->extradata_size;
  973. int extradata_size = avctx->extradata_size;
  974. int s = 0;
  975. unsigned int channel_mask = 0;
  976. int ret;
  977. q->avctx = avctx;
  978. /* Take care of the codec specific extradata. */
  979. if (extradata_size <= 0) {
  980. av_log(avctx, AV_LOG_ERROR, "Necessary extradata missing!\n");
  981. return AVERROR_INVALIDDATA;
  982. }
  983. av_log(avctx, AV_LOG_DEBUG, "codecdata_length=%d\n", avctx->extradata_size);
  984. /* Take data from the AVCodecContext (RM container). */
  985. q->sample_rate = avctx->sample_rate;
  986. q->nb_channels = avctx->channels;
  987. q->bit_rate = avctx->bit_rate;
  988. if (!q->nb_channels) {
  989. av_log(avctx, AV_LOG_ERROR, "Invalid number of channels\n");
  990. return AVERROR_INVALIDDATA;
  991. }
  992. /* Initialize RNG. */
  993. av_lfg_init(&q->random_state, 0);
  994. while (edata_ptr < edata_ptr_end) {
  995. /* 8 for mono, 16 for stereo, ? for multichannel
  996. Swap to right endianness so we don't need to care later on. */
  997. if (extradata_size >= 8) {
  998. q->subpacket[s].cookversion = bytestream_get_be32(&edata_ptr);
  999. q->subpacket[s].samples_per_frame = bytestream_get_be16(&edata_ptr);
  1000. q->subpacket[s].subbands = bytestream_get_be16(&edata_ptr);
  1001. extradata_size -= 8;
  1002. }
  1003. if (extradata_size >= 8) {
  1004. bytestream_get_be32(&edata_ptr); // Unknown unused
  1005. q->subpacket[s].js_subband_start = bytestream_get_be16(&edata_ptr);
  1006. q->subpacket[s].js_vlc_bits = bytestream_get_be16(&edata_ptr);
  1007. extradata_size -= 8;
  1008. }
  1009. /* Initialize extradata related variables. */
  1010. q->subpacket[s].samples_per_channel = q->subpacket[s].samples_per_frame / q->nb_channels;
  1011. q->subpacket[s].bits_per_subpacket = avctx->block_align * 8;
  1012. /* Initialize default data states. */
  1013. q->subpacket[s].log2_numvector_size = 5;
  1014. q->subpacket[s].total_subbands = q->subpacket[s].subbands;
  1015. q->subpacket[s].num_channels = 1;
  1016. /* Initialize version-dependent variables */
  1017. av_log(avctx, AV_LOG_DEBUG, "subpacket[%i].cookversion=%x\n", s,
  1018. q->subpacket[s].cookversion);
  1019. q->subpacket[s].joint_stereo = 0;
  1020. switch (q->subpacket[s].cookversion) {
  1021. case MONO:
  1022. if (q->nb_channels != 1) {
  1023. av_log_ask_for_sample(avctx, "Container channels != 1.\n");
  1024. return AVERROR_PATCHWELCOME;
  1025. }
  1026. av_log(avctx, AV_LOG_DEBUG, "MONO\n");
  1027. break;
  1028. case STEREO:
  1029. if (q->nb_channels != 1) {
  1030. q->subpacket[s].bits_per_subpdiv = 1;
  1031. q->subpacket[s].num_channels = 2;
  1032. }
  1033. av_log(avctx, AV_LOG_DEBUG, "STEREO\n");
  1034. break;
  1035. case JOINT_STEREO:
  1036. if (q->nb_channels != 2) {
  1037. av_log_ask_for_sample(avctx, "Container channels != 2.\n");
  1038. return AVERROR_PATCHWELCOME;
  1039. }
  1040. av_log(avctx, AV_LOG_DEBUG, "JOINT_STEREO\n");
  1041. if (avctx->extradata_size >= 16) {
  1042. q->subpacket[s].total_subbands = q->subpacket[s].subbands +
  1043. q->subpacket[s].js_subband_start;
  1044. q->subpacket[s].joint_stereo = 1;
  1045. q->subpacket[s].num_channels = 2;
  1046. }
  1047. if (q->subpacket[s].samples_per_channel > 256) {
  1048. q->subpacket[s].log2_numvector_size = 6;
  1049. }
  1050. if (q->subpacket[s].samples_per_channel > 512) {
  1051. q->subpacket[s].log2_numvector_size = 7;
  1052. }
  1053. break;
  1054. case MC_COOK:
  1055. av_log(avctx, AV_LOG_DEBUG, "MULTI_CHANNEL\n");
  1056. if (extradata_size >= 4)
  1057. channel_mask |= q->subpacket[s].channel_mask = bytestream_get_be32(&edata_ptr);
  1058. if (cook_count_channels(q->subpacket[s].channel_mask) > 1) {
  1059. q->subpacket[s].total_subbands = q->subpacket[s].subbands +
  1060. q->subpacket[s].js_subband_start;
  1061. q->subpacket[s].joint_stereo = 1;
  1062. q->subpacket[s].num_channels = 2;
  1063. q->subpacket[s].samples_per_channel = q->subpacket[s].samples_per_frame >> 1;
  1064. if (q->subpacket[s].samples_per_channel > 256) {
  1065. q->subpacket[s].log2_numvector_size = 6;
  1066. }
  1067. if (q->subpacket[s].samples_per_channel > 512) {
  1068. q->subpacket[s].log2_numvector_size = 7;
  1069. }
  1070. } else
  1071. q->subpacket[s].samples_per_channel = q->subpacket[s].samples_per_frame;
  1072. break;
  1073. default:
  1074. av_log_ask_for_sample(avctx, "Unknown Cook version.\n");
  1075. return AVERROR_PATCHWELCOME;
  1076. }
  1077. if (s > 1 && q->subpacket[s].samples_per_channel != q->samples_per_channel) {
  1078. av_log(avctx, AV_LOG_ERROR, "different number of samples per channel!\n");
  1079. return AVERROR_INVALIDDATA;
  1080. } else
  1081. q->samples_per_channel = q->subpacket[0].samples_per_channel;
  1082. /* Initialize variable relations */
  1083. q->subpacket[s].numvector_size = (1 << q->subpacket[s].log2_numvector_size);
  1084. /* Try to catch some obviously faulty streams, othervise it might be exploitable */
  1085. if (q->subpacket[s].total_subbands > 53) {
  1086. av_log_ask_for_sample(avctx, "total_subbands > 53\n");
  1087. return AVERROR_PATCHWELCOME;
  1088. }
  1089. if ((q->subpacket[s].js_vlc_bits > 6) ||
  1090. (q->subpacket[s].js_vlc_bits < 2 * q->subpacket[s].joint_stereo)) {
  1091. av_log(avctx, AV_LOG_ERROR, "js_vlc_bits = %d, only >= %d and <= 6 allowed!\n",
  1092. q->subpacket[s].js_vlc_bits, 2 * q->subpacket[s].joint_stereo);
  1093. return AVERROR_INVALIDDATA;
  1094. }
  1095. if (q->subpacket[s].subbands > 50) {
  1096. av_log_ask_for_sample(avctx, "subbands > 50\n");
  1097. return AVERROR_PATCHWELCOME;
  1098. }
  1099. q->subpacket[s].gains1.now = q->subpacket[s].gain_1;
  1100. q->subpacket[s].gains1.previous = q->subpacket[s].gain_2;
  1101. q->subpacket[s].gains2.now = q->subpacket[s].gain_3;
  1102. q->subpacket[s].gains2.previous = q->subpacket[s].gain_4;
  1103. q->num_subpackets++;
  1104. s++;
  1105. if (s > MAX_SUBPACKETS) {
  1106. av_log_ask_for_sample(avctx, "Too many subpackets > 5\n");
  1107. return AVERROR_PATCHWELCOME;
  1108. }
  1109. }
  1110. /* Generate tables */
  1111. init_pow2table();
  1112. init_gain_table(q);
  1113. init_cplscales_table(q);
  1114. if ((ret = init_cook_vlc_tables(q)))
  1115. return ret;
  1116. if (avctx->block_align >= UINT_MAX / 2)
  1117. return AVERROR(EINVAL);
  1118. /* Pad the databuffer with:
  1119. DECODE_BYTES_PAD1 or DECODE_BYTES_PAD2 for decode_bytes(),
  1120. FF_INPUT_BUFFER_PADDING_SIZE, for the bitstreamreader. */
  1121. q->decoded_bytes_buffer =
  1122. av_mallocz(avctx->block_align
  1123. + DECODE_BYTES_PAD1(avctx->block_align)
  1124. + FF_INPUT_BUFFER_PADDING_SIZE);
  1125. if (q->decoded_bytes_buffer == NULL)
  1126. return AVERROR(ENOMEM);
  1127. /* Initialize transform. */
  1128. if ((ret = init_cook_mlt(q)))
  1129. return ret;
  1130. /* Initialize COOK signal arithmetic handling */
  1131. if (1) {
  1132. q->scalar_dequant = scalar_dequant_float;
  1133. q->decouple = decouple_float;
  1134. q->imlt_window = imlt_window_float;
  1135. q->interpolate = interpolate_float;
  1136. q->saturate_output = saturate_output_float;
  1137. }
  1138. /* Try to catch some obviously faulty streams, othervise it might be exploitable */
  1139. if ((q->samples_per_channel == 256) || (q->samples_per_channel == 512)
  1140. || (q->samples_per_channel == 1024)) {
  1141. } else {
  1142. av_log_ask_for_sample(avctx,
  1143. "unknown amount of samples_per_channel = %d\n",
  1144. q->samples_per_channel);
  1145. return AVERROR_PATCHWELCOME;
  1146. }
  1147. avctx->sample_fmt = AV_SAMPLE_FMT_FLT;
  1148. if (channel_mask)
  1149. avctx->channel_layout = channel_mask;
  1150. else
  1151. avctx->channel_layout = (avctx->channels == 2) ? AV_CH_LAYOUT_STEREO : AV_CH_LAYOUT_MONO;
  1152. avcodec_get_frame_defaults(&q->frame);
  1153. avctx->coded_frame = &q->frame;
  1154. #ifdef DEBUG
  1155. dump_cook_context(q);
  1156. #endif
  1157. return 0;
  1158. }
  1159. AVCodec ff_cook_decoder = {
  1160. .name = "cook",
  1161. .type = AVMEDIA_TYPE_AUDIO,
  1162. .id = CODEC_ID_COOK,
  1163. .priv_data_size = sizeof(COOKContext),
  1164. .init = cook_decode_init,
  1165. .close = cook_decode_close,
  1166. .decode = cook_decode_frame,
  1167. .capabilities = CODEC_CAP_DR1,
  1168. .long_name = NULL_IF_CONFIG_SMALL("COOK"),
  1169. };