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.

311 lines
9.1KB

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