qsvenc_hevc: use the correct HW plugin UID
[libav.git] / libavcodec / qsvenc_hevc.c
1 /*
2 * Intel MediaSDK QSV based HEVC encoder
3 *
4 * This file is part of Libav.
5 *
6 * Libav 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 * Libav 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 Libav; 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 "bytestream.h"
32 #include "get_bits.h"
33 #include "hevc.h"
34 #include "internal.h"
35 #include "qsv.h"
36 #include "qsv_internal.h"
37 #include "qsvenc.h"
38
39 enum {
40 LOAD_PLUGIN_NONE,
41 LOAD_PLUGIN_HEVC_SW,
42 LOAD_PLUGIN_HEVC_HW,
43 } LoadPlugin;
44
45 typedef struct QSVHEVCEncContext {
46 AVClass *class;
47 QSVEncContext qsv;
48 int load_plugin;
49 } QSVHEVCEncContext;
50
51 static int generate_fake_vps(QSVEncContext *q, AVCodecContext *avctx)
52 {
53 GetByteContext gbc;
54 PutByteContext pbc;
55
56 GetBitContext gb;
57 HEVCNAL sps_nal = { NULL };
58 HEVCSPS sps = { 0 };
59 HEVCVPS vps = { 0 };
60 uint8_t vps_buf[128], vps_rbsp_buf[128];
61 uint8_t *new_extradata;
62 unsigned int sps_id;
63 int ret, i, type, vps_size;
64
65 if (!avctx->extradata_size) {
66 av_log(avctx, AV_LOG_ERROR, "No extradata returned from libmfx\n");
67 return AVERROR_UNKNOWN;
68 }
69
70 /* parse the SPS */
71 ret = ff_hevc_extract_rbsp(avctx->extradata + 4, avctx->extradata_size - 4, &sps_nal);
72 if (ret < 0) {
73 av_log(avctx, AV_LOG_ERROR, "Error unescaping the SPS buffer\n");
74 return ret;
75 }
76
77 ret = init_get_bits8(&gb, sps_nal.data, sps_nal.size);
78 if (ret < 0) {
79 av_freep(&sps_nal.rbsp_buffer);
80 return ret;
81 }
82
83 get_bits(&gb, 1);
84 type = get_bits(&gb, 6);
85 if (type != NAL_SPS) {
86 av_log(avctx, AV_LOG_ERROR, "Unexpected NAL type in the extradata: %d\n",
87 type);
88 av_freep(&sps_nal.rbsp_buffer);
89 return AVERROR_INVALIDDATA;
90 }
91 get_bits(&gb, 9);
92
93 ret = ff_hevc_parse_sps(&sps, &gb, &sps_id, 0, NULL, avctx);
94 av_freep(&sps_nal.rbsp_buffer);
95 if (ret < 0) {
96 av_log(avctx, AV_LOG_ERROR, "Error parsing the SPS\n");
97 return ret;
98 }
99
100 /* generate the VPS */
101 vps.vps_max_layers = 1;
102 vps.vps_max_sub_layers = sps.max_sub_layers;
103 memcpy(&vps.ptl, &sps.ptl, sizeof(vps.ptl));
104 vps.vps_sub_layer_ordering_info_present_flag = 1;
105 for (i = 0; i < MAX_SUB_LAYERS; i++) {
106 vps.vps_max_dec_pic_buffering[i] = sps.temporal_layer[i].max_dec_pic_buffering;
107 vps.vps_num_reorder_pics[i] = sps.temporal_layer[i].num_reorder_pics;
108 vps.vps_max_latency_increase[i] = sps.temporal_layer[i].max_latency_increase;
109 }
110
111 vps.vps_num_layer_sets = 1;
112 vps.vps_timing_info_present_flag = sps.vui.vui_timing_info_present_flag;
113 vps.vps_num_units_in_tick = sps.vui.vui_num_units_in_tick;
114 vps.vps_time_scale = sps.vui.vui_time_scale;
115 vps.vps_poc_proportional_to_timing_flag = sps.vui.vui_poc_proportional_to_timing_flag;
116 vps.vps_num_ticks_poc_diff_one = sps.vui.vui_num_ticks_poc_diff_one_minus1 + 1;
117
118 /* generate the encoded RBSP form of the VPS */
119 ret = ff_hevc_encode_nal_vps(&vps, sps.vps_id, vps_rbsp_buf, sizeof(vps_rbsp_buf));
120 if (ret < 0) {
121 av_log(avctx, AV_LOG_ERROR, "Error writing the VPS\n");
122 return ret;
123 }
124
125 /* escape and add the startcode */
126 bytestream2_init(&gbc, vps_rbsp_buf, ret);
127 bytestream2_init_writer(&pbc, vps_buf, sizeof(vps_buf));
128
129 bytestream2_put_be32(&pbc, 1); // startcode
130 bytestream2_put_byte(&pbc, NAL_VPS << 1); // NAL
131 bytestream2_put_byte(&pbc, 1); // header
132
133 while (bytestream2_get_bytes_left(&gbc)) {
134 uint32_t b = bytestream2_peek_be24(&gbc);
135 if (b <= 3) {
136 bytestream2_put_be24(&pbc, 3);
137 bytestream2_skip(&gbc, 2);
138 } else
139 bytestream2_put_byte(&pbc, bytestream2_get_byte(&gbc));
140 }
141
142 vps_size = bytestream2_tell_p(&pbc);
143 new_extradata = av_mallocz(vps_size + avctx->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
144 if (!new_extradata)
145 return AVERROR(ENOMEM);
146 memcpy(new_extradata, vps_buf, vps_size);
147 memcpy(new_extradata + vps_size, avctx->extradata, avctx->extradata_size);
148
149 av_freep(&avctx->extradata);
150 avctx->extradata = new_extradata;
151 avctx->extradata_size += vps_size;
152
153 return 0;
154 }
155
156 static av_cold int qsv_enc_init(AVCodecContext *avctx)
157 {
158 QSVHEVCEncContext *q = avctx->priv_data;
159 int ret;
160
161 if (q->load_plugin != LOAD_PLUGIN_NONE) {
162 static const char *uid_hevcenc_sw = "2fca99749fdb49aeb121a5b63ef568f7";
163 static const char *uid_hevcenc_hw = "6fadc791a0c2eb479ab6dcd5ea9da347";
164
165 if (q->qsv.load_plugins[0]) {
166 av_log(avctx, AV_LOG_WARNING,
167 "load_plugins is not empty, but load_plugin is not set to 'none'."
168 "The load_plugin value will be ignored.\n");
169 } else {
170 av_freep(&q->qsv.load_plugins);
171
172 if (q->load_plugin == LOAD_PLUGIN_HEVC_SW)
173 q->qsv.load_plugins = av_strdup(uid_hevcenc_sw);
174 else
175 q->qsv.load_plugins = av_strdup(uid_hevcenc_hw);
176
177 if (!q->qsv.load_plugins)
178 return AVERROR(ENOMEM);
179 }
180 }
181
182 ret = ff_qsv_enc_init(avctx, &q->qsv);
183 if (ret < 0)
184 return ret;
185
186 ret = generate_fake_vps(&q->qsv, avctx);
187 if (ret < 0) {
188 ff_qsv_enc_close(avctx, &q->qsv);
189 return ret;
190 }
191
192 return 0;
193 }
194
195 static int qsv_enc_frame(AVCodecContext *avctx, AVPacket *pkt,
196 const AVFrame *frame, int *got_packet)
197 {
198 QSVHEVCEncContext *q = avctx->priv_data;
199
200 return ff_qsv_encode(avctx, &q->qsv, pkt, frame, got_packet);
201 }
202
203 static av_cold int qsv_enc_close(AVCodecContext *avctx)
204 {
205 QSVHEVCEncContext *q = avctx->priv_data;
206
207 return ff_qsv_enc_close(avctx, &q->qsv);
208 }
209
210 #define OFFSET(x) offsetof(QSVHEVCEncContext, x)
211 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
212 static const AVOption options[] = {
213 { "async_depth", "Maximum processing parallelism", OFFSET(qsv.async_depth), AV_OPT_TYPE_INT, { .i64 = ASYNC_DEPTH_DEFAULT }, 0, INT_MAX, VE },
214 { "avbr_accuracy", "Accuracy of the AVBR ratecontrol", OFFSET(qsv.avbr_accuracy), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, VE },
215 { "avbr_convergence", "Convergence of the AVBR ratecontrol", OFFSET(qsv.avbr_convergence), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, VE },
216
217 { "load_plugin", "A user plugin to load in an internal session", OFFSET(load_plugin), AV_OPT_TYPE_INT, { .i64 = LOAD_PLUGIN_HEVC_SW }, LOAD_PLUGIN_NONE, LOAD_PLUGIN_HEVC_HW, VE, "load_plugin" },
218 { "none", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = LOAD_PLUGIN_NONE }, 0, 0, VE, "load_plugin" },
219 { "hevc_sw", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = LOAD_PLUGIN_HEVC_SW }, 0, 0, VE, "load_plugin" },
220 { "hevc_hw", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = LOAD_PLUGIN_HEVC_HW }, 0, 0, VE, "load_plugin" },
221
222 { "load_plugins", "A :-separate list of hexadecimal plugin UIDs to load in an internal session",
223 OFFSET(qsv.load_plugins), AV_OPT_TYPE_STRING, { .str = "" }, 0, 0, VE },
224
225 { "profile", NULL, OFFSET(qsv.profile), AV_OPT_TYPE_INT, { .i64 = MFX_PROFILE_UNKNOWN }, 0, INT_MAX, VE, "profile" },
226 { "unknown", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = MFX_PROFILE_UNKNOWN }, INT_MIN, INT_MAX, VE, "profile" },
227 { "main", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = MFX_PROFILE_HEVC_MAIN }, INT_MIN, INT_MAX, VE, "profile" },
228 { "main10", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = MFX_PROFILE_HEVC_MAIN10 }, INT_MIN, INT_MAX, VE, "profile" },
229 { "mainsp", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = MFX_PROFILE_HEVC_MAINSP }, INT_MIN, INT_MAX, VE, "profile" },
230
231 { "preset", NULL, OFFSET(qsv.preset), AV_OPT_TYPE_INT, { .i64 = MFX_TARGETUSAGE_BALANCED }, 0, 7, VE, "preset" },
232 { "fast", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = MFX_TARGETUSAGE_BEST_SPEED }, INT_MIN, INT_MAX, VE, "preset" },
233 { "medium", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = MFX_TARGETUSAGE_BALANCED }, INT_MIN, INT_MAX, VE, "preset" },
234 { "slow", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = MFX_TARGETUSAGE_BEST_QUALITY }, INT_MIN, INT_MAX, VE, "preset" },
235
236 { NULL },
237 };
238
239 static const AVClass class = {
240 .class_name = "hevc_qsv encoder",
241 .item_name = av_default_item_name,
242 .option = options,
243 .version = LIBAVUTIL_VERSION_INT,
244 };
245
246 static const AVCodecDefault qsv_enc_defaults[] = {
247 { "b", "1M" },
248 { "refs", "0" },
249 // same as the x264 default
250 { "g", "250" },
251 { "bf", "3" },
252
253 { "flags", "+cgop" },
254 { NULL },
255 };
256
257 AVCodec ff_hevc_qsv_encoder = {
258 .name = "hevc_qsv",
259 .long_name = NULL_IF_CONFIG_SMALL("HEVC (Intel Quick Sync Video acceleration)"),
260 .priv_data_size = sizeof(QSVHEVCEncContext),
261 .type = AVMEDIA_TYPE_VIDEO,
262 .id = AV_CODEC_ID_HEVC,
263 .init = qsv_enc_init,
264 .encode2 = qsv_enc_frame,
265 .close = qsv_enc_close,
266 .capabilities = CODEC_CAP_DELAY,
267 .pix_fmts = (const enum AVPixelFormat[]){ AV_PIX_FMT_NV12,
268 AV_PIX_FMT_QSV,
269 AV_PIX_FMT_NONE },
270 .priv_class = &class,
271 .defaults = qsv_enc_defaults,
272 };