ffmpeg_sched.h 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  1. /*
  2. * Inter-thread scheduling/synchronization.
  3. * Copyright (c) 2023 Anton Khirnov
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #ifndef FFTOOLS_FFMPEG_SCHED_H
  22. #define FFTOOLS_FFMPEG_SCHED_H
  23. #include <stddef.h>
  24. #include <stdint.h>
  25. #include "ffmpeg_utils.h"
  26. /*
  27. * This file contains the API for the transcode scheduler.
  28. *
  29. * Overall architecture of the transcoding process involves instances of the
  30. * following components:
  31. * - demuxers, each containing any number of demuxed streams; demuxed packets
  32. * belonging to some stream are sent to any number of decoders (transcoding)
  33. * and/or muxers (streamcopy);
  34. * - decoders, which receive encoded packets from some demuxed stream or
  35. * encoder, decode them, and send decoded frames to any number of filtergraph
  36. * inputs (audio/video) or encoders (subtitles);
  37. * - filtergraphs, each containing zero or more inputs (0 in case the
  38. * filtergraph contains a lavfi source filter), and one or more outputs; the
  39. * inputs and outputs need not have matching media types;
  40. * each filtergraph input receives decoded frames from some decoder or another
  41. * filtergraph output;
  42. * filtered frames from each output are sent to some encoder;
  43. * - encoders, which receive decoded frames from some decoder (subtitles) or
  44. * some filtergraph output (audio/video), encode them, and send encoded
  45. * packets to any number of muxed streams or decoders;
  46. * - muxers, each containing any number of muxed streams; each muxed stream
  47. * receives encoded packets from some demuxed stream (streamcopy) or some
  48. * encoder (transcoding); those packets are interleaved and written out by the
  49. * muxer.
  50. *
  51. * The structure formed by the above components is a directed acyclic graph
  52. * (absence of cycles is checked at startup).
  53. *
  54. * There must be at least one muxer instance, otherwise the transcode produces
  55. * no output and is meaningless. Otherwise, in a generic transcoding scenario
  56. * there may be arbitrary number of instances of any of the above components,
  57. * interconnected in various ways.
  58. *
  59. * The code tries to keep all the output streams across all the muxers in sync
  60. * (i.e. at the same DTS), which is accomplished by varying the rates at which
  61. * packets are read from different demuxers and lavfi sources. Note that the
  62. * degree of control we have over synchronization is fundamentally limited - if
  63. * some demuxed streams in the same input are interleaved at different rates
  64. * than that at which they are to be muxed (e.g. because an input file is badly
  65. * interleaved, or the user changed their speed by mismatching amounts), then
  66. * there will be increasing amounts of buffering followed by eventual
  67. * transcoding failure.
  68. *
  69. * N.B. 1: there are meaningful transcode scenarios with no demuxers, e.g.
  70. * - encoding and muxing output from filtergraph(s) that have no inputs;
  71. * - creating a file that contains nothing but attachments and/or metadata.
  72. *
  73. * N.B. 2: a filtergraph output could, in principle, feed multiple encoders, but
  74. * this is unnecessary because the (a)split filter provides the same
  75. * functionality.
  76. *
  77. * The scheduler, in the above model, is the master object that oversees and
  78. * facilitates the transcoding process. The basic idea is that all instances
  79. * of the abovementioned components communicate only with the scheduler and not
  80. * with each other. The scheduler is then the single place containing the
  81. * knowledge about the whole transcoding pipeline.
  82. */
  83. struct AVFrame;
  84. struct AVPacket;
  85. typedef struct Scheduler Scheduler;
  86. enum SchedulerNodeType {
  87. SCH_NODE_TYPE_NONE = 0,
  88. SCH_NODE_TYPE_DEMUX,
  89. SCH_NODE_TYPE_MUX,
  90. SCH_NODE_TYPE_DEC,
  91. SCH_NODE_TYPE_ENC,
  92. SCH_NODE_TYPE_FILTER_IN,
  93. SCH_NODE_TYPE_FILTER_OUT,
  94. };
  95. typedef struct SchedulerNode {
  96. enum SchedulerNodeType type;
  97. unsigned idx;
  98. unsigned idx_stream;
  99. } SchedulerNode;
  100. typedef int (*SchThreadFunc)(void *arg);
  101. #define SCH_DSTREAM(file, stream) \
  102. (SchedulerNode){ .type = SCH_NODE_TYPE_DEMUX, \
  103. .idx = file, .idx_stream = stream }
  104. #define SCH_MSTREAM(file, stream) \
  105. (SchedulerNode){ .type = SCH_NODE_TYPE_MUX, \
  106. .idx = file, .idx_stream = stream }
  107. #define SCH_DEC(decoder) \
  108. (SchedulerNode){ .type = SCH_NODE_TYPE_DEC, \
  109. .idx = decoder }
  110. #define SCH_ENC(encoder) \
  111. (SchedulerNode){ .type = SCH_NODE_TYPE_ENC, \
  112. .idx = encoder }
  113. #define SCH_FILTER_IN(filter, input) \
  114. (SchedulerNode){ .type = SCH_NODE_TYPE_FILTER_IN, \
  115. .idx = filter, .idx_stream = input }
  116. #define SCH_FILTER_OUT(filter, output) \
  117. (SchedulerNode){ .type = SCH_NODE_TYPE_FILTER_OUT, \
  118. .idx = filter, .idx_stream = output }
  119. Scheduler *sch_alloc(void);
  120. void sch_free(Scheduler **sch);
  121. int sch_start(Scheduler *sch);
  122. int sch_stop(Scheduler *sch, int64_t *finish_ts);
  123. /**
  124. * Wait until transcoding terminates or the specified timeout elapses.
  125. *
  126. * @param timeout_us Amount of time in microseconds after which this function
  127. * will timeout.
  128. * @param transcode_ts Current transcode timestamp in AV_TIME_BASE_Q, for
  129. * informational purposes only.
  130. *
  131. * @retval 0 waiting timed out, transcoding is not finished
  132. * @retval 1 transcoding is finished
  133. */
  134. int sch_wait(Scheduler *sch, uint64_t timeout_us, int64_t *transcode_ts);
  135. /**
  136. * Add a demuxer to the scheduler.
  137. *
  138. * @param func Function executed as the demuxer task.
  139. * @param ctx Demuxer state; will be passed to func and used for logging.
  140. *
  141. * @retval ">=0" Index of the newly-created demuxer.
  142. * @retval "<0" Error code.
  143. */
  144. int sch_add_demux(Scheduler *sch, SchThreadFunc func, void *ctx);
  145. /**
  146. * Add a demuxed stream for a previously added demuxer.
  147. *
  148. * @param demux_idx index previously returned by sch_add_demux()
  149. *
  150. * @retval ">=0" Index of the newly-created demuxed stream.
  151. * @retval "<0" Error code.
  152. */
  153. int sch_add_demux_stream(Scheduler *sch, unsigned demux_idx);
  154. /**
  155. * Add a decoder to the scheduler.
  156. *
  157. * @param func Function executed as the decoder task.
  158. * @param ctx Decoder state; will be passed to func and used for logging.
  159. * @param send_end_ts The decoder will return an end timestamp after flush packets
  160. * are delivered to it. See documentation for
  161. * sch_dec_receive() for more details.
  162. *
  163. * @retval ">=0" Index of the newly-created decoder.
  164. * @retval "<0" Error code.
  165. */
  166. int sch_add_dec(Scheduler *sch, SchThreadFunc func, void *ctx,
  167. int send_end_ts);
  168. /**
  169. * Add a filtergraph to the scheduler.
  170. *
  171. * @param nb_inputs Number of filtergraph inputs.
  172. * @param nb_outputs number of filtergraph outputs
  173. * @param func Function executed as the filtering task.
  174. * @param ctx Filter state; will be passed to func and used for logging.
  175. *
  176. * @retval ">=0" Index of the newly-created filtergraph.
  177. * @retval "<0" Error code.
  178. */
  179. int sch_add_filtergraph(Scheduler *sch, unsigned nb_inputs, unsigned nb_outputs,
  180. SchThreadFunc func, void *ctx);
  181. /**
  182. * Add a muxer to the scheduler.
  183. *
  184. * Note that muxer thread startup is more complicated than for other components,
  185. * because
  186. * - muxer streams fed by audio/video encoders become initialized dynamically at
  187. * runtime, after those encoders receive their first frame and initialize
  188. * themselves, followed by calling sch_mux_stream_ready()
  189. * - the header can be written after all the streams for a muxer are initialized
  190. * - we may need to write an SDP, which must happen
  191. * - AFTER all the headers are written
  192. * - BEFORE any packets are written by any muxer
  193. * - with all the muxers quiescent
  194. * To avoid complicated muxer-thread synchronization dances, we postpone
  195. * starting the muxer threads until after the SDP is written. The sequence of
  196. * events is then as follows:
  197. * - After sch_mux_stream_ready() is called for all the streams in a given muxer,
  198. * the header for that muxer is written (care is taken that headers for
  199. * different muxers are not written concurrently, since they write file
  200. * information to stderr). If SDP is not wanted, the muxer thread then starts
  201. * and muxing begins.
  202. * - When SDP _is_ wanted, no muxer threads start until the header for the last
  203. * muxer is written. After that, the SDP is written, after which all the muxer
  204. * threads are started at once.
  205. *
  206. * In order for the above to work, the scheduler needs to be able to invoke
  207. * just writing the header, which is the reason the init parameter exists.
  208. *
  209. * @param func Function executed as the muxing task.
  210. * @param init Callback that is called to initialize the muxer and write the
  211. * header. Called after sch_mux_stream_ready() is called for all the
  212. * streams in the muxer.
  213. * @param ctx Muxer state; will be passed to func/init and used for logging.
  214. * @param sdp_auto Determines automatic SDP writing - see sch_sdp_filename().
  215. * @param thread_queue_size number of packets that can be buffered before
  216. * sending to the muxer blocks
  217. *
  218. * @retval ">=0" Index of the newly-created muxer.
  219. * @retval "<0" Error code.
  220. */
  221. int sch_add_mux(Scheduler *sch, SchThreadFunc func, int (*init)(void *),
  222. void *ctx, int sdp_auto, unsigned thread_queue_size);
  223. /**
  224. * Default size of a packet thread queue. For muxing this can be overridden by
  225. * the thread_queue_size option as passed to a call to sch_add_mux().
  226. */
  227. #define DEFAULT_PACKET_THREAD_QUEUE_SIZE 8
  228. /**
  229. * Default size of a frame thread queue.
  230. */
  231. #define DEFAULT_FRAME_THREAD_QUEUE_SIZE 8
  232. /**
  233. * Add a muxed stream for a previously added muxer.
  234. *
  235. * @param mux_idx index previously returned by sch_add_mux()
  236. *
  237. * @retval ">=0" Index of the newly-created muxed stream.
  238. * @retval "<0" Error code.
  239. */
  240. int sch_add_mux_stream(Scheduler *sch, unsigned mux_idx);
  241. /**
  242. * Configure limits on packet buffering performed before the muxer task is
  243. * started.
  244. *
  245. * @param mux_idx index previously returned by sch_add_mux()
  246. * @param stream_idx_idx index previously returned by sch_add_mux_stream()
  247. * @param data_threshold Total size of the buffered packets' data after which
  248. * max_packets applies.
  249. * @param max_packets maximum Maximum number of buffered packets after
  250. * data_threshold is reached.
  251. */
  252. void sch_mux_stream_buffering(Scheduler *sch, unsigned mux_idx, unsigned stream_idx,
  253. size_t data_threshold, int max_packets);
  254. /**
  255. * Signal to the scheduler that the specified muxed stream is initialized and
  256. * ready. Muxing is started once all the streams are ready.
  257. */
  258. int sch_mux_stream_ready(Scheduler *sch, unsigned mux_idx, unsigned stream_idx);
  259. /**
  260. * Set the file path for the SDP.
  261. *
  262. * The SDP is written when either of the following is true:
  263. * - this function is called at least once
  264. * - sdp_auto=1 is passed to EVERY call of sch_add_mux()
  265. */
  266. int sch_sdp_filename(Scheduler *sch, const char *sdp_filename);
  267. /**
  268. * Add an encoder to the scheduler.
  269. *
  270. * @param func Function executed as the encoding task.
  271. * @param ctx Encoder state; will be passed to func and used for logging.
  272. * @param open_cb This callback, if specified, will be called when the first
  273. * frame is obtained for this encoder. For audio encoders with a
  274. * fixed frame size (which use a sync queue in the scheduler to
  275. * rechunk frames), it must return that frame size on success.
  276. * Otherwise (non-audio, variable frame size) it should return 0.
  277. *
  278. * @retval ">=0" Index of the newly-created encoder.
  279. * @retval "<0" Error code.
  280. */
  281. int sch_add_enc(Scheduler *sch, SchThreadFunc func, void *ctx,
  282. int (*open_cb)(void *func_arg, const struct AVFrame *frame));
  283. /**
  284. * Add an pre-encoding sync queue to the scheduler.
  285. *
  286. * @param buf_size_us Sync queue buffering size, passed to sq_alloc().
  287. * @param logctx Logging context for the sync queue. passed to sq_alloc().
  288. *
  289. * @retval ">=0" Index of the newly-created sync queue.
  290. * @retval "<0" Error code.
  291. */
  292. int sch_add_sq_enc(Scheduler *sch, uint64_t buf_size_us, void *logctx);
  293. int sch_sq_add_enc(Scheduler *sch, unsigned sq_idx, unsigned enc_idx,
  294. int limiting, uint64_t max_frames);
  295. int sch_connect(Scheduler *sch, SchedulerNode src, SchedulerNode dst);
  296. enum DemuxSendFlags {
  297. /**
  298. * Treat the packet as an EOF for SCH_NODE_TYPE_MUX destinations
  299. * send normally to other types.
  300. */
  301. DEMUX_SEND_STREAMCOPY_EOF = (1 << 0),
  302. };
  303. /**
  304. * Called by demuxer tasks to communicate with their downstreams. The following
  305. * may be sent:
  306. * - a demuxed packet for the stream identified by pkt->stream_index;
  307. * - demuxer discontinuity/reset (e.g. after a seek) - this is signalled by an
  308. * empty packet with stream_index=-1.
  309. *
  310. * @param demux_idx demuxer index
  311. * @param pkt A demuxed packet to send.
  312. * When flushing (i.e. pkt->stream_index=-1 on entry to this
  313. * function), on successful return pkt->pts/pkt->time_base will be
  314. * set to the maximum end timestamp of any decoded audio stream, or
  315. * AV_NOPTS_VALUE if no decoded audio streams are present.
  316. *
  317. * @retval "non-negative value" success
  318. * @retval AVERROR_EOF all consumers for the stream are done
  319. * @retval AVERROR_EXIT all consumers are done, should terminate demuxing
  320. * @retval "anoter negative error code" other failure
  321. */
  322. int sch_demux_send(Scheduler *sch, unsigned demux_idx, struct AVPacket *pkt,
  323. unsigned flags);
  324. /**
  325. * Called by decoder tasks to receive a packet for decoding.
  326. *
  327. * @param dec_idx decoder index
  328. * @param pkt Input packet will be written here on success.
  329. *
  330. * An empty packet signals that the decoder should be flushed, but
  331. * more packets will follow (e.g. after seeking). When a decoder
  332. * created with send_end_ts=1 receives a flush packet, it must write
  333. * the end timestamp of the stream after flushing to
  334. * pkt->pts/time_base on the next call to this function (if any).
  335. *
  336. * @retval "non-negative value" success
  337. * @retval AVERROR_EOF no more packets will arrive, should terminate decoding
  338. * @retval "another negative error code" other failure
  339. */
  340. int sch_dec_receive(Scheduler *sch, unsigned dec_idx, struct AVPacket *pkt);
  341. /**
  342. * Called by decoder tasks to send a decoded frame downstream.
  343. *
  344. * @param dec_idx Decoder index previously returned by sch_add_dec().
  345. * @param frame Decoded frame; on success it is consumed and cleared by this
  346. * function
  347. *
  348. * @retval ">=0" success
  349. * @retval AVERROR_EOF all consumers are done, should terminate decoding
  350. * @retval "another negative error code" other failure
  351. */
  352. int sch_dec_send(Scheduler *sch, unsigned dec_idx, struct AVFrame *frame);
  353. /**
  354. * Called by filtergraph tasks to obtain frames for filtering. Will wait for a
  355. * frame to become available and return it in frame.
  356. *
  357. * Filtergraphs that contain lavfi sources and do not currently require new
  358. * input frames should call this function as a means of rate control - then
  359. * in_idx should be set equal to nb_inputs on entry to this function.
  360. *
  361. * @param fg_idx Filtergraph index previously returned by sch_add_filtergraph().
  362. * @param[in,out] in_idx On input contains the index of the input on which a frame
  363. * is most desired. May be set to nb_inputs to signal that
  364. * the filtergraph does not need more input currently.
  365. *
  366. * On success, will be replaced with the input index of
  367. * the actually returned frame or EOF timestamp.
  368. *
  369. * @retval ">=0" Frame data or EOF timestamp was delivered into frame, in_idx
  370. * contains the index of the input it belongs to.
  371. * @retval AVERROR(EAGAIN) No frame was returned, the filtergraph should
  372. * resume filtering. May only be returned when
  373. * in_idx=nb_inputs on entry to this function.
  374. * @retval AVERROR_EOF No more frames will arrive, should terminate filtering.
  375. */
  376. int sch_filter_receive(Scheduler *sch, unsigned fg_idx,
  377. unsigned *in_idx, struct AVFrame *frame);
  378. /**
  379. * Called by filter tasks to signal that a filter input will no longer accept input.
  380. *
  381. * @param fg_idx Filtergraph index previously returned from sch_add_filtergraph().
  382. * @param in_idx Index of the input to finish.
  383. */
  384. void sch_filter_receive_finish(Scheduler *sch, unsigned fg_idx, unsigned in_idx);
  385. /**
  386. * Called by filtergraph tasks to send a filtered frame or EOF to consumers.
  387. *
  388. * @param fg_idx Filtergraph index previously returned by sch_add_filtergraph().
  389. * @param out_idx Index of the output which produced the frame.
  390. * @param frame The frame to send to consumers. When NULL, signals that no more
  391. * frames will be produced for the specified output. When non-NULL,
  392. * the frame is consumed and cleared by this function on success.
  393. *
  394. * @retval "non-negative value" success
  395. * @retval AVERROR_EOF all consumers are done
  396. * @retval "anoter negative error code" other failure
  397. */
  398. int sch_filter_send(Scheduler *sch, unsigned fg_idx, unsigned out_idx,
  399. struct AVFrame *frame);
  400. int sch_filter_command(Scheduler *sch, unsigned fg_idx, struct AVFrame *frame);
  401. /**
  402. * Called by encoder tasks to obtain frames for encoding. Will wait for a frame
  403. * to become available and return it in frame.
  404. *
  405. * @param enc_idx Encoder index previously returned by sch_add_enc().
  406. * @param frame Newly-received frame will be stored here on success. Must be
  407. * clean on entrance to this function.
  408. *
  409. * @retval 0 A frame was successfully delivered into frame.
  410. * @retval AVERROR_EOF No more frames will be delivered, the encoder should
  411. * flush everything and terminate.
  412. *
  413. */
  414. int sch_enc_receive(Scheduler *sch, unsigned enc_idx, struct AVFrame *frame);
  415. /**
  416. * Called by encoder tasks to send encoded packets downstream.
  417. *
  418. * @param enc_idx Encoder index previously returned by sch_add_enc().
  419. * @param pkt An encoded packet; it will be consumed and cleared by this
  420. * function on success.
  421. *
  422. * @retval 0 success
  423. * @retval "<0" Error code.
  424. */
  425. int sch_enc_send (Scheduler *sch, unsigned enc_idx, struct AVPacket *pkt);
  426. /**
  427. * Called by muxer tasks to obtain packets for muxing. Will wait for a packet
  428. * for any muxed stream to become available and return it in pkt.
  429. *
  430. * @param mux_idx Muxer index previously returned by sch_add_mux().
  431. * @param pkt Newly-received packet will be stored here on success. Must be
  432. * clean on entrance to this function.
  433. *
  434. * @retval 0 A packet was successfully delivered into pkt. Its stream_index
  435. * corresponds to a stream index previously returned from
  436. * sch_add_mux_stream().
  437. * @retval AVERROR_EOF When pkt->stream_index is non-negative, this signals that
  438. * no more packets will be delivered for this stream index.
  439. * Otherwise this indicates that no more packets will be
  440. * delivered for any stream and the muxer should therefore
  441. * flush everything and terminate.
  442. */
  443. int sch_mux_receive(Scheduler *sch, unsigned mux_idx, struct AVPacket *pkt);
  444. /**
  445. * Called by muxer tasks to signal that a stream will no longer accept input.
  446. *
  447. * @param stream_idx Stream index previously returned from sch_add_mux_stream().
  448. */
  449. void sch_mux_receive_finish(Scheduler *sch, unsigned mux_idx, unsigned stream_idx);
  450. int sch_mux_sub_heartbeat_add(Scheduler *sch, unsigned mux_idx, unsigned stream_idx,
  451. unsigned dec_idx);
  452. int sch_mux_sub_heartbeat(Scheduler *sch, unsigned mux_idx, unsigned stream_idx,
  453. const AVPacket *pkt);
  454. #endif /* FFTOOLS_FFMPEG_SCHED_H */