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.

2567 lines
91KB

  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. * The simplest AC-3 encoder.
  26. */
  27. #include <stdint.h>
  28. #include "libavutil/attributes.h"
  29. #include "libavutil/avassert.h"
  30. #include "libavutil/avstring.h"
  31. #include "libavutil/channel_layout.h"
  32. #include "libavutil/crc.h"
  33. #include "libavutil/internal.h"
  34. #include "libavutil/mem_internal.h"
  35. #include "libavutil/opt.h"
  36. #include "libavutil/thread.h"
  37. #include "avcodec.h"
  38. #include "internal.h"
  39. #include "me_cmp.h"
  40. #include "put_bits.h"
  41. #include "audiodsp.h"
  42. #include "ac3dsp.h"
  43. #include "ac3.h"
  44. #include "fft.h"
  45. #include "ac3enc.h"
  46. #include "eac3enc.h"
  47. typedef struct AC3Mant {
  48. int16_t *qmant1_ptr, *qmant2_ptr, *qmant4_ptr; ///< mantissa pointers for bap=1,2,4
  49. int mant1_cnt, mant2_cnt, mant4_cnt; ///< mantissa counts for bap=1,2,4
  50. } AC3Mant;
  51. #define CMIXLEV_NUM_OPTIONS 3
  52. static const float cmixlev_options[CMIXLEV_NUM_OPTIONS] = {
  53. LEVEL_MINUS_3DB, LEVEL_MINUS_4POINT5DB, LEVEL_MINUS_6DB
  54. };
  55. #define SURMIXLEV_NUM_OPTIONS 3
  56. static const float surmixlev_options[SURMIXLEV_NUM_OPTIONS] = {
  57. LEVEL_MINUS_3DB, LEVEL_MINUS_6DB, LEVEL_ZERO
  58. };
  59. #define EXTMIXLEV_NUM_OPTIONS 8
  60. static const float extmixlev_options[EXTMIXLEV_NUM_OPTIONS] = {
  61. LEVEL_PLUS_3DB, LEVEL_PLUS_1POINT5DB, LEVEL_ONE, LEVEL_MINUS_1POINT5DB,
  62. LEVEL_MINUS_3DB, LEVEL_MINUS_4POINT5DB, LEVEL_MINUS_6DB, LEVEL_ZERO
  63. };
  64. /* The first two options apply only to the AC-3 encoders;
  65. * the rest is also valid for EAC-3. When modifying it,
  66. * it might be necessary to adapt said offset in eac3enc.c. */
  67. #define OFFSET(param) offsetof(AC3EncodeContext, options.param)
  68. #define AC3ENC_PARAM (AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_ENCODING_PARAM)
  69. const AVOption ff_ac3_enc_options[] = {
  70. /* AC-3 downmix levels */
  71. {"center_mixlev", "Center Mix Level", OFFSET(center_mix_level), AV_OPT_TYPE_FLOAT, {.dbl = LEVEL_MINUS_4POINT5DB }, 0.0, 1.0, AC3ENC_PARAM},
  72. {"surround_mixlev", "Surround Mix Level", OFFSET(surround_mix_level), AV_OPT_TYPE_FLOAT, {.dbl = LEVEL_MINUS_6DB }, 0.0, 1.0, AC3ENC_PARAM},
  73. /* audio production information */
  74. {"mixing_level", "Mixing Level", OFFSET(mixing_level), AV_OPT_TYPE_INT, {.i64 = AC3ENC_OPT_NONE }, AC3ENC_OPT_NONE, 111, AC3ENC_PARAM},
  75. {"room_type", "Room Type", OFFSET(room_type), AV_OPT_TYPE_INT, {.i64 = AC3ENC_OPT_NONE }, AC3ENC_OPT_NONE, AC3ENC_OPT_SMALL_ROOM, AC3ENC_PARAM, "room_type"},
  76. {"notindicated", "Not Indicated (default)", 0, AV_OPT_TYPE_CONST, {.i64 = AC3ENC_OPT_NOT_INDICATED }, INT_MIN, INT_MAX, AC3ENC_PARAM, "room_type"},
  77. {"large", "Large Room", 0, AV_OPT_TYPE_CONST, {.i64 = AC3ENC_OPT_LARGE_ROOM }, INT_MIN, INT_MAX, AC3ENC_PARAM, "room_type"},
  78. {"small", "Small Room", 0, AV_OPT_TYPE_CONST, {.i64 = AC3ENC_OPT_SMALL_ROOM }, INT_MIN, INT_MAX, AC3ENC_PARAM, "room_type"},
  79. /* Metadata Options */
  80. {"per_frame_metadata", "Allow Changing Metadata Per-Frame", OFFSET(allow_per_frame_metadata), AV_OPT_TYPE_BOOL, {.i64 = 0 }, 0, 1, AC3ENC_PARAM},
  81. {"copyright", "Copyright Bit", OFFSET(copyright), AV_OPT_TYPE_INT, {.i64 = AC3ENC_OPT_NONE }, AC3ENC_OPT_NONE, 1, AC3ENC_PARAM},
  82. {"dialnorm", "Dialogue Level (dB)", OFFSET(dialogue_level), AV_OPT_TYPE_INT, {.i64 = -31 }, -31, -1, AC3ENC_PARAM},
  83. {"dsur_mode", "Dolby Surround Mode", OFFSET(dolby_surround_mode), AV_OPT_TYPE_INT, {.i64 = AC3ENC_OPT_NONE }, AC3ENC_OPT_NONE, AC3ENC_OPT_MODE_ON, AC3ENC_PARAM, "dsur_mode"},
  84. {"notindicated", "Not Indicated (default)", 0, AV_OPT_TYPE_CONST, {.i64 = AC3ENC_OPT_NOT_INDICATED }, INT_MIN, INT_MAX, AC3ENC_PARAM, "dsur_mode"},
  85. {"on", "Dolby Surround Encoded", 0, AV_OPT_TYPE_CONST, {.i64 = AC3ENC_OPT_MODE_ON }, INT_MIN, INT_MAX, AC3ENC_PARAM, "dsur_mode"},
  86. {"off", "Not Dolby Surround Encoded", 0, AV_OPT_TYPE_CONST, {.i64 = AC3ENC_OPT_MODE_OFF }, INT_MIN, INT_MAX, AC3ENC_PARAM, "dsur_mode"},
  87. {"original", "Original Bit Stream", OFFSET(original), AV_OPT_TYPE_INT, {.i64 = AC3ENC_OPT_NONE }, AC3ENC_OPT_NONE, 1, AC3ENC_PARAM},
  88. /* extended bitstream information */
  89. {"dmix_mode", "Preferred Stereo Downmix Mode", OFFSET(preferred_stereo_downmix), AV_OPT_TYPE_INT, {.i64 = AC3ENC_OPT_NONE }, AC3ENC_OPT_NONE, AC3ENC_OPT_DOWNMIX_DPLII, AC3ENC_PARAM, "dmix_mode"},
  90. {"notindicated", "Not Indicated (default)", 0, AV_OPT_TYPE_CONST, {.i64 = AC3ENC_OPT_NOT_INDICATED }, INT_MIN, INT_MAX, AC3ENC_PARAM, "dmix_mode"},
  91. {"ltrt", "Lt/Rt Downmix Preferred", 0, AV_OPT_TYPE_CONST, {.i64 = AC3ENC_OPT_DOWNMIX_LTRT }, INT_MIN, INT_MAX, AC3ENC_PARAM, "dmix_mode"},
  92. {"loro", "Lo/Ro Downmix Preferred", 0, AV_OPT_TYPE_CONST, {.i64 = AC3ENC_OPT_DOWNMIX_LORO }, INT_MIN, INT_MAX, AC3ENC_PARAM, "dmix_mode"},
  93. {"dplii", "Dolby Pro Logic II Downmix Preferred", 0, AV_OPT_TYPE_CONST, {.i64 = AC3ENC_OPT_DOWNMIX_DPLII }, INT_MIN, INT_MAX, AC3ENC_PARAM, "dmix_mode"},
  94. {"ltrt_cmixlev", "Lt/Rt Center Mix Level", OFFSET(ltrt_center_mix_level), AV_OPT_TYPE_FLOAT, {.dbl = -1.0 }, -1.0, 2.0, AC3ENC_PARAM},
  95. {"ltrt_surmixlev", "Lt/Rt Surround Mix Level", OFFSET(ltrt_surround_mix_level), AV_OPT_TYPE_FLOAT, {.dbl = -1.0 }, -1.0, 2.0, AC3ENC_PARAM},
  96. {"loro_cmixlev", "Lo/Ro Center Mix Level", OFFSET(loro_center_mix_level), AV_OPT_TYPE_FLOAT, {.dbl = -1.0 }, -1.0, 2.0, AC3ENC_PARAM},
  97. {"loro_surmixlev", "Lo/Ro Surround Mix Level", OFFSET(loro_surround_mix_level), AV_OPT_TYPE_FLOAT, {.dbl = -1.0 }, -1.0, 2.0, AC3ENC_PARAM},
  98. {"dsurex_mode", "Dolby Surround EX Mode", OFFSET(dolby_surround_ex_mode), AV_OPT_TYPE_INT, {.i64 = AC3ENC_OPT_NONE }, AC3ENC_OPT_NONE, AC3ENC_OPT_DSUREX_DPLIIZ, AC3ENC_PARAM, "dsurex_mode"},
  99. {"notindicated", "Not Indicated (default)", 0, AV_OPT_TYPE_CONST, {.i64 = AC3ENC_OPT_NOT_INDICATED }, INT_MIN, INT_MAX, AC3ENC_PARAM, "dsurex_mode"},
  100. {"on", "Dolby Surround EX Encoded", 0, AV_OPT_TYPE_CONST, {.i64 = AC3ENC_OPT_MODE_ON }, INT_MIN, INT_MAX, AC3ENC_PARAM, "dsurex_mode"},
  101. {"off", "Not Dolby Surround EX Encoded", 0, AV_OPT_TYPE_CONST, {.i64 = AC3ENC_OPT_MODE_OFF }, INT_MIN, INT_MAX, AC3ENC_PARAM, "dsurex_mode"},
  102. {"dpliiz", "Dolby Pro Logic IIz-encoded", 0, AV_OPT_TYPE_CONST, {.i64 = AC3ENC_OPT_DSUREX_DPLIIZ }, INT_MIN, INT_MAX, AC3ENC_PARAM, "dsurex_mode"},
  103. {"dheadphone_mode", "Dolby Headphone Mode", OFFSET(dolby_headphone_mode), AV_OPT_TYPE_INT, {.i64 = AC3ENC_OPT_NONE }, AC3ENC_OPT_NONE, AC3ENC_OPT_MODE_ON, AC3ENC_PARAM, "dheadphone_mode"},
  104. {"notindicated", "Not Indicated (default)", 0, AV_OPT_TYPE_CONST, {.i64 = AC3ENC_OPT_NOT_INDICATED }, INT_MIN, INT_MAX, AC3ENC_PARAM, "dheadphone_mode"},
  105. {"on", "Dolby Headphone Encoded", 0, AV_OPT_TYPE_CONST, {.i64 = AC3ENC_OPT_MODE_ON }, INT_MIN, INT_MAX, AC3ENC_PARAM, "dheadphone_mode"},
  106. {"off", "Not Dolby Headphone Encoded", 0, AV_OPT_TYPE_CONST, {.i64 = AC3ENC_OPT_MODE_OFF }, INT_MIN, INT_MAX, AC3ENC_PARAM, "dheadphone_mode"},
  107. {"ad_conv_type", "A/D Converter Type", OFFSET(ad_converter_type), AV_OPT_TYPE_INT, {.i64 = AC3ENC_OPT_NONE }, AC3ENC_OPT_NONE, AC3ENC_OPT_ADCONV_HDCD, AC3ENC_PARAM, "ad_conv_type"},
  108. {"standard", "Standard (default)", 0, AV_OPT_TYPE_CONST, {.i64 = AC3ENC_OPT_ADCONV_STANDARD }, INT_MIN, INT_MAX, AC3ENC_PARAM, "ad_conv_type"},
  109. {"hdcd", "HDCD", 0, AV_OPT_TYPE_CONST, {.i64 = AC3ENC_OPT_ADCONV_HDCD }, INT_MIN, INT_MAX, AC3ENC_PARAM, "ad_conv_type"},
  110. /* Other Encoding Options */
  111. {"stereo_rematrixing", "Stereo Rematrixing", OFFSET(stereo_rematrixing), AV_OPT_TYPE_BOOL, {.i64 = 1 }, 0, 1, AC3ENC_PARAM},
  112. {"channel_coupling", "Channel Coupling", OFFSET(channel_coupling), AV_OPT_TYPE_INT, {.i64 = AC3ENC_OPT_AUTO }, AC3ENC_OPT_AUTO, AC3ENC_OPT_ON, AC3ENC_PARAM, "channel_coupling"},
  113. {"auto", "Selected by the Encoder", 0, AV_OPT_TYPE_CONST, {.i64 = AC3ENC_OPT_AUTO }, INT_MIN, INT_MAX, AC3ENC_PARAM, "channel_coupling"},
  114. {"cpl_start_band", "Coupling Start Band", OFFSET(cpl_start), AV_OPT_TYPE_INT, {.i64 = AC3ENC_OPT_AUTO }, AC3ENC_OPT_AUTO, 15, AC3ENC_PARAM, "cpl_start_band"},
  115. {"auto", "Selected by the Encoder", 0, AV_OPT_TYPE_CONST, {.i64 = AC3ENC_OPT_AUTO }, INT_MIN, INT_MAX, AC3ENC_PARAM, "cpl_start_band"},
  116. {NULL}
  117. };
  118. const AVCodecDefault ff_ac3_enc_defaults[] = {
  119. { "b", "0" },
  120. { NULL }
  121. };
  122. /**
  123. * LUT for number of exponent groups.
  124. * exponent_group_tab[coupling][exponent strategy-1][number of coefficients]
  125. */
  126. static uint8_t exponent_group_tab[2][3][256];
  127. /**
  128. * List of supported channel layouts.
  129. */
  130. const uint64_t ff_ac3_channel_layouts[19] = {
  131. AV_CH_LAYOUT_MONO,
  132. AV_CH_LAYOUT_STEREO,
  133. AV_CH_LAYOUT_2_1,
  134. AV_CH_LAYOUT_SURROUND,
  135. AV_CH_LAYOUT_2_2,
  136. AV_CH_LAYOUT_QUAD,
  137. AV_CH_LAYOUT_4POINT0,
  138. AV_CH_LAYOUT_5POINT0,
  139. AV_CH_LAYOUT_5POINT0_BACK,
  140. (AV_CH_LAYOUT_MONO | AV_CH_LOW_FREQUENCY),
  141. (AV_CH_LAYOUT_STEREO | AV_CH_LOW_FREQUENCY),
  142. (AV_CH_LAYOUT_2_1 | AV_CH_LOW_FREQUENCY),
  143. (AV_CH_LAYOUT_SURROUND | AV_CH_LOW_FREQUENCY),
  144. (AV_CH_LAYOUT_2_2 | AV_CH_LOW_FREQUENCY),
  145. (AV_CH_LAYOUT_QUAD | AV_CH_LOW_FREQUENCY),
  146. (AV_CH_LAYOUT_4POINT0 | AV_CH_LOW_FREQUENCY),
  147. AV_CH_LAYOUT_5POINT1,
  148. AV_CH_LAYOUT_5POINT1_BACK,
  149. 0
  150. };
  151. /**
  152. * Table to remap channels from SMPTE order to AC-3 order.
  153. * [channel_mode][lfe][ch]
  154. */
  155. static const uint8_t ac3_enc_channel_map[8][2][6] = {
  156. COMMON_CHANNEL_MAP
  157. { { 0, 1, 2, 3, }, { 0, 1, 3, 4, 2, } },
  158. { { 0, 2, 1, 3, 4, }, { 0, 2, 1, 4, 5, 3 } },
  159. };
  160. /**
  161. * LUT to select the bandwidth code based on the bit rate, sample rate, and
  162. * number of full-bandwidth channels.
  163. * bandwidth_tab[fbw_channels-1][sample rate code][bit rate code]
  164. */
  165. static const uint8_t ac3_bandwidth_tab[5][3][19] = {
  166. // 32 40 48 56 64 80 96 112 128 160 192 224 256 320 384 448 512 576 640
  167. { { 0, 0, 0, 12, 16, 32, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48 },
  168. { 0, 0, 0, 16, 20, 36, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56 },
  169. { 0, 0, 0, 32, 40, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60 } },
  170. { { 0, 0, 0, 0, 0, 0, 0, 20, 24, 32, 48, 48, 48, 48, 48, 48, 48, 48, 48 },
  171. { 0, 0, 0, 0, 0, 0, 4, 24, 28, 36, 56, 56, 56, 56, 56, 56, 56, 56, 56 },
  172. { 0, 0, 0, 0, 0, 0, 20, 44, 52, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60 } },
  173. { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 16, 24, 32, 40, 48, 48, 48, 48, 48, 48 },
  174. { 0, 0, 0, 0, 0, 0, 0, 0, 4, 20, 28, 36, 44, 56, 56, 56, 56, 56, 56 },
  175. { 0, 0, 0, 0, 0, 0, 0, 0, 20, 40, 48, 60, 60, 60, 60, 60, 60, 60, 60 } },
  176. { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 12, 24, 32, 48, 48, 48, 48, 48, 48 },
  177. { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 16, 28, 36, 56, 56, 56, 56, 56, 56 },
  178. { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 32, 48, 60, 60, 60, 60, 60, 60, 60 } },
  179. { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 20, 32, 40, 48, 48, 48, 48 },
  180. { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 12, 24, 36, 44, 56, 56, 56, 56 },
  181. { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 28, 44, 60, 60, 60, 60, 60, 60 } }
  182. };
  183. /**
  184. * LUT to select the coupling start band based on the bit rate, sample rate, and
  185. * number of full-bandwidth channels. -1 = coupling off
  186. * ac3_coupling_start_tab[channel_mode-2][sample rate code][bit rate code]
  187. *
  188. * TODO: more testing for optimal parameters.
  189. * multi-channel tests at 44.1kHz and 32kHz.
  190. */
  191. static const int8_t ac3_coupling_start_tab[6][3][19] = {
  192. // 32 40 48 56 64 80 96 112 128 160 192 224 256 320 384 448 512 576 640
  193. // 2/0
  194. { { 0, 0, 0, 0, 0, 0, 0, 1, 1, 7, 8, 11, 12, -1, -1, -1, -1, -1, -1 },
  195. { 0, 0, 0, 0, 0, 0, 1, 3, 5, 7, 10, 12, 13, -1, -1, -1, -1, -1, -1 },
  196. { 0, 0, 0, 0, 1, 2, 2, 9, 13, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1 } },
  197. // 3/0
  198. { { 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 6, 9, 11, 12, 13, -1, -1, -1, -1 },
  199. { 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 6, 9, 11, 12, 13, -1, -1, -1, -1 },
  200. { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 } },
  201. // 2/1 - untested
  202. { { 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 6, 9, 11, 12, 13, -1, -1, -1, -1 },
  203. { 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 6, 9, 11, 12, 13, -1, -1, -1, -1 },
  204. { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 } },
  205. // 3/1
  206. { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 2, 10, 11, 11, 12, 12, 14, -1 },
  207. { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 2, 10, 11, 11, 12, 12, 14, -1 },
  208. { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 } },
  209. // 2/2 - untested
  210. { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 2, 10, 11, 11, 12, 12, 14, -1 },
  211. { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 2, 10, 11, 11, 12, 12, 14, -1 },
  212. { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 } },
  213. // 3/2
  214. { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 6, 8, 11, 12, 12, -1, -1 },
  215. { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 6, 8, 11, 12, 12, -1, -1 },
  216. { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 } },
  217. };
  218. /**
  219. * Adjust the frame size to make the average bit rate match the target bit rate.
  220. * This is only needed for 11025, 22050, and 44100 sample rates or any E-AC-3.
  221. *
  222. * @param s AC-3 encoder private context
  223. */
  224. void ff_ac3_adjust_frame_size(AC3EncodeContext *s)
  225. {
  226. while (s->bits_written >= s->bit_rate && s->samples_written >= s->sample_rate) {
  227. s->bits_written -= s->bit_rate;
  228. s->samples_written -= s->sample_rate;
  229. }
  230. s->frame_size = s->frame_size_min +
  231. 2 * (s->bits_written * s->sample_rate < s->samples_written * s->bit_rate);
  232. s->bits_written += s->frame_size * 8;
  233. s->samples_written += AC3_BLOCK_SIZE * s->num_blocks;
  234. }
  235. /**
  236. * Set the initial coupling strategy parameters prior to coupling analysis.
  237. *
  238. * @param s AC-3 encoder private context
  239. */
  240. void ff_ac3_compute_coupling_strategy(AC3EncodeContext *s)
  241. {
  242. int blk, ch;
  243. int got_cpl_snr;
  244. int num_cpl_blocks;
  245. /* set coupling use flags for each block/channel */
  246. /* TODO: turn coupling on/off and adjust start band based on bit usage */
  247. for (blk = 0; blk < s->num_blocks; blk++) {
  248. AC3Block *block = &s->blocks[blk];
  249. for (ch = 1; ch <= s->fbw_channels; ch++)
  250. block->channel_in_cpl[ch] = s->cpl_on;
  251. }
  252. /* enable coupling for each block if at least 2 channels have coupling
  253. enabled for that block */
  254. got_cpl_snr = 0;
  255. num_cpl_blocks = 0;
  256. for (blk = 0; blk < s->num_blocks; blk++) {
  257. AC3Block *block = &s->blocks[blk];
  258. block->num_cpl_channels = 0;
  259. for (ch = 1; ch <= s->fbw_channels; ch++)
  260. block->num_cpl_channels += block->channel_in_cpl[ch];
  261. block->cpl_in_use = block->num_cpl_channels > 1;
  262. num_cpl_blocks += block->cpl_in_use;
  263. if (!block->cpl_in_use) {
  264. block->num_cpl_channels = 0;
  265. for (ch = 1; ch <= s->fbw_channels; ch++)
  266. block->channel_in_cpl[ch] = 0;
  267. }
  268. block->new_cpl_strategy = !blk;
  269. if (blk) {
  270. for (ch = 1; ch <= s->fbw_channels; ch++) {
  271. if (block->channel_in_cpl[ch] != s->blocks[blk-1].channel_in_cpl[ch]) {
  272. block->new_cpl_strategy = 1;
  273. break;
  274. }
  275. }
  276. }
  277. block->new_cpl_leak = block->new_cpl_strategy;
  278. if (!blk || (block->cpl_in_use && !got_cpl_snr)) {
  279. block->new_snr_offsets = 1;
  280. if (block->cpl_in_use)
  281. got_cpl_snr = 1;
  282. } else {
  283. block->new_snr_offsets = 0;
  284. }
  285. }
  286. if (!num_cpl_blocks)
  287. s->cpl_on = 0;
  288. /* set bandwidth for each channel */
  289. for (blk = 0; blk < s->num_blocks; blk++) {
  290. AC3Block *block = &s->blocks[blk];
  291. for (ch = 1; ch <= s->fbw_channels; ch++) {
  292. if (block->channel_in_cpl[ch])
  293. block->end_freq[ch] = s->start_freq[CPL_CH];
  294. else
  295. block->end_freq[ch] = s->bandwidth_code * 3 + 73;
  296. }
  297. }
  298. }
  299. /**
  300. * Apply stereo rematrixing to coefficients based on rematrixing flags.
  301. *
  302. * @param s AC-3 encoder private context
  303. */
  304. static void ac3_apply_rematrixing(AC3EncodeContext *s)
  305. {
  306. int nb_coefs;
  307. int blk, bnd, i;
  308. int start, end;
  309. uint8_t *flags = NULL;
  310. if (!s->rematrixing_enabled)
  311. return;
  312. for (blk = 0; blk < s->num_blocks; blk++) {
  313. AC3Block *block = &s->blocks[blk];
  314. if (block->new_rematrixing_strategy)
  315. flags = block->rematrixing_flags;
  316. nb_coefs = FFMIN(block->end_freq[1], block->end_freq[2]);
  317. for (bnd = 0; bnd < block->num_rematrixing_bands; bnd++) {
  318. if (flags[bnd]) {
  319. start = ff_ac3_rematrix_band_tab[bnd];
  320. end = FFMIN(nb_coefs, ff_ac3_rematrix_band_tab[bnd+1]);
  321. for (i = start; i < end; i++) {
  322. int32_t lt = block->fixed_coef[1][i];
  323. int32_t rt = block->fixed_coef[2][i];
  324. block->fixed_coef[1][i] = (lt + rt) >> 1;
  325. block->fixed_coef[2][i] = (lt - rt) >> 1;
  326. }
  327. }
  328. }
  329. }
  330. }
  331. /*
  332. * Initialize exponent tables.
  333. */
  334. static av_cold void exponent_init(void)
  335. {
  336. int expstr, i, grpsize;
  337. for (expstr = EXP_D15-1; expstr <= EXP_D45-1; expstr++) {
  338. grpsize = 3 << expstr;
  339. for (i = 12; i < 256; i++) {
  340. exponent_group_tab[0][expstr][i] = (i + grpsize - 4) / grpsize;
  341. exponent_group_tab[1][expstr][i] = (i ) / grpsize;
  342. }
  343. }
  344. /* LFE */
  345. exponent_group_tab[0][0][7] = 2;
  346. }
  347. /*
  348. * Extract exponents from the MDCT coefficients.
  349. */
  350. static void extract_exponents(AC3EncodeContext *s)
  351. {
  352. int ch = !s->cpl_on;
  353. int chan_size = AC3_MAX_COEFS * s->num_blocks * (s->channels - ch + 1);
  354. AC3Block *block = &s->blocks[0];
  355. s->ac3dsp.extract_exponents(block->exp[ch], block->fixed_coef[ch], chan_size);
  356. }
  357. /**
  358. * Exponent Difference Threshold.
  359. * New exponents are sent if their SAD exceed this number.
  360. */
  361. #define EXP_DIFF_THRESHOLD 500
  362. /**
  363. * Table used to select exponent strategy based on exponent reuse block interval.
  364. */
  365. static const uint8_t exp_strategy_reuse_tab[4][6] = {
  366. { EXP_D15, EXP_D15, EXP_D15, EXP_D15, EXP_D15, EXP_D15 },
  367. { EXP_D15, EXP_D15, EXP_D15, EXP_D15, EXP_D15, EXP_D15 },
  368. { EXP_D25, EXP_D25, EXP_D15, EXP_D15, EXP_D15, EXP_D15 },
  369. { EXP_D45, EXP_D25, EXP_D25, EXP_D15, EXP_D15, EXP_D15 }
  370. };
  371. /*
  372. * Calculate exponent strategies for all channels.
  373. * Array arrangement is reversed to simplify the per-channel calculation.
  374. */
  375. static void compute_exp_strategy(AC3EncodeContext *s)
  376. {
  377. int ch, blk, blk1;
  378. for (ch = !s->cpl_on; ch <= s->fbw_channels; ch++) {
  379. uint8_t *exp_strategy = s->exp_strategy[ch];
  380. uint8_t *exp = s->blocks[0].exp[ch];
  381. int exp_diff;
  382. /* estimate if the exponent variation & decide if they should be
  383. reused in the next frame */
  384. exp_strategy[0] = EXP_NEW;
  385. exp += AC3_MAX_COEFS;
  386. for (blk = 1; blk < s->num_blocks; blk++, exp += AC3_MAX_COEFS) {
  387. if (ch == CPL_CH) {
  388. if (!s->blocks[blk-1].cpl_in_use) {
  389. exp_strategy[blk] = EXP_NEW;
  390. continue;
  391. } else if (!s->blocks[blk].cpl_in_use) {
  392. exp_strategy[blk] = EXP_REUSE;
  393. continue;
  394. }
  395. } else if (s->blocks[blk].channel_in_cpl[ch] != s->blocks[blk-1].channel_in_cpl[ch]) {
  396. exp_strategy[blk] = EXP_NEW;
  397. continue;
  398. }
  399. exp_diff = s->mecc.sad[0](NULL, exp, exp - AC3_MAX_COEFS, 16, 16);
  400. exp_strategy[blk] = EXP_REUSE;
  401. if (ch == CPL_CH && exp_diff > (EXP_DIFF_THRESHOLD * (s->blocks[blk].end_freq[ch] - s->start_freq[ch]) / AC3_MAX_COEFS))
  402. exp_strategy[blk] = EXP_NEW;
  403. else if (ch > CPL_CH && exp_diff > EXP_DIFF_THRESHOLD)
  404. exp_strategy[blk] = EXP_NEW;
  405. }
  406. /* now select the encoding strategy type : if exponents are often
  407. recoded, we use a coarse encoding */
  408. blk = 0;
  409. while (blk < s->num_blocks) {
  410. blk1 = blk + 1;
  411. while (blk1 < s->num_blocks && exp_strategy[blk1] == EXP_REUSE)
  412. blk1++;
  413. exp_strategy[blk] = exp_strategy_reuse_tab[s->num_blks_code][blk1-blk-1];
  414. blk = blk1;
  415. }
  416. }
  417. if (s->lfe_on) {
  418. ch = s->lfe_channel;
  419. s->exp_strategy[ch][0] = EXP_D15;
  420. for (blk = 1; blk < s->num_blocks; blk++)
  421. s->exp_strategy[ch][blk] = EXP_REUSE;
  422. }
  423. /* for E-AC-3, determine frame exponent strategy */
  424. if (CONFIG_EAC3_ENCODER && s->eac3)
  425. ff_eac3_get_frame_exp_strategy(s);
  426. }
  427. /**
  428. * Update the exponents so that they are the ones the decoder will decode.
  429. *
  430. * @param[in,out] exp array of exponents for 1 block in 1 channel
  431. * @param nb_exps number of exponents in active bandwidth
  432. * @param exp_strategy exponent strategy for the block
  433. * @param cpl indicates if the block is in the coupling channel
  434. */
  435. static void encode_exponents_blk_ch(uint8_t *exp, int nb_exps, int exp_strategy,
  436. int cpl)
  437. {
  438. int nb_groups, i, k;
  439. nb_groups = exponent_group_tab[cpl][exp_strategy-1][nb_exps] * 3;
  440. /* for each group, compute the minimum exponent */
  441. switch(exp_strategy) {
  442. case EXP_D25:
  443. for (i = 1, k = 1-cpl; i <= nb_groups; i++) {
  444. uint8_t exp_min = exp[k];
  445. if (exp[k+1] < exp_min)
  446. exp_min = exp[k+1];
  447. exp[i-cpl] = exp_min;
  448. k += 2;
  449. }
  450. break;
  451. case EXP_D45:
  452. for (i = 1, k = 1-cpl; i <= nb_groups; i++) {
  453. uint8_t exp_min = exp[k];
  454. if (exp[k+1] < exp_min)
  455. exp_min = exp[k+1];
  456. if (exp[k+2] < exp_min)
  457. exp_min = exp[k+2];
  458. if (exp[k+3] < exp_min)
  459. exp_min = exp[k+3];
  460. exp[i-cpl] = exp_min;
  461. k += 4;
  462. }
  463. break;
  464. }
  465. /* constraint for DC exponent */
  466. if (!cpl && exp[0] > 15)
  467. exp[0] = 15;
  468. /* decrease the delta between each groups to within 2 so that they can be
  469. differentially encoded */
  470. for (i = 1; i <= nb_groups; i++)
  471. exp[i] = FFMIN(exp[i], exp[i-1] + 2);
  472. i--;
  473. while (--i >= 0)
  474. exp[i] = FFMIN(exp[i], exp[i+1] + 2);
  475. if (cpl)
  476. exp[-1] = exp[0] & ~1;
  477. /* now we have the exponent values the decoder will see */
  478. switch (exp_strategy) {
  479. case EXP_D25:
  480. for (i = nb_groups, k = (nb_groups * 2)-cpl; i > 0; i--) {
  481. uint8_t exp1 = exp[i-cpl];
  482. exp[k--] = exp1;
  483. exp[k--] = exp1;
  484. }
  485. break;
  486. case EXP_D45:
  487. for (i = nb_groups, k = (nb_groups * 4)-cpl; i > 0; i--) {
  488. exp[k] = exp[k-1] = exp[k-2] = exp[k-3] = exp[i-cpl];
  489. k -= 4;
  490. }
  491. break;
  492. }
  493. }
  494. /*
  495. * Encode exponents from original extracted form to what the decoder will see.
  496. * This copies and groups exponents based on exponent strategy and reduces
  497. * deltas between adjacent exponent groups so that they can be differentially
  498. * encoded.
  499. */
  500. static void encode_exponents(AC3EncodeContext *s)
  501. {
  502. int blk, blk1, ch, cpl;
  503. uint8_t *exp, *exp_strategy;
  504. int nb_coefs, num_reuse_blocks;
  505. for (ch = !s->cpl_on; ch <= s->channels; ch++) {
  506. exp = s->blocks[0].exp[ch] + s->start_freq[ch];
  507. exp_strategy = s->exp_strategy[ch];
  508. cpl = (ch == CPL_CH);
  509. blk = 0;
  510. while (blk < s->num_blocks) {
  511. AC3Block *block = &s->blocks[blk];
  512. if (cpl && !block->cpl_in_use) {
  513. exp += AC3_MAX_COEFS;
  514. blk++;
  515. continue;
  516. }
  517. nb_coefs = block->end_freq[ch] - s->start_freq[ch];
  518. blk1 = blk + 1;
  519. /* count the number of EXP_REUSE blocks after the current block
  520. and set exponent reference block numbers */
  521. s->exp_ref_block[ch][blk] = blk;
  522. while (blk1 < s->num_blocks && exp_strategy[blk1] == EXP_REUSE) {
  523. s->exp_ref_block[ch][blk1] = blk;
  524. blk1++;
  525. }
  526. num_reuse_blocks = blk1 - blk - 1;
  527. /* for the EXP_REUSE case we select the min of the exponents */
  528. s->ac3dsp.ac3_exponent_min(exp-s->start_freq[ch], num_reuse_blocks,
  529. AC3_MAX_COEFS);
  530. encode_exponents_blk_ch(exp, nb_coefs, exp_strategy[blk], cpl);
  531. exp += AC3_MAX_COEFS * (num_reuse_blocks + 1);
  532. blk = blk1;
  533. }
  534. }
  535. /* reference block numbers have been changed, so reset ref_bap_set */
  536. s->ref_bap_set = 0;
  537. }
  538. /*
  539. * Count exponent bits based on bandwidth, coupling, and exponent strategies.
  540. */
  541. static int count_exponent_bits(AC3EncodeContext *s)
  542. {
  543. int blk, ch;
  544. int nb_groups, bit_count;
  545. bit_count = 0;
  546. for (blk = 0; blk < s->num_blocks; blk++) {
  547. AC3Block *block = &s->blocks[blk];
  548. for (ch = !block->cpl_in_use; ch <= s->channels; ch++) {
  549. int exp_strategy = s->exp_strategy[ch][blk];
  550. int cpl = (ch == CPL_CH);
  551. int nb_coefs = block->end_freq[ch] - s->start_freq[ch];
  552. if (exp_strategy == EXP_REUSE)
  553. continue;
  554. nb_groups = exponent_group_tab[cpl][exp_strategy-1][nb_coefs];
  555. bit_count += 4 + (nb_groups * 7);
  556. }
  557. }
  558. return bit_count;
  559. }
  560. /**
  561. * Group exponents.
  562. * 3 delta-encoded exponents are in each 7-bit group. The number of groups
  563. * varies depending on exponent strategy and bandwidth.
  564. *
  565. * @param s AC-3 encoder private context
  566. */
  567. static void ac3_group_exponents(AC3EncodeContext *s)
  568. {
  569. int blk, ch, i, cpl;
  570. int group_size, nb_groups;
  571. uint8_t *p;
  572. int delta0, delta1, delta2;
  573. int exp0, exp1;
  574. for (blk = 0; blk < s->num_blocks; blk++) {
  575. AC3Block *block = &s->blocks[blk];
  576. for (ch = !block->cpl_in_use; ch <= s->channels; ch++) {
  577. int exp_strategy = s->exp_strategy[ch][blk];
  578. if (exp_strategy == EXP_REUSE)
  579. continue;
  580. cpl = (ch == CPL_CH);
  581. group_size = exp_strategy + (exp_strategy == EXP_D45);
  582. nb_groups = exponent_group_tab[cpl][exp_strategy-1][block->end_freq[ch]-s->start_freq[ch]];
  583. p = block->exp[ch] + s->start_freq[ch] - cpl;
  584. /* DC exponent */
  585. exp1 = *p++;
  586. block->grouped_exp[ch][0] = exp1;
  587. /* remaining exponents are delta encoded */
  588. for (i = 1; i <= nb_groups; i++) {
  589. /* merge three delta in one code */
  590. exp0 = exp1;
  591. exp1 = p[0];
  592. p += group_size;
  593. delta0 = exp1 - exp0 + 2;
  594. av_assert2(delta0 >= 0 && delta0 <= 4);
  595. exp0 = exp1;
  596. exp1 = p[0];
  597. p += group_size;
  598. delta1 = exp1 - exp0 + 2;
  599. av_assert2(delta1 >= 0 && delta1 <= 4);
  600. exp0 = exp1;
  601. exp1 = p[0];
  602. p += group_size;
  603. delta2 = exp1 - exp0 + 2;
  604. av_assert2(delta2 >= 0 && delta2 <= 4);
  605. block->grouped_exp[ch][i] = ((delta0 * 5 + delta1) * 5) + delta2;
  606. }
  607. }
  608. }
  609. }
  610. /**
  611. * Calculate final exponents from the supplied MDCT coefficients and exponent shift.
  612. * Extract exponents from MDCT coefficients, calculate exponent strategies,
  613. * and encode final exponents.
  614. *
  615. * @param s AC-3 encoder private context
  616. */
  617. static void ac3_process_exponents(AC3EncodeContext *s)
  618. {
  619. extract_exponents(s);
  620. compute_exp_strategy(s);
  621. encode_exponents(s);
  622. emms_c();
  623. }
  624. /*
  625. * Count frame bits that are based solely on fixed parameters.
  626. * This only has to be run once when the encoder is initialized.
  627. */
  628. static void count_frame_bits_fixed(AC3EncodeContext *s)
  629. {
  630. static const uint8_t frame_bits_inc[8] = { 0, 0, 2, 2, 2, 4, 2, 4 };
  631. int blk;
  632. int frame_bits;
  633. /* assumptions:
  634. * no dynamic range codes
  635. * bit allocation parameters do not change between blocks
  636. * no delta bit allocation
  637. * no skipped data
  638. * no auxiliary data
  639. * no E-AC-3 metadata
  640. */
  641. /* header */
  642. frame_bits = 16; /* sync info */
  643. if (s->eac3) {
  644. /* bitstream info header */
  645. frame_bits += 35;
  646. frame_bits += 1 + 1;
  647. if (s->num_blocks != 0x6)
  648. frame_bits++;
  649. frame_bits++;
  650. /* audio frame header */
  651. if (s->num_blocks == 6)
  652. frame_bits += 2;
  653. frame_bits += 10;
  654. /* exponent strategy */
  655. if (s->use_frame_exp_strategy)
  656. frame_bits += 5 * s->fbw_channels;
  657. else
  658. frame_bits += s->num_blocks * 2 * s->fbw_channels;
  659. if (s->lfe_on)
  660. frame_bits += s->num_blocks;
  661. /* converter exponent strategy */
  662. if (s->num_blks_code != 0x3)
  663. frame_bits++;
  664. else
  665. frame_bits += s->fbw_channels * 5;
  666. /* snr offsets */
  667. frame_bits += 10;
  668. /* block start info */
  669. if (s->num_blocks != 1)
  670. frame_bits++;
  671. } else {
  672. frame_bits += 49;
  673. frame_bits += frame_bits_inc[s->channel_mode];
  674. }
  675. /* audio blocks */
  676. for (blk = 0; blk < s->num_blocks; blk++) {
  677. if (!s->eac3) {
  678. /* block switch flags */
  679. frame_bits += s->fbw_channels;
  680. /* dither flags */
  681. frame_bits += s->fbw_channels;
  682. }
  683. /* dynamic range */
  684. frame_bits++;
  685. /* spectral extension */
  686. if (s->eac3)
  687. frame_bits++;
  688. if (!s->eac3) {
  689. /* exponent strategy */
  690. frame_bits += 2 * s->fbw_channels;
  691. if (s->lfe_on)
  692. frame_bits++;
  693. /* bit allocation params */
  694. frame_bits++;
  695. if (!blk)
  696. frame_bits += 2 + 2 + 2 + 2 + 3;
  697. }
  698. /* converter snr offset */
  699. if (s->eac3)
  700. frame_bits++;
  701. if (!s->eac3) {
  702. /* delta bit allocation */
  703. frame_bits++;
  704. /* skipped data */
  705. frame_bits++;
  706. }
  707. }
  708. /* auxiliary data */
  709. frame_bits++;
  710. /* CRC */
  711. frame_bits += 1 + 16;
  712. s->frame_bits_fixed = frame_bits;
  713. }
  714. /*
  715. * Initialize bit allocation.
  716. * Set default parameter codes and calculate parameter values.
  717. */
  718. static av_cold void bit_alloc_init(AC3EncodeContext *s)
  719. {
  720. int ch;
  721. /* init default parameters */
  722. s->slow_decay_code = 2;
  723. s->fast_decay_code = 1;
  724. s->slow_gain_code = 1;
  725. s->db_per_bit_code = s->eac3 ? 2 : 3;
  726. s->floor_code = 7;
  727. for (ch = 0; ch <= s->channels; ch++)
  728. s->fast_gain_code[ch] = 4;
  729. /* initial snr offset */
  730. s->coarse_snr_offset = 40;
  731. /* compute real values */
  732. /* currently none of these values change during encoding, so we can just
  733. set them once at initialization */
  734. s->bit_alloc.slow_decay = ff_ac3_slow_decay_tab[s->slow_decay_code] >> s->bit_alloc.sr_shift;
  735. s->bit_alloc.fast_decay = ff_ac3_fast_decay_tab[s->fast_decay_code] >> s->bit_alloc.sr_shift;
  736. s->bit_alloc.slow_gain = ff_ac3_slow_gain_tab[s->slow_gain_code];
  737. s->bit_alloc.db_per_bit = ff_ac3_db_per_bit_tab[s->db_per_bit_code];
  738. s->bit_alloc.floor = ff_ac3_floor_tab[s->floor_code];
  739. s->bit_alloc.cpl_fast_leak = 0;
  740. s->bit_alloc.cpl_slow_leak = 0;
  741. count_frame_bits_fixed(s);
  742. }
  743. /*
  744. * Count the bits used to encode the frame, minus exponents and mantissas.
  745. * Bits based on fixed parameters have already been counted, so now we just
  746. * have to add the bits based on parameters that change during encoding.
  747. */
  748. static void count_frame_bits(AC3EncodeContext *s)
  749. {
  750. AC3EncOptions *opt = &s->options;
  751. int blk, ch;
  752. int frame_bits = 0;
  753. /* header */
  754. if (s->eac3) {
  755. if (opt->eac3_mixing_metadata) {
  756. if (s->channel_mode > AC3_CHMODE_STEREO)
  757. frame_bits += 2;
  758. if (s->has_center)
  759. frame_bits += 6;
  760. if (s->has_surround)
  761. frame_bits += 6;
  762. frame_bits += s->lfe_on;
  763. frame_bits += 1 + 1 + 2;
  764. if (s->channel_mode < AC3_CHMODE_STEREO)
  765. frame_bits++;
  766. frame_bits++;
  767. }
  768. if (opt->eac3_info_metadata) {
  769. frame_bits += 3 + 1 + 1;
  770. if (s->channel_mode == AC3_CHMODE_STEREO)
  771. frame_bits += 2 + 2;
  772. if (s->channel_mode >= AC3_CHMODE_2F2R)
  773. frame_bits += 2;
  774. frame_bits++;
  775. if (opt->audio_production_info)
  776. frame_bits += 5 + 2 + 1;
  777. frame_bits++;
  778. }
  779. /* coupling */
  780. if (s->channel_mode > AC3_CHMODE_MONO) {
  781. frame_bits++;
  782. for (blk = 1; blk < s->num_blocks; blk++) {
  783. AC3Block *block = &s->blocks[blk];
  784. frame_bits++;
  785. if (block->new_cpl_strategy)
  786. frame_bits++;
  787. }
  788. }
  789. /* coupling exponent strategy */
  790. if (s->cpl_on) {
  791. if (s->use_frame_exp_strategy) {
  792. frame_bits += 5 * s->cpl_on;
  793. } else {
  794. for (blk = 0; blk < s->num_blocks; blk++)
  795. frame_bits += 2 * s->blocks[blk].cpl_in_use;
  796. }
  797. }
  798. } else {
  799. if (opt->audio_production_info)
  800. frame_bits += 7;
  801. if (s->bitstream_id == 6) {
  802. if (opt->extended_bsi_1)
  803. frame_bits += 14;
  804. if (opt->extended_bsi_2)
  805. frame_bits += 14;
  806. }
  807. }
  808. /* audio blocks */
  809. for (blk = 0; blk < s->num_blocks; blk++) {
  810. AC3Block *block = &s->blocks[blk];
  811. /* coupling strategy */
  812. if (!s->eac3)
  813. frame_bits++;
  814. if (block->new_cpl_strategy) {
  815. if (!s->eac3)
  816. frame_bits++;
  817. if (block->cpl_in_use) {
  818. if (s->eac3)
  819. frame_bits++;
  820. if (!s->eac3 || s->channel_mode != AC3_CHMODE_STEREO)
  821. frame_bits += s->fbw_channels;
  822. if (s->channel_mode == AC3_CHMODE_STEREO)
  823. frame_bits++;
  824. frame_bits += 4 + 4;
  825. if (s->eac3)
  826. frame_bits++;
  827. else
  828. frame_bits += s->num_cpl_subbands - 1;
  829. }
  830. }
  831. /* coupling coordinates */
  832. if (block->cpl_in_use) {
  833. for (ch = 1; ch <= s->fbw_channels; ch++) {
  834. if (block->channel_in_cpl[ch]) {
  835. if (!s->eac3 || block->new_cpl_coords[ch] != 2)
  836. frame_bits++;
  837. if (block->new_cpl_coords[ch]) {
  838. frame_bits += 2;
  839. frame_bits += (4 + 4) * s->num_cpl_bands;
  840. }
  841. }
  842. }
  843. }
  844. /* stereo rematrixing */
  845. if (s->channel_mode == AC3_CHMODE_STEREO) {
  846. if (!s->eac3 || blk > 0)
  847. frame_bits++;
  848. if (s->blocks[blk].new_rematrixing_strategy)
  849. frame_bits += block->num_rematrixing_bands;
  850. }
  851. /* bandwidth codes & gain range */
  852. for (ch = 1; ch <= s->fbw_channels; ch++) {
  853. if (s->exp_strategy[ch][blk] != EXP_REUSE) {
  854. if (!block->channel_in_cpl[ch])
  855. frame_bits += 6;
  856. frame_bits += 2;
  857. }
  858. }
  859. /* coupling exponent strategy */
  860. if (!s->eac3 && block->cpl_in_use)
  861. frame_bits += 2;
  862. /* snr offsets and fast gain codes */
  863. if (!s->eac3) {
  864. frame_bits++;
  865. if (block->new_snr_offsets)
  866. frame_bits += 6 + (s->channels + block->cpl_in_use) * (4 + 3);
  867. }
  868. /* coupling leak info */
  869. if (block->cpl_in_use) {
  870. if (!s->eac3 || block->new_cpl_leak != 2)
  871. frame_bits++;
  872. if (block->new_cpl_leak)
  873. frame_bits += 3 + 3;
  874. }
  875. }
  876. s->frame_bits = s->frame_bits_fixed + frame_bits;
  877. }
  878. /*
  879. * Calculate masking curve based on the final exponents.
  880. * Also calculate the power spectral densities to use in future calculations.
  881. */
  882. static void bit_alloc_masking(AC3EncodeContext *s)
  883. {
  884. int blk, ch;
  885. for (blk = 0; blk < s->num_blocks; blk++) {
  886. AC3Block *block = &s->blocks[blk];
  887. for (ch = !block->cpl_in_use; ch <= s->channels; ch++) {
  888. /* We only need psd and mask for calculating bap.
  889. Since we currently do not calculate bap when exponent
  890. strategy is EXP_REUSE we do not need to calculate psd or mask. */
  891. if (s->exp_strategy[ch][blk] != EXP_REUSE) {
  892. ff_ac3_bit_alloc_calc_psd(block->exp[ch], s->start_freq[ch],
  893. block->end_freq[ch], block->psd[ch],
  894. block->band_psd[ch]);
  895. ff_ac3_bit_alloc_calc_mask(&s->bit_alloc, block->band_psd[ch],
  896. s->start_freq[ch], block->end_freq[ch],
  897. ff_ac3_fast_gain_tab[s->fast_gain_code[ch]],
  898. ch == s->lfe_channel,
  899. DBA_NONE, 0, NULL, NULL, NULL,
  900. block->mask[ch]);
  901. }
  902. }
  903. }
  904. }
  905. /*
  906. * Ensure that bap for each block and channel point to the current bap_buffer.
  907. * They may have been switched during the bit allocation search.
  908. */
  909. static void reset_block_bap(AC3EncodeContext *s)
  910. {
  911. int blk, ch;
  912. uint8_t *ref_bap;
  913. if (s->ref_bap[0][0] == s->bap_buffer && s->ref_bap_set)
  914. return;
  915. ref_bap = s->bap_buffer;
  916. for (ch = 0; ch <= s->channels; ch++) {
  917. for (blk = 0; blk < s->num_blocks; blk++)
  918. s->ref_bap[ch][blk] = ref_bap + AC3_MAX_COEFS * s->exp_ref_block[ch][blk];
  919. ref_bap += AC3_MAX_COEFS * s->num_blocks;
  920. }
  921. s->ref_bap_set = 1;
  922. }
  923. /**
  924. * Initialize mantissa counts.
  925. * These are set so that they are padded to the next whole group size when bits
  926. * are counted in compute_mantissa_size.
  927. *
  928. * @param[in,out] mant_cnt running counts for each bap value for each block
  929. */
  930. static void count_mantissa_bits_init(uint16_t mant_cnt[AC3_MAX_BLOCKS][16])
  931. {
  932. int blk;
  933. for (blk = 0; blk < AC3_MAX_BLOCKS; blk++) {
  934. memset(mant_cnt[blk], 0, sizeof(mant_cnt[blk]));
  935. mant_cnt[blk][1] = mant_cnt[blk][2] = 2;
  936. mant_cnt[blk][4] = 1;
  937. }
  938. }
  939. /**
  940. * Update mantissa bit counts for all blocks in 1 channel in a given bandwidth
  941. * range.
  942. *
  943. * @param s AC-3 encoder private context
  944. * @param ch channel index
  945. * @param[in,out] mant_cnt running counts for each bap value for each block
  946. * @param start starting coefficient bin
  947. * @param end ending coefficient bin
  948. */
  949. static void count_mantissa_bits_update_ch(AC3EncodeContext *s, int ch,
  950. uint16_t mant_cnt[AC3_MAX_BLOCKS][16],
  951. int start, int end)
  952. {
  953. int blk;
  954. for (blk = 0; blk < s->num_blocks; blk++) {
  955. AC3Block *block = &s->blocks[blk];
  956. if (ch == CPL_CH && !block->cpl_in_use)
  957. continue;
  958. s->ac3dsp.update_bap_counts(mant_cnt[blk],
  959. s->ref_bap[ch][blk] + start,
  960. FFMIN(end, block->end_freq[ch]) - start);
  961. }
  962. }
  963. /*
  964. * Count the number of mantissa bits in the frame based on the bap values.
  965. */
  966. static int count_mantissa_bits(AC3EncodeContext *s)
  967. {
  968. int ch, max_end_freq;
  969. LOCAL_ALIGNED_16(uint16_t, mant_cnt, [AC3_MAX_BLOCKS], [16]);
  970. count_mantissa_bits_init(mant_cnt);
  971. max_end_freq = s->bandwidth_code * 3 + 73;
  972. for (ch = !s->cpl_enabled; ch <= s->channels; ch++)
  973. count_mantissa_bits_update_ch(s, ch, mant_cnt, s->start_freq[ch],
  974. max_end_freq);
  975. return s->ac3dsp.compute_mantissa_size(mant_cnt);
  976. }
  977. /**
  978. * Run the bit allocation with a given SNR offset.
  979. * This calculates the bit allocation pointers that will be used to determine
  980. * the quantization of each mantissa.
  981. *
  982. * @param s AC-3 encoder private context
  983. * @param snr_offset SNR offset, 0 to 1023
  984. * @return the number of bits needed for mantissas if the given SNR offset is
  985. * is used.
  986. */
  987. static int bit_alloc(AC3EncodeContext *s, int snr_offset)
  988. {
  989. int blk, ch;
  990. snr_offset = (snr_offset - 240) * 4;
  991. reset_block_bap(s);
  992. for (blk = 0; blk < s->num_blocks; blk++) {
  993. AC3Block *block = &s->blocks[blk];
  994. for (ch = !block->cpl_in_use; ch <= s->channels; ch++) {
  995. /* Currently the only bit allocation parameters which vary across
  996. blocks within a frame are the exponent values. We can take
  997. advantage of that by reusing the bit allocation pointers
  998. whenever we reuse exponents. */
  999. if (s->exp_strategy[ch][blk] != EXP_REUSE) {
  1000. s->ac3dsp.bit_alloc_calc_bap(block->mask[ch], block->psd[ch],
  1001. s->start_freq[ch], block->end_freq[ch],
  1002. snr_offset, s->bit_alloc.floor,
  1003. ff_ac3_bap_tab, s->ref_bap[ch][blk]);
  1004. }
  1005. }
  1006. }
  1007. return count_mantissa_bits(s);
  1008. }
  1009. /*
  1010. * Constant bitrate bit allocation search.
  1011. * Find the largest SNR offset that will allow data to fit in the frame.
  1012. */
  1013. static int cbr_bit_allocation(AC3EncodeContext *s)
  1014. {
  1015. int ch;
  1016. int bits_left;
  1017. int snr_offset, snr_incr;
  1018. bits_left = 8 * s->frame_size - (s->frame_bits + s->exponent_bits);
  1019. if (bits_left < 0)
  1020. return AVERROR(EINVAL);
  1021. snr_offset = s->coarse_snr_offset << 4;
  1022. /* if previous frame SNR offset was 1023, check if current frame can also
  1023. use SNR offset of 1023. if so, skip the search. */
  1024. if ((snr_offset | s->fine_snr_offset[1]) == 1023) {
  1025. if (bit_alloc(s, 1023) <= bits_left)
  1026. return 0;
  1027. }
  1028. while (snr_offset >= 0 &&
  1029. bit_alloc(s, snr_offset) > bits_left) {
  1030. snr_offset -= 64;
  1031. }
  1032. if (snr_offset < 0)
  1033. return AVERROR(EINVAL);
  1034. FFSWAP(uint8_t *, s->bap_buffer, s->bap1_buffer);
  1035. for (snr_incr = 64; snr_incr > 0; snr_incr >>= 2) {
  1036. while (snr_offset + snr_incr <= 1023 &&
  1037. bit_alloc(s, snr_offset + snr_incr) <= bits_left) {
  1038. snr_offset += snr_incr;
  1039. FFSWAP(uint8_t *, s->bap_buffer, s->bap1_buffer);
  1040. }
  1041. }
  1042. FFSWAP(uint8_t *, s->bap_buffer, s->bap1_buffer);
  1043. reset_block_bap(s);
  1044. s->coarse_snr_offset = snr_offset >> 4;
  1045. for (ch = !s->cpl_on; ch <= s->channels; ch++)
  1046. s->fine_snr_offset[ch] = snr_offset & 0xF;
  1047. return 0;
  1048. }
  1049. /*
  1050. * Perform bit allocation search.
  1051. * Finds the SNR offset value that maximizes quality and fits in the specified
  1052. * frame size. Output is the SNR offset and a set of bit allocation pointers
  1053. * used to quantize the mantissas.
  1054. */
  1055. static int ac3_compute_bit_allocation(AC3EncodeContext *s)
  1056. {
  1057. count_frame_bits(s);
  1058. s->exponent_bits = count_exponent_bits(s);
  1059. bit_alloc_masking(s);
  1060. return cbr_bit_allocation(s);
  1061. }
  1062. /**
  1063. * Symmetric quantization on 'levels' levels.
  1064. *
  1065. * @param c unquantized coefficient
  1066. * @param e exponent
  1067. * @param levels number of quantization levels
  1068. * @return quantized coefficient
  1069. */
  1070. static inline int sym_quant(int c, int e, int levels)
  1071. {
  1072. int v = (((levels * c) >> (24 - e)) + levels) >> 1;
  1073. av_assert2(v >= 0 && v < levels);
  1074. return v;
  1075. }
  1076. /**
  1077. * Asymmetric quantization on 2^qbits levels.
  1078. *
  1079. * @param c unquantized coefficient
  1080. * @param e exponent
  1081. * @param qbits number of quantization bits
  1082. * @return quantized coefficient
  1083. */
  1084. static inline int asym_quant(int c, int e, int qbits)
  1085. {
  1086. int m;
  1087. c = (((c * (1<<e)) >> (24 - qbits)) + 1) >> 1;
  1088. m = (1 << (qbits-1));
  1089. if (c >= m)
  1090. c = m - 1;
  1091. av_assert2(c >= -m);
  1092. return c;
  1093. }
  1094. /**
  1095. * Quantize a set of mantissas for a single channel in a single block.
  1096. *
  1097. * @param s Mantissa count context
  1098. * @param fixed_coef unquantized fixed-point coefficients
  1099. * @param exp exponents
  1100. * @param bap bit allocation pointer indices
  1101. * @param[out] qmant quantized coefficients
  1102. * @param start_freq starting coefficient bin
  1103. * @param end_freq ending coefficient bin
  1104. */
  1105. static void quantize_mantissas_blk_ch(AC3Mant *s, int32_t *fixed_coef,
  1106. uint8_t *exp, uint8_t *bap,
  1107. int16_t *qmant, int start_freq,
  1108. int end_freq)
  1109. {
  1110. int i;
  1111. for (i = start_freq; i < end_freq; i++) {
  1112. int c = fixed_coef[i];
  1113. int e = exp[i];
  1114. int v = bap[i];
  1115. if (v)
  1116. switch (v) {
  1117. case 1:
  1118. v = sym_quant(c, e, 3);
  1119. switch (s->mant1_cnt) {
  1120. case 0:
  1121. s->qmant1_ptr = &qmant[i];
  1122. v = 9 * v;
  1123. s->mant1_cnt = 1;
  1124. break;
  1125. case 1:
  1126. *s->qmant1_ptr += 3 * v;
  1127. s->mant1_cnt = 2;
  1128. v = 128;
  1129. break;
  1130. default:
  1131. *s->qmant1_ptr += v;
  1132. s->mant1_cnt = 0;
  1133. v = 128;
  1134. break;
  1135. }
  1136. break;
  1137. case 2:
  1138. v = sym_quant(c, e, 5);
  1139. switch (s->mant2_cnt) {
  1140. case 0:
  1141. s->qmant2_ptr = &qmant[i];
  1142. v = 25 * v;
  1143. s->mant2_cnt = 1;
  1144. break;
  1145. case 1:
  1146. *s->qmant2_ptr += 5 * v;
  1147. s->mant2_cnt = 2;
  1148. v = 128;
  1149. break;
  1150. default:
  1151. *s->qmant2_ptr += v;
  1152. s->mant2_cnt = 0;
  1153. v = 128;
  1154. break;
  1155. }
  1156. break;
  1157. case 3:
  1158. v = sym_quant(c, e, 7);
  1159. break;
  1160. case 4:
  1161. v = sym_quant(c, e, 11);
  1162. switch (s->mant4_cnt) {
  1163. case 0:
  1164. s->qmant4_ptr = &qmant[i];
  1165. v = 11 * v;
  1166. s->mant4_cnt = 1;
  1167. break;
  1168. default:
  1169. *s->qmant4_ptr += v;
  1170. s->mant4_cnt = 0;
  1171. v = 128;
  1172. break;
  1173. }
  1174. break;
  1175. case 5:
  1176. v = sym_quant(c, e, 15);
  1177. break;
  1178. case 14:
  1179. v = asym_quant(c, e, 14);
  1180. break;
  1181. case 15:
  1182. v = asym_quant(c, e, 16);
  1183. break;
  1184. default:
  1185. v = asym_quant(c, e, v - 1);
  1186. break;
  1187. }
  1188. qmant[i] = v;
  1189. }
  1190. }
  1191. /**
  1192. * Quantize mantissas using coefficients, exponents, and bit allocation pointers.
  1193. *
  1194. * @param s AC-3 encoder private context
  1195. */
  1196. static void ac3_quantize_mantissas(AC3EncodeContext *s)
  1197. {
  1198. int blk, ch, ch0=0, got_cpl;
  1199. for (blk = 0; blk < s->num_blocks; blk++) {
  1200. AC3Block *block = &s->blocks[blk];
  1201. AC3Mant m = { 0 };
  1202. got_cpl = !block->cpl_in_use;
  1203. for (ch = 1; ch <= s->channels; ch++) {
  1204. if (!got_cpl && ch > 1 && block->channel_in_cpl[ch-1]) {
  1205. ch0 = ch - 1;
  1206. ch = CPL_CH;
  1207. got_cpl = 1;
  1208. }
  1209. quantize_mantissas_blk_ch(&m, block->fixed_coef[ch],
  1210. s->blocks[s->exp_ref_block[ch][blk]].exp[ch],
  1211. s->ref_bap[ch][blk], block->qmant[ch],
  1212. s->start_freq[ch], block->end_freq[ch]);
  1213. if (ch == CPL_CH)
  1214. ch = ch0;
  1215. }
  1216. }
  1217. }
  1218. /*
  1219. * Write the AC-3 frame header to the output bitstream.
  1220. */
  1221. static void ac3_output_frame_header(AC3EncodeContext *s)
  1222. {
  1223. AC3EncOptions *opt = &s->options;
  1224. put_bits(&s->pb, 16, 0x0b77); /* frame header */
  1225. put_bits(&s->pb, 16, 0); /* crc1: will be filled later */
  1226. put_bits(&s->pb, 2, s->bit_alloc.sr_code);
  1227. put_bits(&s->pb, 6, s->frame_size_code + (s->frame_size - s->frame_size_min) / 2);
  1228. put_bits(&s->pb, 5, s->bitstream_id);
  1229. put_bits(&s->pb, 3, s->bitstream_mode);
  1230. put_bits(&s->pb, 3, s->channel_mode);
  1231. if ((s->channel_mode & 0x01) && s->channel_mode != AC3_CHMODE_MONO)
  1232. put_bits(&s->pb, 2, s->center_mix_level);
  1233. if (s->channel_mode & 0x04)
  1234. put_bits(&s->pb, 2, s->surround_mix_level);
  1235. if (s->channel_mode == AC3_CHMODE_STEREO)
  1236. put_bits(&s->pb, 2, opt->dolby_surround_mode);
  1237. put_bits(&s->pb, 1, s->lfe_on); /* LFE */
  1238. put_bits(&s->pb, 5, -opt->dialogue_level);
  1239. put_bits(&s->pb, 1, 0); /* no compression control word */
  1240. put_bits(&s->pb, 1, 0); /* no lang code */
  1241. put_bits(&s->pb, 1, opt->audio_production_info);
  1242. if (opt->audio_production_info) {
  1243. put_bits(&s->pb, 5, opt->mixing_level - 80);
  1244. put_bits(&s->pb, 2, opt->room_type);
  1245. }
  1246. put_bits(&s->pb, 1, opt->copyright);
  1247. put_bits(&s->pb, 1, opt->original);
  1248. if (s->bitstream_id == 6) {
  1249. /* alternate bit stream syntax */
  1250. put_bits(&s->pb, 1, opt->extended_bsi_1);
  1251. if (opt->extended_bsi_1) {
  1252. put_bits(&s->pb, 2, opt->preferred_stereo_downmix);
  1253. put_bits(&s->pb, 3, s->ltrt_center_mix_level);
  1254. put_bits(&s->pb, 3, s->ltrt_surround_mix_level);
  1255. put_bits(&s->pb, 3, s->loro_center_mix_level);
  1256. put_bits(&s->pb, 3, s->loro_surround_mix_level);
  1257. }
  1258. put_bits(&s->pb, 1, opt->extended_bsi_2);
  1259. if (opt->extended_bsi_2) {
  1260. put_bits(&s->pb, 2, opt->dolby_surround_ex_mode);
  1261. put_bits(&s->pb, 2, opt->dolby_headphone_mode);
  1262. put_bits(&s->pb, 1, opt->ad_converter_type);
  1263. put_bits(&s->pb, 9, 0); /* xbsi2 and encinfo : reserved */
  1264. }
  1265. } else {
  1266. put_bits(&s->pb, 1, 0); /* no time code 1 */
  1267. put_bits(&s->pb, 1, 0); /* no time code 2 */
  1268. }
  1269. put_bits(&s->pb, 1, 0); /* no additional bit stream info */
  1270. }
  1271. /*
  1272. * Write one audio block to the output bitstream.
  1273. */
  1274. static void output_audio_block(AC3EncodeContext *s, int blk)
  1275. {
  1276. int ch, i, baie, bnd, got_cpl, av_uninit(ch0);
  1277. AC3Block *block = &s->blocks[blk];
  1278. /* block switching */
  1279. if (!s->eac3) {
  1280. for (ch = 0; ch < s->fbw_channels; ch++)
  1281. put_bits(&s->pb, 1, 0);
  1282. }
  1283. /* dither flags */
  1284. if (!s->eac3) {
  1285. for (ch = 0; ch < s->fbw_channels; ch++)
  1286. put_bits(&s->pb, 1, 1);
  1287. }
  1288. /* dynamic range codes */
  1289. put_bits(&s->pb, 1, 0);
  1290. /* spectral extension */
  1291. if (s->eac3)
  1292. put_bits(&s->pb, 1, 0);
  1293. /* channel coupling */
  1294. if (!s->eac3)
  1295. put_bits(&s->pb, 1, block->new_cpl_strategy);
  1296. if (block->new_cpl_strategy) {
  1297. if (!s->eac3)
  1298. put_bits(&s->pb, 1, block->cpl_in_use);
  1299. if (block->cpl_in_use) {
  1300. int start_sub, end_sub;
  1301. if (s->eac3)
  1302. put_bits(&s->pb, 1, 0); /* enhanced coupling */
  1303. if (!s->eac3 || s->channel_mode != AC3_CHMODE_STEREO) {
  1304. for (ch = 1; ch <= s->fbw_channels; ch++)
  1305. put_bits(&s->pb, 1, block->channel_in_cpl[ch]);
  1306. }
  1307. if (s->channel_mode == AC3_CHMODE_STEREO)
  1308. put_bits(&s->pb, 1, 0); /* phase flags in use */
  1309. start_sub = (s->start_freq[CPL_CH] - 37) / 12;
  1310. end_sub = (s->cpl_end_freq - 37) / 12;
  1311. put_bits(&s->pb, 4, start_sub);
  1312. put_bits(&s->pb, 4, end_sub - 3);
  1313. /* coupling band structure */
  1314. if (s->eac3) {
  1315. put_bits(&s->pb, 1, 0); /* use default */
  1316. } else {
  1317. for (bnd = start_sub+1; bnd < end_sub; bnd++)
  1318. put_bits(&s->pb, 1, ff_eac3_default_cpl_band_struct[bnd]);
  1319. }
  1320. }
  1321. }
  1322. /* coupling coordinates */
  1323. if (block->cpl_in_use) {
  1324. for (ch = 1; ch <= s->fbw_channels; ch++) {
  1325. if (block->channel_in_cpl[ch]) {
  1326. if (!s->eac3 || block->new_cpl_coords[ch] != 2)
  1327. put_bits(&s->pb, 1, block->new_cpl_coords[ch]);
  1328. if (block->new_cpl_coords[ch]) {
  1329. put_bits(&s->pb, 2, block->cpl_master_exp[ch]);
  1330. for (bnd = 0; bnd < s->num_cpl_bands; bnd++) {
  1331. put_bits(&s->pb, 4, block->cpl_coord_exp [ch][bnd]);
  1332. put_bits(&s->pb, 4, block->cpl_coord_mant[ch][bnd]);
  1333. }
  1334. }
  1335. }
  1336. }
  1337. }
  1338. /* stereo rematrixing */
  1339. if (s->channel_mode == AC3_CHMODE_STEREO) {
  1340. if (!s->eac3 || blk > 0)
  1341. put_bits(&s->pb, 1, block->new_rematrixing_strategy);
  1342. if (block->new_rematrixing_strategy) {
  1343. /* rematrixing flags */
  1344. for (bnd = 0; bnd < block->num_rematrixing_bands; bnd++)
  1345. put_bits(&s->pb, 1, block->rematrixing_flags[bnd]);
  1346. }
  1347. }
  1348. /* exponent strategy */
  1349. if (!s->eac3) {
  1350. for (ch = !block->cpl_in_use; ch <= s->fbw_channels; ch++)
  1351. put_bits(&s->pb, 2, s->exp_strategy[ch][blk]);
  1352. if (s->lfe_on)
  1353. put_bits(&s->pb, 1, s->exp_strategy[s->lfe_channel][blk]);
  1354. }
  1355. /* bandwidth */
  1356. for (ch = 1; ch <= s->fbw_channels; ch++) {
  1357. if (s->exp_strategy[ch][blk] != EXP_REUSE && !block->channel_in_cpl[ch])
  1358. put_bits(&s->pb, 6, s->bandwidth_code);
  1359. }
  1360. /* exponents */
  1361. for (ch = !block->cpl_in_use; ch <= s->channels; ch++) {
  1362. int nb_groups;
  1363. int cpl = (ch == CPL_CH);
  1364. if (s->exp_strategy[ch][blk] == EXP_REUSE)
  1365. continue;
  1366. /* DC exponent */
  1367. put_bits(&s->pb, 4, block->grouped_exp[ch][0] >> cpl);
  1368. /* exponent groups */
  1369. nb_groups = exponent_group_tab[cpl][s->exp_strategy[ch][blk]-1][block->end_freq[ch]-s->start_freq[ch]];
  1370. for (i = 1; i <= nb_groups; i++)
  1371. put_bits(&s->pb, 7, block->grouped_exp[ch][i]);
  1372. /* gain range info */
  1373. if (ch != s->lfe_channel && !cpl)
  1374. put_bits(&s->pb, 2, 0);
  1375. }
  1376. /* bit allocation info */
  1377. if (!s->eac3) {
  1378. baie = (blk == 0);
  1379. put_bits(&s->pb, 1, baie);
  1380. if (baie) {
  1381. put_bits(&s->pb, 2, s->slow_decay_code);
  1382. put_bits(&s->pb, 2, s->fast_decay_code);
  1383. put_bits(&s->pb, 2, s->slow_gain_code);
  1384. put_bits(&s->pb, 2, s->db_per_bit_code);
  1385. put_bits(&s->pb, 3, s->floor_code);
  1386. }
  1387. }
  1388. /* snr offset */
  1389. if (!s->eac3) {
  1390. put_bits(&s->pb, 1, block->new_snr_offsets);
  1391. if (block->new_snr_offsets) {
  1392. put_bits(&s->pb, 6, s->coarse_snr_offset);
  1393. for (ch = !block->cpl_in_use; ch <= s->channels; ch++) {
  1394. put_bits(&s->pb, 4, s->fine_snr_offset[ch]);
  1395. put_bits(&s->pb, 3, s->fast_gain_code[ch]);
  1396. }
  1397. }
  1398. } else {
  1399. put_bits(&s->pb, 1, 0); /* no converter snr offset */
  1400. }
  1401. /* coupling leak */
  1402. if (block->cpl_in_use) {
  1403. if (!s->eac3 || block->new_cpl_leak != 2)
  1404. put_bits(&s->pb, 1, block->new_cpl_leak);
  1405. if (block->new_cpl_leak) {
  1406. put_bits(&s->pb, 3, s->bit_alloc.cpl_fast_leak);
  1407. put_bits(&s->pb, 3, s->bit_alloc.cpl_slow_leak);
  1408. }
  1409. }
  1410. if (!s->eac3) {
  1411. put_bits(&s->pb, 1, 0); /* no delta bit allocation */
  1412. put_bits(&s->pb, 1, 0); /* no data to skip */
  1413. }
  1414. /* mantissas */
  1415. got_cpl = !block->cpl_in_use;
  1416. for (ch = 1; ch <= s->channels; ch++) {
  1417. int b, q;
  1418. if (!got_cpl && ch > 1 && block->channel_in_cpl[ch-1]) {
  1419. ch0 = ch - 1;
  1420. ch = CPL_CH;
  1421. got_cpl = 1;
  1422. }
  1423. for (i = s->start_freq[ch]; i < block->end_freq[ch]; i++) {
  1424. q = block->qmant[ch][i];
  1425. b = s->ref_bap[ch][blk][i];
  1426. switch (b) {
  1427. case 0: break;
  1428. case 1: if (q != 128) put_bits (&s->pb, 5, q); break;
  1429. case 2: if (q != 128) put_bits (&s->pb, 7, q); break;
  1430. case 3: put_sbits(&s->pb, 3, q); break;
  1431. case 4: if (q != 128) put_bits (&s->pb, 7, q); break;
  1432. case 14: put_sbits(&s->pb, 14, q); break;
  1433. case 15: put_sbits(&s->pb, 16, q); break;
  1434. default: put_sbits(&s->pb, b-1, q); break;
  1435. }
  1436. }
  1437. if (ch == CPL_CH)
  1438. ch = ch0;
  1439. }
  1440. }
  1441. /** CRC-16 Polynomial */
  1442. #define CRC16_POLY ((1 << 0) | (1 << 2) | (1 << 15) | (1 << 16))
  1443. static unsigned int mul_poly(unsigned int a, unsigned int b, unsigned int poly)
  1444. {
  1445. unsigned int c;
  1446. c = 0;
  1447. while (a) {
  1448. if (a & 1)
  1449. c ^= b;
  1450. a = a >> 1;
  1451. b = b << 1;
  1452. if (b & (1 << 16))
  1453. b ^= poly;
  1454. }
  1455. return c;
  1456. }
  1457. static unsigned int pow_poly(unsigned int a, unsigned int n, unsigned int poly)
  1458. {
  1459. unsigned int r;
  1460. r = 1;
  1461. while (n) {
  1462. if (n & 1)
  1463. r = mul_poly(r, a, poly);
  1464. a = mul_poly(a, a, poly);
  1465. n >>= 1;
  1466. }
  1467. return r;
  1468. }
  1469. /*
  1470. * Fill the end of the frame with 0's and compute the two CRCs.
  1471. */
  1472. static void output_frame_end(AC3EncodeContext *s)
  1473. {
  1474. const AVCRC *crc_ctx = av_crc_get_table(AV_CRC_16_ANSI);
  1475. int frame_size_58, pad_bytes, crc1, crc2_partial, crc2, crc_inv;
  1476. uint8_t *frame;
  1477. frame_size_58 = ((s->frame_size >> 2) + (s->frame_size >> 4)) << 1;
  1478. /* pad the remainder of the frame with zeros */
  1479. av_assert2(s->frame_size * 8 - put_bits_count(&s->pb) >= 18);
  1480. flush_put_bits(&s->pb);
  1481. frame = s->pb.buf;
  1482. pad_bytes = s->frame_size - (put_bits_ptr(&s->pb) - frame) - 2;
  1483. av_assert2(pad_bytes >= 0);
  1484. if (pad_bytes > 0)
  1485. memset(put_bits_ptr(&s->pb), 0, pad_bytes);
  1486. if (s->eac3) {
  1487. /* compute crc2 */
  1488. crc2_partial = av_crc(crc_ctx, 0, frame + 2, s->frame_size - 5);
  1489. } else {
  1490. /* compute crc1 */
  1491. /* this is not so easy because it is at the beginning of the data... */
  1492. crc1 = av_bswap16(av_crc(crc_ctx, 0, frame + 4, frame_size_58 - 4));
  1493. crc_inv = s->crc_inv[s->frame_size > s->frame_size_min];
  1494. crc1 = mul_poly(crc_inv, crc1, CRC16_POLY);
  1495. AV_WB16(frame + 2, crc1);
  1496. /* compute crc2 */
  1497. crc2_partial = av_crc(crc_ctx, 0, frame + frame_size_58,
  1498. s->frame_size - frame_size_58 - 3);
  1499. }
  1500. crc2 = av_crc(crc_ctx, crc2_partial, frame + s->frame_size - 3, 1);
  1501. /* ensure crc2 does not match sync word by flipping crcrsv bit if needed */
  1502. if (crc2 == 0x770B) {
  1503. frame[s->frame_size - 3] ^= 0x1;
  1504. crc2 = av_crc(crc_ctx, crc2_partial, frame + s->frame_size - 3, 1);
  1505. }
  1506. crc2 = av_bswap16(crc2);
  1507. AV_WB16(frame + s->frame_size - 2, crc2);
  1508. }
  1509. /**
  1510. * Write the frame to the output bitstream.
  1511. *
  1512. * @param s AC-3 encoder private context
  1513. * @param frame output data buffer
  1514. */
  1515. static void ac3_output_frame(AC3EncodeContext *s, unsigned char *frame)
  1516. {
  1517. int blk;
  1518. init_put_bits(&s->pb, frame, AC3_MAX_CODED_FRAME_SIZE);
  1519. s->output_frame_header(s);
  1520. for (blk = 0; blk < s->num_blocks; blk++)
  1521. output_audio_block(s, blk);
  1522. output_frame_end(s);
  1523. }
  1524. int ff_ac3_encode_frame_common_end(AVCodecContext *avctx, AVPacket *avpkt,
  1525. const AVFrame *frame, int *got_packet_ptr)
  1526. {
  1527. AC3EncodeContext *const s = avctx->priv_data;
  1528. int ret;
  1529. ac3_apply_rematrixing(s);
  1530. ac3_process_exponents(s);
  1531. ret = ac3_compute_bit_allocation(s);
  1532. if (ret) {
  1533. av_log(avctx, AV_LOG_ERROR, "Bit allocation failed. Try increasing the bitrate.\n");
  1534. return ret;
  1535. }
  1536. ac3_group_exponents(s);
  1537. ac3_quantize_mantissas(s);
  1538. if ((ret = ff_alloc_packet2(avctx, avpkt, s->frame_size, 0)) < 0)
  1539. return ret;
  1540. ac3_output_frame(s, avpkt->data);
  1541. if (frame->pts != AV_NOPTS_VALUE)
  1542. avpkt->pts = frame->pts - ff_samples_to_time_base(avctx, avctx->initial_padding);
  1543. *got_packet_ptr = 1;
  1544. return 0;
  1545. }
  1546. static void dprint_options(AC3EncodeContext *s)
  1547. {
  1548. #ifdef DEBUG
  1549. AVCodecContext *avctx = s->avctx;
  1550. AC3EncOptions *opt = &s->options;
  1551. char strbuf[32];
  1552. switch (s->bitstream_id) {
  1553. case 6: av_strlcpy(strbuf, "AC-3 (alt syntax)", 32); break;
  1554. case 8: av_strlcpy(strbuf, "AC-3 (standard)", 32); break;
  1555. case 9: av_strlcpy(strbuf, "AC-3 (dnet half-rate)", 32); break;
  1556. case 10: av_strlcpy(strbuf, "AC-3 (dnet quater-rate)", 32); break;
  1557. case 16: av_strlcpy(strbuf, "E-AC-3 (enhanced)", 32); break;
  1558. default: snprintf(strbuf, 32, "ERROR");
  1559. }
  1560. ff_dlog(avctx, "bitstream_id: %s (%d)\n", strbuf, s->bitstream_id);
  1561. ff_dlog(avctx, "sample_fmt: %s\n", av_get_sample_fmt_name(avctx->sample_fmt));
  1562. av_get_channel_layout_string(strbuf, 32, s->channels, avctx->channel_layout);
  1563. ff_dlog(avctx, "channel_layout: %s\n", strbuf);
  1564. ff_dlog(avctx, "sample_rate: %d\n", s->sample_rate);
  1565. ff_dlog(avctx, "bit_rate: %d\n", s->bit_rate);
  1566. ff_dlog(avctx, "blocks/frame: %d (code=%d)\n", s->num_blocks, s->num_blks_code);
  1567. if (s->cutoff)
  1568. ff_dlog(avctx, "cutoff: %d\n", s->cutoff);
  1569. ff_dlog(avctx, "per_frame_metadata: %s\n",
  1570. opt->allow_per_frame_metadata?"on":"off");
  1571. if (s->has_center)
  1572. ff_dlog(avctx, "center_mixlev: %0.3f (%d)\n", opt->center_mix_level,
  1573. s->center_mix_level);
  1574. else
  1575. ff_dlog(avctx, "center_mixlev: {not written}\n");
  1576. if (s->has_surround)
  1577. ff_dlog(avctx, "surround_mixlev: %0.3f (%d)\n", opt->surround_mix_level,
  1578. s->surround_mix_level);
  1579. else
  1580. ff_dlog(avctx, "surround_mixlev: {not written}\n");
  1581. if (opt->audio_production_info) {
  1582. ff_dlog(avctx, "mixing_level: %ddB\n", opt->mixing_level);
  1583. switch (opt->room_type) {
  1584. case AC3ENC_OPT_NOT_INDICATED: av_strlcpy(strbuf, "notindicated", 32); break;
  1585. case AC3ENC_OPT_LARGE_ROOM: av_strlcpy(strbuf, "large", 32); break;
  1586. case AC3ENC_OPT_SMALL_ROOM: av_strlcpy(strbuf, "small", 32); break;
  1587. default: snprintf(strbuf, 32, "ERROR (%d)", opt->room_type);
  1588. }
  1589. ff_dlog(avctx, "room_type: %s\n", strbuf);
  1590. } else {
  1591. ff_dlog(avctx, "mixing_level: {not written}\n");
  1592. ff_dlog(avctx, "room_type: {not written}\n");
  1593. }
  1594. ff_dlog(avctx, "copyright: %s\n", opt->copyright?"on":"off");
  1595. ff_dlog(avctx, "dialnorm: %ddB\n", opt->dialogue_level);
  1596. if (s->channel_mode == AC3_CHMODE_STEREO) {
  1597. switch (opt->dolby_surround_mode) {
  1598. case AC3ENC_OPT_NOT_INDICATED: av_strlcpy(strbuf, "notindicated", 32); break;
  1599. case AC3ENC_OPT_MODE_ON: av_strlcpy(strbuf, "on", 32); break;
  1600. case AC3ENC_OPT_MODE_OFF: av_strlcpy(strbuf, "off", 32); break;
  1601. default: snprintf(strbuf, 32, "ERROR (%d)", opt->dolby_surround_mode);
  1602. }
  1603. ff_dlog(avctx, "dsur_mode: %s\n", strbuf);
  1604. } else {
  1605. ff_dlog(avctx, "dsur_mode: {not written}\n");
  1606. }
  1607. ff_dlog(avctx, "original: %s\n", opt->original?"on":"off");
  1608. if (s->bitstream_id == 6) {
  1609. if (opt->extended_bsi_1) {
  1610. switch (opt->preferred_stereo_downmix) {
  1611. case AC3ENC_OPT_NOT_INDICATED: av_strlcpy(strbuf, "notindicated", 32); break;
  1612. case AC3ENC_OPT_DOWNMIX_LTRT: av_strlcpy(strbuf, "ltrt", 32); break;
  1613. case AC3ENC_OPT_DOWNMIX_LORO: av_strlcpy(strbuf, "loro", 32); break;
  1614. default: snprintf(strbuf, 32, "ERROR (%d)", opt->preferred_stereo_downmix);
  1615. }
  1616. ff_dlog(avctx, "dmix_mode: %s\n", strbuf);
  1617. ff_dlog(avctx, "ltrt_cmixlev: %0.3f (%d)\n",
  1618. opt->ltrt_center_mix_level, s->ltrt_center_mix_level);
  1619. ff_dlog(avctx, "ltrt_surmixlev: %0.3f (%d)\n",
  1620. opt->ltrt_surround_mix_level, s->ltrt_surround_mix_level);
  1621. ff_dlog(avctx, "loro_cmixlev: %0.3f (%d)\n",
  1622. opt->loro_center_mix_level, s->loro_center_mix_level);
  1623. ff_dlog(avctx, "loro_surmixlev: %0.3f (%d)\n",
  1624. opt->loro_surround_mix_level, s->loro_surround_mix_level);
  1625. } else {
  1626. ff_dlog(avctx, "extended bitstream info 1: {not written}\n");
  1627. }
  1628. if (opt->extended_bsi_2) {
  1629. switch (opt->dolby_surround_ex_mode) {
  1630. case AC3ENC_OPT_NOT_INDICATED: av_strlcpy(strbuf, "notindicated", 32); break;
  1631. case AC3ENC_OPT_MODE_ON: av_strlcpy(strbuf, "on", 32); break;
  1632. case AC3ENC_OPT_MODE_OFF: av_strlcpy(strbuf, "off", 32); break;
  1633. default: snprintf(strbuf, 32, "ERROR (%d)", opt->dolby_surround_ex_mode);
  1634. }
  1635. ff_dlog(avctx, "dsurex_mode: %s\n", strbuf);
  1636. switch (opt->dolby_headphone_mode) {
  1637. case AC3ENC_OPT_NOT_INDICATED: av_strlcpy(strbuf, "notindicated", 32); break;
  1638. case AC3ENC_OPT_MODE_ON: av_strlcpy(strbuf, "on", 32); break;
  1639. case AC3ENC_OPT_MODE_OFF: av_strlcpy(strbuf, "off", 32); break;
  1640. default: snprintf(strbuf, 32, "ERROR (%d)", opt->dolby_headphone_mode);
  1641. }
  1642. ff_dlog(avctx, "dheadphone_mode: %s\n", strbuf);
  1643. switch (opt->ad_converter_type) {
  1644. case AC3ENC_OPT_ADCONV_STANDARD: av_strlcpy(strbuf, "standard", 32); break;
  1645. case AC3ENC_OPT_ADCONV_HDCD: av_strlcpy(strbuf, "hdcd", 32); break;
  1646. default: snprintf(strbuf, 32, "ERROR (%d)", opt->ad_converter_type);
  1647. }
  1648. ff_dlog(avctx, "ad_conv_type: %s\n", strbuf);
  1649. } else {
  1650. ff_dlog(avctx, "extended bitstream info 2: {not written}\n");
  1651. }
  1652. }
  1653. #endif
  1654. }
  1655. #define FLT_OPTION_THRESHOLD 0.01
  1656. static int validate_float_option(float v, const float *v_list, int v_list_size)
  1657. {
  1658. int i;
  1659. for (i = 0; i < v_list_size; i++) {
  1660. if (v < (v_list[i] + FLT_OPTION_THRESHOLD) &&
  1661. v > (v_list[i] - FLT_OPTION_THRESHOLD))
  1662. break;
  1663. }
  1664. if (i == v_list_size)
  1665. return AVERROR(EINVAL);
  1666. return i;
  1667. }
  1668. static void validate_mix_level(void *log_ctx, const char *opt_name,
  1669. float *opt_param, const float *list,
  1670. int list_size, int default_value, int min_value,
  1671. int *ctx_param)
  1672. {
  1673. int mixlev = validate_float_option(*opt_param, list, list_size);
  1674. if (mixlev < min_value) {
  1675. mixlev = default_value;
  1676. if (*opt_param >= 0.0) {
  1677. av_log(log_ctx, AV_LOG_WARNING, "requested %s is not valid. using "
  1678. "default value: %0.3f\n", opt_name, list[mixlev]);
  1679. }
  1680. }
  1681. *opt_param = list[mixlev];
  1682. *ctx_param = mixlev;
  1683. }
  1684. /**
  1685. * Validate metadata options as set by AVOption system.
  1686. * These values can optionally be changed per-frame.
  1687. *
  1688. * @param s AC-3 encoder private context
  1689. */
  1690. int ff_ac3_validate_metadata(AC3EncodeContext *s)
  1691. {
  1692. AVCodecContext *avctx = s->avctx;
  1693. AC3EncOptions *opt = &s->options;
  1694. opt->audio_production_info = 0;
  1695. opt->extended_bsi_1 = 0;
  1696. opt->extended_bsi_2 = 0;
  1697. opt->eac3_mixing_metadata = 0;
  1698. opt->eac3_info_metadata = 0;
  1699. /* determine mixing metadata / xbsi1 use */
  1700. if (s->channel_mode > AC3_CHMODE_STEREO && opt->preferred_stereo_downmix != AC3ENC_OPT_NONE) {
  1701. opt->extended_bsi_1 = 1;
  1702. opt->eac3_mixing_metadata = 1;
  1703. }
  1704. if (s->has_center &&
  1705. (opt->ltrt_center_mix_level >= 0 || opt->loro_center_mix_level >= 0)) {
  1706. opt->extended_bsi_1 = 1;
  1707. opt->eac3_mixing_metadata = 1;
  1708. }
  1709. if (s->has_surround &&
  1710. (opt->ltrt_surround_mix_level >= 0 || opt->loro_surround_mix_level >= 0)) {
  1711. opt->extended_bsi_1 = 1;
  1712. opt->eac3_mixing_metadata = 1;
  1713. }
  1714. if (s->eac3) {
  1715. /* determine info metadata use */
  1716. if (avctx->audio_service_type != AV_AUDIO_SERVICE_TYPE_MAIN)
  1717. opt->eac3_info_metadata = 1;
  1718. if (opt->copyright != AC3ENC_OPT_NONE || opt->original != AC3ENC_OPT_NONE)
  1719. opt->eac3_info_metadata = 1;
  1720. if (s->channel_mode == AC3_CHMODE_STEREO &&
  1721. (opt->dolby_headphone_mode != AC3ENC_OPT_NONE || opt->dolby_surround_mode != AC3ENC_OPT_NONE))
  1722. opt->eac3_info_metadata = 1;
  1723. if (s->channel_mode >= AC3_CHMODE_2F2R && opt->dolby_surround_ex_mode != AC3ENC_OPT_NONE)
  1724. opt->eac3_info_metadata = 1;
  1725. if (opt->mixing_level != AC3ENC_OPT_NONE || opt->room_type != AC3ENC_OPT_NONE ||
  1726. opt->ad_converter_type != AC3ENC_OPT_NONE) {
  1727. opt->audio_production_info = 1;
  1728. opt->eac3_info_metadata = 1;
  1729. }
  1730. } else {
  1731. /* determine audio production info use */
  1732. if (opt->mixing_level != AC3ENC_OPT_NONE || opt->room_type != AC3ENC_OPT_NONE)
  1733. opt->audio_production_info = 1;
  1734. /* determine xbsi2 use */
  1735. if (s->channel_mode >= AC3_CHMODE_2F2R && opt->dolby_surround_ex_mode != AC3ENC_OPT_NONE)
  1736. opt->extended_bsi_2 = 1;
  1737. if (s->channel_mode == AC3_CHMODE_STEREO && opt->dolby_headphone_mode != AC3ENC_OPT_NONE)
  1738. opt->extended_bsi_2 = 1;
  1739. if (opt->ad_converter_type != AC3ENC_OPT_NONE)
  1740. opt->extended_bsi_2 = 1;
  1741. }
  1742. /* validate AC-3 mixing levels */
  1743. if (!s->eac3) {
  1744. if (s->has_center) {
  1745. validate_mix_level(avctx, "center_mix_level", &opt->center_mix_level,
  1746. cmixlev_options, CMIXLEV_NUM_OPTIONS, 1, 0,
  1747. &s->center_mix_level);
  1748. }
  1749. if (s->has_surround) {
  1750. validate_mix_level(avctx, "surround_mix_level", &opt->surround_mix_level,
  1751. surmixlev_options, SURMIXLEV_NUM_OPTIONS, 1, 0,
  1752. &s->surround_mix_level);
  1753. }
  1754. }
  1755. /* validate extended bsi 1 / mixing metadata */
  1756. if (opt->extended_bsi_1 || opt->eac3_mixing_metadata) {
  1757. /* default preferred stereo downmix */
  1758. if (opt->preferred_stereo_downmix == AC3ENC_OPT_NONE)
  1759. opt->preferred_stereo_downmix = AC3ENC_OPT_NOT_INDICATED;
  1760. if (!s->eac3 || s->has_center) {
  1761. /* validate Lt/Rt center mix level */
  1762. validate_mix_level(avctx, "ltrt_center_mix_level",
  1763. &opt->ltrt_center_mix_level, extmixlev_options,
  1764. EXTMIXLEV_NUM_OPTIONS, 5, 0,
  1765. &s->ltrt_center_mix_level);
  1766. /* validate Lo/Ro center mix level */
  1767. validate_mix_level(avctx, "loro_center_mix_level",
  1768. &opt->loro_center_mix_level, extmixlev_options,
  1769. EXTMIXLEV_NUM_OPTIONS, 5, 0,
  1770. &s->loro_center_mix_level);
  1771. }
  1772. if (!s->eac3 || s->has_surround) {
  1773. /* validate Lt/Rt surround mix level */
  1774. validate_mix_level(avctx, "ltrt_surround_mix_level",
  1775. &opt->ltrt_surround_mix_level, extmixlev_options,
  1776. EXTMIXLEV_NUM_OPTIONS, 6, 3,
  1777. &s->ltrt_surround_mix_level);
  1778. /* validate Lo/Ro surround mix level */
  1779. validate_mix_level(avctx, "loro_surround_mix_level",
  1780. &opt->loro_surround_mix_level, extmixlev_options,
  1781. EXTMIXLEV_NUM_OPTIONS, 6, 3,
  1782. &s->loro_surround_mix_level);
  1783. }
  1784. }
  1785. /* validate audio service type / channels combination */
  1786. if ((avctx->audio_service_type == AV_AUDIO_SERVICE_TYPE_KARAOKE &&
  1787. avctx->channels == 1) ||
  1788. ((avctx->audio_service_type == AV_AUDIO_SERVICE_TYPE_COMMENTARY ||
  1789. avctx->audio_service_type == AV_AUDIO_SERVICE_TYPE_EMERGENCY ||
  1790. avctx->audio_service_type == AV_AUDIO_SERVICE_TYPE_VOICE_OVER)
  1791. && avctx->channels > 1)) {
  1792. av_log(avctx, AV_LOG_ERROR, "invalid audio service type for the "
  1793. "specified number of channels\n");
  1794. return AVERROR(EINVAL);
  1795. }
  1796. /* validate extended bsi 2 / info metadata */
  1797. if (opt->extended_bsi_2 || opt->eac3_info_metadata) {
  1798. /* default dolby headphone mode */
  1799. if (opt->dolby_headphone_mode == AC3ENC_OPT_NONE)
  1800. opt->dolby_headphone_mode = AC3ENC_OPT_NOT_INDICATED;
  1801. /* default dolby surround ex mode */
  1802. if (opt->dolby_surround_ex_mode == AC3ENC_OPT_NONE)
  1803. opt->dolby_surround_ex_mode = AC3ENC_OPT_NOT_INDICATED;
  1804. /* default A/D converter type */
  1805. if (opt->ad_converter_type == AC3ENC_OPT_NONE)
  1806. opt->ad_converter_type = AC3ENC_OPT_ADCONV_STANDARD;
  1807. }
  1808. /* copyright & original defaults */
  1809. if (!s->eac3 || opt->eac3_info_metadata) {
  1810. /* default copyright */
  1811. if (opt->copyright == AC3ENC_OPT_NONE)
  1812. opt->copyright = AC3ENC_OPT_OFF;
  1813. /* default original */
  1814. if (opt->original == AC3ENC_OPT_NONE)
  1815. opt->original = AC3ENC_OPT_ON;
  1816. }
  1817. /* dolby surround mode default */
  1818. if (!s->eac3 || opt->eac3_info_metadata) {
  1819. if (opt->dolby_surround_mode == AC3ENC_OPT_NONE)
  1820. opt->dolby_surround_mode = AC3ENC_OPT_NOT_INDICATED;
  1821. }
  1822. /* validate audio production info */
  1823. if (opt->audio_production_info) {
  1824. if (opt->mixing_level == AC3ENC_OPT_NONE) {
  1825. av_log(avctx, AV_LOG_ERROR, "mixing_level must be set if "
  1826. "room_type is set\n");
  1827. return AVERROR(EINVAL);
  1828. }
  1829. if (opt->mixing_level < 80) {
  1830. av_log(avctx, AV_LOG_ERROR, "invalid mixing level. must be between "
  1831. "80dB and 111dB\n");
  1832. return AVERROR(EINVAL);
  1833. }
  1834. /* default room type */
  1835. if (opt->room_type == AC3ENC_OPT_NONE)
  1836. opt->room_type = AC3ENC_OPT_NOT_INDICATED;
  1837. }
  1838. /* set bitstream id for alternate bitstream syntax */
  1839. if (!s->eac3 && (opt->extended_bsi_1 || opt->extended_bsi_2)) {
  1840. if (s->bitstream_id > 8 && s->bitstream_id < 11) {
  1841. if (!s->warned_alternate_bitstream) {
  1842. av_log(avctx, AV_LOG_WARNING, "alternate bitstream syntax is "
  1843. "not compatible with reduced samplerates. writing of "
  1844. "extended bitstream information will be disabled.\n");
  1845. s->warned_alternate_bitstream = 1;
  1846. }
  1847. } else {
  1848. s->bitstream_id = 6;
  1849. }
  1850. }
  1851. return 0;
  1852. }
  1853. /**
  1854. * Finalize encoding and free any memory allocated by the encoder.
  1855. *
  1856. * @param avctx Codec context
  1857. */
  1858. av_cold int ff_ac3_encode_close(AVCodecContext *avctx)
  1859. {
  1860. int blk, ch;
  1861. AC3EncodeContext *s = avctx->priv_data;
  1862. av_freep(&s->mdct_window);
  1863. av_freep(&s->windowed_samples);
  1864. if (s->planar_samples)
  1865. for (ch = 0; ch < s->channels; ch++)
  1866. av_freep(&s->planar_samples[ch]);
  1867. av_freep(&s->planar_samples);
  1868. av_freep(&s->bap_buffer);
  1869. av_freep(&s->bap1_buffer);
  1870. av_freep(&s->mdct_coef_buffer);
  1871. av_freep(&s->fixed_coef_buffer);
  1872. av_freep(&s->exp_buffer);
  1873. av_freep(&s->grouped_exp_buffer);
  1874. av_freep(&s->psd_buffer);
  1875. av_freep(&s->band_psd_buffer);
  1876. av_freep(&s->mask_buffer);
  1877. av_freep(&s->qmant_buffer);
  1878. av_freep(&s->cpl_coord_exp_buffer);
  1879. av_freep(&s->cpl_coord_mant_buffer);
  1880. av_freep(&s->fdsp);
  1881. for (blk = 0; blk < s->num_blocks; blk++) {
  1882. AC3Block *block = &s->blocks[blk];
  1883. av_freep(&block->mdct_coef);
  1884. av_freep(&block->fixed_coef);
  1885. av_freep(&block->exp);
  1886. av_freep(&block->grouped_exp);
  1887. av_freep(&block->psd);
  1888. av_freep(&block->band_psd);
  1889. av_freep(&block->mask);
  1890. av_freep(&block->qmant);
  1891. av_freep(&block->cpl_coord_exp);
  1892. av_freep(&block->cpl_coord_mant);
  1893. }
  1894. s->mdct_end(s);
  1895. return 0;
  1896. }
  1897. /*
  1898. * Set channel information during initialization.
  1899. */
  1900. static av_cold int set_channel_info(AC3EncodeContext *s, int channels,
  1901. uint64_t *channel_layout)
  1902. {
  1903. int ch_layout;
  1904. if (channels < 1 || channels > AC3_MAX_CHANNELS)
  1905. return AVERROR(EINVAL);
  1906. if (*channel_layout > 0x7FF)
  1907. return AVERROR(EINVAL);
  1908. ch_layout = *channel_layout;
  1909. if (!ch_layout)
  1910. ch_layout = av_get_default_channel_layout(channels);
  1911. s->lfe_on = !!(ch_layout & AV_CH_LOW_FREQUENCY);
  1912. s->channels = channels;
  1913. s->fbw_channels = channels - s->lfe_on;
  1914. s->lfe_channel = s->lfe_on ? s->fbw_channels + 1 : -1;
  1915. if (s->lfe_on)
  1916. ch_layout -= AV_CH_LOW_FREQUENCY;
  1917. switch (ch_layout) {
  1918. case AV_CH_LAYOUT_MONO: s->channel_mode = AC3_CHMODE_MONO; break;
  1919. case AV_CH_LAYOUT_STEREO: s->channel_mode = AC3_CHMODE_STEREO; break;
  1920. case AV_CH_LAYOUT_SURROUND: s->channel_mode = AC3_CHMODE_3F; break;
  1921. case AV_CH_LAYOUT_2_1: s->channel_mode = AC3_CHMODE_2F1R; break;
  1922. case AV_CH_LAYOUT_4POINT0: s->channel_mode = AC3_CHMODE_3F1R; break;
  1923. case AV_CH_LAYOUT_QUAD:
  1924. case AV_CH_LAYOUT_2_2: s->channel_mode = AC3_CHMODE_2F2R; break;
  1925. case AV_CH_LAYOUT_5POINT0:
  1926. case AV_CH_LAYOUT_5POINT0_BACK: s->channel_mode = AC3_CHMODE_3F2R; break;
  1927. default:
  1928. return AVERROR(EINVAL);
  1929. }
  1930. s->has_center = (s->channel_mode & 0x01) && s->channel_mode != AC3_CHMODE_MONO;
  1931. s->has_surround = s->channel_mode & 0x04;
  1932. s->channel_map = ac3_enc_channel_map[s->channel_mode][s->lfe_on];
  1933. *channel_layout = ch_layout;
  1934. if (s->lfe_on)
  1935. *channel_layout |= AV_CH_LOW_FREQUENCY;
  1936. return 0;
  1937. }
  1938. static av_cold int validate_options(AC3EncodeContext *s)
  1939. {
  1940. AVCodecContext *avctx = s->avctx;
  1941. int i, ret, max_sr;
  1942. /* validate channel layout */
  1943. if (!avctx->channel_layout) {
  1944. av_log(avctx, AV_LOG_WARNING, "No channel layout specified. The "
  1945. "encoder will guess the layout, but it "
  1946. "might be incorrect.\n");
  1947. }
  1948. ret = set_channel_info(s, avctx->channels, &avctx->channel_layout);
  1949. if (ret) {
  1950. av_log(avctx, AV_LOG_ERROR, "invalid channel layout\n");
  1951. return ret;
  1952. }
  1953. /* validate sample rate */
  1954. /* note: max_sr could be changed from 2 to 5 for E-AC-3 once we find a
  1955. decoder that supports half sample rate so we can validate that
  1956. the generated files are correct. */
  1957. max_sr = s->eac3 ? 2 : 8;
  1958. for (i = 0; i <= max_sr; i++) {
  1959. if ((ff_ac3_sample_rate_tab[i % 3] >> (i / 3)) == avctx->sample_rate)
  1960. break;
  1961. }
  1962. if (i > max_sr) {
  1963. av_log(avctx, AV_LOG_ERROR, "invalid sample rate\n");
  1964. return AVERROR(EINVAL);
  1965. }
  1966. s->sample_rate = avctx->sample_rate;
  1967. s->bit_alloc.sr_shift = i / 3;
  1968. s->bit_alloc.sr_code = i % 3;
  1969. s->bitstream_id = s->eac3 ? 16 : 8 + s->bit_alloc.sr_shift;
  1970. /* select a default bit rate if not set by the user */
  1971. if (!avctx->bit_rate) {
  1972. switch (s->fbw_channels) {
  1973. case 1: avctx->bit_rate = 96000; break;
  1974. case 2: avctx->bit_rate = 192000; break;
  1975. case 3: avctx->bit_rate = 320000; break;
  1976. case 4: avctx->bit_rate = 384000; break;
  1977. case 5: avctx->bit_rate = 448000; break;
  1978. }
  1979. }
  1980. /* validate bit rate */
  1981. if (s->eac3) {
  1982. int max_br, min_br, wpf, min_br_code;
  1983. int num_blks_code, num_blocks, frame_samples;
  1984. long long min_br_dist;
  1985. /* calculate min/max bitrate */
  1986. /* TODO: More testing with 3 and 2 blocks. All E-AC-3 samples I've
  1987. found use either 6 blocks or 1 block, even though 2 or 3 blocks
  1988. would work as far as the bit rate is concerned. */
  1989. for (num_blks_code = 3; num_blks_code >= 0; num_blks_code--) {
  1990. num_blocks = ((int[]){ 1, 2, 3, 6 })[num_blks_code];
  1991. frame_samples = AC3_BLOCK_SIZE * num_blocks;
  1992. max_br = 2048 * s->sample_rate / frame_samples * 16;
  1993. min_br = ((s->sample_rate + (frame_samples-1)) / frame_samples) * 16;
  1994. if (avctx->bit_rate <= max_br)
  1995. break;
  1996. }
  1997. if (avctx->bit_rate < min_br || avctx->bit_rate > max_br) {
  1998. av_log(avctx, AV_LOG_ERROR, "invalid bit rate. must be %d to %d "
  1999. "for this sample rate\n", min_br, max_br);
  2000. return AVERROR(EINVAL);
  2001. }
  2002. s->num_blks_code = num_blks_code;
  2003. s->num_blocks = num_blocks;
  2004. /* calculate words-per-frame for the selected bitrate */
  2005. wpf = (avctx->bit_rate / 16) * frame_samples / s->sample_rate;
  2006. av_assert1(wpf > 0 && wpf <= 2048);
  2007. /* find the closest AC-3 bitrate code to the selected bitrate.
  2008. this is needed for lookup tables for bandwidth and coupling
  2009. parameter selection */
  2010. min_br_code = -1;
  2011. min_br_dist = INT64_MAX;
  2012. for (i = 0; i < 19; i++) {
  2013. long long br_dist = llabs(ff_ac3_bitrate_tab[i] * 1000 - avctx->bit_rate);
  2014. if (br_dist < min_br_dist) {
  2015. min_br_dist = br_dist;
  2016. min_br_code = i;
  2017. }
  2018. }
  2019. /* make sure the minimum frame size is below the average frame size */
  2020. s->frame_size_code = min_br_code << 1;
  2021. while (wpf > 1 && wpf * s->sample_rate / AC3_FRAME_SIZE * 16 > avctx->bit_rate)
  2022. wpf--;
  2023. s->frame_size_min = 2 * wpf;
  2024. } else {
  2025. int best_br = 0, best_code = 0;
  2026. long long best_diff = INT64_MAX;
  2027. for (i = 0; i < 19; i++) {
  2028. int br = (ff_ac3_bitrate_tab[i] >> s->bit_alloc.sr_shift) * 1000;
  2029. long long diff = llabs(br - avctx->bit_rate);
  2030. if (diff < best_diff) {
  2031. best_br = br;
  2032. best_code = i;
  2033. best_diff = diff;
  2034. }
  2035. if (!best_diff)
  2036. break;
  2037. }
  2038. avctx->bit_rate = best_br;
  2039. s->frame_size_code = best_code << 1;
  2040. s->frame_size_min = 2 * ff_ac3_frame_size_tab[s->frame_size_code][s->bit_alloc.sr_code];
  2041. s->num_blks_code = 0x3;
  2042. s->num_blocks = 6;
  2043. }
  2044. s->bit_rate = avctx->bit_rate;
  2045. s->frame_size = s->frame_size_min;
  2046. /* validate cutoff */
  2047. if (avctx->cutoff < 0) {
  2048. av_log(avctx, AV_LOG_ERROR, "invalid cutoff frequency\n");
  2049. return AVERROR(EINVAL);
  2050. }
  2051. s->cutoff = avctx->cutoff;
  2052. if (s->cutoff > (s->sample_rate >> 1))
  2053. s->cutoff = s->sample_rate >> 1;
  2054. ret = ff_ac3_validate_metadata(s);
  2055. if (ret)
  2056. return ret;
  2057. s->rematrixing_enabled = s->options.stereo_rematrixing &&
  2058. (s->channel_mode == AC3_CHMODE_STEREO);
  2059. s->cpl_enabled = s->options.channel_coupling &&
  2060. s->channel_mode >= AC3_CHMODE_STEREO;
  2061. return 0;
  2062. }
  2063. /*
  2064. * Set bandwidth for all channels.
  2065. * The user can optionally supply a cutoff frequency. Otherwise an appropriate
  2066. * default value will be used.
  2067. */
  2068. static av_cold void set_bandwidth(AC3EncodeContext *s)
  2069. {
  2070. int blk, ch, av_uninit(cpl_start);
  2071. if (s->cutoff) {
  2072. /* calculate bandwidth based on user-specified cutoff frequency */
  2073. int fbw_coeffs;
  2074. fbw_coeffs = s->cutoff * 2 * AC3_MAX_COEFS / s->sample_rate;
  2075. s->bandwidth_code = av_clip((fbw_coeffs - 73) / 3, 0, 60);
  2076. } else {
  2077. /* use default bandwidth setting */
  2078. s->bandwidth_code = ac3_bandwidth_tab[s->fbw_channels-1][s->bit_alloc.sr_code][s->frame_size_code/2];
  2079. }
  2080. /* set number of coefficients for each channel */
  2081. for (ch = 1; ch <= s->fbw_channels; ch++) {
  2082. s->start_freq[ch] = 0;
  2083. for (blk = 0; blk < s->num_blocks; blk++)
  2084. s->blocks[blk].end_freq[ch] = s->bandwidth_code * 3 + 73;
  2085. }
  2086. /* LFE channel always has 7 coefs */
  2087. if (s->lfe_on) {
  2088. s->start_freq[s->lfe_channel] = 0;
  2089. for (blk = 0; blk < s->num_blocks; blk++)
  2090. s->blocks[blk].end_freq[ch] = 7;
  2091. }
  2092. /* initialize coupling strategy */
  2093. if (s->cpl_enabled) {
  2094. if (s->options.cpl_start != AC3ENC_OPT_AUTO) {
  2095. cpl_start = s->options.cpl_start;
  2096. } else {
  2097. cpl_start = ac3_coupling_start_tab[s->channel_mode-2][s->bit_alloc.sr_code][s->frame_size_code/2];
  2098. if (cpl_start < 0) {
  2099. if (s->options.channel_coupling == AC3ENC_OPT_AUTO)
  2100. s->cpl_enabled = 0;
  2101. else
  2102. cpl_start = 15;
  2103. }
  2104. }
  2105. }
  2106. if (s->cpl_enabled) {
  2107. int i, cpl_start_band, cpl_end_band;
  2108. uint8_t *cpl_band_sizes = s->cpl_band_sizes;
  2109. cpl_end_band = s->bandwidth_code / 4 + 3;
  2110. cpl_start_band = av_clip(cpl_start, 0, FFMIN(cpl_end_band-1, 15));
  2111. s->num_cpl_subbands = cpl_end_band - cpl_start_band;
  2112. s->num_cpl_bands = 1;
  2113. *cpl_band_sizes = 12;
  2114. for (i = cpl_start_band + 1; i < cpl_end_band; i++) {
  2115. if (ff_eac3_default_cpl_band_struct[i]) {
  2116. *cpl_band_sizes += 12;
  2117. } else {
  2118. s->num_cpl_bands++;
  2119. cpl_band_sizes++;
  2120. *cpl_band_sizes = 12;
  2121. }
  2122. }
  2123. s->start_freq[CPL_CH] = cpl_start_band * 12 + 37;
  2124. s->cpl_end_freq = cpl_end_band * 12 + 37;
  2125. for (blk = 0; blk < s->num_blocks; blk++)
  2126. s->blocks[blk].end_freq[CPL_CH] = s->cpl_end_freq;
  2127. }
  2128. }
  2129. static av_cold int allocate_buffers(AC3EncodeContext *s)
  2130. {
  2131. int blk, ch;
  2132. int channels = s->channels + 1; /* includes coupling channel */
  2133. int channel_blocks = channels * s->num_blocks;
  2134. int total_coefs = AC3_MAX_COEFS * channel_blocks;
  2135. if (s->allocate_sample_buffers(s))
  2136. return AVERROR(ENOMEM);
  2137. if (!FF_ALLOC_TYPED_ARRAY(s->bap_buffer, total_coefs) ||
  2138. !FF_ALLOC_TYPED_ARRAY(s->bap1_buffer, total_coefs) ||
  2139. !FF_ALLOCZ_TYPED_ARRAY(s->mdct_coef_buffer, total_coefs) ||
  2140. !FF_ALLOC_TYPED_ARRAY(s->exp_buffer, total_coefs) ||
  2141. !FF_ALLOC_TYPED_ARRAY(s->grouped_exp_buffer, channel_blocks * 128) ||
  2142. !FF_ALLOC_TYPED_ARRAY(s->psd_buffer, total_coefs) ||
  2143. !FF_ALLOC_TYPED_ARRAY(s->band_psd_buffer, channel_blocks * 64) ||
  2144. !FF_ALLOC_TYPED_ARRAY(s->mask_buffer, channel_blocks * 64) ||
  2145. !FF_ALLOC_TYPED_ARRAY(s->qmant_buffer, total_coefs))
  2146. return AVERROR(ENOMEM);
  2147. if (s->cpl_enabled) {
  2148. if (!FF_ALLOC_TYPED_ARRAY(s->cpl_coord_exp_buffer, channel_blocks * 16) ||
  2149. !FF_ALLOC_TYPED_ARRAY(s->cpl_coord_mant_buffer, channel_blocks * 16))
  2150. return AVERROR(ENOMEM);
  2151. }
  2152. for (blk = 0; blk < s->num_blocks; blk++) {
  2153. AC3Block *block = &s->blocks[blk];
  2154. if (!FF_ALLOCZ_TYPED_ARRAY(block->mdct_coef, channels) ||
  2155. !FF_ALLOCZ_TYPED_ARRAY(block->exp, channels) ||
  2156. !FF_ALLOCZ_TYPED_ARRAY(block->grouped_exp, channels) ||
  2157. !FF_ALLOCZ_TYPED_ARRAY(block->psd, channels) ||
  2158. !FF_ALLOCZ_TYPED_ARRAY(block->band_psd, channels) ||
  2159. !FF_ALLOCZ_TYPED_ARRAY(block->mask, channels) ||
  2160. !FF_ALLOCZ_TYPED_ARRAY(block->qmant, channels))
  2161. return AVERROR(ENOMEM);
  2162. if (s->cpl_enabled) {
  2163. if (!FF_ALLOCZ_TYPED_ARRAY(block->cpl_coord_exp, channels) ||
  2164. !FF_ALLOCZ_TYPED_ARRAY(block->cpl_coord_mant, channels))
  2165. return AVERROR(ENOMEM);
  2166. }
  2167. for (ch = 0; ch < channels; ch++) {
  2168. /* arrangement: block, channel, coeff */
  2169. block->grouped_exp[ch] = &s->grouped_exp_buffer[128 * (blk * channels + ch)];
  2170. block->psd[ch] = &s->psd_buffer [AC3_MAX_COEFS * (blk * channels + ch)];
  2171. block->band_psd[ch] = &s->band_psd_buffer [64 * (blk * channels + ch)];
  2172. block->mask[ch] = &s->mask_buffer [64 * (blk * channels + ch)];
  2173. block->qmant[ch] = &s->qmant_buffer [AC3_MAX_COEFS * (blk * channels + ch)];
  2174. if (s->cpl_enabled) {
  2175. block->cpl_coord_exp[ch] = &s->cpl_coord_exp_buffer [16 * (blk * channels + ch)];
  2176. block->cpl_coord_mant[ch] = &s->cpl_coord_mant_buffer[16 * (blk * channels + ch)];
  2177. }
  2178. /* arrangement: channel, block, coeff */
  2179. block->exp[ch] = &s->exp_buffer [AC3_MAX_COEFS * (s->num_blocks * ch + blk)];
  2180. block->mdct_coef[ch] = &s->mdct_coef_buffer [AC3_MAX_COEFS * (s->num_blocks * ch + blk)];
  2181. }
  2182. }
  2183. if (!s->fixed_point) {
  2184. if (!FF_ALLOCZ_TYPED_ARRAY(s->fixed_coef_buffer, total_coefs))
  2185. return AVERROR(ENOMEM);
  2186. for (blk = 0; blk < s->num_blocks; blk++) {
  2187. AC3Block *block = &s->blocks[blk];
  2188. if (!FF_ALLOCZ_TYPED_ARRAY(block->fixed_coef, channels))
  2189. return AVERROR(ENOMEM);
  2190. for (ch = 0; ch < channels; ch++)
  2191. block->fixed_coef[ch] = &s->fixed_coef_buffer[AC3_MAX_COEFS * (s->num_blocks * ch + blk)];
  2192. }
  2193. } else {
  2194. for (blk = 0; blk < s->num_blocks; blk++) {
  2195. AC3Block *block = &s->blocks[blk];
  2196. if (!FF_ALLOCZ_TYPED_ARRAY(block->fixed_coef, channels))
  2197. return AVERROR(ENOMEM);
  2198. for (ch = 0; ch < channels; ch++)
  2199. block->fixed_coef[ch] = (int32_t *)block->mdct_coef[ch];
  2200. }
  2201. }
  2202. return 0;
  2203. }
  2204. av_cold int ff_ac3_encode_init(AVCodecContext *avctx)
  2205. {
  2206. static AVOnce init_static_once = AV_ONCE_INIT;
  2207. AC3EncodeContext *s = avctx->priv_data;
  2208. int ret, frame_size_58;
  2209. s->avctx = avctx;
  2210. s->eac3 = avctx->codec_id == AV_CODEC_ID_EAC3;
  2211. ret = validate_options(s);
  2212. if (ret)
  2213. return ret;
  2214. avctx->frame_size = AC3_BLOCK_SIZE * s->num_blocks;
  2215. avctx->initial_padding = AC3_BLOCK_SIZE;
  2216. s->bitstream_mode = avctx->audio_service_type;
  2217. if (s->bitstream_mode == AV_AUDIO_SERVICE_TYPE_KARAOKE)
  2218. s->bitstream_mode = 0x7;
  2219. s->bits_written = 0;
  2220. s->samples_written = 0;
  2221. /* calculate crc_inv for both possible frame sizes */
  2222. frame_size_58 = (( s->frame_size >> 2) + ( s->frame_size >> 4)) << 1;
  2223. s->crc_inv[0] = pow_poly((CRC16_POLY >> 1), (8 * frame_size_58) - 16, CRC16_POLY);
  2224. if (s->bit_alloc.sr_code == 1) {
  2225. frame_size_58 = (((s->frame_size+2) >> 2) + ((s->frame_size+2) >> 4)) << 1;
  2226. s->crc_inv[1] = pow_poly((CRC16_POLY >> 1), (8 * frame_size_58) - 16, CRC16_POLY);
  2227. }
  2228. if (CONFIG_EAC3_ENCODER && s->eac3) {
  2229. static AVOnce init_static_once_eac3 = AV_ONCE_INIT;
  2230. ff_thread_once(&init_static_once_eac3, ff_eac3_exponent_init);
  2231. s->output_frame_header = ff_eac3_output_frame_header;
  2232. } else
  2233. s->output_frame_header = ac3_output_frame_header;
  2234. set_bandwidth(s);
  2235. bit_alloc_init(s);
  2236. ret = s->mdct_init(s);
  2237. if (ret)
  2238. return ret;
  2239. ret = allocate_buffers(s);
  2240. if (ret)
  2241. return ret;
  2242. ff_audiodsp_init(&s->adsp);
  2243. ff_me_cmp_init(&s->mecc, avctx);
  2244. ff_ac3dsp_init(&s->ac3dsp, avctx->flags & AV_CODEC_FLAG_BITEXACT);
  2245. dprint_options(s);
  2246. ff_thread_once(&init_static_once, exponent_init);
  2247. return 0;
  2248. }