You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1132 lines
40KB

  1. /*
  2. * filter layer
  3. * Copyright (c) 2007 Bobby Bingham
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #ifndef AVFILTER_AVFILTER_H
  22. #define AVFILTER_AVFILTER_H
  23. /**
  24. * @file
  25. * @ingroup lavfi
  26. * Main libavfilter public API header
  27. */
  28. /**
  29. * @defgroup lavfi libavfilter
  30. * Graph-based frame editing library.
  31. *
  32. * @{
  33. */
  34. #include <stddef.h>
  35. #include "libavutil/attributes.h"
  36. #include "libavutil/avutil.h"
  37. #include "libavutil/buffer.h"
  38. #include "libavutil/dict.h"
  39. #include "libavutil/frame.h"
  40. #include "libavutil/log.h"
  41. #include "libavutil/samplefmt.h"
  42. #include "libavutil/pixfmt.h"
  43. #include "libavutil/rational.h"
  44. #include "libavfilter/version.h"
  45. /**
  46. * Return the LIBAVFILTER_VERSION_INT constant.
  47. */
  48. unsigned avfilter_version(void);
  49. /**
  50. * Return the libavfilter build-time configuration.
  51. */
  52. const char *avfilter_configuration(void);
  53. /**
  54. * Return the libavfilter license.
  55. */
  56. const char *avfilter_license(void);
  57. typedef struct AVFilterContext AVFilterContext;
  58. typedef struct AVFilterLink AVFilterLink;
  59. typedef struct AVFilterPad AVFilterPad;
  60. typedef struct AVFilterFormats AVFilterFormats;
  61. /**
  62. * Get the number of elements in a NULL-terminated array of AVFilterPads (e.g.
  63. * AVFilter.inputs/outputs).
  64. */
  65. int avfilter_pad_count(const AVFilterPad *pads);
  66. /**
  67. * Get the name of an AVFilterPad.
  68. *
  69. * @param pads an array of AVFilterPads
  70. * @param pad_idx index of the pad in the array it; is the caller's
  71. * responsibility to ensure the index is valid
  72. *
  73. * @return name of the pad_idx'th pad in pads
  74. */
  75. const char *avfilter_pad_get_name(const AVFilterPad *pads, int pad_idx);
  76. /**
  77. * Get the type of an AVFilterPad.
  78. *
  79. * @param pads an array of AVFilterPads
  80. * @param pad_idx index of the pad in the array; it is the caller's
  81. * responsibility to ensure the index is valid
  82. *
  83. * @return type of the pad_idx'th pad in pads
  84. */
  85. enum AVMediaType avfilter_pad_get_type(const AVFilterPad *pads, int pad_idx);
  86. /**
  87. * The number of the filter inputs is not determined just by AVFilter.inputs.
  88. * The filter might add additional inputs during initialization depending on the
  89. * options supplied to it.
  90. */
  91. #define AVFILTER_FLAG_DYNAMIC_INPUTS (1 << 0)
  92. /**
  93. * The number of the filter outputs is not determined just by AVFilter.outputs.
  94. * The filter might add additional outputs during initialization depending on
  95. * the options supplied to it.
  96. */
  97. #define AVFILTER_FLAG_DYNAMIC_OUTPUTS (1 << 1)
  98. /**
  99. * The filter supports multithreading by splitting frames into multiple parts
  100. * and processing them concurrently.
  101. */
  102. #define AVFILTER_FLAG_SLICE_THREADS (1 << 2)
  103. /**
  104. * Some filters support a generic "enable" expression option that can be used
  105. * to enable or disable a filter in the timeline. Filters supporting this
  106. * option have this flag set. When the enable expression is false, the default
  107. * no-op filter_frame() function is called in place of the filter_frame()
  108. * callback defined on each input pad, thus the frame is passed unchanged to
  109. * the next filters.
  110. */
  111. #define AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC (1 << 16)
  112. /**
  113. * Same as AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC, except that the filter will
  114. * have its filter_frame() callback(s) called as usual even when the enable
  115. * expression is false. The filter will disable filtering within the
  116. * filter_frame() callback(s) itself, for example executing code depending on
  117. * the AVFilterContext->is_disabled value.
  118. */
  119. #define AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL (1 << 17)
  120. /**
  121. * Handy mask to test whether the filter supports or no the timeline feature
  122. * (internally or generically).
  123. */
  124. #define AVFILTER_FLAG_SUPPORT_TIMELINE (AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC | AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL)
  125. /**
  126. * Filter definition. This defines the pads a filter contains, and all the
  127. * callback functions used to interact with the filter.
  128. */
  129. typedef struct AVFilter {
  130. /**
  131. * Filter name. Must be non-NULL and unique among filters.
  132. */
  133. const char *name;
  134. /**
  135. * A description of the filter. May be NULL.
  136. *
  137. * You should use the NULL_IF_CONFIG_SMALL() macro to define it.
  138. */
  139. const char *description;
  140. /**
  141. * List of inputs, terminated by a zeroed element.
  142. *
  143. * NULL if there are no (static) inputs. Instances of filters with
  144. * AVFILTER_FLAG_DYNAMIC_INPUTS set may have more inputs than present in
  145. * this list.
  146. */
  147. const AVFilterPad *inputs;
  148. /**
  149. * List of outputs, terminated by a zeroed element.
  150. *
  151. * NULL if there are no (static) outputs. Instances of filters with
  152. * AVFILTER_FLAG_DYNAMIC_OUTPUTS set may have more outputs than present in
  153. * this list.
  154. */
  155. const AVFilterPad *outputs;
  156. /**
  157. * A class for the private data, used to declare filter private AVOptions.
  158. * This field is NULL for filters that do not declare any options.
  159. *
  160. * If this field is non-NULL, the first member of the filter private data
  161. * must be a pointer to AVClass, which will be set by libavfilter generic
  162. * code to this class.
  163. */
  164. const AVClass *priv_class;
  165. /**
  166. * A combination of AVFILTER_FLAG_*
  167. */
  168. int flags;
  169. /*****************************************************************
  170. * All fields below this line are not part of the public API. They
  171. * may not be used outside of libavfilter and can be changed and
  172. * removed at will.
  173. * New public fields should be added right above.
  174. *****************************************************************
  175. */
  176. /**
  177. * Filter pre-initialization function
  178. *
  179. * This callback will be called immediately after the filter context is
  180. * allocated, to allow allocating and initing sub-objects.
  181. *
  182. * If this callback is not NULL, the uninit callback will be called on
  183. * allocation failure.
  184. *
  185. * @return 0 on success,
  186. * AVERROR code on failure (but the code will be
  187. * dropped and treated as ENOMEM by the calling code)
  188. */
  189. int (*preinit)(AVFilterContext *ctx);
  190. /**
  191. * Filter initialization function.
  192. *
  193. * This callback will be called only once during the filter lifetime, after
  194. * all the options have been set, but before links between filters are
  195. * established and format negotiation is done.
  196. *
  197. * Basic filter initialization should be done here. Filters with dynamic
  198. * inputs and/or outputs should create those inputs/outputs here based on
  199. * provided options. No more changes to this filter's inputs/outputs can be
  200. * done after this callback.
  201. *
  202. * This callback must not assume that the filter links exist or frame
  203. * parameters are known.
  204. *
  205. * @ref AVFilter.uninit "uninit" is guaranteed to be called even if
  206. * initialization fails, so this callback does not have to clean up on
  207. * failure.
  208. *
  209. * @return 0 on success, a negative AVERROR on failure
  210. */
  211. int (*init)(AVFilterContext *ctx);
  212. /**
  213. * Should be set instead of @ref AVFilter.init "init" by the filters that
  214. * want to pass a dictionary of AVOptions to nested contexts that are
  215. * allocated during init.
  216. *
  217. * On return, the options dict should be freed and replaced with one that
  218. * contains all the options which could not be processed by this filter (or
  219. * with NULL if all the options were processed).
  220. *
  221. * Otherwise the semantics is the same as for @ref AVFilter.init "init".
  222. */
  223. int (*init_dict)(AVFilterContext *ctx, AVDictionary **options);
  224. /**
  225. * Filter uninitialization function.
  226. *
  227. * Called only once right before the filter is freed. Should deallocate any
  228. * memory held by the filter, release any buffer references, etc. It does
  229. * not need to deallocate the AVFilterContext.priv memory itself.
  230. *
  231. * This callback may be called even if @ref AVFilter.init "init" was not
  232. * called or failed, so it must be prepared to handle such a situation.
  233. */
  234. void (*uninit)(AVFilterContext *ctx);
  235. /**
  236. * Query formats supported by the filter on its inputs and outputs.
  237. *
  238. * This callback is called after the filter is initialized (so the inputs
  239. * and outputs are fixed), shortly before the format negotiation. This
  240. * callback may be called more than once.
  241. *
  242. * This callback must set AVFilterLink.out_formats on every input link and
  243. * AVFilterLink.in_formats on every output link to a list of pixel/sample
  244. * formats that the filter supports on that link. For audio links, this
  245. * filter must also set @ref AVFilterLink.in_samplerates "in_samplerates" /
  246. * @ref AVFilterLink.out_samplerates "out_samplerates" and
  247. * @ref AVFilterLink.in_channel_layouts "in_channel_layouts" /
  248. * @ref AVFilterLink.out_channel_layouts "out_channel_layouts" analogously.
  249. *
  250. * This callback may be NULL for filters with one input, in which case
  251. * libavfilter assumes that it supports all input formats and preserves
  252. * them on output.
  253. *
  254. * @return zero on success, a negative value corresponding to an
  255. * AVERROR code otherwise
  256. */
  257. int (*query_formats)(AVFilterContext *);
  258. int priv_size; ///< size of private data to allocate for the filter
  259. int flags_internal; ///< Additional flags for avfilter internal use only.
  260. /**
  261. * Used by the filter registration system. Must not be touched by any other
  262. * code.
  263. */
  264. struct AVFilter *next;
  265. /**
  266. * Make the filter instance process a command.
  267. *
  268. * @param cmd the command to process, for handling simplicity all commands must be alphanumeric only
  269. * @param arg the argument for the command
  270. * @param res a buffer with size res_size where the filter(s) can return a response. This must not change when the command is not supported.
  271. * @param flags if AVFILTER_CMD_FLAG_FAST is set and the command would be
  272. * time consuming then a filter should treat it like an unsupported command
  273. *
  274. * @returns >=0 on success otherwise an error code.
  275. * AVERROR(ENOSYS) on unsupported commands
  276. */
  277. int (*process_command)(AVFilterContext *, const char *cmd, const char *arg, char *res, int res_len, int flags);
  278. /**
  279. * Filter initialization function, alternative to the init()
  280. * callback. Args contains the user-supplied parameters, opaque is
  281. * used for providing binary data.
  282. */
  283. int (*init_opaque)(AVFilterContext *ctx, void *opaque);
  284. /**
  285. * Filter activation function.
  286. *
  287. * Called when any processing is needed from the filter, instead of any
  288. * filter_frame and request_frame on pads.
  289. *
  290. * The function must examine inlinks and outlinks and perform a single
  291. * step of processing. If there is nothing to do, the function must do
  292. * nothing and not return an error. If more steps are or may be
  293. * possible, it must use ff_filter_set_ready() to schedule another
  294. * activation.
  295. */
  296. int (*activate)(AVFilterContext *ctx);
  297. } AVFilter;
  298. /**
  299. * Process multiple parts of the frame concurrently.
  300. */
  301. #define AVFILTER_THREAD_SLICE (1 << 0)
  302. typedef struct AVFilterInternal AVFilterInternal;
  303. /** An instance of a filter */
  304. struct AVFilterContext {
  305. const AVClass *av_class; ///< needed for av_log() and filters common options
  306. const AVFilter *filter; ///< the AVFilter of which this is an instance
  307. char *name; ///< name of this filter instance
  308. AVFilterPad *input_pads; ///< array of input pads
  309. AVFilterLink **inputs; ///< array of pointers to input links
  310. unsigned nb_inputs; ///< number of input pads
  311. AVFilterPad *output_pads; ///< array of output pads
  312. AVFilterLink **outputs; ///< array of pointers to output links
  313. unsigned nb_outputs; ///< number of output pads
  314. void *priv; ///< private data for use by the filter
  315. struct AVFilterGraph *graph; ///< filtergraph this filter belongs to
  316. /**
  317. * Type of multithreading being allowed/used. A combination of
  318. * AVFILTER_THREAD_* flags.
  319. *
  320. * May be set by the caller before initializing the filter to forbid some
  321. * or all kinds of multithreading for this filter. The default is allowing
  322. * everything.
  323. *
  324. * When the filter is initialized, this field is combined using bit AND with
  325. * AVFilterGraph.thread_type to get the final mask used for determining
  326. * allowed threading types. I.e. a threading type needs to be set in both
  327. * to be allowed.
  328. *
  329. * After the filter is initialized, libavfilter sets this field to the
  330. * threading type that is actually used (0 for no multithreading).
  331. */
  332. int thread_type;
  333. /**
  334. * An opaque struct for libavfilter internal use.
  335. */
  336. AVFilterInternal *internal;
  337. struct AVFilterCommand *command_queue;
  338. char *enable_str; ///< enable expression string
  339. void *enable; ///< parsed expression (AVExpr*)
  340. double *var_values; ///< variable values for the enable expression
  341. int is_disabled; ///< the enabled state from the last expression evaluation
  342. /**
  343. * For filters which will create hardware frames, sets the device the
  344. * filter should create them in. All other filters will ignore this field:
  345. * in particular, a filter which consumes or processes hardware frames will
  346. * instead use the hw_frames_ctx field in AVFilterLink to carry the
  347. * hardware context information.
  348. */
  349. AVBufferRef *hw_device_ctx;
  350. /**
  351. * Max number of threads allowed in this filter instance.
  352. * If <= 0, its value is ignored.
  353. * Overrides global number of threads set per filter graph.
  354. */
  355. int nb_threads;
  356. /**
  357. * Ready status of the filter.
  358. * A non-0 value means that the filter needs activating;
  359. * a higher value suggests a more urgent activation.
  360. */
  361. unsigned ready;
  362. };
  363. /**
  364. * A link between two filters. This contains pointers to the source and
  365. * destination filters between which this link exists, and the indexes of
  366. * the pads involved. In addition, this link also contains the parameters
  367. * which have been negotiated and agreed upon between the filter, such as
  368. * image dimensions, format, etc.
  369. *
  370. * Applications must not normally access the link structure directly.
  371. * Use the buffersrc and buffersink API instead.
  372. * In the future, access to the header may be reserved for filters
  373. * implementation.
  374. */
  375. struct AVFilterLink {
  376. AVFilterContext *src; ///< source filter
  377. AVFilterPad *srcpad; ///< output pad on the source filter
  378. AVFilterContext *dst; ///< dest filter
  379. AVFilterPad *dstpad; ///< input pad on the dest filter
  380. enum AVMediaType type; ///< filter media type
  381. /* These parameters apply only to video */
  382. int w; ///< agreed upon image width
  383. int h; ///< agreed upon image height
  384. AVRational sample_aspect_ratio; ///< agreed upon sample aspect ratio
  385. /* These parameters apply only to audio */
  386. uint64_t channel_layout; ///< channel layout of current buffer (see libavutil/channel_layout.h)
  387. int sample_rate; ///< samples per second
  388. int format; ///< agreed upon media format
  389. /**
  390. * Define the time base used by the PTS of the frames/samples
  391. * which will pass through this link.
  392. * During the configuration stage, each filter is supposed to
  393. * change only the output timebase, while the timebase of the
  394. * input link is assumed to be an unchangeable property.
  395. */
  396. AVRational time_base;
  397. /*****************************************************************
  398. * All fields below this line are not part of the public API. They
  399. * may not be used outside of libavfilter and can be changed and
  400. * removed at will.
  401. * New public fields should be added right above.
  402. *****************************************************************
  403. */
  404. /**
  405. * Lists of formats and channel layouts supported by the input and output
  406. * filters respectively. These lists are used for negotiating the format
  407. * to actually be used, which will be loaded into the format and
  408. * channel_layout members, above, when chosen.
  409. *
  410. */
  411. AVFilterFormats *in_formats;
  412. AVFilterFormats *out_formats;
  413. /**
  414. * Lists of channel layouts and sample rates used for automatic
  415. * negotiation.
  416. */
  417. AVFilterFormats *in_samplerates;
  418. AVFilterFormats *out_samplerates;
  419. struct AVFilterChannelLayouts *in_channel_layouts;
  420. struct AVFilterChannelLayouts *out_channel_layouts;
  421. /**
  422. * Audio only, the destination filter sets this to a non-zero value to
  423. * request that buffers with the given number of samples should be sent to
  424. * it. AVFilterPad.needs_fifo must also be set on the corresponding input
  425. * pad.
  426. * Last buffer before EOF will be padded with silence.
  427. */
  428. int request_samples;
  429. /** stage of the initialization of the link properties (dimensions, etc) */
  430. enum {
  431. AVLINK_UNINIT = 0, ///< not started
  432. AVLINK_STARTINIT, ///< started, but incomplete
  433. AVLINK_INIT ///< complete
  434. } init_state;
  435. /**
  436. * Graph the filter belongs to.
  437. */
  438. struct AVFilterGraph *graph;
  439. /**
  440. * Current timestamp of the link, as defined by the most recent
  441. * frame(s), in link time_base units.
  442. */
  443. int64_t current_pts;
  444. /**
  445. * Current timestamp of the link, as defined by the most recent
  446. * frame(s), in AV_TIME_BASE units.
  447. */
  448. int64_t current_pts_us;
  449. /**
  450. * Index in the age array.
  451. */
  452. int age_index;
  453. /**
  454. * Frame rate of the stream on the link, or 1/0 if unknown or variable;
  455. * if left to 0/0, will be automatically copied from the first input
  456. * of the source filter if it exists.
  457. *
  458. * Sources should set it to the best estimation of the real frame rate.
  459. * If the source frame rate is unknown or variable, set this to 1/0.
  460. * Filters should update it if necessary depending on their function.
  461. * Sinks can use it to set a default output frame rate.
  462. * It is similar to the r_frame_rate field in AVStream.
  463. */
  464. AVRational frame_rate;
  465. /**
  466. * Buffer partially filled with samples to achieve a fixed/minimum size.
  467. */
  468. AVFrame *partial_buf;
  469. /**
  470. * Size of the partial buffer to allocate.
  471. * Must be between min_samples and max_samples.
  472. */
  473. int partial_buf_size;
  474. /**
  475. * Minimum number of samples to filter at once. If filter_frame() is
  476. * called with fewer samples, it will accumulate them in partial_buf.
  477. * This field and the related ones must not be changed after filtering
  478. * has started.
  479. * If 0, all related fields are ignored.
  480. */
  481. int min_samples;
  482. /**
  483. * Maximum number of samples to filter at once. If filter_frame() is
  484. * called with more samples, it will split them.
  485. */
  486. int max_samples;
  487. /**
  488. * Number of channels.
  489. */
  490. int channels;
  491. /**
  492. * Link processing flags.
  493. */
  494. unsigned flags;
  495. /**
  496. * Number of past frames sent through the link.
  497. */
  498. int64_t frame_count_in, frame_count_out;
  499. /**
  500. * A pointer to a FFFramePool struct.
  501. */
  502. void *frame_pool;
  503. /**
  504. * True if a frame is currently wanted on the output of this filter.
  505. * Set when ff_request_frame() is called by the output,
  506. * cleared when a frame is filtered.
  507. */
  508. int frame_wanted_out;
  509. /**
  510. * For hwaccel pixel formats, this should be a reference to the
  511. * AVHWFramesContext describing the frames.
  512. */
  513. AVBufferRef *hw_frames_ctx;
  514. #ifndef FF_INTERNAL_FIELDS
  515. /**
  516. * Internal structure members.
  517. * The fields below this limit are internal for libavfilter's use
  518. * and must in no way be accessed by applications.
  519. */
  520. char reserved[0xF000];
  521. #else /* FF_INTERNAL_FIELDS */
  522. /**
  523. * Queue of frames waiting to be filtered.
  524. */
  525. FFFrameQueue fifo;
  526. /**
  527. * If set, the source filter can not generate a frame as is.
  528. * The goal is to avoid repeatedly calling the request_frame() method on
  529. * the same link.
  530. */
  531. int frame_blocked_in;
  532. /**
  533. * Link input status.
  534. * If not zero, all attempts of filter_frame will fail with the
  535. * corresponding code.
  536. */
  537. int status_in;
  538. /**
  539. * Timestamp of the input status change.
  540. */
  541. int64_t status_in_pts;
  542. /**
  543. * Link output status.
  544. * If not zero, all attempts of request_frame will fail with the
  545. * corresponding code.
  546. */
  547. int status_out;
  548. #endif /* FF_INTERNAL_FIELDS */
  549. };
  550. /**
  551. * Link two filters together.
  552. *
  553. * @param src the source filter
  554. * @param srcpad index of the output pad on the source filter
  555. * @param dst the destination filter
  556. * @param dstpad index of the input pad on the destination filter
  557. * @return zero on success
  558. */
  559. int avfilter_link(AVFilterContext *src, unsigned srcpad,
  560. AVFilterContext *dst, unsigned dstpad);
  561. /**
  562. * Free the link in *link, and set its pointer to NULL.
  563. */
  564. void avfilter_link_free(AVFilterLink **link);
  565. /**
  566. * Get the number of channels of a link.
  567. */
  568. int avfilter_link_get_channels(AVFilterLink *link);
  569. /**
  570. * Set the closed field of a link.
  571. * @deprecated applications are not supposed to mess with links, they should
  572. * close the sinks.
  573. */
  574. attribute_deprecated
  575. void avfilter_link_set_closed(AVFilterLink *link, int closed);
  576. /**
  577. * Negotiate the media format, dimensions, etc of all inputs to a filter.
  578. *
  579. * @param filter the filter to negotiate the properties for its inputs
  580. * @return zero on successful negotiation
  581. */
  582. int avfilter_config_links(AVFilterContext *filter);
  583. #define AVFILTER_CMD_FLAG_ONE 1 ///< Stop once a filter understood the command (for target=all for example), fast filters are favored automatically
  584. #define AVFILTER_CMD_FLAG_FAST 2 ///< Only execute command when its fast (like a video out that supports contrast adjustment in hw)
  585. /**
  586. * Make the filter instance process a command.
  587. * It is recommended to use avfilter_graph_send_command().
  588. */
  589. int avfilter_process_command(AVFilterContext *filter, const char *cmd, const char *arg, char *res, int res_len, int flags);
  590. /** Initialize the filter system. Register all builtin filters. */
  591. void avfilter_register_all(void);
  592. /**
  593. * Register a filter. This is only needed if you plan to use
  594. * avfilter_get_by_name later to lookup the AVFilter structure by name. A
  595. * filter can still by instantiated with avfilter_graph_alloc_filter even if it
  596. * is not registered.
  597. *
  598. * @param filter the filter to register
  599. * @return 0 if the registration was successful, a negative value
  600. * otherwise
  601. */
  602. int avfilter_register(AVFilter *filter);
  603. /**
  604. * Get a filter definition matching the given name.
  605. *
  606. * @param name the filter name to find
  607. * @return the filter definition, if any matching one is registered.
  608. * NULL if none found.
  609. */
  610. const AVFilter *avfilter_get_by_name(const char *name);
  611. /**
  612. * Iterate over all registered filters.
  613. * @return If prev is non-NULL, next registered filter after prev or NULL if
  614. * prev is the last filter. If prev is NULL, return the first registered filter.
  615. */
  616. const AVFilter *avfilter_next(const AVFilter *prev);
  617. /**
  618. * Initialize a filter with the supplied parameters.
  619. *
  620. * @param ctx uninitialized filter context to initialize
  621. * @param args Options to initialize the filter with. This must be a
  622. * ':'-separated list of options in the 'key=value' form.
  623. * May be NULL if the options have been set directly using the
  624. * AVOptions API or there are no options that need to be set.
  625. * @return 0 on success, a negative AVERROR on failure
  626. */
  627. int avfilter_init_str(AVFilterContext *ctx, const char *args);
  628. /**
  629. * Initialize a filter with the supplied dictionary of options.
  630. *
  631. * @param ctx uninitialized filter context to initialize
  632. * @param options An AVDictionary filled with options for this filter. On
  633. * return this parameter will be destroyed and replaced with
  634. * a dict containing options that were not found. This dictionary
  635. * must be freed by the caller.
  636. * May be NULL, then this function is equivalent to
  637. * avfilter_init_str() with the second parameter set to NULL.
  638. * @return 0 on success, a negative AVERROR on failure
  639. *
  640. * @note This function and avfilter_init_str() do essentially the same thing,
  641. * the difference is in manner in which the options are passed. It is up to the
  642. * calling code to choose whichever is more preferable. The two functions also
  643. * behave differently when some of the provided options are not declared as
  644. * supported by the filter. In such a case, avfilter_init_str() will fail, but
  645. * this function will leave those extra options in the options AVDictionary and
  646. * continue as usual.
  647. */
  648. int avfilter_init_dict(AVFilterContext *ctx, AVDictionary **options);
  649. /**
  650. * Free a filter context. This will also remove the filter from its
  651. * filtergraph's list of filters.
  652. *
  653. * @param filter the filter to free
  654. */
  655. void avfilter_free(AVFilterContext *filter);
  656. /**
  657. * Insert a filter in the middle of an existing link.
  658. *
  659. * @param link the link into which the filter should be inserted
  660. * @param filt the filter to be inserted
  661. * @param filt_srcpad_idx the input pad on the filter to connect
  662. * @param filt_dstpad_idx the output pad on the filter to connect
  663. * @return zero on success
  664. */
  665. int avfilter_insert_filter(AVFilterLink *link, AVFilterContext *filt,
  666. unsigned filt_srcpad_idx, unsigned filt_dstpad_idx);
  667. /**
  668. * @return AVClass for AVFilterContext.
  669. *
  670. * @see av_opt_find().
  671. */
  672. const AVClass *avfilter_get_class(void);
  673. typedef struct AVFilterGraphInternal AVFilterGraphInternal;
  674. /**
  675. * A function pointer passed to the @ref AVFilterGraph.execute callback to be
  676. * executed multiple times, possibly in parallel.
  677. *
  678. * @param ctx the filter context the job belongs to
  679. * @param arg an opaque parameter passed through from @ref
  680. * AVFilterGraph.execute
  681. * @param jobnr the index of the job being executed
  682. * @param nb_jobs the total number of jobs
  683. *
  684. * @return 0 on success, a negative AVERROR on error
  685. */
  686. typedef int (avfilter_action_func)(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs);
  687. /**
  688. * A function executing multiple jobs, possibly in parallel.
  689. *
  690. * @param ctx the filter context to which the jobs belong
  691. * @param func the function to be called multiple times
  692. * @param arg the argument to be passed to func
  693. * @param ret a nb_jobs-sized array to be filled with return values from each
  694. * invocation of func
  695. * @param nb_jobs the number of jobs to execute
  696. *
  697. * @return 0 on success, a negative AVERROR on error
  698. */
  699. typedef int (avfilter_execute_func)(AVFilterContext *ctx, avfilter_action_func *func,
  700. void *arg, int *ret, int nb_jobs);
  701. typedef struct AVFilterGraph {
  702. const AVClass *av_class;
  703. AVFilterContext **filters;
  704. unsigned nb_filters;
  705. char *scale_sws_opts; ///< sws options to use for the auto-inserted scale filters
  706. #if FF_API_LAVR_OPTS
  707. attribute_deprecated char *resample_lavr_opts; ///< libavresample options to use for the auto-inserted resample filters
  708. #endif
  709. /**
  710. * Type of multithreading allowed for filters in this graph. A combination
  711. * of AVFILTER_THREAD_* flags.
  712. *
  713. * May be set by the caller at any point, the setting will apply to all
  714. * filters initialized after that. The default is allowing everything.
  715. *
  716. * When a filter in this graph is initialized, this field is combined using
  717. * bit AND with AVFilterContext.thread_type to get the final mask used for
  718. * determining allowed threading types. I.e. a threading type needs to be
  719. * set in both to be allowed.
  720. */
  721. int thread_type;
  722. /**
  723. * Maximum number of threads used by filters in this graph. May be set by
  724. * the caller before adding any filters to the filtergraph. Zero (the
  725. * default) means that the number of threads is determined automatically.
  726. */
  727. int nb_threads;
  728. /**
  729. * Opaque object for libavfilter internal use.
  730. */
  731. AVFilterGraphInternal *internal;
  732. /**
  733. * Opaque user data. May be set by the caller to an arbitrary value, e.g. to
  734. * be used from callbacks like @ref AVFilterGraph.execute.
  735. * Libavfilter will not touch this field in any way.
  736. */
  737. void *opaque;
  738. /**
  739. * This callback may be set by the caller immediately after allocating the
  740. * graph and before adding any filters to it, to provide a custom
  741. * multithreading implementation.
  742. *
  743. * If set, filters with slice threading capability will call this callback
  744. * to execute multiple jobs in parallel.
  745. *
  746. * If this field is left unset, libavfilter will use its internal
  747. * implementation, which may or may not be multithreaded depending on the
  748. * platform and build options.
  749. */
  750. avfilter_execute_func *execute;
  751. char *aresample_swr_opts; ///< swr options to use for the auto-inserted aresample filters, Access ONLY through AVOptions
  752. /**
  753. * Private fields
  754. *
  755. * The following fields are for internal use only.
  756. * Their type, offset, number and semantic can change without notice.
  757. */
  758. AVFilterLink **sink_links;
  759. int sink_links_count;
  760. unsigned disable_auto_convert;
  761. } AVFilterGraph;
  762. /**
  763. * Allocate a filter graph.
  764. *
  765. * @return the allocated filter graph on success or NULL.
  766. */
  767. AVFilterGraph *avfilter_graph_alloc(void);
  768. /**
  769. * Create a new filter instance in a filter graph.
  770. *
  771. * @param graph graph in which the new filter will be used
  772. * @param filter the filter to create an instance of
  773. * @param name Name to give to the new instance (will be copied to
  774. * AVFilterContext.name). This may be used by the caller to identify
  775. * different filters, libavfilter itself assigns no semantics to
  776. * this parameter. May be NULL.
  777. *
  778. * @return the context of the newly created filter instance (note that it is
  779. * also retrievable directly through AVFilterGraph.filters or with
  780. * avfilter_graph_get_filter()) on success or NULL on failure.
  781. */
  782. AVFilterContext *avfilter_graph_alloc_filter(AVFilterGraph *graph,
  783. const AVFilter *filter,
  784. const char *name);
  785. /**
  786. * Get a filter instance identified by instance name from graph.
  787. *
  788. * @param graph filter graph to search through.
  789. * @param name filter instance name (should be unique in the graph).
  790. * @return the pointer to the found filter instance or NULL if it
  791. * cannot be found.
  792. */
  793. AVFilterContext *avfilter_graph_get_filter(AVFilterGraph *graph, const char *name);
  794. /**
  795. * Create and add a filter instance into an existing graph.
  796. * The filter instance is created from the filter filt and inited
  797. * with the parameters args and opaque.
  798. *
  799. * In case of success put in *filt_ctx the pointer to the created
  800. * filter instance, otherwise set *filt_ctx to NULL.
  801. *
  802. * @param name the instance name to give to the created filter instance
  803. * @param graph_ctx the filter graph
  804. * @return a negative AVERROR error code in case of failure, a non
  805. * negative value otherwise
  806. */
  807. int avfilter_graph_create_filter(AVFilterContext **filt_ctx, const AVFilter *filt,
  808. const char *name, const char *args, void *opaque,
  809. AVFilterGraph *graph_ctx);
  810. /**
  811. * Enable or disable automatic format conversion inside the graph.
  812. *
  813. * Note that format conversion can still happen inside explicitly inserted
  814. * scale and aresample filters.
  815. *
  816. * @param flags any of the AVFILTER_AUTO_CONVERT_* constants
  817. */
  818. void avfilter_graph_set_auto_convert(AVFilterGraph *graph, unsigned flags);
  819. enum {
  820. AVFILTER_AUTO_CONVERT_ALL = 0, /**< all automatic conversions enabled */
  821. AVFILTER_AUTO_CONVERT_NONE = -1, /**< all automatic conversions disabled */
  822. };
  823. /**
  824. * Check validity and configure all the links and formats in the graph.
  825. *
  826. * @param graphctx the filter graph
  827. * @param log_ctx context used for logging
  828. * @return >= 0 in case of success, a negative AVERROR code otherwise
  829. */
  830. int avfilter_graph_config(AVFilterGraph *graphctx, void *log_ctx);
  831. /**
  832. * Free a graph, destroy its links, and set *graph to NULL.
  833. * If *graph is NULL, do nothing.
  834. */
  835. void avfilter_graph_free(AVFilterGraph **graph);
  836. /**
  837. * A linked-list of the inputs/outputs of the filter chain.
  838. *
  839. * This is mainly useful for avfilter_graph_parse() / avfilter_graph_parse2(),
  840. * where it is used to communicate open (unlinked) inputs and outputs from and
  841. * to the caller.
  842. * This struct specifies, per each not connected pad contained in the graph, the
  843. * filter context and the pad index required for establishing a link.
  844. */
  845. typedef struct AVFilterInOut {
  846. /** unique name for this input/output in the list */
  847. char *name;
  848. /** filter context associated to this input/output */
  849. AVFilterContext *filter_ctx;
  850. /** index of the filt_ctx pad to use for linking */
  851. int pad_idx;
  852. /** next input/input in the list, NULL if this is the last */
  853. struct AVFilterInOut *next;
  854. } AVFilterInOut;
  855. /**
  856. * Allocate a single AVFilterInOut entry.
  857. * Must be freed with avfilter_inout_free().
  858. * @return allocated AVFilterInOut on success, NULL on failure.
  859. */
  860. AVFilterInOut *avfilter_inout_alloc(void);
  861. /**
  862. * Free the supplied list of AVFilterInOut and set *inout to NULL.
  863. * If *inout is NULL, do nothing.
  864. */
  865. void avfilter_inout_free(AVFilterInOut **inout);
  866. /**
  867. * Add a graph described by a string to a graph.
  868. *
  869. * @note The caller must provide the lists of inputs and outputs,
  870. * which therefore must be known before calling the function.
  871. *
  872. * @note The inputs parameter describes inputs of the already existing
  873. * part of the graph; i.e. from the point of view of the newly created
  874. * part, they are outputs. Similarly the outputs parameter describes
  875. * outputs of the already existing filters, which are provided as
  876. * inputs to the parsed filters.
  877. *
  878. * @param graph the filter graph where to link the parsed graph context
  879. * @param filters string to be parsed
  880. * @param inputs linked list to the inputs of the graph
  881. * @param outputs linked list to the outputs of the graph
  882. * @return zero on success, a negative AVERROR code on error
  883. */
  884. int avfilter_graph_parse(AVFilterGraph *graph, const char *filters,
  885. AVFilterInOut *inputs, AVFilterInOut *outputs,
  886. void *log_ctx);
  887. /**
  888. * Add a graph described by a string to a graph.
  889. *
  890. * In the graph filters description, if the input label of the first
  891. * filter is not specified, "in" is assumed; if the output label of
  892. * the last filter is not specified, "out" is assumed.
  893. *
  894. * @param graph the filter graph where to link the parsed graph context
  895. * @param filters string to be parsed
  896. * @param inputs pointer to a linked list to the inputs of the graph, may be NULL.
  897. * If non-NULL, *inputs is updated to contain the list of open inputs
  898. * after the parsing, should be freed with avfilter_inout_free().
  899. * @param outputs pointer to a linked list to the outputs of the graph, may be NULL.
  900. * If non-NULL, *outputs is updated to contain the list of open outputs
  901. * after the parsing, should be freed with avfilter_inout_free().
  902. * @return non negative on success, a negative AVERROR code on error
  903. */
  904. int avfilter_graph_parse_ptr(AVFilterGraph *graph, const char *filters,
  905. AVFilterInOut **inputs, AVFilterInOut **outputs,
  906. void *log_ctx);
  907. /**
  908. * Add a graph described by a string to a graph.
  909. *
  910. * @param[in] graph the filter graph where to link the parsed graph context
  911. * @param[in] filters string to be parsed
  912. * @param[out] inputs a linked list of all free (unlinked) inputs of the
  913. * parsed graph will be returned here. It is to be freed
  914. * by the caller using avfilter_inout_free().
  915. * @param[out] outputs a linked list of all free (unlinked) outputs of the
  916. * parsed graph will be returned here. It is to be freed by the
  917. * caller using avfilter_inout_free().
  918. * @return zero on success, a negative AVERROR code on error
  919. *
  920. * @note This function returns the inputs and outputs that are left
  921. * unlinked after parsing the graph and the caller then deals with
  922. * them.
  923. * @note This function makes no reference whatsoever to already
  924. * existing parts of the graph and the inputs parameter will on return
  925. * contain inputs of the newly parsed part of the graph. Analogously
  926. * the outputs parameter will contain outputs of the newly created
  927. * filters.
  928. */
  929. int avfilter_graph_parse2(AVFilterGraph *graph, const char *filters,
  930. AVFilterInOut **inputs,
  931. AVFilterInOut **outputs);
  932. /**
  933. * Send a command to one or more filter instances.
  934. *
  935. * @param graph the filter graph
  936. * @param target the filter(s) to which the command should be sent
  937. * "all" sends to all filters
  938. * otherwise it can be a filter or filter instance name
  939. * which will send the command to all matching filters.
  940. * @param cmd the command to send, for handling simplicity all commands must be alphanumeric only
  941. * @param arg the argument for the command
  942. * @param res a buffer with size res_size where the filter(s) can return a response.
  943. *
  944. * @returns >=0 on success otherwise an error code.
  945. * AVERROR(ENOSYS) on unsupported commands
  946. */
  947. int avfilter_graph_send_command(AVFilterGraph *graph, const char *target, const char *cmd, const char *arg, char *res, int res_len, int flags);
  948. /**
  949. * Queue a command for one or more filter instances.
  950. *
  951. * @param graph the filter graph
  952. * @param target the filter(s) to which the command should be sent
  953. * "all" sends to all filters
  954. * otherwise it can be a filter or filter instance name
  955. * which will send the command to all matching filters.
  956. * @param cmd the command to sent, for handling simplicity all commands must be alphanumeric only
  957. * @param arg the argument for the command
  958. * @param ts time at which the command should be sent to the filter
  959. *
  960. * @note As this executes commands after this function returns, no return code
  961. * from the filter is provided, also AVFILTER_CMD_FLAG_ONE is not supported.
  962. */
  963. int avfilter_graph_queue_command(AVFilterGraph *graph, const char *target, const char *cmd, const char *arg, int flags, double ts);
  964. /**
  965. * Dump a graph into a human-readable string representation.
  966. *
  967. * @param graph the graph to dump
  968. * @param options formatting options; currently ignored
  969. * @return a string, or NULL in case of memory allocation failure;
  970. * the string must be freed using av_free
  971. */
  972. char *avfilter_graph_dump(AVFilterGraph *graph, const char *options);
  973. /**
  974. * Request a frame on the oldest sink link.
  975. *
  976. * If the request returns AVERROR_EOF, try the next.
  977. *
  978. * Note that this function is not meant to be the sole scheduling mechanism
  979. * of a filtergraph, only a convenience function to help drain a filtergraph
  980. * in a balanced way under normal circumstances.
  981. *
  982. * Also note that AVERROR_EOF does not mean that frames did not arrive on
  983. * some of the sinks during the process.
  984. * When there are multiple sink links, in case the requested link
  985. * returns an EOF, this may cause a filter to flush pending frames
  986. * which are sent to another sink link, although unrequested.
  987. *
  988. * @return the return value of ff_request_frame(),
  989. * or AVERROR_EOF if all links returned AVERROR_EOF
  990. */
  991. int avfilter_graph_request_oldest(AVFilterGraph *graph);
  992. /**
  993. * @}
  994. */
  995. #endif /* AVFILTER_AVFILTER_H */