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.

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