---
 configure                 |   2 +
 libavcodec/Makefile       |   1 +
 libavcodec/vaapi_encode.c | 840 ++++++++++++++++++++++++++++++++++++++++++++++
 libavcodec/vaapi_encode.h | 204 +++++++++++
 4 files changed, 1047 insertions(+)
 create mode 100644 libavcodec/vaapi_encode.c
 create mode 100644 libavcodec/vaapi_encode.h

diff --git a/configure b/configure
index bafb755..926189e 100755
--- a/configure
+++ b/configure
@@ -1705,6 +1705,7 @@ CONFIG_EXTRA="
     texturedsp
     texturedspenc
     tpeldsp
+    vaapi_encode
     vaapi_surface_attributes
     vc1dsp
     videodsp
@@ -1875,6 +1876,7 @@ nvenc_deps_any="dlopen LoadLibrary"
 nvenc_extralibs='$ldl'
 qsvdec_select="qsv"
 qsvenc_select="qsv"
+vaapi_encode_deps="vaapi_surface_attributes"
 vc1dsp_select="h264chroma qpeldsp startcode"

 # decoders / encoders
diff --git a/libavcodec/Makefile b/libavcodec/Makefile
index 256dee3..a0c0edb 100644
--- a/libavcodec/Makefile
+++ b/libavcodec/Makefile
@@ -105,6 +105,7 @@ OBJS-$(CONFIG_STARTCODE)               += startcode.o
 OBJS-$(CONFIG_TEXTUREDSP)              += texturedsp.o
 OBJS-$(CONFIG_TEXTUREDSPENC)           += texturedspenc.o
 OBJS-$(CONFIG_TPELDSP)                 += tpeldsp.o
+OBJS-$(CONFIG_VAAPI_ENCODE)            += vaapi_encode.o
 OBJS-$(CONFIG_VC1DSP)                  += vc1dsp.o
 OBJS-$(CONFIG_VIDEODSP)                += videodsp.o
 OBJS-$(CONFIG_VP3DSP)                  += vp3dsp.o
