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.

1312 lines
44KB

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