sync_queue.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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 FFTOOLS_SYNC_QUEUE_H
  19. #define FFTOOLS_SYNC_QUEUE_H
  20. #include <stdint.h>
  21. #include "libavcodec/packet.h"
  22. #include "libavutil/frame.h"
  23. enum SyncQueueType {
  24. SYNC_QUEUE_PACKETS,
  25. SYNC_QUEUE_FRAMES,
  26. };
  27. typedef union SyncQueueFrame {
  28. AVFrame *f;
  29. AVPacket *p;
  30. } SyncQueueFrame;
  31. #define SQFRAME(frame) ((SyncQueueFrame){ .f = (frame) })
  32. #define SQPKT(pkt) ((SyncQueueFrame){ .p = (pkt) })
  33. /**
  34. * A sync queue provides timestamp synchronization between multiple streams.
  35. * Some of these streams are marked as "limiting", then the queue ensures no
  36. * stream gets ahead of any of the limiting streams.
  37. */
  38. typedef struct SyncQueue SyncQueue;
  39. /**
  40. * Allocate a sync queue of the given type.
  41. *
  42. * @param buf_size_us maximum duration that will be buffered in microseconds
  43. */
  44. SyncQueue *sq_alloc(enum SyncQueueType type, int64_t buf_size_us, void *logctx);
  45. void sq_free(SyncQueue **sq);
  46. /**
  47. * Add a new stream to the sync queue.
  48. *
  49. * @param limiting whether the stream is limiting, i.e. no other stream can be
  50. * longer than this one
  51. * @return
  52. * - a non-negative stream index on success
  53. * - a negative error code on error
  54. */
  55. int sq_add_stream(SyncQueue *sq, int limiting);
  56. /**
  57. * Limit the number of output frames for stream with index stream_idx
  58. * to max_frames.
  59. */
  60. void sq_limit_frames(SyncQueue *sq, unsigned int stream_idx,
  61. uint64_t max_frames);
  62. /**
  63. * Set a constant output audio frame size, in samples. Can only be used with
  64. * SYNC_QUEUE_FRAMES queues and audio streams.
  65. *
  66. * All output frames will have exactly frame_samples audio samples, except
  67. * possibly for the last one, which may have fewer.
  68. */
  69. void sq_frame_samples(SyncQueue *sq, unsigned int stream_idx,
  70. int frame_samples);
  71. /**
  72. * Submit a frame for the stream with index stream_idx.
  73. *
  74. * On success, the sync queue takes ownership of the frame and will reset the
  75. * contents of the supplied frame. On failure, the frame remains owned by the
  76. * caller.
  77. *
  78. * Sending a frame with NULL contents marks the stream as finished.
  79. *
  80. * @return
  81. * - 0 on success
  82. * - AVERROR_EOF when no more frames should be submitted for this stream
  83. * - another a negative error code on failure
  84. */
  85. int sq_send(SyncQueue *sq, unsigned int stream_idx, SyncQueueFrame frame);
  86. /**
  87. * Read a frame from the queue.
  88. *
  89. * @param stream_idx index of the stream to read a frame for. May be -1, then
  90. * try to read a frame from any stream that is ready for
  91. * output.
  92. * @param frame output frame will be written here on success. The frame is owned
  93. * by the caller.
  94. *
  95. * @return
  96. * - a non-negative index of the stream to which the returned frame belongs
  97. * - AVERROR(EAGAIN) when more frames need to be submitted to the queue
  98. * - AVERROR_EOF when no more frames will be available for this stream (for any
  99. * stream if stream_idx is -1)
  100. * - another negative error code on failure
  101. */
  102. int sq_receive(SyncQueue *sq, int stream_idx, SyncQueueFrame frame);
  103. #endif // FFTOOLS_SYNC_QUEUE_H