History

lavc/h264: Add common code for level handling

Including a unit test.

Mark Thompson committed 8 years ago

Commit cc549141 parent 40724026 files · +374 −1Browse files

Unified · Split

6 files
  1. libavcodec/Makefile +2 −1
  2. libavcodec/h264_levels.c +130 −0
  3. libavcodec/h264_levels.h +53 −0
  4. libavcodec/tests/.gitignore +1 −0
  5. libavcodec/tests/h264_levels.c +183 −0
  6. tests/fate/libavcodec.mak +5 −0
libavcodec/Makefile+2−1
@@ -1059,7 +1059,7 @@ OBJS-$(CONFIG_EAC3_CORE_BSF) += eac3_core_bsf.o · 1058 unmodified lines
10591059 OBJS-$(CONFIG_EXTRACT_EXTRADATA_BSF) += extract_extradata_bsf.o \
10601060 av1_parse.o h2645_parse.o
10611061 OBJS-$(CONFIG_FILTER_UNITS_BSF) += filter_units_bsf.o
1062−OBJS-$(CONFIG_H264_METADATA_BSF) += h264_metadata_bsf.o
1062+OBJS-$(CONFIG_H264_METADATA_BSF) += h264_metadata_bsf.o h264_levels.o
10631063 OBJS-$(CONFIG_H264_MP4TOANNEXB_BSF) += h264_mp4toannexb_bsf.o
10641064 OBJS-$(CONFIG_H264_REDUNDANT_PPS_BSF) += h264_redundant_pps_bsf.o
10651065 OBJS-$(CONFIG_HAPQA_EXTRACT_BSF) += hapqa_extract_bsf.o hap.o
@@ -1140,6 +1140,7 @@ TESTPROGS-$(CONFIG_IDCTDSP) += dct · 74 unmodified lines
11401140 TESTPROGS-$(CONFIG_IIRFILTER) += iirfilter
11411141 TESTPROGS-$(HAVE_MMX) += motion
11421142 TESTPROGS-$(CONFIG_MPEGVIDEO) += mpeg12framerate
1143+TESTPROGS-$(CONFIG_H264_METADATA_BSF) += h264_levels
11431144 TESTPROGS-$(CONFIG_RANGECODER) += rangecoder
11441145 TESTPROGS-$(CONFIG_SNOW_ENCODER) += snowenc
11451146
libavcodec/h264_levels.c+130−0
@@ -0,0 +1,130 @@
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 "avcodec.h"
20+#include "h264_levels.h"
21+
22+// H.264 table A-1.
23+static const H264LevelDescriptor h264_levels[] = {
24+ // Name MaxMBPS MaxBR MinCR
25+ // | level_idc | MaxFS | MaxCPB | MaxMvsPer2Mb
26+ // | | cs3f | | MaxDpbMbs | | MaxVmvR | |
27+ { "1", 10, 0, 1485, 99, 396, 64, 175, 64, 2, 0 },
28+ { "1b", 10, 1, 1485, 99, 396, 128, 350, 64, 2, 0 },
29+ { "1b", 9, 0, 1485, 99, 396, 128, 350, 64, 2, 0 },
30+ { "1.1", 11, 0, 3000, 396, 900, 192, 500, 128, 2, 0 },
31+ { "1.2", 12, 0, 6000, 396, 2376, 384, 1000, 128, 2, 0 },
32+ { "1.3", 13, 0, 11880, 396, 2376, 768, 2000, 128, 2, 0 },
33+ { "2", 20, 0, 11880, 396, 2376, 2000, 2000, 128, 2, 0 },
34+ { "2.1", 21, 0, 19800, 792, 4752, 4000, 4000, 256, 2, 0 },
35+ { "2.2", 22, 0, 20250, 1620, 8100, 4000, 4000, 256, 2, 0 },
36+ { "3", 30, 0, 40500, 1620, 8100, 10000, 10000, 256, 2, 32 },
37+ { "3.1", 31, 0, 108000, 3600, 18000, 14000, 14000, 512, 4, 16 },
38+ { "3.2", 32, 0, 216000, 5120, 20480, 20000, 20000, 512, 4, 16 },
39+ { "4", 40, 0, 245760, 8192, 32768, 20000, 25000, 512, 4, 16 },
40+ { "4.1", 41, 0, 245760, 8192, 32768, 50000, 62500, 512, 2, 16 },
41+ { "4.2", 42, 0, 522240, 8704, 34816, 50000, 62500, 512, 2, 16 },
42+ { "5", 50, 0, 589824, 22080, 110400, 135000, 135000, 512, 2, 16 },
43+ { "5.1", 51, 0, 983040, 36864, 184320, 240000, 240000, 512, 2, 16 },
44+ { "5.2", 52, 0, 2073600, 36864, 184320, 240000, 240000, 512, 2, 16 },
45+ { "6", 60, 0, 4177920, 139264, 696320, 240000, 240000, 8192, 2, 16 },
46+ { "6.1", 61, 0, 8355840, 139264, 696320, 480000, 480000, 8192, 2, 16 },
47+ { "6.2", 62, 0, 16711680, 139264, 696320, 800000, 800000, 8192, 2, 16 },
48+};
49+
50+// H.264 table A-2 plus values from A-1.
51+static const struct {
52+ int profile_idc;
53+ int cpb_br_vcl_factor;
54+ int cpb_br_nal_factor;
55+} h264_br_factors[] = {
56+ { 66, 1000, 1200 },
57+ { 77, 1000, 1200 },
58+ { 88, 1000, 1200 },
59+ { 100, 1250, 1500 },
60+ { 110, 3000, 3600 },
61+ { 122, 4000, 4800 },
62+ { 244, 4000, 4800 },
63+ { 44, 4000, 4800 },
64+};
65+
66+// We are only ever interested in the NAL bitrate factor.
67+static int h264_get_br_factor(int profile_idc)
68+{
69+ int i;
70+ for (i = 0; i < FF_ARRAY_ELEMS(h264_br_factors); i++) {
71+ if (h264_br_factors[i].profile_idc == profile_idc)
72+ return h264_br_factors[i].cpb_br_nal_factor;
73+ }
74+ // Default to the non-high profile value if not specified.
75+ return 1200;
76+}
77+
78+const H264LevelDescriptor *ff_h264_get_level(int level_idc,
79+ int constraint_set3_flag)
80+{
81+ int i;
82+ for (i = 0; i < FF_ARRAY_ELEMS(h264_levels); i++) {
83+ if (h264_levels[i].level_idc == level_idc &&
84+ h264_levels[i].constraint_set3_flag == constraint_set3_flag)
85+ return &h264_levels[i];
86+ }
87+ return NULL;
88+}
89+
90+const H264LevelDescriptor *ff_h264_guess_level(int profile_idc,
91+ int64_t bitrate,
92+ int width, int height,
93+ int max_dec_frame_buffering)
94+{
95+ int width_mbs = (width + 15) / 16;
96+ int height_mbs = (height + 15) / 16;
97+ int no_cs3f = !(profile_idc == 66 ||
98+ profile_idc == 77 ||
99+ profile_idc == 88);
100+ int i;
101+
102+ for (i = 0; i < FF_ARRAY_ELEMS(h264_levels); i++) {
103+ const H264LevelDescriptor *level = &h264_levels[i];
104+
105+ if (level->constraint_set3_flag && no_cs3f)
106+ continue;
107+
108+ if (bitrate > level->max_br * h264_get_br_factor(profile_idc))
109+ continue;
110+
111+ if (width_mbs * height_mbs > level->max_fs)
112+ continue;
113+ if (width_mbs * width_mbs > 8 * level->max_fs)
114+ continue;
115+ if (height_mbs * height_mbs > 8 * level->max_fs)
116+ continue;
117+
118+ if (width_mbs && height_mbs) {
119+ int max_dpb_frames =
120+ FFMIN(level->max_dpb_mbs / (width_mbs * height_mbs), 16);
121+ if (max_dec_frame_buffering > max_dpb_frames)
122+ continue;
123+ }
124+
125+ return level;
126+ }
127+
128+ // No usable levels found - frame is too big or bitrate is too high.
129+ return NULL;
130+}
libavcodec/h264_levels.h+53−0
@@ -0,0 +1,53 @@
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+#ifndef AVCODEC_H264_LEVELS_H
20+#define AVCODEC_H264_LEVELS_H
21+
22+
23+#include <stdint.h>
24+
25+typedef struct H264LevelDescriptor {
26+ const char *name;
27+ uint8_t level_idc;
28+ uint8_t constraint_set3_flag;
29+ uint32_t max_mbps;
30+ uint32_t max_fs;
31+ uint32_t max_dpb_mbs;
32+ uint32_t max_br;
33+ uint32_t max_cpb;
34+ uint16_t max_v_mv_r;
35+ uint8_t min_cr;
36+ uint8_t max_mvs_per_2mb;
37+} H264LevelDescriptor;
38+
39+const H264LevelDescriptor *ff_h264_get_level(int level_idc,
40+ int constraint_set3_flag);
41+
42+/**
43+ * Guess the level of a stream from some parameters.
44+ *
45+ * Unknown parameters may be zero, in which case they are ignored.
46+ */
47+const H264LevelDescriptor *ff_h264_guess_level(int profile_idc,
48+ int64_t bitrate,
49+ int width, int height,
50+ int max_dec_frame_buffering);
51+
52+
53+#endif /* AVCODEC_H264_LEVELS_H */
libavcodec/tests/.gitignore+1−0
@@ -7,6 +7,7 @@ · 6 unmodified lines
77 /fft-fixed
88 /fft-fixed32
99 /golomb
10+/h264_levels
1011 /htmlsubtitles
1112 /iirfilter
1213 /imgconvert
libavcodec/tests/h264_levels.c+183−0
@@ -0,0 +1,183 @@
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/common.h"
20+#include "libavcodec/h264_levels.h"
21+
22+static const struct {
23+ int width;
24+ int height;
25+ int level_idc;
26+} test_sizes[] = {
27+ // First level usable at some standard sizes.
28+ // (From H.264 table A-6.)
29+ { 176, 144, 10 }, // QCIF
30+ { 352, 288, 11 }, // CIF
31+ { 640, 480, 22 }, // VGA
32+ { 720, 480, 22 }, // NTSC
33+ { 720, 576, 22 }, // PAL
34+ { 800, 600, 31 }, // SVGA
35+ { 1280, 720, 31 }, // 720p
36+ { 1280, 1024, 32 }, // SXGA
37+ { 1920, 1080, 40 }, // 1080p
38+ { 2048, 1080, 42 }, // 2Kx1080
39+ { 2048, 1536, 50 }, // 4XGA
40+ { 3840, 2160, 51 }, // 4K
41+ { 7680, 4320, 60 }, // 8K
42+
43+ // Overly wide or tall sizes.
44+ { 1, 256, 10 },
45+ { 1, 512, 11 },
46+ { 1, 1024, 21 },
47+ { 1, 1808, 22 },
48+ { 1, 1824, 31 },
49+ { 256, 1, 10 },
50+ { 512, 1, 11 },
51+ { 1024, 1, 21 },
52+ { 1808, 1, 22 },
53+ { 1824, 1, 31 },
54+ { 512, 4096, 40 },
55+ { 256, 4112, 42 },
56+ { 8688, 1024, 51 },
57+ { 8704, 512, 60 },
58+ { 16880, 1, 60 },
59+ { 16896, 1, 0 },
60+};
61+
62+static const struct {
63+ int width;
64+ int height;
65+ int dpb_size;
66+ int level_idc;
67+} test_dpb[] = {
68+ // First level usable for some DPB sizes.
69+ // (From H.264 table A-7.)
70+ { 176, 144, 4, 10 },
71+ { 176, 144, 8, 11 },
72+ { 176, 144, 16, 12 },
73+ { 1280, 720, 1, 31 },
74+ { 1280, 720, 5, 31 },
75+ { 1280, 720, 9, 40 },
76+ { 1280, 720, 10, 50 },
77+ { 1920, 1080, 1, 40 },
78+ { 1920, 1080, 5, 50 },
79+ { 1920, 1080, 13, 50 },
80+ { 1920, 1080, 14, 51 },
81+ { 3840, 2160, 5, 51 },
82+ { 3840, 2160, 6, 60 },
83+ { 3840, 2160, 16, 60 },
84+ { 7680, 4320, 5, 60 },
85+ { 7680, 4320, 6, 0 },
86+};
87+
88+static const struct {
89+ int64_t bitrate;
90+ int profile_idc;
91+ int level_idc;
92+} test_bitrate[] = {
93+ // Values where profile affects level at a given bitrate.
94+ { 2500000, 77, 21 },
95+ { 2500000, 100, 20 },
96+ { 2500000, 244, 13 },
97+ { 100000000, 77, 50 },
98+ { 100000000, 100, 50 },
99+ { 100000000, 244, 41 },
100+ { 999999999, 77, 0 },
101+ { 999999999, 100, 62 },
102+ // Check level 1b.
103+ { 32 * 1200, 66, 10 },
104+ { 32 * 1500, 100, 10 },
105+ { 96 * 1200, 66, 10 },
106+ { 96 * 1500, 100, 9 },
107+ { 144 * 1200, 66, 11 },
108+ { 144 * 1500, 100, 11 },
109+};
110+
111+static const struct {
112+ const char *name;
113+ int profile_idc;
114+ int64_t bitrate;
115+ int width;
116+ int height;
117+ int dpb_frames;
118+ int level_idc;
119+} test_all[] = {
120+ { "Bluray 1080p 40Mb/s", 100, 40000000, 1920, 1080, 4, 41 },
121+ { "Bluray 1080p 24Mb/s", 100, 24000000, 1920, 1080, 4, 40 },
122+ { "Bluray 720p 40Mb/s", 100, 40000000, 1280, 720, 6, 41 },
123+ { "Bluray 720p 24Mb/s", 100, 24000000, 1280, 720, 6, 40 },
124+ { "Bluray PAL 40Mb/s", 100, 40000000, 720, 576, 6, 41 },
125+ { "Bluray PAL 24Mb/s", 100, 24000000, 720, 576, 6, 32 },
126+ { "Bluray PAL 16Mb/s", 100, 16800000, 720, 576, 6, 31 },
127+ { "Bluray PAL 12Mb/s", 100, 12000000, 720, 576, 5, 30 },
128+ { "Bluray NTSC 40Mb/s", 100, 40000000, 720, 480, 6, 41 },
129+ { "Bluray NTSC 24Mb/s", 100, 24000000, 720, 480, 6, 32 },
130+ { "Bluray NTSC 16Mb/s", 100, 16800000, 720, 480, 6, 31 },
131+ { "Bluray NTSC 12Mb/s", 100, 12000000, 720, 480, 6, 30 },
132+};
133+
134+int main(void)
135+{
136+ const H264LevelDescriptor *level;
137+ int i;
138+
139+#define CHECK(expected, format, ...) do { \
140+ if (expected ? (!level || level->level_idc != expected) \
141+ : !!level) { \
142+ av_log(NULL, AV_LOG_ERROR, "Incorrect level for " \
143+ format ": expected %d, got %d.\n", __VA_ARGS__, \
144+ expected, level ? level->level_idc : -1); \
145+ return 1; \
146+ } \
147+ } while (0)
148+
149+ for (i = 0; i < FF_ARRAY_ELEMS(test_sizes); i++) {
150+ level = ff_h264_guess_level(0, 0, test_sizes[i].width,
151+ test_sizes[i].height, 0);
152+ CHECK(test_sizes[i].level_idc, "size %dx%d",
153+ test_sizes[i].width, test_sizes[i].height);
154+ }
155+
156+ for (i = 0; i < FF_ARRAY_ELEMS(test_dpb); i++) {
157+ level = ff_h264_guess_level(0, 0, test_dpb[i].width,
158+ test_dpb[i].height,
159+ test_dpb[i].dpb_size);
160+ CHECK(test_dpb[i].level_idc, "size %dx%d dpb %d",
161+ test_dpb[i].width, test_dpb[i].height,
162+ test_dpb[i].dpb_size);
163+ }
164+
165+ for (i = 0; i < FF_ARRAY_ELEMS(test_bitrate); i++) {
166+ level = ff_h264_guess_level(test_bitrate[i].profile_idc,
167+ test_bitrate[i].bitrate,
168+ 0, 0, 0);
169+ CHECK(test_bitrate[i].level_idc, "bitrate %"PRId64" profile %d",
170+ test_bitrate[i].bitrate, test_bitrate[i].profile_idc);
171+ }
172+
173+ for (i = 0; i < FF_ARRAY_ELEMS(test_all); i++) {
174+ level = ff_h264_guess_level(test_all[i].profile_idc,
175+ test_all[i].bitrate,
176+ test_all[i].width,
177+ test_all[i].height,
178+ test_all[i].dpb_frames);
179+ CHECK(test_all[i].level_idc, "%s", test_all[i].name);
180+ }
181+
182+ return 0;
183+}
tests/fate/libavcodec.mak+5−0
@@ -46,6 +46,11 @@ fate-dct8x8: libavcodec/tests/dct$(EXESUF) · 45 unmodified lines
4646 fate-dct8x8: CMD = run libavcodec/tests/dct
4747 fate-dct8x8: CMP = null
4848
49+FATE_LIBAVCODEC-$(CONFIG_H264_VAAPI_ENCODER) += fate-h264-levels
50+fate-h264-levels: libavcodec/tests/h264_levels$(EXESUF)
51+fate-h264-levels: CMD = run libavcodec/tests/h264_levels
52+fate-h264-levels: REF = /dev/null
53+
4954 FATE_LIBAVCODEC-$(CONFIG_IIRFILTER) += fate-iirfilter
5055 fate-iirfilter: libavcodec/tests/iirfilter$(EXESUF)
5156 fate-iirfilter: CMD = run libavcodec/tests/iirfilter

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t