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.

277 lines
9.3KB

  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. /* TODO: look for other flags which may be useful in this structure (interlace
  29. * flags, etc)
  30. */
  31. /**
  32. * A reference-counted picture data type used by the filter system. Filters
  33. * should not store pointers to this structure directly, but instead use the
  34. * AVFilterPicRef structure below
  35. */
  36. typedef struct AVFilterPic
  37. {
  38. uint8_t *data[4];
  39. int linesize[4]; ///< number of bytes per line
  40. enum PixelFormat format;
  41. unsigned refcount;
  42. void *priv;
  43. void (*free)(struct AVFilterPic *pic);
  44. } AVFilterPic;
  45. /**
  46. * A reference to an AVFilterPic. Since filters can manipulate the origin of
  47. * a picture to, for example, crop image without any memcpy, the picture origin
  48. * and dimensions are per-reference properties. Linesize is also useful for
  49. * image flipping, frame to field filters, etc, and so is also per-reference.
  50. *
  51. * TODO: add anything necessary for frame reordering
  52. */
  53. typedef struct AVFilterPicRef
  54. {
  55. AVFilterPic *pic;
  56. uint8_t *data[4];
  57. int linesize[4];
  58. int w, h;
  59. int64_t pts; ///< presentation timestamp in milliseconds
  60. int perms; ///< permissions
  61. #define AV_PERM_READ 0x01 ///< can read from the buffer
  62. #define AV_PERM_WRITE 0x02 ///< can write to the buffer
  63. #define AV_PERM_PRESERVE 0x04 ///< nobody else can overwrite the buffer
  64. #define AV_PERM_REUSE 0x08 ///< can output the buffer multiple times
  65. } AVFilterPicRef;
  66. /**
  67. * Add a new reference to a picture.
  68. * @param ref An existing reference to the picture
  69. * @param pmask A bitmask containing the allowable permissions in the new reference
  70. * @return A new reference to the picture with the same properties as the old
  71. */
  72. AVFilterPicRef *avfilter_ref_pic(AVFilterPicRef *ref, int pmask);
  73. /**
  74. * Remove a reference to a picture. If this is the last reference to the
  75. * picture, the picture itself is also automatically freed.
  76. * @param ref Reference to the picture.
  77. */
  78. void avfilter_unref_pic(AVFilterPicRef *ref);
  79. struct AVFilterPad
  80. {
  81. /**
  82. * Pad name. The name is unique among inputs and among oututs, but an
  83. * input may have the same name as an output.
  84. */
  85. char *name;
  86. /**
  87. * AVFilterPad type. Only video supported now, hopefully someone will
  88. * add audio in the future.
  89. */
  90. int type;
  91. #define AV_PAD_VIDEO 0
  92. /**
  93. * Callback to get a list of supported formats. The returned list should
  94. * be terminated by -1. This is used for both input and output pads and
  95. * is required for both.
  96. */
  97. int *(*query_formats)(AVFilterLink *link);
  98. /**
  99. * Callback called before passing the first slice of a new frame. If
  100. * NULL, the filter layer will default to storing a reference to the
  101. * picture inside the link structure.
  102. */
  103. void (*start_frame)(AVFilterLink *link, AVFilterPicRef *picref);
  104. /**
  105. * Callback function to get a buffer. If NULL, the filter system will
  106. * handle buffer requests. Only required for input video pads.
  107. */
  108. AVFilterPicRef *(*get_video_buffer)(AVFilterLink *link, int perms);
  109. /**
  110. * Callback called after the slices of a frame are completely sent. If
  111. * NULL, the filter layer will default to releasing the reference stored
  112. * in the link structure during start_frame().
  113. */
  114. void (*end_frame)(AVFilterLink *link);
  115. /**
  116. * Slice drawing callback. This is where a filter receives video data
  117. * and should do its processing. Only required for input video pads.
  118. */
  119. void (*draw_slice)(AVFilterLink *link, uint8_t *data[4], int y, int height);
  120. /**
  121. * Frame request callback. A call to this should result in at least one
  122. * frame being output over the given link. Video output pads only.
  123. */
  124. void (*request_frame)(AVFilterLink *link);
  125. /**
  126. * Link configuration callback. For output pads, this should set the link
  127. * properties such as width/height. NOTE: this should not set the format
  128. * property - that is negotiated between filters by the filter system using
  129. * the query_formats() callback.
  130. *
  131. * For input pads, this should check the properties of the link, and update
  132. * the filter's internal state as necessary.
  133. */
  134. int (*config_props)(AVFilterLink *link);
  135. };
  136. /* the default implementations of filter entry points */
  137. void avfilter_default_start_frame(AVFilterLink *link, AVFilterPicRef *picref);
  138. void avfilter_default_end_frame(AVFilterLink *link);
  139. int avfilter_default_config_output_link(AVFilterLink *link);
  140. int avfilter_default_config_input_link (AVFilterLink *link);
  141. int *avfilter_default_query_output_formats(AVFilterLink *link);
  142. AVFilterPicRef *avfilter_default_get_video_buffer(AVFilterLink *link,
  143. int perms);
  144. typedef struct
  145. {
  146. char *name;
  147. char *author;
  148. int priv_size;
  149. /**
  150. * Filter initialization function. Args contains the user-supplied
  151. * parameters. FIXME: maybe an AVOption-based system would be better?
  152. * opaque is data provided by the code requesting creation of the filter,
  153. * and is used to pass data to the filter.
  154. */
  155. int (*init)(AVFilterContext *ctx, const char *args, void *opaque);
  156. void (*uninit)(AVFilterContext *ctx);
  157. const AVFilterPad *inputs; /// NULL terminated list of inputs. NULL if none
  158. const AVFilterPad *outputs; /// NULL terminated list of outputs. NULL if none
  159. } AVFilter;
  160. struct AVFilterContext
  161. {
  162. AVClass *av_class;
  163. AVFilter *filter;
  164. char *name;
  165. unsigned input_count;
  166. AVFilterPad *input_pads;
  167. AVFilterLink **inputs;
  168. unsigned output_count;
  169. AVFilterPad *output_pads;
  170. AVFilterLink **outputs;
  171. void *priv;
  172. };
  173. struct AVFilterLink
  174. {
  175. AVFilterContext *src;
  176. unsigned int srcpad;
  177. AVFilterContext *dst;
  178. unsigned int dstpad;
  179. int w, h;
  180. enum PixelFormat format;
  181. AVFilterPicRef *cur_pic;
  182. AVFilterPicRef *outpic;
  183. };
  184. /** Link two filters together */
  185. int avfilter_link(AVFilterContext *src, unsigned srcpad,
  186. AVFilterContext *dst, unsigned dstpad);
  187. /** Configure the colorspace, dimensions, etc of a link */
  188. int avfilter_config_link(AVFilterLink *link);
  189. AVFilterPicRef *avfilter_get_video_buffer(AVFilterLink *link, int perms);
  190. void avfilter_request_frame(AVFilterLink *link);
  191. void avfilter_start_frame(AVFilterLink *link, AVFilterPicRef *picref);
  192. void avfilter_end_frame(AVFilterLink *link);
  193. void avfilter_draw_slice(AVFilterLink *link, uint8_t *data[4], int y, int h);
  194. void avfilter_init(void);
  195. void avfilter_uninit(void);
  196. void avfilter_register(AVFilter *filter);
  197. AVFilter *avfilter_get_by_name(char *name);
  198. AVFilterContext *avfilter_create(AVFilter *filter, char *inst_name);
  199. AVFilterContext *avfilter_create_by_name(char *name, char *inst_name);
  200. int avfilter_init_filter(AVFilterContext *filter, const char *args, void *opaque);
  201. void avfilter_destroy(AVFilterContext *filter);
  202. int *avfilter_make_format_list(int len, ...);
  203. /**
  204. * Insert a new pad
  205. * @param idx Insertion point. Pad is inserted at the end if this point
  206. * is beyond the end of the list of pads.
  207. * @param count Pointer to the number of pads in the list
  208. * @param padidx_off Offset within an AVFilterLink structure to the element
  209. * to increment when inserting a new pad causes link
  210. * numbering to change
  211. * @param pads Pointer to the pointer to the beginning of the list of pads
  212. * @param links Pointer to the pointer to the beginning of the list of links
  213. * @param newpad The new pad to add. A copy is made when adding.
  214. */
  215. void avfilter_insert_pad(unsigned idx, unsigned *count, size_t padidx_off,
  216. AVFilterPad **pads, AVFilterLink ***links,
  217. AVFilterPad *newpad);
  218. /** insert a new input pad for the filter */
  219. static inline void avfilter_insert_inpad(AVFilterContext *f, unsigned index,
  220. AVFilterPad *p)
  221. {
  222. avfilter_insert_pad(index, &f->input_count, offsetof(AVFilterLink, dstpad),
  223. &f->input_pads, &f->inputs, p);
  224. }
  225. /** insert a new output pad for the filter */
  226. static inline void avfilter_insert_outpad(AVFilterContext *f, unsigned index,
  227. AVFilterPad *p)
  228. {
  229. avfilter_insert_pad(index, &f->output_count, offsetof(AVFilterLink, srcpad),
  230. &f->output_pads, &f->outputs, p);
  231. }
  232. #endif /* FFMPEG_AVFILTER_H */