1 /*
2  * Intel MediaSDK QSV based VP9 encoder
3  *
4  * This file is part of FFmpeg.
5  *
6  * FFmpeg is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * FFmpeg is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with FFmpeg; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 
21 
22 #include <stdint.h>
23 #include <sys/types.h>
24 
25 #include <mfx/mfxvideo.h>
26 
27 #include "libavutil/common.h"
28 #include "libavutil/opt.h"
29 
30 #include "avcodec.h"
31 #include "codec_internal.h"
32 #include "qsv.h"
33 #include "qsv_internal.h"
34 #include "qsvenc.h"
35 
36 typedef struct QSVVP9EncContext {
37     AVClass *class;
38     QSVEncContext qsv;
39 } QSVVP9EncContext;
40 
qsv_enc_init(AVCodecContext *avctx)41 static av_cold int qsv_enc_init(AVCodecContext *avctx)
42 {
43     QSVVP9EncContext *q = avctx->priv_data;
44     q->qsv.low_power = 1;
45 
46     return ff_qsv_enc_init(avctx, &q->qsv);
47 }
48 
qsv_enc_frame(AVCodecContext *avctx, AVPacket *pkt, const AVFrame *frame, int *got_packet)49 static int qsv_enc_frame(AVCodecContext *avctx, AVPacket *pkt,
50                          const AVFrame *frame, int *got_packet)
51 {
52     QSVVP9EncContext *q = avctx->priv_data;
53 
54     return ff_qsv_encode(avctx, &q->qsv, pkt, frame, got_packet);
55 }
56 
qsv_enc_close(AVCodecContext *avctx)57 static av_cold int qsv_enc_close(AVCodecContext *avctx)
58 {
59     QSVVP9EncContext *q = avctx->priv_data;
60 
61     return ff_qsv_enc_close(avctx, &q->qsv);
62 }
63 
64 #define OFFSET(x) offsetof(QSVVP9EncContext, x)
65 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
66 static const AVOption options[] = {
67     QSV_COMMON_OPTS
68 
69     { "profile",   NULL, OFFSET(qsv.profile), AV_OPT_TYPE_INT,   { .i64 = MFX_PROFILE_UNKNOWN },   0,       INT_MAX,  VE,  "profile" },
70     { "unknown",   NULL, 0,                   AV_OPT_TYPE_CONST, { .i64 = MFX_PROFILE_UNKNOWN},   INT_MIN,  INT_MAX,  VE,  "profile" },
71     { "profile0",  NULL, 0,                   AV_OPT_TYPE_CONST, { .i64 = MFX_PROFILE_VP9_0   },  INT_MIN,  INT_MAX,  VE,  "profile" },
72     { "profile1",  NULL, 0,                   AV_OPT_TYPE_CONST, { .i64 = MFX_PROFILE_VP9_1   },  INT_MIN,  INT_MAX,  VE,  "profile" },
73     { "profile2",  NULL, 0,                   AV_OPT_TYPE_CONST, { .i64 = MFX_PROFILE_VP9_2   },  INT_MIN,  INT_MAX,  VE,  "profile" },
74     { "profile3",  NULL, 0,                   AV_OPT_TYPE_CONST, { .i64 = MFX_PROFILE_VP9_3   },  INT_MIN,  INT_MAX,  VE,  "profile" },
75 
76 #if QSV_HAVE_EXT_VP9_TILES
77     /* The minimum tile width in luma pixels is 256, set maximum tile_cols to 32 for 8K video */
78     { "tile_cols",  "Number of columns for tiled encoding",   OFFSET(qsv.tile_cols),    AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 32, VE },
79     /* Set maximum tile_rows to 4 per VP9 spec */
80     { "tile_rows",  "Number of rows for tiled encoding",      OFFSET(qsv.tile_rows),    AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 4, VE },
81 #else
82     { "tile_cols",  "(not supported)",                        OFFSET(qsv.tile_cols),    AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 0, VE },
83     { "tile_rows",  "(not supported)",                        OFFSET(qsv.tile_rows),    AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 0, VE },
84 #endif
85 
86     { NULL },
87 };
88 
89 static const AVClass class = {
90     .class_name = "vp9_qsv encoder",
91     .item_name  = av_default_item_name,
92     .option     = options,
93     .version    = LIBAVUTIL_VERSION_INT,
94 };
95 
96 static const FFCodecDefault qsv_enc_defaults[] = {
97     { "b",         "1M"    },
98     { "refs",      "0"     },
99     { "g",         "250"   },
100     { "trellis",   "-1"    },
101     { "flags",     "+cgop" },
102     { NULL },
103 };
104 
105 const FFCodec ff_vp9_qsv_encoder = {
106     .p.name         = "vp9_qsv",
107     .p.long_name    = NULL_IF_CONFIG_SMALL("VP9 video (Intel Quick Sync Video acceleration)"),
108     .priv_data_size = sizeof(QSVVP9EncContext),
109     .p.type         = AVMEDIA_TYPE_VIDEO,
110     .p.id           = AV_CODEC_ID_VP9,
111     .init           = qsv_enc_init,
112     FF_CODEC_ENCODE_CB(qsv_enc_frame),
113     .close          = qsv_enc_close,
114     .p.capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_HYBRID,
115     .p.pix_fmts     = (const enum AVPixelFormat[]){ AV_PIX_FMT_NV12,
116                                                     AV_PIX_FMT_P010,
117                                                     AV_PIX_FMT_QSV,
118                                                     AV_PIX_FMT_NONE },
119     .p.priv_class   = &class,
120     .defaults       = qsv_enc_defaults,
121     .caps_internal  = FF_CODEC_CAP_INIT_CLEANUP,
122     .p.wrapper_name = "qsv",
123     .hw_configs     = ff_qsv_enc_hw_configs,
124 };
125