FFmpeg
vaapi_encode_vp9.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_vp9.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 "internal.h"
30 #include "vaapi_encode.h"
31 
32 #define VP9_MAX_QUANT 255
33 
34 
35 typedef struct VAAPIEncodeVP9Picture {
36  int slot;
38 
39 typedef struct VAAPIEncodeVP9Context {
41 
42  // User options.
45 
46  // Derived settings.
47  int q_idx_idr;
48  int q_idx_p;
49  int q_idx_b;
51 
52 
54 {
56  VAEncSequenceParameterBufferVP9 *vseq = ctx->codec_sequence_params;
57  VAEncPictureParameterBufferVP9 *vpic = ctx->codec_picture_params;
58 
59  vseq->max_frame_width = avctx->width;
60  vseq->max_frame_height = avctx->height;
61 
62  vseq->kf_auto = 0;
63 
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;
67  }
68 
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;
73 
74  return 0;
75 }
76 
78  VAAPIEncodePicture *pic)
79 {
81  VAAPIEncodeVP9Context *priv = avctx->priv_data;
82  VAAPIEncodeVP9Picture *hpic = pic->priv_data;
83  VAEncPictureParameterBufferVP9 *vpic = pic->codec_picture_params;
84  int i;
85 
86  vpic->reconstructed_frame = pic->recon_surface;
87  vpic->coded_buf = pic->output_buffer;
88 
89  switch (pic->type) {
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;
94  hpic->slot = 0;
95  break;
96  case PICTURE_TYPE_P:
97  av_assert0(pic->nb_refs == 1);
98  {
99  VAAPIEncodeVP9Picture *href = pic->refs[0]->priv_data;
100  av_assert0(href->slot == 0 || href->slot == 1);
101 
102  if (ctx->max_b_depth > 0) {
103  hpic->slot = !href->slot;
104  vpic->refresh_frame_flags = 1 << hpic->slot | 0xfc;
105  } else {
106  hpic->slot = 0;
107  vpic->refresh_frame_flags = 0xff;
108  }
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;
112  }
113  break;
114  case PICTURE_TYPE_B:
115  av_assert0(pic->nb_refs == 2);
116  {
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);
121 
122  if (pic->b_depth == ctx->max_b_depth) {
123  // Unreferenced frame.
124  vpic->refresh_frame_flags = 0x00;
125  hpic->slot = 8;
126  } else {
127  vpic->refresh_frame_flags = 0xfe << pic->b_depth & 0xff;
128  hpic->slot = 1 + pic->b_depth;
129  }
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;
136  }
137  break;
138  default:
139  av_assert0(0 && "invalid picture type");
140  }
141  if (vpic->refresh_frame_flags == 0x00) {
142  av_log(avctx, AV_LOG_DEBUG, "Pic %"PRId64" not stored.\n",
143  pic->display_order);
144  } else {
145  av_log(avctx, AV_LOG_DEBUG, "Pic %"PRId64" stored in slot %d.\n",
146  pic->display_order, hpic->slot);
147  }
148 
149  for (i = 0; i < FF_ARRAY_ELEMS(vpic->reference_frames); i++)
150  vpic->reference_frames[i] = VA_INVALID_SURFACE;
151 
152  for (i = 0; i < pic->nb_refs; i++) {
153  VAAPIEncodePicture *ref_pic = pic->refs[i];
154  int slot;
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;
158  }
159 
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;
162 
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;
167  else
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;
172 
173  vpic->filter_level = priv->loop_filter_level;
174  vpic->sharpness_level = priv->loop_filter_sharpness;
175 
176  return 0;
177 }
178 
180 {
181  VAAPIEncodeContext *ctx = avctx->priv_data;
182  VAAPIEncodeVP9Context *priv = avctx->priv_data;
183 
184  if (ctx->rc_mode->quality) {
185  priv->q_idx_p = av_clip(ctx->rc_quality, 0, VP9_MAX_QUANT);
186  if (avctx->i_quant_factor > 0.0)
187  priv->q_idx_idr =
188  av_clip((avctx->i_quant_factor * priv->q_idx_p +
189  avctx->i_quant_offset) + 0.5,
190  0, VP9_MAX_QUANT);
191  else
192  priv->q_idx_idr = priv->q_idx_p;
193  if (avctx->b_quant_factor > 0.0)
194  priv->q_idx_b =
195  av_clip((avctx->b_quant_factor * priv->q_idx_p +
196  avctx->b_quant_offset) + 0.5,
197  0, VP9_MAX_QUANT);
198  else
199  priv->q_idx_b = priv->q_idx_p;
200  } else {
201  // Arbitrary value.
202  priv->q_idx_idr = priv->q_idx_p = priv->q_idx_b = 100;
203  }
204 
205  return 0;
206 }
207 
209  { FF_PROFILE_VP9_0, 8, 3, 1, 1, VAProfileVP9Profile0 },
210  { FF_PROFILE_VP9_2, 10, 3, 1, 1, VAProfileVP9Profile2 },
212 };
213 
216 
217  .flags = FLAG_B_PICTURES |
219 
220  .default_quality = 100,
221 
222  .picture_priv_data_size = sizeof(VAAPIEncodeVP9Picture),
223 
224  .configure = &vaapi_encode_vp9_configure,
225 
226  .sequence_params_size = sizeof(VAEncSequenceParameterBufferVP9),
227  .init_sequence_params = &vaapi_encode_vp9_init_sequence_params,
228 
229  .picture_params_size = sizeof(VAEncPictureParameterBufferVP9),
230  .init_picture_params = &vaapi_encode_vp9_init_picture_params,
231 };
232 
234 {
235  VAAPIEncodeContext *ctx = avctx->priv_data;
236 
237  ctx->codec = &vaapi_encode_type_vp9;
238 
239  // No packed headers are currently desired. They could be written,
240  // but there isn't any reason to do so - the one usable driver (i965)
241  // can write its own headers and there is no metadata to include.
242  ctx->desired_packed_headers = 0;
243 
244  // Surfaces must be aligned to superblock boundaries.
245  ctx->surface_width = FFALIGN(avctx->width, 64);
246  ctx->surface_height = FFALIGN(avctx->height, 64);
247 
248  return ff_vaapi_encode_init(avctx);
249 }
250 
251 #define OFFSET(x) offsetof(VAAPIEncodeVP9Context, x)
252 #define FLAGS (AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM)
256 
257  { "loop_filter_level", "Loop filter level",
258  OFFSET(loop_filter_level), AV_OPT_TYPE_INT, { .i64 = 16 }, 0, 63, FLAGS },
259  { "loop_filter_sharpness", "Loop filter sharpness",
260  OFFSET(loop_filter_sharpness), AV_OPT_TYPE_INT, { .i64 = 4 }, 0, 15, FLAGS },
261  { NULL },
262 };
263 
265  { "b", "0" },
266  { "bf", "0" },
267  { "g", "250" },
268  { "qmin", "-1" },
269  { "qmax", "-1" },
270  { NULL },
271 };
272 
274  .class_name = "vp9_vaapi",
275  .item_name = av_default_item_name,
276  .option = vaapi_encode_vp9_options,
277  .version = LIBAVUTIL_VERSION_INT,
278 };
279 
281  .name = "vp9_vaapi",
282  .long_name = NULL_IF_CONFIG_SMALL("VP9 (VAAPI)"),
283  .type = AVMEDIA_TYPE_VIDEO,
284  .id = AV_CODEC_ID_VP9,
285  .priv_data_size = sizeof(VAAPIEncodeVP9Context),
287  .send_frame = &ff_vaapi_encode_send_frame,
288  .receive_packet = &ff_vaapi_encode_receive_packet,
289  .close = &ff_vaapi_encode_close,
290  .priv_class = &vaapi_encode_vp9_class,
291  .capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_HARDWARE,
293  .pix_fmts = (const enum AVPixelFormat[]) {
296  },
297  .wrapper_name = "vaapi",
298 };
AVCodec
AVCodec.
Definition: avcodec.h:3481
AVPixelFormat
AVPixelFormat
Pixel format.
Definition: pixfmt.h:64
init
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
opt.h
FF_PROFILE_VP9_0
#define FF_PROFILE_VP9_0
Definition: avcodec.h:2981
AV_CODEC_CAP_HARDWARE
#define AV_CODEC_CAP_HARDWARE
Codec is backed by a hardware implementation.
Definition: avcodec.h:1078
vaapi_encode_vp9_configure
static av_cold int vaapi_encode_vp9_configure(AVCodecContext *avctx)
Definition: vaapi_encode_vp9.c:179
VAAPIEncodeVP9Context
Definition: vaapi_encode_vp9.c:39
internal.h
VAAPIEncodeVP9Context::loop_filter_sharpness
int loop_filter_sharpness
Definition: vaapi_encode_vp9.c:44
AVOption
AVOption.
Definition: opt.h:246
VAAPIEncodePicture::refs
struct VAAPIEncodePicture * refs[MAX_PICTURE_REFERENCES]
Definition: vaapi_encode.h:103
vaapi_encode_vp9_init_picture_params
static int vaapi_encode_vp9_init_picture_params(AVCodecContext *avctx, VAAPIEncodePicture *pic)
Definition: vaapi_encode_vp9.c:77
AVCodecContext::b_quant_offset
float b_quant_offset
qscale offset between IP and B-frames
Definition: avcodec.h:1847
ff_vaapi_encode_close
av_cold int ff_vaapi_encode_close(AVCodecContext *avctx)
Definition: vaapi_encode.c:2243
VP9_MAX_QUANT
#define VP9_MAX_QUANT
Definition: vaapi_encode_vp9.c:32
VAAPIEncodePicture::nb_refs
int nb_refs
Definition: vaapi_encode.h:102
VAAPIEncodeVP9Picture
Definition: vaapi_encode_vp9.c:35
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:1870
vaapi_encode.h
VAAPIEncodePicture
Definition: vaapi_encode.h:64
PICTURE_TYPE_P
@ PICTURE_TYPE_P
Definition: vaapi_encode.h:50
defaults
static const AVCodecDefault defaults[]
Definition: amfenc_h264.c:361
vaapi_encode_vp9_defaults
static const AVCodecDefault vaapi_encode_vp9_defaults[]
Definition: vaapi_encode_vp9.c:264
avassert.h
vaapi_encode_vp9_init
static av_cold int vaapi_encode_vp9_init(AVCodecContext *avctx)
Definition: vaapi_encode_vp9.c:233
av_cold
#define av_cold
Definition: attributes.h:84
PICTURE_TYPE_B
@ PICTURE_TYPE_B
Definition: vaapi_encode.h:51
VAAPIEncodePicture::codec_picture_params
void * codec_picture_params
Definition: vaapi_encode.h:90
FLAG_B_PICTURE_REFERENCES
@ FLAG_B_PICTURE_REFERENCES
Definition: vaapi_encode.h:322
AV_CODEC_ID_VP9
@ AV_CODEC_ID_VP9
Definition: avcodec.h:386
FF_PROFILE_UNKNOWN
#define FF_PROFILE_UNKNOWN
Definition: avcodec.h:2899
av_assert0
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
pix_fmts
static enum AVPixelFormat pix_fmts[]
Definition: libkvazaar.c:275
OFFSET
#define OFFSET(x)
Definition: vaapi_encode_vp9.c:251
AV_LOG_DEBUG
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:197
ctx
AVFormatContext * ctx
Definition: movenc.c:48
PICTURE_TYPE_IDR
@ PICTURE_TYPE_IDR
Definition: vaapi_encode.h:48
VAAPIEncodeVP9Context::q_idx_p
int q_idx_p
Definition: vaapi_encode_vp9.c:48
vaapi_encode_type_vp9
static const VAAPIEncodeType vaapi_encode_type_vp9
Definition: vaapi_encode_vp9.c:214
ff_vaapi_encode_receive_packet
int ff_vaapi_encode_receive_packet(AVCodecContext *avctx, AVPacket *pkt)
Definition: vaapi_encode.c:1020
ff_vaapi_encode_send_frame
int ff_vaapi_encode_send_frame(AVCodecContext *avctx, const AVFrame *frame)
Definition: vaapi_encode.c:954
VAAPIEncodeType
Definition: vaapi_encode.h:328
FF_PROFILE_VP9_2
#define FF_PROFILE_VP9_2
Definition: avcodec.h:2983
VAAPIEncodeContext
Definition: vaapi_encode.h:162
AVCodecDefault
Definition: internal.h:231
FLAGS
#define FLAGS
Definition: vaapi_encode_vp9.c:252
LIBAVUTIL_VERSION_INT
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:67
NULL
#define NULL
Definition: coverity.c:32
vaapi_encode_vp9_profiles
static const VAAPIEncodeProfile vaapi_encode_vp9_profiles[]
Definition: vaapi_encode_vp9.c:208
VAAPIEncodeVP9Context::common
VAAPIEncodeContext common
Definition: vaapi_encode_vp9.c:40
VAAPIEncodeType::profiles
const VAAPIEncodeProfile * profiles
Definition: vaapi_encode.h:331
av_default_item_name
const char * av_default_item_name(void *ptr)
Return the context name.
Definition: log.c:191
VAAPIEncodeVP9Context::q_idx_idr
int q_idx_idr
Definition: vaapi_encode_vp9.c:47
vaapi_encode_vp9_init_sequence_params
static int vaapi_encode_vp9_init_sequence_params(AVCodecContext *avctx)
Definition: vaapi_encode_vp9.c:53
VAAPIEncodeVP9Picture::slot
int slot
Definition: vaapi_encode_vp9.c:36
for
for(j=16;j >0;--j)
Definition: h264pred_template.c:469
VAAPIEncodeVP9Context::q_idx_b
int q_idx_b
Definition: vaapi_encode_vp9.c:49
VAAPIEncodePicture::type
int type
Definition: vaapi_encode.h:72
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:188
VAAPI_ENCODE_RC_OPTIONS
#define VAAPI_ENCODE_RC_OPTIONS
Definition: vaapi_encode.h:426
VAAPI_ENCODE_COMMON_OPTIONS
#define VAAPI_ENCODE_COMMON_OPTIONS
Definition: vaapi_encode.h:408
VAAPIEncodePicture::recon_surface
VASurfaceID recon_surface
Definition: vaapi_encode.h:81
VAAPIEncodePicture::output_buffer
VABufferID output_buffer
Definition: vaapi_encode.h:87
VAAPIEncodePicture::priv_data
void * priv_data
Definition: vaapi_encode.h:89
VAAPIEncodePicture::display_order
int64_t display_order
Definition: vaapi_encode.h:67
AV_PIX_FMT_VAAPI
@ AV_PIX_FMT_VAAPI
Definition: pixfmt.h:122
VAAPIEncodePicture::b_depth
int b_depth
Definition: vaapi_encode.h:73
AVCodecContext::b_quant_factor
float b_quant_factor
qscale factor between IP and B-frames If > 0 then the last P-frame quantizer will be used (q= lastp_q...
Definition: avcodec.h:1834
FLAG_B_PICTURES
@ FLAG_B_PICTURES
Definition: vaapi_encode.h:320
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:259
VAAPIEncodeVP9Context::loop_filter_level
int loop_filter_level
Definition: vaapi_encode_vp9.c:43
internal.h
common.h
ff_vp9_vaapi_encoder
AVCodec ff_vp9_vaapi_encoder
Definition: vaapi_encode_vp9.c:280
AVCodec::name
const char * name
Name of the codec implementation.
Definition: avcodec.h:3488
AVCodecContext::height
int height
Definition: avcodec.h:1738
avcodec.h
ff_vaapi_encode_init
av_cold int ff_vaapi_encode_init(AVCodecContext *avctx)
Definition: vaapi_encode.c:2084
pixfmt.h
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:72
FF_ARRAY_ELEMS
#define FF_ARRAY_ELEMS(a)
Definition: sinewin_tablegen_template.c:38
AVCodecContext
main external API structure.
Definition: avcodec.h:1565
AV_PIX_FMT_NONE
@ AV_PIX_FMT_NONE
Definition: pixfmt.h:65
AV_OPT_TYPE_INT
@ AV_OPT_TYPE_INT
Definition: opt.h:223
AVCodecContext::i_quant_offset
float i_quant_offset
qscale offset between P and I-frames
Definition: avcodec.h:1877
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: avcodec.h:1006
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
vaapi_encode_vp9_class
static const AVClass vaapi_encode_vp9_class
Definition: vaapi_encode_vp9.c:273
FFALIGN
#define FFALIGN(x, a)
Definition: macros.h:48
AVCodecContext::priv_data
void * priv_data
Definition: avcodec.h:1592
VAAPIEncodePicture::encode_order
int64_t encode_order
Definition: vaapi_encode.h:68
AVCodecContext::width
int width
picture width / height.
Definition: avcodec.h:1738
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:28
vaapi_encode_vp9_options
static const AVOption vaapi_encode_vp9_options[]
Definition: vaapi_encode_vp9.c:253
VAAPIEncodeProfile
Definition: vaapi_encode.h:117