You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

445 lines
13KB

  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 "cbrt_tablegen.h"
  75. #include "sbr.h"
  76. #include "aacsbr.h"
  77. #include "mpeg4audio.h"
  78. #include "aacadtsdec.h"
  79. #include "libavutil/intfloat.h"
  80. #include <math.h>
  81. #include <string.h>
  82. static av_always_inline void reset_predict_state(PredictorState *ps)
  83. {
  84. ps->r0.mant = 0;
  85. ps->r0.exp = 0;
  86. ps->r1.mant = 0;
  87. ps->r1.exp = 0;
  88. ps->cor0.mant = 0;
  89. ps->cor0.exp = 0;
  90. ps->cor1.mant = 0;
  91. ps->cor1.exp = 0;
  92. ps->var0.mant = 0x20000000;
  93. ps->var0.exp = 1;
  94. ps->var1.mant = 0x20000000;
  95. ps->var1.exp = 1;
  96. }
  97. 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
  98. static inline int *DEC_SPAIR(int *dst, unsigned idx)
  99. {
  100. dst[0] = (idx & 15) - 4;
  101. dst[1] = (idx >> 4 & 15) - 4;
  102. return dst + 2;
  103. }
  104. static inline int *DEC_SQUAD(int *dst, unsigned idx)
  105. {
  106. dst[0] = (idx & 3) - 1;
  107. dst[1] = (idx >> 2 & 3) - 1;
  108. dst[2] = (idx >> 4 & 3) - 1;
  109. dst[3] = (idx >> 6 & 3) - 1;
  110. return dst + 4;
  111. }
  112. static inline int *DEC_UPAIR(int *dst, unsigned idx, unsigned sign)
  113. {
  114. dst[0] = (idx & 15) * (1 - (sign & 0xFFFFFFFE));
  115. dst[1] = (idx >> 4 & 15) * (1 - ((sign & 1) << 1));
  116. return dst + 2;
  117. }
  118. static inline int *DEC_UQUAD(int *dst, unsigned idx, unsigned sign)
  119. {
  120. unsigned nz = idx >> 12;
  121. dst[0] = (idx & 3) * (1 + (((int)sign >> 31) << 1));
  122. sign <<= nz & 1;
  123. nz >>= 1;
  124. dst[1] = (idx >> 2 & 3) * (1 + (((int)sign >> 31) << 1));
  125. sign <<= nz & 1;
  126. nz >>= 1;
  127. dst[2] = (idx >> 4 & 3) * (1 + (((int)sign >> 31) << 1));
  128. sign <<= nz & 1;
  129. nz >>= 1;
  130. dst[3] = (idx >> 6 & 3) * (1 + (((int)sign >> 31) << 1));
  131. return dst + 4;
  132. }
  133. static void vector_pow43(int *coefs, int len)
  134. {
  135. int i, coef;
  136. for (i=0; i<len; i++) {
  137. coef = coefs[i];
  138. if (coef < 0)
  139. coef = -(int)cbrt_tab[-coef];
  140. else
  141. coef = (int)cbrt_tab[coef];
  142. coefs[i] = coef;
  143. }
  144. }
  145. static void subband_scale(int *dst, int *src, int scale, int offset, int len)
  146. {
  147. int ssign = scale < 0 ? -1 : 1;
  148. int s = FFABS(scale);
  149. unsigned int round;
  150. int i, out, c = exp2tab[s & 3];
  151. s = offset - (s >> 2);
  152. if (s > 0) {
  153. round = 1 << (s-1);
  154. for (i=0; i<len; i++) {
  155. out = (int)(((int64_t)src[i] * c) >> 32);
  156. dst[i] = ((int)(out+round) >> s) * ssign;
  157. }
  158. }
  159. else {
  160. s = s + 32;
  161. round = 1 << (s-1);
  162. for (i=0; i<len; i++) {
  163. out = (int)((int64_t)((int64_t)src[i] * c + round) >> s);
  164. dst[i] = out * ssign;
  165. }
  166. }
  167. }
  168. static void noise_scale(int *coefs, int scale, int band_energy, int len)
  169. {
  170. int ssign = scale < 0 ? -1 : 1;
  171. int s = FFABS(scale);
  172. unsigned int round;
  173. int i, out, c = exp2tab[s & 3];
  174. int nlz = 0;
  175. while (band_energy > 0x7fff) {
  176. band_energy >>= 1;
  177. nlz++;
  178. }
  179. c /= band_energy;
  180. s = 21 + nlz - (s >> 2);
  181. if (s > 0) {
  182. round = 1 << (s-1);
  183. for (i=0; i<len; i++) {
  184. out = (int)(((int64_t)coefs[i] * c) >> 32);
  185. coefs[i] = ((int)(out+round) >> s) * ssign;
  186. }
  187. }
  188. else {
  189. s = s + 32;
  190. round = 1 << (s-1);
  191. for (i=0; i<len; i++) {
  192. out = (int)((int64_t)((int64_t)coefs[i] * c + round) >> s);
  193. coefs[i] = out * ssign;
  194. }
  195. }
  196. }
  197. static av_always_inline SoftFloat flt16_round(SoftFloat pf)
  198. {
  199. SoftFloat tmp;
  200. int s;
  201. tmp.exp = pf.exp;
  202. s = pf.mant >> 31;
  203. tmp.mant = (pf.mant ^ s) - s;
  204. tmp.mant = (tmp.mant + 0x00200000U) & 0xFFC00000U;
  205. tmp.mant = (tmp.mant ^ s) - s;
  206. return tmp;
  207. }
  208. static av_always_inline SoftFloat flt16_even(SoftFloat pf)
  209. {
  210. SoftFloat tmp;
  211. int s;
  212. tmp.exp = pf.exp;
  213. s = pf.mant >> 31;
  214. tmp.mant = (pf.mant ^ s) - s;
  215. tmp.mant = (tmp.mant + 0x001FFFFFU + (tmp.mant & 0x00400000U >> 16)) & 0xFFC00000U;
  216. tmp.mant = (tmp.mant ^ s) - s;
  217. return tmp;
  218. }
  219. static av_always_inline SoftFloat flt16_trunc(SoftFloat pf)
  220. {
  221. SoftFloat pun;
  222. int s;
  223. pun.exp = pf.exp;
  224. s = pf.mant >> 31;
  225. pun.mant = (pf.mant ^ s) - s;
  226. pun.mant = pun.mant & 0xFFC00000U;
  227. pun.mant = (pun.mant ^ s) - s;
  228. return pun;
  229. }
  230. static av_always_inline void predict(PredictorState *ps, int *coef,
  231. int output_enable)
  232. {
  233. const SoftFloat a = { 1023410176, 0 }; // 61.0 / 64
  234. const SoftFloat alpha = { 973078528, 0 }; // 29.0 / 32
  235. SoftFloat e0, e1;
  236. SoftFloat pv;
  237. SoftFloat k1, k2;
  238. SoftFloat r0 = ps->r0, r1 = ps->r1;
  239. SoftFloat cor0 = ps->cor0, cor1 = ps->cor1;
  240. SoftFloat var0 = ps->var0, var1 = ps->var1;
  241. SoftFloat tmp;
  242. if (var0.exp > 1 || (var0.exp == 1 && var0.mant > 0x20000000)) {
  243. k1 = av_mul_sf(cor0, flt16_even(av_div_sf(a, var0)));
  244. }
  245. else {
  246. k1.mant = 0;
  247. k1.exp = 0;
  248. }
  249. if (var1.exp > 1 || (var1.exp == 1 && var1.mant > 0x20000000)) {
  250. k2 = av_mul_sf(cor1, flt16_even(av_div_sf(a, var1)));
  251. }
  252. else {
  253. k2.mant = 0;
  254. k2.exp = 0;
  255. }
  256. tmp = av_mul_sf(k1, r0);
  257. pv = flt16_round(av_add_sf(tmp, av_mul_sf(k2, r1)));
  258. if (output_enable) {
  259. int shift = 28 - pv.exp;
  260. if (shift < 31)
  261. *coef += (pv.mant + (1 << (shift - 1))) >> shift;
  262. }
  263. e0 = av_int2sf(*coef, 2);
  264. e1 = av_sub_sf(e0, tmp);
  265. ps->cor1 = flt16_trunc(av_add_sf(av_mul_sf(alpha, cor1), av_mul_sf(r1, e1)));
  266. tmp = av_add_sf(av_mul_sf(r1, r1), av_mul_sf(e1, e1));
  267. tmp.exp--;
  268. ps->var1 = flt16_trunc(av_add_sf(av_mul_sf(alpha, var1), tmp));
  269. ps->cor0 = flt16_trunc(av_add_sf(av_mul_sf(alpha, cor0), av_mul_sf(r0, e0)));
  270. tmp = av_add_sf(av_mul_sf(r0, r0), av_mul_sf(e0, e0));
  271. tmp.exp--;
  272. ps->var0 = flt16_trunc(av_add_sf(av_mul_sf(alpha, var0), tmp));
  273. ps->r1 = flt16_trunc(av_mul_sf(a, av_sub_sf(r0, av_mul_sf(k1, e0))));
  274. ps->r0 = flt16_trunc(av_mul_sf(a, e0));
  275. }
  276. static const int cce_scale_fixed[8] = {
  277. Q30(1.0), //2^(0/8)
  278. Q30(1.0905077327), //2^(1/8)
  279. Q30(1.1892071150), //2^(2/8)
  280. Q30(1.2968395547), //2^(3/8)
  281. Q30(1.4142135624), //2^(4/8)
  282. Q30(1.5422108254), //2^(5/8)
  283. Q30(1.6817928305), //2^(6/8)
  284. Q30(1.8340080864), //2^(7/8)
  285. };
  286. /**
  287. * Apply dependent channel coupling (applied before IMDCT).
  288. *
  289. * @param index index into coupling gain array
  290. */
  291. static void apply_dependent_coupling_fixed(AACContext *ac,
  292. SingleChannelElement *target,
  293. ChannelElement *cce, int index)
  294. {
  295. IndividualChannelStream *ics = &cce->ch[0].ics;
  296. const uint16_t *offsets = ics->swb_offset;
  297. int *dest = target->coeffs;
  298. const int *src = cce->ch[0].coeffs;
  299. int g, i, group, k, idx = 0;
  300. if (ac->oc[1].m4ac.object_type == AOT_AAC_LTP) {
  301. av_log(ac->avctx, AV_LOG_ERROR,
  302. "Dependent coupling is not supported together with LTP\n");
  303. return;
  304. }
  305. for (g = 0; g < ics->num_window_groups; g++) {
  306. for (i = 0; i < ics->max_sfb; i++, idx++) {
  307. if (cce->ch[0].band_type[idx] != ZERO_BT) {
  308. const int gain = cce->coup.gain[index][idx];
  309. int shift, round, c, tmp;
  310. if (gain < 0) {
  311. c = -cce_scale_fixed[-gain & 7];
  312. shift = (-gain-1024) >> 3;
  313. }
  314. else {
  315. c = cce_scale_fixed[gain & 7];
  316. shift = (gain-1024) >> 3;
  317. }
  318. if (shift < 0) {
  319. shift = -shift;
  320. round = 1 << (shift - 1);
  321. for (group = 0; group < ics->group_len[g]; group++) {
  322. for (k = offsets[i]; k < offsets[i + 1]; k++) {
  323. tmp = (int)(((int64_t)src[group * 128 + k] * c + \
  324. (int64_t)0x1000000000) >> 37);
  325. dest[group * 128 + k] += (tmp + round) >> shift;
  326. }
  327. }
  328. }
  329. else {
  330. for (group = 0; group < ics->group_len[g]; group++) {
  331. for (k = offsets[i]; k < offsets[i + 1]; k++) {
  332. tmp = (int)(((int64_t)src[group * 128 + k] * c + \
  333. (int64_t)0x1000000000) >> 37);
  334. dest[group * 128 + k] += tmp << shift;
  335. }
  336. }
  337. }
  338. }
  339. }
  340. dest += ics->group_len[g] * 128;
  341. src += ics->group_len[g] * 128;
  342. }
  343. }
  344. /**
  345. * Apply independent channel coupling (applied after IMDCT).
  346. *
  347. * @param index index into coupling gain array
  348. */
  349. static void apply_independent_coupling_fixed(AACContext *ac,
  350. SingleChannelElement *target,
  351. ChannelElement *cce, int index)
  352. {
  353. int i, c, shift, round, tmp;
  354. const int gain = cce->coup.gain[index][0];
  355. const int *src = cce->ch[0].ret;
  356. int *dest = target->ret;
  357. const int len = 1024 << (ac->oc[1].m4ac.sbr == 1);
  358. c = cce_scale_fixed[gain & 7];
  359. shift = (gain-1024) >> 3;
  360. if (shift < 0) {
  361. shift = -shift;
  362. round = 1 << (shift - 1);
  363. for (i = 0; i < len; i++) {
  364. tmp = (int)(((int64_t)src[i] * c + (int64_t)0x1000000000) >> 37);
  365. dest[i] += (tmp + round) >> shift;
  366. }
  367. }
  368. else {
  369. for (i = 0; i < len; i++) {
  370. tmp = (int)(((int64_t)src[i] * c + (int64_t)0x1000000000) >> 37);
  371. dest[i] += tmp << shift;
  372. }
  373. }
  374. }
  375. #include "aacdec_template.c"
  376. AVCodec ff_aac_fixed_decoder = {
  377. .name = "aac_fixed",
  378. .long_name = NULL_IF_CONFIG_SMALL("AAC (Advanced Audio Coding)"),
  379. .type = AVMEDIA_TYPE_AUDIO,
  380. .id = AV_CODEC_ID_AAC,
  381. .priv_data_size = sizeof(AACContext),
  382. .init = aac_decode_init,
  383. .close = aac_decode_close,
  384. .decode = aac_decode_frame,
  385. .sample_fmts = (const enum AVSampleFormat[]) {
  386. AV_SAMPLE_FMT_S32P, AV_SAMPLE_FMT_NONE
  387. },
  388. .capabilities = AV_CODEC_CAP_CHANNEL_CONF | AV_CODEC_CAP_DR1,
  389. .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE,
  390. .channel_layouts = aac_channel_layout,
  391. .flush = flush,
  392. };