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.

183 lines
5.2KB

  1. /*
  2. * Copyright (c) 2004 Roman Shaposhnik
  3. *
  4. * Many thanks to Steven M. Schultz for providing clever ideas and
  5. * to Michael Niedermayer <michaelni@gmx.at> for writing initial
  6. * implementation.
  7. *
  8. * This file is part of FFmpeg.
  9. *
  10. * FFmpeg is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU Lesser General Public
  12. * License as published by the Free Software Foundation; either
  13. * version 2.1 of the License, or (at your option) any later version.
  14. *
  15. * FFmpeg is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. * Lesser General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Lesser General Public
  21. * License along with FFmpeg; if not, write to the Free Software
  22. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  23. */
  24. #include <pthread.h>
  25. #include "avcodec.h"
  26. typedef int (action_func)(AVCodecContext *c, void *arg);
  27. typedef int (action_func2)(AVCodecContext *c, void *arg, int jobnr, int threadnr);
  28. typedef struct ThreadContext {
  29. pthread_t *workers;
  30. action_func *func;
  31. action_func2 *func2;
  32. void *args;
  33. int *rets;
  34. int rets_count;
  35. int job_count;
  36. int job_size;
  37. pthread_cond_t last_job_cond;
  38. pthread_cond_t current_job_cond;
  39. pthread_mutex_t current_job_lock;
  40. int current_job;
  41. int done;
  42. } ThreadContext;
  43. static void* attribute_align_arg worker(void *v)
  44. {
  45. AVCodecContext *avctx = v;
  46. ThreadContext *c = avctx->thread_opaque;
  47. int our_job = c->job_count;
  48. int thread_count = avctx->thread_count;
  49. int self_id;
  50. pthread_mutex_lock(&c->current_job_lock);
  51. self_id = c->current_job++;
  52. for (;;){
  53. while (our_job >= c->job_count) {
  54. if (c->current_job == thread_count + c->job_count)
  55. pthread_cond_signal(&c->last_job_cond);
  56. pthread_cond_wait(&c->current_job_cond, &c->current_job_lock);
  57. our_job = self_id;
  58. if (c->done) {
  59. pthread_mutex_unlock(&c->current_job_lock);
  60. return NULL;
  61. }
  62. }
  63. pthread_mutex_unlock(&c->current_job_lock);
  64. c->rets[our_job%c->rets_count] = c->func ? c->func(avctx, (char*)c->args + our_job*c->job_size):
  65. c->func2(avctx, c->args, our_job, self_id);
  66. pthread_mutex_lock(&c->current_job_lock);
  67. our_job = c->current_job++;
  68. }
  69. }
  70. static av_always_inline void avcodec_thread_park_workers(ThreadContext *c, int thread_count)
  71. {
  72. pthread_cond_wait(&c->last_job_cond, &c->current_job_lock);
  73. pthread_mutex_unlock(&c->current_job_lock);
  74. }
  75. void avcodec_thread_free(AVCodecContext *avctx)
  76. {
  77. ThreadContext *c = avctx->thread_opaque;
  78. int i;
  79. pthread_mutex_lock(&c->current_job_lock);
  80. c->done = 1;
  81. pthread_cond_broadcast(&c->current_job_cond);
  82. pthread_mutex_unlock(&c->current_job_lock);
  83. for (i=0; i<avctx->thread_count; i++)
  84. pthread_join(c->workers[i], NULL);
  85. pthread_mutex_destroy(&c->current_job_lock);
  86. pthread_cond_destroy(&c->current_job_cond);
  87. pthread_cond_destroy(&c->last_job_cond);
  88. av_free(c->workers);
  89. av_freep(&avctx->thread_opaque);
  90. }
  91. int avcodec_thread_execute(AVCodecContext *avctx, action_func* func, void *arg, int *ret, int job_count, int job_size)
  92. {
  93. ThreadContext *c= avctx->thread_opaque;
  94. int dummy_ret;
  95. if (job_count <= 0)
  96. return 0;
  97. pthread_mutex_lock(&c->current_job_lock);
  98. c->current_job = avctx->thread_count;
  99. c->job_count = job_count;
  100. c->job_size = job_size;
  101. c->args = arg;
  102. c->func = func;
  103. if (ret) {
  104. c->rets = ret;
  105. c->rets_count = job_count;
  106. } else {
  107. c->rets = &dummy_ret;
  108. c->rets_count = 1;
  109. }
  110. pthread_cond_broadcast(&c->current_job_cond);
  111. avcodec_thread_park_workers(c, avctx->thread_count);
  112. return 0;
  113. }
  114. int avcodec_thread_execute2(AVCodecContext *avctx, action_func2* func2, void *arg, int *ret, int job_count)
  115. {
  116. ThreadContext *c= avctx->thread_opaque;
  117. c->func2 = func2;
  118. return avcodec_thread_execute(avctx, NULL, arg, ret, job_count, 0);
  119. }
  120. int avcodec_thread_init(AVCodecContext *avctx, int thread_count)
  121. {
  122. int i;
  123. ThreadContext *c;
  124. c = av_mallocz(sizeof(ThreadContext));
  125. if (!c)
  126. return -1;
  127. c->workers = av_mallocz(sizeof(pthread_t)*thread_count);
  128. if (!c->workers) {
  129. av_free(c);
  130. return -1;
  131. }
  132. avctx->thread_opaque = c;
  133. avctx->thread_count = thread_count;
  134. c->current_job = 0;
  135. c->job_count = 0;
  136. c->job_size = 0;
  137. c->done = 0;
  138. pthread_cond_init(&c->current_job_cond, NULL);
  139. pthread_cond_init(&c->last_job_cond, NULL);
  140. pthread_mutex_init(&c->current_job_lock, NULL);
  141. pthread_mutex_lock(&c->current_job_lock);
  142. for (i=0; i<thread_count; i++) {
  143. if(pthread_create(&c->workers[i], NULL, worker, avctx)) {
  144. avctx->thread_count = i;
  145. pthread_mutex_unlock(&c->current_job_lock);
  146. avcodec_thread_free(avctx);
  147. return -1;
  148. }
  149. }
  150. avcodec_thread_park_workers(c, thread_count);
  151. avctx->execute = avcodec_thread_execute;
  152. avctx->execute2 = avcodec_thread_execute2;
  153. return 0;
  154. }