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