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.

125 lines
3.9KB

  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 Libav.
  6. *
  7. * Libav 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. * Libav 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 Libav; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include "libavutil/crc.h"
  22. #include "libavcodec/ac3_parser.h"
  23. #include "avformat.h"
  24. #include "rawdec.h"
  25. static int ac3_eac3_probe(AVProbeData *p, enum AVCodecID expected_codec_id)
  26. {
  27. int max_frames, first_frames = 0, frames;
  28. uint8_t *buf, *buf2, *end;
  29. enum AVCodecID codec_id = AV_CODEC_ID_AC3;
  30. max_frames = 0;
  31. buf = p->buf;
  32. end = buf + p->buf_size;
  33. for(; buf < end; buf++) {
  34. buf2 = buf;
  35. for(frames = 0; buf2 < end; frames++) {
  36. uint8_t bitstream_id;
  37. uint16_t frame_size;
  38. int ret;
  39. ret = av_ac3_parse_header(buf2, end - buf2, &bitstream_id,
  40. &frame_size);
  41. if (ret < 0)
  42. break;
  43. if (buf2 + frame_size > end ||
  44. av_crc(av_crc_get_table(AV_CRC_16_ANSI), 0, buf2 + 2, frame_size - 2))
  45. break;
  46. if (bitstream_id > 10)
  47. codec_id = AV_CODEC_ID_EAC3;
  48. buf2 += frame_size;
  49. }
  50. max_frames = FFMAX(max_frames, frames);
  51. if(buf == p->buf)
  52. first_frames = frames;
  53. }
  54. if(codec_id != expected_codec_id) return 0;
  55. // keep this in sync with mp3 probe, both need to avoid
  56. // issues with MPEG-files!
  57. if (first_frames >= 4) return AVPROBE_SCORE_EXTENSION + 1;
  58. if (max_frames) {
  59. int pes = 0, i;
  60. unsigned int code = -1;
  61. #define VIDEO_ID 0x000001e0
  62. #define AUDIO_ID 0x000001c0
  63. /* do a search for mpegps headers to be able to properly bias
  64. * towards mpegps if we detect this stream as both. */
  65. for (i = 0; i<p->buf_size; i++) {
  66. code = (code << 8) + p->buf[i];
  67. if ((code & 0xffffff00) == 0x100) {
  68. if ((code & 0x1f0) == VIDEO_ID) pes++;
  69. else if((code & 0x1e0) == AUDIO_ID) pes++;
  70. }
  71. }
  72. if (pes)
  73. max_frames = (max_frames + pes - 1) / pes;
  74. }
  75. if (max_frames > 500) return AVPROBE_SCORE_EXTENSION;
  76. else if (max_frames >= 4) return AVPROBE_SCORE_EXTENSION / 2;
  77. else if (max_frames >= 1) return 1;
  78. else return 0;
  79. }
  80. #if CONFIG_AC3_DEMUXER
  81. static int ac3_probe(AVProbeData *p)
  82. {
  83. return ac3_eac3_probe(p, AV_CODEC_ID_AC3);
  84. }
  85. AVInputFormat ff_ac3_demuxer = {
  86. .name = "ac3",
  87. .long_name = NULL_IF_CONFIG_SMALL("raw AC-3"),
  88. .read_probe = ac3_probe,
  89. .read_header = ff_raw_audio_read_header,
  90. .read_packet = ff_raw_read_partial_packet,
  91. .flags= AVFMT_GENERIC_INDEX,
  92. .extensions = "ac3",
  93. .raw_codec_id = AV_CODEC_ID_AC3,
  94. };
  95. #endif
  96. #if CONFIG_EAC3_DEMUXER
  97. static int eac3_probe(AVProbeData *p)
  98. {
  99. return ac3_eac3_probe(p, AV_CODEC_ID_EAC3);
  100. }
  101. AVInputFormat ff_eac3_demuxer = {
  102. .name = "eac3",
  103. .long_name = NULL_IF_CONFIG_SMALL("raw E-AC-3"),
  104. .read_probe = eac3_probe,
  105. .read_header = ff_raw_audio_read_header,
  106. .read_packet = ff_raw_read_partial_packet,
  107. .flags = AVFMT_GENERIC_INDEX,
  108. .extensions = "eac3",
  109. .raw_codec_id = AV_CODEC_ID_EAC3,
  110. };
  111. #endif