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.

1269 lines
42KB

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