50b13dc416efc8b088790725a70f2302a8e0c6f5
[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 "avfilter.h"
23
24 /* TODO: buffer pool. see comment for avfilter_default_get_video_buffer() */
25 void avfilter_default_free_video_buffer(AVFilterPic *pic)
26 {
27 avpicture_free((AVPicture *) pic);
28 av_free(pic);
29 }
30
31 AVFilterPicRef *avfilter_next_get_video_buffer(AVFilterLink *link, int perms)
32 {
33 if(!link->dst->outputs[0])
34 return NULL;
35 return avfilter_get_video_buffer(link->dst->outputs[0], perms);
36 }
37
38 /* TODO: set the buffer's priv member to a context structure for the whole
39 * filter chain. This will allow for a buffer pool instead of the constant
40 * alloc & free cycle currently implemented. */
41 AVFilterPicRef *avfilter_default_get_video_buffer(AVFilterLink *link, int perms)
42 {
43 AVFilterPic *pic = av_mallocz(sizeof(AVFilterPic));
44 AVFilterPicRef *ref = av_mallocz(sizeof(AVFilterPicRef));
45
46 ref->pic = pic;
47 ref->w = link->w;
48 ref->h = link->h;
49 ref->perms = perms;
50
51 /* we always give the destination filter read access by default */
52 avfilter_add_pic_perms(ref, link->dst, AV_PERM_READ);
53
54 pic->refcount = 1;
55 pic->format = link->format;
56 pic->free = avfilter_default_free_video_buffer;
57 avpicture_alloc((AVPicture *)pic, pic->format, ref->w, ref->h);
58
59 memcpy(ref->data, pic->data, sizeof(pic->data));
60 memcpy(ref->linesize, pic->linesize, sizeof(pic->linesize));
61
62 return ref;
63 }
64
65 void avfilter_default_start_frame(AVFilterLink *link, AVFilterPicRef *picref)
66 {
67 AVFilterLink *out = NULL;
68
69 if(link->dst->output_count)
70 out = link->dst->outputs[0];
71
72 if(out) {
73 out->outpic = avfilter_get_video_buffer(out, AV_PERM_WRITE);
74 out->outpic->pts = picref->pts;
75 avfilter_start_frame(out, avfilter_ref_pic(out->outpic, out->dst, ~0));
76 }
77 }
78
79 void avfilter_default_end_frame(AVFilterLink *link)
80 {
81 AVFilterLink *out = NULL;
82
83 if(link->dst->output_count)
84 out = link->dst->outputs[0];
85
86 avfilter_unref_pic(link->cur_pic);
87 link->cur_pic = NULL;
88
89 if(out) {
90 if(out->outpic) {
91 avfilter_unref_pic(out->outpic);
92 out->outpic = NULL;
93 }
94 avfilter_end_frame(out);
95 }
96 }
97
98 /**
99 * default config_link() implementation for output video links to simplify
100 * the implementation of one input one output video filters */
101 int avfilter_default_config_output_link(AVFilterLink *link)
102 {
103 if(link->src->input_count && link->src->inputs[0]) {
104 link->w = link->src->inputs[0]->w;
105 link->h = link->src->inputs[0]->h;
106 } else {
107 /* XXX: any non-simple filter which would cause this branch to be taken
108 * really should implement its own config_props() for this link. */
109 link->w =
110 link->h = 0;
111 }
112
113 return 0;
114 }
115
116 /**
117 * default config_link() implementation for input video links to simplify
118 * the implementation of one input one output video filters */
119 int avfilter_default_config_input_link(AVFilterLink *link)
120 {
121 if(!link->dst->output_count)
122 return 0;
123 return avfilter_config_link(link->dst->outputs[0]);
124 }
125
126 /**
127 * default query_formats() implementation for output video links to simplify
128 * the implementation of one input one output video filters */
129 int *avfilter_default_query_output_formats(AVFilterLink *link)
130 {
131 if(link->src->input_count && link->src->inputs[0])
132 return avfilter_make_format_list(1, link->src->inputs[0]->format);
133 else
134 /* XXX: any non-simple filter which would cause this branch to be taken
135 * really should implement its own query_formats() for this link */
136 return avfilter_make_format_list(0);
137 }
138