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 "codec_internal.h"
30 #include "vaapi_encode.h"
31 
32 #define VP9_MAX_QUANT 255
33 
34 #define VP9_MAX_TILE_WIDTH 4096
35 
36 typedef struct VAAPIEncodeVP9Picture {
37     int slot;
38 } VAAPIEncodeVP9Picture;
39 
40 typedef struct VAAPIEncodeVP9Context {
41     VAAPIEncodeContext common;
42 
43     // User options.
44     int loop_filter_level;
45     int loop_filter_sharpness;
46 
47     // Derived settings.
48     int q_idx_idr;
49     int q_idx_p;
50     int q_idx_b;
51 } VAAPIEncodeVP9Context;
52 
53 
vaapi_encode_vp9_init_sequence_params(AVCodecContext *avctx)54 static int vaapi_encode_vp9_init_sequence_params(AVCodecContext *avctx)
55 {
56     VAAPIEncodeContext               *ctx = avctx->priv_data;
57     VAEncSequenceParameterBufferVP9 *vseq = ctx->codec_sequence_params;
58     VAEncPictureParameterBufferVP9  *vpic = ctx->codec_picture_params;
59 
60     vseq->max_frame_width  = avctx->width;
61     vseq->max_frame_height = avctx->height;
62 
63     vseq->kf_auto = 0;
64 
65     if (!(ctx->va_rc_mode & VA_RC_CQP)) {
66         vseq->bits_per_second = ctx->va_bit_rate;
67         vseq->intra_period    = ctx->gop_size;
68     }
69 
70     vpic->frame_width_src  = avctx->width;
71     vpic->frame_height_src = avctx->height;
72     vpic->frame_width_dst  = avctx->width;
73     vpic->frame_height_dst = avctx->height;
74 
75     return 0;
76 }
77 
vaapi_encode_vp9_init_picture_params(AVCodecContext *avctx, VAAPIEncodePicture *pic)78 static int vaapi_encode_vp9_init_picture_params(AVCodecContext *avctx,
79                                                 VAAPIEncodePicture *pic)
80 {
81     VAAPIEncodeContext              *ctx = avctx->priv_data;
82     VAAPIEncodeVP9Context          *priv = avctx->priv_data;
83     VAAPIEncodeVP9Picture          *hpic = pic->priv_data;
84     VAEncPictureParameterBufferVP9 *vpic = pic->codec_picture_params;
85     int i;
86     int num_tile_columns;
87 
88     vpic->reconstructed_frame = pic->recon_surface;
89     vpic->coded_buf = pic->output_buffer;
90 
91     // Maximum width of a tile in units of superblocks is MAX_TILE_WIDTH_B64(64)
92     // So the number of tile columns is related to the width of the picture.
93     // We set the minimum possible number for num_tile_columns as default value.
94     num_tile_columns = (vpic->frame_width_src + VP9_MAX_TILE_WIDTH - 1) / VP9_MAX_TILE_WIDTH;
95     vpic->log2_tile_columns = num_tile_columns == 1 ? 0 : av_log2(num_tile_columns - 1) + 1;
96 
97     switch (pic->type) {
98     case PICTURE_TYPE_IDR:
99         av_assert0(pic->nb_refs == 0);
100         vpic->ref_flags.bits.force_kf = 1;
101         vpic->refresh_frame_flags = 0xff;
102         hpic->slot = 0;
103         break;
104     case PICTURE_TYPE_P:
105         av_assert0(pic->nb_refs == 1);
106         {
107             VAAPIEncodeVP9Picture *href = pic->refs[0]->priv_data;
108             av_assert0(href->slot == 0 || href->slot == 1);
109 
110             if (ctx->max_b_depth > 0) {
111                 hpic->slot = !href->slot;
112                 vpic->refresh_frame_flags = 1 << hpic->slot | 0xfc;
113             } else {
114                 hpic->slot = 0;
115                 vpic->refresh_frame_flags = 0xff;
116             }
117             vpic->ref_flags.bits.ref_frame_ctrl_l0  = 1;
118             vpic->ref_flags.bits.ref_last_idx       = href->slot;
119             vpic->ref_flags.bits.ref_last_sign_bias = 1;
120         }
121         break;
122     case PICTURE_TYPE_B:
123         av_assert0(pic->nb_refs == 2);
124         {
125             VAAPIEncodeVP9Picture *href0 = pic->refs[0]->priv_data,
126                                   *href1 = pic->refs[1]->priv_data;
127             av_assert0(href0->slot < pic->b_depth + 1 &&
128                        href1->slot < pic->b_depth + 1);
129 
130             if (pic->b_depth == ctx->max_b_depth) {
131                 // Unreferenced frame.
132                 vpic->refresh_frame_flags = 0x00;
133                 hpic->slot = 8;
134             } else {
135                 vpic->refresh_frame_flags = 0xfe << pic->b_depth & 0xff;
136                 hpic->slot = 1 + pic->b_depth;
137             }
138             vpic->ref_flags.bits.ref_frame_ctrl_l0  = 1;
139             vpic->ref_flags.bits.ref_frame_ctrl_l1  = 2;
140             vpic->ref_flags.bits.ref_last_idx       = href0->slot;
141             vpic->ref_flags.bits.ref_last_sign_bias = 1;
142             vpic->ref_flags.bits.ref_gf_idx         = href1->slot;
143             vpic->ref_flags.bits.ref_gf_sign_bias   = 0;
144         }
145         break;
146     default:
147         av_assert0(0 && "invalid picture type");
148     }
149     if (vpic->refresh_frame_flags == 0x00) {
150         av_log(avctx, AV_LOG_DEBUG, "Pic %"PRId64" not stored.\n",
151                pic->display_order);
152     } else {
153         av_log(avctx, AV_LOG_DEBUG, "Pic %"PRId64" stored in slot %d.\n",
154                pic->display_order, hpic->slot);
155     }
156 
157     for (i = 0; i < FF_ARRAY_ELEMS(vpic->reference_frames); i++)
158         vpic->reference_frames[i] = VA_INVALID_SURFACE;
159 
160     for (i = 0; i < pic->nb_refs; i++) {
161         VAAPIEncodePicture *ref_pic = pic->refs[i];
162         int slot;
163         slot = ((VAAPIEncodeVP9Picture*)ref_pic->priv_data)->slot;
164         av_assert0(vpic->reference_frames[slot] == VA_INVALID_SURFACE);
165         vpic->reference_frames[slot] = ref_pic->recon_surface;
166     }
167 
168     vpic->pic_flags.bits.frame_type = (pic->type != PICTURE_TYPE_IDR);
169     vpic->pic_flags.bits.show_frame = pic->display_order <= pic->encode_order;
170 
171     if (pic->type == PICTURE_TYPE_IDR)
172         vpic->luma_ac_qindex     = priv->q_idx_idr;
173     else if (pic->type == PICTURE_TYPE_P)
174         vpic->luma_ac_qindex     = priv->q_idx_p;
175     else
176         vpic->luma_ac_qindex     = priv->q_idx_b;
177     vpic->luma_dc_qindex_delta   = 0;
178     vpic->chroma_ac_qindex_delta = 0;
179     vpic->chroma_dc_qindex_delta = 0;
180 
181     vpic->filter_level    = priv->loop_filter_level;
182     vpic->sharpness_level = priv->loop_filter_sharpness;
183 
184     return 0;
185 }
186 
vaapi_encode_vp9_get_encoder_caps(AVCodecContext *avctx)187 static av_cold int vaapi_encode_vp9_get_encoder_caps(AVCodecContext *avctx)
188 {
189     VAAPIEncodeContext *ctx = avctx->priv_data;
190 
191     // Surfaces must be aligned to 64x64 superblock boundaries.
192     ctx->surface_width  = FFALIGN(avctx->width,  64);
193     ctx->surface_height = FFALIGN(avctx->height, 64);
194 
195     return 0;
196 }
197 
vaapi_encode_vp9_configure(AVCodecContext *avctx)198 static av_cold int vaapi_encode_vp9_configure(AVCodecContext *avctx)
199 {
200     VAAPIEncodeContext     *ctx = avctx->priv_data;
201     VAAPIEncodeVP9Context *priv = avctx->priv_data;
202 
203     if (ctx->rc_mode->quality) {
204         priv->q_idx_p = av_clip(ctx->rc_quality, 0, VP9_MAX_QUANT);
205         if (avctx->i_quant_factor > 0.0)
206             priv->q_idx_idr =
207                 av_clip((avctx->i_quant_factor * priv->q_idx_p  +
208                          avctx->i_quant_offset) + 0.5,
209                         0, VP9_MAX_QUANT);
210         else
211             priv->q_idx_idr = priv->q_idx_p;
212         if (avctx->b_quant_factor > 0.0)
213             priv->q_idx_b =
214                 av_clip((avctx->b_quant_factor * priv->q_idx_p  +
215                          avctx->b_quant_offset) + 0.5,
216                         0, VP9_MAX_QUANT);
217         else
218             priv->q_idx_b = priv->q_idx_p;
219     } else {
220         // Arbitrary value.
221         priv->q_idx_idr = priv->q_idx_p = priv->q_idx_b = 100;
222     }
223 
224     ctx->roi_quant_range = VP9_MAX_QUANT;
225 
226     return 0;
227 }
228 
229 static const VAAPIEncodeProfile vaapi_encode_vp9_profiles[] = {
230     { FF_PROFILE_VP9_0,  8, 3, 1, 1, VAProfileVP9Profile0 },
231     { FF_PROFILE_VP9_2, 10, 3, 1, 1, VAProfileVP9Profile2 },
232     { FF_PROFILE_UNKNOWN }
233 };
234 
235 static const VAAPIEncodeType vaapi_encode_type_vp9 = {
236     .profiles              = vaapi_encode_vp9_profiles,
237 
238     .flags                 = FLAG_B_PICTURES |
239                              FLAG_B_PICTURE_REFERENCES,
240 
241     .default_quality       = 100,
242 
243     .picture_priv_data_size = sizeof(VAAPIEncodeVP9Picture),
244 
245     .get_encoder_caps      = &vaapi_encode_vp9_get_encoder_caps,
246     .configure             = &vaapi_encode_vp9_configure,
247 
248     .sequence_params_size  = sizeof(VAEncSequenceParameterBufferVP9),
249     .init_sequence_params  = &vaapi_encode_vp9_init_sequence_params,
250 
251     .picture_params_size   = sizeof(VAEncPictureParameterBufferVP9),
252     .init_picture_params   = &vaapi_encode_vp9_init_picture_params,
253 };
254 
vaapi_encode_vp9_init(AVCodecContext *avctx)255 static av_cold int vaapi_encode_vp9_init(AVCodecContext *avctx)
256 {
257     VAAPIEncodeContext *ctx = avctx->priv_data;
258 
259     ctx->codec = &vaapi_encode_type_vp9;
260 
261     // No packed headers are currently desired.  They could be written,
262     // but there isn't any reason to do so - the one usable driver (i965)
263     // can write its own headers and there is no metadata to include.
264     ctx->desired_packed_headers = 0;
265 
266     return ff_vaapi_encode_init(avctx);
267 }
268 
269 #define OFFSET(x) offsetof(VAAPIEncodeVP9Context, x)
270 #define FLAGS (AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM)
271 static const AVOption vaapi_encode_vp9_options[] = {
272     VAAPI_ENCODE_COMMON_OPTIONS,
273     VAAPI_ENCODE_RC_OPTIONS,
274 
275     { "loop_filter_level", "Loop filter level",
276       OFFSET(loop_filter_level), AV_OPT_TYPE_INT, { .i64 = 16 }, 0, 63, FLAGS },
277     { "loop_filter_sharpness", "Loop filter sharpness",
278       OFFSET(loop_filter_sharpness), AV_OPT_TYPE_INT, { .i64 = 4 }, 0, 15, FLAGS },
279     { NULL },
280 };
281 
282 static const FFCodecDefault vaapi_encode_vp9_defaults[] = {
283     { "b",              "0"   },
284     { "bf",             "0"   },
285     { "g",              "250" },
286     { "qmin",           "-1"  },
287     { "qmax",           "-1"  },
288     { NULL },
289 };
290 
291 static const AVClass vaapi_encode_vp9_class = {
292     .class_name = "vp9_vaapi",
293     .item_name  = av_default_item_name,
294     .option     = vaapi_encode_vp9_options,
295     .version    = LIBAVUTIL_VERSION_INT,
296 };
297 
298 const FFCodec ff_vp9_vaapi_encoder = {
299     .p.name         = "vp9_vaapi",
300     .p.long_name    = NULL_IF_CONFIG_SMALL("VP9 (VAAPI)"),
301     .p.type         = AVMEDIA_TYPE_VIDEO,
302     .p.id           = AV_CODEC_ID_VP9,
303     .priv_data_size = sizeof(VAAPIEncodeVP9Context),
304     .init           = &vaapi_encode_vp9_init,
305     FF_CODEC_RECEIVE_PACKET_CB(&ff_vaapi_encode_receive_packet),
306     .close          = &ff_vaapi_encode_close,
307     .p.priv_class   = &vaapi_encode_vp9_class,
308     .p.capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_HARDWARE |
309                       AV_CODEC_CAP_DR1,
310     .caps_internal  = FF_CODEC_CAP_INIT_CLEANUP,
311     .defaults       = vaapi_encode_vp9_defaults,
312     .p.pix_fmts = (const enum AVPixelFormat[]) {
313         AV_PIX_FMT_VAAPI,
314         AV_PIX_FMT_NONE,
315     },
316     .hw_configs     = ff_vaapi_encode_hw_configs,
317     .p.wrapper_name = "vaapi",
318 };
319