pthread_slice.c 7.4 KB

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