Force linesize to be a multiple of 16
[libav.git] / libavfilter / defaults.c
CommitLineData
bf4ce7a3
VS
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() */
25void avfilter_default_free_video_buffer(AVFilterPic *pic)
26{
27 avpicture_free((AVPicture *) pic);
28 av_free(pic);
29}
30
31/* TODO: set the buffer's priv member to a context structure for the whole
32 * filter chain. This will allow for a buffer pool instead of the constant
33 * alloc & free cycle currently implemented. */
34AVFilterPicRef *avfilter_default_get_video_buffer(AVFilterLink *link, int perms)
35{
36 AVFilterPic *pic = av_mallocz(sizeof(AVFilterPic));
37 AVFilterPicRef *ref = av_mallocz(sizeof(AVFilterPicRef));
38
39 ref->pic = pic;
40 ref->w = link->w;
41 ref->h = link->h;
4f909565
VS
42
43 /* make sure the buffer gets read permission or it's useless for output */
44 ref->perms = perms | AV_PERM_READ;
bf4ce7a3
VS
45
46 pic->refcount = 1;
47 pic->format = link->format;
48 pic->free = avfilter_default_free_video_buffer;
4cf48782
VS
49 avpicture_alloc((AVPicture *)pic, pic->format,
50 (ref->w + 15) & (~15), // make linesize a multiple of 16
51 (ref->h + 15) & (~15));
bf4ce7a3
VS
52
53 memcpy(ref->data, pic->data, sizeof(pic->data));
54 memcpy(ref->linesize, pic->linesize, sizeof(pic->linesize));
55
56 return ref;
57}
58
59void avfilter_default_start_frame(AVFilterLink *link, AVFilterPicRef *picref)
60{
61 AVFilterLink *out = NULL;
62
63 if(link->dst->output_count)
64 out = link->dst->outputs[0];
65
bf4ce7a3
VS
66 if(out) {
67 out->outpic = avfilter_get_video_buffer(out, AV_PERM_WRITE);
68 out->outpic->pts = picref->pts;
23274667 69 avfilter_start_frame(out, avfilter_ref_pic(out->outpic, ~0));
bf4ce7a3
VS
70 }
71}
72
73void avfilter_default_end_frame(AVFilterLink *link)
74{
75 AVFilterLink *out = NULL;
76
77 if(link->dst->output_count)
78 out = link->dst->outputs[0];
79
80 avfilter_unref_pic(link->cur_pic);
81 link->cur_pic = NULL;
82
83 if(out) {
552c0208 84 if(out->outpic) {
fc0f39c2
VS
85 avfilter_unref_pic(out->outpic);
86 out->outpic = NULL;
552c0208 87 }
bf4ce7a3
VS
88 avfilter_end_frame(out);
89 }
90}
91
92/**
93 * default config_link() implementation for output video links to simplify
94 * the implementation of one input one output video filters */
95int avfilter_default_config_output_link(AVFilterLink *link)
96{
97 if(link->src->input_count && link->src->inputs[0]) {
98 link->w = link->src->inputs[0]->w;
99 link->h = link->src->inputs[0]->h;
100 } else {
101 /* XXX: any non-simple filter which would cause this branch to be taken
102 * really should implement its own config_props() for this link. */
d0e4eba5 103 return -1;
bf4ce7a3
VS
104 }
105
106 return 0;
107}
108
109/**
e363730c
VS
110 * A helper for query_formats() which sets all links to the same list of
111 * formats. If there are no links hooked to this filter, the list of formats is
112 * freed.
113 *
114 * FIXME: this will need changed for filters with a mix of pad types
115 * (video + audio, etc)
116 */
117void avfilter_set_common_formats(AVFilterContext *ctx, AVFilterFormats *formats)
bf4ce7a3 118{
e363730c
VS
119 int count = 0, i;
120
121 for(i = 0; i < ctx->input_count; i ++) {
122 if(ctx->inputs[i]) {
123 avfilter_formats_ref(formats, &ctx->inputs[i]->out_formats);
124 count ++;
125 }
126 }
127 for(i = 0; i < ctx->output_count; i ++) {
128 if(ctx->outputs[i]) {
129 avfilter_formats_ref(formats, &ctx->outputs[i]->in_formats);
130 count ++;
131 }
132 }
133
134 if(!count) {
135 av_free(formats->formats);
136 av_free(formats->refs);
137 av_free(formats);
138 }
139}
140
141int avfilter_default_query_formats(AVFilterContext *ctx)
142{
143 avfilter_set_common_formats(ctx, avfilter_all_colorspaces());
144 return 0;
bf4ce7a3
VS
145}
146