dtsdec.c 7.61 KB
Newer Older
1 2 3 4
/*
 * dtsdec.c : free DTS Coherent Acoustics stream decoder.
 * Copyright (C) 2004 Benjamin Zores <ben@geexbox.org>
 *
5
 * This file is part of FFmpeg.
6
 *
7
 * FFmpeg is free software; you can redistribute it and/or modify
michael's avatar
michael committed
8
 * it under the terms of the GNU General Public License as published by
9
 * the Free Software Foundation; either version 2 of the License, or
michael's avatar
michael committed
10
 * (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
michael's avatar
michael committed
14 15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
16
 *
michael's avatar
michael committed
17
 * You should have received a copy of the GNU General Public License
18
 * along with FFmpeg; if not, write to the Free Software
diego's avatar
diego committed
19
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 21 22 23 24 25 26 27 28 29 30
 */

#ifdef HAVE_AV_CONFIG_H
#undef HAVE_AV_CONFIG_H
#endif

#include "avcodec.h"
#include <dts.h>

#include <stdlib.h>
#include <string.h>
31 32

#ifdef HAVE_MALLOC_H
33
#include <malloc.h>
34
#endif
35

36
#define BUFFER_SIZE 18726
37 38 39 40 41 42 43 44 45 46
#define HEADER_SIZE 14

#ifdef LIBDTS_FIXED
#define CONVERT_LEVEL (1 << 26)
#define CONVERT_BIAS 0
#else
#define CONVERT_LEVEL 1
#define CONVERT_BIAS 384
#endif

47 48
static inline int16_t
convert(int32_t i)
49 50 51 52 53 54 55 56 57
{
#ifdef LIBDTS_FIXED
    i >>= 15;
#else
    i -= 0x43c00000;
#endif
    return (i > 32767) ? 32767 : ((i < -32768) ? -32768 : i);
}

mru's avatar
mru committed
58
static void
59
convert2s16_2(sample_t * _f, int16_t * s16)
60
{
61 62
    int i;
    int32_t *f = (int32_t *) _f;
63

64 65 66
    for(i = 0; i < 256; i++) {
        s16[2 * i] = convert(f[i]);
        s16[2 * i + 1] = convert(f[i + 256]);
67 68 69
    }
}

mru's avatar
mru committed
70
static void
71
convert2s16_4(sample_t * _f, int16_t * s16)
72
{
73 74
    int i;
    int32_t *f = (int32_t *) _f;
75

76 77 78 79 80
    for(i = 0; i < 256; i++) {
        s16[4 * i] = convert(f[i]);
        s16[4 * i + 1] = convert(f[i + 256]);
        s16[4 * i + 2] = convert(f[i + 512]);
        s16[4 * i + 3] = convert(f[i + 768]);
81 82 83
    }
}

mru's avatar
mru committed
84
static void
85
convert2s16_5(sample_t * _f, int16_t * s16)
86
{
87 88
    int i;
    int32_t *f = (int32_t *) _f;
89

90 91 92 93 94 95
    for(i = 0; i < 256; i++) {
        s16[5 * i] = convert(f[i]);
        s16[5 * i + 1] = convert(f[i + 256]);
        s16[5 * i + 2] = convert(f[i + 512]);
        s16[5 * i + 3] = convert(f[i + 768]);
        s16[5 * i + 4] = convert(f[i + 1024]);
96 97 98 99
    }
}

static void
100
convert2s16_multi(sample_t * _f, int16_t * s16, int flags)
101
{
102 103
    int i;
    int32_t *f = (int32_t *) _f;
104

105
    switch (flags) {
106
    case DTS_MONO:
107 108 109 110
        for(i = 0; i < 256; i++) {
            s16[5 * i] = s16[5 * i + 1] = s16[5 * i + 2] = s16[5 * i + 3] =
                0;
            s16[5 * i + 4] = convert(f[i]);
111
        }
112
        break;
113 114 115
    case DTS_CHANNEL:
    case DTS_STEREO:
    case DTS_DOLBY:
116 117
        convert2s16_2(_f, s16);
        break;
118
    case DTS_3F:
119 120 121 122 123
        for(i = 0; i < 256; i++) {
            s16[5 * i] = convert(f[i]);
            s16[5 * i + 1] = convert(f[i + 512]);
            s16[5 * i + 2] = s16[5 * i + 3] = 0;
            s16[5 * i + 4] = convert(f[i + 256]);
124
        }
125
        break;
126
    case DTS_2F2R:
127 128
        convert2s16_4(_f, s16);
        break;
129
    case DTS_3F2R:
130 131
        convert2s16_5(_f, s16);
        break;
132
    case DTS_MONO | DTS_LFE:
133 134 135 136 137
        for(i = 0; i < 256; i++) {
            s16[6 * i] = s16[6 * i + 1] = s16[6 * i + 2] = s16[6 * i + 3] =
                0;
            s16[6 * i + 4] = convert(f[i + 256]);
            s16[6 * i + 5] = convert(f[i]);
138
        }
139
        break;
140 141 142
    case DTS_CHANNEL | DTS_LFE:
    case DTS_STEREO | DTS_LFE:
    case DTS_DOLBY | DTS_LFE:
143 144 145 146 147
        for(i = 0; i < 256; i++) {
            s16[6 * i] = convert(f[i + 256]);
            s16[6 * i + 1] = convert(f[i + 512]);
            s16[6 * i + 2] = s16[6 * i + 3] = s16[6 * i + 4] = 0;
            s16[6 * i + 5] = convert(f[i]);
148
        }
149
        break;
150
    case DTS_3F | DTS_LFE:
151 152 153 154 155 156
        for(i = 0; i < 256; i++) {
            s16[6 * i] = convert(f[i + 256]);
            s16[6 * i + 1] = convert(f[i + 768]);
            s16[6 * i + 2] = s16[6 * i + 3] = 0;
            s16[6 * i + 4] = convert(f[i + 512]);
            s16[6 * i + 5] = convert(f[i]);
157
        }
158
        break;
159
    case DTS_2F2R | DTS_LFE:
160 161 162 163 164 165 166
        for(i = 0; i < 256; i++) {
            s16[6 * i] = convert(f[i + 256]);
            s16[6 * i + 1] = convert(f[i + 512]);
            s16[6 * i + 2] = convert(f[i + 768]);
            s16[6 * i + 3] = convert(f[i + 1024]);
            s16[6 * i + 4] = 0;
            s16[6 * i + 5] = convert(f[i]);
167
        }
168
        break;
169
    case DTS_3F2R | DTS_LFE:
170 171 172 173 174 175 176
        for(i = 0; i < 256; i++) {
            s16[6 * i] = convert(f[i + 256]);
            s16[6 * i + 1] = convert(f[i + 768]);
            s16[6 * i + 2] = convert(f[i + 1024]);
            s16[6 * i + 3] = convert(f[i + 1280]);
            s16[6 * i + 4] = convert(f[i + 512]);
            s16[6 * i + 5] = convert(f[i]);
177
        }
178
        break;
179 180 181 182
    }
}

