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/avassert.h"
24 #include "libavutil/imgutils.h"
30 AVFilterBufferRef *ff_null_get_video_buffer(AVFilterLink *link, int perms, int w, int h)
32 return ff_get_video_buffer(link->dst->outputs[0], perms, w, h);
35 AVFilterBufferRef *ff_default_get_video_buffer(AVFilterLink *link, int perms, int w, int h)
40 AVFilterBufferRef *picref = NULL;
41 AVFilterPool *pool = link->pool;
44 for (i = 0; i < POOL_SIZE; i++) {
45 picref = pool->pic[i];
46 if (picref && picref->buf->format == link->format && picref->buf->w == w && picref->buf->h == h) {
47 AVFilterBuffer *pic = picref->buf;
52 picref->perms = perms | AV_PERM_READ;
53 picref->format = link->format;
55 memcpy(picref->data, pic->data, sizeof(picref->data));
56 memcpy(picref->linesize, pic->linesize, sizeof(picref->linesize));
62 pool = link->pool = av_mallocz(sizeof(AVFilterPool));
66 // align: +2 is needed for swscaler, +16 to be SIMD-friendly
67 if ((i = av_image_alloc(data, linesize, w, h, link->format, 32)) < 0)
70 picref = avfilter_get_video_buffer_ref_from_arrays(data, linesize,
71 perms, w, h, link->format);
77 memset(data[0], 128, i);
79 picref->buf->priv = pool;
80 picref->buf->free = NULL;
87 avfilter_get_video_buffer_ref_from_arrays(uint8_t * const data[4], const int linesize[4], int perms,
88 int w, int h, enum PixelFormat format)
90 AVFilterBuffer *pic = av_mallocz(sizeof(AVFilterBuffer));
91 AVFilterBufferRef *picref = av_mallocz(sizeof(AVFilterBufferRef));
97 picref->buf->free = ff_avfilter_default_free_buffer;
98 if (!(picref->video = av_mallocz(sizeof(AVFilterBufferRefVideoProps))))
101 pic->w = picref->video->w = w;
102 pic->h = picref->video->h = h;
104 /* make sure the buffer gets read permission or it's useless for output */
105 picref->perms = perms | AV_PERM_READ;
108 picref->type = AVMEDIA_TYPE_VIDEO;
109 pic->format = picref->format = format;
111 memcpy(pic->data, data, 4*sizeof(data[0]));
112 memcpy(pic->linesize, linesize, 4*sizeof(linesize[0]));
113 memcpy(picref->data, pic->data, sizeof(picref->data));
114 memcpy(picref->linesize, pic->linesize, sizeof(picref->linesize));
116 pic-> extended_data = pic->data;
117 picref->extended_data = picref->data;
119 picref->pts = AV_NOPTS_VALUE;
124 if (picref && picref->video)
125 av_free(picref->video);
131 AVFilterBufferRef *ff_get_video_buffer(AVFilterLink *link, int perms, int w, int h)
133 AVFilterBufferRef *ret = NULL;
135 av_unused char buf[16];
136 FF_TPRINTF_START(NULL, get_video_buffer); ff_tlog_link(NULL, link, 0);
137 ff_tlog(NULL, " perms:%s w:%d h:%d\n", ff_get_ref_perms_string(buf, sizeof(buf), perms), w, h);
139 if (link->dstpad->get_video_buffer)
140 ret = link->dstpad->get_video_buffer(link, perms, w, h);
143 ret = ff_default_get_video_buffer(link, perms, w, h);
146 ret->type = AVMEDIA_TYPE_VIDEO;
148 FF_TPRINTF_START(NULL, get_video_buffer); ff_tlog_link(NULL, link, 0); ff_tlog(NULL, " returning "); ff_tlog_ref(NULL, ret, 1);
153 int ff_null_start_frame(AVFilterLink *link, AVFilterBufferRef *picref)
155 AVFilterBufferRef *buf_out = avfilter_ref_buffer(picref, ~0);
157 return AVERROR(ENOMEM);
158 return ff_start_frame(link->dst->outputs[0], buf_out);
161 // for filters that support (but don't require) outpic==inpic
162 int ff_inplace_start_frame(AVFilterLink *inlink, AVFilterBufferRef *inpicref)
164 AVFilterLink *outlink = inlink->dst->outputs[0];
165 AVFilterBufferRef *outpicref = NULL, *for_next_filter;
168 if ((inpicref->perms & AV_PERM_WRITE) && !(inpicref->perms & AV_PERM_PRESERVE)) {
169 outpicref = avfilter_ref_buffer(inpicref, ~0);
171 return AVERROR(ENOMEM);
173 outpicref = ff_get_video_buffer(outlink, AV_PERM_WRITE, outlink->w, outlink->h);
175 return AVERROR(ENOMEM);
177 avfilter_copy_buffer_ref_props(outpicref, inpicref);
178 outpicref->video->w = outlink->w;
179 outpicref->video->h = outlink->h;
182 for_next_filter = avfilter_ref_buffer(outpicref, ~0);
184 ret = ff_start_frame(outlink, for_next_filter);
186 ret = AVERROR(ENOMEM);
189 avfilter_unref_bufferp(&outpicref);
193 outlink->out_buf = outpicref;
197 static int default_start_frame(AVFilterLink *inlink, AVFilterBufferRef *picref)
199 AVFilterLink *outlink = NULL;
201 if (inlink->dst->nb_outputs)
202 outlink = inlink->dst->outputs[0];
205 AVFilterBufferRef *buf_out;
206 outlink->out_buf = ff_get_video_buffer(outlink, AV_PERM_WRITE, outlink->w, outlink->h);
207 if (!outlink->out_buf)
208 return AVERROR(ENOMEM);
210 avfilter_copy_buffer_ref_props(outlink->out_buf, picref);
211 outlink->out_buf->video->w = outlink->w;
212 outlink->out_buf->video->h = outlink->h;
213 buf_out = avfilter_ref_buffer(outlink->out_buf, ~0);
215 return AVERROR(ENOMEM);
217 return ff_start_frame(outlink, buf_out);
222 static void clear_link(AVFilterLink *link)
224 avfilter_unref_bufferp(&link->cur_buf);
225 avfilter_unref_bufferp(&link->src_buf);
226 avfilter_unref_bufferp(&link->out_buf);
227 link->cur_buf_copy = NULL; /* we do not own the reference */
230 /* XXX: should we do the duplicating of the picture ref here, instead of
231 * forcing the source filter to do it? */
232 int ff_start_frame(AVFilterLink *link, AVFilterBufferRef *picref)
234 int (*start_frame)(AVFilterLink *, AVFilterBufferRef *);
235 AVFilterPad *dst = link->dstpad;
236 int ret, perms = picref->perms;
237 AVFilterCommand *cmd= link->dst->command_queue;
240 FF_TPRINTF_START(NULL, start_frame); ff_tlog_link(NULL, link, 0); ff_tlog(NULL, " "); ff_tlog_ref(NULL, picref, 1);
242 if (!(start_frame = dst->start_frame))
243 start_frame = default_start_frame;
245 if (picref->linesize[0] < 0)
246 perms |= AV_PERM_NEG_LINESIZES;
247 /* prepare to copy the picture if it has insufficient permissions */
248 if ((dst->min_perms & perms) != dst->min_perms || dst->rej_perms & perms) {
249 av_log(link->dst, AV_LOG_DEBUG,
250 "frame copy needed (have perms %x, need %x, reject %x)\n",
252 link->dstpad->min_perms, link->dstpad->rej_perms);
254 link->cur_buf = ff_get_video_buffer(link, dst->min_perms, link->w, link->h);
255 if (!link->cur_buf) {
256 avfilter_unref_bufferp(&picref);
257 return AVERROR(ENOMEM);
260 link->src_buf = picref;
261 avfilter_copy_buffer_ref_props(link->cur_buf, link->src_buf);
263 /* copy palette if required */
264 if (av_pix_fmt_descriptors[link->format].flags & PIX_FMT_PAL)
265 memcpy(link->cur_buf->data[1], link->src_buf-> data[1], AVPALETTE_SIZE);
268 link->cur_buf = picref;
270 link->cur_buf_copy = link->cur_buf;
272 while(cmd && cmd->time <= picref->pts * av_q2d(link->time_base)){
273 av_log(link->dst, AV_LOG_DEBUG,
274 "Processing command time:%f command:%s arg:%s\n",
275 cmd->time, cmd->command, cmd->arg);
276 avfilter_process_command(link->dst, cmd->command, cmd->arg, 0, 0, cmd->flags);
277 ff_command_queue_pop(link->dst);
278 cmd= link->dst->command_queue;
280 pts = link->cur_buf->pts;
281 ret = start_frame(link, link->cur_buf);
282 ff_update_link_current_pts(link, pts);
286 /* incoming buffers must not be freed in start frame,
287 because they can still be in use by the automatic copy mechanism */
288 av_assert1(link->cur_buf_copy->buf->refcount > 0);
293 int ff_null_end_frame(AVFilterLink *link)
295 return ff_end_frame(link->dst->outputs[0]);
298 static int default_end_frame(AVFilterLink *inlink)
300 AVFilterLink *outlink = NULL;
302 if (inlink->dst->nb_outputs)
303 outlink = inlink->dst->outputs[0];
306 return ff_end_frame(outlink);
311 int ff_end_frame(AVFilterLink *link)
313 int (*end_frame)(AVFilterLink *);
316 if (!(end_frame = link->dstpad->end_frame))
317 end_frame = default_end_frame;
319 ret = end_frame(link);
326 int ff_null_draw_slice(AVFilterLink *link, int y, int h, int slice_dir)
328 return ff_draw_slice(link->dst->outputs[0], y, h, slice_dir);
331 static int default_draw_slice(AVFilterLink *inlink, int y, int h, int slice_dir)
333 AVFilterLink *outlink = NULL;
335 if (inlink->dst->nb_outputs)
336 outlink = inlink->dst->outputs[0];
339 return ff_draw_slice(outlink, y, h, slice_dir);
343 int ff_draw_slice(AVFilterLink *link, int y, int h, int slice_dir)
345 uint8_t *src[4], *dst[4];
347 int (*draw_slice)(AVFilterLink *, int, int, int);
349 FF_TPRINTF_START(NULL, draw_slice); ff_tlog_link(NULL, link, 0); ff_tlog(NULL, " y:%d h:%d dir:%d\n", y, h, slice_dir);
351 /* copy the slice if needed for permission reasons */
353 vsub = av_pix_fmt_descriptors[link->format].log2_chroma_h;
355 for (i = 0; i < 4; i++) {
356 if (link->src_buf->data[i]) {
357 src[i] = link->src_buf-> data[i] +
358 (y >> (i==1 || i==2 ? vsub : 0)) * link->src_buf-> linesize[i];
359 dst[i] = link->cur_buf_copy->data[i] +
360 (y >> (i==1 || i==2 ? vsub : 0)) * link->cur_buf_copy->linesize[i];
362 src[i] = dst[i] = NULL;
365 for (i = 0; i < 4; i++) {
367 av_image_get_linesize(link->format, link->cur_buf_copy->video->w, i);
369 if (!src[i]) continue;
371 for (j = 0; j < h >> (i==1 || i==2 ? vsub : 0); j++) {
372 memcpy(dst[i], src[i], planew);
373 src[i] += link->src_buf->linesize[i];
374 dst[i] += link->cur_buf_copy->linesize[i];
379 if (!(draw_slice = link->dstpad->draw_slice))
380 draw_slice = default_draw_slice;
381 ret = draw_slice(link, y, h, slice_dir);
385 /* incoming buffers must not be freed in start frame,
386 because they can still be in use by the automatic copy mechanism */
387 av_assert1(link->cur_buf_copy->buf->refcount > 0);
391 int avfilter_default_end_frame(AVFilterLink *inlink)
393 return default_end_frame(inlink);