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.

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