]> git.sesse.net Git - ffmpeg/blob - libavcodec/libdcadec.c
vc2enc: mark as FF_CODEC_CAP_INIT_THREADSAFE and align AVCodec entries
[ffmpeg] / libavcodec / libdcadec.c
1 /*
2  * libdcadec decoder wrapper
3  * Copyright (C) 2015 Hendrik Leppkes
4  *
5  * This file is part of FFmpeg.
6  *
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.
11  *
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.
16  *
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
20  */
21
22 #include <libdcadec/dca_context.h>
23
24 #include "libavutil/channel_layout.h"
25 #include "libavutil/common.h"
26 #include "libavutil/opt.h"
27
28 #include "avcodec.h"
29 #include "dca.h"
30 #include "dca_syncwords.h"
31 #include "internal.h"
32 #include "profiles.h"
33
34 typedef struct DCADecContext {
35     const AVClass *class;
36     struct dcadec_context *ctx;
37     uint8_t *buffer;
38     int buffer_size;
39     int lfe_filter;
40     int core_only;
41 } DCADecContext;
42
43 static void my_log_cb(int level, const char *file, int line,
44                       const char *message, void *cbarg)
45 {
46     int av_level;
47
48     switch (level) {
49     case DCADEC_LOG_ERROR:
50         av_level = AV_LOG_ERROR;
51         break;
52     case DCADEC_LOG_WARNING:
53         av_level = AV_LOG_WARNING;
54         break;
55     case DCADEC_LOG_INFO:
56         av_level = AV_LOG_INFO;
57         break;
58     case DCADEC_LOG_VERBOSE:
59         av_level = AV_LOG_VERBOSE;
60         break;
61     case DCADEC_LOG_DEBUG:
62     default:
63         av_level = AV_LOG_DEBUG;
64         break;
65     }
66
67     av_log(cbarg, av_level, "%s\n", message);
68 }
69
70 static int dcadec_decode_frame(AVCodecContext *avctx, void *data,
71                                int *got_frame_ptr, AVPacket *avpkt)
72 {
73     DCADecContext *s = avctx->priv_data;
74     AVFrame *frame = data;
75     struct dcadec_exss_info *exss;
76     int ret, i, k;
77     int **samples, nsamples, channel_mask, sample_rate, bits_per_sample, profile;
78     uint32_t mrk;
79     uint8_t *input = avpkt->data;
80     int input_size = avpkt->size;
81
82     /* convert bytestream syntax to RAW BE format if required */
83     if (input_size < 8) {
84         av_log(avctx, AV_LOG_ERROR, "Input size too small\n");
85         return AVERROR_INVALIDDATA;
86     }
87     mrk = AV_RB32(input);
88     if (mrk != DCA_SYNCWORD_CORE_BE && mrk != DCA_SYNCWORD_SUBSTREAM) {
89         s->buffer = av_fast_realloc(s->buffer, &s->buffer_size, avpkt->size + AV_INPUT_BUFFER_PADDING_SIZE);
90         if (!s->buffer)
91             return AVERROR(ENOMEM);
92
93         for (i = 0, ret = AVERROR_INVALIDDATA; i < input_size - 3 && ret < 0; i++)
94             ret = avpriv_dca_convert_bitstream(input + i, input_size - i, s->buffer, s->buffer_size);
95
96         if (ret < 0)
97             return ret;
98
99         input      = s->buffer;
100         input_size = ret;
101     }
102
103     if ((ret = dcadec_context_parse(s->ctx, input, input_size)) < 0) {
104         av_log(avctx, AV_LOG_ERROR, "dcadec_context_parse() failed: %d (%s)\n", -ret, dcadec_strerror(ret));
105         return AVERROR_EXTERNAL;
106     }
107     if ((ret = dcadec_context_filter(s->ctx, &samples, &nsamples, &channel_mask,
108                                      &sample_rate, &bits_per_sample, &profile)) < 0) {
109         av_log(avctx, AV_LOG_ERROR, "dcadec_context_filter() failed: %d (%s)\n", -ret, dcadec_strerror(ret));
110         return AVERROR_EXTERNAL;
111     }
112
113     avctx->channels       = av_get_channel_layout_nb_channels(channel_mask);
114     avctx->channel_layout = channel_mask;
115     avctx->sample_rate    = sample_rate;
116
117     if (bits_per_sample == 16)
118         avctx->sample_fmt = AV_SAMPLE_FMT_S16P;
119     else if (bits_per_sample > 16 && bits_per_sample <= 24)
120         avctx->sample_fmt = AV_SAMPLE_FMT_S32P;
121     else {
122         av_log(avctx, AV_LOG_ERROR, "Unsupported number of bits per sample: %d\n",
123                bits_per_sample);
124         return AVERROR(ENOSYS);
125     }
126
127     avctx->bits_per_raw_sample = bits_per_sample;
128
129     switch (profile) {
130     case DCADEC_PROFILE_DS:
131         avctx->profile = FF_PROFILE_DTS;
132         break;
133     case DCADEC_PROFILE_DS_96_24:
134         avctx->profile = FF_PROFILE_DTS_96_24;
135         break;
136     case DCADEC_PROFILE_DS_ES:
137         avctx->profile = FF_PROFILE_DTS_ES;
138         break;
139     case DCADEC_PROFILE_HD_HRA:
140         avctx->profile = FF_PROFILE_DTS_HD_HRA;
141         break;
142     case DCADEC_PROFILE_HD_MA:
143         avctx->profile = FF_PROFILE_DTS_HD_MA;
144         break;
145     case DCADEC_PROFILE_EXPRESS:
146         avctx->profile = FF_PROFILE_DTS_EXPRESS;
147         break;
148     case DCADEC_PROFILE_UNKNOWN:
149     default:
150         avctx->profile = FF_PROFILE_UNKNOWN;
151         break;
152     }
153
154     /* bitrate is only meaningful if there are no HD extensions, as they distort the bitrate */
155     if (profile == DCADEC_PROFILE_DS || profile == DCADEC_PROFILE_DS_96_24 || profile == DCADEC_PROFILE_DS_ES) {
156         struct dcadec_core_info *info = dcadec_context_get_core_info(s->ctx);
157         avctx->bit_rate = info->bit_rate;
158         dcadec_context_free_core_info(info);
159     } else
160         avctx->bit_rate = 0;
161
162     if (exss = dcadec_context_get_exss_info(s->ctx)) {
163         enum AVMatrixEncoding matrix_encoding = AV_MATRIX_ENCODING_NONE;
164
165         switch(exss->matrix_encoding) {
166         case DCADEC_MATRIX_ENCODING_SURROUND:
167             matrix_encoding = AV_MATRIX_ENCODING_DOLBY;
168             break;
169         case DCADEC_MATRIX_ENCODING_HEADPHONE:
170             matrix_encoding = AV_MATRIX_ENCODING_DOLBYHEADPHONE;
171             break;
172         }
173         dcadec_context_free_exss_info(exss);
174
175         if (matrix_encoding != AV_MATRIX_ENCODING_NONE &&
176             (ret = ff_side_data_update_matrix_encoding(frame, matrix_encoding)) < 0)
177             return ret;
178     }
179
180     frame->nb_samples = nsamples;
181     if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
182         return ret;
183
184     for (i = 0; i < avctx->channels; i++) {
185         if (frame->format == AV_SAMPLE_FMT_S16P) {
186             int16_t *plane = (int16_t *)frame->extended_data[i];
187             for (k = 0; k < nsamples; k++)
188                 plane[k] = samples[i][k];
189         } else {
190             int32_t *plane = (int32_t *)frame->extended_data[i];
191             int shift = 32 - bits_per_sample;
192             for (k = 0; k < nsamples; k++)
193                 plane[k] = samples[i][k] << shift;
194         }
195     }
196
197     *got_frame_ptr = 1;
198
199     return avpkt->size;
200 }
201
202 static av_cold void dcadec_flush(AVCodecContext *avctx)
203 {
204     DCADecContext *s = avctx->priv_data;
205     dcadec_context_clear(s->ctx);
206 }
207
208 static av_cold int dcadec_close(AVCodecContext *avctx)
209 {
210     DCADecContext *s = avctx->priv_data;
211
212     dcadec_context_destroy(s->ctx);
213     s->ctx = NULL;
214
215     av_freep(&s->buffer);
216
217     return 0;
218 }
219
220 static av_cold int dcadec_init(AVCodecContext *avctx)
221 {
222     DCADecContext *s = avctx->priv_data;
223     int flags = 0;
224
225     /* Affects only lossy DTS profiles. DTS-HD MA is always bitexact */
226     if (avctx->flags & AV_CODEC_FLAG_BITEXACT)
227         flags |= DCADEC_FLAG_CORE_BIT_EXACT;
228
229     if (avctx->err_recognition & AV_EF_EXPLODE)
230         flags |= DCADEC_FLAG_STRICT;
231
232     if (avctx->request_channel_layout) {
233         switch (avctx->request_channel_layout) {
234         case AV_CH_LAYOUT_STEREO:
235         case AV_CH_LAYOUT_STEREO_DOWNMIX:
236             flags |= DCADEC_FLAG_KEEP_DMIX_2CH;
237             break;
238         case AV_CH_LAYOUT_5POINT1:
239             flags |= DCADEC_FLAG_KEEP_DMIX_6CH;
240             break;
241         case AV_CH_LAYOUT_NATIVE:
242             flags |= DCADEC_FLAG_NATIVE_LAYOUT;
243             break;
244         default:
245             av_log(avctx, AV_LOG_WARNING, "Invalid request_channel_layout\n");
246             break;
247         }
248     }
249
250     if (s->core_only)
251         flags |= DCADEC_FLAG_CORE_ONLY;
252
253     switch (s->lfe_filter) {
254 #if DCADEC_API_VERSION >= DCADEC_VERSION_CODE(0, 1, 0)
255     case 1:
256         flags |= DCADEC_FLAG_CORE_LFE_IIR;
257         break;
258 #endif
259     case 2:
260         flags |= DCADEC_FLAG_CORE_LFE_FIR;
261         break;
262     }
263
264     s->ctx = dcadec_context_create(flags);
265     if (!s->ctx)
266         return AVERROR(ENOMEM);
267
268     dcadec_context_set_log_cb(s->ctx, my_log_cb, avctx);
269
270     avctx->sample_fmt = AV_SAMPLE_FMT_S32P;
271     avctx->bits_per_raw_sample = 24;
272
273     return 0;
274 }
275
276 #define OFFSET(x) offsetof(DCADecContext, x)
277 #define PARAM AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_DECODING_PARAM
278
279 static const AVOption dcadec_options[] = {
280     { "lfe_filter", "Lossy LFE channel interpolation filter", OFFSET(lfe_filter), AV_OPT_TYPE_INT,   { .i64 = 0 }, 0,       2,       PARAM, "lfe_filter" },
281     { "default",    "Library default",                        0,                  AV_OPT_TYPE_CONST, { .i64 = 0 }, INT_MIN, INT_MAX, PARAM, "lfe_filter" },
282     { "iir",        "IIR filter",                             0,                  AV_OPT_TYPE_CONST, { .i64 = 1 }, INT_MIN, INT_MAX, PARAM, "lfe_filter" },
283     { "fir",        "FIR filter",                             0,                  AV_OPT_TYPE_CONST, { .i64 = 2 }, INT_MIN, INT_MAX, PARAM, "lfe_filter" },
284     { "core_only",  "Decode core only without extensions",    OFFSET(core_only),  AV_OPT_TYPE_BOOL,  { .i64 = 0 }, 0,       1,       PARAM },
285     { NULL }
286 };
287
288 static const AVClass dcadec_class = {
289     .class_name = "libdcadec decoder",
290     .item_name  = av_default_item_name,
291     .option     = dcadec_options,
292     .version    = LIBAVUTIL_VERSION_INT,
293     .category   = AV_CLASS_CATEGORY_DECODER,
294 };
295
296 AVCodec ff_libdcadec_decoder = {
297     .name           = "libdcadec",
298     .long_name      = NULL_IF_CONFIG_SMALL("dcadec DCA decoder"),
299     .type           = AVMEDIA_TYPE_AUDIO,
300     .id             = AV_CODEC_ID_DTS,
301     .priv_data_size = sizeof(DCADecContext),
302     .init           = dcadec_init,
303     .decode         = dcadec_decode_frame,
304     .close          = dcadec_close,
305     .flush          = dcadec_flush,
306     .capabilities   = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_CHANNEL_CONF,
307     .sample_fmts    = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_S32P, AV_SAMPLE_FMT_S16P,
308                                                       AV_SAMPLE_FMT_NONE },
309     .priv_class     = &dcadec_class,
310     .profiles       = NULL_IF_CONFIG_SMALL(ff_dca_profiles),
311 };