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.

298 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. void *dxva_data_ptr;
  166. uint8_t *dxva_data;
  167. unsigned dxva_size;
  168. int result;
  169. if (FAILED(IDirectXVideoDecoder_GetBuffer(ctx->decoder,
  170. DXVA2_BitStreamDateBufferType,
  171. &dxva_data_ptr, &dxva_size)))
  172. return -1;
  173. dxva_data = dxva_data_ptr;
  174. result = data_size <= dxva_size ? 0 : -1;
  175. if (!result) {
  176. if (start_code_size > 0)
  177. memcpy(dxva_data, start_code, start_code_size);
  178. memcpy(dxva_data + start_code_size,
  179. ctx_pic->bitstream + slice->dwSliceDataLocation, slice_size);
  180. if (padding > 0)
  181. memset(dxva_data + start_code_size + slice_size, 0, padding);
  182. slice->dwSliceBitsInBuffer = 8 * data_size;
  183. }
  184. if (FAILED(IDirectXVideoDecoder_ReleaseBuffer(ctx->decoder,
  185. DXVA2_BitStreamDateBufferType)))
  186. return -1;
  187. if (result)
  188. return result;
  189. memset(bs, 0, sizeof(*bs));
  190. bs->CompressedBufferType = DXVA2_BitStreamDateBufferType;
  191. bs->DataSize = data_size;
  192. bs->NumMBsInBuffer = s->mb_width * s->mb_height;
  193. assert((bs->DataSize & 127) == 0);
  194. return ff_dxva2_commit_buffer(avctx, ctx, sc,
  195. DXVA2_SliceControlBufferType,
  196. slice, sizeof(*slice), bs->NumMBsInBuffer);
  197. }
  198. static int dxva2_vc1_start_frame(AVCodecContext *avctx,
  199. av_unused const uint8_t *buffer,
  200. av_unused uint32_t size)
  201. {
  202. const VC1Context *v = avctx->priv_data;
  203. struct dxva_context *ctx = avctx->hwaccel_context;
  204. struct dxva2_picture_context *ctx_pic = v->s.current_picture_ptr->hwaccel_picture_private;
  205. if (!ctx->decoder || !ctx->cfg || ctx->surface_count <= 0)
  206. return -1;
  207. assert(ctx_pic);
  208. fill_picture_parameters(avctx, ctx, v, &ctx_pic->pp);
  209. ctx_pic->bitstream_size = 0;
  210. ctx_pic->bitstream = NULL;
  211. return 0;
  212. }
  213. static int dxva2_vc1_decode_slice(AVCodecContext *avctx,
  214. const uint8_t *buffer,
  215. uint32_t size)
  216. {
  217. const VC1Context *v = avctx->priv_data;
  218. const Picture *current_picture = v->s.current_picture_ptr;
  219. struct dxva2_picture_context *ctx_pic = current_picture->hwaccel_picture_private;
  220. if (ctx_pic->bitstream_size > 0)
  221. return -1;
  222. if (avctx->codec_id == AV_CODEC_ID_VC1 &&
  223. size >= 4 && IS_MARKER(AV_RB32(buffer))) {
  224. buffer += 4;
  225. size -= 4;
  226. }
  227. ctx_pic->bitstream_size = size;
  228. ctx_pic->bitstream = buffer;
  229. fill_slice(avctx, &ctx_pic->si, 0, size);
  230. return 0;
  231. }
  232. static int dxva2_vc1_end_frame(AVCodecContext *avctx)
  233. {
  234. VC1Context *v = avctx->priv_data;
  235. struct dxva2_picture_context *ctx_pic = v->s.current_picture_ptr->hwaccel_picture_private;
  236. int ret;
  237. if (ctx_pic->bitstream_size <= 0)
  238. return -1;
  239. ret = ff_dxva2_common_end_frame(avctx, v->s.current_picture_ptr->f,
  240. &ctx_pic->pp, sizeof(ctx_pic->pp),
  241. NULL, 0,
  242. commit_bitstream_and_slice_buffer);
  243. if (!ret)
  244. ff_mpeg_draw_horiz_band(&v->s, 0, avctx->height);
  245. return ret;
  246. }
  247. #if CONFIG_WMV3_DXVA2_HWACCEL
  248. AVHWAccel ff_wmv3_dxva2_hwaccel = {
  249. .name = "wmv3_dxva2",
  250. .type = AVMEDIA_TYPE_VIDEO,
  251. .id = AV_CODEC_ID_WMV3,
  252. .pix_fmt = AV_PIX_FMT_DXVA2_VLD,
  253. .start_frame = dxva2_vc1_start_frame,
  254. .decode_slice = dxva2_vc1_decode_slice,
  255. .end_frame = dxva2_vc1_end_frame,
  256. .frame_priv_data_size = sizeof(struct dxva2_picture_context),
  257. };
  258. #endif
  259. AVHWAccel ff_vc1_dxva2_hwaccel = {
  260. .name = "vc1_dxva2",
  261. .type = AVMEDIA_TYPE_VIDEO,
  262. .id = AV_CODEC_ID_VC1,
  263. .pix_fmt = AV_PIX_FMT_DXVA2_VLD,
  264. .start_frame = dxva2_vc1_start_frame,
  265. .decode_slice = dxva2_vc1_decode_slice,
  266. .end_frame = dxva2_vc1_end_frame,
  267. .frame_priv_data_size = sizeof(struct dxva2_picture_context),
  268. };