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.

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