1 /*
2  * This file is part of mpv.
3  *
4  * mpv is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * mpv is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with mpv.  If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <unistd.h>
21 #include <stdbool.h>
22 #include <assert.h>
23 
24 #include <libavcodec/avcodec.h>
25 #include <libavutil/opt.h>
26 #include <libavutil/common.h>
27 #include <libavutil/intreadwrite.h>
28 
29 #include "mpv_talloc.h"
30 #include "audio/aframe.h"
31 #include "audio/fmt-conversion.h"
32 #include "common/av_common.h"
33 #include "common/codecs.h"
34 #include "common/global.h"
35 #include "common/msg.h"
36 #include "demux/packet.h"
37 #include "demux/stheader.h"
38 #include "filters/f_decoder_wrapper.h"
39 #include "filters/filter_internal.h"
40 #include "options/m_config.h"
41 #include "options/options.h"
42 
43 struct priv {
44     AVCodecContext *avctx;
45     AVFrame *avframe;
46     struct mp_chmap force_channel_map;
47     uint32_t skip_samples, trim_samples;
48     bool preroll_done;
49     double next_pts;
50     AVRational codec_timebase;
51     struct lavc_state state;
52 
53     struct mp_decoder public;
54 };
55 
56 #define OPT_BASE_STRUCT struct ad_lavc_params
57 struct ad_lavc_params {
58     float ac3drc;
59     int downmix;
60     int threads;
61     char **avopts;
62 };
63 
64 const struct m_sub_options ad_lavc_conf = {
65     .opts = (const m_option_t[]) {
66         {"ac3drc", OPT_FLOAT(ac3drc), M_RANGE(0, 6)},
67         {"downmix", OPT_FLAG(downmix)},
68         {"threads", OPT_INT(threads), M_RANGE(0, 16)},
69         {"o", OPT_KEYVALUELIST(avopts)},
70         {0}
71     },
72     .size = sizeof(struct ad_lavc_params),
73     .defaults = &(const struct ad_lavc_params){
74         .ac3drc = 0,
75         .downmix = 0,
76         .threads = 1,
77     },
78 };
79 
init(struct mp_filter * da,struct mp_codec_params * codec,const char * decoder)80 static bool init(struct mp_filter *da, struct mp_codec_params *codec,
81                  const char *decoder)
82 {
83     struct priv *ctx = da->priv;
84     struct MPOpts *mpopts = mp_get_config_group(ctx, da->global, &mp_opt_root);
85     struct ad_lavc_params *opts =
86         mp_get_config_group(ctx, da->global, &ad_lavc_conf);
87     AVCodecContext *lavc_context;
88     const AVCodec *lavc_codec;
89 
90     ctx->codec_timebase = mp_get_codec_timebase(codec);
91 
92     if (codec->force_channels)
93         ctx->force_channel_map = codec->channels;
94 
95     lavc_codec = avcodec_find_decoder_by_name(decoder);
96     if (!lavc_codec) {
97         MP_ERR(da, "Cannot find codec '%s' in libavcodec...\n", decoder);
98         return false;
99     }
100 
101     lavc_context = avcodec_alloc_context3(lavc_codec);
102     ctx->avctx = lavc_context;
103     ctx->avframe = av_frame_alloc();
104     lavc_context->codec_type = AVMEDIA_TYPE_AUDIO;
105     lavc_context->codec_id = lavc_codec->id;
106     lavc_context->pkt_timebase = ctx->codec_timebase;
107 
108     if (opts->downmix && mpopts->audio_output_channels.num_chmaps == 1) {
109         lavc_context->request_channel_layout =
110             mp_chmap_to_lavc(&mpopts->audio_output_channels.chmaps[0]);
111     }
112 
113     // Always try to set - option only exists for AC3 at the moment
114     av_opt_set_double(lavc_context, "drc_scale", opts->ac3drc,
115                       AV_OPT_SEARCH_CHILDREN);
116 
117     // Let decoder add AV_FRAME_DATA_SKIP_SAMPLES.
118     av_opt_set(lavc_context, "flags2", "+skip_manual", AV_OPT_SEARCH_CHILDREN);
119 
120     mp_set_avopts(da->log, lavc_context, opts->avopts);
121 
122     if (mp_set_avctx_codec_headers(lavc_context, codec) < 0) {
123         MP_ERR(da, "Could not set decoder parameters.\n");
124         return false;
125     }
126 
127     mp_set_avcodec_threads(da->log, lavc_context, opts->threads);
128 
129     /* open it */
130     if (avcodec_open2(lavc_context, lavc_codec, NULL) < 0) {
131         MP_ERR(da, "Could not open codec.\n");
132         return false;
133     }
134 
135     ctx->next_pts = MP_NOPTS_VALUE;
136 
137     return true;
138 }
139 
destroy(struct mp_filter * da)140 static void destroy(struct mp_filter *da)
141 {
142     struct priv *ctx = da->priv;
143 
144     avcodec_free_context(&ctx->avctx);
145     av_frame_free(&ctx->avframe);
146 }
147 
reset(struct mp_filter * da)148 static void reset(struct mp_filter *da)
149 {
150     struct priv *ctx = da->priv;
151 
152     avcodec_flush_buffers(ctx->avctx);
153     ctx->skip_samples = 0;
154     ctx->trim_samples = 0;
155     ctx->preroll_done = false;
156     ctx->next_pts = MP_NOPTS_VALUE;
157     ctx->state = (struct lavc_state){0};
158 }
159 
send_packet(struct mp_filter * da,struct demux_packet * mpkt)160 static int send_packet(struct mp_filter *da, struct demux_packet *mpkt)
161 {
162     struct priv *priv = da->priv;
163     AVCodecContext *avctx = priv->avctx;
164 
165     // If the decoder discards the timestamp for some reason, we use the
166     // interpolated PTS. Initialize it so that it works for the initial
167     // packet as well.
168     if (mpkt && priv->next_pts == MP_NOPTS_VALUE)
169         priv->next_pts = mpkt->pts;
170 
171     AVPacket pkt;
172     mp_set_av_packet(&pkt, mpkt, &priv->codec_timebase);
173 
174     int ret = avcodec_send_packet(avctx, mpkt ? &pkt : NULL);
175     if (ret < 0)
176         MP_ERR(da, "Error decoding audio.\n");
177     return ret;
178 }
179 
receive_frame(struct mp_filter * da,struct mp_frame * out)180 static int receive_frame(struct mp_filter *da, struct mp_frame *out)
181 {
182     struct priv *priv = da->priv;
183     AVCodecContext *avctx = priv->avctx;
184 
185     int ret = avcodec_receive_frame(avctx, priv->avframe);
186 
187     if (ret == AVERROR_EOF) {
188         // If flushing was initialized earlier and has ended now, make it start
189         // over in case we get new packets at some point in the future.
190         // (Dont' reset the filter itself, we want to keep other state.)
191         avcodec_flush_buffers(priv->avctx);
192         return ret;
193     } else if (ret < 0 && ret != AVERROR(EAGAIN)) {
194         MP_ERR(da, "Error decoding audio.\n");
195     }
196 
197     if (priv->avframe->flags & AV_FRAME_FLAG_DISCARD)
198         av_frame_unref(priv->avframe);
199 
200     if (!priv->avframe->buf[0])
201         return ret;
202 
203     double out_pts = mp_pts_from_av(priv->avframe->pts, &priv->codec_timebase);
204 
205     struct mp_aframe *mpframe = mp_aframe_from_avframe(priv->avframe);
206     if (!mpframe) {
207         MP_ERR(da, "Converting libavcodec frame to mpv frame failed.\n");
208         return ret;
209     }
210 
211     if (priv->force_channel_map.num)
212         mp_aframe_set_chmap(mpframe, &priv->force_channel_map);
213 
214     if (out_pts == MP_NOPTS_VALUE)
215         out_pts = priv->next_pts;
216     mp_aframe_set_pts(mpframe, out_pts);
217 
218     priv->next_pts = mp_aframe_end_pts(mpframe);
219 
220     AVFrameSideData *sd =
221         av_frame_get_side_data(priv->avframe, AV_FRAME_DATA_SKIP_SAMPLES);
222     if (sd && sd->size >= 10) {
223         char *d = sd->data;
224         priv->skip_samples += AV_RL32(d + 0);
225         priv->trim_samples += AV_RL32(d + 4);
226     }
227 
228     if (!priv->preroll_done) {
229         // Skip only if this isn't already handled by AV_FRAME_DATA_SKIP_SAMPLES.
230         if (!priv->skip_samples)
231             priv->skip_samples = avctx->delay;
232         priv->preroll_done = true;
233     }
234 
235     uint32_t skip = MPMIN(priv->skip_samples, mp_aframe_get_size(mpframe));
236     if (skip) {
237         mp_aframe_skip_samples(mpframe, skip);
238         priv->skip_samples -= skip;
239     }
240     uint32_t trim = MPMIN(priv->trim_samples, mp_aframe_get_size(mpframe));
241     if (trim) {
242         mp_aframe_set_size(mpframe, mp_aframe_get_size(mpframe) - trim);
243         priv->trim_samples -= trim;
244     }
245 
246     if (mp_aframe_get_size(mpframe) > 0) {
247         *out = MAKE_FRAME(MP_FRAME_AUDIO, mpframe);
248     } else {
249         talloc_free(mpframe);
250     }
251 
252     av_frame_unref(priv->avframe);
253 
254     return ret;
255 }
256 
process(struct mp_filter * ad)257 static void process(struct mp_filter *ad)
258 {
259     struct priv *priv = ad->priv;
260 
261     lavc_process(ad, &priv->state, send_packet, receive_frame);
262 }
263 
264 static const struct mp_filter_info ad_lavc_filter = {
265     .name = "ad_lavc",
266     .priv_size = sizeof(struct priv),
267     .process = process,
268     .reset = reset,
269     .destroy = destroy,
270 };
271 
create(struct mp_filter * parent,struct mp_codec_params * codec,const char * decoder)272 static struct mp_decoder *create(struct mp_filter *parent,
273                                  struct mp_codec_params *codec,
274                                  const char *decoder)
275 {
276     struct mp_filter *da = mp_filter_create(parent, &ad_lavc_filter);
277     if (!da)
278         return NULL;
279 
280     mp_filter_add_pin(da, MP_PIN_IN, "in");
281     mp_filter_add_pin(da, MP_PIN_OUT, "out");
282 
283     da->log = mp_log_new(da, parent->log, NULL);
284 
285     struct priv *priv = da->priv;
286     priv->public.f = da;
287 
288     if (!init(da, codec, decoder)) {
289         talloc_free(da);
290         return NULL;
291     }
292     return &priv->public;
293 }
294 
add_decoders(struct mp_decoder_list * list)295 static void add_decoders(struct mp_decoder_list *list)
296 {
297     mp_add_lavc_decoders(list, AVMEDIA_TYPE_AUDIO);
298 }
299 
300 const struct mp_decoder_fns ad_lavc = {
301     .create = create,
302     .add_decoders = add_decoders,
303 };
304