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.

137 lines
3.8KB

  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. */
  21. //#define DEBUG
  22. #include "avcodec.h"
  23. #include "common.h"
  24. #define WIN32_LEAN_AND_MEAN
  25. #include <windows.h>
  26. #include <process.h>
  27. typedef struct ThreadContext{
  28. AVCodecContext *avctx;
  29. HANDLE thread;
  30. HANDLE work_sem;
  31. HANDLE done_sem;
  32. int (*func)(AVCodecContext *c, void *arg);
  33. void *arg;
  34. int ret;
  35. }ThreadContext;
  36. static unsigned __stdcall thread_func(void *v){
  37. ThreadContext *c= v;
  38. for(;;){
  39. //printf("thread_func %X enter wait\n", (int)v); fflush(stdout);
  40. WaitForSingleObject(c->work_sem, INFINITE);
  41. //printf("thread_func %X after wait (func=%X)\n", (int)v, (int)c->func); fflush(stdout);
  42. if(c->func)
  43. c->ret= c->func(c->avctx, c->arg);
  44. else
  45. return 0;
  46. //printf("thread_func %X signal complete\n", (int)v); fflush(stdout);
  47. ReleaseSemaphore(c->done_sem, 1, 0);
  48. }
  49. return 0;
  50. }
  51. /**
  52. * free what has been allocated by avcodec_thread_init().
  53. * must be called after decoding has finished, especially dont call while avcodec_thread_execute() is running
  54. */
  55. void avcodec_thread_free(AVCodecContext *s){
  56. ThreadContext *c= s->thread_opaque;
  57. int i;
  58. for(i=0; i<s->thread_count; i++){
  59. c[i].func= NULL;
  60. ReleaseSemaphore(c[i].work_sem, 1, 0);
  61. WaitForSingleObject(c[i].thread, INFINITE);
  62. if(c[i].work_sem) CloseHandle(c[i].work_sem);
  63. if(c[i].done_sem) CloseHandle(c[i].done_sem);
  64. }
  65. av_freep(&s->thread_opaque);
  66. }
  67. int avcodec_thread_execute(AVCodecContext *s, int (*func)(AVCodecContext *c2, void *arg2),void **arg, int *ret, int count){
  68. ThreadContext *c= s->thread_opaque;
  69. int i;
  70. assert(s == c->avctx);
  71. assert(count <= s->thread_count);
  72. /* note, we can be certain that this is not called with the same AVCodecContext by different threads at the same time */
  73. for(i=0; i<count; i++){
  74. c[i].arg= arg[i];
  75. c[i].func= func;
  76. c[i].ret= 12345;
  77. ReleaseSemaphore(c[i].work_sem, 1, 0);
  78. }
  79. for(i=0; i<count; i++){
  80. WaitForSingleObject(c[i].done_sem, INFINITE);
  81. c[i].func= NULL;
  82. if(ret) ret[i]= c[i].ret;
  83. }
  84. return 0;
  85. }
  86. int avcodec_thread_init(AVCodecContext *s, int thread_count){
  87. int i;
  88. ThreadContext *c;
  89. uint32_t threadid;
  90. s->thread_count= thread_count;
  91. assert(!s->thread_opaque);
  92. c= av_mallocz(sizeof(ThreadContext)*thread_count);
  93. s->thread_opaque= c;
  94. for(i=0; i<thread_count; i++){
  95. //printf("init semaphors %d\n", i); fflush(stdout);
  96. c[i].avctx= s;
  97. if(!(c[i].work_sem = CreateSemaphore(NULL, 0, s->thread_count, NULL)))
  98. goto fail;
  99. if(!(c[i].done_sem = CreateSemaphore(NULL, 0, s->thread_count, NULL)))
  100. goto fail;
  101. //printf("create thread %d\n", i); fflush(stdout);
  102. c[i].thread = (HANDLE)_beginthreadex(NULL, 0, thread_func, &c[i], 0, &threadid );
  103. if( !c[i].thread ) goto fail;
  104. }
  105. //printf("init done\n"); fflush(stdout);
  106. s->execute= avcodec_thread_execute;
  107. return 0;
  108. fail:
  109. avcodec_thread_free(s);
  110. return -1;
  111. }