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.

147 lines
4.3KB

  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 AC3ENC_FLOAT 0
  28. #define FFT_FLOAT 0
  29. #define FFT_FIXED_32 1
  30. #include "internal.h"
  31. #include "audiodsp.h"
  32. #include "ac3enc.h"
  33. #include "eac3enc.h"
  34. #include "kbdwin.h"
  35. static const AVClass ac3enc_class = {
  36. .class_name = "Fixed-Point AC-3 Encoder",
  37. .item_name = av_default_item_name,
  38. .option = ff_ac3_enc_options,
  39. .version = LIBAVUTIL_VERSION_INT,
  40. };
  41. static void sum_square_butterfly(AC3EncodeContext *s, int64_t sum[4],
  42. const int32_t *coef0, const int32_t *coef1,
  43. int len)
  44. {
  45. s->ac3dsp.sum_square_butterfly_int32(sum, coef0, coef1, len);
  46. }
  47. /*
  48. * Clip MDCT coefficients to allowable range.
  49. */
  50. static void clip_coefficients(AudioDSPContext *adsp, int32_t *coef,
  51. unsigned int len)
  52. {
  53. adsp->vector_clip_int32(coef, coef, COEF_MIN, COEF_MAX, len);
  54. }
  55. /*
  56. * Calculate a single coupling coordinate.
  57. */
  58. static CoefType calc_cpl_coord(CoefSumType energy_ch, CoefSumType energy_cpl)
  59. {
  60. if (energy_cpl <= COEF_MAX) {
  61. return 1048576;
  62. } else {
  63. uint64_t coord = energy_ch / (energy_cpl >> 24);
  64. uint32_t coord32 = FFMIN(coord, 1073741824);
  65. coord32 = ff_sqrt(coord32) << 9;
  66. return FFMIN(coord32, COEF_MAX);
  67. }
  68. }
  69. #include "ac3enc_template.c"
  70. /**
  71. * Finalize MDCT and free allocated memory.
  72. *
  73. * @param s AC-3 encoder private context
  74. */
  75. static av_cold void ac3_fixed_mdct_end(AC3EncodeContext *s)
  76. {
  77. ff_mdct_end(&s->mdct);
  78. }
  79. /**
  80. * Initialize MDCT tables.
  81. *
  82. * @param s AC-3 encoder private context
  83. * @return 0 on success, negative error code on failure
  84. */
  85. static av_cold int ac3_fixed_mdct_init(AC3EncodeContext *s)
  86. {
  87. float fwin[AC3_BLOCK_SIZE];
  88. int32_t *iwin = av_malloc_array(AC3_BLOCK_SIZE, sizeof(*iwin));
  89. if (!iwin)
  90. return AVERROR(ENOMEM);
  91. ff_kbd_window_init(fwin, 5.0, AC3_BLOCK_SIZE);
  92. for (int i = 0; i < AC3_BLOCK_SIZE; i++)
  93. iwin[i] = lrintf(fwin[i] * (1 << 22));
  94. s->mdct_window = iwin;
  95. s->fdsp = avpriv_alloc_fixed_dsp(s->avctx->flags & AV_CODEC_FLAG_BITEXACT);
  96. if (!s->fdsp)
  97. return AVERROR(ENOMEM);
  98. return ff_mdct_init(&s->mdct, 9, 0, -1.0);
  99. }
  100. static av_cold int ac3_fixed_encode_init(AVCodecContext *avctx)
  101. {
  102. AC3EncodeContext *s = avctx->priv_data;
  103. s->fixed_point = 1;
  104. s->mdct_end = ac3_fixed_mdct_end;
  105. s->mdct_init = ac3_fixed_mdct_init;
  106. s->allocate_sample_buffers = allocate_sample_buffers;
  107. return ff_ac3_encode_init(avctx);
  108. }
  109. AVCodec ff_ac3_fixed_encoder = {
  110. .name = "ac3_fixed",
  111. .long_name = NULL_IF_CONFIG_SMALL("ATSC A/52A (AC-3)"),
  112. .type = AVMEDIA_TYPE_AUDIO,
  113. .id = AV_CODEC_ID_AC3,
  114. .priv_data_size = sizeof(AC3EncodeContext),
  115. .init = ac3_fixed_encode_init,
  116. .encode2 = ff_ac3_fixed_encode_frame,
  117. .close = ff_ac3_encode_close,
  118. .sample_fmts = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_S32P,
  119. AV_SAMPLE_FMT_NONE },
  120. .priv_class = &ac3enc_class,
  121. .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE | FF_CODEC_CAP_INIT_CLEANUP,
  122. .supported_samplerates = ff_ac3_sample_rate_tab,
  123. .channel_layouts = ff_ac3_channel_layouts,
  124. .defaults = ff_ac3_enc_defaults,
  125. };