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