]> git.sesse.net Git - ffmpeg/blob - libavcodec/vaapi_encode_vp8.c
857054dc6485bd5f5e450dde34f6b6571975d087
[ffmpeg] / libavcodec / vaapi_encode_vp8.c
1 /*
2  * This file is part of Libav.
3  *
4  * Libav 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  * Libav 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 Libav; 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
32
33 typedef struct VAAPIEncodeVP8Context {
34     int q_index_i;
35     int q_index_p;
36 } VAAPIEncodeVP8Context;
37
38 typedef struct VAAPIEncodeVP8Options {
39     int loop_filter_level;
40     int loop_filter_sharpness;
41 } VAAPIEncodeVP8Options;
42
43
44 #define vseq_var(name)     vseq->name, name
45 #define vseq_field(name)   vseq->seq_fields.bits.name, name
46 #define vpic_var(name)     vpic->name, name
47 #define vpic_field(name)   vpic->pic_fields.bits.name, name
48
49
50 static int vaapi_encode_vp8_init_sequence_params(AVCodecContext *avctx)
51 {
52     VAAPIEncodeContext               *ctx = avctx->priv_data;
53     VAEncSequenceParameterBufferVP8 *vseq = ctx->codec_sequence_params;
54
55     vseq->frame_width  = avctx->width;
56     vseq->frame_height = avctx->height;
57
58     vseq->frame_width_scale  = 0;
59     vseq->frame_height_scale = 0;
60
61     vseq->error_resilient = 0;
62     vseq->kf_auto = 0;
63
64     if (!(ctx->va_rc_mode & VA_RC_CQP)) {
65         vseq->bits_per_second = avctx->bit_rate;
66         vseq->intra_period    = avctx->gop_size;
67     }
68
69     return 0;
70 }
71
72 static int vaapi_encode_vp8_init_picture_params(AVCodecContext *avctx,
73                                                 VAAPIEncodePicture *pic)
74 {
75     VAAPIEncodeContext              *ctx = avctx->priv_data;
76     VAEncPictureParameterBufferVP8 *vpic = pic->codec_picture_params;
77     VAAPIEncodeVP8Options           *opt = ctx->codec_options;
78     int i;
79
80     vpic->reconstructed_frame = pic->recon_surface;
81
82     vpic->coded_buf = pic->output_buffer;
83
84     switch (pic->type) {
85     case PICTURE_TYPE_IDR:
86     case PICTURE_TYPE_I:
87         av_assert0(pic->nb_refs == 0);
88         vpic->ref_flags.bits.force_kf = 1;
89         vpic->ref_last_frame =
90         vpic->ref_gf_frame   =
91         vpic->ref_arf_frame  =
92             VA_INVALID_SURFACE;
93         break;
94     case PICTURE_TYPE_P:
95         av_assert0(pic->nb_refs == 1);
96         vpic->ref_flags.bits.no_ref_last = 0;
97         vpic->ref_flags.bits.no_ref_gf   = 1;
98         vpic->ref_flags.bits.no_ref_arf  = 1;
99         vpic->ref_last_frame =
100         vpic->ref_gf_frame   =
101         vpic->ref_arf_frame  =
102             pic->refs[0]->recon_surface;
103         break;
104     default:
105         av_assert0(0 && "invalid picture type");
106     }
107
108     vpic->pic_flags.bits.frame_type = (pic->type != PICTURE_TYPE_IDR);
109     vpic->pic_flags.bits.show_frame = 1;
110
111     vpic->pic_flags.bits.refresh_last            = 1;
112     vpic->pic_flags.bits.refresh_golden_frame    = 1;
113     vpic->pic_flags.bits.refresh_alternate_frame = 1;
114
115     vpic->pic_flags.bits.version = 0;
116     vpic->pic_flags.bits.loop_filter_type = 0;
117     for (i = 0; i < 4; i++)
118         vpic->loop_filter_level[i] = opt->loop_filter_level;
119     vpic->sharpness_level = opt->loop_filter_sharpness;
120
121     vpic->clamp_qindex_low  = 0;
122     vpic->clamp_qindex_high = 127;
123
124     return 0;
125 }
126
127 static int vaapi_encode_vp8_write_quant_table(AVCodecContext *avctx,
128                                               VAAPIEncodePicture *pic,
129                                               int index, int *type,
130                                               char *data, size_t *data_len)
131 {
132     VAAPIEncodeContext     *ctx = avctx->priv_data;
133     VAAPIEncodeVP8Context *priv = ctx->priv_data;
134     VAQMatrixBufferVP8 quant;
135     int i, q;
136
137     if (index > 0)
138         return AVERROR_EOF;
139
140     if (*data_len < sizeof(quant))
141         return AVERROR(EINVAL);
142     *type     = VAQMatrixBufferType;
143     *data_len = sizeof(quant);
144
145     if (pic->type == PICTURE_TYPE_P)
146         q = priv->q_index_p;
147     else
148         q = priv->q_index_i;
149
150     for (i = 0; i < 4; i++)
151         quant.quantization_index[i] = q;
152     for (i = 0; i < 5; i++)
153         quant.quantization_index_delta[i] = 0;
154
155     memcpy(data, &quant, sizeof(quant));
156     return 0;
157 }
158
159 static av_cold int vaapi_encode_vp8_configure(AVCodecContext *avctx)
160 {
161     VAAPIEncodeContext     *ctx = avctx->priv_data;
162     VAAPIEncodeVP8Context *priv = ctx->priv_data;
163
164     priv->q_index_p = av_clip(avctx->global_quality, 0, 127);
165     if (avctx->i_quant_factor > 0.0)
166         priv->q_index_i = av_clip((avctx->global_quality *
167                                    avctx->i_quant_factor +
168                                    avctx->i_quant_offset) + 0.5,
169                                   0, 127);
170     else
171         priv->q_index_i = priv->q_index_p;
172
173     return 0;
174 }
175
176 static const VAAPIEncodeType vaapi_encode_type_vp8 = {
177     .configure             = &vaapi_encode_vp8_configure,
178
179     .priv_data_size        = sizeof(VAAPIEncodeVP8Context),
180
181     .sequence_params_size  = sizeof(VAEncSequenceParameterBufferVP8),
182     .init_sequence_params  = &vaapi_encode_vp8_init_sequence_params,
183
184     .picture_params_size   = sizeof(VAEncPictureParameterBufferVP8),
185     .init_picture_params   = &vaapi_encode_vp8_init_picture_params,
186
187     .write_extra_buffer    = &vaapi_encode_vp8_write_quant_table,
188 };
189
190 static av_cold int vaapi_encode_vp8_init(AVCodecContext *avctx)
191 {
192     VAAPIEncodeContext *ctx = avctx->priv_data;
193
194     if (avctx->max_b_frames > 0) {
195         av_log(avctx, AV_LOG_ERROR, "B-frames are not supported.\n");
196         return AVERROR_PATCHWELCOME;
197     }
198
199     ctx->codec = &vaapi_encode_type_vp8;
200
201     ctx->va_profile    = VAProfileVP8Version0_3;
202     ctx->va_entrypoint = VAEntrypointEncSlice;
203     ctx->va_rt_format  = VA_RT_FORMAT_YUV420;
204
205     if (avctx->flags & AV_CODEC_FLAG_QSCALE) {
206         ctx->va_rc_mode = VA_RC_CQP;
207     } else if (avctx->bit_rate > 0) {
208         if (avctx->rc_max_rate == avctx->bit_rate)
209             ctx->va_rc_mode = VA_RC_CBR;
210         else
211             ctx->va_rc_mode = VA_RC_VBR;
212     } else {
213         ctx->va_rc_mode = VA_RC_CQP;
214     }
215
216     // Packed headers are not currently supported.
217     ctx->va_packed_headers = 0;
218
219     ctx->surface_width  = FFALIGN(avctx->width,  16);
220     ctx->surface_height = FFALIGN(avctx->height, 16);
221
222     return ff_vaapi_encode_init(avctx);
223 }
224
225 #define OFFSET(x) (offsetof(VAAPIEncodeContext, codec_options_data) + \
226                    offsetof(VAAPIEncodeVP8Options, x))
227 #define FLAGS (AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM)
228 static const AVOption vaapi_encode_vp8_options[] = {
229     { "loop_filter_level", "Loop filter level",
230       OFFSET(loop_filter_level), AV_OPT_TYPE_INT, { .i64 = 16 }, 0, 63, FLAGS },
231     { "loop_filter_sharpness", "Loop filter sharpness",
232       OFFSET(loop_filter_sharpness), AV_OPT_TYPE_INT, { .i64 = 4 }, 0, 15, FLAGS },
233     { NULL },
234 };
235
236 static const AVCodecDefault vaapi_encode_vp8_defaults[] = {
237     { "b",              "0"   },
238     { "bf",             "0"   },
239     { "g",              "120" },
240     { "global_quality", "40"  },
241     { NULL },
242 };
243
244 static const AVClass vaapi_encode_vp8_class = {
245     .class_name = "vp8_vaapi",
246     .item_name  = av_default_item_name,
247     .option     = vaapi_encode_vp8_options,
248     .version    = LIBAVUTIL_VERSION_INT,
249 };
250
251 AVCodec ff_vp8_vaapi_encoder = {
252     .name           = "vp8_vaapi",
253     .long_name      = NULL_IF_CONFIG_SMALL("VP8 (VAAPI)"),
254     .type           = AVMEDIA_TYPE_VIDEO,
255     .id             = AV_CODEC_ID_VP8,
256     .priv_data_size = (sizeof(VAAPIEncodeContext) +
257                        sizeof(VAAPIEncodeVP8Options)),
258     .init           = &vaapi_encode_vp8_init,
259     .encode2        = &ff_vaapi_encode2,
260     .close          = &ff_vaapi_encode_close,
261     .priv_class     = &vaapi_encode_vp8_class,
262     .capabilities   = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_HARDWARE,
263     .defaults       = vaapi_encode_vp8_defaults,
264     .pix_fmts = (const enum AVPixelFormat[]) {
265         AV_PIX_FMT_VAAPI,
266         AV_PIX_FMT_NONE,
267     },
268     .wrapper_name   = "vaapi",
269 };