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