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.

237 lines
8.6KB

  1. /*
  2. * This file is part of Libav.
  3. *
  4. * Libav 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. * Libav 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 Libav; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #include <va/va.h>
  19. #include <va/va_dec_vp8.h>
  20. #include "hwaccel.h"
  21. #include "vaapi_decode.h"
  22. #include "vp8.h"
  23. static VASurfaceID vaapi_vp8_surface_id(VP8Frame *vf)
  24. {
  25. if (vf)
  26. return ff_vaapi_get_surface_id(vf->tf.f);
  27. else
  28. return VA_INVALID_SURFACE;
  29. }
  30. static int vaapi_vp8_start_frame(AVCodecContext *avctx,
  31. av_unused const uint8_t *buffer,
  32. av_unused uint32_t size)
  33. {
  34. const VP8Context *s = avctx->priv_data;
  35. VAAPIDecodePicture *pic = s->framep[VP56_FRAME_CURRENT]->hwaccel_picture_private;
  36. VAPictureParameterBufferVP8 pp;
  37. VAProbabilityDataBufferVP8 prob;
  38. VAIQMatrixBufferVP8 quant;
  39. int err, i, j, k;
  40. pic->output_surface = vaapi_vp8_surface_id(s->framep[VP56_FRAME_CURRENT]);
  41. pp = (VAPictureParameterBufferVP8) {
  42. .frame_width = avctx->width,
  43. .frame_height = avctx->height,
  44. .last_ref_frame = vaapi_vp8_surface_id(s->framep[VP56_FRAME_PREVIOUS]),
  45. .golden_ref_frame = vaapi_vp8_surface_id(s->framep[VP56_FRAME_GOLDEN]),
  46. .alt_ref_frame = vaapi_vp8_surface_id(s->framep[VP56_FRAME_GOLDEN2]),
  47. .out_of_loop_frame = VA_INVALID_SURFACE,
  48. .pic_fields.bits = {
  49. .key_frame = !s->keyframe,
  50. .version = s->profile,
  51. .segmentation_enabled = s->segmentation.enabled,
  52. .update_mb_segmentation_map = s->segmentation.update_map,
  53. .update_segment_feature_data = s->segmentation.update_feature_data,
  54. .filter_type = s->filter.simple,
  55. .sharpness_level = s->filter.sharpness,
  56. .loop_filter_adj_enable = s->lf_delta.enabled,
  57. .mode_ref_lf_delta_update = s->lf_delta.update,
  58. .sign_bias_golden = s->sign_bias[VP56_FRAME_GOLDEN],
  59. .sign_bias_alternate = s->sign_bias[VP56_FRAME_GOLDEN2],
  60. .mb_no_coeff_skip = s->mbskip_enabled,
  61. .loop_filter_disable = s->filter.level == 0,
  62. },
  63. .prob_skip_false = s->prob->mbskip,
  64. .prob_intra = s->prob->intra,
  65. .prob_last = s->prob->last,
  66. .prob_gf = s->prob->golden,
  67. };
  68. for (i = 0; i < 3; i++)
  69. pp.mb_segment_tree_probs[i] = s->prob->segmentid[i];
  70. for (i = 0; i < 4; i++) {
  71. if (s->segmentation.enabled) {
  72. pp.loop_filter_level[i] = s->segmentation.filter_level[i];
  73. if (!s->segmentation.absolute_vals)
  74. pp.loop_filter_level[i] += s->filter.level;
  75. } else {
  76. pp.loop_filter_level[i] = s->filter.level;
  77. }
  78. pp.loop_filter_level[i] = av_clip_uintp2(pp.loop_filter_level[i], 6);
  79. }
  80. for (i = 0; i < 4; i++) {
  81. pp.loop_filter_deltas_ref_frame[i] = s->lf_delta.ref[i];
  82. pp.loop_filter_deltas_mode[i] = s->lf_delta.mode[i + 4];
  83. }
  84. if (s->keyframe) {
  85. static const uint8_t keyframe_y_mode_probs[4] = {
  86. 145, 156, 163, 128
  87. };
  88. static const uint8_t keyframe_uv_mode_probs[3] = {
  89. 142, 114, 183
  90. };
  91. memcpy(pp.y_mode_probs, keyframe_y_mode_probs, 4);
  92. memcpy(pp.uv_mode_probs, keyframe_uv_mode_probs, 3);
  93. } else {
  94. for (i = 0; i < 4; i++)
  95. pp.y_mode_probs[i] = s->prob->pred16x16[i];
  96. for (i = 0; i < 3; i++)
  97. pp.uv_mode_probs[i] = s->prob->pred8x8c[i];
  98. }
  99. for (i = 0; i < 2; i++)
  100. for (j = 0; j < 19; j++)
  101. pp.mv_probs[i][j] = s->prob->mvc[i][j];
  102. pp.bool_coder_ctx.range = s->coder_state_at_header_end.range;
  103. pp.bool_coder_ctx.value = s->coder_state_at_header_end.value;
  104. pp.bool_coder_ctx.count = s->coder_state_at_header_end.bit_count;
  105. err = ff_vaapi_decode_make_param_buffer(avctx, pic,
  106. VAPictureParameterBufferType,
  107. &pp, sizeof(pp));
  108. if (err < 0)
  109. goto fail;
  110. for (i = 0; i < 4; i++) {
  111. for (j = 0; j < 8; j++) {
  112. static const int coeff_bands_inverse[8] = {
  113. 0, 1, 2, 3, 5, 6, 4, 15
  114. };
  115. int coeff_pos = coeff_bands_inverse[j];
  116. for (k = 0; k < 3; k++) {
  117. memcpy(prob.dct_coeff_probs[i][j][k],
  118. s->prob->token[i][coeff_pos][k], 11);
  119. }
  120. }
  121. }
  122. err = ff_vaapi_decode_make_param_buffer(avctx, pic,
  123. VAProbabilityBufferType,
  124. &prob, sizeof(prob));
  125. if (err < 0)
  126. goto fail;
  127. for (i = 0; i < 4; i++) {
  128. int base_qi = s->segmentation.base_quant[i];
  129. if (!s->segmentation.absolute_vals)
  130. base_qi += s->quant.yac_qi;
  131. quant.quantization_index[i][0] = av_clip_uintp2(base_qi, 7);
  132. quant.quantization_index[i][1] = av_clip_uintp2(base_qi + s->quant.ydc_delta, 7);
  133. quant.quantization_index[i][2] = av_clip_uintp2(base_qi + s->quant.y2dc_delta, 7);
  134. quant.quantization_index[i][3] = av_clip_uintp2(base_qi + s->quant.y2ac_delta, 7);
  135. quant.quantization_index[i][4] = av_clip_uintp2(base_qi + s->quant.uvdc_delta, 7);
  136. quant.quantization_index[i][5] = av_clip_uintp2(base_qi + s->quant.uvac_delta, 7);
  137. }
  138. err = ff_vaapi_decode_make_param_buffer(avctx, pic,
  139. VAIQMatrixBufferType,
  140. &quant, sizeof(quant));
  141. if (err < 0)
  142. goto fail;
  143. return 0;
  144. fail:
  145. ff_vaapi_decode_cancel(avctx, pic);
  146. return err;
  147. }
  148. static int vaapi_vp8_end_frame(AVCodecContext *avctx)
  149. {
  150. const VP8Context *s = avctx->priv_data;
  151. VAAPIDecodePicture *pic = s->framep[VP56_FRAME_CURRENT]->hwaccel_picture_private;
  152. return ff_vaapi_decode_issue(avctx, pic);
  153. }
  154. static int vaapi_vp8_decode_slice(AVCodecContext *avctx,
  155. const uint8_t *buffer,
  156. uint32_t size)
  157. {
  158. const VP8Context *s = avctx->priv_data;
  159. VAAPIDecodePicture *pic = s->framep[VP56_FRAME_CURRENT]->hwaccel_picture_private;
  160. VASliceParameterBufferVP8 sp;
  161. int err, i;
  162. unsigned int header_size = 3 + 7 * s->keyframe;
  163. const uint8_t *data = buffer + header_size;
  164. unsigned int data_size = size - header_size;
  165. sp = (VASliceParameterBufferVP8) {
  166. .slice_data_size = data_size,
  167. .slice_data_offset = 0,
  168. .slice_data_flag = VA_SLICE_DATA_FLAG_ALL,
  169. .macroblock_offset = (8 * (s->coder_state_at_header_end.input - data) -
  170. s->coder_state_at_header_end.bit_count - 8),
  171. .num_of_partitions = s->num_coeff_partitions + 1,
  172. };
  173. sp.partition_size[0] = s->header_partition_size - ((sp.macroblock_offset + 7) / 8);
  174. for (i = 0; i < 8; i++)
  175. sp.partition_size[i+1] = s->coeff_partition_size[i];
  176. err = ff_vaapi_decode_make_slice_buffer(avctx, pic, &sp, sizeof(sp), data, data_size);
  177. if (err)
  178. goto fail;
  179. return 0;
  180. fail:
  181. ff_vaapi_decode_cancel(avctx, pic);
  182. return err;
  183. }
  184. AVHWAccel ff_vp8_vaapi_hwaccel = {
  185. .name = "vp8_vaapi",
  186. .type = AVMEDIA_TYPE_VIDEO,
  187. .id = AV_CODEC_ID_VP8,
  188. .pix_fmt = AV_PIX_FMT_VAAPI,
  189. .start_frame = &vaapi_vp8_start_frame,
  190. .end_frame = &vaapi_vp8_end_frame,
  191. .decode_slice = &vaapi_vp8_decode_slice,
  192. .frame_priv_data_size = sizeof(VAAPIDecodePicture),
  193. .init = &ff_vaapi_decode_init,
  194. .uninit = &ff_vaapi_decode_uninit,
  195. .priv_data_size = sizeof(VAAPIDecodeContext),
  196. .caps_internal = HWACCEL_CAP_ASYNC_SAFE,
  197. };