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.

410 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 "avfiltergraph.h"
  27. #include "formats.h"
  28. #include "thread.h"
  29. #include "version.h"
  30. #include "video.h"
  31. #include "libavcodec/avcodec.h"
  32. #if FF_API_AVFILTERBUFFER
  33. #define POOL_SIZE 32
  34. typedef struct AVFilterPool {
  35. AVFilterBufferRef *pic[POOL_SIZE];
  36. int count;
  37. int refcount;
  38. int draining;
  39. } AVFilterPool;
  40. #endif
  41. typedef struct AVFilterCommand {
  42. double time; ///< time expressed in seconds
  43. char *command; ///< command
  44. char *arg; ///< optional argument for the command
  45. int flags;
  46. struct AVFilterCommand *next;
  47. } AVFilterCommand;
  48. /**
  49. * Update the position of a link in the age heap.
  50. */
  51. void ff_avfilter_graph_update_heap(AVFilterGraph *graph, AVFilterLink *link);
  52. #if !FF_API_AVFILTERPAD_PUBLIC
  53. /**
  54. * A filter pad used for either input or output.
  55. */
  56. struct AVFilterPad {
  57. /**
  58. * Pad name. The name is unique among inputs and among outputs, but an
  59. * input may have the same name as an output. This may be NULL if this
  60. * pad has no need to ever be referenced by name.
  61. */
  62. const char *name;
  63. /**
  64. * AVFilterPad type.
  65. */
  66. enum AVMediaType type;
  67. /**
  68. * Callback function to get a video buffer. If NULL, the filter system will
  69. * use ff_default_get_video_buffer().
  70. *
  71. * Input video pads only.
  72. */
  73. AVFrame *(*get_video_buffer)(AVFilterLink *link, int w, int h);
  74. /**
  75. * Callback function to get an audio buffer. If NULL, the filter system will
  76. * use ff_default_get_audio_buffer().
  77. *
  78. * Input audio pads only.
  79. */
  80. AVFrame *(*get_audio_buffer)(AVFilterLink *link, int nb_samples);
  81. /**
  82. * Filtering callback. This is where a filter receives a frame with
  83. * audio/video data and should do its processing.
  84. *
  85. * Input pads only.
  86. *
  87. * @return >= 0 on success, a negative AVERROR on error. This function
  88. * must ensure that samplesref is properly unreferenced on error if it
  89. * hasn't been passed on to another filter.
  90. */
  91. int (*filter_frame)(AVFilterLink *link, AVFrame *frame);
  92. /**
  93. * Frame poll callback. This returns the number of immediately available
  94. * samples. It should return a positive value if the next request_frame()
  95. * is guaranteed to return one frame (with no delay).
  96. *
  97. * Defaults to just calling the source poll_frame() method.
  98. *
  99. * Output pads only.
  100. */
  101. int (*poll_frame)(AVFilterLink *link);
  102. /**
  103. * Frame request callback. A call to this should result in at least one
  104. * frame being output over the given link. This should return zero on
  105. * success, and another value on error.
  106. *
  107. * Output pads only.
  108. */
  109. int (*request_frame)(AVFilterLink *link);
  110. /**
  111. * Link configuration callback.
  112. *
  113. * For output pads, this should set the link properties such as
  114. * width/height. This should NOT set the format property - that is
  115. * negotiated between filters by the filter system using the
  116. * query_formats() callback before this function is called.
  117. *
  118. * For input pads, this should check the properties of the link, and update
  119. * the filter's internal state as necessary.
  120. *
  121. * For both input and output filters, this should return zero on success,
  122. * and another value on error.
  123. */
  124. int (*config_props)(AVFilterLink *link);
  125. /**
  126. * The filter expects a fifo to be inserted on its input link,
  127. * typically because it has a delay.
  128. *
  129. * input pads only.
  130. */
  131. int needs_fifo;
  132. /**
  133. * The filter expects writable frames from its input link,
  134. * duplicating data buffers if needed.
  135. *
  136. * input pads only.
  137. */
  138. int needs_writable;
  139. };
  140. #endif
  141. struct AVFilterGraphInternal {
  142. void *thread;
  143. avfilter_execute_func *thread_execute;
  144. };
  145. struct AVFilterInternal {
  146. avfilter_execute_func *execute;
  147. };
  148. #if FF_API_AVFILTERBUFFER
  149. /** default handler for freeing audio/video buffer when there are no references left */
  150. void ff_avfilter_default_free_buffer(AVFilterBuffer *buf);
  151. #endif
  152. /** Tell is a format is contained in the provided list terminated by -1. */
  153. int ff_fmt_is_in(int fmt, const int *fmts);
  154. /* Functions to parse audio format arguments */
  155. /**
  156. * Parse a pixel format.
  157. *
  158. * @param ret pixel format pointer to where the value should be written
  159. * @param arg string to parse
  160. * @param log_ctx log context
  161. * @return >= 0 in case of success, a negative AVERROR code on error
  162. */
  163. int ff_parse_pixel_format(enum AVPixelFormat *ret, const char *arg, void *log_ctx);
  164. /**
  165. * Parse a sample rate.
  166. *
  167. * @param ret unsigned integer pointer to where the value should be written
  168. * @param arg string to parse
  169. * @param log_ctx log context
  170. * @return >= 0 in case of success, a negative AVERROR code on error
  171. */
  172. int ff_parse_sample_rate(int *ret, const char *arg, void *log_ctx);
  173. /**
  174. * Parse a time base.
  175. *
  176. * @param ret unsigned AVRational pointer to where the value should be written
  177. * @param arg string to parse
  178. * @param log_ctx log context
  179. * @return >= 0 in case of success, a negative AVERROR code on error
  180. */
  181. int ff_parse_time_base(AVRational *ret, const char *arg, void *log_ctx);
  182. /**
  183. * Parse a sample format name or a corresponding integer representation.
  184. *
  185. * @param ret integer pointer to where the value should be written
  186. * @param arg string to parse
  187. * @param log_ctx log context
  188. * @return >= 0 in case of success, a negative AVERROR code on error
  189. */
  190. int ff_parse_sample_format(int *ret, const char *arg, void *log_ctx);
  191. /**
  192. * Parse a channel layout or a corresponding integer representation.
  193. *
  194. * @param ret 64bit integer pointer to where the value should be written.
  195. * @param nret integer pointer to the number of channels;
  196. * if not NULL, then unknown channel layouts are accepted
  197. * @param arg string to parse
  198. * @param log_ctx log context
  199. * @return >= 0 in case of success, a negative AVERROR code on error
  200. */
  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. void ff_command_queue_pop(AVFilterContext *filter);
  205. /* misc trace functions */
  206. /* #define FF_AVFILTER_TRACE */
  207. #ifdef FF_AVFILTER_TRACE
  208. # define ff_tlog(pctx, ...) av_log(pctx, AV_LOG_DEBUG, __VA_ARGS__)
  209. #else
  210. # define ff_tlog(pctx, ...) do { if (0) av_log(pctx, AV_LOG_DEBUG, __VA_ARGS__); } while (0)
  211. #endif
  212. #define FF_TPRINTF_START(ctx, func) ff_tlog(NULL, "%-16s: ", #func)
  213. char *ff_get_ref_perms_string(char *buf, size_t buf_size, int perms);
  214. void ff_tlog_ref(void *ctx, AVFrame *ref, int end);
  215. void ff_tlog_link(void *ctx, AVFilterLink *link, int end);
  216. /**
  217. * Insert a new pad.
  218. *
  219. * @param idx Insertion point. Pad is inserted at the end if this point
  220. * is beyond the end of the list of pads.
  221. * @param count Pointer to the number of pads in the list
  222. * @param padidx_off Offset within an AVFilterLink structure to the element
  223. * to increment when inserting a new pad causes link
  224. * numbering to change
  225. * @param pads Pointer to the pointer to the beginning of the list of pads
  226. * @param links Pointer to the pointer to the beginning of the list of links
  227. * @param newpad The new pad to add. A copy is made when adding.
  228. * @return >= 0 in case of success, a negative AVERROR code on error
  229. */
  230. int ff_insert_pad(unsigned idx, unsigned *count, size_t padidx_off,
  231. AVFilterPad **pads, AVFilterLink ***links,
  232. AVFilterPad *newpad);
  233. /** Insert a new input pad for the filter. */
  234. static inline int ff_insert_inpad(AVFilterContext *f, unsigned index,
  235. AVFilterPad *p)
  236. {
  237. int ret = ff_insert_pad(index, &f->nb_inputs, offsetof(AVFilterLink, dstpad),
  238. &f->input_pads, &f->inputs, p);
  239. #if FF_API_FOO_COUNT
  240. FF_DISABLE_DEPRECATION_WARNINGS
  241. f->input_count = f->nb_inputs;
  242. FF_ENABLE_DEPRECATION_WARNINGS
  243. #endif
  244. return ret;
  245. }
  246. /** Insert a new output pad for the filter. */
  247. static inline int ff_insert_outpad(AVFilterContext *f, unsigned index,
  248. AVFilterPad *p)
  249. {
  250. int ret = ff_insert_pad(index, &f->nb_outputs, offsetof(AVFilterLink, srcpad),
  251. &f->output_pads, &f->outputs, p);
  252. #if FF_API_FOO_COUNT
  253. FF_DISABLE_DEPRECATION_WARNINGS
  254. f->output_count = f->nb_outputs;
  255. FF_ENABLE_DEPRECATION_WARNINGS
  256. #endif
  257. return ret;
  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 the filter_frame() function will be called
  284. * at least once before ff_request_frame() returns
  285. *
  286. * @param link the input link
  287. * @return zero on success
  288. * AVERROR_EOF on end of file
  289. * AVERROR(EAGAIN) if the previous filter cannot output a frame
  290. * currently and can neither guarantee that EOF has been reached.
  291. */
  292. int ff_request_frame(AVFilterLink *link);
  293. #define AVFILTER_DEFINE_CLASS(fname) \
  294. static const AVClass fname##_class = { \
  295. .class_name = #fname, \
  296. .item_name = av_default_item_name, \
  297. .option = fname##_options, \
  298. .version = LIBAVUTIL_VERSION_INT, \
  299. .category = AV_CLASS_CATEGORY_FILTER, \
  300. }
  301. /**
  302. * Find the index of a link.
  303. *
  304. * I.e. find i such that link == ctx->(in|out)puts[i]
  305. */
  306. #define FF_INLINK_IDX(link) ((int)((link)->dstpad - (link)->dst->input_pads))
  307. #define FF_OUTLINK_IDX(link) ((int)((link)->srcpad - (link)->src->output_pads))
  308. /**
  309. * Send a frame of data to the next filter.
  310. *
  311. * @param link the output link over which the data is being sent
  312. * @param frame a reference to the buffer of data being sent. The
  313. * receiving filter will free this reference when it no longer
  314. * needs it or pass it on to the next filter.
  315. *
  316. * @return >= 0 on success, a negative AVERROR on error. The receiving filter
  317. * is responsible for unreferencing frame in case of error.
  318. */
  319. int ff_filter_frame(AVFilterLink *link, AVFrame *frame);
  320. /**
  321. * Flags for AVFilterLink.flags.
  322. */
  323. enum {
  324. /**
  325. * Frame requests may need to loop in order to be fulfilled.
  326. * A filter must set this flags on an output link if it may return 0 in
  327. * request_frame() without filtering a frame.
  328. */
  329. FF_LINK_FLAG_REQUEST_LOOP = 1,
  330. };
  331. /**
  332. * Allocate a new filter context and return it.
  333. *
  334. * @param filter what filter to create an instance of
  335. * @param inst_name name to give to the new filter context
  336. *
  337. * @return newly created filter context or NULL on failure
  338. */
  339. AVFilterContext *ff_filter_alloc(const AVFilter *filter, const char *inst_name);
  340. /**
  341. * Remove a filter from a graph;
  342. */
  343. void ff_filter_graph_remove_filter(AVFilterGraph *graph, AVFilterContext *filter);
  344. /**
  345. * Normalize the qscale factor
  346. * FIXME the H264 qscale is a log based scale, mpeg1/2 is not, the code below
  347. * cannot be optimal
  348. */
  349. static inline int ff_norm_qscale(int qscale, int type)
  350. {
  351. switch (type) {
  352. case FF_QSCALE_TYPE_MPEG1: return qscale;
  353. case FF_QSCALE_TYPE_MPEG2: return qscale >> 1;
  354. case FF_QSCALE_TYPE_H264: return qscale >> 2;
  355. case FF_QSCALE_TYPE_VP56: return (63 - qscale + 2) >> 2;
  356. }
  357. return qscale;
  358. }
  359. #endif /* AVFILTER_INTERNAL_H */