d59b4e48feba12556bfcaf55cece88d84885fd1f
[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(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(gb);
419     nb_sh = get_ue_golomb(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(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(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(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(gb) + 1;
650                 if (sh->slice_type == B_SLICE)
651                     sh->nb_refs[L1] = get_ue_golomb(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(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(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(gb);
763         if (sh->num_entry_point_offsets > 0) {
764             int offset_len = get_ue_golomb(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(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 num_last_subset;
893     int x_cg_last_sig, y_cg_last_sig;
894
895     const uint8_t *scan_x_cg, *scan_y_cg, *scan_x_off, *scan_y_off;
896
897     ptrdiff_t stride = s->frame->linesize[c_idx];
898     int hshift = s->sps->hshift[c_idx];
899     int vshift = s->sps->vshift[c_idx];
900     uint8_t *dst = &s->frame->data[c_idx][(y0 >> vshift) * stride +
901                                            ((x0 >> hshift) << s->sps->pixel_shift)];
902     DECLARE_ALIGNED( 16, int16_t, coeffs[MAX_TB_SIZE * MAX_TB_SIZE] ) = {0};
903
904     int trafo_size = 1 << log2_trafo_size;
905     int i;
906     int qp,shift,add,scale,scale_m;
907     const uint8_t level_scale[] = { 40, 45, 51, 57, 64, 72 };
908     const uint8_t *scale_matrix;
909     uint8_t dc_scale;
910
911     // Derive QP for dequant
912     if (!lc->cu.cu_transquant_bypass_flag) {
913         static const int qp_c[] = { 29, 30, 31, 32, 33, 33, 34, 34, 35, 35, 36, 36, 37, 37 };
914         int qp_y = lc->qp_y;
915
916         if (c_idx == 0) {
917             qp = qp_y + s->sps->qp_bd_offset;
918         } else {
919             int qp_i, offset;
920
921             if (c_idx == 1)
922                 offset = s->pps->cb_qp_offset + s->sh.slice_cb_qp_offset;
923             else
924                 offset = s->pps->cr_qp_offset + s->sh.slice_cr_qp_offset;
925
926             qp_i = av_clip_c(qp_y + offset, - s->sps->qp_bd_offset, 57);
927             if (qp_i < 30)
928                 qp = qp_i;
929             else if (qp_i > 43)
930                 qp = qp_i - 6;
931             else
932                 qp = qp_c[qp_i - 30];
933
934             qp += s->sps->qp_bd_offset;
935         }
936
937         shift    = s->sps->bit_depth + log2_trafo_size - 5;
938         add      = 1 << (shift-1);
939         scale    = level_scale[qp%6] << (qp/6);
940         scale_m  = 16; // default when no custom scaling lists.
941         dc_scale = 16;
942
943         if (s->sps->scaling_list_enable_flag) {
944             const ScalingList *sl = s->pps->pps_scaling_list_data_present_flag ?
945                                     &s->pps->scaling_list : &s->sps->scaling_list;
946             int matrix_id = lc->cu.pred_mode != MODE_INTRA;
947
948             if (log2_trafo_size != 5)
949                 matrix_id = 3 * matrix_id + c_idx;
950
951             scale_matrix = sl->sl[log2_trafo_size - 2][matrix_id];
952             if (log2_trafo_size >= 4)
953                 dc_scale = sl->sl_dc[log2_trafo_size - 4][matrix_id];
954         }
955     }
956
957     memset(lc->rc.significant_coeff_group_flag, 0, 8 * 8);
958
959     if (s->pps->transform_skip_enabled_flag && !lc->cu.cu_transquant_bypass_flag &&
960         log2_trafo_size == 2) {
961         transform_skip_flag = ff_hevc_transform_skip_flag_decode(s, c_idx);
962     }
963
964     last_significant_coeff_x =
965         ff_hevc_last_significant_coeff_x_prefix_decode(s, c_idx, log2_trafo_size);
966     last_significant_coeff_y =
967         ff_hevc_last_significant_coeff_y_prefix_decode(s, c_idx, log2_trafo_size);
968
969     if (last_significant_coeff_x > 3) {
970         int suffix = ff_hevc_last_significant_coeff_suffix_decode(s, last_significant_coeff_x);
971         last_significant_coeff_x = (1 << ((last_significant_coeff_x >> 1) - 1)) *
972                                    (2 + (last_significant_coeff_x & 1)) +
973                                    suffix;
974     }
975
976     if (last_significant_coeff_y > 3) {
977         int suffix = ff_hevc_last_significant_coeff_suffix_decode(s, last_significant_coeff_y);
978         last_significant_coeff_y = (1 << ((last_significant_coeff_y >> 1) - 1)) *
979                                    (2 + (last_significant_coeff_y & 1)) +
980                                    suffix;
981     }
982
983     if (scan_idx == SCAN_VERT)
984         FFSWAP(int, last_significant_coeff_x, last_significant_coeff_y);
985
986     x_cg_last_sig = last_significant_coeff_x >> 2;
987     y_cg_last_sig = last_significant_coeff_y >> 2;
988
989     switch (scan_idx) {
990     case SCAN_DIAG: {
991         int last_x_c = last_significant_coeff_x & 3;
992         int last_y_c = last_significant_coeff_y & 3;
993
994         scan_x_off = ff_hevc_diag_scan4x4_x;
995         scan_y_off = ff_hevc_diag_scan4x4_y;
996         num_coeff = diag_scan4x4_inv[last_y_c][last_x_c];
997         if (trafo_size == 4) {
998             scan_x_cg = scan_1x1;
999             scan_y_cg = scan_1x1;
1000         } else if (trafo_size == 8) {
1001             num_coeff += diag_scan2x2_inv[y_cg_last_sig][x_cg_last_sig] << 4;
1002             scan_x_cg = diag_scan2x2_x;
1003             scan_y_cg = diag_scan2x2_y;
1004         } else if (trafo_size == 16) {
1005             num_coeff += diag_scan4x4_inv[y_cg_last_sig][x_cg_last_sig] << 4;
1006             scan_x_cg = ff_hevc_diag_scan4x4_x;
1007             scan_y_cg = ff_hevc_diag_scan4x4_y;
1008         } else { // trafo_size == 32
1009             num_coeff += diag_scan8x8_inv[y_cg_last_sig][x_cg_last_sig] << 4;
1010             scan_x_cg = ff_hevc_diag_scan8x8_x;
1011             scan_y_cg = ff_hevc_diag_scan8x8_y;
1012         }
1013         break;
1014     }
1015     case SCAN_HORIZ:
1016         scan_x_cg = horiz_scan2x2_x;
1017         scan_y_cg = horiz_scan2x2_y;
1018         scan_x_off = horiz_scan4x4_x;
1019         scan_y_off = horiz_scan4x4_y;
1020         num_coeff = horiz_scan8x8_inv[last_significant_coeff_y][last_significant_coeff_x];
1021         break;
1022     default: //SCAN_VERT
1023         scan_x_cg = horiz_scan2x2_y;
1024         scan_y_cg = horiz_scan2x2_x;
1025         scan_x_off = horiz_scan4x4_y;
1026         scan_y_off = horiz_scan4x4_x;
1027         num_coeff = horiz_scan8x8_inv[last_significant_coeff_x][last_significant_coeff_y];
1028         break;
1029     }
1030     num_coeff++;
1031
1032     num_last_subset = (num_coeff - 1) >> 4;
1033
1034     for (i = num_last_subset; i >= 0; i--) {
1035         int n, m;
1036         int first_nz_pos_in_cg, last_nz_pos_in_cg, num_sig_coeff, first_greater1_coeff_idx;
1037         int sign_hidden;
1038         int sum_abs;
1039         int x_cg, y_cg, x_c, y_c, pos;
1040         int implicit_non_zero_coeff = 0;
1041         int64_t trans_coeff_level;
1042
1043         int offset = i << 4;
1044
1045         uint8_t significant_coeff_flag_idx[16] = {0};
1046         uint8_t coeff_abs_level_greater1_flag[16] = {0};
1047         uint8_t coeff_abs_level_greater2_flag[16] = {0};
1048         uint16_t coeff_sign_flag;
1049         uint8_t nb_significant_coeff_flag = 0;
1050
1051         int first_elem;
1052
1053         x_cg = scan_x_cg[i];
1054         y_cg = scan_y_cg[i];
1055
1056         if ((i < num_last_subset) && (i > 0)) {
1057             lc->rc.significant_coeff_group_flag[x_cg][y_cg] =
1058             ff_hevc_significant_coeff_group_flag_decode(s, c_idx, x_cg, y_cg,
1059                                                         log2_trafo_size);
1060             implicit_non_zero_coeff = 1;
1061         } else {
1062             lc->rc.significant_coeff_group_flag[x_cg][y_cg] =
1063             ((x_cg == x_cg_last_sig && y_cg == y_cg_last_sig) ||
1064              (x_cg == 0 && y_cg == 0));
1065         }
1066
1067         last_scan_pos = num_coeff - offset - 1;
1068
1069         if (i == num_last_subset) {
1070             n_end = last_scan_pos - 1;
1071             significant_coeff_flag_idx[0] = last_scan_pos;
1072             nb_significant_coeff_flag = 1;
1073         } else {
1074             n_end = 15;
1075         }
1076         for (n = n_end; n >= 0; n--) {
1077             GET_COORD(offset, n);
1078
1079             if (lc->rc.significant_coeff_group_flag[x_cg][y_cg] &&
1080                 (n > 0 || implicit_non_zero_coeff == 0)) {
1081                 if (ff_hevc_significant_coeff_flag_decode(s, c_idx, x_c, y_c, log2_trafo_size, scan_idx) == 1) {
1082                     significant_coeff_flag_idx[nb_significant_coeff_flag] = n;
1083                     nb_significant_coeff_flag = nb_significant_coeff_flag + 1;
1084                     implicit_non_zero_coeff = 0;
1085                 }
1086             } else {
1087                 int last_cg = (x_c == (x_cg << 2) && y_c == (y_cg << 2));
1088                 if (last_cg && implicit_non_zero_coeff && lc->rc.significant_coeff_group_flag[x_cg][y_cg]) {
1089                     significant_coeff_flag_idx[nb_significant_coeff_flag] = n;
1090                     nb_significant_coeff_flag = nb_significant_coeff_flag + 1;
1091                 }
1092             }
1093         }
1094
1095         n_end = nb_significant_coeff_flag;
1096
1097         first_nz_pos_in_cg = 16;
1098         last_nz_pos_in_cg = -1;
1099         num_sig_coeff = 0;
1100         first_greater1_coeff_idx = -1;
1101         for (m = 0; m < n_end; m++) {
1102             n = significant_coeff_flag_idx[m];
1103             if (num_sig_coeff < 8) {
1104                 coeff_abs_level_greater1_flag[n] =
1105                 ff_hevc_coeff_abs_level_greater1_flag_decode(s, c_idx, i, n,
1106                                                              (num_sig_coeff == 0),
1107                                                              (i == num_last_subset));
1108                 num_sig_coeff++;
1109                 if (coeff_abs_level_greater1_flag[n] &&
1110                     first_greater1_coeff_idx == -1)
1111                     first_greater1_coeff_idx = n;
1112             }
1113             if (last_nz_pos_in_cg == -1)
1114                 last_nz_pos_in_cg = n;
1115             first_nz_pos_in_cg = n;
1116         }
1117
1118         sign_hidden = (last_nz_pos_in_cg - first_nz_pos_in_cg >= 4 &&
1119                        !lc->cu.cu_transquant_bypass_flag);
1120         if (first_greater1_coeff_idx != -1) {
1121             coeff_abs_level_greater2_flag[first_greater1_coeff_idx] =
1122             ff_hevc_coeff_abs_level_greater2_flag_decode(s, c_idx, i, first_greater1_coeff_idx);
1123         }
1124         if (!s->pps->sign_data_hiding_flag || !sign_hidden ) {
1125             coeff_sign_flag = ff_hevc_coeff_sign_flag(s, nb_significant_coeff_flag) << (16 - nb_significant_coeff_flag);
1126         } else {
1127             coeff_sign_flag = ff_hevc_coeff_sign_flag(s, nb_significant_coeff_flag-1) << (16 - (nb_significant_coeff_flag - 1));
1128         }
1129
1130         num_sig_coeff = 0;
1131         sum_abs = 0;
1132         first_elem = 1;
1133         for (m = 0; m < n_end; m++) {
1134             n = significant_coeff_flag_idx[m];
1135             GET_COORD(offset, n);
1136             trans_coeff_level = 1 + coeff_abs_level_greater1_flag[n] +
1137                                 coeff_abs_level_greater2_flag[n];
1138             if (trans_coeff_level == ((num_sig_coeff < 8) ?
1139                                       ((n == first_greater1_coeff_idx) ? 3 : 2) : 1)) {
1140                 trans_coeff_level += ff_hevc_coeff_abs_level_remaining(s, first_elem, trans_coeff_level);
1141                 first_elem = 0;
1142             }
1143             if (s->pps->sign_data_hiding_flag && sign_hidden) {
1144                 sum_abs += trans_coeff_level;
1145                 if (n == first_nz_pos_in_cg && ((sum_abs&1) == 1))
1146                     trans_coeff_level = -trans_coeff_level;
1147             }
1148             if (coeff_sign_flag >> 15)
1149                 trans_coeff_level = -trans_coeff_level;
1150             coeff_sign_flag <<= 1;
1151             num_sig_coeff++;
1152
1153             if (!lc->cu.cu_transquant_bypass_flag) {
1154                 if(s->sps->scaling_list_enable_flag) {
1155                     if(y_c || x_c || log2_trafo_size < 4) {
1156                         switch(log2_trafo_size) {
1157                             case 3: pos = (y_c << 3) + x_c; break;
1158                             case 4: pos = ((y_c >> 1) << 3) + (x_c >> 1); break;
1159                             case 5: pos = ((y_c >> 2) << 3) + (x_c >> 2); break;
1160                             default: pos = (y_c << 2) + x_c;
1161                         }
1162                         scale_m = scale_matrix[pos];
1163                     } else
1164                         scale_m = dc_scale;
1165                 }
1166                 trans_coeff_level = (trans_coeff_level * (int64_t)scale * (int64_t)scale_m + add) >> shift;
1167                 if (trans_coeff_level < 0) {
1168                     if((~trans_coeff_level) & 0xFffffffffff8000)
1169                         trans_coeff_level = -32768;
1170                 } else {
1171                     if(trans_coeff_level & 0xffffffffffff8000)
1172                         trans_coeff_level = 32767;
1173                 }
1174             }
1175
1176             coeffs[y_c * trafo_size + x_c] = trans_coeff_level;
1177         }
1178     }
1179
1180     if (lc->cu.cu_transquant_bypass_flag) {
1181         s->hevcdsp.transquant_bypass[log2_trafo_size-2](dst, coeffs, stride);
1182     } else {
1183         if (transform_skip_flag)
1184             s->hevcdsp.transform_skip(dst, coeffs, stride);
1185         else if (lc->cu.pred_mode == MODE_INTRA && c_idx == 0 && log2_trafo_size == 2)
1186             s->hevcdsp.transform_4x4_luma_add(dst, coeffs, stride);
1187         else
1188             s->hevcdsp.transform_add[log2_trafo_size-2](dst, coeffs, stride);
1189     }
1190 }
1191
1192 static void hls_transform_unit(HEVCContext *s, int x0, int  y0, int xBase, int yBase, int cb_xBase, int cb_yBase,
1193                                int log2_cb_size, int log2_trafo_size, int trafo_depth, int blk_idx)
1194 {
1195     HEVCLocalContext *lc = &s->HEVClc;
1196     int scan_idx = SCAN_DIAG;
1197     int scan_idx_c = SCAN_DIAG;
1198
1199     if (lc->cu.pred_mode == MODE_INTRA) {
1200         int trafo_size = 1 << log2_trafo_size;
1201         ff_hevc_set_neighbour_available(s, x0, y0, trafo_size, trafo_size);
1202
1203         s->hpc.intra_pred(s, x0, y0, log2_trafo_size, 0);
1204         if (log2_trafo_size > 2) {
1205             trafo_size = trafo_size<<(s->sps->hshift[1]-1);
1206             ff_hevc_set_neighbour_available(s, x0, y0, trafo_size, trafo_size);
1207             s->hpc.intra_pred(s, x0, y0, log2_trafo_size - 1, 1);
1208             s->hpc.intra_pred(s, x0, y0, log2_trafo_size - 1, 2);
1209         } else if (blk_idx == 3) {
1210             trafo_size = trafo_size<<(s->sps->hshift[1]);
1211             ff_hevc_set_neighbour_available(s, xBase, yBase, trafo_size, trafo_size);
1212             s->hpc.intra_pred(s, xBase, yBase, log2_trafo_size, 1);
1213             s->hpc.intra_pred(s, xBase, yBase, log2_trafo_size, 2);
1214         }
1215     }
1216
1217     if (lc->tt.cbf_luma ||
1218         SAMPLE_CBF(lc->tt.cbf_cb[trafo_depth], x0, y0) ||
1219         SAMPLE_CBF(lc->tt.cbf_cr[trafo_depth], x0, y0)) {
1220         if (s->pps->cu_qp_delta_enabled_flag && !lc->tu.is_cu_qp_delta_coded) {
1221             lc->tu.cu_qp_delta = ff_hevc_cu_qp_delta_abs(s);
1222             if (lc->tu.cu_qp_delta != 0)
1223                 if (ff_hevc_cu_qp_delta_sign_flag(s) == 1)
1224                     lc->tu.cu_qp_delta = -lc->tu.cu_qp_delta;
1225             lc->tu.is_cu_qp_delta_coded = 1;
1226             ff_hevc_set_qPy(s, x0, y0, cb_xBase, cb_yBase, log2_cb_size);
1227         }
1228
1229         if (lc->cu.pred_mode == MODE_INTRA && log2_trafo_size < 4) {
1230             if (lc->tu.cur_intra_pred_mode >= 6 &&
1231                 lc->tu.cur_intra_pred_mode <= 14) {
1232                 scan_idx = SCAN_VERT;
1233             } else if (lc->tu.cur_intra_pred_mode >= 22 &&
1234                        lc->tu.cur_intra_pred_mode <= 30) {
1235                 scan_idx = SCAN_HORIZ;
1236             }
1237
1238             if (lc->pu.intra_pred_mode_c >= 6 &&
1239                 lc->pu.intra_pred_mode_c <= 14) {
1240                 scan_idx_c = SCAN_VERT;
1241             } else if (lc->pu.intra_pred_mode_c >= 22 &&
1242                        lc->pu.intra_pred_mode_c <= 30) {
1243                 scan_idx_c = SCAN_HORIZ;
1244             }
1245         }
1246
1247         if (lc->tt.cbf_luma)
1248             hls_residual_coding(s, x0, y0, log2_trafo_size, scan_idx, 0);
1249         if (log2_trafo_size > 2) {
1250             if (SAMPLE_CBF(lc->tt.cbf_cb[trafo_depth], x0, y0))
1251                 hls_residual_coding(s, x0, y0, log2_trafo_size - 1, scan_idx_c, 1);
1252             if (SAMPLE_CBF(lc->tt.cbf_cr[trafo_depth], x0, y0))
1253                 hls_residual_coding(s, x0, y0, log2_trafo_size - 1, scan_idx_c, 2);
1254         } else if (blk_idx == 3) {
1255             if (SAMPLE_CBF(lc->tt.cbf_cb[trafo_depth], xBase, yBase))
1256                 hls_residual_coding(s, xBase, yBase, log2_trafo_size, scan_idx_c, 1);
1257             if (SAMPLE_CBF(lc->tt.cbf_cr[trafo_depth], xBase, yBase))
1258                 hls_residual_coding(s, xBase, yBase, log2_trafo_size, scan_idx_c, 2);
1259         }
1260     }
1261 }
1262
1263 static void set_deblocking_bypass(HEVCContext *s, int x0, int y0, int log2_cb_size)
1264 {
1265     int cb_size = 1 << log2_cb_size;
1266     int log2_min_pu_size = s->sps->log2_min_pu_size;
1267
1268     int pic_width_in_min_pu = s->sps->width >> s->sps->log2_min_pu_size;
1269     int x_end = FFMIN(x0 + cb_size, s->sps->width);
1270     int y_end = FFMIN(y0 + cb_size, s->sps->height);
1271     int i, j;
1272
1273     for (j = (y0 >> log2_min_pu_size); j < (y_end >> log2_min_pu_size); j++)
1274         for (i = (x0 >> log2_min_pu_size); i < (x_end >> log2_min_pu_size); i++)
1275             s->is_pcm[i + j * pic_width_in_min_pu] = 2;
1276 }
1277
1278 static void hls_transform_tree(HEVCContext *s, int x0, int y0, int xBase, int yBase, int cb_xBase, int cb_yBase,
1279                                int log2_cb_size, int log2_trafo_size, int trafo_depth, int blk_idx)
1280 {
1281     HEVCLocalContext *lc = &s->HEVClc;
1282     uint8_t split_transform_flag;
1283
1284     if (trafo_depth > 0 && log2_trafo_size == 2) {
1285         SAMPLE_CBF(lc->tt.cbf_cb[trafo_depth], x0, y0) =
1286             SAMPLE_CBF(lc->tt.cbf_cb[trafo_depth - 1], xBase, yBase);
1287         SAMPLE_CBF(lc->tt.cbf_cr[trafo_depth], x0, y0) =
1288             SAMPLE_CBF(lc->tt.cbf_cr[trafo_depth - 1], xBase, yBase);
1289     } else {
1290         SAMPLE_CBF(lc->tt.cbf_cb[trafo_depth], x0, y0) =
1291             SAMPLE_CBF(lc->tt.cbf_cr[trafo_depth], x0, y0) = 0;
1292     }
1293
1294     if (lc->cu.intra_split_flag) {
1295         if (trafo_depth == 1)
1296             lc->tu.cur_intra_pred_mode = lc->pu.intra_pred_mode[blk_idx];
1297     } else {
1298         lc->tu.cur_intra_pred_mode = lc->pu.intra_pred_mode[0];
1299     }
1300
1301     lc->tt.cbf_luma = 1;
1302
1303     lc->tt.inter_split_flag = (s->sps->max_transform_hierarchy_depth_inter == 0 &&
1304                                lc->cu.pred_mode == MODE_INTER &&
1305                                lc->cu.part_mode != PART_2Nx2N && trafo_depth == 0);
1306
1307     if (log2_trafo_size <= s->sps->log2_max_trafo_size &&
1308         log2_trafo_size > s->sps->log2_min_transform_block_size &&
1309         trafo_depth < lc->cu.max_trafo_depth &&
1310         !(lc->cu.intra_split_flag && trafo_depth == 0)) {
1311         split_transform_flag = ff_hevc_split_transform_flag_decode(s, log2_trafo_size);
1312     } else {
1313         split_transform_flag = (log2_trafo_size > s->sps->log2_max_trafo_size ||
1314                                (lc->cu.intra_split_flag && (trafo_depth == 0)) ||
1315                                lc->tt.inter_split_flag);
1316     }
1317
1318     if (log2_trafo_size > 2) {
1319         if (trafo_depth == 0 ||
1320             SAMPLE_CBF(lc->tt.cbf_cb[trafo_depth - 1], xBase, yBase)) {
1321             SAMPLE_CBF(lc->tt.cbf_cb[trafo_depth], x0, y0) =
1322                 ff_hevc_cbf_cb_cr_decode(s, trafo_depth);
1323         }
1324
1325         if (trafo_depth == 0 || SAMPLE_CBF(lc->tt.cbf_cr[trafo_depth - 1], xBase, yBase)) {
1326             SAMPLE_CBF(lc->tt.cbf_cr[trafo_depth], x0, y0) =
1327                 ff_hevc_cbf_cb_cr_decode(s, trafo_depth);
1328         }
1329     }
1330
1331     if (split_transform_flag) {
1332         int x1 = x0 + ((1 << log2_trafo_size) >> 1);
1333         int y1 = y0 + ((1 << log2_trafo_size) >> 1);
1334
1335         hls_transform_tree(s, x0, y0, x0, y0, cb_xBase, cb_yBase, log2_cb_size,
1336                            log2_trafo_size - 1, trafo_depth + 1, 0);
1337         hls_transform_tree(s, x1, y0, x0, y0, cb_xBase, cb_yBase, log2_cb_size,
1338                            log2_trafo_size - 1, trafo_depth + 1, 1);
1339         hls_transform_tree(s, x0, y1, x0, y0, cb_xBase, cb_yBase, log2_cb_size,
1340                            log2_trafo_size - 1, trafo_depth + 1, 2);
1341         hls_transform_tree(s, x1, y1, x0, y0, cb_xBase, cb_yBase, log2_cb_size,
1342                            log2_trafo_size - 1, trafo_depth + 1, 3);
1343     } else {
1344         int min_tu_size = 1 << s->sps->log2_min_transform_block_size;
1345         int log2_min_tu_size = s->sps->log2_min_transform_block_size;
1346         int pic_width_in_min_tu = s->sps->width >> log2_min_tu_size;
1347         int i, j;
1348
1349         if (lc->cu.pred_mode == MODE_INTRA || trafo_depth != 0 ||
1350             SAMPLE_CBF(lc->tt.cbf_cb[trafo_depth], x0, y0) ||
1351             SAMPLE_CBF(lc->tt.cbf_cr[trafo_depth], x0, y0)) {
1352             lc->tt.cbf_luma = ff_hevc_cbf_luma_decode(s, trafo_depth);
1353         }
1354
1355         hls_transform_unit(s, x0, y0, xBase, yBase, cb_xBase, cb_yBase,
1356                 log2_cb_size, log2_trafo_size, trafo_depth, blk_idx);
1357
1358         // TODO: store cbf_luma somewhere else
1359         if (lc->tt.cbf_luma)
1360             for (i = 0; i < (1 << log2_trafo_size); i += min_tu_size)
1361                 for (j = 0; j < (1 << log2_trafo_size); j += min_tu_size) {
1362                     int x_tu = (x0 + j) >> log2_min_tu_size;
1363                     int y_tu = (y0 + i) >> log2_min_tu_size;
1364                     s->cbf_luma[y_tu * pic_width_in_min_tu + x_tu] = 1;
1365                 }
1366         if (!s->sh.disable_deblocking_filter_flag) {
1367             ff_hevc_deblocking_boundary_strengths(s, x0, y0, log2_trafo_size,
1368                                                   lc->slice_or_tiles_up_boundary,
1369                                                   lc->slice_or_tiles_left_boundary);
1370             if (s->pps->transquant_bypass_enable_flag && lc->cu.cu_transquant_bypass_flag)
1371                 set_deblocking_bypass(s, x0, y0, log2_trafo_size);
1372         }
1373     }
1374 }
1375
1376 static int hls_pcm_sample(HEVCContext *s, int x0, int y0, int log2_cb_size)
1377 {
1378     //TODO: non-4:2:0 support
1379     HEVCLocalContext *lc = &s->HEVClc;
1380     GetBitContext gb;
1381     int cb_size = 1 << log2_cb_size;
1382     int    stride0 = s->frame->linesize[0];
1383     uint8_t *dst0 = &s->frame->data[0][y0 * stride0 + (x0 << s->sps->pixel_shift)];
1384     int   stride1 = s->frame->linesize[1];
1385     uint8_t *dst1 = &s->frame->data[1][(y0 >> s->sps->vshift[1]) * stride1 + ((x0 >> s->sps->hshift[1]) << s->sps->pixel_shift)];
1386     int   stride2 = s->frame->linesize[2];
1387     uint8_t *dst2 = &s->frame->data[2][(y0 >> s->sps->vshift[2]) * stride2 + ((x0 >> s->sps->hshift[2]) << s->sps->pixel_shift)];
1388
1389     int length = cb_size * cb_size * s->sps->pcm.bit_depth + ((cb_size * cb_size) >> 1) * s->sps->pcm.bit_depth;
1390     const uint8_t *pcm = skip_bytes(&s->HEVClc.cc, (length + 7) >> 3);
1391     int ret;
1392
1393     ff_hevc_deblocking_boundary_strengths(s, x0, y0, log2_cb_size,
1394                                           lc->slice_or_tiles_up_boundary,
1395                                           lc->slice_or_tiles_left_boundary);
1396
1397     ret = init_get_bits(&gb, pcm, length);
1398     if (ret < 0)
1399         return ret;
1400
1401     s->hevcdsp.put_pcm(dst0, stride0, cb_size, &gb, s->sps->pcm.bit_depth);
1402     s->hevcdsp.put_pcm(dst1, stride1, cb_size / 2, &gb, s->sps->pcm.bit_depth);
1403     s->hevcdsp.put_pcm(dst2, stride2, cb_size / 2, &gb, s->sps->pcm.bit_depth);
1404     return 0;
1405 }
1406
1407 static void hls_mvd_coding(HEVCContext *s, int x0, int y0, int log2_cb_size)
1408 {
1409     HEVCLocalContext *lc = &s->HEVClc;
1410     int x = ff_hevc_abs_mvd_greater0_flag_decode(s);
1411     int y = ff_hevc_abs_mvd_greater0_flag_decode(s);
1412
1413     if (x)
1414         x += ff_hevc_abs_mvd_greater1_flag_decode(s);
1415     if (y)
1416         y += ff_hevc_abs_mvd_greater1_flag_decode(s);
1417
1418     switch (x) {
1419     case 2: lc->pu.mvd.x = ff_hevc_mvd_decode(s);           break;
1420     case 1: lc->pu.mvd.x = ff_hevc_mvd_sign_flag_decode(s); break;
1421     case 0: lc->pu.mvd.x = 0;                               break;
1422     }
1423
1424     switch (y) {
1425     case 2: lc->pu.mvd.y = ff_hevc_mvd_decode(s);           break;
1426     case 1: lc->pu.mvd.y = ff_hevc_mvd_sign_flag_decode(s); break;
1427     case 0: lc->pu.mvd.y = 0;                               break;
1428     }
1429 }
1430
1431 /**
1432  * 8.5.3.2.2.1 Luma sample interpolation process
1433  *
1434  * @param s HEVC decoding context
1435  * @param dst target buffer for block data at block position
1436  * @param dststride stride of the dst buffer
1437  * @param ref reference picture buffer at origin (0, 0)
1438  * @param mv motion vector (relative to block position) to get pixel data from
1439  * @param x_off horizontal position of block from origin (0, 0)
1440  * @param y_off vertical position of block from origin (0, 0)
1441  * @param block_w width of block
1442  * @param block_h height of block
1443  */
1444 static void luma_mc(HEVCContext *s, int16_t *dst, ptrdiff_t dststride, AVFrame *ref,
1445                     const Mv *mv, int x_off, int y_off, int block_w, int block_h)
1446 {
1447     HEVCLocalContext *lc = &s->HEVClc;
1448     uint8_t *src = ref->data[0];
1449     ptrdiff_t srcstride = ref->linesize[0];
1450     int pic_width = s->sps->width;
1451     int pic_height = s->sps->height;
1452
1453     int mx = mv->x & 3;
1454     int my = mv->y & 3;
1455     int extra_left = ff_hevc_qpel_extra_before[mx];
1456     int extra_top  = ff_hevc_qpel_extra_before[my];
1457
1458     x_off += mv->x >> 2;
1459     y_off += mv->y >> 2;
1460     src   += y_off * srcstride + (x_off << s->sps->pixel_shift);
1461
1462     if (x_off < extra_left || x_off >= pic_width - block_w - ff_hevc_qpel_extra_after[mx] ||
1463         y_off < extra_top || y_off >= pic_height - block_h - ff_hevc_qpel_extra_after[my]) {
1464         int offset = extra_top * srcstride + (extra_left << s->sps->pixel_shift);
1465
1466         s->vdsp.emulated_edge_mc(lc->edge_emu_buffer, srcstride, src - offset, srcstride,
1467                                  block_w + ff_hevc_qpel_extra[mx], block_h + ff_hevc_qpel_extra[my],
1468                                  x_off - extra_left, y_off - extra_top,
1469                                  pic_width, pic_height);
1470         src = lc->edge_emu_buffer + offset;
1471     }
1472     s->hevcdsp.put_hevc_qpel[my][mx](dst, dststride, src, srcstride, block_w,
1473                                      block_h, lc->mc_buffer);
1474 }
1475
1476 /**
1477  * 8.5.3.2.2.2 Chroma sample interpolation process
1478  *
1479  * @param s HEVC decoding context
1480  * @param dst1 target buffer for block data at block position (U plane)
1481  * @param dst2 target buffer for block data at block position (V plane)
1482  * @param dststride stride of the dst1 and dst2 buffers
1483  * @param ref reference picture buffer at origin (0, 0)
1484  * @param mv motion vector (relative to block position) to get pixel data from
1485  * @param x_off horizontal position of block from origin (0, 0)
1486  * @param y_off vertical position of block from origin (0, 0)
1487  * @param block_w width of block
1488  * @param block_h height of block
1489  */
1490 static void chroma_mc(HEVCContext *s, int16_t *dst1, int16_t *dst2, ptrdiff_t dststride, AVFrame *ref,
1491                       const Mv *mv, int x_off, int y_off, int block_w, int block_h)
1492 {
1493     HEVCLocalContext *lc = &s->HEVClc;
1494     uint8_t *src1 = ref->data[1];
1495     uint8_t *src2 = ref->data[2];
1496     ptrdiff_t src1stride = ref->linesize[1];
1497     ptrdiff_t src2stride = ref->linesize[2];
1498     int pic_width  = s->sps->width >> 1;
1499     int pic_height = s->sps->height >> 1;
1500
1501     int mx = mv->x & 7;
1502     int my = mv->y & 7;
1503
1504     x_off += mv->x >> 3;
1505     y_off += mv->y >> 3;
1506     src1 += y_off * src1stride + (x_off << s->sps->pixel_shift);
1507     src2 += y_off * src2stride + (x_off << s->sps->pixel_shift);
1508
1509     if (x_off < EPEL_EXTRA_BEFORE || x_off >= pic_width - block_w - EPEL_EXTRA_AFTER ||
1510         y_off < EPEL_EXTRA_AFTER || y_off >= pic_height - block_h - EPEL_EXTRA_AFTER) {
1511         int offset1 = EPEL_EXTRA_BEFORE * (src1stride + (1 << s->sps->pixel_shift));
1512         int offset2 = EPEL_EXTRA_BEFORE * (src2stride + (1 << s->sps->pixel_shift));
1513
1514         s->vdsp.emulated_edge_mc(lc->edge_emu_buffer, src1stride, src1 - offset1, src1stride,
1515                                  block_w + EPEL_EXTRA, block_h + EPEL_EXTRA,
1516                                  x_off - EPEL_EXTRA_BEFORE, y_off - EPEL_EXTRA_BEFORE,
1517                                  pic_width, pic_height);
1518
1519         src1 = lc->edge_emu_buffer + offset1;
1520         s->hevcdsp.put_hevc_epel[!!my][!!mx](dst1, dststride, src1, src1stride,
1521                                              block_w, block_h, mx, my, lc->mc_buffer);
1522
1523         s->vdsp.emulated_edge_mc(lc->edge_emu_buffer, src2stride, src2 - offset2, src2stride,
1524                                  block_w + EPEL_EXTRA, block_h + EPEL_EXTRA,
1525                                  x_off - EPEL_EXTRA_BEFORE, y_off - EPEL_EXTRA_BEFORE,
1526                                  pic_width, pic_height);
1527         src2 = lc->edge_emu_buffer + offset2;
1528         s->hevcdsp.put_hevc_epel[!!my][!!mx](dst2, dststride, src2, src2stride,
1529                                              block_w, block_h, mx, my, lc->mc_buffer);
1530     } else {
1531         s->hevcdsp.put_hevc_epel[!!my][!!mx](dst1, dststride, src1, src1stride,
1532                                              block_w, block_h, mx, my, lc->mc_buffer);
1533         s->hevcdsp.put_hevc_epel[!!my][!!mx](dst2, dststride, src2, src2stride,
1534                                              block_w, block_h, mx, my, lc->mc_buffer);
1535     }
1536 }
1537
1538 static void hevc_await_progress(HEVCContext *s, HEVCFrame *ref,
1539                                 const Mv *mv, int y0)
1540 {
1541     int y = (mv->y >> 2) + y0;
1542
1543     //ff_thread_await_progress(&ref->tf, FFMIN(s->height, y), 0);
1544     ff_thread_await_progress(&ref->tf, INT_MAX, 0);
1545 }
1546
1547 static void hls_prediction_unit(HEVCContext *s, int x0, int y0, int nPbW, int nPbH, int log2_cb_size, int partIdx)
1548 {
1549 #define POS(c_idx, x, y)                                                              \
1550     &s->frame->data[c_idx][((y) >> s->sps->vshift[c_idx]) * s->frame->linesize[c_idx] + \
1551                            (((x) >> s->sps->hshift[c_idx]) << s->sps->pixel_shift)]
1552     HEVCLocalContext *lc = &s->HEVClc;
1553     int merge_idx = 0;
1554     enum InterPredIdc inter_pred_idc = PRED_L0;
1555     struct MvField current_mv = {{{ 0 }}};
1556
1557     int pic_width_in_min_pu = s->sps->width >> s->sps->log2_min_pu_size;
1558
1559     MvField *tab_mvf = s->ref->tab_mvf;
1560     RefPicList  *refPicList = s->ref->refPicList;
1561     HEVCFrame *ref0, *ref1;
1562
1563     int tmpstride = MAX_PB_SIZE;
1564
1565     uint8_t *dst0 = POS(0, x0, y0);
1566     uint8_t *dst1 = POS(1, x0, y0);
1567     uint8_t *dst2 = POS(2, x0, y0);
1568     int log2_min_cb_size = s->sps->log2_min_coding_block_size;
1569     int pic_width_in_ctb = s->sps->width>>log2_min_cb_size;
1570     int x_cb             = x0 >> log2_min_cb_size;
1571     int y_cb             = y0 >> log2_min_cb_size;
1572     int ref_idx[2];
1573     int mvp_flag[2];
1574     int x_pu, y_pu;
1575     int i, j;
1576
1577     if (SAMPLE_CTB(s->skip_flag, x_cb, y_cb)) {
1578         if (s->sh.max_num_merge_cand > 1)
1579             merge_idx = ff_hevc_merge_idx_decode(s);
1580         else
1581             merge_idx = 0;
1582
1583         ff_hevc_luma_mv_merge_mode(s, x0, y0, 1 << log2_cb_size, 1 << log2_cb_size,
1584                                    log2_cb_size, partIdx, merge_idx, &current_mv);
1585         x_pu = x0 >> s->sps->log2_min_pu_size;
1586         y_pu = y0 >> s->sps->log2_min_pu_size;
1587
1588         for (i = 0; i < nPbW >> s->sps->log2_min_pu_size; i++)
1589             for (j = 0; j < nPbH >> s->sps->log2_min_pu_size; j++)
1590                 tab_mvf[(y_pu + j) * pic_width_in_min_pu + x_pu + i] = current_mv;
1591     } else { /* MODE_INTER */
1592         lc->pu.merge_flag = ff_hevc_merge_flag_decode(s);
1593         if (lc->pu.merge_flag) {
1594             if (s->sh.max_num_merge_cand > 1)
1595                 merge_idx = ff_hevc_merge_idx_decode(s);
1596             else
1597                 merge_idx = 0;
1598
1599             ff_hevc_luma_mv_merge_mode(s, x0, y0, nPbW, nPbH, log2_cb_size,
1600                                        partIdx, merge_idx, &current_mv);
1601             x_pu = x0 >> s->sps->log2_min_pu_size;
1602             y_pu = y0 >> s->sps->log2_min_pu_size;
1603
1604             for (i = 0; i < nPbW >> s->sps->log2_min_pu_size; i++)
1605                 for (j = 0; j < nPbH >> s->sps->log2_min_pu_size; j++)
1606                     tab_mvf[(y_pu + j) * pic_width_in_min_pu + x_pu + i] = current_mv;
1607         } else {
1608             ff_hevc_set_neighbour_available(s, x0, y0, nPbW, nPbH);
1609             if (s->sh.slice_type == B_SLICE)
1610                 inter_pred_idc = ff_hevc_inter_pred_idc_decode(s, nPbW, nPbH);
1611
1612             if (inter_pred_idc != PRED_L1) {
1613                 if (s->sh.nb_refs[L0]) {
1614                     ref_idx[0] = ff_hevc_ref_idx_lx_decode(s, s->sh.nb_refs[L0]);
1615                     current_mv.ref_idx[0] = ref_idx[0];
1616                 }
1617                 current_mv.pred_flag[0] = 1;
1618                 hls_mvd_coding(s, x0, y0, 0);
1619                 mvp_flag[0] = ff_hevc_mvp_lx_flag_decode(s);
1620                 ff_hevc_luma_mv_mvp_mode(s, x0, y0, nPbW, nPbH, log2_cb_size,
1621                                          partIdx, merge_idx, &current_mv, mvp_flag[0], 0);
1622                 current_mv.mv[0].x += lc->pu.mvd.x;
1623                 current_mv.mv[0].y += lc->pu.mvd.y;
1624             }
1625
1626             if (inter_pred_idc != PRED_L0) {
1627                 if (s->sh.nb_refs[L1]) {
1628                     ref_idx[1] = ff_hevc_ref_idx_lx_decode(s, s->sh.nb_refs[L1]);
1629                     current_mv.ref_idx[1] = ref_idx[1];
1630                 }
1631
1632                 if (s->sh.mvd_l1_zero_flag == 1 && inter_pred_idc == PRED_BI) {
1633                     lc->pu.mvd.x = 0;
1634                     lc->pu.mvd.y = 0;
1635                 } else {
1636                     hls_mvd_coding(s, x0, y0, 1);
1637                 }
1638
1639                 current_mv.pred_flag[1] = 1;
1640                 mvp_flag[1] = ff_hevc_mvp_lx_flag_decode(s);
1641                 ff_hevc_luma_mv_mvp_mode(s, x0, y0, nPbW, nPbH, log2_cb_size,
1642                                          partIdx, merge_idx, &current_mv, mvp_flag[1], 1);
1643                 current_mv.mv[1].x += lc->pu.mvd.x;
1644                 current_mv.mv[1].y += lc->pu.mvd.y;
1645             }
1646
1647             x_pu = x0 >> s->sps->log2_min_pu_size;
1648             y_pu = y0 >> s->sps->log2_min_pu_size;
1649
1650             for (i = 0; i < nPbW >> s->sps->log2_min_pu_size; i++)
1651                 for(j = 0; j < nPbH >> s->sps->log2_min_pu_size; j++)
1652                     tab_mvf[(y_pu + j) * pic_width_in_min_pu + x_pu + i] = current_mv;
1653         }
1654     }
1655
1656     if (current_mv.pred_flag[0]) {
1657         ref0 = refPicList[0].ref[current_mv.ref_idx[0]];
1658         if (!ref0)
1659             return;
1660         hevc_await_progress(s, ref0, &current_mv.mv[0], y0);
1661     }
1662     if (current_mv.pred_flag[1]) {
1663         ref1 = refPicList[1].ref[current_mv.ref_idx[1]];
1664         if (!ref1)
1665             return;
1666         hevc_await_progress(s, ref1, &current_mv.mv[1], y0);
1667     }
1668
1669     if (current_mv.pred_flag[0] && !current_mv.pred_flag[1]) {
1670         DECLARE_ALIGNED(16, int16_t, tmp [MAX_PB_SIZE * MAX_PB_SIZE]);
1671         DECLARE_ALIGNED(16, int16_t, tmp2[MAX_PB_SIZE * MAX_PB_SIZE]);
1672
1673         luma_mc(s, tmp, tmpstride, ref0->frame,
1674                 &current_mv.mv[0], x0, y0, nPbW, nPbH);
1675
1676         if ((s->sh.slice_type == P_SLICE && s->pps->weighted_pred_flag) ||
1677             (s->sh.slice_type == B_SLICE && s->pps->weighted_bipred_flag)) {
1678             s->hevcdsp.weighted_pred(s->sh.luma_log2_weight_denom,
1679                                      s->sh.luma_weight_l0[current_mv.ref_idx[0]],
1680                                      s->sh.luma_offset_l0[current_mv.ref_idx[0]],
1681                                      dst0, s->frame->linesize[0], tmp, tmpstride, nPbW, nPbH);
1682         } else {
1683             s->hevcdsp.put_unweighted_pred(dst0, s->frame->linesize[0], tmp, tmpstride, nPbW, nPbH);
1684         }
1685         chroma_mc(s, tmp, tmp2, tmpstride, ref0->frame,
1686                   &current_mv.mv[0], x0 / 2, y0 / 2, nPbW / 2, nPbH / 2);
1687
1688         if ((s->sh.slice_type == P_SLICE && s->pps->weighted_pred_flag) ||
1689             (s->sh.slice_type == B_SLICE && s->pps->weighted_bipred_flag)) {
1690             s->hevcdsp.weighted_pred(s->sh.chroma_log2_weight_denom,
1691                                      s->sh.chroma_weight_l0[current_mv.ref_idx[0]][0],
1692                                      s->sh.chroma_offset_l0[current_mv.ref_idx[0]][0],
1693                                      dst1, s->frame->linesize[1], tmp, tmpstride,
1694                                      nPbW / 2, nPbH / 2);
1695             s->hevcdsp.weighted_pred(s->sh.chroma_log2_weight_denom,
1696                                      s->sh.chroma_weight_l0[current_mv.ref_idx[0]][1],
1697                                      s->sh.chroma_offset_l0[current_mv.ref_idx[0]][1],
1698                                      dst2, s->frame->linesize[2], tmp2, tmpstride,
1699                                      nPbW / 2, nPbH / 2);
1700         } else {
1701             s->hevcdsp.put_unweighted_pred(dst1, s->frame->linesize[1], tmp, tmpstride, nPbW/2, nPbH/2);
1702             s->hevcdsp.put_unweighted_pred(dst2, s->frame->linesize[2], tmp2, tmpstride, nPbW/2, nPbH/2);
1703         }
1704     } else if (!current_mv.pred_flag[0] && current_mv.pred_flag[1]) {
1705         DECLARE_ALIGNED(16, int16_t, tmp [MAX_PB_SIZE * MAX_PB_SIZE]);
1706         DECLARE_ALIGNED(16, int16_t, tmp2[MAX_PB_SIZE * MAX_PB_SIZE]);
1707
1708         if (!ref1)
1709             return;
1710
1711         luma_mc(s, tmp, tmpstride, ref1->frame,
1712                 &current_mv.mv[1], x0, y0, nPbW, nPbH);
1713
1714         if ((s->sh.slice_type == P_SLICE && s->pps->weighted_pred_flag) ||
1715             (s->sh.slice_type == B_SLICE && s->pps->weighted_bipred_flag)) {
1716             s->hevcdsp.weighted_pred(s->sh.luma_log2_weight_denom,
1717                                       s->sh.luma_weight_l1[current_mv.ref_idx[1]],
1718                                       s->sh.luma_offset_l1[current_mv.ref_idx[1]],
1719                                       dst0, s->frame->linesize[0], tmp, tmpstride,
1720                                       nPbW, nPbH);
1721         } else {
1722             s->hevcdsp.put_unweighted_pred(dst0, s->frame->linesize[0], tmp, tmpstride, nPbW, nPbH);
1723         }
1724
1725         chroma_mc(s, tmp, tmp2, tmpstride, ref1->frame,
1726                   &current_mv.mv[1], x0/2, y0/2, nPbW/2, nPbH/2);
1727
1728         if ((s->sh.slice_type == P_SLICE && s->pps->weighted_pred_flag) ||
1729             (s->sh.slice_type == B_SLICE && s->pps->weighted_bipred_flag)) {
1730             s->hevcdsp.weighted_pred(s->sh.chroma_log2_weight_denom,
1731                                      s->sh.chroma_weight_l1[current_mv.ref_idx[1]][0],
1732                                      s->sh.chroma_offset_l1[current_mv.ref_idx[1]][0],
1733                                      dst1, s->frame->linesize[1], tmp, tmpstride, nPbW/2, nPbH/2);
1734             s->hevcdsp.weighted_pred(s->sh.chroma_log2_weight_denom,
1735                                      s->sh.chroma_weight_l1[current_mv.ref_idx[1]][1],
1736                                      s->sh.chroma_offset_l1[current_mv.ref_idx[1]][1],
1737                                      dst2, s->frame->linesize[2], tmp2, tmpstride, nPbW/2, nPbH/2);
1738         } else {
1739             s->hevcdsp.put_unweighted_pred(dst1, s->frame->linesize[1], tmp, tmpstride, nPbW/2, nPbH/2);
1740             s->hevcdsp.put_unweighted_pred(dst2, s->frame->linesize[2], tmp2, tmpstride, nPbW/2, nPbH/2);
1741         }
1742     } else if (current_mv.pred_flag[0] && current_mv.pred_flag[1]) {
1743         DECLARE_ALIGNED(16, int16_t, tmp [MAX_PB_SIZE * MAX_PB_SIZE]);
1744         DECLARE_ALIGNED(16, int16_t, tmp2[MAX_PB_SIZE * MAX_PB_SIZE]);
1745         DECLARE_ALIGNED(16, int16_t, tmp3[MAX_PB_SIZE * MAX_PB_SIZE]);
1746         DECLARE_ALIGNED(16, int16_t, tmp4[MAX_PB_SIZE * MAX_PB_SIZE]);
1747         HEVCFrame *ref0 = refPicList[0].ref[current_mv.ref_idx[0]];
1748         HEVCFrame *ref1 = refPicList[1].ref[current_mv.ref_idx[1]];
1749
1750         if (!ref0 || !ref1)
1751             return;
1752
1753         luma_mc(s, tmp, tmpstride, ref0->frame,
1754                 &current_mv.mv[0], x0, y0, nPbW, nPbH);
1755         luma_mc(s, tmp2, tmpstride, ref1->frame,
1756                 &current_mv.mv[1], x0, y0, nPbW, nPbH);
1757
1758         if ((s->sh.slice_type == P_SLICE && s->pps->weighted_pred_flag) ||
1759             (s->sh.slice_type == B_SLICE && s->pps->weighted_bipred_flag)){
1760             s->hevcdsp.weighted_pred_avg(s->sh.luma_log2_weight_denom,
1761                                          s->sh.luma_weight_l0[current_mv.ref_idx[0]],
1762                                          s->sh.luma_weight_l1[current_mv.ref_idx[1]],
1763                                          s->sh.luma_offset_l0[current_mv.ref_idx[0]],
1764                                          s->sh.luma_offset_l1[current_mv.ref_idx[1]],
1765                                          dst0, s->frame->linesize[0], tmp, tmp2, tmpstride, nPbW, nPbH);
1766         } else {
1767             s->hevcdsp.put_weighted_pred_avg(dst0, s->frame->linesize[0], tmp, tmp2, tmpstride, nPbW, nPbH);
1768         }
1769
1770         chroma_mc(s, tmp, tmp2, tmpstride, ref0->frame,
1771                   &current_mv.mv[0], x0/2, y0/2, nPbW/2, nPbH/2);
1772         chroma_mc(s, tmp3, tmp4, tmpstride, ref1->frame,
1773                   &current_mv.mv[1], x0/2, y0/2, nPbW/2, nPbH/2);
1774
1775         if ((s->sh.slice_type == P_SLICE && s->pps->weighted_pred_flag) ||
1776             (s->sh.slice_type == B_SLICE && s->pps->weighted_bipred_flag)) {
1777             s->hevcdsp.weighted_pred_avg(s->sh.chroma_log2_weight_denom ,
1778                                          s->sh.chroma_weight_l0[current_mv.ref_idx[0]][0],
1779                                          s->sh.chroma_weight_l1[current_mv.ref_idx[1]][0],
1780                                          s->sh.chroma_offset_l0[current_mv.ref_idx[0]][0],
1781                                          s->sh.chroma_offset_l1[current_mv.ref_idx[1]][0],
1782                                          dst1, s->frame->linesize[1], tmp, tmp3, tmpstride, nPbW/2, nPbH/2);
1783             s->hevcdsp.weighted_pred_avg(s->sh.chroma_log2_weight_denom ,
1784                                          s->sh.chroma_weight_l0[current_mv.ref_idx[0]][1],
1785                                          s->sh.chroma_weight_l1[current_mv.ref_idx[1]][1],
1786                                          s->sh.chroma_offset_l0[current_mv.ref_idx[0]][1],
1787                                          s->sh.chroma_offset_l1[current_mv.ref_idx[1]][1],
1788                                          dst2, s->frame->linesize[2], tmp2, tmp4, tmpstride, nPbW/2, nPbH/2);
1789         } else {
1790             s->hevcdsp.put_weighted_pred_avg(dst1, s->frame->linesize[1], tmp, tmp3, tmpstride, nPbW/2, nPbH/2);
1791             s->hevcdsp.put_weighted_pred_avg(dst2, s->frame->linesize[2], tmp2, tmp4, tmpstride, nPbW/2, nPbH/2);
1792         }
1793     }
1794 }
1795
1796 /**
1797  * 8.4.1
1798  */
1799 static int luma_intra_pred_mode(HEVCContext *s, int x0, int y0, int pu_size,
1800                                 int prev_intra_luma_pred_flag)
1801 {
1802     HEVCLocalContext *lc = &s->HEVClc;
1803     int x_pu = x0 >> s->sps->log2_min_pu_size;
1804     int y_pu = y0 >> s->sps->log2_min_pu_size;
1805     int pic_width_in_min_pu = s->sps->width >> s->sps->log2_min_pu_size;
1806     int size_in_pus = pu_size >> s->sps->log2_min_pu_size;
1807     int x0b = x0 & ((1 << s->sps->log2_ctb_size) - 1);
1808     int y0b = y0 & ((1 << s->sps->log2_ctb_size) - 1);
1809
1810     int cand_up   = (lc->ctb_up_flag || y0b) ? s->tab_ipm[(y_pu-1)*pic_width_in_min_pu+x_pu] : INTRA_DC ;
1811     int cand_left = (lc->ctb_left_flag || x0b) ? s->tab_ipm[y_pu*pic_width_in_min_pu+x_pu-1] : INTRA_DC ;
1812
1813     int y_ctb = (y0 >> (s->sps->log2_ctb_size)) << (s->sps->log2_ctb_size);
1814     MvField *tab_mvf = s->ref->tab_mvf;
1815     int intra_pred_mode;
1816     int candidate[3];
1817     int i, j;
1818
1819     // intra_pred_mode prediction does not cross vertical CTB boundaries
1820     if ((y0 - 1) < y_ctb)
1821         cand_up = INTRA_DC;
1822
1823     if (cand_left == cand_up) {
1824         if (cand_left < 2) {
1825             candidate[0] = INTRA_PLANAR;
1826             candidate[1] = INTRA_DC;
1827             candidate[2] = INTRA_ANGULAR_26;
1828         } else {
1829             candidate[0] = cand_left;
1830             candidate[1] = 2 + ((cand_left - 2 - 1 + 32) & 31);
1831             candidate[2] = 2 + ((cand_left - 2 + 1) & 31);
1832         }
1833     } else {
1834         candidate[0] = cand_left;
1835         candidate[1] = cand_up;
1836         if (candidate[0] != INTRA_PLANAR && candidate[1] != INTRA_PLANAR) {
1837             candidate[2] = INTRA_PLANAR;
1838         } else if (candidate[0] != INTRA_DC && candidate[1] != INTRA_DC) {
1839             candidate[2] = INTRA_DC;
1840         } else {
1841             candidate[2] = INTRA_ANGULAR_26;
1842         }
1843     }
1844
1845     if (prev_intra_luma_pred_flag) {
1846         intra_pred_mode = candidate[lc->pu.mpm_idx];
1847     } else {
1848         if (candidate[0] > candidate[1])
1849             FFSWAP(uint8_t, candidate[0], candidate[1]);
1850         if (candidate[0] > candidate[2])
1851             FFSWAP(uint8_t, candidate[0], candidate[2]);
1852         if (candidate[1] > candidate[2])
1853             FFSWAP(uint8_t, candidate[1], candidate[2]);
1854
1855         intra_pred_mode = lc->pu.rem_intra_luma_pred_mode;
1856         for (i = 0; i < 3; i++) {
1857             if (intra_pred_mode >= candidate[i])
1858                 intra_pred_mode++;
1859         }
1860     }
1861
1862     /* write the intra prediction units into the mv array */
1863     if(!size_in_pus)
1864         size_in_pus = 1;
1865     for (i = 0; i < size_in_pus; i++) {
1866         memset(&s->tab_ipm[(y_pu + i) * pic_width_in_min_pu + x_pu],
1867                intra_pred_mode, size_in_pus);
1868
1869         for (j = 0; j < size_in_pus; j++) {
1870             tab_mvf[(y_pu + j) * pic_width_in_min_pu + x_pu + i].is_intra     = 1;
1871             tab_mvf[(y_pu + j) * pic_width_in_min_pu + x_pu + i].pred_flag[0] = 0;
1872             tab_mvf[(y_pu + j) * pic_width_in_min_pu + x_pu + i].pred_flag[1] = 0;
1873             tab_mvf[(y_pu + j) * pic_width_in_min_pu + x_pu + i].ref_idx[0]   = 0;
1874             tab_mvf[(y_pu + j) * pic_width_in_min_pu + x_pu + i].ref_idx[1]   = 0;
1875             tab_mvf[(y_pu + j) * pic_width_in_min_pu + x_pu + i].mv[0].x      = 0;
1876             tab_mvf[(y_pu + j) * pic_width_in_min_pu + x_pu + i].mv[0].y      = 0;
1877             tab_mvf[(y_pu + j) * pic_width_in_min_pu + x_pu + i].mv[1].x      = 0;
1878             tab_mvf[(y_pu + j) * pic_width_in_min_pu + x_pu + i].mv[1].y      = 0;
1879         }
1880     }
1881
1882     return intra_pred_mode;
1883 }
1884
1885 static av_always_inline void set_ct_depth(HEVCContext *s, int x0, int y0,
1886                                           int log2_cb_size, int ct_depth)
1887 {
1888     int length = (1 << log2_cb_size) >> s->sps->log2_min_coding_block_size;
1889     int x_cb = x0 >> s->sps->log2_min_coding_block_size;
1890     int y_cb = y0 >> s->sps->log2_min_coding_block_size;
1891     int y;
1892
1893     for (y = 0; y < length; y++)
1894         memset(&s->tab_ct_depth[(y_cb + y) * s->sps->min_cb_width + x_cb],
1895                ct_depth, length);
1896 }
1897
1898 static void intra_prediction_unit(HEVCContext *s, int x0, int y0, int log2_cb_size)
1899 {
1900     HEVCLocalContext *lc = &s->HEVClc;
1901     static const uint8_t intra_chroma_table[4] = {0, 26, 10, 1};
1902     uint8_t prev_intra_luma_pred_flag[4];
1903     int split   = lc->cu.part_mode == PART_NxN;
1904     int pb_size = (1 << log2_cb_size) >> split;
1905     int side    = split + 1;
1906     int chroma_mode;
1907     int i, j;
1908
1909     for (i = 0; i < side; i++)
1910         for (j = 0; j < side; j++)
1911             prev_intra_luma_pred_flag[2 * i + j] = ff_hevc_prev_intra_luma_pred_flag_decode(s);
1912
1913     for (i = 0; i < side; i++) {
1914         for (j = 0; j < side; j++) {
1915             if (prev_intra_luma_pred_flag[2*i+j])
1916                 lc->pu.mpm_idx = ff_hevc_mpm_idx_decode(s);
1917             else
1918                 lc->pu.rem_intra_luma_pred_mode = ff_hevc_rem_intra_luma_pred_mode_decode(s);
1919
1920             lc->pu.intra_pred_mode[2 * i + j] =
1921                 luma_intra_pred_mode(s, x0 + pb_size * j, y0 + pb_size * i, pb_size,
1922                                      prev_intra_luma_pred_flag[2 * i + j]);
1923         }
1924     }
1925
1926     chroma_mode = ff_hevc_intra_chroma_pred_mode_decode(s);
1927     if (chroma_mode != 4) {
1928         if (lc->pu.intra_pred_mode[0] == intra_chroma_table[chroma_mode])
1929             lc->pu.intra_pred_mode_c = 34;
1930         else
1931             lc->pu.intra_pred_mode_c = intra_chroma_table[chroma_mode];
1932     } else {
1933         lc->pu.intra_pred_mode_c = lc->pu.intra_pred_mode[0];
1934     }
1935 }
1936
1937 static void intra_prediction_unit_default_value(HEVCContext *s, int x0, int y0, int log2_cb_size)
1938 {
1939     HEVCLocalContext *lc = &s->HEVClc;
1940     int pb_size = 1 << log2_cb_size;
1941     int size_in_pus = pb_size >> s->sps->log2_min_pu_size;
1942     int pic_width_in_min_pu = s->sps->width >> s->sps->log2_min_pu_size;
1943     MvField *tab_mvf = s->ref->tab_mvf;
1944     int x_pu = x0 >> s->sps->log2_min_pu_size;
1945     int y_pu = y0 >> s->sps->log2_min_pu_size;
1946     int j, k;
1947
1948     if (size_in_pus == 0)
1949         size_in_pus = 1;
1950     for (j = 0; j < size_in_pus; j++) {
1951         memset(&s->tab_ipm[(y_pu + j) * pic_width_in_min_pu + x_pu], INTRA_DC, size_in_pus);
1952         for (k = 0; k <size_in_pus; k++)
1953             tab_mvf[(y_pu + j) * pic_width_in_min_pu + x_pu + k].is_intra = lc->cu.pred_mode == MODE_INTRA;
1954     }
1955 }
1956
1957 static int hls_coding_unit(HEVCContext *s, int x0, int y0, int log2_cb_size)
1958 {
1959     int cb_size          = 1 << log2_cb_size;
1960     HEVCLocalContext *lc = &s->HEVClc;
1961     int log2_min_cb_size = s->sps->log2_min_coding_block_size;
1962     int length           = cb_size >> log2_min_cb_size;
1963     int pic_width_in_ctb = s->sps->width >> log2_min_cb_size;
1964     int x_cb             = x0 >> log2_min_cb_size;
1965     int y_cb             = y0 >> log2_min_cb_size;
1966     int x, y;
1967
1968     lc->cu.x = x0;
1969     lc->cu.y = y0;
1970     lc->cu.rqt_root_cbf = 1;
1971
1972     lc->cu.pred_mode        = MODE_INTRA;
1973     lc->cu.part_mode        = PART_2Nx2N;
1974     lc->cu.intra_split_flag = 0;
1975     lc->cu.pcm_flag         = 0;
1976     SAMPLE_CTB(s->skip_flag, x_cb, y_cb) = 0;
1977     for (x = 0; x < 4; x++)
1978         lc->pu.intra_pred_mode[x] = 1;
1979     if (s->pps->transquant_bypass_enable_flag) {
1980         lc->cu.cu_transquant_bypass_flag = ff_hevc_cu_transquant_bypass_flag_decode(s);
1981         if (lc->cu.cu_transquant_bypass_flag)
1982             set_deblocking_bypass(s, x0, y0, log2_cb_size);
1983     } else
1984         lc->cu.cu_transquant_bypass_flag = 0;
1985
1986
1987     if (s->sh.slice_type != I_SLICE) {
1988         uint8_t skip_flag = ff_hevc_skip_flag_decode(s, x0, y0, x_cb, y_cb);
1989
1990         lc->cu.pred_mode = MODE_SKIP;
1991         x = y_cb * pic_width_in_ctb + x_cb;
1992         for (y = 0; y < length; y++) {
1993             memset(&s->skip_flag[x], skip_flag, length);
1994             x += pic_width_in_ctb;
1995         }
1996         lc->cu.pred_mode = skip_flag ? MODE_SKIP : MODE_INTER;
1997     }
1998
1999     if (SAMPLE_CTB(s->skip_flag, x_cb, y_cb)) {
2000         hls_prediction_unit(s, x0, y0, cb_size, cb_size, log2_cb_size, 0);
2001         intra_prediction_unit_default_value(s, x0, y0, log2_cb_size);
2002
2003         if (!s->sh.disable_deblocking_filter_flag)
2004             ff_hevc_deblocking_boundary_strengths(s, x0, y0, log2_cb_size,
2005                                                   lc->slice_or_tiles_up_boundary,
2006                                                   lc->slice_or_tiles_left_boundary);
2007     } else {
2008         if (s->sh.slice_type != I_SLICE)
2009             lc->cu.pred_mode = ff_hevc_pred_mode_decode(s);
2010         if (lc->cu.pred_mode != MODE_INTRA ||
2011             log2_cb_size == s->sps->log2_min_coding_block_size) {
2012             lc->cu.part_mode = ff_hevc_part_mode_decode(s, log2_cb_size);
2013             lc->cu.intra_split_flag = lc->cu.part_mode == PART_NxN &&
2014                                       lc->cu.pred_mode == MODE_INTRA;
2015         }
2016
2017         if (lc->cu.pred_mode == MODE_INTRA) {
2018             if (lc->cu.part_mode == PART_2Nx2N && s->sps->pcm_enabled_flag &&
2019                 log2_cb_size >= s->sps->pcm.log2_min_pcm_cb_size &&
2020                 log2_cb_size <= s->sps->pcm.log2_max_pcm_cb_size) {
2021                 lc->cu.pcm_flag = ff_hevc_pcm_flag_decode(s);
2022             }
2023             if (lc->cu.pcm_flag) {
2024                 int ret;
2025                 intra_prediction_unit_default_value(s, x0, y0, log2_cb_size);
2026                 ret = hls_pcm_sample(s, x0, y0, log2_cb_size);
2027                 if(s->sps->pcm.loop_filter_disable_flag)
2028                     set_deblocking_bypass(s, x0, y0, log2_cb_size);
2029
2030                 if (ret < 0)
2031                     return ret;
2032             } else {
2033                 intra_prediction_unit(s, x0, y0, log2_cb_size);
2034             }
2035         } else {
2036             intra_prediction_unit_default_value(s, x0, y0, log2_cb_size);
2037             switch (lc->cu.part_mode) {
2038             case PART_2Nx2N:
2039                 hls_prediction_unit(s, x0, y0, cb_size, cb_size, log2_cb_size, 0);
2040                 break;
2041             case PART_2NxN:
2042                 hls_prediction_unit(s, x0, y0, cb_size, cb_size / 2, log2_cb_size, 0);
2043                 hls_prediction_unit(s, x0, y0 + cb_size / 2, cb_size, cb_size/2, log2_cb_size, 1);
2044                 break;
2045             case PART_Nx2N:
2046                 hls_prediction_unit(s, x0, y0, cb_size / 2, cb_size, log2_cb_size, 0);
2047                 hls_prediction_unit(s, x0 + cb_size / 2, y0, cb_size / 2, cb_size, log2_cb_size, 1);
2048                 break;
2049             case PART_2NxnU:
2050                 hls_prediction_unit(s, x0, y0, cb_size, cb_size / 4, log2_cb_size, 0);
2051                 hls_prediction_unit(s, x0, y0 + cb_size / 4, cb_size, cb_size * 3 / 4, log2_cb_size, 1);
2052                 break;
2053             case PART_2NxnD:
2054                 hls_prediction_unit(s, x0, y0, cb_size, cb_size * 3 / 4, log2_cb_size, 0);
2055                 hls_prediction_unit(s, x0, y0 + cb_size * 3 / 4, cb_size, cb_size / 4, log2_cb_size, 1);
2056                 break;
2057             case PART_nLx2N:
2058                 hls_prediction_unit(s, x0, y0, cb_size / 4, cb_size, log2_cb_size,0);
2059                 hls_prediction_unit(s, x0 + cb_size / 4, y0, cb_size * 3 / 4, cb_size, log2_cb_size, 1);
2060                 break;
2061             case PART_nRx2N:
2062                 hls_prediction_unit(s, x0, y0, cb_size * 3 / 4, cb_size, log2_cb_size,0);
2063                 hls_prediction_unit(s, x0 + cb_size * 3 / 4, y0, cb_size/4, cb_size, log2_cb_size, 1);
2064                 break;
2065             case PART_NxN:
2066                 hls_prediction_unit(s, x0, y0, cb_size / 2, cb_size / 2, log2_cb_size, 0);
2067                 hls_prediction_unit(s, x0 + cb_size / 2, y0, cb_size / 2, cb_size / 2, log2_cb_size, 1);
2068                 hls_prediction_unit(s, x0, y0 + cb_size / 2, cb_size / 2, cb_size / 2, log2_cb_size, 2);
2069                 hls_prediction_unit(s, x0 + cb_size / 2, y0 + cb_size / 2, cb_size / 2, cb_size / 2, log2_cb_size, 3);
2070                 break;
2071             }
2072         }
2073
2074         if (!lc->cu.pcm_flag) {
2075             if (lc->cu.pred_mode != MODE_INTRA &&
2076                 !(lc->cu.part_mode == PART_2Nx2N && lc->pu.merge_flag)) {
2077                 lc->cu.rqt_root_cbf = ff_hevc_no_residual_syntax_flag_decode(s);
2078             }
2079             if (lc->cu.rqt_root_cbf) {
2080                 lc->cu.max_trafo_depth = lc->cu.pred_mode == MODE_INTRA ?
2081                                         s->sps->max_transform_hierarchy_depth_intra + lc->cu.intra_split_flag :
2082                                         s->sps->max_transform_hierarchy_depth_inter;
2083                 hls_transform_tree(s, x0, y0, x0, y0, x0, y0, log2_cb_size,
2084                                    log2_cb_size, 0, 0);
2085             } else {
2086                 if (!s->sh.disable_deblocking_filter_flag)
2087                     ff_hevc_deblocking_boundary_strengths(s, x0, y0, log2_cb_size,
2088                                                           lc->slice_or_tiles_up_boundary,
2089                                                           lc->slice_or_tiles_left_boundary);
2090             }
2091         }
2092     }
2093
2094     if (s->pps->cu_qp_delta_enabled_flag && lc->tu.is_cu_qp_delta_coded == 0)
2095         ff_hevc_set_qPy(s, x0, y0, x0, y0, log2_cb_size);
2096
2097     x = y_cb * pic_width_in_ctb + x_cb;
2098     for (y = 0; y < length; y++) {
2099         memset(&s->qp_y_tab[x], lc->qp_y, length);
2100         x += pic_width_in_ctb;
2101     }
2102
2103     set_ct_depth(s, x0, y0, log2_cb_size, lc->ct.depth);
2104
2105     return 0;
2106 }
2107
2108 static int hls_coding_quadtree(HEVCContext *s, int x0, int y0, int log2_cb_size, int cb_depth)
2109 {
2110     HEVCLocalContext *lc = &s->HEVClc;
2111     int ret;
2112
2113     lc->ct.depth = cb_depth;
2114     if ((x0 + (1 << log2_cb_size) <= s->sps->width) &&
2115         (y0 + (1 << log2_cb_size) <= s->sps->height) &&
2116         log2_cb_size > s->sps->log2_min_coding_block_size) {
2117         SAMPLE(s->split_cu_flag, x0, y0) =
2118             ff_hevc_split_coding_unit_flag_decode(s, cb_depth, x0, y0);
2119     } else {
2120         SAMPLE(s->split_cu_flag, x0, y0) =
2121             (log2_cb_size > s->sps->log2_min_coding_block_size);
2122     }
2123     if (s->pps->cu_qp_delta_enabled_flag &&
2124         log2_cb_size >= s->sps->log2_ctb_size - s->pps->diff_cu_qp_delta_depth) {
2125         lc->tu.is_cu_qp_delta_coded = 0;
2126         lc->tu.cu_qp_delta          = 0;
2127     }
2128
2129     if (SAMPLE(s->split_cu_flag, x0, y0)) {
2130         int more_data = 0;
2131         int cb_size = (1 << (log2_cb_size)) >> 1;
2132         int x1 = x0 + cb_size;
2133         int y1 = y0 + cb_size;
2134
2135         more_data = hls_coding_quadtree(s, x0, y0, log2_cb_size - 1, cb_depth + 1);
2136         if (more_data < 0)
2137             return more_data;
2138
2139         if (more_data && x1 < s->sps->width)
2140             more_data = hls_coding_quadtree(s, x1, y0, log2_cb_size - 1, cb_depth + 1);
2141         if (more_data && y1 < s->sps->height)
2142             more_data = hls_coding_quadtree(s, x0, y1, log2_cb_size - 1, cb_depth + 1);
2143         if (more_data && x1 < s->sps->width &&
2144             y1 < s->sps->height) {
2145             return hls_coding_quadtree(s, x1, y1, log2_cb_size - 1, cb_depth + 1);
2146         }
2147         if (more_data)
2148             return ((x1 + cb_size) < s->sps->width ||
2149                     (y1 + cb_size) < s->sps->height);
2150         else
2151             return 0;
2152     } else {
2153         ret = hls_coding_unit(s, x0, y0, log2_cb_size);
2154         if (ret < 0)
2155             return ret;
2156         if ((!((x0 + (1 << log2_cb_size)) %
2157                (1 << (s->sps->log2_ctb_size))) ||
2158              (x0 + (1 << log2_cb_size) >= s->sps->width)) &&
2159             (!((y0 + (1 << log2_cb_size)) %
2160                (1 << (s->sps->log2_ctb_size))) ||
2161              (y0 + (1 << log2_cb_size) >= s->sps->height))) {
2162             int end_of_slice_flag = ff_hevc_end_of_slice_flag_decode(s);
2163             return !end_of_slice_flag;
2164         } else {
2165             return 1;
2166         }
2167     }
2168
2169     return 0;
2170 }
2171
2172 /**
2173  * 7.3.4
2174  */
2175
2176 static void hls_decode_neighbour(HEVCContext *s, int x_ctb, int y_ctb, int ctb_addr_ts)
2177 {
2178     HEVCLocalContext *lc  = &s->HEVClc;
2179     int ctb_size          = 1 << s->sps->log2_ctb_size;
2180     int ctb_addr_rs       = s->pps->ctb_addr_ts_to_rs[ctb_addr_ts];
2181     int ctb_addr_in_slice = ctb_addr_rs - s->sh.slice_addr;
2182
2183     int tile_left_boundary;
2184     int tile_up_boundary;
2185     int slice_left_boundary;
2186     int slice_up_boundary;
2187
2188     s->tab_slice_address[ctb_addr_rs] = s->sh.slice_addr;
2189
2190
2191     if (s->pps->entropy_coding_sync_enabled_flag) {
2192         if (x_ctb == 0 && (y_ctb & (ctb_size - 1)) == 0)
2193             lc->first_qp_group = 1;
2194         lc->end_of_tiles_x = s->sps->width;
2195     } else if (s->pps->tiles_enabled_flag) {
2196         if (ctb_addr_ts && s->pps->tile_id[ctb_addr_ts] != s->pps->tile_id[ctb_addr_ts - 1]) {
2197             int idxX = s->pps->col_idxX[x_ctb >> s->sps->log2_ctb_size];
2198             lc->start_of_tiles_x = x_ctb;
2199             lc->end_of_tiles_x   = x_ctb + (s->pps->column_width[idxX]<< s->sps->log2_ctb_size);
2200             lc->first_qp_group   = 1;
2201         }
2202     } else {
2203         lc->end_of_tiles_x = s->sps->width;
2204     }
2205
2206     lc->end_of_tiles_y = FFMIN(y_ctb + ctb_size, s->sps->height);
2207
2208     if (s->pps->tiles_enabled_flag) {
2209         tile_left_boundary = ((x_ctb > 0) &&
2210                               (s->pps->tile_id[ctb_addr_ts] == s->pps->tile_id[s->pps->ctb_addr_rs_to_ts[ctb_addr_rs-1]]));
2211         slice_left_boundary = ((x_ctb > 0) &&
2212                                (s->tab_slice_address[ctb_addr_rs] == s->tab_slice_address[ctb_addr_rs - 1]));
2213         tile_up_boundary = ((y_ctb > 0) &&
2214                             (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]]));
2215         slice_up_boundary = ((y_ctb > 0) &&
2216                              (s->tab_slice_address[ctb_addr_rs] == s->tab_slice_address[ctb_addr_rs - s->sps->ctb_width]));
2217     } else {
2218         tile_left_boundary =
2219         tile_up_boundary = 1;
2220         slice_left_boundary = ctb_addr_in_slice > 0;
2221         slice_up_boundary = ctb_addr_in_slice >= s->sps->ctb_width;
2222     }
2223     lc->slice_or_tiles_left_boundary = (!slice_left_boundary) + (!tile_left_boundary << 1);
2224     lc->slice_or_tiles_up_boundary   = (!slice_up_boundary + (!tile_up_boundary << 1));
2225     lc->ctb_left_flag = ((x_ctb > 0) && (ctb_addr_in_slice > 0) && tile_left_boundary);
2226     lc->ctb_up_flag   = ((y_ctb > 0) && (ctb_addr_in_slice >= s->sps->ctb_width) && tile_up_boundary);
2227     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]]));
2228     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]]));
2229 }
2230
2231 static int hls_slice_data(HEVCContext *s)
2232 {
2233     int ctb_size    = 1 << s->sps->log2_ctb_size;
2234     int more_data   = 1;
2235     int x_ctb       = 0;
2236     int y_ctb       = 0;
2237     int ctb_addr_ts = s->pps->ctb_addr_rs_to_ts[s->sh.slice_ctb_addr_rs];
2238
2239     while (more_data && ctb_addr_ts < s->sps->ctb_size) {
2240         int ctb_addr_rs = s->pps->ctb_addr_ts_to_rs[ctb_addr_ts];
2241
2242         x_ctb = (ctb_addr_rs % ((s->sps->width + (ctb_size - 1)) >> s->sps->log2_ctb_size)) << s->sps->log2_ctb_size;
2243         y_ctb = (ctb_addr_rs / ((s->sps->width + (ctb_size - 1)) >> s->sps->log2_ctb_size)) << s->sps->log2_ctb_size;
2244         hls_decode_neighbour(s, x_ctb, y_ctb, ctb_addr_ts);
2245
2246         ff_hevc_cabac_init(s, ctb_addr_ts);
2247
2248         hls_sao_param(s, x_ctb >> s->sps->log2_ctb_size, y_ctb >> s->sps->log2_ctb_size);
2249
2250         s->deblock[ctb_addr_rs].disable     = s->sh.disable_deblocking_filter_flag;
2251         s->deblock[ctb_addr_rs].beta_offset = s->sh.beta_offset;
2252         s->deblock[ctb_addr_rs].tc_offset   = s->sh.tc_offset;
2253         s->filter_slice_edges[ctb_addr_rs]  = s->sh.slice_loop_filter_across_slices_enabled_flag;
2254
2255         more_data = hls_coding_quadtree(s, x_ctb, y_ctb, s->sps->log2_ctb_size, 0);
2256         if (more_data < 0)
2257             return more_data;
2258
2259         ctb_addr_ts++;
2260         ff_hevc_save_states(s, ctb_addr_ts);
2261         ff_hevc_hls_filters(s, x_ctb, y_ctb, ctb_size);
2262     }
2263
2264     if (x_ctb + ctb_size >= s->sps->width &&
2265         y_ctb + ctb_size >= s->sps->height)
2266         ff_hevc_hls_filter(s, x_ctb, y_ctb);
2267
2268     return ctb_addr_ts;
2269 }
2270
2271 /**
2272  * @return AVERROR_INVALIDDATA if the packet is not a valid NAL unit,
2273  * 0 if the unit should be skipped, 1 otherwise
2274  */
2275 static int hls_nal_unit(HEVCContext *s)
2276 {
2277     GetBitContext *gb = &s->HEVClc.gb;
2278     int nuh_layer_id;
2279
2280     if (get_bits1(gb) != 0)
2281         return AVERROR_INVALIDDATA;
2282
2283     s->nal_unit_type = get_bits(gb, 6);
2284
2285     nuh_layer_id   = get_bits(gb, 6);
2286     s->temporal_id = get_bits(gb, 3) - 1;
2287     if (s->temporal_id < 0)
2288         return AVERROR_INVALIDDATA;
2289
2290     av_log(s->avctx, AV_LOG_DEBUG,
2291            "nal_unit_type: %d, nuh_layer_id: %dtemporal_id: %d\n",
2292            s->nal_unit_type, nuh_layer_id, s->temporal_id);
2293
2294     return (nuh_layer_id == 0);
2295 }
2296
2297 static void restore_tqb_pixels(HEVCContext *s)
2298 {
2299     int pic_width_in_min_pu  = s->sps->width >> s->sps->log2_min_pu_size;
2300     int pic_height_in_min_pu = s->sps->height >> s->sps->log2_min_pu_size;
2301     int min_pu_size = 1 << s->sps->log2_min_pu_size;
2302     int x, y, c_idx;
2303
2304     for (c_idx = 0; c_idx < 3; c_idx++) {
2305         ptrdiff_t stride = s->frame->linesize[c_idx];
2306         int hshift = s->sps->hshift[c_idx];
2307         int vshift = s->sps->vshift[c_idx];
2308         for (y = 0; y < pic_height_in_min_pu; y++) {
2309             for (x = 0; x < pic_width_in_min_pu; x++) {
2310                 if (s->is_pcm[y*pic_width_in_min_pu+x]) {
2311                     int n;
2312                     int len = min_pu_size >> hshift;
2313                     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)];
2314                     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)];
2315                     for (n = 0;n < (min_pu_size >> vshift); n++) {
2316                        memcpy(dst,src,len);
2317                        src += stride;
2318                        dst += stride;
2319                     }
2320                 }
2321             }
2322         }
2323     }
2324 }
2325
2326 static int hevc_frame_start(HEVCContext *s)
2327 {
2328     HEVCLocalContext *lc     = &s->HEVClc;
2329     int pic_width_in_min_pu  = s->sps->width  >> s->sps->log2_min_pu_size;
2330     int pic_height_in_min_pu = s->sps->height >> s->sps->log2_min_pu_size;
2331     int pic_width_in_min_tu  = s->sps->width  >> s->sps->log2_min_transform_block_size;
2332     int pic_height_in_min_tu = s->sps->height >> s->sps->log2_min_transform_block_size;
2333     int ret;
2334
2335     memset(s->horizontal_bs, 0, 2 * s->bs_width * (s->bs_height + 1));
2336     memset(s->vertical_bs,   0, 2 * s->bs_width * (s->bs_height + 1));
2337     memset(s->cbf_luma,      0, pic_width_in_min_tu * pic_height_in_min_tu);
2338     memset(s->is_pcm,        0, pic_width_in_min_pu * pic_height_in_min_pu);
2339
2340     lc->start_of_tiles_x = 0;
2341     s->is_decoded        = 0;
2342
2343     if (s->pps->tiles_enabled_flag)
2344         lc->end_of_tiles_x   = s->pps->column_width[0] << s->sps->log2_ctb_size;
2345
2346     ret = ff_hevc_set_new_ref(s, s->sps->sao_enabled ? &s->sao_frame : &s->frame,
2347                               s->poc);
2348     if (ret < 0)
2349         goto fail;
2350
2351     av_fast_malloc(&lc->edge_emu_buffer, &lc->edge_emu_buffer_size,
2352                    (MAX_PB_SIZE + 7) * s->ref->frame->linesize[0]);
2353     if (!lc->edge_emu_buffer) {
2354         ret = AVERROR(ENOMEM);
2355         goto fail;
2356     }
2357
2358     ret = ff_hevc_frame_rps(s);
2359     if (ret < 0) {
2360         av_log(s->avctx, AV_LOG_ERROR, "Error constructing the frame RPS.\n");
2361         goto fail;
2362     }
2363
2364     av_frame_unref(s->output_frame);
2365     ret = ff_hevc_output_frame(s, s->output_frame, 0);
2366     if (ret < 0)
2367         goto fail;
2368
2369     ff_thread_finish_setup(s->avctx);
2370
2371     return 0;
2372 fail:
2373     if (s->ref)
2374         ff_thread_report_progress(&s->ref->tf, INT_MAX, 0);
2375     s->ref = NULL;
2376     return ret;
2377 }
2378
2379 static int decode_nal_unit(HEVCContext *s, const uint8_t *nal, int length)
2380 {
2381     HEVCLocalContext *lc = &s->HEVClc;
2382     GetBitContext *gb = &lc->gb;
2383     int ctb_addr_ts;
2384     int ret;
2385
2386     ret = init_get_bits8(gb, nal, length);
2387     if (ret < 0)
2388         return ret;
2389
2390     ret = hls_nal_unit(s);
2391     if (ret < 0) {
2392         av_log(s->avctx, AV_LOG_ERROR, "Invalid NAL unit %d, skipping.\n",
2393                 s->nal_unit_type);
2394         if (s->avctx->err_recognition & AV_EF_EXPLODE)
2395             return ret;
2396         return 0;
2397     } else if (!ret)
2398         return 0;
2399
2400     switch (s->nal_unit_type) {
2401     case NAL_VPS:
2402         ret = ff_hevc_decode_nal_vps(s);
2403         if (ret < 0)
2404             return ret;
2405         break;
2406     case NAL_SPS:
2407         ret = ff_hevc_decode_nal_sps(s);
2408         if (ret < 0)
2409             return ret;
2410         break;
2411     case NAL_PPS:
2412         ret = ff_hevc_decode_nal_pps(s);
2413         if (ret < 0)
2414             return ret;
2415         break;
2416     case NAL_SEI_PREFIX:
2417     case NAL_SEI_SUFFIX:
2418         ret = ff_hevc_decode_nal_sei(s);
2419         if (ret < 0)
2420             return ret;
2421         break;
2422     case NAL_TRAIL_R:
2423     case NAL_TRAIL_N:
2424     case NAL_TSA_N:
2425     case NAL_TSA_R:
2426     case NAL_STSA_N:
2427     case NAL_STSA_R:
2428     case NAL_BLA_W_LP:
2429     case NAL_BLA_W_RADL:
2430     case NAL_BLA_N_LP:
2431     case NAL_IDR_W_RADL:
2432     case NAL_IDR_N_LP:
2433     case NAL_CRA_NUT:
2434     case NAL_RADL_N:
2435     case NAL_RADL_R:
2436     case NAL_RASL_N:
2437     case NAL_RASL_R:
2438         ret = hls_slice_header(s);
2439         if (ret < 0)
2440             return ret;
2441
2442         if (s->max_ra == INT_MAX) {
2443             if (s->nal_unit_type == NAL_CRA_NUT  ||
2444                 s->nal_unit_type == NAL_BLA_W_LP ||
2445                 s->nal_unit_type == NAL_BLA_N_LP ||
2446                 s->nal_unit_type == NAL_BLA_N_LP) {
2447                 s->max_ra = s->poc;
2448             } else {
2449                 if (IS_IDR(s))
2450                     s->max_ra = INT_MIN;
2451             }
2452         }
2453
2454         if ((s->nal_unit_type == NAL_RASL_R || s->nal_unit_type == NAL_RASL_N) &&
2455             s->poc <= s->max_ra) {
2456             s->is_decoded = 0;
2457             break;
2458         } else {
2459             if (s->nal_unit_type == NAL_RASL_R && s->poc > s->max_ra)
2460                 s->max_ra = INT_MIN;
2461         }
2462
2463         if (s->sh.first_slice_in_pic_flag) {
2464             ret = hevc_frame_start(s);
2465             if (ret < 0)
2466                 return ret;
2467         } else if (!s->ref) {
2468             av_log(s->avctx, AV_LOG_ERROR, "First slice in a frame missing.\n");
2469             return AVERROR_INVALIDDATA;
2470         }
2471
2472         if (!s->sh.dependent_slice_segment_flag &&
2473             s->sh.slice_type != I_SLICE) {
2474             ret = ff_hevc_slice_rpl(s);
2475             if (ret < 0) {
2476                 av_log(s->avctx, AV_LOG_WARNING, "Error constructing the reference "
2477                        "lists for the current slice.\n");
2478                 if (s->avctx->err_recognition & AV_EF_EXPLODE)
2479                     return ret;
2480             }
2481         }
2482
2483         ctb_addr_ts = hls_slice_data(s);
2484         if (ctb_addr_ts >= (s->sps->ctb_width * s->sps->ctb_height)) {
2485             s->is_decoded = 1;
2486             if ((s->pps->transquant_bypass_enable_flag ||
2487                  (s->sps->pcm.loop_filter_disable_flag && s->sps->pcm_enabled_flag)) &&
2488                 s->sps->sao_enabled)
2489                 restore_tqb_pixels(s);
2490         }
2491
2492         if (ctb_addr_ts < 0)
2493             return ctb_addr_ts;
2494         break;
2495     case NAL_EOS_NUT:
2496     case NAL_EOB_NUT:
2497         s->seq_decode = (s->seq_decode + 1) & 0xff;
2498         s->max_ra     = INT_MAX;
2499         break;
2500     case NAL_AUD:
2501     case NAL_FD_NUT:
2502         break;
2503     default:
2504         av_log(s->avctx, AV_LOG_INFO, "Skipping NAL unit %d\n", s->nal_unit_type);
2505     }
2506
2507     return 0;
2508 }
2509
2510 /* FIXME: This is adapted from ff_h264_decode_nal, avoiding duplication
2511    between these functions would be nice. */
2512 static int extract_rbsp(const uint8_t *src, int length,
2513                         HEVCNAL *nal)
2514 {
2515     int i, si, di;
2516     uint8_t *dst;
2517
2518 #define STARTCODE_TEST                                                  \
2519         if (i + 2 < length && src[i + 1] == 0 && src[i + 2] <= 3) {     \
2520             if (src[i + 2] != 3) {                                      \
2521                 /* startcode, so we must be past the end */             \
2522                 length = i;                                             \
2523             }                                                           \
2524             break;                                                      \
2525         }
2526 #if HAVE_FAST_UNALIGNED
2527 #define FIND_FIRST_ZERO                                                 \
2528         if (i > 0 && !src[i])                                           \
2529             i--;                                                        \
2530         while (src[i])                                                  \
2531             i++
2532 #if HAVE_FAST_64BIT
2533     for (i = 0; i + 1 < length; i += 9) {
2534         if (!((~AV_RN64A(src + i) &
2535                (AV_RN64A(src + i) - 0x0100010001000101ULL)) &
2536               0x8000800080008080ULL))
2537             continue;
2538         FIND_FIRST_ZERO;
2539         STARTCODE_TEST;
2540         i -= 7;
2541     }
2542 #else
2543     for (i = 0; i + 1 < length; i += 5) {
2544         if (!((~AV_RN32A(src + i) &
2545                (AV_RN32A(src + i) - 0x01000101U)) &
2546               0x80008080U))
2547             continue;
2548         FIND_FIRST_ZERO;
2549         STARTCODE_TEST;
2550         i -= 3;
2551     }
2552 #endif
2553 #else
2554     for (i = 0; i + 1 < length; i += 2) {
2555         if (src[i])
2556             continue;
2557         if (i > 0 && src[i - 1] == 0)
2558             i--;
2559         STARTCODE_TEST;
2560     }
2561 #endif
2562
2563     if (i >= length - 1) { // no escaped 0
2564         nal->data = src;
2565         nal->size = length;
2566         return length;
2567     }
2568
2569     av_fast_malloc(&nal->rbsp_buffer, &nal->rbsp_buffer_size,
2570                    length + FF_INPUT_BUFFER_PADDING_SIZE);
2571     if (!nal->rbsp_buffer)
2572         return AVERROR(ENOMEM);
2573
2574     dst = nal->rbsp_buffer;
2575
2576     memcpy(dst, src, i);
2577     si = di = i;
2578     while (si + 2 < length) {
2579         // remove escapes (very rare 1:2^22)
2580         if (src[si + 2] > 3) {
2581             dst[di++] = src[si++];
2582             dst[di++] = src[si++];
2583         } else if (src[si] == 0 && src[si + 1] == 0) {
2584             if (src[si + 2] == 3) { // escape
2585                 dst[di++]  = 0;
2586                 dst[di++]  = 0;
2587                 si        += 3;
2588
2589                 continue;
2590             } else // next start code
2591                 goto nsc;
2592         }
2593
2594         dst[di++] = src[si++];
2595     }
2596     while (si < length)
2597         dst[di++] = src[si++];
2598 nsc:
2599
2600     memset(dst + di, 0, FF_INPUT_BUFFER_PADDING_SIZE);
2601
2602     nal->data = dst;
2603     nal->size = di;
2604     return si;
2605 }
2606
2607 static int decode_nal_units(HEVCContext *s, const uint8_t *buf, int length)
2608 {
2609     int i, consumed, ret = 0;
2610
2611     s->ref = NULL;
2612     s->eos = 0;
2613
2614     /* split the input packet into NAL units, so we know the upper bound on the
2615      * number of slices in the frame */
2616     s->nb_nals = 0;
2617     while (length >= 4) {
2618         HEVCNAL *nal;
2619         int extract_length = 0;
2620
2621         if (s->disable_au == 0) {
2622             if (s->is_nalff) {
2623                 int i;
2624                 for (i = 0; i < s->nal_length_size; i++)
2625                     extract_length = (extract_length << 8) | buf[i];
2626                 buf    += s->nal_length_size;
2627                 length -= s->nal_length_size;
2628
2629                 if (extract_length > length) {
2630                     av_log(s->avctx, AV_LOG_ERROR, "Invalid NAL unit size.\n");
2631                     ret = AVERROR_INVALIDDATA;
2632                     goto fail;
2633                 }
2634             } else {
2635                 if (buf[2] == 0) {
2636                     length--;
2637                     buf++;
2638                     continue;
2639                 }
2640                 if (buf[0] != 0 || buf[1] != 0 || buf[2] != 1) {
2641                     ret = AVERROR_INVALIDDATA;
2642                     goto fail;
2643                 }
2644
2645                 buf    += 3;
2646                 length -= 3;
2647             }
2648         }
2649         if (!s->is_nalff || s->disable_au)
2650             extract_length = length;
2651
2652         if (s->nals_allocated < s->nb_nals + 1) {
2653             int new_size = s->nals_allocated + 1;
2654             HEVCNAL *tmp = av_realloc_array(s->nals, new_size, sizeof(*tmp));
2655             if (!tmp) {
2656                 ret = AVERROR(ENOMEM);
2657                 goto fail;
2658             }
2659             s->nals = tmp;
2660             memset(s->nals + s->nals_allocated, 0, (new_size - s->nals_allocated) * sizeof(*tmp));
2661             s->nals_allocated = new_size;
2662         }
2663         nal = &s->nals[s->nb_nals++];
2664
2665         consumed = extract_rbsp(buf, extract_length, nal);
2666         if (consumed < 0) {
2667             ret = consumed;
2668             goto fail;
2669         }
2670
2671         ret = init_get_bits8(&s->HEVClc.gb, nal->data, nal->size);
2672         if (ret < 0)
2673             goto fail;
2674         hls_nal_unit(s);
2675
2676         if (s->nal_unit_type == NAL_EOS_NUT || s->nal_unit_type == NAL_EOS_NUT)
2677             s->eos = 1;
2678
2679         buf    += consumed;
2680         length -= consumed;
2681     }
2682
2683     /* parse the NAL units */
2684     for (i = 0; i < s->nb_nals; i++) {
2685         int ret = decode_nal_unit(s, s->nals[i].data, s->nals[i].size);
2686         if (ret < 0) {
2687             av_log(s->avctx, AV_LOG_WARNING, "Error parsing NAL unit #%d.\n", i);
2688             if (s->avctx->err_recognition & AV_EF_EXPLODE)
2689                 goto fail;
2690         }
2691     }
2692
2693 fail:
2694     if (s->ref)
2695         ff_thread_report_progress(&s->ref->tf, INT_MAX, 0);
2696
2697     return ret;
2698 }
2699
2700 static void print_md5(void *log_ctx, int level,  uint8_t md5[16])
2701 {
2702     int i;
2703     for (i = 0; i < 16; i++)
2704         av_log(log_ctx, level, "%02"PRIx8, md5[i]);
2705 }
2706
2707 static int verify_md5(HEVCContext *s, AVFrame *frame)
2708 {
2709     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(frame->format);
2710     int pixel_shift = desc->comp[0].depth_minus1 > 7;
2711     int i, j;
2712
2713     if (!desc)
2714         return AVERROR(EINVAL);
2715
2716     av_log(s->avctx, AV_LOG_DEBUG, "Verifying checksum for frame with POC %d: ",
2717            s->poc);
2718
2719     /* the checksums are LE, so we have to byteswap for >8bpp formats
2720      * on BE arches */
2721 #if HAVE_BIGENDIAN
2722     if (pixel_shift && !s->checksum_buf) {
2723         av_fast_malloc(&s->checksum_buf, &s->checksum_buf_size,
2724                        FFMAX3(frame->linesize[0], frame->linesize[1],
2725                               frame->linesize[2]));
2726         if (!s->checksum_buf)
2727             return AVERROR(ENOMEM);
2728     }
2729 #endif
2730
2731     for (i = 0; frame->data[i]; i++) {
2732         int width  = s->avctx->coded_width;
2733         int height = s->avctx->coded_height;
2734         int w = (i == 1 || i == 2) ? (width  >> desc->log2_chroma_w) : width;
2735         int h = (i == 1 || i == 2) ? (height >> desc->log2_chroma_h) : height;
2736         uint8_t md5[16];
2737
2738         av_md5_init(s->md5_ctx);
2739         for (j = 0; j < h; j++) {
2740             const uint8_t *src = frame->data[i] + j * frame->linesize[i];
2741 #if HAVE_BIGENDIAN
2742             if (pixel_shift) {
2743                 s->dsp.bswap16_buf((uint16_t*)s->checksum_buf,
2744                                    (const uint16_t*)src, w);
2745                 src = s->checksum_buf;
2746             }
2747 #endif
2748             av_md5_update(s->md5_ctx, src, w << pixel_shift);
2749         }
2750         av_md5_final(s->md5_ctx, md5);
2751
2752         if (!memcmp(md5, s->md5[i], 16)) {
2753             av_log   (s->avctx, AV_LOG_DEBUG, "plane %d - correct ", i);
2754             print_md5(s->avctx, AV_LOG_DEBUG, md5);
2755             av_log   (s->avctx, AV_LOG_DEBUG, "; ");
2756         } else {
2757             av_log   (s->avctx, AV_LOG_ERROR, "mismatching checksum of plane %d - ", i);
2758             print_md5(s->avctx, AV_LOG_ERROR, md5);
2759             av_log   (s->avctx, AV_LOG_ERROR, " != ");
2760             print_md5(s->avctx, AV_LOG_ERROR, s->md5[i]);
2761             av_log   (s->avctx, AV_LOG_ERROR, "\n");
2762             return AVERROR_INVALIDDATA;
2763         }
2764     }
2765
2766     av_log(s->avctx, AV_LOG_DEBUG, "\n");
2767
2768     return 0;
2769 }
2770
2771 static int hevc_decode_frame(AVCodecContext *avctx, void *data, int *got_output,
2772                              AVPacket *avpkt)
2773 {
2774     int ret;
2775     HEVCContext *s = avctx->priv_data;
2776
2777     //av_log(avctx, AV_LOG_WARNING, "decode size %d\n", avpkt->size);
2778
2779     if (!avpkt->size) {
2780         ret = ff_hevc_output_frame(s, data, 1);
2781         if (ret < 0)
2782             return ret;
2783
2784         *got_output = ret;
2785         return 0;
2786     }
2787
2788     s->ref = NULL;
2789     ret = decode_nal_units(s, avpkt->data, avpkt->size);
2790     if (ret < 0)
2791         return ret;
2792
2793     /* verify the SEI checksum */
2794     if (avctx->err_recognition & AV_EF_CRCCHECK && s->is_decoded &&
2795         s->is_md5) {
2796         ret = verify_md5(s, s->ref->frame);
2797         if (ret < 0 && avctx->err_recognition & AV_EF_EXPLODE) {
2798             ff_hevc_unref_frame(s, s->ref, ~0);
2799             return ret;
2800         }
2801     }
2802     s->is_md5 = 0;
2803
2804     if (s->is_decoded) {
2805         av_log(avctx, AV_LOG_DEBUG, "Decoded frame with POC %d.\n", s->poc);
2806         s->is_decoded = 0;
2807     }
2808
2809     if (s->output_frame->buf[0]) {
2810         av_frame_move_ref(data, s->output_frame);
2811         *got_output = 1;
2812     }
2813
2814     return avpkt->size;
2815 }
2816
2817 static int hevc_ref_frame(HEVCContext *s, HEVCFrame *dst, HEVCFrame *src)
2818 {
2819     int ret;
2820
2821     ret = ff_thread_ref_frame(&dst->tf, &src->tf);
2822     if (ret < 0)
2823         return ret;
2824
2825     dst->tab_mvf_buf = av_buffer_ref(src->tab_mvf_buf);
2826     if (!dst->tab_mvf_buf)
2827         goto fail;
2828     dst->tab_mvf = src->tab_mvf;
2829
2830     dst->rpl_tab_buf = av_buffer_ref(src->rpl_tab_buf);
2831     if (!dst->rpl_tab_buf)
2832         goto fail;
2833     dst->rpl_tab = src->rpl_tab;
2834
2835     dst->rpl_buf = av_buffer_ref(src->rpl_buf);
2836     if (!dst->rpl_buf)
2837         goto fail;
2838
2839     dst->poc        = src->poc;
2840     dst->ctb_count  = src->ctb_count;
2841     dst->window     = src->window;
2842     dst->flags      = src->flags;
2843     dst->sequence   = src->sequence;
2844
2845     return 0;
2846 fail:
2847     ff_hevc_unref_frame(s, dst, ~0);
2848     return AVERROR(ENOMEM);
2849 }
2850
2851 static av_cold int hevc_decode_free(AVCodecContext *avctx)
2852 {
2853     HEVCContext       *s = avctx->priv_data;
2854     HEVCLocalContext *lc = &s->HEVClc;
2855     int i;
2856
2857     pic_arrays_free(s);
2858
2859     av_freep(&lc->edge_emu_buffer);
2860     av_freep(&s->md5_ctx);
2861
2862     av_frame_free(&s->tmp_frame);
2863     av_frame_free(&s->output_frame);
2864
2865     for (i = 0; i < FF_ARRAY_ELEMS(s->DPB); i++) {
2866         ff_hevc_unref_frame(s, &s->DPB[i], ~0);
2867         av_frame_free(&s->DPB[i].frame);
2868     }
2869
2870     for (i = 0; i < FF_ARRAY_ELEMS(s->vps_list); i++)
2871         av_freep(&s->vps_list[i]);
2872     for (i = 0; i < FF_ARRAY_ELEMS(s->sps_list); i++)
2873         av_buffer_unref(&s->sps_list[i]);
2874     for (i = 0; i < FF_ARRAY_ELEMS(s->pps_list); i++)
2875         av_buffer_unref(&s->pps_list[i]);
2876
2877     for (i = 0; i < s->nals_allocated; i++)
2878         av_freep(&s->nals[i].rbsp_buffer);
2879     av_freep(&s->nals);
2880     s->nals_allocated = 0;
2881
2882     return 0;
2883 }
2884
2885 static av_cold int hevc_init_context(AVCodecContext *avctx)
2886 {
2887     HEVCContext *s = avctx->priv_data;
2888     int i;
2889
2890     s->avctx = avctx;
2891
2892     s->tmp_frame = av_frame_alloc();
2893     if (!s->tmp_frame)
2894         goto fail;
2895
2896     s->output_frame = av_frame_alloc();
2897     if (!s->output_frame)
2898         goto fail;
2899
2900     for (i = 0; i < FF_ARRAY_ELEMS(s->DPB); i++) {
2901         s->DPB[i].frame = av_frame_alloc();
2902         if (!s->DPB[i].frame)
2903             goto fail;
2904         s->DPB[i].tf.f = s->DPB[i].frame;
2905     }
2906
2907     s->max_ra = INT_MAX;
2908
2909     s->md5_ctx = av_md5_alloc();
2910     if (!s->md5_ctx)
2911         goto fail;
2912
2913     ff_dsputil_init(&s->dsp, avctx);
2914
2915     s->context_initialized = 1;
2916
2917     return 0;
2918 fail:
2919     hevc_decode_free(avctx);
2920     return AVERROR(ENOMEM);
2921 }
2922
2923 static int hevc_update_thread_context(AVCodecContext *dst,
2924                                       const AVCodecContext *src)
2925 {
2926     HEVCContext *s  = dst->priv_data;
2927     HEVCContext *s0 = src->priv_data;
2928     int i, ret;
2929
2930     if (!s->context_initialized) {
2931         ret = hevc_init_context(dst);
2932         if (ret < 0)
2933             return ret;
2934     }
2935
2936     for (i = 0; i < FF_ARRAY_ELEMS(s->DPB); i++) {
2937         ff_hevc_unref_frame(s, &s->DPB[i], ~0);
2938         if (s0->DPB[i].frame->buf[0]) {
2939             ret = hevc_ref_frame(s, &s->DPB[i], &s0->DPB[i]);
2940             if (ret < 0)
2941                 return ret;
2942         }
2943     }
2944
2945     for (i = 0; i < FF_ARRAY_ELEMS(s->sps_list); i++) {
2946         av_buffer_unref(&s->sps_list[i]);
2947         if (s0->sps_list[i]) {
2948             s->sps_list[i] = av_buffer_ref(s0->sps_list[i]);
2949             if (!s->sps_list[i])
2950                 return AVERROR(ENOMEM);
2951         }
2952     }
2953
2954     for (i = 0; i < FF_ARRAY_ELEMS(s->pps_list); i++) {
2955         av_buffer_unref(&s->pps_list[i]);
2956         if (s0->pps_list[i]) {
2957             s->pps_list[i] = av_buffer_ref(s0->pps_list[i]);
2958             if (!s->pps_list[i])
2959                 return AVERROR(ENOMEM);
2960         }
2961     }
2962
2963     s->seq_decode = s0->seq_decode;
2964     s->seq_output = s0->seq_output;
2965     s->pocTid0    = s0->pocTid0;
2966     s->max_ra     = s0->max_ra;
2967
2968     s->is_nalff        = s0->is_nalff;
2969     s->nal_length_size = s0->nal_length_size;
2970
2971
2972     if (s0->eos) {
2973         s->seq_decode = (s->seq_decode + 1) & 0xff;
2974         s->max_ra = INT_MAX;
2975     }
2976
2977     return 0;
2978 }
2979
2980 static int hevc_decode_extradata(HEVCContext *s)
2981 {
2982     AVCodecContext *avctx = s->avctx;
2983     GetByteContext gb;
2984     int ret;
2985
2986     bytestream2_init(&gb, avctx->extradata, avctx->extradata_size);
2987
2988     if (avctx->extradata_size > 3 &&
2989         (avctx->extradata[0] || avctx->extradata[1] ||
2990          avctx->extradata[2] > 1)) {
2991         /* It seems the extradata is encoded as hvcC format.
2992          * Temporarily, we support configurationVersion==0 until 14496-15 3rd finalized.
2993          * When finalized, configurationVersion will be 1 and we can recognize hvcC by
2994          * checking if avctx->extradata[0]==1 or not. */
2995         int i, j, num_arrays;
2996         int nal_len_size;
2997
2998         s->is_nalff = 1;
2999
3000         bytestream2_skip(&gb, 21);
3001         nal_len_size = (bytestream2_get_byte(&gb) & 3) + 1;
3002         num_arrays   = bytestream2_get_byte(&gb);
3003
3004         /* nal units in the hvcC always have length coded with 2 bytes,
3005          * so put a fake nal_length_size = 2 while parsing them */
3006         s->nal_length_size = 2;
3007
3008         /* Decode nal units from hvcC. */
3009         for (i = 0; i < num_arrays; i++) {
3010             int type = bytestream2_get_byte(&gb) & 0x3f;
3011             int cnt  = bytestream2_get_be16(&gb);
3012
3013             for (j = 0; j < cnt; j++) {
3014                 // +2 for the nal size field
3015                 int nalsize = bytestream2_peek_be16(&gb) + 2;
3016                 if (bytestream2_get_bytes_left(&gb) < nalsize) {
3017                     av_log(s->avctx, AV_LOG_ERROR, "Invalid NAL unit size in extradata.\n");
3018                     return AVERROR_INVALIDDATA;
3019                 }
3020
3021                 ret = decode_nal_units(s, gb.buffer, nalsize);
3022                 if (ret < 0) {
3023                     av_log(avctx, AV_LOG_ERROR,
3024                            "Decoding nal unit %d %d from hvcC failed\n", type, i);
3025                     return ret;
3026                 }
3027                 bytestream2_skip(&gb, nalsize);
3028             }
3029         }
3030
3031         /* Now store right nal length size, that will be used to parse all other nals */
3032         s->nal_length_size = nal_len_size;
3033     } else {
3034         s->is_nalff = 0;
3035         ret = decode_nal_units(s, avctx->extradata, avctx->extradata_size);
3036         if (ret < 0)
3037             return ret;
3038     }
3039     return 0;
3040 }
3041
3042 static av_cold int hevc_decode_init(AVCodecContext *avctx)
3043 {
3044     HEVCContext *s = avctx->priv_data;
3045     int ret;
3046
3047     ff_init_cabac_states();
3048
3049     avctx->internal->allocate_progress = 1;
3050
3051     ret = hevc_init_context(avctx);
3052     if (ret < 0)
3053         return ret;
3054
3055     if (avctx->extradata_size > 0 && avctx->extradata) {
3056         ret = hevc_decode_extradata(s);
3057         if (ret < 0) {
3058             hevc_decode_free(avctx);
3059             return ret;
3060         }
3061     }
3062
3063     return 0;
3064 }
3065
3066 static av_cold int hevc_init_thread_copy(AVCodecContext *avctx)
3067 {
3068     HEVCContext *s = avctx->priv_data;
3069     int ret;
3070
3071     memset(s, 0, sizeof(*s));
3072
3073     ret = hevc_init_context(avctx);
3074     if (ret < 0)
3075         return ret;
3076
3077     return 0;
3078 }
3079
3080 static void hevc_decode_flush(AVCodecContext *avctx)
3081 {
3082     HEVCContext *s = avctx->priv_data;
3083     ff_hevc_flush_dpb(s);
3084     s->max_ra = INT_MAX;
3085 }
3086
3087 #define OFFSET(x) offsetof(HEVCContext, x)
3088 #define PAR (AV_OPT_FLAG_DECODING_PARAM | AV_OPT_FLAG_VIDEO_PARAM)
3089 static const AVOption options[] = {
3090     { "disable-au", "disable read frame AU by AU", OFFSET(disable_au),
3091         AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1, PAR },
3092     { "strict-displaywin", "stricly apply default display window size", OFFSET(strict_def_disp_win),
3093         AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1, PAR },
3094     { NULL },
3095 };
3096
3097 static const AVClass hevc_decoder_class = {
3098     .class_name = "HEVC decoder",
3099     .item_name  = av_default_item_name,
3100     .option     = options,
3101     .version    = LIBAVUTIL_VERSION_INT,
3102 };
3103
3104 AVCodec ff_hevc_decoder = {
3105     .name                  = "hevc",
3106     .long_name             = NULL_IF_CONFIG_SMALL("HEVC (High Efficiency Video Coding)"),
3107     .type                  = AVMEDIA_TYPE_VIDEO,
3108     .id                    = AV_CODEC_ID_HEVC,
3109     .priv_data_size        = sizeof(HEVCContext),
3110     .priv_class            = &hevc_decoder_class,
3111     .init                  = hevc_decode_init,
3112     .close                 = hevc_decode_free,
3113     .decode                = hevc_decode_frame,
3114     .flush                 = hevc_decode_flush,
3115     .update_thread_context = hevc_update_thread_context,
3116     .init_thread_copy      = hevc_init_thread_copy,
3117     .capabilities   = CODEC_CAP_DR1 | CODEC_CAP_DELAY | CODEC_CAP_FRAME_THREADS | CODEC_CAP_EXPERIMENTAL,
3118 };