[FFmpeg-cvslog] vaapi_hevc: Convert to use the new VAAPI hwaccel code

Anton Khirnov git at videolan.org
Wed Jan 18 01:38:50 EET 2017


ffmpeg | branch: master | Anton Khirnov <anton at khirnov.net> | Sun Oct  2 08:51:32 2016 +0200| [adb54e59c18db347f39e55832104fc3e40a3c42b] | committer: Mark Thompson

vaapi_hevc: Convert to use the new VAAPI hwaccel code

(cherry picked from commit ea8b730d8e67152107d7fcdd5590bbb51ec236b1)
Signed-off-by: Mark Thompson <sw at jkqxz.net>

> http://git.videolan.org/gitweb.cgi/ffmpeg.git/?a=commit;h=adb54e59c18db347f39e55832104fc3e40a3c42b
---

 configure               |   2 +-
 libavcodec/vaapi_hevc.c | 587 ++++++++++++++++++++++--------------------------
 2 files changed, 269 insertions(+), 320 deletions(-)

diff --git a/configure b/configure
index 4a30f82..7154142 100755
--- a/configure
+++ b/configure
@@ -5643,7 +5643,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 dd43463..2b0e8ff 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 <va/va.h>
+#include <va/va_dec_hevc.h>
+
+#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),
 };



More information about the ffmpeg-cvslog mailing list