static int
183
channels_multi(int flags)
184
{
185 186 187 188 189 190 191 192
    if(flags & DTS_LFE)
        return 6;
    else if(flags & 1)          /* center channel */
        return 5;
    else if((flags & DTS_CHANNEL_MASK) == DTS_2F2R)
        return 4;
    else
        return 2;
193 194 195
}

static int
196 197
dts_decode_frame(AVCodecContext * avctx, void *data, int *data_size,
                 uint8_t * buff, int buff_size)
198
{
199 200 201 202 203 204 205 206 207 208 209 210
    uint8_t *start = buff;
    uint8_t *end = buff + buff_size;
    static uint8_t buf[BUFFER_SIZE];
    static uint8_t *bufptr = buf;
    static uint8_t *bufpos = buf + HEADER_SIZE;
    int16_t *out_samples = data;
    static int sample_rate;
    static int frame_length;
    static int flags;
    int bit_rate;
    int len;
    dts_state_t *state = avctx->priv_data;
211 212 213
    level_t level;
    sample_t bias;
    int i;
214

215
    *data_size = 0;
michael's avatar
michael committed
216

217
    while(1) {
218 219
        int length;

220 221 222 223 224 225 226 227 228 229 230 231
        len = end - start;
        if(!len)
            break;
        if(len > bufpos - bufptr)
            len = bufpos - bufptr;
        memcpy(bufptr, start, len);
        bufptr += len;
        start += len;
        if(bufptr != bufpos)
            return start - buff;
        if(bufpos != buf + HEADER_SIZE)
            break;
232

233 234 235 236 237 238 239
        length = dts_syncinfo(state, buf, &flags, &sample_rate, &bit_rate,
                              &frame_length);
        if(!length) {
            av_log(NULL, AV_LOG_INFO, "skip\n");
            for(bufptr = buf; bufptr < buf + HEADER_SIZE - 1; bufptr++)
                bufptr[0] = bufptr[1];
            continue;
240
        }
241 242 243 244 245 246 247 248 249 250 251
        bufpos = buf + length;
    }

    flags = 2;              /* ???????????? */
    level = CONVERT_LEVEL;
    bias = CONVERT_BIAS;

    flags |= DTS_ADJUST_LEVEL;
    if(dts_frame(state, buf, &flags, &level, bias)) {
        av_log(avctx, AV_LOG_ERROR, "dts_frame() failed\n");
        goto end;
252 253
    }

254 255 256 257 258 259 260 261 262 263
    avctx->sample_rate = sample_rate;
    avctx->channels = channels_multi(flags);
    avctx->bit_rate = bit_rate;

    for(i = 0; i < dts_blocks_num(state); i++) {
        int chans;

        if(dts_block(state)) {
            av_log(avctx, AV_LOG_ERROR, "dts_block() failed\n");
            goto end;
264
        }
265 266 267 268 269 270 271

        chans = channels_multi(flags);
        convert2s16_multi(dts_samples(state), out_samples,
                          flags & (DTS_CHANNEL_MASK | DTS_LFE));

        out_samples += 256 * chans;
        *data_size += 256 * sizeof(int16_t) * chans;
272
    }
273

274 275 276
end:
    bufptr = buf;
    bufpos = buf + HEADER_SIZE;
277
    return start - buff;
278 279 280
}

static int
281
dts_decode_init(AVCodecContext * avctx)
282
{
283 284 285
    avctx->priv_data = dts_init(0);
    if(avctx->priv_data == NULL)
        return -1;
286

287
    return 0;
288 289 290
}

static int
291
dts_decode_end(AVCodecContext * s)
292
{
293
    return 0;
294 295 296
}

AVCodec dts_decoder = {
297 298 299 300 301 302 303 304
    "dts",
    CODEC_TYPE_AUDIO,
    CODEC_ID_DTS,
    sizeof(dts_state_t *),
    dts_decode_init,
    NULL,
    dts_decode_end,
    dts_decode_frame,
305
};