GCC Code Coverage Report
Directory: ../../../ffmpeg/ Exec Total Coverage
File: src/libavcodec/hevcdec.c Lines: 1707 2148 79.5 %
Date: 2019-11-22 03:34:36 Branches: 1239 1638 75.6 %

Line Branch Exec Source
1
/*
2
 * HEVC video Decoder
3
 *
4
 * Copyright (C) 2012 - 2013 Guillaume Martres
5
 * Copyright (C) 2012 - 2013 Mickael Raulet
6
 * Copyright (C) 2012 - 2013 Gildas Cocherel
7
 * Copyright (C) 2012 - 2013 Wassim Hamidouche
8
 *
9
 * This file is part of FFmpeg.
10
 *
11
 * FFmpeg is free software; you can redistribute it and/or
12
 * modify it under the terms of the GNU Lesser General Public
13
 * License as published by the Free Software Foundation; either
14
 * version 2.1 of the License, or (at your option) any later version.
15
 *
16
 * FFmpeg is distributed in the hope that it will be useful,
17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19
 * Lesser General Public License for more details.
20
 *
21
 * You should have received a copy of the GNU Lesser General Public
22
 * License along with FFmpeg; if not, write to the Free Software
23
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24
 */
25
26
#include "libavutil/attributes.h"
27
#include "libavutil/common.h"
28
#include "libavutil/display.h"
29
#include "libavutil/internal.h"
30
#include "libavutil/mastering_display_metadata.h"
31
#include "libavutil/md5.h"
32
#include "libavutil/opt.h"
33
#include "libavutil/pixdesc.h"
34
#include "libavutil/stereo3d.h"
35
36
#include "bswapdsp.h"
37
#include "bytestream.h"
38
#include "cabac_functions.h"
39
#include "golomb.h"
40
#include "hevc.h"
41
#include "hevc_data.h"
42
#include "hevc_parse.h"
43
#include "hevcdec.h"
44
#include "hwaccel.h"
45
#include "profiles.h"
46
47
const uint8_t ff_hevc_pel_weight[65] = { [2] = 0, [4] = 1, [6] = 2, [8] = 3, [12] = 4, [16] = 5, [24] = 6, [32] = 7, [48] = 8, [64] = 9 };
48
49
/**
50
 * NOTE: Each function hls_foo correspond to the function foo in the
51
 * specification (HLS stands for High Level Syntax).
52
 */
53
54
/**
55
 * Section 5.7
56
 */
57
58
/* free everything allocated  by pic_arrays_init() */
59
731
static void pic_arrays_free(HEVCContext *s)
60
{
61
731
    av_freep(&s->sao);
62
731
    av_freep(&s->deblock);
63
64
731
    av_freep(&s->skip_flag);
65
731
    av_freep(&s->tab_ct_depth);
66
67
731
    av_freep(&s->tab_ipm);
68
731
    av_freep(&s->cbf_luma);
69
731
    av_freep(&s->is_pcm);
70
71
731
    av_freep(&s->qp_y_tab);
72
731
    av_freep(&s->tab_slice_address);
73
731
    av_freep(&s->filter_slice_edges);
74
75
731
    av_freep(&s->horizontal_bs);
76
731
    av_freep(&s->vertical_bs);
77
78
731
    av_freep(&s->sh.entry_point_offset);
79
731
    av_freep(&s->sh.size);
80
731
    av_freep(&s->sh.offset);
81
82
731
    av_buffer_pool_uninit(&s->tab_mvf_pool);
83
731
    av_buffer_pool_uninit(&s->rpl_tab_pool);
84
731
}
85
86
/* allocate arrays that depend on frame dimensions */
87
366
static int pic_arrays_init(HEVCContext *s, const HEVCSPS *sps)
88
{
89
366
    int log2_min_cb_size = sps->log2_min_cb_size;
90
366
    int width            = sps->width;
91
366
    int height           = sps->height;
92
366
    int pic_size_in_ctb  = ((width  >> log2_min_cb_size) + 1) *
93
366
                           ((height >> log2_min_cb_size) + 1);
94
366
    int ctb_count        = sps->ctb_width * sps->ctb_height;
95
366
    int min_pu_size      = sps->min_pu_width * sps->min_pu_height;
96
97
366
    s->bs_width  = (width  >> 2) + 1;
98
366
    s->bs_height = (height >> 2) + 1;
99
100
366
    s->sao           = av_mallocz_array(ctb_count, sizeof(*s->sao));
101
366
    s->deblock       = av_mallocz_array(ctb_count, sizeof(*s->deblock));
102

366
    if (!s->sao || !s->deblock)
103
        goto fail;
104
105
366
    s->skip_flag    = av_malloc_array(sps->min_cb_height, sps->min_cb_width);
106
366
    s->tab_ct_depth = av_malloc_array(sps->min_cb_height, sps->min_cb_width);
107

366
    if (!s->skip_flag || !s->tab_ct_depth)
108
        goto fail;
109
110
366
    s->cbf_luma = av_malloc_array(sps->min_tb_width, sps->min_tb_height);
111
366
    s->tab_ipm  = av_mallocz(min_pu_size);
112
366
    s->is_pcm   = av_malloc_array(sps->min_pu_width + 1, sps->min_pu_height + 1);
113

366
    if (!s->tab_ipm || !s->cbf_luma || !s->is_pcm)
114
        goto fail;
115
116
366
    s->filter_slice_edges = av_mallocz(ctb_count);
117
366
    s->tab_slice_address  = av_malloc_array(pic_size_in_ctb,
118
                                      sizeof(*s->tab_slice_address));
119
366
    s->qp_y_tab           = av_malloc_array(pic_size_in_ctb,
120
                                      sizeof(*s->qp_y_tab));
121

366
    if (!s->qp_y_tab || !s->filter_slice_edges || !s->tab_slice_address)
122
        goto fail;
123
124
366
    s->horizontal_bs = av_mallocz_array(s->bs_width, s->bs_height);
125
366
    s->vertical_bs   = av_mallocz_array(s->bs_width, s->bs_height);
126

366
    if (!s->horizontal_bs || !s->vertical_bs)
127
        goto fail;
128
129
366
    s->tab_mvf_pool = av_buffer_pool_init(min_pu_size * sizeof(MvField),
130
                                          av_buffer_allocz);
131
366
    s->rpl_tab_pool = av_buffer_pool_init(ctb_count * sizeof(RefPicListTab),
132
                                          av_buffer_allocz);
133

366
    if (!s->tab_mvf_pool || !s->rpl_tab_pool)
134
        goto fail;
135
136
366
    return 0;
137
138
fail:
139
    pic_arrays_free(s);
140
    return AVERROR(ENOMEM);
141
}
142
143
1350
static int pred_weight_table(HEVCContext *s, GetBitContext *gb)
144
{
145
1350
    int i = 0;
146
1350
    int j = 0;
147
    uint8_t luma_weight_l0_flag[16];
148
    uint8_t chroma_weight_l0_flag[16];
149
    uint8_t luma_weight_l1_flag[16];
150
    uint8_t chroma_weight_l1_flag[16];
151
    int luma_log2_weight_denom;
152
153
1350
    luma_log2_weight_denom = get_ue_golomb_long(gb);
154

1350
    if (luma_log2_weight_denom < 0 || luma_log2_weight_denom > 7) {
155
        av_log(s->avctx, AV_LOG_ERROR, "luma_log2_weight_denom %d is invalid\n", luma_log2_weight_denom);
156
        return AVERROR_INVALIDDATA;
157
    }
158
1350
    s->sh.luma_log2_weight_denom = av_clip_uintp2(luma_log2_weight_denom, 3);
159
1350
    if (s->ps.sps->chroma_format_idc != 0) {
160
1350
        int64_t chroma_log2_weight_denom = luma_log2_weight_denom + (int64_t)get_se_golomb(gb);
161

1350
        if (chroma_log2_weight_denom < 0 || chroma_log2_weight_denom > 7) {
162
            av_log(s->avctx, AV_LOG_ERROR, "chroma_log2_weight_denom %"PRId64" is invalid\n", chroma_log2_weight_denom);
163
            return AVERROR_INVALIDDATA;
164
        }
165
1350
        s->sh.chroma_log2_weight_denom = chroma_log2_weight_denom;
166
    }
167
168
5650
    for (i = 0; i < s->sh.nb_refs[L0]; i++) {
169
4300
        luma_weight_l0_flag[i] = get_bits1(gb);
170
4300
        if (!luma_weight_l0_flag[i]) {
171
1183
            s->sh.luma_weight_l0[i] = 1 << s->sh.luma_log2_weight_denom;
172
1183
            s->sh.luma_offset_l0[i] = 0;
173
        }
174
    }
175
1350
    if (s->ps.sps->chroma_format_idc != 0) {
176
5650
        for (i = 0; i < s->sh.nb_refs[L0]; i++)
177
4300
            chroma_weight_l0_flag[i] = get_bits1(gb);
178
    } else {
179
        for (i = 0; i < s->sh.nb_refs[L0]; i++)
180
            chroma_weight_l0_flag[i] = 0;
181
    }
182
5650
    for (i = 0; i < s->sh.nb_refs[L0]; i++) {
183
4300
        if (luma_weight_l0_flag[i]) {
184
3117
            int delta_luma_weight_l0 = get_se_golomb(gb);
185
3117
            if ((int8_t)delta_luma_weight_l0 != delta_luma_weight_l0)
186
                return AVERROR_INVALIDDATA;
187
3117
            s->sh.luma_weight_l0[i] = (1 << s->sh.luma_log2_weight_denom) + delta_luma_weight_l0;
188
3117
            s->sh.luma_offset_l0[i] = get_se_golomb(gb);
189
        }
190
4300
        if (chroma_weight_l0_flag[i]) {
191
3393
            for (j = 0; j < 2; j++) {
192
2262
                int delta_chroma_weight_l0 = get_se_golomb(gb);
193
2262
                int delta_chroma_offset_l0 = get_se_golomb(gb);
194
195
2262
                if (   (int8_t)delta_chroma_weight_l0 != delta_chroma_weight_l0
196

2262
                    || delta_chroma_offset_l0 < -(1<<17) || delta_chroma_offset_l0 > (1<<17)) {
197
                    return AVERROR_INVALIDDATA;
198
                }
199
200
2262
                s->sh.chroma_weight_l0[i][j] = (1 << s->sh.chroma_log2_weight_denom) + delta_chroma_weight_l0;
201
2262
                s->sh.chroma_offset_l0[i][j] = av_clip((delta_chroma_offset_l0 - ((128 * s->sh.chroma_weight_l0[i][j])
202
2262
                                                                                    >> s->sh.chroma_log2_weight_denom) + 128), -128, 127);
203
            }
204
        } else {
205
3169
            s->sh.chroma_weight_l0[i][0] = 1 << s->sh.chroma_log2_weight_denom;
206
3169
            s->sh.chroma_offset_l0[i][0] = 0;
207
3169
            s->sh.chroma_weight_l0[i][1] = 1 << s->sh.chroma_log2_weight_denom;
208
3169
            s->sh.chroma_offset_l0[i][1] = 0;
209
        }
210
    }
211
1350
    if (s->sh.slice_type == HEVC_SLICE_B) {
212
1839
        for (i = 0; i < s->sh.nb_refs[L1]; i++) {
213
1256
            luma_weight_l1_flag[i] = get_bits1(gb);
214
1256
            if (!luma_weight_l1_flag[i]) {
215
510
                s->sh.luma_weight_l1[i] = 1 << s->sh.luma_log2_weight_denom;
216
510
                s->sh.luma_offset_l1[i] = 0;
217
            }
218
        }
219
583
        if (s->ps.sps->chroma_format_idc != 0) {
220
1839
            for (i = 0; i < s->sh.nb_refs[L1]; i++)
221
1256
                chroma_weight_l1_flag[i] = get_bits1(gb);
222
        } else {
223
            for (i = 0; i < s->sh.nb_refs[L1]; i++)
224
                chroma_weight_l1_flag[i] = 0;
225
        }
226
1839
        for (i = 0; i < s->sh.nb_refs[L1]; i++) {
227
1256
            if (luma_weight_l1_flag[i]) {
228
746
                int delta_luma_weight_l1 = get_se_golomb(gb);
229
746
                if ((int8_t)delta_luma_weight_l1 != delta_luma_weight_l1)
230
                    return AVERROR_INVALIDDATA;
231
746
                s->sh.luma_weight_l1[i] = (1 << s->sh.luma_log2_weight_denom) + delta_luma_weight_l1;
232
746
                s->sh.luma_offset_l1[i] = get_se_golomb(gb);
233
            }
234
1256
            if (chroma_weight_l1_flag[i]) {
235
708
                for (j = 0; j < 2; j++) {
236
472
                    int delta_chroma_weight_l1 = get_se_golomb(gb);
237
472
                    int delta_chroma_offset_l1 = get_se_golomb(gb);
238
239
472
                    if (   (int8_t)delta_chroma_weight_l1 != delta_chroma_weight_l1
240

472
                        || delta_chroma_offset_l1 < -(1<<17) || delta_chroma_offset_l1 > (1<<17)) {
241
                        return AVERROR_INVALIDDATA;
242
                    }
243
244
472
                    s->sh.chroma_weight_l1[i][j] = (1 << s->sh.chroma_log2_weight_denom) + delta_chroma_weight_l1;
245
472
                    s->sh.chroma_offset_l1[i][j] = av_clip((delta_chroma_offset_l1 - ((128 * s->sh.chroma_weight_l1[i][j])
246
472
                                                                                        >> s->sh.chroma_log2_weight_denom) + 128), -128, 127);
247
                }
248
            } else {
249
1020
                s->sh.chroma_weight_l1[i][0] = 1 << s->sh.chroma_log2_weight_denom;
250
1020
                s->sh.chroma_offset_l1[i][0] = 0;
251
1020
                s->sh.chroma_weight_l1[i][1] = 1 << s->sh.chroma_log2_weight_denom;
252
1020
                s->sh.chroma_offset_l1[i][1] = 0;
253
            }
254
        }
255
    }
256
1350
    return 0;
257
}
258
259
18961
static int decode_lt_rps(HEVCContext *s, LongTermRPS *rps, GetBitContext *gb)
260
{
261
18961
    const HEVCSPS *sps = s->ps.sps;
262
18961
    int max_poc_lsb    = 1 << sps->log2_max_poc_lsb;
263
18961
    int prev_delta_msb = 0;
264
18961
    unsigned int nb_sps = 0, nb_sh;
265
    int i;
266
267
18961
    rps->nb_refs = 0;
268
18961
    if (!sps->long_term_ref_pics_present_flag)
269
17927
        return 0;
270
271
1034
    if (sps->num_long_term_ref_pics_sps > 0)
272
499
        nb_sps = get_ue_golomb_long(gb);
273
1034
    nb_sh = get_ue_golomb_long(gb);
274
275
1034
    if (nb_sps > sps->num_long_term_ref_pics_sps)
276
        return AVERROR_INVALIDDATA;
277
1034
    if (nb_sh + (uint64_t)nb_sps > FF_ARRAY_ELEMS(rps->poc))
278
        return AVERROR_INVALIDDATA;
279
280
1034
    rps->nb_refs = nb_sh + nb_sps;
281
282
2594
    for (i = 0; i < rps->nb_refs; i++) {
283
        uint8_t delta_poc_msb_present;
284
285
1560
        if (i < nb_sps) {
286
184
            uint8_t lt_idx_sps = 0;
287
288
184
            if (sps->num_long_term_ref_pics_sps > 1)
289
184
                lt_idx_sps = get_bits(gb, av_ceil_log2(sps->num_long_term_ref_pics_sps));
290
291
184
            rps->poc[i]  = sps->lt_ref_pic_poc_lsb_sps[lt_idx_sps];
292
184
            rps->used[i] = sps->used_by_curr_pic_lt_sps_flag[lt_idx_sps];
293
        } else {
294
1376
            rps->poc[i]  = get_bits(gb, sps->log2_max_poc_lsb);
295
1376
            rps->used[i] = get_bits1(gb);
296
        }
297
298
1560
        delta_poc_msb_present = get_bits1(gb);
299
1560
        if (delta_poc_msb_present) {
300
48
            int64_t delta = get_ue_golomb_long(gb);
301
            int64_t poc;
302
303

48
            if (i && i != nb_sps)
304
31
                delta += prev_delta_msb;
305
306
48
            poc = rps->poc[i] + s->poc - delta * max_poc_lsb - s->sh.pic_order_cnt_lsb;
307
48
            if (poc != (int32_t)poc)
308
                return AVERROR_INVALIDDATA;
309
48
            rps->poc[i] = poc;
310
48
            prev_delta_msb = delta;
311
        }
312
    }
313
314
1034
    return 0;
315
}
316
317
541
static void export_stream_params(HEVCContext *s, const HEVCSPS *sps)
318
{
319
541
    AVCodecContext *avctx = s->avctx;
320
541
    const HEVCParamSets *ps = &s->ps;
321
541
    const HEVCVPS *vps = (const HEVCVPS*)ps->vps_list[sps->vps_id]->data;
322
541
    const HEVCWindow *ow = &sps->output_window;
323
541
    unsigned int num = 0, den = 0;
324
325
541
    avctx->pix_fmt             = sps->pix_fmt;
326
541
    avctx->coded_width         = sps->width;
327
541
    avctx->coded_height        = sps->height;
328
541
    avctx->width               = sps->width  - ow->left_offset - ow->right_offset;
329
541
    avctx->height              = sps->height - ow->top_offset  - ow->bottom_offset;
330
541
    avctx->has_b_frames        = sps->temporal_layer[sps->max_sub_layers - 1].num_reorder_pics;
331
541
    avctx->profile             = sps->ptl.general_ptl.profile_idc;
332
541
    avctx->level               = sps->ptl.general_ptl.level_idc;
333
334
541
    ff_set_sar(avctx, sps->vui.sar);
335
336
541
    if (sps->vui.video_signal_type_present_flag)
337
8
        avctx->color_range = sps->vui.video_full_range_flag ? AVCOL_RANGE_JPEG
338
8
                                                            : AVCOL_RANGE_MPEG;
339
    else
340
533
        avctx->color_range = AVCOL_RANGE_MPEG;
341
342
541
    if (sps->vui.colour_description_present_flag) {
343
8
        avctx->color_primaries = sps->vui.colour_primaries;
344
8
        avctx->color_trc       = sps->vui.transfer_characteristic;
345
8
        avctx->colorspace      = sps->vui.matrix_coeffs;
346
    } else {
347
533
        avctx->color_primaries = AVCOL_PRI_UNSPECIFIED;
348
533
        avctx->color_trc       = AVCOL_TRC_UNSPECIFIED;
349
533
        avctx->colorspace      = AVCOL_SPC_UNSPECIFIED;
350
    }
351
352
541
    if (vps->vps_timing_info_present_flag) {
353
20
        num = vps->vps_num_units_in_tick;
354
20
        den = vps->vps_time_scale;
355
521
    } else if (sps->vui.vui_timing_info_present_flag) {
356
24
        num = sps->vui.vui_num_units_in_tick;
357
24
        den = sps->vui.vui_time_scale;
358
    }
359
360

541
    if (num != 0 && den != 0)
361
44
        av_reduce(&avctx->framerate.den, &avctx->framerate.num,
362
                  num, den, 1 << 30);
363
364

541
    if (s->sei.alternative_transfer.present &&
365
        av_color_transfer_name(s->sei.alternative_transfer.preferred_transfer_characteristics) &&
366
        s->sei.alternative_transfer.preferred_transfer_characteristics != AVCOL_TRC_UNSPECIFIED) {
367
        avctx->color_trc = s->sei.alternative_transfer.preferred_transfer_characteristics;
368
    }
369
541
}
370
371
365
static enum AVPixelFormat get_format(HEVCContext *s, const HEVCSPS *sps)
372
{
373
#define HWACCEL_MAX (CONFIG_HEVC_DXVA2_HWACCEL + \
374
                     CONFIG_HEVC_D3D11VA_HWACCEL * 2 + \
375
                     CONFIG_HEVC_NVDEC_HWACCEL + \
376
                     CONFIG_HEVC_VAAPI_HWACCEL + \
377
                     CONFIG_HEVC_VIDEOTOOLBOX_HWACCEL + \
378
                     CONFIG_HEVC_VDPAU_HWACCEL)
379
365
    enum AVPixelFormat pix_fmts[HWACCEL_MAX + 2], *fmt = pix_fmts;
380
381

365
    switch (sps->pix_fmt) {
382
319
    case AV_PIX_FMT_YUV420P:
383
    case AV_PIX_FMT_YUVJ420P:
384
#if CONFIG_HEVC_DXVA2_HWACCEL
385
        *fmt++ = AV_PIX_FMT_DXVA2_VLD;
386
#endif
387
#if CONFIG_HEVC_D3D11VA_HWACCEL
388
        *fmt++ = AV_PIX_FMT_D3D11VA_VLD;
389
        *fmt++ = AV_PIX_FMT_D3D11;
390
#endif
391
#if CONFIG_HEVC_VAAPI_HWACCEL
392
319
        *fmt++ = AV_PIX_FMT_VAAPI;
393
#endif
394
#if CONFIG_HEVC_VDPAU_HWACCEL
395
319
        *fmt++ = AV_PIX_FMT_VDPAU;
396
#endif
397
#if CONFIG_HEVC_NVDEC_HWACCEL
398
        *fmt++ = AV_PIX_FMT_CUDA;
399
#endif
400
#if CONFIG_HEVC_VIDEOTOOLBOX_HWACCEL
401
        *fmt++ = AV_PIX_FMT_VIDEOTOOLBOX;
402
#endif
403
319
        break;
404
22
    case AV_PIX_FMT_YUV420P10:
405
#if CONFIG_HEVC_DXVA2_HWACCEL
406
        *fmt++ = AV_PIX_FMT_DXVA2_VLD;
407
#endif
408
#if CONFIG_HEVC_D3D11VA_HWACCEL
409
        *fmt++ = AV_PIX_FMT_D3D11VA_VLD;
410
        *fmt++ = AV_PIX_FMT_D3D11;
411
#endif
412
#if CONFIG_HEVC_VAAPI_HWACCEL
413
22
        *fmt++ = AV_PIX_FMT_VAAPI;
414
#endif
415
#if CONFIG_HEVC_VIDEOTOOLBOX_HWACCEL
416
        *fmt++ = AV_PIX_FMT_VIDEOTOOLBOX;
417
#endif
418
#if CONFIG_HEVC_NVDEC_HWACCEL
419
        *fmt++ = AV_PIX_FMT_CUDA;
420
#endif
421
22
        break;
422
2
    case AV_PIX_FMT_YUV444P:
423
#if CONFIG_HEVC_VDPAU_HWACCEL
424
2
        *fmt++ = AV_PIX_FMT_VDPAU;
425
#endif
426
#if CONFIG_HEVC_NVDEC_HWACCEL
427
        *fmt++ = AV_PIX_FMT_CUDA;
428
#endif
429
2
        break;
430
10
    case AV_PIX_FMT_YUV420P12:
431
    case AV_PIX_FMT_YUV444P10:
432
    case AV_PIX_FMT_YUV444P12:
433
#if CONFIG_HEVC_NVDEC_HWACCEL
434
        *fmt++ = AV_PIX_FMT_CUDA;
435
#endif
436
10
        break;
437
    }
438
439
365
    *fmt++ = sps->pix_fmt;
440
365
    *fmt = AV_PIX_FMT_NONE;
441
442
365
    return ff_thread_get_format(s->avctx, pix_fmts);
