layer3.c 63 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969
  1. /*
  2. * layer3.c: Mpeg Layer-3 audio decoder
  3. *
  4. * Copyright (C) 1999-2010 The L.A.M.E. project
  5. *
  6. * Initially written by Michael Hipp, see also AUTHORS and README.
  7. *
  8. * This library is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Library General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2 of the License, or (at your option) any later version.
  12. *
  13. * This library is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Library General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Library General Public
  19. * License along with this library; if not, write to the
  20. * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  21. * Boston, MA 02111-1307, USA.
  22. */
  23. /* $Id: layer3.c,v 1.69 2017/08/20 20:06:57 robert Exp $ */
  24. #ifdef HAVE_CONFIG_H
  25. # include <config.h>
  26. #endif
  27. #include <stdlib.h>
  28. #include "common.h"
  29. #include "huffman.h"
  30. #include "lame.h"
  31. #include "machine.h"
  32. #include "encoder.h"
  33. #include "lame-analysis.h"
  34. #include "decode_i386.h"
  35. #include "layer3.h"
  36. #ifdef WITH_DMALLOC
  37. #include <dmalloc.h>
  38. #endif
  39. static int gd_are_hip_tables_layer3_initialized = 0;
  40. static real ispow[8207];
  41. static real aa_ca[8], aa_cs[8];
  42. static real COS1[12][6];
  43. static real win[4][36];
  44. static real win1[4][36];
  45. static real gainpow2[256 + 118 + 4];
  46. static real COS9[9];
  47. static real COS6_1, COS6_2;
  48. static real tfcos36[9];
  49. static real tfcos12[3];
  50. struct bandInfoStruct {
  51. short longIdx[23];
  52. short longDiff[22];
  53. short shortIdx[14];
  54. short shortDiff[13];
  55. };
  56. static int longLimit[9][23];
  57. static int shortLimit[9][14];
  58. /* *INDENT-OFF* */
  59. static const struct bandInfoStruct bandInfo[9] = {
  60. /* MPEG 1.0 */
  61. { {0,4,8,12,16,20,24,30,36,44,52,62,74, 90,110,134,162,196,238,288,342,418,576},
  62. {4,4,4,4,4,4,6,6,8, 8,10,12,16,20,24,28,34,42,50,54, 76,158},
  63. {0,4*3,8*3,12*3,16*3,22*3,30*3,40*3,52*3,66*3, 84*3,106*3,136*3,192*3},
  64. {4,4,4,4,6,8,10,12,14,18,22,30,56} } ,
  65. { {0,4,8,12,16,20,24,30,36,42,50,60,72, 88,106,128,156,190,230,276,330,384,576},
  66. {4,4,4,4,4,4,6,6,6, 8,10,12,16,18,22,28,34,40,46,54, 54,192},
  67. {0,4*3,8*3,12*3,16*3,22*3,28*3,38*3,50*3,64*3, 80*3,100*3,126*3,192*3},
  68. {4,4,4,4,6,6,10,12,14,16,20,26,66} } ,
  69. { {0,4,8,12,16,20,24,30,36,44,54,66,82,102,126,156,194,240,296,364,448,550,576} ,
  70. {4,4,4,4,4,4,6,6,8,10,12,16,20,24,30,38,46,56,68,84,102, 26} ,
  71. {0,4*3,8*3,12*3,16*3,22*3,30*3,42*3,58*3,78*3,104*3,138*3,180*3,192*3} ,
  72. {4,4,4,4,6,8,12,16,20,26,34,42,12} } ,
  73. /* MPEG 2.0 */
  74. { {0,6,12,18,24,30,36,44,54,66,80,96,116,140,168,200,238,284,336,396,464,522,576},
  75. {6,6,6,6,6,6,8,10,12,14,16,20,24,28,32,38,46,52,60,68,58,54 } ,
  76. {0,4*3,8*3,12*3,18*3,24*3,32*3,42*3,56*3,74*3,100*3,132*3,174*3,192*3} ,
  77. {4,4,4,6,6,8,10,14,18,26,32,42,18 } } ,
  78. /* docs: 332. mpg123: 330 */
  79. { {0,6,12,18,24,30,36,44,54,66,80,96,114,136,162,194,232,278,332,394,464,540,576},
  80. {6,6,6,6,6,6,8,10,12,14,16,18,22,26,32,38,46,54,62,70,76,36 } ,
  81. {0,4*3,8*3,12*3,18*3,26*3,36*3,48*3,62*3,80*3,104*3,136*3,180*3,192*3} ,
  82. {4,4,4,6,8,10,12,14,18,24,32,44,12 } } ,
  83. { {0,6,12,18,24,30,36,44,54,66,80,96,116,140,168,200,238,284,336,396,464,522,576},
  84. {6,6,6,6,6,6,8,10,12,14,16,20,24,28,32,38,46,52,60,68,58,54 },
  85. {0,4*3,8*3,12*3,18*3,26*3,36*3,48*3,62*3,80*3,104*3,134*3,174*3,192*3},
  86. {4,4,4,6,8,10,12,14,18,24,30,40,18 } } ,
  87. /* MPEG 2.5 */
  88. { {0,6,12,18,24,30,36,44,54,66,80,96,116,140,168,200,238,284,336,396,464,522,576} ,
  89. {6,6,6,6,6,6,8,10,12,14,16,20,24,28,32,38,46,52,60,68,58,54},
  90. {0,12,24,36,54,78,108,144,186,240,312,402,522,576},
  91. {4,4,4,6,8,10,12,14,18,24,30,40,18} },
  92. { {0,6,12,18,24,30,36,44,54,66,80,96,116,140,168,200,238,284,336,396,464,522,576} ,
  93. {6,6,6,6,6,6,8,10,12,14,16,20,24,28,32,38,46,52,60,68,58,54},
  94. {0,12,24,36,54,78,108,144,186,240,312,402,522,576},
  95. {4,4,4,6,8,10,12,14,18,24,30,40,18} },
  96. { {0,12,24,36,48,60,72,88,108,132,160,192,232,280,336,400,476,566,568,570,572,574,576},
  97. {12,12,12,12,12,12,16,20,24,28,32,40,48,56,64,76,90,2,2,2,2,2},
  98. {0, 24, 48, 72,108,156,216,288,372,480,486,492,498,576},
  99. {8,8,8,12,16,20,24,28,36,2,2,2,26} } ,
  100. };
  101. /* *INDENT-ON* */
  102. static int mapbuf0[9][152];
  103. static int mapbuf1[9][156];
  104. static int mapbuf2[9][44];
  105. static int *map[9][3];
  106. static int *mapend[9][3];
  107. static unsigned int n_slen2[512]; /* MPEG 2.0 slen for 'normal' mode */
  108. static unsigned int i_slen2[256]; /* MPEG 2.0 slen for intensity stereo */
  109. static real tan1_1[16], tan2_1[16], tan1_2[16], tan2_2[16];
  110. static real pow1_1[2][16], pow2_1[2][16], pow1_2[2][16], pow2_2[2][16];
  111. static unsigned int
  112. get1bit(PMPSTR mp)
  113. {
  114. unsigned char rval;
  115. rval = *mp->wordpointer << mp->bitindex;
  116. mp->bitindex++;
  117. mp->wordpointer += (mp->bitindex >> 3);
  118. mp->bitindex &= 7;
  119. return rval >> 7;
  120. }
  121. static real
  122. get_gain(real const* gain_ptr, int idx, int* overflow)
  123. {
  124. static const real* const gainpow2_end_ptr = gainpow2 + (sizeof(gainpow2)/sizeof(gainpow2[0])) -1;
  125. real const * ptr = &gain_ptr[idx];
  126. if (&gain_ptr[idx] > gainpow2_end_ptr) {
  127. ptr = gainpow2_end_ptr;
  128. if (overflow) *overflow = 1;
  129. }
  130. return *ptr;
  131. }
  132. /*
  133. * init tables for layer-3
  134. */
  135. void
  136. hip_init_tables_layer3(void)
  137. {
  138. int i, j, k;
  139. if (gd_are_hip_tables_layer3_initialized) {
  140. return;
  141. }
  142. gd_are_hip_tables_layer3_initialized = 1;
  143. for (i = -256; i < 118 + 4; i++)
  144. gainpow2[i + 256] = pow((double) 2.0, -0.25 * (double) (i + 210));
  145. for (i = 0; i < 8207; i++)
  146. ispow[i] = pow((double) i, (double) 4.0 / 3.0);
  147. for (i = 0; i < 8; i++) {
  148. static const double Ci[8] = { -0.6, -0.535, -0.33, -0.185, -0.095, -0.041, -0.0142, -0.0037 };
  149. double sq = sqrt(1.0 + Ci[i] * Ci[i]);
  150. aa_cs[i] = 1.0 / sq;
  151. aa_ca[i] = Ci[i] / sq;
  152. }
  153. for (i = 0; i < 18; i++) {
  154. win[0][i] = win[1][i] =
  155. 0.5 * sin(M_PI / 72.0 * (double) (2 * (i + 0) + 1)) / cos(M_PI *
  156. (double) (2 * (i + 0) +
  157. 19) / 72.0);
  158. win[0][i + 18] = win[3][i + 18] =
  159. 0.5 * sin(M_PI / 72.0 * (double) (2 * (i + 18) + 1)) / cos(M_PI *
  160. (double) (2 * (i + 18) +
  161. 19) / 72.0);
  162. }
  163. for (i = 0; i < 6; i++) {
  164. win[1][i + 18] = 0.5 / cos(M_PI * (double) (2 * (i + 18) + 19) / 72.0);
  165. win[3][i + 12] = 0.5 / cos(M_PI * (double) (2 * (i + 12) + 19) / 72.0);
  166. win[1][i + 24] =
  167. 0.5 * sin(M_PI / 24.0 * (double) (2 * i + 13)) / cos(M_PI *
  168. (double) (2 * (i + 24) +
  169. 19) / 72.0);
  170. win[1][i + 30] = win[3][i] = 0.0;
  171. win[3][i + 6] =
  172. 0.5 * sin(M_PI / 24.0 * (double) (2 * i + 1)) / cos(M_PI * (double) (2 * (i + 6) + 19) /
  173. 72.0);
  174. }
  175. for (i = 0; i < 9; i++)
  176. COS9[i] = cos(M_PI / 18.0 * (double) i);
  177. for (i = 0; i < 9; i++)
  178. tfcos36[i] = 0.5 / cos(M_PI * (double) (i * 2 + 1) / 36.0);
  179. for (i = 0; i < 3; i++)
  180. tfcos12[i] = 0.5 / cos(M_PI * (double) (i * 2 + 1) / 12.0);
  181. COS6_1 = cos(M_PI / 6.0 * (double) 1);
  182. COS6_2 = cos(M_PI / 6.0 * (double) 2);
  183. for (i = 0; i < 12; i++) {
  184. win[2][i] =
  185. 0.5 * sin(M_PI / 24.0 * (double) (2 * i + 1)) / cos(M_PI * (double) (2 * i + 7) / 24.0);
  186. for (j = 0; j < 6; j++)
  187. COS1[i][j] = cos(M_PI / 24.0 * (double) ((2 * i + 7) * (2 * j + 1)));
  188. }
  189. for (j = 0; j < 4; j++) {
  190. static int const len[4] = { 36, 36, 12, 36 };
  191. for (i = 0; i < len[j]; i += 2)
  192. win1[j][i] = +win[j][i];
  193. for (i = 1; i < len[j]; i += 2)
  194. win1[j][i] = -win[j][i];
  195. }
  196. for (i = 0; i < 16; i++) {
  197. double t = tan((double) i * M_PI / 12.0);
  198. tan1_1[i] = t / (1.0 + t);
  199. tan2_1[i] = 1.0 / (1.0 + t);
  200. tan1_2[i] = M_SQRT2 * t / (1.0 + t);
  201. tan2_2[i] = M_SQRT2 / (1.0 + t);
  202. for (j = 0; j < 2; j++) {
  203. double base = pow(2.0, -0.25 * (j + 1.0));
  204. double p1 = 1.0, p2 = 1.0;
  205. if (i > 0) {
  206. if (i & 1)
  207. p1 = pow(base, (i + 1.0) * 0.5);
  208. else
  209. p2 = pow(base, i * 0.5);
  210. }
  211. pow1_1[j][i] = p1;
  212. pow2_1[j][i] = p2;
  213. pow1_2[j][i] = M_SQRT2 * p1;
  214. pow2_2[j][i] = M_SQRT2 * p2;
  215. }
  216. }
  217. for (j = 0; j < 9; j++) {
  218. struct bandInfoStruct const *bi = (struct bandInfoStruct const *) &bandInfo[j];
  219. int *mp;
  220. int cb, lwin;
  221. short const *bdf;
  222. int switch_idx = (j < 3) ? 8 : 6;
  223. mp = map[j][0] = mapbuf0[j];
  224. bdf = bi->longDiff;
  225. for (i = 0, cb = 0; cb < switch_idx; cb++, i += *bdf++) {
  226. *mp++ = (*bdf) >> 1;
  227. *mp++ = i;
  228. *mp++ = 3;
  229. *mp++ = cb;
  230. }
  231. bdf = bi->shortDiff + 3;
  232. for (cb = 3; cb < 13; cb++) {
  233. int l = (*bdf++) >> 1;
  234. for (lwin = 0; lwin < 3; lwin++) {
  235. *mp++ = l;
  236. *mp++ = i + lwin;
  237. *mp++ = lwin;
  238. *mp++ = cb;
  239. }
  240. i += 6 * l;
  241. }
  242. mapend[j][0] = mp;
  243. mp = map[j][1] = mapbuf1[j];
  244. bdf = bi->shortDiff + 0;
  245. for (i = 0, cb = 0; cb < 13; cb++) {
  246. int l = (*bdf++) >> 1;
  247. for (lwin = 0; lwin < 3; lwin++) {
  248. *mp++ = l;
  249. *mp++ = i + lwin;
  250. *mp++ = lwin;
  251. *mp++ = cb;
  252. }
  253. i += 6 * l;
  254. }
  255. mapend[j][1] = mp;
  256. mp = map[j][2] = mapbuf2[j];
  257. bdf = bi->longDiff;
  258. for (cb = 0; cb < 22; cb++) {
  259. *mp++ = (*bdf++) >> 1;
  260. *mp++ = cb;
  261. }
  262. mapend[j][2] = mp;
  263. }
  264. for (j = 0; j < 9; j++) {
  265. for (i = 0; i < 23; i++) {
  266. longLimit[j][i] = (bandInfo[j].longIdx[i] - 1 + 8) / 18 + 1;
  267. if (longLimit[j][i] > SBLIMIT)
  268. longLimit[j][i] = SBLIMIT;
  269. }
  270. for (i = 0; i < 14; i++) {
  271. shortLimit[j][i] = (bandInfo[j].shortIdx[i] - 1) / 18 + 1;
  272. if (shortLimit[j][i] > SBLIMIT)
  273. shortLimit[j][i] = SBLIMIT;
  274. }
  275. }
  276. for (i = 0; i < 5; i++) {
  277. for (j = 0; j < 6; j++) {
  278. for (k = 0; k < 6; k++) {
  279. int n = k + j * 6 + i * 36;
  280. i_slen2[n] = i | (j << 3) | (k << 6) | (3 << 12);
  281. }
  282. }
  283. }
  284. for (i = 0; i < 4; i++) {
  285. for (j = 0; j < 4; j++) {
  286. for (k = 0; k < 4; k++) {
  287. int n = k + j * 4 + i * 16;
  288. i_slen2[n + 180] = i | (j << 3) | (k << 6) | (4 << 12);
  289. }
  290. }
  291. }
  292. for (i = 0; i < 4; i++) {
  293. for (j = 0; j < 3; j++) {
  294. int n = j + i * 3;
  295. i_slen2[n + 244] = i | (j << 3) | (5 << 12);
  296. n_slen2[n + 500] = i | (j << 3) | (2 << 12) | (1 << 15);
  297. }
  298. }
  299. for (i = 0; i < 5; i++) {
  300. for (j = 0; j < 5; j++) {
  301. for (k = 0; k < 4; k++) {
  302. int l;
  303. for (l = 0; l < 4; l++) {
  304. int n = l + k * 4 + j * 16 + i * 80;
  305. n_slen2[n] = i | (j << 3) | (k << 6) | (l << 9) | (0 << 12);
  306. }
  307. }
  308. }
  309. }
  310. for (i = 0; i < 5; i++) {
  311. for (j = 0; j < 5; j++) {
  312. for (k = 0; k < 4; k++) {
  313. int n = k + j * 4 + i * 20;
  314. n_slen2[n + 400] = i | (j << 3) | (k << 6) | (1 << 12);
  315. }
  316. }
  317. }
  318. }
  319. /*
  320. * read additional side information
  321. */
  322. static void
  323. III_get_side_info_1(PMPSTR mp, int stereo,
  324. int ms_stereo, long sfreq, int single)
  325. {
  326. int ch, gr;
  327. int powdiff = (single == 3) ? 4 : 0;
  328. mp->sideinfo.main_data_begin = getbits(mp, 9);
  329. if (stereo == 1)
  330. mp->sideinfo.private_bits = getbits_fast(mp, 5);
  331. else
  332. mp->sideinfo.private_bits = getbits_fast(mp, 3);
  333. for (ch = 0; ch < stereo; ch++) {
  334. mp->sideinfo.ch[ch].gr[0].scfsi = -1;
  335. mp->sideinfo.ch[ch].gr[1].scfsi = getbits_fast(mp, 4);
  336. }
  337. for (gr = 0; gr < 2; gr++) {
  338. for (ch = 0; ch < stereo; ch++) {
  339. struct gr_info_s *gr_infos = &(mp->sideinfo.ch[ch].gr[gr]);
  340. gr_infos->part2_3_length = getbits(mp, 12);
  341. gr_infos->big_values = getbits_fast(mp, 9);
  342. if (gr_infos->big_values > 288) {
  343. lame_report_fnc(mp->report_err, "big_values too large! %i\n", gr_infos->big_values);
  344. gr_infos->big_values = 288;
  345. }
  346. {
  347. unsigned int qss = getbits_fast(mp, 8);
  348. gr_infos->pow2gain = gainpow2 + 256 - qss + powdiff;
  349. if (mp->pinfo != NULL) {
  350. mp->pinfo->qss[gr][ch] = qss;
  351. }
  352. }
  353. if (ms_stereo)
  354. gr_infos->pow2gain += 2;
  355. gr_infos->scalefac_compress = getbits_fast(mp, 4);
  356. /* window-switching flag == 1 for block_Type != 0 .. and block-type == 0 -> win-sw-flag = 0 */
  357. if (get1bit(mp)) {
  358. int i;
  359. gr_infos->block_type = getbits_fast(mp, 2);
  360. gr_infos->mixed_block_flag = get1bit(mp);
  361. gr_infos->table_select[0] = getbits_fast(mp, 5);
  362. gr_infos->table_select[1] = getbits_fast(mp, 5);
  363. /*
  364. * table_select[2] not needed, because there is no region2,
  365. * but to satisfy some verifications tools we set it either.
  366. */
  367. gr_infos->table_select[2] = 0;
  368. for (i = 0; i < 3; i++) {
  369. unsigned int sbg = (getbits_fast(mp, 3) << 3);
  370. gr_infos->full_gain[i] = gr_infos->pow2gain + sbg;
  371. if (mp->pinfo != NULL)
  372. mp->pinfo->sub_gain[gr][ch][i] = sbg / 8;
  373. }
  374. if (gr_infos->block_type == 0) {
  375. lame_report_fnc(mp->report_err, "Blocktype == 0 and window-switching == 1 not allowed.\n");
  376. /* error seems to be very good recoverable, so don't exit */
  377. /* exit(1); */
  378. }
  379. /* region_count/start parameters are implicit in this case. */
  380. gr_infos->region1start = 36 >> 1;
  381. gr_infos->region2start = 576 >> 1;
  382. }
  383. else {
  384. unsigned int i, r0c, r1c, region0index, region1index;
  385. for (i = 0; i < 3; i++)
  386. gr_infos->table_select[i] = getbits_fast(mp, 5);
  387. r0c = getbits_fast(mp, 4);
  388. r1c = getbits_fast(mp, 3);
  389. region0index = r0c+1;
  390. if (region0index > 22) {
  391. lame_report_fnc(mp->report_err, "region0index=%d > 22\n", region0index);
  392. region0index = 22;
  393. }
  394. region1index = r0c+1 + r1c+1;
  395. if (region1index > 22) {
  396. lame_report_fnc(mp->report_err, "region1index=%d > 22\n", region1index);
  397. region1index = 22;
  398. }
  399. gr_infos->region1start = bandInfo[sfreq].longIdx[region0index] >> 1;
  400. gr_infos->region2start = bandInfo[sfreq].longIdx[region1index] >> 1;
  401. gr_infos->block_type = 0;
  402. gr_infos->mixed_block_flag = 0;
  403. }
  404. gr_infos->preflag = get1bit(mp);
  405. gr_infos->scalefac_scale = get1bit(mp);
  406. gr_infos->count1table_select = get1bit(mp);
  407. }
  408. }
  409. }
  410. /*
  411. * Side Info for MPEG 2.0 / LSF
  412. */
  413. static void
  414. III_get_side_info_2(PMPSTR mp, int stereo, int ms_stereo, long sfreq, int single)
  415. {
  416. int ch;
  417. int powdiff = (single == 3) ? 4 : 0;
  418. mp->sideinfo.main_data_begin = getbits(mp, 8);
  419. if (stereo == 1)
  420. mp->sideinfo.private_bits = get1bit(mp);
  421. else
  422. mp->sideinfo.private_bits = getbits_fast(mp, 2);
  423. for (ch = 0; ch < stereo; ch++) {
  424. struct gr_info_s *gr_infos = &(mp->sideinfo.ch[ch].gr[0]);
  425. unsigned int qss;
  426. gr_infos->part2_3_length = getbits(mp, 12);
  427. gr_infos->big_values = getbits_fast(mp, 9);
  428. if (gr_infos->big_values > 288) {
  429. lame_report_fnc(mp->report_err, "big_values too large! %i\n", gr_infos->big_values);
  430. gr_infos->big_values = 288;
  431. }
  432. qss = getbits_fast(mp, 8);
  433. gr_infos->pow2gain = gainpow2 + 256 - qss + powdiff;
  434. if (mp->pinfo != NULL) {
  435. mp->pinfo->qss[0][ch] = qss;
  436. }
  437. if (ms_stereo)
  438. gr_infos->pow2gain += 2;
  439. gr_infos->scalefac_compress = getbits(mp, 9);
  440. /* window-switching flag == 1 for block_Type != 0 .. and block-type == 0 -> win-sw-flag = 0 */
  441. if (get1bit(mp)) {
  442. int i;
  443. gr_infos->block_type = getbits_fast(mp, 2);
  444. gr_infos->mixed_block_flag = get1bit(mp);
  445. gr_infos->table_select[0] = getbits_fast(mp, 5);
  446. gr_infos->table_select[1] = getbits_fast(mp, 5);
  447. /*
  448. * table_select[2] not needed, because there is no region2,
  449. * but to satisfy some verifications tools we set it either.
  450. */
  451. gr_infos->table_select[2] = 0;
  452. for (i = 0; i < 3; i++) {
  453. unsigned int sbg = (getbits_fast(mp, 3) << 3);
  454. gr_infos->full_gain[i] = gr_infos->pow2gain + sbg;
  455. if (mp->pinfo != NULL)
  456. mp->pinfo->sub_gain[0][ch][i] = sbg / 8;
  457. }
  458. if (gr_infos->block_type == 0) {
  459. lame_report_fnc(mp->report_err, "Blocktype == 0 and window-switching == 1 not allowed.\n");
  460. /* error seems to be very good recoverable, so don't exit */
  461. /* exit(1); */
  462. }
  463. /* region_count/start parameters are implicit in this case. */
  464. if (gr_infos->block_type == 2) {
  465. if (gr_infos->mixed_block_flag == 0)
  466. gr_infos->region1start = 36 >> 1;
  467. else
  468. gr_infos->region1start = 48 >> 1;
  469. }
  470. else
  471. gr_infos->region1start = 54 >> 1;
  472. if (sfreq == 8)
  473. gr_infos->region1start *= 2;
  474. gr_infos->region2start = 576 >> 1;
  475. }
  476. else {
  477. unsigned int i, r0c, r1c, region0index, region1index;
  478. for (i = 0; i < 3; i++)
  479. gr_infos->table_select[i] = getbits_fast(mp, 5);
  480. r0c = getbits_fast(mp, 4);
  481. r1c = getbits_fast(mp, 3);
  482. region0index = r0c+1;
  483. if (region0index > 22) {
  484. lame_report_fnc(mp->report_err, "region0index=%d > 22\n", region0index);
  485. region0index = 22;
  486. }
  487. region1index = r0c+1 + r1c+1;
  488. if (region1index > 22) {
  489. lame_report_fnc(mp->report_err, "region1index=%d > 22\n", region1index);
  490. region1index = 22;
  491. }
  492. gr_infos->region1start = bandInfo[sfreq].longIdx[region0index] >> 1;
  493. gr_infos->region2start = bandInfo[sfreq].longIdx[region1index] >> 1;
  494. gr_infos->block_type = 0;
  495. gr_infos->mixed_block_flag = 0;
  496. }
  497. gr_infos->scalefac_scale = get1bit(mp);
  498. gr_infos->count1table_select = get1bit(mp);
  499. }
  500. }
  501. /*
  502. * read scalefactors
  503. */
  504. static int
  505. III_get_scale_factors_1(PMPSTR mp, int *scf, struct gr_info_s *gr_infos)
  506. {
  507. static const unsigned char slen[2][16] = {
  508. {0, 0, 0, 0, 3, 1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4},
  509. {0, 1, 2, 3, 0, 1, 2, 3, 1, 2, 3, 1, 2, 3, 2, 3}
  510. };
  511. int numbits;
  512. int num0 = slen[0][gr_infos->scalefac_compress];
  513. int num1 = slen[1][gr_infos->scalefac_compress];
  514. if (gr_infos->block_type == 2) {
  515. int i = 18;
  516. numbits = (num0 + num1) * 18;
  517. if (gr_infos->mixed_block_flag) {
  518. for (i = 8; i; i--)
  519. *scf++ = getbits_fast(mp, num0);
  520. i = 9;
  521. numbits -= num0; /* num0 * 17 + num1 * 18 */
  522. }
  523. for (; i; i--)
  524. *scf++ = getbits_fast(mp, num0);
  525. for (i = 18; i; i--)
  526. *scf++ = getbits_fast(mp, num1);
  527. *scf++ = 0;
  528. *scf++ = 0;
  529. *scf++ = 0; /* short[13][0..2] = 0 */
  530. }
  531. else {
  532. int i;
  533. int scfsi = gr_infos->scfsi;
  534. if (scfsi < 0) { /* scfsi < 0 => granule == 0 */
  535. for (i = 11; i; i--)
  536. *scf++ = getbits_fast(mp, num0);
  537. for (i = 10; i; i--)
  538. *scf++ = getbits_fast(mp, num1);
  539. numbits = (num0 + num1) * 10 + num0;
  540. }
  541. else {
  542. numbits = 0;
  543. if (!(scfsi & 0x8)) {
  544. for (i = 6; i; i--)
  545. *scf++ = getbits_fast(mp, num0);
  546. numbits += num0 * 6;
  547. }
  548. else {
  549. scf += 6;
  550. }
  551. if (!(scfsi & 0x4)) {
  552. for (i = 5; i; i--)
  553. *scf++ = getbits_fast(mp, num0);
  554. numbits += num0 * 5;
  555. }
  556. else {
  557. scf += 5;
  558. }
  559. if (!(scfsi & 0x2)) {
  560. for (i = 5; i; i--)
  561. *scf++ = getbits_fast(mp, num1);
  562. numbits += num1 * 5;
  563. }
  564. else {
  565. scf += 5;
  566. }
  567. if (!(scfsi & 0x1)) {
  568. for (i = 5; i; i--)
  569. *scf++ = getbits_fast(mp, num1);
  570. numbits += num1 * 5;
  571. }
  572. else {
  573. scf += 5;
  574. }
  575. }
  576. *scf++ = 0; /* no l[21] in original sources */
  577. }
  578. return numbits;
  579. }
  580. static int
  581. III_get_scale_factors_2(PMPSTR mp, int *scf, struct gr_info_s *gr_infos, int i_stereo)
  582. {
  583. unsigned char const *pnt;
  584. int i, j;
  585. unsigned int slen;
  586. int n = 0;
  587. int numbits = 0;
  588. /* *INDENT-OFF* */
  589. static const unsigned char stab[3][6][4] = {
  590. { { 6, 5, 5,5 } , { 6, 5, 7,3 } , { 11,10,0,0} ,
  591. { 7, 7, 7,0 } , { 6, 6, 6,3 } , { 8, 8,5,0} } ,
  592. { { 9, 9, 9,9 } , { 9, 9,12,6 } , { 18,18,0,0} ,
  593. {12,12,12,0 } , {12, 9, 9,6 } , { 15,12,9,0} } ,
  594. { { 6, 9, 9,9 } , { 6, 9,12,6 } , { 15,18,0,0} ,
  595. { 6,15,12,0 } , { 6,12, 9,6 } , { 6,18,9,0} } };
  596. /* *INDENT-ON* */
  597. if (i_stereo) /* i_stereo AND second channel -> do_layer3() checks this */
  598. slen = i_slen2[gr_infos->scalefac_compress >> 1];
  599. else
  600. slen = n_slen2[gr_infos->scalefac_compress];
  601. gr_infos->preflag = (slen >> 15) & 0x1;
  602. n = 0;
  603. if (gr_infos->block_type == 2) {
  604. n++;
  605. if (gr_infos->mixed_block_flag)
  606. n++;
  607. }
  608. pnt = (unsigned char const *) stab[n][(slen >> 12) & 0x7];
  609. for (i = 0; i < 4; i++) {
  610. int num = slen & 0x7;
  611. slen >>= 3;
  612. if (num) {
  613. for (j = 0; j < (int) (pnt[i]); j++)
  614. *scf++ = getbits_fast(mp, num);
  615. numbits += pnt[i] * num;
  616. }
  617. else {
  618. for (j = 0; j < (int) (pnt[i]); j++)
  619. *scf++ = 0;
  620. }
  621. }
  622. n = (n << 1) + 1;
  623. for (i = 0; i < n; i++)
  624. *scf++ = 0;
  625. return numbits;
  626. }
  627. /* *INDENT-OFF* */
  628. static const int pretab1 [22] = {0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,2,2,3,3,3,2,0}; /* char enough ? */
  629. static const int pretab2 [22] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
  630. /* *INDENT-ON* */
  631. /*
  632. * don't forget to apply the same changes to III_dequantize_sample_ms() !!!
  633. */
  634. static int
  635. III_dequantize_sample(PMPSTR mp, real xr[SBLIMIT][SSLIMIT], int *scf,
  636. struct gr_info_s *gr_infos, int sfreq, int part2bits)
  637. {
  638. int shift = 1 + gr_infos->scalefac_scale;
  639. real *xrpnt = (real *) xr, xr_value=0;
  640. int l[3], l3;
  641. int part2remain = gr_infos->part2_3_length - part2bits;
  642. int *me;
  643. real const * const xr_endptr = &xr[SBLIMIT-1][SSLIMIT-1];
  644. int isbug = 0;
  645. int bobug = 0;
  646. int bobug_sb = 0, bobug_l3=0;
  647. #define BUFFER_OVERFLOW_BUG() if(!bobug){bobug=1;bobug_sb=cb;bobug_l3=l3;}else
  648. /* lame_report_fnc(mp->report_dbg,"part2remain = %d, gr_infos->part2_3_length = %d, part2bits = %d\n",
  649. part2remain, gr_infos->part2_3_length, part2bits); */
  650. {
  651. int i;
  652. for (i = (&xr[SBLIMIT][0] - xrpnt) >> 1; i > 0; i--) {
  653. *xrpnt++ = 0.0;
  654. *xrpnt++ = 0.0;
  655. }
  656. xrpnt = (real *) xr;
  657. }
  658. {
  659. int bv = gr_infos->big_values;
  660. int region1 = gr_infos->region1start;
  661. int region2 = gr_infos->region2start;
  662. l3 = ((576 >> 1) - bv) >> 1;
  663. /*
  664. * we may lose the 'odd' bit here !!
  665. * check this later again
  666. */
  667. if (bv <= region1) {
  668. l[0] = bv;
  669. l[1] = 0;
  670. l[2] = 0;
  671. }
  672. else {
  673. l[0] = region1;
  674. if (bv <= region2) {
  675. l[1] = bv - l[0];
  676. l[2] = 0;
  677. }
  678. else {
  679. l[1] = region2 - l[0];
  680. l[2] = bv - region2;
  681. }
  682. }
  683. }
  684. /* MDH crash fix */
  685. {
  686. int i;
  687. for (i = 0; i < 3; i++) {
  688. if (l[i] < 0) {
  689. lame_report_fnc(mp->report_err, "hip: Bogus region length (%d)\n", l[i]);
  690. l[i] = 0;
  691. }
  692. }
  693. }
  694. /* end MDH crash fix */
  695. if (gr_infos->block_type == 2) {
  696. /*
  697. * decoding with short or mixed mode BandIndex table
  698. */
  699. int i, max[4];
  700. int step = 0, lwin = 0, cb = 0;
  701. real v = 0.0;
  702. int *m, mc;
  703. if (gr_infos->mixed_block_flag) {
  704. max[3] = -1;
  705. max[0] = max[1] = max[2] = 2;
  706. m = map[sfreq][0];
  707. me = mapend[sfreq][0];
  708. }
  709. else {
  710. max[0] = max[1] = max[2] = max[3] = -1;
  711. /* max[3] not really needed in this case */
  712. m = map[sfreq][1];
  713. me = mapend[sfreq][1];
  714. }
  715. mc = 0;
  716. for (i = 0; i < 2; i++) {
  717. int lp = l[i];
  718. struct newhuff const *h = (struct newhuff const *) (ht + gr_infos->table_select[i]);
  719. for (; lp; lp--, mc--) {
  720. int x, y;
  721. if ((!mc)) {
  722. mc = *m++;
  723. xrpnt = ((real *) xr) + (*m++);
  724. lwin = *m++;
  725. cb = *m++;
  726. if (lwin == 3) {
  727. v = get_gain(gr_infos->pow2gain, (*scf++) << shift, &isbug);
  728. step = 1;
  729. }
  730. else {
  731. v = get_gain(gr_infos->full_gain[lwin], (*scf++) << shift, &isbug);
  732. step = 3;
  733. }
  734. }
  735. {
  736. short const *val = (short const *) h->table;
  737. while ((y = *val++) < 0) {
  738. if (get1bit(mp))
  739. val -= y;
  740. part2remain--;
  741. }
  742. x = y >> 4;
  743. y &= 0xf;
  744. }
  745. if (x == 15) {
  746. max[lwin] = cb;
  747. part2remain -= h->linbits + 1;
  748. x += getbits(mp, (int) h->linbits);
  749. if (get1bit(mp))
  750. xr_value = -ispow[x] * v;
  751. else
  752. xr_value = ispow[x] * v;
  753. }
  754. else if (x) {
  755. max[lwin] = cb;
  756. if (get1bit(mp))
  757. xr_value = -ispow[x] * v;
  758. else
  759. xr_value = ispow[x] * v;
  760. part2remain--;
  761. }
  762. else
  763. xr_value = 0.0;
  764. if (xrpnt <= xr_endptr)
  765. *xrpnt = xr_value;
  766. else
  767. BUFFER_OVERFLOW_BUG();
  768. xrpnt += step;
  769. if (y == 15) {
  770. max[lwin] = cb;
  771. part2remain -= h->linbits + 1;
  772. y += getbits(mp, (int) h->linbits);
  773. if (get1bit(mp))
  774. xr_value = -ispow[y] * v;
  775. else
  776. xr_value = ispow[y] * v;
  777. }
  778. else if (y) {
  779. max[lwin] = cb;
  780. if (get1bit(mp))
  781. xr_value = -ispow[y] * v;
  782. else
  783. xr_value = ispow[y] * v;
  784. part2remain--;
  785. }
  786. else
  787. xr_value = 0.0;
  788. if (xrpnt <= xr_endptr)
  789. *xrpnt = xr_value;
  790. else
  791. BUFFER_OVERFLOW_BUG();
  792. xrpnt += step;
  793. }
  794. }
  795. for (; (l3 > 0) && (part2remain > 0); l3--) {
  796. struct newhuff const *h = (struct newhuff const *) (htc + gr_infos->count1table_select);
  797. short const *val = (short const *) h->table;
  798. short a;
  799. while ((a = *val++) < 0) {
  800. part2remain--;
  801. if (part2remain < 0) {
  802. part2remain++;
  803. a = 0;
  804. break;
  805. }
  806. if (get1bit(mp))
  807. val -= a;
  808. }
  809. for (i = 0; i < 4; i++) {
  810. if (!(i & 1)) {
  811. if (!mc) {
  812. mc = *m++;
  813. xrpnt = ((real *) xr) + (*m++);
  814. lwin = *m++;
  815. cb = *m++;
  816. if (lwin == 3) {
  817. v = get_gain(gr_infos->pow2gain, (*scf++) << shift, &isbug);
  818. step = 1;
  819. }
  820. else {
  821. v = get_gain(gr_infos->full_gain[lwin], (*scf++) << shift, &isbug);
  822. step = 3;
  823. }
  824. }
  825. mc--;
  826. }
  827. if ((a & (0x8 >> i))) {
  828. max[lwin] = cb;
  829. part2remain--;
  830. if (part2remain < 0) {
  831. part2remain++;
  832. break;
  833. }
  834. if (get1bit(mp))
  835. xr_value = -v;
  836. else
  837. xr_value = v;
  838. }
  839. else
  840. xr_value = 0.0;
  841. if (xrpnt <= xr_endptr)
  842. *xrpnt = xr_value;
  843. else
  844. BUFFER_OVERFLOW_BUG();
  845. xrpnt += step;
  846. }
  847. }
  848. while (m < me) {
  849. if (!mc) {
  850. mc = *m++;
  851. xrpnt = ((real *) xr) + *m++;
  852. if ((*m++) == 3)
  853. step = 1;
  854. else
  855. step = 3;
  856. m++; /* cb */
  857. }
  858. mc--;
  859. if (xrpnt <= xr_endptr)
  860. *xrpnt = 0.0;
  861. else
  862. BUFFER_OVERFLOW_BUG();
  863. xrpnt += step;
  864. if (xrpnt <= xr_endptr)
  865. *xrpnt = 0.0;
  866. else
  867. BUFFER_OVERFLOW_BUG();
  868. xrpnt += step;
  869. /* we could add a little opt. here:
  870. * if we finished a band for window 3 or a long band
  871. * further bands could copied in a simple loop without a
  872. * special 'map' decoding
  873. */
  874. }
  875. gr_infos->maxband[0] = max[0] + 1;
  876. gr_infos->maxband[1] = max[1] + 1;
  877. gr_infos->maxband[2] = max[2] + 1;
  878. gr_infos->maxbandl = max[3] + 1;
  879. {
  880. int rmax = max[0] > max[1] ? max[0] : max[1];
  881. rmax = (rmax > max[2] ? rmax : max[2]) + 1;
  882. gr_infos->maxb = rmax ? shortLimit[sfreq][rmax] : longLimit[sfreq][max[3] + 1];
  883. }
  884. }
  885. else {
  886. /*
  887. * decoding with 'long' BandIndex table (block_type != 2)
  888. */
  889. int const *pretab = (int const *) (gr_infos->preflag ? pretab1 : pretab2);
  890. int i, max = -1;
  891. int cb = 0;
  892. int *m = map[sfreq][2];
  893. real v = 0.0;
  894. int mc = 0;
  895. /*
  896. * long hash table values
  897. */
  898. for (i = 0; i < 3; i++) {
  899. int lp = l[i];
  900. struct newhuff const *h = (struct newhuff const *) (ht + gr_infos->table_select[i]);
  901. for (; lp; lp--, mc--) {
  902. int x, y;
  903. if (!mc) {
  904. mc = *m++;
  905. v = get_gain(gr_infos->pow2gain, ((*scf++) + (*pretab++)) << shift, &isbug);
  906. cb = *m++;
  907. }
  908. {
  909. short const *val = (short const *) h->table;
  910. while ((y = *val++) < 0) {
  911. if (get1bit(mp))
  912. val -= y;
  913. part2remain--;
  914. }
  915. x = y >> 4;
  916. y &= 0xf;
  917. }
  918. if (x == 15) {
  919. max = cb;
  920. part2remain -= h->linbits + 1;
  921. x += getbits(mp, (int) h->linbits);
  922. if (get1bit(mp))
  923. xr_value = -ispow[x] * v;
  924. else
  925. xr_value = ispow[x] * v;
  926. }
  927. else if (x) {
  928. max = cb;
  929. if (get1bit(mp))
  930. xr_value = -ispow[x] * v;
  931. else
  932. xr_value = ispow[x] * v;
  933. part2remain--;
  934. }
  935. else
  936. xr_value = 0.0;
  937. if (xrpnt <= xr_endptr)
  938. *xrpnt++ = xr_value;
  939. else
  940. BUFFER_OVERFLOW_BUG();
  941. if (y == 15) {
  942. max = cb;
  943. part2remain -= h->linbits + 1;
  944. y += getbits(mp, (int) h->linbits);
  945. if (get1bit(mp))
  946. xr_value = -ispow[y] * v;
  947. else
  948. xr_value = ispow[y] * v;
  949. }
  950. else if (y) {
  951. max = cb;
  952. if (get1bit(mp))
  953. xr_value = -ispow[y] * v;
  954. else
  955. xr_value = ispow[y] * v;
  956. part2remain--;
  957. }
  958. else
  959. xr_value = 0.0;
  960. if (xrpnt <= xr_endptr)
  961. *xrpnt++ = xr_value;
  962. else
  963. BUFFER_OVERFLOW_BUG();
  964. }
  965. }
  966. /*
  967. * short (count1table) values
  968. */
  969. for (; l3 && (part2remain > 0); l3--) {
  970. struct newhuff const *h = (struct newhuff const *) (htc + gr_infos->count1table_select);
  971. short const *val = (short const *) h->table;
  972. short a;
  973. while ((a = *val++) < 0) {
  974. part2remain--;
  975. if (part2remain < 0) {
  976. part2remain++;
  977. a = 0;
  978. break;
  979. }
  980. if (get1bit(mp))
  981. val -= a;
  982. }
  983. for (i = 0; i < 4; i++) {
  984. if (!(i & 1)) {
  985. if (!mc) {
  986. mc = *m++;
  987. cb = *m++;
  988. v = get_gain(gr_infos->pow2gain, ((*scf++) + (*pretab++)) << shift, &isbug);
  989. }
  990. mc--;
  991. }
  992. if ((a & (0x8 >> i))) {
  993. max = cb;
  994. part2remain--;
  995. if (part2remain < 0) {
  996. part2remain++;
  997. break;
  998. }
  999. if (get1bit(mp))
  1000. xr_value = -v;
  1001. else
  1002. xr_value = v;
  1003. }
  1004. else
  1005. xr_value = 0.0;
  1006. if (xrpnt <= xr_endptr)
  1007. *xrpnt++ = xr_value;
  1008. else
  1009. BUFFER_OVERFLOW_BUG();
  1010. }
  1011. }
  1012. /*
  1013. * zero part
  1014. */
  1015. while (xrpnt <= xr_endptr)
  1016. *xrpnt++ = 0.0;
  1017. gr_infos->maxbandl = max + 1;
  1018. gr_infos->maxb = longLimit[sfreq][gr_infos->maxbandl];
  1019. }
  1020. #undef BUFFER_OVERFLOW_BUG
  1021. if (bobug) {
  1022. /* well, there was a bug report, where this happened!
  1023. The problem was, that mixed blocks summed up to over 576,
  1024. because of a wrong long/short switching index.
  1025. It's likely, that the buffer overflow is fixed now, after correcting mixed block map.
  1026. */
  1027. lame_report_fnc
  1028. (mp->report_err
  1029. ,"hip: OOPS, part2remain=%d l3=%d cb=%d bv=%d region1=%d region2=%d b-type=%d mixed=%d\n"
  1030. ,part2remain
  1031. ,bobug_l3
  1032. ,bobug_sb
  1033. ,gr_infos->big_values
  1034. ,gr_infos->region1start
  1035. ,gr_infos->region2start
  1036. ,gr_infos->block_type
  1037. ,gr_infos->mixed_block_flag
  1038. );
  1039. }
  1040. if (isbug) {
  1041. /* there is a bug report, where there is trouble with IS coded short block gain.
  1042. Is intensity stereo coding implementation correct? Likely not.
  1043. */
  1044. int i_stereo = 0;
  1045. if (mp->fr.mode == MPG_MD_JOINT_STEREO) {
  1046. i_stereo = mp->fr.mode_ext & 0x1;
  1047. }
  1048. lame_report_fnc
  1049. (mp->report_err
  1050. ,"hip: OOPS, 'gainpow2' buffer overflow lsf=%d i-stereo=%d b-type=%d mixed=%d\n"
  1051. ,mp->fr.lsf
  1052. ,i_stereo
  1053. ,gr_infos->block_type
  1054. ,gr_infos->mixed_block_flag
  1055. );
  1056. }
  1057. while (part2remain > 16) {
  1058. getbits(mp, 16); /* Dismiss stuffing Bits */
  1059. part2remain -= 16;
  1060. }
  1061. if (part2remain > 0)
  1062. getbits(mp, part2remain);
  1063. else if (part2remain < 0) {
  1064. lame_report_fnc(mp->report_err, "hip: Can't rewind stream by %d bits!\n", -part2remain);
  1065. return 1; /* -> error */
  1066. }
  1067. return 0;
  1068. }
  1069. /* intensity position, transmitted via a scalefactor value, allowed range is 0 - 15 */
  1070. static
  1071. int scalefac_to_is_pos(int sf)
  1072. {
  1073. if (0 <= sf && sf <= 15)
  1074. return sf;
  1075. return (sf < 0 ? 0 : 15);
  1076. }
  1077. /*
  1078. * III_stereo: calculate real channel values for Joint-I-Stereo-mode
  1079. */
  1080. static void
  1081. III_i_stereo(real xr_buf[2][SBLIMIT][SSLIMIT], int *scalefac,
  1082. struct gr_info_s *gr_infos, int sfreq, int ms_stereo, int lsf)
  1083. {
  1084. real(*xr)[SBLIMIT * SSLIMIT] = (real(*)[SBLIMIT * SSLIMIT]) xr_buf;
  1085. struct bandInfoStruct const *bi = (struct bandInfoStruct const *) &bandInfo[sfreq];
  1086. real *tabl1, *tabl2;
  1087. if (lsf) {
  1088. int p = gr_infos->scalefac_compress & 0x1;
  1089. if (ms_stereo) {
  1090. tabl1 = pow1_2[p];
  1091. tabl2 = pow2_2[p];
  1092. }
  1093. else {
  1094. tabl1 = pow1_1[p];
  1095. tabl2 = pow2_1[p];
  1096. }
  1097. }
  1098. else {
  1099. if (ms_stereo) {
  1100. tabl1 = tan1_2;
  1101. tabl2 = tan2_2;
  1102. }
  1103. else {
  1104. tabl1 = tan1_1;
  1105. tabl2 = tan2_1;
  1106. }
  1107. }
  1108. if (gr_infos->block_type == 2) {
  1109. int lwin, do_l = 0;
  1110. if (gr_infos->mixed_block_flag)
  1111. do_l = 1;
  1112. for (lwin = 0; lwin < 3; lwin++) { /* process each window */
  1113. /* get first band with zero values */
  1114. int is_p, sb, idx, sfb = gr_infos->maxband[lwin]; /* sfb is minimal 3 for mixed mode */
  1115. if (sfb > 3)
  1116. do_l = 0;
  1117. for (; sfb < 12; sfb++) {
  1118. is_p = scalefac[sfb * 3 + lwin - gr_infos->mixed_block_flag]; /* scale: 0-15 */
  1119. is_p = scalefac_to_is_pos(is_p);
  1120. if (is_p != 7) {
  1121. real t1, t2;
  1122. sb = bi->shortDiff[sfb];
  1123. idx = bi->shortIdx[sfb] + lwin;
  1124. t1 = tabl1[is_p];
  1125. t2 = tabl2[is_p];
  1126. for (; sb > 0; sb--, idx += 3) {
  1127. real v = xr[0][idx];
  1128. xr[0][idx] = v * t1;
  1129. xr[1][idx] = v * t2;
  1130. }
  1131. }
  1132. }
  1133. #if 1
  1134. /* in the original: copy 10 to 11 , here: copy 11 to 12
  1135. maybe still wrong??? (copy 12 to 13?) */
  1136. is_p = scalefac[11 * 3 + lwin - gr_infos->mixed_block_flag]; /* scale: 0-15 */
  1137. sb = bi->shortDiff[12];
  1138. idx = bi->shortIdx[12] + lwin;
  1139. #else
  1140. is_p = scalefac[10 * 3 + lwin - gr_infos->mixed_block_flag]; /* scale: 0-15 */
  1141. sb = bi->shortDiff[11];
  1142. idx = bi->shortIdx[11] + lwin;
  1143. #endif
  1144. is_p = scalefac_to_is_pos(is_p);
  1145. if (is_p != 7) {
  1146. real t1, t2;
  1147. t1 = tabl1[is_p];
  1148. t2 = tabl2[is_p];
  1149. for (; sb > 0; sb--, idx += 3) {
  1150. real v = xr[0][idx];
  1151. xr[0][idx] = v * t1;
  1152. xr[1][idx] = v * t2;
  1153. }
  1154. }
  1155. } /* end for(lwin; .. ; . ) */
  1156. if (do_l) {
  1157. /* also check l-part, if ALL bands in the three windows are 'empty'
  1158. * and mode = mixed_mode
  1159. */
  1160. int sfb = gr_infos->maxbandl;
  1161. int idx = bi->longIdx[sfb];
  1162. for (; sfb < 8; sfb++) {
  1163. int sb = bi->longDiff[sfb];
  1164. int is_p = scalefac[sfb]; /* scale: 0-15 */
  1165. is_p = scalefac_to_is_pos(is_p);
  1166. if (is_p != 7) {
  1167. real t1, t2;
  1168. t1 = tabl1[is_p];
  1169. t2 = tabl2[is_p];
  1170. for (; sb > 0; sb--, idx++) {
  1171. real v = xr[0][idx];
  1172. xr[0][idx] = v * t1;
  1173. xr[1][idx] = v * t2;
  1174. }
  1175. }
  1176. else
  1177. idx += sb;
  1178. }
  1179. }
  1180. }
  1181. else { /* ((gr_infos->block_type != 2)) */
  1182. int sfb = gr_infos->maxbandl;
  1183. int is_p, idx = bi->longIdx[sfb];
  1184. for (; sfb < 21; sfb++) {
  1185. int sb = bi->longDiff[sfb];
  1186. is_p = scalefac[sfb]; /* scale: 0-15 */
  1187. is_p = scalefac_to_is_pos(is_p);
  1188. if (is_p != 7) {
  1189. real t1, t2;
  1190. t1 = tabl1[is_p];
  1191. t2 = tabl2[is_p];
  1192. for (; sb > 0; sb--, idx++) {
  1193. real v = xr[0][idx];
  1194. xr[0][idx] = v * t1;
  1195. xr[1][idx] = v * t2;
  1196. }
  1197. }
  1198. else
  1199. idx += sb;
  1200. }
  1201. is_p = scalefac[20]; /* copy l-band 20 to l-band 21 */
  1202. is_p = scalefac_to_is_pos(is_p);
  1203. idx = bi->longIdx[21];
  1204. if (is_p != 7) {
  1205. int sb;
  1206. real t1 = tabl1[is_p], t2 = tabl2[is_p];
  1207. for (sb = bi->longDiff[21]; sb > 0; sb--, idx++) {
  1208. real v = xr[0][idx];
  1209. xr[0][idx] = v * t1;
  1210. xr[1][idx] = v * t2;
  1211. }
  1212. }
  1213. } /* ... */
  1214. }
  1215. static void
  1216. III_antialias(real xr[SBLIMIT][SSLIMIT], struct gr_info_s *gr_infos)
  1217. {
  1218. int sblim;
  1219. if (gr_infos->block_type == 2) {
  1220. if (!gr_infos->mixed_block_flag)
  1221. return;
  1222. sblim = 1;
  1223. }
  1224. else {
  1225. sblim = gr_infos->maxb - 1;
  1226. }
  1227. /* 31 alias-reduction operations between each pair of sub-bands */
  1228. /* with 8 butterflies between each pair */
  1229. {
  1230. int sb;
  1231. real *xr1 = (real *) xr[1];
  1232. for (sb = sblim; sb; sb--, xr1 += 10) {
  1233. int ss;
  1234. real *cs = aa_cs, *ca = aa_ca;
  1235. real *xr2 = xr1;
  1236. for (ss = 7; ss >= 0; ss--) { /* upper and lower butterfly inputs */
  1237. real bu = *--xr2, bd = *xr1;
  1238. *xr2 = (bu * (*cs)) - (bd * (*ca));
  1239. *xr1++ = (bd * (*cs++)) + (bu * (*ca++));
  1240. }
  1241. }
  1242. }
  1243. }
  1244. /* *INDENT-OFF* */
  1245. /*
  1246. DCT insipired by Jeff Tsay's DCT from the maplay package
  1247. this is an optimized version with manual unroll.
  1248. References:
  1249. [1] S. Winograd: "On Computing the Discrete Fourier Transform",
  1250. Mathematics of Computation, Volume 32, Number 141, January 1978,
  1251. Pages 175-199
  1252. */
  1253. static void dct36(real *inbuf,real *o1,real *o2,real *wintab,real *tsbuf)
  1254. {
  1255. {
  1256. real *in = inbuf;
  1257. in[17]+=in[16]; in[16]+=in[15]; in[15]+=in[14];
  1258. in[14]+=in[13]; in[13]+=in[12]; in[12]+=in[11];
  1259. in[11]+=in[10]; in[10]+=in[9]; in[9] +=in[8];
  1260. in[8] +=in[7]; in[7] +=in[6]; in[6] +=in[5];
  1261. in[5] +=in[4]; in[4] +=in[3]; in[3] +=in[2];
  1262. in[2] +=in[1]; in[1] +=in[0];
  1263. in[17]+=in[15]; in[15]+=in[13]; in[13]+=in[11]; in[11]+=in[9];
  1264. in[9] +=in[7]; in[7] +=in[5]; in[5] +=in[3]; in[3] +=in[1];
  1265. {
  1266. #define MACRO0(v) { \
  1267. real tmp; \
  1268. out2[9+(v)] = (tmp = sum0 + sum1) * w[27+(v)]; \
  1269. out2[8-(v)] = tmp * w[26-(v)]; } \
  1270. sum0 -= sum1; \
  1271. ts[SBLIMIT*(8-(v))] = out1[8-(v)] + sum0 * w[8-(v)]; \
  1272. ts[SBLIMIT*(9+(v))] = out1[9+(v)] + sum0 * w[9+(v)];
  1273. #define MACRO1(v) { \
  1274. real sum0,sum1; \
  1275. sum0 = tmp1a + tmp2a; \
  1276. sum1 = (tmp1b + tmp2b) * tfcos36[(v)]; \
  1277. MACRO0(v); }
  1278. #define MACRO2(v) { \
  1279. real sum0,sum1; \
  1280. sum0 = tmp2a - tmp1a; \
  1281. sum1 = (tmp2b - tmp1b) * tfcos36[(v)]; \
  1282. MACRO0(v); }
  1283. const real *c = COS9;
  1284. real *out2 = o2;
  1285. real *w = wintab;
  1286. real *out1 = o1;
  1287. real *ts = tsbuf;
  1288. real ta33,ta66,tb33,tb66;
  1289. ta33 = in[2*3+0] * c[3];
  1290. ta66 = in[2*6+0] * c[6];
  1291. tb33 = in[2*3+1] * c[3];
  1292. tb66 = in[2*6+1] * c[6];
  1293. {
  1294. real tmp1a,tmp2a,tmp1b,tmp2b;
  1295. tmp1a = in[2*1+0] * c[1] + ta33 + in[2*5+0] * c[5] + in[2*7+0] * c[7];
  1296. tmp1b = in[2*1+1] * c[1] + tb33 + in[2*5+1] * c[5] + in[2*7+1] * c[7];
  1297. tmp2a = in[2*0+0] + in[2*2+0] * c[2] + in[2*4+0] * c[4] + ta66 + in[2*8+0] * c[8];
  1298. tmp2b = in[2*0+1] + in[2*2+1] * c[2] + in[2*4+1] * c[4] + tb66 + in[2*8+1] * c[8];
  1299. MACRO1(0);
  1300. MACRO2(8);
  1301. }
  1302. {
  1303. real tmp1a,tmp2a,tmp1b,tmp2b;
  1304. tmp1a = ( in[2*1+0] - in[2*5+0] - in[2*7+0] ) * c[3];
  1305. tmp1b = ( in[2*1+1] - in[2*5+1] - in[2*7+1] ) * c[3];
  1306. tmp2a = ( in[2*2+0] - in[2*4+0] - in[2*8+0] ) * c[6] - in[2*6+0] + in[2*0+0];
  1307. tmp2b = ( in[2*2+1] - in[2*4+1] - in[2*8+1] ) * c[6] - in[2*6+1] + in[2*0+1];
  1308. MACRO1(1);
  1309. MACRO2(7);
  1310. }
  1311. {
  1312. real tmp1a,tmp2a,tmp1b,tmp2b;
  1313. tmp1a = in[2*1+0] * c[5] - ta33 - in[2*5+0] * c[7] + in[2*7+0] * c[1];
  1314. tmp1b = in[2*1+1] * c[5] - tb33 - in[2*5+1] * c[7] + in[2*7+1] * c[1];
  1315. tmp2a = in[2*0+0] - in[2*2+0] * c[8] - in[2*4+0] * c[2] + ta66 + in[2*8+0] * c[4];
  1316. tmp2b = in[2*0+1] - in[2*2+1] * c[8] - in[2*4+1] * c[2] + tb66 + in[2*8+1] * c[4];
  1317. MACRO1(2);
  1318. MACRO2(6);
  1319. }
  1320. {
  1321. real tmp1a,tmp2a,tmp1b,tmp2b;
  1322. tmp1a = in[2*1+0] * c[7] - ta33 + in[2*5+0] * c[1] - in[2*7+0] * c[5];
  1323. tmp1b = in[2*1+1] * c[7] - tb33 + in[2*5+1] * c[1] - in[2*7+1] * c[5];
  1324. tmp2a = in[2*0+0] - in[2*2+0] * c[4] + in[2*4+0] * c[8] + ta66 - in[2*8+0] * c[2];
  1325. tmp2b = in[2*0+1] - in[2*2+1] * c[4] + in[2*4+1] * c[8] + tb66 - in[2*8+1] * c[2];
  1326. MACRO1(3);
  1327. MACRO2(5);
  1328. }
  1329. {
  1330. real sum0,sum1;
  1331. sum0 = in[2*0+0] - in[2*2+0] + in[2*4+0] - in[2*6+0] + in[2*8+0];
  1332. sum1 = (in[2*0+1] - in[2*2+1] + in[2*4+1] - in[2*6+1] + in[2*8+1] ) * tfcos36[4];
  1333. MACRO0(4);
  1334. }
  1335. }
  1336. }
  1337. }
  1338. /*
  1339. * new DCT12
  1340. */
  1341. static void dct12(real *in,real *rawout1,real *rawout2,real *wi,real *ts)
  1342. {
  1343. #define DCT12_PART1 \
  1344. in5 = in[5*3]; \
  1345. in5 += (in4 = in[4*3]); \
  1346. in4 += (in3 = in[3*3]); \
  1347. in3 += (in2 = in[2*3]); \
  1348. in2 += (in1 = in[1*3]); \
  1349. in1 += (in0 = in[0*3]); \
  1350. \
  1351. in5 += in3; in3 += in1; \
  1352. \
  1353. in2 *= COS6_1; \
  1354. in3 *= COS6_1; \
  1355. #define DCT12_PART2 \
  1356. in0 += in4 * COS6_2; \
  1357. \
  1358. in4 = in0 + in2; \
  1359. in0 -= in2; \
  1360. \
  1361. in1 += in5 * COS6_2; \
  1362. \
  1363. in5 = (in1 + in3) * tfcos12[0]; \
  1364. in1 = (in1 - in3) * tfcos12[2]; \
  1365. \
  1366. in3 = in4 + in5; \
  1367. in4 -= in5; \
  1368. \
  1369. in2 = in0 + in1; \
  1370. in0 -= in1;
  1371. {
  1372. real in0,in1,in2,in3,in4,in5;
  1373. real *out1 = rawout1;
  1374. ts[SBLIMIT*0] = out1[0]; ts[SBLIMIT*1] = out1[1]; ts[SBLIMIT*2] = out1[2];
  1375. ts[SBLIMIT*3] = out1[3]; ts[SBLIMIT*4] = out1[4]; ts[SBLIMIT*5] = out1[5];
  1376. DCT12_PART1
  1377. {
  1378. real tmp0,tmp1 = (in0 - in4);
  1379. {
  1380. real tmp2 = (in1 - in5) * tfcos12[1];
  1381. tmp0 = tmp1 + tmp2;
  1382. tmp1 -= tmp2;
  1383. }
  1384. ts[(17-1)*SBLIMIT] = out1[17-1] + tmp0 * wi[11-1];
  1385. ts[(12+1)*SBLIMIT] = out1[12+1] + tmp0 * wi[6+1];
  1386. ts[(6 +1)*SBLIMIT] = out1[6 +1] + tmp1 * wi[1];
  1387. ts[(11-1)*SBLIMIT] = out1[11-1] + tmp1 * wi[5-1];
  1388. }
  1389. DCT12_PART2
  1390. ts[(17-0)*SBLIMIT] = out1[17-0] + in2 * wi[11-0];
  1391. ts[(12+0)*SBLIMIT] = out1[12+0] + in2 * wi[6+0];
  1392. ts[(12+2)*SBLIMIT] = out1[12+2] + in3 * wi[6+2];
  1393. ts[(17-2)*SBLIMIT] = out1[17-2] + in3 * wi[11-2];
  1394. ts[(6+0)*SBLIMIT] = out1[6+0] + in0 * wi[0];
  1395. ts[(11-0)*SBLIMIT] = out1[11-0] + in0 * wi[5-0];
  1396. ts[(6+2)*SBLIMIT] = out1[6+2] + in4 * wi[2];
  1397. ts[(11-2)*SBLIMIT] = out1[11-2] + in4 * wi[5-2];
  1398. }
  1399. in++;
  1400. {
  1401. real in0,in1,in2,in3,in4,in5;
  1402. real *out2 = rawout2;
  1403. DCT12_PART1
  1404. {
  1405. real tmp0,tmp1 = (in0 - in4);
  1406. {
  1407. real tmp2 = (in1 - in5) * tfcos12[1];
  1408. tmp0 = tmp1 + tmp2;
  1409. tmp1 -= tmp2;
  1410. }
  1411. out2[5-1] = tmp0 * wi[11-1];
  1412. out2[0+1] = tmp0 * wi[6+1];
  1413. ts[(12+1)*SBLIMIT] += tmp1 * wi[1];
  1414. ts[(17-1)*SBLIMIT] += tmp1 * wi[5-1];
  1415. }
  1416. DCT12_PART2
  1417. out2[5-0] = in2 * wi[11-0];
  1418. out2[0+0] = in2 * wi[6+0];
  1419. out2[0+2] = in3 * wi[6+2];
  1420. out2[5-2] = in3 * wi[11-2];
  1421. ts[(12+0)*SBLIMIT] += in0 * wi[0];
  1422. ts[(17-0)*SBLIMIT] += in0 * wi[5-0];
  1423. ts[(12+2)*SBLIMIT] += in4 * wi[2];
  1424. ts[(17-2)*SBLIMIT] += in4 * wi[5-2];
  1425. }
  1426. in++;
  1427. {
  1428. real in0,in1,in2,in3,in4,in5;
  1429. real *out2 = rawout2;
  1430. out2[12]=out2[13]=out2[14]=out2[15]=out2[16]=out2[17]=0.0;
  1431. DCT12_PART1
  1432. {
  1433. real tmp0,tmp1 = (in0 - in4);
  1434. {
  1435. real tmp2 = (in1 - in5) * tfcos12[1];
  1436. tmp0 = tmp1 + tmp2;
  1437. tmp1 -= tmp2;
  1438. }
  1439. out2[11-1] = tmp0 * wi[11-1];
  1440. out2[6 +1] = tmp0 * wi[6+1];
  1441. out2[0+1] += tmp1 * wi[1];
  1442. out2[5-1] += tmp1 * wi[5-1];
  1443. }
  1444. DCT12_PART2
  1445. out2[11-0] = in2 * wi[11-0];
  1446. out2[6 +0] = in2 * wi[6+0];
  1447. out2[6 +2] = in3 * wi[6+2];
  1448. out2[11-2] = in3 * wi[11-2];
  1449. out2[0+0] += in0 * wi[0];
  1450. out2[5-0] += in0 * wi[5-0];
  1451. out2[0+2] += in4 * wi[2];
  1452. out2[5-2] += in4 * wi[5-2];
  1453. }
  1454. }
  1455. /* *INDENT-ON* */
  1456. /*
  1457. * III_hybrid
  1458. */
  1459. static void
  1460. III_hybrid(PMPSTR mp, real fsIn[SBLIMIT][SSLIMIT], real tsOut[SSLIMIT][SBLIMIT],
  1461. int ch, struct gr_info_s *gr_infos)
  1462. {
  1463. real *tspnt = (real *) tsOut;
  1464. real(*block)[2][SBLIMIT * SSLIMIT] = mp->hybrid_block;
  1465. int *blc = mp->hybrid_blc;
  1466. real *rawout1, *rawout2;
  1467. int bt;
  1468. int sb = 0;
  1469. {
  1470. int b = blc[ch];
  1471. rawout1 = block[b][ch];
  1472. b = -b + 1;
  1473. rawout2 = block[b][ch];
  1474. blc[ch] = b;
  1475. }
  1476. if (gr_infos->mixed_block_flag) {
  1477. sb = 2;
  1478. dct36(fsIn[0], rawout1, rawout2, win[0], tspnt);
  1479. dct36(fsIn[1], rawout1 + 18, rawout2 + 18, win1[0], tspnt + 1);
  1480. rawout1 += 36;
  1481. rawout2 += 36;
  1482. tspnt += 2;
  1483. }
  1484. bt = gr_infos->block_type;
  1485. if (bt == 2) {
  1486. for (; sb < (int) gr_infos->maxb; sb += 2, tspnt += 2, rawout1 += 36, rawout2 += 36) {
  1487. dct12(fsIn[sb], rawout1, rawout2, win[2], tspnt);
  1488. dct12(fsIn[sb + 1], rawout1 + 18, rawout2 + 18, win1[2], tspnt + 1);
  1489. }
  1490. }
  1491. else {
  1492. for (; sb < (int) gr_infos->maxb; sb += 2, tspnt += 2, rawout1 += 36, rawout2 += 36) {
  1493. dct36(fsIn[sb], rawout1, rawout2, win[bt], tspnt);
  1494. dct36(fsIn[sb + 1], rawout1 + 18, rawout2 + 18, win1[bt], tspnt + 1);
  1495. }
  1496. }
  1497. for (; sb < SBLIMIT; sb++, tspnt++) {
  1498. int i;
  1499. for (i = 0; i < SSLIMIT; i++) {
  1500. tspnt[i * SBLIMIT] = *rawout1++;
  1501. *rawout2++ = 0.0;
  1502. }
  1503. }
  1504. }
  1505. /*
  1506. * main layer3 handler
  1507. */
  1508. int
  1509. layer3_audiodata_precedesframes(PMPSTR mp)
  1510. {
  1511. int audioDataInFrame;
  1512. int framesToBacktrack;
  1513. /* specific to Layer 3, since Layer 1 & 2 the audio data starts at the frame that describes it. */
  1514. /* determine how many bytes and therefore bitstream frames the audio data precedes it's matching frame */
  1515. /* lame_report_fnc(mp->report_err, "hip: main_data_begin = %d, mp->bsize %d, mp->fsizeold %d, mp->ssize %d\n",
  1516. sideinfo.main_data_begin, mp->bsize, mp->fsizeold, mp->ssize); */
  1517. /* compute the number of frames to backtrack, 4 for the header, ssize already holds the CRC */
  1518. /* TODO Erroneously assumes current frame is same as previous frame. */
  1519. audioDataInFrame = mp->bsize - 4 - mp->ssize;
  1520. framesToBacktrack = (mp->sideinfo.main_data_begin + audioDataInFrame - 1) / audioDataInFrame;
  1521. /* lame_report_fnc(mp->report_err, "hip: audioDataInFrame %d framesToBacktrack %d\n", audioDataInFrame, framesToBacktrack); */
  1522. return framesToBacktrack;
  1523. }
  1524. int
  1525. decode_layer3_sideinfo(PMPSTR mp)
  1526. {
  1527. struct frame *fr = &mp->fr;
  1528. int stereo = fr->stereo;
  1529. int single = fr->single;
  1530. int ms_stereo;
  1531. int sfreq = fr->sampling_frequency;
  1532. int granules;
  1533. int ch, gr, databits;
  1534. if (stereo == 1) { /* stream is mono */
  1535. single = 0;
  1536. }
  1537. if (fr->mode == MPG_MD_JOINT_STEREO) {
  1538. ms_stereo = fr->mode_ext & 0x2;
  1539. }
  1540. else
  1541. ms_stereo = 0;
  1542. if (fr->lsf) {
  1543. granules = 1;
  1544. III_get_side_info_2(mp, stereo, ms_stereo, sfreq, single);
  1545. }
  1546. else {
  1547. granules = 2;
  1548. III_get_side_info_1(mp, stereo, ms_stereo, sfreq, single);
  1549. }
  1550. databits = 0;
  1551. for (gr = 0; gr < granules; ++gr) {
  1552. for (ch = 0; ch < stereo; ++ch) {
  1553. struct gr_info_s *gr_infos = &(mp->sideinfo.ch[ch].gr[gr]);
  1554. databits += gr_infos->part2_3_length;
  1555. }
  1556. }
  1557. return databits - 8 * mp->sideinfo.main_data_begin;
  1558. }
  1559. int
  1560. decode_layer3_frame(PMPSTR mp, unsigned char *pcm_sample, int *pcm_point,
  1561. int (*synth_1to1_mono_ptr) (PMPSTR, real *, unsigned char *, int *),
  1562. int (*synth_1to1_ptr) (PMPSTR, real *, int, unsigned char *, int *))
  1563. {
  1564. int gr, ch, ss, clip = 0;
  1565. int scalefacs[2][39]; /* max 39 for short[13][3] mode, mixed: 38, long: 22 */
  1566. /* struct III_sideinfo sideinfo; */
  1567. struct frame *fr = &(mp->fr);
  1568. int stereo = fr->stereo;
  1569. int single = fr->single;
  1570. int ms_stereo, i_stereo;
  1571. int sfreq = fr->sampling_frequency;
  1572. int stereo1, granules;
  1573. real hybridIn[2][SBLIMIT][SSLIMIT];
  1574. real hybridOut[2][SSLIMIT][SBLIMIT];
  1575. if (set_pointer(mp, (int) mp->sideinfo.main_data_begin) == MP3_ERR)
  1576. return 0;
  1577. if (stereo == 1) { /* stream is mono */
  1578. stereo1 = 1;
  1579. single = 0;
  1580. }
  1581. else if (single >= 0) /* stream is stereo, but force to mono */
  1582. stereo1 = 1;
  1583. else
  1584. stereo1 = 2;
  1585. if (fr->mode == MPG_MD_JOINT_STEREO) {
  1586. ms_stereo = fr->mode_ext & 0x2;
  1587. i_stereo = fr->mode_ext & 0x1;
  1588. }
  1589. else
  1590. ms_stereo = i_stereo = 0;
  1591. if (fr->lsf) {
  1592. granules = 1;
  1593. }
  1594. else {
  1595. granules = 2;
  1596. }
  1597. for (gr = 0; gr < granules; gr++) {
  1598. {
  1599. struct gr_info_s *gr_infos = &(mp->sideinfo.ch[0].gr[gr]);
  1600. long part2bits;
  1601. if (fr->lsf)
  1602. part2bits = III_get_scale_factors_2(mp, scalefacs[0], gr_infos, 0);
  1603. else {
  1604. part2bits = III_get_scale_factors_1(mp, scalefacs[0], gr_infos);
  1605. }
  1606. if (mp->pinfo != NULL) {
  1607. int i;
  1608. mp->pinfo->sfbits[gr][0] = part2bits;
  1609. for (i = 0; i < 39; i++)
  1610. mp->pinfo->sfb_s[gr][0][i] = scalefacs[0][i];
  1611. }
  1612. /* lame_report_fnc(mp->report_err, "calling III dequantize sample 1 gr_infos->part2_3_length %d\n", gr_infos->part2_3_length); */
  1613. if (III_dequantize_sample(mp, hybridIn[0], scalefacs[0], gr_infos, sfreq, part2bits))
  1614. return clip;
  1615. }
  1616. if (stereo == 2) {
  1617. struct gr_info_s *gr_infos = &(mp->sideinfo.ch[1].gr[gr]);
  1618. long part2bits;
  1619. if (fr->lsf)
  1620. part2bits = III_get_scale_factors_2(mp, scalefacs[1], gr_infos, i_stereo);
  1621. else {
  1622. part2bits = III_get_scale_factors_1(mp, scalefacs[1], gr_infos);
  1623. }
  1624. if (mp->pinfo != NULL) {
  1625. int i;
  1626. mp->pinfo->sfbits[gr][1] = part2bits;
  1627. for (i = 0; i < 39; i++)
  1628. mp->pinfo->sfb_s[gr][1][i] = scalefacs[1][i];
  1629. }
  1630. /* lame_report_fnc(mp->report_err, "calling III dequantize sample 2 gr_infos->part2_3_length %d\n", gr_infos->part2_3_length); */
  1631. if (III_dequantize_sample(mp, hybridIn[1], scalefacs[1], gr_infos, sfreq, part2bits))
  1632. return clip;
  1633. if (ms_stereo) {
  1634. int i;
  1635. for (i = 0; i < SBLIMIT * SSLIMIT; i++) {
  1636. real tmp0, tmp1;
  1637. tmp0 = ((real *) hybridIn[0])[i];
  1638. tmp1 = ((real *) hybridIn[1])[i];
  1639. ((real *) hybridIn[1])[i] = tmp0 - tmp1;
  1640. ((real *) hybridIn[0])[i] = tmp0 + tmp1;
  1641. }
  1642. }
  1643. if (i_stereo)
  1644. III_i_stereo(hybridIn, scalefacs[1], gr_infos, sfreq, ms_stereo, fr->lsf);
  1645. if (ms_stereo || i_stereo || (single == 3)) {
  1646. if (gr_infos->maxb > mp->sideinfo.ch[0].gr[gr].maxb)
  1647. mp->sideinfo.ch[0].gr[gr].maxb = gr_infos->maxb;
  1648. else
  1649. gr_infos->maxb = mp->sideinfo.ch[0].gr[gr].maxb;
  1650. }
  1651. switch (single) {
  1652. case 3:
  1653. {
  1654. int i;
  1655. real *in0 = (real *) hybridIn[0], *in1 = (real *) hybridIn[1];
  1656. for (i = 0; i < (int) (SSLIMIT * gr_infos->maxb); i++, in0++)
  1657. *in0 = (*in0 + *in1++); /* *0.5 done by pow-scale */
  1658. }
  1659. break;
  1660. case 1:
  1661. {
  1662. int i;
  1663. real *in0 = (real *) hybridIn[0], *in1 = (real *) hybridIn[1];
  1664. for (i = 0; i < (int) (SSLIMIT * gr_infos->maxb); i++)
  1665. *in0++ = *in1++;
  1666. }
  1667. break;
  1668. }
  1669. }
  1670. if (mp->pinfo != NULL) {
  1671. int i, sb;
  1672. float ifqstep;
  1673. mp->pinfo->bitrate = tabsel_123[fr->lsf][fr->lay - 1][fr->bitrate_index];
  1674. mp->pinfo->sampfreq = freqs[sfreq];
  1675. mp->pinfo->emph = fr->emphasis;
  1676. mp->pinfo->crc = fr->error_protection;
  1677. mp->pinfo->padding = fr->padding;
  1678. mp->pinfo->stereo = fr->stereo;
  1679. mp->pinfo->js = (fr->mode == MPG_MD_JOINT_STEREO);
  1680. mp->pinfo->ms_stereo = ms_stereo;
  1681. mp->pinfo->i_stereo = i_stereo;
  1682. mp->pinfo->maindata = mp->sideinfo.main_data_begin;
  1683. for (ch = 0; ch < stereo1; ch++) {
  1684. struct gr_info_s *gr_infos = &(mp->sideinfo.ch[ch].gr[gr]);
  1685. mp->pinfo->big_values[gr][ch] = gr_infos->big_values;
  1686. mp->pinfo->scalefac_scale[gr][ch] = gr_infos->scalefac_scale;
  1687. mp->pinfo->mixed[gr][ch] = gr_infos->mixed_block_flag;
  1688. mp->pinfo->mpg123blocktype[gr][ch] = gr_infos->block_type;
  1689. mp->pinfo->mainbits[gr][ch] = gr_infos->part2_3_length;
  1690. mp->pinfo->preflag[gr][ch] = gr_infos->preflag;
  1691. if (gr == 1)
  1692. mp->pinfo->scfsi[ch] = gr_infos->scfsi;
  1693. }
  1694. for (ch = 0; ch < stereo1; ch++) {
  1695. struct gr_info_s *gr_infos = &(mp->sideinfo.ch[ch].gr[gr]);
  1696. ifqstep = (mp->pinfo->scalefac_scale[gr][ch] == 0) ? .5 : 1.0;
  1697. if (2 == gr_infos->block_type) {
  1698. for (i = 0; i < 3; i++) {
  1699. for (sb = 0; sb < 12; sb++) {
  1700. int j = 3 * sb + i;
  1701. /*
  1702. is_p = scalefac[sfb*3+lwin-gr_infos->mixed_block_flag];
  1703. */
  1704. /* scalefac was copied into pinfo->sfb_s[] above */
  1705. mp->pinfo->sfb_s[gr][ch][j] =
  1706. -ifqstep * mp->pinfo->sfb_s[gr][ch][j - gr_infos->mixed_block_flag];
  1707. mp->pinfo->sfb_s[gr][ch][j] -= 2 * (mp->pinfo->sub_gain[gr][ch][i]);
  1708. }
  1709. mp->pinfo->sfb_s[gr][ch][3 * sb + i] =
  1710. -2 * (mp->pinfo->sub_gain[gr][ch][i]);
  1711. }
  1712. }
  1713. else {
  1714. for (sb = 0; sb < 21; sb++) {
  1715. /* scalefac was copied into pinfo->sfb[] above */
  1716. mp->pinfo->sfb[gr][ch][sb] = mp->pinfo->sfb_s[gr][ch][sb];
  1717. if (gr_infos->preflag)
  1718. mp->pinfo->sfb[gr][ch][sb] += pretab1[sb];
  1719. mp->pinfo->sfb[gr][ch][sb] *= -ifqstep;
  1720. }
  1721. mp->pinfo->sfb[gr][ch][21] = 0;
  1722. }
  1723. }
  1724. for (ch = 0; ch < stereo1; ch++) {
  1725. int j = 0;
  1726. for (sb = 0; sb < SBLIMIT; sb++)
  1727. for (ss = 0; ss < SSLIMIT; ss++, j++)
  1728. mp->pinfo->mpg123xr[gr][ch][j] = hybridIn[ch][sb][ss];
  1729. }
  1730. }
  1731. for (ch = 0; ch < stereo1; ch++) {
  1732. struct gr_info_s *gr_infos = &(mp->sideinfo.ch[ch].gr[gr]);
  1733. III_antialias(hybridIn[ch], gr_infos);
  1734. III_hybrid(mp, hybridIn[ch], hybridOut[ch], ch, gr_infos);
  1735. }
  1736. for (ss = 0; ss < SSLIMIT; ss++) {
  1737. if (single >= 0) {
  1738. clip += (*synth_1to1_mono_ptr) (mp, hybridOut[0][ss], pcm_sample, pcm_point);
  1739. }
  1740. else {
  1741. int p1 = *pcm_point;
  1742. clip += (*synth_1to1_ptr) (mp, hybridOut[0][ss], 0, pcm_sample, &p1);
  1743. clip += (*synth_1to1_ptr) (mp, hybridOut[1][ss], 1, pcm_sample, pcm_point);
  1744. }
  1745. }
  1746. }
  1747. return clip;
  1748. }