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.

157 lines
4.6KB

  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. int ff_ac3_parse_header(const uint8_t buf[7], AC3HeaderInfo *hdr)
  31. {
  32. GetBitContext gbc;
  33. int frame_size_code;
  34. int num_blocks;
  35. memset(hdr, 0, sizeof(*hdr));
  36. init_get_bits(&gbc, buf, 54);
  37. hdr->sync_word = get_bits(&gbc, 16);
  38. if(hdr->sync_word != 0x0B77)
  39. return AC3_PARSE_ERROR_SYNC;
  40. /* read ahead to bsid to distinguish between AC-3 and E-AC-3 */
  41. hdr->bitstream_id = show_bits_long(&gbc, 29) & 0x1F;
  42. if(hdr->bitstream_id > 16)
  43. return AC3_PARSE_ERROR_BSID;
  44. if(hdr->bitstream_id <= 10) {
  45. /* Normal AC-3 */
  46. hdr->crc1 = get_bits(&gbc, 16);
  47. hdr->sr_code = get_bits(&gbc, 2);
  48. if(hdr->sr_code == 3)
  49. return AC3_PARSE_ERROR_SAMPLE_RATE;
  50. frame_size_code = get_bits(&gbc, 6);
  51. if(frame_size_code > 37)
  52. return AC3_PARSE_ERROR_FRAME_SIZE;
  53. skip_bits(&gbc, 5); // skip bsid, already got it
  54. skip_bits(&gbc, 3); // skip bitstream mode
  55. hdr->channel_mode = get_bits(&gbc, 3);
  56. if((hdr->channel_mode & 1) && hdr->channel_mode != AC3_CHMODE_MONO) {
  57. skip_bits(&gbc, 2); // skip center mix level
  58. }
  59. if(hdr->channel_mode & 4) {
  60. skip_bits(&gbc, 2); // skip surround mix level
  61. }
  62. if(hdr->channel_mode == AC3_CHMODE_STEREO) {
  63. skip_bits(&gbc, 2); // skip dolby surround mode
  64. }
  65. hdr->lfe_on = get_bits1(&gbc);
  66. hdr->sr_shift = FFMAX(hdr->bitstream_id, 8) - 8;
  67. hdr->sample_rate = ff_ac3_sample_rate_tab[hdr->sr_code] >> hdr->sr_shift;
  68. hdr->bit_rate = (ff_ac3_bitrate_tab[frame_size_code>>1] * 1000) >> hdr->sr_shift;
  69. hdr->channels = ff_ac3_channels_tab[hdr->channel_mode] + hdr->lfe_on;
  70. hdr->frame_size = ff_ac3_frame_size_tab[frame_size_code][hdr->sr_code] * 2;
  71. } else {
  72. /* Enhanced AC-3 */
  73. hdr->crc1 = 0;
  74. skip_bits(&gbc, 2); // skip stream type
  75. skip_bits(&gbc, 3); // skip substream id
  76. hdr->frame_size = (get_bits(&gbc, 11) + 1) << 1;
  77. if(hdr->frame_size < AC3_HEADER_SIZE)
  78. return AC3_PARSE_ERROR_FRAME_SIZE;
  79. hdr->sr_code = get_bits(&gbc, 2);
  80. if (hdr->sr_code == 3) {
  81. int sr_code2 = get_bits(&gbc, 2);
  82. if(sr_code2 == 3)
  83. return AC3_PARSE_ERROR_SAMPLE_RATE;
  84. hdr->sample_rate = ff_ac3_sample_rate_tab[sr_code2] / 2;
  85. hdr->sr_shift = 1;
  86. num_blocks = 6;
  87. } else {
  88. num_blocks = eac3_blocks[get_bits(&gbc, 2)];
  89. hdr->sample_rate = ff_ac3_sample_rate_tab[hdr->sr_code];
  90. hdr->sr_shift = 0;
  91. }
  92. hdr->channel_mode = get_bits(&gbc, 3);
  93. hdr->lfe_on = get_bits1(&gbc);
  94. hdr->bit_rate = (uint32_t)(8.0 * hdr->frame_size * hdr->sample_rate /
  95. (num_blocks * 256.0));
  96. hdr->channels = ff_ac3_channels_tab[hdr->channel_mode] + hdr->lfe_on;
  97. }
  98. return 0;
  99. }
  100. static int ac3_sync(const uint8_t *buf, int *channels, int *sample_rate,
  101. int *bit_rate, int *samples)
  102. {
  103. int err;
  104. AC3HeaderInfo hdr;
  105. err = ff_ac3_parse_header(buf, &hdr);
  106. if(err < 0)
  107. return 0;
  108. *sample_rate = hdr.sample_rate;
  109. *bit_rate = hdr.bit_rate;
  110. *channels = hdr.channels;
  111. *samples = AC3_FRAME_SIZE;
  112. return hdr.frame_size;
  113. }
  114. static int ac3_parse_init(AVCodecParserContext *s1)
  115. {
  116. AACAC3ParseContext *s = s1->priv_data;
  117. s->inbuf_ptr = s->inbuf;
  118. s->header_size = AC3_HEADER_SIZE;
  119. s->sync = ac3_sync;
  120. return 0;
  121. }
  122. AVCodecParser ac3_parser = {
  123. { CODEC_ID_AC3 },
  124. sizeof(AACAC3ParseContext),
  125. ac3_parse_init,
  126. ff_aac_ac3_parse,
  127. NULL,
  128. };