Statistics
| Branch: | Revision:

ffmpeg / libavcodec / dtsdec.c @ 5509bffa

History | View | Annotate | Download (7.65 KB)

1 23c99253 Michael Niedermayer
/*
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 libavcodec.
6
 *
7 a708785f Benjamin Zores
 * This library 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 115329f1 Diego Biurrun
 *
12 23c99253 Michael Niedermayer
 * This library is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 a708785f Benjamin Zores
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 * GNU General Public License for more details.
16 115329f1 Diego Biurrun
 *
17 a708785f Benjamin Zores
 * You should have received a copy of the GNU General Public License
18
 * along with this program; if not, write to the Free Software
19
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 23c99253 Michael Niedermayer
 */
21
22
#ifdef HAVE_AV_CONFIG_H
23
#undef HAVE_AV_CONFIG_H
24
#endif
25
26
#include "avcodec.h"
27
#include <dts.h>
28
29
#include <stdlib.h>
30
#include <string.h>
31 ad730459 Bohdan Horst
32
#ifdef HAVE_MALLOC_H
33 23c99253 Michael Niedermayer
#include <malloc.h>
34 ad730459 Bohdan Horst
#endif
35 23c99253 Michael Niedermayer
36
#define INBUF_SIZE 4096
37
#define BUFFER_SIZE 4096
38
#define HEADER_SIZE 14
39
40
#ifdef LIBDTS_FIXED
41
#define CONVERT_LEVEL (1 << 26)
42
#define CONVERT_BIAS 0
43
#else
44
#define CONVERT_LEVEL 1
45
#define CONVERT_BIAS 384
46
#endif
47
48
static inline
49
int16_t convert (int32_t i)
50
{
51
#ifdef LIBDTS_FIXED
52
    i >>= 15;
53
#else
54
    i -= 0x43c00000;
55
#endif
56
    return (i > 32767) ? 32767 : ((i < -32768) ? -32768 : i);
57
}
58
59
void
60
convert2s16_2 (sample_t * _f, int16_t * s16)
61
{
62
  int i;
63
  int32_t * f = (int32_t *) _f;
64
65
  for (i = 0; i < 256; i++)
66
    {
67
      s16[2*i] = convert (f[i]);
68
      s16[2*i+1] = convert (f[i+256]);
69
    }
70
}
71
72
void
73
convert2s16_4 (sample_t * _f, int16_t * s16)
74
{
75
  int i;
76
  int32_t * f = (int32_t *) _f;
77
78
  for (i = 0; i < 256; i++)
79
    {
80
      s16[4*i] = convert (f[i]);
81
      s16[4*i+1] = convert (f[i+256]);
82
      s16[4*i+2] = convert (f[i+512]);
83
      s16[4*i+3] = convert (f[i+768]);
84
    }
85
}
86
87
void
88
convert2s16_5 (sample_t * _f, int16_t * s16)
89
{
90
  int i;
91
  int32_t * f = (int32_t *) _f;
92
93
  for (i = 0; i < 256; i++)
94
    {
95
      s16[5*i] = convert (f[i]);
96
      s16[5*i+1] = convert (f[i+256]);
97
      s16[5*i+2] = convert (f[i+512]);
98
      s16[5*i+3] = convert (f[i+768]);
99
      s16[5*i+4] = convert (f[i+1024]);
100
    }
101
}
102
103
static void
104
convert2s16_multi (sample_t * _f, int16_t * s16, int flags)
105
{
106
  int i;
107
  int32_t * f = (int32_t *) _f;
108
109
  switch (flags)
110
    {
111
    case DTS_MONO:
112
      for (i = 0; i < 256; i++)
113
        {
114
          s16[5*i] = s16[5*i+1] = s16[5*i+2] = s16[5*i+3] = 0;
115
          s16[5*i+4] = convert (f[i]);
116
        }
117
      break;
118
    case DTS_CHANNEL:
119
    case DTS_STEREO:
120
    case DTS_DOLBY:
121
      convert2s16_2 (_f, s16);
122
      break;
123
    case DTS_3F:
124
      for (i = 0; i < 256; i++)
125
        {
126
          s16[5*i] = convert (f[i]);
127
          s16[5*i+1] = convert (f[i+512]);
128
          s16[5*i+2] = s16[5*i+3] = 0;
129
          s16[5*i+4] = convert (f[i+256]);
130
        }
131
      break;
132
    case DTS_2F2R:
133
      convert2s16_4 (_f, s16);
134
      break;
135
    case DTS_3F2R:
136
      convert2s16_5 (_f, s16);
137
      break;
138
    case DTS_MONO | DTS_LFE:
139
      for (i = 0; i < 256; i++)
140
        {
141
          s16[6*i] = s16[6*i+1] = s16[6*i+2] = s16[6*i+3] = 0;
142
          s16[6*i+4] = convert (f[i+256]);
143
          s16[6*i+5] = convert (f[i]);
144
        }
145
      break;
146
    case DTS_CHANNEL | DTS_LFE:
147
    case DTS_STEREO | DTS_LFE:
148
    case DTS_DOLBY | DTS_LFE:
149
      for (i = 0; i < 256; i++)
150
        {
151
          s16[6*i] = convert (f[i+256]);
152
          s16[6*i+1] = convert (f[i+512]);
153
          s16[6*i+2] = s16[6*i+3] = s16[6*i+4] = 0;
154
          s16[6*i+5] = convert (f[i]);
155
        }
156
      break;
157
    case DTS_3F | DTS_LFE:
158
      for (i = 0; i < 256; i++)
159
        {
160
          s16[6*i] = convert (f[i+256]);
161
          s16[6*i+1] = convert (f[i+768]);
162
          s16[6*i+2] = s16[6*i+3] = 0;
163
          s16[6*i+4] = convert (f[i+512]);
164
          s16[6*i+5] = convert (f[i]);
165
        }
166
      break;
167
    case DTS_2F2R | DTS_LFE:
168
      for (i = 0; i < 256; i++)
169
        {
170
          s16[6*i] = convert (f[i+256]);
171
          s16[6*i+1] = convert (f[i+512]);
172
          s16[6*i+2] = convert (f[i+768]);
173
          s16[6*i+3] = convert (f[i+1024]);
174
          s16[6*i+4] = 0;
175
          s16[6*i+5] = convert (f[i]);
176
        }
177
      break;
178
    case DTS_3F2R | DTS_LFE:
179
      for (i = 0; i < 256; i++)
180
        {
181
          s16[6*i] = convert (f[i+256]);
182
          s16[6*i+1] = convert (f[i+768]);
183
          s16[6*i+2] = convert (f[i+1024]);
184
          s16[6*i+3] = convert (f[i+1280]);
185
          s16[6*i+4] = convert (f[i+512]);
186
          s16[6*i+5] = convert (f[i]);
187
        }
188
      break;
189
    }
190
}
191
192
static int
193
channels_multi (int flags)
194
{
195
  if (flags & DTS_LFE)
196
    return 6;
197 bb270c08 Diego Biurrun
  else if (flags & 1)   /* center channel */
198 23c99253 Michael Niedermayer
    return 5;
199
  else if ((flags & DTS_CHANNEL_MASK) == DTS_2F2R)
200
    return 4;
201
  else
202
    return 2;
203
}
204
205
static int
206
dts_decode_frame (AVCodecContext *avctx, void *data, int *data_size,
207
                  uint8_t *buff, int buff_size)
