Coverage Report

Created: 2026-10-17 07:46

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