avcodec: Implement vc1 nvdec hwaccel
authorPhilip Langdale <philipl@overt.org>
Tue, 14 Nov 2017 05:09:27 +0000 (21:09 -0800)
committerPhilip Langdale <philipl@overt.org>
Wed, 15 Nov 2017 03:40:01 +0000 (19:40 -0800)
This hwaccel is interesting because it also works for wmv3/9 content,
which is not supported by the nvidia parser used by cuviddec.

Changelog
configure
libavcodec/Makefile
libavcodec/allcodecs.c
libavcodec/nvdec.c
libavcodec/nvdec_vc1.c [new file with mode: 0644]
libavcodec/vc1dec.c
libavcodec/version.h

index 68829f2..d2b5530 100644 (file)
--- a/Changelog
+++ b/Changelog
@@ -13,7 +13,7 @@ version <next>:
 - PCE support for extended channel layouts in the AAC encoder
 - native aptX encoder and decoder
 - Raw aptX muxer and demuxer
-- NVIDIA NVDEC-accelerated H.264, HEVC and VP9 hwaccel decoding
+- NVIDIA NVDEC-accelerated H.264, HEVC, VC1 and VP9 hwaccel decoding
 - Intel QSV-accelerated overlay filter
 
 
index 3788f26..934ac3a 100755 (executable)
--- a/configure
+++ b/configure
@@ -2740,6 +2740,8 @@ vc1_d3d11va2_hwaccel_select="vc1_decoder"
 vc1_dxva2_hwaccel_deps="dxva2"
 vc1_dxva2_hwaccel_select="vc1_decoder"
 vc1_mmal_hwaccel_deps="mmal"
+vc1_nvdec_hwaccel_deps="nvdec"
+vc1_nvdec_hwaccel_select="vc1_decoder"
 vc1_qsv_hwaccel_deps="libmfx"
 vc1_vaapi_hwaccel_deps="vaapi"
 vc1_vaapi_hwaccel_select="vc1_decoder"
@@ -2763,6 +2765,7 @@ vp9_vaapi_hwaccel_select="vp9_decoder"
 wmv3_d3d11va_hwaccel_select="vc1_d3d11va_hwaccel"
 wmv3_d3d11va2_hwaccel_select="vc1_d3d11va2_hwaccel"
 wmv3_dxva2_hwaccel_select="vc1_dxva2_hwaccel"
+wmv3_nvdec_hwaccel_select="vc1_nvdec_hwaccel"
 wmv3_vaapi_hwaccel_select="vc1_vaapi_hwaccel"
 wmv3_vdpau_hwaccel_select="vc1_vdpau_hwaccel"
 
index 2476aec..6315672 100644 (file)
@@ -864,6 +864,7 @@ OBJS-$(CONFIG_MPEG4_VDPAU_HWACCEL)        += vdpau_mpeg4.o
 OBJS-$(CONFIG_MPEG4_VIDEOTOOLBOX_HWACCEL) += videotoolbox.o
 OBJS-$(CONFIG_VC1_D3D11VA_HWACCEL)        += dxva2_vc1.o
 OBJS-$(CONFIG_VC1_DXVA2_HWACCEL)          += dxva2_vc1.o
+OBJS-$(CONFIG_VC1_NVDEC_HWACCEL)          += nvdec_vc1.o
 OBJS-$(CONFIG_VC1_QSV_HWACCEL)            += qsvdec_other.o
 OBJS-$(CONFIG_VC1_VAAPI_HWACCEL)          += vaapi_vc1.o
 OBJS-$(CONFIG_VC1_VDPAU_HWACCEL)          += vdpau_vc1.o
index 0781862..e213f37 100644 (file)
@@ -111,6 +111,7 @@ static void register_all(void)
     REGISTER_HWACCEL(VC1_D3D11VA,       vc1_d3d11va);
     REGISTER_HWACCEL(VC1_D3D11VA2,      vc1_d3d11va2);
     REGISTER_HWACCEL(VC1_DXVA2,         vc1_dxva2);
+    REGISTER_HWACCEL(VC1_NVDEC,         vc1_nvdec);
     REGISTER_HWACCEL(VC1_VAAPI,         vc1_vaapi);
     REGISTER_HWACCEL(VC1_VDPAU,         vc1_vdpau);
     REGISTER_HWACCEL(VC1_MMAL,          vc1_mmal);
@@ -128,6 +129,7 @@ static void register_all(void)
     REGISTER_HWACCEL(WMV3_D3D11VA,      wmv3_d3d11va);
     REGISTER_HWACCEL(WMV3_D3D11VA2,     wmv3_d3d11va2);
     REGISTER_HWACCEL(WMV3_DXVA2,        wmv3_dxva2);
