]> git.sesse.net Git - ffmpeg/blob - libavfilter/video.c
Merge remote-tracking branch 'qatar/master'
[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 "libavutil/imgutils.h"
24
25 #include "avfilter.h"
26 #include "internal.h"
27 #include "video.h"
28
29 AVFilterBufferRef *ff_null_get_video_buffer(AVFilterLink *link, int perms, int w, int h)
30 {
31     return ff_get_video_buffer(link->dst->outputs[0], perms, w, h);
32 }
33
34 AVFilterBufferRef *ff_default_get_video_buffer(AVFilterLink *link, int perms, int w, int h)
35 {
36     int linesize[4];
37     uint8_t *data[4];
38     int i;
39     AVFilterBufferRef *picref = NULL;
40     AVFilterPool *pool = link->pool;
41
42     if (pool) {
43         for (i = 0; i < POOL_SIZE; i++) {
44             picref = pool->pic[i];
45             if (picref && picref->buf->format == link->format && picref->buf->w == w && picref->buf->h == h) {
46                 AVFilterBuffer *pic = picref->buf;
47                 pool->pic[i] = NULL;
48                 pool->count--;
49                 picref->video->w = w;
50                 picref->video->h = h;
51                 picref->perms = perms | AV_PERM_READ;
52                 picref->format = link->format;
53                 pic->refcount = 1;
54                 memcpy(picref->data,     pic->data,     sizeof(picref->data));
55                 memcpy(picref->linesize, pic->linesize, sizeof(picref->linesize));
56                 pool->refcount++;
57                 return picref;
58             }
59         }
60     } else {
61         pool = link->pool = av_mallocz(sizeof(AVFilterPool));
62         pool->refcount = 1;
63     }
64
65     // align: +2 is needed for swscaler, +16 to be SIMD-friendly
66     if ((i = av_image_alloc(data, linesize, w, h, link->format, 32)) < 0)
67         return NULL;
68
69     picref = avfilter_get_video_buffer_ref_from_arrays(data, linesize,
70                                                        perms, w, h, link->format);
71     if (!picref) {
72         av_free(data[0]);
73         return NULL;
74     }
75
76     memset(data[0], 128, i);
77
78     picref->buf->priv = pool;
79     picref->buf->free = NULL;
80     pool->refcount++;
81
82     return picref;
83 }
84
85 AVFilterBufferRef *
86 avfilter_get_video_buffer_ref_from_arrays(uint8_t * const data[4], const int linesize[4], int perms,
87                                           int w, int h, enum PixelFormat format)
88 {
89     AVFilterBuffer *pic = av_mallocz(sizeof(AVFilterBuffer));
90     AVFilterBufferRef *picref = av_mallocz(sizeof(AVFilterBufferRef));
91
92     if (!pic || !picref)
93         goto fail;
94
95     picref->buf = pic;
96     picref->buf->free = ff_avfilter_default_free_buffer;
97     if (!(picref->video = av_mallocz(sizeof(AVFilterBufferRefVideoProps))))
98         goto fail;
99
100     pic->w = picref->video->w = w;
101     pic->h = picref->video->h = h;
102
103     /* make sure the buffer gets read permission or it's useless for output */
104     picref->perms = perms | AV_PERM_READ;
105
106     pic->refcount = 1;
107     picref->type = AVMEDIA_TYPE_VIDEO;
108     pic->format = picref->format = format;
109
110     memcpy(pic->data,        data,          4*sizeof(data[0]));
111     memcpy(pic->linesize,    linesize,      4*sizeof(linesize[0]));
112     memcpy(picref->data,     pic->data,     sizeof(picref->data));
113     memcpy(picref->linesize, pic->linesize, sizeof(picref->linesize));
114
115     pic->   extended_data = pic->data;
116     picref->extended_data = picref->data;
117
118     picref->pts = AV_NOPTS_VALUE;
119
120     return picref;
121
122 fail:
123     if (picref && picref->video)
124         av_free(picref->video);
125     av_free(picref);
126     av_free(pic);
127     return NULL;
128 }
129
130 AVFilterBufferRef *ff_get_video_buffer(AVFilterLink *link, int perms, int w, int h)
131 {
132     AVFilterBufferRef *ret = NULL;
133
134     av_unused char buf[16];
135     FF_TPRINTF_START(NULL, get_video_buffer); ff_tlog_link(NULL, link, 0);
136     ff_tlog(NULL, " perms:%s w:%d h:%d\n", ff_get_ref_perms_string(buf, sizeof(buf), perms), w, h);
137
138     if (link->dstpad->get_video_buffer)
139         ret = link->dstpad->get_video_buffer(link, perms, w, h);
140
141     if (!ret)
142         ret = ff_default_get_video_buffer(link, perms, w, h);
143
144     if (ret)
145         ret->type = AVMEDIA_TYPE_VIDEO;
146
147     FF_TPRINTF_START(NULL, get_video_buffer); ff_tlog_link(NULL, link, 0); ff_tlog(NULL, " returning "); ff_tlog_ref(NULL, ret, 1);
148
149     return ret;
150 }
151
152 void ff_null_start_frame(AVFilterLink *link, AVFilterBufferRef *picref)
153 {
154     ff_start_frame(link->dst->outputs[0], picref);
155 }
156
157 static void default_start_frame(AVFilterLink *inlink, AVFilterBufferRef *picref)
158 {
159     AVFilterLink *outlink = NULL;
160
161     if (inlink->dst->nb_outputs)
162         outlink = inlink->dst->outputs[0];
163
164     if (outlink) {
165         outlink->out_buf = ff_get_video_buffer(outlink, AV_PERM_WRITE, outlink->w, outlink->h);
166         avfilter_copy_buffer_ref_props(outlink->out_buf, picref);
167         outlink->out_buf->video->w = outlink->w;
168         outlink->out_buf->video->h = outlink->h;
169         ff_start_frame(outlink, avfilter_ref_buffer(outlink->out_buf, ~0));
170     }
171 }
172
173 /* XXX: should we do the duplicating of the picture ref here, instead of
174  * forcing the source filter to do it? */
175 void ff_start_frame(AVFilterLink *link, AVFilterBufferRef *picref)
176 {
177     void (*start_frame)(AVFilterLink *, AVFilterBufferRef *);
178     AVFilterPad *dst = link->dstpad;
179     int perms = picref->perms;
180     AVFilterCommand *cmd= link->dst->command_queue;
181
182     FF_TPRINTF_START(NULL, start_frame); ff_tlog_link(NULL, link, 0); ff_tlog(NULL, " "); ff_tlog_ref(NULL, picref, 1);
183
184     if (!(start_frame = dst->start_frame))
185         start_frame = default_start_frame;
186
187     if (picref->linesize[0] < 0)
188         perms |= AV_PERM_NEG_LINESIZES;
189     /* prepare to copy the picture if it has insufficient permissions */
190     if ((dst->min_perms & perms) != dst->min_perms || dst->rej_perms & perms) {
191         av_log(link->dst, AV_LOG_DEBUG,
192                 "frame copy needed (have perms %x, need %x, reject %x)\n",
193                 picref->perms,
194                 link->dstpad->min_perms, link->dstpad->rej_perms);
195
196         link->cur_buf = ff_get_video_buffer(link, dst->min_perms, link->w, link->h);
197         link->src_buf = picref;
198         avfilter_copy_buffer_ref_props(link->cur_buf, link->src_buf);
199
200         /* copy palette if required */
201         if (av_pix_fmt_descriptors[link->format].flags & PIX_FMT_PAL)
202             memcpy(link->cur_buf->data[1], link->src_buf-> data[1], AVPALETTE_SIZE);
203     }
204     else
205         link->cur_buf = picref;
206
207     while(cmd && cmd->time <= picref->pts * av_q2d(link->time_base)){
208         av_log(link->dst, AV_LOG_DEBUG,
209                "Processing command time:%f command:%s arg:%s\n",
210                cmd->time, cmd->command, cmd->arg);
211         avfilter_process_command(link->dst, cmd->command, cmd->arg, 0, 0, cmd->flags);
212         ff_command_queue_pop(link->dst);
213         cmd= link->dst->command_queue;
214     }
215
216     start_frame(link, link->cur_buf);
217     ff_update_link_current_pts(link, link->cur_buf->pts);
218 }
219
220 void ff_null_start_frame_keep_ref(AVFilterLink *inlink,
221                                                 AVFilterBufferRef *picref)
222 {
223     ff_start_frame(inlink->dst->outputs[0], avfilter_ref_buffer(picref, ~0));
224 }
225
226 void ff_null_end_frame(AVFilterLink *link)
227 {
228     ff_end_frame(link->dst->outputs[0]);
229 }
230
231 static void default_end_frame(AVFilterLink *inlink)
232 {
233     AVFilterLink *outlink = NULL;
234
235     if (inlink->dst->nb_outputs)
236         outlink = inlink->dst->outputs[0];
237
238     avfilter_unref_buffer(inlink->cur_buf);
239     inlink->cur_buf = NULL;
240
241     if (outlink) {
242         if (outlink->out_buf) {
243             avfilter_unref_buffer(outlink->out_buf);
244             outlink->out_buf = NULL;
245         }
246         ff_end_frame(outlink);
247     }
248 }
249
250 void ff_end_frame(AVFilterLink *link)
251 {
252     void (*end_frame)(AVFilterLink *);
253
254     if (!(end_frame = link->dstpad->end_frame))
255         end_frame = default_end_frame;
256
257     end_frame(link);
258
259     /* unreference the source picture if we're feeding the destination filter
260      * a copied version dues to permission issues */
261     if (link->src_buf) {
262         avfilter_unref_buffer(link->src_buf);
263         link->src_buf = NULL;
264     }
265 }
266
267 void ff_null_draw_slice(AVFilterLink *link, int y, int h, int slice_dir)
268 {
269     ff_draw_slice(link->dst->outputs[0], y, h, slice_dir);
270 }
271
272 static void default_draw_slice(AVFilterLink *inlink, int y, int h, int slice_dir)
273 {
274     AVFilterLink *outlink = NULL;
275
276     if (inlink->dst->nb_outputs)
277         outlink = inlink->dst->outputs[0];
278
279     if (outlink)
280         ff_draw_slice(outlink, y, h, slice_dir);
281 }
282
283 void ff_draw_slice(AVFilterLink *link, int y, int h, int slice_dir)
284 {
285     uint8_t *src[4], *dst[4];
286     int i, j, vsub;
287     void (*draw_slice)(AVFilterLink *, int, int, int);
288
289     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);
290
291     /* copy the slice if needed for permission reasons */
292     if (link->src_buf) {
293         vsub = av_pix_fmt_descriptors[link->format].log2_chroma_h;
294
295         for (i = 0; i < 4; i++) {
296             if (link->src_buf->data[i]) {
297                 src[i] = link->src_buf-> data[i] +
298                     (y >> (i==1 || i==2 ? vsub : 0)) * link->src_buf-> linesize[i];
299                 dst[i] = link->cur_buf->data[i] +
300                     (y >> (i==1 || i==2 ? vsub : 0)) * link->cur_buf->linesize[i];
301             } else
302                 src[i] = dst[i] = NULL;
303         }
304
305         for (i = 0; i < 4; i++) {
306             int planew =
307                 av_image_get_linesize(link->format, link->cur_buf->video->w, i);
308
309             if (!src[i]) continue;
310
311             for (j = 0; j < h >> (i==1 || i==2 ? vsub : 0); j++) {
312                 memcpy(dst[i], src[i], planew);
313                 src[i] += link->src_buf->linesize[i];
314                 dst[i] += link->cur_buf->linesize[i];
315             }
316         }
317     }
318
319     if (!(draw_slice = link->dstpad->draw_slice))
320         draw_slice = default_draw_slice;
321     draw_slice(link, y, h, slice_dir);
322 }
323
324 #if FF_API_FILTERS_PUBLIC
325 AVFilterBufferRef *avfilter_default_get_video_buffer(AVFilterLink *link, int perms, int w, int h)
326 {
327     return ff_default_get_video_buffer(link, perms, w, h);
328 }
329 void avfilter_default_start_frame(AVFilterLink *inlink, AVFilterBufferRef *picref)
330 {
331     default_start_frame(inlink, picref);
332 }
333 void avfilter_default_end_frame(AVFilterLink *inlink)
334 {
335     default_end_frame(inlink);
336 }
337 void avfilter_default_draw_slice(AVFilterLink *inlink, int y, int h, int slice_dir)
338 {
339     default_draw_slice(inlink, y, h, slice_dir);
340 }
341 AVFilterBufferRef *avfilter_null_get_video_buffer(AVFilterLink *link, int perms, int w, int h)
342 {
343     return ff_null_get_video_buffer(link, perms, w, h);
344 }
345 void avfilter_null_start_frame(AVFilterLink *link, AVFilterBufferRef *picref)
346 {
347     ff_null_start_frame(link, picref);
348 }
349 void avfilter_null_end_frame(AVFilterLink *link)
350 {
351     ff_null_end_frame(link);
352 }
353 void avfilter_null_draw_slice(AVFilterLink *link, int y, int h, int slice_dir)
354 {
355     ff_null_draw_slice(link, y, h, slice_dir);
356 }
357 void avfilter_start_frame(AVFilterLink *link, AVFilterBufferRef *picref)
358 {
359     ff_start_frame(link, picref);
360 }
361 void avfilter_end_frame(AVFilterLink *link)
362 {
363     ff_end_frame(link);
364 }
365 void avfilter_draw_slice(AVFilterLink *link, int y, int h, int slice_dir)
366 {
367     ff_draw_slice(link, y, h, slice_dir);
368 }
369 AVFilterBufferRef *avfilter_get_video_buffer(AVFilterLink *link, int perms, int w, int h)
370 {
371     return ff_get_video_buffer(link, perms, w, h);
372 }
373 #endif