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.

250 lines
9.7KB

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