You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

225 lines
5.7KB

  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. /**
  19. * @file
  20. * Libavfilter multithreading support
  21. */
  22. #include "config.h"
  23. #include "libavutil/common.h"
  24. #include "libavutil/cpu.h"
  25. #include "libavutil/mem.h"
  26. #include "libavutil/thread.h"
  27. #include "avfilter.h"
  28. #include "internal.h"
  29. #include "thread.h"
  30. typedef struct ThreadContext {
  31. AVFilterGraph *graph;
  32. int nb_threads;
  33. pthread_t *workers;
  34. avfilter_action_func *func;
  35. /* per-execute parameters */
  36. AVFilterContext *ctx;
  37. void *arg;
  38. int *rets;
  39. int nb_jobs;
  40. pthread_cond_t last_job_cond;
  41. pthread_cond_t current_job_cond;
  42. pthread_mutex_t current_job_lock;
  43. int current_job;
  44. unsigned int current_execute;
  45. int done;
  46. } ThreadContext;
  47. static void* attribute_align_arg worker(void *v)
  48. {
  49. ThreadContext *c = v;
  50. int our_job = c->nb_jobs;
  51. int nb_threads = c->nb_threads;
  52. unsigned int last_execute = 0;
  53. int ret, self_id;
  54. pthread_mutex_lock(&c->current_job_lock);
  55. self_id = c->current_job++;
  56. for (;;) {
  57. while (our_job >= c->nb_jobs) {
  58. if (c->current_job == nb_threads + c->nb_jobs)
  59. pthread_cond_signal(&c->last_job_cond);
  60. while (last_execute == c->current_execute && !c->done)
  61. pthread_cond_wait(&c->current_job_cond, &c->current_job_lock);
  62. last_execute = c->current_execute;
  63. our_job = self_id;
  64. if (c->done) {
  65. pthread_mutex_unlock(&c->current_job_lock);
  66. return NULL;
  67. }
  68. }
  69. pthread_mutex_unlock(&c->current_job_lock);
  70. ret = c->func(c->ctx, c->arg, our_job, c->nb_jobs);
  71. if (c->rets)
  72. c->rets[our_job % c->nb_jobs] = ret;
  73. pthread_mutex_lock(&c->current_job_lock);
  74. our_job = c->current_job++;
  75. }
  76. }
  77. static void slice_thread_uninit(ThreadContext *c)
  78. {
  79. int i;
  80. pthread_mutex_lock(&c->current_job_lock);
  81. c->done = 1;
  82. pthread_cond_broadcast(&c->current_job_cond);
  83. pthread_mutex_unlock(&c->current_job_lock);
  84. for (i = 0; i < c->nb_threads; i++)
  85. pthread_join(c->workers[i], NULL);
  86. pthread_mutex_destroy(&c->current_job_lock);
  87. pthread_cond_destroy(&c->current_job_cond);
  88. pthread_cond_destroy(&c->last_job_cond);
  89. av_freep(&c->workers);
  90. }
  91. static void slice_thread_park_workers(ThreadContext *c)
  92. {
  93. while (c->current_job != c->nb_threads + c->nb_jobs)
  94. pthread_cond_wait(&c->last_job_cond, &c->current_job_lock);
  95. pthread_mutex_unlock(&c->current_job_lock);
  96. }
  97. static int thread_execute(AVFilterContext *ctx, avfilter_action_func *func,
  98. void *arg, int *ret, int nb_jobs)
  99. {
  100. ThreadContext *c = ctx->graph->internal->thread;
  101. if (nb_jobs <= 0)
  102. return 0;
  103. pthread_mutex_lock(&c->current_job_lock);
  104. c->current_job = c->nb_threads;
  105. c->nb_jobs = nb_jobs;
  106. c->ctx = ctx;
  107. c->arg = arg;
  108. c->func = func;
  109. c->rets = ret;
  110. c->current_execute++;
  111. pthread_cond_broadcast(&c->current_job_cond);
  112. slice_thread_park_workers(c);
  113. return 0;
  114. }
  115. static int thread_init_internal(ThreadContext *c, int nb_threads)
  116. {
  117. int i, ret;
  118. if (!nb_threads) {
  119. int nb_cpus = av_cpu_count();
  120. // use number of cores + 1 as thread count if there is more than one
  121. if (nb_cpus > 1)
  122. nb_threads = nb_cpus + 1;
  123. else
  124. nb_threads = 1;
  125. }
  126. if (nb_threads <= 1)
  127. return 1;
  128. c->nb_threads = nb_threads;
  129. c->workers = av_mallocz_array(sizeof(*c->workers), nb_threads);
  130. if (!c->workers)
  131. return AVERROR(ENOMEM);
  132. c->current_job = 0;
  133. c->nb_jobs = 0;
  134. c->done = 0;
  135. pthread_cond_init(&c->current_job_cond, NULL);
  136. pthread_cond_init(&c->last_job_cond, NULL);
  137. pthread_mutex_init(&c->current_job_lock, NULL);
  138. pthread_mutex_lock(&c->current_job_lock);
  139. for (i = 0; i < nb_threads; i++) {
  140. ret = pthread_create(&c->workers[i], NULL, worker, c);
  141. if (ret) {
  142. pthread_mutex_unlock(&c->current_job_lock);
  143. c->nb_threads = i;
  144. slice_thread_uninit(c);
  145. return AVERROR(ret);
  146. }
  147. }
  148. slice_thread_park_workers(c);
  149. return c->nb_threads;
  150. }
  151. int ff_graph_thread_init(AVFilterGraph *graph)
  152. {
  153. int ret;
  154. #if HAVE_W32THREADS
  155. w32thread_init();
  156. #endif
  157. if (graph->nb_threads == 1) {
  158. graph->thread_type = 0;
  159. return 0;
  160. }
  161. graph->internal->thread = av_mallocz(sizeof(ThreadContext));
  162. if (!graph->internal->thread)
  163. return AVERROR(ENOMEM);
  164. ret = thread_init_internal(graph->internal->thread, graph->nb_threads);
  165. if (ret <= 1) {
  166. av_freep(&graph->internal->thread);
  167. graph->thread_type = 0;
  168. graph->nb_threads = 1;
  169. return (ret < 0) ? ret : 0;
  170. }
  171. graph->nb_threads = ret;
  172. graph->internal->thread_execute = thread_execute;
  173. return 0;
  174. }
  175. void ff_graph_thread_free(AVFilterGraph *graph)
  176. {
  177. if (graph->internal->thread)
  178. slice_thread_uninit(graph->internal->thread);
  179. av_freep(&graph->internal->thread);
  180. }