]> git.sesse.net Git - ffmpeg/blob - libavfilter/defaults.c
vf_scale: support PAL8 output by producing BGR8.
[ffmpeg] / 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 "libavutil/audioconvert.h"
23 #include "libavutil/imgutils.h"
24 #include "libavutil/samplefmt.h"
25 #include "avfilter.h"
26 #include "internal.h"
27
28 void ff_avfilter_default_free_buffer(AVFilterBuffer *ptr)
29 {
30     av_free(ptr->data[0]);
31     av_free(ptr);
32 }
33
34 /* TODO: set the buffer's priv member to a context structure for the whole
35  * filter chain.  This will allow for a buffer pool instead of the constant
36  * alloc & free cycle currently implemented. */
37 AVFilterBufferRef *avfilter_default_get_video_buffer(AVFilterLink *link, int perms, int w, int h)
38 {
39     int linesize[4];
40     uint8_t *data[4];
41     int i;
42     AVFilterBufferRef *picref = NULL;
43     AVFilterPool *pool = link->pool;
44
45     if (pool) {
46         for (i = 0; i < POOL_SIZE; i++) {
47             picref = pool->pic[i];
48             if (picref && picref->buf->format == link->format && picref->buf->w == w && picref->buf->h == h) {
49                 AVFilterBuffer *pic = picref->buf;
50                 pool->pic[i] = NULL;
51                 pool->count--;
52                 picref->video->w = w;
53                 picref->video->h = h;
54                 picref->perms = perms | AV_PERM_READ;
55                 picref->format = link->format;
56                 pic->refcount = 1;
57                 memcpy(picref->data,     pic->data,     sizeof(picref->data));
58                 memcpy(picref->linesize, pic->linesize, sizeof(picref->linesize));
59                 return picref;
60             }
61         }
62     } else
63         pool = link->pool = av_mallocz(sizeof(AVFilterPool));
64
65     // align: +2 is needed for swscaler, +16 to be SIMD-friendly
66     if ((i = av_image_alloc(data, linesize, w, h, link->format, 32)) < 0)
67         return NULL;
68
69     picref = avfilter_get_video_buffer_ref_from_arrays(data, linesize,
70                                                        perms, w, h, link->format);
71     if (!picref) {
72         av_free(data[0]);
73         return NULL;
74     }
75     memset(data[0], 128, i);
76
77     picref->buf->priv = pool;
78     picref->buf->free = NULL;
79
80     return picref;
81 }
82
83 AVFilterBufferRef *avfilter_default_get_audio_buffer(AVFilterLink *link, int perms,
84                                                      int nb_samples)
85 {
86     AVFilterBufferRef *samplesref = NULL;
87     int linesize[8];
88     uint8_t *data[8];
89     int nb_channels = av_get_channel_layout_nb_channels(link->channel_layout);
90
91     /* Calculate total buffer size, round to multiple of 16 to be SIMD friendly */
92     if (av_samples_alloc(data, linesize,
93                          nb_channels, nb_samples, link->format,
94                          16) < 0)
95         return NULL;
96
97     samplesref =
98         avfilter_get_audio_buffer_ref_from_arrays(data, linesize, perms,
99                                                   nb_samples, link->format,
100                                                   link->channel_layout, link->planar);
101     if (!samplesref) {
102         av_free(data[0]);
103         return NULL;
104     }
105
106     return samplesref;
107 }
108
109 void avfilter_default_start_frame(AVFilterLink *inlink, AVFilterBufferRef *picref)
110 {
111     AVFilterLink *outlink = NULL;
112
113     if (inlink->dst->output_count)
114         outlink = inlink->dst->outputs[0];
115
116     if (outlink) {
117         outlink->out_buf = avfilter_get_video_buffer(outlink, AV_PERM_WRITE, outlink->w, outlink->h);
118         avfilter_copy_buffer_ref_props(outlink->out_buf, picref);
119         avfilter_start_frame(outlink, avfilter_ref_buffer(outlink->out_buf, ~0));
120     }
121 }
122
123 void avfilter_default_draw_slice(AVFilterLink *inlink, int y, int h, int slice_dir)
124 {
125     AVFilterLink *outlink = NULL;
126
127     if (inlink->dst->output_count)
128         outlink = inlink->dst->outputs[0];
129
130     if (outlink)
131         avfilter_draw_slice(outlink, y, h, slice_dir);
132 }
133
134 void avfilter_default_end_frame(AVFilterLink *inlink)
135 {
136     AVFilterLink *outlink = NULL;
137
138     if (inlink->dst->output_count)
139         outlink = inlink->dst->outputs[0];
140
141     avfilter_unref_buffer(inlink->cur_buf);
142     inlink->cur_buf = NULL;
143
144     if (outlink) {
145         if (outlink->out_buf) {
146             avfilter_unref_buffer(outlink->out_buf);
147             outlink->out_buf = NULL;
148         }
149         avfilter_end_frame(outlink);
150     }
151 }
152
153 /* FIXME: samplesref is same as link->cur_buf. Need to consider removing the redundant parameter. */
154 void avfilter_default_filter_samples(AVFilterLink *inlink, AVFilterBufferRef *samplesref)
155 {
156     AVFilterLink *outlink = NULL;
157
158     if (inlink->dst->output_count)
159         outlink = inlink->dst->outputs[0];
160
161     if (outlink) {
162         outlink->out_buf = avfilter_default_get_audio_buffer(inlink, AV_PERM_WRITE,
163                                                              samplesref->audio->nb_samples);
164         outlink->out_buf->pts                = samplesref->pts;
165         outlink->out_buf->audio->sample_rate = samplesref->audio->sample_rate;
166         avfilter_filter_samples(outlink, avfilter_ref_buffer(outlink->out_buf, ~0));
167         avfilter_unref_buffer(outlink->out_buf);
168         outlink->out_buf = NULL;
169     }
170     avfilter_unref_buffer(samplesref);
171     inlink->cur_buf = NULL;
172 }
173
174 static void set_common_formats(AVFilterContext *ctx, AVFilterFormats *fmts,
175                                enum AVMediaType type, int offin, int offout)
176 {
177     int i;
178     for (i = 0; i < ctx->input_count; i++)
179         if (ctx->inputs[i] && ctx->inputs[i]->type == type)
180             avfilter_formats_ref(fmts,
181                                  (AVFilterFormats **)((uint8_t *)ctx->inputs[i]+offout));
182
183     for (i = 0; i < ctx->output_count; i++)
184         if (ctx->outputs[i] && ctx->outputs[i]->type == type)
185             avfilter_formats_ref(fmts,
186                                  (AVFilterFormats **)((uint8_t *)ctx->outputs[i]+offin));
187
188     if (!fmts->refcount) {
189         av_free(fmts->formats);
190         av_free(fmts->refs);
191         av_free(fmts);
192     }
193 }
194
195 void avfilter_set_common_pixel_formats(AVFilterContext *ctx, AVFilterFormats *formats)
196 {
197     set_common_formats(ctx, formats, AVMEDIA_TYPE_VIDEO,
198                        offsetof(AVFilterLink, in_formats),
199                        offsetof(AVFilterLink, out_formats));
200 }
201
202 void avfilter_set_common_sample_formats(AVFilterContext *ctx, AVFilterFormats *formats)
203 {
204     set_common_formats(ctx, formats, AVMEDIA_TYPE_AUDIO,
205                        offsetof(AVFilterLink, in_formats),
206                        offsetof(AVFilterLink, out_formats));
207 }
208
209 void avfilter_set_common_channel_layouts(AVFilterContext *ctx, AVFilterFormats *formats)
210 {
211     set_common_formats(ctx, formats, AVMEDIA_TYPE_AUDIO,
212                        offsetof(AVFilterLink, in_chlayouts),
213                        offsetof(AVFilterLink, out_chlayouts));
214 }
215
216 void avfilter_set_common_packing_formats(AVFilterContext *ctx, AVFilterFormats *formats)
217 {
218     set_common_formats(ctx, formats, AVMEDIA_TYPE_AUDIO,
219                        offsetof(AVFilterLink, in_packing),
220                        offsetof(AVFilterLink, out_packing));
221 }
222
223 int avfilter_default_query_formats(AVFilterContext *ctx)
224 {
225     avfilter_set_common_pixel_formats(ctx, avfilter_make_all_formats(AVMEDIA_TYPE_VIDEO));
226     avfilter_set_common_sample_formats(ctx, avfilter_make_all_formats(AVMEDIA_TYPE_AUDIO));
227     avfilter_set_common_channel_layouts(ctx, avfilter_make_all_channel_layouts());
228     avfilter_set_common_packing_formats(ctx, avfilter_make_all_packing_formats());
229
230     return 0;
231 }
232
233 void avfilter_null_start_frame(AVFilterLink *link, AVFilterBufferRef *picref)
234 {
235     avfilter_start_frame(link->dst->outputs[0], picref);
236 }
237
238 void avfilter_null_draw_slice(AVFilterLink *link, int y, int h, int slice_dir)
239 {
240     avfilter_draw_slice(link->dst->outputs[0], y, h, slice_dir);
241 }
242
243 void avfilter_null_end_frame(AVFilterLink *link)
244 {
245     avfilter_end_frame(link->dst->outputs[0]);
246 }
247
248 void avfilter_null_filter_samples(AVFilterLink *link, AVFilterBufferRef *samplesref)
249 {
250     avfilter_filter_samples(link->dst->outputs[0], samplesref);
251 }
252
253 AVFilterBufferRef *avfilter_null_get_video_buffer(AVFilterLink *link, int perms, int w, int h)
254 {
255     return avfilter_get_video_buffer(link->dst->outputs[0], perms, w, h);
256 }
257
258 AVFilterBufferRef *avfilter_null_get_audio_buffer(AVFilterLink *link, int perms,
259                                                   int nb_samples)
260 {
261     return avfilter_get_audio_buffer(link->dst->outputs[0], perms, nb_samples);
262 }
263