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.

1294 lines
44KB

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