LCOV - code coverage report
Current view: top level - libavcodec - hevcdec.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 1599 2119 75.5 %
Date: 2018-05-20 11:54:08 Functions: 39 47 83.0 %

          Line data    Source code
       1             : /*
       2             :  * HEVC video Decoder
       3             :  *
       4             :  * Copyright (C) 2012 - 2013 Guillaume Martres
       5             :  * Copyright (C) 2012 - 2013 Mickael Raulet
       6             :  * Copyright (C) 2012 - 2013 Gildas Cocherel
       7             :  * Copyright (C) 2012 - 2013 Wassim Hamidouche
       8             :  *
       9             :  * This file is part of FFmpeg.
      10             :  *
      11             :  * FFmpeg is free software; you can redistribute it and/or
      12             :  * modify it under the terms of the GNU Lesser General Public
      13             :  * License as published by the Free Software Foundation; either
      14             :  * version 2.1 of the License, or (at your option) any later version.
      15             :  *
      16             :  * FFmpeg is distributed in the hope that it will be useful,
      17             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      18             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      19             :  * Lesser General Public License for more details.
      20             :  *
      21             :  * You should have received a copy of the GNU Lesser General Public
      22             :  * License along with FFmpeg; if not, write to the Free Software
      23             :  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
      24             :  */
      25             : 
      26             : #include "libavutil/attributes.h"
      27             : #include "libavutil/common.h"
      28             : #include "libavutil/display.h"
      29             : #include "libavutil/internal.h"
      30             : #include "libavutil/mastering_display_metadata.h"
      31             : #include "libavutil/md5.h"
      32             : #include "libavutil/opt.h"
      33             : #include "libavutil/pixdesc.h"
      34             : #include "libavutil/stereo3d.h"
      35             : 
      36             : #include "bswapdsp.h"
      37             : #include "bytestream.h"
      38             : #include "cabac_functions.h"
      39             : #include "golomb.h"
      40             : #include "hevc.h"
      41             : #include "hevc_data.h"
      42             : #include "hevc_parse.h"
      43             : #include "hevcdec.h"
      44             : #include "hwaccel.h"
      45             : #include "profiles.h"
      46             : 
      47             : const uint8_t ff_hevc_pel_weight[65] = { [2] = 0, [4] = 1, [6] = 2, [8] = 3, [12] = 4, [16] = 5, [24] = 6, [32] = 7, [48] = 8, [64] = 9 };
      48             : 
      49             : /**
      50             :  * NOTE: Each function hls_foo correspond to the function foo in the
      51             :  * specification (HLS stands for High Level Syntax).
      52             :  */
      53             : 
      54             : /**
      55             :  * Section 5.7
      56             :  */
      57             : 
      58             : /* free everything allocated  by pic_arrays_init() */
      59         720 : static void pic_arrays_free(HEVCContext *s)
      60             : {
      61         720 :     av_freep(&s->sao);
      62         720 :     av_freep(&s->deblock);
      63             : 
      64         720 :     av_freep(&s->skip_flag);
      65         720 :     av_freep(&s->tab_ct_depth);
      66             : 
      67         720 :     av_freep(&s->tab_ipm);
      68         720 :     av_freep(&s->cbf_luma);
      69         720 :     av_freep(&s->is_pcm);
      70             : 
      71         720 :     av_freep(&s->qp_y_tab);
      72         720 :     av_freep(&s->tab_slice_address);
      73         720 :     av_freep(&s->filter_slice_edges);
      74             : 
      75         720 :     av_freep(&s->horizontal_bs);
      76         720 :     av_freep(&s->vertical_bs);
      77             : 
      78         720 :     av_freep(&s->sh.entry_point_offset);
      79         720 :     av_freep(&s->sh.size);
      80         720 :     av_freep(&s->sh.offset);
      81             : 
      82         720 :     av_buffer_pool_uninit(&s->tab_mvf_pool);
      83         720 :     av_buffer_pool_uninit(&s->rpl_tab_pool);
      84         720 : }
      85             : 
      86             : /* allocate arrays that depend on frame dimensions */
      87         361 : static int pic_arrays_init(HEVCContext *s, const HEVCSPS *sps)
      88             : {
      89         361 :     int log2_min_cb_size = sps->log2_min_cb_size;
      90         361 :     int width            = sps->width;
      91         361 :     int height           = sps->height;
      92         722 :     int pic_size_in_ctb  = ((width  >> log2_min_cb_size) + 1) *
      93         361 :                            ((height >> log2_min_cb_size) + 1);
      94         361 :     int ctb_count        = sps->ctb_width * sps->ctb_height;
      95         361 :     int min_pu_size      = sps->min_pu_width * sps->min_pu_height;
      96             : 
      97         361 :     s->bs_width  = (width  >> 2) + 1;
      98         361 :     s->bs_height = (height >> 2) + 1;
      99             : 
     100         361 :     s->sao           = av_mallocz_array(ctb_count, sizeof(*s->sao));
     101         361 :     s->deblock       = av_mallocz_array(ctb_count, sizeof(*s->deblock));
     102         361 :     if (!s->sao || !s->deblock)
     103             :         goto fail;
     104             : 
     105         361 :     s->skip_flag    = av_malloc_array(sps->min_cb_height, sps->min_cb_width);
     106         361 :     s->tab_ct_depth = av_malloc_array(sps->min_cb_height, sps->min_cb_width);
     107         722 :     if (!s->skip_flag || !s->tab_ct_depth)
     108             :         goto fail;
     109             : 
     110         361 :     s->cbf_luma = av_malloc_array(sps->min_tb_width, sps->min_tb_height);
     111         361 :     s->tab_ipm  = av_mallocz(min_pu_size);
     112         361 :     s->is_pcm   = av_malloc_array(sps->min_pu_width + 1, sps->min_pu_height + 1);
     113        1083 :     if (!s->tab_ipm || !s->cbf_luma || !s->is_pcm)
     114             :         goto fail;
     115             : 
     116         361 :     s->filter_slice_edges = av_mallocz(ctb_count);
     117         361 :     s->tab_slice_address  = av_malloc_array(pic_size_in_ctb,
     118             :                                       sizeof(*s->tab_slice_address));
     119         361 :     s->qp_y_tab           = av_malloc_array(pic_size_in_ctb,
     120             :                                       sizeof(*s->qp_y_tab));
     121        1083 :     if (!s->qp_y_tab || !s->filter_slice_edges || !s->tab_slice_address)
     122             :         goto fail;
     123             : 
     124         361 :     s->horizontal_bs = av_mallocz_array(s->bs_width, s->bs_height);
     125         361 :     s->vertical_bs   = av_mallocz_array(s->bs_width, s->bs_height);
     126         722 :     if (!s->horizontal_bs || !s->vertical_bs)
     127             :         goto fail;
     128             : 
     129         361 :     s->tab_mvf_pool = av_buffer_pool_init(min_pu_size * sizeof(MvField),
     130             :                                           av_buffer_allocz);
     131         361 :     s->rpl_tab_pool = av_buffer_pool_init(ctb_count * sizeof(RefPicListTab),
     132             :                                           av_buffer_allocz);
     133         722 :     if (!s->tab_mvf_pool || !s->rpl_tab_pool)
     134             :         goto fail;
     135             : 
     136         361 :     return 0;
     137             : 
     138           0 : fail:
     139           0 :     pic_arrays_free(s);
     140           0 :     return AVERROR(ENOMEM);
     141             : }
     142             : 
     143        1350 : static int pred_weight_table(HEVCContext *s, GetBitContext *gb)
     144             : {
     145        1350 :     int i = 0;
     146        1350 :     int j = 0;
     147             :     uint8_t luma_weight_l0_flag[16];
     148             :     uint8_t chroma_weight_l0_flag[16];
     149             :     uint8_t luma_weight_l1_flag[16];
     150             :     uint8_t chroma_weight_l1_flag[16];
     151             :     int luma_log2_weight_denom;
     152             : 
     153        1350 :     luma_log2_weight_denom = get_ue_golomb_long(gb);
     154        1350 :     if (luma_log2_weight_denom < 0 || luma_log2_weight_denom > 7) {
     155           0 :         av_log(s->avctx, AV_LOG_ERROR, "luma_log2_weight_denom %d is invalid\n", luma_log2_weight_denom);
     156           0 :         return AVERROR_INVALIDDATA;
     157             :     }
     158        1350 :     s->sh.luma_log2_weight_denom = av_clip_uintp2(luma_log2_weight_denom, 3);
     159        1350 :     if (s->ps.sps->chroma_format_idc != 0) {
     160        1350 :         int64_t chroma_log2_weight_denom = luma_log2_weight_denom + (int64_t)get_se_golomb(gb);
     161        1350 :         if (chroma_log2_weight_denom < 0 || chroma_log2_weight_denom > 7) {
     162           0 :             av_log(s->avctx, AV_LOG_ERROR, "chroma_log2_weight_denom %"PRId64" is invalid\n", chroma_log2_weight_denom);
     163           0 :             return AVERROR_INVALIDDATA;
     164             :         }
     165        1350 :         s->sh.chroma_log2_weight_denom = chroma_log2_weight_denom;
     166             :     }
     167             : 
     168        5650 :     for (i = 0; i < s->sh.nb_refs[L0]; i++) {
     169        4300 :         luma_weight_l0_flag[i] = get_bits1(gb);
     170        4300 :         if (!luma_weight_l0_flag[i]) {
     171        1183 :             s->sh.luma_weight_l0[i] = 1 << s->sh.luma_log2_weight_denom;
     172        1183 :             s->sh.luma_offset_l0[i] = 0;
     173             :         }
     174             :     }
     175        1350 :     if (s->ps.sps->chroma_format_idc != 0) {
     176        5650 :         for (i = 0; i < s->sh.nb_refs[L0]; i++)
     177        4300 :             chroma_weight_l0_flag[i] = get_bits1(gb);
     178             :     } else {
     179           0 :         for (i = 0; i < s->sh.nb_refs[L0]; i++)
     180           0 :             chroma_weight_l0_flag[i] = 0;
     181             :     }
     182        5650 :     for (i = 0; i < s->sh.nb_refs[L0]; i++) {
     183        4300 :         if (luma_weight_l0_flag[i]) {
     184        3117 :             int delta_luma_weight_l0 = get_se_golomb(gb);
     185        3117 :             s->sh.luma_weight_l0[i] = (1 << s->sh.luma_log2_weight_denom) + delta_luma_weight_l0;
     186        3117 :             s->sh.luma_offset_l0[i] = get_se_golomb(gb);
     187             :         }
     188        4300 :         if (chroma_weight_l0_flag[i]) {
     189        3393 :             for (j = 0; j < 2; j++) {
     190        2262 :                 int delta_chroma_weight_l0 = get_se_golomb(gb);
     191        2262 :                 int delta_chroma_offset_l0 = get_se_golomb(gb);
     192             : 
     193        2262 :                 if (   (int8_t)delta_chroma_weight_l0 != delta_chroma_weight_l0
     194        2262 :                     || delta_chroma_offset_l0 < -(1<<17) || delta_chroma_offset_l0 > (1<<17)) {
     195           0 :                     return AVERROR_INVALIDDATA;
     196             :                 }
     197             : 
     198        2262 :                 s->sh.chroma_weight_l0[i][j] = (1 << s->sh.chroma_log2_weight_denom) + delta_chroma_weight_l0;
     199        4524 :                 s->sh.chroma_offset_l0[i][j] = av_clip((delta_chroma_offset_l0 - ((128 * s->sh.chroma_weight_l0[i][j])
     200        2262 :                                                                                     >> s->sh.chroma_log2_weight_denom) + 128), -128, 127);
     201             :             }
     202             :         } else {
     203        3169 :             s->sh.chroma_weight_l0[i][0] = 1 << s->sh.chroma_log2_weight_denom;
     204        3169 :             s->sh.chroma_offset_l0[i][0] = 0;
     205        3169 :             s->sh.chroma_weight_l0[i][1] = 1 << s->sh.chroma_log2_weight_denom;
     206        3169 :             s->sh.chroma_offset_l0[i][1] = 0;
     207             :         }
     208             :     }
     209        1350 :     if (s->sh.slice_type == HEVC_SLICE_B) {
     210        1839 :         for (i = 0; i < s->sh.nb_refs[L1]; i++) {
     211        1256 :             luma_weight_l1_flag[i] = get_bits1(gb);
     212        1256 :             if (!luma_weight_l1_flag[i]) {
     213         510 :                 s->sh.luma_weight_l1[i] = 1 << s->sh.luma_log2_weight_denom;
     214         510 :                 s->sh.luma_offset_l1[i] = 0;
     215             :             }
     216             :         }
     217         583 :         if (s->ps.sps->chroma_format_idc != 0) {
     218        1839 :             for (i = 0; i < s->sh.nb_refs[L1]; i++)
     219        1256 :                 chroma_weight_l1_flag[i] = get_bits1(gb);
     220             :         } else {
     221           0 :             for (i = 0; i < s->sh.nb_refs[L1]; i++)
     222           0 :                 chroma_weight_l1_flag[i] = 0;
     223             :         }
     224        1839 :         for (i = 0; i < s->sh.nb_refs[L1]; i++) {
     225        1256 :             if (luma_weight_l1_flag[i]) {
     226         746 :                 int delta_luma_weight_l1 = get_se_golomb(gb);
     227         746 :                 s->sh.luma_weight_l1[i] = (1 << s->sh.luma_log2_weight_denom) + delta_luma_weight_l1;
     228         746 :                 s->sh.luma_offset_l1[i] = get_se_golomb(gb);
     229             :             }
     230        1256 :             if (chroma_weight_l1_flag[i]) {
     231         708 :                 for (j = 0; j < 2; j++) {
     232         472 :                     int delta_chroma_weight_l1 = get_se_golomb(gb);
     233         472 :                     int delta_chroma_offset_l1 = get_se_golomb(gb);
     234             : 
     235         472 :                     if (   (int8_t)delta_chroma_weight_l1 != delta_chroma_weight_l1
     236         472 :                         || delta_chroma_offset_l1 < -(1<<17) || delta_chroma_offset_l1 > (1<<17)) {
     237           0 :                         return AVERROR_INVALIDDATA;
     238             :                     }
     239             : 
     240         472 :                     s->sh.chroma_weight_l1[i][j] = (1 << s->sh.chroma_log2_weight_denom) + delta_chroma_weight_l1;
     241         944 :                     s->sh.chroma_offset_l1[i][j] = av_clip((delta_chroma_offset_l1 - ((128 * s->sh.chroma_weight_l1[i][j])
     242         472 :                                                                                         >> s->sh.chroma_log2_weight_denom) + 128), -128, 127);
     243             :                 }
     244             :             } else {
     245        1020 :                 s->sh.chroma_weight_l1[i][0] = 1 << s->sh.chroma_log2_weight_denom;
     246        1020 :                 s->sh.chroma_offset_l1[i][0] = 0;
     247        1020 :                 s->sh.chroma_weight_l1[i][1] = 1 << s->sh.chroma_log2_weight_denom;
     248        1020 :                 s->sh.chroma_offset_l1[i][1] = 0;
     249             :             }
     250             :         }
     251             :     }
     252        1350 :     return 0;
     253             : }
     254             : 
     255       18932 : static int decode_lt_rps(HEVCContext *s, LongTermRPS *rps, GetBitContext *gb)
     256             : {
     257       18932 :     const HEVCSPS *sps = s->ps.sps;
     258       18932 :     int max_poc_lsb    = 1 << sps->log2_max_poc_lsb;
     259       18932 :     int prev_delta_msb = 0;
     260       18932 :     unsigned int nb_sps = 0, nb_sh;
     261             :     int i;
     262             : 
     263       18932 :     rps->nb_refs = 0;
     264       18932 :     if (!sps->long_term_ref_pics_present_flag)
     265       17927 :         return 0;
     266             : 
     267        1005 :     if (sps->num_long_term_ref_pics_sps > 0)
     268         499 :         nb_sps = get_ue_golomb_long(gb);
     269        1005 :     nb_sh = get_ue_golomb_long(gb);
     270             : 
     271        1005 :     if (nb_sps > sps->num_long_term_ref_pics_sps)
     272           0 :         return AVERROR_INVALIDDATA;
     273        1005 :     if (nb_sh + (uint64_t)nb_sps > FF_ARRAY_ELEMS(rps->poc))
     274           0 :         return AVERROR_INVALIDDATA;
     275             : 
     276        1005 :     rps->nb_refs = nb_sh + nb_sps;
     277             : 
     278        2536 :     for (i = 0; i < rps->nb_refs; i++) {
     279             :         uint8_t delta_poc_msb_present;
     280             : 
     281        1531 :         if (i < nb_sps) {
     282         184 :             uint8_t lt_idx_sps = 0;
     283             : 
     284         184 :             if (sps->num_long_term_ref_pics_sps > 1)
     285         184 :                 lt_idx_sps = get_bits(gb, av_ceil_log2(sps->num_long_term_ref_pics_sps));
     286             : 
     287         184 :             rps->poc[i]  = sps->lt_ref_pic_poc_lsb_sps[lt_idx_sps];
     288         184 :             rps->used[i] = sps->used_by_curr_pic_lt_sps_flag[lt_idx_sps];
     289             :         } else {
     290        1347 :             rps->poc[i]  = get_bits(gb, sps->log2_max_poc_lsb);
     291        1347 :             rps->used[i] = get_bits1(gb);
     292             :         }
     293             : 
     294        1531 :         delta_poc_msb_present = get_bits1(gb);
     295        1531 :         if (delta_poc_msb_present) {
     296          48 :             int64_t delta = get_ue_golomb_long(gb);
     297             :             int64_t poc;
     298             : 
     299          48 :             if (i && i != nb_sps)
     300          31 :                 delta += prev_delta_msb;
     301             : 
     302          48 :             poc = rps->poc[i] + s->poc - delta * max_poc_lsb - s->sh.pic_order_cnt_lsb;
     303          48 :             if (poc != (int32_t)poc)
     304           0 :                 return AVERROR_INVALIDDATA;
     305          48 :             rps->poc[i] = poc;
     306          48 :             prev_delta_msb = delta;
     307             :         }
     308             :     }
     309             : 
     310        1005 :     return 0;
     311             : }
     312             : 
     313         533 : static void export_stream_params(AVCodecContext *avctx, const HEVCParamSets *ps,
     314             :                                  const HEVCSPS *sps)
     315             : {
     316         533 :     const HEVCVPS *vps = (const HEVCVPS*)ps->vps_list[sps->vps_id]->data;
     317         533 :     const HEVCWindow *ow = &sps->output_window;
     318         533 :     unsigned int num = 0, den = 0;
     319             : 
     320         533 :     avctx->pix_fmt             = sps->pix_fmt;
     321         533 :     avctx->coded_width         = sps->width;
     322         533 :     avctx->coded_height        = sps->height;
     323         533 :     avctx->width               = sps->width  - ow->left_offset - ow->right_offset;
     324         533 :     avctx->height              = sps->height - ow->top_offset  - ow->bottom_offset;
     325         533 :     avctx->has_b_frames        = sps->temporal_layer[sps->max_sub_layers - 1].num_reorder_pics;
     326         533 :     avctx->profile             = sps->ptl.general_ptl.profile_idc;
     327         533 :     avctx->level               = sps->ptl.general_ptl.level_idc;
     328             : 
     329         533 :     ff_set_sar(avctx, sps->vui.sar);
     330             : 
     331         533 :     if (sps->vui.video_signal_type_present_flag)
     332           8 :         avctx->color_range = sps->vui.video_full_range_flag ? AVCOL_RANGE_JPEG
     333           4 :                                                             : AVCOL_RANGE_MPEG;
     334             :     else
     335         529 :         avctx->color_range = AVCOL_RANGE_MPEG;
     336             : 
     337         533 :     if (sps->vui.colour_description_present_flag) {
     338           4 :         avctx->color_primaries = sps->vui.colour_primaries;
     339           4 :         avctx->color_trc       = sps->vui.transfer_characteristic;
     340           4 :         avctx->colorspace      = sps->vui.matrix_coeffs;
     341             :     } else {
     342         529 :         avctx->color_primaries = AVCOL_PRI_UNSPECIFIED;
     343         529 :         avctx->color_trc       = AVCOL_TRC_UNSPECIFIED;
     344         529 :         avctx->colorspace      = AVCOL_SPC_UNSPECIFIED;
     345             :     }
     346             : 
     347         533 :     if (vps->vps_timing_info_present_flag) {
     348          16 :         num = vps->vps_num_units_in_tick;
     349          16 :         den = vps->vps_time_scale;
     350         517 :     } else if (sps->vui.vui_timing_info_present_flag) {
     351          21 :         num = sps->vui.vui_num_units_in_tick;
     352          21 :         den = sps->vui.vui_time_scale;
     353             :     }
     354             : 
     355         533 :     if (num != 0 && den != 0)
     356          37 :         av_reduce(&avctx->framerate.den, &avctx->framerate.num,
     357             :                   num, den, 1 << 30);
     358         533 : }
     359             : 
     360         361 : static enum AVPixelFormat get_format(HEVCContext *s, const HEVCSPS *sps)
     361             : {
     362             : #define HWACCEL_MAX (CONFIG_HEVC_DXVA2_HWACCEL + \
     363             :                      CONFIG_HEVC_D3D11VA_HWACCEL * 2 + \
     364             :                      CONFIG_HEVC_NVDEC_HWACCEL + \
     365             :                      CONFIG_HEVC_VAAPI_HWACCEL + \
     366             :                      CONFIG_HEVC_VIDEOTOOLBOX_HWACCEL + \
     367             :                      CONFIG_HEVC_VDPAU_HWACCEL)
     368         361 :     enum AVPixelFormat pix_fmts[HWACCEL_MAX + 2], *fmt = pix_fmts;
     369             : 
     370         361 :     switch (sps->pix_fmt) {
     371         317 :     case AV_PIX_FMT_YUV420P:
     372             :     case AV_PIX_FMT_YUVJ420P:
     373             : #if CONFIG_HEVC_DXVA2_HWACCEL
     374             :         *fmt++ = AV_PIX_FMT_DXVA2_VLD;
     375             : #endif
     376             : #if CONFIG_HEVC_D3D11VA_HWACCEL
     377             :         *fmt++ = AV_PIX_FMT_D3D11VA_VLD;
     378             :         *fmt++ = AV_PIX_FMT_D3D11;
     379             : #endif
     380             : #if CONFIG_HEVC_VAAPI_HWACCEL
     381             :         *fmt++ = AV_PIX_FMT_VAAPI;
     382             : #endif
     383             : #if CONFIG_HEVC_VDPAU_HWACCEL
     384             :         *fmt++ = AV_PIX_FMT_VDPAU;
     385             : #endif
     386             : #if CONFIG_HEVC_NVDEC_HWACCEL
     387             :         *fmt++ = AV_PIX_FMT_CUDA;
     388             : #endif
     389             : #if CONFIG_HEVC_VIDEOTOOLBOX_HWACCEL
     390             :         *fmt++ = AV_PIX_FMT_VIDEOTOOLBOX;
     391             : #endif
     392         317 :         break;
     393          22 :     case AV_PIX_FMT_YUV420P10:
     394             : #if CONFIG_HEVC_DXVA2_HWACCEL
     395             :         *fmt++ = AV_PIX_FMT_DXVA2_VLD;
     396             : #endif
     397             : #if CONFIG_HEVC_D3D11VA_HWACCEL
     398             :         *fmt++ = AV_PIX_FMT_D3D11VA_VLD;
     399             :         *fmt++ = AV_PIX_FMT_D3D11;
     400             : #endif
     401             : #if CONFIG_HEVC_VAAPI_HWACCEL
     402             :         *fmt++ = AV_PIX_FMT_VAAPI;
     403             : #endif
     404             : #if CONFIG_HEVC_VIDEOTOOLBOX_HWACCEL
     405             :         *fmt++ = AV_PIX_FMT_VIDEOTOOLBOX;
     406             : #endif
     407             : #if CONFIG_HEVC_NVDEC_HWACCEL
     408             :         *fmt++ = AV_PIX_FMT_CUDA;
     409             : #endif
     410          22 :         break;
     411           0 :     case AV_PIX_FMT_YUV420P12:
     412             : #if CONFIG_HEVC_NVDEC_HWACCEL
     413             :         *fmt++ = AV_PIX_FMT_CUDA;
     414             : #endif
     415           0 :         break;
     416             :     }
     417             : 
     418         361 :     *fmt++ = sps->pix_fmt;
     419         361 :     *fmt = AV_PIX_FMT_NONE;
     420             : 
     421         361 :     return ff_thread_get_format(s->avctx, pix_fmts);
     422             : }
     423             : 
     424         361 : static int set_sps(HEVCContext *s, const HEVCSPS *sps,
     425             :                    enum AVPixelFormat pix_fmt)
     426             : {
     427             :     int ret, i;
     428             : 
     429         361 :     pic_arrays_free(s);
     430         361 :     s->ps.sps = NULL;
     431         361 :     s->ps.vps = NULL;
     432             : 
     433         361 :     if (!sps)
     434           0 :         return 0;
     435             : 
     436         361 :     ret = pic_arrays_init(s, sps);
     437         361 :     if (ret < 0)
     438           0 :         goto fail;
     439             : 
     440         361 :     export_stream_params(s->avctx, &s->ps, sps);
     441             : 
     442         361 :     s->avctx->pix_fmt = pix_fmt;
     443             : 
     444         361 :     ff_hevc_pred_init(&s->hpc,     sps->bit_depth);
     445         361 :     ff_hevc_dsp_init (&s->hevcdsp, sps->bit_depth);
     446         361 :     ff_videodsp_init (&s->vdsp,    sps->bit_depth);
     447             : 
     448        1444 :     for (i = 0; i < 3; i++) {
     449        1083 :         av_freep(&s->sao_pixel_buffer_h[i]);
     450        1083 :         av_freep(&s->sao_pixel_buffer_v[i]);
     451             :     }
     452             : 
     453         361 :     if (sps->sao_enabled && !s->avctx->hwaccel) {
     454         308 :         int c_count = (sps->chroma_format_idc != 0) ? 3 : 1;
     455             :         int c_idx;
     456             : 
     457        1232 :         for(c_idx = 0; c_idx < c_count; c_idx++) {
     458         924 :             int w = sps->width >> sps->hshift[c_idx];
     459         924 :             int h = sps->height >> sps->vshift[c_idx];
     460         924 :             s->sao_pixel_buffer_h[c_idx] =
     461        1848 :                 av_malloc((w * 2 * sps->ctb_height) <<
     462         924 :                           sps->pixel_shift);
     463         924 :             s->sao_pixel_buffer_v[c_idx] =
     464        1848 :                 av_malloc((h * 2 * sps->ctb_width) <<
     465         924 :                           sps->pixel_shift);
     466             :         }
     467             :     }
     468             : 
     469         361 :     s->ps.sps = sps;
     470         361 :     s->ps.vps = (HEVCVPS*) s->ps.vps_list[s->ps.sps->vps_id]->data;
     471             : 
     472         361 :     return 0;
     473             : 
     474           0 : fail:
     475           0 :     pic_arrays_free(s);
     476           0 :     s->ps.sps = NULL;
     477           0 :     return ret;
     478             : }
     479             : 
     480       27475 : static int hls_slice_header(HEVCContext *s)
     481             : {
     482       27475 :     GetBitContext *gb = &s->HEVClc->gb;
     483       27475 :     SliceHeader *sh   = &s->sh;
     484             :     int i, ret;
     485             : 
     486             :     // Coded parameters
     487       27475 :     sh->first_slice_in_pic_flag = get_bits1(gb);
     488       27475 :     if ((IS_IDR(s) || IS_BLA(s)) && sh->first_slice_in_pic_flag) {
     489         370 :         s->seq_decode = (s->seq_decode + 1) & 0xff;
     490         370 :         s->max_ra     = INT_MAX;
     491         370 :         if (IS_IDR(s))
     492         360 :             ff_hevc_clear_refs(s);
     493             :     }
     494       27475 :     sh->no_output_of_prior_pics_flag = 0;
     495       27475 :     if (IS_IRAP(s))
     496        1246 :         sh->no_output_of_prior_pics_flag = get_bits1(gb);
     497             : 
     498       27475 :     sh->pps_id = get_ue_golomb_long(gb);
     499       27475 :     if (sh->pps_id >= HEVC_MAX_PPS_COUNT || !s->ps.pps_list[sh->pps_id]) {
     500          22 :         av_log(s->avctx, AV_LOG_ERROR, "PPS id out of range: %d\n", sh->pps_id);
     501          22 :         return AVERROR_INVALIDDATA;
     502             :     }
     503       45434 :     if (!sh->first_slice_in_pic_flag &&
     504       17981 :         s->ps.pps != (HEVCPPS*)s->ps.pps_list[sh->pps_id]->data) {
     505           0 :         av_log(s->avctx, AV_LOG_ERROR, "PPS changed between slices.\n");
     506           0 :         return AVERROR_INVALIDDATA;
     507             :     }
     508       27453 :     s->ps.pps = (HEVCPPS*)s->ps.pps_list[sh->pps_id]->data;
     509       27453 :     if (s->nal_unit_type == HEVC_NAL_CRA_NUT && s->last_eos == 1)
     510           6 :         sh->no_output_of_prior_pics_flag = 1;
     511             : 
     512       27453 :     if (s->ps.sps != (HEVCSPS*)s->ps.sps_list[s->ps.pps->sps_id]->data) {
     513         361 :         const HEVCSPS *sps = (HEVCSPS*)s->ps.sps_list[s->ps.pps->sps_id]->data;
     514         361 :         const HEVCSPS *last_sps = s->ps.sps;
     515             :         enum AVPixelFormat pix_fmt;
     516             : 
     517         361 :         if (last_sps && IS_IRAP(s) && s->nal_unit_type != HEVC_NAL_CRA_NUT) {
     518           4 :             if (sps->width != last_sps->width || sps->height != last_sps->height ||
     519           2 :                 sps->temporal_layer[sps->max_sub_layers - 1].max_dec_pic_buffering !=
     520           2 :                 last_sps->temporal_layer[last_sps->max_sub_layers - 1].max_dec_pic_buffering)
     521           0 :                 sh->no_output_of_prior_pics_flag = 0;
     522             :         }
     523         361 :         ff_hevc_clear_refs(s);
     524             : 
     525         361 :         ret = set_sps(s, sps, sps->pix_fmt);
     526         361 :         if (ret < 0)
     527           0 :             return ret;
     528             : 
     529         361 :         pix_fmt = get_format(s, sps);
     530         361 :         if (pix_fmt < 0)
     531           0 :             return pix_fmt;
     532         361 :         s->avctx->pix_fmt = pix_fmt;
     533             : 
     534         361 :         s->seq_decode = (s->seq_decode + 1) & 0xff;
     535         361 :         s->max_ra     = INT_MAX;
     536             :     }
     537             : 
     538       27453 :     sh->dependent_slice_segment_flag = 0;
     539       27453 :     if (!sh->first_slice_in_pic_flag) {
     540             :         int slice_address_length;
     541             : 
     542       17981 :         if (s->ps.pps->dependent_slice_segments_enabled_flag)
     543        9092 :             sh->dependent_slice_segment_flag = get_bits1(gb);
     544             : 
     545       35962 :         slice_address_length = av_ceil_log2(s->ps.sps->ctb_width *
     546       17981 :                                             s->ps.sps->ctb_height);
     547       17981 :         sh->slice_segment_addr = get_bitsz(gb, slice_address_length);
     548       17981 :         if (sh->slice_segment_addr >= s->ps.sps->ctb_width * s->ps.sps->ctb_height) {
     549           0 :             av_log(s->avctx, AV_LOG_ERROR,
     550             :                    "Invalid slice segment address: %u.\n",
     551             :                    sh->slice_segment_addr);
     552           0 :             return AVERROR_INVALIDDATA;
     553             :         }
     554             : 
     555       17981 :         if (!sh->dependent_slice_segment_flag) {
     556       10034 :             sh->slice_addr = sh->slice_segment_addr;
     557       10034 :             s->slice_idx++;
     558             :         }
     559             :     } else {
     560        9472 :         sh->slice_segment_addr = sh->slice_addr = 0;
     561        9472 :         s->slice_idx           = 0;
     562        9472 :         s->slice_initialized   = 0;
     563             :     }
     564             : 
     565       27453 :     if (!sh->dependent_slice_segment_flag) {
     566       19506 :         s->slice_initialized = 0;
     567             : 
     568       19510 :         for (i = 0; i < s->ps.pps->num_extra_slice_header_bits; i++)
     569           4 :             skip_bits(gb, 1);  // slice_reserved_undetermined_flag[]
     570             : 
     571       19506 :         sh->slice_type = get_ue_golomb_long(gb);
     572       52591 :         if (!(sh->slice_type == HEVC_SLICE_I ||
     573       18398 :               sh->slice_type == HEVC_SLICE_P ||
     574       14687 :               sh->slice_type == HEVC_SLICE_B)) {
     575           0 :             av_log(s->avctx, AV_LOG_ERROR, "Unknown slice type: %d.\n",
     576           0 :                    sh->slice_type);
     577           0 :             return AVERROR_INVALIDDATA;
     578             :         }
     579       19506 :         if (IS_IRAP(s) && sh->slice_type != HEVC_SLICE_I) {
     580           0 :             av_log(s->avctx, AV_LOG_ERROR, "Inter slices in an IRAP frame.\n");
     581           0 :             return AVERROR_INVALIDDATA;
     582             :         }
     583             : 
     584             :         // when flag is not present, picture is inferred to be output
     585       19506 :         sh->pic_output_flag = 1;
     586       19506 :         if (s->ps.pps->output_flag_present_flag)
     587         703 :             sh->pic_output_flag = get_bits1(gb);
     588             : 
     589       19506 :         if (s->ps.sps->separate_colour_plane_flag)
     590           0 :             sh->colour_plane_id = get_bits(gb, 2);
     591             : 
     592       38438 :         if (!IS_IDR(s)) {
     593             :             int poc, pos;
     594             : 
     595       18932 :             sh->pic_order_cnt_lsb = get_bits(gb, s->ps.sps->log2_max_poc_lsb);
     596       18932 :             poc = ff_hevc_compute_poc(s->ps.sps, s->pocTid0, sh->pic_order_cnt_lsb, s->nal_unit_type);
     597       18932 :             if (!sh->first_slice_in_pic_flag && poc != s->poc) {
     598           0 :                 av_log(s->avctx, AV_LOG_WARNING,
     599             :                        "Ignoring POC change between slices: %d -> %d\n", s->poc, poc);
     600           0 :                 if (s->avctx->err_recognition & AV_EF_EXPLODE)
     601           0 :                     return AVERROR_INVALIDDATA;
     602           0 :                 poc = s->poc;
     603             :             }
     604       18932 :             s->poc = poc;
     605             : 
     606       18932 :             sh->short_term_ref_pic_set_sps_flag = get_bits1(gb);
     607       18932 :             pos = get_bits_left(gb);
     608       18932 :             if (!sh->short_term_ref_pic_set_sps_flag) {
     609        3120 :                 ret = ff_hevc_decode_short_term_rps(gb, s->avctx, &sh->slice_rps, s->ps.sps, 1);
     610        3120 :                 if (ret < 0)
     611           0 :                     return ret;
     612             : 
     613        3120 :                 sh->short_term_rps = &sh->slice_rps;
     614             :             } else {
     615             :                 int numbits, rps_idx;
     616             : 
     617       15812 :                 if (!s->ps.sps->nb_st_rps) {
     618           0 :                     av_log(s->avctx, AV_LOG_ERROR, "No ref lists in the SPS.\n");
     619           0 :                     return AVERROR_INVALIDDATA;
     620             :                 }
     621             : 
     622       15812 :                 numbits = av_ceil_log2(s->ps.sps->nb_st_rps);
     623       15812 :                 rps_idx = numbits > 0 ? get_bits(gb, numbits) : 0;
     624       15812 :                 sh->short_term_rps = &s->ps.sps->st_rps[rps_idx];
     625             :             }
     626       18932 :             sh->short_term_ref_pic_set_size = pos - get_bits_left(gb);
     627             : 
     628       18932 :             pos = get_bits_left(gb);
     629       18932 :             ret = decode_lt_rps(s, &sh->long_term_rps, gb);
     630       18932 :             if (ret < 0) {
     631           0 :                 av_log(s->avctx, AV_LOG_WARNING, "Invalid long term RPS.\n");
     632           0 :                 if (s->avctx->err_recognition & AV_EF_EXPLODE)
     633           0 :                     return AVERROR_INVALIDDATA;
     634             :             }
     635       18932 :             sh->long_term_ref_pic_set_size = pos - get_bits_left(gb);
     636             : 
     637       18932 :             if (s->ps.sps->sps_temporal_mvp_enabled_flag)
     638       16501 :                 sh->slice_temporal_mvp_enabled_flag = get_bits1(gb);
     639             :             else
     640        2431 :                 sh->slice_temporal_mvp_enabled_flag = 0;
     641             :         } else {
     642         574 :             s->sh.short_term_rps = NULL;
     643         574 :             s->poc               = 0;
     644             :         }
     645             : 
     646             :         /* 8.3.1 */
     647       28658 :         if (sh->first_slice_in_pic_flag && s->temporal_id == 0 &&
     648       15845 :             s->nal_unit_type != HEVC_NAL_TRAIL_N &&
     649       13386 :             s->nal_unit_type != HEVC_NAL_TSA_N   &&
     650       13386 :             s->nal_unit_type != HEVC_NAL_STSA_N  &&
     651       13364 :             s->nal_unit_type != HEVC_NAL_RADL_N  &&
     652       13323 :             s->nal_unit_type != HEVC_NAL_RADL_R  &&
     653       12954 :             s->nal_unit_type != HEVC_NAL_RASL_N  &&
     654        6302 :             s->nal_unit_type != HEVC_NAL_RASL_R)
     655        5840 :             s->pocTid0 = s->poc;
     656             : 
     657       19506 :         if (s->ps.sps->sao_enabled) {
     658       16057 :             sh->slice_sample_adaptive_offset_flag[0] = get_bits1(gb);
     659       16057 :             if (s->ps.sps->chroma_format_idc) {
     660       16057 :                 sh->slice_sample_adaptive_offset_flag[1] =
     661       16057 :                 sh->slice_sample_adaptive_offset_flag[2] = get_bits1(gb);
     662             :             }
     663             :         } else {
     664        3449 :             sh->slice_sample_adaptive_offset_flag[0] = 0;
     665        3449 :             sh->slice_sample_adaptive_offset_flag[1] = 0;
     666        3449 :             sh->slice_sample_adaptive_offset_flag[2] = 0;
     667             :         }
     668             : 
     669       19506 :         sh->nb_refs[L0] = sh->nb_refs[L1] = 0;
     670       19506 :         if (sh->slice_type == HEVC_SLICE_P || sh->slice_type == HEVC_SLICE_B) {
     671             :             int nb_refs;
     672             : 
     673       18398 :             sh->nb_refs[L0] = s->ps.pps->num_ref_idx_l0_default_active;
     674       18398 :             if (sh->slice_type == HEVC_SLICE_B)
     675       14687 :                 sh->nb_refs[L1] = s->ps.pps->num_ref_idx_l1_default_active;
     676             : 
     677       18398 :             if (get_bits1(gb)) { // num_ref_idx_active_override_flag
     678        4860 :                 sh->nb_refs[L0] = get_ue_golomb_long(gb) + 1;
     679        4860 :                 if (sh->slice_type == HEVC_SLICE_B)
     680        2699 :                     sh->nb_refs[L1] = get_ue_golomb_long(gb) + 1;
     681             :             }
     682       18398 :             if (sh->nb_refs[L0] > HEVC_MAX_REFS || sh->nb_refs[L1] > HEVC_MAX_REFS) {
     683           0 :                 av_log(s->avctx, AV_LOG_ERROR, "Too many refs: %d/%d.\n",
     684             :                        sh->nb_refs[L0], sh->nb_refs[L1]);
     685           0 :                 return AVERROR_INVALIDDATA;
     686             :             }
     687             : 
     688       18398 :             sh->rpl_modification_flag[0] = 0;
     689       18398 :             sh->rpl_modification_flag[1] = 0;
     690       18398 :             nb_refs = ff_hevc_frame_nb_refs(s);
     691       18398 :             if (!nb_refs) {
     692           0 :                 av_log(s->avctx, AV_LOG_ERROR, "Zero refs for a frame with P or B slices.\n");
     693           0 :                 return AVERROR_INVALIDDATA;
     694             :             }
     695             : 
     696       18398 :             if (s->ps.pps->lists_modification_present_flag && nb_refs > 1) {
     697        1596 :                 sh->rpl_modification_flag[0] = get_bits1(gb);
     698        1596 :                 if (sh->rpl_modification_flag[0]) {
     699        3955 :                     for (i = 0; i < sh->nb_refs[L0]; i++)
     700        2779 :                         sh->list_entry_lx[0][i] = get_bits(gb, av_ceil_log2(nb_refs));
     701             :                 }
     702             : 
     703        1596 :                 if (sh->slice_type == HEVC_SLICE_B) {
     704        1596 :                     sh->rpl_modification_flag[1] = get_bits1(gb);
     705        1596 :                     if (sh->rpl_modification_flag[1] == 1)
     706        2207 :                         for (i = 0; i < sh->nb_refs[L1]; i++)
     707        1555 :                             sh->list_entry_lx[1][i] = get_bits(gb, av_ceil_log2(nb_refs));
     708             :                 }
     709             :             }
     710             : 
     711       18398 :             if (sh->slice_type == HEVC_SLICE_B)
     712       14687 :                 sh->mvd_l1_zero_flag = get_bits1(gb);
     713             : 
     714       18398 :             if (s->ps.pps->cabac_init_present_flag)
     715       15763 :                 sh->cabac_init_flag = get_bits1(gb);
     716             :             else
     717        2635 :                 sh->cabac_init_flag = 0;
     718             : 
     719       18398 :             sh->collocated_ref_idx = 0;
     720       18398 :             if (sh->slice_temporal_mvp_enabled_flag) {
     721       16061 :                 sh->collocated_list = L0;
     722       16061 :                 if (sh->slice_type == HEVC_SLICE_B)
     723       14185 :                     sh->collocated_list = !get_bits1(gb);
     724             : 
     725       16061 :                 if (sh->nb_refs[sh->collocated_list] > 1) {
     726       14395 :                     sh->collocated_ref_idx = get_ue_golomb_long(gb);
     727       14395 :                     if (sh->collocated_ref_idx >= sh->nb_refs[sh->collocated_list]) {
     728           0 :                         av_log(s->avctx, AV_LOG_ERROR,
     729             :                                "Invalid collocated_ref_idx: %d.\n",
     730             :                                sh->collocated_ref_idx);
     731           0 :                         return AVERROR_INVALIDDATA;
     732             :                     }
     733             :                 }
     734             :             }
     735             : 
     736       36029 :             if ((s->ps.pps->weighted_pred_flag   && sh->slice_type == HEVC_SLICE_P) ||
     737       18214 :                 (s->ps.pps->weighted_bipred_flag && sh->slice_type == HEVC_SLICE_B)) {
     738        1350 :                 int ret = pred_weight_table(s, gb);
     739        1350 :                 if (ret < 0)
     740           0 :                     return ret;
     741             :             }
     742             : 
     743       18398 :             sh->max_num_merge_cand = 5 - get_ue_golomb_long(gb);
     744       18398 :             if (sh->max_num_merge_cand < 1 || sh->max_num_merge_cand > 5) {
     745           0 :                 av_log(s->avctx, AV_LOG_ERROR,
     746             :                        "Invalid number of merging MVP candidates: %d.\n",
     747             :                        sh->max_num_merge_cand);
     748           0 :                 return AVERROR_INVALIDDATA;
     749             :             }
     750             :         }
     751             : 
     752       19506 :         sh->slice_qp_delta = get_se_golomb(gb);
     753             : 
     754       19506 :         if (s->ps.pps->pic_slice_level_chroma_qp_offsets_present_flag) {
     755         250 :             sh->slice_cb_qp_offset = get_se_golomb(gb);
     756         250 :             sh->slice_cr_qp_offset = get_se_golomb(gb);
     757             :         } else {
     758       19256 :             sh->slice_cb_qp_offset = 0;
     759       19256 :             sh->slice_cr_qp_offset = 0;
     760             :         }
     761             : 
     762       19506 :         if (s->ps.pps->chroma_qp_offset_list_enabled_flag)
     763          43 :             sh->cu_chroma_qp_offset_enabled_flag = get_bits1(gb);
     764             :         else
     765       19463 :             sh->cu_chroma_qp_offset_enabled_flag = 0;
     766             : 
     767       19506 :         if (s->ps.pps->deblocking_filter_control_present_flag) {
     768        2797 :             int deblocking_filter_override_flag = 0;
     769             : 
     770        2797 :             if (s->ps.pps->deblocking_filter_override_enabled_flag)
     771         577 :                 deblocking_filter_override_flag = get_bits1(gb);
     772             : 
     773        2797 :             if (deblocking_filter_override_flag) {
     774         358 :                 sh->disable_deblocking_filter_flag = get_bits1(gb);
     775         358 :                 if (!sh->disable_deblocking_filter_flag) {
     776         238 :                     int beta_offset_div2 = get_se_golomb(gb);
     777         238 :                     int tc_offset_div2   = get_se_golomb(gb) ;
     778         238 :                     if (beta_offset_div2 < -6 || beta_offset_div2 > 6 ||
     779         238 :                         tc_offset_div2   < -6 || tc_offset_div2   > 6) {
     780           0 :                         av_log(s->avctx, AV_LOG_ERROR,
     781             :                             "Invalid deblock filter offsets: %d, %d\n",
     782             :                             beta_offset_div2, tc_offset_div2);
     783           0 :                         return AVERROR_INVALIDDATA;
     784             :                     }
     785         238 :                     sh->beta_offset = beta_offset_div2 * 2;
     786         238 :                     sh->tc_offset   =   tc_offset_div2 * 2;
     787             :                 }
     788             :             } else {
     789        2439 :                 sh->disable_deblocking_filter_flag = s->ps.pps->disable_dbf;
     790        2439 :                 sh->beta_offset                    = s->ps.pps->beta_offset;
     791        2439 :                 sh->tc_offset                      = s->ps.pps->tc_offset;
     792             :             }
     793             :         } else {
     794       16709 :             sh->disable_deblocking_filter_flag = 0;
     795       16709 :             sh->beta_offset                    = 0;
     796       16709 :             sh->tc_offset                      = 0;
     797             :         }
     798             : 
     799       36479 :         if (s->ps.pps->seq_loop_filter_across_slices_enabled_flag &&
     800       28376 :             (sh->slice_sample_adaptive_offset_flag[0] ||
     801       22788 :              sh->slice_sample_adaptive_offset_flag[1] ||
     802       11385 :              !sh->disable_deblocking_filter_flag)) {
     803       16910 :             sh->slice_loop_filter_across_slices_enabled_flag = get_bits1(gb);
     804             :         } else {
     805        2596 :             sh->slice_loop_filter_across_slices_enabled_flag = s->ps.pps->seq_loop_filter_across_slices_enabled_flag;
     806             :         }
     807        7947 :     } else if (!s->slice_initialized) {
     808           0 :         av_log(s->avctx, AV_LOG_ERROR, "Independent slice segment missing.\n");
     809           0 :         return AVERROR_INVALIDDATA;
     810             :     }
     811             : 
     812       27453 :     sh->num_entry_point_offsets = 0;
     813       27453 :     if (s->ps.pps->tiles_enabled_flag || s->ps.pps->entropy_coding_sync_enabled_flag) {
     814        7409 :         unsigned num_entry_point_offsets = get_ue_golomb_long(gb);
     815             :         // It would be possible to bound this tighter but this here is simpler
     816        7409 :         if (num_entry_point_offsets > get_bits_left(gb)) {
     817           0 :             av_log(s->avctx, AV_LOG_ERROR, "num_entry_point_offsets %d is invalid\n", num_entry_point_offsets);
     818           0 :             return AVERROR_INVALIDDATA;
     819             :         }
     820             : 
     821        7409 :         sh->num_entry_point_offsets = num_entry_point_offsets;
     822        7409 :         if (sh->num_entry_point_offsets > 0) {
     823        1943 :             int offset_len = get_ue_golomb_long(gb) + 1;
     824             : 
     825        1943 :             if (offset_len < 1 || offset_len > 32) {
     826           0 :                 sh->num_entry_point_offsets = 0;
     827           0 :                 av_log(s->avctx, AV_LOG_ERROR, "offset_len %d is invalid\n", offset_len);
     828           0 :                 return AVERROR_INVALIDDATA;
     829             :             }
     830             : 
     831        1943 :             av_freep(&sh->entry_point_offset);
     832        1943 :             av_freep(&sh->offset);
     833        1943 :             av_freep(&sh->size);
     834        1943 :             sh->entry_point_offset = av_malloc_array(sh->num_entry_point_offsets, sizeof(unsigned));
     835        1943 :             sh->offset = av_malloc_array(sh->num_entry_point_offsets, sizeof(int));
     836        1943 :             sh->size = av_malloc_array(sh->num_entry_point_offsets, sizeof(int));
     837        1943 :             if (!sh->entry_point_offset || !sh->offset || !sh->size) {
     838           0 :                 sh->num_entry_point_offsets = 0;
     839           0 :                 av_log(s->avctx, AV_LOG_ERROR, "Failed to allocate memory\n");
     840           0 :                 return AVERROR(ENOMEM);
     841             :             }
     842        9517 :             for (i = 0; i < sh->num_entry_point_offsets; i++) {
     843        7574 :                 unsigned val = get_bits_long(gb, offset_len);
     844        7574 :                 sh->entry_point_offset[i] = val + 1; // +1; // +1 to get the size
     845             :             }
     846        1943 :             if (s->threads_number > 1 && (s->ps.pps->num_tile_rows > 1 || s->ps.pps->num_tile_columns > 1)) {
     847           0 :                 s->enable_parallel_tiles = 0; // TODO: you can enable tiles in parallel here
     848           0 :                 s->threads_number = 1;
     849             :             } else
     850        1943 :                 s->enable_parallel_tiles = 0;
     851             :         } else
     852        5466 :             s->enable_parallel_tiles = 0;
     853             :     }
     854             : 
     855       27453 :     if (s->ps.pps->slice_header_extension_present_flag) {
     856        2765 :         unsigned int length = get_ue_golomb_long(gb);
     857        2765 :         if (length*8LL > get_bits_left(gb)) {
     858           0 :             av_log(s->avctx, AV_LOG_ERROR, "too many slice_header_extension_data_bytes\n");
     859           0 :             return AVERROR_INVALIDDATA;
     860             :         }
     861       23029 :         for (i = 0; i < length; i++)
     862       20264 :             skip_bits(gb, 8);  // slice_header_extension_data_byte
     863             :     }
     864             : 
     865             :     // Inferred parameters
     866       27453 :     sh->slice_qp = 26U + s->ps.pps->pic_init_qp_minus26 + sh->slice_qp_delta;
     867       54906 :     if (sh->slice_qp > 51 ||
     868       27453 :         sh->slice_qp < -s->ps.sps->qp_bd_offset) {
     869           0 :         av_log(s->avctx, AV_LOG_ERROR,
     870             :                "The slice_qp %d is outside the valid range "
     871             :                "[%d, 51].\n",
     872           0 :                sh->slice_qp,
     873           0 :                -s->ps.sps->qp_bd_offset);
     874           0 :         return AVERROR_INVALIDDATA;
     875             :     }
     876             : 
     877       27453 :     sh->slice_ctb_addr_rs = sh->slice_segment_addr;
     878             : 
     879       27453 :     if (!s->sh.slice_ctb_addr_rs && s->sh.dependent_slice_segment_flag) {
     880           0 :         av_log(s->avctx, AV_LOG_ERROR, "Impossible slice segment.\n");
     881           0 :         return AVERROR_INVALIDDATA;
     882             :     }
     883             : 
     884       27453 :     if (get_bits_left(gb) < 0) {
     885           0 :         av_log(s->avctx, AV_LOG_ERROR,
     886           0 :                "Overread slice header by %d bits\n", -get_bits_left(gb));
     887           0 :         return AVERROR_INVALIDDATA;
     888             :     }
     889             : 
     890       27453 :     s->HEVClc->first_qp_group = !s->sh.dependent_slice_segment_flag;
     891             : 
     892       27453 :     if (!s->ps.pps->cu_qp_delta_enabled_flag)
     893       22559 :         s->HEVClc->qp_y = s->sh.slice_qp;
     894             : 
     895       27453 :     s->slice_initialized = 1;
     896       27453 :     s->HEVClc->tu.cu_qp_offset_cb = 0;
     897       27453 :     s->HEVClc->tu.cu_qp_offset_cr = 0;
     898             : 
     899       27453 :     return 0;
     900             : }
     901             : 
     902             : #define CTB(tab, x, y) ((tab)[(y) * s->ps.sps->ctb_width + (x)])
     903             : 
     904             : #define SET_SAO(elem, value)                            \
     905             : do {                                                    \
     906             :     if (!sao_merge_up_flag && !sao_merge_left_flag)     \
     907             :         sao->elem = value;                              \
     908             :     else if (sao_merge_left_flag)                       \
     909             :         sao->elem = CTB(s->sao, rx-1, ry).elem;         \
     910             :     else if (sao_merge_up_flag)                         \
     911             :         sao->elem = CTB(s->sao, rx, ry-1).elem;         \
     912             :     else                                                \
     913             :         sao->elem = 0;                                  \
     914             : } while (0)
     915             : 
     916     1361399 : static void hls_sao_param(HEVCContext *s, int rx, int ry)
     917             : {
     918     1361399 :     HEVCLocalContext *lc    = s->HEVClc;
     919     1361399 :     int sao_merge_left_flag = 0;
     920     1361399 :     int sao_merge_up_flag   = 0;
     921     1361399 :     SAOParams *sao          = &CTB(s->sao, rx, ry);
     922             :     int c_idx, i;
     923             : 
     924     2161249 :     if (s->sh.slice_sample_adaptive_offset_flag[0] ||
     925      799850 :         s->sh.slice_sample_adaptive_offset_flag[1]) {
     926      563193 :         if (rx > 0) {
     927      533337 :             if (lc->ctb_left_flag)
     928      524894 :                 sao_merge_left_flag = ff_hevc_sao_merge_flag_decode(s);
     929             :         }
     930      563193 :         if (ry > 0 && !sao_merge_left_flag) {
     931      254474 :             if (lc->ctb_up_flag)
     932      241126 :                 sao_merge_up_flag = ff_hevc_sao_merge_flag_decode(s);
     933             :         }
     934             :     }
     935             : 
     936     5445596 :     for (c_idx = 0; c_idx < (s->ps.sps->chroma_format_idc ? 3 : 1); c_idx++) {
     937     6806995 :         int log2_sao_offset_scale = c_idx == 0 ? s->ps.pps->log2_sao_offset_scale_luma :
     938     2722798 :                                                  s->ps.pps->log2_sao_offset_scale_chroma;
     939             : 
     940     4084197 :         if (!s->sh.slice_sample_adaptive_offset_flag[c_idx]) {
     941     2896526 :             sao->type_idx[c_idx] = SAO_NOT_APPLIED;
     942     2896526 :             continue;
     943             :         }
     944             : 
     945     1187671 :         if (c_idx == 2) {
     946      313061 :             sao->type_idx[2] = sao->type_idx[1];
     947      313061 :             sao->eo_class[2] = sao->eo_class[1];
     948             :         } else {
     949      874610 :             SET_SAO(type_idx[c_idx], ff_hevc_sao_type_idx_decode(s));
     950             :         }
     951             : 
     952     1187671 :         if (sao->type_idx[c_idx] == SAO_NOT_APPLIED)
     953      707303 :             continue;
     954             : 
     955     2401840 :         for (i = 0; i < 4; i++)
     956     1921472 :             SET_SAO(offset_abs[c_idx][i], ff_hevc_sao_offset_abs_decode(s));
     957             : 
     958      480368 :         if (sao->type_idx[c_idx] == SAO_BAND) {
     959      566260 :             for (i = 0; i < 4; i++) {
     960      453008 :                 if (sao->offset_abs[c_idx][i]) {
     961      342680 :                     SET_SAO(offset_sign[c_idx][i],
     962             :                             ff_hevc_sao_offset_sign_decode(s));
     963             :                 } else {
     964      110328 :                     sao->offset_sign[c_idx][i] = 0;
     965             :                 }
     966             :             }
     967      113252 :             SET_SAO(band_position[c_idx], ff_hevc_sao_band_position_decode(s));
     968      367116 :         } else if (c_idx != 2) {
     969      292444 :             SET_SAO(eo_class[c_idx], ff_hevc_sao_eo_class_decode(s));
     970             :         }
     971             : 
     972             :         // Inferred parameters
     973      480368 :         sao->offset_val[c_idx][0] = 0;
     974     2401840 :         for (i = 0; i < 4; i++) {
     975     1921472 :             sao->offset_val[c_idx][i + 1] = sao->offset_abs[c_idx][i];
     976     1921472 :             if (sao->type_idx[c_idx] == SAO_EDGE) {
     977     1468464 :                 if (i > 1)
     978      734232 :                     sao->offset_val[c_idx][i + 1] = -sao->offset_val[c_idx][i + 1];
     979      453008 :             } else if (sao->offset_sign[c_idx][i]) {
     980      150173 :                 sao->offset_val[c_idx][i + 1] = -sao->offset_val[c_idx][i + 1];
     981             :             }
     982     1921472 :             sao->offset_val[c_idx][i + 1] *= 1 << log2_sao_offset_scale;
     983             :         }
     984             :     }
     985     1361399 : }
     986             : 
     987             : #undef SET_SAO
     988             : #undef CTB
     989             : 
     990      384846 : static int hls_cross_component_pred(HEVCContext *s, int idx) {
     991      384846 :     HEVCLocalContext *lc    = s->HEVClc;
     992      384846 :     int log2_res_scale_abs_plus1 = ff_hevc_log2_res_scale_abs(s, idx);
     993             : 
     994      384846 :     if (log2_res_scale_abs_plus1 !=  0) {
     995      235843 :         int res_scale_sign_flag = ff_hevc_res_scale_sign_flag(s, idx);
     996      235843 :         lc->tu.res_scale_val = (1 << (log2_res_scale_abs_plus1 - 1)) *
     997      235843 :                                (1 - 2 * res_scale_sign_flag);
     998             :     } else {
     999      149003 :         lc->tu.res_scale_val = 0;
    1000             :     }
    1001             : 
    1002             : 
    1003      384846 :     return 0;
    1004             : }
    1005             : 
    1006    14914839 : static int hls_transform_unit(HEVCContext *s, int x0, int y0,
    1007             :                               int xBase, int yBase, int cb_xBase, int cb_yBase,
    1008             :                               int log2_cb_size, int log2_trafo_size,
    1009             :                               int blk_idx, int cbf_luma, int *cbf_cb, int *cbf_cr)
    1010             : {
    1011    14914839 :     HEVCLocalContext *lc = s->HEVClc;
    1012    14914839 :     const int log2_trafo_size_c = log2_trafo_size - s->ps.sps->hshift[1];
    1013             :     int i;
    1014             : 
    1015    14914839 :     if (lc->cu.pred_mode == MODE_INTRA) {
    1016     9361909 :         int trafo_size = 1 << log2_trafo_size;
    1017     9361909 :         ff_hevc_set_neighbour_available(s, x0, y0, trafo_size, trafo_size);
    1018             : 
    1019     9361909 :         s->hpc.intra_pred[log2_trafo_size - 2](s, x0, y0, 0);
    1020             :     }
    1021             : 
    1022    19360207 :     if (cbf_luma || cbf_cb[0] || cbf_cr[0] ||
    1023    15132325 :         (s->ps.sps->chroma_format_idc == 2 && (cbf_cb[1] || cbf_cr[1]))) {
    1024    10531260 :         int scan_idx   = SCAN_DIAG;
    1025    10531260 :         int scan_idx_c = SCAN_DIAG;
    1026    21195393 :         int cbf_chroma = cbf_cb[0] || cbf_cr[0] ||
    1027     5849339 :                          (s->ps.sps->chroma_format_idc == 2 &&
    1028      424150 :                          (cbf_cb[1] || cbf_cr[1]));
    1029             : 
    1030    10531260 :         if (s->ps.pps->cu_qp_delta_enabled_flag && !lc->tu.is_cu_qp_delta_coded) {
    1031      503539 :             lc->tu.cu_qp_delta = ff_hevc_cu_qp_delta_abs(s);
    1032      503539 :             if (lc->tu.cu_qp_delta != 0)
    1033      325375 :                 if (ff_hevc_cu_qp_delta_sign_flag(s) == 1)
    1034      179928 :                     lc->tu.cu_qp_delta = -lc->tu.cu_qp_delta;
    1035      503539 :             lc->tu.is_cu_qp_delta_coded = 1;
    1036             : 
    1037     1007078 :             if (lc->tu.cu_qp_delta < -(26 + s->ps.sps->qp_bd_offset / 2) ||
    1038      503539 :                 lc->tu.cu_qp_delta >  (25 + s->ps.sps->qp_bd_offset / 2)) {
    1039           0 :                 av_log(s->avctx, AV_LOG_ERROR,
    1040             :                        "The cu_qp_delta %d is outside the valid range "
    1041             :                        "[%d, %d].\n",
    1042             :                        lc->tu.cu_qp_delta,
    1043           0 :                        -(26 + s->ps.sps->qp_bd_offset / 2),
    1044           0 :                         (25 + s->ps.sps->qp_bd_offset / 2));
    1045           0 :                 return AVERROR_INVALIDDATA;
    1046             :             }
    1047             : 
    1048      503539 :             ff_hevc_set_qPy(s, cb_xBase, cb_yBase, log2_cb_size);
    1049             :         }
    1050             : 
    1051    11372315 :         if (s->sh.cu_chroma_qp_offset_enabled_flag && cbf_chroma &&
    1052     1682110 :             !lc->cu.cu_transquant_bypass_flag  &&  !lc->tu.is_cu_chroma_qp_offset_coded) {
    1053      266275 :             int cu_chroma_qp_offset_flag = ff_hevc_cu_chroma_qp_offset_flag(s);
    1054      266275 :             if (cu_chroma_qp_offset_flag) {
    1055       48966 :                 int cu_chroma_qp_offset_idx  = 0;
    1056       48966 :                 if (s->ps.pps->chroma_qp_offset_list_len_minus1 > 0) {
    1057           0 :                     cu_chroma_qp_offset_idx = ff_hevc_cu_chroma_qp_offset_idx(s);
    1058           0 :                     av_log(s->avctx, AV_LOG_ERROR,
    1059             :                         "cu_chroma_qp_offset_idx not yet tested.\n");
    1060             :                 }
    1061       48966 :                 lc->tu.cu_qp_offset_cb = s->ps.pps->cb_qp_offset_list[cu_chroma_qp_offset_idx];
    1062       48966 :                 lc->tu.cu_qp_offset_cr = s->ps.pps->cr_qp_offset_list[cu_chroma_qp_offset_idx];
    1063             :             } else {
    1064      217309 :                 lc->tu.cu_qp_offset_cb = 0;
    1065      217309 :                 lc->tu.cu_qp_offset_cr = 0;
    1066             :             }
    1067      266275 :             lc->tu.is_cu_chroma_qp_offset_coded = 1;
    1068             :         }
    1069             : 
    1070    10531260 :         if (lc->cu.pred_mode == MODE_INTRA && log2_trafo_size < 4) {
    1071    10156566 :             if (lc->tu.intra_pred_mode >= 6 &&
    1072     4090121 :                 lc->tu.intra_pred_mode <= 14) {
    1073     1476281 :                 scan_idx = SCAN_VERT;
    1074     6606679 :             } else if (lc->tu.intra_pred_mode >= 22 &&
    1075     2016515 :                        lc->tu.intra_pred_mode <= 30) {
    1076     1692939 :                 scan_idx = SCAN_HORIZ;
    1077             :             }
    1078             : 
    1079     9730390 :             if (lc->tu.intra_pred_mode_c >=  6 &&
    1080     3663945 :                 lc->tu.intra_pred_mode_c <= 14) {
    1081     1388074 :                 scan_idx_c = SCAN_VERT;
    1082     6592854 :             } else if (lc->tu.intra_pred_mode_c >= 22 &&
    1083     1914483 :                        lc->tu.intra_pred_mode_c <= 30) {
    1084     1600910 :                 scan_idx_c = SCAN_HORIZ;
    1085             :             }
    1086             :         }
    1087             : 
    1088    10531260 :         lc->tu.cross_pf = 0;
    1089             : 
    1090    10531260 :         if (cbf_luma)
    1091     9433593 :             ff_hevc_hls_residual_coding(s, x0, y0, log2_trafo_size, scan_idx, 0);
    1092    15272117 :         if (s->ps.sps->chroma_format_idc && (log2_trafo_size > 2 || s->ps.sps->chroma_format_idc == 3)) {
    1093     4740857 :             int trafo_size_h = 1 << (log2_trafo_size_c + s->ps.sps->hshift[1]);
    1094     4740857 :             int trafo_size_v = 1 << (log2_trafo_size_c + s->ps.sps->vshift[1]);
    1095     5151094 :             lc->tu.cross_pf  = (s->ps.pps->cross_component_prediction_enabled_flag && cbf_luma &&
    1096      386685 :                                 (lc->cu.pred_mode == MODE_INTER ||
    1097      168871 :                                  (lc->tu.chroma_mode_c ==  4)));
    1098             : 
    1099     4740857 :             if (lc->tu.cross_pf) {
    1100      192423 :                 hls_cross_component_pred(s, 0);
    1101             :             }
    1102     9859972 :             for (i = 0; i < (s->ps.sps->chroma_format_idc == 2 ? 2 : 1); i++) {
    1103     5119115 :                 if (lc->cu.pred_mode == MODE_INTRA) {
    1104     2772596 :                     ff_hevc_set_neighbour_available(s, x0, y0 + (i << log2_trafo_size_c), trafo_size_h, trafo_size_v);
    1105     2772596 :                     s->hpc.intra_pred[log2_trafo_size_c - 2](s, x0, y0 + (i << log2_trafo_size_c), 1);
    1106             :                 }
    1107     5119115 :                 if (cbf_cb[i])
    1108     1353351 :                     ff_hevc_hls_residual_coding(s, x0, y0 + (i << log2_trafo_size_c),
    1109             :                                                 log2_trafo_size_c, scan_idx_c, 1);
    1110             :                 else
    1111     3765764 :                     if (lc->tu.cross_pf) {
    1112       39658 :                         ptrdiff_t stride = s->frame->linesize[1];
    1113       39658 :                         int hshift = s->ps.sps->hshift[1];
    1114       39658 :                         int vshift = s->ps.sps->vshift[1];
    1115       39658 :                         int16_t *coeffs_y = (int16_t*)lc->edge_emu_buffer;
    1116       39658 :                         int16_t *coeffs   = (int16_t*)lc->edge_emu_buffer2;
    1117       39658 :                         int size = 1 << log2_trafo_size_c;
    1118             : 
    1119       79316 :                         uint8_t *dst = &s->frame->data[1][(y0 >> vshift) * stride +
    1120       39658 :                                                               ((x0 >> hshift) << s->ps.sps->pixel_shift)];
    1121     1871162 :                         for (i = 0; i < (size * size); i++) {
    1122     1831504 :                             coeffs[i] = ((lc->tu.res_scale_val * coeffs_y[i]) >> 3);
    1123             :                         }
    1124       39658 :                         s->hevcdsp.add_residual[log2_trafo_size_c-2](dst, coeffs, stride);
    1125             :                     }
    1126             :             }
    1127             : 
    1128     4740857 :             if (lc->tu.cross_pf) {
    1129      192423 :                 hls_cross_component_pred(s, 1);
    1130             :             }
    1131     9859972 :             for (i = 0; i < (s->ps.sps->chroma_format_idc == 2 ? 2 : 1); i++) {
    1132     5119115 :                 if (lc->cu.pred_mode == MODE_INTRA) {
    1133     2772596 :                     ff_hevc_set_neighbour_available(s, x0, y0 + (i << log2_trafo_size_c), trafo_size_h, trafo_size_v);
    1134     2772596 :                     s->hpc.intra_pred[log2_trafo_size_c - 2](s, x0, y0 + (i << log2_trafo_size_c), 2);
    1135             :                 }
    1136     5119115 :                 if (cbf_cr[i])
    1137     1481051 :                     ff_hevc_hls_residual_coding(s, x0, y0 + (i << log2_trafo_size_c),
    1138             :                                                 log2_trafo_size_c, scan_idx_c, 2);
    1139             :                 else
    1140     3638064 :                     if (lc->tu.cross_pf) {
    1141       75924 :                         ptrdiff_t stride = s->frame->linesize[2];
    1142       75924 :                         int hshift = s->ps.sps->hshift[2];
    1143       75924 :                         int vshift = s->ps.sps->vshift[2];
    1144       75924 :                         int16_t *coeffs_y = (int16_t*)lc->edge_emu_buffer;
    1145       75924 :                         int16_t *coeffs   = (int16_t*)lc->edge_emu_buffer2;
    1146       75924 :                         int size = 1 << log2_trafo_size_c;
    1147             : 
    1148      151848 :                         uint8_t *dst = &s->frame->data[2][(y0 >> vshift) * stride +
    1149       75924 :                                                           ((x0 >> hshift) << s->ps.sps->pixel_shift)];
    1150     3691476 :                         for (i = 0; i < (size * size); i++) {
    1151     3615552 :                             coeffs[i] = ((lc->tu.res_scale_val * coeffs_y[i]) >> 3);
    1152             :                         }
    1153       75924 :                         s->hevcdsp.add_residual[log2_trafo_size_c-2](dst, coeffs, stride);
    1154             :                     }
    1155             :             }
    1156     5790403 :         } else if (s->ps.sps->chroma_format_idc && blk_idx == 3) {
    1157     1487664 :             int trafo_size_h = 1 << (log2_trafo_size + 1);
    1158     1487664 :             int trafo_size_v = 1 << (log2_trafo_size + s->ps.sps->vshift[1]);
    1159     3142197 :             for (i = 0; i < (s->ps.sps->chroma_format_idc == 2 ? 2 : 1); i++) {
    1160     1654533 :                 if (lc->cu.pred_mode == MODE_INTRA) {
    1161     1174446 :                     ff_hevc_set_neighbour_available(s, xBase, yBase + (i << log2_trafo_size),
    1162             :                                                     trafo_size_h, trafo_size_v);
    1163     1174446 :                     s->hpc.intra_pred[log2_trafo_size - 2](s, xBase, yBase + (i << log2_trafo_size), 1);
    1164             :                 }
    1165     1654533 :                 if (cbf_cb[i])
    1166      631573 :                     ff_hevc_hls_residual_coding(s, xBase, yBase + (i << log2_trafo_size),
    1167             :                                                 log2_trafo_size, scan_idx_c, 1);
    1168             :             }
    1169     3142197 :             for (i = 0; i < (s->ps.sps->chroma_format_idc == 2 ? 2 : 1); i++) {
    1170     1654533 :                 if (lc->cu.pred_mode == MODE_INTRA) {
    1171     1174446 :                     ff_hevc_set_neighbour_available(s, xBase, yBase + (i << log2_trafo_size),
    1172             :                                                 trafo_size_h, trafo_size_v);
    1173     1174446 :                     s->hpc.intra_pred[log2_trafo_size - 2](s, xBase, yBase + (i << log2_trafo_size), 2);
    1174             :                 }
    1175     1654533 :                 if (cbf_cr[i])
    1176      712502 :                     ff_hevc_hls_residual_coding(s, xBase, yBase + (i << log2_trafo_size),
    1177             :                                                 log2_trafo_size, scan_idx_c, 2);
    1178             :             }
    1179             :         }
    1180     4383579 :     } else if (s->ps.sps->chroma_format_idc && lc->cu.pred_mode == MODE_INTRA) {
    1181     3448132 :         if (log2_trafo_size > 2 || s->ps.sps->chroma_format_idc == 3) {
    1182      982287 :             int trafo_size_h = 1 << (log2_trafo_size_c + s->ps.sps->hshift[1]);
    1183      982287 :             int trafo_size_v = 1 << (log2_trafo_size_c + s->ps.sps->vshift[1]);
    1184      982287 :             ff_hevc_set_neighbour_available(s, x0, y0, trafo_size_h, trafo_size_v);
    1185      982287 :             s->hpc.intra_pred[log2_trafo_size_c - 2](s, x0, y0, 1);
    1186      982287 :             s->hpc.intra_pred[log2_trafo_size_c - 2](s, x0, y0, 2);
    1187      982287 :             if (s->ps.sps->chroma_format_idc == 2) {
    1188       12423 :                 ff_hevc_set_neighbour_available(s, x0, y0 + (1 << log2_trafo_size_c),
    1189             :                                                 trafo_size_h, trafo_size_v);
    1190       12423 :                 s->hpc.intra_pred[log2_trafo_size_c - 2](s, x0, y0 + (1 << log2_trafo_size_c), 1);
    1191       12423 :                 s->hpc.intra_pred[log2_trafo_size_c - 2](s, x0, y0 + (1 << log2_trafo_size_c), 2);
    1192             :             }
    1193     1483558 :         } else if (blk_idx == 3) {
    1194      336170 :             int trafo_size_h = 1 << (log2_trafo_size + 1);
    1195      336170 :             int trafo_size_v = 1 << (log2_trafo_size + s->ps.sps->vshift[1]);
    1196      336170 :             ff_hevc_set_neighbour_available(s, xBase, yBase,
    1197             :                                             trafo_size_h, trafo_size_v);
    1198      336170 :             s->hpc.intra_pred[log2_trafo_size - 2](s, xBase, yBase, 1);
    1199      336170 :             s->hpc.intra_pred[log2_trafo_size - 2](s, xBase, yBase, 2);
    1200      336170 :             if (s->ps.sps->chroma_format_idc == 2) {
    1201        4196 :                 ff_hevc_set_neighbour_available(s, xBase, yBase + (1 << (log2_trafo_size)),
    1202             :                                                 trafo_size_h, trafo_size_v);
    1203        4196 :                 s->hpc.intra_pred[log2_trafo_size - 2](s, xBase, yBase + (1 << (log2_trafo_size)), 1);
    1204        4196 :                 s->hpc.intra_pred[log2_trafo_size - 2](s, xBase, yBase + (1 << (log2_trafo_size)), 2);
    1205             :             }
    1206             :         }
    1207             :     }
    1208             : 
    1209    14914839 :     return 0;
    1210             : }
    1211             : 
    1212      356478 : static void set_deblocking_bypass(HEVCContext *s, int x0, int y0, int log2_cb_size)
    1213             : {
    1214      356478 :     int cb_size          = 1 << log2_cb_size;
    1215      356478 :     int log2_min_pu_size = s->ps.sps->log2_min_pu_size;
    1216             : 
    1217      356478 :     int min_pu_width     = s->ps.sps->min_pu_width;
    1218      356478 :     int x_end = FFMIN(x0 + cb_size, s->ps.sps->width);
    1219      356478 :     int y_end = FFMIN(y0 + cb_size, s->ps.sps->height);
    1220             :     int i, j;
    1221             : 
    1222      851386 :     for (j = (y0 >> log2_min_pu_size); j < (y_end >> log2_min_pu_size); j++)
    1223     1443964 :         for (i = (x0 >> log2_min_pu_size); i < (x_end >> log2_min_pu_size); i++)
    1224      949056 :             s->is_pcm[i + j * min_pu_width] = 2;
    1225      356478 : }
    1226             : 
    1227    18031458 : static int hls_transform_tree(HEVCContext *s, int x0, int y0,
    1228             :                               int xBase, int yBase, int cb_xBase, int cb_yBase,
    1229             :                               int log2_cb_size, int log2_trafo_size,
    1230             :                               int trafo_depth, int blk_idx,
    1231             :                               const int *base_cbf_cb, const int *base_cbf_cr)
    1232             : {
    1233    18031458 :     HEVCLocalContext *lc = s->HEVClc;
    1234             :     uint8_t split_transform_flag;
    1235             :     int cbf_cb[2];
    1236             :     int cbf_cr[2];
    1237             :     int ret;
    1238             : 
    1239    18031458 :     cbf_cb[0] = base_cbf_cb[0];
    1240    18031458 :     cbf_cb[1] = base_cbf_cb[1];
    1241    18031458 :     cbf_cr[0] = base_cbf_cr[0];
    1242    18031458 :     cbf_cr[1] = base_cbf_cr[1];
    1243             : 
    1244    18031458 :     if (lc->cu.intra_split_flag) {
    1245     6172156 :         if (trafo_depth == 1) {
    1246     4635664 :             lc->tu.intra_pred_mode   = lc->pu.intra_pred_mode[blk_idx];
    1247     4635664 :             if (s->ps.sps->chroma_format_idc == 3) {
    1248      135404 :                 lc->tu.intra_pred_mode_c = lc->pu.intra_pred_mode_c[blk_idx];
    1249      135404 :                 lc->tu.chroma_mode_c     = lc->pu.chroma_mode_c[blk_idx];
    1250             :             } else {
    1251     4500260 :                 lc->tu.intra_pred_mode_c = lc->pu.intra_pred_mode_c[0];
    1252     4500260 :                 lc->tu.chroma_mode_c     = lc->pu.chroma_mode_c[0];
    1253             :             }
    1254             :         }
    1255             :     } else {
    1256    11859302 :         lc->tu.intra_pred_mode   = lc->pu.intra_pred_mode[0];
    1257    11859302 :         lc->tu.intra_pred_mode_c = lc->pu.intra_pred_mode_c[0];
    1258    11859302 :         lc->tu.chroma_mode_c     = lc->pu.chroma_mode_c[0];
    1259             :     }
    1260             : 
    1261    35958978 :     if (log2_trafo_size <= s->ps.sps->log2_max_trafo_size &&
    1262    27308908 :         log2_trafo_size >  s->ps.sps->log2_min_tb_size    &&
    1263    17316831 :         trafo_depth     < lc->cu.max_trafo_depth       &&
    1264     9577584 :         !(lc->cu.intra_split_flag && trafo_depth == 0)) {
    1265     6783914 :         split_transform_flag = ff_hevc_split_transform_flag_decode(s, log2_trafo_size);
    1266             :     } else {
    1267    23873356 :         int inter_split = s->ps.sps->max_transform_hierarchy_depth_inter == 0 &&
    1268     2170383 :                           lc->cu.pred_mode == MODE_INTER &&
    1269    12715208 :                           lc->cu.part_mode != PART_2Nx2N &&
    1270             :                           trafo_depth == 0;
    1271             : 
    1272    33638694 :         split_transform_flag = log2_trafo_size > s->ps.sps->log2_max_trafo_size ||
    1273    16921041 :                                (lc->cu.intra_split_flag && trafo_depth == 0) ||
    1274             :                                inter_split;
    1275             :     }
    1276             : 
    1277    18031458 :     if (s->ps.sps->chroma_format_idc && (log2_trafo_size > 2 || s->ps.sps->chroma_format_idc == 3)) {
    1278     9873086 :         if (trafo_depth == 0 || cbf_cb[0]) {
    1279     6727730 :             cbf_cb[0] = ff_hevc_cbf_cb_cr_decode(s, trafo_depth);
    1280     6727730 :             if (s->ps.sps->chroma_format_idc == 2 && (!split_transform_flag || log2_trafo_size == 3)) {
    1281      452048 :                 cbf_cb[1] = ff_hevc_cbf_cb_cr_decode(s, trafo_depth);
    1282             :             }
    1283             :         }
    1284             : 
    1285     9873086 :         if (trafo_depth == 0 || cbf_cr[0]) {
    1286     6675114 :             cbf_cr[0] = ff_hevc_cbf_cb_cr_decode(s, trafo_depth);
    1287     6675114 :             if (s->ps.sps->chroma_format_idc == 2 && (!split_transform_flag || log2_trafo_size == 3)) {
    1288      587469 :                 cbf_cr[1] = ff_hevc_cbf_cb_cr_decode(s, trafo_depth);
    1289             :             }
    1290             :         }
    1291             :     }
    1292             : 
    1293    18031458 :     if (split_transform_flag) {
    1294     3116619 :         const int trafo_size_split = 1 << (log2_trafo_size - 1);
    1295     3116619 :         const int x1 = x0 + trafo_size_split;
    1296     3116619 :         const int y1 = y0 + trafo_size_split;
    1297             : 
    1298             : #define SUBDIVIDE(x, y, idx)                                                    \
    1299             : do {                                                                            \
    1300             :     ret = hls_transform_tree(s, x, y, x0, y0, cb_xBase, cb_yBase, log2_cb_size, \
    1301             :                              log2_trafo_size - 1, trafo_depth + 1, idx,         \
    1302             :                              cbf_cb, cbf_cr);                                   \
    1303             :     if (ret < 0)                                                                \
    1304             :         return ret;                                                             \
    1305             : } while (0)
    1306             : 
    1307     3116619 :         SUBDIVIDE(x0, y0, 0);
    1308     3116619 :         SUBDIVIDE(x1, y0, 1);
    1309     3116619 :         SUBDIVIDE(x0, y1, 2);
    1310     3116619 :         SUBDIVIDE(x1, y1, 3);
    1311             : 
    1312             : #undef SUBDIVIDE
    1313             :     } else {
    1314    14914839 :         int min_tu_size      = 1 << s->ps.sps->log2_min_tb_size;
    1315    14914839 :         int log2_min_tu_size = s->ps.sps->log2_min_tb_size;
    1316    14914839 :         int min_tu_width     = s->ps.sps->min_tb_width;
    1317    14914839 :         int cbf_luma         = 1;
    1318             : 
    1319    15867037 :         if (lc->cu.pred_mode == MODE_INTRA || trafo_depth != 0 ||
    1320     2389049 :             cbf_cb[0] || cbf_cr[0] ||
    1321      686462 :             (s->ps.sps->chroma_format_idc == 2 && (cbf_cb[1] || cbf_cr[1]))) {
    1322    14265699 :             cbf_luma = ff_hevc_cbf_luma_decode(s, trafo_depth);
    1323             :         }
    1324             : 
    1325    14914839 :         ret = hls_transform_unit(s, x0, y0, xBase, yBase, cb_xBase, cb_yBase,
    1326             :                                  log2_cb_size, log2_trafo_size,
    1327             :                                  blk_idx, cbf_luma, cbf_cb, cbf_cr);
    1328    14914839 :         if (ret < 0)
    1329           0 :             return ret;
    1330             :         // TODO: store cbf_luma somewhere else
    1331    14914839 :         if (cbf_luma) {
    1332             :             int i, j;
    1333    28608837 :             for (i = 0; i < (1 << log2_trafo_size); i += min_tu_size)
    1334    87386448 :                 for (j = 0; j < (1 << log2_trafo_size); j += min_tu_size) {
    1335    68211204 :                     int x_tu = (x0 + j) >> log2_min_tu_size;
    1336    68211204 :                     int y_tu = (y0 + i) >> log2_min_tu_size;
    1337    68211204 :                     s->cbf_luma[y_tu * min_tu_width + x_tu] = 1;
    1338             :                 }
    1339             :         }
    1340    14914839 :         if (!s->sh.disable_deblocking_filter_flag) {
    1341    14382019 :             ff_hevc_deblocking_boundary_strengths(s, x0, y0, log2_trafo_size);
    1342    14755960 :             if (s->ps.pps->transquant_bypass_enable_flag &&
    1343      373941 :                 lc->cu.cu_transquant_bypass_flag)
    1344      275932 :                 set_deblocking_bypass(s, x0, y0, log2_trafo_size);
    1345             :         }
    1346             :     }
    1347    18031458 :     return 0;
    1348             : }
    1349             : 
    1350       12433 : static int hls_pcm_sample(HEVCContext *s, int x0, int y0, int log2_cb_size)
    1351             : {
    1352       12433 :     HEVCLocalContext *lc = s->HEVClc;
    1353             :     GetBitContext gb;
    1354       12433 :     int cb_size   = 1 << log2_cb_size;
    1355       12433 :     ptrdiff_t stride0 = s->frame->linesize[0];
    1356       12433 :     ptrdiff_t stride1 = s->frame->linesize[1];
    1357       12433 :     ptrdiff_t stride2 = s->frame->linesize[2];
    1358       12433 :     uint8_t *dst0 = &s->frame->data[0][y0 * stride0 + (x0 << s->ps.sps->pixel_shift)];
    1359       12433 :     uint8_t *dst1 = &s->frame->data[1][(y0 >> s->ps.sps->vshift[1]) * stride1 + ((x0 >> s->ps.sps->hshift[1]) << s->ps.sps->pixel_shift)];
    1360       12433 :     uint8_t *dst2 = &s->frame->data[2][(y0 >> s->ps.sps->vshift[2]) * stride2 + ((x0 >> s->ps.sps->hshift[2]) << s->ps.sps->pixel_shift)];
    1361             : 
    1362       24866 :     int length         = cb_size * cb_size * s->ps.sps->pcm.bit_depth +
    1363       24866 :                          (((cb_size >> s->ps.sps->hshift[1]) * (cb_size >> s->ps.sps->vshift[1])) +
    1364       24866 :                           ((cb_size >> s->ps.sps->hshift[2]) * (cb_size >> s->ps.sps->vshift[2]))) *
    1365       12433 :                           s->ps.sps->pcm.bit_depth_chroma;
    1366       12433 :     const uint8_t *pcm = skip_bytes(&lc->cc, (length + 7) >> 3);
    1367             :     int ret;
    1368             : 
    1369       12433 :     if (!s->sh.disable_deblocking_filter_flag)
    1370        6937 :         ff_hevc_deblocking_boundary_strengths(s, x0, y0, log2_cb_size);
    1371             : 
    1372       12433 :     ret = init_get_bits(&gb, pcm, length);
    1373       12433 :     if (ret < 0)
    1374           0 :         return ret;
    1375             : 
    1376       12433 :     s->hevcdsp.put_pcm(dst0, stride0, cb_size, cb_size,     &gb, s->ps.sps->pcm.bit_depth);
    1377       12433 :     if (s->ps.sps->chroma_format_idc) {
    1378       49732 :         s->hevcdsp.put_pcm(dst1, stride1,
    1379       12433 :                            cb_size >> s->ps.sps->hshift[1],
    1380       12433 :                            cb_size >> s->ps.sps->vshift[1],
    1381       12433 :                            &gb, s->ps.sps->pcm.bit_depth_chroma);
    1382       49732 :         s->hevcdsp.put_pcm(dst2, stride2,
    1383       12433 :                            cb_size >> s->ps.sps->hshift[2],
    1384       12433 :                            cb_size >> s->ps.sps->vshift[2],
    1385       12433 :                            &gb, s->ps.sps->pcm.bit_depth_chroma);
    1386             :     }
    1387             : 
    1388       12433 :     return 0;
    1389             : }
    1390             : 
    1391             : /**
    1392             :  * 8.5.3.2.2.1 Luma sample unidirectional interpolation process
    1393             :  *
    1394             :  * @param s HEVC decoding context
    1395             :  * @param dst target buffer for block data at block position
    1396             :  * @param dststride stride of the dst buffer
    1397             :  * @param ref reference picture buffer at origin (0, 0)
    1398             :  * @param mv motion vector (relative to block position) to get pixel data from
    1399             :  * @param x_off horizontal position of block from origin (0, 0)
    1400             :  * @param y_off vertical position of block from origin (0, 0)
    1401             :  * @param block_w width of block
    1402             :  * @param block_h height of block
    1403             :  * @param luma_weight weighting factor applied to the luma prediction
    1404             :  * @param luma_offset additive offset applied to the luma prediction value
    1405             :  */
    1406             : 
    1407     4690016 : static void luma_mc_uni(HEVCContext *s, uint8_t *dst, ptrdiff_t dststride,
    1408             :                         AVFrame *ref, const Mv *mv, int x_off, int y_off,
    1409             :                         int block_w, int block_h, int luma_weight, int luma_offset)
    1410             : {
    1411     4690016 :     HEVCLocalContext *lc = s->HEVClc;
    1412     4690016 :     uint8_t *src         = ref->data[0];
    1413     4690016 :     ptrdiff_t srcstride  = ref->linesize[0];
    1414     4690016 :     int pic_width        = s->ps.sps->width;
    1415     4690016 :     int pic_height       = s->ps.sps->height;
    1416     4690016 :     int mx               = mv->x & 3;
    1417     4690016 :     int my               = mv->y & 3;
    1418     9440313 :     int weight_flag      = (s->sh.slice_type == HEVC_SLICE_P && s->ps.pps->weighted_pred_flag) ||
    1419     7486963 :                            (s->sh.slice_type == HEVC_SLICE_B && s->ps.pps->weighted_bipred_flag);
    1420     4690016 :     int idx              = ff_hevc_pel_weight[block_w];
    1421             : 
    1422     4690016 :     x_off += mv->x >> 2;
    1423     4690016 :     y_off += mv->y >> 2;
    1424     4690016 :     src   += y_off * srcstride + (x_off * (1 << s->ps.sps->pixel_shift));
    1425             : 
    1426     9223509 :     if (x_off < QPEL_EXTRA_BEFORE || y_off < QPEL_EXTRA_AFTER ||
    1427     9004852 :         x_off >= pic_width - block_w - QPEL_EXTRA_AFTER ||
    1428     4471359 :         y_off >= pic_height - block_h - QPEL_EXTRA_AFTER) {
    1429      403958 :         const ptrdiff_t edge_emu_stride = EDGE_EMU_BUFFER_STRIDE << s->ps.sps->pixel_shift;
    1430      403958 :         int offset     = QPEL_EXTRA_BEFORE * srcstride       + (QPEL_EXTRA_BEFORE << s->ps.sps->pixel_shift);
    1431      403958 :         int buf_offset = QPEL_EXTRA_BEFORE * edge_emu_stride + (QPEL_EXTRA_BEFORE << s->ps.sps->pixel_shift);
    1432             : 
    1433      403958 :         s->vdsp.emulated_edge_mc(lc->edge_emu_buffer, src - offset,
    1434             :                                  edge_emu_stride, srcstride,
    1435             :                                  block_w + QPEL_EXTRA,
    1436             :                                  block_h + QPEL_EXTRA,
    1437             :                                  x_off - QPEL_EXTRA_BEFORE, y_off - QPEL_EXTRA_BEFORE,
    1438             :                                  pic_width, pic_height);
    1439      403958 :         src = lc->edge_emu_buffer + buf_offset;
    1440      403958 :         srcstride = edge_emu_stride;
    1441             :     }
    1442             : 
    1443     4690016 :     if (!weight_flag)
    1444     4584078 :         s->hevcdsp.put_hevc_qpel_uni[idx][!!my][!!mx](dst, dststride, src, srcstride,
    1445             :                                                       block_h, mx, my, block_w);
    1446             :     else
    1447      211876 :         s->hevcdsp.put_hevc_qpel_uni_w[idx][!!my][!!mx](dst, dststride, src, srcstride,
    1448      105938 :                                                         block_h, s->sh.luma_log2_weight_denom,
    1449             :                                                         luma_weight, luma_offset, mx, my, block_w);
    1450     4690016 : }
    1451             : 
    1452             : /**
    1453             :  * 8.5.3.2.2.1 Luma sample bidirectional interpolation process
    1454             :  *
    1455             :  * @param s HEVC decoding context
    1456             :  * @param dst target buffer for block data at block position
    1457             :  * @param dststride stride of the dst buffer
    1458             :  * @param ref0 reference picture0 buffer at origin (0, 0)
    1459             :  * @param mv0 motion vector0 (relative to block position) to get pixel data from
    1460             :  * @param x_off horizontal position of block from origin (0, 0)
    1461             :  * @param y_off vertical position of block from origin (0, 0)
    1462             :  * @param block_w width of block
    1463             :  * @param block_h height of block
    1464             :  * @param ref1 reference picture1 buffer at origin (0, 0)
    1465             :  * @param mv1 motion vector1 (relative to block position) to get pixel data from
    1466             :  * @param current_mv current motion vector structure
    1467             :  */
    1468     3781449 :  static void luma_mc_bi(HEVCContext *s, uint8_t *dst, ptrdiff_t dststride,
    1469             :                        AVFrame *ref0, const Mv *mv0, int x_off, int y_off,
    1470             :                        int block_w, int block_h, AVFrame *ref1, const Mv *mv1, struct MvField *current_mv)
    1471             : {
    1472     3781449 :     HEVCLocalContext *lc = s->HEVClc;
    1473     3781449 :     ptrdiff_t src0stride  = ref0->linesize[0];
    1474     3781449 :     ptrdiff_t src1stride  = ref1->linesize[0];
    1475     3781449 :     int pic_width        = s->ps.sps->width;
    1476     3781449 :     int pic_height       = s->ps.sps->height;
    1477     3781449 :     int mx0              = mv0->x & 3;
    1478     3781449 :     int my0              = mv0->y & 3;
    1479     3781449 :     int mx1              = mv1->x & 3;
    1480     3781449 :     int my1              = mv1->y & 3;
    1481     7625205 :     int weight_flag      = (s->sh.slice_type == HEVC_SLICE_P && s->ps.pps->weighted_pred_flag) ||
    1482     7562898 :                            (s->sh.slice_type == HEVC_SLICE_B && s->ps.pps->weighted_bipred_flag);
    1483     3781449 :     int x_off0           = x_off + (mv0->x >> 2);
    1484     3781449 :     int y_off0           = y_off + (mv0->y >> 2);
    1485     3781449 :     int x_off1           = x_off + (mv1->x >> 2);
    1486     3781449 :     int y_off1           = y_off + (mv1->y >> 2);
    1487     3781449 :     int idx              = ff_hevc_pel_weight[block_w];
    1488             : 
    1489     3781449 :     uint8_t *src0  = ref0->data[0] + y_off0 * src0stride + (int)((unsigned)x_off0 << s->ps.sps->pixel_shift);
    1490     3781449 :     uint8_t *src1  = ref1->data[0] + y_off1 * src1stride + (int)((unsigned)x_off1 << s->ps.sps->pixel_shift);
    1491             : 
    1492     7411125 :     if (x_off0 < QPEL_EXTRA_BEFORE || y_off0 < QPEL_EXTRA_AFTER ||
    1493     7196299 :         x_off0 >= pic_width - block_w - QPEL_EXTRA_AFTER ||
    1494     3566623 :         y_off0 >= pic_height - block_h - QPEL_EXTRA_AFTER) {
    1495      415542 :         const ptrdiff_t edge_emu_stride = EDGE_EMU_BUFFER_STRIDE << s->ps.sps->pixel_shift;
    1496      415542 :         int offset     = QPEL_EXTRA_BEFORE * src0stride       + (QPEL_EXTRA_BEFORE << s->ps.sps->pixel_shift);
    1497      415542 :         int buf_offset = QPEL_EXTRA_BEFORE * edge_emu_stride + (QPEL_EXTRA_BEFORE << s->ps.sps->pixel_shift);
    1498             : 
    1499      415542 :         s->vdsp.emulated_edge_mc(lc->edge_emu_buffer, src0 - offset,
    1500             :                                  edge_emu_stride, src0stride,
    1501             :                                  block_w + QPEL_EXTRA,
    1502             :                                  block_h + QPEL_EXTRA,
    1503             :                                  x_off0 - QPEL_EXTRA_BEFORE, y_off0 - QPEL_EXTRA_BEFORE,
    1504             :                                  pic_width, pic_height);
    1505      415542 :         src0 = lc->edge_emu_buffer + buf_offset;
    1506      415542 :         src0stride = edge_emu_stride;
    1507             :     }
    1508             : 
    1509     7406334 :     if (x_off1 < QPEL_EXTRA_BEFORE || y_off1 < QPEL_EXTRA_AFTER ||
    1510     7184421 :         x_off1 >= pic_width - block_w - QPEL_EXTRA_AFTER ||
    1511     3559536 :         y_off1 >= pic_height - block_h - QPEL_EXTRA_AFTER) {
    1512      426476 :         const ptrdiff_t edge_emu_stride = EDGE_EMU_BUFFER_STRIDE << s->ps.sps->pixel_shift;
    1513      426476 :         int offset     = QPEL_EXTRA_BEFORE * src1stride       + (QPEL_EXTRA_BEFORE << s->ps.sps->pixel_shift);
    1514      426476 :         int buf_offset = QPEL_EXTRA_BEFORE * edge_emu_stride + (QPEL_EXTRA_BEFORE << s->ps.sps->pixel_shift);
    1515             : 
    1516      426476 :         s->vdsp.emulated_edge_mc(lc->edge_emu_buffer2, src1 - offset,
    1517             :                                  edge_emu_stride, src1stride,
    1518             :                                  block_w + QPEL_EXTRA,
    1519             :                                  block_h + QPEL_EXTRA,
    1520             :                                  x_off1 - QPEL_EXTRA_BEFORE, y_off1 - QPEL_EXTRA_BEFORE,
    1521             :                                  pic_width, pic_height);
    1522      426476 :         src1 = lc->edge_emu_buffer2 + buf_offset;
    1523      426476 :         src1stride = edge_emu_stride;
    1524             :     }
    1525             : 
    1526     3781449 :     s->hevcdsp.put_hevc_qpel[idx][!!my0][!!mx0](lc->tmp, src0, src0stride,
    1527             :                                                 block_h, mx0, my0, block_w);
    1528     3781449 :     if (!weight_flag)
    1529     3719142 :         s->hevcdsp.put_hevc_qpel_bi[idx][!!my1][!!mx1](dst, dststride, src1, src1stride, lc->tmp,
    1530             :                                                        block_h, mx1, my1, block_w);
    1531             :     else
    1532      373842 :         s->hevcdsp.put_hevc_qpel_bi_w[idx][!!my1][!!mx1](dst, dststride, src1, src1stride, lc->tmp,
    1533       62307 :                                                          block_h, s->sh.luma_log2_weight_denom,
    1534       62307 :                                                          s->sh.luma_weight_l0[current_mv->ref_idx[0]],
    1535       62307 :                                                          s->sh.luma_weight_l1[current_mv->ref_idx[1]],
    1536       62307 :                                                          s->sh.luma_offset_l0[current_mv->ref_idx[0]],
    1537       62307 :                                                          s->sh.luma_offset_l1[current_mv->ref_idx[1]],
    1538             :                                                          mx1, my1, block_w);
    1539             : 
    1540     3781449 : }
    1541             : 
    1542             : /**
    1543             :  * 8.5.3.2.2.2 Chroma sample uniprediction interpolation process
    1544             :  *
    1545             :  * @param s HEVC decoding context
    1546             :  * @param dst1 target buffer for block data at block position (U plane)
    1547             :  * @param dst2 target buffer for block data at block position (V plane)
    1548             :  * @param dststride stride of the dst1 and dst2 buffers
    1549             :  * @param ref reference picture buffer at origin (0, 0)
    1550             :  * @param mv motion vector (relative to block position) to get pixel data from
    1551             :  * @param x_off horizontal position of block from origin (0, 0)
    1552             :  * @param y_off vertical position of block from origin (0, 0)
    1553             :  * @param block_w width of block
    1554             :  * @param block_h height of block
    1555             :  * @param chroma_weight weighting factor applied to the chroma prediction
    1556             :  * @param chroma_offset additive offset applied to the chroma prediction value
    1557             :  */
    1558             : 
    1559     9380032 : static void chroma_mc_uni(HEVCContext *s, uint8_t *dst0,
    1560             :                           ptrdiff_t dststride, uint8_t *src0, ptrdiff_t srcstride, int reflist,
    1561             :                           int x_off, int y_off, int block_w, int block_h, struct MvField *current_mv, int chroma_weight, int chroma_offset)
    1562             : {
    1563     9380032 :     HEVCLocalContext *lc = s->HEVClc;
    1564     9380032 :     int pic_width        = s->ps.sps->width >> s->ps.sps->hshift[1];
    1565     9380032 :     int pic_height       = s->ps.sps->height >> s->ps.sps->vshift[1];
    1566     9380032 :     const Mv *mv         = &current_mv->mv[reflist];
    1567    18880626 :     int weight_flag      = (s->sh.slice_type == HEVC_SLICE_P && s->ps.pps->weighted_pred_flag) ||
    1568    14973926 :                            (s->sh.slice_type == HEVC_SLICE_B && s->ps.pps->weighted_bipred_flag);
    1569     9380032 :     int idx              = ff_hevc_pel_weight[block_w];
    1570     9380032 :     int hshift           = s->ps.sps->hshift[1];
    1571     9380032 :     int vshift           = s->ps.sps->vshift[1];
    1572     9380032 :     intptr_t mx          = av_mod_uintp2(mv->x, 2 + hshift);
    1573     9380032 :     intptr_t my          = av_mod_uintp2(mv->y, 2 + vshift);
    1574     9380032 :     intptr_t _mx         = mx << (1 - hshift);
    1575     9380032 :     intptr_t _my         = my << (1 - vshift);
    1576             : 
    1577     9380032 :     x_off += mv->x >> (2 + hshift);
    1578     9380032 :     y_off += mv->y >> (2 + vshift);
    1579     9380032 :     src0  += y_off * srcstride + (x_off * (1 << s->ps.sps->pixel_shift));
    1580             : 
    1581    18456874 :     if (x_off < EPEL_EXTRA_BEFORE || y_off < EPEL_EXTRA_AFTER ||
    1582    18029424 :         x_off >= pic_width - block_w - EPEL_EXTRA_AFTER ||
    1583     8952582 :         y_off >= pic_height - block_h - EPEL_EXTRA_AFTER) {
    1584      798108 :         const int edge_emu_stride = EDGE_EMU_BUFFER_STRIDE << s->ps.sps->pixel_shift;
    1585      798108 :         int offset0 = EPEL_EXTRA_BEFORE * (srcstride + (1 << s->ps.sps->pixel_shift));
    1586      798108 :         int buf_offset0 = EPEL_EXTRA_BEFORE *
    1587      798108 :                           (edge_emu_stride + (1 << s->ps.sps->pixel_shift));
    1588      798108 :         s->vdsp.emulated_edge_mc(lc->edge_emu_buffer, src0 - offset0,
    1589             :                                  edge_emu_stride, srcstride,
    1590             :                                  block_w + EPEL_EXTRA, block_h + EPEL_EXTRA,
    1591             :                                  x_off - EPEL_EXTRA_BEFORE,
    1592             :                                  y_off - EPEL_EXTRA_BEFORE,
    1593             :                                  pic_width, pic_height);
    1594             : 
    1595      798108 :         src0 = lc->edge_emu_buffer + buf_offset0;
    1596      798108 :         srcstride = edge_emu_stride;
    1597             :     }
    1598     9380032 :     if (!weight_flag)
    1599     9168156 :         s->hevcdsp.put_hevc_epel_uni[idx][!!my][!!mx](dst0, dststride, src0, srcstride,
    1600             :                                                   block_h, _mx, _my, block_w);
    1601             :     else
    1602      423752 :         s->hevcdsp.put_hevc_epel_uni_w[idx][!!my][!!mx](dst0, dststride, src0, srcstride,
    1603      211876 :                                                         block_h, s->sh.chroma_log2_weight_denom,
    1604             :                                                         chroma_weight, chroma_offset, _mx, _my, block_w);
    1605     9380032 : }
    1606             : 
    1607             : /**
    1608             :  * 8.5.3.2.2.2 Chroma sample bidirectional interpolation process
    1609             :  *
    1610             :  * @param s HEVC decoding context
    1611             :  * @param dst target buffer for block data at block position
    1612             :  * @param dststride stride of the dst buffer
    1613             :  * @param ref0 reference picture0 buffer at origin (0, 0)
    1614             :  * @param mv0 motion vector0 (relative to block position) to get pixel data from
    1615             :  * @param x_off horizontal position of block from origin (0, 0)
    1616             :  * @param y_off vertical position of block from origin (0, 0)
    1617             :  * @param block_w width of block
    1618             :  * @param block_h height of block
    1619             :  * @param ref1 reference picture1 buffer at origin (0, 0)
    1620             :  * @param mv1 motion vector1 (relative to block position) to get pixel data from
    1621             :  * @param current_mv current motion vector structure
    1622             :  * @param cidx chroma component(cb, cr)
    1623             :  */
    1624     7562898 : static void chroma_mc_bi(HEVCContext *s, uint8_t *dst0, ptrdiff_t dststride, AVFrame *ref0, AVFrame *ref1,
    1625             :                          int x_off, int y_off, int block_w, int block_h, struct MvField *current_mv, int cidx)
    1626             : {
    1627     7562898 :     HEVCLocalContext *lc = s->HEVClc;
    1628     7562898 :     uint8_t *src1        = ref0->data[cidx+1];
    1629     7562898 :     uint8_t *src2        = ref1->data[cidx+1];
    1630     7562898 :     ptrdiff_t src1stride = ref0->linesize[cidx+1];
    1631     7562898 :     ptrdiff_t src2stride = ref1->linesize[cidx+1];
    1632    15250410 :     int weight_flag      = (s->sh.slice_type == HEVC_SLICE_P && s->ps.pps->weighted_pred_flag) ||
    1633    15125796 :                            (s->sh.slice_type == HEVC_SLICE_B && s->ps.pps->weighted_bipred_flag);
    1634     7562898 :     int pic_width        = s->ps.sps->width >> s->ps.sps->hshift[1];
    1635     7562898 :     int pic_height       = s->ps.sps->height >> s->ps.sps->vshift[1];
    1636     7562898 :     Mv *mv0              = &current_mv->mv[0];
    1637     7562898 :     Mv *mv1              = &current_mv->mv[1];
    1638     7562898 :     int hshift = s->ps.sps->hshift[1];
    1639     7562898 :     int vshift = s->ps.sps->vshift[1];
    1640             : 
    1641     7562898 :     intptr_t mx0 = av_mod_uintp2(mv0->x, 2 + hshift);
    1642     7562898 :     intptr_t my0 = av_mod_uintp2(mv0->y, 2 + vshift);
    1643     7562898 :     intptr_t mx1 = av_mod_uintp2(mv1->x, 2 + hshift);
    1644     7562898 :     intptr_t my1 = av_mod_uintp2(mv1->y, 2 + vshift);
    1645     7562898 :     intptr_t _mx0 = mx0 << (1 - hshift);
    1646     7562898 :     intptr_t _my0 = my0 << (1 - vshift);
    1647     7562898 :     intptr_t _mx1 = mx1 << (1 - hshift);
    1648     7562898 :     intptr_t _my1 = my1 << (1 - vshift);
    1649             : 
    1650     7562898 :     int x_off0 = x_off + (mv0->x >> (2 + hshift));
    1651     7562898 :     int y_off0 = y_off + (mv0->y >> (2 + vshift));
    1652     7562898 :     int x_off1 = x_off + (mv1->x >> (2 + hshift));
    1653     7562898 :     int y_off1 = y_off + (mv1->y >> (2 + vshift));
    1654     7562898 :     int idx = ff_hevc_pel_weight[block_w];
    1655     7562898 :     src1  += y_off0 * src1stride + (int)((unsigned)x_off0 << s->ps.sps->pixel_shift);
    1656     7562898 :     src2  += y_off1 * src2stride + (int)((unsigned)x_off1 << s->ps.sps->pixel_shift);
    1657             : 
    1658    14827414 :     if (x_off0 < EPEL_EXTRA_BEFORE || y_off0 < EPEL_EXTRA_AFTER ||
    1659    14402930 :         x_off0 >= pic_width - block_w - EPEL_EXTRA_AFTER ||
    1660     7138414 :         y_off0 >= pic_height - block_h - EPEL_EXTRA_AFTER) {
    1661      825266 :         const int edge_emu_stride = EDGE_EMU_BUFFER_STRIDE << s->ps.sps->pixel_shift;
    1662      825266 :         int offset1 = EPEL_EXTRA_BEFORE * (src1stride + (1 << s->ps.sps->pixel_shift));
    1663      825266 :         int buf_offset1 = EPEL_EXTRA_BEFORE *
    1664      825266 :                           (edge_emu_stride + (1 << s->ps.sps->pixel_shift));
    1665             : 
    1666      825266 :         s->vdsp.emulated_edge_mc(lc->edge_emu_buffer, src1 - offset1,
    1667             :                                  edge_emu_stride, src1stride,
    1668             :                                  block_w + EPEL_EXTRA, block_h + EPEL_EXTRA,
    1669             :                                  x_off0 - EPEL_EXTRA_BEFORE,
    1670             :                                  y_off0 - EPEL_EXTRA_BEFORE,
    1671             :                                  pic_width, pic_height);
    1672             : 
    1673      825266 :         src1 = lc->edge_emu_buffer + buf_offset1;
    1674      825266 :         src1stride = edge_emu_stride;
    1675             :     }
    1676             : 
    1677    14817872 :     if (x_off1 < EPEL_EXTRA_BEFORE || y_off1 < EPEL_EXTRA_AFTER ||
    1678    14379256 :         x_off1 >= pic_width - block_w - EPEL_EXTRA_AFTER ||
    1679     7124282 :         y_off1 >= pic_height - block_h - EPEL_EXTRA_AFTER) {
    1680      847398 :         const int edge_emu_stride = EDGE_EMU_BUFFER_STRIDE << s->ps.sps->pixel_shift;
    1681      847398 :         int offset1 = EPEL_EXTRA_BEFORE * (src2stride + (1 << s->ps.sps->pixel_shift));
    1682      847398 :         int buf_offset1 = EPEL_EXTRA_BEFORE *
    1683      847398 :                           (edge_emu_stride + (1 << s->ps.sps->pixel_shift));
    1684             : 
    1685      847398 :         s->vdsp.emulated_edge_mc(lc->edge_emu_buffer2, src2 - offset1,
    1686             :                                  edge_emu_stride, src2stride,
    1687             :                                  block_w + EPEL_EXTRA, block_h + EPEL_EXTRA,
    1688             :                                  x_off1 - EPEL_EXTRA_BEFORE,
    1689             :                                  y_off1 - EPEL_EXTRA_BEFORE,
    1690             :                                  pic_width, pic_height);
    1691             : 
    1692      847398 :         src2 = lc->edge_emu_buffer2 + buf_offset1;
    1693      847398 :         src2stride = edge_emu_stride;
    1694             :     }
    1695             : 
    1696     7562898 :     s->hevcdsp.put_hevc_epel[idx][!!my0][!!mx0](lc->tmp, src1, src1stride,
    1697             :                                                 block_h, _mx0, _my0, block_w);
    1698     7562898 :     if (!weight_flag)
    1699    14876568 :         s->hevcdsp.put_hevc_epel_bi[idx][!!my1][!!mx1](dst0, s->frame->linesize[cidx+1],
    1700     7438284 :                                                        src2, src2stride, lc->tmp,
    1701             :                                                        block_h, _mx1, _my1, block_w);
    1702             :     else
    1703      872298 :         s->hevcdsp.put_hevc_epel_bi_w[idx][!!my1][!!mx1](dst0, s->frame->linesize[cidx+1],
    1704      124614 :                                                          src2, src2stride, lc->tmp,
    1705             :                                                          block_h,
    1706      124614 :                                                          s->sh.chroma_log2_weight_denom,
    1707      124614 :                                                          s->sh.chroma_weight_l0[current_mv->ref_idx[0]][cidx],
    1708      124614 :                                                          s->sh.chroma_weight_l1[current_mv->ref_idx[1]][cidx],
    1709      124614 :                                                          s->sh.chroma_offset_l0[current_mv->ref_idx[0]][cidx],
    1710      124614 :                                                          s->sh.chroma_offset_l1[current_mv->ref_idx[1]][cidx],
    1711             :                                                          _mx1, _my1, block_w);
    1712     7562898 : }
    1713             : 
    1714    12252914 : static void hevc_await_progress(HEVCContext *s, HEVCFrame *ref,
    1715             :                                 const Mv *mv, int y0, int height)
    1716             : {
    1717    12252914 :     if (s->threads_type == FF_THREAD_FRAME ) {
    1718           0 :         int y = FFMAX(0, (mv->y >> 2) + y0 + height + 9);
    1719             : 
    1720           0 :         ff_thread_await_progress(&ref->tf, y, 0);
    1721             :     }
    1722    12252914 : }
    1723             : 
    1724     2365286 : static void hevc_luma_mv_mvp_mode(HEVCContext *s, int x0, int y0, int nPbW,
    1725             :                                   int nPbH, int log2_cb_size, int part_idx,
    1726             :                                   int merge_idx, MvField *mv)
    1727             : {
    1728     2365286 :     HEVCLocalContext *lc = s->HEVClc;
    1729     2365286 :     enum InterPredIdc inter_pred_idc = PRED_L0;
    1730             :     int mvp_flag;
    1731             : 
    1732     2365286 :     ff_hevc_set_neighbour_available(s, x0, y0, nPbW, nPbH);
    1733     2365286 :     mv->pred_flag = 0;
    1734     2365286 :     if (s->sh.slice_type == HEVC_SLICE_B)
    1735     1811353 :         inter_pred_idc = ff_hevc_inter_pred_idc_decode(s, nPbW, nPbH);
    1736             : 
    1737     2365286 :     if (inter_pred_idc != PRED_L1) {
    1738     1981267 :         if (s->sh.nb_refs[L0])
    1739     1981267 :             mv->ref_idx[0]= ff_hevc_ref_idx_lx_decode(s, s->sh.nb_refs[L0]);
    1740             : 
    1741     1981267 :         mv->pred_flag = PF_L0;
    1742     1981267 :         ff_hevc_hls_mvd_coding(s, x0, y0, 0);
    1743     1981267 :         mvp_flag = ff_hevc_mvp_lx_flag_decode(s);
    1744     1981267 :         ff_hevc_luma_mv_mvp_mode(s, x0, y0, nPbW, nPbH, log2_cb_size,
    1745             :                                  part_idx, merge_idx, mv, mvp_flag, 0);
    1746     1981267 :         mv->mv[0].x += lc->pu.mvd.x;
    1747     1981267 :         mv->mv[0].y += lc->pu.mvd.y;
    1748             :     }
    1749             : 
    1750     2365286 :     if (inter_pred_idc != PRED_L0) {
    1751      788021 :         if (s->sh.nb_refs[L1])
    1752      788021 :             mv->ref_idx[1]= ff_hevc_ref_idx_lx_decode(s, s->sh.nb_refs[L1]);
    1753             : 
    1754      788021 :         if (s->sh.mvd_l1_zero_flag == 1 && inter_pred_idc == PRED_BI) {
    1755      157794 :             AV_ZERO32(&lc->pu.mvd);
    1756             :         } else {
    1757      630227 :             ff_hevc_hls_mvd_coding(s, x0, y0, 1);
    1758             :         }
    1759             : 
    1760      788021 :         mv->pred_flag += PF_L1;
    1761      788021 :         mvp_flag = ff_hevc_mvp_lx_flag_decode(s);
    1762      788021 :         ff_hevc_luma_mv_mvp_mode(s, x0, y0, nPbW, nPbH, log2_cb_size,
    1763             :                                  part_idx, merge_idx, mv, mvp_flag, 1);
    1764      788021 :         mv->mv[1].x += lc->pu.mvd.x;
    1765      788021 :         mv->mv[1].y += lc->pu.mvd.y;
    1766             :     }
    1767     2365286 : }
    1768             : 
    1769     8471465 : static void hls_prediction_unit(HEVCContext *s, int x0, int y0,
    1770             :                                 int nPbW, int nPbH,
    1771             :                                 int log2_cb_size, int partIdx, int idx)
    1772             : {
    1773             : #define POS(c_idx, x, y)                                                              \
    1774             :     &s->frame->data[c_idx][((y) >> s->ps.sps->vshift[c_idx]) * s->frame->linesize[c_idx] + \
    1775             :                            (((x) >> s->ps.sps->hshift[c_idx]) << s->ps.sps->pixel_shift)]
    1776     8471465 :     HEVCLocalContext *lc = s->HEVClc;
    1777     8471465 :     int merge_idx = 0;
    1778     8471465 :     struct MvField current_mv = {{{ 0 }}};
    1779             : 
    1780     8471465 :     int min_pu_width = s->ps.sps->min_pu_width;
    1781             : 
    1782     8471465 :     MvField *tab_mvf = s->ref->tab_mvf;
    1783     8471465 :     RefPicList  *refPicList = s->ref->refPicList;
    1784     8471465 :     HEVCFrame *ref0 = NULL, *ref1 = NULL;
    1785     8471465 :     uint8_t *dst0 = POS(0, x0, y0);
    1786     8471465 :     uint8_t *dst1 = POS(1, x0, y0);
    1787     8471465 :     uint8_t *dst2 = POS(2, x0, y0);
    1788     8471465 :     int log2_min_cb_size = s->ps.sps->log2_min_cb_size;
    1789     8471465 :     int min_cb_width     = s->ps.sps->min_cb_width;
    1790     8471465 :     int x_cb             = x0 >> log2_min_cb_size;
    1791     8471465 :     int y_cb             = y0 >> log2_min_cb_size;
    1792             :     int x_pu, y_pu;
    1793             :     int i, j;
    1794             : 
    1795     8471465 :     int skip_flag = SAMPLE_CTB(s->skip_flag, x_cb, y_cb);
    1796             : 
    1797     8471465 :     if (!skip_flag)
    1798     4672768 :         lc->pu.merge_flag = ff_hevc_merge_flag_decode(s);
    1799             : 
    1800     8471465 :     if (skip_flag || lc->pu.merge_flag) {
    1801     6106179 :         if (s->sh.max_num_merge_cand > 1)
    1802     6029326 :             merge_idx = ff_hevc_merge_idx_decode(s);
    1803             :         else
    1804       76853 :             merge_idx = 0;
    1805             : 
    1806     6106179 :         ff_hevc_luma_mv_merge_mode(s, x0, y0, nPbW, nPbH, log2_cb_size,
    1807             :                                    partIdx, merge_idx, &current_mv);
    1808             :     } else {
    1809     2365286 :         hevc_luma_mv_mvp_mode(s, x0, y0, nPbW, nPbH, log2_cb_size,
    1810             :                               partIdx, merge_idx, &current_mv);
    1811             :     }
    1812             : 
    1813     8471465 :     x_pu = x0 >> s->ps.sps->log2_min_pu_size;
    1814     8471465 :     y_pu = y0 >> s->ps.sps->log2_min_pu_size;
    1815             : 
    1816    40331273 :     for (j = 0; j < nPbH >> s->ps.sps->log2_min_pu_size; j++)
    1817   233267120 :         for (i = 0; i < nPbW >> s->ps.sps->log2_min_pu_size; i++)
    1818   201407312 :             tab_mvf[(y_pu + j) * min_pu_width + x_pu + i] = current_mv;
    1819             : 
    1820     8471465 :     if (current_mv.pred_flag & PF_L0) {
    1821     7710888 :         ref0 = refPicList[0].ref[current_mv.ref_idx[0]];
    1822     7710888 :         if (!ref0)
    1823           0 :             return;
    1824     7710888 :         hevc_await_progress(s, ref0, &current_mv.mv[0], y0, nPbH);
    1825             :     }
    1826     8471465 :     if (current_mv.pred_flag & PF_L1) {
    1827     4542026 :         ref1 = refPicList[1].ref[current_mv.ref_idx[1]];
    1828     4542026 :         if (!ref1)
    1829           0 :             return;
    1830     4542026 :         hevc_await_progress(s, ref1, &current_mv.mv[1], y0, nPbH);
    1831             :     }
    1832             : 
    1833     8471465 :     if (current_mv.pred_flag == PF_L0) {
    1834     3929439 :         int x0_c = x0 >> s->ps.sps->hshift[1];
    1835     3929439 :         int y0_c = y0 >> s->ps.sps->vshift[1];
    1836     3929439 :         int nPbW_c = nPbW >> s->ps.sps->hshift[1];
    1837     3929439 :         int nPbH_c = nPbH >> s->ps.sps->vshift[1];
    1838             : 
    1839     7858878 :         luma_mc_uni(s, dst0, s->frame->linesize[0], ref0->frame,
    1840             :                     &current_mv.mv[0], x0, y0, nPbW, nPbH,
    1841     3929439 :                     s->sh.luma_weight_l0[current_mv.ref_idx[0]],
    1842     3929439 :                     s->sh.luma_offset_l0[current_mv.ref_idx[0]]);
    1843             : 
    1844     3929439 :         if (s->ps.sps->chroma_format_idc) {
    1845     7858878 :             chroma_mc_uni(s, dst1, s->frame->linesize[1], ref0->frame->data[1], ref0->frame->linesize[1],
    1846             :                           0, x0_c, y0_c, nPbW_c, nPbH_c, &current_mv,
    1847     7858878 :                           s->sh.chroma_weight_l0[current_mv.ref_idx[0]][0], s->sh.chroma_offset_l0[current_mv.ref_idx[0]][0]);
    1848     7858878 :             chroma_mc_uni(s, dst2, s->frame->linesize[2], ref0->frame->data[2], ref0->frame->linesize[2],
    1849             :                           0, x0_c, y0_c, nPbW_c, nPbH_c, &current_mv,
    1850     7858878 :                           s->sh.chroma_weight_l0[current_mv.ref_idx[0]][1], s->sh.chroma_offset_l0[current_mv.ref_idx[0]][1]);
    1851             :         }
    1852     4542026 :     } else if (current_mv.pred_flag == PF_L1) {
    1853      760577 :         int x0_c = x0 >> s->ps.sps->hshift[1];
    1854      760577 :         int y0_c = y0 >> s->ps.sps->vshift[1];
    1855      760577 :         int nPbW_c = nPbW >> s->ps.sps->hshift[1];
    1856      760577 :         int nPbH_c = nPbH >> s->ps.sps->vshift[1];
    1857             : 
    1858     1521154 :         luma_mc_uni(s, dst0, s->frame->linesize[0], ref1->frame,
    1859             :                     &current_mv.mv[1], x0, y0, nPbW, nPbH,
    1860      760577 :                     s->sh.luma_weight_l1[current_mv.ref_idx[1]],
    1861      760577 :                     s->sh.luma_offset_l1[current_mv.ref_idx[1]]);
    1862             : 
    1863      760577 :         if (s->ps.sps->chroma_format_idc) {
    1864     1521154 :             chroma_mc_uni(s, dst1, s->frame->linesize[1], ref1->frame->data[1], ref1->frame->linesize[1],
    1865             :                           1, x0_c, y0_c, nPbW_c, nPbH_c, &current_mv,
    1866     1521154 :                           s->sh.chroma_weight_l1[current_mv.ref_idx[1]][0], s->sh.chroma_offset_l1[current_mv.ref_idx[1]][0]);
    1867             : 
    1868     1521154 :             chroma_mc_uni(s, dst2, s->frame->linesize[2], ref1->frame->data[2], ref1->frame->linesize[2],
    1869             :                           1, x0_c, y0_c, nPbW_c, nPbH_c, &current_mv,
    1870     1521154 :                           s->sh.chroma_weight_l1[current_mv.ref_idx[1]][1], s->sh.chroma_offset_l1[current_mv.ref_idx[1]][1]);
    1871             :         }
    1872     3781449 :     } else if (current_mv.pred_flag == PF_BI) {
    1873     3781449 :         int x0_c = x0 >> s->ps.sps->hshift[1];
    1874     3781449 :         int y0_c = y0 >> s->ps.sps->vshift[1];
    1875     3781449 :         int nPbW_c = nPbW >> s->ps.sps->hshift[1];
    1876     3781449 :         int nPbH_c = nPbH >> s->ps.sps->vshift[1];
    1877             : 
    1878     3781449 :         luma_mc_bi(s, dst0, s->frame->linesize[0], ref0->frame,
    1879             :                    &current_mv.mv[0], x0, y0, nPbW, nPbH,
    1880             :                    ref1->frame, &current_mv.mv[1], &current_mv);
    1881             : 
    1882     3781449 :         if (s->ps.sps->chroma_format_idc) {
    1883     3781449 :             chroma_mc_bi(s, dst1, s->frame->linesize[1], ref0->frame, ref1->frame,
    1884             :                          x0_c, y0_c, nPbW_c, nPbH_c, &current_mv, 0);
    1885             : 
    1886     3781449 :             chroma_mc_bi(s, dst2, s->frame->linesize[2], ref0->frame, ref1->frame,
    1887             :                          x0_c, y0_c, nPbW_c, nPbH_c, &current_mv, 1);
    1888             :         }
    1889             :     }
    1890             : }
    1891             : 
    1892             : /**
    1893             :  * 8.4.1
    1894             :  */
    1895     7268068 : static int luma_intra_pred_mode(HEVCContext *s, int x0, int y0, int pu_size,
    1896             :                                 int prev_intra_luma_pred_flag)
    1897             : {
    1898     7268068 :     HEVCLocalContext *lc = s->HEVClc;
    1899     7268068 :     int x_pu             = x0 >> s->ps.sps->log2_min_pu_size;
    1900     7268068 :     int y_pu             = y0 >> s->ps.sps->log2_min_pu_size;
    1901     7268068 :     int min_pu_width     = s->ps.sps->min_pu_width;
    1902     7268068 :     int size_in_pus      = pu_size >> s->ps.sps->log2_min_pu_size;
    1903     7268068 :     int x0b              = av_mod_uintp2(x0, s->ps.sps->log2_ctb_size);
    1904     7268068 :     int y0b              = av_mod_uintp2(y0, s->ps.sps->log2_ctb_size);
    1905             : 
    1906    15694230 :     int cand_up   = (lc->ctb_up_flag || y0b) ?
    1907    14418449 :                     s->tab_ipm[(y_pu - 1) * min_pu_width + x_pu] : INTRA_DC;
    1908    15168569 :     int cand_left = (lc->ctb_left_flag || x0b) ?
    1909    14462107 :                     s->tab_ipm[y_pu * min_pu_width + x_pu - 1]   : INTRA_DC;
    1910             : 
    1911     7268068 :     int y_ctb = (y0 >> (s->ps.sps->log2_ctb_size)) << (s->ps.sps->log2_ctb_size);
    1912             : 
    1913     7268068 :     MvField *tab_mvf = s->ref->tab_mvf;
    1914             :     int intra_pred_mode;
    1915             :     int candidate[3];
    1916             :     int i, j;
    1917             : 
    1918             :     // intra_pred_mode prediction does not cross vertical CTB boundaries
    1919     7268068 :     if ((y0 - 1) < y_ctb)
    1920      899583 :         cand_up = INTRA_DC;
    1921             : 
    1922     7268068 :     if (cand_left == cand_up) {
    1923     1508119 :         if (cand_left < 2) {
    1924      871613 :             candidate[0] = INTRA_PLANAR;
    1925      871613 :             candidate[1] = INTRA_DC;
    1926      871613 :             candidate[2] = INTRA_ANGULAR_26;
    1927             :         } else {
    1928      636506 :             candidate[0] = cand_left;
    1929      636506 :             candidate[1] = 2 + ((cand_left - 2 - 1 + 32) & 31);
    1930      636506 :             candidate[2] = 2 + ((cand_left - 2 + 1) & 31);
    1931             :         }
    1932             :     } else {
    1933     5759949 :         candidate[0] = cand_left;
    1934     5759949 :         candidate[1] = cand_up;
    1935     5759949 :         if (candidate[0] != INTRA_PLANAR && candidate[1] != INTRA_PLANAR) {
    1936     3994553 :             candidate[2] = INTRA_PLANAR;
    1937     1765396 :         } else if (candidate[0] != INTRA_DC && candidate[1] != INTRA_DC) {
    1938     1192079 :             candidate[2] = INTRA_DC;
    1939             :         } else {
    1940      573317 :             candidate[2] = INTRA_ANGULAR_26;
    1941             :         }
    1942             :     }
    1943             : 
    1944     7268068 :     if (prev_intra_luma_pred_flag) {
    1945     4322774 :         intra_pred_mode = candidate[lc->pu.mpm_idx];
    1946             :     } else {
    1947     2945294 :         if (candidate[0] > candidate[1])
    1948     1411151 :             FFSWAP(uint8_t, candidate[0], candidate[1]);
    1949     2945294 :         if (candidate[0] > candidate[2])
    1950     1761749 :             FFSWAP(uint8_t, candidate[0], candidate[2]);
    1951     2945294 :         if (candidate[1] > candidate[2])
    1952     2201581 :             FFSWAP(uint8_t, candidate[1], candidate[2]);
    1953             : 
    1954     2945294 :         intra_pred_mode = lc->pu.rem_intra_luma_pred_mode;
    1955    11781176 :         for (i = 0; i < 3; i++)
    1956     8835882 :             if (intra_pred_mode >= candidate[i])
    1957     6439741 :                 intra_pred_mode++;
    1958             :     }
    1959             : 
    1960             :     /* write the intra prediction units into the mv array */
    1961     7268068 :     if (!size_in_pus)
    1962           0 :         size_in_pus = 1;
    1963    19898238 :     for (i = 0; i < size_in_pus; i++) {
    1964    12630170 :         memset(&s->tab_ipm[(y_pu + i) * min_pu_width + x_pu],
    1965             :                intra_pred_mode, size_in_pus);
    1966             : 
    1967    51990318 :         for (j = 0; j < size_in_pus; j++) {
    1968    39360148 :             tab_mvf[(y_pu + j) * min_pu_width + x_pu + i].pred_flag = PF_INTRA;
    1969             :         }
    1970             :     }
    1971             : 
    1972     7268068 :     return intra_pred_mode;
    1973             : }
    1974             : 
    1975    10685443 : static av_always_inline void set_ct_depth(HEVCContext *s, int x0, int y0,
    1976             :                                           int log2_cb_size, int ct_depth)
    1977             : {
    1978    10685443 :     int length = (1 << log2_cb_size) >> s->ps.sps->log2_min_cb_size;
    1979    10685443 :     int x_cb   = x0 >> s->ps.sps->log2_min_cb_size;
    1980    10685443 :     int y_cb   = y0 >> s->ps.sps->log2_min_cb_size;
    1981             :     int y;
    1982             : 
    1983    30285248 :     for (y = 0; y < length; y++)
    1984    19599805 :         memset(&s->tab_ct_depth[(y_cb + y) * s->ps.sps->min_cb_width + x_cb],
    1985             :                ct_depth, length);
    1986    10685443 : }
    1987             : 
    1988             : static const uint8_t tab_mode_idx[] = {
    1989             :      0,  1,  2,  2,  2,  2,  3,  5,  7,  8, 10, 12, 13, 15, 17, 18, 19, 20,
    1990             :     21, 22, 23, 23, 24, 24, 25, 25, 26, 27, 27, 28, 28, 29, 29, 30, 31};
    1991             : 
    1992     3791320 : static void intra_prediction_unit(HEVCContext *s, int x0, int y0,
    1993             :                                   int log2_cb_size)
    1994             : {
    1995     3791320 :     HEVCLocalContext *lc = s->HEVClc;
    1996             :     static const uint8_t intra_chroma_table[4] = { 0, 26, 10, 1 };
    1997             :     uint8_t prev_intra_luma_pred_flag[4];
    1998     3791320 :     int split   = lc->cu.part_mode == PART_NxN;
    1999     3791320 :     int pb_size = (1 << log2_cb_size) >> split;
    2000     3791320 :     int side    = split + 1;
    2001             :     int chroma_mode;
    2002             :     int i, j;
    2003             : 
    2004     8741556 :     for (i = 0; i < side; i++)
    2005    12218304 :         for (j = 0; j < side; j++)
    2006     7268068 :             prev_intra_luma_pred_flag[2 * i + j] = ff_hevc_prev_intra_luma_pred_flag_decode(s);
    2007             : 
    2008     8741556 :     for (i = 0; i < side; i++) {
    2009    12218304 :         for (j = 0; j < side; j++) {
    2010     7268068 :             if (prev_intra_luma_pred_flag[2 * i + j])
    2011     4322774 :                 lc->pu.mpm_idx = ff_hevc_mpm_idx_decode(s);
    2012             :             else
    2013     2945294 :                 lc->pu.rem_intra_luma_pred_mode = ff_hevc_rem_intra_luma_pred_mode_decode(s);
    2014             : 
    2015     7268068 :             lc->pu.intra_pred_mode[2 * i + j] =
    2016     7268068 :                 luma_intra_pred_mode(s, x0 + pb_size * j, y0 + pb_size * i, pb_size,
    2017     7268068 :                                      prev_intra_luma_pred_flag[2 * i + j]);
    2018             :         }
    2019             :     }
    2020             : 
    2021     3791320 :     if (s->ps.sps->chroma_format_idc == 3) {
    2022      243859 :         for (i = 0; i < side; i++) {
    2023      345412 :             for (j = 0; j < side; j++) {
    2024      206557 :                 lc->pu.chroma_mode_c[2 * i + j] = chroma_mode = ff_hevc_intra_chroma_pred_mode_decode(s);
    2025      206557 :                 if (chroma_mode != 4) {
    2026       34248 :                     if (lc->pu.intra_pred_mode[2 * i + j] == intra_chroma_table[chroma_mode])
    2027        2343 :                         lc->pu.intra_pred_mode_c[2 * i + j] = 34;
    2028             :                     else
    2029       31905 :                         lc->pu.intra_pred_mode_c[2 * i + j] = intra_chroma_table[chroma_mode];
    2030             :                 } else {
    2031      172309 :                     lc->pu.intra_pred_mode_c[2 * i + j] = lc->pu.intra_pred_mode[2 * i + j];
    2032             :                 }
    2033             :             }
    2034             :         }
    2035     3686316 :     } else if (s->ps.sps->chroma_format_idc == 2) {
    2036             :         int mode_idx;
    2037      212786 :         lc->pu.chroma_mode_c[0] = chroma_mode = ff_hevc_intra_chroma_pred_mode_decode(s);
    2038      212786 :         if (chroma_mode != 4) {
    2039       73995 :             if (lc->pu.intra_pred_mode[0] == intra_chroma_table[chroma_mode])
    2040        3515 :                 mode_idx = 34;
    2041             :             else
    2042       70480 :                 mode_idx = intra_chroma_table[chroma_mode];
    2043             :         } else {
    2044      138791 :             mode_idx = lc->pu.intra_pred_mode[0];
    2045             :         }
    2046      212786 :         lc->pu.intra_pred_mode_c[0] = tab_mode_idx[mode_idx];
    2047     3473530 :     } else if (s->ps.sps->chroma_format_idc != 0) {
    2048     3473530 :         chroma_mode = ff_hevc_intra_chroma_pred_mode_decode(s);
    2049     3473530 :         if (chroma_mode != 4) {
    2050      910813 :             if (lc->pu.intra_pred_mode[0] == intra_chroma_table[chroma_mode])
    2051       67294 :                 lc->pu.intra_pred_mode_c[0] = 34;
    2052             :             else
    2053      843519 :                 lc->pu.intra_pred_mode_c[0] = intra_chroma_table[chroma_mode];
    2054             :         } else {
    2055     2562717 :             lc->pu.intra_pred_mode_c[0] = lc->pu.intra_pred_mode[0];
    2056             :         }
    2057             :     }
    2058     3791320 : }
    2059             : 
    2060     6894123 : static void intra_prediction_unit_default_value(HEVCContext *s,
    2061             :                                                 int x0, int y0,
    2062             :                                                 int log2_cb_size)
    2063             : {
    2064     6894123 :     HEVCLocalContext *lc = s->HEVClc;
    2065     6894123 :     int pb_size          = 1 << log2_cb_size;
    2066     6894123 :     int size_in_pus      = pb_size >> s->ps.sps->log2_min_pu_size;
    2067     6894123 :     int min_pu_width     = s->ps.sps->min_pu_width;
    2068     6894123 :     MvField *tab_mvf     = s->ref->tab_mvf;
    2069     6894123 :     int x_pu             = x0 >> s->ps.sps->log2_min_pu_size;
    2070     6894123 :     int y_pu             = y0 >> s->ps.sps->log2_min_pu_size;
    2071             :     int j, k;
    2072             : 
    2073     6894123 :     if (size_in_pus == 0)
    2074           0 :         size_in_pus = 1;
    2075    35781395 :     for (j = 0; j < size_in_pus; j++)
    2076    28887272 :         memset(&s->tab_ipm[(y_pu + j) * min_pu_width + x_pu], INTRA_DC, size_in_pus);
    2077     6894123 :     if (lc->cu.pred_mode == MODE_INTRA)
    2078       53341 :         for (j = 0; j < size_in_pus; j++)
    2079      223420 :             for (k = 0; k < size_in_pus; k++)
    2080      182512 :                 tab_mvf[(y_pu + j) * min_pu_width + x_pu + k].pred_flag = PF_INTRA;
    2081     6894123 : }
    2082             : 
    2083    10685443 : static int hls_coding_unit(HEVCContext *s, int x0, int y0, int log2_cb_size)
    2084             : {
    2085    10685443 :     int cb_size          = 1 << log2_cb_size;
    2086    10685443 :     HEVCLocalContext *lc = s->HEVClc;
    2087    10685443 :     int log2_min_cb_size = s->ps.sps->log2_min_cb_size;
    2088    10685443 :     int length           = cb_size >> log2_min_cb_size;
    2089    10685443 :     int min_cb_width     = s->ps.sps->min_cb_width;
    2090    10685443 :     int x_cb             = x0 >> log2_min_cb_size;
    2091    10685443 :     int y_cb             = y0 >> log2_min_cb_size;
    2092    10685443 :     int idx              = log2_cb_size - 2;
    2093    10685443 :     int qp_block_mask    = (1<<(s->ps.sps->log2_ctb_size - s->ps.pps->diff_cu_qp_delta_depth)) - 1;
    2094             :     int x, y, ret;
    2095             : 
    2096    10685443 :     lc->cu.x                = x0;
    2097    10685443 :     lc->cu.y                = y0;
    2098    10685443 :     lc->cu.pred_mode        = MODE_INTRA;
    2099    10685443 :     lc->cu.part_mode        = PART_2Nx2N;
    2100    10685443 :     lc->cu.intra_split_flag = 0;
    2101             : 
    2102    10685443 :     SAMPLE_CTB(s->skip_flag, x_cb, y_cb) = 0;
    2103    53427215 :     for (x = 0; x < 4; x++)
    2104    42741772 :         lc->pu.intra_pred_mode[x] = 1;
    2105    10685443 :     if (s->ps.pps->transquant_bypass_enable_flag) {
    2106      200139 :         lc->cu.cu_transquant_bypass_flag = ff_hevc_cu_transquant_bypass_flag_decode(s);
    2107      200139 :         if (lc->cu.cu_transquant_bypass_flag)
    2108       80538 :             set_deblocking_bypass(s, x0, y0, log2_cb_size);
    2109             :     } else
    2110    10485304 :         lc->cu.cu_transquant_bypass_flag = 0;
    2111             : 
    2112    10685443 :     if (s->sh.slice_type != HEVC_SLICE_I) {
    2113     7929772 :         uint8_t skip_flag = ff_hevc_skip_flag_decode(s, x0, y0, x_cb, y_cb);
    2114             : 
    2115     7929772 :         x = y_cb * min_cb_width + x_cb;
    2116    23929727 :         for (y = 0; y < length; y++) {
    2117    15999955 :             memset(&s->skip_flag[x], skip_flag, length);
    2118    15999955 :             x += min_cb_width;
    2119             :         }
    2120     7929772 :         lc->cu.pred_mode = skip_flag ? MODE_SKIP : MODE_INTER;
    2121             :     } else {
    2122     2755671 :         x = y_cb * min_cb_width + x_cb;
    2123     6355521 :         for (y = 0; y < length; y++) {
    2124     3599850 :             memset(&s->skip_flag[x], 0, length);
    2125     3599850 :             x += min_cb_width;
    2126             :         }
    2127             :     }
    2128             : 
    2129    10685443 :     if (SAMPLE_CTB(s->skip_flag, x_cb, y_cb)) {
    2130     3798697 :         hls_prediction_unit(s, x0, y0, cb_size, cb_size, log2_cb_size, 0, idx);
    2131     3798697 :         intra_prediction_unit_default_value(s, x0, y0, log2_cb_size);
    2132             : 
    2133     3798697 :         if (!s->sh.disable_deblocking_filter_flag)
    2134     3652184 :             ff_hevc_deblocking_boundary_strengths(s, x0, y0, log2_cb_size);
    2135             :     } else {
    2136     6886746 :         int pcm_flag = 0;
    2137             : 
    2138     6886746 :         if (s->sh.slice_type != HEVC_SLICE_I)
    2139     4131075 :             lc->cu.pred_mode = ff_hevc_pred_mode_decode(s);
    2140    10690499 :         if (lc->cu.pred_mode != MODE_INTRA ||
    2141     3803753 :             log2_cb_size == s->ps.sps->log2_min_cb_size) {
    2142     6010030 :             lc->cu.part_mode        = ff_hevc_part_mode_decode(s, log2_cb_size);
    2143     7274582 :             lc->cu.intra_split_flag = lc->cu.part_mode == PART_NxN &&
    2144     1264552 :                                       lc->cu.pred_mode == MODE_INTRA;
    2145             :         }
    2146             : 
    2147     6886746 :         if (lc->cu.pred_mode == MODE_INTRA) {
    2148     3966437 :             if (lc->cu.part_mode == PART_2Nx2N && s->ps.sps->pcm_enabled_flag &&
    2149      323499 :                 log2_cb_size >= s->ps.sps->pcm.log2_min_pcm_cb_size &&
    2150      160815 :                 log2_cb_size <= s->ps.sps->pcm.log2_max_pcm_cb_size) {
    2151      153183 :                 pcm_flag = ff_hevc_pcm_flag_decode(s);
    2152             :             }
    2153     3803753 :             if (pcm_flag) {
    2154       12433 :                 intra_prediction_unit_default_value(s, x0, y0, log2_cb_size);
    2155       12433 :                 ret = hls_pcm_sample(s, x0, y0, log2_cb_size);
    2156       12433 :                 if (s->ps.sps->pcm.loop_filter_disable_flag)
    2157           8 :                     set_deblocking_bypass(s, x0, y0, log2_cb_size);
    2158             : 
    2159       12433 :                 if (ret < 0)
    2160           0 :                     return ret;
    2161             :             } else {
    2162     3791320 :                 intra_prediction_unit(s, x0, y0, log2_cb_size);
    2163             :             }
    2164             :         } else {
    2165     3082993 :             intra_prediction_unit_default_value(s, x0, y0, log2_cb_size);
    2166     3082993 :             switch (lc->cu.part_mode) {
    2167     1704490 :             case PART_2Nx2N:
    2168     1704490 :                 hls_prediction_unit(s, x0, y0, cb_size, cb_size, log2_cb_size, 0, idx);
    2169     1704490 :                 break;
    2170      445726 :             case PART_2NxN:
    2171      445726 :                 hls_prediction_unit(s, x0, y0,               cb_size, cb_size / 2, log2_cb_size, 0, idx);
    2172      445726 :                 hls_prediction_unit(s, x0, y0 + cb_size / 2, cb_size, cb_size / 2, log2_cb_size, 1, idx);
    2173      445726 :                 break;
    2174      535018 :             case PART_Nx2N:
    2175      535018 :                 hls_prediction_unit(s, x0,               y0, cb_size / 2, cb_size, log2_cb_size, 0, idx - 1);
    2176      535018 :                 hls_prediction_unit(s, x0 + cb_size / 2, y0, cb_size / 2, cb_size, log2_cb_size, 1, idx - 1);
    2177      535018 :                 break;
    2178       63734 :             case PART_2NxnU:
    2179       63734 :                 hls_prediction_unit(s, x0, y0,               cb_size, cb_size     / 4, log2_cb_size, 0, idx);
    2180       63734 :                 hls_prediction_unit(s, x0, y0 + cb_size / 4, cb_size, cb_size * 3 / 4, log2_cb_size, 1, idx);
    2181       63734 :                 break;
    2182       55171 :             case PART_2NxnD:
    2183       55171 :                 hls_prediction_unit(s, x0, y0,                   cb_size, cb_size * 3 / 4, log2_cb_size, 0, idx);
    2184       55171 :                 hls_prediction_unit(s, x0, y0 + cb_size * 3 / 4, cb_size, cb_size     / 4, log2_cb_size, 1, idx);
    2185       55171 :                 break;
    2186       93167 :             case PART_nLx2N:
    2187       93167 :                 hls_prediction_unit(s, x0,               y0, cb_size     / 4, cb_size, log2_cb_size, 0, idx - 2);
    2188       93167 :                 hls_prediction_unit(s, x0 + cb_size / 4, y0, cb_size * 3 / 4, cb_size, log2_cb_size, 1, idx - 2);
    2189       93167 :                 break;
    2190       80051 :             case PART_nRx2N:
    2191       80051 :                 hls_prediction_unit(s, x0,                   y0, cb_size * 3 / 4, cb_size, log2_cb_size, 0, idx - 2);
    2192       80051 :                 hls_prediction_unit(s, x0 + cb_size * 3 / 4, y0, cb_size     / 4, cb_size, log2_cb_size, 1, idx - 2);
    2193       80051 :                 break;
    2194      105636 :             case PART_NxN:
    2195      105636 :                 hls_prediction_unit(s, x0,               y0,               cb_size / 2, cb_size / 2, log2_cb_size, 0, idx - 1);
    2196      105636 :                 hls_prediction_unit(s, x0 + cb_size / 2, y0,               cb_size / 2, cb_size / 2, log2_cb_size, 1, idx - 1);
    2197      105636 :                 hls_prediction_unit(s, x0,               y0 + cb_size / 2, cb_size / 2, cb_size / 2, log2_cb_size, 2, idx - 1);
    2198      105636 :                 hls_prediction_unit(s, x0 + cb_size / 2, y0 + cb_size / 2, cb_size / 2, cb_size / 2, log2_cb_size, 3, idx - 1);
    2199      105636 :                 break;
    2200             :             }
    2201             :         }
    2202             : 
    2203     6886746 :         if (!pcm_flag) {
    2204     6874313 :             int rqt_root_cbf = 1;
    2205             : 
    2206     9957306 :             if (lc->cu.pred_mode != MODE_INTRA &&
    2207     4787483 :                 !(lc->cu.part_mode == PART_2Nx2N && lc->pu.merge_flag)) {
    2208     2333895 :                 rqt_root_cbf = ff_hevc_no_residual_syntax_flag_decode(s);
    2209             :             }
    2210     6874313 :             if (rqt_root_cbf) {
    2211             :                 const static int cbf[2] = { 0 };
    2212    11129964 :                 lc->cu.max_trafo_depth = lc->cu.pred_mode == MODE_INTRA ?
    2213     3791320 :                                          s->ps.sps->max_transform_hierarchy_depth_intra + lc->cu.intra_split_flag :
    2214     1773662 :                                          s->ps.sps->max_transform_hierarchy_depth_inter;
    2215     5564982 :                 ret = hls_transform_tree(s, x0, y0, x0, y0, x0, y0,
    2216             :                                          log2_cb_size,
    2217             :                                          log2_cb_size, 0, 0, cbf, cbf);
    2218     5564982 :                 if (ret < 0)
    2219           0 :                     return ret;
    2220             :             } else {
    2221     1309331 :                 if (!s->sh.disable_deblocking_filter_flag)
    2222     1252899 :                     ff_hevc_deblocking_boundary_strengths(s, x0, y0, log2_cb_size);
    2223             :             }
    2224             :         }
    2225             :     }
    2226             : 
    2227    10685443 :     if (s->ps.pps->cu_qp_delta_enabled_flag && lc->tu.is_cu_qp_delta_coded == 0)
    2228      662047 :         ff_hevc_set_qPy(s, x0, y0, log2_cb_size);
    2229             : 
    2230    10685443 :     x = y_cb * min_cb_width + x_cb;
    2231    30285248 :     for (y = 0; y < length; y++) {
    2232    19599805 :         memset(&s->qp_y_tab[x], lc->qp_y, length);
    2233    19599805 :         x += min_cb_width;
    2234             :     }
    2235             : 
    2236    14201591 :     if(((x0 + (1<<log2_cb_size)) & qp_block_mask) == 0 &&
    2237     3516148 :        ((y0 + (1<<log2_cb_size)) & qp_block_mask) == 0) {
    2238     1996770 :         lc->qPy_pred = lc->qp_y;
    2239             :     }
    2240             : 
    2241    10685443 :     set_ct_depth(s, x0, y0, log2_cb_size, lc->ct_depth);
    2242             : 
    2243    10685443 :     return 0;
    2244             : }
    2245             : 
    2246    13946848 : static int hls_coding_quadtree(HEVCContext *s, int x0, int y0,
    2247             :                                int log2_cb_size, int cb_depth)
    2248             : {
    2249    13946848 :     HEVCLocalContext *lc = s->HEVClc;
    2250    13946848 :     const int cb_size    = 1 << log2_cb_size;
    2251             :     int ret;
    2252             :     int split_cu;
    2253             : 
    2254    13946848 :     lc->ct_depth = cb_depth;
    2255    27865097 :     if (x0 + cb_size <= s->ps.sps->width  &&
    2256    27529248 :         y0 + cb_size <= s->ps.sps->height &&
    2257    13610999 :         log2_cb_size > s->ps.sps->log2_min_cb_size) {
    2258     7256594 :         split_cu = ff_hevc_split_coding_unit_flag_decode(s, cb_depth, x0, y0);
    2259             :     } else {
    2260     6690254 :         split_cu = (log2_cb_size > s->ps.sps->log2_min_cb_size);
    2261             :     }
    2262    16106592 :     if (s->ps.pps->cu_qp_delta_enabled_flag &&
    2263     2159744 :         log2_cb_size >= s->ps.sps->log2_ctb_size - s->ps.pps->diff_cu_qp_delta_depth) {
    2264     1192181 :         lc->tu.is_cu_qp_delta_coded = 0;
    2265     1192181 :         lc->tu.cu_qp_delta          = 0;
    2266             :     }
    2267             : 
    2268    14466174 :     if (s->sh.cu_chroma_qp_offset_enabled_flag &&
    2269      519326 :         log2_cb_size >= s->ps.sps->log2_ctb_size - s->ps.pps->diff_cu_chroma_qp_offset_depth) {
    2270      519326 :         lc->tu.is_cu_chroma_qp_offset_coded = 0;
    2271             :     }
    2272             : 
    2273    13946848 :     if (split_cu) {
    2274     3261405 :         int qp_block_mask = (1<<(s->ps.sps->log2_ctb_size - s->ps.pps->diff_cu_qp_delta_depth)) - 1;
    2275     3261405 :         const int cb_size_split = cb_size >> 1;
    2276     3261405 :         const int x1 = x0 + cb_size_split;
    2277     3261405 :         const int y1 = y0 + cb_size_split;
    2278             : 
    2279     3261405 :         int more_data = 0;
    2280             : 
    2281     3261405 :         more_data = hls_coding_quadtree(s, x0, y0, log2_cb_size - 1, cb_depth + 1);
    2282     3261405 :         if (more_data < 0)
    2283           0 :             return more_data;
    2284             : 
    2285     3261405 :         if (more_data && x1 < s->ps.sps->width) {
    2286     3233719 :             more_data = hls_coding_quadtree(s, x1, y0, log2_cb_size - 1, cb_depth + 1);
    2287     3233719 :             if (more_data < 0)
    2288           0 :                 return more_data;
    2289             :         }
    2290     3261405 :         if (more_data && y1 < s->ps.sps->height) {
    2291     3059001 :             more_data = hls_coding_quadtree(s, x0, y1, log2_cb_size - 1, cb_depth + 1);
    2292     3059001 :             if (more_data < 0)
    2293           0 :                 return more_data;
    2294             :         }
    2295     6484951 :         if (more_data && x1 < s->ps.sps->width &&
    2296     3223546 :             y1 < s->ps.sps->height) {
    2297     3031324 :             more_data = hls_coding_quadtree(s, x1, y1, log2_cb_size - 1, cb_depth + 1);
    2298     3031324 :             if (more_data < 0)
    2299           0 :                 return more_data;
    2300             :         }
    2301             : 
    2302     5062944 :         if(((x0 + (1<<log2_cb_size)) & qp_block_mask) == 0 &&
    2303     1801539 :             ((y0 + (1<<log2_cb_size)) & qp_block_mask) == 0)
    2304     1324948 :             lc->qPy_pred = lc->qp_y;
    2305             : 
    2306     3261405 :         if (more_data)
    2307     3315991 :             return ((x1 + cb_size_split) < s->ps.sps->width ||
    2308       89925 :                     (y1 + cb_size_split) < s->ps.sps->height);
    2309             :         else
    2310       35339 :             return 0;
    2311             :     } else {
    2312    10685443 :         ret = hls_coding_unit(s, x0, y0, log2_cb_size);
    2313    10685443 :         if (ret < 0)
    2314           0 :             return ret;
    2315    21370886 :         if ((!((x0 + cb_size) %
    2316    18503789 :                (1 << (s->ps.sps->log2_ctb_size))) ||
    2317    10756116 :              (x0 + cb_size >= s->ps.sps->width)) &&
    2318     5875540 :             (!((y0 + cb_size) %
    2319     4608581 :                (1 << (s->ps.sps->log2_ctb_size))) ||
    2320     1670811 :              (y0 + cb_size >= s->ps.sps->height))) {
    2321     1361399 :             int end_of_slice_flag = ff_hevc_end_of_slice_flag_decode(s);
    2322     1361399 :             return !end_of_slice_flag;
    2323             :         } else {
    2324     9324044 :             return 1;
    2325             :         }
    2326             :     }
    2327             : 
    2328             :     return 0;
    2329             : }
    2330             : 
    2331     1361399 : static void hls_decode_neighbour(HEVCContext *s, int x_ctb, int y_ctb,
    2332             :                                  int ctb_addr_ts)
    2333             : {
    2334     1361399 :     HEVCLocalContext *lc  = s->HEVClc;
    2335     1361399 :     int ctb_size          = 1 << s->ps.sps->log2_ctb_size;
    2336     1361399 :     int ctb_addr_rs       = s->ps.pps->ctb_addr_ts_to_rs[ctb_addr_ts];
    2337     1361399 :     int ctb_addr_in_slice = ctb_addr_rs - s->sh.slice_addr;
    2338             : 
    2339     1361399 :     s->tab_slice_address[ctb_addr_rs] = s->sh.slice_addr;
    2340             : 
    2341     1361399 :     if (s->ps.pps->entropy_coding_sync_enabled_flag) {
    2342       93336 :         if (x_ctb == 0 && (y_ctb & (ctb_size - 1)) == 0)
    2343        5326 :             lc->first_qp_group = 1;
    2344       93336 :         lc->end_of_tiles_x = s->ps.sps->width;
    2345     1268063 :     } else if (s->ps.pps->tiles_enabled_flag) {
    2346      271890 :         if (ctb_addr_ts && s->ps.pps->tile_id[ctb_addr_ts] != s->ps.pps->tile_id[ctb_addr_ts - 1]) {
    2347        6253 :             int idxX = s->ps.pps->col_idxX[x_ctb >> s->ps.sps->log2_ctb_size];
    2348        6253 :             lc->end_of_tiles_x   = x_ctb + (s->ps.pps->column_width[idxX] << s->ps.sps->log2_ctb_size);
    2349        6253 :             lc->first_qp_group   = 1;
    2350             :         }
    2351             :     } else {
    2352      996173 :         lc->end_of_tiles_x = s->ps.sps->width;
    2353             :     }
    2354             : 
    2355     1361399 :     lc->end_of_tiles_y = FFMIN(y_ctb + ctb_size, s->ps.sps->height);
    2356             : 
    2357     1361399 :     lc->boundary_flags = 0;
    2358     1361399 :     if (s->ps.pps->tiles_enabled_flag) {
    2359      271890 :         if (x_ctb > 0 && s->ps.pps->tile_id[ctb_addr_ts] != s->ps.pps->tile_id[s->ps.pps->ctb_addr_rs_to_ts[ctb_addr_rs - 1]])
    2360       30253 :             lc->boundary_flags |= BOUNDARY_LEFT_TILE;
    2361      271890 :         if (x_ctb > 0 && s->tab_slice_address[ctb_addr_rs] != s->tab_slice_address[ctb_addr_rs - 1])
    2362        3649 :             lc->boundary_flags |= BOUNDARY_LEFT_SLICE;
    2363      271890 :         if (y_ctb > 0 && s->ps.pps->tile_id[ctb_addr_ts] != s->ps.pps->tile_id[s->ps.pps->ctb_addr_rs_to_ts[ctb_addr_rs - s->ps.sps->ctb_width]])
    2364       33061 :             lc->boundary_flags |= BOUNDARY_UPPER_TILE;
    2365      271890 :         if (y_ctb > 0 && s->tab_slice_address[ctb_addr_rs] != s->tab_slice_address[ctb_addr_rs - s->ps.sps->ctb_width])
    2366       13770 :             lc->boundary_flags |= BOUNDARY_UPPER_SLICE;
    2367             :     } else {
    2368     1089509 :         if (ctb_addr_in_slice <= 0)
    2369       17514 :             lc->boundary_flags |= BOUNDARY_LEFT_SLICE;
    2370     1089509 :         if (ctb_addr_in_slice < s->ps.sps->ctb_width)
    2371      182843 :             lc->boundary_flags |= BOUNDARY_UPPER_SLICE;
    2372             :     }
    2373             : 
    2374     1361399 :     lc->ctb_left_flag = ((x_ctb > 0) && (ctb_addr_in_slice > 0) && !(lc->boundary_flags & BOUNDARY_LEFT_TILE));
    2375     1361399 :     lc->ctb_up_flag   = ((y_ctb > 0) && (ctb_addr_in_slice >= s->ps.sps->ctb_width) && !(lc->boundary_flags & BOUNDARY_UPPER_TILE));
    2376     1361399 :     lc->ctb_up_right_flag = ((y_ctb > 0)  && (ctb_addr_in_slice+1 >= s->ps.sps->ctb_width) && (s->ps.pps->tile_id[ctb_addr_ts] == s->ps.pps->tile_id[s->ps.pps->ctb_addr_rs_to_ts[ctb_addr_rs+1 - s->ps.sps->ctb_width]]));
    2377     1361399 :     lc->ctb_up_left_flag = ((x_ctb > 0) && (y_ctb > 0)  && (ctb_addr_in_slice-1 >= s->ps.sps->ctb_width) && (s->ps.pps->tile_id[ctb_addr_ts] == s->ps.pps->tile_id[s->ps.pps->ctb_addr_rs_to_ts[ctb_addr_rs-1 - s->ps.sps->ctb_width]]));
    2378     1361399 : }
    2379             : 
    2380       27424 : static int hls_decode_entry(AVCodecContext *avctxt, void *isFilterThread)
    2381             : {
    2382       27424 :     HEVCContext *s  = avctxt->priv_data;
    2383       27424 :     int ctb_size    = 1 << s->ps.sps->log2_ctb_size;
    2384       27424 :     int more_data   = 1;
    2385       27424 :     int x_ctb       = 0;
    2386       27424 :     int y_ctb       = 0;
    2387       27424 :     int ctb_addr_ts = s->ps.pps->ctb_addr_rs_to_ts[s->sh.slice_ctb_addr_rs];
    2388             :     int ret;
    2389             : 
    2390       27424 :     if (!ctb_addr_ts && s->sh.dependent_slice_segment_flag) {
    2391           0 :         av_log(s->avctx, AV_LOG_ERROR, "Impossible initial tile.\n");
    2392           0 :         return AVERROR_INVALIDDATA;
    2393             :     }
    2394             : 
    2395       27424 :     if (s->sh.dependent_slice_segment_flag) {
    2396        7947 :         int prev_rs = s->ps.pps->ctb_addr_ts_to_rs[ctb_addr_ts - 1];
    2397        7947 :         if (s->tab_slice_address[prev_rs] != s->sh.slice_addr) {
    2398           0 :             av_log(s->avctx, AV_LOG_ERROR, "Previous slice segment missing\n");
    2399           0 :             return AVERROR_INVALIDDATA;
    2400             :         }
    2401             :     }
    2402             : 
    2403     1416247 :     while (more_data && ctb_addr_ts < s->ps.sps->ctb_size) {
    2404     1361399 :         int ctb_addr_rs = s->ps.pps->ctb_addr_ts_to_rs[ctb_addr_ts];
    2405             : 
    2406     1361399 :         x_ctb = (ctb_addr_rs % ((s->ps.sps->width + ctb_size - 1) >> s->ps.sps->log2_ctb_size)) << s->ps.sps->log2_ctb_size;
    2407     1361399 :         y_ctb = (ctb_addr_rs / ((s->ps.sps->width + ctb_size - 1) >> s->ps.sps->log2_ctb_size)) << s->ps.sps->log2_ctb_size;
    2408     1361399 :         hls_decode_neighbour(s, x_ctb, y_ctb, ctb_addr_ts);
    2409             : 
    2410     1361399 :         ret = ff_hevc_cabac_init(s, ctb_addr_ts);
    2411     1361399 :         if (ret < 0) {
    2412           0 :             s->tab_slice_address[ctb_addr_rs] = -1;
    2413           0 :             return ret;
    2414             :         }
    2415             : 
    2416     1361399 :         hls_sao_param(s, x_ctb >> s->ps.sps->log2_ctb_size, y_ctb >> s->ps.sps->log2_ctb_size);
    2417             : 
    2418     1361399 :         s->deblock[ctb_addr_rs].beta_offset = s->sh.beta_offset;
    2419     1361399 :         s->deblock[ctb_addr_rs].tc_offset   = s->sh.tc_offset;
    2420     1361399 :         s->filter_slice_edges[ctb_addr_rs]  = s->sh.slice_loop_filter_across_slices_enabled_flag;
    2421             : 
    2422     1361399 :         more_data = hls_coding_quadtree(s, x_ctb, y_ctb, s->ps.sps->log2_ctb_size, 0);
    2423     1361399 :         if (more_data < 0) {
    2424           0 :             s->tab_slice_address[ctb_addr_rs] = -1;
    2425           0 :             return more_data;
    2426             :         }
    2427             : 
    2428             : 
    2429     1361399 :         ctb_addr_ts++;
    2430     1361399 :         ff_hevc_save_states(s, ctb_addr_ts);
    2431     1361399 :         ff_hevc_hls_filters(s, x_ctb, y_ctb, ctb_size);
    2432             :     }
    2433             : 
    2434       41342 :     if (x_ctb + ctb_size >= s->ps.sps->width &&
    2435       13918 :         y_ctb + ctb_size >= s->ps.sps->height)
    2436        9443 :         ff_hevc_hls_filter(s, x_ctb, y_ctb, ctb_size);
    2437             : 
    2438       27424 :     return ctb_addr_ts;
    2439             : }
    2440             : 
    2441       27424 : static int hls_slice_data(HEVCContext *s)
    2442             : {
    2443             :     int arg[2];
    2444             :     int ret[2];
    2445             : 
    2446       27424 :     arg[0] = 0;
    2447       27424 :     arg[1] = 1;
    2448             : 
    2449       27424 :     s->avctx->execute(s->avctx, hls_decode_entry, arg, ret , 1, sizeof(int));
    2450       27424 :     return ret[0];
    2451             : }
    2452           0 : static int hls_decode_entry_wpp(AVCodecContext *avctxt, void *input_ctb_row, int job, int self_id)
    2453             : {
    2454           0 :     HEVCContext *s1  = avctxt->priv_data, *s;
    2455             :     HEVCLocalContext *lc;
    2456           0 :     int ctb_size    = 1<< s1->ps.sps->log2_ctb_size;
    2457           0 :     int more_data   = 1;
    2458           0 :     int *ctb_row_p    = input_ctb_row;
    2459           0 :     int ctb_row = ctb_row_p[job];
    2460           0 :     int ctb_addr_rs = s1->sh.slice_ctb_addr_rs + ctb_row * ((s1->ps.sps->width + ctb_size - 1) >> s1->ps.sps->log2_ctb_size);
    2461           0 :     int ctb_addr_ts = s1->ps.pps->ctb_addr_rs_to_ts[ctb_addr_rs];
    2462           0 :     int thread = ctb_row % s1->threads_number;
    2463             :     int ret;
    2464             : 
    2465           0 :     s = s1->sList[self_id];
    2466           0 :     lc = s->HEVClc;
    2467             : 
    2468           0 :     if(ctb_row) {
    2469           0 :         ret = init_get_bits8(&lc->gb, s->data + s->sh.offset[ctb_row - 1], s->sh.size[ctb_row - 1]);
    2470           0 :         if (ret < 0)
    2471           0 :             goto error;
    2472           0 :         ff_init_cabac_decoder(&lc->cc, s->data + s->sh.offset[(ctb_row)-1], s->sh.size[ctb_row - 1]);
    2473             :     }
    2474             : 
    2475           0 :     while(more_data && ctb_addr_ts < s->ps.sps->ctb_size) {
    2476           0 :         int x_ctb = (ctb_addr_rs % s->ps.sps->ctb_width) << s->ps.sps->log2_ctb_size;
    2477           0 :         int y_ctb = (ctb_addr_rs / s->ps.sps->ctb_width) << s->ps.sps->log2_ctb_size;
    2478             : 
    2479           0 :         hls_decode_neighbour(s, x_ctb, y_ctb, ctb_addr_ts);
    2480             : 
    2481           0 :         ff_thread_await_progress2(s->avctx, ctb_row, thread, SHIFT_CTB_WPP);
    2482             : 
    2483           0 :         if (atomic_load(&s1->wpp_err)) {
    2484           0 :             ff_thread_report_progress2(s->avctx, ctb_row , thread, SHIFT_CTB_WPP);
    2485           0 :             return 0;
    2486             :         }
    2487             : 
    2488           0 :         ret = ff_hevc_cabac_init(s, ctb_addr_ts);
    2489           0 :         if (ret < 0)
    2490           0 :             goto error;
    2491           0 :         hls_sao_param(s, x_ctb >> s->ps.sps->log2_ctb_size, y_ctb >> s->ps.sps->log2_ctb_size);
    2492           0 :         more_data = hls_coding_quadtree(s, x_ctb, y_ctb, s->ps.sps->log2_ctb_size, 0);
    2493             : 
    2494           0 :         if (more_data < 0) {
    2495           0 :             ret = more_data;
    2496           0 :             goto error;
    2497             :         }
    2498             : 
    2499           0 :         ctb_addr_ts++;
    2500             : 
    2501           0 :         ff_hevc_save_states(s, ctb_addr_ts);
    2502           0 :         ff_thread_report_progress2(s->avctx, ctb_row, thread, 1);
    2503           0 :         ff_hevc_hls_filters(s, x_ctb, y_ctb, ctb_size);
    2504             : 
    2505           0 :         if (!more_data && (x_ctb+ctb_size) < s->ps.sps->width && ctb_row != s->sh.num_entry_point_offsets) {
    2506           0 :             atomic_store(&s1->wpp_err, 1);
    2507           0 :             ff_thread_report_progress2(s->avctx, ctb_row ,thread, SHIFT_CTB_WPP);
    2508           0 :             return 0;
    2509             :         }
    2510             : 
    2511           0 :         if ((x_ctb+ctb_size) >= s->ps.sps->width && (y_ctb+ctb_size) >= s->ps.sps->height ) {
    2512           0 :             ff_hevc_hls_filter(s, x_ctb, y_ctb, ctb_size);
    2513           0 :             ff_thread_report_progress2(s->avctx, ctb_row , thread, SHIFT_CTB_WPP);
    2514           0 :             return ctb_addr_ts;
    2515             :         }
    2516           0 :         ctb_addr_rs       = s->ps.pps->ctb_addr_ts_to_rs[ctb_addr_ts];
    2517           0 :         x_ctb+=ctb_size;
    2518             : 
    2519           0 :         if(x_ctb >= s->ps.sps->width) {
    2520           0 :             break;
    2521             :         }
    2522             :     }
    2523           0 :     ff_thread_report_progress2(s->avctx, ctb_row ,thread, SHIFT_CTB_WPP);
    2524             : 
    2525           0 :     return 0;
    2526           0 : error:
    2527           0 :     s->tab_slice_address[ctb_addr_rs] = -1;
    2528           0 :     atomic_store(&s1->wpp_err, 1);
    2529           0 :     ff_thread_report_progress2(s->avctx, ctb_row ,thread, SHIFT_CTB_WPP);
    2530           0 :     return ret;
    2531             : }
    2532             : 
    2533           0 : static int hls_slice_data_wpp(HEVCContext *s, const H2645NAL *nal)
    2534             : {
    2535           0 :     const uint8_t *data = nal->data;
    2536           0 :     int length          = nal->size;
    2537           0 :     HEVCLocalContext *lc = s->HEVClc;
    2538           0 :     int *ret = av_malloc_array(s->sh.num_entry_point_offsets + 1, sizeof(int));
    2539           0 :     int *arg = av_malloc_array(s->sh.num_entry_point_offsets + 1, sizeof(int));
    2540             :     int64_t offset;
    2541           0 :     int64_t startheader, cmpt = 0;
    2542           0 :     int i, j, res = 0;
    2543             : 
    2544           0 :     if (!ret || !arg) {
    2545           0 :         av_free(ret);
    2546           0 :         av_free(arg);
    2547           0 :         return AVERROR(ENOMEM);
    2548             :     }
    2549             : 
    2550           0 :     if (s->sh.slice_ctb_addr_rs + s->sh.num_entry_point_offsets * s->ps.sps->ctb_width >= s->ps.sps->ctb_width * s->ps.sps->ctb_height) {
    2551           0 :         av_log(s->avctx, AV_LOG_ERROR, "WPP ctb addresses are wrong (%d %d %d %d)\n",
    2552             :             s->sh.slice_ctb_addr_rs, s->sh.num_entry_point_offsets,
    2553           0 :             s->ps.sps->ctb_width, s->ps.sps->ctb_height
    2554             :         );
    2555           0 :         res = AVERROR_INVALIDDATA;
    2556           0 :         goto error;
    2557             :     }
    2558             : 
    2559           0 :     ff_alloc_entries(s->avctx, s->sh.num_entry_point_offsets + 1);
    2560             : 
    2561           0 :     if (!s->sList[1]) {
    2562           0 :         for (i = 1; i < s->threads_number; i++) {
    2563           0 :             s->sList[i] = av_malloc(sizeof(HEVCContext));
    2564           0 :             memcpy(s->sList[i], s, sizeof(HEVCContext));
    2565           0 :             s->HEVClcList[i] = av_mallocz(sizeof(HEVCLocalContext));
    2566           0 :             s->sList[i]->HEVClc = s->HEVClcList[i];
    2567             :         }
    2568             :     }
    2569             : 
    2570           0 :     offset = (lc->gb.index >> 3);
    2571             : 
    2572           0 :     for (j = 0, cmpt = 0, startheader = offset + s->sh.entry_point_offset[0]; j < nal->skipped_bytes; j++) {
    2573           0 :         if (nal->skipped_bytes_pos[j] >= offset && nal->skipped_bytes_pos[j] < startheader) {
    2574           0 :             startheader--;
    2575           0 :             cmpt++;
    2576             :         }
    2577             :     }
    2578             : 
    2579           0 :     for (i = 1; i < s->sh.num_entry_point_offsets; i++) {
    2580           0 :         offset += (s->sh.entry_point_offset[i - 1] - cmpt);
    2581           0 :         for (j = 0, cmpt = 0, startheader = offset
    2582           0 :              + s->sh.entry_point_offset[i]; j < nal->skipped_bytes; j++) {
    2583           0 :             if (nal->skipped_bytes_pos[j] >= offset && nal->skipped_bytes_pos[j] < startheader) {
    2584           0 :                 startheader--;
    2585           0 :                 cmpt++;
    2586             :             }
    2587             :         }
    2588           0 :         s->sh.size[i - 1] = s->sh.entry_point_offset[i] - cmpt;
    2589           0 :         s->sh.offset[i - 1] = offset;
    2590             : 
    2591             :     }
    2592           0 :     if (s->sh.num_entry_point_offsets != 0) {
    2593           0 :         offset += s->sh.entry_point_offset[s->sh.num_entry_point_offsets - 1] - cmpt;
    2594           0 :         if (length < offset) {
    2595           0 :             av_log(s->avctx, AV_LOG_ERROR, "entry_point_offset table is corrupted\n");
    2596           0 :             res = AVERROR_INVALIDDATA;
    2597           0 :             goto error;
    2598             :         }
    2599           0 :         s->sh.size[s->sh.num_entry_point_offsets - 1] = length - offset;
    2600           0 :         s->sh.offset[s->sh.num_entry_point_offsets - 1] = offset;
    2601             : 
    2602             :     }
    2603           0 :     s->data = data;
    2604             : 
    2605           0 :     for (i = 1; i < s->threads_number; i++) {
    2606           0 :         s->sList[i]->HEVClc->first_qp_group = 1;
    2607           0 :         s->sList[i]->HEVClc->qp_y = s->sList[0]->HEVClc->qp_y;
    2608           0 :         memcpy(s->sList[i], s, sizeof(HEVCContext));
    2609           0 :         s->sList[i]->HEVClc = s->HEVClcList[i];
    2610             :     }
    2611             : 
    2612           0 :     atomic_store(&s->wpp_err, 0);
    2613           0 :     ff_reset_entries(s->avctx);
    2614             : 
    2615           0 :     for (i = 0; i <= s->sh.num_entry_point_offsets; i++) {
    2616           0 :         arg[i] = i;
    2617           0 :         ret[i] = 0;
    2618             :     }
    2619             : 
    2620           0 :     if (s->ps.pps->entropy_coding_sync_enabled_flag)
    2621           0 :         s->avctx->execute2(s->avctx, hls_decode_entry_wpp, arg, ret, s->sh.num_entry_point_offsets + 1);
    2622             : 
    2623           0 :     for (i = 0; i <= s->sh.num_entry_point_offsets; i++)
    2624           0 :         res += ret[i];
    2625           0 : error:
    2626           0 :     av_free(ret);
    2627           0 :     av_free(arg);
    2628           0 :     return res;
    2629             : }
    2630             : 
    2631        9443 : static int set_side_data(HEVCContext *s)
    2632             : {
    2633        9443 :     AVFrame *out = s->ref->frame;
    2634             : 
    2635        9443 :     if (s->sei.frame_packing.present &&
    2636           0 :         s->sei.frame_packing.arrangement_type >= 3 &&
    2637           0 :         s->sei.frame_packing.arrangement_type <= 5 &&
    2638           0 :         s->sei.frame_packing.content_interpretation_type > 0 &&
    2639           0 :         s->sei.frame_packing.content_interpretation_type < 3) {
    2640           0 :         AVStereo3D *stereo = av_stereo3d_create_side_data(out);
    2641           0 :         if (!stereo)
    2642           0 :             return AVERROR(ENOMEM);
    2643             : 
    2644           0 :         switch (s->sei.frame_packing.arrangement_type) {
    2645           0 :         case 3:
    2646           0 :             if (s->sei.frame_packing.quincunx_subsampling)
    2647           0 :                 stereo->type = AV_STEREO3D_SIDEBYSIDE_QUINCUNX;
    2648             :             else
    2649           0 :                 stereo->type = AV_STEREO3D_SIDEBYSIDE;
    2650           0 :             break;
    2651           0 :         case 4:
    2652           0 :             stereo->type = AV_STEREO3D_TOPBOTTOM;
    2653           0 :             break;
    2654           0 :         case 5:
    2655           0 :             stereo->type = AV_STEREO3D_FRAMESEQUENCE;
    2656           0 :             break;
    2657             :         }
    2658             : 
    2659           0 :         if (s->sei.frame_packing.content_interpretation_type == 2)
    2660           0 :             stereo->flags = AV_STEREO3D_FLAG_INVERT;
    2661             : 
    2662           0 :         if (s->sei.frame_packing.arrangement_type == 5) {
    2663           0 :             if (s->sei.frame_packing.current_frame_is_frame0_flag)
    2664           0 :                 stereo->view = AV_STEREO3D_VIEW_LEFT;
    2665             :             else
    2666           0 :                 stereo->view = AV_STEREO3D_VIEW_RIGHT;
    2667             :         }
    2668             :     }
    2669             : 
    2670        9443 :     if (s->sei.display_orientation.present &&
    2671           0 :         (s->sei.display_orientation.anticlockwise_rotation ||
    2672           0 :          s->sei.display_orientation.hflip || s->sei.display_orientation.vflip)) {
    2673           0 :         double angle = s->sei.display_orientation.anticlockwise_rotation * 360 / (double) (1 << 16);
    2674           0 :         AVFrameSideData *rotation = av_frame_new_side_data(out,
    2675             :                                                            AV_FRAME_DATA_DISPLAYMATRIX,
    2676             :                                                            sizeof(int32_t) * 9);
    2677           0 :         if (!rotation)
    2678           0 :             return AVERROR(ENOMEM);
    2679             : 
    2680           0 :         av_display_rotation_set((int32_t *)rotation->data, angle);
    2681           0 :         av_display_matrix_flip((int32_t *)rotation->data,
    2682             :                                s->sei.display_orientation.hflip,
    2683             :                                s->sei.display_orientation.vflip);
    2684             :     }
    2685             : 
    2686             :     // Decrement the mastering display flag when IRAP frame has no_rasl_output_flag=1
    2687             :     // so the side data persists for the entire coded video sequence.
    2688        9443 :     if (s->sei.mastering_display.present > 0 &&
    2689           0 :         IS_IRAP(s) && s->no_rasl_output_flag) {
    2690           0 :         s->sei.mastering_display.present--;
    2691             :     }
    2692        9443 :     if (s->sei.mastering_display.present) {
    2693             :         // HEVC uses a g,b,r ordering, which we convert to a more natural r,g,b
    2694           0 :         const int mapping[3] = {2, 0, 1};
    2695           0 :         const int chroma_den = 50000;
    2696           0 :         const int luma_den = 10000;
    2697             :         int i;
    2698           0 :         AVMasteringDisplayMetadata *metadata =
    2699             :             av_mastering_display_metadata_create_side_data(out);
    2700           0 :         if (!metadata)
    2701           0 :             return AVERROR(ENOMEM);
    2702             : 
    2703           0 :         for (i = 0; i < 3; i++) {
    2704           0 :             const int j = mapping[i];
    2705           0 :             metadata->display_primaries[i][0].num = s->sei.mastering_display.display_primaries[j][0];
    2706           0 :             metadata->display_primaries[i][0].den = chroma_den;
    2707           0 :             metadata->display_primaries[i][1].num = s->sei.mastering_display.display_primaries[j][1];
    2708           0 :             metadata->display_primaries[i][1].den = chroma_den;
    2709             :         }
    2710           0 :         metadata->white_point[0].num = s->sei.mastering_display.white_point[0];
    2711           0 :         metadata->white_point[0].den = chroma_den;
    2712           0 :         metadata->white_point[1].num = s->sei.mastering_display.white_point[1];
    2713           0 :         metadata->white_point[1].den = chroma_den;
    2714             : 
    2715           0 :         metadata->max_luminance.num = s->sei.mastering_display.max_luminance;
    2716           0 :         metadata->max_luminance.den = luma_den;
    2717           0 :         metadata->min_luminance.num = s->sei.mastering_display.min_luminance;
    2718           0 :         metadata->min_luminance.den = luma_den;
    2719           0 :         metadata->has_luminance = 1;
    2720           0 :         metadata->has_primaries = 1;
    2721             : 
    2722           0 :         av_log(s->avctx, AV_LOG_DEBUG, "Mastering Display Metadata:\n");
    2723           0 :         av_log(s->avctx, AV_LOG_DEBUG,
    2724             :                "r(%5.4f,%5.4f) g(%5.4f,%5.4f) b(%5.4f %5.4f) wp(%5.4f, %5.4f)\n",
    2725             :                av_q2d(metadata->display_primaries[0][0]),
    2726             :                av_q2d(metadata->display_primaries[0][1]),
    2727             :                av_q2d(metadata->display_primaries[1][0]),
    2728             :                av_q2d(metadata->display_primaries[1][1]),
    2729             :                av_q2d(metadata->display_primaries[2][0]),
    2730             :                av_q2d(metadata->display_primaries[2][1]),
    2731             :                av_q2d(metadata->white_point[0]), av_q2d(metadata->white_point[1]));
    2732           0 :         av_log(s->avctx, AV_LOG_DEBUG,
    2733             :                "min_luminance=%f, max_luminance=%f\n",
    2734             :                av_q2d(metadata->min_luminance), av_q2d(metadata->max_luminance));
    2735             :     }
    2736             :     // Decrement the mastering display flag when IRAP frame has no_rasl_output_flag=1
    2737             :     // so the side data persists for the entire coded video sequence.
    2738        9443 :     if (s->sei.content_light.present > 0 &&
    2739           0 :         IS_IRAP(s) && s->no_rasl_output_flag) {
    2740           0 :         s->sei.content_light.present--;
    2741             :     }
    2742        9443 :     if (s->sei.content_light.present) {
    2743           0 :         AVContentLightMetadata *metadata =
    2744             :             av_content_light_metadata_create_side_data(out);
    2745           0 :         if (!metadata)
    2746           0 :             return AVERROR(ENOMEM);
    2747           0 :         metadata->MaxCLL  = s->sei.content_light.max_content_light_level;
    2748           0 :         metadata->MaxFALL = s->sei.content_light.max_pic_average_light_level;
    2749             : 
    2750           0 :         av_log(s->avctx, AV_LOG_DEBUG, "Content Light Level Metadata:\n");
    2751           0 :         av_log(s->avctx, AV_LOG_DEBUG, "MaxCLL=%d, MaxFALL=%d\n",
    2752             :                metadata->MaxCLL, metadata->MaxFALL);
    2753             :     }
    2754             : 
    2755        9443 :     if (s->sei.a53_caption.a53_caption) {
    2756           0 :         AVFrameSideData* sd = av_frame_new_side_data(out,
    2757             :                                                      AV_FRAME_DATA_A53_CC,
    2758             :                                                      s->sei.a53_caption.a53_caption_size);
    2759           0 :         if (sd)
    2760           0 :             memcpy(sd->data, s->sei.a53_caption.a53_caption, s->sei.a53_caption.a53_caption_size);
    2761           0 :         av_freep(&s->sei.a53_caption.a53_caption);
    2762           0 :         s->sei.a53_caption.a53_caption_size = 0;
    2763           0 :         s->avctx->properties |= FF_CODEC_PROPERTY_CLOSED_CAPTIONS;
    2764             :     }
    2765             : 
    2766        9443 :     if (s->sei.alternative_transfer.present &&
    2767           0 :         av_color_transfer_name(s->sei.alternative_transfer.preferred_transfer_characteristics) &&
    2768           0 :         s->sei.alternative_transfer.preferred_transfer_characteristics != AVCOL_TRC_UNSPECIFIED) {
    2769           0 :         s->avctx->color_trc = out->color_trc = s->sei.alternative_transfer.preferred_transfer_characteristics;
    2770             :     }
    2771             : 
    2772        9443 :     return 0;
    2773             : }
    2774             : 
    2775        9443 : static int hevc_frame_start(HEVCContext *s)
    2776             : {
    2777        9443 :     HEVCLocalContext *lc = s->HEVClc;
    2778       18886 :     int pic_size_in_ctb  = ((s->ps.sps->width  >> s->ps.sps->log2_min_cb_size) + 1) *
    2779        9443 :                            ((s->ps.sps->height >> s->ps.sps->log2_min_cb_size) + 1);
    2780             :     int ret;
    2781             : 
    2782        9443 :     memset(s->horizontal_bs, 0, s->bs_width * s->bs_height);
    2783        9443 :     memset(s->vertical_bs,   0, s->bs_width * s->bs_height);
    2784        9443 :     memset(s->cbf_luma,      0, s->ps.sps->min_tb_width * s->ps.sps->min_tb_height);
    2785        9443 :     memset(s->is_pcm,        0, (s->ps.sps->min_pu_width + 1) * (s->ps.sps->min_pu_height + 1));
    2786        9443 :     memset(s->tab_slice_address, -1, pic_size_in_ctb * sizeof(*s->tab_slice_address));
    2787             : 
    2788        9443 :     s->is_decoded        = 0;
    2789        9443 :     s->first_nal_type    = s->nal_unit_type;
    2790             : 
    2791        9443 :     s->no_rasl_output_flag = IS_IDR(s) || IS_BLA(s) || (s->nal_unit_type == HEVC_NAL_CRA_NUT && s->last_eos);
    2792             : 
    2793        9443 :     if (s->ps.pps->tiles_enabled_flag)
    2794         620 :         lc->end_of_tiles_x = s->ps.pps->column_width[0] << s->ps.sps->log2_ctb_size;
    2795             : 
    2796        9443 :     ret = ff_hevc_set_new_ref(s, &s->frame, s->poc);
    2797        9443 :     if (ret < 0)
    2798           0 :         goto fail;
    2799             : 
    2800        9443 :     ret = ff_hevc_frame_rps(s);
    2801        9443 :     if (ret < 0) {
    2802           0 :         av_log(s->avctx, AV_LOG_ERROR, "Error constructing the frame RPS.\n");
    2803           0 :         goto fail;
    2804             :     }
    2805             : 
    2806        9443 :     s->ref->frame->key_frame = IS_IRAP(s);
    2807             : 
    2808        9443 :     ret = set_side_data(s);
    2809        9443 :     if (ret < 0)
    2810           0 :         goto fail;
    2811             : 
    2812        9443 :     s->frame->pict_type = 3 - s->sh.slice_type;
    2813             : 
    2814        9443 :     if (!IS_IRAP(s))
    2815        8920 :         ff_hevc_bump_frame(s);
    2816             : 
    2817        9443 :     av_frame_unref(s->output_frame);
    2818        9443 :     ret = ff_hevc_output_frame(s, s->output_frame, 0);
    2819        9443 :     if (ret < 0)
    2820           0 :         goto fail;
    2821             : 
    2822        9443 :     if (!s->avctx->hwaccel)
    2823        9443 :         ff_thread_finish_setup(s->avctx);
    2824             : 
    2825        9443 :     return 0;
    2826             : 
    2827           0 : fail:
    2828           0 :     if (s->ref)
    2829           0 :         ff_hevc_unref_frame(s, s->ref, ~0);
    2830           0 :     s->ref = NULL;
    2831           0 :     return ret;
    2832             : }
    2833             : 
    2834       37678 : static int decode_nal_unit(HEVCContext *s, const H2645NAL *nal)
    2835             : {
    2836       37678 :     HEVCLocalContext *lc = s->HEVClc;
    2837       37678 :     GetBitContext *gb    = &lc->gb;
    2838             :     int ctb_addr_ts, ret;
    2839             : 
    2840       37678 :     *gb              = nal->gb;
    2841       37678 :     s->nal_unit_type = nal->type;
    2842       37678 :     s->temporal_id   = nal->temporal_id;
    2843             : 
    2844       37678 :     switch (s->nal_unit_type) {
    2845         388 :     case HEVC_NAL_VPS:
    2846         388 :         if (s->avctx->hwaccel && s->avctx->hwaccel->decode_params) {
    2847           0 :             ret = s->avctx->hwaccel->decode_params(s->avctx,
    2848             :                                                    nal->type,
    2849             :                                                    nal->raw_data,
    2850           0 :                                                    nal->raw_size);
    2851           0 :             if (ret < 0)
    2852           0 :                 goto fail;
    2853             :         }
    2854         388 :         ret = ff_hevc_decode_nal_vps(gb, s->avctx, &s->ps);
    2855         388 :         if (ret < 0)
    2856           0 :             goto fail;
    2857         388 :         break;
    2858         385 :     case HEVC_NAL_SPS:
    2859         385 :         if (s->avctx->hwaccel && s->avctx->hwaccel->decode_params) {
    2860           0 :             ret = s->avctx->hwaccel->decode_params(s->avctx,
    2861             :                                                    nal->type,
    2862             :                                                    nal->raw_data,
    2863           0 :                                                    nal->raw_size);
    2864           0 :             if (ret < 0)
    2865           0 :                 goto fail;
    2866             :         }
    2867         385 :         ret = ff_hevc_decode_nal_sps(gb, s->avctx, &s->ps,
    2868             :                                      s->apply_defdispwin);
    2869         385 :         if (ret < 0)
    2870           0 :             goto fail;
    2871         385 :         break;
    2872        1343 :     case HEVC_NAL_PPS:
    2873        1343 :         if (s->avctx->hwaccel && s->avctx->hwaccel->decode_params) {
    2874           0 :             ret = s->avctx->hwaccel->decode_params(s->avctx,
    2875             :                                                    nal->type,
    2876             :                                                    nal->raw_data,
    2877           0 :                                                    nal->raw_size);
    2878           0 :             if (ret < 0)
    2879           0 :                 goto fail;
    2880             :         }
    2881        1343 :         ret = ff_hevc_decode_nal_pps(gb, s->avctx, &s->ps);
    2882        1343 :         if (ret < 0)
    2883           2 :             goto fail;
    2884        1341 :         break;
    2885        7460 :     case HEVC_NAL_SEI_PREFIX:
    2886             :     case HEVC_NAL_SEI_SUFFIX:
    2887        7460 :         if (s->avctx->hwaccel && s->avctx->hwaccel->decode_params) {
    2888           0 :             ret = s->avctx->hwaccel->decode_params(s->avctx,
    2889             :                                                    nal->type,
    2890             :                                                    nal->raw_data,
    2891           0 :                                                    nal->raw_size);
    2892           0 :             if (ret < 0)
    2893           0 :                 goto fail;
    2894             :         }
    2895        7460 :         ret = ff_hevc_decode_nal_sei(gb, s->avctx, &s->sei, &s->ps, s->nal_unit_type);
    2896        7460 :         if (ret < 0)
    2897          22 :             goto fail;
    2898        7438 :         break;
    2899       27475 :     case HEVC_NAL_TRAIL_R:
    2900             :     case HEVC_NAL_TRAIL_N:
    2901             :     case HEVC_NAL_TSA_N:
    2902             :     case HEVC_NAL_TSA_R:
    2903             :     case HEVC_NAL_STSA_N:
    2904             :     case HEVC_NAL_STSA_R:
    2905             :     case HEVC_NAL_BLA_W_LP:
    2906             :     case HEVC_NAL_BLA_W_RADL:
    2907             :     case HEVC_NAL_BLA_N_LP:
    2908             :     case HEVC_NAL_IDR_W_RADL:
    2909             :     case HEVC_NAL_IDR_N_LP:
    2910             :     case HEVC_NAL_CRA_NUT:
    2911             :     case HEVC_NAL_RADL_N:
    2912             :     case HEVC_NAL_RADL_R:
    2913             :     case HEVC_NAL_RASL_N:
    2914             :     case HEVC_NAL_RASL_R:
    2915       27475 :         ret = hls_slice_header(s);
    2916       27475 :         if (ret < 0)
    2917          22 :             return ret;
    2918             : 
    2919       27453 :         if (
    2920       54906 :             (s->avctx->skip_frame >= AVDISCARD_BIDIR && s->sh.slice_type == HEVC_SLICE_B) ||
    2921       54906 :             (s->avctx->skip_frame >= AVDISCARD_NONINTRA && s->sh.slice_type != HEVC_SLICE_I) ||
    2922       27453 :             (s->avctx->skip_frame >= AVDISCARD_NONKEY && !IS_IDR(s))) {
    2923             :             break;
    2924             :         }
    2925             : 
    2926       27453 :         if (s->sh.first_slice_in_pic_flag) {
    2927        9472 :             if (s->max_ra == INT_MAX) {
    2928         378 :                 if (s->nal_unit_type == HEVC_NAL_CRA_NUT || IS_BLA(s)) {
    2929          18 :                     s->max_ra = s->poc;
    2930             :                 } else {
    2931         360 :                     if (IS_IDR(s))
    2932         360 :                         s->max_ra = INT_MIN;
    2933             :                 }
    2934             :             }
    2935             : 
    2936       10301 :             if ((s->nal_unit_type == HEVC_NAL_RASL_R || s->nal_unit_type == HEVC_NAL_RASL_N) &&
    2937         829 :                 s->poc <= s->max_ra) {
    2938          29 :                 s->is_decoded = 0;
    2939          29 :                 break;
    2940             :             } else {
    2941        9443 :                 if (s->nal_unit_type == HEVC_NAL_RASL_R && s->poc > s->max_ra)
    2942         463 :                     s->max_ra = INT_MIN;
    2943             :             }
    2944             : 
    2945        9443 :             ret = hevc_frame_start(s);
    2946        9443 :             if (ret < 0)
    2947           0 :                 return ret;
    2948       17981 :         } else if (!s->ref) {
    2949           0 :             av_log(s->avctx, AV_LOG_ERROR, "First slice in a frame missing.\n");
    2950           0 :             goto fail;
    2951             :         }
    2952             : 
    2953       27424 :         if (s->nal_unit_type != s->first_nal_type) {
    2954           0 :             av_log(s->avctx, AV_LOG_ERROR,
    2955             :                    "Non-matching NAL types of the VCL NALUs: %d %d\n",
    2956           0 :                    s->first_nal_type, s->nal_unit_type);
    2957           0 :             return AVERROR_INVALIDDATA;
    2958             :         }
    2959             : 
    2960       46901 :         if (!s->sh.dependent_slice_segment_flag &&
    2961       19477 :             s->sh.slice_type != HEVC_SLICE_I) {
    2962       18369 :             ret = ff_hevc_slice_rpl(s);
    2963       18369 :             if (ret < 0) {
    2964           0 :                 av_log(s->avctx, AV_LOG_WARNING,
    2965             :                        "Error constructing the reference lists for the current slice.\n");
    2966           0 :                 goto fail;
    2967             :             }
    2968             :         }
    2969             : 
    2970       27424 :         if (s->sh.first_slice_in_pic_flag && s->avctx->hwaccel) {
    2971           0 :             ret = s->avctx->hwaccel->start_frame(s->avctx, NULL, 0);
    2972           0 :             if (ret < 0)
    2973           0 :                 goto fail;
    2974             :         }
    2975             : 
    2976       27424 :         if (s->avctx->hwaccel) {
    2977           0 :             ret = s->avctx->hwaccel->decode_slice(s->avctx, nal->raw_data, nal->raw_size);
    2978           0 :             if (ret < 0)
    2979           0 :                 goto fail;
    2980             :         } else {
    2981       27424 :             if (s->threads_number > 1 && s->sh.num_entry_point_offsets > 0)
    2982           0 :                 ctb_addr_ts = hls_slice_data_wpp(s, nal);
    2983             :             else
    2984       27424 :                 ctb_addr_ts = hls_slice_data(s);
    2985       27424 :             if (ctb_addr_ts >= (s->ps.sps->ctb_width * s->ps.sps->ctb_height)) {
    2986        9443 :                 s->is_decoded = 1;
    2987             :             }
    2988             : 
    2989       27424 :             if (ctb_addr_ts < 0) {
    2990           0 :                 ret = ctb_addr_ts;
    2991           0 :                 goto fail;
    2992             :             }
    2993             :         }
    2994       27424 :         break;
    2995           4 :     case HEVC_NAL_EOS_NUT:
    2996             :     case HEVC_NAL_EOB_NUT:
    2997           4 :         s->seq_decode = (s->seq_decode + 1) & 0xff;
    2998           4 :         s->max_ra     = INT_MAX;
    2999           4 :         break;
    3000         623 :     case HEVC_NAL_AUD:
    3001             :     case HEVC_NAL_FD_NUT:
    3002         623 :         break;
    3003           0 :     default:
    3004           0 :         av_log(s->avctx, AV_LOG_INFO,
    3005           0 :                "Skipping NAL unit %d\n", s->nal_unit_type);
    3006             :     }
    3007             : 
    3008       37632 :     return 0;
    3009          24 : fail:
    3010          24 :     if (s->avctx->err_recognition & AV_EF_EXPLODE)
    3011           0 :         return ret;
    3012          24 :     return 0;
    3013             : }
    3014             : 
    3015        9495 : static int decode_nal_units(HEVCContext *s, const uint8_t *buf, int length)
    3016             : {
    3017        9495 :     int i, ret = 0;
    3018        9495 :     int eos_at_start = 1;
    3019             : 
    3020        9495 :     s->ref = NULL;
    3021        9495 :     s->last_eos = s->eos;
    3022        9495 :     s->eos = 0;
    3023             : 
    3024             :     /* split the input packet into NAL units, so we know the upper bound on the
    3025             :      * number of slices in the frame */
    3026        9495 :     ret = ff_h2645_packet_split(&s->pkt, buf, length, s->avctx, s->is_nalff,
    3027        9495 :                                 s->nal_length_size, s->avctx->codec_id, 1);
    3028        9495 :     if (ret < 0) {
    3029           0 :         av_log(s->avctx, AV_LOG_ERROR,
    3030             :                "Error splitting the input into NAL units.\n");
    3031           0 :         return ret;
    3032             :     }
    3033             : 
    3034       47173 :     for (i = 0; i < s->pkt.nb_nals; i++) {
    3035       75355 :         if (s->pkt.nals[i].type == HEVC_NAL_EOB_NUT ||
    3036       37677 :             s->pkt.nals[i].type == HEVC_NAL_EOS_NUT) {
    3037           8 :             if (eos_at_start) {
    3038           4 :                 s->last_eos = 1;
    3039             :             } else {
    3040           0 :                 s->eos = 1;
    3041             :             }
    3042             :         } else {
    3043       37674 :             eos_at_start = 0;
    3044             :         }
    3045             :     }
    3046             : 
    3047             :     /* decode the NAL units */
    3048       47151 :     for (i = 0; i < s->pkt.nb_nals; i++) {
    3049       37678 :         H2645NAL *nal = &s->pkt.nals[i];
    3050             : 
    3051       75356 :         if (s->avctx->skip_frame >= AVDISCARD_ALL ||
    3052       37678 :             (s->avctx->skip_frame >= AVDISCARD_NONREF
    3053           0 :             && ff_hevc_nal_is_nonref(nal->type)))
    3054           0 :             continue;
    3055             : 
    3056       37678 :         ret = decode_nal_unit(s, nal);
    3057       37678 :         if (ret < 0) {
    3058          22 :             av_log(s->avctx, AV_LOG_WARNING,
    3059             :                    "Error parsing NAL unit #%d.\n", i);
    3060          22 :             goto fail;
    3061             :         }
    3062             :     }
    3063             : 
    3064        9473 : fail:
    3065        9495 :     if (s->ref && s->threads_type == FF_THREAD_FRAME)
    3066           0 :         ff_thread_report_progress(&s->ref->tf, INT_MAX, 0);
    3067             : 
    3068        9495 :     return ret;
    3069             : }
    3070             : 
    3071           0 : static void print_md5(void *log_ctx, int level, uint8_t md5[16])
    3072             : {
    3073             :     int i;
    3074           0 :     for (i = 0; i < 16; i++)
    3075           0 :         av_log(log_ctx, level, "%02"PRIx8, md5[i]);
    3076           0 : }
    3077             : 
    3078           0 : static int verify_md5(HEVCContext *s, AVFrame *frame)
    3079             : {
    3080           0 :     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(frame->format);
    3081             :     int pixel_shift;
    3082             :     int i, j;
    3083             : 
    3084           0 :     if (!desc)
    3085           0 :         return AVERROR(EINVAL);
    3086             : 
    3087           0 :     pixel_shift = desc->comp[0].depth > 8;
    3088             : 
    3089           0 :     av_log(s->avctx, AV_LOG_DEBUG, "Verifying checksum for frame with POC %d: ",
    3090             :            s->poc);
    3091             : 
    3092             :     /* the checksums are LE, so we have to byteswap for >8bpp formats
    3093             :      * on BE arches */
    3094             : #if HAVE_BIGENDIAN
    3095             :     if (pixel_shift && !s->checksum_buf) {
    3096             :         av_fast_malloc(&s->checksum_buf, &s->checksum_buf_size,
    3097             :                        FFMAX3(frame->linesize[0], frame->linesize[1],
    3098             :                               frame->linesize[2]));
    3099             :         if (!s->checksum_buf)
    3100             :             return AVERROR(ENOMEM);
    3101             :     }
    3102             : #endif
    3103             : 
    3104           0 :     for (i = 0; frame->data[i]; i++) {
    3105           0 :         int width  = s->avctx->coded_width;
    3106           0 :         int height = s->avctx->coded_height;
    3107           0 :         int w = (i == 1 || i == 2) ? (width  >> desc->log2_chroma_w) : width;
    3108           0 :         int h = (i == 1 || i == 2) ? (height >> desc->log2_chroma_h) : height;
    3109             :         uint8_t md5[16];
    3110             : 
    3111           0 :         av_md5_init(s->md5_ctx);
    3112           0 :         for (j = 0; j < h; j++) {
    3113           0 :             const uint8_t *src = frame->data[i] + j * frame->linesize[i];
    3114             : #if HAVE_BIGENDIAN
    3115             :             if (pixel_shift) {
    3116             :                 s->bdsp.bswap16_buf((uint16_t *) s->checksum_buf,
    3117             :                                     (const uint16_t *) src, w);
    3118             :                 src = s->checksum_buf;
    3119             :             }
    3120             : #endif
    3121           0 :             av_md5_update(s->md5_ctx, src, w << pixel_shift);
    3122             :         }
    3123           0 :         av_md5_final(s->md5_ctx, md5);
    3124             : 
    3125           0 :         if (!memcmp(md5, s->sei.picture_hash.md5[i], 16)) {
    3126           0 :             av_log   (s->avctx, AV_LOG_DEBUG, "plane %d - correct ", i);
    3127           0 :             print_md5(s->avctx, AV_LOG_DEBUG, md5);
    3128           0 :             av_log   (s->avctx, AV_LOG_DEBUG, "; ");
    3129             :         } else {
    3130           0 :             av_log   (s->avctx, AV_LOG_ERROR, "mismatching checksum of plane %d - ", i);
    3131           0 :             print_md5(s->avctx, AV_LOG_ERROR, md5);
    3132           0 :             av_log   (s->avctx, AV_LOG_ERROR, " != ");
    3133           0 :             print_md5(s->avctx, AV_LOG_ERROR, s->sei.picture_hash.md5[i]);
    3134           0 :             av_log   (s->avctx, AV_LOG_ERROR, "\n");
    3135           0 :             return AVERROR_INVALIDDATA;
    3136             :         }
    3137             :     }
    3138             : 
    3139           0 :     av_log(s->avctx, AV_LOG_DEBUG, "\n");
    3140             : 
    3141           0 :     return 0;
    3142             : }
    3143             : 
    3144         174 : static int hevc_decode_extradata(HEVCContext *s, uint8_t *buf, int length, int first)
    3145             : {
    3146             :     int ret, i;
    3147             : 
    3148         348 :     ret = ff_hevc_decode_extradata(buf, length, &s->ps, &s->sei, &s->is_nalff,
    3149         174 :                                    &s->nal_length_size, s->avctx->err_recognition,
    3150         174 :                                    s->apply_defdispwin, s->avctx);
    3151         174 :     if (ret < 0)
    3152           0 :         return ret;
    3153             : 
    3154             :     /* export stream parameters from the first SPS */
    3155         206 :     for (i = 0; i < FF_ARRAY_ELEMS(s->ps.sps_list); i++) {
    3156         204 :         if (first && s->ps.sps_list[i]) {
    3157         172 :             const HEVCSPS *sps = (const HEVCSPS*)s->ps.sps_list[i]->data;
    3158         172 :             export_stream_params(s->avctx, &s->ps, sps);
    3159         172 :             break;
    3160             :         }
    3161             :     }
    3162             : 
    3163         174 :     return 0;
    3164             : }
    3165             : 
    3166        9959 : static int hevc_decode_frame(AVCodecContext *avctx, void *data, int *got_output,
    3167             :                              AVPacket *avpkt)
    3168             : {
    3169             :     int ret;
    3170             :     int new_extradata_size;
    3171             :     uint8_t *new_extradata;
    3172        9959 :     HEVCContext *s = avctx->priv_data;
    3173             : 
    3174        9959 :     if (!avpkt->size) {
    3175         464 :         ret = ff_hevc_output_frame(s, data, 1);
    3176         464 :         if (ret < 0)
    3177           0 :             return ret;
    3178             : 
    3179         464 :         *got_output = ret;
    3180         464 :         return 0;
    3181             :     }
    3182             : 
    3183        9495 :     new_extradata = av_packet_get_side_data(avpkt, AV_PKT_DATA_NEW_EXTRADATA,
    3184             :                                             &new_extradata_size);
    3185        9495 :     if (new_extradata && new_extradata_size > 0) {
    3186           1 :         ret = hevc_decode_extradata(s, new_extradata, new_extradata_size, 0);
    3187           1 :         if (ret < 0)
    3188           0 :             return ret;
    3189             :     }
    3190             : 
    3191        9495 :     s->ref = NULL;
    3192        9495 :     ret    = decode_nal_units(s, avpkt->data, avpkt->size);
    3193        9495 :     if (ret < 0)
    3194          22 :         return ret;
    3195             : 
    3196        9473 :     if (avctx->hwaccel) {
    3197           0 :         if (s->ref && (ret = avctx->hwaccel->end_frame(avctx)) < 0) {
    3198           0 :             av_log(avctx, AV_LOG_ERROR,
    3199             :                    "hardware accelerator failed to decode picture\n");
    3200           0 :             ff_hevc_unref_frame(s, s->ref, ~0);
    3201           0 :             return ret;
    3202             :         }
    3203             :     } else {
    3204             :         /* verify the SEI checksum */
    3205        9473 :         if (avctx->err_recognition & AV_EF_CRCCHECK && s->is_decoded &&
    3206           0 :             s->sei.picture_hash.is_md5) {
    3207           0 :             ret = verify_md5(s, s->ref->frame);
    3208           0 :             if (ret < 0 && avctx->err_recognition & AV_EF_EXPLODE) {
    3209           0 :                 ff_hevc_unref_frame(s, s->ref, ~0);
    3210           0 :                 return ret;
    3211             :             }
    3212             :         }
    3213             :     }
    3214        9473 :     s->sei.picture_hash.is_md5 = 0;
    3215             : 
    3216        9473 :     if (s->is_decoded) {
    3217        9443 :         av_log(avctx, AV_LOG_DEBUG, "Decoded frame with POC %d.\n", s->poc);
    3218        9443 :         s->is_decoded = 0;
    3219             :     }
    3220             : 
    3221        9473 :     if (s->output_frame->buf[0]) {
    3222        8997 :         av_frame_move_ref(data, s->output_frame);
    3223        8997 :         *got_output = 1;
    3224             :     }
    3225             : 
    3226        9473 :     return avpkt->size;
    3227             : }
    3228             : 
    3229           0 : static int hevc_ref_frame(HEVCContext *s, HEVCFrame *dst, HEVCFrame *src)
    3230             : {
    3231             :     int ret;
    3232             : 
    3233           0 :     ret = ff_thread_ref_frame(&dst->tf, &src->tf);
    3234           0 :     if (ret < 0)
    3235           0 :         return ret;
    3236             : 
    3237           0 :     dst->tab_mvf_buf = av_buffer_ref(src->tab_mvf_buf);
    3238           0 :     if (!dst->tab_mvf_buf)
    3239           0 :         goto fail;
    3240           0 :     dst->tab_mvf = src->tab_mvf;
    3241             : 
    3242           0 :     dst->rpl_tab_buf = av_buffer_ref(src->rpl_tab_buf);
    3243           0 :     if (!dst->rpl_tab_buf)
    3244           0 :         goto fail;
    3245           0 :     dst->rpl_tab = src->rpl_tab;
    3246             : 
    3247           0 :     dst->rpl_buf = av_buffer_ref(src->rpl_buf);
    3248           0 :     if (!dst->rpl_buf)
    3249           0 :         goto fail;
    3250             : 
    3251           0 :     dst->poc        = src->poc;
    3252           0 :     dst->ctb_count  = src->ctb_count;
    3253           0 :     dst->flags      = src->flags;
    3254           0 :     dst->sequence   = src->sequence;
    3255             : 
    3256           0 :     if (src->hwaccel_picture_private) {
    3257           0 :         dst->hwaccel_priv_buf = av_buffer_ref(src->hwaccel_priv_buf);
    3258           0 :         if (!dst->hwaccel_priv_buf)
    3259           0 :             goto fail;
    3260           0 :         dst->hwaccel_picture_private = dst->hwaccel_priv_buf->data;
    3261             :     }
    3262             : 
    3263           0 :     return 0;
    3264           0 : fail:
    3265           0 :     ff_hevc_unref_frame(s, dst, ~0);
    3266           0 :     return AVERROR(ENOMEM);
    3267             : }
    3268             : 
    3269         359 : static av_cold int hevc_decode_free(AVCodecContext *avctx)
    3270             : {
    3271         359 :     HEVCContext       *s = avctx->priv_data;
    3272             :     int i;
    3273             : 
    3274         359 :     pic_arrays_free(s);
    3275             : 
    3276         359 :     av_freep(&s->md5_ctx);
    3277             : 
    3278         359 :     av_freep(&s->cabac_state);
    3279             : 
    3280        1436 :     for (i = 0; i < 3; i++) {
    3281        1077 :         av_freep(&s->sao_pixel_buffer_h[i]);
    3282        1077 :         av_freep(&s->sao_pixel_buffer_v[i]);
    3283             :     }
    3284         359 :     av_frame_free(&s->output_frame);
    3285             : 
    3286       11847 :     for (i = 0; i < FF_ARRAY_ELEMS(s->DPB); i++) {
    3287       11488 :         ff_hevc_unref_frame(s, &s->DPB[i], ~0);
    3288       11488 :         av_frame_free(&s->DPB[i].frame);
    3289             :     }
    3290             : 
    3291         359 :     ff_hevc_ps_uninit(&s->ps);
    3292             : 
    3293         359 :     av_freep(&s->sh.entry_point_offset);
    3294         359 :     av_freep(&s->sh.offset);
    3295         359 :     av_freep(&s->sh.size);
    3296             : 
    3297         359 :     for (i = 1; i < s->threads_number; i++) {
    3298           0 :         HEVCLocalContext *lc = s->HEVClcList[i];
    3299           0 :         if (lc) {
    3300           0 :             av_freep(&s->HEVClcList[i]);
    3301           0 :             av_freep(&s->sList[i]);
    3302             :         }
    3303             :     }
    3304         359 :     if (s->HEVClc == s->HEVClcList[0])
    3305         359 :         s->HEVClc = NULL;
    3306         359 :     av_freep(&s->HEVClcList[0]);
    3307             : 
    3308         359 :     ff_h2645_packet_uninit(&s->pkt);
    3309             : 
    3310         359 :     return 0;
    3311             : }
    3312             : 
    3313         359 : static av_cold int hevc_init_context(AVCodecContext *avctx)
    3314             : {
    3315         359 :     HEVCContext *s = avctx->priv_data;
    3316             :     int i;
    3317             : 
    3318         359 :     s->avctx = avctx;
    3319             : 
    3320         359 :     s->HEVClc = av_mallocz(sizeof(HEVCLocalContext));
    3321         359 :     if (!s->HEVClc)
    3322           0 :         goto fail;
    3323         359 :     s->HEVClcList[0] = s->HEVClc;
    3324         359 :     s->sList[0] = s;
    3325             : 
    3326         359 :     s->cabac_state = av_malloc(HEVC_CONTEXTS);
    3327         359 :     if (!s->cabac_state)
    3328           0 :         goto fail;
    3329             : 
    3330         359 :     s->output_frame = av_frame_alloc();
    3331         359 :     if (!s->output_frame)
    3332           0 :         goto fail;
    3333             : 
    3334       11847 :     for (i = 0; i < FF_ARRAY_ELEMS(s->DPB); i++) {
    3335       11488 :         s->DPB[i].frame = av_frame_alloc();
    3336       11488 :         if (!s->DPB[i].frame)
    3337           0 :             goto fail;
    3338       11488 :         s->DPB[i].tf.f = s->DPB[i].frame;
    3339             :     }
    3340             : 
    3341         359 :     s->max_ra = INT_MAX;
    3342             : 
    3343         359 :     s->md5_ctx = av_md5_alloc();
    3344         359 :     if (!s->md5_ctx)
    3345           0 :         goto fail;
    3346             : 
    3347         359 :     ff_bswapdsp_init(&s->bdsp);
    3348             : 
    3349         359 :     s->context_initialized = 1;
    3350         359 :     s->eos = 0;
    3351             : 
    3352         359 :     ff_hevc_reset_sei(&s->sei);
    3353             : 
    3354         359 :     return 0;
    3355             : 
    3356           0 : fail:
    3357           0 :     hevc_decode_free(avctx);
    3358           0 :     return AVERROR(ENOMEM);
    3359             : }
    3360             : 
    3361           0 : static int hevc_update_thread_context(AVCodecContext *dst,
    3362             :                                       const AVCodecContext *src)
    3363             : {
    3364           0 :     HEVCContext *s  = dst->priv_data;
    3365           0 :     HEVCContext *s0 = src->priv_data;
    3366             :     int i, ret;
    3367             : 
    3368           0 :     if (!s->context_initialized) {
    3369           0 :         ret = hevc_init_context(dst);
    3370           0 :         if (ret < 0)
    3371           0 :             return ret;
    3372             :     }
    3373             : 
    3374           0 :     for (i = 0; i < FF_ARRAY_ELEMS(s->DPB); i++) {
    3375           0 :         ff_hevc_unref_frame(s, &s->DPB[i], ~0);
    3376           0 :         if (s0->DPB[i].frame->buf[0]) {
    3377           0 :             ret = hevc_ref_frame(s, &s->DPB[i], &s0->DPB[i]);
    3378           0 :             if (ret < 0)
    3379           0 :                 return ret;
    3380             :         }
    3381             :     }
    3382             : 
    3383           0 :     if (s->ps.sps != s0->ps.sps)
    3384           0 :         s->ps.sps = NULL;
    3385           0 :     for (i = 0; i < FF_ARRAY_ELEMS(s->ps.vps_list); i++) {
    3386           0 :         av_buffer_unref(&s->ps.vps_list[i]);
    3387           0 :         if (s0->ps.vps_list[i]) {
    3388           0 :             s->ps.vps_list[i] = av_buffer_ref(s0->ps.vps_list[i]);
    3389           0 :             if (!s->ps.vps_list[i])
    3390           0 :                 return AVERROR(ENOMEM);
    3391             :         }
    3392             :     }
    3393             : 
    3394           0 :     for (i = 0; i < FF_ARRAY_ELEMS(s->ps.sps_list); i++) {
    3395           0 :         av_buffer_unref(&s->ps.sps_list[i]);
    3396           0 :         if (s0->ps.sps_list[i]) {
    3397           0 :             s->ps.sps_list[i] = av_buffer_ref(s0->ps.sps_list[i]);
    3398           0 :             if (!s->ps.sps_list[i])
    3399           0 :                 return AVERROR(ENOMEM);
    3400             :         }
    3401             :     }
    3402             : 
    3403           0 :     for (i = 0; i < FF_ARRAY_ELEMS(s->ps.pps_list); i++) {
    3404           0 :         av_buffer_unref(&s->ps.pps_list[i]);
    3405           0 :         if (s0->ps.pps_list[i]) {
    3406           0 :             s->ps.pps_list[i] = av_buffer_ref(s0->ps.pps_list[i]);
    3407           0 :             if (!s->ps.pps_list[i])
    3408           0 :                 return AVERROR(ENOMEM);
    3409             :         }
    3410             :     }
    3411             : 
    3412           0 :     if (s->ps.sps != s0->ps.sps)
    3413           0 :         if ((ret = set_sps(s, s0->ps.sps, src->pix_fmt)) < 0)
    3414           0 :             return ret;
    3415             : 
    3416           0 :     s->seq_decode = s0->seq_decode;
    3417           0 :     s->seq_output = s0->seq_output;
    3418           0 :     s->pocTid0    = s0->pocTid0;
    3419           0 :     s->max_ra     = s0->max_ra;
    3420           0 :     s->eos        = s0->eos;
    3421           0 :     s->no_rasl_output_flag = s0->no_rasl_output_flag;
    3422             : 
    3423           0 :     s->is_nalff        = s0->is_nalff;
    3424           0 :     s->nal_length_size = s0->nal_length_size;
    3425             : 
    3426           0 :     s->threads_number      = s0->threads_number;
    3427           0 :     s->threads_type        = s0->threads_type;
    3428             : 
    3429           0 :     if (s0->eos) {
    3430           0 :         s->seq_decode = (s->seq_decode + 1) & 0xff;
    3431           0 :         s->max_ra = INT_MAX;
    3432             :     }
    3433             : 
    3434           0 :     s->sei.frame_packing        = s0->sei.frame_packing;
    3435           0 :     s->sei.display_orientation  = s0->sei.display_orientation;
    3436           0 :     s->sei.mastering_display    = s0->sei.mastering_display;
    3437           0 :     s->sei.content_light        = s0->sei.content_light;
    3438           0 :     s->sei.alternative_transfer = s0->sei.alternative_transfer;
    3439             : 
    3440           0 :     return 0;
    3441             : }
    3442             : 
    3443         359 : static av_cold int hevc_decode_init(AVCodecContext *avctx)
    3444             : {
    3445         359 :     HEVCContext *s = avctx->priv_data;
    3446             :     int ret;
    3447             : 
    3448         359 :     avctx->internal->allocate_progress = 1;
    3449             : 
    3450         359 :     ret = hevc_init_context(avctx);
    3451         359 :     if (ret < 0)
    3452           0 :         return ret;
    3453             : 
    3454         359 :     s->enable_parallel_tiles = 0;
    3455         359 :     s->sei.picture_timing.picture_struct = 0;
    3456         359 :     s->eos = 1;
    3457             : 
    3458         359 :     atomic_init(&s->wpp_err, 0);
    3459             : 
    3460         359 :     if(avctx->active_thread_type & FF_THREAD_SLICE)
    3461           0 :         s->threads_number = avctx->thread_count;
    3462             :     else
    3463         359 :         s->threads_number = 1;
    3464             : 
    3465         359 :     if (avctx->extradata_size > 0 && avctx->extradata) {
    3466         173 :         ret = hevc_decode_extradata(s, avctx->extradata, avctx->extradata_size, 1);
    3467         173 :         if (ret < 0) {
    3468           0 :             hevc_decode_free(avctx);
    3469           0 :             return ret;
    3470             :         }
    3471             :     }
    3472             : 
    3473         359 :     if((avctx->active_thread_type & FF_THREAD_FRAME) && avctx->thread_count > 1)
    3474           0 :             s->threads_type = FF_THREAD_FRAME;
    3475             :         else
    3476         359 :             s->threads_type = FF_THREAD_SLICE;
    3477             : 
    3478         359 :     return 0;
    3479             : }
    3480             : 
    3481           0 : static av_cold int hevc_init_thread_copy(AVCodecContext *avctx)
    3482             : {
    3483           0 :     HEVCContext *s = avctx->priv_data;
    3484             :     int ret;
    3485             : 
    3486           0 :     memset(s, 0, sizeof(*s));
    3487             : 
    3488           0 :     ret = hevc_init_context(avctx);
    3489           0 :     if (ret < 0)
    3490           0 :         return ret;
    3491             : 
    3492           0 :     return 0;
    3493             : }
    3494             : 
    3495           0 : static void hevc_decode_flush(AVCodecContext *avctx)
    3496             : {
    3497           0 :     HEVCContext *s = avctx->priv_data;
    3498           0 :     ff_hevc_flush_dpb(s);
    3499           0 :     s->max_ra = INT_MAX;
    3500           0 :     s->eos = 1;
    3501           0 : }
    3502             : 
    3503             : #define OFFSET(x) offsetof(HEVCContext, x)
    3504             : #define PAR (AV_OPT_FLAG_DECODING_PARAM | AV_OPT_FLAG_VIDEO_PARAM)
    3505             : 
    3506             : static const AVOption options[] = {
    3507             :     { "apply_defdispwin", "Apply default display window from VUI", OFFSET(apply_defdispwin),
    3508             :         AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, PAR },
    3509             :     { "strict-displaywin", "stricly apply default display window size", OFFSET(apply_defdispwin),
    3510             :         AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, PAR },
    3511             :     { NULL },
    3512             : };
    3513             : 
    3514             : static const AVClass hevc_decoder_class = {
    3515             :     .class_name = "HEVC decoder",
    3516             :     .item_name  = av_default_item_name,
    3517             :     .option     = options,
    3518             :     .version    = LIBAVUTIL_VERSION_INT,
    3519             : };
    3520             : 
    3521             : AVCodec ff_hevc_decoder = {
    3522             :     .name                  = "hevc",
    3523             :     .long_name             = NULL_IF_CONFIG_SMALL("HEVC (High Efficiency Video Coding)"),
    3524             :     .type                  = AVMEDIA_TYPE_VIDEO,
    3525             :     .id                    = AV_CODEC_ID_HEVC,
    3526             :     .priv_data_size        = sizeof(HEVCContext),
    3527             :     .priv_class            = &hevc_decoder_class,
    3528             :     .init                  = hevc_decode_init,
    3529             :     .close                 = hevc_decode_free,
    3530             :     .decode                = hevc_decode_frame,
    3531             :     .flush                 = hevc_decode_flush,
    3532             :     .update_thread_context = hevc_update_thread_context,
    3533             :     .init_thread_copy      = hevc_init_thread_copy,
    3534             :     .capabilities          = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DELAY |
    3535             :                              AV_CODEC_CAP_SLICE_THREADS | AV_CODEC_CAP_FRAME_THREADS,
    3536             :     .caps_internal         = FF_CODEC_CAP_INIT_THREADSAFE | FF_CODEC_CAP_EXPORTS_CROPPING,
    3537             :     .profiles              = NULL_IF_CONFIG_SMALL(ff_hevc_profiles),
    3538             :     .hw_configs            = (const AVCodecHWConfigInternal*[]) {
    3539             : #if CONFIG_HEVC_DXVA2_HWACCEL
    3540             :                                HWACCEL_DXVA2(hevc),
    3541             : #endif
    3542             : #if CONFIG_HEVC_D3D11VA_HWACCEL
    3543             :                                HWACCEL_D3D11VA(hevc),
    3544             : #endif
    3545             : #if CONFIG_HEVC_D3D11VA2_HWACCEL
    3546             :                                HWACCEL_D3D11VA2(hevc),
    3547             : #endif
    3548             : #if CONFIG_HEVC_NVDEC_HWACCEL
    3549             :                                HWACCEL_NVDEC(hevc),
    3550             : #endif
    3551             : #if CONFIG_HEVC_VAAPI_HWACCEL
    3552             :                                HWACCEL_VAAPI(hevc),
    3553             : #endif
    3554             : #if CONFIG_HEVC_VDPAU_HWACCEL
    3555             :                                HWACCEL_VDPAU(hevc),
    3556             : #endif
    3557             : #if CONFIG_HEVC_VIDEOTOOLBOX_HWACCEL
    3558             :                                HWACCEL_VIDEOTOOLBOX(hevc),
    3559             : #endif
    3560             :                                NULL
    3561             :                            },
    3562             : };

Generated by: LCOV version 1.13