hevc_ps.c 63 KB
Newer Older
1
/*
Guillaume Martres's avatar
Guillaume Martres committed
2
 * HEVC Parameter Set decoding
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
 *
 * Copyright (C) 2012 - 2103 Guillaume Martres
 * Copyright (C) 2012 - 2103 Mickael Raulet
 * Copyright (C) 2012 - 2013 Gildas Cocherel
 * Copyright (C) 2013 Vittorio Giovara
 *
 * This file is part of FFmpeg.
 *
 * FFmpeg is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * FFmpeg is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with FFmpeg; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

#include "libavutil/imgutils.h"
Guillaume Martres's avatar
Guillaume Martres committed
27
#include "golomb.h"
28 29 30 31 32 33 34 35 36 37
#include "hevc.h"

static const uint8_t default_scaling_list_intra[] = {
    16, 16, 16, 16, 17, 18, 21, 24,
    16, 16, 16, 16, 17, 19, 22, 25,
    16, 16, 17, 18, 20, 22, 25, 29,
    16, 16, 18, 21, 24, 27, 31, 36,
    17, 17, 20, 24, 30, 35, 41, 47,
    18, 19, 22, 27, 35, 44, 54, 65,
    21, 22, 25, 31, 41, 54, 70, 88,
Guillaume Martres's avatar
Guillaume Martres committed
38
    24, 25, 29, 36, 47, 65, 88, 115
39 40 41 42 43 44 45 46 47 48 49 50 51 52
};

static const uint8_t default_scaling_list_inter[] = {
    16, 16, 16, 16, 17, 18, 20, 24,
    16, 16, 16, 17, 18, 20, 24, 25,
    16, 16, 17, 18, 20, 24, 25, 28,
    16, 17, 18, 20, 24, 25, 28, 33,
    17, 18, 20, 24, 25, 28, 33, 41,
    18, 20, 24, 25, 28, 33, 41, 54,
    20, 24, 25, 28, 33, 41, 54, 71,
    24, 25, 28, 33, 41, 54, 71, 91
};

static const AVRational vui_sar[] = {
Guillaume Martres's avatar
Guillaume Martres committed
53 54
    {  0,   1 },
    {  1,   1 },
55 56 57 58 59 60 61 62 63 64 65 66
    { 12,  11 },
    { 10,  11 },
    { 16,  11 },
    { 40,  33 },
    { 24,  11 },
    { 20,  11 },
    { 32,  11 },
    { 80,  33 },
    { 18,  11 },
    { 15,  11 },
    { 64,  33 },
    { 160, 99 },
Guillaume Martres's avatar
Guillaume Martres committed
67 68 69
    {  4,   3 },
    {  3,   2 },
    {  2,   1 },
70 71
};

72
static void remove_pps(HEVCParamSets *s, int id)
73 74 75 76 77 78
{
    if (s->pps_list[id] && s->pps == (const HEVCPPS*)s->pps_list[id]->data)
        s->pps = NULL;
    av_buffer_unref(&s->pps_list[id]);
}

79
static void remove_sps(HEVCParamSets *s, int id)
80 81 82 83 84 85 86 87 88 89
{
    int i;
    if (s->sps_list[id]) {
        if (s->sps == (const HEVCSPS*)s->sps_list[id]->data)
            s->sps = NULL;

        /* drop all PPS that depend on this SPS */
        for (i = 0; i < FF_ARRAY_ELEMS(s->pps_list); i++)
            if (s->pps_list[i] && ((HEVCPPS*)s->pps_list[i]->data)->sps_id == id)
                remove_pps(s, i);
90

91
        av_assert0(!(s->sps_list[id] && s->sps == (HEVCSPS*)s->sps_list[id]->data));
92 93 94 95
    }
    av_buffer_unref(&s->sps_list[id]);
}

96
static void remove_vps(HEVCParamSets *s, int id)
97 98 99 100 101 102 103 104 105 106 107 108 109
{
    int i;
    if (s->vps_list[id]) {
        if (s->vps == (const HEVCVPS*)s->vps_list[id]->data)
            s->vps = NULL;

        for (i = 0; i < FF_ARRAY_ELEMS(s->sps_list); i++)
            if (s->sps_list[i] && ((HEVCSPS*)s->sps_list[i]->data)->vps_id == id)
                remove_sps(s, i);
    }
    av_buffer_unref(&s->vps_list[id]);
}

110 111
int ff_hevc_decode_short_term_rps(GetBitContext *gb, AVCodecContext *avctx,
                                  ShortTermRPS *rps, const HEVCSPS *sps, int is_slice_header)
112 113 114 115 116 117 118 119 120
{
    uint8_t rps_predict = 0;
    int delta_poc;
    int k0 = 0;
    int k1 = 0;
    int k  = 0;
    int i;

    if (rps != sps->st_rps && sps->nb_st_rps)
Guillaume Martres's avatar
Guillaume Martres committed
121
        rps_predict = get_bits1(gb);
122 123 124

    if (rps_predict) {
        const ShortTermRPS *rps_ridx;
125 126
        int delta_rps;
        unsigned abs_delta_rps;
127 128 129 130
        uint8_t use_delta_flag = 0;
        uint8_t delta_rps_sign;

        if (is_slice_header) {
131
            unsigned int delta_idx = get_ue_golomb_long(gb) + 1;
132
            if (delta_idx > sps->nb_st_rps) {
133
                av_log(avctx, AV_LOG_ERROR,
Guillaume Martres's avatar
Guillaume Martres committed
134 135
                       "Invalid value of delta_idx in slice header RPS: %d > %d.\n",
                       delta_idx, sps->nb_st_rps);
136 137 138
                return AVERROR_INVALIDDATA;
            }
            rps_ridx = &sps->st_rps[sps->nb_st_rps - delta_idx];
139
            rps->rps_idx_num_delta_pocs = rps_ridx->num_delta_pocs;
140 141 142 143
        } else
            rps_ridx = &sps->st_rps[rps - sps->st_rps - 1];

        delta_rps_sign = get_bits1(gb);
144
        abs_delta_rps  = get_ue_golomb_long(gb) + 1;
145
        if (abs_delta_rps < 1 || abs_delta_rps > 32768) {
146
            av_log(avctx, AV_LOG_ERROR,
147 148 149 150
                   "Invalid value of abs_delta_rps: %d\n",
                   abs_delta_rps);
            return AVERROR_INVALIDDATA;
        }
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
        delta_rps      = (1 - (delta_rps_sign << 1)) * abs_delta_rps;
        for (i = 0; i <= rps_ridx->num_delta_pocs; i++) {
            int used = rps->used[k] = get_bits1(gb);

            if (!used)
                use_delta_flag = get_bits1(gb);

            if (used || use_delta_flag) {
                if (i < rps_ridx->num_delta_pocs)
                    delta_poc = delta_rps + rps_ridx->delta_poc[i];
                else
                    delta_poc = delta_rps;
                rps->delta_poc[k] = delta_poc;
                if (delta_poc < 0)
                    k0++;
                else
                    k1++;
                k++;
            }
        }

        rps->num_delta_pocs    = k;
        rps->num_negative_pics = k0;
        // sort in increasing order (smallest first)
        if (rps->num_delta_pocs != 0) {
            int used, tmp;
            for (i = 1; i < rps->num_delta_pocs; i++) {
                delta_poc = rps->delta_poc[i];
                used      = rps->used[i];
Guillaume Martres's avatar
Guillaume Martres committed
180
                for (k = i - 1; k >= 0; k--) {
181
                    tmp = rps->delta_poc[k];
Guillaume Martres's avatar
Guillaume Martres committed
182 183 184 185 186
                    if (delta_poc < tmp) {
                        rps->delta_poc[k + 1] = tmp;
                        rps->used[k + 1]      = rps->used[k];
                        rps->delta_poc[k]     = delta_poc;
                        rps->used[k]          = used;
187 188 189 190 191 192 193 194
                    }
                }
            }
        }
        if ((rps->num_negative_pics >> 1) != 0) {
            int used;
            k = rps->num_negative_pics - 1;
            // flip the negative values to largest first
Guillaume Martres's avatar
Guillaume Martres committed
195 196 197 198 199 200 201
            for (i = 0; i < rps->num_negative_pics >> 1; i++) {
                delta_poc         = rps->delta_poc[i];
                used              = rps->used[i];
                rps->delta_poc[i] = rps->delta_poc[k];
                rps->used[i]      = rps->used[k];
                rps->delta_poc[k] = delta_poc;
                rps->used[k]      = used;
202 203 204 205 206
                k--;
            }
        }
    } else {
        unsigned int prev, nb_positive_pics;
207 208
        rps->num_negative_pics = get_ue_golomb_long(gb);
        nb_positive_pics       = get_ue_golomb_long(gb);
209 210 211

        if (rps->num_negative_pics >= MAX_REFS ||
            nb_positive_pics >= MAX_REFS) {
212
            av_log(avctx, AV_LOG_ERROR, "Too many refs in a short term RPS.\n");
213 214 215 216 217 218 219
            return AVERROR_INVALIDDATA;
        }

        rps->num_delta_pocs = rps->num_negative_pics + nb_positive_pics;
        if (rps->num_delta_pocs) {
            prev = 0;
            for (i = 0; i < rps->num_negative_pics; i++) {
220
                delta_poc = get_ue_golomb_long(gb) + 1;
221 222
                prev -= delta_poc;
                rps->delta_poc[i] = prev;
Guillaume Martres's avatar
Guillaume Martres committed
223
                rps->used[i]      = get_bits1(gb);
224 225 226
            }
            prev = 0;
            for (i = 0; i < nb_positive_pics; i++) {
227
                delta_poc = get_ue_golomb_long(gb) + 1;
228 229
                prev += delta_poc;
                rps->delta_poc[rps->num_negative_pics + i] = prev;
Guillaume Martres's avatar
Guillaume Martres committed
230
                rps->used[rps->num_negative_pics + i]      = get_bits1(gb);
231 232 233 234 235 236
            }
        }
    }
    return 0;
}

gcocherel's avatar
gcocherel committed
237

238
static int decode_profile_tier_level(GetBitContext *gb, AVCodecContext *avctx,
239
                                      PTLCommon *ptl)
