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.

202 lines
6.2KB

  1. /*
  2. * Copyright (c) 2004 Michael Niedermayer <michaelni@gmx.at>
  3. *
  4. * This library 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 of the License, or (at your option) any later version.
  8. *
  9. * This library 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 this library; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  17. *
  18. */
  19. #include <semaphore.h>
  20. #include <pthread.h>
  21. //#define DEBUG
  22. #include "avcodec.h"
  23. #include "common.h"
  24. typedef struct JobContext{
  25. sem_t available_sem;
  26. int assigned;
  27. int (*func)(AVCodecContext *c, void *arg);
  28. void *arg;
  29. int ret;
  30. }JobContext;
  31. typedef struct WorkerContext{
  32. AVCodecContext *avctx;
  33. pthread_t thread;
  34. int start_index;
  35. sem_t work_sem;
  36. sem_t done_sem;
  37. }WorkerContext;
  38. typedef struct ThreadContext{
  39. WorkerContext *worker;
  40. JobContext *job;
  41. int job_count;
  42. int allocated_job_count;
  43. }ThreadContext;
  44. static void * thread_func(void *v){
  45. WorkerContext *w= v;
  46. ThreadContext *c= w->avctx->thread_opaque;
  47. int i;
  48. for(;;){
  49. //av_log(w->avctx, AV_LOG_DEBUG, "thread_func %X enter wait\n", (int)v);
  50. sem_wait(&w->work_sem);
  51. //av_log(w->avctx, AV_LOG_DEBUG, "thread_func %X after wait\n", (int)v);
  52. if(c->job_count == 0)
  53. break;
  54. for(i=0; i<c->job_count; i++){
  55. int index= (i + w->start_index) % c->job_count;
  56. JobContext *j= &c->job[index];
  57. //av_log(w->avctx, AV_LOG_DEBUG, "thread_func %X first check of %d\n", (int)v, index);
  58. if(j->assigned) continue; //unsynced check, if != 0 it is already given to another worker, it never becomes available before the next execute() call so this should be safe
  59. //av_log(w->avctx, AV_LOG_DEBUG, "thread_func %X second check of %d\n", (int)v, index);
  60. if(sem_trywait(&j->available_sem) == 0){
  61. j->assigned=1;
  62. j->ret= j->func(w->avctx, j->arg);
  63. //av_log(w->avctx, AV_LOG_DEBUG, "thread_func %X done %d\n", (int)v, index);
  64. }
  65. }
  66. //av_log(w->avctx, AV_LOG_DEBUG, "thread_func %X complete\n", (int)v);
  67. sem_post(&w->done_sem);
  68. }
  69. return NULL;
  70. }
  71. /**
  72. * free what has been allocated by avcodec_thread_init().
  73. * must be called after decoding has finished, especially dont call while avcodec_thread_execute() is running
  74. */
  75. void avcodec_thread_free(AVCodecContext *s){
  76. ThreadContext *c= s->thread_opaque;
  77. int i, val;
  78. for(i=0; i<c->allocated_job_count; i++){
  79. sem_getvalue(&c->job[i].available_sem, &val); assert(val == 0);
  80. sem_destroy(&c->job[i].available_sem);
  81. }
  82. c->job_count= 0;
  83. for(i=0; i<s->thread_count; i++){
  84. sem_getvalue(&c->worker[i].work_sem, &val); assert(val == 0);
  85. sem_getvalue(&c->worker[i].done_sem, &val); assert(val == 0);
  86. sem_post(&c->worker[i].work_sem);
  87. pthread_join(c->worker[i].thread, NULL);
  88. sem_destroy(&c->worker[i].work_sem);
  89. sem_destroy(&c->worker[i].done_sem);
  90. }
  91. av_freep(&c->job);
  92. av_freep(&c->worker);
  93. av_freep(&s->thread_opaque);
  94. }
  95. int avcodec_thread_execute(AVCodecContext *s, int (*func)(AVCodecContext *c2, void *arg2),void **arg, int *ret, int job_count){
  96. ThreadContext *c= s->thread_opaque;
  97. int i, val;
  98. assert(s == c->avctx);
  99. if(job_count > c->allocated_job_count){
  100. c->job= av_realloc(c->job, job_count*sizeof(JobContext));
  101. for(i=c->allocated_job_count; i<job_count; i++){
  102. memset(&c->job[i], 0, sizeof(JobContext));
  103. c->allocated_job_count++;
  104. if(sem_init(&c->job[i].available_sem, 0, 0))
  105. return -1;
  106. }
  107. }
  108. c->job_count= job_count;
  109. /* note, we can be certain that this is not called with the same AVCodecContext by different threads at the same time */
  110. for(i=0; i<job_count; i++){
  111. sem_getvalue(&c->job[i].available_sem, &val); assert(val == 0);
  112. c->job[i].arg= arg[i];
  113. c->job[i].func= func;
  114. c->job[i].ret= 12345;
  115. c->job[i].assigned= 0;
  116. sem_post(&c->job[i].available_sem);
  117. }
  118. for(i=0; i<s->thread_count && i<job_count; i++){
  119. sem_getvalue(&c->worker[i].work_sem, &val); assert(val == 0);
  120. sem_getvalue(&c->worker[i].done_sem, &val); assert(val == 0);
  121. c->worker[i].start_index= (i + job_count/2)/job_count;
  122. //av_log(s, AV_LOG_DEBUG, "start worker %d\n", i);
  123. sem_post(&c->worker[i].work_sem);
  124. }
  125. for(i=0; i<s->thread_count && i<job_count; i++){
  126. //av_log(s, AV_LOG_DEBUG, "wait for worker %d\n", i);
  127. sem_wait(&c->worker[i].done_sem);
  128. sem_getvalue(&c->worker[i].work_sem, &val); assert(val == 0);
  129. sem_getvalue(&c->worker[i].done_sem, &val); assert(val == 0);
  130. }
  131. for(i=0; i<job_count; i++){
  132. sem_getvalue(&c->job[i].available_sem, &val); assert(val == 0);
  133. c->job[i].func= NULL;
  134. if(ret) ret[i]= c->job[i].ret;
  135. }
  136. return 0;
  137. }
  138. int avcodec_thread_init(AVCodecContext *s, int thread_count){
  139. int i;
  140. ThreadContext *c;
  141. WorkerContext *worker;
  142. s->thread_count= thread_count;
  143. assert(!s->thread_opaque);
  144. c= av_mallocz(sizeof(ThreadContext));
  145. worker= av_mallocz(sizeof(WorkerContext)*thread_count);
  146. s->thread_opaque= c;
  147. c->worker= worker;
  148. for(i=0; i<thread_count; i++){
  149. //printf("init semaphors %d\n", i); fflush(stdout);
  150. worker[i].avctx= s;
  151. if(sem_init(&worker[i].work_sem, 0, 0))
  152. goto fail;
  153. if(sem_init(&worker[i].done_sem, 0, 0))
  154. goto fail;
  155. //printf("create thread %d\n", i); fflush(stdout);
  156. if(pthread_create(&worker[i].thread, NULL, thread_func, &worker[i]))
  157. goto fail;
  158. }
  159. //printf("init done\n"); fflush(stdout);
  160. s->execute= avcodec_thread_execute;
  161. return 0;
  162. fail:
  163. avcodec_thread_free(s);
  164. return -1;
  165. }