FFmpeg coverage


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