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.

1308 lines
43KB

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