Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | RSS feed

  1. /*
  2.  * DXVA2 WMV3/VC-1 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.  
  23. #include "dxva2_internal.h"
  24. #include "mpegutils.h"
  25. #include "vc1.h"
  26. #include "vc1data.h"
  27.  
  28. struct dxva2_picture_context {
  29.     DXVA_PictureParameters pp;
  30.     DXVA_SliceInfo         si;
  31.  
  32.     const uint8_t          *bitstream;
  33.     unsigned               bitstream_size;
  34. };
  35.  
  36. static void fill_picture_parameters(AVCodecContext *avctx,
  37.                                     AVDXVAContext *ctx, const VC1Context *v,
  38.                                     DXVA_PictureParameters *pp)
  39. {
  40.     const MpegEncContext *s = &v->s;
  41.     const Picture *current_picture = s->current_picture_ptr;
  42.     int intcomp = 0;
  43.  
  44.     // determine if intensity compensation is needed
  45.     if (s->pict_type == AV_PICTURE_TYPE_P) {
  46.       if ((v->fcm == ILACE_FRAME && v->intcomp) || (v->fcm != ILACE_FRAME && v->mv_mode == MV_PMODE_INTENSITY_COMP)) {
  47.         if (v->lumscale != 32 || v->lumshift != 0 || (s->picture_structure != PICT_FRAME && (v->lumscale2 != 32 || v->lumshift2 != 0)))
  48.           intcomp = 1;
  49.       }
  50.     }
  51.  
  52.     memset(pp, 0, sizeof(*pp));
  53.     pp->wDecodedPictureIndex    =
  54.     pp->wDeblockedPictureIndex  = ff_dxva2_get_surface_index(avctx, ctx, current_picture->f);
  55.     if (s->pict_type != AV_PICTURE_TYPE_I && !v->bi_type)
  56.         pp->wForwardRefPictureIndex = ff_dxva2_get_surface_index(avctx, ctx, s->last_picture.f);
  57.     else
  58.         pp->wForwardRefPictureIndex = 0xffff;
  59.     if (s->pict_type == AV_PICTURE_TYPE_B && !v->bi_type)
  60.         pp->wBackwardRefPictureIndex = ff_dxva2_get_surface_index(avctx, ctx, s->next_picture.f);
  61.     else
  62.         pp->wBackwardRefPictureIndex = 0xffff;
  63.     if (v->profile == PROFILE_ADVANCED) {
  64.         /* It is the cropped width/height -1 of the frame */
  65.         pp->wPicWidthInMBminus1 = avctx->width  - 1;
  66.         pp->wPicHeightInMBminus1= avctx->height - 1;
  67.     } else {
  68.         /* It is the coded width/height in macroblock -1 of the frame */
  69.         pp->wPicWidthInMBminus1 = s->mb_width  - 1;
  70.         pp->wPicHeightInMBminus1= s->mb_height - 1;
  71.     }
  72.     pp->bMacroblockWidthMinus1  = 15;
  73.     pp->bMacroblockHeightMinus1 = 15;
  74.     pp->bBlockWidthMinus1       = 7;
  75.     pp->bBlockHeightMinus1      = 7;
  76.     pp->bBPPminus1              = 7;
  77.     if (s->picture_structure & PICT_TOP_FIELD)
  78.         pp->bPicStructure      |= 0x01;
  79.     if (s->picture_structure & PICT_BOTTOM_FIELD)
  80.         pp->bPicStructure      |= 0x02;
  81.     pp->bSecondField            = v->interlace && v->fcm == ILACE_FIELD && v->second_field;
  82.     pp->bPicIntra               = s->pict_type == AV_PICTURE_TYPE_I || v->bi_type;
  83.     pp->bPicBackwardPrediction  = s->pict_type == AV_PICTURE_TYPE_B && !v->bi_type;
  84.     pp->bBidirectionalAveragingMode = (1                                           << 7) |
  85.                                       ((DXVA_CONTEXT_CFG_INTRARESID(avctx, ctx) != 0) << 6) |
  86.                                       ((DXVA_CONTEXT_CFG_RESIDACCEL(avctx, ctx) != 0) << 5) |
  87.                                       (intcomp                                     << 4) |
  88.                                       ((v->profile == PROFILE_ADVANCED)            << 3);
  89.     pp->bMVprecisionAndChromaRelation = ((v->mv_mode == MV_PMODE_1MV_HPEL_BILIN) << 3) |
  90.                                         (1                                       << 2) |
  91.                                         (0                                       << 1) |
  92.                                         (!s->quarter_sample                          );
  93.     pp->bChromaFormat           = v->chromaformat;
  94.     DXVA_CONTEXT_REPORT_ID(avctx, ctx)++;
  95.     if (DXVA_CONTEXT_REPORT_ID(avctx, ctx) >= (1 << 16))
  96.         DXVA_CONTEXT_REPORT_ID(avctx, ctx) = 1;
  97.     pp->bPicScanFixed           = DXVA_CONTEXT_REPORT_ID(avctx, ctx) >> 8;
  98.     pp->bPicScanMethod          = DXVA_CONTEXT_REPORT_ID(avctx, ctx) & 0xff;
  99.     pp->bPicReadbackRequests    = 0;
  100.     pp->bRcontrol               = v->rnd;
  101.     pp->bPicSpatialResid8       = (v->panscanflag  << 7) |
  102.                                   (v->refdist_flag << 6) |
  103.                                   (s->loop_filter  << 5) |
  104.                                   (v->fastuvmc     << 4) |
  105.                                   (v->extended_mv  << 3) |
  106.                                   (v->dquant       << 1) |
  107.                                   (v->vstransform      );
  108.     pp->bPicOverflowBlocks      = (v->quantizer_mode << 6) |
  109.                                   (v->multires       << 5) |
  110.                                   (v->resync_marker  << 4) |
  111.                                   (v->rangered       << 3) |
  112.                                   (s->max_b_frames       );
  113.     pp->bPicExtrapolation       = (!v->interlace || v->fcm == PROGRESSIVE) ? 1 : 2;
  114.     pp->bPicDeblocked           = ((!pp->bPicBackwardPrediction && v->overlap)        << 6) |
  115.                                   ((v->profile != PROFILE_ADVANCED && v->rangeredfrm) << 5) |
  116.                                   (s->loop_filter                                     << 1);
  117.     pp->bPicDeblockConfined     = (v->postprocflag             << 7) |
  118.                                   (v->broadcast                << 6) |
  119.                                   (v->interlace                << 5) |
  120.                                   (v->tfcntrflag               << 4) |
  121.                                   (v->finterpflag              << 3) |
  122.                                   ((s->pict_type != AV_PICTURE_TYPE_B) << 2) |
  123.                                   (v->psf                      << 1) |
  124.                                   (v->extended_dmv                 );
  125.     if (s->pict_type != AV_PICTURE_TYPE_I)
  126.         pp->bPic4MVallowed      = v->mv_mode == MV_PMODE_MIXED_MV ||
  127.                                   (v->mv_mode == MV_PMODE_INTENSITY_COMP &&
  128.                                    v->mv_mode2 == MV_PMODE_MIXED_MV);
  129.     if (v->profile == PROFILE_ADVANCED)
  130.         pp->bPicOBMC            = (v->range_mapy_flag  << 7) |
  131.                                   (v->range_mapy       << 4) |
  132.                                   (v->range_mapuv_flag << 3) |
  133.                                   (v->range_mapuv          );
  134.     pp->bPicBinPB               = 0;
  135.     pp->bMV_RPS                 = (v->fcm == ILACE_FIELD && pp->bPicBackwardPrediction) ? v->refdist + 9 : 0;
  136.     pp->bReservedBits           = v->pq;
  137.     if (s->picture_structure == PICT_FRAME) {
  138.         if (intcomp) {
  139.             pp->wBitstreamFcodes      = v->lumscale;
  140.             pp->wBitstreamPCEelements = v->lumshift;
  141.         } else {
  142.             pp->wBitstreamFcodes      = 32;
  143.             pp->wBitstreamPCEelements = 0;
  144.         }
  145.     } else {
  146.         /* Syntax: (top_field_param << 8) | bottom_field_param */
  147.         if (intcomp) {
  148.             pp->wBitstreamFcodes      = (v->lumscale << 8) | v->lumscale2;
  149.             pp->wBitstreamPCEelements = (v->lumshift << 8) | v->lumshift2;
  150.         } else {
  151.             pp->wBitstreamFcodes      = (32 << 8) | 32;
  152.             pp->wBitstreamPCEelements = 0;
  153.         }
  154.     }
  155.     pp->bBitstreamConcealmentNeed   = 0;
  156.     pp->bBitstreamConcealmentMethod = 0;
  157. }
  158.  
  159. static void fill_slice(AVCodecContext *avctx, DXVA_SliceInfo *slice,
  160.                        unsigned position, unsigned size)
  161. {
  162.     const VC1Context *v = avctx->priv_data;
  163.     const MpegEncContext *s = &v->s;
  164.  
  165.     memset(slice, 0, sizeof(*slice));
  166.     slice->wHorizontalPosition = 0;
  167.     slice->wVerticalPosition   = s->mb_y;
  168.     slice->dwSliceBitsInBuffer = 8 * size;
  169.     slice->dwSliceDataLocation = position;
  170.     slice->bStartCodeBitOffset = 0;
  171.     slice->bReservedBits       = (s->pict_type == AV_PICTURE_TYPE_B && !v->bi_type) ? v->bfraction_lut_index + 9 : 0;
  172.     slice->wMBbitOffset        = v->p_frame_skipped ? 0xffff : get_bits_count(&s->gb) + (avctx->codec_id == AV_CODEC_ID_VC1 ? 32 : 0);
  173.     slice->wNumberMBsInSlice   = s->mb_width * s->mb_height; /* XXX We assume 1 slice */
  174.     slice->wQuantizerScaleCode = v->pq;
  175.     slice->wBadSliceChopping   = 0;
  176. }
  177.  
  178. static int commit_bitstream_and_slice_buffer(AVCodecContext *avctx,
  179.                                              DECODER_BUFFER_DESC *bs,
  180.                                              DECODER_BUFFER_DESC *sc)
  181. {
  182.     const VC1Context *v = avctx->priv_data;
  183.     AVDXVAContext *ctx = avctx->hwaccel_context;
  184.     const MpegEncContext *s = &v->s;
  185.     struct dxva2_picture_context *ctx_pic = s->current_picture_ptr->hwaccel_picture_private;
  186.  
  187.     DXVA_SliceInfo *slice = &ctx_pic->si;
  188.  
  189.     static const uint8_t start_code[] = { 0, 0, 1, 0x0d };
  190.     const unsigned start_code_size = avctx->codec_id == AV_CODEC_ID_VC1 ? sizeof(start_code) : 0;
  191.     const unsigned slice_size = slice->dwSliceBitsInBuffer / 8;
  192.     const unsigned padding = 128 - ((start_code_size + slice_size) & 127);
  193.     const unsigned data_size = start_code_size + slice_size + padding;
  194.  
  195.     void     *dxva_data_ptr;
  196.     uint8_t  *dxva_data;
  197.     unsigned dxva_size;
  198.     int result;
  199.     unsigned type;
  200.  
  201. #if CONFIG_D3D11VA
  202.     if (avctx->pix_fmt == AV_PIX_FMT_D3D11VA_VLD) {
  203.         type = D3D11_VIDEO_DECODER_BUFFER_BITSTREAM;
  204.         if (FAILED(ID3D11VideoContext_GetDecoderBuffer(D3D11VA_CONTEXT(ctx)->video_context,
  205.                                                        D3D11VA_CONTEXT(ctx)->decoder,
  206.                                                        type,
  207.                                                        &dxva_size, &dxva_data_ptr)))
  208.             return -1;
  209.     }
  210. #endif
  211. #if CONFIG_DXVA2
  212.     if (avctx->pix_fmt == AV_PIX_FMT_DXVA2_VLD) {
  213.         type = DXVA2_BitStreamDateBufferType;
  214.         if (FAILED(IDirectXVideoDecoder_GetBuffer(DXVA2_CONTEXT(ctx)->decoder,
  215.                                                   type,
  216.                                                   &dxva_data_ptr, &dxva_size)))
  217.             return -1;
  218.     }
  219. #endif
  220.  
  221.     dxva_data = dxva_data_ptr;
  222.     result = data_size <= dxva_size ? 0 : -1;
  223.     if (!result) {
  224.         if (start_code_size > 0) {
  225.             memcpy(dxva_data, start_code, start_code_size);
  226.             if (v->second_field)
  227.                 dxva_data[3] = 0x0c;
  228.         }
  229.         memcpy(dxva_data + start_code_size,
  230.                ctx_pic->bitstream + slice->dwSliceDataLocation, slice_size);
  231.         if (padding > 0)
  232.             memset(dxva_data + start_code_size + slice_size, 0, padding);
  233.         slice->dwSliceBitsInBuffer = 8 * data_size;
  234.     }
  235. #if CONFIG_D3D11VA
  236.     if (avctx->pix_fmt == AV_PIX_FMT_D3D11VA_VLD)
  237.         if (FAILED(ID3D11VideoContext_ReleaseDecoderBuffer(D3D11VA_CONTEXT(ctx)->video_context, D3D11VA_CONTEXT(ctx)->decoder, type)))
  238.             return -1;
  239. #endif
  240. #if CONFIG_DXVA2
  241.     if (avctx->pix_fmt == AV_PIX_FMT_DXVA2_VLD)
  242.         if (FAILED(IDirectXVideoDecoder_ReleaseBuffer(DXVA2_CONTEXT(ctx)->decoder, type)))
  243.             return -1;
  244. #endif
  245.     if (result)
  246.         return result;
  247.  
  248. #if CONFIG_D3D11VA
  249.     if (avctx->pix_fmt == AV_PIX_FMT_D3D11VA_VLD) {
  250.         D3D11_VIDEO_DECODER_BUFFER_DESC *dsc11 = bs;
  251.         memset(dsc11, 0, sizeof(*dsc11));
  252.         dsc11->BufferType           = type;
  253.         dsc11->DataSize             = data_size;
  254.         dsc11->NumMBsInBuffer       = s->mb_width * s->mb_height;
  255.  
  256.         type = D3D11_VIDEO_DECODER_BUFFER_SLICE_CONTROL;
  257.     }
  258. #endif
  259. #if CONFIG_DXVA2
  260.     if (avctx->pix_fmt == AV_PIX_FMT_DXVA2_VLD) {
  261.         DXVA2_DecodeBufferDesc *dsc2 = bs;
  262.         memset(dsc2, 0, sizeof(*dsc2));
  263.         dsc2->CompressedBufferType = type;
  264.         dsc2->DataSize             = data_size;
  265.         dsc2->NumMBsInBuffer       = s->mb_width * s->mb_height;
  266.  
  267.         type = DXVA2_SliceControlBufferType;
  268.     }
  269. #endif
  270.     assert((data_size & 127) == 0);
  271.  
  272.     return ff_dxva2_commit_buffer(avctx, ctx, sc,
  273.                                   type,
  274.                                   slice, sizeof(*slice), s->mb_width * s->mb_height);
  275. }
  276.  
  277. static int dxva2_vc1_start_frame(AVCodecContext *avctx,
  278.                                  av_unused const uint8_t *buffer,
  279.                                  av_unused uint32_t size)
  280. {
  281.     const VC1Context *v = avctx->priv_data;
  282.     AVDXVAContext *ctx = avctx->hwaccel_context;
  283.     struct dxva2_picture_context *ctx_pic = v->s.current_picture_ptr->hwaccel_picture_private;
  284.  
  285.     if (DXVA_CONTEXT_DECODER(avctx, ctx) == NULL ||
  286.         DXVA_CONTEXT_CFG(avctx, ctx) == NULL ||
  287.         DXVA_CONTEXT_COUNT(avctx, ctx) <= 0)
  288.         return -1;
  289.     assert(ctx_pic);
  290.  
  291.     fill_picture_parameters(avctx, ctx, v, &ctx_pic->pp);
  292.  
  293.     ctx_pic->bitstream_size = 0;
  294.     ctx_pic->bitstream      = NULL;
  295.     return 0;
  296. }
  297.  
  298. static int dxva2_vc1_decode_slice(AVCodecContext *avctx,
  299.                                   const uint8_t *buffer,
  300.                                   uint32_t size)
  301. {
  302.     const VC1Context *v = avctx->priv_data;
  303.     const Picture *current_picture = v->s.current_picture_ptr;
  304.     struct dxva2_picture_context *ctx_pic = current_picture->hwaccel_picture_private;
  305.  
  306.     if (ctx_pic->bitstream_size > 0)
  307.         return -1;
  308.  
  309.     if (avctx->codec_id == AV_CODEC_ID_VC1 &&
  310.         size >= 4 && IS_MARKER(AV_RB32(buffer))) {
  311.         buffer += 4;
  312.         size   -= 4;
  313.     }
  314.  
  315.     ctx_pic->bitstream_size = size;
  316.     ctx_pic->bitstream      = buffer;
  317.  
  318.     fill_slice(avctx, &ctx_pic->si, 0, size);
  319.     return 0;
  320. }
  321.  
  322. static int dxva2_vc1_end_frame(AVCodecContext *avctx)
  323. {
  324.     VC1Context *v = avctx->priv_data;
  325.     struct dxva2_picture_context *ctx_pic = v->s.current_picture_ptr->hwaccel_picture_private;
  326.     int ret;
  327.  
  328.     if (ctx_pic->bitstream_size <= 0)
  329.         return -1;
  330.  
  331.     ret = ff_dxva2_common_end_frame(avctx, v->s.current_picture_ptr->f,
  332.                                     &ctx_pic->pp, sizeof(ctx_pic->pp),
  333.                                     NULL, 0,
  334.                                     commit_bitstream_and_slice_buffer);
  335.     if (!ret)
  336.         ff_mpeg_draw_horiz_band(&v->s, 0, avctx->height);
  337.     return ret;
  338. }
  339.  
  340. #if CONFIG_WMV3_DXVA2_HWACCEL
  341. AVHWAccel ff_wmv3_dxva2_hwaccel = {
  342.     .name           = "wmv3_dxva2",
  343.     .type           = AVMEDIA_TYPE_VIDEO,
  344.     .id             = AV_CODEC_ID_WMV3,
  345.     .pix_fmt        = AV_PIX_FMT_DXVA2_VLD,
  346.     .start_frame    = dxva2_vc1_start_frame,
  347.     .decode_slice   = dxva2_vc1_decode_slice,
  348.     .end_frame      = dxva2_vc1_end_frame,
  349.     .frame_priv_data_size = sizeof(struct dxva2_picture_context),
  350. };
  351. #endif
  352.  
  353. #if CONFIG_VC1_DXVA2_HWACCEL
  354. AVHWAccel ff_vc1_dxva2_hwaccel = {
  355.     .name           = "vc1_dxva2",
  356.     .type           = AVMEDIA_TYPE_VIDEO,
  357.     .id             = AV_CODEC_ID_VC1,
  358.     .pix_fmt        = AV_PIX_FMT_DXVA2_VLD,
  359.     .start_frame    = dxva2_vc1_start_frame,
  360.     .decode_slice   = dxva2_vc1_decode_slice,
  361.     .end_frame      = dxva2_vc1_end_frame,
  362.     .frame_priv_data_size = sizeof(struct dxva2_picture_context),
  363. };
  364. #endif
  365.  
  366. #if CONFIG_WMV3_D3D11VA_HWACCEL
  367. AVHWAccel ff_wmv3_d3d11va_hwaccel = {
  368.     .name           = "wmv3_d3d11va",
  369.     .type           = AVMEDIA_TYPE_VIDEO,
  370.     .id             = AV_CODEC_ID_WMV3,
  371.     .pix_fmt        = AV_PIX_FMT_D3D11VA_VLD,
  372.     .start_frame    = dxva2_vc1_start_frame,
  373.     .decode_slice   = dxva2_vc1_decode_slice,
  374.     .end_frame      = dxva2_vc1_end_frame,
  375.     .frame_priv_data_size = sizeof(struct dxva2_picture_context),
  376. };
  377. #endif
  378.  
  379. #if CONFIG_VC1_D3D11VA_HWACCEL
  380. AVHWAccel ff_vc1_d3d11va_hwaccel = {
  381.     .name           = "vc1_d3d11va",
  382.     .type           = AVMEDIA_TYPE_VIDEO,
  383.     .id             = AV_CODEC_ID_VC1,
  384.     .pix_fmt        = AV_PIX_FMT_D3D11VA_VLD,
  385.     .start_frame    = dxva2_vc1_start_frame,
  386.     .decode_slice   = dxva2_vc1_decode_slice,
  387.     .end_frame      = dxva2_vc1_end_frame,
  388.     .frame_priv_data_size = sizeof(struct dxva2_picture_context),
  389. };
  390. #endif
  391.