]> git.sesse.net Git - ffmpeg/blob - libavfilter/video.c
Merge remote-tracking branch 'cus/stable'
[ffmpeg] / libavfilter / video.c
1 /*
2  * This file is part of FFmpeg.
3  *
4  * FFmpeg is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * FFmpeg is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with FFmpeg; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18
19 #include "libavutil/imgutils.h"
20
21 #include "avfilter.h"
22 #include "internal.h"
23
24 static char *ff_get_ref_perms_string(char *buf, size_t buf_size, int perms)
25 {
26     snprintf(buf, buf_size, "%s%s%s%s%s%s",
27              perms & AV_PERM_READ      ? "r" : "",
28              perms & AV_PERM_WRITE     ? "w" : "",
29              perms & AV_PERM_PRESERVE  ? "p" : "",
30              perms & AV_PERM_REUSE     ? "u" : "",
31              perms & AV_PERM_REUSE2    ? "U" : "",
32              perms & AV_PERM_NEG_LINESIZES ? "n" : "");
33     return buf;
34 }
35
36 static void ff_dlog_ref(void *ctx, AVFilterBufferRef *ref, int end)
37 {
38     av_unused char buf[16];
39     av_dlog(ctx,
40             "ref[%p buf:%p refcount:%d perms:%s data:%p linesize[%d, %d, %d, %d] pts:%"PRId64" pos:%"PRId64,
41             ref, ref->buf, ref->buf->refcount, ff_get_ref_perms_string(buf, sizeof(buf), ref->perms), ref->data[0],
42             ref->linesize[0], ref->linesize[1], ref->linesize[2], ref->linesize[3],
43             ref->pts, ref->pos);
44
45     if (ref->video) {
46         av_dlog(ctx, " a:%d/%d s:%dx%d i:%c iskey:%d type:%c",
47                 ref->video->sample_aspect_ratio.num, ref->video->sample_aspect_ratio.den,
48                 ref->video->w, ref->video->h,
49                 !ref->video->interlaced     ? 'P' :         /* Progressive  */
50                 ref->video->top_field_first ? 'T' : 'B',    /* Top / Bottom */
51                 ref->video->key_frame,
52                 av_get_picture_type_char(ref->video->pict_type));
53     }
54     if (ref->audio) {
55         av_dlog(ctx, " cl:%"PRId64"d n:%d r:%d p:%d",
56                 ref->audio->channel_layout,
57                 ref->audio->nb_samples,
58                 ref->audio->sample_rate,
59                 ref->audio->planar);
60     }
61
62     av_dlog(ctx, "]%s", end ? "\n" : "");
63 }
64
65 AVFilterBufferRef *avfilter_null_get_video_buffer(AVFilterLink *link, int perms, int w, int h)
66 {
67     return avfilter_get_video_buffer(link->dst->outputs[0], perms, w, h);
68 }
69
70 AVFilterBufferRef *avfilter_default_get_video_buffer(AVFilterLink *link, int perms, int w, int h)
71 {
72     int linesize[4];
73     uint8_t *data[4];
74     int i;
75     AVFilterBufferRef *picref = NULL;
76     AVFilterPool *pool = link->pool;
77
78     if (pool) {
79         for (i = 0; i < POOL_SIZE; i++) {
80             picref = pool->pic[i];
81             if (picref && picref->buf->format == link->format && picref->buf->w == w && picref->buf->h == h) {
82                 AVFilterBuffer *pic = picref->buf;
83                 pool->pic[i] = NULL;
84                 pool->count--;
85                 picref->video->w = w;
86                 picref->video->h = h;
87                 picref->perms = perms | AV_PERM_READ;
88                 picref->format = link->format;
89                 pic->refcount = 1;
90                 memcpy(picref->data,     pic->data,     sizeof(picref->data));
91                 memcpy(picref->linesize, pic->linesize, sizeof(picref->linesize));
92                 pool->refcount++;
93                 return picref;
94             }
95         }
96     } else {
97         pool = link->pool = av_mallocz(sizeof(AVFilterPool));
98         pool->refcount = 1;
99     }
100
101     // align: +2 is needed for swscaler, +16 to be SIMD-friendly
102     if ((i = av_image_alloc(data, linesize, w, h, link->format, 32)) < 0)
103         return NULL;
104
105     picref = avfilter_get_video_buffer_ref_from_arrays(data, linesize,
106                                                        perms, w, h, link->format);
107     if (!picref) {
108         av_free(data[0]);
109         return NULL;
110     }
111
112     memset(data[0], 128, i);
113
114     picref->buf->priv = pool;
115     picref->buf->free = NULL;
116     pool->refcount++;
117
118     return picref;
119 }
120
121 AVFilterBufferRef *
122 avfilter_get_video_buffer_ref_from_arrays(uint8_t * const data[4], const int linesize[4], int perms,
123                                           int w, int h, enum PixelFormat format)
124 {
125     AVFilterBuffer *pic = av_mallocz(sizeof(AVFilterBuffer));
126     AVFilterBufferRef *picref = av_mallocz(sizeof(AVFilterBufferRef));
127
128     if (!pic || !picref)
129         goto fail;
130
131     picref->buf = pic;
132     picref->buf->free = ff_avfilter_default_free_buffer;
133     if (!(picref->video = av_mallocz(sizeof(AVFilterBufferRefVideoProps))))
134         goto fail;
135
136     pic->w = picref->video->w = w;
137     pic->h = picref->video->h = h;
138
139     /* make sure the buffer gets read permission or it's useless for output */
140     picref->perms = perms | AV_PERM_READ;
141
142     pic->refcount = 1;
143     picref->type = AVMEDIA_TYPE_VIDEO;
144     pic->format = picref->format = format;
145
146     memcpy(pic->data,        data,          4*sizeof(data[0]));
147     memcpy(pic->linesize,    linesize,      4*sizeof(linesize[0]));
148     memcpy(picref->data,     pic->data,     sizeof(picref->data));
149     memcpy(picref->linesize, pic->linesize, sizeof(picref->linesize));
150
151     pic->   extended_data = pic->data;
152     picref->extended_data = picref->data;
153
154     return picref;
155
156 fail:
157     if (picref && picref->video)
158         av_free(picref->video);
159     av_free(picref);
160     av_free(pic);
161     return NULL;
162 }
163
164 AVFilterBufferRef *avfilter_get_video_buffer(AVFilterLink *link, int perms, int w, int h)
165 {
166     AVFilterBufferRef *ret = NULL;
167
168     av_unused char buf[16];
169     FF_DPRINTF_START(NULL, get_video_buffer); ff_dlog_link(NULL, link, 0);
170     av_dlog(NULL, " perms:%s w:%d h:%d\n", ff_get_ref_perms_string(buf, sizeof(buf), perms), w, h);
171
172     if (link->dstpad->get_video_buffer)
173         ret = link->dstpad->get_video_buffer(link, perms, w, h);
174
175     if (!ret)
176         ret = avfilter_default_get_video_buffer(link, perms, w, h);
177
178     if (ret)
179         ret->type = AVMEDIA_TYPE_VIDEO;
180
181     FF_DPRINTF_START(NULL, get_video_buffer); ff_dlog_link(NULL, link, 0); av_dlog(NULL, " returning "); ff_dlog_ref(NULL, ret, 1);
182
183     return ret;
184 }
185
186 void avfilter_null_start_frame(AVFilterLink *link, AVFilterBufferRef *picref)
187 {
188     avfilter_start_frame(link->dst->outputs[0], picref);
189 }
190
191 void avfilter_default_start_frame(AVFilterLink *inlink, AVFilterBufferRef *picref)
192 {
193     AVFilterLink *outlink = NULL;
194
195     if (inlink->dst->output_count)
196         outlink = inlink->dst->outputs[0];
197
198     if (outlink) {
199         outlink->out_buf = avfilter_get_video_buffer(outlink, AV_PERM_WRITE, outlink->w, outlink->h);
200         avfilter_copy_buffer_ref_props(outlink->out_buf, picref);
201         avfilter_start_frame(outlink, avfilter_ref_buffer(outlink->out_buf, ~0));
202     }
203 }
204
205 /* XXX: should we do the duplicating of the picture ref here, instead of
206  * forcing the source filter to do it? */
207 void avfilter_start_frame(AVFilterLink *link, AVFilterBufferRef *picref)
208 {
209     void (*start_frame)(AVFilterLink *, AVFilterBufferRef *);
210     AVFilterPad *dst = link->dstpad;
211     int perms = picref->perms;
212     AVFilterCommand *cmd= link->dst->command_queue;
213
214     FF_DPRINTF_START(NULL, start_frame); ff_dlog_link(NULL, link, 0); av_dlog(NULL, " "); ff_dlog_ref(NULL, picref, 1);
215
216     if (!(start_frame = dst->start_frame))
217         start_frame = avfilter_default_start_frame;
218
219     if (picref->linesize[0] < 0)
220         perms |= AV_PERM_NEG_LINESIZES;
221     /* prepare to copy the picture if it has insufficient permissions */
222     if ((dst->min_perms & perms) != dst->min_perms || dst->rej_perms & perms) {
223         av_log(link->dst, AV_LOG_DEBUG,
224                 "frame copy needed (have perms %x, need %x, reject %x)\n",
225                 picref->perms,
226                 link->dstpad->min_perms, link->dstpad->rej_perms);
227
228         link->cur_buf = avfilter_get_video_buffer(link, dst->min_perms, link->w, link->h);
229         link->src_buf = picref;
230         avfilter_copy_buffer_ref_props(link->cur_buf, link->src_buf);
231     }
232     else
233         link->cur_buf = picref;
234
235     while(cmd && cmd->time <= picref->pts * av_q2d(link->time_base)){
236         av_log(link->dst, AV_LOG_DEBUG,
237                "Processing command time:%f command:%s arg:%s\n",
238                cmd->time, cmd->command, cmd->arg);
239         avfilter_process_command(link->dst, cmd->command, cmd->arg, 0, 0, cmd->flags);
240         ff_command_queue_pop(link->dst);
241         cmd= link->dst->command_queue;
242     }
243
244     start_frame(link, link->cur_buf);
245     ff_update_link_current_pts(link, link->cur_buf->pts);
246 }
247
248 void avfilter_null_end_frame(AVFilterLink *link)
249 {
250     avfilter_end_frame(link->dst->outputs[0]);
251 }
252
253 void avfilter_default_end_frame(AVFilterLink *inlink)
254 {
255     AVFilterLink *outlink = NULL;
256
257     if (inlink->dst->output_count)
258         outlink = inlink->dst->outputs[0];
259
260     avfilter_unref_buffer(inlink->cur_buf);
261     inlink->cur_buf = NULL;
262
263     if (outlink) {
264         if (outlink->out_buf) {
265             avfilter_unref_buffer(outlink->out_buf);
266             outlink->out_buf = NULL;
267         }
268         avfilter_end_frame(outlink);
269     }
270 }
271
272 void avfilter_end_frame(AVFilterLink *link)
273 {
274     void (*end_frame)(AVFilterLink *);
275
276     if (!(end_frame = link->dstpad->end_frame))
277         end_frame = avfilter_default_end_frame;
278
279     end_frame(link);
280
281     /* unreference the source picture if we're feeding the destination filter
282      * a copied version dues to permission issues */
283     if (link->src_buf) {
284         avfilter_unref_buffer(link->src_buf);
285         link->src_buf = NULL;
286     }
287 }
288
289 void avfilter_null_draw_slice(AVFilterLink *link, int y, int h, int slice_dir)
290 {
291     avfilter_draw_slice(link->dst->outputs[0], y, h, slice_dir);
292 }
293
294 void avfilter_default_draw_slice(AVFilterLink *inlink, int y, int h, int slice_dir)
295 {
296     AVFilterLink *outlink = NULL;
297
298     if (inlink->dst->output_count)
299         outlink = inlink->dst->outputs[0];
300
301     if (outlink)
302         avfilter_draw_slice(outlink, y, h, slice_dir);
303 }
304
305 void avfilter_draw_slice(AVFilterLink *link, int y, int h, int slice_dir)
306 {
307     uint8_t *src[4], *dst[4];
308     int i, j, vsub;
309     void (*draw_slice)(AVFilterLink *, int, int, int);
310
311     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);
312
313     /* copy the slice if needed for permission reasons */
314     if (link->src_buf) {
315         vsub = av_pix_fmt_descriptors[link->format].log2_chroma_h;
316
317         for (i = 0; i < 4; i++) {
318             if (link->src_buf->data[i]) {
319                 src[i] = link->src_buf-> data[i] +
320                     (y >> (i==1 || i==2 ? vsub : 0)) * link->src_buf-> linesize[i];
321                 dst[i] = link->cur_buf->data[i] +
322                     (y >> (i==1 || i==2 ? vsub : 0)) * link->cur_buf->linesize[i];
323             } else
324                 src[i] = dst[i] = NULL;
325         }
326
327         for (i = 0; i < 4; i++) {
328             int planew =
329                 av_image_get_linesize(link->format, link->cur_buf->video->w, i);
330
331             if (!src[i]) continue;
332
333             for (j = 0; j < h >> (i==1 || i==2 ? vsub : 0); j++) {
334                 memcpy(dst[i], src[i], planew);
335                 src[i] += link->src_buf->linesize[i];
336                 dst[i] += link->cur_buf->linesize[i];
337             }
338         }
339     }
340
341     if (!(draw_slice = link->dstpad->draw_slice))
342         draw_slice = avfilter_default_draw_slice;
343     draw_slice(link, y, h, slice_dir);
344 }
345