FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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 #define FF_INTERNAL_FIELDS 1
31 #include "framequeue.h"
32 
33 #include "avfilter.h"
34 #include "bufferqueue.h"
35 #include "formats.h"
36 #include "internal.h"
37 #include "audio.h"
38 #include "video.h"
39 
40 typedef struct InterleaveContext {
41  const AVClass *class;
42  int nb_inputs;
43  struct FFBufQueue *queues;
45 
46 #define OFFSET(x) offsetof(InterleaveContext, x)
47 
48 #define DEFINE_OPTIONS(filt_name, flags_) \
49 static const AVOption filt_name##_options[] = { \
50  { "nb_inputs", "set number of inputs", OFFSET(nb_inputs), AV_OPT_TYPE_INT, {.i64 = 2}, 1, INT_MAX, .flags = flags_ }, \
51  { "n", "set number of inputs", OFFSET(nb_inputs), AV_OPT_TYPE_INT, {.i64 = 2}, 1, INT_MAX, .flags = flags_ }, \
52  { NULL } \
53 }
54 
55 inline static int push_frame(AVFilterContext *ctx)
56 {
57  InterleaveContext *s = ctx->priv;
58  AVFrame *frame;
59  int i, queue_idx = -1;
60  int64_t pts_min = INT64_MAX;
61 
62  /* look for oldest frame */
63  for (i = 0; i < ctx->nb_inputs; i++) {
64  struct FFBufQueue *q = &s->queues[i];
65 
66  if (!q->available && !ctx->inputs[i]->status_out)
67  return 0;
68  if (q->available) {
69  frame = ff_bufqueue_peek(q, 0);
70  if (frame->pts < pts_min) {
71  pts_min = frame->pts;
72  queue_idx = i;
73  }
74  }
75  }
76 
77  /* all inputs are closed */
78  if (queue_idx < 0)
79  return AVERROR_EOF;
80 
81  frame = ff_bufqueue_get(&s->queues[queue_idx]);
82  av_log(ctx, AV_LOG_DEBUG, "queue:%d -> frame time:%f\n",
83  queue_idx, frame->pts * av_q2d(AV_TIME_BASE_Q));
84  return ff_filter_frame(ctx->outputs[0], frame);
85 }
86 
87 static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
88 {
89  AVFilterContext *ctx = inlink->dst;
90  InterleaveContext *s = ctx->priv;
91  unsigned in_no = FF_INLINK_IDX(inlink);
92 
93  if (frame->pts == AV_NOPTS_VALUE) {
95  "NOPTS value for input frame cannot be accepted, frame discarded\n");
96  av_frame_free(&frame);
97  return AVERROR_INVALIDDATA;
98  }
99 
100  /* queue frame */
101  frame->pts = av_rescale_q(frame->pts, inlink->time_base, AV_TIME_BASE_Q);
102  av_log(ctx, AV_LOG_DEBUG, "frame pts:%f -> queue idx:%d available:%d\n",
103  frame->pts * av_q2d(AV_TIME_BASE_Q), in_no, s->queues[in_no].available);
104  ff_bufqueue_add(ctx, &s->queues[in_no], frame);
105 
106  return push_frame(ctx);
107 }
108 
110 {
111  InterleaveContext *s = ctx->priv;
112  const AVFilterPad *outpad = &ctx->filter->outputs[0];
113  int i, ret;
114 
115  s->queues = av_calloc(s->nb_inputs, sizeof(s->queues[0]));
116  if (!s->queues)
117  return AVERROR(ENOMEM);
118 
119  for (i = 0; i < s->nb_inputs; i++) {
120  AVFilterPad inpad = { 0 };
121 
122  inpad.name = av_asprintf("input%d", i);
123  if (!inpad.name)
124  return AVERROR(ENOMEM);
125  inpad.type = outpad->type;
126  inpad.filter_frame = filter_frame;
127 
128  switch (outpad->type) {
129  case AVMEDIA_TYPE_VIDEO:
131  case AVMEDIA_TYPE_AUDIO:
133  default:
134  av_assert0(0);
135  }
136  if ((ret = ff_insert_inpad(ctx, i, &inpad)) < 0) {
137  av_freep(&inpad.name);
138  return ret;
139  }
140  }
141 
142  return 0;
143 }
144 
146 {
147  InterleaveContext *s = ctx->priv;
148  int i;
149 
150  for (i = 0; i < ctx->nb_inputs; i++) {
152  av_freep(&s->queues[i]);
153  av_freep(&ctx->input_pads[i].name);
154  }
155 }
156 
157 static int config_output(AVFilterLink *outlink)
158 {
159  AVFilterContext *ctx = outlink->src;
160  AVFilterLink *inlink0 = ctx->inputs[0];
161  int i;
162 
163  if (outlink->type == AVMEDIA_TYPE_VIDEO) {
164  outlink->time_base = AV_TIME_BASE_Q;
165  outlink->w = inlink0->w;
166  outlink->h = inlink0->h;
167  outlink->sample_aspect_ratio = inlink0->sample_aspect_ratio;
168  outlink->format = inlink0->format;
169  outlink->frame_rate = (AVRational) {1, 0};
170  for (i = 1; i < ctx->nb_inputs; i++) {
171  AVFilterLink *inlink = ctx->inputs[i];
172 
173  if (outlink->w != inlink->w ||
174  outlink->h != inlink->h ||
175  outlink->sample_aspect_ratio.num != inlink->sample_aspect_ratio.num ||
176  outlink->sample_aspect_ratio.den != inlink->sample_aspect_ratio.den) {
177  av_log(ctx, AV_LOG_ERROR, "Parameters for input link %s "
178  "(size %dx%d, SAR %d:%d) do not match the corresponding "
179  "output link parameters (%dx%d, SAR %d:%d)\n",
180  ctx->input_pads[i].name, inlink->w, inlink->h,
181  inlink->sample_aspect_ratio.num,
182  inlink->sample_aspect_ratio.den,
183  outlink->w, outlink->h,
184  outlink->sample_aspect_ratio.num,
185  outlink->sample_aspect_ratio.den);
186  return AVERROR(EINVAL);
187  }
188  }
189  }
190  return 0;
191 }
192 
193 static int request_frame(AVFilterLink *outlink)
194 {
195  AVFilterContext *ctx = outlink->src;
196  InterleaveContext *s = ctx->priv;
197  int i, ret;
198 
199  for (i = 0; i < ctx->nb_inputs; i++) {
200  if (!s->queues[i].available && !ctx->inputs[i]->status_out) {
201  ret = ff_request_frame(ctx->inputs[i]);
202  if (ret != AVERROR_EOF)
203  return ret;
204  }
205  }
206 
207  return push_frame(ctx);
208 }
209 
210 #if CONFIG_INTERLEAVE_FILTER
211 
214 
215 static const AVFilterPad interleave_outputs[] = {
216  {
217  .name = "default",
218  .type = AVMEDIA_TYPE_VIDEO,
219  .config_props = config_output,
220  .request_frame = request_frame,
221  },
222  { NULL }
223 };
224 
225 AVFilter ff_vf_interleave = {
226  .name = "interleave",
227  .description = NULL_IF_CONFIG_SMALL("Temporally interleave video inputs."),
228  .priv_size = sizeof(InterleaveContext),
229  .init = init,
230  .uninit = uninit,
231  .outputs = interleave_outputs,
232  .priv_class = &interleave_class,
234 };
235 
236 #endif
237 
238 #if CONFIG_AINTERLEAVE_FILTER
239 
241 AVFILTER_DEFINE_CLASS(ainterleave);
242 
243 static const AVFilterPad ainterleave_outputs[] = {
244  {
245  .name = "default",
246  .type = AVMEDIA_TYPE_AUDIO,
247  .config_props = config_output,
248  .request_frame = request_frame,
249  },
250  { NULL }
251 };
252 
253 AVFilter ff_af_ainterleave = {
254  .name = "ainterleave",
255  .description = NULL_IF_CONFIG_SMALL("Temporally interleave audio inputs."),
256  .priv_size = sizeof(InterleaveContext),
257  .init = init,
258  .uninit = uninit,
259  .outputs = ainterleave_outputs,
260  .priv_class = &ainterleave_class,
262 };
263 
264 #endif
static AVFrame * ff_bufqueue_get(struct FFBufQueue *queue)
Get the first buffer from the queue and remove it.
Definition: bufferqueue.h:98
#define NULL
Definition: coverity.c:32
const char * s
Definition: avisynth_c.h:768
#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:201
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:182
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:281
AVFrame * ff_null_get_video_buffer(AVFilterLink *link, int w, int h)
Definition: video.c:39
enum AVMediaType type
AVFilterPad type.
Definition: internal.h:65
static int config_output(AVFilterLink *outlink)
Definition: f_interleave.c:157
void * av_calloc(size_t nmemb, size_t size)
Non-inlined equivalent of av_mallocz_array().
Definition: mem.c:230
Structure holding the queue.
Definition: bufferqueue.h:49
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:1151
#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:294
static AVFrame * frame
static double av_q2d(AVRational a)
Convert an AVRational to a double.
Definition: rational.h:104
static int flags
Definition: log.c:57
#define AVERROR_EOF
End of file.
Definition: error.h:55
#define av_log(a,...)
#define DEFINE_OPTIONS(filt_name, flags_)
Definition: f_interleave.c:48
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 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:293
#define AVERROR(e)
Definition: error.h:43
struct FFBufQueue * queues
Definition: f_interleave.c:43
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:163
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:179
void * priv
private data for use by the filter
Definition: avfilter.h:353
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:197
int(* filter_frame)(AVFilterLink *link, AVFrame *frame)
Filtering callback.
Definition: internal.h:93
simple assert() macros that are a bit more flexible than ISO C assert().
char * av_asprintf(const char *fmt,...)
Definition: avstring.c:113
unsigned nb_inputs
number of input pads
Definition: avfilter.h:347
static av_cold void uninit(AVFilterContext *ctx)
Definition: f_interleave.c:145
AVFormatContext * ctx
Definition: movenc.c:48
static av_cold int init(AVFilterContext *ctx)
Definition: f_interleave.c:109
static const AVFilterPad outputs[]
Definition: af_afftfilt.c:389
static void ff_bufqueue_discard_all(struct FFBufQueue *queue)
Unref and remove all buffers from the queue.
Definition: bufferqueue.h:111
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 *(* get_audio_buffer)(AVFilterLink *link, int nb_samples)
Callback function to get an audio buffer.
Definition: internal.h:81
AVFrame *(* get_video_buffer)(AVFilterLink *link, int w, int h)
Callback function to get a video buffer.
Definition: internal.h:73
#define AV_TIME_BASE_Q
Internal time base represented as fractional value.
Definition: avutil.h:260
unsigned short available
number of available buffers
Definition: bufferqueue.h:52
#define AV_OPT_FLAG_VIDEO_PARAM
Definition: opt.h:282
Describe the class of an AVClass context structure.
Definition: log.h:67
Filter definition.
Definition: avfilter.h:144
Rational number (pair of numerator and denominator).
Definition: rational.h:58
const char * name
Filter name.
Definition: avfilter.h:148
AVFilterLink ** outputs
array of pointers to output links
Definition: avfilter.h:350
static void interleave(short *output, short **input, int channels, int samples)
Definition: resample.c:161
#define FF_INLINK_IDX(link)
Find the index of a link.
Definition: internal.h:348
static int request_frame(AVFilterLink *outlink)
Definition: f_interleave.c:193
static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
Definition: f_interleave.c:87
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)
static void ff_bufqueue_add(void *log, struct FFBufQueue *queue, AVFrame *buf)
Add a buffer to the queue.
Definition: bufferqueue.h:71
int ff_request_frame(AVFilterLink *link)
Request an input frame from the filter at the other end of the link.
Definition: avfilter.c:405
internal API functions
const AVFilter * filter
the AVFilter of which this is an instance
Definition: avfilter.h:341
static int push_frame(AVFilterContext *ctx)
Definition: f_interleave.c:55
#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
static AVFrame * ff_bufqueue_peek(struct FFBufQueue *queue, unsigned index)
Get a buffer from the queue without altering it.
Definition: bufferqueue.h:87