FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/h264_levels.c
Date: 2021-09-26 18:22:30
Exec Total Coverage
Lines: 30 30 100.0%
Branches: 30 32 93.8%

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 <stddef.h>
20 #include "libavutil/common.h"
21 #include "h264_levels.h"
22
23 // H.264 table A-1.
24 static const H264LevelDescriptor h264_levels[] = {
25 // Name MaxMBPS MaxBR MinCR
26 // | level_idc | MaxFS | MaxCPB | MaxMvsPer2Mb
27 // | | cs3f | | MaxDpbMbs | | MaxVmvR | |
28 { "1", 10, 0, 1485, 99, 396, 64, 175, 64, 2, 0 },
29 { "1b", 11, 1, 1485, 99, 396, 128, 350, 64, 2, 0 },
30 { "1b", 9, 0, 1485, 99, 396, 128, 350, 64, 2, 0 },
31 { "1.1", 11, 0, 3000, 396, 900, 192, 500, 128, 2, 0 },
32 { "1.2", 12, 0, 6000, 396, 2376, 384, 1000, 128, 2, 0 },
33 { "1.3", 13, 0, 11880, 396, 2376, 768, 2000, 128, 2, 0 },
34 { "2", 20, 0, 11880, 396, 2376, 2000, 2000, 128, 2, 0 },
35 { "2.1", 21, 0, 19800, 792, 4752, 4000, 4000, 256, 2, 0 },
36 { "2.2", 22, 0, 20250, 1620, 8100, 4000, 4000, 256, 2, 0 },
37 { "3", 30, 0, 40500, 1620, 8100, 10000, 10000, 256, 2, 32 },
38 { "3.1", 31, 0, 108000, 3600, 18000, 14000, 14000, 512, 4, 16 },
39 { "3.2", 32, 0, 216000, 5120, 20480, 20000, 20000, 512, 4, 16 },
40 { "4", 40, 0, 245760, 8192, 32768, 20000, 25000, 512, 4, 16 },
41 { "4.1", 41, 0, 245760, 8192, 32768, 50000, 62500, 512, 2, 16 },
42 { "4.2", 42, 0, 522240, 8704, 34816, 50000, 62500, 512, 2, 16 },
43 { "5", 50, 0, 589824, 22080, 110400, 135000, 135000, 512, 2, 16 },
44 { "5.1", 51, 0, 983040, 36864, 184320, 240000, 240000, 512, 2, 16 },
45 { "5.2", 52, 0, 2073600, 36864, 184320, 240000, 240000, 512, 2, 16 },
46 { "6", 60, 0, 4177920, 139264, 696320, 240000, 240000, 8192, 2, 16 },
47 { "6.1", 61, 0, 8355840, 139264, 696320, 480000, 480000, 8192, 2, 16 },
48 { "6.2", 62, 0, 16711680, 139264, 696320, 800000, 800000, 8192, 2, 16 },
49 };
50
51 // H.264 table A-2 plus values from A-1.
52 static const struct {
53 int profile_idc;
54 int cpb_br_vcl_factor;
55 int cpb_br_nal_factor;
56 } h264_br_factors[] = {
57 { 66, 1000, 1200 },
58 { 77, 1000, 1200 },
59 { 88, 1000, 1200 },
60 { 100, 1250, 1500 },
61 { 110, 3000, 3600 },
62 { 122, 4000, 4800 },
63 { 244, 4000, 4800 },
64 { 44, 4000, 4800 },
65 };
66
67 // We are only ever interested in the NAL bitrate factor.
68 1074 static int h264_get_br_factor(int profile_idc)
69 {
70 int i;
71
2/2
✓ Branch 0 taken 7523 times.
✓ Branch 1 taken 821 times.
8344 for (i = 0; i < FF_ARRAY_ELEMS(h264_br_factors); i++) {
72
2/2
✓ Branch 0 taken 253 times.
✓ Branch 1 taken 7270 times.
7523 if (h264_br_factors[i].profile_idc == profile_idc)
73 253 return h264_br_factors[i].cpb_br_nal_factor;
74 }
75 // Default to the non-high profile value if not specified.
76 821 return 1200;
77 }
78
79 103 const H264LevelDescriptor *ff_h264_guess_level(int profile_idc,
80 int64_t bitrate,
81 int framerate,
82 int width, int height,
83 int max_dec_frame_buffering)
84 {
85 103 int width_mbs = (width + 15) / 16;
86 103 int height_mbs = (height + 15) / 16;
87
5/6
✓ Branch 0 taken 100 times.
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 97 times.
✓ Branch 3 taken 3 times.
✓ Branch 4 taken 97 times.
✗ Branch 5 not taken.
103 int no_cs3f = !(profile_idc == 66 ||
88 profile_idc == 77 ||
89 profile_idc == 88);
90 int i;
91
92
2/2
✓ Branch 0 taken 1165 times.
✓ Branch 1 taken 3 times.
1168 for (i = 0; i < FF_ARRAY_ELEMS(h264_levels); i++) {
93 1165 const H264LevelDescriptor *level = &h264_levels[i];
94
95
4/4
✓ Branch 0 taken 96 times.
✓ Branch 1 taken 1069 times.
✓ Branch 2 taken 91 times.
✓ Branch 3 taken 5 times.
1165 if (level->constraint_set3_flag && no_cs3f)
96 91 continue;
97
98
2/2
✓ Branch 1 taken 226 times.
✓ Branch 2 taken 848 times.
1074 if (bitrate > (int64_t)level->max_br * h264_get_br_factor(profile_idc))
99 226 continue;
100
101
2/2
✓ Branch 0 taken 616 times.
✓ Branch 1 taken 232 times.
848 if (width_mbs * height_mbs > level->max_fs)
102 616 continue;
103
2/2
✓ Branch 0 taken 46 times.
✓ Branch 1 taken 186 times.
232 if (width_mbs * width_mbs > 8 * level->max_fs)
104 46 continue;
105
2/2
✓ Branch 0 taken 23 times.
✓ Branch 1 taken 163 times.
186 if (height_mbs * height_mbs > 8 * level->max_fs)
106 23 continue;
107
108
3/4
✓ Branch 0 taken 150 times.
✓ Branch 1 taken 13 times.
✓ Branch 2 taken 150 times.
✗ Branch 3 not taken.
163 if (width_mbs && height_mbs) {
109 150 int max_dpb_frames =
110 150 FFMIN(level->max_dpb_mbs / (width_mbs * height_mbs), 16);
111
2/2
✓ Branch 0 taken 30 times.
✓ Branch 1 taken 120 times.
150 if (max_dec_frame_buffering > max_dpb_frames)
112 30 continue;
113
114
2/2
✓ Branch 0 taken 33 times.
✓ Branch 1 taken 87 times.
120 if (framerate > (level->max_mbps / (width_mbs * height_mbs)))
115 33 continue;
116 }
117
118 100 return level;
119 }
120
121 // No usable levels found - frame is too big or bitrate is too high.
122 3 return NULL;
123 }
124