2 * Intel MediaSDK QSV based HEVC encoder
4 * This file is part of FFmpeg.
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.
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.
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
23 #include <sys/types.h>
25 #include <mfx/mfxvideo.h>
27 #include "libavutil/common.h"
28 #include "libavutil/opt.h"
31 #include "bytestream.h"
35 #include "h2645_parse.h"
38 #include "qsv_internal.h"
47 typedef struct QSVHEVCEncContext {
53 static int generate_fake_vps(QSVEncContext *q, AVCodecContext *avctx)
59 H2645RBSP sps_rbsp = { NULL };
60 H2645NAL sps_nal = { NULL };
63 uint8_t vps_buf[128], vps_rbsp_buf[128];
64 uint8_t *new_extradata;
66 int ret, i, type, vps_size;
68 if (!avctx->extradata_size) {
69 av_log(avctx, AV_LOG_ERROR, "No extradata returned from libmfx\n");
70 return AVERROR_UNKNOWN;
73 av_fast_padded_malloc(&sps_rbsp.rbsp_buffer, &sps_rbsp.rbsp_buffer_alloc_size, avctx->extradata_size);
74 if (!sps_rbsp.rbsp_buffer)
75 return AVERROR(ENOMEM);
78 ret = ff_h2645_extract_rbsp(avctx->extradata + 4, avctx->extradata_size - 4, &sps_rbsp, &sps_nal, 1);
80 av_log(avctx, AV_LOG_ERROR, "Error unescaping the SPS buffer\n");
84 ret = init_get_bits8(&gb, sps_nal.data, sps_nal.size);
86 av_freep(&sps_rbsp.rbsp_buffer);
91 type = get_bits(&gb, 6);
92 if (type != HEVC_NAL_SPS) {
93 av_log(avctx, AV_LOG_ERROR, "Unexpected NAL type in the extradata: %d\n",
95 av_freep(&sps_rbsp.rbsp_buffer);
96 return AVERROR_INVALIDDATA;
100 ret = ff_hevc_parse_sps(&sps, &gb, &sps_id, 0, NULL, avctx);
101 av_freep(&sps_rbsp.rbsp_buffer);
103 av_log(avctx, AV_LOG_ERROR, "Error parsing the SPS\n");
107 /* generate the VPS */
108 vps.vps_max_layers = 1;
109 vps.vps_max_sub_layers = sps.max_sub_layers;
110 memcpy(&vps.ptl, &sps.ptl, sizeof(vps.ptl));
111 vps.vps_sub_layer_ordering_info_present_flag = 1;
112 for (i = 0; i < HEVC_MAX_SUB_LAYERS; i++) {
113 vps.vps_max_dec_pic_buffering[i] = sps.temporal_layer[i].max_dec_pic_buffering;
114 vps.vps_num_reorder_pics[i] = sps.temporal_layer[i].num_reorder_pics;
115 vps.vps_max_latency_increase[i] = sps.temporal_layer[i].max_latency_increase;
118 vps.vps_num_layer_sets = 1;
119 vps.vps_timing_info_present_flag = sps.vui.vui_timing_info_present_flag;
120 vps.vps_num_units_in_tick = sps.vui.vui_num_units_in_tick;
121 vps.vps_time_scale = sps.vui.vui_time_scale;
122 vps.vps_poc_proportional_to_timing_flag = sps.vui.vui_poc_proportional_to_timing_flag;
123 vps.vps_num_ticks_poc_diff_one = sps.vui.vui_num_ticks_poc_diff_one_minus1 + 1;
125 /* generate the encoded RBSP form of the VPS */
126 ret = ff_hevc_encode_nal_vps(&vps, sps.vps_id, vps_rbsp_buf, sizeof(vps_rbsp_buf));
128 av_log(avctx, AV_LOG_ERROR, "Error writing the VPS\n");
132 /* escape and add the startcode */
133 bytestream2_init(&gbc, vps_rbsp_buf, ret);
134 bytestream2_init_writer(&pbc, vps_buf, sizeof(vps_buf));
136 bytestream2_put_be32(&pbc, 1); // startcode
137 bytestream2_put_byte(&pbc, HEVC_NAL_VPS << 1); // NAL
138 bytestream2_put_byte(&pbc, 1); // header
140 while (bytestream2_get_bytes_left(&gbc)) {
141 uint32_t b = bytestream2_peek_be24(&gbc);
143 bytestream2_put_be24(&pbc, 3);
144 bytestream2_skip(&gbc, 2);
146 bytestream2_put_byte(&pbc, bytestream2_get_byte(&gbc));
149 vps_size = bytestream2_tell_p(&pbc);
150 new_extradata = av_mallocz(vps_size + avctx->extradata_size + AV_INPUT_BUFFER_PADDING_SIZE);
152 return AVERROR(ENOMEM);
153 memcpy(new_extradata, vps_buf, vps_size);
154 memcpy(new_extradata + vps_size, avctx->extradata, avctx->extradata_size);
156 av_freep(&avctx->extradata);
157 avctx->extradata = new_extradata;
158 avctx->extradata_size += vps_size;
163 static av_cold int qsv_enc_init(AVCodecContext *avctx)
165 QSVHEVCEncContext *q = avctx->priv_data;
168 if (q->load_plugin != LOAD_PLUGIN_NONE) {
169 static const char * const uid_hevcenc_sw = "2fca99749fdb49aeb121a5b63ef568f7";
170 static const char * const uid_hevcenc_hw = "6fadc791a0c2eb479ab6dcd5ea9da347";
172 if (q->qsv.load_plugins[0]) {
173 av_log(avctx, AV_LOG_WARNING,
174 "load_plugins is not empty, but load_plugin is not set to 'none'."
175 "The load_plugin value will be ignored.\n");
177 av_freep(&q->qsv.load_plugins);
179 if (q->load_plugin == LOAD_PLUGIN_HEVC_SW)
180 q->qsv.load_plugins = av_strdup(uid_hevcenc_sw);
182 q->qsv.load_plugins = av_strdup(uid_hevcenc_hw);
184 if (!q->qsv.load_plugins)
185 return AVERROR(ENOMEM);
189 ret = ff_qsv_enc_init(avctx, &q->qsv);
193 ret = generate_fake_vps(&q->qsv, avctx);
195 ff_qsv_enc_close(avctx, &q->qsv);
202 static int qsv_enc_frame(AVCodecContext *avctx, AVPacket *pkt,
203 const AVFrame *frame, int *got_packet)
205 QSVHEVCEncContext *q = avctx->priv_data;
207 return ff_qsv_encode(avctx, &q->qsv, pkt, frame, got_packet);
210 static av_cold int qsv_enc_close(AVCodecContext *avctx)
212 QSVHEVCEncContext *q = avctx->priv_data;
214 return ff_qsv_enc_close(avctx, &q->qsv);
218 #define LOAD_PLUGIN_DEFAULT LOAD_PLUGIN_HEVC_SW
220 #define LOAD_PLUGIN_DEFAULT LOAD_PLUGIN_HEVC_HW
223 #define OFFSET(x) offsetof(QSVHEVCEncContext, x)
224 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
225 static const AVOption options[] = {
228 { "load_plugin", "A user plugin to load in an internal session", OFFSET(load_plugin), AV_OPT_TYPE_INT, { .i64 = LOAD_PLUGIN_DEFAULT }, LOAD_PLUGIN_NONE, LOAD_PLUGIN_HEVC_HW, VE, "load_plugin" },
229 { "none", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = LOAD_PLUGIN_NONE }, 0, 0, VE, "load_plugin" },
230 { "hevc_sw", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = LOAD_PLUGIN_HEVC_SW }, 0, 0, VE, "load_plugin" },
231 { "hevc_hw", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = LOAD_PLUGIN_HEVC_HW }, 0, 0, VE, "load_plugin" },
233 { "load_plugins", "A :-separate list of hexadecimal plugin UIDs to load in an internal session",
234 OFFSET(qsv.load_plugins), AV_OPT_TYPE_STRING, { .str = "" }, 0, 0, VE },
236 { "profile", NULL, OFFSET(qsv.profile), AV_OPT_TYPE_INT, { .i64 = MFX_PROFILE_UNKNOWN }, 0, INT_MAX, VE, "profile" },
237 { "unknown", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = MFX_PROFILE_UNKNOWN }, INT_MIN, INT_MAX, VE, "profile" },
238 { "main", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = MFX_PROFILE_HEVC_MAIN }, INT_MIN, INT_MAX, VE, "profile" },
239 { "main10", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = MFX_PROFILE_HEVC_MAIN10 }, INT_MIN, INT_MAX, VE, "profile" },
240 { "mainsp", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = MFX_PROFILE_HEVC_MAINSP }, INT_MIN, INT_MAX, VE, "profile" },
245 static const AVClass class = {
246 .class_name = "hevc_qsv encoder",
247 .item_name = av_default_item_name,
249 .version = LIBAVUTIL_VERSION_INT,
252 static const AVCodecDefault qsv_enc_defaults[] = {
255 // same as the x264 default
259 { "flags", "+cgop" },
260 #if FF_API_PRIVATE_OPT
261 { "b_strategy", "-1" },
266 AVCodec ff_hevc_qsv_encoder = {
268 .long_name = NULL_IF_CONFIG_SMALL("HEVC (Intel Quick Sync Video acceleration)"),
269 .priv_data_size = sizeof(QSVHEVCEncContext),
270 .type = AVMEDIA_TYPE_VIDEO,
271 .id = AV_CODEC_ID_HEVC,
272 .init = qsv_enc_init,
273 .encode2 = qsv_enc_frame,
274 .close = qsv_enc_close,
275 .capabilities = AV_CODEC_CAP_DELAY,
276 .pix_fmts = (const enum AVPixelFormat[]){ AV_PIX_FMT_NV12,
280 .priv_class = &class,
281 .defaults = qsv_enc_defaults,
282 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,