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