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_vp8.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"
34 typedef struct VAAPIEncodeVP8Context {
37 } VAAPIEncodeVP8Context;
39 typedef struct VAAPIEncodeVP8Options {
40 int loop_filter_level;
41 int loop_filter_sharpness;
42 } VAAPIEncodeVP8Options;
45 #define vseq_var(name) vseq->name, name
46 #define vseq_field(name) vseq->seq_fields.bits.name, name
47 #define vpic_var(name) vpic->name, name
48 #define vpic_field(name) vpic->pic_fields.bits.name, name
51 static int vaapi_encode_vp8_init_sequence_params(AVCodecContext *avctx)
53 VAAPIEncodeContext *ctx = avctx->priv_data;
54 VAEncSequenceParameterBufferVP8 *vseq = ctx->codec_sequence_params;
56 vseq->frame_width = avctx->width;
57 vseq->frame_height = avctx->height;
59 vseq->frame_width_scale = 0;
60 vseq->frame_height_scale = 0;
62 vseq->error_resilient = 0;
65 if (!(ctx->va_rc_mode & VA_RC_CQP)) {
66 vseq->bits_per_second = avctx->bit_rate;
67 vseq->intra_period = avctx->gop_size;
73 static int vaapi_encode_vp8_init_picture_params(AVCodecContext *avctx,
74 VAAPIEncodePicture *pic)
76 VAAPIEncodeContext *ctx = avctx->priv_data;
77 VAEncPictureParameterBufferVP8 *vpic = pic->codec_picture_params;
78 VAAPIEncodeVP8Options *opt = ctx->codec_options;
81 vpic->reconstructed_frame = pic->recon_surface;
83 vpic->coded_buf = pic->output_buffer;
86 case PICTURE_TYPE_IDR:
88 av_assert0(pic->nb_refs == 0);
89 vpic->ref_flags.bits.force_kf = 1;
90 vpic->ref_last_frame =
96 av_assert0(pic->nb_refs == 1);
97 vpic->ref_flags.bits.no_ref_last = 0;
98 vpic->ref_flags.bits.no_ref_gf = 1;
99 vpic->ref_flags.bits.no_ref_arf = 1;
100 vpic->ref_last_frame =
102 vpic->ref_arf_frame =
103 pic->refs[0]->recon_surface;
106 av_assert0(0 && "invalid picture type");
109 vpic->pic_flags.bits.frame_type = (pic->type != PICTURE_TYPE_IDR);
110 vpic->pic_flags.bits.show_frame = 1;
112 vpic->pic_flags.bits.refresh_last = 1;
113 vpic->pic_flags.bits.refresh_golden_frame = 1;
114 vpic->pic_flags.bits.refresh_alternate_frame = 1;
116 vpic->pic_flags.bits.version = 0;
117 vpic->pic_flags.bits.loop_filter_type = 0;
118 for (i = 0; i < 4; i++)
119 vpic->loop_filter_level[i] = opt->loop_filter_level;
120 vpic->sharpness_level = opt->loop_filter_sharpness;
122 vpic->clamp_qindex_low = 0;
123 vpic->clamp_qindex_high = 127;
128 static int vaapi_encode_vp8_write_quant_table(AVCodecContext *avctx,
129 VAAPIEncodePicture *pic,
130 int index, int *type,
131 char *data, size_t *data_len)
133 VAAPIEncodeContext *ctx = avctx->priv_data;
134 VAAPIEncodeVP8Context *priv = ctx->priv_data;
135 VAQMatrixBufferVP8 quant;
141 if (*data_len < sizeof(quant))
142 return AVERROR(EINVAL);
143 *type = VAQMatrixBufferType;
144 *data_len = sizeof(quant);
146 memset(&quant, 0, sizeof(quant));
148 if (pic->type == PICTURE_TYPE_P)
153 for (i = 0; i < 4; i++)
154 quant.quantization_index[i] = q;
155 for (i = 0; i < 5; i++)
156 quant.quantization_index_delta[i] = 0;
158 memcpy(data, &quant, sizeof(quant));
162 static av_cold int vaapi_encode_vp8_configure(AVCodecContext *avctx)
164 VAAPIEncodeContext *ctx = avctx->priv_data;
165 VAAPIEncodeVP8Context *priv = ctx->priv_data;
167 priv->q_index_p = av_clip(avctx->global_quality, 0, VP8_MAX_QUANT);
168 if (avctx->i_quant_factor > 0.0)
169 priv->q_index_i = av_clip((avctx->global_quality *
170 avctx->i_quant_factor +
171 avctx->i_quant_offset) + 0.5,
174 priv->q_index_i = priv->q_index_p;
179 static const VAAPIEncodeType vaapi_encode_type_vp8 = {
180 .configure = &vaapi_encode_vp8_configure,
182 .priv_data_size = sizeof(VAAPIEncodeVP8Context),
184 .sequence_params_size = sizeof(VAEncSequenceParameterBufferVP8),
185 .init_sequence_params = &vaapi_encode_vp8_init_sequence_params,
187 .picture_params_size = sizeof(VAEncPictureParameterBufferVP8),
188 .init_picture_params = &vaapi_encode_vp8_init_picture_params,
190 .write_extra_buffer = &vaapi_encode_vp8_write_quant_table,
193 static av_cold int vaapi_encode_vp8_init(AVCodecContext *avctx)
195 VAAPIEncodeContext *ctx = avctx->priv_data;
197 if (avctx->max_b_frames > 0) {
198 av_log(avctx, AV_LOG_ERROR, "B-frames are not supported.\n");
199 return AVERROR_PATCHWELCOME;
202 ctx->codec = &vaapi_encode_type_vp8;
204 ctx->va_profile = VAProfileVP8Version0_3;
205 ctx->va_entrypoint = VAEntrypointEncSlice;
206 ctx->va_rt_format = VA_RT_FORMAT_YUV420;
208 if (avctx->flags & AV_CODEC_FLAG_QSCALE) {
209 ctx->va_rc_mode = VA_RC_CQP;
210 } else if (avctx->bit_rate > 0) {
211 if (avctx->rc_max_rate == avctx->bit_rate)
212 ctx->va_rc_mode = VA_RC_CBR;
214 ctx->va_rc_mode = VA_RC_VBR;
216 ctx->va_rc_mode = VA_RC_CQP;
219 // Packed headers are not currently supported.
220 ctx->va_packed_headers = 0;
222 ctx->surface_width = FFALIGN(avctx->width, 16);
223 ctx->surface_height = FFALIGN(avctx->height, 16);
225 return ff_vaapi_encode_init(avctx);
228 #define OFFSET(x) (offsetof(VAAPIEncodeContext, codec_options_data) + \
229 offsetof(VAAPIEncodeVP8Options, x))
230 #define FLAGS (AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM)
231 static const AVOption vaapi_encode_vp8_options[] = {
232 { "loop_filter_level", "Loop filter level",
233 OFFSET(loop_filter_level), AV_OPT_TYPE_INT, { .i64 = 16 }, 0, 63, FLAGS },
234 { "loop_filter_sharpness", "Loop filter sharpness",
235 OFFSET(loop_filter_sharpness), AV_OPT_TYPE_INT, { .i64 = 4 }, 0, 15, FLAGS },
239 static const AVCodecDefault vaapi_encode_vp8_defaults[] = {
243 { "global_quality", "40" },
247 static const AVClass vaapi_encode_vp8_class = {
248 .class_name = "vp8_vaapi",
249 .item_name = av_default_item_name,
250 .option = vaapi_encode_vp8_options,
251 .version = LIBAVUTIL_VERSION_INT,
254 AVCodec ff_vp8_vaapi_encoder = {
256 .long_name = NULL_IF_CONFIG_SMALL("VP8 (VAAPI)"),
257 .type = AVMEDIA_TYPE_VIDEO,
258 .id = AV_CODEC_ID_VP8,
259 .priv_data_size = (sizeof(VAAPIEncodeContext) +
260 sizeof(VAAPIEncodeVP8Options)),
261 .init = &vaapi_encode_vp8_init,
262 .encode2 = &ff_vaapi_encode2,
263 .close = &ff_vaapi_encode_close,
264 .priv_class = &vaapi_encode_vp8_class,
265 .capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_HARDWARE,
266 .defaults = vaapi_encode_vp8_defaults,
267 .pix_fmts = (const enum AVPixelFormat[]) {
271 .wrapper_name = "vaapi",