avfilter.h 37 KB

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