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