FFmpeg
codec_par.c
Go to the documentation of this file.
1 /*
2  * AVCodecParameters functions for libavcodec
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  * AVCodecParameters functions for libavcodec.
24  */
25 
26 #include <string.h>
27 #include "libavutil/mem.h"
28 #include "avcodec.h"
29 #include "codec_par.h"
30 #include "packet.h"
31 
33 {
34  av_freep(&par->extradata);
37 
38  memset(par, 0, sizeof(*par));
39 
42  par->format = -1;
50  par->sample_aspect_ratio = (AVRational){ 0, 1 };
51  par->framerate = (AVRational){ 0, 1 };
53  par->level = AV_LEVEL_UNKNOWN;
55 }
56 
58 {
59  AVCodecParameters *par = av_mallocz(sizeof(*par));
60 
61  if (!par)
62  return NULL;
64  return par;
65 }
66 
68 {
69  AVCodecParameters *par = *ppar;
70 
71  if (!par)
72  return;
74 
75  av_freep(ppar);
76 }
77 
78 static int codec_parameters_copy_side_data(AVPacketSideData **pdst, int *pnb_dst,
79  const AVPacketSideData *src, int nb_src)
80 {
82  int nb_dst = *pnb_dst;
83 
84  if (!src)
85  return 0;
86 
87  *pdst = dst = av_calloc(nb_src, sizeof(*dst));
88  if (!dst)
89  return AVERROR(ENOMEM);
90 
91  for (int i = 0; i < nb_src; i++) {
92  const AVPacketSideData *src_sd = &src[i];
93  AVPacketSideData *dst_sd = &dst[i];
94 
95  dst_sd->data = av_memdup(src_sd->data, src_sd->size);
96  if (!dst_sd->data)
97  return AVERROR(ENOMEM);
98 
99  dst_sd->type = src_sd->type;
100  dst_sd->size = src_sd->size;
101  *pnb_dst = ++nb_dst;
102  }
103 
104  return 0;
105 }
106 
108 {
109  int ret;
110 
112  memcpy(dst, src, sizeof(*dst));
113 
114  dst->ch_layout = (AVChannelLayout){0};
115  dst->extradata = NULL;
116  dst->extradata_size = 0;
117  dst->coded_side_data = NULL;
118  dst->nb_coded_side_data = 0;
119  if (src->extradata) {
120  dst->extradata = av_mallocz(src->extradata_size + AV_INPUT_BUFFER_PADDING_SIZE);
121  if (!dst->extradata)
122  return AVERROR(ENOMEM);
123  memcpy(dst->extradata, src->extradata, src->extradata_size);
124  dst->extradata_size = src->extradata_size;
125  }
126  ret = codec_parameters_copy_side_data(&dst->coded_side_data, &dst->nb_coded_side_data,
127  src->coded_side_data, src->nb_coded_side_data);
128  if (ret < 0)
129  return ret;
130 
131  ret = av_channel_layout_copy(&dst->ch_layout, &src->ch_layout);
132  if (ret < 0)
133  return ret;
134 
135  return 0;
136 }
137 
139  const AVCodecContext *codec)
140 {
141  int ret;
142 
144 
145  par->codec_type = codec->codec_type;
146  par->codec_id = codec->codec_id;
147  par->codec_tag = codec->codec_tag;
148 
149  par->bit_rate = codec->bit_rate;
150  par->bits_per_coded_sample = codec->bits_per_coded_sample;
151  par->bits_per_raw_sample = codec->bits_per_raw_sample;
152  par->profile = codec->profile;
153  par->level = codec->level;
154 
155  switch (par->codec_type) {
156  case AVMEDIA_TYPE_VIDEO:
157  par->format = codec->pix_fmt;
158  par->width = codec->width;
159  par->height = codec->height;
160  par->field_order = codec->field_order;
161  par->color_range = codec->color_range;
162  par->color_primaries = codec->color_primaries;
163  par->color_trc = codec->color_trc;
164  par->color_space = codec->colorspace;
165  par->chroma_location = codec->chroma_sample_location;
166  par->sample_aspect_ratio = codec->sample_aspect_ratio;
167  par->video_delay = codec->has_b_frames;
168  par->framerate = codec->framerate;
169  par->alpha_mode = codec->alpha_mode;
170  break;
171  case AVMEDIA_TYPE_AUDIO:
172  par->format = codec->sample_fmt;
173  ret = av_channel_layout_copy(&par->ch_layout, &codec->ch_layout);
174  if (ret < 0)
175  return ret;
176  par->sample_rate = codec->sample_rate;
177  par->block_align = codec->block_align;
178  par->frame_size = codec->frame_size;
179  par->initial_padding = codec->initial_padding;
180  par->trailing_padding = codec->trailing_padding;
181  par->seek_preroll = codec->seek_preroll;
182  break;
184  par->width = codec->width;
185  par->height = codec->height;
186  break;
187  }
188 
189  if (codec->extradata) {
190  par->extradata = av_mallocz(codec->extradata_size + AV_INPUT_BUFFER_PADDING_SIZE);
191  if (!par->extradata)
192  return AVERROR(ENOMEM);
193  memcpy(par->extradata, codec->extradata, codec->extradata_size);
194  par->extradata_size = codec->extradata_size;
195  }
196 
198  codec->coded_side_data, codec->nb_coded_side_data);
199  if (ret < 0)
200  return ret;
201 
202  return 0;
203 }
204 
206  const AVCodecParameters *par)
207 {
208  int ret;
209 
210  codec->codec_type = par->codec_type;
211  codec->codec_id = par->codec_id;
212  codec->codec_tag = par->codec_tag;
213 
214  codec->bit_rate = par->bit_rate;
215  codec->bits_per_coded_sample = par->bits_per_coded_sample;
216  codec->bits_per_raw_sample = par->bits_per_raw_sample;
217  codec->profile = par->profile;
218  codec->level = par->level;
219 
220  switch (par->codec_type) {
221  case AVMEDIA_TYPE_VIDEO:
222  codec->pix_fmt = par->format;
223  codec->width = par->width;
224  codec->height = par->height;
225  codec->field_order = par->field_order;
226  codec->color_range = par->color_range;
227  codec->color_primaries = par->color_primaries;
228  codec->color_trc = par->color_trc;
229  codec->colorspace = par->color_space;
230  codec->chroma_sample_location = par->chroma_location;
231  codec->sample_aspect_ratio = par->sample_aspect_ratio;
232  codec->has_b_frames = par->video_delay;
233  codec->framerate = par->framerate;
234  codec->alpha_mode = par->alpha_mode;
235  break;
236  case AVMEDIA_TYPE_AUDIO:
237  codec->sample_fmt = par->format;
238  ret = av_channel_layout_copy(&codec->ch_layout, &par->ch_layout);
239  if (ret < 0)
240  return ret;
241  codec->sample_rate = par->sample_rate;
242  codec->block_align = par->block_align;
243  codec->frame_size = par->frame_size;
244  codec->delay =
245  codec->initial_padding = par->initial_padding;
246  codec->trailing_padding = par->trailing_padding;
247  codec->seek_preroll = par->seek_preroll;
248  break;
250  codec->width = par->width;
251  codec->height = par->height;
252  break;
253  }
254 
255  av_freep(&codec->extradata);
256  codec->extradata_size = 0;
257  if (par->extradata) {
259  if (!codec->extradata)
260  return AVERROR(ENOMEM);
261  memcpy(codec->extradata, par->extradata, par->extradata_size);
262  codec->extradata_size = par->extradata_size;
263  }
264 
265  av_packet_side_data_free(&codec->coded_side_data, &codec->nb_coded_side_data);
266  ret = codec_parameters_copy_side_data(&codec->coded_side_data, &codec->nb_coded_side_data,
268  if (ret < 0)
269  return ret;
270 
271  return 0;
272 }
AVMEDIA_TYPE_SUBTITLE
@ AVMEDIA_TYPE_SUBTITLE
Definition: avutil.h:203
AVCodecParameters::extradata
uint8_t * extradata
Extra binary data needed for initializing the decoder, codec-dependent.
Definition: codec_par.h:69
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame 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. Frame references ownership and permissions
AVCodecParameters::codec_type
enum AVMediaType codec_type
General type of the encoded data.
Definition: codec_par.h:51
AVCodecParameters
This struct describes the properties of an encoded stream.
Definition: codec_par.h:47
AVCodecParameters::color_space
enum AVColorSpace color_space
Definition: codec_par.h:169
avcodec_parameters_to_context
int avcodec_parameters_to_context(AVCodecContext *codec, const AVCodecParameters *par)
Definition: codec_par.c:205
AVPacketSideData
This structure stores auxiliary information for decoding, presenting, or otherwise processing the cod...
Definition: packet.h:409
AVCodecParameters::seek_preroll
int seek_preroll
Audio only.
Definition: codec_par.h:214
AVCOL_TRC_UNSPECIFIED
@ AVCOL_TRC_UNSPECIFIED
Definition: pixfmt.h:664
AVCodecParameters::framerate
AVRational framerate
Video only.
Definition: codec_par.h:156
AVCodecParameters::codec_tag
uint32_t codec_tag
Additional information about the codec (corresponds to the AVI FOURCC).
Definition: codec_par.h:59
AVChannelLayout::order
enum AVChannelOrder order
Channel order used in this layout.
Definition: channel_layout.h:324
av_memdup
void * av_memdup(const void *p, size_t size)
Duplicate a buffer with av_malloc().
Definition: mem.c:304
AVCodecParameters::color_primaries
enum AVColorPrimaries color_primaries
Definition: codec_par.h:167
AVPacketSideData::size
size_t size
Definition: packet.h:411
AVCodecContext::codec
const struct AVCodec * codec
Definition: avcodec.h:440
AVCodecParameters::bits_per_raw_sample
int bits_per_raw_sample
This is the number of valid bits in each output sample.
Definition: codec_par.h:123
AVCodecParameters::color_trc
enum AVColorTransferCharacteristic color_trc
Definition: codec_par.h:168
AV_PROFILE_UNKNOWN
#define AV_PROFILE_UNKNOWN
Definition: defs.h:65
AV_FIELD_UNKNOWN
@ AV_FIELD_UNKNOWN
Definition: defs.h:212
AVCodecParameters::frame_size
int frame_size
Audio only.
Definition: codec_par.h:195
AVCodecParameters::sample_aspect_ratio
AVRational sample_aspect_ratio
Video only.
Definition: codec_par.h:144
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:201
AVCodecParameters::width
int width
Video only.
Definition: codec_par.h:134
AV_CHANNEL_ORDER_UNSPEC
@ AV_CHANNEL_ORDER_UNSPEC
Only the channel count is specified, without any further information about the channel order.
Definition: channel_layout.h:119
AVPacketSideData::data
uint8_t * data
Definition: packet.h:410
AVCodecParameters::nb_coded_side_data
int nb_coded_side_data
Amount of entries in coded_side_data.
Definition: codec_par.h:86
AVCOL_PRI_UNSPECIFIED
@ AVCOL_PRI_UNSPECIFIED
Definition: pixfmt.h:639
av_packet_side_data_free
void av_packet_side_data_free(AVPacketSideData **psd, int *pnb_sd)
Convenience function to free all the side data stored in an array, and the array itself.
Definition: packet.c:748
NULL
#define NULL
Definition: coverity.c:32
avcodec_parameters_free
void avcodec_parameters_free(AVCodecParameters **ppar)
Free an AVCodecParameters instance and everything associated with it and write NULL to the supplied p...
Definition: codec_par.c:67
AV_LEVEL_UNKNOWN
#define AV_LEVEL_UNKNOWN
Definition: defs.h:209
AVRational
Rational number (pair of numerator and denominator).
Definition: rational.h:58
AVPacketSideData::type
enum AVPacketSideDataType type
Definition: packet.h:412
AVCodecParameters::ch_layout
AVChannelLayout ch_layout
Audio only.
Definition: codec_par.h:180
AVCOL_RANGE_UNSPECIFIED
@ AVCOL_RANGE_UNSPECIFIED
Definition: pixfmt.h:733
AVCodecParameters::level
int level
Definition: codec_par.h:129
AVCodecParameters::sample_rate
int sample_rate
Audio only.
Definition: codec_par.h:184
AVCodecParameters::extradata_size
int extradata_size
Size of the extradata content in bytes.
Definition: codec_par.h:73
AVChannelLayout
An AVChannelLayout holds information about the channel layout of audio data.
Definition: channel_layout.h:319
dst
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
Definition: dsp.h:87
AVCodecParameters::alpha_mode
enum AVAlphaMode alpha_mode
Video with alpha channel only.
Definition: codec_par.h:219
AVCodecParameters::profile
int profile
Codec-specific bitstream restrictions that the stream conforms to.
Definition: codec_par.h:128
AVCHROMA_LOC_UNSPECIFIED
@ AVCHROMA_LOC_UNSPECIFIED
Definition: pixfmt.h:787
AVMEDIA_TYPE_UNKNOWN
@ AVMEDIA_TYPE_UNKNOWN
Usually treated as AVMEDIA_TYPE_DATA.
Definition: avutil.h:199
codec_parameters_reset
static void codec_parameters_reset(AVCodecParameters *par)
Definition: codec_par.c:32
avcodec_parameters_alloc
AVCodecParameters * avcodec_parameters_alloc(void)
Allocate a new AVCodecParameters and set its fields to default values (unknown/invalid/0).
Definition: codec_par.c:57
AV_CODEC_ID_NONE
@ AV_CODEC_ID_NONE
Definition: codec_id.h:50
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:256
codec_parameters_copy_side_data
static int codec_parameters_copy_side_data(AVPacketSideData **pdst, int *pnb_dst, const AVPacketSideData *src, int nb_src)
Definition: codec_par.c:78
packet.h
AVCodecParameters::height
int height
Definition: codec_par.h:135
AVCodecParameters::block_align
int block_align
Audio only.
Definition: codec_par.h:191
av_mallocz
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
Definition: mem.c:256
AVCodecParameters::color_range
enum AVColorRange color_range
Video only.
Definition: codec_par.h:166
AVCOL_SPC_UNSPECIFIED
@ AVCOL_SPC_UNSPECIFIED
Definition: pixfmt.h:693
AVCodecParameters::coded_side_data
AVPacketSideData * coded_side_data
Additional data associated with the entire stream.
Definition: codec_par.h:81
av_calloc
void * av_calloc(size_t nmemb, size_t size)
Definition: mem.c:264
AVCodecParameters::field_order
enum AVFieldOrder field_order
Video only.
Definition: codec_par.h:161
avcodec.h
ret
ret
Definition: filter_design.txt:187
AVALPHA_MODE_UNSPECIFIED
@ AVALPHA_MODE_UNSPECIFIED
Unknown alpha handling, or no alpha channel.
Definition: pixfmt.h:801
AV_INPUT_BUFFER_PADDING_SIZE
#define AV_INPUT_BUFFER_PADDING_SIZE
Definition: defs.h:40
AVCodecParameters::chroma_location
enum AVChromaLocation chroma_location
Definition: codec_par.h:170
AVCodecParameters::trailing_padding
int trailing_padding
Audio only.
Definition: codec_par.h:210
AVCodecContext
main external API structure.
Definition: avcodec.h:431
av_channel_layout_uninit
void av_channel_layout_uninit(AVChannelLayout *channel_layout)
Free any allocated data in the channel layout and reset the channel count to 0.
Definition: channel_layout.c:442
av_channel_layout_copy
int av_channel_layout_copy(AVChannelLayout *dst, const AVChannelLayout *src)
Make a copy of a channel layout.
Definition: channel_layout.c:449
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:200
AVCodecParameters::bits_per_coded_sample
int bits_per_coded_sample
The number of bits per sample in the codedwords.
Definition: codec_par.h:110
mem.h
AVCodecParameters::video_delay
int video_delay
Video only.
Definition: codec_par.h:175
avcodec_parameters_from_context
int avcodec_parameters_from_context(AVCodecParameters *par, const AVCodecContext *codec)
Fill the parameters struct based on the values from the supplied codec context.
Definition: codec_par.c:138
AVCodecParameters::format
int format
Definition: codec_par.h:92
codec_par.h
AVCodecParameters::codec_id
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: codec_par.h:55
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:35
AVCodecParameters::bit_rate
int64_t bit_rate
The average bitrate of the encoded data (in bits per second).
Definition: codec_par.h:97
AVCodecParameters::initial_padding
int initial_padding
Audio only.
Definition: codec_par.h:203
src
#define src
Definition: vp8dsp.c:248
avcodec_parameters_copy
int avcodec_parameters_copy(AVCodecParameters *dst, const AVCodecParameters *src)
Copy the contents of src to dst.
Definition: codec_par.c:107