vaapi_encode.h 9.8 KB
Newer Older
1
/*
2
 * This file is part of FFmpeg.
3
 *
4
 * FFmpeg is free software; you can redistribute it and/or
5 6 7 8
 * 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.
 *
9
 * FFmpeg is distributed in the hope that it will be useful,
10 11 12 13 14
 * 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
15
 * License along with FFmpeg; if not, write to the Free Software
16 17 18 19 20 21 22 23 24 25
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

#ifndef AVCODEC_VAAPI_ENCODE_H
#define AVCODEC_VAAPI_ENCODE_H

#include <stdint.h>

#include <va/va.h>

26 27 28 29
#if VA_CHECK_VERSION(1, 0, 0)
#include <va/va_str.h>
#endif

30 31 32 33 34 35 36 37 38
#include "libavutil/hwcontext.h"
#include "libavutil/hwcontext_vaapi.h"

#include "avcodec.h"

struct VAAPIEncodeType;
struct VAAPIEncodePicture;

enum {
39 40
    MAX_CONFIG_ATTRIBUTES  = 4,
    MAX_GLOBAL_PARAMS      = 4,
41 42 43 44 45 46 47 48 49 50 51 52 53
    MAX_PICTURE_REFERENCES = 2,
    MAX_REORDER_DELAY      = 16,
    MAX_PARAM_BUFFER_SIZE  = 1024,
};

enum {
    PICTURE_TYPE_IDR = 0,
    PICTURE_TYPE_I   = 1,
    PICTURE_TYPE_P   = 2,
    PICTURE_TYPE_B   = 3,
};

typedef struct VAAPIEncodeSlice {
M
Mark Thompson 已提交
54
    int             index;
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
    void           *priv_data;
    void           *codec_slice_params;
} VAAPIEncodeSlice;

typedef struct VAAPIEncodePicture {
    struct VAAPIEncodePicture *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;
78
    VABufferID     *param_buffers;
79

80
    AVBufferRef    *output_buffer_ref;
81 82 83 84 85 86 87 88 89
    VABufferID      output_buffer;

    void           *priv_data;
    void           *codec_picture_params;

    int          nb_refs;
    struct VAAPIEncodePicture *refs[MAX_PICTURE_REFERENCES];

    int          nb_slices;
90
    VAAPIEncodeSlice *slices;
91 92
} VAAPIEncodePicture;

93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
typedef struct VAAPIEncodeProfile {
    // lavc profile value (FF_PROFILE_*).
    int       av_profile;
    // Supported bit depth.
    int       depth;
    // Number of components.
    int       nb_components;
    // Chroma subsampling in width dimension.
    int       log2_chroma_w;
    // Chroma subsampling in height dimension.
    int       log2_chroma_h;
    // VAAPI profile value.
    VAProfile va_profile;
} VAAPIEncodeProfile;

108 109 110 111 112 113
typedef struct VAAPIEncodeContext {
    const AVClass *class;

    // Codec-specific hooks.
    const struct VAAPIEncodeType *codec;

114 115 116 117 118
    // Global options.

    // Use low power encoding mode.
    int             low_power;

119 120 121
    // Supported packed headers (initially the desired set, modified
    // later to what is actually supported).
    unsigned int    va_packed_headers;
122 123 124 125 126 127 128 129 130 131

    // The required size of surfaces.  This is probably the input
    // size (AVCodecContext.width|height) aligned up to whatever
    // block size is required by the codec.
    int             surface_width;
    int             surface_height;

    // Everything above this point must be set before calling
    // ff_vaapi_encode_init().

132 133 134 135 136 137 138
    // Chosen encoding profile details.
    const VAAPIEncodeProfile *profile;

    // Encoding profile (VAProfile*).
    VAProfile       va_profile;
    // Encoding entrypoint (VAEntryoint*).
    VAEntrypoint    va_entrypoint;
139 140 141 142
    // Rate control mode.
    unsigned int    va_rc_mode;
    // Bitrate for codec-specific encoder parameters.
    unsigned int    va_bit_rate;
143

144 145 146 147
    // Configuration attributes to use when creating va_config.
    VAConfigAttrib  config_attributes[MAX_CONFIG_ATTRIBUTES];
    int          nb_config_attributes;

148 149 150 151 152 153 154
    VAConfigID      va_config;
    VAContextID     va_context;

    AVBufferRef    *device_ref;
    AVHWDeviceContext *device;
    AVVAAPIDeviceContext *hwctx;

155
    // The hardware frame context containing the input frames.
156 157 158
    AVBufferRef    *input_frames_ref;
    AVHWFramesContext *input_frames;

159
    // The hardware frame context containing the reconstructed frames.
160 161 162
    AVBufferRef    *recon_frames_ref;
    AVHWFramesContext *recon_frames;

163
    // Pool of (reusable) bitstream output buffers.
164 165
    AVBufferPool   *output_buffer_pool;

166 167
    // Global parameters which will be applied at the start of the
    // sequence (includes rate control parameters below).
168 169 170 171
    VAEncMiscParameterBuffer *global_params[MAX_GLOBAL_PARAMS];
    size_t          global_params_size[MAX_GLOBAL_PARAMS];
    int          nb_global_params;

172 173 174 175 176 177 178 179 180
    // Rate control parameters.
    struct {
        VAEncMiscParameterBuffer misc;
        VAEncMiscParameterRateControl rc;
    } rc_params;
    struct {
        VAEncMiscParameterBuffer misc;
        VAEncMiscParameterHRD hrd;
    } hrd_params;
181 182 183 184
    struct {
        VAEncMiscParameterBuffer misc;
        VAEncMiscParameterFrameRate fr;
    } fr_params;
185 186 187 188 189 190
#if VA_CHECK_VERSION(0, 36, 0)
    struct {
        VAEncMiscParameterBuffer misc;
        VAEncMiscParameterBufferQualityLevel quality;
    } quality_params;
#endif
191

192 193 194 195 196
    // Per-sequence parameter structure (VAEncSequenceParameterBuffer*).
    void           *codec_sequence_params;

    // Per-sequence parameters found in the per-picture parameter
    // structure (VAEncPictureParameterBuffer*).
197
    void           *codec_picture_params;
198 199 200 201 202 203 204 205 206 207 208 209 210

    // Current encoding window, in display (input) order.
    VAAPIEncodePicture *pic_start, *pic_end;

    // Next input order index (display order).
    int64_t         input_order;
    // Number of frames that output is behind input.
    int64_t         output_delay;
    // Number of frames decode output will need to be delayed.
    int64_t         decode_delay;
    // Next output order index (encode order).
    int64_t         output_order;

211 212 213 214 215 216 217 218 219
    enum {
        // All encode operations are done independently (synchronise
        // immediately after every operation).
        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,
    } issue_mode;
220 221 222 223 224 225 226

    // Timestamp handling.
    int64_t         first_pts;
    int64_t         dts_pts_diff;
    int64_t         ts_ring[MAX_REORDER_DELAY * 3];

    // Frame type decision.
227
    int gop_size;
228 229
    int p_per_i;
    int b_per_p;
230
    int force_idr;
M
Mark Thompson 已提交
231
    int gop_counter;
232 233 234 235 236
    int p_counter;
    int end_of_stream;
} VAAPIEncodeContext;

typedef struct VAAPIEncodeType {
237 238 239 240
    // List of supported profiles and corresponding VAAPI profiles.
    // (Must end with FF_PROFILE_UNKNOWN.)
    const VAAPIEncodeProfile *profiles;

241 242 243 244
    // Perform any extra codec-specific configuration after the
    // codec context is initialised (set up the private data and
    // add any necessary global parameters).
    int (*configure)(AVCodecContext *avctx);
245

246 247
    // The size of the parameter structures:
    // sizeof(VAEnc{type}ParameterBuffer{codec}).
248 249 250 251
    size_t sequence_params_size;
    size_t picture_params_size;
    size_t slice_params_size;

252
    // Fill the parameter structures.
253 254 255 256 257 258 259
    int  (*init_sequence_params)(AVCodecContext *avctx);
    int   (*init_picture_params)(AVCodecContext *avctx,
                                 VAAPIEncodePicture *pic);
    int     (*init_slice_params)(AVCodecContext *avctx,
                                 VAAPIEncodePicture *pic,
                                 VAAPIEncodeSlice *slice);

260 261
    // The type used by the packed header: this should look like
    // VAEncPackedHeader{something}.
262 263 264 265
    int sequence_header_type;
    int picture_header_type;
    int slice_header_type;

266
    // Write the packed header data to the provided buffer.
267 268
    // The sequence header is also used to fill the codec extradata
    // when the encoder is starting.
269 270 271 272 273 274 275 276 277 278
    int (*write_sequence_header)(AVCodecContext *avctx,
                                 char *data, size_t *data_len);
    int  (*write_picture_header)(AVCodecContext *avctx,
                                 VAAPIEncodePicture *pic,
                                 char *data, size_t *data_len);
    int    (*write_slice_header)(AVCodecContext *avctx,
                                 VAAPIEncodePicture *pic,
                                 VAAPIEncodeSlice *slice,
                                 char *data, size_t *data_len);

279 280 281 282
    // Fill an extra parameter structure, which will then be
    // passed to vaRenderPicture().  Will be called repeatedly
    // with increasing index argument until AVERROR_EOF is
    // returned.
283 284 285 286
    int    (*write_extra_buffer)(AVCodecContext *avctx,
                                 VAAPIEncodePicture *pic,
                                 int index, int *type,
                                 char *data, size_t *data_len);
287 288 289 290

    // Write an extra packed header.  Will be called repeatedly
    // with increasing index argument until AVERROR_EOF is
    // returned.
291 292 293 294
    int    (*write_extra_header)(AVCodecContext *avctx,
                                 VAAPIEncodePicture *pic,
                                 int index, int *type,
                                 char *data, size_t *data_len);
295 296 297 298 299 300
} VAAPIEncodeType;


int ff_vaapi_encode2(AVCodecContext *avctx, AVPacket *pkt,
                     const AVFrame *input_image, int *got_packet);

301
int ff_vaapi_encode_init(AVCodecContext *avctx);
302 303
int ff_vaapi_encode_close(AVCodecContext *avctx);

304 305 306 307 308 309 310 311 312

#define VAAPI_ENCODE_COMMON_OPTIONS \
    { "low_power", \
      "Use low-power encoding mode (only available on some platforms; " \
      "may not support all encoding features)", \
      OFFSET(common.low_power), AV_OPT_TYPE_BOOL, \
      { .i64 = 0 }, 0, 1, FLAGS }


313
#endif /* AVCODEC_VAAPI_ENCODE_H */