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