framesync.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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. /*
  24. * TODO
  25. * Callback-based API similar to dualinput.
  26. * Export convenient options.
  27. */
  28. /**
  29. * This API is intended as a helper for filters that have several video
  30. * input and need to combine them somehow. If the inputs have different or
  31. * variable frame rate, getting the input frames to match requires a rather
  32. * complex logic and a few user-tunable options.
  33. *
  34. * In this API, when a set of synchronized input frames is ready to be
  35. * procesed is called a frame event. Frame event can be generated in
  36. * response to input frames on any or all inputs and the handling of
  37. * situations where some stream extend beyond the beginning or the end of
  38. * others can be configured.
  39. *
  40. * The basic working of this API is the following:
  41. *
  42. * - When a frame is available on any input, add it using
  43. * ff_framesync_add_frame().
  44. *
  45. * - When a frame event is ready to be processed (i.e. after adding a frame
  46. * or when requested on input):
  47. * - call ff_framesync_next();
  48. * - if fs->frame_ready is true, process the frames;
  49. * - call ff_framesync_drop().
  50. */
  51. /**
  52. * Stream extrapolation mode
  53. *
  54. * Describe how the frames of a stream are extrapolated before the first one
  55. * and after EOF to keep sync with possibly longer other streams.
  56. */
  57. enum FFFrameSyncExtMode {
  58. /**
  59. * Completely stop all streams with this one.
  60. */
  61. EXT_STOP,
  62. /**
  63. * Ignore this stream and continue processing the other ones.
  64. */
  65. EXT_NULL,
  66. /**
  67. * Extend the frame to infinity.
  68. */
  69. EXT_INFINITY,
  70. };
  71. /**
  72. * Input stream structure
  73. */
  74. typedef struct FFFrameSyncIn {
  75. /**
  76. * Queue of incoming AVFrame, and NULL to mark EOF
  77. */
  78. struct FFBufQueue queue;
  79. /**
  80. * Extrapolation mode for timestamps before the first frame
  81. */
  82. enum FFFrameSyncExtMode before;
  83. /**
  84. * Extrapolation mode for timestamps after the last frame
  85. */
  86. enum FFFrameSyncExtMode after;
  87. /**
  88. * Time base for the incoming frames
  89. */
  90. AVRational time_base;
  91. /**
  92. * Current frame, may be NULL before the first one or after EOF
  93. */
  94. AVFrame *frame;
  95. /**
  96. * Next frame, for internal use
  97. */
  98. AVFrame *frame_next;
  99. /**
  100. * PTS of the current frame
  101. */
  102. int64_t pts;
  103. /**
  104. * PTS of the next frame, for internal use
  105. */
  106. int64_t pts_next;
  107. /**
  108. * Boolean flagging the next frame, for internal use
  109. */
  110. uint8_t have_next;
  111. /**
  112. * State: before first, in stream or after EOF, for internal use
  113. */
  114. uint8_t state;
  115. /**
  116. * Synchronization level: frames on input at the highest sync level will
  117. * generate output frame events.
  118. *
  119. * For example, if inputs #0 and #1 have sync level 2 and input #2 has
  120. * sync level 1, then a frame on either input #0 or #1 will generate a
  121. * frame event, but not a frame on input #2 until both inputs #0 and #1
  122. * have reached EOF.
  123. *
  124. * If sync is 0, no frame event will be generated.
  125. */
  126. unsigned sync;
  127. } FFFrameSyncIn;
  128. /**
  129. * Frame sync structure.
  130. */
  131. typedef struct FFFrameSync {
  132. const AVClass *class;
  133. void *parent;
  134. /**
  135. * Number of input streams
  136. */
  137. unsigned nb_in;
  138. /**
  139. * Time base for the output events
  140. */
  141. AVRational time_base;
  142. /**
  143. * Timestamp of the current event
  144. */
  145. int64_t pts;
  146. /**
  147. * Callback called when a frame event is ready
  148. */
  149. int (*on_event)(struct FFFrameSync *fs);
  150. /**
  151. * Opaque pointer, not used by the API
  152. */
  153. void *opaque;
  154. /**
  155. * Index of the input that requires a request
  156. */
  157. unsigned in_request;
  158. /**
  159. * Synchronization level: only inputs with the same sync level are sync
  160. * sources.
  161. */
  162. unsigned sync_level;
  163. /**
  164. * Flag indicating that a frame event is ready
  165. */
  166. uint8_t frame_ready;
  167. /**
  168. * Flag indicating that output has reached EOF.
  169. */
  170. uint8_t eof;
  171. /**
  172. * Pointer to array of inputs.
  173. */
  174. FFFrameSyncIn *in;
  175. } FFFrameSync;
  176. /**
  177. * Initialize a frame sync structure.
  178. *
  179. * The entire structure is expected to be already set to 0.
  180. *
  181. * @param fs frame sync structure to initialize
  182. * @param parent parent object, used for logging
  183. * @param nb_in number of inputs
  184. * @return >= 0 for success or a negative error code
  185. */
  186. int ff_framesync_init(FFFrameSync *fs, void *parent, unsigned nb_in);
  187. /**
  188. * Configure a frame sync structure.
  189. *
  190. * Must be called after all options are set but before all use.
  191. *
  192. * @return >= 0 for success or a negative error code
  193. */
  194. int ff_framesync_configure(FFFrameSync *fs);
  195. /**
  196. * Free all memory currently allocated.
  197. */
  198. void ff_framesync_uninit(FFFrameSync *fs);
  199. /**
  200. * Add a frame to an input
  201. *
  202. * Typically called from the filter_frame() method.
  203. *
  204. * @param fs frame sync structure
  205. * @param in index of the input
  206. * @param frame input frame, or NULL for EOF
  207. */
  208. int ff_framesync_add_frame(FFFrameSync *fs, unsigned in, AVFrame *frame);
  209. /**
  210. * Prepare the next frame event.
  211. *
  212. * The status of the operation can be found in fs->frame_ready and fs->eof.
  213. */
  214. void ff_framesync_next(FFFrameSync *fs);
  215. /**
  216. * Drop the current frame event.
  217. */
  218. void ff_framesync_drop(FFFrameSync *fs);
  219. /**
  220. * Get the current frame in an input.
  221. *
  222. * @param fs frame sync structure
  223. * @param in index of the input
  224. * @param rframe used to return the current frame (or NULL)
  225. * @param get if not zero, the calling code needs to get ownership of
  226. * the returned frame; the current frame will either be
  227. * duplicated or removed from the framesync structure
  228. */
  229. int ff_framesync_get_frame(FFFrameSync *fs, unsigned in, AVFrame **rframe,
  230. unsigned get);
  231. /**
  232. * Process one or several frame using the on_event callback.
  233. *
  234. * @return number of frames processed or negative error code
  235. */
  236. int ff_framesync_process_frame(FFFrameSync *fs, unsigned all);
  237. /**
  238. * Accept a frame on a filter input.
  239. *
  240. * This function can be the complete implementation of all filter_frame
  241. * methods of a filter using framesync.
  242. */
  243. int ff_framesync_filter_frame(FFFrameSync *fs, AVFilterLink *inlink,
  244. AVFrame *in);
  245. /**
  246. * Request a frame on the filter output.
  247. *
  248. * This function can be the complete implementation of all filter_frame
  249. * methods of a filter using framesync if it has only one output.
  250. */
  251. int ff_framesync_request_frame(FFFrameSync *fs, AVFilterLink *outlink);
  252. #endif /* AVFILTER_FRAMESYNC_H */