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