+    REGISTER_HWACCEL(WMV3_NVDEC,        wmv3_nvdec);
     REGISTER_HWACCEL(WMV3_VAAPI,        wmv3_vaapi);
     REGISTER_HWACCEL(WMV3_VDPAU,        wmv3_vdpau);
 
index ac68fac..20d7c3d 100644 (file)
@@ -54,7 +54,9 @@ static int map_avcodec_id(enum AVCodecID id)
     switch (id) {
     case AV_CODEC_ID_H264: return cudaVideoCodec_H264;
     case AV_CODEC_ID_HEVC: return cudaVideoCodec_HEVC;
+    case AV_CODEC_ID_VC1:  return cudaVideoCodec_VC1;
     case AV_CODEC_ID_VP9:  return cudaVideoCodec_VP9;
+    case AV_CODEC_ID_WMV3:  return cudaVideoCodec_VC1;
     }
     return -1;
 }
diff --git a/libavcodec/nvdec_vc1.c b/libavcodec/nvdec_vc1.c
new file mode 100644 (file)
index 0000000..cf75ba5
--- /dev/null
@@ -0,0 +1,184 @@
+/*
+ * VC1 HW decode acceleration through NVDEC
+ *
+ * Copyright (c) 2017 Philip Langdale
+ *
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * FFmpeg is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with FFmpeg; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#include "avcodec.h"
+#include "nvdec.h"
+#include "decode.h"
+#include "vc1.h"
+
+static unsigned char get_ref_idx(AVFrame *frame)
+{
+    FrameDecodeData *fdd;
+    NVDECFrame *cf;
+
+    if (!frame || !frame->private_ref)
+        return 255;
+
+    fdd = (FrameDecodeData*)frame->private_ref->data;
+    cf  = (NVDECFrame*)fdd->hwaccel_priv;
+
+    return cf->idx;
+}
+
+static int nvdec_vc1_start_frame(AVCodecContext *avctx, const uint8_t *buffer, uint32_t size)
+{
+    VC1Context *v = avctx->priv_data;
+    MpegEncContext *s = &v->s;
+
+    NVDECContext      *ctx = avctx->internal->hwaccel_priv_data;
+    CUVIDPICPARAMS     *pp = &ctx->pic_params;
+    FrameDecodeData *fdd;
+    NVDECFrame *cf;
+    AVFrame *cur_frame = s->current_picture.f;
+
+    int ret;
+
+    ret = ff_nvdec_start_frame(avctx, cur_frame);
+    if (ret < 0)
+        return ret;
+
+    fdd = (FrameDecodeData*)cur_frame->private_ref->data;
+    cf  = (NVDECFrame*)fdd->hwaccel_priv;
+
+    *pp = (CUVIDPICPARAMS) {
+        .PicWidthInMbs     = (cur_frame->width  + 15) / 16,
+        .FrameHeightInMbs  = (cur_frame->height + 15) / 16,
+        .CurrPicIdx        = cf->idx,
+        .field_pic_flag    = v->field_mode,
+        .bottom_field_flag = v->cur_field_type,
+        .second_field      = v->second_field,
+
+        .intra_pic_flag    = s->pict_type == AV_PICTURE_TYPE_I ||
+                             s->pict_type == AV_PICTURE_TYPE_BI,
+        .ref_pic_flag      = s->pict_type == AV_PICTURE_TYPE_I ||
+                             s->pict_type == AV_PICTURE_TYPE_P,
+
+        .CodecSpecific.vc1 = {
+            .ForwardRefIdx     = get_ref_idx(s->last_picture.f),
+            .BackwardRefIdx    = get_ref_idx(s->next_picture.f),
+            .FrameWidth        = cur_frame->width,
+            .FrameHeight       = cur_frame->height,
+
+            .intra_pic_flag    = s->pict_type == AV_PICTURE_TYPE_I ||
+                                 s->pict_type == AV_PICTURE_TYPE_BI,
+            .ref_pic_flag      = s->pict_type == AV_PICTURE_TYPE_I ||
+                                 s->pict_type == AV_PICTURE_TYPE_P,
+            .progressive_fcm   = v->fcm == 0,
+
+            .profile           = v->profile,
+            .postprocflag      = v->postprocflag,
+            .pulldown          = v->broadcast,
+            .interlace         = v->interlace,
+            .tfcntrflag        = v->tfcntrflag,
+            .finterpflag       = v->finterpflag,
+            .psf               = v->psf,
+            .multires          = v->multires,
+            .syncmarker        = v->resync_marker,
+            .rangered          = v->rangered,
+            .maxbframes        = s->max_b_frames,
+
+            .panscan_flag      = v->panscanflag,
+            .refdist_flag      = v->refdist_flag,
+            .extended_mv       = v->extended_mv,
+            .dquant            = v->dquant,
+            .vstransform       = v->vstransform,
+            .loopfilter        = v->s.loop_filter,
+            .fastuvmc          = v->fastuvmc,
+            .overlap           = v->overlap,
+            .quantizer         = v->quantizer_mode,
+            .extended_dmv      = v->extended_dmv,
+            .range_mapy_flag   = v->range_mapy_flag,
+            .range_mapy        = v->range_mapy,
+            .range_mapuv_flag  = v->range_mapuv_flag,
+            .range_mapuv       = v->range_mapuv,
+            .rangeredfrm       = v->rangeredfrm,
+        }
+    };
+
+    return 0;
+}
+
+static int nvdec_vc1_end_frame(AVCodecContext *avctx)
+{
+    NVDECContext *ctx = avctx->internal->hwaccel_priv_data;
+    int ret = ff_nvdec_end_frame(avctx);
+    ctx->bitstream = NULL;
+    return ret;
+}
+
+static int nvdec_vc1_decode_slice(AVCodecContext *avctx, const uint8_t *buffer, uint32_t size)
+{
+    NVDECContext *ctx = avctx->internal->hwaccel_priv_data;
+    void *tmp;
+
+    tmp = av_fast_realloc(ctx->slice_offsets, &ctx->slice_offsets_allocated,
+                          (ctx->nb_slices + 1) * sizeof(*ctx->slice_offsets));
+    if (!tmp)
+        return AVERROR(ENOMEM);
+    ctx->slice_offsets = tmp;
+
+    if (!ctx->bitstream)
+        ctx->bitstream = (uint8_t*)buffer;
+
+    ctx->slice_offsets[ctx->nb_slices] = buffer - ctx->bitstream;
+    ctx->bitstream_len += size;
+    ctx->nb_slices++;
+
+    return 0;
+}
+
+static int nvdec_vc1_frame_params(AVCodecContext *avctx,
+                                  AVBufferRef *hw_frames_ctx)
+{
+    // Each frame can at most have one P and one B reference
+    return ff_nvdec_frame_params(avctx, hw_frames_ctx, 2);
+}
+
+AVHWAccel ff_vc1_nvdec_hwaccel = {
+    .name                 = "vc1_nvdec",
+    .type                 = AVMEDIA_TYPE_VIDEO,
+    .id                   = AV_CODEC_ID_VC1,
+    .pix_fmt              = AV_PIX_FMT_CUDA,
+    .start_frame          = nvdec_vc1_start_frame,
+    .end_frame            = nvdec_vc1_end_frame,
+    .decode_slice         = nvdec_vc1_decode_slice,
+    .frame_params         = nvdec_vc1_frame_params,
+    .init                 = ff_nvdec_decode_init,
+    .uninit               = ff_nvdec_decode_uninit,
+    .priv_data_size       = sizeof(NVDECContext),
+};
+
+#if CONFIG_WMV3_NVDEC_HWACCEL
+AVHWAccel ff_wmv3_nvdec_hwaccel = {
+    .name                 = "wmv3_nvdec",
+    .type                 = AVMEDIA_TYPE_VIDEO,
+    .id                   = AV_CODEC_ID_WMV3,
+    .pix_fmt              = AV_PIX_FMT_CUDA,
+    .start_frame          = nvdec_vc1_start_frame,
+    .end_frame            = nvdec_vc1_end_frame,
+    .decode_slice         = nvdec_vc1_decode_slice,
+    .frame_params         = nvdec_vc1_frame_params,
+    .init                 = ff_nvdec_decode_init,
+    .uninit               = ff_nvdec_decode_uninit,
+    .priv_data_size       = sizeof(NVDECContext),
+};
+#endif
index 6bdaeca..96b8bb5 100644 (file)
@@ -1119,6 +1119,9 @@ static const enum AVPixelFormat vc1_hwaccel_pixfmt_list_420[] = {
     AV_PIX_FMT_D3D11VA_VLD,
     AV_PIX_FMT_D3D11,
 #endif
+#if CONFIG_VC1_NVDEC_HWACCEL
+    AV_PIX_FMT_CUDA,
+#endif
 #if CONFIG_VC1_VAAPI_HWACCEL
     AV_PIX_FMT_VAAPI,
 #endif
index 9d1b0ee..88500e3 100644 (file)
@@ -29,7 +29,7 @@
 
 #define LIBAVCODEC_VERSION_MAJOR  58
 #define LIBAVCODEC_VERSION_MINOR   3
-#define LIBAVCODEC_VERSION_MICRO 101
+#define LIBAVCODEC_VERSION_MICRO 102
 
 #define LIBAVCODEC_VERSION_INT  AV_VERSION_INT(LIBAVCODEC_VERSION_MAJOR, \
                                                LIBAVCODEC_VERSION_MINOR, \