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.

166 lines
4.8KB

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