mpegaudiodec.c 78 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612
  1. /*
  2. * MPEG Audio decoder
  3. * Copyright (c) 2001, 2002 Fabrice Bellard
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. /**
  22. * @file libavcodec/mpegaudiodec.c
  23. * MPEG Audio decoder.
  24. */
  25. #include "avcodec.h"
  26. #include "bitstream.h"
  27. #include "dsputil.h"
  28. /*
  29. * TODO:
  30. * - in low precision mode, use more 16 bit multiplies in synth filter
  31. * - test lsf / mpeg25 extensively.
  32. */
  33. #include "mpegaudio.h"
  34. #include "mpegaudiodecheader.h"
  35. #include "mathops.h"
  36. /* WARNING: only correct for posititive numbers */
  37. #define FIXR(a) ((int)((a) * FRAC_ONE + 0.5))
  38. #define FRAC_RND(a) (((a) + (FRAC_ONE/2)) >> FRAC_BITS)
  39. #define FIXHR(a) ((int)((a) * (1LL<<32) + 0.5))
  40. /****************/
  41. #define HEADER_SIZE 4
  42. /* layer 3 "granule" */
  43. typedef struct GranuleDef {
  44. uint8_t scfsi;
  45. int part2_3_length;
  46. int big_values;
  47. int global_gain;
  48. int scalefac_compress;
  49. uint8_t block_type;
  50. uint8_t switch_point;
  51. int table_select[3];
  52. int subblock_gain[3];
  53. uint8_t scalefac_scale;
  54. uint8_t count1table_select;
  55. int region_size[3]; /* number of huffman codes in each region */
  56. int preflag;
  57. int short_start, long_end; /* long/short band indexes */
  58. uint8_t scale_factors[40];
  59. int32_t sb_hybrid[SBLIMIT * 18]; /* 576 samples */
  60. } GranuleDef;
  61. #include "mpegaudiodata.h"
  62. #include "mpegaudiodectab.h"
  63. static void compute_antialias_integer(MPADecodeContext *s, GranuleDef *g);
  64. static void compute_antialias_float(MPADecodeContext *s, GranuleDef *g);
  65. /* vlc structure for decoding layer 3 huffman tables */
  66. static VLC huff_vlc[16];
  67. static VLC_TYPE huff_vlc_tables[
  68. 0+128+128+128+130+128+154+166+
  69. 142+204+190+170+542+460+662+414
  70. ][2];
  71. static const int huff_vlc_tables_sizes[16] = {
  72. 0, 128, 128, 128, 130, 128, 154, 166,
  73. 142, 204, 190, 170, 542, 460, 662, 414
  74. };
  75. static VLC huff_quad_vlc[2];
  76. static VLC_TYPE huff_quad_vlc_tables[128+16][2];
  77. static const int huff_quad_vlc_tables_sizes[2] = {
  78. 128, 16
  79. };
  80. /* computed from band_size_long */
  81. static uint16_t band_index_long[9][23];
  82. /* XXX: free when all decoders are closed */
  83. #define TABLE_4_3_SIZE (8191 + 16)*4
  84. static int8_t table_4_3_exp[TABLE_4_3_SIZE];
  85. static uint32_t table_4_3_value[TABLE_4_3_SIZE];
  86. static uint32_t exp_table[512];
  87. static uint32_t expval_table[512][16];
  88. /* intensity stereo coef table */
  89. static int32_t is_table[2][16];
  90. static int32_t is_table_lsf[2][2][16];
  91. static int32_t csa_table[8][4];
  92. static float csa_table_float[8][4];
  93. static int32_t mdct_win[8][36];
  94. /* lower 2 bits: modulo 3, higher bits: shift */
  95. static uint16_t scale_factor_modshift[64];
  96. /* [i][j]: 2^(-j/3) * FRAC_ONE * 2^(i+2) / (2^(i+2) - 1) */
  97. static int32_t scale_factor_mult[15][3];
  98. /* mult table for layer 2 group quantization */
  99. #define SCALE_GEN(v) \
  100. { FIXR(1.0 * (v)), FIXR(0.7937005259 * (v)), FIXR(0.6299605249 * (v)) }
  101. static const int32_t scale_factor_mult2[3][3] = {
  102. SCALE_GEN(4.0 / 3.0), /* 3 steps */
  103. SCALE_GEN(4.0 / 5.0), /* 5 steps */
  104. SCALE_GEN(4.0 / 9.0), /* 9 steps */
  105. };
  106. static DECLARE_ALIGNED_16(MPA_INT, window[512]);
  107. /**
  108. * Convert region offsets to region sizes and truncate
  109. * size to big_values.
  110. */
  111. void ff_region_offset2size(GranuleDef *g){
  112. int i, k, j=0;
  113. g->region_size[2] = (576 / 2);
  114. for(i=0;i<3;i++) {
  115. k = FFMIN(g->region_size[i], g->big_values);
  116. g->region_size[i] = k - j;
  117. j = k;
  118. }
  119. }
  120. void ff_init_short_region(MPADecodeContext *s, GranuleDef *g){
  121. if (g->block_type == 2)
  122. g->region_size[0] = (36 / 2);
  123. else {
  124. if (s->sample_rate_index <= 2)
  125. g->region_size[0] = (36 / 2);
  126. else if (s->sample_rate_index != 8)
  127. g->region_size[0] = (54 / 2);
  128. else
  129. g->region_size[0] = (108 / 2);
  130. }
  131. g->region_size[1] = (576 / 2);
  132. }
  133. void ff_init_long_region(MPADecodeContext *s, GranuleDef *g, int ra1, int ra2){
  134. int l;
  135. g->region_size[0] =
  136. band_index_long[s->sample_rate_index][ra1 + 1] >> 1;
  137. /* should not overflow */
  138. l = FFMIN(ra1 + ra2 + 2, 22);
  139. g->region_size[1] =
  140. band_index_long[s->sample_rate_index][l] >> 1;
  141. }
  142. void ff_compute_band_indexes(MPADecodeContext *s, GranuleDef *g){
  143. if (g->block_type == 2) {
  144. if (g->switch_point) {
  145. /* if switched mode, we handle the 36 first samples as
  146. long blocks. For 8000Hz, we handle the 48 first
  147. exponents as long blocks (XXX: check this!) */
  148. if (s->sample_rate_index <= 2)
  149. g->long_end = 8;
  150. else if (s->sample_rate_index != 8)
  151. g->long_end = 6;
  152. else
  153. g->long_end = 4; /* 8000 Hz */
  154. g->short_start = 3;
  155. } else {
  156. g->long_end = 0;
  157. g->short_start = 0;
  158. }
  159. } else {
  160. g->short_start = 13;
  161. g->long_end = 22;
  162. }
  163. }
  164. /* layer 1 unscaling */
  165. /* n = number of bits of the mantissa minus 1 */
  166. static inline int l1_unscale(int n, int mant, int scale_factor)
  167. {
  168. int shift, mod;
  169. int64_t val;
  170. shift = scale_factor_modshift[scale_factor];
  171. mod = shift & 3;
  172. shift >>= 2;
  173. val = MUL64(mant + (-1 << n) + 1, scale_factor_mult[n-1][mod]);
  174. shift += n;
  175. /* NOTE: at this point, 1 <= shift >= 21 + 15 */
  176. return (int)((val + (1LL << (shift - 1))) >> shift);
  177. }
  178. static inline int l2_unscale_group(int steps, int mant, int scale_factor)
  179. {
  180. int shift, mod, val;
  181. shift = scale_factor_modshift[scale_factor];
  182. mod = shift & 3;
  183. shift >>= 2;
  184. val = (mant - (steps >> 1)) * scale_factor_mult2[steps >> 2][mod];
  185. /* NOTE: at this point, 0 <= shift <= 21 */
  186. if (shift > 0)
  187. val = (val + (1 << (shift - 1))) >> shift;
  188. return val;
  189. }
  190. /* compute value^(4/3) * 2^(exponent/4). It normalized to FRAC_BITS */
  191. static inline int l3_unscale(int value, int exponent)
  192. {
  193. unsigned int m;
  194. int e;
  195. e = table_4_3_exp [4*value + (exponent&3)];
  196. m = table_4_3_value[4*value + (exponent&3)];
  197. e -= (exponent >> 2);
  198. assert(e>=1);
  199. if (e > 31)
  200. return 0;
  201. m = (m + (1 << (e-1))) >> e;
  202. return m;
  203. }
  204. /* all integer n^(4/3) computation code */
  205. #define DEV_ORDER 13
  206. #define POW_FRAC_BITS 24
  207. #define POW_FRAC_ONE (1 << POW_FRAC_BITS)
  208. #define POW_FIX(a) ((int)((a) * POW_FRAC_ONE))
  209. #define POW_MULL(a,b) (((int64_t)(a) * (int64_t)(b)) >> POW_FRAC_BITS)
  210. static int dev_4_3_coefs[DEV_ORDER];
  211. #if 0 /* unused */
  212. static int pow_mult3[3] = {
  213. POW_FIX(1.0),
  214. POW_FIX(1.25992104989487316476),
  215. POW_FIX(1.58740105196819947474),
  216. };
  217. #endif
  218. static av_cold void int_pow_init(void)
  219. {
  220. int i, a;
  221. a = POW_FIX(1.0);
  222. for(i=0;i<DEV_ORDER;i++) {
  223. a = POW_MULL(a, POW_FIX(4.0 / 3.0) - i * POW_FIX(1.0)) / (i + 1);
  224. dev_4_3_coefs[i] = a;
  225. }
  226. }
  227. #if 0 /* unused, remove? */
  228. /* return the mantissa and the binary exponent */
  229. static int int_pow(int i, int *exp_ptr)
  230. {
  231. int e, er, eq, j;
  232. int a, a1;
  233. /* renormalize */
  234. a = i;
  235. e = POW_FRAC_BITS;
  236. while (a < (1 << (POW_FRAC_BITS - 1))) {
  237. a = a << 1;
  238. e--;
  239. }
  240. a -= (1 << POW_FRAC_BITS);
  241. a1 = 0;
  242. for(j = DEV_ORDER - 1; j >= 0; j--)
  243. a1 = POW_MULL(a, dev_4_3_coefs[j] + a1);
  244. a = (1 << POW_FRAC_BITS) + a1;
  245. /* exponent compute (exact) */
  246. e = e * 4;
  247. er = e % 3;
  248. eq = e / 3;
  249. a = POW_MULL(a, pow_mult3[er]);
  250. while (a >= 2 * POW_FRAC_ONE) {
  251. a = a >> 1;
  252. eq++;
  253. }
  254. /* convert to float */
  255. while (a < POW_FRAC_ONE) {
  256. a = a << 1;
  257. eq--;
  258. }
  259. /* now POW_FRAC_ONE <= a < 2 * POW_FRAC_ONE */
  260. #if POW_FRAC_BITS > FRAC_BITS
  261. a = (a + (1 << (POW_FRAC_BITS - FRAC_BITS - 1))) >> (POW_FRAC_BITS - FRAC_BITS);
  262. /* correct overflow */
  263. if (a >= 2 * (1 << FRAC_BITS)) {
  264. a = a >> 1;
  265. eq++;
  266. }
  267. #endif
  268. *exp_ptr = eq;
  269. return a;
  270. }
  271. #endif
  272. static av_cold int decode_init(AVCodecContext * avctx)
  273. {
  274. MPADecodeContext *s = avctx->priv_data;
  275. static int init=0;
  276. int i, j, k;
  277. s->avctx = avctx;
  278. avctx->sample_fmt= OUT_FMT;
  279. s->error_recognition= avctx->error_recognition;
  280. if(avctx->antialias_algo != FF_AA_FLOAT)
  281. s->compute_antialias= compute_antialias_integer;
  282. else
  283. s->compute_antialias= compute_antialias_float;
  284. if (!init && !avctx->parse_only) {
  285. int offset;
  286. /* scale factors table for layer 1/2 */
  287. for(i=0;i<64;i++) {
  288. int shift, mod;
  289. /* 1.0 (i = 3) is normalized to 2 ^ FRAC_BITS */
  290. shift = (i / 3);
  291. mod = i % 3;
  292. scale_factor_modshift[i] = mod | (shift << 2);
  293. }
  294. /* scale factor multiply for layer 1 */
  295. for(i=0;i<15;i++) {
  296. int n, norm;
  297. n = i + 2;
  298. norm = ((INT64_C(1) << n) * FRAC_ONE) / ((1 << n) - 1);
  299. scale_factor_mult[i][0] = MULL(FIXR(1.0 * 2.0), norm, FRAC_BITS);
  300. scale_factor_mult[i][1] = MULL(FIXR(0.7937005259 * 2.0), norm, FRAC_BITS);
  301. scale_factor_mult[i][2] = MULL(FIXR(0.6299605249 * 2.0), norm, FRAC_BITS);
  302. dprintf(avctx, "%d: norm=%x s=%x %x %x\n",
  303. i, norm,
  304. scale_factor_mult[i][0],
  305. scale_factor_mult[i][1],
  306. scale_factor_mult[i][2]);
  307. }
  308. ff_mpa_synth_init(window);
  309. /* huffman decode tables */
  310. offset = 0;
  311. for(i=1;i<16;i++) {
  312. const HuffTable *h = &mpa_huff_tables[i];
  313. int xsize, x, y;
  314. unsigned int n;
  315. uint8_t tmp_bits [512];
  316. uint16_t tmp_codes[512];
  317. memset(tmp_bits , 0, sizeof(tmp_bits ));
  318. memset(tmp_codes, 0, sizeof(tmp_codes));
  319. xsize = h->xsize;
  320. n = xsize * xsize;
  321. j = 0;
  322. for(x=0;x<xsize;x++) {
  323. for(y=0;y<xsize;y++){
  324. tmp_bits [(x << 5) | y | ((x&&y)<<4)]= h->bits [j ];
  325. tmp_codes[(x << 5) | y | ((x&&y)<<4)]= h->codes[j++];
  326. }
  327. }
  328. /* XXX: fail test */
  329. huff_vlc[i].table = huff_vlc_tables+offset;
  330. huff_vlc[i].table_allocated = huff_vlc_tables_sizes[i];
  331. init_vlc(&huff_vlc[i], 7, 512,
  332. tmp_bits, 1, 1, tmp_codes, 2, 2,
  333. INIT_VLC_USE_NEW_STATIC);
  334. offset += huff_vlc_tables_sizes[i];
  335. }
  336. assert(offset == FF_ARRAY_ELEMS(huff_vlc_tables));
  337. offset = 0;
  338. for(i=0;i<2;i++) {
  339. huff_quad_vlc[i].table = huff_quad_vlc_tables+offset;
  340. huff_quad_vlc[i].table_allocated = huff_quad_vlc_tables_sizes[i];
  341. init_vlc(&huff_quad_vlc[i], i == 0 ? 7 : 4, 16,
  342. mpa_quad_bits[i], 1, 1, mpa_quad_codes[i], 1, 1,
  343. INIT_VLC_USE_NEW_STATIC);
  344. offset += huff_quad_vlc_tables_sizes[i];
  345. }
  346. assert(offset == FF_ARRAY_ELEMS(huff_quad_vlc_tables));
  347. for(i=0;i<9;i++) {
  348. k = 0;
  349. for(j=0;j<22;j++) {
  350. band_index_long[i][j] = k;
  351. k += band_size_long[i][j];
  352. }
  353. band_index_long[i][22] = k;
  354. }
  355. /* compute n ^ (4/3) and store it in mantissa/exp format */
  356. int_pow_init();
  357. for(i=1;i<TABLE_4_3_SIZE;i++) {
  358. double f, fm;
  359. int e, m;
  360. f = pow((double)(i/4), 4.0 / 3.0) * pow(2, (i&3)*0.25);
  361. fm = frexp(f, &e);
  362. m = (uint32_t)(fm*(1LL<<31) + 0.5);
  363. e+= FRAC_BITS - 31 + 5 - 100;
  364. /* normalized to FRAC_BITS */
  365. table_4_3_value[i] = m;
  366. table_4_3_exp[i] = -e;
  367. }
  368. for(i=0; i<512*16; i++){
  369. int exponent= (i>>4);
  370. double f= pow(i&15, 4.0 / 3.0) * pow(2, (exponent-400)*0.25 + FRAC_BITS + 5);
  371. expval_table[exponent][i&15]= llrint(f);
  372. if((i&15)==1)
  373. exp_table[exponent]= llrint(f);
  374. }
  375. for(i=0;i<7;i++) {
  376. float f;
  377. int v;
  378. if (i != 6) {
  379. f = tan((double)i * M_PI / 12.0);
  380. v = FIXR(f / (1.0 + f));
  381. } else {
  382. v = FIXR(1.0);
  383. }
  384. is_table[0][i] = v;
  385. is_table[1][6 - i] = v;
  386. }
  387. /* invalid values */
  388. for(i=7;i<16;i++)
  389. is_table[0][i] = is_table[1][i] = 0.0;
  390. for(i=0;i<16;i++) {
  391. double f;
  392. int e, k;
  393. for(j=0;j<2;j++) {
  394. e = -(j + 1) * ((i + 1) >> 1);
  395. f = pow(2.0, e / 4.0);
  396. k = i & 1;
  397. is_table_lsf[j][k ^ 1][i] = FIXR(f);
  398. is_table_lsf[j][k][i] = FIXR(1.0);
  399. dprintf(avctx, "is_table_lsf %d %d: %x %x\n",
  400. i, j, is_table_lsf[j][0][i], is_table_lsf[j][1][i]);
  401. }
  402. }
  403. for(i=0;i<8;i++) {
  404. float ci, cs, ca;
  405. ci = ci_table[i];
  406. cs = 1.0 / sqrt(1.0 + ci * ci);
  407. ca = cs * ci;
  408. csa_table[i][0] = FIXHR(cs/4);
  409. csa_table[i][1] = FIXHR(ca/4);
  410. csa_table[i][2] = FIXHR(ca/4) + FIXHR(cs/4);
  411. csa_table[i][3] = FIXHR(ca/4) - FIXHR(cs/4);
  412. csa_table_float[i][0] = cs;
  413. csa_table_float[i][1] = ca;
  414. csa_table_float[i][2] = ca + cs;
  415. csa_table_float[i][3] = ca - cs;
  416. }
  417. /* compute mdct windows */
  418. for(i=0;i<36;i++) {
  419. for(j=0; j<4; j++){
  420. double d;
  421. if(j==2 && i%3 != 1)
  422. continue;
  423. d= sin(M_PI * (i + 0.5) / 36.0);
  424. if(j==1){
  425. if (i>=30) d= 0;
  426. else if(i>=24) d= sin(M_PI * (i - 18 + 0.5) / 12.0);
  427. else if(i>=18) d= 1;
  428. }else if(j==3){
  429. if (i< 6) d= 0;
  430. else if(i< 12) d= sin(M_PI * (i - 6 + 0.5) / 12.0);
  431. else if(i< 18) d= 1;
  432. }
  433. //merge last stage of imdct into the window coefficients
  434. d*= 0.5 / cos(M_PI*(2*i + 19)/72);
  435. if(j==2)
  436. mdct_win[j][i/3] = FIXHR((d / (1<<5)));
  437. else
  438. mdct_win[j][i ] = FIXHR((d / (1<<5)));
  439. }
  440. }
  441. /* NOTE: we do frequency inversion adter the MDCT by changing
  442. the sign of the right window coefs */
  443. for(j=0;j<4;j++) {
  444. for(i=0;i<36;i+=2) {
  445. mdct_win[j + 4][i] = mdct_win[j][i];
  446. mdct_win[j + 4][i + 1] = -mdct_win[j][i + 1];
  447. }
  448. }
  449. init = 1;
  450. }
  451. if (avctx->codec_id == CODEC_ID_MP3ADU)
  452. s->adu_mode = 1;
  453. return 0;
  454. }
  455. /* tab[i][j] = 1.0 / (2.0 * cos(pi*(2*k+1) / 2^(6 - j))) */
  456. /* cos(i*pi/64) */
  457. #define COS0_0 FIXHR(0.50060299823519630134/2)
  458. #define COS0_1 FIXHR(0.50547095989754365998/2)
  459. #define COS0_2 FIXHR(0.51544730992262454697/2)
  460. #define COS0_3 FIXHR(0.53104259108978417447/2)
  461. #define COS0_4 FIXHR(0.55310389603444452782/2)
  462. #define COS0_5 FIXHR(0.58293496820613387367/2)
  463. #define COS0_6 FIXHR(0.62250412303566481615/2)
  464. #define COS0_7 FIXHR(0.67480834145500574602/2)
  465. #define COS0_8 FIXHR(0.74453627100229844977/2)
  466. #define COS0_9 FIXHR(0.83934964541552703873/2)
  467. #define COS0_10 FIXHR(0.97256823786196069369/2)
  468. #define COS0_11 FIXHR(1.16943993343288495515/4)
  469. #define COS0_12 FIXHR(1.48416461631416627724/4)
  470. #define COS0_13 FIXHR(2.05778100995341155085/8)
  471. #define COS0_14 FIXHR(3.40760841846871878570/8)
  472. #define COS0_15 FIXHR(10.19000812354805681150/32)
  473. #define COS1_0 FIXHR(0.50241928618815570551/2)
  474. #define COS1_1 FIXHR(0.52249861493968888062/2)
  475. #define COS1_2 FIXHR(0.56694403481635770368/2)
  476. #define COS1_3 FIXHR(0.64682178335999012954/2)
  477. #define COS1_4 FIXHR(0.78815462345125022473/2)
  478. #define COS1_5 FIXHR(1.06067768599034747134/4)
  479. #define COS1_6 FIXHR(1.72244709823833392782/4)
  480. #define COS1_7 FIXHR(5.10114861868916385802/16)
  481. #define COS2_0 FIXHR(0.50979557910415916894/2)
  482. #define COS2_1 FIXHR(0.60134488693504528054/2)
  483. #define COS2_2 FIXHR(0.89997622313641570463/2)
  484. #define COS2_3 FIXHR(2.56291544774150617881/8)
  485. #define COS3_0 FIXHR(0.54119610014619698439/2)
  486. #define COS3_1 FIXHR(1.30656296487637652785/4)
  487. #define COS4_0 FIXHR(0.70710678118654752439/2)
  488. /* butterfly operator */
  489. #define BF(a, b, c, s)\
  490. {\
  491. tmp0 = tab[a] + tab[b];\
  492. tmp1 = tab[a] - tab[b];\
  493. tab[a] = tmp0;\
  494. tab[b] = MULH(tmp1<<(s), c);\
  495. }
  496. #define BF1(a, b, c, d)\
  497. {\
  498. BF(a, b, COS4_0, 1);\
  499. BF(c, d,-COS4_0, 1);\
  500. tab[c] += tab[d];\
  501. }
  502. #define BF2(a, b, c, d)\
  503. {\
  504. BF(a, b, COS4_0, 1);\
  505. BF(c, d,-COS4_0, 1);\
  506. tab[c] += tab[d];\
  507. tab[a] += tab[c];\
  508. tab[c] += tab[b];\
  509. tab[b] += tab[d];\
  510. }
  511. #define ADD(a, b) tab[a] += tab[b]
  512. /* DCT32 without 1/sqrt(2) coef zero scaling. */
  513. static void dct32(int32_t *out, int32_t *tab)
  514. {
  515. int tmp0, tmp1;
  516. /* pass 1 */
  517. BF( 0, 31, COS0_0 , 1);
  518. BF(15, 16, COS0_15, 5);
  519. /* pass 2 */
  520. BF( 0, 15, COS1_0 , 1);
  521. BF(16, 31,-COS1_0 , 1);
  522. /* pass 1 */
  523. BF( 7, 24, COS0_7 , 1);
  524. BF( 8, 23, COS0_8 , 1);
  525. /* pass 2 */
  526. BF( 7, 8, COS1_7 , 4);
  527. BF(23, 24,-COS1_7 , 4);
  528. /* pass 3 */
  529. BF( 0, 7, COS2_0 , 1);
  530. BF( 8, 15,-COS2_0 , 1);
  531. BF(16, 23, COS2_0 , 1);
  532. BF(24, 31,-COS2_0 , 1);
  533. /* pass 1 */
  534. BF( 3, 28, COS0_3 , 1);
  535. BF(12, 19, COS0_12, 2);
  536. /* pass 2 */
  537. BF( 3, 12, COS1_3 , 1);
  538. BF(19, 28,-COS1_3 , 1);
  539. /* pass 1 */
  540. BF( 4, 27, COS0_4 , 1);
  541. BF(11, 20, COS0_11, 2);
  542. /* pass 2 */
  543. BF( 4, 11, COS1_4 , 1);
  544. BF(20, 27,-COS1_4 , 1);
  545. /* pass 3 */
  546. BF( 3, 4, COS2_3 , 3);
  547. BF(11, 12,-COS2_3 , 3);
  548. BF(19, 20, COS2_3 , 3);
  549. BF(27, 28,-COS2_3 , 3);
  550. /* pass 4 */
  551. BF( 0, 3, COS3_0 , 1);
  552. BF( 4, 7,-COS3_0 , 1);
  553. BF( 8, 11, COS3_0 , 1);
  554. BF(12, 15,-COS3_0 , 1);
  555. BF(16, 19, COS3_0 , 1);
  556. BF(20, 23,-COS3_0 , 1);
  557. BF(24, 27, COS3_0 , 1);
  558. BF(28, 31,-COS3_0 , 1);
  559. /* pass 1 */
  560. BF( 1, 30, COS0_1 , 1);
  561. BF(14, 17, COS0_14, 3);
  562. /* pass 2 */
  563. BF( 1, 14, COS1_1 , 1);
  564. BF(17, 30,-COS1_1 , 1);
  565. /* pass 1 */
  566. BF( 6, 25, COS0_6 , 1);
  567. BF( 9, 22, COS0_9 , 1);
  568. /* pass 2 */
  569. BF( 6, 9, COS1_6 , 2);
  570. BF(22, 25,-COS1_6 , 2);
  571. /* pass 3 */
  572. BF( 1, 6, COS2_1 , 1);
  573. BF( 9, 14,-COS2_1 , 1);
  574. BF(17, 22, COS2_1 , 1);
  575. BF(25, 30,-COS2_1 , 1);
  576. /* pass 1 */
  577. BF( 2, 29, COS0_2 , 1);
  578. BF(13, 18, COS0_13, 3);
  579. /* pass 2 */
  580. BF( 2, 13, COS1_2 , 1);
  581. BF(18, 29,-COS1_2 , 1);
  582. /* pass 1 */
  583. BF( 5, 26, COS0_5 , 1);
  584. BF(10, 21, COS0_10, 1);
  585. /* pass 2 */
  586. BF( 5, 10, COS1_5 , 2);
  587. BF(21, 26,-COS1_5 , 2);
  588. /* pass 3 */
  589. BF( 2, 5, COS2_2 , 1);
  590. BF(10, 13,-COS2_2 , 1);
  591. BF(18, 21, COS2_2 , 1);
  592. BF(26, 29,-COS2_2 , 1);
  593. /* pass 4 */
  594. BF( 1, 2, COS3_1 , 2);
  595. BF( 5, 6,-COS3_1 , 2);
  596. BF( 9, 10, COS3_1 , 2);
  597. BF(13, 14,-COS3_1 , 2);
  598. BF(17, 18, COS3_1 , 2);
  599. BF(21, 22,-COS3_1 , 2);
  600. BF(25, 26, COS3_1 , 2);
  601. BF(29, 30,-COS3_1 , 2);
  602. /* pass 5 */
  603. BF1( 0, 1, 2, 3);
  604. BF2( 4, 5, 6, 7);
  605. BF1( 8, 9, 10, 11);
  606. BF2(12, 13, 14, 15);
  607. BF1(16, 17, 18, 19);
  608. BF2(20, 21, 22, 23);
  609. BF1(24, 25, 26, 27);
  610. BF2(28, 29, 30, 31);
  611. /* pass 6 */
  612. ADD( 8, 12);
  613. ADD(12, 10);
  614. ADD(10, 14);
  615. ADD(14, 9);
  616. ADD( 9, 13);
  617. ADD(13, 11);
  618. ADD(11, 15);
  619. out[ 0] = tab[0];
  620. out[16] = tab[1];
  621. out[ 8] = tab[2];
  622. out[24] = tab[3];
  623. out[ 4] = tab[4];
  624. out[20] = tab[5];
  625. out[12] = tab[6];
  626. out[28] = tab[7];
  627. out[ 2] = tab[8];
  628. out[18] = tab[9];
  629. out[10] = tab[10];
  630. out[26] = tab[11];
  631. out[ 6] = tab[12];
  632. out[22] = tab[13];
  633. out[14] = tab[14];
  634. out[30] = tab[15];
  635. ADD(24, 28);
  636. ADD(28, 26);
  637. ADD(26, 30);
  638. ADD(30, 25);
  639. ADD(25, 29);
  640. ADD(29, 27);
  641. ADD(27, 31);
  642. out[ 1] = tab[16] + tab[24];
  643. out[17] = tab[17] + tab[25];
  644. out[ 9] = tab[18] + tab[26];
  645. out[25] = tab[19] + tab[27];
  646. out[ 5] = tab[20] + tab[28];
  647. out[21] = tab[21] + tab[29];
  648. out[13] = tab[22] + tab[30];
  649. out[29] = tab[23] + tab[31];
  650. out[ 3] = tab[24] + tab[20];
  651. out[19] = tab[25] + tab[21];
  652. out[11] = tab[26] + tab[22];
  653. out[27] = tab[27] + tab[23];
  654. out[ 7] = tab[28] + tab[18];
  655. out[23] = tab[29] + tab[19];
  656. out[15] = tab[30] + tab[17];
  657. out[31] = tab[31];
  658. }
  659. #if FRAC_BITS <= 15
  660. static inline int round_sample(int *sum)
  661. {
  662. int sum1;
  663. sum1 = (*sum) >> OUT_SHIFT;
  664. *sum &= (1<<OUT_SHIFT)-1;
  665. if (sum1 < OUT_MIN)
  666. sum1 = OUT_MIN;
  667. else if (sum1 > OUT_MAX)
  668. sum1 = OUT_MAX;
  669. return sum1;
  670. }
  671. /* signed 16x16 -> 32 multiply add accumulate */
  672. #define MACS(rt, ra, rb) MAC16(rt, ra, rb)
  673. /* signed 16x16 -> 32 multiply */
  674. #define MULS(ra, rb) MUL16(ra, rb)
  675. #define MLSS(rt, ra, rb) MLS16(rt, ra, rb)
  676. #else
  677. static inline int round_sample(int64_t *sum)
  678. {
  679. int sum1;
  680. sum1 = (int)((*sum) >> OUT_SHIFT);
  681. *sum &= (1<<OUT_SHIFT)-1;
  682. if (sum1 < OUT_MIN)
  683. sum1 = OUT_MIN;
  684. else if (sum1 > OUT_MAX)
  685. sum1 = OUT_MAX;
  686. return sum1;
  687. }
  688. # define MULS(ra, rb) MUL64(ra, rb)
  689. # define MACS(rt, ra, rb) MAC64(rt, ra, rb)
  690. # define MLSS(rt, ra, rb) MLS64(rt, ra, rb)
  691. #endif
  692. #define SUM8(op, sum, w, p) \
  693. { \
  694. op(sum, (w)[0 * 64], p[0 * 64]); \
  695. op(sum, (w)[1 * 64], p[1 * 64]); \
  696. op(sum, (w)[2 * 64], p[2 * 64]); \
  697. op(sum, (w)[3 * 64], p[3 * 64]); \
  698. op(sum, (w)[4 * 64], p[4 * 64]); \
  699. op(sum, (w)[5 * 64], p[5 * 64]); \
  700. op(sum, (w)[6 * 64], p[6 * 64]); \
  701. op(sum, (w)[7 * 64], p[7 * 64]); \
  702. }
  703. #define SUM8P2(sum1, op1, sum2, op2, w1, w2, p) \
  704. { \
  705. int tmp;\
  706. tmp = p[0 * 64];\
  707. op1(sum1, (w1)[0 * 64], tmp);\
  708. op2(sum2, (w2)[0 * 64], tmp);\
  709. tmp = p[1 * 64];\
  710. op1(sum1, (w1)[1 * 64], tmp);\
  711. op2(sum2, (w2)[1 * 64], tmp);\
  712. tmp = p[2 * 64];\
  713. op1(sum1, (w1)[2 * 64], tmp);\
  714. op2(sum2, (w2)[2 * 64], tmp);\
  715. tmp = p[3 * 64];\
  716. op1(sum1, (w1)[3 * 64], tmp);\
  717. op2(sum2, (w2)[3 * 64], tmp);\
  718. tmp = p[4 * 64];\
  719. op1(sum1, (w1)[4 * 64], tmp);\
  720. op2(sum2, (w2)[4 * 64], tmp);\
  721. tmp = p[5 * 64];\
  722. op1(sum1, (w1)[5 * 64], tmp);\
  723. op2(sum2, (w2)[5 * 64], tmp);\
  724. tmp = p[6 * 64];\
  725. op1(sum1, (w1)[6 * 64], tmp);\
  726. op2(sum2, (w2)[6 * 64], tmp);\
  727. tmp = p[7 * 64];\
  728. op1(sum1, (w1)[7 * 64], tmp);\
  729. op2(sum2, (w2)[7 * 64], tmp);\
  730. }
  731. void av_cold ff_mpa_synth_init(MPA_INT *window)
  732. {
  733. int i;
  734. /* max = 18760, max sum over all 16 coefs : 44736 */
  735. for(i=0;i<257;i++) {
  736. int v;
  737. v = ff_mpa_enwindow[i];
  738. #if WFRAC_BITS < 16
  739. v = (v + (1 << (16 - WFRAC_BITS - 1))) >> (16 - WFRAC_BITS);
  740. #endif
  741. window[i] = v;
  742. if ((i & 63) != 0)
  743. v = -v;
  744. if (i != 0)
  745. window[512 - i] = v;
  746. }
  747. }
  748. /* 32 sub band synthesis filter. Input: 32 sub band samples, Output:
  749. 32 samples. */
  750. /* XXX: optimize by avoiding ring buffer usage */
  751. void ff_mpa_synth_filter(MPA_INT *synth_buf_ptr, int *synth_buf_offset,
  752. MPA_INT *window, int *dither_state,
  753. OUT_INT *samples, int incr,
  754. int32_t sb_samples[SBLIMIT])
  755. {
  756. int32_t tmp[32];
  757. register MPA_INT *synth_buf;
  758. register const MPA_INT *w, *w2, *p;
  759. int j, offset, v;
  760. OUT_INT *samples2;
  761. #if FRAC_BITS <= 15
  762. int sum, sum2;
  763. #else
  764. int64_t sum, sum2;
  765. #endif
  766. dct32(tmp, sb_samples);
  767. offset = *synth_buf_offset;
  768. synth_buf = synth_buf_ptr + offset;
  769. for(j=0;j<32;j++) {
  770. v = tmp[j];
  771. #if FRAC_BITS <= 15
  772. /* NOTE: can cause a loss in precision if very high amplitude
  773. sound */
  774. v = av_clip_int16(v);
  775. #endif
  776. synth_buf[j] = v;
  777. }
  778. /* copy to avoid wrap */
  779. memcpy(synth_buf + 512, synth_buf, 32 * sizeof(MPA_INT));
  780. samples2 = samples + 31 * incr;
  781. w = window;
  782. w2 = window + 31;
  783. sum = *dither_state;
  784. p = synth_buf + 16;
  785. SUM8(MACS, sum, w, p);
  786. p = synth_buf + 48;
  787. SUM8(MLSS, sum, w + 32, p);
  788. *samples = round_sample(&sum);
  789. samples += incr;
  790. w++;
  791. /* we calculate two samples at the same time to avoid one memory
  792. access per two sample */
  793. for(j=1;j<16;j++) {
  794. sum2 = 0;
  795. p = synth_buf + 16 + j;
  796. SUM8P2(sum, MACS, sum2, MLSS, w, w2, p);
  797. p = synth_buf + 48 - j;
  798. SUM8P2(sum, MLSS, sum2, MLSS, w + 32, w2 + 32, p);
  799. *samples = round_sample(&sum);
  800. samples += incr;
  801. sum += sum2;
  802. *samples2 = round_sample(&sum);
  803. samples2 -= incr;
  804. w++;
  805. w2--;
  806. }
  807. p = synth_buf + 32;
  808. SUM8(MLSS, sum, w + 32, p);
  809. *samples = round_sample(&sum);
  810. *dither_state= sum;
  811. offset = (offset - 32) & 511;
  812. *synth_buf_offset = offset;
  813. }
  814. #define C3 FIXHR(0.86602540378443864676/2)
  815. /* 0.5 / cos(pi*(2*i+1)/36) */
  816. static const int icos36[9] = {
  817. FIXR(0.50190991877167369479),
  818. FIXR(0.51763809020504152469), //0
  819. FIXR(0.55168895948124587824),
  820. FIXR(0.61038729438072803416),
  821. FIXR(0.70710678118654752439), //1
  822. FIXR(0.87172339781054900991),
  823. FIXR(1.18310079157624925896),
  824. FIXR(1.93185165257813657349), //2
  825. FIXR(5.73685662283492756461),
  826. };
  827. /* 0.5 / cos(pi*(2*i+1)/36) */
  828. static const int icos36h[9] = {
  829. FIXHR(0.50190991877167369479/2),
  830. FIXHR(0.51763809020504152469/2), //0
  831. FIXHR(0.55168895948124587824/2),
  832. FIXHR(0.61038729438072803416/2),
  833. FIXHR(0.70710678118654752439/2), //1
  834. FIXHR(0.87172339781054900991/2),
  835. FIXHR(1.18310079157624925896/4),
  836. FIXHR(1.93185165257813657349/4), //2
  837. // FIXHR(5.73685662283492756461),
  838. };
  839. /* 12 points IMDCT. We compute it "by hand" by factorizing obvious
  840. cases. */
  841. static void imdct12(int *out, int *in)
  842. {
  843. int in0, in1, in2, in3, in4, in5, t1, t2;
  844. in0= in[0*3];
  845. in1= in[1*3] + in[0*3];
  846. in2= in[2*3] + in[1*3];
  847. in3= in[3*3] + in[2*3];
  848. in4= in[4*3] + in[3*3];
  849. in5= in[5*3] + in[4*3];
  850. in5 += in3;
  851. in3 += in1;
  852. in2= MULH(2*in2, C3);
  853. in3= MULH(4*in3, C3);
  854. t1 = in0 - in4;
  855. t2 = MULH(2*(in1 - in5), icos36h[4]);
  856. out[ 7]=
  857. out[10]= t1 + t2;
  858. out[ 1]=
  859. out[ 4]= t1 - t2;
  860. in0 += in4>>1;
  861. in4 = in0 + in2;
  862. in5 += 2*in1;
  863. in1 = MULH(in5 + in3, icos36h[1]);
  864. out[ 8]=
  865. out[ 9]= in4 + in1;
  866. out[ 2]=
  867. out[ 3]= in4 - in1;
  868. in0 -= in2;
  869. in5 = MULH(2*(in5 - in3), icos36h[7]);
  870. out[ 0]=
  871. out[ 5]= in0 - in5;
  872. out[ 6]=
  873. out[11]= in0 + in5;
  874. }
  875. /* cos(pi*i/18) */
  876. #define C1 FIXHR(0.98480775301220805936/2)
  877. #define C2 FIXHR(0.93969262078590838405/2)
  878. #define C3 FIXHR(0.86602540378443864676/2)
  879. #define C4 FIXHR(0.76604444311897803520/2)
  880. #define C5 FIXHR(0.64278760968653932632/2)
  881. #define C6 FIXHR(0.5/2)
  882. #define C7 FIXHR(0.34202014332566873304/2)
  883. #define C8 FIXHR(0.17364817766693034885/2)
  884. /* using Lee like decomposition followed by hand coded 9 points DCT */
  885. static void imdct36(int *out, int *buf, int *in, int *win)
  886. {
  887. int i, j, t0, t1, t2, t3, s0, s1, s2, s3;
  888. int tmp[18], *tmp1, *in1;
  889. for(i=17;i>=1;i--)
  890. in[i] += in[i-1];
  891. for(i=17;i>=3;i-=2)
  892. in[i] += in[i-2];
  893. for(j=0;j<2;j++) {
  894. tmp1 = tmp + j;
  895. in1 = in + j;
  896. #if 0
  897. //more accurate but slower
  898. int64_t t0, t1, t2, t3;
  899. t2 = in1[2*4] + in1[2*8] - in1[2*2];
  900. t3 = (in1[2*0] + (int64_t)(in1[2*6]>>1))<<32;
  901. t1 = in1[2*0] - in1[2*6];
  902. tmp1[ 6] = t1 - (t2>>1);
  903. tmp1[16] = t1 + t2;
  904. t0 = MUL64(2*(in1[2*2] + in1[2*4]), C2);
  905. t1 = MUL64( in1[2*4] - in1[2*8] , -2*C8);
  906. t2 = MUL64(2*(in1[2*2] + in1[2*8]), -C4);
  907. tmp1[10] = (t3 - t0 - t2) >> 32;
  908. tmp1[ 2] = (t3 + t0 + t1) >> 32;
  909. tmp1[14] = (t3 + t2 - t1) >> 32;
  910. tmp1[ 4] = MULH(2*(in1[2*5] + in1[2*7] - in1[2*1]), -C3);
  911. t2 = MUL64(2*(in1[2*1] + in1[2*5]), C1);
  912. t3 = MUL64( in1[2*5] - in1[2*7] , -2*C7);
  913. t0 = MUL64(2*in1[2*3], C3);
  914. t1 = MUL64(2*(in1[2*1] + in1[2*7]), -C5);
  915. tmp1[ 0] = (t2 + t3 + t0) >> 32;
  916. tmp1[12] = (t2 + t1 - t0) >> 32;
  917. tmp1[ 8] = (t3 - t1 - t0) >> 32;
  918. #else
  919. t2 = in1[2*4] + in1[2*8] - in1[2*2];
  920. t3 = in1[2*0] + (in1[2*6]>>1);
  921. t1 = in1[2*0] - in1[2*6];
  922. tmp1[ 6] = t1 - (t2>>1);
  923. tmp1[16] = t1 + t2;
  924. t0 = MULH(2*(in1[2*2] + in1[2*4]), C2);
  925. t1 = MULH( in1[2*4] - in1[2*8] , -2*C8);
  926. t2 = MULH(2*(in1[2*2] + in1[2*8]), -C4);
  927. tmp1[10] = t3 - t0 - t2;
  928. tmp1[ 2] = t3 + t0 + t1;
  929. tmp1[14] = t3 + t2 - t1;
  930. tmp1[ 4] = MULH(2*(in1[2*5] + in1[2*7] - in1[2*1]), -C3);
  931. t2 = MULH(2*(in1[2*1] + in1[2*5]), C1);
  932. t3 = MULH( in1[2*5] - in1[2*7] , -2*C7);
  933. t0 = MULH(2*in1[2*3], C3);
  934. t1 = MULH(2*(in1[2*1] + in1[2*7]), -C5);
  935. tmp1[ 0] = t2 + t3 + t0;
  936. tmp1[12] = t2 + t1 - t0;
  937. tmp1[ 8] = t3 - t1 - t0;
  938. #endif
  939. }
  940. i = 0;
  941. for(j=0;j<4;j++) {
  942. t0 = tmp[i];
  943. t1 = tmp[i + 2];
  944. s0 = t1 + t0;
  945. s2 = t1 - t0;
  946. t2 = tmp[i + 1];
  947. t3 = tmp[i + 3];
  948. s1 = MULH(2*(t3 + t2), icos36h[j]);
  949. s3 = MULL(t3 - t2, icos36[8 - j], FRAC_BITS);
  950. t0 = s0 + s1;
  951. t1 = s0 - s1;
  952. out[(9 + j)*SBLIMIT] = MULH(t1, win[9 + j]) + buf[9 + j];
  953. out[(8 - j)*SBLIMIT] = MULH(t1, win[8 - j]) + buf[8 - j];
  954. buf[9 + j] = MULH(t0, win[18 + 9 + j]);
  955. buf[8 - j] = MULH(t0, win[18 + 8 - j]);
  956. t0 = s2 + s3;
  957. t1 = s2 - s3;
  958. out[(9 + 8 - j)*SBLIMIT] = MULH(t1, win[9 + 8 - j]) + buf[9 + 8 - j];
  959. out[( j)*SBLIMIT] = MULH(t1, win[ j]) + buf[ j];
  960. buf[9 + 8 - j] = MULH(t0, win[18 + 9 + 8 - j]);
  961. buf[ + j] = MULH(t0, win[18 + j]);
  962. i += 4;
  963. }
  964. s0 = tmp[16];
  965. s1 = MULH(2*tmp[17], icos36h[4]);
  966. t0 = s0 + s1;
  967. t1 = s0 - s1;
  968. out[(9 + 4)*SBLIMIT] = MULH(t1, win[9 + 4]) + buf[9 + 4];
  969. out[(8 - 4)*SBLIMIT] = MULH(t1, win[8 - 4]) + buf[8 - 4];
  970. buf[9 + 4] = MULH(t0, win[18 + 9 + 4]);
  971. buf[8 - 4] = MULH(t0, win[18 + 8 - 4]);
  972. }
  973. /* return the number of decoded frames */
  974. static int mp_decode_layer1(MPADecodeContext *s)
  975. {
  976. int bound, i, v, n, ch, j, mant;
  977. uint8_t allocation[MPA_MAX_CHANNELS][SBLIMIT];
  978. uint8_t scale_factors[MPA_MAX_CHANNELS][SBLIMIT];
  979. if (s->mode == MPA_JSTEREO)
  980. bound = (s->mode_ext + 1) * 4;
  981. else
  982. bound = SBLIMIT;
  983. /* allocation bits */
  984. for(i=0;i<bound;i++) {
  985. for(ch=0;ch<s->nb_channels;ch++) {
  986. allocation[ch][i] = get_bits(&s->gb, 4);
  987. }
  988. }
  989. for(i=bound;i<SBLIMIT;i++) {
  990. allocation[0][i] = get_bits(&s->gb, 4);
  991. }
  992. /* scale factors */
  993. for(i=0;i<bound;i++) {
  994. for(ch=0;ch<s->nb_channels;ch++) {
  995. if (allocation[ch][i])
  996. scale_factors[ch][i] = get_bits(&s->gb, 6);
  997. }
  998. }
  999. for(i=bound;i<SBLIMIT;i++) {
  1000. if (allocation[0][i]) {
  1001. scale_factors[0][i] = get_bits(&s->gb, 6);
  1002. scale_factors[1][i] = get_bits(&s->gb, 6);
  1003. }
  1004. }
  1005. /* compute samples */
  1006. for(j=0;j<12;j++) {
  1007. for(i=0;i<bound;i++) {
  1008. for(ch=0;ch<s->nb_channels;ch++) {
  1009. n = allocation[ch][i];
  1010. if (n) {
  1011. mant = get_bits(&s->gb, n + 1);
  1012. v = l1_unscale(n, mant, scale_factors[ch][i]);
  1013. } else {
  1014. v = 0;
  1015. }
  1016. s->sb_samples[ch][j][i] = v;
  1017. }
  1018. }
  1019. for(i=bound;i<SBLIMIT;i++) {
  1020. n = allocation[0][i];
  1021. if (n) {
  1022. mant = get_bits(&s->gb, n + 1);
  1023. v = l1_unscale(n, mant, scale_factors[0][i]);
  1024. s->sb_samples[0][j][i] = v;
  1025. v = l1_unscale(n, mant, scale_factors[1][i]);
  1026. s->sb_samples[1][j][i] = v;
  1027. } else {
  1028. s->sb_samples[0][j][i] = 0;
  1029. s->sb_samples[1][j][i] = 0;
  1030. }
  1031. }
  1032. }
  1033. return 12;
  1034. }
  1035. static int mp_decode_layer2(MPADecodeContext *s)
  1036. {
  1037. int sblimit; /* number of used subbands */
  1038. const unsigned char *alloc_table;
  1039. int table, bit_alloc_bits, i, j, ch, bound, v;
  1040. unsigned char bit_alloc[MPA_MAX_CHANNELS][SBLIMIT];
  1041. unsigned char scale_code[MPA_MAX_CHANNELS][SBLIMIT];
  1042. unsigned char scale_factors[MPA_MAX_CHANNELS][SBLIMIT][3], *sf;
  1043. int scale, qindex, bits, steps, k, l, m, b;
  1044. /* select decoding table */
  1045. table = ff_mpa_l2_select_table(s->bit_rate / 1000, s->nb_channels,
  1046. s->sample_rate, s->lsf);
  1047. sblimit = ff_mpa_sblimit_table[table];
  1048. alloc_table = ff_mpa_alloc_tables[table];
  1049. if (s->mode == MPA_JSTEREO)
  1050. bound = (s->mode_ext + 1) * 4;
  1051. else
  1052. bound = sblimit;
  1053. dprintf(s->avctx, "bound=%d sblimit=%d\n", bound, sblimit);
  1054. /* sanity check */
  1055. if( bound > sblimit ) bound = sblimit;
  1056. /* parse bit allocation */
  1057. j = 0;
  1058. for(i=0;i<bound;i++) {
  1059. bit_alloc_bits = alloc_table[j];
  1060. for(ch=0;ch<s->nb_channels;ch++) {
  1061. bit_alloc[ch][i] = get_bits(&s->gb, bit_alloc_bits);
  1062. }
  1063. j += 1 << bit_alloc_bits;
  1064. }
  1065. for(i=bound;i<sblimit;i++) {
  1066. bit_alloc_bits = alloc_table[j];
  1067. v = get_bits(&s->gb, bit_alloc_bits);
  1068. bit_alloc[0][i] = v;
  1069. bit_alloc[1][i] = v;
  1070. j += 1 << bit_alloc_bits;
  1071. }
  1072. /* scale codes */
  1073. for(i=0;i<sblimit;i++) {
  1074. for(ch=0;ch<s->nb_channels;ch++) {
  1075. if (bit_alloc[ch][i])
  1076. scale_code[ch][i] = get_bits(&s->gb, 2);
  1077. }
  1078. }
  1079. /* scale factors */
  1080. for(i=0;i<sblimit;i++) {
  1081. for(ch=0;ch<s->nb_channels;ch++) {
  1082. if (bit_alloc[ch][i]) {
  1083. sf = scale_factors[ch][i];
  1084. switch(scale_code[ch][i]) {
  1085. default:
  1086. case 0:
  1087. sf[0] = get_bits(&s->gb, 6);
  1088. sf[1] = get_bits(&s->gb, 6);
  1089. sf[2] = get_bits(&s->gb, 6);
  1090. break;
  1091. case 2:
  1092. sf[0] = get_bits(&s->gb, 6);
  1093. sf[1] = sf[0];
  1094. sf[2] = sf[0];
  1095. break;
  1096. case 1:
  1097. sf[0] = get_bits(&s->gb, 6);
  1098. sf[2] = get_bits(&s->gb, 6);
  1099. sf[1] = sf[0];
  1100. break;
  1101. case 3:
  1102. sf[0] = get_bits(&s->gb, 6);
  1103. sf[2] = get_bits(&s->gb, 6);
  1104. sf[1] = sf[2];
  1105. break;
  1106. }
  1107. }
  1108. }
  1109. }
  1110. /* samples */
  1111. for(k=0;k<3;k++) {
  1112. for(l=0;l<12;l+=3) {
  1113. j = 0;
  1114. for(i=0;i<bound;i++) {
  1115. bit_alloc_bits = alloc_table[j];
  1116. for(ch=0;ch<s->nb_channels;ch++) {
  1117. b = bit_alloc[ch][i];
  1118. if (b) {
  1119. scale = scale_factors[ch][i][k];
  1120. qindex = alloc_table[j+b];
  1121. bits = ff_mpa_quant_bits[qindex];
  1122. if (bits < 0) {
  1123. /* 3 values at the same time */
  1124. v = get_bits(&s->gb, -bits);
  1125. steps = ff_mpa_quant_steps[qindex];
  1126. s->sb_samples[ch][k * 12 + l + 0][i] =
  1127. l2_unscale_group(steps, v % steps, scale);
  1128. v = v / steps;
  1129. s->sb_samples[ch][k * 12 + l + 1][i] =
  1130. l2_unscale_group(steps, v % steps, scale);
  1131. v = v / steps;
  1132. s->sb_samples[ch][k * 12 + l + 2][i] =
  1133. l2_unscale_group(steps, v, scale);
  1134. } else {
  1135. for(m=0;m<3;m++) {
  1136. v = get_bits(&s->gb, bits);
  1137. v = l1_unscale(bits - 1, v, scale);
  1138. s->sb_samples[ch][k * 12 + l + m][i] = v;
  1139. }
  1140. }
  1141. } else {
  1142. s->sb_samples[ch][k * 12 + l + 0][i] = 0;
  1143. s->sb_samples[ch][k * 12 + l + 1][i] = 0;
  1144. s->sb_samples[ch][k * 12 + l + 2][i] = 0;
  1145. }
  1146. }
  1147. /* next subband in alloc table */
  1148. j += 1 << bit_alloc_bits;
  1149. }
  1150. /* XXX: find a way to avoid this duplication of code */
  1151. for(i=bound;i<sblimit;i++) {
  1152. bit_alloc_bits = alloc_table[j];
  1153. b = bit_alloc[0][i];
  1154. if (b) {
  1155. int mant, scale0, scale1;
  1156. scale0 = scale_factors[0][i][k];
  1157. scale1 = scale_factors[1][i][k];
  1158. qindex = alloc_table[j+b];
  1159. bits = ff_mpa_quant_bits[qindex];
  1160. if (bits < 0) {
  1161. /* 3 values at the same time */
  1162. v = get_bits(&s->gb, -bits);
  1163. steps = ff_mpa_quant_steps[qindex];
  1164. mant = v % steps;
  1165. v = v / steps;
  1166. s->sb_samples[0][k * 12 + l + 0][i] =
  1167. l2_unscale_group(steps, mant, scale0);
  1168. s->sb_samples[1][k * 12 + l + 0][i] =
  1169. l2_unscale_group(steps, mant, scale1);
  1170. mant = v % steps;
  1171. v = v / steps;
  1172. s->sb_samples[0][k * 12 + l + 1][i] =
  1173. l2_unscale_group(steps, mant, scale0);
  1174. s->sb_samples[1][k * 12 + l + 1][i] =
  1175. l2_unscale_group(steps, mant, scale1);
  1176. s->sb_samples[0][k * 12 + l + 2][i] =
  1177. l2_unscale_group(steps, v, scale0);
  1178. s->sb_samples[1][k * 12 + l + 2][i] =
  1179. l2_unscale_group(steps, v, scale1);
  1180. } else {
  1181. for(m=0;m<3;m++) {
  1182. mant = get_bits(&s->gb, bits);
  1183. s->sb_samples[0][k * 12 + l + m][i] =
  1184. l1_unscale(bits - 1, mant, scale0);
  1185. s->sb_samples[1][k * 12 + l + m][i] =
  1186. l1_unscale(bits - 1, mant, scale1);
  1187. }
  1188. }
  1189. } else {
  1190. s->sb_samples[0][k * 12 + l + 0][i] = 0;
  1191. s->sb_samples[0][k * 12 + l + 1][i] = 0;
  1192. s->sb_samples[0][k * 12 + l + 2][i] = 0;
  1193. s->sb_samples[1][k * 12 + l + 0][i] = 0;
  1194. s->sb_samples[1][k * 12 + l + 1][i] = 0;
  1195. s->sb_samples[1][k * 12 + l + 2][i] = 0;
  1196. }
  1197. /* next subband in alloc table */
  1198. j += 1 << bit_alloc_bits;
  1199. }
  1200. /* fill remaining samples to zero */
  1201. for(i=sblimit;i<SBLIMIT;i++) {
  1202. for(ch=0;ch<s->nb_channels;ch++) {
  1203. s->sb_samples[ch][k * 12 + l + 0][i] = 0;
  1204. s->sb_samples[ch][k * 12 + l + 1][i] = 0;
  1205. s->sb_samples[ch][k * 12 + l + 2][i] = 0;
  1206. }
  1207. }
  1208. }
  1209. }
  1210. return 3 * 12;
  1211. }
  1212. static inline void lsf_sf_expand(int *slen,
  1213. int sf, int n1, int n2, int n3)
  1214. {
  1215. if (n3) {
  1216. slen[3] = sf % n3;
  1217. sf /= n3;
  1218. } else {
  1219. slen[3] = 0;
  1220. }
  1221. if (n2) {
  1222. slen[2] = sf % n2;
  1223. sf /= n2;
  1224. } else {
  1225. slen[2] = 0;
  1226. }
  1227. slen[1] = sf % n1;
  1228. sf /= n1;
  1229. slen[0] = sf;
  1230. }
  1231. static void exponents_from_scale_factors(MPADecodeContext *s,
  1232. GranuleDef *g,
  1233. int16_t *exponents)
  1234. {
  1235. const uint8_t *bstab, *pretab;
  1236. int len, i, j, k, l, v0, shift, gain, gains[3];
  1237. int16_t *exp_ptr;
  1238. exp_ptr = exponents;
  1239. gain = g->global_gain - 210;
  1240. shift = g->scalefac_scale + 1;
  1241. bstab = band_size_long[s->sample_rate_index];
  1242. pretab = mpa_pretab[g->preflag];
  1243. for(i=0;i<g->long_end;i++) {
  1244. v0 = gain - ((g->scale_factors[i] + pretab[i]) << shift) + 400;
  1245. len = bstab[i];
  1246. for(j=len;j>0;j--)
  1247. *exp_ptr++ = v0;
  1248. }
  1249. if (g->short_start < 13) {
  1250. bstab = band_size_short[s->sample_rate_index];
  1251. gains[0] = gain - (g->subblock_gain[0] << 3);
  1252. gains[1] = gain - (g->subblock_gain[1] << 3);
  1253. gains[2] = gain - (g->subblock_gain[2] << 3);
  1254. k = g->long_end;
  1255. for(i=g->short_start;i<13;i++) {
  1256. len = bstab[i];
  1257. for(l=0;l<3;l++) {
  1258. v0 = gains[l] - (g->scale_factors[k++] << shift) + 400;
  1259. for(j=len;j>0;j--)
  1260. *exp_ptr++ = v0;
  1261. }
  1262. }
  1263. }
  1264. }
  1265. /* handle n = 0 too */
  1266. static inline int get_bitsz(GetBitContext *s, int n)
  1267. {
  1268. if (n == 0)
  1269. return 0;
  1270. else
  1271. return get_bits(s, n);
  1272. }
  1273. static void switch_buffer(MPADecodeContext *s, int *pos, int *end_pos, int *end_pos2){
  1274. if(s->in_gb.buffer && *pos >= s->gb.size_in_bits){
  1275. s->gb= s->in_gb;
  1276. s->in_gb.buffer=NULL;
  1277. assert((get_bits_count(&s->gb) & 7) == 0);
  1278. skip_bits_long(&s->gb, *pos - *end_pos);
  1279. *end_pos2=
  1280. *end_pos= *end_pos2 + get_bits_count(&s->gb) - *pos;
  1281. *pos= get_bits_count(&s->gb);
  1282. }
  1283. }
  1284. static int huffman_decode(MPADecodeContext *s, GranuleDef *g,
  1285. int16_t *exponents, int end_pos2)
  1286. {
  1287. int s_index;
  1288. int i;
  1289. int last_pos, bits_left;
  1290. VLC *vlc;
  1291. int end_pos= FFMIN(end_pos2, s->gb.size_in_bits);
  1292. /* low frequencies (called big values) */
  1293. s_index = 0;
  1294. for(i=0;i<3;i++) {
  1295. int j, k, l, linbits;
  1296. j = g->region_size[i];
  1297. if (j == 0)
  1298. continue;
  1299. /* select vlc table */
  1300. k = g->table_select[i];
  1301. l = mpa_huff_data[k][0];
  1302. linbits = mpa_huff_data[k][1];
  1303. vlc = &huff_vlc[l];
  1304. if(!l){
  1305. memset(&g->sb_hybrid[s_index], 0, sizeof(*g->sb_hybrid)*2*j);
  1306. s_index += 2*j;
  1307. continue;
  1308. }
  1309. /* read huffcode and compute each couple */
  1310. for(;j>0;j--) {
  1311. int exponent, x, y, v;
  1312. int pos= get_bits_count(&s->gb);
  1313. if (pos >= end_pos){
  1314. // av_log(NULL, AV_LOG_ERROR, "pos: %d %d %d %d\n", pos, end_pos, end_pos2, s_index);
  1315. switch_buffer(s, &pos, &end_pos, &end_pos2);
  1316. // av_log(NULL, AV_LOG_ERROR, "new pos: %d %d\n", pos, end_pos);
  1317. if(pos >= end_pos)
  1318. break;
  1319. }
  1320. y = get_vlc2(&s->gb, vlc->table, 7, 3);
  1321. if(!y){
  1322. g->sb_hybrid[s_index ] =
  1323. g->sb_hybrid[s_index+1] = 0;
  1324. s_index += 2;
  1325. continue;
  1326. }
  1327. exponent= exponents[s_index];
  1328. dprintf(s->avctx, "region=%d n=%d x=%d y=%d exp=%d\n",
  1329. i, g->region_size[i] - j, x, y, exponent);
  1330. if(y&16){
  1331. x = y >> 5;
  1332. y = y & 0x0f;
  1333. if (x < 15){
  1334. v = expval_table[ exponent ][ x ];
  1335. // v = expval_table[ (exponent&3) ][ x ] >> FFMIN(0 - (exponent>>2), 31);
  1336. }else{
  1337. x += get_bitsz(&s->gb, linbits);
  1338. v = l3_unscale(x, exponent);
  1339. }
  1340. if (get_bits1(&s->gb))
  1341. v = -v;
  1342. g->sb_hybrid[s_index] = v;
  1343. if (y < 15){
  1344. v = expval_table[ exponent ][ y ];
  1345. }else{
  1346. y += get_bitsz(&s->gb, linbits);
  1347. v = l3_unscale(y, exponent);
  1348. }
  1349. if (get_bits1(&s->gb))
  1350. v = -v;
  1351. g->sb_hybrid[s_index+1] = v;
  1352. }else{
  1353. x = y >> 5;
  1354. y = y & 0x0f;
  1355. x += y;
  1356. if (x < 15){
  1357. v = expval_table[ exponent ][ x ];
  1358. }else{
  1359. x += get_bitsz(&s->gb, linbits);
  1360. v = l3_unscale(x, exponent);
  1361. }
  1362. if (get_bits1(&s->gb))
  1363. v = -v;
  1364. g->sb_hybrid[s_index+!!y] = v;
  1365. g->sb_hybrid[s_index+ !y] = 0;
  1366. }
  1367. s_index+=2;
  1368. }
  1369. }
  1370. /* high frequencies */
  1371. vlc = &huff_quad_vlc[g->count1table_select];
  1372. last_pos=0;
  1373. while (s_index <= 572) {
  1374. int pos, code;
  1375. pos = get_bits_count(&s->gb);
  1376. if (pos >= end_pos) {
  1377. if (pos > end_pos2 && last_pos){
  1378. /* some encoders generate an incorrect size for this
  1379. part. We must go back into the data */
  1380. s_index -= 4;
  1381. skip_bits_long(&s->gb, last_pos - pos);
  1382. av_log(s->avctx, AV_LOG_INFO, "overread, skip %d enddists: %d %d\n", last_pos - pos, end_pos-pos, end_pos2-pos);
  1383. if(s->error_recognition >= FF_ER_COMPLIANT)
  1384. s_index=0;
  1385. break;
  1386. }
  1387. // av_log(NULL, AV_LOG_ERROR, "pos2: %d %d %d %d\n", pos, end_pos, end_pos2, s_index);
  1388. switch_buffer(s, &pos, &end_pos, &end_pos2);
  1389. // av_log(NULL, AV_LOG_ERROR, "new pos2: %d %d %d\n", pos, end_pos, s_index);
  1390. if(pos >= end_pos)
  1391. break;
  1392. }
  1393. last_pos= pos;
  1394. code = get_vlc2(&s->gb, vlc->table, vlc->bits, 1);
  1395. dprintf(s->avctx, "t=%d code=%d\n", g->count1table_select, code);
  1396. g->sb_hybrid[s_index+0]=
  1397. g->sb_hybrid[s_index+1]=
  1398. g->sb_hybrid[s_index+2]=
  1399. g->sb_hybrid[s_index+3]= 0;
  1400. while(code){
  1401. static const int idxtab[16]={3,3,2,2,1,1,1,1,0,0,0,0,0,0,0,0};
  1402. int v;
  1403. int pos= s_index+idxtab[code];
  1404. code ^= 8>>idxtab[code];
  1405. v = exp_table[ exponents[pos] ];
  1406. // v = exp_table[ (exponents[pos]&3) ] >> FFMIN(0 - (exponents[pos]>>2), 31);
  1407. if(get_bits1(&s->gb))
  1408. v = -v;
  1409. g->sb_hybrid[pos] = v;
  1410. }
  1411. s_index+=4;
  1412. }
  1413. /* skip extension bits */
  1414. bits_left = end_pos2 - get_bits_count(&s->gb);
  1415. //av_log(NULL, AV_LOG_ERROR, "left:%d buf:%p\n", bits_left, s->in_gb.buffer);
  1416. if (bits_left < 0 && s->error_recognition >= FF_ER_COMPLIANT) {
  1417. av_log(s->avctx, AV_LOG_ERROR, "bits_left=%d\n", bits_left);
  1418. s_index=0;
  1419. }else if(bits_left > 0 && s->error_recognition >= FF_ER_AGGRESSIVE){
  1420. av_log(s->avctx, AV_LOG_ERROR, "bits_left=%d\n", bits_left);
  1421. s_index=0;
  1422. }
  1423. memset(&g->sb_hybrid[s_index], 0, sizeof(*g->sb_hybrid)*(576 - s_index));
  1424. skip_bits_long(&s->gb, bits_left);
  1425. i= get_bits_count(&s->gb);
  1426. switch_buffer(s, &i, &end_pos, &end_pos2);
  1427. return 0;
  1428. }
  1429. /* Reorder short blocks from bitstream order to interleaved order. It
  1430. would be faster to do it in parsing, but the code would be far more
  1431. complicated */
  1432. static void reorder_block(MPADecodeContext *s, GranuleDef *g)
  1433. {
  1434. int i, j, len;
  1435. int32_t *ptr, *dst, *ptr1;
  1436. int32_t tmp[576];
  1437. if (g->block_type != 2)
  1438. return;
  1439. if (g->switch_point) {
  1440. if (s->sample_rate_index != 8) {
  1441. ptr = g->sb_hybrid + 36;
  1442. } else {
  1443. ptr = g->sb_hybrid + 48;
  1444. }
  1445. } else {
  1446. ptr = g->sb_hybrid;
  1447. }
  1448. for(i=g->short_start;i<13;i++) {
  1449. len = band_size_short[s->sample_rate_index][i];
  1450. ptr1 = ptr;
  1451. dst = tmp;
  1452. for(j=len;j>0;j--) {
  1453. *dst++ = ptr[0*len];
  1454. *dst++ = ptr[1*len];
  1455. *dst++ = ptr[2*len];
  1456. ptr++;
  1457. }
  1458. ptr+=2*len;
  1459. memcpy(ptr1, tmp, len * 3 * sizeof(*ptr1));
  1460. }
  1461. }
  1462. #define ISQRT2 FIXR(0.70710678118654752440)
  1463. static void compute_stereo(MPADecodeContext *s,
  1464. GranuleDef *g0, GranuleDef *g1)
  1465. {
  1466. int i, j, k, l;
  1467. int32_t v1, v2;
  1468. int sf_max, tmp0, tmp1, sf, len, non_zero_found;
  1469. int32_t (*is_tab)[16];
  1470. int32_t *tab0, *tab1;
  1471. int non_zero_found_short[3];
  1472. /* intensity stereo */
  1473. if (s->mode_ext & MODE_EXT_I_STEREO) {
  1474. if (!s->lsf) {
  1475. is_tab = is_table;
  1476. sf_max = 7;
  1477. } else {
  1478. is_tab = is_table_lsf[g1->scalefac_compress & 1];
  1479. sf_max = 16;
  1480. }
  1481. tab0 = g0->sb_hybrid + 576;
  1482. tab1 = g1->sb_hybrid + 576;
  1483. non_zero_found_short[0] = 0;
  1484. non_zero_found_short[1] = 0;
  1485. non_zero_found_short[2] = 0;
  1486. k = (13 - g1->short_start) * 3 + g1->long_end - 3;
  1487. for(i = 12;i >= g1->short_start;i--) {
  1488. /* for last band, use previous scale factor */
  1489. if (i != 11)
  1490. k -= 3;
  1491. len = band_size_short[s->sample_rate_index][i];
  1492. for(l=2;l>=0;l--) {
  1493. tab0 -= len;
  1494. tab1 -= len;
  1495. if (!non_zero_found_short[l]) {
  1496. /* test if non zero band. if so, stop doing i-stereo */
  1497. for(j=0;j<len;j++) {
  1498. if (tab1[j] != 0) {
  1499. non_zero_found_short[l] = 1;
  1500. goto found1;
  1501. }
  1502. }
  1503. sf = g1->scale_factors[k + l];
  1504. if (sf >= sf_max)
  1505. goto found1;
  1506. v1 = is_tab[0][sf];
  1507. v2 = is_tab[1][sf];
  1508. for(j=0;j<len;j++) {
  1509. tmp0 = tab0[j];
  1510. tab0[j] = MULL(tmp0, v1, FRAC_BITS);
  1511. tab1[j] = MULL(tmp0, v2, FRAC_BITS);
  1512. }
  1513. } else {
  1514. found1:
  1515. if (s->mode_ext & MODE_EXT_MS_STEREO) {
  1516. /* lower part of the spectrum : do ms stereo
  1517. if enabled */
  1518. for(j=0;j<len;j++) {
  1519. tmp0 = tab0[j];
  1520. tmp1 = tab1[j];
  1521. tab0[j] = MULL(tmp0 + tmp1, ISQRT2, FRAC_BITS);
  1522. tab1[j] = MULL(tmp0 - tmp1, ISQRT2, FRAC_BITS);
  1523. }
  1524. }
  1525. }
  1526. }
  1527. }
  1528. non_zero_found = non_zero_found_short[0] |
  1529. non_zero_found_short[1] |
  1530. non_zero_found_short[2];
  1531. for(i = g1->long_end - 1;i >= 0;i--) {
  1532. len = band_size_long[s->sample_rate_index][i];
  1533. tab0 -= len;
  1534. tab1 -= len;
  1535. /* test if non zero band. if so, stop doing i-stereo */
  1536. if (!non_zero_found) {
  1537. for(j=0;j<len;j++) {
  1538. if (tab1[j] != 0) {
  1539. non_zero_found = 1;
  1540. goto found2;
  1541. }
  1542. }
  1543. /* for last band, use previous scale factor */
  1544. k = (i == 21) ? 20 : i;
  1545. sf = g1->scale_factors[k];
  1546. if (sf >= sf_max)
  1547. goto found2;
  1548. v1 = is_tab[0][sf];
  1549. v2 = is_tab[1][sf];
  1550. for(j=0;j<len;j++) {
  1551. tmp0 = tab0[j];
  1552. tab0[j] = MULL(tmp0, v1, FRAC_BITS);
  1553. tab1[j] = MULL(tmp0, v2, FRAC_BITS);
  1554. }
  1555. } else {
  1556. found2:
  1557. if (s->mode_ext & MODE_EXT_MS_STEREO) {
  1558. /* lower part of the spectrum : do ms stereo
  1559. if enabled */
  1560. for(j=0;j<len;j++) {
  1561. tmp0 = tab0[j];
  1562. tmp1 = tab1[j];
  1563. tab0[j] = MULL(tmp0 + tmp1, ISQRT2, FRAC_BITS);
  1564. tab1[j] = MULL(tmp0 - tmp1, ISQRT2, FRAC_BITS);
  1565. }
  1566. }
  1567. }
  1568. }
  1569. } else if (s->mode_ext & MODE_EXT_MS_STEREO) {
  1570. /* ms stereo ONLY */
  1571. /* NOTE: the 1/sqrt(2) normalization factor is included in the
  1572. global gain */
  1573. tab0 = g0->sb_hybrid;
  1574. tab1 = g1->sb_hybrid;
  1575. for(i=0;i<576;i++) {
  1576. tmp0 = tab0[i];
  1577. tmp1 = tab1[i];
  1578. tab0[i] = tmp0 + tmp1;
  1579. tab1[i] = tmp0 - tmp1;
  1580. }
  1581. }
  1582. }
  1583. static void compute_antialias_integer(MPADecodeContext *s,
  1584. GranuleDef *g)
  1585. {
  1586. int32_t *ptr, *csa;
  1587. int n, i;
  1588. /* we antialias only "long" bands */
  1589. if (g->block_type == 2) {
  1590. if (!g->switch_point)
  1591. return;
  1592. /* XXX: check this for 8000Hz case */
  1593. n = 1;
  1594. } else {
  1595. n = SBLIMIT - 1;
  1596. }
  1597. ptr = g->sb_hybrid + 18;
  1598. for(i = n;i > 0;i--) {
  1599. int tmp0, tmp1, tmp2;
  1600. csa = &csa_table[0][0];
  1601. #define INT_AA(j) \
  1602. tmp0 = ptr[-1-j];\
  1603. tmp1 = ptr[ j];\
  1604. tmp2= MULH(tmp0 + tmp1, csa[0+4*j]);\
  1605. ptr[-1-j] = 4*(tmp2 - MULH(tmp1, csa[2+4*j]));\
  1606. ptr[ j] = 4*(tmp2 + MULH(tmp0, csa[3+4*j]));
  1607. INT_AA(0)
  1608. INT_AA(1)
  1609. INT_AA(2)
  1610. INT_AA(3)
  1611. INT_AA(4)
  1612. INT_AA(5)
  1613. INT_AA(6)
  1614. INT_AA(7)
  1615. ptr += 18;
  1616. }
  1617. }
  1618. static void compute_antialias_float(MPADecodeContext *s,
  1619. GranuleDef *g)
  1620. {
  1621. int32_t *ptr;
  1622. int n, i;
  1623. /* we antialias only "long" bands */
  1624. if (g->block_type == 2) {
  1625. if (!g->switch_point)
  1626. return;
  1627. /* XXX: check this for 8000Hz case */
  1628. n = 1;
  1629. } else {
  1630. n = SBLIMIT - 1;
  1631. }
  1632. ptr = g->sb_hybrid + 18;
  1633. for(i = n;i > 0;i--) {
  1634. float tmp0, tmp1;
  1635. float *csa = &csa_table_float[0][0];
  1636. #define FLOAT_AA(j)\
  1637. tmp0= ptr[-1-j];\
  1638. tmp1= ptr[ j];\
  1639. ptr[-1-j] = lrintf(tmp0 * csa[0+4*j] - tmp1 * csa[1+4*j]);\
  1640. ptr[ j] = lrintf(tmp0 * csa[1+4*j] + tmp1 * csa[0+4*j]);
  1641. FLOAT_AA(0)
  1642. FLOAT_AA(1)
  1643. FLOAT_AA(2)
  1644. FLOAT_AA(3)
  1645. FLOAT_AA(4)
  1646. FLOAT_AA(5)
  1647. FLOAT_AA(6)
  1648. FLOAT_AA(7)
  1649. ptr += 18;
  1650. }
  1651. }
  1652. static void compute_imdct(MPADecodeContext *s,
  1653. GranuleDef *g,
  1654. int32_t *sb_samples,
  1655. int32_t *mdct_buf)
  1656. {
  1657. int32_t *ptr, *win, *win1, *buf, *out_ptr, *ptr1;
  1658. int32_t out2[12];
  1659. int i, j, mdct_long_end, v, sblimit;
  1660. /* find last non zero block */
  1661. ptr = g->sb_hybrid + 576;
  1662. ptr1 = g->sb_hybrid + 2 * 18;
  1663. while (ptr >= ptr1) {
  1664. ptr -= 6;
  1665. v = ptr[0] | ptr[1] | ptr[2] | ptr[3] | ptr[4] | ptr[5];
  1666. if (v != 0)
  1667. break;
  1668. }
  1669. sblimit = ((ptr - g->sb_hybrid) / 18) + 1;
  1670. if (g->block_type == 2) {
  1671. /* XXX: check for 8000 Hz */
  1672. if (g->switch_point)
  1673. mdct_long_end = 2;
  1674. else
  1675. mdct_long_end = 0;
  1676. } else {
  1677. mdct_long_end = sblimit;
  1678. }
  1679. buf = mdct_buf;
  1680. ptr = g->sb_hybrid;
  1681. for(j=0;j<mdct_long_end;j++) {
  1682. /* apply window & overlap with previous buffer */
  1683. out_ptr = sb_samples + j;
  1684. /* select window */
  1685. if (g->switch_point && j < 2)
  1686. win1 = mdct_win[0];
  1687. else
  1688. win1 = mdct_win[g->block_type];
  1689. /* select frequency inversion */
  1690. win = win1 + ((4 * 36) & -(j & 1));
  1691. imdct36(out_ptr, buf, ptr, win);
  1692. out_ptr += 18*SBLIMIT;
  1693. ptr += 18;
  1694. buf += 18;
  1695. }
  1696. for(j=mdct_long_end;j<sblimit;j++) {
  1697. /* select frequency inversion */
  1698. win = mdct_win[2] + ((4 * 36) & -(j & 1));
  1699. out_ptr = sb_samples + j;
  1700. for(i=0; i<6; i++){
  1701. *out_ptr = buf[i];
  1702. out_ptr += SBLIMIT;
  1703. }
  1704. imdct12(out2, ptr + 0);
  1705. for(i=0;i<6;i++) {
  1706. *out_ptr = MULH(out2[i], win[i]) + buf[i + 6*1];
  1707. buf[i + 6*2] = MULH(out2[i + 6], win[i + 6]);
  1708. out_ptr += SBLIMIT;
  1709. }
  1710. imdct12(out2, ptr + 1);
  1711. for(i=0;i<6;i++) {
  1712. *out_ptr = MULH(out2[i], win[i]) + buf[i + 6*2];
  1713. buf[i + 6*0] = MULH(out2[i + 6], win[i + 6]);
  1714. out_ptr += SBLIMIT;
  1715. }
  1716. imdct12(out2, ptr + 2);
  1717. for(i=0;i<6;i++) {
  1718. buf[i + 6*0] = MULH(out2[i], win[i]) + buf[i + 6*0];
  1719. buf[i + 6*1] = MULH(out2[i + 6], win[i + 6]);
  1720. buf[i + 6*2] = 0;
  1721. }
  1722. ptr += 18;
  1723. buf += 18;
  1724. }
  1725. /* zero bands */
  1726. for(j=sblimit;j<SBLIMIT;j++) {
  1727. /* overlap */
  1728. out_ptr = sb_samples + j;
  1729. for(i=0;i<18;i++) {
  1730. *out_ptr = buf[i];
  1731. buf[i] = 0;
  1732. out_ptr += SBLIMIT;
  1733. }
  1734. buf += 18;
  1735. }
  1736. }
  1737. /* main layer3 decoding function */
  1738. static int mp_decode_layer3(MPADecodeContext *s)
  1739. {
  1740. int nb_granules, main_data_begin, private_bits;
  1741. int gr, ch, blocksplit_flag, i, j, k, n, bits_pos;
  1742. GranuleDef granules[2][2], *g;
  1743. int16_t exponents[576];
  1744. /* read side info */
  1745. if (s->lsf) {
  1746. main_data_begin = get_bits(&s->gb, 8);
  1747. private_bits = get_bits(&s->gb, s->nb_channels);
  1748. nb_granules = 1;
  1749. } else {
  1750. main_data_begin = get_bits(&s->gb, 9);
  1751. if (s->nb_channels == 2)
  1752. private_bits = get_bits(&s->gb, 3);
  1753. else
  1754. private_bits = get_bits(&s->gb, 5);
  1755. nb_granules = 2;
  1756. for(ch=0;ch<s->nb_channels;ch++) {
  1757. granules[ch][0].scfsi = 0; /* all scale factors are transmitted */
  1758. granules[ch][1].scfsi = get_bits(&s->gb, 4);
  1759. }
  1760. }
  1761. for(gr=0;gr<nb_granules;gr++) {
  1762. for(ch=0;ch<s->nb_channels;ch++) {
  1763. dprintf(s->avctx, "gr=%d ch=%d: side_info\n", gr, ch);
  1764. g = &granules[ch][gr];
  1765. g->part2_3_length = get_bits(&s->gb, 12);
  1766. g->big_values = get_bits(&s->gb, 9);
  1767. if(g->big_values > 288){
  1768. av_log(s->avctx, AV_LOG_ERROR, "big_values too big\n");
  1769. return -1;
  1770. }
  1771. g->global_gain = get_bits(&s->gb, 8);
  1772. /* if MS stereo only is selected, we precompute the
  1773. 1/sqrt(2) renormalization factor */
  1774. if ((s->mode_ext & (MODE_EXT_MS_STEREO | MODE_EXT_I_STEREO)) ==
  1775. MODE_EXT_MS_STEREO)
  1776. g->global_gain -= 2;
  1777. if (s->lsf)
  1778. g->scalefac_compress = get_bits(&s->gb, 9);
  1779. else
  1780. g->scalefac_compress = get_bits(&s->gb, 4);
  1781. blocksplit_flag = get_bits1(&s->gb);
  1782. if (blocksplit_flag) {
  1783. g->block_type = get_bits(&s->gb, 2);
  1784. if (g->block_type == 0){
  1785. av_log(s->avctx, AV_LOG_ERROR, "invalid block type\n");
  1786. return -1;
  1787. }
  1788. g->switch_point = get_bits1(&s->gb);
  1789. for(i=0;i<2;i++)
  1790. g->table_select[i] = get_bits(&s->gb, 5);
  1791. for(i=0;i<3;i++)
  1792. g->subblock_gain[i] = get_bits(&s->gb, 3);
  1793. ff_init_short_region(s, g);
  1794. } else {
  1795. int region_address1, region_address2;
  1796. g->block_type = 0;
  1797. g->switch_point = 0;
  1798. for(i=0;i<3;i++)
  1799. g->table_select[i] = get_bits(&s->gb, 5);
  1800. /* compute huffman coded region sizes */
  1801. region_address1 = get_bits(&s->gb, 4);
  1802. region_address2 = get_bits(&s->gb, 3);
  1803. dprintf(s->avctx, "region1=%d region2=%d\n",
  1804. region_address1, region_address2);
  1805. ff_init_long_region(s, g, region_address1, region_address2);
  1806. }
  1807. ff_region_offset2size(g);
  1808. ff_compute_band_indexes(s, g);
  1809. g->preflag = 0;
  1810. if (!s->lsf)
  1811. g->preflag = get_bits1(&s->gb);
  1812. g->scalefac_scale = get_bits1(&s->gb);
  1813. g->count1table_select = get_bits1(&s->gb);
  1814. dprintf(s->avctx, "block_type=%d switch_point=%d\n",
  1815. g->block_type, g->switch_point);
  1816. }
  1817. }
  1818. if (!s->adu_mode) {
  1819. const uint8_t *ptr = s->gb.buffer + (get_bits_count(&s->gb)>>3);
  1820. assert((get_bits_count(&s->gb) & 7) == 0);
  1821. /* now we get bits from the main_data_begin offset */
  1822. dprintf(s->avctx, "seekback: %d\n", main_data_begin);
  1823. //av_log(NULL, AV_LOG_ERROR, "backstep:%d, lastbuf:%d\n", main_data_begin, s->last_buf_size);
  1824. memcpy(s->last_buf + s->last_buf_size, ptr, EXTRABYTES);
  1825. s->in_gb= s->gb;
  1826. init_get_bits(&s->gb, s->last_buf, s->last_buf_size*8);
  1827. skip_bits_long(&s->gb, 8*(s->last_buf_size - main_data_begin));
  1828. }
  1829. for(gr=0;gr<nb_granules;gr++) {
  1830. for(ch=0;ch<s->nb_channels;ch++) {
  1831. g = &granules[ch][gr];
  1832. if(get_bits_count(&s->gb)<0){
  1833. av_log(s->avctx, AV_LOG_ERROR, "mdb:%d, lastbuf:%d skipping granule %d\n",
  1834. main_data_begin, s->last_buf_size, gr);
  1835. skip_bits_long(&s->gb, g->part2_3_length);
  1836. memset(g->sb_hybrid, 0, sizeof(g->sb_hybrid));
  1837. if(get_bits_count(&s->gb) >= s->gb.size_in_bits && s->in_gb.buffer){
  1838. skip_bits_long(&s->in_gb, get_bits_count(&s->gb) - s->gb.size_in_bits);
  1839. s->gb= s->in_gb;
  1840. s->in_gb.buffer=NULL;
  1841. }
  1842. continue;
  1843. }
  1844. bits_pos = get_bits_count(&s->gb);
  1845. if (!s->lsf) {
  1846. uint8_t *sc;
  1847. int slen, slen1, slen2;
  1848. /* MPEG1 scale factors */
  1849. slen1 = slen_table[0][g->scalefac_compress];
  1850. slen2 = slen_table[1][g->scalefac_compress];
  1851. dprintf(s->avctx, "slen1=%d slen2=%d\n", slen1, slen2);
  1852. if (g->block_type == 2) {
  1853. n = g->switch_point ? 17 : 18;
  1854. j = 0;
  1855. if(slen1){
  1856. for(i=0;i<n;i++)
  1857. g->scale_factors[j++] = get_bits(&s->gb, slen1);
  1858. }else{
  1859. for(i=0;i<n;i++)
  1860. g->scale_factors[j++] = 0;
  1861. }
  1862. if(slen2){
  1863. for(i=0;i<18;i++)
  1864. g->scale_factors[j++] = get_bits(&s->gb, slen2);
  1865. for(i=0;i<3;i++)
  1866. g->scale_factors[j++] = 0;
  1867. }else{
  1868. for(i=0;i<21;i++)
  1869. g->scale_factors[j++] = 0;
  1870. }
  1871. } else {
  1872. sc = granules[ch][0].scale_factors;
  1873. j = 0;
  1874. for(k=0;k<4;k++) {
  1875. n = (k == 0 ? 6 : 5);
  1876. if ((g->scfsi & (0x8 >> k)) == 0) {
  1877. slen = (k < 2) ? slen1 : slen2;
  1878. if(slen){
  1879. for(i=0;i<n;i++)
  1880. g->scale_factors[j++] = get_bits(&s->gb, slen);
  1881. }else{
  1882. for(i=0;i<n;i++)
  1883. g->scale_factors[j++] = 0;
  1884. }
  1885. } else {
  1886. /* simply copy from last granule */
  1887. for(i=0;i<n;i++) {
  1888. g->scale_factors[j] = sc[j];
  1889. j++;
  1890. }
  1891. }
  1892. }
  1893. g->scale_factors[j++] = 0;
  1894. }
  1895. } else {
  1896. int tindex, tindex2, slen[4], sl, sf;
  1897. /* LSF scale factors */
  1898. if (g->block_type == 2) {
  1899. tindex = g->switch_point ? 2 : 1;
  1900. } else {
  1901. tindex = 0;
  1902. }
  1903. sf = g->scalefac_compress;
  1904. if ((s->mode_ext & MODE_EXT_I_STEREO) && ch == 1) {
  1905. /* intensity stereo case */
  1906. sf >>= 1;
  1907. if (sf < 180) {
  1908. lsf_sf_expand(slen, sf, 6, 6, 0);
  1909. tindex2 = 3;
  1910. } else if (sf < 244) {
  1911. lsf_sf_expand(slen, sf - 180, 4, 4, 0);
  1912. tindex2 = 4;
  1913. } else {
  1914. lsf_sf_expand(slen, sf - 244, 3, 0, 0);
  1915. tindex2 = 5;
  1916. }
  1917. } else {
  1918. /* normal case */
  1919. if (sf < 400) {
  1920. lsf_sf_expand(slen, sf, 5, 4, 4);
  1921. tindex2 = 0;
  1922. } else if (sf < 500) {
  1923. lsf_sf_expand(slen, sf - 400, 5, 4, 0);
  1924. tindex2 = 1;
  1925. } else {
  1926. lsf_sf_expand(slen, sf - 500, 3, 0, 0);
  1927. tindex2 = 2;
  1928. g->preflag = 1;
  1929. }
  1930. }
  1931. j = 0;
  1932. for(k=0;k<4;k++) {
  1933. n = lsf_nsf_table[tindex2][tindex][k];
  1934. sl = slen[k];
  1935. if(sl){
  1936. for(i=0;i<n;i++)
  1937. g->scale_factors[j++] = get_bits(&s->gb, sl);
  1938. }else{
  1939. for(i=0;i<n;i++)
  1940. g->scale_factors[j++] = 0;
  1941. }
  1942. }
  1943. /* XXX: should compute exact size */
  1944. for(;j<40;j++)
  1945. g->scale_factors[j] = 0;
  1946. }
  1947. exponents_from_scale_factors(s, g, exponents);
  1948. /* read Huffman coded residue */
  1949. huffman_decode(s, g, exponents, bits_pos + g->part2_3_length);
  1950. } /* ch */
  1951. if (s->nb_channels == 2)
  1952. compute_stereo(s, &granules[0][gr], &granules[1][gr]);
  1953. for(ch=0;ch<s->nb_channels;ch++) {
  1954. g = &granules[ch][gr];
  1955. reorder_block(s, g);
  1956. s->compute_antialias(s, g);
  1957. compute_imdct(s, g, &s->sb_samples[ch][18 * gr][0], s->mdct_buf[ch]);
  1958. }
  1959. } /* gr */
  1960. if(get_bits_count(&s->gb)<0)
  1961. skip_bits_long(&s->gb, -get_bits_count(&s->gb));
  1962. return nb_granules * 18;
  1963. }
  1964. static int mp_decode_frame(MPADecodeContext *s,
  1965. OUT_INT *samples, const uint8_t *buf, int buf_size)
  1966. {
  1967. int i, nb_frames, ch;
  1968. OUT_INT *samples_ptr;
  1969. init_get_bits(&s->gb, buf + HEADER_SIZE, (buf_size - HEADER_SIZE)*8);
  1970. /* skip error protection field */
  1971. if (s->error_protection)
  1972. skip_bits(&s->gb, 16);
  1973. dprintf(s->avctx, "frame %d:\n", s->frame_count);
  1974. switch(s->layer) {
  1975. case 1:
  1976. s->avctx->frame_size = 384;
  1977. nb_frames = mp_decode_layer1(s);
  1978. break;
  1979. case 2:
  1980. s->avctx->frame_size = 1152;
  1981. nb_frames = mp_decode_layer2(s);
  1982. break;
  1983. case 3:
  1984. s->avctx->frame_size = s->lsf ? 576 : 1152;
  1985. default:
  1986. nb_frames = mp_decode_layer3(s);
  1987. s->last_buf_size=0;
  1988. if(s->in_gb.buffer){
  1989. align_get_bits(&s->gb);
  1990. i= (s->gb.size_in_bits - get_bits_count(&s->gb))>>3;
  1991. if(i >= 0 && i <= BACKSTEP_SIZE){
  1992. memmove(s->last_buf, s->gb.buffer + (get_bits_count(&s->gb)>>3), i);
  1993. s->last_buf_size=i;
  1994. }else
  1995. av_log(s->avctx, AV_LOG_ERROR, "invalid old backstep %d\n", i);
  1996. s->gb= s->in_gb;
  1997. s->in_gb.buffer= NULL;
  1998. }
  1999. align_get_bits(&s->gb);
  2000. assert((get_bits_count(&s->gb) & 7) == 0);
  2001. i= (s->gb.size_in_bits - get_bits_count(&s->gb))>>3;
  2002. if(i<0 || i > BACKSTEP_SIZE || nb_frames<0){
  2003. if(i<0)
  2004. av_log(s->avctx, AV_LOG_ERROR, "invalid new backstep %d\n", i);
  2005. i= FFMIN(BACKSTEP_SIZE, buf_size - HEADER_SIZE);
  2006. }
  2007. assert(i <= buf_size - HEADER_SIZE && i>= 0);
  2008. memcpy(s->last_buf + s->last_buf_size, s->gb.buffer + buf_size - HEADER_SIZE - i, i);
  2009. s->last_buf_size += i;
  2010. break;
  2011. }
  2012. /* apply the synthesis filter */
  2013. for(ch=0;ch<s->nb_channels;ch++) {
  2014. samples_ptr = samples + ch;
  2015. for(i=0;i<nb_frames;i++) {
  2016. ff_mpa_synth_filter(s->synth_buf[ch], &(s->synth_buf_offset[ch]),
  2017. window, &s->dither_state,
  2018. samples_ptr, s->nb_channels,
  2019. s->sb_samples[ch][i]);
  2020. samples_ptr += 32 * s->nb_channels;
  2021. }
  2022. }
  2023. return nb_frames * 32 * sizeof(OUT_INT) * s->nb_channels;
  2024. }
  2025. static int decode_frame(AVCodecContext * avctx,
  2026. void *data, int *data_size,
  2027. const uint8_t * buf, int buf_size)
  2028. {
  2029. MPADecodeContext *s = avctx->priv_data;
  2030. uint32_t header;
  2031. int out_size;
  2032. OUT_INT *out_samples = data;
  2033. retry:
  2034. if(buf_size < HEADER_SIZE)
  2035. return -1;
  2036. header = AV_RB32(buf);
  2037. if(ff_mpa_check_header(header) < 0){
  2038. buf++;
  2039. // buf_size--;
  2040. av_log(avctx, AV_LOG_ERROR, "Header missing skipping one byte.\n");
  2041. goto retry;
  2042. }
  2043. if (ff_mpegaudio_decode_header((MPADecodeHeader *)s, header) == 1) {
  2044. /* free format: prepare to compute frame size */
  2045. s->frame_size = -1;
  2046. return -1;
  2047. }
  2048. /* update codec info */
  2049. avctx->channels = s->nb_channels;
  2050. avctx->bit_rate = s->bit_rate;
  2051. avctx->sub_id = s->layer;
  2052. if(*data_size < 1152*avctx->channels*sizeof(OUT_INT))
  2053. return -1;
  2054. *data_size = 0;
  2055. if(s->frame_size<=0 || s->frame_size > buf_size){
  2056. av_log(avctx, AV_LOG_ERROR, "incomplete frame\n");
  2057. return -1;
  2058. }else if(s->frame_size < buf_size){
  2059. av_log(avctx, AV_LOG_ERROR, "incorrect frame size\n");
  2060. buf_size= s->frame_size;
  2061. }
  2062. out_size = mp_decode_frame(s, out_samples, buf, buf_size);
  2063. if(out_size>=0){
  2064. *data_size = out_size;
  2065. avctx->sample_rate = s->sample_rate;
  2066. //FIXME maybe move the other codec info stuff from above here too
  2067. }else
  2068. av_log(avctx, AV_LOG_DEBUG, "Error while decoding MPEG audio frame.\n"); //FIXME return -1 / but also return the number of bytes consumed
  2069. s->frame_size = 0;
  2070. return buf_size;
  2071. }
  2072. static void flush(AVCodecContext *avctx){
  2073. MPADecodeContext *s = avctx->priv_data;
  2074. memset(s->synth_buf, 0, sizeof(s->synth_buf));
  2075. s->last_buf_size= 0;
  2076. }
  2077. #if CONFIG_MP3ADU_DECODER
  2078. static int decode_frame_adu(AVCodecContext * avctx,
  2079. void *data, int *data_size,
  2080. const uint8_t * buf, int buf_size)
  2081. {
  2082. MPADecodeContext *s = avctx->priv_data;
  2083. uint32_t header;
  2084. int len, out_size;
  2085. OUT_INT *out_samples = data;
  2086. len = buf_size;
  2087. // Discard too short frames
  2088. if (buf_size < HEADER_SIZE) {
  2089. *data_size = 0;
  2090. return buf_size;
  2091. }
  2092. if (len > MPA_MAX_CODED_FRAME_SIZE)
  2093. len = MPA_MAX_CODED_FRAME_SIZE;
  2094. // Get header and restore sync word
  2095. header = AV_RB32(buf) | 0xffe00000;
  2096. if (ff_mpa_check_header(header) < 0) { // Bad header, discard frame
  2097. *data_size = 0;
  2098. return buf_size;
  2099. }
  2100. ff_mpegaudio_decode_header((MPADecodeHeader *)s, header);
  2101. /* update codec info */
  2102. avctx->sample_rate = s->sample_rate;
  2103. avctx->channels = s->nb_channels;
  2104. avctx->bit_rate = s->bit_rate;
  2105. avctx->sub_id = s->layer;
  2106. s->frame_size = len;
  2107. if (avctx->parse_only) {
  2108. out_size = buf_size;
  2109. } else {
  2110. out_size = mp_decode_frame(s, out_samples, buf, buf_size);
  2111. }
  2112. *data_size = out_size;
  2113. return buf_size;
  2114. }
  2115. #endif /* CONFIG_MP3ADU_DECODER */
  2116. #if CONFIG_MP3ON4_DECODER
  2117. /**
  2118. * Context for MP3On4 decoder
  2119. */
  2120. typedef struct MP3On4DecodeContext {
  2121. int frames; ///< number of mp3 frames per block (number of mp3 decoder instances)
  2122. int syncword; ///< syncword patch
  2123. const uint8_t *coff; ///< channels offsets in output buffer
  2124. MPADecodeContext *mp3decctx[5]; ///< MPADecodeContext for every decoder instance
  2125. } MP3On4DecodeContext;
  2126. #include "mpeg4audio.h"
  2127. /* Next 3 arrays are indexed by channel config number (passed via codecdata) */
  2128. static const uint8_t mp3Frames[8] = {0,1,1,2,3,3,4,5}; /* number of mp3 decoder instances */
  2129. /* offsets into output buffer, assume output order is FL FR BL BR C LFE */
  2130. static const uint8_t chan_offset[8][5] = {
  2131. {0},
  2132. {0}, // C
  2133. {0}, // FLR
  2134. {2,0}, // C FLR
  2135. {2,0,3}, // C FLR BS
  2136. {4,0,2}, // C FLR BLRS
  2137. {4,0,2,5}, // C FLR BLRS LFE
  2138. {4,0,2,6,5}, // C FLR BLRS BLR LFE
  2139. };
  2140. static int decode_init_mp3on4(AVCodecContext * avctx)
  2141. {
  2142. MP3On4DecodeContext *s = avctx->priv_data;
  2143. MPEG4AudioConfig cfg;
  2144. int i;
  2145. if ((avctx->extradata_size < 2) || (avctx->extradata == NULL)) {
  2146. av_log(avctx, AV_LOG_ERROR, "Codec extradata missing or too short.\n");
  2147. return -1;
  2148. }
  2149. ff_mpeg4audio_get_config(&cfg, avctx->extradata, avctx->extradata_size);
  2150. if (!cfg.chan_config || cfg.chan_config > 7) {
  2151. av_log(avctx, AV_LOG_ERROR, "Invalid channel config number.\n");
  2152. return -1;
  2153. }
  2154. s->frames = mp3Frames[cfg.chan_config];
  2155. s->coff = chan_offset[cfg.chan_config];
  2156. avctx->channels = ff_mpeg4audio_channels[cfg.chan_config];
  2157. if (cfg.sample_rate < 16000)
  2158. s->syncword = 0xffe00000;
  2159. else
  2160. s->syncword = 0xfff00000;
  2161. /* Init the first mp3 decoder in standard way, so that all tables get builded
  2162. * We replace avctx->priv_data with the context of the first decoder so that
  2163. * decode_init() does not have to be changed.
  2164. * Other decoders will be initialized here copying data from the first context
  2165. */
  2166. // Allocate zeroed memory for the first decoder context
  2167. s->mp3decctx[0] = av_mallocz(sizeof(MPADecodeContext));
  2168. // Put decoder context in place to make init_decode() happy
  2169. avctx->priv_data = s->mp3decctx[0];
  2170. decode_init(avctx);
  2171. // Restore mp3on4 context pointer
  2172. avctx->priv_data = s;
  2173. s->mp3decctx[0]->adu_mode = 1; // Set adu mode
  2174. /* Create a separate codec/context for each frame (first is already ok).
  2175. * Each frame is 1 or 2 channels - up to 5 frames allowed
  2176. */
  2177. for (i = 1; i < s->frames; i++) {
  2178. s->mp3decctx[i] = av_mallocz(sizeof(MPADecodeContext));
  2179. s->mp3decctx[i]->compute_antialias = s->mp3decctx[0]->compute_antialias;
  2180. s->mp3decctx[i]->adu_mode = 1;
  2181. s->mp3decctx[i]->avctx = avctx;
  2182. }
  2183. return 0;
  2184. }
  2185. static av_cold int decode_close_mp3on4(AVCodecContext * avctx)
  2186. {
  2187. MP3On4DecodeContext *s = avctx->priv_data;
  2188. int i;
  2189. for (i = 0; i < s->frames; i++)
  2190. if (s->mp3decctx[i])
  2191. av_free(s->mp3decctx[i]);
  2192. return 0;
  2193. }
  2194. static int decode_frame_mp3on4(AVCodecContext * avctx,
  2195. void *data, int *data_size,
  2196. const uint8_t * buf, int buf_size)
  2197. {
  2198. MP3On4DecodeContext *s = avctx->priv_data;
  2199. MPADecodeContext *m;
  2200. int fsize, len = buf_size, out_size = 0;
  2201. uint32_t header;
  2202. OUT_INT *out_samples = data;
  2203. OUT_INT decoded_buf[MPA_FRAME_SIZE * MPA_MAX_CHANNELS];
  2204. OUT_INT *outptr, *bp;
  2205. int fr, j, n;
  2206. if(*data_size < MPA_FRAME_SIZE * MPA_MAX_CHANNELS * s->frames * sizeof(OUT_INT))
  2207. return -1;
  2208. *data_size = 0;
  2209. // Discard too short frames
  2210. if (buf_size < HEADER_SIZE)
  2211. return -1;
  2212. // If only one decoder interleave is not needed
  2213. outptr = s->frames == 1 ? out_samples : decoded_buf;
  2214. avctx->bit_rate = 0;
  2215. for (fr = 0; fr < s->frames; fr++) {
  2216. fsize = AV_RB16(buf) >> 4;
  2217. fsize = FFMIN3(fsize, len, MPA_MAX_CODED_FRAME_SIZE);
  2218. m = s->mp3decctx[fr];
  2219. assert (m != NULL);
  2220. header = (AV_RB32(buf) & 0x000fffff) | s->syncword; // patch header
  2221. if (ff_mpa_check_header(header) < 0) // Bad header, discard block
  2222. break;
  2223. ff_mpegaudio_decode_header((MPADecodeHeader *)m, header);
  2224. out_size += mp_decode_frame(m, outptr, buf, fsize);
  2225. buf += fsize;
  2226. len -= fsize;
  2227. if(s->frames > 1) {
  2228. n = m->avctx->frame_size*m->nb_channels;
  2229. /* interleave output data */
  2230. bp = out_samples + s->coff[fr];
  2231. if(m->nb_channels == 1) {
  2232. for(j = 0; j < n; j++) {
  2233. *bp = decoded_buf[j];
  2234. bp += avctx->channels;
  2235. }
  2236. } else {
  2237. for(j = 0; j < n; j++) {
  2238. bp[0] = decoded_buf[j++];
  2239. bp[1] = decoded_buf[j];
  2240. bp += avctx->channels;
  2241. }
  2242. }
  2243. }
  2244. avctx->bit_rate += m->bit_rate;
  2245. }
  2246. /* update codec info */
  2247. avctx->sample_rate = s->mp3decctx[0]->sample_rate;
  2248. *data_size = out_size;
  2249. return buf_size;
  2250. }
  2251. #endif /* CONFIG_MP3ON4_DECODER */
  2252. #if CONFIG_MP1_DECODER
  2253. AVCodec mp1_decoder =
  2254. {
  2255. "mp1",
  2256. CODEC_TYPE_AUDIO,
  2257. CODEC_ID_MP1,
  2258. sizeof(MPADecodeContext),
  2259. decode_init,
  2260. NULL,
  2261. NULL,
  2262. decode_frame,
  2263. CODEC_CAP_PARSE_ONLY,
  2264. .flush= flush,
  2265. .long_name= NULL_IF_CONFIG_SMALL("MP1 (MPEG audio layer 1)"),
  2266. };
  2267. #endif
  2268. #if CONFIG_MP2_DECODER
  2269. AVCodec mp2_decoder =
  2270. {
  2271. "mp2",
  2272. CODEC_TYPE_AUDIO,
  2273. CODEC_ID_MP2,
  2274. sizeof(MPADecodeContext),
  2275. decode_init,
  2276. NULL,
  2277. NULL,
  2278. decode_frame,
  2279. CODEC_CAP_PARSE_ONLY,
  2280. .flush= flush,
  2281. .long_name= NULL_IF_CONFIG_SMALL("MP2 (MPEG audio layer 2)"),
  2282. };
  2283. #endif
  2284. #if CONFIG_MP3_DECODER
  2285. AVCodec mp3_decoder =
  2286. {
  2287. "mp3",
  2288. CODEC_TYPE_AUDIO,
  2289. CODEC_ID_MP3,
  2290. sizeof(MPADecodeContext),
  2291. decode_init,
  2292. NULL,
  2293. NULL,
  2294. decode_frame,
  2295. CODEC_CAP_PARSE_ONLY,
  2296. .flush= flush,
  2297. .long_name= NULL_IF_CONFIG_SMALL("MP3 (MPEG audio layer 3)"),
  2298. };
  2299. #endif
  2300. #if CONFIG_MP3ADU_DECODER
  2301. AVCodec mp3adu_decoder =
  2302. {
  2303. "mp3adu",
  2304. CODEC_TYPE_AUDIO,
  2305. CODEC_ID_MP3ADU,
  2306. sizeof(MPADecodeContext),
  2307. decode_init,
  2308. NULL,
  2309. NULL,
  2310. decode_frame_adu,
  2311. CODEC_CAP_PARSE_ONLY,
  2312. .flush= flush,
  2313. .long_name= NULL_IF_CONFIG_SMALL("ADU (Application Data Unit) MP3 (MPEG audio layer 3)"),
  2314. };
  2315. #endif
  2316. #if CONFIG_MP3ON4_DECODER
  2317. AVCodec mp3on4_decoder =
  2318. {
  2319. "mp3on4",
  2320. CODEC_TYPE_AUDIO,
  2321. CODEC_ID_MP3ON4,
  2322. sizeof(MP3On4DecodeContext),
  2323. decode_init_mp3on4,
  2324. NULL,
  2325. decode_close_mp3on4,
  2326. decode_frame_mp3on4,
  2327. .flush= flush,
  2328. .long_name= NULL_IF_CONFIG_SMALL("MP3onMP4"),
  2329. };
  2330. #endif