]> git.sesse.net Git - ffmpeg/blob - libavfilter/video.c
Add some more missing includes after removing the implicit common.h
[ffmpeg] / libavfilter / video.c
1 /*
2  * This file is part of Libav.
3  *
4  * Libav 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  * Libav 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 Libav; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18
19 #include <string.h>
20 #include <stdio.h>
21
22 #include "libavutil/imgutils.h"
23 #include "libavutil/mem.h"
24
25 #include "avfilter.h"
26 #include "internal.h"
27 #include "video.h"
28
29 #ifdef DEBUG
30 static char *ff_get_ref_perms_string(char *buf, size_t buf_size, int perms)
31 {
32     snprintf(buf, buf_size, "%s%s%s%s%s%s",
33              perms & AV_PERM_READ      ? "r" : "",
34              perms & AV_PERM_WRITE     ? "w" : "",
35              perms & AV_PERM_PRESERVE  ? "p" : "",
36              perms & AV_PERM_REUSE     ? "u" : "",
37              perms & AV_PERM_REUSE2    ? "U" : "",
38              perms & AV_PERM_NEG_LINESIZES ? "n" : "");
39     return buf;
40 }
41 #endif
42
43 static void ff_dlog_ref(void *ctx, AVFilterBufferRef *ref, int end)
44 {
45     av_unused char buf[16];
46     av_dlog(ctx,
47             "ref[%p buf:%p refcount:%d perms:%s data:%p linesize[%d, %d, %d, %d] pts:%"PRId64" pos:%"PRId64,
48             ref, ref->buf, ref->buf->refcount, ff_get_ref_perms_string(buf, sizeof(buf), ref->perms), ref->data[0],
49             ref->linesize[0], ref->linesize[1], ref->linesize[2], ref->linesize[3],
50             ref->pts, ref->pos);
51
52     if (ref->video) {
53         av_dlog(ctx, " a:%d/%d s:%dx%d i:%c iskey:%d type:%c",
54                 ref->video->pixel_aspect.num, ref->video->pixel_aspect.den,
55                 ref->video->w, ref->video->h,
56                 !ref->video->interlaced     ? 'P' :         /* Progressive  */
57                 ref->video->top_field_first ? 'T' : 'B',    /* Top / Bottom */
58                 ref->video->key_frame,
59                 av_get_picture_type_char(ref->video->pict_type));
60     }
61     if (ref->audio) {
62         av_dlog(ctx, " cl:%"PRId64"d n:%d r:%d p:%d",
63                 ref->audio->channel_layout,
64                 ref->audio->nb_samples,
65                 ref->audio->sample_rate,
66                 ref->audio->planar);
67     }
68
69     av_dlog(ctx, "]%s", end ? "\n" : "");
70 }
71
72 AVFilterBufferRef *ff_null_get_video_buffer(AVFilterLink *link, int perms, int w, int h)
73 {
74     return ff_get_video_buffer(link->dst->outputs[0], perms, w, h);
75 }
76
77 /* TODO: set the buffer's priv member to a context structure for the whole
78  * filter chain.  This will allow for a buffer pool instead of the constant
79  * alloc & free cycle currently implemented. */
80 AVFilterBufferRef *ff_default_get_video_buffer(AVFilterLink *link, int perms, int w, int h)
81 {
82     int linesize[4];
83     uint8_t *data[4];
84     AVFilterBufferRef *picref = NULL;
85
86     // +2 is needed for swscaler, +16 to be SIMD-friendly
87     if (av_image_alloc(data, linesize, w, h, link->format, 16) < 0)
88         return NULL;
89
90     picref = avfilter_get_video_buffer_ref_from_arrays(data, linesize,
91                                                        perms, w, h, link->format);
92     if (!picref) {
93         av_free(data[0]);
94         return NULL;
95     }
96
97     return picref;
98 }
99
100 AVFilterBufferRef *
101 avfilter_get_video_buffer_ref_from_arrays(uint8_t *data[4], int linesize[4], int perms,
102                                           int w, int h, enum PixelFormat format)
103 {
104     AVFilterBuffer *pic = av_mallocz(sizeof(AVFilterBuffer));
105     AVFilterBufferRef *picref = av_mallocz(sizeof(AVFilterBufferRef));
106
107     if (!pic || !picref)
108         goto fail;
109
110     picref->buf = pic;
111     picref->buf->free = ff_avfilter_default_free_buffer;
112     if (!(picref->video = av_mallocz(sizeof(AVFilterBufferRefVideoProps))))
113         goto fail;
114
115     pic->w = picref->video->w = w;
116     pic->h = picref->video->h = h;
117
118     /* make sure the buffer gets read permission or it's useless for output */
119     picref->perms = perms | AV_PERM_READ;
120
121     pic->refcount = 1;
122     picref->type = AVMEDIA_TYPE_VIDEO;
123     pic->format = picref->format = format;
124
125     memcpy(pic->data,        data,          4*sizeof(data[0]));
126     memcpy(pic->linesize,    linesize,      4*sizeof(linesize[0]));
127     memcpy(picref->data,     pic->data,     sizeof(picref->data));
128     memcpy(picref->linesize, pic->linesize, sizeof(picref->linesize));
129
130     pic->   extended_data = pic->data;
131     picref->extended_data = picref->data;
132
133     picref->pts = AV_NOPTS_VALUE;
134
135     return picref;
136
137 fail:
138     if (picref && picref->video)
139         av_free(picref->video);
140     av_free(picref);
141     av_free(pic);
142     return NULL;
143 }
144
145 AVFilterBufferRef *ff_get_video_buffer(AVFilterLink *link, int perms, int w, int h)
146 {
147     AVFilterBufferRef *ret = NULL;
148
149     av_unused char buf[16];
150     FF_DPRINTF_START(NULL, get_video_buffer); ff_dlog_link(NULL, link, 0);
151     av_dlog(NULL, " perms:%s w:%d h:%d\n", ff_get_ref_perms_string(buf, sizeof(buf), perms), w, h);
152
153     if (link->dstpad->get_video_buffer)
154         ret = link->dstpad->get_video_buffer(link, perms, w, h);
155
156     if (!ret)
157         ret = ff_default_get_video_buffer(link, perms, w, h);
158
159     if (ret)
160         ret->type = AVMEDIA_TYPE_VIDEO;
161
162     FF_DPRINTF_START(NULL, get_video_buffer); ff_dlog_link(NULL, link, 0); av_dlog(NULL, " returning "); ff_dlog_ref(NULL, ret, 1);
163
164     return ret;
165 }
166
167 int ff_null_start_frame(AVFilterLink *link, AVFilterBufferRef *picref)
168 {
169     AVFilterBufferRef *buf_out = avfilter_ref_buffer(picref, ~0);
170     if (!buf_out)
171         return AVERROR(ENOMEM);
172     return ff_start_frame(link->dst->outputs[0], buf_out);
173 }
174
175 // for filters that support (but don't require) outpic==inpic
176 int ff_inplace_start_frame(AVFilterLink *inlink, AVFilterBufferRef *inpicref)
177 {
178     AVFilterLink *outlink = inlink->dst->outputs[0];
179     AVFilterBufferRef *outpicref = NULL, *for_next_filter;
180     int ret = 0;
181
182     if ((inpicref->perms & AV_PERM_WRITE) && !(inpicref->perms & AV_PERM_PRESERVE)) {
183         outpicref = avfilter_ref_buffer(inpicref, ~0);
184         if (!outpicref)
185             return AVERROR(ENOMEM);
186     } else {
187         outpicref = ff_get_video_buffer(outlink, AV_PERM_WRITE, outlink->w, outlink->h);
188         if (!outpicref)
189             return AVERROR(ENOMEM);
190
191         avfilter_copy_buffer_ref_props(outpicref, inpicref);
192         outpicref->video->w = outlink->w;
193         outpicref->video->h = outlink->h;
194     }
195
196     for_next_filter = avfilter_ref_buffer(outpicref, ~0);
197     if (for_next_filter)
198         ret = ff_start_frame(outlink, for_next_filter);
199     else
200         ret = AVERROR(ENOMEM);
201
202     if (ret < 0) {
203         avfilter_unref_bufferp(&outpicref);
204         return ret;
205     }
206
207     outlink->out_buf = outpicref;
208     return 0;
209 }
210
211 static int default_start_frame(AVFilterLink *inlink, AVFilterBufferRef *picref)
212 {
213     AVFilterLink *outlink = NULL;
214
215     if (inlink->dst->nb_outputs)
216         outlink = inlink->dst->outputs[0];
217
218     if (outlink) {
219         AVFilterBufferRef *buf_out;
220         outlink->out_buf = ff_get_video_buffer(outlink, AV_PERM_WRITE, outlink->w, outlink->h);
221         if (!outlink->out_buf)
222             return AVERROR(ENOMEM);
223
224         avfilter_copy_buffer_ref_props(outlink->out_buf, picref);
225         buf_out = avfilter_ref_buffer(outlink->out_buf, ~0);
226         if (!buf_out)
227             return AVERROR(ENOMEM);
228
229         return ff_start_frame(outlink, buf_out);
230     }
231     return 0;
232 }
233
234 static void clear_link(AVFilterLink *link)
235 {
236     avfilter_unref_bufferp(&link->cur_buf);
237     avfilter_unref_bufferp(&link->src_buf);
238     avfilter_unref_bufferp(&link->out_buf);
239 }
240
241 /* XXX: should we do the duplicating of the picture ref here, instead of
242  * forcing the source filter to do it? */
243 int ff_start_frame(AVFilterLink *link, AVFilterBufferRef *picref)
244 {
245     int (*start_frame)(AVFilterLink *, AVFilterBufferRef *);
246     AVFilterPad *dst = link->dstpad;
247     int ret, perms = picref->perms;
248
249     FF_DPRINTF_START(NULL, start_frame); ff_dlog_link(NULL, link, 0); av_dlog(NULL, " "); ff_dlog_ref(NULL, picref, 1);
250
251     if (!(start_frame = dst->start_frame))
252         start_frame = default_start_frame;
253
254     if (picref->linesize[0] < 0)
255         perms |= AV_PERM_NEG_LINESIZES;
256     /* prepare to copy the picture if it has insufficient permissions */
257     if ((dst->min_perms & perms) != dst->min_perms || dst->rej_perms & perms) {
258         av_log(link->dst, AV_LOG_DEBUG,
259                 "frame copy needed (have perms %x, need %x, reject %x)\n",
260                 picref->perms,
261                 link->dstpad->min_perms, link->dstpad->rej_perms);
262
263         link->cur_buf = ff_get_video_buffer(link, dst->min_perms, link->w, link->h);
264         if (!link->cur_buf) {
265             avfilter_unref_bufferp(&picref);
266             return AVERROR(ENOMEM);
267         }
268
269         link->src_buf = picref;
270         avfilter_copy_buffer_ref_props(link->cur_buf, link->src_buf);
271     }
272     else
273         link->cur_buf = picref;
274
275     ret = start_frame(link, link->cur_buf);
276     if (ret < 0)
277         clear_link(link);
278
279     return ret;
280 }
281
282 int ff_null_end_frame(AVFilterLink *link)
283 {
284     return ff_end_frame(link->dst->outputs[0]);
285 }
286
287 static int default_end_frame(AVFilterLink *inlink)
288 {
289     AVFilterLink *outlink = NULL;
290
291     if (inlink->dst->nb_outputs)
292         outlink = inlink->dst->outputs[0];
293
294     if (outlink) {
295         return ff_end_frame(outlink);
296     }
297     return 0;
298 }
299
300 int ff_end_frame(AVFilterLink *link)
301 {
302     int (*end_frame)(AVFilterLink *);
303     int ret;
304
305     if (!(end_frame = link->dstpad->end_frame))
306         end_frame = default_end_frame;
307
308     ret = end_frame(link);
309
310     clear_link(link);
311
312     return ret;
313 }
314
315 int ff_null_draw_slice(AVFilterLink *link, int y, int h, int slice_dir)
316 {
317     return ff_draw_slice(link->dst->outputs[0], y, h, slice_dir);
318 }
319
320 static int default_draw_slice(AVFilterLink *inlink, int y, int h, int slice_dir)
321 {
322     AVFilterLink *outlink = NULL;
323
324     if (inlink->dst->nb_outputs)
325         outlink = inlink->dst->outputs[0];
326
327     if (outlink)
328         return ff_draw_slice(outlink, y, h, slice_dir);
329     return 0;
330 }
331
332 int ff_draw_slice(AVFilterLink *link, int y, int h, int slice_dir)
333 {
334     uint8_t *src[4], *dst[4];
335     int i, j, vsub, ret;
336     int (*draw_slice)(AVFilterLink *, int, int, int);
337
338     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);
339
340     /* copy the slice if needed for permission reasons */
341     if (link->src_buf) {
342         vsub = av_pix_fmt_descriptors[link->format].log2_chroma_h;
343
344         for (i = 0; i < 4; i++) {
345             if (link->src_buf->data[i]) {
346                 src[i] = link->src_buf-> data[i] +
347                     (y >> (i==1 || i==2 ? vsub : 0)) * link->src_buf-> linesize[i];
348                 dst[i] = link->cur_buf->data[i] +
349                     (y >> (i==1 || i==2 ? vsub : 0)) * link->cur_buf->linesize[i];
350             } else
351                 src[i] = dst[i] = NULL;
352         }
353
354         for (i = 0; i < 4; i++) {
355             int planew =
356                 av_image_get_linesize(link->format, link->cur_buf->video->w, i);
357
358             if (!src[i]) continue;
359
360             for (j = 0; j < h >> (i==1 || i==2 ? vsub : 0); j++) {
361                 memcpy(dst[i], src[i], planew);
362                 src[i] += link->src_buf->linesize[i];
363                 dst[i] += link->cur_buf->linesize[i];
364             }
365         }
366     }
367
368     if (!(draw_slice = link->dstpad->draw_slice))
369         draw_slice = default_draw_slice;
370     ret = draw_slice(link, y, h, slice_dir);
371     if (ret < 0)
372         clear_link(link);
373     return ret;
374 }