2 * This file is part of Libav.
4 * Libav is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * Libav is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with Libav; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 #ifndef AVCODEC_VAAPI_ENCODE_H
20 #define AVCODEC_VAAPI_ENCODE_H
26 #include "libavutil/hwcontext.h"
27 #include "libavutil/hwcontext_vaapi.h"
31 struct VAAPIEncodeType
;
32 struct VAAPIEncodePicture
;
35 MAX_CONFIG_ATTRIBUTES
= 4,
36 MAX_GLOBAL_PARAMS
= 4,
37 MAX_PICTURE_REFERENCES
= 2,
38 MAX_PICTURE_SLICES
= 1,
39 MAX_PARAM_BUFFERS
= 16,
40 MAX_REORDER_DELAY
= 16,
41 MAX_PARAM_BUFFER_SIZE
= 1024,
51 typedef struct VAAPIEncodeSlice
{
53 void *codec_slice_params
;
56 typedef struct VAAPIEncodePicture
{
57 struct VAAPIEncodePicture
*next
;
59 int64_t display_order
;
69 VASurfaceID input_surface
;
72 VASurfaceID recon_surface
;
75 VABufferID param_buffers
[MAX_PARAM_BUFFERS
];
77 AVBufferRef
*output_buffer_ref
;
78 VABufferID output_buffer
;
81 void *codec_picture_params
;
84 struct VAAPIEncodePicture
*refs
[MAX_PICTURE_REFERENCES
];
87 VAAPIEncodeSlice
*slices
[MAX_PICTURE_SLICES
];
90 typedef struct VAAPIEncodeContext
{
93 // Codec-specific hooks.
94 const struct VAAPIEncodeType
*codec
;
96 // Encoding profile (VAProfileXXX).
98 // Encoding entrypoint (usually VAEntryointEncSlice).
99 VAEntrypoint va_entrypoint
;
100 // Surface colour/sampling format (usually VA_RT_FORMAT_YUV420).
101 unsigned int va_rt_format
;
102 // Rate control mode.
103 unsigned int va_rc_mode
;
105 // The required size of surfaces. This is probably the input
106 // size (AVCodecContext.width|height) aligned up to whatever
107 // block size is required by the codec.
111 // Everything above this point must be set before calling
112 // ff_vaapi_encode_init().
114 // Codec-specific state.
117 // Configuration attributes to use when creating va_config.
118 VAConfigAttrib config_attributes
[MAX_CONFIG_ATTRIBUTES
];
119 int nb_config_attributes
;
121 VAConfigID va_config
;
122 VAContextID va_context
;
124 AVBufferRef
*device_ref
;
125 AVHWDeviceContext
*device
;
126 AVVAAPIDeviceContext
*hwctx
;
128 // The hardware frame context containing the input frames.
129 AVBufferRef
*input_frames_ref
;
130 AVHWFramesContext
*input_frames
;
132 // The hardware frame context containing the reconstructed frames.
133 AVBufferRef
*recon_frames_ref
;
134 AVHWFramesContext
*recon_frames
;
136 // Pool of (reusable) bitstream output buffers.
137 AVBufferPool
*output_buffer_pool
;
139 // Global parameters which will be applied at the start of the
140 // sequence (includes rate control parameters below).
141 VAEncMiscParameterBuffer
*global_params
[MAX_GLOBAL_PARAMS
];
142 size_t global_params_size
[MAX_GLOBAL_PARAMS
];
143 int nb_global_params
;
145 // Rate control parameters.
147 VAEncMiscParameterBuffer misc
;
148 VAEncMiscParameterRateControl rc
;
151 VAEncMiscParameterBuffer misc
;
152 VAEncMiscParameterHRD hrd
;
155 // Per-sequence parameter structure (VAEncSequenceParameterBuffer*).
156 void *codec_sequence_params
;
158 // Per-sequence parameters found in the per-picture parameter
159 // structure (VAEncPictureParameterBuffer*).
160 void *codec_picture_params
;
162 // Current encoding window, in display (input) order.
163 VAAPIEncodePicture
*pic_start
, *pic_end
;
165 // Next input order index (display order).
167 // Number of frames that output is behind input.
168 int64_t output_delay
;
169 // Number of frames decode output will need to be delayed.
170 int64_t decode_delay
;
171 // Next output order index (encode order).
172 int64_t output_order
;
175 // All encode operations are done independently (synchronise
176 // immediately after every operation).
177 ISSUE_MODE_SERIALISE_EVERYTHING
= 0,
178 // Overlap as many operations as possible.
179 ISSUE_MODE_MAXIMISE_THROUGHPUT
,
180 // Overlap operations only when satisfying parallel dependencies.
181 ISSUE_MODE_MINIMISE_LATENCY
,
184 // Timestamp handling.
186 int64_t dts_pts_diff
;
187 int64_t ts_ring
[MAX_REORDER_DELAY
* 3];
189 // Frame type decision.
198 // Codec-local options are allocated to follow this structure in
199 // memory (in the AVCodec definition, set priv_data_size to
200 // sizeof(VAAPIEncodeContext) + sizeof(VAAPIEncodeFooOptions)).
202 char codec_options_data
[0];
203 } VAAPIEncodeContext
;
206 typedef struct VAAPIEncodeType
{
207 size_t priv_data_size
;
209 // Perform any extra codec-specific configuration after the
210 // codec context is initialised (set up the private data and
211 // add any necessary global parameters).
212 int (*configure
)(AVCodecContext
*avctx
);
214 // The size of the parameter structures:
215 // sizeof(VAEnc{type}ParameterBuffer{codec}).
216 size_t sequence_params_size
;
217 size_t picture_params_size
;
218 size_t slice_params_size
;
220 // Fill the parameter structures.
221 int (*init_sequence_params
)(AVCodecContext
*avctx
);
222 int (*init_picture_params
)(AVCodecContext
*avctx
,
223 VAAPIEncodePicture
*pic
);
224 int (*init_slice_params
)(AVCodecContext
*avctx
,
225 VAAPIEncodePicture
*pic
,
226 VAAPIEncodeSlice
*slice
);
228 // The type used by the packed header: this should look like
229 // VAEncPackedHeader{something}.
230 int sequence_header_type
;
231 int picture_header_type
;
232 int slice_header_type
;
234 // Write the packed header data to the provided buffer.
235 int (*write_sequence_header
)(AVCodecContext
*avctx
,
236 char *data
, size_t *data_len
);
237 int (*write_picture_header
)(AVCodecContext
*avctx
,
238 VAAPIEncodePicture
*pic
,
239 char *data
, size_t *data_len
);
240 int (*write_slice_header
)(AVCodecContext
*avctx
,
241 VAAPIEncodePicture
*pic
,
242 VAAPIEncodeSlice
*slice
,
243 char *data
, size_t *data_len
);
245 // Fill an extra parameter structure, which will then be
246 // passed to vaRenderPicture(). Will be called repeatedly
247 // with increasing index argument until AVERROR_EOF is
249 int (*write_extra_buffer
)(AVCodecContext
*avctx
,
250 VAAPIEncodePicture
*pic
,
251 int index
, int *type
,
252 char *data
, size_t *data_len
);
254 // Write an extra packed header. Will be called repeatedly
255 // with increasing index argument until AVERROR_EOF is
257 int (*write_extra_header
)(AVCodecContext
*avctx
,
258 VAAPIEncodePicture
*pic
,
259 int index
, int *type
,
260 char *data
, size_t *data_len
);
264 int ff_vaapi_encode2(AVCodecContext
*avctx
, AVPacket
*pkt
,
265 const AVFrame
*input_image
, int *got_packet
);
267 int ff_vaapi_encode_init(AVCodecContext
*avctx
);
268 int ff_vaapi_encode_close(AVCodecContext
*avctx
);
270 #endif /* AVCODEC_VAAPI_ENCODE_H */