]> git.sesse.net Git - ffmpeg/blob - libavfilter/video.c
Merge commit 'd488c3bcbaf7ddda42597e014deb661a7e9e2112'
[ffmpeg] / libavfilter / video.c
1 /*
2  * Copyright 2007 Bobby Bingham
3  * Copyright Stefano Sabatini <stefasab gmail com>
4  * Copyright Vitor Sessak <vitor1001 gmail com>
5  *
6  * This file is part of FFmpeg.
7  *
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.
12  *
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.
17  *
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
21  */
22
23 #include <string.h>
24 #include <stdio.h>
25
26 #include "libavutil/avassert.h"
27 #include "libavutil/imgutils.h"
28 #include "libavutil/mem.h"
29
30 #include "avfilter.h"
31 #include "internal.h"
32 #include "video.h"
33
34 AVFilterBufferRef *ff_null_get_video_buffer(AVFilterLink *link, int perms, int w, int h)
35 {
36     return ff_get_video_buffer(link->dst->outputs[0], perms, w, h);
37 }
38
39 AVFilterBufferRef *ff_default_get_video_buffer(AVFilterLink *link, int perms, int w, int h)
40 {
41     int linesize[4];
42     uint8_t *data[4];
43     int i;
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;
48
49     av_assert1(!(perms & ~(full_perms | AV_PERM_NEG_LINESIZES)));
50
51     if (pool) {
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;
56                 pool->pic[i] = NULL;
57                 pool->count--;
58                 picref->video->w = w;
59                 picref->video->h = h;
60                 picref->perms = full_perms;
61                 picref->format = link->format;
62                 pic->refcount = 1;
63                 memcpy(picref->data,     pic->data,     sizeof(picref->data));
64                 memcpy(picref->linesize, pic->linesize, sizeof(picref->linesize));
65                 pool->refcount++;
66                 return picref;
67             }
68         }
69     } else {
70         pool = link->pool = av_mallocz(sizeof(AVFilterPool));
71         pool->refcount = 1;
72     }
73
74     // align: +2 is needed for swscaler, +16 to be SIMD-friendly
75     if ((i = av_image_alloc(data, linesize, w, h, link->format, 32)) < 0)
76         return NULL;
77
78     picref = avfilter_get_video_buffer_ref_from_arrays(data, linesize,
79                                                        full_perms, w, h, link->format);
80     if (!picref) {
81         av_free(data[0]);
82         return NULL;
83     }
84
85     memset(data[0], 128, i);
86
87     picref->buf->priv = pool;
88     picref->buf->free = NULL;
89     pool->refcount++;
90
91     return picref;
92 }
93
94 AVFilterBufferRef *
95 avfilter_get_video_buffer_ref_from_arrays(uint8_t * const data[4], const int linesize[4], int perms,
96                                           int w, int h, enum PixelFormat format)
97 {
98     AVFilterBuffer *pic = av_mallocz(sizeof(AVFilterBuffer));
99     AVFilterBufferRef *picref = av_mallocz(sizeof(AVFilterBufferRef));
100
101     if (!pic || !picref)
102         goto fail;
103
104     picref->buf = pic;
105     picref->buf->free = ff_avfilter_default_free_buffer;
106     if (!(picref->video = av_mallocz(sizeof(AVFilterBufferRefVideoProps))))
107         goto fail;
108
109     pic->w = picref->video->w = w;
110     pic->h = picref->video->h = h;
111
112     /* make sure the buffer gets read permission or it's useless for output */
113     picref->perms = perms | AV_PERM_READ;
114
115     pic->refcount = 1;
116     picref->type = AVMEDIA_TYPE_VIDEO;
117     pic->format = picref->format = format;
118
119     memcpy(pic->data,        data,          4*sizeof(data[0]));
120     memcpy(pic->linesize,    linesize,      4*sizeof(linesize[0]));
121     memcpy(picref->data,     pic->data,     sizeof(picref->data));
122     memcpy(picref->linesize, pic->linesize, sizeof(picref->linesize));
123
124     pic->   extended_data = pic->data;
125     picref->extended_data = picref->data;
126
127     picref->pts = AV_NOPTS_VALUE;
128
129     return picref;
130
131 fail:
132     if (picref && picref->video)
133         av_free(picref->video);
134     av_free(picref);
135     av_free(pic);
136     return NULL;
137 }
138
139 AVFilterBufferRef *ff_get_video_buffer(AVFilterLink *link, int perms, int w, int h)
140 {
141     AVFilterBufferRef *ret = NULL;
142
143     av_unused char buf[16];
144     FF_TPRINTF_START(NULL, get_video_buffer); ff_tlog_link(NULL, link, 0);
145     ff_tlog(NULL, " perms:%s w:%d h:%d\n", ff_get_ref_perms_string(buf, sizeof(buf), perms), w, h);
146
147     if (link->dstpad->get_video_buffer)
148         ret = link->dstpad->get_video_buffer(link, perms, w, h);
149
150     if (!ret)
151         ret = ff_default_get_video_buffer(link, perms, w, h);
152
153     if (ret)
154         ret->type = AVMEDIA_TYPE_VIDEO;
155
156     FF_TPRINTF_START(NULL, get_video_buffer); ff_tlog_link(NULL, link, 0); ff_tlog(NULL, " returning "); ff_tlog_ref(NULL, ret, 1);
157
158     return ret;
159 }
160
161 int ff_null_start_frame(AVFilterLink *link, AVFilterBufferRef *picref)
162 {
163     AVFilterBufferRef *buf_out = avfilter_ref_buffer(picref, ~0);
164     if (!buf_out)
165         return AVERROR(ENOMEM);
166     return ff_start_frame(link->dst->outputs[0], buf_out);
167 }
168
169 // for filters that support (but don't require) outpic==inpic
170 int ff_inplace_start_frame(AVFilterLink *inlink, AVFilterBufferRef *inpicref)
171 {
172     AVFilterLink *outlink = inlink->dst->outputs[0];
173     AVFilterBufferRef *outpicref = NULL, *for_next_filter;
174     int ret = 0;
175
176     if (inpicref->perms & AV_PERM_WRITE) {
177         outpicref = avfilter_ref_buffer(inpicref, ~0);
178         if (!outpicref)
179             return AVERROR(ENOMEM);
180     } else {
181         outpicref = ff_get_video_buffer(outlink, AV_PERM_WRITE, outlink->w, outlink->h);
182         if (!outpicref)
183             return AVERROR(ENOMEM);
184
185         avfilter_copy_buffer_ref_props(outpicref, inpicref);
186         outpicref->video->w = outlink->w;
187         outpicref->video->h = outlink->h;
188     }
189
190     for_next_filter = avfilter_ref_buffer(outpicref, ~0);
191     if (for_next_filter)
192         ret = ff_start_frame(outlink, for_next_filter);
193     else
194         ret = AVERROR(ENOMEM);
195
196     if (ret < 0) {
197         avfilter_unref_bufferp(&outpicref);
198         return ret;
199     }
200
201     outlink->out_buf = outpicref;
202     return 0;
203 }
204
205 static int default_start_frame(AVFilterLink *inlink, AVFilterBufferRef *picref)
206 {
207     AVFilterLink *outlink = NULL;
208
209     if (inlink->dst->nb_outputs)
210         outlink = inlink->dst->outputs[0];
211
212     if (outlink) {
213         AVFilterBufferRef *buf_out;
214         outlink->out_buf = ff_get_video_buffer(outlink, AV_PERM_WRITE, outlink->w, outlink->h);
215         if (!outlink->out_buf)
216             return AVERROR(ENOMEM);
217
218         avfilter_copy_buffer_ref_props(outlink->out_buf, picref);
219         outlink->out_buf->video->w = outlink->w;
220         outlink->out_buf->video->h = outlink->h;
221         buf_out = avfilter_ref_buffer(outlink->out_buf, ~0);
222         if (!buf_out)
223             return AVERROR(ENOMEM);
224
225         return ff_start_frame(outlink, buf_out);
226     }
227     return 0;
228 }
229
230 static void clear_link(AVFilterLink *link)
231 {
232     avfilter_unref_bufferp(&link->cur_buf);
233     avfilter_unref_bufferp(&link->src_buf);
234     avfilter_unref_bufferp(&link->out_buf);
235     link->cur_buf_copy = NULL; /* we do not own the reference */
236 }
237
238 /* XXX: should we do the duplicating of the picture ref here, instead of
239  * forcing the source filter to do it? */
240 int ff_start_frame(AVFilterLink *link, AVFilterBufferRef *picref)
241 {
242     int (*start_frame)(AVFilterLink *, AVFilterBufferRef *);
243     AVFilterPad *src = link->srcpad;
244     AVFilterPad *dst = link->dstpad;
245     int ret, perms;
246     AVFilterCommand *cmd= link->dst->command_queue;
247     int64_t pts;
248
249     FF_TPRINTF_START(NULL, start_frame); ff_tlog_link(NULL, link, 0); ff_tlog(NULL, " "); ff_tlog_ref(NULL, picref, 1);
250
251     if (link->closed) {
252         avfilter_unref_buffer(picref);
253         return AVERROR_EOF;
254     }
255
256     if (!(start_frame = dst->start_frame))
257         start_frame = default_start_frame;
258
259     av_assert1((picref->perms & src->min_perms) == src->min_perms);
260     picref->perms &= ~ src->rej_perms;
261     perms = picref->perms;
262
263     if (picref->linesize[0] < 0)
264         perms |= AV_PERM_NEG_LINESIZES;
265     /* prepare to copy the picture if it has insufficient permissions */
266     if ((dst->min_perms & perms) != dst->min_perms || dst->rej_perms & perms) {
267         av_log(link->dst, AV_LOG_DEBUG,
268                 "frame copy needed (have perms %x, need %x, reject %x)\n",
269                 picref->perms,
270                 link->dstpad->min_perms, link->dstpad->rej_perms);
271
272         link->cur_buf = ff_get_video_buffer(link, dst->min_perms, link->w, link->h);
273         if (!link->cur_buf) {
274             avfilter_unref_bufferp(&picref);
275             return AVERROR(ENOMEM);
276         }
277
278         link->src_buf = picref;
279         avfilter_copy_buffer_ref_props(link->cur_buf, link->src_buf);
280
281         /* copy palette if required */
282         if (av_pix_fmt_descriptors[link->format].flags & PIX_FMT_PAL)
283             memcpy(link->cur_buf->data[1], link->src_buf-> data[1], AVPALETTE_SIZE);
284     }
285     else
286         link->cur_buf = picref;
287
288     link->cur_buf_copy = link->cur_buf;
289
290     while(cmd && cmd->time <= picref->pts * av_q2d(link->time_base)){
291         av_log(link->dst, AV_LOG_DEBUG,
292                "Processing command time:%f command:%s arg:%s\n",
293                cmd->time, cmd->command, cmd->arg);
294         avfilter_process_command(link->dst, cmd->command, cmd->arg, 0, 0, cmd->flags);
295         ff_command_queue_pop(link->dst);
296         cmd= link->dst->command_queue;
297     }
298     pts = link->cur_buf->pts;
299     ret = start_frame(link, link->cur_buf);
300     ff_update_link_current_pts(link, pts);
301     if (ret < 0)
302         clear_link(link);
303     else
304         /* incoming buffers must not be freed in start frame,
305            because they can still be in use by the automatic copy mechanism */
306         av_assert1(link->cur_buf_copy->buf->refcount > 0);
307
308     return ret;
309 }
310
311 int ff_null_end_frame(AVFilterLink *link)
312 {
313     return ff_end_frame(link->dst->outputs[0]);
314 }
315
316 static int default_end_frame(AVFilterLink *inlink)
317 {
318     AVFilterLink *outlink = NULL;
319
320     if (inlink->dst->nb_outputs)
321         outlink = inlink->dst->outputs[0];
322
323     if (outlink) {
324         return ff_end_frame(outlink);
325     }
326     return 0;
327 }
328
329 int ff_end_frame(AVFilterLink *link)
330 {
331     int (*end_frame)(AVFilterLink *);
332     int ret;
333
334     if (!(end_frame = link->dstpad->end_frame))
335         end_frame = default_end_frame;
336
337     ret = end_frame(link);
338
339     clear_link(link);
340
341     return ret;
342 }
343
344 int ff_null_draw_slice(AVFilterLink *link, int y, int h, int slice_dir)
345 {
346     return ff_draw_slice(link->dst->outputs[0], y, h, slice_dir);
347 }
348
349 static int default_draw_slice(AVFilterLink *inlink, int y, int h, int slice_dir)
350 {
351     AVFilterLink *outlink = NULL;
352
353     if (inlink->dst->nb_outputs)
354         outlink = inlink->dst->outputs[0];
355
356     if (outlink)
357         return ff_draw_slice(outlink, y, h, slice_dir);
358     return 0;
359 }
360
361 int ff_draw_slice(AVFilterLink *link, int y, int h, int slice_dir)
362 {
363     uint8_t *src[4], *dst[4];
364     int i, j, vsub, ret;
365     int (*draw_slice)(AVFilterLink *, int, int, int);
366
367     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);
368
369     /* copy the slice if needed for permission reasons */
370     if (link->src_buf) {
371         vsub = av_pix_fmt_descriptors[link->format].log2_chroma_h;
372
373         for (i = 0; i < 4; i++) {
374             if (link->src_buf->data[i]) {
375                 src[i] = link->src_buf-> data[i] +
376                     (y >> (i==1 || i==2 ? vsub : 0)) * link->src_buf-> linesize[i];
377                 dst[i] = link->cur_buf_copy->data[i] +
378                     (y >> (i==1 || i==2 ? vsub : 0)) * link->cur_buf_copy->linesize[i];
379             } else
380                 src[i] = dst[i] = NULL;
381         }
382
383         for (i = 0; i < 4; i++) {
384             int planew =
385                 av_image_get_linesize(link->format, link->cur_buf_copy->video->w, i);
386
387             if (!src[i]) continue;
388
389             for (j = 0; j < h >> (i==1 || i==2 ? vsub : 0); j++) {
390                 memcpy(dst[i], src[i], planew);
391                 src[i] += link->src_buf->linesize[i];
392                 dst[i] += link->cur_buf_copy->linesize[i];
393             }
394         }
395     }
396
397     if (!(draw_slice = link->dstpad->draw_slice))
398         draw_slice = default_draw_slice;
399     ret = draw_slice(link, y, h, slice_dir);
400     if (ret < 0)
401         clear_link(link);
402     else
403         /* incoming buffers must not be freed in start frame,
404            because they can still be in use by the automatic copy mechanism */
405         av_assert1(link->cur_buf_copy->buf->refcount > 0);
406     return ret;
407 }
408
409 int avfilter_default_end_frame(AVFilterLink *inlink)
410 {
411     return default_end_frame(inlink);
412 }
413