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.

427 lines
19KB

  1. /*
  2. * MPEG-H Part 2 / HEVC / H.265 HW decode acceleration through VDPAU
  3. *
  4. * Copyright (c) 2013 Philip Langdale
  5. *
  6. * This file is part of Libav.
  7. *
  8. * Libav is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * Libav is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with Libav; if not, write to the Free Software Foundation,
  20. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. #include <vdpau/vdpau.h>
  23. #include "avcodec.h"
  24. #include "internal.h"
  25. #include "hevc.h"
  26. #include "vdpau.h"
  27. #include "vdpau_internal.h"
  28. static int vdpau_hevc_start_frame(AVCodecContext *avctx,
  29. const uint8_t *buffer, uint32_t size)
  30. {
  31. HEVCContext *h = avctx->priv_data;
  32. HEVCFrame *pic = h->ref;
  33. struct vdpau_picture_context *pic_ctx = pic->hwaccel_picture_private;
  34. VdpPictureInfoHEVC *info = &pic_ctx->info.hevc;
  35. const HEVCSPS *sps = h->ps.sps;
  36. const HEVCPPS *pps = h->ps.pps;
  37. const SliceHeader *sh = &h->sh;
  38. const ScalingList *sl = pps->scaling_list_data_present_flag ?
  39. &pps->scaling_list : &sps->scaling_list;
  40. /* init VdpPictureInfoHEVC */
  41. /* SPS */
  42. info->chroma_format_idc = sps->chroma_format_idc;
  43. info->separate_colour_plane_flag = sps->separate_colour_plane_flag;
  44. info->pic_width_in_luma_samples = sps->width;
  45. info->pic_height_in_luma_samples = sps->height;
  46. info->bit_depth_luma_minus8 = sps->bit_depth - 8;
  47. info->bit_depth_chroma_minus8 = sps->bit_depth - 8;
  48. info->log2_max_pic_order_cnt_lsb_minus4 = sps->log2_max_poc_lsb - 4;
  49. /* Provide the value corresponding to the nuh_temporal_id of the frame
  50. to be decoded. */
  51. info->sps_max_dec_pic_buffering_minus1 = sps->temporal_layer[sps->max_sub_layers - 1].max_dec_pic_buffering - 1;
  52. info->log2_min_luma_coding_block_size_minus3 = sps->log2_min_cb_size - 3;
  53. info->log2_diff_max_min_luma_coding_block_size = sps->log2_diff_max_min_coding_block_size;
  54. info->log2_min_transform_block_size_minus2 = sps->log2_min_tb_size - 2;
  55. info->log2_diff_max_min_transform_block_size = sps->log2_max_trafo_size - sps->log2_min_tb_size;
  56. info->max_transform_hierarchy_depth_inter = sps->max_transform_hierarchy_depth_inter;
  57. info->max_transform_hierarchy_depth_intra = sps->max_transform_hierarchy_depth_intra;
  58. info->scaling_list_enabled_flag = sps->scaling_list_enable_flag;
  59. /* Scaling lists, in diagonal order, to be used for this frame. */
  60. for (size_t i = 0; i < 6; i++) {
  61. for (size_t j = 0; j < 16; j++) {
  62. /* Scaling List for 4x4 quantization matrix,
  63. indexed as ScalingList4x4[matrixId][i]. */
  64. uint8_t pos = 4 * ff_hevc_diag_scan4x4_y[j] + ff_hevc_diag_scan4x4_x[j];
  65. info->ScalingList4x4[i][j] = sl->sl[0][i][pos];
  66. }
  67. for (size_t j = 0; j < 64; j++) {
  68. uint8_t pos = 8 * ff_hevc_diag_scan8x8_y[j] + ff_hevc_diag_scan8x8_x[j];
  69. /* Scaling List for 8x8 quantization matrix,
  70. indexed as ScalingList8x8[matrixId][i]. */
  71. info->ScalingList8x8[i][j] = sl->sl[1][i][pos];
  72. /* Scaling List for 16x16 quantization matrix,
  73. indexed as ScalingList16x16[matrixId][i]. */
  74. info->ScalingList16x16[i][j] = sl->sl[2][i][pos];
  75. if (i < 2) {
  76. /* Scaling List for 32x32 quantization matrix,
  77. indexed as ScalingList32x32[matrixId][i]. */
  78. info->ScalingList32x32[i][j] = sl->sl[3][i * 3][pos];
  79. }
  80. }
  81. /* Scaling List DC Coefficients for 16x16,
  82. indexed as ScalingListDCCoeff16x16[matrixId]. */
  83. info->ScalingListDCCoeff16x16[i] = sl->sl_dc[0][i];
  84. if (i < 2) {
  85. /* Scaling List DC Coefficients for 32x32,
  86. indexed as ScalingListDCCoeff32x32[matrixId]. */
  87. info->ScalingListDCCoeff32x32[i] = sl->sl_dc[1][i * 3];
  88. }
  89. }
  90. info->amp_enabled_flag = sps->amp_enabled_flag;
  91. info->sample_adaptive_offset_enabled_flag = sps->sao_enabled;
  92. info->pcm_enabled_flag = sps->pcm_enabled_flag;
  93. if (info->pcm_enabled_flag) {
  94. /* Only needs to be set if pcm_enabled_flag is set. Ignored otherwise. */
  95. info->pcm_sample_bit_depth_luma_minus1 = sps->pcm.bit_depth - 1;
  96. /* Only needs to be set if pcm_enabled_flag is set. Ignored otherwise. */
  97. info->pcm_sample_bit_depth_chroma_minus1 = sps->pcm.bit_depth_chroma - 1;
  98. /* Only needs to be set if pcm_enabled_flag is set. Ignored otherwise. */
  99. info->log2_min_pcm_luma_coding_block_size_minus3 = sps->pcm.log2_min_pcm_cb_size - 3;
  100. /* Only needs to be set if pcm_enabled_flag is set. Ignored otherwise. */
  101. info->log2_diff_max_min_pcm_luma_coding_block_size = sps->pcm.log2_max_pcm_cb_size - sps->pcm.log2_min_pcm_cb_size;
  102. /* Only needs to be set if pcm_enabled_flag is set. Ignored otherwise. */
  103. info->pcm_loop_filter_disabled_flag = sps->pcm.loop_filter_disable_flag;
  104. }
  105. /* Per spec, when zero, assume short_term_ref_pic_set_sps_flag
  106. is also zero. */
  107. info->num_short_term_ref_pic_sets = sps->nb_st_rps;
  108. info->long_term_ref_pics_present_flag = sps->long_term_ref_pics_present_flag;
  109. /* Only needed if long_term_ref_pics_present_flag is set. Ignored
  110. otherwise. */
  111. info->num_long_term_ref_pics_sps = sps->num_long_term_ref_pics_sps;
  112. info->sps_temporal_mvp_enabled_flag = sps->sps_temporal_mvp_enabled_flag;
  113. info->strong_intra_smoothing_enabled_flag = sps->sps_strong_intra_smoothing_enable_flag;
  114. /* Copy the HEVC Picture Parameter Set bitstream fields. */
  115. info->dependent_slice_segments_enabled_flag = pps->dependent_slice_segments_enabled_flag;
  116. info->output_flag_present_flag = pps->output_flag_present_flag;
  117. info->num_extra_slice_header_bits = pps->num_extra_slice_header_bits;
  118. info->sign_data_hiding_enabled_flag = pps->sign_data_hiding_flag;
  119. info->cabac_init_present_flag = pps->cabac_init_present_flag;
  120. info->num_ref_idx_l0_default_active_minus1 = pps->num_ref_idx_l0_default_active - 1;
  121. info->num_ref_idx_l1_default_active_minus1 = pps->num_ref_idx_l1_default_active - 1;
  122. info->init_qp_minus26 = pps->pic_init_qp_minus26;
  123. info->constrained_intra_pred_flag = pps->constrained_intra_pred_flag;
  124. info->transform_skip_enabled_flag = pps->transform_skip_enabled_flag;
  125. info->cu_qp_delta_enabled_flag = pps->cu_qp_delta_enabled_flag;
  126. /* Only needed if cu_qp_delta_enabled_flag is set. Ignored otherwise. */
  127. info->diff_cu_qp_delta_depth = pps->diff_cu_qp_delta_depth;
  128. info->pps_cb_qp_offset = pps->cb_qp_offset;
  129. info->pps_cr_qp_offset = pps->cr_qp_offset;
  130. info->pps_slice_chroma_qp_offsets_present_flag = pps->pic_slice_level_chroma_qp_offsets_present_flag;
  131. info->weighted_pred_flag = pps->weighted_pred_flag;
  132. info->weighted_bipred_flag = pps->weighted_bipred_flag;
  133. info->transquant_bypass_enabled_flag = pps->transquant_bypass_enable_flag;
  134. info->tiles_enabled_flag = pps->tiles_enabled_flag;
  135. info->entropy_coding_sync_enabled_flag = pps->entropy_coding_sync_enabled_flag;
  136. if (info->tiles_enabled_flag) {
  137. /* Only valid if tiles_enabled_flag is set. Ignored otherwise. */
  138. info->num_tile_columns_minus1 = pps->num_tile_columns - 1;
  139. /* Only valid if tiles_enabled_flag is set. Ignored otherwise. */
  140. info->num_tile_rows_minus1 = pps->num_tile_rows - 1;
  141. /* Only valid if tiles_enabled_flag is set. Ignored otherwise. */
  142. info->uniform_spacing_flag = pps->uniform_spacing_flag;
  143. /* Only need to set 0..num_tile_columns_minus1. The struct
  144. definition reserves up to the maximum of 20. Invalid values are
  145. ignored. */
  146. for (ssize_t i = 0; i < pps->num_tile_columns; i++) {
  147. info->column_width_minus1[i] = pps->column_width[i] - 1;
  148. }
  149. /* Only need to set 0..num_tile_rows_minus1. The struct
  150. definition reserves up to the maximum of 22. Invalid values are
  151. ignored.*/
  152. for (ssize_t i = 0; i < pps->num_tile_rows; i++) {
  153. info->row_height_minus1[i] = pps->row_height[i] - 1;
  154. }
  155. /* Only needed if tiles_enabled_flag is set. Invalid values are
  156. ignored. */
  157. info->loop_filter_across_tiles_enabled_flag = pps->loop_filter_across_tiles_enabled_flag;
  158. }
  159. info->pps_loop_filter_across_slices_enabled_flag = pps->seq_loop_filter_across_slices_enabled_flag;
  160. info->deblocking_filter_control_present_flag = pps->deblocking_filter_control_present_flag;
  161. /* Only valid if deblocking_filter_control_present_flag is set. Ignored
  162. otherwise. */
  163. info->deblocking_filter_override_enabled_flag = pps->deblocking_filter_override_enabled_flag;
  164. /* Only valid if deblocking_filter_control_present_flag is set. Ignored
  165. otherwise. */
  166. info->pps_deblocking_filter_disabled_flag = pps->disable_dbf;
  167. /* Only valid if deblocking_filter_control_present_flag is set and
  168. pps_deblocking_filter_disabled_flag is not set. Ignored otherwise.*/
  169. info->pps_beta_offset_div2 = pps->beta_offset / 2;
  170. /* Only valid if deblocking_filter_control_present_flag is set and
  171. pps_deblocking_filter_disabled_flag is not set. Ignored otherwise. */
  172. info->pps_tc_offset_div2 = pps->tc_offset / 2;
  173. info->lists_modification_present_flag = pps->lists_modification_present_flag;
  174. info->log2_parallel_merge_level_minus2 = pps->log2_parallel_merge_level - 2;
  175. info->slice_segment_header_extension_present_flag = pps->slice_header_extension_present_flag;
  176. /* Set to 1 if nal_unit_type is equal to IDR_W_RADL or IDR_N_LP.
  177. Set to zero otherwise. */
  178. info->IDRPicFlag = IS_IDR(h);
  179. /* Set to 1 if nal_unit_type in the range of BLA_W_LP to
  180. RSV_IRAP_VCL23, inclusive. Set to zero otherwise.*/
  181. info->RAPPicFlag = IS_IRAP(h);
  182. /* See section 7.4.7.1 of the specification. */
  183. info->CurrRpsIdx = sps->nb_st_rps;
  184. if (sh->short_term_ref_pic_set_sps_flag == 1) {
  185. for (size_t i = 0; i < sps->nb_st_rps; i++) {
  186. if (sh->short_term_rps == &sps->st_rps[i]) {
  187. info->CurrRpsIdx = i;
  188. break;
  189. }
  190. }
  191. }
  192. /* See section 7.4.7.2 of the specification. */
  193. info->NumPocTotalCurr = ff_hevc_frame_nb_refs(h);
  194. if (sh->short_term_ref_pic_set_sps_flag == 0 && sh->short_term_rps) {
  195. /* Corresponds to specification field, NumDeltaPocs[RefRpsIdx].
  196. Only applicable when short_term_ref_pic_set_sps_flag == 0.
  197. Implementations will ignore this value in other cases. See 7.4.8. */
  198. info->NumDeltaPocsOfRefRpsIdx = sh->short_term_rps->rps_idx_num_delta_pocs;
  199. }
  200. /* Section 7.6.3.1 of the H.265/HEVC Specification defines the syntax of
  201. the slice_segment_header. This header contains information that
  202. some VDPAU implementations may choose to skip. The VDPAU API
  203. requires client applications to track the number of bits used in the
  204. slice header for structures associated with short term and long term
  205. reference pictures. First, VDPAU requires the number of bits used by
  206. the short_term_ref_pic_set array in the slice_segment_header. */
  207. info->NumShortTermPictureSliceHeaderBits = sh->short_term_ref_pic_set_size;
  208. /* Second, VDPAU requires the number of bits used for long term reference
  209. pictures in the slice_segment_header. This is equal to the number
  210. of bits used for the contents of the block beginning with
  211. "if(long_term_ref_pics_present_flag)". */
  212. info->NumLongTermPictureSliceHeaderBits = sh->long_term_ref_pic_set_size;
  213. /* The value of PicOrderCntVal of the picture in the access unit
  214. containing the SEI message. The picture being decoded. */
  215. info->CurrPicOrderCntVal = h->poc;
  216. /* Slice Decoding Process - Reference Picture Sets */
  217. for (size_t i = 0; i < 16; i++) {
  218. info->RefPics[i] = VDP_INVALID_HANDLE;
  219. info->PicOrderCntVal[i] = 0;
  220. info->IsLongTerm[i] = 0;
  221. }
  222. for (size_t i = 0, j = 0; i < FF_ARRAY_ELEMS(h->DPB); i++) {
  223. const HEVCFrame *frame = &h->DPB[i];
  224. if (frame != h->ref && (frame->flags & (HEVC_FRAME_FLAG_LONG_REF |
  225. HEVC_FRAME_FLAG_SHORT_REF))) {
  226. if (j > 16) {
  227. av_log(avctx, AV_LOG_WARNING,
  228. "VDPAU only supports up to 16 references in the DPB. "
  229. "This frame may not be decoded correctly.\n");
  230. break;
  231. }
  232. /* Array of video reference surfaces.
  233. Set any unused positions to VDP_INVALID_HANDLE. */
  234. info->RefPics[j] = ff_vdpau_get_surface_id(frame->frame);
  235. /* Array of picture order counts. These correspond to positions
  236. in the RefPics array. */
  237. info->PicOrderCntVal[j] = frame->poc;
  238. /* Array used to specify whether a particular RefPic is
  239. a long term reference. A value of "1" indicates a long-term
  240. reference. */
  241. // XXX: Setting this caused glitches in the nvidia implementation
  242. // Always setting it to zero, produces correct results
  243. //info->IsLongTerm[j] = frame->flags & HEVC_FRAME_FLAG_LONG_REF;
  244. info->IsLongTerm[j] = 0;
  245. j++;
  246. }
  247. }
  248. /* Copy of specification field, see Section 8.3.2 of the
  249. H.265/HEVC Specification. */
  250. info->NumPocStCurrBefore = h->rps[ST_CURR_BEF].nb_refs;
  251. if (info->NumPocStCurrBefore > 8) {
  252. av_log(avctx, AV_LOG_WARNING,
  253. "VDPAU only supports up to 8 references in StCurrBefore. "
  254. "This frame may not be decoded correctly.\n");
  255. info->NumPocStCurrBefore = 8;
  256. }
  257. /* Copy of specification field, see Section 8.3.2 of the
  258. H.265/HEVC Specification. */
  259. info->NumPocStCurrAfter = h->rps[ST_CURR_AFT].nb_refs;
  260. if (info->NumPocStCurrAfter > 8) {
  261. av_log(avctx, AV_LOG_WARNING,
  262. "VDPAU only supports up to 8 references in StCurrAfter. "
  263. "This frame may not be decoded correctly.\n");
  264. info->NumPocStCurrAfter = 8;
  265. }
  266. /* Copy of specification field, see Section 8.3.2 of the
  267. H.265/HEVC Specification. */
  268. info->NumPocLtCurr = h->rps[LT_CURR].nb_refs;
  269. if (info->NumPocLtCurr > 8) {
  270. av_log(avctx, AV_LOG_WARNING,
  271. "VDPAU only supports up to 8 references in LtCurr. "
  272. "This frame may not be decoded correctly.\n");
  273. info->NumPocLtCurr = 8;
  274. }
  275. /* Reference Picture Set list, one of the short-term RPS. These
  276. correspond to positions in the RefPics array. */
  277. for (ssize_t i = 0, j = 0; i < h->rps[ST_CURR_BEF].nb_refs; i++) {
  278. HEVCFrame *frame = h->rps[ST_CURR_BEF].ref[i];
  279. if (frame) {
  280. uint8_t found = 0;
  281. uintptr_t id = ff_vdpau_get_surface_id(frame->frame);
  282. for (size_t k = 0; k < 16; k++) {
  283. if (id == info->RefPics[k]) {
  284. info->RefPicSetStCurrBefore[j] = k;
  285. j++;
  286. found = 1;
  287. break;
  288. }
  289. }
  290. if (!found) {
  291. av_log(avctx, AV_LOG_WARNING, "missing surface: %p\n",
  292. (void *)id);
  293. }
  294. } else {
  295. av_log(avctx, AV_LOG_WARNING, "missing STR Before frame: %zd\n", i);
  296. }
  297. }
  298. /* Reference Picture Set list, one of the short-term RPS. These
  299. correspond to positions in the RefPics array. */
  300. for (ssize_t i = 0, j = 0; i < h->rps[ST_CURR_AFT].nb_refs; i++) {
  301. HEVCFrame *frame = h->rps[ST_CURR_AFT].ref[i];
  302. if (frame) {
  303. uint8_t found = 0;
  304. uintptr_t id = ff_vdpau_get_surface_id(frame->frame);
  305. for (size_t k = 0; k < 16; k++) {
  306. if (id == info->RefPics[k]) {
  307. info->RefPicSetStCurrAfter[j] = k;
  308. j++;
  309. found = 1;
  310. break;
  311. }
  312. }
  313. if (!found) {
  314. av_log(avctx, AV_LOG_WARNING, "missing surface: %p\n",
  315. (void *)id);
  316. }
  317. } else {
  318. av_log(avctx, AV_LOG_WARNING, "missing STR After frame: %zd\n", i);
  319. }
  320. }
  321. /* Reference Picture Set list, one of the long-term RPS. These
  322. correspond to positions in the RefPics array. */
  323. for (ssize_t i = 0, j = 0; i < h->rps[LT_CURR].nb_refs; i++) {
  324. HEVCFrame *frame = h->rps[LT_CURR].ref[i];
  325. if (frame) {
  326. uint8_t found = 0;
  327. uintptr_t id = ff_vdpau_get_surface_id(frame->frame);
  328. for (size_t k = 0; k < 16; k++) {
  329. if (id == info->RefPics[k]) {
  330. info->RefPicSetLtCurr[j] = k;
  331. j++;
  332. found = 1;
  333. break;
  334. }
  335. }
  336. if (!found) {
  337. av_log(avctx, AV_LOG_WARNING, "missing surface: %p\n",
  338. (void *)id);
  339. }
  340. } else {
  341. av_log(avctx, AV_LOG_WARNING, "missing LTR frame: %zd\n", i);
  342. }
  343. }
  344. return ff_vdpau_common_start_frame(pic_ctx, buffer, size);
  345. }
  346. static const uint8_t start_code_prefix[3] = { 0x00, 0x00, 0x01 };
  347. static int vdpau_hevc_decode_slice(AVCodecContext *avctx,
  348. const uint8_t *buffer, uint32_t size)
  349. {
  350. HEVCContext *h = avctx->priv_data;
  351. struct vdpau_picture_context *pic_ctx = h->ref->hwaccel_picture_private;
  352. int val;
  353. val = ff_vdpau_add_buffer(pic_ctx, start_code_prefix, 3);
  354. if (val)
  355. return val;
  356. val = ff_vdpau_add_buffer(pic_ctx, buffer, size);
  357. if (val)
  358. return val;
  359. return 0;
  360. }
  361. static int vdpau_hevc_end_frame(AVCodecContext *avctx)
  362. {
  363. HEVCContext *h = avctx->priv_data;
  364. struct vdpau_picture_context *pic_ctx = h->ref->hwaccel_picture_private;
  365. int val;
  366. val = ff_vdpau_common_end_frame(avctx, h->ref->frame, pic_ctx);
  367. if (val < 0)
  368. return val;
  369. return 0;
  370. }
  371. static int vdpau_hevc_init(AVCodecContext *avctx)
  372. {
  373. VdpDecoderProfile profile;
  374. uint32_t level = avctx->level;
  375. switch (avctx->profile) {
  376. case FF_PROFILE_HEVC_MAIN:
  377. profile = VDP_DECODER_PROFILE_HEVC_MAIN;
  378. break;
  379. case FF_PROFILE_HEVC_MAIN_10:
  380. profile = VDP_DECODER_PROFILE_HEVC_MAIN_10;
  381. break;
  382. case FF_PROFILE_HEVC_MAIN_STILL_PICTURE:
  383. profile = VDP_DECODER_PROFILE_HEVC_MAIN_STILL;
  384. break;
  385. default:
  386. return AVERROR(ENOTSUP);
  387. }
  388. return ff_vdpau_common_init(avctx, profile, level);
  389. }
  390. AVHWAccel ff_hevc_vdpau_hwaccel = {
  391. .name = "hevc_vdpau",
  392. .type = AVMEDIA_TYPE_VIDEO,
  393. .id = AV_CODEC_ID_HEVC,
  394. .pix_fmt = AV_PIX_FMT_VDPAU,
  395. .start_frame = vdpau_hevc_start_frame,
  396. .end_frame = vdpau_hevc_end_frame,
  397. .decode_slice = vdpau_hevc_decode_slice,
  398. .frame_priv_data_size = sizeof(struct vdpau_picture_context),
  399. .init = vdpau_hevc_init,
  400. .uninit = ff_vdpau_common_uninit,
  401. .priv_data_size = sizeof(VDPAUContext),
  402. };