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.

1363 lines
45KB

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