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.

170 lines
6.6KB

  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 "vaapi_internal.h"
  24. #include "vp9.h"
  25. static void fill_picture_parameters(AVCodecContext *avctx,
  26. const VP9SharedContext *h,
  27. VADecPictureParameterBufferVP9 *pp)
  28. {
  29. const AVPixFmtDescriptor *pixdesc = av_pix_fmt_desc_get(avctx->sw_pix_fmt);
  30. int i;
  31. pp->frame_width = avctx->width;
  32. pp->frame_height = avctx->height;
  33. pp->frame_header_length_in_bytes = h->h.uncompressed_header_size;
  34. pp->first_partition_size = h->h.compressed_header_size;
  35. pp->profile = h->h.profile;
  36. pp->bit_depth = h->h.bpp;
  37. pp->filter_level = h->h.filter.level;
  38. pp->sharpness_level = h->h.filter.sharpness;
  39. pp->log2_tile_rows = h->h.tiling.log2_tile_rows;
  40. pp->log2_tile_columns = h->h.tiling.log2_tile_cols;
  41. pp->pic_fields.bits.subsampling_x = pixdesc->log2_chroma_w;
  42. pp->pic_fields.bits.subsampling_y = pixdesc->log2_chroma_h;
  43. pp->pic_fields.bits.frame_type = !h->h.keyframe;
  44. pp->pic_fields.bits.show_frame = !h->h.invisible;
  45. pp->pic_fields.bits.error_resilient_mode = h->h.errorres;
  46. pp->pic_fields.bits.intra_only = h->h.intraonly;
  47. pp->pic_fields.bits.allow_high_precision_mv = h->h.keyframe ? 0 : h->h.highprecisionmvs;
  48. pp->pic_fields.bits.mcomp_filter_type = h->h.filtermode ^ (h->h.filtermode <= 1);
  49. pp->pic_fields.bits.frame_parallel_decoding_mode = h->h.parallelmode;
  50. pp->pic_fields.bits.reset_frame_context = h->h.resetctx;
  51. pp->pic_fields.bits.refresh_frame_context = h->h.refreshctx;
  52. pp->pic_fields.bits.frame_context_idx = h->h.framectxid;
  53. pp->pic_fields.bits.segmentation_enabled = h->h.segmentation.enabled;
  54. pp->pic_fields.bits.segmentation_temporal_update = h->h.segmentation.temporal;
  55. pp->pic_fields.bits.segmentation_update_map = h->h.segmentation.update_map;
  56. pp->pic_fields.bits.last_ref_frame = h->h.refidx[0];
  57. pp->pic_fields.bits.last_ref_frame_sign_bias = h->h.signbias[0];
  58. pp->pic_fields.bits.golden_ref_frame = h->h.refidx[1];
  59. pp->pic_fields.bits.golden_ref_frame_sign_bias = h->h.signbias[1];
  60. pp->pic_fields.bits.alt_ref_frame = h->h.refidx[2];
  61. pp->pic_fields.bits.alt_ref_frame_sign_bias = h->h.signbias[2];
  62. pp->pic_fields.bits.lossless_flag = h->h.lossless;
  63. for (i = 0; i < 7; i++)
  64. pp->mb_segment_tree_probs[i] = h->h.segmentation.prob[i];
  65. if (h->h.segmentation.temporal) {
  66. for (i = 0; i < 3; i++)
  67. pp->segment_pred_probs[i] = h->h.segmentation.pred_prob[i];
  68. } else {
  69. memset(pp->segment_pred_probs, 255, sizeof(pp->segment_pred_probs));
  70. }
  71. for (i = 0; i < 8; i++) {
  72. if (h->refs[i].f->buf[0]) {
  73. pp->reference_frames[i] = ff_vaapi_get_surface_id(h->refs[i].f);
  74. } else {
  75. pp->reference_frames[i] = VA_INVALID_ID;
  76. }
  77. }
  78. }
  79. static int vaapi_vp9_start_frame(AVCodecContext *avctx,
  80. av_unused const uint8_t *buffer,
  81. av_unused uint32_t size)
  82. {
  83. const VP9SharedContext *h = avctx->priv_data;
  84. FFVAContext * const vactx = ff_vaapi_get_context(avctx);
  85. VADecPictureParameterBufferVP9 *pic_param;
  86. vactx->slice_param_size = sizeof(VASliceParameterBufferVP9);
  87. pic_param = ff_vaapi_alloc_pic_param(vactx, sizeof(VADecPictureParameterBufferVP9));
  88. if (!pic_param)
  89. return -1;
  90. fill_picture_parameters(avctx, h, pic_param);
  91. return 0;
  92. }
  93. static int vaapi_vp9_end_frame(AVCodecContext *avctx)
  94. {
  95. FFVAContext * const vactx = ff_vaapi_get_context(avctx);
  96. const VP9SharedContext *h = avctx->priv_data;
  97. int ret;
  98. ret = ff_vaapi_commit_slices(vactx);
  99. if (ret < 0)
  100. goto finish;
  101. ret = ff_vaapi_render_picture(vactx, ff_vaapi_get_surface_id(h->frames[CUR_FRAME].tf.f));
  102. if (ret < 0)
  103. goto finish;
  104. finish:
  105. ff_vaapi_common_end_frame(avctx);
  106. return ret;
  107. }
  108. static int vaapi_vp9_decode_slice(AVCodecContext *avctx,
  109. const uint8_t *buffer,
  110. uint32_t size)
  111. {
  112. FFVAContext * const vactx = ff_vaapi_get_context(avctx);
  113. const VP9SharedContext *h = avctx->priv_data;
  114. VASliceParameterBufferVP9 *slice_param;
  115. int i;
  116. slice_param = (VASliceParameterBufferVP9*)ff_vaapi_alloc_slice(vactx, buffer, size);
  117. if (!slice_param)
  118. return -1;
  119. for (i = 0; i < 8; i++) {
  120. slice_param->seg_param[i].segment_flags.fields.segment_reference_enabled = h->h.segmentation.feat[i].ref_enabled;
  121. slice_param->seg_param[i].segment_flags.fields.segment_reference = h->h.segmentation.feat[i].ref_val;
  122. slice_param->seg_param[i].segment_flags.fields.segment_reference_skipped = h->h.segmentation.feat[i].skip_enabled;
  123. memcpy(slice_param->seg_param[i].filter_level, h->h.segmentation.feat[i].lflvl, sizeof(slice_param->seg_param[i].filter_level));
  124. slice_param->seg_param[i].luma_dc_quant_scale = h->h.segmentation.feat[i].qmul[0][0];
  125. slice_param->seg_param[i].luma_ac_quant_scale = h->h.segmentation.feat[i].qmul[0][1];
  126. slice_param->seg_param[i].chroma_dc_quant_scale = h->h.segmentation.feat[i].qmul[1][0];
  127. slice_param->seg_param[i].chroma_ac_quant_scale = h->h.segmentation.feat[i].qmul[1][1];
  128. }
  129. return 0;
  130. }
  131. AVHWAccel ff_vp9_vaapi_hwaccel = {
  132. .name = "vp9_vaapi",
  133. .type = AVMEDIA_TYPE_VIDEO,
  134. .id = AV_CODEC_ID_VP9,
  135. .pix_fmt = AV_PIX_FMT_VAAPI,
  136. .start_frame = vaapi_vp9_start_frame,
  137. .end_frame = vaapi_vp9_end_frame,
  138. .decode_slice = vaapi_vp9_decode_slice,
  139. .init = ff_vaapi_context_init,
  140. .uninit = ff_vaapi_context_fini,
  141. .priv_data_size = sizeof(FFVAContext),
  142. };