443
}
444
445
366
static int set_sps(HEVCContext *s, const HEVCSPS *sps,
446
                   enum AVPixelFormat pix_fmt)
447
{
448
    int ret, i;
449
450
366
    pic_arrays_free(s);
451
366
    s->ps.sps = NULL;
452
366
    s->ps.vps = NULL;
453
454
366
    if (!sps)
455
        return 0;
456
457
366
    ret = pic_arrays_init(s, sps);
458
366
    if (ret < 0)
459
        goto fail;
460
461
366
    export_stream_params(s, sps);
462
463
366
    s->avctx->pix_fmt = pix_fmt;
464
465
366
    ff_hevc_pred_init(&s->hpc,     sps->bit_depth);
466
366
    ff_hevc_dsp_init (&s->hevcdsp, sps->bit_depth);
467
366
    ff_videodsp_init (&s->vdsp,    sps->bit_depth);
468
469
1464
    for (i = 0; i < 3; i++) {
470
1098
        av_freep(&s->sao_pixel_buffer_h[i]);
471
1098
        av_freep(&s->sao_pixel_buffer_v[i]);
472
    }
473
474

366
    if (sps->sao_enabled && !s->avctx->hwaccel) {
475
313
        int c_count = (sps->chroma_format_idc != 0) ? 3 : 1;
476
        int c_idx;
477
478
1248
        for(c_idx = 0; c_idx < c_count; c_idx++) {
479
935
            int w = sps->width >> sps->hshift[c_idx];
480
935
            int h = sps->height >> sps->vshift[c_idx];
481
935
            s->sao_pixel_buffer_h[c_idx] =
482
935
                av_malloc((w * 2 * sps->ctb_height) <<
483
935
                          sps->pixel_shift);
484
935
            s->sao_pixel_buffer_v[c_idx] =
485
935
                av_malloc((h * 2 * sps->ctb_width) <<
486
935
                          sps->pixel_shift);
487
        }
488
    }
489
490
366
    s->ps.sps = sps;
491
366
    s->ps.vps = (HEVCVPS*) s->ps.vps_list[s->ps.sps->vps_id]->data;
492
493
366
    return 0;
494
495
fail:
496
    pic_arrays_free(s);
497
    s->ps.sps = NULL;
498
    return ret;
499
}
500
501
27511
static int hls_slice_header(HEVCContext *s)
502
{
503
27511
    GetBitContext *gb = &s->HEVClc->gb;
504
27511
    SliceHeader *sh   = &s->sh;
505
    int i, ret;
506
507
    // Coded parameters
508
27511
    sh->first_slice_in_pic_flag = get_bits1(gb);
509

27511
    if (s->ref && sh->first_slice_in_pic_flag) {
510
1
        av_log(s->avctx, AV_LOG_ERROR, "Two slices reporting being the first in the same frame.\n");
511
1
        return 1; // This slice will be skipped later, do not corrupt state
512
    }
513
514



27510
    if ((IS_IDR(s) || IS_BLA(s)) && sh->first_slice_in_pic_flag) {
515
376
        s->seq_decode = (s->seq_decode + 1) & 0xff;
516
376
        s->max_ra     = INT_MAX;
517

376
        if (IS_IDR(s))
518
366
            ff_hevc_clear_refs(s);
519
    }
520
27510
    sh->no_output_of_prior_pics_flag = 0;
521

27510
    if (IS_IRAP(s))
522
1252
        sh->no_output_of_prior_pics_flag = get_bits1(gb);
523
524
27510
    sh->pps_id = get_ue_golomb_long(gb);
525

27510
    if (sh->pps_id >= HEVC_MAX_PPS_COUNT || !s->ps.pps_list[sh->pps_id]) {
526
22
        av_log(s->avctx, AV_LOG_ERROR, "PPS id out of range: %d\n", sh->pps_id);
527
22
        return AVERROR_INVALIDDATA;
528
    }
529
27488
    if (!sh->first_slice_in_pic_flag &&
530
17981
        s->ps.pps != (HEVCPPS*)s->ps.pps_list[sh->pps_id]->data) {
531
        av_log(s->avctx, AV_LOG_ERROR, "PPS changed between slices.\n");
532
        return AVERROR_INVALIDDATA;
533
    }
534
27488
    s->ps.pps = (HEVCPPS*)s->ps.pps_list[sh->pps_id]->data;
535

27488
    if (s->nal_unit_type == HEVC_NAL_CRA_NUT && s->last_eos == 1)
536
6
        sh->no_output_of_prior_pics_flag = 1;
537
538
27488
    if (s->ps.sps != (HEVCSPS*)s->ps.sps_list[s->ps.pps->sps_id]->data) {
539
365
        const HEVCSPS *sps = (HEVCSPS*)s->ps.sps_list[s->ps.pps->sps_id]->data;
540
365
        const HEVCSPS *last_sps = s->ps.sps;
541
        enum AVPixelFormat pix_fmt;
542
543


365
        if (last_sps && IS_IRAP(s) && s->nal_unit_type != HEVC_NAL_CRA_NUT) {
544

2
            if (sps->width != last_sps->width || sps->height != last_sps->height ||
545
2
                sps->temporal_layer[sps->max_sub_layers - 1].max_dec_pic_buffering !=
546
2
                last_sps->temporal_layer[last_sps->max_sub_layers - 1].max_dec_pic_buffering)
547
                sh->no_output_of_prior_pics_flag = 0;
548
        }
549
365
        ff_hevc_clear_refs(s);
550
551
365
        ret = set_sps(s, sps, sps->pix_fmt);
552
365
        if (ret < 0)
553
            return ret;
554
555
365
        pix_fmt = get_format(s, sps);
556
365
        if (pix_fmt < 0)
557
            return pix_fmt;
558
365
        s->avctx->pix_fmt = pix_fmt;
559
560
365
        s->seq_decode = (s->seq_decode + 1) & 0xff;
561
365
        s->max_ra     = INT_MAX;
562
    }
563
564
27488
    sh->dependent_slice_segment_flag = 0;
565
27488
    if (!sh->first_slice_in_pic_flag) {
566
        int slice_address_length;
567
568
17981
        if (s->ps.pps->dependent_slice_segments_enabled_flag)
569
9092
            sh->dependent_slice_segment_flag = get_bits1(gb);
570
571
17981
        slice_address_length = av_ceil_log2(s->ps.sps->ctb_width *
572
17981
                                            s->ps.sps->ctb_height);
573
17981
        sh->slice_segment_addr = get_bitsz(gb, slice_address_length);
574
17981
        if (sh->slice_segment_addr >= s->ps.sps->ctb_width * s->ps.sps->ctb_height) {
575
            av_log(s->avctx, AV_LOG_ERROR,
576
                   "Invalid slice segment address: %u.\n",
577
                   sh->slice_segment_addr);
578
            return AVERROR_INVALIDDATA;
579
        }
580
581
17981
        if (!sh->dependent_slice_segment_flag) {
582
10034
            sh->slice_addr = sh->slice_segment_addr;
583
10034
            s->slice_idx++;
584
        }
585
    } else {
586
9507
        sh->slice_segment_addr = sh->slice_addr = 0;
587
9507
        s->slice_idx           = 0;
588
9507
        s->slice_initialized   = 0;
589
    }
590
591
27488
    if (!sh->dependent_slice_segment_flag) {
592
19541
        s->slice_initialized = 0;
593
594
19545
        for (i = 0; i < s->ps.pps->num_extra_slice_header_bits; i++)
595
4
            skip_bits(gb, 1);  // slice_reserved_undetermined_flag[]
596
597
19541
        sh->slice_type = get_ue_golomb_long(gb);
598
19541
        if (!(sh->slice_type == HEVC_SLICE_I ||
599
18427
              sh->slice_type == HEVC_SLICE_P ||
600
14687
              sh->slice_type == HEVC_SLICE_B)) {
601
            av_log(s->avctx, AV_LOG_ERROR, "Unknown slice type: %d.\n",
602
                   sh->slice_type);
603
            return AVERROR_INVALIDDATA;
604
        }
605

19541
        if (IS_IRAP(s) && sh->slice_type != HEVC_SLICE_I) {
606
            av_log(s->avctx, AV_LOG_ERROR, "Inter slices in an IRAP frame.\n");
607
            return AVERROR_INVALIDDATA;
608
        }
609
610
        // when flag is not present, picture is inferred to be output
611
19541
        sh->pic_output_flag = 1;
612
19541
        if (s->ps.pps->output_flag_present_flag)
613
703
            sh->pic_output_flag = get_bits1(gb);
614
615
19541
        if (s->ps.sps->separate_colour_plane_flag)
616
            sh->colour_plane_id = get_bits(gb, 2);
617
618

38502
        if (!IS_IDR(s)) {
619
            int poc, pos;
620
621
18961
            sh->pic_order_cnt_lsb = get_bits(gb, s->ps.sps->log2_max_poc_lsb);
622
18961
            poc = ff_hevc_compute_poc(s->ps.sps, s->pocTid0, sh->pic_order_cnt_lsb, s->nal_unit_type);
623

18961
            if (!sh->first_slice_in_pic_flag && poc != s->poc) {
624
                av_log(s->avctx, AV_LOG_WARNING,
625
                       "Ignoring POC change between slices: %d -> %d\n", s->poc, poc);
626
                if (s->avctx->err_recognition & AV_EF_EXPLODE)
627
                    return AVERROR_INVALIDDATA;
628
                poc = s->poc;
629
            }
630
18961
            s->poc = poc;
631
632
18961
            sh->short_term_ref_pic_set_sps_flag = get_bits1(gb);
633
18961
            pos = get_bits_left(gb);
634
18961
            if (!sh->short_term_ref_pic_set_sps_flag) {
635
3149
                ret = ff_hevc_decode_short_term_rps(gb, s->avctx, &sh->slice_rps, s->ps.sps, 1);
636
3149
                if (ret < 0)
637
                    return ret;
638
639
3149
                sh->short_term_rps = &sh->slice_rps;
640
            } else {
641
                int numbits, rps_idx;
642
643
15812
                if (!s->ps.sps->nb_st_rps) {
644
                    av_log(s->avctx, AV_LOG_ERROR, "No ref lists in the SPS.\n");
645
                    return AVERROR_INVALIDDATA;
646
                }
647
648
15812
                numbits = av_ceil_log2(s->ps.sps->nb_st_rps);
649
15812
                rps_idx = numbits > 0 ? get_bits(gb, numbits) : 0;
650
15812
                sh->short_term_rps = &s->ps.sps->st_rps[rps_idx];
651
            }
652
18961
            sh->short_term_ref_pic_set_size = pos - get_bits_left(gb);
653
654
18961
            pos = get_bits_left(gb);
655
18961
            ret = decode_lt_rps(s, &sh->long_term_rps, gb);
656
18961
            if (ret < 0) {
657
                av_log(s->avctx, AV_LOG_WARNING, "Invalid long term RPS.\n");
658
                if (s->avctx->err_recognition & AV_EF_EXPLODE)
659
                    return AVERROR_INVALIDDATA;
660
            }
661
18961
            sh->long_term_ref_pic_set_size = pos - get_bits_left(gb);
662
663
18961
            if (s->ps.sps->sps_temporal_mvp_enabled_flag)
664
16530
                sh->slice_temporal_mvp_enabled_flag = get_bits1(gb);
665
            else
666
2431
                sh->slice_temporal_mvp_enabled_flag = 0;
667
        } else {
668
580
            s->sh.short_term_rps = NULL;
669
580
            s->poc               = 0;
670
        }
671
672
        /* 8.3.1 */
673

19541
        if (sh->first_slice_in_pic_flag && s->temporal_id == 0 &&
674
9164
            s->nal_unit_type != HEVC_NAL_TRAIL_N &&
675
6705
            s->nal_unit_type != HEVC_NAL_TSA_N   &&
676
6705
            s->nal_unit_type != HEVC_NAL_STSA_N  &&
677
6705
            s->nal_unit_type != HEVC_NAL_RADL_N  &&
678
6683
            s->nal_unit_type != HEVC_NAL_RADL_R  &&
679
6664
            s->nal_unit_type != HEVC_NAL_RASL_N  &&
680
6314
            s->nal_unit_type != HEVC_NAL_RASL_R)
681
5852
            s->pocTid0 = s->poc;
682
683
19541
        if (s->ps.sps->sao_enabled) {
684
16092
            sh->slice_sample_adaptive_offset_flag[0] = get_bits1(gb);
685
16092
            if (s->ps.sps->chroma_format_idc) {
686
16090
                sh->slice_sample_adaptive_offset_flag[1] =
687
16090
                sh->slice_sample_adaptive_offset_flag[2] = get_bits1(gb);
688
            }
689
        } else {
690
3449
            sh->slice_sample_adaptive_offset_flag[0] = 0;
691
3449
            sh->slice_sample_adaptive_offset_flag[1] = 0;
692
3449
            sh->slice_sample_adaptive_offset_flag[2] = 0;
693
        }
694
695
19541
        sh->nb_refs[L0] = sh->nb_refs[L1] = 0;
696

19541
        if (sh->slice_type == HEVC_SLICE_P || sh->slice_type == HEVC_SLICE_B) {
697
            int nb_refs;
698
699
18427
            sh->nb_refs[L0] = s->ps.pps->num_ref_idx_l0_default_active;
700
18427
            if (sh->slice_type == HEVC_SLICE_B)
701
14687
                sh->nb_refs[L1] = s->ps.pps->num_ref_idx_l1_default_active;
702
703
18427
            if (get_bits1(gb)) { // num_ref_idx_active_override_flag
704
4860
                sh->nb_refs[L0] = get_ue_golomb_long(gb) + 1;
705
4860
                if (sh->slice_type == HEVC_SLICE_B)
706
2699
                    sh->nb_refs[L1] = get_ue_golomb_long(gb) + 1;
707
            }
708

18427
            if (sh->nb_refs[L0] > HEVC_MAX_REFS || sh->nb_refs[L1] > HEVC_MAX_REFS) {
709
                av_log(s->avctx, AV_LOG_ERROR, "Too many refs: %d/%d.\n",
710
                       sh->nb_refs[L0], sh->nb_refs[L1]);
711
                return AVERROR_INVALIDDATA;
712
            }
713
714
18427
            sh->rpl_modification_flag[0] = 0;
715
18427
            sh->rpl_modification_flag[1] = 0;
716
18427
            nb_refs = ff_hevc_frame_nb_refs(s);
717
18427
            if (!nb_refs) {
718
                av_log(s->avctx, AV_LOG_ERROR, "Zero refs for a frame with P or B slices.\n");
719
                return AVERROR_INVALIDDATA;
720
            }
721
722

18427
            if (s->ps.pps->lists_modification_present_flag && nb_refs > 1) {
723
1596
                sh->rpl_modification_flag[0] = get_bits1(gb);
724
1596
                if (sh->rpl_modification_flag[0]) {
725
3955
                    for (i = 0; i < sh->nb_refs[L0]; i++)
726
2779
                        sh->list_entry_lx[0][i] = get_bits(gb, av_ceil_log2(nb_refs));
727
                }
728
729
1596
                if (sh->slice_type == HEVC_SLICE_B) {
730
1596
                    sh->rpl_modification_flag[1] = get_bits1(gb);
731
1596
                    if (sh->rpl_modification_flag[1] == 1)
732
2207
                        for (i = 0; i < sh->nb_refs[L1]; i++)
733
1555
                            sh->list_entry_lx[1][i] = get_bits(gb, av_ceil_log2(nb_refs));
734
                }
735
            }
736
737
18427
            if (sh->slice_type == HEVC_SLICE_B)
738
14687
                sh->mvd_l1_zero_flag = get_bits1(gb);
739
740
18427
            if (s->ps.pps->cabac_init_present_flag)
741
15792
                sh->cabac_init_flag = get_bits1(gb);
742
            else
743
2635
                sh->cabac_init_flag = 0;
744
745
18427
            sh->collocated_ref_idx = 0;
746
18427
            if (sh->slice_temporal_mvp_enabled_flag) {
747
16090
                sh->collocated_list = L0;
748
16090
                if (sh->slice_type == HEVC_SLICE_B)
749
14185
                    sh->collocated_list = !get_bits1(gb);
750
751
16090
                if (sh->nb_refs[sh->collocated_list] > 1) {
752
14395
                    sh->collocated_ref_idx = get_ue_golomb_long(gb);
753
14395
                    if (sh->collocated_ref_idx >= sh->nb_refs[sh->collocated_list]) {
754
                        av_log(s->avctx, AV_LOG_ERROR,
755
                               "Invalid collocated_ref_idx: %d.\n",
756
                               sh->collocated_ref_idx);
757
                        return AVERROR_INVALIDDATA;
758
                    }
759
                }
760
            }
761
762

18427
            if ((s->ps.pps->weighted_pred_flag   && sh->slice_type == HEVC_SLICE_P) ||
763

17660
                (s->ps.pps->weighted_bipred_flag && sh->slice_type == HEVC_SLICE_B)) {
764
1350
                int ret = pred_weight_table(s, gb);
765
1350
                if (ret < 0)
766
                    return ret;
767
            }
768
769
18427
            sh->max_num_merge_cand = 5 - get_ue_golomb_long(gb);
770

18427
            if (sh->max_num_merge_cand < 1 || sh->max_num_merge_cand > 5) {
771
                av_log(s->avctx, AV_LOG_ERROR,
772
                       "Invalid number of merging MVP candidates: %d.\n",
773
                       sh->max_num_merge_cand);
774
                return AVERROR_INVALIDDATA;
775
            }
776
        }
777
778
19541
        sh->slice_qp_delta = get_se_golomb(gb);
779
780
19541
        if (s->ps.pps->pic_slice_level_chroma_qp_offsets_present_flag) {
781
250
            sh->slice_cb_qp_offset = get_se_golomb(gb);
782
250
            sh->slice_cr_qp_offset = get_se_golomb(gb);
783
        } else {
784
19291
            sh->slice_cb_qp_offset = 0;
785
19291
            sh->slice_cr_qp_offset = 0;
786
        }
787
788
19541
        if (s->ps.pps->chroma_qp_offset_list_enabled_flag)
789
43
            sh->cu_chroma_qp_offset_enabled_flag = get_bits1(gb);
790
        else
791
19498
            sh->cu_chroma_qp_offset_enabled_flag = 0;
792
793
19541
        if (s->ps.pps->deblocking_filter_control_present_flag) {
794
2829
            int deblocking_filter_override_flag = 0;
795
796
2829
            if (s->ps.pps->deblocking_filter_override_enabled_flag)
797
609
                deblocking_filter_override_flag = get_bits1(gb);
798
799
2829
            if (deblocking_filter_override_flag) {
800
390
                sh->disable_deblocking_filter_flag = get_bits1(gb);
801
390
                if (!sh->disable_deblocking_filter_flag) {
802
270
                    int beta_offset_div2 = get_se_golomb(gb);
803
270
                    int tc_offset_div2   = get_se_golomb(gb) ;
804

270
                    if (beta_offset_div2 < -6 || beta_offset_div2 > 6 ||
805
270
                        tc_offset_div2   < -6 || tc_offset_div2   > 6) {
806
                        av_log(s->avctx, AV_LOG_ERROR,
807
                            "Invalid deblock filter offsets: %d, %d\n",
808
                            beta_offset_div2, tc_offset_div2);
809
                        return AVERROR_INVALIDDATA;
810
                    }
811
270
                    sh->beta_offset = beta_offset_div2 * 2;
812
270
                    sh->tc_offset   =   tc_offset_div2 * 2;
813
                }
814
            } else {
815
2439
                sh->disable_deblocking_filter_flag = s->ps.pps->disable_dbf;
816
2439
                sh->beta_offset                    = s->ps.pps->beta_offset;
817
2439
                sh->tc_offset                      = s->ps.pps->tc_offset;
818
            }
819
        } else {
820
16712
            sh->disable_deblocking_filter_flag = 0;
821
16712
            sh->beta_offset                    = 0;
822
16712
            sh->tc_offset                      = 0;
823
        }
824
825
19541
        if (s->ps.pps->seq_loop_filter_across_slices_enabled_flag &&
826
17008
            (sh->slice_sample_adaptive_offset_flag[0] ||
827
11403
             sh->slice_sample_adaptive_offset_flag[1] ||
828
11385
             !sh->disable_deblocking_filter_flag)) {
829
16945
            sh->slice_loop_filter_across_slices_enabled_flag = get_bits1(gb);
830
        } else {
831
2596
            sh->slice_loop_filter_across_slices_enabled_flag = s->ps.pps->seq_loop_filter_across_slices_enabled_flag;
832
        }
833
7947
    } else if (!s->slice_initialized) {
834
        av_log(s->avctx, AV_LOG_ERROR, "Independent slice segment missing.\n");
835
        return AVERROR_INVALIDDATA;
836
    }
837
838
27488
    sh->num_entry_point_offsets = 0;
839

27488
    if (s->ps.pps->tiles_enabled_flag || s->ps.pps->entropy_coding_sync_enabled_flag) {
840
7411
        unsigned num_entry_point_offsets = get_ue_golomb_long(gb);
841
        // It would be possible to bound this tighter but this here is simpler
842
7411
        if (num_entry_point_offsets > get_bits_left(gb)) {
843
            av_log(s->avctx, AV_LOG_ERROR, "num_entry_point_offsets %d is invalid\n", num_entry_point_offsets);
844
            return AVERROR_INVALIDDATA;
845
        }
846
847
7411
        sh->num_entry_point_offsets = num_entry_point_offsets;
848
7411
        if (sh->num_entry_point_offsets > 0) {
849
1945
            int offset_len = get_ue_golomb_long(gb) + 1;
850
851

1945
            if (offset_len < 1 || offset_len > 32) {
852
                sh->num_entry_point_offsets = 0;
853
                av_log(s->avctx, AV_LOG_ERROR, "offset_len %d is invalid\n", offset_len);
854
                return AVERROR_INVALIDDATA;
855
            }
856
857
1945
            av_freep(&sh->entry_point_offset);
858
1945
            av_freep(&sh->offset);
859
1945
            av_freep(&sh->size);
860
1945
            sh->entry_point_offset = av_malloc_array(sh->num_entry_point_offsets, sizeof(unsigned));
861
1945
            sh->offset = av_malloc_array(sh->num_entry_point_offsets, sizeof(int));
862
1945
            sh->size = av_malloc_array(sh->num_entry_point_offsets, sizeof(int));
863

1945
            if (!sh->entry_point_offset || !sh->offset || !sh->size) {
864
                sh->num_entry_point_offsets = 0;
865
                av_log(s->avctx, AV_LOG_ERROR, "Failed to allocate memory\n");
866
                return AVERROR(ENOMEM);
867
            }
868
9525
            for (i = 0; i < sh->num_entry_point_offsets; i++) {
869
7580
                unsigned val = get_bits_long(gb, offset_len);
870
7580
                sh->entry_point_offset[i] = val + 1; // +1; // +1 to get the size
871
            }
872

1945
            if (s->threads_number > 1 && (s->ps.pps->num_tile_rows > 1 || s->ps.pps->num_tile_columns > 1)) {
873
                s->enable_parallel_tiles = 0; // TODO: you can enable tiles in parallel here
874
                s->threads_number = 1;
875
            } else
876
1945
                s->enable_parallel_tiles = 0;
877
        } else
878
5466
            s->enable_parallel_tiles = 0;
879
    }
880
881
27488
    if (s->ps.pps->slice_header_extension_present_flag) {
882
2765
        unsigned int length = get_ue_golomb_long(gb);
883
2765
        if (length*8LL > get_bits_left(gb)) {
884
            av_log(s->avctx, AV_LOG_ERROR, "too many slice_header_extension_data_bytes\n");
885
            return AVERROR_INVALIDDATA;
886
        }
887
23029
        for (i = 0; i < length; i++)
888
20264
            skip_bits(gb, 8);  // slice_header_extension_data_byte
889
    }
890
891
    // Inferred parameters
892
27488
    sh->slice_qp = 26U + s->ps.pps->pic_init_qp_minus26 + sh->slice_qp_delta;
893
27488
    if (sh->slice_qp > 51 ||
894
27488
        sh->slice_qp < -s->ps.sps->qp_bd_offset) {
895
        av_log(s->avctx, AV_LOG_ERROR,
896
               "The slice_qp %d is outside the valid range "
897
               "[%d, 51].\n",
898
               sh->slice_qp,
899
               -s->ps.sps->qp_bd_offset);
900
        return AVERROR_INVALIDDATA;
901
    }
902
903
27488
    sh->slice_ctb_addr_rs = sh->slice_segment_addr;
904
905

27488
    if (!s->sh.slice_ctb_addr_rs && s->sh.dependent_slice_segment_flag) {
906
        av_log(s->avctx, AV_LOG_ERROR, "Impossible slice segment.\n");
907
        return AVERROR_INVALIDDATA;
908
    }
909
910
27488
    if (get_bits_left(gb) < 0) {
911
        av_log(s->avctx, AV_LOG_ERROR,
912
               "Overread slice header by %d bits\n", -get_bits_left(gb));
913
        return AVERROR_INVALIDDATA;
914
    }
915
916
27488
    s->HEVClc->first_qp_group = !s->sh.dependent_slice_segment_flag;
917
918
27488
    if (!s->ps.pps->cu_qp_delta_enabled_flag)
919
22560
        s->HEVClc->qp_y = s->sh.slice_qp;
920
921
27488
    s->slice_initialized = 1;
922
27488
    s->HEVClc->tu.cu_qp_offset_cb = 0;
923
27488
    s->HEVClc->tu.cu_qp_offset_cr = 0;
924
925
27488
    return 0;
926
}
927
928
#define CTB(tab, x, y) ((tab)[(y) * s->ps.sps->ctb_width + (x)])
929
930
#define SET_SAO(elem, value)                            \
931
do {                                                    \
932
    if (!sao_merge_up_flag && !sao_merge_left_flag)     \
933
        sao->elem = value;                              \
934
    else if (sao_merge_left_flag)                       \
935
        sao->elem = CTB(s->sao, rx-1, ry).elem;         \
936
    else if (sao_merge_up_flag)                         \
937
        sao->elem = CTB(s->sao, rx, ry-1).elem;         \
938
    else                                                \
939
        sao->elem = 0;                                  \
940
} while (0)
941
942
1377404
static void hls_sao_param(HEVCContext *s, int rx, int ry)
943
{
944
1377404
    HEVCLocalContext *lc    = s->HEVClc;
945
1377404
    int sao_merge_left_flag = 0;
946
1377404
    int sao_merge_up_flag   = 0;
947
1377404
    SAOParams *sao          = &CTB(s->sao, rx, ry);
948
    int c_idx, i;
949
950
1377404
    if (s->sh.slice_sample_adaptive_offset_flag[0] ||
951
799850
        s->sh.slice_sample_adaptive_offset_flag[1]) {
952
579198
        if (rx > 0) {
953
548799
            if (lc->ctb_left_flag)
954
540356
                sao_merge_left_flag = ff_hevc_sao_merge_flag_decode(s);
955
        }
956

579198
        if (ry > 0 && !sao_merge_left_flag) {
957
258412
            if (lc->ctb_up_flag)
958
245064
                sao_merge_up_flag = ff_hevc_sao_merge_flag_decode(s);
959
        }
960
    }
961
962

5509520
    for (c_idx = 0; c_idx < (s->ps.sps->chroma_format_idc ? 3 : 1); c_idx++) {
963
4132116
        int log2_sao_offset_scale = c_idx == 0 ? s->ps.pps->log2_sao_offset_scale_luma :
964
2754712
                                                 s->ps.pps->log2_sao_offset_scale_chroma;
965
966
4132116
        if (!s->sh.slice_sample_adaptive_offset_flag[c_idx]) {
967
2896526
            sao->type_idx[c_idx] = SAO_NOT_APPLIED;
968
2896526
            continue;
969
        }
970
971
1235590
        if (c_idx == 2) {
972
329018
            sao->type_idx[2] = sao->type_idx[1];
973
329018
            sao->eo_class[2] = sao->eo_class[1];
974
        } else {
975


906572
            SET_SAO(type_idx[c_idx], ff_hevc_sao_type_idx_decode(s));
976
        }
977
978
1235590
        if (sao->type_idx[c_idx] == SAO_NOT_APPLIED)
979
745770
            continue;
980
981
2449100
        for (i = 0; i < 4; i++)
982


1959280
            SET_SAO(offset_abs[c_idx][i], ff_hevc_sao_offset_abs_decode(s));
983
984
489820
        if (sao->type_idx[c_idx] == SAO_BAND) {
985
579255
            for (i = 0; i < 4; i++) {
986
463404
                if (sao->offset_abs[c_idx][i]) {
987


350013
                    SET_SAO(offset_sign[c_idx][i],
988
                            ff_hevc_sao_offset_sign_decode(s));
989
                } else {
990
113391
                    sao->offset_sign[c_idx][i] = 0;
991
                }
992
            }
993


115851
            SET_SAO(band_position[c_idx], ff_hevc_sao_band_position_decode(s));
994
373969
        } else if (c_idx != 2) {
995


299275
            SET_SAO(eo_class[c_idx], ff_hevc_sao_eo_class_decode(s));
996
        }
997
998
        // Inferred parameters
999
489820
        sao->offset_val[c_idx][0] = 0;
1000
2449100
        for (i = 0; i < 4; i++) {
1001
1959280
            sao->offset_val[c_idx][i + 1] = sao->offset_abs[c_idx][i];
1002
1959280
            if (sao->type_idx[c_idx] == SAO_EDGE) {
1003
1495876
                if (i > 1)
1004
747938
                    sao->offset_val[c_idx][i + 1] = -sao->offset_val[c_idx][i + 1];
1005
463404
            } else if (sao->offset_sign[c_idx][i]) {
1006
154231
                sao->offset_val[c_idx][i + 1] = -sao->offset_val[c_idx][i + 1];
1007
            }
1008
1959280
            sao->offset_val[c_idx][i + 1] *= 1 << log2_sao_offset_scale;
1009
        }
1010
    }
1011
1377404
}
1012
1013
#undef SET_SAO
1014
#undef CTB
1015
1016
384846
static int hls_cross_component_pred(HEVCContext *s, int idx) {
1017
384846
    HEVCLocalContext *lc    = s->HEVClc;
1018
384846
    int log2_res_scale_abs_plus1 = ff_hevc_log2_res_scale_abs(s, idx);
1019
1020
384846
    if (log2_res_scale_abs_plus1 !=  0) {
1021
235843
        int res_scale_sign_flag = ff_hevc_res_scale_sign_flag(s, idx);
1022
235843
        lc->tu.res_scale_val = (1 << (log2_res_scale_abs_plus1 - 1)) *
1023
235843
                               (1 - 2 * res_scale_sign_flag);
1024
    } else {
1025
149003
        lc->tu.res_scale_val = 0;
1026
    }
1027
1028
1029
384846
    return 0;
1030
}
1031
1032
15053834
static int hls_transform_unit(HEVCContext *s, int x0, int y0,
1033
                              int xBase, int yBase, int cb_xBase, int cb_yBase,
1034
                              int log2_cb_size, int log2_trafo_size,
1035
                              int blk_idx, int cbf_luma, int *cbf_cb, int *cbf_cr)
