thread_queue.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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. #include <stdint.h>
  19. #include <string.h>
  20. #include "libavutil/avassert.h"
  21. #include "libavutil/container_fifo.h"
  22. #include "libavutil/error.h"
  23. #include "libavutil/fifo.h"
  24. #include "libavutil/frame.h"
  25. #include "libavutil/intreadwrite.h"
  26. #include "libavutil/mem.h"
  27. #include "libavutil/thread.h"
  28. #include "libavcodec/packet.h"
  29. #include "thread_queue.h"
  30. enum {
  31. FINISHED_SEND = (1 << 0),
  32. FINISHED_RECV = (1 << 1),
  33. };
  34. struct ThreadQueue {
  35. int *finished;
  36. unsigned int nb_streams;
  37. enum ThreadQueueType type;
  38. AVContainerFifo *fifo;
  39. AVFifo *fifo_stream_index;
  40. pthread_mutex_t lock;
  41. pthread_cond_t cond;
  42. };
  43. void tq_free(ThreadQueue **ptq)
  44. {
  45. ThreadQueue *tq = *ptq;
  46. if (!tq)
  47. return;
  48. av_container_fifo_free(&tq->fifo);
  49. av_fifo_freep2(&tq->fifo_stream_index);
  50. av_freep(&tq->finished);
  51. pthread_cond_destroy(&tq->cond);
  52. pthread_mutex_destroy(&tq->lock);
  53. av_freep(ptq);
  54. }
  55. ThreadQueue *tq_alloc(unsigned int nb_streams, size_t queue_size,
  56. enum ThreadQueueType type)
  57. {
  58. ThreadQueue *tq;
  59. int ret;
  60. tq = av_mallocz(sizeof(*tq));
  61. if (!tq)
  62. return NULL;
  63. ret = pthread_cond_init(&tq->cond, NULL);
  64. if (ret) {
  65. av_freep(&tq);
  66. return NULL;
  67. }
  68. ret = pthread_mutex_init(&tq->lock, NULL);
  69. if (ret) {
  70. pthread_cond_destroy(&tq->cond);
  71. av_freep(&tq);
  72. return NULL;
  73. }
  74. tq->finished = av_calloc(nb_streams, sizeof(*tq->finished));
  75. if (!tq->finished)
  76. goto fail;
  77. tq->nb_streams = nb_streams;
  78. tq->type = type;
  79. tq->fifo = (type == THREAD_QUEUE_FRAMES) ?
  80. av_container_fifo_alloc_avframe(0) : av_container_fifo_alloc_avpacket(0);
  81. if (!tq->fifo)
  82. goto fail;
  83. tq->fifo_stream_index = av_fifo_alloc2(queue_size, sizeof(unsigned), 0);
  84. if (!tq->fifo_stream_index)
  85. goto fail;
  86. return tq;
  87. fail:
  88. tq_free(&tq);
  89. return NULL;
  90. }
  91. int tq_send(ThreadQueue *tq, unsigned int stream_idx, void *data)
  92. {
  93. int *finished;
  94. int ret;
  95. av_assert0(stream_idx < tq->nb_streams);
  96. finished = &tq->finished[stream_idx];
  97. pthread_mutex_lock(&tq->lock);
  98. if (*finished & FINISHED_SEND) {
  99. ret = AVERROR(EINVAL);
  100. goto finish;
  101. }
  102. while (!(*finished & FINISHED_RECV) && !av_fifo_can_write(tq->fifo_stream_index))
  103. pthread_cond_wait(&tq->cond, &tq->lock);
  104. if (*finished & FINISHED_RECV) {
  105. ret = AVERROR_EOF;
  106. *finished |= FINISHED_SEND;
  107. } else {
  108. ret = av_fifo_write(tq->fifo_stream_index, &stream_idx, 1);
  109. if (ret < 0)
  110. goto finish;
  111. ret = av_container_fifo_write(tq->fifo, data, 0);
  112. if (ret < 0)
  113. goto finish;
  114. pthread_cond_broadcast(&tq->cond);
  115. }
  116. finish:
  117. pthread_mutex_unlock(&tq->lock);
  118. return ret;
  119. }
  120. static int receive_locked(ThreadQueue *tq, int *stream_idx,
  121. void *data)
  122. {
  123. unsigned int nb_finished = 0;
  124. while (av_container_fifo_read(tq->fifo, data, 0) >= 0) {
  125. unsigned idx;
  126. int ret;
  127. ret = av_fifo_read(tq->fifo_stream_index, &idx, 1);
  128. av_assert0(ret >= 0);
  129. if (tq->finished[idx] & FINISHED_RECV) {
  130. (tq->type == THREAD_QUEUE_FRAMES) ?
  131. av_frame_unref(data) : av_packet_unref(data);
  132. continue;
  133. }
  134. *stream_idx = idx;
  135. return 0;
  136. }
  137. for (unsigned int i = 0; i < tq->nb_streams; i++) {
  138. if (!tq->finished[i])
  139. continue;
  140. /* return EOF to the consumer at most once for each stream */
  141. if (!(tq->finished[i] & FINISHED_RECV)) {
  142. tq->finished[i] |= FINISHED_RECV;
  143. *stream_idx = i;
  144. return AVERROR_EOF;
  145. }
  146. nb_finished++;
  147. }
  148. return nb_finished == tq->nb_streams ? AVERROR_EOF : AVERROR(EAGAIN);
  149. }
  150. int tq_receive(ThreadQueue *tq, int *stream_idx, void *data)
  151. {
  152. int ret;
  153. *stream_idx = -1;
  154. pthread_mutex_lock(&tq->lock);
  155. while (1) {
  156. size_t can_read = av_container_fifo_can_read(tq->fifo);
  157. ret = receive_locked(tq, stream_idx, data);
  158. // signal other threads if the fifo state changed
  159. if (can_read != av_container_fifo_can_read(tq->fifo))
  160. pthread_cond_broadcast(&tq->cond);
  161. if (ret == AVERROR(EAGAIN)) {
  162. pthread_cond_wait(&tq->cond, &tq->lock);
  163. continue;
  164. }
  165. break;
  166. }
  167. pthread_mutex_unlock(&tq->lock);
  168. return ret;
  169. }
  170. void tq_send_finish(ThreadQueue *tq, unsigned int stream_idx)
  171. {
  172. av_assert0(stream_idx < tq->nb_streams);
  173. pthread_mutex_lock(&tq->lock);
  174. /* mark the stream as send-finished;
  175. * next time the consumer thread tries to read this stream it will get
  176. * an EOF and recv-finished flag will be set */
  177. tq->finished[stream_idx] |= FINISHED_SEND;
  178. pthread_cond_broadcast(&tq->cond);
  179. pthread_mutex_unlock(&tq->lock);
  180. }
  181. void tq_receive_finish(ThreadQueue *tq, unsigned int stream_idx)
  182. {
  183. av_assert0(stream_idx < tq->nb_streams);
  184. pthread_mutex_lock(&tq->lock);
  185. /* mark the stream as recv-finished;
  186. * next time the producer thread tries to send for this stream, it will
  187. * get an EOF and send-finished flag will be set */
  188. tq->finished[stream_idx] |= FINISHED_RECV;
  189. pthread_cond_broadcast(&tq->cond);
  190. pthread_mutex_unlock(&tq->lock);
  191. }