240
{
gcocherel's avatar
gcocherel committed
241
    int i;
242

243 244 245
    if (get_bits_left(gb) < 2+1+5 + 32 + 4 + 16 + 16 + 12)
        return -1;

gcocherel's avatar
gcocherel committed
246 247 248
    ptl->profile_space = get_bits(gb, 2);
    ptl->tier_flag     = get_bits1(gb);
    ptl->profile_idc   = get_bits(gb, 5);
249
    if (ptl->profile_idc == FF_PROFILE_HEVC_MAIN)
250
        av_log(avctx, AV_LOG_DEBUG, "Main profile bitstream\n");
251
    else if (ptl->profile_idc == FF_PROFILE_HEVC_MAIN_10)
252
        av_log(avctx, AV_LOG_DEBUG, "Main 10 profile bitstream\n");
253
    else if (ptl->profile_idc == FF_PROFILE_HEVC_MAIN_STILL_PICTURE)
254
        av_log(avctx, AV_LOG_DEBUG, "Main Still Picture profile bitstream\n");
255
    else if (ptl->profile_idc == FF_PROFILE_HEVC_REXT)
256
        av_log(avctx, AV_LOG_DEBUG, "Range Extension profile bitstream\n");
257
    else
258
        av_log(avctx, AV_LOG_WARNING, "Unknown HEVC profile: %d\n", ptl->profile_idc);
259

260
    for (i = 0; i < 32; i++) {
gcocherel's avatar
gcocherel committed
261
        ptl->profile_compatibility_flag[i] = get_bits1(gb);
262 263 264 265

        if (ptl->profile_idc == 0 && i > 0 && ptl->profile_compatibility_flag[i])
            ptl->profile_idc = i;
    }
gcocherel's avatar
gcocherel committed
266 267 268 269
    ptl->progressive_source_flag    = get_bits1(gb);
    ptl->interlaced_source_flag     = get_bits1(gb);
    ptl->non_packed_constraint_flag = get_bits1(gb);
    ptl->frame_only_constraint_flag = get_bits1(gb);
270 271 272 273

    skip_bits(gb, 16); // XXX_reserved_zero_44bits[0..15]
    skip_bits(gb, 16); // XXX_reserved_zero_44bits[16..31]
    skip_bits(gb, 12); // XXX_reserved_zero_44bits[32..43]
274 275

    return 0;
gcocherel's avatar
gcocherel committed
276 277
}

278
static int parse_ptl(GetBitContext *gb, AVCodecContext *avctx,
279
                      PTL *ptl, int max_num_sub_layers)
gcocherel's avatar
gcocherel committed
280 281
{
    int i;
282
    if (decode_profile_tier_level(gb, avctx, &ptl->general_ptl) < 0 ||
283
        get_bits_left(gb) < 8 + (8*2 * (max_num_sub_layers - 1 > 0))) {
284
        av_log(avctx, AV_LOG_ERROR, "PTL information too short\n");
285 286 287
        return -1;
    }

288
    ptl->general_ptl.level_idc = get_bits(gb, 8);
289 290 291

    for (i = 0; i < max_num_sub_layers - 1; i++) {
        ptl->sub_layer_profile_present_flag[i] = get_bits1(gb);
Guillaume Martres's avatar
Guillaume Martres committed
292
        ptl->sub_layer_level_present_flag[i]   = get_bits1(gb);
293
    }
294

gcocherel's avatar
gcocherel committed
295
    if (max_num_sub_layers - 1> 0)
296
        for (i = max_num_sub_layers - 1; i < 8; i++)
gcocherel's avatar
gcocherel committed
297
            skip_bits(gb, 2); // reserved_zero_2bits[i]
298
    for (i = 0; i < max_num_sub_layers - 1; i++) {
299
        if (ptl->sub_layer_profile_present_flag[i] &&
300 301
            decode_profile_tier_level(gb, avctx, &ptl->sub_layer_ptl[i]) < 0) {
            av_log(avctx, AV_LOG_ERROR,
302 303 304 305 306
                   "PTL information for sublayer %i too short\n", i);
            return -1;
        }
        if (ptl->sub_layer_level_present_flag[i]) {
            if (get_bits_left(gb) < 8) {
307
                av_log(avctx, AV_LOG_ERROR,
308 309 310 311 312
                       "Not enough data for sublayer %i level_idc\n", i);
                return -1;
            } else
                ptl->sub_layer_ptl[i].level_idc = get_bits(gb, 8);
        }
313
    }
314 315

    return 0;
316 317
}

318
static void decode_sublayer_hrd(GetBitContext *gb, unsigned int nb_cpb,
Guillaume Martres's avatar
Guillaume Martres committed
319
                                int subpic_params_present)
320
{
Mickaël Raulet's avatar
Mickaël Raulet committed
321 322 323 324 325 326 327 328 329 330 331 332 333 334
    int i;

    for (i = 0; i < nb_cpb; i++) {
        get_ue_golomb_long(gb); // bit_rate_value_minus1
        get_ue_golomb_long(gb); // cpb_size_value_minus1

        if (subpic_params_present) {
            get_ue_golomb_long(gb); // cpb_size_du_value_minus1
            get_ue_golomb_long(gb); // bit_rate_du_value_minus1
        }
        skip_bits1(gb); // cbr_flag
    }
}

335
static int decode_hrd(GetBitContext *gb, int common_inf_present,
336
                       int max_sublayers)
Mickaël Raulet's avatar
Mickaël Raulet committed
337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359
{
    int nal_params_present = 0, vcl_params_present = 0;
    int subpic_params_present = 0;
    int i;

    if (common_inf_present) {
        nal_params_present = get_bits1(gb);
        vcl_params_present = get_bits1(gb);

        if (nal_params_present || vcl_params_present) {
            subpic_params_present = get_bits1(gb);

            if (subpic_params_present) {
                skip_bits(gb, 8); // tick_divisor_minus2
                skip_bits(gb, 5); // du_cpb_removal_delay_increment_length_minus1
                skip_bits(gb, 1); // sub_pic_cpb_params_in_pic_timing_sei_flag
                skip_bits(gb, 5); // dpb_output_delay_du_length_minus1
            }

            skip_bits(gb, 4); // bit_rate_scale
            skip_bits(gb, 4); // cpb_size_scale

            if (subpic_params_present)
360
                skip_bits(gb, 4);  // cpb_size_du_scale
Mickaël Raulet's avatar
Mickaël Raulet committed
361 362 363 364 365 366 367 368 369

            skip_bits(gb, 5); // initial_cpb_removal_delay_length_minus1
            skip_bits(gb, 5); // au_cpb_removal_delay_length_minus1
            skip_bits(gb, 5); // dpb_output_delay_length_minus1
        }
    }

    for (i = 0; i < max_sublayers; i++) {
        int low_delay = 0;
370
        unsigned int nb_cpb = 1;
Mickaël Raulet's avatar
Mickaël Raulet committed
371 372 373 374 375 376
        int fixed_rate = get_bits1(gb);

        if (!fixed_rate)
            fixed_rate = get_bits1(gb);

        if (fixed_rate)
377
            get_ue_golomb_long(gb);  // elemental_duration_in_tc_minus1
Mickaël Raulet's avatar
Mickaël Raulet committed
378 379 380
        else
            low_delay = get_bits1(gb);

381
        if (!low_delay) {
Mickaël Raulet's avatar
Mickaël Raulet committed
382
            nb_cpb = get_ue_golomb_long(gb) + 1;
383
            if (nb_cpb < 1 || nb_cpb > 32) {
384
                av_log(NULL, AV_LOG_ERROR, "nb_cpb %d invalid\n", nb_cpb);
385 386 387
                return AVERROR_INVALIDDATA;
            }
        }
Mickaël Raulet's avatar
Mickaël Raulet committed
388 389

        if (nal_params_present)
390
            decode_sublayer_hrd(gb, nb_cpb, subpic_params_present);
Mickaël Raulet's avatar
Mickaël Raulet committed
391
        if (vcl_params_present)
392
            decode_sublayer_hrd(gb, nb_cpb, subpic_params_present);
Mickaël Raulet's avatar
Mickaël Raulet committed
393
    }
394
    return 0;
395 396
}

397 398
int ff_hevc_decode_nal_vps(GetBitContext *gb, AVCodecContext *avctx,
                           HEVCParamSets *ps)