1036
{
1037
15053834
    HEVCLocalContext *lc = s->HEVClc;
1038
15053834
    const int log2_trafo_size_c = log2_trafo_size - s->ps.sps->hshift[1];
1039
    int i;
1040
1041
15053834
    if (lc->cu.pred_mode == MODE_INTRA) {
1042
9461909
        int trafo_size = 1 << log2_trafo_size;
1043
9461909
        ff_hevc_set_neighbour_available(s, x0, y0, trafo_size, trafo_size);
1044
1045
9461909
        s->hpc.intra_pred[log2_trafo_size - 2](s, x0, y0, 0);
1046
    }
1047
1048

15053834
    if (cbf_luma || cbf_cb[0] || cbf_cr[0] ||
1049

15115622
        (s->ps.sps->chroma_format_idc == 2 && (cbf_cb[1] || cbf_cr[1]))) {
1050
10623604
        int scan_idx   = SCAN_DIAG;
1051
10623604
        int scan_idx_c = SCAN_DIAG;
1052

16327302
        int cbf_chroma = cbf_cb[0] || cbf_cr[0] ||
1053
5703698
                         (s->ps.sps->chroma_format_idc == 2 &&
1054

227786
                         (cbf_cb[1] || cbf_cr[1]));
1055
1056

10623604
        if (s->ps.pps->cu_qp_delta_enabled_flag && !lc->tu.is_cu_qp_delta_coded) {
1057
543293
            lc->tu.cu_qp_delta = ff_hevc_cu_qp_delta_abs(s);
1058
543293
            if (lc->tu.cu_qp_delta != 0)
1059
353071
                if (ff_hevc_cu_qp_delta_sign_flag(s) == 1)
1060
194965
                    lc->tu.cu_qp_delta = -lc->tu.cu_qp_delta;
1061
543293
            lc->tu.is_cu_qp_delta_coded = 1;
1062
1063
543293
            if (lc->tu.cu_qp_delta < -(26 + s->ps.sps->qp_bd_offset / 2) ||
1064
543292
                lc->tu.cu_qp_delta >  (25 + s->ps.sps->qp_bd_offset / 2)) {
1065
1
                av_log(s->avctx, AV_LOG_ERROR,
1066
                       "The cu_qp_delta %d is outside the valid range "
1067
                       "[%d, %d].\n",
1068
                       lc->tu.cu_qp_delta,
1069
1
                       -(26 + s->ps.sps->qp_bd_offset / 2),
1070
1
                        (25 + s->ps.sps->qp_bd_offset / 2));
1071
1
                return AVERROR_INVALIDDATA;
1072
            }
1073
1074
543292
            ff_hevc_set_qPy(s, cb_xBase, cb_yBase, log2_cb_size);
1075
        }
1076
1077

10623603
        if (s->sh.cu_chroma_qp_offset_enabled_flag && cbf_chroma &&
1078

841055
            !lc->cu.cu_transquant_bypass_flag  &&  !lc->tu.is_cu_chroma_qp_offset_coded) {
1079
266275
            int cu_chroma_qp_offset_flag = ff_hevc_cu_chroma_qp_offset_flag(s);
1080
266275
            if (cu_chroma_qp_offset_flag) {
1081
48966
                int cu_chroma_qp_offset_idx  = 0;
1082
48966
                if (s->ps.pps->chroma_qp_offset_list_len_minus1 > 0) {
1083
                    cu_chroma_qp_offset_idx = ff_hevc_cu_chroma_qp_offset_idx(s);
1084
                    av_log(s->avctx, AV_LOG_ERROR,
1085
                        "cu_chroma_qp_offset_idx not yet tested.\n");
1086
                }
1087
48966
                lc->tu.cu_qp_offset_cb = s->ps.pps->cb_qp_offset_list[cu_chroma_qp_offset_idx];
1088
48966
                lc->tu.cu_qp_offset_cr = s->ps.pps->cr_qp_offset_list[cu_chroma_qp_offset_idx];
1089
            } else {
1090
217309
                lc->tu.cu_qp_offset_cb = 0;
1091
217309
                lc->tu.cu_qp_offset_cr = 0;
1092
            }
1093
266275
            lc->tu.is_cu_chroma_qp_offset_coded = 1;
1094
        }
1095
1096

10623603
        if (lc->cu.pred_mode == MODE_INTRA && log2_trafo_size < 4) {
1097
6121546
            if (lc->tu.intra_pred_mode >= 6 &&
1098
4116596
                lc->tu.intra_pred_mode <= 14) {
1099
1491262
                scan_idx = SCAN_VERT;
1100
4630284
            } else if (lc->tu.intra_pred_mode >= 22 &&
1101
2024225
                       lc->tu.intra_pred_mode <= 30) {
1102
1699652
                scan_idx = SCAN_HORIZ;
1103
            }
1104
1105
6121546
            if (lc->tu.intra_pred_mode_c >=  6 &&
1106
3689970
                lc->tu.intra_pred_mode_c <= 14) {
1107
1402714
                scan_idx_c = SCAN_VERT;
1108
4718832
            } else if (lc->tu.intra_pred_mode_c >= 22 &&
1109
1922258
                       lc->tu.intra_pred_mode_c <= 30) {
1110
1607627
                scan_idx_c = SCAN_HORIZ;
1111
            }
1112
        }
1113
1114
10623603
        lc->tu.cross_pf = 0;
1115
1116
10623603
        if (cbf_luma)
1117
9522273
            ff_hevc_hls_residual_coding(s, x0, y0, log2_trafo_size, scan_idx, 0);
1118

15424750
        if (s->ps.sps->chroma_format_idc && (log2_trafo_size > 2 || s->ps.sps->chroma_format_idc == 3)) {
1119
4801147
            int trafo_size_h = 1 << (log2_trafo_size_c + s->ps.sps->hshift[1]);
1120
4801147
            int trafo_size_v = 1 << (log2_trafo_size_c + s->ps.sps->vshift[1]);
1121

5018961
            lc->tu.cross_pf  = (s->ps.pps->cross_component_prediction_enabled_flag && cbf_luma &&
1122
217814
                                (lc->cu.pred_mode == MODE_INTER ||
1123
168871
                                 (lc->tu.chroma_mode_c ==  4)));
1124
1125
4801147
            if (lc->tu.cross_pf) {
1126
192423
                hls_cross_component_pred(s, 0);
1127
            }
1128

9980552
            for (i = 0; i < (s->ps.sps->chroma_format_idc == 2 ? 2 : 1); i++) {
1129
5179405
                if (lc->cu.pred_mode == MODE_INTRA) {
1130
2802165
                    ff_hevc_set_neighbour_available(s, x0, y0 + (i << log2_trafo_size_c), trafo_size_h, trafo_size_v);
1131
2802165
                    s->hpc.intra_pred[log2_trafo_size_c - 2](s, x0, y0 + (i << log2_trafo_size_c), 1);
1132
                }
1133
5179405
                if (cbf_cb[i])
1134
1356649
                    ff_hevc_hls_residual_coding(s, x0, y0 + (i << log2_trafo_size_c),
1135
                                                log2_trafo_size_c, scan_idx_c, 1);
1136
                else
1137
3822756
                    if (lc->tu.cross_pf) {
1138
39658
                        ptrdiff_t stride = s->frame->linesize[1];
1139
39658
                        int hshift = s->ps.sps->hshift[1];
1140
39658
                        int vshift = s->ps.sps->vshift[1];
1141
39658
                        int16_t *coeffs_y = (int16_t*)lc->edge_emu_buffer;
1142
39658
                        int16_t *coeffs   = (int16_t*)lc->edge_emu_buffer2;
1143
39658
                        int size = 1 << log2_trafo_size_c;
1144
1145
39658
                        uint8_t *dst = &s->frame->data[1][(y0 >> vshift) * stride +
1146
39658
                                                              ((x0 >> hshift) << s->ps.sps->pixel_shift)];
1147
1871162
                        for (i = 0; i < (size * size); i++) {
1148
1831504
                            coeffs[i] = ((lc->tu.res_scale_val * coeffs_y[i]) >> 3);
1149
                        }
1150
39658
                        s->hevcdsp.add_residual[log2_trafo_size_c-2](dst, coeffs, stride);
1151
                    }
1152
            }
1153
1154
4801147
            if (lc->tu.cross_pf) {
1155
192423
                hls_cross_component_pred(s, 1);
1156
            }
1157

9980552
            for (i = 0; i < (s->ps.sps->chroma_format_idc == 2 ? 2 : 1); i++) {
1158
5179405
                if (lc->cu.pred_mode == MODE_INTRA) {
1159
2802165
                    ff_hevc_set_neighbour_available(s, x0, y0 + (i << log2_trafo_size_c), trafo_size_h, trafo_size_v);
1160
2802165
                    s->hpc.intra_pred[log2_trafo_size_c - 2](s, x0, y0 + (i << log2_trafo_size_c), 2);
1161
                }
1162
5179405
                if (cbf_cr[i])
1163
1483795
                    ff_hevc_hls_residual_coding(s, x0, y0 + (i << log2_trafo_size_c),
1164
                                                log2_trafo_size_c, scan_idx_c, 2);
1165
                else
1166
3695610
                    if (lc->tu.cross_pf) {
1167
75924
                        ptrdiff_t stride = s->frame->linesize[2];
1168
75924
                        int hshift = s->ps.sps->hshift[2];
1169
75924
                        int vshift = s->ps.sps->vshift[2];
1170
75924
                        int16_t *coeffs_y = (int16_t*)lc->edge_emu_buffer;
1171
75924
                        int16_t *coeffs   = (int16_t*)lc->edge_emu_buffer2;
1172
75924
                        int size = 1 << log2_trafo_size_c;
1173
1174
75924
                        uint8_t *dst = &s->frame->data[2][(y0 >> vshift) * stride +
1175
75924
                                                          ((x0 >> hshift) << s->ps.sps->pixel_shift)];
1176
3691476
                        for (i = 0; i < (size * size); i++) {
1177
3615552
                            coeffs[i] = ((lc->tu.res_scale_val * coeffs_y[i]) >> 3);
1178
                        }
1179
75924
                        s->hevcdsp.add_residual[log2_trafo_size_c-2](dst, coeffs, stride);
1180
                    }
1181
            }
1182

5822456
        } else if (s->ps.sps->chroma_format_idc && blk_idx == 3) {
1183
1495788
            int trafo_size_h = 1 << (log2_trafo_size + 1);
1184
1495788
            int trafo_size_v = 1 << (log2_trafo_size + s->ps.sps->vshift[1]);
1185

3158445
            for (i = 0; i < (s->ps.sps->chroma_format_idc == 2 ? 2 : 1); i++) {
1186
1662657
                if (lc->cu.pred_mode == MODE_INTRA) {
1187
1182570
                    ff_hevc_set_neighbour_available(s, xBase, yBase + (i << log2_trafo_size),
1188
                                                    trafo_size_h, trafo_size_v);
1189
1182570
                    s->hpc.intra_pred[log2_trafo_size - 2](s, xBase, yBase + (i << log2_trafo_size), 1);
1190
                }
1191
1662657
                if (cbf_cb[i])
1192
632269
                    ff_hevc_hls_residual_coding(s, xBase, yBase + (i << log2_trafo_size),
1193
                                                log2_trafo_size, scan_idx_c, 1);
1194
            }
1195

3158445
            for (i = 0; i < (s->ps.sps->chroma_format_idc == 2 ? 2 : 1); i++) {
1196
1662657
                if (lc->cu.pred_mode == MODE_INTRA) {
1197
1182570
                    ff_hevc_set_neighbour_available(s, xBase, yBase + (i << log2_trafo_size),
1198
                                                trafo_size_h, trafo_size_v);
1199
1182570
                    s->hpc.intra_pred[log2_trafo_size - 2](s, xBase, yBase + (i << log2_trafo_size), 2);
1200
                }
1201
1662657
                if (cbf_cr[i])
1202
713145
                    ff_hevc_hls_residual_coding(s, xBase, yBase + (i << log2_trafo_size),
1203
                                                log2_trafo_size, scan_idx_c, 2);
1204
            }
1205
        }
1206

4430230
    } else if (s->ps.sps->chroma_format_idc && lc->cu.pred_mode == MODE_INTRA) {
1207

3500199
        if (log2_trafo_size > 2 || s->ps.sps->chroma_format_idc == 3) {
1208
995977
            int trafo_size_h = 1 << (log2_trafo_size_c + s->ps.sps->hshift[1]);
1209
995977
            int trafo_size_v = 1 << (log2_trafo_size_c + s->ps.sps->vshift[1]);
1210
995977
            ff_hevc_set_neighbour_available(s, x0, y0, trafo_size_h, trafo_size_v);
1211
995977
            s->hpc.intra_pred[log2_trafo_size_c - 2](s, x0, y0, 1);
1212
995977
            s->hpc.intra_pred[log2_trafo_size_c - 2](s, x0, y0, 2);
1213
995977
            if (s->ps.sps->chroma_format_idc == 2) {
1214
12423
                ff_hevc_set_neighbour_available(s, x0, y0 + (1 << log2_trafo_size_c),
1215
                                                trafo_size_h, trafo_size_v);
1216
12423
                s->hpc.intra_pred[log2_trafo_size_c - 2](s, x0, y0 + (1 << log2_trafo_size_c), 1);
1217
12423
                s->hpc.intra_pred[log2_trafo_size_c - 2](s, x0, y0 + (1 << log2_trafo_size_c), 2);
1218
            }
1219
1508245
        } else if (blk_idx == 3) {
1220
342183
            int trafo_size_h = 1 << (log2_trafo_size + 1);
1221
342183
            int trafo_size_v = 1 << (log2_trafo_size + s->ps.sps->vshift[1]);
1222
342183
            ff_hevc_set_neighbour_available(s, xBase, yBase,
1223
                                            trafo_size_h, trafo_size_v);
1224
342183
            s->hpc.intra_pred[log2_trafo_size - 2](s, xBase, yBase, 1);
1225
342183
            s->hpc.intra_pred[log2_trafo_size - 2](s, xBase, yBase, 2);
1226
342183
            if (s->ps.sps->chroma_format_idc == 2) {
1227
4196
                ff_hevc_set_neighbour_available(s, xBase, yBase + (1 << (log2_trafo_size)),
1228
                                                trafo_size_h, trafo_size_v);
1229
4196
                s->hpc.intra_pred[log2_trafo_size - 2](s, xBase, yBase + (1 << (log2_trafo_size)), 1);
1230
4196
                s->hpc.intra_pred[log2_trafo_size - 2](s, xBase, yBase + (1 << (log2_trafo_size)), 2);
1231
            }
1232
        }
1233
    }
1234
1235
15053833
    return 0;
1236
}
1237
1238
356478
static void set_deblocking_bypass(HEVCContext *s, int x0, int y0, int log2_cb_size)
1239
{
1240
356478
    int cb_size          = 1 << log2_cb_size;
1241
356478
    int log2_min_pu_size = s->ps.sps->log2_min_pu_size;
1242
1243
356478
    int min_pu_width     = s->ps.sps->min_pu_width;
1244
356478
    int x_end = FFMIN(x0 + cb_size, s->ps.sps->width);
1245
356478
    int y_end = FFMIN(y0 + cb_size, s->ps.sps->height);
1246
    int i, j;
1247
1248
851386
    for (j = (y0 >> log2_min_pu_size); j < (y_end >> log2_min_pu_size); j++)
1249
1443964
        for (i = (x0 >> log2_min_pu_size); i < (x_end >> log2_min_pu_size); i++)
1250
949056
            s->is_pcm[i + j * min_pu_width] = 2;
1251
356478
}
1252
1253
18188413
static int hls_transform_tree(HEVCContext *s, int x0, int y0,
1254
                              int xBase, int yBase, int cb_xBase, int cb_yBase,
1255
                              int log2_cb_size, int log2_trafo_size,
1256
                              int trafo_depth, int blk_idx,
1257
                              const int *base_cbf_cb, const int *base_cbf_cr)
