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.

229 lines
6.7KB

  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. #include "libavutil/slicethread.h"
  34. typedef int (action_func)(AVCodecContext *c, void *arg);
  35. typedef int (action_func2)(AVCodecContext *c, void *arg, int jobnr, int threadnr);
  36. typedef struct SliceThreadContext {
  37. AVSliceThread *thread;
  38. action_func *func;
  39. action_func2 *func2;
  40. void *args;
  41. int *rets;
  42. int job_size;
  43. int *entries;
  44. int entries_count;
  45. int thread_count;
  46. pthread_cond_t *progress_cond;
  47. pthread_mutex_t *progress_mutex;
  48. } SliceThreadContext;
  49. static void worker_func(void *priv, int jobnr, int threadnr, int nb_jobs, int nb_threads)
  50. {
  51. AVCodecContext *avctx = priv;
  52. SliceThreadContext *c = avctx->internal->thread_ctx;
  53. int ret;
  54. ret = c->func ? c->func(avctx, (char *)c->args + c->job_size * jobnr)
  55. : c->func2(avctx, c->args, jobnr, threadnr);
  56. if (c->rets)
  57. c->rets[jobnr] = ret;
  58. }
  59. void ff_slice_thread_free(AVCodecContext *avctx)
  60. {
  61. SliceThreadContext *c = avctx->internal->thread_ctx;
  62. int i;
  63. avpriv_slicethread_free(&c->thread);
  64. for (i = 0; i < c->thread_count; i++) {
  65. pthread_mutex_destroy(&c->progress_mutex[i]);
  66. pthread_cond_destroy(&c->progress_cond[i]);
  67. }
  68. av_freep(&c->entries);
  69. av_freep(&c->progress_mutex);
  70. av_freep(&c->progress_cond);
  71. av_freep(&avctx->internal->thread_ctx);
  72. }
  73. static int thread_execute(AVCodecContext *avctx, action_func* func, void *arg, int *ret, int job_count, int job_size)
  74. {
  75. SliceThreadContext *c = avctx->internal->thread_ctx;
  76. if (!(avctx->active_thread_type&FF_THREAD_SLICE) || avctx->thread_count <= 1)
  77. return avcodec_default_execute(avctx, func, arg, ret, job_count, job_size);
  78. if (job_count <= 0)
  79. return 0;
  80. c->job_size = job_size;
  81. c->args = arg;
  82. c->func = func;
  83. c->rets = ret;
  84. avpriv_slicethread_execute(c->thread, job_count, 0);
  85. return 0;
  86. }
  87. static int thread_execute2(AVCodecContext *avctx, action_func2* func2, void *arg, int *ret, int job_count)
  88. {
  89. SliceThreadContext *c = avctx->internal->thread_ctx;
  90. c->func2 = func2;
  91. return thread_execute(avctx, NULL, arg, ret, job_count, 0);
  92. }
  93. int ff_slice_thread_init(AVCodecContext *avctx)
  94. {
  95. SliceThreadContext *c;
  96. int thread_count = avctx->thread_count;
  97. #if HAVE_W32THREADS
  98. w32thread_init();
  99. #endif
  100. // We cannot do this in the encoder init as the threads are created before
  101. if (av_codec_is_encoder(avctx->codec) &&
  102. avctx->codec_id == AV_CODEC_ID_MPEG1VIDEO &&
  103. avctx->height > 2800)
  104. thread_count = avctx->thread_count = 1;
  105. if (!thread_count) {
  106. int nb_cpus = av_cpu_count();
  107. if (avctx->height)
  108. nb_cpus = FFMIN(nb_cpus, (avctx->height+15)/16);
  109. // use number of cores + 1 as thread count if there is more than one
  110. if (nb_cpus > 1)
  111. thread_count = avctx->thread_count = FFMIN(nb_cpus + 1, MAX_AUTO_THREADS);
  112. else
  113. thread_count = avctx->thread_count = 1;
  114. }
  115. if (thread_count <= 1) {
  116. avctx->active_thread_type = 0;
  117. return 0;
  118. }
  119. avctx->internal->thread_ctx = c = av_mallocz(sizeof(*c));
  120. if (!c || (thread_count = avpriv_slicethread_create(&c->thread, avctx, worker_func, NULL, thread_count)) <= 1) {
  121. if (c)
  122. avpriv_slicethread_free(&c->thread);
  123. av_freep(&avctx->internal->thread_ctx);
  124. avctx->thread_count = 1;
  125. avctx->active_thread_type = 0;
  126. return 0;
  127. }
  128. avctx->thread_count = thread_count;
  129. avctx->execute = thread_execute;
  130. avctx->execute2 = thread_execute2;
  131. return 0;
  132. }
  133. void ff_thread_report_progress2(AVCodecContext *avctx, int field, int thread, int n)
  134. {
  135. SliceThreadContext *p = avctx->internal->thread_ctx;
  136. int *entries = p->entries;
  137. pthread_mutex_lock(&p->progress_mutex[thread]);
  138. entries[field] +=n;
  139. pthread_cond_signal(&p->progress_cond[thread]);
  140. pthread_mutex_unlock(&p->progress_mutex[thread]);
  141. }
  142. void ff_thread_await_progress2(AVCodecContext *avctx, int field, int thread, int shift)
  143. {
  144. SliceThreadContext *p = avctx->internal->thread_ctx;
  145. int *entries = p->entries;
  146. if (!entries || !field) return;
  147. thread = thread ? thread - 1 : p->thread_count - 1;
  148. pthread_mutex_lock(&p->progress_mutex[thread]);
  149. while ((entries[field - 1] - entries[field]) < shift){
  150. pthread_cond_wait(&p->progress_cond[thread], &p->progress_mutex[thread]);
  151. }
  152. pthread_mutex_unlock(&p->progress_mutex[thread]);
  153. }
  154. int ff_alloc_entries(AVCodecContext *avctx, int count)
  155. {
  156. int i;
  157. if (avctx->active_thread_type & FF_THREAD_SLICE) {
  158. SliceThreadContext *p = avctx->internal->thread_ctx;
  159. if (p->entries) {
  160. av_assert0(p->thread_count == avctx->thread_count);
  161. av_freep(&p->entries);
  162. }
  163. p->thread_count = avctx->thread_count;
  164. p->entries = av_mallocz_array(count, sizeof(int));
  165. if (!p->progress_mutex) {
  166. p->progress_mutex = av_malloc_array(p->thread_count, sizeof(pthread_mutex_t));
  167. p->progress_cond = av_malloc_array(p->thread_count, sizeof(pthread_cond_t));
  168. }
  169. if (!p->entries || !p->progress_mutex || !p->progress_cond) {
  170. av_freep(&p->entries);
  171. av_freep(&p->progress_mutex);
  172. av_freep(&p->progress_cond);
  173. return AVERROR(ENOMEM);
  174. }
  175. p->entries_count = count;
  176. for (i = 0; i < p->thread_count; i++) {
  177. pthread_mutex_init(&p->progress_mutex[i], NULL);
  178. pthread_cond_init(&p->progress_cond[i], NULL);
  179. }
  180. }
  181. return 0;
  182. }
  183. void ff_reset_entries(AVCodecContext *avctx)
  184. {
  185. SliceThreadContext *p = avctx->internal->thread_ctx;
  186. memset(p->entries, 0, p->entries_count * sizeof(int));
  187. }