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