ac3dec.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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/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 CodecID expected_codec_id)
  26. {
  27. int max_frames, first_frames = 0, frames;
  28. uint8_t *buf, *buf2, *end;
  29. AC3HeaderInfo hdr;
  30. GetBitContext gbc;
  31. enum CodecID codec_id = CODEC_ID_AC3;
  32. max_frames = 0;
  33. buf = p->buf;
  34. end = buf + p->buf_size;
  35. for(; buf < end; buf++) {
  36. buf2 = buf;
  37. for(frames = 0; buf2 < end; frames++) {
  38. if(!memcmp(buf2, "\x1\x10\0\0\0\0\0\0", 8))
  39. buf2+=16;
  40. init_get_bits(&gbc, buf2, 54);
  41. if(ff_ac3_parse_header(&gbc, &hdr) < 0)
  42. break;
  43. if(buf2 + hdr.frame_size > end ||
  44. av_crc(av_crc_get_table(AV_CRC_16_ANSI), 0, buf2 + 2, hdr.frame_size - 2))
  45. break;
  46. if (hdr.bitstream_id > 10)
  47. codec_id = CODEC_ID_EAC3;
  48. buf2 += hdr.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_MAX/2+1;
  58. else if(max_frames>500)return AVPROBE_SCORE_MAX/2;
  59. else if(max_frames>=4) return AVPROBE_SCORE_MAX/4;
  60. else if(max_frames>=1) return 1;
  61. else return 0;
  62. }
  63. #if CONFIG_AC3_DEMUXER
  64. static int ac3_probe(AVProbeData *p)
  65. {
  66. return ac3_eac3_probe(p, CODEC_ID_AC3);
  67. }
  68. AVInputFormat ff_ac3_demuxer = {
  69. .name = "ac3",
  70. .long_name = NULL_IF_CONFIG_SMALL("raw AC-3"),
  71. .read_probe = ac3_probe,
  72. .read_header = ff_raw_audio_read_header,
  73. .read_packet = ff_raw_read_partial_packet,
  74. .flags= AVFMT_GENERIC_INDEX,
  75. .extensions = "ac3",
  76. .value = CODEC_ID_AC3,
  77. };
  78. #endif
  79. #if CONFIG_EAC3_DEMUXER
  80. static int eac3_probe(AVProbeData *p)
  81. {
  82. return ac3_eac3_probe(p, CODEC_ID_EAC3);
  83. }
  84. AVInputFormat ff_eac3_demuxer = {
  85. .name = "eac3",
  86. .long_name = NULL_IF_CONFIG_SMALL("raw E-AC-3"),
  87. .read_probe = eac3_probe,
  88. .read_header = ff_raw_audio_read_header,
  89. .read_packet = ff_raw_read_partial_packet,
  90. .flags= AVFMT_GENERIC_INDEX,
  91. .extensions = "eac3",
  92. .value = CODEC_ID_EAC3,
  93. };
  94. #endif