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.

1170 lines
37KB

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