defaults.c
Go to the documentation of this file.
1 /*
2  * Filter layer - default implementations
3  * Copyright (c) 2007 Bobby Bingham
4  *
5  * This file is part of Libav.
6  *
7  * Libav is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * Libav is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with Libav; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
22 #include "libavutil/audioconvert.h"
23 #include "libavutil/imgutils.h"
24 #include "libavutil/samplefmt.h"
25 #include "avfilter.h"
26 #include "internal.h"
27 
28 /* TODO: buffer pool. see comment for avfilter_default_get_video_buffer() */
30 {
31  av_free(ptr->data[0]);
32  av_free(ptr);
33 }
34 
35 /* TODO: set the buffer's priv member to a context structure for the whole
36  * filter chain. This will allow for a buffer pool instead of the constant
37  * alloc & free cycle currently implemented. */
39 {
40  int linesize[4];
41  uint8_t *data[4];
42  AVFilterBufferRef *picref = NULL;
43 
44  // +2 is needed for swscaler, +16 to be SIMD-friendly
45  if (av_image_alloc(data, linesize, w, h, link->format, 16) < 0)
46  return NULL;
47 
48  picref = avfilter_get_video_buffer_ref_from_arrays(data, linesize,
49  perms, w, h, link->format);
50  if (!picref) {
51  av_free(data[0]);
52  return NULL;
53  }
54 
55  return picref;
56 }
57 
59  enum AVSampleFormat sample_fmt, int size,
60  uint64_t channel_layout, int planar)
61 {
63  AVFilterBufferRef *ref = NULL;
64  int i, sample_size, chans_nb, bufsize, per_channel_size, step_size = 0;
65  char *buf;
66 
67  if (!samples || !(ref = av_mallocz(sizeof(AVFilterBufferRef))))
68  goto fail;
69 
70  ref->buf = samples;
71  ref->format = sample_fmt;
72 
74  if (!ref->audio)
75  goto fail;
76 
77  ref->audio->channel_layout = channel_layout;
78  ref->audio->size = size;
79  ref->audio->planar = planar;
80 
81  /* make sure the buffer gets read permission or it's useless for output */
82  ref->perms = perms | AV_PERM_READ;
83 
84  samples->refcount = 1;
86 
87  sample_size = av_get_bytes_per_sample(sample_fmt);
88  chans_nb = av_get_channel_layout_nb_channels(channel_layout);
89 
90  per_channel_size = size/chans_nb;
91  ref->audio->nb_samples = per_channel_size/sample_size;
92 
93  /* Set the number of bytes to traverse to reach next sample of a particular channel:
94  * For planar, this is simply the sample size.
95  * For packed, this is the number of samples * sample_size.
96  */
97  for (i = 0; i < chans_nb; i++)
98  samples->linesize[i] = planar > 0 ? per_channel_size : sample_size;
99  memset(&samples->linesize[chans_nb], 0, (8-chans_nb) * sizeof(samples->linesize[0]));
100 
101  /* Calculate total buffer size, round to multiple of 16 to be SIMD friendly */
102  bufsize = (size + 15)&~15;
103  buf = av_malloc(bufsize);
104  if (!buf)
105  goto fail;
106 
107  /* For planar, set the start point of each channel's data within the buffer
108  * For packed, set the start point of the entire buffer only
109  */
110  samples->data[0] = buf;
111  if (buf && planar) {
112  for (i = 1; i < chans_nb; i++) {
113  step_size += per_channel_size;
114  samples->data[i] = buf + step_size;
115  }
116  } else {
117  for (i = 1; i < chans_nb; i++)
118  samples->data[i] = buf;
119  }
120 
121  memset(&samples->data[chans_nb], 0, (8-chans_nb) * sizeof(samples->data[0]));
122 
123  memcpy(ref->data, samples->data, sizeof(ref->data));
124  memcpy(ref->linesize, samples->linesize, sizeof(ref->linesize));
125 
126  return ref;
127 
128 fail:
129  if (ref)
130  av_free(ref->audio);
131  av_free(ref);
132  av_free(samples);
133  return NULL;
134 }
135 
137 {
138  AVFilterLink *outlink = NULL;
139 
140  if (inlink->dst->output_count)
141  outlink = inlink->dst->outputs[0];
142 
143  if (outlink) {
144  outlink->out_buf = avfilter_get_video_buffer(outlink, AV_PERM_WRITE, outlink->w, outlink->h);
145  avfilter_copy_buffer_ref_props(outlink->out_buf, picref);
146  avfilter_start_frame(outlink, avfilter_ref_buffer(outlink->out_buf, ~0));
147  }
148 }
149 
150 void avfilter_default_draw_slice(AVFilterLink *inlink, int y, int h, int slice_dir)
151 {
152  AVFilterLink *outlink = NULL;
153 
154  if (inlink->dst->output_count)
155  outlink = inlink->dst->outputs[0];
156 
157  if (outlink)
158  avfilter_draw_slice(outlink, y, h, slice_dir);
159 }
160 
162 {
163  AVFilterLink *outlink = NULL;
164 
165  if (inlink->dst->output_count)
166  outlink = inlink->dst->outputs[0];
167 
169  inlink->cur_buf = NULL;
170 
171  if (outlink) {
172  if (outlink->out_buf) {
173  avfilter_unref_buffer(outlink->out_buf);
174  outlink->out_buf = NULL;
175  }
176  avfilter_end_frame(outlink);
177  }
178 }
179 
180 /* FIXME: samplesref is same as link->cur_buf. Need to consider removing the redundant parameter. */
182 {
183  AVFilterLink *outlink = NULL;
184 
185  if (inlink->dst->output_count)
186  outlink = inlink->dst->outputs[0];
187 
188  if (outlink) {
189  outlink->out_buf = avfilter_default_get_audio_buffer(inlink, AV_PERM_WRITE, samplesref->format,
190  samplesref->audio->size,
191  samplesref->audio->channel_layout,
192  samplesref->audio->planar);
193  outlink->out_buf->pts = samplesref->pts;
194  outlink->out_buf->audio->sample_rate = samplesref->audio->sample_rate;
195  avfilter_filter_samples(outlink, avfilter_ref_buffer(outlink->out_buf, ~0));
196  avfilter_unref_buffer(outlink->out_buf);
197  outlink->out_buf = NULL;
198  }
199  avfilter_unref_buffer(samplesref);
200  inlink->cur_buf = NULL;
201 }
202 
207 {
208  if (link->src->input_count && link->src->inputs[0]) {
209  if (link->type == AVMEDIA_TYPE_VIDEO) {
210  link->w = link->src->inputs[0]->w;
211  link->h = link->src->inputs[0]->h;
212  link->time_base = link->src->inputs[0]->time_base;
213  } else if (link->type == AVMEDIA_TYPE_AUDIO) {
214  link->channel_layout = link->src->inputs[0]->channel_layout;
215  link->sample_rate = link->src->inputs[0]->sample_rate;
216  }
217  } else {
218  /* XXX: any non-simple filter which would cause this branch to be taken
219  * really should implement its own config_props() for this link. */
220  return -1;
221  }
222 
223  return 0;
224 }
225 
235 {
236  int count = 0, i;
237 
238  for (i = 0; i < ctx->input_count; i++) {
239  if (ctx->inputs[i]) {
240  avfilter_formats_ref(formats, &ctx->inputs[i]->out_formats);
241  count++;
242  }
243  }
244  for (i = 0; i < ctx->output_count; i++) {
245  if (ctx->outputs[i]) {
246  avfilter_formats_ref(formats, &ctx->outputs[i]->in_formats);
247  count++;
248  }
249  }
250 
251  if (!count) {
252  av_free(formats->formats);
253  av_free(formats->refs);
254  av_free(formats);
255  }
256 }
257 
259 {
260  enum AVMediaType type = ctx->inputs && ctx->inputs [0] ? ctx->inputs [0]->type :
261  ctx->outputs && ctx->outputs[0] ? ctx->outputs[0]->type :
263 
265  return 0;
266 }
267 
269 {
270  avfilter_start_frame(link->dst->outputs[0], picref);
271 }
272 
273 void avfilter_null_draw_slice(AVFilterLink *link, int y, int h, int slice_dir)
274 {
275  avfilter_draw_slice(link->dst->outputs[0], y, h, slice_dir);
276 }
277 
279 {
280  avfilter_end_frame(link->dst->outputs[0]);
281 }
282 
284 {
285  avfilter_filter_samples(link->dst->outputs[0], samplesref);
286 }
287 
289 {
290  return avfilter_get_video_buffer(link->dst->outputs[0], perms, w, h);
291 }
292 
294  enum AVSampleFormat sample_fmt, int size,
295  uint64_t channel_layout, int packed)
296 {
297  return avfilter_get_audio_buffer(link->dst->outputs[0], perms, sample_fmt,
298  size, channel_layout, packed);
299 }
300 
uint8_t * data[8]
buffer data for each plane/channel
Definition: avfilter.h:62
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:154
AVFilterBufferRef * avfilter_get_video_buffer(AVFilterLink *link, int perms, int w, int h)
Request a picture buffer with a specific set of permissions.
Definition: avfilter.c:289
int size
audio buffer size
Definition: avfilter.h:96
int size
static short * samples
Definition: ffmpeg.c:233
int nb_samples
number of audio samples
Definition: avfilter.h:95
AVFilterBufferRefAudioProps * audio
audio buffer specific properties
Definition: avfilter.h:142
int linesize[8]
number of bytes per line
Definition: avfilter.h:127
misc image utilities
static void avfilter_copy_buffer_ref_props(AVFilterBufferRef *dst, AVFilterBufferRef *src)
Copy properties of src to dst, without copying the actual data.
Definition: avfilter.h:148
AVFilterFormats * avfilter_all_formats(enum AVMediaType type)
Return a list of all formats supported by Libav for the given media type.
Definition: formats.c:124
Audio specific properties in a reference to an AVFilterBuffer.
Definition: avfilter.h:93
A reference-counted buffer data type used by the filter system.
Definition: avfilter.h:61
void avfilter_null_draw_slice(AVFilterLink *link, int y, int h, int slice_dir)
draw_slice() handler for filters which simply pass video along
Definition: defaults.c:273
void ff_avfilter_default_free_buffer(AVFilterBuffer *ptr)
default handler for freeing audio/video buffer when there are no references left
Definition: defaults.c:29
#define AV_PERM_READ
can read from the buffer
Definition: avfilter.h:81
AVFilterLink ** inputs
array of pointers to input links
Definition: avfilter.h:547
int linesize[8]
number of bytes per line
Definition: avfilter.h:63
void avfilter_null_filter_samples(AVFilterLink *link, AVFilterBufferRef *samplesref)
filter_samples() handler for filters which simply pass audio along
Definition: defaults.c:283
void avfilter_end_frame(AVFilterLink *link)
Notifie the next filter that the current frame has finished.
Definition: avfilter.c:430
struct AVFilterFormats *** refs
references to this list
Definition: avfilter.h:225
unsigned input_count
number of input pads
Definition: avfilter.h:545
const char data[16]
Definition: mxf.c:60
void avfilter_default_filter_samples(AVFilterLink *inlink, AVFilterBufferRef *samplesref)
default handler for filter_samples() for audio inputs
Definition: defaults.c:181
int64_t pts
presentation timestamp.
Definition: avfilter.h:135
audio conversion routines
void av_free(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc(). ...
Definition: mem.c:137
AVFilterBuffer * buf
the buffer that this is a reference to
Definition: avfilter.h:125
int av_get_channel_layout_nb_channels(uint64_t channel_layout)
Return the number of channels in the channel layout.
Definition: audioconvert.c:126
AVFilterBufferRef * avfilter_default_get_video_buffer(AVFilterLink *link, int perms, int w, int h)
default handler for get_video_buffer() for video inputs
Definition: defaults.c:38
void avfilter_default_start_frame(AVFilterLink *inlink, AVFilterBufferRef *picref)
default handler for start_frame() for video inputs
Definition: defaults.c:136
unsigned output_count
number of output pads
Definition: avfilter.h:549
int av_image_alloc(uint8_t *pointers[4], int linesizes[4], int w, int h, enum PixelFormat pix_fmt, int align)
Allocate an image with size w and h and pixel format pix_fmt, and fill pointers and linesizes accordi...
Definition: imgutils.c:176
AVFilterBufferRef * avfilter_get_audio_buffer(AVFilterLink *link, int perms, enum AVSampleFormat sample_fmt, int size, uint64_t channel_layout, int planar)
Request an audio samples buffer with a specific set of permissions.
Definition: avfilter.c:351
int avfilter_default_config_output_link(AVFilterLink *link)
default config_link() implementation for output video links to simplify the implementation of one inp...
Definition: defaults.c:206
unsigned refcount
number of references to this buffer
Definition: avfilter.h:65
void avfilter_null_start_frame(AVFilterLink *link, AVFilterBufferRef *picref)
start_frame() handler for filters which simply pass video along
Definition: defaults.c:268
void avfilter_draw_slice(AVFilterLink *link, int y, int h, int slice_dir)
Send a slice to the next filter.
Definition: avfilter.c:447
void avfilter_null_end_frame(AVFilterLink *link)
end_frame() handler for filters which simply pass video along
Definition: defaults.c:278
void(* free)(struct AVFilterBuffer *buf)
A pointer to the function to deallocate this buffer if the default function is not sufficient...
Definition: avfilter.h:75
int av_get_bytes_per_sample(enum AVSampleFormat sample_fmt)
Return number of bytes per sample.
Definition: samplefmt.c:75
void avfilter_unref_buffer(AVFilterBufferRef *ref)
Remove a reference to a buffer.
Definition: avfilter.c:73
A reference to an AVFilterBuffer.
Definition: avfilter.h:124
NULL
Definition: eval.c:50
int avfilter_default_query_formats(AVFilterContext *ctx)
Default handler for query_formats()
Definition: defaults.c:258
int perms
permissions, see the AV_PERM_* flags
Definition: avfilter.h:138
uint32_t sample_rate
audio buffer sample rate
Definition: avfilter.h:97
AVFilterBufferRef * avfilter_null_get_video_buffer(AVFilterLink *link, int perms, int w, int h)
get_video_buffer() handler for filters which simply pass video along
Definition: defaults.c:288
void avfilter_formats_ref(AVFilterFormats *formats, AVFilterFormats **ref)
Add *ref as a new reference to formats.
Definition: formats.c:139
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:64
AVFilterBufferRef * avfilter_ref_buffer(AVFilterBufferRef *ref, int pmask)
Add a new reference to a buffer.
Definition: avfilter.c:47
void avfilter_start_frame(AVFilterLink *link, AVFilterBufferRef *picref)
Notify the next filter of the start of a frame.
Definition: avfilter.c:400
AVMediaType
Definition: avutil.h:228
AVFilterBufferRef * avfilter_get_video_buffer_ref_from_arrays(uint8_t *data[4], int linesize[4], int perms, int w, int h, enum PixelFormat format)
Create a buffer reference wrapped around an already allocated image buffer.
Definition: avfilter.c:312
uint64_t channel_layout
channel layout of audio buffer
Definition: avfilter.h:94
AVFilterLink ** outputs
array of pointers to output links
Definition: avfilter.h:551
int planar
audio buffer - planar or packed
Definition: avfilter.h:98
AVFilterBufferRef * avfilter_default_get_audio_buffer(AVFilterLink *link, int perms, enum AVSampleFormat sample_fmt, int size, uint64_t channel_layout, int planar)
default handler for get_audio_buffer() for audio inputs
Definition: defaults.c:58
void avfilter_default_draw_slice(AVFilterLink *inlink, int y, int h, int slice_dir)
default handler for draw_slice() for video inputs
Definition: defaults.c:150
AVSampleFormat
all in native-endian format
Definition: samplefmt.h:27
void avfilter_default_end_frame(AVFilterLink *inlink)
default handler for end_frame() for video inputs
Definition: defaults.c:161
#define AV_PERM_WRITE
can write to the buffer
Definition: avfilter.h:82
AVFilterBufferRef * avfilter_null_get_audio_buffer(AVFilterLink *link, int perms, enum AVSampleFormat sample_fmt, int size, uint64_t channel_layout, int packed)
get_audio_buffer() handler for filters which simply pass audio along
Definition: defaults.c:293
void avfilter_set_common_formats(AVFilterContext *ctx, AVFilterFormats *formats)
A helper for query_formats() which sets all links to the same list of formats.
Definition: defaults.c:234
uint8_t * data[8]
picture/audio data for each plane
Definition: avfilter.h:126
int format
media format
Definition: avfilter.h:128
A list of supported formats for one end of a filter link.
Definition: avfilter.h:220
An instance of a filter.
Definition: avfilter.h:538
internal API functions
void avfilter_filter_samples(AVFilterLink *link, AVFilterBufferRef *samplesref)
Send a buffer of audio samples to the next filter.
Definition: avfilter.c:488
int * formats
list of media formats
Definition: avfilter.h:222