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.

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