internal.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  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 "avfiltergraph.h"
  27. #include "formats.h"
  28. #include "framepool.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. };
  135. struct AVFilterInternal {
  136. avfilter_execute_func *execute;
  137. };
  138. /**
  139. * Tell if an integer is contained in the provided -1-terminated list of integers.
  140. * This is useful for determining (for instance) if an AVPixelFormat is in an
  141. * array of supported formats.
  142. *
  143. * @param fmt provided format
  144. * @param fmts -1-terminated list of formats
  145. * @return 1 if present, 0 if absent
  146. */
  147. int ff_fmt_is_in(int fmt, const int *fmts);
  148. /* Functions to parse audio format arguments */
  149. /**
  150. * Parse a pixel format.
  151. *
  152. * @param ret pixel format pointer to where the value should be written
  153. * @param arg string to parse
  154. * @param log_ctx log context
  155. * @return >= 0 in case of success, a negative AVERROR code on error
  156. */
  157. av_warn_unused_result
  158. int ff_parse_pixel_format(enum AVPixelFormat *ret, const char *arg, void *log_ctx);
  159. /**
  160. * Parse a sample rate.
  161. *
  162. * @param ret unsigned integer pointer to where the value should be written
  163. * @param arg string to parse
  164. * @param log_ctx log context
  165. * @return >= 0 in case of success, a negative AVERROR code on error
  166. */
  167. av_warn_unused_result
  168. int ff_parse_sample_rate(int *ret, const char *arg, void *log_ctx);
  169. /**
  170. * Parse a time base.
  171. *
  172. * @param ret unsigned AVRational pointer to where the value should be written
  173. * @param arg string to parse
  174. * @param log_ctx log context
  175. * @return >= 0 in case of success, a negative AVERROR code on error
  176. */
  177. av_warn_unused_result
  178. int ff_parse_time_base(AVRational *ret, const char *arg, void *log_ctx);
  179. /**
  180. * Parse a sample format name or a corresponding integer representation.
  181. *
  182. * @param ret integer pointer to where the value should be written
  183. * @param arg string to parse
  184. * @param log_ctx log context
  185. * @return >= 0 in case of success, a negative AVERROR code on error
  186. */
  187. av_warn_unused_result
  188. int ff_parse_sample_format(int *ret, const char *arg, void *log_ctx);
  189. /**
  190. * Parse a channel layout or a corresponding integer representation.
  191. *
  192. * @param ret 64bit integer pointer to where the value should be written.
  193. * @param nret integer pointer to the number of channels;
  194. * if not NULL, then unknown channel layouts are accepted
  195. * @param arg string to parse
  196. * @param log_ctx log context
  197. * @return >= 0 in case of success, a negative AVERROR code on error
  198. */
  199. av_warn_unused_result
  200. int ff_parse_channel_layout(int64_t *ret, int *nret, const char *arg,
  201. void *log_ctx);
  202. void ff_update_link_current_pts(AVFilterLink *link, int64_t pts);
  203. /**
  204. * Set the status field of a link from the source filter.
  205. * The pts should reflect the timestamp of the status change,
  206. * in link time base and relative to the frames timeline.
  207. * In particular, for AVERROR_EOF, it should reflect the
  208. * end time of the last frame.
  209. */
  210. void ff_avfilter_link_set_in_status(AVFilterLink *link, int status, int64_t pts);
  211. /**
  212. * Set the status field of a link from the destination filter.
  213. * The pts should probably be left unset (AV_NOPTS_VALUE).
  214. */
  215. void ff_avfilter_link_set_out_status(AVFilterLink *link, int status, int64_t pts);
  216. void ff_command_queue_pop(AVFilterContext *filter);
  217. /* misc trace functions */
  218. /* #define FF_AVFILTER_TRACE */
  219. #ifdef FF_AVFILTER_TRACE
  220. # define ff_tlog(pctx, ...) av_log(pctx, AV_LOG_DEBUG, __VA_ARGS__)
  221. #else
  222. # define ff_tlog(pctx, ...) do { if (0) av_log(pctx, AV_LOG_DEBUG, __VA_ARGS__); } while (0)
  223. #endif
  224. #define FF_TPRINTF_START(ctx, func) ff_tlog(NULL, "%-16s: ", #func)
  225. char *ff_get_ref_perms_string(char *buf, size_t buf_size, int perms);
  226. void ff_tlog_ref(void *ctx, AVFrame *ref, int end);
  227. void ff_tlog_link(void *ctx, AVFilterLink *link, int end);
  228. /**
  229. * Insert a new pad.
  230. *
  231. * @param idx Insertion point. Pad is inserted at the end if this point
  232. * is beyond the end of the list of pads.
  233. * @param count Pointer to the number of pads in the list
  234. * @param padidx_off Offset within an AVFilterLink structure to the element
  235. * to increment when inserting a new pad causes link
  236. * numbering to change
  237. * @param pads Pointer to the pointer to the beginning of the list of pads
  238. * @param links Pointer to the pointer to the beginning of the list of links
  239. * @param newpad The new pad to add. A copy is made when adding.
  240. * @return >= 0 in case of success, a negative AVERROR code on error
  241. */
  242. int ff_insert_pad(unsigned idx, unsigned *count, size_t padidx_off,
  243. AVFilterPad **pads, AVFilterLink ***links,
  244. AVFilterPad *newpad);
  245. /** Insert a new input pad for the filter. */
  246. static inline int ff_insert_inpad(AVFilterContext *f, unsigned index,
  247. AVFilterPad *p)
  248. {
  249. return ff_insert_pad(index, &f->nb_inputs, offsetof(AVFilterLink, dstpad),
  250. &f->input_pads, &f->inputs, p);
  251. }
  252. /** Insert a new output pad for the filter. */
  253. static inline int ff_insert_outpad(AVFilterContext *f, unsigned index,
  254. AVFilterPad *p)
  255. {
  256. return ff_insert_pad(index, &f->nb_outputs, offsetof(AVFilterLink, srcpad),
  257. &f->output_pads, &f->outputs, p);
  258. }
  259. /**
  260. * Poll a frame from the filter chain.
  261. *
  262. * @param link the input link
  263. * @return the number of immediately available frames, a negative
  264. * number in case of error
  265. */
  266. int ff_poll_frame(AVFilterLink *link);
  267. /**
  268. * Request an input frame from the filter at the other end of the link.
  269. *
  270. * The input filter may pass the request on to its inputs, fulfill the
  271. * request from an internal buffer or any other means specific to its function.
  272. *
  273. * When the end of a stream is reached AVERROR_EOF is returned and no further
  274. * frames are returned after that.
  275. *
  276. * When a filter is unable to output a frame for example due to its sources
  277. * being unable to do so or because it depends on external means pushing data
  278. * into it then AVERROR(EAGAIN) is returned.
  279. * It is important that a AVERROR(EAGAIN) return is returned all the way to the
  280. * caller (generally eventually a user application) as this step may (but does
  281. * not have to be) necessary to provide the input with the next frame.
  282. *
  283. * If a request is successful then some progress has been made towards
  284. * providing a frame on the link (through ff_filter_frame()). A filter that
  285. * needs several frames to produce one is allowed to return success if one
  286. * more frame has been processed but no output has been produced yet. A
  287. * filter is also allowed to simply forward a success return value.
  288. *
  289. * @param link the input link
  290. * @return zero on success
  291. * AVERROR_EOF on end of file
  292. * AVERROR(EAGAIN) if the previous filter cannot output a frame
  293. * currently and can neither guarantee that EOF has been reached.
  294. */
  295. int ff_request_frame(AVFilterLink *link);
  296. int ff_request_frame_to_filter(AVFilterLink *link);
  297. #define AVFILTER_DEFINE_CLASS(fname) \
  298. static const AVClass fname##_class = { \
  299. .class_name = #fname, \
  300. .item_name = av_default_item_name, \
  301. .option = fname##_options, \
  302. .version = LIBAVUTIL_VERSION_INT, \
  303. .category = AV_CLASS_CATEGORY_FILTER, \
  304. }
  305. /**
  306. * Find the index of a link.
  307. *
  308. * I.e. find i such that link == ctx->(in|out)puts[i]
  309. */
  310. #define FF_INLINK_IDX(link) ((int)((link)->dstpad - (link)->dst->input_pads))
  311. #define FF_OUTLINK_IDX(link) ((int)((link)->srcpad - (link)->src->output_pads))
  312. /**
  313. * Send a frame of data to the next filter.
  314. *
  315. * @param link the output link over which the data is being sent
  316. * @param frame a reference to the buffer of data being sent. The
  317. * receiving filter will free this reference when it no longer
  318. * needs it or pass it on to the next filter.
  319. *
  320. * @return >= 0 on success, a negative AVERROR on error. The receiving filter
  321. * is responsible for unreferencing frame in case of error.
  322. */
  323. int ff_filter_frame(AVFilterLink *link, AVFrame *frame);
  324. /**
  325. * Allocate a new filter context and return it.
  326. *
  327. * @param filter what filter to create an instance of
  328. * @param inst_name name to give to the new filter context
  329. *
  330. * @return newly created filter context or NULL on failure
  331. */
  332. AVFilterContext *ff_filter_alloc(const AVFilter *filter, const char *inst_name);
  333. /**
  334. * Remove a filter from a graph;
  335. */
  336. void ff_filter_graph_remove_filter(AVFilterGraph *graph, AVFilterContext *filter);
  337. /**
  338. * Run one round of processing on a filter graph.
  339. */
  340. int ff_filter_graph_run_once(AVFilterGraph *graph);
  341. /**
  342. * Normalize the qscale factor
  343. * FIXME the H264 qscale is a log based scale, mpeg1/2 is not, the code below
  344. * cannot be optimal
  345. */
  346. static inline int ff_norm_qscale(int qscale, int type)
  347. {
  348. switch (type) {
  349. case FF_QSCALE_TYPE_MPEG1: return qscale;
  350. case FF_QSCALE_TYPE_MPEG2: return qscale >> 1;
  351. case FF_QSCALE_TYPE_H264: return qscale >> 2;
  352. case FF_QSCALE_TYPE_VP56: return (63 - qscale + 2) >> 2;
  353. }
  354. return qscale;
  355. }
  356. #endif /* AVFILTER_INTERNAL_H */