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 
32 {
33  av_freep(&par->extradata);
34 
35  memset(par, 0, sizeof(*par));
36 
39  par->format = -1;
46  par->sample_aspect_ratio = (AVRational){ 0, 1 };
48  par->level = FF_LEVEL_UNKNOWN;
49 }
50 
52 {
53  AVCodecParameters *par = av_mallocz(sizeof(*par));
54 
55  if (!par)
56  return NULL;
58  return par;
59 }
60 
62 {
63  AVCodecParameters *par = *ppar;
64 
65  if (!par)
66  return;
68 
69  av_freep(ppar);
70 }
71 
73 {
75  memcpy(dst, src, sizeof(*dst));
76 
77  dst->extradata = NULL;
78  dst->extradata_size = 0;
79  if (src->extradata) {
80  dst->extradata = av_mallocz(src->extradata_size + AV_INPUT_BUFFER_PADDING_SIZE);
81  if (!dst->extradata)
82  return AVERROR(ENOMEM);
83  memcpy(dst->extradata, src->extradata, src->extradata_size);
84  dst->extradata_size = src->extradata_size;
85  }
86 
87  return 0;
88 }
89 
91  const AVCodecContext *codec)
92 {
94 
95  par->codec_type = codec->codec_type;
96  par->codec_id = codec->codec_id;
97  par->codec_tag = codec->codec_tag;
98 
99  par->bit_rate = codec->bit_rate;
102  par->profile = codec->profile;
103  par->level = codec->level;
104 
105  switch (par->codec_type) {
106  case AVMEDIA_TYPE_VIDEO:
107  par->format = codec->pix_fmt;
108  par->width = codec->width;
109  par->height = codec->height;
110  par->field_order = codec->field_order;
111  par->color_range = codec->color_range;
112  par->color_primaries = codec->color_primaries;
113  par->color_trc = codec->color_trc;
114  par->color_space = codec->colorspace;
117  par->video_delay = codec->has_b_frames;
118  break;
119  case AVMEDIA_TYPE_AUDIO:
120  par->format = codec->sample_fmt;
121  par->channel_layout = codec->channel_layout;
122  par->channels = codec->channels;
123  par->sample_rate = codec->sample_rate;
124  par->block_align = codec->block_align;
125  par->frame_size = codec->frame_size;
126  par->initial_padding = codec->initial_padding;
127  par->trailing_padding = codec->trailing_padding;
128  par->seek_preroll = codec->seek_preroll;
129  break;
131  par->width = codec->width;
132  par->height = codec->height;
133  break;
134  }
135 
136  if (codec->extradata) {
138  if (!par->extradata)
139  return AVERROR(ENOMEM);
140  memcpy(par->extradata, codec->extradata, codec->extradata_size);
141  par->extradata_size = codec->extradata_size;
142  }
143 
144  return 0;
145 }
146 
148  const AVCodecParameters *par)
149 {
150  codec->codec_type = par->codec_type;
151  codec->codec_id = par->codec_id;
152  codec->codec_tag = par->codec_tag;
153 
154  codec->bit_rate = par->bit_rate;
157  codec->profile = par->profile;
158  codec->level = par->level;
159 
160  switch (par->codec_type) {
161  case AVMEDIA_TYPE_VIDEO:
162  codec->pix_fmt = par->format;
163  codec->width = par->width;
164  codec->height = par->height;
165  codec->field_order = par->field_order;
166  codec->color_range = par->color_range;
167  codec->color_primaries = par->color_primaries;
168  codec->color_trc = par->color_trc;
169  codec->colorspace = par->color_space;
172  codec->has_b_frames = par->video_delay;
173  break;
174  case AVMEDIA_TYPE_AUDIO:
175  codec->sample_fmt = par->format;
176  codec->channel_layout = par->channel_layout;
177  codec->channels = par->channels;
178  codec->sample_rate = par->sample_rate;
179  codec->block_align = par->block_align;
180  codec->frame_size = par->frame_size;
181  codec->delay =
182  codec->initial_padding = par->initial_padding;
183  codec->trailing_padding = par->trailing_padding;
184  codec->seek_preroll = par->seek_preroll;
185  break;
187  codec->width = par->width;
188  codec->height = par->height;
189  break;
190  }
191 
192  if (par->extradata) {
193  av_freep(&codec->extradata);
195  if (!codec->extradata)
196  return AVERROR(ENOMEM);
197  memcpy(codec->extradata, par->extradata, par->extradata_size);
198  codec->extradata_size = par->extradata_size;
199  }
200 
201  return 0;
202 }
AVCodecContext::frame_size
int frame_size
Number of samples per channel in an audio frame.
Definition: avcodec.h:1012
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:74
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:56
AVCodecContext::channel_layout
uint64_t channel_layout
Audio channel layout.
Definition: avcodec.h:1043
AVCodecContext::colorspace
enum AVColorSpace colorspace
YUV colorspace type.
Definition: avcodec.h:960
AVCodecContext::sample_rate
int sample_rate
samples per second
Definition: avcodec.h:992
AVCodecParameters
This struct describes the properties of an encoded stream.
Definition: codec_par.h:52
AVCodecParameters::color_space
enum AVColorSpace color_space
Definition: codec_par.h:149
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:90
AVCodecContext::color_trc
enum AVColorTransferCharacteristic color_trc
Color Transfer Characteristic.
Definition: avcodec.h:953
AVCodecContext::field_order
enum AVFieldOrder field_order
Field order.
Definition: avcodec.h:989
AVCodecParameters::seek_preroll
int seek_preroll
Audio only.
Definition: codec_par.h:200
AVCOL_TRC_UNSPECIFIED
@ AVCOL_TRC_UNSPECIFIED
Definition: pixfmt.h:497
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:61
AVCodecParameters::codec_tag
uint32_t codec_tag
Additional information about the codec (corresponds to the AVI FOURCC).
Definition: codec_par.h:64
FF_LEVEL_UNKNOWN
#define FF_LEVEL_UNKNOWN
Definition: avcodec.h:1652
AVCodecContext::delay
int delay
Codec delay.
Definition: avcodec.h:539
AVCodecParameters::color_primaries
enum AVColorPrimaries color_primaries
Definition: codec_par.h:147
AVCodecParameters::channels
int channels
Audio only.
Definition: codec_par.h:166
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:115
AVCodecContext::initial_padding
int initial_padding
Audio only.
Definition: avcodec.h:1701
AVCodecParameters::color_trc
enum AVColorTransferCharacteristic color_trc
Definition: codec_par.h:148
AVCodecContext::color_primaries
enum AVColorPrimaries color_primaries
Chromaticity coordinates of the source primaries.
Definition: avcodec.h:946
AVCodecParameters::frame_size
int frame_size
Audio only.
Definition: codec_par.h:181
AVCodecContext::extradata_size
int extradata_size
Definition: avcodec.h:485
AVCodecContext::has_b_frames
int has_b_frames
Size of the frame reordering buffer in the decoder.
Definition: avcodec.h:679
AVCodecParameters::sample_aspect_ratio
AVRational sample_aspect_ratio
Video only.
Definition: codec_par.h:136
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
AVCodecParameters::width
int width
Video only.
Definition: codec_par.h:126
FF_PROFILE_UNKNOWN
#define FF_PROFILE_UNKNOWN
Definition: avcodec.h:1526
AV_FIELD_UNKNOWN
@ AV_FIELD_UNKNOWN
Definition: codec_par.h:37
AVCodecContext::bits_per_raw_sample
int bits_per_raw_sample
Bits per sample/pixel of internal libavcodec pixel/sample format.
Definition: avcodec.h:1425
AVCOL_PRI_UNSPECIFIED
@ AVCOL_PRI_UNSPECIFIED
Definition: pixfmt.h:472
AVCodecContext::codec_id
enum AVCodecID codec_id
Definition: avcodec.h:393
NULL
#define NULL
Definition: coverity.c:32
AVCodecContext::color_range
enum AVColorRange color_range
MPEG vs JPEG YUV range.
Definition: avcodec.h:967
AVRational
Rational number (pair of numerator and denominator).
Definition: rational.h:58
AVCodecContext::bit_rate
int64_t bit_rate
the average bitrate
Definition: avcodec.h:433
src
#define src
Definition: vp8dsp.c:255
AVCodecContext::trailing_padding
int trailing_padding
Audio only.
Definition: avcodec.h:1878
AVCodecContext::level
int level
level
Definition: avcodec.h:1651
AVCOL_RANGE_UNSPECIFIED
@ AVCOL_RANGE_UNSPECIFIED
Definition: pixfmt.h:563
AVCodecParameters::level
int level
Definition: codec_par.h:121
AVCodecParameters::sample_rate
int sample_rate
Audio only.
Definition: codec_par.h:170
AVCodecParameters::extradata_size
int extradata_size
Size of the extradata content in bytes.
Definition: codec_par.h:78
AVCodecContext::sample_fmt
enum AVSampleFormat sample_fmt
audio sample format
Definition: avcodec.h:1000
AVCodecParameters::profile
int profile
Codec-specific bitstream restrictions that the stream conforms to.
Definition: codec_par.h:120
AVCHROMA_LOC_UNSPECIFIED
@ AVCHROMA_LOC_UNSPECIFIED
Definition: pixfmt.h:617
AVMEDIA_TYPE_UNKNOWN
@ AVMEDIA_TYPE_UNKNOWN
Usually treated as AVMEDIA_TYPE_DATA.
Definition: avutil.h:200
codec_parameters_reset
static void codec_parameters_reset(AVCodecParameters *par)
Definition: codec_par.c:31
AVCodecContext::channels
int channels
number of audio channels
Definition: avcodec.h:993
avcodec_parameters_copy
int avcodec_parameters_copy(AVCodecParameters *dst, const AVCodecParameters *src)
Copy the contents of src to dst.
Definition: codec_par.c:72
AVCodecContext::bits_per_coded_sample
int bits_per_coded_sample
bits per sample/pixel from the demuxer (needed for huffyuv).
Definition: avcodec.h:1418
AV_CODEC_ID_NONE
@ AV_CODEC_ID_NONE
Definition: codec_id.h:48
AVCodecContext::extradata
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:484
AVCodecParameters::height
int height
Definition: codec_par.h:127
avcodec_parameters_to_context
int avcodec_parameters_to_context(AVCodecContext *codec, const AVCodecParameters *par)
Fill the codec context based on the values from the supplied codec parameters.
Definition: codec_par.c:147
AVCodecParameters::block_align
int block_align
Audio only.
Definition: codec_par.h:177
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:263
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:51
AVCodecContext::chroma_sample_location
enum AVChromaLocation chroma_sample_location
This defines the location of chroma samples.
Definition: avcodec.h:974
AVCodecParameters::color_range
enum AVColorRange color_range
Video only.
Definition: codec_par.h:146
AVCOL_SPC_UNSPECIFIED
@ AVCOL_SPC_UNSPECIFIED
Definition: pixfmt.h:526
AVCodecContext::height
int height
Definition: avcodec.h:556
AVCodecContext::pix_fmt
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:593
AVCodecParameters::field_order
enum AVFieldOrder field_order
Video only.
Definition: codec_par.h:141
avcodec.h
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:1029
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:150
AVCodecParameters::trailing_padding
int trailing_padding
Audio only.
Definition: codec_par.h:196
AVCodecContext
main external API structure.
Definition: avcodec.h:383
AVCodecContext::profile
int profile
profile
Definition: avcodec.h:1525
AVCodecContext::codec_type
enum AVMediaType codec_type
Definition: avcodec.h:391
AVCodecContext::seek_preroll
int seek_preroll
Number of samples to skip after a discontinuity.
Definition: avcodec.h:1781
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:102
mem.h
AVCodecParameters::video_delay
int video_delay
Video only.
Definition: codec_par.h:155
AVCodecParameters::format
int format
Definition: codec_par.h:84
AVCodecContext::codec_tag
unsigned int codec_tag
fourcc (LSB first, so "ABCD" -> ('D'<<24) + ('C'<<16) + ('B'<<8) + 'A').
Definition: avcodec.h:408
codec_par.h
AVCodecParameters::codec_id
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: codec_par.h:60
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:35
AVCodecParameters::channel_layout
uint64_t channel_layout
Audio only.
Definition: codec_par.h:162
AVCodecContext::width
int width
picture width / height.
Definition: avcodec.h:556
AVCodecParameters::bit_rate
int64_t bit_rate
The average bitrate of the encoded data (in bits per second).
Definition: codec_par.h:89
AVCodecParameters::initial_padding
int initial_padding
Audio only.
Definition: codec_par.h:189
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:753