FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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 "internal.h"
30 #include "vaapi_encode.h"
31 #include "vp8.h"
32 
33 
34 typedef struct VAAPIEncodeVP8Context {
35  int q_index_i;
36  int q_index_p;
38 
39 typedef struct VAAPIEncodeVP8Options {
43 
44 
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
49 
50 
52 {
54  VAEncSequenceParameterBufferVP8 *vseq = ctx->codec_sequence_params;
55 
56  vseq->frame_width = avctx->width;
57  vseq->frame_height = avctx->height;
58 
59  vseq->frame_width_scale = 0;
60  vseq->frame_height_scale = 0;
61 
62  vseq->error_resilient = 0;
63  vseq->kf_auto = 0;
64 
65  if (!(ctx->va_rc_mode & VA_RC_CQP)) {
66  vseq->bits_per_second = avctx->bit_rate;
67  vseq->intra_period = avctx->gop_size;
68  }
69 
70  return 0;
71 }
72 
74  VAAPIEncodePicture *pic)
75 {
77  VAEncPictureParameterBufferVP8 *vpic = pic->codec_picture_params;
79  int i;
80 
81  vpic->reconstructed_frame = pic->recon_surface;
82 
83  vpic->coded_buf = pic->output_buffer;
84 
85  switch (pic->type) {
86  case PICTURE_TYPE_IDR:
87  case PICTURE_TYPE_I:
88  av_assert0(pic->nb_refs == 0);
89  vpic->ref_flags.bits.force_kf = 1;
90  vpic->ref_last_frame =
91  vpic->ref_gf_frame =
92  vpic->ref_arf_frame =
93  VA_INVALID_SURFACE;
94  break;
95  case PICTURE_TYPE_P:
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 =
101  vpic->ref_gf_frame =
102  vpic->ref_arf_frame =
103  pic->refs[0]->recon_surface;
104  break;
105  default:
106  av_assert0(0 && "invalid picture type");
107  }
108 
109  vpic->pic_flags.bits.frame_type = (pic->type != PICTURE_TYPE_IDR);
110  vpic->pic_flags.bits.show_frame = 1;
111 
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;
115 
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;
121 
122  vpic->clamp_qindex_low = 0;
123  vpic->clamp_qindex_high = 127;
124 
125  return 0;
126 }
127 
129  VAAPIEncodePicture *pic,
130  int index, int *type,
131  char *data, size_t *data_len)
132 {
133  VAAPIEncodeContext *ctx = avctx->priv_data;
134  VAAPIEncodeVP8Context *priv = ctx->priv_data;
135  VAQMatrixBufferVP8 quant;
136  int i, q;
137 
138  if (index > 0)
139  return AVERROR_EOF;
140 
141  if (*data_len < sizeof(quant))
142  return AVERROR(EINVAL);
143  *type = VAQMatrixBufferType;
144  *data_len = sizeof(quant);
145 
146  if (pic->type == PICTURE_TYPE_P)
147  q = priv->q_index_p;
148  else
149  q = priv->q_index_i;
150 
151  for (i = 0; i < 4; i++)
152  quant.quantization_index[i] = q;
153  for (i = 0; i < 5; i++)
154  quant.quantization_index_delta[i] = 0;
155 
156  memcpy(data, &quant, sizeof(quant));
157  return 0;
158 }
159 
161 {
162  VAAPIEncodeContext *ctx = avctx->priv_data;
163  VAAPIEncodeVP8Context *priv = ctx->priv_data;
164 
165  priv->q_index_p = av_clip(avctx->global_quality, 0, VP8_MAX_QUANT);
166  if (avctx->i_quant_factor > 0.0)
167  priv->q_index_i = av_clip((avctx->global_quality *
168  avctx->i_quant_factor +
169  avctx->i_quant_offset) + 0.5,
170  0, VP8_MAX_QUANT);
171  else
172  priv->q_index_i = priv->q_index_p;
173 
174  return 0;
175 }
176 
179 
180  .priv_data_size = sizeof(VAAPIEncodeVP8Context),
181 
182  .sequence_params_size = sizeof(VAEncSequenceParameterBufferVP8),
183  .init_sequence_params = &vaapi_encode_vp8_init_sequence_params,
184 
185  .picture_params_size = sizeof(VAEncPictureParameterBufferVP8),
186  .init_picture_params = &vaapi_encode_vp8_init_picture_params,
187 
188  .write_extra_buffer = &vaapi_encode_vp8_write_quant_table,
189 };
190 
192 {
193  VAAPIEncodeContext *ctx = avctx->priv_data;
194 
195  if (avctx->max_b_frames > 0) {
196  av_log(avctx, AV_LOG_ERROR, "B-frames are not supported.\n");
197  return AVERROR_PATCHWELCOME;
198  }
199 
201 
202  ctx->va_profile = VAProfileVP8Version0_3;
203  ctx->va_entrypoint = VAEntrypointEncSlice;
204  ctx->va_rt_format = VA_RT_FORMAT_YUV420;
205 
206  if (avctx->flags & AV_CODEC_FLAG_QSCALE) {
207  ctx->va_rc_mode = VA_RC_CQP;
208  } else if (avctx->bit_rate > 0) {
209  if (avctx->rc_max_rate == avctx->bit_rate)
210  ctx->va_rc_mode = VA_RC_CBR;
211  else
212  ctx->va_rc_mode = VA_RC_VBR;
213  } else {
214  ctx->va_rc_mode = VA_RC_CQP;
215  }
216 
217  // Packed headers are not currently supported.
218  ctx->va_packed_headers = 0;
219 
220  ctx->surface_width = FFALIGN(avctx->width, 16);
221  ctx->surface_height = FFALIGN(avctx->height, 16);
222 
223  return ff_vaapi_encode_init(avctx);
224 }
225 
226 #define OFFSET(x) (offsetof(VAAPIEncodeContext, codec_options_data) + \
227  offsetof(VAAPIEncodeVP8Options, x))
228 #define FLAGS (AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM)
230  { "loop_filter_level", "Loop filter level",
231  OFFSET(loop_filter_level), AV_OPT_TYPE_INT, { .i64 = 16 }, 0, 63, FLAGS },
232  { "loop_filter_sharpness", "Loop filter sharpness",
233  OFFSET(loop_filter_sharpness), AV_OPT_TYPE_INT, { .i64 = 4 }, 0, 15, FLAGS },
234  { NULL },
235 };
236 
238  { "b", "0" },
239  { "bf", "0" },
240  { "g", "120" },
241  { "global_quality", "40" },
242  { NULL },
243 };
244 
246  .class_name = "vp8_vaapi",
247  .item_name = av_default_item_name,
248  .option = vaapi_encode_vp8_options,
249  .version = LIBAVUTIL_VERSION_INT,
250 };
251 
253  .name = "vp8_vaapi",
254  .long_name = NULL_IF_CONFIG_SMALL("VP8 (VAAPI)"),
255  .type = AVMEDIA_TYPE_VIDEO,
256  .id = AV_CODEC_ID_VP8,
257  .priv_data_size = (sizeof(VAAPIEncodeContext) +
258  sizeof(VAAPIEncodeVP8Options)),
260  .encode2 = &ff_vaapi_encode2,
261  .close = &ff_vaapi_encode_close,
262  .priv_class = &vaapi_encode_vp8_class,
263  .capabilities = AV_CODEC_CAP_DELAY,
264  .defaults = vaapi_encode_vp8_defaults,
265  .pix_fmts = (const enum AVPixelFormat[]) {
268  },
269 };
#define NULL
Definition: coverity.c:32
static const AVCodecDefault vaapi_encode_vp8_defaults[]
#define VP8_MAX_QUANT
Definition: vp8.h:39
VAProfile va_profile
Definition: vaapi_encode.h:98
VAEntrypoint va_entrypoint
Definition: vaapi_encode.h:100
AVOption.
Definition: opt.h:246
ptrdiff_t const GLvoid * data
Definition: opengl_enc.c:101
int64_t bit_rate
the average bitrate
Definition: avcodec.h:1797
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
av_cold int ff_vaapi_encode_init(AVCodecContext *avctx)
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
int max_b_frames
maximum number of B-frames between non-B-frames Note: The output will be delayed by max_b_frames+1 re...
Definition: avcodec.h:2018
av_cold int ff_vaapi_encode_close(AVCodecContext *avctx)
static int vaapi_encode_vp8_write_quant_table(AVCodecContext *avctx, VAAPIEncodePicture *pic, int index, int *type, char *data, size_t *data_len)
void * codec_sequence_params
Definition: vaapi_encode.h:164
AVCodec.
Definition: avcodec.h:3681
float i_quant_offset
qscale offset between P and I-frames
Definition: avcodec.h:2076
unsigned int va_packed_headers
Definition: vaapi_encode.h:107
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
#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:1019
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
#define av_cold
Definition: attributes.h:82
AVOptions.
AVCodec ff_vp8_vaapi_encoder
#define AVERROR_EOF
End of file.
Definition: error.h:55
VASurfaceID recon_surface
Definition: vaapi_encode.h:73
#define FFALIGN(x, a)
Definition: macros.h:48
#define av_log(a,...)
unsigned int va_rc_mode
Definition: vaapi_encode.h:104
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
av_default_item_name
#define AVERROR(e)
Definition: error.h:43
int ff_vaapi_encode2(AVCodecContext *avctx, AVPacket *pkt, const AVFrame *input_image, int *got_packet)
Definition: vaapi_encode.c:831
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:179
static int vaapi_encode_vp8_init_sequence_params(AVCodecContext *avctx)
int(* configure)(AVCodecContext *avctx)
Definition: vaapi_encode.h:219
int flags
AV_CODEC_FLAG_*.
Definition: avcodec.h:1827
simple assert() macros that are a bit more flexible than ISO C assert().
const char * name
Name of the codec implementation.
Definition: avcodec.h:3688
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:2069
void * codec_picture_params
Definition: vaapi_encode.h:82
static const AVClass vaapi_encode_vp8_class
common internal API header
#define AV_CODEC_FLAG_QSCALE
Use fixed qscale.
Definition: avcodec.h:868
int width
picture width / height.
Definition: avcodec.h:1919
AVFormatContext * ctx
Definition: movenc.c:48
static av_cold int vaapi_encode_vp8_configure(AVCodecContext *avctx)
unsigned int va_rt_format
Definition: vaapi_encode.h:102
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition: error.h:62
struct VAAPIEncodePicture * refs[MAX_PICTURE_REFERENCES]
Definition: vaapi_encode.h:85
const struct VAAPIEncodeType * codec
Definition: vaapi_encode.h:95
Libavcodec external API header.
main external API structure.
Definition: avcodec.h:1732
GLint GLenum type
Definition: opengl_enc.c:105
Describe the class of an AVClass context structure.
Definition: log.h:67
int index
Definition: gxfenc.c:89
#define OFFSET(x)
static int vaapi_encode_vp8_init_picture_params(AVCodecContext *avctx, VAAPIEncodePicture *pic)
static enum AVPixelFormat pix_fmts[]
Definition: libkvazaar.c:262
#define FLAGS
const uint8_t * quant
int global_quality
Global quality for codecs which cannot change it per frame.
Definition: avcodec.h:1813
int gop_size
the number of pictures in a group of pictures, or 0 for intra_only
Definition: avcodec.h:1945
static const AVOption vaapi_encode_vp8_options[]
common internal api header.
common internal and external API header
void * priv_data
Definition: avcodec.h:1774
pixel format definitions
static const AVCodecDefault defaults[]
Definition: dcaenc.c:1095
static const VAAPIEncodeType vaapi_encode_type_vp8
VABufferID output_buffer
Definition: vaapi_encode.h:79
AVPixelFormat
Pixel format.
Definition: pixfmt.h:60
for(j=16;j >0;--j)
static av_cold int vaapi_encode_vp8_init(AVCodecContext *avctx)
int64_t rc_max_rate
maximum bitrate
Definition: avcodec.h:2732