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.

356 lines
17KB

  1. /*
  2. * VC-1 HW decode acceleration through VA API
  3. *
  4. * Copyright (C) 2008-2009 Splitted-Desktop Systems
  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 "vaapi_internal.h"
  23. #include "vc1.h"
  24. #include "vc1data.h"
  25. /** Translates FFmpeg MV modes to VA API */
  26. static int get_VAMvModeVC1(enum MVModes mv_mode)
  27. {
  28. switch (mv_mode) {
  29. case MV_PMODE_1MV_HPEL_BILIN: return VAMvMode1MvHalfPelBilinear;
  30. case MV_PMODE_1MV: return VAMvMode1Mv;
  31. case MV_PMODE_1MV_HPEL: return VAMvMode1MvHalfPel;
  32. case MV_PMODE_MIXED_MV: return VAMvModeMixedMv;
  33. case MV_PMODE_INTENSITY_COMP: return VAMvModeIntensityCompensation;
  34. }
  35. return 0;
  36. }
  37. /** Checks whether the MVTYPEMB bitplane is present */
  38. static inline int vc1_has_MVTYPEMB_bitplane(VC1Context *v)
  39. {
  40. if (v->mv_type_is_raw)
  41. return 0;
  42. return (v->s.pict_type == FF_P_TYPE &&
  43. (v->mv_mode == MV_PMODE_MIXED_MV ||
  44. (v->mv_mode == MV_PMODE_INTENSITY_COMP &&
  45. v->mv_mode2 == MV_PMODE_MIXED_MV)));
  46. }
  47. /** Checks whether the SKIPMB bitplane is present */
  48. static inline int vc1_has_SKIPMB_bitplane(VC1Context *v)
  49. {
  50. if (v->skip_is_raw)
  51. return 0;
  52. return (v->s.pict_type == FF_P_TYPE ||
  53. (v->s.pict_type == FF_B_TYPE && !v->bi_type));
  54. }
  55. /** Checks whether the DIRECTMB bitplane is present */
  56. static inline int vc1_has_DIRECTMB_bitplane(VC1Context *v)
  57. {
  58. if (v->dmb_is_raw)
  59. return 0;
  60. return v->s.pict_type == FF_B_TYPE && !v->bi_type;
  61. }
  62. /** Checks whether the ACPRED bitplane is present */
  63. static inline int vc1_has_ACPRED_bitplane(VC1Context *v)
  64. {
  65. if (v->acpred_is_raw)
  66. return 0;
  67. return (v->profile == PROFILE_ADVANCED &&
  68. (v->s.pict_type == FF_I_TYPE ||
  69. (v->s.pict_type == FF_B_TYPE && v->bi_type)));
  70. }
  71. /** Check whether the OVERFLAGS bitplane is present */
  72. static inline int vc1_has_OVERFLAGS_bitplane(VC1Context *v)
  73. {
  74. if (v->overflg_is_raw)
  75. return 0;
  76. return (v->profile == PROFILE_ADVANCED &&
  77. (v->s.pict_type == FF_I_TYPE ||
  78. (v->s.pict_type == FF_B_TYPE && v->bi_type)) &&
  79. (v->overlap && v->pq <= 8) &&
  80. v->condover == CONDOVER_SELECT);
  81. }
  82. /** Reconstruct bitstream PTYPE (7.1.1.4, index into Table-35) */
  83. static int vc1_get_PTYPE(VC1Context *v)
  84. {
  85. MpegEncContext * const s = &v->s;
  86. switch (s->pict_type) {
  87. case FF_I_TYPE: return 0;
  88. case FF_P_TYPE: return v->p_frame_skipped ? 4 : 1;
  89. case FF_B_TYPE: return v->bi_type ? 3 : 2;
  90. }
  91. return 0;
  92. }
  93. /** Reconstruct bitstream MVMODE (7.1.1.32) */
  94. static inline VAMvModeVC1 vc1_get_MVMODE(VC1Context *v)
  95. {
  96. if (v->s.pict_type == FF_P_TYPE ||
  97. (v->s.pict_type == FF_B_TYPE && !v->bi_type))
  98. return get_VAMvModeVC1(v->mv_mode);
  99. return 0;
  100. }
  101. /** Reconstruct bitstream MVMODE2 (7.1.1.33) */
  102. static inline VAMvModeVC1 vc1_get_MVMODE2(VC1Context *v)
  103. {
  104. if (v->s.pict_type == FF_P_TYPE && v->mv_mode == MV_PMODE_INTENSITY_COMP)
  105. return get_VAMvModeVC1(v->mv_mode2);
  106. return 0;
  107. }
  108. /** Pack FFmpeg bitplanes into a VABitPlaneBuffer element */
  109. static inline uint8_t vc1_pack_bitplanes(const uint8_t *ff_bp[3], int x, int y, int stride)
  110. {
  111. const int n = y * stride + x;
  112. uint8_t v = 0;
  113. if (ff_bp[0])
  114. v = ff_bp[0][n];
  115. if (ff_bp[1])
  116. v |= ff_bp[1][n] << 1;
  117. if (ff_bp[2])
  118. v |= ff_bp[2][n] << 2;
  119. return v;
  120. }
  121. static int vaapi_vc1_start_frame(AVCodecContext *avctx, av_unused const uint8_t *buffer, av_unused uint32_t size)
  122. {
  123. VC1Context * const v = avctx->priv_data;
  124. MpegEncContext * const s = &v->s;
  125. struct vaapi_context * const vactx = avctx->hwaccel_context;
  126. VAPictureParameterBufferVC1 *pic_param;
  127. dprintf(avctx, "vaapi_vc1_start_frame()\n");
  128. vactx->slice_param_size = sizeof(VASliceParameterBufferVC1);
  129. /* Fill in VAPictureParameterBufferVC1 */
  130. pic_param = ff_vaapi_alloc_picture(vactx, sizeof(VAPictureParameterBufferVC1));
  131. if (!pic_param)
  132. return -1;
  133. pic_param->forward_reference_picture = 0xffffffff;
  134. pic_param->backward_reference_picture = 0xffffffff;
  135. pic_param->inloop_decoded_picture = 0xffffffff;
  136. pic_param->sequence_fields.value = 0; /* reset all bits */
  137. pic_param->sequence_fields.bits.pulldown = v->broadcast;
  138. pic_param->sequence_fields.bits.interlace = v->interlace;
  139. pic_param->sequence_fields.bits.tfcntrflag = v->tfcntrflag;
  140. pic_param->sequence_fields.bits.finterpflag = v->finterpflag;
  141. pic_param->sequence_fields.bits.psf = v->psf;
  142. pic_param->sequence_fields.bits.multires = v->multires;
  143. pic_param->sequence_fields.bits.overlap = v->overlap;
  144. pic_param->sequence_fields.bits.syncmarker = s->resync_marker;
  145. pic_param->sequence_fields.bits.rangered = v->rangered;
  146. pic_param->sequence_fields.bits.max_b_frames = s->avctx->max_b_frames;
  147. pic_param->coded_width = s->avctx->coded_width;
  148. pic_param->coded_height = s->avctx->coded_height;
  149. pic_param->entrypoint_fields.value = 0; /* reset all bits */
  150. pic_param->entrypoint_fields.bits.broken_link = v->broken_link;
  151. pic_param->entrypoint_fields.bits.closed_entry = v->closed_entry;
  152. pic_param->entrypoint_fields.bits.panscan_flag = v->panscanflag;
  153. pic_param->entrypoint_fields.bits.loopfilter = s->loop_filter;
  154. pic_param->conditional_overlap_flag = v->condover;
  155. pic_param->fast_uvmc_flag = v->fastuvmc;
  156. pic_param->range_mapping_fields.value = 0; /* reset all bits */
  157. pic_param->range_mapping_fields.bits.luma_flag = v->range_mapy_flag;
  158. pic_param->range_mapping_fields.bits.luma = v->range_mapy;
  159. pic_param->range_mapping_fields.bits.chroma_flag = v->range_mapuv_flag;
  160. pic_param->range_mapping_fields.bits.chroma = v->range_mapuv;
  161. pic_param->b_picture_fraction = v->bfraction_lut_index;
  162. pic_param->cbp_table = v->cbpcy_vlc ? v->cbpcy_vlc - ff_vc1_cbpcy_p_vlc : 0;
  163. pic_param->mb_mode_table = 0; /* XXX: interlaced frame */
  164. pic_param->range_reduction_frame = v->rangeredfrm;
  165. pic_param->rounding_control = v->rnd;
  166. pic_param->post_processing = v->postproc;
  167. pic_param->picture_resolution_index = v->respic;
  168. pic_param->luma_scale = v->lumscale;
  169. pic_param->luma_shift = v->lumshift;
  170. pic_param->picture_fields.value = 0; /* reset all bits */
  171. pic_param->picture_fields.bits.picture_type = vc1_get_PTYPE(v);
  172. pic_param->picture_fields.bits.frame_coding_mode = v->fcm;
  173. pic_param->picture_fields.bits.top_field_first = v->tff;
  174. pic_param->picture_fields.bits.is_first_field = v->fcm == 0; /* XXX: interlaced frame */
  175. pic_param->picture_fields.bits.intensity_compensation = v->mv_mode == MV_PMODE_INTENSITY_COMP;
  176. pic_param->raw_coding.value = 0; /* reset all bits */
  177. pic_param->raw_coding.flags.mv_type_mb = v->mv_type_is_raw;
  178. pic_param->raw_coding.flags.direct_mb = v->dmb_is_raw;
  179. pic_param->raw_coding.flags.skip_mb = v->skip_is_raw;
  180. pic_param->raw_coding.flags.field_tx = 0; /* XXX: interlaced frame */
  181. pic_param->raw_coding.flags.forward_mb = 0; /* XXX: interlaced frame */
  182. pic_param->raw_coding.flags.ac_pred = v->acpred_is_raw;
  183. pic_param->raw_coding.flags.overflags = v->overflg_is_raw;
  184. pic_param->bitplane_present.value = 0; /* reset all bits */
  185. pic_param->bitplane_present.flags.bp_mv_type_mb = vc1_has_MVTYPEMB_bitplane(v);
  186. pic_param->bitplane_present.flags.bp_direct_mb = vc1_has_DIRECTMB_bitplane(v);
  187. pic_param->bitplane_present.flags.bp_skip_mb = vc1_has_SKIPMB_bitplane(v);
  188. pic_param->bitplane_present.flags.bp_field_tx = 0; /* XXX: interlaced frame */
  189. pic_param->bitplane_present.flags.bp_forward_mb = 0; /* XXX: interlaced frame */
  190. pic_param->bitplane_present.flags.bp_ac_pred = vc1_has_ACPRED_bitplane(v);
  191. pic_param->bitplane_present.flags.bp_overflags = vc1_has_OVERFLAGS_bitplane(v);
  192. pic_param->reference_fields.value = 0; /* reset all bits */
  193. pic_param->reference_fields.bits.reference_distance_flag = v->refdist_flag;
  194. pic_param->reference_fields.bits.reference_distance = 0; /* XXX: interlaced frame */
  195. pic_param->reference_fields.bits.num_reference_pictures = 0; /* XXX: interlaced frame */
  196. pic_param->reference_fields.bits.reference_field_pic_indicator = 0; /* XXX: interlaced frame */
  197. pic_param->mv_fields.value = 0; /* reset all bits */
  198. pic_param->mv_fields.bits.mv_mode = vc1_get_MVMODE(v);
  199. pic_param->mv_fields.bits.mv_mode2 = vc1_get_MVMODE2(v);
  200. pic_param->mv_fields.bits.mv_table = s->mv_table_index;
  201. pic_param->mv_fields.bits.two_mv_block_pattern_table = 0; /* XXX: interlaced frame */
  202. pic_param->mv_fields.bits.four_mv_switch = 0; /* XXX: interlaced frame */
  203. pic_param->mv_fields.bits.four_mv_block_pattern_table = 0; /* XXX: interlaced frame */
  204. pic_param->mv_fields.bits.extended_mv_flag = v->extended_mv;
  205. pic_param->mv_fields.bits.extended_mv_range = v->mvrange;
  206. pic_param->mv_fields.bits.extended_dmv_flag = v->extended_dmv;
  207. pic_param->mv_fields.bits.extended_dmv_range = 0; /* XXX: interlaced frame */
  208. pic_param->pic_quantizer_fields.value = 0; /* reset all bits */
  209. pic_param->pic_quantizer_fields.bits.dquant = v->dquant;
  210. pic_param->pic_quantizer_fields.bits.quantizer = v->quantizer_mode;
  211. pic_param->pic_quantizer_fields.bits.half_qp = v->halfpq;
  212. pic_param->pic_quantizer_fields.bits.pic_quantizer_scale = v->pq;
  213. pic_param->pic_quantizer_fields.bits.pic_quantizer_type = v->pquantizer;
  214. pic_param->pic_quantizer_fields.bits.dq_frame = v->dquantfrm;
  215. pic_param->pic_quantizer_fields.bits.dq_profile = v->dqprofile;
  216. pic_param->pic_quantizer_fields.bits.dq_sb_edge = v->dqprofile == DQPROFILE_SINGLE_EDGE ? v->dqsbedge : 0;
  217. pic_param->pic_quantizer_fields.bits.dq_db_edge = v->dqprofile == DQPROFILE_DOUBLE_EDGES ? v->dqsbedge : 0;
  218. pic_param->pic_quantizer_fields.bits.dq_binary_level = v->dqbilevel;
  219. pic_param->pic_quantizer_fields.bits.alt_pic_quantizer = v->altpq;
  220. pic_param->transform_fields.value = 0; /* reset all bits */
  221. pic_param->transform_fields.bits.variable_sized_transform_flag = v->vstransform;
  222. pic_param->transform_fields.bits.mb_level_transform_type_flag = v->ttmbf;
  223. pic_param->transform_fields.bits.frame_level_transform_type = v->ttfrm;
  224. pic_param->transform_fields.bits.transform_ac_codingset_idx1 = v->c_ac_table_index;
  225. pic_param->transform_fields.bits.transform_ac_codingset_idx2 = v->y_ac_table_index;
  226. pic_param->transform_fields.bits.intra_transform_dc_table = v->s.dc_table_index;
  227. switch (s->pict_type) {
  228. case FF_B_TYPE:
  229. pic_param->backward_reference_picture = ff_vaapi_get_surface(&s->next_picture);
  230. // fall-through
  231. case FF_P_TYPE:
  232. pic_param->forward_reference_picture = ff_vaapi_get_surface(&s->last_picture);
  233. break;
  234. }
  235. if (pic_param->bitplane_present.value) {
  236. uint8_t *bitplane;
  237. const uint8_t *ff_bp[3];
  238. int x, y, n;
  239. switch (s->pict_type) {
  240. case FF_P_TYPE:
  241. ff_bp[0] = pic_param->bitplane_present.flags.bp_direct_mb ? v->direct_mb_plane : NULL;
  242. ff_bp[1] = pic_param->bitplane_present.flags.bp_skip_mb ? s->mbskip_table : NULL;
  243. ff_bp[2] = pic_param->bitplane_present.flags.bp_mv_type_mb ? v->mv_type_mb_plane : NULL;
  244. break;
  245. case FF_B_TYPE:
  246. if (!v->bi_type) {
  247. ff_bp[0] = pic_param->bitplane_present.flags.bp_direct_mb ? v->direct_mb_plane : NULL;
  248. ff_bp[1] = pic_param->bitplane_present.flags.bp_skip_mb ? s->mbskip_table : NULL;
  249. ff_bp[2] = NULL; /* XXX: interlaced frame (FORWARD plane) */
  250. break;
  251. }
  252. /* fall-through (BI-type) */
  253. case FF_I_TYPE:
  254. ff_bp[0] = NULL; /* XXX: interlaced frame (FIELDTX plane) */
  255. ff_bp[1] = pic_param->bitplane_present.flags.bp_ac_pred ? v->acpred_plane : NULL;
  256. ff_bp[2] = pic_param->bitplane_present.flags.bp_overflags ? v->over_flags_plane : NULL;
  257. break;
  258. default:
  259. ff_bp[0] = NULL;
  260. ff_bp[1] = NULL;
  261. ff_bp[2] = NULL;
  262. break;
  263. }
  264. bitplane = ff_vaapi_alloc_bitplane(vactx, s->mb_height * ((s->mb_width + 1) / 2));
  265. if (!bitplane)
  266. return -1;
  267. n = 0;
  268. for (y = 0; y < s->mb_height; y++) {
  269. for (x = 0; x < s->mb_width; x += 2) {
  270. bitplane[n] = vc1_pack_bitplanes(ff_bp, x+1, y, s->mb_stride);
  271. bitplane[n] |= (vc1_pack_bitplanes(ff_bp, x, y, s->mb_stride) << 4);
  272. ++n;
  273. }
  274. }
  275. }
  276. return 0;
  277. }
  278. static int vaapi_vc1_end_frame(AVCodecContext *avctx)
  279. {
  280. VC1Context * const v = avctx->priv_data;
  281. return ff_vaapi_common_end_frame(&v->s);
  282. }
  283. static int vaapi_vc1_decode_slice(AVCodecContext *avctx, const uint8_t *buffer, uint32_t size)
  284. {
  285. VC1Context * const v = avctx->priv_data;
  286. MpegEncContext * const s = &v->s;
  287. VASliceParameterBufferVC1 *slice_param;
  288. dprintf(avctx, "vaapi_vc1_decode_slice(): buffer %p, size %d\n", buffer, size);
  289. /* Current bit buffer is beyond any marker for VC-1, so skip it */
  290. if (avctx->codec_id == CODEC_ID_VC1 && IS_MARKER(AV_RB32(buffer))) {
  291. buffer += 4;
  292. size -= 4;
  293. }
  294. /* Fill in VASliceParameterBufferVC1 */
  295. slice_param = (VASliceParameterBufferVC1 *)ff_vaapi_alloc_slice(avctx->hwaccel_context, buffer, size);
  296. if (!slice_param)
  297. return -1;
  298. slice_param->macroblock_offset = get_bits_count(&s->gb);
  299. slice_param->slice_vertical_position = s->mb_y;
  300. return 0;
  301. }
  302. #if CONFIG_WMV3_VAAPI_HWACCEL
  303. AVHWAccel wmv3_vaapi_hwaccel = {
  304. .name = "wmv3_vaapi",
  305. .type = CODEC_TYPE_VIDEO,
  306. .id = CODEC_ID_WMV3,
  307. .pix_fmt = PIX_FMT_VAAPI_VLD,
  308. .capabilities = 0,
  309. .start_frame = vaapi_vc1_start_frame,
  310. .end_frame = vaapi_vc1_end_frame,
  311. .decode_slice = vaapi_vc1_decode_slice,
  312. .priv_data_size = 0,
  313. };
  314. #endif
  315. #if CONFIG_VC1_VAAPI_HWACCEL
  316. AVHWAccel vc1_vaapi_hwaccel = {
  317. .name = "vc1_vaapi",
  318. .type = CODEC_TYPE_VIDEO,
  319. .id = CODEC_ID_VC1,
  320. .pix_fmt = PIX_FMT_VAAPI_VLD,
  321. .capabilities = 0,
  322. .start_frame = vaapi_vc1_start_frame,
  323. .end_frame = vaapi_vc1_end_frame,
  324. .decode_slice = vaapi_vc1_decode_slice,
  325. .priv_data_size = 0,
  326. };
  327. #endif