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