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.

293 lines
8.4KB

  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. * Slice multithreading support functions
  21. * @see doc/multithreading.txt
  22. */
  23. #include "config.h"
  24. #if HAVE_PTHREADS
  25. #include <pthread.h>
  26. #elif HAVE_W32THREADS
  27. #include "compat/w32pthreads.h"
  28. #elif HAVE_OS2THREADS
  29. #include "compat/os2threads.h"
  30. #endif
  31. #include "avcodec.h"
  32. #include "internal.h"
  33. #include "pthread_internal.h"
  34. #include "thread.h"
  35. #include "libavutil/common.h"
  36. #include "libavutil/cpu.h"
  37. #include "libavutil/mem.h"
  38. typedef int (action_func)(AVCodecContext *c, void *arg);
  39. typedef int (action_func2)(AVCodecContext *c, void *arg, int jobnr, int threadnr);
  40. typedef struct SliceThreadContext {
  41. pthread_t *workers;
  42. action_func *func;
  43. action_func2 *func2;
  44. void *args;
  45. int *rets;
  46. int rets_count;
  47. int job_count;
  48. int job_size;
  49. pthread_cond_t last_job_cond;
  50. pthread_cond_t current_job_cond;
  51. pthread_mutex_t current_job_lock;
  52. unsigned current_execute;
  53. int current_job;
  54. int done;
  55. int *entries;
  56. int entries_count;
  57. int thread_count;
  58. pthread_cond_t *progress_cond;
  59. pthread_mutex_t *progress_mutex;
  60. } SliceThreadContext;
  61. static void* attribute_align_arg worker(void *v)
  62. {
  63. AVCodecContext *avctx = v;
  64. SliceThreadContext *c = avctx->internal->thread_ctx;
  65. unsigned last_execute = 0;
  66. int our_job = c->job_count;
  67. int thread_count = avctx->thread_count;
  68. int self_id;
  69. pthread_mutex_lock(&c->current_job_lock);
  70. self_id = c->current_job++;
  71. for (;;){
  72. while (our_job >= c->job_count) {
  73. if (c->current_job == thread_count + c->job_count)
  74. pthread_cond_signal(&c->last_job_cond);
  75. while (last_execute == c->current_execute && !c->done)
  76. pthread_cond_wait(&c->current_job_cond, &c->current_job_lock);
  77. last_execute = c->current_execute;
  78. our_job = self_id;
  79. if (c->done) {
  80. pthread_mutex_unlock(&c->current_job_lock);
  81. return NULL;
  82. }
  83. }
  84. pthread_mutex_unlock(&c->current_job_lock);
  85. c->rets[our_job%c->rets_count] = c->func ? c->func(avctx, (char*)c->args + our_job*c->job_size):
  86. c->func2(avctx, c->args, our_job, self_id);
  87. pthread_mutex_lock(&c->current_job_lock);
  88. our_job = c->current_job++;
  89. }
  90. }
  91. void ff_slice_thread_free(AVCodecContext *avctx)
  92. {
  93. SliceThreadContext *c = avctx->internal->thread_ctx;
  94. int i;
  95. pthread_mutex_lock(&c->current_job_lock);
  96. c->done = 1;
  97. pthread_cond_broadcast(&c->current_job_cond);
  98. pthread_mutex_unlock(&c->current_job_lock);
  99. for (i=0; i<avctx->thread_count; i++)
  100. pthread_join(c->workers[i], NULL);
  101. pthread_mutex_destroy(&c->current_job_lock);
  102. pthread_cond_destroy(&c->current_job_cond);
  103. pthread_cond_destroy(&c->last_job_cond);
  104. av_free(c->workers);
  105. av_freep(&avctx->internal->thread_ctx);
  106. }
  107. static av_always_inline void thread_park_workers(SliceThreadContext *c, int thread_count)
  108. {
  109. while (c->current_job != thread_count + c->job_count)
  110. pthread_cond_wait(&c->last_job_cond, &c->current_job_lock);
  111. pthread_mutex_unlock(&c->current_job_lock);
  112. }
  113. static int thread_execute(AVCodecContext *avctx, action_func* func, void *arg, int *ret, int job_count, int job_size)
  114. {
  115. SliceThreadContext *c = avctx->internal->thread_ctx;
  116. int dummy_ret;
  117. if (!(avctx->active_thread_type&FF_THREAD_SLICE) || avctx->thread_count <= 1)
  118. return avcodec_default_execute(avctx, func, arg, ret, job_count, job_size);
  119. if (job_count <= 0)
  120. return 0;
  121. pthread_mutex_lock(&c->current_job_lock);
  122. c->current_job = avctx->thread_count;
  123. c->job_count = job_count;
  124. c->job_size = job_size;
  125. c->args = arg;
  126. c->func = func;
  127. if (ret) {
  128. c->rets = ret;
  129. c->rets_count = job_count;
  130. } else {
  131. c->rets = &dummy_ret;
  132. c->rets_count = 1;
  133. }
  134. c->current_execute++;
  135. pthread_cond_broadcast(&c->current_job_cond);
  136. thread_park_workers(c, avctx->thread_count);
  137. return 0;
  138. }
  139. static int thread_execute2(AVCodecContext *avctx, action_func2* func2, void *arg, int *ret, int job_count)
  140. {
  141. SliceThreadContext *c = avctx->internal->thread_ctx;
  142. c->func2 = func2;
  143. return thread_execute(avctx, NULL, arg, ret, job_count, 0);
  144. }
  145. int ff_slice_thread_init(AVCodecContext *avctx)
  146. {
  147. int i;
  148. SliceThreadContext *c;
  149. int thread_count = avctx->thread_count;
  150. #if HAVE_W32THREADS
  151. w32thread_init();
  152. #endif
  153. if (!thread_count) {
  154. int nb_cpus = av_cpu_count();
  155. if (avctx->height)
  156. nb_cpus = FFMIN(nb_cpus, (avctx->height+15)/16);
  157. // use number of cores + 1 as thread count if there is more than one
  158. if (nb_cpus > 1)
  159. thread_count = avctx->thread_count = FFMIN(nb_cpus + 1, MAX_AUTO_THREADS);
  160. else
  161. thread_count = avctx->thread_count = 1;
  162. }
  163. if (thread_count <= 1) {
  164. avctx->active_thread_type = 0;
  165. return 0;
  166. }
  167. c = av_mallocz(sizeof(SliceThreadContext));
  168. if (!c)
  169. return -1;
  170. c->workers = av_mallocz_array(thread_count, sizeof(pthread_t));
  171. if (!c->workers) {
  172. av_free(c);
  173. return -1;
  174. }
  175. avctx->internal->thread_ctx = c;
  176. c->current_job = 0;
  177. c->job_count = 0;
  178. c->job_size = 0;
  179. c->done = 0;
  180. pthread_cond_init(&c->current_job_cond, NULL);
  181. pthread_cond_init(&c->last_job_cond, NULL);
  182. pthread_mutex_init(&c->current_job_lock, NULL);
  183. pthread_mutex_lock(&c->current_job_lock);
  184. for (i=0; i<thread_count; i++) {
  185. if(pthread_create(&c->workers[i], NULL, worker, avctx)) {
  186. avctx->thread_count = i;
  187. pthread_mutex_unlock(&c->current_job_lock);
  188. ff_thread_free(avctx);
  189. return -1;
  190. }
  191. }
  192. thread_park_workers(c, thread_count);
  193. avctx->execute = thread_execute;
  194. avctx->execute2 = thread_execute2;
  195. return 0;
  196. }
  197. void ff_thread_report_progress2(AVCodecContext *avctx, int field, int thread, int n)
  198. {
  199. SliceThreadContext *p = avctx->internal->thread_ctx;
  200. int *entries = p->entries;
  201. pthread_mutex_lock(&p->progress_mutex[thread]);
  202. entries[field] +=n;
  203. pthread_cond_signal(&p->progress_cond[thread]);
  204. pthread_mutex_unlock(&p->progress_mutex[thread]);
  205. }
  206. void ff_thread_await_progress2(AVCodecContext *avctx, int field, int thread, int shift)
  207. {
  208. SliceThreadContext *p = avctx->internal->thread_ctx;
  209. int *entries = p->entries;
  210. if (!entries || !field) return;
  211. thread = thread ? thread - 1 : p->thread_count - 1;
  212. pthread_mutex_lock(&p->progress_mutex[thread]);
  213. while ((entries[field - 1] - entries[field]) < shift){
  214. pthread_cond_wait(&p->progress_cond[thread], &p->progress_mutex[thread]);
  215. }
  216. pthread_mutex_unlock(&p->progress_mutex[thread]);
  217. }
  218. int ff_alloc_entries(AVCodecContext *avctx, int count)
  219. {
  220. int i;
  221. if (avctx->active_thread_type & FF_THREAD_SLICE) {
  222. SliceThreadContext *p = avctx->internal->thread_ctx;
  223. p->thread_count = avctx->thread_count;
  224. p->entries = av_mallocz_array(count, sizeof(int));
  225. if (!p->entries) {
  226. return AVERROR(ENOMEM);
  227. }
  228. p->entries_count = count;
  229. p->progress_mutex = av_malloc_array(p->thread_count, sizeof(pthread_mutex_t));
  230. p->progress_cond = av_malloc_array(p->thread_count, sizeof(pthread_cond_t));
  231. for (i = 0; i < p->thread_count; i++) {
  232. pthread_mutex_init(&p->progress_mutex[i], NULL);
  233. pthread_cond_init(&p->progress_cond[i], NULL);
  234. }
  235. }
  236. return 0;
  237. }
  238. void ff_reset_entries(AVCodecContext *avctx)
  239. {
  240. SliceThreadContext *p = avctx->internal->thread_ctx;
  241. memset(p->entries, 0, p->entries_count * sizeof(int));
  242. }