dxva2_vc1.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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 "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 != FF_I_TYPE)
  41. pp->wForwardRefPictureIndex = ff_dxva2_get_surface_index(ctx, &s->last_picture);
  42. else
  43. pp->wForwardRefPictureIndex = 0xffff;
  44. if (s->pict_type == FF_B_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 != 0x03 && !s->first_field;
  67. pp->bPicIntra = s->pict_type == FF_I_TYPE;
  68. pp->bPicBackwardPrediction = s->pict_type == FF_B_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 == 0x00) ? 1 : 2;
  99. pp->bPicDeblocked = ((v->profile != PROFILE_ADVANCED && v->rangeredfrm) << 5) |
  100. (s->loop_filter << 1);
  101. pp->bPicDeblockConfined = (v->postprocflag << 7) |
  102. (v->broadcast << 6) |
  103. (v->interlace << 5) |
  104. (v->tfcntrflag << 4) |
  105. (v->finterpflag << 3) |
  106. ((s->pict_type != FF_B_TYPE) << 2) |
  107. (v->psf << 1) |
  108. (v->extended_dmv );
  109. if (s->pict_type != FF_I_TYPE)
  110. pp->bPic4MVallowed = v->mv_mode == MV_PMODE_MIXED_MV ||
  111. (v->mv_mode == MV_PMODE_INTENSITY_COMP &&
  112. v->mv_mode2 == MV_PMODE_MIXED_MV);
  113. if (v->profile == PROFILE_ADVANCED)
  114. pp->bPicOBMC = (v->range_mapy_flag << 7) |
  115. (v->range_mapy << 4) |
  116. (v->range_mapuv_flag << 3) |
  117. (v->range_mapuv );
  118. pp->bPicBinPB = 0;
  119. pp->bMV_RPS = 0;
  120. pp->bReservedBits = 0;
  121. if (s->picture_structure == PICT_FRAME) {
  122. pp->wBitstreamFcodes = v->lumscale;
  123. pp->wBitstreamPCEelements = v->lumshift;
  124. } else {
  125. /* Syntax: (top_field_param << 8) | bottom_field_param */
  126. pp->wBitstreamFcodes = (v->lumscale << 8) | v->lumscale;
  127. pp->wBitstreamPCEelements = (v->lumshift << 8) | v->lumshift;
  128. }
  129. pp->bBitstreamConcealmentNeed = 0;
  130. pp->bBitstreamConcealmentMethod = 0;
  131. }
  132. static void fill_slice(AVCodecContext *avctx, DXVA_SliceInfo *slice,
  133. unsigned position, unsigned size)
  134. {
  135. const VC1Context *v = avctx->priv_data;
  136. const MpegEncContext *s = &v->s;
  137. memset(slice, 0, sizeof(*slice));
  138. slice->wHorizontalPosition = 0;
  139. slice->wVerticalPosition = s->mb_y;
  140. slice->dwSliceBitsInBuffer = 8 * size;
  141. slice->dwSliceDataLocation = position;
  142. slice->bStartCodeBitOffset = 0;
  143. slice->bReservedBits = 0;
  144. slice->wMBbitOffset = get_bits_count(&s->gb);
  145. slice->wNumberMBsInSlice = s->mb_width * s->mb_height; /* XXX We assume 1 slice */
  146. slice->wQuantizerScaleCode = v->pq;
  147. slice->wBadSliceChopping = 0;
  148. }
  149. static int commit_bitstream_and_slice_buffer(AVCodecContext *avctx,
  150. DXVA2_DecodeBufferDesc *bs,
  151. DXVA2_DecodeBufferDesc *sc)
  152. {
  153. const VC1Context *v = avctx->priv_data;
  154. struct dxva_context *ctx = avctx->hwaccel_context;
  155. const MpegEncContext *s = &v->s;
  156. struct dxva2_picture_context *ctx_pic = s->current_picture_ptr->hwaccel_picture_private;
  157. DXVA_SliceInfo *slice = &ctx_pic->si;
  158. static const uint8_t start_code[] = { 0, 0, 1, 0x0d };
  159. const unsigned start_code_size = avctx->codec_id == CODEC_ID_VC1 ? sizeof(start_code) : 0;
  160. const unsigned slice_size = slice->dwSliceBitsInBuffer / 8;
  161. const unsigned padding = 128 - ((start_code_size + slice_size) & 127);
  162. const unsigned data_size = start_code_size + slice_size + padding;
  163. uint8_t *dxva_data;
  164. unsigned dxva_size;
  165. int result;
  166. if (FAILED(IDirectXVideoDecoder_GetBuffer(ctx->decoder,
  167. DXVA2_BitStreamDateBufferType,
  168. &dxva_data, &dxva_size)))
  169. return -1;
  170. result = data_size <= dxva_size ? 0 : -1;
  171. if (!result) {
  172. if (start_code_size > 0)
  173. memcpy(dxva_data, start_code, start_code_size);
  174. memcpy(dxva_data + start_code_size,
  175. ctx_pic->bitstream + slice->dwSliceDataLocation, slice_size);
  176. if (padding > 0)
  177. memset(dxva_data + start_code_size + slice_size, 0, padding);
  178. slice->dwSliceBitsInBuffer = 8 * data_size;
  179. }
  180. if (FAILED(IDirectXVideoDecoder_ReleaseBuffer(ctx->decoder,
  181. DXVA2_BitStreamDateBufferType)))
  182. return -1;
  183. if (result)
  184. return result;
  185. memset(bs, 0, sizeof(*bs));
  186. bs->CompressedBufferType = DXVA2_BitStreamDateBufferType;
  187. bs->DataSize = data_size;
  188. bs->NumMBsInBuffer = s->mb_width * s->mb_height;
  189. assert((bs->DataSize & 127) == 0);
  190. return ff_dxva2_commit_buffer(avctx, ctx, sc,
  191. DXVA2_SliceControlBufferType,
  192. slice, sizeof(*slice), bs->NumMBsInBuffer);
  193. }
  194. static int start_frame(AVCodecContext *avctx,
  195. av_unused const uint8_t *buffer,
  196. av_unused uint32_t size)
  197. {
  198. const VC1Context *v = avctx->priv_data;
  199. struct dxva_context *ctx = avctx->hwaccel_context;
  200. struct dxva2_picture_context *ctx_pic = v->s.current_picture_ptr->hwaccel_picture_private;
  201. if (!ctx->decoder || !ctx->cfg || ctx->surface_count <= 0)
  202. return -1;
  203. assert(ctx_pic);
  204. fill_picture_parameters(avctx, ctx, v, &ctx_pic->pp);
  205. ctx_pic->bitstream_size = 0;
  206. ctx_pic->bitstream = NULL;
  207. return 0;
  208. }
  209. static int decode_slice(AVCodecContext *avctx,
  210. const uint8_t *buffer, uint32_t size)
  211. {
  212. const VC1Context *v = avctx->priv_data;
  213. const Picture *current_picture = v->s.current_picture_ptr;
  214. struct dxva2_picture_context *ctx_pic = current_picture->hwaccel_picture_private;
  215. if (ctx_pic->bitstream_size > 0)
  216. return -1;
  217. if (avctx->codec_id == CODEC_ID_VC1 &&
  218. size >= 4 && IS_MARKER(AV_RB32(buffer))) {
  219. buffer += 4;
  220. size -= 4;
  221. }
  222. ctx_pic->bitstream_size = size;
  223. ctx_pic->bitstream = buffer;
  224. fill_slice(avctx, &ctx_pic->si, 0, size);
  225. return 0;
  226. }
  227. static int end_frame(AVCodecContext *avctx)
  228. {
  229. VC1Context *v = avctx->priv_data;
  230. struct dxva2_picture_context *ctx_pic = v->s.current_picture_ptr->hwaccel_picture_private;
  231. if (ctx_pic->bitstream_size <= 0)
  232. return -1;
  233. return ff_dxva2_common_end_frame(avctx, &v->s,
  234. &ctx_pic->pp, sizeof(ctx_pic->pp),
  235. NULL, 0,
  236. commit_bitstream_and_slice_buffer);
  237. }
  238. #if CONFIG_WMV3_DXVA2_HWACCEL
  239. AVHWAccel ff_wmv3_dxva2_hwaccel = {
  240. .name = "wmv3_dxva2",
  241. .type = AVMEDIA_TYPE_VIDEO,
  242. .id = CODEC_ID_WMV3,
  243. .pix_fmt = PIX_FMT_DXVA2_VLD,
  244. .capabilities = 0,
  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 = CODEC_ID_VC1,
  255. .pix_fmt = PIX_FMT_DXVA2_VLD,
  256. .capabilities = 0,
  257. .start_frame = start_frame,
  258. .decode_slice = decode_slice,
  259. .end_frame = end_frame,
  260. .priv_data_size = sizeof(struct dxva2_picture_context),
  261. };