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