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;
54 }
55 
57 {
58  AVCodecParameters *par = av_mallocz(sizeof(*par));
59 
60  if (!par)
61  return NULL;
63  return par;
64 }
65 
67 {
68  AVCodecParameters *par = *ppar;
69 
70  if (!par)
71  return;
73 
74  av_freep(ppar);
75 }
76 
77 static int codec_parameters_copy_side_data(AVPacketSideData **pdst, int *pnb_dst,
78  const AVPacketSideData *src, int nb_src)
79 {
80  AVPacketSideData *dst;
81  int nb_dst = *pnb_dst;
82 
83  if (!src)
84  return 0;
85 
86  *pdst = dst = av_calloc(nb_src, sizeof(*dst));
87  if (!dst)
88  return AVERROR(ENOMEM);
89 
90  for (int i = 0; i < nb_src; i++) {
91  const AVPacketSideData *src_sd = &src[i];
92  AVPacketSideData *dst_sd = &dst[i];
93 
94  dst_sd->data = av_memdup(src_sd->data, src_sd->size);
95  if (!dst_sd->data)
96  return AVERROR(ENOMEM);
97 
98  dst_sd->type = src_sd->type;
99  dst_sd->size = src_sd->size;
100  *pnb_dst = ++nb_dst;
101  }
102 
103  return 0;
104 }
105 
107 {
108  int ret;
109 
111  memcpy(dst, src, sizeof(*dst));
112 
113  dst->ch_layout = (AVChannelLayout){0};
114  dst->extradata = NULL;
115  dst->extradata_size = 0;
116  dst->coded_side_data = NULL;
117  dst->nb_coded_side_data = 0;
118  if (src->extradata) {
119  dst->extradata = av_mallocz(src->extradata_size + AV_INPUT_BUFFER_PADDING_SIZE);
120  if (!dst->extradata)
121  return AVERROR(ENOMEM);
122  memcpy(dst->extradata, src->extradata, src->extradata_size);
123  dst->extradata_size = src->extradata_size;
124  }
126  src->coded_side_data, src->nb_coded_side_data);
127  if (ret < 0)
128  return ret;
129 
130  ret = av_channel_layout_copy(&dst->ch_layout, &src->ch_layout);
131  if (ret < 0)
132  return ret;
133 
134  return 0;
135 }
136 
138  const AVCodecContext *codec)
139 {
140  int ret;
141 
143 
144  par->codec_type = codec->codec_type;
145  par->codec_id = codec->codec_id;
146  par->codec_tag = codec->codec_tag;
147 
148  par->bit_rate = codec->bit_rate;
151  par->profile = codec->profile;
152  par->level = codec->level;
153 
154  switch (par->codec_type) {
155  case AVMEDIA_TYPE_VIDEO:
156  par->format = codec->pix_fmt;
157  par->width = codec->width;
158  par->height = codec->height;
159  par->field_order = codec->field_order;
160  par->color_range = codec->color_range;
161  par->color_primaries = codec->color_primaries;
162  par->color_trc = codec->color_trc;
163  par->color_space = codec->colorspace;
166  par->video_delay = codec->has_b_frames;
167  par->framerate = codec->framerate;
168  break;
169  case AVMEDIA_TYPE_AUDIO:
170  par->format = codec->sample_fmt;
171 #if FF_API_OLD_CHANNEL_LAYOUT
173  // if the old/new fields are set inconsistently, prefer the old ones
174  if ((codec->channels && codec->channels != codec->ch_layout.nb_channels) ||
175  (codec->channel_layout && (codec->ch_layout.order != AV_CHANNEL_ORDER_NATIVE ||
176  codec->ch_layout.u.mask != codec->channel_layout))) {
177  if (codec->channel_layout)
178  av_channel_layout_from_mask(&par->ch_layout, codec->channel_layout);
179  else {
181  par->ch_layout.nb_channels = codec->channels;
182  }
184  } else {
185 #endif
186  ret = av_channel_layout_copy(&par->ch_layout, &codec->ch_layout);
187  if (ret < 0)
188  return ret;
189 #if FF_API_OLD_CHANNEL_LAYOUT
191  }
192  par->channel_layout = par->ch_layout.order == AV_CHANNEL_ORDER_NATIVE ?
193  par->ch_layout.u.mask : 0;
194  par->channels = par->ch_layout.nb_channels;
196 #endif
197  par->sample_rate = codec->sample_rate;
198  par->block_align = codec->block_align;
199  par->frame_size = codec->frame_size;
200  par->initial_padding = codec->initial_padding;
201  par->trailing_padding = codec->trailing_padding;
202  par->seek_preroll = codec->seek_preroll;
203  break;
205  par->width = codec->width;
206  par->height = codec->height;
207  break;
208  }
209 
210  if (codec->extradata) {
212  if (!par->extradata)
213  return AVERROR(ENOMEM);
214  memcpy(par->extradata, codec->extradata, codec->extradata_size);
215  par->extradata_size = codec->extradata_size;
216  }
217 
219  codec->coded_side_data, codec->nb_coded_side_data);
220  if (ret < 0)
221  return ret;
222 
223  return 0;
224 }
225 
227  const AVCodecParameters *par)
228 {
229  int ret;
230 
231  codec->codec_type = par->codec_type;
232  codec->codec_id = par->codec_id;
233  codec->codec_tag = par->codec_tag;
234 
235  codec->bit_rate = par->bit_rate;
238  codec->profile = par->profile;
239  codec->level = par->level;
240 
241  switch (par->codec_type) {
242  case AVMEDIA_TYPE_VIDEO:
243  codec->pix_fmt = par->format;
244  codec->width = par->width;
245  codec->height = par->height;
246  codec->field_order = par->field_order;
247  codec->color_range = par->color_range;
248  codec->color_primaries = par->color_primaries;
249  codec->color_trc = par->color_trc;
250  codec->colorspace = par->color_space;
253  codec->has_b_frames = par->video_delay;
254  codec->framerate = par->framerate;
255  break;
256  case AVMEDIA_TYPE_AUDIO:
257  codec->sample_fmt = par->format;
258 #if FF_API_OLD_CHANNEL_LAYOUT
260  // if the old/new fields are set inconsistently, prefer the old ones
261  if ((par->channels && par->channels != par->ch_layout.nb_channels) ||
262  (par->channel_layout && (par->ch_layout.order != AV_CHANNEL_ORDER_NATIVE ||
263  par->ch_layout.u.mask != par->channel_layout))) {
264  if (par->channel_layout)
265  av_channel_layout_from_mask(&codec->ch_layout, par->channel_layout);
266  else {
268  codec->ch_layout.nb_channels = par->channels;
269  }
271  } else {
272 #endif
273  ret = av_channel_layout_copy(&codec->ch_layout, &par->ch_layout);
274  if (ret < 0)
275  return ret;
276 #if FF_API_OLD_CHANNEL_LAYOUT
278  }
279  codec->channel_layout = codec->ch_layout.order == AV_CHANNEL_ORDER_NATIVE ?
280  codec->ch_layout.u.mask : 0;
281  codec->channels = codec->ch_layout.nb_channels;
283 #endif
284  codec->sample_rate = par->sample_rate;
285  codec->block_align = par->block_align;
286  codec->frame_size = par->frame_size;
287  codec->delay =
288  codec->initial_padding = par->initial_padding;
289  codec->trailing_padding = par->trailing_padding;
290  codec->seek_preroll = par->seek_preroll;
291  break;
293  codec->width = par->width;
294  codec->height = par->height;
295  break;
296  }
297 
298  av_freep(&codec->extradata);
299  if (par->extradata) {
301  if (!codec->extradata)
302  return AVERROR(ENOMEM);
303  memcpy(codec->extradata, par->extradata, par->extradata_size);
304  codec->extradata_size = par->extradata_size;
305  }
306 
310  if (ret < 0)
311  return ret;
312 
313  return 0;
314 }
AVCodecContext::frame_size
int frame_size
Number of samples per channel in an audio frame.
Definition: avcodec.h:1092
FF_ENABLE_DEPRECATION_WARNINGS
#define FF_ENABLE_DEPRECATION_WARNINGS
Definition: internal.h:73
AVMEDIA_TYPE_SUBTITLE
@ AVMEDIA_TYPE_SUBTITLE
Definition: avutil.h:204
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
AVCodecContext::colorspace
enum AVColorSpace colorspace
YUV colorspace type.
Definition: avcodec.h:1029
AVCodecContext::sample_rate
int sample_rate
samples per second
Definition: avcodec.h:1064
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:144
AVCodecContext::coded_side_data
AVPacketSideData * coded_side_data
Additional data associated with the entire coded stream.
Definition: avcodec.h:1915
avcodec_parameters_to_context
int avcodec_parameters_to_context(AVCodecContext *codec, const AVCodecParameters *par)
Definition: codec_par.c:226
AVCodecContext::color_trc
enum AVColorTransferCharacteristic color_trc
Color Transfer Characteristic.
Definition: avcodec.h:1022
AVPacketSideData
This structure stores auxiliary information for decoding, presenting, or otherwise processing the cod...
Definition: packet.h:342
AVCodecContext::field_order
enum AVFieldOrder field_order
Field order.
Definition: avcodec.h:1061
AVCodecParameters::seek_preroll
int seek_preroll
Audio only.
Definition: codec_par.h:201
AVCOL_TRC_UNSPECIFIED
@ AVCOL_TRC_UNSPECIFIED
Definition: pixfmt.h:573
AVCodecParameters::framerate
AVRational framerate
Video only.
Definition: codec_par.h:218
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:312
AVChannelLayout::mask
uint64_t mask
This member must be used for AV_CHANNEL_ORDER_NATIVE, and may be used for AV_CHANNEL_ORDER_AMBISONIC ...
Definition: channel_layout.h:339
AVChannelLayout::nb_channels
int nb_channels
Number of channels in this layout.
Definition: channel_layout.h:317
AVCodecContext::delay
int delay
Codec delay.
Definition: avcodec.h:604
av_memdup
void * av_memdup(const void *p, size_t size)
Duplicate a buffer with av_malloc().
Definition: mem.c:302
AVCodecParameters::color_primaries
enum AVColorPrimaries color_primaries
Definition: codec_par.h:142
AVCodecContext::framerate
AVRational framerate
Definition: avcodec.h:1803
AVPacketSideData::size
size_t size
Definition: packet.h:344
AVCodecContext::ch_layout
AVChannelLayout ch_layout
Audio channel layout.
Definition: avcodec.h:2107
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:110
AVCodecContext::initial_padding
int initial_padding
Audio only.
Definition: avcodec.h:1794
AVChannelLayout::u
union AVChannelLayout::@332 u
Details about which channels are present in this layout.
AVCodecParameters::color_trc
enum AVColorTransferCharacteristic color_trc
Definition: codec_par.h:143
AVCodecContext::color_primaries
enum AVColorPrimaries color_primaries
Chromaticity coordinates of the source primaries.
Definition: avcodec.h:1015
AV_PROFILE_UNKNOWN
#define AV_PROFILE_UNKNOWN
Definition: defs.h:65
AV_FIELD_UNKNOWN
@ AV_FIELD_UNKNOWN
Definition: defs.h:199
AVCodecParameters::frame_size
int frame_size
Audio only.
Definition: codec_par.h:182
AVCodecContext::extradata_size
int extradata_size
Definition: avcodec.h:543
AVCodecContext::has_b_frames
int has_b_frames
Size of the frame reordering buffer in the decoder.
Definition: avcodec.h:744
AVCodecParameters::sample_aspect_ratio
AVRational sample_aspect_ratio
Video only.
Definition: codec_par.h:131
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
AVCodecParameters::width
int width
Video only.
Definition: codec_par.h:121
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:112
av_channel_layout_from_mask
FF_ENABLE_DEPRECATION_WARNINGS int av_channel_layout_from_mask(AVChannelLayout *channel_layout, uint64_t mask)
Initialize a native channel layout from a bitmask indicating which channels are present.
Definition: channel_layout.c:399
AVCodecContext::bits_per_raw_sample
int bits_per_raw_sample
Bits per sample/pixel of internal libavcodec pixel/sample format.
Definition: avcodec.h:1517
AVPacketSideData::data
uint8_t * data
Definition: packet.h:343
AVCodecParameters::nb_coded_side_data
int nb_coded_side_data
Amount of entries in coded_side_data.
Definition: codec_par.h:228
AVCOL_PRI_UNSPECIFIED
@ AVCOL_PRI_UNSPECIFIED
Definition: pixfmt.h:548
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: avpacket.c:738
AVCodecContext::codec_id
enum AVCodecID codec_id
Definition: avcodec.h:451
NULL
#define NULL
Definition: coverity.c:32
AVCodecContext::color_range
enum AVColorRange color_range
MPEG vs JPEG YUV range.
Definition: avcodec.h:1039
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:66
AV_LEVEL_UNKNOWN
#define AV_LEVEL_UNKNOWN
Definition: defs.h:196
AVRational
Rational number (pair of numerator and denominator).
Definition: rational.h:58
AVCodecContext::nb_coded_side_data
int nb_coded_side_data
Definition: avcodec.h:1916
AVCodecContext::bit_rate
int64_t bit_rate
the average bitrate
Definition: avcodec.h:491
AVPacketSideData::type
enum AVPacketSideDataType type
Definition: packet.h:345
AVCodecContext::trailing_padding
int trailing_padding
Audio only.
Definition: avcodec.h:1951
AVCodecParameters::ch_layout
AVChannelLayout ch_layout
Audio only.
Definition: codec_par.h:206
AVCodecContext::level
int level
Encoding level descriptor.
Definition: avcodec.h:1740
AVCOL_RANGE_UNSPECIFIED
@ AVCOL_RANGE_UNSPECIFIED
Definition: pixfmt.h:639
AVCodecParameters::level
int level
Definition: codec_par.h:116
AVCodecParameters::sample_rate
int sample_rate
Audio only.
Definition: codec_par.h:171
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:307
AVCodecContext::sample_fmt
enum AVSampleFormat sample_fmt
audio sample format
Definition: avcodec.h:1080
AVCodecParameters::profile
int profile
Codec-specific bitstream restrictions that the stream conforms to.
Definition: codec_par.h:115
AVCHROMA_LOC_UNSPECIFIED
@ AVCHROMA_LOC_UNSPECIFIED
Definition: pixfmt.h:693
AVMEDIA_TYPE_UNKNOWN
@ AVMEDIA_TYPE_UNKNOWN
Usually treated as AVMEDIA_TYPE_DATA.
Definition: avutil.h:200
AV_CHANNEL_ORDER_NATIVE
@ AV_CHANNEL_ORDER_NATIVE
The native channel order, i.e.
Definition: channel_layout.h:118
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:56
AVCodecContext::bits_per_coded_sample
int bits_per_coded_sample
bits per sample/pixel from the demuxer (needed for huffyuv).
Definition: avcodec.h:1510
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:255
AVCodecContext::extradata
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:542
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:77
packet.h
AVCodecParameters::height
int height
Definition: codec_par.h:122
AVCodecParameters::block_align
int block_align
Audio only.
Definition: codec_par.h:178
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:254
AVCodecContext::chroma_sample_location
enum AVChromaLocation chroma_sample_location
This defines the location of chroma samples.
Definition: avcodec.h:1046
AVCodecParameters::color_range
enum AVColorRange color_range
Video only.
Definition: codec_par.h:141
AVCOL_SPC_UNSPECIFIED
@ AVCOL_SPC_UNSPECIFIED
Definition: pixfmt.h:602
AVCodecContext::height
int height
Definition: avcodec.h:621
AVCodecParameters::coded_side_data
AVPacketSideData * coded_side_data
Additional data associated with the entire stream.
Definition: codec_par.h:223
AVCodecContext::pix_fmt
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:658
av_calloc
void * av_calloc(size_t nmemb, size_t size)
Definition: mem.c:262
AVCodecParameters::field_order
enum AVFieldOrder field_order
Video only.
Definition: codec_par.h:136
avcodec.h
ret
ret
Definition: filter_design.txt:187
AVCodecContext::block_align
int block_align
number of bytes per packet if constant and known or 0 Used by some WAV based audio codecs.
Definition: avcodec.h:1113
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:145
AVCodecParameters::trailing_padding
int trailing_padding
Audio only.
Definition: codec_par.h:197
AVCodecContext
main external API structure.
Definition: avcodec.h:441
AVCodecContext::profile
int profile
profile
Definition: avcodec.h:1596
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:640
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:647
FF_DISABLE_DEPRECATION_WARNINGS
#define FF_DISABLE_DEPRECATION_WARNINGS
Definition: internal.h:72
AVCodecContext::codec_type
enum AVMediaType codec_type
Definition: avcodec.h:449
AVCodecContext::seek_preroll
int seek_preroll
Number of samples to skip after a discontinuity.
Definition: avcodec.h:1874
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
AVCodecParameters::bits_per_coded_sample
int bits_per_coded_sample
The number of bits per sample in the codedwords.
Definition: codec_par.h:97
mem.h
AVCodecParameters::video_delay
int video_delay
Video only.
Definition: codec_par.h:150
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:137
AVCodecParameters::format
int format
Definition: codec_par.h:79
AVCodecContext::codec_tag
unsigned int codec_tag
fourcc (LSB first, so "ABCD" -> ('D'<<24) + ('C'<<16) + ('B'<<8) + 'A').
Definition: avcodec.h:466
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:34
src
INIT_CLIP pixel * src
Definition: h264pred_template.c:418
AVCodecContext::width
int width
picture width / height.
Definition: avcodec.h:621
AVCodecParameters::bit_rate
int64_t bit_rate
The average bitrate of the encoded data (in bits per second).
Definition: codec_par.h:84
AVCodecParameters::initial_padding
int initial_padding
Audio only.
Definition: codec_par.h:190
AVCodecContext::sample_aspect_ratio
AVRational sample_aspect_ratio
sample aspect ratio (0 if unknown) That is the width of a pixel divided by the height of the pixel.
Definition: avcodec.h:822
avcodec_parameters_copy
int avcodec_parameters_copy(AVCodecParameters *dst, const AVCodecParameters *src)
Copy the contents of src to dst.
Definition: codec_par.c:106