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.

208 lines
6.5KB

  1. /*
  2. * AC3 parser
  3. * Copyright (c) 2003 Fabrice Bellard.
  4. * Copyright (c) 2003 Michael Niedermayer.
  5. *
  6. * This file is part of FFmpeg.
  7. *
  8. * FFmpeg is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * FFmpeg is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with FFmpeg; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. #include "parser.h"
  23. #include "ac3_parser.h"
  24. #include "aac_ac3_parser.h"
  25. #include "bitstream.h"
  26. #define AC3_HEADER_SIZE 7
  27. static const uint8_t eac3_blocks[4] = {
  28. 1, 2, 3, 6
  29. };
  30. /**
  31. * Table for center mix levels
  32. * reference: Section 5.4.2.4 cmixlev
  33. */
  34. static const uint8_t center_levels[4] = { 2, 3, 4, 3 };
  35. /**
  36. * Table for surround mix levels
  37. * reference: Section 5.4.2.5 surmixlev
  38. */
  39. static const uint8_t surround_levels[4] = { 2, 4, 0, 4 };
  40. int ff_ac3_parse_header(GetBitContext *gbc, AC3HeaderInfo *hdr)
  41. {
  42. int frame_size_code;
  43. int num_blocks;
  44. memset(hdr, 0, sizeof(*hdr));
  45. hdr->sync_word = get_bits(gbc, 16);
  46. if(hdr->sync_word != 0x0B77)
  47. return AC3_PARSE_ERROR_SYNC;
  48. /* read ahead to bsid to distinguish between AC-3 and E-AC-3 */
  49. hdr->bitstream_id = show_bits_long(gbc, 29) & 0x1F;
  50. if(hdr->bitstream_id > 16)
  51. return AC3_PARSE_ERROR_BSID;
  52. if(hdr->bitstream_id <= 10) {
  53. /* Normal AC-3 */
  54. hdr->crc1 = get_bits(gbc, 16);
  55. hdr->sr_code = get_bits(gbc, 2);
  56. if(hdr->sr_code == 3)
  57. return AC3_PARSE_ERROR_SAMPLE_RATE;
  58. frame_size_code = get_bits(gbc, 6);
  59. if(frame_size_code > 37)
  60. return AC3_PARSE_ERROR_FRAME_SIZE;
  61. skip_bits(gbc, 5); // skip bsid, already got it
  62. skip_bits(gbc, 3); // skip bitstream mode
  63. hdr->channel_mode = get_bits(gbc, 3);
  64. /* set default mix levels */
  65. hdr->center_mix_level = 3; // -4.5dB
  66. hdr->surround_mix_level = 4; // -6.0dB
  67. if(hdr->channel_mode == AC3_CHMODE_STEREO) {
  68. skip_bits(gbc, 2); // skip dsurmod
  69. } else {
  70. if((hdr->channel_mode & 1) && hdr->channel_mode != AC3_CHMODE_MONO)
  71. hdr->center_mix_level = center_levels[get_bits(gbc, 2)];
  72. if(hdr->channel_mode & 4)
  73. hdr->surround_mix_level = surround_levels[get_bits(gbc, 2)];
  74. }
  75. hdr->lfe_on = get_bits1(gbc);
  76. hdr->sr_shift = FFMAX(hdr->bitstream_id, 8) - 8;
  77. hdr->sample_rate = ff_ac3_sample_rate_tab[hdr->sr_code] >> hdr->sr_shift;
  78. hdr->bit_rate = (ff_ac3_bitrate_tab[frame_size_code>>1] * 1000) >> hdr->sr_shift;
  79. hdr->channels = ff_ac3_channels_tab[hdr->channel_mode] + hdr->lfe_on;
  80. hdr->frame_size = ff_ac3_frame_size_tab[frame_size_code][hdr->sr_code] * 2;
  81. hdr->frame_type = EAC3_FRAME_TYPE_AC3_CONVERT; //EAC3_FRAME_TYPE_INDEPENDENT;
  82. } else {
  83. /* Enhanced AC-3 */
  84. hdr->crc1 = 0;
  85. hdr->frame_type = get_bits(gbc, 2);
  86. if(hdr->frame_type == EAC3_FRAME_TYPE_RESERVED)
  87. return AC3_PARSE_ERROR_FRAME_TYPE;
  88. skip_bits(gbc, 3); // skip substream id
  89. hdr->frame_size = (get_bits(gbc, 11) + 1) << 1;
  90. if(hdr->frame_size < AC3_HEADER_SIZE)
  91. return AC3_PARSE_ERROR_FRAME_SIZE;
  92. hdr->sr_code = get_bits(gbc, 2);
  93. if (hdr->sr_code == 3) {
  94. int sr_code2 = get_bits(gbc, 2);
  95. if(sr_code2 == 3)
  96. return AC3_PARSE_ERROR_SAMPLE_RATE;
  97. hdr->sample_rate = ff_ac3_sample_rate_tab[sr_code2] / 2;
  98. hdr->sr_shift = 1;
  99. num_blocks = 6;
  100. } else {
  101. num_blocks = eac3_blocks[get_bits(gbc, 2)];
  102. hdr->sample_rate = ff_ac3_sample_rate_tab[hdr->sr_code];
  103. hdr->sr_shift = 0;
  104. }
  105. hdr->channel_mode = get_bits(gbc, 3);
  106. hdr->lfe_on = get_bits1(gbc);
  107. hdr->bit_rate = (uint32_t)(8.0 * hdr->frame_size * hdr->sample_rate /
  108. (num_blocks * 256.0));
  109. hdr->channels = ff_ac3_channels_tab[hdr->channel_mode] + hdr->lfe_on;
  110. }
  111. return 0;
  112. }
  113. int ff_ac3_parse_header_full(GetBitContext *gbc, AC3HeaderInfo *hdr){
  114. int ret, i;
  115. ret = ff_ac3_parse_header(gbc, hdr);
  116. if(!ret){
  117. if(hdr->bitstream_id>10){
  118. /* Enhanced AC-3 */
  119. skip_bits(gbc, 5); // skip bitstream id
  120. /* skip dialog normalization and compression gain */
  121. for (i = 0; i < (hdr->channel_mode ? 1 : 2); i++) {
  122. skip_bits(gbc, 5); // skip dialog normalization
  123. if (get_bits1(gbc)) {
  124. skip_bits(gbc, 8); //skip Compression gain word
  125. }
  126. }
  127. /* dependent stream channel map */
  128. if (hdr->frame_type == EAC3_FRAME_TYPE_DEPENDENT && get_bits1(gbc)) {
  129. hdr->channel_map = get_bits(gbc, 16); //custom channel map
  130. return 0;
  131. }
  132. }
  133. //default channel map based on acmod and lfeon
  134. hdr->channel_map = ff_eac3_default_chmap[hdr->channel_mode];
  135. if(hdr->lfe_on)
  136. hdr->channel_map |= AC3_CHMAP_LFE;
  137. }
  138. return ret;
  139. }
  140. static int ac3_sync(uint64_t state, AACAC3ParseContext *hdr_info,
  141. int *need_next_header, int *new_frame_start)
  142. {
  143. int err;
  144. uint64_t tmp = be2me_64(state);
  145. AC3HeaderInfo hdr;
  146. GetBitContext gbc;
  147. init_get_bits(&gbc, ((uint8_t *)&tmp)+8-AC3_HEADER_SIZE, 54);
  148. err = ff_ac3_parse_header(&gbc, &hdr);
  149. if(err < 0)
  150. return 0;
  151. hdr_info->sample_rate = hdr.sample_rate;
  152. hdr_info->bit_rate = hdr.bit_rate;
  153. hdr_info->channels = hdr.channels;
  154. hdr_info->samples = AC3_FRAME_SIZE;
  155. *need_next_header = (hdr.frame_type != EAC3_FRAME_TYPE_AC3_CONVERT);
  156. *new_frame_start = (hdr.frame_type != EAC3_FRAME_TYPE_DEPENDENT);
  157. return hdr.frame_size;
  158. }
  159. static av_cold int ac3_parse_init(AVCodecParserContext *s1)
  160. {
  161. AACAC3ParseContext *s = s1->priv_data;
  162. s->header_size = AC3_HEADER_SIZE;
  163. s->sync = ac3_sync;
  164. return 0;
  165. }
  166. AVCodecParser ac3_parser = {
  167. { CODEC_ID_AC3 },
  168. sizeof(AACAC3ParseContext),
  169. ac3_parse_init,
  170. ff_aac_ac3_parse,
  171. NULL,
  172. };