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 
20 #include "libavutil/common.h"
21 #include "libavcodec/avcodec.h"
22 
23 #include "avfilter.h"
24 #include "internal.h"
25 
26 /* TODO: buffer pool. see comment for avfilter_default_get_video_buffer() */
28 {
29  if (ptr->extended_data != ptr->data)
30  av_freep(&ptr->extended_data);
31  av_free(ptr->data[0]);
32  av_free(ptr);
33 }
34 
36 {
38  if (!ret)
39  return NULL;
40  *ret = *ref;
41  if (ref->type == AVMEDIA_TYPE_VIDEO) {
43  if (!ret->video) {
44  av_free(ret);
45  return NULL;
46  }
47  *ret->video = *ref->video;
48  ret->extended_data = ret->data;
49  } else if (ref->type == AVMEDIA_TYPE_AUDIO) {
51  if (!ret->audio) {
52  av_free(ret);
53  return NULL;
54  }
55  *ret->audio = *ref->audio;
56 
57  if (ref->extended_data != ref->data) {
59  if (!(ret->extended_data = av_malloc(sizeof(*ret->extended_data) *
60  nb_channels))) {
61  av_freep(&ret->audio);
62  av_freep(&ret);
63  return NULL;
64  }
65  memcpy(ret->extended_data, ref->extended_data,
66  sizeof(*ret->extended_data) * nb_channels);
67  } else
68  ret->extended_data = ret->data;
69  }
70  ret->perms &= pmask;
71  ret->buf->refcount ++;
72  return ret;
73 }
74 
76 {
77  if (!ref)
78  return;
79  if (!(--ref->buf->refcount))
80  ref->buf->free(ref->buf);
81  if (ref->extended_data != ref->data)
82  av_freep(&ref->extended_data);
83  av_free(ref->video);
84  av_free(ref->audio);
85  av_free(ref);
86 }
87 
89 {
91  *ref = NULL;
92 }
93 
95 {
96  dst->pts = src->pts;
97  dst->format = src->format;
98 
99  switch (dst->type) {
100  case AVMEDIA_TYPE_VIDEO:
101  dst->video->w = src->width;
102  dst->video->h = src->height;
104  dst->video->interlaced = src->interlaced_frame;
106  dst->video->key_frame = src->key_frame;
107  dst->video->pict_type = src->pict_type;
108  break;
109  case AVMEDIA_TYPE_AUDIO:
110  dst->audio->sample_rate = src->sample_rate;
111  dst->audio->channel_layout = src->channel_layout;
112  break;
113  default:
114  return AVERROR(EINVAL);
115  }
116 
117  return 0;
118 }
119 
121 {
122  int planes, nb_channels;
123 
124  memcpy(dst->data, src->data, sizeof(dst->data));
125  memcpy(dst->linesize, src->linesize, sizeof(dst->linesize));
126 
127  dst->pts = src->pts;
128  dst->format = src->format;
129 
130  switch (src->type) {
131  case AVMEDIA_TYPE_VIDEO:
132  dst->width = src->video->w;
133  dst->height = src->video->h;
135  dst->interlaced_frame = src->video->interlaced;
137  dst->key_frame = src->video->key_frame;
138  dst->pict_type = src->video->pict_type;
139  break;
140  case AVMEDIA_TYPE_AUDIO:
142  planes = av_sample_fmt_is_planar(src->format) ? nb_channels : 1;
143 
144  if (planes > FF_ARRAY_ELEMS(dst->data)) {
145  dst->extended_data = av_mallocz(planes * sizeof(*dst->extended_data));
146  if (!dst->extended_data)
147  return AVERROR(ENOMEM);
148  memcpy(dst->extended_data, src->extended_data,
149  planes * sizeof(*dst->extended_data));
150  } else
151  dst->extended_data = dst->data;
152 
153  dst->sample_rate = src->audio->sample_rate;
154  dst->channel_layout = src->audio->channel_layout;
155  dst->nb_samples = src->audio->nb_samples;
156  break;
157  default:
158  return AVERROR(EINVAL);
159  }
160 
161  return 0;
162 }
163 
165 {
166  // copy common properties
167  dst->pts = src->pts;
168  dst->pos = src->pos;
169 
170  switch (src->type) {
171  case AVMEDIA_TYPE_VIDEO: *dst->video = *src->video; break;
172  case AVMEDIA_TYPE_AUDIO: *dst->audio = *src->audio; break;
173  default: break;
174  }
175 }
uint8_t * data[8]
buffer data for each plane/channel
Definition: avfilter.h:63
uint8_t ** extended_data
pointers to the data planes/channels.
Definition: avfilter.h:156
void * av_malloc(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:61
This structure describes decoded (raw) audio or video data.
Definition: avcodec.h:989
int nb_samples
number of audio samples
Definition: avfilter.h:111
AVFilterBufferRefAudioProps * audio
audio buffer specific properties
Definition: avfilter.h:160
AVFilterBufferRefVideoProps * video
video buffer specific properties
Definition: avfilter.h:159
int linesize[8]
number of bytes per line
Definition: avfilter.h:157
enum AVMediaType type
media type of buffer data
Definition: avfilter.h:174
Audio specific properties in a reference to an AVFilterBuffer.
Definition: avfilter.h:109
A reference-counted buffer data type used by the filter system.
Definition: avfilter.h:62
int avfilter_copy_buf_props(AVFrame *dst, const AVFilterBufferRef *src)
Copy the frame properties and data pointers of src to dst, without copying the actual data...
Definition: buffer.c:120
void avfilter_copy_buffer_ref_props(AVFilterBufferRef *dst, AVFilterBufferRef *src)
Copy properties of src to dst, without copying the actual data.
Definition: buffer.c:164
void av_freep(void *arg)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
Definition: mem.c:151
void avfilter_unref_bufferp(AVFilterBufferRef **ref)
Remove a reference to a buffer and set the pointer to NULL.
Definition: buffer.c:88
int top_field_first
field order
Definition: avfilter.h:126
int64_t pts
presentation timestamp in time_base units (time when frame should be shown to user) If AV_NOPTS_VALUE...
Definition: avcodec.h:1088
int key_frame
1 -> keyframe, 0-> not
Definition: avfilter.h:128
int interlaced_frame
The content of the picture is interlaced.
Definition: avcodec.h:1232
AVRational pixel_aspect
pixel aspect ratio
Definition: avfilter.h:124
int64_t pts
presentation timestamp.
Definition: avfilter.h:167
int width
width and height of the video frame
Definition: avcodec.h:1035
void av_free(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc(). ...
Definition: mem.c:139
int h
image height
Definition: avfilter.h:123
AVFilterBuffer * buf
the buffer that this is a reference to
Definition: avfilter.h:140
int av_get_channel_layout_nb_channels(uint64_t channel_layout)
Return the number of channels in the channel layout.
Video specific properties in a reference to an AVFilterBuffer.
Definition: avfilter.h:121
uint64_t channel_layout
Channel layout of the audio data.
Definition: avcodec.h:1318
void ff_avfilter_default_free_buffer(AVFilterBuffer *ptr)
default handler for freeing audio/video buffer when there are no references left
Definition: buffer.c:27
audio channel layout utility functions
enum AVPictureType pict_type
Picture type of the frame, see ?_TYPE below.
Definition: avcodec.h:1065
unsigned refcount
number of references to this buffer
Definition: avfilter.h:94
AVFilterBufferRef * avfilter_ref_buffer(AVFilterBufferRef *ref, int pmask)
Add a new reference to a buffer.
Definition: buffer.c:35
void(* free)(struct AVFilterBuffer *buf)
A pointer to the function to deallocate this buffer if the default function is not sufficient...
Definition: avfilter.h:90
int format
format of the frame, -1 if unknown or unset Values correspond to enum AVPixelFormat for video frames...
Definition: avcodec.h:1051
A reference to an AVFilterBuffer.
Definition: avfilter.h:139
NULL
Definition: eval.c:52
external API header
int linesize[AV_NUM_DATA_POINTERS]
Size, in bytes, of the data for each picture/channel plane.
Definition: avcodec.h:1008
int perms
permissions, see the AV_PERM_* flags
Definition: avfilter.h:172
int sample_rate
audio buffer sample rate
Definition: avfilter.h:112
AVRational sample_aspect_ratio
sample aspect ratio for the video frame, 0/1 if unknown/unspecified
Definition: avcodec.h:1080
enum AVPictureType pict_type
picture type of the frame
Definition: avfilter.h:127
int sample_rate
Sample rate of the audio data.
Definition: avcodec.h:1310
int av_sample_fmt_is_planar(enum AVSampleFormat sample_fmt)
Check if the sample format is planar.
Definition: samplefmt.c:101
uint8_t ** extended_data
pointers to the data planes/channels.
Definition: avfilter.h:79
uint64_t channel_layout
channel layout of audio buffer
Definition: avfilter.h:110
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: avcodec.h:997
int interlaced
is frame interlaced
Definition: avfilter.h:125
common internal and external API header
int64_t pos
byte position in stream, -1 if unknown
Definition: avfilter.h:168
int top_field_first
If the content is interlaced, is top field displayed first.
Definition: avcodec.h:1239
uint8_t * data[8]
picture/audio data for each plane
Definition: avfilter.h:141
int key_frame
1 -> keyframe, 0-> not
Definition: avcodec.h:1058
int format
media format
Definition: avfilter.h:170
int height
Definition: avcodec.h:1035
void avfilter_unref_buffer(AVFilterBufferRef *ref)
Remove a reference to a buffer.
Definition: buffer.c:75
int nb_channels
internal API functions
uint8_t ** extended_data
pointers to the data planes/channels.
Definition: avcodec.h:1028
int nb_samples
number of audio samples (per channel) described by this frame
Definition: avcodec.h:1042
void * av_mallocz(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:158
int avfilter_copy_frame_props(AVFilterBufferRef *dst, const AVFrame *src)
Copy the frame properties of src to dst, without copying the actual image data.
Definition: buffer.c:94