/libavformat/ac3dec.c

http://github.com/FFmpeg/FFmpeg · C · 139 lines · 107 code · 10 blank · 22 comment · 41 complexity · 8a905af60529dfed6d791015fbb88636 MD5 · raw file

  1. /*
  2. * RAW AC-3 and E-AC-3 demuxer
  3. * Copyright (c) 2007 Justin Ruggles <justin.ruggles@gmail.com>
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg 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. * FFmpeg 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 FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include "libavutil/avassert.h"
  22. #include "libavutil/crc.h"
  23. #include "libavcodec/ac3_parser.h"
  24. #include "avformat.h"
  25. #include "rawdec.h"
  26. static int ac3_eac3_probe(const AVProbeData *p, enum AVCodecID expected_codec_id)
  27. {
  28. int max_frames, first_frames = 0, frames;
  29. const uint8_t *buf, *buf2, *end;
  30. enum AVCodecID codec_id = AV_CODEC_ID_AC3;
  31. max_frames = 0;
  32. buf = p->buf;
  33. end = buf + p->buf_size;
  34. for(; buf < end; buf++) {
  35. if(buf > p->buf && !(buf[0] == 0x0B && buf[1] == 0x77)
  36. && !(buf[0] == 0x77 && buf[1] == 0x0B) )
  37. continue;
  38. buf2 = buf;
  39. for(frames = 0; buf2 < end; frames++) {
  40. uint8_t buf3[4096];
  41. uint8_t bitstream_id;
  42. uint16_t frame_size;
  43. int i, ret;
  44. if(!memcmp(buf2, "\x1\x10", 2)) {
  45. if (buf2 + 16 > end)
  46. break;
  47. buf2+=16;
  48. }
  49. if (buf[0] == 0x77 && buf[1] == 0x0B) {
  50. for(i=0; i<8; i+=2) {
  51. buf3[i ] = buf2[i+1];
  52. buf3[i+1] = buf2[i ];
  53. }
  54. ret = av_ac3_parse_header(buf3, 8, &bitstream_id,
  55. &frame_size);
  56. }else
  57. ret = av_ac3_parse_header(buf2, end - buf2, &bitstream_id,
  58. &frame_size);
  59. if (ret < 0)
  60. break;
  61. if(buf2 + frame_size > end)
  62. break;
  63. if (buf[0] == 0x77 && buf[1] == 0x0B) {
  64. av_assert0(frame_size <= sizeof(buf3));
  65. for(i = 8; i < frame_size; i += 2) {
  66. buf3[i ] = buf2[i+1];
  67. buf3[i+1] = buf2[i ];
  68. }
  69. if (av_crc(av_crc_get_table(AV_CRC_16_ANSI), 0, buf3 + 2, frame_size - 2))
  70. break;
  71. } else {
  72. if (av_crc(av_crc_get_table(AV_CRC_16_ANSI), 0, buf2 + 2, frame_size - 2))
  73. break;
  74. }
  75. if (bitstream_id > 10)
  76. codec_id = AV_CODEC_ID_EAC3;
  77. buf2 += frame_size;
  78. }
  79. max_frames = FFMAX(max_frames, frames);
  80. if(buf == p->buf)
  81. first_frames = frames;
  82. }
  83. if(codec_id != expected_codec_id) return 0;
  84. // keep this in sync with mp3 probe, both need to avoid
  85. // issues with MPEG-files!
  86. if (first_frames>=7) return AVPROBE_SCORE_EXTENSION + 1;
  87. else if(max_frames>200)return AVPROBE_SCORE_EXTENSION;
  88. else if(max_frames>=4) return AVPROBE_SCORE_EXTENSION/2;
  89. else if(max_frames>=1) return 1;
  90. else return 0;
  91. }
  92. #if CONFIG_AC3_DEMUXER
  93. static int ac3_probe(const AVProbeData *p)
  94. {
  95. return ac3_eac3_probe(p, AV_CODEC_ID_AC3);
  96. }
  97. FF_RAW_DEMUXER_CLASS(ac3)
  98. AVInputFormat ff_ac3_demuxer = {
  99. .name = "ac3",
  100. .long_name = NULL_IF_CONFIG_SMALL("raw AC-3"),
  101. .read_probe = ac3_probe,
  102. .read_header = ff_raw_audio_read_header,
  103. .read_packet = ff_raw_read_partial_packet,
  104. .flags= AVFMT_GENERIC_INDEX,
  105. .extensions = "ac3",
  106. .raw_codec_id = AV_CODEC_ID_AC3,
  107. .priv_data_size = sizeof(FFRawDemuxerContext),
  108. .priv_class = &ac3_demuxer_class,
  109. };
  110. #endif
  111. #if CONFIG_EAC3_DEMUXER
  112. static int eac3_probe(const AVProbeData *p)
  113. {
  114. return ac3_eac3_probe(p, AV_CODEC_ID_EAC3);
  115. }
  116. FF_RAW_DEMUXER_CLASS(eac3)
  117. AVInputFormat ff_eac3_demuxer = {
  118. .name = "eac3",
  119. .long_name = NULL_IF_CONFIG_SMALL("raw E-AC-3"),
  120. .read_probe = eac3_probe,
  121. .read_header = ff_raw_audio_read_header,
  122. .read_packet = ff_raw_read_partial_packet,
  123. .flags = AVFMT_GENERIC_INDEX,
  124. .extensions = "eac3",
  125. .raw_codec_id = AV_CODEC_ID_EAC3,
  126. .priv_data_size = sizeof(FFRawDemuxerContext),
  127. .priv_class = &eac3_demuxer_class,
  128. };
  129. #endif