dtsdec.c 4.86 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/*
 * RAW DTS demuxer
 * Copyright (c) 2008 Benjamin Larsson
 *
 * This file is part of FFmpeg.
 *
 * FFmpeg is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * FFmpeg is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with FFmpeg; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

22 23
#include "libavutil/crc.h"

24
#include "libavcodec/bytestream.h"
25
#include "libavcodec/dca.h"
26
#include "libavcodec/dca_syncwords.h"
27
#include "libavcodec/get_bits.h"
28

29
#include "avformat.h"
30
#include "rawdec.h"
31 32 33 34 35

static int dts_probe(AVProbeData *p)
{
    const uint8_t *buf, *bufp;
    uint32_t state = -1;
36
    int markers[4*16] = {0};
37 38
    int exss_markers = 0, exss_nextpos = 0;
    int sum, max, pos, i;
39
    int64_t diff = 0;
40
    uint8_t hdr[12 + AV_INPUT_BUFFER_PADDING_SIZE] = { 0 };
41

42
    for (pos = FFMIN(4096, p->buf_size); pos < p->buf_size - 2; pos += 2) {
43
        int marker, sample_blocks, sample_rate, sr_code, framesize;
44
        int lfe, wide_hdr, hdr_size;
45 46
        GetBitContext gb;

47
        bufp = buf = p->buf + pos;
48 49
        state = (state << 16) | bytestream_get_be16(&bufp);

50
        if (pos >= 4)
51 52
            diff += FFABS(((int16_t)AV_RL16(buf)) - (int16_t)AV_RL16(buf-4));

53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
        /* extension substream (EXSS) */
        if (state == DCA_SYNCWORD_SUBSTREAM) {
            if (pos < exss_nextpos)
                continue;

            init_get_bits(&gb, buf - 2, 96);
            skip_bits_long(&gb, 42);

            wide_hdr  = get_bits1(&gb);
            hdr_size  = get_bits(&gb,  8 + 4 * wide_hdr) + 1;
            framesize = get_bits(&gb, 16 + 4 * wide_hdr) + 1;
            if (hdr_size & 3 || framesize & 3)
                continue;
            if (hdr_size < 16 || framesize < hdr_size)
                continue;
            if (pos - 2 + hdr_size > p->buf_size)
                continue;
70
            if (av_crc(av_crc_get_table(AV_CRC_16_CCITT), 0xffff, buf + 3, hdr_size - 5))
71 72 73 74 75 76 77 78 79 80
                continue;

            if (pos == exss_nextpos)
                exss_markers++;
            else
                exss_markers = FFMAX(1, exss_markers - 1);
            exss_nextpos = pos + framesize;
            continue;
        }

81
        /* regular bitstream */
82 83
        if (state == DCA_SYNCWORD_CORE_BE &&
            (bytestream_get_be16(&bufp) & 0xFC00) == 0xFC00)
84
            marker = 0;
85 86
        else if (state == DCA_SYNCWORD_CORE_LE &&
                 (bytestream_get_be16(&bufp) & 0x00FC) == 0x00FC)
87
            marker = 1;
88 89

        /* 14 bits big-endian bitstream */
90
        else if (state == DCA_SYNCWORD_CORE_14B_BE &&
91 92
                 (bytestream_get_be16(&bufp) & 0xFFF0) == 0x07F0)
            marker = 2;
93 94

        /* 14 bits little-endian bitstream */
95
        else if (state == DCA_SYNCWORD_CORE_14B_LE &&
96 97 98 99
                 (bytestream_get_be16(&bufp) & 0xF0FF) == 0xF007)
            marker = 3;
        else
            continue;
100

101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
        if (avpriv_dca_convert_bitstream(buf-2, 12, hdr, 12) < 0)
            continue;

        init_get_bits(&gb, hdr, 96);
        skip_bits_long(&gb, 39);

        sample_blocks = get_bits(&gb, 7) + 1;
        if (sample_blocks < 8)
            continue;

        framesize = get_bits(&gb, 14) + 1;
        if (framesize < 95)
            continue;

        skip_bits(&gb, 6);
        sr_code = get_bits(&gb, 4);
        sample_rate = avpriv_dca_sample_rates[sr_code];
        if (sample_rate == 0)
            continue;

121 122 123 124 125 126 127 128 129 130 131
        get_bits(&gb, 5);
        if (get_bits(&gb, 1))
            continue;

        skip_bits_long(&gb, 9);
        lfe = get_bits(&gb, 2);
        if (lfe > 2)
            continue;

        marker += 4* sr_code;

132
        markers[marker] ++;
133
    }
134

135 136 137
    if (exss_markers > 3)
        return AVPROBE_SCORE_EXTENSION + 1;

138 139 140
    sum = max = 0;
    for (i=0; i<FF_ARRAY_ELEMS(markers); i++) {
        sum += markers[i];
141 142
        if (markers[max] < markers[i])
            max = i;
143 144
    }

145
    if (markers[max] > 3 && p->buf_size / markers[max] < 32*1024 &&
146 147
        markers[max] * 4 > sum * 3 &&
        diff / p->buf_size > 200)
148
        return AVPROBE_SCORE_EXTENSION + 1;
149 150 151 152

    return 0;
}

153
AVInputFormat ff_dts_demuxer = {
154 155 156 157 158
    .name           = "dts",
    .long_name      = NULL_IF_CONFIG_SMALL("raw DTS"),
    .read_probe     = dts_probe,
    .read_header    = ff_raw_audio_read_header,
    .read_packet    = ff_raw_read_partial_packet,
159 160
    .flags          = AVFMT_GENERIC_INDEX,
    .extensions     = "dts",
161
    .raw_codec_id   = AV_CODEC_ID_DTS,
162
};