2004-07-14 03:32:14 +02:00
|
|
|
/*
|
|
|
|
* dtsdec.c : free DTS Coherent Acoustics stream decoder.
|
|
|
|
* Copyright (C) 2004 Benjamin Zores <ben@geexbox.org>
|
|
|
|
*
|
2006-10-07 17:30:46 +02:00
|
|
|
* This file is part of FFmpeg.
|
2004-07-14 03:32:14 +02:00
|
|
|
*
|
2006-10-07 17:30:46 +02:00
|
|
|
* FFmpeg is free software; you can redistribute it and/or modify
|
2004-07-14 22:23:35 +02:00
|
|
|
* it under the terms of the GNU General Public License as published by
|
2006-10-09 09:31:27 +02:00
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
2004-07-14 22:23:35 +02:00
|
|
|
* (at your option) any later version.
|
2005-12-17 19:14:38 +01:00
|
|
|
*
|
2006-10-07 17:30:46 +02:00
|
|
|
* FFmpeg is distributed in the hope that it will be useful,
|
2004-07-14 03:32:14 +02:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2004-07-14 22:23:35 +02:00
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
2005-12-17 19:14:38 +01:00
|
|
|
*
|
2004-07-14 22:23:35 +02:00
|
|
|
* You should have received a copy of the GNU General Public License
|
2006-10-07 17:30:46 +02:00
|
|
|
* along with FFmpeg; if not, write to the Free Software
|
2007-01-22 02:18:33 +01:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
2004-07-14 03:32:14 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "avcodec.h"
|
|
|
|
#include <dts.h>
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2004-08-25 02:01:34 +02:00
|
|
|
|
2006-04-20 21:23:57 +02:00
|
|
|
#define BUFFER_SIZE 18726
|
2004-07-14 03:32:14 +02:00
|
|
|
#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
|
|
|
|
|
2007-02-05 21:17:02 +01:00
|
|
|
typedef struct DTSContext {
|
|
|
|
dts_state_t *state;
|
|
|
|
uint8_t buf[BUFFER_SIZE];
|
|
|
|
uint8_t *bufptr;
|
|
|
|
uint8_t *bufpos;
|
|
|
|
} DTSContext;
|
|
|
|
|
2007-02-05 20:35:36 +01:00
|
|
|
static inline int16_t
|
|
|
|
convert(int32_t i)
|
2004-07-14 03:32:14 +02:00
|
|
|
{
|
|
|
|
#ifdef LIBDTS_FIXED
|
|
|
|
i >>= 15;
|
|
|
|
#else
|
|
|
|
i -= 0x43c00000;
|
|
|
|
#endif
|
|
|
|
return (i > 32767) ? 32767 : ((i < -32768) ? -32768 : i);
|
|
|
|
}
|
|
|
|
|
2006-11-11 21:54:48 +01:00
|
|
|
static void
|
2007-02-05 20:35:36 +01:00
|
|
|
convert2s16_2(sample_t * _f, int16_t * s16)
|
2004-07-14 03:32:14 +02:00
|
|
|
{
|
2007-02-05 20:35:36 +01:00
|
|
|
int i;
|
|
|
|
int32_t *f = (int32_t *) _f;
|
2004-07-14 03:32:14 +02:00
|
|
|
|
2007-02-05 20:35:36 +01:00
|
|
|
for(i = 0; i < 256; i++) {
|
|
|
|
s16[2 * i] = convert(f[i]);
|
|
|
|
s16[2 * i + 1] = convert(f[i + 256]);
|
2004-07-14 03:32:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-11-11 21:54:48 +01:00
|
|
|
static void
|
2007-02-05 20:35:36 +01:00
|
|
|
convert2s16_4(sample_t * _f, int16_t * s16)
|
2004-07-14 03:32:14 +02:00
|
|
|
{
|
2007-02-05 20:35:36 +01:00
|
|
|
int i;
|
|
|
|
int32_t *f = (int32_t *) _f;
|
2004-07-14 03:32:14 +02:00
|
|
|
|
2007-02-05 20:35:36 +01:00
|
|
|
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]);
|
2004-07-14 03:32:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-11-11 21:54:48 +01:00
|
|
|
static void
|
2007-02-05 20:35:36 +01:00
|
|
|
convert2s16_5(sample_t * _f, int16_t * s16)
|
2004-07-14 03:32:14 +02:00
|
|
|
{
|
2007-02-05 20:35:36 +01:00
|
|
|
int i;
|
|
|
|
int32_t *f = (int32_t *) _f;
|
2004-07-14 03:32:14 +02:00
|
|
|
|
2007-02-05 20:35:36 +01:00
|
|
|
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]);
|
2004-07-14 03:32:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2007-02-05 20:35:36 +01:00
|
|
|
convert2s16_multi(sample_t * _f, int16_t * s16, int flags)
|
2004-07-14 03:32:14 +02:00
|
|
|
{
|
2007-02-05 20:35:36 +01:00
|
|
|
int i;
|
|
|
|
int32_t *f = (int32_t *) _f;
|
2004-07-14 03:32:14 +02:00
|
|
|
|
2007-02-05 20:35:36 +01:00
|
|
|
switch (flags) {
|
2004-07-14 03:32:14 +02:00
|
|
|
case DTS_MONO:
|
2007-02-05 20:35:36 +01:00
|
|
|
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]);
|
2004-07-14 03:32:14 +02:00
|
|
|
}
|
2007-02-05 20:35:36 +01:00
|
|
|
break;
|
2004-07-14 03:32:14 +02:00
|
|
|
case DTS_CHANNEL:
|
|
|
|
case DTS_STEREO:
|
|
|
|
case DTS_DOLBY:
|
2007-02-05 20:35:36 +01:00
|
|
|
convert2s16_2(_f, s16);
|
|
|
|
break;
|
2004-07-14 03:32:14 +02:00
|
|
|
case DTS_3F:
|
2007-02-05 20:35:36 +01:00
|
|
|
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]);
|
2004-07-14 03:32:14 +02:00
|
|
|
}
|
2007-02-05 20:35:36 +01:00
|
|
|
break;
|
2004-07-14 03:32:14 +02:00
|
|
|
case DTS_2F2R:
|
2007-02-05 20:35:36 +01:00
|
|
|
convert2s16_4(_f, s16);
|
|
|
|
break;
|
2004-07-14 03:32:14 +02:00
|
|
|
case DTS_3F2R:
|
2007-02-05 20:35:36 +01:00
|
|
|
convert2s16_5(_f, s16);
|
|
|
|
break;
|
2004-07-14 03:32:14 +02:00
|
|
|
case DTS_MONO | DTS_LFE:
|
2007-02-05 20:35:36 +01:00
|
|
|
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]);
|
2004-07-14 03:32:14 +02:00
|
|
|
}
|
2007-02-05 20:35:36 +01:00
|
|
|
break;
|
2004-07-14 03:32:14 +02:00
|
|
|
case DTS_CHANNEL | DTS_LFE:
|
|
|
|
case DTS_STEREO | DTS_LFE:
|
|
|
|
case DTS_DOLBY | DTS_LFE:
|
2007-02-05 20:35:36 +01:00
|
|
|
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]);
|
2004-07-14 03:32:14 +02:00
|
|
|
}
|
2007-02-05 20:35:36 +01:00
|
|
|
break;
|
2004-07-14 03:32:14 +02:00
|
|
|
case DTS_3F | DTS_LFE:
|
2007-02-05 20:35:36 +01:00
|
|
|
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]);
|
2004-07-14 03:32:14 +02:00
|
|
|
}
|
2007-02-05 20:35:36 +01:00
|
|
|
break;
|
2004-07-14 03:32:14 +02:00
|
|
|
case DTS_2F2R | DTS_LFE:
|
2007-02-05 20:35:36 +01:00
|
|
|
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]);
|
2004-07-14 03:32:14 +02:00
|
|
|
}
|
2007-02-05 20:35:36 +01:00
|
|
|
break;
|
2004-07-14 03:32:14 +02:00
|
|
|
case DTS_3F2R | DTS_LFE:
|
2007-02-05 20:35:36 +01:00
|
|
|
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]);
|
2004-07-14 03:32:14 +02:00
|
|
|
}
|
2007-02-05 20:35:36 +01:00
|
|
|
break;
|
2004-07-14 03:32:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2007-02-05 20:35:36 +01:00
|
|
|
channels_multi(int flags)
|
2004-07-14 03:32:14 +02:00
|
|
|
{
|
2007-02-05 20:35:36 +01:00
|
|
|
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;
|
2004-07-14 03:32:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2007-02-05 20:35:36 +01:00
|
|
|
dts_decode_frame(AVCodecContext * avctx, void *data, int *data_size,
|
|
|
|
uint8_t * buff, int buff_size)
|
2004-07-14 03:32:14 +02:00
|
|
|
{
|
2007-02-05 21:17:02 +01:00
|
|
|
DTSContext *s = avctx->priv_data;
|
2007-02-05 20:35:36 +01:00
|
|
|
uint8_t *start = buff;
|
|
|
|
uint8_t *end = buff + buff_size;
|
|
|
|
int16_t *out_samples = data;
|
2007-02-05 21:17:02 +01:00
|
|
|
int sample_rate;
|
|
|
|
int frame_length;
|
|
|
|
int flags;
|
2007-02-05 20:35:36 +01:00
|
|
|
int bit_rate;
|
|
|
|
int len;
|
2007-02-05 20:55:15 +01:00
|
|
|
level_t level;
|
|
|
|
sample_t bias;
|
|
|
|
int i;
|
2004-07-14 03:32:14 +02:00
|
|
|
|
2007-02-05 20:35:36 +01:00
|
|
|
*data_size = 0;
|
2004-09-12 19:35:54 +02:00
|
|
|
|
2007-02-05 20:35:36 +01:00
|
|
|
while(1) {
|
2007-02-05 20:55:15 +01:00
|
|
|
int length;
|
|
|
|
|
2007-02-05 20:35:36 +01:00
|
|
|
len = end - start;
|
|
|
|
if(!len)
|
|
|
|
break;
|
2007-02-05 21:17:02 +01:00
|
|
|
if(len > s->bufpos - s->bufptr)
|
|
|
|
len = s->bufpos - s->bufptr;
|
|
|
|
memcpy(s->bufptr, start, len);
|
|
|
|
s->bufptr += len;
|
2007-02-05 20:35:36 +01:00
|
|
|
start += len;
|
2007-02-05 21:17:02 +01:00
|
|
|
if(s->bufptr != s->bufpos)
|
2007-02-05 20:35:36 +01:00
|
|
|
return start - buff;
|
2007-02-05 21:17:02 +01:00
|
|
|
if(s->bufpos != s->buf + HEADER_SIZE)
|
2007-02-05 20:35:36 +01:00
|
|
|
break;
|
2006-04-20 21:23:57 +02:00
|
|
|
|
2007-02-05 21:17:02 +01:00
|
|
|
length = dts_syncinfo(s->state, s->buf, &flags, &sample_rate,
|
|
|
|
&bit_rate, &frame_length);
|
2007-02-05 20:55:15 +01:00
|
|
|
if(!length) {
|
|
|
|
av_log(NULL, AV_LOG_INFO, "skip\n");
|
2007-02-05 21:17:02 +01:00
|
|
|
for(s->bufptr = s->buf; s->bufptr < s->buf + HEADER_SIZE - 1; s->bufptr++)
|
|
|
|
s->bufptr[0] = s->bufptr[1];
|
2007-02-05 20:55:15 +01:00
|
|
|
continue;
|
2007-02-05 20:35:36 +01:00
|
|
|
}
|
2007-02-05 21:17:02 +01:00
|
|
|
s->bufpos = s->buf + length;
|
2007-02-05 20:55:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
flags = 2; /* ???????????? */
|
|
|
|
level = CONVERT_LEVEL;
|
|
|
|
bias = CONVERT_BIAS;
|
|
|
|
|
|
|
|
flags |= DTS_ADJUST_LEVEL;
|
2007-02-05 21:17:02 +01:00
|
|
|
if(dts_frame(s->state, s->buf, &flags, &level, bias)) {
|
2007-02-05 20:55:15 +01:00
|
|
|
av_log(avctx, AV_LOG_ERROR, "dts_frame() failed\n");
|
|
|
|
goto end;
|
2006-04-20 21:23:57 +02:00
|
|
|
}
|
|
|
|
|
2007-02-05 20:55:15 +01:00
|
|
|
avctx->sample_rate = sample_rate;
|
|
|
|
avctx->channels = channels_multi(flags);
|
|
|
|
avctx->bit_rate = bit_rate;
|
|
|
|
|
2007-02-05 21:17:02 +01:00
|
|
|
for(i = 0; i < dts_blocks_num(s->state); i++) {
|
2007-02-05 20:55:15 +01:00
|
|
|
int chans;
|
|
|
|
|
2007-02-05 21:17:02 +01:00
|
|
|
if(dts_block(s->state)) {
|
2007-02-05 20:55:15 +01:00
|
|
|
av_log(avctx, AV_LOG_ERROR, "dts_block() failed\n");
|
|
|
|
goto end;
|
2007-02-05 20:35:36 +01:00
|
|
|
}
|
2007-02-05 20:55:15 +01:00
|
|
|
|
|
|
|
chans = channels_multi(flags);
|
2007-02-05 21:17:02 +01:00
|
|
|
convert2s16_multi(dts_samples(s->state), out_samples,
|
2007-02-05 20:55:15 +01:00
|
|
|
flags & (DTS_CHANNEL_MASK | DTS_LFE));
|
|
|
|
|
|
|
|
out_samples += 256 * chans;
|
|
|
|
*data_size += 256 * sizeof(int16_t) * chans;
|
2007-02-05 20:35:36 +01:00
|
|
|
}
|
2004-07-14 03:32:14 +02:00
|
|
|
|
2007-02-05 20:55:15 +01:00
|
|
|
end:
|
2007-02-05 21:17:02 +01:00
|
|
|
s->bufptr = s->buf;
|
|
|
|
s->bufpos = s->buf + HEADER_SIZE;
|
2007-02-05 20:35:36 +01:00
|
|
|
return start - buff;
|
2004-07-14 03:32:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2007-02-05 20:35:36 +01:00
|
|
|
dts_decode_init(AVCodecContext * avctx)
|
2004-07-14 03:32:14 +02:00
|
|
|
{
|
2007-02-05 21:17:02 +01:00
|
|
|
DTSContext *s = avctx->priv_data;
|
|
|
|
s->bufptr = s->buf;
|
|
|
|
s->bufpos = s->buf + HEADER_SIZE;
|
|
|
|
s->state = dts_init(0);
|
|
|
|
if(s->state == NULL)
|
2007-02-05 20:35:36 +01:00
|
|
|
return -1;
|
2004-07-14 03:32:14 +02:00
|
|
|
|
2007-02-05 20:35:36 +01:00
|
|
|
return 0;
|
2004-07-14 03:32:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2007-02-05 21:17:02 +01:00
|
|
|
dts_decode_end(AVCodecContext * avctx)
|
2004-07-14 03:32:14 +02:00
|
|
|
{
|
2007-02-05 21:17:02 +01:00
|
|
|
DTSContext *s = avctx->priv_data;
|
|
|
|
dts_free(s->state);
|
2007-02-05 20:35:36 +01:00
|
|
|
return 0;
|
2004-07-14 03:32:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
AVCodec dts_decoder = {
|
2007-02-05 20:35:36 +01:00
|
|
|
"dts",
|
|
|
|
CODEC_TYPE_AUDIO,
|
|
|
|
CODEC_ID_DTS,
|
2007-02-05 21:17:02 +01:00
|
|
|
sizeof(DTSContext),
|
2007-02-05 20:35:36 +01:00
|
|
|
dts_decode_init,
|
|
|
|
NULL,
|
|
|
|
dts_decode_end,
|
|
|
|
dts_decode_frame,
|
2004-07-14 03:32:14 +02:00
|
|
|
};
|