LCOV - code coverage report
Current view: top level - libavcodec - hevc_ps.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 828 1078 76.8 %
Date: 2018-05-20 11:54:08 Functions: 21 21 100.0 %

          Line data    Source code
       1             : /*
       2             :  * HEVC Parameter Set decoding
       3             :  *
       4             :  * Copyright (C) 2012 - 2103 Guillaume Martres
       5             :  * Copyright (C) 2012 - 2103 Mickael Raulet
       6             :  * Copyright (C) 2012 - 2013 Gildas Cocherel
       7             :  * Copyright (C) 2013 Vittorio Giovara
       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/imgutils.h"
      27             : #include "golomb.h"
      28             : #include "hevc_data.h"
      29             : #include "hevc_ps.h"
      30             : 
      31             : static const uint8_t default_scaling_list_intra[] = {
      32             :     16, 16, 16, 16, 17, 18, 21, 24,
      33             :     16, 16, 16, 16, 17, 19, 22, 25,
      34             :     16, 16, 17, 18, 20, 22, 25, 29,
      35             :     16, 16, 18, 21, 24, 27, 31, 36,
      36             :     17, 17, 20, 24, 30, 35, 41, 47,
      37             :     18, 19, 22, 27, 35, 44, 54, 65,
      38             :     21, 22, 25, 31, 41, 54, 70, 88,
      39             :     24, 25, 29, 36, 47, 65, 88, 115
      40             : };
      41             : 
      42             : static const uint8_t default_scaling_list_inter[] = {
      43             :     16, 16, 16, 16, 17, 18, 20, 24,
      44             :     16, 16, 16, 17, 18, 20, 24, 25,
      45             :     16, 16, 17, 18, 20, 24, 25, 28,
      46             :     16, 17, 18, 20, 24, 25, 28, 33,
      47             :     17, 18, 20, 24, 25, 28, 33, 41,
      48             :     18, 20, 24, 25, 28, 33, 41, 54,
      49             :     20, 24, 25, 28, 33, 41, 54, 71,
      50             :     24, 25, 28, 33, 41, 54, 71, 91
      51             : };
      52             : 
      53             : static const AVRational vui_sar[] = {
      54             :     {  0,   1 },
      55             :     {  1,   1 },
      56             :     { 12,  11 },
      57             :     { 10,  11 },
      58             :     { 16,  11 },
      59             :     { 40,  33 },
      60             :     { 24,  11 },
      61             :     { 20,  11 },
      62             :     { 32,  11 },
      63             :     { 80,  33 },
      64             :     { 18,  11 },
      65             :     { 15,  11 },
      66             :     { 64,  33 },
      67             :     { 160, 99 },
      68             :     {  4,   3 },
      69             :     {  3,   2 },
      70             :     {  2,   1 },
      71             : };
      72             : 
      73        3185 : static void remove_pps(HEVCParamSets *s, int id)
      74             : {
      75        3185 :     if (s->pps_list[id] && s->pps == (const HEVCPPS*)s->pps_list[id]->data)
      76        1781 :         s->pps = NULL;
      77        3185 :     av_buffer_unref(&s->pps_list[id]);
      78        3185 : }
      79             : 
      80         567 : static void remove_sps(HEVCParamSets *s, int id)
      81             : {
      82             :     int i;
      83         567 :     if (s->sps_list[id]) {
      84          11 :         if (s->sps == (const HEVCSPS*)s->sps_list[id]->data)
      85          11 :             s->sps = NULL;
      86             : 
      87             :         /* drop all PPS that depend on this SPS */
      88         715 :         for (i = 0; i < FF_ARRAY_ELEMS(s->pps_list); i++)
      89         704 :             if (s->pps_list[i] && ((HEVCPPS*)s->pps_list[i]->data)->sps_id == id)
      90          11 :                 remove_pps(s, i);
      91             : 
      92          11 :         av_assert0(!(s->sps_list[id] && s->sps == (HEVCSPS*)s->sps_list[id]->data));
      93             :     }
      94         567 :     av_buffer_unref(&s->sps_list[id]);
      95         567 : }
      96             : 
      97         554 : static void remove_vps(HEVCParamSets *s, int id)
      98             : {
      99             :     int i;
     100         554 :     if (s->vps_list[id]) {
     101           3 :         if (s->vps == (const HEVCVPS*)s->vps_list[id]->data)
     102           3 :             s->vps = NULL;
     103             : 
     104          51 :         for (i = 0; i < FF_ARRAY_ELEMS(s->sps_list); i++)
     105          48 :             if (s->sps_list[i] && ((HEVCSPS*)s->sps_list[i]->data)->vps_id == id)
     106           3 :                 remove_sps(s, i);
     107             :     }
     108         554 :     av_buffer_unref(&s->vps_list[id]);
     109         554 : }
     110             : 
     111       12108 : int ff_hevc_decode_short_term_rps(GetBitContext *gb, AVCodecContext *avctx,
     112             :                                   ShortTermRPS *rps, const HEVCSPS *sps, int is_slice_header)
     113             : {
     114       12108 :     uint8_t rps_predict = 0;
     115             :     int delta_poc;
     116       12108 :     int k0 = 0;
     117       12108 :     int k1 = 0;
     118       12108 :     int k  = 0;
     119             :     int i;
     120             : 
     121       12108 :     if (rps != sps->st_rps && sps->nb_st_rps)
     122       11061 :         rps_predict = get_bits1(gb);
     123             : 
     124       12108 :     if (rps_predict) {
     125             :         const ShortTermRPS *rps_ridx;
     126             :         int delta_rps;
     127             :         unsigned abs_delta_rps;
     128        7332 :         uint8_t use_delta_flag = 0;
     129             :         uint8_t delta_rps_sign;
     130             : 
     131        7332 :         if (is_slice_header) {
     132         619 :             unsigned int delta_idx = get_ue_golomb_long(gb) + 1;
     133         619 :             if (delta_idx > sps->nb_st_rps) {
     134           0 :                 av_log(avctx, AV_LOG_ERROR,
     135             :                        "Invalid value of delta_idx in slice header RPS: %d > %d.\n",
     136             :                        delta_idx, sps->nb_st_rps);
     137           0 :                 return AVERROR_INVALIDDATA;
     138             :             }
     139         619 :             rps_ridx = &sps->st_rps[sps->nb_st_rps - delta_idx];
     140         619 :             rps->rps_idx_num_delta_pocs = rps_ridx->num_delta_pocs;
     141             :         } else
     142        6713 :             rps_ridx = &sps->st_rps[rps - sps->st_rps - 1];
     143             : 
     144        7332 :         delta_rps_sign = get_bits1(gb);
     145        7332 :         abs_delta_rps  = get_ue_golomb_long(gb) + 1;
     146        7332 :         if (abs_delta_rps < 1 || abs_delta_rps > 32768) {
     147           0 :             av_log(avctx, AV_LOG_ERROR,
     148             :                    "Invalid value of abs_delta_rps: %d\n",
     149             :                    abs_delta_rps);
     150           0 :             return AVERROR_INVALIDDATA;
     151             :         }
     152        7332 :         delta_rps      = (1 - (delta_rps_sign << 1)) * abs_delta_rps;
     153       39155 :         for (i = 0; i <= rps_ridx->num_delta_pocs; i++) {
     154       31823 :             int used = rps->used[k] = get_bits1(gb);
     155             : 
     156       31823 :             if (!used)
     157        6320 :                 use_delta_flag = get_bits1(gb);
     158             : 
     159       31823 :             if (used || use_delta_flag) {
     160       25553 :                 if (i < rps_ridx->num_delta_pocs)
     161       20142 :                     delta_poc = delta_rps + rps_ridx->delta_poc[i];
     162             :                 else
     163        5411 :                     delta_poc = delta_rps;
     164       25553 :                 rps->delta_poc[k] = delta_poc;
     165       25553 :                 if (delta_poc < 0)
     166       17674 :                     k0++;
     167             :                 else
     168        7879 :                     k1++;
     169       25553 :                 k++;
     170             :             }
     171             :         }
     172             : 
     173        7332 :         if (k >= FF_ARRAY_ELEMS(rps->used)) {
     174           0 :             av_log(avctx, AV_LOG_ERROR,
     175             :                    "Invalid num_delta_pocs: %d\n", k);
     176           0 :             return AVERROR_INVALIDDATA;
     177             :         }
     178             : 
     179        7332 :         rps->num_delta_pocs    = k;
     180        7332 :         rps->num_negative_pics = k0;
     181             :         // sort in increasing order (smallest first)
     182        7332 :         if (rps->num_delta_pocs != 0) {
     183             :             int used, tmp;
     184       25553 :             for (i = 1; i < rps->num_delta_pocs; i++) {
     185       18221 :                 delta_poc = rps->delta_poc[i];
     186       18221 :                 used      = rps->used[i];
     187       52170 :                 for (k = i - 1; k >= 0; k--) {
     188       33949 :                     tmp = rps->delta_poc[k];
     189       33949 :                     if (delta_poc < tmp) {
     190       10822 :                         rps->delta_poc[k + 1] = tmp;
     191       10822 :                         rps->used[k + 1]      = rps->used[k];
     192       10822 :                         rps->delta_poc[k]     = delta_poc;
     193       10822 :                         rps->used[k]          = used;
     194             :                     }
     195             :                 }
     196             :             }
     197             :         }
     198        7332 :         if ((rps->num_negative_pics >> 1) != 0) {
     199             :             int used;
     200        5559 :             k = rps->num_negative_pics - 1;
     201             :             // flip the negative values to largest first
     202       12846 :             for (i = 0; i < rps->num_negative_pics >> 1; i++) {
     203        7287 :                 delta_poc         = rps->delta_poc[i];
     204        7287 :                 used              = rps->used[i];
     205        7287 :                 rps->delta_poc[i] = rps->delta_poc[k];
     206        7287 :                 rps->used[i]      = rps->used[k];
     207        7287 :                 rps->delta_poc[k] = delta_poc;
     208        7287 :                 rps->used[k]      = used;
     209        7287 :                 k--;
     210             :             }
     211             :         }
     212             :     } else {
     213             :         unsigned int prev, nb_positive_pics;
     214        4776 :         rps->num_negative_pics = get_ue_golomb_long(gb);
     215        4776 :         nb_positive_pics       = get_ue_golomb_long(gb);
     216             : 
     217        4776 :         if (rps->num_negative_pics >= HEVC_MAX_REFS ||
     218             :             nb_positive_pics >= HEVC_MAX_REFS) {
     219           0 :             av_log(avctx, AV_LOG_ERROR, "Too many refs in a short term RPS.\n");
     220           0 :             return AVERROR_INVALIDDATA;
     221             :         }
     222             : 
     223        4776 :         rps->num_delta_pocs = rps->num_negative_pics + nb_positive_pics;
     224        4776 :         if (rps->num_delta_pocs) {
     225        4493 :             prev = 0;
     226       18117 :             for (i = 0; i < rps->num_negative_pics; i++) {
     227       13624 :                 delta_poc = get_ue_golomb_long(gb) + 1;
     228       13624 :                 if (delta_poc < 1 || delta_poc > 32768) {
     229           0 :                     av_log(avctx, AV_LOG_ERROR,
     230             :                         "Invalid value of delta_poc: %d\n",
     231             :                         delta_poc);
     232           0 :                     return AVERROR_INVALIDDATA;
     233             :                 }
     234       13624 :                 prev -= delta_poc;
     235       13624 :                 rps->delta_poc[i] = prev;
     236       13624 :                 rps->used[i]      = get_bits1(gb);
     237             :             }
     238        4493 :             prev = 0;
     239        5546 :             for (i = 0; i < nb_positive_pics; i++) {
     240        1053 :                 delta_poc = get_ue_golomb_long(gb) + 1;
     241        1053 :                 if (delta_poc < 1 || delta_poc > 32768) {
     242           0 :                     av_log(avctx, AV_LOG_ERROR,
     243             :                         "Invalid value of delta_poc: %d\n",
     244             :                         delta_poc);
     245           0 :                     return AVERROR_INVALIDDATA;
     246             :                 }
     247        1053 :                 prev += delta_poc;
     248        1053 :                 rps->delta_poc[rps->num_negative_pics + i] = prev;
     249        1053 :                 rps->used[rps->num_negative_pics + i]      = get_bits1(gb);
     250             :             }
     251             :         }
     252             :     }
     253       12108 :     return 0;
     254             : }
     255             : 
     256             : 
     257        1987 : static int decode_profile_tier_level(GetBitContext *gb, AVCodecContext *avctx,
     258             :                                       PTLCommon *ptl)
     259             : {
     260             :     int i;
     261             : 
     262        1987 :     if (get_bits_left(gb) < 2+1+5 + 32 + 4 + 16 + 16 + 12)
     263           0 :         return -1;
     264             : 
     265        1987 :     ptl->profile_space = get_bits(gb, 2);
     266        1987 :     ptl->tier_flag     = get_bits1(gb);
     267        1987 :     ptl->profile_idc   = get_bits(gb, 5);
     268        1987 :     if (ptl->profile_idc == FF_PROFILE_HEVC_MAIN)
     269        1723 :         av_log(avctx, AV_LOG_DEBUG, "Main profile bitstream\n");
     270         264 :     else if (ptl->profile_idc == FF_PROFILE_HEVC_MAIN_10)
     271         112 :         av_log(avctx, AV_LOG_DEBUG, "Main 10 profile bitstream\n");
     272         152 :     else if (ptl->profile_idc == FF_PROFILE_HEVC_MAIN_STILL_PICTURE)
     273           8 :         av_log(avctx, AV_LOG_DEBUG, "Main Still Picture profile bitstream\n");
     274         144 :     else if (ptl->profile_idc == FF_PROFILE_HEVC_REXT)
     275         104 :         av_log(avctx, AV_LOG_DEBUG, "Range Extension profile bitstream\n");
     276             :     else
     277          40 :         av_log(avctx, AV_LOG_WARNING, "Unknown HEVC profile: %d\n", ptl->profile_idc);
     278             : 
     279       65571 :     for (i = 0; i < 32; i++) {
     280       63584 :         ptl->profile_compatibility_flag[i] = get_bits1(gb);
     281             : 
     282       63584 :         if (ptl->profile_idc == 0 && i > 0 && ptl->profile_compatibility_flag[i])
     283          40 :             ptl->profile_idc = i;
     284             :     }
     285        1987 :     ptl->progressive_source_flag    = get_bits1(gb);
     286        1987 :     ptl->interlaced_source_flag     = get_bits1(gb);
     287        1987 :     ptl->non_packed_constraint_flag = get_bits1(gb);
     288        1987 :     ptl->frame_only_constraint_flag = get_bits1(gb);
     289             : 
     290        1987 :     skip_bits(gb, 16); // XXX_reserved_zero_44bits[0..15]
     291        1987 :     skip_bits(gb, 16); // XXX_reserved_zero_44bits[16..31]
     292        1987 :     skip_bits(gb, 12); // XXX_reserved_zero_44bits[32..43]
     293             : 
     294        1987 :     return 0;
     295             : }
     296             : 
     297        1947 : static int parse_ptl(GetBitContext *gb, AVCodecContext *avctx,
     298             :                       PTL *ptl, int max_num_sub_layers)
     299             : {
     300             :     int i;
     301        3894 :     if (decode_profile_tier_level(gb, avctx, &ptl->general_ptl) < 0 ||
     302        1947 :         get_bits_left(gb) < 8 + (8*2 * (max_num_sub_layers - 1 > 0))) {
     303           0 :         av_log(avctx, AV_LOG_ERROR, "PTL information too short\n");
     304           0 :         return -1;
     305             :     }
     306             : 
     307        1947 :     ptl->general_ptl.level_idc = get_bits(gb, 8);
     308             : 
     309        2403 :     for (i = 0; i < max_num_sub_layers - 1; i++) {
     310         456 :         ptl->sub_layer_profile_present_flag[i] = get_bits1(gb);
     311         456 :         ptl->sub_layer_level_present_flag[i]   = get_bits1(gb);
     312             :     }
     313             : 
     314        1947 :     if (max_num_sub_layers - 1> 0)
     315        1614 :         for (i = max_num_sub_layers - 1; i < 8; i++)
     316        1384 :             skip_bits(gb, 2); // reserved_zero_2bits[i]
     317        2403 :     for (i = 0; i < max_num_sub_layers - 1; i++) {
     318         496 :         if (ptl->sub_layer_profile_present_flag[i] &&
     319          40 :             decode_profile_tier_level(gb, avctx, &ptl->sub_layer_ptl[i]) < 0) {
     320           0 :             av_log(avctx, AV_LOG_ERROR,
     321             :                    "PTL information for sublayer %i too short\n", i);
     322           0 :             return -1;
     323             :         }
     324         456 :         if (ptl->sub_layer_level_present_flag[i]) {
     325          40 :             if (get_bits_left(gb) < 8) {
     326           0 :                 av_log(avctx, AV_LOG_ERROR,
     327             :                        "Not enough data for sublayer %i level_idc\n", i);
     328           0 :                 return -1;
     329             :             } else
     330          40 :                 ptl->sub_layer_ptl[i].level_idc = get_bits(gb, 8);
     331             :         }
     332             :     }
     333             : 
     334        1947 :     return 0;
     335             : }
     336             : 
     337          92 : static void decode_sublayer_hrd(GetBitContext *gb, unsigned int nb_cpb,
     338             :                                 int subpic_params_present)
     339             : {
     340             :     int i;
     341             : 
     342         184 :     for (i = 0; i < nb_cpb; i++) {
     343          92 :         get_ue_golomb_long(gb); // bit_rate_value_minus1
     344          92 :         get_ue_golomb_long(gb); // cpb_size_value_minus1
     345             : 
     346          92 :         if (subpic_params_present) {
     347          26 :             get_ue_golomb_long(gb); // cpb_size_du_value_minus1
     348          26 :             get_ue_golomb_long(gb); // bit_rate_du_value_minus1
     349             :         }
     350          92 :         skip_bits1(gb); // cbr_flag
     351             :     }
     352          92 : }
     353             : 
     354          46 : static int decode_hrd(GetBitContext *gb, int common_inf_present,
     355             :                        int max_sublayers)
     356             : {
     357          46 :     int nal_params_present = 0, vcl_params_present = 0;
     358          46 :     int subpic_params_present = 0;
     359             :     int i;
     360             : 
     361          46 :     if (common_inf_present) {
     362          46 :         nal_params_present = get_bits1(gb);
     363          46 :         vcl_params_present = get_bits1(gb);
     364             : 
     365          46 :         if (nal_params_present || vcl_params_present) {
     366          46 :             subpic_params_present = get_bits1(gb);
     367             : 
     368          46 :             if (subpic_params_present) {
     369          13 :                 skip_bits(gb, 8); // tick_divisor_minus2
     370          13 :                 skip_bits(gb, 5); // du_cpb_removal_delay_increment_length_minus1
     371          13 :                 skip_bits(gb, 1); // sub_pic_cpb_params_in_pic_timing_sei_flag
     372          13 :                 skip_bits(gb, 5); // dpb_output_delay_du_length_minus1
     373             :             }
     374             : 
     375          46 :             skip_bits(gb, 4); // bit_rate_scale
     376          46 :             skip_bits(gb, 4); // cpb_size_scale
     377             : 
     378          46 :             if (subpic_params_present)
     379          13 :                 skip_bits(gb, 4);  // cpb_size_du_scale
     380             : 
     381          46 :             skip_bits(gb, 5); // initial_cpb_removal_delay_length_minus1
     382          46 :             skip_bits(gb, 5); // au_cpb_removal_delay_length_minus1
     383          46 :             skip_bits(gb, 5); // dpb_output_delay_length_minus1
     384             :         }
     385             :     }
     386             : 
     387          92 :     for (i = 0; i < max_sublayers; i++) {
     388          46 :         int low_delay = 0;
     389          46 :         unsigned int nb_cpb = 1;
     390          46 :         int fixed_rate = get_bits1(gb);
     391             : 
     392          46 :         if (!fixed_rate)
     393          23 :             fixed_rate = get_bits1(gb);
     394             : 
     395          46 :         if (fixed_rate)
     396          23 :             get_ue_golomb_long(gb);  // elemental_duration_in_tc_minus1
     397             :         else
     398          23 :             low_delay = get_bits1(gb);
     399             : 
     400          46 :         if (!low_delay) {
     401          46 :             nb_cpb = get_ue_golomb_long(gb) + 1;
     402          46 :             if (nb_cpb < 1 || nb_cpb > 32) {
     403           0 :                 av_log(NULL, AV_LOG_ERROR, "nb_cpb %d invalid\n", nb_cpb);
     404           0 :                 return AVERROR_INVALIDDATA;
     405             :             }
     406             :         }
     407             : 
     408          46 :         if (nal_params_present)
     409          46 :             decode_sublayer_hrd(gb, nb_cpb, subpic_params_present);
     410          46 :         if (vcl_params_present)
     411          46 :             decode_sublayer_hrd(gb, nb_cpb, subpic_params_present);
     412             :     }
     413          46 :     return 0;
     414             : }
     415             : 
     416         977 : int ff_hevc_decode_nal_vps(GetBitContext *gb, AVCodecContext *avctx,
     417             :                            HEVCParamSets *ps)
     418             : {
     419             :     int i,j;
     420         977 :     int vps_id = 0;
     421             :     ptrdiff_t nal_size;
     422             :     HEVCVPS *vps;
     423         977 :     AVBufferRef *vps_buf = av_buffer_allocz(sizeof(*vps));
     424             : 
     425         977 :     if (!vps_buf)
     426           0 :         return AVERROR(ENOMEM);
     427         977 :     vps = (HEVCVPS*)vps_buf->data;
     428             : 
     429         977 :     av_log(avctx, AV_LOG_DEBUG, "Decoding VPS\n");
     430             : 
     431         977 :     nal_size = gb->buffer_end - gb->buffer;
     432         977 :     if (nal_size > sizeof(vps->data)) {
     433           0 :         av_log(avctx, AV_LOG_WARNING, "Truncating likely oversized VPS "
     434             :                "(%"PTRDIFF_SPECIFIER" > %"SIZE_SPECIFIER")\n",
     435             :                nal_size, sizeof(vps->data));
     436           0 :         vps->data_size = sizeof(vps->data);
     437             :     } else {
     438         977 :         vps->data_size = nal_size;
     439             :     }
     440         977 :     memcpy(vps->data, gb->buffer, vps->data_size);
     441             : 
     442         977 :     vps_id = get_bits(gb, 4);
     443         977 :     if (vps_id >= HEVC_MAX_VPS_COUNT) {
     444           0 :         av_log(avctx, AV_LOG_ERROR, "VPS id out of range: %d\n", vps_id);
     445           0 :         goto err;
     446             :     }
     447             : 
     448         977 :     if (get_bits(gb, 2) != 3) { // vps_reserved_three_2bits
     449           0 :         av_log(avctx, AV_LOG_ERROR, "vps_reserved_three_2bits is not three\n");
     450           0 :         goto err;
     451             :     }
     452             : 
     453         977 :     vps->vps_max_layers               = get_bits(gb, 6) + 1;
     454         977 :     vps->vps_max_sub_layers           = get_bits(gb, 3) + 1;
     455         977 :     vps->vps_temporal_id_nesting_flag = get_bits1(gb);
     456             : 
     457         977 :     if (get_bits(gb, 16) != 0xffff) { // vps_reserved_ffff_16bits
     458           0 :         av_log(avctx, AV_LOG_ERROR, "vps_reserved_ffff_16bits is not 0xffff\n");
     459           0 :         goto err;
     460             :     }
     461             : 
     462         977 :     if (vps->vps_max_sub_layers > HEVC_MAX_SUB_LAYERS) {
     463           0 :         av_log(avctx, AV_LOG_ERROR, "vps_max_sub_layers out of range: %d\n",
     464             :                vps->vps_max_sub_layers);
     465           0 :         goto err;
     466             :     }
     467             : 
     468         977 :     if (parse_ptl(gb, avctx, &vps->ptl, vps->vps_max_sub_layers) < 0)
     469           0 :         goto err;
     470             : 
     471         977 :     vps->vps_sub_layer_ordering_info_present_flag = get_bits1(gb);
     472             : 
     473         977 :     i = vps->vps_sub_layer_ordering_info_present_flag ? 0 : vps->vps_max_sub_layers - 1;
     474        2095 :     for (; i < vps->vps_max_sub_layers; i++) {
     475        1118 :         vps->vps_max_dec_pic_buffering[i] = get_ue_golomb_long(gb) + 1;
     476        1118 :         vps->vps_num_reorder_pics[i]      = get_ue_golomb_long(gb);
     477        1118 :         vps->vps_max_latency_increase[i]  = get_ue_golomb_long(gb) - 1;
     478             : 
     479        1118 :         if (vps->vps_max_dec_pic_buffering[i] > HEVC_MAX_DPB_SIZE || !vps->vps_max_dec_pic_buffering[i]) {
     480           0 :             av_log(avctx, AV_LOG_ERROR, "vps_max_dec_pic_buffering_minus1 out of range: %d\n",
     481           0 :                    vps->vps_max_dec_pic_buffering[i] - 1);
     482           0 :             goto err;
     483             :         }
     484        1118 :         if (vps->vps_num_reorder_pics[i] > vps->vps_max_dec_pic_buffering[i] - 1) {
     485           0 :             av_log(avctx, AV_LOG_WARNING, "vps_max_num_reorder_pics out of range: %d\n",
     486             :                    vps->vps_num_reorder_pics[i]);
     487           0 :             if (avctx->err_recognition & AV_EF_EXPLODE)
     488           0 :                 goto err;
     489             :         }
     490             :     }
     491             : 
     492         977 :     vps->vps_max_layer_id   = get_bits(gb, 6);
     493         977 :     vps->vps_num_layer_sets = get_ue_golomb_long(gb) + 1;
     494        1954 :     if (vps->vps_num_layer_sets < 1 || vps->vps_num_layer_sets > 1024 ||
     495         977 :         (vps->vps_num_layer_sets - 1LL) * (vps->vps_max_layer_id + 1LL) > get_bits_left(gb)) {
     496           0 :         av_log(avctx, AV_LOG_ERROR, "too many layer_id_included_flags\n");
     497           0 :         goto err;
     498             :     }
     499             : 
     500         977 :     for (i = 1; i < vps->vps_num_layer_sets; i++)
     501           0 :         for (j = 0; j <= vps->vps_max_layer_id; j++)
     502           0 :             skip_bits(gb, 1);  // layer_id_included_flag[i][j]
     503             : 
     504         977 :     vps->vps_timing_info_present_flag = get_bits1(gb);
     505         977 :     if (vps->vps_timing_info_present_flag) {
     506          28 :         vps->vps_num_units_in_tick               = get_bits_long(gb, 32);
     507          28 :         vps->vps_time_scale                      = get_bits_long(gb, 32);
     508          28 :         vps->vps_poc_proportional_to_timing_flag = get_bits1(gb);
     509          28 :         if (vps->vps_poc_proportional_to_timing_flag)
     510          28 :             vps->vps_num_ticks_poc_diff_one = get_ue_golomb_long(gb) + 1;
     511          28 :         vps->vps_num_hrd_parameters = get_ue_golomb_long(gb);
     512          28 :         if (vps->vps_num_hrd_parameters > (unsigned)vps->vps_num_layer_sets) {
     513           0 :             av_log(avctx, AV_LOG_ERROR,
     514             :                    "vps_num_hrd_parameters %d is invalid\n", vps->vps_num_hrd_parameters);
     515           0 :             goto err;
     516             :         }
     517          33 :         for (i = 0; i < vps->vps_num_hrd_parameters; i++) {
     518           5 :             int common_inf_present = 1;
     519             : 
     520           5 :             get_ue_golomb_long(gb); // hrd_layer_set_idx
     521           5 :             if (i)
     522           0 :                 common_inf_present = get_bits1(gb);
     523           5 :             decode_hrd(gb, common_inf_present, vps->vps_max_sub_layers);
     524             :         }
     525             :     }
     526         977 :     get_bits1(gb); /* vps_extension_flag */
     527             : 
     528         977 :     if (get_bits_left(gb) < 0) {
     529           0 :         av_log(avctx, AV_LOG_ERROR,
     530           0 :                "Overread VPS by %d bits\n", -get_bits_left(gb));
     531           0 :         if (ps->vps_list[vps_id])
     532           0 :             goto err;
     533             :     }
     534             : 
     535        1403 :     if (ps->vps_list[vps_id] &&
     536         426 :         !memcmp(ps->vps_list[vps_id]->data, vps_buf->data, vps_buf->size)) {
     537         423 :         av_buffer_unref(&vps_buf);
     538             :     } else {
     539         554 :         remove_vps(ps, vps_id);
     540         554 :         ps->vps_list[vps_id] = vps_buf;
     541             :     }
     542             : 
     543         977 :     return 0;
     544             : 
     545           0 : err:
     546           0 :     av_buffer_unref(&vps_buf);
     547           0 :     return AVERROR_INVALIDDATA;
     548             : }
     549             : 
     550         206 : static void decode_vui(GetBitContext *gb, AVCodecContext *avctx,
     551             :                        int apply_defdispwin, HEVCSPS *sps)
     552             : {
     553         206 :     VUI backup_vui, *vui = &sps->vui;
     554             :     GetBitContext backup;
     555         206 :     int sar_present, alt = 0;
     556             : 
     557         206 :     av_log(avctx, AV_LOG_DEBUG, "Decoding VUI\n");
     558             : 
     559         206 :     sar_present = get_bits1(gb);
     560         206 :     if (sar_present) {
     561         103 :         uint8_t sar_idx = get_bits(gb, 8);
     562         103 :         if (sar_idx < FF_ARRAY_ELEMS(vui_sar))
     563         103 :             vui->sar = vui_sar[sar_idx];
     564           0 :         else if (sar_idx == 255) {
     565           0 :             vui->sar.num = get_bits(gb, 16);
     566           0 :             vui->sar.den = get_bits(gb, 16);
     567             :         } else
     568           0 :             av_log(avctx, AV_LOG_WARNING,
     569             :                    "Unknown SAR index: %u.\n", sar_idx);
     570             :     }
     571             : 
     572         206 :     vui->overscan_info_present_flag = get_bits1(gb);
     573         206 :     if (vui->overscan_info_present_flag)
     574           0 :         vui->overscan_appropriate_flag = get_bits1(gb);
     575             : 
     576         206 :     vui->video_signal_type_present_flag = get_bits1(gb);
     577         206 :     if (vui->video_signal_type_present_flag) {
     578           8 :         vui->video_format                    = get_bits(gb, 3);
     579           8 :         vui->video_full_range_flag           = get_bits1(gb);
     580           8 :         vui->colour_description_present_flag = get_bits1(gb);
     581           8 :         if (vui->video_full_range_flag && sps->pix_fmt == AV_PIX_FMT_YUV420P)
     582           0 :             sps->pix_fmt = AV_PIX_FMT_YUVJ420P;
     583           8 :         if (vui->colour_description_present_flag) {
     584           8 :             vui->colour_primaries        = get_bits(gb, 8);
     585           8 :             vui->transfer_characteristic = get_bits(gb, 8);
     586           8 :             vui->matrix_coeffs           = get_bits(gb, 8);
     587             : 
     588             :             // Set invalid values to "unspecified"
     589           8 :             if (!av_color_primaries_name(vui->colour_primaries))
     590           0 :                 vui->colour_primaries = AVCOL_PRI_UNSPECIFIED;
     591           8 :             if (!av_color_transfer_name(vui->transfer_characteristic))
     592           0 :                 vui->transfer_characteristic = AVCOL_TRC_UNSPECIFIED;
     593           8 :             if (!av_color_space_name(vui->matrix_coeffs))
     594           0 :                 vui->matrix_coeffs = AVCOL_SPC_UNSPECIFIED;
     595           8 :             if (vui->matrix_coeffs == AVCOL_SPC_RGB) {
     596           0 :                 switch (sps->pix_fmt) {
     597           0 :                 case AV_PIX_FMT_YUV444P:
     598           0 :                     sps->pix_fmt = AV_PIX_FMT_GBRP;
     599           0 :                     break;
     600           0 :                 case AV_PIX_FMT_YUV444P10:
     601           0 :                     sps->pix_fmt = AV_PIX_FMT_GBRP10;
     602           0 :                     break;
     603           0 :                 case AV_PIX_FMT_YUV444P12:
     604           0 :                     sps->pix_fmt = AV_PIX_FMT_GBRP12;
     605           0 :                     break;
     606             :                 }
     607             :             }
     608             :         }
     609             :     }
     610             : 
     611         206 :     vui->chroma_loc_info_present_flag = get_bits1(gb);
     612         206 :     if (vui->chroma_loc_info_present_flag) {
     613           8 :         vui->chroma_sample_loc_type_top_field    = get_ue_golomb_long(gb);
     614           8 :         vui->chroma_sample_loc_type_bottom_field = get_ue_golomb_long(gb);
     615             :     }
     616             : 
     617         206 :     vui->neutra_chroma_indication_flag = get_bits1(gb);
     618         206 :     vui->field_seq_flag                = get_bits1(gb);
     619         206 :     vui->frame_field_info_present_flag = get_bits1(gb);
     620             : 
     621             :     // Backup context in case an alternate header is detected
     622         206 :     memcpy(&backup, gb, sizeof(backup));
     623         206 :     memcpy(&backup_vui, vui, sizeof(backup_vui));
     624         206 :     if (get_bits_left(gb) >= 68 && show_bits_long(gb, 21) == 0x100000) {
     625           0 :         vui->default_display_window_flag = 0;
     626           0 :         av_log(avctx, AV_LOG_WARNING, "Invalid default display window\n");
     627             :     } else
     628         206 :         vui->default_display_window_flag = get_bits1(gb);
     629             : 
     630         206 :     if (vui->default_display_window_flag) {
     631          91 :         int vert_mult  = 1 + (sps->chroma_format_idc < 2);
     632          91 :         int horiz_mult = 1 + (sps->chroma_format_idc < 3);
     633          91 :         vui->def_disp_win.left_offset   = get_ue_golomb_long(gb) * horiz_mult;
     634          91 :         vui->def_disp_win.right_offset  = get_ue_golomb_long(gb) * horiz_mult;
     635          91 :         vui->def_disp_win.top_offset    = get_ue_golomb_long(gb) *  vert_mult;
     636          91 :         vui->def_disp_win.bottom_offset = get_ue_golomb_long(gb) *  vert_mult;
     637             : 
     638         131 :         if (apply_defdispwin &&
     639          40 :             avctx->flags2 & AV_CODEC_FLAG2_IGNORE_CROP) {
     640           0 :             av_log(avctx, AV_LOG_DEBUG,
     641             :                    "discarding vui default display window, "
     642             :                    "original values are l:%u r:%u t:%u b:%u\n",
     643             :                    vui->def_disp_win.left_offset,
     644             :                    vui->def_disp_win.right_offset,
     645             :                    vui->def_disp_win.top_offset,
     646             :                    vui->def_disp_win.bottom_offset);
     647             : 
     648           0 :             vui->def_disp_win.left_offset   =
     649           0 :             vui->def_disp_win.right_offset  =
     650           0 :             vui->def_disp_win.top_offset    =
     651           0 :             vui->def_disp_win.bottom_offset = 0;
     652             :         }
     653             :     }
     654             : 
     655         412 : timing_info:
     656         206 :     vui->vui_timing_info_present_flag = get_bits1(gb);
     657             : 
     658         206 :     if (vui->vui_timing_info_present_flag) {
     659          56 :         if( get_bits_left(gb) < 66 && !alt) {
     660             :             // The alternate syntax seem to have timing info located
     661             :             // at where def_disp_win is normally located
     662           0 :             av_log(avctx, AV_LOG_WARNING,
     663             :                    "Strange VUI timing information, retrying...\n");
     664           0 :             memcpy(vui, &backup_vui, sizeof(backup_vui));
     665           0 :             memcpy(gb, &backup, sizeof(backup));
     666           0 :             alt = 1;
     667           0 :             goto timing_info;
     668             :         }
     669          56 :         vui->vui_num_units_in_tick               = get_bits_long(gb, 32);
     670          56 :         vui->vui_time_scale                      = get_bits_long(gb, 32);
     671          56 :         if (alt) {
     672           0 :             av_log(avctx, AV_LOG_INFO, "Retry got %"PRIu32"/%"PRIu32" fps\n",
     673             :                    vui->vui_time_scale, vui->vui_num_units_in_tick);
     674             :         }
     675          56 :         vui->vui_poc_proportional_to_timing_flag = get_bits1(gb);
     676          56 :         if (vui->vui_poc_proportional_to_timing_flag)
     677          28 :             vui->vui_num_ticks_poc_diff_one_minus1 = get_ue_golomb_long(gb);
     678          56 :         vui->vui_hrd_parameters_present_flag = get_bits1(gb);
     679          56 :         if (vui->vui_hrd_parameters_present_flag)
     680          41 :             decode_hrd(gb, 1, sps->max_sub_layers);
     681             :     }
     682             : 
     683         206 :     vui->bitstream_restriction_flag = get_bits1(gb);
     684         206 :     if (vui->bitstream_restriction_flag) {
     685         103 :         if (get_bits_left(gb) < 8 && !alt) {
     686           0 :             av_log(avctx, AV_LOG_WARNING,
     687             :                    "Strange VUI bitstream restriction information, retrying"
     688             :                    " from timing information...\n");
     689           0 :             memcpy(vui, &backup_vui, sizeof(backup_vui));
     690           0 :             memcpy(gb, &backup, sizeof(backup));
     691           0 :             alt = 1;
     692           0 :             goto timing_info;
     693             :         }
     694         103 :         vui->tiles_fixed_structure_flag              = get_bits1(gb);
     695         103 :         vui->motion_vectors_over_pic_boundaries_flag = get_bits1(gb);
     696         103 :         vui->restricted_ref_pic_lists_flag           = get_bits1(gb);
     697         103 :         vui->min_spatial_segmentation_idc            = get_ue_golomb_long(gb);
     698         103 :         vui->max_bytes_per_pic_denom                 = get_ue_golomb_long(gb);
     699         103 :         vui->max_bits_per_min_cu_denom               = get_ue_golomb_long(gb);
     700         103 :         vui->log2_max_mv_length_horizontal           = get_ue_golomb_long(gb);
     701         103 :         vui->log2_max_mv_length_vertical             = get_ue_golomb_long(gb);
     702             :     }
     703             : 
     704         206 :     if (get_bits_left(gb) < 1 && !alt) {
     705             :         // XXX: Alternate syntax when sps_range_extension_flag != 0?
     706           0 :         av_log(avctx, AV_LOG_WARNING,
     707             :                "Overread in VUI, retrying from timing information...\n");
     708           0 :         memcpy(vui, &backup_vui, sizeof(backup_vui));
     709           0 :         memcpy(gb, &backup, sizeof(backup));
     710           0 :         alt = 1;
     711           0 :         goto timing_info;
     712             :     }
     713         206 : }
     714             : 
     715         499 : static void set_default_scaling_list_data(ScalingList *sl)
     716             : {
     717             :     int matrixId;
     718             : 
     719        3493 :     for (matrixId = 0; matrixId < 6; matrixId++) {
     720             :         // 4x4 default is 16
     721        2994 :         memset(sl->sl[0][matrixId], 16, 16);
     722        2994 :         sl->sl_dc[0][matrixId] = 16; // default for 16x16
     723        2994 :         sl->sl_dc[1][matrixId] = 16; // default for 32x32
     724             :     }
     725         499 :     memcpy(sl->sl[1][0], default_scaling_list_intra, 64);
     726         499 :     memcpy(sl->sl[1][1], default_scaling_list_intra, 64);
     727         499 :     memcpy(sl->sl[1][2], default_scaling_list_intra, 64);
     728         499 :     memcpy(sl->sl[1][3], default_scaling_list_inter, 64);
     729         499 :     memcpy(sl->sl[1][4], default_scaling_list_inter, 64);
     730         499 :     memcpy(sl->sl[1][5], default_scaling_list_inter, 64);
     731         499 :     memcpy(sl->sl[2][0], default_scaling_list_intra, 64);
     732         499 :     memcpy(sl->sl[2][1], default_scaling_list_intra, 64);
     733         499 :     memcpy(sl->sl[2][2], default_scaling_list_intra, 64);
     734         499 :     memcpy(sl->sl[2][3], default_scaling_list_inter, 64);
     735         499 :     memcpy(sl->sl[2][4], default_scaling_list_inter, 64);
     736         499 :     memcpy(sl->sl[2][5], default_scaling_list_inter, 64);
     737         499 :     memcpy(sl->sl[3][0], default_scaling_list_intra, 64);
     738         499 :     memcpy(sl->sl[3][1], default_scaling_list_intra, 64);
     739         499 :     memcpy(sl->sl[3][2], default_scaling_list_intra, 64);
     740         499 :     memcpy(sl->sl[3][3], default_scaling_list_inter, 64);
     741         499 :     memcpy(sl->sl[3][4], default_scaling_list_inter, 64);
     742         499 :     memcpy(sl->sl[3][5], default_scaling_list_inter, 64);
     743         499 : }
     744             : 
     745         489 : static int scaling_list_data(GetBitContext *gb, AVCodecContext *avctx, ScalingList *sl, HEVCSPS *sps)
     746             : {
     747             :     uint8_t scaling_list_pred_mode_flag;
     748             :     int32_t scaling_list_dc_coef[2][6];
     749             :     int size_id, matrix_id, pos;
     750             :     int i;
     751             : 
     752        2445 :     for (size_id = 0; size_id < 4; size_id++)
     753       11736 :         for (matrix_id = 0; matrix_id < 6; matrix_id += ((size_id == 3) ? 3 : 1)) {
     754        9780 :             scaling_list_pred_mode_flag = get_bits1(gb);
     755        9780 :             if (!scaling_list_pred_mode_flag) {
     756        3978 :                 unsigned int delta = get_ue_golomb_long(gb);
     757             :                 /* Only need to handle non-zero delta. Zero means default,
     758             :                  * which should already be in the arrays. */
     759        3978 :                 if (delta) {
     760             :                     // Copy from previous array.
     761        3852 :                     delta *= (size_id == 3) ? 3 : 1;
     762        3852 :                     if (matrix_id < delta) {
     763           0 :                         av_log(avctx, AV_LOG_ERROR,
     764             :                                "Invalid delta in scaling list data: %d.\n", delta);
     765           0 :                         return AVERROR_INVALIDDATA;
     766             :                     }
     767             : 
     768        7704 :                     memcpy(sl->sl[size_id][matrix_id],
     769        3852 :                            sl->sl[size_id][matrix_id - delta],
     770             :                            size_id > 0 ? 64 : 16);
     771        3852 :                     if (size_id > 1)
     772         530 :                         sl->sl_dc[size_id - 2][matrix_id] = sl->sl_dc[size_id - 2][matrix_id - delta];
     773             :                 }
     774             :             } else {
     775             :                 int next_coef, coef_num;
     776             :                 int32_t scaling_list_delta_coef;
     777             : 
     778        5802 :                 next_coef = 8;
     779        5802 :                 coef_num  = FFMIN(64, 1 << (4 + (size_id << 1)));
     780        5802 :                 if (size_id > 1) {
     781        3340 :                     scaling_list_dc_coef[size_id - 2][matrix_id] = get_se_golomb(gb) + 8;
     782        3340 :                     next_coef = scaling_list_dc_coef[size_id - 2][matrix_id];
     783        3340 :                     sl->sl_dc[size_id - 2][matrix_id] = next_coef;
     784             :                 }
     785      315114 :                 for (i = 0; i < coef_num; i++) {
     786      309312 :                     if (size_id == 0)
     787       41344 :                         pos = 4 * ff_hevc_diag_scan4x4_y[i] +
     788       20672 :                                   ff_hevc_diag_scan4x4_x[i];
     789             :                     else
     790      577280 :                         pos = 8 * ff_hevc_diag_scan8x8_y[i] +
     791      288640 :                                   ff_hevc_diag_scan8x8_x[i];
     792             : 
     793      309312 :                     scaling_list_delta_coef = get_se_golomb(gb);
     794      309312 :                     next_coef = (next_coef + 256U + scaling_list_delta_coef) % 256;
     795      309312 :                     sl->sl[size_id][matrix_id][pos] = next_coef;
     796             :                 }
     797             :             }
     798             :         }
     799             : 
     800         489 :     if (sps->chroma_format_idc == 3) {
     801        2795 :         for (i = 0; i < 64; i++) {
     802        2752 :             sl->sl[3][1][i] = sl->sl[2][1][i];
     803        2752 :             sl->sl[3][2][i] = sl->sl[2][2][i];
     804        2752 :             sl->sl[3][4][i] = sl->sl[2][4][i];
     805        2752 :             sl->sl[3][5][i] = sl->sl[2][5][i];
     806             :         }
     807          43 :         sl->sl_dc[1][1] = sl->sl_dc[0][1];
     808          43 :         sl->sl_dc[1][2] = sl->sl_dc[0][2];
     809          43 :         sl->sl_dc[1][4] = sl->sl_dc[0][4];
     810          43 :         sl->sl_dc[1][5] = sl->sl_dc[0][5];
     811             :     }
     812             : 
     813             : 
     814         489 :     return 0;
     815             : }
     816             : 
     817         970 : static int map_pixel_format(AVCodecContext *avctx, HEVCSPS *sps)
     818             : {
     819             :     const AVPixFmtDescriptor *desc;
     820         970 :     switch (sps->bit_depth) {
     821         867 :     case 8:
     822         867 :         if (sps->chroma_format_idc == 0) sps->pix_fmt = AV_PIX_FMT_GRAY8;
     823         867 :         if (sps->chroma_format_idc == 1) sps->pix_fmt = AV_PIX_FMT_YUV420P;
     824         867 :         if (sps->chroma_format_idc == 2) sps->pix_fmt = AV_PIX_FMT_YUV422P;
     825         867 :         if (sps->chroma_format_idc == 3) sps->pix_fmt = AV_PIX_FMT_YUV444P;
     826         867 :        break;
     827           0 :     case 9:
     828           0 :         if (sps->chroma_format_idc == 0) sps->pix_fmt = AV_PIX_FMT_GRAY16;
     829           0 :         if (sps->chroma_format_idc == 1) sps->pix_fmt = AV_PIX_FMT_YUV420P9;
     830           0 :         if (sps->chroma_format_idc == 2) sps->pix_fmt = AV_PIX_FMT_YUV422P9;
     831           0 :         if (sps->chroma_format_idc == 3) sps->pix_fmt = AV_PIX_FMT_YUV444P9;
     832           0 :         break;
     833          80 :     case 10:
     834          80 :         if (sps->chroma_format_idc == 0) sps->pix_fmt = AV_PIX_FMT_GRAY10;
     835          80 :         if (sps->chroma_format_idc == 1) sps->pix_fmt = AV_PIX_FMT_YUV420P10;
     836          80 :         if (sps->chroma_format_idc == 2) sps->pix_fmt = AV_PIX_FMT_YUV422P10;
     837          80 :         if (sps->chroma_format_idc == 3) sps->pix_fmt = AV_PIX_FMT_YUV444P10;
     838          80 :         break;
     839          23 :     case 12:
     840          23 :         if (sps->chroma_format_idc == 0) sps->pix_fmt = AV_PIX_FMT_GRAY12;
     841          23 :         if (sps->chroma_format_idc == 1) sps->pix_fmt = AV_PIX_FMT_YUV420P12;
     842          23 :         if (sps->chroma_format_idc == 2) sps->pix_fmt = AV_PIX_FMT_YUV422P12;
     843          23 :         if (sps->chroma_format_idc == 3) sps->pix_fmt = AV_PIX_FMT_YUV444P12;
     844          23 :         break;
     845           0 :     default:
     846           0 :         av_log(avctx, AV_LOG_ERROR,
     847             :                "The following bit-depths are currently specified: 8, 9, 10 and 12 bits, "
     848             :                "chroma_format_idc is %d, depth is %d\n",
     849             :                sps->chroma_format_idc, sps->bit_depth);
     850           0 :         return AVERROR_INVALIDDATA;
     851             :     }
     852             : 
     853         970 :     desc = av_pix_fmt_desc_get(sps->pix_fmt);
     854         970 :     if (!desc)
     855           0 :         return AVERROR(EINVAL);
     856             : 
     857         970 :     sps->hshift[0] = sps->vshift[0] = 0;
     858         970 :     sps->hshift[2] = sps->hshift[1] = desc->log2_chroma_w;
     859         970 :     sps->vshift[2] = sps->vshift[1] = desc->log2_chroma_h;
     860             : 
     861         970 :     sps->pixel_shift = sps->bit_depth > 8;
     862             : 
     863         970 :     return 0;
     864             : }
     865             : 
     866         970 : int ff_hevc_parse_sps(HEVCSPS *sps, GetBitContext *gb, unsigned int *sps_id,
     867             :                       int apply_defdispwin, AVBufferRef **vps_list, AVCodecContext *avctx)
     868             : {
     869             :     HEVCWindow *ow;
     870         970 :     int ret = 0;
     871             :     int log2_diff_max_min_transform_block_size;
     872             :     int bit_depth_chroma, start, vui_present, sublayer_ordering_info;
     873             :     int i;
     874             : 
     875             :     // Coded parameters
     876             : 
     877         970 :     sps->vps_id = get_bits(gb, 4);
     878         970 :     if (sps->vps_id >= HEVC_MAX_VPS_COUNT) {
     879           0 :         av_log(avctx, AV_LOG_ERROR, "VPS id out of range: %d\n", sps->vps_id);
     880           0 :         return AVERROR_INVALIDDATA;
     881             :     }
     882             : 
     883         970 :     if (vps_list && !vps_list[sps->vps_id]) {
     884           0 :         av_log(avctx, AV_LOG_ERROR, "VPS %d does not exist\n",
     885             :                sps->vps_id);
     886           0 :         return AVERROR_INVALIDDATA;
     887             :     }
     888             : 
     889         970 :     sps->max_sub_layers = get_bits(gb, 3) + 1;
     890         970 :     if (sps->max_sub_layers > HEVC_MAX_SUB_LAYERS) {
     891           0 :         av_log(avctx, AV_LOG_ERROR, "sps_max_sub_layers out of range: %d\n",
     892             :                sps->max_sub_layers);
     893           0 :         return AVERROR_INVALIDDATA;
     894             :     }
     895             : 
     896         970 :     sps->temporal_id_nesting_flag = get_bits(gb, 1);
     897             : 
     898         970 :     if ((ret = parse_ptl(gb, avctx, &sps->ptl, sps->max_sub_layers)) < 0)
     899           0 :         return ret;
     900             : 
     901         970 :     *sps_id = get_ue_golomb_long(gb);
     902         970 :     if (*sps_id >= HEVC_MAX_SPS_COUNT) {
     903           0 :         av_log(avctx, AV_LOG_ERROR, "SPS id out of range: %d\n", *sps_id);
     904           0 :         return AVERROR_INVALIDDATA;
     905             :     }
     906             : 
     907         970 :     sps->chroma_format_idc = get_ue_golomb_long(gb);
     908         970 :     if (sps->chroma_format_idc > 3U) {
     909           0 :         av_log(avctx, AV_LOG_ERROR, "chroma_format_idc %d is invalid\n", sps->chroma_format_idc);
     910           0 :         return AVERROR_INVALIDDATA;
     911             :     }
     912             : 
     913         970 :     if (sps->chroma_format_idc == 3)
     914          28 :         sps->separate_colour_plane_flag = get_bits1(gb);
     915             : 
     916         970 :     if (sps->separate_colour_plane_flag)
     917           0 :         sps->chroma_format_idc = 0;
     918             : 
     919         970 :     sps->width  = get_ue_golomb_long(gb);
     920         970 :     sps->height = get_ue_golomb_long(gb);
     921         970 :     if ((ret = av_image_check_size(sps->width,
     922         970 :                                    sps->height, 0, avctx)) < 0)
     923           0 :         return ret;
     924             : 
     925         970 :     if (get_bits1(gb)) { // pic_conformance_flag
     926         812 :         int vert_mult  = 1 + (sps->chroma_format_idc < 2);
     927         812 :         int horiz_mult = 1 + (sps->chroma_format_idc < 3);
     928         812 :         sps->pic_conf_win.left_offset   = get_ue_golomb_long(gb) * horiz_mult;
     929         812 :         sps->pic_conf_win.right_offset  = get_ue_golomb_long(gb) * horiz_mult;
     930         812 :         sps->pic_conf_win.top_offset    = get_ue_golomb_long(gb) *  vert_mult;
     931         812 :         sps->pic_conf_win.bottom_offset = get_ue_golomb_long(gb) *  vert_mult;
     932             : 
     933         812 :         if (avctx->flags2 & AV_CODEC_FLAG2_IGNORE_CROP) {
     934           0 :             av_log(avctx, AV_LOG_DEBUG,
     935             :                    "discarding sps conformance window, "
     936             :                    "original values are l:%u r:%u t:%u b:%u\n",
     937             :                    sps->pic_conf_win.left_offset,
     938             :                    sps->pic_conf_win.right_offset,
     939             :                    sps->pic_conf_win.top_offset,
     940             :                    sps->pic_conf_win.bottom_offset);
     941             : 
     942           0 :             sps->pic_conf_win.left_offset   =
     943           0 :             sps->pic_conf_win.right_offset  =
     944           0 :             sps->pic_conf_win.top_offset    =
     945           0 :             sps->pic_conf_win.bottom_offset = 0;
     946             :         }
     947         812 :         sps->output_window = sps->pic_conf_win;
     948             :     }
     949             : 
     950         970 :     sps->bit_depth   = get_ue_golomb_long(gb) + 8;
     951         970 :     bit_depth_chroma = get_ue_golomb_long(gb) + 8;
     952         970 :     if (sps->chroma_format_idc && bit_depth_chroma != sps->bit_depth) {
     953           0 :         av_log(avctx, AV_LOG_ERROR,
     954             :                "Luma bit depth (%d) is different from chroma bit depth (%d), "
     955             :                "this is unsupported.\n",
     956             :                sps->bit_depth, bit_depth_chroma);
     957           0 :         return AVERROR_INVALIDDATA;
     958             :     }
     959         970 :     sps->bit_depth_chroma = bit_depth_chroma;
     960             : 
     961         970 :     ret = map_pixel_format(avctx, sps);
     962         970 :     if (ret < 0)
     963           0 :         return ret;
     964             : 
     965         970 :     sps->log2_max_poc_lsb = get_ue_golomb_long(gb) + 4;
     966         970 :     if (sps->log2_max_poc_lsb > 16) {
     967           0 :         av_log(avctx, AV_LOG_ERROR, "log2_max_pic_order_cnt_lsb_minus4 out range: %d\n",
     968           0 :                sps->log2_max_poc_lsb - 4);
     969           0 :         return AVERROR_INVALIDDATA;
     970             :     }
     971             : 
     972         970 :     sublayer_ordering_info = get_bits1(gb);
     973         970 :     start = sublayer_ordering_info ? 0 : sps->max_sub_layers - 1;
     974        2081 :     for (i = start; i < sps->max_sub_layers; i++) {
     975        1111 :         sps->temporal_layer[i].max_dec_pic_buffering = get_ue_golomb_long(gb) + 1;
     976        1111 :         sps->temporal_layer[i].num_reorder_pics      = get_ue_golomb_long(gb);
     977        1111 :         sps->temporal_layer[i].max_latency_increase  = get_ue_golomb_long(gb) - 1;
     978        1111 :         if (sps->temporal_layer[i].max_dec_pic_buffering > (unsigned)HEVC_MAX_DPB_SIZE) {
     979           0 :             av_log(avctx, AV_LOG_ERROR, "sps_max_dec_pic_buffering_minus1 out of range: %d\n",
     980           0 :                    sps->temporal_layer[i].max_dec_pic_buffering - 1U);
     981           0 :             return AVERROR_INVALIDDATA;
     982             :         }
     983        1111 :         if (sps->temporal_layer[i].num_reorder_pics > sps->temporal_layer[i].max_dec_pic_buffering - 1) {
     984           0 :             av_log(avctx, AV_LOG_WARNING, "sps_max_num_reorder_pics out of range: %d\n",
     985             :                    sps->temporal_layer[i].num_reorder_pics);
     986           0 :             if (avctx->err_recognition & AV_EF_EXPLODE ||
     987           0 :                 sps->temporal_layer[i].num_reorder_pics > HEVC_MAX_DPB_SIZE - 1) {
     988           0 :                 return AVERROR_INVALIDDATA;
     989             :             }
     990           0 :             sps->temporal_layer[i].max_dec_pic_buffering = sps->temporal_layer[i].num_reorder_pics + 1;
     991             :         }
     992             :     }
     993             : 
     994         970 :     if (!sublayer_ordering_info) {
     995         211 :         for (i = 0; i < start; i++) {
     996          82 :             sps->temporal_layer[i].max_dec_pic_buffering = sps->temporal_layer[start].max_dec_pic_buffering;
     997          82 :             sps->temporal_layer[i].num_reorder_pics      = sps->temporal_layer[start].num_reorder_pics;
     998          82 :             sps->temporal_layer[i].max_latency_increase  = sps->temporal_layer[start].max_latency_increase;
     999             :         }
    1000             :     }
    1001             : 
    1002         970 :     sps->log2_min_cb_size                    = get_ue_golomb_long(gb) + 3;
    1003         970 :     sps->log2_diff_max_min_coding_block_size = get_ue_golomb_long(gb);
    1004         970 :     sps->log2_min_tb_size                    = get_ue_golomb_long(gb) + 2;
    1005         970 :     log2_diff_max_min_transform_block_size   = get_ue_golomb_long(gb);
    1006         970 :     sps->log2_max_trafo_size                 = log2_diff_max_min_transform_block_size +
    1007         970 :                                                sps->log2_min_tb_size;
    1008             : 
    1009         970 :     if (sps->log2_min_cb_size < 3 || sps->log2_min_cb_size > 30) {
    1010           0 :         av_log(avctx, AV_LOG_ERROR, "Invalid value %d for log2_min_cb_size", sps->log2_min_cb_size);
    1011           0 :         return AVERROR_INVALIDDATA;
    1012             :     }
    1013             : 
    1014         970 :     if (sps->log2_diff_max_min_coding_block_size > 30) {
    1015           0 :         av_log(avctx, AV_LOG_ERROR, "Invalid value %d for log2_diff_max_min_coding_block_size", sps->log2_diff_max_min_coding_block_size);
    1016           0 :         return AVERROR_INVALIDDATA;
    1017             :     }
    1018             : 
    1019         970 :     if (sps->log2_min_tb_size >= sps->log2_min_cb_size || sps->log2_min_tb_size < 2) {
    1020           0 :         av_log(avctx, AV_LOG_ERROR, "Invalid value for log2_min_tb_size");
    1021           0 :         return AVERROR_INVALIDDATA;
    1022             :     }
    1023             : 
    1024         970 :     if (log2_diff_max_min_transform_block_size < 0 || log2_diff_max_min_transform_block_size > 30) {
    1025           0 :         av_log(avctx, AV_LOG_ERROR, "Invalid value %d for log2_diff_max_min_transform_block_size", log2_diff_max_min_transform_block_size);
    1026           0 :         return AVERROR_INVALIDDATA;
    1027             :     }
    1028             : 
    1029         970 :     sps->max_transform_hierarchy_depth_inter = get_ue_golomb_long(gb);
    1030         970 :     sps->max_transform_hierarchy_depth_intra = get_ue_golomb_long(gb);
    1031             : 
    1032         970 :     sps->scaling_list_enable_flag = get_bits1(gb);
    1033         970 :     if (sps->scaling_list_enable_flag) {
    1034          36 :         set_default_scaling_list_data(&sps->scaling_list);
    1035             : 
    1036          36 :         if (get_bits1(gb)) {
    1037          26 :             ret = scaling_list_data(gb, avctx, &sps->scaling_list, sps);
    1038          26 :             if (ret < 0)
    1039           0 :                 return ret;
    1040             :         }
    1041             :     }
    1042             : 
    1043         970 :     sps->amp_enabled_flag = get_bits1(gb);
    1044         970 :     sps->sao_enabled      = get_bits1(gb);
    1045             : 
    1046         970 :     sps->pcm_enabled_flag = get_bits1(gb);
    1047         970 :     if (sps->pcm_enabled_flag) {
    1048          75 :         sps->pcm.bit_depth   = get_bits(gb, 4) + 1;
    1049          75 :         sps->pcm.bit_depth_chroma = get_bits(gb, 4) + 1;
    1050          75 :         sps->pcm.log2_min_pcm_cb_size = get_ue_golomb_long(gb) + 3;
    1051         150 :         sps->pcm.log2_max_pcm_cb_size = sps->pcm.log2_min_pcm_cb_size +
    1052          75 :                                         get_ue_golomb_long(gb);
    1053          75 :         if (FFMAX(sps->pcm.bit_depth, sps->pcm.bit_depth_chroma) > sps->bit_depth) {
    1054           0 :             av_log(avctx, AV_LOG_ERROR,
    1055             :                    "PCM bit depth (%d, %d) is greater than normal bit depth (%d)\n",
    1056           0 :                    sps->pcm.bit_depth, sps->pcm.bit_depth_chroma, sps->bit_depth);
    1057           0 :             return AVERROR_INVALIDDATA;
    1058             :         }
    1059             : 
    1060          75 :         sps->pcm.loop_filter_disable_flag = get_bits1(gb);
    1061             :     }
    1062             : 
    1063         970 :     sps->nb_st_rps = get_ue_golomb_long(gb);
    1064         970 :     if (sps->nb_st_rps > HEVC_MAX_SHORT_TERM_REF_PIC_SETS) {
    1065           0 :         av_log(avctx, AV_LOG_ERROR, "Too many short term RPS: %d.\n",
    1066             :                sps->nb_st_rps);
    1067           0 :         return AVERROR_INVALIDDATA;
    1068             :     }
    1069        9958 :     for (i = 0; i < sps->nb_st_rps; i++) {
    1070        8988 :         if ((ret = ff_hevc_decode_short_term_rps(gb, avctx, &sps->st_rps[i],
    1071             :                                                  sps, 0)) < 0)
    1072           0 :             return ret;
    1073             :     }
    1074             : 
    1075         970 :     sps->long_term_ref_pics_present_flag = get_bits1(gb);
    1076         970 :     if (sps->long_term_ref_pics_present_flag) {
    1077          96 :         sps->num_long_term_ref_pics_sps = get_ue_golomb_long(gb);
    1078          96 :         if (sps->num_long_term_ref_pics_sps > HEVC_MAX_LONG_TERM_REF_PICS) {
    1079           0 :             av_log(avctx, AV_LOG_ERROR, "Too many long term ref pics: %d.\n",
    1080           0 :                    sps->num_long_term_ref_pics_sps);
    1081           0 :             return AVERROR_INVALIDDATA;
    1082             :         }
    1083         160 :         for (i = 0; i < sps->num_long_term_ref_pics_sps; i++) {
    1084          64 :             sps->lt_ref_pic_poc_lsb_sps[i]       = get_bits(gb, sps->log2_max_poc_lsb);
    1085          64 :             sps->used_by_curr_pic_lt_sps_flag[i] = get_bits1(gb);
    1086             :         }
    1087             :     }
    1088             : 
    1089         970 :     sps->sps_temporal_mvp_enabled_flag          = get_bits1(gb);
    1090         970 :     sps->sps_strong_intra_smoothing_enable_flag = get_bits1(gb);
    1091         970 :     sps->vui.sar = (AVRational){0, 1};
    1092         970 :     vui_present = get_bits1(gb);
    1093         970 :     if (vui_present)
    1094         206 :         decode_vui(gb, avctx, apply_defdispwin, sps);
    1095             : 
    1096         970 :     if (get_bits1(gb)) { // sps_extension_flag
    1097             :         int sps_extension_flag[1];
    1098         134 :         for (i = 0; i < 1; i++)
    1099          67 :             sps_extension_flag[i] = get_bits1(gb);
    1100          67 :         skip_bits(gb, 7); //sps_extension_7bits = get_bits(gb, 7);
    1101          67 :         if (sps_extension_flag[0]) {
    1102             :             int extended_precision_processing_flag;
    1103             :             int cabac_bypass_alignment_enabled_flag;
    1104             : 
    1105          47 :             sps->transform_skip_rotation_enabled_flag = get_bits1(gb);
    1106          47 :             sps->transform_skip_context_enabled_flag  = get_bits1(gb);
    1107          47 :             sps->implicit_rdpcm_enabled_flag = get_bits1(gb);
    1108             : 
    1109          47 :             sps->explicit_rdpcm_enabled_flag = get_bits1(gb);
    1110             : 
    1111          47 :             extended_precision_processing_flag = get_bits1(gb);
    1112          47 :             if (extended_precision_processing_flag)
    1113           0 :                 av_log(avctx, AV_LOG_WARNING,
    1114             :                    "extended_precision_processing_flag not yet implemented\n");
    1115             : 
    1116          47 :             sps->intra_smoothing_disabled_flag       = get_bits1(gb);
    1117          47 :             sps->high_precision_offsets_enabled_flag = get_bits1(gb);
    1118          47 :             if (sps->high_precision_offsets_enabled_flag)
    1119          29 :                 av_log(avctx, AV_LOG_WARNING,
    1120             :                    "high_precision_offsets_enabled_flag not yet implemented\n");
    1121             : 
    1122          47 :             sps->persistent_rice_adaptation_enabled_flag = get_bits1(gb);
    1123             : 
    1124          47 :             cabac_bypass_alignment_enabled_flag  = get_bits1(gb);
    1125          47 :             if (cabac_bypass_alignment_enabled_flag)
    1126           0 :                 av_log(avctx, AV_LOG_WARNING,
    1127             :                    "cabac_bypass_alignment_enabled_flag not yet implemented\n");
    1128             :         }
    1129             :     }
    1130         970 :     if (apply_defdispwin) {
    1131         406 :         sps->output_window.left_offset   += sps->vui.def_disp_win.left_offset;
    1132         406 :         sps->output_window.right_offset  += sps->vui.def_disp_win.right_offset;
    1133         406 :         sps->output_window.top_offset    += sps->vui.def_disp_win.top_offset;
    1134         406 :         sps->output_window.bottom_offset += sps->vui.def_disp_win.bottom_offset;
    1135             :     }
    1136             : 
    1137         970 :     ow = &sps->output_window;
    1138        1940 :     if (ow->left_offset >= INT_MAX - ow->right_offset     ||
    1139        1940 :         ow->top_offset  >= INT_MAX - ow->bottom_offset    ||
    1140        1940 :         ow->left_offset + ow->right_offset  >= sps->width ||
    1141         970 :         ow->top_offset  + ow->bottom_offset >= sps->height) {
    1142           0 :         av_log(avctx, AV_LOG_WARNING, "Invalid cropping offsets: %u/%u/%u/%u\n",
    1143             :                ow->left_offset, ow->right_offset, ow->top_offset, ow->bottom_offset);
    1144           0 :         if (avctx->err_recognition & AV_EF_EXPLODE) {
    1145           0 :             return AVERROR_INVALIDDATA;
    1146             :         }
    1147           0 :         av_log(avctx, AV_LOG_WARNING,
    1148             :                "Displaying the whole video surface.\n");
    1149           0 :         memset(ow, 0, sizeof(*ow));
    1150           0 :         memset(&sps->pic_conf_win, 0, sizeof(sps->pic_conf_win));
    1151             :     }
    1152             : 
    1153             :     // Inferred parameters
    1154        1940 :     sps->log2_ctb_size = sps->log2_min_cb_size +
    1155         970 :                          sps->log2_diff_max_min_coding_block_size;
    1156         970 :     sps->log2_min_pu_size = sps->log2_min_cb_size - 1;
    1157             : 
    1158         970 :     if (sps->log2_ctb_size > HEVC_MAX_LOG2_CTB_SIZE) {
    1159           0 :         av_log(avctx, AV_LOG_ERROR, "CTB size out of range: 2^%d\n", sps->log2_ctb_size);
    1160           0 :         return AVERROR_INVALIDDATA;
    1161             :     }
    1162         970 :     if (sps->log2_ctb_size < 4) {
    1163           0 :         av_log(avctx,
    1164             :                AV_LOG_ERROR,
    1165             :                "log2_ctb_size %d differs from the bounds of any known profile\n",
    1166             :                sps->log2_ctb_size);
    1167           0 :         avpriv_request_sample(avctx, "log2_ctb_size %d", sps->log2_ctb_size);
    1168           0 :         return AVERROR_INVALIDDATA;
    1169             :     }
    1170             : 
    1171         970 :     sps->ctb_width  = (sps->width  + (1 << sps->log2_ctb_size) - 1) >> sps->log2_ctb_size;
    1172         970 :     sps->ctb_height = (sps->height + (1 << sps->log2_ctb_size) - 1) >> sps->log2_ctb_size;
    1173         970 :     sps->ctb_size   = sps->ctb_width * sps->ctb_height;
    1174             : 
    1175         970 :     sps->min_cb_width  = sps->width  >> sps->log2_min_cb_size;
    1176         970 :     sps->min_cb_height = sps->height >> sps->log2_min_cb_size;
    1177         970 :     sps->min_tb_width  = sps->width  >> sps->log2_min_tb_size;
    1178         970 :     sps->min_tb_height = sps->height >> sps->log2_min_tb_size;
    1179         970 :     sps->min_pu_width  = sps->width  >> sps->log2_min_pu_size;
    1180         970 :     sps->min_pu_height = sps->height >> sps->log2_min_pu_size;
    1181         970 :     sps->tb_mask       = (1 << (sps->log2_ctb_size - sps->log2_min_tb_size)) - 1;
    1182             : 
    1183         970 :     sps->qp_bd_offset = 6 * (sps->bit_depth - 8);
    1184             : 
    1185        1940 :     if (av_mod_uintp2(sps->width, sps->log2_min_cb_size) ||
    1186         970 :         av_mod_uintp2(sps->height, sps->log2_min_cb_size)) {
    1187           0 :         av_log(avctx, AV_LOG_ERROR, "Invalid coded frame dimensions.\n");
    1188           0 :         return AVERROR_INVALIDDATA;
    1189             :     }
    1190             : 
    1191         970 :     if (sps->max_transform_hierarchy_depth_inter > sps->log2_ctb_size - sps->log2_min_tb_size) {
    1192           0 :         av_log(avctx, AV_LOG_ERROR, "max_transform_hierarchy_depth_inter out of range: %d\n",
    1193             :                sps->max_transform_hierarchy_depth_inter);
    1194           0 :         return AVERROR_INVALIDDATA;
    1195             :     }
    1196         970 :     if (sps->max_transform_hierarchy_depth_intra > sps->log2_ctb_size - sps->log2_min_tb_size) {
    1197           0 :         av_log(avctx, AV_LOG_ERROR, "max_transform_hierarchy_depth_intra out of range: %d\n",
    1198             :                sps->max_transform_hierarchy_depth_intra);
    1199           0 :         return AVERROR_INVALIDDATA;
    1200             :     }
    1201         970 :     if (sps->log2_max_trafo_size > FFMIN(sps->log2_ctb_size, 5)) {
    1202           0 :         av_log(avctx, AV_LOG_ERROR,
    1203             :                "max transform block size out of range: %d\n",
    1204             :                sps->log2_max_trafo_size);
    1205           0 :         return AVERROR_INVALIDDATA;
    1206             :     }
    1207             : 
    1208         970 :     if (get_bits_left(gb) < 0) {
    1209           0 :         av_log(avctx, AV_LOG_ERROR,
    1210           0 :                "Overread SPS by %d bits\n", -get_bits_left(gb));
    1211           0 :         return AVERROR_INVALIDDATA;
    1212             :     }
    1213             : 
    1214         970 :     return 0;
    1215             : }
    1216             : 
    1217         970 : int ff_hevc_decode_nal_sps(GetBitContext *gb, AVCodecContext *avctx,
    1218             :                            HEVCParamSets *ps, int apply_defdispwin)
    1219             : {
    1220             :     HEVCSPS *sps;
    1221         970 :     AVBufferRef *sps_buf = av_buffer_allocz(sizeof(*sps));
    1222             :     unsigned int sps_id;
    1223             :     int ret;
    1224             :     ptrdiff_t nal_size;
    1225             : 
    1226         970 :     if (!sps_buf)
    1227           0 :         return AVERROR(ENOMEM);
    1228         970 :     sps = (HEVCSPS*)sps_buf->data;
    1229             : 
    1230         970 :     av_log(avctx, AV_LOG_DEBUG, "Decoding SPS\n");
    1231             : 
    1232         970 :     nal_size = gb->buffer_end - gb->buffer;
    1233         970 :     if (nal_size > sizeof(sps->data)) {
    1234           0 :         av_log(avctx, AV_LOG_WARNING, "Truncating likely oversized SPS "
    1235             :                "(%"PTRDIFF_SPECIFIER" > %"SIZE_SPECIFIER")\n",
    1236             :                nal_size, sizeof(sps->data));
    1237           0 :         sps->data_size = sizeof(sps->data);
    1238             :     } else {
    1239         970 :         sps->data_size = nal_size;
    1240             :     }
    1241         970 :     memcpy(sps->data, gb->buffer, sps->data_size);
    1242             : 
    1243         970 :     ret = ff_hevc_parse_sps(sps, gb, &sps_id,
    1244             :                             apply_defdispwin,
    1245         970 :                             ps->vps_list, avctx);
    1246         970 :     if (ret < 0) {
    1247           0 :         av_buffer_unref(&sps_buf);
    1248           0 :         return ret;
    1249             :     }
    1250             : 
    1251         970 :     if (avctx->debug & FF_DEBUG_BITSTREAM) {
    1252           0 :         av_log(avctx, AV_LOG_DEBUG,
    1253             :                "Parsed SPS: id %d; coded wxh: %dx%d; "
    1254             :                "cropped wxh: %dx%d; pix_fmt: %s.\n",
    1255             :                sps_id, sps->width, sps->height,
    1256           0 :                sps->width - (sps->output_window.left_offset + sps->output_window.right_offset),
    1257           0 :                sps->height - (sps->output_window.top_offset + sps->output_window.bottom_offset),
    1258             :                av_get_pix_fmt_name(sps->pix_fmt));
    1259             :     }
    1260             : 
    1261             :     /* check if this is a repeat of an already parsed SPS, then keep the
    1262             :      * original one.
    1263             :      * otherwise drop all PPSes that depend on it */
    1264        1384 :     if (ps->sps_list[sps_id] &&
    1265         414 :         !memcmp(ps->sps_list[sps_id]->data, sps_buf->data, sps_buf->size)) {
    1266         406 :         av_buffer_unref(&sps_buf);
    1267             :     } else {
    1268         564 :         remove_sps(ps, sps_id);
    1269         564 :         ps->sps_list[sps_id] = sps_buf;
    1270             :     }
    1271             : 
    1272         970 :     return 0;
    1273             : }
    1274             : 
    1275        3178 : static void hevc_pps_free(void *opaque, uint8_t *data)
    1276             : {
    1277        3178 :     HEVCPPS *pps = (HEVCPPS*)data;
    1278             : 
    1279        3178 :     av_freep(&pps->column_width);
    1280        3178 :     av_freep(&pps->row_height);
    1281        3178 :     av_freep(&pps->col_bd);
    1282        3178 :     av_freep(&pps->row_bd);
    1283        3178 :     av_freep(&pps->col_idxX);
    1284        3178 :     av_freep(&pps->ctb_addr_rs_to_ts);
    1285        3178 :     av_freep(&pps->ctb_addr_ts_to_rs);
    1286        3178 :     av_freep(&pps->tile_pos_rs);
    1287        3178 :     av_freep(&pps->tile_id);
    1288        3178 :     av_freep(&pps->min_tb_addr_zs_tab);
    1289             : 
    1290        3178 :     av_freep(&pps);
    1291        3178 : }
    1292             : 
    1293          72 : static int pps_range_extensions(GetBitContext *gb, AVCodecContext *avctx,
    1294             :                                 HEVCPPS *pps, HEVCSPS *sps) {
    1295             :     int i;
    1296             : 
    1297          72 :     if (pps->transform_skip_enabled_flag) {
    1298          72 :         pps->log2_max_transform_skip_block_size = get_ue_golomb_long(gb) + 2;
    1299             :     }
    1300          72 :     pps->cross_component_prediction_enabled_flag = get_bits1(gb);
    1301          72 :     pps->chroma_qp_offset_list_enabled_flag = get_bits1(gb);
    1302          72 :     if (pps->chroma_qp_offset_list_enabled_flag) {
    1303          10 :         pps->diff_cu_chroma_qp_offset_depth = get_ue_golomb_long(gb);
    1304          10 :         pps->chroma_qp_offset_list_len_minus1 = get_ue_golomb_long(gb);
    1305          10 :         if (pps->chroma_qp_offset_list_len_minus1 > 5) {
    1306           0 :             av_log(avctx, AV_LOG_ERROR,
    1307             :                    "chroma_qp_offset_list_len_minus1 shall be in the range [0, 5].\n");
    1308           0 :             return AVERROR_INVALIDDATA;
    1309             :         }
    1310          20 :         for (i = 0; i <= pps->chroma_qp_offset_list_len_minus1; i++) {
    1311          10 :             pps->cb_qp_offset_list[i] = get_se_golomb_long(gb);
    1312          10 :             if (pps->cb_qp_offset_list[i]) {
    1313          10 :                 av_log(avctx, AV_LOG_WARNING,
    1314             :                        "cb_qp_offset_list not tested yet.\n");
    1315             :             }
    1316          10 :             pps->cr_qp_offset_list[i] = get_se_golomb_long(gb);
    1317          10 :             if (pps->cr_qp_offset_list[i]) {
    1318          10 :                 av_log(avctx, AV_LOG_WARNING,
    1319             :                        "cb_qp_offset_list not tested yet.\n");
    1320             :             }
    1321             :         }
    1322             :     }
    1323          72 :     pps->log2_sao_offset_scale_luma = get_ue_golomb_long(gb);
    1324          72 :     pps->log2_sao_offset_scale_chroma = get_ue_golomb_long(gb);
    1325             : 
    1326          72 :     if (   pps->log2_sao_offset_scale_luma   > FFMAX(sps->bit_depth        - 10, 0)
    1327          72 :         || pps->log2_sao_offset_scale_chroma > FFMAX(sps->bit_depth_chroma - 10, 0)
    1328             :     )
    1329           0 :         return AVERROR_INVALIDDATA;
    1330             : 
    1331          72 :     return(0);
    1332             : }
    1333             : 
    1334        3174 : static inline int setup_pps(AVCodecContext *avctx, GetBitContext *gb,
    1335             :                             HEVCPPS *pps, HEVCSPS *sps)
    1336             : {
    1337             :     int log2_diff;
    1338             :     int pic_area_in_ctbs;
    1339             :     int i, j, x, y, ctb_addr_rs, tile_id;
    1340             : 
    1341             :     // Inferred parameters
    1342        3174 :     pps->col_bd   = av_malloc_array(pps->num_tile_columns + 1, sizeof(*pps->col_bd));
    1343        3174 :     pps->row_bd   = av_malloc_array(pps->num_tile_rows + 1,    sizeof(*pps->row_bd));
    1344        3174 :     pps->col_idxX = av_malloc_array(sps->ctb_width,    sizeof(*pps->col_idxX));
    1345        3174 :     if (!pps->col_bd || !pps->row_bd || !pps->col_idxX)
    1346           0 :         return AVERROR(ENOMEM);
    1347             : 
    1348        3174 :     if (pps->uniform_spacing_flag) {
    1349        2646 :         if (!pps->column_width) {
    1350        2275 :             pps->column_width = av_malloc_array(pps->num_tile_columns, sizeof(*pps->column_width));
    1351        2275 :             pps->row_height   = av_malloc_array(pps->num_tile_rows,    sizeof(*pps->row_height));
    1352             :         }
    1353        2646 :         if (!pps->column_width || !pps->row_height)
    1354           0 :             return AVERROR(ENOMEM);
    1355             : 
    1356        5544 :         for (i = 0; i < pps->num_tile_columns; i++) {
    1357        5796 :             pps->column_width[i] = ((i + 1) * sps->ctb_width) / pps->num_tile_columns -
    1358        2898 :                                    (i * sps->ctb_width) / pps->num_tile_columns;
    1359             :         }
    1360             : 
    1361        5575 :         for (i = 0; i < pps->num_tile_rows; i++) {
    1362        5858 :             pps->row_height[i] = ((i + 1) * sps->ctb_height) / pps->num_tile_rows -
    1363        2929 :                                  (i * sps->ctb_height) / pps->num_tile_rows;
    1364             :         }
    1365             :     }
    1366             : 
    1367        3174 :     pps->col_bd[0] = 0;
    1368        8672 :     for (i = 0; i < pps->num_tile_columns; i++)
    1369        5498 :         pps->col_bd[i + 1] = pps->col_bd[i] + pps->column_width[i];
    1370             : 
    1371        3174 :     pps->row_bd[0] = 0;
    1372        8693 :     for (i = 0; i < pps->num_tile_rows; i++)
    1373        5519 :         pps->row_bd[i + 1] = pps->row_bd[i] + pps->row_height[i];
    1374             : 
    1375       56978 :     for (i = 0, j = 0; i < sps->ctb_width; i++) {
    1376       53804 :         if (i > pps->col_bd[j])
    1377        5488 :             j++;
    1378       53804 :         pps->col_idxX[i] = j;
    1379             :     }
    1380             : 
    1381             :     /**
    1382             :      * 6.5
    1383             :      */
    1384        3174 :     pic_area_in_ctbs     = sps->ctb_width    * sps->ctb_height;
    1385             : 
    1386        3174 :     pps->ctb_addr_rs_to_ts = av_malloc_array(pic_area_in_ctbs,    sizeof(*pps->ctb_addr_rs_to_ts));
    1387        3174 :     pps->ctb_addr_ts_to_rs = av_malloc_array(pic_area_in_ctbs,    sizeof(*pps->ctb_addr_ts_to_rs));
    1388        3174 :     pps->tile_id           = av_malloc_array(pic_area_in_ctbs,    sizeof(*pps->tile_id));
    1389        3174 :     pps->min_tb_addr_zs_tab = av_malloc_array((sps->tb_mask+2) * (sps->tb_mask+2), sizeof(*pps->min_tb_addr_zs_tab));
    1390        6348 :     if (!pps->ctb_addr_rs_to_ts || !pps->ctb_addr_ts_to_rs ||
    1391        6348 :         !pps->tile_id || !pps->min_tb_addr_zs_tab) {
    1392           0 :         return AVERROR(ENOMEM);
    1393             :     }
    1394             : 
    1395      717747 :     for (ctb_addr_rs = 0; ctb_addr_rs < pic_area_in_ctbs; ctb_addr_rs++) {
    1396      714573 :         int tb_x   = ctb_addr_rs % sps->ctb_width;
    1397      714573 :         int tb_y   = ctb_addr_rs / sps->ctb_width;
    1398      714573 :         int tile_x = 0;
    1399      714573 :         int tile_y = 0;
    1400      714573 :         int val    = 0;
    1401             : 
    1402     1222635 :         for (i = 0; i < pps->num_tile_columns; i++) {
    1403     1222635 :             if (tb_x < pps->col_bd[i + 1]) {
    1404      714573 :                 tile_x = i;
    1405      714573 :                 break;
    1406             :             }
    1407             :         }
    1408             : 
    1409     1072839 :         for (i = 0; i < pps->num_tile_rows; i++) {
    1410     1072839 :             if (tb_y < pps->row_bd[i + 1]) {
    1411      714573 :                 tile_y = i;
    1412      714573 :                 break;
    1413             :             }
    1414             :         }
    1415             : 
    1416     1222635 :         for (i = 0; i < tile_x; i++)
    1417      508062 :             val += pps->row_height[tile_y] * pps->column_width[i];
    1418     1072839 :         for (i = 0; i < tile_y; i++)
    1419      358266 :             val += sps->ctb_width * pps->row_height[i];
    1420             : 
    1421     1429146 :         val += (tb_y - pps->row_bd[tile_y]) * pps->column_width[tile_x] +
    1422      714573 :                tb_x - pps->col_bd[tile_x];
    1423             : 
    1424      714573 :         pps->ctb_addr_rs_to_ts[ctb_addr_rs] = val;
    1425      714573 :         pps->ctb_addr_ts_to_rs[val]         = ctb_addr_rs;
    1426             :     }
    1427             : 
    1428        8693 :     for (j = 0, tile_id = 0; j < pps->num_tile_rows; j++)
    1429       21618 :         for (i = 0; i < pps->num_tile_columns; i++, tile_id++)
    1430       91895 :             for (y = pps->row_bd[j]; y < pps->row_bd[j + 1]; y++)
    1431      790369 :                 for (x = pps->col_bd[i]; x < pps->col_bd[i + 1]; x++)
    1432      714573 :                     pps->tile_id[pps->ctb_addr_rs_to_ts[y * sps->ctb_width + x]] = tile_id;
    1433             : 
    1434        3174 :     pps->tile_pos_rs = av_malloc_array(tile_id, sizeof(*pps->tile_pos_rs));
    1435        3174 :     if (!pps->tile_pos_rs)
    1436           0 :         return AVERROR(ENOMEM);
    1437             : 
    1438        8693 :     for (j = 0; j < pps->num_tile_rows; j++)
    1439       21618 :         for (i = 0; i < pps->num_tile_columns; i++)
    1440       32198 :             pps->tile_pos_rs[j * pps->num_tile_columns + i] =
    1441       32198 :                 pps->row_bd[j] * sps->ctb_width + pps->col_bd[i];
    1442             : 
    1443        3174 :     log2_diff = sps->log2_ctb_size - sps->log2_min_tb_size;
    1444        3174 :     pps->min_tb_addr_zs = &pps->min_tb_addr_zs_tab[1*(sps->tb_mask+2)+1];
    1445       56072 :     for (y = 0; y < sps->tb_mask+2; y++) {
    1446       52898 :         pps->min_tb_addr_zs_tab[y*(sps->tb_mask+2)] = -1;
    1447       52898 :         pps->min_tb_addr_zs_tab[y]    = -1;
    1448             :     }
    1449       52898 :     for (y = 0; y < sps->tb_mask+1; y++) {
    1450      838892 :         for (x = 0; x < sps->tb_mask+1; x++) {
    1451      789168 :             int tb_x = x >> log2_diff;
    1452      789168 :             int tb_y = y >> log2_diff;
    1453      789168 :             int rs   = sps->ctb_width * tb_y + tb_x;
    1454      789168 :             int val  = pps->ctb_addr_rs_to_ts[rs] << (log2_diff * 2);
    1455     3940112 :             for (i = 0; i < log2_diff; i++) {
    1456     3150944 :                 int m = 1 << i;
    1457     3150944 :                 val += (m & x ? m * m : 0) + (m & y ? 2 * m * m : 0);
    1458             :             }
    1459      789168 :             pps->min_tb_addr_zs[y * (sps->tb_mask+2) + x] = val;
    1460             :         }
    1461             :     }
    1462             : 
    1463        3174 :     return 0;
    1464             : }
    1465             : 
    1466        3178 : int ff_hevc_decode_nal_pps(GetBitContext *gb, AVCodecContext *avctx,
    1467             :                            HEVCParamSets *ps)
    1468             : {
    1469        3178 :     HEVCSPS      *sps = NULL;
    1470        3178 :     int i, ret = 0;
    1471        3178 :     unsigned int pps_id = 0;
    1472             :     ptrdiff_t nal_size;
    1473             :     unsigned log2_parallel_merge_level_minus2;
    1474             : 
    1475             :     AVBufferRef *pps_buf;
    1476        3178 :     HEVCPPS *pps = av_mallocz(sizeof(*pps));
    1477             : 
    1478        3178 :     if (!pps)
    1479           0 :         return AVERROR(ENOMEM);
    1480             : 
    1481        3178 :     pps_buf = av_buffer_create((uint8_t *)pps, sizeof(*pps),
    1482             :                                hevc_pps_free, NULL, 0);
    1483        3178 :     if (!pps_buf) {
    1484           0 :         av_freep(&pps);
    1485           0 :         return AVERROR(ENOMEM);
    1486             :     }
    1487             : 
    1488        3178 :     av_log(avctx, AV_LOG_DEBUG, "Decoding PPS\n");
    1489             : 
    1490        3178 :     nal_size = gb->buffer_end - gb->buffer;
    1491        3178 :     if (nal_size > sizeof(pps->data)) {
    1492           0 :         av_log(avctx, AV_LOG_WARNING, "Truncating likely oversized PPS "
    1493             :                "(%"PTRDIFF_SPECIFIER" > %"SIZE_SPECIFIER")\n",
    1494             :                nal_size, sizeof(pps->data));
    1495           0 :         pps->data_size = sizeof(pps->data);
    1496             :     } else {
    1497        3178 :         pps->data_size = nal_size;
    1498             :     }
    1499        3178 :     memcpy(pps->data, gb->buffer, pps->data_size);
    1500             : 
    1501             :     // Default values
    1502        3178 :     pps->loop_filter_across_tiles_enabled_flag = 1;
    1503        3178 :     pps->num_tile_columns                      = 1;
    1504        3178 :     pps->num_tile_rows                         = 1;
    1505        3178 :     pps->uniform_spacing_flag                  = 1;
    1506        3178 :     pps->disable_dbf                           = 0;
    1507        3178 :     pps->beta_offset                           = 0;
    1508        3178 :     pps->tc_offset                             = 0;
    1509        3178 :     pps->log2_max_transform_skip_block_size    = 2;
    1510             : 
    1511             :     // Coded parameters
    1512        3178 :     pps_id = get_ue_golomb_long(gb);
    1513        3178 :     if (pps_id >= HEVC_MAX_PPS_COUNT) {
    1514           0 :         av_log(avctx, AV_LOG_ERROR, "PPS id out of range: %d\n", pps_id);
    1515           0 :         ret = AVERROR_INVALIDDATA;
    1516           0 :         goto err;
    1517             :     }
    1518        3178 :     pps->sps_id = get_ue_golomb_long(gb);
    1519        3178 :     if (pps->sps_id >= HEVC_MAX_SPS_COUNT) {
    1520           0 :         av_log(avctx, AV_LOG_ERROR, "SPS id out of range: %d\n", pps->sps_id);
    1521           0 :         ret = AVERROR_INVALIDDATA;
    1522           0 :         goto err;
    1523             :     }
    1524        3178 :     if (!ps->sps_list[pps->sps_id]) {
    1525           4 :         av_log(avctx, AV_LOG_ERROR, "SPS %u does not exist.\n", pps->sps_id);
    1526           4 :         ret = AVERROR_INVALIDDATA;
    1527           4 :         goto err;
    1528             :     }
    1529        3174 :     sps = (HEVCSPS *)ps->sps_list[pps->sps_id]->data;
    1530             : 
    1531        3174 :     pps->dependent_slice_segments_enabled_flag = get_bits1(gb);
    1532        3174 :     pps->output_flag_present_flag              = get_bits1(gb);
    1533        3174 :     pps->num_extra_slice_header_bits           = get_bits(gb, 3);
    1534             : 
    1535        3174 :     pps->sign_data_hiding_flag = get_bits1(gb);
    1536             : 
    1537        3174 :     pps->cabac_init_present_flag = get_bits1(gb);
    1538             : 
    1539        3174 :     pps->num_ref_idx_l0_default_active = get_ue_golomb_long(gb) + 1;
    1540        3174 :     pps->num_ref_idx_l1_default_active = get_ue_golomb_long(gb) + 1;
    1541             : 
    1542        3174 :     pps->pic_init_qp_minus26 = get_se_golomb(gb);
    1543             : 
    1544        3174 :     pps->constrained_intra_pred_flag = get_bits1(gb);
    1545        3174 :     pps->transform_skip_enabled_flag = get_bits1(gb);
    1546             : 
    1547        3174 :     pps->cu_qp_delta_enabled_flag = get_bits1(gb);
    1548        3174 :     pps->diff_cu_qp_delta_depth   = 0;
    1549        3174 :     if (pps->cu_qp_delta_enabled_flag)
    1550         467 :         pps->diff_cu_qp_delta_depth = get_ue_golomb_long(gb);
    1551             : 
    1552        6348 :     if (pps->diff_cu_qp_delta_depth < 0 ||
    1553        3174 :         pps->diff_cu_qp_delta_depth > sps->log2_diff_max_min_coding_block_size) {
    1554           0 :         av_log(avctx, AV_LOG_ERROR, "diff_cu_qp_delta_depth %d is invalid\n",
    1555           0 :                pps->diff_cu_qp_delta_depth);
    1556           0 :         ret = AVERROR_INVALIDDATA;
    1557           0 :         goto err;
    1558             :     }
    1559             : 
    1560        3174 :     pps->cb_qp_offset = get_se_golomb(gb);
    1561        3174 :     if (pps->cb_qp_offset < -12 || pps->cb_qp_offset > 12) {
    1562           0 :         av_log(avctx, AV_LOG_ERROR, "pps_cb_qp_offset out of range: %d\n",
    1563           0 :                pps->cb_qp_offset);
    1564           0 :         ret = AVERROR_INVALIDDATA;
    1565           0 :         goto err;
    1566             :     }
    1567        3174 :     pps->cr_qp_offset = get_se_golomb(gb);
    1568        3174 :     if (pps->cr_qp_offset < -12 || pps->cr_qp_offset > 12) {
    1569           0 :         av_log(avctx, AV_LOG_ERROR, "pps_cr_qp_offset out of range: %d\n",
    1570           0 :                pps->cr_qp_offset);
    1571           0 :         ret = AVERROR_INVALIDDATA;
    1572           0 :         goto err;
    1573             :     }
    1574        3174 :     pps->pic_slice_level_chroma_qp_offsets_present_flag = get_bits1(gb);
    1575             : 
    1576        3174 :     pps->weighted_pred_flag   = get_bits1(gb);
    1577        3174 :     pps->weighted_bipred_flag = get_bits1(gb);
    1578             : 
    1579        3174 :     pps->transquant_bypass_enable_flag    = get_bits1(gb);
    1580        3174 :     pps->tiles_enabled_flag               = get_bits1(gb);
    1581        3174 :     pps->entropy_coding_sync_enabled_flag = get_bits1(gb);
    1582             : 
    1583        3174 :     if (pps->tiles_enabled_flag) {
    1584         899 :         pps->num_tile_columns = get_ue_golomb_long(gb) + 1;
    1585         899 :         pps->num_tile_rows    = get_ue_golomb_long(gb) + 1;
    1586        1798 :         if (pps->num_tile_columns <= 0 ||
    1587         899 :             pps->num_tile_columns >= sps->width) {
    1588           0 :             av_log(avctx, AV_LOG_ERROR, "num_tile_columns_minus1 out of range: %d\n",
    1589           0 :                    pps->num_tile_columns - 1);
    1590           0 :             ret = AVERROR_INVALIDDATA;
    1591           0 :             goto err;
    1592             :         }
    1593        1798 :         if (pps->num_tile_rows <= 0 ||
    1594         899 :             pps->num_tile_rows >= sps->height) {
    1595           0 :             av_log(avctx, AV_LOG_ERROR, "num_tile_rows_minus1 out of range: %d\n",
    1596           0 :                    pps->num_tile_rows - 1);
    1597           0 :             ret = AVERROR_INVALIDDATA;
    1598           0 :             goto err;
    1599             :         }
    1600             : 
    1601         899 :         pps->column_width = av_malloc_array(pps->num_tile_columns, sizeof(*pps->column_width));
    1602         899 :         pps->row_height   = av_malloc_array(pps->num_tile_rows,    sizeof(*pps->row_height));
    1603         899 :         if (!pps->column_width || !pps->row_height) {
    1604           0 :             ret = AVERROR(ENOMEM);
    1605           0 :             goto err;
    1606             :         }
    1607             : 
    1608         899 :         pps->uniform_spacing_flag = get_bits1(gb);
    1609         899 :         if (!pps->uniform_spacing_flag) {
    1610         528 :             uint64_t sum = 0;
    1611        2600 :             for (i = 0; i < pps->num_tile_columns - 1; i++) {
    1612        2072 :                 pps->column_width[i] = get_ue_golomb_long(gb) + 1;
    1613        2072 :                 sum                 += pps->column_width[i];
    1614             :             }
    1615         528 :             if (sum >= sps->ctb_width) {
    1616           0 :                 av_log(avctx, AV_LOG_ERROR, "Invalid tile widths.\n");
    1617           0 :                 ret = AVERROR_INVALIDDATA;
    1618           0 :                 goto err;
    1619             :             }
    1620         528 :             pps->column_width[pps->num_tile_columns - 1] = sps->ctb_width - sum;
    1621             : 
    1622         528 :             sum = 0;
    1623        2590 :             for (i = 0; i < pps->num_tile_rows - 1; i++) {
    1624        2062 :                 pps->row_height[i] = get_ue_golomb_long(gb) + 1;
    1625        2062 :                 sum               += pps->row_height[i];
    1626             :             }
    1627         528 :             if (sum >= sps->ctb_height) {
    1628           0 :                 av_log(avctx, AV_LOG_ERROR, "Invalid tile heights.\n");
    1629           0 :                 ret = AVERROR_INVALIDDATA;
    1630           0 :                 goto err;
    1631             :             }
    1632         528 :             pps->row_height[pps->num_tile_rows - 1] = sps->ctb_height - sum;
    1633             :         }
    1634         899 :         pps->loop_filter_across_tiles_enabled_flag = get_bits1(gb);
    1635             :     }
    1636             : 
    1637        3174 :     pps->seq_loop_filter_across_slices_enabled_flag = get_bits1(gb);
    1638             : 
    1639        3174 :     pps->deblocking_filter_control_present_flag = get_bits1(gb);
    1640        3174 :     if (pps->deblocking_filter_control_present_flag) {
    1641         989 :         pps->deblocking_filter_override_enabled_flag = get_bits1(gb);
    1642         989 :         pps->disable_dbf                             = get_bits1(gb);
    1643         989 :         if (!pps->disable_dbf) {
    1644         852 :             int beta_offset_div2 = get_se_golomb(gb);
    1645         852 :             int tc_offset_div2   = get_se_golomb(gb) ;
    1646         852 :             if (beta_offset_div2 < -6 || beta_offset_div2 > 6) {
    1647           0 :                 av_log(avctx, AV_LOG_ERROR, "pps_beta_offset_div2 out of range: %d\n",
    1648             :                        beta_offset_div2);
    1649           0 :                 ret = AVERROR_INVALIDDATA;
    1650           0 :                 goto err;
    1651             :             }
    1652         852 :             if (tc_offset_div2 < -6 || tc_offset_div2 > 6) {
    1653           0 :                 av_log(avctx, AV_LOG_ERROR, "pps_tc_offset_div2 out of range: %d\n",
    1654             :                        tc_offset_div2);
    1655           0 :                 ret = AVERROR_INVALIDDATA;
    1656           0 :                 goto err;
    1657             :             }
    1658         852 :             pps->beta_offset = 2 * beta_offset_div2;
    1659         852 :             pps->tc_offset   = 2 *   tc_offset_div2;
    1660             :         }
    1661             :     }
    1662             : 
    1663        3174 :     pps->scaling_list_data_present_flag = get_bits1(gb);
    1664        3174 :     if (pps->scaling_list_data_present_flag) {
    1665         463 :         set_default_scaling_list_data(&pps->scaling_list);
    1666         463 :         ret = scaling_list_data(gb, avctx, &pps->scaling_list, sps);
    1667         463 :         if (ret < 0)
    1668           0 :             goto err;
    1669             :     }
    1670        3174 :     pps->lists_modification_present_flag = get_bits1(gb);
    1671        3174 :     log2_parallel_merge_level_minus2     = get_ue_golomb_long(gb);
    1672        3174 :     if (log2_parallel_merge_level_minus2 > sps->log2_ctb_size) {
    1673           0 :         av_log(avctx, AV_LOG_ERROR, "log2_parallel_merge_level_minus2 out of range: %d\n",
    1674             :                log2_parallel_merge_level_minus2);
    1675           0 :         ret = AVERROR_INVALIDDATA;
    1676           0 :         goto err;
    1677             :     }
    1678        3174 :     pps->log2_parallel_merge_level       = log2_parallel_merge_level_minus2 + 2;
    1679             : 
    1680        3174 :     pps->slice_header_extension_present_flag = get_bits1(gb);
    1681             : 
    1682        3174 :     if (get_bits1(gb)) { // pps_extension_present_flag
    1683          87 :         int pps_range_extensions_flag = get_bits1(gb);
    1684          87 :         /* int pps_extension_7bits = */ get_bits(gb, 7);
    1685          87 :         if (sps->ptl.general_ptl.profile_idc == FF_PROFILE_HEVC_REXT && pps_range_extensions_flag) {
    1686          72 :             if ((ret = pps_range_extensions(gb, avctx, pps, sps)) < 0)
    1687           0 :                 goto err;
    1688             :         }
    1689             :     }
    1690             : 
    1691        3174 :     ret = setup_pps(avctx, gb, pps, sps);
    1692        3174 :     if (ret < 0)
    1693           0 :         goto err;
    1694             : 
    1695        3174 :     if (get_bits_left(gb) < 0) {
    1696           0 :         av_log(avctx, AV_LOG_ERROR,
    1697           0 :                "Overread PPS by %d bits\n", -get_bits_left(gb));
    1698           0 :         goto err;
    1699             :     }
    1700             : 
    1701        3174 :     remove_pps(ps, pps_id);
    1702        3174 :     ps->pps_list[pps_id] = pps_buf;
    1703             : 
    1704        3174 :     return 0;
    1705             : 
    1706           4 : err:
    1707           4 :     av_buffer_unref(&pps_buf);
    1708           4 :     return ret;
    1709             : }
    1710             : 
    1711         549 : void ff_hevc_ps_uninit(HEVCParamSets *ps)
    1712             : {
    1713             :     int i;
    1714             : 
    1715        9333 :     for (i = 0; i < FF_ARRAY_ELEMS(ps->vps_list); i++)
    1716        8784 :         av_buffer_unref(&ps->vps_list[i]);
    1717        9333 :     for (i = 0; i < FF_ARRAY_ELEMS(ps->sps_list); i++)
    1718        8784 :         av_buffer_unref(&ps->sps_list[i]);
    1719       35685 :     for (i = 0; i < FF_ARRAY_ELEMS(ps->pps_list); i++)
    1720       35136 :         av_buffer_unref(&ps->pps_list[i]);
    1721             : 
    1722         549 :     ps->sps = NULL;
    1723         549 :     ps->pps = NULL;
    1724         549 :     ps->vps = NULL;
    1725         549 : }
    1726             : 
    1727       30131 : int ff_hevc_compute_poc(const HEVCSPS *sps, int pocTid0, int poc_lsb, int nal_unit_type)
    1728             : {
    1729       30131 :     int max_poc_lsb  = 1 << sps->log2_max_poc_lsb;
    1730       30131 :     int prev_poc_lsb = pocTid0 % max_poc_lsb;
    1731       30131 :     int prev_poc_msb = pocTid0 - prev_poc_lsb;
    1732             :     int poc_msb;
    1733             : 
    1734       30131 :     if (poc_lsb < prev_poc_lsb && prev_poc_lsb - poc_lsb >= max_poc_lsb / 2)
    1735          40 :         poc_msb = prev_poc_msb + max_poc_lsb;
    1736       30091 :     else if (poc_lsb > prev_poc_lsb && poc_lsb - prev_poc_lsb > max_poc_lsb / 2)
    1737         252 :         poc_msb = prev_poc_msb - max_poc_lsb;
    1738             :     else
    1739       29839 :         poc_msb = prev_poc_msb;
    1740             : 
    1741             :     // For BLA picture types, POCmsb is set to 0.
    1742       30131 :     if (nal_unit_type == HEVC_NAL_BLA_W_LP   ||
    1743       30110 :         nal_unit_type == HEVC_NAL_BLA_W_RADL ||
    1744             :         nal_unit_type == HEVC_NAL_BLA_N_LP)
    1745          24 :         poc_msb = 0;
    1746             : 
    1747       30131 :     return poc_msb + poc_lsb;
    1748             : }

Generated by: LCOV version 1.13