hw_base_encode_h265.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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 "hw_base_encode_h265.h"
  19. #include "h2645data.h"
  20. #include "h265_profile_level.h"
  21. #include "libavutil/pixdesc.h"
  22. int ff_hw_base_encode_init_params_h265(FFHWBaseEncodeContext *base_ctx,
  23. AVCodecContext *avctx,
  24. FFHWBaseEncodeH265 *common,
  25. FFHWBaseEncodeH265Opts *opts)
  26. {
  27. H265RawVPS *vps = &common->raw_vps;
  28. H265RawSPS *sps = &common->raw_sps;
  29. H265RawPPS *pps = &common->raw_pps;
  30. H265RawProfileTierLevel *ptl = &vps->profile_tier_level;
  31. H265RawVUI *vui = &sps->vui;
  32. const AVPixFmtDescriptor *desc;
  33. int chroma_format, bit_depth;
  34. int i;
  35. memset(vps, 0, sizeof(*vps));
  36. memset(sps, 0, sizeof(*sps));
  37. memset(pps, 0, sizeof(*pps));
  38. desc = av_pix_fmt_desc_get(base_ctx->input_frames->sw_format);
  39. av_assert0(desc);
  40. if (desc->nb_components == 1) {
  41. chroma_format = 0;
  42. } else {
  43. if (desc->log2_chroma_w == 1 && desc->log2_chroma_h == 1) {
  44. chroma_format = 1;
  45. } else if (desc->log2_chroma_w == 1 && desc->log2_chroma_h == 0) {
  46. chroma_format = 2;
  47. } else if (desc->log2_chroma_w == 0 && desc->log2_chroma_h == 0) {
  48. chroma_format = 3;
  49. } else {
  50. av_log(avctx, AV_LOG_ERROR, "Chroma format of input pixel format "
  51. "%s is not supported.\n", desc->name);
  52. return AVERROR(EINVAL);
  53. }
  54. }
  55. bit_depth = desc->comp[0].depth;
  56. // VPS
  57. vps->nal_unit_header = (H265RawNALUnitHeader) {
  58. .nal_unit_type = HEVC_NAL_VPS,
  59. .nuh_layer_id = 0,
  60. .nuh_temporal_id_plus1 = 1,
  61. };
  62. vps->vps_video_parameter_set_id = 0;
  63. vps->vps_base_layer_internal_flag = 1;
  64. vps->vps_base_layer_available_flag = 1;
  65. vps->vps_max_layers_minus1 = 0;
  66. vps->vps_max_sub_layers_minus1 = 0;
  67. vps->vps_temporal_id_nesting_flag = 1;
  68. ptl->general_profile_space = 0;
  69. ptl->general_profile_idc = avctx->profile;
  70. ptl->general_tier_flag = opts->tier;
  71. ptl->general_profile_compatibility_flag[ptl->general_profile_idc] = 1;
  72. if (ptl->general_profile_compatibility_flag[1])
  73. ptl->general_profile_compatibility_flag[2] = 1;
  74. if (ptl->general_profile_compatibility_flag[3]) {
  75. ptl->general_profile_compatibility_flag[1] = 1;
  76. ptl->general_profile_compatibility_flag[2] = 1;
  77. }
  78. ptl->general_progressive_source_flag = 1;
  79. ptl->general_interlaced_source_flag = 0;
  80. ptl->general_non_packed_constraint_flag = 1;
  81. ptl->general_frame_only_constraint_flag = 1;
  82. ptl->general_max_14bit_constraint_flag = bit_depth <= 14;
  83. ptl->general_max_12bit_constraint_flag = bit_depth <= 12;
  84. ptl->general_max_10bit_constraint_flag = bit_depth <= 10;
  85. ptl->general_max_8bit_constraint_flag = bit_depth == 8;
  86. ptl->general_max_422chroma_constraint_flag = chroma_format <= 2;
  87. ptl->general_max_420chroma_constraint_flag = chroma_format <= 1;
  88. ptl->general_max_monochrome_constraint_flag = chroma_format == 0;
  89. ptl->general_intra_constraint_flag = base_ctx->gop_size == 1;
  90. ptl->general_one_picture_only_constraint_flag = 0;
  91. ptl->general_lower_bit_rate_constraint_flag = 1;
  92. if (avctx->level != AV_LEVEL_UNKNOWN) {
  93. ptl->general_level_idc = avctx->level;
  94. } else {
  95. const H265LevelDescriptor *level;
  96. level = ff_h265_guess_level(ptl, avctx->bit_rate,
  97. base_ctx->surface_width, base_ctx->surface_height,
  98. opts->nb_slices, opts->tile_rows, opts->tile_cols,
  99. (base_ctx->b_per_p > 0) + 1);
  100. if (level) {
  101. av_log(avctx, AV_LOG_VERBOSE, "Using level %s.\n", level->name);
  102. ptl->general_level_idc = level->level_idc;
  103. } else {
  104. av_log(avctx, AV_LOG_VERBOSE, "Stream will not conform to "
  105. "any normal level; using level 8.5.\n");
  106. ptl->general_level_idc = 255;
  107. // The tier flag must be set in level 8.5.
  108. ptl->general_tier_flag = 1;
  109. }
  110. }
  111. vps->vps_sub_layer_ordering_info_present_flag = 0;
  112. vps->vps_max_dec_pic_buffering_minus1[0] = base_ctx->max_b_depth + 1;
  113. vps->vps_max_num_reorder_pics[0] = base_ctx->max_b_depth;
  114. vps->vps_max_latency_increase_plus1[0] = 0;
  115. vps->vps_max_layer_id = 0;
  116. vps->vps_num_layer_sets_minus1 = 0;
  117. vps->layer_id_included_flag[0][0] = 1;
  118. vps->vps_timing_info_present_flag = 1;
  119. if (avctx->framerate.num > 0 && avctx->framerate.den > 0) {
  120. vps->vps_num_units_in_tick = avctx->framerate.den;
  121. vps->vps_time_scale = avctx->framerate.num;
  122. vps->vps_poc_proportional_to_timing_flag = 1;
  123. vps->vps_num_ticks_poc_diff_one_minus1 = 0;
  124. } else {
  125. vps->vps_num_units_in_tick = avctx->time_base.num;
  126. vps->vps_time_scale = avctx->time_base.den;
  127. vps->vps_poc_proportional_to_timing_flag = 0;
  128. }
  129. vps->vps_num_hrd_parameters = 0;
  130. // SPS
  131. sps->nal_unit_header = (H265RawNALUnitHeader) {
  132. .nal_unit_type = HEVC_NAL_SPS,
  133. .nuh_layer_id = 0,
  134. .nuh_temporal_id_plus1 = 1,
  135. };
  136. sps->sps_video_parameter_set_id = vps->vps_video_parameter_set_id;
  137. sps->sps_max_sub_layers_minus1 = vps->vps_max_sub_layers_minus1;
  138. sps->sps_temporal_id_nesting_flag = vps->vps_temporal_id_nesting_flag;
  139. sps->profile_tier_level = vps->profile_tier_level;
  140. sps->sps_seq_parameter_set_id = 0;
  141. sps->chroma_format_idc = chroma_format;
  142. sps->separate_colour_plane_flag = 0;
  143. sps->pic_width_in_luma_samples = base_ctx->surface_width;
  144. sps->pic_height_in_luma_samples = base_ctx->surface_height;
  145. if (avctx->width != base_ctx->surface_width ||
  146. avctx->height != base_ctx->surface_height) {
  147. sps->conformance_window_flag = 1;
  148. sps->conf_win_left_offset = 0;
  149. sps->conf_win_right_offset =
  150. (base_ctx->surface_width - avctx->width) >> desc->log2_chroma_w;
  151. sps->conf_win_top_offset = 0;
  152. sps->conf_win_bottom_offset =
  153. (base_ctx->surface_height - avctx->height) >> desc->log2_chroma_h;
  154. } else {
  155. sps->conformance_window_flag = 0;
  156. }
  157. sps->bit_depth_luma_minus8 = bit_depth - 8;
  158. sps->bit_depth_chroma_minus8 = bit_depth - 8;
  159. sps->log2_max_pic_order_cnt_lsb_minus4 = 8;
  160. sps->sps_sub_layer_ordering_info_present_flag =
  161. vps->vps_sub_layer_ordering_info_present_flag;
  162. for (i = 0; i <= sps->sps_max_sub_layers_minus1; i++) {
  163. sps->sps_max_dec_pic_buffering_minus1[i] =
  164. vps->vps_max_dec_pic_buffering_minus1[i];
  165. sps->sps_max_num_reorder_pics[i] =
  166. vps->vps_max_num_reorder_pics[i];
  167. sps->sps_max_latency_increase_plus1[i] =
  168. vps->vps_max_latency_increase_plus1[i];
  169. }
  170. // These values come from the capabilities of the first encoder
  171. // implementation in the i965 driver on Intel Skylake. They may
  172. // fail badly with other platforms or drivers.
  173. // CTB size from 8x8 to 32x32.
  174. sps->log2_min_luma_coding_block_size_minus3 = 0;
  175. sps->log2_diff_max_min_luma_coding_block_size = 2;
  176. // Transform size from 4x4 to 32x32.
  177. sps->log2_min_luma_transform_block_size_minus2 = 0;
  178. sps->log2_diff_max_min_luma_transform_block_size = 3;
  179. // Full transform hierarchy allowed (2-5).
  180. sps->max_transform_hierarchy_depth_inter = 3;
  181. sps->max_transform_hierarchy_depth_intra = 3;
  182. // AMP works.
  183. sps->amp_enabled_flag = 1;
  184. // SAO and temporal MVP do not work.
  185. sps->sample_adaptive_offset_enabled_flag = 0;
  186. sps->sps_temporal_mvp_enabled_flag = 0;
  187. sps->pcm_enabled_flag = 0;
  188. // STRPSs should ideally be here rather than defined individually in
  189. // each slice, but the structure isn't completely fixed so for now
  190. // don't bother.
  191. sps->num_short_term_ref_pic_sets = 0;
  192. sps->long_term_ref_pics_present_flag = 0;
  193. sps->vui_parameters_present_flag = 1;
  194. if (avctx->sample_aspect_ratio.num != 0 &&
  195. avctx->sample_aspect_ratio.den != 0) {
  196. int num, den, i;
  197. av_reduce(&num, &den, avctx->sample_aspect_ratio.num,
  198. avctx->sample_aspect_ratio.den, 65535);
  199. for (i = 0; i < FF_ARRAY_ELEMS(ff_h2645_pixel_aspect); i++) {
  200. if (num == ff_h2645_pixel_aspect[i].num &&
  201. den == ff_h2645_pixel_aspect[i].den) {
  202. vui->aspect_ratio_idc = i;
  203. break;
  204. }
  205. }
  206. if (i >= FF_ARRAY_ELEMS(ff_h2645_pixel_aspect)) {
  207. vui->aspect_ratio_idc = 255;
  208. vui->sar_width = num;
  209. vui->sar_height = den;
  210. }
  211. vui->aspect_ratio_info_present_flag = 1;
  212. }
  213. // Unspecified video format, from table E-2.
  214. vui->video_format = 5;
  215. vui->video_full_range_flag =
  216. avctx->color_range == AVCOL_RANGE_JPEG;
  217. vui->colour_primaries = avctx->color_primaries;
  218. vui->transfer_characteristics = avctx->color_trc;
  219. vui->matrix_coefficients = avctx->colorspace;
  220. if (avctx->color_primaries != AVCOL_PRI_UNSPECIFIED ||
  221. avctx->color_trc != AVCOL_TRC_UNSPECIFIED ||
  222. avctx->colorspace != AVCOL_SPC_UNSPECIFIED)
  223. vui->colour_description_present_flag = 1;
  224. if (avctx->color_range != AVCOL_RANGE_UNSPECIFIED ||
  225. vui->colour_description_present_flag)
  226. vui->video_signal_type_present_flag = 1;
  227. if (avctx->chroma_sample_location != AVCHROMA_LOC_UNSPECIFIED) {
  228. vui->chroma_loc_info_present_flag = 1;
  229. vui->chroma_sample_loc_type_top_field =
  230. vui->chroma_sample_loc_type_bottom_field =
  231. avctx->chroma_sample_location - 1;
  232. }
  233. vui->vui_timing_info_present_flag = 1;
  234. vui->vui_num_units_in_tick = vps->vps_num_units_in_tick;
  235. vui->vui_time_scale = vps->vps_time_scale;
  236. vui->vui_poc_proportional_to_timing_flag = vps->vps_poc_proportional_to_timing_flag;
  237. vui->vui_num_ticks_poc_diff_one_minus1 = vps->vps_num_ticks_poc_diff_one_minus1;
  238. vui->vui_hrd_parameters_present_flag = 0;
  239. vui->bitstream_restriction_flag = 1;
  240. vui->motion_vectors_over_pic_boundaries_flag = 1;
  241. vui->restricted_ref_pic_lists_flag = 1;
  242. vui->max_bytes_per_pic_denom = 0;
  243. vui->max_bits_per_min_cu_denom = 0;
  244. vui->log2_max_mv_length_horizontal = 15;
  245. vui->log2_max_mv_length_vertical = 15;
  246. // PPS
  247. pps->nal_unit_header = (H265RawNALUnitHeader) {
  248. .nal_unit_type = HEVC_NAL_PPS,
  249. .nuh_layer_id = 0,
  250. .nuh_temporal_id_plus1 = 1,
  251. };
  252. pps->pps_pic_parameter_set_id = 0;
  253. pps->pps_seq_parameter_set_id = sps->sps_seq_parameter_set_id;
  254. pps->num_ref_idx_l0_default_active_minus1 = 0;
  255. pps->num_ref_idx_l1_default_active_minus1 = 0;
  256. pps->init_qp_minus26 = opts->fixed_qp_idr - 26;
  257. pps->cu_qp_delta_enabled_flag = opts->cu_qp_delta_enabled_flag;
  258. pps->diff_cu_qp_delta_depth = 0;
  259. if (opts->tile_rows && opts->tile_cols) {
  260. int uniform_spacing;
  261. pps->tiles_enabled_flag = 1;
  262. pps->num_tile_columns_minus1 = opts->tile_cols - 1;
  263. pps->num_tile_rows_minus1 = opts->tile_rows - 1;
  264. // Test whether the spacing provided matches the H.265 uniform
  265. // spacing, and set the flag if it does.
  266. uniform_spacing = 1;
  267. for (i = 0; i <= pps->num_tile_columns_minus1 &&
  268. uniform_spacing; i++) {
  269. if (opts->col_width[i] !=
  270. (i + 1) * opts->slice_block_cols / opts->tile_cols -
  271. i * opts->slice_block_cols / opts->tile_cols)
  272. uniform_spacing = 0;
  273. }
  274. for (i = 0; i <= pps->num_tile_rows_minus1 &&
  275. uniform_spacing; i++) {
  276. if (opts->row_height[i] !=
  277. (i + 1) * opts->slice_block_rows / opts->tile_rows -
  278. i * opts->slice_block_rows / opts->tile_rows)
  279. uniform_spacing = 0;
  280. }
  281. pps->uniform_spacing_flag = uniform_spacing;
  282. for (i = 0; i <= pps->num_tile_columns_minus1; i++)
  283. pps->column_width_minus1[i] = opts->col_width[i] - 1;
  284. for (i = 0; i <= pps->num_tile_rows_minus1; i++)
  285. pps->row_height_minus1[i] = opts->row_height[i] - 1;
  286. pps->loop_filter_across_tiles_enabled_flag = 1;
  287. }
  288. pps->pps_loop_filter_across_slices_enabled_flag = 1;
  289. return 0;
  290. }