1258
{
1259
18188413
    HEVCLocalContext *lc = s->HEVClc;
1260
    uint8_t split_transform_flag;
1261
    int cbf_cb[2];
1262
    int cbf_cr[2];
1263
    int ret;
1264
1265
18188413
    cbf_cb[0] = base_cbf_cb[0];
1266
18188413
    cbf_cb[1] = base_cbf_cb[1];
1267
18188413
    cbf_cr[0] = base_cbf_cr[0];
1268
18188413
    cbf_cr[1] = base_cbf_cr[1];
1269
1270
18188413
    if (lc->cu.intra_split_flag) {
1271
6242541
        if (trafo_depth == 1) {
1272
4691972
            lc->tu.intra_pred_mode   = lc->pu.intra_pred_mode[blk_idx];
1273
4691972
            if (s->ps.sps->chroma_format_idc == 3) {
1274
135404
                lc->tu.intra_pred_mode_c = lc->pu.intra_pred_mode_c[blk_idx];
1275
135404
                lc->tu.chroma_mode_c     = lc->pu.chroma_mode_c[blk_idx];
1276
            } else {
1277
4556568
                lc->tu.intra_pred_mode_c = lc->pu.intra_pred_mode_c[0];
1278
4556568
                lc->tu.chroma_mode_c     = lc->pu.chroma_mode_c[0];
1279
            }
1280
        }
1281
    } else {
1282
11945872
        lc->tu.intra_pred_mode   = lc->pu.intra_pred_mode[0];
1283
11945872
        lc->tu.intra_pred_mode_c = lc->pu.intra_pred_mode_c[0];
1284
11945872
        lc->tu.chroma_mode_c     = lc->pu.chroma_mode_c[0];
1285
    }
1286
1287
18188413
    if (log2_trafo_size <= s->ps.sps->log2_max_trafo_size &&
1288
18080705
        log2_trafo_size >  s->ps.sps->log2_min_tb_size    &&
1289
9478025
        trafo_depth     < lc->cu.max_trafo_depth       &&
1290

7950273
        !(lc->cu.intra_split_flag && trafo_depth == 0)) {
1291
6784667
        split_transform_flag = ff_hevc_split_transform_flag_decode(s, log2_trafo_size);
1292
    } else {
1293
24340458
        int inter_split = s->ps.sps->max_transform_hierarchy_depth_inter == 0 &&
1294
1532966
                          lc->cu.pred_mode == MODE_INTER &&
1295

12936712
                          lc->cu.part_mode != PART_2Nx2N &&
1296
                          trafo_depth == 0;
1297
1298
11403746
        split_transform_flag = log2_trafo_size > s->ps.sps->log2_max_trafo_size ||
1299


11403746
                               (lc->cu.intra_split_flag && trafo_depth == 0) ||
1300
                               inter_split;
1301
    }
1302
1303

18188413
    if (s->ps.sps->chroma_format_idc && (log2_trafo_size > 2 || s->ps.sps->chroma_format_idc == 3)) {
1304

9973301
        if (trafo_depth == 0 || cbf_cb[0]) {
1305
6815497
            cbf_cb[0] = ff_hevc_cbf_cb_cr_decode(s, trafo_depth);
1306

6815497
            if (s->ps.sps->chroma_format_idc == 2 && (!split_transform_flag || log2_trafo_size == 3)) {
1307
452048
                cbf_cb[1] = ff_hevc_cbf_cb_cr_decode(s, trafo_depth);
1308
            }
1309
        }
1310
1311

9973301
        if (trafo_depth == 0 || cbf_cr[0]) {
1312
6762369
            cbf_cr[0] = ff_hevc_cbf_cb_cr_decode(s, trafo_depth);
1313

6762369
            if (s->ps.sps->chroma_format_idc == 2 && (!split_transform_flag || log2_trafo_size == 3)) {
1314
587469
                cbf_cr[1] = ff_hevc_cbf_cb_cr_decode(s, trafo_depth);
1315
            }
1316
        }
1317
    }
1318
1319
18188413
    if (split_transform_flag) {
1320
3134579
        const int trafo_size_split = 1 << (log2_trafo_size - 1);
1321
3134579
        const int x1 = x0 + trafo_size_split;
1322
3134579
        const int y1 = y0 + trafo_size_split;
1323
1324
#define SUBDIVIDE(x, y, idx)                                                    \
1325
do {                                                                            \
1326
    ret = hls_transform_tree(s, x, y, x0, y0, cb_xBase, cb_yBase, log2_cb_size, \
1327
                             log2_trafo_size - 1, trafo_depth + 1, idx,         \
1328
                             cbf_cb, cbf_cr);                                   \
1329
    if (ret < 0)                                                                \
1330
        return ret;                                                             \
1331
} while (0)
1332
1333
3134579
        SUBDIVIDE(x0, y0, 0);
1334
3134579
        SUBDIVIDE(x1, y0, 1);
1335
3134579
        SUBDIVIDE(x0, y1, 2);
1336
3134579
        SUBDIVIDE(x1, y1, 3);
1337
1338
#undef SUBDIVIDE
1339
    } else {
1340
15053834
        int min_tu_size      = 1 << s->ps.sps->log2_min_tb_size;
1341
15053834
        int log2_min_tu_size = s->ps.sps->log2_min_tb_size;
1342
15053834
        int min_tu_width     = s->ps.sps->min_tb_width;
1343
15053834
        int cbf_luma         = 1;
1344
1345

15053834
        if (lc->cu.pred_mode == MODE_INTRA || trafo_depth != 0 ||
1346

976133
            cbf_cb[0] || cbf_cr[0] ||
1347

684251
            (s->ps.sps->chroma_format_idc == 2 && (cbf_cb[1] || cbf_cr[1]))) {
1348
14382229
            cbf_luma = ff_hevc_cbf_luma_decode(s, trafo_depth);
1349
        }
1350
1351
15053834
        ret = hls_transform_unit(s, x0, y0, xBase, yBase, cb_xBase, cb_yBase,
1352
                                 log2_cb_size, log2_trafo_size,
1353
                                 blk_idx, cbf_luma, cbf_cb, cbf_cr);
1354
15053834
        if (ret < 0)
1355
1
            return ret;
1356
        // TODO: store cbf_luma somewhere else
1357
15053833
        if (cbf_luma) {
1358
            int i, j;
1359
28919808
            for (i = 0; i < (1 << log2_trafo_size); i += min_tu_size)
1360
88500438
                for (j = 0; j < (1 << log2_trafo_size); j += min_tu_size) {
1361
69102903
                    int x_tu = (x0 + j) >> log2_min_tu_size;
1362
69102903
                    int y_tu = (y0 + i) >> log2_min_tu_size;
1363
69102903
                    s->cbf_luma[y_tu * min_tu_width + x_tu] = 1;
1364
                }
1365
        }
1366
15053833
        if (!s->sh.disable_deblocking_filter_flag) {
1367
14521013
            ff_hevc_deblocking_boundary_strengths(s, x0, y0, log2_trafo_size);
1368
14521013
            if (s->ps.pps->transquant_bypass_enable_flag &&
1369
373941
                lc->cu.cu_transquant_bypass_flag)
1370
275932
                set_deblocking_bypass(s, x0, y0, log2_trafo_size);
1371
        }
1372
    }
1373
18188412
    return 0;
1374
}
1375
1376
12433
static int hls_pcm_sample(HEVCContext *s, int x0, int y0, int log2_cb_size)
1377
{
1378
12433
    HEVCLocalContext *lc = s->HEVClc;
1379
    GetBitContext gb;
1380
12433
    int cb_size   = 1 << log2_cb_size;
1381
12433
    ptrdiff_t stride0 = s->frame->linesize[0];
1382
12433
    ptrdiff_t stride1 = s->frame->linesize[1];
1383
12433
    ptrdiff_t stride2 = s->frame->linesize[2];
1384
12433
    uint8_t *dst0 = &s->frame->data[0][y0 * stride0 + (x0 << s->ps.sps->pixel_shift)];
1385
12433
    uint8_t *dst1 = &s->frame->data[1][(y0 >> s->ps.sps->vshift[1]) * stride1 + ((x0 >> s->ps.sps->hshift[1]) << s->ps.sps->pixel_shift)];
1386
12433
    uint8_t *dst2 = &s->frame->data[2][(y0 >> s->ps.sps->vshift[2]) * stride2 + ((x0 >> s->ps.sps->hshift[2]) << s->ps.sps->pixel_shift)];
1387
1388
12433
    int length         = cb_size * cb_size * s->ps.sps->pcm.bit_depth +
1389
12433
                         (((cb_size >> s->ps.sps->hshift[1]) * (cb_size >> s->ps.sps->vshift[1])) +
1390
12433
                          ((cb_size >> s->ps.sps->hshift[2]) * (cb_size >> s->ps.sps->vshift[2]))) *
1391
12433
                          s->ps.sps->pcm.bit_depth_chroma;
1392
12433
    const uint8_t *pcm = skip_bytes(&lc->cc, (length + 7) >> 3);
1393
    int ret;
1394
1395
12433
    if (!s->sh.disable_deblocking_filter_flag)
1396
6937
        ff_hevc_deblocking_boundary_strengths(s, x0, y0, log2_cb_size);
1397
1398
12433
    ret = init_get_bits(&gb, pcm, length);
1399
12433
    if (ret < 0)
1400
        return ret;
1401
1402
12433
    s->hevcdsp.put_pcm(dst0, stride0, cb_size, cb_size,     &gb, s->ps.sps->pcm.bit_depth);
1403
12433
    if (s->ps.sps->chroma_format_idc) {
1404
12433
        s->hevcdsp.put_pcm(dst1, stride1,
1405
12433
                           cb_size >> s->ps.sps->hshift[1],
1406
12433
                           cb_size >> s->ps.sps->vshift[1],
1407
12433
                           &gb, s->ps.sps->pcm.bit_depth_chroma);
1408
12433
        s->hevcdsp.put_pcm(dst2, stride2,
1409
12433
                           cb_size >> s->ps.sps->hshift[2],
1410
12433
                           cb_size >> s->ps.sps->vshift[2],
1411
12433
                           &gb, s->ps.sps->pcm.bit_depth_chroma);
1412
    }
1413
1414
12433
    return 0;
1415
}
1416
1417
/**
1418
 * 8.5.3.2.2.1 Luma sample unidirectional interpolation process
1419
 *
1420
 * @param s HEVC decoding context
1421
 * @param dst target buffer for block data at block position
1422
 * @param dststride stride of the dst buffer
1423
 * @param ref reference picture buffer at origin (0, 0)
1424
 * @param mv motion vector (relative to block position) to get pixel data from
1425
 * @param x_off horizontal position of block from origin (0, 0)
1426
 * @param y_off vertical position of block from origin (0, 0)
1427
 * @param block_w width of block
1428
 * @param block_h height of block
1429
 * @param luma_weight weighting factor applied to the luma prediction
1430
 * @param luma_offset additive offset applied to the luma prediction value
1431
 */
1432
1433
4797591
static void luma_mc_uni(HEVCContext *s, uint8_t *dst, ptrdiff_t dststride,
1434
                        AVFrame *ref, const Mv *mv, int x_off, int y_off,
1435
                        int block_w, int block_h, int luma_weight, int luma_offset)
1436
{
1437
4797591
    HEVCLocalContext *lc = s->HEVClc;
1438
4797591
    uint8_t *src         = ref->data[0];
1439
4797591
    ptrdiff_t srcstride  = ref->linesize[0];
1440
4797591
    int pic_width        = s->ps.sps->width;
1441
4797591
    int pic_height       = s->ps.sps->height;
1442
4797591
    int mx               = mv->x & 3;
1443
4797591
    int my               = mv->y & 3;
1444

9549525
    int weight_flag      = (s->sh.slice_type == HEVC_SLICE_P && s->ps.pps->weighted_pred_flag) ||
1445

4751934
                           (s->sh.slice_type == HEVC_SLICE_B && s->ps.pps->weighted_bipred_flag);
1446
4797591
    int idx              = ff_hevc_pel_weight[block_w];
1447
1448
4797591
    x_off += mv->x >> 2;
1449
4797591
    y_off += mv->y >> 2;
1450
4797591
    src   += y_off * srcstride + (x_off * (1 << s->ps.sps->pixel_shift));
1451
1452

4797591
    if (x_off < QPEL_EXTRA_BEFORE || y_off < QPEL_EXTRA_AFTER ||
1453
4638547
        x_off >= pic_width - block_w - QPEL_EXTRA_AFTER ||
1454
4575674
        y_off >= pic_height - block_h - QPEL_EXTRA_AFTER) {
1455
414120
        const ptrdiff_t edge_emu_stride = EDGE_EMU_BUFFER_STRIDE << s->ps.sps->pixel_shift;
1456
414120
        int offset     = QPEL_EXTRA_BEFORE * srcstride       + (QPEL_EXTRA_BEFORE << s->ps.sps->pixel_shift);
1457
414120
        int buf_offset = QPEL_EXTRA_BEFORE * edge_emu_stride + (QPEL_EXTRA_BEFORE << s->ps.sps->pixel_shift);
1458
1459
414120
        s->vdsp.emulated_edge_mc(lc->edge_emu_buffer, src - offset,
1460
                                 edge_emu_stride, srcstride,
1461
                                 block_w + QPEL_EXTRA,
1462
                                 block_h + QPEL_EXTRA,
1463
                                 x_off - QPEL_EXTRA_BEFORE, y_off - QPEL_EXTRA_BEFORE,
1464
                                 pic_width, pic_height);
1465
414120
        src = lc->edge_emu_buffer + buf_offset;
1466
414120
        srcstride = edge_emu_stride;
1467
    }
1468
1469
4797591
    if (!weight_flag)
1470
4691653
        s->hevcdsp.put_hevc_qpel_uni[idx][!!my][!!mx](dst, dststride, src, srcstride,
1471
                                                      block_h, mx, my, block_w);
1472
    else
1473
105938
        s->hevcdsp.put_hevc_qpel_uni_w[idx][!!my][!!mx](dst, dststride, src, srcstride,
1474
105938
                                                        block_h, s->sh.luma_log2_weight_denom,
1475
                                                        luma_weight, luma_offset, mx, my, block_w);
1476
4797591
}
1477
1478
/**
1479
 * 8.5.3.2.2.1 Luma sample bidirectional interpolation process
1480
 *
1481
 * @param s HEVC decoding context
1482
 * @param dst target buffer for block data at block position
1483
 * @param dststride stride of the dst buffer
1484
 * @param ref0 reference picture0 buffer at origin (0, 0)
1485
 * @param mv0 motion vector0 (relative to block position) to get pixel data from
1486
 * @param x_off horizontal position of block from origin (0, 0)
1487
 * @param y_off vertical position of block from origin (0, 0)
1488
 * @param block_w width of block
1489
 * @param block_h height of block
1490
 * @param ref1 reference picture1 buffer at origin (0, 0)
1491
 * @param mv1 motion vector1 (relative to block position) to get pixel data from
1492
 * @param current_mv current motion vector structure
1493
 */
1494
3781449
 static void luma_mc_bi(HEVCContext *s, uint8_t *dst, ptrdiff_t dststride,
1495
                       AVFrame *ref0, const Mv *mv0, int x_off, int y_off,
1496
                       int block_w, int block_h, AVFrame *ref1, const Mv *mv1, struct MvField *current_mv)
1497
{
1498
3781449
    HEVCLocalContext *lc = s->HEVClc;
1499
3781449
    ptrdiff_t src0stride  = ref0->linesize[0];
1500
3781449
    ptrdiff_t src1stride  = ref1->linesize[0];
1501
3781449
    int pic_width        = s->ps.sps->width;
1502
3781449
    int pic_height       = s->ps.sps->height;
1503
3781449
    int mx0              = mv0->x & 3;
1504
3781449
    int my0              = mv0->y & 3;
1505
3781449
    int mx1              = mv1->x & 3;
1506
3781449
    int my1              = mv1->y & 3;
1507

7562898
    int weight_flag      = (s->sh.slice_type == HEVC_SLICE_P && s->ps.pps->weighted_pred_flag) ||
1508

3781449
                           (s->sh.slice_type == HEVC_SLICE_B && s->ps.pps->weighted_bipred_flag);
1509
3781449
    int x_off0           = x_off + (mv0->x >> 2);
1510
3781449
    int y_off0           = y_off + (mv0->y >> 2);
1511
3781449
    int x_off1           = x_off + (mv1->x >> 2);
1512
3781449
    int y_off1           = y_off + (mv1->y >> 2);
1513
3781449
    int idx              = ff_hevc_pel_weight[block_w];
1514
1515
3781449
    uint8_t *src0  = ref0->data[0] + y_off0 * src0stride + (int)((unsigned)x_off0 << s->ps.sps->pixel_shift);
1516
3781449
    uint8_t *src1  = ref1->data[0] + y_off1 * src1stride + (int)((unsigned)x_off1 << s->ps.sps->pixel_shift);
1517
1518

3781449
    if (x_off0 < QPEL_EXTRA_BEFORE || y_off0 < QPEL_EXTRA_AFTER ||
1519
3629676
        x_off0 >= pic_width - block_w - QPEL_EXTRA_AFTER ||
1520
3566623
        y_off0 >= pic_height - block_h - QPEL_EXTRA_AFTER) {
1521
415542
        const ptrdiff_t edge_emu_stride = EDGE_EMU_BUFFER_STRIDE << s->ps.sps->pixel_shift;
1522
415542
        int offset     = QPEL_EXTRA_BEFORE * src0stride       + (QPEL_EXTRA_BEFORE << s->ps.sps->pixel_shift);
1523
415542
        int buf_offset = QPEL_EXTRA_BEFORE * edge_emu_stride + (QPEL_EXTRA_BEFORE << s->ps.sps->pixel_shift);
1524
1525
415542
        s->vdsp.emulated_edge_mc(lc->edge_emu_buffer, src0 - offset,
1526
                                 edge_emu_stride, src0stride,
1527
                                 block_w + QPEL_EXTRA,
1528
                                 block_h + QPEL_EXTRA,
1529
                                 x_off0 - QPEL_EXTRA_BEFORE, y_off0 - QPEL_EXTRA_BEFORE,
1530
                                 pic_width, pic_height);
1531
415542
        src0 = lc->edge_emu_buffer + buf_offset;
1532
415542
        src0stride = edge_emu_stride;
1533
    }
1534
1535

3781449
    if (x_off1 < QPEL_EXTRA_BEFORE || y_off1 < QPEL_EXTRA_AFTER ||
1536
3624885
        x_off1 >= pic_width - block_w - QPEL_EXTRA_AFTER ||
1537
3559536
        y_off1 >= pic_height - block_h - QPEL_EXTRA_AFTER) {
1538
426476
        const ptrdiff_t edge_emu_stride = EDGE_EMU_BUFFER_STRIDE << s->ps.sps->pixel_shift;
1539
426476
        int offset     = QPEL_EXTRA_BEFORE * src1stride       + (QPEL_EXTRA_BEFORE << s->ps.sps->pixel_shift);
1540
426476
        int buf_offset = QPEL_EXTRA_BEFORE * edge_emu_stride + (QPEL_EXTRA_BEFORE << s->ps.sps->pixel_shift);
1541
1542
426476
        s->vdsp.emulated_edge_mc(lc->edge_emu_buffer2, src1 - offset,
1543
                                 edge_emu_stride, src1stride,
1544
                                 block_w + QPEL_EXTRA,
1545
                                 block_h + QPEL_EXTRA,
1546
                                 x_off1 - QPEL_EXTRA_BEFORE, y_off1 - QPEL_EXTRA_BEFORE,
1547
                                 pic_width, pic_height);
1548
426476
        src1 = lc->edge_emu_buffer2 + buf_offset;
1549
426476
        src1stride = edge_emu_stride;
1550
    }
1551
1552
3781449
    s->hevcdsp.put_hevc_qpel[idx][!!my0][!!mx0](lc->tmp, src0, src0stride,
1553
                                                block_h, mx0, my0, block_w);
1554
3781449
    if (!weight_flag)
1555
3719142
        s->hevcdsp.put_hevc_qpel_bi[idx][!!my1][!!mx1](dst, dststride, src1, src1stride, lc->tmp,
1556
                                                       block_h, mx1, my1, block_w);
1557
    else
1558
62307
        s->hevcdsp.put_hevc_qpel_bi_w[idx][!!my1][!!mx1](dst, dststride, src1, src1stride, lc->tmp,
1559
62307
                                                         block_h, s->sh.luma_log2_weight_denom,
1560
62307
                                                         s->sh.luma_weight_l0[current_mv->ref_idx[0]],
1561
62307
                                                         s->sh.luma_weight_l1[current_mv->ref_idx[1]],
1562
62307
                                                         s->sh.luma_offset_l0[current_mv->ref_idx[0]],
1563
62307
                                                         s->sh.luma_offset_l1[current_mv->ref_idx[1]],
1564
                                                         mx1, my1, block_w);
1565
1566
3781449
}
1567
1568
/**
1569
 * 8.5.3.2.2.2 Chroma sample uniprediction interpolation process
1570
 *
1571
 * @param s HEVC decoding context
1572
 * @param dst1 target buffer for block data at block position (U plane)
1573
 * @param dst2 target buffer for block data at block position (V plane)
1574
 * @param dststride stride of the dst1 and dst2 buffers
1575
 * @param ref reference picture buffer at origin (0, 0)
1576
 * @param mv motion vector (relative to block position) to get pixel data from
1577
 * @param x_off horizontal position of block from origin (0, 0)
1578
 * @param y_off vertical position of block from origin (0, 0)
1579
 * @param block_w width of block
1580
 * @param block_h height of block
1581
 * @param chroma_weight weighting factor applied to the chroma prediction
1582
 * @param chroma_offset additive offset applied to the chroma prediction value
1583
 */
1584
1585
9595182
static void chroma_mc_uni(HEVCContext *s, uint8_t *dst0,
1586
                          ptrdiff_t dststride, uint8_t *src0, ptrdiff_t srcstride, int reflist,
1587
                          int x_off, int y_off, int block_w, int block_h, struct MvField *current_mv, int chroma_weight, int chroma_offset)
