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.

290 lines
12KB

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