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.

392 lines
13KB

  1. /*
  2. * This file is part of FFmpeg.
  3. *
  4. * FFmpeg is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2.1 of the License, or (at your option) any later version.
  8. *
  9. * FFmpeg is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with FFmpeg; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #ifndef AVFILTER_INTERNAL_H
  19. #define AVFILTER_INTERNAL_H
  20. /**
  21. * @file
  22. * internal API functions
  23. */
  24. #include "libavutil/internal.h"
  25. #include "avfilter.h"
  26. #include "formats.h"
  27. #include "framepool.h"
  28. #include "framequeue.h"
  29. #include "thread.h"
  30. #include "version.h"
  31. #include "video.h"
  32. #include "libavcodec/avcodec.h"
  33. #include "libavcodec/internal.h"
  34. typedef struct AVFilterCommand {
  35. double time; ///< time expressed in seconds
  36. char *command; ///< command
  37. char *arg; ///< optional argument for the command
  38. int flags;
  39. struct AVFilterCommand *next;
  40. } AVFilterCommand;
  41. /**
  42. * Update the position of a link in the age heap.
  43. */
  44. void ff_avfilter_graph_update_heap(AVFilterGraph *graph, AVFilterLink *link);
  45. /**
  46. * A filter pad used for either input or output.
  47. */
  48. struct AVFilterPad {
  49. /**
  50. * Pad name. The name is unique among inputs and among outputs, but an
  51. * input may have the same name as an output. This may be NULL if this
  52. * pad has no need to ever be referenced by name.
  53. */
  54. const char *name;
  55. /**
  56. * AVFilterPad type.
  57. */
  58. enum AVMediaType type;
  59. /**
  60. * Callback function to get a video buffer. If NULL, the filter system will
  61. * use ff_default_get_video_buffer().
  62. *
  63. * Input video pads only.
  64. */
  65. AVFrame *(*get_video_buffer)(AVFilterLink *link, int w, int h);
  66. /**
  67. * Callback function to get an audio buffer. If NULL, the filter system will
  68. * use ff_default_get_audio_buffer().
  69. *
  70. * Input audio pads only.
  71. */
  72. AVFrame *(*get_audio_buffer)(AVFilterLink *link, int nb_samples);
  73. /**
  74. * Filtering callback. This is where a filter receives a frame with
  75. * audio/video data and should do its processing.
  76. *
  77. * Input pads only.
  78. *
  79. * @return >= 0 on success, a negative AVERROR on error. This function
  80. * must ensure that frame is properly unreferenced on error if it
  81. * hasn't been passed on to another filter.
  82. */
  83. int (*filter_frame)(AVFilterLink *link, AVFrame *frame);
  84. /**
  85. * Frame request callback. A call to this should result in some progress
  86. * towards producing output over the given link. This should return zero
  87. * on success, and another value on error.
  88. *
  89. * Output pads only.
  90. */
  91. int (*request_frame)(AVFilterLink *link);
  92. /**
  93. * Link configuration callback.
  94. *
  95. * For output pads, this should set the link properties such as
  96. * width/height. This should NOT set the format property - that is
  97. * negotiated between filters by the filter system using the
  98. * query_formats() callback before this function is called.
  99. *
  100. * For input pads, this should check the properties of the link, and update
  101. * the filter's internal state as necessary.
  102. *
  103. * For both input and output filters, this should return zero on success,
  104. * and another value on error.
  105. */
  106. int (*config_props)(AVFilterLink *link);
  107. /**
  108. * The filter expects writable frames from its input link,
  109. * duplicating data buffers if needed.
  110. *
  111. * input pads only.
  112. */
  113. int needs_writable;
  114. };
  115. struct AVFilterGraphInternal {
  116. void *thread;
  117. avfilter_execute_func *thread_execute;
  118. FFFrameQueueGlobal frame_queues;
  119. };
  120. struct AVFilterInternal {
  121. avfilter_execute_func *execute;
  122. };
  123. /**
  124. * Tell if an integer is contained in the provided -1-terminated list of integers.
  125. * This is useful for determining (for instance) if an AVPixelFormat is in an
  126. * array of supported formats.
  127. *
  128. * @param fmt provided format
  129. * @param fmts -1-terminated list of formats
  130. * @return 1 if present, 0 if absent
  131. */
  132. int ff_fmt_is_in(int fmt, const int *fmts);
  133. /* Functions to parse audio format arguments */
  134. /**
  135. * Parse a pixel format.
  136. *
  137. * @param ret pixel format pointer to where the value should be written
  138. * @param arg string to parse
  139. * @param log_ctx log context
  140. * @return >= 0 in case of success, a negative AVERROR code on error
  141. */
  142. av_warn_unused_result
  143. int ff_parse_pixel_format(enum AVPixelFormat *ret, const char *arg, void *log_ctx);
  144. /**
  145. * Parse a sample rate.
  146. *
  147. * @param ret unsigned integer pointer to where the value should be written
  148. * @param arg string to parse
  149. * @param log_ctx log context
  150. * @return >= 0 in case of success, a negative AVERROR code on error
  151. */
  152. av_warn_unused_result
  153. int ff_parse_sample_rate(int *ret, const char *arg, void *log_ctx);
  154. /**
  155. * Parse a channel layout or a corresponding integer representation.
  156. *
  157. * @param ret 64bit integer pointer to where the value should be written.
  158. * @param nret integer pointer to the number of channels;
  159. * if not NULL, then unknown channel layouts are accepted
  160. * @param arg string to parse
  161. * @param log_ctx log context
  162. * @return >= 0 in case of success, a negative AVERROR code on error
  163. */
  164. av_warn_unused_result
  165. int ff_parse_channel_layout(int64_t *ret, int *nret, const char *arg,
  166. void *log_ctx);
  167. void ff_update_link_current_pts(AVFilterLink *link, int64_t pts);
  168. /**
  169. * Set the status field of a link from the source filter.
  170. * The pts should reflect the timestamp of the status change,
  171. * in link time base and relative to the frames timeline.
  172. * In particular, for AVERROR_EOF, it should reflect the
  173. * end time of the last frame.
  174. */
  175. void ff_avfilter_link_set_in_status(AVFilterLink *link, int status, int64_t pts);
  176. /**
  177. * Set the status field of a link from the destination filter.
  178. * The pts should probably be left unset (AV_NOPTS_VALUE).
  179. */
  180. void ff_avfilter_link_set_out_status(AVFilterLink *link, int status, int64_t pts);
  181. void ff_command_queue_pop(AVFilterContext *filter);
  182. #define D2TS(d) (isnan(d) ? AV_NOPTS_VALUE : (int64_t)(d))
  183. #define TS2D(ts) ((ts) == AV_NOPTS_VALUE ? NAN : (double)(ts))
  184. #define TS2T(ts, tb) ((ts) == AV_NOPTS_VALUE ? NAN : (double)(ts) * av_q2d(tb))
  185. /* misc trace functions */
  186. #define FF_TPRINTF_START(ctx, func) ff_tlog(NULL, "%-16s: ", #func)
  187. char *ff_get_ref_perms_string(char *buf, size_t buf_size, int perms);
  188. void ff_tlog_ref(void *ctx, AVFrame *ref, int end);
  189. void ff_tlog_link(void *ctx, AVFilterLink *link, int end);
  190. /**
  191. * Insert a new pad.
  192. *
  193. * @param idx Insertion point. Pad is inserted at the end if this point
  194. * is beyond the end of the list of pads.
  195. * @param count Pointer to the number of pads in the list
  196. * @param padidx_off Offset within an AVFilterLink structure to the element
  197. * to increment when inserting a new pad causes link
  198. * numbering to change
  199. * @param pads Pointer to the pointer to the beginning of the list of pads
  200. * @param links Pointer to the pointer to the beginning of the list of links
  201. * @param newpad The new pad to add. A copy is made when adding.
  202. * @return >= 0 in case of success, a negative AVERROR code on error
  203. */
  204. int ff_insert_pad(unsigned idx, unsigned *count, size_t padidx_off,
  205. AVFilterPad **pads, AVFilterLink ***links,
  206. AVFilterPad *newpad);
  207. /** Insert a new input pad for the filter. */
  208. static inline int ff_insert_inpad(AVFilterContext *f, unsigned index,
  209. AVFilterPad *p)
  210. {
  211. return ff_insert_pad(index, &f->nb_inputs, offsetof(AVFilterLink, dstpad),
  212. &f->input_pads, &f->inputs, p);
  213. }
  214. /** Insert a new output pad for the filter. */
  215. static inline int ff_insert_outpad(AVFilterContext *f, unsigned index,
  216. AVFilterPad *p)
  217. {
  218. return ff_insert_pad(index, &f->nb_outputs, offsetof(AVFilterLink, srcpad),
  219. &f->output_pads, &f->outputs, p);
  220. }
  221. /**
  222. * Request an input frame from the filter at the other end of the link.
  223. *
  224. * This function must not be used by filters using the activate callback,
  225. * use ff_link_set_frame_wanted() instead.
  226. *
  227. * The input filter may pass the request on to its inputs, fulfill the
  228. * request from an internal buffer or any other means specific to its function.
  229. *
  230. * When the end of a stream is reached AVERROR_EOF is returned and no further
  231. * frames are returned after that.
  232. *
  233. * When a filter is unable to output a frame for example due to its sources
  234. * being unable to do so or because it depends on external means pushing data
  235. * into it then AVERROR(EAGAIN) is returned.
  236. * It is important that a AVERROR(EAGAIN) return is returned all the way to the
  237. * caller (generally eventually a user application) as this step may (but does
  238. * not have to be) necessary to provide the input with the next frame.
  239. *
  240. * If a request is successful then some progress has been made towards
  241. * providing a frame on the link (through ff_filter_frame()). A filter that
  242. * needs several frames to produce one is allowed to return success if one
  243. * more frame has been processed but no output has been produced yet. A
  244. * filter is also allowed to simply forward a success return value.
  245. *
  246. * @param link the input link
  247. * @return zero on success
  248. * AVERROR_EOF on end of file
  249. * AVERROR(EAGAIN) if the previous filter cannot output a frame
  250. * currently and can neither guarantee that EOF has been reached.
  251. */
  252. int ff_request_frame(AVFilterLink *link);
  253. #define AVFILTER_DEFINE_CLASS(fname) \
  254. static const AVClass fname##_class = { \
  255. .class_name = #fname, \
  256. .item_name = av_default_item_name, \
  257. .option = fname##_options, \
  258. .version = LIBAVUTIL_VERSION_INT, \
  259. .category = AV_CLASS_CATEGORY_FILTER, \
  260. }
  261. /**
  262. * Find the index of a link.
  263. *
  264. * I.e. find i such that link == ctx->(in|out)puts[i]
  265. */
  266. #define FF_INLINK_IDX(link) ((int)((link)->dstpad - (link)->dst->input_pads))
  267. #define FF_OUTLINK_IDX(link) ((int)((link)->srcpad - (link)->src->output_pads))
  268. /**
  269. * Send a frame of data to the next filter.
  270. *
  271. * @param link the output link over which the data is being sent
  272. * @param frame a reference to the buffer of data being sent. The
  273. * receiving filter will free this reference when it no longer
  274. * needs it or pass it on to the next filter.
  275. *
  276. * @return >= 0 on success, a negative AVERROR on error. The receiving filter
  277. * is responsible for unreferencing frame in case of error.
  278. */
  279. int ff_filter_frame(AVFilterLink *link, AVFrame *frame);
  280. /**
  281. * Allocate a new filter context and return it.
  282. *
  283. * @param filter what filter to create an instance of
  284. * @param inst_name name to give to the new filter context
  285. *
  286. * @return newly created filter context or NULL on failure
  287. */
  288. AVFilterContext *ff_filter_alloc(const AVFilter *filter, const char *inst_name);
  289. int ff_filter_activate(AVFilterContext *filter);
  290. /**
  291. * Remove a filter from a graph;
  292. */
  293. void ff_filter_graph_remove_filter(AVFilterGraph *graph, AVFilterContext *filter);
  294. /**
  295. * The filter is aware of hardware frames, and any hardware frame context
  296. * should not be automatically propagated through it.
  297. */
  298. #define FF_FILTER_FLAG_HWFRAME_AWARE (1 << 0)
  299. /**
  300. * Run one round of processing on a filter graph.
  301. */
  302. int ff_filter_graph_run_once(AVFilterGraph *graph);
  303. /**
  304. * Normalize the qscale factor
  305. * FIXME the H264 qscale is a log based scale, mpeg1/2 is not, the code below
  306. * cannot be optimal
  307. */
  308. static inline int ff_norm_qscale(int qscale, int type)
  309. {
  310. switch (type) {
  311. case FF_QSCALE_TYPE_MPEG1: return qscale;
  312. case FF_QSCALE_TYPE_MPEG2: return qscale >> 1;
  313. case FF_QSCALE_TYPE_H264: return qscale >> 2;
  314. case FF_QSCALE_TYPE_VP56: return (63 - qscale + 2) >> 2;
  315. }
  316. return qscale;
  317. }
  318. /**
  319. * Get number of threads for current filter instance.
  320. * This number is always same or less than graph->nb_threads.
  321. */
  322. int ff_filter_get_nb_threads(AVFilterContext *ctx);
  323. /**
  324. * Generic processing of user supplied commands that are set
  325. * in the same way as the filter options.
  326. */
  327. int ff_filter_process_command(AVFilterContext *ctx, const char *cmd,
  328. const char *arg, char *res, int res_len, int flags);
  329. /**
  330. * Perform any additional setup required for hardware frames.
  331. *
  332. * link->hw_frames_ctx must be set before calling this function.
  333. * Inside link->hw_frames_ctx, the fields format, sw_format, width and
  334. * height must be set. If dynamically allocated pools are not supported,
  335. * then initial_pool_size must also be set, to the minimum hardware frame
  336. * pool size necessary for the filter to work (taking into account any
  337. * frames which need to stored for use in operations as appropriate). If
  338. * default_pool_size is nonzero, then it will be used as the pool size if
  339. * no other modification takes place (this can be used to preserve
  340. * compatibility).
  341. */
  342. int ff_filter_init_hw_frames(AVFilterContext *avctx, AVFilterLink *link,
  343. int default_pool_size);
  344. #endif /* AVFILTER_INTERNAL_H */