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.

132 lines
5.3KB

  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. void ff_eac3_set_cpl_states(AC3EncodeContext *s)
  29. {
  30. int ch, blk;
  31. int first_cpl_coords[AC3_MAX_CHANNELS];
  32. /* set first cpl coords */
  33. for (ch = 1; ch <= s->fbw_channels; ch++)
  34. first_cpl_coords[ch] = 1;
  35. for (blk = 0; blk < AC3_MAX_BLOCKS; blk++) {
  36. AC3Block *block = &s->blocks[blk];
  37. for (ch = 1; ch <= s->fbw_channels; ch++) {
  38. if (block->channel_in_cpl[ch]) {
  39. if (first_cpl_coords[ch]) {
  40. block->new_cpl_coords = 2;
  41. first_cpl_coords[ch] = 0;
  42. }
  43. } else {
  44. first_cpl_coords[ch] = 1;
  45. }
  46. }
  47. }
  48. /* set first cpl leak */
  49. for (blk = 0; blk < AC3_MAX_BLOCKS; blk++) {
  50. AC3Block *block = &s->blocks[blk];
  51. if (block->cpl_in_use) {
  52. block->new_cpl_leak = 2;
  53. break;
  54. }
  55. }
  56. }
  57. void ff_eac3_output_frame_header(AC3EncodeContext *s)
  58. {
  59. int blk, ch;
  60. AC3EncOptions *opt = &s->options;
  61. put_bits(&s->pb, 16, 0x0b77); /* sync word */
  62. /* BSI header */
  63. put_bits(&s->pb, 2, 0); /* stream type = independent */
  64. put_bits(&s->pb, 3, 0); /* substream id = 0 */
  65. put_bits(&s->pb, 11, (s->frame_size / 2) - 1); /* frame size */
  66. if (s->bit_alloc.sr_shift) {
  67. put_bits(&s->pb, 2, 0x3); /* fscod2 */
  68. put_bits(&s->pb, 2, s->bit_alloc.sr_code); /* sample rate code */
  69. } else {
  70. put_bits(&s->pb, 2, s->bit_alloc.sr_code); /* sample rate code */
  71. put_bits(&s->pb, 2, 0x3); /* number of blocks = 6 */
  72. }
  73. put_bits(&s->pb, 3, s->channel_mode); /* audio coding mode */
  74. put_bits(&s->pb, 1, s->lfe_on); /* LFE channel indicator */
  75. put_bits(&s->pb, 5, s->bitstream_id); /* bitstream id (EAC3=16) */
  76. put_bits(&s->pb, 5, -opt->dialogue_level); /* dialogue normalization level */
  77. put_bits(&s->pb, 1, 0); /* no compression gain */
  78. put_bits(&s->pb, 1, 0); /* no mixing metadata */
  79. /* TODO: mixing metadata */
  80. put_bits(&s->pb, 1, 0); /* no info metadata */
  81. /* TODO: info metadata */
  82. put_bits(&s->pb, 1, 0); /* no additional bit stream info */
  83. /* frame header */
  84. put_bits(&s->pb, 1, 1); /* exponent strategy syntax = each block */
  85. put_bits(&s->pb, 1, 0); /* aht enabled = no */
  86. put_bits(&s->pb, 2, 0); /* snr offset strategy = 1 */
  87. put_bits(&s->pb, 1, 0); /* transient pre-noise processing enabled = no */
  88. put_bits(&s->pb, 1, 0); /* block switch syntax enabled = no */
  89. put_bits(&s->pb, 1, 0); /* dither flag syntax enabled = no */
  90. put_bits(&s->pb, 1, 0); /* bit allocation model syntax enabled = no */
  91. put_bits(&s->pb, 1, 0); /* fast gain codes enabled = no */
  92. put_bits(&s->pb, 1, 0); /* dba syntax enabled = no */
  93. put_bits(&s->pb, 1, 0); /* skip field syntax enabled = no */
  94. put_bits(&s->pb, 1, 0); /* spx enabled = no */
  95. /* coupling strategy use flags */
  96. if (s->channel_mode > AC3_CHMODE_MONO) {
  97. put_bits(&s->pb, 1, s->blocks[0].cpl_in_use);
  98. for (blk = 1; blk < AC3_MAX_BLOCKS; blk++) {
  99. AC3Block *block = &s->blocks[blk];
  100. put_bits(&s->pb, 1, block->new_cpl_strategy);
  101. if (block->new_cpl_strategy)
  102. put_bits(&s->pb, 1, block->cpl_in_use);
  103. }
  104. }
  105. /* exponent strategy */
  106. for (blk = 0; blk < AC3_MAX_BLOCKS; blk++)
  107. for (ch = !s->blocks[blk].cpl_in_use; ch <= s->fbw_channels; ch++)
  108. put_bits(&s->pb, 2, s->exp_strategy[ch][blk]);
  109. if (s->lfe_on) {
  110. for (blk = 0; blk < AC3_MAX_BLOCKS; blk++)
  111. put_bits(&s->pb, 1, s->exp_strategy[s->lfe_channel][blk]);
  112. }
  113. /* E-AC-3 to AC-3 converter exponent strategy (unfortunately not optional...) */
  114. for (ch = 1; ch <= s->fbw_channels; ch++)
  115. put_bits(&s->pb, 5, 0);
  116. /* snr offsets */
  117. put_bits(&s->pb, 6, s->coarse_snr_offset);
  118. put_bits(&s->pb, 4, s->fine_snr_offset[1]);
  119. /* block start info */
  120. put_bits(&s->pb, 1, 0);
  121. }