208
{
209
  uint8_t * start = buff;
210
  uint8_t * end = buff + buff_size;
211
  static uint8_t buf[BUFFER_SIZE];
212
  static uint8_t * bufptr = buf;
213
  static uint8_t * bufpos = buf + HEADER_SIZE;
214
215
  static int sample_rate;
216
  static int frame_length;
217
  static int flags;
218
  int bit_rate;
219
  int len;
220
  dts_state_t *state = avctx->priv_data;
221
222 0fd847a1 Michael Niedermayer
  *data_size = 0;
223
224 23c99253 Michael Niedermayer
  while (1)
225
    {
226
      len = end - start;
227
      if (!len)
228
        break;
229
      if (len > bufpos - bufptr)
230
        len = bufpos - bufptr;
231
      memcpy (bufptr, start, len);
232
      bufptr += len;
233
      start += len;
234
      if (bufptr == bufpos)
235
        {
236
          if (bufpos == buf + HEADER_SIZE)
237
            {
238
              int length;
239
240
              length = dts_syncinfo (state, buf, &flags, &sample_rate,
241
                                     &bit_rate, &frame_length);
242
              if (!length)
243
                {
244
                  av_log (NULL, AV_LOG_INFO, "skip\n");
245
                  for (bufptr = buf; bufptr < buf + HEADER_SIZE-1; bufptr++)
246
                    bufptr[0] = bufptr[1];
247
                  continue;
248
                }
249
              bufpos = buf + length;
250
            }
251
          else
252
            {
253
              level_t level;
254
              sample_t bias;
255
              int i;
256
257
              flags = 2; /* ???????????? */
258
              level = CONVERT_LEVEL;
259
              bias = CONVERT_BIAS;
260
261
              flags |= DTS_ADJUST_LEVEL;
262
              if (dts_frame (state, buf, &flags, &level, bias))
263
                goto error;
264 c5c97a8d Michael Niedermayer
              avctx->sample_rate = sample_rate;
265
              avctx->channels = channels_multi (flags);
266
              avctx->bit_rate = bit_rate;
267 23c99253 Michael Niedermayer
              for (i = 0; i < dts_blocks_num (state); i++)
268
                {
269
                  if (dts_block (state))
270
                    goto error;
271
                  {
272
                    int chans;
273
                    chans = channels_multi (flags);
274
                    convert2s16_multi (dts_samples (state), data,
275
                                       flags & (DTS_CHANNEL_MASK | DTS_LFE));
276
277
                    data += 256 * sizeof (int16_t) * chans;
278
                    *data_size += 256 * sizeof (int16_t) * chans;
279
                  }
280
                }
281
              bufptr = buf;
282
              bufpos = buf + HEADER_SIZE;
283
              continue;
284
            error:
285
              av_log (NULL, AV_LOG_ERROR, "error\n");
286
              bufptr = buf;
287
              bufpos = buf + HEADER_SIZE;
288
            }
289
        }
290
    }
291
292
  return buff_size;
293
}
294
295
static int
296
dts_decode_init (AVCodecContext *avctx)
297
{
298 a708785f Benjamin Zores
  avctx->priv_data = dts_init (0);
299
  if (avctx->priv_data == NULL)
300 23c99253 Michael Niedermayer
    return 1;
301
302
  return 0;
303
}
304
305
static int
306
dts_decode_end (AVCodecContext *s)
307
{
308
  return 0;
309
}
310
311
AVCodec dts_decoder = {
312 115329f1 Diego Biurrun
  "dts",
313 23c99253 Michael Niedermayer
  CODEC_TYPE_AUDIO,
314
  CODEC_ID_DTS,
315 a708785f Benjamin Zores
  sizeof (dts_state_t *),
316 23c99253 Michael Niedermayer
  dts_decode_init,
317
  NULL,
318
  dts_decode_end,
319
  dts_decode_frame,
320
};