History

Initial stab at an API

Commited in SoC by Bobby Bingham on 2007-06-29 17:03:03 Originally committed as revision 11965 to svn://svn.ffmpeg.org/ffmpeg/trunk

Vitor Sessak committed 18 years ago

Commit 4dbbcde1 parent f037f7e1 file · +268 −0Browse files

Unified · Split

libavfilter/avfilter.c+268−0
@@ -0,0 +1,268 @@
1+/*
2+ * Filter layer
3+ * copyright (c) 2007 Bobby Bingham
4+ *
5+ * This file is part of FFmpeg.
6+ *
7+ * FFmpeg is free software; you can redistribute it and/or
8+ * modify it under the terms of the GNU Lesser General Public
9+ * License as published by the Free Software Foundation; either
10+ * version 2.1 of the License, or (at your option) any later version.
11+ *
12+ * FFmpeg is distributed in the hope that it will be useful,
13+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
14+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15+ * Lesser General Public License for more details.
16+ *
17+ * You should have received a copy of the GNU Lesser General Public
18+ * License along with FFmpeg; if not, write to the Free Software
19+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20+ */
21+
22+#include <stdlib.h>
23+#include <string.h>
24+#include <stdio.h>
25+
26+#include "avfilter.h"
27+
28+/** list of registered filters, sorted by name */
29+static int filter_count = 0;
30+static AVFilter **filters = NULL;
31+
32+/* TODO: buffer pool. see comment for avfilter_default_get_video_buffer() */
33+void avfilter_default_free_video_buffer(AVFilterPic *pic)
34+{
35+ avpicture_free((AVPicture *) pic);
36+ av_free(pic);
37+}
38+
39+/* TODO: set the buffer's priv member to a context structure for the whole
40+ * filter chain. This will allow for a buffer pool instead of the constant
41+ * alloc & free cycle currently implemented. */
42+AVFilterPicRef *avfilter_default_get_video_buffer(AVFilterLink *link, int perms)
43+{
44+ AVFilterPic *pic = av_mallocz(sizeof(AVFilterPic));
45+ AVFilterPicRef *ref = av_mallocz(sizeof(AVFilterPicRef));
46+
47+ ref->pic = pic;
48+ ref->w = link->w;
49+ ref->h = link->h;
50+ ref->perms = perms;
51+
52+ pic->refcount = 1;
53+ pic->format = link->format;
54+ pic->free = avfilter_default_free_video_buffer;
55+ avpicture_alloc((AVPicture *)pic, pic->format, ref->w, ref->h);
56+
57+ ref->data[0] = pic->data[0];
58+ ref->data[1] = pic->data[1];
59+ ref->data[2] = pic->data[2];
60+ ref->data[3] = pic->data[3];
61+
62+ return ref;
63+}
64+
65+void avfilter_default_start_frame(AVFilterLink *link, AVFilterPicRef *picref)
66+{
67+ link->cur_pic = picref;
68+}
69+
70+void avfilter_default_end_frame(AVFilterLink *link)
71+{
72+ avfilter_unref_pic(link->cur_pic);
73+ link->cur_pic = NULL;
74+}
75+
76+AVFilterPicRef *avfilter_ref_pic(AVFilterPicRef *ref)
77+{
78+ AVFilterPicRef *ret = av_malloc(sizeof(AVFilterPicRef));
79+ memcpy(ret, ref, sizeof(AVFilterPicRef));
80+ ret->pic->refcount ++;
81+ return ret;
82+}
83+
84+void avfilter_unref_pic(AVFilterPicRef *ref)
85+{
86+ if(-- ref->pic->refcount == 0)
87+ ref->pic->free(ref->pic);
88+ av_free(ref);
89+}
90+
91+int avfilter_link(AVFilterContext *src, unsigned srcpad,
92+ AVFilterContext *dst, unsigned dstpad)
93+{
94+ AVFilterLink *link;
95+
96+ if(src->outputs[srcpad] || dst->inputs[dstpad])
97+ return -1;
98+
99+ src->outputs[srcpad] =
100+ dst->inputs[dstpad] = link = av_malloc(sizeof(AVFilterLink));
101+
102+ link->src = src;
103+ link->dst = dst;
104+ link->srcpad = srcpad;
105+ link->dstpad = dstpad;
106+ link->cur_pic = NULL;
107+
108+ src->filter->outputs[dstpad].set_video_props(link);
109+ return 0;
110+}
111+
112+AVFilterPicRef *avfilter_get_video_buffer(AVFilterLink *link, int perms)
113+{
114+ AVFilterPicRef *ret = NULL;
115+
116+ if(link->dst->filter->inputs[link->dstpad].get_video_buffer)
117+ ret = link->dst->filter->inputs[link->dstpad].get_video_buffer(link, perms);
118+
119+ if(!ret)
120+ ret = avfilter_default_get_video_buffer(link, perms);
121+
122+ return ret;
123+}
124+
125+void avfilter_request_frame(AVFilterLink *link)
126+{
127+ link->src->filter->outputs[link->srcpad].request_frame(link);
128+}
129+
130+/* XXX: should we do the duplicating of the picture ref here, instead of
131+ * forcing the source filter to do it? */
132+void avfilter_start_frame(AVFilterLink *link, AVFilterPicRef *picref)
133+{
134+ void (*start_frame)(AVFilterLink *, AVFilterPicRef *);
135+
136+ start_frame = link->dst->filter->inputs[link->dstpad].start_frame;
137+ if(!start_frame)
138+ start_frame = avfilter_default_start_frame;
139+
140+ start_frame(link, picref);
141+}
142+
143+void avfilter_end_frame(AVFilterLink *link)
144+{
145+ void (*end_frame)(AVFilterLink *);
146+
147+ end_frame = link->dst->filter->inputs[link->dstpad].end_frame;
148+ if(!end_frame)
149+ end_frame = avfilter_default_end_frame;
150+
151+ end_frame(link);
152+}
153+
154+void avfilter_draw_slice(AVFilterLink *link, uint8_t *data[4], int y, int h)
155+{
156+ link->dst->filter->inputs[link->dstpad].draw_slice(link, data, y, h);
157+}
158+
159+static int filter_cmp(const void *aa, const void *bb)
160+{
161+ const AVFilter *a = *(const AVFilter **)aa, *b = *(const AVFilter **)bb;
162+ return strcmp(a->name, b->name);
163+}
164+
165+AVFilter *avfilter_get_by_name(char *name)
166+{
167+ AVFilter key = { .name = name, };
168+ AVFilter *key2 = &key;
169+ AVFilter **ret;
170+
171+ ret = bsearch(&key2, filters, filter_count, sizeof(AVFilter **), filter_cmp);
172+ if(ret)
173+ return *ret;
174+ return NULL;
175+}
176+
177+/* FIXME: insert in order, rather than insert at end + resort */
178+void avfilter_register(AVFilter *filter)
179+{
180+ filters = av_realloc(filters, sizeof(AVFilter*) * (filter_count+1));
181+ filters[filter_count] = filter;
182+ qsort(filters, ++filter_count, sizeof(AVFilter **), filter_cmp);
183+}
184+
185+void avfilter_init(void)
186+{
187+}
188+
189+void avfilter_uninit(void)
190+{
191+ av_freep(&filters);
192+ filter_count = 0;
193+}
194+
195+static int pad_count(const AVFilterPad *pads)
196+{
197+ AVFilterPad *p = (AVFilterPad *) pads;
198+ int count;
199+
200+ for(count = 0; p->name; count ++) p ++;
201+ return count;
202+}
203+
204+static const char *filter_name(void *p)
205+{
206+ AVFilterContext *filter = p;
207+ return filter->filter->name;
208+}
209+
210+AVFilterContext *avfilter_create(AVFilter *filter)
211+{
212+ AVFilterContext *ret = av_malloc(sizeof(AVFilterContext));
213+
214+ ret->av_class = av_mallocz(sizeof(AVClass));
215+ ret->av_class->item_name = filter_name;
216+ ret->filter = filter;
217+ ret->inputs = av_mallocz(sizeof(AVFilterLink*) * pad_count(filter->inputs));
218+ ret->outputs = av_mallocz(sizeof(AVFilterLink*) * pad_count(filter->outputs));
219+ ret->priv = av_mallocz(filter->priv_size);
220+
221+ return ret;
222+}
223+
224+void avfilter_destroy(AVFilterContext *filter)
225+{
226+ int i;
227+
228+ if(filter->filter->uninit)
229+ filter->filter->uninit(filter);
230+
231+ for(i = 0; i < pad_count(filter->filter->inputs); i ++) {
232+ if(filter->inputs[i])
233+ filter->inputs[i]->src->outputs[filter->inputs[i]->srcpad] = NULL;
234+ av_free(filter->inputs[i]);
235+ }
236+ for(i = 0; i < pad_count(filter->filter->outputs); i ++) {
237+ if(filter->outputs[i])
238+ filter->outputs[i]->dst->inputs[filter->outputs[i]->dstpad] = NULL;
239+ av_free(filter->outputs[i]);
240+ }
241+
242+ av_free(filter->inputs);
243+ av_free(filter->outputs);
244+ av_free(filter->priv);
245+ av_free(filter->av_class);
246+ av_free(filter);
247+}
248+
249+AVFilterContext *avfilter_create_by_name(char *name)
250+{
251+ AVFilter *filt;
252+
253+ if(!(filt = avfilter_get_by_name(name))) return NULL;
254+ return avfilter_create(filt);
255+}
256+
257+int avfilter_init_filter(AVFilterContext *filter)
258+{
259+ int ret, i;
260+
261+ if(filter->filter->init)
262+ if((ret = filter->filter->init(filter))) return ret;
263+ for(i = 0; i < pad_count(filter->filter->outputs); i ++)
264+ if(filter->outputs[i])
265+ filter->filter->outputs[i].set_video_props(filter->outputs[i]);
266+ return 0;
267+}
268+

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t