399 400 401
{
    int i,j;
    int vps_id = 0;
402
    ptrdiff_t nal_size;
Guillaume Martres's avatar
Guillaume Martres committed
403
    HEVCVPS *vps;
gcocherel's avatar
gcocherel committed
404 405 406 407 408
    AVBufferRef *vps_buf = av_buffer_allocz(sizeof(*vps));

    if (!vps_buf)
        return AVERROR(ENOMEM);
    vps = (HEVCVPS*)vps_buf->data;
409

410
    av_log(avctx, AV_LOG_DEBUG, "Decoding VPS\n");
411

412 413 414 415 416 417 418 419 420 421 422
    nal_size = gb->buffer_end - gb->buffer;
    if (nal_size > sizeof(vps->data)) {
        av_log(avctx, AV_LOG_WARNING, "Truncating likely oversized VPS "
               "(%"PTRDIFF_SPECIFIER" > %"SIZE_SPECIFIER")\n",
               nal_size, sizeof(vps->data));
        vps->data_size = sizeof(vps->data);
    } else {
        vps->data_size = nal_size;
    }
    memcpy(vps->data, gb->buffer, vps->data_size);

423 424
    vps_id = get_bits(gb, 4);
    if (vps_id >= MAX_VPS_COUNT) {
425
        av_log(avctx, AV_LOG_ERROR, "VPS id out of range: %d\n", vps_id);
426 427 428 429
        goto err;
    }

    if (get_bits(gb, 2) != 3) { // vps_reserved_three_2bits
430
        av_log(avctx, AV_LOG_ERROR, "vps_reserved_three_2bits is not three\n");
431 432 433 434 435 436 437 438
        goto err;
    }

    vps->vps_max_layers               = get_bits(gb, 6) + 1;
    vps->vps_max_sub_layers           = get_bits(gb, 3) + 1;
    vps->vps_temporal_id_nesting_flag = get_bits1(gb);

    if (get_bits(gb, 16) != 0xffff) { // vps_reserved_ffff_16bits
439
        av_log(avctx, AV_LOG_ERROR, "vps_reserved_ffff_16bits is not 0xffff\n");
440 441 442 443
        goto err;
    }

    if (vps->vps_max_sub_layers > MAX_SUB_LAYERS) {
444
        av_log(avctx, AV_LOG_ERROR, "vps_max_sub_layers out of range: %d\n",
445 446 447 448
               vps->vps_max_sub_layers);
        goto err;
    }

449
    if (parse_ptl(gb, avctx, &vps->ptl, vps->vps_max_sub_layers) < 0)
450
        goto err;
451

452 453 454 455
    vps->vps_sub_layer_ordering_info_present_flag = get_bits1(gb);

    i = vps->vps_sub_layer_ordering_info_present_flag ? 0 : vps->vps_max_sub_layers - 1;
    for (; i < vps->vps_max_sub_layers; i++) {
456 457 458
        vps->vps_max_dec_pic_buffering[i] = get_ue_golomb_long(gb) + 1;
        vps->vps_num_reorder_pics[i]      = get_ue_golomb_long(gb);
        vps->vps_max_latency_increase[i]  = get_ue_golomb_long(gb) - 1;
459

460
        if (vps->vps_max_dec_pic_buffering[i] > MAX_DPB_SIZE || !vps->vps_max_dec_pic_buffering[i]) {
461
            av_log(avctx, AV_LOG_ERROR, "vps_max_dec_pic_buffering_minus1 out of range: %d\n",
462 463 464
                   vps->vps_max_dec_pic_buffering[i] - 1);
            goto err;
        }
465
        if (vps->vps_num_reorder_pics[i] > vps->vps_max_dec_pic_buffering[i] - 1) {
466
            av_log(avctx, AV_LOG_WARNING, "vps_max_num_reorder_pics out of range: %d\n",
467
                   vps->vps_num_reorder_pics[i]);
468
            if (avctx->err_recognition & AV_EF_EXPLODE)
Kieran's avatar
Kieran committed
469
                goto err;
470 471 472 473
        }
    }

    vps->vps_max_layer_id   = get_bits(gb, 6);
474
    vps->vps_num_layer_sets = get_ue_golomb_long(gb) + 1;
475 476
    if (vps->vps_num_layer_sets < 1 || vps->vps_num_layer_sets > 1024 ||
        (vps->vps_num_layer_sets - 1LL) * (vps->vps_max_layer_id + 1LL) > get_bits_left(gb)) {
477
        av_log(avctx, AV_LOG_ERROR, "too many layer_id_included_flags\n");
478 479 480
        goto err;
    }

481 482
    for (i = 1; i < vps->vps_num_layer_sets; i++)
        for (j = 0; j <= vps->vps_max_layer_id; j++)
483
            skip_bits(gb, 1);  // layer_id_included_flag[i][j]
484 485 486 487 488 489 490

    vps->vps_timing_info_present_flag = get_bits1(gb);
    if (vps->vps_timing_info_present_flag) {
        vps->vps_num_units_in_tick               = get_bits_long(gb, 32);
        vps->vps_time_scale                      = get_bits_long(gb, 32);
        vps->vps_poc_proportional_to_timing_flag = get_bits1(gb);
        if (vps->vps_poc_proportional_to_timing_flag)
491 492
            vps->vps_num_ticks_poc_diff_one = get_ue_golomb_long(gb) + 1;
        vps->vps_num_hrd_parameters = get_ue_golomb_long(gb);
493
        if (vps->vps_num_hrd_parameters > (unsigned)vps->vps_num_layer_sets) {
494
            av_log(avctx, AV_LOG_ERROR,
495 496 497
                   "vps_num_hrd_parameters %d is invalid\n", vps->vps_num_hrd_parameters);
            goto err;
        }
Mickaël Raulet's avatar
Mickaël Raulet committed
498 499 500 501 502 503
        for (i = 0; i < vps->vps_num_hrd_parameters; i++) {
            int common_inf_present = 1;

            get_ue_golomb_long(gb); // hrd_layer_set_idx
            if (i)
                common_inf_present = get_bits1(gb);
504
            decode_hrd(gb, common_inf_present, vps->vps_max_sub_layers);
505 506
        }
    }
507
    get_bits1(gb); /* vps_extension_flag */
gcocherel's avatar
gcocherel committed
508

509
    if (get_bits_left(gb) < 0) {
510
        av_log(avctx, AV_LOG_ERROR,
511
               "Overread VPS by %d bits\n", -get_bits_left(gb));
512
        if (ps->vps_list[vps_id])
513
            goto err;
514 515
    }

516 517
    if (ps->vps_list[vps_id] &&
        !memcmp(ps->vps_list[vps_id]->data, vps_buf->data, vps_buf->size)) {
518 519
        av_buffer_unref(&vps_buf);
    } else {
520 521
        remove_vps(ps, vps_id);
        ps->vps_list[vps_id] = vps_buf;
522 523
    }

524 525 526
    return 0;

err:
gcocherel's avatar
gcocherel committed
527
    av_buffer_unref(&vps_buf);
528 529 530
    return AVERROR_INVALIDDATA;
}

531 532
static void decode_vui(GetBitContext *gb, AVCodecContext *avctx,
                       int apply_defdispwin, HEVCSPS *sps)
533
{
Guillaume Martres's avatar
Guillaume Martres committed
534
    VUI *vui          = &sps->vui;
535
    GetBitContext backup;
536
    int sar_present, alt = 0;
537

538
    av_log(avctx, AV_LOG_DEBUG, "Decoding VUI\n");
539 540 541 542 543 544 545 546 547 548

    sar_present = get_bits1(gb);
    if (sar_present) {
        uint8_t sar_idx = get_bits(gb, 8);
        if (sar_idx < FF_ARRAY_ELEMS(vui_sar))
            vui->sar = vui_sar[sar_idx];
        else if (sar_idx == 255) {
            vui->sar.num = get_bits(gb, 16);
            vui->sar.den = get_bits(gb, 16);
        } else
549
            av_log(avctx, AV_LOG_WARNING,
Guillaume Martres's avatar
Guillaume Martres committed
550
                   "Unknown SAR index: %u.\n", sar_idx);
551 552 553 554 555 556 557 558 559 560 561
    }

    vui->overscan_info_present_flag = get_bits1(gb);
    if (vui->overscan_info_present_flag)
        vui->overscan_appropriate_flag = get_bits1(gb);

    vui->video_signal_type_present_flag = get_bits1(gb);
    if (vui->video_signal_type_present_flag) {
        vui->video_format                    = get_bits(gb, 3);
        vui->video_full_range_flag           = get_bits1(gb);
        vui->colour_description_present_flag = get_bits1(gb);
562 563
        if (vui->video_full_range_flag && sps->pix_fmt == AV_PIX_FMT_YUV420P)
            sps->pix_fmt = AV_PIX_FMT_YUVJ420P;
564 565 566 567
        if (vui->colour_description_present_flag) {
            vui->colour_primaries        = get_bits(gb, 8);
            vui->transfer_characteristic = get_bits(gb, 8);
            vui->matrix_coeffs           = get_bits(gb, 8);
568 569 570 571 572 573 574 575

            // Set invalid values to "unspecified"
            if (vui->colour_primaries >= AVCOL_PRI_NB)
                vui->colour_primaries = AVCOL_PRI_UNSPECIFIED;
            if (vui->transfer_characteristic >= AVCOL_TRC_NB)
                vui->transfer_characteristic = AVCOL_TRC_UNSPECIFIED;
            if (vui->matrix_coeffs >= AVCOL_SPC_NB)
                vui->matrix_coeffs = AVCOL_SPC_UNSPECIFIED;
576 577 578 579 580 581 582 583 584 585 586 587 588
            if (vui->matrix_coeffs == AVCOL_SPC_RGB) {
                switch (sps->pix_fmt) {
                case AV_PIX_FMT_YUV444P:
                    sps->pix_fmt = AV_PIX_FMT_GBRP;
                    break;
                case AV_PIX_FMT_YUV444P10:
                    sps->pix_fmt = AV_PIX_FMT_GBRP10;
                    break;
                case AV_PIX_FMT_YUV444P12:
                    sps->pix_fmt = AV_PIX_FMT_GBRP12;
                    break;
                }
            }
589 590 591 592 593
        }
    }

    vui->chroma_loc_info_present_flag = get_bits1(gb);
    if (vui->chroma_loc_info_present_flag) {
594 595
        vui->chroma_sample_loc_type_top_field    = get_ue_golomb_long(gb);
        vui->chroma_sample_loc_type_bottom_field = get_ue_golomb_long(gb);
596 597 598 599 600 601
    }

    vui->neutra_chroma_indication_flag = get_bits1(gb);
    vui->field_seq_flag                = get_bits1(gb);
    vui->frame_field_info_present_flag = get_bits1(gb);

602 603
    if (get_bits_left(gb) >= 68 && show_bits_long(gb, 21) == 0x100000) {
        vui->default_display_window_flag = 0;
604
        av_log(avctx, AV_LOG_WARNING, "Invalid default display window\n");
605 606
    } else
        vui->default_display_window_flag = get_bits1(gb);
607
    // Backup context in case an alternate header is detected
608
    memcpy(&backup, gb, sizeof(backup));
609

610
    if (vui->default_display_window_flag) {
611 612 613 614 615 616
        int vert_mult  = 1 + (sps->chroma_format_idc < 2);
        int horiz_mult = 1 + (sps->chroma_format_idc < 3);
        vui->def_disp_win.left_offset   = get_ue_golomb_long(gb) * horiz_mult;
        vui->def_disp_win.right_offset  = get_ue_golomb_long(gb) * horiz_mult;
        vui->def_disp_win.top_offset    = get_ue_golomb_long(gb) *  vert_mult;
        vui->def_disp_win.bottom_offset = get_ue_golomb_long(gb) *  vert_mult;
617

618
        if (apply_defdispwin &&
619
            avctx->flags2 & AV_CODEC_FLAG2_IGNORE_CROP) {
620
            av_log(avctx, AV_LOG_DEBUG,
621 622 623 624 625 626 627 628 629 630 631 632 633 634 635
                   "discarding vui default display window, "
                   "original values are l:%u r:%u t:%u b:%u\n",
                   vui->def_disp_win.left_offset,
                   vui->def_disp_win.right_offset,
                   vui->def_disp_win.top_offset,
                   vui->def_disp_win.bottom_offset);

            vui->def_disp_win.left_offset   =
            vui->def_disp_win.right_offset  =
            vui->def_disp_win.top_offset    =
            vui->def_disp_win.bottom_offset = 0;
        }
    }

    vui->vui_timing_info_present_flag = get_bits1(gb);
gcocherel's avatar
gcocherel committed
636

637
    if (vui->vui_timing_info_present_flag) {
638 639 640
        if( get_bits_left(gb) < 66) {
            // The alternate syntax seem to have timing info located
            // at where def_disp_win is normally located
641
            av_log(avctx, AV_LOG_WARNING,
642 643 644 645 646 647
                   "Strange VUI timing information, retrying...\n");
            vui->default_display_window_flag = 0;
            memset(&vui->def_disp_win, 0, sizeof(vui->def_disp_win));
            memcpy(gb, &backup, sizeof(backup));
            alt = 1;
        }
648 649
        vui->vui_num_units_in_tick               = get_bits_long(gb, 32);
        vui->vui_time_scale                      = get_bits_long(gb, 32);
650
        if (alt) {
651
            av_log(avctx, AV_LOG_INFO, "Retry got %i/%i fps\n",
652 653
                   vui->vui_time_scale, vui->vui_num_units_in_tick);
        }
654 655
        vui->vui_poc_proportional_to_timing_flag = get_bits1(gb);
        if (vui->vui_poc_proportional_to_timing_flag)
656
            vui->vui_num_ticks_poc_diff_one_minus1 = get_ue_golomb_long(gb);
657 658
        vui->vui_hrd_parameters_present_flag = get_bits1(gb);
        if (vui->vui_hrd_parameters_present_flag)
659
            decode_hrd(gb, 1, sps->max_sub_layers);
660 661 662 663 664 665 666
    }

    vui->bitstream_restriction_flag = get_bits1(gb);
    if (vui->bitstream_restriction_flag) {
        vui->tiles_fixed_structure_flag              = get_bits1(gb);
        vui->motion_vectors_over_pic_boundaries_flag = get_bits1(gb);
        vui->restricted_ref_pic_lists_flag           = get_bits1(gb);
667 668 669 670 671
        vui->min_spatial_segmentation_idc            = get_ue_golomb_long(gb);
        vui->max_bytes_per_pic_denom                 = get_ue_golomb_long(gb);
        vui->max_bits_per_min_cu_denom               = get_ue_golomb_long(gb);
        vui->log2_max_mv_length_horizontal           = get_ue_golomb_long(gb);
        vui->log2_max_mv_length_vertical             = get_ue_golomb_long(gb);
672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697
    }
}

