]> git.sesse.net Git - ffmpeg/blob - libavcodec/vaapi_encode.h
avcodec/mpeg4_unpack_bframes_bsf: Use avpriv_find_start_code
[ffmpeg] / libavcodec / vaapi_encode.h
1 /*
2  * This file is part of FFmpeg.
3  *
4  * FFmpeg 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.
8  *
9  * FFmpeg 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.
13  *
14  * 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  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18
19 #ifndef AVCODEC_VAAPI_ENCODE_H
20 #define AVCODEC_VAAPI_ENCODE_H
21
22 #include <stdint.h>
23
24 #include <va/va.h>
25
26 #if VA_CHECK_VERSION(1, 0, 0)
27 #include <va/va_str.h>
28 #endif
29
30 #include "libavutil/hwcontext.h"
31 #include "libavutil/hwcontext_vaapi.h"
32
33 #include "avcodec.h"
34
35 struct VAAPIEncodeType;
36 struct VAAPIEncodePicture;
37
38 enum {
39     MAX_CONFIG_ATTRIBUTES  = 4,
40     MAX_GLOBAL_PARAMS      = 4,
41     MAX_DPB_SIZE           = 16,
42     MAX_PICTURE_REFERENCES = 2,
43     MAX_REORDER_DELAY      = 16,
44     MAX_PARAM_BUFFER_SIZE  = 1024,
45 };
46
47 enum {
48     PICTURE_TYPE_IDR = 0,
49     PICTURE_TYPE_I   = 1,
50     PICTURE_TYPE_P   = 2,
51     PICTURE_TYPE_B   = 3,
52 };
53
54 typedef struct VAAPIEncodeSlice {
55     int             index;
56     int             row_start;
57     int             row_size;
58     int             block_start;
59     int             block_size;
60     void           *priv_data;
61     void           *codec_slice_params;
62 } VAAPIEncodeSlice;
63
64 typedef struct VAAPIEncodePicture {
65     struct VAAPIEncodePicture *next;
66
67     int64_t         display_order;
68     int64_t         encode_order;
69     int64_t         pts;
70     int             force_idr;
71
72     int             type;
73     int             b_depth;
74     int             encode_issued;
75     int             encode_complete;
76
77     AVFrame        *input_image;
78     VASurfaceID     input_surface;
79
80     AVFrame        *recon_image;
81     VASurfaceID     recon_surface;
82
83     int          nb_param_buffers;
84     VABufferID     *param_buffers;
85
86     AVBufferRef    *output_buffer_ref;
87     VABufferID      output_buffer;
88
89     void           *priv_data;
90     void           *codec_picture_params;
91
92     // Whether this picture is a reference picture.
93     int             is_reference;
94
95     // The contents of the DPB after this picture has been decoded.
96     // This will contain the picture itself if it is a reference picture,
97     // but not if it isn't.
98     int                     nb_dpb_pics;
99     struct VAAPIEncodePicture *dpb[MAX_DPB_SIZE];
100     // The reference pictures used in decoding this picture.  If they are
101     // used by later pictures they will also appear in the DPB.
102     int                     nb_refs;
103     struct VAAPIEncodePicture *refs[MAX_PICTURE_REFERENCES];
104     // The previous reference picture in encode order.  Must be in at least
105     // one of the reference list and DPB list.
106     struct VAAPIEncodePicture *prev;
107     // Reference count for other pictures referring to this one through
108     // the above pointers, directly from incomplete pictures and indirectly
109     // through completed pictures.
110     int             ref_count[2];
111     int             ref_removed[2];
112
113     int          nb_slices;
114     VAAPIEncodeSlice *slices;
115 } VAAPIEncodePicture;
116
117 typedef struct VAAPIEncodeProfile {
118     // lavc profile value (FF_PROFILE_*).
119     int       av_profile;
120     // Supported bit depth.
121     int       depth;
122     // Number of components.
123     int       nb_components;
124     // Chroma subsampling in width dimension.
125     int       log2_chroma_w;
126     // Chroma subsampling in height dimension.
127     int       log2_chroma_h;
128     // VAAPI profile value.
129     VAProfile va_profile;
130 } VAAPIEncodeProfile;
131
132 typedef struct VAAPIEncodeContext {
133     const AVClass *class;
134
135     // Codec-specific hooks.
136     const struct VAAPIEncodeType *codec;
137
138     // Global options.
139
140     // Use low power encoding mode.
141     int             low_power;
142
143     // Number of I frames between IDR frames.
144     int             idr_interval;
145
146     // Desired B frame reference depth.
147     int             desired_b_depth;
148
149     // Desired packed headers.
150     unsigned int    desired_packed_headers;
151
152     // The required size of surfaces.  This is probably the input
153     // size (AVCodecContext.width|height) aligned up to whatever
154     // block size is required by the codec.
155     int             surface_width;
156     int             surface_height;
157
158     // The block size for slice calculations.
159     int             slice_block_width;
160     int             slice_block_height;
161
162     // Everything above this point must be set before calling
163     // ff_vaapi_encode_init().
164
165     // Chosen encoding profile details.
166     const VAAPIEncodeProfile *profile;
167
168     // Encoding profile (VAProfile*).
169     VAProfile       va_profile;
170     // Encoding entrypoint (VAEntryoint*).
171     VAEntrypoint    va_entrypoint;
172     // Rate control mode.
173     unsigned int    va_rc_mode;
174     // Bitrate for codec-specific encoder parameters.
175     unsigned int    va_bit_rate;
176     // Packed headers which will actually be sent.
177     unsigned int    va_packed_headers;
178
179     // Configuration attributes to use when creating va_config.
180     VAConfigAttrib  config_attributes[MAX_CONFIG_ATTRIBUTES];
181     int          nb_config_attributes;
182
183     VAConfigID      va_config;
184     VAContextID     va_context;
185
186     AVBufferRef    *device_ref;
187     AVHWDeviceContext *device;
188     AVVAAPIDeviceContext *hwctx;
189
190     // The hardware frame context containing the input frames.
191     AVBufferRef    *input_frames_ref;
192     AVHWFramesContext *input_frames;
193
194     // The hardware frame context containing the reconstructed frames.
195     AVBufferRef    *recon_frames_ref;
196     AVHWFramesContext *recon_frames;
197
198     // Pool of (reusable) bitstream output buffers.
199     AVBufferPool   *output_buffer_pool;
200
201     // Global parameters which will be applied at the start of the
202     // sequence (includes rate control parameters below).
203     VAEncMiscParameterBuffer *global_params[MAX_GLOBAL_PARAMS];
204     size_t          global_params_size[MAX_GLOBAL_PARAMS];
205     int          nb_global_params;
206
207     // Rate control parameters.
208     struct {
209         VAEncMiscParameterBuffer misc;
210         VAEncMiscParameterRateControl rc;
211     } rc_params;
212     struct {
213         VAEncMiscParameterBuffer misc;
214         VAEncMiscParameterHRD hrd;
215     } hrd_params;
216     struct {
217         VAEncMiscParameterBuffer misc;
218         VAEncMiscParameterFrameRate fr;
219     } fr_params;
220 #if VA_CHECK_VERSION(0, 36, 0)
221     struct {
222         VAEncMiscParameterBuffer misc;
223         VAEncMiscParameterBufferQualityLevel quality;
224     } quality_params;
225 #endif
226
227     // Per-sequence parameter structure (VAEncSequenceParameterBuffer*).
228     void           *codec_sequence_params;
229
230     // Per-sequence parameters found in the per-picture parameter
231     // structure (VAEncPictureParameterBuffer*).
232     void           *codec_picture_params;
233
234     // Current encoding window, in display (input) order.
235     VAAPIEncodePicture *pic_start, *pic_end;
236     // The next picture to use as the previous reference picture in
237     // encoding order.
238     VAAPIEncodePicture *next_prev;
239
240     // Next input order index (display order).
241     int64_t         input_order;
242     // Number of frames that output is behind input.
243     int64_t         output_delay;
244     // Next encode order index.
245     int64_t         encode_order;
246     // Number of frames decode output will need to be delayed.
247     int64_t         decode_delay;
248     // Next output order index (in encode order).
249     int64_t         output_order;
250
251     // Timestamp handling.
252     int64_t         first_pts;
253     int64_t         dts_pts_diff;
254     int64_t         ts_ring[MAX_REORDER_DELAY * 3];
255
256     // Slice structure.
257     int slice_block_rows;
258     int slice_block_cols;
259     int nb_slices;
260     int slice_size;
261
262     // Frame type decision.
263     int gop_size;
264     int closed_gop;
265     int gop_per_idr;
266     int p_per_i;
267     int max_b_depth;
268     int b_per_p;
269     int force_idr;
270     int idr_counter;
271     int gop_counter;
272     int end_of_stream;
273 } VAAPIEncodeContext;
274
275 enum {
276     // Codec supports controlling the subdivision of pictures into slices.
277     FLAG_SLICE_CONTROL         = 1 << 0,
278     // Codec only supports constant quality (no rate control).
279     FLAG_CONSTANT_QUALITY_ONLY = 1 << 1,
280     // Codec is intra-only.
281     FLAG_INTRA_ONLY            = 1 << 2,
282     // Codec supports B-pictures.
283     FLAG_B_PICTURES            = 1 << 3,
284     // Codec supports referencing B-pictures.
285     FLAG_B_PICTURE_REFERENCES  = 1 << 4,
286     // Codec supports non-IDR key pictures (that is, key pictures do
287     // not necessarily empty the DPB).
288     FLAG_NON_IDR_KEY_PICTURES  = 1 << 5,
289 };
290
291 typedef struct VAAPIEncodeType {
292     // List of supported profiles and corresponding VAAPI profiles.
293     // (Must end with FF_PROFILE_UNKNOWN.)
294     const VAAPIEncodeProfile *profiles;
295
296     // Codec feature flags.
297     int flags;
298
299     // Perform any extra codec-specific configuration after the
300     // codec context is initialised (set up the private data and
301     // add any necessary global parameters).
302     int (*configure)(AVCodecContext *avctx);
303
304     // The size of any private data structure associated with each
305     // picture (can be zero if not required).
306     size_t picture_priv_data_size;
307
308     // The size of the parameter structures:
309     // sizeof(VAEnc{type}ParameterBuffer{codec}).
310     size_t sequence_params_size;
311     size_t picture_params_size;
312     size_t slice_params_size;
313
314     // Fill the parameter structures.
315     int  (*init_sequence_params)(AVCodecContext *avctx);
316     int   (*init_picture_params)(AVCodecContext *avctx,
317                                  VAAPIEncodePicture *pic);
318     int     (*init_slice_params)(AVCodecContext *avctx,
319                                  VAAPIEncodePicture *pic,
320                                  VAAPIEncodeSlice *slice);
321
322     // The type used by the packed header: this should look like
323     // VAEncPackedHeader{something}.
324     int sequence_header_type;
325     int picture_header_type;
326     int slice_header_type;
327
328     // Write the packed header data to the provided buffer.
329     // The sequence header is also used to fill the codec extradata
330     // when the encoder is starting.
331     int (*write_sequence_header)(AVCodecContext *avctx,
332                                  char *data, size_t *data_len);
333     int  (*write_picture_header)(AVCodecContext *avctx,
334                                  VAAPIEncodePicture *pic,
335                                  char *data, size_t *data_len);
336     int    (*write_slice_header)(AVCodecContext *avctx,
337                                  VAAPIEncodePicture *pic,
338                                  VAAPIEncodeSlice *slice,
339                                  char *data, size_t *data_len);
340
341     // Fill an extra parameter structure, which will then be
342     // passed to vaRenderPicture().  Will be called repeatedly
343     // with increasing index argument until AVERROR_EOF is
344     // returned.
345     int    (*write_extra_buffer)(AVCodecContext *avctx,
346                                  VAAPIEncodePicture *pic,
347                                  int index, int *type,
348                                  char *data, size_t *data_len);
349
350     // Write an extra packed header.  Will be called repeatedly
351     // with increasing index argument until AVERROR_EOF is
352     // returned.
353     int    (*write_extra_header)(AVCodecContext *avctx,
354                                  VAAPIEncodePicture *pic,
355                                  int index, int *type,
356                                  char *data, size_t *data_len);
357 } VAAPIEncodeType;
358
359
360 int ff_vaapi_encode2(AVCodecContext *avctx, AVPacket *pkt,
361                      const AVFrame *input_image, int *got_packet);
362
363 int ff_vaapi_encode_send_frame(AVCodecContext *avctx, const AVFrame *frame);
364 int ff_vaapi_encode_receive_packet(AVCodecContext *avctx, AVPacket *pkt);
365
366 int ff_vaapi_encode_init(AVCodecContext *avctx);
367 int ff_vaapi_encode_close(AVCodecContext *avctx);
368
369
370 #define VAAPI_ENCODE_COMMON_OPTIONS \
371     { "low_power", \
372       "Use low-power encoding mode (only available on some platforms; " \
373       "may not support all encoding features)", \
374       OFFSET(common.low_power), AV_OPT_TYPE_BOOL, \
375       { .i64 = 0 }, 0, 1, FLAGS }, \
376     { "idr_interval", \
377       "Distance (in I-frames) between IDR frames", \
378       OFFSET(common.idr_interval), AV_OPT_TYPE_INT, \
379       { .i64 = 0 }, 0, INT_MAX, FLAGS }, \
380     { "b_depth", \
381       "Maximum B-frame reference depth", \
382       OFFSET(common.desired_b_depth), AV_OPT_TYPE_INT, \
383       { .i64 = 1 }, 1, INT_MAX, FLAGS }
384
385
386 #endif /* AVCODEC_VAAPI_ENCODE_H */