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.

166 lines
5.0KB

  1. /*
  2. * Copyright (c) 2004 Michael Niedermayer <michaelni@gmx.at>
  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. #define WIN32_LEAN_AND_MEAN
  23. #include <windows.h>
  24. #include <process.h>
  25. typedef struct ThreadContext{
  26. AVCodecContext *avctx;
  27. HANDLE thread;
  28. HANDLE work_sem;
  29. HANDLE job_sem;
  30. HANDLE done_sem;
  31. int (*func)(AVCodecContext *c, void *arg);
  32. int (*func2)(AVCodecContext *c, void *arg, int, int);
  33. void *arg;
  34. int argsize;
  35. int *jobnr;
  36. int *ret;
  37. int threadnr;
  38. }ThreadContext;
  39. static unsigned WINAPI attribute_align_arg thread_func(void *v){
  40. ThreadContext *c= v;
  41. for(;;){
  42. int ret, jobnr;
  43. //printf("thread_func %X enter wait\n", (int)v); fflush(stdout);
  44. WaitForSingleObject(c->work_sem, INFINITE);
  45. // avoid trying to access jobnr if we should quit
  46. if (!c->func && !c->func2)
  47. break;
  48. WaitForSingleObject(c->job_sem, INFINITE);
  49. jobnr = (*c->jobnr)++;
  50. ReleaseSemaphore(c->job_sem, 1, 0);
  51. //printf("thread_func %X after wait (func=%X)\n", (int)v, (int)c->func); fflush(stdout);
  52. if(c->func)
  53. ret= c->func(c->avctx, (uint8_t *)c->arg + jobnr*c->argsize);
  54. else
  55. ret= c->func2(c->avctx, c->arg, jobnr, c->threadnr);
  56. if (c->ret)
  57. c->ret[jobnr] = ret;
  58. //printf("thread_func %X signal complete\n", (int)v); fflush(stdout);
  59. ReleaseSemaphore(c->done_sem, 1, 0);
  60. }
  61. return 0;
  62. }
  63. /**
  64. * Free what has been allocated by avcodec_thread_init().
  65. * Must be called after decoding has finished, especially do not call while avcodec_thread_execute() is running.
  66. */
  67. void avcodec_thread_free(AVCodecContext *s){
  68. ThreadContext *c= s->thread_opaque;
  69. int i;
  70. for(i=0; i<s->thread_count; i++){
  71. c[i].func= NULL;
  72. c[i].func2= NULL;
  73. }
  74. ReleaseSemaphore(c[0].work_sem, s->thread_count, 0);
  75. for(i=0; i<s->thread_count; i++){
  76. WaitForSingleObject(c[i].thread, INFINITE);
  77. if(c[i].thread) CloseHandle(c[i].thread);
  78. }
  79. if(c[0].work_sem) CloseHandle(c[0].work_sem);
  80. if(c[0].job_sem) CloseHandle(c[0].job_sem);
  81. if(c[0].done_sem) CloseHandle(c[0].done_sem);
  82. av_freep(&s->thread_opaque);
  83. }
  84. int avcodec_thread_execute(AVCodecContext *s, int (*func)(AVCodecContext *c2, void *arg2),void *arg, int *ret, int count, int size){
  85. ThreadContext *c= s->thread_opaque;
  86. int i;
  87. int jobnr = 0;
  88. assert(s == c->avctx);
  89. /* note, we can be certain that this is not called with the same AVCodecContext by different threads at the same time */
  90. for(i=0; i<s->thread_count; i++){
  91. c[i].arg= arg;
  92. c[i].argsize= size;
  93. c[i].func= func;
  94. c[i].ret= ret;
  95. c[i].jobnr = &jobnr;
  96. }
  97. ReleaseSemaphore(c[0].work_sem, count, 0);
  98. for(i=0; i<count; i++)
  99. WaitForSingleObject(c[0].done_sem, INFINITE);
  100. return 0;
  101. }
  102. int avcodec_thread_execute2(AVCodecContext *s, int (*func)(AVCodecContext *c2, void *arg2, int, int),void *arg, int *ret, int count){
  103. ThreadContext *c= s->thread_opaque;
  104. int i;
  105. for(i=0; i<s->thread_count; i++)
  106. c[i].func2 = func;
  107. avcodec_thread_execute(s, NULL, arg, ret, count, 0);
  108. }
  109. int avcodec_thread_init(AVCodecContext *s, int thread_count){
  110. int i;
  111. ThreadContext *c;
  112. uint32_t threadid;
  113. s->thread_count= thread_count;
  114. assert(!s->thread_opaque);
  115. c= av_mallocz(sizeof(ThreadContext)*thread_count);
  116. s->thread_opaque= c;
  117. if(!(c[0].work_sem = CreateSemaphore(NULL, 0, INT_MAX, NULL)))
  118. goto fail;
  119. if(!(c[0].job_sem = CreateSemaphore(NULL, 1, 1, NULL)))
  120. goto fail;
  121. if(!(c[0].done_sem = CreateSemaphore(NULL, 0, INT_MAX, NULL)))
  122. goto fail;
  123. for(i=0; i<thread_count; i++){
  124. //printf("init semaphors %d\n", i); fflush(stdout);
  125. c[i].avctx= s;
  126. c[i].work_sem = c[0].work_sem;
  127. c[i].job_sem = c[0].job_sem;
  128. c[i].done_sem = c[0].done_sem;
  129. c[i].threadnr = i;
  130. //printf("create thread %d\n", i); fflush(stdout);
  131. c[i].thread = (HANDLE)_beginthreadex(NULL, 0, thread_func, &c[i], 0, &threadid );
  132. if( !c[i].thread ) goto fail;
  133. }
  134. //printf("init done\n"); fflush(stdout);
  135. s->execute= avcodec_thread_execute;
  136. s->execute2= avcodec_thread_execute2;
  137. return 0;
  138. fail:
  139. avcodec_thread_free(s);
  140. return -1;
  141. }