mpegaudiodec_template.c 65 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000
  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
  23. * MPEG Audio decoder
  24. */
  25. #include "libavutil/attributes.h"
  26. #include "libavutil/avassert.h"
  27. #include "libavutil/channel_layout.h"
  28. #include "libavutil/float_dsp.h"
  29. #include "libavutil/libm.h"
  30. #include "avcodec.h"
  31. #include "get_bits.h"
  32. #include "internal.h"
  33. #include "mathops.h"
  34. #include "mpegaudiodsp.h"
  35. /*
  36. * TODO:
  37. * - test lsf / mpeg25 extensively.
  38. */
  39. #include "mpegaudio.h"
  40. #include "mpegaudiodecheader.h"
  41. #define BACKSTEP_SIZE 512
  42. #define EXTRABYTES 24
  43. #define LAST_BUF_SIZE 2 * BACKSTEP_SIZE + EXTRABYTES
  44. /* layer 3 "granule" */
  45. typedef struct GranuleDef {
  46. uint8_t scfsi;
  47. int part2_3_length;
  48. int big_values;
  49. int global_gain;
  50. int scalefac_compress;
  51. uint8_t block_type;
  52. uint8_t switch_point;
  53. int table_select[3];
  54. int subblock_gain[3];
  55. uint8_t scalefac_scale;
  56. uint8_t count1table_select;
  57. int region_size[3]; /* number of huffman codes in each region */
  58. int preflag;
  59. int short_start, long_end; /* long/short band indexes */
  60. uint8_t scale_factors[40];
  61. DECLARE_ALIGNED(16, INTFLOAT, sb_hybrid)[SBLIMIT * 18]; /* 576 samples */
  62. } GranuleDef;
  63. typedef struct MPADecodeContext {
  64. MPA_DECODE_HEADER
  65. uint8_t last_buf[LAST_BUF_SIZE];
  66. int last_buf_size;
  67. int extrasize;
  68. /* next header (used in free format parsing) */
  69. uint32_t free_format_next_header;
  70. GetBitContext gb;
  71. GetBitContext in_gb;
  72. DECLARE_ALIGNED(32, MPA_INT, synth_buf)[MPA_MAX_CHANNELS][512 * 2];
  73. int synth_buf_offset[MPA_MAX_CHANNELS];
  74. DECLARE_ALIGNED(32, INTFLOAT, sb_samples)[MPA_MAX_CHANNELS][36][SBLIMIT];
  75. INTFLOAT mdct_buf[MPA_MAX_CHANNELS][SBLIMIT * 18]; /* previous samples, for layer 3 MDCT */
  76. GranuleDef granules[2][2]; /* Used in Layer 3 */
  77. int adu_mode; ///< 0 for standard mp3, 1 for adu formatted mp3
  78. int dither_state;
  79. int err_recognition;
  80. AVCodecContext* avctx;
  81. MPADSPContext mpadsp;
  82. AVFloatDSPContext *fdsp;
  83. AVFrame *frame;
  84. } MPADecodeContext;
  85. #define HEADER_SIZE 4
  86. #include "mpegaudiodata.h"
  87. #include "mpegaudiodectab.h"
  88. /* vlc structure for decoding layer 3 huffman tables */
  89. static VLC huff_vlc[16];
  90. static VLC_TYPE huff_vlc_tables[
  91. 0 + 128 + 128 + 128 + 130 + 128 + 154 + 166 +
  92. 142 + 204 + 190 + 170 + 542 + 460 + 662 + 414
  93. ][2];
  94. static const int huff_vlc_tables_sizes[16] = {
  95. 0, 128, 128, 128, 130, 128, 154, 166,
  96. 142, 204, 190, 170, 542, 460, 662, 414
  97. };
  98. static VLC huff_quad_vlc[2];
  99. static VLC_TYPE huff_quad_vlc_tables[128+16][2];
  100. static const int huff_quad_vlc_tables_sizes[2] = { 128, 16 };
  101. /* computed from band_size_long */
  102. static uint16_t band_index_long[9][23];
  103. #include "mpegaudio_tablegen.h"
  104. /* intensity stereo coef table */
  105. static INTFLOAT is_table[2][16];
  106. static INTFLOAT is_table_lsf[2][2][16];
  107. static INTFLOAT csa_table[8][4];
  108. static int16_t division_tab3[1<<6 ];
  109. static int16_t division_tab5[1<<8 ];
  110. static int16_t division_tab9[1<<11];
  111. static int16_t * const division_tabs[4] = {
  112. division_tab3, division_tab5, NULL, division_tab9
  113. };
  114. /* lower 2 bits: modulo 3, higher bits: shift */
  115. static uint16_t scale_factor_modshift[64];
  116. /* [i][j]: 2^(-j/3) * FRAC_ONE * 2^(i+2) / (2^(i+2) - 1) */
  117. static int32_t scale_factor_mult[15][3];
  118. /* mult table for layer 2 group quantization */
  119. #define SCALE_GEN(v) \
  120. { FIXR_OLD(1.0 * (v)), FIXR_OLD(0.7937005259 * (v)), FIXR_OLD(0.6299605249 * (v)) }
  121. static const int32_t scale_factor_mult2[3][3] = {
  122. SCALE_GEN(4.0 / 3.0), /* 3 steps */
  123. SCALE_GEN(4.0 / 5.0), /* 5 steps */
  124. SCALE_GEN(4.0 / 9.0), /* 9 steps */
  125. };
  126. /**
  127. * Convert region offsets to region sizes and truncate
  128. * size to big_values.
  129. */
  130. static void region_offset2size(GranuleDef *g)
  131. {
  132. int i, k, j = 0;
  133. g->region_size[2] = 576 / 2;
  134. for (i = 0; i < 3; i++) {
  135. k = FFMIN(g->region_size[i], g->big_values);
  136. g->region_size[i] = k - j;
  137. j = k;
  138. }
  139. }
  140. static void init_short_region(MPADecodeContext *s, GranuleDef *g)
  141. {
  142. if (g->block_type == 2) {
  143. if (s->sample_rate_index != 8)
  144. g->region_size[0] = (36 / 2);
  145. else
  146. g->region_size[0] = (72 / 2);
  147. } else {
  148. if (s->sample_rate_index <= 2)
  149. g->region_size[0] = (36 / 2);
  150. else if (s->sample_rate_index != 8)
  151. g->region_size[0] = (54 / 2);
  152. else
  153. g->region_size[0] = (108 / 2);
  154. }
  155. g->region_size[1] = (576 / 2);
  156. }
  157. static void init_long_region(MPADecodeContext *s, GranuleDef *g,
  158. int ra1, int ra2)
  159. {
  160. int l;
  161. g->region_size[0] = band_index_long[s->sample_rate_index][ra1 + 1] >> 1;
  162. /* should not overflow */
  163. l = FFMIN(ra1 + ra2 + 2, 22);
  164. g->region_size[1] = band_index_long[s->sample_rate_index][ l] >> 1;
  165. }
  166. static void compute_band_indexes(MPADecodeContext *s, GranuleDef *g)
  167. {
  168. if (g->block_type == 2) {
  169. if (g->switch_point) {
  170. if(s->sample_rate_index == 8)
  171. avpriv_request_sample(s->avctx, "switch point in 8khz");
  172. /* if switched mode, we handle the 36 first samples as
  173. long blocks. For 8000Hz, we handle the 72 first
  174. exponents as long blocks */
  175. if (s->sample_rate_index <= 2)
  176. g->long_end = 8;
  177. else
  178. g->long_end = 6;
  179. g->short_start = 3;
  180. } else {
  181. g->long_end = 0;
  182. g->short_start = 0;
  183. }
  184. } else {
  185. g->short_start = 13;
  186. g->long_end = 22;
  187. }
  188. }
  189. /* layer 1 unscaling */
  190. /* n = number of bits of the mantissa minus 1 */
  191. static inline int l1_unscale(int n, int mant, int scale_factor)
  192. {
  193. int shift, mod;
  194. int64_t val;
  195. shift = scale_factor_modshift[scale_factor];
  196. mod = shift & 3;
  197. shift >>= 2;
  198. val = MUL64((int)(mant + (-1U << n) + 1), scale_factor_mult[n-1][mod]);
  199. shift += n;
  200. /* NOTE: at this point, 1 <= shift >= 21 + 15 */
  201. return (int)((val + (1LL << (shift - 1))) >> shift);
  202. }
  203. static inline int l2_unscale_group(int steps, int mant, int scale_factor)
  204. {
  205. int shift, mod, val;
  206. shift = scale_factor_modshift[scale_factor];
  207. mod = shift & 3;
  208. shift >>= 2;
  209. val = (mant - (steps >> 1)) * scale_factor_mult2[steps >> 2][mod];
  210. /* NOTE: at this point, 0 <= shift <= 21 */
  211. if (shift > 0)
  212. val = (val + (1 << (shift - 1))) >> shift;
  213. return val;
  214. }
  215. /* compute value^(4/3) * 2^(exponent/4). It normalized to FRAC_BITS */
  216. static inline int l3_unscale(int value, int exponent)
  217. {
  218. unsigned int m;
  219. int e;
  220. e = table_4_3_exp [4 * value + (exponent & 3)];
  221. m = table_4_3_value[4 * value + (exponent & 3)];
  222. e -= exponent >> 2;
  223. #ifdef DEBUG
  224. if(e < 1)
  225. av_log(NULL, AV_LOG_WARNING, "l3_unscale: e is %d\n", e);
  226. #endif
  227. if (e > (SUINT)31)
  228. return 0;
  229. m = (m + ((1U << e)>>1)) >> e;
  230. return m;
  231. }
  232. static av_cold void decode_init_static(void)
  233. {
  234. int i, j, k;
  235. int offset;
  236. /* scale factors table for layer 1/2 */
  237. for (i = 0; i < 64; i++) {
  238. int shift, mod;
  239. /* 1.0 (i = 3) is normalized to 2 ^ FRAC_BITS */
  240. shift = i / 3;
  241. mod = i % 3;
  242. scale_factor_modshift[i] = mod | (shift << 2);
  243. }
  244. /* scale factor multiply for layer 1 */
  245. for (i = 0; i < 15; i++) {
  246. int n, norm;
  247. n = i + 2;
  248. norm = ((INT64_C(1) << n) * FRAC_ONE) / ((1 << n) - 1);
  249. scale_factor_mult[i][0] = MULLx(norm, FIXR(1.0 * 2.0), FRAC_BITS);
  250. scale_factor_mult[i][1] = MULLx(norm, FIXR(0.7937005259 * 2.0), FRAC_BITS);
  251. scale_factor_mult[i][2] = MULLx(norm, FIXR(0.6299605249 * 2.0), FRAC_BITS);
  252. ff_dlog(NULL, "%d: norm=%x s=%"PRIx32" %"PRIx32" %"PRIx32"\n", i,
  253. (unsigned)norm,
  254. scale_factor_mult[i][0],
  255. scale_factor_mult[i][1],
  256. scale_factor_mult[i][2]);
  257. }
  258. RENAME(ff_mpa_synth_init)(RENAME(ff_mpa_synth_window));
  259. /* huffman decode tables */
  260. offset = 0;
  261. for (i = 1; i < 16; i++) {
  262. const HuffTable *h = &mpa_huff_tables[i];
  263. int xsize, x, y;
  264. uint8_t tmp_bits [512] = { 0 };
  265. uint16_t tmp_codes[512] = { 0 };
  266. xsize = h->xsize;
  267. j = 0;
  268. for (x = 0; x < xsize; x++) {
  269. for (y = 0; y < xsize; y++) {
  270. tmp_bits [(x << 5) | y | ((x&&y)<<4)]= h->bits [j ];
  271. tmp_codes[(x << 5) | y | ((x&&y)<<4)]= h->codes[j++];
  272. }
  273. }
  274. /* XXX: fail test */
  275. huff_vlc[i].table = huff_vlc_tables+offset;
  276. huff_vlc[i].table_allocated = huff_vlc_tables_sizes[i];
  277. init_vlc(&huff_vlc[i], 7, 512,
  278. tmp_bits, 1, 1, tmp_codes, 2, 2,
  279. INIT_VLC_USE_NEW_STATIC);
  280. offset += huff_vlc_tables_sizes[i];
  281. }
  282. av_assert0(offset == FF_ARRAY_ELEMS(huff_vlc_tables));
  283. offset = 0;
  284. for (i = 0; i < 2; i++) {
  285. huff_quad_vlc[i].table = huff_quad_vlc_tables+offset;
  286. huff_quad_vlc[i].table_allocated = huff_quad_vlc_tables_sizes[i];
  287. init_vlc(&huff_quad_vlc[i], i == 0 ? 7 : 4, 16,
  288. mpa_quad_bits[i], 1, 1, mpa_quad_codes[i], 1, 1,
  289. INIT_VLC_USE_NEW_STATIC);
  290. offset += huff_quad_vlc_tables_sizes[i];
  291. }
  292. av_assert0(offset == FF_ARRAY_ELEMS(huff_quad_vlc_tables));
  293. for (i = 0; i < 9; i++) {
  294. k = 0;
  295. for (j = 0; j < 22; j++) {
  296. band_index_long[i][j] = k;
  297. k += band_size_long[i][j];
  298. }
  299. band_index_long[i][22] = k;
  300. }
  301. /* compute n ^ (4/3) and store it in mantissa/exp format */
  302. mpegaudio_tableinit();
  303. for (i = 0; i < 4; i++) {
  304. if (ff_mpa_quant_bits[i] < 0) {
  305. for (j = 0; j < (1 << (-ff_mpa_quant_bits[i]+1)); j++) {
  306. int val1, val2, val3, steps;
  307. int val = j;
  308. steps = ff_mpa_quant_steps[i];
  309. val1 = val % steps;
  310. val /= steps;
  311. val2 = val % steps;
  312. val3 = val / steps;
  313. division_tabs[i][j] = val1 + (val2 << 4) + (val3 << 8);
  314. }
  315. }
  316. }
  317. for (i = 0; i < 7; i++) {
  318. float f;
  319. INTFLOAT v;
  320. if (i != 6) {
  321. f = tan((double)i * M_PI / 12.0);
  322. v = FIXR(f / (1.0 + f));
  323. } else {
  324. v = FIXR(1.0);
  325. }
  326. is_table[0][ i] = v;
  327. is_table[1][6 - i] = v;
  328. }
  329. /* invalid values */
  330. for (i = 7; i < 16; i++)
  331. is_table[0][i] = is_table[1][i] = 0.0;
  332. for (i = 0; i < 16; i++) {
  333. double f;
  334. int e, k;
  335. for (j = 0; j < 2; j++) {
  336. e = -(j + 1) * ((i + 1) >> 1);
  337. f = exp2(e / 4.0);
  338. k = i & 1;
  339. is_table_lsf[j][k ^ 1][i] = FIXR(f);
  340. is_table_lsf[j][k ][i] = FIXR(1.0);
  341. ff_dlog(NULL, "is_table_lsf %d %d: %f %f\n",
  342. i, j, (float) is_table_lsf[j][0][i],
  343. (float) is_table_lsf[j][1][i]);
  344. }
  345. }
  346. for (i = 0; i < 8; i++) {
  347. double ci, cs, ca;
  348. ci = ci_table[i];
  349. cs = 1.0 / sqrt(1.0 + ci * ci);
  350. ca = cs * ci;
  351. #if !USE_FLOATS
  352. csa_table[i][0] = FIXHR(cs/4);
  353. csa_table[i][1] = FIXHR(ca/4);
  354. csa_table[i][2] = FIXHR(ca/4) + FIXHR(cs/4);
  355. csa_table[i][3] = FIXHR(ca/4) - FIXHR(cs/4);
  356. #else
  357. csa_table[i][0] = cs;
  358. csa_table[i][1] = ca;
  359. csa_table[i][2] = ca + cs;
  360. csa_table[i][3] = ca - cs;
  361. #endif
  362. }
  363. }
  364. #if USE_FLOATS
  365. static av_cold int decode_close(AVCodecContext * avctx)
  366. {
  367. MPADecodeContext *s = avctx->priv_data;
  368. av_freep(&s->fdsp);
  369. return 0;
  370. }
  371. #endif
  372. static av_cold int decode_init(AVCodecContext * avctx)
  373. {
  374. static int initialized_tables = 0;
  375. MPADecodeContext *s = avctx->priv_data;
  376. if (!initialized_tables) {
  377. decode_init_static();
  378. initialized_tables = 1;
  379. }
  380. s->avctx = avctx;
  381. #if USE_FLOATS
  382. s->fdsp = avpriv_float_dsp_alloc(avctx->flags & AV_CODEC_FLAG_BITEXACT);
  383. if (!s->fdsp)
  384. return AVERROR(ENOMEM);
  385. #endif
  386. ff_mpadsp_init(&s->mpadsp);
  387. if (avctx->request_sample_fmt == OUT_FMT &&
  388. avctx->codec_id != AV_CODEC_ID_MP3ON4)
  389. avctx->sample_fmt = OUT_FMT;
  390. else
  391. avctx->sample_fmt = OUT_FMT_P;
  392. s->err_recognition = avctx->err_recognition;
  393. if (avctx->codec_id == AV_CODEC_ID_MP3ADU)
  394. s->adu_mode = 1;
  395. return 0;
  396. }
  397. #define C3 FIXHR(0.86602540378443864676/2)
  398. #define C4 FIXHR(0.70710678118654752439/2) //0.5 / cos(pi*(9)/36)
  399. #define C5 FIXHR(0.51763809020504152469/2) //0.5 / cos(pi*(5)/36)
  400. #define C6 FIXHR(1.93185165257813657349/4) //0.5 / cos(pi*(15)/36)
  401. /* 12 points IMDCT. We compute it "by hand" by factorizing obvious
  402. cases. */
  403. static void imdct12(INTFLOAT *out, SUINTFLOAT *in)
  404. {
  405. SUINTFLOAT in0, in1, in2, in3, in4, in5, t1, t2;
  406. in0 = in[0*3];
  407. in1 = in[1*3] + in[0*3];
  408. in2 = in[2*3] + in[1*3];
  409. in3 = in[3*3] + in[2*3];
  410. in4 = in[4*3] + in[3*3];
  411. in5 = in[5*3] + in[4*3];
  412. in5 += in3;
  413. in3 += in1;
  414. in2 = MULH3(in2, C3, 2);
  415. in3 = MULH3(in3, C3, 4);
  416. t1 = in0 - in4;
  417. t2 = MULH3(in1 - in5, C4, 2);
  418. out[ 7] =
  419. out[10] = t1 + t2;
  420. out[ 1] =
  421. out[ 4] = t1 - t2;
  422. in0 += SHR(in4, 1);
  423. in4 = in0 + in2;
  424. in5 += 2*in1;
  425. in1 = MULH3(in5 + in3, C5, 1);
  426. out[ 8] =
  427. out[ 9] = in4 + in1;
  428. out[ 2] =
  429. out[ 3] = in4 - in1;
  430. in0 -= in2;
  431. in5 = MULH3(in5 - in3, C6, 2);
  432. out[ 0] =
  433. out[ 5] = in0 - in5;
  434. out[ 6] =
  435. out[11] = in0 + in5;
  436. }
  437. /* return the number of decoded frames */
  438. static int mp_decode_layer1(MPADecodeContext *s)
  439. {
  440. int bound, i, v, n, ch, j, mant;
  441. uint8_t allocation[MPA_MAX_CHANNELS][SBLIMIT];
  442. uint8_t scale_factors[MPA_MAX_CHANNELS][SBLIMIT];
  443. if (s->mode == MPA_JSTEREO)
  444. bound = (s->mode_ext + 1) * 4;
  445. else
  446. bound = SBLIMIT;
  447. /* allocation bits */
  448. for (i = 0; i < bound; i++) {
  449. for (ch = 0; ch < s->nb_channels; ch++) {
  450. allocation[ch][i] = get_bits(&s->gb, 4);
  451. }
  452. }
  453. for (i = bound; i < SBLIMIT; i++)
  454. allocation[0][i] = get_bits(&s->gb, 4);
  455. /* scale factors */
  456. for (i = 0; i < bound; i++) {
  457. for (ch = 0; ch < s->nb_channels; ch++) {
  458. if (allocation[ch][i])
  459. scale_factors[ch][i] = get_bits(&s->gb, 6);
  460. }
  461. }
  462. for (i = bound; i < SBLIMIT; i++) {
  463. if (allocation[0][i]) {
  464. scale_factors[0][i] = get_bits(&s->gb, 6);
  465. scale_factors[1][i] = get_bits(&s->gb, 6);
  466. }
  467. }
  468. /* compute samples */
  469. for (j = 0; j < 12; j++) {
  470. for (i = 0; i < bound; i++) {
  471. for (ch = 0; ch < s->nb_channels; ch++) {
  472. n = allocation[ch][i];
  473. if (n) {
  474. mant = get_bits(&s->gb, n + 1);
  475. v = l1_unscale(n, mant, scale_factors[ch][i]);
  476. } else {
  477. v = 0;
  478. }
  479. s->sb_samples[ch][j][i] = v;
  480. }
  481. }
  482. for (i = bound; i < SBLIMIT; i++) {
  483. n = allocation[0][i];
  484. if (n) {
  485. mant = get_bits(&s->gb, n + 1);
  486. v = l1_unscale(n, mant, scale_factors[0][i]);
  487. s->sb_samples[0][j][i] = v;
  488. v = l1_unscale(n, mant, scale_factors[1][i]);
  489. s->sb_samples[1][j][i] = v;
  490. } else {
  491. s->sb_samples[0][j][i] = 0;
  492. s->sb_samples[1][j][i] = 0;
  493. }
  494. }
  495. }
  496. return 12;
  497. }
  498. static int mp_decode_layer2(MPADecodeContext *s)
  499. {
  500. int sblimit; /* number of used subbands */
  501. const unsigned char *alloc_table;
  502. int table, bit_alloc_bits, i, j, ch, bound, v;
  503. unsigned char bit_alloc[MPA_MAX_CHANNELS][SBLIMIT];
  504. unsigned char scale_code[MPA_MAX_CHANNELS][SBLIMIT];
  505. unsigned char scale_factors[MPA_MAX_CHANNELS][SBLIMIT][3], *sf;
  506. int scale, qindex, bits, steps, k, l, m, b;
  507. /* select decoding table */
  508. table = ff_mpa_l2_select_table(s->bit_rate / 1000, s->nb_channels,
  509. s->sample_rate, s->lsf);
  510. sblimit = ff_mpa_sblimit_table[table];
  511. alloc_table = ff_mpa_alloc_tables[table];
  512. if (s->mode == MPA_JSTEREO)
  513. bound = (s->mode_ext + 1) * 4;
  514. else
  515. bound = sblimit;
  516. ff_dlog(s->avctx, "bound=%d sblimit=%d\n", bound, sblimit);
  517. /* sanity check */
  518. if (bound > sblimit)
  519. bound = sblimit;
  520. /* parse bit allocation */
  521. j = 0;
  522. for (i = 0; i < bound; i++) {
  523. bit_alloc_bits = alloc_table[j];
  524. for (ch = 0; ch < s->nb_channels; ch++)
  525. bit_alloc[ch][i] = get_bits(&s->gb, bit_alloc_bits);
  526. j += 1 << bit_alloc_bits;
  527. }
  528. for (i = bound; i < sblimit; i++) {
  529. bit_alloc_bits = alloc_table[j];
  530. v = get_bits(&s->gb, bit_alloc_bits);
  531. bit_alloc[0][i] = v;
  532. bit_alloc[1][i] = v;
  533. j += 1 << bit_alloc_bits;
  534. }
  535. /* scale codes */
  536. for (i = 0; i < sblimit; i++) {
  537. for (ch = 0; ch < s->nb_channels; ch++) {
  538. if (bit_alloc[ch][i])
  539. scale_code[ch][i] = get_bits(&s->gb, 2);
  540. }
  541. }
  542. /* scale factors */
  543. for (i = 0; i < sblimit; i++) {
  544. for (ch = 0; ch < s->nb_channels; ch++) {
  545. if (bit_alloc[ch][i]) {
  546. sf = scale_factors[ch][i];
  547. switch (scale_code[ch][i]) {
  548. default:
  549. case 0:
  550. sf[0] = get_bits(&s->gb, 6);
  551. sf[1] = get_bits(&s->gb, 6);
  552. sf[2] = get_bits(&s->gb, 6);
  553. break;
  554. case 2:
  555. sf[0] = get_bits(&s->gb, 6);
  556. sf[1] = sf[0];
  557. sf[2] = sf[0];
  558. break;
  559. case 1:
  560. sf[0] = get_bits(&s->gb, 6);
  561. sf[2] = get_bits(&s->gb, 6);
  562. sf[1] = sf[0];
  563. break;
  564. case 3:
  565. sf[0] = get_bits(&s->gb, 6);
  566. sf[2] = get_bits(&s->gb, 6);
  567. sf[1] = sf[2];
  568. break;
  569. }
  570. }
  571. }
  572. }
  573. /* samples */
  574. for (k = 0; k < 3; k++) {
  575. for (l = 0; l < 12; l += 3) {
  576. j = 0;
  577. for (i = 0; i < bound; i++) {
  578. bit_alloc_bits = alloc_table[j];
  579. for (ch = 0; ch < s->nb_channels; ch++) {
  580. b = bit_alloc[ch][i];
  581. if (b) {
  582. scale = scale_factors[ch][i][k];
  583. qindex = alloc_table[j+b];
  584. bits = ff_mpa_quant_bits[qindex];
  585. if (bits < 0) {
  586. int v2;
  587. /* 3 values at the same time */
  588. v = get_bits(&s->gb, -bits);
  589. v2 = division_tabs[qindex][v];
  590. steps = ff_mpa_quant_steps[qindex];
  591. s->sb_samples[ch][k * 12 + l + 0][i] =
  592. l2_unscale_group(steps, v2 & 15, scale);
  593. s->sb_samples[ch][k * 12 + l + 1][i] =
  594. l2_unscale_group(steps, (v2 >> 4) & 15, scale);
  595. s->sb_samples[ch][k * 12 + l + 2][i] =
  596. l2_unscale_group(steps, v2 >> 8 , scale);
  597. } else {
  598. for (m = 0; m < 3; m++) {
  599. v = get_bits(&s->gb, bits);
  600. v = l1_unscale(bits - 1, v, scale);
  601. s->sb_samples[ch][k * 12 + l + m][i] = v;
  602. }
  603. }
  604. } else {
  605. s->sb_samples[ch][k * 12 + l + 0][i] = 0;
  606. s->sb_samples[ch][k * 12 + l + 1][i] = 0;
  607. s->sb_samples[ch][k * 12 + l + 2][i] = 0;
  608. }
  609. }
  610. /* next subband in alloc table */
  611. j += 1 << bit_alloc_bits;
  612. }
  613. /* XXX: find a way to avoid this duplication of code */
  614. for (i = bound; i < sblimit; i++) {
  615. bit_alloc_bits = alloc_table[j];
  616. b = bit_alloc[0][i];
  617. if (b) {
  618. int mant, scale0, scale1;
  619. scale0 = scale_factors[0][i][k];
  620. scale1 = scale_factors[1][i][k];
  621. qindex = alloc_table[j+b];
  622. bits = ff_mpa_quant_bits[qindex];
  623. if (bits < 0) {
  624. /* 3 values at the same time */
  625. v = get_bits(&s->gb, -bits);
  626. steps = ff_mpa_quant_steps[qindex];
  627. mant = v % steps;
  628. v = v / steps;
  629. s->sb_samples[0][k * 12 + l + 0][i] =
  630. l2_unscale_group(steps, mant, scale0);
  631. s->sb_samples[1][k * 12 + l + 0][i] =
  632. l2_unscale_group(steps, mant, scale1);
  633. mant = v % steps;
  634. v = v / steps;
  635. s->sb_samples[0][k * 12 + l + 1][i] =
  636. l2_unscale_group(steps, mant, scale0);
  637. s->sb_samples[1][k * 12 + l + 1][i] =
  638. l2_unscale_group(steps, mant, scale1);
  639. s->sb_samples[0][k * 12 + l + 2][i] =
  640. l2_unscale_group(steps, v, scale0);
  641. s->sb_samples[1][k * 12 + l + 2][i] =
  642. l2_unscale_group(steps, v, scale1);
  643. } else {
  644. for (m = 0; m < 3; m++) {
  645. mant = get_bits(&s->gb, bits);
  646. s->sb_samples[0][k * 12 + l + m][i] =
  647. l1_unscale(bits - 1, mant, scale0);
  648. s->sb_samples[1][k * 12 + l + m][i] =
  649. l1_unscale(bits - 1, mant, scale1);
  650. }
  651. }
  652. } else {
  653. s->sb_samples[0][k * 12 + l + 0][i] = 0;
  654. s->sb_samples[0][k * 12 + l + 1][i] = 0;
  655. s->sb_samples[0][k * 12 + l + 2][i] = 0;
  656. s->sb_samples[1][k * 12 + l + 0][i] = 0;
  657. s->sb_samples[1][k * 12 + l + 1][i] = 0;
  658. s->sb_samples[1][k * 12 + l + 2][i] = 0;
  659. }
  660. /* next subband in alloc table */
  661. j += 1 << bit_alloc_bits;
  662. }
  663. /* fill remaining samples to zero */
  664. for (i = sblimit; i < SBLIMIT; i++) {
  665. for (ch = 0; ch < s->nb_channels; ch++) {
  666. s->sb_samples[ch][k * 12 + l + 0][i] = 0;
  667. s->sb_samples[ch][k * 12 + l + 1][i] = 0;
  668. s->sb_samples[ch][k * 12 + l + 2][i] = 0;
  669. }
  670. }
  671. }
  672. }
  673. return 3 * 12;
  674. }
  675. #define SPLIT(dst,sf,n) \
  676. if (n == 3) { \
  677. int m = (sf * 171) >> 9; \
  678. dst = sf - 3 * m; \
  679. sf = m; \
  680. } else if (n == 4) { \
  681. dst = sf & 3; \
  682. sf >>= 2; \
  683. } else if (n == 5) { \
  684. int m = (sf * 205) >> 10; \
  685. dst = sf - 5 * m; \
  686. sf = m; \
  687. } else if (n == 6) { \
  688. int m = (sf * 171) >> 10; \
  689. dst = sf - 6 * m; \
  690. sf = m; \
  691. } else { \
  692. dst = 0; \
  693. }
  694. static av_always_inline void lsf_sf_expand(int *slen, int sf, int n1, int n2,
  695. int n3)
  696. {
  697. SPLIT(slen[3], sf, n3)
  698. SPLIT(slen[2], sf, n2)
  699. SPLIT(slen[1], sf, n1)
  700. slen[0] = sf;
  701. }
  702. static void exponents_from_scale_factors(MPADecodeContext *s, GranuleDef *g,
  703. int16_t *exponents)
  704. {
  705. const uint8_t *bstab, *pretab;
  706. int len, i, j, k, l, v0, shift, gain, gains[3];
  707. int16_t *exp_ptr;
  708. exp_ptr = exponents;
  709. gain = g->global_gain - 210;
  710. shift = g->scalefac_scale + 1;
  711. bstab = band_size_long[s->sample_rate_index];
  712. pretab = mpa_pretab[g->preflag];
  713. for (i = 0; i < g->long_end; i++) {
  714. v0 = gain - ((g->scale_factors[i] + pretab[i]) << shift) + 400;
  715. len = bstab[i];
  716. for (j = len; j > 0; j--)
  717. *exp_ptr++ = v0;
  718. }
  719. if (g->short_start < 13) {
  720. bstab = band_size_short[s->sample_rate_index];
  721. gains[0] = gain - (g->subblock_gain[0] << 3);
  722. gains[1] = gain - (g->subblock_gain[1] << 3);
  723. gains[2] = gain - (g->subblock_gain[2] << 3);
  724. k = g->long_end;
  725. for (i = g->short_start; i < 13; i++) {
  726. len = bstab[i];
  727. for (l = 0; l < 3; l++) {
  728. v0 = gains[l] - (g->scale_factors[k++] << shift) + 400;
  729. for (j = len; j > 0; j--)
  730. *exp_ptr++ = v0;
  731. }
  732. }
  733. }
  734. }
  735. static void switch_buffer(MPADecodeContext *s, int *pos, int *end_pos,
  736. int *end_pos2)
  737. {
  738. if (s->in_gb.buffer && *pos >= s->gb.size_in_bits - s->extrasize * 8) {
  739. s->gb = s->in_gb;
  740. s->in_gb.buffer = NULL;
  741. s->extrasize = 0;
  742. av_assert2((get_bits_count(&s->gb) & 7) == 0);
  743. skip_bits_long(&s->gb, *pos - *end_pos);
  744. *end_pos2 =
  745. *end_pos = *end_pos2 + get_bits_count(&s->gb) - *pos;
  746. *pos = get_bits_count(&s->gb);
  747. }
  748. }
  749. /* Following is an optimized code for
  750. INTFLOAT v = *src
  751. if(get_bits1(&s->gb))
  752. v = -v;
  753. *dst = v;
  754. */
  755. #if USE_FLOATS
  756. #define READ_FLIP_SIGN(dst,src) \
  757. v = AV_RN32A(src) ^ (get_bits1(&s->gb) << 31); \
  758. AV_WN32A(dst, v);
  759. #else
  760. #define READ_FLIP_SIGN(dst,src) \
  761. v = -get_bits1(&s->gb); \
  762. *(dst) = (*(src) ^ v) - v;
  763. #endif
  764. static int huffman_decode(MPADecodeContext *s, GranuleDef *g,
  765. int16_t *exponents, int end_pos2)
  766. {
  767. int s_index;
  768. int i;
  769. int last_pos, bits_left;
  770. VLC *vlc;
  771. int end_pos = FFMIN(end_pos2, s->gb.size_in_bits - s->extrasize * 8);
  772. /* low frequencies (called big values) */
  773. s_index = 0;
  774. for (i = 0; i < 3; i++) {
  775. int j, k, l, linbits;
  776. j = g->region_size[i];
  777. if (j == 0)
  778. continue;
  779. /* select vlc table */
  780. k = g->table_select[i];
  781. l = mpa_huff_data[k][0];
  782. linbits = mpa_huff_data[k][1];
  783. vlc = &huff_vlc[l];
  784. if (!l) {
  785. memset(&g->sb_hybrid[s_index], 0, sizeof(*g->sb_hybrid) * 2 * j);
  786. s_index += 2 * j;
  787. continue;
  788. }
  789. /* read huffcode and compute each couple */
  790. for (; j > 0; j--) {
  791. int exponent, x, y;
  792. int v;
  793. int pos = get_bits_count(&s->gb);
  794. if (pos >= end_pos){
  795. switch_buffer(s, &pos, &end_pos, &end_pos2);
  796. if (pos >= end_pos)
  797. break;
  798. }
  799. y = get_vlc2(&s->gb, vlc->table, 7, 3);
  800. if (!y) {
  801. g->sb_hybrid[s_index ] =
  802. g->sb_hybrid[s_index+1] = 0;
  803. s_index += 2;
  804. continue;
  805. }
  806. exponent= exponents[s_index];
  807. ff_dlog(s->avctx, "region=%d n=%d y=%d exp=%d\n",
  808. i, g->region_size[i] - j, y, exponent);
  809. if (y & 16) {
  810. x = y >> 5;
  811. y = y & 0x0f;
  812. if (x < 15) {
  813. READ_FLIP_SIGN(g->sb_hybrid + s_index, RENAME(expval_table)[exponent] + x)
  814. } else {
  815. x += get_bitsz(&s->gb, linbits);
  816. v = l3_unscale(x, exponent);
  817. if (get_bits1(&s->gb))
  818. v = -v;
  819. g->sb_hybrid[s_index] = v;
  820. }
  821. if (y < 15) {
  822. READ_FLIP_SIGN(g->sb_hybrid + s_index + 1, RENAME(expval_table)[exponent] + y)
  823. } else {
  824. y += get_bitsz(&s->gb, linbits);
  825. v = l3_unscale(y, exponent);
  826. if (get_bits1(&s->gb))
  827. v = -v;
  828. g->sb_hybrid[s_index+1] = v;
  829. }
  830. } else {
  831. x = y >> 5;
  832. y = y & 0x0f;
  833. x += y;
  834. if (x < 15) {
  835. READ_FLIP_SIGN(g->sb_hybrid + s_index + !!y, RENAME(expval_table)[exponent] + x)
  836. } else {
  837. x += get_bitsz(&s->gb, linbits);
  838. v = l3_unscale(x, exponent);
  839. if (get_bits1(&s->gb))
  840. v = -v;
  841. g->sb_hybrid[s_index+!!y] = v;
  842. }
  843. g->sb_hybrid[s_index + !y] = 0;
  844. }
  845. s_index += 2;
  846. }
  847. }
  848. /* high frequencies */
  849. vlc = &huff_quad_vlc[g->count1table_select];
  850. last_pos = 0;
  851. while (s_index <= 572) {
  852. int pos, code;
  853. pos = get_bits_count(&s->gb);
  854. if (pos >= end_pos) {
  855. if (pos > end_pos2 && last_pos) {
  856. /* some encoders generate an incorrect size for this
  857. part. We must go back into the data */
  858. s_index -= 4;
  859. skip_bits_long(&s->gb, last_pos - pos);
  860. av_log(s->avctx, AV_LOG_INFO, "overread, skip %d enddists: %d %d\n", last_pos - pos, end_pos-pos, end_pos2-pos);
  861. if(s->err_recognition & (AV_EF_BITSTREAM|AV_EF_COMPLIANT))
  862. s_index=0;
  863. break;
  864. }
  865. switch_buffer(s, &pos, &end_pos, &end_pos2);
  866. if (pos >= end_pos)
  867. break;
  868. }
  869. last_pos = pos;
  870. code = get_vlc2(&s->gb, vlc->table, vlc->bits, 1);
  871. ff_dlog(s->avctx, "t=%d code=%d\n", g->count1table_select, code);
  872. g->sb_hybrid[s_index+0] =
  873. g->sb_hybrid[s_index+1] =
  874. g->sb_hybrid[s_index+2] =
  875. g->sb_hybrid[s_index+3] = 0;
  876. while (code) {
  877. static const int idxtab[16] = { 3,3,2,2,1,1,1,1,0,0,0,0,0,0,0,0 };
  878. int v;
  879. int pos = s_index + idxtab[code];
  880. code ^= 8 >> idxtab[code];
  881. READ_FLIP_SIGN(g->sb_hybrid + pos, RENAME(exp_table)+exponents[pos])
  882. }
  883. s_index += 4;
  884. }
  885. /* skip extension bits */
  886. bits_left = end_pos2 - get_bits_count(&s->gb);
  887. if (bits_left < 0 && (s->err_recognition & (AV_EF_BUFFER|AV_EF_COMPLIANT))) {
  888. av_log(s->avctx, AV_LOG_ERROR, "bits_left=%d\n", bits_left);
  889. s_index=0;
  890. } else if (bits_left > 0 && (s->err_recognition & (AV_EF_BUFFER|AV_EF_AGGRESSIVE))) {
  891. av_log(s->avctx, AV_LOG_ERROR, "bits_left=%d\n", bits_left);
  892. s_index = 0;
  893. }
  894. memset(&g->sb_hybrid[s_index], 0, sizeof(*g->sb_hybrid) * (576 - s_index));
  895. skip_bits_long(&s->gb, bits_left);
  896. i = get_bits_count(&s->gb);
  897. switch_buffer(s, &i, &end_pos, &end_pos2);
  898. return 0;
  899. }
  900. /* Reorder short blocks from bitstream order to interleaved order. It
  901. would be faster to do it in parsing, but the code would be far more
  902. complicated */
  903. static void reorder_block(MPADecodeContext *s, GranuleDef *g)
  904. {
  905. int i, j, len;
  906. INTFLOAT *ptr, *dst, *ptr1;
  907. INTFLOAT tmp[576];
  908. if (g->block_type != 2)
  909. return;
  910. if (g->switch_point) {
  911. if (s->sample_rate_index != 8)
  912. ptr = g->sb_hybrid + 36;
  913. else
  914. ptr = g->sb_hybrid + 72;
  915. } else {
  916. ptr = g->sb_hybrid;
  917. }
  918. for (i = g->short_start; i < 13; i++) {
  919. len = band_size_short[s->sample_rate_index][i];
  920. ptr1 = ptr;
  921. dst = tmp;
  922. for (j = len; j > 0; j--) {
  923. *dst++ = ptr[0*len];
  924. *dst++ = ptr[1*len];
  925. *dst++ = ptr[2*len];
  926. ptr++;
  927. }
  928. ptr += 2 * len;
  929. memcpy(ptr1, tmp, len * 3 * sizeof(*ptr1));
  930. }
  931. }
  932. #define ISQRT2 FIXR(0.70710678118654752440)
  933. static void compute_stereo(MPADecodeContext *s, GranuleDef *g0, GranuleDef *g1)
  934. {
  935. int i, j, k, l;
  936. int sf_max, sf, len, non_zero_found;
  937. INTFLOAT (*is_tab)[16], *tab0, *tab1, v1, v2;
  938. SUINTFLOAT tmp0, tmp1;
  939. int non_zero_found_short[3];
  940. /* intensity stereo */
  941. if (s->mode_ext & MODE_EXT_I_STEREO) {
  942. if (!s->lsf) {
  943. is_tab = is_table;
  944. sf_max = 7;
  945. } else {
  946. is_tab = is_table_lsf[g1->scalefac_compress & 1];
  947. sf_max = 16;
  948. }
  949. tab0 = g0->sb_hybrid + 576;
  950. tab1 = g1->sb_hybrid + 576;
  951. non_zero_found_short[0] = 0;
  952. non_zero_found_short[1] = 0;
  953. non_zero_found_short[2] = 0;
  954. k = (13 - g1->short_start) * 3 + g1->long_end - 3;
  955. for (i = 12; i >= g1->short_start; i--) {
  956. /* for last band, use previous scale factor */
  957. if (i != 11)
  958. k -= 3;
  959. len = band_size_short[s->sample_rate_index][i];
  960. for (l = 2; l >= 0; l--) {
  961. tab0 -= len;
  962. tab1 -= len;
  963. if (!non_zero_found_short[l]) {
  964. /* test if non zero band. if so, stop doing i-stereo */
  965. for (j = 0; j < len; j++) {
  966. if (tab1[j] != 0) {
  967. non_zero_found_short[l] = 1;
  968. goto found1;
  969. }
  970. }
  971. sf = g1->scale_factors[k + l];
  972. if (sf >= sf_max)
  973. goto found1;
  974. v1 = is_tab[0][sf];
  975. v2 = is_tab[1][sf];
  976. for (j = 0; j < len; j++) {
  977. tmp0 = tab0[j];
  978. tab0[j] = MULLx(tmp0, v1, FRAC_BITS);
  979. tab1[j] = MULLx(tmp0, v2, FRAC_BITS);
  980. }
  981. } else {
  982. found1:
  983. if (s->mode_ext & MODE_EXT_MS_STEREO) {
  984. /* lower part of the spectrum : do ms stereo
  985. if enabled */
  986. for (j = 0; j < len; j++) {
  987. tmp0 = tab0[j];
  988. tmp1 = tab1[j];
  989. tab0[j] = MULLx(tmp0 + tmp1, ISQRT2, FRAC_BITS);
  990. tab1[j] = MULLx(tmp0 - tmp1, ISQRT2, FRAC_BITS);
  991. }
  992. }
  993. }
  994. }
  995. }
  996. non_zero_found = non_zero_found_short[0] |
  997. non_zero_found_short[1] |
  998. non_zero_found_short[2];
  999. for (i = g1->long_end - 1;i >= 0;i--) {
  1000. len = band_size_long[s->sample_rate_index][i];
  1001. tab0 -= len;
  1002. tab1 -= len;
  1003. /* test if non zero band. if so, stop doing i-stereo */
  1004. if (!non_zero_found) {
  1005. for (j = 0; j < len; j++) {
  1006. if (tab1[j] != 0) {
  1007. non_zero_found = 1;
  1008. goto found2;
  1009. }
  1010. }
  1011. /* for last band, use previous scale factor */
  1012. k = (i == 21) ? 20 : i;
  1013. sf = g1->scale_factors[k];
  1014. if (sf >= sf_max)
  1015. goto found2;
  1016. v1 = is_tab[0][sf];
  1017. v2 = is_tab[1][sf];
  1018. for (j = 0; j < len; j++) {
  1019. tmp0 = tab0[j];
  1020. tab0[j] = MULLx(tmp0, v1, FRAC_BITS);
  1021. tab1[j] = MULLx(tmp0, v2, FRAC_BITS);
  1022. }
  1023. } else {
  1024. found2:
  1025. if (s->mode_ext & MODE_EXT_MS_STEREO) {
  1026. /* lower part of the spectrum : do ms stereo
  1027. if enabled */
  1028. for (j = 0; j < len; j++) {
  1029. tmp0 = tab0[j];
  1030. tmp1 = tab1[j];
  1031. tab0[j] = MULLx(tmp0 + tmp1, ISQRT2, FRAC_BITS);
  1032. tab1[j] = MULLx(tmp0 - tmp1, ISQRT2, FRAC_BITS);
  1033. }
  1034. }
  1035. }
  1036. }
  1037. } else if (s->mode_ext & MODE_EXT_MS_STEREO) {
  1038. /* ms stereo ONLY */
  1039. /* NOTE: the 1/sqrt(2) normalization factor is included in the
  1040. global gain */
  1041. #if USE_FLOATS
  1042. s->fdsp->butterflies_float(g0->sb_hybrid, g1->sb_hybrid, 576);
  1043. #else
  1044. tab0 = g0->sb_hybrid;
  1045. tab1 = g1->sb_hybrid;
  1046. for (i = 0; i < 576; i++) {
  1047. tmp0 = tab0[i];
  1048. tmp1 = tab1[i];
  1049. tab0[i] = tmp0 + tmp1;
  1050. tab1[i] = tmp0 - tmp1;
  1051. }
  1052. #endif
  1053. }
  1054. }
  1055. #if USE_FLOATS
  1056. #if HAVE_MIPSFPU
  1057. # include "mips/compute_antialias_float.h"
  1058. #endif /* HAVE_MIPSFPU */
  1059. #else
  1060. #if HAVE_MIPSDSP
  1061. # include "mips/compute_antialias_fixed.h"
  1062. #endif /* HAVE_MIPSDSP */
  1063. #endif /* USE_FLOATS */
  1064. #ifndef compute_antialias
  1065. #if USE_FLOATS
  1066. #define AA(j) do { \
  1067. float tmp0 = ptr[-1-j]; \
  1068. float tmp1 = ptr[ j]; \
  1069. ptr[-1-j] = tmp0 * csa_table[j][0] - tmp1 * csa_table[j][1]; \
  1070. ptr[ j] = tmp0 * csa_table[j][1] + tmp1 * csa_table[j][0]; \
  1071. } while (0)
  1072. #else
  1073. #define AA(j) do { \
  1074. SUINT tmp0 = ptr[-1-j]; \
  1075. SUINT tmp1 = ptr[ j]; \
  1076. SUINT tmp2 = MULH(tmp0 + tmp1, csa_table[j][0]); \
  1077. ptr[-1-j] = 4 * (tmp2 - MULH(tmp1, csa_table[j][2])); \
  1078. ptr[ j] = 4 * (tmp2 + MULH(tmp0, csa_table[j][3])); \
  1079. } while (0)
  1080. #endif
  1081. static void compute_antialias(MPADecodeContext *s, GranuleDef *g)
  1082. {
  1083. INTFLOAT *ptr;
  1084. int n, i;
  1085. /* we antialias only "long" bands */
  1086. if (g->block_type == 2) {
  1087. if (!g->switch_point)
  1088. return;
  1089. /* XXX: check this for 8000Hz case */
  1090. n = 1;
  1091. } else {
  1092. n = SBLIMIT - 1;
  1093. }
  1094. ptr = g->sb_hybrid + 18;
  1095. for (i = n; i > 0; i--) {
  1096. AA(0);
  1097. AA(1);
  1098. AA(2);
  1099. AA(3);
  1100. AA(4);
  1101. AA(5);
  1102. AA(6);
  1103. AA(7);
  1104. ptr += 18;
  1105. }
  1106. }
  1107. #endif /* compute_antialias */
  1108. static void compute_imdct(MPADecodeContext *s, GranuleDef *g,
  1109. INTFLOAT *sb_samples, INTFLOAT *mdct_buf)
  1110. {
  1111. INTFLOAT *win, *out_ptr, *ptr, *buf, *ptr1;
  1112. INTFLOAT out2[12];
  1113. int i, j, mdct_long_end, sblimit;
  1114. /* find last non zero block */
  1115. ptr = g->sb_hybrid + 576;
  1116. ptr1 = g->sb_hybrid + 2 * 18;
  1117. while (ptr >= ptr1) {
  1118. int32_t *p;
  1119. ptr -= 6;
  1120. p = (int32_t*)ptr;
  1121. if (p[0] | p[1] | p[2] | p[3] | p[4] | p[5])
  1122. break;
  1123. }
  1124. sblimit = ((ptr - g->sb_hybrid) / 18) + 1;
  1125. if (g->block_type == 2) {
  1126. /* XXX: check for 8000 Hz */
  1127. if (g->switch_point)
  1128. mdct_long_end = 2;
  1129. else
  1130. mdct_long_end = 0;
  1131. } else {
  1132. mdct_long_end = sblimit;
  1133. }
  1134. s->mpadsp.RENAME(imdct36_blocks)(sb_samples, mdct_buf, g->sb_hybrid,
  1135. mdct_long_end, g->switch_point,
  1136. g->block_type);
  1137. buf = mdct_buf + 4*18*(mdct_long_end >> 2) + (mdct_long_end & 3);
  1138. ptr = g->sb_hybrid + 18 * mdct_long_end;
  1139. for (j = mdct_long_end; j < sblimit; j++) {
  1140. /* select frequency inversion */
  1141. win = RENAME(ff_mdct_win)[2 + (4 & -(j & 1))];
  1142. out_ptr = sb_samples + j;
  1143. for (i = 0; i < 6; i++) {
  1144. *out_ptr = buf[4*i];
  1145. out_ptr += SBLIMIT;
  1146. }
  1147. imdct12(out2, ptr + 0);
  1148. for (i = 0; i < 6; i++) {
  1149. *out_ptr = MULH3(out2[i ], win[i ], 1) + buf[4*(i + 6*1)];
  1150. buf[4*(i + 6*2)] = MULH3(out2[i + 6], win[i + 6], 1);
  1151. out_ptr += SBLIMIT;
  1152. }
  1153. imdct12(out2, ptr + 1);
  1154. for (i = 0; i < 6; i++) {
  1155. *out_ptr = MULH3(out2[i ], win[i ], 1) + buf[4*(i + 6*2)];
  1156. buf[4*(i + 6*0)] = MULH3(out2[i + 6], win[i + 6], 1);
  1157. out_ptr += SBLIMIT;
  1158. }
  1159. imdct12(out2, ptr + 2);
  1160. for (i = 0; i < 6; i++) {
  1161. buf[4*(i + 6*0)] = MULH3(out2[i ], win[i ], 1) + buf[4*(i + 6*0)];
  1162. buf[4*(i + 6*1)] = MULH3(out2[i + 6], win[i + 6], 1);
  1163. buf[4*(i + 6*2)] = 0;
  1164. }
  1165. ptr += 18;
  1166. buf += (j&3) != 3 ? 1 : (4*18-3);
  1167. }
  1168. /* zero bands */
  1169. for (j = sblimit; j < SBLIMIT; j++) {
  1170. /* overlap */
  1171. out_ptr = sb_samples + j;
  1172. for (i = 0; i < 18; i++) {
  1173. *out_ptr = buf[4*i];
  1174. buf[4*i] = 0;
  1175. out_ptr += SBLIMIT;
  1176. }
  1177. buf += (j&3) != 3 ? 1 : (4*18-3);
  1178. }
  1179. }
  1180. /* main layer3 decoding function */
  1181. static int mp_decode_layer3(MPADecodeContext *s)
  1182. {
  1183. int nb_granules, main_data_begin;
  1184. int gr, ch, blocksplit_flag, i, j, k, n, bits_pos;
  1185. GranuleDef *g;
  1186. int16_t exponents[576]; //FIXME try INTFLOAT
  1187. /* read side info */
  1188. if (s->lsf) {
  1189. main_data_begin = get_bits(&s->gb, 8);
  1190. skip_bits(&s->gb, s->nb_channels);
  1191. nb_granules = 1;
  1192. } else {
  1193. main_data_begin = get_bits(&s->gb, 9);
  1194. if (s->nb_channels == 2)
  1195. skip_bits(&s->gb, 3);
  1196. else
  1197. skip_bits(&s->gb, 5);
  1198. nb_granules = 2;
  1199. for (ch = 0; ch < s->nb_channels; ch++) {
  1200. s->granules[ch][0].scfsi = 0;/* all scale factors are transmitted */
  1201. s->granules[ch][1].scfsi = get_bits(&s->gb, 4);
  1202. }
  1203. }
  1204. for (gr = 0; gr < nb_granules; gr++) {
  1205. for (ch = 0; ch < s->nb_channels; ch++) {
  1206. ff_dlog(s->avctx, "gr=%d ch=%d: side_info\n", gr, ch);
  1207. g = &s->granules[ch][gr];
  1208. g->part2_3_length = get_bits(&s->gb, 12);
  1209. g->big_values = get_bits(&s->gb, 9);
  1210. if (g->big_values > 288) {
  1211. av_log(s->avctx, AV_LOG_ERROR, "big_values too big\n");
  1212. return AVERROR_INVALIDDATA;
  1213. }
  1214. g->global_gain = get_bits(&s->gb, 8);
  1215. /* if MS stereo only is selected, we precompute the
  1216. 1/sqrt(2) renormalization factor */
  1217. if ((s->mode_ext & (MODE_EXT_MS_STEREO | MODE_EXT_I_STEREO)) ==
  1218. MODE_EXT_MS_STEREO)
  1219. g->global_gain -= 2;
  1220. if (s->lsf)
  1221. g->scalefac_compress = get_bits(&s->gb, 9);
  1222. else
  1223. g->scalefac_compress = get_bits(&s->gb, 4);
  1224. blocksplit_flag = get_bits1(&s->gb);
  1225. if (blocksplit_flag) {
  1226. g->block_type = get_bits(&s->gb, 2);
  1227. if (g->block_type == 0) {
  1228. av_log(s->avctx, AV_LOG_ERROR, "invalid block type\n");
  1229. return AVERROR_INVALIDDATA;
  1230. }
  1231. g->switch_point = get_bits1(&s->gb);
  1232. for (i = 0; i < 2; i++)
  1233. g->table_select[i] = get_bits(&s->gb, 5);
  1234. for (i = 0; i < 3; i++)
  1235. g->subblock_gain[i] = get_bits(&s->gb, 3);
  1236. init_short_region(s, g);
  1237. } else {
  1238. int region_address1, region_address2;
  1239. g->block_type = 0;
  1240. g->switch_point = 0;
  1241. for (i = 0; i < 3; i++)
  1242. g->table_select[i] = get_bits(&s->gb, 5);
  1243. /* compute huffman coded region sizes */
  1244. region_address1 = get_bits(&s->gb, 4);
  1245. region_address2 = get_bits(&s->gb, 3);
  1246. ff_dlog(s->avctx, "region1=%d region2=%d\n",
  1247. region_address1, region_address2);
  1248. init_long_region(s, g, region_address1, region_address2);
  1249. }
  1250. region_offset2size(g);
  1251. compute_band_indexes(s, g);
  1252. g->preflag = 0;
  1253. if (!s->lsf)
  1254. g->preflag = get_bits1(&s->gb);
  1255. g->scalefac_scale = get_bits1(&s->gb);
  1256. g->count1table_select = get_bits1(&s->gb);
  1257. ff_dlog(s->avctx, "block_type=%d switch_point=%d\n",
  1258. g->block_type, g->switch_point);
  1259. }
  1260. }
  1261. if (!s->adu_mode) {
  1262. int skip;
  1263. const uint8_t *ptr = s->gb.buffer + (get_bits_count(&s->gb)>>3);
  1264. s->extrasize = av_clip((get_bits_left(&s->gb) >> 3) - s->extrasize, 0,
  1265. FFMAX(0, LAST_BUF_SIZE - s->last_buf_size));
  1266. av_assert1((get_bits_count(&s->gb) & 7) == 0);
  1267. /* now we get bits from the main_data_begin offset */
  1268. ff_dlog(s->avctx, "seekback:%d, lastbuf:%d\n",
  1269. main_data_begin, s->last_buf_size);
  1270. memcpy(s->last_buf + s->last_buf_size, ptr, s->extrasize);
  1271. s->in_gb = s->gb;
  1272. init_get_bits(&s->gb, s->last_buf, (s->last_buf_size + s->extrasize) * 8);
  1273. s->last_buf_size <<= 3;
  1274. for (gr = 0; gr < nb_granules && (s->last_buf_size >> 3) < main_data_begin; gr++) {
  1275. for (ch = 0; ch < s->nb_channels; ch++) {
  1276. g = &s->granules[ch][gr];
  1277. s->last_buf_size += g->part2_3_length;
  1278. memset(g->sb_hybrid, 0, sizeof(g->sb_hybrid));
  1279. compute_imdct(s, g, &s->sb_samples[ch][18 * gr][0], s->mdct_buf[ch]);
  1280. }
  1281. }
  1282. skip = s->last_buf_size - 8 * main_data_begin;
  1283. if (skip >= s->gb.size_in_bits - s->extrasize * 8 && s->in_gb.buffer) {
  1284. skip_bits_long(&s->in_gb, skip - s->gb.size_in_bits + s->extrasize * 8);
  1285. s->gb = s->in_gb;
  1286. s->in_gb.buffer = NULL;
  1287. s->extrasize = 0;
  1288. } else {
  1289. skip_bits_long(&s->gb, skip);
  1290. }
  1291. } else {
  1292. gr = 0;
  1293. s->extrasize = 0;
  1294. }
  1295. for (; gr < nb_granules; gr++) {
  1296. for (ch = 0; ch < s->nb_channels; ch++) {
  1297. g = &s->granules[ch][gr];
  1298. bits_pos = get_bits_count(&s->gb);
  1299. if (!s->lsf) {
  1300. uint8_t *sc;
  1301. int slen, slen1, slen2;
  1302. /* MPEG-1 scale factors */
  1303. slen1 = slen_table[0][g->scalefac_compress];
  1304. slen2 = slen_table[1][g->scalefac_compress];
  1305. ff_dlog(s->avctx, "slen1=%d slen2=%d\n", slen1, slen2);
  1306. if (g->block_type == 2) {
  1307. n = g->switch_point ? 17 : 18;
  1308. j = 0;
  1309. if (slen1) {
  1310. for (i = 0; i < n; i++)
  1311. g->scale_factors[j++] = get_bits(&s->gb, slen1);
  1312. } else {
  1313. for (i = 0; i < n; i++)
  1314. g->scale_factors[j++] = 0;
  1315. }
  1316. if (slen2) {
  1317. for (i = 0; i < 18; i++)
  1318. g->scale_factors[j++] = get_bits(&s->gb, slen2);
  1319. for (i = 0; i < 3; i++)
  1320. g->scale_factors[j++] = 0;
  1321. } else {
  1322. for (i = 0; i < 21; i++)
  1323. g->scale_factors[j++] = 0;
  1324. }
  1325. } else {
  1326. sc = s->granules[ch][0].scale_factors;
  1327. j = 0;
  1328. for (k = 0; k < 4; k++) {
  1329. n = k == 0 ? 6 : 5;
  1330. if ((g->scfsi & (0x8 >> k)) == 0) {
  1331. slen = (k < 2) ? slen1 : slen2;
  1332. if (slen) {
  1333. for (i = 0; i < n; i++)
  1334. g->scale_factors[j++] = get_bits(&s->gb, slen);
  1335. } else {
  1336. for (i = 0; i < n; i++)
  1337. g->scale_factors[j++] = 0;
  1338. }
  1339. } else {
  1340. /* simply copy from last granule */
  1341. for (i = 0; i < n; i++) {
  1342. g->scale_factors[j] = sc[j];
  1343. j++;
  1344. }
  1345. }
  1346. }
  1347. g->scale_factors[j++] = 0;
  1348. }
  1349. } else {
  1350. int tindex, tindex2, slen[4], sl, sf;
  1351. /* LSF scale factors */
  1352. if (g->block_type == 2)
  1353. tindex = g->switch_point ? 2 : 1;
  1354. else
  1355. tindex = 0;
  1356. sf = g->scalefac_compress;
  1357. if ((s->mode_ext & MODE_EXT_I_STEREO) && ch == 1) {
  1358. /* intensity stereo case */
  1359. sf >>= 1;
  1360. if (sf < 180) {
  1361. lsf_sf_expand(slen, sf, 6, 6, 0);
  1362. tindex2 = 3;
  1363. } else if (sf < 244) {
  1364. lsf_sf_expand(slen, sf - 180, 4, 4, 0);
  1365. tindex2 = 4;
  1366. } else {
  1367. lsf_sf_expand(slen, sf - 244, 3, 0, 0);
  1368. tindex2 = 5;
  1369. }
  1370. } else {
  1371. /* normal case */
  1372. if (sf < 400) {
  1373. lsf_sf_expand(slen, sf, 5, 4, 4);
  1374. tindex2 = 0;
  1375. } else if (sf < 500) {
  1376. lsf_sf_expand(slen, sf - 400, 5, 4, 0);
  1377. tindex2 = 1;
  1378. } else {
  1379. lsf_sf_expand(slen, sf - 500, 3, 0, 0);
  1380. tindex2 = 2;
  1381. g->preflag = 1;
  1382. }
  1383. }
  1384. j = 0;
  1385. for (k = 0; k < 4; k++) {
  1386. n = lsf_nsf_table[tindex2][tindex][k];
  1387. sl = slen[k];
  1388. if (sl) {
  1389. for (i = 0; i < n; i++)
  1390. g->scale_factors[j++] = get_bits(&s->gb, sl);
  1391. } else {
  1392. for (i = 0; i < n; i++)
  1393. g->scale_factors[j++] = 0;
  1394. }
  1395. }
  1396. /* XXX: should compute exact size */
  1397. for (; j < 40; j++)
  1398. g->scale_factors[j] = 0;
  1399. }
  1400. exponents_from_scale_factors(s, g, exponents);
  1401. /* read Huffman coded residue */
  1402. huffman_decode(s, g, exponents, bits_pos + g->part2_3_length);
  1403. } /* ch */
  1404. if (s->mode == MPA_JSTEREO)
  1405. compute_stereo(s, &s->granules[0][gr], &s->granules[1][gr]);
  1406. for (ch = 0; ch < s->nb_channels; ch++) {
  1407. g = &s->granules[ch][gr];
  1408. reorder_block(s, g);
  1409. compute_antialias(s, g);
  1410. compute_imdct(s, g, &s->sb_samples[ch][18 * gr][0], s->mdct_buf[ch]);
  1411. }
  1412. } /* gr */
  1413. if (get_bits_count(&s->gb) < 0)
  1414. skip_bits_long(&s->gb, -get_bits_count(&s->gb));
  1415. return nb_granules * 18;
  1416. }
  1417. static int mp_decode_frame(MPADecodeContext *s, OUT_INT **samples,
  1418. const uint8_t *buf, int buf_size)
  1419. {
  1420. int i, nb_frames, ch, ret;
  1421. OUT_INT *samples_ptr;
  1422. init_get_bits(&s->gb, buf + HEADER_SIZE, (buf_size - HEADER_SIZE) * 8);
  1423. /* skip error protection field */
  1424. if (s->error_protection)
  1425. skip_bits(&s->gb, 16);
  1426. switch(s->layer) {
  1427. case 1:
  1428. s->avctx->frame_size = 384;
  1429. nb_frames = mp_decode_layer1(s);
  1430. break;
  1431. case 2:
  1432. s->avctx->frame_size = 1152;
  1433. nb_frames = mp_decode_layer2(s);
  1434. break;
  1435. case 3:
  1436. s->avctx->frame_size = s->lsf ? 576 : 1152;
  1437. default:
  1438. nb_frames = mp_decode_layer3(s);
  1439. s->last_buf_size=0;
  1440. if (s->in_gb.buffer) {
  1441. align_get_bits(&s->gb);
  1442. i = (get_bits_left(&s->gb) >> 3) - s->extrasize;
  1443. if (i >= 0 && i <= BACKSTEP_SIZE) {
  1444. memmove(s->last_buf, s->gb.buffer + (get_bits_count(&s->gb)>>3), i);
  1445. s->last_buf_size=i;
  1446. } else
  1447. av_log(s->avctx, AV_LOG_ERROR, "invalid old backstep %d\n", i);
  1448. s->gb = s->in_gb;
  1449. s->in_gb.buffer = NULL;
  1450. s->extrasize = 0;
  1451. }
  1452. align_get_bits(&s->gb);
  1453. av_assert1((get_bits_count(&s->gb) & 7) == 0);
  1454. i = (get_bits_left(&s->gb) >> 3) - s->extrasize;
  1455. if (i < 0 || i > BACKSTEP_SIZE || nb_frames < 0) {
  1456. if (i < 0)
  1457. av_log(s->avctx, AV_LOG_ERROR, "invalid new backstep %d\n", i);
  1458. i = FFMIN(BACKSTEP_SIZE, buf_size - HEADER_SIZE);
  1459. }
  1460. av_assert1(i <= buf_size - HEADER_SIZE && i >= 0);
  1461. memcpy(s->last_buf + s->last_buf_size, s->gb.buffer + buf_size - HEADER_SIZE - i, i);
  1462. s->last_buf_size += i;
  1463. }
  1464. if(nb_frames < 0)
  1465. return nb_frames;
  1466. /* get output buffer */
  1467. if (!samples) {
  1468. av_assert0(s->frame);
  1469. s->frame->nb_samples = s->avctx->frame_size;
  1470. if ((ret = ff_get_buffer(s->avctx, s->frame, 0)) < 0)
  1471. return ret;
  1472. samples = (OUT_INT **)s->frame->extended_data;
  1473. }
  1474. /* apply the synthesis filter */
  1475. for (ch = 0; ch < s->nb_channels; ch++) {
  1476. int sample_stride;
  1477. if (s->avctx->sample_fmt == OUT_FMT_P) {
  1478. samples_ptr = samples[ch];
  1479. sample_stride = 1;
  1480. } else {
  1481. samples_ptr = samples[0] + ch;
  1482. sample_stride = s->nb_channels;
  1483. }
  1484. for (i = 0; i < nb_frames; i++) {
  1485. RENAME(ff_mpa_synth_filter)(&s->mpadsp, s->synth_buf[ch],
  1486. &(s->synth_buf_offset[ch]),
  1487. RENAME(ff_mpa_synth_window),
  1488. &s->dither_state, samples_ptr,
  1489. sample_stride, s->sb_samples[ch][i]);
  1490. samples_ptr += 32 * sample_stride;
  1491. }
  1492. }
  1493. return nb_frames * 32 * sizeof(OUT_INT) * s->nb_channels;
  1494. }
  1495. static int decode_frame(AVCodecContext * avctx, void *data, int *got_frame_ptr,
  1496. AVPacket *avpkt)
  1497. {
  1498. const uint8_t *buf = avpkt->data;
  1499. int buf_size = avpkt->size;
  1500. MPADecodeContext *s = avctx->priv_data;
  1501. uint32_t header;
  1502. int ret;
  1503. int skipped = 0;
  1504. while(buf_size && !*buf){
  1505. buf++;
  1506. buf_size--;
  1507. skipped++;
  1508. }
  1509. if (buf_size < HEADER_SIZE)
  1510. return AVERROR_INVALIDDATA;
  1511. header = AV_RB32(buf);
  1512. if (header>>8 == AV_RB32("TAG")>>8) {
  1513. av_log(avctx, AV_LOG_DEBUG, "discarding ID3 tag\n");
  1514. return buf_size + skipped;
  1515. }
  1516. ret = avpriv_mpegaudio_decode_header((MPADecodeHeader *)s, header);
  1517. if (ret < 0) {
  1518. av_log(avctx, AV_LOG_ERROR, "Header missing\n");
  1519. return AVERROR_INVALIDDATA;
  1520. } else if (ret == 1) {
  1521. /* free format: prepare to compute frame size */
  1522. s->frame_size = -1;
  1523. return AVERROR_INVALIDDATA;
  1524. }
  1525. /* update codec info */
  1526. avctx->channels = s->nb_channels;
  1527. avctx->channel_layout = s->nb_channels == 1 ? AV_CH_LAYOUT_MONO : AV_CH_LAYOUT_STEREO;
  1528. if (!avctx->bit_rate)
  1529. avctx->bit_rate = s->bit_rate;
  1530. if (s->frame_size <= 0) {
  1531. av_log(avctx, AV_LOG_ERROR, "incomplete frame\n");
  1532. return AVERROR_INVALIDDATA;
  1533. } else if (s->frame_size < buf_size) {
  1534. av_log(avctx, AV_LOG_DEBUG, "incorrect frame size - multiple frames in buffer?\n");
  1535. buf_size= s->frame_size;
  1536. }
  1537. s->frame = data;
  1538. ret = mp_decode_frame(s, NULL, buf, buf_size);
  1539. if (ret >= 0) {
  1540. s->frame->nb_samples = avctx->frame_size;
  1541. *got_frame_ptr = 1;
  1542. avctx->sample_rate = s->sample_rate;
  1543. //FIXME maybe move the other codec info stuff from above here too
  1544. } else {
  1545. av_log(avctx, AV_LOG_ERROR, "Error while decoding MPEG audio frame.\n");
  1546. /* Only return an error if the bad frame makes up the whole packet or
  1547. * the error is related to buffer management.
  1548. * If there is more data in the packet, just consume the bad frame
  1549. * instead of returning an error, which would discard the whole
  1550. * packet. */
  1551. *got_frame_ptr = 0;
  1552. if (buf_size == avpkt->size || ret != AVERROR_INVALIDDATA)
  1553. return ret;
  1554. }
  1555. s->frame_size = 0;
  1556. return buf_size + skipped;
  1557. }
  1558. static void mp_flush(MPADecodeContext *ctx)
  1559. {
  1560. memset(ctx->synth_buf, 0, sizeof(ctx->synth_buf));
  1561. memset(ctx->mdct_buf, 0, sizeof(ctx->mdct_buf));
  1562. ctx->last_buf_size = 0;
  1563. ctx->dither_state = 0;
  1564. }
  1565. static void flush(AVCodecContext *avctx)
  1566. {
  1567. mp_flush(avctx->priv_data);
  1568. }
  1569. #if CONFIG_MP3ADU_DECODER || CONFIG_MP3ADUFLOAT_DECODER
  1570. static int decode_frame_adu(AVCodecContext *avctx, void *data,
  1571. int *got_frame_ptr, AVPacket *avpkt)
  1572. {
  1573. const uint8_t *buf = avpkt->data;
  1574. int buf_size = avpkt->size;
  1575. MPADecodeContext *s = avctx->priv_data;
  1576. uint32_t header;
  1577. int len, ret;
  1578. int av_unused out_size;
  1579. len = buf_size;
  1580. // Discard too short frames
  1581. if (buf_size < HEADER_SIZE) {
  1582. av_log(avctx, AV_LOG_ERROR, "Packet is too small\n");
  1583. return AVERROR_INVALIDDATA;
  1584. }
  1585. if (len > MPA_MAX_CODED_FRAME_SIZE)
  1586. len = MPA_MAX_CODED_FRAME_SIZE;
  1587. // Get header and restore sync word
  1588. header = AV_RB32(buf) | 0xffe00000;
  1589. ret = avpriv_mpegaudio_decode_header((MPADecodeHeader *)s, header);
  1590. if (ret < 0) {
  1591. av_log(avctx, AV_LOG_ERROR, "Invalid frame header\n");
  1592. return ret;
  1593. }
  1594. /* update codec info */
  1595. avctx->sample_rate = s->sample_rate;
  1596. avctx->channels = s->nb_channels;
  1597. avctx->channel_layout = s->nb_channels == 1 ? AV_CH_LAYOUT_MONO : AV_CH_LAYOUT_STEREO;
  1598. if (!avctx->bit_rate)
  1599. avctx->bit_rate = s->bit_rate;
  1600. s->frame_size = len;
  1601. s->frame = data;
  1602. ret = mp_decode_frame(s, NULL, buf, buf_size);
  1603. if (ret < 0) {
  1604. av_log(avctx, AV_LOG_ERROR, "Error while decoding MPEG audio frame.\n");
  1605. return ret;
  1606. }
  1607. *got_frame_ptr = 1;
  1608. return buf_size;
  1609. }
  1610. #endif /* CONFIG_MP3ADU_DECODER || CONFIG_MP3ADUFLOAT_DECODER */
  1611. #if CONFIG_MP3ON4_DECODER || CONFIG_MP3ON4FLOAT_DECODER
  1612. /**
  1613. * Context for MP3On4 decoder
  1614. */
  1615. typedef struct MP3On4DecodeContext {
  1616. int frames; ///< number of mp3 frames per block (number of mp3 decoder instances)
  1617. int syncword; ///< syncword patch
  1618. const uint8_t *coff; ///< channel offsets in output buffer
  1619. MPADecodeContext *mp3decctx[5]; ///< MPADecodeContext for every decoder instance
  1620. } MP3On4DecodeContext;
  1621. #include "mpeg4audio.h"
  1622. /* Next 3 arrays are indexed by channel config number (passed via codecdata) */
  1623. /* number of mp3 decoder instances */
  1624. static const uint8_t mp3Frames[8] = { 0, 1, 1, 2, 3, 3, 4, 5 };
  1625. /* offsets into output buffer, assume output order is FL FR C LFE BL BR SL SR */
  1626. static const uint8_t chan_offset[8][5] = {
  1627. { 0 },
  1628. { 0 }, // C
  1629. { 0 }, // FLR
  1630. { 2, 0 }, // C FLR
  1631. { 2, 0, 3 }, // C FLR BS
  1632. { 2, 0, 3 }, // C FLR BLRS
  1633. { 2, 0, 4, 3 }, // C FLR BLRS LFE
  1634. { 2, 0, 6, 4, 3 }, // C FLR BLRS BLR LFE
  1635. };
  1636. /* mp3on4 channel layouts */
  1637. static const int16_t chan_layout[8] = {
  1638. 0,
  1639. AV_CH_LAYOUT_MONO,
  1640. AV_CH_LAYOUT_STEREO,
  1641. AV_CH_LAYOUT_SURROUND,
  1642. AV_CH_LAYOUT_4POINT0,
  1643. AV_CH_LAYOUT_5POINT0,
  1644. AV_CH_LAYOUT_5POINT1,
  1645. AV_CH_LAYOUT_7POINT1
  1646. };
  1647. static av_cold int decode_close_mp3on4(AVCodecContext * avctx)
  1648. {
  1649. MP3On4DecodeContext *s = avctx->priv_data;
  1650. int i;
  1651. if (s->mp3decctx[0])
  1652. av_freep(&s->mp3decctx[0]->fdsp);
  1653. for (i = 0; i < s->frames; i++)
  1654. av_freep(&s->mp3decctx[i]);
  1655. return 0;
  1656. }
  1657. static av_cold int decode_init_mp3on4(AVCodecContext * avctx)
  1658. {
  1659. MP3On4DecodeContext *s = avctx->priv_data;
  1660. MPEG4AudioConfig cfg;
  1661. int i;
  1662. if ((avctx->extradata_size < 2) || !avctx->extradata) {
  1663. av_log(avctx, AV_LOG_ERROR, "Codec extradata missing or too short.\n");
  1664. return AVERROR_INVALIDDATA;
  1665. }
  1666. avpriv_mpeg4audio_get_config(&cfg, avctx->extradata,
  1667. avctx->extradata_size * 8, 1);
  1668. if (!cfg.chan_config || cfg.chan_config > 7) {
  1669. av_log(avctx, AV_LOG_ERROR, "Invalid channel config number.\n");
  1670. return AVERROR_INVALIDDATA;
  1671. }
  1672. s->frames = mp3Frames[cfg.chan_config];
  1673. s->coff = chan_offset[cfg.chan_config];
  1674. avctx->channels = ff_mpeg4audio_channels[cfg.chan_config];
  1675. avctx->channel_layout = chan_layout[cfg.chan_config];
  1676. if (cfg.sample_rate < 16000)
  1677. s->syncword = 0xffe00000;
  1678. else
  1679. s->syncword = 0xfff00000;
  1680. /* Init the first mp3 decoder in standard way, so that all tables get builded
  1681. * We replace avctx->priv_data with the context of the first decoder so that
  1682. * decode_init() does not have to be changed.
  1683. * Other decoders will be initialized here copying data from the first context
  1684. */
  1685. // Allocate zeroed memory for the first decoder context
  1686. s->mp3decctx[0] = av_mallocz(sizeof(MPADecodeContext));
  1687. if (!s->mp3decctx[0])
  1688. goto alloc_fail;
  1689. // Put decoder context in place to make init_decode() happy
  1690. avctx->priv_data = s->mp3decctx[0];
  1691. decode_init(avctx);
  1692. // Restore mp3on4 context pointer
  1693. avctx->priv_data = s;
  1694. s->mp3decctx[0]->adu_mode = 1; // Set adu mode
  1695. /* Create a separate codec/context for each frame (first is already ok).
  1696. * Each frame is 1 or 2 channels - up to 5 frames allowed
  1697. */
  1698. for (i = 1; i < s->frames; i++) {
  1699. s->mp3decctx[i] = av_mallocz(sizeof(MPADecodeContext));
  1700. if (!s->mp3decctx[i])
  1701. goto alloc_fail;
  1702. s->mp3decctx[i]->adu_mode = 1;
  1703. s->mp3decctx[i]->avctx = avctx;
  1704. s->mp3decctx[i]->mpadsp = s->mp3decctx[0]->mpadsp;
  1705. s->mp3decctx[i]->fdsp = s->mp3decctx[0]->fdsp;
  1706. }
  1707. return 0;
  1708. alloc_fail:
  1709. decode_close_mp3on4(avctx);
  1710. return AVERROR(ENOMEM);
  1711. }
  1712. static void flush_mp3on4(AVCodecContext *avctx)
  1713. {
  1714. int i;
  1715. MP3On4DecodeContext *s = avctx->priv_data;
  1716. for (i = 0; i < s->frames; i++)
  1717. mp_flush(s->mp3decctx[i]);
  1718. }
  1719. static int decode_frame_mp3on4(AVCodecContext *avctx, void *data,
  1720. int *got_frame_ptr, AVPacket *avpkt)
  1721. {
  1722. AVFrame *frame = data;
  1723. const uint8_t *buf = avpkt->data;
  1724. int buf_size = avpkt->size;
  1725. MP3On4DecodeContext *s = avctx->priv_data;
  1726. MPADecodeContext *m;
  1727. int fsize, len = buf_size, out_size = 0;
  1728. uint32_t header;
  1729. OUT_INT **out_samples;
  1730. OUT_INT *outptr[2];
  1731. int fr, ch, ret;
  1732. /* get output buffer */
  1733. frame->nb_samples = MPA_FRAME_SIZE;
  1734. if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
  1735. return ret;
  1736. out_samples = (OUT_INT **)frame->extended_data;
  1737. // Discard too short frames
  1738. if (buf_size < HEADER_SIZE)
  1739. return AVERROR_INVALIDDATA;
  1740. avctx->bit_rate = 0;
  1741. ch = 0;
  1742. for (fr = 0; fr < s->frames; fr++) {
  1743. fsize = AV_RB16(buf) >> 4;
  1744. fsize = FFMIN3(fsize, len, MPA_MAX_CODED_FRAME_SIZE);
  1745. m = s->mp3decctx[fr];
  1746. av_assert1(m);
  1747. if (fsize < HEADER_SIZE) {
  1748. av_log(avctx, AV_LOG_ERROR, "Frame size smaller than header size\n");
  1749. return AVERROR_INVALIDDATA;
  1750. }
  1751. header = (AV_RB32(buf) & 0x000fffff) | s->syncword; // patch header
  1752. ret = avpriv_mpegaudio_decode_header((MPADecodeHeader *)m, header);
  1753. if (ret < 0) {
  1754. av_log(avctx, AV_LOG_ERROR, "Bad header, discard block\n");
  1755. return AVERROR_INVALIDDATA;
  1756. }
  1757. if (ch + m->nb_channels > avctx->channels ||
  1758. s->coff[fr] + m->nb_channels > avctx->channels) {
  1759. av_log(avctx, AV_LOG_ERROR, "frame channel count exceeds codec "
  1760. "channel count\n");
  1761. return AVERROR_INVALIDDATA;
  1762. }
  1763. ch += m->nb_channels;
  1764. outptr[0] = out_samples[s->coff[fr]];
  1765. if (m->nb_channels > 1)
  1766. outptr[1] = out_samples[s->coff[fr] + 1];
  1767. if ((ret = mp_decode_frame(m, outptr, buf, fsize)) < 0) {
  1768. av_log(avctx, AV_LOG_ERROR, "failed to decode channel %d\n", ch);
  1769. memset(outptr[0], 0, MPA_FRAME_SIZE*sizeof(OUT_INT));
  1770. if (m->nb_channels > 1)
  1771. memset(outptr[1], 0, MPA_FRAME_SIZE*sizeof(OUT_INT));
  1772. ret = m->nb_channels * MPA_FRAME_SIZE*sizeof(OUT_INT);
  1773. }
  1774. out_size += ret;
  1775. buf += fsize;
  1776. len -= fsize;
  1777. avctx->bit_rate += m->bit_rate;
  1778. }
  1779. if (ch != avctx->channels) {
  1780. av_log(avctx, AV_LOG_ERROR, "failed to decode all channels\n");
  1781. return AVERROR_INVALIDDATA;
  1782. }
  1783. /* update codec info */
  1784. avctx->sample_rate = s->mp3decctx[0]->sample_rate;
  1785. frame->nb_samples = out_size / (avctx->channels * sizeof(OUT_INT));
  1786. *got_frame_ptr = 1;
  1787. return buf_size;
  1788. }
  1789. #endif /* CONFIG_MP3ON4_DECODER || CONFIG_MP3ON4FLOAT_DECODER */