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.

186 lines
7.3KB

  1. /*
  2. * VP9 HW decode acceleration through VA API
  3. *
  4. * Copyright (C) 2015 Timo Rothenpieler <timo@rothenpieler.org>
  5. *
  6. * This file is part of FFmpeg.
  7. *
  8. * FFmpeg 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. * FFmpeg 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 FFmpeg; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. #include "libavutil/pixdesc.h"
  23. #include "hwaccel.h"
  24. #include "vaapi_decode.h"
  25. #include "vp9shared.h"
  26. static VASurfaceID vaapi_vp9_surface_id(const VP9Frame *vf)
  27. {
  28. if (vf)
  29. return ff_vaapi_get_surface_id(vf->tf.f);
  30. else
  31. return VA_INVALID_SURFACE;
  32. }
  33. static int vaapi_vp9_start_frame(AVCodecContext *avctx,
  34. av_unused const uint8_t *buffer,
  35. av_unused uint32_t size)
  36. {
  37. const VP9SharedContext *h = avctx->priv_data;
  38. VAAPIDecodePicture *pic = h->frames[CUR_FRAME].hwaccel_picture_private;
  39. VADecPictureParameterBufferVP9 pic_param;
  40. const AVPixFmtDescriptor *pixdesc = av_pix_fmt_desc_get(avctx->sw_pix_fmt);
  41. int err, i;
  42. pic->output_surface = vaapi_vp9_surface_id(&h->frames[CUR_FRAME]);
  43. pic_param = (VADecPictureParameterBufferVP9) {
  44. .frame_width = avctx->width,
  45. .frame_height = avctx->height,
  46. .pic_fields.bits = {
  47. .subsampling_x = pixdesc->log2_chroma_w,
  48. .subsampling_y = pixdesc->log2_chroma_h,
  49. .frame_type = !h->h.keyframe,
  50. .show_frame = !h->h.invisible,
  51. .error_resilient_mode = h->h.errorres,
  52. .intra_only = h->h.intraonly,
  53. .allow_high_precision_mv = h->h.keyframe ? 0 : h->h.highprecisionmvs,
  54. .mcomp_filter_type = h->h.filtermode ^ (h->h.filtermode <= 1),
  55. .frame_parallel_decoding_mode = h->h.parallelmode,
  56. .reset_frame_context = h->h.resetctx,
  57. .refresh_frame_context = h->h.refreshctx,
  58. .frame_context_idx = h->h.framectxid,
  59. .segmentation_enabled = h->h.segmentation.enabled,
  60. .segmentation_temporal_update = h->h.segmentation.temporal,
  61. .segmentation_update_map = h->h.segmentation.update_map,
  62. .last_ref_frame = h->h.refidx[0],
  63. .last_ref_frame_sign_bias = h->h.signbias[0],
  64. .golden_ref_frame = h->h.refidx[1],
  65. .golden_ref_frame_sign_bias = h->h.signbias[1],
  66. .alt_ref_frame = h->h.refidx[2],
  67. .alt_ref_frame_sign_bias = h->h.signbias[2],
  68. .lossless_flag = h->h.lossless,
  69. },
  70. .filter_level = h->h.filter.level,
  71. .sharpness_level = h->h.filter.sharpness,
  72. .log2_tile_rows = h->h.tiling.log2_tile_rows,
  73. .log2_tile_columns = h->h.tiling.log2_tile_cols,
  74. .frame_header_length_in_bytes = h->h.uncompressed_header_size,
  75. .first_partition_size = h->h.compressed_header_size,
  76. .profile = h->h.profile,
  77. .bit_depth = h->h.bpp,
  78. };
  79. for (i = 0; i < 7; i++)
  80. pic_param.mb_segment_tree_probs[i] = h->h.segmentation.prob[i];
  81. if (h->h.segmentation.temporal) {
  82. for (i = 0; i < 3; i++)
  83. pic_param.segment_pred_probs[i] = h->h.segmentation.pred_prob[i];
  84. } else {
  85. memset(pic_param.segment_pred_probs, 255, sizeof(pic_param.segment_pred_probs));
  86. }
  87. for (i = 0; i < 8; i++) {
  88. if (h->refs[i].f->buf[0])
  89. pic_param.reference_frames[i] = ff_vaapi_get_surface_id(h->refs[i].f);
  90. else
  91. pic_param.reference_frames[i] = VA_INVALID_ID;
  92. }
  93. err = ff_vaapi_decode_make_param_buffer(avctx, pic,
  94. VAPictureParameterBufferType,
  95. &pic_param, sizeof(pic_param));
  96. if (err < 0) {
  97. ff_vaapi_decode_cancel(avctx, pic);
  98. return err;
  99. }
  100. return 0;
  101. }
  102. static int vaapi_vp9_end_frame(AVCodecContext *avctx)
  103. {
  104. const VP9SharedContext *h = avctx->priv_data;
  105. VAAPIDecodePicture *pic = h->frames[CUR_FRAME].hwaccel_picture_private;
  106. return ff_vaapi_decode_issue(avctx, pic);
  107. }
  108. static int vaapi_vp9_decode_slice(AVCodecContext *avctx,
  109. const uint8_t *buffer,
  110. uint32_t size)
  111. {
  112. const VP9SharedContext *h = avctx->priv_data;
  113. VAAPIDecodePicture *pic = h->frames[CUR_FRAME].hwaccel_picture_private;
  114. VASliceParameterBufferVP9 slice_param;
  115. int err, i;
  116. slice_param = (VASliceParameterBufferVP9) {
  117. .slice_data_size = size,
  118. .slice_data_offset = 0,
  119. .slice_data_flag = VA_SLICE_DATA_FLAG_ALL,
  120. };
  121. for (i = 0; i < 8; i++) {
  122. slice_param.seg_param[i] = (VASegmentParameterVP9) {
  123. .segment_flags.fields = {
  124. .segment_reference_enabled = h->h.segmentation.feat[i].ref_enabled,
  125. .segment_reference = h->h.segmentation.feat[i].ref_val,
  126. .segment_reference_skipped = h->h.segmentation.feat[i].skip_enabled,
  127. },
  128. .luma_dc_quant_scale = h->h.segmentation.feat[i].qmul[0][0],
  129. .luma_ac_quant_scale = h->h.segmentation.feat[i].qmul[0][1],
  130. .chroma_dc_quant_scale = h->h.segmentation.feat[i].qmul[1][0],
  131. .chroma_ac_quant_scale = h->h.segmentation.feat[i].qmul[1][1],
  132. };
  133. memcpy(slice_param.seg_param[i].filter_level, h->h.segmentation.feat[i].lflvl, sizeof(slice_param.seg_param[i].filter_level));
  134. }
  135. err = ff_vaapi_decode_make_slice_buffer(avctx, pic,
  136. &slice_param, sizeof(slice_param),
  137. buffer, size);
  138. if (err) {
  139. ff_vaapi_decode_cancel(avctx, pic);
  140. return err;
  141. }
  142. return 0;
  143. }
  144. const AVHWAccel ff_vp9_vaapi_hwaccel = {
  145. .name = "vp9_vaapi",
  146. .type = AVMEDIA_TYPE_VIDEO,
  147. .id = AV_CODEC_ID_VP9,
  148. .pix_fmt = AV_PIX_FMT_VAAPI,
  149. .start_frame = vaapi_vp9_start_frame,
  150. .end_frame = vaapi_vp9_end_frame,
  151. .decode_slice = vaapi_vp9_decode_slice,
  152. .frame_priv_data_size = sizeof(VAAPIDecodePicture),
  153. .init = ff_vaapi_decode_init,
  154. .uninit = ff_vaapi_decode_uninit,
  155. .frame_params = ff_vaapi_common_frame_params,
  156. .priv_data_size = sizeof(VAAPIDecodeContext),
  157. .caps_internal = HWACCEL_CAP_ASYNC_SAFE,
  158. };