1588
{
1589
9595182
    HEVCLocalContext *lc = s->HEVClc;
1590
9595182
    int pic_width        = s->ps.sps->width >> s->ps.sps->hshift[1];
1591
9595182
    int pic_height       = s->ps.sps->height >> s->ps.sps->vshift[1];
1592
9595182
    const Mv *mv         = &current_mv->mv[reflist];
1593

19099050
    int weight_flag      = (s->sh.slice_type == HEVC_SLICE_P && s->ps.pps->weighted_pred_flag) ||
1594

9503868
                           (s->sh.slice_type == HEVC_SLICE_B && s->ps.pps->weighted_bipred_flag);
1595
9595182
    int idx              = ff_hevc_pel_weight[block_w];
1596
9595182
    int hshift           = s->ps.sps->hshift[1];
1597
9595182
    int vshift           = s->ps.sps->vshift[1];
1598
9595182
    intptr_t mx          = av_mod_uintp2(mv->x, 2 + hshift);
1599
9595182
    intptr_t my          = av_mod_uintp2(mv->y, 2 + vshift);
1600
9595182
    intptr_t _mx         = mx << (1 - hshift);
1601
9595182
    intptr_t _my         = my << (1 - vshift);
1602
1603
9595182
    x_off += mv->x >> (2 + hshift);
1604
9595182
    y_off += mv->y >> (2 + vshift);
1605
9595182
    src0  += y_off * srcstride + (x_off * (1 << s->ps.sps->pixel_shift));
1606
1607

9595182
    if (x_off < EPEL_EXTRA_BEFORE || y_off < EPEL_EXTRA_AFTER ||
1608
9286952
        x_off >= pic_width - block_w - EPEL_EXTRA_AFTER ||
1609
9161214
        y_off >= pic_height - block_h - EPEL_EXTRA_AFTER) {
1610
818430
        const int edge_emu_stride = EDGE_EMU_BUFFER_STRIDE << s->ps.sps->pixel_shift;
1611
818430
        int offset0 = EPEL_EXTRA_BEFORE * (srcstride + (1 << s->ps.sps->pixel_shift));
1612
818430
        int buf_offset0 = EPEL_EXTRA_BEFORE *
1613
818430
                          (edge_emu_stride + (1 << s->ps.sps->pixel_shift));
1614
818430
        s->vdsp.emulated_edge_mc(lc->edge_emu_buffer, src0 - offset0,
1615
                                 edge_emu_stride, srcstride,
1616
                                 block_w + EPEL_EXTRA, block_h + EPEL_EXTRA,
1617
                                 x_off - EPEL_EXTRA_BEFORE,
1618
                                 y_off - EPEL_EXTRA_BEFORE,
1619
                                 pic_width, pic_height);
1620
1621
818430
        src0 = lc->edge_emu_buffer + buf_offset0;
1622
818430
        srcstride = edge_emu_stride;
1623
    }
1624
9595182
    if (!weight_flag)
1625
9383306
        s->hevcdsp.put_hevc_epel_uni[idx][!!my][!!mx](dst0, dststride, src0, srcstride,
1626
                                                  block_h, _mx, _my, block_w);
1627
    else
1628
211876
        s->hevcdsp.put_hevc_epel_uni_w[idx][!!my][!!mx](dst0, dststride, src0, srcstride,
1629
211876
                                                        block_h, s->sh.chroma_log2_weight_denom,
1630
                                                        chroma_weight, chroma_offset, _mx, _my, block_w);
1631
9595182
}
1632
1633
/**
1634
 * 8.5.3.2.2.2 Chroma sample bidirectional interpolation process
1635
 *
1636
 * @param s HEVC decoding context
1637
 * @param dst target buffer for block data at block position
1638
 * @param dststride stride of the dst buffer
1639
 * @param ref0 reference picture0 buffer at origin (0, 0)
1640
 * @param mv0 motion vector0 (relative to block position) to get pixel data from
1641
 * @param x_off horizontal position of block from origin (0, 0)
1642
 * @param y_off vertical position of block from origin (0, 0)
1643
 * @param block_w width of block
1644
 * @param block_h height of block
1645
 * @param ref1 reference picture1 buffer at origin (0, 0)
1646
 * @param mv1 motion vector1 (relative to block position) to get pixel data from
1647
 * @param current_mv current motion vector structure
1648
 * @param cidx chroma component(cb, cr)
1649
 */
1650
7562898
static void chroma_mc_bi(HEVCContext *s, uint8_t *dst0, ptrdiff_t dststride, AVFrame *ref0, AVFrame *ref1,
1651
                         int x_off, int y_off, int block_w, int block_h, struct MvField *current_mv, int cidx)
1652
{
1653
7562898
    HEVCLocalContext *lc = s->HEVClc;
1654
7562898
    uint8_t *src1        = ref0->data[cidx+1];
1655
7562898
    uint8_t *src2        = ref1->data[cidx+1];
1656
7562898
    ptrdiff_t src1stride = ref0->linesize[cidx+1];
1657
7562898
    ptrdiff_t src2stride = ref1->linesize[cidx+1];
1658

15125796
    int weight_flag      = (s->sh.slice_type == HEVC_SLICE_P && s->ps.pps->weighted_pred_flag) ||
1659

7562898
                           (s->sh.slice_type == HEVC_SLICE_B && s->ps.pps->weighted_bipred_flag);
1660
7562898
    int pic_width        = s->ps.sps->width >> s->ps.sps->hshift[1];
1661
7562898
    int pic_height       = s->ps.sps->height >> s->ps.sps->vshift[1];
1662
7562898
    Mv *mv0              = &current_mv->mv[0];
1663
7562898
    Mv *mv1              = &current_mv->mv[1];
1664
7562898
    int hshift = s->ps.sps->hshift[1];
1665
7562898
    int vshift = s->ps.sps->vshift[1];
1666
1667
7562898
    intptr_t mx0 = av_mod_uintp2(mv0->x, 2 + hshift);
1668
7562898
    intptr_t my0 = av_mod_uintp2(mv0->y, 2 + vshift);
1669
7562898
    intptr_t mx1 = av_mod_uintp2(mv1->x, 2 + hshift);
1670
7562898
    intptr_t my1 = av_mod_uintp2(mv1->y, 2 + vshift);
1671
7562898
    intptr_t _mx0 = mx0 << (1 - hshift);
1672
7562898
    intptr_t _my0 = my0 << (1 - vshift);
1673
7562898
    intptr_t _mx1 = mx1 << (1 - hshift);
1674
7562898
    intptr_t _my1 = my1 << (1 - vshift);
1675
1676
7562898
    int x_off0 = x_off + (mv0->x >> (2 + hshift));
1677
7562898
    int y_off0 = y_off + (mv0->y >> (2 + vshift));
1678
7562898
    int x_off1 = x_off + (mv1->x >> (2 + hshift));
1679
7562898
    int y_off1 = y_off + (mv1->y >> (2 + vshift));
1680
7562898
    int idx = ff_hevc_pel_weight[block_w];
1681
7562898
    src1  += y_off0 * src1stride + (int)((unsigned)x_off0 << s->ps.sps->pixel_shift);
1682
7562898
    src2  += y_off1 * src2stride + (int)((unsigned)x_off1 << s->ps.sps->pixel_shift);
1683
1684

7562898
    if (x_off0 < EPEL_EXTRA_BEFORE || y_off0 < EPEL_EXTRA_AFTER ||
1685
7264516
        x_off0 >= pic_width - block_w - EPEL_EXTRA_AFTER ||
1686
7138414
        y_off0 >= pic_height - block_h - EPEL_EXTRA_AFTER) {
1687
825266
        const int edge_emu_stride = EDGE_EMU_BUFFER_STRIDE << s->ps.sps->pixel_shift;
1688
825266
        int offset1 = EPEL_EXTRA_BEFORE * (src1stride + (1 << s->ps.sps->pixel_shift));
1689
825266
        int buf_offset1 = EPEL_EXTRA_BEFORE *
1690
825266
                          (edge_emu_stride + (1 << s->ps.sps->pixel_shift));
1691
1692
825266
        s->vdsp.emulated_edge_mc(lc->edge_emu_buffer, src1 - offset1,
1693
                                 edge_emu_stride, src1stride,
1694
                                 block_w + EPEL_EXTRA, block_h + EPEL_EXTRA,
1695
                                 x_off0 - EPEL_EXTRA_BEFORE,
1696
                                 y_off0 - EPEL_EXTRA_BEFORE,
1697
                                 pic_width, pic_height);
1698
1699
825266
        src1 = lc->edge_emu_buffer + buf_offset1;
1700
825266
        src1stride = edge_emu_stride;
1701
    }
1702
1703

7562898
    if (x_off1 < EPEL_EXTRA_BEFORE || y_off1 < EPEL_EXTRA_AFTER ||
1704
7254974
        x_off1 >= pic_width - block_w - EPEL_EXTRA_AFTER ||
1705
7124282
        y_off1 >= pic_height - block_h - EPEL_EXTRA_AFTER) {
1706
847398
        const int edge_emu_stride = EDGE_EMU_BUFFER_STRIDE << s->ps.sps->pixel_shift;
1707
847398
        int offset1 = EPEL_EXTRA_BEFORE * (src2stride + (1 << s->ps.sps->pixel_shift));
1708
847398
        int buf_offset1 = EPEL_EXTRA_BEFORE *
1709
847398
                          (edge_emu_stride + (1 << s->ps.sps->pixel_shift));
1710
1711
847398
        s->vdsp.emulated_edge_mc(lc->edge_emu_buffer2, src2 - offset1,
1712
                                 edge_emu_stride, src2stride,
1713
                                 block_w + EPEL_EXTRA, block_h + EPEL_EXTRA,
1714
                                 x_off1 - EPEL_EXTRA_BEFORE,
1715
                                 y_off1 - EPEL_EXTRA_BEFORE,
1716
                                 pic_width, pic_height);
1717
1718
847398
        src2 = lc->edge_emu_buffer2 + buf_offset1;
1719
847398
        src2stride = edge_emu_stride;
1720
    }
1721
1722
7562898
    s->hevcdsp.put_hevc_epel[idx][!!my0][!!mx0](lc->tmp, src1, src1stride,
1723
                                                block_h, _mx0, _my0, block_w);
1724
7562898
    if (!weight_flag)
1725
7438284
        s->hevcdsp.put_hevc_epel_bi[idx][!!my1][!!mx1](dst0, s->frame->linesize[cidx+1],
1726
7438284
                                                       src2, src2stride, lc->tmp,
1727
                                                       block_h, _mx1, _my1, block_w);
1728
    else
1729
124614
        s->hevcdsp.put_hevc_epel_bi_w[idx][!!my1][!!mx1](dst0, s->frame->linesize[cidx+1],
1730
124614
                                                         src2, src2stride, lc->tmp,
1731
                                                         block_h,
1732
124614
                                                         s->sh.chroma_log2_weight_denom,
1733
124614
                                                         s->sh.chroma_weight_l0[current_mv->ref_idx[0]][cidx],
1734
124614
                                                         s->sh.chroma_weight_l1[current_mv->ref_idx[1]][cidx],
1735
124614
                                                         s->sh.chroma_offset_l0[current_mv->ref_idx[0]][cidx],
1736
124614
                                                         s->sh.chroma_offset_l1[current_mv->ref_idx[1]][cidx],
1737
                                                         _mx1, _my1, block_w);
1738
7562898
}
1739
1740
12360489
static void hevc_await_progress(HEVCContext *s, HEVCFrame *ref,
1741
                                const Mv *mv, int y0, int height)
1742
{
1743
12360489
    if (s->threads_type == FF_THREAD_FRAME ) {
1744
107575
        int y = FFMAX(0, (mv->y >> 2) + y0 + height + 9);
1745
1746
107575
        ff_thread_await_progress(&ref->tf, y, 0);
1747
    }
1748
12360489
}
1749
1750
2377473
static void hevc_luma_mv_mvp_mode(HEVCContext *s, int x0, int y0, int nPbW,
1751
                                  int nPbH, int log2_cb_size, int part_idx,
1752
                                  int merge_idx, MvField *mv)
1753
{
1754
2377473
    HEVCLocalContext *lc = s->HEVClc;
1755
2377473
    enum InterPredIdc inter_pred_idc = PRED_L0;
1756
    int mvp_flag;
1757
1758
2377473
    ff_hevc_set_neighbour_available(s, x0, y0, nPbW, nPbH);
1759
2377473
    mv->pred_flag = 0;
1760
2377473
    if (s->sh.slice_type == HEVC_SLICE_B)
1761
1811353
        inter_pred_idc = ff_hevc_inter_pred_idc_decode(s, nPbW, nPbH);
1762
1763
2377473
    if (inter_pred_idc != PRED_L1) {
1764
1993454
        if (s->sh.nb_refs[L0])
1765
1993454
            mv->ref_idx[0]= ff_hevc_ref_idx_lx_decode(s, s->sh.nb_refs[L0]);
1766
1767
1993454
        mv->pred_flag = PF_L0;
1768
1993454
        ff_hevc_hls_mvd_coding(s, x0, y0, 0);
1769
1993454
        mvp_flag = ff_hevc_mvp_lx_flag_decode(s);
1770
1993454
        ff_hevc_luma_mv_mvp_mode(s, x0, y0, nPbW, nPbH, log2_cb_size,
1771
                                 part_idx, merge_idx, mv, mvp_flag, 0);
1772
1993454
        mv->mv[0].x += lc->pu.mvd.x;
1773
1993454
        mv->mv[0].y += lc->pu.mvd.y;
1774
    }
1775
1776
2377473
    if (inter_pred_idc != PRED_L0) {
1777
788021
        if (s->sh.nb_refs[L1])
1778
788021
            mv->ref_idx[1]= ff_hevc_ref_idx_lx_decode(s, s->sh.nb_refs[L1]);
1779
1780

788021
        if (s->sh.mvd_l1_zero_flag == 1 && inter_pred_idc == PRED_BI) {
1781
157794
            AV_ZERO32(&lc->pu.mvd);
1782
        } else {
1783
630227
            ff_hevc_hls_mvd_coding(s, x0, y0, 1);
1784
        }
1785
1786
788021
        mv->pred_flag += PF_L1;
1787
788021
        mvp_flag = ff_hevc_mvp_lx_flag_decode(s);
1788
788021
        ff_hevc_luma_mv_mvp_mode(s, x0, y0, nPbW, nPbH, log2_cb_size,
1789
                                 part_idx, merge_idx, mv, mvp_flag, 1);
1790
788021
        mv->mv[1].x += lc->pu.mvd.x;
1791
788021
        mv->mv[1].y += lc->pu.mvd.y;
1792
    }
1793
2377473
}
1794
1795
8579040
static void hls_prediction_unit(HEVCContext *s, int x0, int y0,
1796
                                int nPbW, int nPbH,
1797
                                int log2_cb_size, int partIdx, int idx)
1798
{
1799
#define POS(c_idx, x, y)                                                              \
1800
    &s->frame->data[c_idx][((y) >> s->ps.sps->vshift[c_idx]) * s->frame->linesize[c_idx] + \
1801
                           (((x) >> s->ps.sps->hshift[c_idx]) << s->ps.sps->pixel_shift)]
1802
8579040
    HEVCLocalContext *lc = s->HEVClc;
1803
8579040
    int merge_idx = 0;
1804
8579040
    struct MvField current_mv = {{{ 0 }}};
1805
1806
8579040
    int min_pu_width = s->ps.sps->min_pu_width;
1807
1808
8579040
    MvField *tab_mvf = s->ref->tab_mvf;
1809
8579040
    RefPicList  *refPicList = s->ref->refPicList;
1810
8579040
    HEVCFrame *ref0 = NULL, *ref1 = NULL;
1811
8579040
    uint8_t *dst0 = POS(0, x0, y0);
1812
8579040
    uint8_t *dst1 = POS(1, x0, y0);
1813
8579040
    uint8_t *dst2 = POS(2, x0, y0);
1814
8579040
    int log2_min_cb_size = s->ps.sps->log2_min_cb_size;
1815
8579040
    int min_cb_width     = s->ps.sps->min_cb_width;
1816
8579040
    int x_cb             = x0 >> log2_min_cb_size;
1817
8579040
    int y_cb             = y0 >> log2_min_cb_size;
1818
    int x_pu, y_pu;
1819
    int i, j;
1820
1821
8579040
    int skip_flag = SAMPLE_CTB(s->skip_flag, x_cb, y_cb);
1822
1823
8579040
    if (!skip_flag)
1824
4706854
        lc->pu.merge_flag = ff_hevc_merge_flag_decode(s);
1825
1826

8579040
    if (skip_flag || lc->pu.merge_flag) {
1827
6201567
        if (s->sh.max_num_merge_cand > 1)
1828
6124714
            merge_idx = ff_hevc_merge_idx_decode(s);
1829
        else
1830
76853
            merge_idx = 0;
1831
1832
6201567
        ff_hevc_luma_mv_merge_mode(s, x0, y0, nPbW, nPbH, log2_cb_size,
1833
                                   partIdx, merge_idx, &current_mv);
1834
    } else {
1835
2377473
        hevc_luma_mv_mvp_mode(s, x0, y0, nPbW, nPbH, log2_cb_size,
1836
                              partIdx, merge_idx, &current_mv);
1837
    }
1838
1839
8579040
    x_pu = x0 >> s->ps.sps->log2_min_pu_size;
1840
8579040
    y_pu = y0 >> s->ps.sps->log2_min_pu_size;
1841
1842
40921612
    for (j = 0; j < nPbH >> s->ps.sps->log2_min_pu_size; j++)
1843
237414796
        for (i = 0; i < nPbW >> s->ps.sps->log2_min_pu_size; i++)
1844
205072224
            tab_mvf[(y_pu + j) * min_pu_width + x_pu + i] = current_mv;
1845
1846
8579040
    if (current_mv.pred_flag & PF_L0) {
1847
7818463
        ref0 = refPicList[0].ref[current_mv.ref_idx[0]];
1848
7818463
        if (!ref0)
1849
            return;
1850
7818463
        hevc_await_progress(s, ref0, &current_mv.mv[0], y0, nPbH);
1851
    }
1852
8579040
    if (current_mv.pred_flag & PF_L1) {
1853
4542026
        ref1 = refPicList[1].ref[current_mv.ref_idx[1]];
1854
4542026
        if (!ref1)
1855
            return;
1856
4542026
        hevc_await_progress(s, ref1, &current_mv.mv[1], y0, nPbH);
1857
    }
1858
1859
8579040
    if (current_mv.pred_flag == PF_L0) {
1860
4037014
        int x0_c = x0 >> s->ps.sps->hshift[1];
1861
4037014
        int y0_c = y0 >> s->ps.sps->vshift[1];
1862
4037014
        int nPbW_c = nPbW >> s->ps.sps->hshift[1];
1863
4037014
        int nPbH_c = nPbH >> s->ps.sps->vshift[1];
1864
1865
4037014
        luma_mc_uni(s, dst0, s->frame->linesize[0], ref0->frame,
1866
                    &current_mv.mv[0], x0, y0, nPbW, nPbH,
1867
4037014
                    s->sh.luma_weight_l0[current_mv.ref_idx[0]],
1868
4037014
                    s->sh.luma_offset_l0[current_mv.ref_idx[0]]);
1869
1870
4037014
        if (s->ps.sps->chroma_format_idc) {
1871
4037014
            chroma_mc_uni(s, dst1, s->frame->linesize[1], ref0->frame->data[1], ref0->frame->linesize[1],
1872
                          0, x0_c, y0_c, nPbW_c, nPbH_c, &current_mv,
1873
4037014
                          s->sh.chroma_weight_l0[current_mv.ref_idx[0]][0], s->sh.chroma_offset_l0[current_mv.ref_idx[0]][0]);
1874
4037014
            chroma_mc_uni(s, dst2, s->frame->linesize[2], ref0->frame->data[2], ref0->frame->linesize[2],
1875
                          0, x0_c, y0_c, nPbW_c, nPbH_c, &current_mv,
1876
4037014
                          s->sh.chroma_weight_l0[current_mv.ref_idx[0]][1], s->sh.chroma_offset_l0[current_mv.ref_idx[0]][1]);
1877
        }
1878
4542026
    } else if (current_mv.pred_flag == PF_L1) {
1879
760577
        int x0_c = x0 >> s->ps.sps->hshift[1];
1880
760577
        int y0_c = y0 >> s->ps.sps->vshift[1];
1881
760577
        int nPbW_c = nPbW >> s->ps.sps->hshift[1];
1882
760577
        int nPbH_c = nPbH >> s->ps.sps->vshift[1];
1883
1884
760577
        luma_mc_uni(s, dst0, s->frame->linesize[0], ref1->frame,
1885
                    &current_mv.mv[1], x0, y0, nPbW, nPbH,
1886
760577
                    s->sh.luma_weight_l1[current_mv.ref_idx[1]],
1887
760577
                    s->sh.luma_offset_l1[current_mv.ref_idx[1]]);
1888
1889
760577
        if (s->ps.sps->chroma_format_idc) {
1890
760577
            chroma_mc_uni(s, dst1, s->frame->linesize[1], ref1->frame->data[1], ref1->frame->linesize[1],
1891
                          1, x0_c, y0_c, nPbW_c, nPbH_c, &current_mv,
1892
760577
                          s->sh.chroma_weight_l1[current_mv.ref_idx[1]][0], s->sh.chroma_offset_l1[current_mv.ref_idx[1]][0]);
1893
1894
760577
            chroma_mc_uni(s, dst2, s->frame->linesize[2], ref1->frame->data[2], ref1->frame->linesize[2],
1895
                          1, x0_c, y0_c, nPbW_c, nPbH_c, &current_mv,
1896
760577
                          s->sh.chroma_weight_l1[current_mv.ref_idx[1]][1], s->sh.chroma_offset_l1[current_mv.ref_idx[1]][1]);
1897
        }
1898
3781449
    } else if (current_mv.pred_flag == PF_BI) {
1899
3781449
        int x0_c = x0 >> s->ps.sps->hshift[1];
1900
3781449
        int y0_c = y0 >> s->ps.sps->vshift[1];
1901
3781449
        int nPbW_c = nPbW >> s->ps.sps->hshift[1];
1902
3781449
        int nPbH_c = nPbH >> s->ps.sps->vshift[1];
1903
1904
3781449
        luma_mc_bi(s, dst0, s->frame->linesize[0], ref0->frame,
1905
                   &current_mv.mv[0], x0, y0, nPbW, nPbH,
1906
                   ref1->frame, &current_mv.mv[1], &current_mv);
1907
1908
3781449
        if (s->ps.sps->chroma_format_idc) {
1909
3781449
            chroma_mc_bi(s, dst1, s->frame->linesize[1], ref0->frame, ref1->frame,
1910
                         x0_c, y0_c, nPbW_c, nPbH_c, &current_mv, 0);
1911
1912
3781449
            chroma_mc_bi(s, dst2, s->frame->linesize[2], ref0->frame, ref1->frame,
1913
                         x0_c, y0_c, nPbW_c, nPbH_c, &current_mv, 1);
1914
        }
1915
    }
1916
}
1917
1918
/**
1919
 * 8.4.1
1920
 */
1921
7367714
static int luma_intra_pred_mode(HEVCContext *s, int x0, int y0, int pu_size,
1922
                                int prev_intra_luma_pred_flag)
