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