2 * This file is part of Libav.
4 * Libav is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * Libav is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with Libav; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 #include "libavutil/imgutils.h"
23 #include "libavutil/mem.h"
30 static char *ff_get_ref_perms_string(char *buf, size_t buf_size, int perms)
32 snprintf(buf, buf_size, "%s%s%s%s%s%s",
33 perms & AV_PERM_READ ? "r" : "",
34 perms & AV_PERM_WRITE ? "w" : "",
35 perms & AV_PERM_PRESERVE ? "p" : "",
36 perms & AV_PERM_REUSE ? "u" : "",
37 perms & AV_PERM_REUSE2 ? "U" : "",
38 perms & AV_PERM_NEG_LINESIZES ? "n" : "");
43 static void ff_dlog_ref(void *ctx, AVFilterBufferRef *ref, int end)
45 av_unused char buf[16];
47 "ref[%p buf:%p refcount:%d perms:%s data:%p linesize[%d, %d, %d, %d] pts:%"PRId64" pos:%"PRId64,
48 ref, ref->buf, ref->buf->refcount, ff_get_ref_perms_string(buf, sizeof(buf), ref->perms), ref->data[0],
49 ref->linesize[0], ref->linesize[1], ref->linesize[2], ref->linesize[3],
53 av_dlog(ctx, " a:%d/%d s:%dx%d i:%c iskey:%d type:%c",
54 ref->video->pixel_aspect.num, ref->video->pixel_aspect.den,
55 ref->video->w, ref->video->h,
56 !ref->video->interlaced ? 'P' : /* Progressive */
57 ref->video->top_field_first ? 'T' : 'B', /* Top / Bottom */
58 ref->video->key_frame,
59 av_get_picture_type_char(ref->video->pict_type));
62 av_dlog(ctx, " cl:%"PRId64"d n:%d r:%d p:%d",
63 ref->audio->channel_layout,
64 ref->audio->nb_samples,
65 ref->audio->sample_rate,
69 av_dlog(ctx, "]%s", end ? "\n" : "");
72 AVFilterBufferRef *ff_null_get_video_buffer(AVFilterLink *link, int perms, int w, int h)
74 return ff_get_video_buffer(link->dst->outputs[0], perms, w, h);
77 /* TODO: set the buffer's priv member to a context structure for the whole
78 * filter chain. This will allow for a buffer pool instead of the constant
79 * alloc & free cycle currently implemented. */
80 AVFilterBufferRef *ff_default_get_video_buffer(AVFilterLink *link, int perms, int w, int h)
84 AVFilterBufferRef *picref = NULL;
86 // +2 is needed for swscaler, +16 to be SIMD-friendly
87 if (av_image_alloc(data, linesize, w, h, link->format, 16) < 0)
90 picref = avfilter_get_video_buffer_ref_from_arrays(data, linesize,
91 perms, w, h, link->format);
101 avfilter_get_video_buffer_ref_from_arrays(uint8_t *data[4], int linesize[4], int perms,
102 int w, int h, enum PixelFormat format)
104 AVFilterBuffer *pic = av_mallocz(sizeof(AVFilterBuffer));
105 AVFilterBufferRef *picref = av_mallocz(sizeof(AVFilterBufferRef));
111 picref->buf->free = ff_avfilter_default_free_buffer;
112 if (!(picref->video = av_mallocz(sizeof(AVFilterBufferRefVideoProps))))
115 pic->w = picref->video->w = w;
116 pic->h = picref->video->h = h;
118 /* make sure the buffer gets read permission or it's useless for output */
119 picref->perms = perms | AV_PERM_READ;
122 picref->type = AVMEDIA_TYPE_VIDEO;
123 pic->format = picref->format = format;
125 memcpy(pic->data, data, 4*sizeof(data[0]));
126 memcpy(pic->linesize, linesize, 4*sizeof(linesize[0]));
127 memcpy(picref->data, pic->data, sizeof(picref->data));
128 memcpy(picref->linesize, pic->linesize, sizeof(picref->linesize));
130 pic-> extended_data = pic->data;
131 picref->extended_data = picref->data;
133 picref->pts = AV_NOPTS_VALUE;
138 if (picref && picref->video)
139 av_free(picref->video);
145 AVFilterBufferRef *ff_get_video_buffer(AVFilterLink *link, int perms, int w, int h)
147 AVFilterBufferRef *ret = NULL;
149 av_unused char buf[16];
150 FF_DPRINTF_START(NULL, get_video_buffer); ff_dlog_link(NULL, link, 0);
151 av_dlog(NULL, " perms:%s w:%d h:%d\n", ff_get_ref_perms_string(buf, sizeof(buf), perms), w, h);
153 if (link->dstpad->get_video_buffer)
154 ret = link->dstpad->get_video_buffer(link, perms, w, h);
157 ret = ff_default_get_video_buffer(link, perms, w, h);
160 ret->type = AVMEDIA_TYPE_VIDEO;
162 FF_DPRINTF_START(NULL, get_video_buffer); ff_dlog_link(NULL, link, 0); av_dlog(NULL, " returning "); ff_dlog_ref(NULL, ret, 1);
167 int ff_null_start_frame(AVFilterLink *link, AVFilterBufferRef *picref)
169 AVFilterBufferRef *buf_out = avfilter_ref_buffer(picref, ~0);
171 return AVERROR(ENOMEM);
172 return ff_start_frame(link->dst->outputs[0], buf_out);
175 // for filters that support (but don't require) outpic==inpic
176 int ff_inplace_start_frame(AVFilterLink *inlink, AVFilterBufferRef *inpicref)
178 AVFilterLink *outlink = inlink->dst->outputs[0];
179 AVFilterBufferRef *outpicref = NULL, *for_next_filter;
182 if ((inpicref->perms & AV_PERM_WRITE) && !(inpicref->perms & AV_PERM_PRESERVE)) {
183 outpicref = avfilter_ref_buffer(inpicref, ~0);
185 return AVERROR(ENOMEM);
187 outpicref = ff_get_video_buffer(outlink, AV_PERM_WRITE, outlink->w, outlink->h);
189 return AVERROR(ENOMEM);
191 avfilter_copy_buffer_ref_props(outpicref, inpicref);
192 outpicref->video->w = outlink->w;
193 outpicref->video->h = outlink->h;
196 for_next_filter = avfilter_ref_buffer(outpicref, ~0);
198 ret = ff_start_frame(outlink, for_next_filter);
200 ret = AVERROR(ENOMEM);
203 avfilter_unref_bufferp(&outpicref);
207 outlink->out_buf = outpicref;
211 static int default_start_frame(AVFilterLink *inlink, AVFilterBufferRef *picref)
213 AVFilterLink *outlink = NULL;
215 if (inlink->dst->nb_outputs)
216 outlink = inlink->dst->outputs[0];
219 AVFilterBufferRef *buf_out;
220 outlink->out_buf = ff_get_video_buffer(outlink, AV_PERM_WRITE, outlink->w, outlink->h);
221 if (!outlink->out_buf)
222 return AVERROR(ENOMEM);
224 avfilter_copy_buffer_ref_props(outlink->out_buf, picref);
225 buf_out = avfilter_ref_buffer(outlink->out_buf, ~0);
227 return AVERROR(ENOMEM);
229 return ff_start_frame(outlink, buf_out);
234 static void clear_link(AVFilterLink *link)
236 avfilter_unref_bufferp(&link->cur_buf);
237 avfilter_unref_bufferp(&link->src_buf);
238 avfilter_unref_bufferp(&link->out_buf);
241 /* XXX: should we do the duplicating of the picture ref here, instead of
242 * forcing the source filter to do it? */
243 int ff_start_frame(AVFilterLink *link, AVFilterBufferRef *picref)
245 int (*start_frame)(AVFilterLink *, AVFilterBufferRef *);
246 AVFilterPad *dst = link->dstpad;
247 int ret, perms = picref->perms;
249 FF_DPRINTF_START(NULL, start_frame); ff_dlog_link(NULL, link, 0); av_dlog(NULL, " "); ff_dlog_ref(NULL, picref, 1);
251 if (!(start_frame = dst->start_frame))
252 start_frame = default_start_frame;
254 if (picref->linesize[0] < 0)
255 perms |= AV_PERM_NEG_LINESIZES;
256 /* prepare to copy the picture if it has insufficient permissions */
257 if ((dst->min_perms & perms) != dst->min_perms || dst->rej_perms & perms) {
258 av_log(link->dst, AV_LOG_DEBUG,
259 "frame copy needed (have perms %x, need %x, reject %x)\n",
261 link->dstpad->min_perms, link->dstpad->rej_perms);
263 link->cur_buf = ff_get_video_buffer(link, dst->min_perms, link->w, link->h);
264 if (!link->cur_buf) {
265 avfilter_unref_bufferp(&picref);
266 return AVERROR(ENOMEM);
269 link->src_buf = picref;
270 avfilter_copy_buffer_ref_props(link->cur_buf, link->src_buf);
273 link->cur_buf = picref;
275 ret = start_frame(link, link->cur_buf);
282 int ff_null_end_frame(AVFilterLink *link)
284 return ff_end_frame(link->dst->outputs[0]);
287 static int default_end_frame(AVFilterLink *inlink)
289 AVFilterLink *outlink = NULL;
291 if (inlink->dst->nb_outputs)
292 outlink = inlink->dst->outputs[0];
295 return ff_end_frame(outlink);
300 int ff_end_frame(AVFilterLink *link)
302 int (*end_frame)(AVFilterLink *);
305 if (!(end_frame = link->dstpad->end_frame))
306 end_frame = default_end_frame;
308 ret = end_frame(link);
315 int ff_null_draw_slice(AVFilterLink *link, int y, int h, int slice_dir)
317 return ff_draw_slice(link->dst->outputs[0], y, h, slice_dir);
320 static int default_draw_slice(AVFilterLink *inlink, int y, int h, int slice_dir)
322 AVFilterLink *outlink = NULL;
324 if (inlink->dst->nb_outputs)
325 outlink = inlink->dst->outputs[0];
328 return ff_draw_slice(outlink, y, h, slice_dir);
332 int ff_draw_slice(AVFilterLink *link, int y, int h, int slice_dir)
334 uint8_t *src[4], *dst[4];
336 int (*draw_slice)(AVFilterLink *, int, int, int);
338 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);
340 /* copy the slice if needed for permission reasons */
342 vsub = av_pix_fmt_descriptors[link->format].log2_chroma_h;
344 for (i = 0; i < 4; i++) {
345 if (link->src_buf->data[i]) {
346 src[i] = link->src_buf-> data[i] +
347 (y >> (i==1 || i==2 ? vsub : 0)) * link->src_buf-> linesize[i];
348 dst[i] = link->cur_buf->data[i] +
349 (y >> (i==1 || i==2 ? vsub : 0)) * link->cur_buf->linesize[i];
351 src[i] = dst[i] = NULL;
354 for (i = 0; i < 4; i++) {
356 av_image_get_linesize(link->format, link->cur_buf->video->w, i);
358 if (!src[i]) continue;
360 for (j = 0; j < h >> (i==1 || i==2 ? vsub : 0); j++) {
361 memcpy(dst[i], src[i], planew);
362 src[i] += link->src_buf->linesize[i];
363 dst[i] += link->cur_buf->linesize[i];
368 if (!(draw_slice = link->dstpad->draw_slice))
369 draw_slice = default_draw_slice;
370 ret = draw_slice(link, y, h, slice_dir);