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.

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