]> git.sesse.net Git - ffmpeg/blob - libavcodec/libdcadec.c
Merge commit '06db45523c1068c24f049ef2b20fcdead3bf36d8'
[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     struct dcadec_context *ctx;
35     uint8_t *buffer;
36     int buffer_size;
37 } DCADecContext;
38
39 static int dcadec_decode_frame(AVCodecContext *avctx, void *data,
40                                int *got_frame_ptr, AVPacket *avpkt)
41 {
42     DCADecContext *s = avctx->priv_data;
43     AVFrame *frame = data;
44     int ret, i, k;
45     int **samples, nsamples, channel_mask, sample_rate, bits_per_sample, profile;
46     uint32_t mrk;
47     uint8_t *input = avpkt->data;
48     int input_size = avpkt->size;
49
50     /* convert bytestream syntax to RAW BE format if required */
51     if (input_size < 8) {
52         av_log(avctx, AV_LOG_ERROR, "Input size too small\n");
53         return AVERROR_INVALIDDATA;
54     }
55     mrk = AV_RB32(input);
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);
58         if (!s->buffer)
59             return AVERROR(ENOMEM);
60
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);
63
64         if (ret < 0)
65             return ret;
66
67         input      = s->buffer;
68         input_size = ret;
69     }
70
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;
74     }
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;
79     }
80
81     avctx->channels       = av_get_channel_layout_nb_channels(channel_mask);
82     avctx->channel_layout = channel_mask;
83     avctx->sample_rate    = sample_rate;
84
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;
89     else {
90         av_log(avctx, AV_LOG_ERROR, "Unsupported number of bits per sample: %d\n",
91                bits_per_sample);
92         return AVERROR(ENOSYS);
93     }
94
95     avctx->bits_per_raw_sample = bits_per_sample;
96
97     switch (profile) {
98     case DCADEC_PROFILE_DS:
99         avctx->profile = FF_PROFILE_DTS;
100         break;
101     case DCADEC_PROFILE_DS_96_24:
102         avctx->profile = FF_PROFILE_DTS_96_24;
103         break;
104     case DCADEC_PROFILE_DS_ES:
105         avctx->profile = FF_PROFILE_DTS_ES;
106         break;
107     case DCADEC_PROFILE_HD_HRA:
108         avctx->profile = FF_PROFILE_DTS_HD_HRA;
109         break;
110     case DCADEC_PROFILE_HD_MA:
111         avctx->profile = FF_PROFILE_DTS_HD_MA;
112         break;
113     case DCADEC_PROFILE_EXPRESS:
114         avctx->profile = FF_PROFILE_DTS_EXPRESS;
115         break;
116     case DCADEC_PROFILE_UNKNOWN:
117     default:
118         avctx->profile = FF_PROFILE_UNKNOWN;
119         break;
120     }
121
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);
127     } else
128         avctx->bit_rate = 0;
129
130     frame->nb_samples = nsamples;
131     if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
132         return ret;
133
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];
139         } else {
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;
144         }
145     }
146
147     *got_frame_ptr = 1;
148
149     return avpkt->size;
150 }
151
152 static av_cold void dcadec_flush(AVCodecContext *avctx)
153 {
154     DCADecContext *s = avctx->priv_data;
155     dcadec_context_clear(s->ctx);
156 }
157
158 static av_cold int dcadec_close(AVCodecContext *avctx)
159 {
160     DCADecContext *s = avctx->priv_data;
161
162     dcadec_context_destroy(s->ctx);
163     s->ctx = NULL;
164
165     av_freep(&s->buffer);
166
167     return 0;
168 }
169
170 static av_cold int dcadec_init(AVCodecContext *avctx)
171 {
172     DCADecContext *s = avctx->priv_data;
173     int flags = 0;
174
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;
178
179     s->ctx = dcadec_context_create(flags);
180     if (!s->ctx)
181         return AVERROR(ENOMEM);
182
183     avctx->sample_fmt = AV_SAMPLE_FMT_S32P;
184     avctx->bits_per_raw_sample = 24;
185
186     return 0;
187 }
188
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 },
197 };
198
199 AVCodec ff_libdcadec_decoder = {
200     .name           = "libdcadec",
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),
205     .init           = dcadec_init,
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),
213 };