FFmpeg
buffer.c
Go to the documentation of this file.
1 /*
2  * Copyright Stefano Sabatini <stefasab gmail com>
3  * Copyright Anton Khirnov <anton khirnov net>
4  * Copyright Michael Niedermayer <michaelni gmx at>
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 
24 #include "libavutil/avassert.h"
25 #include "libavutil/common.h"
26 #include "libavutil/imgutils.h"
27 #include "libavcodec/avcodec.h"
28 
29 #include "avfilter.h"
30 #include "internal.h"
31 #include "audio.h"
32 #include "avcodec.h"
33 
35 {
36  if (ptr->extended_data != ptr->data)
37  av_freep(&ptr->extended_data);
38  av_free(ptr->data[0]);
39  av_free(ptr);
40 }
41 
43  *dst = *src;
44  if (src->qp_table) {
45  int qsize = src->qp_table_size;
46  dst->qp_table = av_malloc(qsize);
47  memcpy(dst->qp_table, src->qp_table, qsize);
48  }
49 }
50 
52 {
54  if (!ret)
55  return NULL;
56  *ret = *ref;
57 
58  ret->metadata = NULL;
59  av_dict_copy(&ret->metadata, ref->metadata, 0);
60 
61  if (ref->type == AVMEDIA_TYPE_VIDEO) {
63  if (!ret->video) {
64  av_free(ret);
65  return NULL;
66  }
67  copy_video_props(ret->video, ref->video);
68  ret->extended_data = ret->data;
69  } else if (ref->type == AVMEDIA_TYPE_AUDIO) {
71  if (!ret->audio) {
72  av_free(ret);
73  return NULL;
74  }
75  *ret->audio = *ref->audio;
76 
77  if (ref->extended_data && ref->extended_data != ref->data) {
79  if (!(ret->extended_data = av_malloc(sizeof(*ret->extended_data) *
80  nb_channels))) {
81  av_freep(&ret->audio);
82  av_freep(&ret);
83  return NULL;
84  }
85  memcpy(ret->extended_data, ref->extended_data,
86  sizeof(*ret->extended_data) * nb_channels);
87  } else
88  ret->extended_data = ret->data;
89  }
90  ret->perms &= pmask;
91  ret->buf->refcount ++;
92  return ret;
93 }
94 
96 {
97  int i;
98 
99  av_assert0(pool->refcount > 0);
100 
101  for (i = 0; i < POOL_SIZE; i++) {
102  if (pool->pic[i]) {
103  AVFilterBufferRef *picref = pool->pic[i];
104  /* free buffer: picrefs stored in the pool are not
105  * supposed to contain a free callback */
106  av_assert0(!picref->buf->refcount);
107  av_freep(&picref->buf->data[0]);
108  av_freep(&picref->buf);
109 
110  av_freep(&picref->audio);
111  av_assert0(!picref->video || !picref->video->qp_table);
112  av_freep(&picref->video);
113  av_freep(&pool->pic[i]);
114  pool->count--;
115  }
116  }
117  pool->draining = 1;
118 
119  if (!--pool->refcount) {
120  av_assert0(!pool->count);
121  av_free(pool);
122  }
123 }
124 
126 {
127  int i;
128  AVFilterPool *pool= ref->buf->priv;
129 
130  av_assert0(ref->buf->data[0]);
131  av_assert0(pool->refcount>0);
132 
133  if (ref->video)
134  av_freep(&ref->video->qp_table);
135 
136  if (pool->count == POOL_SIZE) {
137  AVFilterBufferRef *ref1 = pool->pic[0];
138  av_freep(&ref1->video);
139  av_freep(&ref1->audio);
140  av_freep(&ref1->buf->data[0]);
141  av_freep(&ref1->buf);
142  av_free(ref1);
143  memmove(&pool->pic[0], &pool->pic[1], sizeof(void*)*(POOL_SIZE-1));
144  pool->count--;
145  pool->pic[POOL_SIZE-1] = NULL;
146  }
147 
148  for (i = 0; i < POOL_SIZE; i++) {
149  if (!pool->pic[i]) {
150  pool->pic[i] = ref;
151  pool->count++;
152  break;
153  }
154  }
155  if (pool->draining) {
156  ff_free_pool(pool);
157  } else
158  --pool->refcount;
159 }
160 
162 {
163  if (!ref)
164  return;
165  av_assert0(ref->buf->refcount > 0);
166  if (!(--ref->buf->refcount)) {
167  if (!ref->buf->free) {
168  store_in_pool(ref);
169  return;
170  }
171  ref->buf->free(ref->buf);
172  }
173  if (ref->extended_data != ref->data)
174  av_freep(&ref->extended_data);
175  if (ref->video)
176  av_freep(&ref->video->qp_table);
177  av_freep(&ref->video);
178  av_freep(&ref->audio);
179  av_dict_free(&ref->metadata);
180  av_free(ref);
181 }
182 
184 {
185  avfilter_unref_buffer(*ref);
186  *ref = NULL;
187 }
188 
190 {
191  // copy common properties
192  dst->pts = src->pts;
193  dst->pos = src->pos;
194 
195  switch (src->type) {
196  case AVMEDIA_TYPE_VIDEO: {
197  if (dst->video->qp_table)
198  av_freep(&dst->video->qp_table);
199  copy_video_props(dst->video, src->video);
200  break;
201  }
202  case AVMEDIA_TYPE_AUDIO: *dst->audio = *src->audio; break;
203  default: break;
204  }
205 
206  av_dict_free(&dst->metadata);
207  av_dict_copy(&dst->metadata, src->metadata, 0);
208 }
209 
211  AVFilterBufferRef *ref)
212 {
213  AVFilterBufferRef *buf;
214  int channels;
215 
216  switch (outlink->type) {
217 
218  case AVMEDIA_TYPE_VIDEO:
219  buf = ff_get_video_buffer(outlink, AV_PERM_WRITE,
220  ref->video->w, ref->video->h);
221  if(!buf)
222  return NULL;
223  av_image_copy(buf->data, buf->linesize,
224  (void*)ref->data, ref->linesize,
225  ref->format, ref->video->w, ref->video->h);
226  break;
227 
228  case AVMEDIA_TYPE_AUDIO:
229  buf = ff_get_audio_buffer(outlink, AV_PERM_WRITE,
230  ref->audio->nb_samples);
231  if(!buf)
232  return NULL;
233  channels = ref->audio->channels;
235  0, 0, ref->audio->nb_samples,
236  channels,
237  ref->format);
238  break;
239 
240  default:
241  return NULL;
242  }
244  return buf;
245 }