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.

296 lines
12KB

  1. /*
  2. * DXVA2 WMV3/VC-1 HW acceleration.
  3. *
  4. * copyright (c) 2010 Laurent Aimar
  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 "dxva2_internal.h"
  23. #include "mpegutils.h"
  24. #include "vc1.h"
  25. #include "vc1data.h"
  26. struct dxva2_picture_context {
  27. DXVA_PictureParameters pp;
  28. DXVA_SliceInfo si;
  29. const uint8_t *bitstream;
  30. unsigned bitstream_size;
  31. };
  32. static void fill_picture_parameters(AVCodecContext *avctx,
  33. struct dxva_context *ctx, const VC1Context *v,
  34. DXVA_PictureParameters *pp)
  35. {
  36. const MpegEncContext *s = &v->s;
  37. const Picture *current_picture = s->current_picture_ptr;
  38. memset(pp, 0, sizeof(*pp));
  39. pp->wDecodedPictureIndex =
  40. pp->wDeblockedPictureIndex = ff_dxva2_get_surface_index(ctx, current_picture->f);
  41. if (s->pict_type != AV_PICTURE_TYPE_I && !v->bi_type)
  42. pp->wForwardRefPictureIndex = ff_dxva2_get_surface_index(ctx, s->last_picture.f);
  43. else
  44. pp->wForwardRefPictureIndex = 0xffff;
  45. if (s->pict_type == AV_PICTURE_TYPE_B && !v->bi_type)
  46. pp->wBackwardRefPictureIndex = ff_dxva2_get_surface_index(ctx, s->next_picture.f);
  47. else
  48. pp->wBackwardRefPictureIndex = 0xffff;
  49. if (v->profile == PROFILE_ADVANCED) {
  50. /* It is the cropped width/height -1 of the frame */
  51. pp->wPicWidthInMBminus1 = avctx->width - 1;
  52. pp->wPicHeightInMBminus1= avctx->height - 1;
  53. } else {
  54. /* It is the coded width/height in macroblock -1 of the frame */
  55. pp->wPicWidthInMBminus1 = s->mb_width - 1;
  56. pp->wPicHeightInMBminus1= s->mb_height - 1;
  57. }
  58. pp->bMacroblockWidthMinus1 = 15;
  59. pp->bMacroblockHeightMinus1 = 15;
  60. pp->bBlockWidthMinus1 = 7;
  61. pp->bBlockHeightMinus1 = 7;
  62. pp->bBPPminus1 = 7;
  63. if (s->picture_structure & PICT_TOP_FIELD)
  64. pp->bPicStructure |= 0x01;
  65. if (s->picture_structure & PICT_BOTTOM_FIELD)
  66. pp->bPicStructure |= 0x02;
  67. pp->bSecondField = v->interlace && v->fcm != ILACE_FIELD && !s->first_field;
  68. pp->bPicIntra = s->pict_type == AV_PICTURE_TYPE_I || v->bi_type;
  69. pp->bPicBackwardPrediction = s->pict_type == AV_PICTURE_TYPE_B && !v->bi_type;
  70. pp->bBidirectionalAveragingMode = (1 << 7) |
  71. ((ctx->cfg->ConfigIntraResidUnsigned != 0) << 6) |
  72. ((ctx->cfg->ConfigResidDiffAccelerator != 0) << 5) |
  73. ((v->lumscale != 32 || v->lumshift != 0) << 4) |
  74. ((v->profile == PROFILE_ADVANCED) << 3);
  75. pp->bMVprecisionAndChromaRelation = ((v->mv_mode == MV_PMODE_1MV_HPEL_BILIN) << 3) |
  76. (1 << 2) |
  77. (0 << 1) |
  78. (!s->quarter_sample );
  79. pp->bChromaFormat = v->chromaformat;
  80. ctx->report_id++;
  81. if (ctx->report_id >= (1 << 16))
  82. ctx->report_id = 1;
  83. pp->bPicScanFixed = ctx->report_id >> 8;
  84. pp->bPicScanMethod = ctx->report_id & 0xff;
  85. pp->bPicReadbackRequests = 0;
  86. pp->bRcontrol = v->rnd;
  87. pp->bPicSpatialResid8 = (v->panscanflag << 7) |
  88. (v->refdist_flag << 6) |
  89. (s->loop_filter << 5) |
  90. (v->fastuvmc << 4) |
  91. (v->extended_mv << 3) |
  92. (v->dquant << 1) |
  93. (v->vstransform );
  94. pp->bPicOverflowBlocks = (v->quantizer_mode << 6) |
  95. (v->multires << 5) |
  96. (v->resync_marker << 4) |
  97. (v->rangered << 3) |
  98. (s->max_b_frames );
  99. pp->bPicExtrapolation = (!v->interlace || v->fcm == PROGRESSIVE) ? 1 : 2;
  100. pp->bPicDeblocked = ((!pp->bPicBackwardPrediction && v->overlap) << 6) |
  101. ((v->profile != PROFILE_ADVANCED && v->rangeredfrm) << 5) |
  102. (s->loop_filter << 1);
  103. pp->bPicDeblockConfined = (v->postprocflag << 7) |
  104. (v->broadcast << 6) |
  105. (v->interlace << 5) |
  106. (v->tfcntrflag << 4) |
  107. (v->finterpflag << 3) |
  108. ((s->pict_type != AV_PICTURE_TYPE_B) << 2) |
  109. (v->psf << 1) |
  110. (v->extended_dmv );
  111. if (s->pict_type != AV_PICTURE_TYPE_I)
  112. pp->bPic4MVallowed = v->mv_mode == MV_PMODE_MIXED_MV ||
  113. (v->mv_mode == MV_PMODE_INTENSITY_COMP &&
  114. v->mv_mode2 == MV_PMODE_MIXED_MV);
  115. if (v->profile == PROFILE_ADVANCED)
  116. pp->bPicOBMC = (v->range_mapy_flag << 7) |
  117. (v->range_mapy << 4) |
  118. (v->range_mapuv_flag << 3) |
  119. (v->range_mapuv );
  120. pp->bPicBinPB = 0;
  121. pp->bMV_RPS = 0;
  122. pp->bReservedBits = 0;
  123. if (s->picture_structure == PICT_FRAME) {
  124. pp->wBitstreamFcodes = v->lumscale;
  125. pp->wBitstreamPCEelements = v->lumshift;
  126. } else {
  127. /* Syntax: (top_field_param << 8) | bottom_field_param */
  128. pp->wBitstreamFcodes = (v->lumscale << 8) | v->lumscale;
  129. pp->wBitstreamPCEelements = (v->lumshift << 8) | v->lumshift;
  130. }
  131. pp->bBitstreamConcealmentNeed = 0;
  132. pp->bBitstreamConcealmentMethod = 0;
  133. }
  134. static void fill_slice(AVCodecContext *avctx, DXVA_SliceInfo *slice,
  135. unsigned position, unsigned size)
  136. {
  137. const VC1Context *v = avctx->priv_data;
  138. const MpegEncContext *s = &v->s;
  139. memset(slice, 0, sizeof(*slice));
  140. slice->wHorizontalPosition = 0;
  141. slice->wVerticalPosition = s->mb_y;
  142. slice->dwSliceBitsInBuffer = 8 * size;
  143. slice->dwSliceDataLocation = position;
  144. slice->bStartCodeBitOffset = 0;
  145. slice->bReservedBits = 0;
  146. slice->wMBbitOffset = get_bits_count(&s->gb);
  147. slice->wNumberMBsInSlice = s->mb_width * s->mb_height; /* XXX We assume 1 slice */
  148. slice->wQuantizerScaleCode = v->pq;
  149. slice->wBadSliceChopping = 0;
  150. }
  151. static int commit_bitstream_and_slice_buffer(AVCodecContext *avctx,
  152. DXVA2_DecodeBufferDesc *bs,
  153. DXVA2_DecodeBufferDesc *sc)
  154. {
  155. const VC1Context *v = avctx->priv_data;
  156. struct dxva_context *ctx = avctx->hwaccel_context;
  157. const MpegEncContext *s = &v->s;
  158. struct dxva2_picture_context *ctx_pic = s->current_picture_ptr->hwaccel_picture_private;
  159. DXVA_SliceInfo *slice = &ctx_pic->si;
  160. static const uint8_t start_code[] = { 0, 0, 1, 0x0d };
  161. const unsigned start_code_size = avctx->codec_id == AV_CODEC_ID_VC1 ? sizeof(start_code) : 0;
  162. const unsigned slice_size = slice->dwSliceBitsInBuffer / 8;
  163. const unsigned padding = 128 - ((start_code_size + slice_size) & 127);
  164. const unsigned data_size = start_code_size + slice_size + padding;
  165. uint8_t *dxva_data;
  166. unsigned dxva_size;
  167. int result;
  168. if (FAILED(IDirectXVideoDecoder_GetBuffer(ctx->decoder,
  169. DXVA2_BitStreamDateBufferType,
  170. &dxva_data, &dxva_size)))
  171. return -1;
  172. result = data_size <= dxva_size ? 0 : -1;
  173. if (!result) {
  174. if (start_code_size > 0)
  175. memcpy(dxva_data, start_code, start_code_size);
  176. memcpy(dxva_data + start_code_size,
  177. ctx_pic->bitstream + slice->dwSliceDataLocation, slice_size);
  178. if (padding > 0)
  179. memset(dxva_data + start_code_size + slice_size, 0, padding);
  180. slice->dwSliceBitsInBuffer = 8 * data_size;
  181. }
  182. if (FAILED(IDirectXVideoDecoder_ReleaseBuffer(ctx->decoder,
  183. DXVA2_BitStreamDateBufferType)))
  184. return -1;
  185. if (result)
  186. return result;
  187. memset(bs, 0, sizeof(*bs));
  188. bs->CompressedBufferType = DXVA2_BitStreamDateBufferType;
  189. bs->DataSize = data_size;
  190. bs->NumMBsInBuffer = s->mb_width * s->mb_height;
  191. assert((bs->DataSize & 127) == 0);
  192. return ff_dxva2_commit_buffer(avctx, ctx, sc,
  193. DXVA2_SliceControlBufferType,
  194. slice, sizeof(*slice), bs->NumMBsInBuffer);
  195. }
  196. static int dxva2_vc1_start_frame(AVCodecContext *avctx,
  197. av_unused const uint8_t *buffer,
  198. av_unused uint32_t size)
  199. {
  200. const VC1Context *v = avctx->priv_data;
  201. struct dxva_context *ctx = avctx->hwaccel_context;
  202. struct dxva2_picture_context *ctx_pic = v->s.current_picture_ptr->hwaccel_picture_private;
  203. if (!ctx->decoder || !ctx->cfg || ctx->surface_count <= 0)
  204. return -1;
  205. assert(ctx_pic);
  206. fill_picture_parameters(avctx, ctx, v, &ctx_pic->pp);
  207. ctx_pic->bitstream_size = 0;
  208. ctx_pic->bitstream = NULL;
  209. return 0;
  210. }
  211. static int dxva2_vc1_decode_slice(AVCodecContext *avctx,
  212. const uint8_t *buffer,
  213. uint32_t size)
  214. {
  215. const VC1Context *v = avctx->priv_data;
  216. const Picture *current_picture = v->s.current_picture_ptr;
  217. struct dxva2_picture_context *ctx_pic = current_picture->hwaccel_picture_private;
  218. if (ctx_pic->bitstream_size > 0)
  219. return -1;
  220. if (avctx->codec_id == AV_CODEC_ID_VC1 &&
  221. size >= 4 && IS_MARKER(AV_RB32(buffer))) {
  222. buffer += 4;
  223. size -= 4;
  224. }
  225. ctx_pic->bitstream_size = size;
  226. ctx_pic->bitstream = buffer;
  227. fill_slice(avctx, &ctx_pic->si, 0, size);
  228. return 0;
  229. }
  230. static int dxva2_vc1_end_frame(AVCodecContext *avctx)
  231. {
  232. VC1Context *v = avctx->priv_data;
  233. struct dxva2_picture_context *ctx_pic = v->s.current_picture_ptr->hwaccel_picture_private;
  234. int ret;
  235. if (ctx_pic->bitstream_size <= 0)
  236. return -1;
  237. ret = ff_dxva2_common_end_frame(avctx, v->s.current_picture_ptr->f,
  238. &ctx_pic->pp, sizeof(ctx_pic->pp),
  239. NULL, 0,
  240. commit_bitstream_and_slice_buffer);
  241. if (!ret)
  242. ff_mpeg_draw_horiz_band(&v->s, 0, avctx->height);
  243. return ret;
  244. }
  245. #if CONFIG_WMV3_DXVA2_HWACCEL
  246. AVHWAccel ff_wmv3_dxva2_hwaccel = {
  247. .name = "wmv3_dxva2",
  248. .type = AVMEDIA_TYPE_VIDEO,
  249. .id = AV_CODEC_ID_WMV3,
  250. .pix_fmt = AV_PIX_FMT_DXVA2_VLD,
  251. .start_frame = dxva2_vc1_start_frame,
  252. .decode_slice = dxva2_vc1_decode_slice,
  253. .end_frame = dxva2_vc1_end_frame,
  254. .frame_priv_data_size = sizeof(struct dxva2_picture_context),
  255. };
  256. #endif
  257. AVHWAccel ff_vc1_dxva2_hwaccel = {
  258. .name = "vc1_dxva2",
  259. .type = AVMEDIA_TYPE_VIDEO,
  260. .id = AV_CODEC_ID_VC1,
  261. .pix_fmt = AV_PIX_FMT_DXVA2_VLD,
  262. .start_frame = dxva2_vc1_start_frame,
  263. .decode_slice = dxva2_vc1_decode_slice,
  264. .end_frame = dxva2_vc1_end_frame,
  265. .frame_priv_data_size = sizeof(struct dxva2_picture_context),
  266. };