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.

1281 lines
41KB

  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. /**
  24. * @file cook.c
  25. * Cook compatible decoder.
  26. * This decoder handles RealNetworks, RealAudio G2 data.
  27. * Cook is identified by the codec name cook in RM files.
  28. *
  29. * To use this decoder, a calling application must supply the extradata
  30. * bytes provided from the RM container; 8+ bytes for mono streams and
  31. * 16+ for stereo streams (maybe more).
  32. *
  33. * Codec technicalities (all this assume a buffer length of 1024):
  34. * Cook works with several different techniques to achieve its compression.
  35. * In the timedomain the buffer is divided into 8 pieces and quantized. If
  36. * two neighboring pieces have different quantization index a smooth
  37. * quantization curve is used to get a smooth overlap between the different
  38. * pieces.
  39. * To get to the transformdomain Cook uses a modulated lapped transform.
  40. * The transform domain has 50 subbands with 20 elements each. This
  41. * means only a maximum of 50*20=1000 coefficients are used out of the 1024
  42. * available.
  43. */
  44. #include <math.h>
  45. #include <stddef.h>
  46. #include <stdio.h>
  47. #include "avcodec.h"
  48. #include "bitstream.h"
  49. #include "dsputil.h"
  50. #include "common.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 COOKDEBUG
  59. typedef struct {
  60. int size;
  61. int qidx_table1[8];
  62. int qidx_table2[8];
  63. } COOKgain;
  64. typedef struct __attribute__((__packed__)){
  65. /* codec data start */
  66. uint32_t cookversion; //in network order, bigendian
  67. uint16_t samples_per_frame; //amount of samples per frame per channel, bigendian
  68. uint16_t subbands; //amount of bands used in the frequency domain, bigendian
  69. /* Mono extradata ends here. */
  70. uint32_t unused;
  71. uint16_t js_subband_start; //bigendian
  72. uint16_t js_vlc_bits; //bigendian
  73. /* Stereo extradata ends here. */
  74. } COOKextradata;
  75. typedef struct {
  76. GetBitContext gb;
  77. /* stream data */
  78. int nb_channels;
  79. int joint_stereo;
  80. int bit_rate;
  81. int sample_rate;
  82. int samples_per_channel;
  83. int samples_per_frame;
  84. int subbands;
  85. int log2_numvector_size;
  86. int numvector_size; //1 << log2_numvector_size;
  87. int js_subband_start;
  88. int total_subbands;
  89. int num_vectors;
  90. int bits_per_subpacket;
  91. /* states */
  92. int random_state;
  93. /* transform data */
  94. FFTContext fft_ctx;
  95. FFTSample mlt_tmp[1024] __attribute__((aligned(16))); /* temporary storage for imlt */
  96. float* mlt_window;
  97. float* mlt_precos;
  98. float* mlt_presin;
  99. float* mlt_postcos;
  100. int fft_size;
  101. int fft_order;
  102. int mlt_size; //modulated lapped transform size
  103. /* gain buffers */
  104. COOKgain *gain_ptr1[2];
  105. COOKgain *gain_ptr2[2];
  106. COOKgain gain_1;
  107. COOKgain gain_2;
  108. COOKgain gain_3;
  109. COOKgain gain_4;
  110. /* VLC data */
  111. int js_vlc_bits;
  112. VLC envelope_quant_index[13];
  113. VLC sqvh[7]; //scalar quantization
  114. VLC ccpl; //channel coupling
  115. /* generatable tables and related variables */
  116. int gain_size_factor;
  117. float gain_table[23];
  118. float pow2tab[127];
  119. float rootpow2tab[127];
  120. /* data buffers */
  121. uint8_t* decoded_bytes_buffer;
  122. float mono_mdct_output[2048] __attribute__((aligned(16)));
  123. float mono_previous_buffer1[1024];
  124. float mono_previous_buffer2[1024];
  125. float decode_buffer_1[1024];
  126. float decode_buffer_2[1024];
  127. } COOKContext;
  128. /* debug functions */
  129. #ifdef COOKDEBUG
  130. static void dump_float_table(float* table, int size, int delimiter) {
  131. int i=0;
  132. av_log(NULL,AV_LOG_ERROR,"\n[%d]: ",i);
  133. for (i=0 ; i<size ; i++) {
  134. av_log(NULL, AV_LOG_ERROR, "%5.1f, ", table[i]);
  135. if ((i+1)%delimiter == 0) av_log(NULL,AV_LOG_ERROR,"\n[%d]: ",i+1);
  136. }
  137. }
  138. static void dump_int_table(int* table, int size, int delimiter) {
  139. int i=0;
  140. av_log(NULL,AV_LOG_ERROR,"\n[%d]: ",i);
  141. for (i=0 ; i<size ; i++) {
  142. av_log(NULL, AV_LOG_ERROR, "%d, ", table[i]);
  143. if ((i+1)%delimiter == 0) av_log(NULL,AV_LOG_ERROR,"\n[%d]: ",i+1);
  144. }
  145. }
  146. static void dump_short_table(short* table, int size, int delimiter) {
  147. int i=0;
  148. av_log(NULL,AV_LOG_ERROR,"\n[%d]: ",i);
  149. for (i=0 ; i<size ; i++) {
  150. av_log(NULL, AV_LOG_ERROR, "%d, ", table[i]);
  151. if ((i+1)%delimiter == 0) av_log(NULL,AV_LOG_ERROR,"\n[%d]: ",i+1);
  152. }
  153. }
  154. #endif
  155. /*************** init functions ***************/
  156. /* table generator */
  157. static void init_pow2table(COOKContext *q){
  158. int i;
  159. q->pow2tab[63] = 1.0;
  160. for (i=1 ; i<64 ; i++){
  161. q->pow2tab[63+i]=(float)((uint64_t)1<<i);
  162. q->pow2tab[63-i]=1.0/(float)((uint64_t)1<<i);
  163. }
  164. }
  165. /* table generator */
  166. static void init_rootpow2table(COOKContext *q){
  167. int i;
  168. q->rootpow2tab[63] = 1.0;
  169. for (i=1 ; i<64 ; i++){
  170. q->rootpow2tab[63+i]=sqrt((float)((uint64_t)1<<i));
  171. q->rootpow2tab[63-i]=sqrt(1.0/(float)((uint64_t)1<<i));
  172. }
  173. }
  174. /* table generator */
  175. static void init_gain_table(COOKContext *q) {
  176. int i;
  177. q->gain_size_factor = q->samples_per_channel/8;
  178. for (i=0 ; i<23 ; i++) {
  179. q->gain_table[i] = pow((double)q->pow2tab[i+52] ,
  180. (1.0/(double)q->gain_size_factor));
  181. }
  182. }
  183. static int init_cook_vlc_tables(COOKContext *q) {
  184. int i, result;
  185. result = 0;
  186. for (i=0 ; i<13 ; i++) {
  187. result &= init_vlc (&q->envelope_quant_index[i], 9, 24,
  188. envelope_quant_index_huffbits[i], 1, 1,
  189. envelope_quant_index_huffcodes[i], 2, 2, 0);
  190. }
  191. av_log(NULL,AV_LOG_DEBUG,"sqvh VLC init\n");
  192. for (i=0 ; i<7 ; i++) {
  193. result &= init_vlc (&q->sqvh[i], vhvlcsize_tab[i], vhsize_tab[i],
  194. cvh_huffbits[i], 1, 1,
  195. cvh_huffcodes[i], 2, 2, 0);
  196. }
  197. if (q->nb_channels==2 && q->joint_stereo==1){
  198. result &= init_vlc (&q->ccpl, 6, (1<<q->js_vlc_bits)-1,
  199. ccpl_huffbits[q->js_vlc_bits-2], 1, 1,
  200. ccpl_huffcodes[q->js_vlc_bits-2], 2, 2, 0);
  201. av_log(NULL,AV_LOG_DEBUG,"Joint-stereo VLC used.\n");
  202. }
  203. av_log(NULL,AV_LOG_DEBUG,"VLC tables initialized.\n");
  204. return result;
  205. }
  206. static int init_cook_mlt(COOKContext *q) {
  207. int j;
  208. float alpha;
  209. /* Allocate the buffers, could be replaced with a static [512]
  210. array if needed. */
  211. q->mlt_size = q->samples_per_channel;
  212. q->mlt_window = av_malloc(sizeof(float)*q->mlt_size);
  213. q->mlt_precos = av_malloc(sizeof(float)*q->mlt_size/2);
  214. q->mlt_presin = av_malloc(sizeof(float)*q->mlt_size/2);
  215. q->mlt_postcos = av_malloc(sizeof(float)*q->mlt_size/2);
  216. /* Initialize the MLT window: simple sine window. */
  217. alpha = M_PI / (2.0 * (float)q->mlt_size);
  218. for(j=0 ; j<q->mlt_size ; j++) {
  219. q->mlt_window[j] = sin((j + 512.0/(float)q->mlt_size) * alpha);
  220. }
  221. /* pre/post twiddle factors */
  222. for (j=0 ; j<q->mlt_size/2 ; j++){
  223. q->mlt_precos[j] = cos( ((j+0.25)*M_PI)/q->mlt_size);
  224. q->mlt_presin[j] = sin( ((j+0.25)*M_PI)/q->mlt_size);
  225. q->mlt_postcos[j] = (float)sqrt(2.0/(float)q->mlt_size)*cos( ((float)j*M_PI) /q->mlt_size); //sqrt(2/MLT_size) = scalefactor
  226. }
  227. /* Initialize the FFT. */
  228. ff_fft_init(&q->fft_ctx, av_log2(q->mlt_size)-1, 0);
  229. av_log(NULL,AV_LOG_DEBUG,"FFT initialized, order = %d.\n",
  230. av_log2(q->samples_per_channel)-1);
  231. return (int)(q->mlt_window && q->mlt_precos && q->mlt_presin && q->mlt_postcos);
  232. }
  233. /*************** init functions end ***********/
  234. /**
  235. * Cook indata decoding, every 32 bits are XORed with 0x37c511f2.
  236. * Why? No idea, some checksum/error detection method maybe.
  237. *
  238. * Out buffer size: extra bytes are needed to cope with
  239. * padding/missalignment.
  240. * Subpackets passed to the decoder can contain two, consecutive
  241. * half-subpackets, of identical but arbitrary size.
  242. * 1234 1234 1234 1234 extraA extraB
  243. * Case 1: AAAA BBBB 0 0
  244. * Case 2: AAAA ABBB BB-- 3 3
  245. * Case 3: AAAA AABB BBBB 2 2
  246. * Case 4: AAAA AAAB BBBB BB-- 1 5
  247. *
  248. * Nice way to waste CPU cycles.
  249. *
  250. * @param inbuffer pointer to byte array of indata
  251. * @param out pointer to byte array of outdata
  252. * @param bytes number of bytes
  253. */
  254. #define DECODE_BYTES_PAD1(bytes) (3 - ((bytes)+3) % 4)
  255. #define DECODE_BYTES_PAD2(bytes) ((bytes) % 4 + DECODE_BYTES_PAD1(2 * (bytes)))
  256. static inline int decode_bytes(uint8_t* inbuffer, uint8_t* out, int bytes){
  257. int i, off;
  258. uint32_t c;
  259. uint32_t* buf;
  260. uint32_t* obuf = (uint32_t*) out;
  261. /* FIXME: 64 bit platforms would be able to do 64 bits at a time.
  262. * I'm too lazy though, should be something like
  263. * for(i=0 ; i<bitamount/64 ; i++)
  264. * (int64_t)out[i] = 0x37c511f237c511f2^be2me_64(int64_t)in[i]);
  265. * Buffer alignment needs to be checked. */
  266. off = (uint32_t)inbuffer % 4;
  267. buf = (uint32_t*) (inbuffer - off);
  268. c = be2me_32((0x37c511f2 >> (off*8)) | (0x37c511f2 << (32-(off*8))));
  269. bytes += 3 + off;
  270. for (i = 0; i < bytes/4; i++)
  271. obuf[i] = c ^ buf[i];
  272. return off;
  273. }
  274. /**
  275. * Cook uninit
  276. */
  277. static int cook_decode_close(AVCodecContext *avctx)
  278. {
  279. int i;
  280. COOKContext *q = avctx->priv_data;
  281. av_log(avctx,AV_LOG_DEBUG, "Deallocating memory.\n");
  282. /* Free allocated memory buffers. */
  283. av_free(q->mlt_window);
  284. av_free(q->mlt_precos);
  285. av_free(q->mlt_presin);
  286. av_free(q->mlt_postcos);
  287. av_free(q->decoded_bytes_buffer);
  288. /* Free the transform. */
  289. ff_fft_end(&q->fft_ctx);
  290. /* Free the VLC tables. */
  291. for (i=0 ; i<13 ; i++) {
  292. free_vlc(&q->envelope_quant_index[i]);
  293. }
  294. for (i=0 ; i<7 ; i++) {
  295. free_vlc(&q->sqvh[i]);
  296. }
  297. if(q->nb_channels==2 && q->joint_stereo==1 ){
  298. free_vlc(&q->ccpl);
  299. }
  300. av_log(NULL,AV_LOG_DEBUG,"Memory deallocated.\n");
  301. return 0;
  302. }
  303. /**
  304. * Fill the COOKgain structure for the timedomain quantization.
  305. *
  306. * @param q pointer to the COOKContext
  307. * @param gaininfo pointer to the COOKgain
  308. */
  309. static void decode_gain_info(GetBitContext *gb, COOKgain* gaininfo) {
  310. int i;
  311. while (get_bits1(gb)) {}
  312. gaininfo->size = get_bits_count(gb) - 1; //amount of elements*2 to update
  313. if (get_bits_count(gb) - 1 <= 0) return;
  314. for (i=0 ; i<gaininfo->size ; i++){
  315. gaininfo->qidx_table1[i] = get_bits(gb,3);
  316. if (get_bits1(gb)) {
  317. gaininfo->qidx_table2[i] = get_bits(gb,4) - 7; //convert to signed
  318. } else {
  319. gaininfo->qidx_table2[i] = -1;
  320. }
  321. }
  322. }
  323. /**
  324. * Create the quant index table needed for the envelope.
  325. *
  326. * @param q pointer to the COOKContext
  327. * @param quant_index_table pointer to the array
  328. */
  329. static void decode_envelope(COOKContext *q, int* quant_index_table) {
  330. int i,j, vlc_index;
  331. int bitbias;
  332. bitbias = get_bits_count(&q->gb);
  333. quant_index_table[0]= get_bits(&q->gb,6) - 6; //This is used later in categorize
  334. for (i=1 ; i < q->total_subbands ; i++){
  335. vlc_index=i;
  336. if (i >= q->js_subband_start * 2) {
  337. vlc_index-=q->js_subband_start;
  338. } else {
  339. vlc_index/=2;
  340. if(vlc_index < 1) vlc_index = 1;
  341. }
  342. if (vlc_index>13) vlc_index = 13; //the VLC tables >13 are identical to No. 13
  343. j = get_vlc2(&q->gb, q->envelope_quant_index[vlc_index-1].table,
  344. q->envelope_quant_index[vlc_index-1].bits,2);
  345. quant_index_table[i] = quant_index_table[i-1] + j - 12; //differential encoding
  346. }
  347. }
  348. /**
  349. * Create the quant value table.
  350. *
  351. * @param q pointer to the COOKContext
  352. * @param quant_value_table pointer to the array
  353. */
  354. static void inline dequant_envelope(COOKContext *q, int* quant_index_table,
  355. float* quant_value_table){
  356. int i;
  357. for(i=0 ; i < q->total_subbands ; i++){
  358. quant_value_table[i] = q->rootpow2tab[quant_index_table[i]+63];
  359. }
  360. }
  361. /**
  362. * Calculate the category and category_index vector.
  363. *
  364. * @param q pointer to the COOKContext
  365. * @param quant_index_table pointer to the array
  366. * @param category pointer to the category array
  367. * @param category_index pointer to the category_index array
  368. */
  369. static void categorize(COOKContext *q, int* quant_index_table,
  370. int* category, int* category_index){
  371. int exp_idx, bias, tmpbias, bits_left, num_bits, index, v, i, j;
  372. int exp_index2[102];
  373. int exp_index1[102];
  374. int tmp_categorize_array1[128];
  375. int tmp_categorize_array1_idx=0;
  376. int tmp_categorize_array2[128];
  377. int tmp_categorize_array2_idx=0;
  378. int category_index_size=0;
  379. bits_left = q->bits_per_subpacket - get_bits_count(&q->gb);
  380. if(bits_left > q->samples_per_channel) {
  381. bits_left = q->samples_per_channel +
  382. ((bits_left - q->samples_per_channel)*5)/8;
  383. //av_log(NULL, AV_LOG_ERROR, "bits_left = %d\n",bits_left);
  384. }
  385. memset(&exp_index1,0,102*sizeof(int));
  386. memset(&exp_index2,0,102*sizeof(int));
  387. memset(&tmp_categorize_array1,0,128*sizeof(int));
  388. memset(&tmp_categorize_array2,0,128*sizeof(int));
  389. bias=-32;
  390. /* Estimate bias. */
  391. for (i=32 ; i>0 ; i=i/2){
  392. num_bits = 0;
  393. index = 0;
  394. for (j=q->total_subbands ; j>0 ; j--){
  395. exp_idx = (i - quant_index_table[index] + bias) / 2;
  396. if (exp_idx<0){
  397. exp_idx=0;
  398. } else if(exp_idx >7) {
  399. exp_idx=7;
  400. }
  401. index++;
  402. num_bits+=expbits_tab[exp_idx];
  403. }
  404. if(num_bits >= bits_left - 32){
  405. bias+=i;
  406. }
  407. }
  408. /* Calculate total number of bits. */
  409. num_bits=0;
  410. for (i=0 ; i<q->total_subbands ; i++) {
  411. exp_idx = (bias - quant_index_table[i]) / 2;
  412. if (exp_idx<0) {
  413. exp_idx=0;
  414. } else if(exp_idx >7) {
  415. exp_idx=7;
  416. }
  417. num_bits += expbits_tab[exp_idx];
  418. exp_index1[i] = exp_idx;
  419. exp_index2[i] = exp_idx;
  420. }
  421. tmpbias = bias = num_bits;
  422. for (j = 1 ; j < q->numvector_size ; j++) {
  423. if (tmpbias + bias > 2*bits_left) { /* ---> */
  424. int max = -999999;
  425. index=-1;
  426. for (i=0 ; i<q->total_subbands ; i++){
  427. if (exp_index1[i] < 7) {
  428. v = (-2*exp_index1[i]) - quant_index_table[i] - 32;
  429. if ( v >= max) {
  430. max = v;
  431. index = i;
  432. }
  433. }
  434. }
  435. if(index==-1)break;
  436. tmp_categorize_array1[tmp_categorize_array1_idx++] = index;
  437. tmpbias -= expbits_tab[exp_index1[index]] -
  438. expbits_tab[exp_index1[index]+1];
  439. ++exp_index1[index];
  440. } else { /* <--- */
  441. int min = 999999;
  442. index=-1;
  443. for (i=0 ; i<q->total_subbands ; i++){
  444. if(exp_index2[i] > 0){
  445. v = (-2*exp_index2[i])-quant_index_table[i];
  446. if ( v < min) {
  447. min = v;
  448. index = i;
  449. }
  450. }
  451. }
  452. if(index == -1)break;
  453. tmp_categorize_array2[tmp_categorize_array2_idx++] = index;
  454. tmpbias -= expbits_tab[exp_index2[index]] -
  455. expbits_tab[exp_index2[index]-1];
  456. --exp_index2[index];
  457. }
  458. }
  459. for(i=0 ; i<q->total_subbands ; i++)
  460. category[i] = exp_index2[i];
  461. /* Concatenate the two arrays. */
  462. for(i=tmp_categorize_array2_idx-1 ; i >= 0; i--)
  463. category_index[category_index_size++] = tmp_categorize_array2[i];
  464. for(i=0;i<tmp_categorize_array1_idx;i++)
  465. category_index[category_index_size++ ] = tmp_categorize_array1[i];
  466. /* FIXME: mc_sich_ra8_20.rm triggers this, not sure with what we
  467. should fill the remaining bytes. */
  468. for(i=category_index_size;i<q->numvector_size;i++)
  469. category_index[i]=0;
  470. }
  471. /**
  472. * Expand the category vector.
  473. *
  474. * @param q pointer to the COOKContext
  475. * @param category pointer to the category array
  476. * @param category_index pointer to the category_index array
  477. */
  478. static void inline expand_category(COOKContext *q, int* category,
  479. int* category_index){
  480. int i;
  481. for(i=0 ; i<q->num_vectors ; i++){
  482. ++category[category_index[i]];
  483. }
  484. }
  485. /**
  486. * The real requantization of the mltcoefs
  487. *
  488. * @param q pointer to the COOKContext
  489. * @param index index
  490. * @param band current subband
  491. * @param quant_value_table pointer to the array
  492. * @param subband_coef_index array of indexes to quant_centroid_tab
  493. * @param subband_coef_noise use random noise instead of predetermined value
  494. * @param mlt_buffer pointer to the mlt buffer
  495. */
  496. static void scalar_dequant(COOKContext *q, int index, int band,
  497. float* quant_value_table, int* subband_coef_index,
  498. int* subband_coef_noise, float* mlt_buffer){
  499. int i;
  500. float f1;
  501. for(i=0 ; i<SUBBAND_SIZE ; i++) {
  502. if (subband_coef_index[i]) {
  503. if (subband_coef_noise[i]) {
  504. f1 = -quant_centroid_tab[index][subband_coef_index[i]];
  505. } else {
  506. f1 = quant_centroid_tab[index][subband_coef_index[i]];
  507. }
  508. } else {
  509. /* noise coding if subband_coef_noise[i] == 0 */
  510. q->random_state = q->random_state * 214013 + 2531011; //typical RNG numbers
  511. f1 = randsign[(q->random_state/0x1000000)&1] * dither_tab[index]; //>>31
  512. }
  513. mlt_buffer[band*20+ i] = f1 * quant_value_table[band];
  514. }
  515. }
  516. /**
  517. * Unpack the subband_coef_index and subband_coef_noise vectors.
  518. *
  519. * @param q pointer to the COOKContext
  520. * @param category pointer to the category array
  521. * @param subband_coef_index array of indexes to quant_centroid_tab
  522. * @param subband_coef_noise use random noise instead of predetermined value
  523. */
  524. static int unpack_SQVH(COOKContext *q, int category, int* subband_coef_index,
  525. int* subband_coef_noise) {
  526. int i,j;
  527. int vlc, vd ,tmp, result;
  528. int ub;
  529. int cb;
  530. vd = vd_tab[category];
  531. result = 0;
  532. for(i=0 ; i<vpr_tab[category] ; i++){
  533. ub = get_bits_count(&q->gb);
  534. vlc = get_vlc2(&q->gb, q->sqvh[category].table, q->sqvh[category].bits, 3);
  535. cb = get_bits_count(&q->gb);
  536. if (q->bits_per_subpacket < get_bits_count(&q->gb)){
  537. vlc = 0;
  538. result = 1;
  539. }
  540. for(j=vd-1 ; j>=0 ; j--){
  541. tmp = (vlc * invradix_tab[category])/0x100000;
  542. subband_coef_index[vd*i+j] = vlc - tmp * (kmax_tab[category]+1);
  543. vlc = tmp;
  544. }
  545. for(j=0 ; j<vd ; j++){
  546. if (subband_coef_index[i*vd + j]) {
  547. if(get_bits_count(&q->gb) < q->bits_per_subpacket){
  548. subband_coef_noise[i*vd+j] = get_bits1(&q->gb);
  549. } else {
  550. result=1;
  551. subband_coef_noise[i*vd+j]=0;
  552. }
  553. } else {
  554. subband_coef_noise[i*vd+j]=0;
  555. }
  556. }
  557. }
  558. return result;
  559. }
  560. /**
  561. * Fill the mlt_buffer with mlt coefficients.
  562. *
  563. * @param q pointer to the COOKContext
  564. * @param category pointer to the category array
  565. * @param quant_value_table pointer to the array
  566. * @param mlt_buffer pointer to mlt coefficients
  567. */
  568. static void decode_vectors(COOKContext* q, int* category,
  569. float* quant_value_table, float* mlt_buffer){
  570. /* A zero in this table means that the subband coefficient is
  571. random noise coded. */
  572. int subband_coef_noise[SUBBAND_SIZE];
  573. /* A zero in this table means that the subband coefficient is a
  574. positive multiplicator. */
  575. int subband_coef_index[SUBBAND_SIZE];
  576. int band, j;
  577. int index=0;
  578. for(band=0 ; band<q->total_subbands ; band++){
  579. index = category[band];
  580. if(category[band] < 7){
  581. if(unpack_SQVH(q, category[band], subband_coef_index, subband_coef_noise)){
  582. index=7;
  583. for(j=0 ; j<q->total_subbands ; j++) category[band+j]=7;
  584. }
  585. }
  586. if(index==7) {
  587. memset(subband_coef_index, 0, sizeof(subband_coef_index));
  588. memset(subband_coef_noise, 0, sizeof(subband_coef_noise));
  589. }
  590. scalar_dequant(q, index, band, quant_value_table, subband_coef_index,
  591. subband_coef_noise, mlt_buffer);
  592. }
  593. if(q->total_subbands*SUBBAND_SIZE >= q->samples_per_channel){
  594. return;
  595. }
  596. }
  597. /**
  598. * function for decoding mono data
  599. *
  600. * @param q pointer to the COOKContext
  601. * @param mlt_buffer1 pointer to left channel mlt coefficients
  602. * @param mlt_buffer2 pointer to right channel mlt coefficients
  603. */
  604. static void mono_decode(COOKContext *q, float* mlt_buffer) {
  605. int category_index[128];
  606. float quant_value_table[102];
  607. int quant_index_table[102];
  608. int category[128];
  609. memset(&category, 0, 128*sizeof(int));
  610. memset(&quant_value_table, 0, 102*sizeof(int));
  611. memset(&category_index, 0, 128*sizeof(int));
  612. decode_envelope(q, quant_index_table);
  613. q->num_vectors = get_bits(&q->gb,q->log2_numvector_size);
  614. dequant_envelope(q, quant_index_table, quant_value_table);
  615. categorize(q, quant_index_table, category, category_index);
  616. expand_category(q, category, category_index);
  617. decode_vectors(q, category, quant_value_table, mlt_buffer);
  618. }
  619. /**
  620. * The modulated lapped transform, this takes transform coefficients
  621. * and transforms them into timedomain samples. This is done through
  622. * an FFT-based algorithm with pre- and postrotation steps.
  623. * A window and reorder step is also included.
  624. *
  625. * @param q pointer to the COOKContext
  626. * @param inbuffer pointer to the mltcoefficients
  627. * @param outbuffer pointer to the timedomain buffer
  628. * @param mlt_tmp pointer to temporary storage space
  629. */
  630. static void cook_imlt(COOKContext *q, float* inbuffer, float* outbuffer,
  631. float* mlt_tmp){
  632. int i;
  633. /* prerotation */
  634. for(i=0 ; i<q->mlt_size ; i+=2){
  635. outbuffer[i] = (q->mlt_presin[i/2] * inbuffer[q->mlt_size-1-i]) +
  636. (q->mlt_precos[i/2] * inbuffer[i]);
  637. outbuffer[i+1] = (q->mlt_precos[i/2] * inbuffer[q->mlt_size-1-i]) -
  638. (q->mlt_presin[i/2] * inbuffer[i]);
  639. }
  640. /* FFT */
  641. ff_fft_permute(&q->fft_ctx, (FFTComplex *) outbuffer);
  642. ff_fft_calc (&q->fft_ctx, (FFTComplex *) outbuffer);
  643. /* postrotation */
  644. for(i=0 ; i<q->mlt_size ; i+=2){
  645. mlt_tmp[i] = (q->mlt_postcos[(q->mlt_size-1-i)/2] * outbuffer[i+1]) +
  646. (q->mlt_postcos[i/2] * outbuffer[i]);
  647. mlt_tmp[q->mlt_size-1-i] = (q->mlt_postcos[(q->mlt_size-1-i)/2] * outbuffer[i]) -
  648. (q->mlt_postcos[i/2] * outbuffer[i+1]);
  649. }
  650. /* window and reorder */
  651. for(i=0 ; i<q->mlt_size/2 ; i++){
  652. outbuffer[i] = mlt_tmp[q->mlt_size/2-1-i] * q->mlt_window[i];
  653. outbuffer[q->mlt_size-1-i]= mlt_tmp[q->mlt_size/2-1-i] *
  654. q->mlt_window[q->mlt_size-1-i];
  655. outbuffer[q->mlt_size+i]= mlt_tmp[q->mlt_size/2+i] *
  656. q->mlt_window[q->mlt_size-1-i];
  657. outbuffer[2*q->mlt_size-1-i]= -(mlt_tmp[q->mlt_size/2+i] *
  658. q->mlt_window[i]);
  659. }
  660. }
  661. /**
  662. * the actual requantization of the timedomain samples
  663. *
  664. * @param q pointer to the COOKContext
  665. * @param buffer pointer to the timedomain buffer
  666. * @param gain_index index for the block multiplier
  667. * @param gain_index_next index for the next block multiplier
  668. */
  669. static void interpolate(COOKContext *q, float* buffer,
  670. int gain_index, int gain_index_next){
  671. int i;
  672. float fc1, fc2;
  673. fc1 = q->pow2tab[gain_index+63];
  674. if(gain_index == gain_index_next){ //static gain
  675. for(i=0 ; i<q->gain_size_factor ; i++){
  676. buffer[i]*=fc1;
  677. }
  678. return;
  679. } else { //smooth gain
  680. fc2 = q->gain_table[11 + (gain_index_next-gain_index)];
  681. for(i=0 ; i<q->gain_size_factor ; i++){
  682. buffer[i]*=fc1;
  683. fc1*=fc2;
  684. }
  685. return;
  686. }
  687. }
  688. /**
  689. * timedomain requantization of the timedomain samples
  690. *
  691. * @param q pointer to the COOKContext
  692. * @param buffer pointer to the timedomain buffer
  693. * @param gain_now current gain structure
  694. * @param gain_previous previous gain structure
  695. */
  696. static void gain_window(COOKContext *q, float* buffer, COOKgain* gain_now,
  697. COOKgain* gain_previous){
  698. int i, index;
  699. int gain_index[9];
  700. int tmp_gain_index;
  701. gain_index[8]=0;
  702. index = gain_previous->size;
  703. for (i=7 ; i>=0 ; i--) {
  704. if(index && gain_previous->qidx_table1[index-1]==i) {
  705. gain_index[i] = gain_previous->qidx_table2[index-1];
  706. index--;
  707. } else {
  708. gain_index[i]=gain_index[i+1];
  709. }
  710. }
  711. /* This is applied to the to be previous data buffer. */
  712. for(i=0;i<8;i++){
  713. interpolate(q, &buffer[q->samples_per_channel+q->gain_size_factor*i],
  714. gain_index[i], gain_index[i+1]);
  715. }
  716. tmp_gain_index = gain_index[0];
  717. index = gain_now->size;
  718. for (i=7 ; i>=0 ; i--) {
  719. if(index && gain_now->qidx_table1[index-1]==i) {
  720. gain_index[i]= gain_now->qidx_table2[index-1];
  721. index--;
  722. } else {
  723. gain_index[i]=gain_index[i+1];
  724. }
  725. }
  726. /* This is applied to the to be current block. */
  727. for(i=0;i<8;i++){
  728. interpolate(q, &buffer[i*q->gain_size_factor],
  729. tmp_gain_index+gain_index[i],
  730. tmp_gain_index+gain_index[i+1]);
  731. }
  732. }
  733. /**
  734. * mlt overlapping and buffer management
  735. *
  736. * @param q pointer to the COOKContext
  737. * @param buffer pointer to the timedomain buffer
  738. * @param gain_now current gain structure
  739. * @param gain_previous previous gain structure
  740. * @param previous_buffer pointer to the previous buffer to be used for overlapping
  741. *
  742. */
  743. static void gain_compensate(COOKContext *q, float* buffer, COOKgain* gain_now,
  744. COOKgain* gain_previous, float* previous_buffer) {
  745. int i;
  746. if((gain_now->size || gain_previous->size)) {
  747. gain_window(q, buffer, gain_now, gain_previous);
  748. }
  749. /* Overlap with the previous block. */
  750. for(i=0 ; i<q->samples_per_channel ; i++) buffer[i]+=previous_buffer[i];
  751. /* Save away the current to be previous block. */
  752. memcpy(previous_buffer, buffer+q->samples_per_channel,
  753. sizeof(float)*q->samples_per_channel);
  754. }
  755. /**
  756. * function for getting the jointstereo coupling information
  757. *
  758. * @param q pointer to the COOKContext
  759. * @param decouple_tab decoupling array
  760. *
  761. */
  762. static void decouple_info(COOKContext *q, int* decouple_tab){
  763. int length, i;
  764. if(get_bits1(&q->gb)) {
  765. if(cplband[q->js_subband_start] > cplband[q->subbands-1]) return;
  766. length = cplband[q->subbands-1] - cplband[q->js_subband_start] + 1;
  767. for (i=0 ; i<length ; i++) {
  768. decouple_tab[cplband[q->js_subband_start] + i] = get_vlc2(&q->gb, q->ccpl.table, q->ccpl.bits, 2);
  769. }
  770. return;
  771. }
  772. if(cplband[q->js_subband_start] > cplband[q->subbands-1]) return;
  773. length = cplband[q->subbands-1] - cplband[q->js_subband_start] + 1;
  774. for (i=0 ; i<length ; i++) {
  775. decouple_tab[cplband[q->js_subband_start] + i] = get_bits(&q->gb, q->js_vlc_bits);
  776. }
  777. return;
  778. }
  779. /**
  780. * function for decoding joint stereo data
  781. *
  782. * @param q pointer to the COOKContext
  783. * @param mlt_buffer1 pointer to left channel mlt coefficients
  784. * @param mlt_buffer2 pointer to right channel mlt coefficients
  785. */
  786. static void joint_decode(COOKContext *q, float* mlt_buffer1,
  787. float* mlt_buffer2) {
  788. int i,j;
  789. int decouple_tab[SUBBAND_SIZE];
  790. float decode_buffer[1060];
  791. int idx, cpl_tmp,tmp_idx;
  792. float f1,f2;
  793. float* cplscale;
  794. memset(decouple_tab, 0, sizeof(decouple_tab));
  795. memset(decode_buffer, 0, sizeof(decode_buffer));
  796. /* Make sure the buffers are zeroed out. */
  797. memset(mlt_buffer1,0, 1024*sizeof(float));
  798. memset(mlt_buffer2,0, 1024*sizeof(float));
  799. decouple_info(q, decouple_tab);
  800. mono_decode(q, decode_buffer);
  801. /* The two channels are stored interleaved in decode_buffer. */
  802. for (i=0 ; i<q->js_subband_start ; i++) {
  803. for (j=0 ; j<SUBBAND_SIZE ; j++) {
  804. mlt_buffer1[i*20+j] = decode_buffer[i*40+j];
  805. mlt_buffer2[i*20+j] = decode_buffer[i*40+20+j];
  806. }
  807. }
  808. /* When we reach js_subband_start (the higher frequencies)
  809. the coefficients are stored in a coupling scheme. */
  810. idx = (1 << q->js_vlc_bits) - 1;
  811. for (i=q->js_subband_start ; i<q->subbands ; i++) {
  812. cpl_tmp = cplband[i];
  813. idx -=decouple_tab[cpl_tmp];
  814. cplscale = (float*)cplscales[q->js_vlc_bits-2]; //choose decoupler table
  815. f1 = cplscale[decouple_tab[cpl_tmp]];
  816. f2 = cplscale[idx-1];
  817. for (j=0 ; j<SUBBAND_SIZE ; j++) {
  818. tmp_idx = ((q->js_subband_start + i)*20)+j;
  819. mlt_buffer1[20*i + j] = f1 * decode_buffer[tmp_idx];
  820. mlt_buffer2[20*i + j] = f2 * decode_buffer[tmp_idx];
  821. }
  822. idx = (1 << q->js_vlc_bits) - 1;
  823. }
  824. }
  825. /**
  826. * First part of subpacket decoding:
  827. * decode raw stream bytes and read gain info.
  828. *
  829. * @param q pointer to the COOKContext
  830. * @param inbuffer pointer to raw stream data
  831. * @param gain_ptr array of current/prev gain pointers
  832. */
  833. static inline void
  834. decode_bytes_and_gain(COOKContext *q, uint8_t *inbuffer,
  835. COOKgain *gain_ptr[])
  836. {
  837. int offset;
  838. offset = decode_bytes(inbuffer, q->decoded_bytes_buffer,
  839. q->bits_per_subpacket/8);
  840. init_get_bits(&q->gb, q->decoded_bytes_buffer + offset,
  841. q->bits_per_subpacket);
  842. decode_gain_info(&q->gb, gain_ptr[0]);
  843. /* Swap current and previous gains */
  844. FFSWAP(COOKgain *, gain_ptr[0], gain_ptr[1]);
  845. }
  846. /**
  847. * Final part of subpacket decoding:
  848. * Apply modulated lapped transform, gain compensation,
  849. * clip and convert to integer.
  850. *
  851. * @param q pointer to the COOKContext
  852. * @param decode_buffer pointer to the mlt coefficients
  853. * @param gain_ptr array of current/prev gain pointers
  854. * @param previous_buffer pointer to the previous buffer to be used for overlapping
  855. * @param out pointer to the output buffer
  856. * @param chan 0: left or single channel, 1: right channel
  857. */
  858. static inline void
  859. mlt_compensate_output(COOKContext *q, float *decode_buffer,
  860. COOKgain *gain_ptr[], float *previous_buffer,
  861. int16_t *out, int chan)
  862. {
  863. int j;
  864. cook_imlt(q, decode_buffer, q->mono_mdct_output, q->mlt_tmp);
  865. gain_compensate(q, q->mono_mdct_output, gain_ptr[0],
  866. gain_ptr[1], previous_buffer);
  867. /* Clip and convert floats to 16 bits.
  868. */
  869. for (j = 0; j < q->samples_per_channel; j++) {
  870. out[chan + q->nb_channels * j] =
  871. clip(lrintf(q->mono_mdct_output[j]), -32768, 32767);
  872. }
  873. }
  874. /**
  875. * Cook subpacket decoding. This function returns one decoded subpacket,
  876. * usually 1024 samples per channel.
  877. *
  878. * @param q pointer to the COOKContext
  879. * @param inbuffer pointer to the inbuffer
  880. * @param sub_packet_size subpacket size
  881. * @param outbuffer pointer to the outbuffer
  882. */
  883. static int decode_subpacket(COOKContext *q, uint8_t *inbuffer,
  884. int sub_packet_size, int16_t *outbuffer) {
  885. /* packet dump */
  886. // for (i=0 ; i<sub_packet_size ; i++) {
  887. // av_log(NULL, AV_LOG_ERROR, "%02x", inbuffer[i]);
  888. // }
  889. // av_log(NULL, AV_LOG_ERROR, "\n");
  890. decode_bytes_and_gain(q, inbuffer, q->gain_ptr1);
  891. if (q->joint_stereo) {
  892. joint_decode(q, q->decode_buffer_1, q->decode_buffer_2);
  893. } else {
  894. mono_decode(q, q->decode_buffer_1);
  895. if (q->nb_channels == 2) {
  896. decode_bytes_and_gain(q, inbuffer + sub_packet_size/2,
  897. q->gain_ptr2);
  898. mono_decode(q, q->decode_buffer_2);
  899. }
  900. }
  901. mlt_compensate_output(q, q->decode_buffer_1, q->gain_ptr1,
  902. q->mono_previous_buffer1, outbuffer, 0);
  903. if (q->nb_channels == 2) {
  904. if (q->joint_stereo) {
  905. mlt_compensate_output(q, q->decode_buffer_2, q->gain_ptr1,
  906. q->mono_previous_buffer2, outbuffer, 1);
  907. } else {
  908. mlt_compensate_output(q, q->decode_buffer_2, q->gain_ptr2,
  909. q->mono_previous_buffer2, outbuffer, 1);
  910. }
  911. }
  912. return q->samples_per_frame * sizeof(int16_t);
  913. }
  914. /**
  915. * Cook frame decoding
  916. *
  917. * @param avctx pointer to the AVCodecContext
  918. */
  919. static int cook_decode_frame(AVCodecContext *avctx,
  920. void *data, int *data_size,
  921. uint8_t *buf, int buf_size) {
  922. COOKContext *q = avctx->priv_data;
  923. if (buf_size < avctx->block_align)
  924. return buf_size;
  925. *data_size = decode_subpacket(q, buf, avctx->block_align, data);
  926. return avctx->block_align;
  927. }
  928. #ifdef COOKDEBUG
  929. static void dump_cook_context(COOKContext *q, COOKextradata *e)
  930. {
  931. //int i=0;
  932. #define PRINT(a,b) av_log(NULL,AV_LOG_ERROR," %s = %d\n", a, b);
  933. av_log(NULL,AV_LOG_ERROR,"COOKextradata\n");
  934. av_log(NULL,AV_LOG_ERROR,"cookversion=%x\n",e->cookversion);
  935. if (e->cookversion > STEREO) {
  936. PRINT("js_subband_start",e->js_subband_start);
  937. PRINT("js_vlc_bits",e->js_vlc_bits);
  938. }
  939. av_log(NULL,AV_LOG_ERROR,"COOKContext\n");
  940. PRINT("nb_channels",q->nb_channels);
  941. PRINT("bit_rate",q->bit_rate);
  942. PRINT("sample_rate",q->sample_rate);
  943. PRINT("samples_per_channel",q->samples_per_channel);
  944. PRINT("samples_per_frame",q->samples_per_frame);
  945. PRINT("subbands",q->subbands);
  946. PRINT("random_state",q->random_state);
  947. PRINT("mlt_size",q->mlt_size);
  948. PRINT("js_subband_start",q->js_subband_start);
  949. PRINT("log2_numvector_size",q->log2_numvector_size);
  950. PRINT("numvector_size",q->numvector_size);
  951. PRINT("total_subbands",q->total_subbands);
  952. }
  953. #endif
  954. /**
  955. * Cook initialization
  956. *
  957. * @param avctx pointer to the AVCodecContext
  958. */
  959. static int cook_decode_init(AVCodecContext *avctx)
  960. {
  961. COOKextradata *e = (COOKextradata *)avctx->extradata;
  962. COOKContext *q = avctx->priv_data;
  963. /* Take care of the codec specific extradata. */
  964. if (avctx->extradata_size <= 0) {
  965. av_log(avctx,AV_LOG_ERROR,"Necessary extradata missing!\n");
  966. return -1;
  967. } else {
  968. /* 8 for mono, 16 for stereo, ? for multichannel
  969. Swap to right endianness so we don't need to care later on. */
  970. av_log(avctx,AV_LOG_DEBUG,"codecdata_length=%d\n",avctx->extradata_size);
  971. if (avctx->extradata_size >= 8){
  972. e->cookversion = be2me_32(e->cookversion);
  973. e->samples_per_frame = be2me_16(e->samples_per_frame);
  974. e->subbands = be2me_16(e->subbands);
  975. }
  976. if (avctx->extradata_size >= 16){
  977. e->js_subband_start = be2me_16(e->js_subband_start);
  978. e->js_vlc_bits = be2me_16(e->js_vlc_bits);
  979. }
  980. }
  981. /* Take data from the AVCodecContext (RM container). */
  982. q->sample_rate = avctx->sample_rate;
  983. q->nb_channels = avctx->channels;
  984. q->bit_rate = avctx->bit_rate;
  985. /* Initialize state. */
  986. q->random_state = 1;
  987. /* Initialize extradata related variables. */
  988. q->samples_per_channel = e->samples_per_frame / q->nb_channels;
  989. q->samples_per_frame = e->samples_per_frame;
  990. q->subbands = e->subbands;
  991. q->bits_per_subpacket = avctx->block_align * 8;
  992. /* Initialize default data states. */
  993. q->js_subband_start = 0;
  994. q->log2_numvector_size = 5;
  995. q->total_subbands = q->subbands;
  996. /* Initialize version-dependent variables */
  997. av_log(NULL,AV_LOG_DEBUG,"e->cookversion=%x\n",e->cookversion);
  998. q->joint_stereo = 0;
  999. switch (e->cookversion) {
  1000. case MONO:
  1001. if (q->nb_channels != 1) {
  1002. av_log(avctx,AV_LOG_ERROR,"Container channels != 1, report sample!\n");
  1003. return -1;
  1004. }
  1005. av_log(avctx,AV_LOG_DEBUG,"MONO\n");
  1006. break;
  1007. case STEREO:
  1008. if (q->nb_channels != 1) {
  1009. q->bits_per_subpacket = q->bits_per_subpacket/2;
  1010. }
  1011. av_log(avctx,AV_LOG_DEBUG,"STEREO\n");
  1012. break;
  1013. case JOINT_STEREO:
  1014. if (q->nb_channels != 2) {
  1015. av_log(avctx,AV_LOG_ERROR,"Container channels != 2, report sample!\n");
  1016. return -1;
  1017. }
  1018. av_log(avctx,AV_LOG_DEBUG,"JOINT_STEREO\n");
  1019. if (avctx->extradata_size >= 16){
  1020. q->total_subbands = q->subbands + e->js_subband_start;
  1021. q->js_subband_start = e->js_subband_start;
  1022. q->joint_stereo = 1;
  1023. q->js_vlc_bits = e->js_vlc_bits;
  1024. }
  1025. if (q->samples_per_channel > 256) {
  1026. q->log2_numvector_size = 6;
  1027. }
  1028. if (q->samples_per_channel > 512) {
  1029. q->log2_numvector_size = 7;
  1030. }
  1031. break;
  1032. case MC_COOK:
  1033. av_log(avctx,AV_LOG_ERROR,"MC_COOK not supported!\n");
  1034. return -1;
  1035. break;
  1036. default:
  1037. av_log(avctx,AV_LOG_ERROR,"Unknown Cook version, report sample!\n");
  1038. return -1;
  1039. break;
  1040. }
  1041. /* Initialize variable relations */
  1042. q->mlt_size = q->samples_per_channel;
  1043. q->numvector_size = (1 << q->log2_numvector_size);
  1044. /* Generate tables */
  1045. init_rootpow2table(q);
  1046. init_pow2table(q);
  1047. init_gain_table(q);
  1048. if (init_cook_vlc_tables(q) != 0)
  1049. return -1;
  1050. if(avctx->block_align >= UINT_MAX/2)
  1051. return -1;
  1052. /* Pad the databuffer with:
  1053. DECODE_BYTES_PAD1 or DECODE_BYTES_PAD2 for decode_bytes(),
  1054. FF_INPUT_BUFFER_PADDING_SIZE, for the bitstreamreader. */
  1055. if (q->nb_channels==2 && q->joint_stereo==0) {
  1056. q->decoded_bytes_buffer =
  1057. av_mallocz(avctx->block_align/2
  1058. + DECODE_BYTES_PAD2(avctx->block_align/2)
  1059. + FF_INPUT_BUFFER_PADDING_SIZE);
  1060. } else {
  1061. q->decoded_bytes_buffer =
  1062. av_mallocz(avctx->block_align
  1063. + DECODE_BYTES_PAD1(avctx->block_align)
  1064. + FF_INPUT_BUFFER_PADDING_SIZE);
  1065. }
  1066. if (q->decoded_bytes_buffer == NULL)
  1067. return -1;
  1068. q->gain_ptr1[0] = &q->gain_1;
  1069. q->gain_ptr1[1] = &q->gain_2;
  1070. q->gain_ptr2[0] = &q->gain_3;
  1071. q->gain_ptr2[1] = &q->gain_4;
  1072. /* Initialize transform. */
  1073. if ( init_cook_mlt(q) == 0 )
  1074. return -1;
  1075. /* Try to catch some obviously faulty streams, othervise it might be exploitable */
  1076. if (q->total_subbands > 53) {
  1077. av_log(avctx,AV_LOG_ERROR,"total_subbands > 53, report sample!\n");
  1078. return -1;
  1079. }
  1080. if (q->subbands > 50) {
  1081. av_log(avctx,AV_LOG_ERROR,"subbands > 50, report sample!\n");
  1082. return -1;
  1083. }
  1084. if ((q->samples_per_channel == 256) || (q->samples_per_channel == 512) || (q->samples_per_channel == 1024)) {
  1085. } else {
  1086. av_log(avctx,AV_LOG_ERROR,"unknown amount of samples_per_channel = %d, report sample!\n",q->samples_per_channel);
  1087. return -1;
  1088. }
  1089. #ifdef COOKDEBUG
  1090. dump_cook_context(q,e);
  1091. #endif
  1092. return 0;
  1093. }
  1094. AVCodec cook_decoder =
  1095. {
  1096. .name = "cook",
  1097. .type = CODEC_TYPE_AUDIO,
  1098. .id = CODEC_ID_COOK,
  1099. .priv_data_size = sizeof(COOKContext),
  1100. .init = cook_decode_init,
  1101. .close = cook_decode_close,
  1102. .decode = cook_decode_frame,
  1103. };