FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/codec_par.c
Date: 2022-07-04 19:11:22
Exec Total Coverage
Lines: 148 161 91.9%
Branches: 42 58 72.4%

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
31 151131 static void codec_parameters_reset(AVCodecParameters *par)
32 {
33 151131 av_freep(&par->extradata);
34 151131 av_channel_layout_uninit(&par->ch_layout);
35
36 151131 memset(par, 0, sizeof(*par));
37
38 151131 par->codec_type = AVMEDIA_TYPE_UNKNOWN;
39 151131 par->codec_id = AV_CODEC_ID_NONE;
40 151131 par->format = -1;
41 151131 par->ch_layout.order = AV_CHANNEL_ORDER_UNSPEC;
42 151131 par->field_order = AV_FIELD_UNKNOWN;
43 151131 par->color_range = AVCOL_RANGE_UNSPECIFIED;
44 151131 par->color_primaries = AVCOL_PRI_UNSPECIFIED;
45 151131 par->color_trc = AVCOL_TRC_UNSPECIFIED;
46 151131 par->color_space = AVCOL_SPC_UNSPECIFIED;
47 151131 par->chroma_location = AVCHROMA_LOC_UNSPECIFIED;
48 151131 par->sample_aspect_ratio = (AVRational){ 0, 1 };
49 151131 par->profile = FF_PROFILE_UNKNOWN;
50 151131 par->level = FF_LEVEL_UNKNOWN;
51 151131 }
52
53 50902 AVCodecParameters *avcodec_parameters_alloc(void)
54 {
55 50902 AVCodecParameters *par = av_mallocz(sizeof(*par));
56
57
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 50902 times.
50902 if (!par)
58 return NULL;
59 50902 codec_parameters_reset(par);
60 50902 return par;
61 }
62
63 50896 void avcodec_parameters_free(AVCodecParameters **ppar)
64 {
65 50896 AVCodecParameters *par = *ppar;
66
67
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 50896 times.
50896 if (!par)
68 return;
69 50896 codec_parameters_reset(par);
70
71 50896 av_freep(ppar);
72 }
73
74 15600 int avcodec_parameters_copy(AVCodecParameters *dst, const AVCodecParameters *src)
75 {
76 int ret;
77
78 15600 codec_parameters_reset(dst);
79 15600 memcpy(dst, src, sizeof(*dst));
80
81 15600 dst->ch_layout = (AVChannelLayout){0};
82 15600 dst->extradata = NULL;
83 15600 dst->extradata_size = 0;
84
2/2
✓ Branch 0 taken 2423 times.
✓ Branch 1 taken 13177 times.
15600 if (src->extradata) {
85 2423 dst->extradata = av_mallocz(src->extradata_size + AV_INPUT_BUFFER_PADDING_SIZE);
86
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2423 times.
2423 if (!dst->extradata)
87 return AVERROR(ENOMEM);
88 2423 memcpy(dst->extradata, src->extradata, src->extradata_size);
89 2423 dst->extradata_size = src->extradata_size;
90 }
91
92 15600 ret = av_channel_layout_copy(&dst->ch_layout, &src->ch_layout);
93
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 15600 times.
15600 if (ret < 0)
94 return ret;
95
96 15600 return 0;
97 }
98
99 33733 int avcodec_parameters_from_context(AVCodecParameters *par,
100 const AVCodecContext *codec)
101 {
102 int ret;
103
104 33733 codec_parameters_reset(par);
105
106 33733 par->codec_type = codec->codec_type;
107 33733 par->codec_id = codec->codec_id;
108 33733 par->codec_tag = codec->codec_tag;
109
110 33733 par->bit_rate = codec->bit_rate;
111 33733 par->bits_per_coded_sample = codec->bits_per_coded_sample;
112 33733 par->bits_per_raw_sample = codec->bits_per_raw_sample;
113 33733 par->profile = codec->profile;
114 33733 par->level = codec->level;
115
116
4/4
✓ Branch 0 taken 25879 times.
✓ Branch 1 taken 7390 times.
✓ Branch 2 taken 324 times.
✓ Branch 3 taken 140 times.
33733 switch (par->codec_type) {
117 25879 case AVMEDIA_TYPE_VIDEO:
118 25879 par->format = codec->pix_fmt;
119 25879 par->width = codec->width;
120 25879 par->height = codec->height;
121 25879 par->field_order = codec->field_order;
122 25879 par->color_range = codec->color_range;
123 25879 par->color_primaries = codec->color_primaries;
124 25879 par->color_trc = codec->color_trc;
125 25879 par->color_space = codec->colorspace;
126 25879 par->chroma_location = codec->chroma_sample_location;
127 25879 par->sample_aspect_ratio = codec->sample_aspect_ratio;
128 25879 par->video_delay = codec->has_b_frames;
129 25879 break;
130 7390 case AVMEDIA_TYPE_AUDIO:
131 7390 par->format = codec->sample_fmt;
132 #if FF_API_OLD_CHANNEL_LAYOUT
133 FF_DISABLE_DEPRECATION_WARNINGS
134 // if the old/new fields are set inconsistently, prefer the old ones
135
4/4
✓ Branch 0 taken 6019 times.
✓ Branch 1 taken 1371 times.
✓ Branch 2 taken 6016 times.
✓ Branch 3 taken 3 times.
7390 if ((codec->channels && codec->channels != codec->ch_layout.nb_channels) ||
136
3/4
✓ Branch 0 taken 2512 times.
✓ Branch 1 taken 4875 times.
✓ Branch 2 taken 2512 times.
✗ Branch 3 not taken.
7387 (codec->channel_layout && (codec->ch_layout.order != AV_CHANNEL_ORDER_NATIVE ||
137
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2512 times.
2512 codec->ch_layout.u.mask != codec->channel_layout))) {
138
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (codec->channel_layout)
139 av_channel_layout_from_mask(&par->ch_layout, codec->channel_layout);
140 else {
141 3 par->ch_layout.order = AV_CHANNEL_ORDER_UNSPEC;
142 3 par->ch_layout.nb_channels = codec->channels;
143 }
144 FF_ENABLE_DEPRECATION_WARNINGS
145 } else {
146 #endif
147 7387 ret = av_channel_layout_copy(&par->ch_layout, &codec->ch_layout);
148
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7387 times.
7387 if (ret < 0)
149 return ret;
150 #if FF_API_OLD_CHANNEL_LAYOUT
151 FF_DISABLE_DEPRECATION_WARNINGS
152 }
153 14780 par->channel_layout = par->ch_layout.order == AV_CHANNEL_ORDER_NATIVE ?
154
2/2
✓ Branch 0 taken 5070 times.
✓ Branch 1 taken 2320 times.
7390 par->ch_layout.u.mask : 0;
155 7390 par->channels = par->ch_layout.nb_channels;
156 FF_ENABLE_DEPRECATION_WARNINGS
157 #endif
158 7390 par->sample_rate = codec->sample_rate;
159 7390 par->block_align = codec->block_align;
160 7390 par->frame_size = codec->frame_size;
161 7390 par->initial_padding = codec->initial_padding;
162 7390 par->trailing_padding = codec->trailing_padding;
163 7390 par->seek_preroll = codec->seek_preroll;
164 7390 break;
165 324 case AVMEDIA_TYPE_SUBTITLE:
166 324 par->width = codec->width;
167 324 par->height = codec->height;
168 324 break;
169 }
170
171
2/2
✓ Branch 0 taken 5294 times.
✓ Branch 1 taken 28439 times.
33733 if (codec->extradata) {
172 5294 par->extradata = av_mallocz(codec->extradata_size + AV_INPUT_BUFFER_PADDING_SIZE);
173
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5294 times.
5294 if (!par->extradata)
174 return AVERROR(ENOMEM);
175 5294 memcpy(par->extradata, codec->extradata, codec->extradata_size);
176 5294 par->extradata_size = codec->extradata_size;
177 }
178
179 33733 return 0;
180 }
181
182 35584 int avcodec_parameters_to_context(AVCodecContext *codec,
183 const AVCodecParameters *par)
184 {
185 int ret;
186
187 35584 codec->codec_type = par->codec_type;
188 35584 codec->codec_id = par->codec_id;
189 35584 codec->codec_tag = par->codec_tag;
190
191 35584 codec->bit_rate = par->bit_rate;
192 35584 codec->bits_per_coded_sample = par->bits_per_coded_sample;
193 35584 codec->bits_per_raw_sample = par->bits_per_raw_sample;
194 35584 codec->profile = par->profile;
195 35584 codec->level = par->level;
196
197
4/4
✓ Branch 0 taken 26445 times.
✓ Branch 1 taken 8445 times.
✓ Branch 2 taken 388 times.
✓ Branch 3 taken 306 times.
35584 switch (par->codec_type) {
198 26445 case AVMEDIA_TYPE_VIDEO:
199 26445 codec->pix_fmt = par->format;
200 26445 codec->width = par->width;
201 26445 codec->height = par->height;
202 26445 codec->field_order = par->field_order;
203 26445 codec->color_range = par->color_range;
204 26445 codec->color_primaries = par->color_primaries;
205 26445 codec->color_trc = par->color_trc;
206 26445 codec->colorspace = par->color_space;
207 26445 codec->chroma_sample_location = par->chroma_location;
208 26445 codec->sample_aspect_ratio = par->sample_aspect_ratio;
209 26445 codec->has_b_frames = par->video_delay;
210 26445 break;
211 8445 case AVMEDIA_TYPE_AUDIO:
212 8445 codec->sample_fmt = par->format;
213 #if FF_API_OLD_CHANNEL_LAYOUT
214 FF_DISABLE_DEPRECATION_WARNINGS
215 // if the old/new fields are set inconsistently, prefer the old ones
216
3/4
✓ Branch 0 taken 7933 times.
✓ Branch 1 taken 512 times.
✓ Branch 2 taken 7933 times.
✗ Branch 3 not taken.
8445 if ((par->channels && par->channels != par->ch_layout.nb_channels) ||
217
3/4
✓ Branch 0 taken 4285 times.
✓ Branch 1 taken 4160 times.
✓ Branch 2 taken 4285 times.
✗ Branch 3 not taken.
8445 (par->channel_layout && (par->ch_layout.order != AV_CHANNEL_ORDER_NATIVE ||
218
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4285 times.
4285 par->ch_layout.u.mask != par->channel_layout))) {
219 if (par->channel_layout)
220 av_channel_layout_from_mask(&codec->ch_layout, par->channel_layout);
221 else {
222 codec->ch_layout.order = AV_CHANNEL_ORDER_UNSPEC;
223 codec->ch_layout.nb_channels = par->channels;
224 }
225 FF_ENABLE_DEPRECATION_WARNINGS
226 } else {
227 #endif
228 8445 ret = av_channel_layout_copy(&codec->ch_layout, &par->ch_layout);
229
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8445 times.
8445 if (ret < 0)
230 return ret;
231 #if FF_API_OLD_CHANNEL_LAYOUT
232 FF_DISABLE_DEPRECATION_WARNINGS
233 }
234 16890 codec->channel_layout = codec->ch_layout.order == AV_CHANNEL_ORDER_NATIVE ?
235
2/2
✓ Branch 0 taken 4341 times.
✓ Branch 1 taken 4104 times.
8445 codec->ch_layout.u.mask : 0;
236 8445 codec->channels = codec->ch_layout.nb_channels;
237 FF_ENABLE_DEPRECATION_WARNINGS
238 #endif
239 8445 codec->sample_rate = par->sample_rate;
240 8445 codec->block_align = par->block_align;
241 8445 codec->frame_size = par->frame_size;
242 8445 codec->delay =
243 8445 codec->initial_padding = par->initial_padding;
244 8445 codec->trailing_padding = par->trailing_padding;
245 8445 codec->seek_preroll = par->seek_preroll;
246 8445 break;
247 388 case AVMEDIA_TYPE_SUBTITLE:
248 388 codec->width = par->width;
249 388 codec->height = par->height;
250 388 break;
251 }
252
253
2/2
✓ Branch 0 taken 5180 times.
✓ Branch 1 taken 30404 times.
35584 if (par->extradata) {
254 5180 av_freep(&codec->extradata);
255 5180 codec->extradata = av_mallocz(par->extradata_size + AV_INPUT_BUFFER_PADDING_SIZE);
256
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5180 times.
5180 if (!codec->extradata)
257 return AVERROR(ENOMEM);
258 5180 memcpy(codec->extradata, par->extradata, par->extradata_size);
259 5180 codec->extradata_size = par->extradata_size;
260 }
261
262 35584 return 0;
263 }
264