cook.c 38 KB

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