ac3enc_fixed.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*
  2. * The simplest AC-3 encoder
  3. * Copyright (c) 2000 Fabrice Bellard
  4. * Copyright (c) 2006-2010 Justin Ruggles <justin.ruggles@gmail.com>
  5. * Copyright (c) 2006-2010 Prakash Punnoor <prakash@punnoor.de>
  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. * fixed-point AC-3 encoder.
  26. */
  27. #define CONFIG_FFT_FLOAT 0
  28. #undef CONFIG_AC3ENC_FLOAT
  29. #include "ac3enc.c"
  30. /**
  31. * Finalize MDCT and free allocated memory.
  32. */
  33. static av_cold void mdct_end(AC3MDCTContext *mdct)
  34. {
  35. ff_fft_end(&mdct->fft);
  36. }
  37. /**
  38. * Initialize MDCT tables.
  39. * @param nbits log2(MDCT size)
  40. */
  41. static av_cold int mdct_init(AVCodecContext *avctx, AC3MDCTContext *mdct,
  42. int nbits)
  43. {
  44. int ret = ff_mdct_init(&mdct->fft, nbits, 0, 1.0);
  45. mdct->window = ff_ac3_window;
  46. return ret;
  47. }
  48. /**
  49. * Apply KBD window to input samples prior to MDCT.
  50. */
  51. static void apply_window(DSPContext *dsp, int16_t *output, const int16_t *input,
  52. const int16_t *window, unsigned int len)
  53. {
  54. dsp->apply_window_int16(output, input, window, len);
  55. }
  56. /**
  57. * Calculate the log2() of the maximum absolute value in an array.
  58. * @param tab input array
  59. * @param n number of values in the array
  60. * @return log2(max(abs(tab[])))
  61. */
  62. static int log2_tab(AC3EncodeContext *s, int16_t *src, int len)
  63. {
  64. int v = s->ac3dsp.ac3_max_msb_abs_int16(src, len);
  65. return av_log2(v);
  66. }
  67. /**
  68. * Normalize the input samples to use the maximum available precision.
  69. * This assumes signed 16-bit input samples.
  70. *
  71. * @return exponent shift
  72. */
  73. static int normalize_samples(AC3EncodeContext *s)
  74. {
  75. int v = 14 - log2_tab(s, s->windowed_samples, AC3_WINDOW_SIZE);
  76. if (v > 0)
  77. s->ac3dsp.ac3_lshift_int16(s->windowed_samples, AC3_WINDOW_SIZE, v);
  78. /* +6 to right-shift from 31-bit to 25-bit */
  79. return v + 6;
  80. }
  81. /**
  82. * Scale MDCT coefficients to 25-bit signed fixed-point.
  83. */
  84. static void scale_coefficients(AC3EncodeContext *s)
  85. {
  86. int blk, ch;
  87. for (blk = 0; blk < AC3_MAX_BLOCKS; blk++) {
  88. AC3Block *block = &s->blocks[blk];
  89. for (ch = 0; ch < s->channels; ch++) {
  90. s->ac3dsp.ac3_rshift_int32(block->mdct_coef[ch], AC3_MAX_COEFS,
  91. block->coeff_shift[ch]);
  92. }
  93. }
  94. }
  95. AVCodec ff_ac3_fixed_encoder = {
  96. "ac3_fixed",
  97. AVMEDIA_TYPE_AUDIO,
  98. CODEC_ID_AC3,
  99. sizeof(AC3EncodeContext),
  100. ac3_encode_init,
  101. ac3_encode_frame,
  102. ac3_encode_close,
  103. NULL,
  104. .sample_fmts = (const enum AVSampleFormat[]){AV_SAMPLE_FMT_S16,AV_SAMPLE_FMT_NONE},
  105. .long_name = NULL_IF_CONFIG_SMALL("ATSC A/52A (AC-3)"),
  106. .priv_class = &ac3enc_class,
  107. .channel_layouts = ac3_channel_layouts,
  108. };