framesync.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. /*
  2. * Copyright (c) 2013 Nicolas George
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public License
  8. * as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with FFmpeg; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #ifndef AVFILTER_FRAMESYNC_H
  21. #define AVFILTER_FRAMESYNC_H
  22. #include "bufferqueue.h"
  23. enum EOFAction {
  24. EOF_ACTION_REPEAT,
  25. EOF_ACTION_ENDALL,
  26. EOF_ACTION_PASS
  27. };
  28. /*
  29. * TODO
  30. * Export convenient options.
  31. */
  32. /**
  33. * This API is intended as a helper for filters that have several video
  34. * input and need to combine them somehow. If the inputs have different or
  35. * variable frame rate, getting the input frames to match requires a rather
  36. * complex logic and a few user-tunable options.
  37. *
  38. * In this API, when a set of synchronized input frames is ready to be
  39. * procesed is called a frame event. Frame event can be generated in
  40. * response to input frames on any or all inputs and the handling of
  41. * situations where some stream extend beyond the beginning or the end of
  42. * others can be configured.
  43. *
  44. * The basic working of this API is the following: set the on_event
  45. * callback, then call ff_framesync_activate() from the filter's activate
  46. * callback.
  47. */
  48. /**
  49. * Stream extrapolation mode
  50. *
  51. * Describe how the frames of a stream are extrapolated before the first one
  52. * and after EOF to keep sync with possibly longer other streams.
  53. */
  54. enum FFFrameSyncExtMode {
  55. /**
  56. * Completely stop all streams with this one.
  57. */
  58. EXT_STOP,
  59. /**
  60. * Ignore this stream and continue processing the other ones.
  61. */
  62. EXT_NULL,
  63. /**
  64. * Extend the frame to infinity.
  65. */
  66. EXT_INFINITY,
  67. };
  68. /**
  69. * Input stream structure
  70. */
  71. typedef struct FFFrameSyncIn {
  72. /**
  73. * Extrapolation mode for timestamps before the first frame
  74. */
  75. enum FFFrameSyncExtMode before;
  76. /**
  77. * Extrapolation mode for timestamps after the last frame
  78. */
  79. enum FFFrameSyncExtMode after;
  80. /**
  81. * Time base for the incoming frames
  82. */
  83. AVRational time_base;
  84. /**
  85. * Current frame, may be NULL before the first one or after EOF
  86. */
  87. AVFrame *frame;
  88. /**
  89. * Next frame, for internal use
  90. */
  91. AVFrame *frame_next;
  92. /**
  93. * PTS of the current frame
  94. */
  95. int64_t pts;
  96. /**
  97. * PTS of the next frame, for internal use
  98. */
  99. int64_t pts_next;
  100. /**
  101. * Boolean flagging the next frame, for internal use
  102. */
  103. uint8_t have_next;
  104. /**
  105. * State: before first, in stream or after EOF, for internal use
  106. */
  107. uint8_t state;
  108. /**
  109. * Synchronization level: frames on input at the highest sync level will
  110. * generate output frame events.
  111. *
  112. * For example, if inputs #0 and #1 have sync level 2 and input #2 has
  113. * sync level 1, then a frame on either input #0 or #1 will generate a
  114. * frame event, but not a frame on input #2 until both inputs #0 and #1
  115. * have reached EOF.
  116. *
  117. * If sync is 0, no frame event will be generated.
  118. */
  119. unsigned sync;
  120. } FFFrameSyncIn;
  121. /**
  122. * Frame sync structure.
  123. */
  124. typedef struct FFFrameSync {
  125. const AVClass *class;
  126. /**
  127. * Parent filter context.
  128. */
  129. AVFilterContext *parent;
  130. /**
  131. * Number of input streams
  132. */
  133. unsigned nb_in;
  134. /**
  135. * Time base for the output events
  136. */
  137. AVRational time_base;
  138. /**
  139. * Timestamp of the current event
  140. */
  141. int64_t pts;
  142. /**
  143. * Callback called when a frame event is ready
  144. */
  145. int (*on_event)(struct FFFrameSync *fs);
  146. /**
  147. * Opaque pointer, not used by the API
  148. */
  149. void *opaque;
  150. /**
  151. * Index of the input that requires a request
  152. */
  153. unsigned in_request;
  154. /**
  155. * Synchronization level: only inputs with the same sync level are sync
  156. * sources.
  157. */
  158. unsigned sync_level;
  159. /**
  160. * Flag indicating that a frame event is ready
  161. */
  162. uint8_t frame_ready;
  163. /**
  164. * Flag indicating that output has reached EOF.
  165. */
  166. uint8_t eof;
  167. /**
  168. * Pointer to array of inputs.
  169. */
  170. FFFrameSyncIn *in;
  171. int opt_repeatlast;
  172. int opt_shortest;
  173. int opt_eof_action;
  174. } FFFrameSync;
  175. /**
  176. * Get the class for the framesync object.
  177. */
  178. const AVClass *ff_framesync_get_class(void);
  179. /**
  180. * Pre-initialize a frame sync structure.
  181. *
  182. * It sets the class pointer and inits the options to their default values.
  183. * The entire structure is expected to be already set to 0.
  184. * This step is optional, but necessary to use the options.
  185. */
  186. void ff_framesync_preinit(FFFrameSync *fs);
  187. /**
  188. * Initialize a frame sync structure.
  189. *
  190. * The entire structure is expected to be already set to 0 or preinited.
  191. *
  192. * @param fs frame sync structure to initialize
  193. * @param parent parent AVFilterContext object
  194. * @param nb_in number of inputs
  195. * @return >= 0 for success or a negative error code
  196. */
  197. int ff_framesync_init(FFFrameSync *fs, AVFilterContext *parent, unsigned nb_in);
  198. /**
  199. * Configure a frame sync structure.
  200. *
  201. * Must be called after all options are set but before all use.
  202. *
  203. * @return >= 0 for success or a negative error code
  204. */
  205. int ff_framesync_configure(FFFrameSync *fs);
  206. /**
  207. * Free all memory currently allocated.
  208. */
  209. void ff_framesync_uninit(FFFrameSync *fs);
  210. /**
  211. * Get the current frame in an input.
  212. *
  213. * @param fs frame sync structure
  214. * @param in index of the input
  215. * @param rframe used to return the current frame (or NULL)
  216. * @param get if not zero, the calling code needs to get ownership of
  217. * the returned frame; the current frame will either be
  218. * duplicated or removed from the framesync structure
  219. */
  220. int ff_framesync_get_frame(FFFrameSync *fs, unsigned in, AVFrame **rframe,
  221. unsigned get);
  222. /**
  223. * Examine the frames in the filter's input and try to produce output.
  224. *
  225. * This function can be the complete implementation of the activate
  226. * method of a filter using framesync.
  227. */
  228. int ff_framesync_activate(FFFrameSync *fs);
  229. /**
  230. * Initialize a frame sync structure for dualinput.
  231. *
  232. * Compared to generic framesync, dualinput assumes the first input is the
  233. * main one and the filtering is performed on it. The first input will be
  234. * the only one with sync set and generic timeline support will just pass it
  235. * unchanged when disabled.
  236. *
  237. * Equivalent to ff_framesync_init(fs, parent, 2) then setting the time
  238. * base, sync and ext modes on the inputs.
  239. */
  240. int ff_framesync_init_dualinput(FFFrameSync *fs, AVFilterContext *parent);
  241. /**
  242. * @param f0 used to return the main frame
  243. * @param f1 used to return the second frame, or NULL if disabled
  244. * @return >=0 for success or AVERROR code
  245. * @note The frame returned in f0 belongs to the caller (get = 1 in
  246. * ff_framesync_get_frame()) while the frame returned in f1 is still owned
  247. * by the framesync structure.
  248. */
  249. int ff_framesync_dualinput_get(FFFrameSync *fs, AVFrame **f0, AVFrame **f1);
  250. /**
  251. * Same as ff_framesync_dualinput_get(), but make sure that f0 is writable.
  252. */
  253. int ff_framesync_dualinput_get_writable(FFFrameSync *fs, AVFrame **f0, AVFrame **f1);
  254. #define FRAMESYNC_DEFINE_CLASS(name, context, field) \
  255. static int name##_framesync_preinit(AVFilterContext *ctx) { \
  256. context *s = ctx->priv; \
  257. ff_framesync_preinit(&s->field); \
  258. return 0; \
  259. } \
  260. static const AVClass *name##_child_class_next(const AVClass *prev) { \
  261. return prev ? NULL : ff_framesync_get_class(); \
  262. } \
  263. static void *name##_child_next(void *obj, void *prev) { \
  264. context *s = obj; \
  265. s->fs.class = ff_framesync_get_class(); /* FIXME */ \
  266. return prev ? NULL : &s->field; \
  267. } \
  268. static const AVClass name##_class = { \
  269. .class_name = #name, \
  270. .item_name = av_default_item_name, \
  271. .option = name##_options, \
  272. .version = LIBAVUTIL_VERSION_INT, \
  273. .category = AV_CLASS_CATEGORY_FILTER, \
  274. .child_class_next = name##_child_class_next, \
  275. .child_next = name##_child_next, \
  276. }
  277. #endif /* AVFILTER_FRAMESYNC_H */