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.

151 lines
4.3KB

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