[FFmpeg-devel] Added HW H.264 and HEVC encoding for AMD GPUs based on AMF SDK

Mark Thompson sw at jkqxz.net
Sun Nov 12 20:21:55 EET 2017


On 05/11/17 03:49, Mikhail Mironov wrote:
> From fc6a3f63eb9c3734f4101cee2a2f5707e063ab62 Mon Sep 17 00:00:00 2001
> From: mmironov <mikhail.mironov at amd.com>
> Date: Fri, 27 Oct 2017 13:03:15 -0400
> Subject: [PATCH] Added: HW accelerated H.264 and HEVC encoding for AMD GPUs
>  based on AMF SDK
> 
> Signed-off-by: mmironov <mikhail.mironov at amd.com>
> ---
>  Changelog                |    3 +-
>  compat/amd/amfsdkenc.h   | 1753 ++++++++++++++++++++++++++++++++++++++++++++++
>  configure                |   25 +
>  libavcodec/Makefile      |    4 +
>  libavcodec/allcodecs.c   |    2 +
>  libavcodec/amfenc.c      |  515 ++++++++++++++
>  libavcodec/amfenc.h      |  137 ++++
>  libavcodec/amfenc_h264.c |  366 ++++++++++
>  libavcodec/amfenc_hevc.c |  294 ++++++++
>  libavcodec/version.h     |    4 +-
>  10 files changed, 3100 insertions(+), 3 deletions(-)
>  create mode 100644 compat/amd/amfsdkenc.h
>  create mode 100644 libavcodec/amfenc.c
>  create mode 100644 libavcodec/amfenc.h
>  create mode 100644 libavcodec/amfenc_h264.c
>  create mode 100644 libavcodec/amfenc_hevc.c
> 
> ...
> diff --git a/libavcodec/amfenc.c b/libavcodec/amfenc.c
> new file mode 100644
> index 0000000..fcfbd20
> --- /dev/null
> +++ b/libavcodec/amfenc.c
> ...
> +
> +static int amf_init_context(AVCodecContext *avctx)
> +{
> +    AmfContext         *ctx = avctx->priv_data;
> +    AMF_RESULT          res = AMF_OK;
> +
> +    // confugure AMF logger
> +    // the return of these functions indicates old state and do not affect behaviour
> +    ctx->trace->pVtbl->EnableWriter(ctx->trace, AMF_TRACE_WRITER_DEBUG_OUTPUT, ctx->log_to_dbg != 0 );
> +    if (ctx->log_to_dbg)
> +        ctx->trace->pVtbl->SetWriterLevel(ctx->trace, AMF_TRACE_WRITER_DEBUG_OUTPUT, AMF_TRACE_TRACE);
> +    ctx->trace->pVtbl->EnableWriter(ctx->trace, AMF_TRACE_WRITER_CONSOLE, 0);
> +    ctx->trace->pVtbl->SetGlobalLevel(ctx->trace, AMF_TRACE_TRACE);
> +
> +    // connect AMF logger to av_log
> +    ctx->tracer.vtbl = &tracer_vtbl;
> +    ctx->tracer.avctx = avctx;
> +    ctx->trace->pVtbl->RegisterWriter(ctx->trace, FFMPEG_AMF_WRITER_ID,(AMFTraceWriter*)&ctx->tracer, 1);
> +    ctx->trace->pVtbl->SetWriterLevel(ctx->trace, FFMPEG_AMF_WRITER_ID, AMF_TRACE_TRACE);
> +
> +    res = ctx->factory->pVtbl->CreateContext(ctx->factory, &ctx->context);
> +    AMF_RETURN_IF_FALSE(ctx, res == AMF_OK, AVERROR_UNKNOWN, "CreateContext() failed with error %d\n", res);
> +    // try to reuse existing DX device
> +    if (avctx->hw_frames_ctx) {
> +        AVHWFramesContext *device_ctx = (AVHWFramesContext*)avctx->hw_frames_ctx->data;
> +        if (device_ctx->device_ctx->type == AV_HWDEVICE_TYPE_D3D11VA){
> +            if (amf_av_to_amf_format(device_ctx->sw_format) == AMF_SURFACE_UNKNOWN) {

This test is inverted.

Have you actually tested this path?  Even with that test fixed, I'm unable to pass the following initialisation test with an AMD D3D11 device.

> +                if (device_ctx->device_ctx->hwctx) {
> +                    AVD3D11VADeviceContext *device_d3d11 = (AVD3D11VADeviceContext *)device_ctx->device_ctx->hwctx;
> +                    res = ctx->context->pVtbl->InitDX11(ctx->context, device_d3d11->device, AMF_DX11_1);
> +                    if (res == AMF_OK) {
> +                        ctx->hw_frames_ctx = av_buffer_ref(avctx->hw_frames_ctx);
> +                    }else {
> +                        av_log(avctx, AV_LOG_INFO, "amf_shared: avctx->hw_frames_ctx has non-AMD device, switching to default\n");
> +                    }
> +                }
> +            }else {
> +                av_log(avctx, AV_LOG_INFO, "amf_shared: avctx->hw_frames_ctx has format not uspported by AMF, switching to default\n");
> +            }
> +        }
> +    } else if (avctx->hw_device_ctx) {
> +        AVHWDeviceContext *device_ctx = (AVHWDeviceContext*)(avctx->hw_device_ctx->data);
> +        if (device_ctx->type == AV_HWDEVICE_TYPE_D3D11VA) {
> +            if (device_ctx->hwctx) {
> +                AVD3D11VADeviceContext *device_d3d11 = (AVD3D11VADeviceContext *)device_ctx->hwctx;
> +                res = ctx->context->pVtbl->InitDX11(ctx->context, device_d3d11->device, AMF_DX11_1);
> +                if (res == AMF_OK) {
> +                    ctx->hw_device_ctx = av_buffer_ref(avctx->hw_device_ctx);
> +                } else {
> +                    av_log(avctx, AV_LOG_INFO, "amf_shared: avctx->hw_device_ctx has non-AMD device, switching to default\n");
> +                }
> +            }
> +        }
> +    }
> +    if (!ctx->hw_frames_ctx && !ctx->hw_device_ctx) {
> +        res = ctx->context->pVtbl->InitDX11(ctx->context, NULL, AMF_DX11_1);
> +        if (res != AMF_OK) {
> +            res = ctx->context->pVtbl->InitDX9(ctx->context, NULL);
> +            AMF_RETURN_IF_FALSE(ctx, res == AMF_OK, AVERROR_UNKNOWN, "InitDX9() failed with error %d\n", res);
> +        }
> +    }
> +    return 0;
> +}
> +
> +static int amf_init_encoder(AVCodecContext *avctx)
> +{
> +    AmfContext          *ctx = avctx->priv_data;
> +    const wchar_t       *codec_id = NULL;
> +    AMF_RESULT           res = AMF_OK;
> +
> +    switch (avctx->codec->id) {
> +        case AV_CODEC_ID_H264:
> +            codec_id = AMFVideoEncoderVCE_AVC;
> +            break;
> +        case AV_CODEC_ID_HEVC:
> +            codec_id = AMFVideoEncoder_HEVC;
> +            break;
> +        default:
> +            break;
> +    }
> +    AMF_RETURN_IF_FALSE(ctx, codec_id != NULL, AVERROR(EINVAL), "Codec %d is not supported\n", avctx->codec->id);
> +
> +    ctx->format = amf_av_to_amf_format(avctx->pix_fmt);
> +    AMF_RETURN_IF_FALSE(ctx, ctx->format != AMF_SURFACE_UNKNOWN, AVERROR(EINVAL), "Format %d is not supported\n", avctx->pix_fmt);
> +
> +    res = ctx->factory->pVtbl->CreateComponent(ctx->factory, ctx->context, codec_id, &ctx->encoder);
> +    AMF_RETURN_IF_FALSE(ctx, res == AMF_OK, AVERROR_ENCODER_NOT_FOUND, "CreateComponent(%ls) failed with error %d\n", codec_id, res);
> +
> +    return 0;
> +}
> +
> +int av_cold ff_amf_encode_close(AVCodecContext *avctx)
> +{
> +    AmfContext      *ctx = avctx->priv_data;
> +    if (ctx->delayed_surface)
> +    {
> +        ctx->delayed_surface->pVtbl->Release(ctx->delayed_surface);
> +        ctx->delayed_surface = NULL;
> +    }
> +
> +    if (ctx->encoder) {
> +        ctx->encoder->pVtbl->Terminate(ctx->encoder);
> +        ctx->encoder->pVtbl->Release(ctx->encoder);
> +        ctx->encoder = NULL;
> +    }
> +
> +    if (ctx->context) {
> +        ctx->context->pVtbl->Terminate(ctx->context);
> +        ctx->context->pVtbl->Release(ctx->context);
> +        ctx->context = NULL;
> +    }
> +    av_buffer_unref(&ctx->hw_device_ctx);
> +    av_buffer_unref(&ctx->hw_frames_ctx);
> +
> +    if (ctx->trace) {
> +        ctx->trace->pVtbl->UnregisterWriter(ctx->trace, FFMPEG_AMF_WRITER_ID);
> +    }
> +    if (ctx->library) {
> +        dlclose(ctx->library);
> +        ctx->library = NULL;
> +    }
> +    ctx->trace = NULL;
> +    ctx->debug = NULL;
> +    ctx->factory = NULL;
> +    ctx->version = 0;
> +    ctx->delayed_drain = 0;
> +    av_frame_free(&ctx->delayed_frame);
> +
> +    return 0;
> +}
> +
> +static int amf_copy_surface(AVCodecContext *avctx, const AVFrame *frame,
> +    AMFSurface* surface)
> +{
> +    AVFrame        *sw_frame = NULL;
> +    AMFPlane       *plane = NULL;
> +    uint8_t        *dst_data[4];
> +    int             dst_linesize[4];
> +    int             ret = 0;
> +    int             planes;
> +
> +    if (frame->hw_frames_ctx && is_hwaccel_pix_fmt(frame->format)) {
> +        if (!(sw_frame = av_frame_alloc())) {
> +            av_log(avctx, AV_LOG_ERROR, "Can not alloc frame\n");
> +            ret = AVERROR(ENOMEM);
> +            goto fail;
> +        }
> +        if ((ret = av_hwframe_transfer_data(sw_frame, frame, 0)) < 0) {
> +            av_log(avctx, AV_LOG_ERROR, "Error transferring the data to system memory\n");
> +            ret = AVERROR(EINVAL);
> +            goto fail;
> +        }
> +        frame = sw_frame;
> +    }
> +    planes = (int)surface->pVtbl->GetPlanesCount(surface);
> +    if (planes > amf_countof(dst_data)) {
> +        av_log(avctx, AV_LOG_ERROR, "Invalid number of planes %d in surface\n", planes);
> +        ret = AVERROR(EINVAL);
> +        goto fail;
> +    }
> +
> +    for (int i = 0; i < planes; i++) {
> +        plane = surface->pVtbl->GetPlaneAt(surface, i);
> +        dst_data[i] = plane->pVtbl->GetNative(plane);
> +        dst_linesize[i] = plane->pVtbl->GetHPitch(plane);
> +    }
> +    av_image_copy(dst_data, dst_linesize,
> +        (const uint8_t**)frame->data, frame->linesize, frame->format,
> +        avctx->width, avctx->height);
> +
> +fail:
> +    if (sw_frame){
> +        av_frame_free(&sw_frame);
> +    }
> +    return ret;
> +}
> +
> +static int amf_copy_buffer(AVCodecContext *avctx, AVPacket *pkt, AMFBuffer *buffer)
> +{
> +    int                 ret;
> +    AMFVariantStruct    var = {0};
> +    int64_t                 size = buffer->pVtbl->GetSize(buffer);
> +
> +    if (ret = ff_alloc_packet2(avctx, pkt, size, 0) < 0) {
> +        return ret;
> +    }
> +    memcpy(pkt->data, buffer->pVtbl->GetNative(buffer), size);
> +
> +    switch (avctx->codec->id) {
> +        case AV_CODEC_ID_H264:
> +            buffer->pVtbl->GetProperty(buffer, AMF_VIDEO_ENCODER_OUTPUT_DATA_TYPE, &var);
> +            if(var.int64Value == AMF_VIDEO_ENCODER_OUTPUT_DATA_TYPE_IDR) {
> +                pkt->flags = AV_PKT_FLAG_KEY;
> +            }
> +            break;
> +        case AV_CODEC_ID_HEVC:
> +            buffer->pVtbl->GetProperty(buffer, AMF_VIDEO_ENCODER_HEVC_OUTPUT_DATA_TYPE, &var);
> +            if (var.int64Value == AMF_VIDEO_ENCODER_HEVC_OUTPUT_DATA_TYPE_IDR) {
> +                pkt->flags = AV_PKT_FLAG_KEY;
> +            }
> +            break;
> +        default:
> +            break;
> +    }
> +
> +    buffer->pVtbl->GetProperty(buffer, PTS_PROP, &var);
> +
> +    pkt->pts = var.int64Value; // original pts
> +    pkt->dts = buffer->pVtbl->GetPts(buffer); // in monotonic order

This generates invalid dts values.  dts cannot be greater than pts (that is, a frame cannot be decoded after its intended presentation time).

E.g. for the three-frame stream made by "./ffmpeg_g -y -f lavfi -i testsrc -an -c:v h264_amf -bsf:v trace_headers -frames:v 3 -bf 1 out.mp4" we get the three packets:

pts 0     dts 0
pts 1024  dts 512
pts 512   dts 1024

and the mp4 muxer correctly complains "Invalid DTS: 1024 PTS: 512 in output stream 0:0, replacing by guess".

> +
> +    return 0;
> +}
> +
> +// amfenc API implmentation
> +int ff_amf_encode_init(AVCodecContext *avctx)
> +{
> +    AmfContext     *ctx = avctx->priv_data;
> +    int             ret;
> +
> +    ctx->factory = NULL;
> +    ctx->debug = NULL;
> +    ctx->trace = NULL;
> +    ctx->context = NULL;
> +    ctx->encoder = NULL;
> +    ctx->library = NULL;
> +    ctx->version = 0;
> +    ctx->eof = 0;
> +    ctx->format = 0;
> +    ctx->tracer.vtbl = NULL;
> +    ctx->tracer.avctx = NULL;
> +
> +    if ((ret = amf_load_library(avctx)) == 0) {
> +        if ((ret = amf_init_context(avctx)) == 0) {
> +            if ((ret = amf_init_encoder(avctx)) == 0) {
> +                return 0;
> +            }
> +        }
> +    }
> +    ff_amf_encode_close(avctx);
> +    return ret;
> +}
> +
> +
> +int ff_amf_send_frame(AVCodecContext *avctx, const AVFrame *frame)
> +{
> +    AMF_RESULT      res = AMF_OK;
> +    AmfContext     *ctx = avctx->priv_data;
> +    AMFSurface     *surface = NULL;
> +
> +    if (!ctx->encoder)
> +        return AVERROR(EINVAL);
> +
> +    if (!frame) { // submit drain
> +        if (!ctx->eof) { // submit drain one time only
> +            if (ctx->delayed_surface != NULL) {
> +                ctx->delayed_drain = 1; // input queue is full: resubmit Drain() in ff_amf_receive_packet
> +            } else if(!ctx->delayed_drain){
> +                res = ctx->encoder->pVtbl->Drain(ctx->encoder);
> +                if (res == AMF_INPUT_FULL) {
> +                    ctx->delayed_drain = 1; // input queue is full: resubmit Drain() in ff_amf_receive_packet
> +                }else {
> +                    if (res == AMF_OK) {
> +                        ctx->eof = 1; // drain started
> +                    }
> +                    AMF_RETURN_IF_FALSE(ctx, res == AMF_OK, AVERROR_UNKNOWN, "Drain() failed with error %d\n", res);
> +                }
> +            }
> +        }else{
> +            return AVERROR_EOF;
> +        }
> +    } else { // submit frame
> +        if (ctx->delayed_surface != NULL) {
> +            return AVERROR(EAGAIN); // should not happen when called from ffmpeg, other clients may resubmit
> +        }
> +        // prepare surface from frame
> +        if (frame->hw_frames_ctx && ( // HW frame detected
> +            // check if the same hw_frames_ctx as used in initialization
> +            (ctx->hw_frames_ctx && frame->hw_frames_ctx->data == ctx->hw_frames_ctx->data) ||
> +            // check if the same hw_device_ctx as used in initialization
> +            (ctx->hw_device_ctx && ((AVHWFramesContext*)frame->hw_frames_ctx->data)->device_ctx ==
> +            (AVHWDeviceContext*)ctx->hw_device_ctx->data)
> +        )) {
> +            GUID             AMFTextureArrayIndexGUID = AMFTextureArrayIndexGUIDDef;
> +            ID3D11Texture2D *texture = (ID3D11Texture2D*)frame->data[0]; // actual texture
> +            int index = (int)(size_t)frame->data[1]; // index is a slice in texture array is - set to tell AMF which slice to use
> +            texture->lpVtbl->SetPrivateData(texture, &AMFTextureArrayIndexGUID, sizeof(index), &index);
> +
> +            res = ctx->context->pVtbl->CreateSurfaceFromDX11Native(ctx->context, texture, &surface, NULL); // wrap to AMF surface
> +            AMF_RETURN_IF_FALSE(ctx, res == AMF_OK, AVERROR(ENOMEM), "CreateSurfaceFromDX11Native() failed  with error %d\n", res);
> +
> +            // input HW surfaces can be vertically aligned by 16; tell AMF the real size
> +            surface->pVtbl->SetCrop(surface, 0, 0, frame->width, frame->height);
> +        } else {
> +            res = ctx->context->pVtbl->AllocSurface(ctx->context, AMF_MEMORY_HOST, ctx->format, avctx->width, avctx->height, &surface);
> +            AMF_RETURN_IF_FALSE(ctx, res == AMF_OK, AVERROR(ENOMEM), "AllocSurface() failed  with error %d\n", res);
> +            amf_copy_surface(avctx, frame, surface);
> +        }
> +        surface->pVtbl->SetPts(surface, frame->pts);
> +        AMF_ASSIGN_PROPERTY_INT64(res, surface, PTS_PROP, frame->pts);
> +
> +        // submit surface
> +        res = ctx->encoder->pVtbl->SubmitInput(ctx->encoder, (AMFData*)surface);
> +        if (res == AMF_INPUT_FULL) { // handle full queue
> +            //store surface for later submission
> +            ctx->delayed_surface = surface;
> +            if (surface->pVtbl->GetMemoryType(surface) == AMF_MEMORY_DX11) {
> +                av_frame_ref(ctx->delayed_frame, frame);
> +            }
> +        }
> +        else {
> +            surface->pVtbl->Release(surface);
> +            AMF_RETURN_IF_FALSE(ctx, res == AMF_OK, AVERROR_UNKNOWN, "SubmitInput() failed with error %d\n", res);
> +        }
> +    }
> +    return 0;
> +}
> +int ff_amf_receive_packet(AVCodecContext *avctx, AVPacket *avpkt)
> +{
> +    int             ret;
> +    AMF_RESULT      res;
> +    AMF_RESULT      res_query;
> +    AmfContext     *ctx = avctx->priv_data;
> +    AMFData        *data = NULL;
> +    int             block_and_wait;
> +
> +    if (!ctx->encoder)
> +        return AVERROR(EINVAL);
> +
> +    do {
> +        block_and_wait = 0;
> +        // poll data
> +        res_query = ctx->encoder->pVtbl->QueryOutput(ctx->encoder, &data);
> +        if (data) {
> +            // copy data to packet
> +            AMFBuffer* buffer;
> +            AMFGuid guid = IID_AMFBuffer();
> +            data->pVtbl->QueryInterface(data, &guid, (void**)&buffer); // query for buffer interface
> +            ret = amf_copy_buffer(avctx, avpkt, buffer);
> +
> +            buffer->pVtbl->Release(buffer);
> +            data->pVtbl->Release(data);
> +
> +            AMF_RETURN_IF_FALSE(ctx, ret >= 0, ret, "amf_copy_buffer() failed with error %d\n", ret);
> +
> +            if (ctx->delayed_surface != NULL) { // try to resubmit frame
> +                res = ctx->encoder->pVtbl->SubmitInput(ctx->encoder, (AMFData*)ctx->delayed_surface);
> +                if (res != AMF_INPUT_FULL) {
> +                    ctx->delayed_surface->pVtbl->Release(ctx->delayed_surface);
> +                    ctx->delayed_surface = NULL;
> +                    av_frame_unref(ctx->delayed_frame);
> +
> +                    AMF_RETURN_IF_FALSE(ctx, res == AMF_OK, AVERROR_UNKNOWN, "Repeated SubmitInput() failed with error %d\n", res);
> +                }else {
> +                    av_log(avctx, AV_LOG_WARNING, "Data acquired but delayed frame submission got AMF_INPUT_FULL- should not happen\n");
> +                }
> +            }else if (ctx->delayed_drain) { // try to resubmit drain
> +                res = ctx->encoder->pVtbl->Drain(ctx->encoder);
> +                if (res != AMF_INPUT_FULL) {
> +                    ctx->delayed_drain = 0;
> +                    ctx->eof = 1; // drain started
> +                    AMF_RETURN_IF_FALSE(ctx, res == AMF_OK, AVERROR_UNKNOWN, "Repeated Drain() failed with error %d\n", res);
> +                }else {
> +                    av_log(avctx, AV_LOG_WARNING, "Data acquired but delayed drain submission got AMF_INPUT_FULL- should not happen\n");
> +                }
> +            }
> +        }else if (ctx->delayed_surface != NULL || ctx->delayed_drain || (ctx->eof && res_query != AMF_EOF)) {
> +            block_and_wait = 1;
> +            av_usleep(1000); // wait and poll again
> +        }
> +    } while (block_and_wait);
> +
> +    if (res_query == AMF_EOF) {
> +        ret = AVERROR_EOF;
> +    }else if (data == NULL) {
> +        ret = AVERROR(EAGAIN);
> +    }else {
> +        ret = 0;
> +    }
> +    return ret;
> +}
> diff --git a/libavcodec/amfenc.h b/libavcodec/amfenc.h
> new file mode 100644
> index 0000000..21e9c67
> --- /dev/null
> +++ b/libavcodec/amfenc.h
> ...
> +
> +/**
> +* Error handling helper
> +*/
> +#define AMF_RETURN_IF_FALSE(avctx, exp, ret_value, /*message,*/ ...) \
> +    if (!(exp)) { \
> +        av_log(avctx, AV_LOG_ERROR, __VA_ARGS__); \
> +        return AVERROR(ret_value); \

In most cases you call with an AVERROR() value here rather than an errno.  The wrapping in AVERROR therefore makes it positive and renders many of your "< 0" error checks ineffective, leading to segfaults.

> +    }
> +
> +#endif //AVCODEC_AMFENC_H
> diff --git a/libavcodec/amfenc_h264.c b/libavcodec/amfenc_h264.c
> new file mode 100644
> index 0000000..f7812a1
> --- /dev/null
> +++ b/libavcodec/amfenc_h264.c
> @@ -0,0 +1,366 @@
> +/*
> + * 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 "libavutil/internal.h"
> +#include "libavutil/opt.h"
> +#include "amfenc.h"
> +#include "internal.h"
> +
> +#define OFFSET(x) offsetof(AmfContext, x)
> +#define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
> +
> +static const AVOption options[] = {
> +    // Static
> +    /// Usage
> +    { "usage",          "Encoder Usage",        OFFSET(usage),  AV_OPT_TYPE_INT,   { .i64 = AMF_VIDEO_ENCODER_USAGE_TRANSCONDING      }, AMF_VIDEO_ENCODER_USAGE_TRANSCONDING, AMF_VIDEO_ENCODER_USAGE_WEBCAM, VE, "usage" },
> +    { "transcoding",    "Generic Transcoding",  0,              AV_OPT_TYPE_CONST, { .i64 = AMF_VIDEO_ENCODER_USAGE_TRANSCONDING      }, 0, 0, VE, "usage" },
> +    { "ultralowlatency","",                     0,              AV_OPT_TYPE_CONST, { .i64 = AMF_VIDEO_ENCODER_USAGE_ULTRA_LOW_LATENCY }, 0, 0, VE, "usage" },
> +    { "lowlatency",     "",                     0,              AV_OPT_TYPE_CONST, { .i64 = AMF_VIDEO_ENCODER_USAGE_LOW_LATENCY       }, 0, 0, VE, "usage" },
> +    { "webcam",         "Webcam",               0,              AV_OPT_TYPE_CONST, { .i64 = AMF_VIDEO_ENCODER_USAGE_WEBCAM            }, 0, 0, VE, "usage" },
> +
> +    /// Profile,
> +    { "profile",        "Profile",              OFFSET(profile),AV_OPT_TYPE_INT, { .i64 = AMF_VIDEO_ENCODER_PROFILE_MAIN       }, AMF_VIDEO_ENCODER_PROFILE_BASELINE, AMF_VIDEO_ENCODER_PROFILE_CONSTRAINED_HIGH, VE, "profile" },
> +    { "main",           "",                     0,              AV_OPT_TYPE_CONST, { .i64 = AMF_VIDEO_ENCODER_PROFILE_MAIN     }, 0, 0, VE, "profile" },
> +    { "high",           "",                     0,              AV_OPT_TYPE_CONST, { .i64 = AMF_VIDEO_ENCODER_PROFILE_HIGH     }, 0, 0, VE, "profile" },
> +    { "constrained_baseline",           "",     0,              AV_OPT_TYPE_CONST, { .i64 = AMF_VIDEO_ENCODER_PROFILE_CONSTRAINED_BASELINE }, 0, 0, VE, "profile" },
> +    { "constrained_high",           "",         0,              AV_OPT_TYPE_CONST, { .i64 = AMF_VIDEO_ENCODER_PROFILE_CONSTRAINED_HIGH }, 0, 0, VE, "profile" },
> +
> +    /// Profile Level
> +    { "level",          "Profile Level",        OFFSET(level),  AV_OPT_TYPE_INT,   { .i64 = 0  }, 0, 62, VE, "level" },
> +    { "auto",           "",                     0,              AV_OPT_TYPE_CONST, { .i64 = 0  }, 0, 0,  VE, "level" },
> +    { "1.0",            "",                     0,              AV_OPT_TYPE_CONST, { .i64 = 10 }, 0, 0,  VE, "level" },
> +    { "1.1",            "",                     0,              AV_OPT_TYPE_CONST, { .i64 = 11 }, 0, 0,  VE, "level" },
> +    { "1.2",            "",                     0,              AV_OPT_TYPE_CONST, { .i64 = 12 }, 0, 0,  VE, "level" },
> +    { "1.3",            "",                     0,              AV_OPT_TYPE_CONST, { .i64 = 13 }, 0, 0,  VE, "level" },
> +    { "2.0",            "",                     0,              AV_OPT_TYPE_CONST, { .i64 = 20 }, 0, 0,  VE, "level" },
> +    { "2.1",            "",                     0,              AV_OPT_TYPE_CONST, { .i64 = 21 }, 0, 0,  VE, "level" },
> +    { "2.2",            "",                     0,              AV_OPT_TYPE_CONST, { .i64 = 22 }, 0, 0,  VE, "level" },
> +    { "3.0",            "",                     0,              AV_OPT_TYPE_CONST, { .i64 = 30 }, 0, 0,  VE, "level" },
> +    { "3.1",            "",                     0,              AV_OPT_TYPE_CONST, { .i64 = 31 }, 0, 0,  VE, "level" },
> +    { "3.2",            "",                     0,              AV_OPT_TYPE_CONST, { .i64 = 32 }, 0, 0,  VE, "level" },
> +    { "4.0",            "",                     0,              AV_OPT_TYPE_CONST, { .i64 = 40 }, 0, 0,  VE, "level" },
> +    { "4.1",            "",                     0,              AV_OPT_TYPE_CONST, { .i64 = 41 }, 0, 0,  VE, "level" },
> +    { "4.2",            "",                     0,              AV_OPT_TYPE_CONST, { .i64 = 42 }, 0, 0,  VE, "level" },
> +    { "5.0",            "",                     0,              AV_OPT_TYPE_CONST, { .i64 = 50 }, 0, 0,  VE, "level" },
> +    { "5.1",            "",                     0,              AV_OPT_TYPE_CONST, { .i64 = 51 }, 0, 0,  VE, "level" },
> +    { "5.2",            "",                     0,              AV_OPT_TYPE_CONST, { .i64 = 52 }, 0, 0,  VE, "level" },
> +    { "6.0",            "",                     0,              AV_OPT_TYPE_CONST, { .i64 = 60 }, 0, 0,  VE, "level" },
> +    { "6.1",            "",                     0,              AV_OPT_TYPE_CONST, { .i64 = 61 }, 0, 0,  VE, "level" },
> +    { "6.2",            "",                     0,              AV_OPT_TYPE_CONST, { .i64 = 62 }, 0, 0,  VE, "level" },
> +
> +
> +    /// Quality Preset
> +    { "quality",        "Quality Preference",                   OFFSET(quality),    AV_OPT_TYPE_INT,   { .i64 = AMF_VIDEO_ENCODER_QUALITY_PRESET_SPEED    }, AMF_VIDEO_ENCODER_QUALITY_PRESET_BALANCED, AMF_VIDEO_ENCODER_QUALITY_PRESET_QUALITY, VE, "quality" },
> +    { "speed",          "Prefer Speed",                         0,                  AV_OPT_TYPE_CONST, { .i64 = AMF_VIDEO_ENCODER_QUALITY_PRESET_SPEED    },       0, 0, VE, "quality" },
> +    { "balanced",       "Balanced",                             0,                  AV_OPT_TYPE_CONST, { .i64 = AMF_VIDEO_ENCODER_QUALITY_PRESET_BALANCED },    0, 0, VE, "quality" },
> +    { "quality",        "Prefer Quality",                       0,                  AV_OPT_TYPE_CONST, { .i64 = AMF_VIDEO_ENCODER_QUALITY_PRESET_QUALITY  },     0, 0, VE, "quality" },
> +
> +    // Dynamic
> +    /// Rate Control Method
> +    { "rc",             "Rate Control Method",                  OFFSET(rate_control_mode),  AV_OPT_TYPE_INT,   { .i64 = AMF_VIDEO_ENCODER_RATE_CONTROL_METHOD_PEAK_CONSTRAINED_VBR    }, AMF_VIDEO_ENCODER_RATE_CONTROL_METHOD_CONSTANT_QP, AMF_VIDEO_ENCODER_RATE_CONTROL_METHOD_LATENCY_CONSTRAINED_VBR, VE, "rc" },
> +    { "cqp",            "Constant Quantization Parameter",      0,                          AV_OPT_TYPE_CONST, { .i64 = AMF_VIDEO_ENCODER_RATE_CONTROL_METHOD_CONSTANT_QP             }, 0, 0, VE, "rc" },
> +    { "cbr",            "Constant Bitrate",                     0,                          AV_OPT_TYPE_CONST, { .i64 = AMF_VIDEO_ENCODER_RATE_CONTROL_METHOD_CBR                     }, 0, 0, VE, "rc" },
> +    { "vbr_peak",       "Peak Contrained Variable Bitrate",     0,                          AV_OPT_TYPE_CONST, { .i64 = AMF_VIDEO_ENCODER_RATE_CONTROL_METHOD_PEAK_CONSTRAINED_VBR    }, 0, 0, VE, "rc" },
> +    { "vbr_latency",    "Latency Constrained Variable Bitrate", 0,                          AV_OPT_TYPE_CONST, { .i64 = AMF_VIDEO_ENCODER_RATE_CONTROL_METHOD_LATENCY_CONSTRAINED_VBR }, 0, 0, VE, "rc" },

I think the default for this option needs to be decided dynamically.  Just setting "-b:v" is a not-unreasonable thing to do, and currently the choice of PEAK_CONSTRAINED_VBR makes it then complain that maxrate isn't set.  Similarly, if the only setting is some constant-quality option (-q/-global_quality, or your private ones below), it ignores that and use the default 2Mbps instead.

> +    /// Enforce HRD, Filler Data, VBAQ, Frame Skipping
> +    { "enforce_hrd",    "Enforce HRD",                          OFFSET(enforce_hrd),        AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, VE },

Does this option work?  I don't seem to be able to push it into generating HRD information with any combination of options.

> +    { "filler_data",    "Filler Data Enable",                   OFFSET(filler_data),        AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, VE },
> +    { "vbaq",           "Enable VBAQ",                          OFFSET(enable_vbaq),        AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, VE },
> +    { "frame_skipping", "Rate Control Based Frame Skip",        OFFSET(skip_frame),         AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, VE },
> +
> +    /// QP Values
> +    { "qp_i",           "Quantization Parameter for I-Frame",   OFFSET(qp_i),               AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 51, VE },
> +    { "qp_p",           "Quantization Parameter for P-Frame",   OFFSET(qp_p),               AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 51, VE },
> +    { "qp_b",           "Quantization Parameter for B-Frame",   OFFSET(qp_b),               AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 51, VE },
> +
> +    /// Pre-Pass, Pre-Analysis, Two-Pass
> +    { "preanalysis",    "Pre-Analysis Mode",                    OFFSET(preanalysis),        AV_OPT_TYPE_BOOL,{ .i64 = 0 }, 0, 1, VE, NULL },
> +
> +    /// Maximum Access Unit Size
> +    { "max_au_size",    "Maximum Access Unit Size for rate control (in bits)",   OFFSET(max_au_size),        AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, VE },

Can you explain more about what this option does?  I don't seem to be able to get it to do anything - e.g. setting -max_au_size 80000 with 30fps CBR 1M (which should be easily achievable) still makes packets of more than 80000 bits.)

> +
> +    /// Header Insertion Spacing
> +    { "header_spacing", "Header Insertion Spacing",             OFFSET(header_spacing),     AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 1000, VE },
> +
> +    /// B-Frames
> +    // BPicturesPattern=bf
> +    { "bf_delta_qp",    "B-Picture Delta QP",                   OFFSET(b_frame_delta_qp),   AV_OPT_TYPE_INT,  { .i64 = 4 }, -10, 10, VE },
> +    { "bf_ref",         "Enable Reference to B-Frames",         OFFSET(b_frame_ref),        AV_OPT_TYPE_BOOL, { .i64 = 1 }, 0, 1, VE },
> +    { "bf_ref_delta_qp","Reference B-Picture Delta QP",         OFFSET(ref_b_frame_delta_qp), AV_OPT_TYPE_INT,  { .i64 = 4 }, -10, 10, VE },
> +
> +    /// Intra-Refresh
> +    { "intra_refresh_mb","Intra Refresh MBs Number Per Slot in Macroblocks",       OFFSET(intra_refresh_mb),    AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, VE },
> +
> +    /// coder
> +    { "coder",          "Coding Type",                          OFFSET(coding_mode),   AV_OPT_TYPE_INT,   { .i64 = AMF_VIDEO_ENCODER_UNDEFINED }, AMF_VIDEO_ENCODER_UNDEFINED, AMF_VIDEO_ENCODER_CALV, VE, "coder" },
> +    { "auto",           "Automatic",                            0,                     AV_OPT_TYPE_CONST, { .i64 = AMF_VIDEO_ENCODER_UNDEFINED }, 0, 0, VE, "coder" },
> +    { "cavlc",          "Context Adaptive Variable-Length Coding", 0,                  AV_OPT_TYPE_CONST, { .i64 = AMF_VIDEO_ENCODER_CALV },      0, 0, VE, "coder" },
> +    { "cabac",          "Context Adaptive Binary Arithmetic Coding", 0,                AV_OPT_TYPE_CONST, { .i64 = AMF_VIDEO_ENCODER_CABAC },     0, 0, VE, "coder" },
> +
> +    { "me_half_pel",    "Enable ME Half Pixel",                 OFFSET(me_half_pel),   AV_OPT_TYPE_BOOL,  { .i64 = 1 }, 0, 1, VE },
> +    { "me_quarter_pel", "Enable ME Quarter Pixel",              OFFSET(me_quarter_pel),AV_OPT_TYPE_BOOL,  { .i64 = 1 }, 0, 1, VE },
> +
> +    { "log_to_dbg",     "Enable AMF logging to debug output",   OFFSET(log_to_dbg), AV_OPT_TYPE_BOOL,{ .i64 = 0 }, 0, 1, VE },
> +    { NULL }
> +};
> +
> ...
> +AVCodec ff_h264_amf_encoder = {
> +    .name           = "h264_amf",
> +    .long_name      = NULL_IF_CONFIG_SMALL("AMD AMF H.264 Encoder"),
> +    .type           = AVMEDIA_TYPE_VIDEO,
> +    .id             = AV_CODEC_ID_H264,
> +    .init           = amf_encode_init_h264,
> +    .send_frame     = ff_amf_send_frame,
> +    .receive_packet = ff_amf_receive_packet,
> +    .close          = ff_amf_encode_close,
> +    .priv_data_size = sizeof(AmfContext),
> +    .priv_class     = &h264_amf_class,
> +    .defaults       = defaults,
> +    .capabilities   = AV_CODEC_CAP_DELAY,
> +    .caps_internal  = FF_CODEC_CAP_INIT_CLEANUP,
> +    .pix_fmts       = ff_amf_pix_fmts,
> +};

And some thoughts on the stream it makes:

"ffmpeg_g -report -y -f lavfi -i testsrc -an -c:v h264_amf -bsf:v trace_headers -frames:v 1000 out.mp4"

[AVBSFContext @ 000000000049b9c0] Sequence Parameter Set
[AVBSFContext @ 000000000049b9c0] 40          max_num_ref_frames                                      00101 = 4
[AVBSFContext @ 000000000049b9c0] 206         max_dec_frame_buffering                                 00101 = 4

Where did 4 come from?  It never uses more than 1 reference in the stream.

[AVBSFContext @ 000000000049b9c0] Access Unit Delimiter

It seems to put AUDs in every packet by default.  Is there a way to turn that off?  (It messes with sending over RTP by forcing a useless extra packet because they can't be combined with fragmentation units.)


Thanks,

- Mark


More information about the ffmpeg-devel mailing list