~ubuntu-branches/ubuntu/jaunty/xvidcap/jaunty-proposed

« back to all changes in this revision

Viewing changes to ffmpeg/libavcodec/dtsdec.c

  • Committer: Bazaar Package Importer
  • Author(s): John Dong
  • Date: 2008-02-25 15:47:12 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20080225154712-qvr11ekcea4c9ry8
Tags: 1.1.6-0.1ubuntu1
* Merge from debian-multimedia (LP: #120003), Ubuntu Changes:
 - For ffmpeg-related build-deps, remove cvs from package names.
 - Standards-Version 3.7.3
 - Maintainer Spec

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * dtsdec.c : free DTS Coherent Acoustics stream decoder.
 
3
 * Copyright (C) 2004 Benjamin Zores <ben@geexbox.org>
 
4
 *
 
5
 * This file is part of FFmpeg.
 
6
 *
 
7
 * FFmpeg is free software; you can redistribute it and/or modify
 
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
 
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
 
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
15
 * GNU General Public License for more details.
 
16
 *
 
17
 * You should have received a copy of the GNU General Public License
 
18
 * 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
 
 
22
#include "avcodec.h"
 
23
#include <dts.h>
 
24
 
 
25
#include <stdlib.h>
 
26
#include <string.h>
 
27
 
 
28
#define BUFFER_SIZE 18726
 
29
#define HEADER_SIZE 14
 
30
 
 
31
#define CONVERT_LEVEL 1
 
32
#define CONVERT_BIAS 0
 
33
 
 
34
typedef struct DTSContext {
 
35
    dts_state_t *state;
 
36
    uint8_t buf[BUFFER_SIZE];
 
37
    uint8_t *bufptr;
 
38
    uint8_t *bufpos;
 
39
} DTSContext;
 
40
 
 
41
static inline int16_t
 
42
convert(sample_t s)
 
43
{
 
44
    return s * 0x7fff;
 
45
}
 
46
 
 
47
static void
 
48
convert2s16_multi(sample_t *f, int16_t *s16, int flags)
 
49
{
 
50
    int i;
 
51
 
 
52
    switch(flags & (DTS_CHANNEL_MASK | DTS_LFE)){
 
53
    case DTS_MONO:
 
54
        for(i = 0; i < 256; i++){
 
55
            s16[5*i] = s16[5*i+1] = s16[5*i+2] = s16[5*i+3] = 0;
 
56
            s16[5*i+4] = convert(f[i]);
 
57
        }
 
58
    case DTS_CHANNEL:
 
59
    case DTS_STEREO:
 
60
    case DTS_DOLBY:
 
61
        for(i = 0; i < 256; i++){
 
62
            s16[2*i] = convert(f[i]);
 
63
            s16[2*i+1] = convert(f[i+256]);
 
64
        }
 
65
    case DTS_3F:
 
66
        for(i = 0; i < 256; i++){
 
67
            s16[5*i] = convert(f[i+256]);
 
68
            s16[5*i+1] = convert(f[i+512]);
 
69
            s16[5*i+2] = s16[5*i+3] = 0;
 
70
            s16[5*i+4] = convert(f[i]);
 
71
        }
 
72
    case DTS_2F2R:
 
73
        for(i = 0; i < 256; i++){
 
74
            s16[4*i] = convert(f[i]);
 
75
            s16[4*i+1] = convert(f[i+256]);
 
76
            s16[4*i+2] = convert(f[i+512]);
 
77
            s16[4*i+3] = convert(f[i+768]);
 
78
        }
 
79
    case DTS_3F2R:
 
80
        for(i = 0; i < 256; i++){
 
81
            s16[5*i] = convert(f[i+256]);
 
82
            s16[5*i+1] = convert(f[i+512]);
 
83
            s16[5*i+2] = convert(f[i+768]);
 
84
            s16[5*i+3] = convert(f[i+1024]);
 
85
            s16[5*i+4] = convert(f[i]);
 
86
        }
 
87
    case DTS_MONO | DTS_LFE:
 
88
        for(i = 0; i < 256; i++){
 
89
            s16[6*i] = s16[6*i+1] = s16[6*i+2] = s16[6*i+3] = 0;
 
90
            s16[6*i+4] = convert(f[i]);
 
91
            s16[6*i+5] = convert(f[i+256]);
 
92
        }
 
93
    case DTS_CHANNEL | DTS_LFE:
 
94
    case DTS_STEREO | DTS_LFE:
 
95
    case DTS_DOLBY | DTS_LFE:
 
96
        for(i = 0; i < 256; i++){
 
97
            s16[6*i] = convert(f[i]);
 
98
            s16[6*i+1] = convert(f[i+256]);
 
99
            s16[6*i+2] = s16[6*i+3] = s16[6*i+4] = 0;
 
100
            s16[6*i+5] = convert(f[i+512]);
 
101
        }
 
102
    case DTS_3F | DTS_LFE:
 
103
        for(i = 0; i < 256; i++){
 
104
            s16[6*i] = convert(f[i+256]);
 
105
            s16[6*i+1] = convert(f[i+512]);
 
106
            s16[6*i+2] = s16[6*i+3] = 0;
 
107
            s16[6*i+4] = convert(f[i]);
 
108
            s16[6*i+5] = convert(f[i+768]);
 
109
        }
 
110
    case DTS_2F2R | DTS_LFE:
 
111
        for(i = 0; i < 256; i++){
 
112
            s16[6*i] = convert(f[i]);
 
113
            s16[6*i+1] = convert(f[i+256]);
 
114
            s16[6*i+2] = convert(f[i+512]);
 
115
            s16[6*i+3] = convert(f[i+768]);
 
116
            s16[6*i+4] = 0;
 
117
            s16[6*i+5] = convert(f[i+1024]);
 
118
        }
 
119
    case DTS_3F2R | DTS_LFE:
 
120
        for(i = 0; i < 256; i++){
 
121
            s16[6*i] = convert(f[i+256]);
 
122
            s16[6*i+1] = convert(f[i+512]);
 
123
            s16[6*i+2] = convert(f[i+768]);
 
124
            s16[6*i+3] = convert(f[i+1024]);
 
125
            s16[6*i+4] = convert(f[i]);
 
126
            s16[6*i+5] = convert(f[i+1280]);
 
127
        }
 
128
    }
 
129
}
 
130
 
 
131
static int
 
132
channels_multi(int flags)
 
133
{
 
134
    switch(flags & (DTS_CHANNEL_MASK | DTS_LFE)){
 
135
    case DTS_CHANNEL:
 
136
    case DTS_STEREO:
 
137
    case DTS_DOLBY:
 
138
        return 2;
 
139
    case DTS_2F2R:
 
140
        return 4;
 
141
    case DTS_MONO:
 
142
    case DTS_3F:
 
143
    case DTS_3F2R:
 
144
        return 5;
 
145
    case DTS_MONO | DTS_LFE:
 
146
    case DTS_CHANNEL | DTS_LFE:
 
147
    case DTS_STEREO | DTS_LFE:
 
148
    case DTS_DOLBY | DTS_LFE:
 
149
    case DTS_3F | DTS_LFE:
 
150
    case DTS_2F2R | DTS_LFE:
 
151
    case DTS_3F2R | DTS_LFE:
 
152
        return 6;
 
153
    }
 
154
 
 
155
    return -1;
 
156
}
 
157
 
 
158
static int
 
159
dts_decode_frame(AVCodecContext * avctx, void *data, int *data_size,
 
160
                 uint8_t * buff, int buff_size)
 
161
{
 
162
    DTSContext *s = avctx->priv_data;
 
163
    uint8_t *start = buff;
 
164
    uint8_t *end = buff + buff_size;
 
165
    int16_t *out_samples = data;
 
166
    int sample_rate;
 
167
    int frame_length;
 
168
    int flags;
 
169
    int bit_rate;
 
170
    int len;
 
171
    level_t level;
 
172
    sample_t bias;
 
173
    int nblocks;
 
174
    int i;
 
175
 
 
176
    *data_size = 0;
 
177
 
 
178
    while(1) {
 
179
        int length;
 
180
 
 
181
        len = end - start;
 
182
        if(!len)
 
183
            break;
 
184
        if(len > s->bufpos - s->bufptr)
 
185
            len = s->bufpos - s->bufptr;
 
186
        memcpy(s->bufptr, start, len);
 
187
        s->bufptr += len;
 
188
        start += len;
 
189
        if(s->bufptr != s->bufpos)
 
190
            return start - buff;
 
191
        if(s->bufpos != s->buf + HEADER_SIZE)
 
192
            break;
 
193
 
 
194
        length = dts_syncinfo(s->state, s->buf, &flags, &sample_rate,
 
195
                              &bit_rate, &frame_length);
 
196
        if(!length) {
 
197
            av_log(NULL, AV_LOG_INFO, "skip\n");
 
198
            for(s->bufptr = s->buf; s->bufptr < s->buf + HEADER_SIZE - 1; s->bufptr++)
 
199
                s->bufptr[0] = s->bufptr[1];
 
200
            continue;
 
201
        }
 
202
        s->bufpos = s->buf + length;
 
203
    }
 
204
 
 
205
    level = CONVERT_LEVEL;
 
206
    bias = CONVERT_BIAS;
 
207
 
 
208
    flags |= DTS_ADJUST_LEVEL;
 
209
    if(dts_frame(s->state, s->buf, &flags, &level, bias)) {
 
210
        av_log(avctx, AV_LOG_ERROR, "dts_frame() failed\n");
 
211
        goto end;
 
212
    }
 
213
 
 
214
    avctx->sample_rate = sample_rate;
 
215
    avctx->channels = channels_multi(flags);
 
216
    avctx->bit_rate = bit_rate;
 
217
 
 
218
    nblocks = dts_blocks_num(s->state);
 
219
 
 
220
    for(i = 0; i < nblocks; i++) {
 
221
        if(dts_block(s->state)) {
 
222
            av_log(avctx, AV_LOG_ERROR, "dts_block() failed\n");
 
223
            goto end;
 
224
        }
 
225
 
 
226
        convert2s16_multi(dts_samples(s->state), out_samples, flags);
 
227
 
 
228
        out_samples += 256 * avctx->channels;
 
229
        *data_size += 256 * sizeof(int16_t) * avctx->channels;
 
230
    }
 
231
 
 
232
end:
 
233
    s->bufptr = s->buf;
 
234
    s->bufpos = s->buf + HEADER_SIZE;
 
235
    return start - buff;
 
236
}
 
237
 
 
238
static int
 
239
dts_decode_init(AVCodecContext * avctx)
 
240
{
 
241
    DTSContext *s = avctx->priv_data;
 
242
    s->bufptr = s->buf;
 
243
    s->bufpos = s->buf + HEADER_SIZE;
 
244
    s->state = dts_init(0);
 
245
    if(s->state == NULL)
 
246
        return -1;
 
247
 
 
248
    return 0;
 
249
}
 
250
 
 
251
static int
 
252
dts_decode_end(AVCodecContext * avctx)
 
253
{
 
254
    DTSContext *s = avctx->priv_data;
 
255
    dts_free(s->state);
 
256
    return 0;
 
257
}
 
258
 
 
259
AVCodec dts_decoder = {
 
260
    "dts",
 
261
    CODEC_TYPE_AUDIO,
 
262
    CODEC_ID_DTS,
 
263
    sizeof(DTSContext),
 
264
    dts_decode_init,
 
265
    NULL,
 
266
    dts_decode_end,
 
267
    dts_decode_frame,
 
268
};