]> git.sesse.net Git - ffmpeg/blob - libavcodec/vaapi_encode.h
mpeg12dec: move setting first_field to mpeg_field_start()
[ffmpeg] / libavcodec / vaapi_encode.h
1 /*
2  * This file is part of Libav.
3  *
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.
8  *
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.
13  *
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
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 typedef struct VAAPIEncodeSlice {
52     void           *priv_data;
53     void           *codec_slice_params;
54 } VAAPIEncodeSlice;
55
56 typedef struct VAAPIEncodePicture {
57     struct VAAPIEncodePicture *next;
58
59     int64_t         display_order;
60     int64_t         encode_order;
61     int64_t         pts;
62
63     int             type;
64     int             input_available;
65     int             encode_issued;
66     int             encode_complete;
67
68     AVFrame        *input_image;
69     VASurfaceID     input_surface;
70
71     AVFrame        *recon_image;
72     VASurfaceID     recon_surface;
73
74     int          nb_param_buffers;
75     VABufferID      param_buffers[MAX_PARAM_BUFFERS];
76
77     AVBufferRef    *output_buffer_ref;
78     VABufferID      output_buffer;
79
80     void           *priv_data;
81     void           *codec_picture_params;
82
83     int          nb_refs;
84     struct VAAPIEncodePicture *refs[MAX_PICTURE_REFERENCES];
85
86     int          nb_slices;
87     VAAPIEncodeSlice *slices[MAX_PICTURE_SLICES];
88 } VAAPIEncodePicture;
89
90 typedef struct VAAPIEncodeContext {
91     const AVClass *class;
92
93     // Codec-specific hooks.
94     const struct VAAPIEncodeType *codec;
95
96     // Encoding profile (VAProfileXXX).
97     VAProfile       va_profile;
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;
104     // Supported packed headers (initially the desired set, modified
105     // later to what is actually supported).
106     unsigned int    va_packed_headers;
107
108     // The required size of surfaces.  This is probably the input
109     // size (AVCodecContext.width|height) aligned up to whatever
110     // block size is required by the codec.
111     int             surface_width;
112     int             surface_height;
113
114     // Everything above this point must be set before calling
115     // ff_vaapi_encode_init().
116
117     // Codec-specific state.
118     void *priv_data;
119
120     // Configuration attributes to use when creating va_config.
121     VAConfigAttrib  config_attributes[MAX_CONFIG_ATTRIBUTES];
122     int          nb_config_attributes;
123
124     VAConfigID      va_config;
125     VAContextID     va_context;
126
127     AVBufferRef    *device_ref;
128     AVHWDeviceContext *device;
129     AVVAAPIDeviceContext *hwctx;
130
131     // The hardware frame context containing the input frames.
132     AVBufferRef    *input_frames_ref;
133     AVHWFramesContext *input_frames;
134
135     // The hardware frame context containing the reconstructed frames.
136     AVBufferRef    *recon_frames_ref;
137     AVHWFramesContext *recon_frames;
138
139     // Pool of (reusable) bitstream output buffers.
140     AVBufferPool   *output_buffer_pool;
141
142     // Global parameters which will be applied at the start of the
143     // sequence (includes rate control parameters below).
144     VAEncMiscParameterBuffer *global_params[MAX_GLOBAL_PARAMS];
145     size_t          global_params_size[MAX_GLOBAL_PARAMS];
146     int          nb_global_params;
147
148     // Rate control parameters.
149     struct {
150         VAEncMiscParameterBuffer misc;
151         VAEncMiscParameterRateControl rc;
152     } rc_params;
153     struct {
154         VAEncMiscParameterBuffer misc;
155         VAEncMiscParameterHRD hrd;
156     } hrd_params;
157
158     // Per-sequence parameter structure (VAEncSequenceParameterBuffer*).
159     void           *codec_sequence_params;
160
161     // Per-sequence parameters found in the per-picture parameter
162     // structure (VAEncPictureParameterBuffer*).
163     void           *codec_picture_params;
164
165     // Current encoding window, in display (input) order.
166     VAAPIEncodePicture *pic_start, *pic_end;
167
168     // Next input order index (display order).
169     int64_t         input_order;
170     // Number of frames that output is behind input.
171     int64_t         output_delay;
172     // Number of frames decode output will need to be delayed.
173     int64_t         decode_delay;
174     // Next output order index (encode order).
175     int64_t         output_order;
176
177     enum {
178         // All encode operations are done independently (synchronise
179         // immediately after every operation).
180         ISSUE_MODE_SERIALISE_EVERYTHING = 0,
181         // Overlap as many operations as possible.
182         ISSUE_MODE_MAXIMISE_THROUGHPUT,
183         // Overlap operations only when satisfying parallel dependencies.
184         ISSUE_MODE_MINIMISE_LATENCY,
185     } issue_mode;
186
187     // Timestamp handling.
188     int64_t         first_pts;
189     int64_t         dts_pts_diff;
190     int64_t         ts_ring[MAX_REORDER_DELAY * 3];
191
192     // Frame type decision.
193     int i_per_idr;
194     int p_per_i;
195     int b_per_p;
196     int idr_counter;
197     int i_counter;
198     int p_counter;
199     int end_of_stream;
200
201     // Codec-local options are allocated to follow this structure in
202     // memory (in the AVCodec definition, set priv_data_size to
203     // sizeof(VAAPIEncodeContext) + sizeof(VAAPIEncodeFooOptions)).
204     void *codec_options;
205     char codec_options_data[0];
206 } VAAPIEncodeContext;
207
208
209 typedef struct VAAPIEncodeType {
210     size_t priv_data_size;
211
212     // Perform any extra codec-specific configuration after the
213     // codec context is initialised (set up the private data and
214     // add any necessary global parameters).
215     int (*configure)(AVCodecContext *avctx);
216
217     // The size of the parameter structures:
218     // sizeof(VAEnc{type}ParameterBuffer{codec}).
219     size_t sequence_params_size;
220     size_t picture_params_size;
221     size_t slice_params_size;
222
223     // Fill the parameter structures.
224     int  (*init_sequence_params)(AVCodecContext *avctx);
225     int   (*init_picture_params)(AVCodecContext *avctx,
226                                  VAAPIEncodePicture *pic);
227     int     (*init_slice_params)(AVCodecContext *avctx,
228                                  VAAPIEncodePicture *pic,
229                                  VAAPIEncodeSlice *slice);
230
231     // The type used by the packed header: this should look like
232     // VAEncPackedHeader{something}.
233     int sequence_header_type;
234     int picture_header_type;
235     int slice_header_type;
236
237     // Write the packed header data to the provided buffer.
238     // The sequence header is also used to fill the codec extradata
239     // when the encoder is starting.
240     int (*write_sequence_header)(AVCodecContext *avctx,
241                                  char *data, size_t *data_len);
242     int  (*write_picture_header)(AVCodecContext *avctx,
243                                  VAAPIEncodePicture *pic,
244                                  char *data, size_t *data_len);
245     int    (*write_slice_header)(AVCodecContext *avctx,
246                                  VAAPIEncodePicture *pic,
247                                  VAAPIEncodeSlice *slice,
248                                  char *data, size_t *data_len);
249
250     // Fill an extra parameter structure, which will then be
251     // passed to vaRenderPicture().  Will be called repeatedly
252     // with increasing index argument until AVERROR_EOF is
253     // returned.
254     int    (*write_extra_buffer)(AVCodecContext *avctx,
255                                  VAAPIEncodePicture *pic,
256                                  int index, int *type,
257                                  char *data, size_t *data_len);
258
259     // Write an extra packed header.  Will be called repeatedly
260     // with increasing index argument until AVERROR_EOF is
261     // returned.
262     int    (*write_extra_header)(AVCodecContext *avctx,
263                                  VAAPIEncodePicture *pic,
264                                  int index, int *type,
265                                  char *data, size_t *data_len);
266 } VAAPIEncodeType;
267
268
269 int ff_vaapi_encode2(AVCodecContext *avctx, AVPacket *pkt,
270                      const AVFrame *input_image, int *got_packet);
271
272 int ff_vaapi_encode_init(AVCodecContext *avctx);
273 int ff_vaapi_encode_close(AVCodecContext *avctx);
274
275 #endif /* AVCODEC_VAAPI_ENCODE_H */