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.

164 lines
4.9KB

  1. /*
  2. * RAW DTS demuxer
  3. * Copyright (c) 2008 Benjamin Larsson
  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/bytestream.h"
  23. #include "libavcodec/dca.h"
  24. #include "libavcodec/dca_syncwords.h"
  25. #include "libavcodec/get_bits.h"
  26. #include "avformat.h"
  27. #include "rawdec.h"
  28. static int dts_probe(AVProbeData *p)
  29. {
  30. const uint8_t *buf, *bufp;
  31. uint32_t state = -1;
  32. int markers[4*16] = {0};
  33. int exss_markers = 0, exss_nextpos = 0;
  34. int sum, max, pos, i;
  35. int64_t diff = 0;
  36. uint8_t hdr[12 + AV_INPUT_BUFFER_PADDING_SIZE] = { 0 };
  37. const AVCRC *crctab = av_crc_get_table(AV_CRC_16_CCITT);
  38. for (pos = FFMIN(4096, p->buf_size); pos < p->buf_size - 2; pos += 2) {
  39. int marker, sample_blocks, sample_rate, sr_code, framesize;
  40. int lfe, wide_hdr, hdr_size;
  41. GetBitContext gb;
  42. bufp = buf = p->buf + pos;
  43. state = (state << 16) | bytestream_get_be16(&bufp);
  44. if (pos >= 4)
  45. diff += FFABS(((int16_t)AV_RL16(buf)) - (int16_t)AV_RL16(buf-4));
  46. /* extension substream (EXSS) */
  47. if (state == DCA_SYNCWORD_SUBSTREAM) {
  48. if (pos < exss_nextpos)
  49. continue;
  50. init_get_bits(&gb, buf - 2, 96);
  51. skip_bits_long(&gb, 42);
  52. wide_hdr = get_bits1(&gb);
  53. hdr_size = get_bits(&gb, 8 + 4 * wide_hdr) + 1;
  54. framesize = get_bits(&gb, 16 + 4 * wide_hdr) + 1;
  55. if (hdr_size & 3 || framesize & 3)
  56. continue;
  57. if (hdr_size < 16 || framesize < hdr_size)
  58. continue;
  59. if (pos - 2 + hdr_size > p->buf_size)
  60. continue;
  61. if (av_crc(crctab, 0xffff, buf + 3, hdr_size - 5))
  62. continue;
  63. if (pos == exss_nextpos)
  64. exss_markers++;
  65. else
  66. exss_markers = FFMAX(1, exss_markers - 1);
  67. exss_nextpos = pos + framesize;
  68. continue;
  69. }
  70. /* regular bitstream */
  71. if (state == DCA_SYNCWORD_CORE_BE &&
  72. (bytestream_get_be16(&bufp) & 0xFC00) == 0xFC00)
  73. marker = 0;
  74. else if (state == DCA_SYNCWORD_CORE_LE &&
  75. (bytestream_get_be16(&bufp) & 0x00FC) == 0x00FC)
  76. marker = 1;
  77. /* 14 bits big-endian bitstream */
  78. else if (state == DCA_SYNCWORD_CORE_14B_BE &&
  79. (bytestream_get_be16(&bufp) & 0xFFF0) == 0x07F0)
  80. marker = 2;
  81. /* 14 bits little-endian bitstream */
  82. else if (state == DCA_SYNCWORD_CORE_14B_LE &&
  83. (bytestream_get_be16(&bufp) & 0xF0FF) == 0xF007)
  84. marker = 3;
  85. else
  86. continue;
  87. if (avpriv_dca_convert_bitstream(buf-2, 12, hdr, 12) < 0)
  88. continue;
  89. init_get_bits(&gb, hdr, 96);
  90. skip_bits_long(&gb, 39);
  91. sample_blocks = get_bits(&gb, 7) + 1;
  92. if (sample_blocks < 8)
  93. continue;
  94. framesize = get_bits(&gb, 14) + 1;
  95. if (framesize < 95)
  96. continue;
  97. skip_bits(&gb, 6);
  98. sr_code = get_bits(&gb, 4);
  99. sample_rate = avpriv_dca_sample_rates[sr_code];
  100. if (sample_rate == 0)
  101. continue;
  102. get_bits(&gb, 5);
  103. if (get_bits(&gb, 1))
  104. continue;
  105. skip_bits_long(&gb, 9);
  106. lfe = get_bits(&gb, 2);
  107. if (lfe > 2)
  108. continue;
  109. marker += 4* sr_code;
  110. markers[marker] ++;
  111. }
  112. if (exss_markers > 3)
  113. return AVPROBE_SCORE_EXTENSION + 1;
  114. sum = max = 0;
  115. for (i=0; i<FF_ARRAY_ELEMS(markers); i++) {
  116. sum += markers[i];
  117. if (markers[max] < markers[i])
  118. max = i;
  119. }
  120. if (markers[max] > 3 && p->buf_size / markers[max] < 32*1024 &&
  121. markers[max] * 4 > sum * 3 &&
  122. diff / p->buf_size > 200)
  123. return AVPROBE_SCORE_EXTENSION + 1;
  124. return 0;
  125. }
  126. AVInputFormat ff_dts_demuxer = {
  127. .name = "dts",
  128. .long_name = NULL_IF_CONFIG_SMALL("raw DTS"),
  129. .read_probe = dts_probe,
  130. .read_header = ff_raw_audio_read_header,
  131. .read_packet = ff_raw_read_partial_packet,
  132. .flags = AVFMT_GENERIC_INDEX,
  133. .extensions = "dts",
  134. .raw_codec_id = AV_CODEC_ID_DTS,
  135. };