diff --git a/libavcodec/vaapi_encode.c b/libavcodec/vaapi_encode.c
new file mode 100644
index 0000000..9c76727
--- /dev/null
+++ b/libavcodec/vaapi_encode.c
@@ -0,0 +1,840 @@
+/*
+ * This file is part of Libav.
+ *
+ * Libav 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.
+ *
+ * Libav 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 Libav; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#include <inttypes.h>
+#include <string.h>
+
+#include "libavutil/avassert.h"
+#include "libavutil/log.h"
+
+#include "avcodec.h"
+#include "vaapi_encode.h"
+
+static const char *picture_type_name[] = { "IDR", "I", "P", "B" };
+
+static int vaapi_encode_make_packed_header(FFVAAPIEncodeContext *ctx,
+                                           FFVAAPIEncodePicture *pic,
+                                           int type, char *data, size_t 
bit_len)
+{
+    VAStatus vas;
+    VABufferID param_buffer, data_buffer;
+    VAEncPackedHeaderParameterBuffer params = {
+        .type = type,
+        .bit_length = bit_len,
+        .has_emulation_bytes = 1,
+    };
+
+    av_assert0(pic->nb_param_buffers + 2 <= MAX_PARAM_BUFFERS);
+
+    vas = vaCreateBuffer(ctx->hwctx->display, ctx->va_context,
+                         VAEncPackedHeaderParameterBufferType,
+                         sizeof(&params), 1, &params, &param_buffer);
+    if (vas != VA_STATUS_SUCCESS) {
+        av_log(ctx, AV_LOG_ERROR, "Failed to create parameter buffer for 
packed "
+               "header (type %d): %d (%s).\n", type, vas, vaErrorStr(vas));
+        return AVERROR(EIO);
+    }
+    pic->param_buffers[pic->nb_param_buffers++] = param_buffer;
+
+    vas = vaCreateBuffer(ctx->hwctx->display, ctx->va_context,
+                         VAEncPackedHeaderDataBufferType,
+                         (bit_len + 7) / 8, 1, data, &data_buffer);
+    if (vas != VA_STATUS_SUCCESS) {
+        av_log(ctx, AV_LOG_ERROR, "Failed to create data buffer for packed "
+               "header (type %d): %d (%s).\n", type, vas, vaErrorStr(vas));
+        return AVERROR(EIO);
+    }
+    pic->param_buffers[pic->nb_param_buffers++] = data_buffer;
+
+    av_log(ctx, AV_LOG_DEBUG, "Packed header buffer (%d) is %#x/%#x "
+           "(%zu bits).\n", type, param_buffer, data_buffer, bit_len);
+    return 0;
+}
+
+static int vaapi_encode_make_param_buffer(FFVAAPIEncodeContext *ctx,
+                                          FFVAAPIEncodePicture *pic,
+                                          int type, char *data, size_t len)
+{
+    VAStatus vas;
+    VABufferID buffer;
+
+    av_assert0(pic->nb_param_buffers + 1 <= MAX_PARAM_BUFFERS);
+
+    vas = vaCreateBuffer(ctx->hwctx->display, ctx->va_context,
+                         type, len, 1, data, &buffer);
+    if (vas != VA_STATUS_SUCCESS) {
+        av_log(ctx, AV_LOG_ERROR, "Failed to create parameter buffer "
+               "(type %d): %d (%s).\n", type, vas, vaErrorStr(vas));
+        return AVERROR(EIO);
+    }
+    pic->param_buffers[pic->nb_param_buffers++] = buffer;
+
+    av_log(ctx, AV_LOG_DEBUG, "Param buffer (%d) is %#x.\n",
+           type, buffer);
+    return 0;
+}
+
+static int vaapi_encode_wait(FFVAAPIEncodeContext *ctx,
+                             FFVAAPIEncodePicture *pic)
+{
+    VAStatus vas;
+
+    av_assert0(pic->encode_issued);
+
+    if (pic->encode_complete) {
+        // Already waited for this picture.
+        return 0;
+    }
+
+    av_log(ctx, AV_LOG_DEBUG, "Sync to pic %"PRId64"/%"PRId64" "
+           "(recon surface %#x).\n", pic->display_order,
+           pic->encode_order, pic->recon_surface);
+
+    vas = vaSyncSurface(ctx->hwctx->display, pic->recon_surface);
+    if (vas != VA_STATUS_SUCCESS) {
+        av_log(ctx, AV_LOG_ERROR, "Failed to sync to picture completion: "
+               "%d (%s).\n", vas, vaErrorStr(vas));
+        return AVERROR(EIO);
+    }
+
+    // Input is definitely finished with now.
+    av_frame_free(&pic->input_image);
+
+    pic->encode_complete = 1;
+    return 0;
+}
+
+static int vaapi_encode_issue(FFVAAPIEncodeContext *ctx,
+                              FFVAAPIEncodePicture *pic)
+{
+    FFVAAPIEncodeSlice *slice;
+    VAStatus vas;
+    int err, i;
+    char data[MAX_PARAM_BUFFER_SIZE];
+    size_t bit_len;
+
+    av_log(ctx, AV_LOG_DEBUG, "Issuing encode for pic %"PRId64"/%"PRId64" "
+           "as type %s.\n", pic->display_order, pic->encode_order,
+           picture_type_name[pic->type]);
+    if (pic->nb_refs == 0) {
+        av_log(ctx, AV_LOG_DEBUG, "No reference pictures.\n");
+    } else {
+        av_log(ctx, AV_LOG_DEBUG, "Refers to:");
+        for (i = 0; i < pic->nb_refs; i++) {
+            av_log(ctx, AV_LOG_DEBUG, " %"PRId64"/%"PRId64,
+                   pic->refs[i]->display_order, pic->refs[i]->encode_order);
+        }
+        av_log(ctx, AV_LOG_DEBUG, ".\n");
+    }
+
+    av_assert0(pic->input_available && !pic->encode_issued);
+    for (i = 0; i < pic->nb_refs; i++) {
+        av_assert0(pic->refs[i]);
+        // If we are serialised then the references must have already
+        // completed.  If not, they must have been issued but need not
+        // have completed yet.
+        if (ctx->issue_mode == ISSUE_MODE_SERIALISE_EVERYTHING)
+            av_assert0(pic->refs[i]->encode_complete);
+        else
+            av_assert0(pic->refs[i]->encode_issued);
+    }
+
+    av_log(ctx, AV_LOG_DEBUG, "Input surface is %#x.\n", pic->input_surface);
+
+    pic->recon_image = av_frame_alloc();
+    if (!pic->recon_image) {
+        err = AVERROR(ENOMEM);
+        goto fail;
+    }
+
+    err = av_hwframe_get_buffer(ctx->recon_frames_ref, pic->recon_image, 0);
+    if (err < 0) {
+        err = AVERROR(ENOMEM);
+        goto fail;
+    }
+    pic->recon_surface = (VASurfaceID)(uintptr_t)pic->recon_image->data[3];
+    av_log(ctx, AV_LOG_DEBUG, "Recon surface is %#x.\n", pic->recon_surface);
+
+    vas = vaCreateBuffer(ctx->hwctx->display, ctx->va_context,
+                         VAEncCodedBufferType,
+                         MAX_OUTPUT_BUFFER_SIZE, 1, 0,
+                         &pic->output_buffer);
+    if (vas != VA_STATUS_SUCCESS) {
+        av_log(ctx, AV_LOG_ERROR, "Failed to create bitstream output buffer: "
+               "%d (%s).\n", vas, vaErrorStr(vas));
+        err = AVERROR(ENOMEM);
+        goto fail;
+    }
+    av_log(ctx, AV_LOG_DEBUG, "Output buffer is %#x.\n", pic->output_buffer);
+
+    pic->codec_picture_params = av_malloc(ctx->codec->picture_params_size);
+    if (!pic->codec_picture_params)
+        goto fail;
+    memcpy(pic->codec_picture_params, ctx->codec_picture_params,
+           ctx->codec->picture_params_size);
+
+    pic->nb_param_buffers = 0;
+
+    if (pic->type == PICTURE_TYPE_IDR && ctx->codec->init_sequence_params) {
+        err = vaapi_encode_make_param_buffer(ctx, pic,
+                                             VAEncSequenceParameterBufferType,
+                                             ctx->codec_sequence_params,
+                                             ctx->codec->sequence_params_size);
+        if (err < 0)
+            goto fail;
+    }
+
+    if (ctx->codec->init_picture_params) {
+        err = ctx->codec->init_picture_params(ctx, pic);
+        if (err < 0) {
+            av_log(ctx, AV_LOG_ERROR, "Failed to initialise picture "
+                   "parameters: %d.\n", err);
+            goto fail;
+        }
+        err = vaapi_encode_make_param_buffer(ctx, pic,
+                                             VAEncPictureParameterBufferType,
+                                             pic->codec_picture_params,
+                                             ctx->codec->picture_params_size);
+        if (err < 0)
+            goto fail;
+    }
+
+    if (pic->type == PICTURE_TYPE_IDR) {
+        if (ctx->codec->write_sequence_header) {
+            bit_len = 8 * sizeof(data);
+            err = ctx->codec->write_sequence_header(ctx, data, &bit_len);
+            if (err < 0) {
+                av_log(ctx, AV_LOG_ERROR, "Failed to write per-sequence "
+                       "header: %d.\n", err);
+                goto fail;
+            }
+            err = vaapi_encode_make_packed_header(ctx, pic,
+                                                  VAEncPackedHeaderSequence,
+                                                  data, bit_len);
+            if (err < 0)
+                goto fail;
+        }
+    }
+
+    if (ctx->codec->write_picture_header) {
+        bit_len = 8 * sizeof(data);
+        err = ctx->codec->write_picture_header(ctx, pic, data, &bit_len);
+        if (err < 0) {
+            av_log(ctx, AV_LOG_ERROR, "Failed to write per-picture "
+                   "header: %d.\n", err);
+            goto fail;
+        }
+        err = vaapi_encode_make_packed_header(ctx, pic,
+                                              VAEncPackedHeaderPicture,
+                                              data, bit_len);
+        if (err < 0)
+            goto fail;
+    }
+
+    slice = pic->slice_list;
+    for (i = 0; i < pic->nb_slices; i++) {
+        slice = av_mallocz(sizeof(*slice));
+        slice->codec_slice_params = av_mallocz(ctx->codec->slice_params_size);
+        av_assert0(slice);
+
+        if (ctx->codec->init_slice_params) {
+            err = ctx->codec->init_slice_params(ctx, pic, slice);
+            if (err < 0) {
+                av_log(ctx, AV_LOG_ERROR, "Failed to initalise slice "
+                       "parameters: %d.\n", err);
+                goto fail;
+            }
+        }
+
+        if (ctx->codec->write_slice_header) {
+            bit_len = 8 * sizeof(data);
+            err = ctx->codec->write_slice_header(ctx, pic, slice,
+                                                 data, &bit_len);
+            if (err < 0) {
+                av_log(ctx, AV_LOG_ERROR, "Failed to write per-slice "
+                       "header: %d.\n", err);
+                goto fail;
+            }
+            err = vaapi_encode_make_packed_header(ctx, pic,
+                                                  VAEncPackedHeaderSlice,
+                                                  data, bit_len);
+            if (err < 0)
+                goto fail;
+        }
+
+        if (ctx->codec->init_slice_params) {
+            err = vaapi_encode_make_param_buffer(ctx, pic,
+                                                 VAEncSliceParameterBufferType,
+                                                 slice->codec_slice_params,
+                                                 
ctx->codec->slice_params_size);
+            if (err < 0)
+                goto fail;
+        }
+
+        slice = slice->next;
+    }
+    av_assert0(!slice);
+
+    vas = vaBeginPicture(ctx->hwctx->display, ctx->va_context,
+                         pic->input_surface);
+    if (vas != VA_STATUS_SUCCESS) {
+        av_log(ctx, AV_LOG_ERROR, "Failed to begin picture encode issue: "
+               "%d (%s).\n", vas, vaErrorStr(vas));
+        err = AVERROR(EIO);
+        goto fail_with_picture;
+    }
+
+    vas = vaRenderPicture(ctx->hwctx->display, ctx->va_context,
+                          pic->param_buffers, pic->nb_param_buffers);
+    if (vas != VA_STATUS_SUCCESS) {
+        av_log(ctx, AV_LOG_ERROR, "Failed to upload encode parameters: "
+               "%d (%s).\n", vas, vaErrorStr(vas));
+        err = AVERROR(EIO);
+        goto fail_with_picture;
+    }
+
+    vas = vaEndPicture(ctx->hwctx->display, ctx->va_context);
+    if (vas != VA_STATUS_SUCCESS) {
+        av_log(ctx, AV_LOG_ERROR, "Failed to end picture encode issue: "
+               "%d (%s).\n", vas, vaErrorStr(vas));
+        err = AVERROR(EIO);
+        goto fail_at_end;
+    }
+
+    pic->encode_issued = 1;
+
+    if (ctx->issue_mode == ISSUE_MODE_SERIALISE_EVERYTHING)
+        return vaapi_encode_wait(ctx, pic);
+    else
+        return 0;
+
+  fail_with_picture:
+    vaEndPicture(ctx->hwctx->display, ctx->va_context);
+  fail:
+    for(i = 0; i < pic->nb_param_buffers; i++)
+        vaDestroyBuffer(ctx->hwctx->display, pic->param_buffers[i]);
+  fail_at_end:
+    av_freep(&pic->codec_picture_params);
+    av_frame_free(&pic->recon_image);
+    return err;
+}
+
+static int vaapi_encode_output(FFVAAPIEncodeContext *ctx,
+                               FFVAAPIEncodePicture *pic,
+                               AVPacket *pkt)
+{
+    VACodedBufferSegment *buf_list, *buf;
+    VAStatus vas;
+    int err;
+
+    err = vaapi_encode_wait(ctx, pic);
+    if (err < 0)
+        return err;
+
+    buf_list = NULL;
+    vas = vaMapBuffer(ctx->hwctx->display, pic->output_buffer,
+                      (void**)&buf_list);
+    if (vas != VA_STATUS_SUCCESS) {
+        av_log(ctx, AV_LOG_ERROR, "Failed to map output buffers: "
+               "%d (%s).\n", vas, vaErrorStr(vas));
+        err = AVERROR(EIO);
+        goto fail;
+    }
+
+    for (buf = buf_list; buf; buf = buf->next) {
+        av_log(ctx, AV_LOG_DEBUG, "Output buffer: %u bytes.\n", buf->size);
+
+        err = av_new_packet(pkt, buf->size);
+        if (err < 0)
+            goto fail;
+
+        memcpy(pkt->data, buf->buf, buf->size);
+    }
+
+    if (pic->type == PICTURE_TYPE_IDR)
+        pkt->flags |= AV_PKT_FLAG_KEY;
+
+    pkt->pts = pic->pts;
+
+    vas = vaUnmapBuffer(ctx->hwctx->display, pic->output_buffer);
+    if (vas != VA_STATUS_SUCCESS) {
+        av_log(ctx, AV_LOG_ERROR, "Failed to unmap output buffers: "
+               "%d (%s).\n", vas, vaErrorStr(vas));
+        err = AVERROR(EIO);
+        goto fail;
+    }
+
+    vaDestroyBuffer(ctx->hwctx->display, pic->output_buffer);
+    pic->output_buffer = VA_INVALID_ID;
+
+    av_log(ctx, AV_LOG_DEBUG, "Output read for pic %"PRId64"/%"PRId64".\n",
+           pic->display_order, pic->encode_order);
+    return 0;
+
+  fail:
+    if (pic->output_buffer != VA_INVALID_ID) {
+        vaUnmapBuffer(ctx->hwctx->display, pic->output_buffer);
+        vaDestroyBuffer(ctx->hwctx->display, pic->output_buffer);
+    }
+    return err;
+}
+
+static FFVAAPIEncodePicture *vaapi_encode_alloc(void)
+{
+    FFVAAPIEncodePicture *pic;
+
+    pic = av_mallocz(sizeof(*pic));
+    if (!pic)
+        return NULL;
+
+    pic->input_surface = VA_INVALID_ID;
+    pic->recon_surface = VA_INVALID_ID;
+    pic->output_buffer = VA_INVALID_ID;
+
+    return pic;
+}
+
+static int vaapi_encode_free(FFVAAPIEncodeContext *ctx,
+                             FFVAAPIEncodePicture *pic)
+{
+    av_frame_free(&pic->input_image);
+    av_frame_free(&pic->recon_image);
+
+    // Output buffer should already be destroyed.
+    av_assert0(pic->output_buffer == VA_INVALID_ID);
+
+    av_freep(&pic->priv_data);
+    av_freep(&pic->codec_picture_params);
+
+    return 0;
+}
+
+static int vaapi_encode_step(FFVAAPIEncodeContext *ctx,
+                             FFVAAPIEncodePicture *target)
+{
+    FFVAAPIEncodePicture *pic;
+    int i, err;
+
+    if (ctx->issue_mode == ISSUE_MODE_SERIALISE_EVERYTHING) {
+        if (!target) {
+            // No target, nothing to do yet.
+            return 0;
+        }
+
+        if (target->encode_complete) {
+            // Already done.
+            return 0;
+        }
+
+        pic = target;
+        for (i = 0; i < pic->nb_refs; i++) {
+            if (!pic->refs[i]->encode_complete) {
+                err = vaapi_encode_step(ctx, pic->refs[i]);
+                if (err < 0)
+                    return err;
+            }
+        }
+
+        err = vaapi_encode_issue(ctx, pic);
+        if (err < 0)
+            return err;
+
+    } else if (ctx->issue_mode == ISSUE_MODE_MAXIMISE_THROUGHPUT) {
+        int activity;
+
+        do {
+            activity = 0;
+            for (pic = ctx->pic_start; pic; pic = pic->next) {
+                if (!pic->input_available || pic->encode_issued)
+                    continue;
+                for (i = 0; i < pic->nb_refs; i++) {
+                    if (!pic->refs[i]->encode_issued)
+                        break;
+                }
+                if (i < pic->nb_refs)
+                    continue;
+                err = vaapi_encode_issue(ctx, pic);
+                if (err < 0)
+                    return err;
+                activity = 1;
+            }
+        } while(activity);
+
+        if (target) {
+            av_assert0(target->encode_issued && "broken dependencies?");
+        }
+
+    } else if (ctx->issue_mode == ISSUE_MODE_MINIMISE_LATENCY) {
+        // Like maximise-throughput, except only issue things which the
+        // current target actually depends on.
+        return AVERROR_PATCHWELCOME;
+
+    } else {
+        av_assert0(0);
+    }
+
+    return 0;
+}
+
+static int vaapi_encode_get_next(FFVAAPIEncodeContext *ctx,
+                                 FFVAAPIEncodePicture **pic_out)
+{
+    FFVAAPIEncodePicture *start, *end, *pic;
+    int i;
+
+    for (pic = ctx->pic_start; pic; pic = pic->next) {
+        if (pic->next)
+            av_assert0(pic->display_order + 1 == pic->next->display_order);
+        if (pic->display_order == ctx->input_order) {
+            *pic_out = pic;
+            return 0;
+        }
+    }
+
+    if (ctx->input_order == 0) {
+        // First frame is always an IDR frame.
+        av_assert0(!ctx->pic_start && !ctx->pic_end);
+
+        pic = vaapi_encode_alloc();
+        if (!pic)
+            return AVERROR(ENOMEM);
+
+        pic->type = PICTURE_TYPE_IDR;
+        pic->display_order = 0;
+        pic->encode_order  = 0;
+
+        ctx->pic_start = ctx->pic_end = pic;
+
+        *pic_out = pic;
+        return 0;
+    }
+
+    pic = vaapi_encode_alloc();
+    if (!pic)
+        return AVERROR(ENOMEM);
+
+    if (ctx->p_per_i == 0 || ctx->p_counter == ctx->p_per_i) {
+        if (ctx->i_per_idr == 0 || ctx->i_counter == ctx->i_per_idr) {
+            pic->type = PICTURE_TYPE_IDR;
+            ctx->i_counter = 0;
+        } else {
+            pic->type = PICTURE_TYPE_I;
+            ++ctx->i_counter;
+        }
+        ctx->p_counter = 0;
+    } else {
+        pic->type = PICTURE_TYPE_P;
+        pic->refs[0] = ctx->pic_end;
+        pic->nb_refs = 1;
+        ++ctx->p_counter;
+    }
+    start = end = pic;
+
+    if (pic->type != PICTURE_TYPE_IDR) {
+        // If that was not an IDR frame, add B frames display-before and
+        // encode-after it.
+
+        for (i = 0; i < ctx->b_per_p; i++) {
+            pic = vaapi_encode_alloc();
+            if (!pic)
+                goto fail;
+
+            pic->type = PICTURE_TYPE_B;
+            pic->refs[0] = ctx->pic_end;
+            pic->refs[1] = end;
+            pic->nb_refs = 2;
+
+            pic->next = start;
+            pic->display_order = ctx->input_order + ctx->b_per_p - i - 1;
+            pic->encode_order  = pic->display_order + 1;
+            start = pic;
+        }
+    }
+
+    for (i = 0, pic = start; pic; i++, pic = pic->next) {
+        pic->display_order = ctx->input_order + i;
+        if (end->type == PICTURE_TYPE_IDR)
+            pic->encode_order = ctx->input_order + i;
+        else if (pic == end)
+            pic->encode_order = ctx->input_order;
+        else
+            pic->encode_order = ctx->input_order + i + 1;
+    }
+
+    av_assert0(ctx->pic_end);
+    ctx->pic_end->next = start;
+    ctx->pic_end = end;
+
+    *pic_out = start;
+
+    av_log(ctx, AV_LOG_DEBUG, "Pictures:");
+    for (pic = ctx->pic_start; pic; pic = pic->next) {
+        av_log(ctx, AV_LOG_DEBUG, " %s (%"PRId64"/%"PRId64")",
+               picture_type_name[pic->type],
+               pic->display_order, pic->encode_order);
+    }
+    av_log(ctx, AV_LOG_DEBUG, "\n");
+
+    return 0;
+
+  fail:
+    while (start) {
+        pic = start->next;
+        vaapi_encode_free(ctx, start);
+        start = pic;
+    }
+    return AVERROR(ENOMEM);
+}
+
+static int vaapi_encode_clear_old(FFVAAPIEncodeContext *ctx)
+{
+    FFVAAPIEncodePicture *pic, *old;
+    int i;
+
+    while (ctx->pic_start != ctx->pic_end) {
+        old = ctx->pic_start;
+        if (old->encode_order > ctx->output_order)
+            break;
+
+        for (pic = old->next; pic; pic = pic->next) {
+            if (pic->encode_complete)
+                continue;
+            for (i = 0; i < pic->nb_refs; i++) {
+                if (pic->refs[i] == old)
+                    goto done;
+            }
+        }
+
+        pic = ctx->pic_start;
+        ctx->pic_start = pic->next;
+        vaapi_encode_free(ctx, pic);
+    }
+
+  done:
+    return 0;
+}
+
+int ff_vaapi_encode2(AVCodecContext *avctx, AVPacket *pkt,
+                     const AVFrame *input_image, int *got_packet)
+{
+    FFVAAPIEncodeContext *ctx = avctx->priv_data;
+    FFVAAPIEncodePicture *pic;
+    int err;
+
+    if (input_image) {
+        av_log(ctx, AV_LOG_DEBUG, "Encode frame: %ux%u (%"PRId64").\n",
+               input_image->width, input_image->height, input_image->pts);
+
+        err = vaapi_encode_get_next(ctx, &pic);
+        if (err) {
+            av_log(ctx, AV_LOG_ERROR, "Input setup failed: %d.\n", err);
+            return err;
+        }
+
+        pic->input_image = av_frame_alloc();
+        if (!pic->input_image) {
+            err = AVERROR(ENOMEM);
+            goto fail;
+        }
+        err = av_frame_ref(pic->input_image, input_image);
+        if (err < 0)
+            goto fail;
+        pic->input_surface = (VASurfaceID)(uintptr_t)input_image->data[3];
+        pic->pts = input_image->pts;
+
+        pic->input_available = 1;
+    }
+
+    ++ctx->input_order;
+    ++ctx->output_order;
+
+    for (pic = ctx->pic_start; pic; pic = pic->next)
+        if (pic->encode_order == ctx->output_order)
+            break;
+
+    // pic can be null here if we don't have a specific target in this
+    // iteration.  We might still issue encodes if things can be overlapped,
+    // even though we don't intend to output anything.
+
+    err = vaapi_encode_step(ctx, pic);
+    if (err < 0) {
+        av_log(ctx, AV_LOG_ERROR, "Encode failed: %d.\n", err);
+        goto fail;
+    }
+
+    if (!pic) {
+        *got_packet = 0;
+    } else {
+        err = vaapi_encode_output(ctx, pic, pkt);
+        if (err < 0) {
+            av_log(ctx, AV_LOG_ERROR, "Output failed: %d.\n", err);
+            goto fail;
+        }
+        *got_packet = 1;
+    }
+
+    err = vaapi_encode_clear_old(ctx);
+    if (err < 0) {
+        av_log(ctx, AV_LOG_ERROR, "List clearing failed: %d.\n", err);
+        goto fail;
+    }
+
+    return 0;
+
+  fail:
+    return err;
+}
+
+av_cold int ff_vaapi_encode_init(AVCodecContext *avctx,
+                                 const FFVAAPIEncodeType *type)
+{
+    FFVAAPIEncodeContext *ctx = avctx->priv_data;
+    AVVAAPIFramesContext *recon_hwctx;
+    VAStatus vas;
+    int err;
+
+    ctx->avctx = avctx;
+    ctx->codec = type;
+
+    ctx->priv_data = av_mallocz(type->priv_data_size);
+    if (!ctx->priv_data) {
+        err = AVERROR(ENOMEM);
+        goto fail;
+    }
+
+    ctx->input_frames_ref = av_buffer_ref(avctx->hw_frames_ctx);
+    if (!ctx->input_frames_ref) {
+        err = AVERROR(ENOMEM);
+        goto fail;
+    }
+    ctx->input_frames = (AVHWFramesContext*)ctx->input_frames_ref->data;
+
+    ctx->device_ref = av_buffer_ref(ctx->input_frames->device_ref);
+    if (!ctx->device_ref) {
+        err = AVERROR(ENOMEM);
+        goto fail;
+    }
+    ctx->device = (AVHWDeviceContext*)ctx->device_ref->data;
+    ctx->hwctx = ctx->device->hwctx;
+
+    err = ctx->codec->init(ctx);
+    if (err < 0)
+        goto fail;
+
+    vas = vaCreateConfig(ctx->hwctx->display,
+                         ctx->va_profile, ctx->va_entrypoint,
+                         ctx->config_attributes, ctx->nb_config_attributes,
+                         &ctx->va_config);
+    if (vas != VA_STATUS_SUCCESS) {
+        av_log(ctx, AV_LOG_ERROR, "Failed to create encode pipeline "
+               "configuration: %d (%s).\n", vas, vaErrorStr(vas));
+        err = AVERROR(EIO);
+        goto fail;
+    }
+
+    ctx->recon_frames_ref = av_hwframe_ctx_alloc(ctx->device_ref);
+    if (!ctx->recon_frames_ref) {
+        err = AVERROR(ENOMEM);
+        goto fail;
+    }
+    ctx->recon_frames = (AVHWFramesContext*)ctx->recon_frames_ref->data;
+
+    ctx->recon_frames->format    = AV_PIX_FMT_VAAPI_VLD;
+    ctx->recon_frames->sw_format = ctx->input_frames->sw_format;
+    ctx->recon_frames->width     = ctx->aligned_width;
+    ctx->recon_frames->height    = ctx->aligned_height;
+    ctx->recon_frames->initial_pool_size = ctx->nb_recon_frames;
+
+    err = av_hwframe_ctx_init(ctx->recon_frames_ref);
+    if (err < 0) {
+        av_log(ctx, AV_LOG_ERROR, "Failed to initialise reconstructed "
+               "frame context: %d.\n", err);
+        goto fail;
+    }
+    recon_hwctx = ctx->recon_frames->hwctx;
+
+    vas = vaCreateContext(ctx->hwctx->display, ctx->va_config,
+                          ctx->aligned_width, ctx->aligned_height,
+                          VA_PROGRESSIVE,
+                          recon_hwctx->surface_ids,
+                          recon_hwctx->nb_surfaces,
+                          &ctx->va_context);
+    if (vas != VA_STATUS_SUCCESS) {
+        av_log(ctx, AV_LOG_ERROR, "Failed to create encode pipeline "
+               "context: %d (%s).\n", vas, vaErrorStr(vas));
+        err = AVERROR(EIO);
+        goto fail;
+    }
+
+    ctx->input_order = 0;
+    ctx->output_order = -avctx->max_b_frames - 2;
+
+    if (ctx->codec->sequence_params_size > 0) {
+        ctx->codec_sequence_params =
+            av_mallocz(ctx->codec->sequence_params_size);
+        if (!ctx->codec_sequence_params) {
+            err = AVERROR(ENOMEM);
+            goto fail;
+        }
+    }
+    if (ctx->codec->picture_params_size > 0) {
+        ctx->codec_picture_params =
+            av_mallocz(ctx->codec->picture_params_size);
+        if (!ctx->codec_picture_params) {
+            err = AVERROR(ENOMEM);
+            goto fail;
+        }
+    }
+
+    if (ctx->codec->init_sequence_params) {
+        err = ctx->codec->init_sequence_params(ctx);
+        if (err < 0) {
+            av_log(ctx, AV_LOG_ERROR, "Codec sequence initialisation "
+                   "failed: %d.\n", err);
+            goto fail;
+        }
+    }
+
+    // All I are IDR for now.
+    ctx->i_per_idr = 0;
+    ctx->p_per_i = avctx->gop_size;
+    ctx->b_per_p = avctx->max_b_frames;
+
+    //ctx->issue_mode = ISSUE_MODE_SERIALISE_EVERYTHING;
+    ctx->issue_mode = ISSUE_MODE_MAXIMISE_THROUGHPUT;
+
+    return 0;
+
+  fail:
+    return err;
+}
+
+av_cold int ff_vaapi_encode_close(AVCodecContext *avctx)
+{
+    FFVAAPIEncodeContext *ctx = avctx->priv_data;
+
+    vaDestroyContext(ctx->hwctx->display, ctx->va_context);
+
+    vaDestroyConfig(ctx->hwctx->display, ctx->va_config);
+
+    ctx->codec->close(ctx);
+
+    av_freep(&ctx->priv_data);
+
+    return 0;
+}
diff --git a/libavcodec/vaapi_encode.h b/libavcodec/vaapi_encode.h
new file mode 100644
index 0000000..e310316
--- /dev/null
+++ b/libavcodec/vaapi_encode.h
@@ -0,0 +1,204 @@
+/*
+ * This file is part of Libav.
+ *
+ * Libav 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.
+ *
+ * Libav 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 Libav; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#ifndef AVCODEC_VAAPI_ENCODE_H
+#define AVCODEC_VAAPI_ENCODE_H
+
+#include <va/va.h>
+
+#include "libavutil/hwcontext.h"
+#include "libavutil/hwcontext_vaapi.h"
+
+#include "avcodec.h"
+#include "put_bits.h"
+
+struct FFVAAPIEncodeType;
+struct FFVAAPIEncodePicture;
+
+enum {
+    MAX_FRAME_REFERENCES   = 2,
+    MAX_PARAM_BUFFERS      = 16,
+    MAX_PARAM_BUFFER_SIZE  = 1024,
+    MAX_OUTPUT_BUFFER_SIZE = 1024 * 1024,
+};
+
+enum {
+    PICTURE_TYPE_IDR = 0,
+    PICTURE_TYPE_I   = 1,
+    PICTURE_TYPE_P   = 2,
+    PICTURE_TYPE_B   = 3,
+};
+
+enum {
+    // All encode operations are done independently.
+    ISSUE_MODE_SERIALISE_EVERYTHING = 0,
+    // Overlap as many operations as possible.
+    ISSUE_MODE_MAXIMISE_THROUGHPUT,
+    // Overlap operations only when satisfying parallel dependencies.
+    ISSUE_MODE_MINIMISE_LATENCY,
+};
+
+typedef struct FFVAAPIEncodeSlice {
+    struct FFVAAPIEncodeSlice *next;
+
+    void           *priv_data;
+    void           *codec_slice_params;
+} FFVAAPIEncodeSlice;
+
+typedef struct FFVAAPIEncodePicture {
+    struct FFVAAPIEncodePicture *next;
+
+    int64_t         display_order;
+    int64_t         encode_order;
+    int64_t         pts;
+
+    int             type;
+    int             input_available;
+    int             encode_issued;
+    int             encode_complete;
+
+    AVFrame        *input_image;
+    VASurfaceID     input_surface;
+
+    AVFrame        *recon_image;
+    VASurfaceID     recon_surface;
+
+    int          nb_param_buffers;
+    VABufferID      param_buffers[MAX_PARAM_BUFFERS];
+
+    VABufferID      output_buffer;
+
+    void           *priv_data;
+    void           *codec_picture_params;
+
+    int          nb_refs;
+    struct FFVAAPIEncodePicture *refs[MAX_FRAME_REFERENCES];
+
+    int          nb_slices;
+    FFVAAPIEncodeSlice *slice_list;
+} FFVAAPIEncodePicture;
+
+typedef struct FFVAAPIEncodeContext {
+    const AVClass *class;
+
+    const AVCodecContext *avctx;
+
+    // Codec-specific hooks.
+    const struct FFVAAPIEncodeType *codec;
+
+    // Codec-specific state.
+    void *priv_data;
+
+    VAProfile       va_profile;
+    VAEntrypoint    va_entrypoint;
+    VAConfigID      va_config;
+    VAContextID     va_context;
+
+    int             codec_profile;
+    int             codec_level;
+
+    int             va_rc_mode;
+    int             fixed_qp;
+
+    AVBufferRef    *device_ref;
+    AVHWDeviceContext *device;
+    AVVAAPIDeviceContext *hwctx;
+
+    AVBufferRef    *input_frames_ref;
+    AVHWFramesContext *input_frames;
+
+    // Input size, set from input frames.
+    int             input_width;
+    int             input_height;
+    // Aligned size, set by codec init, becomes hwframe size.
+    int             aligned_width;
+    int             aligned_height;
+
+    int          nb_recon_frames;
+    AVBufferRef    *recon_frames_ref;
+    AVHWFramesContext *recon_frames;
+
+    VAConfigAttrib *config_attributes;
+    int          nb_config_attributes;
+
+    void           *codec_sequence_params;
+
+    // Current encoding window, in display (input) order.
+    FFVAAPIEncodePicture *pic_start, *pic_end;
+
+    // Next input order index (display order).
+    int64_t         input_order;
+    // Next output order index (trails input order by a constant).
+    int64_t         output_order;
+
+    int             issue_mode;
+
+    // Frame type decision.
+    int i_per_idr;
+    int p_per_i;
+    int b_per_p;
+    int idr_counter;
+    int i_counter;
+    int p_counter;
+
+    // This exists because the VAAPI encode API conflates individual
+    // per-picture parameters with the picture parameter set.
+    void *codec_picture_params;
+
+    struct {
+        int qp;
+    } options;
+} FFVAAPIEncodeContext;
+
+
+typedef struct FFVAAPIEncodeType {
+    size_t    priv_data_size;
+
+    int  (*init)(FFVAAPIEncodeContext *enc);
+    int (*close)(FFVAAPIEncodeContext *enc);
+
+    size_t sequence_params_size;
+    size_t picture_params_size;
+    size_t slice_params_size;
+
+    int  (*init_sequence_params)(FFVAAPIEncodeContext *enc);
+    int (*write_sequence_header)(FFVAAPIEncodeContext *enc,
+                                 char *data, size_t *data_len);
+    int   (*init_picture_params)(FFVAAPIEncodeContext *enc,
+                                 FFVAAPIEncodePicture *pic);
+    int  (*write_picture_header)(FFVAAPIEncodeContext *enc,
+                                 FFVAAPIEncodePicture *pic,
+                                 char *data, size_t *data_len);
+    int     (*init_slice_params)(FFVAAPIEncodeContext *enc,
+                                 FFVAAPIEncodePicture *pic,
+                                 FFVAAPIEncodeSlice *slice);
+    int    (*write_slice_header)(FFVAAPIEncodeContext *enc,
+                                 FFVAAPIEncodePicture *pic,
+                                 FFVAAPIEncodeSlice *slice,
+                                 char *data, size_t *data_len);
+
+} FFVAAPIEncodeType;
+
+
+int ff_vaapi_encode2(AVCodecContext *avctx, AVPacket *pkt,
+                     const AVFrame *input_image, int *got_packet);
+
+int ff_vaapi_encode_init(AVCodecContext *avctx, const FFVAAPIEncodeType *type);
+int ff_vaapi_encode_close(AVCodecContext *avctx);
+
+#endif /* AVCODEC_VAAPI_ENCODE_H */
-- 
2.7.0

_______________________________________________
libav-devel mailing list
libav-devel@libav.org
https://lists.libav.org/mailman/listinfo/libav-devel

Reply via email to