2 * This file is part of FFmpeg.
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.
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.
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
20 #include <va/va_enc_vp9.h>
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"
30 #include "vaapi_encode.h"
32 #define VP9_MAX_QUANT 255
35 typedef struct VAAPIEncodeVP9Picture {
37 } VAAPIEncodeVP9Picture;
39 typedef struct VAAPIEncodeVP9Context {
40 VAAPIEncodeContext common;
43 int loop_filter_level;
44 int loop_filter_sharpness;
50 } VAAPIEncodeVP9Context;
53 static int vaapi_encode_vp9_init_sequence_params(AVCodecContext *avctx)
55 VAAPIEncodeContext *ctx = avctx->priv_data;
56 VAEncSequenceParameterBufferVP9 *vseq = ctx->codec_sequence_params;
57 VAEncPictureParameterBufferVP9 *vpic = ctx->codec_picture_params;
59 vseq->max_frame_width = avctx->width;
60 vseq->max_frame_height = avctx->height;
64 if (!(ctx->va_rc_mode & VA_RC_CQP)) {
65 vseq->bits_per_second = ctx->va_bit_rate;
66 vseq->intra_period = ctx->gop_size;
69 vpic->frame_width_src = avctx->width;
70 vpic->frame_height_src = avctx->height;
71 vpic->frame_width_dst = avctx->width;
72 vpic->frame_height_dst = avctx->height;
77 static int vaapi_encode_vp9_init_picture_params(AVCodecContext *avctx,
78 VAAPIEncodePicture *pic)
80 VAAPIEncodeContext *ctx = avctx->priv_data;
81 VAAPIEncodeVP9Context *priv = avctx->priv_data;
82 VAAPIEncodeVP9Picture *hpic = pic->priv_data;
83 VAEncPictureParameterBufferVP9 *vpic = pic->codec_picture_params;
86 vpic->reconstructed_frame = pic->recon_surface;
87 vpic->coded_buf = pic->output_buffer;
90 case PICTURE_TYPE_IDR:
91 av_assert0(pic->nb_refs == 0);
92 vpic->ref_flags.bits.force_kf = 1;
93 vpic->refresh_frame_flags = 0xff;
97 av_assert0(pic->nb_refs == 1);
99 VAAPIEncodeVP9Picture *href = pic->refs[0]->priv_data;
100 av_assert0(href->slot == 0 || href->slot == 1);
102 if (ctx->max_b_depth > 0) {
103 hpic->slot = !href->slot;
104 vpic->refresh_frame_flags = 1 << hpic->slot | 0xfc;
107 vpic->refresh_frame_flags = 0xff;
109 vpic->ref_flags.bits.ref_frame_ctrl_l0 = 1;
110 vpic->ref_flags.bits.ref_last_idx = href->slot;
111 vpic->ref_flags.bits.ref_last_sign_bias = 1;
115 av_assert0(pic->nb_refs == 2);
117 VAAPIEncodeVP9Picture *href0 = pic->refs[0]->priv_data,
118 *href1 = pic->refs[1]->priv_data;
119 av_assert0(href0->slot < pic->b_depth + 1 &&
120 href1->slot < pic->b_depth + 1);
122 if (pic->b_depth == ctx->max_b_depth) {
123 // Unreferenced frame.
124 vpic->refresh_frame_flags = 0x00;
127 vpic->refresh_frame_flags = 0xfe << pic->b_depth & 0xff;
128 hpic->slot = 1 + pic->b_depth;
130 vpic->ref_flags.bits.ref_frame_ctrl_l0 = 1;
131 vpic->ref_flags.bits.ref_frame_ctrl_l1 = 2;
132 vpic->ref_flags.bits.ref_last_idx = href0->slot;
133 vpic->ref_flags.bits.ref_last_sign_bias = 1;
134 vpic->ref_flags.bits.ref_gf_idx = href1->slot;
135 vpic->ref_flags.bits.ref_gf_sign_bias = 0;
139 av_assert0(0 && "invalid picture type");
141 if (vpic->refresh_frame_flags == 0x00) {
142 av_log(avctx, AV_LOG_DEBUG, "Pic %"PRId64" not stored.\n",
145 av_log(avctx, AV_LOG_DEBUG, "Pic %"PRId64" stored in slot %d.\n",
146 pic->display_order, hpic->slot);
149 for (i = 0; i < FF_ARRAY_ELEMS(vpic->reference_frames); i++)
150 vpic->reference_frames[i] = VA_INVALID_SURFACE;
152 for (i = 0; i < pic->nb_refs; i++) {
153 VAAPIEncodePicture *ref_pic = pic->refs[i];
155 slot = ((VAAPIEncodeVP9Picture*)ref_pic->priv_data)->slot;
156 av_assert0(vpic->reference_frames[slot] == VA_INVALID_SURFACE);
157 vpic->reference_frames[slot] = ref_pic->recon_surface;
160 vpic->pic_flags.bits.frame_type = (pic->type != PICTURE_TYPE_IDR);
161 vpic->pic_flags.bits.show_frame = pic->display_order <= pic->encode_order;
163 if (pic->type == PICTURE_TYPE_IDR)
164 vpic->luma_ac_qindex = priv->q_idx_idr;
165 else if (pic->type == PICTURE_TYPE_P)
166 vpic->luma_ac_qindex = priv->q_idx_p;
168 vpic->luma_ac_qindex = priv->q_idx_b;
169 vpic->luma_dc_qindex_delta = 0;
170 vpic->chroma_ac_qindex_delta = 0;
171 vpic->chroma_dc_qindex_delta = 0;
173 vpic->filter_level = priv->loop_filter_level;
174 vpic->sharpness_level = priv->loop_filter_sharpness;
179 static av_cold int vaapi_encode_vp9_configure(AVCodecContext *avctx)
181 VAAPIEncodeVP9Context *priv = avctx->priv_data;
183 priv->q_idx_p = av_clip(avctx->global_quality, 0, VP9_MAX_QUANT);
184 if (avctx->i_quant_factor > 0.0)
185 priv->q_idx_idr = av_clip((avctx->global_quality *
186 avctx->i_quant_factor +
187 avctx->i_quant_offset) + 0.5,
190 priv->q_idx_idr = priv->q_idx_p;
191 if (avctx->b_quant_factor > 0.0)
192 priv->q_idx_b = av_clip((avctx->global_quality *
193 avctx->b_quant_factor +
194 avctx->b_quant_offset) + 0.5,
197 priv->q_idx_b = priv->q_idx_p;
202 static const VAAPIEncodeProfile vaapi_encode_vp9_profiles[] = {
203 { FF_PROFILE_VP9_0, 8, 3, 1, 1, VAProfileVP9Profile0 },
204 { FF_PROFILE_VP9_2, 10, 3, 1, 1, VAProfileVP9Profile2 },
205 { FF_PROFILE_UNKNOWN }
208 static const VAAPIEncodeType vaapi_encode_type_vp9 = {
209 .profiles = vaapi_encode_vp9_profiles,
211 .flags = FLAG_B_PICTURES |
212 FLAG_B_PICTURE_REFERENCES,
214 .picture_priv_data_size = sizeof(VAAPIEncodeVP9Picture),
216 .configure = &vaapi_encode_vp9_configure,
218 .sequence_params_size = sizeof(VAEncSequenceParameterBufferVP9),
219 .init_sequence_params = &vaapi_encode_vp9_init_sequence_params,
221 .picture_params_size = sizeof(VAEncPictureParameterBufferVP9),
222 .init_picture_params = &vaapi_encode_vp9_init_picture_params,
225 static av_cold int vaapi_encode_vp9_init(AVCodecContext *avctx)
227 VAAPIEncodeContext *ctx = avctx->priv_data;
229 ctx->codec = &vaapi_encode_type_vp9;
231 // No packed headers are currently desired. They could be written,
232 // but there isn't any reason to do so - the one usable driver (i965)
233 // can write its own headers and there is no metadata to include.
234 ctx->desired_packed_headers = 0;
236 // Surfaces must be aligned to superblock boundaries.
237 ctx->surface_width = FFALIGN(avctx->width, 64);
238 ctx->surface_height = FFALIGN(avctx->height, 64);
240 return ff_vaapi_encode_init(avctx);
243 #define OFFSET(x) offsetof(VAAPIEncodeVP9Context, x)
244 #define FLAGS (AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM)
245 static const AVOption vaapi_encode_vp9_options[] = {
246 VAAPI_ENCODE_COMMON_OPTIONS,
247 { "loop_filter_level", "Loop filter level",
248 OFFSET(loop_filter_level), AV_OPT_TYPE_INT, { .i64 = 16 }, 0, 63, FLAGS },
249 { "loop_filter_sharpness", "Loop filter sharpness",
250 OFFSET(loop_filter_sharpness), AV_OPT_TYPE_INT, { .i64 = 4 }, 0, 15, FLAGS },
254 static const AVCodecDefault vaapi_encode_vp9_defaults[] = {
258 { "global_quality", "100" },
264 static const AVClass vaapi_encode_vp9_class = {
265 .class_name = "vp9_vaapi",
266 .item_name = av_default_item_name,
267 .option = vaapi_encode_vp9_options,
268 .version = LIBAVUTIL_VERSION_INT,
271 AVCodec ff_vp9_vaapi_encoder = {
273 .long_name = NULL_IF_CONFIG_SMALL("VP9 (VAAPI)"),
274 .type = AVMEDIA_TYPE_VIDEO,
275 .id = AV_CODEC_ID_VP9,
276 .priv_data_size = sizeof(VAAPIEncodeVP9Context),
277 .init = &vaapi_encode_vp9_init,
278 .send_frame = &ff_vaapi_encode_send_frame,
279 .receive_packet = &ff_vaapi_encode_receive_packet,
280 .close = &ff_vaapi_encode_close,
281 .priv_class = &vaapi_encode_vp9_class,
282 .capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_HARDWARE,
283 .defaults = vaapi_encode_vp9_defaults,
284 .pix_fmts = (const enum AVPixelFormat[]) {
288 .wrapper_name = "vaapi",