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.

353 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. /** Translate 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. /** Check 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. /** Check 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. /** Check 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. /** Check 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 void vc1_pack_bitplanes(uint8_t *bitplane, int n, const uint8_t *ff_bp[3], int x, int y, int stride)
  110. {
  111. const int bitplane_index = n / 2;
  112. const int ff_bp_index = y * stride + x;
  113. uint8_t v = 0;
  114. if (ff_bp[0])
  115. v = ff_bp[0][ff_bp_index];
  116. if (ff_bp[1])
  117. v |= ff_bp[1][ff_bp_index] << 1;
  118. if (ff_bp[2])
  119. v |= ff_bp[2][ff_bp_index] << 2;
  120. bitplane[bitplane_index] = (bitplane[bitplane_index] << 4) | v;
  121. }
  122. static int vaapi_vc1_start_frame(AVCodecContext *avctx, av_unused const uint8_t *buffer, av_unused uint32_t size)
  123. {
  124. VC1Context * const v = avctx->priv_data;
  125. MpegEncContext * const s = &v->s;
  126. struct vaapi_context * const vactx = avctx->hwaccel_context;
  127. VAPictureParameterBufferVC1 *pic_param;
  128. dprintf(avctx, "vaapi_vc1_start_frame()\n");
  129. vactx->slice_param_size = sizeof(VASliceParameterBufferVC1);
  130. /* Fill in VAPictureParameterBufferVC1 */
  131. pic_param = ff_vaapi_alloc_pic_param(vactx, sizeof(VAPictureParameterBufferVC1));
  132. if (!pic_param)
  133. return -1;
  134. pic_param->forward_reference_picture = VA_INVALID_ID;
  135. pic_param->backward_reference_picture = VA_INVALID_ID;
  136. pic_param->inloop_decoded_picture = VA_INVALID_ID;
  137. pic_param->sequence_fields.value = 0; /* reset all bits */
  138. pic_param->sequence_fields.bits.pulldown = v->broadcast;
  139. pic_param->sequence_fields.bits.interlace = v->interlace;
  140. pic_param->sequence_fields.bits.tfcntrflag = v->tfcntrflag;
  141. pic_param->sequence_fields.bits.finterpflag = v->finterpflag;
  142. pic_param->sequence_fields.bits.psf = v->psf;
  143. pic_param->sequence_fields.bits.multires = v->multires;
  144. pic_param->sequence_fields.bits.overlap = v->overlap;
  145. pic_param->sequence_fields.bits.syncmarker = s->resync_marker;
  146. pic_param->sequence_fields.bits.rangered = v->rangered;
  147. pic_param->sequence_fields.bits.max_b_frames = s->avctx->max_b_frames;
  148. pic_param->coded_width = s->avctx->coded_width;
  149. pic_param->coded_height = s->avctx->coded_height;
  150. pic_param->entrypoint_fields.value = 0; /* reset all bits */
  151. pic_param->entrypoint_fields.bits.broken_link = v->broken_link;
  152. pic_param->entrypoint_fields.bits.closed_entry = v->closed_entry;
  153. pic_param->entrypoint_fields.bits.panscan_flag = v->panscanflag;
  154. pic_param->entrypoint_fields.bits.loopfilter = s->loop_filter;
  155. pic_param->conditional_overlap_flag = v->condover;
  156. pic_param->fast_uvmc_flag = v->fastuvmc;
  157. pic_param->range_mapping_fields.value = 0; /* reset all bits */
  158. pic_param->range_mapping_fields.bits.luma_flag = v->range_mapy_flag;
  159. pic_param->range_mapping_fields.bits.luma = v->range_mapy;
  160. pic_param->range_mapping_fields.bits.chroma_flag = v->range_mapuv_flag;
  161. pic_param->range_mapping_fields.bits.chroma = v->range_mapuv;
  162. pic_param->b_picture_fraction = v->bfraction_lut_index;
  163. pic_param->cbp_table = v->cbpcy_vlc ? v->cbpcy_vlc - ff_vc1_cbpcy_p_vlc : 0;
  164. pic_param->mb_mode_table = 0; /* XXX: interlaced frame */
  165. pic_param->range_reduction_frame = v->rangeredfrm;
  166. pic_param->rounding_control = v->rnd;
  167. pic_param->post_processing = v->postproc;
  168. pic_param->picture_resolution_index = v->respic;
  169. pic_param->luma_scale = v->lumscale;
  170. pic_param->luma_shift = v->lumshift;
  171. pic_param->picture_fields.value = 0; /* reset all bits */
  172. pic_param->picture_fields.bits.picture_type = vc1_get_PTYPE(v);
  173. pic_param->picture_fields.bits.frame_coding_mode = v->fcm;
  174. pic_param->picture_fields.bits.top_field_first = v->tff;
  175. pic_param->picture_fields.bits.is_first_field = v->fcm == 0; /* XXX: interlaced frame */
  176. pic_param->picture_fields.bits.intensity_compensation = v->mv_mode == MV_PMODE_INTENSITY_COMP;
  177. pic_param->raw_coding.value = 0; /* reset all bits */
  178. pic_param->raw_coding.flags.mv_type_mb = v->mv_type_is_raw;
  179. pic_param->raw_coding.flags.direct_mb = v->dmb_is_raw;
  180. pic_param->raw_coding.flags.skip_mb = v->skip_is_raw;
  181. pic_param->raw_coding.flags.field_tx = 0; /* XXX: interlaced frame */
  182. pic_param->raw_coding.flags.forward_mb = 0; /* XXX: interlaced frame */
  183. pic_param->raw_coding.flags.ac_pred = v->acpred_is_raw;
  184. pic_param->raw_coding.flags.overflags = v->overflg_is_raw;
  185. pic_param->bitplane_present.value = 0; /* reset all bits */
  186. pic_param->bitplane_present.flags.bp_mv_type_mb = vc1_has_MVTYPEMB_bitplane(v);
  187. pic_param->bitplane_present.flags.bp_direct_mb = vc1_has_DIRECTMB_bitplane(v);
  188. pic_param->bitplane_present.flags.bp_skip_mb = vc1_has_SKIPMB_bitplane(v);
  189. pic_param->bitplane_present.flags.bp_field_tx = 0; /* XXX: interlaced frame */
  190. pic_param->bitplane_present.flags.bp_forward_mb = 0; /* XXX: interlaced frame */
  191. pic_param->bitplane_present.flags.bp_ac_pred = vc1_has_ACPRED_bitplane(v);
  192. pic_param->bitplane_present.flags.bp_overflags = vc1_has_OVERFLAGS_bitplane(v);
  193. pic_param->reference_fields.value = 0; /* reset all bits */
  194. pic_param->reference_fields.bits.reference_distance_flag = v->refdist_flag;
  195. pic_param->reference_fields.bits.reference_distance = 0; /* XXX: interlaced frame */
  196. pic_param->reference_fields.bits.num_reference_pictures = 0; /* XXX: interlaced frame */
  197. pic_param->reference_fields.bits.reference_field_pic_indicator = 0; /* XXX: interlaced frame */
  198. pic_param->mv_fields.value = 0; /* reset all bits */
  199. pic_param->mv_fields.bits.mv_mode = vc1_get_MVMODE(v);
  200. pic_param->mv_fields.bits.mv_mode2 = vc1_get_MVMODE2(v);
  201. pic_param->mv_fields.bits.mv_table = s->mv_table_index;
  202. pic_param->mv_fields.bits.two_mv_block_pattern_table = 0; /* XXX: interlaced frame */
  203. pic_param->mv_fields.bits.four_mv_switch = 0; /* XXX: interlaced frame */
  204. pic_param->mv_fields.bits.four_mv_block_pattern_table = 0; /* XXX: interlaced frame */
  205. pic_param->mv_fields.bits.extended_mv_flag = v->extended_mv;
  206. pic_param->mv_fields.bits.extended_mv_range = v->mvrange;
  207. pic_param->mv_fields.bits.extended_dmv_flag = v->extended_dmv;
  208. pic_param->mv_fields.bits.extended_dmv_range = 0; /* XXX: interlaced frame */
  209. pic_param->pic_quantizer_fields.value = 0; /* reset all bits */
  210. pic_param->pic_quantizer_fields.bits.dquant = v->dquant;
  211. pic_param->pic_quantizer_fields.bits.quantizer = v->quantizer_mode;
  212. pic_param->pic_quantizer_fields.bits.half_qp = v->halfpq;
  213. pic_param->pic_quantizer_fields.bits.pic_quantizer_scale = v->pq;
  214. pic_param->pic_quantizer_fields.bits.pic_quantizer_type = v->pquantizer;
  215. pic_param->pic_quantizer_fields.bits.dq_frame = v->dquantfrm;
  216. pic_param->pic_quantizer_fields.bits.dq_profile = v->dqprofile;
  217. pic_param->pic_quantizer_fields.bits.dq_sb_edge = v->dqprofile == DQPROFILE_SINGLE_EDGE ? v->dqsbedge : 0;
  218. pic_param->pic_quantizer_fields.bits.dq_db_edge = v->dqprofile == DQPROFILE_DOUBLE_EDGES ? v->dqsbedge : 0;
  219. pic_param->pic_quantizer_fields.bits.dq_binary_level = v->dqbilevel;
  220. pic_param->pic_quantizer_fields.bits.alt_pic_quantizer = v->altpq;
  221. pic_param->transform_fields.value = 0; /* reset all bits */
  222. pic_param->transform_fields.bits.variable_sized_transform_flag = v->vstransform;
  223. pic_param->transform_fields.bits.mb_level_transform_type_flag = v->ttmbf;
  224. pic_param->transform_fields.bits.frame_level_transform_type = v->ttfrm;
  225. pic_param->transform_fields.bits.transform_ac_codingset_idx1 = v->c_ac_table_index;
  226. pic_param->transform_fields.bits.transform_ac_codingset_idx2 = v->y_ac_table_index;
  227. pic_param->transform_fields.bits.intra_transform_dc_table = v->s.dc_table_index;
  228. switch (s->pict_type) {
  229. case FF_B_TYPE:
  230. pic_param->backward_reference_picture = ff_vaapi_get_surface_id(&s->next_picture);
  231. // fall-through
  232. case FF_P_TYPE:
  233. pic_param->forward_reference_picture = ff_vaapi_get_surface_id(&s->last_picture);
  234. break;
  235. }
  236. if (pic_param->bitplane_present.value) {
  237. uint8_t *bitplane;
  238. const uint8_t *ff_bp[3];
  239. int x, y, n;
  240. switch (s->pict_type) {
  241. case FF_P_TYPE:
  242. ff_bp[0] = pic_param->bitplane_present.flags.bp_direct_mb ? v->direct_mb_plane : NULL;
  243. ff_bp[1] = pic_param->bitplane_present.flags.bp_skip_mb ? s->mbskip_table : NULL;
  244. ff_bp[2] = pic_param->bitplane_present.flags.bp_mv_type_mb ? v->mv_type_mb_plane : NULL;
  245. break;
  246. case FF_B_TYPE:
  247. if (!v->bi_type) {
  248. ff_bp[0] = pic_param->bitplane_present.flags.bp_direct_mb ? v->direct_mb_plane : NULL;
  249. ff_bp[1] = pic_param->bitplane_present.flags.bp_skip_mb ? s->mbskip_table : NULL;
  250. ff_bp[2] = NULL; /* XXX: interlaced frame (FORWARD plane) */
  251. break;
  252. }
  253. /* fall-through (BI-type) */
  254. case FF_I_TYPE:
  255. ff_bp[0] = NULL; /* XXX: interlaced frame (FIELDTX plane) */
  256. ff_bp[1] = pic_param->bitplane_present.flags.bp_ac_pred ? v->acpred_plane : NULL;
  257. ff_bp[2] = pic_param->bitplane_present.flags.bp_overflags ? v->over_flags_plane : NULL;
  258. break;
  259. default:
  260. ff_bp[0] = NULL;
  261. ff_bp[1] = NULL;
  262. ff_bp[2] = NULL;
  263. break;
  264. }
  265. bitplane = ff_vaapi_alloc_bitplane(vactx, (s->mb_width * s->mb_height + 1) / 2);
  266. if (!bitplane)
  267. return -1;
  268. n = 0;
  269. for (y = 0; y < s->mb_height; y++)
  270. for (x = 0; x < s->mb_width; x++, n++)
  271. vc1_pack_bitplanes(bitplane, n, ff_bp, x, y, s->mb_stride);
  272. if (n & 1) /* move last nibble to the high order */
  273. bitplane[n/2] <<= 4;
  274. }
  275. return 0;
  276. }
  277. static int vaapi_vc1_end_frame(AVCodecContext *avctx)
  278. {
  279. VC1Context * const v = avctx->priv_data;
  280. return ff_vaapi_common_end_frame(&v->s);
  281. }
  282. static int vaapi_vc1_decode_slice(AVCodecContext *avctx, const uint8_t *buffer, uint32_t size)
  283. {
  284. VC1Context * const v = avctx->priv_data;
  285. MpegEncContext * const s = &v->s;
  286. VASliceParameterBufferVC1 *slice_param;
  287. dprintf(avctx, "vaapi_vc1_decode_slice(): buffer %p, size %d\n", buffer, size);
  288. /* Current bit buffer is beyond any marker for VC-1, so skip it */
  289. if (avctx->codec_id == CODEC_ID_VC1 && IS_MARKER(AV_RB32(buffer))) {
  290. buffer += 4;
  291. size -= 4;
  292. }
  293. /* Fill in VASliceParameterBufferVC1 */
  294. slice_param = (VASliceParameterBufferVC1 *)ff_vaapi_alloc_slice(avctx->hwaccel_context, buffer, size);
  295. if (!slice_param)
  296. return -1;
  297. slice_param->macroblock_offset = get_bits_count(&s->gb);
  298. slice_param->slice_vertical_position = s->mb_y;
  299. return 0;
  300. }
  301. #if CONFIG_WMV3_VAAPI_HWACCEL
  302. AVHWAccel wmv3_vaapi_hwaccel = {
  303. .name = "wmv3_vaapi",
  304. .type = AVMEDIA_TYPE_VIDEO,
  305. .id = CODEC_ID_WMV3,
  306. .pix_fmt = PIX_FMT_VAAPI_VLD,
  307. .capabilities = 0,
  308. .start_frame = vaapi_vc1_start_frame,
  309. .end_frame = vaapi_vc1_end_frame,
  310. .decode_slice = vaapi_vc1_decode_slice,
  311. .priv_data_size = 0,
  312. };
  313. #endif
  314. AVHWAccel vc1_vaapi_hwaccel = {
  315. .name = "vc1_vaapi",
  316. .type = AVMEDIA_TYPE_VIDEO,
  317. .id = CODEC_ID_VC1,
  318. .pix_fmt = PIX_FMT_VAAPI_VLD,
  319. .capabilities = 0,
  320. .start_frame = vaapi_vc1_start_frame,
  321. .end_frame = vaapi_vc1_end_frame,
  322. .decode_slice = vaapi_vc1_decode_slice,
  323. .priv_data_size = 0,
  324. };