FFmpeg  4.3.9
f_interleave.c
Go to the documentation of this file.
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 "libavutil/avassert.h"
27 #include "libavutil/avstring.h"
28 #include "libavutil/opt.h"
29 
30 #include "avfilter.h"
31 #include "formats.h"
32 #include "filters.h"
33 #include "internal.h"
34 #include "audio.h"
35 #include "video.h"
36 
37 typedef struct InterleaveContext {
38  const AVClass *class;
39  int nb_inputs;
43 
44 #define DURATION_LONGEST 0
45 #define DURATION_SHORTEST 1
46 #define DURATION_FIRST 2
47 
48 #define OFFSET(x) offsetof(InterleaveContext, x)
49 
50 #define DEFINE_OPTIONS(filt_name, flags_) \
51 static const AVOption filt_name##_options[] = { \
52  { "nb_inputs", "set number of inputs", OFFSET(nb_inputs), AV_OPT_TYPE_INT, {.i64 = 2}, 1, INT_MAX, .flags = flags_ }, \
53  { "n", "set number of inputs", OFFSET(nb_inputs), AV_OPT_TYPE_INT, {.i64 = 2}, 1, INT_MAX, .flags = flags_ }, \
54  { "duration", "how to determine the end-of-stream", \
55  OFFSET(duration_mode), AV_OPT_TYPE_INT, { .i64 = DURATION_LONGEST }, 0, 2, flags_, "duration" }, \
56  { "longest", "Duration of longest input", 0, AV_OPT_TYPE_CONST, { .i64 = DURATION_LONGEST }, 0, 0, flags_, "duration" }, \
57  { "shortest", "Duration of shortest input", 0, AV_OPT_TYPE_CONST, { .i64 = DURATION_SHORTEST }, 0, 0, flags_, "duration" }, \
58  { "first", "Duration of first input", 0, AV_OPT_TYPE_CONST, { .i64 = DURATION_FIRST }, 0, 0, flags_, "duration" }, \
59  { NULL } \
60 }
61 
63 {
64  AVFilterLink *outlink = ctx->outputs[0];
65  InterleaveContext *s = ctx->priv;
66  int64_t q_pts, pts = INT64_MAX;
67  int i, nb_eofs = 0, input_idx = -1;
68  int nb_inputs_with_frames = 0;
69 
71 
72  for (i = 0; i < ctx->nb_inputs; i++) {
73  if (!ff_inlink_queued_frames(ctx->inputs[i]))
74  continue;
75  nb_inputs_with_frames++;
76  }
77 
78  if (nb_inputs_with_frames > 0) {
79  for (i = 0; i < ctx->nb_inputs; i++) {
80  AVFrame *frame;
81 
82  if (ff_inlink_queued_frames(ctx->inputs[i]) == 0)
83  continue;
84 
85  frame = ff_inlink_peek_frame(ctx->inputs[i], 0);
86  if (frame->pts == AV_NOPTS_VALUE) {
87  int ret;
88 
90  "NOPTS value for input frame cannot be accepted, frame discarded\n");
91  ret = ff_inlink_consume_frame(ctx->inputs[i], &frame);
92  if (ret < 0)
93  return ret;
94  av_frame_free(&frame);
95  return AVERROR_INVALIDDATA;
96  }
97 
98  q_pts = av_rescale_q(frame->pts, ctx->inputs[i]->time_base, AV_TIME_BASE_Q);
99  if (q_pts < pts) {
100  pts = q_pts;
101  input_idx = i;
102  }
103  }
104 
105  if (input_idx >= 0) {
106  AVFrame *frame;
107  int ret;
108 
109  ret = ff_inlink_consume_frame(ctx->inputs[input_idx], &frame);
110  if (ret < 0)
111  return ret;
112 
113  frame->pts = s->pts = pts;
114  return ff_filter_frame(outlink, frame);
115  }
116  }
117 
118  for (i = 0; i < ctx->nb_inputs; i++)
119  nb_eofs += !!ff_outlink_get_status(ctx->inputs[i]);
120 
121  if ((nb_eofs > 0 && s->duration_mode == DURATION_SHORTEST) ||
122  (nb_eofs == ctx->nb_inputs && s->duration_mode == DURATION_LONGEST) ||
124  ff_outlink_set_status(outlink, AVERROR_EOF, s->pts);
125  return 0;
126  }
127 
128  for (i = 0; i < ctx->nb_inputs; i++) {
129  if (ff_inlink_queued_frames(ctx->inputs[i]))
130  continue;
131  if (ff_outlink_frame_wanted(outlink) &&
132  !ff_outlink_get_status(ctx->inputs[i])) {
134  return 0;
135  }
136  }
137 
138  if (i) {
139  ff_filter_set_ready(ctx, 100);
140  return 0;
141  }
142 
143  return FFERROR_NOT_READY;
144 }
145 
147 {
148  InterleaveContext *s = ctx->priv;
149  const AVFilterPad *outpad = &ctx->filter->outputs[0];
150  int i, ret;
151 
152  for (i = 0; i < s->nb_inputs; i++) {
153  AVFilterPad inpad = { 0 };
154 
155  inpad.name = av_asprintf("input%d", i);
156  if (!inpad.name)
157  return AVERROR(ENOMEM);
158  inpad.type = outpad->type;
159 
160  switch (outpad->type) {
161  case AVMEDIA_TYPE_VIDEO:
163  case AVMEDIA_TYPE_AUDIO:
165  default:
166  av_assert0(0);
167  }
168  if ((ret = ff_insert_inpad(ctx, i, &inpad)) < 0) {
169  av_freep(&inpad.name);
170  return ret;
171  }
172  }
173 
174  return 0;
175 }
176 
178 {
179  for (int i = 0; i < ctx->nb_inputs; i++)
180  av_freep(&ctx->input_pads[i].name);
181 }
182 
183 static int config_output(AVFilterLink *outlink)
184 {
185  AVFilterContext *ctx = outlink->src;
186  AVFilterLink *inlink0 = ctx->inputs[0];
187  int i;
188 
189  if (outlink->type == AVMEDIA_TYPE_VIDEO) {
190  outlink->time_base = AV_TIME_BASE_Q;
191  outlink->w = inlink0->w;
192  outlink->h = inlink0->h;
193  outlink->sample_aspect_ratio = inlink0->sample_aspect_ratio;
194  outlink->format = inlink0->format;
195  outlink->frame_rate = (AVRational) {1, 0};
196  for (i = 1; i < ctx->nb_inputs; i++) {
197  AVFilterLink *inlink = ctx->inputs[i];
198 
199  if (outlink->w != inlink->w ||
200  outlink->h != inlink->h ||
201  outlink->sample_aspect_ratio.num != inlink->sample_aspect_ratio.num ||
202  outlink->sample_aspect_ratio.den != inlink->sample_aspect_ratio.den) {
203  av_log(ctx, AV_LOG_ERROR, "Parameters for input link %s "
204  "(size %dx%d, SAR %d:%d) do not match the corresponding "
205  "output link parameters (%dx%d, SAR %d:%d)\n",
206  ctx->input_pads[i].name, inlink->w, inlink->h,
207  inlink->sample_aspect_ratio.num,
208  inlink->sample_aspect_ratio.den,
209  outlink->w, outlink->h,
210  outlink->sample_aspect_ratio.num,
211  outlink->sample_aspect_ratio.den);
212  return AVERROR(EINVAL);
213  }
214  }
215  }
216  return 0;
217 }
218 
219 #if CONFIG_INTERLEAVE_FILTER
220 
223 
224 static const AVFilterPad interleave_outputs[] = {
225  {
226  .name = "default",
227  .type = AVMEDIA_TYPE_VIDEO,
228  .config_props = config_output,
229  },
230  { NULL }
231 };
232 
234  .name = "interleave",
235  .description = NULL_IF_CONFIG_SMALL("Temporally interleave video inputs."),
236  .priv_size = sizeof(InterleaveContext),
237  .init = init,
238  .uninit = uninit,
239  .activate = activate,
240  .outputs = interleave_outputs,
241  .priv_class = &interleave_class,
243 };
244 
245 #endif
246 
247 #if CONFIG_AINTERLEAVE_FILTER
248 
250 AVFILTER_DEFINE_CLASS(ainterleave);
251 
252 static const AVFilterPad ainterleave_outputs[] = {
253  {
254  .name = "default",
255  .type = AVMEDIA_TYPE_AUDIO,
256  .config_props = config_output,
257  },
258  { NULL }
259 };
260 
262  .name = "ainterleave",
263  .description = NULL_IF_CONFIG_SMALL("Temporally interleave audio inputs."),
264  .priv_size = sizeof(InterleaveContext),
265  .init = init,
266  .uninit = uninit,
267  .activate = activate,
268  .outputs = ainterleave_outputs,
269  .priv_class = &ainterleave_class,
271 };
272 
273 #endif
int ff_inlink_consume_frame(AVFilterLink *link, AVFrame **rframe)
Take a frame from the link&#39;s FIFO and update the link&#39;s stats.
Definition: avfilter.c:1476
#define NULL
Definition: coverity.c:32
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
This structure describes decoded (raw) audio or video data.
Definition: frame.h:300
static int activate(AVFilterContext *ctx)
Definition: f_interleave.c:62
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:182
#define FF_FILTER_FORWARD_STATUS_BACK_ALL(outlink, filter)
Forward the status on an output link to all input links.
Definition: filters.h:212
Main libavfilter public API header.
#define DURATION_FIRST
Definition: f_interleave.c:46
#define AVFILTER_FLAG_DYNAMIC_INPUTS
The number of the filter inputs is not determined just by AVFilter.inputs.
Definition: avfilter.h:105
int num
Numerator.
Definition: rational.h:59
#define AV_OPT_FLAG_AUDIO_PARAM
Definition: opt.h:278
AVFrame * ff_null_get_video_buffer(AVFilterLink *link, int w, int h)
Definition: video.c:39
AVFilter ff_af_ainterleave
#define FFERROR_NOT_READY
Filters implementation helper functions.
Definition: filters.h:34
enum AVMediaType type
AVFilterPad type.
Definition: internal.h:65
static int config_output(AVFilterLink *outlink)
Definition: f_interleave.c:183
static void ff_outlink_set_status(AVFilterLink *link, int status, int64_t pts)
Set the status field of a link from the source filter.
Definition: filters.h:189
void ff_inlink_request_frame(AVFilterLink *link)
Mark that a frame is wanted on the link.
Definition: avfilter.c:1602
static int ff_outlink_frame_wanted(AVFilterLink *link)
Test if a frame is wanted on an output link.
Definition: filters.h:172
const char * name
Pad name.
Definition: internal.h:60
AVFilterLink ** inputs
array of pointers to input links
Definition: avfilter.h:346
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1075
AVFilter ff_vf_interleave
#define av_cold
Definition: attributes.h:88
AVOptions.
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition: frame.h:393
static AVFrame * frame
#define AVERROR_EOF
End of file.
Definition: error.h:55
#define av_log(a,...)
#define DEFINE_OPTIONS(filt_name, flags_)
Definition: f_interleave.c:50
A filter pad used for either input or output.
Definition: internal.h:54
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
Definition: mathematics.c:142
AVFilterPad * input_pads
array of input pads
Definition: avfilter.h:345
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:269
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
#define AV_OPT_FLAG_FILTERING_PARAM
a generic parameter which can be set by the user for filtering
Definition: opt.h:292
#define AVERROR(e)
Definition: error.h:43
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:203
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:188
void * priv
private data for use by the filter
Definition: avfilter.h:353
simple assert() macros that are a bit more flexible than ISO C assert().
char * av_asprintf(const char *fmt,...)
Definition: avstring.c:113
AVFrame *(* get_video_buffer)(AVFilterLink *link, int w, int h)
Callback function to get a video buffer.
Definition: internal.h:73
unsigned nb_inputs
number of input pads
Definition: avfilter.h:347
static av_cold void uninit(AVFilterContext *ctx)
Definition: f_interleave.c:177
AVFormatContext * ctx
Definition: movenc.c:48
#define DURATION_SHORTEST
Definition: f_interleave.c:45
#define s(width, name)
Definition: cbs_vp9.c:257
static av_cold int init(AVFilterContext *ctx)
Definition: f_interleave.c:146
AVFrame * ff_null_get_audio_buffer(AVFilterLink *link, int nb_samples)
get_audio_buffer() handler for filters which simply pass audio along
Definition: audio.c:33
AVFrame * ff_inlink_peek_frame(AVFilterLink *link, size_t idx)
Access a frame in the link fifo without consuming it.
Definition: avfilter.c:1515
static const AVFilterPad outputs[]
Definition: af_acontrast.c:203
#define AV_TIME_BASE_Q
Internal time base represented as fractional value.
Definition: avutil.h:260
#define AV_OPT_FLAG_VIDEO_PARAM
Definition: opt.h:279
long long int64_t
Definition: coverity.c:34
static av_always_inline void RENAME() interleave(TYPE *dst, TYPE *src0, TYPE *src1, int w2, int add, int shift)
AVFrame *(* get_audio_buffer)(AVFilterLink *link, int nb_samples)
Callback function to get an audio buffer.
Definition: internal.h:81
Describe the class of an AVClass context structure.
Definition: log.h:67
Filter definition.
Definition: avfilter.h:144
int ff_outlink_get_status(AVFilterLink *link)
Get the status on an output link.
Definition: avfilter.c:1625
Rational number (pair of numerator and denominator).
Definition: rational.h:58
const char * name
Filter name.
Definition: avfilter.h:148
size_t ff_inlink_queued_frames(AVFilterLink *link)
Get the number of frames available on the link.
Definition: avfilter.c:1446
AVFilterLink ** outputs
array of pointers to output links
Definition: avfilter.h:350
#define flags(name, subs,...)
Definition: cbs_av1.c:576
#define DURATION_LONGEST
Definition: f_interleave.c:44
void ff_filter_set_ready(AVFilterContext *filter, unsigned priority)
Mark a filter ready and schedule it for activation.
Definition: avfilter.c:193
int den
Denominator.
Definition: rational.h:60
#define AVFILTER_DEFINE_CLASS(fname)
Definition: internal.h:314
An instance of a filter.
Definition: avfilter.h:338
const AVFilterPad * outputs
List of outputs, terminated by a zeroed element.
Definition: avfilter.h:172
#define av_freep(p)
internal API functions
const AVFilter * filter
the AVFilter of which this is an instance
Definition: avfilter.h:341
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:248
static int ff_insert_inpad(AVFilterContext *f, unsigned index, AVFilterPad *p)
Insert a new input pad for the filter.
Definition: internal.h:266