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 static char *ff_get_ref_perms_string(char *buf, size_t buf_size, int perms)
31 snprintf(buf, buf_size, "%s%s%s%s%s%s",
32 perms & AV_PERM_READ ? "r" : "",
33 perms & AV_PERM_WRITE ? "w" : "",
34 perms & AV_PERM_PRESERVE ? "p" : "",
35 perms & AV_PERM_REUSE ? "u" : "",
36 perms & AV_PERM_REUSE2 ? "U" : "",
37 perms & AV_PERM_NEG_LINESIZES ? "n" : "");
41 static void ff_dlog_ref(void *ctx, AVFilterBufferRef *ref, int end)
43 av_unused char buf[16];
45 "ref[%p buf:%p refcount:%d perms:%s data:%p linesize[%d, %d, %d, %d] pts:%"PRId64" pos:%"PRId64,
46 ref, ref->buf, ref->buf->refcount, ff_get_ref_perms_string(buf, sizeof(buf), ref->perms), ref->data[0],
47 ref->linesize[0], ref->linesize[1], ref->linesize[2], ref->linesize[3],
51 av_dlog(ctx, " a:%d/%d s:%dx%d i:%c iskey:%d type:%c",
52 ref->video->sample_aspect_ratio.num, ref->video->sample_aspect_ratio.den,
53 ref->video->w, ref->video->h,
54 !ref->video->interlaced ? 'P' : /* Progressive */
55 ref->video->top_field_first ? 'T' : 'B', /* Top / Bottom */
56 ref->video->key_frame,
57 av_get_picture_type_char(ref->video->pict_type));
60 av_dlog(ctx, " cl:%"PRId64"d n:%d r:%d",
61 ref->audio->channel_layout,
62 ref->audio->nb_samples,
63 ref->audio->sample_rate);
66 av_dlog(ctx, "]%s", end ? "\n" : "");
69 AVFilterBufferRef *ff_null_get_video_buffer(AVFilterLink *link, int perms, int w, int h)
71 return avfilter_get_video_buffer(link->dst->outputs[0], perms, w, h);
74 AVFilterBufferRef *ff_default_get_video_buffer(AVFilterLink *link, int perms, int w, int h)
79 AVFilterBufferRef *picref = NULL;
80 AVFilterPool *pool = link->pool;
83 for (i = 0; i < POOL_SIZE; i++) {
84 picref = pool->pic[i];
85 if (picref && picref->buf->format == link->format && picref->buf->w == w && picref->buf->h == h) {
86 AVFilterBuffer *pic = picref->buf;
91 picref->perms = perms | AV_PERM_READ;
92 picref->format = link->format;
94 memcpy(picref->data, pic->data, sizeof(picref->data));
95 memcpy(picref->linesize, pic->linesize, sizeof(picref->linesize));
101 pool = link->pool = av_mallocz(sizeof(AVFilterPool));
105 // align: +2 is needed for swscaler, +16 to be SIMD-friendly
106 if ((i = av_image_alloc(data, linesize, w, h, link->format, 32)) < 0)
109 picref = avfilter_get_video_buffer_ref_from_arrays(data, linesize,
110 perms, w, h, link->format);
116 memset(data[0], 128, i);
118 picref->buf->priv = pool;
119 picref->buf->free = NULL;
126 avfilter_get_video_buffer_ref_from_arrays(uint8_t * const data[4], const int linesize[4], int perms,
127 int w, int h, enum PixelFormat format)
129 AVFilterBuffer *pic = av_mallocz(sizeof(AVFilterBuffer));
130 AVFilterBufferRef *picref = av_mallocz(sizeof(AVFilterBufferRef));
136 picref->buf->free = ff_avfilter_default_free_buffer;
137 if (!(picref->video = av_mallocz(sizeof(AVFilterBufferRefVideoProps))))
140 pic->w = picref->video->w = w;
141 pic->h = picref->video->h = h;
143 /* make sure the buffer gets read permission or it's useless for output */
144 picref->perms = perms | AV_PERM_READ;
147 picref->type = AVMEDIA_TYPE_VIDEO;
148 pic->format = picref->format = format;
150 memcpy(pic->data, data, 4*sizeof(data[0]));
151 memcpy(pic->linesize, linesize, 4*sizeof(linesize[0]));
152 memcpy(picref->data, pic->data, sizeof(picref->data));
153 memcpy(picref->linesize, pic->linesize, sizeof(picref->linesize));
155 pic-> extended_data = pic->data;
156 picref->extended_data = picref->data;
158 picref->pts = AV_NOPTS_VALUE;
163 if (picref && picref->video)
164 av_free(picref->video);
170 AVFilterBufferRef *avfilter_get_video_buffer(AVFilterLink *link, int perms, int w, int h)
172 AVFilterBufferRef *ret = NULL;
174 av_unused char buf[16];
175 FF_DPRINTF_START(NULL, get_video_buffer); ff_dlog_link(NULL, link, 0);
176 av_dlog(NULL, " perms:%s w:%d h:%d\n", ff_get_ref_perms_string(buf, sizeof(buf), perms), w, h);
178 if (link->dstpad->get_video_buffer)
179 ret = link->dstpad->get_video_buffer(link, perms, w, h);
182 ret = ff_default_get_video_buffer(link, perms, w, h);
185 ret->type = AVMEDIA_TYPE_VIDEO;
187 FF_DPRINTF_START(NULL, get_video_buffer); ff_dlog_link(NULL, link, 0); av_dlog(NULL, " returning "); ff_dlog_ref(NULL, ret, 1);
192 void ff_null_start_frame(AVFilterLink *link, AVFilterBufferRef *picref)
194 avfilter_start_frame(link->dst->outputs[0], picref);
197 static void default_start_frame(AVFilterLink *inlink, AVFilterBufferRef *picref)
199 AVFilterLink *outlink = NULL;
201 if (inlink->dst->output_count)
202 outlink = inlink->dst->outputs[0];
205 outlink->out_buf = avfilter_get_video_buffer(outlink, AV_PERM_WRITE, outlink->w, outlink->h);
206 avfilter_copy_buffer_ref_props(outlink->out_buf, picref);
207 avfilter_start_frame(outlink, avfilter_ref_buffer(outlink->out_buf, ~0));
211 /* XXX: should we do the duplicating of the picture ref here, instead of
212 * forcing the source filter to do it? */
213 void avfilter_start_frame(AVFilterLink *link, AVFilterBufferRef *picref)
215 void (*start_frame)(AVFilterLink *, AVFilterBufferRef *);
216 AVFilterPad *dst = link->dstpad;
217 int perms = picref->perms;
218 AVFilterCommand *cmd= link->dst->command_queue;
220 FF_DPRINTF_START(NULL, start_frame); ff_dlog_link(NULL, link, 0); av_dlog(NULL, " "); ff_dlog_ref(NULL, picref, 1);
222 if (!(start_frame = dst->start_frame))
223 start_frame = default_start_frame;
225 if (picref->linesize[0] < 0)
226 perms |= AV_PERM_NEG_LINESIZES;
227 /* prepare to copy the picture if it has insufficient permissions */
228 if ((dst->min_perms & perms) != dst->min_perms || dst->rej_perms & perms) {
229 av_log(link->dst, AV_LOG_DEBUG,
230 "frame copy needed (have perms %x, need %x, reject %x)\n",
232 link->dstpad->min_perms, link->dstpad->rej_perms);
234 link->cur_buf = avfilter_get_video_buffer(link, dst->min_perms, link->w, link->h);
235 link->src_buf = picref;
236 avfilter_copy_buffer_ref_props(link->cur_buf, link->src_buf);
238 /* copy palette if required */
239 if (av_pix_fmt_descriptors[link->format].flags & PIX_FMT_PAL)
240 memcpy(link->cur_buf->data[1], link->src_buf-> data[1], AVPALETTE_SIZE);
243 link->cur_buf = picref;
245 while(cmd && cmd->time <= picref->pts * av_q2d(link->time_base)){
246 av_log(link->dst, AV_LOG_DEBUG,
247 "Processing command time:%f command:%s arg:%s\n",
248 cmd->time, cmd->command, cmd->arg);
249 avfilter_process_command(link->dst, cmd->command, cmd->arg, 0, 0, cmd->flags);
250 ff_command_queue_pop(link->dst);
251 cmd= link->dst->command_queue;
254 start_frame(link, link->cur_buf);
255 ff_update_link_current_pts(link, link->cur_buf->pts);
258 void ff_null_end_frame(AVFilterLink *link)
260 avfilter_end_frame(link->dst->outputs[0]);
263 static void default_end_frame(AVFilterLink *inlink)
265 AVFilterLink *outlink = NULL;
267 if (inlink->dst->output_count)
268 outlink = inlink->dst->outputs[0];
270 avfilter_unref_buffer(inlink->cur_buf);
271 inlink->cur_buf = NULL;
274 if (outlink->out_buf) {
275 avfilter_unref_buffer(outlink->out_buf);
276 outlink->out_buf = NULL;
278 avfilter_end_frame(outlink);
282 void avfilter_end_frame(AVFilterLink *link)
284 void (*end_frame)(AVFilterLink *);
286 if (!(end_frame = link->dstpad->end_frame))
287 end_frame = default_end_frame;
291 /* unreference the source picture if we're feeding the destination filter
292 * a copied version dues to permission issues */
294 avfilter_unref_buffer(link->src_buf);
295 link->src_buf = NULL;
299 void ff_null_draw_slice(AVFilterLink *link, int y, int h, int slice_dir)
301 avfilter_draw_slice(link->dst->outputs[0], y, h, slice_dir);
304 static void default_draw_slice(AVFilterLink *inlink, int y, int h, int slice_dir)
306 AVFilterLink *outlink = NULL;
308 if (inlink->dst->output_count)
309 outlink = inlink->dst->outputs[0];
312 avfilter_draw_slice(outlink, y, h, slice_dir);
315 void avfilter_draw_slice(AVFilterLink *link, int y, int h, int slice_dir)
317 uint8_t *src[4], *dst[4];
319 void (*draw_slice)(AVFilterLink *, int, int, int);
321 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);
323 /* copy the slice if needed for permission reasons */
325 vsub = av_pix_fmt_descriptors[link->format].log2_chroma_h;
327 for (i = 0; i < 4; i++) {
328 if (link->src_buf->data[i]) {
329 src[i] = link->src_buf-> data[i] +
330 (y >> (i==1 || i==2 ? vsub : 0)) * link->src_buf-> linesize[i];
331 dst[i] = link->cur_buf->data[i] +
332 (y >> (i==1 || i==2 ? vsub : 0)) * link->cur_buf->linesize[i];
334 src[i] = dst[i] = NULL;
337 for (i = 0; i < 4; i++) {
339 av_image_get_linesize(link->format, link->cur_buf->video->w, i);
341 if (!src[i]) continue;
343 for (j = 0; j < h >> (i==1 || i==2 ? vsub : 0); j++) {
344 memcpy(dst[i], src[i], planew);
345 src[i] += link->src_buf->linesize[i];
346 dst[i] += link->cur_buf->linesize[i];
351 if (!(draw_slice = link->dstpad->draw_slice))
352 draw_slice = default_draw_slice;
353 draw_slice(link, y, h, slice_dir);
356 #if FF_API_FILTERS_PUBLIC
357 AVFilterBufferRef *avfilter_default_get_video_buffer(AVFilterLink *link, int perms, int w, int h)
359 return ff_default_get_video_buffer(link, perms, w, h);
361 void avfilter_default_start_frame(AVFilterLink *inlink, AVFilterBufferRef *picref)
363 default_start_frame(inlink, picref);
365 void avfilter_default_end_frame(AVFilterLink *inlink)
367 default_end_frame(inlink);
369 void avfilter_default_draw_slice(AVFilterLink *inlink, int y, int h, int slice_dir)
371 default_draw_slice(inlink, y, h, slice_dir);
373 AVFilterBufferRef *avfilter_null_get_video_buffer(AVFilterLink *link, int perms, int w, int h)
375 return ff_null_get_video_buffer(link, perms, w, h);
377 void avfilter_null_start_frame(AVFilterLink *link, AVFilterBufferRef *picref)
379 ff_null_start_frame(link, picref);
381 void avfilter_null_end_frame(AVFilterLink *link)
383 ff_null_end_frame(link);
385 void avfilter_null_draw_slice(AVFilterLink *link, int y, int h, int slice_dir)
387 ff_null_draw_slice(link, y, h, slice_dir);