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