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.

500 lines
19KB

  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 FFMPEG_AVFILTER_H
  22. #define FFMPEG_AVFILTER_H
  23. #include <stddef.h>
  24. #include "avcodec.h"
  25. typedef struct AVFilterContext AVFilterContext;
  26. typedef struct AVFilterLink AVFilterLink;
  27. typedef struct AVFilterPad AVFilterPad;
  28. /**
  29. * A linked list of filters which reference this picture and the permissions
  30. * they each have. This is needed for the case that filter A requests a buffer
  31. * from filter B. Filter B gives it a buffer to use with the permissions it
  32. * requested, while reserving more permissions for itself to use when filter A
  33. * eventually passes the buffer to filter B. However, filter A does not know
  34. * what these permissions are to reinsert them in the reference passed to B,
  35. * filter B can't assume that any picture it is passed is one that it allocated.
  36. *
  37. * Rather than have each filter implement their own code to check for this
  38. * case, we store all the permissions here in the picture structure.
  39. *
  40. * Because the number of filters holding references to any one picture should
  41. * be rather low, this should not be a major source of performance problems.
  42. */
  43. typedef struct AVFilterPicPerms
  44. {
  45. AVFilterContext *filter; ///< the filter
  46. int perms; ///< the permissions that filter has
  47. struct AVFilterPicPerms *next;
  48. } AVFilterPicPerms;
  49. /* TODO: look for other flags which may be useful in this structure (interlace
  50. * flags, etc)
  51. */
  52. /**
  53. * A reference-counted picture data type used by the filter system. Filters
  54. * should not store pointers to this structure directly, but instead use the
  55. * AVFilterPicRef structure below
  56. */
  57. typedef struct AVFilterPic
  58. {
  59. uint8_t *data[4]; ///< picture data for each plane
  60. int linesize[4]; ///< number of bytes per line
  61. enum PixelFormat format; ///< colorspace
  62. unsigned refcount; ///< number of references to this image
  63. AVFilterPicPerms *perms; ///< list of permissions held by filters
  64. /** private data to be used by a custom free function */
  65. void *priv;
  66. /**
  67. * A pointer to the function to deallocate this image if the default
  68. * function is not sufficient. This could, for example, add the memory
  69. * back into a memory pool to be reused later without the overhead of
  70. * reallocating it from scratch.
  71. */
  72. void (*free)(struct AVFilterPic *pic);
  73. } AVFilterPic;
  74. /**
  75. * A reference to an AVFilterPic. Since filters can manipulate the origin of
  76. * a picture to, for example, crop image without any memcpy, the picture origin
  77. * and dimensions are per-reference properties. Linesize is also useful for
  78. * image flipping, frame to field filters, etc, and so is also per-reference.
  79. *
  80. * TODO: add anything necessary for frame reordering
  81. */
  82. typedef struct AVFilterPicRef
  83. {
  84. AVFilterPic *pic; ///< the picture that this is a reference to
  85. uint8_t *data[4]; ///< picture data for each plane
  86. int linesize[4]; ///< number of bytes per line
  87. int w; ///< image width
  88. int h; ///< image height
  89. int64_t pts; ///< presentation timestamp in milliseconds
  90. int perms; ///< permissions
  91. #define AV_PERM_READ 0x01 ///< can read from the buffer
  92. #define AV_PERM_WRITE 0x02 ///< can write to the buffer
  93. #define AV_PERM_PRESERVE 0x04 ///< nobody else can overwrite the buffer
  94. #define AV_PERM_REUSE 0x08 ///< can output the buffer multiple times
  95. } AVFilterPicRef;
  96. /** Get the permissions the filter has to access the picture. */
  97. int avfilter_get_pic_perms(AVFilterPicRef *pic, AVFilterContext *filter);
  98. /** Give the filter more permissions to access the picture */
  99. void avfilter_add_pic_perms(AVFilterPicRef *pic, AVFilterContext *filter,
  100. int perms);
  101. /**
  102. * Add a new reference to a picture.
  103. * @param ref An existing reference to the picture
  104. * @param ref If non-NULL, a pointer to the filter to which the permissions
  105. * to the picture are to be given
  106. * @param pmask A bitmask containing the allowable permissions in the new
  107. * reference
  108. * @return A new reference to the picture with the same properties as the
  109. * old, excluding any permissions denied by pmask
  110. */
  111. AVFilterPicRef *avfilter_ref_pic(AVFilterPicRef *ref, AVFilterContext *filter,
  112. int pmask);
  113. /**
  114. * Remove a reference to a picture. If this is the last reference to the
  115. * picture, the picture itself is also automatically freed.
  116. * @param ref Reference to the picture.
  117. */
  118. void avfilter_unref_pic(AVFilterPicRef *ref);
  119. /**
  120. * A filter pad used for either input or output
  121. */
  122. struct AVFilterPad
  123. {
  124. /**
  125. * Pad name. The name is unique among inputs and among outputs, but an
  126. * input may have the same name as an output. This may be NULL if this
  127. * pad has no need to ever be referenced by name.
  128. */
  129. char *name;
  130. /**
  131. * AVFilterPad type. Only video supported now, hopefully someone will
  132. * add audio in the future.
  133. */
  134. int type;
  135. #define AV_PAD_VIDEO 0 ///< video pad
  136. /**
  137. * Minimum required permissions on incoming buffers. Any buffers with
  138. * insufficient permissions will be automatically copied by the filter
  139. * system to a new buffer which provides the needed access permissions.
  140. *
  141. * Input pads only.
  142. */
  143. int min_perms;
  144. /**
  145. * Permissions which are not accepted on incoming buffers. Any buffer
  146. * which has any of these permissions set be automatically copied by the
  147. * filter system to a new buffer which does not have those permissions.
  148. * This can be used to easily disallow buffers with AV_PERM_REUSE.
  149. *
  150. * Input pads only.
  151. */
  152. int rej_perms;
  153. /**
  154. * Callback to get a list of supported formats. The returned list should
  155. * be terminated by -1 (see avfilter_make_format_list for an easy way to
  156. * create such a list).
  157. *
  158. * This is used for both input and output pads. If ommitted from an output
  159. * pad, it is assumed that the only format supported is the same format
  160. * that is being used for the filter's first input. If the filter has no
  161. * inputs, then this may not be ommitted for its output pads.
  162. */
  163. int *(*query_formats)(AVFilterLink *link);
  164. /**
  165. * Callback called before passing the first slice of a new frame. If
  166. * NULL, the filter layer will default to storing a reference to the
  167. * picture inside the link structure.
  168. *
  169. * Input video pads only.
  170. */
  171. void (*start_frame)(AVFilterLink *link, AVFilterPicRef *picref);
  172. /**
  173. * Callback function to get a buffer. If NULL, the filter system will
  174. * handle buffer requests.
  175. *
  176. * Input video pads only.
  177. */
  178. AVFilterPicRef *(*get_video_buffer)(AVFilterLink *link, int perms);
  179. /**
  180. * Callback called after the slices of a frame are completely sent. If
  181. * NULL, the filter layer will default to releasing the reference stored
  182. * in the link structure during start_frame().
  183. *
  184. * Input video pads only.
  185. */
  186. void (*end_frame)(AVFilterLink *link);
  187. /**
  188. * Slice drawing callback. This is where a filter receives video data
  189. * and should do its processing.
  190. *
  191. * Input video pads only.
  192. */
  193. void (*draw_slice)(AVFilterLink *link, int y, int height);
  194. /**
  195. * Frame request callback. A call to this should result in at least one
  196. * frame being output over the given link. This should return zero on
  197. * success, and another value on error.
  198. *
  199. * Output video pads only.
  200. */
  201. int (*request_frame)(AVFilterLink *link);
  202. /**
  203. * Link configuration callback.
  204. *
  205. * For output pads, this should set the link properties such as
  206. * width/height. This should NOT set the format property - that is
  207. * negotiated between filters by the filter system using the
  208. * query_formats() callback before this function is called.
  209. *
  210. * For input pads, this should check the properties of the link, and update
  211. * the filter's internal state as necessary.
  212. *
  213. * For both input and output filters, this should return zero on success,
  214. * and another value on error.
  215. */
  216. int (*config_props)(AVFilterLink *link);
  217. };
  218. /** Default handler for start_frame() for video inputs */
  219. void avfilter_default_start_frame(AVFilterLink *link, AVFilterPicRef *picref);
  220. /** Default handler for end_frame() for video inputs */
  221. void avfilter_default_end_frame(AVFilterLink *link);
  222. /** Default handler for config_props() for video outputs */
  223. int avfilter_default_config_output_link(AVFilterLink *link);
  224. /** Default handler for config_props() for video inputs */
  225. int avfilter_default_config_input_link (AVFilterLink *link);
  226. /** Default handler for query_formats() for video outputs */
  227. int *avfilter_default_query_output_formats(AVFilterLink *link);
  228. /** Default handler for get_video_buffer() for video inputs */
  229. AVFilterPicRef *avfilter_default_get_video_buffer(AVFilterLink *link,
  230. int perms);
  231. /** handler for get_video_buffer() which forwards the request down the chain */
  232. AVFilterPicRef *avfilter_next_get_video_buffer(AVFilterLink *link, int perms);
  233. /**
  234. * Filter definition. This defines the pads a filter contains, and all the
  235. * callback functions used to interact with the filter.
  236. */
  237. typedef struct
  238. {
  239. char *name; ///< filter name
  240. char *author; ///< filter author
  241. int priv_size; ///< size of private data to allocate for the filter
  242. /**
  243. * Filter initialization function. Args contains the user-supplied
  244. * parameters. FIXME: maybe an AVOption-based system would be better?
  245. * opaque is data provided by the code requesting creation of the filter,
  246. * and is used to pass data to the filter.
  247. */
  248. int (*init)(AVFilterContext *ctx, const char *args, void *opaque);
  249. /**
  250. * Filter uninitialization function. Should deallocate any memory held
  251. * by the filter, release any picture references, etc. This does not need
  252. * to deallocate the AVFilterContext->priv memory itself.
  253. */
  254. void (*uninit)(AVFilterContext *ctx);
  255. const AVFilterPad *inputs; ///< NULL terminated list of inputs. NULL if none
  256. const AVFilterPad *outputs; ///< NULL terminated list of outputs. NULL if none
  257. } AVFilter;
  258. /** An instance of a filter */
  259. struct AVFilterContext
  260. {
  261. AVClass *av_class; ///< Needed for av_log()
  262. AVFilter *filter; ///< The AVFilter of which this is an instance
  263. char *name; ///< name of this filter instance
  264. unsigned input_count; ///< number of input pads
  265. AVFilterPad *input_pads; ///< array of input pads
  266. AVFilterLink **inputs; ///< array of pointers to input links
  267. unsigned output_count; ///< number of output pads
  268. AVFilterPad *output_pads; ///< array of output pads
  269. AVFilterLink **outputs; ///< array of pointers to output links
  270. void *priv; ///< private data for use by the filter
  271. };
  272. /**
  273. * A links between two filters. This contains pointers to the source and
  274. * destination filters between which this link exists, and the indices of
  275. * the pads involved. In addition, this link also contains the parameters
  276. * which have been negotiated and agreed upon between the filter, such as
  277. * image dimensions, format, etc
  278. */
  279. struct AVFilterLink
  280. {
  281. AVFilterContext *src; ///< source filter
  282. unsigned int srcpad; ///< index of the output pad on the source filter
  283. AVFilterContext *dst; ///< dest filter
  284. unsigned int dstpad; ///< index of the input pad on the dest filter
  285. int w; ///< agreed upon image width
  286. int h; ///< agreed upon image height
  287. enum PixelFormat format; ///< agreed upon image colorspace
  288. /**
  289. * The picture reference currently being sent across the link by the source
  290. * filter. This is used internally by the filter system to allow
  291. * automatic copying of pictures which d not have sufficient permissions
  292. * for the destination. This should not be accessed directly by the
  293. * filters.
  294. */
  295. AVFilterPicRef *srcpic;
  296. AVFilterPicRef *cur_pic;
  297. AVFilterPicRef *outpic;
  298. };
  299. /**
  300. * Link two filters together
  301. * @param src The source filter
  302. * @param srcpad Index of the output pad on the source filter
  303. * @param dst The destination filter
  304. * @param dstpad Index of the input pad on the destination filter
  305. * @return Zero on success
  306. */
  307. int avfilter_link(AVFilterContext *src, unsigned srcpad,
  308. AVFilterContext *dst, unsigned dstpad);
  309. /**
  310. * Negotiate the colorspace, dimensions, etc of a link
  311. * @param link The link to negotiate the properties of
  312. * @return Zero on successful negotiation
  313. */
  314. int avfilter_config_link(AVFilterLink *link);
  315. /**
  316. * Request a picture buffer with a specific set of permissions
  317. * @param link The output link to the filter from which the picture will
  318. * be requested
  319. * @param perms The required access permissions
  320. * @return A reference to the picture. This must be unreferenced with
  321. * avfilter_unref_pic when you are finished with it.
  322. */
  323. AVFilterPicRef *avfilter_get_video_buffer(AVFilterLink *link, int perms);
  324. /**
  325. * Request an input frame from the filter at the other end of the link.
  326. * @param link The input link
  327. * @return Zero on success
  328. */
  329. int avfilter_request_frame(AVFilterLink *link);
  330. /**
  331. * Notify the next filter of the start of a frame.
  332. * @param link The output link the frame will be sent over
  333. * @param picref A reference to the frame about to be sent. The data for this
  334. * frame need only be valid once draw_slice() is called for that
  335. * portion. The receiving filter will free this reference when
  336. * it no longer needs it.
  337. */
  338. void avfilter_start_frame(AVFilterLink *link, AVFilterPicRef *picref);
  339. /**
  340. * Notify the next filter that the current frame has finished
  341. * @param link The output link the frame was sent over
  342. */
  343. void avfilter_end_frame(AVFilterLink *link);
  344. /**
  345. * Send a slice to the next filter
  346. * @param link The output link over which the frame is being sent
  347. * @param y Offset in pixels from the top of the image for this slice
  348. * @param h Height of this slice in pixels
  349. */
  350. void avfilter_draw_slice(AVFilterLink *link, int y, int h);
  351. /** Initialize the filter system. Registers all builtin filters */
  352. void avfilter_init(void);
  353. /** Uninitialize the filter system. Unregisters all filters */
  354. void avfilter_uninit(void);
  355. /**
  356. * Register a filter. This is only needed if you plan to create an instance of
  357. * this filter by name later with avfilter_create_by_name. A filter can still
  358. * by created with acfilter_create even if it is not registered.
  359. * @param filter The filter to register
  360. */
  361. void avfilter_register(AVFilter *filter);
  362. /**
  363. * Gets a filter definition matching the given name
  364. * @param name The filter name to find
  365. * @return The filter definition, if any matching one is registered.
  366. * NULL if none found.
  367. */
  368. AVFilter *avfilter_get_by_name(char *name);
  369. /**
  370. * Create a filter instance
  371. * @param filter The filter to create an instance of
  372. * @param inst_name Name to give to the new instance. Can be NULL for none.
  373. * @return Pointer to the new instance on success. NULL on failure.
  374. */
  375. AVFilterContext *avfilter_create(AVFilter *filter, char *inst_name);
  376. /**
  377. * Creates a filter instace
  378. * @param name The name of the filter type which is to be instantiated
  379. * @param inst_name Name to give to the new instance. Can be NULL for none.
  380. * @return Pointer to the new instance on success. NULL in failure.
  381. */
  382. AVFilterContext *avfilter_create_by_name(char *name, char *inst_name);
  383. /**
  384. * Initialize a filter
  385. * @param filter The filter to initialize
  386. * @param args A string of parameters to use when initializing the filter.
  387. * The format and meaning of this string varies by filter.
  388. * @param opaque Any extra non-string data needed by the filter. The meaning
  389. * of this parameter varies by filter.
  390. * @return Zero on success
  391. */
  392. int avfilter_init_filter(AVFilterContext *filter, const char *args, void *opaque);
  393. /**
  394. * Destroy a filter
  395. * @param filter The filter to destroy
  396. */
  397. void avfilter_destroy(AVFilterContext *filter);
  398. /**
  399. * Helper function to create a list of supported formats. This is intended
  400. * for use in AVFilterPad->query_formats().
  401. * @param len The number of formats supported
  402. * @param ... A list of the supported formats
  403. * @return The format list in a form suitable for returning from
  404. * AVFilterPad->query_formats()
  405. */
  406. int *avfilter_make_format_list(int len, ...);
  407. /**
  408. * Insert a new pad
  409. * @param idx Insertion point. Pad is inserted at the end if this point
  410. * is beyond the end of the list of pads.
  411. * @param count Pointer to the number of pads in the list
  412. * @param padidx_off Offset within an AVFilterLink structure to the element
  413. * to increment when inserting a new pad causes link
  414. * numbering to change
  415. * @param pads Pointer to the pointer to the beginning of the list of pads
  416. * @param links Pointer to the pointer to the beginning of the list of links
  417. * @param newpad The new pad to add. A copy is made when adding.
  418. */
  419. void avfilter_insert_pad(unsigned idx, unsigned *count, size_t padidx_off,
  420. AVFilterPad **pads, AVFilterLink ***links,
  421. AVFilterPad *newpad);
  422. /** insert a new input pad for the filter */
  423. static inline void avfilter_insert_inpad(AVFilterContext *f, unsigned index,
  424. AVFilterPad *p)
  425. {
  426. avfilter_insert_pad(index, &f->input_count, offsetof(AVFilterLink, dstpad),
  427. &f->input_pads, &f->inputs, p);
  428. }
  429. /** insert a new output pad for the filter */
  430. static inline void avfilter_insert_outpad(AVFilterContext *f, unsigned index,
  431. AVFilterPad *p)
  432. {
  433. avfilter_insert_pad(index, &f->output_count, offsetof(AVFilterLink, srcpad),
  434. &f->output_pads, &f->outputs, p);
  435. }
  436. #endif /* FFMPEG_AVFILTER_H */