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.

307 lines
8.8KB

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