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.

345 lines
13KB

  1. /*
  2. * MPEG-2 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 "libavutil/log.h"
  23. #include "mpegutils.h"
  24. #include "mpegvideo.h"
  25. // The headers above may include w32threads.h, which uses the original
  26. // _WIN32_WINNT define, while dxva2_internal.h redefines it to target a
  27. // potentially newer version.
  28. #include "dxva2_internal.h"
  29. #define MAX_SLICES 1024
  30. struct dxva2_picture_context {
  31. DXVA_PictureParameters pp;
  32. DXVA_QmatrixData qm;
  33. unsigned slice_count;
  34. DXVA_SliceInfo slice[MAX_SLICES];
  35. const uint8_t *bitstream;
  36. unsigned bitstream_size;
  37. };
  38. static void fill_picture_parameters(AVCodecContext *avctx,
  39. AVDXVAContext *ctx,
  40. const struct MpegEncContext *s,
  41. DXVA_PictureParameters *pp)
  42. {
  43. const Picture *current_picture = s->current_picture_ptr;
  44. int is_field = s->picture_structure != PICT_FRAME;
  45. memset(pp, 0, sizeof(*pp));
  46. pp->wDecodedPictureIndex = ff_dxva2_get_surface_index(avctx, ctx, current_picture->f);
  47. pp->wDeblockedPictureIndex = 0;
  48. if (s->pict_type != AV_PICTURE_TYPE_I)
  49. pp->wForwardRefPictureIndex = ff_dxva2_get_surface_index(avctx, ctx, s->last_picture.f);
  50. else
  51. pp->wForwardRefPictureIndex = 0xffff;
  52. if (s->pict_type == AV_PICTURE_TYPE_B)
  53. pp->wBackwardRefPictureIndex = ff_dxva2_get_surface_index(avctx, ctx, s->next_picture.f);
  54. else
  55. pp->wBackwardRefPictureIndex = 0xffff;
  56. pp->wPicWidthInMBminus1 = s->mb_width - 1;
  57. pp->wPicHeightInMBminus1 = (s->mb_height >> is_field) - 1;
  58. pp->bMacroblockWidthMinus1 = 15;
  59. pp->bMacroblockHeightMinus1 = 15;
  60. pp->bBlockWidthMinus1 = 7;
  61. pp->bBlockHeightMinus1 = 7;
  62. pp->bBPPminus1 = 7;
  63. pp->bPicStructure = s->picture_structure;
  64. pp->bSecondField = is_field && !s->first_field;
  65. pp->bPicIntra = s->pict_type == AV_PICTURE_TYPE_I;
  66. pp->bPicBackwardPrediction = s->pict_type == AV_PICTURE_TYPE_B;
  67. pp->bBidirectionalAveragingMode = 0;
  68. pp->bMVprecisionAndChromaRelation= 0; /* FIXME */
  69. pp->bChromaFormat = s->chroma_format;
  70. pp->bPicScanFixed = 1;
  71. pp->bPicScanMethod = s->alternate_scan ? 1 : 0;
  72. pp->bPicReadbackRequests = 0;
  73. pp->bRcontrol = 0;
  74. pp->bPicSpatialResid8 = 0;
  75. pp->bPicOverflowBlocks = 0;
  76. pp->bPicExtrapolation = 0;
  77. pp->bPicDeblocked = 0;
  78. pp->bPicDeblockConfined = 0;
  79. pp->bPic4MVallowed = 0;
  80. pp->bPicOBMC = 0;
  81. pp->bPicBinPB = 0;
  82. pp->bMV_RPS = 0;
  83. pp->bReservedBits = 0;
  84. pp->wBitstreamFcodes = (s->mpeg_f_code[0][0] << 12) |
  85. (s->mpeg_f_code[0][1] << 8) |
  86. (s->mpeg_f_code[1][0] << 4) |
  87. (s->mpeg_f_code[1][1] );
  88. pp->wBitstreamPCEelements = (s->intra_dc_precision << 14) |
  89. (s->picture_structure << 12) |
  90. (s->top_field_first << 11) |
  91. (s->frame_pred_frame_dct << 10) |
  92. (s->concealment_motion_vectors << 9) |
  93. (s->q_scale_type << 8) |
  94. (s->intra_vlc_format << 7) |
  95. (s->alternate_scan << 6) |
  96. (s->repeat_first_field << 5) |
  97. (s->chroma_420_type << 4) |
  98. (s->progressive_frame << 3);
  99. pp->bBitstreamConcealmentNeed = 0;
  100. pp->bBitstreamConcealmentMethod = 0;
  101. }
  102. static void fill_quantization_matrices(AVCodecContext *avctx,
  103. AVDXVAContext *ctx,
  104. const struct MpegEncContext *s,
  105. DXVA_QmatrixData *qm)
  106. {
  107. int i;
  108. for (i = 0; i < 4; i++)
  109. qm->bNewQmatrix[i] = 1;
  110. for (i = 0; i < 64; i++) {
  111. int n = s->idsp.idct_permutation[ff_zigzag_direct[i]];
  112. qm->Qmatrix[0][i] = s->intra_matrix[n];;
  113. qm->Qmatrix[1][i] = s->inter_matrix[n];;
  114. qm->Qmatrix[2][i] = s->chroma_intra_matrix[n];;
  115. qm->Qmatrix[3][i] = s->chroma_inter_matrix[n];;
  116. }
  117. }
  118. static void fill_slice(AVCodecContext *avctx,
  119. const struct MpegEncContext *s,
  120. DXVA_SliceInfo *slice,
  121. unsigned position,
  122. const uint8_t *buffer, unsigned size)
  123. {
  124. int is_field = s->picture_structure != PICT_FRAME;
  125. GetBitContext gb;
  126. memset(slice, 0, sizeof(*slice));
  127. slice->wHorizontalPosition = s->mb_x;
  128. slice->wVerticalPosition = s->mb_y >> is_field;
  129. slice->dwSliceBitsInBuffer = 8 * size;
  130. slice->dwSliceDataLocation = position;
  131. slice->bStartCodeBitOffset = 0;
  132. slice->bReservedBits = 0;
  133. /* XXX We store the index of the first MB and it will be fixed later */
  134. slice->wNumberMBsInSlice = (s->mb_y >> is_field) * s->mb_width + s->mb_x;
  135. slice->wBadSliceChopping = 0;
  136. init_get_bits(&gb, &buffer[4], 8 * (size - 4));
  137. slice->wQuantizerScaleCode = get_bits(&gb, 5);
  138. while (get_bits1(&gb))
  139. skip_bits(&gb, 8);
  140. slice->wMBbitOffset = 4 * 8 + get_bits_count(&gb);
  141. }
  142. static int commit_bitstream_and_slice_buffer(AVCodecContext *avctx,
  143. DECODER_BUFFER_DESC *bs,
  144. DECODER_BUFFER_DESC *sc)
  145. {
  146. const struct MpegEncContext *s = avctx->priv_data;
  147. AVDXVAContext *ctx = avctx->hwaccel_context;
  148. struct dxva2_picture_context *ctx_pic =
  149. s->current_picture_ptr->hwaccel_picture_private;
  150. const int is_field = s->picture_structure != PICT_FRAME;
  151. const unsigned mb_count = s->mb_width * (s->mb_height >> is_field);
  152. void *dxva_data_ptr;
  153. uint8_t *dxva_data, *current, *end;
  154. unsigned dxva_size;
  155. unsigned i;
  156. unsigned type;
  157. #if CONFIG_D3D11VA
  158. if (avctx->pix_fmt == AV_PIX_FMT_D3D11VA_VLD) {
  159. type = D3D11_VIDEO_DECODER_BUFFER_BITSTREAM;
  160. if (FAILED(ID3D11VideoContext_GetDecoderBuffer(D3D11VA_CONTEXT(ctx)->video_context,
  161. D3D11VA_CONTEXT(ctx)->decoder,
  162. type,
  163. &dxva_size, &dxva_data_ptr)))
  164. return -1;
  165. }
  166. #endif
  167. #if CONFIG_DXVA2
  168. if (avctx->pix_fmt == AV_PIX_FMT_DXVA2_VLD) {
  169. type = DXVA2_BitStreamDateBufferType;
  170. if (FAILED(IDirectXVideoDecoder_GetBuffer(DXVA2_CONTEXT(ctx)->decoder,
  171. type,
  172. &dxva_data_ptr, &dxva_size)))
  173. return -1;
  174. }
  175. #endif
  176. dxva_data = dxva_data_ptr;
  177. current = dxva_data;
  178. end = dxva_data + dxva_size;
  179. for (i = 0; i < ctx_pic->slice_count; i++) {
  180. DXVA_SliceInfo *slice = &ctx_pic->slice[i];
  181. unsigned position = slice->dwSliceDataLocation;
  182. unsigned size = slice->dwSliceBitsInBuffer / 8;
  183. if (size > end - current) {
  184. av_log(avctx, AV_LOG_ERROR, "Failed to build bitstream");
  185. break;
  186. }
  187. slice->dwSliceDataLocation = current - dxva_data;
  188. if (i < ctx_pic->slice_count - 1)
  189. slice->wNumberMBsInSlice =
  190. slice[1].wNumberMBsInSlice - slice[0].wNumberMBsInSlice;
  191. else
  192. slice->wNumberMBsInSlice =
  193. mb_count - slice[0].wNumberMBsInSlice;
  194. memcpy(current, &ctx_pic->bitstream[position], size);
  195. current += size;
  196. }
  197. #if CONFIG_D3D11VA
  198. if (avctx->pix_fmt == AV_PIX_FMT_D3D11VA_VLD)
  199. if (FAILED(ID3D11VideoContext_ReleaseDecoderBuffer(D3D11VA_CONTEXT(ctx)->video_context, D3D11VA_CONTEXT(ctx)->decoder, type)))
  200. return -1;
  201. #endif
  202. #if CONFIG_DXVA2
  203. if (avctx->pix_fmt == AV_PIX_FMT_DXVA2_VLD)
  204. if (FAILED(IDirectXVideoDecoder_ReleaseBuffer(DXVA2_CONTEXT(ctx)->decoder, type)))
  205. return -1;
  206. #endif
  207. if (i < ctx_pic->slice_count)
  208. return -1;
  209. #if CONFIG_D3D11VA
  210. if (avctx->pix_fmt == AV_PIX_FMT_D3D11VA_VLD) {
  211. D3D11_VIDEO_DECODER_BUFFER_DESC *dsc11 = bs;
  212. memset(dsc11, 0, sizeof(*dsc11));
  213. dsc11->BufferType = type;
  214. dsc11->DataSize = current - dxva_data;
  215. dsc11->NumMBsInBuffer = mb_count;
  216. type = D3D11_VIDEO_DECODER_BUFFER_SLICE_CONTROL;
  217. }
  218. #endif
  219. #if CONFIG_DXVA2
  220. if (avctx->pix_fmt == AV_PIX_FMT_DXVA2_VLD) {
  221. DXVA2_DecodeBufferDesc *dsc2 = bs;
  222. memset(dsc2, 0, sizeof(*dsc2));
  223. dsc2->CompressedBufferType = type;
  224. dsc2->DataSize = current - dxva_data;
  225. dsc2->NumMBsInBuffer = mb_count;
  226. type = DXVA2_SliceControlBufferType;
  227. }
  228. #endif
  229. return ff_dxva2_commit_buffer(avctx, ctx, sc,
  230. type,
  231. ctx_pic->slice,
  232. ctx_pic->slice_count * sizeof(*ctx_pic->slice),
  233. mb_count);
  234. }
  235. static int dxva2_mpeg2_start_frame(AVCodecContext *avctx,
  236. av_unused const uint8_t *buffer,
  237. av_unused uint32_t size)
  238. {
  239. const struct MpegEncContext *s = avctx->priv_data;
  240. AVDXVAContext *ctx = avctx->hwaccel_context;
  241. struct dxva2_picture_context *ctx_pic =
  242. s->current_picture_ptr->hwaccel_picture_private;
  243. if (!DXVA_CONTEXT_VALID(avctx, ctx))
  244. return -1;
  245. assert(ctx_pic);
  246. fill_picture_parameters(avctx, ctx, s, &ctx_pic->pp);
  247. fill_quantization_matrices(avctx, ctx, s, &ctx_pic->qm);
  248. ctx_pic->slice_count = 0;
  249. ctx_pic->bitstream_size = 0;
  250. ctx_pic->bitstream = NULL;
  251. return 0;
  252. }
  253. static int dxva2_mpeg2_decode_slice(AVCodecContext *avctx,
  254. const uint8_t *buffer, uint32_t size)
  255. {
  256. const struct MpegEncContext *s = avctx->priv_data;
  257. struct dxva2_picture_context *ctx_pic =
  258. s->current_picture_ptr->hwaccel_picture_private;
  259. unsigned position;
  260. if (ctx_pic->slice_count >= MAX_SLICES) {
  261. avpriv_request_sample(avctx, "%d slices in dxva2",
  262. ctx_pic->slice_count);
  263. return -1;
  264. }
  265. if (!ctx_pic->bitstream)
  266. ctx_pic->bitstream = buffer;
  267. ctx_pic->bitstream_size += size;
  268. position = buffer - ctx_pic->bitstream;
  269. fill_slice(avctx, s, &ctx_pic->slice[ctx_pic->slice_count++], position,
  270. buffer, size);
  271. return 0;
  272. }
  273. static int dxva2_mpeg2_end_frame(AVCodecContext *avctx)
  274. {
  275. struct MpegEncContext *s = avctx->priv_data;
  276. struct dxva2_picture_context *ctx_pic =
  277. s->current_picture_ptr->hwaccel_picture_private;
  278. int ret;
  279. if (ctx_pic->slice_count <= 0 || ctx_pic->bitstream_size <= 0)
  280. return -1;
  281. ret = ff_dxva2_common_end_frame(avctx, s->current_picture_ptr->f,
  282. &ctx_pic->pp, sizeof(ctx_pic->pp),
  283. &ctx_pic->qm, sizeof(ctx_pic->qm),
  284. commit_bitstream_and_slice_buffer);
  285. if (!ret)
  286. ff_mpeg_draw_horiz_band(s, 0, avctx->height);
  287. return ret;
  288. }
  289. #if CONFIG_MPEG2_DXVA2_HWACCEL
  290. AVHWAccel ff_mpeg2_dxva2_hwaccel = {
  291. .name = "mpeg2_dxva2",
  292. .type = AVMEDIA_TYPE_VIDEO,
  293. .id = AV_CODEC_ID_MPEG2VIDEO,
  294. .pix_fmt = AV_PIX_FMT_DXVA2_VLD,
  295. .start_frame = dxva2_mpeg2_start_frame,
  296. .decode_slice = dxva2_mpeg2_decode_slice,
  297. .end_frame = dxva2_mpeg2_end_frame,
  298. .frame_priv_data_size = sizeof(struct dxva2_picture_context),
  299. };
  300. #endif
  301. #if CONFIG_MPEG2_D3D11VA_HWACCEL
  302. AVHWAccel ff_mpeg2_d3d11va_hwaccel = {
  303. .name = "mpeg2_d3d11va",
  304. .type = AVMEDIA_TYPE_VIDEO,
  305. .id = AV_CODEC_ID_MPEG2VIDEO,
  306. .pix_fmt = AV_PIX_FMT_D3D11VA_VLD,
  307. .start_frame = dxva2_mpeg2_start_frame,
  308. .decode_slice = dxva2_mpeg2_decode_slice,
  309. .end_frame = dxva2_mpeg2_end_frame,
  310. .frame_priv_data_size = sizeof(struct dxva2_picture_context),
  311. };
  312. #endif