static void set_default_scaling_list_data(ScalingList *sl)
{
    int matrixId;

    for (matrixId = 0; matrixId < 6; matrixId++) {
        // 4x4 default is 16
        memset(sl->sl[0][matrixId], 16, 16);
        sl->sl_dc[0][matrixId] = 16; // default for 16x16
        sl->sl_dc[1][matrixId] = 16; // default for 32x32
    }
    memcpy(sl->sl[1][0], default_scaling_list_intra, 64);
    memcpy(sl->sl[1][1], default_scaling_list_intra, 64);
    memcpy(sl->sl[1][2], default_scaling_list_intra, 64);
    memcpy(sl->sl[1][3], default_scaling_list_inter, 64);
    memcpy(sl->sl[1][4], default_scaling_list_inter, 64);
    memcpy(sl->sl[1][5], default_scaling_list_inter, 64);
    memcpy(sl->sl[2][0], default_scaling_list_intra, 64);
    memcpy(sl->sl[2][1], default_scaling_list_intra, 64);
    memcpy(sl->sl[2][2], default_scaling_list_intra, 64);
    memcpy(sl->sl[2][3], default_scaling_list_inter, 64);
    memcpy(sl->sl[2][4], default_scaling_list_inter, 64);
    memcpy(sl->sl[2][5], default_scaling_list_inter, 64);
    memcpy(sl->sl[3][0], default_scaling_list_intra, 64);
698 699 700 701 702
    memcpy(sl->sl[3][1], default_scaling_list_intra, 64);
    memcpy(sl->sl[3][2], default_scaling_list_intra, 64);
    memcpy(sl->sl[3][3], default_scaling_list_inter, 64);
    memcpy(sl->sl[3][4], default_scaling_list_inter, 64);
    memcpy(sl->sl[3][5], default_scaling_list_inter, 64);
703 704
}

705
static int scaling_list_data(GetBitContext *gb, AVCodecContext *avctx, ScalingList *sl, HEVCSPS *sps)
706
{
707
    uint8_t scaling_list_pred_mode_flag;
708
    int32_t scaling_list_dc_coef[2][6];
709 710
    int size_id, matrix_id, pos;
    int i;
Guillaume Martres's avatar
Guillaume Martres committed
711

712
    for (size_id = 0; size_id < 4; size_id++)
713 714 715
        for (matrix_id = 0; matrix_id < 6; matrix_id += ((size_id == 3) ? 3 : 1)) {
            scaling_list_pred_mode_flag = get_bits1(gb);
            if (!scaling_list_pred_mode_flag) {
716
                unsigned int delta = get_ue_golomb_long(gb);
Guillaume Martres's avatar
Guillaume Martres committed
717 718
                /* Only need to handle non-zero delta. Zero means default,
                 * which should already be in the arrays. */
719 720
                if (delta) {
                    // Copy from previous array.
721
                    if (matrix_id < delta) {
722
                        av_log(avctx, AV_LOG_ERROR,
723 724 725 726 727 728 729 730 731 732 733
                               "Invalid delta in scaling list data: %d.\n", delta);
                        return AVERROR_INVALIDDATA;
                    }

                    memcpy(sl->sl[size_id][matrix_id],
                           sl->sl[size_id][matrix_id - delta],
                           size_id > 0 ? 64 : 16);
                    if (size_id > 1)
                        sl->sl_dc[size_id - 2][matrix_id] = sl->sl_dc[size_id - 2][matrix_id - delta];
                }
            } else {
Guillaume Martres's avatar
Guillaume Martres committed
734
                int next_coef, coef_num;
735 736 737
                int32_t scaling_list_delta_coef;

                next_coef = 8;
738
                coef_num  = FFMIN(64, 1 << (4 + (size_id << 1)));
739 740 741 742 743 744 745
                if (size_id > 1) {
                    scaling_list_dc_coef[size_id - 2][matrix_id] = get_se_golomb(gb) + 8;
                    next_coef = scaling_list_dc_coef[size_id - 2][matrix_id];
                    sl->sl_dc[size_id - 2][matrix_id] = next_coef;
                }
                for (i = 0; i < coef_num; i++) {
                    if (size_id == 0)
Guillaume Martres's avatar
Guillaume Martres committed
746 747
                        pos = 4 * ff_hevc_diag_scan4x4_y[i] +
                                  ff_hevc_diag_scan4x4_x[i];
748
                    else
Guillaume Martres's avatar
Guillaume Martres committed
749 750
                        pos = 8 * ff_hevc_diag_scan8x8_y[i] +
                                  ff_hevc_diag_scan8x8_x[i];
751 752

                    scaling_list_delta_coef = get_se_golomb(gb);
Guillaume Martres's avatar
Guillaume Martres committed
753
                    next_coef = (next_coef + scaling_list_delta_coef + 256) % 256;
754 755 756 757 758
                    sl->sl[size_id][matrix_id][pos] = next_coef;
                }
            }
        }

759 760 761 762 763 764 765 766 767 768 769 770 771 772
    if (sps->chroma_format_idc == 3) {
        for (i = 0; i < 64; i++) {
            sl->sl[3][1][i] = sl->sl[2][1][i];
            sl->sl[3][2][i] = sl->sl[2][2][i];
            sl->sl[3][4][i] = sl->sl[2][4][i];
            sl->sl[3][5][i] = sl->sl[2][5][i];
        }
        sl->sl_dc[1][1] = sl->sl_dc[0][1];
        sl->sl_dc[1][2] = sl->sl_dc[0][2];
        sl->sl_dc[1][4] = sl->sl_dc[0][4];
        sl->sl_dc[1][5] = sl->sl_dc[0][5];
    }


773 774 775
    return 0;
}

776 777 778
static int map_pixel_format(AVCodecContext *avctx, HEVCSPS *sps)
{
    const AVPixFmtDescriptor *desc;
779 780 781 782 783 784 785 786 787 788 789 790 791 792
    switch (sps->bit_depth) {
    case 8:
        if (sps->chroma_format_idc == 0) sps->pix_fmt = AV_PIX_FMT_GRAY8;
        if (sps->chroma_format_idc == 1) sps->pix_fmt = AV_PIX_FMT_YUV420P;
        if (sps->chroma_format_idc == 2) sps->pix_fmt = AV_PIX_FMT_YUV422P;
        if (sps->chroma_format_idc == 3) sps->pix_fmt = AV_PIX_FMT_YUV444P;
       break;
    case 9:
        if (sps->chroma_format_idc == 0) sps->pix_fmt = AV_PIX_FMT_GRAY16;
        if (sps->chroma_format_idc == 1) sps->pix_fmt = AV_PIX_FMT_YUV420P9;
        if (sps->chroma_format_idc == 2) sps->pix_fmt = AV_PIX_FMT_YUV422P9;
        if (sps->chroma_format_idc == 3) sps->pix_fmt = AV_PIX_FMT_YUV444P9;
        break;
    case 10:
793
        if (sps->chroma_format_idc == 0) sps->pix_fmt = AV_PIX_FMT_GRAY10;
794 795 796 797 798
        if (sps->chroma_format_idc == 1) sps->pix_fmt = AV_PIX_FMT_YUV420P10;
        if (sps->chroma_format_idc == 2) sps->pix_fmt = AV_PIX_FMT_YUV422P10;
        if (sps->chroma_format_idc == 3) sps->pix_fmt = AV_PIX_FMT_YUV444P10;
        break;
    case 12:
799
        if (sps->chroma_format_idc == 0) sps->pix_fmt = AV_PIX_FMT_GRAY12;
800 801 802 803 804
        if (sps->chroma_format_idc == 1) sps->pix_fmt = AV_PIX_FMT_YUV420P12;
        if (sps->chroma_format_idc == 2) sps->pix_fmt = AV_PIX_FMT_YUV422P12;
        if (sps->chroma_format_idc == 3) sps->pix_fmt = AV_PIX_FMT_YUV444P12;
        break;
    default:
805
        av_log(avctx, AV_LOG_ERROR,
806 807
               "The following bit-depths are currently specified: 8, 9, 10 and 12 bits, "
               "chroma_format_idc is %d, depth is %d\n",
808
               sps->chroma_format_idc, sps->bit_depth);
809
        return AVERROR_INVALIDDATA;
810 811 812 813 814 815 816 817 818 819 820 821 822 823 824
    }

    desc = av_pix_fmt_desc_get(sps->pix_fmt);
    if (!desc)
        return AVERROR(EINVAL);

    sps->hshift[0] = sps->vshift[0] = 0;
    sps->hshift[2] = sps->hshift[1] = desc->log2_chroma_w;
    sps->vshift[2] = sps->vshift[1] = desc->log2_chroma_h;

    sps->pixel_shift = sps->bit_depth > 8;

    return 0;
}

825 826
int ff_hevc_parse_sps(HEVCSPS *sps, GetBitContext *gb, unsigned int *sps_id,
                      int apply_defdispwin, AVBufferRef **vps_list, AVCodecContext *avctx)
