Merge commit 'a1a143adb0fd11c474221431417cff25db7d920f'
[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     if (s->threads_type == FF_THREAD_FRAME ) {
1688         int y = FFMAX(0, (mv->y >> 2) + y0 + height + 9);
1689
1690         ff_thread_await_progress(&ref->tf, y, 0);
1691     }
1692 }
1693
1694 static void hevc_luma_mv_mvp_mode(HEVCContext *s, int x0, int y0, int nPbW,
1695                                   int nPbH, int log2_cb_size, int part_idx,
1696                                   int merge_idx, MvField *mv)
1697 {
1698     HEVCLocalContext *lc = s->HEVClc;
1699     enum InterPredIdc inter_pred_idc = PRED_L0;
1700     int mvp_flag;
1701
1702     ff_hevc_set_neighbour_available(s, x0, y0, nPbW, nPbH);
1703     mv->pred_flag = 0;
1704     if (s->sh.slice_type == HEVC_SLICE_B)
1705         inter_pred_idc = ff_hevc_inter_pred_idc_decode(s, nPbW, nPbH);
1706
1707     if (inter_pred_idc != PRED_L1) {
1708         if (s->sh.nb_refs[L0])
1709             mv->ref_idx[0]= ff_hevc_ref_idx_lx_decode(s, s->sh.nb_refs[L0]);
1710
1711         mv->pred_flag = PF_L0;
1712         ff_hevc_hls_mvd_coding(s, x0, y0, 0);
1713         mvp_flag = ff_hevc_mvp_lx_flag_decode(s);
1714         ff_hevc_luma_mv_mvp_mode(s, x0, y0, nPbW, nPbH, log2_cb_size,
1715                                  part_idx, merge_idx, mv, mvp_flag, 0);
1716         mv->mv[0].x += lc->pu.mvd.x;
1717         mv->mv[0].y += lc->pu.mvd.y;
1718     }
1719
1720     if (inter_pred_idc != PRED_L0) {
1721         if (s->sh.nb_refs[L1])
1722             mv->ref_idx[1]= ff_hevc_ref_idx_lx_decode(s, s->sh.nb_refs[L1]);
1723
1724         if (s->sh.mvd_l1_zero_flag == 1 && inter_pred_idc == PRED_BI) {
1725             AV_ZERO32(&lc->pu.mvd);
1726         } else {
1727             ff_hevc_hls_mvd_coding(s, x0, y0, 1);
1728         }
1729
1730         mv->pred_flag += PF_L1;
1731         mvp_flag = ff_hevc_mvp_lx_flag_decode(s);
1732         ff_hevc_luma_mv_mvp_mode(s, x0, y0, nPbW, nPbH, log2_cb_size,
1733                                  part_idx, merge_idx, mv, mvp_flag, 1);
1734         mv->mv[1].x += lc->pu.mvd.x;
1735         mv->mv[1].y += lc->pu.mvd.y;
1736     }
1737 }
1738
1739 static void hls_prediction_unit(HEVCContext *s, int x0, int y0,
1740                                 int nPbW, int nPbH,
1741                                 int log2_cb_size, int partIdx, int idx)
1742 {
1743 #define POS(c_idx, x, y)                                                              \
1744     &s->frame->data[c_idx][((y) >> s->ps.sps->vshift[c_idx]) * s->frame->linesize[c_idx] + \
1745                            (((x) >> s->ps.sps->hshift[c_idx]) << s->ps.sps->pixel_shift)]
1746     HEVCLocalContext *lc = s->HEVClc;
1747     int merge_idx = 0;
1748     struct MvField current_mv = {{{ 0 }}};
1749
1750     int min_pu_width = s->ps.sps->min_pu_width;
1751
1752     MvField *tab_mvf = s->ref->tab_mvf;
1753     RefPicList  *refPicList = s->ref->refPicList;
1754     HEVCFrame *ref0 = NULL, *ref1 = NULL;
1755     uint8_t *dst0 = POS(0, x0, y0);
1756     uint8_t *dst1 = POS(1, x0, y0);
1757     uint8_t *dst2 = POS(2, x0, y0);
1758     int log2_min_cb_size = s->ps.sps->log2_min_cb_size;
1759     int min_cb_width     = s->ps.sps->min_cb_width;
1760     int x_cb             = x0 >> log2_min_cb_size;
1761     int y_cb             = y0 >> log2_min_cb_size;
1762     int x_pu, y_pu;
1763     int i, j;
1764
1765     int skip_flag = SAMPLE_CTB(s->skip_flag, x_cb, y_cb);
1766
1767     if (!skip_flag)
1768         lc->pu.merge_flag = ff_hevc_merge_flag_decode(s);
1769
1770     if (skip_flag || lc->pu.merge_flag) {
1771         if (s->sh.max_num_merge_cand > 1)
1772             merge_idx = ff_hevc_merge_idx_decode(s);
1773         else
1774             merge_idx = 0;
1775
1776         ff_hevc_luma_mv_merge_mode(s, x0, y0, nPbW, nPbH, log2_cb_size,
1777                                    partIdx, merge_idx, &current_mv);
1778     } else {
1779         hevc_luma_mv_mvp_mode(s, x0, y0, nPbW, nPbH, log2_cb_size,
1780                               partIdx, merge_idx, &current_mv);
1781     }
1782
1783     x_pu = x0 >> s->ps.sps->log2_min_pu_size;
1784     y_pu = y0 >> s->ps.sps->log2_min_pu_size;
1785
1786     for (j = 0; j < nPbH >> s->ps.sps->log2_min_pu_size; j++)
1787         for (i = 0; i < nPbW >> s->ps.sps->log2_min_pu_size; i++)
1788             tab_mvf[(y_pu + j) * min_pu_width + x_pu + i] = current_mv;
1789
1790     if (current_mv.pred_flag & PF_L0) {
1791         ref0 = refPicList[0].ref[current_mv.ref_idx[0]];
1792         if (!ref0)
1793             return;
1794         hevc_await_progress(s, ref0, &current_mv.mv[0], y0, nPbH);
1795     }
1796     if (current_mv.pred_flag & PF_L1) {
1797         ref1 = refPicList[1].ref[current_mv.ref_idx[1]];
1798         if (!ref1)
1799             return;
1800         hevc_await_progress(s, ref1, &current_mv.mv[1], y0, nPbH);
1801     }
1802
1803     if (current_mv.pred_flag == PF_L0) {
1804         int x0_c = x0 >> s->ps.sps->hshift[1];
1805         int y0_c = y0 >> s->ps.sps->vshift[1];
1806         int nPbW_c = nPbW >> s->ps.sps->hshift[1];
1807         int nPbH_c = nPbH >> s->ps.sps->vshift[1];
1808
1809         luma_mc_uni(s, dst0, s->frame->linesize[0], ref0->frame,
1810                     &current_mv.mv[0], x0, y0, nPbW, nPbH,
1811                     s->sh.luma_weight_l0[current_mv.ref_idx[0]],
1812                     s->sh.luma_offset_l0[current_mv.ref_idx[0]]);
1813
1814         if (s->ps.sps->chroma_format_idc) {
1815             chroma_mc_uni(s, dst1, s->frame->linesize[1], ref0->frame->data[1], ref0->frame->linesize[1],
1816                           0, x0_c, y0_c, nPbW_c, nPbH_c, &current_mv,
1817                           s->sh.chroma_weight_l0[current_mv.ref_idx[0]][0], s->sh.chroma_offset_l0[current_mv.ref_idx[0]][0]);
1818             chroma_mc_uni(s, dst2, s->frame->linesize[2], ref0->frame->data[2], ref0->frame->linesize[2],
1819                           0, x0_c, y0_c, nPbW_c, nPbH_c, &current_mv,
1820                           s->sh.chroma_weight_l0[current_mv.ref_idx[0]][1], s->sh.chroma_offset_l0[current_mv.ref_idx[0]][1]);
1821         }
1822     } else if (current_mv.pred_flag == PF_L1) {
1823         int x0_c = x0 >> s->ps.sps->hshift[1];
1824         int y0_c = y0 >> s->ps.sps->vshift[1];
1825         int nPbW_c = nPbW >> s->ps.sps->hshift[1];
1826         int nPbH_c = nPbH >> s->ps.sps->vshift[1];
1827
1828         luma_mc_uni(s, dst0, s->frame->linesize[0], ref1->frame,
1829                     &current_mv.mv[1], x0, y0, nPbW, nPbH,
1830                     s->sh.luma_weight_l1[current_mv.ref_idx[1]],
1831                     s->sh.luma_offset_l1[current_mv.ref_idx[1]]);
1832
1833         if (s->ps.sps->chroma_format_idc) {
1834             chroma_mc_uni(s, dst1, s->frame->linesize[1], ref1->frame->data[1], ref1->frame->linesize[1],
1835                           1, x0_c, y0_c, nPbW_c, nPbH_c, &current_mv,
1836                           s->sh.chroma_weight_l1[current_mv.ref_idx[1]][0], s->sh.chroma_offset_l1[current_mv.ref_idx[1]][0]);
1837
1838             chroma_mc_uni(s, dst2, s->frame->linesize[2], ref1->frame->data[2], ref1->frame->linesize[2],
1839                           1, x0_c, y0_c, nPbW_c, nPbH_c, &current_mv,
1840                           s->sh.chroma_weight_l1[current_mv.ref_idx[1]][1], s->sh.chroma_offset_l1[current_mv.ref_idx[1]][1]);
1841         }
1842     } else if (current_mv.pred_flag == PF_BI) {
1843         int x0_c = x0 >> s->ps.sps->hshift[1];
1844         int y0_c = y0 >> s->ps.sps->vshift[1];
1845         int nPbW_c = nPbW >> s->ps.sps->hshift[1];
1846         int nPbH_c = nPbH >> s->ps.sps->vshift[1];
1847
1848         luma_mc_bi(s, dst0, s->frame->linesize[0], ref0->frame,
1849                    &current_mv.mv[0], x0, y0, nPbW, nPbH,
1850                    ref1->frame, &current_mv.mv[1], &current_mv);
1851
1852         if (s->ps.sps->chroma_format_idc) {
1853             chroma_mc_bi(s, dst1, s->frame->linesize[1], ref0->frame, ref1->frame,
1854                          x0_c, y0_c, nPbW_c, nPbH_c, &current_mv, 0);
1855
1856             chroma_mc_bi(s, dst2, s->frame->linesize[2], ref0->frame, ref1->frame,
1857                          x0_c, y0_c, nPbW_c, nPbH_c, &current_mv, 1);
1858         }
1859     }
1860 }
1861
1862 /**
1863  * 8.4.1
1864  */
1865 static int luma_intra_pred_mode(HEVCContext *s, int x0, int y0, int pu_size,
1866                                 int prev_intra_luma_pred_flag)
1867 {
1868     HEVCLocalContext *lc = s->HEVClc;
1869     int x_pu             = x0 >> s->ps.sps->log2_min_pu_size;
1870     int y_pu             = y0 >> s->ps.sps->log2_min_pu_size;
1871     int min_pu_width     = s->ps.sps->min_pu_width;
1872     int size_in_pus      = pu_size >> s->ps.sps->log2_min_pu_size;
1873     int x0b              = av_mod_uintp2(x0, s->ps.sps->log2_ctb_size);
1874     int y0b              = av_mod_uintp2(y0, s->ps.sps->log2_ctb_size);
1875
1876     int cand_up   = (lc->ctb_up_flag || y0b) ?
1877                     s->tab_ipm[(y_pu - 1) * min_pu_width + x_pu] : INTRA_DC;
1878     int cand_left = (lc->ctb_left_flag || x0b) ?
1879                     s->tab_ipm[y_pu * min_pu_width + x_pu - 1]   : INTRA_DC;
1880
1881     int y_ctb = (y0 >> (s->ps.sps->log2_ctb_size)) << (s->ps.sps->log2_ctb_size);
1882
1883     MvField *tab_mvf = s->ref->tab_mvf;
1884     int intra_pred_mode;
1885     int candidate[3];
1886     int i, j;
1887
1888     // intra_pred_mode prediction does not cross vertical CTB boundaries
1889     if ((y0 - 1) < y_ctb)
1890         cand_up = INTRA_DC;
1891
1892     if (cand_left == cand_up) {
1893         if (cand_left < 2) {
1894             candidate[0] = INTRA_PLANAR;
1895             candidate[1] = INTRA_DC;
1896             candidate[2] = INTRA_ANGULAR_26;
1897         } else {
1898             candidate[0] = cand_left;
1899             candidate[1] = 2 + ((cand_left - 2 - 1 + 32) & 31);
1900             candidate[2] = 2 + ((cand_left - 2 + 1) & 31);
1901         }
1902     } else {
1903         candidate[0] = cand_left;
1904         candidate[1] = cand_up;
1905         if (candidate[0] != INTRA_PLANAR && candidate[1] != INTRA_PLANAR) {
1906             candidate[2] = INTRA_PLANAR;
1907         } else if (candidate[0] != INTRA_DC && candidate[1] != INTRA_DC) {
1908             candidate[2] = INTRA_DC;
1909         } else {
1910             candidate[2] = INTRA_ANGULAR_26;
1911         }
1912     }
1913
1914     if (prev_intra_luma_pred_flag) {
1915         intra_pred_mode = candidate[lc->pu.mpm_idx];
1916     } else {
1917         if (candidate[0] > candidate[1])
1918             FFSWAP(uint8_t, candidate[0], candidate[1]);
1919         if (candidate[0] > candidate[2])
1920             FFSWAP(uint8_t, candidate[0], candidate[2]);
1921         if (candidate[1] > candidate[2])
1922             FFSWAP(uint8_t, candidate[1], candidate[2]);
1923
1924         intra_pred_mode = lc->pu.rem_intra_luma_pred_mode;
1925         for (i = 0; i < 3; i++)
1926             if (intra_pred_mode >= candidate[i])
1927                 intra_pred_mode++;
1928     }
1929
1930     /* write the intra prediction units into the mv array */
1931     if (!size_in_pus)
1932         size_in_pus = 1;
1933     for (i = 0; i < size_in_pus; i++) {
1934         memset(&s->tab_ipm[(y_pu + i) * min_pu_width + x_pu],
1935                intra_pred_mode, size_in_pus);
1936
1937         for (j = 0; j < size_in_pus; j++) {
1938             tab_mvf[(y_pu + j) * min_pu_width + x_pu + i].pred_flag = PF_INTRA;
1939         }
1940     }
1941
1942     return intra_pred_mode;
1943 }
1944
1945 static av_always_inline void set_ct_depth(HEVCContext *s, int x0, int y0,
1946                                           int log2_cb_size, int ct_depth)
1947 {
1948     int length = (1 << log2_cb_size) >> s->ps.sps->log2_min_cb_size;
1949     int x_cb   = x0 >> s->ps.sps->log2_min_cb_size;
1950     int y_cb   = y0 >> s->ps.sps->log2_min_cb_size;
1951     int y;
1952
1953     for (y = 0; y < length; y++)
1954         memset(&s->tab_ct_depth[(y_cb + y) * s->ps.sps->min_cb_width + x_cb],
1955                ct_depth, length);
1956 }
1957
1958 static const uint8_t tab_mode_idx[] = {
1959      0,  1,  2,  2,  2,  2,  3,  5,  7,  8, 10, 12, 13, 15, 17, 18, 19, 20,
1960     21, 22, 23, 23, 24, 24, 25, 25, 26, 27, 27, 28, 28, 29, 29, 30, 31};
1961
1962 static void intra_prediction_unit(HEVCContext *s, int x0, int y0,
1963                                   int log2_cb_size)
1964 {
1965     HEVCLocalContext *lc = s->HEVClc;
1966     static const uint8_t intra_chroma_table[4] = { 0, 26, 10, 1 };
1967     uint8_t prev_intra_luma_pred_flag[4];
1968     int split   = lc->cu.part_mode == PART_NxN;
1969     int pb_size = (1 << log2_cb_size) >> split;
1970     int side    = split + 1;
1971     int chroma_mode;
1972     int i, j;
1973
1974     for (i = 0; i < side; i++)
1975         for (j = 0; j < side; j++)
1976             prev_intra_luma_pred_flag[2 * i + j] = ff_hevc_prev_intra_luma_pred_flag_decode(s);
1977
1978     for (i = 0; i < side; i++) {
1979         for (j = 0; j < side; j++) {
1980             if (prev_intra_luma_pred_flag[2 * i + j])
1981                 lc->pu.mpm_idx = ff_hevc_mpm_idx_decode(s);
1982             else
1983                 lc->pu.rem_intra_luma_pred_mode = ff_hevc_rem_intra_luma_pred_mode_decode(s);
1984
1985             lc->pu.intra_pred_mode[2 * i + j] =
1986                 luma_intra_pred_mode(s, x0 + pb_size * j, y0 + pb_size * i, pb_size,
1987                                      prev_intra_luma_pred_flag[2 * i + j]);
1988         }
1989     }
1990
1991     if (s->ps.sps->chroma_format_idc == 3) {
1992         for (i = 0; i < side; i++) {
1993             for (j = 0; j < side; j++) {
1994                 lc->pu.chroma_mode_c[2 * i + j] = chroma_mode = ff_hevc_intra_chroma_pred_mode_decode(s);
1995                 if (chroma_mode != 4) {
1996                     if (lc->pu.intra_pred_mode[2 * i + j] == intra_chroma_table[chroma_mode])
1997                         lc->pu.intra_pred_mode_c[2 * i + j] = 34;
1998                     else
1999                         lc->pu.intra_pred_mode_c[2 * i + j] = intra_chroma_table[chroma_mode];
2000                 } else {
2001                     lc->pu.intra_pred_mode_c[2 * i + j] = lc->pu.intra_pred_mode[2 * i + j];
2002                 }
2003             }
2004         }
2005     } else if (s->ps.sps->chroma_format_idc == 2) {
2006         int mode_idx;
2007         lc->pu.chroma_mode_c[0] = chroma_mode = ff_hevc_intra_chroma_pred_mode_decode(s);
2008         if (chroma_mode != 4) {
2009             if (lc->pu.intra_pred_mode[0] == intra_chroma_table[chroma_mode])
2010                 mode_idx = 34;
2011             else
2012                 mode_idx = intra_chroma_table[chroma_mode];
2013         } else {
2014             mode_idx = lc->pu.intra_pred_mode[0];
2015         }
2016         lc->pu.intra_pred_mode_c[0] = tab_mode_idx[mode_idx];
2017     } else if (s->ps.sps->chroma_format_idc != 0) {
2018         chroma_mode = ff_hevc_intra_chroma_pred_mode_decode(s);
2019         if (chroma_mode != 4) {
2020             if (lc->pu.intra_pred_mode[0] == intra_chroma_table[chroma_mode])
2021                 lc->pu.intra_pred_mode_c[0] = 34;
2022             else
2023                 lc->pu.intra_pred_mode_c[0] = intra_chroma_table[chroma_mode];
2024         } else {
2025             lc->pu.intra_pred_mode_c[0] = lc->pu.intra_pred_mode[0];
2026         }
2027     }
2028 }
2029
2030 static void intra_prediction_unit_default_value(HEVCContext *s,
2031                                                 int x0, int y0,
2032                                                 int log2_cb_size)
2033 {
2034     HEVCLocalContext *lc = s->HEVClc;
2035     int pb_size          = 1 << log2_cb_size;
2036     int size_in_pus      = pb_size >> s->ps.sps->log2_min_pu_size;
2037     int min_pu_width     = s->ps.sps->min_pu_width;
2038     MvField *tab_mvf     = s->ref->tab_mvf;
2039     int x_pu             = x0 >> s->ps.sps->log2_min_pu_size;
2040     int y_pu             = y0 >> s->ps.sps->log2_min_pu_size;
2041     int j, k;
2042
2043     if (size_in_pus == 0)
2044         size_in_pus = 1;
2045     for (j = 0; j < size_in_pus; j++)
2046         memset(&s->tab_ipm[(y_pu + j) * min_pu_width + x_pu], INTRA_DC, size_in_pus);
2047     if (lc->cu.pred_mode == MODE_INTRA)
2048         for (j = 0; j < size_in_pus; j++)
2049             for (k = 0; k < size_in_pus; k++)
2050                 tab_mvf[(y_pu + j) * min_pu_width + x_pu + k].pred_flag = PF_INTRA;
2051 }
2052
2053 static int hls_coding_unit(HEVCContext *s, int x0, int y0, int log2_cb_size)
2054 {
2055     int cb_size          = 1 << log2_cb_size;
2056     HEVCLocalContext *lc = s->HEVClc;
2057     int log2_min_cb_size = s->ps.sps->log2_min_cb_size;
2058     int length           = cb_size >> log2_min_cb_size;
2059     int min_cb_width     = s->ps.sps->min_cb_width;
2060     int x_cb             = x0 >> log2_min_cb_size;
2061     int y_cb             = y0 >> log2_min_cb_size;
2062     int idx              = log2_cb_size - 2;
2063     int qp_block_mask    = (1<<(s->ps.sps->log2_ctb_size - s->ps.pps->diff_cu_qp_delta_depth)) - 1;
2064     int x, y, ret;
2065
2066     lc->cu.x                = x0;
2067     lc->cu.y                = y0;
2068     lc->cu.pred_mode        = MODE_INTRA;
2069     lc->cu.part_mode        = PART_2Nx2N;
2070     lc->cu.intra_split_flag = 0;
2071
2072     SAMPLE_CTB(s->skip_flag, x_cb, y_cb) = 0;
2073     for (x = 0; x < 4; x++)
2074         lc->pu.intra_pred_mode[x] = 1;
2075     if (s->ps.pps->transquant_bypass_enable_flag) {
2076         lc->cu.cu_transquant_bypass_flag = ff_hevc_cu_transquant_bypass_flag_decode(s);
2077         if (lc->cu.cu_transquant_bypass_flag)
2078             set_deblocking_bypass(s, x0, y0, log2_cb_size);
2079     } else
2080         lc->cu.cu_transquant_bypass_flag = 0;
2081
2082     if (s->sh.slice_type != HEVC_SLICE_I) {
2083         uint8_t skip_flag = ff_hevc_skip_flag_decode(s, x0, y0, x_cb, y_cb);
2084
2085         x = y_cb * min_cb_width + x_cb;
2086         for (y = 0; y < length; y++) {
2087             memset(&s->skip_flag[x], skip_flag, length);
2088             x += min_cb_width;
2089         }
2090         lc->cu.pred_mode = skip_flag ? MODE_SKIP : MODE_INTER;
2091     } else {
2092         x = y_cb * min_cb_width + x_cb;
2093         for (y = 0; y < length; y++) {
2094             memset(&s->skip_flag[x], 0, length);
2095             x += min_cb_width;
2096         }
2097     }
2098
2099     if (SAMPLE_CTB(s->skip_flag, x_cb, y_cb)) {
2100         hls_prediction_unit(s, x0, y0, cb_size, cb_size, log2_cb_size, 0, idx);
2101         intra_prediction_unit_default_value(s, x0, y0, log2_cb_size);
2102
2103         if (!s->sh.disable_deblocking_filter_flag)
2104             ff_hevc_deblocking_boundary_strengths(s, x0, y0, log2_cb_size);
2105     } else {
2106         int pcm_flag = 0;
2107
2108         if (s->sh.slice_type != HEVC_SLICE_I)
2109             lc->cu.pred_mode = ff_hevc_pred_mode_decode(s);
2110         if (lc->cu.pred_mode != MODE_INTRA ||
2111             log2_cb_size == s->ps.sps->log2_min_cb_size) {
2112             lc->cu.part_mode        = ff_hevc_part_mode_decode(s, log2_cb_size);
2113             lc->cu.intra_split_flag = lc->cu.part_mode == PART_NxN &&
2114                                       lc->cu.pred_mode == MODE_INTRA;
2115         }
2116
2117         if (lc->cu.pred_mode == MODE_INTRA) {
2118             if (lc->cu.part_mode == PART_2Nx2N && s->ps.sps->pcm_enabled_flag &&
2119                 log2_cb_size >= s->ps.sps->pcm.log2_min_pcm_cb_size &&
2120                 log2_cb_size <= s->ps.sps->pcm.log2_max_pcm_cb_size) {
2121                 pcm_flag = ff_hevc_pcm_flag_decode(s);
2122             }
2123             if (pcm_flag) {
2124                 intra_prediction_unit_default_value(s, x0, y0, log2_cb_size);
2125                 ret = hls_pcm_sample(s, x0, y0, log2_cb_size);
2126                 if (s->ps.sps->pcm.loop_filter_disable_flag)
2127                     set_deblocking_bypass(s, x0, y0, log2_cb_size);
2128
2129                 if (ret < 0)
2130                     return ret;
2131             } else {
2132                 intra_prediction_unit(s, x0, y0, log2_cb_size);
2133             }
2134         } else {
2135             intra_prediction_unit_default_value(s, x0, y0, log2_cb_size);
2136             switch (lc->cu.part_mode) {
2137             case PART_2Nx2N:
2138                 hls_prediction_unit(s, x0, y0, cb_size, cb_size, log2_cb_size, 0, idx);
2139                 break;
2140             case PART_2NxN:
2141                 hls_prediction_unit(s, x0, y0,               cb_size, cb_size / 2, log2_cb_size, 0, idx);
2142                 hls_prediction_unit(s, x0, y0 + cb_size / 2, cb_size, cb_size / 2, log2_cb_size, 1, idx);
2143                 break;
2144             case PART_Nx2N:
2145                 hls_prediction_unit(s, x0,               y0, cb_size / 2, cb_size, log2_cb_size, 0, idx - 1);
2146                 hls_prediction_unit(s, x0 + cb_size / 2, y0, cb_size / 2, cb_size, log2_cb_size, 1, idx - 1);
2147                 break;
2148             case PART_2NxnU:
2149                 hls_prediction_unit(s, x0, y0,               cb_size, cb_size     / 4, log2_cb_size, 0, idx);
2150                 hls_prediction_unit(s, x0, y0 + cb_size / 4, cb_size, cb_size * 3 / 4, log2_cb_size, 1, idx);
2151                 break;
2152             case PART_2NxnD:
2153                 hls_prediction_unit(s, x0, y0,                   cb_size, cb_size * 3 / 4, log2_cb_size, 0, idx);
2154                 hls_prediction_unit(s, x0, y0 + cb_size * 3 / 4, cb_size, cb_size     / 4, log2_cb_size, 1, idx);
2155                 break;
2156             case PART_nLx2N:
2157                 hls_prediction_unit(s, x0,               y0, cb_size     / 4, cb_size, log2_cb_size, 0, idx - 2);
2158                 hls_prediction_unit(s, x0 + cb_size / 4, y0, cb_size * 3 / 4, cb_size, log2_cb_size, 1, idx - 2);
2159                 break;
2160             case PART_nRx2N:
2161                 hls_prediction_unit(s, x0,                   y0, cb_size * 3 / 4, cb_size, log2_cb_size, 0, idx - 2);
2162                 hls_prediction_unit(s, x0 + cb_size * 3 / 4, y0, cb_size     / 4, cb_size, log2_cb_size, 1, idx - 2);
2163                 break;
2164             case PART_NxN:
2165                 hls_prediction_unit(s, x0,               y0,               cb_size / 2, cb_size / 2, log2_cb_size, 0, idx - 1);
2166                 hls_prediction_unit(s, x0 + cb_size / 2, y0,               cb_size / 2, cb_size / 2, log2_cb_size, 1, idx - 1);
2167                 hls_prediction_unit(s, x0,               y0 + cb_size / 2, cb_size / 2, cb_size / 2, log2_cb_size, 2, idx - 1);
2168                 hls_prediction_unit(s, x0 + cb_size / 2, y0 + cb_size / 2, cb_size / 2, cb_size / 2, log2_cb_size, 3, idx - 1);
2169                 break;
2170             }
2171         }
2172
2173         if (!pcm_flag) {
2174             int rqt_root_cbf = 1;
2175
2176             if (lc->cu.pred_mode != MODE_INTRA &&
2177                 !(lc->cu.part_mode == PART_2Nx2N && lc->pu.merge_flag)) {
2178                 rqt_root_cbf = ff_hevc_no_residual_syntax_flag_decode(s);
2179             }
2180             if (rqt_root_cbf) {
2181                 const static int cbf[2] = { 0 };
2182                 lc->cu.max_trafo_depth = lc->cu.pred_mode == MODE_INTRA ?
2183                                          s->ps.sps->max_transform_hierarchy_depth_intra + lc->cu.intra_split_flag :
2184                                          s->ps.sps->max_transform_hierarchy_depth_inter;
2185                 ret = hls_transform_tree(s, x0, y0, x0, y0, x0, y0,
2186                                          log2_cb_size,
2187                                          log2_cb_size, 0, 0, cbf, cbf);
2188                 if (ret < 0)
2189                     return ret;
2190             } else {
2191                 if (!s->sh.disable_deblocking_filter_flag)
2192                     ff_hevc_deblocking_boundary_strengths(s, x0, y0, log2_cb_size);
2193             }
2194         }
2195     }
2196
2197     if (s->ps.pps->cu_qp_delta_enabled_flag && lc->tu.is_cu_qp_delta_coded == 0)
2198         ff_hevc_set_qPy(s, x0, y0, log2_cb_size);
2199
2200     x = y_cb * min_cb_width + x_cb;
2201     for (y = 0; y < length; y++) {
2202         memset(&s->qp_y_tab[x], lc->qp_y, length);
2203         x += min_cb_width;
2204     }
2205
2206     if(((x0 + (1<<log2_cb_size)) & qp_block_mask) == 0 &&
2207        ((y0 + (1<<log2_cb_size)) & qp_block_mask) == 0) {
2208         lc->qPy_pred = lc->qp_y;
2209     }
2210
2211     set_ct_depth(s, x0, y0, log2_cb_size, lc->ct_depth);
2212
2213     return 0;
2214 }
2215
2216 static int hls_coding_quadtree(HEVCContext *s, int x0, int y0,
2217                                int log2_cb_size, int cb_depth)
2218 {
2219     HEVCLocalContext *lc = s->HEVClc;
2220     const int cb_size    = 1 << log2_cb_size;
2221     int ret;
2222     int split_cu;
2223
2224     lc->ct_depth = cb_depth;
2225     if (x0 + cb_size <= s->ps.sps->width  &&
2226         y0 + cb_size <= s->ps.sps->height &&
2227         log2_cb_size > s->ps.sps->log2_min_cb_size) {
2228         split_cu = ff_hevc_split_coding_unit_flag_decode(s, cb_depth, x0, y0);
2229     } else {
2230         split_cu = (log2_cb_size > s->ps.sps->log2_min_cb_size);
2231     }
2232     if (s->ps.pps->cu_qp_delta_enabled_flag &&
2233         log2_cb_size >= s->ps.sps->log2_ctb_size - s->ps.pps->diff_cu_qp_delta_depth) {
2234         lc->tu.is_cu_qp_delta_coded = 0;
2235         lc->tu.cu_qp_delta          = 0;
2236     }
2237
2238     if (s->sh.cu_chroma_qp_offset_enabled_flag &&
2239         log2_cb_size >= s->ps.sps->log2_ctb_size - s->ps.pps->diff_cu_chroma_qp_offset_depth) {
2240         lc->tu.is_cu_chroma_qp_offset_coded = 0;
2241     }
2242
2243     if (split_cu) {
2244         int qp_block_mask = (1<<(s->ps.sps->log2_ctb_size - s->ps.pps->diff_cu_qp_delta_depth)) - 1;
2245         const int cb_size_split = cb_size >> 1;
2246         const int x1 = x0 + cb_size_split;
2247         const int y1 = y0 + cb_size_split;
2248
2249         int more_data = 0;
2250
2251         more_data = hls_coding_quadtree(s, x0, y0, log2_cb_size - 1, cb_depth + 1);
2252         if (more_data < 0)
2253             return more_data;
2254
2255         if (more_data && x1 < s->ps.sps->width) {
2256             more_data = hls_coding_quadtree(s, x1, y0, log2_cb_size - 1, cb_depth + 1);
2257             if (more_data < 0)
2258                 return more_data;
2259         }
2260         if (more_data && y1 < s->ps.sps->height) {
2261             more_data = hls_coding_quadtree(s, x0, y1, log2_cb_size - 1, cb_depth + 1);
2262             if (more_data < 0)
2263                 return more_data;
2264         }
2265         if (more_data && x1 < s->ps.sps->width &&
2266             y1 < s->ps.sps->height) {
2267             more_data = hls_coding_quadtree(s, x1, y1, log2_cb_size - 1, cb_depth + 1);
2268             if (more_data < 0)
2269                 return more_data;
2270         }
2271
2272         if(((x0 + (1<<log2_cb_size)) & qp_block_mask) == 0 &&
2273             ((y0 + (1<<log2_cb_size)) & qp_block_mask) == 0)
2274             lc->qPy_pred = lc->qp_y;
2275
2276         if (more_data)
2277             return ((x1 + cb_size_split) < s->ps.sps->width ||
2278                     (y1 + cb_size_split) < s->ps.sps->height);
2279         else
2280             return 0;
2281     } else {
2282         ret = hls_coding_unit(s, x0, y0, log2_cb_size);
2283         if (ret < 0)
2284             return ret;
2285         if ((!((x0 + cb_size) %
2286                (1 << (s->ps.sps->log2_ctb_size))) ||
2287              (x0 + cb_size >= s->ps.sps->width)) &&
2288             (!((y0 + cb_size) %
2289                (1 << (s->ps.sps->log2_ctb_size))) ||
2290              (y0 + cb_size >= s->ps.sps->height))) {
2291             int end_of_slice_flag = ff_hevc_end_of_slice_flag_decode(s);
2292             return !end_of_slice_flag;
2293         } else {
2294             return 1;
2295         }
2296     }
2297
2298     return 0;
2299 }
2300
2301 static void hls_decode_neighbour(HEVCContext *s, int x_ctb, int y_ctb,
2302                                  int ctb_addr_ts)
2303 {
2304     HEVCLocalContext *lc  = s->HEVClc;
2305     int ctb_size          = 1 << s->ps.sps->log2_ctb_size;
2306     int ctb_addr_rs       = s->ps.pps->ctb_addr_ts_to_rs[ctb_addr_ts];
2307     int ctb_addr_in_slice = ctb_addr_rs - s->sh.slice_addr;
2308
2309     s->tab_slice_address[ctb_addr_rs] = s->sh.slice_addr;
2310
2311     if (s->ps.pps->entropy_coding_sync_enabled_flag) {
2312         if (x_ctb == 0 && (y_ctb & (ctb_size - 1)) == 0)
2313             lc->first_qp_group = 1;
2314         lc->end_of_tiles_x = s->ps.sps->width;
2315     } else if (s->ps.pps->tiles_enabled_flag) {
2316         if (ctb_addr_ts && s->ps.pps->tile_id[ctb_addr_ts] != s->ps.pps->tile_id[ctb_addr_ts - 1]) {
2317             int idxX = s->ps.pps->col_idxX[x_ctb >> s->ps.sps->log2_ctb_size];
2318             lc->end_of_tiles_x   = x_ctb + (s->ps.pps->column_width[idxX] << s->ps.sps->log2_ctb_size);
2319             lc->first_qp_group   = 1;
2320         }
2321     } else {
2322         lc->end_of_tiles_x = s->ps.sps->width;
2323     }
2324
2325     lc->end_of_tiles_y = FFMIN(y_ctb + ctb_size, s->ps.sps->height);
2326
2327     lc->boundary_flags = 0;
2328     if (s->ps.pps->tiles_enabled_flag) {
2329         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]])
2330             lc->boundary_flags |= BOUNDARY_LEFT_TILE;
2331         if (x_ctb > 0 && s->tab_slice_address[ctb_addr_rs] != s->tab_slice_address[ctb_addr_rs - 1])
2332             lc->boundary_flags |= BOUNDARY_LEFT_SLICE;
2333         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]])
2334             lc->boundary_flags |= BOUNDARY_UPPER_TILE;
2335         if (y_ctb > 0 && s->tab_slice_address[ctb_addr_rs] != s->tab_slice_address[ctb_addr_rs - s->ps.sps->ctb_width])
2336             lc->boundary_flags |= BOUNDARY_UPPER_SLICE;
2337     } else {
2338         if (ctb_addr_in_slice <= 0)
2339             lc->boundary_flags |= BOUNDARY_LEFT_SLICE;
2340         if (ctb_addr_in_slice < s->ps.sps->ctb_width)
2341             lc->boundary_flags |= BOUNDARY_UPPER_SLICE;
2342     }
2343
2344     lc->ctb_left_flag = ((x_ctb > 0) && (ctb_addr_in_slice > 0) && !(lc->boundary_flags & BOUNDARY_LEFT_TILE));
2345     lc->ctb_up_flag   = ((y_ctb > 0) && (ctb_addr_in_slice >= s->ps.sps->ctb_width) && !(lc->boundary_flags & BOUNDARY_UPPER_TILE));
2346     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]]));
2347     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]]));
2348 }
2349
2350 static int hls_decode_entry(AVCodecContext *avctxt, void *isFilterThread)
2351 {
2352     HEVCContext *s  = avctxt->priv_data;
2353     int ctb_size    = 1 << s->ps.sps->log2_ctb_size;
2354     int more_data   = 1;
2355     int x_ctb       = 0;
2356     int y_ctb       = 0;
2357     int ctb_addr_ts = s->ps.pps->ctb_addr_rs_to_ts[s->sh.slice_ctb_addr_rs];
2358     int ret;
2359
2360     if (!ctb_addr_ts && s->sh.dependent_slice_segment_flag) {
2361         av_log(s->avctx, AV_LOG_ERROR, "Impossible initial tile.\n");
2362         return AVERROR_INVALIDDATA;
2363     }
2364
2365     if (s->sh.dependent_slice_segment_flag) {
2366         int prev_rs = s->ps.pps->ctb_addr_ts_to_rs[ctb_addr_ts - 1];
2367         if (s->tab_slice_address[prev_rs] != s->sh.slice_addr) {
2368             av_log(s->avctx, AV_LOG_ERROR, "Previous slice segment missing\n");
2369             return AVERROR_INVALIDDATA;
2370         }
2371     }
2372
2373     while (more_data && ctb_addr_ts < s->ps.sps->ctb_size) {
2374         int ctb_addr_rs = s->ps.pps->ctb_addr_ts_to_rs[ctb_addr_ts];
2375
2376         x_ctb = (ctb_addr_rs % ((s->ps.sps->width + ctb_size - 1) >> s->ps.sps->log2_ctb_size)) << s->ps.sps->log2_ctb_size;
2377         y_ctb = (ctb_addr_rs / ((s->ps.sps->width + ctb_size - 1) >> s->ps.sps->log2_ctb_size)) << s->ps.sps->log2_ctb_size;
2378         hls_decode_neighbour(s, x_ctb, y_ctb, ctb_addr_ts);
2379
2380         ret = ff_hevc_cabac_init(s, ctb_addr_ts);
2381         if (ret < 0) {
2382             s->tab_slice_address[ctb_addr_rs] = -1;
2383             return ret;
2384         }
2385
2386         hls_sao_param(s, x_ctb >> s->ps.sps->log2_ctb_size, y_ctb >> s->ps.sps->log2_ctb_size);
2387
2388         s->deblock[ctb_addr_rs].beta_offset = s->sh.beta_offset;
2389         s->deblock[ctb_addr_rs].tc_offset   = s->sh.tc_offset;
2390         s->filter_slice_edges[ctb_addr_rs]  = s->sh.slice_loop_filter_across_slices_enabled_flag;
2391
2392         more_data = hls_coding_quadtree(s, x_ctb, y_ctb, s->ps.sps->log2_ctb_size, 0);
2393         if (more_data < 0) {
2394             s->tab_slice_address[ctb_addr_rs] = -1;
2395             return more_data;
2396         }
2397
2398
2399         ctb_addr_ts++;
2400         ff_hevc_save_states(s, ctb_addr_ts);
2401         ff_hevc_hls_filters(s, x_ctb, y_ctb, ctb_size);
2402     }
2403
2404     if (x_ctb + ctb_size >= s->ps.sps->width &&
2405         y_ctb + ctb_size >= s->ps.sps->height)
2406         ff_hevc_hls_filter(s, x_ctb, y_ctb, ctb_size);
2407
2408     return ctb_addr_ts;
2409 }
2410
2411 static int hls_slice_data(HEVCContext *s)
2412 {
2413     int arg[2];
2414     int ret[2];
2415
2416     arg[0] = 0;
2417     arg[1] = 1;
2418
2419     s->avctx->execute(s->avctx, hls_decode_entry, arg, ret , 1, sizeof(int));
2420     return ret[0];
2421 }
2422 static int hls_decode_entry_wpp(AVCodecContext *avctxt, void *input_ctb_row, int job, int self_id)
2423 {
2424     HEVCContext *s1  = avctxt->priv_data, *s;
2425     HEVCLocalContext *lc;
2426     int ctb_size    = 1<< s1->ps.sps->log2_ctb_size;
2427     int more_data   = 1;
2428     int *ctb_row_p    = input_ctb_row;
2429     int ctb_row = ctb_row_p[job];
2430     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);
2431     int ctb_addr_ts = s1->ps.pps->ctb_addr_rs_to_ts[ctb_addr_rs];
2432     int thread = ctb_row % s1->threads_number;
2433     int ret;
2434
2435     s = s1->sList[self_id];
2436     lc = s->HEVClc;
2437
2438     if(ctb_row) {
2439         ret = init_get_bits8(&lc->gb, s->data + s->sh.offset[ctb_row - 1], s->sh.size[ctb_row - 1]);
2440         if (ret < 0)
2441             goto error;
2442         ff_init_cabac_decoder(&lc->cc, s->data + s->sh.offset[(ctb_row)-1], s->sh.size[ctb_row - 1]);
2443     }
2444
2445     while(more_data && ctb_addr_ts < s->ps.sps->ctb_size) {
2446         int x_ctb = (ctb_addr_rs % s->ps.sps->ctb_width) << s->ps.sps->log2_ctb_size;
2447         int y_ctb = (ctb_addr_rs / s->ps.sps->ctb_width) << s->ps.sps->log2_ctb_size;
2448
2449         hls_decode_neighbour(s, x_ctb, y_ctb, ctb_addr_ts);
2450
2451         ff_thread_await_progress2(s->avctx, ctb_row, thread, SHIFT_CTB_WPP);
2452
2453         if (atomic_load(&s1->wpp_err)) {
2454             ff_thread_report_progress2(s->avctx, ctb_row , thread, SHIFT_CTB_WPP);
2455             return 0;
2456         }
2457
2458         ret = ff_hevc_cabac_init(s, ctb_addr_ts);
2459         if (ret < 0)
2460             goto error;
2461         hls_sao_param(s, x_ctb >> s->ps.sps->log2_ctb_size, y_ctb >> s->ps.sps->log2_ctb_size);
2462         more_data = hls_coding_quadtree(s, x_ctb, y_ctb, s->ps.sps->log2_ctb_size, 0);
2463
2464         if (more_data < 0) {
2465             ret = more_data;
2466             goto error;
2467         }
2468
2469         ctb_addr_ts++;
2470
2471         ff_hevc_save_states(s, ctb_addr_ts);
2472         ff_thread_report_progress2(s->avctx, ctb_row, thread, 1);
2473         ff_hevc_hls_filters(s, x_ctb, y_ctb, ctb_size);
2474
2475         if (!more_data && (x_ctb+ctb_size) < s->ps.sps->width && ctb_row != s->sh.num_entry_point_offsets) {
2476             atomic_store(&s1->wpp_err, 1);
2477             ff_thread_report_progress2(s->avctx, ctb_row ,thread, SHIFT_CTB_WPP);
2478             return 0;
2479         }
2480
2481         if ((x_ctb+ctb_size) >= s->ps.sps->width && (y_ctb+ctb_size) >= s->ps.sps->height ) {
2482             ff_hevc_hls_filter(s, x_ctb, y_ctb, ctb_size);
2483             ff_thread_report_progress2(s->avctx, ctb_row , thread, SHIFT_CTB_WPP);
2484             return ctb_addr_ts;
2485         }
2486         ctb_addr_rs       = s->ps.pps->ctb_addr_ts_to_rs[ctb_addr_ts];
2487         x_ctb+=ctb_size;
2488
2489         if(x_ctb >= s->ps.sps->width) {
2490             break;
2491         }
2492     }
2493     ff_thread_report_progress2(s->avctx, ctb_row ,thread, SHIFT_CTB_WPP);
2494
2495     return 0;
2496 error:
2497     s->tab_slice_address[ctb_addr_rs] = -1;
2498     atomic_store(&s1->wpp_err, 1);
2499     ff_thread_report_progress2(s->avctx, ctb_row ,thread, SHIFT_CTB_WPP);
2500     return ret;
2501 }
2502
2503 static int hls_slice_data_wpp(HEVCContext *s, const H2645NAL *nal)
2504 {
2505     const uint8_t *data = nal->data;
2506     int length          = nal->size;
2507     HEVCLocalContext *lc = s->HEVClc;
2508     int *ret = av_malloc_array(s->sh.num_entry_point_offsets + 1, sizeof(int));
2509     int *arg = av_malloc_array(s->sh.num_entry_point_offsets + 1, sizeof(int));
2510     int64_t offset;
2511     int64_t startheader, cmpt = 0;
2512     int i, j, res = 0;
2513
2514     if (!ret || !arg) {
2515         av_free(ret);
2516         av_free(arg);
2517         return AVERROR(ENOMEM);
2518     }
2519
2520     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) {
2521         av_log(s->avctx, AV_LOG_ERROR, "WPP ctb addresses are wrong (%d %d %d %d)\n",
2522             s->sh.slice_ctb_addr_rs, s->sh.num_entry_point_offsets,
2523             s->ps.sps->ctb_width, s->ps.sps->ctb_height
2524         );
2525         res = AVERROR_INVALIDDATA;
2526         goto error;
2527     }
2528
2529     ff_alloc_entries(s->avctx, s->sh.num_entry_point_offsets + 1);
2530
2531     if (!s->sList[1]) {
2532         for (i = 1; i < s->threads_number; i++) {
2533             s->sList[i] = av_malloc(sizeof(HEVCContext));
2534             memcpy(s->sList[i], s, sizeof(HEVCContext));
2535             s->HEVClcList[i] = av_mallocz(sizeof(HEVCLocalContext));
2536             s->sList[i]->HEVClc = s->HEVClcList[i];
2537         }
2538     }
2539
2540     offset = (lc->gb.index >> 3);
2541
2542     for (j = 0, cmpt = 0, startheader = offset + s->sh.entry_point_offset[0]; j < nal->skipped_bytes; j++) {
2543         if (nal->skipped_bytes_pos[j] >= offset && nal->skipped_bytes_pos[j] < startheader) {
2544             startheader--;
2545             cmpt++;
2546         }
2547     }
2548
2549     for (i = 1; i < s->sh.num_entry_point_offsets; i++) {
2550         offset += (s->sh.entry_point_offset[i - 1] - cmpt);
2551         for (j = 0, cmpt = 0, startheader = offset
2552              + s->sh.entry_point_offset[i]; j < nal->skipped_bytes; j++) {
2553             if (nal->skipped_bytes_pos[j] >= offset && nal->skipped_bytes_pos[j] < startheader) {
2554                 startheader--;
2555                 cmpt++;
2556             }
2557         }
2558         s->sh.size[i - 1] = s->sh.entry_point_offset[i] - cmpt;
2559         s->sh.offset[i - 1] = offset;
2560
2561     }
2562     if (s->sh.num_entry_point_offsets != 0) {
2563         offset += s->sh.entry_point_offset[s->sh.num_entry_point_offsets - 1] - cmpt;
2564         if (length < offset) {
2565             av_log(s->avctx, AV_LOG_ERROR, "entry_point_offset table is corrupted\n");
2566             res = AVERROR_INVALIDDATA;
2567             goto error;
2568         }
2569         s->sh.size[s->sh.num_entry_point_offsets - 1] = length - offset;
2570         s->sh.offset[s->sh.num_entry_point_offsets - 1] = offset;
2571
2572     }
2573     s->data = data;
2574
2575     for (i = 1; i < s->threads_number; i++) {
2576         s->sList[i]->HEVClc->first_qp_group = 1;
2577         s->sList[i]->HEVClc->qp_y = s->sList[0]->HEVClc->qp_y;
2578         memcpy(s->sList[i], s, sizeof(HEVCContext));
2579         s->sList[i]->HEVClc = s->HEVClcList[i];
2580     }
2581
2582     atomic_store(&s->wpp_err, 0);
2583     ff_reset_entries(s->avctx);
2584
2585     for (i = 0; i <= s->sh.num_entry_point_offsets; i++) {
2586         arg[i] = i;
2587         ret[i] = 0;
2588     }
2589
2590     if (s->ps.pps->entropy_coding_sync_enabled_flag)
2591         s->avctx->execute2(s->avctx, hls_decode_entry_wpp, arg, ret, s->sh.num_entry_point_offsets + 1);
2592
2593     for (i = 0; i <= s->sh.num_entry_point_offsets; i++)
2594         res += ret[i];
2595 error:
2596     av_free(ret);
2597     av_free(arg);
2598     return res;
2599 }
2600
2601 static int set_side_data(HEVCContext *s)
2602 {
2603     AVFrame *out = s->ref->frame;
2604
2605     if (s->sei.frame_packing.present &&
2606         s->sei.frame_packing.arrangement_type >= 3 &&
2607         s->sei.frame_packing.arrangement_type <= 5 &&
2608         s->sei.frame_packing.content_interpretation_type > 0 &&
2609         s->sei.frame_packing.content_interpretation_type < 3) {
2610         AVStereo3D *stereo = av_stereo3d_create_side_data(out);
2611         if (!stereo)
2612             return AVERROR(ENOMEM);
2613
2614         switch (s->sei.frame_packing.arrangement_type) {
2615         case 3:
2616             if (s->sei.frame_packing.quincunx_subsampling)
2617                 stereo->type = AV_STEREO3D_SIDEBYSIDE_QUINCUNX;
2618             else
2619                 stereo->type = AV_STEREO3D_SIDEBYSIDE;
2620             break;
2621         case 4:
2622             stereo->type = AV_STEREO3D_TOPBOTTOM;
2623             break;
2624         case 5:
2625             stereo->type = AV_STEREO3D_FRAMESEQUENCE;
2626             break;
2627         }
2628
2629         if (s->sei.frame_packing.content_interpretation_type == 2)
2630             stereo->flags = AV_STEREO3D_FLAG_INVERT;
2631     }
2632
2633     if (s->sei.display_orientation.present &&
2634         (s->sei.display_orientation.anticlockwise_rotation ||
2635          s->sei.display_orientation.hflip || s->sei.display_orientation.vflip)) {
2636         double angle = s->sei.display_orientation.anticlockwise_rotation * 360 / (double) (1 << 16);
2637         AVFrameSideData *rotation = av_frame_new_side_data(out,
2638                                                            AV_FRAME_DATA_DISPLAYMATRIX,
2639                                                            sizeof(int32_t) * 9);
2640         if (!rotation)
2641             return AVERROR(ENOMEM);
2642
2643         av_display_rotation_set((int32_t *)rotation->data, angle);
2644         av_display_matrix_flip((int32_t *)rotation->data,
2645                                s->sei.display_orientation.hflip,
2646                                s->sei.display_orientation.vflip);
2647     }
2648
2649     // Decrement the mastering display flag when IRAP frame has no_rasl_output_flag=1
2650     // so the side data persists for the entire coded video sequence.
2651     if (s->sei.mastering_display.present > 0 &&
2652         IS_IRAP(s) && s->no_rasl_output_flag) {
2653         s->sei.mastering_display.present--;
2654     }
2655     if (s->sei.mastering_display.present) {
2656         // HEVC uses a g,b,r ordering, which we convert to a more natural r,g,b
2657         const int mapping[3] = {2, 0, 1};
2658         const int chroma_den = 50000;
2659         const int luma_den = 10000;
2660         int i;
2661         AVMasteringDisplayMetadata *metadata =
2662             av_mastering_display_metadata_create_side_data(out);
2663         if (!metadata)
2664             return AVERROR(ENOMEM);
2665
2666         for (i = 0; i < 3; i++) {
2667             const int j = mapping[i];
2668             metadata->display_primaries[i][0].num = s->sei.mastering_display.display_primaries[j][0];
2669             metadata->display_primaries[i][0].den = chroma_den;
2670             metadata->display_primaries[i][1].num = s->sei.mastering_display.display_primaries[j][1];
2671             metadata->display_primaries[i][1].den = chroma_den;
2672         }
2673         metadata->white_point[0].num = s->sei.mastering_display.white_point[0];
2674         metadata->white_point[0].den = chroma_den;
2675         metadata->white_point[1].num = s->sei.mastering_display.white_point[1];
2676         metadata->white_point[1].den = chroma_den;
2677
2678         metadata->max_luminance.num = s->sei.mastering_display.max_luminance;
2679         metadata->max_luminance.den = luma_den;
2680         metadata->min_luminance.num = s->sei.mastering_display.min_luminance;
2681         metadata->min_luminance.den = luma_den;
2682         metadata->has_luminance = 1;
2683         metadata->has_primaries = 1;
2684
2685         av_log(s->avctx, AV_LOG_DEBUG, "Mastering Display Metadata:\n");
2686         av_log(s->avctx, AV_LOG_DEBUG,
2687                "r(%5.4f,%5.4f) g(%5.4f,%5.4f) b(%5.4f %5.4f) wp(%5.4f, %5.4f)\n",
2688                av_q2d(metadata->display_primaries[0][0]),
2689                av_q2d(metadata->display_primaries[0][1]),
2690                av_q2d(metadata->display_primaries[1][0]),
2691                av_q2d(metadata->display_primaries[1][1]),
2692                av_q2d(metadata->display_primaries[2][0]),
2693                av_q2d(metadata->display_primaries[2][1]),
2694                av_q2d(metadata->white_point[0]), av_q2d(metadata->white_point[1]));
2695         av_log(s->avctx, AV_LOG_DEBUG,
2696                "min_luminance=%f, max_luminance=%f\n",
2697                av_q2d(metadata->min_luminance), av_q2d(metadata->max_luminance));
2698     }
2699     // Decrement the mastering display flag when IRAP frame has no_rasl_output_flag=1
2700     // so the side data persists for the entire coded video sequence.
2701     if (s->sei.content_light.present > 0 &&
2702         IS_IRAP(s) && s->no_rasl_output_flag) {
2703         s->sei.content_light.present--;
2704     }
2705     if (s->sei.content_light.present) {
2706         AVContentLightMetadata *metadata =
2707             av_content_light_metadata_create_side_data(out);
2708         if (!metadata)
2709             return AVERROR(ENOMEM);
2710         metadata->MaxCLL  = s->sei.content_light.max_content_light_level;
2711         metadata->MaxFALL = s->sei.content_light.max_pic_average_light_level;
2712
2713         av_log(s->avctx, AV_LOG_DEBUG, "Content Light Level Metadata:\n");
2714         av_log(s->avctx, AV_LOG_DEBUG, "MaxCLL=%d, MaxFALL=%d\n",
2715                metadata->MaxCLL, metadata->MaxFALL);
2716     }
2717
2718     if (s->sei.a53_caption.a53_caption) {
2719         AVFrameSideData* sd = av_frame_new_side_data(out,
2720                                                      AV_FRAME_DATA_A53_CC,
2721                                                      s->sei.a53_caption.a53_caption_size);
2722         if (sd)
2723             memcpy(sd->data, s->sei.a53_caption.a53_caption, s->sei.a53_caption.a53_caption_size);
2724         av_freep(&s->sei.a53_caption.a53_caption);
2725         s->sei.a53_caption.a53_caption_size = 0;
2726         s->avctx->properties |= FF_CODEC_PROPERTY_CLOSED_CAPTIONS;
2727     }
2728
2729     if (s->sei.alternative_transfer.present &&
2730         av_color_transfer_name(s->sei.alternative_transfer.preferred_transfer_characteristics) &&
2731         s->sei.alternative_transfer.preferred_transfer_characteristics != AVCOL_TRC_UNSPECIFIED) {
2732         s->avctx->color_trc = out->color_trc = s->sei.alternative_transfer.preferred_transfer_characteristics;
2733     }
2734
2735     return 0;
2736 }
2737
2738 static int hevc_frame_start(HEVCContext *s)
2739 {
2740     HEVCLocalContext *lc = s->HEVClc;
2741     int pic_size_in_ctb  = ((s->ps.sps->width  >> s->ps.sps->log2_min_cb_size) + 1) *
2742                            ((s->ps.sps->height >> s->ps.sps->log2_min_cb_size) + 1);
2743     int ret;
2744
2745     memset(s->horizontal_bs, 0, s->bs_width * s->bs_height);
2746     memset(s->vertical_bs,   0, s->bs_width * s->bs_height);
2747     memset(s->cbf_luma,      0, s->ps.sps->min_tb_width * s->ps.sps->min_tb_height);
2748     memset(s->is_pcm,        0, (s->ps.sps->min_pu_width + 1) * (s->ps.sps->min_pu_height + 1));
2749     memset(s->tab_slice_address, -1, pic_size_in_ctb * sizeof(*s->tab_slice_address));
2750
2751     s->is_decoded        = 0;
2752     s->first_nal_type    = s->nal_unit_type;
2753
2754     s->no_rasl_output_flag = IS_IDR(s) || IS_BLA(s) || (s->nal_unit_type == HEVC_NAL_CRA_NUT && s->last_eos);
2755
2756     if (s->ps.pps->tiles_enabled_flag)
2757         lc->end_of_tiles_x = s->ps.pps->column_width[0] << s->ps.sps->log2_ctb_size;
2758
2759     ret = ff_hevc_set_new_ref(s, &s->frame, s->poc);
2760     if (ret < 0)
2761         goto fail;
2762
2763     ret = ff_hevc_frame_rps(s);
2764     if (ret < 0) {
2765         av_log(s->avctx, AV_LOG_ERROR, "Error constructing the frame RPS.\n");
2766         goto fail;
2767     }
2768
2769     s->ref->frame->key_frame = IS_IRAP(s);
2770
2771     ret = set_side_data(s);
2772     if (ret < 0)
2773         goto fail;
2774
2775     s->frame->pict_type = 3 - s->sh.slice_type;
2776
2777     if (!IS_IRAP(s))
2778         ff_hevc_bump_frame(s);
2779
2780     av_frame_unref(s->output_frame);
2781     ret = ff_hevc_output_frame(s, s->output_frame, 0);
2782     if (ret < 0)
2783         goto fail;
2784
2785     if (!s->avctx->hwaccel)
2786         ff_thread_finish_setup(s->avctx);
2787
2788     return 0;
2789
2790 fail:
2791     if (s->ref)
2792         ff_hevc_unref_frame(s, s->ref, ~0);
2793     s->ref = NULL;
2794     return ret;
2795 }
2796
2797 static int decode_nal_unit(HEVCContext *s, const H2645NAL *nal)
2798 {
2799     HEVCLocalContext *lc = s->HEVClc;
2800     GetBitContext *gb    = &lc->gb;
2801     int ctb_addr_ts, ret;
2802
2803     *gb              = nal->gb;
2804     s->nal_unit_type = nal->type;
2805     s->temporal_id   = nal->temporal_id;
2806
2807     switch (s->nal_unit_type) {
2808     case HEVC_NAL_VPS:
2809         ret = ff_hevc_decode_nal_vps(gb, s->avctx, &s->ps);
2810         if (ret < 0)
2811             goto fail;
2812         break;
2813     case HEVC_NAL_SPS:
2814         ret = ff_hevc_decode_nal_sps(gb, s->avctx, &s->ps,
2815                                      s->apply_defdispwin);
2816         if (ret < 0)
2817             goto fail;
2818         break;
2819     case HEVC_NAL_PPS:
2820         ret = ff_hevc_decode_nal_pps(gb, s->avctx, &s->ps);
2821         if (ret < 0)
2822             goto fail;
2823         break;
2824     case HEVC_NAL_SEI_PREFIX:
2825     case HEVC_NAL_SEI_SUFFIX:
2826         ret = ff_hevc_decode_nal_sei(gb, s->avctx, &s->sei, &s->ps, s->nal_unit_type);
2827         if (ret < 0)
2828             goto fail;
2829         break;
2830     case HEVC_NAL_TRAIL_R:
2831     case HEVC_NAL_TRAIL_N:
2832     case HEVC_NAL_TSA_N:
2833     case HEVC_NAL_TSA_R:
2834     case HEVC_NAL_STSA_N:
2835     case HEVC_NAL_STSA_R:
2836     case HEVC_NAL_BLA_W_LP:
2837     case HEVC_NAL_BLA_W_RADL:
2838     case HEVC_NAL_BLA_N_LP:
2839     case HEVC_NAL_IDR_W_RADL:
2840     case HEVC_NAL_IDR_N_LP:
2841     case HEVC_NAL_CRA_NUT:
2842     case HEVC_NAL_RADL_N:
2843     case HEVC_NAL_RADL_R:
2844     case HEVC_NAL_RASL_N:
2845     case HEVC_NAL_RASL_R:
2846         ret = hls_slice_header(s);
2847         if (ret < 0)
2848             return ret;
2849
2850         if (s->sh.first_slice_in_pic_flag) {
2851             if (s->max_ra == INT_MAX) {
2852                 if (s->nal_unit_type == HEVC_NAL_CRA_NUT || IS_BLA(s)) {
2853                     s->max_ra = s->poc;
2854                 } else {
2855                     if (IS_IDR(s))
2856                         s->max_ra = INT_MIN;
2857                 }
2858             }
2859
2860             if ((s->nal_unit_type == HEVC_NAL_RASL_R || s->nal_unit_type == HEVC_NAL_RASL_N) &&
2861                 s->poc <= s->max_ra) {
2862                 s->is_decoded = 0;
2863                 break;
2864             } else {
2865                 if (s->nal_unit_type == HEVC_NAL_RASL_R && s->poc > s->max_ra)
2866                     s->max_ra = INT_MIN;
2867             }
2868
2869             ret = hevc_frame_start(s);
2870             if (ret < 0)
2871                 return ret;
2872         } else if (!s->ref) {
2873             av_log(s->avctx, AV_LOG_ERROR, "First slice in a frame missing.\n");
2874             goto fail;
2875         }
2876
2877         if (s->nal_unit_type != s->first_nal_type) {
2878             av_log(s->avctx, AV_LOG_ERROR,
2879                    "Non-matching NAL types of the VCL NALUs: %d %d\n",
2880                    s->first_nal_type, s->nal_unit_type);
2881             return AVERROR_INVALIDDATA;
2882         }
2883
2884         if (!s->sh.dependent_slice_segment_flag &&
2885             s->sh.slice_type != HEVC_SLICE_I) {
2886             ret = ff_hevc_slice_rpl(s);
2887             if (ret < 0) {
2888                 av_log(s->avctx, AV_LOG_WARNING,
2889                        "Error constructing the reference lists for the current slice.\n");
2890                 goto fail;
2891             }
2892         }
2893
2894         if (s->sh.first_slice_in_pic_flag && s->avctx->hwaccel) {
2895             ret = s->avctx->hwaccel->start_frame(s->avctx, NULL, 0);
2896             if (ret < 0)
2897                 goto fail;
2898         }
2899
2900         if (s->avctx->hwaccel) {
2901             ret = s->avctx->hwaccel->decode_slice(s->avctx, nal->raw_data, nal->raw_size);
2902             if (ret < 0)
2903                 goto fail;
2904         } else {
2905             if (s->threads_number > 1 && s->sh.num_entry_point_offsets > 0)
2906                 ctb_addr_ts = hls_slice_data_wpp(s, nal);
2907             else
2908                 ctb_addr_ts = hls_slice_data(s);
2909             if (ctb_addr_ts >= (s->ps.sps->ctb_width * s->ps.sps->ctb_height)) {
2910                 s->is_decoded = 1;
2911             }
2912
2913             if (ctb_addr_ts < 0) {
2914                 ret = ctb_addr_ts;
2915                 goto fail;
2916             }
2917         }
2918         break;
2919     case HEVC_NAL_EOS_NUT:
2920     case HEVC_NAL_EOB_NUT:
2921         s->seq_decode = (s->seq_decode + 1) & 0xff;
2922         s->max_ra     = INT_MAX;
2923         break;
2924     case HEVC_NAL_AUD:
2925     case HEVC_NAL_FD_NUT:
2926         break;
2927     default:
2928         av_log(s->avctx, AV_LOG_INFO,
2929                "Skipping NAL unit %d\n", s->nal_unit_type);
2930     }
2931
2932     return 0;
2933 fail:
2934     if (s->avctx->err_recognition & AV_EF_EXPLODE)
2935         return ret;
2936     return 0;
2937 }
2938
2939 static int decode_nal_units(HEVCContext *s, const uint8_t *buf, int length)
2940 {
2941     int i, ret = 0;
2942     int eos_at_start = 1;
2943
2944     s->ref = NULL;
2945     s->last_eos = s->eos;
2946     s->eos = 0;
2947
2948     /* split the input packet into NAL units, so we know the upper bound on the
2949      * number of slices in the frame */
2950     ret = ff_h2645_packet_split(&s->pkt, buf, length, s->avctx, s->is_nalff,
2951                                 s->nal_length_size, s->avctx->codec_id, 1);
2952     if (ret < 0) {
2953         av_log(s->avctx, AV_LOG_ERROR,
2954                "Error splitting the input into NAL units.\n");
2955         return ret;
2956     }
2957
2958     for (i = 0; i < s->pkt.nb_nals; i++) {
2959         if (s->pkt.nals[i].type == HEVC_NAL_EOB_NUT ||
2960             s->pkt.nals[i].type == HEVC_NAL_EOS_NUT) {
2961             if (eos_at_start) {
2962                 s->last_eos = 1;
2963             } else {
2964                 s->eos = 1;
2965             }
2966         } else {
2967             eos_at_start = 0;
2968         }
2969     }
2970
2971     /* decode the NAL units */
2972     for (i = 0; i < s->pkt.nb_nals; i++) {
2973         ret = decode_nal_unit(s, &s->pkt.nals[i]);
2974         if (ret < 0) {
2975             av_log(s->avctx, AV_LOG_WARNING,
2976                    "Error parsing NAL unit #%d.\n", i);
2977             goto fail;
2978         }
2979     }
2980
2981 fail:
2982     if (s->ref && s->threads_type == FF_THREAD_FRAME)
2983         ff_thread_report_progress(&s->ref->tf, INT_MAX, 0);
2984
2985     return ret;
2986 }
2987
2988 static void print_md5(void *log_ctx, int level, uint8_t md5[16])
2989 {
2990     int i;
2991     for (i = 0; i < 16; i++)
2992         av_log(log_ctx, level, "%02"PRIx8, md5[i]);
2993 }
2994
2995 static int verify_md5(HEVCContext *s, AVFrame *frame)
2996 {
2997     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(frame->format);
2998     int pixel_shift;
2999     int i, j;
3000
3001     if (!desc)
3002         return AVERROR(EINVAL);
3003
3004     pixel_shift = desc->comp[0].depth > 8;
3005
3006     av_log(s->avctx, AV_LOG_DEBUG, "Verifying checksum for frame with POC %d: ",
3007            s->poc);
3008
3009     /* the checksums are LE, so we have to byteswap for >8bpp formats
3010      * on BE arches */
3011 #if HAVE_BIGENDIAN
3012     if (pixel_shift && !s->checksum_buf) {
3013         av_fast_malloc(&s->checksum_buf, &s->checksum_buf_size,
3014                        FFMAX3(frame->linesize[0], frame->linesize[1],
3015                               frame->linesize[2]));
3016         if (!s->checksum_buf)
3017             return AVERROR(ENOMEM);
3018     }
3019 #endif
3020
3021     for (i = 0; frame->data[i]; i++) {
3022         int width  = s->avctx->coded_width;
3023         int height = s->avctx->coded_height;
3024         int w = (i == 1 || i == 2) ? (width  >> desc->log2_chroma_w) : width;
3025         int h = (i == 1 || i == 2) ? (height >> desc->log2_chroma_h) : height;
3026         uint8_t md5[16];
3027
3028         av_md5_init(s->sei.picture_hash.md5_ctx);
3029         for (j = 0; j < h; j++) {
3030             const uint8_t *src = frame->data[i] + j * frame->linesize[i];
3031 #if HAVE_BIGENDIAN
3032             if (pixel_shift) {
3033                 s->bdsp.bswap16_buf((uint16_t *) s->checksum_buf,
3034                                     (const uint16_t *) src, w);
3035                 src = s->checksum_buf;
3036             }
3037 #endif
3038             av_md5_update(s->sei.picture_hash.md5_ctx, src, w << pixel_shift);
3039         }
3040         av_md5_final(s->sei.picture_hash.md5_ctx, md5);
3041
3042         if (!memcmp(md5, s->sei.picture_hash.md5[i], 16)) {
3043             av_log   (s->avctx, AV_LOG_DEBUG, "plane %d - correct ", i);
3044             print_md5(s->avctx, AV_LOG_DEBUG, md5);
3045             av_log   (s->avctx, AV_LOG_DEBUG, "; ");
3046         } else {
3047             av_log   (s->avctx, AV_LOG_ERROR, "mismatching checksum of plane %d - ", i);
3048             print_md5(s->avctx, AV_LOG_ERROR, md5);
3049             av_log   (s->avctx, AV_LOG_ERROR, " != ");
3050             print_md5(s->avctx, AV_LOG_ERROR, s->sei.picture_hash.md5[i]);
3051             av_log   (s->avctx, AV_LOG_ERROR, "\n");
3052             return AVERROR_INVALIDDATA;
3053         }
3054     }
3055
3056     av_log(s->avctx, AV_LOG_DEBUG, "\n");
3057
3058     return 0;
3059 }
3060
3061 static int hevc_decode_extradata(HEVCContext *s, uint8_t *buf, int length, int first)
3062 {
3063     int ret, i;
3064
3065     ret = ff_hevc_decode_extradata(buf, length, &s->ps, &s->sei, &s->is_nalff,
3066                                    &s->nal_length_size, s->avctx->err_recognition,
3067                                    s->apply_defdispwin, s->avctx);
3068     if (ret < 0)
3069         return ret;
3070
3071     /* export stream parameters from the first SPS */
3072     for (i = 0; i < FF_ARRAY_ELEMS(s->ps.sps_list); i++) {
3073         if (first && s->ps.sps_list[i]) {
3074             const HEVCSPS *sps = (const HEVCSPS*)s->ps.sps_list[i]->data;
3075             export_stream_params(s->avctx, &s->ps, sps);
3076             break;
3077         }
3078     }
3079
3080     return 0;
3081 }
3082
3083 static int hevc_decode_frame(AVCodecContext *avctx, void *data, int *got_output,
3084                              AVPacket *avpkt)
3085 {
3086     int ret;
3087     int new_extradata_size;
3088     uint8_t *new_extradata;
3089     HEVCContext *s = avctx->priv_data;
3090
3091     if (!avpkt->size) {
3092         ret = ff_hevc_output_frame(s, data, 1);
3093         if (ret < 0)
3094             return ret;
3095
3096         *got_output = ret;
3097         return 0;
3098     }
3099
3100     new_extradata = av_packet_get_side_data(avpkt, AV_PKT_DATA_NEW_EXTRADATA,
3101                                             &new_extradata_size);
3102     if (new_extradata && new_extradata_size > 0) {
3103         ret = hevc_decode_extradata(s, new_extradata, new_extradata_size, 0);
3104         if (ret < 0)
3105             return ret;
3106     }
3107
3108     s->ref = NULL;
3109     ret    = decode_nal_units(s, avpkt->data, avpkt->size);
3110     if (ret < 0)
3111         return ret;
3112
3113     if (avctx->hwaccel) {
3114         if (s->ref && (ret = avctx->hwaccel->end_frame(avctx)) < 0) {
3115             av_log(avctx, AV_LOG_ERROR,
3116                    "hardware accelerator failed to decode picture\n");
3117             ff_hevc_unref_frame(s, s->ref, ~0);
3118             return ret;
3119         }
3120     } else {
3121         /* verify the SEI checksum */
3122         if (avctx->err_recognition & AV_EF_CRCCHECK && s->is_decoded &&
3123             s->sei.picture_hash.is_md5) {
3124             ret = verify_md5(s, s->ref->frame);
3125             if (ret < 0 && avctx->err_recognition & AV_EF_EXPLODE) {
3126                 ff_hevc_unref_frame(s, s->ref, ~0);
3127                 return ret;
3128             }
3129         }
3130     }
3131     s->sei.picture_hash.is_md5 = 0;
3132
3133     if (s->is_decoded) {
3134         av_log(avctx, AV_LOG_DEBUG, "Decoded frame with POC %d.\n", s->poc);
3135         s->is_decoded = 0;
3136     }
3137
3138     if (s->output_frame->buf[0]) {
3139         av_frame_move_ref(data, s->output_frame);
3140         *got_output = 1;
3141     }
3142
3143     return avpkt->size;
3144 }
3145
3146 static int hevc_ref_frame(HEVCContext *s, HEVCFrame *dst, HEVCFrame *src)
3147 {
3148     int ret;
3149
3150     ret = ff_thread_ref_frame(&dst->tf, &src->tf);
3151     if (ret < 0)
3152         return ret;
3153
3154     dst->tab_mvf_buf = av_buffer_ref(src->tab_mvf_buf);
3155     if (!dst->tab_mvf_buf)
3156         goto fail;
3157     dst->tab_mvf = src->tab_mvf;
3158
3159     dst->rpl_tab_buf = av_buffer_ref(src->rpl_tab_buf);
3160     if (!dst->rpl_tab_buf)
3161         goto fail;
3162     dst->rpl_tab = src->rpl_tab;
3163
3164     dst->rpl_buf = av_buffer_ref(src->rpl_buf);
3165     if (!dst->rpl_buf)
3166         goto fail;
3167
3168     dst->poc        = src->poc;
3169     dst->ctb_count  = src->ctb_count;
3170     dst->flags      = src->flags;
3171     dst->sequence   = src->sequence;
3172
3173     if (src->hwaccel_picture_private) {
3174         dst->hwaccel_priv_buf = av_buffer_ref(src->hwaccel_priv_buf);
3175         if (!dst->hwaccel_priv_buf)
3176             goto fail;
3177         dst->hwaccel_picture_private = dst->hwaccel_priv_buf->data;
3178     }
3179
3180     return 0;
3181 fail:
3182     ff_hevc_unref_frame(s, dst, ~0);
3183     return AVERROR(ENOMEM);
3184 }
3185
3186 static av_cold int hevc_decode_free(AVCodecContext *avctx)
3187 {
3188     HEVCContext       *s = avctx->priv_data;
3189     int i;
3190
3191     pic_arrays_free(s);
3192
3193     av_freep(&s->sei.picture_hash.md5_ctx);
3194
3195     av_freep(&s->cabac_state);
3196
3197     for (i = 0; i < 3; i++) {
3198         av_freep(&s->sao_pixel_buffer_h[i]);
3199         av_freep(&s->sao_pixel_buffer_v[i]);
3200     }
3201     av_frame_free(&s->output_frame);
3202
3203     for (i = 0; i < FF_ARRAY_ELEMS(s->DPB); i++) {
3204         ff_hevc_unref_frame(s, &s->DPB[i], ~0);
3205         av_frame_free(&s->DPB[i].frame);
3206     }
3207
3208     for (i = 0; i < FF_ARRAY_ELEMS(s->ps.vps_list); i++)
3209         av_buffer_unref(&s->ps.vps_list[i]);
3210     for (i = 0; i < FF_ARRAY_ELEMS(s->ps.sps_list); i++)
3211         av_buffer_unref(&s->ps.sps_list[i]);
3212     for (i = 0; i < FF_ARRAY_ELEMS(s->ps.pps_list); i++)
3213         av_buffer_unref(&s->ps.pps_list[i]);
3214     s->ps.sps = NULL;
3215     s->ps.pps = NULL;
3216     s->ps.vps = NULL;
3217
3218     av_freep(&s->sh.entry_point_offset);
3219     av_freep(&s->sh.offset);
3220     av_freep(&s->sh.size);
3221
3222     for (i = 1; i < s->threads_number; i++) {
3223         HEVCLocalContext *lc = s->HEVClcList[i];
3224         if (lc) {
3225             av_freep(&s->HEVClcList[i]);
3226             av_freep(&s->sList[i]);
3227         }
3228     }
3229     if (s->HEVClc == s->HEVClcList[0])
3230         s->HEVClc = NULL;
3231     av_freep(&s->HEVClcList[0]);
3232
3233     ff_h2645_packet_uninit(&s->pkt);
3234
3235     return 0;
3236 }
3237
3238 static av_cold int hevc_init_context(AVCodecContext *avctx)
3239 {
3240     HEVCContext *s = avctx->priv_data;
3241     int i;
3242
3243     s->avctx = avctx;
3244
3245     s->HEVClc = av_mallocz(sizeof(HEVCLocalContext));
3246     if (!s->HEVClc)
3247         goto fail;
3248     s->HEVClcList[0] = s->HEVClc;
3249     s->sList[0] = s;
3250
3251     s->cabac_state = av_malloc(HEVC_CONTEXTS);
3252     if (!s->cabac_state)
3253         goto fail;
3254
3255     s->output_frame = av_frame_alloc();
3256     if (!s->output_frame)
3257         goto fail;
3258
3259     for (i = 0; i < FF_ARRAY_ELEMS(s->DPB); i++) {
3260         s->DPB[i].frame = av_frame_alloc();
3261         if (!s->DPB[i].frame)
3262             goto fail;
3263         s->DPB[i].tf.f = s->DPB[i].frame;
3264     }
3265
3266     s->max_ra = INT_MAX;
3267
3268     s->sei.picture_hash.md5_ctx = av_md5_alloc();
3269     if (!s->sei.picture_hash.md5_ctx)
3270         goto fail;
3271
3272     ff_bswapdsp_init(&s->bdsp);
3273
3274     s->context_initialized = 1;
3275     s->eos = 0;
3276
3277     ff_hevc_reset_sei(&s->sei);
3278
3279     return 0;
3280
3281 fail:
3282     hevc_decode_free(avctx);
3283     return AVERROR(ENOMEM);
3284 }
3285
3286 static int hevc_update_thread_context(AVCodecContext *dst,
3287                                       const AVCodecContext *src)
3288 {
3289     HEVCContext *s  = dst->priv_data;
3290     HEVCContext *s0 = src->priv_data;
3291     int i, ret;
3292
3293     if (!s->context_initialized) {
3294         ret = hevc_init_context(dst);
3295         if (ret < 0)
3296             return ret;
3297     }
3298
3299     for (i = 0; i < FF_ARRAY_ELEMS(s->DPB); i++) {
3300         ff_hevc_unref_frame(s, &s->DPB[i], ~0);
3301         if (s0->DPB[i].frame->buf[0]) {
3302             ret = hevc_ref_frame(s, &s->DPB[i], &s0->DPB[i]);
3303             if (ret < 0)
3304                 return ret;
3305         }
3306     }
3307
3308     if (s->ps.sps != s0->ps.sps)
3309         s->ps.sps = NULL;
3310     for (i = 0; i < FF_ARRAY_ELEMS(s->ps.vps_list); i++) {
3311         av_buffer_unref(&s->ps.vps_list[i]);
3312         if (s0->ps.vps_list[i]) {
3313             s->ps.vps_list[i] = av_buffer_ref(s0->ps.vps_list[i]);
3314             if (!s->ps.vps_list[i])
3315                 return AVERROR(ENOMEM);
3316         }
3317     }
3318
3319     for (i = 0; i < FF_ARRAY_ELEMS(s->ps.sps_list); i++) {
3320         av_buffer_unref(&s->ps.sps_list[i]);
3321         if (s0->ps.sps_list[i]) {
3322             s->ps.sps_list[i] = av_buffer_ref(s0->ps.sps_list[i]);
3323             if (!s->ps.sps_list[i])
3324                 return AVERROR(ENOMEM);
3325         }
3326     }
3327
3328     for (i = 0; i < FF_ARRAY_ELEMS(s->ps.pps_list); i++) {
3329         av_buffer_unref(&s->ps.pps_list[i]);
3330         if (s0->ps.pps_list[i]) {
3331             s->ps.pps_list[i] = av_buffer_ref(s0->ps.pps_list[i]);
3332             if (!s->ps.pps_list[i])
3333                 return AVERROR(ENOMEM);
3334         }
3335     }
3336
3337     if (s->ps.sps != s0->ps.sps)
3338         if ((ret = set_sps(s, s0->ps.sps, src->pix_fmt)) < 0)
3339             return ret;
3340
3341     s->seq_decode = s0->seq_decode;
3342     s->seq_output = s0->seq_output;
3343     s->pocTid0    = s0->pocTid0;
3344     s->max_ra     = s0->max_ra;
3345     s->eos        = s0->eos;
3346     s->no_rasl_output_flag = s0->no_rasl_output_flag;
3347
3348     s->is_nalff        = s0->is_nalff;
3349     s->nal_length_size = s0->nal_length_size;
3350
3351     s->threads_number      = s0->threads_number;
3352     s->threads_type        = s0->threads_type;
3353
3354     if (s0->eos) {
3355         s->seq_decode = (s->seq_decode + 1) & 0xff;
3356         s->max_ra = INT_MAX;
3357     }
3358
3359     s->sei.frame_packing        = s0->sei.frame_packing;
3360     s->sei.display_orientation  = s0->sei.display_orientation;
3361     s->sei.mastering_display    = s0->sei.mastering_display;
3362     s->sei.content_light        = s0->sei.content_light;
3363     s->sei.alternative_transfer = s0->sei.alternative_transfer;
3364
3365     return 0;
3366 }
3367
3368 static av_cold int hevc_decode_init(AVCodecContext *avctx)
3369 {
3370     HEVCContext *s = avctx->priv_data;
3371     int ret;
3372
3373     avctx->internal->allocate_progress = 1;
3374
3375     ret = hevc_init_context(avctx);
3376     if (ret < 0)
3377         return ret;
3378
3379     s->enable_parallel_tiles = 0;
3380     s->sei.picture_timing.picture_struct = 0;
3381     s->eos = 1;
3382
3383     atomic_init(&s->wpp_err, 0);
3384
3385     if(avctx->active_thread_type & FF_THREAD_SLICE)
3386         s->threads_number = avctx->thread_count;
3387     else
3388         s->threads_number = 1;
3389
3390     if (avctx->extradata_size > 0 && avctx->extradata) {
3391         ret = hevc_decode_extradata(s, avctx->extradata, avctx->extradata_size, 1);
3392         if (ret < 0) {
3393             hevc_decode_free(avctx);
3394             return ret;
3395         }
3396     }
3397
3398     if((avctx->active_thread_type & FF_THREAD_FRAME) && avctx->thread_count > 1)
3399             s->threads_type = FF_THREAD_FRAME;
3400         else
3401             s->threads_type = FF_THREAD_SLICE;