framesync.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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. * Array of inputs; all inputs must be in consecutive memory
  173. */
  174. FFFrameSyncIn in[1]; /* must be the last field */
  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. */
  185. void ff_framesync_init(FFFrameSync *fs, void *parent, unsigned nb_in);
  186. /**
  187. * Configure a frame sync structure.
  188. *
  189. * Must be called after all options are set but before all use.
  190. *
  191. * @return >= 0 for success or a negative error code
  192. */
  193. int ff_framesync_configure(FFFrameSync *fs);
  194. /**
  195. * Free all memory currently allocated.
  196. */
  197. void ff_framesync_uninit(FFFrameSync *fs);
  198. /**
  199. * Add a frame to an input
  200. *
  201. * Typically called from the filter_frame() method.
  202. *
  203. * @param fs frame sync structure
  204. * @param in index of the input
  205. * @param frame input frame, or NULL for EOF
  206. */
  207. int ff_framesync_add_frame(FFFrameSync *fs, unsigned in, AVFrame *frame);
  208. /**
  209. * Prepare the next frame event.
  210. *
  211. * The status of the operation can be found in fs->frame_ready and fs->eof.
  212. */
  213. void ff_framesync_next(FFFrameSync *fs);
  214. /**
  215. * Drop the current frame event.
  216. */
  217. void ff_framesync_drop(FFFrameSync *fs);
  218. /**
  219. * Get the current frame in an input.
  220. *
  221. * @param fs frame sync structure
  222. * @param in index of the input
  223. * @param rframe used to return the current frame (or NULL)
  224. * @param get if not zero, the calling code needs to get ownership of
  225. * the returned frame; the current frame will either be
  226. * duplicated or removed from the framesync structure
  227. */
  228. int ff_framesync_get_frame(FFFrameSync *fs, unsigned in, AVFrame **rframe,
  229. unsigned get);
  230. /**
  231. * Process one or several frame using the on_event callback.
  232. *
  233. * @return number of frames processed or negative error code
  234. */
  235. int ff_framesync_process_frame(FFFrameSync *fs, unsigned all);
  236. /**
  237. * Accept a frame on a filter input.
  238. *
  239. * This function can be the complete implementation of all filter_frame
  240. * methods of a filter using framesync.
  241. */
  242. int ff_framesync_filter_frame(FFFrameSync *fs, AVFilterLink *inlink,
  243. AVFrame *in);
  244. /**
  245. * Request a frame on the filter output.
  246. *
  247. * This function can be the complete implementation of all filter_frame
  248. * methods of a filter using framesync if it has only one output.
  249. */
  250. int ff_framesync_request_frame(FFFrameSync *fs, AVFilterLink *outlink);
  251. #endif /* AVFILTER_FRAMESYNC_H */