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.

421 lines
16KB

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