cook.c 43 KB

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