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.

211 lines
7.9KB

  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 = { "E-AC-3 Encoder", av_default_item_name,
  32. eac3_options, LIBAVUTIL_VERSION_INT };
  33. /**
  34. * LUT for finding a matching frame exponent strategy index from a set of
  35. * exponent strategies for a single channel across all 6 blocks.
  36. */
  37. static int8_t eac3_frame_expstr_index_tab[3][4][4][4][4][4];
  38. void ff_eac3_exponent_init(void)
  39. {
  40. int i;
  41. memset(eac3_frame_expstr_index_tab, -1, sizeof(eac3_frame_expstr_index_tab));
  42. for (i = 0; i < 32; i++) {
  43. eac3_frame_expstr_index_tab[ff_eac3_frm_expstr[i][0]-1]
  44. [ff_eac3_frm_expstr[i][1]]
  45. [ff_eac3_frm_expstr[i][2]]
  46. [ff_eac3_frm_expstr[i][3]]
  47. [ff_eac3_frm_expstr[i][4]]
  48. [ff_eac3_frm_expstr[i][5]] = i;
  49. }
  50. }
  51. void ff_eac3_get_frame_exp_strategy(AC3EncodeContext *s)
  52. {
  53. int ch;
  54. s->use_frame_exp_strategy = 1;
  55. for (ch = !s->cpl_on; ch <= s->fbw_channels; ch++) {
  56. int expstr = eac3_frame_expstr_index_tab[s->exp_strategy[ch][0]-1]
  57. [s->exp_strategy[ch][1]]
  58. [s->exp_strategy[ch][2]]
  59. [s->exp_strategy[ch][3]]
  60. [s->exp_strategy[ch][4]]
  61. [s->exp_strategy[ch][5]];
  62. if (expstr < 0) {
  63. s->use_frame_exp_strategy = 0;
  64. break;
  65. }
  66. s->frame_exp_strategy[ch] = expstr;
  67. }
  68. }
  69. void ff_eac3_set_cpl_states(AC3EncodeContext *s)
  70. {
  71. int ch, blk;
  72. int first_cpl_coords[AC3_MAX_CHANNELS];
  73. /* set first cpl coords */
  74. for (ch = 1; ch <= s->fbw_channels; ch++)
  75. first_cpl_coords[ch] = 1;
  76. for (blk = 0; blk < AC3_MAX_BLOCKS; blk++) {
  77. AC3Block *block = &s->blocks[blk];
  78. for (ch = 1; ch <= s->fbw_channels; ch++) {
  79. if (block->channel_in_cpl[ch]) {
  80. if (first_cpl_coords[ch]) {
  81. block->new_cpl_coords = 2;
  82. first_cpl_coords[ch] = 0;
  83. }
  84. } else {
  85. first_cpl_coords[ch] = 1;
  86. }
  87. }
  88. }
  89. /* set first cpl leak */
  90. for (blk = 0; blk < AC3_MAX_BLOCKS; blk++) {
  91. AC3Block *block = &s->blocks[blk];
  92. if (block->cpl_in_use) {
  93. block->new_cpl_leak = 2;
  94. break;
  95. }
  96. }
  97. }
  98. void ff_eac3_output_frame_header(AC3EncodeContext *s)
  99. {
  100. int blk, ch;
  101. AC3EncOptions *opt = &s->options;
  102. put_bits(&s->pb, 16, 0x0b77); /* sync word */
  103. /* BSI header */
  104. put_bits(&s->pb, 2, 0); /* stream type = independent */
  105. put_bits(&s->pb, 3, 0); /* substream id = 0 */
  106. put_bits(&s->pb, 11, (s->frame_size / 2) - 1); /* frame size */
  107. if (s->bit_alloc.sr_shift) {
  108. put_bits(&s->pb, 2, 0x3); /* fscod2 */
  109. put_bits(&s->pb, 2, s->bit_alloc.sr_code); /* sample rate code */
  110. } else {
  111. put_bits(&s->pb, 2, s->bit_alloc.sr_code); /* sample rate code */
  112. put_bits(&s->pb, 2, 0x3); /* number of blocks = 6 */
  113. }
  114. put_bits(&s->pb, 3, s->channel_mode); /* audio coding mode */
  115. put_bits(&s->pb, 1, s->lfe_on); /* LFE channel indicator */
  116. put_bits(&s->pb, 5, s->bitstream_id); /* bitstream id (EAC3=16) */
  117. put_bits(&s->pb, 5, -opt->dialogue_level); /* dialogue normalization level */
  118. put_bits(&s->pb, 1, 0); /* no compression gain */
  119. put_bits(&s->pb, 1, 0); /* no mixing metadata */
  120. /* TODO: mixing metadata */
  121. put_bits(&s->pb, 1, 0); /* no info metadata */
  122. /* TODO: info metadata */
  123. put_bits(&s->pb, 1, 0); /* no additional bit stream info */
  124. /* frame header */
  125. put_bits(&s->pb, 1, !s->use_frame_exp_strategy);/* exponent strategy syntax */
  126. put_bits(&s->pb, 1, 0); /* aht enabled = no */
  127. put_bits(&s->pb, 2, 0); /* snr offset strategy = 1 */
  128. put_bits(&s->pb, 1, 0); /* transient pre-noise processing enabled = no */
  129. put_bits(&s->pb, 1, 0); /* block switch syntax enabled = no */
  130. put_bits(&s->pb, 1, 0); /* dither flag syntax enabled = no */
  131. put_bits(&s->pb, 1, 0); /* bit allocation model syntax enabled = no */
  132. put_bits(&s->pb, 1, 0); /* fast gain codes enabled = no */
  133. put_bits(&s->pb, 1, 0); /* dba syntax enabled = no */
  134. put_bits(&s->pb, 1, 0); /* skip field syntax enabled = no */
  135. put_bits(&s->pb, 1, 0); /* spx enabled = no */
  136. /* coupling strategy use flags */
  137. if (s->channel_mode > AC3_CHMODE_MONO) {
  138. put_bits(&s->pb, 1, s->blocks[0].cpl_in_use);
  139. for (blk = 1; blk < AC3_MAX_BLOCKS; blk++) {
  140. AC3Block *block = &s->blocks[blk];
  141. put_bits(&s->pb, 1, block->new_cpl_strategy);
  142. if (block->new_cpl_strategy)
  143. put_bits(&s->pb, 1, block->cpl_in_use);
  144. }
  145. }
  146. /* exponent strategy */
  147. if (s->use_frame_exp_strategy) {
  148. for (ch = !s->cpl_on; ch <= s->fbw_channels; ch++)
  149. put_bits(&s->pb, 5, s->frame_exp_strategy[ch]);
  150. } else {
  151. for (blk = 0; blk < AC3_MAX_BLOCKS; blk++)
  152. for (ch = !s->blocks[blk].cpl_in_use; ch <= s->fbw_channels; ch++)
  153. put_bits(&s->pb, 2, s->exp_strategy[ch][blk]);
  154. }
  155. if (s->lfe_on) {
  156. for (blk = 0; blk < AC3_MAX_BLOCKS; blk++)
  157. put_bits(&s->pb, 1, s->exp_strategy[s->lfe_channel][blk]);
  158. }
  159. /* E-AC-3 to AC-3 converter exponent strategy (unfortunately not optional...) */
  160. for (ch = 1; ch <= s->fbw_channels; ch++) {
  161. if (s->use_frame_exp_strategy)
  162. put_bits(&s->pb, 5, s->frame_exp_strategy[ch]);
  163. else
  164. put_bits(&s->pb, 5, 0);
  165. }
  166. /* snr offsets */
  167. put_bits(&s->pb, 6, s->coarse_snr_offset);
  168. put_bits(&s->pb, 4, s->fine_snr_offset[1]);
  169. /* block start info */
  170. put_bits(&s->pb, 1, 0);
  171. }
  172. #if CONFIG_EAC3_ENCODER
  173. AVCodec ff_eac3_encoder = {
  174. .name = "eac3",
  175. .type = AVMEDIA_TYPE_AUDIO,
  176. .id = CODEC_ID_EAC3,
  177. .priv_data_size = sizeof(AC3EncodeContext),
  178. .init = ff_ac3_encode_init,
  179. .encode = ff_ac3_float_encode_frame,
  180. .close = ff_ac3_encode_close,
  181. .sample_fmts = (const enum AVSampleFormat[]){AV_SAMPLE_FMT_FLT,AV_SAMPLE_FMT_NONE},
  182. .long_name = NULL_IF_CONFIG_SMALL("ATSC A/52 E-AC-3"),
  183. .priv_class = &eac3enc_class,
  184. .channel_layouts = ff_ac3_channel_layouts,
  185. };
  186. #endif