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.

1298 lines
43KB

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