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.

341 lines
13KB

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