FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/tests/avcodec.c
Date: 2026-08-15 14:54:27
Exec Total Coverage
Lines: 77 117 65.8%
Functions: 2 3 66.7%
Branches: 136 201 67.7%

Line Branch Exec Source
1 /*
2 * This file is part of FFmpeg.
3 *
4 * FFmpeg is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * FFmpeg is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with FFmpeg; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18
19 #include "libavutil/opt.h"
20 #include "libavutil/pixdesc.h"
21 #include "libavcodec/codec.h"
22 #include "libavcodec/codec_desc.h"
23 #include "libavcodec/codec_internal.h"
24
25 static const char *get_type_string(enum AVMediaType type)
26 {
27 const char *ret = av_get_media_type_string(type);
28 return ret ? ret : "unknown";
29 }
30
31 #define AV_LOG(...) av_log(NULL, AV_LOG_FATAL, __VA_ARGS__)
32 #define ERR_INTERNAL(msg, ...) \
33 do { \
34 AV_LOG(msg, codec->name __VA_ARGS__); \
35 ret = 1; \
36 } while (0)
37 #define ERR(msg) ERR_INTERNAL(msg, )
38 #define ERR_EXT(msg, ...) ERR_INTERNAL(msg, , __VA_ARGS__)
39
40 704 static int priv_data_size_wrong(const FFCodec *codec)
41 {
42
1/2
✓ Branch 0 taken 704 times.
✗ Branch 1 not taken.
704 if (codec->priv_data_size < 0 ||
43
3/4
✓ Branch 0 taken 139 times.
✓ Branch 1 taken 565 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 139 times.
704 codec->p.priv_class && codec->priv_data_size < sizeof(AVClass*))
44 return 1;
45
3/4
✓ Branch 0 taken 139 times.
✓ Branch 1 taken 565 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 139 times.
704 if (!codec->p.priv_class || !codec->p.priv_class->option)
46 565 return 0;
47
2/2
✓ Branch 0 taken 1584 times.
✓ Branch 1 taken 139 times.
1723 for (const AVOption *opt = codec->p.priv_class->option; opt->name; opt++) {
48
1/2
✓ Branch 0 taken 1584 times.
✗ Branch 1 not taken.
1584 if (opt->offset >= codec->priv_data_size ||
49
3/4
✓ Branch 0 taken 770 times.
✓ Branch 1 taken 814 times.
✓ Branch 2 taken 770 times.
✗ Branch 3 not taken.
1584 opt->type == AV_OPT_TYPE_CONST && opt->offset != 0 ||
50
4/6
✓ Branch 0 taken 814 times.
✓ Branch 1 taken 770 times.
✓ Branch 2 taken 814 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 814 times.
1584 opt->type != AV_OPT_TYPE_CONST && (opt->offset < sizeof(AVClass*) || opt->offset < 0)) {
51 AV_LOG("Option %s offset %d nonsensical\n",
52 opt->name, opt->offset);
53 return 1;
54 }
55 }
56 139 return 0;
57 }
58
59 #define ARRAY_CHECK(field, var, type, is_sentinel, check, sentinel_check) \
60 do { \
61 const type *ptr = codec2->field; \
62 if (!ptr) \
63 break; \
64 type var = *ptr; \
65 if (is_sentinel) { \
66 ERR("Codec %s sets " #field ", but without valid elements.\n"); \
67 break; \
68 } \
69 do { \
70 if (!(check)) { \
71 ERR("Codec's %s " #field " array contains invalid element\n");\
72 break; \
73 } \
74 ++ptr; \
75 var = *ptr; \
76 } while (!(is_sentinel)); \
77 if (!(sentinel_check)) { \
78 ERR("Codec's %s " #field " array has malformed sentinel\n"); \
79 break; \
80 } \
81 } while (0)
82
83 1 int main(void){
84 1 void *iter = NULL;
85 1 const AVCodec *codec = NULL;
86 1 int ret = 0;
87
88
2/2
✓ Branch 1 taken 704 times.
✓ Branch 2 taken 1 times.
705 while (codec = av_codec_iterate(&iter)) {
89 704 const FFCodec *const codec2 = ffcodec(codec);
90 const AVCodecDescriptor *desc;
91 704 int is_decoder = 0, is_encoder = 0;
92
93
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 704 times.
704 if (!codec->name) {
94 AV_LOG("Codec for format %s has no name\n",
95 avcodec_get_name(codec->id));
96 ret = 1;
97 continue;
98 }
99
2/2
✓ Branch 0 taken 327 times.
✓ Branch 1 taken 377 times.
704 if (codec->type != AVMEDIA_TYPE_VIDEO &&
100
2/2
✓ Branch 0 taken 33 times.
✓ Branch 1 taken 294 times.
327 codec->type != AVMEDIA_TYPE_AUDIO &&
101
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 33 times.
33 codec->type != AVMEDIA_TYPE_SUBTITLE)
102 ERR_EXT("Codec %s has unsupported type %s\n",
103 get_type_string(codec->type));
104
2/2
✓ Branch 0 taken 410 times.
✓ Branch 1 taken 294 times.
704 if (codec->type != AVMEDIA_TYPE_AUDIO) {
105 #if defined(ASSERT_LEVEL) && ASSERT_LEVEL >= 2
106 if (codec2->ch_layouts || codec2->sample_fmts ||
107 codec2->supported_samplerates)
108 ERR("Non-audio codec %s has audio-only fields set\n");
109 #endif
110
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 410 times.
410 if (codec->capabilities & (AV_CODEC_CAP_SMALL_LAST_FRAME |
111 AV_CODEC_CAP_CHANNEL_CONF |
112 AV_CODEC_CAP_VARIABLE_FRAME_SIZE))
113 ERR("Non-audio codec %s has audio-only capabilities set\n");
114 } else {
115
6/8
✓ Branch 0 taken 275 times.
✓ Branch 1 taken 19 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 19 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 83 times.
✓ Branch 8 taken 64 times.
✓ Branch 9 taken 19 times.
358 ARRAY_CHECK(supported_samplerates, sample_rate, int, sample_rate == 0,
116 sample_rate > 0, 1);
117
6/8
✓ Branch 0 taken 199 times.
✓ Branch 1 taken 95 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 95 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 131 times.
✓ Branch 8 taken 36 times.
✓ Branch 9 taken 95 times.
330 ARRAY_CHECK(sample_fmts, sample_fmt, enum AVSampleFormat, sample_fmt == AV_SAMPLE_FMT_NONE,
118 (unsigned)sample_fmt < AV_SAMPLE_FMT_NB, 1);
119 static const AVChannelLayout zero_channel_layout = { 0 };
120
7/10
✓ Branch 0 taken 258 times.
✓ Branch 1 taken 36 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 36 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 166 times.
✓ Branch 9 taken 130 times.
✓ Branch 10 taken 36 times.
✗ Branch 11 not taken.
✓ Branch 12 taken 36 times.
424 ARRAY_CHECK(ch_layouts, ch_layout, AVChannelLayout, ch_layout.nb_channels == 0,
121 av_channel_layout_check(&ch_layout), !memcmp(ptr, &zero_channel_layout, sizeof(ch_layout)));
122 }
123
2/2
✓ Branch 0 taken 327 times.
✓ Branch 1 taken 377 times.
704 if (codec->type != AVMEDIA_TYPE_VIDEO) {
124
2/4
✓ Branch 0 taken 327 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 327 times.
327 if (codec2->color_ranges ||
125 #if defined(ASSERT_LEVEL) && ASSERT_LEVEL >= 2
126 codec2->pix_fmts || codec2->supported_framerates ||
127 #endif
128 codec2->alpha_modes)
129 ERR("Non-video codec %s has video-only fields set\n");
130
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 327 times.
327 if (codec2->caps_internal & FF_CODEC_CAP_EXPORTS_CROPPING)
131 ERR("Non-video codec %s exports cropping\n");
132 }
133
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 703 times.
704 if (codec2->caps_internal & FF_CODEC_CAP_SLICE_THREAD_HAS_MF &&
134
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 !(codec->capabilities & AV_CODEC_CAP_SLICE_THREADS))
135 ERR("Codec %s wants mainfunction despite not being "
136 "slice-threading capable");
137
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 703 times.
704 if (codec2->caps_internal & FF_CODEC_CAP_AUTO_THREADS &&
138
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 !(codec->capabilities & (AV_CODEC_CAP_FRAME_THREADS |
139 AV_CODEC_CAP_SLICE_THREADS |
140 AV_CODEC_CAP_OTHER_THREADS)))
141 ERR("Codec %s has private-only threading support\n");
142
143
2/3
✓ Branch 0 taken 518 times.
✓ Branch 1 taken 186 times.
✗ Branch 2 not taken.
704 switch (codec2->cb_type) {
144 518 case FF_CODEC_CB_TYPE_DECODE:
145 case FF_CODEC_CB_TYPE_DECODE_SUB:
146 case FF_CODEC_CB_TYPE_RECEIVE_FRAME:
147 518 is_decoder = 1;
148 518 break;
149 186 case FF_CODEC_CB_TYPE_ENCODE:
150 case FF_CODEC_CB_TYPE_ENCODE_SUB:
151 case FF_CODEC_CB_TYPE_RECEIVE_PACKET:
152 186 is_encoder = 1;
153 186 break;
154 default:
155 ERR("Codec %s has unknown cb_type\n");
156 continue;
157 }
158
2/4
✓ Branch 1 taken 704 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 704 times.
1408 if (is_decoder != av_codec_is_decoder(codec) ||
159 704 is_encoder != av_codec_is_encoder(codec)) {
160 ERR("Codec %s cb_type and av_codec_is_(de|en)coder inconsistent.\n");
161 continue;
162 }
163 #define CHECK(TYPE, type) (codec2->cb_type == FF_CODEC_CB_TYPE_ ## TYPE && !codec2->cb.type)
164
6/8
✓ Branch 0 taken 480 times.
✓ Branch 1 taken 224 times.
✓ Branch 2 taken 480 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 22 times.
✓ Branch 5 taken 682 times.
✓ Branch 6 taken 22 times.
✗ Branch 7 not taken.
704 if (CHECK(DECODE, decode) || CHECK(DECODE_SUB, decode_sub) ||
165
3/4
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 692 times.
✓ Branch 2 taken 12 times.
✗ Branch 3 not taken.
704 CHECK(RECEIVE_PACKET, receive_packet) ||
166
6/8
✓ Branch 0 taken 163 times.
✓ Branch 1 taken 541 times.
✓ Branch 2 taken 163 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 11 times.
✓ Branch 5 taken 693 times.
✓ Branch 6 taken 11 times.
✗ Branch 7 not taken.
704 CHECK(ENCODE, encode) || CHECK(ENCODE_SUB, encode_sub) ||
167
3/4
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 688 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 16 times.
704 CHECK(RECEIVE_FRAME, receive_frame)) {
168 ERR_EXT("Codec %s does not implement its %s callback.\n",
169 is_decoder ? "decoding" : "encoding");
170 }
171 #undef CHECK
172
2/2
✓ Branch 0 taken 186 times.
✓ Branch 1 taken 518 times.
704 if (is_encoder) {
173
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 186 times.
186 if ((codec->type == AVMEDIA_TYPE_SUBTITLE) != (codec2->cb_type == FF_CODEC_CB_TYPE_ENCODE_SUB))
174 ERR("Encoder %s is both subtitle encoder and not subtitle encoder.");
175
2/4
✓ Branch 0 taken 186 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 186 times.
186 if (codec2->update_thread_context || codec2->update_thread_context_for_user)
176 ERR("Encoder %s has decoder-only thread functions or bsf.\n");
177
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 186 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
186 if (codec2->reconf && !(codec->capabilities & AV_CODEC_CAP_ENCODER_RECONF))
178 ERR("Encoder %s has reconf callback without supporting recondiguration.\n");
179
2/2
✓ Branch 0 taken 74 times.
✓ Branch 1 taken 112 times.
186 if (codec->type == AVMEDIA_TYPE_AUDIO) {
180
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 74 times.
74 if (!codec2->sample_fmts) {
181 av_log(NULL, AV_LOG_FATAL, "Encoder %s is missing the sample_fmts field\n", codec->name);
182 ret = 1;
183 }
184
2/2
✓ Branch 0 taken 101 times.
✓ Branch 1 taken 11 times.
112 } else if (codec->type == AVMEDIA_TYPE_VIDEO) {
185
6/8
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 93 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 93 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 446 times.
✓ Branch 9 taken 353 times.
✓ Branch 10 taken 93 times.
454 ARRAY_CHECK(pix_fmts, pix_fmt, enum AVPixelFormat, pix_fmt == AV_PIX_FMT_NONE,
186 av_pix_fmt_desc_get(pix_fmt), 1);
187
8/12
✓ Branch 0 taken 99 times.
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
✓ Branch 5 taken 75 times.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✓ Branch 8 taken 75 times.
✓ Branch 10 taken 73 times.
✓ Branch 11 taken 2 times.
✗ Branch 12 not taken.
✓ Branch 13 taken 2 times.
174 ARRAY_CHECK(supported_framerates, framerate, AVRational, framerate.num == 0,
188 framerate.num > 0 && framerate.den > 0, framerate.den == 0);
189 }
190
1/2
✓ Branch 0 taken 186 times.
✗ Branch 1 not taken.
186 if (codec2->caps_internal & (FF_CODEC_CAP_USES_PROGRESSFRAMES |
191 FF_CODEC_CAP_SETS_PKT_DTS |
192 FF_CODEC_CAP_SKIP_FRAME_FILL_PARAM |
193 FF_CODEC_CAP_EXPORTS_CROPPING |
194 186 FF_CODEC_CAP_SETS_FRAME_PROPS) ||
195
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 186 times.
186 codec->capabilities & (AV_CODEC_CAP_AVOID_PROBING |
196 AV_CODEC_CAP_CHANNEL_CONF |
197 AV_CODEC_CAP_DRAW_HORIZ_BAND))
198 ERR("Encoder %s has decoder-only capabilities set\n");
199
2/2
✓ Branch 0 taken 25 times.
✓ Branch 1 taken 161 times.
186 if (codec->capabilities & AV_CODEC_CAP_FRAME_THREADS &&
200
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 25 times.
25 codec->capabilities & AV_CODEC_CAP_ENCODER_FLUSH)
201 ERR("Frame-threaded encoder %s claims to support flushing\n");
202
2/2
✓ Branch 0 taken 25 times.
✓ Branch 1 taken 161 times.
186 if (codec->capabilities & AV_CODEC_CAP_FRAME_THREADS &&
203
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 25 times.
25 codec->capabilities & AV_CODEC_CAP_DELAY)
204 ERR("Frame-threaded encoder %s claims to have delay\n");
205
206
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 183 times.
186 if (codec2->caps_internal & FF_CODEC_CAP_EOF_FLUSH &&
207
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 !(codec->capabilities & AV_CODEC_CAP_DELAY))
208 ERR("EOF_FLUSH encoder %s is not marked as having delay\n");
209 } else {
210
3/4
✓ Branch 0 taken 497 times.
✓ Branch 1 taken 21 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 497 times.
518 if ((codec2->update_thread_context || codec2->update_thread_context_for_user) &&
211
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 21 times.
21 !(codec->capabilities & AV_CODEC_CAP_FRAME_THREADS))
212 ERR("Non-frame-threaded decoder %s has update_thread_context set");
213
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 518 times.
518 if ((codec->type == AVMEDIA_TYPE_SUBTITLE) != (codec2->cb_type == FF_CODEC_CB_TYPE_DECODE_SUB))
214 ERR("Subtitle decoder %s does not implement decode_sub callback\n");
215
3/4
✓ Branch 0 taken 22 times.
✓ Branch 1 taken 496 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 22 times.
518 if (codec->type == AVMEDIA_TYPE_SUBTITLE && codec2->bsfs)
216 ERR("Automatic bitstream filtering unsupported for subtitles; "
217 "yet decoder %s has it set\n");
218
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 518 times.
518 if (codec->capabilities & (AV_CODEC_CAP_SMALL_LAST_FRAME |
219 AV_CODEC_CAP_VARIABLE_FRAME_SIZE |
220 AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE |
221 AV_CODEC_CAP_ENCODER_FLUSH |
222 AV_CODEC_CAP_ENCODER_RECONF |
223 AV_CODEC_CAP_ENCODER_RECON_FRAME))
224 ERR("Decoder %s has encoder-only capabilities\n");
225
2/2
✓ Branch 0 taken 38 times.
✓ Branch 1 taken 480 times.
518 if (codec2->cb_type != FF_CODEC_CB_TYPE_DECODE &&
226
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 38 times.
38 codec2->caps_internal & FF_CODEC_CAP_SETS_PKT_DTS)
227 ERR("Decoder %s is marked as setting pkt_dts when it doesn't have"
228 "any effect\n");
229
4/6
✓ Branch 0 taken 276 times.
✓ Branch 1 taken 242 times.
✓ Branch 2 taken 276 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 276 times.
518 if (codec->type == AVMEDIA_TYPE_VIDEO && (codec2->pix_fmts || codec2->supported_framerates))
230 ERR("Decoder %s sets pix_fmts or supported_framerates.\n");
231 }
232
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 704 times.
704 if (priv_data_size_wrong(codec2))
233 ERR_EXT("Private context of codec %s is impossibly-sized (size %d).",
234 codec2->priv_data_size);
235
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 704 times.
704 if (!(desc = avcodec_descriptor_get(codec->id))) {
236 ERR("Codec %s lacks a corresponding descriptor\n");
237 } else {
238
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 704 times.
704 if (desc->type != codec->type)
239 ERR_EXT("The type of AVCodec %s and its AVCodecDescriptor %s "
240 "differ: %s vs %s\n",
241 desc->name, get_type_string(codec->type), get_type_string(desc->type));
242
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 704 times.
704 if (desc->props & AV_CODEC_PROP_ENHANCEMENT)
243 ERR_EXT("Codec descriptor for codec %s (descriptor name %s) has "
244 "AV_CODEC_PROP_ENHANCEMENT flag set.\n", desc->name);
245 }
246 }
247 1 return ret;
248 }
249