8deef93fe64698ee6c8e041250e6d00c3d5a0b06
[libav.git] / libavfilter / defaults.c
1 /*
2 * Filter layer - default implementations
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 "libavcore/imgutils.h"
23 #include "avfilter.h"
24
25 /* TODO: buffer pool. see comment for avfilter_default_get_video_buffer() */
26 static void avfilter_default_free_buffer(AVFilterBuffer *ptr)
27 {
28 av_free(ptr->data[0]);
29 av_free(ptr);
30 }
31
32 /* TODO: set the buffer's priv member to a context structure for the whole
33 * filter chain. This will allow for a buffer pool instead of the constant
34 * alloc & free cycle currently implemented. */
35 AVFilterPicRef *avfilter_default_get_video_buffer(AVFilterLink *link, int perms, int w, int h)
36 {
37 AVFilterBuffer *pic = av_mallocz(sizeof(AVFilterBuffer));
38 AVFilterPicRef *ref = av_mallocz(sizeof(AVFilterPicRef));
39 int i, tempsize;
40 char *buf;
41
42 ref->pic = pic;
43 ref->w = w;
44 ref->h = h;
45
46 /* make sure the buffer gets read permission or it's useless for output */
47 ref->perms = perms | AV_PERM_READ;
48
49 pic->refcount = 1;
50 pic->format = link->format;
51 pic->free = avfilter_default_free_buffer;
52 av_fill_image_linesizes(pic->linesize, pic->format, ref->w);
53
54 for (i=0; i<4;i++)
55 pic->linesize[i] = FFALIGN(pic->linesize[i], 16);
56
57 tempsize = av_fill_image_pointers(pic->data, pic->format, ref->h, NULL, pic->linesize);
58 buf = av_malloc(tempsize + 16); // +2 is needed for swscaler, +16 to be
59 // SIMD-friendly
60 av_fill_image_pointers(pic->data, pic->format, ref->h, buf, pic->linesize);
61
62 memcpy(ref->data, pic->data, sizeof(pic->data));
63 memcpy(ref->linesize, pic->linesize, sizeof(pic->linesize));
64
65 return ref;
66 }
67
68 void avfilter_default_start_frame(AVFilterLink *link, AVFilterPicRef *picref)
69 {
70 AVFilterLink *out = NULL;
71
72 if(link->dst->output_count)
73 out = link->dst->outputs[0];
74
75 if(out) {
76 out->outpic = avfilter_get_video_buffer(out, AV_PERM_WRITE, out->w, out->h);
77 avfilter_copy_picref_props(out->outpic, picref);
78 avfilter_start_frame(out, avfilter_ref_pic(out->outpic, ~0));
79 }
80 }
81
82 void avfilter_default_draw_slice(AVFilterLink *link, int y, int h, int slice_dir)
83 {
84 AVFilterLink *out = NULL;
85
86 if(link->dst->output_count)
87 out = link->dst->outputs[0];
88
89 if(out)
90 avfilter_draw_slice(out, y, h, slice_dir);
91 }
92
93 void avfilter_default_end_frame(AVFilterLink *link)
94 {
95 AVFilterLink *out = NULL;
96
97 if(link->dst->output_count)
98 out = link->dst->outputs[0];
99
100 avfilter_unref_pic(link->cur_pic);
101 link->cur_pic = NULL;
102
103 if(out) {
104 if(out->outpic) {
105 avfilter_unref_pic(out->outpic);
106 out->outpic = NULL;
107 }
108 avfilter_end_frame(out);
109 }
110 }
111
112 /**
113 * default config_link() implementation for output video links to simplify
114 * the implementation of one input one output video filters */
115 int avfilter_default_config_output_link(AVFilterLink *link)
116 {
117 if(link->src->input_count && link->src->inputs[0]) {
118 link->w = link->src->inputs[0]->w;
119 link->h = link->src->inputs[0]->h;
120 } else {
121 /* XXX: any non-simple filter which would cause this branch to be taken
122 * really should implement its own config_props() for this link. */
123 return -1;
124 }
125
126 return 0;
127 }
128
129 /**
130 * A helper for query_formats() which sets all links to the same list of
131 * formats. If there are no links hooked to this filter, the list of formats is
132 * freed.
133 *
134 * FIXME: this will need changed for filters with a mix of pad types
135 * (video + audio, etc)
136 */
137 void avfilter_set_common_formats(AVFilterContext *ctx, AVFilterFormats *formats)
138 {
139 int count = 0, i;
140
141 for(i = 0; i < ctx->input_count; i ++) {
142 if(ctx->inputs[i]) {
143 avfilter_formats_ref(formats, &ctx->inputs[i]->out_formats);
144 count ++;
145 }
146 }
147 for(i = 0; i < ctx->output_count; i ++) {
148 if(ctx->outputs[i]) {
149 avfilter_formats_ref(formats, &ctx->outputs[i]->in_formats);
150 count ++;
151 }
152 }
153
154 if(!count) {
155 av_free(formats->formats);
156 av_free(formats->refs);
157 av_free(formats);
158 }
159 }
160
161 int avfilter_default_query_formats(AVFilterContext *ctx)
162 {
163 enum AVMediaType type = ctx->inputs [0] ? ctx->inputs [0]->type :
164 ctx->outputs[0] ? ctx->outputs[0]->type :
165 AVMEDIA_TYPE_VIDEO;
166
167 avfilter_set_common_formats(ctx, avfilter_all_formats(type));
168 return 0;
169 }
170
171 void avfilter_null_start_frame(AVFilterLink *link, AVFilterPicRef *picref)
172 {
173 avfilter_start_frame(link->dst->outputs[0], picref);
174 }
175
176 void avfilter_null_draw_slice(AVFilterLink *link, int y, int h, int slice_dir)
177 {
178 avfilter_draw_slice(link->dst->outputs[0], y, h, slice_dir);
179 }
180
181 void avfilter_null_end_frame(AVFilterLink *link)
182 {
183 avfilter_end_frame(link->dst->outputs[0]);
184 }
185
186 AVFilterPicRef *avfilter_null_get_video_buffer(AVFilterLink *link, int perms, int w, int h)
187 {
188 return avfilter_get_video_buffer(link->dst->outputs[0], perms, w, h);
189 }
190