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.

422 lines
14KB

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