827
{
828
    int ret = 0;
829 830 831 832 833 834 835 836
    int log2_diff_max_min_transform_block_size;
    int bit_depth_chroma, start, vui_present, sublayer_ordering_info;
    int i;

    // Coded parameters

    sps->vps_id = get_bits(gb, 4);
    if (sps->vps_id >= MAX_VPS_COUNT) {
837
        av_log(avctx, AV_LOG_ERROR, "VPS id out of range: %d\n", sps->vps_id);
838
        return AVERROR_INVALIDDATA;
839 840
    }

841 842
    if (vps_list && !vps_list[sps->vps_id]) {
        av_log(avctx, AV_LOG_ERROR, "VPS %d does not exist\n",
843
               sps->vps_id);
844
        return AVERROR_INVALIDDATA;
845 846
    }

847 848
    sps->max_sub_layers = get_bits(gb, 3) + 1;
    if (sps->max_sub_layers > MAX_SUB_LAYERS) {
849
        av_log(avctx, AV_LOG_ERROR, "sps_max_sub_layers out of range: %d\n",
850
               sps->max_sub_layers);
851
        return AVERROR_INVALIDDATA;
852 853 854
    }

    skip_bits1(gb); // temporal_id_nesting_flag
gcocherel's avatar
gcocherel committed
855

856
    if ((ret = parse_ptl(gb, avctx, &sps->ptl, sps->max_sub_layers)) < 0)
857
        return ret;
858

859 860 861
    *sps_id = get_ue_golomb_long(gb);
    if (*sps_id >= MAX_SPS_COUNT) {
        av_log(avctx, AV_LOG_ERROR, "SPS id out of range: %d\n", *sps_id);
862
        return AVERROR_INVALIDDATA;
863 864
    }

865
    sps->chroma_format_idc = get_ue_golomb_long(gb);
866 867 868 869
    if (sps->chroma_format_idc > 3U) {
        av_log(avctx, AV_LOG_ERROR, "chroma_format_idc %d is invalid\n", sps->chroma_format_idc);
        return AVERROR_INVALIDDATA;
    }
870 871 872 873

    if (sps->chroma_format_idc == 3)
        sps->separate_colour_plane_flag = get_bits1(gb);

874 875 876
    if (sps->separate_colour_plane_flag)
        sps->chroma_format_idc = 0;

877 878
    sps->width  = get_ue_golomb_long(gb);
    sps->height = get_ue_golomb_long(gb);
879
    if ((ret = av_image_check_size(sps->width,
880
                                   sps->height, 0, avctx)) < 0)
881
        return ret;
882 883

    if (get_bits1(gb)) { // pic_conformance_flag
884 885 886 887 888 889
        int vert_mult  = 1 + (sps->chroma_format_idc < 2);
        int horiz_mult = 1 + (sps->chroma_format_idc < 3);
        sps->pic_conf_win.left_offset   = get_ue_golomb_long(gb) * horiz_mult;
        sps->pic_conf_win.right_offset  = get_ue_golomb_long(gb) * horiz_mult;
        sps->pic_conf_win.top_offset    = get_ue_golomb_long(gb) *  vert_mult;
        sps->pic_conf_win.bottom_offset = get_ue_golomb_long(gb) *  vert_mult;
890

891
        if (avctx->flags2 & AV_CODEC_FLAG2_IGNORE_CROP) {
892
            av_log(avctx, AV_LOG_DEBUG,
893 894 895 896 897 898 899 900 901 902 903 904 905 906 907
                   "discarding sps conformance window, "
                   "original values are l:%u r:%u t:%u b:%u\n",
                   sps->pic_conf_win.left_offset,
                   sps->pic_conf_win.right_offset,
                   sps->pic_conf_win.top_offset,
                   sps->pic_conf_win.bottom_offset);

            sps->pic_conf_win.left_offset   =
            sps->pic_conf_win.right_offset  =
            sps->pic_conf_win.top_offset    =
            sps->pic_conf_win.bottom_offset = 0;
        }
        sps->output_window = sps->pic_conf_win;
    }

908 909
    sps->bit_depth   = get_ue_golomb_long(gb) + 8;
    bit_depth_chroma = get_ue_golomb_long(gb) + 8;
910
    if (sps->chroma_format_idc && bit_depth_chroma != sps->bit_depth) {
911
        av_log(avctx, AV_LOG_ERROR,
Guillaume Martres's avatar
Guillaume Martres committed
912 913
               "Luma bit depth (%d) is different from chroma bit depth (%d), "
               "this is unsupported.\n",
914
               sps->bit_depth, bit_depth_chroma);
915
        return AVERROR_INVALIDDATA;
916 917
    }

918 919
    ret = map_pixel_format(avctx, sps);
    if (ret < 0)
920
        return ret;
921

922
    sps->log2_max_poc_lsb = get_ue_golomb_long(gb) + 4;
923
    if (sps->log2_max_poc_lsb > 16) {
924
        av_log(avctx, AV_LOG_ERROR, "log2_max_pic_order_cnt_lsb_minus4 out range: %d\n",
925
               sps->log2_max_poc_lsb - 4);
926
        return AVERROR_INVALIDDATA;
927 928 929 930 931
    }

    sublayer_ordering_info = get_bits1(gb);
    start = sublayer_ordering_info ? 0 : sps->max_sub_layers - 1;
    for (i = start; i < sps->max_sub_layers; i++) {
932 933 934 935
        sps->temporal_layer[i].max_dec_pic_buffering = get_ue_golomb_long(gb) + 1;
        sps->temporal_layer[i].num_reorder_pics      = get_ue_golomb_long(gb);
        sps->temporal_layer[i].max_latency_increase  = get_ue_golomb_long(gb) - 1;
        if (sps->temporal_layer[i].max_dec_pic_buffering > MAX_DPB_SIZE) {
936
            av_log(avctx, AV_LOG_ERROR, "sps_max_dec_pic_buffering_minus1 out of range: %d\n",
937
                   sps->temporal_layer[i].max_dec_pic_buffering - 1);
938
            return AVERROR_INVALIDDATA;
939
        }
940
        if (sps->temporal_layer[i].num_reorder_pics > sps->temporal_layer[i].max_dec_pic_buffering - 1) {
941
            av_log(avctx, AV_LOG_WARNING, "sps_max_num_reorder_pics out of range: %d\n",
942
                   sps->temporal_layer[i].num_reorder_pics);
943
            if (avctx->err_recognition & AV_EF_EXPLODE ||
944
                sps->temporal_layer[i].num_reorder_pics > MAX_DPB_SIZE - 1) {
945
                return AVERROR_INVALIDDATA;
946 947
            }
            sps->temporal_layer[i].max_dec_pic_buffering = sps->temporal_layer[i].num_reorder_pics + 1;
948 949 950 951
        }
    }

    if (!sublayer_ordering_info) {
Guillaume Martres's avatar
Guillaume Martres committed
952
        for (i = 0; i < start; i++) {
953 954 955 956 957 958
            sps->temporal_layer[i].max_dec_pic_buffering = sps->temporal_layer[start].max_dec_pic_buffering;
            sps->temporal_layer[i].num_reorder_pics      = sps->temporal_layer[start].num_reorder_pics;
            sps->temporal_layer[i].max_latency_increase  = sps->temporal_layer[start].max_latency_increase;
        }
    }

Guillaume Martres's avatar
Guillaume Martres committed
959 960 961 962 963 964
    sps->log2_min_cb_size                    = get_ue_golomb_long(gb) + 3;
    sps->log2_diff_max_min_coding_block_size = get_ue_golomb_long(gb);
    sps->log2_min_tb_size                    = get_ue_golomb_long(gb) + 2;
    log2_diff_max_min_transform_block_size   = get_ue_golomb_long(gb);
    sps->log2_max_trafo_size                 = log2_diff_max_min_transform_block_size +
                                               sps->log2_min_tb_size;
965

966
    if (sps->log2_min_cb_size < 3 || sps->log2_min_cb_size > 30) {
967
        av_log(avctx, AV_LOG_ERROR, "Invalid value %d for log2_min_cb_size", sps->log2_min_cb_size);
968
        return AVERROR_INVALIDDATA;
969 970 971
    }

    if (sps->log2_diff_max_min_coding_block_size > 30) {
972
        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);
973
        return AVERROR_INVALIDDATA;
974 975 976
    }

    if (sps->log2_min_tb_size >= sps->log2_min_cb_size || sps->log2_min_tb_size < 2) {
977
        av_log(avctx, AV_LOG_ERROR, "Invalid value for log2_min_tb_size");
978
        return AVERROR_INVALIDDATA;
979
    }
980 981

    if (log2_diff_max_min_transform_block_size < 0 || log2_diff_max_min_transform_block_size > 30) {
982
        av_log(avctx, AV_LOG_ERROR, "Invalid value %d for log2_diff_max_min_transform_block_size", log2_diff_max_min_transform_block_size);
983
        return AVERROR_INVALIDDATA;
984 985
    }

986 987
    sps->max_transform_hierarchy_depth_inter = get_ue_golomb_long(gb);
    sps->max_transform_hierarchy_depth_intra = get_ue_golomb_long(gb);
988 989 990 991 992 993

    sps->scaling_list_enable_flag = get_bits1(gb);
    if (sps->scaling_list_enable_flag) {
        set_default_scaling_list_data(&sps->scaling_list);

        if (get_bits1(gb)) {
994
            ret = scaling_list_data(gb, avctx, &sps->scaling_list, sps);
995
            if (ret < 0)
996
                return ret;
997 998 999 1000 1001 1002 1003 1004 1005
        }
    }

    sps->amp_enabled_flag = get_bits1(gb);
    sps->sao_enabled      = get_bits1(gb);

    sps->pcm_enabled_flag = get_bits1(gb);
    if (sps->pcm_enabled_flag) {
        sps->pcm.bit_depth   = get_bits(gb, 4) + 1;
1006
        sps->pcm.bit_depth_chroma = get_bits(gb, 4) + 1;
1007
        sps->pcm.log2_min_pcm_cb_size = get_ue_golomb_long(gb) + 3;
1008
        sps->pcm.log2_max_pcm_cb_size = sps->pcm.log2_min_pcm_cb_size +
1009
                                        get_ue_golomb_long(gb);
1010
        if (sps->pcm.bit_depth > sps->bit_depth) {
1011
            av_log(avctx, AV_LOG_ERROR,
1012 1013
                   "PCM bit depth (%d) is greater than normal bit depth (%d)\n",
                   sps->pcm.bit_depth, sps->bit_depth);
1014
            return AVERROR_INVALIDDATA;
1015 1016 1017 1018 1019
        }

        sps->pcm.loop_filter_disable_flag = get_bits1(gb);
    }

1020
    sps->nb_st_rps = get_ue_golomb_long(gb);
1021
    if (sps->nb_st_rps > MAX_SHORT_TERM_RPS_COUNT) {
1022
        av_log(avctx, AV_LOG_ERROR, "Too many short term RPS: %d.\n",
1023
               sps->nb_st_rps);
1024
        return AVERROR_INVALIDDATA;
1025
    }
1026
    for (i = 0; i < sps->nb_st_rps; i++) {
1027
        if ((ret = ff_hevc_decode_short_term_rps(gb, avctx, &sps->st_rps[i],
1028
                                                 sps, 0)) < 0)
1029
            return ret;
1030 1031 1032 1033
    }

    sps->long_term_ref_pics_present_flag = get_bits1(gb);
    if (sps->long_term_ref_pics_present_flag) {
1034
        sps->num_long_term_ref_pics_sps = get_ue_golomb_long(gb);
1035
        if (sps->num_long_term_ref_pics_sps > 31U) {
1036
            av_log(avctx, AV_LOG_ERROR, "num_long_term_ref_pics_sps %d is out of range.\n",
1037
                   sps->num_long_term_ref_pics_sps);
1038
            return AVERROR_INVALIDDATA;
1039
        }
1040 1041 1042 1043 1044 1045 1046 1047
        for (i = 0; i < sps->num_long_term_ref_pics_sps; i++) {
            sps->lt_ref_pic_poc_lsb_sps[i]       = get_bits(gb, sps->log2_max_poc_lsb);
            sps->used_by_curr_pic_lt_sps_flag[i] = get_bits1(gb);
        }
    }

    sps->sps_temporal_mvp_enabled_flag          = get_bits1(gb);
    sps->sps_strong_intra_smoothing_enable_flag = get_bits1(gb);
