]> git.sesse.net Git - ffmpeg/blob - libavcodec/vaapi_encode_vp9.c
avcodec: rename the AV1 profiles
[ffmpeg] / libavcodec / vaapi_encode_vp9.c
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 #include <va/va.h>
20 #include <va/va_enc_vp9.h>
21
22 #include "libavutil/avassert.h"
23 #include "libavutil/common.h"
24 #include "libavutil/internal.h"
25 #include "libavutil/opt.h"
26 #include "libavutil/pixfmt.h"
27
28 #include "avcodec.h"
29 #include "internal.h"
30 #include "vaapi_encode.h"
31
32
33 typedef struct VAAPIEncodeVP9Context {
34     int q_idx_idr;
35     int q_idx_p;
36     int q_idx_b;
37
38     // Reference direction for B-like frames:
39     // 0 - most recent P/IDR frame is last.
40     // 1 - most recent P frame is golden.
41     int last_ref_dir;
42 } VAAPIEncodeVP9Context;
43
44 typedef struct VAAPIEncodeVP9Options {
45     int loop_filter_level;
46     int loop_filter_sharpness;
47 } VAAPIEncodeVP9Options;
48
49
50 #define vseq_var(name)     vseq->name, name
51 #define vseq_field(name)   vseq->seq_fields.bits.name, name
52 #define vpic_var(name)     vpic->name, name
53 #define vpic_field(name)   vpic->pic_fields.bits.name, name
54
55
56 static int vaapi_encode_vp9_init_sequence_params(AVCodecContext *avctx)
57 {
58     VAAPIEncodeContext               *ctx = avctx->priv_data;
59     VAEncSequenceParameterBufferVP9 *vseq = ctx->codec_sequence_params;
60     VAEncPictureParameterBufferVP9  *vpic = ctx->codec_picture_params;
61
62     vseq->max_frame_width  = avctx->width;
63     vseq->max_frame_height = avctx->height;
64
65     vseq->kf_auto = 0;
66
67     if (!(ctx->va_rc_mode & VA_RC_CQP)) {
68         vseq->bits_per_second = avctx->bit_rate;
69         vseq->intra_period    = avctx->gop_size;
70     }
71
72     vpic->frame_width_src  = avctx->width;
73     vpic->frame_height_src = avctx->height;
74     vpic->frame_width_dst  = avctx->width;
75     vpic->frame_height_dst = avctx->height;
76
77     return 0;
78 }
79
80 static int vaapi_encode_vp9_init_picture_params(AVCodecContext *avctx,
81                                                 VAAPIEncodePicture *pic)
82 {
83     VAAPIEncodeContext              *ctx = avctx->priv_data;
84     VAEncPictureParameterBufferVP9 *vpic = pic->codec_picture_params;
85     VAAPIEncodeVP9Context          *priv = ctx->priv_data;
86     VAAPIEncodeVP9Options           *opt = ctx->codec_options;
87     int i;
88
89     vpic->reconstructed_frame = pic->recon_surface;
90     vpic->coded_buf = pic->output_buffer;
91
92     switch (pic->type) {
93     case PICTURE_TYPE_IDR:
94         av_assert0(pic->nb_refs == 0);
95         vpic->ref_flags.bits.force_kf = 1;
96         vpic->refresh_frame_flags = 0x01;
97         priv->last_ref_dir = 0;
98         break;
99     case PICTURE_TYPE_P:
100         av_assert0(pic->nb_refs == 1);
101         if (avctx->max_b_frames > 0) {
102             if (priv->last_ref_dir) {
103                 vpic->ref_flags.bits.ref_frame_ctrl_l0  = 2;
104                 vpic->ref_flags.bits.ref_gf_idx         = 1;
105                 vpic->ref_flags.bits.ref_gf_sign_bias   = 1;
106                 vpic->refresh_frame_flags = 0x01;
107             } else {
108                 vpic->ref_flags.bits.ref_frame_ctrl_l0  = 1;
109                 vpic->ref_flags.bits.ref_last_idx       = 0;
110                 vpic->ref_flags.bits.ref_last_sign_bias = 1;
111                 vpic->refresh_frame_flags = 0x02;
112             }
113         } else {
114             vpic->ref_flags.bits.ref_frame_ctrl_l0  = 1;
115             vpic->ref_flags.bits.ref_last_idx       = 0;
116             vpic->ref_flags.bits.ref_last_sign_bias = 1;
117             vpic->refresh_frame_flags = 0x01;
118         }
119         break;
120     case PICTURE_TYPE_B:
121         av_assert0(pic->nb_refs == 2);
122         if (priv->last_ref_dir) {
123             vpic->ref_flags.bits.ref_frame_ctrl_l0  = 1;
124             vpic->ref_flags.bits.ref_frame_ctrl_l1  = 2;
125             vpic->ref_flags.bits.ref_last_idx       = 0;
126             vpic->ref_flags.bits.ref_last_sign_bias = 1;
127             vpic->ref_flags.bits.ref_gf_idx         = 1;
128             vpic->ref_flags.bits.ref_gf_sign_bias   = 0;
129         } else {
130             vpic->ref_flags.bits.ref_frame_ctrl_l0  = 2;
131             vpic->ref_flags.bits.ref_frame_ctrl_l1  = 1;
132             vpic->ref_flags.bits.ref_last_idx       = 0;
133             vpic->ref_flags.bits.ref_last_sign_bias = 0;
134             vpic->ref_flags.bits.ref_gf_idx         = 1;
135             vpic->ref_flags.bits.ref_gf_sign_bias   = 1;
136         }
137         vpic->refresh_frame_flags = 0x00;
138         break;
139     default:
140         av_assert0(0 && "invalid picture type");
141     }
142
143     for (i = 0; i < FF_ARRAY_ELEMS(vpic->reference_frames); i++)
144         vpic->reference_frames[i] = VA_INVALID_SURFACE;
145     if (pic->type == PICTURE_TYPE_P) {
146         av_assert0(pic->refs[0]);
147         vpic->reference_frames[priv->last_ref_dir] =
148             pic->refs[0]->recon_surface;
149     } else if (pic->type == PICTURE_TYPE_B) {
150         av_assert0(pic->refs[0] && pic->refs[1]);
151         vpic->reference_frames[!priv->last_ref_dir] =
152             pic->refs[0]->recon_surface;
153         vpic->reference_frames[priv->last_ref_dir] =
154             pic->refs[1]->recon_surface;
155     }
156
157     vpic->pic_flags.bits.frame_type = (pic->type != PICTURE_TYPE_IDR);
158     vpic->pic_flags.bits.show_frame = pic->display_order <= pic->encode_order;
159
160     if (pic->type == PICTURE_TYPE_IDR)
161         vpic->luma_ac_qindex     = priv->q_idx_idr;
162     else if (pic->type == PICTURE_TYPE_P)
163         vpic->luma_ac_qindex     = priv->q_idx_p;
164     else
165         vpic->luma_ac_qindex     = priv->q_idx_b;
166     vpic->luma_dc_qindex_delta   = 0;
167     vpic->chroma_ac_qindex_delta = 0;
168     vpic->chroma_dc_qindex_delta = 0;
169
170     vpic->filter_level    = opt->loop_filter_level;
171     vpic->sharpness_level = opt->loop_filter_sharpness;
172
173     if (avctx->max_b_frames > 0 && pic->type == PICTURE_TYPE_P)
174         priv->last_ref_dir = !priv->last_ref_dir;
175
176     return 0;
177 }
178
179 static av_cold int vaapi_encode_vp9_configure(AVCodecContext *avctx)
180 {
181     VAAPIEncodeContext     *ctx = avctx->priv_data;
182     VAAPIEncodeVP9Context *priv = ctx->priv_data;
183
184     priv->q_idx_p = av_clip(avctx->global_quality, 0, 255);
185     if (avctx->i_quant_factor > 0.0)
186         priv->q_idx_idr = av_clip((avctx->global_quality *
187                                    avctx->i_quant_factor +
188                                    avctx->i_quant_offset) + 0.5,
189                                   0, 255);
190     else
191         priv->q_idx_idr = priv->q_idx_p;
192     if (avctx->b_quant_factor > 0.0)
193         priv->q_idx_b = av_clip((avctx->global_quality *
194                                  avctx->b_quant_factor +
195                                  avctx->b_quant_offset) + 0.5,
196                                 0, 255);
197     else
198         priv->q_idx_b = priv->q_idx_p;
199
200     return 0;
201 }
202
203 static const VAAPIEncodeType vaapi_encode_type_vp9 = {
204     .configure             = &vaapi_encode_vp9_configure,
205
206     .priv_data_size        = sizeof(VAAPIEncodeVP9Context),
207
208     .sequence_params_size  = sizeof(VAEncSequenceParameterBufferVP9),
209     .init_sequence_params  = &vaapi_encode_vp9_init_sequence_params,
210
211     .picture_params_size   = sizeof(VAEncPictureParameterBufferVP9),
212     .init_picture_params   = &vaapi_encode_vp9_init_picture_params,
213 };
214
215 static av_cold int vaapi_encode_vp9_init(AVCodecContext *avctx)
216 {
217     VAAPIEncodeContext *ctx = avctx->priv_data;
218
219     ctx->codec = &vaapi_encode_type_vp9;
220
221     switch (avctx->profile) {
222     case FF_PROFILE_VP9_0:
223     case FF_PROFILE_UNKNOWN:
224         ctx->va_profile = VAProfileVP9Profile0;
225         ctx->va_rt_format = VA_RT_FORMAT_YUV420;
226         break;
227     case FF_PROFILE_VP9_1:
228         av_log(avctx, AV_LOG_ERROR, "VP9 profile 1 is not "
229                "supported.\n");
230         return AVERROR_PATCHWELCOME;
231     case FF_PROFILE_VP9_2:
232         ctx->va_profile = VAProfileVP9Profile2;
233         ctx->va_rt_format = VA_RT_FORMAT_YUV420_10BPP;
234         break;
235     case FF_PROFILE_VP9_3:
236         av_log(avctx, AV_LOG_ERROR, "VP9 profile 3 is not "
237                "supported.\n");
238         return AVERROR_PATCHWELCOME;
239     default:
240         av_log(avctx, AV_LOG_ERROR, "Unknown VP9 profile %d.\n",
241                avctx->profile);
242         return AVERROR(EINVAL);
243     }
244     ctx->va_entrypoint = VAEntrypointEncSlice;
245
246     if (avctx->flags & AV_CODEC_FLAG_QSCALE) {
247         ctx->va_rc_mode = VA_RC_CQP;
248     } else if (avctx->bit_rate > 0) {
249         if (avctx->bit_rate == avctx->rc_max_rate)
250             ctx->va_rc_mode = VA_RC_CBR;
251         else
252             ctx->va_rc_mode = VA_RC_VBR;
253     } else {
254         ctx->va_rc_mode = VA_RC_CQP;
255     }
256
257     // Packed headers are not currently supported.
258     ctx->va_packed_headers = 0;
259
260     // Surfaces must be aligned to superblock boundaries.
261     ctx->surface_width  = FFALIGN(avctx->width,  64);
262     ctx->surface_height = FFALIGN(avctx->height, 64);
263
264     return ff_vaapi_encode_init(avctx);
265 }
266
267 #define OFFSET(x) (offsetof(VAAPIEncodeContext, codec_options_data) + \
268                    offsetof(VAAPIEncodeVP9Options, x))
269 #define FLAGS (AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM)
270 static const AVOption vaapi_encode_vp9_options[] = {
271     { "loop_filter_level", "Loop filter level",
272       OFFSET(loop_filter_level), AV_OPT_TYPE_INT, { .i64 = 16 }, 0, 63, FLAGS },
273     { "loop_filter_sharpness", "Loop filter sharpness",
274       OFFSET(loop_filter_sharpness), AV_OPT_TYPE_INT, { .i64 = 4 }, 0, 15, FLAGS },
275     { NULL },
276 };
277
278 static const AVCodecDefault vaapi_encode_vp9_defaults[] = {
279     { "profile",        "0"   },
280     { "b",              "0"   },
281     { "bf",             "0"   },
282     { "g",              "250" },
283     { "global_quality", "100" },
284     { NULL },
285 };
286
287 static const AVClass vaapi_encode_vp9_class = {
288     .class_name = "vp9_vaapi",
289     .item_name  = av_default_item_name,
290     .option     = vaapi_encode_vp9_options,
291     .version    = LIBAVUTIL_VERSION_INT,
292 };
293
294 AVCodec ff_vp9_vaapi_encoder = {
295     .name           = "vp9_vaapi",
296     .long_name      = NULL_IF_CONFIG_SMALL("VP9 (VAAPI)"),
297     .type           = AVMEDIA_TYPE_VIDEO,
298     .id             = AV_CODEC_ID_VP9,
299     .priv_data_size = (sizeof(VAAPIEncodeContext) +
300                        sizeof(VAAPIEncodeVP9Options)),
301     .init           = &vaapi_encode_vp9_init,
302     .encode2        = &ff_vaapi_encode2,
303     .close          = &ff_vaapi_encode_close,
304     .priv_class     = &vaapi_encode_vp9_class,
305     .capabilities   = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_HARDWARE,
306     .defaults       = vaapi_encode_vp9_defaults,
307     .pix_fmts = (const enum AVPixelFormat[]) {
308         AV_PIX_FMT_VAAPI,
309         AV_PIX_FMT_NONE,
310     },
311     .wrapper_name   = "vaapi",
312 };