lavc: VAAPI encode common infrastructure
[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_PICTURE_REFERENCES = 2,
36     MAX_PICTURE_SLICES     = 1,
37     MAX_PARAM_BUFFERS      = 16,
38     MAX_REORDER_DELAY      = 16,
39     MAX_PARAM_BUFFER_SIZE  = 1024,
40     MAX_OUTPUT_BUFFER_SIZE = 1024 * 1024,
41 };
42
43 enum {
44     PICTURE_TYPE_IDR = 0,
45     PICTURE_TYPE_I   = 1,
46     PICTURE_TYPE_P   = 2,
47     PICTURE_TYPE_B   = 3,
48 };
49
50 enum {
51     // All encode operations are done independently.
52     ISSUE_MODE_SERIALISE_EVERYTHING = 0,
53     // Overlap as many operations as possible.
54     ISSUE_MODE_MAXIMISE_THROUGHPUT,
55     // Overlap operations only when satisfying parallel dependencies.
56     ISSUE_MODE_MINIMISE_LATENCY,
57 };
58
59 typedef struct VAAPIEncodeSlice {
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
71     int             type;
72     int             input_available;
73     int             encode_issued;
74     int             encode_complete;
75
76     AVFrame        *input_image;
77     VASurfaceID     input_surface;
78
79     AVFrame        *recon_image;
80     VASurfaceID     recon_surface;
81
82     int          nb_param_buffers;
83     VABufferID      param_buffers[MAX_PARAM_BUFFERS];
84
85     VABufferID      output_buffer;
86
87     void           *priv_data;
88     void           *codec_picture_params;
89
90     int          nb_refs;
91     struct VAAPIEncodePicture *refs[MAX_PICTURE_REFERENCES];
92
93     int          nb_slices;
94     VAAPIEncodeSlice *slices[MAX_PICTURE_SLICES];
95 } VAAPIEncodePicture;
96
97 typedef struct VAAPIEncodeContext {
98     const AVClass *class;
99
100     // Codec-specific hooks.
101     const struct VAAPIEncodeType *codec;
102
103     // Codec-specific state.
104     void *priv_data;
105
106     VAProfile       va_profile;
107     VAEntrypoint    va_entrypoint;
108     VAConfigID      va_config;
109     VAContextID     va_context;
110
111     int             va_rc_mode;
112
113     AVBufferRef    *device_ref;
114     AVHWDeviceContext *device;
115     AVVAAPIDeviceContext *hwctx;
116
117     AVBufferRef    *input_frames_ref;
118     AVHWFramesContext *input_frames;
119
120     // Input size, set from input frames.
121     int             input_width;
122     int             input_height;
123     // Aligned size, set by codec init, becomes hwframe size.
124     int             aligned_width;
125     int             aligned_height;
126
127     int          nb_recon_frames;
128     AVBufferRef    *recon_frames_ref;
129     AVHWFramesContext *recon_frames;
130
131     VAConfigAttrib *config_attributes;
132     int          nb_config_attributes;
133
134     // Per-sequence parameter structure (VAEncSequenceParameterBuffer*).
135     void           *codec_sequence_params;
136
137     // Per-sequence parameters found in the per-picture parameter
138     // structure (VAEncPictureParameterBuffer*).
139     void *codec_picture_params;
140
141     // Current encoding window, in display (input) order.
142     VAAPIEncodePicture *pic_start, *pic_end;
143
144     // Next input order index (display order).
145     int64_t         input_order;
146     // Number of frames that output is behind input.
147     int64_t         output_delay;
148     // Number of frames decode output will need to be delayed.
149     int64_t         decode_delay;
150     // Next output order index (encode order).
151     int64_t         output_order;
152
153     int             issue_mode;
154
155     // Timestamp handling.
156     int64_t         first_pts;
157     int64_t         dts_pts_diff;
158     int64_t         ts_ring[MAX_REORDER_DELAY * 3];
159
160     // Frame type decision.
161     int i_per_idr;
162     int p_per_i;
163     int b_per_p;
164     int idr_counter;
165     int i_counter;
166     int p_counter;
167     int end_of_stream;
168
169 } VAAPIEncodeContext;
170
171
172 typedef struct VAAPIEncodeType {
173     size_t    priv_data_size;
174
175     int  (*init)(AVCodecContext *avctx);
176     int (*close)(AVCodecContext *avctx);
177
178     size_t sequence_params_size;
179     size_t picture_params_size;
180     size_t slice_params_size;
181
182     int  (*init_sequence_params)(AVCodecContext *avctx);
183     int   (*init_picture_params)(AVCodecContext *avctx,
184                                  VAAPIEncodePicture *pic);
185     int     (*init_slice_params)(AVCodecContext *avctx,
186                                  VAAPIEncodePicture *pic,
187                                  VAAPIEncodeSlice *slice);
188
189     int sequence_header_type;
190     int picture_header_type;
191     int slice_header_type;
192
193     int (*write_sequence_header)(AVCodecContext *avctx,
194                                  char *data, size_t *data_len);
195     int  (*write_picture_header)(AVCodecContext *avctx,
196                                  VAAPIEncodePicture *pic,
197                                  char *data, size_t *data_len);
198     int    (*write_slice_header)(AVCodecContext *avctx,
199                                  VAAPIEncodePicture *pic,
200                                  VAAPIEncodeSlice *slice,
201                                  char *data, size_t *data_len);
202
203     int    (*write_extra_buffer)(AVCodecContext *avctx,
204                                  VAAPIEncodePicture *pic,
205                                  int index, int *type,
206                                  char *data, size_t *data_len);
207 } VAAPIEncodeType;
208
209
210 int ff_vaapi_encode2(AVCodecContext *avctx, AVPacket *pkt,
211                      const AVFrame *input_image, int *got_packet);
212
213 int ff_vaapi_encode_init(AVCodecContext *avctx,
214                          const VAAPIEncodeType *type);
215 int ff_vaapi_encode_close(AVCodecContext *avctx);
216
217 #endif /* AVCODEC_VAAPI_ENCODE_H */