FFmpeg
Loading...
Searching...
No Matches
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
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
78static 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;
152 par->profile = codec->profile;
153 par->level = codec->level;
154
155 switch (par->codec_type) {
157 par->format = codec->sw_pix_fmt != AV_PIX_FMT_NONE ?
158 codec->sw_pix_fmt : codec->pix_fmt;
159 par->width = codec->width;
160 par->height = codec->height;
161 par->field_order = codec->field_order;
162 par->color_range = codec->color_range;
163 par->color_primaries = codec->color_primaries;
164 par->color_trc = codec->color_trc;
165 par->color_space = codec->colorspace;
168 par->video_delay = codec->has_b_frames;
169 par->framerate = codec->framerate;
170 par->alpha_mode = codec->alpha_mode;
171 break;
173 par->format = codec->sample_fmt;
174 ret = av_channel_layout_copy(&par->ch_layout, &codec->ch_layout);
175 if (ret < 0)
176 return ret;
177 par->sample_rate = codec->sample_rate;
178 par->block_align = codec->block_align;
179 par->frame_size = codec->frame_size;
180 par->initial_padding = codec->initial_padding;
182 par->seek_preroll = codec->seek_preroll;
183 break;
185 par->width = codec->width;
186 par->height = codec->height;
187 break;
188 }
189
190 if (codec->extradata) {
192 if (!par->extradata)
193 return AVERROR(ENOMEM);
194 memcpy(par->extradata, codec->extradata, codec->extradata_size);
195 par->extradata_size = codec->extradata_size;
196 }
197
199 codec->coded_side_data, codec->nb_coded_side_data);
200 if (ret < 0)
201 return ret;
202
203 return 0;
204}
205
207 const AVCodecParameters *par)
208{
209 int ret;
210
211 codec->codec_type = par->codec_type;
212 codec->codec_id = par->codec_id;
213 codec->codec_tag = par->codec_tag;
214
215 codec->bit_rate = par->bit_rate;
218 codec->profile = par->profile;
219 codec->level = par->level;
220
221 switch (par->codec_type) {
223 codec->pix_fmt = par->format;
224 codec->width = par->width;
225 codec->height = par->height;
226 codec->field_order = par->field_order;
227 codec->color_range = par->color_range;
228 codec->color_primaries = par->color_primaries;
229 codec->color_trc = par->color_trc;
230 codec->colorspace = par->color_space;
233 codec->has_b_frames = par->video_delay;
234 codec->framerate = par->framerate;
235 codec->alpha_mode = par->alpha_mode;
236 break;
238 codec->sample_fmt = par->format;
239 ret = av_channel_layout_copy(&codec->ch_layout, &par->ch_layout);
240 if (ret < 0)
241 return ret;
242 codec->sample_rate = par->sample_rate;
243 codec->block_align = par->block_align;
244 codec->frame_size = par->frame_size;
245 codec->delay =
246 codec->initial_padding = par->initial_padding;
248 codec->seek_preroll = par->seek_preroll;
249 break;
251 codec->width = par->width;
252 codec->height = par->height;
253 break;
254 }
255
256 av_freep(&codec->extradata);
257 codec->extradata_size = 0;
258 if (par->extradata) {
260 if (!codec->extradata)
261 return AVERROR(ENOMEM);
262 memcpy(codec->extradata, par->extradata, par->extradata_size);
263 codec->extradata_size = par->extradata_size;
264 }
265
269 if (ret < 0)
270 return ret;
271
272 return 0;
273}
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
Definition dsp.h:87
Libavcodec external API header.
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
int avcodec_parameters_from_context(AVCodecParameters *par, const AVCodecContext *codec)
Definition codec_par.c:138
AVCodecParameters * avcodec_parameters_alloc(void)
Definition codec_par.c:57
int avcodec_parameters_copy(AVCodecParameters *dst, const AVCodecParameters *src)
Definition codec_par.c:107
static void codec_parameters_reset(AVCodecParameters *par)
Definition codec_par.c:32
void avcodec_parameters_free(AVCodecParameters **ppar)
Definition codec_par.c:67
static int codec_parameters_copy_side_data(AVPacketSideData **pdst, int *pnb_dst, const AVPacketSideData *src, int nb_src)
Definition codec_par.c:78
int avcodec_parameters_to_context(AVCodecContext *codec, const AVCodecParameters *par)
Definition codec_par.c:206
#define NULL
Definition coverity.c:32
#define AV_PROFILE_UNKNOWN
Definition defs.h:65
#define AV_LEVEL_UNKNOWN
Definition defs.h:209
@ AV_FIELD_UNKNOWN
Definition defs.h:212
@ AV_CODEC_ID_NONE
Definition codec_id.h:48
#define AV_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding.
Definition defs.h:40
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:658
void av_channel_layout_uninit(AVChannelLayout *channel_layout)
Free any allocated data in the channel layout and reset the channel count to 0.
int av_channel_layout_copy(AVChannelLayout *dst, const AVChannelLayout *src)
Make a copy of a channel layout.
@ AV_CHANNEL_ORDER_UNSPEC
Only the channel count is specified, without any further information about the channel order.
#define AVERROR(e)
Definition error.h:45
void * av_memdup(const void *p, size_t size)
Duplicate a buffer with av_malloc().
Definition mem.c:302
@ AVMEDIA_TYPE_AUDIO
Definition avutil.h:201
@ AVMEDIA_TYPE_SUBTITLE
Definition avutil.h:203
@ AVMEDIA_TYPE_VIDEO
Definition avutil.h:200
@ AVMEDIA_TYPE_UNKNOWN
Usually treated as AVMEDIA_TYPE_DATA.
Definition avutil.h:199
void * av_calloc(size_t nmemb, size_t size)
Definition mem.c:264
Memory handling functions.
@ AVCHROMA_LOC_UNSPECIFIED
Definition pixfmt.h:803
@ AVCOL_RANGE_UNSPECIFIED
Definition pixfmt.h:749
@ AVALPHA_MODE_UNSPECIFIED
Unknown alpha handling, or no alpha channel.
Definition pixfmt.h:817
@ AV_PIX_FMT_NONE
Definition pixfmt.h:72
@ AVCOL_PRI_UNSPECIFIED
Definition pixfmt.h:645
@ AVCOL_TRC_UNSPECIFIED
Definition pixfmt.h:675
@ AVCOL_SPC_UNSPECIFIED
Definition pixfmt.h:709
An AVChannelLayout holds information about the channel layout of audio data.
enum AVChannelOrder order
Channel order used in this layout.
main external API structure.
Definition avcodec.h:443
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition avcodec.h:643
int width
picture width / height.
Definition avcodec.h:604
AVPacketSideData * coded_side_data
Additional data associated with the entire coded stream.
Definition avcodec.h:1768
AVChannelLayout ch_layout
Audio channel layout.
Definition avcodec.h:1055
enum AVSampleFormat sample_fmt
audio sample format
Definition avcodec.h:1047
enum AVPixelFormat sw_pix_fmt
Nominal unaccelerated pixel format, see AV_PIX_FMT_xxx.
Definition avcodec.h:650
enum AVColorRange color_range
MPEG vs JPEG YUV range.
Definition avcodec.h:681
unsigned int codec_tag
fourcc (LSB first, so "ABCD" -> ('D'<<24) + ('C'<<16) + ('B'<<8) + 'A').
Definition avcodec.h:468
int nb_coded_side_data
Definition avcodec.h:1769
enum AVColorPrimaries color_primaries
Chromaticity coordinates of the source primaries.
Definition avcodec.h:657
enum AVMediaType codec_type
Definition avcodec.h:451
AVRational framerate
Definition avcodec.h:563
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:628
int bits_per_coded_sample
bits per sample/pixel from the demuxer (needed for huffyuv).
Definition avcodec.h:1564
enum AVFieldOrder field_order
Field order.
Definition avcodec.h:694
int has_b_frames
Size of the frame reordering buffer in the decoder.
Definition avcodec.h:709
int level
Encoding level descriptor.
Definition avcodec.h:1646
int64_t bit_rate
the average bitrate
Definition avcodec.h:493
int profile
profile
Definition avcodec.h:1636
int bits_per_raw_sample
Bits per sample/pixel of internal libavcodec pixel/sample format.
Definition avcodec.h:1571
enum AVColorSpace colorspace
YUV colorspace type.
Definition avcodec.h:671
int initial_padding
Audio only.
Definition avcodec.h:1114
int sample_rate
samples per second
Definition avcodec.h:1040
int delay
Codec delay.
Definition avcodec.h:587
enum AVColorTransferCharacteristic color_trc
Color Transfer Characteristic.
Definition avcodec.h:664
int seek_preroll
Number of samples to skip after a discontinuity.
Definition avcodec.h:1132
uint8_t * extradata
Out-of-band global headers that may be used by some codecs.
Definition avcodec.h:526
int trailing_padding
Audio only.
Definition avcodec.h:1125
enum AVChromaLocation chroma_sample_location
This defines the location of chroma samples.
Definition avcodec.h:688
enum AVAlphaMode alpha_mode
Indicates how the alpha channel of the video is represented.
Definition avcodec.h:1937
enum AVCodecID codec_id
Definition avcodec.h:453
int extradata_size
Definition avcodec.h:527
int block_align
number of bytes per packet if constant and known or 0 Used by some WAV based audio codecs.
Definition avcodec.h:1075
int frame_size
Number of samples per channel in an audio frame.
Definition avcodec.h:1068
This struct describes the properties of an encoded stream.
Definition codec_par.h:49
enum AVColorSpace color_space
Definition codec_par.h:192
int extradata_size
Size of the extradata content in bytes.
Definition codec_par.h:75
int frame_size
Audio frame size, if known.
Definition codec_par.h:227
enum AVFieldOrder field_order
The order of the fields in interlaced video.
Definition codec_par.h:182
int height
The height of the video frame in pixels.
Definition codec_par.h:150
int nb_coded_side_data
Amount of entries in coded_side_data.
Definition codec_par.h:88
int bits_per_coded_sample
The number of bits per sample in the codedwords.
Definition codec_par.h:113
AVChannelLayout ch_layout
The channel layout and number of channels.
Definition codec_par.h:207
int width
The width of the video frame in pixels.
Definition codec_par.h:143
int64_t bit_rate
The average bitrate of the encoded data (in bits per second).
Definition codec_par.h:99
int seek_preroll
Number of audio samples to skip after a discontinuity.
Definition codec_par.h:256
enum AVMediaType codec_type
General type of the encoded data.
Definition codec_par.h:53
int trailing_padding
Number of padding audio samples at the end.
Definition codec_par.h:250
AVRational framerate
Number of frames per second, for streams with constant frame durations.
Definition codec_par.h:175
int profile
Codec-specific bitstream restrictions that the stream conforms to.
Definition codec_par.h:135
int block_align
The number of bytes per coded audio frame, required by some formats.
Definition codec_par.h:221
int bits_per_raw_sample
The number of valid bits in each output sample.
Definition codec_par.h:130
AVRational sample_aspect_ratio
The aspect ratio (width/height) which a single pixel should have when displayed.
Definition codec_par.h:161
int video_delay
Number of delayed frames.
Definition codec_par.h:200
uint32_t codec_tag
Additional information about the codec (corresponds to the AVI FOURCC).
Definition codec_par.h:61
uint8_t * extradata
Extra binary data needed for initializing the decoder, codec-dependent.
Definition codec_par.h:71
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition codec_par.h:57
enum AVColorPrimaries color_primaries
Definition codec_par.h:190
int sample_rate
The number of audio samples per second.
Definition codec_par.h:213
enum AVColorTransferCharacteristic color_trc
Definition codec_par.h:191
enum AVAlphaMode alpha_mode
Video with alpha channel only.
Definition codec_par.h:261
int initial_padding
Number of padding audio samples at the start.
Definition codec_par.h:239
AVPacketSideData * coded_side_data
Additional data associated with the entire stream.
Definition codec_par.h:83
enum AVChromaLocation chroma_location
Definition codec_par.h:193
enum AVColorRange color_range
Additional colorspace characteristics.
Definition codec_par.h:189
This structure stores auxiliary information for decoding, presenting, or otherwise processing the cod...
Definition packet.h:424
uint8_t * data
Definition packet.h:425
enum AVPacketSideDataType type
Definition packet.h:427
Rational number (pair of numerator and denominator).
Definition rational.h:58
#define av_mallocz(s)
#define av_freep(p)
#define src
Definition vp8dsp.c:248