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