FFmpeg
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
video.c
Go to the documentation of this file.
1 /*
2  * Copyright 2007 Bobby Bingham
3  * Copyright Stefano Sabatini <stefasab gmail com>
4  * Copyright Vitor Sessak <vitor1001 gmail com>
5  *
6  * This file is part of FFmpeg.
7  *
8  * FFmpeg is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * FFmpeg is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with FFmpeg; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22 
23 #include <string.h>
24 #include <stdio.h>
25 
26 #include "libavutil/avassert.h"
27 #include "libavutil/imgutils.h"
28 #include "libavutil/mem.h"
29 
30 #include "avfilter.h"
31 #include "internal.h"
32 #include "video.h"
33 
35 {
36  return ff_get_video_buffer(link->dst->outputs[0], perms, w, h);
37 }
38 
40 {
41  int linesize[4];
42  uint8_t *data[4];
43  int i;
44  AVFilterBufferRef *picref = NULL;
45  AVFilterPool *pool = link->pool;
46  int full_perms = AV_PERM_READ | AV_PERM_WRITE | AV_PERM_PRESERVE |
48 
49  av_assert1(!(perms & ~(full_perms | AV_PERM_NEG_LINESIZES)));
50 
51  if (pool) {
52  for (i = 0; i < POOL_SIZE; i++) {
53  picref = pool->pic[i];
54  if (picref && picref->buf->format == link->format && picref->buf->w == w && picref->buf->h == h) {
55  AVFilterBuffer *pic = picref->buf;
56  pool->pic[i] = NULL;
57  pool->count--;
58  av_assert0(!picref->video->qp_table);
59  picref->video->w = w;
60  picref->video->h = h;
61  picref->perms = full_perms;
62  picref->format = link->format;
63  pic->refcount = 1;
64  memcpy(picref->data, pic->data, sizeof(picref->data));
65  memcpy(picref->linesize, pic->linesize, sizeof(picref->linesize));
66  pool->refcount++;
67  return picref;
68  }
69  }
70  } else {
71  pool = link->pool = av_mallocz(sizeof(AVFilterPool));
72  pool->refcount = 1;
73  }
74 
75  // align: +2 is needed for swscaler, +16 to be SIMD-friendly
76  if ((i = av_image_alloc(data, linesize, w, h, link->format, 32)) < 0)
77  return NULL;
78 
79  picref = avfilter_get_video_buffer_ref_from_arrays(data, linesize,
80  full_perms, w, h, link->format);
81  if (!picref) {
82  av_free(data[0]);
83  return NULL;
84  }
85 
86  memset(data[0], 128, i);
87 
88  picref->buf->priv = pool;
89  picref->buf->free = NULL;
90  pool->refcount++;
91 
92  return picref;
93 }
94 
96 avfilter_get_video_buffer_ref_from_arrays(uint8_t * const data[4], const int linesize[4], int perms,
97  int w, int h, enum AVPixelFormat format)
98 {
101 
102  if (!pic || !picref)
103  goto fail;
104 
105  picref->buf = pic;
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 *ff_get_video_buffer(AVFilterLink *link, int perms, int w, int h)
141 {
142  AVFilterBufferRef *ret = NULL;
143 
144  av_unused char buf[16];
146  ff_tlog(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 = ff_default_get_video_buffer(link, perms, w, h);
153 
154  if (ret)
155  ret->type = AVMEDIA_TYPE_VIDEO;
156 
157  FF_TPRINTF_START(NULL, get_video_buffer); ff_tlog_link(NULL, link, 0); ff_tlog(NULL, " returning "); ff_tlog_ref(NULL, ret, 1);
158 
159  return ret;
160 }