2015-06-16 18:22:11 +02:00
/*
* Intel MediaSDK QSV based HEVC encoder
*
2015-07-09 11:52:05 +02:00
* This file is part of FFmpeg .
2015-06-16 18:22:11 +02:00
*
2015-07-09 11:52:05 +02:00
* FFmpeg is free software ; you can redistribute it and / or
2015-06-16 18:22:11 +02:00
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation ; either
* version 2.1 of the License , or ( at your option ) any later version .
*
2015-07-09 11:52:05 +02:00
* FFmpeg is distributed in the hope that it will be useful ,
2015-06-16 18:22:11 +02:00
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the GNU
* Lesser General Public License for more details .
*
* You should have received a copy of the GNU Lesser General Public
2015-07-09 11:52:05 +02:00
* License along with FFmpeg ; if not , write to the Free Software
2015-06-16 18:22:11 +02:00
* Foundation , Inc . , 51 Franklin Street , Fifth Floor , Boston , MA 02110 - 1301 USA
*/
# include <stdint.h>
# include <sys/types.h>
# include <mfx/mfxvideo.h>
# include "libavutil/common.h"
# include "libavutil/opt.h"
# include "avcodec.h"
# include "bytestream.h"
2022-03-16 19:18:28 +02:00
# include "codec_internal.h"
2015-06-16 18:22:11 +02:00
# include "get_bits.h"
2016-08-21 17:35:28 +02:00
# include "hevc.h"
2016-08-21 17:14:24 +02:00
# include "hevcdec.h"
2016-03-21 08:26:42 +02:00
# include "h2645_parse.h"
2015-06-16 18:22:11 +02:00
# include "qsv.h"
# include "qsv_internal.h"
# include "qsvenc.h"
2015-07-21 19:43:34 +02:00
enum LoadPlugin {
2015-06-16 18:22:11 +02:00
LOAD_PLUGIN_NONE ,
LOAD_PLUGIN_HEVC_SW ,
LOAD_PLUGIN_HEVC_HW ,
2015-07-21 19:43:34 +02:00
} ;
2015-06-16 18:22:11 +02:00
typedef struct QSVHEVCEncContext {
AVClass * class ;
QSVEncContext qsv ;
int load_plugin ;
} QSVHEVCEncContext ;
static int generate_fake_vps ( QSVEncContext * q , AVCodecContext * avctx )
{
GetByteContext gbc ;
PutByteContext pbc ;
GetBitContext gb ;
2017-11-04 19:41:06 +02:00
H2645RBSP sps_rbsp = { NULL } ;
2016-03-21 08:26:42 +02:00
H2645NAL sps_nal = { NULL } ;
2015-06-16 18:22:11 +02:00
HEVCSPS sps = { 0 } ;
HEVCVPS vps = { 0 } ;
uint8_t vps_buf [ 128 ] , vps_rbsp_buf [ 128 ] ;
uint8_t * new_extradata ;
unsigned int sps_id ;
int ret , i , type , vps_size ;
if ( ! avctx - > extradata_size ) {
av_log ( avctx , AV_LOG_ERROR , " No extradata returned from libmfx \n " ) ;
return AVERROR_UNKNOWN ;
}
2017-11-04 19:41:06 +02:00
av_fast_padded_malloc ( & sps_rbsp . rbsp_buffer , & sps_rbsp . rbsp_buffer_alloc_size , avctx - > extradata_size ) ;
if ( ! sps_rbsp . rbsp_buffer )
return AVERROR ( ENOMEM ) ;
2015-06-16 18:22:11 +02:00
/* parse the SPS */
2017-11-04 19:41:06 +02:00
ret = ff_h2645_extract_rbsp ( avctx - > extradata + 4 , avctx - > extradata_size - 4 , & sps_rbsp , & sps_nal , 1 ) ;
2015-06-16 18:22:11 +02:00
if ( ret < 0 ) {
av_log ( avctx , AV_LOG_ERROR , " Error unescaping the SPS buffer \n " ) ;
return ret ;
}
ret = init_get_bits8 ( & gb , sps_nal . data , sps_nal . size ) ;
if ( ret < 0 ) {
2017-11-04 19:41:06 +02:00
av_freep ( & sps_rbsp . rbsp_buffer ) ;
2015-06-16 18:22:11 +02:00
return ret ;
}
get_bits ( & gb , 1 ) ;
type = get_bits ( & gb , 6 ) ;
2016-08-21 17:35:28 +02:00
if ( type ! = HEVC_NAL_SPS ) {
2015-06-16 18:22:11 +02:00
av_log ( avctx , AV_LOG_ERROR , " Unexpected NAL type in the extradata: %d \n " ,
type ) ;
2017-11-04 19:41:06 +02:00
av_freep ( & sps_rbsp . rbsp_buffer ) ;
2015-06-16 18:22:11 +02:00
return AVERROR_INVALIDDATA ;
}
get_bits ( & gb , 9 ) ;
ret = ff_hevc_parse_sps ( & sps , & gb , & sps_id , 0 , NULL , avctx ) ;
2017-11-04 19:41:06 +02:00
av_freep ( & sps_rbsp . rbsp_buffer ) ;
2015-06-16 18:22:11 +02:00
if ( ret < 0 ) {
av_log ( avctx , AV_LOG_ERROR , " Error parsing the SPS \n " ) ;
return ret ;
}
/* generate the VPS */
vps . vps_max_layers = 1 ;
vps . vps_max_sub_layers = sps . max_sub_layers ;
2019-03-26 00:10:21 +02:00
vps . vps_temporal_id_nesting_flag = sps . temporal_id_nesting_flag ;
2015-06-16 18:22:11 +02:00
memcpy ( & vps . ptl , & sps . ptl , sizeof ( vps . ptl ) ) ;
vps . vps_sub_layer_ordering_info_present_flag = 1 ;
2016-08-21 17:35:28 +02:00
for ( i = 0 ; i < HEVC_MAX_SUB_LAYERS ; i + + ) {
2015-06-16 18:22:11 +02:00
vps . vps_max_dec_pic_buffering [ i ] = sps . temporal_layer [ i ] . max_dec_pic_buffering ;
vps . vps_num_reorder_pics [ i ] = sps . temporal_layer [ i ] . num_reorder_pics ;
vps . vps_max_latency_increase [ i ] = sps . temporal_layer [ i ] . max_latency_increase ;
}
vps . vps_num_layer_sets = 1 ;
vps . vps_timing_info_present_flag = sps . vui . vui_timing_info_present_flag ;
vps . vps_num_units_in_tick = sps . vui . vui_num_units_in_tick ;
vps . vps_time_scale = sps . vui . vui_time_scale ;
vps . vps_poc_proportional_to_timing_flag = sps . vui . vui_poc_proportional_to_timing_flag ;
vps . vps_num_ticks_poc_diff_one = sps . vui . vui_num_ticks_poc_diff_one_minus1 + 1 ;
2019-03-26 00:10:21 +02:00
vps . vps_num_hrd_parameters = 0 ;
2015-06-16 18:22:11 +02:00
/* generate the encoded RBSP form of the VPS */
ret = ff_hevc_encode_nal_vps ( & vps , sps . vps_id , vps_rbsp_buf , sizeof ( vps_rbsp_buf ) ) ;
if ( ret < 0 ) {
av_log ( avctx , AV_LOG_ERROR , " Error writing the VPS \n " ) ;
return ret ;
}
/* escape and add the startcode */
bytestream2_init ( & gbc , vps_rbsp_buf , ret ) ;
bytestream2_init_writer ( & pbc , vps_buf , sizeof ( vps_buf ) ) ;
2016-08-21 17:35:28 +02:00
bytestream2_put_be32 ( & pbc , 1 ) ; // startcode
bytestream2_put_byte ( & pbc , HEVC_NAL_VPS < < 1 ) ; // NAL
bytestream2_put_byte ( & pbc , 1 ) ; // header
2015-06-16 18:22:11 +02:00
while ( bytestream2_get_bytes_left ( & gbc ) ) {
2019-03-26 00:10:21 +02:00
if ( bytestream2_get_bytes_left ( & gbc ) > = 3 & & bytestream2_peek_be24 ( & gbc ) < = 3 ) {
2015-06-16 18:22:11 +02:00
bytestream2_put_be24 ( & pbc , 3 ) ;
bytestream2_skip ( & gbc , 2 ) ;
} else
bytestream2_put_byte ( & pbc , bytestream2_get_byte ( & gbc ) ) ;
}
vps_size = bytestream2_tell_p ( & pbc ) ;
2015-06-29 23:48:34 +02:00
new_extradata = av_mallocz ( vps_size + avctx - > extradata_size + AV_INPUT_BUFFER_PADDING_SIZE ) ;
2015-06-16 18:22:11 +02:00
if ( ! new_extradata )
return AVERROR ( ENOMEM ) ;
memcpy ( new_extradata , vps_buf , vps_size ) ;
memcpy ( new_extradata + vps_size , avctx - > extradata , avctx - > extradata_size ) ;
av_freep ( & avctx - > extradata ) ;
avctx - > extradata = new_extradata ;
avctx - > extradata_size + = vps_size ;
return 0 ;
}
static av_cold int qsv_enc_init ( AVCodecContext * avctx )
{
QSVHEVCEncContext * q = avctx - > priv_data ;
int ret ;
if ( q - > load_plugin ! = LOAD_PLUGIN_NONE ) {
2016-07-03 10:09:36 +02:00
static const char * const uid_hevcenc_sw = " 2fca99749fdb49aeb121a5b63ef568f7 " ;
static const char * const uid_hevcenc_hw = " 6fadc791a0c2eb479ab6dcd5ea9da347 " ;
2015-06-16 18:22:11 +02:00
if ( q - > qsv . load_plugins [ 0 ] ) {
av_log ( avctx , AV_LOG_WARNING ,
" load_plugins is not empty, but load_plugin is not set to 'none'. "
" The load_plugin value will be ignored. \n " ) ;
} else {
av_freep ( & q - > qsv . load_plugins ) ;
if ( q - > load_plugin = = LOAD_PLUGIN_HEVC_SW )
q - > qsv . load_plugins = av_strdup ( uid_hevcenc_sw ) ;
else
q - > qsv . load_plugins = av_strdup ( uid_hevcenc_hw ) ;
if ( ! q - > qsv . load_plugins )
return AVERROR ( ENOMEM ) ;
}
}
2017-11-11 22:25:57 +02:00
// HEVC and H264 meaning of the value is shifted by 1, make it consistent
q - > qsv . idr_interval + + ;
2015-06-16 18:22:11 +02:00
ret = ff_qsv_enc_init ( avctx , & q - > qsv ) ;
if ( ret < 0 )
return ret ;
2019-03-26 20:56:24 +02:00
if ( ! q - > qsv . hevc_vps ) {
ret = generate_fake_vps ( & q - > qsv , avctx ) ;
if ( ret < 0 ) {
ff_qsv_enc_close ( avctx , & q - > qsv ) ;
return ret ;
}
2015-06-16 18:22:11 +02:00
}
return 0 ;
}
static int qsv_enc_frame ( AVCodecContext * avctx , AVPacket * pkt ,
const AVFrame * frame , int * got_packet )
{
QSVHEVCEncContext * q = avctx - > priv_data ;
return ff_qsv_encode ( avctx , & q - > qsv , pkt , frame , got_packet ) ;
}
static av_cold int qsv_enc_close ( AVCodecContext * avctx )
{
QSVHEVCEncContext * q = avctx - > priv_data ;
return ff_qsv_enc_close ( avctx , & q - > qsv ) ;
}
# define OFFSET(x) offsetof(QSVHEVCEncContext, x)
# define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
static const AVOption options [ ] = {
2015-11-07 21:50:41 +02:00
QSV_COMMON_OPTS
2015-06-16 18:22:11 +02:00
2017-11-11 22:25:57 +02:00
{ " idr_interval " , " Distance (in I-frames) between IDR frames " , OFFSET ( qsv . idr_interval ) , AV_OPT_TYPE_INT , { . i64 = 0 } , - 1 , INT_MAX , VE , " idr_interval " } ,
{ " begin_only " , " Output an IDR-frame only at the beginning of the stream " , 0 , AV_OPT_TYPE_CONST , { . i64 = - 1 } , 0 , 0 , VE , " idr_interval " } ,
2018-12-12 09:03:21 +02:00
{ " load_plugin " , " A user plugin to load in an internal session " , OFFSET ( load_plugin ) , AV_OPT_TYPE_INT , { . i64 = LOAD_PLUGIN_HEVC_HW } , LOAD_PLUGIN_NONE , LOAD_PLUGIN_HEVC_HW , VE , " load_plugin " } ,
2015-06-16 18:22:11 +02:00
{ " none " , NULL , 0 , AV_OPT_TYPE_CONST , { . i64 = LOAD_PLUGIN_NONE } , 0 , 0 , VE , " load_plugin " } ,
{ " hevc_sw " , NULL , 0 , AV_OPT_TYPE_CONST , { . i64 = LOAD_PLUGIN_HEVC_SW } , 0 , 0 , VE , " load_plugin " } ,
{ " hevc_hw " , NULL , 0 , AV_OPT_TYPE_CONST , { . i64 = LOAD_PLUGIN_HEVC_HW } , 0 , 0 , VE , " load_plugin " } ,
{ " load_plugins " , " A :-separate list of hexadecimal plugin UIDs to load in an internal session " ,
OFFSET ( qsv . load_plugins ) , AV_OPT_TYPE_STRING , { . str = " " } , 0 , 0 , VE } ,
2021-12-21 17:22:25 +02:00
# if QSV_HAVE_LA
{ " look_ahead_depth " , " Depth of look ahead in number frames, available when extbrc option is enabled " , OFFSET ( qsv . look_ahead_depth ) , AV_OPT_TYPE_INT , { . i64 = 0 } , 0 , 100 , VE } ,
# endif
2015-06-16 18:22:11 +02:00
{ " profile " , NULL , OFFSET ( qsv . profile ) , AV_OPT_TYPE_INT , { . i64 = MFX_PROFILE_UNKNOWN } , 0 , INT_MAX , VE , " profile " } ,
{ " unknown " , NULL , 0 , AV_OPT_TYPE_CONST , { . i64 = MFX_PROFILE_UNKNOWN } , INT_MIN , INT_MAX , VE , " profile " } ,
{ " main " , NULL , 0 , AV_OPT_TYPE_CONST , { . i64 = MFX_PROFILE_HEVC_MAIN } , INT_MIN , INT_MAX , VE , " profile " } ,
{ " main10 " , NULL , 0 , AV_OPT_TYPE_CONST , { . i64 = MFX_PROFILE_HEVC_MAIN10 } , INT_MIN , INT_MAX , VE , " profile " } ,
{ " mainsp " , NULL , 0 , AV_OPT_TYPE_CONST , { . i64 = MFX_PROFILE_HEVC_MAINSP } , INT_MIN , INT_MAX , VE , " profile " } ,
2020-02-26 10:40:02 +02:00
{ " rext " , NULL , 0 , AV_OPT_TYPE_CONST , { . i64 = MFX_PROFILE_HEVC_REXT } , INT_MIN , INT_MAX , VE , " profile " } ,
2020-09-28 08:14:42 +02:00
# if QSV_VERSION_ATLEAST(1, 32)
{ " scc " , NULL , 0 , AV_OPT_TYPE_CONST , { . i64 = MFX_PROFILE_HEVC_SCC } , INT_MIN , INT_MAX , VE , " profile " } ,
# endif
2015-06-16 18:22:11 +02:00
2019-01-11 09:09:07 +02:00
{ " gpb " , " 1: GPB (generalized P/B frame); 0: regular P frame " , OFFSET ( qsv . gpb ) , AV_OPT_TYPE_BOOL , { . i64 = 1 } , 0 , 1 , VE } ,
2019-11-26 05:56:18 +02:00
{ " tile_cols " , " Number of columns for tiled encoding " , OFFSET ( qsv . tile_cols ) , AV_OPT_TYPE_INT , { . i64 = 0 } , 0 , UINT16_MAX , VE } ,
{ " tile_rows " , " Number of rows for tiled encoding " , OFFSET ( qsv . tile_rows ) , AV_OPT_TYPE_INT , { . i64 = 0 } , 0 , UINT16_MAX , VE } ,
2020-11-05 09:20:10 +02:00
{ " recovery_point_sei " , " Insert recovery point SEI messages " , OFFSET ( qsv . recovery_point_sei ) , AV_OPT_TYPE_INT , { . i64 = - 1 } , - 1 , 1 , VE } ,
2021-12-25 05:48:58 +02:00
{ " aud " , " Insert the Access Unit Delimiter NAL " , OFFSET ( qsv . aud ) , AV_OPT_TYPE_BOOL , { . i64 = 0 } , 0 , 1 , VE } ,
2021-08-06 04:10:33 +02:00
{ " pic_timing_sei " , " Insert picture timing SEI with pic_struct_syntax element " , OFFSET ( qsv . pic_timing_sei ) , AV_OPT_TYPE_BOOL , { . i64 = 1 } , 0 , 1 , VE } ,
2022-01-17 07:11:22 +02:00
# if QSV_VERSION_ATLEAST(1, 26)
{ " transform_skip " , " Turn this option ON to enable transformskip " , OFFSET ( qsv . transform_skip ) , AV_OPT_TYPE_INT , { . i64 = - 1 } , - 1 , 1 , VE } ,
# endif
2022-01-25 09:12:37 +02:00
{ " int_ref_type " , " Intra refresh type. B frames should be set to 0 " , OFFSET ( qsv . int_ref_type ) , AV_OPT_TYPE_INT , { . i64 = - 1 } , - 1 , UINT16_MAX , VE , " int_ref_type " } ,
{ " none " , NULL , 0 , AV_OPT_TYPE_CONST , { . i64 = 0 } , . flags = VE , " int_ref_type " } ,
{ " vertical " , NULL , 0 , AV_OPT_TYPE_CONST , { . i64 = 1 } , . flags = VE , " int_ref_type " } ,
{ " horizontal " , NULL , 0 , AV_OPT_TYPE_CONST , { . i64 = 2 } , . flags = VE , " int_ref_type " } ,
{ " int_ref_cycle_size " , " Number of frames in the intra refresh cycle " , OFFSET ( qsv . int_ref_cycle_size ) , AV_OPT_TYPE_INT , { . i64 = - 1 } , - 1 , UINT16_MAX , VE } ,
{ " int_ref_qp_delta " , " QP difference for the refresh MBs " , OFFSET ( qsv . int_ref_qp_delta ) , AV_OPT_TYPE_INT , { . i64 = INT16_MIN } , INT16_MIN , INT16_MAX , VE } ,
# if QSV_VERSION_ATLEAST(1, 16)
{ " int_ref_cycle_dist " , " Distance between the beginnings of the intra-refresh cycles in frames " , OFFSET ( qsv . int_ref_cycle_dist ) , AV_OPT_TYPE_INT , { . i64 = - 1 } , - 1 , INT16_MAX , VE } ,
# endif
2019-11-26 05:56:18 +02:00
2015-06-16 18:22:11 +02:00
{ NULL } ,
} ;
static const AVClass class = {
. class_name = " hevc_qsv encoder " ,
. item_name = av_default_item_name ,
. option = options ,
. version = LIBAVUTIL_VERSION_INT ,
} ;
2022-03-16 22:26:11 +02:00
static const FFCodecDefault qsv_enc_defaults [ ] = {
2015-06-16 18:22:11 +02:00
{ " b " , " 1M " } ,
{ " refs " , " 0 " } ,
// same as the x264 default
2015-12-18 10:55:13 +02:00
{ " g " , " 248 " } ,
{ " bf " , " 8 " } ,
2020-03-11 12:39:28 +02:00
{ " qmin " , " -1 " } ,
{ " qmax " , " -1 " } ,
2019-03-27 13:33:37 +02:00
{ " trellis " , " -1 " } ,
2015-06-16 18:22:11 +02:00
{ " flags " , " +cgop " } ,
{ NULL } ,
} ;
2022-03-16 22:09:54 +02:00
const FFCodec ff_hevc_qsv_encoder = {
. p . name = " hevc_qsv " ,
. p . long_name = NULL_IF_CONFIG_SMALL ( " HEVC (Intel Quick Sync Video acceleration) " ) ,
2015-06-16 18:22:11 +02:00
. priv_data_size = sizeof ( QSVHEVCEncContext ) ,
2022-03-16 22:09:54 +02:00
. p . type = AVMEDIA_TYPE_VIDEO ,
. p . id = AV_CODEC_ID_HEVC ,
2015-06-16 18:22:11 +02:00
. init = qsv_enc_init ,
2022-03-30 23:28:24 +02:00
FF_CODEC_ENCODE_CB ( qsv_enc_frame ) ,
2015-06-16 18:22:11 +02:00
. close = qsv_enc_close ,
2022-03-16 22:09:54 +02:00
. p . capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_HYBRID ,
. p . pix_fmts = ( const enum AVPixelFormat [ ] ) { AV_PIX_FMT_NV12 ,
2016-06-22 19:57:28 +02:00
AV_PIX_FMT_P010 ,
2022-04-06 10:48:04 +02:00
AV_PIX_FMT_YUYV422 ,
AV_PIX_FMT_Y210 ,
2015-06-16 18:22:11 +02:00
AV_PIX_FMT_QSV ,
2022-02-18 07:50:12 +02:00
# if QSV_VERSION_ATLEAST(1, 17)
AV_PIX_FMT_BGRA ,
# endif
# if QSV_VERSION_ATLEAST(1, 9)
AV_PIX_FMT_X2RGB10 ,
# endif
2015-06-16 18:22:11 +02:00
AV_PIX_FMT_NONE } ,
2022-03-16 22:09:54 +02:00
. p . priv_class = & class ,
2015-06-16 18:22:11 +02:00
. defaults = qsv_enc_defaults ,
2015-08-05 13:42:08 +02:00
. caps_internal = FF_CODEC_CAP_INIT_CLEANUP ,
2022-03-16 22:09:54 +02:00
. p . wrapper_name = " qsv " ,
2020-04-13 17:33:16 +02:00
. hw_configs = ff_qsv_enc_hw_configs ,
2015-06-16 18:22:11 +02:00
} ;