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.

315 lines
9.2KB

  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. if (ret) {
  133. c->rets = ret;
  134. } else {
  135. c->rets = NULL;
  136. }
  137. c->current_execute++;
  138. pthread_cond_broadcast(&c->current_job_cond);
  139. thread_park_workers(c, avctx->thread_count);
  140. return 0;
  141. }
  142. static int thread_execute2(AVCodecContext *avctx, action_func2* func2, void *arg, int *ret, int job_count)
  143. {
  144. SliceThreadContext *c = avctx->internal->thread_ctx;
  145. c->func2 = func2;
  146. return thread_execute(avctx, NULL, arg, ret, job_count, 0);
  147. }
  148. int ff_slice_thread_init(AVCodecContext *avctx)
  149. {
  150. int i;
  151. SliceThreadContext *c;
  152. int thread_count = avctx->thread_count;
  153. #if HAVE_W32THREADS
  154. w32thread_init();
  155. #endif
  156. // We cannot do this in the encoder init as the threads are created before
  157. if (av_codec_is_encoder(avctx->codec) &&
  158. avctx->codec_id == AV_CODEC_ID_MPEG1VIDEO &&
  159. avctx->height > 2800)
  160. thread_count = avctx->thread_count = 1;
  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. if (p->entries) {
  232. av_assert0(p->thread_count == avctx->thread_count);
  233. av_freep(&p->entries);
  234. }
  235. p->thread_count = avctx->thread_count;
  236. p->entries = av_mallocz_array(count, sizeof(int));
  237. if (!p->progress_mutex) {
  238. p->progress_mutex = av_malloc_array(p->thread_count, sizeof(pthread_mutex_t));
  239. p->progress_cond = av_malloc_array(p->thread_count, sizeof(pthread_cond_t));
  240. }
  241. if (!p->entries || !p->progress_mutex || !p->progress_cond) {
  242. av_freep(&p->entries);
  243. av_freep(&p->progress_mutex);
  244. av_freep(&p->progress_cond);
  245. return AVERROR(ENOMEM);
  246. }
  247. p->entries_count = count;
  248. for (i = 0; i < p->thread_count; i++) {
  249. pthread_mutex_init(&p->progress_mutex[i], NULL);
  250. pthread_cond_init(&p->progress_cond[i], NULL);
  251. }
  252. }
  253. return 0;
  254. }
  255. void ff_reset_entries(AVCodecContext *avctx)
  256. {
  257. SliceThreadContext *p = avctx->internal->thread_ctx;
  258. memset(p->entries, 0, p->entries_count * sizeof(int));
  259. }