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.

171 lines
4.6KB

  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. */
  25. #include <pthread.h>
  26. #include "avcodec.h"
  27. #include "common.h"
  28. typedef int (action_t)(AVCodecContext *c, void *arg);
  29. typedef struct ThreadContext {
  30. pthread_t *workers;
  31. action_t *func;
  32. void **args;
  33. int *rets;
  34. int rets_count;
  35. int job_count;
  36. pthread_cond_t last_job_cond;
  37. pthread_cond_t current_job_cond;
  38. pthread_mutex_t current_job_lock;
  39. int current_job;
  40. int done;
  41. } ThreadContext;
  42. static void* worker(void *v)
  43. {
  44. AVCodecContext *avctx = v;
  45. ThreadContext *c = avctx->thread_opaque;
  46. int our_job = c->job_count;
  47. int thread_count = avctx->thread_count;
  48. int self_id;
  49. pthread_mutex_lock(&c->current_job_lock);
  50. self_id = c->current_job++;
  51. for (;;){
  52. while (our_job >= c->job_count) {
  53. if (c->current_job == thread_count + c->job_count)
  54. pthread_cond_signal(&c->last_job_cond);
  55. pthread_cond_wait(&c->current_job_cond, &c->current_job_lock);
  56. our_job = self_id;
  57. if (c->done) {
  58. pthread_mutex_unlock(&c->current_job_lock);
  59. return NULL;
  60. }
  61. }
  62. pthread_mutex_unlock(&c->current_job_lock);
  63. c->rets[our_job%c->rets_count] = c->func(avctx, c->args[our_job]);
  64. pthread_mutex_lock(&c->current_job_lock);
  65. our_job = c->current_job++;
  66. }
  67. }
  68. static av_always_inline void avcodec_thread_park_workers(ThreadContext *c, int thread_count)
  69. {
  70. pthread_cond_wait(&c->last_job_cond, &c->current_job_lock);
  71. pthread_mutex_unlock(&c->current_job_lock);
  72. }
  73. void avcodec_thread_free(AVCodecContext *avctx)
  74. {
  75. ThreadContext *c = avctx->thread_opaque;
  76. int i;
  77. pthread_mutex_lock(&c->current_job_lock);
  78. c->done = 1;
  79. pthread_cond_broadcast(&c->current_job_cond);
  80. pthread_mutex_unlock(&c->current_job_lock);
  81. for (i=0; i<avctx->thread_count; i++)
  82. pthread_join(c->workers[i], NULL);
  83. pthread_mutex_destroy(&c->current_job_lock);
  84. pthread_cond_destroy(&c->current_job_cond);
  85. pthread_cond_destroy(&c->last_job_cond);
  86. av_free(c->workers);
  87. av_free(c);
  88. }
  89. int avcodec_thread_execute(AVCodecContext *avctx, action_t* func, void **arg, int *ret, int job_count)
  90. {
  91. ThreadContext *c= avctx->thread_opaque;
  92. int dummy_ret;
  93. if (job_count <= 0)
  94. return 0;
  95. pthread_mutex_lock(&c->current_job_lock);
  96. c->current_job = avctx->thread_count;
  97. c->job_count = job_count;
  98. c->args = arg;
  99. c->func = func;
  100. if (ret) {
  101. c->rets = ret;
  102. c->rets_count = job_count;
  103. } else {
  104. c->rets = &dummy_ret;
  105. c->rets_count = 1;
  106. }
  107. pthread_cond_broadcast(&c->current_job_cond);
  108. avcodec_thread_park_workers(c, avctx->thread_count);
  109. return 0;
  110. }
  111. int avcodec_thread_init(AVCodecContext *avctx, int thread_count)
  112. {
  113. int i;
  114. ThreadContext *c;
  115. c = av_mallocz(sizeof(ThreadContext));
  116. if (!c)
  117. return -1;
  118. c->workers = av_mallocz(sizeof(pthread_t)*thread_count);
  119. if (!c->workers) {
  120. av_free(c);
  121. return -1;
  122. }
  123. avctx->thread_opaque = c;
  124. avctx->thread_count = thread_count;
  125. c->current_job = 0;
  126. c->job_count = 0;
  127. c->done = 0;
  128. pthread_cond_init(&c->current_job_cond, NULL);
  129. pthread_cond_init(&c->last_job_cond, NULL);
  130. pthread_mutex_init(&c->current_job_lock, NULL);
  131. pthread_mutex_lock(&c->current_job_lock);
  132. for (i=0; i<thread_count; i++) {
  133. if(pthread_create(&c->workers[i], NULL, worker, avctx)) {
  134. avctx->thread_count = i;
  135. pthread_mutex_unlock(&c->current_job_lock);
  136. avcodec_thread_free(avctx);
  137. return -1;
  138. }
  139. }
  140. avcodec_thread_park_workers(c, thread_count);
  141. avctx->execute = avcodec_thread_execute;
  142. return 0;
  143. }