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.

224 lines
8.5KB

  1. /*
  2. * MPEG-4 / H.263 HW decode acceleration through VA API
  3. *
  4. * Copyright (C) 2008-2009 Splitted-Desktop Systems
  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
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. #include "h263.h"
  23. #include "hwaccel.h"
  24. #include "internal.h"
  25. #include "mpeg4video.h"
  26. #include "mpegvideo.h"
  27. #include "vaapi_decode.h"
  28. /** Reconstruct bitstream intra_dc_vlc_thr */
  29. static int mpeg4_get_intra_dc_vlc_thr(Mpeg4DecContext *s)
  30. {
  31. switch (s->intra_dc_threshold) {
  32. case 99: return 0;
  33. case 13: return 1;
  34. case 15: return 2;
  35. case 17: return 3;
  36. case 19: return 4;
  37. case 21: return 5;
  38. case 23: return 6;
  39. case 0: return 7;
  40. }
  41. return 0;
  42. }
  43. static int vaapi_mpeg4_start_frame(AVCodecContext *avctx, av_unused const uint8_t *buffer, av_unused uint32_t size)
  44. {
  45. Mpeg4DecContext *ctx = avctx->priv_data;
  46. MpegEncContext *s = &ctx->m;
  47. VAAPIDecodePicture *pic = s->current_picture_ptr->hwaccel_picture_private;
  48. VAPictureParameterBufferMPEG4 pic_param;
  49. int i, err;
  50. pic->output_surface = ff_vaapi_get_surface_id(s->current_picture_ptr->f);
  51. pic_param = (VAPictureParameterBufferMPEG4) {
  52. .vop_width = s->width,
  53. .vop_height = s->height,
  54. .forward_reference_picture = VA_INVALID_ID,
  55. .backward_reference_picture = VA_INVALID_ID,
  56. .vol_fields.bits = {
  57. .short_video_header = avctx->codec->id == AV_CODEC_ID_H263,
  58. .chroma_format = CHROMA_420,
  59. .interlaced = !s->progressive_sequence,
  60. .obmc_disable = 1,
  61. .sprite_enable = ctx->vol_sprite_usage,
  62. .sprite_warping_accuracy = s->sprite_warping_accuracy,
  63. .quant_type = s->mpeg_quant,
  64. .quarter_sample = s->quarter_sample,
  65. .data_partitioned = s->data_partitioning,
  66. .reversible_vlc = ctx->rvlc,
  67. .resync_marker_disable = !ctx->resync_marker,
  68. },
  69. .no_of_sprite_warping_points = ctx->num_sprite_warping_points,
  70. .quant_precision = s->quant_precision,
  71. .vop_fields.bits = {
  72. .vop_coding_type = s->pict_type - AV_PICTURE_TYPE_I,
  73. .backward_reference_vop_coding_type =
  74. s->pict_type == AV_PICTURE_TYPE_B ? s->next_picture.f->pict_type - AV_PICTURE_TYPE_I : 0,
  75. .vop_rounding_type = s->no_rounding,
  76. .intra_dc_vlc_thr = mpeg4_get_intra_dc_vlc_thr(ctx),
  77. .top_field_first = s->top_field_first,
  78. .alternate_vertical_scan_flag = s->alternate_scan,
  79. },
  80. .vop_fcode_forward = s->f_code,
  81. .vop_fcode_backward = s->b_code,
  82. .vop_time_increment_resolution = avctx->framerate.num,
  83. .num_macroblocks_in_gob = s->mb_width * H263_GOB_HEIGHT(s->height),
  84. .num_gobs_in_vop =
  85. (s->mb_width * s->mb_height) / (s->mb_width * H263_GOB_HEIGHT(s->height)),
  86. .TRB = s->pb_time,
  87. .TRD = s->pp_time,
  88. };
  89. for (i = 0; i < ctx->num_sprite_warping_points && i < 3; i++) {
  90. pic_param.sprite_trajectory_du[i] = ctx->sprite_traj[i][0];
  91. pic_param.sprite_trajectory_dv[i] = ctx->sprite_traj[i][1];
  92. }
  93. if (s->pict_type == AV_PICTURE_TYPE_B)
  94. pic_param.backward_reference_picture = ff_vaapi_get_surface_id(s->next_picture.f);
  95. if (s->pict_type != AV_PICTURE_TYPE_I)
  96. pic_param.forward_reference_picture = ff_vaapi_get_surface_id(s->last_picture.f);
  97. err = ff_vaapi_decode_make_param_buffer(avctx, pic,
  98. VAPictureParameterBufferType,
  99. &pic_param, sizeof(pic_param));
  100. if (err < 0)
  101. goto fail;
  102. /* Only the first inverse quantisation method uses the weighting matrices */
  103. if (pic_param.vol_fields.bits.quant_type) {
  104. VAIQMatrixBufferMPEG4 iq_matrix;
  105. iq_matrix.load_intra_quant_mat = 1;
  106. iq_matrix.load_non_intra_quant_mat = 1;
  107. for (i = 0; i < 64; i++) {
  108. int n = s->idsp.idct_permutation[ff_zigzag_direct[i]];
  109. iq_matrix.intra_quant_mat[i] = s->intra_matrix[n];
  110. iq_matrix.non_intra_quant_mat[i] = s->inter_matrix[n];
  111. }
  112. err = ff_vaapi_decode_make_param_buffer(avctx, pic,
  113. VAIQMatrixBufferType,
  114. &iq_matrix, sizeof(iq_matrix));
  115. if (err < 0)
  116. goto fail;
  117. }
  118. return 0;
  119. fail:
  120. ff_vaapi_decode_cancel(avctx, pic);
  121. return err;
  122. }
  123. static int vaapi_mpeg4_end_frame(AVCodecContext *avctx)
  124. {
  125. MpegEncContext *s = avctx->priv_data;
  126. VAAPIDecodePicture *pic = s->current_picture_ptr->hwaccel_picture_private;
  127. int ret;
  128. ret = ff_vaapi_decode_issue(avctx, pic);
  129. if (ret < 0)
  130. goto fail;
  131. ff_mpeg_draw_horiz_band(s, 0, s->avctx->height);
  132. fail:
  133. return ret;
  134. }
  135. static int vaapi_mpeg4_decode_slice(AVCodecContext *avctx, const uint8_t *buffer, uint32_t size)
  136. {
  137. MpegEncContext *s = avctx->priv_data;
  138. VAAPIDecodePicture *pic = s->current_picture_ptr->hwaccel_picture_private;
  139. VASliceParameterBufferMPEG4 slice_param;
  140. int err;
  141. /* video_plane_with_short_video_header() contains all GOBs
  142. * in-order, and this is what VA API (Intel backend) expects: only
  143. * a single slice param. So fake macroblock_number for Libav so
  144. * that we don't call vaapi_mpeg4_decode_slice() again
  145. */
  146. if (avctx->codec->id == AV_CODEC_ID_H263)
  147. size = s->gb.buffer_end - buffer;
  148. slice_param = (VASliceParameterBufferMPEG4) {
  149. .slice_data_size = size,
  150. .slice_data_offset = 0,
  151. .slice_data_flag = VA_SLICE_DATA_FLAG_ALL,
  152. .macroblock_offset = get_bits_count(&s->gb) % 8,
  153. .macroblock_number = s->mb_y * s->mb_width + s->mb_x,
  154. .quant_scale = s->qscale,
  155. };
  156. if (avctx->codec->id == AV_CODEC_ID_H263)
  157. s->mb_y = s->mb_height;
  158. err = ff_vaapi_decode_make_slice_buffer(avctx, pic,
  159. &slice_param, sizeof(slice_param),
  160. buffer, size);
  161. if (err < 0) {
  162. ff_vaapi_decode_cancel(avctx, pic);
  163. return err;
  164. }
  165. return 0;
  166. }
  167. #if CONFIG_MPEG4_VAAPI_HWACCEL
  168. AVHWAccel ff_mpeg4_vaapi_hwaccel = {
  169. .name = "mpeg4_vaapi",
  170. .type = AVMEDIA_TYPE_VIDEO,
  171. .id = AV_CODEC_ID_MPEG4,
  172. .pix_fmt = AV_PIX_FMT_VAAPI,
  173. .start_frame = &vaapi_mpeg4_start_frame,
  174. .end_frame = &vaapi_mpeg4_end_frame,
  175. .decode_slice = &vaapi_mpeg4_decode_slice,
  176. .frame_priv_data_size = sizeof(VAAPIDecodePicture),
  177. .init = &ff_vaapi_decode_init,
  178. .uninit = &ff_vaapi_decode_uninit,
  179. .priv_data_size = sizeof(VAAPIDecodeContext),
  180. .caps_internal = HWACCEL_CAP_ASYNC_SAFE,
  181. };
  182. #endif
  183. #if CONFIG_H263_VAAPI_HWACCEL
  184. AVHWAccel ff_h263_vaapi_hwaccel = {
  185. .name = "h263_vaapi",
  186. .type = AVMEDIA_TYPE_VIDEO,
  187. .id = AV_CODEC_ID_H263,
  188. .pix_fmt = AV_PIX_FMT_VAAPI,
  189. .start_frame = &vaapi_mpeg4_start_frame,
  190. .end_frame = &vaapi_mpeg4_end_frame,
  191. .decode_slice = &vaapi_mpeg4_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. };
  198. #endif