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.

1313 lines
43KB

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