2 * libdcadec decoder wrapper
3 * Copyright (C) 2015 Hendrik Leppkes
5 * This file is part of FFmpeg.
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
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 GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 #include <libdcadec/dca_context.h>
24 #include "libavutil/channel_layout.h"
25 #include "libavutil/common.h"
26 #include "libavutil/opt.h"
30 #include "dca_syncwords.h"
33 typedef struct DCADecContext {
34 struct dcadec_context *ctx;
39 static int dcadec_decode_frame(AVCodecContext *avctx, void *data,
40 int *got_frame_ptr, AVPacket *avpkt)
42 DCADecContext *s = avctx->priv_data;
43 AVFrame *frame = data;
45 int **samples, nsamples, channel_mask, sample_rate, bits_per_sample, profile;
47 uint8_t *input = avpkt->data;
48 int input_size = avpkt->size;
50 /* convert bytestream syntax to RAW BE format if required */
52 av_log(avctx, AV_LOG_ERROR, "Input size too small\n");
53 return AVERROR_INVALIDDATA;
56 if (mrk != DCA_SYNCWORD_CORE_BE && mrk != DCA_SYNCWORD_SUBSTREAM) {
57 s->buffer = av_fast_realloc(s->buffer, &s->buffer_size, avpkt->size + FF_INPUT_BUFFER_PADDING_SIZE);
59 return AVERROR(ENOMEM);
61 for (i = 0, ret = AVERROR_INVALIDDATA; i < input_size - 3 && ret < 0; i++)
62 ret = avpriv_dca_convert_bitstream(input + i, input_size - i, s->buffer, s->buffer_size);
71 if ((ret = dcadec_context_parse(s->ctx, input, input_size)) < 0) {
72 av_log(avctx, AV_LOG_ERROR, "dcadec_context_parse() failed: %d (%s)\n", -ret, dcadec_strerror(ret));
73 return AVERROR_EXTERNAL;
75 if ((ret = dcadec_context_filter(s->ctx, &samples, &nsamples, &channel_mask,
76 &sample_rate, &bits_per_sample, &profile)) < 0) {
77 av_log(avctx, AV_LOG_ERROR, "dcadec_context_filter() failed: %d (%s)\n", -ret, dcadec_strerror(ret));
78 return AVERROR_EXTERNAL;
81 avctx->channels = av_get_channel_layout_nb_channels(channel_mask);
82 avctx->channel_layout = channel_mask;
83 avctx->sample_rate = sample_rate;
85 if (bits_per_sample == 16)
86 avctx->sample_fmt = AV_SAMPLE_FMT_S16P;
87 else if (bits_per_sample > 16 && bits_per_sample <= 24)
88 avctx->sample_fmt = AV_SAMPLE_FMT_S32P;
90 av_log(avctx, AV_LOG_ERROR, "Unsupported number of bits per sample: %d\n",
92 return AVERROR(ENOSYS);
95 avctx->bits_per_raw_sample = bits_per_sample;
98 case DCADEC_PROFILE_DS:
99 avctx->profile = FF_PROFILE_DTS;
101 case DCADEC_PROFILE_DS_96_24:
102 avctx->profile = FF_PROFILE_DTS_96_24;
104 case DCADEC_PROFILE_DS_ES:
105 avctx->profile = FF_PROFILE_DTS_ES;
107 case DCADEC_PROFILE_HD_HRA:
108 avctx->profile = FF_PROFILE_DTS_HD_HRA;
110 case DCADEC_PROFILE_HD_MA:
111 avctx->profile = FF_PROFILE_DTS_HD_MA;
113 case DCADEC_PROFILE_EXPRESS:
114 avctx->profile = FF_PROFILE_DTS_EXPRESS;
116 case DCADEC_PROFILE_UNKNOWN:
118 avctx->profile = FF_PROFILE_UNKNOWN;
122 /* bitrate is only meaningful if there are no HD extensions, as they distort the bitrate */
123 if (profile == DCADEC_PROFILE_DS || profile == DCADEC_PROFILE_DS_96_24 || profile == DCADEC_PROFILE_DS_ES) {
124 struct dcadec_core_info *info = dcadec_context_get_core_info(s->ctx);
125 avctx->bit_rate = info->bit_rate;
126 dcadec_context_free_core_info(info);
130 frame->nb_samples = nsamples;
131 if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
134 for (i = 0; i < avctx->channels; i++) {
135 if (frame->format == AV_SAMPLE_FMT_S16P) {
136 int16_t *plane = (int16_t *)frame->extended_data[i];
137 for (k = 0; k < nsamples; k++)
138 plane[k] = samples[i][k];
140 int32_t *plane = (int32_t *)frame->extended_data[i];
141 int shift = 32 - bits_per_sample;
142 for (k = 0; k < nsamples; k++)
143 plane[k] = samples[i][k] << shift;
152 static av_cold void dcadec_flush(AVCodecContext *avctx)
154 DCADecContext *s = avctx->priv_data;
155 dcadec_context_clear(s->ctx);
158 static av_cold int dcadec_close(AVCodecContext *avctx)
160 DCADecContext *s = avctx->priv_data;
162 dcadec_context_destroy(s->ctx);
165 av_freep(&s->buffer);
170 static av_cold int dcadec_init(AVCodecContext *avctx)
172 DCADecContext *s = avctx->priv_data;
175 /* Affects only lossy DTS profiles. DTS-HD MA is always bitexact */
176 if (avctx->flags & CODEC_FLAG_BITEXACT)
177 flags |= DCADEC_FLAG_CORE_BIT_EXACT;
179 s->ctx = dcadec_context_create(flags);
181 return AVERROR(ENOMEM);
183 avctx->sample_fmt = AV_SAMPLE_FMT_S32P;
184 avctx->bits_per_raw_sample = 24;
189 static const AVProfile profiles[] = {
190 { FF_PROFILE_DTS, "DTS" },
191 { FF_PROFILE_DTS_ES, "DTS-ES" },
192 { FF_PROFILE_DTS_96_24, "DTS 96/24" },
193 { FF_PROFILE_DTS_HD_HRA, "DTS-HD HRA" },
194 { FF_PROFILE_DTS_HD_MA, "DTS-HD MA" },
195 { FF_PROFILE_DTS_EXPRESS, "DTS Express" },
196 { FF_PROFILE_UNKNOWN },
199 AVCodec ff_libdcadec_decoder = {
201 .long_name = NULL_IF_CONFIG_SMALL("dcadec DCA decoder"),
202 .type = AVMEDIA_TYPE_AUDIO,
203 .id = AV_CODEC_ID_DTS,
204 .priv_data_size = sizeof(DCADecContext),
206 .decode = dcadec_decode_frame,
207 .close = dcadec_close,
208 .flush = dcadec_flush,
209 .capabilities = CODEC_CAP_DR1 | CODEC_CAP_CHANNEL_CONF,
210 .sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_S32P, AV_SAMPLE_FMT_S16P,
211 AV_SAMPLE_FMT_NONE },
212 .profiles = NULL_IF_CONFIG_SMALL(profiles),