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.

232 lines
8.5KB

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