FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/f_interleave.c
Date: 2024-03-28 14:59:00
Exec Total Coverage
Lines: 0 94 0.0%
Functions: 0 3 0.0%
Branches: 0 73 0.0%

Line Branch Exec Source
1 /*
2 * Copyright (c) 2013 Stefano Sabatini
3 *
4 * This file is part of FFmpeg.
5 *
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21 /**
22 * @file
23 * audio and video interleaver
24 */
25
26 #include "config_components.h"
27
28 #include "libavutil/avassert.h"
29 #include "libavutil/avstring.h"
30 #include "libavutil/opt.h"
31
32 #include "avfilter.h"
33 #include "filters.h"
34 #include "internal.h"
35 #include "audio.h"
36 #include "video.h"
37
38 typedef struct InterleaveContext {
39 const AVClass *class;
40 int nb_inputs;
41 int duration_mode;
42 int64_t pts;
43 } InterleaveContext;
44
45 #define DURATION_LONGEST 0
46 #define DURATION_SHORTEST 1
47 #define DURATION_FIRST 2
48
49 #define OFFSET(x) offsetof(InterleaveContext, x)
50
51 #define DEFINE_OPTIONS(filt_name, flags_) \
52 static const AVOption filt_name##_options[] = { \
53 { "nb_inputs", "set number of inputs", OFFSET(nb_inputs), AV_OPT_TYPE_INT, {.i64 = 2}, 1, INT_MAX, .flags = flags_ }, \
54 { "n", "set number of inputs", OFFSET(nb_inputs), AV_OPT_TYPE_INT, {.i64 = 2}, 1, INT_MAX, .flags = flags_ }, \
55 { "duration", "how to determine the end-of-stream", \
56 OFFSET(duration_mode), AV_OPT_TYPE_INT, { .i64 = DURATION_LONGEST }, 0, 2, flags_, .unit = "duration" }, \
57 { "longest", "Duration of longest input", 0, AV_OPT_TYPE_CONST, { .i64 = DURATION_LONGEST }, 0, 0, flags_, .unit = "duration" }, \
58 { "shortest", "Duration of shortest input", 0, AV_OPT_TYPE_CONST, { .i64 = DURATION_SHORTEST }, 0, 0, flags_, .unit = "duration" }, \
59 { "first", "Duration of first input", 0, AV_OPT_TYPE_CONST, { .i64 = DURATION_FIRST }, 0, 0, flags_, .unit = "duration" }, \
60 { NULL } \
61 }
62
63 static int activate(AVFilterContext *ctx)
64 {
65 AVFilterLink *outlink = ctx->outputs[0];
66 InterleaveContext *s = ctx->priv;
67 int64_t q_pts, pts = INT64_MAX;
68 int i, nb_eofs = 0, input_idx = -1;
69 int first_eof = 0;
70 int64_t rpts;
71 int status;
72 int nb_inputs_with_frames = 0;
73
74 FF_FILTER_FORWARD_STATUS_BACK_ALL(outlink, ctx);
75
76 for (i = 0; i < ctx->nb_inputs; i++) {
77 int is_eof = !!ff_inlink_acknowledge_status(ctx->inputs[i], &status, &rpts);
78
79 nb_eofs += is_eof;
80 if (i == 0)
81 first_eof = is_eof;
82 }
83
84 if ((nb_eofs > 0 && s->duration_mode == DURATION_SHORTEST) ||
85 (nb_eofs == ctx->nb_inputs && s->duration_mode == DURATION_LONGEST) ||
86 (first_eof && s->duration_mode == DURATION_FIRST)) {
87 ff_outlink_set_status(outlink, AVERROR_EOF, s->pts);
88 return 0;
89 }
90
91 for (i = 0; i < ctx->nb_inputs; i++) {
92 if (!ff_inlink_queued_frames(ctx->inputs[i]))
93 continue;
94 nb_inputs_with_frames++;
95 }
96
97 if (nb_inputs_with_frames >= ctx->nb_inputs - nb_eofs) {
98 for (i = 0; i < ctx->nb_inputs; i++) {
99 AVFrame *frame;
100
101 if (ff_inlink_queued_frames(ctx->inputs[i]) == 0)
102 continue;
103
104 frame = ff_inlink_peek_frame(ctx->inputs[i], 0);
105 if (frame->pts == AV_NOPTS_VALUE) {
106 int ret;
107
108 av_log(ctx, AV_LOG_WARNING,
109 "NOPTS value for input frame cannot be accepted, frame discarded\n");
110 ret = ff_inlink_consume_frame(ctx->inputs[i], &frame);
111 if (ret < 0)
112 return ret;
113 av_frame_free(&frame);
114 return AVERROR_INVALIDDATA;
115 }
116
117 q_pts = av_rescale_q(frame->pts, ctx->inputs[i]->time_base, AV_TIME_BASE_Q);
118 if (q_pts < pts) {
119 pts = q_pts;
120 input_idx = i;
121 }
122 }
123
124 if (input_idx >= 0) {
125 AVFrame *frame;
126 int ret;
127
128 ret = ff_inlink_consume_frame(ctx->inputs[input_idx], &frame);
129 if (ret < 0)
130 return ret;
131
132 frame->pts = s->pts = pts;
133 return ff_filter_frame(outlink, frame);
134 }
135 }
136
137 for (i = 0; i < ctx->nb_inputs; i++) {
138 if (ff_inlink_queued_frames(ctx->inputs[i]))
139 continue;
140 if (ff_outlink_frame_wanted(outlink) &&
141 !ff_outlink_get_status(ctx->inputs[i])) {
142 ff_inlink_request_frame(ctx->inputs[i]);
143 return 0;
144 }
145 }
146
147 if (i == ctx->nb_inputs - nb_eofs && ff_outlink_frame_wanted(outlink)) {
148 ff_filter_set_ready(ctx, 100);
149 return 0;
150 }
151
152 return FFERROR_NOT_READY;
153 }
154
155 static av_cold int init(AVFilterContext *ctx)
156 {
157 InterleaveContext *s = ctx->priv;
158 const AVFilterPad *outpad = &ctx->filter->outputs[0];
159 int i, ret;
160
161 for (i = 0; i < s->nb_inputs; i++) {
162 AVFilterPad inpad = { 0 };
163
164 inpad.name = av_asprintf("input%d", i);
165 if (!inpad.name)
166 return AVERROR(ENOMEM);
167 inpad.type = outpad->type;
168
169 switch (outpad->type) {
170 case AVMEDIA_TYPE_VIDEO:
171 inpad.get_buffer.video = ff_null_get_video_buffer; break;
172 case AVMEDIA_TYPE_AUDIO:
173 inpad.get_buffer.audio = ff_null_get_audio_buffer; break;
174 default:
175 av_assert0(0);
176 }
177 if ((ret = ff_append_inpad_free_name(ctx, &inpad)) < 0)
178 return ret;
179 }
180
181 return 0;
182 }
183
184 static int config_output(AVFilterLink *outlink)
185 {
186 AVFilterContext *ctx = outlink->src;
187 AVFilterLink *inlink0 = ctx->inputs[0];
188 int i;
189
190 if (outlink->type == AVMEDIA_TYPE_VIDEO) {
191 outlink->time_base = AV_TIME_BASE_Q;
192 outlink->w = inlink0->w;
193 outlink->h = inlink0->h;
194 outlink->sample_aspect_ratio = inlink0->sample_aspect_ratio;
195 outlink->format = inlink0->format;
196 outlink->frame_rate = (AVRational) {1, 0};
197 for (i = 1; i < ctx->nb_inputs; i++) {
198 AVFilterLink *inlink = ctx->inputs[i];
199
200 if (outlink->w != inlink->w ||
201 outlink->h != inlink->h ||
202 outlink->sample_aspect_ratio.num != inlink->sample_aspect_ratio.num ||
203 outlink->sample_aspect_ratio.den != inlink->sample_aspect_ratio.den) {
204 av_log(ctx, AV_LOG_ERROR, "Parameters for input link %s "
205 "(size %dx%d, SAR %d:%d) do not match the corresponding "
206 "output link parameters (%dx%d, SAR %d:%d)\n",
207 ctx->input_pads[i].name, inlink->w, inlink->h,
208 inlink->sample_aspect_ratio.num,
209 inlink->sample_aspect_ratio.den,
210 outlink->w, outlink->h,
211 outlink->sample_aspect_ratio.num,
212 outlink->sample_aspect_ratio.den);
213 return AVERROR(EINVAL);
214 }
215 }
216 }
217 return 0;
218 }
219
220 #if CONFIG_INTERLEAVE_FILTER
221
222 DEFINE_OPTIONS(interleave, AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM);
223 AVFILTER_DEFINE_CLASS(interleave);
224
225 static const AVFilterPad interleave_outputs[] = {
226 {
227 .name = "default",
228 .type = AVMEDIA_TYPE_VIDEO,
229 .config_props = config_output,
230 },
231 };
232
233 const AVFilter ff_vf_interleave = {
234 .name = "interleave",
235 .description = NULL_IF_CONFIG_SMALL("Temporally interleave video inputs."),
236 .priv_size = sizeof(InterleaveContext),
237 .init = init,
238 .activate = activate,
239 FILTER_OUTPUTS(interleave_outputs),
240 .priv_class = &interleave_class,
241 .flags = AVFILTER_FLAG_DYNAMIC_INPUTS,
242 };
243
244 #endif
245
246 #if CONFIG_AINTERLEAVE_FILTER
247
248 DEFINE_OPTIONS(ainterleave, AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM);
249 AVFILTER_DEFINE_CLASS(ainterleave);
250
251 static const AVFilterPad ainterleave_outputs[] = {
252 {
253 .name = "default",
254 .type = AVMEDIA_TYPE_AUDIO,
255 .config_props = config_output,
256 },
257 };
258
259 const AVFilter ff_af_ainterleave = {
260 .name = "ainterleave",
261 .description = NULL_IF_CONFIG_SMALL("Temporally interleave audio inputs."),
262 .priv_size = sizeof(InterleaveContext),
263 .init = init,
264 .activate = activate,
265 FILTER_OUTPUTS(ainterleave_outputs),
266 .priv_class = &ainterleave_class,
267 .flags = AVFILTER_FLAG_DYNAMIC_INPUTS,
268 };
269
270 #endif
271