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