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