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 ff_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 *ff_get_video_buffer(AVFilterLink *link, int perms, int w, int h)
132 AVFilterBufferRef *ret = NULL;
134 av_unused char buf[16];
135 FF_TPRINTF_START(NULL, get_video_buffer); ff_tlog_link(NULL, link, 0);
136 ff_tlog(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_TPRINTF_START(NULL, get_video_buffer); ff_tlog_link(NULL, link, 0); ff_tlog(NULL, " returning "); ff_tlog_ref(NULL, ret, 1);
152 int ff_null_start_frame(AVFilterLink *link, AVFilterBufferRef *picref)
154 AVFilterBufferRef *buf_out = avfilter_ref_buffer(picref, ~0);
156 return AVERROR(ENOMEM);
157 return ff_start_frame(link->dst->outputs[0], buf_out);
160 static int default_start_frame(AVFilterLink *inlink, AVFilterBufferRef *picref)
162 AVFilterLink *outlink = NULL;
164 if (inlink->dst->nb_outputs)
165 outlink = inlink->dst->outputs[0];
168 AVFilterBufferRef *buf_out;
169 outlink->out_buf = ff_get_video_buffer(outlink, AV_PERM_WRITE, outlink->w, outlink->h);
170 if (!outlink->out_buf)
171 return AVERROR(ENOMEM);
173 avfilter_copy_buffer_ref_props(outlink->out_buf, picref);
174 outlink->out_buf->video->w = outlink->w;
175 outlink->out_buf->video->h = outlink->h;
176 buf_out = avfilter_ref_buffer(outlink->out_buf, ~0);
178 return AVERROR(ENOMEM);
180 return ff_start_frame(outlink, buf_out);
185 static void clear_link(AVFilterLink *link)
187 avfilter_unref_bufferp(&link->cur_buf);
188 avfilter_unref_bufferp(&link->src_buf);
189 avfilter_unref_bufferp(&link->out_buf);
192 /* XXX: should we do the duplicating of the picture ref here, instead of
193 * forcing the source filter to do it? */
194 int ff_start_frame(AVFilterLink *link, AVFilterBufferRef *picref)
196 int (*start_frame)(AVFilterLink *, AVFilterBufferRef *);
197 AVFilterPad *dst = link->dstpad;
198 int ret, perms = picref->perms;
199 AVFilterCommand *cmd= link->dst->command_queue;
202 FF_TPRINTF_START(NULL, start_frame); ff_tlog_link(NULL, link, 0); ff_tlog(NULL, " "); ff_tlog_ref(NULL, picref, 1);
204 if (!(start_frame = dst->start_frame))
205 start_frame = default_start_frame;
207 if (picref->linesize[0] < 0)
208 perms |= AV_PERM_NEG_LINESIZES;
209 /* prepare to copy the picture if it has insufficient permissions */
210 if ((dst->min_perms & perms) != dst->min_perms || dst->rej_perms & perms) {
211 av_log(link->dst, AV_LOG_DEBUG,
212 "frame copy needed (have perms %x, need %x, reject %x)\n",
214 link->dstpad->min_perms, link->dstpad->rej_perms);
216 link->cur_buf = ff_get_video_buffer(link, dst->min_perms, link->w, link->h);
217 if (!link->cur_buf) {
218 avfilter_unref_bufferp(&picref);
219 return AVERROR(ENOMEM);
222 link->src_buf = picref;
223 avfilter_copy_buffer_ref_props(link->cur_buf, link->src_buf);
225 /* copy palette if required */
226 if (av_pix_fmt_descriptors[link->format].flags & PIX_FMT_PAL)
227 memcpy(link->cur_buf->data[1], link->src_buf-> data[1], AVPALETTE_SIZE);
230 link->cur_buf = picref;
232 while(cmd && cmd->time <= picref->pts * av_q2d(link->time_base)){
233 av_log(link->dst, AV_LOG_DEBUG,
234 "Processing command time:%f command:%s arg:%s\n",
235 cmd->time, cmd->command, cmd->arg);
236 avfilter_process_command(link->dst, cmd->command, cmd->arg, 0, 0, cmd->flags);
237 ff_command_queue_pop(link->dst);
238 cmd= link->dst->command_queue;
240 pts = link->cur_buf->pts;
241 ret = start_frame(link, link->cur_buf);
242 ff_update_link_current_pts(link,link->cur_buf ? link->cur_buf->pts : pts);
249 int ff_null_start_frame_keep_ref(AVFilterLink *inlink,
250 AVFilterBufferRef *picref)
252 return ff_start_frame(inlink->dst->outputs[0], avfilter_ref_buffer(picref, ~0));
255 int ff_null_end_frame(AVFilterLink *link)
257 return ff_end_frame(link->dst->outputs[0]);
260 static int default_end_frame(AVFilterLink *inlink)
262 AVFilterLink *outlink = NULL;
264 if (inlink->dst->nb_outputs)
265 outlink = inlink->dst->outputs[0];
268 return ff_end_frame(outlink);
273 int ff_end_frame(AVFilterLink *link)
275 int (*end_frame)(AVFilterLink *);
278 if (!(end_frame = link->dstpad->end_frame))
279 end_frame = default_end_frame;
281 ret = end_frame(link);
288 int ff_null_draw_slice(AVFilterLink *link, int y, int h, int slice_dir)
290 return ff_draw_slice(link->dst->outputs[0], y, h, slice_dir);
293 static int default_draw_slice(AVFilterLink *inlink, int y, int h, int slice_dir)
295 AVFilterLink *outlink = NULL;
297 if (inlink->dst->nb_outputs)
298 outlink = inlink->dst->outputs[0];
301 return ff_draw_slice(outlink, y, h, slice_dir);
305 int ff_draw_slice(AVFilterLink *link, int y, int h, int slice_dir)
307 uint8_t *src[4], *dst[4];
309 int (*draw_slice)(AVFilterLink *, int, int, int);
311 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);
313 /* copy the slice if needed for permission reasons */
315 vsub = av_pix_fmt_descriptors[link->format].log2_chroma_h;
317 for (i = 0; i < 4; i++) {
318 if (link->src_buf->data[i]) {
319 src[i] = link->src_buf-> data[i] +
320 (y >> (i==1 || i==2 ? vsub : 0)) * link->src_buf-> linesize[i];
321 dst[i] = link->cur_buf->data[i] +
322 (y >> (i==1 || i==2 ? vsub : 0)) * link->cur_buf->linesize[i];
324 src[i] = dst[i] = NULL;
327 for (i = 0; i < 4; i++) {
329 av_image_get_linesize(link->format, link->cur_buf->video->w, i);
331 if (!src[i]) continue;
333 for (j = 0; j < h >> (i==1 || i==2 ? vsub : 0); j++) {
334 memcpy(dst[i], src[i], planew);
335 src[i] += link->src_buf->linesize[i];
336 dst[i] += link->cur_buf->linesize[i];
341 if (!(draw_slice = link->dstpad->draw_slice))
342 draw_slice = default_draw_slice;
343 ret = draw_slice(link, y, h, slice_dir);
349 int avfilter_default_end_frame(AVFilterLink *inlink)
351 return default_end_frame(inlink);