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
4.1KB

  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 AVCodecID expected_codec_id)
  26. {
  27. int max_frames, first_frames = 0, frames;
  28. const uint8_t *buf, *buf2, *end;
  29. AC3HeaderInfo *phdr = NULL;
  30. GetBitContext gbc;
  31. enum AVCodecID codec_id = AV_CODEC_ID_AC3;
  32. max_frames = 0;
  33. buf = p->buf;
  34. end = buf + p->buf_size;
  35. for(; buf < end; buf++) {
  36. if(buf > p->buf && !(buf[0] == 0x0B && buf[1] == 0x77)
  37. && !(buf[0] == 0x77 && buf[1] == 0x0B) )
  38. continue;
  39. buf2 = buf;
  40. for(frames = 0; buf2 < end; frames++) {
  41. uint8_t buf3[4096];
  42. int i;
  43. if(!memcmp(buf2, "\x1\x10\0\0\0\0\0\0", 8))
  44. buf2+=16;
  45. if (buf[0] == 0x77 && buf[1] == 0x0B) {
  46. for(i=0; i<8; i+=2) {
  47. buf3[i ] = buf[i+1];
  48. buf3[i+1] = buf[i ];
  49. }
  50. init_get_bits(&gbc, buf3, 54);
  51. }else
  52. init_get_bits(&gbc, buf2, 54);
  53. if(avpriv_ac3_parse_header2(&gbc, &phdr) < 0)
  54. break;
  55. if(buf2 + phdr->frame_size > end)
  56. break;
  57. if (buf[0] == 0x77 && buf[1] == 0x0B) {
  58. av_assert0(phdr->frame_size <= sizeof(buf3));
  59. for(i=8; i<phdr->frame_size; i+=2) {
  60. buf3[i ] = buf[i+1];
  61. buf3[i+1] = buf[i ];
  62. }
  63. }
  64. if(av_crc(av_crc_get_table(AV_CRC_16_ANSI), 0, gbc.buffer + 2, phdr->frame_size - 2))
  65. break;
  66. if (phdr->bitstream_id > 10)
  67. codec_id = AV_CODEC_ID_EAC3;
  68. buf2 += phdr->frame_size;
  69. }
  70. max_frames = FFMAX(max_frames, frames);
  71. if(buf == p->buf)
  72. first_frames = frames;
  73. }
  74. av_freep(&phdr);
  75. if(codec_id != expected_codec_id) return 0;
  76. // keep this in sync with mp3 probe, both need to avoid
  77. // issues with MPEG-files!
  78. if (first_frames>=4) return AVPROBE_SCORE_EXTENSION + 1;
  79. else if(max_frames>200)return AVPROBE_SCORE_EXTENSION;
  80. else if(max_frames>=4) return AVPROBE_SCORE_EXTENSION/2;
  81. else if(max_frames>=1) return 1;
  82. else return 0;
  83. }
  84. #if CONFIG_AC3_DEMUXER
  85. static int ac3_probe(AVProbeData *p)
  86. {
  87. return ac3_eac3_probe(p, AV_CODEC_ID_AC3);
  88. }
  89. AVInputFormat ff_ac3_demuxer = {
  90. .name = "ac3",
  91. .long_name = NULL_IF_CONFIG_SMALL("raw AC-3"),
  92. .read_probe = ac3_probe,
  93. .read_header = ff_raw_audio_read_header,
  94. .read_packet = ff_raw_read_partial_packet,
  95. .flags= AVFMT_GENERIC_INDEX,
  96. .extensions = "ac3",
  97. .raw_codec_id = AV_CODEC_ID_AC3,
  98. };
  99. #endif
  100. #if CONFIG_EAC3_DEMUXER
  101. static int eac3_probe(AVProbeData *p)
  102. {
  103. return ac3_eac3_probe(p, AV_CODEC_ID_EAC3);
  104. }
  105. AVInputFormat ff_eac3_demuxer = {
  106. .name = "eac3",
  107. .long_name = NULL_IF_CONFIG_SMALL("raw E-AC-3"),
  108. .read_probe = eac3_probe,
  109. .read_header = ff_raw_audio_read_header,
  110. .read_packet = ff_raw_read_partial_packet,
  111. .flags = AVFMT_GENERIC_INDEX,
  112. .extensions = "eac3",
  113. .raw_codec_id = AV_CODEC_ID_EAC3,
  114. };
  115. #endif