cook.c 42 KB

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