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