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.

1284 lines
43KB

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