FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/tests/avcodec.c
Date: 2025-11-27 19:16:05
Exec Total Coverage
Lines: 78 117 66.7%
Functions: 2 3 66.7%
Branches: 140 207 67.6%

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 708 static int priv_data_size_wrong(const FFCodec *codec)
41 {
42
1/2
✓ Branch 0 taken 708 times.
✗ Branch 1 not taken.
708 if (codec->priv_data_size < 0 ||
43
3/4
✓ Branch 0 taken 142 times.
✓ Branch 1 taken 566 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 142 times.
708 codec->p.priv_class && codec->priv_data_size < sizeof(AVClass*))
44 return 1;
45
3/4
✓ Branch 0 taken 142 times.
✓ Branch 1 taken 566 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 142 times.
708 if (!codec->p.priv_class || !codec->p.priv_class->option)
46 566 return 0;
47
2/2
✓ Branch 0 taken 1600 times.
✓ Branch 1 taken 142 times.
1742 for (const AVOption *opt = codec->p.priv_class->option; opt->name; opt++) {
48
1/2
✓ Branch 0 taken 1600 times.
✗ Branch 1 not taken.
1600 if (opt->offset >= codec->priv_data_size ||
49
3/4
✓ Branch 0 taken 785 times.
✓ Branch 1 taken 815 times.
✓ Branch 2 taken 785 times.
✗ Branch 3 not taken.
1600 opt->type == AV_OPT_TYPE_CONST && opt->offset != 0 ||
50
4/6
✓ Branch 0 taken 815 times.
✓ Branch 1 taken 785 times.
✓ Branch 2 taken 815 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 815 times.
1600 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 142 return 0;
57 }
58
59 #define ARRAY_CHECK(field, var, type, is_sentinel, check, sentinel_check) \
60 do { \
61 const type *ptr = codec->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 708 times.
✓ Branch 2 taken 1 times.
709 while (codec = av_codec_iterate(&iter)) {
89 708 const FFCodec *const codec2 = ffcodec(codec);
90 const AVCodecDescriptor *desc;
91 708 int is_decoder = 0, is_encoder = 0;
92
93
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 708 times.
708 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 381 times.
708 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 414 times.
✓ Branch 1 taken 294 times.
708 if (codec->type != AVMEDIA_TYPE_AUDIO) {
105 FF_DISABLE_DEPRECATION_WARNINGS
106
2/4
✓ Branch 0 taken 414 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 414 times.
✗ Branch 3 not taken.
414 if (codec->ch_layouts || codec->sample_fmts ||
107
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 414 times.
414 codec->supported_samplerates)
108 ERR("Non-audio codec %s has audio-only fields set\n");
109 FF_ENABLE_DEPRECATION_WARNINGS
110
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 414 times.
414 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 FF_DISABLE_DEPRECATION_WARNINGS
116
6/8
✓ Branch 0 taken 276 times.
✓ Branch 1 taken 18 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 18 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 83 times.
✓ Branch 8 taken 65 times.
✓ Branch 9 taken 18 times.
359 ARRAY_CHECK(supported_samplerates, sample_rate, int, sample_rate == 0,
117 sample_rate > 0, 1);
118
6/8
✓ Branch 0 taken 94 times.
✓ Branch 1 taken 200 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 200 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 251 times.
✓ Branch 8 taken 51 times.
✓ Branch 9 taken 200 times.
345 ARRAY_CHECK(sample_fmts, sample_fmt, enum AVSampleFormat, sample_fmt == AV_SAMPLE_FMT_NONE,
119 (unsigned)sample_fmt < AV_SAMPLE_FMT_NB, 1);
120 static const AVChannelLayout zero_channel_layout = { 0 };
121
7/10
✓ Branch 0 taken 256 times.
✓ Branch 1 taken 38 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 38 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 177 times.
✓ Branch 9 taken 139 times.
✓ Branch 10 taken 38 times.
✗ Branch 11 not taken.
✓ Branch 12 taken 38 times.
433 ARRAY_CHECK(ch_layouts, ch_layout, AVChannelLayout, ch_layout.nb_channels == 0,
122 av_channel_layout_check(&ch_layout), !memcmp(ptr, &zero_channel_layout, sizeof(ch_layout)));
123 FF_ENABLE_DEPRECATION_WARNINGS
124 }
125
2/2
✓ Branch 0 taken 327 times.
✓ Branch 1 taken 381 times.
708 if (codec->type != AVMEDIA_TYPE_VIDEO) {
126 FF_DISABLE_DEPRECATION_WARNINGS
127
3/6
✓ Branch 0 taken 327 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 327 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 327 times.
✗ Branch 5 not taken.
327 if (codec->pix_fmts || codec->supported_framerates ||
128
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 327 times.
327 codec2->color_ranges || codec2->alpha_modes)
129 ERR("Non-video codec %s has video-only fields set\n");
130 FF_ENABLE_DEPRECATION_WARNINGS
131
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 327 times.
327 if (codec2->caps_internal & FF_CODEC_CAP_EXPORTS_CROPPING)
132 ERR("Non-video codec %s exports cropping\n");
133 }
134
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 707 times.
708 if (codec2->caps_internal & FF_CODEC_CAP_SLICE_THREAD_HAS_MF &&
135
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 !(codec->capabilities & AV_CODEC_CAP_SLICE_THREADS))
136 ERR("Codec %s wants mainfunction despite not being "
137 "slice-threading capable");
138
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 707 times.
708 if (codec2->caps_internal & FF_CODEC_CAP_AUTO_THREADS &&
139
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 !(codec->capabilities & (AV_CODEC_CAP_FRAME_THREADS |
140 AV_CODEC_CAP_SLICE_THREADS |
141 AV_CODEC_CAP_OTHER_THREADS)))
142 ERR("Codec %s has private-only threading support\n");
143
144
2/3
✓ Branch 0 taken 520 times.
✓ Branch 1 taken 188 times.
✗ Branch 2 not taken.
708 switch (codec2->cb_type) {
145 520 case FF_CODEC_CB_TYPE_DECODE:
146 case FF_CODEC_CB_TYPE_DECODE_SUB:
147 case FF_CODEC_CB_TYPE_RECEIVE_FRAME:
148 520 is_decoder = 1;
149 520 break;
150 188 case FF_CODEC_CB_TYPE_ENCODE:
151 case FF_CODEC_CB_TYPE_ENCODE_SUB:
152 case FF_CODEC_CB_TYPE_RECEIVE_PACKET:
153 188 is_encoder = 1;
154 188 break;
155 default:
156 ERR("Codec %s has unknown cb_type\n");
157 continue;
158 }
159
2/4
✓ Branch 1 taken 708 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 708 times.
1416 if (is_decoder != av_codec_is_decoder(codec) ||
160 708 is_encoder != av_codec_is_encoder(codec)) {
161 ERR("Codec %s cb_type and av_codec_is_(de|en)coder inconsistent.\n");
162 continue;
163 }
164 #define CHECK(TYPE, type) (codec2->cb_type == FF_CODEC_CB_TYPE_ ## TYPE && !codec2->cb.type)
165
6/8
✓ Branch 0 taken 482 times.
✓ Branch 1 taken 226 times.
✓ Branch 2 taken 482 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 22 times.
✓ Branch 5 taken 686 times.
✓ Branch 6 taken 22 times.
✗ Branch 7 not taken.
708 if (CHECK(DECODE, decode) || CHECK(DECODE_SUB, decode_sub) ||
166
3/4
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 696 times.
✓ Branch 2 taken 12 times.
✗ Branch 3 not taken.
708 CHECK(RECEIVE_PACKET, receive_packet) ||
167
6/8
✓ Branch 0 taken 165 times.
✓ Branch 1 taken 543 times.
✓ Branch 2 taken 165 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 11 times.
✓ Branch 5 taken 697 times.
✓ Branch 6 taken 11 times.
✗ Branch 7 not taken.
708 CHECK(ENCODE, encode) || CHECK(ENCODE_SUB, encode_sub) ||
168
3/4
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 692 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 16 times.
708 CHECK(RECEIVE_FRAME, receive_frame)) {
169 ERR_EXT("Codec %s does not implement its %s callback.\n",
170 is_decoder ? "decoding" : "encoding");
171 }
172 #undef CHECK
173
2/2
✓ Branch 0 taken 188 times.
✓ Branch 1 taken 520 times.
708 if (is_encoder) {
174
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 188 times.
188 if ((codec->type == AVMEDIA_TYPE_SUBTITLE) != (codec2->cb_type == FF_CODEC_CB_TYPE_ENCODE_SUB))
175 ERR("Encoder %s is both subtitle encoder and not subtitle encoder.");
176
3/6
✓ Branch 0 taken 188 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 188 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 188 times.
188 if (codec2->update_thread_context || codec2->update_thread_context_for_user || codec2->bsfs)
177 ERR("Encoder %s has decoder-only thread functions or bsf.\n");
178
2/2
✓ Branch 0 taken 74 times.
✓ Branch 1 taken 114 times.
188 if (codec->type == AVMEDIA_TYPE_AUDIO) {
179 FF_DISABLE_DEPRECATION_WARNINGS
180
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 74 times.
74 if (!codec->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 103 times.
✓ Branch 1 taken 11 times.
114 } else if (codec->type == AVMEDIA_TYPE_VIDEO) {
185
6/8
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 95 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 95 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 447 times.
✓ Branch 9 taken 352 times.
✓ Branch 10 taken 95 times.
455 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 101 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.
176 ARRAY_CHECK(supported_framerates, framerate, AVRational, framerate.num == 0,
188 framerate.num > 0 && framerate.den > 0, framerate.den == 0);
189 FF_ENABLE_DEPRECATION_WARNINGS
190 }
191
1/2
✓ Branch 0 taken 188 times.
✗ Branch 1 not taken.
188 if (codec2->caps_internal & (FF_CODEC_CAP_USES_PROGRESSFRAMES |
192 FF_CODEC_CAP_SETS_PKT_DTS |
193 FF_CODEC_CAP_SKIP_FRAME_FILL_PARAM |
194 FF_CODEC_CAP_EXPORTS_CROPPING |
195 188 FF_CODEC_CAP_SETS_FRAME_PROPS) ||
196
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 188 times.
188 codec->capabilities & (AV_CODEC_CAP_AVOID_PROBING |
197 AV_CODEC_CAP_CHANNEL_CONF |
198 AV_CODEC_CAP_DRAW_HORIZ_BAND))
199 ERR("Encoder %s has decoder-only capabilities set\n");
200
2/2
✓ Branch 0 taken 25 times.
✓ Branch 1 taken 163 times.
188 if (codec->capabilities & AV_CODEC_CAP_FRAME_THREADS &&
201
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 25 times.
25 codec->capabilities & AV_CODEC_CAP_ENCODER_FLUSH)
202 ERR("Frame-threaded encoder %s claims to support flushing\n");
203
2/2
✓ Branch 0 taken 25 times.
✓ Branch 1 taken 163 times.
188 if (codec->capabilities & AV_CODEC_CAP_FRAME_THREADS &&
204
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 25 times.
25 codec->capabilities & AV_CODEC_CAP_DELAY)
205 ERR("Frame-threaded encoder %s claims to have delay\n");
206
207
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 185 times.
188 if (codec2->caps_internal & FF_CODEC_CAP_EOF_FLUSH &&
208
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 !(codec->capabilities & AV_CODEC_CAP_DELAY))
209 ERR("EOF_FLUSH encoder %s is not marked as having delay\n");
210 } else {
211
3/4
✓ Branch 0 taken 500 times.
✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 500 times.
520 if ((codec2->update_thread_context || codec2->update_thread_context_for_user) &&
212
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 20 times.
20 !(codec->capabilities & AV_CODEC_CAP_FRAME_THREADS))
213 ERR("Non-frame-threaded decoder %s has update_thread_context set");
214
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 520 times.
520 if ((codec->type == AVMEDIA_TYPE_SUBTITLE) != (codec2->cb_type == FF_CODEC_CB_TYPE_DECODE_SUB))
215 ERR("Subtitle decoder %s does not implement decode_sub callback\n");
216
3/4
✓ Branch 0 taken 22 times.
✓ Branch 1 taken 498 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 22 times.
520 if (codec->type == AVMEDIA_TYPE_SUBTITLE && codec2->bsfs)
217 ERR("Automatic bitstream filtering unsupported for subtitles; "
218 "yet decoder %s has it set\n");
219
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 520 times.
520 if (codec->capabilities & (AV_CODEC_CAP_SMALL_LAST_FRAME |
220 AV_CODEC_CAP_VARIABLE_FRAME_SIZE |
221 AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE |
222 AV_CODEC_CAP_ENCODER_FLUSH))
223 ERR("Decoder %s has encoder-only capabilities\n");
224
2/2
✓ Branch 0 taken 38 times.
✓ Branch 1 taken 482 times.
520 if (codec2->cb_type != FF_CODEC_CB_TYPE_DECODE &&
225
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 38 times.
38 codec2->caps_internal & FF_CODEC_CAP_SETS_PKT_DTS)
226 ERR("Decoder %s is marked as setting pkt_dts when it doesn't have"
227 "any effect\n");
228 FF_DISABLE_DEPRECATION_WARNINGS
229
4/6
✓ Branch 0 taken 278 times.
✓ Branch 1 taken 242 times.
✓ Branch 2 taken 278 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 278 times.
520 if (codec->type == AVMEDIA_TYPE_VIDEO && (codec->pix_fmts || codec->supported_framerates))
230 ERR("Decoder %s sets pix_fmts or supported_framerates.\n");
231 FF_ENABLE_DEPRECATION_WARNINGS
232 }
233
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 708 times.
708 if (priv_data_size_wrong(codec2))
234 ERR_EXT("Private context of codec %s is impossibly-sized (size %d).",
235 codec2->priv_data_size);
236
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 708 times.
708 if (!(desc = avcodec_descriptor_get(codec->id))) {
237 ERR("Codec %s lacks a corresponding descriptor\n");
238
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 708 times.
708 } else if (desc->type != codec->type)
239 ERR_EXT("The type of AVCodec %s and its AVCodecDescriptor differ: "
240 "%s vs %s\n",
241 get_type_string(codec->type), get_type_string(desc->type));
242 }
243 1 return ret;
244 }
245