Commit 86e493a6 authored by Peter Ross's avatar Peter Ross Committed by Paul B Mahol

avcodec: add Direct Stream Transfer (DST) decoder

Signed-off-by: 's avatarPaul B Mahol <onemda@gmail.com>
parent 365b0c13
......@@ -239,13 +239,14 @@ OBJS-$(CONFIG_DNXHD_DECODER) += dnxhddec.o dnxhddata.o
OBJS-$(CONFIG_DNXHD_ENCODER) += dnxhdenc.o dnxhddata.o
OBJS-$(CONFIG_DPX_DECODER) += dpx.o
OBJS-$(CONFIG_DPX_ENCODER) += dpxenc.o
OBJS-$(CONFIG_DSD_LSBF_DECODER) += dsddec.o
OBJS-$(CONFIG_DSD_MSBF_DECODER) += dsddec.o
OBJS-$(CONFIG_DSD_LSBF_PLANAR_DECODER) += dsddec.o
OBJS-$(CONFIG_DSD_MSBF_PLANAR_DECODER) += dsddec.o
OBJS-$(CONFIG_DSD_LSBF_DECODER) += dsddec.o dsd.o
OBJS-$(CONFIG_DSD_MSBF_DECODER) += dsddec.o dsd.o
OBJS-$(CONFIG_DSD_LSBF_PLANAR_DECODER) += dsddec.o dsd.o
OBJS-$(CONFIG_DSD_MSBF_PLANAR_DECODER) += dsddec.o dsd.o
OBJS-$(CONFIG_DSICINAUDIO_DECODER) += dsicinaudio.o
OBJS-$(CONFIG_DSICINVIDEO_DECODER) += dsicinvideo.o
OBJS-$(CONFIG_DSS_SP_DECODER) += dss_sp.o
OBJS-$(CONFIG_DST_DECODER) += dstdec.o dsd.o
OBJS-$(CONFIG_DVBSUB_DECODER) += dvbsubdec.o
OBJS-$(CONFIG_DVBSUB_ENCODER) += dvbsub.o
OBJS-$(CONFIG_DVDSUB_DECODER) += dvdsubdec.o
......
......@@ -395,6 +395,7 @@ void avcodec_register_all(void)
REGISTER_DECODER(DSD_MSBF_PLANAR, dsd_msbf_planar);
REGISTER_DECODER(DSICINAUDIO, dsicinaudio);
REGISTER_DECODER(DSS_SP, dss_sp);
REGISTER_DECODER(DST, dst);
REGISTER_ENCDEC (EAC3, eac3);
REGISTER_DECODER(EVRC, evrc);
REGISTER_DECODER(FFWAVESYNTH, ffwavesynth);
......
......@@ -591,6 +591,7 @@ enum AVCodecID {
AV_CODEC_ID_INTERPLAY_ACM,
AV_CODEC_ID_XMA1,
AV_CODEC_ID_XMA2,
AV_CODEC_ID_DST,
/* subtitle codecs */
AV_CODEC_ID_FIRST_SUBTITLE = 0x17000, ///< A dummy ID pointing at the start of subtitle codecs.
......
......@@ -2683,6 +2683,13 @@ static const AVCodecDescriptor codec_descriptors[] = {
.long_name = NULL_IF_CONFIG_SMALL("Xbox Media Audio 2"),
.props = AV_CODEC_PROP_LOSSY,
},
{
.id = AV_CODEC_ID_DST,
.type = AVMEDIA_TYPE_AUDIO,
.name = "dst",
.long_name = NULL_IF_CONFIG_SMALL("DST (Direct Stream Transfer)"),
.props = AV_CODEC_PROP_LOSSLESS,
},
/* subtitle codecs */
{
......
/*
* Direct Stream Digital (DSD) decoder
* based on BSD licensed dsd2pcm by Sebastian Gesemann
* Copyright (c) 2009, 2011 Sebastian Gesemann. All rights reserved.
* Copyright (c) 2014 Peter Ross
*
* 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
*/
#include "libavcodec/internal.h"
#include "libavcodec/mathops.h"
#include "avcodec.h"
#include "dsd_tablegen.h"
#include "dsd.h"
static av_cold void dsd_ctables_tableinit(void)
{
int t, e, m, sign;
double acc[CTABLES];
for (e = 0; e < 256; ++e) {
memset(acc, 0, sizeof(acc));
for (m = 0; m < 8; ++m) {
sign = (((e >> (7 - m)) & 1) * 2 - 1);
for (t = 0; t < CTABLES; ++t)
acc[t] += sign * htaps[t * 8 + m];
}
for (t = 0; t < CTABLES; ++t)
ctables[CTABLES - 1 - t][e] = acc[t];
}
}
av_cold void ff_init_dsd_data(void)
{
static int done = 0;
if (done)
return;
dsd_ctables_tableinit();
done = 1;
}
void ff_dsd2pcm_translate(DSDContext* s, size_t samples, int lsbf,
const unsigned char *src, ptrdiff_t src_stride,
float *dst, ptrdiff_t dst_stride)
{
unsigned pos, i;
unsigned char* p;
double sum;
pos = s->pos;
while (samples-- > 0) {
s->buf[pos] = lsbf ? ff_reverse[*src] : *src;
src += src_stride;
p = s->buf + ((pos - CTABLES) & FIFOMASK);
*p = ff_reverse[*p];
sum = 0.0;
for (i = 0; i < CTABLES; i++) {
unsigned char a = s->buf[(pos - i) & FIFOMASK];
unsigned char b = s->buf[(pos - (CTABLES*2 - 1) + i) & FIFOMASK];
sum += ctables[i][a] + ctables[i][b];
}
*dst = (float)sum;
dst += dst_stride;
pos = (pos + 1) & FIFOMASK;
}
s->pos = pos;
}
/*
* Direct Stream Digital (DSD) decoder
* based on BSD licensed dsd2pcm by Sebastian Gesemann
* Copyright (c) 2009, 2011 Sebastian Gesemann. All rights reserved.
* Copyright (c) 2014 Peter Ross
*
* 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
*/
#ifndef AVCODEC_DSD_H
#define AVCODEC_DSD_H
#include "libavcodec/internal.h"
#include "libavcodec/mathops.h"
#include "avcodec.h"
#include "dsd_tablegen.h"
#define HTAPS 48 /** number of FIR constants */
#define FIFOSIZE 16 /** must be a power of two */
#define FIFOMASK (FIFOSIZE - 1) /** bit mask for FIFO offsets */
#if FIFOSIZE * 8 < HTAPS * 2
#error "FIFOSIZE too small"
#endif
/**
* Per-channel buffer
*/
typedef struct DSDContext {
unsigned char buf[FIFOSIZE];
unsigned pos;
} DSDContext;
void ff_init_dsd_data(void);
void ff_dsd2pcm_translate(DSDContext* s, size_t samples, int lsbf,
const unsigned char *src, ptrdiff_t src_stride,
float *dst, ptrdiff_t dst_stride);
#endif /* AVCODEC_DSD_H */
......@@ -25,6 +25,7 @@
#include <stdint.h>
#include "libavutil/attributes.h"
#include "dsd.h"
#define HTAPS 48 /** number of FIR constants */
#define CTABLES ((HTAPS + 7) / 8) /** number of "8 MACs" lookup tables */
......@@ -71,21 +72,4 @@ static const double htaps[HTAPS] = {
};
static float ctables[CTABLES][256];
static av_cold void dsd_ctables_tableinit(void)
{
int t, e, m, sign;
double acc[CTABLES];
for (e = 0; e < 256; ++e) {
memset(acc, 0, sizeof(acc));
for (m = 0; m < 8; ++m) {
sign = (((e >> (7 - m)) & 1) * 2 - 1);
for (t = 0; t < CTABLES; ++t)
acc[t] += sign * htaps[t * 8 + m];
}
for (t = 0; t < CTABLES; ++t)
ctables[CTABLES - 1 - t][e] = acc[t];
}
}
#endif /* AVCODEC_DSD_TABLEGEN_H */
......@@ -29,71 +29,14 @@
#include "libavcodec/internal.h"
#include "libavcodec/mathops.h"
#include "avcodec.h"
#include "dsd_tablegen.h"
#define FIFOSIZE 16 /** must be a power of two */
#define FIFOMASK (FIFOSIZE - 1) /** bit mask for FIFO offsets */
#if FIFOSIZE * 8 < HTAPS * 2
#error "FIFOSIZE too small"
#endif
/**
* Per-channel buffer
*/
typedef struct {
unsigned char buf[FIFOSIZE];
unsigned pos;
} DSDContext;
static void dsd2pcm_translate(DSDContext* s, size_t samples, int lsbf,
const unsigned char *src, ptrdiff_t src_stride,
float *dst, ptrdiff_t dst_stride)
{
unsigned pos, i;
unsigned char* p;
double sum;
pos = s->pos;
while (samples-- > 0) {
s->buf[pos] = lsbf ? ff_reverse[*src] : *src;
src += src_stride;
p = s->buf + ((pos - CTABLES) & FIFOMASK);
*p = ff_reverse[*p];
sum = 0.0;
for (i = 0; i < CTABLES; i++) {
unsigned char a = s->buf[(pos - i) & FIFOMASK];
unsigned char b = s->buf[(pos - (CTABLES*2 - 1) + i) & FIFOMASK];
sum += ctables[i][a] + ctables[i][b];
}
*dst = (float)sum;
dst += dst_stride;
pos = (pos + 1) & FIFOMASK;
}
s->pos = pos;
}
static av_cold void init_static_data(void)
{
static int done = 0;
if (done)
return;
dsd_ctables_tableinit();
done = 1;
}
#include "dsd.h"
static av_cold int decode_init(AVCodecContext *avctx)
{
DSDContext * s;
int i;
init_static_data();
ff_init_dsd_data();
s = av_malloc_array(sizeof(DSDContext), avctx->channels);
if (!s)
......@@ -140,7 +83,7 @@ static int decode_frame(AVCodecContext *avctx, void *data,
for (i = 0; i < avctx->channels; i++) {
float * dst = ((float **)frame->extended_data)[i];
dsd2pcm_translate(&s[i], frame->nb_samples, lsbf,
ff_dsd2pcm_translate(&s[i], frame->nb_samples, lsbf,
avpkt->data + i * src_next, src_stride,
dst, 1);
}
......
This diff is collapsed.
......@@ -3490,6 +3490,8 @@ static int get_audio_frame_duration(enum AVCodecID id, int sr, int ch, int ba,
/* calc from sample rate */
if (id == AV_CODEC_ID_TTA)
return 256 * sr / 245;
else if (id == AV_CODEC_ID_DST)
return 588 * sr / 44100;
if (ch > 0) {
/* calc from sample rate and channels */
......
......@@ -59,6 +59,10 @@
#define ID_RGB8 MKTAG('R','G','B','8')
#define ID_RGBN MKTAG('R','G','B','N')
#define ID_DSD MKTAG('D','S','D',' ')
#define ID_DST MKTAG('D','S','T',' ')
#define ID_DSTC MKTAG('D','S','T','C')
#define ID_DSTF MKTAG('D','S','T','F')
#define ID_FRTE MKTAG('F','R','T','E')
#define ID_ANIM MKTAG('A','N','I','M')
#define ID_ANHD MKTAG('A','N','H','D')
#define ID_DLTA MKTAG('D','L','T','A')
......@@ -159,6 +163,7 @@ static int iff_probe(AVProbeData *p)
static const AVCodecTag dsd_codec_tags[] = {
{ AV_CODEC_ID_DSD_MSBF, ID_DSD },
{ AV_CODEC_ID_DST, ID_DST },
{ AV_CODEC_ID_NONE, 0 },
};
......@@ -287,7 +292,7 @@ static int parse_dsd_prop(AVFormatContext *s, AVStream *st, uint64_t eof)
case MKTAG('C','M','P','R'):
if (size < 4)
return AVERROR_INVALIDDATA;
tag = avio_rl32(pb);
st->codecpar->codec_tag = tag = avio_rl32(pb);
st->codecpar->codec_id = ff_codec_get_id(dsd_codec_tags, tag);
if (!st->codecpar->codec_id) {
av_log(s, AV_LOG_ERROR, "'%c%c%c%c' compression is not supported\n",
......@@ -338,6 +343,63 @@ static int parse_dsd_prop(AVFormatContext *s, AVStream *st, uint64_t eof)
return 0;
}
static int read_dst_frame(AVFormatContext *s, AVPacket *pkt)
{
IffDemuxContext *iff = s->priv_data;
AVIOContext *pb = s->pb;
uint32_t chunk_id;
uint64_t chunk_pos, data_pos, data_size;
int ret = AVERROR_EOF;
while (!avio_feof(pb)) {
chunk_pos = avio_tell(pb);
if (chunk_pos >= iff->body_end)
return AVERROR_EOF;
chunk_id = avio_rl32(pb);
data_size = iff->is_64bit ? avio_rb64(pb) : avio_rb32(pb);
data_pos = avio_tell(pb);
if (data_size < 1)
return AVERROR_INVALIDDATA;
switch (chunk_id) {
case ID_DSTF:
if (!pkt) {
iff->body_pos = avio_tell(pb) - (iff->is_64bit ? 12 : 8);
iff->body_size = iff->body_end - iff->body_pos;
return 0;
}
ret = av_get_packet(pb, pkt, data_size);
if (ret < 0)
return ret;
if (data_size & 1)
avio_skip(pb, 1);
pkt->flags |= AV_PKT_FLAG_KEY;
pkt->stream_index = 0;
pkt->duration = 588 * s->streams[0]->codecpar->sample_rate / 44100;
pkt->pos = chunk_pos;
chunk_pos = avio_tell(pb);
if (chunk_pos >= iff->body_end)
return 0;
avio_seek(pb, chunk_pos, SEEK_SET);
return 0;
case ID_FRTE:
if (data_size < 4)
return AVERROR_INVALIDDATA;
s->streams[0]->duration = avio_rb32(pb) * 588LL * s->streams[0]->codecpar->sample_rate / 44100;
break;
}
avio_skip(pb, data_size - (avio_tell(pb) - data_pos) + (data_size & 1));
}
return ret;
}
static const uint8_t deep_rgb24[] = {0, 0, 0, 3, 0, 1, 0, 8, 0, 2, 0, 8, 0, 3, 0, 8};
static const uint8_t deep_rgba[] = {0, 0, 0, 4, 0, 1, 0, 8, 0, 2, 0, 8, 0, 3, 0, 8};
static const uint8_t deep_bgra[] = {0, 0, 0, 4, 0, 3, 0, 8, 0, 2, 0, 8, 0, 1, 0, 8};
......@@ -425,10 +487,16 @@ static int iff_read_header(AVFormatContext *s)
case ID_BODY:
case ID_DBOD:
case ID_DSD:
case ID_DST:
case ID_MDAT:
iff->body_pos = avio_tell(pb);
iff->body_end = iff->body_pos + data_size;
iff->body_size = data_size;
if (chunk_id == ID_DST) {
int ret = read_dst_frame(s, NULL);
if (ret < 0)
return ret;
}
break;
case ID_CHAN:
......@@ -654,7 +722,8 @@ static int iff_read_header(AVFormatContext *s)
avpriv_request_sample(s, "compression %d and bit depth %d", iff->maud_compression, iff->maud_bits);
return AVERROR_PATCHWELCOME;
}
} else if (st->codecpar->codec_tag != ID_DSD) {
} else if (st->codecpar->codec_tag != ID_DSD &&
st->codecpar->codec_tag != ID_DST) {
switch (iff->svx8_compression) {
case COMP_NONE:
st->codecpar->codec_id = AV_CODEC_ID_PCM_S8_PLANAR;
......@@ -675,6 +744,8 @@ static int iff_read_header(AVFormatContext *s)
st->codecpar->bits_per_coded_sample = av_get_bits_per_sample(st->codecpar->codec_id);
st->codecpar->bit_rate = st->codecpar->channels * st->codecpar->sample_rate * st->codecpar->bits_per_coded_sample;
st->codecpar->block_align = st->codecpar->channels * st->codecpar->bits_per_coded_sample;
if (st->codecpar->codec_tag == ID_DSD && st->codecpar->block_align <= 0)
return AVERROR_INVALIDDATA;
break;
case AVMEDIA_TYPE_VIDEO:
......@@ -745,16 +816,16 @@ static int iff_read_packet(AVFormatContext *s,
int ret;
int64_t pos = avio_tell(pb);
if (st->codecpar->codec_tag == ID_ANIM) {
if (avio_feof(pb))
return AVERROR_EOF;
} else if (pos >= iff->body_end) {
if (avio_feof(pb))
return AVERROR_EOF;
if (st->codecpar->codec_tag != ID_ANIM && pos >= iff->body_end)
return AVERROR_EOF;
}
if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
if (st->codecpar->codec_tag == ID_DSD || st->codecpar->codec_tag == ID_MAUD) {
ret = av_get_packet(pb, pkt, FFMIN(iff->body_end - pos, 1024 * st->codecpar->block_align));
} else if (st->codecpar->codec_tag == ID_DST) {
return read_dst_frame(s, pkt);
} else {
if (iff->body_size > INT_MAX)
return AVERROR_INVALIDDATA;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment