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 {
35 VAAPIEncodeContext common;
38 int loop_filter_level;
39 int loop_filter_sharpness;
44 } VAAPIEncodeVP8Context;
47 #define vseq_var(name) vseq->name, name
48 #define vseq_field(name) vseq->seq_fields.bits.name, name
49 #define vpic_var(name) vpic->name, name
50 #define vpic_field(name) vpic->pic_fields.bits.name, name
53 static int vaapi_encode_vp8_init_sequence_params(AVCodecContext *avctx)
55 VAAPIEncodeContext *ctx = avctx->priv_data;
56 VAEncSequenceParameterBufferVP8 *vseq = ctx->codec_sequence_params;
58 vseq->frame_width = avctx->width;
59 vseq->frame_height = avctx->height;
61 vseq->frame_width_scale = 0;
62 vseq->frame_height_scale = 0;
64 vseq->error_resilient = 0;
67 if (!(ctx->va_rc_mode & VA_RC_CQP)) {
68 vseq->bits_per_second = ctx->va_bit_rate;
69 vseq->intra_period = ctx->gop_size;
75 static int vaapi_encode_vp8_init_picture_params(AVCodecContext *avctx,
76 VAAPIEncodePicture *pic)
78 VAAPIEncodeVP8Context *priv = avctx->priv_data;
79 VAEncPictureParameterBufferVP8 *vpic = pic->codec_picture_params;
82 vpic->reconstructed_frame = pic->recon_surface;
84 vpic->coded_buf = pic->output_buffer;
87 case PICTURE_TYPE_IDR:
89 av_assert0(pic->nb_refs == 0);
90 vpic->ref_flags.bits.force_kf = 1;
91 vpic->ref_last_frame =
97 av_assert0(pic->nb_refs == 1);
98 vpic->ref_flags.bits.no_ref_last = 0;
99 vpic->ref_flags.bits.no_ref_gf = 1;
100 vpic->ref_flags.bits.no_ref_arf = 1;
101 vpic->ref_last_frame =
103 vpic->ref_arf_frame =
104 pic->refs[0]->recon_surface;
107 av_assert0(0 && "invalid picture type");
110 vpic->pic_flags.bits.frame_type = (pic->type != PICTURE_TYPE_IDR);
111 vpic->pic_flags.bits.show_frame = 1;
113 vpic->pic_flags.bits.refresh_last = 1;
114 vpic->pic_flags.bits.refresh_golden_frame = 1;
115 vpic->pic_flags.bits.refresh_alternate_frame = 1;
117 vpic->pic_flags.bits.version = 0;
118 vpic->pic_flags.bits.loop_filter_type = 0;
119 for (i = 0; i < 4; i++)
120 vpic->loop_filter_level[i] = priv->loop_filter_level;
121 vpic->sharpness_level = priv->loop_filter_sharpness;
123 vpic->clamp_qindex_low = 0;
124 vpic->clamp_qindex_high = 127;
129 static int vaapi_encode_vp8_write_quant_table(AVCodecContext *avctx,
130 VAAPIEncodePicture *pic,
131 int index, int *type,
132 char *data, size_t *data_len)
134 VAAPIEncodeVP8Context *priv = avctx->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 = avctx->priv_data;
167 priv->q_index_p = av_clip(ctx->rc_quality, 0, VP8_MAX_QUANT);
168 if (avctx->i_quant_factor > 0.0)
170 av_clip((avctx->i_quant_factor * priv->q_index_p +
171 avctx->i_quant_offset) + 0.5,
174 priv->q_index_i = priv->q_index_p;
176 ctx->roi_quant_range = VP8_MAX_QUANT;
181 static const VAAPIEncodeProfile vaapi_encode_vp8_profiles[] = {
182 { 0 /* VP8 has no profiles */, 8, 3, 1, 1, VAProfileVP8Version0_3 },
183 { FF_PROFILE_UNKNOWN }
186 static const VAAPIEncodeType vaapi_encode_type_vp8 = {
187 .profiles = vaapi_encode_vp8_profiles,
189 .configure = &vaapi_encode_vp8_configure,
191 .default_quality = 40,
193 .sequence_params_size = sizeof(VAEncSequenceParameterBufferVP8),
194 .init_sequence_params = &vaapi_encode_vp8_init_sequence_params,
196 .picture_params_size = sizeof(VAEncPictureParameterBufferVP8),
197 .init_picture_params = &vaapi_encode_vp8_init_picture_params,
199 .write_extra_buffer = &vaapi_encode_vp8_write_quant_table,
202 static av_cold int vaapi_encode_vp8_init(AVCodecContext *avctx)
204 VAAPIEncodeContext *ctx = avctx->priv_data;
206 ctx->codec = &vaapi_encode_type_vp8;
208 // No packed headers are currently desired. VP8 has no metadata
209 // which would be useful to write, and no existing driver supports
210 // adding them anyway.
211 ctx->desired_packed_headers = 0;
213 ctx->surface_width = FFALIGN(avctx->width, 16);
214 ctx->surface_height = FFALIGN(avctx->height, 16);
216 return ff_vaapi_encode_init(avctx);
219 #define OFFSET(x) offsetof(VAAPIEncodeVP8Context, x)
220 #define FLAGS (AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM)
221 static const AVOption vaapi_encode_vp8_options[] = {
222 VAAPI_ENCODE_COMMON_OPTIONS,
223 VAAPI_ENCODE_RC_OPTIONS,
225 { "loop_filter_level", "Loop filter level",
226 OFFSET(loop_filter_level), AV_OPT_TYPE_INT, { .i64 = 16 }, 0, 63, FLAGS },
227 { "loop_filter_sharpness", "Loop filter sharpness",
228 OFFSET(loop_filter_sharpness), AV_OPT_TYPE_INT, { .i64 = 4 }, 0, 15, FLAGS },
232 static const AVCodecDefault vaapi_encode_vp8_defaults[] = {
241 static const AVClass vaapi_encode_vp8_class = {
242 .class_name = "vp8_vaapi",
243 .item_name = av_default_item_name,
244 .option = vaapi_encode_vp8_options,
245 .version = LIBAVUTIL_VERSION_INT,
248 const AVCodec ff_vp8_vaapi_encoder = {
250 .long_name = NULL_IF_CONFIG_SMALL("VP8 (VAAPI)"),
251 .type = AVMEDIA_TYPE_VIDEO,
252 .id = AV_CODEC_ID_VP8,
253 .priv_data_size = sizeof(VAAPIEncodeVP8Context),
254 .init = &vaapi_encode_vp8_init,
255 .receive_packet = &ff_vaapi_encode_receive_packet,
256 .close = &ff_vaapi_encode_close,
257 .priv_class = &vaapi_encode_vp8_class,
258 .capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_HARDWARE |
260 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
261 .defaults = vaapi_encode_vp8_defaults,
262 .pix_fmts = (const enum AVPixelFormat[]) {
266 .hw_configs = ff_vaapi_encode_hw_configs,
267 .wrapper_name = "vaapi",