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.

1293 lines
45KB

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