FFmpeg  4.2.1
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;
40  int64_t pts;
42 
43 #define OFFSET(x) offsetof(InterleaveContext, x)
44 
45 #define DEFINE_OPTIONS(filt_name, flags_) \
46 static const AVOption filt_name##_options[] = { \
47  { "nb_inputs", "set number of inputs", OFFSET(nb_inputs), AV_OPT_TYPE_INT, {.i64 = 2}, 1, INT_MAX, .flags = flags_ }, \
48  { "n", "set number of inputs", OFFSET(nb_inputs), AV_OPT_TYPE_INT, {.i64 = 2}, 1, INT_MAX, .flags = flags_ }, \
49  { NULL } \
50 }
51 
53 {
54  AVFilterLink *outlink = ctx->outputs[0];
55  InterleaveContext *s = ctx->priv;
56  int64_t q_pts, pts = INT64_MAX;
57  int i, nb_eofs = 0, input_idx = -1;
58 
60 
61  for (i = 0; i < ctx->nb_inputs; i++) {
62  if (!ff_outlink_get_status(ctx->inputs[i]) &&
64  break;
65  }
66 
67  if (i == ctx->nb_inputs) {
68  for (i = 0; i < ctx->nb_inputs; i++) {
69  AVFrame *frame;
70 
71  if (ff_outlink_get_status(ctx->inputs[i]))
72  continue;
73 
74  frame = ff_inlink_peek_frame(ctx->inputs[i], 0);
75  if (frame->pts == AV_NOPTS_VALUE) {
76  int ret;
77 
79  "NOPTS value for input frame cannot be accepted, frame discarded\n");
80  ret = ff_inlink_consume_frame(ctx->inputs[i], &frame);
81  if (ret < 0)
82  return ret;
83  av_frame_free(&frame);
84  return AVERROR_INVALIDDATA;
85  }
86 
87  q_pts = av_rescale_q(frame->pts, ctx->inputs[i]->time_base, AV_TIME_BASE_Q);
88  if (q_pts < pts) {
89  pts = q_pts;
90  input_idx = i;
91  }
92  }
93 
94  if (input_idx >= 0) {
95  AVFrame *frame;
96  int ret;
97 
98  ret = ff_inlink_consume_frame(ctx->inputs[input_idx], &frame);
99  if (ret < 0)
100  return ret;
101 
102  frame->pts = s->pts = pts;
103  return ff_filter_frame(outlink, frame);
104  }
105  }
106 
107  for (i = 0; i < ctx->nb_inputs; i++) {
108  if (ff_inlink_queued_frames(ctx->inputs[i]))
109  continue;
110  if (ff_outlink_frame_wanted(outlink) &&
111  !ff_outlink_get_status(ctx->inputs[i])) {
113  return 0;
114  }
115  nb_eofs++;
116  }
117 
118  if (nb_eofs == ctx->nb_inputs) {
119  ff_outlink_set_status(outlink, AVERROR_EOF, s->pts);
120  return 0;
121  }
122 
123  return FFERROR_NOT_READY;
124 }
125 
127 {
128  InterleaveContext *s = ctx->priv;
129  const AVFilterPad *outpad = &ctx->filter->outputs[0];
130  int i, ret;
131 
132  for (i = 0; i < s->nb_inputs; i++) {
133  AVFilterPad inpad = { 0 };
134 
135  inpad.name = av_asprintf("input%d", i);
136  if (!inpad.name)
137  return AVERROR(ENOMEM);
138  inpad.type = outpad->type;
139 
140  switch (outpad->type) {
141  case AVMEDIA_TYPE_VIDEO:
143  case AVMEDIA_TYPE_AUDIO:
145  default:
146  av_assert0(0);
147  }
148  if ((ret = ff_insert_inpad(ctx, i, &inpad)) < 0) {
149  av_freep(&inpad.name);
150  return ret;
151  }
152  }
153 
154  return 0;
155 }
156 
158 {
159  for (int i = 0; i < ctx->nb_inputs; i++)
160  av_freep(&ctx->input_pads[i].name);
161 }
162 
163 static int config_output(AVFilterLink *outlink)
164 {
165  AVFilterContext *ctx = outlink->src;
166  AVFilterLink *inlink0 = ctx->inputs[0];
167  int i;
168 
169  if (outlink->type == AVMEDIA_TYPE_VIDEO) {
170  outlink->time_base = AV_TIME_BASE_Q;
171  outlink->w = inlink0->w;
172  outlink->h = inlink0->h;
173  outlink->sample_aspect_ratio = inlink0->sample_aspect_ratio;
174  outlink->format = inlink0->format;
175  outlink->frame_rate = (AVRational) {1, 0};
176  for (i = 1; i < ctx->nb_inputs; i++) {
177  AVFilterLink *inlink = ctx->inputs[i];
178 
179  if (outlink->w != inlink->w ||
180  outlink->h != inlink->h ||
181  outlink->sample_aspect_ratio.num != inlink->sample_aspect_ratio.num ||
182  outlink->sample_aspect_ratio.den != inlink->sample_aspect_ratio.den) {
183  av_log(ctx, AV_LOG_ERROR, "Parameters for input link %s "
184  "(size %dx%d, SAR %d:%d) do not match the corresponding "
185  "output link parameters (%dx%d, SAR %d:%d)\n",
186  ctx->input_pads[i].name, inlink->w, inlink->h,
187  inlink->sample_aspect_ratio.num,
188  inlink->sample_aspect_ratio.den,
189  outlink->w, outlink->h,
190  outlink->sample_aspect_ratio.num,
191  outlink->sample_aspect_ratio.den);
192  return AVERROR(EINVAL);
193  }
194  }
195  }
196  return 0;
197 }
198 
199 #if CONFIG_INTERLEAVE_FILTER
200 
203 
204 static const AVFilterPad interleave_outputs[] = {
205  {
206  .name = "default",
207  .type = AVMEDIA_TYPE_VIDEO,
208  .config_props = config_output,
209  },
210  { NULL }
211 };
212 
214  .name = "interleave",
215  .description = NULL_IF_CONFIG_SMALL("Temporally interleave video inputs."),
216  .priv_size = sizeof(InterleaveContext),
217  .init = init,
218  .uninit = uninit,
219  .activate = activate,
220  .outputs = interleave_outputs,
221  .priv_class = &interleave_class,
223 };
224 
225 #endif
226 
227 #if CONFIG_AINTERLEAVE_FILTER
228 
230 AVFILTER_DEFINE_CLASS(ainterleave);
231 
232 static const AVFilterPad ainterleave_outputs[] = {
233  {
234  .name = "default",
235  .type = AVMEDIA_TYPE_AUDIO,
236  .config_props = config_output,
237  },
238  { NULL }
239 };
240 
242  .name = "ainterleave",
243  .description = NULL_IF_CONFIG_SMALL("Temporally interleave audio inputs."),
244  .priv_size = sizeof(InterleaveContext),
245  .init = init,
246  .uninit = uninit,
247  .activate = activate,
248  .outputs = ainterleave_outputs,
249  .priv_class = &ainterleave_class,
251 };
252 
253 #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:1481
#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:295
static int activate(AVFilterContext *ctx)
Definition: f_interleave.c:52
#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 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:163
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:1607
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:1080
AVFilter ff_vf_interleave
#define av_cold
Definition: attributes.h:82
AVOptions.
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition: frame.h:388
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:45
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:259
#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:291
#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:202
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:186
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:157
AVFormatContext * ctx
Definition: movenc.c:48
#define s(width, name)
Definition: cbs_vp9.c:257
static av_cold int init(AVFilterContext *ctx)
Definition: f_interleave.c:126
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:1520
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
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:1630
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:1451
AVFilterLink ** outputs
array of pointers to output links
Definition: avfilter.h:350
#define flags(name, subs,...)
Definition: cbs_av1.c:561
int den
Denominator.
Definition: rational.h:60
#define AVFILTER_DEFINE_CLASS(fname)
Definition: internal.h:334
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:277