Mickaël Raulet's avatar
Mickaël Raulet committed
1048
    sps->vui.sar = (AVRational){0, 1};
1049 1050
    vui_present = get_bits1(gb);
    if (vui_present)
1051
        decode_vui(gb, avctx, apply_defdispwin, sps);
1052

1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070
    if (get_bits1(gb)) { // sps_extension_flag
        int sps_extension_flag[1];
        for (i = 0; i < 1; i++)
            sps_extension_flag[i] = get_bits1(gb);
        skip_bits(gb, 7); //sps_extension_7bits = get_bits(gb, 7);
        if (sps_extension_flag[0]) {
            int extended_precision_processing_flag;
            int high_precision_offsets_enabled_flag;
            int cabac_bypass_alignment_enabled_flag;

            sps->transform_skip_rotation_enabled_flag = get_bits1(gb);
            sps->transform_skip_context_enabled_flag  = get_bits1(gb);
            sps->implicit_rdpcm_enabled_flag = get_bits1(gb);

            sps->explicit_rdpcm_enabled_flag = get_bits1(gb);

            extended_precision_processing_flag = get_bits1(gb);
            if (extended_precision_processing_flag)
1071
                av_log(avctx, AV_LOG_WARNING,
1072 1073 1074 1075 1076
                   "extended_precision_processing_flag not yet implemented\n");

            sps->intra_smoothing_disabled_flag       = get_bits1(gb);
            high_precision_offsets_enabled_flag  = get_bits1(gb);
            if (high_precision_offsets_enabled_flag)
1077
                av_log(avctx, AV_LOG_WARNING,
1078 1079 1080 1081 1082 1083
                   "high_precision_offsets_enabled_flag not yet implemented\n");

            sps->persistent_rice_adaptation_enabled_flag = get_bits1(gb);

            cabac_bypass_alignment_enabled_flag  = get_bits1(gb);
            if (cabac_bypass_alignment_enabled_flag)
1084
                av_log(avctx, AV_LOG_WARNING,
1085 1086 1087
                   "cabac_bypass_alignment_enabled_flag not yet implemented\n");
        }
    }
1088
    if (apply_defdispwin) {
1089 1090 1091 1092 1093 1094
        sps->output_window.left_offset   += sps->vui.def_disp_win.left_offset;
        sps->output_window.right_offset  += sps->vui.def_disp_win.right_offset;
        sps->output_window.top_offset    += sps->vui.def_disp_win.top_offset;
        sps->output_window.bottom_offset += sps->vui.def_disp_win.bottom_offset;
    }
    if (sps->output_window.left_offset & (0x1F >> (sps->pixel_shift)) &&
1095
        !(avctx->flags & AV_CODEC_FLAG_UNALIGNED)) {
1096
        sps->output_window.left_offset &= ~(0x1F >> (sps->pixel_shift));
1097
        av_log(avctx, AV_LOG_WARNING, "Reducing left output window to %d "
1098 1099 1100 1101 1102 1103 1104
               "chroma samples to preserve alignment.\n",
               sps->output_window.left_offset);
    }
    sps->output_width  = sps->width -
                         (sps->output_window.left_offset + sps->output_window.right_offset);
    sps->output_height = sps->height -
                         (sps->output_window.top_offset + sps->output_window.bottom_offset);
1105 1106
    if (sps->width  <= sps->output_window.left_offset + (int64_t)sps->output_window.right_offset  ||
        sps->height <= sps->output_window.top_offset  + (int64_t)sps->output_window.bottom_offset) {
1107
        av_log(avctx, AV_LOG_WARNING, "Invalid visible frame dimensions: %dx%d.\n",
1108
               sps->output_width, sps->output_height);
1109
        if (avctx->err_recognition & AV_EF_EXPLODE) {
1110
            return AVERROR_INVALIDDATA;
1111
        }
1112
        av_log(avctx, AV_LOG_WARNING,
1113
               "Displaying the whole video surface.\n");
1114 1115
        memset(&sps->pic_conf_win, 0, sizeof(sps->pic_conf_win));
        memset(&sps->output_window, 0, sizeof(sps->output_window));
Guillaume Martres's avatar
Guillaume Martres committed
1116 1117
        sps->output_width               = sps->width;
        sps->output_height              = sps->height;
1118 1119 1120
    }

    // Inferred parameters
Guillaume Martres's avatar
Guillaume Martres committed
1121 1122 1123
    sps->log2_ctb_size = sps->log2_min_cb_size +
                         sps->log2_diff_max_min_coding_block_size;
    sps->log2_min_pu_size = sps->log2_min_cb_size - 1;
1124

1125
    if (sps->log2_ctb_size > MAX_LOG2_CTB_SIZE) {
1126
        av_log(avctx, AV_LOG_ERROR, "CTB size out of range: 2^%d\n", sps->log2_ctb_size);
1127
        return AVERROR_INVALIDDATA;
1128 1129
    }
    if (sps->log2_ctb_size < 4) {
1130
        av_log(avctx,
1131 1132 1133
               AV_LOG_ERROR,
               "log2_ctb_size %d differs from the bounds of any known profile\n",
               sps->log2_ctb_size);
1134
        avpriv_request_sample(avctx, "log2_ctb_size %d", sps->log2_ctb_size);
1135
        return AVERROR_INVALIDDATA;
1136 1137
    }

Guillaume Martres's avatar
Guillaume Martres committed
1138 1139 1140
    sps->ctb_width  = (sps->width  + (1 << sps->log2_ctb_size) - 1) >> sps->log2_ctb_size;
    sps->ctb_height = (sps->height + (1 << sps->log2_ctb_size) - 1) >> sps->log2_ctb_size;
    sps->ctb_size   = sps->ctb_width * sps->ctb_height;
1141

Guillaume Martres's avatar
Guillaume Martres committed
1142 1143 1144 1145 1146 1147
    sps->min_cb_width  = sps->width  >> sps->log2_min_cb_size;
    sps->min_cb_height = sps->height >> sps->log2_min_cb_size;
    sps->min_tb_width  = sps->width  >> sps->log2_min_tb_size;
    sps->min_tb_height = sps->height >> sps->log2_min_tb_size;
    sps->min_pu_width  = sps->width  >> sps->log2_min_pu_size;
    sps->min_pu_height = sps->height >> sps->log2_min_pu_size;
1148
    sps->tb_mask       = (1 << (sps->log2_ctb_size - sps->log2_min_tb_size)) - 1;
1149

Guillaume Martres's avatar
Guillaume Martres committed
1150
    sps->qp_bd_offset = 6 * (sps->bit_depth - 8);
1151

1152 1153
    if (av_mod_uintp2(sps->width, sps->log2_min_cb_size) ||
        av_mod_uintp2(sps->height, sps->log2_min_cb_size)) {
1154
        av_log(avctx, AV_LOG_ERROR, "Invalid coded frame dimensions.\n");
1155
        return AVERROR_INVALIDDATA;
1156 1157
    }

1158
    if (sps->max_transform_hierarchy_depth_inter > sps->log2_ctb_size - sps->log2_min_tb_size) {
1159
        av_log(avctx, AV_LOG_ERROR, "max_transform_hierarchy_depth_inter out of range: %d\n",
1160
               sps->max_transform_hierarchy_depth_inter);
1161
        return AVERROR_INVALIDDATA;
1162
    }
1163
    if (sps->max_transform_hierarchy_depth_intra > sps->log2_ctb_size - sps->log2_min_tb_size) {
1164
        av_log(avctx, AV_LOG_ERROR, "max_transform_hierarchy_depth_intra out of range: %d\n",
1165
               sps->max_transform_hierarchy_depth_intra);
1166
        return AVERROR_INVALIDDATA;
1167 1168
    }
    if (sps->log2_max_trafo_size > FFMIN(sps->log2_ctb_size, 5)) {
1169
        av_log(avctx, AV_LOG_ERROR,
Guillaume Martres's avatar
Guillaume Martres committed
1170
               "max transform block size out of range: %d\n",
1171
               sps->log2_max_trafo_size);
1172
        return AVERROR_INVALIDDATA;
1173 1174
    }

1175
    if (get_bits_left(gb) < 0) {
1176
        av_log(avctx, AV_LOG_ERROR,
1177
               "Overread SPS by %d bits\n", -get_bits_left(gb));
1178
        return AVERROR_INVALIDDATA;
1179 1180
    }

1181 1182 1183
    return 0;
}

1184 1185
int ff_hevc_decode_nal_sps(GetBitContext *gb, AVCodecContext *avctx,
                           HEVCParamSets *ps, int apply_defdispwin)
1186 1187 1188 1189 1190
{
    HEVCSPS *sps;
    AVBufferRef *sps_buf = av_buffer_allocz(sizeof(*sps));
    unsigned int sps_id;
    int ret;
1191
    ptrdiff_t nal_size;
1192 1193 1194 1195 1196

    if (!sps_buf)
        return AVERROR(ENOMEM);
    sps = (HEVCSPS*)sps_buf->data;

1197
    av_log(avctx, AV_LOG_DEBUG, "Decoding SPS\n");
1198

1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209
    nal_size = gb->buffer_end - gb->buffer;
    if (nal_size > sizeof(sps->data)) {
        av_log(avctx, AV_LOG_WARNING, "Truncating likely oversized SPS "
               "(%"PTRDIFF_SPECIFIER" > %"SIZE_SPECIFIER")\n",
               nal_size, sizeof(sps->data));
        sps->data_size = sizeof(sps->data);
    } else {
        sps->data_size = nal_size;
    }
    memcpy(sps->data, gb->buffer, sps->data_size);

1210 1211 1212
    ret = ff_hevc_parse_sps(sps, gb, &sps_id,
                            apply_defdispwin,
                            ps->vps_list, avctx);
1213 1214 1215 1216 1217
    if (ret < 0) {
        av_buffer_unref(&sps_buf);
        return ret;
    }

1218 1219
    if (avctx->debug & FF_DEBUG_BITSTREAM) {
        av_log(avctx, AV_LOG_DEBUG,
Guillaume Martres's avatar
Guillaume Martres committed
1220
               "Parsed SPS: id %d; coded wxh: %dx%d; "
1221 1222 1223 1224 1225 1226
               "cropped wxh: %dx%d; pix_fmt: %s.\n",
               sps_id, sps->width, sps->height,
               sps->output_width, sps->output_height,
               av_get_pix_fmt_name(sps->pix_fmt));
    }

1227 1228 1229
    /* check if this is a repeat of an already parsed SPS, then keep the
     * original one.
     * otherwise drop all PPSes that depend on it */
1230 1231
    if (ps->sps_list[sps_id] &&
        !memcmp(ps->sps_list[sps_id]->data, sps_buf->data, sps_buf->size)) {
1232 1233
        av_buffer_unref(&sps_buf);
    } else {
1234 1235
        remove_sps(ps, sps_id);
        ps->sps_list[sps_id] = sps_buf;
1236 1237
    }

1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253
    return 0;
}

