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
19 #include "libavutil/imgutils.h"
26 static char *ff_get_ref_perms_string(char *buf, size_t buf_size, int perms)
28 snprintf(buf, buf_size, "%s%s%s%s%s%s",
29 perms & AV_PERM_READ ? "r" : "",
30 perms & AV_PERM_WRITE ? "w" : "",
31 perms & AV_PERM_PRESERVE ? "p" : "",
32 perms & AV_PERM_REUSE ? "u" : "",
33 perms & AV_PERM_REUSE2 ? "U" : "",
34 perms & AV_PERM_NEG_LINESIZES ? "n" : "");
39 static void ff_dlog_ref(void *ctx, AVFilterBufferRef *ref, int end)
41 av_unused char buf[16];
43 "ref[%p buf:%p refcount:%d perms:%s data:%p linesize[%d, %d, %d, %d] pts:%"PRId64" pos:%"PRId64,
44 ref, ref->buf, ref->buf->refcount, ff_get_ref_perms_string(buf, sizeof(buf), ref->perms), ref->data[0],
45 ref->linesize[0], ref->linesize[1], ref->linesize[2], ref->linesize[3],
49 av_dlog(ctx, " a:%d/%d s:%dx%d i:%c iskey:%d type:%c",
50 ref->video->pixel_aspect.num, ref->video->pixel_aspect.den,
51 ref->video->w, ref->video->h,
52 !ref->video->interlaced ? 'P' : /* Progressive */
53 ref->video->top_field_first ? 'T' : 'B', /* Top / Bottom */
54 ref->video->key_frame,
55 av_get_picture_type_char(ref->video->pict_type));
58 av_dlog(ctx, " cl:%"PRId64"d n:%d r:%d p:%d",
59 ref->audio->channel_layout,
60 ref->audio->nb_samples,
61 ref->audio->sample_rate,
65 av_dlog(ctx, "]%s", end ? "\n" : "");
68 AVFilterBufferRef *ff_null_get_video_buffer(AVFilterLink *link, int perms, int w, int h)
70 return avfilter_get_video_buffer(link->dst->outputs[0], perms, w, h);
73 /* TODO: set the buffer's priv member to a context structure for the whole
74 * filter chain. This will allow for a buffer pool instead of the constant
75 * alloc & free cycle currently implemented. */
76 AVFilterBufferRef *ff_default_get_video_buffer(AVFilterLink *link, int perms, int w, int h)
80 AVFilterBufferRef *picref = NULL;
82 // +2 is needed for swscaler, +16 to be SIMD-friendly
83 if (av_image_alloc(data, linesize, w, h, link->format, 16) < 0)
86 picref = avfilter_get_video_buffer_ref_from_arrays(data, linesize,
87 perms, w, h, link->format);
97 avfilter_get_video_buffer_ref_from_arrays(uint8_t *data[4], int linesize[4], int perms,
98 int w, int h, enum PixelFormat format)
100 AVFilterBuffer *pic = av_mallocz(sizeof(AVFilterBuffer));
101 AVFilterBufferRef *picref = av_mallocz(sizeof(AVFilterBufferRef));
107 picref->buf->free = ff_avfilter_default_free_buffer;
108 if (!(picref->video = av_mallocz(sizeof(AVFilterBufferRefVideoProps))))
111 pic->w = picref->video->w = w;
112 pic->h = picref->video->h = h;
114 /* make sure the buffer gets read permission or it's useless for output */
115 picref->perms = perms | AV_PERM_READ;
118 picref->type = AVMEDIA_TYPE_VIDEO;
119 pic->format = picref->format = format;
121 memcpy(pic->data, data, 4*sizeof(data[0]));
122 memcpy(pic->linesize, linesize, 4*sizeof(linesize[0]));
123 memcpy(picref->data, pic->data, sizeof(picref->data));
124 memcpy(picref->linesize, pic->linesize, sizeof(picref->linesize));
126 pic-> extended_data = pic->data;
127 picref->extended_data = picref->data;
129 picref->pts = AV_NOPTS_VALUE;
134 if (picref && picref->video)
135 av_free(picref->video);
141 AVFilterBufferRef *avfilter_get_video_buffer(AVFilterLink *link, int perms, int w, int h)
143 AVFilterBufferRef *ret = NULL;
145 av_unused char buf[16];
146 FF_DPRINTF_START(NULL, get_video_buffer); ff_dlog_link(NULL, link, 0);
147 av_dlog(NULL, " perms:%s w:%d h:%d\n", ff_get_ref_perms_string(buf, sizeof(buf), perms), w, h);
149 if (link->dstpad->get_video_buffer)
150 ret = link->dstpad->get_video_buffer(link, perms, w, h);
153 ret = ff_default_get_video_buffer(link, perms, w, h);
156 ret->type = AVMEDIA_TYPE_VIDEO;
158 FF_DPRINTF_START(NULL, get_video_buffer); ff_dlog_link(NULL, link, 0); av_dlog(NULL, " returning "); ff_dlog_ref(NULL, ret, 1);
163 void ff_null_start_frame(AVFilterLink *link, AVFilterBufferRef *picref)
165 avfilter_start_frame(link->dst->outputs[0], picref);
168 static void default_start_frame(AVFilterLink *inlink, AVFilterBufferRef *picref)
170 AVFilterLink *outlink = NULL;
172 if (inlink->dst->output_count)
173 outlink = inlink->dst->outputs[0];
176 outlink->out_buf = avfilter_get_video_buffer(outlink, AV_PERM_WRITE, outlink->w, outlink->h);
177 avfilter_copy_buffer_ref_props(outlink->out_buf, picref);
178 avfilter_start_frame(outlink, avfilter_ref_buffer(outlink->out_buf, ~0));
182 /* XXX: should we do the duplicating of the picture ref here, instead of
183 * forcing the source filter to do it? */
184 void avfilter_start_frame(AVFilterLink *link, AVFilterBufferRef *picref)
186 void (*start_frame)(AVFilterLink *, AVFilterBufferRef *);
187 AVFilterPad *dst = link->dstpad;
188 int perms = picref->perms;
190 FF_DPRINTF_START(NULL, start_frame); ff_dlog_link(NULL, link, 0); av_dlog(NULL, " "); ff_dlog_ref(NULL, picref, 1);
192 if (!(start_frame = dst->start_frame))
193 start_frame = default_start_frame;
195 if (picref->linesize[0] < 0)
196 perms |= AV_PERM_NEG_LINESIZES;
197 /* prepare to copy the picture if it has insufficient permissions */
198 if ((dst->min_perms & perms) != dst->min_perms || dst->rej_perms & perms) {
199 av_log(link->dst, AV_LOG_DEBUG,
200 "frame copy needed (have perms %x, need %x, reject %x)\n",
202 link->dstpad->min_perms, link->dstpad->rej_perms);
204 link->cur_buf = avfilter_get_video_buffer(link, dst->min_perms, link->w, link->h);
205 link->src_buf = picref;
206 avfilter_copy_buffer_ref_props(link->cur_buf, link->src_buf);
209 link->cur_buf = picref;
211 start_frame(link, link->cur_buf);
214 void ff_null_end_frame(AVFilterLink *link)
216 avfilter_end_frame(link->dst->outputs[0]);
219 static void default_end_frame(AVFilterLink *inlink)
221 AVFilterLink *outlink = NULL;
223 if (inlink->dst->output_count)
224 outlink = inlink->dst->outputs[0];
226 avfilter_unref_buffer(inlink->cur_buf);
227 inlink->cur_buf = NULL;
230 if (outlink->out_buf) {
231 avfilter_unref_buffer(outlink->out_buf);
232 outlink->out_buf = NULL;
234 avfilter_end_frame(outlink);
238 void avfilter_end_frame(AVFilterLink *link)
240 void (*end_frame)(AVFilterLink *);
242 if (!(end_frame = link->dstpad->end_frame))
243 end_frame = default_end_frame;
247 /* unreference the source picture if we're feeding the destination filter
248 * a copied version dues to permission issues */
250 avfilter_unref_buffer(link->src_buf);
251 link->src_buf = NULL;
255 void ff_null_draw_slice(AVFilterLink *link, int y, int h, int slice_dir)
257 avfilter_draw_slice(link->dst->outputs[0], y, h, slice_dir);
260 static void default_draw_slice(AVFilterLink *inlink, int y, int h, int slice_dir)
262 AVFilterLink *outlink = NULL;
264 if (inlink->dst->output_count)
265 outlink = inlink->dst->outputs[0];
268 avfilter_draw_slice(outlink, y, h, slice_dir);
271 void avfilter_draw_slice(AVFilterLink *link, int y, int h, int slice_dir)
273 uint8_t *src[4], *dst[4];
275 void (*draw_slice)(AVFilterLink *, int, int, int);
277 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);
279 /* copy the slice if needed for permission reasons */
281 vsub = av_pix_fmt_descriptors[link->format].log2_chroma_h;
283 for (i = 0; i < 4; i++) {
284 if (link->src_buf->data[i]) {
285 src[i] = link->src_buf-> data[i] +
286 (y >> (i==1 || i==2 ? vsub : 0)) * link->src_buf-> linesize[i];
287 dst[i] = link->cur_buf->data[i] +
288 (y >> (i==1 || i==2 ? vsub : 0)) * link->cur_buf->linesize[i];
290 src[i] = dst[i] = NULL;
293 for (i = 0; i < 4; i++) {
295 av_image_get_linesize(link->format, link->cur_buf->video->w, i);
297 if (!src[i]) continue;
299 for (j = 0; j < h >> (i==1 || i==2 ? vsub : 0); j++) {
300 memcpy(dst[i], src[i], planew);
301 src[i] += link->src_buf->linesize[i];
302 dst[i] += link->cur_buf->linesize[i];
307 if (!(draw_slice = link->dstpad->draw_slice))
308 draw_slice = default_draw_slice;
309 draw_slice(link, y, h, slice_dir);
312 #if FF_API_FILTERS_PUBLIC
313 AVFilterBufferRef *avfilter_default_get_video_buffer(AVFilterLink *link, int perms, int w, int h)
315 return ff_default_get_video_buffer(link, perms, w, h);
317 void avfilter_default_start_frame(AVFilterLink *inlink, AVFilterBufferRef *picref)
319 default_start_frame(inlink, picref);
321 void avfilter_default_end_frame(AVFilterLink *inlink)
323 default_end_frame(inlink);
325 void avfilter_default_draw_slice(AVFilterLink *inlink, int y, int h, int slice_dir)
327 default_draw_slice(inlink, y, h, slice_dir);
329 AVFilterBufferRef *avfilter_null_get_video_buffer(AVFilterLink *link, int perms, int w, int h)
331 return ff_null_get_video_buffer(link, perms, w, h);
333 void avfilter_null_start_frame(AVFilterLink *link, AVFilterBufferRef *picref)
335 ff_null_start_frame(link, picref);
337 void avfilter_null_end_frame(AVFilterLink *link)
339 ff_null_end_frame(link);
341 void avfilter_null_draw_slice(AVFilterLink *link, int y, int h, int slice_dir)
343 ff_null_draw_slice(link, y, h, slice_dir);