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