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.

1148 lines
36KB

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