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.

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