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/avassert.h"
25 #include "libavutil/channel_layout.h"
26 #include "libavutil/common.h"
27 #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 if (mrk != DCA_SYNCWORD_CORE_BE && mrk != DCA_SYNCWORD_SUBSTREAM) {
53 s->buffer = av_fast_realloc(s->buffer, &s->buffer_size, avpkt->size + FF_INPUT_BUFFER_PADDING_SIZE);
55 return AVERROR(ENOMEM);
57 if ((ret = avpriv_dca_convert_bitstream(avpkt->data, avpkt->size, s->buffer, s->buffer_size)) < 0)
64 if ((ret = dcadec_context_parse(s->ctx, input, input_size)) < 0) {
65 av_log(avctx, AV_LOG_ERROR, "dcadec_context_parse() failed: %d (%s)\n", -ret, dcadec_strerror(ret));
66 return AVERROR_EXTERNAL;
68 if ((ret = dcadec_context_filter(s->ctx, &samples, &nsamples, &channel_mask,
69 &sample_rate, &bits_per_sample, &profile)) < 0) {
70 av_log(avctx, AV_LOG_ERROR, "dcadec_context_filter() failed: %d (%s)\n", -ret, dcadec_strerror(ret));
71 return AVERROR_EXTERNAL;
74 avctx->channels = av_get_channel_layout_nb_channels(channel_mask);
75 avctx->channel_layout = channel_mask;
76 avctx->sample_rate = sample_rate;
78 av_assert0(bits_per_sample >= 16 && bits_per_sample <= 24);
79 if (bits_per_sample == 16)
80 avctx->sample_fmt = AV_SAMPLE_FMT_S16P;
82 avctx->sample_fmt = AV_SAMPLE_FMT_S32P;
84 avctx->bits_per_raw_sample = bits_per_sample;
87 case DCADEC_PROFILE_DS:
88 avctx->profile = FF_PROFILE_DTS;
90 case DCADEC_PROFILE_DS_96_24:
91 avctx->profile = FF_PROFILE_DTS_96_24;
93 case DCADEC_PROFILE_DS_ES:
94 avctx->profile = FF_PROFILE_DTS_ES;
96 case DCADEC_PROFILE_HD_HRA:
97 avctx->profile = FF_PROFILE_DTS_HD_HRA;
99 case DCADEC_PROFILE_HD_MA:
100 avctx->profile = FF_PROFILE_DTS_HD_MA;
102 case DCADEC_PROFILE_EXPRESS:
103 avctx->profile = FF_PROFILE_DTS_EXPRESS;
105 case DCADEC_PROFILE_UNKNOWN:
107 avctx->profile = FF_PROFILE_UNKNOWN;
111 /* bitrate is only meaningful if there are no HD extensions, as they distort the bitrate */
112 if (profile == DCADEC_PROFILE_DS || profile == DCADEC_PROFILE_DS_96_24 || profile == DCADEC_PROFILE_DS_ES) {
113 struct dcadec_core_info *info = dcadec_context_get_core_info(s->ctx);
114 avctx->bit_rate = info->bit_rate;
115 dcadec_context_free_core_info(info);
119 frame->nb_samples = nsamples;
120 if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
123 for (i = 0; i < avctx->channels; i++) {
124 if (frame->format == AV_SAMPLE_FMT_S16P) {
125 int16_t *plane = (int16_t *)frame->extended_data[i];
126 for (k = 0; k < nsamples; k++)
127 plane[k] = samples[i][k];
129 int32_t *plane = (int32_t *)frame->extended_data[i];
130 int shift = 32 - bits_per_sample;
131 for (k = 0; k < nsamples; k++)
132 plane[k] = samples[i][k] << shift;
141 static av_cold void dcadec_flush(AVCodecContext *avctx)
143 DCADecContext *s = avctx->priv_data;
144 dcadec_context_clear(s->ctx);
147 static av_cold int dcadec_close(AVCodecContext *avctx)
149 DCADecContext *s = avctx->priv_data;
151 dcadec_context_destroy(s->ctx);
154 av_freep(&s->buffer);
159 static av_cold int dcadec_init(AVCodecContext *avctx)
161 DCADecContext *s = avctx->priv_data;
163 s->ctx = dcadec_context_create(0);
165 return AVERROR(ENOMEM);
167 avctx->sample_fmt = AV_SAMPLE_FMT_S32P;
168 avctx->bits_per_raw_sample = 24;
173 static const AVProfile profiles[] = {
174 { FF_PROFILE_DTS, "DTS" },
175 { FF_PROFILE_DTS_ES, "DTS-ES" },
176 { FF_PROFILE_DTS_96_24, "DTS 96/24" },
177 { FF_PROFILE_DTS_HD_HRA, "DTS-HD HRA" },
178 { FF_PROFILE_DTS_HD_MA, "DTS-HD MA" },
179 { FF_PROFILE_DTS_EXPRESS, "DTS Express" },
180 { FF_PROFILE_UNKNOWN },
183 AVCodec ff_libdcadec_decoder = {
185 .long_name = NULL_IF_CONFIG_SMALL("dcadec DCA decoder"),
186 .type = AVMEDIA_TYPE_AUDIO,
187 .id = AV_CODEC_ID_DTS,
188 .priv_data_size = sizeof(DCADecContext),
190 .decode = dcadec_decode_frame,
191 .close = dcadec_close,
192 .flush = dcadec_flush,
193 .capabilities = CODEC_CAP_DR1 | CODEC_CAP_CHANNEL_CONF,
194 .sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_S32P, AV_SAMPLE_FMT_S16P,
195 AV_SAMPLE_FMT_NONE },
196 .profiles = NULL_IF_CONFIG_SMALL(profiles),