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.

145 lines
4.8KB

  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 nellymoserenc.c
  25. * Nellymoser encoder
  26. * by Bartlomiej Wolowiec
  27. *
  28. * Generic codec information: libavcodec/nellymoserdec.c
  29. *
  30. * Some information also from: http://www1.mplayerhq.hu/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. typedef struct NellyMoserEncodeContext {
  44. AVCodecContext *avctx;
  45. int last_frame;
  46. } NellyMoserEncodeContext;
  47. static float pow_table[POW_TABLE_SIZE]; ///< -pow(2, -i / 2048.0 - 3.0);
  48. static const uint8_t sf_lut[96] = {
  49. 0, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 4, 4,
  50. 5, 5, 5, 6, 7, 7, 8, 8, 9, 10, 11, 11, 12, 13, 13, 14,
  51. 15, 15, 16, 17, 17, 18, 19, 19, 20, 21, 22, 22, 23, 24, 25, 26,
  52. 27, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 37, 38, 39, 40,
  53. 41, 41, 42, 43, 44, 45, 45, 46, 47, 48, 49, 50, 51, 52, 52, 53,
  54. 54, 55, 55, 56, 57, 57, 58, 59, 59, 60, 60, 60, 61, 61, 61, 62,
  55. };
  56. static const uint8_t sf_delta_lut[78] = {
  57. 0, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 4, 4,
  58. 4, 5, 5, 5, 6, 6, 7, 7, 8, 8, 9, 10, 10, 11, 11, 12,
  59. 13, 13, 14, 15, 16, 17, 17, 18, 19, 19, 20, 21, 21, 22, 22, 23,
  60. 23, 24, 24, 25, 25, 25, 26, 26, 26, 26, 27, 27, 27, 27, 27, 28,
  61. 28, 28, 28, 28, 28, 29, 29, 29, 29, 29, 29, 29, 29, 30,
  62. };
  63. static const uint8_t quant_lut[230] = {
  64. 0,
  65. 0, 1, 2,
  66. 0, 1, 2, 3, 4, 5, 6,
  67. 0, 1, 1, 2, 2, 3, 3, 4, 5, 6, 7, 8, 9, 10, 11, 11,
  68. 12, 13, 13, 13, 14,
  69. 0, 1, 1, 2, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 8,
  70. 8, 9, 10, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22,
  71. 22, 23, 23, 24, 24, 25, 25, 26, 26, 27, 27, 28, 28, 29, 29, 29,
  72. 30,
  73. 0, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 3,
  74. 4, 4, 4, 5, 5, 5, 6, 6, 7, 7, 7, 8, 8, 9, 9, 9,
  75. 10, 10, 11, 11, 11, 12, 12, 13, 13, 13, 13, 14, 14, 14, 15, 15,
  76. 15, 15, 16, 16, 16, 17, 17, 17, 18, 18, 18, 19, 19, 20, 20, 20,
  77. 21, 21, 22, 22, 23, 23, 24, 25, 26, 26, 27, 28, 29, 30, 31, 32,
  78. 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 42, 43, 44, 44, 45, 45,
  79. 46, 47, 47, 48, 48, 49, 49, 50, 50, 50, 51, 51, 51, 52, 52, 52,
  80. 53, 53, 53, 54, 54, 54, 55, 55, 55, 56, 56, 56, 57, 57, 57, 57,
  81. 58, 58, 58, 58, 59, 59, 59, 59, 60, 60, 60, 60, 60, 61, 61, 61,
  82. 61, 61, 61, 61, 62,
  83. };
  84. static const float quant_lut_mul[7] = { 0.0, 0.0, 2.0, 2.0, 5.0, 12.0, 36.6 };
  85. static const float quant_lut_add[7] = { 0.0, 0.0, 2.0, 7.0, 21.0, 56.0, 157.0 };
  86. static const uint8_t quant_lut_offset[8] = { 0, 0, 1, 4, 11, 32, 81, 230 };
  87. static av_cold int encode_init(AVCodecContext *avctx)
  88. {
  89. NellyMoserEncodeContext *s = avctx->priv_data;
  90. int i;
  91. if (avctx->channels != 1) {
  92. av_log(avctx, AV_LOG_ERROR, "Nellymoser supports only 1 channel\n");
  93. return -1;
  94. }
  95. avctx->frame_size = NELLY_SAMPLES;
  96. s->avctx = avctx;
  97. ff_mdct_init(&s->mdct_ctx, 8, 0);
  98. dsputil_init(&s->dsp, avctx);
  99. /* Generate overlap window */
  100. ff_sine_window_init(ff_sine_128, 128);
  101. for (i = 0; i < POW_TABLE_SIZE; i++)
  102. pow_table[i] = -pow(2, -i / 2048.0 - 3.0 + POW_TABLE_OFFSET);
  103. return 0;
  104. }
  105. static av_cold int encode_end(AVCodecContext *avctx)
  106. {
  107. NellyMoserEncodeContext *s = avctx->priv_data;
  108. ff_mdct_end(&s->mdct_ctx);
  109. return 0;
  110. }
  111. AVCodec nellymoser_encoder = {
  112. .name = "nellymoser",
  113. .type = CODEC_TYPE_AUDIO,
  114. .id = CODEC_ID_NELLYMOSER,
  115. .priv_data_size = sizeof(NellyMoserEncodeContext),
  116. .init = encode_init,
  117. .encode = encode_frame,
  118. .close = encode_end,
  119. .capabilities = CODEC_CAP_SMALL_LAST_FRAME | CODEC_CAP_DELAY,
  120. .long_name = NULL_IF_CONFIG_SMALL("Nellymoser Asao Codec"),
  121. };