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
26 #include "libavutil/avassert.h"
27 #include "libavutil/imgutils.h"
28 #include "libavutil/mem.h"
34 AVFilterBufferRef *ff_null_get_video_buffer(AVFilterLink *link, int perms, int w, int h)
36 return ff_get_video_buffer(link->dst->outputs[0], perms, w, h);
39 AVFilterBufferRef *ff_default_get_video_buffer(AVFilterLink *link, int perms, int w, int h)
44 AVFilterBufferRef *picref = NULL;
45 AVFilterPool *pool = link->pool;
46 int full_perms = AV_PERM_READ | AV_PERM_WRITE | AV_PERM_PRESERVE |
47 AV_PERM_REUSE | AV_PERM_REUSE2 | AV_PERM_ALIGN;
49 av_assert1(!(perms & ~(full_perms | AV_PERM_NEG_LINESIZES)));
52 for (i = 0; i < POOL_SIZE; i++) {
53 picref = pool->pic[i];
54 if (picref && picref->buf->format == link->format && picref->buf->w == w && picref->buf->h == h) {
55 AVFilterBuffer *pic = picref->buf;
58 av_assert0(!picref->video->qp_table);
61 picref->perms = full_perms;
62 picref->format = link->format;
64 memcpy(picref->data, pic->data, sizeof(picref->data));
65 memcpy(picref->linesize, pic->linesize, sizeof(picref->linesize));
71 pool = link->pool = av_mallocz(sizeof(AVFilterPool));
75 // align: +2 is needed for swscaler, +16 to be SIMD-friendly
76 if ((i = av_image_alloc(data, linesize, w, h, link->format, 32)) < 0)
79 picref = avfilter_get_video_buffer_ref_from_arrays(data, linesize,
80 full_perms, w, h, link->format);
86 memset(data[0], 128, i);
88 picref->buf->priv = pool;
89 picref->buf->free = NULL;
96 avfilter_get_video_buffer_ref_from_arrays(uint8_t * const data[4], const int linesize[4], int perms,
97 int w, int h, enum AVPixelFormat format)
99 AVFilterBuffer *pic = av_mallocz(sizeof(AVFilterBuffer));
100 AVFilterBufferRef *picref = av_mallocz(sizeof(AVFilterBufferRef));
106 picref->buf->free = ff_avfilter_default_free_buffer;
107 if (!(picref->video = av_mallocz(sizeof(AVFilterBufferRefVideoProps))))
110 pic->w = picref->video->w = w;
111 pic->h = picref->video->h = h;
113 /* make sure the buffer gets read permission or it's useless for output */
114 picref->perms = perms | AV_PERM_READ;
117 picref->type = AVMEDIA_TYPE_VIDEO;
118 pic->format = picref->format = format;
120 memcpy(pic->data, data, 4*sizeof(data[0]));
121 memcpy(pic->linesize, linesize, 4*sizeof(linesize[0]));
122 memcpy(picref->data, pic->data, sizeof(picref->data));
123 memcpy(picref->linesize, pic->linesize, sizeof(picref->linesize));
125 pic-> extended_data = pic->data;
126 picref->extended_data = picref->data;
128 picref->pts = AV_NOPTS_VALUE;
133 if (picref && picref->video)
134 av_free(picref->video);
140 AVFilterBufferRef *ff_get_video_buffer(AVFilterLink *link, int perms, int w, int h)
142 AVFilterBufferRef *ret = NULL;
144 av_unused char buf[16];
145 FF_TPRINTF_START(NULL, get_video_buffer); ff_tlog_link(NULL, link, 0);
146 ff_tlog(NULL, " perms:%s w:%d h:%d\n", ff_get_ref_perms_string(buf, sizeof(buf), perms), w, h);
148 if (link->dstpad->get_video_buffer)
149 ret = link->dstpad->get_video_buffer(link, perms, w, h);
152 ret = ff_default_get_video_buffer(link, perms, w, h);
155 ret->type = AVMEDIA_TYPE_VIDEO;
157 FF_TPRINTF_START(NULL, get_video_buffer); ff_tlog_link(NULL, link, 0); ff_tlog(NULL, " returning "); ff_tlog_ref(NULL, ret, 1);
162 static int default_start_frame(AVFilterLink *inlink, AVFilterBufferRef *picref)
164 AVFilterLink *outlink = NULL;
166 if (inlink->dstpad->filter_frame)
169 if (inlink->dst->nb_outputs)
170 outlink = inlink->dst->outputs[0];
172 if (outlink && (inlink->dstpad->start_frame || inlink->dstpad->end_frame)) {
173 AVFilterBufferRef *buf_out;
174 outlink->out_buf = ff_get_video_buffer(outlink, AV_PERM_WRITE, outlink->w, outlink->h);
175 if (!outlink->out_buf)
176 return AVERROR(ENOMEM);
178 avfilter_copy_buffer_ref_props(outlink->out_buf, picref);
179 outlink->out_buf->video->w = outlink->w;
180 outlink->out_buf->video->h = outlink->h;
181 buf_out = avfilter_ref_buffer(outlink->out_buf, ~0);
183 return AVERROR(ENOMEM);
185 return ff_start_frame(outlink, buf_out);
190 static void clear_link(AVFilterLink *link)
192 avfilter_unref_bufferp(&link->cur_buf);
193 avfilter_unref_bufferp(&link->src_buf);
194 avfilter_unref_bufferp(&link->out_buf);
195 link->cur_buf_copy = NULL; /* we do not own the reference */
198 /* XXX: should we do the duplicating of the picture ref here, instead of
199 * forcing the source filter to do it? */
200 int ff_start_frame(AVFilterLink *link, AVFilterBufferRef *picref)
202 int (*start_frame)(AVFilterLink *, AVFilterBufferRef *);
203 AVFilterPad *src = link->srcpad;
204 AVFilterPad *dst = link->dstpad;
206 AVFilterCommand *cmd= link->dst->command_queue;
209 FF_TPRINTF_START(NULL, start_frame); ff_tlog_link(NULL, link, 0); ff_tlog(NULL, " "); ff_tlog_ref(NULL, picref, 1);
211 if (strcmp(link->dst->filter->name, "scale")) {
212 av_assert1(picref->format == link->format);
213 av_assert1(picref->video->w == link->w);
214 av_assert1(picref->video->h == link->h);
218 avfilter_unref_buffer(picref);
222 if (!(start_frame = dst->start_frame))
223 start_frame = default_start_frame;
225 av_assert1((picref->perms & src->min_perms) == src->min_perms);
226 picref->perms &= ~ src->rej_perms;
227 perms = picref->perms;
229 if (picref->linesize[0] < 0)
230 perms |= AV_PERM_NEG_LINESIZES;
231 /* prepare to copy the picture if it has insufficient permissions */
232 if ((dst->min_perms & perms) != dst->min_perms || dst->rej_perms & perms) {
233 av_log(link->dst, AV_LOG_DEBUG,
234 "frame copy needed (have perms %x, need %x, reject %x)\n",
236 link->dstpad->min_perms, link->dstpad->rej_perms);
238 link->cur_buf = ff_get_video_buffer(link, dst->min_perms, link->w, link->h);
239 if (!link->cur_buf) {
240 avfilter_unref_bufferp(&picref);
241 return AVERROR(ENOMEM);
244 link->src_buf = picref;
245 avfilter_copy_buffer_ref_props(link->cur_buf, link->src_buf);
247 /* copy palette if required */
248 if (av_pix_fmt_desc_get(link->format)->flags & PIX_FMT_PAL)
249 memcpy(link->cur_buf->data[1], link->src_buf-> data[1], AVPALETTE_SIZE);
252 link->cur_buf = picref;
254 link->cur_buf_copy = link->cur_buf;
256 while(cmd && cmd->time <= picref->pts * av_q2d(link->time_base)){
257 av_log(link->dst, AV_LOG_DEBUG,
258 "Processing command time:%f command:%s arg:%s\n",
259 cmd->time, cmd->command, cmd->arg);
260 avfilter_process_command(link->dst, cmd->command, cmd->arg, 0, 0, cmd->flags);
261 ff_command_queue_pop(link->dst);
262 cmd= link->dst->command_queue;
264 pts = link->cur_buf->pts;
265 ret = start_frame(link, link->cur_buf);
266 ff_update_link_current_pts(link, pts);
270 /* incoming buffers must not be freed in start frame,
271 because they can still be in use by the automatic copy mechanism */
272 av_assert1(link->cur_buf_copy->buf->refcount > 0);
277 static int default_end_frame(AVFilterLink *inlink)
279 AVFilterLink *outlink = NULL;
281 if (inlink->dstpad->filter_frame) {
282 int ret = inlink->dstpad->filter_frame(inlink, inlink->cur_buf);
283 inlink->cur_buf = NULL;
287 if (inlink->dst->nb_outputs)
288 outlink = inlink->dst->outputs[0];
291 if (inlink->dstpad->filter_frame) {
292 int ret = inlink->dstpad->filter_frame(inlink, inlink->cur_buf);
293 inlink->cur_buf = NULL;
295 } else if (inlink->dstpad->start_frame || inlink->dstpad->end_frame){
296 return ff_end_frame(outlink);
298 int ret = ff_filter_frame(outlink, inlink->cur_buf);
299 inlink->cur_buf = NULL;
306 int ff_end_frame(AVFilterLink *link)
308 int (*end_frame)(AVFilterLink *);
311 if (!(end_frame = link->dstpad->end_frame))
312 end_frame = default_end_frame;
314 ret = end_frame(link);
321 int ff_draw_slice(AVFilterLink *link, int y, int h, int slice_dir)
323 uint8_t *src[4], *dst[4];
326 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);
328 /* copy the slice if needed for permission reasons */
330 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(link->format);
331 vsub = desc->log2_chroma_h;
333 for (i = 0; i < 4; i++) {
334 if (link->src_buf->data[i]) {
335 src[i] = link->src_buf-> data[i] +
336 (y >> (i==1 || i==2 ? vsub : 0)) * link->src_buf-> linesize[i];
337 dst[i] = link->cur_buf_copy->data[i] +
338 (y >> (i==1 || i==2 ? vsub : 0)) * link->cur_buf_copy->linesize[i];
340 src[i] = dst[i] = NULL;
343 for (i = 0; i < 4; i++) {
345 av_image_get_linesize(link->format, link->cur_buf_copy->video->w, i);
347 if (!src[i]) continue;
349 for (j = 0; j < h >> (i==1 || i==2 ? vsub : 0); j++) {
350 memcpy(dst[i], src[i], planew);
351 src[i] += link->src_buf->linesize[i];
352 dst[i] += link->cur_buf_copy->linesize[i];
357 /* incoming buffers must not be freed in start frame,
358 because they can still be in use by the automatic copy mechanism */
359 av_assert1(link->cur_buf_copy->buf->refcount > 0);