FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/codec_par.c
Date: 2026-04-19 20:43:40
Exec Total Coverage
Lines: 169 181 93.4%
Functions: 7 7 100.0%
Branches: 32 44 72.7%

Line Branch Exec Source
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
32 270397 static void codec_parameters_reset(AVCodecParameters *par)
33 {
34 270397 av_freep(&par->extradata);
35 270397 av_channel_layout_uninit(&par->ch_layout);
36 270397 av_packet_side_data_free(&par->coded_side_data, &par->nb_coded_side_data);
37
38 270397 memset(par, 0, sizeof(*par));
39
40 270397 par->codec_type = AVMEDIA_TYPE_UNKNOWN;
41 270397 par->codec_id = AV_CODEC_ID_NONE;
42 270397 par->format = -1;
43 270397 par->ch_layout.order = AV_CHANNEL_ORDER_UNSPEC;
44 270397 par->field_order = AV_FIELD_UNKNOWN;
45 270397 par->color_range = AVCOL_RANGE_UNSPECIFIED;
46 270397 par->color_primaries = AVCOL_PRI_UNSPECIFIED;
47 270397 par->color_trc = AVCOL_TRC_UNSPECIFIED;
48 270397 par->color_space = AVCOL_SPC_UNSPECIFIED;
49 270397 par->chroma_location = AVCHROMA_LOC_UNSPECIFIED;
50 270397 par->sample_aspect_ratio = (AVRational){ 0, 1 };
51 270397 par->framerate = (AVRational){ 0, 1 };
52 270397 par->profile = AV_PROFILE_UNKNOWN;
53 270397 par->level = AV_LEVEL_UNKNOWN;
54 270397 par->alpha_mode = AVALPHA_MODE_UNSPECIFIED;
55 270397 }
56
57 91023 AVCodecParameters *avcodec_parameters_alloc(void)
58 {
59 91023 AVCodecParameters *par = av_mallocz(sizeof(*par));
60
61
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 91023 times.
91023 if (!par)
62 return NULL;
63 91023 codec_parameters_reset(par);
64 91023 return par;
65 }
66
67 2234024 void avcodec_parameters_free(AVCodecParameters **ppar)
68 {
69 2234024 AVCodecParameters *par = *ppar;
70
71
2/2
✓ Branch 0 taken 2143007 times.
✓ Branch 1 taken 91017 times.
2234024 if (!par)
72 2143007 return;
73 91017 codec_parameters_reset(par);
74
75 91017 av_freep(ppar);
76 }
77
78 154313 static int codec_parameters_copy_side_data(AVPacketSideData **pdst, int *pnb_dst,
79 const AVPacketSideData *src, int nb_src)
80 {
81 AVPacketSideData *dst;
82 154313 int nb_dst = *pnb_dst;
83
84
2/2
✓ Branch 0 taken 146372 times.
✓ Branch 1 taken 7941 times.
154313 if (!src)
85 146372 return 0;
86
87 7941 *pdst = dst = av_calloc(nb_src, sizeof(*dst));
88
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7941 times.
7941 if (!dst)
89 return AVERROR(ENOMEM);
90
91
2/2
✓ Branch 0 taken 8484 times.
✓ Branch 1 taken 7941 times.
16425 for (int i = 0; i < nb_src; i++) {
92 8484 const AVPacketSideData *src_sd = &src[i];
93 8484 AVPacketSideData *dst_sd = &dst[i];
94
95 8484 dst_sd->data = av_memdup(src_sd->data, src_sd->size);
96
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8484 times.
8484 if (!dst_sd->data)
97 return AVERROR(ENOMEM);
98
99 8484 dst_sd->type = src_sd->type;
100 8484 dst_sd->size = src_sd->size;
101 8484 *pnb_dst = ++nb_dst;
102 }
103
104 7941 return 0;
105 }
106
107 44182 int avcodec_parameters_copy(AVCodecParameters *dst, const AVCodecParameters *src)
108 {
109 int ret;
110
111 44182 codec_parameters_reset(dst);
112 44182 memcpy(dst, src, sizeof(*dst));
113
114 44182 dst->ch_layout = (AVChannelLayout){0};
115 44182 dst->extradata = NULL;
116 44182 dst->extradata_size = 0;
117 44182 dst->coded_side_data = NULL;
118 44182 dst->nb_coded_side_data = 0;
119
2/2
✓ Branch 0 taken 6842 times.
✓ Branch 1 taken 37340 times.
44182 if (src->extradata) {
120 6842 dst->extradata = av_mallocz(src->extradata_size + AV_INPUT_BUFFER_PADDING_SIZE);
121
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6842 times.
6842 if (!dst->extradata)
122 return AVERROR(ENOMEM);
123 6842 memcpy(dst->extradata, src->extradata, src->extradata_size);
124 6842 dst->extradata_size = src->extradata_size;
125 }
126 44182 ret = codec_parameters_copy_side_data(&dst->coded_side_data, &dst->nb_coded_side_data,
127 44182 src->coded_side_data, src->nb_coded_side_data);
128
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 44182 times.
44182 if (ret < 0)
129 return ret;
130
131 44182 ret = av_channel_layout_copy(&dst->ch_layout, &src->ch_layout);
132
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 44182 times.
44182 if (ret < 0)
133 return ret;
134
135 44182 return 0;
136 }
137
138 44175 int avcodec_parameters_from_context(AVCodecParameters *par,
139 const AVCodecContext *codec)
140 {
141 int ret;
142
143 44175 codec_parameters_reset(par);
144
145 44175 par->codec_type = codec->codec_type;
146 44175 par->codec_id = codec->codec_id;
147 44175 par->codec_tag = codec->codec_tag;
148
149 44175 par->bit_rate = codec->bit_rate;
150 44175 par->bits_per_coded_sample = codec->bits_per_coded_sample;
151 44175 par->bits_per_raw_sample = codec->bits_per_raw_sample;
152 44175 par->profile = codec->profile;
153 44175 par->level = codec->level;
154
155
4/4
✓ Branch 0 taken 34224 times.
✓ Branch 1 taken 9449 times.
✓ Branch 2 taken 412 times.
✓ Branch 3 taken 90 times.
44175 switch (par->codec_type) {
156 34224 case AVMEDIA_TYPE_VIDEO:
157 34224 par->format = codec->pix_fmt;
158 34224 par->width = codec->width;
159 34224 par->height = codec->height;
160 34224 par->field_order = codec->field_order;
161 34224 par->color_range = codec->color_range;
162 34224 par->color_primaries = codec->color_primaries;
163 34224 par->color_trc = codec->color_trc;
164 34224 par->color_space = codec->colorspace;
165 34224 par->chroma_location = codec->chroma_sample_location;
166 34224 par->sample_aspect_ratio = codec->sample_aspect_ratio;
167 34224 par->video_delay = codec->has_b_frames;
168 34224 par->framerate = codec->framerate;
169 34224 par->alpha_mode = codec->alpha_mode;
170 34224 break;
171 9449 case AVMEDIA_TYPE_AUDIO:
172 9449 par->format = codec->sample_fmt;
173 9449 ret = av_channel_layout_copy(&par->ch_layout, &codec->ch_layout);
174
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9449 times.
9449 if (ret < 0)
175 return ret;
176 9449 par->sample_rate = codec->sample_rate;
177 9449 par->block_align = codec->block_align;
178 9449 par->frame_size = codec->frame_size;
179 9449 par->initial_padding = codec->initial_padding;
180 9449 par->trailing_padding = codec->trailing_padding;
181 9449 par->seek_preroll = codec->seek_preroll;
182 9449 break;
183 412 case AVMEDIA_TYPE_SUBTITLE:
184 412 par->width = codec->width;
185 412 par->height = codec->height;
186 412 break;
187 }
188
189
2/2
✓ Branch 0 taken 7502 times.
✓ Branch 1 taken 36673 times.
44175 if (codec->extradata) {
190 7502 par->extradata = av_mallocz(codec->extradata_size + AV_INPUT_BUFFER_PADDING_SIZE);
191
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7502 times.
7502 if (!par->extradata)
192 return AVERROR(ENOMEM);
193 7502 memcpy(par->extradata, codec->extradata, codec->extradata_size);
194 7502 par->extradata_size = codec->extradata_size;
195 }
196
197 44175 ret = codec_parameters_copy_side_data(&par->coded_side_data, &par->nb_coded_side_data,
198 44175 codec->coded_side_data, codec->nb_coded_side_data);
199
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 44175 times.
44175 if (ret < 0)
200 return ret;
201
202 44175 return 0;
203 }
204
205 65956 int avcodec_parameters_to_context(AVCodecContext *codec,
206 const AVCodecParameters *par)
207 {
208 int ret;
209
210 65956 codec->codec_type = par->codec_type;
211 65956 codec->codec_id = par->codec_id;
212 65956 codec->codec_tag = par->codec_tag;
213
214 65956 codec->bit_rate = par->bit_rate;
215 65956 codec->bits_per_coded_sample = par->bits_per_coded_sample;
216 65956 codec->bits_per_raw_sample = par->bits_per_raw_sample;
217 65956 codec->profile = par->profile;
218 65956 codec->level = par->level;
219
220
4/4
✓ Branch 0 taken 52662 times.
✓ Branch 1 taken 12454 times.
✓ Branch 2 taken 561 times.
✓ Branch 3 taken 279 times.
65956 switch (par->codec_type) {
221 52662 case AVMEDIA_TYPE_VIDEO:
222 52662 codec->pix_fmt = par->format;
223 52662 codec->width = par->width;
224 52662 codec->height = par->height;
225 52662 codec->field_order = par->field_order;
226 52662 codec->color_range = par->color_range;
227 52662 codec->color_primaries = par->color_primaries;
228 52662 codec->color_trc = par->color_trc;
229 52662 codec->colorspace = par->color_space;
230 52662 codec->chroma_sample_location = par->chroma_location;
231 52662 codec->sample_aspect_ratio = par->sample_aspect_ratio;
232 52662 codec->has_b_frames = par->video_delay;
233 52662 codec->framerate = par->framerate;
234 52662 codec->alpha_mode = par->alpha_mode;
235 52662 break;
236 12454 case AVMEDIA_TYPE_AUDIO:
237 12454 codec->sample_fmt = par->format;
238 12454 ret = av_channel_layout_copy(&codec->ch_layout, &par->ch_layout);
239
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12454 times.
12454 if (ret < 0)
240 return ret;
241 12454 codec->sample_rate = par->sample_rate;
242 12454 codec->block_align = par->block_align;
243 12454 codec->frame_size = par->frame_size;
244 12454 codec->delay =
245 12454 codec->initial_padding = par->initial_padding;
246 12454 codec->trailing_padding = par->trailing_padding;
247 12454 codec->seek_preroll = par->seek_preroll;
248 12454 break;
249 561 case AVMEDIA_TYPE_SUBTITLE:
250 561 codec->width = par->width;
251 561 codec->height = par->height;
252 561 break;
253 }
254
255 65956 av_freep(&codec->extradata);
256 65956 codec->extradata_size = 0;
257
2/2
✓ Branch 0 taken 9307 times.
✓ Branch 1 taken 56649 times.
65956 if (par->extradata) {
258 9307 codec->extradata = av_mallocz(par->extradata_size + AV_INPUT_BUFFER_PADDING_SIZE);
259
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9307 times.
9307 if (!codec->extradata)
260 return AVERROR(ENOMEM);
261 9307 memcpy(codec->extradata, par->extradata, par->extradata_size);
262 9307 codec->extradata_size = par->extradata_size;
263 }
264
265 65956 av_packet_side_data_free(&codec->coded_side_data, &codec->nb_coded_side_data);
266 65956 ret = codec_parameters_copy_side_data(&codec->coded_side_data, &codec->nb_coded_side_data,
267 65956 par->coded_side_data, par->nb_coded_side_data);
268
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 65956 times.
65956 if (ret < 0)
269 return ret;
270
271 65956 return 0;
272 }
273