static void hevc_pps_free(void *opaque, uint8_t *data)
{
    HEVCPPS *pps = (HEVCPPS*)data;

    av_freep(&pps->column_width);
    av_freep(&pps->row_height);
    av_freep(&pps->col_bd);
    av_freep(&pps->row_bd);
    av_freep(&pps->col_idxX);
    av_freep(&pps->ctb_addr_rs_to_ts);
    av_freep(&pps->ctb_addr_ts_to_rs);
    av_freep(&pps->tile_pos_rs);
    av_freep(&pps->tile_id);
1254
    av_freep(&pps->min_tb_addr_zs_tab);
1255 1256 1257 1258

    av_freep(&pps);
}

1259 1260
static int pps_range_extensions(GetBitContext *gb, AVCodecContext *avctx,
                                HEVCPPS *pps, HEVCSPS *sps) {
1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271
    int i;

    if (pps->transform_skip_enabled_flag) {
        pps->log2_max_transform_skip_block_size = get_ue_golomb_long(gb) + 2;
    }
    pps->cross_component_prediction_enabled_flag = get_bits1(gb);
    pps->chroma_qp_offset_list_enabled_flag = get_bits1(gb);
    if (pps->chroma_qp_offset_list_enabled_flag) {
        pps->diff_cu_chroma_qp_offset_depth = get_ue_golomb_long(gb);
        pps->chroma_qp_offset_list_len_minus1 = get_ue_golomb_long(gb);
        if (pps->chroma_qp_offset_list_len_minus1 && pps->chroma_qp_offset_list_len_minus1 >= 5) {
1272
            av_log(avctx, AV_LOG_ERROR,
1273 1274 1275 1276 1277 1278
                   "chroma_qp_offset_list_len_minus1 shall be in the range [0, 5].\n");
            return AVERROR_INVALIDDATA;
        }
        for (i = 0; i <= pps->chroma_qp_offset_list_len_minus1; i++) {
            pps->cb_qp_offset_list[i] = get_se_golomb_long(gb);
            if (pps->cb_qp_offset_list[i]) {
1279
                av_log(avctx, AV_LOG_WARNING,
1280 1281 1282 1283
                       "cb_qp_offset_list not tested yet.\n");
            }
            pps->cr_qp_offset_list[i] = get_se_golomb_long(gb);
            if (pps->cr_qp_offset_list[i]) {
1284
                av_log(avctx, AV_LOG_WARNING,
1285 1286 1287 1288 1289 1290 1291 1292 1293 1294
                       "cb_qp_offset_list not tested yet.\n");
            }
        }
    }
    pps->log2_sao_offset_scale_luma = get_ue_golomb_long(gb);
    pps->log2_sao_offset_scale_chroma = get_ue_golomb_long(gb);

    return(0);
}

1295 1296 1297 1298
static inline int setup_pps(AVCodecContext *avctx, GetBitContext *gb,
                            HEVCPPS *pps, HEVCSPS *sps)
{
    int log2_diff;
1299
    int pic_area_in_ctbs;
1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349
    int i, j, x, y, ctb_addr_rs, tile_id;

    // Inferred parameters
    pps->col_bd   = av_malloc_array(pps->num_tile_columns + 1, sizeof(*pps->col_bd));
    pps->row_bd   = av_malloc_array(pps->num_tile_rows + 1,    sizeof(*pps->row_bd));
    pps->col_idxX = av_malloc_array(sps->ctb_width,    sizeof(*pps->col_idxX));
    if (!pps->col_bd || !pps->row_bd || !pps->col_idxX)
        return AVERROR(ENOMEM);

    if (pps->uniform_spacing_flag) {
        if (!pps->column_width) {
            pps->column_width = av_malloc_array(pps->num_tile_columns, sizeof(*pps->column_width));
            pps->row_height   = av_malloc_array(pps->num_tile_rows,    sizeof(*pps->row_height));
        }
        if (!pps->column_width || !pps->row_height)
            return AVERROR(ENOMEM);

        for (i = 0; i < pps->num_tile_columns; i++) {
            pps->column_width[i] = ((i + 1) * sps->ctb_width) / pps->num_tile_columns -
                                   (i * sps->ctb_width) / pps->num_tile_columns;
        }

        for (i = 0; i < pps->num_tile_rows; i++) {
            pps->row_height[i] = ((i + 1) * sps->ctb_height) / pps->num_tile_rows -
                                 (i * sps->ctb_height) / pps->num_tile_rows;
        }
    }

    pps->col_bd[0] = 0;
    for (i = 0; i < pps->num_tile_columns; i++)
        pps->col_bd[i + 1] = pps->col_bd[i] + pps->column_width[i];

    pps->row_bd[0] = 0;
    for (i = 0; i < pps->num_tile_rows; i++)
        pps->row_bd[i + 1] = pps->row_bd[i] + pps->row_height[i];

    for (i = 0, j = 0; i < sps->ctb_width; i++) {
        if (i > pps->col_bd[j])
            j++;
        pps->col_idxX[i] = j;
    }

    /**
     * 6.5
     */
    pic_area_in_ctbs     = sps->ctb_width    * sps->ctb_height;

    pps->ctb_addr_rs_to_ts = av_malloc_array(pic_area_in_ctbs,    sizeof(*pps->ctb_addr_rs_to_ts));
    pps->ctb_addr_ts_to_rs = av_malloc_array(pic_area_in_ctbs,    sizeof(*pps->ctb_addr_ts_to_rs));
    pps->tile_id           = av_malloc_array(pic_area_in_ctbs,    sizeof(*pps->tile_id));
1350
    pps->min_tb_addr_zs_tab = av_malloc_array((sps->tb_mask+2) * (sps->tb_mask+2), sizeof(*pps->min_tb_addr_zs_tab));
1351
    if (!pps->ctb_addr_rs_to_ts || !pps->ctb_addr_ts_to_rs ||
1352
        !pps->tile_id || !pps->min_tb_addr_zs_tab) {
1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404
        return AVERROR(ENOMEM);
    }

    for (ctb_addr_rs = 0; ctb_addr_rs < pic_area_in_ctbs; ctb_addr_rs++) {
        int tb_x   = ctb_addr_rs % sps->ctb_width;
        int tb_y   = ctb_addr_rs / sps->ctb_width;
        int tile_x = 0;
        int tile_y = 0;
        int val    = 0;

        for (i = 0; i < pps->num_tile_columns; i++) {
            if (tb_x < pps->col_bd[i + 1]) {
                tile_x = i;
                break;
            }
        }

        for (i = 0; i < pps->num_tile_rows; i++) {
            if (tb_y < pps->row_bd[i + 1]) {
                tile_y = i;
                break;
            }
        }

        for (i = 0; i < tile_x; i++)
            val += pps->row_height[tile_y] * pps->column_width[i];
        for (i = 0; i < tile_y; i++)
            val += sps->ctb_width * pps->row_height[i];

        val += (tb_y - pps->row_bd[tile_y]) * pps->column_width[tile_x] +
               tb_x - pps->col_bd[tile_x];

        pps->ctb_addr_rs_to_ts[ctb_addr_rs] = val;
        pps->ctb_addr_ts_to_rs[val]         = ctb_addr_rs;
    }

    for (j = 0, tile_id = 0; j < pps->num_tile_rows; j++)
        for (i = 0; i < pps->num_tile_columns; i++, tile_id++)
            for (y = pps->row_bd[j]; y < pps->row_bd[j + 1]; y++)
                for (x = pps->col_bd[i]; x < pps->col_bd[i + 1]; x++)
                    pps->tile_id[pps->ctb_addr_rs_to_ts[y * sps->ctb_width + x]] = tile_id;

    pps->tile_pos_rs = av_malloc_array(tile_id, sizeof(*pps->tile_pos_rs));
    if (!pps->tile_pos_rs)
        return AVERROR(ENOMEM);

    for (j = 0; j < pps->num_tile_rows; j++)
        for (i = 0; i < pps->num_tile_columns; i++)
            pps->tile_pos_rs[j * pps->num_tile_columns + i] =
                pps->row_bd[j] * sps->ctb_width + pps->col_bd[i];

    log2_diff = sps->log2_ctb_size - sps->log2_min_tb_size;
1405 1406 1407 1408 1409 1410 1411
    pps->min_tb_addr_zs = &pps->min_tb_addr_zs_tab[1*(sps->tb_mask+2)+1];
    for (y = 0; y < sps->tb_mask+2; y++) {
        pps->min_tb_addr_zs_tab[y*(sps->tb_mask+2)] = -1;
        pps->min_tb_addr_zs_tab[y]    = -1;
    }
    for (y = 0; y < sps->tb_mask+1; y++) {
        for (x = 0; x < sps->tb_mask+1; x++) {
1412 1413 1414 1415 1416 1417 1418 1419
            int tb_x = x >> log2_diff;
            int tb_y = y >> log2_diff;
            int rs   = sps->ctb_width * tb_y + tb_x;
            int val  = pps->ctb_addr_rs_to_ts[rs] << (log2_diff * 2);
            for (i = 0; i < log2_diff; i++) {
                int m = 1 << i;
                val += (m & x ? m * m : 0) + (m & y ? 2 * m * m : 0);
            }
1420
            pps->min_tb_addr_zs[y * (sps->tb_mask+2) + x] = val;
1421 1422 1423 1424 1425 1426
        }
    }

    return 0;
}

1427 1428
int ff_hevc_decode_nal_pps(GetBitContext *gb, AVCodecContext *avctx,
                           HEVCParamSets *ps)
