FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/filters.h
Date: 2024-11-20 23:03:26
Exec Total Coverage
Lines: 5 5 100.0%
Functions: 2 2 100.0%
Branches: 0 0 -%

Line Branch Exec Source
1 /*
2 * Filters implementation helper functions
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 License
8 * 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
14 * 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 #ifndef AVFILTER_FILTERS_H
22 #define AVFILTER_FILTERS_H
23
24 /**
25 * Filters implementation helper functions
26 */
27
28 #include "avfilter.h"
29
30 /**
31 * Special return code when activate() did not do anything.
32 */
33 #define FFERROR_NOT_READY FFERRTAG('N','R','D','Y')
34
35 /**
36 * A filter pad used for either input or output.
37 */
38 struct AVFilterPad {
39 /**
40 * Pad name. The name is unique among inputs and among outputs, but an
41 * input may have the same name as an output. This may be NULL if this
42 * pad has no need to ever be referenced by name.
43 */
44 const char *name;
45
46 /**
47 * AVFilterPad type.
48 */
49 enum AVMediaType type;
50
51 /**
52 * The filter expects writable frames from its input link,
53 * duplicating data buffers if needed.
54 *
55 * input pads only.
56 */
57 #define AVFILTERPAD_FLAG_NEEDS_WRITABLE (1 << 0)
58
59 /**
60 * The pad's name is allocated and should be freed generically.
61 */
62 #define AVFILTERPAD_FLAG_FREE_NAME (1 << 1)
63
64 /**
65 * A combination of AVFILTERPAD_FLAG_* flags.
66 */
67 int flags;
68
69 /**
70 * Callback functions to get a video/audio buffers. If NULL,
71 * the filter system will use ff_default_get_video_buffer() for video
72 * and ff_default_get_audio_buffer() for audio.
73 *
74 * The state of the union is determined by type.
75 *
76 * Input pads only.
77 */
78 union {
79 AVFrame *(*video)(AVFilterLink *link, int w, int h);
80 AVFrame *(*audio)(AVFilterLink *link, int nb_samples);
81 } get_buffer;
82
83 /**
84 * Filtering callback. This is where a filter receives a frame with
85 * audio/video data and should do its processing.
86 *
87 * Input pads only.
88 *
89 * @return >= 0 on success, a negative AVERROR on error. This function
90 * must ensure that frame is properly unreferenced on error if it
91 * hasn't been passed on to another filter.
92 */
93 int (*filter_frame)(AVFilterLink *link, AVFrame *frame);
94
95 /**
96 * Frame request callback. A call to this should result in some progress
97 * towards producing output over the given link. This should return zero
98 * on success, and another value on error.
99 *
100 * Output pads only.
101 */
102 int (*request_frame)(AVFilterLink *link);
103
104 /**
105 * Link configuration callback.
106 *
107 * For output pads, this should set the link properties such as
108 * width/height. This should NOT set the format property - that is
109 * negotiated between filters by the filter system using the
110 * query_formats() callback before this function is called.
111 *
112 * For input pads, this should check the properties of the link, and update
113 * the filter's internal state as necessary.
114 *
115 * For both input and output filters, this should return zero on success,
116 * and another value on error.
117 */
118 int (*config_props)(AVFilterLink *link);
119 };
120
121 /**
122 * Link properties exposed to filter code, but not external callers.
123 *
124 * Cf. AVFilterLink for public properties, FilterLinkInternal for
125 * properties private to the generic layer.
126 */
127 typedef struct FilterLink {
128 AVFilterLink pub;
129
130 /**
131 * Graph the filter belongs to.
132 */
133 struct AVFilterGraph *graph;
134
135 /**
136 * Current timestamp of the link, as defined by the most recent
137 * frame(s), in link time_base units.
138 */
139 int64_t current_pts;
140
141 /**
142 * Current timestamp of the link, as defined by the most recent
143 * frame(s), in AV_TIME_BASE units.
144 */
145 int64_t current_pts_us;
146
147 /**
148 * Minimum number of samples to filter at once.
149 *
150 * May be set by the link destination filter in its config_props().
151 * If 0, all related fields are ignored.
152 */
153 int min_samples;
154
155 /**
156 * Maximum number of samples to filter at once. If filter_frame() is
157 * called with more samples, it will split them.
158 *
159 * May be set by the link destination filter in its config_props().
160 */
161 int max_samples;
162
163 /**
164 * Number of past frames sent through the link.
165 */
166 int64_t frame_count_in, frame_count_out;
167
168 /**
169 * Number of past samples sent through the link.
170 */
171 int64_t sample_count_in, sample_count_out;
172
173 /**
174 * Frame rate of the stream on the link, or 1/0 if unknown or variable.
175 *
176 * May be set by the link source filter in its config_props(); if left to
177 * 0/0, will be automatically copied from the first input of the source
178 * filter if it exists.
179 *
180 * Sources should set it to the best estimation of the real frame rate.
181 * If the source frame rate is unknown or variable, set this to 1/0.
182 * Filters should update it if necessary depending on their function.
183 * Sinks can use it to set a default output frame rate.
184 * It is similar to the r_frame_rate field in AVStream.
185 */
186 AVRational frame_rate;
187
188 /**
189 * For hwaccel pixel formats, this should be a reference to the
190 * AVHWFramesContext describing the frames.
191 *
192 * May be set by the link source filter in its config_props().
193 */
194 AVBufferRef *hw_frames_ctx;
195 } FilterLink;
196
197 4501415 static inline FilterLink* ff_filter_link(AVFilterLink *link)
198 {
199 4501415 return (FilterLink*)link;
200 }
201
202 /**
203 * The filter is aware of hardware frames, and any hardware frame context
204 * should not be automatically propagated through it.
205 */
206 #define FF_FILTER_FLAG_HWFRAME_AWARE (1 << 0)
207
208 /**
209 * Find the index of a link.
210 *
211 * I.e. find i such that link == ctx->(in|out)puts[i]
212 */
213 #define FF_INLINK_IDX(link) ((int)((link)->dstpad - (link)->dst->input_pads))
214 #define FF_OUTLINK_IDX(link) ((int)((link)->srcpad - (link)->src->output_pads))
215
216 enum FilterFormatsState {
217 /**
218 * The default value meaning that this filter supports all formats
219 * and (for audio) sample rates and channel layouts/counts as long
220 * as these properties agree for all inputs and outputs.
221 * This state is only allowed in case all inputs and outputs actually
222 * have the same type.
223 * The union is unused in this state.
224 *
225 * This value must always be zero (for default static initialization).
226 */
227 FF_FILTER_FORMATS_PASSTHROUGH = 0,
228 FF_FILTER_FORMATS_QUERY_FUNC, ///< formats.query active.
229 FF_FILTER_FORMATS_QUERY_FUNC2, ///< formats.query_func2 active.
230 FF_FILTER_FORMATS_PIXFMT_LIST, ///< formats.pixels_list active.
231 FF_FILTER_FORMATS_SAMPLEFMTS_LIST, ///< formats.samples_list active.
232 FF_FILTER_FORMATS_SINGLE_PIXFMT, ///< formats.pix_fmt active
233 FF_FILTER_FORMATS_SINGLE_SAMPLEFMT, ///< formats.sample_fmt active.
234 };
235
236 #define FILTER_QUERY_FUNC(func) \
237 .formats.query_func = func, \
238 .formats_state = FF_FILTER_FORMATS_QUERY_FUNC
239 #define FILTER_QUERY_FUNC2(func) \
240 .formats.query_func2 = func, \
241 .formats_state = FF_FILTER_FORMATS_QUERY_FUNC2
242 #define FILTER_PIXFMTS_ARRAY(array) \
243 .formats.pixels_list = array, \
244 .formats_state = FF_FILTER_FORMATS_PIXFMT_LIST
245 #define FILTER_SAMPLEFMTS_ARRAY(array) \
246 .formats.samples_list = array, \
247 .formats_state = FF_FILTER_FORMATS_SAMPLEFMTS_LIST
248 #define FILTER_PIXFMTS(...) \
249 FILTER_PIXFMTS_ARRAY(((const enum AVPixelFormat []) { __VA_ARGS__, AV_PIX_FMT_NONE }))
250 #define FILTER_SAMPLEFMTS(...) \
251 FILTER_SAMPLEFMTS_ARRAY(((const enum AVSampleFormat[]) { __VA_ARGS__, AV_SAMPLE_FMT_NONE }))
252 #define FILTER_SINGLE_PIXFMT(pix_fmt_) \
253 .formats.pix_fmt = pix_fmt_, \
254 .formats_state = FF_FILTER_FORMATS_SINGLE_PIXFMT
255 #define FILTER_SINGLE_SAMPLEFMT(sample_fmt_) \
256 .formats.sample_fmt = sample_fmt_, \
257 .formats_state = FF_FILTER_FORMATS_SINGLE_SAMPLEFMT
258
259 #define FILTER_INOUTPADS(inout, array) \
260 .inout = array, \
261 .nb_ ## inout = FF_ARRAY_ELEMS(array)
262 #define FILTER_INPUTS(array) FILTER_INOUTPADS(inputs, (array))
263 #define FILTER_OUTPUTS(array) FILTER_INOUTPADS(outputs, (array))
264
265 #define AVFILTER_DEFINE_CLASS_EXT(name, desc, options) \
266 static const AVClass name##_class = { \
267 .class_name = desc, \
268 .item_name = av_default_item_name, \
269 .option = options, \
270 .version = LIBAVUTIL_VERSION_INT, \
271 .category = AV_CLASS_CATEGORY_FILTER, \
272 }
273 #define AVFILTER_DEFINE_CLASS(fname) \
274 AVFILTER_DEFINE_CLASS_EXT(fname, #fname, fname##_options)
275
276 #define D2TS(d) (isnan(d) ? AV_NOPTS_VALUE : (int64_t)(d))
277 #define TS2D(ts) ((ts) == AV_NOPTS_VALUE ? NAN : (double)(ts))
278 #define TS2T(ts, tb) ((ts) == AV_NOPTS_VALUE ? NAN : (double)(ts) * av_q2d(tb))
279
280 /**
281 * Mark a filter ready and schedule it for activation.
282 *
283 * This is automatically done when something happens to the filter (queued
284 * frame, status change, request on output).
285 * Filters implementing the activate callback can call it directly to
286 * perform one more round of processing later.
287 * It is also useful for filters reacting to external or asynchronous
288 * events.
289 */
290 void ff_filter_set_ready(AVFilterContext *filter, unsigned priority);
291
292 /**
293 * Get the number of frames available on the link.
294 * @return the number of frames available in the link fifo.
295 */
296 size_t ff_inlink_queued_frames(AVFilterLink *link);
297
298 /**
299 * Test if a frame is available on the link.
300 * @return >0 if a frame is available
301 */
302 int ff_inlink_check_available_frame(AVFilterLink *link);
303
304
305 /***
306 * Get the number of samples available on the link.
307 * @return the numer of samples available on the link.
308 */
309 int ff_inlink_queued_samples(AVFilterLink *link);
310
311 /**
312 * Test if enough samples are available on the link.
313 * @return >0 if enough samples are available
314 * @note on EOF and error, min becomes 1
315 */
316 int ff_inlink_check_available_samples(AVFilterLink *link, unsigned min);
317
318 /**
319 * Take a frame from the link's FIFO and update the link's stats.
320 *
321 * If ff_inlink_check_available_frame() was previously called, the
322 * preferred way of expressing it is "av_assert1(ret);" immediately after
323 * ff_inlink_consume_frame(). Negative error codes must still be checked.
324 *
325 * @note May trigger process_command() and/or update is_disabled.
326 * @return >0 if a frame is available,
327 * 0 and set rframe to NULL if no frame available,
328 * or AVERROR code
329 */
330 int ff_inlink_consume_frame(AVFilterLink *link, AVFrame **rframe);
331
332 /**
333 * Take samples from the link's FIFO and update the link's stats.
334 *
335 * If ff_inlink_check_available_samples() was previously called, the
336 * preferred way of expressing it is "av_assert1(ret);" immediately after
337 * ff_inlink_consume_samples(). Negative error codes must still be checked.
338 *
339 * @note May trigger process_command() and/or update is_disabled.
340 * @return >0 if a frame is available,
341 * 0 and set rframe to NULL if no frame available,
342 * or AVERROR code
343 */
344 int ff_inlink_consume_samples(AVFilterLink *link, unsigned min, unsigned max,
345 AVFrame **rframe);
346
347 /**
348 * Access a frame in the link fifo without consuming it.
349 * The first frame is numbered 0; the designated frame must exist.
350 * @return the frame at idx position in the link fifo.
351 */
352 AVFrame *ff_inlink_peek_frame(AVFilterLink *link, size_t idx);
353
354 /**
355 * Make sure a frame is writable.
356 * This is similar to av_frame_make_writable() except it uses the link's
357 * buffer allocation callback, and therefore allows direct rendering.
358 */
359 int ff_inlink_make_frame_writable(AVFilterLink *link, AVFrame **rframe);
360
361 /**
362 * Test and acknowledge the change of status on the link.
363 *
364 * Status means EOF or an error condition; a change from the normal (0)
365 * status to a non-zero status can be queued in a filter's input link, it
366 * becomes relevant after the frames queued in the link's FIFO are
367 * processed. This function tests if frames are still queued and if a queued
368 * status change has not yet been processed. In that case it performs basic
369 * treatment (updating the link's timestamp) and returns a positive value to
370 * let the filter do its own treatments (flushing...).
371 *
372 * Filters implementing the activate callback should call this function when
373 * they think it might succeed (usually after checking unsuccessfully for a
374 * queued frame).
375 * Filters implementing the filter_frame and request_frame callbacks do not
376 * need to call that since the same treatment happens in ff_filter_frame().
377 *
378 * @param[out] rstatus new or current status
379 * @param[out] rpts current timestamp of the link in link time base
380 * @return >0 if status changed, <0 if status already acked, 0 otherwise
381 */
382 int ff_inlink_acknowledge_status(AVFilterLink *link, int *rstatus, int64_t *rpts);
383
384 /**
385 * Mark that a frame is wanted on the link.
386 * Unlike ff_filter_frame(), it must not be called when the link has a
387 * non-zero status, and thus does not acknowledge it.
388 * Also it cannot fail.
389 */
390 void ff_inlink_request_frame(AVFilterLink *link);
391
392 /**
393 * Set the status on an input link.
394 * Also discard all frames in the link's FIFO.
395 */
396 void ff_inlink_set_status(AVFilterLink *link, int status);
397
398 /**
399 * Test if a frame is wanted on an output link.
400 */
401 int ff_outlink_frame_wanted(AVFilterLink *link);
402
403 /**
404 * Get the status on an output link.
405 */
406 int ff_outlink_get_status(AVFilterLink *link);
407
408 /**
409 * Set the status field of a link from the source filter.
410 * The pts should reflect the timestamp of the status change,
411 * in link time base and relative to the frames timeline.
412 * In particular, for AVERROR_EOF, it should reflect the
413 * end time of the last frame.
414 */
415 void ff_avfilter_link_set_in_status(AVFilterLink *link, int status, int64_t pts);
416
417 /**
418 * Set the status field of a link from the source filter.
419 * The pts should reflect the timestamp of the status change,
420 * in link time base and relative to the frames timeline.
421 * In particular, for AVERROR_EOF, it should reflect the
422 * end time of the last frame.
423 */
424 7973 static inline void ff_outlink_set_status(AVFilterLink *link, int status, int64_t pts)
425 {
426 7973 ff_avfilter_link_set_in_status(link, status, pts);
427 7973 }
428
429 /**
430 * Forward the status on an output link to an input link.
431 * If the status is set, it will discard all queued frames and this macro
432 * will return immediately.
433 */
434 #define FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink) do { \
435 int ret = ff_outlink_get_status(outlink); \
436 if (ret) { \
437 ff_inlink_set_status(inlink, ret); \
438 return 0; \
439 } \
440 } while (0)
441
442 /**
443 * Forward the status on an output link to all input links.
444 * If the status is set, it will discard all queued frames and this macro
445 * will return immediately.
446 */
447 #define FF_FILTER_FORWARD_STATUS_BACK_ALL(outlink, filter) do { \
448 int ret = ff_outlink_get_status(outlink); \
449 if (ret) { \
450 unsigned i; \
451 for (i = 0; i < filter->nb_inputs; i++) \
452 ff_inlink_set_status(filter->inputs[i], ret); \
453 return 0; \
454 } \
455 } while (0)
456
457 /**
458 * Acknowledge the status on an input link and forward it to an output link.
459 * If the status is set, this macro will return immediately.
460 */
461 #define FF_FILTER_FORWARD_STATUS(inlink, outlink) do { \
462 int status; \
463 int64_t pts; \
464 if (ff_inlink_acknowledge_status(inlink, &status, &pts)) { \
465 ff_outlink_set_status(outlink, status, pts); \
466 return 0; \
467 } \
468 } while (0)
469
470 /**
471 * Acknowledge the status on an input link and forward it to an output link.
472 * If the status is set, this macro will return immediately.
473 */
474 #define FF_FILTER_FORWARD_STATUS_ALL(inlink, filter) do { \
475 int status; \
476 int64_t pts; \
477 if (ff_inlink_acknowledge_status(inlink, &status, &pts)) { \
478 unsigned i; \
479 for (i = 0; i < filter->nb_outputs; i++) \
480 ff_outlink_set_status(filter->outputs[i], status, pts); \
481 return 0; \
482 } \
483 } while (0)
484
485 /**
486 * Forward the frame_wanted_out flag from an output link to an input link.
487 * If the flag is set, this macro will return immediately.
488 */
489 #define FF_FILTER_FORWARD_WANTED(outlink, inlink) do { \
490 if (ff_outlink_frame_wanted(outlink)) { \
491 ff_inlink_request_frame(inlink); \
492 return 0; \
493 } \
494 } while (0)
495
496 /**
497 * Check for flow control between input and output.
498 * This is necessary for filters that may produce several output frames for
499 * a single input event, otherwise they may produce them all at once,
500 * causing excessive memory consumption.
501 */
502 int ff_inoutlink_check_flow(AVFilterLink *inlink, AVFilterLink *outlink);
503
504 /**
505 * Perform any additional setup required for hardware frames.
506 *
507 * link->hw_frames_ctx must be set before calling this function.
508 * Inside link->hw_frames_ctx, the fields format, sw_format, width and
509 * height must be set. If dynamically allocated pools are not supported,
510 * then initial_pool_size must also be set, to the minimum hardware frame
511 * pool size necessary for the filter to work (taking into account any
512 * frames which need to stored for use in operations as appropriate). If
513 * default_pool_size is nonzero, then it will be used as the pool size if
514 * no other modification takes place (this can be used to preserve
515 * compatibility).
516 */
517 int ff_filter_init_hw_frames(AVFilterContext *avctx, AVFilterLink *link,
518 int default_pool_size);
519
520 /**
521 * Generic processing of user supplied commands that are set
522 * in the same way as the filter options.
523 * NOTE: 'enable' option is handled separately, and not by
524 * this function.
525 */
526 int ff_filter_process_command(AVFilterContext *ctx, const char *cmd,
527 const char *arg, char *res, int res_len, int flags);
528
529 /**
530 * Get number of threads for current filter instance.
531 * This number is always same or less than graph->nb_threads.
532 */
533 int ff_filter_get_nb_threads(AVFilterContext *ctx) av_pure;
534
535 /**
536 * Send a frame of data to the next filter.
537 *
538 * @param link the output link over which the data is being sent
539 * @param frame a reference to the buffer of data being sent. The
540 * receiving filter will free this reference when it no longer
541 * needs it or pass it on to the next filter.
542 *
543 * @return >= 0 on success, a negative AVERROR on error. The receiving filter
544 * is responsible for unreferencing frame in case of error.
545 */
546 int ff_filter_frame(AVFilterLink *link, AVFrame *frame);
547
548 /**
549 * Request an input frame from the filter at the other end of the link.
550 *
551 * This function must not be used by filters using the activate callback,
552 * use ff_link_set_frame_wanted() instead.
553 *
554 * The input filter may pass the request on to its inputs, fulfill the
555 * request from an internal buffer or any other means specific to its function.
556 *
557 * When the end of a stream is reached AVERROR_EOF is returned and no further
558 * frames are returned after that.
559 *
560 * When a filter is unable to output a frame for example due to its sources
561 * being unable to do so or because it depends on external means pushing data
562 * into it then AVERROR(EAGAIN) is returned.
563 * It is important that a AVERROR(EAGAIN) return is returned all the way to the
564 * caller (generally eventually a user application) as this step may (but does
565 * not have to be) necessary to provide the input with the next frame.
566 *
567 * If a request is successful then some progress has been made towards
568 * providing a frame on the link (through ff_filter_frame()). A filter that
569 * needs several frames to produce one is allowed to return success if one
570 * more frame has been processed but no output has been produced yet. A
571 * filter is also allowed to simply forward a success return value.
572 *
573 * @param link the input link
574 * @return zero on success
575 * AVERROR_EOF on end of file
576 * AVERROR(EAGAIN) if the previous filter cannot output a frame
577 * currently and can neither guarantee that EOF has been reached.
578 */
579 int ff_request_frame(AVFilterLink *link);
580
581 /**
582 * Append a new input/output pad to the filter's list of such pads.
583 *
584 * The *_free_name versions will set the AVFILTERPAD_FLAG_FREE_NAME flag
585 * ensuring that the name will be freed generically (even on insertion error).
586 */
587 int ff_append_inpad (AVFilterContext *f, AVFilterPad *p);
588 int ff_append_outpad(AVFilterContext *f, AVFilterPad *p);
589 int ff_append_inpad_free_name (AVFilterContext *f, AVFilterPad *p);
590 int ff_append_outpad_free_name(AVFilterContext *f, AVFilterPad *p);
591
592 /**
593 * Tell if an integer is contained in the provided -1-terminated list of integers.
594 * This is useful for determining (for instance) if an AVPixelFormat is in an
595 * array of supported formats.
596 *
597 * @param fmt provided format
598 * @param fmts -1-terminated list of formats
599 * @return 1 if present, 0 if absent
600 */
601 int ff_fmt_is_in(int fmt, const int *fmts);
602
603 int ff_filter_execute(AVFilterContext *ctx, avfilter_action_func *func,
604 void *arg, int *ret, int nb_jobs);
605
606 #endif /* AVFILTER_FILTERS_H */
607