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.

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