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.

184 lines
4.8KB

  1. /*
  2. * Copyright (c) 2004 François Revol <revol@free.fr>
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. //#define DEBUG
  21. #include "avcodec.h"
  22. #include <OS.h>
  23. typedef struct ThreadContext{
  24. AVCodecContext *avctx;
  25. thread_id thread;
  26. sem_id work_sem;
  27. sem_id done_sem;
  28. int (*func)(AVCodecContext *c, void *arg);
  29. void *arg;
  30. int ret;
  31. }ThreadContext;
  32. // it's odd Be never patented that :D
  33. struct benaphore {
  34. vint32 atom;
  35. sem_id sem;
  36. };
  37. static inline int lock_ben(struct benaphore *ben)
  38. {
  39. if (atomic_add(&ben->atom, 1) > 0)
  40. return acquire_sem(ben->sem);
  41. return B_OK;
  42. }
  43. static inline int unlock_ben(struct benaphore *ben)
  44. {
  45. if (atomic_add(&ben->atom, -1) > 1)
  46. return release_sem(ben->sem);
  47. return B_OK;
  48. }
  49. static struct benaphore av_thread_lib_ben;
  50. static int32 ff_thread_func(void *v){
  51. ThreadContext *c= v;
  52. for(;;){
  53. //printf("thread_func %X enter wait\n", (int)v); fflush(stdout);
  54. acquire_sem(c->work_sem);
  55. //printf("thread_func %X after wait (func=%X)\n", (int)v, (int)c->func); fflush(stdout);
  56. if(c->func)
  57. c->ret= c->func(c->avctx, c->arg);
  58. else
  59. return 0;
  60. //printf("thread_func %X signal complete\n", (int)v); fflush(stdout);
  61. release_sem(c->done_sem);
  62. }
  63. return B_OK;
  64. }
  65. /**
  66. * Free what has been allocated by avcodec_thread_init().
  67. * Must be called after decoding has finished, especially do not call while avcodec_thread_execute() is running.
  68. */
  69. void avcodec_thread_free(AVCodecContext *s){
  70. ThreadContext *c= s->thread_opaque;
  71. int i;
  72. int32 ret;
  73. for(i=0; i<s->thread_count; i++){
  74. c[i].func= NULL;
  75. release_sem(c[i].work_sem);
  76. wait_for_thread(c[i].thread, &ret);
  77. if(c[i].work_sem > B_OK) delete_sem(c[i].work_sem);
  78. if(c[i].done_sem > B_OK) delete_sem(c[i].done_sem);
  79. }
  80. av_freep(&s->thread_opaque);
  81. }
  82. int avcodec_thread_execute(AVCodecContext *s, int (*func)(AVCodecContext *c2, void *arg2),void *arg, int *ret, int count, int size){
  83. ThreadContext *c= s->thread_opaque;
  84. int i;
  85. assert(s == c->avctx);
  86. assert(count <= s->thread_count);
  87. /* note, we can be certain that this is not called with the same AVCodecContext by different threads at the same time */
  88. for(i=0; i<count; i++){
  89. c[i].arg= (char*)arg + i*size;
  90. c[i].func= func;
  91. c[i].ret= 12345;
  92. release_sem(c[i].work_sem);
  93. }
  94. for(i=0; i<count; i++){
  95. acquire_sem(c[i].done_sem);
  96. c[i].func= NULL;
  97. if(ret) ret[i]= c[i].ret;
  98. }
  99. return 0;
  100. }
  101. int avcodec_thread_init(AVCodecContext *s, int thread_count){
  102. int i;
  103. ThreadContext *c;
  104. s->thread_count= thread_count;
  105. if (thread_count <= 1)
  106. return 0;
  107. assert(!s->thread_opaque);
  108. c= av_mallocz(sizeof(ThreadContext)*thread_count);
  109. s->thread_opaque= c;
  110. for(i=0; i<thread_count; i++){
  111. //printf("init semaphors %d\n", i); fflush(stdout);
  112. c[i].avctx= s;
  113. if((c[i].work_sem = create_sem(0, "ff work sem")) < B_OK)
  114. goto fail;
  115. if((c[i].done_sem = create_sem(0, "ff done sem")) < B_OK)
  116. goto fail;
  117. //printf("create thread %d\n", i); fflush(stdout);
  118. c[i].thread = spawn_thread(ff_thread_func, "libavcodec thread", B_LOW_PRIORITY, &c[i] );
  119. if( c[i].thread < B_OK ) goto fail;
  120. resume_thread(c[i].thread );
  121. }
  122. //printf("init done\n"); fflush(stdout);
  123. s->execute= avcodec_thread_execute;
  124. return 0;
  125. fail:
  126. avcodec_thread_free(s);
  127. return -1;
  128. }
  129. /* provide a mean to serialize calls to avcodec_*() for thread safety. */
  130. int avcodec_thread_lock_lib(void)
  131. {
  132. return lock_ben(&av_thread_lib_ben);
  133. }
  134. int avcodec_thread_unlock_lib(void)
  135. {
  136. return unlock_ben(&av_thread_lib_ben);
  137. }
  138. /* our versions of _init and _fini (which are called by those actually from crt.o) */
  139. void initialize_after(void)
  140. {
  141. av_thread_lib_ben.atom = 0;
  142. av_thread_lib_ben.sem = create_sem(0, "libavcodec benaphore");
  143. }
  144. void uninitialize_before(void)
  145. {
  146. delete_sem(av_thread_lib_ben.sem);
  147. }