nellymoserenc.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. /*
  2. * Nellymoser encoder
  3. * This code is developed as part of Google Summer of Code 2008 Program.
  4. *
  5. * Copyright (c) 2008 Bartlomiej Wolowiec
  6. *
  7. * This file is part of FFmpeg.
  8. *
  9. * FFmpeg is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU Lesser General Public
  11. * License as published by the Free Software Foundation; either
  12. * version 2.1 of the License, or (at your option) any later version.
  13. *
  14. * FFmpeg is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * Lesser General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public
  20. * License along with FFmpeg; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  22. */
  23. /**
  24. * @file libavcodec/nellymoserenc.c
  25. * Nellymoser encoder
  26. * by Bartlomiej Wolowiec
  27. *
  28. * Generic codec information: libavcodec/nellymoserdec.c
  29. *
  30. * Some information also from: http://samples.mplayerhq.hu/A-codecs/Nelly_Moser/ASAO/ASAO.zip
  31. * (Copyright Joseph Artsimovich and UAB "DKD")
  32. *
  33. * for more information about nellymoser format, visit:
  34. * http://wiki.multimedia.cx/index.php?title=Nellymoser
  35. */
  36. #include "nellymoser.h"
  37. #include "avcodec.h"
  38. #include "dsputil.h"
  39. #define BITSTREAM_WRITER_LE
  40. #include "bitstream.h"
  41. #define POW_TABLE_SIZE (1<<11)
  42. #define POW_TABLE_OFFSET 3
  43. #define OPT_SIZE ((1<<15) + 3000)
  44. typedef struct NellyMoserEncodeContext {
  45. AVCodecContext *avctx;
  46. int last_frame;
  47. int bufsel;
  48. int have_saved;
  49. DSPContext dsp;
  50. MDCTContext mdct_ctx;
  51. DECLARE_ALIGNED_16(float, mdct_out[NELLY_SAMPLES]);
  52. DECLARE_ALIGNED_16(float, buf[2][3 * NELLY_BUF_LEN]); ///< sample buffer
  53. float (*opt )[NELLY_BANDS];
  54. uint8_t (*path)[NELLY_BANDS];
  55. } NellyMoserEncodeContext;
  56. static float pow_table[POW_TABLE_SIZE]; ///< -pow(2, -i / 2048.0 - 3.0);
  57. static const uint8_t sf_lut[96] = {
  58. 0, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 4, 4,
  59. 5, 5, 5, 6, 7, 7, 8, 8, 9, 10, 11, 11, 12, 13, 13, 14,
  60. 15, 15, 16, 17, 17, 18, 19, 19, 20, 21, 22, 22, 23, 24, 25, 26,
  61. 27, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 37, 38, 39, 40,
  62. 41, 41, 42, 43, 44, 45, 45, 46, 47, 48, 49, 50, 51, 52, 52, 53,
  63. 54, 55, 55, 56, 57, 57, 58, 59, 59, 60, 60, 60, 61, 61, 61, 62,
  64. };
  65. static const uint8_t sf_delta_lut[78] = {
  66. 0, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 4, 4,
  67. 4, 5, 5, 5, 6, 6, 7, 7, 8, 8, 9, 10, 10, 11, 11, 12,
  68. 13, 13, 14, 15, 16, 17, 17, 18, 19, 19, 20, 21, 21, 22, 22, 23,
  69. 23, 24, 24, 25, 25, 25, 26, 26, 26, 26, 27, 27, 27, 27, 27, 28,
  70. 28, 28, 28, 28, 28, 29, 29, 29, 29, 29, 29, 29, 29, 30,
  71. };
  72. static const uint8_t quant_lut[230] = {
  73. 0,
  74. 0, 1, 2,
  75. 0, 1, 2, 3, 4, 5, 6,
  76. 0, 1, 1, 2, 2, 3, 3, 4, 5, 6, 7, 8, 9, 10, 11, 11,
  77. 12, 13, 13, 13, 14,
  78. 0, 1, 1, 2, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 8,
  79. 8, 9, 10, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22,
  80. 22, 23, 23, 24, 24, 25, 25, 26, 26, 27, 27, 28, 28, 29, 29, 29,
  81. 30,
  82. 0, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 3,
  83. 4, 4, 4, 5, 5, 5, 6, 6, 7, 7, 7, 8, 8, 9, 9, 9,
  84. 10, 10, 11, 11, 11, 12, 12, 13, 13, 13, 13, 14, 14, 14, 15, 15,
  85. 15, 15, 16, 16, 16, 17, 17, 17, 18, 18, 18, 19, 19, 20, 20, 20,
  86. 21, 21, 22, 22, 23, 23, 24, 25, 26, 26, 27, 28, 29, 30, 31, 32,
  87. 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 42, 43, 44, 44, 45, 45,
  88. 46, 47, 47, 48, 48, 49, 49, 50, 50, 50, 51, 51, 51, 52, 52, 52,
  89. 53, 53, 53, 54, 54, 54, 55, 55, 55, 56, 56, 56, 57, 57, 57, 57,
  90. 58, 58, 58, 58, 59, 59, 59, 59, 60, 60, 60, 60, 60, 61, 61, 61,
  91. 61, 61, 61, 61, 62,
  92. };
  93. static const float quant_lut_mul[7] = { 0.0, 0.0, 2.0, 2.0, 5.0, 12.0, 36.6 };
  94. static const float quant_lut_add[7] = { 0.0, 0.0, 2.0, 7.0, 21.0, 56.0, 157.0 };
  95. static const uint8_t quant_lut_offset[8] = { 0, 0, 1, 4, 11, 32, 81, 230 };
  96. void apply_mdct(NellyMoserEncodeContext *s)
  97. {
  98. DECLARE_ALIGNED_16(float, in_buff[NELLY_SAMPLES]);
  99. memcpy(in_buff, s->buf[s->bufsel], NELLY_BUF_LEN * sizeof(float));
  100. s->dsp.vector_fmul(in_buff, ff_sine_128, NELLY_BUF_LEN);
  101. s->dsp.vector_fmul_reverse(in_buff + NELLY_BUF_LEN, s->buf[s->bufsel] + NELLY_BUF_LEN, ff_sine_128,
  102. NELLY_BUF_LEN);
  103. ff_mdct_calc(&s->mdct_ctx, s->mdct_out, in_buff);
  104. s->dsp.vector_fmul(s->buf[s->bufsel] + NELLY_BUF_LEN, ff_sine_128, NELLY_BUF_LEN);
  105. s->dsp.vector_fmul_reverse(s->buf[s->bufsel] + 2 * NELLY_BUF_LEN, s->buf[1 - s->bufsel], ff_sine_128,
  106. NELLY_BUF_LEN);
  107. ff_mdct_calc(&s->mdct_ctx, s->mdct_out + NELLY_BUF_LEN, s->buf[s->bufsel] + NELLY_BUF_LEN);
  108. }
  109. static av_cold int encode_init(AVCodecContext *avctx)
  110. {
  111. NellyMoserEncodeContext *s = avctx->priv_data;
  112. int i;
  113. if (avctx->channels != 1) {
  114. av_log(avctx, AV_LOG_ERROR, "Nellymoser supports only 1 channel\n");
  115. return -1;
  116. }
  117. if (avctx->sample_rate != 8000 && avctx->sample_rate != 11025 &&
  118. avctx->sample_rate != 22050 && avctx->sample_rate != 44100 &&
  119. avctx->strict_std_compliance >= FF_COMPLIANCE_NORMAL) {
  120. av_log(avctx, AV_LOG_ERROR, "Nellymoser works only with 8000, 11025, 22050 and 44100 sample rate\n");
  121. return -1;
  122. }
  123. avctx->frame_size = NELLY_SAMPLES;
  124. s->avctx = avctx;
  125. ff_mdct_init(&s->mdct_ctx, 8, 0);
  126. dsputil_init(&s->dsp, avctx);
  127. /* Generate overlap window */
  128. ff_sine_window_init(ff_sine_128, 128);
  129. for (i = 0; i < POW_TABLE_SIZE; i++)
  130. pow_table[i] = -pow(2, -i / 2048.0 - 3.0 + POW_TABLE_OFFSET);
  131. if (s->avctx->trellis) {
  132. s->opt = av_malloc(NELLY_BANDS * OPT_SIZE * sizeof(float ));
  133. s->path = av_malloc(NELLY_BANDS * OPT_SIZE * sizeof(uint8_t));
  134. }
  135. return 0;
  136. }
  137. static av_cold int encode_end(AVCodecContext *avctx)
  138. {
  139. NellyMoserEncodeContext *s = avctx->priv_data;
  140. ff_mdct_end(&s->mdct_ctx);
  141. if (s->avctx->trellis) {
  142. av_free(s->opt);
  143. av_free(s->path);
  144. }
  145. return 0;
  146. }
  147. #define find_best(val, table, LUT, LUT_add, LUT_size) \
  148. best_idx = \
  149. LUT[av_clip ((lrintf(val) >> 8) + LUT_add, 0, LUT_size - 1)]; \
  150. if (fabs(val - table[best_idx]) > fabs(val - table[best_idx + 1])) \
  151. best_idx++;
  152. static void get_exponent_greedy(NellyMoserEncodeContext *s, float *cand, int *idx_table)
  153. {
  154. int band, best_idx, power_idx = 0;
  155. float power_candidate;
  156. //base exponent
  157. find_best(cand[0], ff_nelly_init_table, sf_lut, -20, 96);
  158. idx_table[0] = best_idx;
  159. power_idx = ff_nelly_init_table[best_idx];
  160. for (band = 1; band < NELLY_BANDS; band++) {
  161. power_candidate = cand[band] - power_idx;
  162. find_best(power_candidate, ff_nelly_delta_table, sf_delta_lut, 37, 78);
  163. idx_table[band] = best_idx;
  164. power_idx += ff_nelly_delta_table[best_idx];
  165. }
  166. }
  167. static inline float distance(float x, float y, int band)
  168. {
  169. //return pow(fabs(x-y), 2.0);
  170. float tmp = x - y;
  171. return tmp * tmp;
  172. }
  173. static void get_exponent_dynamic(NellyMoserEncodeContext *s, float *cand, int *idx_table)
  174. {
  175. int i, j, band, best_idx;
  176. float power_candidate, best_val;
  177. float (*opt )[NELLY_BANDS] = s->opt ;
  178. uint8_t(*path)[NELLY_BANDS] = s->path;
  179. for (i = 0; i < NELLY_BANDS * OPT_SIZE; i++) {
  180. opt[0][i] = INFINITY;
  181. }
  182. for (i = 0; i < 64; i++) {
  183. opt[0][ff_nelly_init_table[i]] = distance(cand[0], ff_nelly_init_table[i], 0);
  184. path[0][ff_nelly_init_table[i]] = i;
  185. }
  186. for (band = 1; band < NELLY_BANDS; band++) {
  187. int q, c = 0;
  188. float tmp;
  189. int idx_min, idx_max, idx;
  190. power_candidate = cand[band];
  191. for (q = 1000; !c && q < OPT_SIZE; q <<= 2) {
  192. idx_min = FFMAX(0, cand[band] - q);
  193. idx_max = FFMIN(OPT_SIZE, cand[band - 1] + q);
  194. for (i = FFMAX(0, cand[band - 1] - q); i < FFMIN(OPT_SIZE, cand[band - 1] + q); i++) {
  195. if ( isinf(opt[band - 1][i]) )
  196. continue;
  197. for (j = 0; j < 32; j++) {
  198. idx = i + ff_nelly_delta_table[j];
  199. if (idx > idx_max)
  200. break;
  201. if (idx >= idx_min) {
  202. tmp = opt[band - 1][i] + distance(idx, power_candidate, band);
  203. if (opt[band][idx] > tmp) {
  204. opt[band][idx] = tmp;
  205. path[band][idx] = j;
  206. c = 1;
  207. }
  208. }
  209. }
  210. }
  211. }
  212. assert(c); //FIXME
  213. }
  214. best_val = INFINITY;
  215. best_idx = -1;
  216. band = NELLY_BANDS - 1;
  217. for (i = 0; i < OPT_SIZE; i++) {
  218. if (best_val > opt[band][i]) {
  219. best_val = opt[band][i];
  220. best_idx = i;
  221. }
  222. }
  223. for (band = NELLY_BANDS - 1; band >= 0; band--) {
  224. idx_table[band] = path[band][best_idx];
  225. if (band) {
  226. best_idx -= ff_nelly_delta_table[path[band][best_idx]];
  227. }
  228. }
  229. }
  230. /**
  231. * Encodes NELLY_SAMPLES samples. It assumes, that samples contains 3 * NELLY_BUF_LEN values
  232. * @param s encoder context
  233. * @param output output buffer
  234. * @param output_size size of output buffer
  235. */
  236. static void encode_block(NellyMoserEncodeContext *s, unsigned char *output, int output_size)
  237. {
  238. PutBitContext pb;
  239. int i, j, band, block, best_idx, power_idx = 0;
  240. float power_val, coeff, coeff_sum;
  241. float pows[NELLY_FILL_LEN];
  242. int bits[NELLY_BUF_LEN], idx_table[NELLY_BANDS];
  243. float cand[NELLY_BANDS];
  244. apply_mdct(s);
  245. init_put_bits(&pb, output, output_size * 8);
  246. i = 0;
  247. for (band = 0; band < NELLY_BANDS; band++) {
  248. coeff_sum = 0;
  249. for (j = 0; j < ff_nelly_band_sizes_table[band]; i++, j++) {
  250. coeff_sum += s->mdct_out[i ] * s->mdct_out[i ]
  251. + s->mdct_out[i + NELLY_BUF_LEN] * s->mdct_out[i + NELLY_BUF_LEN];
  252. }
  253. cand[band] =
  254. log(FFMAX(1.0, coeff_sum / (ff_nelly_band_sizes_table[band] << 7))) * 1024.0 / M_LN2;
  255. }
  256. if (s->avctx->trellis) {
  257. get_exponent_dynamic(s, cand, idx_table);
  258. } else {
  259. get_exponent_greedy(s, cand, idx_table);
  260. }
  261. i = 0;
  262. for (band = 0; band < NELLY_BANDS; band++) {
  263. if (band) {
  264. power_idx += ff_nelly_delta_table[idx_table[band]];
  265. put_bits(&pb, 5, idx_table[band]);
  266. } else {
  267. power_idx = ff_nelly_init_table[idx_table[0]];
  268. put_bits(&pb, 6, idx_table[0]);
  269. }
  270. power_val = pow_table[power_idx & 0x7FF] / (1 << ((power_idx >> 11) + POW_TABLE_OFFSET));
  271. for (j = 0; j < ff_nelly_band_sizes_table[band]; i++, j++) {
  272. s->mdct_out[i] *= power_val;
  273. s->mdct_out[i + NELLY_BUF_LEN] *= power_val;
  274. pows[i] = power_idx;
  275. }
  276. }
  277. ff_nelly_get_sample_bits(pows, bits);
  278. for (block = 0; block < 2; block++) {
  279. for (i = 0; i < NELLY_FILL_LEN; i++) {
  280. if (bits[i] > 0) {
  281. const float *table = ff_nelly_dequantization_table + (1 << bits[i]) - 1;
  282. coeff = s->mdct_out[block * NELLY_BUF_LEN + i];
  283. best_idx =
  284. quant_lut[av_clip (
  285. coeff * quant_lut_mul[bits[i]] + quant_lut_add[bits[i]],
  286. quant_lut_offset[bits[i]],
  287. quant_lut_offset[bits[i]+1] - 1
  288. )];
  289. if (fabs(coeff - table[best_idx]) > fabs(coeff - table[best_idx + 1]))
  290. best_idx++;
  291. put_bits(&pb, bits[i], best_idx);
  292. }
  293. }
  294. if (!block)
  295. put_bits(&pb, NELLY_HEADER_BITS + NELLY_DETAIL_BITS - put_bits_count(&pb), 0);
  296. }
  297. flush_put_bits(&pb);
  298. }
  299. static int encode_frame(AVCodecContext *avctx, uint8_t *frame, int buf_size, void *data)
  300. {
  301. NellyMoserEncodeContext *s = avctx->priv_data;
  302. int16_t *samples = data;
  303. int i;
  304. if (s->last_frame)
  305. return 0;
  306. if (data) {
  307. for (i = 0; i < avctx->frame_size; i++) {
  308. s->buf[s->bufsel][i] = samples[i];
  309. }
  310. for (; i < NELLY_SAMPLES; i++) {
  311. s->buf[s->bufsel][i] = 0;
  312. }
  313. s->bufsel = 1 - s->bufsel;
  314. if (!s->have_saved) {
  315. s->have_saved = 1;
  316. return 0;
  317. }
  318. } else {
  319. memset(s->buf[s->bufsel], 0, sizeof(s->buf[0][0]) * NELLY_BUF_LEN);
  320. s->bufsel = 1 - s->bufsel;
  321. s->last_frame = 1;
  322. }
  323. if (s->have_saved) {
  324. encode_block(s, frame, buf_size);
  325. return NELLY_BLOCK_LEN;
  326. }
  327. return 0;
  328. }
  329. AVCodec nellymoser_encoder = {
  330. .name = "nellymoser",
  331. .type = CODEC_TYPE_AUDIO,
  332. .id = CODEC_ID_NELLYMOSER,
  333. .priv_data_size = sizeof(NellyMoserEncodeContext),
  334. .init = encode_init,
  335. .encode = encode_frame,
  336. .close = encode_end,
  337. .capabilities = CODEC_CAP_SMALL_LAST_FRAME | CODEC_CAP_DELAY,
  338. .long_name = NULL_IF_CONFIG_SMALL("Nellymoser Asao"),
  339. };