h264: on reference overflow, reset the reference count to 0, not 1.
[FFMpeg-mirror/mplayer-patches.git] / libavfilter / video.c
blobcb68ca41625336d091f3b061bf383ae6b488fa8c
1 /*
2 * This file is part of Libav.
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.
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.
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
19 #include <string.h>
20 #include <stdio.h>
22 #include "libavutil/imgutils.h"
23 #include "libavutil/mem.h"
25 #include "avfilter.h"
26 #include "internal.h"
27 #include "video.h"
29 #ifdef DEBUG
30 static char *ff_get_ref_perms_string(char *buf, size_t buf_size, int perms)
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;
41 #endif
43 static void ff_dlog_ref(void *ctx, AVFilterBufferRef *ref, int end)
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);
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));
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);
69 av_dlog(ctx, "]%s", end ? "\n" : "");
72 AVFilterBufferRef *ff_null_get_video_buffer(AVFilterLink *link, int perms, int w, int h)
74 return ff_get_video_buffer(link->dst->outputs[0], perms, w, h);
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)
82 int linesize[4];
83 uint8_t *data[4];
84 AVFilterBufferRef *picref = NULL;
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;
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;
97 return picref;
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)
104 AVFilterBuffer *pic = av_mallocz(sizeof(AVFilterBuffer));
105 AVFilterBufferRef *picref = av_mallocz(sizeof(AVFilterBufferRef));
107 if (!pic || !picref)
108 goto fail;
110 picref->buf = pic;
111 picref->buf->free = ff_avfilter_default_free_buffer;
112 if (!(picref->video = av_mallocz(sizeof(AVFilterBufferRefVideoProps))))
113 goto fail;
115 pic->w = picref->video->w = w;
116 pic->h = picref->video->h = h;
118 /* make sure the buffer gets read permission or it's useless for output */
119 picref->perms = perms | AV_PERM_READ;
121 pic->refcount = 1;
122 picref->type = AVMEDIA_TYPE_VIDEO;
123 pic->format = picref->format = format;
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));
130 pic-> extended_data = pic->data;
131 picref->extended_data = picref->data;
133 picref->pts = AV_NOPTS_VALUE;
135 return picref;
137 fail:
138 if (picref && picref->video)
139 av_free(picref->video);
140 av_free(picref);
141 av_free(pic);
142 return NULL;
145 AVFilterBufferRef *ff_get_video_buffer(AVFilterLink *link, int perms, int w, int h)
147 AVFilterBufferRef *ret = NULL;
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);
153 if (link->dstpad->get_video_buffer)
154 ret = link->dstpad->get_video_buffer(link, perms, w, h);
156 if (!ret)
157 ret = ff_default_get_video_buffer(link, perms, w, h);
159 if (ret)
160 ret->type = AVMEDIA_TYPE_VIDEO;
162 FF_DPRINTF_START(NULL, get_video_buffer); ff_dlog_link(NULL, link, 0); av_dlog(NULL, " returning "); ff_dlog_ref(NULL, ret, 1);
164 return ret;