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.

280 lines
11KB

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