FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/tests/avcodec.c
Date: 2025-04-25 22:50:00
Exec Total Coverage
Lines: 68 105 64.8%
Functions: 2 3 66.7%
Branches: 95 143 66.4%

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 "libavcodec/codec.h"
21 #include "libavcodec/codec_desc.h"
22 #include "libavcodec/codec_internal.h"
23
24 static const char *get_type_string(enum AVMediaType type)
25 {
26 const char *ret = av_get_media_type_string(type);
27 return ret ? ret : "unknown";
28 }
29
30 #define AV_LOG(...) av_log(NULL, AV_LOG_FATAL, __VA_ARGS__)
31 #define ERR_INTERNAL(msg, ...) \
32 do { \
33 AV_LOG(msg, codec->name __VA_ARGS__); \
34 ret = 1; \
35 } while (0)
36 #define ERR(msg) ERR_INTERNAL(msg, )
37 #define ERR_EXT(msg, ...) ERR_INTERNAL(msg, , __VA_ARGS__)
38
39 695 static int priv_data_size_wrong(const FFCodec *codec)
40 {
41
1/2
✓ Branch 0 taken 695 times.
✗ Branch 1 not taken.
695 if (codec->priv_data_size < 0 ||
42
3/4
✓ Branch 0 taken 140 times.
✓ Branch 1 taken 555 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 140 times.
695 codec->p.priv_class && codec->priv_data_size < sizeof(AVClass*))
43 return 1;
44
3/4
✓ Branch 0 taken 140 times.
✓ Branch 1 taken 555 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 140 times.
695 if (!codec->p.priv_class || !codec->p.priv_class->option)
45 555 return 0;
46
2/2
✓ Branch 0 taken 1599 times.
✓ Branch 1 taken 140 times.
1739 for (const AVOption *opt = codec->p.priv_class->option; opt->name; opt++) {
47
1/2
✓ Branch 0 taken 1599 times.
✗ Branch 1 not taken.
1599 if (opt->offset >= codec->priv_data_size ||
48
3/4
✓ Branch 0 taken 786 times.
✓ Branch 1 taken 813 times.
✓ Branch 2 taken 786 times.
✗ Branch 3 not taken.
1599 opt->type == AV_OPT_TYPE_CONST && opt->offset != 0 ||
49
4/6
✓ Branch 0 taken 813 times.
✓ Branch 1 taken 786 times.
✓ Branch 2 taken 813 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 813 times.
1599 opt->type != AV_OPT_TYPE_CONST && (opt->offset < sizeof(AVClass*) || opt->offset < 0)) {
50 AV_LOG("Option %s offset %d nonsensical\n",
51 opt->name, opt->offset);
52 return 1;
53 }
54 }
55 140 return 0;
56 }
57
58 1 int main(void){
59 1 void *iter = NULL;
60 1 const AVCodec *codec = NULL;
61 1 int ret = 0;
62
63
2/2
✓ Branch 1 taken 695 times.
✓ Branch 2 taken 1 times.
696 while (codec = av_codec_iterate(&iter)) {
64 695 const FFCodec *const codec2 = ffcodec(codec);
65 const AVCodecDescriptor *desc;
66 695 int is_decoder = 0, is_encoder = 0;
67
68
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 695 times.
695 if (!codec->name) {
69 AV_LOG("Codec for format %s has no name\n",
70 avcodec_get_name(codec->id));
71 ret = 1;
72 continue;
73 }
74
2/2
✓ Branch 0 taken 316 times.
✓ Branch 1 taken 379 times.
695 if (codec->type != AVMEDIA_TYPE_VIDEO &&
75
2/2
✓ Branch 0 taken 33 times.
✓ Branch 1 taken 283 times.
316 codec->type != AVMEDIA_TYPE_AUDIO &&
76
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 33 times.
33 codec->type != AVMEDIA_TYPE_SUBTITLE)
77 ERR_EXT("Codec %s has unsupported type %s\n",
78 get_type_string(codec->type));
79
2/2
✓ Branch 0 taken 412 times.
✓ Branch 1 taken 283 times.
695 if (codec->type != AVMEDIA_TYPE_AUDIO) {
80 FF_DISABLE_DEPRECATION_WARNINGS
81
2/4
✓ Branch 0 taken 412 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 412 times.
✗ Branch 3 not taken.
412 if (codec->ch_layouts || codec->sample_fmts ||
82
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 412 times.
412 codec->supported_samplerates)
83 ERR("Non-audio codec %s has audio-only fields set\n");
84 FF_ENABLE_DEPRECATION_WARNINGS
85
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 412 times.
412 if (codec->capabilities & (AV_CODEC_CAP_SMALL_LAST_FRAME |
86 AV_CODEC_CAP_CHANNEL_CONF |
87 AV_CODEC_CAP_VARIABLE_FRAME_SIZE))
88 ERR("Non-audio codec %s has audio-only capabilities set\n");
89 }
90
2/2
✓ Branch 0 taken 316 times.
✓ Branch 1 taken 379 times.
695 if (codec->type != AVMEDIA_TYPE_VIDEO) {
91 FF_DISABLE_DEPRECATION_WARNINGS
92
2/4
✓ Branch 0 taken 316 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 316 times.
316 if (codec->pix_fmts || codec->supported_framerates)
93 ERR("Non-video codec %s has video-only fields set\n");
94 FF_ENABLE_DEPRECATION_WARNINGS
95
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 316 times.
316 if (codec2->caps_internal & FF_CODEC_CAP_EXPORTS_CROPPING)
96 ERR("Non-video codec %s exports cropping\n");
97 }
98
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 694 times.
695 if (codec2->caps_internal & FF_CODEC_CAP_SLICE_THREAD_HAS_MF &&
99
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 !(codec->capabilities & AV_CODEC_CAP_SLICE_THREADS))
100 ERR("Codec %s wants mainfunction despite not being "
101 "slice-threading capable");
102
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 694 times.
695 if (codec2->caps_internal & FF_CODEC_CAP_AUTO_THREADS &&
103
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 !(codec->capabilities & (AV_CODEC_CAP_FRAME_THREADS |
104 AV_CODEC_CAP_SLICE_THREADS |
105 AV_CODEC_CAP_OTHER_THREADS)))
106 ERR("Codec %s has private-only threading support\n");
107
108
2/3
✓ Branch 0 taken 507 times.
✓ Branch 1 taken 188 times.
✗ Branch 2 not taken.
695 switch (codec2->cb_type) {
109 507 case FF_CODEC_CB_TYPE_DECODE:
110 case FF_CODEC_CB_TYPE_DECODE_SUB:
111 case FF_CODEC_CB_TYPE_RECEIVE_FRAME:
112 507 is_decoder = 1;
113 507 break;
114 188 case FF_CODEC_CB_TYPE_ENCODE:
115 case FF_CODEC_CB_TYPE_ENCODE_SUB:
116 case FF_CODEC_CB_TYPE_RECEIVE_PACKET:
117 188 is_encoder = 1;
118 188 break;
119 default:
120 ERR("Codec %s has unknown cb_type\n");
121 continue;
122 }
123
2/4
✓ Branch 1 taken 695 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 695 times.
1390 if (is_decoder != av_codec_is_decoder(codec) ||
124 695 is_encoder != av_codec_is_encoder(codec)) {
125 ERR("Codec %s cb_type and av_codec_is_(de|en)coder inconsistent.\n");
126 continue;
127 }
128 #define CHECK(TYPE, type) (codec2->cb_type == FF_CODEC_CB_TYPE_ ## TYPE && !codec2->cb.type)
129
6/8
✓ Branch 0 taken 470 times.
✓ Branch 1 taken 225 times.
✓ Branch 2 taken 470 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 22 times.
✓ Branch 5 taken 673 times.
✓ Branch 6 taken 22 times.
✗ Branch 7 not taken.
695 if (CHECK(DECODE, decode) || CHECK(DECODE_SUB, decode_sub) ||
130
3/4
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 683 times.
✓ Branch 2 taken 12 times.
✗ Branch 3 not taken.
695 CHECK(RECEIVE_PACKET, receive_packet) ||
131
6/8
✓ Branch 0 taken 165 times.
✓ Branch 1 taken 530 times.
✓ Branch 2 taken 165 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 11 times.
✓ Branch 5 taken 684 times.
✓ Branch 6 taken 11 times.
✗ Branch 7 not taken.
695 CHECK(ENCODE, encode) || CHECK(ENCODE_SUB, encode_sub) ||
132
3/4
✓ Branch 0 taken 15 times.
✓ Branch 1 taken 680 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 15 times.
695 CHECK(RECEIVE_FRAME, receive_frame)) {
133 ERR_EXT("Codec %s does not implement its %s callback.\n",
134 is_decoder ? "decoding" : "encoding");
135 }
136 #undef CHECK
137
2/2
✓ Branch 0 taken 188 times.
✓ Branch 1 taken 507 times.
695 if (is_encoder) {
138
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))
139 ERR("Encoder %s is both subtitle encoder and not subtitle encoder.");
140
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)
141 ERR("Encoder %s has decoder-only thread functions or bsf.\n");
142
2/2
✓ Branch 0 taken 74 times.
✓ Branch 1 taken 114 times.
188 if (codec->type == AVMEDIA_TYPE_AUDIO) {
143 FF_DISABLE_DEPRECATION_WARNINGS
144
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 74 times.
74 if (!codec->sample_fmts) {
145 av_log(NULL, AV_LOG_FATAL, "Encoder %s is missing the sample_fmts field\n", codec->name);
146 ret = 1;
147 }
148 FF_ENABLE_DEPRECATION_WARNINGS
149 }
150
1/2
✓ Branch 0 taken 188 times.
✗ Branch 1 not taken.
188 if (codec2->caps_internal & (FF_CODEC_CAP_USES_PROGRESSFRAMES |
151 FF_CODEC_CAP_SETS_PKT_DTS |
152 FF_CODEC_CAP_SKIP_FRAME_FILL_PARAM |
153 FF_CODEC_CAP_EXPORTS_CROPPING |
154 188 FF_CODEC_CAP_SETS_FRAME_PROPS) ||
155
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 188 times.
188 codec->capabilities & (AV_CODEC_CAP_AVOID_PROBING |
156 AV_CODEC_CAP_CHANNEL_CONF |
157 AV_CODEC_CAP_DRAW_HORIZ_BAND))
158 ERR("Encoder %s has decoder-only capabilities set\n");
159
2/2
✓ Branch 0 taken 25 times.
✓ Branch 1 taken 163 times.
188 if (codec->capabilities & AV_CODEC_CAP_FRAME_THREADS &&
160
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 25 times.
25 codec->capabilities & AV_CODEC_CAP_ENCODER_FLUSH)
161 ERR("Frame-threaded encoder %s claims to support flushing\n");
162
2/2
✓ Branch 0 taken 25 times.
✓ Branch 1 taken 163 times.
188 if (codec->capabilities & AV_CODEC_CAP_FRAME_THREADS &&
163
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 25 times.
25 codec->capabilities & AV_CODEC_CAP_DELAY)
164 ERR("Frame-threaded encoder %s claims to have delay\n");
165
166
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 185 times.
188 if (codec2->caps_internal & FF_CODEC_CAP_EOF_FLUSH &&
167
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 !(codec->capabilities & AV_CODEC_CAP_DELAY))
168 ERR("EOF_FLUSH encoder %s is not marked as having delay\n");
169 } else {
170
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 507 times.
507 if ((codec->type == AVMEDIA_TYPE_SUBTITLE) != (codec2->cb_type == FF_CODEC_CB_TYPE_DECODE_SUB))
171 ERR("Subtitle decoder %s does not implement decode_sub callback\n");
172
3/4
✓ Branch 0 taken 22 times.
✓ Branch 1 taken 485 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 22 times.
507 if (codec->type == AVMEDIA_TYPE_SUBTITLE && codec2->bsfs)
173 ERR("Automatic bitstream filtering unsupported for subtitles; "
174 "yet decoder %s has it set\n");
175
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 507 times.
507 if (codec->capabilities & (AV_CODEC_CAP_SMALL_LAST_FRAME |
176 AV_CODEC_CAP_VARIABLE_FRAME_SIZE |
177 AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE |
178 AV_CODEC_CAP_ENCODER_FLUSH))
179 ERR("Decoder %s has encoder-only capabilities\n");
180
2/2
✓ Branch 0 taken 37 times.
✓ Branch 1 taken 470 times.
507 if (codec2->cb_type != FF_CODEC_CB_TYPE_DECODE &&
181
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 37 times.
37 codec2->caps_internal & FF_CODEC_CAP_SETS_PKT_DTS)
182 ERR("Decoder %s is marked as setting pkt_dts when it doesn't have"
183 "any effect\n");
184 }
185
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 695 times.
695 if (priv_data_size_wrong(codec2))
186 ERR_EXT("Private context of codec %s is impossibly-sized (size %d).",
187 codec2->priv_data_size);
188
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 695 times.
695 if (!(desc = avcodec_descriptor_get(codec->id))) {
189 ERR("Codec %s lacks a corresponding descriptor\n");
190
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 695 times.
695 } else if (desc->type != codec->type)
191 ERR_EXT("The type of AVCodec %s and its AVCodecDescriptor differ: "
192 "%s vs %s\n",
193 get_type_string(codec->type), get_type_string(desc->type));
194 }
195 1 return ret;
196 }
197