]> git.sesse.net Git - ffmpeg/blob - libavcodec/vaapi_encode.h
Merge commit 'd60c2d5216930ef98c7d4d6837d6229b37e0dcb3'
[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 #include "libavutil/hwcontext.h"
27 #include "libavutil/hwcontext_vaapi.h"
28
29 #include "avcodec.h"
30
31 struct VAAPIEncodeType;
32 struct VAAPIEncodePicture;
33
34 enum {
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,
42 };
43
44 enum {
45     PICTURE_TYPE_IDR = 0,
46     PICTURE_TYPE_I   = 1,
47     PICTURE_TYPE_P   = 2,
48     PICTURE_TYPE_B   = 3,
49 };
50
51 enum {
52     // All encode operations are done independently.
53     ISSUE_MODE_SERIALISE_EVERYTHING = 0,
54     // Overlap as many operations as possible.
55     ISSUE_MODE_MAXIMISE_THROUGHPUT,
56     // Overlap operations only when satisfying parallel dependencies.
57     ISSUE_MODE_MINIMISE_LATENCY,
58 };
59
60 typedef struct VAAPIEncodeSlice {
61     void           *priv_data;
62     void           *codec_slice_params;
63 } VAAPIEncodeSlice;
64
65 typedef struct VAAPIEncodePicture {
66     struct VAAPIEncodePicture *next;
67
68     int64_t         display_order;
69     int64_t         encode_order;
70     int64_t         pts;
71
72     int             type;
73     int             input_available;
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[MAX_PARAM_BUFFERS];
85
86     AVBufferRef    *output_buffer_ref;
87     VABufferID      output_buffer;
88
89     void           *priv_data;
90     void           *codec_picture_params;
91
92     int          nb_refs;
93     struct VAAPIEncodePicture *refs[MAX_PICTURE_REFERENCES];
94
95     int          nb_slices;
96     VAAPIEncodeSlice *slices[MAX_PICTURE_SLICES];
97 } VAAPIEncodePicture;
98
99 typedef struct VAAPIEncodeContext {
100     const AVClass *class;
101
102     // Codec-specific hooks.
103     const struct VAAPIEncodeType *codec;
104
105     // Codec-specific state.
106     void *priv_data;
107
108     VAProfile       va_profile;
109     VAEntrypoint    va_entrypoint;
110     VAConfigID      va_config;
111     VAContextID     va_context;
112
113     int             va_rc_mode;
114
115     AVBufferRef    *device_ref;
116     AVHWDeviceContext *device;
117     AVVAAPIDeviceContext *hwctx;
118
119     AVBufferRef    *input_frames_ref;
120     AVHWFramesContext *input_frames;
121
122     // Input size, set from input frames.
123     int             input_width;
124     int             input_height;
125     // Aligned size, set by codec init, becomes hwframe size.
126     int             aligned_width;
127     int             aligned_height;
128
129     int          nb_recon_frames;
130     AVBufferRef    *recon_frames_ref;
131     AVHWFramesContext *recon_frames;
132
133     AVBufferPool   *output_buffer_pool;
134
135     VAConfigAttrib  config_attributes[MAX_CONFIG_ATTRIBUTES];
136     int          nb_config_attributes;
137
138     VAEncMiscParameterBuffer *global_params[MAX_GLOBAL_PARAMS];
139     size_t          global_params_size[MAX_GLOBAL_PARAMS];
140     int          nb_global_params;
141
142     // Per-sequence parameter structure (VAEncSequenceParameterBuffer*).
143     void           *codec_sequence_params;
144
145     // Per-sequence parameters found in the per-picture parameter
146     // structure (VAEncPictureParameterBuffer*).
147     void           *codec_picture_params;
148
149     // Current encoding window, in display (input) order.
150     VAAPIEncodePicture *pic_start, *pic_end;
151
152     // Next input order index (display order).
153     int64_t         input_order;
154     // Number of frames that output is behind input.
155     int64_t         output_delay;
156     // Number of frames decode output will need to be delayed.
157     int64_t         decode_delay;
158     // Next output order index (encode order).
159     int64_t         output_order;
160
161     int             issue_mode;
162
163     // Timestamp handling.
164     int64_t         first_pts;
165     int64_t         dts_pts_diff;
166     int64_t         ts_ring[MAX_REORDER_DELAY * 3];
167
168     // Frame type decision.
169     int i_per_idr;
170     int p_per_i;
171     int b_per_p;
172     int idr_counter;
173     int i_counter;
174     int p_counter;
175     int end_of_stream;
176
177     // Codec-local options are allocated to follow this structure in
178     // memory (in the AVCodec definition, set priv_data_size to
179     // sizeof(VAAPIEncodeContext) + sizeof(VAAPIEncodeFooOptions)).
180     void *codec_options;
181     char codec_options_data[0];
182 } VAAPIEncodeContext;
183
184
185 typedef struct VAAPIEncodeType {
186     size_t    priv_data_size;
187
188     int  (*init)(AVCodecContext *avctx);
189     int (*close)(AVCodecContext *avctx);
190
191     size_t sequence_params_size;
192     size_t picture_params_size;
193     size_t slice_params_size;
194
195     int  (*init_sequence_params)(AVCodecContext *avctx);
196     int   (*init_picture_params)(AVCodecContext *avctx,
197                                  VAAPIEncodePicture *pic);
198     int     (*init_slice_params)(AVCodecContext *avctx,
199                                  VAAPIEncodePicture *pic,
200                                  VAAPIEncodeSlice *slice);
201
202     int sequence_header_type;
203     int picture_header_type;
204     int slice_header_type;
205
206     int (*write_sequence_header)(AVCodecContext *avctx,
207                                  char *data, size_t *data_len);
208     int  (*write_picture_header)(AVCodecContext *avctx,
209                                  VAAPIEncodePicture *pic,
210                                  char *data, size_t *data_len);
211     int    (*write_slice_header)(AVCodecContext *avctx,
212                                  VAAPIEncodePicture *pic,
213                                  VAAPIEncodeSlice *slice,
214                                  char *data, size_t *data_len);
215
216     int    (*write_extra_buffer)(AVCodecContext *avctx,
217                                  VAAPIEncodePicture *pic,
218                                  int index, int *type,
219                                  char *data, size_t *data_len);
220     int    (*write_extra_header)(AVCodecContext *avctx,
221                                  VAAPIEncodePicture *pic,
222                                  int index, int *type,
223                                  char *data, size_t *data_len);
224 } VAAPIEncodeType;
225
226
227 int ff_vaapi_encode2(AVCodecContext *avctx, AVPacket *pkt,
228                      const AVFrame *input_image, int *got_packet);
229
230 int ff_vaapi_encode_init(AVCodecContext *avctx,
231                          const VAAPIEncodeType *type);
232 int ff_vaapi_encode_close(AVCodecContext *avctx);
233
234 #endif /* AVCODEC_VAAPI_ENCODE_H */