You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

298 lines
10KB

  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. #include "libavutil/common.h"
  19. #include "libavcodec/h265_profile_level.h"
  20. static const struct {
  21. int width;
  22. int height;
  23. int level_idc;
  24. } test_sizes[] = {
  25. // First level usable at standard sizes, from H.265 table A.9.
  26. { 176, 144, 30 }, // QCIF
  27. { 352, 288, 60 }, // CIF
  28. { 640, 480, 90 }, // VGA
  29. { 720, 480, 90 }, // NTSC
  30. { 720, 576, 90 }, // PAL
  31. { 1024, 768, 93 }, // XGA
  32. { 1280, 720, 93 }, // 720p
  33. { 1280, 1024, 120 }, // SXGA
  34. { 1920, 1080, 120 }, // 1080p
  35. { 2048, 1080, 120 }, // 2Kx1080
  36. { 2048, 1536, 150 }, // 4XGA
  37. { 3840, 2160, 150 }, // 4K
  38. { 7680, 4320, 180 }, // 8K
  39. // Overly wide or tall sizes.
  40. { 1, 512, 30 },
  41. { 1, 1024, 63 },
  42. { 1, 2048, 90 },
  43. { 1, 4096, 120 },
  44. { 1, 8192, 150 },
  45. { 1, 16384, 180 },
  46. { 1, 32768, 0 },
  47. { 512, 1, 30 },
  48. { 1024, 1, 63 },
  49. { 2048, 1, 90 },
  50. { 4096, 1, 120 },
  51. { 8192, 1, 150 },
  52. { 16384, 1, 180 },
  53. { 32768, 1, 0 },
  54. { 2800, 256, 93 },
  55. { 2816, 128, 120 },
  56. { 256, 4208, 120 },
  57. { 128, 4224, 150 },
  58. { 8432, 256, 150 },
  59. { 8448, 128, 180 },
  60. { 256, 16880, 180 },
  61. { 128, 16896, 0 },
  62. };
  63. static const struct {
  64. int width;
  65. int height;
  66. int dpb_size;
  67. int level_idc;
  68. } test_dpb[] = {
  69. // First level usable for some DPB sizes.
  70. // L1: 176 * 144 = 25344 <= 36864 * 3/4 = 27648
  71. // L2: <= 122880 * 1/4 = 30720
  72. { 176, 144, 8, 30 },
  73. { 176, 144, 9, 60 },
  74. // L2: 352 * 288 = 101376 <= 122880
  75. // L2.1: <= 245760 * 1/2 = 122880
  76. // L3: <= 552960 * 1/4 = 138240
  77. { 352, 288, 6, 60 },
  78. { 352, 288, 7, 63 },
  79. { 352, 288, 13, 90 },
  80. // L3.1: 1280 * 720 = 921600 <= 983040
  81. // L4: <= 2228224 * 1/2 = 1114112
  82. // L5: <= 8912896 * 1/4 = 2228224
  83. { 1280, 720, 6, 93 },
  84. { 1280, 720, 12, 120 },
  85. { 1280, 720, 16, 150 },
  86. // L5: 3840 * 2160 = 8294400 <= 8912896
  87. // L6: <= 35651584 * 1/4 = 8912896
  88. { 3840, 2160, 6, 150 },
  89. { 3840, 2160, 7, 180 },
  90. { 3840, 2160, 16, 180 },
  91. };
  92. static const H265RawProfileTierLevel profile_main = {
  93. // CpbNalFactor = 1100
  94. .general_profile_space = 0,
  95. .general_profile_idc = 1,
  96. .general_tier_flag = 0,
  97. .general_profile_compatibility_flag[1] = 1,
  98. };
  99. static const H265RawProfileTierLevel profile_main_12 = {
  100. // CpbNalFactor = 1650
  101. .general_profile_space = 0,
  102. .general_profile_idc = 4,
  103. .general_tier_flag = 0,
  104. .general_profile_compatibility_flag[4] = 1,
  105. .general_max_12bit_constraint_flag = 1,
  106. .general_max_10bit_constraint_flag = 0,
  107. .general_max_8bit_constraint_flag = 0,
  108. .general_max_422chroma_constraint_flag = 1,
  109. .general_max_420chroma_constraint_flag = 1,
  110. .general_max_monochrome_constraint_flag = 0,
  111. .general_intra_constraint_flag = 0,
  112. .general_one_picture_only_constraint_flag = 0,
  113. .general_lower_bit_rate_constraint_flag = 1,
  114. };
  115. static const H265RawProfileTierLevel profile_main_422_12_intra = {
  116. // CpbNalFactor = 2200
  117. .general_profile_space = 0,
  118. .general_profile_idc = 4,
  119. .general_tier_flag = 0,
  120. .general_profile_compatibility_flag[4] = 1,
  121. .general_max_12bit_constraint_flag = 1,
  122. .general_max_10bit_constraint_flag = 0,
  123. .general_max_8bit_constraint_flag = 0,
  124. .general_max_422chroma_constraint_flag = 1,
  125. .general_max_420chroma_constraint_flag = 0,
  126. .general_max_monochrome_constraint_flag = 0,
  127. .general_intra_constraint_flag = 1,
  128. .general_one_picture_only_constraint_flag = 0,
  129. };
  130. static const H265RawProfileTierLevel profile_ht_444_14 = {
  131. // CpbNalFactor = 3850
  132. .general_profile_space = 0,
  133. .general_profile_idc = 5,
  134. .general_tier_flag = 0,
  135. .general_profile_compatibility_flag[5] = 1,
  136. .general_max_14bit_constraint_flag = 1,
  137. .general_max_12bit_constraint_flag = 0,
  138. .general_max_10bit_constraint_flag = 0,
  139. .general_max_8bit_constraint_flag = 0,
  140. .general_max_422chroma_constraint_flag = 0,
  141. .general_max_420chroma_constraint_flag = 0,
  142. .general_max_monochrome_constraint_flag = 0,
  143. .general_intra_constraint_flag = 0,
  144. .general_one_picture_only_constraint_flag = 0,
  145. .general_lower_bit_rate_constraint_flag = 1,
  146. };
  147. static const H265RawProfileTierLevel profile_main_high_tier = {
  148. // CpbNalFactor = 1100
  149. .general_profile_space = 0,
  150. .general_profile_idc = 1,
  151. .general_tier_flag = 1,
  152. .general_profile_compatibility_flag[1] = 1,
  153. };
  154. static const struct {
  155. int64_t bitrate;
  156. const H265RawProfileTierLevel *ptl;
  157. int level_idc;
  158. } test_bitrate[] = {
  159. // First level usable for some bitrates and profiles.
  160. // L2.1: 3000 * 1100 = 3300000
  161. // L3: 6000 * 1100 = 6600000
  162. { 4000000, &profile_main, 90 },
  163. // L2: 1500 * 1650 = 2475000
  164. // L2.1: 3000 * 1650 = 4950000
  165. { 4000000, &profile_main_12, 63 },
  166. // L1: 350 * 2200 * 2 = 1540000
  167. // L2: 1500 * 2200 * 2 = 6600000
  168. { 4000000, &profile_main_422_12_intra, 60 },
  169. // L5.1: 40000 * 1100 = 44000000
  170. // L5.2: 60000 * 1100 = 66000000
  171. { 50000000, &profile_main, 156 },
  172. // L5: 25000 * 1650 = 41250000
  173. // L5.1: 40000 * 1650 = 66000000
  174. { 50000000, &profile_main_12, 153 },
  175. // L3.1: 10000 * 2200 * 2 = 44000000
  176. // L4: 12000 * 2200 * 2 = 52800000
  177. { 50000000, &profile_main_422_12_intra, 120 },
  178. // L2: 1500 * 3850 * 6 = 34650000
  179. // L2.1: 3000 * 3850 * 6 = 69300000
  180. { 50000000, &profile_ht_444_14, 63 },
  181. // Level changes based on tier.
  182. { 1000, &profile_main, 30 },
  183. { 1000, &profile_main_high_tier, 120 },
  184. { 40000000, &profile_main, 153 },
  185. { 40000000, &profile_main_high_tier, 123 },
  186. { 200000000, &profile_main, 186 },
  187. { 200000000, &profile_main_high_tier, 156 },
  188. // Overflowing 32-bit integers.
  189. // L6: 60000 * 3850 * 6 = 1386000000
  190. // L6.1: 120000 * 3850 * 6 = 2772000000
  191. // L6.2: 240000 * 3850 * 6 = 5544000000
  192. { INT64_C(2700000000), &profile_ht_444_14, 183 },
  193. { INT64_C(4200000000), &profile_ht_444_14, 186 },
  194. { INT64_C(5600000000), &profile_ht_444_14, 0 },
  195. };
  196. static const struct {
  197. int slice_segments;
  198. int tile_rows;
  199. int tile_cols;
  200. int level_idc;
  201. } test_fragments[] = {
  202. // Slices.
  203. { 4, 1, 1, 30 },
  204. { 32, 1, 1, 93 },
  205. { 70, 1, 1, 120 },
  206. { 80, 1, 1, 150 },
  207. { 201, 1, 1, 180 },
  208. { 600, 1, 1, 180 },
  209. { 601, 1, 1, 0 },
  210. // Tiles.
  211. { 1, 2, 1, 90 },
  212. { 1, 1, 2, 90 },
  213. { 1, 3, 3, 93 },
  214. { 1, 4, 2, 120 },
  215. { 1, 2, 4, 120 },
  216. { 1, 11, 10, 150 },
  217. { 1, 10, 11, 180 },
  218. { 1, 22, 20, 180 },
  219. { 1, 20, 22, 0 },
  220. };
  221. int main(void)
  222. {
  223. const H265ProfileDescriptor *profile;
  224. const H265LevelDescriptor *level;
  225. int i;
  226. #define CHECK(expected, format, ...) do { \
  227. if (expected ? (!level || level->level_idc != expected) \
  228. : !!level) { \
  229. av_log(NULL, AV_LOG_ERROR, "Incorrect level for " \
  230. format ": expected %d, got %d.\n", __VA_ARGS__, \
  231. expected, level ? level->level_idc : -1); \
  232. return 1; \
  233. } \
  234. } while (0)
  235. for (i = 0; i < FF_ARRAY_ELEMS(test_sizes); i++) {
  236. level = ff_h265_guess_level(&profile_main, 0,
  237. test_sizes[i].width,
  238. test_sizes[i].height,
  239. 0, 0, 0, 0);
  240. CHECK(test_sizes[i].level_idc, "size %dx%d",
  241. test_sizes[i].width, test_sizes[i].height);
  242. }
  243. for (i = 0; i < FF_ARRAY_ELEMS(test_dpb); i++) {
  244. level = ff_h265_guess_level(&profile_main, 0,
  245. test_dpb[i].width,
  246. test_dpb[i].height,
  247. 0, 0, 0, test_dpb[i].dpb_size);
  248. CHECK(test_dpb[i].level_idc, "size %dx%d dpb %d",
  249. test_dpb[i].width, test_dpb[i].height,
  250. test_dpb[i].dpb_size);
  251. }
  252. for (i = 0; i < FF_ARRAY_ELEMS(test_bitrate); i++) {
  253. profile = ff_h265_get_profile(test_bitrate[i].ptl);
  254. level = ff_h265_guess_level(test_bitrate[i].ptl,
  255. test_bitrate[i].bitrate,
  256. 0, 0, 0, 0, 0, 0);
  257. CHECK(test_bitrate[i].level_idc, "bitrate %"PRId64" profile %s",
  258. test_bitrate[i].bitrate, profile->name);
  259. }
  260. for (i = 0; i < FF_ARRAY_ELEMS(test_fragments); i++) {
  261. level = ff_h265_guess_level(&profile_main, 0, 0, 0,
  262. test_fragments[i].slice_segments,
  263. test_fragments[i].tile_rows,
  264. test_fragments[i].tile_cols, 0);
  265. CHECK(test_fragments[i].level_idc, "%d slices %dx%d tiles",
  266. test_fragments[i].slice_segments,
  267. test_fragments[i].tile_cols, test_fragments[i].tile_rows);
  268. }
  269. return 0;
  270. }