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.

284 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. #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. struct dxva_context *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(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(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(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. struct dxva_context *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. while (get_bits1(&gb))
  135. skip_bits(&gb, 8);
  136. slice->wMBbitOffset = 4 * 8 + get_bits_count(&gb);
  137. }
  138. static int commit_bitstream_and_slice_buffer(AVCodecContext *avctx,
  139. DXVA2_DecodeBufferDesc *bs,
  140. DXVA2_DecodeBufferDesc *sc)
  141. {
  142. const struct MpegEncContext *s = avctx->priv_data;
  143. struct dxva_context *ctx = avctx->hwaccel_context;
  144. struct dxva2_picture_context *ctx_pic =
  145. s->current_picture_ptr->hwaccel_picture_private;
  146. const int is_field = s->picture_structure != PICT_FRAME;
  147. const unsigned mb_count = s->mb_width * (s->mb_height >> is_field);
  148. void *dxva_data_ptr;
  149. uint8_t *dxva_data, *current, *end;
  150. unsigned dxva_size;
  151. unsigned i;
  152. if (FAILED(IDirectXVideoDecoder_GetBuffer(ctx->decoder,
  153. DXVA2_BitStreamDateBufferType,
  154. &dxva_data_ptr, &dxva_size)))
  155. return -1;
  156. dxva_data = dxva_data_ptr;
  157. current = dxva_data;
  158. end = dxva_data + dxva_size;
  159. for (i = 0; i < ctx_pic->slice_count; i++) {
  160. DXVA_SliceInfo *slice = &ctx_pic->slice[i];
  161. unsigned position = slice->dwSliceDataLocation;
  162. unsigned size = slice->dwSliceBitsInBuffer / 8;
  163. if (size > end - current) {
  164. av_log(avctx, AV_LOG_ERROR, "Failed to build bitstream");
  165. break;
  166. }
  167. slice->dwSliceDataLocation = current - dxva_data;
  168. if (i < ctx_pic->slice_count - 1)
  169. slice->wNumberMBsInSlice =
  170. slice[1].wNumberMBsInSlice - slice[0].wNumberMBsInSlice;
  171. else
  172. slice->wNumberMBsInSlice =
  173. mb_count - slice[0].wNumberMBsInSlice;
  174. memcpy(current, &ctx_pic->bitstream[position], size);
  175. current += size;
  176. }
  177. if (FAILED(IDirectXVideoDecoder_ReleaseBuffer(ctx->decoder,
  178. DXVA2_BitStreamDateBufferType)))
  179. return -1;
  180. if (i < ctx_pic->slice_count)
  181. return -1;
  182. memset(bs, 0, sizeof(*bs));
  183. bs->CompressedBufferType = DXVA2_BitStreamDateBufferType;
  184. bs->DataSize = current - dxva_data;
  185. bs->NumMBsInBuffer = mb_count;
  186. return ff_dxva2_commit_buffer(avctx, ctx, sc,
  187. DXVA2_SliceControlBufferType,
  188. ctx_pic->slice,
  189. ctx_pic->slice_count * sizeof(*ctx_pic->slice),
  190. mb_count);
  191. }
  192. static int dxva2_mpeg2_start_frame(AVCodecContext *avctx,
  193. av_unused const uint8_t *buffer,
  194. av_unused uint32_t size)
  195. {
  196. const struct MpegEncContext *s = avctx->priv_data;
  197. struct dxva_context *ctx = avctx->hwaccel_context;
  198. struct dxva2_picture_context *ctx_pic =
  199. s->current_picture_ptr->hwaccel_picture_private;
  200. if (!ctx->decoder || !ctx->cfg || ctx->surface_count <= 0)
  201. return -1;
  202. assert(ctx_pic);
  203. fill_picture_parameters(avctx, ctx, s, &ctx_pic->pp);
  204. fill_quantization_matrices(avctx, ctx, s, &ctx_pic->qm);
  205. ctx_pic->slice_count = 0;
  206. ctx_pic->bitstream_size = 0;
  207. ctx_pic->bitstream = NULL;
  208. return 0;
  209. }
  210. static int dxva2_mpeg2_decode_slice(AVCodecContext *avctx,
  211. const uint8_t *buffer, uint32_t size)
  212. {
  213. const struct MpegEncContext *s = avctx->priv_data;
  214. struct dxva2_picture_context *ctx_pic =
  215. s->current_picture_ptr->hwaccel_picture_private;
  216. unsigned position;
  217. if (ctx_pic->slice_count >= MAX_SLICES) {
  218. avpriv_request_sample(avctx, "%d slices in dxva2",
  219. ctx_pic->slice_count);
  220. return -1;
  221. }
  222. if (!ctx_pic->bitstream)
  223. ctx_pic->bitstream = buffer;
  224. ctx_pic->bitstream_size += size;
  225. position = buffer - ctx_pic->bitstream;
  226. fill_slice(avctx, s, &ctx_pic->slice[ctx_pic->slice_count++], position,
  227. buffer, size);
  228. return 0;
  229. }
  230. static int dxva2_mpeg2_end_frame(AVCodecContext *avctx)
  231. {
  232. struct MpegEncContext *s = avctx->priv_data;
  233. struct dxva2_picture_context *ctx_pic =
  234. s->current_picture_ptr->hwaccel_picture_private;
  235. int ret;
  236. if (ctx_pic->slice_count <= 0 || ctx_pic->bitstream_size <= 0)
  237. return -1;
  238. ret = ff_dxva2_common_end_frame(avctx, s->current_picture_ptr->f,
  239. &ctx_pic->pp, sizeof(ctx_pic->pp),
  240. &ctx_pic->qm, sizeof(ctx_pic->qm),
  241. commit_bitstream_and_slice_buffer);
  242. if (!ret)
  243. ff_mpeg_draw_horiz_band(s, 0, avctx->height);
  244. return ret;
  245. }
  246. AVHWAccel ff_mpeg2_dxva2_hwaccel = {
  247. .name = "mpeg2_dxva2",
  248. .type = AVMEDIA_TYPE_VIDEO,
  249. .id = AV_CODEC_ID_MPEG2VIDEO,
  250. .pix_fmt = AV_PIX_FMT_DXVA2_VLD,
  251. .start_frame = dxva2_mpeg2_start_frame,
  252. .decode_slice = dxva2_mpeg2_decode_slice,
  253. .end_frame = dxva2_mpeg2_end_frame,
  254. .frame_priv_data_size = sizeof(struct dxva2_picture_context),
  255. };