From patchwork Sun Jan 8 18:13:05 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mark Thompson X-Patchwork-Id: 2115 Delivered-To: ffmpegpatchwork@gmail.com Received: by 10.103.89.21 with SMTP id n21csp6753866vsb; Sun, 8 Jan 2017 10:13:16 -0800 (PST) X-Received: by 10.28.130.137 with SMTP id e131mr2894740wmd.21.1483899196837; Sun, 08 Jan 2017 10:13:16 -0800 (PST) Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id a186si8098352wmh.2.2017.01.08.10.13.16; Sun, 08 Jan 2017 10:13:16 -0800 (PST) Received-SPF: pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) client-ip=79.124.17.100; Authentication-Results: mx.google.com; dkim=neutral (body hash did not verify) header.i=@jkqxz-net.20150623.gappssmtp.com; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 7BF8668A550; Sun, 8 Jan 2017 20:13:07 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wm0-f47.google.com (mail-wm0-f47.google.com [74.125.82.47]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 020B5689D47 for ; Sun, 8 Jan 2017 20:13:01 +0200 (EET) Received: by mail-wm0-f47.google.com with SMTP id c85so77889595wmi.1 for ; Sun, 08 Jan 2017 10:13:07 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=jkqxz-net.20150623.gappssmtp.com; s=20150623; h=subject:to:references:from:message-id:date:user-agent:mime-version :in-reply-to:content-transfer-encoding; bh=IMPlEMh0klgTXVvzuEOnMcQM2Ay7IBYetT8nL9luebg=; b=P1EI1hEtslXXHYbVjNUEwqWNeb055+Vwyv7XyJPkL707L0MnEzkBzC3CsH8CvieUGS XeU7aN6PHWJ3sWNow8iwZ4VHWGMvjGRQY9TMrfpTL47xzr8xJYA07rWObJgwz9bbNa7t +guFJp26MVX16sWq9LLzSycdGUnPkHNvXo5/B108SCazVApvOrrQk7MiX3CgHCplO2Ys Gwj/l8rRRcIC2TY84fsrs1Y21D7g131L3CZghzrzG4CeC8PSYDB63D0YQtOeFTavtGEI srG488hdNoj0fstDlYZbRhxlhwXtpYv9BC7XG+QYitjdMXWnaOuzsgQ0Q6z2srFjOiXZ Dz3Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-transfer-encoding; bh=IMPlEMh0klgTXVvzuEOnMcQM2Ay7IBYetT8nL9luebg=; b=pEZSK0qUhN+iAViJXPib1Er0sGH1jxb04JBQMvFE1T0grwNbvRyikpKerbRGJWw36L FHh6x7xGg9eec20n0mvq7wmD2p+BgJ42SGPGTh9Oz54XN5QfI2t04o2ssVdrt3h4y9OE xEVawr2xmvZqtA3ZH2hhaC5NjE6KkuKYGiEuh28wORUprJWDgm+EO3eG4P2gCRZwO3pQ 9X3Z8oai5WkXci/4bucPRiAoqL+DiriBv1MEXLUMhLc43Xw/wybDz6b1s1gMBUG4csGb WOx9j4uOlFcrP7zRLMqE94nUFV7FpyxL+dJkoH0MrbaITjP6HrCYY4OIV/qw6MKupMyK 1T0A== X-Gm-Message-State: AIkVDXLyR/Q7m+QZaIFMVbGqyJdsy0Q+8efqATcFjn0yFUceFtzhZ8dkDCU+nfT31UyaUQ== X-Received: by 10.28.12.80 with SMTP id 77mr3269251wmm.106.1483899186778; Sun, 08 Jan 2017 10:13:06 -0800 (PST) Received: from [192.168.0.8] (cpc91242-cmbg18-2-0-cust650.5-4.cable.virginm.net. [82.8.130.139]) by smtp.gmail.com with ESMTPSA id e14sm5571566wmd.14.2017.01.08.10.13.05 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sun, 08 Jan 2017 10:13:06 -0800 (PST) To: FFmpeg development discussions and patches References: <320e2652-f3d9-5509-11ac-fbd84e22db6a@jkqxz.net> From: Mark Thompson Message-ID: <22621831-706a-8134-7420-7f8e2e7969f6@jkqxz.net> Date: Sun, 8 Jan 2017 18:13:05 +0000 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Icedove/45.4.0 MIME-Version: 1.0 In-Reply-To: <320e2652-f3d9-5509-11ac-fbd84e22db6a@jkqxz.net> Subject: [FFmpeg-devel] [PATCH 10/16] vaapi_hevc: Convert to use the new VAAPI hwaccel code X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" From: Anton Khirnov (cherry picked from commit ea8b730d8e67152107d7fcdd5590bbb51ec236b1) Signed-off-by: Mark Thompson --- configure | 2 +- libavcodec/vaapi_hevc.c | 587 ++++++++++++++++++++++-------------------------- 2 files changed, 269 insertions(+), 320 deletions(-) diff --git a/configure b/configure index 398e843332..d0dc37585a 100755 --- a/configure +++ b/configure @@ -5640,7 +5640,7 @@ check_type "windows.h d3d11.h" "ID3D11VideoDecoder" check_type "windows.h d3d11.h" "ID3D11VideoContext" check_type "d3d9.h dxva2api.h" DXVA2_ConfigPictureDecode -D_WIN32_WINNT=0x0602 -check_type "va/va.h" "VAPictureParameterBufferHEVC" +check_type "va/va.h va/va_dec_hevc.h" "VAPictureParameterBufferHEVC" check_struct "va/va.h" "VADecPictureParameterBufferVP9" bit_depth check_type "va/va.h va/va_vpp.h" "VAProcPipelineParameterBuffer" check_type "va/va.h va/va_enc_h264.h" "VAEncPictureParameterBufferH264" diff --git a/libavcodec/vaapi_hevc.c b/libavcodec/vaapi_hevc.c index dd43463bdb..2b0e8ff326 100644 --- a/libavcodec/vaapi_hevc.c +++ b/libavcodec/vaapi_hevc.c @@ -20,38 +20,34 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ -#include "vaapi_internal.h" +#include +#include + +#include "avcodec.h" #include "hevc.h" -#include "mpegutils.h" +#include "vaapi_decode.h" -/** - * @file - * This file implements the glue code between FFmpeg's and VA API's - * structures for HEVC decoding. - */ +typedef struct VAAPIDecodePictureHEVC { + VAPictureParameterBufferHEVC pic_param; + VASliceParameterBufferHEVC last_slice_param; + const uint8_t *last_buffer; + size_t last_size; -typedef struct vaapi_hevc_frame_data { - VAPictureParameterBufferHEVC *pic_param; - VASliceParameterBufferHEVC *last_slice_param; -} vaapi_hevc_frame_data; + VAAPIDecodePicture pic; +} VAAPIDecodePictureHEVC; -/** - * Initialize an empty VA API picture. - * - * VA API requires a fixed-size reference picture array. - */ static void init_vaapi_pic(VAPictureHEVC *va_pic) { - va_pic->picture_id = VA_INVALID_ID; - va_pic->flags = VA_PICTURE_HEVC_INVALID; + va_pic->picture_id = VA_INVALID_ID; + va_pic->flags = VA_PICTURE_HEVC_INVALID; va_pic->pic_order_cnt = 0; } static void fill_vaapi_pic(VAPictureHEVC *va_pic, const HEVCFrame *pic, int rps_type) { - va_pic->picture_id = ff_vaapi_get_surface_id(pic->frame); + va_pic->picture_id = ff_vaapi_get_surface_id(pic->frame); va_pic->pic_order_cnt = pic->poc; - va_pic->flags = rps_type; + va_pic->flags = rps_type; if (pic->flags & HEVC_FRAME_FLAG_LONG_REF) va_pic->flags |= VA_PICTURE_HEVC_LONG_TERM_REFERENCE; @@ -59,9 +55,8 @@ static void fill_vaapi_pic(VAPictureHEVC *va_pic, const HEVCFrame *pic, int rps_ if (pic->frame->interlaced_frame) { va_pic->flags |= VA_PICTURE_HEVC_FIELD_PIC; - if (!pic->frame->top_field_first) { + if (!pic->frame->top_field_first) va_pic->flags |= VA_PICTURE_HEVC_BOTTOM_FIELD; - } } } @@ -70,17 +65,17 @@ static int find_frame_rps_type(const HEVCContext *h, const HEVCFrame *pic) VASurfaceID pic_surf = ff_vaapi_get_surface_id(pic->frame); int i; - for (i = 0; i < h->rps[ST_CURR_BEF].nb_refs; ++i) { + for (i = 0; i < h->rps[ST_CURR_BEF].nb_refs; i++) { if (pic_surf == ff_vaapi_get_surface_id(h->rps[ST_CURR_BEF].ref[i]->frame)) return VA_PICTURE_HEVC_RPS_ST_CURR_BEFORE; } - for (i = 0; i < h->rps[ST_CURR_AFT].nb_refs; ++i) { + for (i = 0; i < h->rps[ST_CURR_AFT].nb_refs; i++) { if (pic_surf == ff_vaapi_get_surface_id(h->rps[ST_CURR_AFT].ref[i]->frame)) return VA_PICTURE_HEVC_RPS_ST_CURR_AFTER; } - for (i = 0; i < h->rps[LT_CURR].nb_refs; ++i) { + for (i = 0; i < h->rps[LT_CURR].nb_refs; i++) { if (pic_surf == ff_vaapi_get_surface_id(h->rps[LT_CURR].ref[i]->frame)) return VA_PICTURE_HEVC_RPS_LT_CURR; } @@ -88,7 +83,7 @@ static int find_frame_rps_type(const HEVCContext *h, const HEVCFrame *pic) return 0; } -static void fill_vaapi_ReferenceFrames(const HEVCContext *h, VAPictureParameterBufferHEVC *pp) +static void fill_vaapi_reference_frames(const HEVCContext *h, VAPictureParameterBufferHEVC *pp) { const HEVCFrame *current_picture = h->ref; int i, j, rps_type; @@ -111,254 +106,200 @@ static void fill_vaapi_ReferenceFrames(const HEVCContext *h, VAPictureParameterB } } -static uint8_t get_ref_pic_index(const HEVCContext *h, const HEVCFrame *frame) -{ - vaapi_hevc_frame_data *frame_data = h->ref->hwaccel_picture_private; - VAPictureParameterBufferHEVC *pp = frame_data->pic_param; - uint8_t i; - - if (!frame) - return 0xff; - - for (i = 0; i < FF_ARRAY_ELEMS(pp->ReferenceFrames); ++i) { - VASurfaceID pid = pp->ReferenceFrames[i].picture_id; - int poc = pp->ReferenceFrames[i].pic_order_cnt; - if (pid != VA_INVALID_ID && pid == ff_vaapi_get_surface_id(frame->frame) && poc == frame->poc) - return i; - } - - return 0xff; -} - -static void fill_picture_parameters(const HEVCContext *h, VAPictureParameterBufferHEVC *pp) -{ - int i; - - pp->pic_fields.value = 0; - pp->slice_parsing_fields.value = 0; - - fill_vaapi_pic(&pp->CurrPic, h->ref, 0); - fill_vaapi_ReferenceFrames(h, pp); - - pp->pic_width_in_luma_samples = h->ps.sps->width; - pp->pic_height_in_luma_samples = h->ps.sps->height; - - pp->log2_min_luma_coding_block_size_minus3 = h->ps.sps->log2_min_cb_size - 3; - - pp->pic_fields.bits.chroma_format_idc = h->ps.sps->chroma_format_idc; - - pp->sps_max_dec_pic_buffering_minus1 = h->ps.sps->temporal_layer[h->ps.sps->max_sub_layers - 1].max_dec_pic_buffering - 1; - pp->log2_diff_max_min_luma_coding_block_size = h->ps.sps->log2_diff_max_min_coding_block_size; - pp->log2_min_transform_block_size_minus2 = h->ps.sps->log2_min_tb_size - 2; - pp->log2_diff_max_min_transform_block_size = h->ps.sps->log2_max_trafo_size - h->ps.sps->log2_min_tb_size; - pp->max_transform_hierarchy_depth_inter = h->ps.sps->max_transform_hierarchy_depth_inter; - pp->max_transform_hierarchy_depth_intra = h->ps.sps->max_transform_hierarchy_depth_intra; - pp->num_short_term_ref_pic_sets = h->ps.sps->nb_st_rps; - pp->num_long_term_ref_pic_sps = h->ps.sps->num_long_term_ref_pics_sps; - - pp->num_ref_idx_l0_default_active_minus1 = h->ps.pps->num_ref_idx_l0_default_active - 1; - pp->num_ref_idx_l1_default_active_minus1 = h->ps.pps->num_ref_idx_l1_default_active - 1; - pp->init_qp_minus26 = h->ps.pps->pic_init_qp_minus26; - - pp->pps_cb_qp_offset = h->ps.pps->cb_qp_offset; - pp->pps_cr_qp_offset = h->ps.pps->cr_qp_offset; - - pp->pic_fields.bits.tiles_enabled_flag = h->ps.pps->tiles_enabled_flag; - pp->pic_fields.bits.separate_colour_plane_flag = h->ps.sps->separate_colour_plane_flag; - pp->pic_fields.bits.pcm_enabled_flag = h->ps.sps->pcm_enabled_flag; - pp->pic_fields.bits.scaling_list_enabled_flag = h->ps.sps->scaling_list_enable_flag; - pp->pic_fields.bits.transform_skip_enabled_flag = h->ps.pps->transform_skip_enabled_flag; - pp->pic_fields.bits.amp_enabled_flag = h->ps.sps->amp_enabled_flag; - pp->pic_fields.bits.strong_intra_smoothing_enabled_flag = h->ps.sps->sps_strong_intra_smoothing_enable_flag; - pp->pic_fields.bits.sign_data_hiding_enabled_flag = h->ps.pps->sign_data_hiding_flag; - pp->pic_fields.bits.constrained_intra_pred_flag = h->ps.pps->constrained_intra_pred_flag; - pp->pic_fields.bits.cu_qp_delta_enabled_flag = h->ps.pps->cu_qp_delta_enabled_flag; - pp->pic_fields.bits.weighted_pred_flag = h->ps.pps->weighted_pred_flag; - pp->pic_fields.bits.weighted_bipred_flag = h->ps.pps->weighted_bipred_flag; - pp->pic_fields.bits.transquant_bypass_enabled_flag = h->ps.pps->transquant_bypass_enable_flag; - pp->pic_fields.bits.entropy_coding_sync_enabled_flag = h->ps.pps->entropy_coding_sync_enabled_flag; - pp->pic_fields.bits.pps_loop_filter_across_slices_enabled_flag = h->ps.pps->seq_loop_filter_across_slices_enabled_flag; - pp->pic_fields.bits.loop_filter_across_tiles_enabled_flag = h->ps.pps->loop_filter_across_tiles_enabled_flag; - - pp->pic_fields.bits.pcm_loop_filter_disabled_flag = h->ps.sps->pcm.loop_filter_disable_flag; - pp->pcm_sample_bit_depth_luma_minus1 = h->ps.sps->pcm.bit_depth - 1; - pp->pcm_sample_bit_depth_chroma_minus1 = h->ps.sps->pcm.bit_depth_chroma - 1; - pp->log2_min_pcm_luma_coding_block_size_minus3 = h->ps.sps->pcm.log2_min_pcm_cb_size - 3; - pp->log2_diff_max_min_pcm_luma_coding_block_size = h->ps.sps->pcm.log2_max_pcm_cb_size - h->ps.sps->pcm.log2_min_pcm_cb_size; - - memset(pp->column_width_minus1, 0, sizeof(pp->column_width_minus1)); - memset(pp->row_height_minus1, 0, sizeof(pp->row_height_minus1)); - - if (h->ps.pps->tiles_enabled_flag) { - pp->num_tile_columns_minus1 = h->ps.pps->num_tile_columns - 1; - pp->num_tile_rows_minus1 = h->ps.pps->num_tile_rows - 1; - - for (i = 0; i < h->ps.pps->num_tile_columns; i++) - pp->column_width_minus1[i] = h->ps.pps->column_width[i] - 1; - - for (i = 0; i < h->ps.pps->num_tile_rows; i++) - pp->row_height_minus1[i] = h->ps.pps->row_height[i] - 1; - } - - pp->diff_cu_qp_delta_depth = h->ps.pps->diff_cu_qp_delta_depth; - pp->pps_beta_offset_div2 = h->ps.pps->beta_offset / 2; - pp->pps_tc_offset_div2 = h->ps.pps->tc_offset / 2; - pp->log2_parallel_merge_level_minus2 = h->ps.pps->log2_parallel_merge_level - 2; - - /* Different chroma/luma bit depths are currently not supported by ffmpeg. */ - pp->bit_depth_luma_minus8 = h->ps.sps->bit_depth - 8; - pp->bit_depth_chroma_minus8 = h->ps.sps->bit_depth - 8; - - pp->slice_parsing_fields.bits.lists_modification_present_flag = h->ps.pps->lists_modification_present_flag; - pp->slice_parsing_fields.bits.long_term_ref_pics_present_flag = h->ps.sps->long_term_ref_pics_present_flag; - pp->slice_parsing_fields.bits.sps_temporal_mvp_enabled_flag = h->ps.sps->sps_temporal_mvp_enabled_flag; - pp->slice_parsing_fields.bits.cabac_init_present_flag = h->ps.pps->cabac_init_present_flag; - pp->slice_parsing_fields.bits.output_flag_present_flag = h->ps.pps->output_flag_present_flag; - pp->slice_parsing_fields.bits.dependent_slice_segments_enabled_flag = h->ps.pps->dependent_slice_segments_enabled_flag; - pp->slice_parsing_fields.bits.pps_slice_chroma_qp_offsets_present_flag = h->ps.pps->pic_slice_level_chroma_qp_offsets_present_flag; - pp->slice_parsing_fields.bits.sample_adaptive_offset_enabled_flag = h->ps.sps->sao_enabled; - pp->slice_parsing_fields.bits.deblocking_filter_override_enabled_flag = h->ps.pps->deblocking_filter_override_enabled_flag; - pp->slice_parsing_fields.bits.pps_disable_deblocking_filter_flag = h->ps.pps->disable_dbf; - pp->slice_parsing_fields.bits.slice_segment_header_extension_present_flag = h->ps.pps->slice_header_extension_present_flag; - - pp->log2_max_pic_order_cnt_lsb_minus4 = h->ps.sps->log2_max_poc_lsb - 4; - pp->num_extra_slice_header_bits = h->ps.pps->num_extra_slice_header_bits; - - if (h->nal_unit_type >= NAL_BLA_W_LP && h->nal_unit_type <= NAL_CRA_NUT) { - pp->slice_parsing_fields.bits.RapPicFlag = 1; - } else { - pp->slice_parsing_fields.bits.RapPicFlag = 0; - } - - if (IS_IDR(h)) { - pp->slice_parsing_fields.bits.IdrPicFlag = 1; - } else { - pp->slice_parsing_fields.bits.IdrPicFlag = 0; - } - - if (IS_IRAP(h)) { - pp->slice_parsing_fields.bits.IntraPicFlag = 1; - } else { - pp->slice_parsing_fields.bits.IntraPicFlag = 0; - } - - if (h->sh.short_term_ref_pic_set_sps_flag == 0 && h->sh.short_term_rps) { - pp->st_rps_bits = h->sh.short_term_ref_pic_set_size; - } else { - pp->st_rps_bits = 0; - } - - /* TODO */ - pp->pic_fields.bits.NoPicReorderingFlag = 0; - pp->pic_fields.bits.NoBiPredFlag = 0; -} - - -/** Initialize and start decoding a frame with VA API. */ static int vaapi_hevc_start_frame(AVCodecContext *avctx, av_unused const uint8_t *buffer, av_unused uint32_t size) { - HEVCContext * const h = avctx->priv_data; - FFVAContext * const vactx = ff_vaapi_get_context(avctx); - vaapi_hevc_frame_data *frame_data = h->ref->hwaccel_picture_private; - VAPictureParameterBufferHEVC *pic_param; - VAIQMatrixBufferHEVC *iq_matrix; - ScalingList const * scaling_list; - int i, j; - - ff_dlog(avctx, "vaapi_hevc_start_frame()\n"); - - vactx->slice_param_size = sizeof(VASliceParameterBufferHEVC); - - /* Fill in VAPictureParameterBufferHEVC. */ - pic_param = ff_vaapi_alloc_pic_param(vactx, sizeof(VAPictureParameterBufferHEVC)); - if (!pic_param) - return -1; - fill_picture_parameters(h, pic_param); - frame_data->pic_param = pic_param; - - /* Fill in VAIQMatrixBufferHEVC. */ - if (h->ps.pps->scaling_list_data_present_flag) { - scaling_list = &h->ps.pps->scaling_list; - } else if (h->ps.sps->scaling_list_enable_flag) { - scaling_list = &h->ps.sps->scaling_list; - } else { - return 0; + const HEVCContext *h = avctx->priv_data; + VAAPIDecodePictureHEVC *pic = h->ref->hwaccel_picture_private; + const HEVCSPS *sps = h->ps.sps; + const HEVCPPS *pps = h->ps.pps; + + const ScalingList *scaling_list = NULL; + int err, i; + + pic->pic.output_surface = ff_vaapi_get_surface_id(h->ref->frame); + + pic->pic_param = (VAPictureParameterBufferHEVC) { + .pic_fields.value = 0, + .slice_parsing_fields.value = 0, + .pic_width_in_luma_samples = sps->width, + .pic_height_in_luma_samples = sps->height, + .log2_min_luma_coding_block_size_minus3 = sps->log2_min_cb_size - 3, + .sps_max_dec_pic_buffering_minus1 = sps->temporal_layer[sps->max_sub_layers - 1].max_dec_pic_buffering - 1, + .log2_diff_max_min_luma_coding_block_size = sps->log2_diff_max_min_coding_block_size, + .log2_min_transform_block_size_minus2 = sps->log2_min_tb_size - 2, + .log2_diff_max_min_transform_block_size = sps->log2_max_trafo_size - sps->log2_min_tb_size, + .max_transform_hierarchy_depth_inter = sps->max_transform_hierarchy_depth_inter, + .max_transform_hierarchy_depth_intra = sps->max_transform_hierarchy_depth_intra, + .num_short_term_ref_pic_sets = sps->nb_st_rps, + .num_long_term_ref_pic_sps = sps->num_long_term_ref_pics_sps, + .num_ref_idx_l0_default_active_minus1 = pps->num_ref_idx_l0_default_active - 1, + .num_ref_idx_l1_default_active_minus1 = pps->num_ref_idx_l1_default_active - 1, + .init_qp_minus26 = pps->pic_init_qp_minus26, + .pps_cb_qp_offset = pps->cb_qp_offset, + .pps_cr_qp_offset = pps->cr_qp_offset, + .pcm_sample_bit_depth_luma_minus1 = sps->pcm.bit_depth - 1, + .pcm_sample_bit_depth_chroma_minus1 = sps->pcm.bit_depth_chroma - 1, + .log2_min_pcm_luma_coding_block_size_minus3 = sps->pcm.log2_min_pcm_cb_size - 3, + .log2_diff_max_min_pcm_luma_coding_block_size = sps->pcm.log2_max_pcm_cb_size - sps->pcm.log2_min_pcm_cb_size, + .diff_cu_qp_delta_depth = pps->diff_cu_qp_delta_depth, + .pps_beta_offset_div2 = pps->beta_offset / 2, + .pps_tc_offset_div2 = pps->tc_offset / 2, + .log2_parallel_merge_level_minus2 = pps->log2_parallel_merge_level - 2, + .bit_depth_luma_minus8 = sps->bit_depth - 8, + .bit_depth_chroma_minus8 = sps->bit_depth - 8, + .log2_max_pic_order_cnt_lsb_minus4 = sps->log2_max_poc_lsb - 4, + .num_extra_slice_header_bits = pps->num_extra_slice_header_bits, + .pic_fields.bits = { + .chroma_format_idc = sps->chroma_format_idc, + .tiles_enabled_flag = pps->tiles_enabled_flag, + .separate_colour_plane_flag = sps->separate_colour_plane_flag, + .pcm_enabled_flag = sps->pcm_enabled_flag, + .scaling_list_enabled_flag = sps->scaling_list_enable_flag, + .transform_skip_enabled_flag = pps->transform_skip_enabled_flag, + .amp_enabled_flag = sps->amp_enabled_flag, + .strong_intra_smoothing_enabled_flag = sps->sps_strong_intra_smoothing_enable_flag, + .sign_data_hiding_enabled_flag = pps->sign_data_hiding_flag, + .constrained_intra_pred_flag = pps->constrained_intra_pred_flag, + .cu_qp_delta_enabled_flag = pps->cu_qp_delta_enabled_flag, + .weighted_pred_flag = pps->weighted_pred_flag, + .weighted_bipred_flag = pps->weighted_bipred_flag, + .transquant_bypass_enabled_flag = pps->transquant_bypass_enable_flag, + .entropy_coding_sync_enabled_flag = pps->entropy_coding_sync_enabled_flag, + .pps_loop_filter_across_slices_enabled_flag = pps->seq_loop_filter_across_slices_enabled_flag, + .loop_filter_across_tiles_enabled_flag = pps->loop_filter_across_tiles_enabled_flag, + .pcm_loop_filter_disabled_flag = sps->pcm.loop_filter_disable_flag, + }, + .slice_parsing_fields.bits = { + .lists_modification_present_flag = pps->lists_modification_present_flag, + .long_term_ref_pics_present_flag = sps->long_term_ref_pics_present_flag, + .sps_temporal_mvp_enabled_flag = sps->sps_temporal_mvp_enabled_flag, + .cabac_init_present_flag = pps->cabac_init_present_flag, + .output_flag_present_flag = pps->output_flag_present_flag, + .dependent_slice_segments_enabled_flag = pps->dependent_slice_segments_enabled_flag, + .pps_slice_chroma_qp_offsets_present_flag = pps->pic_slice_level_chroma_qp_offsets_present_flag, + .sample_adaptive_offset_enabled_flag = sps->sao_enabled, + .deblocking_filter_override_enabled_flag = pps->deblocking_filter_override_enabled_flag, + .pps_disable_deblocking_filter_flag = pps->disable_dbf, + .slice_segment_header_extension_present_flag = pps->slice_header_extension_present_flag, + .RapPicFlag = IS_IRAP(h), + .IdrPicFlag = IS_IDR(h), + .IntraPicFlag = IS_IRAP(h), + }, + }; + + fill_vaapi_pic(&pic->pic_param.CurrPic, h->ref, 0); + fill_vaapi_reference_frames(h, &pic->pic_param); + + if (pps->tiles_enabled_flag) { + pic->pic_param.num_tile_columns_minus1 = pps->num_tile_columns - 1; + pic->pic_param.num_tile_rows_minus1 = pps->num_tile_rows - 1; + + for (i = 0; i < pps->num_tile_columns; i++) + pic->pic_param.column_width_minus1[i] = pps->column_width[i] - 1; + + for (i = 0; i < pps->num_tile_rows; i++) + pic->pic_param.row_height_minus1[i] = pps->row_height[i] - 1; } - iq_matrix = ff_vaapi_alloc_iq_matrix(vactx, sizeof(VAIQMatrixBufferHEVC)); - if (!iq_matrix) - return -1; + if (h->sh.short_term_ref_pic_set_sps_flag == 0 && h->sh.short_term_rps) { + pic->pic_param.st_rps_bits = h->sh.short_term_ref_pic_set_size; + } else { + pic->pic_param.st_rps_bits = 0; + } - for (i = 0; i < 6; ++i) { - for (j = 0; j < 16; ++j) { - iq_matrix->ScalingList4x4[i][j] = scaling_list->sl[0][i][j]; - } - for (j = 0; j < 64; ++j) { - iq_matrix->ScalingList8x8[i][j] = scaling_list->sl[1][i][j]; - iq_matrix->ScalingList16x16[i][j] = scaling_list->sl[2][i][j]; - if (i < 2) { - iq_matrix->ScalingList32x32[i][j] = scaling_list->sl[3][i * 3][j]; + err = ff_vaapi_decode_make_param_buffer(avctx, &pic->pic, + VAPictureParameterBufferType, + &pic->pic_param, sizeof(pic->pic_param)); + if (err < 0) + goto fail; + + if (pps->scaling_list_data_present_flag) + scaling_list = &pps->scaling_list; + else if (sps->scaling_list_enable_flag) + scaling_list = &sps->scaling_list; + + if (scaling_list) { + VAIQMatrixBufferHEVC iq_matrix; + int j; + + for (i = 0; i < 6; i++) { + for (j = 0; j < 16; j++) + iq_matrix.ScalingList4x4[i][j] = scaling_list->sl[0][i][j]; + for (j = 0; j < 64; j++) { + iq_matrix.ScalingList8x8[i][j] = scaling_list->sl[1][i][j]; + iq_matrix.ScalingList16x16[i][j] = scaling_list->sl[2][i][j]; + if (i < 2) + iq_matrix.ScalingList32x32[i][j] = scaling_list->sl[3][i * 3][j]; } + iq_matrix.ScalingListDC16x16[i] = scaling_list->sl_dc[0][i]; + if (i < 2) + iq_matrix.ScalingListDC32x32[i] = scaling_list->sl_dc[1][i * 3]; } - iq_matrix->ScalingListDC16x16[i] = scaling_list->sl_dc[0][i]; - if (i < 2) { - iq_matrix->ScalingListDC32x32[i] = scaling_list->sl_dc[1][i * 3]; - } + + err = ff_vaapi_decode_make_param_buffer(avctx, &pic->pic, + VAIQMatrixBufferType, + &iq_matrix, sizeof(iq_matrix)); + if (err < 0) + goto fail; } return 0; + +fail: + ff_vaapi_decode_cancel(avctx, &pic->pic); + return err; } -/** End a hardware decoding based frame. */ static int vaapi_hevc_end_frame(AVCodecContext *avctx) { - FFVAContext * const vactx = ff_vaapi_get_context(avctx); - HEVCContext * const h = avctx->priv_data; - vaapi_hevc_frame_data *frame_data = h->ref->hwaccel_picture_private; + const HEVCContext *h = avctx->priv_data; + VAAPIDecodePictureHEVC *pic = h->ref->hwaccel_picture_private; int ret; - ff_dlog(avctx, "vaapi_hevc_end_frame()\n"); - - frame_data->last_slice_param->LongSliceFlags.fields.LastSliceOfPic = 1; + if (pic->last_size) { + pic->last_slice_param.LongSliceFlags.fields.LastSliceOfPic = 1; + ret = ff_vaapi_decode_make_slice_buffer(avctx, &pic->pic, + &pic->last_slice_param, sizeof(pic->last_slice_param), + pic->last_buffer, pic->last_size); + if (ret < 0) + goto fail; + } - ret = ff_vaapi_commit_slices(vactx); - if (ret < 0) - goto finish; - ret = ff_vaapi_render_picture(vactx, ff_vaapi_get_surface_id(h->ref->frame)); + ret = ff_vaapi_decode_issue(avctx, &pic->pic); if (ret < 0) - goto finish; + goto fail; -finish: - ff_vaapi_common_end_frame(avctx); + return 0; +fail: + ff_vaapi_decode_cancel(avctx, &pic->pic); return ret; } -static int fill_pred_weight_table(HEVCContext * const h, - VASliceParameterBufferHEVC *slice_param, - SliceHeader * const sh) +static void fill_pred_weight_table(const HEVCContext *h, + const SliceHeader *sh, + VASliceParameterBufferHEVC *slice_param) { int i; - memset(slice_param->delta_luma_weight_l0, 0, sizeof(slice_param->delta_luma_weight_l0)); - memset(slice_param->delta_luma_weight_l1, 0, sizeof(slice_param->delta_luma_weight_l1)); - memset(slice_param->luma_offset_l0, 0, sizeof(slice_param->luma_offset_l0)); - memset(slice_param->luma_offset_l1, 0, sizeof(slice_param->luma_offset_l1)); + memset(slice_param->delta_luma_weight_l0, 0, sizeof(slice_param->delta_luma_weight_l0)); + memset(slice_param->delta_luma_weight_l1, 0, sizeof(slice_param->delta_luma_weight_l1)); + memset(slice_param->luma_offset_l0, 0, sizeof(slice_param->luma_offset_l0)); + memset(slice_param->luma_offset_l1, 0, sizeof(slice_param->luma_offset_l1)); memset(slice_param->delta_chroma_weight_l0, 0, sizeof(slice_param->delta_chroma_weight_l0)); memset(slice_param->delta_chroma_weight_l1, 0, sizeof(slice_param->delta_chroma_weight_l1)); - memset(slice_param->ChromaOffsetL0, 0, sizeof(slice_param->ChromaOffsetL0)); - memset(slice_param->ChromaOffsetL1, 0, sizeof(slice_param->ChromaOffsetL1)); + memset(slice_param->ChromaOffsetL0, 0, sizeof(slice_param->ChromaOffsetL0)); + memset(slice_param->ChromaOffsetL1, 0, sizeof(slice_param->ChromaOffsetL1)); slice_param->delta_chroma_log2_weight_denom = 0; - slice_param->luma_log2_weight_denom = 0; + slice_param->luma_log2_weight_denom = 0; - if ( sh->slice_type == I_SLICE - || (sh->slice_type == P_SLICE && !h->ps.pps->weighted_pred_flag) - || (sh->slice_type == B_SLICE && !h->ps.pps->weighted_bipred_flag)) { - return 0; - } + if (sh->slice_type == I_SLICE || + (sh->slice_type == P_SLICE && !h->ps.pps->weighted_pred_flag) || + (sh->slice_type == B_SLICE && !h->ps.pps->weighted_bipred_flag)) + return; slice_param->luma_log2_weight_denom = sh->luma_log2_weight_denom; @@ -366,7 +307,7 @@ static int fill_pred_weight_table(HEVCContext * const h, slice_param->delta_chroma_log2_weight_denom = sh->chroma_log2_weight_denom - sh->luma_log2_weight_denom; } - for (i = 0; i < 15 && i < sh->nb_refs[L0]; ++i) { + for (i = 0; i < 15 && i < sh->nb_refs[L0]; i++) { slice_param->delta_luma_weight_l0[i] = sh->luma_weight_l0[i] - (1 << sh->luma_log2_weight_denom); slice_param->luma_offset_l0[i] = sh->luma_offset_l0[i]; slice_param->delta_chroma_weight_l0[i][0] = sh->chroma_weight_l0[i][0] - (1 << sh->chroma_log2_weight_denom); @@ -376,7 +317,7 @@ static int fill_pred_weight_table(HEVCContext * const h, } if (sh->slice_type == B_SLICE) { - for (i = 0; i < 15 && i < sh->nb_refs[L1]; ++i) { + for (i = 0; i < 15 && i < sh->nb_refs[L1]; i++) { slice_param->delta_luma_weight_l1[i] = sh->luma_weight_l1[i] - (1 << sh->luma_log2_weight_denom); slice_param->luma_offset_l1[i] = sh->luma_offset_l1[i]; slice_param->delta_chroma_weight_l1[i][0] = sh->chroma_weight_l1[i][0] - (1 << sh->chroma_log2_weight_denom); @@ -385,92 +326,100 @@ static int fill_pred_weight_table(HEVCContext * const h, slice_param->ChromaOffsetL1[i][1] = sh->chroma_offset_l1[i][1]; } } +} - return 0; +static uint8_t get_ref_pic_index(const HEVCContext *h, const HEVCFrame *frame) +{ + VAAPIDecodePictureHEVC *pic = h->ref->hwaccel_picture_private; + VAPictureParameterBufferHEVC *pp = &pic->pic_param; + uint8_t i; + + if (!frame) + return 0xff; + + for (i = 0; i < FF_ARRAY_ELEMS(pp->ReferenceFrames); i++) { + VASurfaceID pid = pp->ReferenceFrames[i].picture_id; + int poc = pp->ReferenceFrames[i].pic_order_cnt; + if (pid != VA_INVALID_ID && pid == ff_vaapi_get_surface_id(frame->frame) && poc == frame->poc) + return i; + } + + return 0xff; } -/** Decode the given hevc slice with VA API. */ static int vaapi_hevc_decode_slice(AVCodecContext *avctx, const uint8_t *buffer, uint32_t size) { - FFVAContext * const vactx = ff_vaapi_get_context(avctx); - HEVCContext * const h = avctx->priv_data; - vaapi_hevc_frame_data *frame_data = h->ref->hwaccel_picture_private; - SliceHeader * const sh = &h->sh; - VASliceParameterBufferHEVC *slice_param; - int i, list_idx; - uint8_t nb_list = sh->slice_type == B_SLICE ? 2 : 1; - - if (sh->slice_type == I_SLICE) - nb_list = 0; - - ff_dlog(avctx, "vaapi_hevc_decode_slice(): buffer %p, size %d\n", buffer, size); - - /* Fill in VASliceParameterBufferH264. */ - slice_param = (VASliceParameterBufferHEVC *)ff_vaapi_alloc_slice(vactx, buffer, size); - if (!slice_param) - return -1; - - frame_data->last_slice_param = slice_param; - - /* The base structure changed, so this has to be re-set in order to be valid on every byte order. */ - slice_param->slice_data_flag = VA_SLICE_DATA_FLAG_ALL; - - /* Add 1 to the bits count here to account for the byte_alignment bit, which allways is at least one bit and not accounted for otherwise. */ - slice_param->slice_data_byte_offset = (get_bits_count(&h->HEVClc->gb) + 1 + 7) / 8; - - slice_param->slice_segment_address = sh->slice_segment_addr; - - slice_param->LongSliceFlags.value = 0; - slice_param->LongSliceFlags.fields.dependent_slice_segment_flag = sh->dependent_slice_segment_flag; - slice_param->LongSliceFlags.fields.slice_type = sh->slice_type; - slice_param->LongSliceFlags.fields.color_plane_id = sh->colour_plane_id; - slice_param->LongSliceFlags.fields.mvd_l1_zero_flag = sh->mvd_l1_zero_flag; - slice_param->LongSliceFlags.fields.cabac_init_flag = sh->cabac_init_flag; - slice_param->LongSliceFlags.fields.slice_temporal_mvp_enabled_flag = sh->slice_temporal_mvp_enabled_flag; - slice_param->LongSliceFlags.fields.slice_deblocking_filter_disabled_flag = sh->disable_deblocking_filter_flag; - slice_param->LongSliceFlags.fields.collocated_from_l0_flag = sh->collocated_list == L0 ? 1 : 0; - slice_param->LongSliceFlags.fields.slice_loop_filter_across_slices_enabled_flag = sh->slice_loop_filter_across_slices_enabled_flag; - - slice_param->LongSliceFlags.fields.slice_sao_luma_flag = sh->slice_sample_adaptive_offset_flag[0]; - if (h->ps.sps->chroma_format_idc) { - slice_param->LongSliceFlags.fields.slice_sao_chroma_flag = sh->slice_sample_adaptive_offset_flag[1]; - } - - if (sh->slice_temporal_mvp_enabled_flag) { - slice_param->collocated_ref_idx = sh->collocated_ref_idx; - } else { - slice_param->collocated_ref_idx = 0xFF; + const HEVCContext *h = avctx->priv_data; + const SliceHeader *sh = &h->sh; + VAAPIDecodePictureHEVC *pic = h->ref->hwaccel_picture_private; + + int nb_list = (sh->slice_type == B_SLICE) ? + 2 : (sh->slice_type == I_SLICE ? 0 : 1); + + int err, i, list_idx; + + if (!sh->first_slice_in_pic_flag) { + err = ff_vaapi_decode_make_slice_buffer(avctx, &pic->pic, + &pic->last_slice_param, sizeof(pic->last_slice_param), + pic->last_buffer, pic->last_size); + pic->last_buffer = NULL; + pic->last_size = 0; + if (err) { + ff_vaapi_decode_cancel(avctx, &pic->pic); + return err; + } } - slice_param->slice_qp_delta = sh->slice_qp_delta; - slice_param->slice_cb_qp_offset = sh->slice_cb_qp_offset; - slice_param->slice_cr_qp_offset = sh->slice_cr_qp_offset; - slice_param->slice_beta_offset_div2 = sh->beta_offset / 2; - slice_param->slice_tc_offset_div2 = sh->tc_offset / 2; + pic->last_slice_param = (VASliceParameterBufferHEVC) { + .slice_data_size = size, + .slice_data_offset = 0, + .slice_data_flag = VA_SLICE_DATA_FLAG_ALL, + /* Add 1 to the bits count here to account for the byte_alignment bit, which + * always is at least one bit and not accounted for otherwise. */ + .slice_data_byte_offset = (get_bits_count(&h->HEVClc->gb) + 1 + 7) / 8, + .slice_segment_address = sh->slice_segment_addr, + .slice_qp_delta = sh->slice_qp_delta, + .slice_cb_qp_offset = sh->slice_cb_qp_offset, + .slice_cr_qp_offset = sh->slice_cr_qp_offset, + .slice_beta_offset_div2 = sh->beta_offset / 2, + .slice_tc_offset_div2 = sh->tc_offset / 2, + .collocated_ref_idx = sh->slice_temporal_mvp_enabled_flag ? sh->collocated_ref_idx : 0xFF, + .five_minus_max_num_merge_cand = sh->slice_type == I_SLICE ? 0 : 5 - sh->max_num_merge_cand, + .num_ref_idx_l0_active_minus1 = sh->nb_refs[L0] ? sh->nb_refs[L0] - 1 : 0, + .num_ref_idx_l1_active_minus1 = sh->nb_refs[L1] ? sh->nb_refs[L1] - 1 : 0, + + .LongSliceFlags.fields = { + .dependent_slice_segment_flag = sh->dependent_slice_segment_flag, + .slice_type = sh->slice_type, + .color_plane_id = sh->colour_plane_id, + .mvd_l1_zero_flag = sh->mvd_l1_zero_flag, + .cabac_init_flag = sh->cabac_init_flag, + .slice_temporal_mvp_enabled_flag = sh->slice_temporal_mvp_enabled_flag, + .slice_deblocking_filter_disabled_flag = sh->disable_deblocking_filter_flag, + .collocated_from_l0_flag = sh->collocated_list == L0 ? 1 : 0, + .slice_loop_filter_across_slices_enabled_flag = sh->slice_loop_filter_across_slices_enabled_flag, + .slice_sao_luma_flag = sh->slice_sample_adaptive_offset_flag[0], + .slice_sao_chroma_flag = sh->slice_sample_adaptive_offset_flag[1], + }, + }; + + memset(pic->last_slice_param.RefPicList, 0xFF, sizeof(pic->last_slice_param.RefPicList)); + + for (list_idx = 0; list_idx < nb_list; list_idx++) { + RefPicList *rpl = &h->ref->refPicList[list_idx]; - if (sh->slice_type == I_SLICE) { - slice_param->five_minus_max_num_merge_cand = 0; - } else { - slice_param->five_minus_max_num_merge_cand = 5 - sh->max_num_merge_cand; + for (i = 0; i < rpl->nb_refs; i++) + pic->last_slice_param.RefPicList[list_idx][i] = get_ref_pic_index(h, rpl->ref[i]); } - slice_param->num_ref_idx_l0_active_minus1 = sh->nb_refs[L0] ? sh->nb_refs[L0] - 1 : 0; - slice_param->num_ref_idx_l1_active_minus1 = sh->nb_refs[L1] ? sh->nb_refs[L1] - 1 : 0; - - memset(slice_param->RefPicList, 0xFF, sizeof(slice_param->RefPicList)); + fill_pred_weight_table(h, sh, &pic->last_slice_param); - /* h->ref->refPicList is updated befor calling each slice */ - for (list_idx = 0; list_idx < nb_list; ++list_idx) { - RefPicList *rpl = &h->ref->refPicList[list_idx]; + pic->last_buffer = buffer; + pic->last_size = size; - for (i = 0; i < rpl->nb_refs; ++i) { - slice_param->RefPicList[list_idx][i] = get_ref_pic_index(h, rpl->ref[i]); - } - } - - return fill_pred_weight_table(h, slice_param, sh); + return 0; } AVHWAccel ff_hevc_vaapi_hwaccel = { @@ -481,8 +430,8 @@ AVHWAccel ff_hevc_vaapi_hwaccel = { .start_frame = vaapi_hevc_start_frame, .end_frame = vaapi_hevc_end_frame, .decode_slice = vaapi_hevc_decode_slice, - .init = ff_vaapi_context_init, - .uninit = ff_vaapi_context_fini, - .priv_data_size = sizeof(FFVAContext), - .frame_priv_data_size = sizeof(vaapi_hevc_frame_data), + .frame_priv_data_size = sizeof(VAAPIDecodePictureHEVC), + .init = ff_vaapi_decode_init, + .uninit = ff_vaapi_decode_uninit, + .priv_data_size = sizeof(VAAPIDecodeContext), };