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.

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