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