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.

221 lines
8.4KB

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