libavfilter/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 
34 void ff_avfilter_default_free_buffer(AVFilterBuffer *ptr)
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 
42 static void copy_video_props(AVFilterBufferRefVideoProps *dst, AVFilterBufferRefVideoProps *src) {
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 
51 AVFilterBufferRef *avfilter_ref_buffer(AVFilterBufferRef *ref, int pmask)
52 {
53  AVFilterBufferRef *ret = av_malloc(sizeof(AVFilterBufferRef));
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) {
62  ret->video = av_malloc(sizeof(AVFilterBufferRefVideoProps));
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) {
70  ret->audio = av_malloc(sizeof(AVFilterBufferRefAudioProps));
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) {
78  int nb_channels = av_get_channel_layout_nb_channels(ref->audio->channel_layout);
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 
95 void avfilter_unref_buffer(AVFilterBufferRef *ref)
96 {
97  if (!ref)
98  return;
99  av_assert0(ref->buf->refcount > 0);
100  if (!(--ref->buf->refcount))
101  ref->buf->free(ref->buf);
102  if (ref->extended_data != ref->data)
103  av_freep(&ref->extended_data);
104  if (ref->video)
105  av_freep(&ref->video->qp_table);
106  av_freep(&ref->video);
107  av_freep(&ref->audio);
108  av_dict_free(&ref->metadata);
109  av_free(ref);
110 }
111 
112 void avfilter_unref_bufferp(AVFilterBufferRef **ref)
113 {
114  avfilter_unref_buffer(*ref);
115  *ref = NULL;
116 }
117 
118 int avfilter_copy_frame_props(AVFilterBufferRef *dst, const AVFrame *src)
119 {
120  dst->pts = src->pts;
121  dst->pos = av_frame_get_pkt_pos(src);
122  dst->format = src->format;
123 
124  av_dict_free(&dst->metadata);
125  av_dict_copy(&dst->metadata, av_frame_get_metadata(src), 0);
126 
127  switch (dst->type) {
128  case AVMEDIA_TYPE_VIDEO:
129  dst->video->w = src->width;
130  dst->video->h = src->height;
131  dst->video->sample_aspect_ratio = src->sample_aspect_ratio;
132  dst->video->interlaced = src->interlaced_frame;
133  dst->video->top_field_first = src->top_field_first;
134  dst->video->key_frame = src->key_frame;
135  dst->video->pict_type = src->pict_type;
136  break;
137  case AVMEDIA_TYPE_AUDIO:
138  dst->audio->sample_rate = src->sample_rate;
139  dst->audio->channel_layout = src->channel_layout;
140  break;
141  default:
142  return AVERROR(EINVAL);
143  }
144 
145  return 0;
146 }
147 
148 void avfilter_copy_buffer_ref_props(AVFilterBufferRef *dst, AVFilterBufferRef *src)
149 {
150  // copy common properties
151  dst->pts = src->pts;
152  dst->pos = src->pos;
153 
154  switch (src->type) {
155  case AVMEDIA_TYPE_VIDEO: {
156  if (dst->video->qp_table)
157  av_freep(&dst->video->qp_table);
158  copy_video_props(dst->video, src->video);
159  break;
160  }
161  case AVMEDIA_TYPE_AUDIO: *dst->audio = *src->audio; break;
162  default: break;
163  }
164 
165  av_dict_free(&dst->metadata);
166  av_dict_copy(&dst->metadata, src->metadata, 0);
167 }
This structure describes decoded (raw) audio or video data.
Definition: frame.h:76
FIXME Range Coding of cr are ref
Definition: snow.txt:367
misc image utilities
external API header
static void copy_video_props(AVFilterBufferRefVideoProps *dst, AVFilterBufferRefVideoProps *src)
void avfilter_unref_buffer(AVFilterBufferRef *ref)
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:198
void ff_avfilter_default_free_buffer(AVFilterBuffer *ptr)
default handler for freeing audio/video buffer when there are no references left
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition: frame.h:159
int interlaced_frame
The content of the picture is interlaced.
Definition: frame.h:270
void av_dict_copy(AVDictionary **dst, AVDictionary *src, int flags)
Copy entries from one AVDictionary struct into another.
Definition: dict.c:176
int width
width and height of the video frame
Definition: frame.h:122
void av_free(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc(). ...
Definition: mem.c:183
void av_dict_free(AVDictionary **pm)
Free all the memory allocated for an AVDictionary struct and all keys and values. ...
Definition: dict.c:162
int av_get_channel_layout_nb_channels(uint64_t channel_layout)
Return the number of channels in the channel layout.
simple assert() macros that are a bit more flexible than ISO C assert().
external API header
uint64_t channel_layout
Channel layout of the audio data.
Definition: frame.h:331
audio channel layout utility functions
enum AVPictureType pict_type
Picture type of the frame.
Definition: frame.h:144
ret
Definition: avfilter.c:821
void avfilter_copy_buffer_ref_props(AVFilterBufferRef *dst, AVFilterBufferRef *src)
int format
format of the frame, -1 if unknown or unset Values correspond to enum AVPixelFormat for video frames...
Definition: frame.h:134
NULL
Definition: eval.c:55
AVS_Value src
Definition: avisynth_c.h:523
AVRational sample_aspect_ratio
Sample aspect ratio for the video frame, 0/1 if unknown/unspecified.
Definition: frame.h:154
AVDictionary * av_frame_get_metadata(const AVFrame *frame)
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:73
int sample_rate
Sample rate of the audio data.
Definition: frame.h:326
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFilterBuffer structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later.That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another.Buffer references ownership and permissions
common internal and external API header
int avfilter_copy_frame_props(AVFilterBufferRef *dst, const AVFrame *src)
int top_field_first
If the content is interlaced, is top field displayed first.
Definition: frame.h:275
else dst[i][x+y *dst_stride[i]]
Definition: vf_mcdeint.c:160
int key_frame
1 -> keyframe, 0-> not
Definition: frame.h:139
int64_t av_frame_get_pkt_pos(const AVFrame *frame)
int height
Definition: frame.h:122
AVFilterBufferRef * avfilter_ref_buffer(AVFilterBufferRef *ref, int pmask)
int nb_channels
internal API functions
void avfilter_unref_bufferp(AVFilterBufferRef **ref)