]> git.sesse.net Git - ffmpeg/blob - libavfilter/defaults.c
7c75ab9c4bf63384fe9e62509d6a44dc8dd8c164
[ffmpeg] / libavfilter / defaults.c
1 /*
2  * Filter layer - default implementations
3  * Copyright (c) 2007 Bobby Bingham
4  *
5  * This file is part of Libav.
6  *
7  * Libav 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  * Libav 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 Libav; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21
22 #include "libavutil/audioconvert.h"
23 #include "libavutil/imgutils.h"
24 #include "libavutil/samplefmt.h"
25 #include "avfilter.h"
26 #include "internal.h"
27
28 /* TODO: buffer pool.  see comment for avfilter_default_get_video_buffer() */
29 void ff_avfilter_default_free_buffer(AVFilterBuffer *ptr)
30 {
31     if (ptr->extended_data != ptr->data)
32         av_freep(&ptr->extended_data);
33     av_free(ptr->data[0]);
34     av_free(ptr);
35 }
36
37 /* TODO: set the buffer's priv member to a context structure for the whole
38  * filter chain.  This will allow for a buffer pool instead of the constant
39  * alloc & free cycle currently implemented. */
40 AVFilterBufferRef *avfilter_default_get_video_buffer(AVFilterLink *link, int perms, int w, int h)
41 {
42     int linesize[4];
43     uint8_t *data[4];
44     AVFilterBufferRef *picref = NULL;
45
46     // +2 is needed for swscaler, +16 to be SIMD-friendly
47     if (av_image_alloc(data, linesize, w, h, link->format, 16) < 0)
48         return NULL;
49
50     picref = avfilter_get_video_buffer_ref_from_arrays(data, linesize,
51                                                        perms, w, h, link->format);
52     if (!picref) {
53         av_free(data[0]);
54         return NULL;
55     }
56
57     return picref;
58 }
59
60 AVFilterBufferRef *avfilter_default_get_audio_buffer(AVFilterLink *link, int perms,
61                                                      enum AVSampleFormat sample_fmt, int nb_samples,
62                                                      uint64_t channel_layout)
63 {
64     AVFilterBufferRef *samplesref = NULL;
65     uint8_t **data;
66     int planar      = av_sample_fmt_is_planar(sample_fmt);
67     int nb_channels = av_get_channel_layout_nb_channels(channel_layout);
68     int planes      = planar ? nb_channels : 1;
69     int linesize;
70
71     if (!(data = av_mallocz(sizeof(*data) * planes)))
72         goto fail;
73
74     if (av_samples_alloc(data, &linesize, nb_channels, nb_samples, sample_fmt, 0) < 0)
75         goto fail;
76
77     samplesref = avfilter_get_audio_buffer_ref_from_arrays(data, linesize, perms,
78                                                            nb_samples, sample_fmt,
79                                                            channel_layout);
80     if (!samplesref)
81         goto fail;
82
83     av_freep(&data);
84
85 fail:
86     if (data)
87         av_freep(&data[0]);
88     av_freep(&data);
89     return samplesref;
90 }
91
92 void avfilter_default_start_frame(AVFilterLink *inlink, AVFilterBufferRef *picref)
93 {
94     AVFilterLink *outlink = NULL;
95
96     if (inlink->dst->output_count)
97         outlink = inlink->dst->outputs[0];
98
99     if (outlink) {
100         outlink->out_buf = avfilter_get_video_buffer(outlink, AV_PERM_WRITE, outlink->w, outlink->h);
101         avfilter_copy_buffer_ref_props(outlink->out_buf, picref);
102         avfilter_start_frame(outlink, avfilter_ref_buffer(outlink->out_buf, ~0));
103     }
104 }
105
106 void avfilter_default_draw_slice(AVFilterLink *inlink, int y, int h, int slice_dir)
107 {
108     AVFilterLink *outlink = NULL;
109
110     if (inlink->dst->output_count)
111         outlink = inlink->dst->outputs[0];
112
113     if (outlink)
114         avfilter_draw_slice(outlink, y, h, slice_dir);
115 }
116
117 void avfilter_default_end_frame(AVFilterLink *inlink)
118 {
119     AVFilterLink *outlink = NULL;
120
121     if (inlink->dst->output_count)
122         outlink = inlink->dst->outputs[0];
123
124     avfilter_unref_buffer(inlink->cur_buf);
125     inlink->cur_buf = NULL;
126
127     if (outlink) {
128         if (outlink->out_buf) {
129             avfilter_unref_buffer(outlink->out_buf);
130             outlink->out_buf = NULL;
131         }
132         avfilter_end_frame(outlink);
133     }
134 }
135
136 /* FIXME: samplesref is same as link->cur_buf. Need to consider removing the redundant parameter. */
137 void avfilter_default_filter_samples(AVFilterLink *inlink, AVFilterBufferRef *samplesref)
138 {
139     AVFilterLink *outlink = NULL;
140
141     if (inlink->dst->output_count)
142         outlink = inlink->dst->outputs[0];
143
144     if (outlink) {
145         outlink->out_buf = avfilter_default_get_audio_buffer(inlink, AV_PERM_WRITE, samplesref->format,
146                                                              samplesref->audio->nb_samples,
147                                                              samplesref->audio->channel_layout);
148         outlink->out_buf->pts                = samplesref->pts;
149         outlink->out_buf->audio->sample_rate = samplesref->audio->sample_rate;
150         avfilter_filter_samples(outlink, avfilter_ref_buffer(outlink->out_buf, ~0));
151         avfilter_unref_buffer(outlink->out_buf);
152         outlink->out_buf = NULL;
153     }
154     avfilter_unref_buffer(samplesref);
155     inlink->cur_buf = NULL;
156 }
157
158 /**
159  * default config_link() implementation for output video links to simplify
160  * the implementation of one input one output video filters */
161 int avfilter_default_config_output_link(AVFilterLink *link)
162 {
163     if (link->src->input_count && link->src->inputs[0]) {
164         if (link->type == AVMEDIA_TYPE_VIDEO) {
165             link->w = link->src->inputs[0]->w;
166             link->h = link->src->inputs[0]->h;
167             link->time_base = link->src->inputs[0]->time_base;
168         } else if (link->type == AVMEDIA_TYPE_AUDIO) {
169             link->channel_layout = link->src->inputs[0]->channel_layout;
170             link->sample_rate    = link->src->inputs[0]->sample_rate;
171         }
172     } else {
173         /* XXX: any non-simple filter which would cause this branch to be taken
174          * really should implement its own config_props() for this link. */
175         return -1;
176     }
177
178     return 0;
179 }
180
181 /**
182  * A helper for query_formats() which sets all links to the same list of
183  * formats. If there are no links hooked to this filter, the list of formats is
184  * freed.
185  *
186  * FIXME: this will need changed for filters with a mix of pad types
187  * (video + audio, etc)
188  */
189 void avfilter_set_common_formats(AVFilterContext *ctx, AVFilterFormats *formats)
190 {
191     int count = 0, i;
192
193     for (i = 0; i < ctx->input_count; i++) {
194         if (ctx->inputs[i]) {
195             avfilter_formats_ref(formats, &ctx->inputs[i]->out_formats);
196             count++;
197         }
198     }
199     for (i = 0; i < ctx->output_count; i++) {
200         if (ctx->outputs[i]) {
201             avfilter_formats_ref(formats, &ctx->outputs[i]->in_formats);
202             count++;
203         }
204     }
205
206     if (!count) {
207         av_free(formats->formats);
208         av_free(formats->refs);
209         av_free(formats);
210     }
211 }
212
213 int avfilter_default_query_formats(AVFilterContext *ctx)
214 {
215     enum AVMediaType type = ctx->inputs  && ctx->inputs [0] ? ctx->inputs [0]->type :
216                             ctx->outputs && ctx->outputs[0] ? ctx->outputs[0]->type :
217                             AVMEDIA_TYPE_VIDEO;
218
219     avfilter_set_common_formats(ctx, avfilter_all_formats(type));
220     return 0;
221 }
222
223 void avfilter_null_start_frame(AVFilterLink *link, AVFilterBufferRef *picref)
224 {
225     avfilter_start_frame(link->dst->outputs[0], picref);
226 }
227
228 void avfilter_null_draw_slice(AVFilterLink *link, int y, int h, int slice_dir)
229 {
230     avfilter_draw_slice(link->dst->outputs[0], y, h, slice_dir);
231 }
232
233 void avfilter_null_end_frame(AVFilterLink *link)
234 {
235     avfilter_end_frame(link->dst->outputs[0]);
236 }
237
238 void avfilter_null_filter_samples(AVFilterLink *link, AVFilterBufferRef *samplesref)
239 {
240     avfilter_filter_samples(link->dst->outputs[0], samplesref);
241 }
242
243 AVFilterBufferRef *avfilter_null_get_video_buffer(AVFilterLink *link, int perms, int w, int h)
244 {
245     return avfilter_get_video_buffer(link->dst->outputs[0], perms, w, h);
246 }
247
248 AVFilterBufferRef *avfilter_null_get_audio_buffer(AVFilterLink *link, int perms,
249                                                   enum AVSampleFormat sample_fmt, int nb_samples,
250                                                   uint64_t channel_layout)
251 {
252     return avfilter_get_audio_buffer(link->dst->outputs[0], perms, sample_fmt,
253                                      nb_samples, channel_layout);
254 }