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