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.

271 lines
10KB

  1. /*
  2. * E-AC-3 encoder
  3. * Copyright (c) 2011 Justin Ruggles <justin.ruggles@gmail.com>
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. /**
  22. * @file
  23. * E-AC-3 encoder
  24. */
  25. #define CONFIG_AC3ENC_FLOAT 1
  26. #include "libavutil/attributes.h"
  27. #include "ac3enc.h"
  28. #include "eac3enc.h"
  29. #include "eac3_data.h"
  30. #define AC3ENC_TYPE AC3ENC_TYPE_EAC3
  31. #include "ac3enc_opts_template.c"
  32. static const AVClass eac3enc_class = {
  33. .class_name = "E-AC-3 Encoder",
  34. .item_name = av_default_item_name,
  35. .option = ac3_options,
  36. .version = LIBAVUTIL_VERSION_INT,
  37. };
  38. /**
  39. * LUT for finding a matching frame exponent strategy index from a set of
  40. * exponent strategies for a single channel across all 6 blocks.
  41. */
  42. static int8_t eac3_frame_expstr_index_tab[3][4][4][4][4][4];
  43. av_cold void ff_eac3_exponent_init(void)
  44. {
  45. int i;
  46. memset(eac3_frame_expstr_index_tab, -1, sizeof(eac3_frame_expstr_index_tab));
  47. for (i = 0; i < 32; i++) {
  48. eac3_frame_expstr_index_tab[ff_eac3_frm_expstr[i][0]-1]
  49. [ff_eac3_frm_expstr[i][1]]
  50. [ff_eac3_frm_expstr[i][2]]
  51. [ff_eac3_frm_expstr[i][3]]
  52. [ff_eac3_frm_expstr[i][4]]
  53. [ff_eac3_frm_expstr[i][5]] = i;
  54. }
  55. }
  56. void ff_eac3_get_frame_exp_strategy(AC3EncodeContext *s)
  57. {
  58. int ch;
  59. if (s->num_blocks < 6) {
  60. s->use_frame_exp_strategy = 0;
  61. return;
  62. }
  63. s->use_frame_exp_strategy = 1;
  64. for (ch = !s->cpl_on; ch <= s->fbw_channels; ch++) {
  65. int expstr = eac3_frame_expstr_index_tab[s->exp_strategy[ch][0]-1]
  66. [s->exp_strategy[ch][1]]
  67. [s->exp_strategy[ch][2]]
  68. [s->exp_strategy[ch][3]]
  69. [s->exp_strategy[ch][4]]
  70. [s->exp_strategy[ch][5]];
  71. if (expstr < 0) {
  72. s->use_frame_exp_strategy = 0;
  73. break;
  74. }
  75. s->frame_exp_strategy[ch] = expstr;
  76. }
  77. }
  78. void ff_eac3_set_cpl_states(AC3EncodeContext *s)
  79. {
  80. int ch, blk;
  81. int first_cpl_coords[AC3_MAX_CHANNELS];
  82. /* set first cpl coords */
  83. for (ch = 1; ch <= s->fbw_channels; ch++)
  84. first_cpl_coords[ch] = 1;
  85. for (blk = 0; blk < s->num_blocks; blk++) {
  86. AC3Block *block = &s->blocks[blk];
  87. for (ch = 1; ch <= s->fbw_channels; ch++) {
  88. if (block->channel_in_cpl[ch]) {
  89. if (first_cpl_coords[ch]) {
  90. block->new_cpl_coords[ch] = 2;
  91. first_cpl_coords[ch] = 0;
  92. }
  93. } else {
  94. first_cpl_coords[ch] = 1;
  95. }
  96. }
  97. }
  98. /* set first cpl leak */
  99. for (blk = 0; blk < s->num_blocks; blk++) {
  100. AC3Block *block = &s->blocks[blk];
  101. if (block->cpl_in_use) {
  102. block->new_cpl_leak = 2;
  103. break;
  104. }
  105. }
  106. }
  107. void ff_eac3_output_frame_header(AC3EncodeContext *s)
  108. {
  109. int blk, ch;
  110. AC3EncOptions *opt = &s->options;
  111. put_bits(&s->pb, 16, 0x0b77); /* sync word */
  112. /* BSI header */
  113. put_bits(&s->pb, 2, 0); /* stream type = independent */
  114. put_bits(&s->pb, 3, 0); /* substream id = 0 */
  115. put_bits(&s->pb, 11, (s->frame_size / 2) - 1); /* frame size */
  116. if (s->bit_alloc.sr_shift) {
  117. put_bits(&s->pb, 2, 0x3); /* fscod2 */
  118. put_bits(&s->pb, 2, s->bit_alloc.sr_code); /* sample rate code */
  119. } else {
  120. put_bits(&s->pb, 2, s->bit_alloc.sr_code); /* sample rate code */
  121. put_bits(&s->pb, 2, s->num_blks_code); /* number of blocks */
  122. }
  123. put_bits(&s->pb, 3, s->channel_mode); /* audio coding mode */
  124. put_bits(&s->pb, 1, s->lfe_on); /* LFE channel indicator */
  125. put_bits(&s->pb, 5, s->bitstream_id); /* bitstream id (EAC3=16) */
  126. put_bits(&s->pb, 5, -opt->dialogue_level); /* dialogue normalization level */
  127. put_bits(&s->pb, 1, 0); /* no compression gain */
  128. /* mixing metadata*/
  129. put_bits(&s->pb, 1, opt->eac3_mixing_metadata);
  130. if (opt->eac3_mixing_metadata) {
  131. if (s->channel_mode > AC3_CHMODE_STEREO)
  132. put_bits(&s->pb, 2, opt->preferred_stereo_downmix);
  133. if (s->has_center) {
  134. put_bits(&s->pb, 3, s->ltrt_center_mix_level);
  135. put_bits(&s->pb, 3, s->loro_center_mix_level);
  136. }
  137. if (s->has_surround) {
  138. put_bits(&s->pb, 3, s->ltrt_surround_mix_level);
  139. put_bits(&s->pb, 3, s->loro_surround_mix_level);
  140. }
  141. if (s->lfe_on)
  142. put_bits(&s->pb, 1, 0);
  143. put_bits(&s->pb, 1, 0); /* no program scale */
  144. put_bits(&s->pb, 1, 0); /* no ext program scale */
  145. put_bits(&s->pb, 2, 0); /* no mixing parameters */
  146. if (s->channel_mode < AC3_CHMODE_STEREO)
  147. put_bits(&s->pb, 1, 0); /* no pan info */
  148. put_bits(&s->pb, 1, 0); /* no frame mix config info */
  149. }
  150. /* info metadata*/
  151. put_bits(&s->pb, 1, opt->eac3_info_metadata);
  152. if (opt->eac3_info_metadata) {
  153. put_bits(&s->pb, 3, s->bitstream_mode);
  154. put_bits(&s->pb, 1, opt->copyright);
  155. put_bits(&s->pb, 1, opt->original);
  156. if (s->channel_mode == AC3_CHMODE_STEREO) {
  157. put_bits(&s->pb, 2, opt->dolby_surround_mode);
  158. put_bits(&s->pb, 2, opt->dolby_headphone_mode);
  159. }
  160. if (s->channel_mode >= AC3_CHMODE_2F2R)
  161. put_bits(&s->pb, 2, opt->dolby_surround_ex_mode);
  162. put_bits(&s->pb, 1, opt->audio_production_info);
  163. if (opt->audio_production_info) {
  164. put_bits(&s->pb, 5, opt->mixing_level - 80);
  165. put_bits(&s->pb, 2, opt->room_type);
  166. put_bits(&s->pb, 1, opt->ad_converter_type);
  167. }
  168. put_bits(&s->pb, 1, 0);
  169. }
  170. if (s->num_blocks != 6)
  171. put_bits(&s->pb, 1, !(s->avctx->frame_number % 6)); /* converter sync flag */
  172. put_bits(&s->pb, 1, 0); /* no additional bit stream info */
  173. /* frame header */
  174. if (s->num_blocks == 6) {
  175. put_bits(&s->pb, 1, !s->use_frame_exp_strategy);/* exponent strategy syntax */
  176. put_bits(&s->pb, 1, 0); /* aht enabled = no */
  177. }
  178. put_bits(&s->pb, 2, 0); /* snr offset strategy = 1 */
  179. put_bits(&s->pb, 1, 0); /* transient pre-noise processing enabled = no */
  180. put_bits(&s->pb, 1, 0); /* block switch syntax enabled = no */
  181. put_bits(&s->pb, 1, 0); /* dither flag syntax enabled = no */
  182. put_bits(&s->pb, 1, 0); /* bit allocation model syntax enabled = no */
  183. put_bits(&s->pb, 1, 0); /* fast gain codes enabled = no */
  184. put_bits(&s->pb, 1, 0); /* dba syntax enabled = no */
  185. put_bits(&s->pb, 1, 0); /* skip field syntax enabled = no */
  186. put_bits(&s->pb, 1, 0); /* spx enabled = no */
  187. /* coupling strategy use flags */
  188. if (s->channel_mode > AC3_CHMODE_MONO) {
  189. put_bits(&s->pb, 1, s->blocks[0].cpl_in_use);
  190. for (blk = 1; blk < s->num_blocks; blk++) {
  191. AC3Block *block = &s->blocks[blk];
  192. put_bits(&s->pb, 1, block->new_cpl_strategy);
  193. if (block->new_cpl_strategy)
  194. put_bits(&s->pb, 1, block->cpl_in_use);
  195. }
  196. }
  197. /* exponent strategy */
  198. if (s->use_frame_exp_strategy) {
  199. for (ch = !s->cpl_on; ch <= s->fbw_channels; ch++)
  200. put_bits(&s->pb, 5, s->frame_exp_strategy[ch]);
  201. } else {
  202. for (blk = 0; blk < s->num_blocks; blk++)
  203. for (ch = !s->blocks[blk].cpl_in_use; ch <= s->fbw_channels; ch++)
  204. put_bits(&s->pb, 2, s->exp_strategy[ch][blk]);
  205. }
  206. if (s->lfe_on) {
  207. for (blk = 0; blk < s->num_blocks; blk++)
  208. put_bits(&s->pb, 1, s->exp_strategy[s->lfe_channel][blk]);
  209. }
  210. /* E-AC-3 to AC-3 converter exponent strategy (not optional when num blocks == 6) */
  211. if (s->num_blocks != 6) {
  212. put_bits(&s->pb, 1, 0);
  213. } else {
  214. for (ch = 1; ch <= s->fbw_channels; ch++) {
  215. if (s->use_frame_exp_strategy)
  216. put_bits(&s->pb, 5, s->frame_exp_strategy[ch]);
  217. else
  218. put_bits(&s->pb, 5, 0);
  219. }
  220. }
  221. /* snr offsets */
  222. put_bits(&s->pb, 6, s->coarse_snr_offset);
  223. put_bits(&s->pb, 4, s->fine_snr_offset[1]);
  224. /* block start info */
  225. if (s->num_blocks > 1)
  226. put_bits(&s->pb, 1, 0);
  227. }
  228. #if CONFIG_EAC3_ENCODER
  229. AVCodec ff_eac3_encoder = {
  230. .name = "eac3",
  231. .type = AVMEDIA_TYPE_AUDIO,
  232. .id = AV_CODEC_ID_EAC3,
  233. .priv_data_size = sizeof(AC3EncodeContext),
  234. .init = ff_ac3_encode_init,
  235. .encode2 = ff_ac3_float_encode_frame,
  236. .close = ff_ac3_encode_close,
  237. .sample_fmts = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_FLTP,
  238. AV_SAMPLE_FMT_NONE },
  239. .long_name = NULL_IF_CONFIG_SMALL("ATSC A/52 E-AC-3"),
  240. .priv_class = &eac3enc_class,
  241. .channel_layouts = ff_ac3_channel_layouts,
  242. .defaults = ac3_defaults,
  243. };
  244. #endif