FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/h265_profile_level.c
Date: 2024-04-15 22:47:37
Exec Total Coverage
Lines: 66 75 88.0%
Functions: 2 2 100.0%
Branches: 83 98 84.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 "h265_profile_level.h"
20
21
22 static const H265LevelDescriptor h265_levels[] = {
23 // Name CpbFactor-Main MaxSliceSegmentsPerPicture
24 // | level_idc | CpbFactor-High MaxLumaSr BrFactor-High
25 // | | MaxLumaPs | | | MaxTileRows | BrFactor-Main | MinCr-Main
26 // | | | | | | | MaxTileCols | | | MinCr-High
27 { "1", 30, 36864, 350, 0, 16, 1, 1, 552960, 128, 0, 2, 2 },
28 { "2", 60, 122880, 1500, 0, 16, 1, 1, 3686400, 1500, 0, 2, 2 },
29 { "2.1", 63, 245760, 3000, 0, 20, 1, 1, 7372800, 3000, 0, 2, 2 },
30 { "3", 90, 552960, 6000, 0, 30, 2, 2, 16588800, 6000, 0, 2, 2 },
31 { "3.1", 93, 983040, 10000, 0, 40, 3, 3, 33177600, 10000, 0, 2, 2 },
32 { "4", 120, 2228224, 12000, 30000, 75, 5, 5, 66846720, 12000, 30000, 4, 4 },
33 { "4.1", 123, 2228224, 20000, 50000, 75, 5, 5, 133693440, 20000, 50000, 4, 4 },
34 { "5", 150, 8912896, 25000, 100000, 200, 11, 10, 267386880, 25000, 100000, 6, 4 },
35 { "5.1", 153, 8912896, 40000, 160000, 200, 11, 10, 534773760, 40000, 160000, 8, 4 },
36 { "5.2", 156, 8912896, 60000, 240000, 200, 11, 10, 1069547520, 60000, 240000, 8, 4 },
37 { "6", 180, 35651584, 60000, 240000, 600, 22, 20, 1069547520, 60000, 240000, 8, 4 },
38 { "6.1", 183, 35651584, 120000, 480000, 600, 22, 20, 2139095040, 120000, 480000, 8, 4 },
39 { "6.2", 186, 35651584, 240000, 800000, 600, 22, 20, 4278190080, 240000, 800000, 6, 4 },
40 };
41
42 static const H265ProfileDescriptor h265_profiles[] = {
43 // profile_idc 8bit one-picture
44 // HT-profile | 422chroma | lower-bit-rate
45 // | 14bit | | 420chroma | | CpbVclFactor MinCrScaleFactor
46 // | | 12bit | | | monochrome| | CpbNalFactor | maxDpbPicBuf
47 // | | | 10bit | | | intra | | | FormatCapabilityFactor
48 { "Monochrome", // | | | | | | | | | | |
49 4, 0, 2, 1, 1, 1, 1, 1, 1, 0, 0, 1, 667, 733, 1.000, 1.0, 6 },
50 { "Monochrome 10",
51 4, 0, 2, 1, 1, 0, 1, 1, 1, 0, 0, 1, 833, 917, 1.250, 1.0, 6 },
52 { "Monochrome 12",
53 4, 0, 2, 1, 0, 0, 1, 1, 1, 0, 0, 1, 1000, 1100, 1.500, 1.0, 6 },
54 { "Monochrome 16",
55 4, 0, 2, 0, 0, 0, 1, 1, 1, 0, 0, 1, 1333, 1467, 2.000, 1.0, 6 },
56 { "Main",
57 1, 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1000, 1100, 1.500, 1.0, 6 },
58 { "Screen-Extended Main",
59 9, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1000, 1100, 1.500, 1.0, 7 },
60 { "Main 10",
61 2, 0, 2, 2, 2, 2, 2, 2, 2, 2, 0, 2, 1000, 1100, 1.875, 1.0, 6 },
62 { "Screen-Extended Main 10",
63 9, 0, 1, 1, 1, 0, 1, 1, 0, 0, 0, 1, 1000, 1100, 1.875, 1.0, 7 },
64 { "Main 12",
65 4, 0, 2, 1, 0, 0, 1, 1, 0, 0, 0, 1, 1500, 1650, 2.250, 1.0, 6 },
66 { "Main Still Picture",
67 3, 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1000, 1100, 1.500, 1.0, 6 },
68 { "Main 10 Still Picture",
69 2, 0, 2, 2, 2, 2, 2, 2, 2, 2, 1, 2, 1000, 1100, 1.875, 1.0, 6 },
70 { "Main 4:2:2 10",
71 4, 0, 2, 1, 1, 0, 1, 0, 0, 0, 0, 1, 1667, 1833, 2.500, 0.5, 6 },
72 { "Main 4:2:2 12",
73 4, 0, 2, 1, 0, 0, 1, 0, 0, 0, 0, 1, 2000, 2200, 3.000, 0.5, 6 },
74 { "Main 4:4:4",
75 4, 0, 2, 1, 1, 1, 0, 0, 0, 0, 0, 1, 2000, 2200, 3.000, 0.5, 6 },
76 { "High Throughput 4:4:4",
77 5, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 2000, 2200, 3.000, 0.5, 6 },
78 { "Screen-Extended Main 4:4:4",
79 9, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 2000, 2200, 3.000, 0.5, 7 },
80 { "Screen-Extended High Throughput 4:4:4",
81 9, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 2000, 2200, 3.000, 0.5, 7 },
82 { "Main 4:4:4 10",
83 4, 0, 2, 1, 1, 0, 0, 0, 0, 0, 0, 1, 2500, 2750, 3.750, 0.5, 6 },
84 { "High Throughput 4:4:4 10",
85 5, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 2500, 2750, 3.750, 0.5, 6 },
86 { "Screen-Extended Main 4:4:4 10",
87 9, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 2500, 2750, 3.750, 0.5, 7 },
88 { "Screen-Extended High Throughput 4:4:4 10",
89 9, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 2500, 2750, 3.750, 0.5, 7 },
90 { "Main 4:4:4 12",
91 4, 0, 2, 1, 0, 0, 0, 0, 0, 0, 0, 1, 3000, 3300, 4.500, 0.5, 6 },
92 { "High Throughput 4:4:4 14",
93 5, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 3500, 3850, 5.250, 0.5, 6 },
94 { "Screen-Extended High Throughput 4:4:4 14",
95 9, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 3500, 3850, 5.250, 0.5, 7 },
96 { "Main Intra",
97 4, 0, 2, 1, 1, 1, 1, 1, 0, 1, 0, 2, 1000, 1100, 1.500, 1.0, 6 },
98 { "Main 10 Intra",
99 4, 0, 2, 1, 1, 0, 1, 1, 0, 1, 0, 2, 1000, 1100, 1.875, 1.0, 6 },
100 { "Main 12 Intra",
101 4, 0, 2, 1, 0, 0, 1, 1, 0, 1, 0, 2, 1500, 1650, 2.250, 1.0, 6 },
102 { "Main 4:2:2 10 Intra",
103 4, 0, 2, 1, 1, 0, 1, 0, 0, 1, 0, 2, 1667, 1833, 2.500, 0.5, 6 },
104 { "Main 4:2:2 12 Intra",
105 4, 0, 2, 1, 0, 0, 1, 0, 0, 1, 0, 2, 2000, 2200, 3.000, 0.5, 6 },
106 { "Main 4:4:4 Intra",
107 4, 0, 2, 1, 1, 1, 0, 0, 0, 1, 0, 2, 2000, 2200, 3.000, 0.5, 6 },
108 { "Main 4:4:4 10 Intra",
109 4, 0, 2, 1, 1, 0, 0, 0, 0, 1, 0, 2, 2500, 2750, 3.750, 0.5, 6 },
110 { "Main 4:4:4 12 Intra",
111 4, 0, 2, 1, 0, 0, 0, 0, 0, 1, 0, 2, 3000, 3300, 4.500, 0.5, 6 },
112 { "Main 4:4:4 16 Intra",
113 4, 0, 2, 0, 0, 0, 0, 0, 0, 1, 0, 2, 4000, 4400, 6.000, 0.5, 6 },
114 { "Main 4:4:4 Still Picture",
115 4, 0, 2, 1, 1, 1, 0, 0, 0, 1, 1, 2, 2000, 2200, 3.000, 0.5, 6 },
116 { "Main 4:4:4 16 Still Picture",
117 4, 0, 2, 0, 0, 0, 0, 0, 0, 1, 1, 2, 4000, 4400, 6.000, 0.5, 6 },
118 { "High Throughput 4:4:4 16 Intra",
119 5, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 2, 4000, 4400, 6.000, 0.5, 6 },
120 };
121
122
123 94 const H265ProfileDescriptor *ff_h265_get_profile(const H265RawProfileTierLevel *ptl)
124 {
125 int i;
126
127
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 94 times.
94 if (ptl->general_profile_space)
128 return NULL;
129
130
1/2
✓ Branch 0 taken 726 times.
✗ Branch 1 not taken.
726 for (i = 0; i < FF_ARRAY_ELEMS(h265_profiles); i++) {
131 726 const H265ProfileDescriptor *profile = &h265_profiles[i];
132
133
1/2
✓ Branch 0 taken 726 times.
✗ Branch 1 not taken.
726 if (ptl->general_profile_idc &&
134
2/2
✓ Branch 0 taken 544 times.
✓ Branch 1 taken 182 times.
726 ptl->general_profile_idc != profile->profile_idc)
135 544 continue;
136
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 182 times.
182 if (!ptl->general_profile_compatibility_flag[profile->profile_idc])
137 continue;
138
139 #define check_flag(name) \
140 if (profile->name < 2) { \
141 if (profile->name != ptl->general_ ## name ## _constraint_flag) \
142 continue; \
143 }
144
3/4
✓ Branch 0 taken 24 times.
✓ Branch 1 taken 158 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 24 times.
182 check_flag(max_14bit);
145
4/4
✓ Branch 0 taken 104 times.
✓ Branch 1 taken 78 times.
✓ Branch 2 taken 24 times.
✓ Branch 3 taken 80 times.
182 check_flag(max_12bit);
146
4/4
✓ Branch 0 taken 80 times.
✓ Branch 1 taken 78 times.
✓ Branch 2 taken 40 times.
✓ Branch 3 taken 40 times.
158 check_flag(max_10bit);
147
3/4
✓ Branch 0 taken 40 times.
✓ Branch 1 taken 78 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 40 times.
118 check_flag(max_8bit);
148
4/4
✓ Branch 0 taken 40 times.
✓ Branch 1 taken 78 times.
✓ Branch 2 taken 4 times.
✓ Branch 3 taken 36 times.
118 check_flag(max_422chroma);
149
4/4
✓ Branch 0 taken 36 times.
✓ Branch 1 taken 78 times.
✓ Branch 2 taken 12 times.
✓ Branch 3 taken 24 times.
114 check_flag(max_420chroma);
150
4/4
✓ Branch 0 taken 24 times.
✓ Branch 1 taken 78 times.
✓ Branch 2 taken 4 times.
✓ Branch 3 taken 20 times.
102 check_flag(max_monochrome);
151
4/4
✓ Branch 0 taken 20 times.
✓ Branch 1 taken 78 times.
✓ Branch 2 taken 4 times.
✓ Branch 3 taken 16 times.
98 check_flag(intra);
152
3/4
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 78 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 16 times.
94 check_flag(one_picture_only);
153
3/4
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 82 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 12 times.
94 check_flag(lower_bit_rate);
154 #undef check_flag
155
156 94 return profile;
157 }
158
159 return NULL;
160 }
161
162 78 const H265LevelDescriptor *ff_h265_guess_level(const H265RawProfileTierLevel *ptl,
163 int64_t bitrate,
164 int width, int height,
165 int slice_segments,
166 int tile_rows, int tile_cols,
167 int max_dec_pic_buffering)
168 {
169 const H265ProfileDescriptor *profile;
170 int pic_size, tier_flag, lbr_flag, hbr_factor;
171 int i;
172
173
1/2
✓ Branch 0 taken 78 times.
✗ Branch 1 not taken.
78 if (ptl)
174 78 profile = ff_h265_get_profile(ptl);
175 else
176 profile = NULL;
177
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 78 times.
78 if (!profile) {
178 // Default to using multiplication factors for Main profile.
179 profile = &h265_profiles[4];
180 }
181
182 78 pic_size = width * height;
183
184
1/2
✓ Branch 0 taken 78 times.
✗ Branch 1 not taken.
78 if (ptl) {
185 78 tier_flag = ptl->general_tier_flag;
186 78 lbr_flag = ptl->general_lower_bit_rate_constraint_flag;
187 } else {
188 tier_flag = 0;
189 lbr_flag = profile->lower_bit_rate > 0;
190 }
191
3/4
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 70 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 8 times.
78 if (profile->profile_idc == 1 || profile->profile_idc == 2) {
192 70 hbr_factor = 1;
193
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 4 times.
8 } else if (profile->high_throughput) {
194
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (profile->intra)
195 hbr_factor = 24 - 12 * lbr_flag;
196 else
197 4 hbr_factor = 6;
198 } else {
199 4 hbr_factor = 2 - lbr_flag;
200 }
201
202
2/2
✓ Branch 0 taken 535 times.
✓ Branch 1 taken 6 times.
541 for (i = 0; i < FF_ARRAY_ELEMS(h265_levels); i++) {
203 535 const H265LevelDescriptor *level = &h265_levels[i];
204 int max_br, max_dpb_size;
205
206
4/4
✓ Branch 0 taken 23 times.
✓ Branch 1 taken 512 times.
✓ Branch 2 taken 15 times.
✓ Branch 3 taken 8 times.
535 if (tier_flag && !level->max_br_high)
207 15 continue;
208
209
2/2
✓ Branch 0 taken 130 times.
✓ Branch 1 taken 390 times.
520 if (pic_size > level->max_luma_ps)
210 130 continue;
211
2/2
✓ Branch 0 taken 49 times.
✓ Branch 1 taken 341 times.
390 if (width * width > 8 * level->max_luma_ps)
212 49 continue;
213
2/2
✓ Branch 0 taken 55 times.
✓ Branch 1 taken 286 times.
341 if (height * height > 8 * level->max_luma_ps)
214 55 continue;
215
216
2/2
✓ Branch 0 taken 49 times.
✓ Branch 1 taken 237 times.
286 if (slice_segments > level->max_slice_segments_per_picture)
217 49 continue;
218
2/2
✓ Branch 0 taken 49 times.
✓ Branch 1 taken 188 times.
237 if (tile_rows > level->max_tile_rows)
219 49 continue;
220
2/2
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 177 times.
188 if (tile_cols > level->max_tile_cols)
221 11 continue;
222
223
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 169 times.
177 if (tier_flag)
224 8 max_br = level->max_br_high;
225 else
226 169 max_br = level->max_br_main;
227
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 177 times.
177 if (!max_br)
228 continue;
229
2/2
✓ Branch 0 taken 91 times.
✓ Branch 1 taken 86 times.
177 if (bitrate > (int64_t)profile->cpb_nal_factor * hbr_factor * max_br)
230 91 continue;
231
232
2/2
✓ Branch 0 taken 51 times.
✓ Branch 1 taken 35 times.
86 if (pic_size <= (level->max_luma_ps >> 2))
233 51 max_dpb_size = FFMIN(4 * profile->max_dpb_pic_buf, 16);
234
2/2
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 28 times.
35 else if (pic_size <= (level->max_luma_ps >> 1))
235 7 max_dpb_size = FFMIN(2 * profile->max_dpb_pic_buf, 16);
236
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 20 times.
28 else if (pic_size <= (3 * level->max_luma_ps >> 2))
237
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
8 max_dpb_size = FFMIN(4 * profile->max_dpb_pic_buf / 3, 16);
238 else
239 20 max_dpb_size = profile->max_dpb_pic_buf;
240
2/2
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 72 times.
86 if (max_dec_pic_buffering > max_dpb_size)
241 14 continue;
242
243 72 return level;
244 }
245
246 6 return NULL;
247 }
248