1429 1430
{
    HEVCSPS      *sps = NULL;
1431
    int i, ret = 0;
1432
    unsigned int pps_id = 0;
1433
    ptrdiff_t nal_size;
1434 1435 1436 1437 1438 1439 1440

    AVBufferRef *pps_buf;
    HEVCPPS *pps = av_mallocz(sizeof(*pps));

    if (!pps)
        return AVERROR(ENOMEM);

Guillaume Martres's avatar
Guillaume Martres committed
1441 1442
    pps_buf = av_buffer_create((uint8_t *)pps, sizeof(*pps),
                               hevc_pps_free, NULL, 0);
1443 1444 1445 1446 1447
    if (!pps_buf) {
        av_freep(&pps);
        return AVERROR(ENOMEM);
    }

1448
    av_log(avctx, AV_LOG_DEBUG, "Decoding PPS\n");
1449

1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460
    nal_size = gb->buffer_end - gb->buffer;
    if (nal_size > sizeof(pps->data)) {
        av_log(avctx, AV_LOG_WARNING, "Truncating likely oversized PPS "
               "(%"PTRDIFF_SPECIFIER" > %"SIZE_SPECIFIER")\n",
               nal_size, sizeof(pps->data));
        pps->data_size = sizeof(pps->data);
    } else {
        pps->data_size = nal_size;
    }
    memcpy(pps->data, gb->buffer, pps->data_size);

1461 1462 1463 1464 1465
    // Default values
    pps->loop_filter_across_tiles_enabled_flag = 1;
    pps->num_tile_columns                      = 1;
    pps->num_tile_rows                         = 1;
    pps->uniform_spacing_flag                  = 1;
1466
    pps->disable_dbf                           = 0;
1467 1468
    pps->beta_offset                           = 0;
    pps->tc_offset                             = 0;
1469
    pps->log2_max_transform_skip_block_size    = 2;
1470 1471

    // Coded parameters
1472
    pps_id = get_ue_golomb_long(gb);
1473
    if (pps_id >= MAX_PPS_COUNT) {
1474
        av_log(avctx, AV_LOG_ERROR, "PPS id out of range: %d\n", pps_id);
1475
        ret = AVERROR_INVALIDDATA;
1476 1477
        goto err;
    }
1478
    pps->sps_id = get_ue_golomb_long(gb);
1479
    if (pps->sps_id >= MAX_SPS_COUNT) {
1480
        av_log(avctx, AV_LOG_ERROR, "SPS id out of range: %d\n", pps->sps_id);
1481 1482 1483
        ret = AVERROR_INVALIDDATA;
        goto err;
    }
1484 1485
    if (!ps->sps_list[pps->sps_id]) {
        av_log(avctx, AV_LOG_ERROR, "SPS %u does not exist.\n", pps->sps_id);
1486 1487 1488
        ret = AVERROR_INVALIDDATA;
        goto err;
    }
1489
    sps = (HEVCSPS *)ps->sps_list[pps->sps_id]->data;
1490 1491 1492 1493 1494 1495 1496 1497 1498

    pps->dependent_slice_segments_enabled_flag = get_bits1(gb);
    pps->output_flag_present_flag              = get_bits1(gb);
    pps->num_extra_slice_header_bits           = get_bits(gb, 3);

    pps->sign_data_hiding_flag = get_bits1(gb);

    pps->cabac_init_present_flag = get_bits1(gb);

1499 1500
    pps->num_ref_idx_l0_default_active = get_ue_golomb_long(gb) + 1;
    pps->num_ref_idx_l1_default_active = get_ue_golomb_long(gb) + 1;
1501 1502 1503 1504 1505 1506 1507 1508 1509

    pps->pic_init_qp_minus26 = get_se_golomb(gb);

    pps->constrained_intra_pred_flag = get_bits1(gb);
    pps->transform_skip_enabled_flag = get_bits1(gb);

    pps->cu_qp_delta_enabled_flag = get_bits1(gb);
    pps->diff_cu_qp_delta_depth   = 0;
    if (pps->cu_qp_delta_enabled_flag)
1510
        pps->diff_cu_qp_delta_depth = get_ue_golomb_long(gb);
1511

1512 1513
    if (pps->diff_cu_qp_delta_depth < 0 ||
        pps->diff_cu_qp_delta_depth > sps->log2_diff_max_min_coding_block_size) {
1514
        av_log(avctx, AV_LOG_ERROR, "diff_cu_qp_delta_depth %d is invalid\n",
1515 1516 1517 1518 1519
               pps->diff_cu_qp_delta_depth);
        ret = AVERROR_INVALIDDATA;
        goto err;
    }

1520 1521
    pps->cb_qp_offset = get_se_golomb(gb);
    if (pps->cb_qp_offset < -12 || pps->cb_qp_offset > 12) {
1522
        av_log(avctx, AV_LOG_ERROR, "pps_cb_qp_offset out of range: %d\n",
1523 1524 1525 1526 1527 1528
               pps->cb_qp_offset);
        ret = AVERROR_INVALIDDATA;
        goto err;
    }
    pps->cr_qp_offset = get_se_golomb(gb);
    if (pps->cr_qp_offset < -12 || pps->cr_qp_offset > 12) {
1529
        av_log(avctx, AV_LOG_ERROR, "pps_cr_qp_offset out of range: %d\n",
1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543
               pps->cr_qp_offset);
        ret = AVERROR_INVALIDDATA;
        goto err;
    }
    pps->pic_slice_level_chroma_qp_offsets_present_flag = get_bits1(gb);

    pps->weighted_pred_flag   = get_bits1(gb);
    pps->weighted_bipred_flag = get_bits1(gb);

    pps->transquant_bypass_enable_flag    = get_bits1(gb);
    pps->tiles_enabled_flag               = get_bits1(gb);
    pps->entropy_coding_sync_enabled_flag = get_bits1(gb);

    if (pps->tiles_enabled_flag) {
Guillaume Martres's avatar
Guillaume Martres committed
1544 1545
        pps->num_tile_columns = get_ue_golomb_long(gb) + 1;
        pps->num_tile_rows    = get_ue_golomb_long(gb) + 1;
1546
        if (pps->num_tile_columns <= 0 ||
1547
            pps->num_tile_columns >= sps->width) {
1548
            av_log(avctx, AV_LOG_ERROR, "num_tile_columns_minus1 out of range: %d\n",
1549 1550
                   pps->num_tile_columns - 1);
            ret = AVERROR_INVALIDDATA;
1551 1552
            goto err;
        }
1553
        if (pps->num_tile_rows <= 0 ||
1554
            pps->num_tile_rows >= sps->height) {
1555
            av_log(avctx, AV_LOG_ERROR, "num_tile_rows_minus1 out of range: %d\n",
1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569
                   pps->num_tile_rows - 1);
            ret = AVERROR_INVALIDDATA;
            goto err;
        }

        pps->column_width = av_malloc_array(pps->num_tile_columns, sizeof(*pps->column_width));
        pps->row_height   = av_malloc_array(pps->num_tile_rows,    sizeof(*pps->row_height));
        if (!pps->column_width || !pps->row_height) {
            ret = AVERROR(ENOMEM);
            goto err;
        }

        pps->uniform_spacing_flag = get_bits1(gb);
        if (!pps->uniform_spacing_flag) {
1570
            uint64_t sum = 0;
1571
            for (i = 0; i < pps->num_tile_columns - 1; i++) {
1572
                pps->column_width[i] = get_ue_golomb_long(gb) + 1;
Guillaume Martres's avatar
Guillaume Martres committed
1573
                sum                 += pps->column_width[i];
1574 1575
            }
            if (sum >= sps->ctb_width) {
1576
                av_log(avctx, AV_LOG_ERROR, "Invalid tile widths.\n");
1577
                ret = AVERROR_INVALIDDATA;
1578 1579
                goto err;
            }
1580 1581 1582 1583
            pps->column_width[pps->num_tile_columns - 1] = sps->ctb_width - sum;

            sum = 0;
            for (i = 0; i < pps->num_tile_rows - 1; i++) {
1584
                pps->row_height[i] = get_ue_golomb_long(gb) + 1;
Guillaume Martres's avatar
Guillaume Martres committed
1585
                sum               += pps->row_height[i];
1586 1587
            }
            if (sum >= sps->ctb_height) {
1588
                av_log(avctx, AV_LOG_ERROR, "Invalid tile heights.\n");
1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601
                ret = AVERROR_INVALIDDATA;
                goto err;
            }
            pps->row_height[pps->num_tile_rows - 1] = sps->ctb_height - sum;
        }
        pps->loop_filter_across_tiles_enabled_flag = get_bits1(gb);
    }

    pps->seq_loop_filter_across_slices_enabled_flag = get_bits1(gb);

    pps->deblocking_filter_control_present_flag = get_bits1(gb);
    if (pps->deblocking_filter_control_present_flag) {
        pps->deblocking_filter_override_enabled_flag = get_bits1(gb);
Guillaume Martres's avatar
Guillaume Martres committed
1602
        pps->disable_dbf                             = get_bits1(gb);
1603
        if (!pps->disable_dbf) {
1604 1605 1606
            pps->beta_offset = get_se_golomb(gb) * 2;
            pps->tc_offset = get_se_golomb(gb) * 2;
            if (pps->beta_offset/2 < -6 || pps->beta_offset/2 > 6) {
1607
                av_log(avctx, AV_LOG_ERROR, "pps_beta_offset_div2 out of range: %d\n",
1608 1609 1610 1611 1612
                       pps->beta_offset/2);
                ret = AVERROR_INVALIDDATA;
                goto err;
            }
            if (pps->tc_offset/2 < -6 || pps->tc_offset/2 > 6) {
1613
                av_log(avctx, AV_LOG_ERROR, "pps_tc_offset_div2 out of range: %d\n",
1614 1615 1616 1617 1618 1619 1620
                       pps->tc_offset/2);
                ret = AVERROR_INVALIDDATA;
                goto err;
            }
        }
    }

Guillaume Martres's avatar
Guillaume Martres committed
1621 1622
    pps->scaling_list_data_present_flag = get_bits1(gb);
    if (pps->scaling_list_data_present_flag) {
1623
        set_default_scaling_list_data(&pps->scaling_list);
1624
        ret = scaling_list_data(gb, avctx, &pps->scaling_list, sps);
1625 1626 1627 1628
        if (ret < 0)
            goto err;
    }
    pps->lists_modification_present_flag = get_bits1(gb);
1629
    pps->log2_parallel_merge_level       = get_ue_golomb_long(gb) + 2;
1630
    if (pps->log2_parallel_merge_level > sps->log2_ctb_size) {
1631
        av_log(avctx, AV_LOG_ERROR, "log2_parallel_merge_level_minus2 out of range: %d\n",
1632 1633
               pps->log2_parallel_merge_level - 2);
        ret = AVERROR_INVALIDDATA;
1634 1635
        goto err;
    }
1636 1637

    pps->slice_header_extension_present_flag = get_bits1(gb);
1638 1639 1640 1641 1642

    if (get_bits1(gb)) { // pps_extension_present_flag
        int pps_range_extensions_flag = get_bits1(gb);
        /* int pps_extension_7bits = */ get_bits(gb, 7);
        if (sps->ptl.general_ptl.profile_idc == FF_PROFILE_HEVC_REXT && pps_range_extensions_flag) {
1643
            if ((ret = pps_range_extensions(gb, avctx, pps, sps)) < 0)
1644
                goto err;
1645 1646
        }
    }
1647

1648 1649
    ret = setup_pps(avctx, gb, pps, sps);
    if (ret < 0)
1650 1651
        goto err;

1652
    if (get_bits_left(gb) < 0) {
1653
        av_log(avctx, AV_LOG_ERROR,
1654 1655 1656 1657
               "Overread PPS by %d bits\n", -get_bits_left(gb));
        goto err;
    }

1658 1659
    remove_pps(ps, pps_id);
    ps->pps_list[pps_id] = pps_buf;
1660 1661 1662 1663 1664 1665 1666

    return 0;

err:
    av_buffer_unref(&pps_buf);
    return ret;
}