]> git.sesse.net Git - ffmpeg/blob - libavfilter/video.c
dsputil: Move copy_block functions to a separate header
[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 AVPixelFormat 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 }