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.

231 lines
5.9KB

  1. /*
  2. *
  3. * This file is part of FFmpeg.
  4. *
  5. * FFmpeg is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2.1 of the License, or (at your option) any later version.
  9. *
  10. * FFmpeg is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with FFmpeg; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  18. */
  19. /**
  20. * @file
  21. * Libavfilter multithreading support
  22. */
  23. #include "config.h"
  24. #include "libavutil/common.h"
  25. #include "libavutil/cpu.h"
  26. #include "libavutil/mem.h"
  27. #include "libavutil/thread.h"
  28. #include "avfilter.h"
  29. #include "internal.h"
  30. #include "thread.h"
  31. typedef struct ThreadContext {
  32. AVFilterGraph *graph;
  33. int nb_threads;
  34. pthread_t *workers;
  35. avfilter_action_func *func;
  36. /* per-execute parameters */
  37. AVFilterContext *ctx;
  38. void *arg;
  39. int *rets;
  40. int nb_rets;
  41. int nb_jobs;
  42. pthread_cond_t last_job_cond;
  43. pthread_cond_t current_job_cond;
  44. pthread_mutex_t current_job_lock;
  45. int current_job;
  46. unsigned int current_execute;
  47. int done;
  48. } ThreadContext;
  49. static void* attribute_align_arg worker(void *v)
  50. {
  51. ThreadContext *c = v;
  52. int our_job = c->nb_jobs;
  53. int nb_threads = c->nb_threads;
  54. unsigned int last_execute = 0;
  55. int self_id;
  56. pthread_mutex_lock(&c->current_job_lock);
  57. self_id = c->current_job++;
  58. for (;;) {
  59. while (our_job >= c->nb_jobs) {
  60. if (c->current_job == nb_threads + c->nb_jobs)
  61. pthread_cond_signal(&c->last_job_cond);
  62. while (last_execute == c->current_execute && !c->done)
  63. pthread_cond_wait(&c->current_job_cond, &c->current_job_lock);
  64. last_execute = c->current_execute;
  65. our_job = self_id;
  66. if (c->done) {
  67. pthread_mutex_unlock(&c->current_job_lock);
  68. return NULL;
  69. }
  70. }
  71. pthread_mutex_unlock(&c->current_job_lock);
  72. c->rets[our_job % c->nb_rets] = c->func(c->ctx, c->arg, our_job, c->nb_jobs);
  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. int dummy_ret;
  102. if (nb_jobs <= 0)
  103. return 0;
  104. pthread_mutex_lock(&c->current_job_lock);
  105. c->current_job = c->nb_threads;
  106. c->nb_jobs = nb_jobs;
  107. c->ctx = ctx;
  108. c->arg = arg;
  109. c->func = func;
  110. if (ret) {
  111. c->rets = ret;
  112. c->nb_rets = nb_jobs;
  113. } else {
  114. c->rets = &dummy_ret;
  115. c->nb_rets = 1;
  116. }
  117. c->current_execute++;
  118. pthread_cond_broadcast(&c->current_job_cond);
  119. slice_thread_park_workers(c);
  120. return 0;
  121. }
  122. static int thread_init_internal(ThreadContext *c, int nb_threads)
  123. {
  124. int i, ret;
  125. if (!nb_threads) {
  126. int nb_cpus = av_cpu_count();
  127. // use number of cores + 1 as thread count if there is more than one
  128. if (nb_cpus > 1)
  129. nb_threads = nb_cpus + 1;
  130. else
  131. nb_threads = 1;
  132. }
  133. if (nb_threads <= 1)
  134. return 1;
  135. c->nb_threads = nb_threads;
  136. c->workers = av_mallocz_array(sizeof(*c->workers), nb_threads);
  137. if (!c->workers)
  138. return AVERROR(ENOMEM);
  139. c->current_job = 0;
  140. c->nb_jobs = 0;
  141. c->done = 0;
  142. pthread_cond_init(&c->current_job_cond, NULL);
  143. pthread_cond_init(&c->last_job_cond, NULL);
  144. pthread_mutex_init(&c->current_job_lock, NULL);
  145. pthread_mutex_lock(&c->current_job_lock);
  146. for (i = 0; i < nb_threads; i++) {
  147. ret = pthread_create(&c->workers[i], NULL, worker, c);
  148. if (ret) {
  149. pthread_mutex_unlock(&c->current_job_lock);
  150. c->nb_threads = i;
  151. slice_thread_uninit(c);
  152. return AVERROR(ret);
  153. }
  154. }
  155. slice_thread_park_workers(c);
  156. return c->nb_threads;
  157. }
  158. int ff_graph_thread_init(AVFilterGraph *graph)
  159. {
  160. int ret;
  161. #if HAVE_W32THREADS
  162. w32thread_init();
  163. #endif
  164. if (graph->nb_threads == 1) {
  165. graph->thread_type = 0;
  166. return 0;
  167. }
  168. graph->internal->thread = av_mallocz(sizeof(ThreadContext));
  169. if (!graph->internal->thread)
  170. return AVERROR(ENOMEM);
  171. ret = thread_init_internal(graph->internal->thread, graph->nb_threads);
  172. if (ret <= 1) {
  173. av_freep(&graph->internal->thread);
  174. graph->thread_type = 0;
  175. graph->nb_threads = 1;
  176. return (ret < 0) ? ret : 0;
  177. }
  178. graph->nb_threads = ret;
  179. graph->internal->thread_execute = thread_execute;
  180. return 0;
  181. }
  182. void ff_graph_thread_free(AVFilterGraph *graph)
  183. {
  184. if (graph->internal->thread)
  185. slice_thread_uninit(graph->internal->thread);
  186. av_freep(&graph->internal->thread);
  187. }