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.

142 lines
4.6KB

  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(const 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, ret, i;
  35. int64_t diff = 0;
  36. uint8_t hdr[DCA_CORE_FRAME_HEADER_SIZE + AV_INPUT_BUFFER_PADDING_SIZE] = { 0 };
  37. for (pos = FFMIN(4096, p->buf_size); pos < p->buf_size - 2; pos += 2) {
  38. int marker, wide_hdr, hdr_size, framesize;
  39. DCACoreFrameHeader h;
  40. GetBitContext gb;
  41. bufp = buf = p->buf + pos;
  42. state = (state << 16) | bytestream_get_be16(&bufp);
  43. if (pos >= 4)
  44. diff += FFABS(((int16_t)AV_RL16(buf)) - (int16_t)AV_RL16(buf-4));
  45. /* extension substream (EXSS) */
  46. if (state == DCA_SYNCWORD_SUBSTREAM) {
  47. if (pos < exss_nextpos)
  48. continue;
  49. init_get_bits(&gb, buf - 2, 96);
  50. skip_bits_long(&gb, 42);
  51. wide_hdr = get_bits1(&gb);
  52. hdr_size = get_bits(&gb, 8 + 4 * wide_hdr) + 1;
  53. framesize = get_bits(&gb, 16 + 4 * wide_hdr) + 1;
  54. if (hdr_size & 3 || framesize & 3)
  55. continue;
  56. if (hdr_size < 16 || framesize < hdr_size)
  57. continue;
  58. if (pos - 2 + hdr_size > p->buf_size)
  59. continue;
  60. if (av_crc(av_crc_get_table(AV_CRC_16_CCITT), 0xffff, buf + 3, hdr_size - 5))
  61. continue;
  62. if (pos == exss_nextpos)
  63. exss_markers++;
  64. else
  65. exss_markers = FFMAX(1, exss_markers - 1);
  66. exss_nextpos = pos + framesize;
  67. continue;
  68. }
  69. /* regular bitstream */
  70. if (state == DCA_SYNCWORD_CORE_BE &&
  71. (bytestream_get_be16(&bufp) & 0xFC00) == 0xFC00)
  72. marker = 0;
  73. else if (state == DCA_SYNCWORD_CORE_LE &&
  74. (bytestream_get_be16(&bufp) & 0x00FC) == 0x00FC)
  75. marker = 1;
  76. /* 14 bits big-endian bitstream */
  77. else if (state == DCA_SYNCWORD_CORE_14B_BE &&
  78. (bytestream_get_be16(&bufp) & 0xFFF0) == 0x07F0)
  79. marker = 2;
  80. /* 14 bits little-endian bitstream */
  81. else if (state == DCA_SYNCWORD_CORE_14B_LE &&
  82. (bytestream_get_be16(&bufp) & 0xF0FF) == 0xF007)
  83. marker = 3;
  84. else
  85. continue;
  86. if ((ret = avpriv_dca_convert_bitstream(buf - 2, DCA_CORE_FRAME_HEADER_SIZE,
  87. hdr, DCA_CORE_FRAME_HEADER_SIZE)) < 0)
  88. continue;
  89. if (avpriv_dca_parse_core_frame_header(&h, hdr, ret) < 0)
  90. continue;
  91. marker += 4 * h.sr_code;
  92. markers[marker] ++;
  93. }
  94. if (exss_markers > 3)
  95. return AVPROBE_SCORE_EXTENSION + 1;
  96. sum = max = 0;
  97. for (i=0; i<FF_ARRAY_ELEMS(markers); i++) {
  98. sum += markers[i];
  99. if (markers[max] < markers[i])
  100. max = i;
  101. }
  102. if (markers[max] > 3 && p->buf_size / markers[max] < 32*1024 &&
  103. markers[max] * 4 > sum * 3 &&
  104. diff / p->buf_size > 200)
  105. return AVPROBE_SCORE_EXTENSION + 1;
  106. return 0;
  107. }
  108. FF_RAW_DEMUXER_CLASS(dts)
  109. AVInputFormat ff_dts_demuxer = {
  110. .name = "dts",
  111. .long_name = NULL_IF_CONFIG_SMALL("raw DTS"),
  112. .read_probe = dts_probe,
  113. .read_header = ff_raw_audio_read_header,
  114. .read_packet = ff_raw_read_partial_packet,
  115. .flags = AVFMT_GENERIC_INDEX,
  116. .extensions = "dts",
  117. .raw_codec_id = AV_CODEC_ID_DTS,
  118. .priv_data_size = sizeof(FFRawDemuxerContext),
  119. .priv_class = &dts_demuxer_class,};