1923
{
1924
7367714
    HEVCLocalContext *lc = s->HEVClc;
1925
7367714
    int x_pu             = x0 >> s->ps.sps->log2_min_pu_size;
1926
7367714
    int y_pu             = y0 >> s->ps.sps->log2_min_pu_size;
1927
7367714
    int min_pu_width     = s->ps.sps->min_pu_width;
1928
7367714
    int size_in_pus      = pu_size >> s->ps.sps->log2_min_pu_size;
1929
7367714
    int x0b              = av_mod_uintp2(x0, s->ps.sps->log2_ctb_size);
1930
7367714
    int y0b              = av_mod_uintp2(y0, s->ps.sps->log2_ctb_size);
1931
1932
1163687
    int cand_up   = (lc->ctb_up_flag || y0b) ?
1933
8531401
                    s->tab_ipm[(y_pu - 1) * min_pu_width + x_pu] : INTRA_DC;
1934
635780
    int cand_left = (lc->ctb_left_flag || x0b) ?
1935
8003494
                    s->tab_ipm[y_pu * min_pu_width + x_pu - 1]   : INTRA_DC;
1936
1937
7367714
    int y_ctb = (y0 >> (s->ps.sps->log2_ctb_size)) << (s->ps.sps->log2_ctb_size);
1938
1939
7367714
    MvField *tab_mvf = s->ref->tab_mvf;
1940
    int intra_pred_mode;
1941
    int candidate[3];
1942
    int i, j;
1943
1944
    // intra_pred_mode prediction does not cross vertical CTB boundaries
1945
7367714
    if ((y0 - 1) < y_ctb)
1946
909610
        cand_up = INTRA_DC;
1947
1948
7367714
    if (cand_left == cand_up) {
1949
1533572
        if (cand_left < 2) {
1950
892161
            candidate[0] = INTRA_PLANAR;
1951
892161
            candidate[1] = INTRA_DC;
1952
892161
            candidate[2] = INTRA_ANGULAR_26;
1953
        } else {
1954
641411
            candidate[0] = cand_left;
1955
641411
            candidate[1] = 2 + ((cand_left - 2 - 1 + 32) & 31);
1956
641411
            candidate[2] = 2 + ((cand_left - 2 + 1) & 31);
1957
        }
1958
    } else {
1959
5834142
        candidate[0] = cand_left;
1960
5834142
        candidate[1] = cand_up;
1961

5834142
        if (candidate[0] != INTRA_PLANAR && candidate[1] != INTRA_PLANAR) {
1962
4039502
            candidate[2] = INTRA_PLANAR;
1963

1794640
        } else if (candidate[0] != INTRA_DC && candidate[1] != INTRA_DC) {
1964
1205855
            candidate[2] = INTRA_DC;
1965
        } else {
1966
588785
            candidate[2] = INTRA_ANGULAR_26;
1967
        }
1968
    }
1969
1970
7367714
    if (prev_intra_luma_pred_flag) {
1971
4388604
        intra_pred_mode = candidate[lc->pu.mpm_idx];
1972
    } else {
1973
2979110
        if (candidate[0] > candidate[1])
1974
1426440
            FFSWAP(uint8_t, candidate[0], candidate[1]);
1975
2979110
        if (candidate[0] > candidate[2])
1976
1780084
            FFSWAP(uint8_t, candidate[0], candidate[2]);
1977
2979110
        if (candidate[1] > candidate[2])
1978
2224428
            FFSWAP(uint8_t, candidate[1], candidate[2]);
1979
1980
2979110
        intra_pred_mode = lc->pu.rem_intra_luma_pred_mode;
1981
11916440
        for (i = 0; i < 3; i++)
1982
8937330
            if (intra_pred_mode >= candidate[i])
1983
6512279
                intra_pred_mode++;
1984
    }
1985
1986
    /* write the intra prediction units into the mv array */
1987
7367714
    if (!size_in_pus)
1988
        size_in_pus = 1;
1989
20162436
    for (i = 0; i < size_in_pus; i++) {
1990
12794722
        memset(&s->tab_ipm[(y_pu + i) * min_pu_width + x_pu],
1991
               intra_pred_mode, size_in_pus);
1992
1993
52544826
        for (j = 0; j < size_in_pus; j++) {
1994
39750104
            tab_mvf[(y_pu + j) * min_pu_width + x_pu + i].pred_flag = PF_INTRA;
1995
        }
1996
    }
1997
1998
7367714
    return intra_pred_mode;
1999
}
2000
2001
10850432
static av_always_inline void set_ct_depth(HEVCContext *s, int x0, int y0,
2002
                                          int log2_cb_size, int ct_depth)
2003
{
2004
10850432
    int length = (1 << log2_cb_size) >> s->ps.sps->log2_min_cb_size;
2005
10850432
    int x_cb   = x0 >> s->ps.sps->log2_min_cb_size;
2006
10850432
    int y_cb   = y0 >> s->ps.sps->log2_min_cb_size;
2007
    int y;
2008
2009
30759817
    for (y = 0; y < length; y++)
2010
19909385
        memset(&s->tab_ct_depth[(y_cb + y) * s->ps.sps->min_cb_width + x_cb],
2011
               ct_depth, length);
2012
10850432
}
2013
2014
static const uint8_t tab_mode_idx[] = {
2015
     0,  1,  2,  2,  2,  2,  3,  5,  7,  8, 10, 12, 13, 15, 17, 18, 19, 20,
2016
    21, 22, 23, 23, 24, 24, 25, 25, 26, 27, 27, 28, 28, 29, 29, 30, 31};
2017
2018
3848735
static void intra_prediction_unit(HEVCContext *s, int x0, int y0,
2019
                                  int log2_cb_size)
2020
{
2021
3848735
    HEVCLocalContext *lc = s->HEVClc;
2022
    static const uint8_t intra_chroma_table[4] = { 0, 26, 10, 1 };
2023
    uint8_t prev_intra_luma_pred_flag[4];
2024
3848735
    int split   = lc->cu.part_mode == PART_NxN;
2025
3848735
    int pb_size = (1 << log2_cb_size) >> split;
2026
3848735
    int side    = split + 1;
2027
    int chroma_mode;
2028
    int i, j;
2029
2030
8870463
    for (i = 0; i < side; i++)
2031
12389442
        for (j = 0; j < side; j++)
2032
7367714
            prev_intra_luma_pred_flag[2 * i + j] = ff_hevc_prev_intra_luma_pred_flag_decode(s);
2033
2034
8870463
    for (i = 0; i < side; i++) {
2035
12389442
        for (j = 0; j < side; j++) {
2036
7367714
            if (prev_intra_luma_pred_flag[2 * i + j])
2037
4388604
                lc->pu.mpm_idx = ff_hevc_mpm_idx_decode(s);
2038
            else
2039
2979110
                lc->pu.rem_intra_luma_pred_mode = ff_hevc_rem_intra_luma_pred_mode_decode(s);
2040
2041
7367714
            lc->pu.intra_pred_mode[2 * i + j] =
2042
7367714
                luma_intra_pred_mode(s, x0 + pb_size * j, y0 + pb_size * i, pb_size,
2043
7367714
                                     prev_intra_luma_pred_flag[2 * i + j]);
2044
        }
2045
    }
2046
2047
3848735
    if (s->ps.sps->chroma_format_idc == 3) {
2048
243859
        for (i = 0; i < side; i++) {
2049
345412
            for (j = 0; j < side; j++) {
2050
206557
                lc->pu.chroma_mode_c[2 * i + j] = chroma_mode = ff_hevc_intra_chroma_pred_mode_decode(s);
2051
206557
                if (chroma_mode != 4) {
2052
34248
                    if (lc->pu.intra_pred_mode[2 * i + j] == intra_chroma_table[chroma_mode])
2053
2343
                        lc->pu.intra_pred_mode_c[2 * i + j] = 34;
2054
                    else
2055
31905
                        lc->pu.intra_pred_mode_c[2 * i + j] = intra_chroma_table[chroma_mode];
2056
                } else {
2057
172309
                    lc->pu.intra_pred_mode_c[2 * i + j] = lc->pu.intra_pred_mode[2 * i + j];
2058
                }
2059
            }
2060
        }
2061
3743731
    } else if (s->ps.sps->chroma_format_idc == 2) {
2062
        int mode_idx;
2063
212786
        lc->pu.chroma_mode_c[0] = chroma_mode = ff_hevc_intra_chroma_pred_mode_decode(s);
2064
212786
        if (chroma_mode != 4) {
2065
73995
            if (lc->pu.intra_pred_mode[0] == intra_chroma_table[chroma_mode])
2066
3515
                mode_idx = 34;
2067
            else
2068
70480
                mode_idx = intra_chroma_table[chroma_mode];
2069
        } else {
2070
138791
            mode_idx = lc->pu.intra_pred_mode[0];
2071
        }
2072
212786
        lc->pu.intra_pred_mode_c[0] = tab_mode_idx[mode_idx];
2073
3530945
    } else if (s->ps.sps->chroma_format_idc != 0) {
2074
3530753
        chroma_mode = ff_hevc_intra_chroma_pred_mode_decode(s);
2075
3530753
        if (chroma_mode != 4) {
2076
916359
            if (lc->pu.intra_pred_mode[0] == intra_chroma_table[chroma_mode])
2077
67820
                lc->pu.intra_pred_mode_c[0] = 34;
2078
            else
2079
848539
                lc->pu.intra_pred_mode_c[0] = intra_chroma_table[chroma_mode];
2080
        } else {
2081
2614394
            lc->pu.intra_pred_mode_c[0] = lc->pu.intra_pred_mode[0];
2082
        }
2083
    }
2084
3848735
}
2085
2086
7001698
static void intra_prediction_unit_default_value(HEVCContext *s,
2087
                                                int x0, int y0,
2088
                                                int log2_cb_size)
2089
{
2090
7001698
    HEVCLocalContext *lc = s->HEVClc;
2091
7001698
    int pb_size          = 1 << log2_cb_size;
2092
7001698
    int size_in_pus      = pb_size >> s->ps.sps->log2_min_pu_size;
2093
7001698
    int min_pu_width     = s->ps.sps->min_pu_width;
2094
7001698
    MvField *tab_mvf     = s->ref->tab_mvf;
2095
7001698
    int x_pu             = x0 >> s->ps.sps->log2_min_pu_size;
2096
7001698
    int y_pu             = y0 >> s->ps.sps->log2_min_pu_size;
2097
    int j, k;
2098
2099
7001698
    if (size_in_pus == 0)
2100
        size_in_pus = 1;
2101
36371734
    for (j = 0; j < size_in_pus; j++)
2102
29370036
        memset(&s->tab_ipm[(y_pu + j) * min_pu_width + x_pu], INTRA_DC, size_in_pus);
2103
7001698
    if (lc->cu.pred_mode == MODE_INTRA)
2104
53341
        for (j = 0; j < size_in_pus; j++)
2105
223420
            for (k = 0; k < size_in_pus; k++)
2106
182512
                tab_mvf[(y_pu + j) * min_pu_width + x_pu + k].pred_flag = PF_INTRA;
2107
7001698
}
2108
2109
10850433
static int hls_coding_unit(HEVCContext *s, int x0, int y0, int log2_cb_size)
2110
{
2111
10850433
    int cb_size          = 1 << log2_cb_size;
2112
10850433
    HEVCLocalContext *lc = s->HEVClc;
2113
10850433
    int log2_min_cb_size = s->ps.sps->log2_min_cb_size;
2114
10850433
    int length           = cb_size >> log2_min_cb_size;
2115
10850433
    int min_cb_width     = s->ps.sps->min_cb_width;
2116
10850433
    int x_cb             = x0 >> log2_min_cb_size;
2117
10850433
    int y_cb             = y0 >> log2_min_cb_size;
2118
10850433
    int idx              = log2_cb_size - 2;
2119
10850433
    int qp_block_mask    = (1<<(s->ps.sps->log2_ctb_size - s->ps.pps->diff_cu_qp_delta_depth)) - 1;
2120
    int x, y, ret;
2121
2122
10850433
    lc->cu.x                = x0;
2123
10850433
    lc->cu.y                = y0;
2124
10850433
    lc->cu.pred_mode        = MODE_INTRA;
2125
10850433
    lc->cu.part_mode        = PART_2Nx2N;
2126
10850433
    lc->cu.intra_split_flag = 0;
2127
2128
10850433
    SAMPLE_CTB(s->skip_flag, x_cb, y_cb) = 0;
2129
54252165
    for (x = 0; x < 4; x++)
2130
43401732
        lc->pu.intra_pred_mode[x] = 1;
2131
10850433
    if (s->ps.pps->transquant_bypass_enable_flag) {
2132
200139
        lc->cu.cu_transquant_bypass_flag = ff_hevc_cu_transquant_bypass_flag_decode(s);
2133
200139
        if (lc->cu.cu_transquant_bypass_flag)
2134
80538
            set_deblocking_bypass(s, x0, y0, log2_cb_size);
2135
    } else
2136
10650294
        lc->cu.cu_transquant_bypass_flag = 0;
2137
2138
10850433
    if (s->sh.slice_type != HEVC_SLICE_I) {
2139
8054524
        uint8_t skip_flag = ff_hevc_skip_flag_decode(s, x0, y0, x_cb, y_cb);
2140
2141
8054524
        x = y_cb * min_cb_width + x_cb;
2142
24315103
        for (y = 0; y < length; y++) {
2143
16260579
            memset(&s->skip_flag[x], skip_flag, length);
2144
16260579
            x += min_cb_width;
2145
        }
2146
8054524
        lc->cu.pred_mode = skip_flag ? MODE_SKIP : MODE_INTER;
2147
    } else {
2148
2795909
        x = y_cb * min_cb_width + x_cb;
2149
6444716
        for (y = 0; y < length; y++) {
2150
3648807
            memset(&s->skip_flag[x], 0, length);
2151
3648807
            x += min_cb_width;
2152
        }
2153
    }
2154
2155
10850433
    if (SAMPLE_CTB(s->skip_flag, x_cb, y_cb)) {
2156
3872186
        hls_prediction_unit(s, x0, y0, cb_size, cb_size, log2_cb_size, 0, idx);
2157
3872186
        intra_prediction_unit_default_value(s, x0, y0, log2_cb_size);
2158
2159
3872186
        if (!s->sh.disable_deblocking_filter_flag)
2160
3725673
            ff_hevc_deblocking_boundary_strengths(s, x0, y0, log2_cb_size);
2161
    } else {
2162
6978247
        int pcm_flag = 0;
2163
2164
6978247
        if (s->sh.slice_type != HEVC_SLICE_I)
2165
4182338
            lc->cu.pred_mode = ff_hevc_pred_mode_decode(s);
2166
6978247
        if (lc->cu.pred_mode != MODE_INTRA ||
2167
3861168
            log2_cb_size == s->ps.sps->log2_min_cb_size) {
2168
6093281
            lc->cu.part_mode        = ff_hevc_part_mode_decode(s, log2_cb_size);
2169
7371910
            lc->cu.intra_split_flag = lc->cu.part_mode == PART_NxN &&
2170
1278629
                                      lc->cu.pred_mode == MODE_INTRA;
2171
        }
2172
2173
6978247
        if (lc->cu.pred_mode == MODE_INTRA) {
2174

3861168
            if (lc->cu.part_mode == PART_2Nx2N && s->ps.sps->pcm_enabled_flag &&
2175
162684
                log2_cb_size >= s->ps.sps->pcm.log2_min_pcm_cb_size &&
2176
160815
                log2_cb_size <= s->ps.sps->pcm.log2_max_pcm_cb_size) {
2177
153183
                pcm_flag = ff_hevc_pcm_flag_decode(s);
2178
            }
2179
3861168
            if (pcm_flag) {
2180
12433
                intra_prediction_unit_default_value(s, x0, y0, log2_cb_size);
2181
12433
                ret = hls_pcm_sample(s, x0, y0, log2_cb_size);
2182
12433
                if (s->ps.sps->pcm.loop_filter_disable_flag)
2183
8
                    set_deblocking_bypass(s, x0, y0, log2_cb_size);
2184
2185
12433
                if (ret < 0)
2186
                    return ret;
2187
            } else {
2188
3848735
                intra_prediction_unit(s, x0, y0, log2_cb_size);
2189
            }
2190
        } else {
2191
3117079
            intra_prediction_unit_default_value(s, x0, y0, log2_cb_size);
2192


3117079
            switch (lc->cu.part_mode) {
2193
1738576
            case PART_2Nx2N:
2194
1738576
                hls_prediction_unit(s, x0, y0, cb_size, cb_size, log2_cb_size, 0, idx);
2195
1738576
                break;
2196
445726
            case PART_2NxN:
2197
445726
                hls_prediction_unit(s, x0, y0,               cb_size, cb_size / 2, log2_cb_size, 0, idx);
2198
445726
                hls_prediction_unit(s, x0, y0 + cb_size / 2, cb_size, cb_size / 2, log2_cb_size, 1, idx);
2199
445726
                break;
2200
535018
            case PART_Nx2N:
2201
535018
                hls_prediction_unit(s, x0,               y0, cb_size / 2, cb_size, log2_cb_size, 0, idx - 1);
2202
535018
                hls_prediction_unit(s, x0 + cb_size / 2, y0, cb_size / 2, cb_size, log2_cb_size, 1, idx - 1);
2203
535018
                break;
2204
63734
            case PART_2NxnU:
2205
63734
                hls_prediction_unit(s, x0, y0,               cb_size, cb_size     / 4, log2_cb_size, 0, idx);
2206
63734
                hls_prediction_unit(s, x0, y0 + cb_size / 4, cb_size, cb_size * 3 / 4, log2_cb_size, 1, idx);
2207
63734
                break;
2208
55171
            case PART_2NxnD:
2209
55171
                hls_prediction_unit(s, x0, y0,                   cb_size, cb_size * 3 / 4, log2_cb_size, 0, idx);
2210
55171
                hls_prediction_unit(s, x0, y0 + cb_size * 3 / 4, cb_size, cb_size     / 4, log2_cb_size, 1, idx);
2211
55171
                break;
2212
93167
            case PART_nLx2N:
2213
93167
                hls_prediction_unit(s, x0,               y0, cb_size     / 4, cb_size, log2_cb_size, 0, idx - 2);
2214
93167
                hls_prediction_unit(s, x0 + cb_size / 4, y0, cb_size * 3 / 4, cb_size, log2_cb_size, 1, idx - 2);
2215
93167
                break;
2216
80051
            case PART_nRx2N:
2217
80051
                hls_prediction_unit(s, x0,                   y0, cb_size * 3 / 4, cb_size, log2_cb_size, 0, idx - 2);
2218
80051
                hls_prediction_unit(s, x0 + cb_size * 3 / 4, y0, cb_size     / 4, cb_size, log2_cb_size, 1, idx - 2);
2219
80051
                break;
2220
105636
            case PART_NxN:
2221
105636
                hls_prediction_unit(s, x0,               y0,               cb_size / 2, cb_size / 2, log2_cb_size, 0, idx - 1);
2222
105636
                hls_prediction_unit(s, x0 + cb_size / 2, y0,               cb_size / 2, cb_size / 2, log2_cb_size, 1, idx - 1);
2223
105636
                hls_prediction_unit(s, x0,               y0 + cb_size / 2, cb_size / 2, cb_size / 2, log2_cb_size, 2, idx - 1);
2224
105636
                hls_prediction_unit(s, x0 + cb_size / 2, y0 + cb_size / 2, cb_size / 2, cb_size / 2, log2_cb_size, 3, idx - 1);
2225
105636
                break;
2226
            }
2227
6978247
        }
2228
2229
6978247
        if (!pcm_flag) {
2230
6965814
            int rqt_root_cbf = 1;
2231
2232
6965814
            if (lc->cu.pred_mode != MODE_INTRA &&
2233

3117079
                !(lc->cu.part_mode == PART_2Nx2N && lc->pu.merge_flag)) {
2234
2346082
                rqt_root_cbf = ff_hevc_no_residual_syntax_flag_decode(s);
2235
            }
2236
6965814
            if (rqt_root_cbf) {
2237
                const static int cbf[2] = { 0 };
2238
5650097
                lc->cu.max_trafo_depth = lc->cu.pred_mode == MODE_INTRA ?
2239
3848735
                                         s->ps.sps->max_transform_hierarchy_depth_intra + lc->cu.intra_split_flag :
2240
1801362
                                         s->ps.sps->max_transform_hierarchy_depth_inter;
2241
5650097
                ret = hls_transform_tree(s, x0, y0, x0, y0, x0, y0,
2242
                                         log2_cb_size,
2243
                                         log2_cb_size, 0, 0, cbf, cbf);
2244
5650097
                if (ret < 0)
2245
1
                    return ret;
2246
            } else {
2247
1315717
                if (!s->sh.disable_deblocking_filter_flag)
2248
1259285
                    ff_hevc_deblocking_boundary_strengths(s, x0, y0, log2_cb_size);
2249
            }
2250
        }
2251
    }
2252
2253

10850432
    if (s->ps.pps->cu_qp_delta_enabled_flag && lc->tu.is_cu_qp_delta_coded == 0)
2254
743468
        ff_hevc_set_qPy(s, x0, y0, log2_cb_size);
2255
2256
10850432
    x = y_cb * min_cb_width + x_cb;
2257
30759817
    for (y = 0; y < length; y++) {
2258
19909385
        memset(&s->qp_y_tab[x], lc->qp_y, length);
2259
19909385
        x += min_cb_width;
2260
    }
2261
2262
10850432
    if(((x0 + (1<<log2_cb_size)) & qp_block_mask) == 0 &&
2263
3640234
       ((y0 + (1<<log2_cb_size)) & qp_block_mask) == 0) {
2264
2098786
        lc->qPy_pred = lc->qp_y;
2265
    }
2266
2267
10850432
    set_ct_depth(s, x0, y0, log2_cb_size, lc->ct_depth);
2268
2269
10850432
    return 0;
2270
}
2271
2272
14163993
static int hls_coding_quadtree(HEVCContext *s, int x0, int y0,
2273
                               int log2_cb_size, int cb_depth)
2274
{
2275
14163993
    HEVCLocalContext *lc = s->HEVClc;
2276
14163993
    const int cb_size    = 1 << log2_cb_size;
2277
    int ret;
2278
    int split_cu;
2279
2280
14163993
    lc->ct_depth = cb_depth;
2281
14163993
    if (x0 + cb_size <= s->ps.sps->width  &&
2282
14135390
        y0 + cb_size <= s->ps.sps->height &&
2283
13821611
        log2_cb_size > s->ps.sps->log2_min_cb_size) {
2284
7385929
        split_cu = ff_hevc_split_coding_unit_flag_decode(s, cb_depth, x0, y0);
2285
    } else {
2286
6778064
        split_cu = (log2_cb_size > s->ps.sps->log2_min_cb_size);
2287
    }
2288
14163993
    if (s->ps.pps->cu_qp_delta_enabled_flag &&
2289
2375803
        log2_cb_size >= s->ps.sps->log2_ctb_size - s->ps.pps->diff_cu_qp_delta_depth) {
2290
1327815
        lc->tu.is_cu_qp_delta_coded = 0;
2291
1327815
        lc->tu.cu_qp_delta          = 0;
2292
    }
2293
2294
14163993
    if (s->sh.cu_chroma_qp_offset_enabled_flag &&
2295
519326
        log2_cb_size >= s->ps.sps->log2_ctb_size - s->ps.pps->diff_cu_chroma_qp_offset_depth) {
2296
519326
        lc->tu.is_cu_chroma_qp_offset_coded = 0;
2297
    }
2298
2299
14163993
    if (split_cu) {
2300
3313560
        int qp_block_mask = (1<<(s->ps.sps->log2_ctb_size - s->ps.pps->diff_cu_qp_delta_depth)) - 1;
2301
3313560
        const int cb_size_split = cb_size >> 1;
2302
3313560
        const int x1 = x0 + cb_size_split;
2303
3313560
        const int y1 = y0 + cb_size_split;
2304
2305
3313560
        int more_data = 0;
2306
2307
3313560
        more_data = hls_coding_quadtree(s, x0, y0, log2_cb_size - 1, cb_depth + 1);
2308
3313560
        if (more_data < 0)
2309
2
            return more_data;
2310
2311

3313558
        if (more_data && x1 < s->ps.sps->width) {
2312
3285868
            more_data = hls_coding_quadtree(s, x1, y0, log2_cb_size - 1, cb_depth + 1);
2313
3285868
            if (more_data < 0)
2314
                return more_data;
2315
        }
2316

3313558
        if (more_data && y1 < s->ps.sps->height) {
2317
3107421
            more_data = hls_coding_quadtree(s, x0, y1, log2_cb_size - 1, cb_depth + 1);
2318
3107421
            if (more_data < 0)
2319
                return more_data;
2320
        }
2321

3313558
        if (more_data && x1 < s->ps.sps->width &&
2322
3275663
            y1 < s->ps.sps->height) {
2323
3079740
            more_data = hls_coding_quadtree(s, x1, y1, log2_cb_size - 1, cb_depth + 1);
2324
3079740
            if (more_data < 0)
2325
1
                return more_data;
2326
        }
2327
2328
3313557
        if(((x0 + (1<<log2_cb_size)) & qp_block_mask) == 0 &&
2329
1853526
            ((y0 + (1<<log2_cb_size)) & qp_block_mask) == 0)
2330
1376880
            lc->qPy_pred = lc->qp_y;
2331
2332
3313557
        if (more_data)
2333
3368573
            return ((x1 + cb_size_split) < s->ps.sps->width ||
2334
90453
                    (y1 + cb_size_split) < s->ps.sps->height);
2335
        else
2336
35437
            return 0;
2337
    } else {
2338
10850433
        ret = hls_coding_unit(s, x0, y0, log2_cb_size);
2339
10850433
        if (ret < 0)
2340
1
            return ret;
2341
10850432
        if ((!((x0 + cb_size) %
2342
10850432
               (1 << (s->ps.sps->log2_ctb_size))) ||
2343
7944655
             (x0 + cb_size >= s->ps.sps->width)) &&
2344
2976474
            (!((y0 + cb_size) %
2345
2976474
               (1 << (s->ps.sps->log2_ctb_size))) ||
2346
1694448
             (y0 + cb_size >= s->ps.sps->height))) {
2347
1377403
            int end_of_slice_flag = ff_hevc_end_of_slice_flag_decode(s);
2348
1377403
            return !end_of_slice_flag;
2349
        } else {
2350
9473029
            return 1;
2351
        }
2352
    }
2353
2354
    return 0;
2355
}
2356
2357
1377404
static void hls_decode_neighbour(HEVCContext *s, int x_ctb, int y_ctb,
2358
                                 int ctb_addr_ts)
