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