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.

1028 lines
39KB

  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. #include "libavutil/avutil.h"
  24. #include "libavutil/log.h"
  25. #include "libavutil/samplefmt.h"
  26. #include "libavutil/pixfmt.h"
  27. #include "libavutil/rational.h"
  28. #include "libavcodec/avcodec.h"
  29. #ifndef FF_API_OLD_VSINK_API
  30. #define FF_API_OLD_VSINK_API (LIBAVFILTER_VERSION_MAJOR < 3)
  31. #endif
  32. #ifndef FF_API_OLD_ALL_FORMATS_API
  33. #define FF_API_OLD_ALL_FORMATS_API (LIBAVFILTER_VERSION_MAJOR < 3)
  34. #endif
  35. #include <stddef.h>
  36. #include "libavfilter/version.h"
  37. /**
  38. * Return the LIBAVFILTER_VERSION_INT constant.
  39. */
  40. unsigned avfilter_version(void);
  41. /**
  42. * Return the libavfilter build-time configuration.
  43. */
  44. const char *avfilter_configuration(void);
  45. /**
  46. * Return the libavfilter license.
  47. */
  48. const char *avfilter_license(void);
  49. typedef struct AVFilterContext AVFilterContext;
  50. typedef struct AVFilterLink AVFilterLink;
  51. typedef struct AVFilterPad AVFilterPad;
  52. /**
  53. * A reference-counted buffer data type used by the filter system. Filters
  54. * should not store pointers to this structure directly, but instead use the
  55. * AVFilterBufferRef structure below.
  56. */
  57. typedef struct AVFilterBuffer {
  58. uint8_t *data[8]; ///< buffer data for each plane/channel
  59. int linesize[8]; ///< number of bytes per line
  60. unsigned refcount; ///< number of references to this buffer
  61. /** private data to be used by a custom free function */
  62. void *priv;
  63. /**
  64. * A pointer to the function to deallocate this buffer if the default
  65. * function is not sufficient. This could, for example, add the memory
  66. * back into a memory pool to be reused later without the overhead of
  67. * reallocating it from scratch.
  68. */
  69. void (*free)(struct AVFilterBuffer *buf);
  70. int format; ///< media format
  71. int w, h; ///< width and height of the allocated buffer
  72. /**
  73. * pointers to the data planes/channels.
  74. *
  75. * For video, this should simply point to data[].
  76. *
  77. * For planar audio, each channel has a separate data pointer, and
  78. * linesize[0] contains the size of each channel buffer.
  79. * For packed audio, there is just one data pointer, and linesize[0]
  80. * contains the total size of the buffer for all channels.
  81. *
  82. * Note: Both data and extended_data will always be set, but for planar
  83. * audio with more channels that can fit in data, extended_data must be used
  84. * in order to access all channels.
  85. */
  86. uint8_t **extended_data;
  87. } AVFilterBuffer;
  88. #define AV_PERM_READ 0x01 ///< can read from the buffer
  89. #define AV_PERM_WRITE 0x02 ///< can write to the buffer
  90. #define AV_PERM_PRESERVE 0x04 ///< nobody else can overwrite the buffer
  91. #define AV_PERM_REUSE 0x08 ///< can output the buffer multiple times, with the same contents each time
  92. #define AV_PERM_REUSE2 0x10 ///< can output the buffer multiple times, modified each time
  93. #define AV_PERM_NEG_LINESIZES 0x20 ///< the buffer requested can have negative linesizes
  94. #define AV_PERM_ALIGN 0x40 ///< the buffer must be aligned
  95. #define AVFILTER_ALIGN 16 //not part of ABI
  96. /**
  97. * Audio specific properties in a reference to an AVFilterBuffer. Since
  98. * AVFilterBufferRef is common to different media formats, audio specific
  99. * per reference properties must be separated out.
  100. */
  101. typedef struct AVFilterBufferRefAudioProps {
  102. uint64_t channel_layout; ///< channel layout of audio buffer
  103. int nb_samples; ///< number of audio samples per channel
  104. int sample_rate; ///< audio buffer sample rate
  105. int planar; ///< audio buffer - planar or packed
  106. } AVFilterBufferRefAudioProps;
  107. /**
  108. * Video specific properties in a reference to an AVFilterBuffer. Since
  109. * AVFilterBufferRef is common to different media formats, video specific
  110. * per reference properties must be separated out.
  111. */
  112. typedef struct AVFilterBufferRefVideoProps {
  113. int w; ///< image width
  114. int h; ///< image height
  115. AVRational sample_aspect_ratio; ///< sample aspect ratio
  116. int interlaced; ///< is frame interlaced
  117. int top_field_first; ///< field order
  118. enum AVPictureType pict_type; ///< picture type of the frame
  119. int key_frame; ///< 1 -> keyframe, 0-> not
  120. } AVFilterBufferRefVideoProps;
  121. /**
  122. * A reference to an AVFilterBuffer. Since filters can manipulate the origin of
  123. * a buffer to, for example, crop image without any memcpy, the buffer origin
  124. * and dimensions are per-reference properties. Linesize is also useful for
  125. * image flipping, frame to field filters, etc, and so is also per-reference.
  126. *
  127. * TODO: add anything necessary for frame reordering
  128. */
  129. typedef struct AVFilterBufferRef {
  130. AVFilterBuffer *buf; ///< the buffer that this is a reference to
  131. uint8_t *data[8]; ///< picture/audio data for each plane
  132. int linesize[8]; ///< number of bytes per line
  133. int format; ///< media format
  134. /**
  135. * presentation timestamp. The time unit may change during
  136. * filtering, as it is specified in the link and the filter code
  137. * may need to rescale the PTS accordingly.
  138. */
  139. int64_t pts;
  140. int64_t pos; ///< byte position in stream, -1 if unknown
  141. int perms; ///< permissions, see the AV_PERM_* flags
  142. enum AVMediaType type; ///< media type of buffer data
  143. AVFilterBufferRefVideoProps *video; ///< video buffer specific properties
  144. AVFilterBufferRefAudioProps *audio; ///< audio buffer specific properties
  145. /**
  146. * pointers to the data planes/channels.
  147. *
  148. * For video, this should simply point to data[].
  149. *
  150. * For planar audio, each channel has a separate data pointer, and
  151. * linesize[0] contains the size of each channel buffer.
  152. * For packed audio, there is just one data pointer, and linesize[0]
  153. * contains the total size of the buffer for all channels.
  154. *
  155. * Note: Both data and extended_data will always be set, but for planar
  156. * audio with more channels that can fit in data, extended_data must be used
  157. * in order to access all channels.
  158. */
  159. uint8_t **extended_data;
  160. } AVFilterBufferRef;
  161. /**
  162. * Copy properties of src to dst, without copying the actual data
  163. */
  164. void avfilter_copy_buffer_ref_props(AVFilterBufferRef *dst, AVFilterBufferRef *src);
  165. /**
  166. * Add a new reference to a buffer.
  167. *
  168. * @param ref an existing reference to the buffer
  169. * @param pmask a bitmask containing the allowable permissions in the new
  170. * reference
  171. * @return a new reference to the buffer with the same properties as the
  172. * old, excluding any permissions denied by pmask
  173. */
  174. AVFilterBufferRef *avfilter_ref_buffer(AVFilterBufferRef *ref, int pmask);
  175. /**
  176. * Remove a reference to a buffer. If this is the last reference to the
  177. * buffer, the buffer itself is also automatically freed.
  178. *
  179. * @param ref reference to the buffer, may be NULL
  180. */
  181. void avfilter_unref_buffer(AVFilterBufferRef *ref);
  182. /**
  183. * Remove a reference to a buffer and set the pointer to NULL.
  184. * If this is the last reference to the buffer, the buffer itself
  185. * is also automatically freed.
  186. *
  187. * @param ref pointer to the buffer reference
  188. */
  189. void avfilter_unref_bufferp(AVFilterBufferRef **ref);
  190. /**
  191. * A list of supported formats for one end of a filter link. This is used
  192. * during the format negotiation process to try to pick the best format to
  193. * use to minimize the number of necessary conversions. Each filter gives a
  194. * list of the formats supported by each input and output pad. The list
  195. * given for each pad need not be distinct - they may be references to the
  196. * same list of formats, as is often the case when a filter supports multiple
  197. * formats, but will always output the same format as it is given in input.
  198. *
  199. * In this way, a list of possible input formats and a list of possible
  200. * output formats are associated with each link. When a set of formats is
  201. * negotiated over a link, the input and output lists are merged to form a
  202. * new list containing only the common elements of each list. In the case
  203. * that there were no common elements, a format conversion is necessary.
  204. * Otherwise, the lists are merged, and all other links which reference
  205. * either of the format lists involved in the merge are also affected.
  206. *
  207. * For example, consider the filter chain:
  208. * filter (a) --> (b) filter (b) --> (c) filter
  209. *
  210. * where the letters in parenthesis indicate a list of formats supported on
  211. * the input or output of the link. Suppose the lists are as follows:
  212. * (a) = {A, B}
  213. * (b) = {A, B, C}
  214. * (c) = {B, C}
  215. *
  216. * First, the first link's lists are merged, yielding:
  217. * filter (a) --> (a) filter (a) --> (c) filter
  218. *
  219. * Notice that format list (b) now refers to the same list as filter list (a).
  220. * Next, the lists for the second link are merged, yielding:
  221. * filter (a) --> (a) filter (a) --> (a) filter
  222. *
  223. * where (a) = {B}.
  224. *
  225. * Unfortunately, when the format lists at the two ends of a link are merged,
  226. * we must ensure that all links which reference either pre-merge format list
  227. * get updated as well. Therefore, we have the format list structure store a
  228. * pointer to each of the pointers to itself.
  229. */
  230. typedef struct AVFilterFormats {
  231. unsigned format_count; ///< number of formats
  232. int64_t *formats; ///< list of media formats
  233. unsigned refcount; ///< number of references to this list
  234. struct AVFilterFormats ***refs; ///< references to this list
  235. } AVFilterFormats;
  236. /**
  237. * Create a list of supported formats. This is intended for use in
  238. * AVFilter->query_formats().
  239. *
  240. * @param fmts list of media formats, terminated by -1. If NULL an
  241. * empty list is created.
  242. * @return the format list, with no existing references
  243. */
  244. AVFilterFormats *avfilter_make_format_list(const int *fmts);
  245. AVFilterFormats *avfilter_make_format64_list(const int64_t *fmts);
  246. /**
  247. * Add fmt to the list of media formats contained in *avff.
  248. * If *avff is NULL the function allocates the filter formats struct
  249. * and puts its pointer in *avff.
  250. *
  251. * @return a non negative value in case of success, or a negative
  252. * value corresponding to an AVERROR code in case of error
  253. */
  254. int avfilter_add_format(AVFilterFormats **avff, int64_t fmt);
  255. #if FF_API_OLD_ALL_FORMATS_API
  256. /**
  257. * @deprecated Use avfilter_make_all_formats() instead.
  258. */
  259. attribute_deprecated
  260. AVFilterFormats *avfilter_all_formats(enum AVMediaType type);
  261. #endif
  262. /**
  263. * Return a list of all formats supported by FFmpeg for the given media type.
  264. */
  265. AVFilterFormats *avfilter_make_all_formats(enum AVMediaType type);
  266. /**
  267. * A list of all channel layouts supported by libavfilter.
  268. */
  269. extern const int64_t avfilter_all_channel_layouts[];
  270. /**
  271. * Return a list of all channel layouts supported by FFmpeg.
  272. */
  273. AVFilterFormats *avfilter_make_all_channel_layouts(void);
  274. /**
  275. * Return a list of all audio packing formats.
  276. */
  277. AVFilterFormats *avfilter_make_all_packing_formats(void);
  278. /**
  279. * Return a format list which contains the intersection of the formats of
  280. * a and b. Also, all the references of a, all the references of b, and
  281. * a and b themselves will be deallocated.
  282. *
  283. * If a and b do not share any common formats, neither is modified, and NULL
  284. * is returned.
  285. */
  286. AVFilterFormats *avfilter_merge_formats(AVFilterFormats *a, AVFilterFormats *b);
  287. /**
  288. * Add *ref as a new reference to formats.
  289. * That is the pointers will point like in the ASCII art below:
  290. * ________
  291. * |formats |<--------.
  292. * | ____ | ____|___________________
  293. * | |refs| | | __|_
  294. * | |* * | | | | | | AVFilterLink
  295. * | |* *--------->|*ref|
  296. * | |____| | | |____|
  297. * |________| |________________________
  298. */
  299. void avfilter_formats_ref(AVFilterFormats *formats, AVFilterFormats **ref);
  300. /**
  301. * If *ref is non-NULL, remove *ref as a reference to the format list
  302. * it currently points to, deallocates that list if this was the last
  303. * reference, and sets *ref to NULL.
  304. *
  305. * Before After
  306. * ________ ________ NULL
  307. * |formats |<--------. |formats | ^
  308. * | ____ | ____|________________ | ____ | ____|________________
  309. * | |refs| | | __|_ | |refs| | | __|_
  310. * | |* * | | | | | | AVFilterLink | |* * | | | | | | AVFilterLink
  311. * | |* *--------->|*ref| | |* | | | |*ref|
  312. * | |____| | | |____| | |____| | | |____|
  313. * |________| |_____________________ |________| |_____________________
  314. */
  315. void avfilter_formats_unref(AVFilterFormats **ref);
  316. /**
  317. *
  318. * Before After
  319. * ________ ________
  320. * |formats |<---------. |formats |<---------.
  321. * | ____ | ___|___ | ____ | ___|___
  322. * | |refs| | | | | | |refs| | | | | NULL
  323. * | |* *--------->|*oldref| | |* *--------->|*newref| ^
  324. * | |* * | | |_______| | |* * | | |_______| ___|___
  325. * | |____| | | |____| | | | |
  326. * |________| |________| |*oldref|
  327. * |_______|
  328. */
  329. void avfilter_formats_changeref(AVFilterFormats **oldref,
  330. AVFilterFormats **newref);
  331. /**
  332. * A filter pad used for either input or output.
  333. */
  334. struct AVFilterPad {
  335. /**
  336. * Pad name. The name is unique among inputs and among outputs, but an
  337. * input may have the same name as an output. This may be NULL if this
  338. * pad has no need to ever be referenced by name.
  339. */
  340. const char *name;
  341. /**
  342. * AVFilterPad type. Can be AVMEDIA_TYPE_VIDEO or AVMEDIA_TYPE_AUDIO.
  343. */
  344. enum AVMediaType type;
  345. /**
  346. * Minimum required permissions on incoming buffers. Any buffer with
  347. * insufficient permissions will be automatically copied by the filter
  348. * system to a new buffer which provides the needed access permissions.
  349. *
  350. * Input pads only.
  351. */
  352. int min_perms;
  353. /**
  354. * Permissions which are not accepted on incoming buffers. Any buffer
  355. * which has any of these permissions set will be automatically copied
  356. * by the filter system to a new buffer which does not have those
  357. * permissions. This can be used to easily disallow buffers with
  358. * AV_PERM_REUSE.
  359. *
  360. * Input pads only.
  361. */
  362. int rej_perms;
  363. /**
  364. * Callback called before passing the first slice of a new frame. If
  365. * NULL, the filter layer will default to storing a reference to the
  366. * picture inside the link structure.
  367. *
  368. * Input video pads only.
  369. */
  370. void (*start_frame)(AVFilterLink *link, AVFilterBufferRef *picref);
  371. /**
  372. * Callback function to get a video buffer. If NULL, the filter system will
  373. * use avfilter_default_get_video_buffer().
  374. *
  375. * Input video pads only.
  376. */
  377. AVFilterBufferRef *(*get_video_buffer)(AVFilterLink *link, int perms, int w, int h);
  378. /**
  379. * Callback function to get an audio buffer. If NULL, the filter system will
  380. * use avfilter_default_get_audio_buffer().
  381. *
  382. * Input audio pads only.
  383. */
  384. AVFilterBufferRef *(*get_audio_buffer)(AVFilterLink *link, int perms, int nb_samples);
  385. /**
  386. * Callback called after the slices of a frame are completely sent. If
  387. * NULL, the filter layer will default to releasing the reference stored
  388. * in the link structure during start_frame().
  389. *
  390. * Input video pads only.
  391. */
  392. void (*end_frame)(AVFilterLink *link);
  393. /**
  394. * Slice drawing callback. This is where a filter receives video data
  395. * and should do its processing.
  396. *
  397. * Input video pads only.
  398. */
  399. void (*draw_slice)(AVFilterLink *link, int y, int height, int slice_dir);
  400. /**
  401. * Samples filtering callback. This is where a filter receives audio data
  402. * and should do its processing.
  403. *
  404. * Input audio pads only.
  405. */
  406. void (*filter_samples)(AVFilterLink *link, AVFilterBufferRef *samplesref);
  407. /**
  408. * Frame poll callback. This returns the number of immediately available
  409. * samples. It should return a positive value if the next request_frame()
  410. * is guaranteed to return one frame (with no delay).
  411. *
  412. * Defaults to just calling the source poll_frame() method.
  413. *
  414. * Output video pads only.
  415. */
  416. int (*poll_frame)(AVFilterLink *link);
  417. /**
  418. * Frame request callback. A call to this should result in at least one
  419. * frame being output over the given link. This should return zero on
  420. * success, and another value on error.
  421. * See avfilter_request_frame() for the error codes with a specific
  422. * meaning.
  423. *
  424. * Output video pads only.
  425. */
  426. int (*request_frame)(AVFilterLink *link);
  427. /**
  428. * Link configuration callback.
  429. *
  430. * For output pads, this should set the following link properties:
  431. * video: width, height, sample_aspect_ratio, time_base
  432. * audio: sample_rate.
  433. *
  434. * This should NOT set properties such as format, channel_layout, etc which
  435. * are negotiated between filters by the filter system using the
  436. * query_formats() callback before this function is called.
  437. *
  438. * For input pads, this should check the properties of the link, and update
  439. * the filter's internal state as necessary.
  440. *
  441. * For both input and output pads, this should return zero on success,
  442. * and another value on error.
  443. */
  444. int (*config_props)(AVFilterLink *link);
  445. };
  446. /** default handler for start_frame() for video inputs */
  447. void avfilter_default_start_frame(AVFilterLink *link, AVFilterBufferRef *picref);
  448. /** default handler for draw_slice() for video inputs */
  449. void avfilter_default_draw_slice(AVFilterLink *link, int y, int h, int slice_dir);
  450. /** default handler for end_frame() for video inputs */
  451. void avfilter_default_end_frame(AVFilterLink *link);
  452. /** default handler for filter_samples() for audio inputs */
  453. void avfilter_default_filter_samples(AVFilterLink *link, AVFilterBufferRef *samplesref);
  454. /** default handler for get_video_buffer() for video inputs */
  455. AVFilterBufferRef *avfilter_default_get_video_buffer(AVFilterLink *link,
  456. int perms, int w, int h);
  457. /** default handler for get_audio_buffer() for audio inputs */
  458. AVFilterBufferRef *avfilter_default_get_audio_buffer(AVFilterLink *link,
  459. int perms, int nb_samples);
  460. /**
  461. * Helpers for query_formats() which set all links to the same list of
  462. * formats/layouts. If there are no links hooked to this filter, the list
  463. * of formats is freed.
  464. */
  465. void avfilter_set_common_pixel_formats(AVFilterContext *ctx, AVFilterFormats *formats);
  466. void avfilter_set_common_sample_formats(AVFilterContext *ctx, AVFilterFormats *formats);
  467. void avfilter_set_common_channel_layouts(AVFilterContext *ctx, AVFilterFormats *formats);
  468. void avfilter_set_common_packing_formats(AVFilterContext *ctx, AVFilterFormats *formats);
  469. /** Default handler for query_formats() */
  470. int avfilter_default_query_formats(AVFilterContext *ctx);
  471. /** start_frame() handler for filters which simply pass video along */
  472. void avfilter_null_start_frame(AVFilterLink *link, AVFilterBufferRef *picref);
  473. /** draw_slice() handler for filters which simply pass video along */
  474. void avfilter_null_draw_slice(AVFilterLink *link, int y, int h, int slice_dir);
  475. /** end_frame() handler for filters which simply pass video along */
  476. void avfilter_null_end_frame(AVFilterLink *link);
  477. /** filter_samples() handler for filters which simply pass audio along */
  478. void avfilter_null_filter_samples(AVFilterLink *link, AVFilterBufferRef *samplesref);
  479. /** get_video_buffer() handler for filters which simply pass video along */
  480. AVFilterBufferRef *avfilter_null_get_video_buffer(AVFilterLink *link,
  481. int perms, int w, int h);
  482. /** get_audio_buffer() handler for filters which simply pass audio along */
  483. AVFilterBufferRef *avfilter_null_get_audio_buffer(AVFilterLink *link,
  484. int perms, int nb_samples);
  485. /**
  486. * Filter definition. This defines the pads a filter contains, and all the
  487. * callback functions used to interact with the filter.
  488. */
  489. typedef struct AVFilter {
  490. const char *name; ///< filter name
  491. int priv_size; ///< size of private data to allocate for the filter
  492. /**
  493. * Filter initialization function. Args contains the user-supplied
  494. * parameters. FIXME: maybe an AVOption-based system would be better?
  495. * opaque is data provided by the code requesting creation of the filter,
  496. * and is used to pass data to the filter.
  497. */
  498. int (*init)(AVFilterContext *ctx, const char *args, void *opaque);
  499. /**
  500. * Filter uninitialization function. Should deallocate any memory held
  501. * by the filter, release any buffer references, etc. This does not need
  502. * to deallocate the AVFilterContext->priv memory itself.
  503. */
  504. void (*uninit)(AVFilterContext *ctx);
  505. /**
  506. * Queries formats/layouts supported by the filter and its pads, and sets
  507. * the in_formats/in_chlayouts for links connected to its output pads,
  508. * and out_formats/out_chlayouts for links connected to its input pads.
  509. *
  510. * @return zero on success, a negative value corresponding to an
  511. * AVERROR code otherwise
  512. */
  513. int (*query_formats)(AVFilterContext *);
  514. const AVFilterPad *inputs; ///< NULL terminated list of inputs. NULL if none
  515. const AVFilterPad *outputs; ///< NULL terminated list of outputs. NULL if none
  516. /**
  517. * A description for the filter. You should use the
  518. * NULL_IF_CONFIG_SMALL() macro to define it.
  519. */
  520. const char *description;
  521. /**
  522. * Make the filter instance process a command.
  523. *
  524. * @param cmd the command to process, for handling simplicity all commands must be alphanumeric only
  525. * @param arg the argument for the command
  526. * @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.
  527. * @param flags if AVFILTER_CMD_FLAG_FAST is set and the command would be
  528. * time consuming then a filter should treat it like an unsupported command
  529. *
  530. * @returns >=0 on success otherwise an error code.
  531. * AVERROR(ENOSYS) on unsupported commands
  532. */
  533. int (*process_command)(AVFilterContext *, const char *cmd, const char *arg, char *res, int res_len, int flags);
  534. } AVFilter;
  535. /** An instance of a filter */
  536. struct AVFilterContext {
  537. const AVClass *av_class; ///< needed for av_log()
  538. AVFilter *filter; ///< the AVFilter of which this is an instance
  539. char *name; ///< name of this filter instance
  540. unsigned input_count; ///< number of input pads
  541. AVFilterPad *input_pads; ///< array of input pads
  542. AVFilterLink **inputs; ///< array of pointers to input links
  543. unsigned output_count; ///< number of output pads
  544. AVFilterPad *output_pads; ///< array of output pads
  545. AVFilterLink **outputs; ///< array of pointers to output links
  546. void *priv; ///< private data for use by the filter
  547. struct AVFilterCommand *command_queue;
  548. };
  549. enum AVFilterPacking {
  550. AVFILTER_PACKED = 0,
  551. AVFILTER_PLANAR,
  552. };
  553. /**
  554. * A link between two filters. This contains pointers to the source and
  555. * destination filters between which this link exists, and the indexes of
  556. * the pads involved. In addition, this link also contains the parameters
  557. * which have been negotiated and agreed upon between the filter, such as
  558. * image dimensions, format, etc.
  559. */
  560. struct AVFilterLink {
  561. AVFilterContext *src; ///< source filter
  562. AVFilterPad *srcpad; ///< output pad on the source filter
  563. AVFilterContext *dst; ///< dest filter
  564. AVFilterPad *dstpad; ///< input pad on the dest filter
  565. /** stage of the initialization of the link properties (dimensions, etc) */
  566. enum {
  567. AVLINK_UNINIT = 0, ///< not started
  568. AVLINK_STARTINIT, ///< started, but incomplete
  569. AVLINK_INIT ///< complete
  570. } init_state;
  571. enum AVMediaType type; ///< filter media type
  572. /* These parameters apply only to video */
  573. int w; ///< agreed upon image width
  574. int h; ///< agreed upon image height
  575. AVRational sample_aspect_ratio; ///< agreed upon sample aspect ratio
  576. /* These parameters apply only to audio */
  577. uint64_t channel_layout; ///< channel layout of current buffer (see libavutil/audioconvert.h)
  578. #if LIBAVFILTER_VERSION_MAJOR < 3
  579. int64_t sample_rate; ///< samples per second
  580. #else
  581. int sample_rate; ///< samples per second
  582. #endif
  583. int planar; ///< agreed upon packing mode of audio buffers. true if planar.
  584. int format; ///< agreed upon media format
  585. /**
  586. * Lists of formats and channel layouts supported by the input and output
  587. * filters respectively. These lists are used for negotiating the format
  588. * to actually be used, which will be loaded into the format and
  589. * channel_layout members, above, when chosen.
  590. *
  591. */
  592. AVFilterFormats *in_formats;
  593. AVFilterFormats *out_formats;
  594. AVFilterFormats *in_chlayouts;
  595. AVFilterFormats *out_chlayouts;
  596. AVFilterFormats *in_packing;
  597. AVFilterFormats *out_packing;
  598. /**
  599. * The buffer reference currently being sent across the link by the source
  600. * filter. This is used internally by the filter system to allow
  601. * automatic copying of buffers which do not have sufficient permissions
  602. * for the destination. This should not be accessed directly by the
  603. * filters.
  604. */
  605. AVFilterBufferRef *src_buf;
  606. AVFilterBufferRef *cur_buf;
  607. AVFilterBufferRef *out_buf;
  608. /**
  609. * Define the time base used by the PTS of the frames/samples
  610. * which will pass through this link.
  611. * During the configuration stage, each filter is supposed to
  612. * change only the output timebase, while the timebase of the
  613. * input link is assumed to be an unchangeable property.
  614. */
  615. AVRational time_base;
  616. struct AVFilterPool *pool;
  617. /**
  618. * Graph the filter belongs to.
  619. */
  620. struct AVFilterGraph *graph;
  621. /**
  622. * Current timestamp of the link, as defined by the most recent
  623. * frame(s), in AV_TIME_BASE units.
  624. */
  625. int64_t current_pts;
  626. /**
  627. * Private fields
  628. *
  629. * The following fields are for internal use only.
  630. * Their type, offset, number and semantic can change without notice.
  631. */
  632. /**
  633. * Index in the age array.
  634. */
  635. int age_index;
  636. };
  637. /**
  638. * Link two filters together.
  639. *
  640. * @param src the source filter
  641. * @param srcpad index of the output pad on the source filter
  642. * @param dst the destination filter
  643. * @param dstpad index of the input pad on the destination filter
  644. * @return zero on success
  645. */
  646. int avfilter_link(AVFilterContext *src, unsigned srcpad,
  647. AVFilterContext *dst, unsigned dstpad);
  648. /**
  649. * Free the link in *link, and set its pointer to NULL.
  650. */
  651. void avfilter_link_free(AVFilterLink **link);
  652. /**
  653. * Negotiate the media format, dimensions, etc of all inputs to a filter.
  654. *
  655. * @param filter the filter to negotiate the properties for its inputs
  656. * @return zero on successful negotiation
  657. */
  658. int avfilter_config_links(AVFilterContext *filter);
  659. /**
  660. * Request a picture buffer with a specific set of permissions.
  661. *
  662. * @param link the output link to the filter from which the buffer will
  663. * be requested
  664. * @param perms the required access permissions
  665. * @param w the minimum width of the buffer to allocate
  666. * @param h the minimum height of the buffer to allocate
  667. * @return A reference to the buffer. This must be unreferenced with
  668. * avfilter_unref_buffer when you are finished with it.
  669. */
  670. AVFilterBufferRef *avfilter_get_video_buffer(AVFilterLink *link, int perms,
  671. int w, int h);
  672. /**
  673. * Create a buffer reference wrapped around an already allocated image
  674. * buffer.
  675. *
  676. * @param data pointers to the planes of the image to reference
  677. * @param linesize linesizes for the planes of the image to reference
  678. * @param perms the required access permissions
  679. * @param w the width of the image specified by the data and linesize arrays
  680. * @param h the height of the image specified by the data and linesize arrays
  681. * @param format the pixel format of the image specified by the data and linesize arrays
  682. */
  683. AVFilterBufferRef *
  684. avfilter_get_video_buffer_ref_from_arrays(uint8_t * const data[4], const int linesize[4], int perms,
  685. int w, int h, enum PixelFormat format);
  686. /**
  687. * Request an audio samples buffer with a specific set of permissions.
  688. *
  689. * @param link the output link to the filter from which the buffer will
  690. * be requested
  691. * @param perms the required access permissions
  692. * @param nb_samples the number of samples per channel
  693. * @return A reference to the samples. This must be unreferenced with
  694. * avfilter_unref_buffer when you are finished with it.
  695. */
  696. AVFilterBufferRef *avfilter_get_audio_buffer(AVFilterLink *link, int perms,
  697. int nb_samples);
  698. /**
  699. * Create an audio buffer reference wrapped around an already
  700. * allocated samples buffer.
  701. *
  702. * @param data pointers to the samples plane buffers
  703. * @param linesize linesize for the samples plane buffers
  704. * @param perms the required access permissions
  705. * @param nb_samples number of samples per channel
  706. * @param sample_fmt the format of each sample in the buffer to allocate
  707. * @param channel_layout the channel layout of the buffer
  708. * @param planar audio data layout - planar or packed
  709. */
  710. AVFilterBufferRef *avfilter_get_audio_buffer_ref_from_arrays(uint8_t *data[8],
  711. int linesize[8],
  712. int perms,
  713. int nb_samples,
  714. enum AVSampleFormat sample_fmt,
  715. uint64_t channel_layout,
  716. int planar);
  717. /**
  718. * Create an audio buffer reference wrapped around an already
  719. * allocated samples buffer.
  720. *
  721. * @param data pointers to the samples plane buffers
  722. * @param linesize linesize for the samples plane buffers
  723. * @param perms the required access permissions
  724. * @param nb_samples number of samples per channel
  725. * @param sample_fmt the format of each sample in the buffer to allocate
  726. * @param channel_layout the channel layout of the buffer
  727. */
  728. AVFilterBufferRef *avfilter_get_audio_buffer_ref_from_arrays_alt(uint8_t **data,
  729. int linesize,
  730. int perms,
  731. int nb_samples,
  732. enum AVSampleFormat sample_fmt,
  733. uint64_t channel_layout);
  734. /**
  735. * Request an input frame from the filter at the other end of the link.
  736. *
  737. * @param link the input link
  738. * @return zero on success or a negative error code; in particular:
  739. * AVERROR_EOF means that the end of frames have been reached;
  740. * AVERROR(EAGAIN) means that no frame could be immediately
  741. * produced.
  742. */
  743. int avfilter_request_frame(AVFilterLink *link);
  744. /**
  745. * Poll a frame from the filter chain.
  746. *
  747. * @param link the input link
  748. * @return the number of immediately available frames, a negative
  749. * number in case of error
  750. */
  751. int avfilter_poll_frame(AVFilterLink *link);
  752. /**
  753. * Notify the next filter of the start of a frame.
  754. *
  755. * @param link the output link the frame will be sent over
  756. * @param picref A reference to the frame about to be sent. The data for this
  757. * frame need only be valid once draw_slice() is called for that
  758. * portion. The receiving filter will free this reference when
  759. * it no longer needs it.
  760. */
  761. void avfilter_start_frame(AVFilterLink *link, AVFilterBufferRef *picref);
  762. /**
  763. * Notify the next filter that the current frame has finished.
  764. *
  765. * @param link the output link the frame was sent over
  766. */
  767. void avfilter_end_frame(AVFilterLink *link);
  768. /**
  769. * Send a slice to the next filter.
  770. *
  771. * Slices have to be provided in sequential order, either in
  772. * top-bottom or bottom-top order. If slices are provided in
  773. * non-sequential order the behavior of the function is undefined.
  774. *
  775. * @param link the output link over which the frame is being sent
  776. * @param y offset in pixels from the top of the image for this slice
  777. * @param h height of this slice in pixels
  778. * @param slice_dir the assumed direction for sending slices,
  779. * from the top slice to the bottom slice if the value is 1,
  780. * from the bottom slice to the top slice if the value is -1,
  781. * for other values the behavior of the function is undefined.
  782. */
  783. void avfilter_draw_slice(AVFilterLink *link, int y, int h, int slice_dir);
  784. #define AVFILTER_CMD_FLAG_ONE 1 ///< Stop once a filter understood the command (for target=all for example), fast filters are favored automatically
  785. #define AVFILTER_CMD_FLAG_FAST 2 ///< Only execute command when its fast (like a video out that supports contrast adjustment in hw)
  786. /**
  787. * Make the filter instance process a command.
  788. * It is recommended to use avfilter_graph_send_command().
  789. */
  790. int avfilter_process_command(AVFilterContext *filter, const char *cmd, const char *arg, char *res, int res_len, int flags);
  791. /**
  792. * Send a buffer of audio samples to the next filter.
  793. *
  794. * @param link the output link over which the audio samples are being sent
  795. * @param samplesref a reference to the buffer of audio samples being sent. The
  796. * receiving filter will free this reference when it no longer
  797. * needs it or pass it on to the next filter.
  798. */
  799. void avfilter_filter_samples(AVFilterLink *link, AVFilterBufferRef *samplesref);
  800. /** Initialize the filter system. Register all built-in filters. */
  801. void avfilter_register_all(void);
  802. /** Uninitialize the filter system. Unregister all filters. */
  803. void avfilter_uninit(void);
  804. /**
  805. * Register a filter. This is only needed if you plan to use
  806. * avfilter_get_by_name later to lookup the AVFilter structure by name. A
  807. * filter can still by instantiated with avfilter_open even if it is not
  808. * registered.
  809. *
  810. * @param filter the filter to register
  811. * @return 0 if the registration was successful, a negative value
  812. * otherwise
  813. */
  814. int avfilter_register(AVFilter *filter);
  815. /**
  816. * Get a filter definition matching the given name.
  817. *
  818. * @param name the filter name to find
  819. * @return the filter definition, if any matching one is registered.
  820. * NULL if none found.
  821. */
  822. AVFilter *avfilter_get_by_name(const char *name);
  823. /**
  824. * If filter is NULL, returns a pointer to the first registered filter pointer,
  825. * if filter is non-NULL, returns the next pointer after filter.
  826. * If the returned pointer points to NULL, the last registered filter
  827. * was already reached.
  828. */
  829. AVFilter **av_filter_next(AVFilter **filter);
  830. /**
  831. * Create a filter instance.
  832. *
  833. * @param filter_ctx put here a pointer to the created filter context
  834. * on success, NULL on failure
  835. * @param filter the filter to create an instance of
  836. * @param inst_name Name to give to the new instance. Can be NULL for none.
  837. * @return >= 0 in case of success, a negative error code otherwise
  838. */
  839. int avfilter_open(AVFilterContext **filter_ctx, AVFilter *filter, const char *inst_name);
  840. /**
  841. * Initialize a filter.
  842. *
  843. * @param filter the filter to initialize
  844. * @param args A string of parameters to use when initializing the filter.
  845. * The format and meaning of this string varies by filter.
  846. * @param opaque Any extra non-string data needed by the filter. The meaning
  847. * of this parameter varies by filter.
  848. * @return zero on success
  849. */
  850. int avfilter_init_filter(AVFilterContext *filter, const char *args, void *opaque);
  851. /**
  852. * Free a filter context.
  853. *
  854. * @param filter the filter to free
  855. */
  856. void avfilter_free(AVFilterContext *filter);
  857. /**
  858. * Insert a filter in the middle of an existing link.
  859. *
  860. * @param link the link into which the filter should be inserted
  861. * @param filt the filter to be inserted
  862. * @param filt_srcpad_idx the input pad on the filter to connect
  863. * @param filt_dstpad_idx the output pad on the filter to connect
  864. * @return zero on success
  865. */
  866. int avfilter_insert_filter(AVFilterLink *link, AVFilterContext *filt,
  867. unsigned filt_srcpad_idx, unsigned filt_dstpad_idx);
  868. /**
  869. * Insert a new pad.
  870. *
  871. * @param idx Insertion point. Pad is inserted at the end if this point
  872. * is beyond the end of the list of pads.
  873. * @param count Pointer to the number of pads in the list
  874. * @param padidx_off Offset within an AVFilterLink structure to the element
  875. * to increment when inserting a new pad causes link
  876. * numbering to change
  877. * @param pads Pointer to the pointer to the beginning of the list of pads
  878. * @param links Pointer to the pointer to the beginning of the list of links
  879. * @param newpad The new pad to add. A copy is made when adding.
  880. */
  881. void avfilter_insert_pad(unsigned idx, unsigned *count, size_t padidx_off,
  882. AVFilterPad **pads, AVFilterLink ***links,
  883. AVFilterPad *newpad);
  884. /** Insert a new input pad for the filter. */
  885. static inline void avfilter_insert_inpad(AVFilterContext *f, unsigned index,
  886. AVFilterPad *p)
  887. {
  888. avfilter_insert_pad(index, &f->input_count, offsetof(AVFilterLink, dstpad),
  889. &f->input_pads, &f->inputs, p);
  890. }
  891. /** Insert a new output pad for the filter. */
  892. static inline void avfilter_insert_outpad(AVFilterContext *f, unsigned index,
  893. AVFilterPad *p)
  894. {
  895. avfilter_insert_pad(index, &f->output_count, offsetof(AVFilterLink, srcpad),
  896. &f->output_pads, &f->outputs, p);
  897. }
  898. #endif /* AVFILTER_AVFILTER_H */