FFmpeg
vaapi_encode_vp8.c
Go to the documentation of this file.
1 /*
2  * This file is part of FFmpeg.
3  *
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.
8  *
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.
13  *
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
17  */
18 
19 #include <va/va.h>
20 #include <va/va_enc_vp8.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 "codec_internal.h"
30 #include "vaapi_encode.h"
31 #include "vp8.h"
32 
33 
34 typedef struct VAAPIEncodeVP8Context {
36 
37  // User options.
40 
41  // Derived settings.
42  int q_index_i;
43  int q_index_p;
45 
46 
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
51 
52 
54 {
55  FFHWBaseEncodeContext *base_ctx = avctx->priv_data;
57  VAEncSequenceParameterBufferVP8 *vseq = ctx->codec_sequence_params;
58 
59  vseq->frame_width = avctx->width;
60  vseq->frame_height = avctx->height;
61 
62  vseq->frame_width_scale = 0;
63  vseq->frame_height_scale = 0;
64 
65  vseq->error_resilient = 0;
66  vseq->kf_auto = 0;
67 
68  if (!(ctx->va_rc_mode & VA_RC_CQP)) {
69  vseq->bits_per_second = ctx->va_bit_rate;
70  vseq->intra_period = base_ctx->gop_size;
71  }
72 
73  return 0;
74 }
75 
78 {
79  VAAPIEncodePicture *vaapi_pic = pic->priv;
80  VAAPIEncodeVP8Context *priv = avctx->priv_data;
81  VAEncPictureParameterBufferVP8 *vpic = vaapi_pic->codec_picture_params;
82  int i;
83 
84  vpic->reconstructed_frame = vaapi_pic->recon_surface;
85 
86  vpic->coded_buf = vaapi_pic->output_buffer;
87 
88  switch (pic->type) {
91  av_assert0(pic->nb_refs[0] == 0 && pic->nb_refs[1] == 0);
92  vpic->ref_flags.bits.force_kf = 1;
93  vpic->ref_last_frame =
94  vpic->ref_gf_frame =
95  vpic->ref_arf_frame =
96  VA_INVALID_SURFACE;
97  break;
99  av_assert0(!pic->nb_refs[1]);
100  vpic->ref_flags.bits.no_ref_last = 0;
101  vpic->ref_flags.bits.no_ref_gf = 1;
102  vpic->ref_flags.bits.no_ref_arf = 1;
103  vpic->ref_last_frame =
104  vpic->ref_gf_frame =
105  vpic->ref_arf_frame =
106  ((VAAPIEncodePicture *)pic->refs[0][0]->priv)->recon_surface;
107  break;
108  default:
109  av_assert0(0 && "invalid picture type");
110  }
111 
112  vpic->pic_flags.bits.frame_type = (pic->type != FF_HW_PICTURE_TYPE_IDR);
113  vpic->pic_flags.bits.show_frame = 1;
114 
115  vpic->pic_flags.bits.refresh_last = 1;
116  vpic->pic_flags.bits.refresh_golden_frame = 1;
117  vpic->pic_flags.bits.refresh_alternate_frame = 1;
118 
119  vpic->pic_flags.bits.version = 0;
120  vpic->pic_flags.bits.loop_filter_type = 0;
121  for (i = 0; i < 4; i++)
122  vpic->loop_filter_level[i] = priv->loop_filter_level;
123  vpic->sharpness_level = priv->loop_filter_sharpness;
124 
125  vpic->clamp_qindex_low = 0;
126  vpic->clamp_qindex_high = 127;
127 
128  return 0;
129 }
130 
132  FFHWBaseEncodePicture *base_pic,
133  int index, int *type,
134  char *data, size_t *data_len)
135 {
136  VAAPIEncodeVP8Context *priv = avctx->priv_data;
137  VAQMatrixBufferVP8 quant;
138  int i, q;
139 
140  if (index > 0)
141  return AVERROR_EOF;
142 
143  if (*data_len < sizeof(quant))
144  return AVERROR(EINVAL);
145  *type = VAQMatrixBufferType;
146  *data_len = sizeof(quant);
147 
148  memset(&quant, 0, sizeof(quant));
149 
150  if (base_pic->type == FF_HW_PICTURE_TYPE_P)
151  q = priv->q_index_p;
152  else
153  q = priv->q_index_i;
154 
155  for (i = 0; i < 4; i++)
156  quant.quantization_index[i] = q;
157  for (i = 0; i < 5; i++)
158  quant.quantization_index_delta[i] = 0;
159 
160  memcpy(data, &quant, sizeof(quant));
161  return 0;
162 }
163 
165 {
166  VAAPIEncodeContext *ctx = avctx->priv_data;
167  VAAPIEncodeVP8Context *priv = avctx->priv_data;
168 
169  priv->q_index_p = av_clip(ctx->rc_quality, 0, VP8_MAX_QUANT);
170  if (avctx->i_quant_factor > 0.0)
171  priv->q_index_i =
172  av_clip((avctx->i_quant_factor * priv->q_index_p +
173  avctx->i_quant_offset) + 0.5,
174  0, VP8_MAX_QUANT);
175  else
176  priv->q_index_i = priv->q_index_p;
177 
178  ctx->roi_quant_range = VP8_MAX_QUANT;
179 
180  return 0;
181 }
182 
184  { 0 /* VP8 has no profiles */, 8, 3, 1, 1, VAProfileVP8Version0_3 },
186 };
187 
190 
191  .configure = &vaapi_encode_vp8_configure,
192 
193  .default_quality = 40,
194 
195  .sequence_params_size = sizeof(VAEncSequenceParameterBufferVP8),
196  .init_sequence_params = &vaapi_encode_vp8_init_sequence_params,
197 
198  .picture_params_size = sizeof(VAEncPictureParameterBufferVP8),
199  .init_picture_params = &vaapi_encode_vp8_init_picture_params,
200 
201  .write_extra_buffer = &vaapi_encode_vp8_write_quant_table,
202 };
203 
205 {
206  VAAPIEncodeContext *ctx = avctx->priv_data;
207 
208  ctx->codec = &vaapi_encode_type_vp8;
209 
210  // No packed headers are currently desired. VP8 has no metadata
211  // which would be useful to write, and no existing driver supports
212  // adding them anyway.
213  ctx->desired_packed_headers = 0;
214 
215  return ff_vaapi_encode_init(avctx);
216 }
217 
218 #define OFFSET(x) offsetof(VAAPIEncodeVP8Context, x)
219 #define FLAGS (AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM)
224 
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 },
229  { NULL },
230 };
231 
233  { "b", "0" },
234  { "bf", "0" },
235  { "g", "120" },
236  { "qmin", "-1" },
237  { "qmax", "-1" },
238  { NULL },
239 };
240 
242  .class_name = "vp8_vaapi",
243  .item_name = av_default_item_name,
244  .option = vaapi_encode_vp8_options,
245  .version = LIBAVUTIL_VERSION_INT,
246 };
247 
249  .p.name = "vp8_vaapi",
250  CODEC_LONG_NAME("VP8 (VAAPI)"),
251  .p.type = AVMEDIA_TYPE_VIDEO,
252  .p.id = AV_CODEC_ID_VP8,
253  .priv_data_size = sizeof(VAAPIEncodeVP8Context),
256  .close = &ff_vaapi_encode_close,
257  .p.priv_class = &vaapi_encode_vp8_class,
258  .p.capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_HARDWARE |
260  .caps_internal = FF_CODEC_CAP_NOT_INIT_THREADSAFE |
262  .defaults = vaapi_encode_vp8_defaults,
263  .p.pix_fmts = (const enum AVPixelFormat[]) {
266  },
267  .color_ranges = AVCOL_RANGE_MPEG, /* FIXME: implement tagging */
268  .hw_configs = ff_vaapi_encode_hw_configs,
269  .p.wrapper_name = "vaapi",
270 };
VAAPIEncodeVP8Context::loop_filter_level
int loop_filter_level
Definition: vaapi_encode_vp8.c:38
AVPixelFormat
AVPixelFormat
Pixel format.
Definition: pixfmt.h:71
av_clip
#define av_clip
Definition: common.h:100
FF_HW_PICTURE_TYPE_IDR
@ FF_HW_PICTURE_TYPE_IDR
Definition: hw_base_encode.h:39
FF_CODEC_CAP_INIT_CLEANUP
#define FF_CODEC_CAP_INIT_CLEANUP
The codec allows calling the close function for deallocation even if the init function returned a fai...
Definition: codec_internal.h:43
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
opt.h
FFHWBaseEncodePicture::priv
void * priv
Definition: hw_base_encode.h:63
AVERROR_EOF
#define AVERROR_EOF
End of file.
Definition: error.h:57
AV_CODEC_CAP_HARDWARE
#define AV_CODEC_CAP_HARDWARE
Codec is backed by a hardware implementation.
Definition: codec.h:145
VAAPIEncodeVP8Context::common
VAAPIEncodeContext common
Definition: vaapi_encode_vp8.c:35
AVOption
AVOption.
Definition: opt.h:429
data
const char data[16]
Definition: mxf.c:149
FF_CODEC_CAP_NOT_INIT_THREADSAFE
#define FF_CODEC_CAP_NOT_INIT_THREADSAFE
The codec is not known to be init-threadsafe (i.e.
Definition: codec_internal.h:35
FFCodec
Definition: codec_internal.h:127
VAAPIEncodeVP8Context::q_index_p
int q_index_p
Definition: vaapi_encode_vp8.c:43
ff_vaapi_encode_close
av_cold int ff_vaapi_encode_close(AVCodecContext *avctx)
Definition: vaapi_encode.c:2275
FFHWBaseEncodeContext
Definition: hw_base_encode.h:122
AVCodecContext::i_quant_factor
float i_quant_factor
qscale factor between P- and I-frames If > 0 then the last P-frame quantizer will be used (q = lastp_...
Definition: avcodec.h:826
FFCodecDefault
Definition: codec_internal.h:97
FFCodec::p
AVCodec p
The public AVCodec.
Definition: codec_internal.h:131
FFHWBaseEncodePicture::type
int type
Definition: hw_base_encode.h:78
vaapi_encode.h
vaapi_encode_vp8_init
static av_cold int vaapi_encode_vp8_init(AVCodecContext *avctx)
Definition: vaapi_encode_vp8.c:204
VAAPIEncodePicture
Definition: vaapi_encode.h:65
VAAPIEncodeVP8Context::loop_filter_sharpness
int loop_filter_sharpness
Definition: vaapi_encode_vp8.c:39
type
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf type
Definition: writing_filters.txt:86
quant
static const uint8_t quant[64]
Definition: vmixdec.c:71
avassert.h
vaapi_encode_type_vp8
static const VAAPIEncodeType vaapi_encode_type_vp8
Definition: vaapi_encode_vp8.c:188
av_cold
#define av_cold
Definition: attributes.h:90
AV_PROFILE_UNKNOWN
#define AV_PROFILE_UNKNOWN
Definition: defs.h:65
VAAPIEncodePicture::codec_picture_params
void * codec_picture_params
Definition: vaapi_encode.h:83
vaapi_encode_vp8_profiles
static const VAAPIEncodeProfile vaapi_encode_vp8_profiles[]
Definition: vaapi_encode_vp8.c:183
VP8_MAX_QUANT
#define VP8_MAX_QUANT
Definition: vp8.h:41
AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE
#define AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE
This encoder can reorder user opaque values from input AVFrames and return them with corresponding ou...
Definition: codec.h:159
av_assert0
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:40
vaapi_encode_vp8_init_sequence_params
static int vaapi_encode_vp8_init_sequence_params(AVCodecContext *avctx)
Definition: vaapi_encode_vp8.c:53
ctx
AVFormatContext * ctx
Definition: movenc.c:49
ff_vaapi_encode_receive_packet
int ff_vaapi_encode_receive_packet(AVCodecContext *avctx, AVPacket *pkt)
Definition: vaapi_encode.c:2086
VAAPIEncodeType
Definition: vaapi_encode.h:265
CODEC_LONG_NAME
#define CODEC_LONG_NAME(str)
Definition: codec_internal.h:296
VAAPIEncodeContext
Definition: vaapi_encode.h:145
LIBAVUTIL_VERSION_INT
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:75
NULL
#define NULL
Definition: coverity.c:32
ff_vp8_vaapi_encoder
const FFCodec ff_vp8_vaapi_encoder
Definition: vaapi_encode_vp8.c:248
VAAPIEncodeType::profiles
const VAAPIEncodeProfile * profiles
Definition: vaapi_encode.h:268
FF_CODEC_RECEIVE_PACKET_CB
#define FF_CODEC_RECEIVE_PACKET_CB(func)
Definition: codec_internal.h:326
av_default_item_name
const char * av_default_item_name(void *ptr)
Return the context name.
Definition: log.c:237
vaapi_encode_vp8_configure
static av_cold int vaapi_encode_vp8_configure(AVCodecContext *avctx)
Definition: vaapi_encode_vp8.c:164
index
int index
Definition: gxfenc.c:90
vp8.h
vaapi_encode_vp8_options
static const AVOption vaapi_encode_vp8_options[]
Definition: vaapi_encode_vp8.c:220
init
int(* init)(AVBSFContext *ctx)
Definition: dts2pts.c:368
AV_CODEC_CAP_DR1
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
Definition: codec.h:52
codec_internal.h
VAAPI_ENCODE_RC_OPTIONS
#define VAAPI_ENCODE_RC_OPTIONS
Definition: vaapi_encode.h:364
FFHWBaseEncodePicture::nb_refs
int nb_refs[MAX_REFERENCE_LIST_NUM]
Definition: hw_base_encode.h:97
for
for(k=2;k<=8;++k)
Definition: h264pred_template.c:425
VAAPI_ENCODE_COMMON_OPTIONS
#define VAAPI_ENCODE_COMMON_OPTIONS
Definition: vaapi_encode.h:350
VAAPIEncodePicture::recon_surface
VASurfaceID recon_surface
Definition: vaapi_encode.h:74
VAAPIEncodePicture::output_buffer
VABufferID output_buffer
Definition: vaapi_encode.h:81
AV_PIX_FMT_VAAPI
@ AV_PIX_FMT_VAAPI
Hardware acceleration through VA-API, data[3] contains a VASurfaceID.
Definition: pixfmt.h:126
VAAPIEncodeVP8Context
Definition: vaapi_encode_vp8.c:34
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:256
VAAPIEncodeVP8Context::q_index_i
int q_index_i
Definition: vaapi_encode_vp8.c:42
FF_HW_PICTURE_TYPE_P
@ FF_HW_PICTURE_TYPE_P
Definition: hw_base_encode.h:41
internal.h
vaapi_encode_vp8_class
static const AVClass vaapi_encode_vp8_class
Definition: vaapi_encode_vp8.c:241
common.h
FFHWBaseEncodePicture::refs
struct FFHWBaseEncodePicture * refs[MAX_REFERENCE_LIST_NUM][MAX_PICTURE_REFERENCES]
Definition: hw_base_encode.h:98
AVCodec::name
const char * name
Name of the codec implementation.
Definition: codec.h:194
AVCodecContext::height
int height
Definition: avcodec.h:624
AVCOL_RANGE_MPEG
@ AVCOL_RANGE_MPEG
Narrow or limited range content.
Definition: pixfmt.h:700
avcodec.h
ff_vaapi_encode_hw_configs
const AVCodecHWConfigInternal *const ff_vaapi_encode_hw_configs[]
Definition: vaapi_encode.c:36
ff_vaapi_encode_init
av_cold int ff_vaapi_encode_init(AVCodecContext *avctx)
Definition: vaapi_encode.c:2091
pixfmt.h
FFHWBaseEncodeContext::gop_size
int gop_size
Definition: hw_base_encode.h:184
FFHWBaseEncodePicture
Definition: hw_base_encode.h:61
AVClass::class_name
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
Definition: log.h:80
FLAGS
#define FLAGS
Definition: vaapi_encode_vp8.c:219
AVCodecContext
main external API structure.
Definition: avcodec.h:451
vaapi_encode_vp8_init_picture_params
static int vaapi_encode_vp8_init_picture_params(AVCodecContext *avctx, FFHWBaseEncodePicture *pic)
Definition: vaapi_encode_vp8.c:76
FF_HW_PICTURE_TYPE_I
@ FF_HW_PICTURE_TYPE_I
Definition: hw_base_encode.h:40
AV_PIX_FMT_NONE
@ AV_PIX_FMT_NONE
Definition: pixfmt.h:72
AV_OPT_TYPE_INT
@ AV_OPT_TYPE_INT
Underlying C type is int.
Definition: opt.h:259
OFFSET
#define OFFSET(x)
Definition: vaapi_encode_vp8.c:218
AVCodecContext::i_quant_offset
float i_quant_offset
qscale offset between P and I-frames
Definition: avcodec.h:833
AV_CODEC_CAP_DELAY
#define AV_CODEC_CAP_DELAY
Encoder or decoder requires flushing with NULL input at the end in order to give the complete and cor...
Definition: codec.h:76
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
vaapi_encode_vp8_write_quant_table
static int vaapi_encode_vp8_write_quant_table(AVCodecContext *avctx, FFHWBaseEncodePicture *base_pic, int index, int *type, char *data, size_t *data_len)
Definition: vaapi_encode_vp8.c:131
AVCodecContext::priv_data
void * priv_data
Definition: avcodec.h:478
AVCodecContext::width
int width
picture width / height.
Definition: avcodec.h:624
AV_CODEC_ID_VP8
@ AV_CODEC_ID_VP8
Definition: codec_id.h:192
HW_BASE_ENCODE_COMMON_OPTIONS
#define HW_BASE_ENCODE_COMMON_OPTIONS
Definition: hw_base_encode.h:239
VAAPIEncodeProfile
Definition: vaapi_encode.h:100
vaapi_encode_vp8_defaults
static const FFCodecDefault vaapi_encode_vp8_defaults[]
Definition: vaapi_encode_vp8.c:232