2359
{
2360
1377404
    HEVCLocalContext *lc  = s->HEVClc;
2361
1377404
    int ctb_size          = 1 << s->ps.sps->log2_ctb_size;
2362
1377404
    int ctb_addr_rs       = s->ps.pps->ctb_addr_ts_to_rs[ctb_addr_ts];
2363
1377404
    int ctb_addr_in_slice = ctb_addr_rs - s->sh.slice_addr;
2364
2365
1377404
    s->tab_slice_address[ctb_addr_rs] = s->sh.slice_addr;
2366
2367
1377404
    if (s->ps.pps->entropy_coding_sync_enabled_flag) {
2368

93384
        if (x_ctb == 0 && (y_ctb & (ctb_size - 1)) == 0)
2369
5334
            lc->first_qp_group = 1;
2370
93384
        lc->end_of_tiles_x = s->ps.sps->width;
2371
1284020
    } else if (s->ps.pps->tiles_enabled_flag) {
2372

271890
        if (ctb_addr_ts && s->ps.pps->tile_id[ctb_addr_ts] != s->ps.pps->tile_id[ctb_addr_ts - 1]) {
2373
6253
            int idxX = s->ps.pps->col_idxX[x_ctb >> s->ps.sps->log2_ctb_size];
2374
6253
            lc->end_of_tiles_x   = x_ctb + (s->ps.pps->column_width[idxX] << s->ps.sps->log2_ctb_size);
2375
6253
            lc->first_qp_group   = 1;
2376
        }
2377
    } else {
2378
1012130
        lc->end_of_tiles_x = s->ps.sps->width;
2379
    }
2380
2381
1377404
    lc->end_of_tiles_y = FFMIN(y_ctb + ctb_size, s->ps.sps->height);
2382
2383
1377404
    lc->boundary_flags = 0;
2384
1377404
    if (s->ps.pps->tiles_enabled_flag) {
2385

271890
        if (x_ctb > 0 && s->ps.pps->tile_id[ctb_addr_ts] != s->ps.pps->tile_id[s->ps.pps->ctb_addr_rs_to_ts[ctb_addr_rs - 1]])
2386
30253
            lc->boundary_flags |= BOUNDARY_LEFT_TILE;
2387

271890
        if (x_ctb > 0 && s->tab_slice_address[ctb_addr_rs] != s->tab_slice_address[ctb_addr_rs - 1])
2388
3649
            lc->boundary_flags |= BOUNDARY_LEFT_SLICE;
2389

271890
        if (y_ctb > 0 && s->ps.pps->tile_id[ctb_addr_ts] != s->ps.pps->tile_id[s->ps.pps->ctb_addr_rs_to_ts[ctb_addr_rs - s->ps.sps->ctb_width]])
2390
33061
            lc->boundary_flags |= BOUNDARY_UPPER_TILE;
2391

271890
        if (y_ctb > 0 && s->tab_slice_address[ctb_addr_rs] != s->tab_slice_address[ctb_addr_rs - s->ps.sps->ctb_width])
2392
13770
            lc->boundary_flags |= BOUNDARY_UPPER_SLICE;
2393
    } else {
2394
1105514
        if (ctb_addr_in_slice <= 0)
2395
17549
            lc->boundary_flags |= BOUNDARY_LEFT_SLICE;
2396
1105514
        if (ctb_addr_in_slice < s->ps.sps->ctb_width)
2397
183822
            lc->boundary_flags |= BOUNDARY_UPPER_SLICE;
2398
    }
2399
2400

1377404
    lc->ctb_left_flag = ((x_ctb > 0) && (ctb_addr_in_slice > 0) && !(lc->boundary_flags & BOUNDARY_LEFT_TILE));
2401

1377404
    lc->ctb_up_flag   = ((y_ctb > 0) && (ctb_addr_in_slice >= s->ps.sps->ctb_width) && !(lc->boundary_flags & BOUNDARY_UPPER_TILE));
2402

1377404
    lc->ctb_up_right_flag = ((y_ctb > 0)  && (ctb_addr_in_slice+1 >= s->ps.sps->ctb_width) && (s->ps.pps->tile_id[ctb_addr_ts] == s->ps.pps->tile_id[s->ps.pps->ctb_addr_rs_to_ts[ctb_addr_rs+1 - s->ps.sps->ctb_width]]));
2403


1377404
    lc->ctb_up_left_flag = ((x_ctb > 0) && (y_ctb > 0)  && (ctb_addr_in_slice-1 >= s->ps.sps->ctb_width) && (s->ps.pps->tile_id[ctb_addr_ts] == s->ps.pps->tile_id[s->ps.pps->ctb_addr_rs_to_ts[ctb_addr_rs-1 - s->ps.sps->ctb_width]]));
2404
1377404
}
2405
2406
27459
static int hls_decode_entry(AVCodecContext *avctxt, void *isFilterThread)
2407
{
2408
27459
    HEVCContext *s  = avctxt->priv_data;
2409
27459
    int ctb_size    = 1 << s->ps.sps->log2_ctb_size;
2410
27459
    int more_data   = 1;
2411
27459
    int x_ctb       = 0;
2412
27459
    int y_ctb       = 0;
2413
27459
    int ctb_addr_ts = s->ps.pps->ctb_addr_rs_to_ts[s->sh.slice_ctb_addr_rs];
2414
    int ret;
2415
2416

27459
    if (!ctb_addr_ts && s->sh.dependent_slice_segment_flag) {
2417
        av_log(s->avctx, AV_LOG_ERROR, "Impossible initial tile.\n");
2418
        return AVERROR_INVALIDDATA;
2419
    }
2420
2421
27459
    if (s->sh.dependent_slice_segment_flag) {
2422
7947
        int prev_rs = s->ps.pps->ctb_addr_ts_to_rs[ctb_addr_ts - 1];
2423
7947
        if (s->tab_slice_address[prev_rs] != s->sh.slice_addr) {
2424
            av_log(s->avctx, AV_LOG_ERROR, "Previous slice segment missing\n");
2425
            return AVERROR_INVALIDDATA;
2426
        }
2427
    }
2428
2429

1404862
    while (more_data && ctb_addr_ts < s->ps.sps->ctb_size) {
2430
1377404
        int ctb_addr_rs = s->ps.pps->ctb_addr_ts_to_rs[ctb_addr_ts];
2431
2432
1377404
        x_ctb = (ctb_addr_rs % ((s->ps.sps->width + ctb_size - 1) >> s->ps.sps->log2_ctb_size)) << s->ps.sps->log2_ctb_size;
2433
1377404
        y_ctb = (ctb_addr_rs / ((s->ps.sps->width + ctb_size - 1) >> s->ps.sps->log2_ctb_size)) << s->ps.sps->log2_ctb_size;
2434
1377404
        hls_decode_neighbour(s, x_ctb, y_ctb, ctb_addr_ts);
2435
2436
1377404
        ret = ff_hevc_cabac_init(s, ctb_addr_ts);
2437
1377404
        if (ret < 0) {
2438
            s->tab_slice_address[ctb_addr_rs] = -1;
2439
            return ret;
2440
        }
2441
2442
1377404
        hls_sao_param(s, x_ctb >> s->ps.sps->log2_ctb_size, y_ctb >> s->ps.sps->log2_ctb_size);
2443
2444
1377404
        s->deblock[ctb_addr_rs].beta_offset = s->sh.beta_offset;
2445
1377404
        s->deblock[ctb_addr_rs].tc_offset   = s->sh.tc_offset;
2446
1377404
        s->filter_slice_edges[ctb_addr_rs]  = s->sh.slice_loop_filter_across_slices_enabled_flag;
2447
2448
1377404
        more_data = hls_coding_quadtree(s, x_ctb, y_ctb, s->ps.sps->log2_ctb_size, 0);
2449
1377404
        if (more_data < 0) {
2450
1
            s->tab_slice_address[ctb_addr_rs] = -1;
2451
1
            return more_data;
2452
        }
2453
2454
2455
1377403
        ctb_addr_ts++;
2456
1377403
        ff_hevc_save_states(s, ctb_addr_ts);
2457
1377403
        ff_hevc_hls_filters(s, x_ctb, y_ctb, ctb_size);
2458
    }
2459
2460
27458
    if (x_ctb + ctb_size >= s->ps.sps->width &&
2461
13952
        y_ctb + ctb_size >= s->ps.sps->height)
2462
9477
        ff_hevc_hls_filter(s, x_ctb, y_ctb, ctb_size);
2463
2464
27458
    return ctb_addr_ts;
2465
}
2466
2467
27459
static int hls_slice_data(HEVCContext *s)
2468
{
2469
    int arg[2];
2470
    int ret[2];
2471
2472
27459
    arg[0] = 0;
2473
27459
    arg[1] = 1;
2474
2475
27459
    s->avctx->execute(s->avctx, hls_decode_entry, arg, ret , 1, sizeof(int));
2476
27459
    return ret[0];
2477
}
2478
static int hls_decode_entry_wpp(AVCodecContext *avctxt, void *input_ctb_row, int job, int self_id)
2479
{
2480
    HEVCContext *s1  = avctxt->priv_data, *s;
2481
    HEVCLocalContext *lc;
2482
    int ctb_size    = 1<< s1->ps.sps->log2_ctb_size;
2483
    int more_data   = 1;
2484
    int *ctb_row_p    = input_ctb_row;
2485
    int ctb_row = ctb_row_p[job];
2486
    int ctb_addr_rs = s1->sh.slice_ctb_addr_rs + ctb_row * ((s1->ps.sps->width + ctb_size - 1) >> s1->ps.sps->log2_ctb_size);
2487
    int ctb_addr_ts = s1->ps.pps->ctb_addr_rs_to_ts[ctb_addr_rs];
2488
    int thread = ctb_row % s1->threads_number;
2489
    int ret;
2490
2491
    s = s1->sList[self_id];
2492
    lc = s->HEVClc;
2493
2494
    if(ctb_row) {
2495
        ret = init_get_bits8(&lc->gb, s->data + s->sh.offset[ctb_row - 1], s->sh.size[ctb_row - 1]);
2496
        if (ret < 0)
2497
            goto error;
2498
        ff_init_cabac_decoder(&lc->cc, s->data + s->sh.offset[(ctb_row)-1], s->sh.size[ctb_row - 1]);
2499
    }
2500
2501
    while(more_data && ctb_addr_ts < s->ps.sps->ctb_size) {
2502
        int x_ctb = (ctb_addr_rs % s->ps.sps->ctb_width) << s->ps.sps->log2_ctb_size;
2503
        int y_ctb = (ctb_addr_rs / s->ps.sps->ctb_width) << s->ps.sps->log2_ctb_size;
2504
2505
        hls_decode_neighbour(s, x_ctb, y_ctb, ctb_addr_ts);
2506
2507
        ff_thread_await_progress2(s->avctx, ctb_row, thread, SHIFT_CTB_WPP);
2508
2509
        if (atomic_load(&s1->wpp_err)) {
2510
            ff_thread_report_progress2(s->avctx, ctb_row , thread, SHIFT_CTB_WPP);
2511
            return 0;
2512
        }
2513
2514
        ret = ff_hevc_cabac_init(s, ctb_addr_ts);
2515
        if (ret < 0)
2516
            goto error;
2517
        hls_sao_param(s, x_ctb >> s->ps.sps->log2_ctb_size, y_ctb >> s->ps.sps->log2_ctb_size);
2518
        more_data = hls_coding_quadtree(s, x_ctb, y_ctb, s->ps.sps->log2_ctb_size, 0);
2519
2520
        if (more_data < 0) {
2521
            ret = more_data;
2522
            goto error;
2523
        }
2524
2525
        ctb_addr_ts++;
2526
2527
        ff_hevc_save_states(s, ctb_addr_ts);
2528
        ff_thread_report_progress2(s->avctx, ctb_row, thread, 1);
2529
        ff_hevc_hls_filters(s, x_ctb, y_ctb, ctb_size);
2530
2531
        if (!more_data && (x_ctb+ctb_size) < s->ps.sps->width && ctb_row != s->sh.num_entry_point_offsets) {
2532
            atomic_store(&s1->wpp_err, 1);
2533
            ff_thread_report_progress2(s->avctx, ctb_row ,thread, SHIFT_CTB_WPP);
2534
            return 0;
2535
        }
2536
2537
        if ((x_ctb+ctb_size) >= s->ps.sps->width && (y_ctb+ctb_size) >= s->ps.sps->height ) {
2538
            ff_hevc_hls_filter(s, x_ctb, y_ctb, ctb_size);
2539
            ff_thread_report_progress2(s->avctx, ctb_row , thread, SHIFT_CTB_WPP);
2540
            return ctb_addr_ts;
2541
        }
2542
        ctb_addr_rs       = s->ps.pps->ctb_addr_ts_to_rs[ctb_addr_ts];
2543
        x_ctb+=ctb_size;
2544
2545
        if(x_ctb >= s->ps.sps->width) {
2546
            break;
2547
        }
2548
    }
2549
    ff_thread_report_progress2(s->avctx, ctb_row ,thread, SHIFT_CTB_WPP);
2550
2551
    return 0;
2552
error:
2553
    s->tab_slice_address[ctb_addr_rs] = -1;
2554
    atomic_store(&s1->wpp_err, 1);
2555
    ff_thread_report_progress2(s->avctx, ctb_row ,thread, SHIFT_CTB_WPP);
2556
    return ret;
2557
}
2558
2559
static int hls_slice_data_wpp(HEVCContext *s, const H2645NAL *nal)
2560
{
2561
    const uint8_t *data = nal->data;
2562
    int length          = nal->size;
2563
    HEVCLocalContext *lc = s->HEVClc;
2564
    int *ret = av_malloc_array(s->sh.num_entry_point_offsets + 1, sizeof(int));
2565
    int *arg = av_malloc_array(s->sh.num_entry_point_offsets + 1, sizeof(int));
2566
    int64_t offset;
2567
    int64_t startheader, cmpt = 0;
2568
    int i, j, res = 0;
2569
2570
    if (!ret || !arg) {
2571
        av_free(ret);
2572
        av_free(arg);
2573
        return AVERROR(ENOMEM);
2574
    }
2575
2576
    if (s->sh.slice_ctb_addr_rs + s->sh.num_entry_point_offsets * s->ps.sps->ctb_width >= s->ps.sps->ctb_width * s->ps.sps->ctb_height) {
2577
        av_log(s->avctx, AV_LOG_ERROR, "WPP ctb addresses are wrong (%d %d %d %d)\n",
2578
            s->sh.slice_ctb_addr_rs, s->sh.num_entry_point_offsets,
2579
            s->ps.sps->ctb_width, s->ps.sps->ctb_height
2580
        );
2581
        res = AVERROR_INVALIDDATA;
2582
        goto error;
2583
    }
2584
2585
    ff_alloc_entries(s->avctx, s->sh.num_entry_point_offsets + 1);
2586
2587
    if (!s->sList[1]) {
2588
        for (i = 1; i < s->threads_number; i++) {
2589
            s->sList[i] = av_malloc(sizeof(HEVCContext));
2590
            memcpy(s->sList[i], s, sizeof(HEVCContext));
2591
            s->HEVClcList[i] = av_mallocz(sizeof(HEVCLocalContext));
2592
            s->sList[i]->HEVClc = s->HEVClcList[i];
2593
        }
2594
    }
2595
2596
    offset = (lc->gb.index >> 3);
2597
2598
    for (j = 0, cmpt = 0, startheader = offset + s->sh.entry_point_offset[0]; j < nal->skipped_bytes; j++) {
2599
        if (nal->skipped_bytes_pos[j] >= offset && nal->skipped_bytes_pos[j] < startheader) {
2600
            startheader--;
2601
            cmpt++;
2602
        }
2603
    }
2604
2605
    for (i = 1; i < s->sh.num_entry_point_offsets; i++) {
2606
        offset += (s->sh.entry_point_offset[i - 1] - cmpt);
2607
        for (j = 0, cmpt = 0, startheader = offset
2608
             + s->sh.entry_point_offset[i]; j < nal->skipped_bytes; j++) {
2609
            if (nal->skipped_bytes_pos[j] >= offset && nal->skipped_bytes_pos[j] < startheader) {
2610
                startheader--;
2611
                cmpt++;
2612
            }
2613
        }
2614
        s->sh.size[i - 1] = s->sh.entry_point_offset[i] - cmpt;
2615
        s->sh.offset[i - 1] = offset;
2616
2617
    }
2618
    if (s->sh.num_entry_point_offsets != 0) {
2619
        offset += s->sh.entry_point_offset[s->sh.num_entry_point_offsets - 1] - cmpt;
2620
        if (length < offset) {
2621
            av_log(s->avctx, AV_LOG_ERROR, "entry_point_offset table is corrupted\n");
2622
            res = AVERROR_INVALIDDATA;
2623
            goto error;
2624
        }
2625
        s->sh.size[s->sh.num_entry_point_offsets - 1] = length - offset;
2626
        s->sh.offset[s->sh.num_entry_point_offsets - 1] = offset;
2627
2628
    }
2629
    s->data = data;
2630
2631
    for (i = 1; i < s->threads_number; i++) {
2632
        s->sList[i]->HEVClc->first_qp_group = 1;
2633
        s->sList[i]->HEVClc->qp_y = s->sList[0]->HEVClc->qp_y;
2634
        memcpy(s->sList[i], s, sizeof(HEVCContext));
2635
        s->sList[i]->HEVClc = s->HEVClcList[i];
2636
    }
2637
2638
    atomic_store(&s->wpp_err, 0);
2639
    ff_reset_entries(s->avctx);
2640
2641
    for (i = 0; i <= s->sh.num_entry_point_offsets; i++) {
2642
        arg[i] = i;
2643
        ret[i] = 0;
2644
    }
2645
2646
    if (s->ps.pps->entropy_coding_sync_enabled_flag)
2647
        s->avctx->execute2(s->avctx, hls_decode_entry_wpp, arg, ret, s->sh.num_entry_point_offsets + 1);
2648
2649
    for (i = 0; i <= s->sh.num_entry_point_offsets; i++)
2650
        res += ret[i];
2651
error:
2652
    av_free(ret);
2653
    av_free(arg);
2654
    return res;
2655
}
2656
2657
9478
static int set_side_data(HEVCContext *s)
2658
{
2659
9478
    AVFrame *out = s->ref->frame;
2660
2661
9478
    if (s->sei.frame_packing.present &&
2662
        s->sei.frame_packing.arrangement_type >= 3 &&
2663
        s->sei.frame_packing.arrangement_type <= 5 &&
2664
        s->sei.frame_packing.content_interpretation_type > 0 &&
2665
        s->sei.frame_packing.content_interpretation_type < 3) {
2666
        AVStereo3D *stereo = av_stereo3d_create_side_data(out);
2667
        if (!stereo)
2668
            return AVERROR(ENOMEM);
2669
2670
        switch (s->sei.frame_packing.arrangement_type) {
2671
        case 3:
2672
            if (s->sei.frame_packing.quincunx_subsampling)
2673
                stereo->type = AV_STEREO3D_SIDEBYSIDE_QUINCUNX;
2674
            else
2675
                stereo->type = AV_STEREO3D_SIDEBYSIDE;
2676
            break;
2677
        case 4:
2678
            stereo->type = AV_STEREO3D_TOPBOTTOM;
2679
            break;
2680
        case 5:
2681
            stereo->type = AV_STEREO3D_FRAMESEQUENCE;
2682
            break;
2683
        }
2684
2685
        if (s->sei.frame_packing.content_interpretation_type == 2)
2686
            stereo->flags = AV_STEREO3D_FLAG_INVERT;
2687
2688
        if (s->sei.frame_packing.arrangement_type == 5) {
2689
            if (s->sei.frame_packing.current_frame_is_frame0_flag)
2690
                stereo->view = AV_STEREO3D_VIEW_LEFT;
2691
            else
2692
                stereo->view = AV_STEREO3D_VIEW_RIGHT;
2693
        }
2694
    }
2695
2696
9478
    if (s->sei.display_orientation.present &&
2697
        (s->sei.display_orientation.anticlockwise_rotation ||
2698
         s->sei.display_orientation.hflip || s->sei.display_orientation.vflip)) {
2699
        double angle = s->sei.display_orientation.anticlockwise_rotation * 360 / (double) (1 << 16);
2700
        AVFrameSideData *rotation = av_frame_new_side_data(out,
2701
                                                           AV_FRAME_DATA_DISPLAYMATRIX,
2702
                                                           sizeof(int32_t) * 9);
2703
        if (!rotation)
2704
            return AVERROR(ENOMEM);
2705
2706
        av_display_rotation_set((int32_t *)rotation->data, angle);
2707
        av_display_matrix_flip((int32_t *)rotation->data,
2708
                               s->sei.display_orientation.hflip,
2709
                               s->sei.display_orientation.vflip);
2710
    }
2711
2712
    // Decrement the mastering display flag when IRAP frame has no_rasl_output_flag=1
2713
    // so the side data persists for the entire coded video sequence.
2714
9478
    if (s->sei.mastering_display.present > 0 &&
2715
        IS_IRAP(s) && s->no_rasl_output_flag) {
2716
        s->sei.mastering_display.present--;
2717
    }
2718
9478
    if (s->sei.mastering_display.present) {
2719
        // HEVC uses a g,b,r ordering, which we convert to a more natural r,g,b
2720
        const int mapping[3] = {2, 0, 1};
2721
        const int chroma_den = 50000;
2722
        const int luma_den = 10000;
2723
        int i;
2724
        AVMasteringDisplayMetadata *metadata =
2725
            av_mastering_display_metadata_create_side_data(out);
2726
        if (!metadata)
2727
            return AVERROR(ENOMEM);
2728
2729
        for (i = 0; i < 3; i++) {
2730
            const int j = mapping[i];
2731
            metadata->display_primaries[i][0].num = s->sei.mastering_display.display_primaries[j][0];
2732
            metadata->display_primaries[i][0].den = chroma_den;
2733
            metadata->display_primaries[i][1].num = s->sei.mastering_display.display_primaries[j][1];
2734
            metadata->display_primaries[i][1].den = chroma_den;
2735
        }
2736
        metadata->white_point[0].num = s->sei.mastering_display.white_point[0];
2737
        metadata->white_point[0].den = chroma_den;
2738
        metadata->white_point[1].num = s->sei.mastering_display.white_point[1];
2739
        metadata->white_point[1].den = chroma_den;
2740
2741
        metadata->max_luminance.num = s->sei.mastering_display.max_luminance;
2742
        metadata->max_luminance.den = luma_den;
2743
        metadata->min_luminance.num = s->sei.mastering_display.min_luminance;
2744
        metadata->min_luminance.den = luma_den;
2745
        metadata->has_luminance = 1;
2746
        metadata->has_primaries = 1;
2747
2748
        av_log(s->avctx, AV_LOG_DEBUG, "Mastering Display Metadata:\n");
2749
        av_log(s->avctx, AV_LOG_DEBUG,
2750
               "r(%5.4f,%5.4f) g(%5.4f,%5.4f) b(%5.4f %5.4f) wp(%5.4f, %5.4f)\n",
2751
               av_q2d(metadata->display_primaries[0][0]),
2752
               av_q2d(metadata->display_primaries[0][1]),
2753
               av_q2d(metadata->display_primaries[1][0]),
2754
               av_q2d(metadata->display_primaries[1][1]),
2755
               av_q2d(metadata->display_primaries[2][0]),
2756
               av_q2d(metadata->display_primaries[2][1]),
2757
               av_q2d(metadata->white_point[0]), av_q2d(metadata->white_point[1]));
2758
        av_log(s->avctx, AV_LOG_DEBUG,
2759
               "min_luminance=%f, max_luminance=%f\n",
2760
               av_q2d(metadata->min_luminance), av_q2d(metadata->max_luminance));
2761
    }
2762
    // Decrement the mastering display flag when IRAP frame has no_rasl_output_flag=1
2763
    // so the side data persists for the entire coded video sequence.
2764
9478
    if (s->sei.content_light.present > 0 &&
2765
        IS_IRAP(s) && s->no_rasl_output_flag) {
2766
        s->sei.content_light.present--;
2767
    }
2768
9478
    if (s->sei.content_light.present) {
2769
        AVContentLightMetadata *metadata =
2770
            av_content_light_metadata_create_side_data(out);
2771
        if (!metadata)
2772
            return AVERROR(ENOMEM);
2773
        metadata->MaxCLL  = s->sei.content_light.max_content_light_level;
2774
        metadata->MaxFALL = s->sei.content_light.max_pic_average_light_level;
2775
2776
        av_log(s->avctx, AV_LOG_DEBUG, "Content Light Level Metadata:\n");
2777
        av_log(s->avctx, AV_LOG_DEBUG, "MaxCLL=%d, MaxFALL=%d\n",
2778
               metadata->MaxCLL, metadata->MaxFALL);
2779
    }
2780
2781
9478
    if (s->sei.a53_caption.a53_caption) {
2782
        AVFrameSideData* sd = av_frame_new_side_data(out,
2783
                                                     AV_FRAME_DATA_A53_CC,
2784
                                                     s->sei.a53_caption.a53_caption_size);
2785
        if (sd)
2786
            memcpy(sd->data, s->sei.a53_caption.a53_caption, s->sei.a53_caption.a53_caption_size);
2787
        av_freep(&s->sei.a53_caption.a53_caption);
2788
        s->sei.a53_caption.a53_caption_size = 0;
2789
        s->avctx->properties |= FF_CODEC_PROPERTY_CLOSED_CAPTIONS;
2790
    }
2791
2792
9478
    return 0;
2793
}
2794
2795
9478
static int hevc_frame_start(HEVCContext *s)
2796
{
2797
9478
    HEVCLocalContext *lc = s->HEVClc;
2798
9478
    int pic_size_in_ctb  = ((s->ps.sps->width  >> s->ps.sps->log2_min_cb_size) + 1) *
2799
9478
                           ((s->ps.sps->height >> s->ps.sps->log2_min_cb_size) + 1);
2800
    int ret;
2801
2802
9478
    memset(s->horizontal_bs, 0, s->bs_width * s->bs_height);
2803
9478
    memset(s->vertical_bs,   0, s->bs_width * s->bs_height);
2804
9478
    memset(s->cbf_luma,      0, s->ps.sps->min_tb_width * s->ps.sps->min_tb_height);
2805
9478
    memset(s->is_pcm,        0, (s->ps.sps->min_pu_width + 1) * (s->ps.sps->min_pu_height + 1));
2806
9478
    memset(s->tab_slice_address, -1, pic_size_in_ctb * sizeof(*s->tab_slice_address));
2807
2808
9478
    s->is_decoded        = 0;
2809
9478
    s->first_nal_type    = s->nal_unit_type;
2810
2811



9478
    s->no_rasl_output_flag = IS_IDR(s) || IS_BLA(s) || (s->nal_unit_type == HEVC_NAL_CRA_NUT && s->last_eos);
2812
2813
9478
    if (s->ps.pps->tiles_enabled_flag)
2814
620
        lc->end_of_tiles_x = s->ps.pps->column_width[0] << s->ps.sps->log2_ctb_size;
2815
2816
9478
    ret = ff_hevc_set_new_ref(s, &s->frame, s->poc);
2817
9478
    if (ret < 0)
2818
        goto fail;
2819
2820
9478
    ret = ff_hevc_frame_rps(s);
2821
9478
    if (ret < 0) {
2822
        av_log(s->avctx, AV_LOG_ERROR, "Error constructing the frame RPS.\n");
2823
        goto fail;
2824
    }
2825
2826

9478
    s->ref->frame->key_frame = IS_IRAP(s);
2827
2828
9478
    ret = set_side_data(s);
2829
9478
    if (ret < 0)
2830
        goto fail;
2831
2832
9478
    s->frame->pict_type = 3 - s->sh.slice_type;
2833
2834

9478
    if (!IS_IRAP(s))
2835
8949
        ff_hevc_bump_frame(s);
2836
2837
9478
    av_frame_unref(s->output_frame);
2838
9478
    ret = ff_hevc_output_frame(s, s->output_frame, 0);
2839
9478
    if (ret < 0)
2840
        goto fail;
2841
2842
9478
    if (!s->avctx->hwaccel)
2843
9478
        ff_thread_finish_setup(s->avctx);
2844
2845
9478
    return 0;
2846
2847
fail:
2848
    if (s->ref)
2849
        ff_hevc_unref_frame(s, s->ref, ~0);
2850
    s->ref = NULL;
2851
    return ret;
2852
}
2853
2854
37738
static int decode_nal_unit(HEVCContext *s, const H2645NAL *nal)
2855
{
2856
37738
    HEVCLocalContext *lc = s->HEVClc;
2857
37738
    GetBitContext *gb    = &lc->gb;
2858
    int ctb_addr_ts, ret;
2859
2860
37738
    *gb              = nal->gb;
2861
37738
    s->nal_unit_type = nal->type;
2862
37738
    s->temporal_id   = nal->temporal_id;
2863
2864


37738
    switch (s->nal_unit_type) {
2865
394
    case HEVC_NAL_VPS:
2866

394
        if (s->avctx->hwaccel && s->avctx->hwaccel->decode_params) {
2867
            ret = s->avctx->hwaccel->decode_params(s->avctx,
2868
                                                   nal->type,
2869
                                                   nal->raw_data,
2870
                                                   nal->raw_size);
2871
            if (ret < 0)
2872
                goto fail;
2873
        }
2874
394
        ret = ff_hevc_decode_nal_vps(gb, s->avctx, &s->ps);
2875
394
        if (ret < 0)
2876
            goto fail;
2877
394
        break;
2878
391
    case HEVC_NAL_SPS:
2879

391
        if (s->avctx->hwaccel && s->avctx->hwaccel->decode_params) {
2880
            ret = s->avctx->hwaccel->decode_params(s->avctx,
2881
                                                   nal->type,
2882
                                                   nal->raw_data,
2883
                                                   nal->raw_size);
2884
            if (ret < 0)
2885
                goto fail;
2886
        }
2887
391
        ret = ff_hevc_decode_nal_sps(gb, s->avctx, &s->ps,
2888
                                     s->apply_defdispwin);
2889
391
        if (ret < 0)
2890
            goto fail;
2891
391
        break;
2892
1349
    case HEVC_NAL_PPS:
2893

1349
        if (s->avctx->hwaccel && s->avctx->hwaccel->decode_params) {
2894
            ret = s->avctx->hwaccel->decode_params(s->avctx,
2895
                                                   nal->type,
2896
                                                   nal->raw_data,
2897
                                                   nal->raw_size);
2898
            if (ret < 0)
2899
                goto fail;
2900
        }
2901
1349
        ret = ff_hevc_decode_nal_pps(gb, s->avctx, &s->ps);
2902
1349
        if (ret < 0)
2903
2
            goto fail;
2904
1347
        break;
2905
7466
    case HEVC_NAL_SEI_PREFIX:
2906
    case HEVC_NAL_SEI_SUFFIX:
2907

7466
        if (s->avctx->hwaccel && s->avctx->hwaccel->decode_params) {
2908
            ret = s->avctx->hwaccel->decode_params(s->avctx,
2909
                                                   nal->type,
2910
                                                   nal->raw_data,
2911
                                                   nal->raw_size);
2912
            if (ret < 0)
2913
                goto fail;
2914
        }
2915
7466
        ret = ff_hevc_decode_nal_sei(gb, s->avctx, &s->sei, &s->ps, s->nal_unit_type);
2916
7466
        if (ret < 0)
2917
22
            goto fail;
2918
7444
        break;
2919
27511
    case HEVC_NAL_TRAIL_R:
2920
    case HEVC_NAL_TRAIL_N:
2921
    case HEVC_NAL_TSA_N:
2922
    case HEVC_NAL_TSA_R:
2923
    case HEVC_NAL_STSA_N:
2924
    case HEVC_NAL_STSA_R:
2925
    case HEVC_NAL_BLA_W_LP:
2926
    case HEVC_NAL_BLA_W_RADL:
2927
    case HEVC_NAL_BLA_N_LP:
2928
    case HEVC_NAL_IDR_W_RADL:
2929
    case HEVC_NAL_IDR_N_LP:
2930
    case HEVC_NAL_CRA_NUT:
2931
    case HEVC_NAL_RADL_N:
2932
    case HEVC_NAL_RADL_R:
2933
    case HEVC_NAL_RASL_N:
2934
    case HEVC_NAL_RASL_R:
2935
27511
        ret = hls_slice_header(s);
2936
27511
        if (ret < 0)
2937
22
            return ret;
2938
27489
        if (ret == 1) {
2939
1
            ret = AVERROR_INVALIDDATA;
2940
1
            goto fail;
2941
        }
2942
2943
2944
27488
        if (
2945

27488
            (s->avctx->skip_frame >= AVDISCARD_BIDIR && s->sh.slice_type == HEVC_SLICE_B) ||
2946

27488
            (s->avctx->skip_frame >= AVDISCARD_NONINTRA && s->sh.slice_type != HEVC_SLICE_I) ||
2947

27488
            (s->avctx->skip_frame >= AVDISCARD_NONKEY && !IS_IRAP(s))) {
2948
            break;
2949
        }
2950
2951
27488
        if (s->sh.first_slice_in_pic_flag) {
2952
9507
            if (s->max_ra == INT_MAX) {
2953


384
                if (s->nal_unit_type == HEVC_NAL_CRA_NUT || IS_BLA(s)) {
2954
18
                    s->max_ra = s->poc;
2955
                } else {
2956

366
                    if (IS_IDR(s))
2957
366
                        s->max_ra = INT_MIN;
2958
                }
2959
            }
2960
2961

9507
            if ((s->nal_unit_type == HEVC_NAL_RASL_R || s->nal_unit_type == HEVC_NAL_RASL_N) &&
2962
829
                s->poc <= s->max_ra) {
2963
29
                s->is_decoded = 0;
2964
29
                break;
2965
            } else {
2966

9478
                if (s->nal_unit_type == HEVC_NAL_RASL_R && s->poc > s->max_ra)
2967
463
                    s->max_ra = INT_MIN;
2968
            }
2969
2970
9478
            s->overlap ++;
2971
9478
            ret = hevc_frame_start(s);
2972
9478
            if (ret < 0)
2973
                return ret;
2974
17981
        } else if (!s->ref) {
2975
            av_log(s->avctx, AV_LOG_ERROR, "First slice in a frame missing.\n");
2976
            goto fail;
2977
        }
2978
2979
27459
        if (s->nal_unit_type != s->first_nal_type) {
2980
            av_log(s->avctx, AV_LOG_ERROR,
2981
                   "Non-matching NAL types of the VCL NALUs: %d %d\n",
2982
                   s->first_nal_type, s->nal_unit_type);
2983
            return AVERROR_INVALIDDATA;
2984
        }
2985
2986
27459
        if (!s->sh.dependent_slice_segment_flag &&
2987
19512
            s->sh.slice_type != HEVC_SLICE_I) {
2988
18398
            ret = ff_hevc_slice_rpl(s);
2989
18398
            if (ret < 0) {
2990
                av_log(s->avctx, AV_LOG_WARNING,
2991
                       "Error constructing the reference lists for the current slice.\n");
2992
                goto fail;
2993
            }
2994
        }
2995
2996

27459
        if (s->sh.first_slice_in_pic_flag && s->avctx->hwaccel) {
2997
            ret = s->avctx->hwaccel->start_frame(s->avctx, NULL, 0);
2998
            if (ret < 0)
2999
                goto fail;
3000
        }
3001
3002
27459
        if (s->avctx->hwaccel) {
3003
            ret = s->avctx->hwaccel->decode_slice(s->avctx, nal->raw_data, nal->raw_size);
3004
            if (ret < 0)
3005
                goto fail;
3006
        } else {
3007

27459
            if (s->threads_number > 1 && s->sh.num_entry_point_offsets > 0)
3008
                ctb_addr_ts = hls_slice_data_wpp(s, nal);
3009
            else
3010
27459
                ctb_addr_ts = hls_slice_data(s);
3011
27459
            if (ctb_addr_ts >= (s->ps.sps->ctb_width * s->ps.sps->ctb_height)) {
3012
9477
                s->is_decoded = 1;
3013
            }
3014
3015
27459
            if (ctb_addr_ts < 0) {
3016
1
                ret = ctb_addr_ts;
3017
1
                goto fail;
3018
            }
3019
        }
3020
27458
        break;
3021
4
    case HEVC_NAL_EOS_NUT:
3022
    case HEVC_NAL_EOB_NUT:
3023
4
        s->seq_decode = (s->seq_decode + 1) & 0xff;
3024
4
        s->max_ra     = INT_MAX;
3025
4
        break;
3026
623
    case HEVC_NAL_AUD:
3027
    case HEVC_NAL_FD_NUT:
3028
623
        break;
3029
    default:
3030
        av_log(s->avctx, AV_LOG_INFO,
3031
               "Skipping NAL unit %d\n", s->nal_unit_type);
3032
    }
3033
3034
37690
    return 0;
3035
26
fail:
3036
26
    if (s->avctx->err_recognition & AV_EF_EXPLODE)
3037
        return ret;
3038
26
    return 0;
3039
}
3040
3041
9530
static int decode_nal_units(HEVCContext *s, const uint8_t *buf, int length)
3042
{
3043
9530
    int i, ret = 0;
3044
9530
    int eos_at_start = 1;
3045
3046
9530
    s->ref = NULL;
3047
9530
    s->last_eos = s->eos;
3048
9530
    s->eos = 0;
3049
9530
    s->overlap = 0;
3050
3051
    /* split the input packet into NAL units, so we know the upper bound on the
3052
     * number of slices in the frame */
3053
9530
    ret = ff_h2645_packet_split(&s->pkt, buf, length, s->avctx, s->is_nalff,
3054
9530
                                s->nal_length_size, s->avctx->codec_id, 1, 0);
3055
9530
    if (ret < 0) {
3056
        av_log(s->avctx, AV_LOG_ERROR,
3057
               "Error splitting the input into NAL units.\n");
3058
        return ret;
3059
    }
3060
3061
47268
    for (i = 0; i < s->pkt.nb_nals; i++) {
3062
37738
        if (s->pkt.nals[i].type == HEVC_NAL_EOB_NUT ||
3063
37737
            s->pkt.nals[i].type == HEVC_NAL_EOS_NUT) {
3064
4
            if (eos_at_start) {
3065
4
                s->last_eos = 1;
3066
            } else {
3067
                s->eos = 1;
3068
            }
3069
        } else {
3070
37734
            eos_at_start = 0;
3071
        }
3072
    }
3073
3074
    /* decode the NAL units */
3075
47246
    for (i = 0; i < s->pkt.nb_nals; i++) {
3076
37738
        H2645NAL *nal = &s->pkt.nals[i];
3077
3078
37738
        if (s->avctx->skip_frame >= AVDISCARD_ALL ||
3079
37738
            (s->avctx->skip_frame >= AVDISCARD_NONREF
3080
            && ff_hevc_nal_is_nonref(nal->type)))
3081
            continue;
3082
3083
37738
        ret = decode_nal_unit(s, nal);
3084

37738
        if (ret >= 0 && s->overlap > 2)
3085
            ret = AVERROR_INVALIDDATA;
3086
37738
        if (ret < 0) {
3087
22
            av_log(s->avctx, AV_LOG_WARNING,
3088
                   "Error parsing NAL unit #%d.\n", i);
3089
22
            goto fail;
3090
        }
3091
    }
3092
3093
9508
fail:
3094

9530
    if (s->ref && s->threads_type == FF_THREAD_FRAME)
3095
32
        ff_thread_report_progress(&s->ref->tf, INT_MAX, 0);
3096
3097
9530
    return ret;
3098
}
3099
3100
static void print_md5(void *log_ctx, int level, uint8_t md5[16])
3101
{
3102
    int i;
3103
    for (i = 0; i < 16; i++)
3104
        av_log(log_ctx, level, "%02"PRIx8, md5[i]);
3105
}
3106
3107
static int verify_md5(HEVCContext *s, AVFrame *frame)
3108
{
3109
    const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(frame->format);
3110
    int pixel_shift;
3111
    int i, j;
3112
3113
    if (!desc)
3114
        return AVERROR(EINVAL);
3115
3116
    pixel_shift = desc->comp[0].depth > 8;
3117
3118
    av_log(s->avctx, AV_LOG_DEBUG, "Verifying checksum for frame with POC %d: ",
3119
           s->poc);
3120
3121
    /* the checksums are LE, so we have to byteswap for >8bpp formats
3122
     * on BE arches */
3123
#if HAVE_BIGENDIAN
3124
    if (pixel_shift && !s->checksum_buf) {
3125
        av_fast_malloc(&s->checksum_buf, &s->checksum_buf_size,
3126
                       FFMAX3(frame->linesize[0], frame->linesize[1],
3127
                              frame->linesize[2]));
3128
        if (!s->checksum_buf)
3129
            return AVERROR(ENOMEM);
3130
    }
3131
#endif
3132
3133
    for (i = 0; frame->data[i]; i++) {
3134
        int width  = s->avctx->coded_width;
3135
        int height = s->avctx->coded_height;
3136
        int w = (i == 1 || i == 2) ? (width  >> desc->log2_chroma_w) : width;
3137
        int h = (i == 1 || i == 2) ? (height >> desc->log2_chroma_h) : height;
3138
        uint8_t md5[16];
3139
3140
        av_md5_init(s->md5_ctx);
3141
        for (j = 0; j < h; j++) {
3142
            const uint8_t *src = frame->data[i] + j * frame->linesize[i];
3143
#if HAVE_BIGENDIAN
3144
            if (pixel_shift) {
3145
                s->bdsp.bswap16_buf((uint16_t *) s->checksum_buf,
3146
                                    (const uint16_t *) src, w);
3147
                src = s->checksum_buf;
3148
            }
3149
#endif
3150
            av_md5_update(s->md5_ctx, src, w << pixel_shift);
3151
        }
3152
        av_md5_final(s->md5_ctx, md5);
3153
3154
        if (!memcmp(md5, s->sei.picture_hash.md5[i], 16)) {
3155
            av_log   (s->avctx, AV_LOG_DEBUG, "plane %d - correct ", i);
3156
            print_md5(s->avctx, AV_LOG_DEBUG, md5);
3157
            av_log   (s->avctx, AV_LOG_DEBUG, "; ");
3158
        } else {
3159
            av_log   (s->avctx, AV_LOG_ERROR, "mismatching checksum of plane %d - ", i);
3160
            print_md5(s->avctx, AV_LOG_ERROR, md5);
3161
            av_log   (s->avctx, AV_LOG_ERROR, " != ");
3162
            print_md5(s->avctx, AV_LOG_ERROR, s->sei.picture_hash.md5[i]);
3163
            av_log   (s->avctx, AV_LOG_ERROR, "\n");
3164
            return AVERROR_INVALIDDATA;
3165
        }
3166
    }
3167
3168
    av_log(s->avctx, AV_LOG_DEBUG, "\n");
3169
3170
    return 0;
3171
}
3172
3173
177
static int hevc_decode_extradata(HEVCContext *s, uint8_t *buf, int length, int first)
3174
{
3175
    int ret, i;
3176
3177
177
    ret = ff_hevc_decode_extradata(buf, length, &s->ps, &s->sei, &s->is_nalff,
3178
177
                                   &s->nal_length_size, s->avctx->err_recognition,
3179
177
                                   s->apply_defdispwin, s->avctx);
3180
177
    if (ret < 0)
3181
        return ret;
3182
3183
    /* export stream parameters from the first SPS */
3184
209
    for (i = 0; i < FF_ARRAY_ELEMS(s->ps.sps_list); i++) {
3185

207
        if (first && s->ps.sps_list[i]) {
3186
175
            const HEVCSPS *sps = (const HEVCSPS*)s->ps.sps_list[i]->data;
3187
175
            export_stream_params(s, sps);
3188
175
            break;
3189
        }
3190
    }
3191
3192
177
    return 0;
3193
}
3194
3195
9997
static int hevc_decode_frame(AVCodecContext *avctx, void *data, int *got_output,
3196
                             AVPacket *avpkt)
3197
{
3198
    int ret;
3199
    int new_extradata_size;
3200
    uint8_t *new_extradata;
3201
9997
    HEVCContext *s = avctx->priv_data;
3202
3203
9997
    if (!avpkt->size) {
3204
467
        ret = ff_hevc_output_frame(s, data, 1);
3205
467
        if (ret < 0)
3206
            return ret;
3207
3208
467
        *got_output = ret;
3209
467
        return 0;
3210
    }
3211
3212
9530
    new_extradata = av_packet_get_side_data(avpkt, AV_PKT_DATA_NEW_EXTRADATA,
3213
                                            &new_extradata_size);
3214

9530
    if (new_extradata && new_extradata_size > 0) {
3215
1
        ret = hevc_decode_extradata(s, new_extradata, new_extradata_size, 0);
3216
1
        if (ret < 0)
3217
            return ret;
3218
    }
3219
3220
9530
    s->ref = NULL;
3221
9530
    ret    = decode_nal_units(s, avpkt->data, avpkt->size);
3222
9530
    if (ret < 0)
3223
22
        return ret;
3224
3225
9508
    if (avctx->hwaccel) {
3226
        if (s->ref && (ret = avctx->hwaccel->end_frame(avctx)) < 0) {
3227