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.

418 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 "internal.h"
  23. #include "vaapi_decode.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(const 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(const 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(const 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(const 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(const 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(const VC1Context *v)
  85. {
  86. const MpegEncContext *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(const 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(const 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(const 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. const VC1Context *v = avctx->priv_data;
  137. const MpegEncContext *s = &v->s;
  138. VAAPIDecodePicture *pic = s->current_picture_ptr->hwaccel_picture_private;
  139. VAPictureParameterBufferVC1 pic_param;
  140. int err;
  141. pic->output_surface = ff_vaapi_get_surface_id(s->current_picture_ptr->f);
  142. pic_param = (VAPictureParameterBufferVC1) {
  143. .forward_reference_picture = VA_INVALID_ID,
  144. .backward_reference_picture = VA_INVALID_ID,
  145. .inloop_decoded_picture = VA_INVALID_ID,
  146. .sequence_fields.bits = {
  147. .pulldown = v->broadcast,
  148. .interlace = v->interlace,
  149. .tfcntrflag = v->tfcntrflag,
  150. .finterpflag = v->finterpflag,
  151. .psf = v->psf,
  152. .multires = v->multires,
  153. .overlap = v->overlap,
  154. .syncmarker = v->resync_marker,
  155. .rangered = v->rangered,
  156. .max_b_frames = s->avctx->max_b_frames,
  157. .profile = v->profile,
  158. },
  159. .coded_width = s->avctx->coded_width,
  160. .coded_height = s->avctx->coded_height,
  161. .entrypoint_fields.bits = {
  162. .broken_link = v->broken_link,
  163. .closed_entry = v->closed_entry,
  164. .panscan_flag = v->panscanflag,
  165. .loopfilter = s->loop_filter,
  166. },
  167. .conditional_overlap_flag = v->condover,
  168. .fast_uvmc_flag = v->fastuvmc,
  169. .range_mapping_fields.bits = {
  170. .luma_flag = v->range_mapy_flag,
  171. .luma = v->range_mapy,
  172. .chroma_flag = v->range_mapuv_flag,
  173. .chroma = v->range_mapuv,
  174. },
  175. .b_picture_fraction = v->bfraction_lut_index,
  176. .cbp_table = v->cbpcy_vlc ? v->cbpcy_vlc - ff_vc1_cbpcy_p_vlc : 0,
  177. .mb_mode_table = 0, /* XXX: interlaced frame */
  178. .range_reduction_frame = v->rangeredfrm,
  179. .rounding_control = v->rnd,
  180. .post_processing = v->postproc,
  181. .picture_resolution_index = v->respic,
  182. .luma_scale = v->lumscale,
  183. .luma_shift = v->lumshift,
  184. .picture_fields.bits = {
  185. .picture_type = vc1_get_PTYPE(v),
  186. .frame_coding_mode = v->fcm,
  187. .top_field_first = v->tff,
  188. .is_first_field = v->fcm == 0, /* XXX: interlaced frame */
  189. .intensity_compensation = v->mv_mode == MV_PMODE_INTENSITY_COMP,
  190. },
  191. .raw_coding.flags = {
  192. .mv_type_mb = v->mv_type_is_raw,
  193. .direct_mb = v->dmb_is_raw,
  194. .skip_mb = v->skip_is_raw,
  195. .field_tx = 0, /* XXX: interlaced frame */
  196. .forward_mb = 0, /* XXX: interlaced frame */
  197. .ac_pred = v->acpred_is_raw,
  198. .overflags = v->overflg_is_raw,
  199. },
  200. .bitplane_present.flags = {
  201. .bp_mv_type_mb = vc1_has_MVTYPEMB_bitplane(v),
  202. .bp_direct_mb = vc1_has_DIRECTMB_bitplane(v),
  203. .bp_skip_mb = vc1_has_SKIPMB_bitplane(v),
  204. .bp_field_tx = 0, /* XXX: interlaced frame */
  205. .bp_forward_mb = 0, /* XXX: interlaced frame */
  206. .bp_ac_pred = vc1_has_ACPRED_bitplane(v),
  207. .bp_overflags = vc1_has_OVERFLAGS_bitplane(v),
  208. },
  209. .reference_fields.bits = {
  210. .reference_distance_flag = v->refdist_flag,
  211. .reference_distance = 0, /* XXX: interlaced frame */
  212. .num_reference_pictures = 0, /* XXX: interlaced frame */
  213. .reference_field_pic_indicator = 0, /* XXX: interlaced frame */
  214. },
  215. .mv_fields.bits = {
  216. .mv_mode = vc1_get_MVMODE(v),
  217. .mv_mode2 = vc1_get_MVMODE2(v),
  218. .mv_table = s->mv_table_index,
  219. .two_mv_block_pattern_table = 0, /* XXX: interlaced frame */
  220. .four_mv_switch = 0, /* XXX: interlaced frame */
  221. .four_mv_block_pattern_table = 0, /* XXX: interlaced frame */
  222. .extended_mv_flag = v->extended_mv,
  223. .extended_mv_range = v->mvrange,
  224. .extended_dmv_flag = v->extended_dmv,
  225. .extended_dmv_range = 0, /* XXX: interlaced frame */
  226. },
  227. .pic_quantizer_fields.bits = {
  228. .dquant = v->dquant,
  229. .quantizer = v->quantizer_mode,
  230. .half_qp = v->halfpq,
  231. .pic_quantizer_scale = v->pq,
  232. .pic_quantizer_type = v->pquantizer,
  233. .dq_frame = v->dquantfrm,
  234. .dq_profile = v->dqprofile,
  235. .dq_sb_edge = v->dqprofile == DQPROFILE_SINGLE_EDGE ? v->dqsbedge : 0,
  236. .dq_db_edge = v->dqprofile == DQPROFILE_DOUBLE_EDGES ? v->dqsbedge : 0,
  237. .dq_binary_level = v->dqbilevel,
  238. .alt_pic_quantizer = v->altpq,
  239. },
  240. .transform_fields.bits = {
  241. .variable_sized_transform_flag = v->vstransform,
  242. .mb_level_transform_type_flag = v->ttmbf,
  243. .frame_level_transform_type = vc1_get_TTFRM(v),
  244. .transform_ac_codingset_idx1 = v->c_ac_table_index,
  245. .transform_ac_codingset_idx2 = v->y_ac_table_index,
  246. .intra_transform_dc_table = v->s.dc_table_index,
  247. },
  248. };
  249. switch (s->pict_type) {
  250. case AV_PICTURE_TYPE_B:
  251. pic_param.backward_reference_picture = ff_vaapi_get_surface_id(s->next_picture.f);
  252. // fall-through
  253. case AV_PICTURE_TYPE_P:
  254. pic_param.forward_reference_picture = ff_vaapi_get_surface_id(s->last_picture.f);
  255. break;
  256. }
  257. err = ff_vaapi_decode_make_param_buffer(avctx, pic,
  258. VAPictureParameterBufferType,
  259. &pic_param, sizeof(pic_param));
  260. if (err)
  261. goto fail;
  262. if (pic_param.bitplane_present.value) {
  263. uint8_t *bitplane;
  264. const uint8_t *ff_bp[3];
  265. int x, y, n;
  266. size_t size = (s->mb_width * s->mb_height + 1) / 2;
  267. bitplane = av_mallocz(size);
  268. if (!bitplane) {
  269. err = AVERROR(ENOMEM);
  270. goto fail;
  271. }
  272. switch (s->pict_type) {
  273. case AV_PICTURE_TYPE_P:
  274. ff_bp[0] = pic_param.bitplane_present.flags.bp_direct_mb ? v->direct_mb_plane : NULL;
  275. ff_bp[1] = pic_param.bitplane_present.flags.bp_skip_mb ? s->mbskip_table : NULL;
  276. ff_bp[2] = pic_param.bitplane_present.flags.bp_mv_type_mb ? v->mv_type_mb_plane : NULL;
  277. break;
  278. case AV_PICTURE_TYPE_B:
  279. if (!v->bi_type) {
  280. ff_bp[0] = pic_param.bitplane_present.flags.bp_direct_mb ? v->direct_mb_plane : NULL;
  281. ff_bp[1] = pic_param.bitplane_present.flags.bp_skip_mb ? s->mbskip_table : NULL;
  282. ff_bp[2] = NULL; /* XXX: interlaced frame (FORWARD plane) */
  283. break;
  284. }
  285. /* fall-through (BI-type) */
  286. case AV_PICTURE_TYPE_I:
  287. ff_bp[0] = NULL; /* XXX: interlaced frame (FIELDTX plane) */
  288. ff_bp[1] = pic_param.bitplane_present.flags.bp_ac_pred ? v->acpred_plane : NULL;
  289. ff_bp[2] = pic_param.bitplane_present.flags.bp_overflags ? v->over_flags_plane : NULL;
  290. break;
  291. default:
  292. ff_bp[0] = NULL;
  293. ff_bp[1] = NULL;
  294. ff_bp[2] = NULL;
  295. break;
  296. }
  297. n = 0;
  298. for (y = 0; y < s->mb_height; y++)
  299. for (x = 0; x < s->mb_width; x++, n++)
  300. vc1_pack_bitplanes(bitplane, n, ff_bp, x, y, s->mb_stride);
  301. if (n & 1) /* move last nibble to the high order */
  302. bitplane[n/2] <<= 4;
  303. err = ff_vaapi_decode_make_param_buffer(avctx, pic,
  304. VABitPlaneBufferType,
  305. bitplane, size);
  306. av_free(bitplane);
  307. if (err)
  308. goto fail;
  309. }
  310. return 0;
  311. fail:
  312. ff_vaapi_decode_cancel(avctx, pic);
  313. return err;
  314. }
  315. static int vaapi_vc1_end_frame(AVCodecContext *avctx)
  316. {
  317. VC1Context *v = avctx->priv_data;
  318. MpegEncContext *s = &v->s;
  319. VAAPIDecodePicture *pic = s->current_picture_ptr->hwaccel_picture_private;
  320. int ret;
  321. ret = ff_vaapi_decode_issue(avctx, pic);
  322. if (ret < 0)
  323. goto fail;
  324. ff_mpeg_draw_horiz_band(s, 0, s->avctx->height);
  325. fail:
  326. return ret;
  327. }
  328. static int vaapi_vc1_decode_slice(AVCodecContext *avctx, const uint8_t *buffer, uint32_t size)
  329. {
  330. const VC1Context *v = avctx->priv_data;
  331. const MpegEncContext *s = &v->s;
  332. VAAPIDecodePicture *pic = s->current_picture_ptr->hwaccel_picture_private;
  333. VASliceParameterBufferVC1 slice_param;
  334. int err;
  335. /* Current bit buffer is beyond any marker for VC-1, so skip it */
  336. if (avctx->codec_id == AV_CODEC_ID_VC1 && IS_MARKER(AV_RB32(buffer))) {
  337. buffer += 4;
  338. size -= 4;
  339. }
  340. slice_param = (VASliceParameterBufferVC1) {
  341. .slice_data_size = size,
  342. .slice_data_offset = 0,
  343. .slice_data_flag = VA_SLICE_DATA_FLAG_ALL,
  344. .macroblock_offset = get_bits_count(&s->gb),
  345. .slice_vertical_position = s->mb_y,
  346. };
  347. err = ff_vaapi_decode_make_slice_buffer(avctx, pic,
  348. &slice_param, sizeof(slice_param),
  349. buffer, size);
  350. if (err < 0) {
  351. ff_vaapi_decode_cancel(avctx, pic);
  352. return err;
  353. }
  354. return 0;
  355. }
  356. #if CONFIG_WMV3_VAAPI_HWACCEL
  357. AVHWAccel ff_wmv3_vaapi_hwaccel = {
  358. .name = "wmv3_vaapi",
  359. .type = AVMEDIA_TYPE_VIDEO,
  360. .id = AV_CODEC_ID_WMV3,
  361. .pix_fmt = AV_PIX_FMT_VAAPI,
  362. .start_frame = &vaapi_vc1_start_frame,
  363. .end_frame = &vaapi_vc1_end_frame,
  364. .decode_slice = &vaapi_vc1_decode_slice,
  365. .frame_priv_data_size = sizeof(VAAPIDecodePicture),
  366. .init = &ff_vaapi_decode_init,
  367. .uninit = &ff_vaapi_decode_uninit,
  368. .priv_data_size = sizeof(VAAPIDecodeContext),
  369. };
  370. #endif
  371. AVHWAccel ff_vc1_vaapi_hwaccel = {
  372. .name = "vc1_vaapi",
  373. .type = AVMEDIA_TYPE_VIDEO,
  374. .id = AV_CODEC_ID_VC1,
  375. .pix_fmt = AV_PIX_FMT_VAAPI,
  376. .start_frame = &vaapi_vc1_start_frame,
  377. .end_frame = &vaapi_vc1_end_frame,
  378. .decode_slice = &vaapi_vc1_decode_slice,
  379. .frame_priv_data_size = sizeof(VAAPIDecodePicture),
  380. .init = &ff_vaapi_decode_init,
  381. .uninit = &ff_vaapi_decode_uninit,
  382. .priv_data_size = sizeof(VAAPIDecodeContext),
  383. };