2 * Copyright 2007 Bobby Bingham
3 * Copyright Stefano Sabatini <stefasab gmail com>
4 * Copyright Vitor Sessak <vitor1001 gmail com>
6 * This file is part of FFmpeg.
8 * FFmpeg is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * FFmpeg is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with FFmpeg; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 #include "libavutil/imgutils.h"
29 AVFilterBufferRef *ff_null_get_video_buffer(AVFilterLink *link, int perms, int w, int h)
31 return avfilter_get_video_buffer(link->dst->outputs[0], perms, w, h);
34 AVFilterBufferRef *ff_default_get_video_buffer(AVFilterLink *link, int perms, int w, int h)
39 AVFilterBufferRef *picref = NULL;
40 AVFilterPool *pool = link->pool;
43 for (i = 0; i < POOL_SIZE; i++) {
44 picref = pool->pic[i];
45 if (picref && picref->buf->format == link->format && picref->buf->w == w && picref->buf->h == h) {
46 AVFilterBuffer *pic = picref->buf;
51 picref->perms = perms | AV_PERM_READ;
52 picref->format = link->format;
54 memcpy(picref->data, pic->data, sizeof(picref->data));
55 memcpy(picref->linesize, pic->linesize, sizeof(picref->linesize));
61 pool = link->pool = av_mallocz(sizeof(AVFilterPool));
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)
69 picref = avfilter_get_video_buffer_ref_from_arrays(data, linesize,
70 perms, w, h, link->format);
76 memset(data[0], 128, i);
78 picref->buf->priv = pool;
79 picref->buf->free = NULL;
86 avfilter_get_video_buffer_ref_from_arrays(uint8_t * const data[4], const int linesize[4], int perms,
87 int w, int h, enum PixelFormat format)
89 AVFilterBuffer *pic = av_mallocz(sizeof(AVFilterBuffer));
90 AVFilterBufferRef *picref = av_mallocz(sizeof(AVFilterBufferRef));
96 picref->buf->free = ff_avfilter_default_free_buffer;
97 if (!(picref->video = av_mallocz(sizeof(AVFilterBufferRefVideoProps))))
100 pic->w = picref->video->w = w;
101 pic->h = picref->video->h = h;
103 /* make sure the buffer gets read permission or it's useless for output */
104 picref->perms = perms | AV_PERM_READ;
107 picref->type = AVMEDIA_TYPE_VIDEO;
108 pic->format = picref->format = format;
110 memcpy(pic->data, data, 4*sizeof(data[0]));
111 memcpy(pic->linesize, linesize, 4*sizeof(linesize[0]));
112 memcpy(picref->data, pic->data, sizeof(picref->data));
113 memcpy(picref->linesize, pic->linesize, sizeof(picref->linesize));
115 pic-> extended_data = pic->data;
116 picref->extended_data = picref->data;
118 picref->pts = AV_NOPTS_VALUE;
123 if (picref && picref->video)
124 av_free(picref->video);
130 AVFilterBufferRef *avfilter_get_video_buffer(AVFilterLink *link, int perms, int w, int h)
132 AVFilterBufferRef *ret = NULL;
134 av_unused char buf[16];
135 FF_DPRINTF_START(NULL, get_video_buffer); ff_dlog_link(NULL, link, 0);
136 av_dlog(NULL, " perms:%s w:%d h:%d\n", ff_get_ref_perms_string(buf, sizeof(buf), perms), w, h);
138 if (link->dstpad->get_video_buffer)
139 ret = link->dstpad->get_video_buffer(link, perms, w, h);
142 ret = ff_default_get_video_buffer(link, perms, w, h);
145 ret->type = AVMEDIA_TYPE_VIDEO;
147 FF_DPRINTF_START(NULL, get_video_buffer); ff_dlog_link(NULL, link, 0); av_dlog(NULL, " returning "); ff_dlog_ref(NULL, ret, 1);
152 void ff_null_start_frame(AVFilterLink *link, AVFilterBufferRef *picref)
154 avfilter_start_frame(link->dst->outputs[0], picref);
157 static void default_start_frame(AVFilterLink *inlink, AVFilterBufferRef *picref)
159 AVFilterLink *outlink = NULL;
161 if (inlink->dst->output_count)
162 outlink = inlink->dst->outputs[0];
165 outlink->out_buf = avfilter_get_video_buffer(outlink, AV_PERM_WRITE, outlink->w, outlink->h);
166 avfilter_copy_buffer_ref_props(outlink->out_buf, picref);
167 avfilter_start_frame(outlink, avfilter_ref_buffer(outlink->out_buf, ~0));
171 /* XXX: should we do the duplicating of the picture ref here, instead of
172 * forcing the source filter to do it? */
173 void avfilter_start_frame(AVFilterLink *link, AVFilterBufferRef *picref)
175 void (*start_frame)(AVFilterLink *, AVFilterBufferRef *);
176 AVFilterPad *dst = link->dstpad;
177 int perms = picref->perms;
178 AVFilterCommand *cmd= link->dst->command_queue;
180 FF_DPRINTF_START(NULL, start_frame); ff_dlog_link(NULL, link, 0); av_dlog(NULL, " "); ff_dlog_ref(NULL, picref, 1);
182 if (!(start_frame = dst->start_frame))
183 start_frame = default_start_frame;
185 if (picref->linesize[0] < 0)
186 perms |= AV_PERM_NEG_LINESIZES;
187 /* prepare to copy the picture if it has insufficient permissions */
188 if ((dst->min_perms & perms) != dst->min_perms || dst->rej_perms & perms) {
189 av_log(link->dst, AV_LOG_DEBUG,
190 "frame copy needed (have perms %x, need %x, reject %x)\n",
192 link->dstpad->min_perms, link->dstpad->rej_perms);
194 link->cur_buf = avfilter_get_video_buffer(link, dst->min_perms, link->w, link->h);
195 link->src_buf = picref;
196 avfilter_copy_buffer_ref_props(link->cur_buf, link->src_buf);
198 /* copy palette if required */
199 if (av_pix_fmt_descriptors[link->format].flags & PIX_FMT_PAL)
200 memcpy(link->cur_buf->data[1], link->src_buf-> data[1], AVPALETTE_SIZE);
203 link->cur_buf = picref;
205 while(cmd && cmd->time <= picref->pts * av_q2d(link->time_base)){
206 av_log(link->dst, AV_LOG_DEBUG,
207 "Processing command time:%f command:%s arg:%s\n",
208 cmd->time, cmd->command, cmd->arg);
209 avfilter_process_command(link->dst, cmd->command, cmd->arg, 0, 0, cmd->flags);
210 ff_command_queue_pop(link->dst);
211 cmd= link->dst->command_queue;
214 start_frame(link, link->cur_buf);
215 ff_update_link_current_pts(link, link->cur_buf->pts);
218 void ff_null_end_frame(AVFilterLink *link)
220 avfilter_end_frame(link->dst->outputs[0]);
223 static void default_end_frame(AVFilterLink *inlink)
225 AVFilterLink *outlink = NULL;
227 if (inlink->dst->output_count)
228 outlink = inlink->dst->outputs[0];
230 avfilter_unref_buffer(inlink->cur_buf);
231 inlink->cur_buf = NULL;
234 if (outlink->out_buf) {
235 avfilter_unref_buffer(outlink->out_buf);
236 outlink->out_buf = NULL;
238 avfilter_end_frame(outlink);
242 void avfilter_end_frame(AVFilterLink *link)
244 void (*end_frame)(AVFilterLink *);
246 if (!(end_frame = link->dstpad->end_frame))
247 end_frame = default_end_frame;
251 /* unreference the source picture if we're feeding the destination filter
252 * a copied version dues to permission issues */
254 avfilter_unref_buffer(link->src_buf);
255 link->src_buf = NULL;
259 void ff_null_draw_slice(AVFilterLink *link, int y, int h, int slice_dir)
261 avfilter_draw_slice(link->dst->outputs[0], y, h, slice_dir);
264 static void default_draw_slice(AVFilterLink *inlink, int y, int h, int slice_dir)
266 AVFilterLink *outlink = NULL;
268 if (inlink->dst->output_count)
269 outlink = inlink->dst->outputs[0];
272 avfilter_draw_slice(outlink, y, h, slice_dir);
275 void avfilter_draw_slice(AVFilterLink *link, int y, int h, int slice_dir)
277 uint8_t *src[4], *dst[4];
279 void (*draw_slice)(AVFilterLink *, int, int, int);
281 FF_DPRINTF_START(NULL, draw_slice); ff_dlog_link(NULL, link, 0); av_dlog(NULL, " y:%d h:%d dir:%d\n", y, h, slice_dir);
283 /* copy the slice if needed for permission reasons */
285 vsub = av_pix_fmt_descriptors[link->format].log2_chroma_h;
287 for (i = 0; i < 4; i++) {
288 if (link->src_buf->data[i]) {
289 src[i] = link->src_buf-> data[i] +
290 (y >> (i==1 || i==2 ? vsub : 0)) * link->src_buf-> linesize[i];
291 dst[i] = link->cur_buf->data[i] +
292 (y >> (i==1 || i==2 ? vsub : 0)) * link->cur_buf->linesize[i];
294 src[i] = dst[i] = NULL;
297 for (i = 0; i < 4; i++) {
299 av_image_get_linesize(link->format, link->cur_buf->video->w, i);
301 if (!src[i]) continue;
303 for (j = 0; j < h >> (i==1 || i==2 ? vsub : 0); j++) {
304 memcpy(dst[i], src[i], planew);
305 src[i] += link->src_buf->linesize[i];
306 dst[i] += link->cur_buf->linesize[i];
311 if (!(draw_slice = link->dstpad->draw_slice))
312 draw_slice = default_draw_slice;
313 draw_slice(link, y, h, slice_dir);
316 #if FF_API_FILTERS_PUBLIC
317 AVFilterBufferRef *avfilter_default_get_video_buffer(AVFilterLink *link, int perms, int w, int h)
319 return ff_default_get_video_buffer(link, perms, w, h);
321 void avfilter_default_start_frame(AVFilterLink *inlink, AVFilterBufferRef *picref)
323 default_start_frame(inlink, picref);
325 void avfilter_default_end_frame(AVFilterLink *inlink)
327 default_end_frame(inlink);
329 void avfilter_default_draw_slice(AVFilterLink *inlink, int y, int h, int slice_dir)
331 default_draw_slice(inlink, y, h, slice_dir);
333 AVFilterBufferRef *avfilter_null_get_video_buffer(AVFilterLink *link, int perms, int w, int h)
335 return ff_null_get_video_buffer(link, perms, w, h);
337 void avfilter_null_start_frame(AVFilterLink *link, AVFilterBufferRef *picref)
339 ff_null_start_frame(link, picref);
341 void avfilter_null_end_frame(AVFilterLink *link)
343 ff_null_end_frame(link);
345 void avfilter_null_draw_slice(AVFilterLink *link, int y, int h, int slice_dir)
347 ff_null_draw_slice(link, y, h, slice_dir);