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.

308 lines
8.9KB

  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. for (i = 0; i < c->thread_count; i++)
  99. pthread_cond_broadcast(&c->progress_cond[i]);
  100. pthread_mutex_unlock(&c->current_job_lock);
  101. for (i=0; i<avctx->thread_count; i++)
  102. pthread_join(c->workers[i], NULL);
  103. for (i = 0; i < c->thread_count; i++) {
  104. pthread_mutex_destroy(&c->progress_mutex[i]);
  105. pthread_cond_destroy(&c->progress_cond[i]);
  106. }
  107. pthread_mutex_destroy(&c->current_job_lock);
  108. pthread_cond_destroy(&c->current_job_cond);
  109. pthread_cond_destroy(&c->last_job_cond);
  110. av_freep(&c->entries);
  111. av_freep(&c->progress_mutex);
  112. av_freep(&c->progress_cond);
  113. av_freep(&c->workers);
  114. av_freep(&avctx->internal->thread_ctx);
  115. }
  116. static av_always_inline void thread_park_workers(SliceThreadContext *c, int thread_count)
  117. {
  118. while (c->current_job != thread_count + c->job_count)
  119. pthread_cond_wait(&c->last_job_cond, &c->current_job_lock);
  120. pthread_mutex_unlock(&c->current_job_lock);
  121. }
  122. static int thread_execute(AVCodecContext *avctx, action_func* func, void *arg, int *ret, int job_count, int job_size)
  123. {
  124. SliceThreadContext *c = avctx->internal->thread_ctx;
  125. int dummy_ret;
  126. if (!(avctx->active_thread_type&FF_THREAD_SLICE) || avctx->thread_count <= 1)
  127. return avcodec_default_execute(avctx, func, arg, ret, job_count, job_size);
  128. if (job_count <= 0)
  129. return 0;
  130. pthread_mutex_lock(&c->current_job_lock);
  131. c->current_job = avctx->thread_count;
  132. c->job_count = job_count;
  133. c->job_size = job_size;
  134. c->args = arg;
  135. c->func = func;
  136. if (ret) {
  137. c->rets = ret;
  138. c->rets_count = job_count;
  139. } else {
  140. c->rets = &dummy_ret;
  141. c->rets_count = 1;
  142. }
  143. c->current_execute++;
  144. pthread_cond_broadcast(&c->current_job_cond);
  145. thread_park_workers(c, avctx->thread_count);
  146. return 0;
  147. }
  148. static int thread_execute2(AVCodecContext *avctx, action_func2* func2, void *arg, int *ret, int job_count)
  149. {
  150. SliceThreadContext *c = avctx->internal->thread_ctx;
  151. c->func2 = func2;
  152. return thread_execute(avctx, NULL, arg, ret, job_count, 0);
  153. }
  154. int ff_slice_thread_init(AVCodecContext *avctx)
  155. {
  156. int i;
  157. SliceThreadContext *c;
  158. int thread_count = avctx->thread_count;
  159. #if HAVE_W32THREADS
  160. w32thread_init();
  161. #endif
  162. if (!thread_count) {
  163. int nb_cpus = av_cpu_count();
  164. if (avctx->height)
  165. nb_cpus = FFMIN(nb_cpus, (avctx->height+15)/16);
  166. // use number of cores + 1 as thread count if there is more than one
  167. if (nb_cpus > 1)
  168. thread_count = avctx->thread_count = FFMIN(nb_cpus + 1, MAX_AUTO_THREADS);
  169. else
  170. thread_count = avctx->thread_count = 1;
  171. }
  172. if (thread_count <= 1) {
  173. avctx->active_thread_type = 0;
  174. return 0;
  175. }
  176. c = av_mallocz(sizeof(SliceThreadContext));
  177. if (!c)
  178. return -1;
  179. c->workers = av_mallocz_array(thread_count, sizeof(pthread_t));
  180. if (!c->workers) {
  181. av_free(c);
  182. return -1;
  183. }
  184. avctx->internal->thread_ctx = c;
  185. c->current_job = 0;
  186. c->job_count = 0;
  187. c->job_size = 0;
  188. c->done = 0;
  189. pthread_cond_init(&c->current_job_cond, NULL);
  190. pthread_cond_init(&c->last_job_cond, NULL);
  191. pthread_mutex_init(&c->current_job_lock, NULL);
  192. pthread_mutex_lock(&c->current_job_lock);
  193. for (i=0; i<thread_count; i++) {
  194. if(pthread_create(&c->workers[i], NULL, worker, avctx)) {
  195. avctx->thread_count = i;
  196. pthread_mutex_unlock(&c->current_job_lock);
  197. ff_thread_free(avctx);
  198. return -1;
  199. }
  200. }
  201. thread_park_workers(c, thread_count);
  202. avctx->execute = thread_execute;
  203. avctx->execute2 = thread_execute2;
  204. return 0;
  205. }
  206. void ff_thread_report_progress2(AVCodecContext *avctx, int field, int thread, int n)
  207. {
  208. SliceThreadContext *p = avctx->internal->thread_ctx;
  209. int *entries = p->entries;
  210. pthread_mutex_lock(&p->progress_mutex[thread]);
  211. entries[field] +=n;
  212. pthread_cond_signal(&p->progress_cond[thread]);
  213. pthread_mutex_unlock(&p->progress_mutex[thread]);
  214. }
  215. void ff_thread_await_progress2(AVCodecContext *avctx, int field, int thread, int shift)
  216. {
  217. SliceThreadContext *p = avctx->internal->thread_ctx;
  218. int *entries = p->entries;
  219. if (!entries || !field) return;
  220. thread = thread ? thread - 1 : p->thread_count - 1;
  221. pthread_mutex_lock(&p->progress_mutex[thread]);
  222. while ((entries[field - 1] - entries[field]) < shift){
  223. pthread_cond_wait(&p->progress_cond[thread], &p->progress_mutex[thread]);
  224. }
  225. pthread_mutex_unlock(&p->progress_mutex[thread]);
  226. }
  227. int ff_alloc_entries(AVCodecContext *avctx, int count)
  228. {
  229. int i;
  230. if (avctx->active_thread_type & FF_THREAD_SLICE) {
  231. SliceThreadContext *p = avctx->internal->thread_ctx;
  232. p->thread_count = avctx->thread_count;
  233. p->entries = av_mallocz_array(count, sizeof(int));
  234. p->progress_mutex = av_malloc_array(p->thread_count, sizeof(pthread_mutex_t));
  235. p->progress_cond = av_malloc_array(p->thread_count, sizeof(pthread_cond_t));
  236. if (!p->entries || !p->progress_mutex || !p->progress_cond) {
  237. av_freep(&p->entries);
  238. av_freep(&p->progress_mutex);
  239. av_freep(&p->progress_cond);
  240. return AVERROR(ENOMEM);
  241. }
  242. p->entries_count = count;
  243. for (i = 0; i < p->thread_count; i++) {
  244. pthread_mutex_init(&p->progress_mutex[i], NULL);
  245. pthread_cond_init(&p->progress_cond[i], NULL);
  246. }
  247. }
  248. return 0;
  249. }
  250. void ff_reset_entries(AVCodecContext *avctx)
  251. {
  252. SliceThreadContext *p = avctx->internal->thread_ctx;
  253. memset(p->entries, 0, p->entries_count * sizeof(int));
  254. }