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.

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