FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
qsvenc_hevc.c
Go to the documentation of this file.
1 /*
2  * Intel MediaSDK QSV based HEVC 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 "bytestream.h"
32 #include "get_bits.h"
33 #include "hevc.h"
34 #include "hevcdec.h"
35 #include "h2645_parse.h"
36 #include "internal.h"
37 #include "qsv.h"
38 #include "qsv_internal.h"
39 #include "qsvenc.h"
40 
41 enum LoadPlugin {
45 };
46 
47 typedef struct QSVHEVCEncContext {
48  AVClass *class;
52 
54 {
55  GetByteContext gbc;
56  PutByteContext pbc;
57 
58  GetBitContext gb;
59  H2645NAL sps_nal = { NULL };
60  HEVCSPS sps = { 0 };
61  HEVCVPS vps = { 0 };
62  uint8_t vps_buf[128], vps_rbsp_buf[128];
63  uint8_t *new_extradata;
64  unsigned int sps_id;
65  int ret, i, type, vps_size;
66 
67  if (!avctx->extradata_size) {
68  av_log(avctx, AV_LOG_ERROR, "No extradata returned from libmfx\n");
69  return AVERROR_UNKNOWN;
70  }
71 
72  /* parse the SPS */
73  ret = ff_h2645_extract_rbsp(avctx->extradata + 4, avctx->extradata_size - 4, &sps_nal, 1);
74  if (ret < 0) {
75  av_log(avctx, AV_LOG_ERROR, "Error unescaping the SPS buffer\n");
76  return ret;
77  }
78 
79  ret = init_get_bits8(&gb, sps_nal.data, sps_nal.size);
80  if (ret < 0) {
81  av_freep(&sps_nal.rbsp_buffer);
82  return ret;
83  }
84 
85  get_bits(&gb, 1);
86  type = get_bits(&gb, 6);
87  if (type != HEVC_NAL_SPS) {
88  av_log(avctx, AV_LOG_ERROR, "Unexpected NAL type in the extradata: %d\n",
89  type);
90  av_freep(&sps_nal.rbsp_buffer);
91  return AVERROR_INVALIDDATA;
92  }
93  get_bits(&gb, 9);
94 
95  ret = ff_hevc_parse_sps(&sps, &gb, &sps_id, 0, NULL, avctx);
96  av_freep(&sps_nal.rbsp_buffer);
97  if (ret < 0) {
98  av_log(avctx, AV_LOG_ERROR, "Error parsing the SPS\n");
99  return ret;
100  }
101 
102  /* generate the VPS */
103  vps.vps_max_layers = 1;
105  memcpy(&vps.ptl, &sps.ptl, sizeof(vps.ptl));
107  for (i = 0; i < HEVC_MAX_SUB_LAYERS; i++) {
111  }
112 
113  vps.vps_num_layer_sets = 1;
119 
120  /* generate the encoded RBSP form of the VPS */
121  ret = ff_hevc_encode_nal_vps(&vps, sps.vps_id, vps_rbsp_buf, sizeof(vps_rbsp_buf));
122  if (ret < 0) {
123  av_log(avctx, AV_LOG_ERROR, "Error writing the VPS\n");
124  return ret;
125  }
126 
127  /* escape and add the startcode */
128  bytestream2_init(&gbc, vps_rbsp_buf, ret);
129  bytestream2_init_writer(&pbc, vps_buf, sizeof(vps_buf));
130 
131  bytestream2_put_be32(&pbc, 1); // startcode
132  bytestream2_put_byte(&pbc, HEVC_NAL_VPS << 1); // NAL
133  bytestream2_put_byte(&pbc, 1); // header
134 
135  while (bytestream2_get_bytes_left(&gbc)) {
136  uint32_t b = bytestream2_peek_be24(&gbc);
137  if (b <= 3) {
138  bytestream2_put_be24(&pbc, 3);
139  bytestream2_skip(&gbc, 2);
140  } else
141  bytestream2_put_byte(&pbc, bytestream2_get_byte(&gbc));
142  }
143 
144  vps_size = bytestream2_tell_p(&pbc);
145  new_extradata = av_mallocz(vps_size + avctx->extradata_size + AV_INPUT_BUFFER_PADDING_SIZE);
146  if (!new_extradata)
147  return AVERROR(ENOMEM);
148  memcpy(new_extradata, vps_buf, vps_size);
149  memcpy(new_extradata + vps_size, avctx->extradata, avctx->extradata_size);
150 
151  av_freep(&avctx->extradata);
152  avctx->extradata = new_extradata;
153  avctx->extradata_size += vps_size;
154 
155  return 0;
156 }
157 
159 {
160  QSVHEVCEncContext *q = avctx->priv_data;
161  int ret;
162 
163  if (q->load_plugin != LOAD_PLUGIN_NONE) {
164  static const char *uid_hevcenc_sw = "2fca99749fdb49aeb121a5b63ef568f7";
165  static const char *uid_hevcenc_hw = "6fadc791a0c2eb479ab6dcd5ea9da347";
166 
167  if (q->qsv.load_plugins[0]) {
168  av_log(avctx, AV_LOG_WARNING,
169  "load_plugins is not empty, but load_plugin is not set to 'none'."
170  "The load_plugin value will be ignored.\n");
171  } else {
173 
175  q->qsv.load_plugins = av_strdup(uid_hevcenc_sw);
176  else
177  q->qsv.load_plugins = av_strdup(uid_hevcenc_hw);
178 
179  if (!q->qsv.load_plugins)
180  return AVERROR(ENOMEM);
181  }
182  }
183 
184  ret = ff_qsv_enc_init(avctx, &q->qsv);
185  if (ret < 0)
186  return ret;
187 
188  ret = generate_fake_vps(&q->qsv, avctx);
189  if (ret < 0) {
190  ff_qsv_enc_close(avctx, &q->qsv);
191  return ret;
192  }
193 
194  return 0;
195 }
196 
198  const AVFrame *frame, int *got_packet)
199 {
200  QSVHEVCEncContext *q = avctx->priv_data;
201 
202  return ff_qsv_encode(avctx, &q->qsv, pkt, frame, got_packet);
203 }
204 
206 {
207  QSVHEVCEncContext *q = avctx->priv_data;
208 
209  return ff_qsv_enc_close(avctx, &q->qsv);
210 }
211 
212 #define OFFSET(x) offsetof(QSVHEVCEncContext, x)
213 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
214 static const AVOption options[] = {
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  { NULL },
232 };
233 
234 static const AVClass class = {
235  .class_name = "hevc_qsv encoder",
236  .item_name = av_default_item_name,
237  .option = options,
239 };
240 
242  { "b", "1M" },
243  { "refs", "0" },
244  // same as the x264 default
245  { "g", "248" },
246  { "bf", "8" },
247 
248  { "flags", "+cgop" },
249 #if FF_API_PRIVATE_OPT
250  { "b_strategy", "-1" },
251 #endif
252  { NULL },
253 };
254 
256  .name = "hevc_qsv",
257  .long_name = NULL_IF_CONFIG_SMALL("HEVC (Intel Quick Sync Video acceleration)"),
258  .priv_data_size = sizeof(QSVHEVCEncContext),
260  .id = AV_CODEC_ID_HEVC,
261  .init = qsv_enc_init,
262  .encode2 = qsv_enc_frame,
263  .close = qsv_enc_close,
264  .capabilities = AV_CODEC_CAP_DELAY,
265  .pix_fmts = (const enum AVPixelFormat[]){ AV_PIX_FMT_NV12,
268  AV_PIX_FMT_NONE },
269  .priv_class = &class,
270  .defaults = qsv_enc_defaults,
271  .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
272 };
#define FF_CODEC_CAP_INIT_CLEANUP
The codec allows calling the close function for deallocation even if the init function returned a fai...
Definition: internal.h:48
#define NULL
Definition: coverity.c:32
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
AVCodec ff_hevc_qsv_encoder
Definition: qsvenc_hevc.c:255
This structure describes decoded (raw) audio or video data.
Definition: frame.h:187
AVOption.
Definition: opt.h:246
int size
Definition: h2645_parse.h:35
int vui_num_ticks_poc_diff_one_minus1
Definition: hevc_ps.h:78
int max_dec_pic_buffering
Definition: hevc_ps.h:162
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:261
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:182
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
int vps_num_ticks_poc_diff_one
vps_num_ticks_poc_diff_one_minus1 + 1
Definition: hevc_ps.h:128
unsigned int vps_max_latency_increase[HEVC_MAX_SUB_LAYERS]
Definition: hevc_ps.h:121
VUI vui
Definition: hevc_ps.h:167
const char * b
Definition: vf_curves.c:113
static av_always_inline void bytestream2_init_writer(PutByteContext *p, uint8_t *buf, int buf_size)
Definition: bytestream.h:143
uint32_t vui_time_scale
Definition: hevc_ps.h:76
int ff_hevc_encode_nal_vps(HEVCVPS *vps, unsigned int id, uint8_t *buf, int buf_size)
Definition: hevc_ps_enc.c:66
static int generate_fake_vps(QSVEncContext *q, AVCodecContext *avctx)
Definition: qsvenc_hevc.c:53
static av_always_inline void bytestream2_init(GetByteContext *g, const uint8_t *buf, int buf_size)
Definition: bytestream.h:133
unsigned int vps_max_dec_pic_buffering[HEVC_MAX_SUB_LAYERS]
Definition: hevc_ps.h:119
int max_latency_increase
Definition: hevc_ps.h:164
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
Definition: mem.c:222
static AVPacket pkt
AVCodec.
Definition: avcodec.h:3681
static int qsv_enc_frame(AVCodecContext *avctx, AVPacket *pkt, const AVFrame *frame, int *got_packet)
Definition: qsvenc_hevc.c:197
#define AV_PIX_FMT_P010
Definition: pixfmt.h:394
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
Definition: log.h:72
#define AV_CODEC_CAP_DELAY
Encoder or decoder requires flushing with NULL input at the end in order to give the complete and cor...
Definition: avcodec.h:1019
int ff_qsv_encode(AVCodecContext *avctx, QSVEncContext *q, AVPacket *pkt, const AVFrame *frame, int *got_packet)
Definition: qsvenc.c:1032
uint8_t
#define av_cold
Definition: attributes.h:82
AVOptions.
uint8_t vps_timing_info_present_flag
Definition: hevc_ps.h:124
int ff_hevc_parse_sps(HEVCSPS *sps, GetBitContext *gb, unsigned int *sps_id, int apply_defdispwin, AVBufferRef **vps_list, AVCodecContext *avctx)
Parse the SPS from the bitstream into the provided HEVCSPS struct.
Definition: hevc_ps.c:865
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:1847
int vps_max_sub_layers
vps_max_temporal_layers_minus1 + 1
Definition: hevc_ps.h:115
static AVFrame * frame
int ff_h2645_extract_rbsp(const uint8_t *src, int length, H2645NAL *nal, int small_padding)
Extract the raw (unescaped) bitstream.
Definition: h2645_parse.c:32
#define OFFSET(x)
Definition: qsvenc_hevc.c:212
#define QSV_COMMON_OPTS
Definition: qsvenc.h:51
bitstream reader API header.
int ff_qsv_enc_close(AVCodecContext *avctx, QSVEncContext *q)
Definition: qsvenc.c:1101
#define av_log(a,...)
QSVEncContext qsv
Definition: qsvenc_hevc.c:49
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
int vps_max_layers
Definition: hevc_ps.h:114
av_default_item_name
#define AVERROR(e)
Definition: error.h:43
static av_always_inline void bytestream2_skip(GetByteContext *g, unsigned int size)
Definition: bytestream.h:164
int vui_timing_info_present_flag
Definition: hevc_ps.h:74
char * load_plugins
Definition: qsvenc.h:143
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:179
static av_always_inline unsigned int bytestream2_get_bytes_left(GetByteContext *g)
Definition: bytestream.h:154
planar YUV 4:2:0, 12bpp, 1 plane for Y and 1 plane for the UV components, which are interleaved (firs...
Definition: pixfmt.h:90
const char * name
Name of the codec implementation.
Definition: avcodec.h:3688
int vui_poc_proportional_to_timing_flag
Definition: hevc_ps.h:77
static av_always_inline int bytestream2_tell_p(PutByteContext *p)
Definition: bytestream.h:193
uint32_t vps_num_units_in_tick
Definition: hevc_ps.h:125
int vps_num_layer_sets
vps_num_layer_sets_minus1 + 1
Definition: hevc_ps.h:123
static const AVCodecDefault qsv_enc_defaults[]
Definition: qsvenc_hevc.c:241
uint8_t vps_poc_proportional_to_timing_flag
Definition: hevc_ps.h:127
unsigned int vps_num_reorder_pics[HEVC_MAX_SUB_LAYERS]
Definition: hevc_ps.h:120
struct HEVCSPS::@67 temporal_layer[HEVC_MAX_SUB_LAYERS]
unsigned vps_id
Definition: hevc_ps.h:143
PTL ptl
Definition: hevc_ps.h:117
uint32_t vps_time_scale
Definition: hevc_ps.h:126
static av_cold int qsv_enc_close(AVCodecContext *avctx)
Definition: qsvenc_hevc.c:205
char * av_strdup(const char *s)
Duplicate a string.
Definition: mem.c:237
Libavcodec external API header.
PTL ptl
Definition: hevc_ps.h:168
int max_sub_layers
Definition: hevc_ps.h:160
static int init_get_bits8(GetBitContext *s, const uint8_t *buffer, int byte_size)
Initialize GetBitContext.
Definition: get_bits.h:456
main external API structure.
Definition: avcodec.h:1732
const uint8_t * data
Definition: h2645_parse.h:36
uint32_t vui_num_units_in_tick
Definition: hevc_ps.h:75
GLint GLenum type
Definition: opengl_enc.c:105
int extradata_size
Definition: avcodec.h:1848
Describe the class of an AVClass context structure.
Definition: log.h:67
int ff_qsv_enc_init(AVCodecContext *avctx, QSVEncContext *q)
Definition: qsvenc.c:698
HW acceleration through QSV, data[3] contains a pointer to the mfxFrameSurface1 structure.
Definition: pixfmt.h:236
#define HEVC_MAX_SUB_LAYERS
7.4.2.1
Definition: hevc.h:64
static enum AVPixelFormat pix_fmts[]
Definition: libkvazaar.c:262
common internal api header.
common internal and external API header
#define AVERROR_UNKNOWN
Unknown error, typically from an external library.
Definition: error.h:71
#define AV_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding...
Definition: avcodec.h:769
void * priv_data
Definition: avcodec.h:1774
static av_cold int qsv_enc_init(AVCodecContext *avctx)
Definition: qsvenc_hevc.c:158
uint8_t * rbsp_buffer
Definition: h2645_parse.h:32
#define VE
Definition: qsvenc_hevc.c:213
#define av_freep(p)
int num_reorder_pics
Definition: hevc_ps.h:163
AVPixelFormat
Pixel format.
Definition: pixfmt.h:60
This structure stores compressed data.
Definition: avcodec.h:1634
LoadPlugin
Definition: qsvdec_h2645.c:40
int vps_sub_layer_ordering_info_present_flag
Definition: hevc_ps.h:118
static const AVOption options[]
Definition: qsvenc_hevc.c:214