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