Libav
buffer.c
Go to the documentation of this file.
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/channel_layout.h"
20 #include "libavutil/common.h"
21 #include "libavutil/internal.h"
22 #include "libavcodec/avcodec.h"
23 
24 #include "avfilter.h"
25 #include "internal.h"
26 #include "version.h"
27 
28 #if FF_API_AVFILTERBUFFER
29 /* TODO: buffer pool. see comment for avfilter_default_get_video_buffer() */
30 void ff_avfilter_default_free_buffer(AVFilterBuffer *ptr)
31 {
32  if (ptr->extended_data != ptr->data)
33  av_freep(&ptr->extended_data);
34  av_free(ptr->data[0]);
35  av_free(ptr);
36 }
37 
38 AVFilterBufferRef *avfilter_ref_buffer(AVFilterBufferRef *ref, int pmask)
39 {
40  AVFilterBufferRef *ret = av_malloc(sizeof(AVFilterBufferRef));
41  if (!ret)
42  return NULL;
43  *ret = *ref;
44  if (ref->type == AVMEDIA_TYPE_VIDEO) {
45  ret->video = av_malloc(sizeof(AVFilterBufferRefVideoProps));
46  if (!ret->video) {
47  av_free(ret);
48  return NULL;
49  }
50  *ret->video = *ref->video;
51  ret->extended_data = ret->data;
52  } else if (ref->type == AVMEDIA_TYPE_AUDIO) {
53  ret->audio = av_malloc(sizeof(AVFilterBufferRefAudioProps));
54  if (!ret->audio) {
55  av_free(ret);
56  return NULL;
57  }
58  *ret->audio = *ref->audio;
59 
60  if (ref->extended_data != ref->data) {
61  int nb_channels = av_get_channel_layout_nb_channels(ref->audio->channel_layout);
62  if (!(ret->extended_data = av_malloc(sizeof(*ret->extended_data) *
63  nb_channels))) {
64  av_freep(&ret->audio);
65  av_freep(&ret);
66  return NULL;
67  }
68  memcpy(ret->extended_data, ref->extended_data,
69  sizeof(*ret->extended_data) * nb_channels);
70  } else
71  ret->extended_data = ret->data;
72  }
73  ret->perms &= pmask;
74  ret->buf->refcount ++;
75  return ret;
76 }
77 
78 void avfilter_unref_buffer(AVFilterBufferRef *ref)
79 {
80  if (!ref)
81  return;
82  if (!(--ref->buf->refcount))
83  ref->buf->free(ref->buf);
84  if (ref->extended_data != ref->data)
85  av_freep(&ref->extended_data);
86  av_free(ref->video);
87  av_free(ref->audio);
88  av_free(ref);
89 }
90 
91 void avfilter_unref_bufferp(AVFilterBufferRef **ref)
92 {
96  *ref = NULL;
97 }
98 
99 int avfilter_copy_frame_props(AVFilterBufferRef *dst, const AVFrame *src)
100 {
101  dst->pts = src->pts;
102  dst->format = src->format;
103 
104  switch (dst->type) {
105  case AVMEDIA_TYPE_VIDEO:
106  dst->video->w = src->width;
107  dst->video->h = src->height;
108  dst->video->pixel_aspect = src->sample_aspect_ratio;
109  dst->video->interlaced = src->interlaced_frame;
110  dst->video->top_field_first = src->top_field_first;
111  dst->video->key_frame = src->key_frame;
112  dst->video->pict_type = src->pict_type;
113  break;
114  case AVMEDIA_TYPE_AUDIO:
115  dst->audio->sample_rate = src->sample_rate;
116  dst->audio->channel_layout = src->channel_layout;
117  break;
118  default:
119  return AVERROR(EINVAL);
120  }
121 
122  return 0;
123 }
124 
125 int avfilter_copy_buf_props(AVFrame *dst, const AVFilterBufferRef *src)
126 {
127  int planes, nb_channels;
128 
129  memcpy(dst->data, src->data, sizeof(dst->data));
130  memcpy(dst->linesize, src->linesize, sizeof(dst->linesize));
131 
132  dst->pts = src->pts;
133  dst->format = src->format;
134 
135  switch (src->type) {
136  case AVMEDIA_TYPE_VIDEO:
137  dst->width = src->video->w;
138  dst->height = src->video->h;
139  dst->sample_aspect_ratio = src->video->pixel_aspect;
140  dst->interlaced_frame = src->video->interlaced;
141  dst->top_field_first = src->video->top_field_first;
142  dst->key_frame = src->video->key_frame;
143  dst->pict_type = src->video->pict_type;
144  break;
145  case AVMEDIA_TYPE_AUDIO:
146  nb_channels = av_get_channel_layout_nb_channels(src->audio->channel_layout);
147  planes = av_sample_fmt_is_planar(src->format) ? nb_channels : 1;
148 
149  if (planes > FF_ARRAY_ELEMS(dst->data)) {
150  dst->extended_data = av_mallocz(planes * sizeof(*dst->extended_data));
151  if (!dst->extended_data)
152  return AVERROR(ENOMEM);
153  memcpy(dst->extended_data, src->extended_data,
154  planes * sizeof(*dst->extended_data));
155  } else
156  dst->extended_data = dst->data;
157 
158  dst->sample_rate = src->audio->sample_rate;
159  dst->channel_layout = src->audio->channel_layout;
160  dst->nb_samples = src->audio->nb_samples;
161  break;
162  default:
163  return AVERROR(EINVAL);
164  }
165 
166  return 0;
167 }
168 
169 void avfilter_copy_buffer_ref_props(AVFilterBufferRef *dst, AVFilterBufferRef *src)
170 {
171  // copy common properties
172  dst->pts = src->pts;
173  dst->pos = src->pos;
174 
175  switch (src->type) {
176  case AVMEDIA_TYPE_VIDEO: *dst->video = *src->video; break;
177  case AVMEDIA_TYPE_AUDIO: *dst->audio = *src->audio; break;
178  default: break;
179  }
180 }
181 #endif /* FF_API_AVFILTERBUFFER */
This structure describes decoded (raw) audio or video data.
Definition: frame.h:135
uint8_t ** extended_data
pointers to the data planes/channels.
Definition: frame.h:169
Libavfilter version macros.
void avfilter_unref_buffer(AVFilterBufferRef *ref)
Definition: buffer.c:78
void av_freep(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
Definition: mem.c:198
void ff_avfilter_default_free_buffer(AVFilterBuffer *ptr)
default handler for freeing audio/video buffer when there are no references left
Definition: buffer.c:30
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition: frame.h:211
int interlaced_frame
The content of the picture is interlaced.
Definition: frame.h:320
int av_sample_fmt_is_planar(enum AVSampleFormat sample_fmt)
Check if the sample format is planar.
Definition: samplefmt.c:101
int width
width and height of the video frame
Definition: frame.h:174
void av_free(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc(). ...
Definition: mem.c:186
#define AVERROR(e)
Definition: error.h:43
int av_get_channel_layout_nb_channels(uint64_t channel_layout)
Return the number of channels in the channel layout.
uint64_t channel_layout
Channel layout of the audio data.
Definition: frame.h:381
common internal API header
enum AVPictureType pict_type
Picture type of the frame.
Definition: frame.h:196
void * av_malloc(size_t size) av_malloc_attrib 1(1)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:62
#define FF_ARRAY_ELEMS(a)
Definition: common.h:61
void avfilter_copy_buffer_ref_props(AVFilterBufferRef *dst, AVFilterBufferRef *src)
Definition: buffer.c:169
Main libavfilter public API header.
int format
format of the frame, -1 if unknown or unset Values correspond to enum AVPixelFormat for video frames...
Definition: frame.h:186
NULL
Definition: eval.c:55
AVRational sample_aspect_ratio
Sample aspect ratio for the video frame, 0/1 if unknown/unspecified.
Definition: frame.h:206
int sample_rate
Sample rate of the audio data.
Definition: frame.h:376
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:153
#define FF_DISABLE_DEPRECATION_WARNINGS
Definition: internal.h:76
int avfilter_copy_frame_props(AVFilterBufferRef *dst, const AVFrame *src)
Definition: buffer.c:99
#define FF_ENABLE_DEPRECATION_WARNINGS
Definition: internal.h:77
int top_field_first
If the content is interlaced, is top field displayed first.
Definition: frame.h:325
int key_frame
1 -> keyframe, 0-> not
Definition: frame.h:191
int avfilter_copy_buf_props(AVFrame *dst, const AVFilterBufferRef *src)
Definition: buffer.c:125
int height
Definition: frame.h:174
AVFilterBufferRef * avfilter_ref_buffer(AVFilterBufferRef *ref, int pmask)
Definition: buffer.c:38
int nb_channels
internal API functions
int nb_samples
number of audio samples (per channel) described by this frame
Definition: frame.h:179
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:141
void avfilter_unref_bufferp(AVFilterBufferRef **ref)
Definition: buffer.c:91
void * av_mallocz(size_t size) av_malloc_attrib 1(1)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:205