nellymoserenc.c 13 KB

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