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.

273 lines
11KB

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