2016-11-29 22:38:29 +02:00
|
|
|
/*
|
|
|
|
* This file is part of FFmpeg.
|
|
|
|
*
|
|
|
|
* FFmpeg is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* FFmpeg is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with FFmpeg; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <va/va.h>
|
|
|
|
#include <va/va_enc_vp8.h>
|
|
|
|
|
|
|
|
#include "libavutil/avassert.h"
|
|
|
|
#include "libavutil/common.h"
|
|
|
|
#include "libavutil/internal.h"
|
|
|
|
#include "libavutil/opt.h"
|
|
|
|
#include "libavutil/pixfmt.h"
|
|
|
|
|
|
|
|
#include "avcodec.h"
|
|
|
|
#include "internal.h"
|
|
|
|
#include "vaapi_encode.h"
|
2017-02-16 22:40:55 +02:00
|
|
|
#include "vp8.h"
|
2016-11-29 22:38:29 +02:00
|
|
|
|
|
|
|
|
|
|
|
typedef struct VAAPIEncodeVP8Context {
|
2018-08-23 01:44:38 +02:00
|
|
|
VAAPIEncodeContext common;
|
2016-11-29 22:38:29 +02:00
|
|
|
|
2018-08-23 01:44:38 +02:00
|
|
|
// User options.
|
2016-11-29 22:38:29 +02:00
|
|
|
int loop_filter_level;
|
|
|
|
int loop_filter_sharpness;
|
2018-08-23 01:44:38 +02:00
|
|
|
|
|
|
|
// Derived settings.
|
|
|
|
int q_index_i;
|
|
|
|
int q_index_p;
|
|
|
|
} VAAPIEncodeVP8Context;
|
2016-11-29 22:38:29 +02:00
|
|
|
|
|
|
|
|
|
|
|
#define vseq_var(name) vseq->name, name
|
|
|
|
#define vseq_field(name) vseq->seq_fields.bits.name, name
|
|
|
|
#define vpic_var(name) vpic->name, name
|
|
|
|
#define vpic_field(name) vpic->pic_fields.bits.name, name
|
|
|
|
|
|
|
|
|
|
|
|
static int vaapi_encode_vp8_init_sequence_params(AVCodecContext *avctx)
|
|
|
|
{
|
|
|
|
VAAPIEncodeContext *ctx = avctx->priv_data;
|
|
|
|
VAEncSequenceParameterBufferVP8 *vseq = ctx->codec_sequence_params;
|
|
|
|
|
|
|
|
vseq->frame_width = avctx->width;
|
|
|
|
vseq->frame_height = avctx->height;
|
|
|
|
|
|
|
|
vseq->frame_width_scale = 0;
|
|
|
|
vseq->frame_height_scale = 0;
|
|
|
|
|
|
|
|
vseq->error_resilient = 0;
|
|
|
|
vseq->kf_auto = 0;
|
|
|
|
|
|
|
|
if (!(ctx->va_rc_mode & VA_RC_CQP)) {
|
2018-09-19 00:30:45 +02:00
|
|
|
vseq->bits_per_second = ctx->va_bit_rate;
|
2018-09-19 00:30:47 +02:00
|
|
|
vseq->intra_period = ctx->gop_size;
|
2016-11-29 22:38:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int vaapi_encode_vp8_init_picture_params(AVCodecContext *avctx,
|
|
|
|
VAAPIEncodePicture *pic)
|
|
|
|
{
|
2018-08-23 01:44:38 +02:00
|
|
|
VAAPIEncodeVP8Context *priv = avctx->priv_data;
|
2016-11-29 22:38:29 +02:00
|
|
|
VAEncPictureParameterBufferVP8 *vpic = pic->codec_picture_params;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
vpic->reconstructed_frame = pic->recon_surface;
|
|
|
|
|
|
|
|
vpic->coded_buf = pic->output_buffer;
|
|
|
|
|
|
|
|
switch (pic->type) {
|
|
|
|
case PICTURE_TYPE_IDR:
|
|
|
|
case PICTURE_TYPE_I:
|
|
|
|
av_assert0(pic->nb_refs == 0);
|
|
|
|
vpic->ref_flags.bits.force_kf = 1;
|
|
|
|
vpic->ref_last_frame =
|
|
|
|
vpic->ref_gf_frame =
|
|
|
|
vpic->ref_arf_frame =
|
|
|
|
VA_INVALID_SURFACE;
|
|
|
|
break;
|
|
|
|
case PICTURE_TYPE_P:
|
|
|
|
av_assert0(pic->nb_refs == 1);
|
|
|
|
vpic->ref_flags.bits.no_ref_last = 0;
|
|
|
|
vpic->ref_flags.bits.no_ref_gf = 1;
|
|
|
|
vpic->ref_flags.bits.no_ref_arf = 1;
|
|
|
|
vpic->ref_last_frame =
|
|
|
|
vpic->ref_gf_frame =
|
|
|
|
vpic->ref_arf_frame =
|
|
|
|
pic->refs[0]->recon_surface;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
av_assert0(0 && "invalid picture type");
|
|
|
|
}
|
|
|
|
|
|
|
|
vpic->pic_flags.bits.frame_type = (pic->type != PICTURE_TYPE_IDR);
|
|
|
|
vpic->pic_flags.bits.show_frame = 1;
|
|
|
|
|
|
|
|
vpic->pic_flags.bits.refresh_last = 1;
|
|
|
|
vpic->pic_flags.bits.refresh_golden_frame = 1;
|
|
|
|
vpic->pic_flags.bits.refresh_alternate_frame = 1;
|
|
|
|
|
|
|
|
vpic->pic_flags.bits.version = 0;
|
|
|
|
vpic->pic_flags.bits.loop_filter_type = 0;
|
|
|
|
for (i = 0; i < 4; i++)
|
2018-08-23 01:44:38 +02:00
|
|
|
vpic->loop_filter_level[i] = priv->loop_filter_level;
|
|
|
|
vpic->sharpness_level = priv->loop_filter_sharpness;
|
2016-11-29 22:38:29 +02:00
|
|
|
|
|
|
|
vpic->clamp_qindex_low = 0;
|
|
|
|
vpic->clamp_qindex_high = 127;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int vaapi_encode_vp8_write_quant_table(AVCodecContext *avctx,
|
|
|
|
VAAPIEncodePicture *pic,
|
|
|
|
int index, int *type,
|
|
|
|
char *data, size_t *data_len)
|
|
|
|
{
|
2018-08-23 01:44:38 +02:00
|
|
|
VAAPIEncodeVP8Context *priv = avctx->priv_data;
|
2016-11-29 22:38:29 +02:00
|
|
|
VAQMatrixBufferVP8 quant;
|
|
|
|
int i, q;
|
|
|
|
|
|
|
|
if (index > 0)
|
|
|
|
return AVERROR_EOF;
|
|
|
|
|
|
|
|
if (*data_len < sizeof(quant))
|
|
|
|
return AVERROR(EINVAL);
|
|
|
|
*type = VAQMatrixBufferType;
|
|
|
|
*data_len = sizeof(quant);
|
|
|
|
|
2018-05-04 16:41:07 +02:00
|
|
|
memset(&quant, 0, sizeof(quant));
|
|
|
|
|
2016-11-29 22:38:29 +02:00
|
|
|
if (pic->type == PICTURE_TYPE_P)
|
|
|
|
q = priv->q_index_p;
|
|
|
|
else
|
|
|
|
q = priv->q_index_i;
|
|
|
|
|
|
|
|
for (i = 0; i < 4; i++)
|
|
|
|
quant.quantization_index[i] = q;
|
|
|
|
for (i = 0; i < 5; i++)
|
|
|
|
quant.quantization_index_delta[i] = 0;
|
|
|
|
|
|
|
|
memcpy(data, &quant, sizeof(quant));
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static av_cold int vaapi_encode_vp8_configure(AVCodecContext *avctx)
|
|
|
|
{
|
2019-02-10 21:46:58 +02:00
|
|
|
VAAPIEncodeContext *ctx = avctx->priv_data;
|
2018-08-23 01:44:38 +02:00
|
|
|
VAAPIEncodeVP8Context *priv = avctx->priv_data;
|
2016-11-29 22:38:29 +02:00
|
|
|
|
2019-02-10 21:46:58 +02:00
|
|
|
priv->q_index_p = av_clip(ctx->rc_quality, 0, VP8_MAX_QUANT);
|
2016-11-29 22:38:29 +02:00
|
|
|
if (avctx->i_quant_factor > 0.0)
|
2019-02-10 21:46:58 +02:00
|
|
|
priv->q_index_i =
|
|
|
|
av_clip((avctx->i_quant_factor * priv->q_index_p +
|
|
|
|
avctx->i_quant_offset) + 0.5,
|
|
|
|
0, VP8_MAX_QUANT);
|
2016-11-29 22:38:29 +02:00
|
|
|
else
|
|
|
|
priv->q_index_i = priv->q_index_p;
|
|
|
|
|
2019-07-07 21:26:49 +02:00
|
|
|
ctx->roi_quant_range = VP8_MAX_QUANT;
|
|
|
|
|
2016-11-29 22:38:29 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-09-19 00:30:40 +02:00
|
|
|
static const VAAPIEncodeProfile vaapi_encode_vp8_profiles[] = {
|
|
|
|
{ 0 /* VP8 has no profiles */, 8, 3, 1, 1, VAProfileVP8Version0_3 },
|
|
|
|
{ FF_PROFILE_UNKNOWN }
|
|
|
|
};
|
|
|
|
|
2016-11-29 22:38:29 +02:00
|
|
|
static const VAAPIEncodeType vaapi_encode_type_vp8 = {
|
2018-09-19 00:30:40 +02:00
|
|
|
.profiles = vaapi_encode_vp8_profiles,
|
|
|
|
|
2016-11-29 22:38:29 +02:00
|
|
|
.configure = &vaapi_encode_vp8_configure,
|
|
|
|
|
2019-02-10 21:46:58 +02:00
|
|
|
.default_quality = 40,
|
|
|
|
|
2016-11-29 22:38:29 +02:00
|
|
|
.sequence_params_size = sizeof(VAEncSequenceParameterBufferVP8),
|
|
|
|
.init_sequence_params = &vaapi_encode_vp8_init_sequence_params,
|
|
|
|
|
|
|
|
.picture_params_size = sizeof(VAEncPictureParameterBufferVP8),
|
|
|
|
.init_picture_params = &vaapi_encode_vp8_init_picture_params,
|
|
|
|
|
|
|
|
.write_extra_buffer = &vaapi_encode_vp8_write_quant_table,
|
|
|
|
};
|
|
|
|
|
|
|
|
static av_cold int vaapi_encode_vp8_init(AVCodecContext *avctx)
|
|
|
|
{
|
|
|
|
VAAPIEncodeContext *ctx = avctx->priv_data;
|
|
|
|
|
|
|
|
ctx->codec = &vaapi_encode_type_vp8;
|
|
|
|
|
2018-09-19 00:30:48 +02:00
|
|
|
// No packed headers are currently desired. VP8 has no metadata
|
|
|
|
// which would be useful to write, and no existing driver supports
|
|
|
|
// adding them anyway.
|
|
|
|
ctx->desired_packed_headers = 0;
|
2016-11-29 22:38:29 +02:00
|
|
|
|
|
|
|
ctx->surface_width = FFALIGN(avctx->width, 16);
|
|
|
|
ctx->surface_height = FFALIGN(avctx->height, 16);
|
|
|
|
|
|
|
|
return ff_vaapi_encode_init(avctx);
|
|
|
|
}
|
|
|
|
|
2018-08-23 01:44:38 +02:00
|
|
|
#define OFFSET(x) offsetof(VAAPIEncodeVP8Context, x)
|
2016-11-29 22:38:29 +02:00
|
|
|
#define FLAGS (AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM)
|
|
|
|
static const AVOption vaapi_encode_vp8_options[] = {
|
2018-09-19 00:30:41 +02:00
|
|
|
VAAPI_ENCODE_COMMON_OPTIONS,
|
2019-02-10 21:46:58 +02:00
|
|
|
VAAPI_ENCODE_RC_OPTIONS,
|
|
|
|
|
2016-11-29 22:38:29 +02:00
|
|
|
{ "loop_filter_level", "Loop filter level",
|
|
|
|
OFFSET(loop_filter_level), AV_OPT_TYPE_INT, { .i64 = 16 }, 0, 63, FLAGS },
|
|
|
|
{ "loop_filter_sharpness", "Loop filter sharpness",
|
|
|
|
OFFSET(loop_filter_sharpness), AV_OPT_TYPE_INT, { .i64 = 4 }, 0, 15, FLAGS },
|
|
|
|
{ NULL },
|
|
|
|
};
|
|
|
|
|
|
|
|
static const AVCodecDefault vaapi_encode_vp8_defaults[] = {
|
|
|
|
{ "b", "0" },
|
|
|
|
{ "bf", "0" },
|
|
|
|
{ "g", "120" },
|
2018-09-19 00:30:46 +02:00
|
|
|
{ "qmin", "-1" },
|
|
|
|
{ "qmax", "-1" },
|
2016-11-29 22:38:29 +02:00
|
|
|
{ NULL },
|
|
|
|
};
|
|
|
|
|
|
|
|
static const AVClass vaapi_encode_vp8_class = {
|
|
|
|
.class_name = "vp8_vaapi",
|
|
|
|
.item_name = av_default_item_name,
|
|
|
|
.option = vaapi_encode_vp8_options,
|
|
|
|
.version = LIBAVUTIL_VERSION_INT,
|
|
|
|
};
|
|
|
|
|
|
|
|
AVCodec ff_vp8_vaapi_encoder = {
|
|
|
|
.name = "vp8_vaapi",
|
|
|
|
.long_name = NULL_IF_CONFIG_SMALL("VP8 (VAAPI)"),
|
|
|
|
.type = AVMEDIA_TYPE_VIDEO,
|
|
|
|
.id = AV_CODEC_ID_VP8,
|
2018-08-23 01:44:38 +02:00
|
|
|
.priv_data_size = sizeof(VAAPIEncodeVP8Context),
|
2016-11-29 22:38:29 +02:00
|
|
|
.init = &vaapi_encode_vp8_init,
|
2018-12-20 22:39:56 +02:00
|
|
|
.send_frame = &ff_vaapi_encode_send_frame,
|
|
|
|
.receive_packet = &ff_vaapi_encode_receive_packet,
|
2016-11-29 22:38:29 +02:00
|
|
|
.close = &ff_vaapi_encode_close,
|
|
|
|
.priv_class = &vaapi_encode_vp8_class,
|
2017-12-11 17:18:44 +02:00
|
|
|
.capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_HARDWARE,
|
2020-03-31 17:34:00 +02:00
|
|
|
.caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
|
2016-11-29 22:38:29 +02:00
|
|
|
.defaults = vaapi_encode_vp8_defaults,
|
|
|
|
.pix_fmts = (const enum AVPixelFormat[]) {
|
|
|
|
AV_PIX_FMT_VAAPI,
|
|
|
|
AV_PIX_FMT_NONE,
|
|
|
|
},
|
2020-04-13 17:33:17 +02:00
|
|
|
.hw_configs = ff_vaapi_encode_hw_configs,
|
2017-12-11 17:18:44 +02:00
|
|
|
.wrapper_name = "vaapi",
|
2016-11-29 22:38:29 +02:00
|
|
|
};
|