FFmpeg: libavfilter/avf_concat.c Source File
Go to the documentation of this file. 1 /*
2 * Copyright (c) 2012 Nicolas George
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.
14 * See the GNU Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with FFmpeg; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21 /**
22 * @file
23 * concat audio-video filter
24 */
25
34
36
41 unsigned cur_idx;
/**< index of the first input of current segment */
42 int64_t
delta_ts;
/**< timestamp to add to produce output timestamps */
43 unsigned nb_in_active;
/**< number of active inputs in current segment */
51
52 #define OFFSET(x) offsetof(ConcatContext, x)
53 #define A AV_OPT_FLAG_AUDIO_PARAM
54 #define F AV_OPT_FLAG_FILTERING_PARAM
55 #define V AV_OPT_FLAG_VIDEO_PARAM
56
58 {
"n",
"specify the number of segments",
OFFSET(nb_segments),
60 { "v", "specify the number of video streams",
63 { "a", "specify the number of audio streams",
66 { "unsafe", "enable unsafe mode",
70 };
71
73
75 {
77 unsigned type, nb_str, idx0 = 0, idx,
str, seg;
81
85 idx = idx0;
86
87 /* Set the output formats */
91
99 }
100
101 /* Set the same formats for each corresponding input */
102 for (seg = 0; seg <
cat->nb_segments; seg++) {
109 }
110 idx +=
ctx->nb_outputs;
111 }
112
113 idx0++;
114 }
115 }
116 return 0;
117 }
118
120 {
124 unsigned in_no = out_no, seg;
126
127 /* enhancement: find a common one */
134
135 for (seg = 1; seg <
cat->nb_segments; seg++) {
140 "Video inputs have different frame rates, output will be VFR\n");
142 break;
143 }
144 }
145
146 for (seg = 1; seg <
cat->nb_segments; seg++) {
150 /* possible enhancement: unsafe mode, do not check */
154 inlink->sample_aspect_ratio.num ||
157 "(size %dx%d, SAR %d:%d) do not match the corresponding "
158 "output link %s parameters (%dx%d, SAR %d:%d)\n",
160 inlink->sample_aspect_ratio.num,
161 inlink->sample_aspect_ratio.den,
162 ctx->input_pads[out_no].name, outlink->
w, outlink->
h,
167 }
168 }
169
170 return 0;
171 }
172
174 {
176 unsigned out_no = in_no %
ctx->nb_outputs;
179 struct concat_in *in = &
cat->in[in_no];
180
183 in->nb_frames++;
184 /* add duration to input PTS */
186 /* use number of audio samples */
190 else if (in->nb_frames >= 2)
191 /* use mean duration */
192 in->pts =
av_rescale(in->pts, in->nb_frames, in->nb_frames - 1);
193
194 buf->
pts +=
cat->delta_ts;
196 }
197
199 {
203
205 }
206
208 {
212
214 }
215
217 {
219
220 cat->in[in_no].eof = 1;
223 ctx->input_pads[in_no].name,
cat->nb_in_active);
224 }
225
227 {
229 unsigned i =
cat->cur_idx;
230 unsigned imax =
i +
ctx->nb_outputs;
232
234 for (;
i < imax;
i++)
238 }
239
241 int64_t seg_delta)
242 {
245 int64_t base_pts =
cat->in[in_no].pts +
cat->delta_ts - seg_delta;
246 int64_t nb_samples, sent = 0;
247 int frame_nb_samples,
ret;
250
253 if (
cat->in[in_no].pts < INT64_MIN + seg_delta)
255 if (seg_delta < cat->in[in_no].
pts)
259 frame_nb_samples =
FFMAX(9600, rate_tb.
den / 5);
/* arbitrary */
260 while (nb_samples) {
261 frame_nb_samples =
FFMIN(frame_nb_samples, nb_samples);
263 if (!buf)
271 sent += frame_nb_samples;
272 nb_samples -= frame_nb_samples;
273 }
274 return 0;
275 }
276
278 {
281 unsigned str, str_max;
282 int64_t seg_delta;
283
285 cat->cur_idx +=
ctx->nb_outputs;
286 cat->nb_in_active =
ctx->nb_outputs;
289
290 if (
cat->cur_idx <
ctx->nb_inputs) {
291 /* pad audio streams with silence */
294 for (;
str < str_max;
str++) {
296 seg_delta);
299 }
300 }
301 return 0;
302 }
303
305 {
309
310 /* create input pads */
311 for (seg = 0; seg <
cat->nb_segments; seg++) {
316 };
319 else
324 }
325 }
326 }
327 /* create output pads */
333 };
337 }
338 }
339
343 cat->nb_in_active =
ctx->nb_outputs;
344 return 0;
345 }
346
348 {
350
352 }
353
355 {
361
362 /* Forward status back */
363 for (
i = 0;
i <
ctx->nb_outputs;
i++) {
366 continue;
367 for (j =
i; j <
ctx->nb_inputs; j +=
ctx->nb_outputs) {
368 if (!
cat->in[j].eof) {
371 return 0;
372 }
373 }
374
375 }
376
377 /* Forward available frames */
378 if (
cat->cur_idx <
ctx->nb_inputs) {
379 for (
i = 0;
i <
ctx->nb_outputs;
i++) {
386 }
387 }
388 }
389
390 /* Forward status change */
391 if (
cat->cur_idx <
ctx->nb_inputs) {
392 for (
i = 0;
i <
ctx->nb_outputs;
i++) {
394
396 /* TODO use pts */
399 if (
cat->cur_idx +
ctx->nb_outputs >=
ctx->nb_inputs) {
400 int64_t eof_pts =
cat->delta_ts;
401
404 }
405 if (!
cat->nb_in_active) {
409 }
411 return 0;
412 }
413 }
414 }
415
417 for (
i = 0;
i <
ctx->nb_outputs;
i++) {
419 if (
cat->in[
cat->cur_idx +
i].eof) {
420 for (j = 0; j <
ctx->nb_outputs; j++)
421 if (!
cat->in[
cat->cur_idx + j].eof)
423 return 0;
424 } else {
427 }
428 }
429 }
430
432 }
433
435 char *res,
int res_len,
int flags)
436 {
438
439 if (!strcmp(cmd, "next")) {
442 }
443
445 }
446
460 };
AVFrame * ff_get_video_buffer(AVFilterLink *link, int w, int h)
Request a picture buffer with a specific set of permissions.
AVFrame * ff_get_audio_buffer(AVFilterLink *link, int nb_samples)
Request an audio samples buffer with a specific set of permissions.
A list of supported channel layouts.
they must not be accessed directly The fifo field contains the frames that are queued in the input for processing by the filter The status_in and status_out fields contains the queued status(EOF or error) of the link
Filter the word "frame" indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
enum MovChannelLayoutTag * layouts
#define AV_TIME_BASE_Q
Internal time base represented as fractional value.
The exact code depends on how similar the blocks are and how related they are to the and needs to apply these operations to the correct inlink or outlink if there are several Macros are available to factor that when no extra processing is inlink
char * av_asprintf(const char *fmt,...)
static int activate(AVFilterContext *ctx)
This structure describes decoded (raw) audio or video data.
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
#define FILTER_QUERY_FUNC(func)
#define AV_LOG_VERBOSE
Detailed information.
static int config_output(AVFilterLink *outlink)
const char * name
Filter name.
int nb_channels
Number of channels in this layout.
A link between two filters.
static int flush_segment(AVFilterContext *ctx)
int ff_inlink_consume_frame(AVFilterLink *link, AVFrame **rframe)
Take a frame from the link's FIFO and update the link's stats.
static void find_next_delta_ts(AVFilterContext *ctx, int64_t *seg_delta)
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf type
#define AVFILTER_FLAG_DYNAMIC_INPUTS
The number of the filter inputs is not determined just by AVFilter.inputs.
unsigned cur_idx
index of the first input of current segment
A filter pad used for either input or output.
static int query_formats(AVFilterContext *ctx)
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
static void ff_outlink_set_status(AVFilterLink *link, int status, int64_t pts)
Set the status field of a link from the source filter.
AVFILTER_DEFINE_CLASS(concat)
void ff_inlink_request_frame(AVFilterLink *link)
Mark that a frame is wanted on the link.
#define FF_OUTLINK_IDX(link)
AVRational sample_aspect_ratio
agreed upon sample aspect ratio
static const AVFilterPad outputs[]
AVRational frame_rate
Frame rate of the stream on the link, or 1/0 if unknown or variable; if left to 0/0,...
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
const AVFilter ff_avf_concat
Describe the class of an AVClass context structure.
Rational number (pair of numerator and denominator).
int ff_append_inpad_free_name(AVFilterContext *f, AVFilterPad *p)
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several inputs
static const AVOption concat_options[]
int ff_inlink_acknowledge_status(AVFilterLink *link, int *rstatus, int64_t *rpts)
Test and acknowledge the change of status on the link.
#define AVFILTER_FLAG_DYNAMIC_OUTPUTS
The number of the filter outputs is not determined just by AVFilter.outputs.
unsigned nb_in_active
number of active inputs in current segment
static AVFrame * get_audio_buffer(AVFilterLink *inlink, int nb_samples)
void ff_inlink_set_status(AVFilterLink *link, int status)
Set the status on an input link.
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
int format
agreed upon media format
static int process_command(AVFilterContext *ctx, const char *cmd, const char *args, char *res, int res_len, int flags)
static AVRational av_make_q(int num, int den)
Create an AVRational.
unsigned nb_streams[TYPE_ALL]
number of out streams of each type
AVFilterContext * src
source filter
static const AVClass concat_class
static void close_input(AVFilterContext *ctx, unsigned in_no)
int nb_samples
number of audio samples (per channel) described by this frame
#define i(width, name, range_min, range_max)
int w
agreed upon image width
uint8_t ** extended_data
pointers to the data planes/channels.
union AVFilterPad::@214 get_buffer
Callback functions to get a video/audio buffers.
struct ConcatContext::concat_in * in
const char * name
Pad name.
int64_t av_rescale(int64_t a, int64_t b, int64_t c)
Rescale a 64-bit integer with rounding to nearest.
static int send_silence(AVFilterContext *ctx, unsigned in_no, unsigned out_no, int64_t seg_delta)
void * av_calloc(size_t nmemb, size_t size)
static av_cold int init(AVFilterContext *ctx)
int av_samples_set_silence(uint8_t **audio_data, int offset, int nb_samples, int nb_channels, enum AVSampleFormat sample_fmt)
Fill an audio buffer with silence.
enum AVMediaType type
AVFilterPad type.
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several the filter must be ready for frames arriving randomly on any input any filter with several inputs will most likely require some kind of queuing mechanism It is perfectly acceptable to have a limited queue and to drop frames when the inputs are too unbalanced request_frame For filters that do not use the this method is called when a frame is wanted on an output For a it should directly call filter_frame on the corresponding output For a if there are queued frames already one of these frames should be pushed If the filter should request a frame on one of its repeatedly until at least one frame has been pushed Return or at least make progress towards producing a frame
AVFrame *(* video)(AVFilterLink *link, int w, int h)
int h
agreed upon image height
#define FF_INLINK_IDX(link)
Find the index of a link.
AVRational time_base
Define the time base used by the PTS of the frames/samples which will pass through this link.
static av_cold void uninit(AVFilterContext *ctx)
int ff_outlink_get_status(AVFilterLink *link)
Get the status on an output link.
AVChannelLayout ch_layout
channel layout of current buffer (see libavutil/channel_layout.h)
AVFrame *(* audio)(AVFilterLink *link, int nb_samples)
int ff_append_outpad_free_name(AVFilterContext *f, AVFilterPad *p)
#define flags(name, subs,...)
#define AVERROR_BUG
Internal bug, also see AVERROR_BUG2.
int64_t delta_ts
timestamp to add to produce output timestamps
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
the definition of that something depends on the semantic of the filter The callback must examine the status of the filter s links and proceed accordingly The status of output links is stored in the status_in and status_out fields and tested by the ff_outlink_frame_wanted() function. If this function returns true
static AVFrame * get_video_buffer(AVFilterLink *inlink, int w, int h)
void ff_filter_set_ready(AVFilterContext *filter, unsigned priority)
Mark a filter ready and schedule it for activation.
static int push_frame(AVFilterContext *ctx, unsigned in_no, AVFrame *buf)
Generated on Wed Aug 24 2022 21:42:00 for FFmpeg by
doxygen
1.8.17