minimp3.c 83 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656
  1. /*
  2. * MPEG Audio Layer III decoder
  3. * Copyright (c) 2001, 2002 Fabrice Bellard,
  4. * (c) 2007 Martin J. Fiedler
  5. *
  6. * This file is a stripped-down version of the MPEG Audio decoder from
  7. * the FFmpeg libavcodec library.
  8. *
  9. * FFmpeg and minimp3 are free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU Lesser General Public
  11. * License as published by the Free Software Foundation; either
  12. * version 2.1 of the License, or (at your option) any later version.
  13. *
  14. * FFmpeg and minimp3 are distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * Lesser General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public
  20. * License along with FFmpeg; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  22. */
  23. #include "libc.h"
  24. #include "minimp3.h"
  25. #define MP3_FRAME_SIZE 1152
  26. #define MP3_MAX_CODED_FRAME_SIZE 1792
  27. #define MP3_MAX_CHANNELS 2
  28. #define SBLIMIT 32
  29. #define MP3_STEREO 0
  30. #define MP3_JSTEREO 1
  31. #define MP3_DUAL 2
  32. #define MP3_MONO 3
  33. #define SAME_HEADER_MASK \
  34. (0xffe00000 | (3 << 17) | (0xf << 12) | (3 << 10) | (3 << 19))
  35. #define FRAC_BITS 15
  36. #define WFRAC_BITS 14
  37. #define OUT_MAX (32767)
  38. #define OUT_MIN (-32768)
  39. #define OUT_SHIFT (WFRAC_BITS + FRAC_BITS - 15)
  40. #define MODE_EXT_MS_STEREO 2
  41. #define MODE_EXT_I_STEREO 1
  42. #define FRAC_ONE (1 << FRAC_BITS)
  43. #define FIX(a) ((int)((a) * FRAC_ONE))
  44. #define FIXR(a) ((int)((a) * FRAC_ONE + 0.5))
  45. #define FRAC_RND(a) (((a) + (FRAC_ONE/2)) >> FRAC_BITS)
  46. #define FIXHR(a) ((int)((a) * (1LL<<32) + 0.5))
  47. #ifndef _MSC_VER
  48. #define MULL(a,b) (((int64_t)(a) * (int64_t)(b)) >> FRAC_BITS)
  49. #define MULH(a,b) (((int64_t)(a) * (int64_t)(b)) >> 32)
  50. #else
  51. static INLINE int MULL(int a, int b) {
  52. int res;
  53. __asm {
  54. mov eax, a
  55. imul b
  56. shr eax, 15
  57. shl edx, 17
  58. or eax, edx
  59. mov res, eax
  60. }
  61. return res;
  62. }
  63. static INLINE int MULH(int a, int b) {
  64. int res;
  65. __asm {
  66. mov eax, a
  67. imul b
  68. mov res, edx
  69. }
  70. return res;
  71. }
  72. #endif
  73. #define MULS(ra, rb) ((ra) * (rb))
  74. #define ISQRT2 FIXR(0.70710678118654752440)
  75. #define HEADER_SIZE 4
  76. #define BACKSTEP_SIZE 512
  77. #define EXTRABYTES 24
  78. #define VLC_TYPE int16_t
  79. ////////////////////////////////////////////////////////////////////////////////
  80. struct _granule;
  81. typedef struct _bitstream {
  82. const uint8_t *buffer, *buffer_end;
  83. int index;
  84. int size_in_bits;
  85. } bitstream_t;
  86. typedef struct _vlc {
  87. int bits;
  88. VLC_TYPE (*table)[2]; ///< code, bits
  89. int table_size, table_allocated;
  90. } vlc_t;
  91. typedef struct _mp3_context {
  92. uint8_t last_buf[2*BACKSTEP_SIZE + EXTRABYTES];
  93. int last_buf_size;
  94. int frame_size;
  95. uint32_t free_format_next_header;
  96. int error_protection;
  97. int sample_rate;
  98. int sample_rate_index;
  99. int bit_rate;
  100. bitstream_t gb;
  101. bitstream_t in_gb;
  102. int nb_channels;
  103. int mode;
  104. int mode_ext;
  105. int lsf;
  106. int16_t synth_buf[MP3_MAX_CHANNELS][512 * 2];
  107. int synth_buf_offset[MP3_MAX_CHANNELS];
  108. int32_t sb_samples[MP3_MAX_CHANNELS][36][SBLIMIT];
  109. int32_t mdct_buf[MP3_MAX_CHANNELS][SBLIMIT * 18];
  110. int dither_state;
  111. } mp3_context_t;
  112. typedef struct _granule {
  113. uint8_t scfsi;
  114. int part2_3_length;
  115. int big_values;
  116. int global_gain;
  117. int scalefac_compress;
  118. uint8_t block_type;
  119. uint8_t switch_point;
  120. int table_select[3];
  121. int subblock_gain[3];
  122. uint8_t scalefac_scale;
  123. uint8_t count1table_select;
  124. int region_size[3];
  125. int preflag;
  126. int short_start, long_end;
  127. uint8_t scale_factors[40];
  128. int32_t sb_hybrid[SBLIMIT * 18];
  129. } granule_t;
  130. typedef struct _huff_table {
  131. int xsize;
  132. const uint8_t *bits;
  133. const uint16_t *codes;
  134. } huff_table_t;
  135. static vlc_t huff_vlc[16];
  136. static vlc_t huff_quad_vlc[2];
  137. static uint16_t band_index_long[9][23];
  138. #define TABLE_4_3_SIZE (8191 + 16)*4
  139. static int8_t *table_4_3_exp;
  140. static uint32_t *table_4_3_value;
  141. static uint32_t exp_table[512];
  142. static uint32_t expval_table[512][16];
  143. static int32_t is_table[2][16];
  144. static int32_t is_table_lsf[2][2][16];
  145. static int32_t csa_table[8][4];
  146. static float csa_table_float[8][4];
  147. static int32_t mdct_win[8][36];
  148. static int16_t window[512];
  149. ////////////////////////////////////////////////////////////////////////////////
  150. static const uint16_t mp3_bitrate_tab[2][15] = {
  151. {0, 32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320 },
  152. {0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160}
  153. };
  154. static const uint16_t mp3_freq_tab[3] = { 44100, 48000, 32000 };
  155. static const int32_t mp3_enwindow[257] = {
  156. 0, -1, -1, -1, -1, -1, -1, -2,
  157. -2, -2, -2, -3, -3, -4, -4, -5,
  158. -5, -6, -7, -7, -8, -9, -10, -11,
  159. -13, -14, -16, -17, -19, -21, -24, -26,
  160. -29, -31, -35, -38, -41, -45, -49, -53,
  161. -58, -63, -68, -73, -79, -85, -91, -97,
  162. -104, -111, -117, -125, -132, -139, -147, -154,
  163. -161, -169, -176, -183, -190, -196, -202, -208,
  164. 213, 218, 222, 225, 227, 228, 228, 227,
  165. 224, 221, 215, 208, 200, 189, 177, 163,
  166. 146, 127, 106, 83, 57, 29, -2, -36,
  167. -72, -111, -153, -197, -244, -294, -347, -401,
  168. -459, -519, -581, -645, -711, -779, -848, -919,
  169. -991, -1064, -1137, -1210, -1283, -1356, -1428, -1498,
  170. -1567, -1634, -1698, -1759, -1817, -1870, -1919, -1962,
  171. -2001, -2032, -2057, -2075, -2085, -2087, -2080, -2063,
  172. 2037, 2000, 1952, 1893, 1822, 1739, 1644, 1535,
  173. 1414, 1280, 1131, 970, 794, 605, 402, 185,
  174. -45, -288, -545, -814, -1095, -1388, -1692, -2006,
  175. -2330, -2663, -3004, -3351, -3705, -4063, -4425, -4788,
  176. -5153, -5517, -5879, -6237, -6589, -6935, -7271, -7597,
  177. -7910, -8209, -8491, -8755, -8998, -9219, -9416, -9585,
  178. -9727, -9838, -9916, -9959, -9966, -9935, -9863, -9750,
  179. -9592, -9389, -9139, -8840, -8492, -8092, -7640, -7134,
  180. 6574, 5959, 5288, 4561, 3776, 2935, 2037, 1082,
  181. 70, -998, -2122, -3300, -4533, -5818, -7154, -8540,
  182. -9975,-11455,-12980,-14548,-16155,-17799,-19478,-21189,
  183. -22929,-24694,-26482,-28289,-30112,-31947,-33791,-35640,
  184. -37489,-39336,-41176,-43006,-44821,-46617,-48390,-50137,
  185. -51853,-53534,-55178,-56778,-58333,-59838,-61289,-62684,
  186. -64019,-65290,-66494,-67629,-68692,-69679,-70590,-71420,
  187. -72169,-72835,-73415,-73908,-74313,-74630,-74856,-74992,
  188. 75038,
  189. };
  190. static const uint8_t slen_table[2][16] = {
  191. { 0, 0, 0, 0, 3, 1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4 },
  192. { 0, 1, 2, 3, 0, 1, 2, 3, 1, 2, 3, 1, 2, 3, 2, 3 },
  193. };
  194. static const uint8_t lsf_nsf_table[6][3][4] = {
  195. { { 6, 5, 5, 5 }, { 9, 9, 9, 9 }, { 6, 9, 9, 9 } },
  196. { { 6, 5, 7, 3 }, { 9, 9, 12, 6 }, { 6, 9, 12, 6 } },
  197. { { 11, 10, 0, 0 }, { 18, 18, 0, 0 }, { 15, 18, 0, 0 } },
  198. { { 7, 7, 7, 0 }, { 12, 12, 12, 0 }, { 6, 15, 12, 0 } },
  199. { { 6, 6, 6, 3 }, { 12, 9, 9, 6 }, { 6, 12, 9, 6 } },
  200. { { 8, 8, 5, 0 }, { 15, 12, 9, 0 }, { 6, 18, 9, 0 } },
  201. };
  202. static const uint16_t mp3_huffcodes_1[4] = {
  203. 0x0001, 0x0001, 0x0001, 0x0000,
  204. };
  205. static const uint8_t mp3_huffbits_1[4] = {
  206. 1, 3, 2, 3,
  207. };
  208. static const uint16_t mp3_huffcodes_2[9] = {
  209. 0x0001, 0x0002, 0x0001, 0x0003, 0x0001, 0x0001, 0x0003, 0x0002,
  210. 0x0000,
  211. };
  212. static const uint8_t mp3_huffbits_2[9] = {
  213. 1, 3, 6, 3, 3, 5, 5, 5,
  214. 6,
  215. };
  216. static const uint16_t mp3_huffcodes_3[9] = {
  217. 0x0003, 0x0002, 0x0001, 0x0001, 0x0001, 0x0001, 0x0003, 0x0002,
  218. 0x0000,
  219. };
  220. static const uint8_t mp3_huffbits_3[9] = {
  221. 2, 2, 6, 3, 2, 5, 5, 5,
  222. 6,
  223. };
  224. static const uint16_t mp3_huffcodes_5[16] = {
  225. 0x0001, 0x0002, 0x0006, 0x0005, 0x0003, 0x0001, 0x0004, 0x0004,
  226. 0x0007, 0x0005, 0x0007, 0x0001, 0x0006, 0x0001, 0x0001, 0x0000,
  227. };
  228. static const uint8_t mp3_huffbits_5[16] = {
  229. 1, 3, 6, 7, 3, 3, 6, 7,
  230. 6, 6, 7, 8, 7, 6, 7, 8,
  231. };
  232. static const uint16_t mp3_huffcodes_6[16] = {
  233. 0x0007, 0x0003, 0x0005, 0x0001, 0x0006, 0x0002, 0x0003, 0x0002,
  234. 0x0005, 0x0004, 0x0004, 0x0001, 0x0003, 0x0003, 0x0002, 0x0000,
  235. };
  236. static const uint8_t mp3_huffbits_6[16] = {
  237. 3, 3, 5, 7, 3, 2, 4, 5,
  238. 4, 4, 5, 6, 6, 5, 6, 7,
  239. };
  240. static const uint16_t mp3_huffcodes_7[36] = {
  241. 0x0001, 0x0002, 0x000a, 0x0013, 0x0010, 0x000a, 0x0003, 0x0003,
  242. 0x0007, 0x000a, 0x0005, 0x0003, 0x000b, 0x0004, 0x000d, 0x0011,
  243. 0x0008, 0x0004, 0x000c, 0x000b, 0x0012, 0x000f, 0x000b, 0x0002,
  244. 0x0007, 0x0006, 0x0009, 0x000e, 0x0003, 0x0001, 0x0006, 0x0004,
  245. 0x0005, 0x0003, 0x0002, 0x0000,
  246. };
  247. static const uint8_t mp3_huffbits_7[36] = {
  248. 1, 3, 6, 8, 8, 9, 3, 4,
  249. 6, 7, 7, 8, 6, 5, 7, 8,
  250. 8, 9, 7, 7, 8, 9, 9, 9,
  251. 7, 7, 8, 9, 9, 10, 8, 8,
  252. 9, 10, 10, 10,
  253. };
  254. static const uint16_t mp3_huffcodes_8[36] = {
  255. 0x0003, 0x0004, 0x0006, 0x0012, 0x000c, 0x0005, 0x0005, 0x0001,
  256. 0x0002, 0x0010, 0x0009, 0x0003, 0x0007, 0x0003, 0x0005, 0x000e,
  257. 0x0007, 0x0003, 0x0013, 0x0011, 0x000f, 0x000d, 0x000a, 0x0004,
  258. 0x000d, 0x0005, 0x0008, 0x000b, 0x0005, 0x0001, 0x000c, 0x0004,
  259. 0x0004, 0x0001, 0x0001, 0x0000,
  260. };
  261. static const uint8_t mp3_huffbits_8[36] = {
  262. 2, 3, 6, 8, 8, 9, 3, 2,
  263. 4, 8, 8, 8, 6, 4, 6, 8,
  264. 8, 9, 8, 8, 8, 9, 9, 10,
  265. 8, 7, 8, 9, 10, 10, 9, 8,
  266. 9, 9, 11, 11,
  267. };
  268. static const uint16_t mp3_huffcodes_9[36] = {
  269. 0x0007, 0x0005, 0x0009, 0x000e, 0x000f, 0x0007, 0x0006, 0x0004,
  270. 0x0005, 0x0005, 0x0006, 0x0007, 0x0007, 0x0006, 0x0008, 0x0008,
  271. 0x0008, 0x0005, 0x000f, 0x0006, 0x0009, 0x000a, 0x0005, 0x0001,
  272. 0x000b, 0x0007, 0x0009, 0x0006, 0x0004, 0x0001, 0x000e, 0x0004,
  273. 0x0006, 0x0002, 0x0006, 0x0000,
  274. };
  275. static const uint8_t mp3_huffbits_9[36] = {
  276. 3, 3, 5, 6, 8, 9, 3, 3,
  277. 4, 5, 6, 8, 4, 4, 5, 6,
  278. 7, 8, 6, 5, 6, 7, 7, 8,
  279. 7, 6, 7, 7, 8, 9, 8, 7,
  280. 8, 8, 9, 9,
  281. };
  282. static const uint16_t mp3_huffcodes_10[64] = {
  283. 0x0001, 0x0002, 0x000a, 0x0017, 0x0023, 0x001e, 0x000c, 0x0011,
  284. 0x0003, 0x0003, 0x0008, 0x000c, 0x0012, 0x0015, 0x000c, 0x0007,
  285. 0x000b, 0x0009, 0x000f, 0x0015, 0x0020, 0x0028, 0x0013, 0x0006,
  286. 0x000e, 0x000d, 0x0016, 0x0022, 0x002e, 0x0017, 0x0012, 0x0007,
  287. 0x0014, 0x0013, 0x0021, 0x002f, 0x001b, 0x0016, 0x0009, 0x0003,
  288. 0x001f, 0x0016, 0x0029, 0x001a, 0x0015, 0x0014, 0x0005, 0x0003,
  289. 0x000e, 0x000d, 0x000a, 0x000b, 0x0010, 0x0006, 0x0005, 0x0001,
  290. 0x0009, 0x0008, 0x0007, 0x0008, 0x0004, 0x0004, 0x0002, 0x0000,
  291. };
  292. static const uint8_t mp3_huffbits_10[64] = {
  293. 1, 3, 6, 8, 9, 9, 9, 10,
  294. 3, 4, 6, 7, 8, 9, 8, 8,
  295. 6, 6, 7, 8, 9, 10, 9, 9,
  296. 7, 7, 8, 9, 10, 10, 9, 10,
  297. 8, 8, 9, 10, 10, 10, 10, 10,
  298. 9, 9, 10, 10, 11, 11, 10, 11,
  299. 8, 8, 9, 10, 10, 10, 11, 11,
  300. 9, 8, 9, 10, 10, 11, 11, 11,
  301. };
  302. static const uint16_t mp3_huffcodes_11[64] = {
  303. 0x0003, 0x0004, 0x000a, 0x0018, 0x0022, 0x0021, 0x0015, 0x000f,
  304. 0x0005, 0x0003, 0x0004, 0x000a, 0x0020, 0x0011, 0x000b, 0x000a,
  305. 0x000b, 0x0007, 0x000d, 0x0012, 0x001e, 0x001f, 0x0014, 0x0005,
  306. 0x0019, 0x000b, 0x0013, 0x003b, 0x001b, 0x0012, 0x000c, 0x0005,
  307. 0x0023, 0x0021, 0x001f, 0x003a, 0x001e, 0x0010, 0x0007, 0x0005,
  308. 0x001c, 0x001a, 0x0020, 0x0013, 0x0011, 0x000f, 0x0008, 0x000e,
  309. 0x000e, 0x000c, 0x0009, 0x000d, 0x000e, 0x0009, 0x0004, 0x0001,
  310. 0x000b, 0x0004, 0x0006, 0x0006, 0x0006, 0x0003, 0x0002, 0x0000,
  311. };
  312. static const uint8_t mp3_huffbits_11[64] = {
  313. 2, 3, 5, 7, 8, 9, 8, 9,
  314. 3, 3, 4, 6, 8, 8, 7, 8,
  315. 5, 5, 6, 7, 8, 9, 8, 8,
  316. 7, 6, 7, 9, 8, 10, 8, 9,
  317. 8, 8, 8, 9, 9, 10, 9, 10,
  318. 8, 8, 9, 10, 10, 11, 10, 11,
  319. 8, 7, 7, 8, 9, 10, 10, 10,
  320. 8, 7, 8, 9, 10, 10, 10, 10,
  321. };
  322. static const uint16_t mp3_huffcodes_12[64] = {
  323. 0x0009, 0x0006, 0x0010, 0x0021, 0x0029, 0x0027, 0x0026, 0x001a,
  324. 0x0007, 0x0005, 0x0006, 0x0009, 0x0017, 0x0010, 0x001a, 0x000b,
  325. 0x0011, 0x0007, 0x000b, 0x000e, 0x0015, 0x001e, 0x000a, 0x0007,
  326. 0x0011, 0x000a, 0x000f, 0x000c, 0x0012, 0x001c, 0x000e, 0x0005,
  327. 0x0020, 0x000d, 0x0016, 0x0013, 0x0012, 0x0010, 0x0009, 0x0005,
  328. 0x0028, 0x0011, 0x001f, 0x001d, 0x0011, 0x000d, 0x0004, 0x0002,
  329. 0x001b, 0x000c, 0x000b, 0x000f, 0x000a, 0x0007, 0x0004, 0x0001,
  330. 0x001b, 0x000c, 0x0008, 0x000c, 0x0006, 0x0003, 0x0001, 0x0000,
  331. };
  332. static const uint8_t mp3_huffbits_12[64] = {
  333. 4, 3, 5, 7, 8, 9, 9, 9,
  334. 3, 3, 4, 5, 7, 7, 8, 8,
  335. 5, 4, 5, 6, 7, 8, 7, 8,
  336. 6, 5, 6, 6, 7, 8, 8, 8,
  337. 7, 6, 7, 7, 8, 8, 8, 9,
  338. 8, 7, 8, 8, 8, 9, 8, 9,
  339. 8, 7, 7, 8, 8, 9, 9, 10,
  340. 9, 8, 8, 9, 9, 9, 9, 10,
  341. };
  342. static const uint16_t mp3_huffcodes_13[256] = {
  343. 0x0001, 0x0005, 0x000e, 0x0015, 0x0022, 0x0033, 0x002e, 0x0047,
  344. 0x002a, 0x0034, 0x0044, 0x0034, 0x0043, 0x002c, 0x002b, 0x0013,
  345. 0x0003, 0x0004, 0x000c, 0x0013, 0x001f, 0x001a, 0x002c, 0x0021,
  346. 0x001f, 0x0018, 0x0020, 0x0018, 0x001f, 0x0023, 0x0016, 0x000e,
  347. 0x000f, 0x000d, 0x0017, 0x0024, 0x003b, 0x0031, 0x004d, 0x0041,
  348. 0x001d, 0x0028, 0x001e, 0x0028, 0x001b, 0x0021, 0x002a, 0x0010,
  349. 0x0016, 0x0014, 0x0025, 0x003d, 0x0038, 0x004f, 0x0049, 0x0040,
  350. 0x002b, 0x004c, 0x0038, 0x0025, 0x001a, 0x001f, 0x0019, 0x000e,
  351. 0x0023, 0x0010, 0x003c, 0x0039, 0x0061, 0x004b, 0x0072, 0x005b,
  352. 0x0036, 0x0049, 0x0037, 0x0029, 0x0030, 0x0035, 0x0017, 0x0018,
  353. 0x003a, 0x001b, 0x0032, 0x0060, 0x004c, 0x0046, 0x005d, 0x0054,
  354. 0x004d, 0x003a, 0x004f, 0x001d, 0x004a, 0x0031, 0x0029, 0x0011,
  355. 0x002f, 0x002d, 0x004e, 0x004a, 0x0073, 0x005e, 0x005a, 0x004f,
  356. 0x0045, 0x0053, 0x0047, 0x0032, 0x003b, 0x0026, 0x0024, 0x000f,
  357. 0x0048, 0x0022, 0x0038, 0x005f, 0x005c, 0x0055, 0x005b, 0x005a,
  358. 0x0056, 0x0049, 0x004d, 0x0041, 0x0033, 0x002c, 0x002b, 0x002a,
  359. 0x002b, 0x0014, 0x001e, 0x002c, 0x0037, 0x004e, 0x0048, 0x0057,
  360. 0x004e, 0x003d, 0x002e, 0x0036, 0x0025, 0x001e, 0x0014, 0x0010,
  361. 0x0035, 0x0019, 0x0029, 0x0025, 0x002c, 0x003b, 0x0036, 0x0051,
  362. 0x0042, 0x004c, 0x0039, 0x0036, 0x0025, 0x0012, 0x0027, 0x000b,
  363. 0x0023, 0x0021, 0x001f, 0x0039, 0x002a, 0x0052, 0x0048, 0x0050,
  364. 0x002f, 0x003a, 0x0037, 0x0015, 0x0016, 0x001a, 0x0026, 0x0016,
  365. 0x0035, 0x0019, 0x0017, 0x0026, 0x0046, 0x003c, 0x0033, 0x0024,
  366. 0x0037, 0x001a, 0x0022, 0x0017, 0x001b, 0x000e, 0x0009, 0x0007,
  367. 0x0022, 0x0020, 0x001c, 0x0027, 0x0031, 0x004b, 0x001e, 0x0034,
  368. 0x0030, 0x0028, 0x0034, 0x001c, 0x0012, 0x0011, 0x0009, 0x0005,
  369. 0x002d, 0x0015, 0x0022, 0x0040, 0x0038, 0x0032, 0x0031, 0x002d,
  370. 0x001f, 0x0013, 0x000c, 0x000f, 0x000a, 0x0007, 0x0006, 0x0003,
  371. 0x0030, 0x0017, 0x0014, 0x0027, 0x0024, 0x0023, 0x0035, 0x0015,
  372. 0x0010, 0x0017, 0x000d, 0x000a, 0x0006, 0x0001, 0x0004, 0x0002,
  373. 0x0010, 0x000f, 0x0011, 0x001b, 0x0019, 0x0014, 0x001d, 0x000b,
  374. 0x0011, 0x000c, 0x0010, 0x0008, 0x0001, 0x0001, 0x0000, 0x0001,
  375. };
  376. static const uint8_t mp3_huffbits_13[256] = {
  377. 1, 4, 6, 7, 8, 9, 9, 10,
  378. 9, 10, 11, 11, 12, 12, 13, 13,
  379. 3, 4, 6, 7, 8, 8, 9, 9,
  380. 9, 9, 10, 10, 11, 12, 12, 12,
  381. 6, 6, 7, 8, 9, 9, 10, 10,
  382. 9, 10, 10, 11, 11, 12, 13, 13,
  383. 7, 7, 8, 9, 9, 10, 10, 10,
  384. 10, 11, 11, 11, 11, 12, 13, 13,
  385. 8, 7, 9, 9, 10, 10, 11, 11,
  386. 10, 11, 11, 12, 12, 13, 13, 14,
  387. 9, 8, 9, 10, 10, 10, 11, 11,
  388. 11, 11, 12, 11, 13, 13, 14, 14,
  389. 9, 9, 10, 10, 11, 11, 11, 11,
  390. 11, 12, 12, 12, 13, 13, 14, 14,
  391. 10, 9, 10, 11, 11, 11, 12, 12,
  392. 12, 12, 13, 13, 13, 14, 16, 16,
  393. 9, 8, 9, 10, 10, 11, 11, 12,
  394. 12, 12, 12, 13, 13, 14, 15, 15,
  395. 10, 9, 10, 10, 11, 11, 11, 13,
  396. 12, 13, 13, 14, 14, 14, 16, 15,
  397. 10, 10, 10, 11, 11, 12, 12, 13,
  398. 12, 13, 14, 13, 14, 15, 16, 17,
  399. 11, 10, 10, 11, 12, 12, 12, 12,
  400. 13, 13, 13, 14, 15, 15, 15, 16,
  401. 11, 11, 11, 12, 12, 13, 12, 13,
  402. 14, 14, 15, 15, 15, 16, 16, 16,
  403. 12, 11, 12, 13, 13, 13, 14, 14,
  404. 14, 14, 14, 15, 16, 15, 16, 16,
  405. 13, 12, 12, 13, 13, 13, 15, 14,
  406. 14, 17, 15, 15, 15, 17, 16, 16,
  407. 12, 12, 13, 14, 14, 14, 15, 14,
  408. 15, 15, 16, 16, 19, 18, 19, 16,
  409. };
  410. static const uint16_t mp3_huffcodes_15[256] = {
  411. 0x0007, 0x000c, 0x0012, 0x0035, 0x002f, 0x004c, 0x007c, 0x006c,
  412. 0x0059, 0x007b, 0x006c, 0x0077, 0x006b, 0x0051, 0x007a, 0x003f,
  413. 0x000d, 0x0005, 0x0010, 0x001b, 0x002e, 0x0024, 0x003d, 0x0033,
  414. 0x002a, 0x0046, 0x0034, 0x0053, 0x0041, 0x0029, 0x003b, 0x0024,
  415. 0x0013, 0x0011, 0x000f, 0x0018, 0x0029, 0x0022, 0x003b, 0x0030,
  416. 0x0028, 0x0040, 0x0032, 0x004e, 0x003e, 0x0050, 0x0038, 0x0021,
  417. 0x001d, 0x001c, 0x0019, 0x002b, 0x0027, 0x003f, 0x0037, 0x005d,
  418. 0x004c, 0x003b, 0x005d, 0x0048, 0x0036, 0x004b, 0x0032, 0x001d,
  419. 0x0034, 0x0016, 0x002a, 0x0028, 0x0043, 0x0039, 0x005f, 0x004f,
  420. 0x0048, 0x0039, 0x0059, 0x0045, 0x0031, 0x0042, 0x002e, 0x001b,
  421. 0x004d, 0x0025, 0x0023, 0x0042, 0x003a, 0x0034, 0x005b, 0x004a,
  422. 0x003e, 0x0030, 0x004f, 0x003f, 0x005a, 0x003e, 0x0028, 0x0026,
  423. 0x007d, 0x0020, 0x003c, 0x0038, 0x0032, 0x005c, 0x004e, 0x0041,
  424. 0x0037, 0x0057, 0x0047, 0x0033, 0x0049, 0x0033, 0x0046, 0x001e,
  425. 0x006d, 0x0035, 0x0031, 0x005e, 0x0058, 0x004b, 0x0042, 0x007a,
  426. 0x005b, 0x0049, 0x0038, 0x002a, 0x0040, 0x002c, 0x0015, 0x0019,
  427. 0x005a, 0x002b, 0x0029, 0x004d, 0x0049, 0x003f, 0x0038, 0x005c,
  428. 0x004d, 0x0042, 0x002f, 0x0043, 0x0030, 0x0035, 0x0024, 0x0014,
  429. 0x0047, 0x0022, 0x0043, 0x003c, 0x003a, 0x0031, 0x0058, 0x004c,
  430. 0x0043, 0x006a, 0x0047, 0x0036, 0x0026, 0x0027, 0x0017, 0x000f,
  431. 0x006d, 0x0035, 0x0033, 0x002f, 0x005a, 0x0052, 0x003a, 0x0039,
  432. 0x0030, 0x0048, 0x0039, 0x0029, 0x0017, 0x001b, 0x003e, 0x0009,
  433. 0x0056, 0x002a, 0x0028, 0x0025, 0x0046, 0x0040, 0x0034, 0x002b,
  434. 0x0046, 0x0037, 0x002a, 0x0019, 0x001d, 0x0012, 0x000b, 0x000b,
  435. 0x0076, 0x0044, 0x001e, 0x0037, 0x0032, 0x002e, 0x004a, 0x0041,
  436. 0x0031, 0x0027, 0x0018, 0x0010, 0x0016, 0x000d, 0x000e, 0x0007,
  437. 0x005b, 0x002c, 0x0027, 0x0026, 0x0022, 0x003f, 0x0034, 0x002d,
  438. 0x001f, 0x0034, 0x001c, 0x0013, 0x000e, 0x0008, 0x0009, 0x0003,
  439. 0x007b, 0x003c, 0x003a, 0x0035, 0x002f, 0x002b, 0x0020, 0x0016,
  440. 0x0025, 0x0018, 0x0011, 0x000c, 0x000f, 0x000a, 0x0002, 0x0001,
  441. 0x0047, 0x0025, 0x0022, 0x001e, 0x001c, 0x0014, 0x0011, 0x001a,
  442. 0x0015, 0x0010, 0x000a, 0x0006, 0x0008, 0x0006, 0x0002, 0x0000,
  443. };
  444. static const uint8_t mp3_huffbits_15[256] = {
  445. 3, 4, 5, 7, 7, 8, 9, 9,
  446. 9, 10, 10, 11, 11, 11, 12, 13,
  447. 4, 3, 5, 6, 7, 7, 8, 8,
  448. 8, 9, 9, 10, 10, 10, 11, 11,
  449. 5, 5, 5, 6, 7, 7, 8, 8,
  450. 8, 9, 9, 10, 10, 11, 11, 11,
  451. 6, 6, 6, 7, 7, 8, 8, 9,
  452. 9, 9, 10, 10, 10, 11, 11, 11,
  453. 7, 6, 7, 7, 8, 8, 9, 9,
  454. 9, 9, 10, 10, 10, 11, 11, 11,
  455. 8, 7, 7, 8, 8, 8, 9, 9,
  456. 9, 9, 10, 10, 11, 11, 11, 12,
  457. 9, 7, 8, 8, 8, 9, 9, 9,
  458. 9, 10, 10, 10, 11, 11, 12, 12,
  459. 9, 8, 8, 9, 9, 9, 9, 10,
  460. 10, 10, 10, 10, 11, 11, 11, 12,
  461. 9, 8, 8, 9, 9, 9, 9, 10,
  462. 10, 10, 10, 11, 11, 12, 12, 12,
  463. 9, 8, 9, 9, 9, 9, 10, 10,
  464. 10, 11, 11, 11, 11, 12, 12, 12,
  465. 10, 9, 9, 9, 10, 10, 10, 10,
  466. 10, 11, 11, 11, 11, 12, 13, 12,
  467. 10, 9, 9, 9, 10, 10, 10, 10,
  468. 11, 11, 11, 11, 12, 12, 12, 13,
  469. 11, 10, 9, 10, 10, 10, 11, 11,
  470. 11, 11, 11, 11, 12, 12, 13, 13,
  471. 11, 10, 10, 10, 10, 11, 11, 11,
  472. 11, 12, 12, 12, 12, 12, 13, 13,
  473. 12, 11, 11, 11, 11, 11, 11, 11,
  474. 12, 12, 12, 12, 13, 13, 12, 13,
  475. 12, 11, 11, 11, 11, 11, 11, 12,
  476. 12, 12, 12, 12, 13, 13, 13, 13,
  477. };
  478. static const uint16_t mp3_huffcodes_16[256] = {
  479. 0x0001, 0x0005, 0x000e, 0x002c, 0x004a, 0x003f, 0x006e, 0x005d,
  480. 0x00ac, 0x0095, 0x008a, 0x00f2, 0x00e1, 0x00c3, 0x0178, 0x0011,
  481. 0x0003, 0x0004, 0x000c, 0x0014, 0x0023, 0x003e, 0x0035, 0x002f,
  482. 0x0053, 0x004b, 0x0044, 0x0077, 0x00c9, 0x006b, 0x00cf, 0x0009,
  483. 0x000f, 0x000d, 0x0017, 0x0026, 0x0043, 0x003a, 0x0067, 0x005a,
  484. 0x00a1, 0x0048, 0x007f, 0x0075, 0x006e, 0x00d1, 0x00ce, 0x0010,
  485. 0x002d, 0x0015, 0x0027, 0x0045, 0x0040, 0x0072, 0x0063, 0x0057,
  486. 0x009e, 0x008c, 0x00fc, 0x00d4, 0x00c7, 0x0183, 0x016d, 0x001a,
  487. 0x004b, 0x0024, 0x0044, 0x0041, 0x0073, 0x0065, 0x00b3, 0x00a4,
  488. 0x009b, 0x0108, 0x00f6, 0x00e2, 0x018b, 0x017e, 0x016a, 0x0009,
  489. 0x0042, 0x001e, 0x003b, 0x0038, 0x0066, 0x00b9, 0x00ad, 0x0109,
  490. 0x008e, 0x00fd, 0x00e8, 0x0190, 0x0184, 0x017a, 0x01bd, 0x0010,
  491. 0x006f, 0x0036, 0x0034, 0x0064, 0x00b8, 0x00b2, 0x00a0, 0x0085,
  492. 0x0101, 0x00f4, 0x00e4, 0x00d9, 0x0181, 0x016e, 0x02cb, 0x000a,
  493. 0x0062, 0x0030, 0x005b, 0x0058, 0x00a5, 0x009d, 0x0094, 0x0105,
  494. 0x00f8, 0x0197, 0x018d, 0x0174, 0x017c, 0x0379, 0x0374, 0x0008,
  495. 0x0055, 0x0054, 0x0051, 0x009f, 0x009c, 0x008f, 0x0104, 0x00f9,
  496. 0x01ab, 0x0191, 0x0188, 0x017f, 0x02d7, 0x02c9, 0x02c4, 0x0007,
  497. 0x009a, 0x004c, 0x0049, 0x008d, 0x0083, 0x0100, 0x00f5, 0x01aa,
  498. 0x0196, 0x018a, 0x0180, 0x02df, 0x0167, 0x02c6, 0x0160, 0x000b,
  499. 0x008b, 0x0081, 0x0043, 0x007d, 0x00f7, 0x00e9, 0x00e5, 0x00db,
  500. 0x0189, 0x02e7, 0x02e1, 0x02d0, 0x0375, 0x0372, 0x01b7, 0x0004,
  501. 0x00f3, 0x0078, 0x0076, 0x0073, 0x00e3, 0x00df, 0x018c, 0x02ea,
  502. 0x02e6, 0x02e0, 0x02d1, 0x02c8, 0x02c2, 0x00df, 0x01b4, 0x0006,
  503. 0x00ca, 0x00e0, 0x00de, 0x00da, 0x00d8, 0x0185, 0x0182, 0x017d,
  504. 0x016c, 0x0378, 0x01bb, 0x02c3, 0x01b8, 0x01b5, 0x06c0, 0x0004,
  505. 0x02eb, 0x00d3, 0x00d2, 0x00d0, 0x0172, 0x017b, 0x02de, 0x02d3,
  506. 0x02ca, 0x06c7, 0x0373, 0x036d, 0x036c, 0x0d83, 0x0361, 0x0002,
  507. 0x0179, 0x0171, 0x0066, 0x00bb, 0x02d6, 0x02d2, 0x0166, 0x02c7,
  508. 0x02c5, 0x0362, 0x06c6, 0x0367, 0x0d82, 0x0366, 0x01b2, 0x0000,
  509. 0x000c, 0x000a, 0x0007, 0x000b, 0x000a, 0x0011, 0x000b, 0x0009,
  510. 0x000d, 0x000c, 0x000a, 0x0007, 0x0005, 0x0003, 0x0001, 0x0003,
  511. };
  512. static const uint8_t mp3_huffbits_16[256] = {
  513. 1, 4, 6, 8, 9, 9, 10, 10,
  514. 11, 11, 11, 12, 12, 12, 13, 9,
  515. 3, 4, 6, 7, 8, 9, 9, 9,
  516. 10, 10, 10, 11, 12, 11, 12, 8,
  517. 6, 6, 7, 8, 9, 9, 10, 10,
  518. 11, 10, 11, 11, 11, 12, 12, 9,
  519. 8, 7, 8, 9, 9, 10, 10, 10,
  520. 11, 11, 12, 12, 12, 13, 13, 10,
  521. 9, 8, 9, 9, 10, 10, 11, 11,
  522. 11, 12, 12, 12, 13, 13, 13, 9,
  523. 9, 8, 9, 9, 10, 11, 11, 12,
  524. 11, 12, 12, 13, 13, 13, 14, 10,
  525. 10, 9, 9, 10, 11, 11, 11, 11,
  526. 12, 12, 12, 12, 13, 13, 14, 10,
  527. 10, 9, 10, 10, 11, 11, 11, 12,
  528. 12, 13, 13, 13, 13, 15, 15, 10,
  529. 10, 10, 10, 11, 11, 11, 12, 12,
  530. 13, 13, 13, 13, 14, 14, 14, 10,
  531. 11, 10, 10, 11, 11, 12, 12, 13,
  532. 13, 13, 13, 14, 13, 14, 13, 11,
  533. 11, 11, 10, 11, 12, 12, 12, 12,
  534. 13, 14, 14, 14, 15, 15, 14, 10,
  535. 12, 11, 11, 11, 12, 12, 13, 14,
  536. 14, 14, 14, 14, 14, 13, 14, 11,
  537. 12, 12, 12, 12, 12, 13, 13, 13,
  538. 13, 15, 14, 14, 14, 14, 16, 11,
  539. 14, 12, 12, 12, 13, 13, 14, 14,
  540. 14, 16, 15, 15, 15, 17, 15, 11,
  541. 13, 13, 11, 12, 14, 14, 13, 14,
  542. 14, 15, 16, 15, 17, 15, 14, 11,
  543. 9, 8, 8, 9, 9, 10, 10, 10,
  544. 11, 11, 11, 11, 11, 11, 11, 8,
  545. };
  546. static const uint16_t mp3_huffcodes_24[256] = {
  547. 0x000f, 0x000d, 0x002e, 0x0050, 0x0092, 0x0106, 0x00f8, 0x01b2,
  548. 0x01aa, 0x029d, 0x028d, 0x0289, 0x026d, 0x0205, 0x0408, 0x0058,
  549. 0x000e, 0x000c, 0x0015, 0x0026, 0x0047, 0x0082, 0x007a, 0x00d8,
  550. 0x00d1, 0x00c6, 0x0147, 0x0159, 0x013f, 0x0129, 0x0117, 0x002a,
  551. 0x002f, 0x0016, 0x0029, 0x004a, 0x0044, 0x0080, 0x0078, 0x00dd,
  552. 0x00cf, 0x00c2, 0x00b6, 0x0154, 0x013b, 0x0127, 0x021d, 0x0012,
  553. 0x0051, 0x0027, 0x004b, 0x0046, 0x0086, 0x007d, 0x0074, 0x00dc,
  554. 0x00cc, 0x00be, 0x00b2, 0x0145, 0x0137, 0x0125, 0x010f, 0x0010,
  555. 0x0093, 0x0048, 0x0045, 0x0087, 0x007f, 0x0076, 0x0070, 0x00d2,
  556. 0x00c8, 0x00bc, 0x0160, 0x0143, 0x0132, 0x011d, 0x021c, 0x000e,
  557. 0x0107, 0x0042, 0x0081, 0x007e, 0x0077, 0x0072, 0x00d6, 0x00ca,
  558. 0x00c0, 0x00b4, 0x0155, 0x013d, 0x012d, 0x0119, 0x0106, 0x000c,
  559. 0x00f9, 0x007b, 0x0079, 0x0075, 0x0071, 0x00d7, 0x00ce, 0x00c3,
  560. 0x00b9, 0x015b, 0x014a, 0x0134, 0x0123, 0x0110, 0x0208, 0x000a,
  561. 0x01b3, 0x0073, 0x006f, 0x006d, 0x00d3, 0x00cb, 0x00c4, 0x00bb,
  562. 0x0161, 0x014c, 0x0139, 0x012a, 0x011b, 0x0213, 0x017d, 0x0011,
  563. 0x01ab, 0x00d4, 0x00d0, 0x00cd, 0x00c9, 0x00c1, 0x00ba, 0x00b1,
  564. 0x00a9, 0x0140, 0x012f, 0x011e, 0x010c, 0x0202, 0x0179, 0x0010,
  565. 0x014f, 0x00c7, 0x00c5, 0x00bf, 0x00bd, 0x00b5, 0x00ae, 0x014d,
  566. 0x0141, 0x0131, 0x0121, 0x0113, 0x0209, 0x017b, 0x0173, 0x000b,
  567. 0x029c, 0x00b8, 0x00b7, 0x00b3, 0x00af, 0x0158, 0x014b, 0x013a,
  568. 0x0130, 0x0122, 0x0115, 0x0212, 0x017f, 0x0175, 0x016e, 0x000a,
  569. 0x028c, 0x015a, 0x00ab, 0x00a8, 0x00a4, 0x013e, 0x0135, 0x012b,
  570. 0x011f, 0x0114, 0x0107, 0x0201, 0x0177, 0x0170, 0x016a, 0x0006,
  571. 0x0288, 0x0142, 0x013c, 0x0138, 0x0133, 0x012e, 0x0124, 0x011c,
  572. 0x010d, 0x0105, 0x0200, 0x0178, 0x0172, 0x016c, 0x0167, 0x0004,
  573. 0x026c, 0x012c, 0x0128, 0x0126, 0x0120, 0x011a, 0x0111, 0x010a,
  574. 0x0203, 0x017c, 0x0176, 0x0171, 0x016d, 0x0169, 0x0165, 0x0002,
  575. 0x0409, 0x0118, 0x0116, 0x0112, 0x010b, 0x0108, 0x0103, 0x017e,
  576. 0x017a, 0x0174, 0x016f, 0x016b, 0x0168, 0x0166, 0x0164, 0x0000,
  577. 0x002b, 0x0014, 0x0013, 0x0011, 0x000f, 0x000d, 0x000b, 0x0009,
  578. 0x0007, 0x0006, 0x0004, 0x0007, 0x0005, 0x0003, 0x0001, 0x0003,
  579. };
  580. static const uint8_t mp3_huffbits_24[256] = {
  581. 4, 4, 6, 7, 8, 9, 9, 10,
  582. 10, 11, 11, 11, 11, 11, 12, 9,
  583. 4, 4, 5, 6, 7, 8, 8, 9,
  584. 9, 9, 10, 10, 10, 10, 10, 8,
  585. 6, 5, 6, 7, 7, 8, 8, 9,
  586. 9, 9, 9, 10, 10, 10, 11, 7,
  587. 7, 6, 7, 7, 8, 8, 8, 9,
  588. 9, 9, 9, 10, 10, 10, 10, 7,
  589. 8, 7, 7, 8, 8, 8, 8, 9,
  590. 9, 9, 10, 10, 10, 10, 11, 7,
  591. 9, 7, 8, 8, 8, 8, 9, 9,
  592. 9, 9, 10, 10, 10, 10, 10, 7,
  593. 9, 8, 8, 8, 8, 9, 9, 9,
  594. 9, 10, 10, 10, 10, 10, 11, 7,
  595. 10, 8, 8, 8, 9, 9, 9, 9,
  596. 10, 10, 10, 10, 10, 11, 11, 8,
  597. 10, 9, 9, 9, 9, 9, 9, 9,
  598. 9, 10, 10, 10, 10, 11, 11, 8,
  599. 10, 9, 9, 9, 9, 9, 9, 10,
  600. 10, 10, 10, 10, 11, 11, 11, 8,
  601. 11, 9, 9, 9, 9, 10, 10, 10,
  602. 10, 10, 10, 11, 11, 11, 11, 8,
  603. 11, 10, 9, 9, 9, 10, 10, 10,
  604. 10, 10, 10, 11, 11, 11, 11, 8,
  605. 11, 10, 10, 10, 10, 10, 10, 10,
  606. 10, 10, 11, 11, 11, 11, 11, 8,
  607. 11, 10, 10, 10, 10, 10, 10, 10,
  608. 11, 11, 11, 11, 11, 11, 11, 8,
  609. 12, 10, 10, 10, 10, 10, 10, 11,
  610. 11, 11, 11, 11, 11, 11, 11, 8,
  611. 8, 7, 7, 7, 7, 7, 7, 7,
  612. 7, 7, 7, 8, 8, 8, 8, 4,
  613. };
  614. static const huff_table_t mp3_huff_tables[16] = {
  615. { 1, NULL, NULL },
  616. { 2, mp3_huffbits_1, mp3_huffcodes_1 },
  617. { 3, mp3_huffbits_2, mp3_huffcodes_2 },
  618. { 3, mp3_huffbits_3, mp3_huffcodes_3 },
  619. { 4, mp3_huffbits_5, mp3_huffcodes_5 },
  620. { 4, mp3_huffbits_6, mp3_huffcodes_6 },
  621. { 6, mp3_huffbits_7, mp3_huffcodes_7 },
  622. { 6, mp3_huffbits_8, mp3_huffcodes_8 },
  623. { 6, mp3_huffbits_9, mp3_huffcodes_9 },
  624. { 8, mp3_huffbits_10, mp3_huffcodes_10 },
  625. { 8, mp3_huffbits_11, mp3_huffcodes_11 },
  626. { 8, mp3_huffbits_12, mp3_huffcodes_12 },
  627. { 16, mp3_huffbits_13, mp3_huffcodes_13 },
  628. { 16, mp3_huffbits_15, mp3_huffcodes_15 },
  629. { 16, mp3_huffbits_16, mp3_huffcodes_16 },
  630. { 16, mp3_huffbits_24, mp3_huffcodes_24 },
  631. };
  632. static const uint8_t mp3_huff_data[32][2] = {
  633. { 0, 0 },
  634. { 1, 0 },
  635. { 2, 0 },
  636. { 3, 0 },
  637. { 0, 0 },
  638. { 4, 0 },
  639. { 5, 0 },
  640. { 6, 0 },
  641. { 7, 0 },
  642. { 8, 0 },
  643. { 9, 0 },
  644. { 10, 0 },
  645. { 11, 0 },
  646. { 12, 0 },
  647. { 0, 0 },
  648. { 13, 0 },
  649. { 14, 1 },
  650. { 14, 2 },
  651. { 14, 3 },
  652. { 14, 4 },
  653. { 14, 6 },
  654. { 14, 8 },
  655. { 14, 10 },
  656. { 14, 13 },
  657. { 15, 4 },
  658. { 15, 5 },
  659. { 15, 6 },
  660. { 15, 7 },
  661. { 15, 8 },
  662. { 15, 9 },
  663. { 15, 11 },
  664. { 15, 13 },
  665. };
  666. static const uint8_t mp3_quad_codes[2][16] = {
  667. { 1, 5, 4, 5, 6, 5, 4, 4, 7, 3, 6, 0, 7, 2, 3, 1, },
  668. { 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, },
  669. };
  670. static const uint8_t mp3_quad_bits[2][16] = {
  671. { 1, 4, 4, 5, 4, 6, 5, 6, 4, 5, 5, 6, 5, 6, 6, 6, },
  672. { 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, },
  673. };
  674. static const uint8_t band_size_long[9][22] = {
  675. { 4, 4, 4, 4, 4, 4, 6, 6, 8, 8, 10,
  676. 12, 16, 20, 24, 28, 34, 42, 50, 54, 76, 158, }, /* 44100 */
  677. { 4, 4, 4, 4, 4, 4, 6, 6, 6, 8, 10,
  678. 12, 16, 18, 22, 28, 34, 40, 46, 54, 54, 192, }, /* 48000 */
  679. { 4, 4, 4, 4, 4, 4, 6, 6, 8, 10, 12,
  680. 16, 20, 24, 30, 38, 46, 56, 68, 84, 102, 26, }, /* 32000 */
  681. { 6, 6, 6, 6, 6, 6, 8, 10, 12, 14, 16,
  682. 20, 24, 28, 32, 38, 46, 52, 60, 68, 58, 54, }, /* 22050 */
  683. { 6, 6, 6, 6, 6, 6, 8, 10, 12, 14, 16,
  684. 18, 22, 26, 32, 38, 46, 52, 64, 70, 76, 36, }, /* 24000 */
  685. { 6, 6, 6, 6, 6, 6, 8, 10, 12, 14, 16,
  686. 20, 24, 28, 32, 38, 46, 52, 60, 68, 58, 54, }, /* 16000 */
  687. { 6, 6, 6, 6, 6, 6, 8, 10, 12, 14, 16,
  688. 20, 24, 28, 32, 38, 46, 52, 60, 68, 58, 54, }, /* 11025 */
  689. { 6, 6, 6, 6, 6, 6, 8, 10, 12, 14, 16,
  690. 20, 24, 28, 32, 38, 46, 52, 60, 68, 58, 54, }, /* 12000 */
  691. { 12, 12, 12, 12, 12, 12, 16, 20, 24, 28, 32,
  692. 40, 48, 56, 64, 76, 90, 2, 2, 2, 2, 2, }, /* 8000 */
  693. };
  694. static const uint8_t band_size_short[9][13] = {
  695. { 4, 4, 4, 4, 6, 8, 10, 12, 14, 18, 22, 30, 56, }, /* 44100 */
  696. { 4, 4, 4, 4, 6, 6, 10, 12, 14, 16, 20, 26, 66, }, /* 48000 */
  697. { 4, 4, 4, 4, 6, 8, 12, 16, 20, 26, 34, 42, 12, }, /* 32000 */
  698. { 4, 4, 4, 6, 6, 8, 10, 14, 18, 26, 32, 42, 18, }, /* 22050 */
  699. { 4, 4, 4, 6, 8, 10, 12, 14, 18, 24, 32, 44, 12, }, /* 24000 */
  700. { 4, 4, 4, 6, 8, 10, 12, 14, 18, 24, 30, 40, 18, }, /* 16000 */
  701. { 4, 4, 4, 6, 8, 10, 12, 14, 18, 24, 30, 40, 18, }, /* 11025 */
  702. { 4, 4, 4, 6, 8, 10, 12, 14, 18, 24, 30, 40, 18, }, /* 12000 */
  703. { 8, 8, 8, 12, 16, 20, 24, 28, 36, 2, 2, 2, 26, }, /* 8000 */
  704. };
  705. static const uint8_t mp3_pretab[2][22] = {
  706. { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
  707. { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 3, 3, 3, 2, 0 },
  708. };
  709. static const float ci_table[8] = {
  710. -0.6f, -0.535f, -0.33f, -0.185f, -0.095f, -0.041f, -0.0142f, -0.0037f,
  711. };
  712. #define C1 FIXHR(0.98480775301220805936/2)
  713. #define C2 FIXHR(0.93969262078590838405/2)
  714. #define C3 FIXHR(0.86602540378443864676/2)
  715. #define C4 FIXHR(0.76604444311897803520/2)
  716. #define C5 FIXHR(0.64278760968653932632/2)
  717. #define C6 FIXHR(0.5/2)
  718. #define C7 FIXHR(0.34202014332566873304/2)
  719. #define C8 FIXHR(0.17364817766693034885/2)
  720. static const int icos36[9] = {
  721. FIXR(0.50190991877167369479),
  722. FIXR(0.51763809020504152469), //0
  723. FIXR(0.55168895948124587824),
  724. FIXR(0.61038729438072803416),
  725. FIXR(0.70710678118654752439), //1
  726. FIXR(0.87172339781054900991),
  727. FIXR(1.18310079157624925896),
  728. FIXR(1.93185165257813657349), //2
  729. FIXR(5.73685662283492756461),
  730. };
  731. static const int icos36h[9] = {
  732. FIXHR(0.50190991877167369479/2),
  733. FIXHR(0.51763809020504152469/2), //0
  734. FIXHR(0.55168895948124587824/2),
  735. FIXHR(0.61038729438072803416/2),
  736. FIXHR(0.70710678118654752439/2), //1
  737. FIXHR(0.87172339781054900991/2),
  738. FIXHR(1.18310079157624925896/4),
  739. FIXHR(1.93185165257813657349/4), //2
  740. // FIXHR(5.73685662283492756461),
  741. };
  742. ////////////////////////////////////////////////////////////////////////////////
  743. static INLINE int unaligned32_be(const uint8_t *p)
  744. {
  745. return (((p[0]<<8) | p[1])<<16) | (p[2]<<8) | (p[3]);
  746. }
  747. #define MIN_CACHE_BITS 25
  748. #define NEG_SSR32(a,s) ((( int32_t)(a))>>(32-(s)))
  749. #define NEG_USR32(a,s) (((uint32_t)(a))>>(32-(s)))
  750. #define OPEN_READER(name, gb) \
  751. int name##_index= (gb)->index;\
  752. int name##_cache= 0;\
  753. #define CLOSE_READER(name, gb)\
  754. (gb)->index= name##_index;\
  755. #define UPDATE_CACHE(name, gb)\
  756. name##_cache= unaligned32_be(&((gb)->buffer[name##_index>>3])) << (name##_index&0x07); \
  757. #define SKIP_CACHE(name, gb, num)\
  758. name##_cache <<= (num);
  759. #define SKIP_COUNTER(name, gb, num)\
  760. name##_index += (num);\
  761. #define SKIP_BITS(name, gb, num)\
  762. {\
  763. SKIP_CACHE(name, gb, num)\
  764. SKIP_COUNTER(name, gb, num)\
  765. }\
  766. #define LAST_SKIP_BITS(name, gb, num) SKIP_COUNTER(name, gb, num)
  767. #define LAST_SKIP_CACHE(name, gb, num) ;
  768. #define SHOW_UBITS(name, gb, num)\
  769. NEG_USR32(name##_cache, num)
  770. #define SHOW_SBITS(name, gb, num)\
  771. NEG_SSR32(name##_cache, num)
  772. #define GET_CACHE(name, gb)\
  773. ((uint32_t)name##_cache)
  774. static INLINE int get_bits_count(bitstream_t *s){
  775. return s->index;
  776. }
  777. static INLINE void skip_bits_long(bitstream_t *s, int n){
  778. s->index += n;
  779. }
  780. #define skip_bits skip_bits_long
  781. static void init_get_bits(bitstream_t *s, const uint8_t *buffer, int bit_size) {
  782. int buffer_size= (bit_size+7)>>3;
  783. if(buffer_size < 0 || bit_size < 0) {
  784. buffer_size = bit_size = 0;
  785. buffer = NULL;
  786. }
  787. s->buffer= buffer;
  788. s->size_in_bits= bit_size;
  789. s->buffer_end= buffer + buffer_size;
  790. s->index=0;
  791. }
  792. static INLINE unsigned int get_bits(bitstream_t *s, int n){
  793. register int tmp;
  794. OPEN_READER(re, s)
  795. UPDATE_CACHE(re, s)
  796. tmp= SHOW_UBITS(re, s, n);
  797. LAST_SKIP_BITS(re, s, n)
  798. CLOSE_READER(re, s)
  799. return tmp;
  800. }
  801. static INLINE int get_bitsz(bitstream_t *s, int n)
  802. {
  803. if (n == 0)
  804. return 0;
  805. else
  806. return get_bits(s, n);
  807. }
  808. static INLINE unsigned int get_bits1(bitstream_t *s){
  809. int index= s->index;
  810. uint8_t result= s->buffer[ index>>3 ];
  811. result<<= (index&0x07);
  812. result>>= 8 - 1;
  813. index++;
  814. s->index= index;
  815. return result;
  816. }
  817. static INLINE void align_get_bits(bitstream_t *s)
  818. {
  819. int n= (-get_bits_count(s)) & 7;
  820. if(n) skip_bits(s, n);
  821. }
  822. #define GET_DATA(v, table, i, wrap, size) \
  823. {\
  824. const uint8_t *ptr = (const uint8_t *)table + i * wrap;\
  825. switch(size) {\
  826. case 1:\
  827. v = *(const uint8_t *)ptr;\
  828. break;\
  829. case 2:\
  830. v = *(const uint16_t *)ptr;\
  831. break;\
  832. default:\
  833. v = *(const uint32_t *)ptr;\
  834. break;\
  835. }\
  836. }
  837. static INLINE int alloc_table(vlc_t *vlc, int size) {
  838. int index;
  839. index = vlc->table_size;
  840. vlc->table_size += size;
  841. if (vlc->table_size > vlc->table_allocated) {
  842. vlc->table_allocated += (1 << vlc->bits);
  843. vlc->table = libc_realloc(vlc->table, sizeof(VLC_TYPE) * 2 * vlc->table_allocated);
  844. if (!vlc->table)
  845. return -1;
  846. }
  847. return index;
  848. }
  849. static int build_table(
  850. vlc_t *vlc, int table_nb_bits,
  851. int nb_codes,
  852. const void *bits, int bits_wrap, int bits_size,
  853. const void *codes, int codes_wrap, int codes_size,
  854. uint32_t code_prefix, int n_prefix
  855. ) {
  856. int i, j, k, n, table_size, table_index, nb, n1, index, code_prefix2;
  857. uint32_t code;
  858. VLC_TYPE (*table)[2];
  859. table_size = 1 << table_nb_bits;
  860. table_index = alloc_table(vlc, table_size);
  861. if (table_index < 0)
  862. return -1;
  863. table = &vlc->table[table_index];
  864. for(i=0;i<table_size;i++) {
  865. table[i][1] = 0; //bits
  866. table[i][0] = -1; //codes
  867. }
  868. for(i=0;i<nb_codes;i++) {
  869. GET_DATA(n, bits, i, bits_wrap, bits_size);
  870. GET_DATA(code, codes, i, codes_wrap, codes_size);
  871. if (n <= 0)
  872. continue;
  873. n -= n_prefix;
  874. code_prefix2= code >> n;
  875. if (n > 0 && code_prefix2 == code_prefix) {
  876. if (n <= table_nb_bits) {
  877. j = (code << (table_nb_bits - n)) & (table_size - 1);
  878. nb = 1 << (table_nb_bits - n);
  879. for(k=0;k<nb;k++) {
  880. if (table[j][1] /*bits*/ != 0) {
  881. return -1;
  882. }
  883. table[j][1] = n; //bits
  884. table[j][0] = i; //code
  885. j++;
  886. }
  887. } else {
  888. n -= table_nb_bits;
  889. j = (code >> n) & ((1 << table_nb_bits) - 1);
  890. n1 = -table[j][1]; //bits
  891. if (n > n1)
  892. n1 = n;
  893. table[j][1] = -n1; //bits
  894. }
  895. }
  896. }
  897. for(i=0;i<table_size;i++) {
  898. n = table[i][1]; //bits
  899. if (n < 0) {
  900. n = -n;
  901. if (n > table_nb_bits) {
  902. n = table_nb_bits;
  903. table[i][1] = -n; //bits
  904. }
  905. index = build_table(vlc, n, nb_codes,
  906. bits, bits_wrap, bits_size,
  907. codes, codes_wrap, codes_size,
  908. (code_prefix << table_nb_bits) | i,
  909. n_prefix + table_nb_bits);
  910. if (index < 0)
  911. return -1;
  912. table = &vlc->table[table_index];
  913. table[i][0] = index; //code
  914. }
  915. }
  916. return table_index;
  917. }
  918. static INLINE int init_vlc(
  919. vlc_t *vlc, int nb_bits, int nb_codes,
  920. const void *bits, int bits_wrap, int bits_size,
  921. const void *codes, int codes_wrap, int codes_size
  922. ) {
  923. vlc->bits = nb_bits;
  924. if (build_table(vlc, nb_bits, nb_codes,
  925. bits, bits_wrap, bits_size,
  926. codes, codes_wrap, codes_size,
  927. 0, 0) < 0) {
  928. libc_free(vlc->table);
  929. return -1;
  930. }
  931. return 0;
  932. }
  933. #define GET_VLC(code, name, gb, table, bits, max_depth)\
  934. {\
  935. int n, index, nb_bits;\
  936. \
  937. index= SHOW_UBITS(name, gb, bits);\
  938. code = table[index][0];\
  939. n = table[index][1];\
  940. \
  941. if(max_depth > 1 && n < 0){\
  942. LAST_SKIP_BITS(name, gb, bits)\
  943. UPDATE_CACHE(name, gb)\
  944. \
  945. nb_bits = -n;\
  946. \
  947. index= SHOW_UBITS(name, gb, nb_bits) + code;\
  948. code = table[index][0];\
  949. n = table[index][1];\
  950. if(max_depth > 2 && n < 0){\
  951. LAST_SKIP_BITS(name, gb, nb_bits)\
  952. UPDATE_CACHE(name, gb)\
  953. \
  954. nb_bits = -n;\
  955. \
  956. index= SHOW_UBITS(name, gb, nb_bits) + code;\
  957. code = table[index][0];\
  958. n = table[index][1];\
  959. }\
  960. }\
  961. SKIP_BITS(name, gb, n)\
  962. }
  963. static INLINE int get_vlc2(bitstream_t *s, VLC_TYPE (*table)[2], int bits, int max_depth) {
  964. int code;
  965. OPEN_READER(re, s)
  966. UPDATE_CACHE(re, s)
  967. GET_VLC(code, re, s, table, bits, max_depth)
  968. CLOSE_READER(re, s)
  969. return code;
  970. }
  971. static void switch_buffer(mp3_context_t *s, int *pos, int *end_pos, int *end_pos2) {
  972. if(s->in_gb.buffer && *pos >= s->gb.size_in_bits){
  973. s->gb= s->in_gb;
  974. s->in_gb.buffer=NULL;
  975. skip_bits_long(&s->gb, *pos - *end_pos);
  976. *end_pos2=
  977. *end_pos= *end_pos2 + get_bits_count(&s->gb) - *pos;
  978. *pos= get_bits_count(&s->gb);
  979. }
  980. }
  981. ////////////////////////////////////////////////////////////////////////////////
  982. static INLINE int mp3_check_header(uint32_t header){
  983. /* header */
  984. if ((header & 0xffe00000) != 0xffe00000)
  985. return -1;
  986. /* layer check */
  987. if ((header & (3<<17)) != (1 << 17))
  988. return -1;
  989. /* bit rate */
  990. if ((header & (0xf<<12)) == 0xf<<12)
  991. return -1;
  992. /* frequency */
  993. if ((header & (3<<10)) == 3<<10)
  994. return -1;
  995. return 0;
  996. }
  997. static void lsf_sf_expand(
  998. int *slen, int sf, int n1, int n2, int n3
  999. ) {
  1000. if (n3) {
  1001. slen[3] = sf % n3;
  1002. sf /= n3;
  1003. } else {
  1004. slen[3] = 0;
  1005. }
  1006. if (n2) {
  1007. slen[2] = sf % n2;
  1008. sf /= n2;
  1009. } else {
  1010. slen[2] = 0;
  1011. }
  1012. slen[1] = sf % n1;
  1013. sf /= n1;
  1014. slen[0] = sf;
  1015. }
  1016. static INLINE int l3_unscale(int value, int exponent)
  1017. {
  1018. unsigned int m;
  1019. int e;
  1020. e = table_4_3_exp [4*value + (exponent&3)];
  1021. m = table_4_3_value[4*value + (exponent&3)];
  1022. e -= (exponent >> 2);
  1023. if (e > 31)
  1024. return 0;
  1025. m = (m + (1 << (e-1))) >> e;
  1026. return m;
  1027. }
  1028. static INLINE int round_sample(int *sum) {
  1029. int sum1;
  1030. sum1 = (*sum) >> OUT_SHIFT;
  1031. *sum &= (1<<OUT_SHIFT)-1;
  1032. if (sum1 < OUT_MIN)
  1033. sum1 = OUT_MIN;
  1034. else if (sum1 > OUT_MAX)
  1035. sum1 = OUT_MAX;
  1036. return sum1;
  1037. }
  1038. static void exponents_from_scale_factors(
  1039. mp3_context_t *s, granule_t *g, int16_t *exponents
  1040. ) {
  1041. const uint8_t *bstab, *pretab;
  1042. int len, i, j, k, l, v0, shift, gain, gains[3];
  1043. int16_t *exp_ptr;
  1044. exp_ptr = exponents;
  1045. gain = g->global_gain - 210;
  1046. shift = g->scalefac_scale + 1;
  1047. bstab = band_size_long[s->sample_rate_index];
  1048. pretab = mp3_pretab[g->preflag];
  1049. for(i=0;i<g->long_end;i++) {
  1050. v0 = gain - ((g->scale_factors[i] + pretab[i]) << shift) + 400;
  1051. len = bstab[i];
  1052. for(j=len;j>0;j--)
  1053. *exp_ptr++ = v0;
  1054. }
  1055. if (g->short_start < 13) {
  1056. bstab = band_size_short[s->sample_rate_index];
  1057. gains[0] = gain - (g->subblock_gain[0] << 3);
  1058. gains[1] = gain - (g->subblock_gain[1] << 3);
  1059. gains[2] = gain - (g->subblock_gain[2] << 3);
  1060. k = g->long_end;
  1061. for(i=g->short_start;i<13;i++) {
  1062. len = bstab[i];
  1063. for(l=0;l<3;l++) {
  1064. v0 = gains[l] - (g->scale_factors[k++] << shift) + 400;
  1065. for(j=len;j>0;j--)
  1066. *exp_ptr++ = v0;
  1067. }
  1068. }
  1069. }
  1070. }
  1071. static void reorder_block(mp3_context_t *s, granule_t *g)
  1072. {
  1073. int i, j, len;
  1074. int32_t *ptr, *dst, *ptr1;
  1075. int32_t tmp[576];
  1076. if (g->block_type != 2)
  1077. return;
  1078. if (g->switch_point) {
  1079. if (s->sample_rate_index != 8) {
  1080. ptr = g->sb_hybrid + 36;
  1081. } else {
  1082. ptr = g->sb_hybrid + 48;
  1083. }
  1084. } else {
  1085. ptr = g->sb_hybrid;
  1086. }
  1087. for(i=g->short_start;i<13;i++) {
  1088. len = band_size_short[s->sample_rate_index][i];
  1089. ptr1 = ptr;
  1090. dst = tmp;
  1091. for(j=len;j>0;j--) {
  1092. *dst++ = ptr[0*len];
  1093. *dst++ = ptr[1*len];
  1094. *dst++ = ptr[2*len];
  1095. ptr++;
  1096. }
  1097. ptr+=2*len;
  1098. libc_memcpy(ptr1, tmp, len * 3 * sizeof(*ptr1));
  1099. }
  1100. }
  1101. static void compute_antialias(mp3_context_t *s, granule_t *g) {
  1102. int32_t *ptr, *csa;
  1103. int n, i;
  1104. /* we antialias only "long" bands */
  1105. if (g->block_type == 2) {
  1106. if (!g->switch_point)
  1107. return;
  1108. /* XXX: check this for 8000Hz case */
  1109. n = 1;
  1110. } else {
  1111. n = SBLIMIT - 1;
  1112. }
  1113. ptr = g->sb_hybrid + 18;
  1114. for(i = n;i > 0;i--) {
  1115. int tmp0, tmp1, tmp2;
  1116. csa = &csa_table[0][0];
  1117. #define INT_AA(j) \
  1118. tmp0 = ptr[-1-j];\
  1119. tmp1 = ptr[ j];\
  1120. tmp2= MULH(tmp0 + tmp1, csa[0+4*j]);\
  1121. ptr[-1-j] = 4*(tmp2 - MULH(tmp1, csa[2+4*j]));\
  1122. ptr[ j] = 4*(tmp2 + MULH(tmp0, csa[3+4*j]));
  1123. INT_AA(0)
  1124. INT_AA(1)
  1125. INT_AA(2)
  1126. INT_AA(3)
  1127. INT_AA(4)
  1128. INT_AA(5)
  1129. INT_AA(6)
  1130. INT_AA(7)
  1131. ptr += 18;
  1132. }
  1133. }
  1134. static void compute_stereo(
  1135. mp3_context_t *s, granule_t *g0, granule_t *g1
  1136. ) {
  1137. int i, j, k, l;
  1138. int32_t v1, v2;
  1139. int sf_max, tmp0, tmp1, sf, len, non_zero_found;
  1140. int32_t (*is_tab)[16];
  1141. int32_t *tab0, *tab1;
  1142. int non_zero_found_short[3];
  1143. if (s->mode_ext & MODE_EXT_I_STEREO) {
  1144. if (!s->lsf) {
  1145. is_tab = is_table;
  1146. sf_max = 7;
  1147. } else {
  1148. is_tab = is_table_lsf[g1->scalefac_compress & 1];
  1149. sf_max = 16;
  1150. }
  1151. tab0 = g0->sb_hybrid + 576;
  1152. tab1 = g1->sb_hybrid + 576;
  1153. non_zero_found_short[0] = 0;
  1154. non_zero_found_short[1] = 0;
  1155. non_zero_found_short[2] = 0;
  1156. k = (13 - g1->short_start) * 3 + g1->long_end - 3;
  1157. for(i = 12;i >= g1->short_start;i--) {
  1158. /* for last band, use previous scale factor */
  1159. if (i != 11)
  1160. k -= 3;
  1161. len = band_size_short[s->sample_rate_index][i];
  1162. for(l=2;l>=0;l--) {
  1163. tab0 -= len;
  1164. tab1 -= len;
  1165. if (!non_zero_found_short[l]) {
  1166. /* test if non zero band. if so, stop doing i-stereo */
  1167. for(j=0;j<len;j++) {
  1168. if (tab1[j] != 0) {
  1169. non_zero_found_short[l] = 1;
  1170. goto found1;
  1171. }
  1172. }
  1173. sf = g1->scale_factors[k + l];
  1174. if (sf >= sf_max)
  1175. goto found1;
  1176. v1 = is_tab[0][sf];
  1177. v2 = is_tab[1][sf];
  1178. for(j=0;j<len;j++) {
  1179. tmp0 = tab0[j];
  1180. tab0[j] = MULL(tmp0, v1);
  1181. tab1[j] = MULL(tmp0, v2);
  1182. }
  1183. } else {
  1184. found1:
  1185. if (s->mode_ext & MODE_EXT_MS_STEREO) {
  1186. /* lower part of the spectrum : do ms stereo
  1187. if enabled */
  1188. for(j=0;j<len;j++) {
  1189. tmp0 = tab0[j];
  1190. tmp1 = tab1[j];
  1191. tab0[j] = MULL(tmp0 + tmp1, ISQRT2);
  1192. tab1[j] = MULL(tmp0 - tmp1, ISQRT2);
  1193. }
  1194. }
  1195. }
  1196. }
  1197. }
  1198. non_zero_found = non_zero_found_short[0] |
  1199. non_zero_found_short[1] |
  1200. non_zero_found_short[2];
  1201. for(i = g1->long_end - 1;i >= 0;i--) {
  1202. len = band_size_long[s->sample_rate_index][i];
  1203. tab0 -= len;
  1204. tab1 -= len;
  1205. /* test if non zero band. if so, stop doing i-stereo */
  1206. if (!non_zero_found) {
  1207. for(j=0;j<len;j++) {
  1208. if (tab1[j] != 0) {
  1209. non_zero_found = 1;
  1210. goto found2;
  1211. }
  1212. }
  1213. /* for last band, use previous scale factor */
  1214. k = (i == 21) ? 20 : i;
  1215. sf = g1->scale_factors[k];
  1216. if (sf >= sf_max)
  1217. goto found2;
  1218. v1 = is_tab[0][sf];
  1219. v2 = is_tab[1][sf];
  1220. for(j=0;j<len;j++) {
  1221. tmp0 = tab0[j];
  1222. tab0[j] = MULL(tmp0, v1);
  1223. tab1[j] = MULL(tmp0, v2);
  1224. }
  1225. } else {
  1226. found2:
  1227. if (s->mode_ext & MODE_EXT_MS_STEREO) {
  1228. /* lower part of the spectrum : do ms stereo
  1229. if enabled */
  1230. for(j=0;j<len;j++) {
  1231. tmp0 = tab0[j];
  1232. tmp1 = tab1[j];
  1233. tab0[j] = MULL(tmp0 + tmp1, ISQRT2);
  1234. tab1[j] = MULL(tmp0 - tmp1, ISQRT2);
  1235. }
  1236. }
  1237. }
  1238. }
  1239. } else if (s->mode_ext & MODE_EXT_MS_STEREO) {
  1240. /* ms stereo ONLY */
  1241. /* NOTE: the 1/sqrt(2) normalization factor is included in the
  1242. global gain */
  1243. tab0 = g0->sb_hybrid;
  1244. tab1 = g1->sb_hybrid;
  1245. for(i=0;i<576;i++) {
  1246. tmp0 = tab0[i];
  1247. tmp1 = tab1[i];
  1248. tab0[i] = tmp0 + tmp1;
  1249. tab1[i] = tmp0 - tmp1;
  1250. }
  1251. }
  1252. }
  1253. static int huffman_decode(
  1254. mp3_context_t *s, granule_t *g, int16_t *exponents, int end_pos2
  1255. ) {
  1256. int s_index;
  1257. int i;
  1258. int last_pos, bits_left;
  1259. vlc_t *vlc;
  1260. int end_pos= s->gb.size_in_bits;
  1261. if (end_pos2 < end_pos) end_pos = end_pos2;
  1262. /* low frequencies (called big values) */
  1263. s_index = 0;
  1264. for(i=0;i<3;i++) {
  1265. int j, k, l, linbits;
  1266. j = g->region_size[i];
  1267. if (j == 0)
  1268. continue;
  1269. /* select vlc table */
  1270. k = g->table_select[i];
  1271. l = mp3_huff_data[k][0];
  1272. linbits = mp3_huff_data[k][1];
  1273. vlc = &huff_vlc[l];
  1274. if(!l){
  1275. libc_memset(&g->sb_hybrid[s_index], 0, sizeof(*g->sb_hybrid)*2*j);
  1276. s_index += 2*j;
  1277. continue;
  1278. }
  1279. /* read huffcode and compute each couple */
  1280. for(;j>0;j--) {
  1281. int exponent, x, y, v;
  1282. int pos= get_bits_count(&s->gb);
  1283. if (pos >= end_pos){
  1284. switch_buffer(s, &pos, &end_pos, &end_pos2);
  1285. if(pos >= end_pos)
  1286. break;
  1287. }
  1288. y = get_vlc2(&s->gb, vlc->table, 7, 3);
  1289. if(!y){
  1290. g->sb_hybrid[s_index ] =
  1291. g->sb_hybrid[s_index+1] = 0;
  1292. s_index += 2;
  1293. continue;
  1294. }
  1295. exponent= exponents[s_index];
  1296. if(y&16){
  1297. x = y >> 5;
  1298. y = y & 0x0f;
  1299. if (x < 15){
  1300. v = expval_table[ exponent ][ x ];
  1301. }else{
  1302. x += get_bitsz(&s->gb, linbits);
  1303. v = l3_unscale(x, exponent);
  1304. }
  1305. if (get_bits1(&s->gb))
  1306. v = -v;
  1307. g->sb_hybrid[s_index] = v;
  1308. if (y < 15){
  1309. v = expval_table[ exponent ][ y ];
  1310. }else{
  1311. y += get_bitsz(&s->gb, linbits);
  1312. v = l3_unscale(y, exponent);
  1313. }
  1314. if (get_bits1(&s->gb))
  1315. v = -v;
  1316. g->sb_hybrid[s_index+1] = v;
  1317. }else{
  1318. x = y >> 5;
  1319. y = y & 0x0f;
  1320. x += y;
  1321. if (x < 15){
  1322. v = expval_table[ exponent ][ x ];
  1323. }else{
  1324. x += get_bitsz(&s->gb, linbits);
  1325. v = l3_unscale(x, exponent);
  1326. }
  1327. if (get_bits1(&s->gb))
  1328. v = -v;
  1329. g->sb_hybrid[s_index+!!y] = v;
  1330. g->sb_hybrid[s_index+ !y] = 0;
  1331. }
  1332. s_index+=2;
  1333. }
  1334. }
  1335. /* high frequencies */
  1336. vlc = &huff_quad_vlc[g->count1table_select];
  1337. last_pos=0;
  1338. while (s_index <= 572) {
  1339. int pos, code;
  1340. pos = get_bits_count(&s->gb);
  1341. if (pos >= end_pos) {
  1342. if (pos > end_pos2 && last_pos){
  1343. /* some encoders generate an incorrect size for this
  1344. part. We must go back into the data */
  1345. s_index -= 4;
  1346. skip_bits_long(&s->gb, last_pos - pos);
  1347. break;
  1348. }
  1349. switch_buffer(s, &pos, &end_pos, &end_pos2);
  1350. if(pos >= end_pos)
  1351. break;
  1352. }
  1353. last_pos= pos;
  1354. code = get_vlc2(&s->gb, vlc->table, vlc->bits, 1);
  1355. g->sb_hybrid[s_index+0]=
  1356. g->sb_hybrid[s_index+1]=
  1357. g->sb_hybrid[s_index+2]=
  1358. g->sb_hybrid[s_index+3]= 0;
  1359. while(code){
  1360. const static int idxtab[16]={3,3,2,2,1,1,1,1,0,0,0,0,0,0,0,0};
  1361. int v;
  1362. int pos= s_index+idxtab[code];
  1363. code ^= 8>>idxtab[code];
  1364. v = exp_table[ exponents[pos] ];
  1365. if(get_bits1(&s->gb))
  1366. v = -v;
  1367. g->sb_hybrid[pos] = v;
  1368. }
  1369. s_index+=4;
  1370. }
  1371. libc_memset(&g->sb_hybrid[s_index], 0, sizeof(*g->sb_hybrid)*(576 - s_index));
  1372. /* skip extension bits */
  1373. bits_left = end_pos2 - get_bits_count(&s->gb);
  1374. if (bits_left < 0) {
  1375. return -1;
  1376. }
  1377. skip_bits_long(&s->gb, bits_left);
  1378. i= get_bits_count(&s->gb);
  1379. switch_buffer(s, &i, &end_pos, &end_pos2);
  1380. return 0;
  1381. }
  1382. ////////////////////////////////////////////////////////////////////////////////
  1383. static void imdct12(int *out, int *in)
  1384. {
  1385. int in0, in1, in2, in3, in4, in5, t1, t2;
  1386. in0= in[0*3];
  1387. in1= in[1*3] + in[0*3];
  1388. in2= in[2*3] + in[1*3];
  1389. in3= in[3*3] + in[2*3];
  1390. in4= in[4*3] + in[3*3];
  1391. in5= in[5*3] + in[4*3];
  1392. in5 += in3;
  1393. in3 += in1;
  1394. in2= MULH(2*in2, C3);
  1395. in3= MULH(4*in3, C3);
  1396. t1 = in0 - in4;
  1397. t2 = MULH(2*(in1 - in5), icos36h[4]);
  1398. out[ 7]=
  1399. out[10]= t1 + t2;
  1400. out[ 1]=
  1401. out[ 4]= t1 - t2;
  1402. in0 += in4>>1;
  1403. in4 = in0 + in2;
  1404. in5 += 2*in1;
  1405. in1 = MULH(in5 + in3, icos36h[1]);
  1406. out[ 8]=
  1407. out[ 9]= in4 + in1;
  1408. out[ 2]=
  1409. out[ 3]= in4 - in1;
  1410. in0 -= in2;
  1411. in5 = MULH(2*(in5 - in3), icos36h[7]);
  1412. out[ 0]=
  1413. out[ 5]= in0 - in5;
  1414. out[ 6]=
  1415. out[11]= in0 + in5;
  1416. }
  1417. static void imdct36(int *out, int *buf, int *in, int *win)
  1418. {
  1419. int i, j, t0, t1, t2, t3, s0, s1, s2, s3;
  1420. int tmp[18], *tmp1, *in1;
  1421. for(i=17;i>=1;i--)
  1422. in[i] += in[i-1];
  1423. for(i=17;i>=3;i-=2)
  1424. in[i] += in[i-2];
  1425. for(j=0;j<2;j++) {
  1426. tmp1 = tmp + j;
  1427. in1 = in + j;
  1428. t2 = in1[2*4] + in1[2*8] - in1[2*2];
  1429. t3 = in1[2*0] + (in1[2*6]>>1);
  1430. t1 = in1[2*0] - in1[2*6];
  1431. tmp1[ 6] = t1 - (t2>>1);
  1432. tmp1[16] = t1 + t2;
  1433. t0 = MULH(2*(in1[2*2] + in1[2*4]), C2);
  1434. t1 = MULH( in1[2*4] - in1[2*8] , -2*C8);
  1435. t2 = MULH(2*(in1[2*2] + in1[2*8]), -C4);
  1436. tmp1[10] = t3 - t0 - t2;
  1437. tmp1[ 2] = t3 + t0 + t1;
  1438. tmp1[14] = t3 + t2 - t1;
  1439. tmp1[ 4] = MULH(2*(in1[2*5] + in1[2*7] - in1[2*1]), -C3);
  1440. t2 = MULH(2*(in1[2*1] + in1[2*5]), C1);
  1441. t3 = MULH( in1[2*5] - in1[2*7] , -2*C7);
  1442. t0 = MULH(2*in1[2*3], C3);
  1443. t1 = MULH(2*(in1[2*1] + in1[2*7]), -C5);
  1444. tmp1[ 0] = t2 + t3 + t0;
  1445. tmp1[12] = t2 + t1 - t0;
  1446. tmp1[ 8] = t3 - t1 - t0;
  1447. }
  1448. i = 0;
  1449. for(j=0;j<4;j++) {
  1450. t0 = tmp[i];
  1451. t1 = tmp[i + 2];
  1452. s0 = t1 + t0;
  1453. s2 = t1 - t0;
  1454. t2 = tmp[i + 1];
  1455. t3 = tmp[i + 3];
  1456. s1 = MULH(2*(t3 + t2), icos36h[j]);
  1457. s3 = MULL(t3 - t2, icos36[8 - j]);
  1458. t0 = s0 + s1;
  1459. t1 = s0 - s1;
  1460. out[(9 + j)*SBLIMIT] = MULH(t1, win[9 + j]) + buf[9 + j];
  1461. out[(8 - j)*SBLIMIT] = MULH(t1, win[8 - j]) + buf[8 - j];
  1462. buf[9 + j] = MULH(t0, win[18 + 9 + j]);
  1463. buf[8 - j] = MULH(t0, win[18 + 8 - j]);
  1464. t0 = s2 + s3;
  1465. t1 = s2 - s3;
  1466. out[(9 + 8 - j)*SBLIMIT] = MULH(t1, win[9 + 8 - j]) + buf[9 + 8 - j];
  1467. out[( j)*SBLIMIT] = MULH(t1, win[ j]) + buf[ j];
  1468. buf[9 + 8 - j] = MULH(t0, win[18 + 9 + 8 - j]);
  1469. buf[ + j] = MULH(t0, win[18 + j]);
  1470. i += 4;
  1471. }
  1472. s0 = tmp[16];
  1473. s1 = MULH(2*tmp[17], icos36h[4]);
  1474. t0 = s0 + s1;
  1475. t1 = s0 - s1;
  1476. out[(9 + 4)*SBLIMIT] = MULH(t1, win[9 + 4]) + buf[9 + 4];
  1477. out[(8 - 4)*SBLIMIT] = MULH(t1, win[8 - 4]) + buf[8 - 4];
  1478. buf[9 + 4] = MULH(t0, win[18 + 9 + 4]);
  1479. buf[8 - 4] = MULH(t0, win[18 + 8 - 4]);
  1480. }
  1481. static void compute_imdct(
  1482. mp3_context_t *s, granule_t *g, int32_t *sb_samples, int32_t *mdct_buf
  1483. ) {
  1484. int32_t *ptr, *win, *win1, *buf, *out_ptr, *ptr1;
  1485. int32_t out2[12];
  1486. int i, j, mdct_long_end, v, sblimit;
  1487. /* find last non zero block */
  1488. ptr = g->sb_hybrid + 576;
  1489. ptr1 = g->sb_hybrid + 2 * 18;
  1490. while (ptr >= ptr1) {
  1491. ptr -= 6;
  1492. v = ptr[0] | ptr[1] | ptr[2] | ptr[3] | ptr[4] | ptr[5];
  1493. if (v != 0)
  1494. break;
  1495. }
  1496. sblimit = ((ptr - g->sb_hybrid) / 18) + 1;
  1497. if (g->block_type == 2) {
  1498. /* XXX: check for 8000 Hz */
  1499. if (g->switch_point)
  1500. mdct_long_end = 2;
  1501. else
  1502. mdct_long_end = 0;
  1503. } else {
  1504. mdct_long_end = sblimit;
  1505. }
  1506. buf = mdct_buf;
  1507. ptr = g->sb_hybrid;
  1508. for(j=0;j<mdct_long_end;j++) {
  1509. /* apply window & overlap with previous buffer */
  1510. out_ptr = sb_samples + j;
  1511. /* select window */
  1512. if (g->switch_point && j < 2)
  1513. win1 = mdct_win[0];
  1514. else
  1515. win1 = mdct_win[g->block_type];
  1516. /* select frequency inversion */
  1517. win = win1 + ((4 * 36) & -(j & 1));
  1518. imdct36(out_ptr, buf, ptr, win);
  1519. out_ptr += 18*SBLIMIT;
  1520. ptr += 18;
  1521. buf += 18;
  1522. }
  1523. for(j=mdct_long_end;j<sblimit;j++) {
  1524. /* select frequency inversion */
  1525. win = mdct_win[2] + ((4 * 36) & -(j & 1));
  1526. out_ptr = sb_samples + j;
  1527. for(i=0; i<6; i++){
  1528. *out_ptr = buf[i];
  1529. out_ptr += SBLIMIT;
  1530. }
  1531. imdct12(out2, ptr + 0);
  1532. for(i=0;i<6;i++) {
  1533. *out_ptr = MULH(out2[i], win[i]) + buf[i + 6*1];
  1534. buf[i + 6*2] = MULH(out2[i + 6], win[i + 6]);
  1535. out_ptr += SBLIMIT;
  1536. }
  1537. imdct12(out2, ptr + 1);
  1538. for(i=0;i<6;i++) {
  1539. *out_ptr = MULH(out2[i], win[i]) + buf[i + 6*2];
  1540. buf[i + 6*0] = MULH(out2[i + 6], win[i + 6]);
  1541. out_ptr += SBLIMIT;
  1542. }
  1543. imdct12(out2, ptr + 2);
  1544. for(i=0;i<6;i++) {
  1545. buf[i + 6*0] = MULH(out2[i], win[i]) + buf[i + 6*0];
  1546. buf[i + 6*1] = MULH(out2[i + 6], win[i + 6]);
  1547. buf[i + 6*2] = 0;
  1548. }
  1549. ptr += 18;
  1550. buf += 18;
  1551. }
  1552. /* zero bands */
  1553. for(j=sblimit;j<SBLIMIT;j++) {
  1554. /* overlap */
  1555. out_ptr = sb_samples + j;
  1556. for(i=0;i<18;i++) {
  1557. *out_ptr = buf[i];
  1558. buf[i] = 0;
  1559. out_ptr += SBLIMIT;
  1560. }
  1561. buf += 18;
  1562. }
  1563. }
  1564. #define SUM8(sum, op, w, p) \
  1565. { \
  1566. sum op MULS((w)[0 * 64], p[0 * 64]);\
  1567. sum op MULS((w)[1 * 64], p[1 * 64]);\
  1568. sum op MULS((w)[2 * 64], p[2 * 64]);\
  1569. sum op MULS((w)[3 * 64], p[3 * 64]);\
  1570. sum op MULS((w)[4 * 64], p[4 * 64]);\
  1571. sum op MULS((w)[5 * 64], p[5 * 64]);\
  1572. sum op MULS((w)[6 * 64], p[6 * 64]);\
  1573. sum op MULS((w)[7 * 64], p[7 * 64]);\
  1574. }
  1575. #define SUM8P2(sum1, op1, sum2, op2, w1, w2, p) \
  1576. { \
  1577. int tmp;\
  1578. tmp = p[0 * 64];\
  1579. sum1 op1 MULS((w1)[0 * 64], tmp);\
  1580. sum2 op2 MULS((w2)[0 * 64], tmp);\
  1581. tmp = p[1 * 64];\
  1582. sum1 op1 MULS((w1)[1 * 64], tmp);\
  1583. sum2 op2 MULS((w2)[1 * 64], tmp);\
  1584. tmp = p[2 * 64];\
  1585. sum1 op1 MULS((w1)[2 * 64], tmp);\
  1586. sum2 op2 MULS((w2)[2 * 64], tmp);\
  1587. tmp = p[3 * 64];\
  1588. sum1 op1 MULS((w1)[3 * 64], tmp);\
  1589. sum2 op2 MULS((w2)[3 * 64], tmp);\
  1590. tmp = p[4 * 64];\
  1591. sum1 op1 MULS((w1)[4 * 64], tmp);\
  1592. sum2 op2 MULS((w2)[4 * 64], tmp);\
  1593. tmp = p[5 * 64];\
  1594. sum1 op1 MULS((w1)[5 * 64], tmp);\
  1595. sum2 op2 MULS((w2)[5 * 64], tmp);\
  1596. tmp = p[6 * 64];\
  1597. sum1 op1 MULS((w1)[6 * 64], tmp);\
  1598. sum2 op2 MULS((w2)[6 * 64], tmp);\
  1599. tmp = p[7 * 64];\
  1600. sum1 op1 MULS((w1)[7 * 64], tmp);\
  1601. sum2 op2 MULS((w2)[7 * 64], tmp);\
  1602. }
  1603. #define COS0_0 FIXHR(0.50060299823519630134/2)
  1604. #define COS0_1 FIXHR(0.50547095989754365998/2)
  1605. #define COS0_2 FIXHR(0.51544730992262454697/2)
  1606. #define COS0_3 FIXHR(0.53104259108978417447/2)
  1607. #define COS0_4 FIXHR(0.55310389603444452782/2)
  1608. #define COS0_5 FIXHR(0.58293496820613387367/2)
  1609. #define COS0_6 FIXHR(0.62250412303566481615/2)
  1610. #define COS0_7 FIXHR(0.67480834145500574602/2)
  1611. #define COS0_8 FIXHR(0.74453627100229844977/2)
  1612. #define COS0_9 FIXHR(0.83934964541552703873/2)
  1613. #define COS0_10 FIXHR(0.97256823786196069369/2)
  1614. #define COS0_11 FIXHR(1.16943993343288495515/4)
  1615. #define COS0_12 FIXHR(1.48416461631416627724/4)
  1616. #define COS0_13 FIXHR(2.05778100995341155085/8)
  1617. #define COS0_14 FIXHR(3.40760841846871878570/8)
  1618. #define COS0_15 FIXHR(10.19000812354805681150/32)
  1619. #define COS1_0 FIXHR(0.50241928618815570551/2)
  1620. #define COS1_1 FIXHR(0.52249861493968888062/2)
  1621. #define COS1_2 FIXHR(0.56694403481635770368/2)
  1622. #define COS1_3 FIXHR(0.64682178335999012954/2)
  1623. #define COS1_4 FIXHR(0.78815462345125022473/2)
  1624. #define COS1_5 FIXHR(1.06067768599034747134/4)
  1625. #define COS1_6 FIXHR(1.72244709823833392782/4)
  1626. #define COS1_7 FIXHR(5.10114861868916385802/16)
  1627. #define COS2_0 FIXHR(0.50979557910415916894/2)
  1628. #define COS2_1 FIXHR(0.60134488693504528054/2)
  1629. #define COS2_2 FIXHR(0.89997622313641570463/2)
  1630. #define COS2_3 FIXHR(2.56291544774150617881/8)
  1631. #define COS3_0 FIXHR(0.54119610014619698439/2)
  1632. #define COS3_1 FIXHR(1.30656296487637652785/4)
  1633. #define COS4_0 FIXHR(0.70710678118654752439/2)
  1634. #define BF(a, b, c, s)\
  1635. {\
  1636. tmp0 = tab[a] + tab[b];\
  1637. tmp1 = tab[a] - tab[b];\
  1638. tab[a] = tmp0;\
  1639. tab[b] = MULH(tmp1<<(s), c);\
  1640. }
  1641. #define BF1(a, b, c, d)\
  1642. {\
  1643. BF(a, b, COS4_0, 1);\
  1644. BF(c, d,-COS4_0, 1);\
  1645. tab[c] += tab[d];\
  1646. }
  1647. #define BF2(a, b, c, d)\
  1648. {\
  1649. BF(a, b, COS4_0, 1);\
  1650. BF(c, d,-COS4_0, 1);\
  1651. tab[c] += tab[d];\
  1652. tab[a] += tab[c];\
  1653. tab[c] += tab[b];\
  1654. tab[b] += tab[d];\
  1655. }
  1656. #define ADD(a, b) tab[a] += tab[b]
  1657. static void dct32(int32_t *out, int32_t *tab)
  1658. {
  1659. int tmp0, tmp1;
  1660. /* pass 1 */
  1661. BF( 0, 31, COS0_0 , 1);
  1662. BF(15, 16, COS0_15, 5);
  1663. /* pass 2 */
  1664. BF( 0, 15, COS1_0 , 1);
  1665. BF(16, 31,-COS1_0 , 1);
  1666. /* pass 1 */
  1667. BF( 7, 24, COS0_7 , 1);
  1668. BF( 8, 23, COS0_8 , 1);
  1669. /* pass 2 */
  1670. BF( 7, 8, COS1_7 , 4);
  1671. BF(23, 24,-COS1_7 , 4);
  1672. /* pass 3 */
  1673. BF( 0, 7, COS2_0 , 1);
  1674. BF( 8, 15,-COS2_0 , 1);
  1675. BF(16, 23, COS2_0 , 1);
  1676. BF(24, 31,-COS2_0 , 1);
  1677. /* pass 1 */
  1678. BF( 3, 28, COS0_3 , 1);
  1679. BF(12, 19, COS0_12, 2);
  1680. /* pass 2 */
  1681. BF( 3, 12, COS1_3 , 1);
  1682. BF(19, 28,-COS1_3 , 1);
  1683. /* pass 1 */
  1684. BF( 4, 27, COS0_4 , 1);
  1685. BF(11, 20, COS0_11, 2);
  1686. /* pass 2 */
  1687. BF( 4, 11, COS1_4 , 1);
  1688. BF(20, 27,-COS1_4 , 1);
  1689. /* pass 3 */
  1690. BF( 3, 4, COS2_3 , 3);
  1691. BF(11, 12,-COS2_3 , 3);
  1692. BF(19, 20, COS2_3 , 3);
  1693. BF(27, 28,-COS2_3 , 3);
  1694. /* pass 4 */
  1695. BF( 0, 3, COS3_0 , 1);
  1696. BF( 4, 7,-COS3_0 , 1);
  1697. BF( 8, 11, COS3_0 , 1);
  1698. BF(12, 15,-COS3_0 , 1);
  1699. BF(16, 19, COS3_0 , 1);
  1700. BF(20, 23,-COS3_0 , 1);
  1701. BF(24, 27, COS3_0 , 1);
  1702. BF(28, 31,-COS3_0 , 1);
  1703. /* pass 1 */
  1704. BF( 1, 30, COS0_1 , 1);
  1705. BF(14, 17, COS0_14, 3);
  1706. /* pass 2 */
  1707. BF( 1, 14, COS1_1 , 1);
  1708. BF(17, 30,-COS1_1 , 1);
  1709. /* pass 1 */
  1710. BF( 6, 25, COS0_6 , 1);
  1711. BF( 9, 22, COS0_9 , 1);
  1712. /* pass 2 */
  1713. BF( 6, 9, COS1_6 , 2);
  1714. BF(22, 25,-COS1_6 , 2);
  1715. /* pass 3 */
  1716. BF( 1, 6, COS2_1 , 1);
  1717. BF( 9, 14,-COS2_1 , 1);
  1718. BF(17, 22, COS2_1 , 1);
  1719. BF(25, 30,-COS2_1 , 1);
  1720. /* pass 1 */
  1721. BF( 2, 29, COS0_2 , 1);
  1722. BF(13, 18, COS0_13, 3);
  1723. /* pass 2 */
  1724. BF( 2, 13, COS1_2 , 1);
  1725. BF(18, 29,-COS1_2 , 1);
  1726. /* pass 1 */
  1727. BF( 5, 26, COS0_5 , 1);
  1728. BF(10, 21, COS0_10, 1);
  1729. /* pass 2 */
  1730. BF( 5, 10, COS1_5 , 2);
  1731. BF(21, 26,-COS1_5 , 2);
  1732. /* pass 3 */
  1733. BF( 2, 5, COS2_2 , 1);
  1734. BF(10, 13,-COS2_2 , 1);
  1735. BF(18, 21, COS2_2 , 1);
  1736. BF(26, 29,-COS2_2 , 1);
  1737. /* pass 4 */
  1738. BF( 1, 2, COS3_1 , 2);
  1739. BF( 5, 6,-COS3_1 , 2);
  1740. BF( 9, 10, COS3_1 , 2);
  1741. BF(13, 14,-COS3_1 , 2);
  1742. BF(17, 18, COS3_1 , 2);
  1743. BF(21, 22,-COS3_1 , 2);
  1744. BF(25, 26, COS3_1 , 2);
  1745. BF(29, 30,-COS3_1 , 2);
  1746. /* pass 5 */
  1747. BF1( 0, 1, 2, 3);
  1748. BF2( 4, 5, 6, 7);
  1749. BF1( 8, 9, 10, 11);
  1750. BF2(12, 13, 14, 15);
  1751. BF1(16, 17, 18, 19);
  1752. BF2(20, 21, 22, 23);
  1753. BF1(24, 25, 26, 27);
  1754. BF2(28, 29, 30, 31);
  1755. /* pass 6 */
  1756. ADD( 8, 12);
  1757. ADD(12, 10);
  1758. ADD(10, 14);
  1759. ADD(14, 9);
  1760. ADD( 9, 13);
  1761. ADD(13, 11);
  1762. ADD(11, 15);
  1763. out[ 0] = tab[0];
  1764. out[16] = tab[1];
  1765. out[ 8] = tab[2];
  1766. out[24] = tab[3];
  1767. out[ 4] = tab[4];
  1768. out[20] = tab[5];
  1769. out[12] = tab[6];
  1770. out[28] = tab[7];
  1771. out[ 2] = tab[8];
  1772. out[18] = tab[9];
  1773. out[10] = tab[10];
  1774. out[26] = tab[11];
  1775. out[ 6] = tab[12];
  1776. out[22] = tab[13];
  1777. out[14] = tab[14];
  1778. out[30] = tab[15];
  1779. ADD(24, 28);
  1780. ADD(28, 26);
  1781. ADD(26, 30);
  1782. ADD(30, 25);
  1783. ADD(25, 29);
  1784. ADD(29, 27);
  1785. ADD(27, 31);
  1786. out[ 1] = tab[16] + tab[24];
  1787. out[17] = tab[17] + tab[25];
  1788. out[ 9] = tab[18] + tab[26];
  1789. out[25] = tab[19] + tab[27];
  1790. out[ 5] = tab[20] + tab[28];
  1791. out[21] = tab[21] + tab[29];
  1792. out[13] = tab[22] + tab[30];
  1793. out[29] = tab[23] + tab[31];
  1794. out[ 3] = tab[24] + tab[20];
  1795. out[19] = tab[25] + tab[21];
  1796. out[11] = tab[26] + tab[22];
  1797. out[27] = tab[27] + tab[23];
  1798. out[ 7] = tab[28] + tab[18];
  1799. out[23] = tab[29] + tab[19];
  1800. out[15] = tab[30] + tab[17];
  1801. out[31] = tab[31];
  1802. }
  1803. static void mp3_synth_filter(
  1804. int16_t *synth_buf_ptr, int *synth_buf_offset,
  1805. int16_t *window, int *dither_state,
  1806. int16_t *samples, int incr,
  1807. int32_t sb_samples[SBLIMIT]
  1808. ) {
  1809. int32_t tmp[32];
  1810. register int16_t *synth_buf;
  1811. register const int16_t *w, *w2, *p;
  1812. int j, offset, v;
  1813. int16_t *samples2;
  1814. int sum, sum2;
  1815. dct32(tmp, sb_samples);
  1816. offset = *synth_buf_offset;
  1817. synth_buf = synth_buf_ptr + offset;
  1818. for(j=0;j<32;j++) {
  1819. v = tmp[j];
  1820. /* NOTE: can cause a loss in precision if very high amplitude
  1821. sound */
  1822. if (v > 32767)
  1823. v = 32767;
  1824. else if (v < -32768)
  1825. v = -32768;
  1826. synth_buf[j] = v;
  1827. }
  1828. /* copy to avoid wrap */
  1829. libc_memcpy(synth_buf + 512, synth_buf, 32 * sizeof(int16_t));
  1830. samples2 = samples + 31 * incr;
  1831. w = window;
  1832. w2 = window + 31;
  1833. sum = *dither_state;
  1834. p = synth_buf + 16;
  1835. SUM8(sum, +=, w, p);
  1836. p = synth_buf + 48;
  1837. SUM8(sum, -=, w + 32, p);
  1838. *samples = round_sample(&sum);
  1839. samples += incr;
  1840. w++;
  1841. /* we calculate two samples at the same time to avoid one memory
  1842. access per two sample */
  1843. for(j=1;j<16;j++) {
  1844. sum2 = 0;
  1845. p = synth_buf + 16 + j;
  1846. SUM8P2(sum, +=, sum2, -=, w, w2, p);
  1847. p = synth_buf + 48 - j;
  1848. SUM8P2(sum, -=, sum2, -=, w + 32, w2 + 32, p);
  1849. *samples = round_sample(&sum);
  1850. samples += incr;
  1851. sum += sum2;
  1852. *samples2 = round_sample(&sum);
  1853. samples2 -= incr;
  1854. w++;
  1855. w2--;
  1856. }
  1857. p = synth_buf + 32;
  1858. SUM8(sum, -=, w + 32, p);
  1859. *samples = round_sample(&sum);
  1860. *dither_state= sum;
  1861. offset = (offset - 32) & 511;
  1862. *synth_buf_offset = offset;
  1863. }
  1864. ////////////////////////////////////////////////////////////////////////////////
  1865. static int decode_header(mp3_context_t *s, uint32_t header) {
  1866. int sample_rate, frame_size, mpeg25, padding;
  1867. int sample_rate_index, bitrate_index;
  1868. if (header & (1<<20)) {
  1869. s->lsf = (header & (1<<19)) ? 0 : 1;
  1870. mpeg25 = 0;
  1871. } else {
  1872. s->lsf = 1;
  1873. mpeg25 = 1;
  1874. }
  1875. sample_rate_index = (header >> 10) & 3;
  1876. sample_rate = mp3_freq_tab[sample_rate_index] >> (s->lsf + mpeg25);
  1877. sample_rate_index += 3 * (s->lsf + mpeg25);
  1878. s->sample_rate_index = sample_rate_index;
  1879. s->error_protection = ((header >> 16) & 1) ^ 1;
  1880. s->sample_rate = sample_rate;
  1881. bitrate_index = (header >> 12) & 0xf;
  1882. padding = (header >> 9) & 1;
  1883. s->mode = (header >> 6) & 3;
  1884. s->mode_ext = (header >> 4) & 3;
  1885. s->nb_channels = (s->mode == MP3_MONO) ? 1 : 2;
  1886. if (bitrate_index != 0) {
  1887. frame_size = mp3_bitrate_tab[s->lsf][bitrate_index];
  1888. s->bit_rate = frame_size * 1000;
  1889. s->frame_size = (frame_size * 144000) / (sample_rate << s->lsf) + padding;
  1890. } else {
  1891. /* if no frame size computed, signal it */
  1892. return 1;
  1893. }
  1894. return 0;
  1895. }
  1896. static int mp_decode_layer3(mp3_context_t *s) {
  1897. int nb_granules, main_data_begin, private_bits;
  1898. int gr, ch, blocksplit_flag, i, j, k, n, bits_pos;
  1899. granule_t *g;
  1900. static granule_t granules[2][2];
  1901. static int16_t exponents[576];
  1902. const uint8_t *ptr;
  1903. if (s->lsf) {
  1904. main_data_begin = get_bits(&s->gb, 8);
  1905. private_bits = get_bits(&s->gb, s->nb_channels);
  1906. nb_granules = 1;
  1907. } else {
  1908. main_data_begin = get_bits(&s->gb, 9);
  1909. if (s->nb_channels == 2)
  1910. private_bits = get_bits(&s->gb, 3);
  1911. else
  1912. private_bits = get_bits(&s->gb, 5);
  1913. nb_granules = 2;
  1914. for(ch=0;ch<s->nb_channels;ch++) {
  1915. granules[ch][0].scfsi = 0; /* all scale factors are transmitted */
  1916. granules[ch][1].scfsi = get_bits(&s->gb, 4);
  1917. }
  1918. }
  1919. for(gr=0;gr<nb_granules;gr++) {
  1920. for(ch=0;ch<s->nb_channels;ch++) {
  1921. g = &granules[ch][gr];
  1922. g->part2_3_length = get_bits(&s->gb, 12);
  1923. g->big_values = get_bits(&s->gb, 9);
  1924. g->global_gain = get_bits(&s->gb, 8);
  1925. /* if MS stereo only is selected, we precompute the
  1926. 1/sqrt(2) renormalization factor */
  1927. if ((s->mode_ext & (MODE_EXT_MS_STEREO | MODE_EXT_I_STEREO)) ==
  1928. MODE_EXT_MS_STEREO)
  1929. g->global_gain -= 2;
  1930. if (s->lsf)
  1931. g->scalefac_compress = get_bits(&s->gb, 9);
  1932. else
  1933. g->scalefac_compress = get_bits(&s->gb, 4);
  1934. blocksplit_flag = get_bits(&s->gb, 1);
  1935. if (blocksplit_flag) {
  1936. g->block_type = get_bits(&s->gb, 2);
  1937. if (g->block_type == 0)
  1938. return -1;
  1939. g->switch_point = get_bits(&s->gb, 1);
  1940. for(i=0;i<2;i++)
  1941. g->table_select[i] = get_bits(&s->gb, 5);
  1942. for(i=0;i<3;i++)
  1943. g->subblock_gain[i] = get_bits(&s->gb, 3);
  1944. /* compute huffman coded region sizes */
  1945. if (g->block_type == 2)
  1946. g->region_size[0] = (36 / 2);
  1947. else {
  1948. if (s->sample_rate_index <= 2)
  1949. g->region_size[0] = (36 / 2);
  1950. else if (s->sample_rate_index != 8)
  1951. g->region_size[0] = (54 / 2);
  1952. else
  1953. g->region_size[0] = (108 / 2);
  1954. }
  1955. g->region_size[1] = (576 / 2);
  1956. } else {
  1957. int region_address1, region_address2, l;
  1958. g->block_type = 0;
  1959. g->switch_point = 0;
  1960. for(i=0;i<3;i++)
  1961. g->table_select[i] = get_bits(&s->gb, 5);
  1962. /* compute huffman coded region sizes */
  1963. region_address1 = get_bits(&s->gb, 4);
  1964. region_address2 = get_bits(&s->gb, 3);
  1965. g->region_size[0] =
  1966. band_index_long[s->sample_rate_index][region_address1 + 1] >> 1;
  1967. l = region_address1 + region_address2 + 2;
  1968. /* should not overflow */
  1969. if (l > 22)
  1970. l = 22;
  1971. g->region_size[1] =
  1972. band_index_long[s->sample_rate_index][l] >> 1;
  1973. }
  1974. /* convert region offsets to region sizes and truncate
  1975. size to big_values */
  1976. g->region_size[2] = (576 / 2);
  1977. j = 0;
  1978. for(i=0;i<3;i++) {
  1979. k = g->region_size[i];
  1980. if (g->big_values < k) k = g->big_values;
  1981. g->region_size[i] = k - j;
  1982. j = k;
  1983. }
  1984. /* compute band indexes */
  1985. if (g->block_type == 2) {
  1986. if (g->switch_point) {
  1987. /* if switched mode, we handle the 36 first samples as
  1988. long blocks. For 8000Hz, we handle the 48 first
  1989. exponents as long blocks (XXX: check this!) */
  1990. if (s->sample_rate_index <= 2)
  1991. g->long_end = 8;
  1992. else if (s->sample_rate_index != 8)
  1993. g->long_end = 6;
  1994. else
  1995. g->long_end = 4; /* 8000 Hz */
  1996. g->short_start = 2 + (s->sample_rate_index != 8);
  1997. } else {
  1998. g->long_end = 0;
  1999. g->short_start = 0;
  2000. }
  2001. } else {
  2002. g->short_start = 13;
  2003. g->long_end = 22;
  2004. }
  2005. g->preflag = 0;
  2006. if (!s->lsf)
  2007. g->preflag = get_bits(&s->gb, 1);
  2008. g->scalefac_scale = get_bits(&s->gb, 1);
  2009. g->count1table_select = get_bits(&s->gb, 1);
  2010. }
  2011. }
  2012. ptr = s->gb.buffer + (get_bits_count(&s->gb)>>3);
  2013. /* now we get bits from the main_data_begin offset */
  2014. if(main_data_begin > s->last_buf_size){
  2015. s->last_buf_size= main_data_begin;
  2016. }
  2017. memcpy(s->last_buf + s->last_buf_size, ptr, EXTRABYTES);
  2018. s->in_gb= s->gb;
  2019. init_get_bits(&s->gb, s->last_buf + s->last_buf_size - main_data_begin, main_data_begin*8);
  2020. for(gr=0;gr<nb_granules;gr++) {
  2021. for(ch=0;ch<s->nb_channels;ch++) {
  2022. g = &granules[ch][gr];
  2023. bits_pos = get_bits_count(&s->gb);
  2024. if (!s->lsf) {
  2025. uint8_t *sc;
  2026. int slen, slen1, slen2;
  2027. /* MPEG1 scale factors */
  2028. slen1 = slen_table[0][g->scalefac_compress];
  2029. slen2 = slen_table[1][g->scalefac_compress];
  2030. if (g->block_type == 2) {
  2031. n = g->switch_point ? 17 : 18;
  2032. j = 0;
  2033. if(slen1){
  2034. for(i=0;i<n;i++)
  2035. g->scale_factors[j++] = get_bits(&s->gb, slen1);
  2036. }else{
  2037. libc_memset((void*) &g->scale_factors[j], 0, n);
  2038. j += n;
  2039. // for(i=0;i<n;i++)
  2040. // g->scale_factors[j++] = 0;
  2041. }
  2042. if(slen2){
  2043. for(i=0;i<18;i++)
  2044. g->scale_factors[j++] = get_bits(&s->gb, slen2);
  2045. for(i=0;i<3;i++)
  2046. g->scale_factors[j++] = 0;
  2047. }else{
  2048. for(i=0;i<21;i++)
  2049. g->scale_factors[j++] = 0;
  2050. }
  2051. } else {
  2052. sc = granules[ch][0].scale_factors;
  2053. j = 0;
  2054. for(k=0;k<4;k++) {
  2055. n = (k == 0 ? 6 : 5);
  2056. if ((g->scfsi & (0x8 >> k)) == 0) {
  2057. slen = (k < 2) ? slen1 : slen2;
  2058. if(slen){
  2059. for(i=0;i<n;i++)
  2060. g->scale_factors[j++] = get_bits(&s->gb, slen);
  2061. }else{
  2062. libc_memset((void*) &g->scale_factors[j], 0, n);
  2063. j += n;
  2064. // for(i=0;i<n;i++)
  2065. // g->scale_factors[j++] = 0;
  2066. }
  2067. } else {
  2068. /* simply copy from last granule */
  2069. for(i=0;i<n;i++) {
  2070. g->scale_factors[j] = sc[j];
  2071. j++;
  2072. }
  2073. }
  2074. }
  2075. g->scale_factors[j++] = 0;
  2076. }
  2077. } else {
  2078. int tindex, tindex2, slen[4], sl, sf;
  2079. /* LSF scale factors */
  2080. if (g->block_type == 2) {
  2081. tindex = g->switch_point ? 2 : 1;
  2082. } else {
  2083. tindex = 0;
  2084. }
  2085. sf = g->scalefac_compress;
  2086. if ((s->mode_ext & MODE_EXT_I_STEREO) && ch == 1) {
  2087. /* intensity stereo case */
  2088. sf >>= 1;
  2089. if (sf < 180) {
  2090. lsf_sf_expand(slen, sf, 6, 6, 0);
  2091. tindex2 = 3;
  2092. } else if (sf < 244) {
  2093. lsf_sf_expand(slen, sf - 180, 4, 4, 0);
  2094. tindex2 = 4;
  2095. } else {
  2096. lsf_sf_expand(slen, sf - 244, 3, 0, 0);
  2097. tindex2 = 5;
  2098. }
  2099. } else {
  2100. /* normal case */
  2101. if (sf < 400) {
  2102. lsf_sf_expand(slen, sf, 5, 4, 4);
  2103. tindex2 = 0;
  2104. } else if (sf < 500) {
  2105. lsf_sf_expand(slen, sf - 400, 5, 4, 0);
  2106. tindex2 = 1;
  2107. } else {
  2108. lsf_sf_expand(slen, sf - 500, 3, 0, 0);
  2109. tindex2 = 2;
  2110. g->preflag = 1;
  2111. }
  2112. }
  2113. j = 0;
  2114. for(k=0;k<4;k++) {
  2115. n = lsf_nsf_table[tindex2][tindex][k];
  2116. sl = slen[k];
  2117. if(sl){
  2118. for(i=0;i<n;i++)
  2119. g->scale_factors[j++] = get_bits(&s->gb, sl);
  2120. }else{
  2121. libc_memset((void*) &g->scale_factors[j], 0, n);
  2122. j += n;
  2123. // for(i=0;i<n;i++)
  2124. // g->scale_factors[j++] = 0;
  2125. }
  2126. }
  2127. /* XXX: should compute exact size */
  2128. libc_memset((void*) &g->scale_factors[j], 0, 40 - j);
  2129. // for(;j<40;j++)
  2130. // g->scale_factors[j] = 0;
  2131. }
  2132. exponents_from_scale_factors(s, g, exponents);
  2133. /* read Huffman coded residue */
  2134. if (huffman_decode(s, g, exponents,
  2135. bits_pos + g->part2_3_length) < 0)
  2136. return -1;
  2137. } /* ch */
  2138. if (s->nb_channels == 2)
  2139. compute_stereo(s, &granules[0][gr], &granules[1][gr]);
  2140. for(ch=0;ch<s->nb_channels;ch++) {
  2141. g = &granules[ch][gr];
  2142. reorder_block(s, g);
  2143. compute_antialias(s, g);
  2144. compute_imdct(s, g, &s->sb_samples[ch][18 * gr][0], s->mdct_buf[ch]);
  2145. }
  2146. } /* gr */
  2147. return nb_granules * 18;
  2148. }
  2149. static int mp3_decode_main(
  2150. mp3_context_t *s,
  2151. int16_t *samples, const uint8_t *buf, int buf_size
  2152. ) {
  2153. int i, nb_frames, ch;
  2154. int16_t *samples_ptr;
  2155. init_get_bits(&s->gb, buf + HEADER_SIZE, (buf_size - HEADER_SIZE)*8);
  2156. if (s->error_protection)
  2157. get_bits(&s->gb, 16);
  2158. nb_frames = mp_decode_layer3(s);
  2159. s->last_buf_size=0;
  2160. if(s->in_gb.buffer){
  2161. align_get_bits(&s->gb);
  2162. i= (s->gb.size_in_bits - get_bits_count(&s->gb))>>3;
  2163. if(i >= 0 && i <= BACKSTEP_SIZE){
  2164. libc_memmove(s->last_buf, s->gb.buffer + (get_bits_count(&s->gb)>>3), i);
  2165. s->last_buf_size=i;
  2166. }
  2167. s->gb= s->in_gb;
  2168. }
  2169. align_get_bits(&s->gb);
  2170. i= (s->gb.size_in_bits - get_bits_count(&s->gb))>>3;
  2171. if(i<0 || i > BACKSTEP_SIZE || nb_frames<0){
  2172. i = buf_size - HEADER_SIZE;
  2173. if (BACKSTEP_SIZE < i) i = BACKSTEP_SIZE;
  2174. }
  2175. libc_memcpy(s->last_buf + s->last_buf_size, s->gb.buffer + buf_size - HEADER_SIZE - i, i);
  2176. s->last_buf_size += i;
  2177. /* apply the synthesis filter */
  2178. for(ch=0;ch<s->nb_channels;ch++) {
  2179. samples_ptr = samples + ch;
  2180. for(i=0;i<nb_frames;i++) {
  2181. mp3_synth_filter(
  2182. s->synth_buf[ch], &(s->synth_buf_offset[ch]),
  2183. window, &s->dither_state,
  2184. samples_ptr, s->nb_channels,
  2185. s->sb_samples[ch][i]
  2186. );
  2187. samples_ptr += 32 * s->nb_channels;
  2188. }
  2189. }
  2190. return nb_frames * 32 * sizeof(uint16_t) * s->nb_channels;
  2191. }
  2192. ////////////////////////////////////////////////////////////////////////////////
  2193. static int mp3_decode_init(mp3_context_t *s) {
  2194. static int init=0;
  2195. int i, j, k;
  2196. if (!init) {
  2197. /* synth init */
  2198. for(i=0;i<257;i++) {
  2199. int v;
  2200. v = mp3_enwindow[i];
  2201. #if WFRAC_BITS < 16
  2202. v = (v + (1 << (16 - WFRAC_BITS - 1))) >> (16 - WFRAC_BITS);
  2203. #endif
  2204. window[i] = v;
  2205. if ((i & 63) != 0)
  2206. v = -v;
  2207. if (i != 0)
  2208. window[512 - i] = v;
  2209. }
  2210. /* huffman decode tables */
  2211. for(i=1;i<16;i++) {
  2212. const huff_table_t *h = &mp3_huff_tables[i];
  2213. int xsize, x, y;
  2214. unsigned int n;
  2215. uint8_t tmp_bits [512];
  2216. uint16_t tmp_codes[512];
  2217. libc_memset(tmp_bits , 0, sizeof(tmp_bits ));
  2218. libc_memset(tmp_codes, 0, sizeof(tmp_codes));
  2219. xsize = h->xsize;
  2220. n = xsize * xsize;
  2221. j = 0;
  2222. for(x=0;x<xsize;x++) {
  2223. for(y=0;y<xsize;y++){
  2224. tmp_bits [(x << 5) | y | ((x&&y)<<4)]= h->bits [j ];
  2225. tmp_codes[(x << 5) | y | ((x&&y)<<4)]= h->codes[j++];
  2226. }
  2227. }
  2228. init_vlc(&huff_vlc[i], 7, 512,
  2229. tmp_bits, 1, 1, tmp_codes, 2, 2);
  2230. }
  2231. for(i=0;i<2;i++) {
  2232. init_vlc(&huff_quad_vlc[i], i == 0 ? 7 : 4, 16,
  2233. mp3_quad_bits[i], 1, 1, mp3_quad_codes[i], 1, 1);
  2234. }
  2235. for(i=0;i<9;i++) {
  2236. k = 0;
  2237. for(j=0;j<22;j++) {
  2238. band_index_long[i][j] = k;
  2239. k += band_size_long[i][j];
  2240. }
  2241. band_index_long[i][22] = k;
  2242. }
  2243. /* compute n ^ (4/3) and store it in mantissa/exp format */
  2244. table_4_3_exp= libc_malloc(TABLE_4_3_SIZE * sizeof(table_4_3_exp[0]));
  2245. if(!table_4_3_exp)
  2246. return -1;
  2247. table_4_3_value= libc_malloc(TABLE_4_3_SIZE * sizeof(table_4_3_value[0]));
  2248. if(!table_4_3_value)
  2249. return -1;
  2250. for(i=1;i<TABLE_4_3_SIZE;i++) {
  2251. double f, fm;
  2252. int e, m;
  2253. f = libc_pow((double)(i/4), 4.0 / 3.0) * libc_pow(2, (i&3)*0.25);
  2254. fm = libc_frexp(f, &e);
  2255. m = (uint32_t)(fm*(1LL<<31) + 0.5);
  2256. e+= FRAC_BITS - 31 + 5 - 100;
  2257. table_4_3_value[i] = m;
  2258. table_4_3_exp[i] = -e;
  2259. }
  2260. for(i=0; i<512*16; i++){
  2261. int exponent= (i>>4);
  2262. double f= libc_pow(i&15, 4.0 / 3.0) * libc_pow(2, (exponent-400)*0.25 + FRAC_BITS + 5);
  2263. expval_table[exponent][i&15]= f;
  2264. if((i&15)==1)
  2265. exp_table[exponent]= f;
  2266. }
  2267. for(i=0;i<7;i++) {
  2268. float f;
  2269. int v;
  2270. if (i != 6) {
  2271. f = tan((double)i * M_PI / 12.0);
  2272. v = FIXR(f / (1.0 + f));
  2273. } else {
  2274. v = FIXR(1.0);
  2275. }
  2276. is_table[0][i] = v;
  2277. is_table[1][6 - i] = v;
  2278. }
  2279. for(i=7;i<16;i++)
  2280. is_table[0][i] = is_table[1][i] = 0.0;
  2281. for(i=0;i<16;i++) {
  2282. double f;
  2283. int e, k;
  2284. for(j=0;j<2;j++) {
  2285. e = -(j + 1) * ((i + 1) >> 1);
  2286. f = libc_pow(2.0, e / 4.0);
  2287. k = i & 1;
  2288. is_table_lsf[j][k ^ 1][i] = FIXR(f);
  2289. is_table_lsf[j][k][i] = FIXR(1.0);
  2290. }
  2291. }
  2292. for(i=0;i<8;i++) {
  2293. float ci, cs, ca;
  2294. ci = ci_table[i];
  2295. cs = 1.0 / sqrt(1.0 + ci * ci);
  2296. ca = cs * ci;
  2297. csa_table[i][0] = FIXHR(cs/4);
  2298. csa_table[i][1] = FIXHR(ca/4);
  2299. csa_table[i][2] = FIXHR(ca/4) + FIXHR(cs/4);
  2300. csa_table[i][3] = FIXHR(ca/4) - FIXHR(cs/4);
  2301. csa_table_float[i][0] = cs;
  2302. csa_table_float[i][1] = ca;
  2303. csa_table_float[i][2] = ca + cs;
  2304. csa_table_float[i][3] = ca - cs;
  2305. }
  2306. /* compute mdct windows */
  2307. for(i=0;i<36;i++) {
  2308. for(j=0; j<4; j++){
  2309. double d;
  2310. if(j==2 && i%3 != 1)
  2311. continue;
  2312. d= sin(M_PI * (i + 0.5) / 36.0);
  2313. if(j==1){
  2314. if (i>=30) d= 0;
  2315. else if(i>=24) d= sin(M_PI * (i - 18 + 0.5) / 12.0);
  2316. else if(i>=18) d= 1;
  2317. }else if(j==3){
  2318. if (i< 6) d= 0;
  2319. else if(i< 12) d= sin(M_PI * (i - 6 + 0.5) / 12.0);
  2320. else if(i< 18) d= 1;
  2321. }
  2322. d*= 0.5 / cos(M_PI*(2*i + 19)/72);
  2323. if(j==2)
  2324. mdct_win[j][i/3] = FIXHR((d / (1<<5)));
  2325. else
  2326. mdct_win[j][i ] = FIXHR((d / (1<<5)));
  2327. }
  2328. }
  2329. for(j=0;j<4;j++) {
  2330. for(i=0;i<36;i+=2) {
  2331. mdct_win[j + 4][i] = mdct_win[j][i];
  2332. mdct_win[j + 4][i + 1] = -mdct_win[j][i + 1];
  2333. }
  2334. }
  2335. init = 1;
  2336. }
  2337. return 0;
  2338. }
  2339. static int mp3_decode_frame(
  2340. mp3_context_t *s,
  2341. int16_t *out_samples, int *data_size,
  2342. uint8_t *buf, int buf_size
  2343. ) {
  2344. uint32_t header;
  2345. int out_size;
  2346. int extra_bytes = 0;
  2347. retry:
  2348. if(buf_size < HEADER_SIZE)
  2349. return -1;
  2350. header = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
  2351. if(mp3_check_header(header) < 0){
  2352. buf++;
  2353. buf_size--;
  2354. extra_bytes++;
  2355. goto retry;
  2356. }
  2357. if (decode_header(s, header) == 1) {
  2358. s->frame_size = -1;
  2359. return -1;
  2360. }
  2361. if(s->frame_size<=0 || s->frame_size > buf_size){
  2362. return -1; // incomplete frame
  2363. }
  2364. if(s->frame_size < buf_size) {
  2365. buf_size = s->frame_size;
  2366. }
  2367. out_size = mp3_decode_main(s, out_samples, buf, buf_size);
  2368. if(out_size>=0)
  2369. *data_size = out_size;
  2370. // else: Error while decoding MPEG audio frame.
  2371. s->frame_size += extra_bytes;
  2372. return buf_size;
  2373. }
  2374. ////////////////////////////////////////////////////////////////////////////////
  2375. mp3_decoder_t mp3_create(void) {
  2376. void *dec = libc_calloc(sizeof(mp3_context_t), 1);
  2377. if (dec) mp3_decode_init((mp3_context_t*) dec);
  2378. return (mp3_decoder_t) dec;
  2379. }
  2380. void mp3_done(mp3_decoder_t *dec) {
  2381. if (dec) libc_free(dec);
  2382. }
  2383. int mp3_decode(mp3_decoder_t *dec, void *buf, int bytes, signed short *out, mp3_info_t *info) {
  2384. int res, size = -1;
  2385. mp3_context_t *s = (mp3_context_t*) dec;
  2386. if (!s) return 0;
  2387. res = mp3_decode_frame(s, (int16_t*) out, &size, buf, bytes);
  2388. if (res < 0) return 0;
  2389. if (info) {
  2390. info->sample_rate = s->sample_rate;
  2391. info->channels = s->nb_channels;
  2392. info->audio_bytes = size;
  2393. }
  2394. return s->frame_size;
  2395. }