Fix bugs in previous commit that caused FTBFS in synfig and ETL FTBFS with older...
[synfig.git] / synfig-core / tags / synfig_0_61_05 / synfig-core / src / modules / mod_libavcodec / libavformat / framehook.c
1 /*
2  * Video processing hooks
3  * Copyright (c) 2000, 2001 Fabrice Bellard.
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  */
19 #include <errno.h>
20 #include "config.h"
21 #include "framehook.h"
22 #include "avformat.h"
23
24 #ifdef CONFIG_HAVE_DLFCN
25 #include <dlfcn.h>
26 #endif
27
28
29 typedef struct _FrameHookEntry {
30     struct _FrameHookEntry *next;
31     FrameHookConfigureFn Configure;
32     FrameHookProcessFn Process;
33     FrameHookReleaseFn Release;
34     void *ctx;
35 } FrameHookEntry;
36
37 static FrameHookEntry *first_hook;
38
39 /* Returns 0 on OK */
40 int frame_hook_add(int argc, char *argv[])
41 {
42 #ifdef HAVE_VHOOK
43     void *loaded;
44     FrameHookEntry *fhe, **fhep;
45
46     if (argc < 1) {
47         return ENOENT;
48     }
49
50     loaded = dlopen(argv[0], RTLD_NOW);
51     if (!loaded) {
52         fprintf(stderr, "%s\n", dlerror());
53         return -1;
54     }
55
56     fhe = av_mallocz(sizeof(*fhe));
57     if (!fhe) {
58         return errno;
59     }
60
61     fhe->Configure = dlsym(loaded, "Configure");
62     fhe->Process = dlsym(loaded, "Process");
63     fhe->Release = dlsym(loaded, "Release");    /* Optional */
64
65     if (!fhe->Process) {
66         fprintf(stderr, "Failed to find Process entrypoint in %s\n", argv[0]);
67         return -1;
68     }
69
70     if (!fhe->Configure && argc > 1) {
71         fprintf(stderr, "Failed to find Configure entrypoint in %s\n", argv[0]);
72         return -1;
73     }
74
75     if (argc > 1 || fhe->Configure) {
76         if (fhe->Configure(&fhe->ctx, argc, argv)) {
77             fprintf(stderr, "Failed to Configure %s\n", argv[0]);
78             return -1;
79         }
80     }
81
82     for (fhep = &first_hook; *fhep; fhep = &((*fhep)->next)) {
83     }
84
85     *fhep = fhe;
86
87     return 0;
88 #else
89     fprintf(stderr, "Video hooking not compiled into this version\n");
90     return 1;
91 #endif
92 }
93
94 void frame_hook_process(AVPicture *pict, enum PixelFormat pix_fmt, int width, int height)
95 {
96     if (first_hook) {
97         FrameHookEntry *fhe;
98         int64_t pts = av_gettime();
99
100         for (fhe = first_hook; fhe; fhe = fhe->next) {
101             fhe->Process(fhe->ctx, pict, pix_fmt, width, height, pts);
102         }
103     }
104 }
105
106 void frame_hook_release(void)
107 {
108     FrameHookEntry *fhe;
109     FrameHookEntry *fhenext;
110
111     for (fhe = first_hook; fhe; fhe = fhenext) {
112         fhenext = fhe->next;
113         if (fhe->Release)
114             fhe->Release(fhe->ctx);
115         av_free(fhe);
116     }
117
118     first_hook = NULL;
119 }