]> git.sesse.net Git - ffmpeg/blob - libavfilter/video.c
avconv: reindent
[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 "libavutil/imgutils.h"
20
21 #include "avfilter.h"
22 #include "internal.h"
23
24 #ifdef DEBUG
25 static char *ff_get_ref_perms_string(char *buf, size_t buf_size, int perms)
26 {
27     snprintf(buf, buf_size, "%s%s%s%s%s%s",
28              perms & AV_PERM_READ      ? "r" : "",
29              perms & AV_PERM_WRITE     ? "w" : "",
30              perms & AV_PERM_PRESERVE  ? "p" : "",
31              perms & AV_PERM_REUSE     ? "u" : "",
32              perms & AV_PERM_REUSE2    ? "U" : "",
33              perms & AV_PERM_NEG_LINESIZES ? "n" : "");
34     return buf;
35 }
36 #endif
37
38 static void ff_dlog_ref(void *ctx, AVFilterBufferRef *ref, int end)
39 {
40     av_unused char buf[16];
41     av_dlog(ctx,
42             "ref[%p buf:%p refcount:%d perms:%s data:%p linesize[%d, %d, %d, %d] pts:%"PRId64" pos:%"PRId64,
43             ref, ref->buf, ref->buf->refcount, ff_get_ref_perms_string(buf, sizeof(buf), ref->perms), ref->data[0],
44             ref->linesize[0], ref->linesize[1], ref->linesize[2], ref->linesize[3],
45             ref->pts, ref->pos);
46
47     if (ref->video) {
48         av_dlog(ctx, " a:%d/%d s:%dx%d i:%c iskey:%d type:%c",
49                 ref->video->pixel_aspect.num, ref->video->pixel_aspect.den,
50                 ref->video->w, ref->video->h,
51                 !ref->video->interlaced     ? 'P' :         /* Progressive  */
52                 ref->video->top_field_first ? 'T' : 'B',    /* Top / Bottom */
53                 ref->video->key_frame,
54                 av_get_picture_type_char(ref->video->pict_type));
55     }
56     if (ref->audio) {
57         av_dlog(ctx, " cl:%"PRId64"d n:%d r:%d p:%d",
58                 ref->audio->channel_layout,
59                 ref->audio->nb_samples,
60                 ref->audio->sample_rate,
61                 ref->audio->planar);
62     }
63
64     av_dlog(ctx, "]%s", end ? "\n" : "");
65 }
66
67 AVFilterBufferRef *avfilter_null_get_video_buffer(AVFilterLink *link, int perms, int w, int h)
68 {
69     return avfilter_get_video_buffer(link->dst->outputs[0], perms, w, h);
70 }
71
72 /* TODO: set the buffer's priv member to a context structure for the whole
73  * filter chain.  This will allow for a buffer pool instead of the constant
74  * alloc & free cycle currently implemented. */
75 AVFilterBufferRef *avfilter_default_get_video_buffer(AVFilterLink *link, int perms, int w, int h)
76 {
77     int linesize[4];
78     uint8_t *data[4];
79     AVFilterBufferRef *picref = NULL;
80
81     // +2 is needed for swscaler, +16 to be SIMD-friendly
82     if (av_image_alloc(data, linesize, w, h, link->format, 16) < 0)
83         return NULL;
84
85     picref = avfilter_get_video_buffer_ref_from_arrays(data, linesize,
86                                                        perms, w, h, link->format);
87     if (!picref) {
88         av_free(data[0]);
89         return NULL;
90     }
91
92     return picref;
93 }
94
95 AVFilterBufferRef *
96 avfilter_get_video_buffer_ref_from_arrays(uint8_t *data[4], int linesize[4], int perms,
97                                           int w, int h, enum PixelFormat format)
98 {
99     AVFilterBuffer *pic = av_mallocz(sizeof(AVFilterBuffer));
100     AVFilterBufferRef *picref = av_mallocz(sizeof(AVFilterBufferRef));
101
102     if (!pic || !picref)
103         goto fail;
104
105     picref->buf = pic;
106     picref->buf->free = ff_avfilter_default_free_buffer;
107     if (!(picref->video = av_mallocz(sizeof(AVFilterBufferRefVideoProps))))
108         goto fail;
109
110     pic->w = picref->video->w = w;
111     pic->h = picref->video->h = h;
112
113     /* make sure the buffer gets read permission or it's useless for output */
114     picref->perms = perms | AV_PERM_READ;
115
116     pic->refcount = 1;
117     picref->type = AVMEDIA_TYPE_VIDEO;
118     pic->format = picref->format = format;
119
120     memcpy(pic->data,        data,          4*sizeof(data[0]));
121     memcpy(pic->linesize,    linesize,      4*sizeof(linesize[0]));
122     memcpy(picref->data,     pic->data,     sizeof(picref->data));
123     memcpy(picref->linesize, pic->linesize, sizeof(picref->linesize));
124
125     pic->   extended_data = pic->data;
126     picref->extended_data = picref->data;
127
128     picref->pts = AV_NOPTS_VALUE;
129
130     return picref;
131
132 fail:
133     if (picref && picref->video)
134         av_free(picref->video);
135     av_free(picref);
136     av_free(pic);
137     return NULL;
138 }
139
140 AVFilterBufferRef *avfilter_get_video_buffer(AVFilterLink *link, int perms, int w, int h)
141 {
142     AVFilterBufferRef *ret = NULL;
143
144     av_unused char buf[16];
145     FF_DPRINTF_START(NULL, get_video_buffer); ff_dlog_link(NULL, link, 0);
146     av_dlog(NULL, " perms:%s w:%d h:%d\n", ff_get_ref_perms_string(buf, sizeof(buf), perms), w, h);
147
148     if (link->dstpad->get_video_buffer)
149         ret = link->dstpad->get_video_buffer(link, perms, w, h);
150
151     if (!ret)
152         ret = avfilter_default_get_video_buffer(link, perms, w, h);
153
154     if (ret)
155         ret->type = AVMEDIA_TYPE_VIDEO;
156
157     FF_DPRINTF_START(NULL, get_video_buffer); ff_dlog_link(NULL, link, 0); av_dlog(NULL, " returning "); ff_dlog_ref(NULL, ret, 1);
158
159     return ret;
160 }
161
162 void avfilter_null_start_frame(AVFilterLink *link, AVFilterBufferRef *picref)
163 {
164     avfilter_start_frame(link->dst->outputs[0], picref);
165 }
166
167 void avfilter_default_start_frame(AVFilterLink *inlink, AVFilterBufferRef *picref)
168 {
169     AVFilterLink *outlink = NULL;
170
171     if (inlink->dst->output_count)
172         outlink = inlink->dst->outputs[0];
173
174     if (outlink) {
175         outlink->out_buf = avfilter_get_video_buffer(outlink, AV_PERM_WRITE, outlink->w, outlink->h);
176         avfilter_copy_buffer_ref_props(outlink->out_buf, picref);
177         avfilter_start_frame(outlink, avfilter_ref_buffer(outlink->out_buf, ~0));
178     }
179 }
180
181 /* XXX: should we do the duplicating of the picture ref here, instead of
182  * forcing the source filter to do it? */
183 void avfilter_start_frame(AVFilterLink *link, AVFilterBufferRef *picref)
184 {
185     void (*start_frame)(AVFilterLink *, AVFilterBufferRef *);
186     AVFilterPad *dst = link->dstpad;
187     int perms = picref->perms;
188
189     FF_DPRINTF_START(NULL, start_frame); ff_dlog_link(NULL, link, 0); av_dlog(NULL, " "); ff_dlog_ref(NULL, picref, 1);
190
191     if (!(start_frame = dst->start_frame))
192         start_frame = avfilter_default_start_frame;
193
194     if (picref->linesize[0] < 0)
195         perms |= AV_PERM_NEG_LINESIZES;
196     /* prepare to copy the picture if it has insufficient permissions */
197     if ((dst->min_perms & perms) != dst->min_perms || dst->rej_perms & perms) {
198         av_log(link->dst, AV_LOG_DEBUG,
199                 "frame copy needed (have perms %x, need %x, reject %x)\n",
200                 picref->perms,
201                 link->dstpad->min_perms, link->dstpad->rej_perms);
202
203         link->cur_buf = avfilter_get_video_buffer(link, dst->min_perms, link->w, link->h);
204         link->src_buf = picref;
205         avfilter_copy_buffer_ref_props(link->cur_buf, link->src_buf);
206     }
207     else
208         link->cur_buf = picref;
209
210     start_frame(link, link->cur_buf);
211 }
212
213 void avfilter_null_end_frame(AVFilterLink *link)
214 {
215     avfilter_end_frame(link->dst->outputs[0]);
216 }
217
218 void avfilter_default_end_frame(AVFilterLink *inlink)
219 {
220     AVFilterLink *outlink = NULL;
221
222     if (inlink->dst->output_count)
223         outlink = inlink->dst->outputs[0];
224
225     avfilter_unref_buffer(inlink->cur_buf);
226     inlink->cur_buf = NULL;
227
228     if (outlink) {
229         if (outlink->out_buf) {
230             avfilter_unref_buffer(outlink->out_buf);
231             outlink->out_buf = NULL;
232         }
233         avfilter_end_frame(outlink);
234     }
235 }
236
237 void avfilter_end_frame(AVFilterLink *link)
238 {
239     void (*end_frame)(AVFilterLink *);
240
241     if (!(end_frame = link->dstpad->end_frame))
242         end_frame = avfilter_default_end_frame;
243
244     end_frame(link);
245
246     /* unreference the source picture if we're feeding the destination filter
247      * a copied version dues to permission issues */
248     if (link->src_buf) {
249         avfilter_unref_buffer(link->src_buf);
250         link->src_buf = NULL;
251     }
252 }
253
254 void avfilter_null_draw_slice(AVFilterLink *link, int y, int h, int slice_dir)
255 {
256     avfilter_draw_slice(link->dst->outputs[0], y, h, slice_dir);
257 }
258
259 void avfilter_default_draw_slice(AVFilterLink *inlink, int y, int h, int slice_dir)
260 {
261     AVFilterLink *outlink = NULL;
262
263     if (inlink->dst->output_count)
264         outlink = inlink->dst->outputs[0];
265
266     if (outlink)
267         avfilter_draw_slice(outlink, y, h, slice_dir);
268 }
269
270 void avfilter_draw_slice(AVFilterLink *link, int y, int h, int slice_dir)
271 {
272     uint8_t *src[4], *dst[4];
273     int i, j, vsub;
274     void (*draw_slice)(AVFilterLink *, int, int, int);
275
276     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);
277
278     /* copy the slice if needed for permission reasons */
279     if (link->src_buf) {
280         vsub = av_pix_fmt_descriptors[link->format].log2_chroma_h;
281
282         for (i = 0; i < 4; i++) {
283             if (link->src_buf->data[i]) {
284                 src[i] = link->src_buf-> data[i] +
285                     (y >> (i==1 || i==2 ? vsub : 0)) * link->src_buf-> linesize[i];
286                 dst[i] = link->cur_buf->data[i] +
287                     (y >> (i==1 || i==2 ? vsub : 0)) * link->cur_buf->linesize[i];
288             } else
289                 src[i] = dst[i] = NULL;
290         }
291
292         for (i = 0; i < 4; i++) {
293             int planew =
294                 av_image_get_linesize(link->format, link->cur_buf->video->w, i);
295
296             if (!src[i]) continue;
297
298             for (j = 0; j < h >> (i==1 || i==2 ? vsub : 0); j++) {
299                 memcpy(dst[i], src[i], planew);
300                 src[i] += link->src_buf->linesize[i];
301                 dst[i] += link->cur_buf->linesize[i];
302             }
303         }
304     }
305
306     if (!(draw_slice = link->dstpad->draw_slice))
307         draw_slice = avfilter_default_draw_slice;
308     draw_slice(link, y, h, slice_dir);
309 }
310