2
0

aacdec_fixed.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  1. /*
  2. * Copyright (c) 2013
  3. * MIPS Technologies, Inc., California.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * 3. Neither the name of the MIPS Technologies, Inc., nor the names of its
  14. * contributors may be used to endorse or promote products derived from
  15. * this software without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE MIPS TECHNOLOGIES, INC. ``AS IS'' AND
  18. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20. * ARE DISCLAIMED. IN NO EVENT SHALL THE MIPS TECHNOLOGIES, INC. BE LIABLE
  21. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  22. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  23. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  24. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  25. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  26. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  27. * SUCH DAMAGE.
  28. *
  29. * AAC decoder fixed-point implementation
  30. *
  31. * Copyright (c) 2005-2006 Oded Shimon ( ods15 ods15 dyndns org )
  32. * Copyright (c) 2006-2007 Maxim Gavrilov ( maxim.gavrilov gmail com )
  33. *
  34. * This file is part of FFmpeg.
  35. *
  36. * FFmpeg is free software; you can redistribute it and/or
  37. * modify it under the terms of the GNU Lesser General Public
  38. * License as published by the Free Software Foundation; either
  39. * version 2.1 of the License, or (at your option) any later version.
  40. *
  41. * FFmpeg is distributed in the hope that it will be useful,
  42. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  43. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  44. * Lesser General Public License for more details.
  45. *
  46. * You should have received a copy of the GNU Lesser General Public
  47. * License along with FFmpeg; if not, write to the Free Software
  48. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  49. */
  50. /**
  51. * @file
  52. * AAC decoder
  53. * @author Oded Shimon ( ods15 ods15 dyndns org )
  54. * @author Maxim Gavrilov ( maxim.gavrilov gmail com )
  55. *
  56. * Fixed point implementation
  57. * @author Stanislav Ocovaj ( stanislav.ocovaj imgtec com )
  58. */
  59. #define FFT_FLOAT 0
  60. #define FFT_FIXED_32 1
  61. #define USE_FIXED 1
  62. #include "libavutil/fixed_dsp.h"
  63. #include "libavutil/opt.h"
  64. #include "avcodec.h"
  65. #include "internal.h"
  66. #include "get_bits.h"
  67. #include "fft.h"
  68. #include "lpc.h"
  69. #include "kbdwin.h"
  70. #include "sinewin.h"
  71. #include "aac.h"
  72. #include "aactab.h"
  73. #include "aacdectab.h"
  74. #include "adts_header.h"
  75. #include "cbrt_data.h"
  76. #include "sbr.h"
  77. #include "aacsbr.h"
  78. #include "mpeg4audio.h"
  79. #include "profiles.h"
  80. #include "libavutil/intfloat.h"
  81. #include <math.h>
  82. #include <string.h>
  83. static av_always_inline void reset_predict_state(PredictorState *ps)
  84. {
  85. ps->r0.mant = 0;
  86. ps->r0.exp = 0;
  87. ps->r1.mant = 0;
  88. ps->r1.exp = 0;
  89. ps->cor0.mant = 0;
  90. ps->cor0.exp = 0;
  91. ps->cor1.mant = 0;
  92. ps->cor1.exp = 0;
  93. ps->var0.mant = 0x20000000;
  94. ps->var0.exp = 1;
  95. ps->var1.mant = 0x20000000;
  96. ps->var1.exp = 1;
  97. }
  98. static const int exp2tab[4] = { Q31(1.0000000000/2), Q31(1.1892071150/2), Q31(1.4142135624/2), Q31(1.6817928305/2) }; // 2^0, 2^0.25, 2^0.5, 2^0.75
  99. static inline int *DEC_SPAIR(int *dst, unsigned idx)
  100. {
  101. dst[0] = (idx & 15) - 4;
  102. dst[1] = (idx >> 4 & 15) - 4;
  103. return dst + 2;
  104. }
  105. static inline int *DEC_SQUAD(int *dst, unsigned idx)
  106. {
  107. dst[0] = (idx & 3) - 1;
  108. dst[1] = (idx >> 2 & 3) - 1;
  109. dst[2] = (idx >> 4 & 3) - 1;
  110. dst[3] = (idx >> 6 & 3) - 1;
  111. return dst + 4;
  112. }
  113. static inline int *DEC_UPAIR(int *dst, unsigned idx, unsigned sign)
  114. {
  115. dst[0] = (idx & 15) * (1 - (sign & 0xFFFFFFFE));
  116. dst[1] = (idx >> 4 & 15) * (1 - ((sign & 1) * 2));
  117. return dst + 2;
  118. }
  119. static inline int *DEC_UQUAD(int *dst, unsigned idx, unsigned sign)
  120. {
  121. unsigned nz = idx >> 12;
  122. dst[0] = (idx & 3) * (1 + (((int)sign >> 31) * 2));
  123. sign <<= nz & 1;
  124. nz >>= 1;
  125. dst[1] = (idx >> 2 & 3) * (1 + (((int)sign >> 31) * 2));
  126. sign <<= nz & 1;
  127. nz >>= 1;
  128. dst[2] = (idx >> 4 & 3) * (1 + (((int)sign >> 31) * 2));
  129. sign <<= nz & 1;
  130. nz >>= 1;
  131. dst[3] = (idx >> 6 & 3) * (1 + (((int)sign >> 31) * 2));
  132. return dst + 4;
  133. }
  134. static void vector_pow43(int *coefs, int len)
  135. {
  136. int i, coef;
  137. for (i=0; i<len; i++) {
  138. coef = coefs[i];
  139. if (coef < 0)
  140. coef = -(int)ff_cbrt_tab_fixed[-coef];
  141. else
  142. coef = (int)ff_cbrt_tab_fixed[coef];
  143. coefs[i] = coef;
  144. }
  145. }
  146. static void subband_scale(int *dst, int *src, int scale, int offset, int len)
  147. {
  148. int ssign = scale < 0 ? -1 : 1;
  149. int s = FFABS(scale);
  150. unsigned int round;
  151. int i, out, c = exp2tab[s & 3];
  152. s = offset - (s >> 2);
  153. if (s > 31) {
  154. for (i=0; i<len; i++) {
  155. dst[i] = 0;
  156. }
  157. } else if (s > 0) {
  158. round = 1 << (s-1);
  159. for (i=0; i<len; i++) {
  160. out = (int)(((int64_t)src[i] * c) >> 32);
  161. dst[i] = ((int)(out+round) >> s) * ssign;
  162. }
  163. } else if (s > -32) {
  164. s = s + 32;
  165. round = 1U << (s-1);
  166. for (i=0; i<len; i++) {
  167. out = (int)((int64_t)((int64_t)src[i] * c + round) >> s);
  168. dst[i] = out * (unsigned)ssign;
  169. }
  170. } else {
  171. av_log(NULL, AV_LOG_ERROR, "Overflow in subband_scale()\n");
  172. }
  173. }
  174. static void noise_scale(int *coefs, int scale, int band_energy, int len)
  175. {
  176. int s = -scale;
  177. unsigned int round;
  178. int i, out, c = exp2tab[s & 3];
  179. int nlz = 0;
  180. av_assert0(s >= 0);
  181. while (band_energy > 0x7fff) {
  182. band_energy >>= 1;
  183. nlz++;
  184. }
  185. c /= band_energy;
  186. s = 21 + nlz - (s >> 2);
  187. if (s > 31) {
  188. for (i=0; i<len; i++) {
  189. coefs[i] = 0;
  190. }
  191. } else if (s >= 0) {
  192. round = s ? 1 << (s-1) : 0;
  193. for (i=0; i<len; i++) {
  194. out = (int)(((int64_t)coefs[i] * c) >> 32);
  195. coefs[i] = -((int)(out+round) >> s);
  196. }
  197. }
  198. else {
  199. s = s + 32;
  200. if (s > 0) {
  201. round = 1 << (s-1);
  202. for (i=0; i<len; i++) {
  203. out = (int)((int64_t)((int64_t)coefs[i] * c + round) >> s);
  204. coefs[i] = -out;
  205. }
  206. } else {
  207. for (i=0; i<len; i++)
  208. coefs[i] = -(int64_t)coefs[i] * c * (1 << -s);
  209. }
  210. }
  211. }
  212. static av_always_inline SoftFloat flt16_round(SoftFloat pf)
  213. {
  214. SoftFloat tmp;
  215. int s;
  216. tmp.exp = pf.exp;
  217. s = pf.mant >> 31;
  218. tmp.mant = (pf.mant ^ s) - s;
  219. tmp.mant = (tmp.mant + 0x00200000U) & 0xFFC00000U;
  220. tmp.mant = (tmp.mant ^ s) - s;
  221. return tmp;
  222. }
  223. static av_always_inline SoftFloat flt16_even(SoftFloat pf)
  224. {
  225. SoftFloat tmp;
  226. int s;
  227. tmp.exp = pf.exp;
  228. s = pf.mant >> 31;
  229. tmp.mant = (pf.mant ^ s) - s;
  230. tmp.mant = (tmp.mant + 0x001FFFFFU + (tmp.mant & 0x00400000U >> 16)) & 0xFFC00000U;
  231. tmp.mant = (tmp.mant ^ s) - s;
  232. return tmp;
  233. }
  234. static av_always_inline SoftFloat flt16_trunc(SoftFloat pf)
  235. {
  236. SoftFloat pun;
  237. int s;
  238. pun.exp = pf.exp;
  239. s = pf.mant >> 31;
  240. pun.mant = (pf.mant ^ s) - s;
  241. pun.mant = pun.mant & 0xFFC00000U;
  242. pun.mant = (pun.mant ^ s) - s;
  243. return pun;
  244. }
  245. static av_always_inline void predict(PredictorState *ps, int *coef,
  246. int output_enable)
  247. {
  248. const SoftFloat a = { 1023410176, 0 }; // 61.0 / 64
  249. const SoftFloat alpha = { 973078528, 0 }; // 29.0 / 32
  250. SoftFloat e0, e1;
  251. SoftFloat pv;
  252. SoftFloat k1, k2;
  253. SoftFloat r0 = ps->r0, r1 = ps->r1;
  254. SoftFloat cor0 = ps->cor0, cor1 = ps->cor1;
  255. SoftFloat var0 = ps->var0, var1 = ps->var1;
  256. SoftFloat tmp;
  257. if (var0.exp > 1 || (var0.exp == 1 && var0.mant > 0x20000000)) {
  258. k1 = av_mul_sf(cor0, flt16_even(av_div_sf(a, var0)));
  259. }
  260. else {
  261. k1.mant = 0;
  262. k1.exp = 0;
  263. }
  264. if (var1.exp > 1 || (var1.exp == 1 && var1.mant > 0x20000000)) {
  265. k2 = av_mul_sf(cor1, flt16_even(av_div_sf(a, var1)));
  266. }
  267. else {
  268. k2.mant = 0;
  269. k2.exp = 0;
  270. }
  271. tmp = av_mul_sf(k1, r0);
  272. pv = flt16_round(av_add_sf(tmp, av_mul_sf(k2, r1)));
  273. if (output_enable) {
  274. int shift = 28 - pv.exp;
  275. if (shift < 31) {
  276. if (shift > 0) {
  277. *coef += (unsigned)((pv.mant + (1 << (shift - 1))) >> shift);
  278. } else
  279. *coef += (unsigned)pv.mant << -shift;
  280. }
  281. }
  282. e0 = av_int2sf(*coef, 2);
  283. e1 = av_sub_sf(e0, tmp);
  284. ps->cor1 = flt16_trunc(av_add_sf(av_mul_sf(alpha, cor1), av_mul_sf(r1, e1)));
  285. tmp = av_add_sf(av_mul_sf(r1, r1), av_mul_sf(e1, e1));
  286. tmp.exp--;
  287. ps->var1 = flt16_trunc(av_add_sf(av_mul_sf(alpha, var1), tmp));
  288. ps->cor0 = flt16_trunc(av_add_sf(av_mul_sf(alpha, cor0), av_mul_sf(r0, e0)));
  289. tmp = av_add_sf(av_mul_sf(r0, r0), av_mul_sf(e0, e0));
  290. tmp.exp--;
  291. ps->var0 = flt16_trunc(av_add_sf(av_mul_sf(alpha, var0), tmp));
  292. ps->r1 = flt16_trunc(av_mul_sf(a, av_sub_sf(r0, av_mul_sf(k1, e0))));
  293. ps->r0 = flt16_trunc(av_mul_sf(a, e0));
  294. }
  295. static const int cce_scale_fixed[8] = {
  296. Q30(1.0), //2^(0/8)
  297. Q30(1.0905077327), //2^(1/8)
  298. Q30(1.1892071150), //2^(2/8)
  299. Q30(1.2968395547), //2^(3/8)
  300. Q30(1.4142135624), //2^(4/8)
  301. Q30(1.5422108254), //2^(5/8)
  302. Q30(1.6817928305), //2^(6/8)
  303. Q30(1.8340080864), //2^(7/8)
  304. };
  305. /**
  306. * Apply dependent channel coupling (applied before IMDCT).
  307. *
  308. * @param index index into coupling gain array
  309. */
  310. static void apply_dependent_coupling_fixed(AACContext *ac,
  311. SingleChannelElement *target,
  312. ChannelElement *cce, int index)
  313. {
  314. IndividualChannelStream *ics = &cce->ch[0].ics;
  315. const uint16_t *offsets = ics->swb_offset;
  316. int *dest = target->coeffs;
  317. const int *src = cce->ch[0].coeffs;
  318. int g, i, group, k, idx = 0;
  319. if (ac->oc[1].m4ac.object_type == AOT_AAC_LTP) {
  320. av_log(ac->avctx, AV_LOG_ERROR,
  321. "Dependent coupling is not supported together with LTP\n");
  322. return;
  323. }
  324. for (g = 0; g < ics->num_window_groups; g++) {
  325. for (i = 0; i < ics->max_sfb; i++, idx++) {
  326. if (cce->ch[0].band_type[idx] != ZERO_BT) {
  327. const int gain = cce->coup.gain[index][idx];
  328. int shift, round, c, tmp;
  329. if (gain < 0) {
  330. c = -cce_scale_fixed[-gain & 7];
  331. shift = (-gain-1024) >> 3;
  332. }
  333. else {
  334. c = cce_scale_fixed[gain & 7];
  335. shift = (gain-1024) >> 3;
  336. }
  337. if (shift < -31) {
  338. // Nothing to do
  339. } else if (shift < 0) {
  340. shift = -shift;
  341. round = 1 << (shift - 1);
  342. for (group = 0; group < ics->group_len[g]; group++) {
  343. for (k = offsets[i]; k < offsets[i + 1]; k++) {
  344. tmp = (int)(((int64_t)src[group * 128 + k] * c + \
  345. (int64_t)0x1000000000) >> 37);
  346. dest[group * 128 + k] += (tmp + (int64_t)round) >> shift;
  347. }
  348. }
  349. }
  350. else {
  351. for (group = 0; group < ics->group_len[g]; group++) {
  352. for (k = offsets[i]; k < offsets[i + 1]; k++) {
  353. tmp = (int)(((int64_t)src[group * 128 + k] * c + \
  354. (int64_t)0x1000000000) >> 37);
  355. dest[group * 128 + k] += tmp * (1U << shift);
  356. }
  357. }
  358. }
  359. }
  360. }
  361. dest += ics->group_len[g] * 128;
  362. src += ics->group_len[g] * 128;
  363. }
  364. }
  365. /**
  366. * Apply independent channel coupling (applied after IMDCT).
  367. *
  368. * @param index index into coupling gain array
  369. */
  370. static void apply_independent_coupling_fixed(AACContext *ac,
  371. SingleChannelElement *target,
  372. ChannelElement *cce, int index)
  373. {
  374. int i, c, shift, round, tmp;
  375. const int gain = cce->coup.gain[index][0];
  376. const int *src = cce->ch[0].ret;
  377. unsigned int *dest = target->ret;
  378. const int len = 1024 << (ac->oc[1].m4ac.sbr == 1);
  379. c = cce_scale_fixed[gain & 7];
  380. shift = (gain-1024) >> 3;
  381. if (shift < -31) {
  382. return;
  383. } else if (shift < 0) {
  384. shift = -shift;
  385. round = 1 << (shift - 1);
  386. for (i = 0; i < len; i++) {
  387. tmp = (int)(((int64_t)src[i] * c + (int64_t)0x1000000000) >> 37);
  388. dest[i] += (tmp + round) >> shift;
  389. }
  390. }
  391. else {
  392. for (i = 0; i < len; i++) {
  393. tmp = (int)(((int64_t)src[i] * c + (int64_t)0x1000000000) >> 37);
  394. dest[i] += tmp * (1U << shift);
  395. }
  396. }
  397. }
  398. #include "aacdec_template.c"
  399. AVCodec ff_aac_fixed_decoder = {
  400. .name = "aac_fixed",
  401. .long_name = NULL_IF_CONFIG_SMALL("AAC (Advanced Audio Coding)"),
  402. .type = AVMEDIA_TYPE_AUDIO,
  403. .id = AV_CODEC_ID_AAC,
  404. .priv_data_size = sizeof(AACContext),
  405. .init = aac_decode_init,
  406. .close = aac_decode_close,
  407. .decode = aac_decode_frame,
  408. .sample_fmts = (const enum AVSampleFormat[]) {
  409. AV_SAMPLE_FMT_S32P, AV_SAMPLE_FMT_NONE
  410. },
  411. .capabilities = AV_CODEC_CAP_CHANNEL_CONF | AV_CODEC_CAP_DR1,
  412. .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE,
  413. .channel_layouts = aac_channel_layout,
  414. .profiles = NULL_IF_CONFIG_SMALL(ff_aac_profiles),
  415. .flush = flush,
  416. };