9716578e5a62d5ad0c873c9be0bbcc7dfde8dccc
[ffmpeg.git] / 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     MAX_OUTPUT_BUFFER_SIZE = 1024 * 1024,
43 };
44
45 enum {
46     PICTURE_TYPE_IDR = 0,
47     PICTURE_TYPE_I   = 1,
48     PICTURE_TYPE_P   = 2,
49     PICTURE_TYPE_B   = 3,
50 };
51
52 enum {
53     // All encode operations are done independently.
54     ISSUE_MODE_SERIALISE_EVERYTHING = 0,
55     // Overlap as many operations as possible.
56     ISSUE_MODE_MAXIMISE_THROUGHPUT,
57     // Overlap operations only when satisfying parallel dependencies.
58     ISSUE_MODE_MINIMISE_LATENCY,
59 };
60
61 typedef struct VAAPIEncodeSlice {
62     void           *priv_data;
63     void           *codec_slice_params;
64 } VAAPIEncodeSlice;
65
66 typedef struct VAAPIEncodePicture {
67     struct VAAPIEncodePicture *next;
68
69     int64_t         display_order;
70     int64_t         encode_order;
71     int64_t         pts;
72
73     int             type;
74     int             input_available;
75     int             encode_issued;
76     int             encode_complete;
77
78     AVFrame        *input_image;
79     VASurfaceID     input_surface;
80
81     AVFrame        *recon_image;
82     VASurfaceID     recon_surface;
83
84     int          nb_param_buffers;
85     VABufferID      param_buffers[MAX_PARAM_BUFFERS];
86
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     VAConfigAttrib  config_attributes[MAX_CONFIG_ATTRIBUTES];
134     int          nb_config_attributes;
135
136     VAEncMiscParameterBuffer *global_params[MAX_GLOBAL_PARAMS];
137     size_t          global_params_size[MAX_GLOBAL_PARAMS];
138     int          nb_global_params;
139
140     // Per-sequence parameter structure (VAEncSequenceParameterBuffer*).
141     void           *codec_sequence_params;
142
143     // Per-sequence parameters found in the per-picture parameter
144     // structure (VAEncPictureParameterBuffer*).
145     void           *codec_picture_params;
146
147     // Current encoding window, in display (input) order.
148     VAAPIEncodePicture *pic_start, *pic_end;
149
150     // Next input order index (display order).
151     int64_t         input_order;
152     // Number of frames that output is behind input.
153     int64_t         output_delay;
154     // Number of frames decode output will need to be delayed.
155     int64_t         decode_delay;
156     // Next output order index (encode order).
157     int64_t         output_order;
158
159     int             issue_mode;
160
161     // Timestamp handling.
162     int64_t         first_pts;
163     int64_t         dts_pts_diff;
164     int64_t         ts_ring[MAX_REORDER_DELAY * 3];
165
166     // Frame type decision.
167     int i_per_idr;
168     int p_per_i;
169     int b_per_p;
170     int idr_counter;
171     int i_counter;
172     int p_counter;
173     int end_of_stream;
174
175 } VAAPIEncodeContext;
176
177
178 typedef struct VAAPIEncodeType {
179     size_t    priv_data_size;
180
181     int  (*init)(AVCodecContext *avctx);
182     int (*close)(AVCodecContext *avctx);
183
184     size_t sequence_params_size;
185     size_t picture_params_size;
186     size_t slice_params_size;
187
188     int  (*init_sequence_params)(AVCodecContext *avctx);
189     int   (*init_picture_params)(AVCodecContext *avctx,
190                                  VAAPIEncodePicture *pic);
191     int     (*init_slice_params)(AVCodecContext *avctx,
192                                  VAAPIEncodePicture *pic,
193                                  VAAPIEncodeSlice *slice);
194
195     int sequence_header_type;
196     int picture_header_type;
197     int slice_header_type;
198
199     int (*write_sequence_header)(AVCodecContext *avctx,
200                                  char *data, size_t *data_len);
201     int  (*write_picture_header)(AVCodecContext *avctx,
202                                  VAAPIEncodePicture *pic,
203                                  char *data, size_t *data_len);
204     int    (*write_slice_header)(AVCodecContext *avctx,
205                                  VAAPIEncodePicture *pic,
206                                  VAAPIEncodeSlice *slice,
207                                  char *data, size_t *data_len);
208
209     int    (*write_extra_buffer)(AVCodecContext *avctx,
210                                  VAAPIEncodePicture *pic,
211                                  int index, int *type,
212                                  char *data, size_t *data_len);
213 } VAAPIEncodeType;
214
215
216 int ff_vaapi_encode2(AVCodecContext *avctx, AVPacket *pkt,
217                      const AVFrame *input_image, int *got_packet);
218
219 int ff_vaapi_encode_init(AVCodecContext *avctx,
220                          const VAAPIEncodeType *type);
221 int ff_vaapi_encode_close(AVCodecContext *avctx);
222
223 #endif /* AVCODEC_VAAPI_ENCODE_H */