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.

156 lines
6.1KB

  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. #define AC3ENC_TYPE AC3ENC_TYPE_EAC3
  29. #include "ac3enc_opts_template.c"
  30. static AVClass eac3enc_class = { "E-AC-3 Encoder", av_default_item_name,
  31. eac3_options, LIBAVUTIL_VERSION_INT };
  32. void ff_eac3_set_cpl_states(AC3EncodeContext *s)
  33. {
  34. int ch, blk;
  35. int first_cpl_coords[AC3_MAX_CHANNELS];
  36. /* set first cpl coords */
  37. for (ch = 1; ch <= s->fbw_channels; ch++)
  38. first_cpl_coords[ch] = 1;
  39. for (blk = 0; blk < AC3_MAX_BLOCKS; blk++) {
  40. AC3Block *block = &s->blocks[blk];
  41. for (ch = 1; ch <= s->fbw_channels; ch++) {
  42. if (block->channel_in_cpl[ch]) {
  43. if (first_cpl_coords[ch]) {
  44. block->new_cpl_coords = 2;
  45. first_cpl_coords[ch] = 0;
  46. }
  47. } else {
  48. first_cpl_coords[ch] = 1;
  49. }
  50. }
  51. }
  52. /* set first cpl leak */
  53. for (blk = 0; blk < AC3_MAX_BLOCKS; blk++) {
  54. AC3Block *block = &s->blocks[blk];
  55. if (block->cpl_in_use) {
  56. block->new_cpl_leak = 2;
  57. break;
  58. }
  59. }
  60. }
  61. void ff_eac3_output_frame_header(AC3EncodeContext *s)
  62. {
  63. int blk, ch;
  64. AC3EncOptions *opt = &s->options;
  65. put_bits(&s->pb, 16, 0x0b77); /* sync word */
  66. /* BSI header */
  67. put_bits(&s->pb, 2, 0); /* stream type = independent */
  68. put_bits(&s->pb, 3, 0); /* substream id = 0 */
  69. put_bits(&s->pb, 11, (s->frame_size / 2) - 1); /* frame size */
  70. if (s->bit_alloc.sr_shift) {
  71. put_bits(&s->pb, 2, 0x3); /* fscod2 */
  72. put_bits(&s->pb, 2, s->bit_alloc.sr_code); /* sample rate code */
  73. } else {
  74. put_bits(&s->pb, 2, s->bit_alloc.sr_code); /* sample rate code */
  75. put_bits(&s->pb, 2, 0x3); /* number of blocks = 6 */
  76. }
  77. put_bits(&s->pb, 3, s->channel_mode); /* audio coding mode */
  78. put_bits(&s->pb, 1, s->lfe_on); /* LFE channel indicator */
  79. put_bits(&s->pb, 5, s->bitstream_id); /* bitstream id (EAC3=16) */
  80. put_bits(&s->pb, 5, -opt->dialogue_level); /* dialogue normalization level */
  81. put_bits(&s->pb, 1, 0); /* no compression gain */
  82. put_bits(&s->pb, 1, 0); /* no mixing metadata */
  83. /* TODO: mixing metadata */
  84. put_bits(&s->pb, 1, 0); /* no info metadata */
  85. /* TODO: info metadata */
  86. put_bits(&s->pb, 1, 0); /* no additional bit stream info */
  87. /* frame header */
  88. put_bits(&s->pb, 1, 1); /* exponent strategy syntax = each block */
  89. put_bits(&s->pb, 1, 0); /* aht enabled = no */
  90. put_bits(&s->pb, 2, 0); /* snr offset strategy = 1 */
  91. put_bits(&s->pb, 1, 0); /* transient pre-noise processing enabled = no */
  92. put_bits(&s->pb, 1, 0); /* block switch syntax enabled = no */
  93. put_bits(&s->pb, 1, 0); /* dither flag syntax enabled = no */
  94. put_bits(&s->pb, 1, 0); /* bit allocation model syntax enabled = no */
  95. put_bits(&s->pb, 1, 0); /* fast gain codes enabled = no */
  96. put_bits(&s->pb, 1, 0); /* dba syntax enabled = no */
  97. put_bits(&s->pb, 1, 0); /* skip field syntax enabled = no */
  98. put_bits(&s->pb, 1, 0); /* spx enabled = no */
  99. /* coupling strategy use flags */
  100. if (s->channel_mode > AC3_CHMODE_MONO) {
  101. put_bits(&s->pb, 1, s->blocks[0].cpl_in_use);
  102. for (blk = 1; blk < AC3_MAX_BLOCKS; blk++) {
  103. AC3Block *block = &s->blocks[blk];
  104. put_bits(&s->pb, 1, block->new_cpl_strategy);
  105. if (block->new_cpl_strategy)
  106. put_bits(&s->pb, 1, block->cpl_in_use);
  107. }
  108. }
  109. /* exponent strategy */
  110. for (blk = 0; blk < AC3_MAX_BLOCKS; blk++)
  111. for (ch = !s->blocks[blk].cpl_in_use; ch <= s->fbw_channels; ch++)
  112. put_bits(&s->pb, 2, s->exp_strategy[ch][blk]);
  113. if (s->lfe_on) {
  114. for (blk = 0; blk < AC3_MAX_BLOCKS; blk++)
  115. put_bits(&s->pb, 1, s->exp_strategy[s->lfe_channel][blk]);
  116. }
  117. /* E-AC-3 to AC-3 converter exponent strategy (unfortunately not optional...) */
  118. for (ch = 1; ch <= s->fbw_channels; ch++)
  119. put_bits(&s->pb, 5, 0);
  120. /* snr offsets */
  121. put_bits(&s->pb, 6, s->coarse_snr_offset);
  122. put_bits(&s->pb, 4, s->fine_snr_offset[1]);
  123. /* block start info */
  124. put_bits(&s->pb, 1, 0);
  125. }
  126. #if CONFIG_EAC3_ENCODER
  127. AVCodec ff_eac3_encoder = {
  128. .name = "eac3",
  129. .type = AVMEDIA_TYPE_AUDIO,
  130. .id = CODEC_ID_EAC3,
  131. .priv_data_size = sizeof(AC3EncodeContext),
  132. .init = ff_ac3_encode_init,
  133. .encode = ff_ac3_float_encode_frame,
  134. .close = ff_ac3_encode_close,
  135. .sample_fmts = (const enum AVSampleFormat[]){AV_SAMPLE_FMT_FLT,AV_SAMPLE_FMT_NONE},
  136. .long_name = NULL_IF_CONFIG_SMALL("ATSC A/52 E-AC-3"),
  137. .priv_class = &eac3enc_class,
  138. .channel_layouts = ff_ac3_channel_layouts,
  139. };
  140. #endif