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.

1225 lines
39KB

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