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.

147 lines
4.2KB

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