]> git.sesse.net Git - ffmpeg/blob - libavcodec/libdcadec.c
Merge commit '92e0b7342c0490894cbcea4461380321e0569de2'
[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/avassert.h"
25 #include "libavutil/channel_layout.h"
26 #include "libavutil/common.h"
27 #include "libavutil/opt.h"
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     mrk = AV_RB32(input);
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);
54         if (!s->buffer)
55             return AVERROR(ENOMEM);
56
57         if ((ret = avpriv_dca_convert_bitstream(avpkt->data, avpkt->size, s->buffer, s->buffer_size)) < 0)
58             return ret;
59
60         input      = s->buffer;
61         input_size = ret;
62     }
63
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;
67     }
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;
72     }
73
74     avctx->channels       = av_get_channel_layout_nb_channels(channel_mask);
75     avctx->channel_layout = channel_mask;
76     avctx->sample_rate    = sample_rate;
77
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;
81     else
82         avctx->sample_fmt = AV_SAMPLE_FMT_S32P;
83
84     avctx->bits_per_raw_sample = bits_per_sample;
85
86     switch (profile) {
87     case DCADEC_PROFILE_DS:
88         avctx->profile = FF_PROFILE_DTS;
89         break;
90     case DCADEC_PROFILE_DS_96_24:
91         avctx->profile = FF_PROFILE_DTS_96_24;
92         break;
93     case DCADEC_PROFILE_DS_ES:
94         avctx->profile = FF_PROFILE_DTS_ES;
95         break;
96     case DCADEC_PROFILE_HD_HRA:
97         avctx->profile = FF_PROFILE_DTS_HD_HRA;
98         break;
99     case DCADEC_PROFILE_HD_MA:
100         avctx->profile = FF_PROFILE_DTS_HD_MA;
101         break;
102     case DCADEC_PROFILE_EXPRESS:
103         avctx->profile = FF_PROFILE_DTS_EXPRESS;
104         break;
105     case DCADEC_PROFILE_UNKNOWN:
106     default:
107         avctx->profile = FF_PROFILE_UNKNOWN;
108         break;
109     }
110
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);
116     } else
117         avctx->bit_rate = 0;
118
119     frame->nb_samples = nsamples;
120     if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
121         return ret;
122
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];
128         } else {
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;
133         }
134     }
135
136     *got_frame_ptr = 1;
137
138     return avpkt->size;
139 }
140
141 static av_cold void dcadec_flush(AVCodecContext *avctx)
142 {
143     DCADecContext *s = avctx->priv_data;
144     dcadec_context_clear(s->ctx);
145 }
146
147 static av_cold int dcadec_close(AVCodecContext *avctx)
148 {
149     DCADecContext *s = avctx->priv_data;
150
151     dcadec_context_destroy(s->ctx);
152     s->ctx = NULL;
153
154     av_freep(&s->buffer);
155
156     return 0;
157 }
158
159 static av_cold int dcadec_init(AVCodecContext *avctx)
160 {
161     DCADecContext *s = avctx->priv_data;
162
163     s->ctx = dcadec_context_create(0);
164     if (!s->ctx)
165         return AVERROR(ENOMEM);
166
167     avctx->sample_fmt = AV_SAMPLE_FMT_S32P;
168     avctx->bits_per_raw_sample = 24;
169
170     return 0;
171 }
172
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 },
181 };
182
183 AVCodec ff_libdcadec_decoder = {
184     .name           = "libdcadec",
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),
189     .init           = dcadec_init,
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),
197 };