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.

163 lines
4.4KB

  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 -1;
  38. /* read ahead to bsid to make sure this is AC-3, not E-AC-3 */
  39. hdr->bsid = show_bits_long(&gbc, 29) & 0x1F;
  40. if(hdr->bsid > 10)
  41. return -2;
  42. hdr->crc1 = get_bits(&gbc, 16);
  43. hdr->fscod = get_bits(&gbc, 2);
  44. if(hdr->fscod == 3)
  45. return -3;
  46. hdr->frmsizecod = get_bits(&gbc, 6);
  47. if(hdr->frmsizecod > 37)
  48. return -4;
  49. skip_bits(&gbc, 5); // skip bsid, already got it
  50. hdr->bsmod = get_bits(&gbc, 3);
  51. hdr->acmod = get_bits(&gbc, 3);
  52. if((hdr->acmod & 1) && hdr->acmod != AC3_ACMOD_MONO) {
  53. hdr->cmixlev = get_bits(&gbc, 2);
  54. }
  55. if(hdr->acmod & 4) {
  56. hdr->surmixlev = get_bits(&gbc, 2);
  57. }
  58. if(hdr->acmod == AC3_ACMOD_STEREO) {
  59. hdr->dsurmod = get_bits(&gbc, 2);
  60. }
  61. hdr->lfeon = get_bits1(&gbc);
  62. hdr->halfratecod = FFMAX(hdr->bsid, 8) - 8;
  63. hdr->sample_rate = ff_ac3_freqs[hdr->fscod] >> hdr->halfratecod;
  64. hdr->bit_rate = (ff_ac3_bitratetab[hdr->frmsizecod>>1] * 1000) >> hdr->halfratecod;
  65. hdr->channels = ff_ac3_channels[hdr->acmod] + hdr->lfeon;
  66. hdr->frame_size = ff_ac3_frame_sizes[hdr->frmsizecod][hdr->fscod] * 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 fscod, acmod, bsid, lfeon;
  74. unsigned int strmtyp, substreamid, frmsiz, fscod2, numblkscod;
  75. GetBitContext bits;
  76. AC3HeaderInfo hdr;
  77. err = ff_ac3_parse_header(buf, &hdr);
  78. if(err < 0 && err != -2)
  79. return 0;
  80. bsid = hdr.bsid;
  81. if(bsid <= 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 (bsid > 10 && bsid <= 16) { /* Enhanced AC-3 */
  88. init_get_bits(&bits, &buf[2], (AC3_HEADER_SIZE-2) * 8);
  89. strmtyp = get_bits(&bits, 2);
  90. substreamid = get_bits(&bits, 3);
  91. if (strmtyp != 0 || substreamid != 0)
  92. return 0; /* Currently don't support additional streams */
  93. frmsiz = get_bits(&bits, 11) + 1;
  94. fscod = get_bits(&bits, 2);
  95. if (fscod == 3) {
  96. fscod2 = get_bits(&bits, 2);
  97. numblkscod = 3;
  98. if(fscod2 == 3)
  99. return 0;
  100. *sample_rate = ff_ac3_freqs[fscod2] / 2;
  101. } else {
  102. numblkscod = get_bits(&bits, 2);
  103. *sample_rate = ff_ac3_freqs[fscod];
  104. }
  105. acmod = get_bits(&bits, 3);
  106. lfeon = get_bits1(&bits);
  107. *samples = eac3_blocks[numblkscod] * 256;
  108. *bit_rate = frmsiz * (*sample_rate) * 16 / (*samples);
  109. *channels = ff_ac3_channels[acmod] + lfeon;
  110. return frmsiz * 2;
  111. }
  112. /* Unsupported bitstream version */
  113. return 0;
  114. }
  115. static int ac3_parse_init(AVCodecParserContext *s1)
  116. {
  117. AACAC3ParseContext *s = s1->priv_data;
  118. s->inbuf_ptr = s->inbuf;
  119. s->header_size = AC3_HEADER_SIZE;
  120. s->sync = ac3_sync;
  121. return 0;
  122. }
  123. AVCodecParser ac3_parser = {
  124. { CODEC_ID_AC3 },
  125. sizeof(AACAC3ParseContext),
  126. ac3_parse_init,
  127. ff_aac_ac3_parse,
  128. NULL,
  129. };