avcodec.c
Go to the documentation of this file.
1 /*
2  * Copyright 2011 Stefano Sabatini | stefasab at gmail.com
3  *
4  * This file is part of FFmpeg.
5  *
6  * FFmpeg is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * FFmpeg is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with FFmpeg; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 
21 /**
22  * @file
23  * libavcodec/libavfilter gluing utilities
24  */
25 
26 #include "avcodec.h"
27 #include "libavutil/avassert.h"
29 #include "libavutil/opt.h"
30 
31 #if FF_API_AVFILTERBUFFER
32 AVFilterBufferRef *avfilter_get_video_buffer_ref_from_frame(const AVFrame *frame,
33  int perms)
34 {
35  AVFilterBufferRef *picref =
36  avfilter_get_video_buffer_ref_from_arrays(frame->data, frame->linesize, perms,
37  frame->width, frame->height,
38  frame->format);
39  if (!picref)
40  return NULL;
41  if (avfilter_copy_frame_props(picref, frame) < 0) {
42  picref->buf->data[0] = NULL;
43  avfilter_unref_bufferp(&picref);
44  }
45  return picref;
46 }
47 
48 AVFilterBufferRef *avfilter_get_audio_buffer_ref_from_frame(const AVFrame *frame,
49  int perms)
50 {
51  AVFilterBufferRef *samplesref;
52  int channels = av_frame_get_channels(frame);
53  int64_t layout = av_frame_get_channel_layout(frame);
54 
55  if (layout && av_get_channel_layout_nb_channels(layout) != av_frame_get_channels(frame)) {
56  av_log(0, AV_LOG_ERROR, "Layout indicates a different number of channels than actually present\n");
57  return NULL;
58  }
59 
60  samplesref = avfilter_get_audio_buffer_ref_from_arrays_channels(
61  (uint8_t **)frame->extended_data, frame->linesize[0], perms,
62  frame->nb_samples, frame->format, channels, layout);
63  if (!samplesref)
64  return NULL;
65  if (avfilter_copy_frame_props(samplesref, frame) < 0) {
66  samplesref->buf->data[0] = NULL;
67  avfilter_unref_bufferp(&samplesref);
68  }
69  return samplesref;
70 }
71 
72 AVFilterBufferRef *avfilter_get_buffer_ref_from_frame(enum AVMediaType type,
73  const AVFrame *frame,
74  int perms)
75 {
76  switch (type) {
77  case AVMEDIA_TYPE_VIDEO:
78  return avfilter_get_video_buffer_ref_from_frame(frame, perms);
79  case AVMEDIA_TYPE_AUDIO:
80  return avfilter_get_audio_buffer_ref_from_frame(frame, perms);
81  default:
82  return NULL;
83  }
84 }
85 
86 int avfilter_copy_buf_props(AVFrame *dst, const AVFilterBufferRef *src)
87 {
88  int planes, nb_channels;
89 
90  if (!dst)
91  return AVERROR(EINVAL);
92  /* abort in case the src is NULL and dst is not, avoid inconsistent state in dst */
93  av_assert0(src);
94 
95  memcpy(dst->data, src->data, sizeof(dst->data));
96  memcpy(dst->linesize, src->linesize, sizeof(dst->linesize));
97 
98  dst->pts = src->pts;
99  dst->format = src->format;
100  av_frame_set_pkt_pos(dst, src->pos);
101 
102  switch (src->type) {
103  case AVMEDIA_TYPE_VIDEO:
104  av_assert0(src->video);
105  dst->width = src->video->w;
106  dst->height = src->video->h;
107  dst->sample_aspect_ratio = src->video->sample_aspect_ratio;
108  dst->interlaced_frame = src->video->interlaced;
109  dst->top_field_first = src->video->top_field_first;
110  dst->key_frame = src->video->key_frame;
111  dst->pict_type = src->video->pict_type;
112  break;
113  case AVMEDIA_TYPE_AUDIO:
114  av_assert0(src->audio);
115  nb_channels = av_get_channel_layout_nb_channels(src->audio->channel_layout);
116  planes = av_sample_fmt_is_planar(src->format) ? nb_channels : 1;
117 
118  if (planes > FF_ARRAY_ELEMS(dst->data)) {
119  dst->extended_data = av_mallocz(planes * sizeof(*dst->extended_data));
120  if (!dst->extended_data)
121  return AVERROR(ENOMEM);
122  memcpy(dst->extended_data, src->extended_data,
123  planes * sizeof(*dst->extended_data));
124  } else
125  dst->extended_data = dst->data;
126  dst->nb_samples = src->audio->nb_samples;
127  av_frame_set_sample_rate (dst, src->audio->sample_rate);
129  av_frame_set_channels (dst, src->audio->channels);
130  break;
131  default:
132  return AVERROR(EINVAL);
133  }
134 
135  return 0;
136 }
137 #endif
138 
139 #if FF_API_FILL_FRAME
140 int avfilter_fill_frame_from_audio_buffer_ref(AVFrame *frame,
141  const AVFilterBufferRef *samplesref)
142 {
143  return avfilter_copy_buf_props(frame, samplesref);
144 }
145 
146 int avfilter_fill_frame_from_video_buffer_ref(AVFrame *frame,
147  const AVFilterBufferRef *picref)
148 {
149  return avfilter_copy_buf_props(frame, picref);
150 }
151 
152 int avfilter_fill_frame_from_buffer_ref(AVFrame *frame,
153  const AVFilterBufferRef *ref)
154 {
155  return avfilter_copy_buf_props(frame, ref);
156 }
157 #endif
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:205
This structure describes decoded (raw) audio or video data.
Definition: frame.h:76
FIXME Range Coding of cr are ref
Definition: snow.txt:367
#define FF_ARRAY_ELEMS(a)
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
uint8_t
AVOptions.
libavcodec/libavfilter gluing utilities
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
frame
Definition: stft.m:14
int width
width and height of the video frame
Definition: frame.h:122
void av_frame_set_pkt_pos(AVFrame *frame, int64_t val)
int av_get_channel_layout_nb_channels(uint64_t channel_layout)
Return the number of channels in the channel layout.
void av_frame_set_channels(AVFrame *frame, int val)
simple assert() macros that are a bit more flexible than ISO C assert().
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:246
uint64_t channel_layout
Channel layout of the audio data.
Definition: frame.h:331
int channels
number of audio channels, only used for audio.
Definition: frame.h:423
audio channel layout utility functions
int av_frame_get_channels(const AVFrame *frame)
enum AVPictureType pict_type
Picture type of the frame.
Definition: frame.h:144
void av_frame_set_sample_rate(AVFrame *frame, int val)
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
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:101
AVRational sample_aspect_ratio
Sample aspect ratio for the video frame, 0/1 if unknown/unspecified.
Definition: frame.h:154
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:148
int sample_rate
Sample rate of the audio data.
Definition: frame.h:326
int av_sample_fmt_is_planar(enum AVSampleFormat sample_fmt)
Check if the sample format is planar.
Definition: samplefmt.c:118
AVMediaType
Definition: avutil.h:141
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
#define type
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:87
int avfilter_copy_frame_props(AVFilterBufferRef *dst, const AVFrame *src)
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 layout
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
void av_frame_set_channel_layout(AVFrame *frame, int64_t val)
int64_t av_frame_get_channel_layout(const AVFrame *frame)
int height
Definition: frame.h:122
int nb_channels
uint8_t ** extended_data
pointers to the data planes/channels.
Definition: frame.h:117
int nb_samples
number of audio samples (per channel) described by this frame
Definition: frame.h:127
void avfilter_unref_bufferp(AVFilterBufferRef **ref)