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