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.

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