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
9 * This file is part of FFmpeg.
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.
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.
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
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"
37 #include "bytestream.h"
38 #include "cabac_functions.h"
41 #include "hevc_data.h"
42 #include "hevc_parse.h"
46 const uint8_t ff_hevc_pel_weight[65] = { [2] = 0, [4] = 1, [6] = 2, [8] = 3, [12] = 4, [16] = 5, [24] = 6, [32] = 7, [48] = 8, [64] = 9 };
49 * NOTE: Each function hls_foo correspond to the function foo in the
50 * specification (HLS stands for High Level Syntax).
57 /* free everything allocated by pic_arrays_init() */
58 static void pic_arrays_free(HEVCContext *s)
61 av_freep(&s->deblock);
63 av_freep(&s->skip_flag);
64 av_freep(&s->tab_ct_depth);
66 av_freep(&s->tab_ipm);
67 av_freep(&s->cbf_luma);
70 av_freep(&s->qp_y_tab);
71 av_freep(&s->tab_slice_address);
72 av_freep(&s->filter_slice_edges);
74 av_freep(&s->horizontal_bs);
75 av_freep(&s->vertical_bs);
77 av_freep(&s->sh.entry_point_offset);
78 av_freep(&s->sh.size);
79 av_freep(&s->sh.offset);
81 av_buffer_pool_uninit(&s->tab_mvf_pool);
82 av_buffer_pool_uninit(&s->rpl_tab_pool);
85 /* allocate arrays that depend on frame dimensions */
86 static int pic_arrays_init(HEVCContext *s, const HEVCSPS *sps)
88 int log2_min_cb_size = sps->log2_min_cb_size;
89 int width = sps->width;
90 int height = sps->height;
91 int pic_size_in_ctb = ((width >> log2_min_cb_size) + 1) *
92 ((height >> log2_min_cb_size) + 1);
93 int ctb_count = sps->ctb_width * sps->ctb_height;
94 int min_pu_size = sps->min_pu_width * sps->min_pu_height;
96 s->bs_width = (width >> 2) + 1;
97 s->bs_height = (height >> 2) + 1;
99 s->sao = av_mallocz_array(ctb_count, sizeof(*s->sao));
100 s->deblock = av_mallocz_array(ctb_count, sizeof(*s->deblock));
101 if (!s->sao || !s->deblock)
104 s->skip_flag = av_malloc_array(sps->min_cb_height, sps->min_cb_width);
105 s->tab_ct_depth = av_malloc_array(sps->min_cb_height, sps->min_cb_width);
106 if (!s->skip_flag || !s->tab_ct_depth)
109 s->cbf_luma = av_malloc_array(sps->min_tb_width, sps->min_tb_height);
110 s->tab_ipm = av_mallocz(min_pu_size);
111 s->is_pcm = av_malloc_array(sps->min_pu_width + 1, sps->min_pu_height + 1);
112 if (!s->tab_ipm || !s->cbf_luma || !s->is_pcm)
115 s->filter_slice_edges = av_mallocz(ctb_count);
116 s->tab_slice_address = av_malloc_array(pic_size_in_ctb,
117 sizeof(*s->tab_slice_address));
118 s->qp_y_tab = av_malloc_array(pic_size_in_ctb,
119 sizeof(*s->qp_y_tab));
120 if (!s->qp_y_tab || !s->filter_slice_edges || !s->tab_slice_address)
123 s->horizontal_bs = av_mallocz_array(s->bs_width, s->bs_height);
124 s->vertical_bs = av_mallocz_array(s->bs_width, s->bs_height);
125 if (!s->horizontal_bs || !s->vertical_bs)
128 s->tab_mvf_pool = av_buffer_pool_init(min_pu_size * sizeof(MvField),
130 s->rpl_tab_pool = av_buffer_pool_init(ctb_count * sizeof(RefPicListTab),
132 if (!s->tab_mvf_pool || !s->rpl_tab_pool)
139 return AVERROR(ENOMEM);
142 static int pred_weight_table(HEVCContext *s, GetBitContext *gb)
146 uint8_t luma_weight_l0_flag[16];
147 uint8_t chroma_weight_l0_flag[16];
148 uint8_t luma_weight_l1_flag[16];
149 uint8_t chroma_weight_l1_flag[16];
150 int luma_log2_weight_denom;
152 luma_log2_weight_denom = get_ue_golomb_long(gb);
153 if (luma_log2_weight_denom < 0 || luma_log2_weight_denom > 7)
154 av_log(s->avctx, AV_LOG_ERROR, "luma_log2_weight_denom %d is invalid\n", luma_log2_weight_denom);
155 s->sh.luma_log2_weight_denom = av_clip_uintp2(luma_log2_weight_denom, 3);
156 if (s->ps.sps->chroma_format_idc != 0) {
157 int delta = get_se_golomb(gb);
158 s->sh.chroma_log2_weight_denom = av_clip_uintp2(s->sh.luma_log2_weight_denom + delta, 3);
161 for (i = 0; i < s->sh.nb_refs[L0]; i++) {
162 luma_weight_l0_flag[i] = get_bits1(gb);
163 if (!luma_weight_l0_flag[i]) {
164 s->sh.luma_weight_l0[i] = 1 << s->sh.luma_log2_weight_denom;
165 s->sh.luma_offset_l0[i] = 0;
168 if (s->ps.sps->chroma_format_idc != 0) {
169 for (i = 0; i < s->sh.nb_refs[L0]; i++)
170 chroma_weight_l0_flag[i] = get_bits1(gb);
172 for (i = 0; i < s->sh.nb_refs[L0]; i++)
173 chroma_weight_l0_flag[i] = 0;
175 for (i = 0; i < s->sh.nb_refs[L0]; i++) {
176 if (luma_weight_l0_flag[i]) {
177 int delta_luma_weight_l0 = get_se_golomb(gb);
178 s->sh.luma_weight_l0[i] = (1 << s->sh.luma_log2_weight_denom) + delta_luma_weight_l0;
179 s->sh.luma_offset_l0[i] = get_se_golomb(gb);
181 if (chroma_weight_l0_flag[i]) {
182 for (j = 0; j < 2; j++) {
183 int delta_chroma_weight_l0 = get_se_golomb(gb);
184 int delta_chroma_offset_l0 = get_se_golomb(gb);
186 if ( (int8_t)delta_chroma_weight_l0 != delta_chroma_weight_l0
187 || delta_chroma_offset_l0 < -(1<<17) || delta_chroma_offset_l0 > (1<<17)) {
188 return AVERROR_INVALIDDATA;
191 s->sh.chroma_weight_l0[i][j] = (1 << s->sh.chroma_log2_weight_denom) + delta_chroma_weight_l0;
192 s->sh.chroma_offset_l0[i][j] = av_clip((delta_chroma_offset_l0 - ((128 * s->sh.chroma_weight_l0[i][j])
193 >> s->sh.chroma_log2_weight_denom) + 128), -128, 127);
196 s->sh.chroma_weight_l0[i][0] = 1 << s->sh.chroma_log2_weight_denom;
197 s->sh.chroma_offset_l0[i][0] = 0;
198 s->sh.chroma_weight_l0[i][1] = 1 << s->sh.chroma_log2_weight_denom;
199 s->sh.chroma_offset_l0[i][1] = 0;
202 if (s->sh.slice_type == HEVC_SLICE_B) {
203 for (i = 0; i < s->sh.nb_refs[L1]; i++) {
204 luma_weight_l1_flag[i] = get_bits1(gb);
205 if (!luma_weight_l1_flag[i]) {
206 s->sh.luma_weight_l1[i] = 1 << s->sh.luma_log2_weight_denom;
207 s->sh.luma_offset_l1[i] = 0;
210 if (s->ps.sps->chroma_format_idc != 0) {
211 for (i = 0; i < s->sh.nb_refs[L1]; i++)
212 chroma_weight_l1_flag[i] = get_bits1(gb);
214 for (i = 0; i < s->sh.nb_refs[L1]; i++)
215 chroma_weight_l1_flag[i] = 0;
217 for (i = 0; i < s->sh.nb_refs[L1]; i++) {
218 if (luma_weight_l1_flag[i]) {
219 int delta_luma_weight_l1 = get_se_golomb(gb);
220 s->sh.luma_weight_l1[i] = (1 << s->sh.luma_log2_weight_denom) + delta_luma_weight_l1;
221 s->sh.luma_offset_l1[i] = get_se_golomb(gb);
223 if (chroma_weight_l1_flag[i]) {
224 for (j = 0; j < 2; j++) {
225 int delta_chroma_weight_l1 = get_se_golomb(gb);
226 int delta_chroma_offset_l1 = get_se_golomb(gb);
228 if ( (int8_t)delta_chroma_weight_l1 != delta_chroma_weight_l1
229 || delta_chroma_offset_l1 < -(1<<17) || delta_chroma_offset_l1 > (1<<17)) {
230 return AVERROR_INVALIDDATA;
233 s->sh.chroma_weight_l1[i][j] = (1 << s->sh.chroma_log2_weight_denom) + delta_chroma_weight_l1;
234 s->sh.chroma_offset_l1[i][j] = av_clip((delta_chroma_offset_l1 - ((128 * s->sh.chroma_weight_l1[i][j])
235 >> s->sh.chroma_log2_weight_denom) + 128), -128, 127);
238 s->sh.chroma_weight_l1[i][0] = 1 << s->sh.chroma_log2_weight_denom;
239 s->sh.chroma_offset_l1[i][0] = 0;
240 s->sh.chroma_weight_l1[i][1] = 1 << s->sh.chroma_log2_weight_denom;
241 s->sh.chroma_offset_l1[i][1] = 0;
248 static int decode_lt_rps(HEVCContext *s, LongTermRPS *rps, GetBitContext *gb)
250 const HEVCSPS *sps = s->ps.sps;
251 int max_poc_lsb = 1 << sps->log2_max_poc_lsb;
252 int prev_delta_msb = 0;
253 unsigned int nb_sps = 0, nb_sh;
257 if (!sps->long_term_ref_pics_present_flag)
260 if (sps->num_long_term_ref_pics_sps > 0)
261 nb_sps = get_ue_golomb_long(gb);
262 nb_sh = get_ue_golomb_long(gb);
264 if (nb_sps > sps->num_long_term_ref_pics_sps)
265 return AVERROR_INVALIDDATA;
266 if (nb_sh + (uint64_t)nb_sps > FF_ARRAY_ELEMS(rps->poc))
267 return AVERROR_INVALIDDATA;
269 rps->nb_refs = nb_sh + nb_sps;
271 for (i = 0; i < rps->nb_refs; i++) {
272 uint8_t delta_poc_msb_present;
275 uint8_t lt_idx_sps = 0;
277 if (sps->num_long_term_ref_pics_sps > 1)
278 lt_idx_sps = get_bits(gb, av_ceil_log2(sps->num_long_term_ref_pics_sps));
280 rps->poc[i] = sps->lt_ref_pic_poc_lsb_sps[lt_idx_sps];
281 rps->used[i] = sps->used_by_curr_pic_lt_sps_flag[lt_idx_sps];
283 rps->poc[i] = get_bits(gb, sps->log2_max_poc_lsb);
284 rps->used[i] = get_bits1(gb);
287 delta_poc_msb_present = get_bits1(gb);
288 if (delta_poc_msb_present) {
289 int64_t delta = get_ue_golomb_long(gb);
292 if (i && i != nb_sps)
293 delta += prev_delta_msb;
295 poc = rps->poc[i] + s->poc - delta * max_poc_lsb - s->sh.pic_order_cnt_lsb;
296 if (poc != (int32_t)poc)
297 return AVERROR_INVALIDDATA;
299 prev_delta_msb = delta;
306 static void export_stream_params(AVCodecContext *avctx, const HEVCParamSets *ps,
309 const HEVCVPS *vps = (const HEVCVPS*)ps->vps_list[sps->vps_id]->data;
310 const HEVCWindow *ow = &sps->output_window;
311 unsigned int num = 0, den = 0;
313 avctx->pix_fmt = sps->pix_fmt;
314 avctx->coded_width = sps->width;
315 avctx->coded_height = sps->height;
316 avctx->width = sps->width - ow->left_offset - ow->right_offset;
317 avctx->height = sps->height - ow->top_offset - ow->bottom_offset;
318 avctx->has_b_frames = sps->temporal_layer[sps->max_sub_layers - 1].num_reorder_pics;
319 avctx->profile = sps->ptl.general_ptl.profile_idc;
320 avctx->level = sps->ptl.general_ptl.level_idc;
322 ff_set_sar(avctx, sps->vui.sar);
324 if (sps->vui.video_signal_type_present_flag)
325 avctx->color_range = sps->vui.video_full_range_flag ? AVCOL_RANGE_JPEG
328 avctx->color_range = AVCOL_RANGE_MPEG;
330 if (sps->vui.colour_description_present_flag) {
331 avctx->color_primaries = sps->vui.colour_primaries;
332 avctx->color_trc = sps->vui.transfer_characteristic;
333 avctx->colorspace = sps->vui.matrix_coeffs;
335 avctx->color_primaries = AVCOL_PRI_UNSPECIFIED;
336 avctx->color_trc = AVCOL_TRC_UNSPECIFIED;
337 avctx->colorspace = AVCOL_SPC_UNSPECIFIED;
340 if (vps->vps_timing_info_present_flag) {
341 num = vps->vps_num_units_in_tick;
342 den = vps->vps_time_scale;
343 } else if (sps->vui.vui_timing_info_present_flag) {
344 num = sps->vui.vui_num_units_in_tick;
345 den = sps->vui.vui_time_scale;
348 if (num != 0 && den != 0)
349 av_reduce(&avctx->framerate.den, &avctx->framerate.num,
353 static enum AVPixelFormat get_format(HEVCContext *s, const HEVCSPS *sps)
355 #define HWACCEL_MAX (CONFIG_HEVC_DXVA2_HWACCEL + CONFIG_HEVC_D3D11VA_HWACCEL * 2 + CONFIG_HEVC_VAAPI_HWACCEL + CONFIG_HEVC_VDPAU_HWACCEL)
356 enum AVPixelFormat pix_fmts[HWACCEL_MAX + 2], *fmt = pix_fmts;
358 switch (sps->pix_fmt) {
359 case AV_PIX_FMT_YUV420P:
360 case AV_PIX_FMT_YUVJ420P:
361 #if CONFIG_HEVC_DXVA2_HWACCEL
362 *fmt++ = AV_PIX_FMT_DXVA2_VLD;
364 #if CONFIG_HEVC_D3D11VA_HWACCEL
365 *fmt++ = AV_PIX_FMT_D3D11VA_VLD;
366 *fmt++ = AV_PIX_FMT_D3D11;
368 #if CONFIG_HEVC_VAAPI_HWACCEL
369 *fmt++ = AV_PIX_FMT_VAAPI;
371 #if CONFIG_HEVC_VDPAU_HWACCEL
372 *fmt++ = AV_PIX_FMT_VDPAU;
375 case AV_PIX_FMT_YUV420P10:
376 #if CONFIG_HEVC_DXVA2_HWACCEL
377 *fmt++ = AV_PIX_FMT_DXVA2_VLD;
379 #if CONFIG_HEVC_D3D11VA_HWACCEL
380 *fmt++ = AV_PIX_FMT_D3D11VA_VLD;
381 *fmt++ = AV_PIX_FMT_D3D11;
383 #if CONFIG_HEVC_VAAPI_HWACCEL
384 *fmt++ = AV_PIX_FMT_VAAPI;
389 *fmt++ = sps->pix_fmt;
390 *fmt = AV_PIX_FMT_NONE;
392 return ff_thread_get_format(s->avctx, pix_fmts);
395 static int set_sps(HEVCContext *s, const HEVCSPS *sps,
396 enum AVPixelFormat pix_fmt)
407 ret = pic_arrays_init(s, sps);
411 export_stream_params(s->avctx, &s->ps, sps);
413 s->avctx->pix_fmt = pix_fmt;
415 ff_hevc_pred_init(&s->hpc, sps->bit_depth);
416 ff_hevc_dsp_init (&s->hevcdsp, sps->bit_depth);
417 ff_videodsp_init (&s->vdsp, sps->bit_depth);
419 for (i = 0; i < 3; i++) {
420 av_freep(&s->sao_pixel_buffer_h[i]);
421 av_freep(&s->sao_pixel_buffer_v[i]);
424 if (sps->sao_enabled && !s->avctx->hwaccel) {
425 int c_count = (sps->chroma_format_idc != 0) ? 3 : 1;
428 for(c_idx = 0; c_idx < c_count; c_idx++) {
429 int w = sps->width >> sps->hshift[c_idx];
430 int h = sps->height >> sps->vshift[c_idx];
431 s->sao_pixel_buffer_h[c_idx] =
432 av_malloc((w * 2 * sps->ctb_height) <<
434 s->sao_pixel_buffer_v[c_idx] =
435 av_malloc((h * 2 * sps->ctb_width) <<
441 s->ps.vps = (HEVCVPS*) s->ps.vps_list[s->ps.sps->vps_id]->data;
451 static int hls_slice_header(HEVCContext *s)
453 GetBitContext *gb = &s->HEVClc->gb;
454 SliceHeader *sh = &s->sh;
458 sh->first_slice_in_pic_flag = get_bits1(gb);
459 if ((IS_IDR(s) || IS_BLA(s)) && sh->first_slice_in_pic_flag) {
460 s->seq_decode = (s->seq_decode + 1) & 0xff;
463 ff_hevc_clear_refs(s);
465 sh->no_output_of_prior_pics_flag = 0;
467 sh->no_output_of_prior_pics_flag = get_bits1(gb);
469 sh->pps_id = get_ue_golomb_long(gb);
470 if (sh->pps_id >= HEVC_MAX_PPS_COUNT || !s->ps.pps_list[sh->pps_id]) {
471 av_log(s->avctx, AV_LOG_ERROR, "PPS id out of range: %d\n", sh->pps_id);
472 return AVERROR_INVALIDDATA;
474 if (!sh->first_slice_in_pic_flag &&
475 s->ps.pps != (HEVCPPS*)s->ps.pps_list[sh->pps_id]->data) {
476 av_log(s->avctx, AV_LOG_ERROR, "PPS changed between slices.\n");
477 return AVERROR_INVALIDDATA;
479 s->ps.pps = (HEVCPPS*)s->ps.pps_list[sh->pps_id]->data;
480 if (s->nal_unit_type == HEVC_NAL_CRA_NUT && s->last_eos == 1)
481 sh->no_output_of_prior_pics_flag = 1;
483 if (s->ps.sps != (HEVCSPS*)s->ps.sps_list[s->ps.pps->sps_id]->data) {
484 const HEVCSPS *sps = (HEVCSPS*)s->ps.sps_list[s->ps.pps->sps_id]->data;
485 const HEVCSPS *last_sps = s->ps.sps;
486 enum AVPixelFormat pix_fmt;
488 if (last_sps && IS_IRAP(s) && s->nal_unit_type != HEVC_NAL_CRA_NUT) {
489 if (sps->width != last_sps->width || sps->height != last_sps->height ||
490 sps->temporal_layer[sps->max_sub_layers - 1].max_dec_pic_buffering !=
491 last_sps->temporal_layer[last_sps->max_sub_layers - 1].max_dec_pic_buffering)
492 sh->no_output_of_prior_pics_flag = 0;
494 ff_hevc_clear_refs(s);
496 pix_fmt = get_format(s, sps);
500 ret = set_sps(s, sps, pix_fmt);
504 s->seq_decode = (s->seq_decode + 1) & 0xff;
508 sh->dependent_slice_segment_flag = 0;
509 if (!sh->first_slice_in_pic_flag) {
510 int slice_address_length;
512 if (s->ps.pps->dependent_slice_segments_enabled_flag)
513 sh->dependent_slice_segment_flag = get_bits1(gb);
515 slice_address_length = av_ceil_log2(s->ps.sps->ctb_width *
516 s->ps.sps->ctb_height);
517 sh->slice_segment_addr = get_bitsz(gb, slice_address_length);
518 if (sh->slice_segment_addr >= s->ps.sps->ctb_width * s->ps.sps->ctb_height) {
519 av_log(s->avctx, AV_LOG_ERROR,
520 "Invalid slice segment address: %u.\n",
521 sh->slice_segment_addr);
522 return AVERROR_INVALIDDATA;
525 if (!sh->dependent_slice_segment_flag) {
526 sh->slice_addr = sh->slice_segment_addr;
530 sh->slice_segment_addr = sh->slice_addr = 0;
532 s->slice_initialized = 0;
535 if (!sh->dependent_slice_segment_flag) {
536 s->slice_initialized = 0;
538 for (i = 0; i < s->ps.pps->num_extra_slice_header_bits; i++)
539 skip_bits(gb, 1); // slice_reserved_undetermined_flag[]
541 sh->slice_type = get_ue_golomb_long(gb);
542 if (!(sh->slice_type == HEVC_SLICE_I ||
543 sh->slice_type == HEVC_SLICE_P ||
544 sh->slice_type == HEVC_SLICE_B)) {
545 av_log(s->avctx, AV_LOG_ERROR, "Unknown slice type: %d.\n",
547 return AVERROR_INVALIDDATA;
549 if (IS_IRAP(s) && sh->slice_type != HEVC_SLICE_I) {
550 av_log(s->avctx, AV_LOG_ERROR, "Inter slices in an IRAP frame.\n");
551 return AVERROR_INVALIDDATA;
554 // when flag is not present, picture is inferred to be output
555 sh->pic_output_flag = 1;
556 if (s->ps.pps->output_flag_present_flag)
557 sh->pic_output_flag = get_bits1(gb);
559 if (s->ps.sps->separate_colour_plane_flag)
560 sh->colour_plane_id = get_bits(gb, 2);
565 sh->pic_order_cnt_lsb = get_bits(gb, s->ps.sps->log2_max_poc_lsb);
566 poc = ff_hevc_compute_poc(s->ps.sps, s->pocTid0, sh->pic_order_cnt_lsb, s->nal_unit_type);
567 if (!sh->first_slice_in_pic_flag && poc != s->poc) {
568 av_log(s->avctx, AV_LOG_WARNING,
569 "Ignoring POC change between slices: %d -> %d\n", s->poc, poc);
570 if (s->avctx->err_recognition & AV_EF_EXPLODE)
571 return AVERROR_INVALIDDATA;
576 sh->short_term_ref_pic_set_sps_flag = get_bits1(gb);
577 pos = get_bits_left(gb);
578 if (!sh->short_term_ref_pic_set_sps_flag) {
579 ret = ff_hevc_decode_short_term_rps(gb, s->avctx, &sh->slice_rps, s->ps.sps, 1);
583 sh->short_term_rps = &sh->slice_rps;
585 int numbits, rps_idx;
587 if (!s->ps.sps->nb_st_rps) {
588 av_log(s->avctx, AV_LOG_ERROR, "No ref lists in the SPS.\n");
589 return AVERROR_INVALIDDATA;
592 numbits = av_ceil_log2(s->ps.sps->nb_st_rps);
593 rps_idx = numbits > 0 ? get_bits(gb, numbits) : 0;
594 sh->short_term_rps = &s->ps.sps->st_rps[rps_idx];
596 sh->short_term_ref_pic_set_size = pos - get_bits_left(gb);
598 pos = get_bits_left(gb);
599 ret = decode_lt_rps(s, &sh->long_term_rps, gb);
601 av_log(s->avctx, AV_LOG_WARNING, "Invalid long term RPS.\n");
602 if (s->avctx->err_recognition & AV_EF_EXPLODE)
603 return AVERROR_INVALIDDATA;
605 sh->long_term_ref_pic_set_size = pos - get_bits_left(gb);
607 if (s->ps.sps->sps_temporal_mvp_enabled_flag)
608 sh->slice_temporal_mvp_enabled_flag = get_bits1(gb);
610 sh->slice_temporal_mvp_enabled_flag = 0;
612 s->sh.short_term_rps = NULL;
617 if (sh->first_slice_in_pic_flag && s->temporal_id == 0 &&
618 s->nal_unit_type != HEVC_NAL_TRAIL_N &&
619 s->nal_unit_type != HEVC_NAL_TSA_N &&
620 s->nal_unit_type != HEVC_NAL_STSA_N &&
621 s->nal_unit_type != HEVC_NAL_RADL_N &&
622 s->nal_unit_type != HEVC_NAL_RADL_R &&
623 s->nal_unit_type != HEVC_NAL_RASL_N &&
624 s->nal_unit_type != HEVC_NAL_RASL_R)
627 if (s->ps.sps->sao_enabled) {
628 sh->slice_sample_adaptive_offset_flag[0] = get_bits1(gb);
629 if (s->ps.sps->chroma_format_idc) {
630 sh->slice_sample_adaptive_offset_flag[1] =
631 sh->slice_sample_adaptive_offset_flag[2] = get_bits1(gb);
634 sh->slice_sample_adaptive_offset_flag[0] = 0;
635 sh->slice_sample_adaptive_offset_flag[1] = 0;
636 sh->slice_sample_adaptive_offset_flag[2] = 0;
639 sh->nb_refs[L0] = sh->nb_refs[L1] = 0;
640 if (sh->slice_type == HEVC_SLICE_P || sh->slice_type == HEVC_SLICE_B) {
643 sh->nb_refs[L0] = s->ps.pps->num_ref_idx_l0_default_active;
644 if (sh->slice_type == HEVC_SLICE_B)
645 sh->nb_refs[L1] = s->ps.pps->num_ref_idx_l1_default_active;
647 if (get_bits1(gb)) { // num_ref_idx_active_override_flag
648 sh->nb_refs[L0] = get_ue_golomb_long(gb) + 1;
649 if (sh->slice_type == HEVC_SLICE_B)
650 sh->nb_refs[L1] = get_ue_golomb_long(gb) + 1;
652 if (sh->nb_refs[L0] > HEVC_MAX_REFS || sh->nb_refs[L1] > HEVC_MAX_REFS) {
653 av_log(s->avctx, AV_LOG_ERROR, "Too many refs: %d/%d.\n",
654 sh->nb_refs[L0], sh->nb_refs[L1]);
655 return AVERROR_INVALIDDATA;
658 sh->rpl_modification_flag[0] = 0;
659 sh->rpl_modification_flag[1] = 0;
660 nb_refs = ff_hevc_frame_nb_refs(s);
662 av_log(s->avctx, AV_LOG_ERROR, "Zero refs for a frame with P or B slices.\n");
663 return AVERROR_INVALIDDATA;
666 if (s->ps.pps->lists_modification_present_flag && nb_refs > 1) {
667 sh->rpl_modification_flag[0] = get_bits1(gb);
668 if (sh->rpl_modification_flag[0]) {
669 for (i = 0; i < sh->nb_refs[L0]; i++)
670 sh->list_entry_lx[0][i] = get_bits(gb, av_ceil_log2(nb_refs));
673 if (sh->slice_type == HEVC_SLICE_B) {
674 sh->rpl_modification_flag[1] = get_bits1(gb);
675 if (sh->rpl_modification_flag[1] == 1)
676 for (i = 0; i < sh->nb_refs[L1]; i++)
677 sh->list_entry_lx[1][i] = get_bits(gb, av_ceil_log2(nb_refs));
681 if (sh->slice_type == HEVC_SLICE_B)
682 sh->mvd_l1_zero_flag = get_bits1(gb);
684 if (s->ps.pps->cabac_init_present_flag)
685 sh->cabac_init_flag = get_bits1(gb);
687 sh->cabac_init_flag = 0;
689 sh->collocated_ref_idx = 0;
690 if (sh->slice_temporal_mvp_enabled_flag) {
691 sh->collocated_list = L0;
692 if (sh->slice_type == HEVC_SLICE_B)
693 sh->collocated_list = !get_bits1(gb);
695 if (sh->nb_refs[sh->collocated_list] > 1) {
696 sh->collocated_ref_idx = get_ue_golomb_long(gb);
697 if (sh->collocated_ref_idx >= sh->nb_refs[sh->collocated_list]) {
698 av_log(s->avctx, AV_LOG_ERROR,
699 "Invalid collocated_ref_idx: %d.\n",
700 sh->collocated_ref_idx);
701 return AVERROR_INVALIDDATA;
706 if ((s->ps.pps->weighted_pred_flag && sh->slice_type == HEVC_SLICE_P) ||
707 (s->ps.pps->weighted_bipred_flag && sh->slice_type == HEVC_SLICE_B)) {
708 int ret = pred_weight_table(s, gb);
713 sh->max_num_merge_cand = 5 - get_ue_golomb_long(gb);
714 if (sh->max_num_merge_cand < 1 || sh->max_num_merge_cand > 5) {
715 av_log(s->avctx, AV_LOG_ERROR,
716 "Invalid number of merging MVP candidates: %d.\n",
717 sh->max_num_merge_cand);
718 return AVERROR_INVALIDDATA;
722 sh->slice_qp_delta = get_se_golomb(gb);
724 if (s->ps.pps->pic_slice_level_chroma_qp_offsets_present_flag) {
725 sh->slice_cb_qp_offset = get_se_golomb(gb);
726 sh->slice_cr_qp_offset = get_se_golomb(gb);
728 sh->slice_cb_qp_offset = 0;
729 sh->slice_cr_qp_offset = 0;
732 if (s->ps.pps->chroma_qp_offset_list_enabled_flag)
733 sh->cu_chroma_qp_offset_enabled_flag = get_bits1(gb);
735 sh->cu_chroma_qp_offset_enabled_flag = 0;
737 if (s->ps.pps->deblocking_filter_control_present_flag) {
738 int deblocking_filter_override_flag = 0;
740 if (s->ps.pps->deblocking_filter_override_enabled_flag)
741 deblocking_filter_override_flag = get_bits1(gb);
743 if (deblocking_filter_override_flag) {
744 sh->disable_deblocking_filter_flag = get_bits1(gb);
745 if (!sh->disable_deblocking_filter_flag) {
746 int beta_offset_div2 = get_se_golomb(gb);
747 int tc_offset_div2 = get_se_golomb(gb) ;
748 if (beta_offset_div2 < -6 || beta_offset_div2 > 6 ||
749 tc_offset_div2 < -6 || tc_offset_div2 > 6) {
750 av_log(s->avctx, AV_LOG_ERROR,
751 "Invalid deblock filter offsets: %d, %d\n",
752 beta_offset_div2, tc_offset_div2);
753 return AVERROR_INVALIDDATA;
755 sh->beta_offset = beta_offset_div2 * 2;
756 sh->tc_offset = tc_offset_div2 * 2;
759 sh->disable_deblocking_filter_flag = s->ps.pps->disable_dbf;
760 sh->beta_offset = s->ps.pps->beta_offset;
761 sh->tc_offset = s->ps.pps->tc_offset;
764 sh->disable_deblocking_filter_flag = 0;
769 if (s->ps.pps->seq_loop_filter_across_slices_enabled_flag &&
770 (sh->slice_sample_adaptive_offset_flag[0] ||
771 sh->slice_sample_adaptive_offset_flag[1] ||
772 !sh->disable_deblocking_filter_flag)) {
773 sh->slice_loop_filter_across_slices_enabled_flag = get_bits1(gb);
775 sh->slice_loop_filter_across_slices_enabled_flag = s->ps.pps->seq_loop_filter_across_slices_enabled_flag;
777 } else if (!s->slice_initialized) {
778 av_log(s->avctx, AV_LOG_ERROR, "Independent slice segment missing.\n");
779 return AVERROR_INVALIDDATA;
782 sh->num_entry_point_offsets = 0;
783 if (s->ps.pps->tiles_enabled_flag || s->ps.pps->entropy_coding_sync_enabled_flag) {
784 unsigned num_entry_point_offsets = get_ue_golomb_long(gb);
785 // It would be possible to bound this tighter but this here is simpler
786 if (num_entry_point_offsets > get_bits_left(gb)) {
787 av_log(s->avctx, AV_LOG_ERROR, "num_entry_point_offsets %d is invalid\n", num_entry_point_offsets);
788 return AVERROR_INVALIDDATA;
791 sh->num_entry_point_offsets = num_entry_point_offsets;
792 if (sh->num_entry_point_offsets > 0) {
793 int offset_len = get_ue_golomb_long(gb) + 1;
795 if (offset_len < 1 || offset_len > 32) {
796 sh->num_entry_point_offsets = 0;
797 av_log(s->avctx, AV_LOG_ERROR, "offset_len %d is invalid\n", offset_len);
798 return AVERROR_INVALIDDATA;
801 av_freep(&sh->entry_point_offset);
802 av_freep(&sh->offset);
804 sh->entry_point_offset = av_malloc_array(sh->num_entry_point_offsets, sizeof(unsigned));
805 sh->offset = av_malloc_array(sh->num_entry_point_offsets, sizeof(int));
806 sh->size = av_malloc_array(sh->num_entry_point_offsets, sizeof(int));
807 if (!sh->entry_point_offset || !sh->offset || !sh->size) {
808 sh->num_entry_point_offsets = 0;
809 av_log(s->avctx, AV_LOG_ERROR, "Failed to allocate memory\n");
810 return AVERROR(ENOMEM);
812 for (i = 0; i < sh->num_entry_point_offsets; i++) {
813 unsigned val = get_bits_long(gb, offset_len);
814 sh->entry_point_offset[i] = val + 1; // +1; // +1 to get the size
816 if (s->threads_number > 1 && (s->ps.pps->num_tile_rows > 1 || s->ps.pps->num_tile_columns > 1)) {
817 s->enable_parallel_tiles = 0; // TODO: you can enable tiles in parallel here
818 s->threads_number = 1;
820 s->enable_parallel_tiles = 0;
822 s->enable_parallel_tiles = 0;
825 if (s->ps.pps->slice_header_extension_present_flag) {
826 unsigned int length = get_ue_golomb_long(gb);
827 if (length*8LL > get_bits_left(gb)) {
828 av_log(s->avctx, AV_LOG_ERROR, "too many slice_header_extension_data_bytes\n");
829 return AVERROR_INVALIDDATA;
831 for (i = 0; i < length; i++)
832 skip_bits(gb, 8); // slice_header_extension_data_byte
835 // Inferred parameters
836 sh->slice_qp = 26U + s->ps.pps->pic_init_qp_minus26 + sh->slice_qp_delta;
837 if (sh->slice_qp > 51 ||
838 sh->slice_qp < -s->ps.sps->qp_bd_offset) {
839 av_log(s->avctx, AV_LOG_ERROR,
840 "The slice_qp %d is outside the valid range "
843 -s->ps.sps->qp_bd_offset);
844 return AVERROR_INVALIDDATA;
847 sh->slice_ctb_addr_rs = sh->slice_segment_addr;
849 if (!s->sh.slice_ctb_addr_rs && s->sh.dependent_slice_segment_flag) {
850 av_log(s->avctx, AV_LOG_ERROR, "Impossible slice segment.\n");
851 return AVERROR_INVALIDDATA;
854 if (get_bits_left(gb) < 0) {
855 av_log(s->avctx, AV_LOG_ERROR,
856 "Overread slice header by %d bits\n", -get_bits_left(gb));
857 return AVERROR_INVALIDDATA;
860 s->HEVClc->first_qp_group = !s->sh.dependent_slice_segment_flag;
862 if (!s->ps.pps->cu_qp_delta_enabled_flag)
863 s->HEVClc->qp_y = s->sh.slice_qp;
865 s->slice_initialized = 1;
866 s->HEVClc->tu.cu_qp_offset_cb = 0;
867 s->HEVClc->tu.cu_qp_offset_cr = 0;
872 #define CTB(tab, x, y) ((tab)[(y) * s->ps.sps->ctb_width + (x)])
874 #define SET_SAO(elem, value) \
876 if (!sao_merge_up_flag && !sao_merge_left_flag) \
878 else if (sao_merge_left_flag) \
879 sao->elem = CTB(s->sao, rx-1, ry).elem; \
880 else if (sao_merge_up_flag) \
881 sao->elem = CTB(s->sao, rx, ry-1).elem; \
886 static void hls_sao_param(HEVCContext *s, int rx, int ry)
888 HEVCLocalContext *lc = s->HEVClc;
889 int sao_merge_left_flag = 0;
890 int sao_merge_up_flag = 0;
891 SAOParams *sao = &CTB(s->sao, rx, ry);
894 if (s->sh.slice_sample_adaptive_offset_flag[0] ||
895 s->sh.slice_sample_adaptive_offset_flag[1]) {
897 if (lc->ctb_left_flag)
898 sao_merge_left_flag = ff_hevc_sao_merge_flag_decode(s);
900 if (ry > 0 && !sao_merge_left_flag) {
902 sao_merge_up_flag = ff_hevc_sao_merge_flag_decode(s);
906 for (c_idx = 0; c_idx < (s->ps.sps->chroma_format_idc ? 3 : 1); c_idx++) {
907 int log2_sao_offset_scale = c_idx == 0 ? s->ps.pps->log2_sao_offset_scale_luma :
908 s->ps.pps->log2_sao_offset_scale_chroma;
910 if (!s->sh.slice_sample_adaptive_offset_flag[c_idx]) {
911 sao->type_idx[c_idx] = SAO_NOT_APPLIED;
916 sao->type_idx[2] = sao->type_idx[1];
917 sao->eo_class[2] = sao->eo_class[1];
919 SET_SAO(type_idx[c_idx], ff_hevc_sao_type_idx_decode(s));
922 if (sao->type_idx[c_idx] == SAO_NOT_APPLIED)
925 for (i = 0; i < 4; i++)
926 SET_SAO(offset_abs[c_idx][i], ff_hevc_sao_offset_abs_decode(s));
928 if (sao->type_idx[c_idx] == SAO_BAND) {
929 for (i = 0; i < 4; i++) {
930 if (sao->offset_abs[c_idx][i]) {
931 SET_SAO(offset_sign[c_idx][i],
932 ff_hevc_sao_offset_sign_decode(s));
934 sao->offset_sign[c_idx][i] = 0;
937 SET_SAO(band_position[c_idx], ff_hevc_sao_band_position_decode(s));
938 } else if (c_idx != 2) {
939 SET_SAO(eo_class[c_idx], ff_hevc_sao_eo_class_decode(s));
942 // Inferred parameters
943 sao->offset_val[c_idx][0] = 0;
944 for (i = 0; i < 4; i++) {
945 sao->offset_val[c_idx][i + 1] = sao->offset_abs[c_idx][i];
946 if (sao->type_idx[c_idx] == SAO_EDGE) {
948 sao->offset_val[c_idx][i + 1] = -sao->offset_val[c_idx][i + 1];
949 } else if (sao->offset_sign[c_idx][i]) {
950 sao->offset_val[c_idx][i + 1] = -sao->offset_val[c_idx][i + 1];
952 sao->offset_val[c_idx][i + 1] *= 1 << log2_sao_offset_scale;
960 static int hls_cross_component_pred(HEVCContext *s, int idx) {
961 HEVCLocalContext *lc = s->HEVClc;
962 int log2_res_scale_abs_plus1 = ff_hevc_log2_res_scale_abs(s, idx);
964 if (log2_res_scale_abs_plus1 != 0) {
965 int res_scale_sign_flag = ff_hevc_res_scale_sign_flag(s, idx);
966 lc->tu.res_scale_val = (1 << (log2_res_scale_abs_plus1 - 1)) *
967 (1 - 2 * res_scale_sign_flag);
969 lc->tu.res_scale_val = 0;
976 static int hls_transform_unit(HEVCContext *s, int x0, int y0,
977 int xBase, int yBase, int cb_xBase, int cb_yBase,
978 int log2_cb_size, int log2_trafo_size,
979 int blk_idx, int cbf_luma, int *cbf_cb, int *cbf_cr)
981 HEVCLocalContext *lc = s->HEVClc;
982 const int log2_trafo_size_c = log2_trafo_size - s->ps.sps->hshift[1];
985 if (lc->cu.pred_mode == MODE_INTRA) {
986 int trafo_size = 1 << log2_trafo_size;
987 ff_hevc_set_neighbour_available(s, x0, y0, trafo_size, trafo_size);
989 s->hpc.intra_pred[log2_trafo_size - 2](s, x0, y0, 0);
992 if (cbf_luma || cbf_cb[0] || cbf_cr[0] ||
993 (s->ps.sps->chroma_format_idc == 2 && (cbf_cb[1] || cbf_cr[1]))) {
994 int scan_idx = SCAN_DIAG;
995 int scan_idx_c = SCAN_DIAG;
996 int cbf_chroma = cbf_cb[0] || cbf_cr[0] ||
997 (s->ps.sps->chroma_format_idc == 2 &&
998 (cbf_cb[1] || cbf_cr[1]));
1000 if (s->ps.pps->cu_qp_delta_enabled_flag && !lc->tu.is_cu_qp_delta_coded) {
1001 lc->tu.cu_qp_delta = ff_hevc_cu_qp_delta_abs(s);
1002 if (lc->tu.cu_qp_delta != 0)
1003 if (ff_hevc_cu_qp_delta_sign_flag(s) == 1)
1004 lc->tu.cu_qp_delta = -lc->tu.cu_qp_delta;
1005 lc->tu.is_cu_qp_delta_coded = 1;
1007 if (lc->tu.cu_qp_delta < -(26 + s->ps.sps->qp_bd_offset / 2) ||
1008 lc->tu.cu_qp_delta > (25 + s->ps.sps->qp_bd_offset / 2)) {
1009 av_log(s->avctx, AV_LOG_ERROR,
1010 "The cu_qp_delta %d is outside the valid range "
1013 -(26 + s->ps.sps->qp_bd_offset / 2),
1014 (25 + s->ps.sps->qp_bd_offset / 2));
1015 return AVERROR_INVALIDDATA;
1018 ff_hevc_set_qPy(s, cb_xBase, cb_yBase, log2_cb_size);
1021 if (s->sh.cu_chroma_qp_offset_enabled_flag && cbf_chroma &&
1022 !lc->cu.cu_transquant_bypass_flag && !lc->tu.is_cu_chroma_qp_offset_coded) {
1023 int cu_chroma_qp_offset_flag = ff_hevc_cu_chroma_qp_offset_flag(s);
1024 if (cu_chroma_qp_offset_flag) {
1025 int cu_chroma_qp_offset_idx = 0;
1026 if (s->ps.pps->chroma_qp_offset_list_len_minus1 > 0) {
1027 cu_chroma_qp_offset_idx = ff_hevc_cu_chroma_qp_offset_idx(s);
1028 av_log(s->avctx, AV_LOG_ERROR,
1029 "cu_chroma_qp_offset_idx not yet tested.\n");
1031 lc->tu.cu_qp_offset_cb = s->ps.pps->cb_qp_offset_list[cu_chroma_qp_offset_idx];
1032 lc->tu.cu_qp_offset_cr = s->ps.pps->cr_qp_offset_list[cu_chroma_qp_offset_idx];
1034 lc->tu.cu_qp_offset_cb = 0;
1035 lc->tu.cu_qp_offset_cr = 0;
1037 lc->tu.is_cu_chroma_qp_offset_coded = 1;
1040 if (lc->cu.pred_mode == MODE_INTRA && log2_trafo_size < 4) {
1041 if (lc->tu.intra_pred_mode >= 6 &&
1042 lc->tu.intra_pred_mode <= 14) {
1043 scan_idx = SCAN_VERT;
1044 } else if (lc->tu.intra_pred_mode >= 22 &&
1045 lc->tu.intra_pred_mode <= 30) {
1046 scan_idx = SCAN_HORIZ;
1049 if (lc->tu.intra_pred_mode_c >= 6 &&
1050 lc->tu.intra_pred_mode_c <= 14) {
1051 scan_idx_c = SCAN_VERT;
1052 } else if (lc->tu.intra_pred_mode_c >= 22 &&
1053 lc->tu.intra_pred_mode_c <= 30) {
1054 scan_idx_c = SCAN_HORIZ;
1058 lc->tu.cross_pf = 0;
1061 ff_hevc_hls_residual_coding(s, x0, y0, log2_trafo_size, scan_idx, 0);
1062 if (s->ps.sps->chroma_format_idc && (log2_trafo_size > 2 || s->ps.sps->chroma_format_idc == 3)) {
1063 int trafo_size_h = 1 << (log2_trafo_size_c + s->ps.sps->hshift[1]);
1064 int trafo_size_v = 1 << (log2_trafo_size_c + s->ps.sps->vshift[1]);
1065 lc->tu.cross_pf = (s->ps.pps->cross_component_prediction_enabled_flag && cbf_luma &&
1066 (lc->cu.pred_mode == MODE_INTER ||
1067 (lc->tu.chroma_mode_c == 4)));
1069 if (lc->tu.cross_pf) {
1070 hls_cross_component_pred(s, 0);
1072 for (i = 0; i < (s->ps.sps->chroma_format_idc == 2 ? 2 : 1); i++) {
1073 if (lc->cu.pred_mode == MODE_INTRA) {
1074 ff_hevc_set_neighbour_available(s, x0, y0 + (i << log2_trafo_size_c), trafo_size_h, trafo_size_v);
1075 s->hpc.intra_pred[log2_trafo_size_c - 2](s, x0, y0 + (i << log2_trafo_size_c), 1);
1078 ff_hevc_hls_residual_coding(s, x0, y0 + (i << log2_trafo_size_c),
1079 log2_trafo_size_c, scan_idx_c, 1);
1081 if (lc->tu.cross_pf) {
1082 ptrdiff_t stride = s->frame->linesize[1];
1083 int hshift = s->ps.sps->hshift[1];
1084 int vshift = s->ps.sps->vshift[1];
1085 int16_t *coeffs_y = (int16_t*)lc->edge_emu_buffer;
1086 int16_t *coeffs = (int16_t*)lc->edge_emu_buffer2;
1087 int size = 1 << log2_trafo_size_c;
1089 uint8_t *dst = &s->frame->data[1][(y0 >> vshift) * stride +
1090 ((x0 >> hshift) << s->ps.sps->pixel_shift)];
1091 for (i = 0; i < (size * size); i++) {
1092 coeffs[i] = ((lc->tu.res_scale_val * coeffs_y[i]) >> 3);
1094 s->hevcdsp.add_residual[log2_trafo_size_c-2](dst, coeffs, stride);
1098 if (lc->tu.cross_pf) {
1099 hls_cross_component_pred(s, 1);
1101 for (i = 0; i < (s->ps.sps->chroma_format_idc == 2 ? 2 : 1); i++) {
1102 if (lc->cu.pred_mode == MODE_INTRA) {
1103 ff_hevc_set_neighbour_available(s, x0, y0 + (i << log2_trafo_size_c), trafo_size_h, trafo_size_v);
1104 s->hpc.intra_pred[log2_trafo_size_c - 2](s, x0, y0 + (i << log2_trafo_size_c), 2);
1107 ff_hevc_hls_residual_coding(s, x0, y0 + (i << log2_trafo_size_c),
1108 log2_trafo_size_c, scan_idx_c, 2);
1110 if (lc->tu.cross_pf) {
1111 ptrdiff_t stride = s->frame->linesize[2];
1112 int hshift = s->ps.sps->hshift[2];
1113 int vshift = s->ps.sps->vshift[2];
1114 int16_t *coeffs_y = (int16_t*)lc->edge_emu_buffer;
1115 int16_t *coeffs = (int16_t*)lc->edge_emu_buffer2;
1116 int size = 1 << log2_trafo_size_c;
1118 uint8_t *dst = &s->frame->data[2][(y0 >> vshift) * stride +
1119 ((x0 >> hshift) << s->ps.sps->pixel_shift)];
1120 for (i = 0; i < (size * size); i++) {
1121 coeffs[i] = ((lc->tu.res_scale_val * coeffs_y[i]) >> 3);
1123 s->hevcdsp.add_residual[log2_trafo_size_c-2](dst, coeffs, stride);
1126 } else if (s->ps.sps->chroma_format_idc && blk_idx == 3) {
1127 int trafo_size_h = 1 << (log2_trafo_size + 1);
1128 int trafo_size_v = 1 << (log2_trafo_size + s->ps.sps->vshift[1]);
1129 for (i = 0; i < (s->ps.sps->chroma_format_idc == 2 ? 2 : 1); i++) {
1130 if (lc->cu.pred_mode == MODE_INTRA) {
1131 ff_hevc_set_neighbour_available(s, xBase, yBase + (i << log2_trafo_size),
1132 trafo_size_h, trafo_size_v);
1133 s->hpc.intra_pred[log2_trafo_size - 2](s, xBase, yBase + (i << log2_trafo_size), 1);
1136 ff_hevc_hls_residual_coding(s, xBase, yBase + (i << log2_trafo_size),
1137 log2_trafo_size, scan_idx_c, 1);
1139 for (i = 0; i < (s->ps.sps->chroma_format_idc == 2 ? 2 : 1); i++) {
1140 if (lc->cu.pred_mode == MODE_INTRA) {
1141 ff_hevc_set_neighbour_available(s, xBase, yBase + (i << log2_trafo_size),
1142 trafo_size_h, trafo_size_v);
1143 s->hpc.intra_pred[log2_trafo_size - 2](s, xBase, yBase + (i << log2_trafo_size), 2);
1146 ff_hevc_hls_residual_coding(s, xBase, yBase + (i << log2_trafo_size),
1147 log2_trafo_size, scan_idx_c, 2);
1150 } else if (s->ps.sps->chroma_format_idc && lc->cu.pred_mode == MODE_INTRA) {
1151 if (log2_trafo_size > 2 || s->ps.sps->chroma_format_idc == 3) {
1152 int trafo_size_h = 1 << (log2_trafo_size_c + s->ps.sps->hshift[1]);
1153 int trafo_size_v = 1 << (log2_trafo_size_c + s->ps.sps->vshift[1]);
1154 ff_hevc_set_neighbour_available(s, x0, y0, trafo_size_h, trafo_size_v);
1155 s->hpc.intra_pred[log2_trafo_size_c - 2](s, x0, y0, 1);
1156 s->hpc.intra_pred[log2_trafo_size_c - 2](s, x0, y0, 2);
1157 if (s->ps.sps->chroma_format_idc == 2) {
1158 ff_hevc_set_neighbour_available(s, x0, y0 + (1 << log2_trafo_size_c),
1159 trafo_size_h, trafo_size_v);
1160 s->hpc.intra_pred[log2_trafo_size_c - 2](s, x0, y0 + (1 << log2_trafo_size_c), 1);
1161 s->hpc.intra_pred[log2_trafo_size_c - 2](s, x0, y0 + (1 << log2_trafo_size_c), 2);
1163 } else if (blk_idx == 3) {
1164 int trafo_size_h = 1 << (log2_trafo_size + 1);
1165 int trafo_size_v = 1 << (log2_trafo_size + s->ps.sps->vshift[1]);
1166 ff_hevc_set_neighbour_available(s, xBase, yBase,
1167 trafo_size_h, trafo_size_v);
1168 s->hpc.intra_pred[log2_trafo_size - 2](s, xBase, yBase, 1);
1169 s->hpc.intra_pred[log2_trafo_size - 2](s, xBase, yBase, 2);
1170 if (s->ps.sps->chroma_format_idc == 2) {
1171 ff_hevc_set_neighbour_available(s, xBase, yBase + (1 << (log2_trafo_size)),
1172 trafo_size_h, trafo_size_v);
1173 s->hpc.intra_pred[log2_trafo_size - 2](s, xBase, yBase + (1 << (log2_trafo_size)), 1);
1174 s->hpc.intra_pred[log2_trafo_size - 2](s, xBase, yBase + (1 << (log2_trafo_size)), 2);
1182 static void set_deblocking_bypass(HEVCContext *s, int x0, int y0, int log2_cb_size)
1184 int cb_size = 1 << log2_cb_size;
1185 int log2_min_pu_size = s->ps.sps->log2_min_pu_size;
1187 int min_pu_width = s->ps.sps->min_pu_width;
1188 int x_end = FFMIN(x0 + cb_size, s->ps.sps->width);
1189 int y_end = FFMIN(y0 + cb_size, s->ps.sps->height);
1192 for (j = (y0 >> log2_min_pu_size); j < (y_end >> log2_min_pu_size); j++)
1193 for (i = (x0 >> log2_min_pu_size); i < (x_end >> log2_min_pu_size); i++)
1194 s->is_pcm[i + j * min_pu_width] = 2;
1197 static int hls_transform_tree(HEVCContext *s, int x0, int y0,
1198 int xBase, int yBase, int cb_xBase, int cb_yBase,
1199 int log2_cb_size, int log2_trafo_size,
1200 int trafo_depth, int blk_idx,
1201 const int *base_cbf_cb, const int *base_cbf_cr)
1203 HEVCLocalContext *lc = s->HEVClc;
1204 uint8_t split_transform_flag;
1209 cbf_cb[0] = base_cbf_cb[0];
1210 cbf_cb[1] = base_cbf_cb[1];
1211 cbf_cr[0] = base_cbf_cr[0];
1212 cbf_cr[1] = base_cbf_cr[1];
1214 if (lc->cu.intra_split_flag) {
1215 if (trafo_depth == 1) {
1216 lc->tu.intra_pred_mode = lc->pu.intra_pred_mode[blk_idx];
1217 if (s->ps.sps->chroma_format_idc == 3) {
1218 lc->tu.intra_pred_mode_c = lc->pu.intra_pred_mode_c[blk_idx];
1219 lc->tu.chroma_mode_c = lc->pu.chroma_mode_c[blk_idx];
1221 lc->tu.intra_pred_mode_c = lc->pu.intra_pred_mode_c[0];
1222 lc->tu.chroma_mode_c = lc->pu.chroma_mode_c[0];
1226 lc->tu.intra_pred_mode = lc->pu.intra_pred_mode[0];
1227 lc->tu.intra_pred_mode_c = lc->pu.intra_pred_mode_c[0];
1228 lc->tu.chroma_mode_c = lc->pu.chroma_mode_c[0];
1231 if (log2_trafo_size <= s->ps.sps->log2_max_trafo_size &&
1232 log2_trafo_size > s->ps.sps->log2_min_tb_size &&
1233 trafo_depth < lc->cu.max_trafo_depth &&
1234 !(lc->cu.intra_split_flag && trafo_depth == 0)) {
1235 split_transform_flag = ff_hevc_split_transform_flag_decode(s, log2_trafo_size);
1237 int inter_split = s->ps.sps->max_transform_hierarchy_depth_inter == 0 &&
1238 lc->cu.pred_mode == MODE_INTER &&
1239 lc->cu.part_mode != PART_2Nx2N &&
1242 split_transform_flag = log2_trafo_size > s->ps.sps->log2_max_trafo_size ||
1243 (lc->cu.intra_split_flag && trafo_depth == 0) ||
1247 if (s->ps.sps->chroma_format_idc && (log2_trafo_size > 2 || s->ps.sps->chroma_format_idc == 3)) {
1248 if (trafo_depth == 0 || cbf_cb[0]) {
1249 cbf_cb[0] = ff_hevc_cbf_cb_cr_decode(s, trafo_depth);
1250 if (s->ps.sps->chroma_format_idc == 2 && (!split_transform_flag || log2_trafo_size == 3)) {
1251 cbf_cb[1] = ff_hevc_cbf_cb_cr_decode(s, trafo_depth);
1255 if (trafo_depth == 0 || cbf_cr[0]) {
1256 cbf_cr[0] = ff_hevc_cbf_cb_cr_decode(s, trafo_depth);
1257 if (s->ps.sps->chroma_format_idc == 2 && (!split_transform_flag || log2_trafo_size == 3)) {
1258 cbf_cr[1] = ff_hevc_cbf_cb_cr_decode(s, trafo_depth);
1263 if (split_transform_flag) {
1264 const int trafo_size_split = 1 << (log2_trafo_size - 1);
1265 const int x1 = x0 + trafo_size_split;
1266 const int y1 = y0 + trafo_size_split;
1268 #define SUBDIVIDE(x, y, idx) \
1270 ret = hls_transform_tree(s, x, y, x0, y0, cb_xBase, cb_yBase, log2_cb_size, \
1271 log2_trafo_size - 1, trafo_depth + 1, idx, \
1277 SUBDIVIDE(x0, y0, 0);
1278 SUBDIVIDE(x1, y0, 1);
1279 SUBDIVIDE(x0, y1, 2);
1280 SUBDIVIDE(x1, y1, 3);
1284 int min_tu_size = 1 << s->ps.sps->log2_min_tb_size;
1285 int log2_min_tu_size = s->ps.sps->log2_min_tb_size;
1286 int min_tu_width = s->ps.sps->min_tb_width;
1289 if (lc->cu.pred_mode == MODE_INTRA || trafo_depth != 0 ||
1290 cbf_cb[0] || cbf_cr[0] ||
1291 (s->ps.sps->chroma_format_idc == 2 && (cbf_cb[1] || cbf_cr[1]))) {
1292 cbf_luma = ff_hevc_cbf_luma_decode(s, trafo_depth);
1295 ret = hls_transform_unit(s, x0, y0, xBase, yBase, cb_xBase, cb_yBase,
1296 log2_cb_size, log2_trafo_size,
1297 blk_idx, cbf_luma, cbf_cb, cbf_cr);
1300 // TODO: store cbf_luma somewhere else
1303 for (i = 0; i < (1 << log2_trafo_size); i += min_tu_size)
1304 for (j = 0; j < (1 << log2_trafo_size); j += min_tu_size) {
1305 int x_tu = (x0 + j) >> log2_min_tu_size;
1306 int y_tu = (y0 + i) >> log2_min_tu_size;
1307 s->cbf_luma[y_tu * min_tu_width + x_tu] = 1;
1310 if (!s->sh.disable_deblocking_filter_flag) {
1311 ff_hevc_deblocking_boundary_strengths(s, x0, y0, log2_trafo_size);
1312 if (s->ps.pps->transquant_bypass_enable_flag &&
1313 lc->cu.cu_transquant_bypass_flag)
1314 set_deblocking_bypass(s, x0, y0, log2_trafo_size);
1320 static int hls_pcm_sample(HEVCContext *s, int x0, int y0, int log2_cb_size)
1322 HEVCLocalContext *lc = s->HEVClc;
1324 int cb_size = 1 << log2_cb_size;
1325 ptrdiff_t stride0 = s->frame->linesize[0];
1326 ptrdiff_t stride1 = s->frame->linesize[1];
1327 ptrdiff_t stride2 = s->frame->linesize[2];
1328 uint8_t *dst0 = &s->frame->data[0][y0 * stride0 + (x0 << s->ps.sps->pixel_shift)];
1329 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)];
1330 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)];
1332 int length = cb_size * cb_size * s->ps.sps->pcm.bit_depth +
1333 (((cb_size >> s->ps.sps->hshift[1]) * (cb_size >> s->ps.sps->vshift[1])) +
1334 ((cb_size >> s->ps.sps->hshift[2]) * (cb_size >> s->ps.sps->vshift[2]))) *
1335 s->ps.sps->pcm.bit_depth_chroma;
1336 const uint8_t *pcm = skip_bytes(&lc->cc, (length + 7) >> 3);
1339 if (!s->sh.disable_deblocking_filter_flag)
1340 ff_hevc_deblocking_boundary_strengths(s, x0, y0, log2_cb_size);
1342 ret = init_get_bits(&gb, pcm, length);
1346 s->hevcdsp.put_pcm(dst0, stride0, cb_size, cb_size, &gb, s->ps.sps->pcm.bit_depth);
1347 if (s->ps.sps->chroma_format_idc) {
1348 s->hevcdsp.put_pcm(dst1, stride1,
1349 cb_size >> s->ps.sps->hshift[1],
1350 cb_size >> s->ps.sps->vshift[1],
1351 &gb, s->ps.sps->pcm.bit_depth_chroma);
1352 s->hevcdsp.put_pcm(dst2, stride2,
1353 cb_size >> s->ps.sps->hshift[2],
1354 cb_size >> s->ps.sps->vshift[2],
1355 &gb, s->ps.sps->pcm.bit_depth_chroma);
1362 * 8.5.3.2.2.1 Luma sample unidirectional interpolation process
1364 * @param s HEVC decoding context
1365 * @param dst target buffer for block data at block position
1366 * @param dststride stride of the dst buffer
1367 * @param ref reference picture buffer at origin (0, 0)
1368 * @param mv motion vector (relative to block position) to get pixel data from
1369 * @param x_off horizontal position of block from origin (0, 0)
1370 * @param y_off vertical position of block from origin (0, 0)
1371 * @param block_w width of block
1372 * @param block_h height of block
1373 * @param luma_weight weighting factor applied to the luma prediction
1374 * @param luma_offset additive offset applied to the luma prediction value
1377 static void luma_mc_uni(HEVCContext *s, uint8_t *dst, ptrdiff_t dststride,
1378 AVFrame *ref, const Mv *mv, int x_off, int y_off,
1379 int block_w, int block_h, int luma_weight, int luma_offset)
1381 HEVCLocalContext *lc = s->HEVClc;
1382 uint8_t *src = ref->data[0];
1383 ptrdiff_t srcstride = ref->linesize[0];
1384 int pic_width = s->ps.sps->width;
1385 int pic_height = s->ps.sps->height;
1388 int weight_flag = (s->sh.slice_type == HEVC_SLICE_P && s->ps.pps->weighted_pred_flag) ||
1389 (s->sh.slice_type == HEVC_SLICE_B && s->ps.pps->weighted_bipred_flag);
1390 int idx = ff_hevc_pel_weight[block_w];
1392 x_off += mv->x >> 2;
1393 y_off += mv->y >> 2;
1394 src += y_off * srcstride + (x_off * (1 << s->ps.sps->pixel_shift));
1396 if (x_off < QPEL_EXTRA_BEFORE || y_off < QPEL_EXTRA_AFTER ||
1397 x_off >= pic_width - block_w - QPEL_EXTRA_AFTER ||
1398 y_off >= pic_height - block_h - QPEL_EXTRA_AFTER) {
1399 const ptrdiff_t edge_emu_stride = EDGE_EMU_BUFFER_STRIDE << s->ps.sps->pixel_shift;
1400 int offset = QPEL_EXTRA_BEFORE * srcstride + (QPEL_EXTRA_BEFORE << s->ps.sps->pixel_shift);
1401 int buf_offset = QPEL_EXTRA_BEFORE * edge_emu_stride + (QPEL_EXTRA_BEFORE << s->ps.sps->pixel_shift);
1403 s->vdsp.emulated_edge_mc(lc->edge_emu_buffer, src - offset,
1404 edge_emu_stride, srcstride,
1405 block_w + QPEL_EXTRA,
1406 block_h + QPEL_EXTRA,
1407 x_off - QPEL_EXTRA_BEFORE, y_off - QPEL_EXTRA_BEFORE,
1408 pic_width, pic_height);
1409 src = lc->edge_emu_buffer + buf_offset;
1410 srcstride = edge_emu_stride;
1414 s->hevcdsp.put_hevc_qpel_uni[idx][!!my][!!mx](dst, dststride, src, srcstride,
1415 block_h, mx, my, block_w);
1417 s->hevcdsp.put_hevc_qpel_uni_w[idx][!!my][!!mx](dst, dststride, src, srcstride,
1418 block_h, s->sh.luma_log2_weight_denom,
1419 luma_weight, luma_offset, mx, my, block_w);
1423 * 8.5.3.2.2.1 Luma sample bidirectional interpolation process
1425 * @param s HEVC decoding context
1426 * @param dst target buffer for block data at block position
1427 * @param dststride stride of the dst buffer
1428 * @param ref0 reference picture0 buffer at origin (0, 0)
1429 * @param mv0 motion vector0 (relative to block position) to get pixel data from
1430 * @param x_off horizontal position of block from origin (0, 0)
1431 * @param y_off vertical position of block from origin (0, 0)
1432 * @param block_w width of block
1433 * @param block_h height of block
1434 * @param ref1 reference picture1 buffer at origin (0, 0)
1435 * @param mv1 motion vector1 (relative to block position) to get pixel data from
1436 * @param current_mv current motion vector structure
1438 static void luma_mc_bi(HEVCContext *s, uint8_t *dst, ptrdiff_t dststride,
1439 AVFrame *ref0, const Mv *mv0, int x_off, int y_off,
1440 int block_w, int block_h, AVFrame *ref1, const Mv *mv1, struct MvField *current_mv)
1442 HEVCLocalContext *lc = s->HEVClc;
1443 ptrdiff_t src0stride = ref0->linesize[0];
1444 ptrdiff_t src1stride = ref1->linesize[0];
1445 int pic_width = s->ps.sps->width;
1446 int pic_height = s->ps.sps->height;
1447 int mx0 = mv0->x & 3;
1448 int my0 = mv0->y & 3;
1449 int mx1 = mv1->x & 3;
1450 int my1 = mv1->y & 3;
1451 int weight_flag = (s->sh.slice_type == HEVC_SLICE_P && s->ps.pps->weighted_pred_flag) ||
1452 (s->sh.slice_type == HEVC_SLICE_B && s->ps.pps->weighted_bipred_flag);
1453 int x_off0 = x_off + (mv0->x >> 2);
1454 int y_off0 = y_off + (mv0->y >> 2);
1455 int x_off1 = x_off + (mv1->x >> 2);
1456 int y_off1 = y_off + (mv1->y >> 2);
1457 int idx = ff_hevc_pel_weight[block_w];
1459 uint8_t *src0 = ref0->data[0] + y_off0 * src0stride + (int)((unsigned)x_off0 << s->ps.sps->pixel_shift);
1460 uint8_t *src1 = ref1->data[0] + y_off1 * src1stride + (int)((unsigned)x_off1 << s->ps.sps->pixel_shift);
1462 if (x_off0 < QPEL_EXTRA_BEFORE || y_off0 < QPEL_EXTRA_AFTER ||
1463 x_off0 >= pic_width - block_w - QPEL_EXTRA_AFTER ||
1464 y_off0 >= pic_height - block_h - QPEL_EXTRA_AFTER) {
1465 const ptrdiff_t edge_emu_stride = EDGE_EMU_BUFFER_STRIDE << s->ps.sps->pixel_shift;
1466 int offset = QPEL_EXTRA_BEFORE * src0stride + (QPEL_EXTRA_BEFORE << s->ps.sps->pixel_shift);
1467 int buf_offset = QPEL_EXTRA_BEFORE * edge_emu_stride + (QPEL_EXTRA_BEFORE << s->ps.sps->pixel_shift);
1469 s->vdsp.emulated_edge_mc(lc->edge_emu_buffer, src0 - offset,
1470 edge_emu_stride, src0stride,
1471 block_w + QPEL_EXTRA,
1472 block_h + QPEL_EXTRA,
1473 x_off0 - QPEL_EXTRA_BEFORE, y_off0 - QPEL_EXTRA_BEFORE,
1474 pic_width, pic_height);
1475 src0 = lc->edge_emu_buffer + buf_offset;
1476 src0stride = edge_emu_stride;
1479 if (x_off1 < QPEL_EXTRA_BEFORE || y_off1 < QPEL_EXTRA_AFTER ||
1480 x_off1 >= pic_width - block_w - QPEL_EXTRA_AFTER ||
1481 y_off1 >= pic_height - block_h - QPEL_EXTRA_AFTER) {
1482 const ptrdiff_t edge_emu_stride = EDGE_EMU_BUFFER_STRIDE << s->ps.sps->pixel_shift;
1483 int offset = QPEL_EXTRA_BEFORE * src1stride + (QPEL_EXTRA_BEFORE << s->ps.sps->pixel_shift);
1484 int buf_offset = QPEL_EXTRA_BEFORE * edge_emu_stride + (QPEL_EXTRA_BEFORE << s->ps.sps->pixel_shift);
1486 s->vdsp.emulated_edge_mc(lc->edge_emu_buffer2, src1 - offset,
1487 edge_emu_stride, src1stride,
1488 block_w + QPEL_EXTRA,
1489 block_h + QPEL_EXTRA,
1490 x_off1 - QPEL_EXTRA_BEFORE, y_off1 - QPEL_EXTRA_BEFORE,
1491 pic_width, pic_height);
1492 src1 = lc->edge_emu_buffer2 + buf_offset;
1493 src1stride = edge_emu_stride;
1496 s->hevcdsp.put_hevc_qpel[idx][!!my0][!!mx0](lc->tmp, src0, src0stride,
1497 block_h, mx0, my0, block_w);
1499 s->hevcdsp.put_hevc_qpel_bi[idx][!!my1][!!mx1](dst, dststride, src1, src1stride, lc->tmp,
1500 block_h, mx1, my1, block_w);
1502 s->hevcdsp.put_hevc_qpel_bi_w[idx][!!my1][!!mx1](dst, dststride, src1, src1stride, lc->tmp,
1503 block_h, s->sh.luma_log2_weight_denom,
1504 s->sh.luma_weight_l0[current_mv->ref_idx[0]],
1505 s->sh.luma_weight_l1[current_mv->ref_idx[1]],
1506 s->sh.luma_offset_l0[current_mv->ref_idx[0]],
1507 s->sh.luma_offset_l1[current_mv->ref_idx[1]],
1513 * 8.5.3.2.2.2 Chroma sample uniprediction interpolation process
1515 * @param s HEVC decoding context
1516 * @param dst1 target buffer for block data at block position (U plane)
1517 * @param dst2 target buffer for block data at block position (V plane)
1518 * @param dststride stride of the dst1 and dst2 buffers
1519 * @param ref reference picture buffer at origin (0, 0)
1520 * @param mv motion vector (relative to block position) to get pixel data from
1521 * @param x_off horizontal position of block from origin (0, 0)
1522 * @param y_off vertical position of block from origin (0, 0)
1523 * @param block_w width of block
1524 * @param block_h height of block
1525 * @param chroma_weight weighting factor applied to the chroma prediction
1526 * @param chroma_offset additive offset applied to the chroma prediction value
1529 static void chroma_mc_uni(HEVCContext *s, uint8_t *dst0,
1530 ptrdiff_t dststride, uint8_t *src0, ptrdiff_t srcstride, int reflist,
1531 int x_off, int y_off, int block_w, int block_h, struct MvField *current_mv, int chroma_weight, int chroma_offset)
1533 HEVCLocalContext *lc = s->HEVClc;
1534 int pic_width = s->ps.sps->width >> s->ps.sps->hshift[1];
1535 int pic_height = s->ps.sps->height >> s->ps.sps->vshift[1];
1536 const Mv *mv = ¤t_mv->mv[reflist];
1537 int weight_flag = (s->sh.slice_type == HEVC_SLICE_P && s->ps.pps->weighted_pred_flag) ||
1538 (s->sh.slice_type == HEVC_SLICE_B && s->ps.pps->weighted_bipred_flag);
1539 int idx = ff_hevc_pel_weight[block_w];
1540 int hshift = s->ps.sps->hshift[1];
1541 int vshift = s->ps.sps->vshift[1];
1542 intptr_t mx = av_mod_uintp2(mv->x, 2 + hshift);
1543 intptr_t my = av_mod_uintp2(mv->y, 2 + vshift);
1544 intptr_t _mx = mx << (1 - hshift);
1545 intptr_t _my = my << (1 - vshift);
1547 x_off += mv->x >> (2 + hshift);
1548 y_off += mv->y >> (2 + vshift);
1549 src0 += y_off * srcstride + (x_off * (1 << s->ps.sps->pixel_shift));
1551 if (x_off < EPEL_EXTRA_BEFORE || y_off < EPEL_EXTRA_AFTER ||
1552 x_off >= pic_width - block_w - EPEL_EXTRA_AFTER ||
1553 y_off >= pic_height - block_h - EPEL_EXTRA_AFTER) {
1554 const int edge_emu_stride = EDGE_EMU_BUFFER_STRIDE << s->ps.sps->pixel_shift;
1555 int offset0 = EPEL_EXTRA_BEFORE * (srcstride + (1 << s->ps.sps->pixel_shift));
1556 int buf_offset0 = EPEL_EXTRA_BEFORE *
1557 (edge_emu_stride + (1 << s->ps.sps->pixel_shift));
1558 s->vdsp.emulated_edge_mc(lc->edge_emu_buffer, src0 - offset0,
1559 edge_emu_stride, srcstride,
1560 block_w + EPEL_EXTRA, block_h + EPEL_EXTRA,
1561 x_off - EPEL_EXTRA_BEFORE,
1562 y_off - EPEL_EXTRA_BEFORE,
1563 pic_width, pic_height);
1565 src0 = lc->edge_emu_buffer + buf_offset0;
1566 srcstride = edge_emu_stride;
1569 s->hevcdsp.put_hevc_epel_uni[idx][!!my][!!mx](dst0, dststride, src0, srcstride,
1570 block_h, _mx, _my, block_w);
1572 s->hevcdsp.put_hevc_epel_uni_w[idx][!!my][!!mx](dst0, dststride, src0, srcstride,
1573 block_h, s->sh.chroma_log2_weight_denom,
1574 chroma_weight, chroma_offset, _mx, _my, block_w);
1578 * 8.5.3.2.2.2 Chroma sample bidirectional interpolation process
1580 * @param s HEVC decoding context
1581 * @param dst target buffer for block data at block position
1582 * @param dststride stride of the dst buffer
1583 * @param ref0 reference picture0 buffer at origin (0, 0)
1584 * @param mv0 motion vector0 (relative to block position) to get pixel data from
1585 * @param x_off horizontal position of block from origin (0, 0)
1586 * @param y_off vertical position of block from origin (0, 0)
1587 * @param block_w width of block
1588 * @param block_h height of block
1589 * @param ref1 reference picture1 buffer at origin (0, 0)
1590 * @param mv1 motion vector1 (relative to block position) to get pixel data from
1591 * @param current_mv current motion vector structure
1592 * @param cidx chroma component(cb, cr)
1594 static void chroma_mc_bi(HEVCContext *s, uint8_t *dst0, ptrdiff_t dststride, AVFrame *ref0, AVFrame *ref1,
1595 int x_off, int y_off, int block_w, int block_h, struct MvField *current_mv, int cidx)
1597 HEVCLocalContext *lc = s->HEVClc;
1598 uint8_t *src1 = ref0->data[cidx+1];
1599 uint8_t *src2 = ref1->data[cidx+1];
1600 ptrdiff_t src1stride = ref0->linesize[cidx+1];
1601 ptrdiff_t src2stride = ref1->linesize[cidx+1];
1602 int weight_flag = (s->sh.slice_type == HEVC_SLICE_P && s->ps.pps->weighted_pred_flag) ||
1603 (s->sh.slice_type == HEVC_SLICE_B && s->ps.pps->weighted_bipred_flag);
1604 int pic_width = s->ps.sps->width >> s->ps.sps->hshift[1];
1605 int pic_height = s->ps.sps->height >> s->ps.sps->vshift[1];
1606 Mv *mv0 = ¤t_mv->mv[0];
1607 Mv *mv1 = ¤t_mv->mv[1];
1608 int hshift = s->ps.sps->hshift[1];
1609 int vshift = s->ps.sps->vshift[1];
1611 intptr_t mx0 = av_mod_uintp2(mv0->x, 2 + hshift);
1612 intptr_t my0 = av_mod_uintp2(mv0->y, 2 + vshift);
1613 intptr_t mx1 = av_mod_uintp2(mv1->x, 2 + hshift);
1614 intptr_t my1 = av_mod_uintp2(mv1->y, 2 + vshift);
1615 intptr_t _mx0 = mx0 << (1 - hshift);
1616 intptr_t _my0 = my0 << (1 - vshift);
1617 intptr_t _mx1 = mx1 << (1 - hshift);
1618 intptr_t _my1 = my1 << (1 - vshift);
1620 int x_off0 = x_off + (mv0->x >> (2 + hshift));
1621 int y_off0 = y_off + (mv0->y >> (2 + vshift));
1622 int x_off1 = x_off + (mv1->x >> (2 + hshift));
1623 int y_off1 = y_off + (mv1->y >> (2 + vshift));
1624 int idx = ff_hevc_pel_weight[block_w];
1625 src1 += y_off0 * src1stride + (int)((unsigned)x_off0 << s->ps.sps->pixel_shift);
1626 src2 += y_off1 * src2stride + (int)((unsigned)x_off1 << s->ps.sps->pixel_shift);
1628 if (x_off0 < EPEL_EXTRA_BEFORE || y_off0 < EPEL_EXTRA_AFTER ||
1629 x_off0 >= pic_width - block_w - EPEL_EXTRA_AFTER ||
1630 y_off0 >= pic_height - block_h - EPEL_EXTRA_AFTER) {
1631 const int edge_emu_stride = EDGE_EMU_BUFFER_STRIDE << s->ps.sps->pixel_shift;
1632 int offset1 = EPEL_EXTRA_BEFORE * (src1stride + (1 << s->ps.sps->pixel_shift));
1633 int buf_offset1 = EPEL_EXTRA_BEFORE *
1634 (edge_emu_stride + (1 << s->ps.sps->pixel_shift));
1636 s->vdsp.emulated_edge_mc(lc->edge_emu_buffer, src1 - offset1,
1637 edge_emu_stride, src1stride,
1638 block_w + EPEL_EXTRA, block_h + EPEL_EXTRA,
1639 x_off0 - EPEL_EXTRA_BEFORE,
1640 y_off0 - EPEL_EXTRA_BEFORE,
1641 pic_width, pic_height);
1643 src1 = lc->edge_emu_buffer + buf_offset1;
1644 src1stride = edge_emu_stride;
1647 if (x_off1 < EPEL_EXTRA_BEFORE || y_off1 < EPEL_EXTRA_AFTER ||
1648 x_off1 >= pic_width - block_w - EPEL_EXTRA_AFTER ||
1649 y_off1 >= pic_height - block_h - EPEL_EXTRA_AFTER) {
1650 const int edge_emu_stride = EDGE_EMU_BUFFER_STRIDE << s->ps.sps->pixel_shift;
1651 int offset1 = EPEL_EXTRA_BEFORE * (src2stride + (1 << s->ps.sps->pixel_shift));
1652 int buf_offset1 = EPEL_EXTRA_BEFORE *
1653 (edge_emu_stride + (1 << s->ps.sps->pixel_shift));
1655 s->vdsp.emulated_edge_mc(lc->edge_emu_buffer2, src2 - offset1,
1656 edge_emu_stride, src2stride,
1657 block_w + EPEL_EXTRA, block_h + EPEL_EXTRA,
1658 x_off1 - EPEL_EXTRA_BEFORE,
1659 y_off1 - EPEL_EXTRA_BEFORE,
1660 pic_width, pic_height);
1662 src2 = lc->edge_emu_buffer2 + buf_offset1;
1663 src2stride = edge_emu_stride;
1666 s->hevcdsp.put_hevc_epel[idx][!!my0][!!mx0](lc->tmp, src1, src1stride,
1667 block_h, _mx0, _my0, block_w);
1669 s->hevcdsp.put_hevc_epel_bi[idx][!!my1][!!mx1](dst0, s->frame->linesize[cidx+1],
1670 src2, src2stride, lc->tmp,
1671 block_h, _mx1, _my1, block_w);
1673 s->hevcdsp.put_hevc_epel_bi_w[idx][!!my1][!!mx1](dst0, s->frame->linesize[cidx+1],
1674 src2, src2stride, lc->tmp,
1676 s->sh.chroma_log2_weight_denom,
1677 s->sh.chroma_weight_l0[current_mv->ref_idx[0]][cidx],
1678 s->sh.chroma_weight_l1[current_mv->ref_idx[1]][cidx],
1679 s->sh.chroma_offset_l0[current_mv->ref_idx[0]][cidx],
1680 s->sh.chroma_offset_l1[current_mv->ref_idx[1]][cidx],
1681 _mx1, _my1, block_w);
1684 static void hevc_await_progress(HEVCContext *s, HEVCFrame *ref,
1685 const Mv *mv, int y0, int height)
1687 int y = FFMAX(0, (mv->y >> 2) + y0 + height + 9);
1689 if (s->threads_type == FF_THREAD_FRAME )
1690 ff_thread_await_progress(&ref->tf, y, 0);
1693 static void hevc_luma_mv_mvp_mode(HEVCContext *s, int x0, int y0, int nPbW,
1694 int nPbH, int log2_cb_size, int part_idx,
1695 int merge_idx, MvField *mv)
1697 HEVCLocalContext *lc = s->HEVClc;
1698 enum InterPredIdc inter_pred_idc = PRED_L0;
1701 ff_hevc_set_neighbour_available(s, x0, y0, nPbW, nPbH);
1703 if (s->sh.slice_type == HEVC_SLICE_B)
1704 inter_pred_idc = ff_hevc_inter_pred_idc_decode(s, nPbW, nPbH);
1706 if (inter_pred_idc != PRED_L1) {
1707 if (s->sh.nb_refs[L0])
1708 mv->ref_idx[0]= ff_hevc_ref_idx_lx_decode(s, s->sh.nb_refs[L0]);
1710 mv->pred_flag = PF_L0;
1711 ff_hevc_hls_mvd_coding(s, x0, y0, 0);
1712 mvp_flag = ff_hevc_mvp_lx_flag_decode(s);
1713 ff_hevc_luma_mv_mvp_mode(s, x0, y0, nPbW, nPbH, log2_cb_size,
1714 part_idx, merge_idx, mv, mvp_flag, 0);
1715 mv->mv[0].x += lc->pu.mvd.x;
1716 mv->mv[0].y += lc->pu.mvd.y;
1719 if (inter_pred_idc != PRED_L0) {
1720 if (s->sh.nb_refs[L1])
1721 mv->ref_idx[1]= ff_hevc_ref_idx_lx_decode(s, s->sh.nb_refs[L1]);
1723 if (s->sh.mvd_l1_zero_flag == 1 && inter_pred_idc == PRED_BI) {
1724 AV_ZERO32(&lc->pu.mvd);
1726 ff_hevc_hls_mvd_coding(s, x0, y0, 1);
1729 mv->pred_flag += PF_L1;
1730 mvp_flag = ff_hevc_mvp_lx_flag_decode(s);
1731 ff_hevc_luma_mv_mvp_mode(s, x0, y0, nPbW, nPbH, log2_cb_size,
1732 part_idx, merge_idx, mv, mvp_flag, 1);
1733 mv->mv[1].x += lc->pu.mvd.x;
1734 mv->mv[1].y += lc->pu.mvd.y;
1738 static void hls_prediction_unit(HEVCContext *s, int x0, int y0,
1740 int log2_cb_size, int partIdx, int idx)
1742 #define POS(c_idx, x, y) \
1743 &s->frame->data[c_idx][((y) >> s->ps.sps->vshift[c_idx]) * s->frame->linesize[c_idx] + \
1744 (((x) >> s->ps.sps->hshift[c_idx]) << s->ps.sps->pixel_shift)]
1745 HEVCLocalContext *lc = s->HEVClc;
1747 struct MvField current_mv = {{{ 0 }}};
1749 int min_pu_width = s->ps.sps->min_pu_width;
1751 MvField *tab_mvf = s->ref->tab_mvf;
1752 RefPicList *refPicList = s->ref->refPicList;
1753 HEVCFrame *ref0 = NULL, *ref1 = NULL;
1754 uint8_t *dst0 = POS(0, x0, y0);
1755 uint8_t *dst1 = POS(1, x0, y0);
1756 uint8_t *dst2 = POS(2, x0, y0);
1757 int log2_min_cb_size = s->ps.sps->log2_min_cb_size;
1758 int min_cb_width = s->ps.sps->min_cb_width;
1759 int x_cb = x0 >> log2_min_cb_size;
1760 int y_cb = y0 >> log2_min_cb_size;
1764 int skip_flag = SAMPLE_CTB(s->skip_flag, x_cb, y_cb);
1767 lc->pu.merge_flag = ff_hevc_merge_flag_decode(s);
1769 if (skip_flag || lc->pu.merge_flag) {
1770 if (s->sh.max_num_merge_cand > 1)
1771 merge_idx = ff_hevc_merge_idx_decode(s);
1775 ff_hevc_luma_mv_merge_mode(s, x0, y0, nPbW, nPbH, log2_cb_size,
1776 partIdx, merge_idx, ¤t_mv);
1778 hevc_luma_mv_mvp_mode(s, x0, y0, nPbW, nPbH, log2_cb_size,
1779 partIdx, merge_idx, ¤t_mv);
1782 x_pu = x0 >> s->ps.sps->log2_min_pu_size;
1783 y_pu = y0 >> s->ps.sps->log2_min_pu_size;
1785 for (j = 0; j < nPbH >> s->ps.sps->log2_min_pu_size; j++)
1786 for (i = 0; i < nPbW >> s->ps.sps->log2_min_pu_size; i++)
1787 tab_mvf[(y_pu + j) * min_pu_width + x_pu + i] = current_mv;
1789 if (current_mv.pred_flag & PF_L0) {
1790 ref0 = refPicList[0].ref[current_mv.ref_idx[0]];
1793 hevc_await_progress(s, ref0, ¤t_mv.mv[0], y0, nPbH);
1795 if (current_mv.pred_flag & PF_L1) {
1796 ref1 = refPicList[1].ref[current_mv.ref_idx[1]];
1799 hevc_await_progress(s, ref1, ¤t_mv.mv[1], y0, nPbH);
1802 if (current_mv.pred_flag == PF_L0) {
1803 int x0_c = x0 >> s->ps.sps->hshift[1];
1804 int y0_c = y0 >> s->ps.sps->vshift[1];
1805 int nPbW_c = nPbW >> s->ps.sps->hshift[1];
1806 int nPbH_c = nPbH >> s->ps.sps->vshift[1];
1808 luma_mc_uni(s, dst0, s->frame->linesize[0], ref0->frame,
1809 ¤t_mv.mv[0], x0, y0, nPbW, nPbH,
1810 s->sh.luma_weight_l0[current_mv.ref_idx[0]],
1811 s->sh.luma_offset_l0[current_mv.ref_idx[0]]);
1813 if (s->ps.sps->chroma_format_idc) {
1814 chroma_mc_uni(s, dst1, s->frame->linesize[1], ref0->frame->data[1], ref0->frame->linesize[1],
1815 0, x0_c, y0_c, nPbW_c, nPbH_c, ¤t_mv,
1816 s->sh.chroma_weight_l0[current_mv.ref_idx[0]][0], s->sh.chroma_offset_l0[current_mv.ref_idx[0]][0]);
1817 chroma_mc_uni(s, dst2, s->frame->linesize[2], ref0->frame->data[2], ref0->frame->linesize[2],
1818 0, x0_c, y0_c, nPbW_c, nPbH_c, ¤t_mv,
1819 s->sh.chroma_weight_l0[current_mv.ref_idx[0]][1], s->sh.chroma_offset_l0[current_mv.ref_idx[0]][1]);
1821 } else if (current_mv.pred_flag == PF_L1) {
1822 int x0_c = x0 >> s->ps.sps->hshift[1];
1823 int y0_c = y0 >> s->ps.sps->vshift[1];
1824 int nPbW_c = nPbW >> s->ps.sps->hshift[1];
1825 int nPbH_c = nPbH >> s->ps.sps->vshift[1];
1827 luma_mc_uni(s, dst0, s->frame->linesize[0], ref1->frame,
1828 ¤t_mv.mv[1], x0, y0, nPbW, nPbH,
1829 s->sh.luma_weight_l1[current_mv.ref_idx[1]],
1830 s->sh.luma_offset_l1[current_mv.ref_idx[1]]);
1832 if (s->ps.sps->chroma_format_idc) {
1833 chroma_mc_uni(s, dst1, s->frame->linesize[1], ref1->frame->data[1], ref1->frame->linesize[1],
1834 1, x0_c, y0_c, nPbW_c, nPbH_c, ¤t_mv,
1835 s->sh.chroma_weight_l1[current_mv.ref_idx[1]][0], s->sh.chroma_offset_l1[current_mv.ref_idx[1]][0]);
1837 chroma_mc_uni(s, dst2, s->frame->linesize[2], ref1->frame->data[2], ref1->frame->linesize[2],
1838 1, x0_c, y0_c, nPbW_c, nPbH_c, ¤t_mv,
1839 s->sh.chroma_weight_l1[current_mv.ref_idx[1]][1], s->sh.chroma_offset_l1[current_mv.ref_idx[1]][1]);
1841 } else if (current_mv.pred_flag == PF_BI) {
1842 int x0_c = x0 >> s->ps.sps->hshift[1];
1843 int y0_c = y0 >> s->ps.sps->vshift[1];
1844 int nPbW_c = nPbW >> s->ps.sps->hshift[1];
1845 int nPbH_c = nPbH >> s->ps.sps->vshift[1];
1847 luma_mc_bi(s, dst0, s->frame->linesize[0], ref0->frame,
1848 ¤t_mv.mv[0], x0, y0, nPbW, nPbH,
1849 ref1->frame, ¤t_mv.mv[1], ¤t_mv);
1851 if (s->ps.sps->chroma_format_idc) {
1852 chroma_mc_bi(s, dst1, s->frame->linesize[1], ref0->frame, ref1->frame,
1853 x0_c, y0_c, nPbW_c, nPbH_c, ¤t_mv, 0);
1855 chroma_mc_bi(s, dst2, s->frame->linesize[2], ref0->frame, ref1->frame,
1856 x0_c, y0_c, nPbW_c, nPbH_c, ¤t_mv, 1);
1864 static int luma_intra_pred_mode(HEVCContext *s, int x0, int y0, int pu_size,
1865 int prev_intra_luma_pred_flag)
1867 HEVCLocalContext *lc = s->HEVClc;
1868 int x_pu = x0 >> s->ps.sps->log2_min_pu_size;
1869 int y_pu = y0 >> s->ps.sps->log2_min_pu_size;
1870 int min_pu_width = s->ps.sps->min_pu_width;
1871 int size_in_pus = pu_size >> s->ps.sps->log2_min_pu_size;
1872 int x0b = av_mod_uintp2(x0, s->ps.sps->log2_ctb_size);
1873 int y0b = av_mod_uintp2(y0, s->ps.sps->log2_ctb_size);
1875 int cand_up = (lc->ctb_up_flag || y0b) ?
1876 s->tab_ipm[(y_pu - 1) * min_pu_width + x_pu] : INTRA_DC;
1877 int cand_left = (lc->ctb_left_flag || x0b) ?
1878 s->tab_ipm[y_pu * min_pu_width + x_pu - 1] : INTRA_DC;
1880 int y_ctb = (y0 >> (s->ps.sps->log2_ctb_size)) << (s->ps.sps->log2_ctb_size);
1882 MvField *tab_mvf = s->ref->tab_mvf;
1883 int intra_pred_mode;
1887 // intra_pred_mode prediction does not cross vertical CTB boundaries
1888 if ((y0 - 1) < y_ctb)
1891 if (cand_left == cand_up) {
1892 if (cand_left < 2) {
1893 candidate[0] = INTRA_PLANAR;
1894 candidate[1] = INTRA_DC;
1895 candidate[2] = INTRA_ANGULAR_26;
1897 candidate[0] = cand_left;
1898 candidate[1] = 2 + ((cand_left - 2 - 1 + 32) & 31);
1899 candidate[2] = 2 + ((cand_left - 2 + 1) & 31);
1902 candidate[0] = cand_left;
1903 candidate[1] = cand_up;
1904 if (candidate[0] != INTRA_PLANAR && candidate[1] != INTRA_PLANAR) {
1905 candidate[2] = INTRA_PLANAR;
1906 } else if (candidate[0] != INTRA_DC && candidate[1] != INTRA_DC) {
1907 candidate[2] = INTRA_DC;
1909 candidate[2] = INTRA_ANGULAR_26;
1913 if (prev_intra_luma_pred_flag) {
1914 intra_pred_mode = candidate[lc->pu.mpm_idx];
1916 if (candidate[0] > candidate[1])
1917 FFSWAP(uint8_t, candidate[0], candidate[1]);
1918 if (candidate[0] > candidate[2])
1919 FFSWAP(uint8_t, candidate[0], candidate[2]);
1920 if (candidate[1] > candidate[2])
1921 FFSWAP(uint8_t, candidate[1], candidate[2]);
1923 intra_pred_mode = lc->pu.rem_intra_luma_pred_mode;
1924 for (i = 0; i < 3; i++)
1925 if (intra_pred_mode >= candidate[i])
1929 /* write the intra prediction units into the mv array */
1932 for (i = 0; i < size_in_pus; i++) {
1933 memset(&s->tab_ipm[(y_pu + i) * min_pu_width + x_pu],
1934 intra_pred_mode, size_in_pus);
1936 for (j = 0; j < size_in_pus; j++) {
1937 tab_mvf[(y_pu + j) * min_pu_width + x_pu + i].pred_flag = PF_INTRA;
1941 return intra_pred_mode;
1944 static av_always_inline void set_ct_depth(HEVCContext *s, int x0, int y0,
1945 int log2_cb_size, int ct_depth)
1947 int length = (1 << log2_cb_size) >> s->ps.sps->log2_min_cb_size;
1948 int x_cb = x0 >> s->ps.sps->log2_min_cb_size;
1949 int y_cb = y0 >> s->ps.sps->log2_min_cb_size;
1952 for (y = 0; y < length; y++)
1953 memset(&s->tab_ct_depth[(y_cb + y) * s->ps.sps->min_cb_width + x_cb],
1957 static const uint8_t tab_mode_idx[] = {
1958 0, 1, 2, 2, 2, 2, 3, 5, 7, 8, 10, 12, 13, 15, 17, 18, 19, 20,
1959 21, 22, 23, 23, 24, 24, 25, 25, 26, 27, 27, 28, 28, 29, 29, 30, 31};
1961 static void intra_prediction_unit(HEVCContext *s, int x0, int y0,
1964 HEVCLocalContext *lc = s->HEVClc;
1965 static const uint8_t intra_chroma_table[4] = { 0, 26, 10, 1 };
1966 uint8_t prev_intra_luma_pred_flag[4];
1967 int split = lc->cu.part_mode == PART_NxN;
1968 int pb_size = (1 << log2_cb_size) >> split;
1969 int side = split + 1;
1973 for (i = 0; i < side; i++)
1974 for (j = 0; j < side; j++)
1975 prev_intra_luma_pred_flag[2 * i + j] = ff_hevc_prev_intra_luma_pred_flag_decode(s);
1977 for (i = 0; i < side; i++) {
1978 for (j = 0; j < side; j++) {
1979 if (prev_intra_luma_pred_flag[2 * i + j])
1980 lc->pu.mpm_idx = ff_hevc_mpm_idx_decode(s);
1982 lc->pu.rem_intra_luma_pred_mode = ff_hevc_rem_intra_luma_pred_mode_decode(s);
1984 lc->pu.intra_pred_mode[2 * i + j] =
1985 luma_intra_pred_mode(s, x0 + pb_size * j, y0 + pb_size * i, pb_size,
1986 prev_intra_luma_pred_flag[2 * i + j]);
1990 if (s->ps.sps->chroma_format_idc == 3) {
1991 for (i = 0; i < side; i++) {
1992 for (j = 0; j < side; j++) {
1993 lc->pu.chroma_mode_c[2 * i + j] = chroma_mode = ff_hevc_intra_chroma_pred_mode_decode(s);
1994 if (chroma_mode != 4) {
1995 if (lc->pu.intra_pred_mode[2 * i + j] == intra_chroma_table[chroma_mode])
1996 lc->pu.intra_pred_mode_c[2 * i + j] = 34;
1998 lc->pu.intra_pred_mode_c[2 * i + j] = intra_chroma_table[chroma_mode];
2000 lc->pu.intra_pred_mode_c[2 * i + j] = lc->pu.intra_pred_mode[2 * i + j];
2004 } else if (s->ps.sps->chroma_format_idc == 2) {
2006 lc->pu.chroma_mode_c[0] = chroma_mode = ff_hevc_intra_chroma_pred_mode_decode(s);
2007 if (chroma_mode != 4) {
2008 if (lc->pu.intra_pred_mode[0] == intra_chroma_table[chroma_mode])
2011 mode_idx = intra_chroma_table[chroma_mode];
2013 mode_idx = lc->pu.intra_pred_mode[0];
2015 lc->pu.intra_pred_mode_c[0] = tab_mode_idx[mode_idx];
2016 } else if (s->ps.sps->chroma_format_idc != 0) {
2017 chroma_mode = ff_hevc_intra_chroma_pred_mode_decode(s);
2018 if (chroma_mode != 4) {
2019 if (lc->pu.intra_pred_mode[0] == intra_chroma_table[chroma_mode])
2020 lc->pu.intra_pred_mode_c[0] = 34;
2022 lc->pu.intra_pred_mode_c[0] = intra_chroma_table[chroma_mode];
2024 lc->pu.intra_pred_mode_c[0] = lc->pu.intra_pred_mode[0];
2029 static void intra_prediction_unit_default_value(HEVCContext *s,
2033 HEVCLocalContext *lc = s->HEVClc;
2034 int pb_size = 1 << log2_cb_size;
2035 int size_in_pus = pb_size >> s->ps.sps->log2_min_pu_size;
2036 int min_pu_width = s->ps.sps->min_pu_width;
2037 MvField *tab_mvf = s->ref->tab_mvf;
2038 int x_pu = x0 >> s->ps.sps->log2_min_pu_size;
2039 int y_pu = y0 >> s->ps.sps->log2_min_pu_size;
2042 if (size_in_pus == 0)
2044 for (j = 0; j < size_in_pus; j++)
2045 memset(&s->tab_ipm[(y_pu + j) * min_pu_width + x_pu], INTRA_DC, size_in_pus);
2046 if (lc->cu.pred_mode == MODE_INTRA)
2047 for (j = 0; j < size_in_pus; j++)
2048 for (k = 0; k < size_in_pus; k++)
2049 tab_mvf[(y_pu + j) * min_pu_width + x_pu + k].pred_flag = PF_INTRA;
2052 static int hls_coding_unit(HEVCContext *s, int x0, int y0, int log2_cb_size)
2054 int cb_size = 1 << log2_cb_size;
2055 HEVCLocalContext *lc = s->HEVClc;
2056 int log2_min_cb_size = s->ps.sps->log2_min_cb_size;
2057 int length = cb_size >> log2_min_cb_size;
2058 int min_cb_width = s->ps.sps->min_cb_width;
2059 int x_cb = x0 >> log2_min_cb_size;
2060 int y_cb = y0 >> log2_min_cb_size;
2061 int idx = log2_cb_size - 2;
2062 int qp_block_mask = (1<<(s->ps.sps->log2_ctb_size - s->ps.pps->diff_cu_qp_delta_depth)) - 1;
2067 lc->cu.pred_mode = MODE_INTRA;
2068 lc->cu.part_mode = PART_2Nx2N;
2069 lc->cu.intra_split_flag = 0;
2071 SAMPLE_CTB(s->skip_flag, x_cb, y_cb) = 0;
2072 for (x = 0; x < 4; x++)
2073 lc->pu.intra_pred_mode[x] = 1;
2074 if (s->ps.pps->transquant_bypass_enable_flag) {
2075 lc->cu.cu_transquant_bypass_flag = ff_hevc_cu_transquant_bypass_flag_decode(s);
2076 if (lc->cu.cu_transquant_bypass_flag)
2077 set_deblocking_bypass(s, x0, y0, log2_cb_size);
2079 lc->cu.cu_transquant_bypass_flag = 0;
2081 if (s->sh.slice_type != HEVC_SLICE_I) {
2082 uint8_t skip_flag = ff_hevc_skip_flag_decode(s, x0, y0, x_cb, y_cb);
2084 x = y_cb * min_cb_width + x_cb;
2085 for (y = 0; y < length; y++) {
2086 memset(&s->skip_flag[x], skip_flag, length);
2089 lc->cu.pred_mode = skip_flag ? MODE_SKIP : MODE_INTER;
2091 x = y_cb * min_cb_width + x_cb;
2092 for (y = 0; y < length; y++) {
2093 memset(&s->skip_flag[x], 0, length);
2098 if (SAMPLE_CTB(s->skip_flag, x_cb, y_cb)) {
2099 hls_prediction_unit(s, x0, y0, cb_size, cb_size, log2_cb_size, 0, idx);
2100 intra_prediction_unit_default_value(s, x0, y0, log2_cb_size);
2102 if (!s->sh.disable_deblocking_filter_flag)
2103 ff_hevc_deblocking_boundary_strengths(s, x0, y0, log2_cb_size);
2107 if (s->sh.slice_type != HEVC_SLICE_I)
2108 lc->cu.pred_mode = ff_hevc_pred_mode_decode(s);
2109 if (lc->cu.pred_mode != MODE_INTRA ||
2110 log2_cb_size == s->ps.sps->log2_min_cb_size) {
2111 lc->cu.part_mode = ff_hevc_part_mode_decode(s, log2_cb_size);
2112 lc->cu.intra_split_flag = lc->cu.part_mode == PART_NxN &&
2113 lc->cu.pred_mode == MODE_INTRA;
2116 if (lc->cu.pred_mode == MODE_INTRA) {
2117 if (lc->cu.part_mode == PART_2Nx2N && s->ps.sps->pcm_enabled_flag &&
2118 log2_cb_size >= s->ps.sps->pcm.log2_min_pcm_cb_size &&
2119 log2_cb_size <= s->ps.sps->pcm.log2_max_pcm_cb_size) {
2120 pcm_flag = ff_hevc_pcm_flag_decode(s);
2123 intra_prediction_unit_default_value(s, x0, y0, log2_cb_size);
2124 ret = hls_pcm_sample(s, x0, y0, log2_cb_size);
2125 if (s->ps.sps->pcm.loop_filter_disable_flag)
2126 set_deblocking_bypass(s, x0, y0, log2_cb_size);
2131 intra_prediction_unit(s, x0, y0, log2_cb_size);
2134 intra_prediction_unit_default_value(s, x0, y0, log2_cb_size);
2135 switch (lc->cu.part_mode) {
2137 hls_prediction_unit(s, x0, y0, cb_size, cb_size, log2_cb_size, 0, idx);
2140 hls_prediction_unit(s, x0, y0, cb_size, cb_size / 2, log2_cb_size, 0, idx);
2141 hls_prediction_unit(s, x0, y0 + cb_size / 2, cb_size, cb_size / 2, log2_cb_size, 1, idx);
2144 hls_prediction_unit(s, x0, y0, cb_size / 2, cb_size, log2_cb_size, 0, idx - 1);
2145 hls_prediction_unit(s, x0 + cb_size / 2, y0, cb_size / 2, cb_size, log2_cb_size, 1, idx - 1);
2148 hls_prediction_unit(s, x0, y0, cb_size, cb_size / 4, log2_cb_size, 0, idx);
2149 hls_prediction_unit(s, x0, y0 + cb_size / 4, cb_size, cb_size * 3 / 4, log2_cb_size, 1, idx);
2152 hls_prediction_unit(s, x0, y0, cb_size, cb_size * 3 / 4, log2_cb_size, 0, idx);
2153 hls_prediction_unit(s, x0, y0 + cb_size * 3 / 4, cb_size, cb_size / 4, log2_cb_size, 1, idx);
2156 hls_prediction_unit(s, x0, y0, cb_size / 4, cb_size, log2_cb_size, 0, idx - 2);
2157 hls_prediction_unit(s, x0 + cb_size / 4, y0, cb_size * 3 / 4, cb_size, log2_cb_size, 1, idx - 2);
2160 hls_prediction_unit(s, x0, y0, cb_size * 3 / 4, cb_size, log2_cb_size, 0, idx - 2);
2161 hls_prediction_unit(s, x0 + cb_size * 3 / 4, y0, cb_size / 4, cb_size, log2_cb_size, 1, idx - 2);
2164 hls_prediction_unit(s, x0, y0, cb_size / 2, cb_size / 2, log2_cb_size, 0, idx - 1);
2165 hls_prediction_unit(s, x0 + cb_size / 2, y0, cb_size / 2, cb_size / 2, log2_cb_size, 1, idx - 1);
2166 hls_prediction_unit(s, x0, y0 + cb_size / 2, cb_size / 2, cb_size / 2, log2_cb_size, 2, idx - 1);
2167 hls_prediction_unit(s, x0 + cb_size / 2, y0 + cb_size / 2, cb_size / 2, cb_size / 2, log2_cb_size, 3, idx - 1);
2173 int rqt_root_cbf = 1;
2175 if (lc->cu.pred_mode != MODE_INTRA &&
2176 !(lc->cu.part_mode == PART_2Nx2N && lc->pu.merge_flag)) {
2177 rqt_root_cbf = ff_hevc_no_residual_syntax_flag_decode(s);
2180 const static int cbf[2] = { 0 };
2181 lc->cu.max_trafo_depth = lc->cu.pred_mode == MODE_INTRA ?
2182 s->ps.sps->max_transform_hierarchy_depth_intra + lc->cu.intra_split_flag :
2183 s->ps.sps->max_transform_hierarchy_depth_inter;
2184 ret = hls_transform_tree(s, x0, y0, x0, y0, x0, y0,
2186 log2_cb_size, 0, 0, cbf, cbf);
2190 if (!s->sh.disable_deblocking_filter_flag)
2191 ff_hevc_deblocking_boundary_strengths(s, x0, y0, log2_cb_size);
2196 if (s->ps.pps->cu_qp_delta_enabled_flag && lc->tu.is_cu_qp_delta_coded == 0)
2197 ff_hevc_set_qPy(s, x0, y0, log2_cb_size);
2199 x = y_cb * min_cb_width + x_cb;
2200 for (y = 0; y < length; y++) {
2201 memset(&s->qp_y_tab[x], lc->qp_y, length);
2205 if(((x0 + (1<<log2_cb_size)) & qp_block_mask) == 0 &&
2206 ((y0 + (1<<log2_cb_size)) & qp_block_mask) == 0) {
2207 lc->qPy_pred = lc->qp_y;
2210 set_ct_depth(s, x0, y0, log2_cb_size, lc->ct_depth);
2215 static int hls_coding_quadtree(HEVCContext *s, int x0, int y0,
2216 int log2_cb_size, int cb_depth)
2218 HEVCLocalContext *lc = s->HEVClc;
2219 const int cb_size = 1 << log2_cb_size;
2223 lc->ct_depth = cb_depth;
2224 if (x0 + cb_size <= s->ps.sps->width &&
2225 y0 + cb_size <= s->ps.sps->height &&
2226 log2_cb_size > s->ps.sps->log2_min_cb_size) {
2227 split_cu = ff_hevc_split_coding_unit_flag_decode(s, cb_depth, x0, y0);
2229 split_cu = (log2_cb_size > s->ps.sps->log2_min_cb_size);
2231 if (s->ps.pps->cu_qp_delta_enabled_flag &&
2232 log2_cb_size >= s->ps.sps->log2_ctb_size - s->ps.pps->diff_cu_qp_delta_depth) {
2233 lc->tu.is_cu_qp_delta_coded = 0;
2234 lc->tu.cu_qp_delta = 0;
2237 if (s->sh.cu_chroma_qp_offset_enabled_flag &&
2238 log2_cb_size >= s->ps.sps->log2_ctb_size - s->ps.pps->diff_cu_chroma_qp_offset_depth) {
2239 lc->tu.is_cu_chroma_qp_offset_coded = 0;
2243 int qp_block_mask = (1<<(s->ps.sps->log2_ctb_size - s->ps.pps->diff_cu_qp_delta_depth)) - 1;
2244 const int cb_size_split = cb_size >> 1;
2245 const int x1 = x0 + cb_size_split;
2246 const int y1 = y0 + cb_size_split;
2250 more_data = hls_coding_quadtree(s, x0, y0, log2_cb_size - 1, cb_depth + 1);
2254 if (more_data && x1 < s->ps.sps->width) {
2255 more_data = hls_coding_quadtree(s, x1, y0, log2_cb_size - 1, cb_depth + 1);
2259 if (more_data && y1 < s->ps.sps->height) {
2260 more_data = hls_coding_quadtree(s, x0, y1, log2_cb_size - 1, cb_depth + 1);
2264 if (more_data && x1 < s->ps.sps->width &&
2265 y1 < s->ps.sps->height) {
2266 more_data = hls_coding_quadtree(s, x1, y1, log2_cb_size - 1, cb_depth + 1);
2271 if(((x0 + (1<<log2_cb_size)) & qp_block_mask) == 0 &&
2272 ((y0 + (1<<log2_cb_size)) & qp_block_mask) == 0)
2273 lc->qPy_pred = lc->qp_y;
2276 return ((x1 + cb_size_split) < s->ps.sps->width ||
2277 (y1 + cb_size_split) < s->ps.sps->height);
2281 ret = hls_coding_unit(s, x0, y0, log2_cb_size);
2284 if ((!((x0 + cb_size) %
2285 (1 << (s->ps.sps->log2_ctb_size))) ||
2286 (x0 + cb_size >= s->ps.sps->width)) &&
2288 (1 << (s->ps.sps->log2_ctb_size))) ||
2289 (y0 + cb_size >= s->ps.sps->height))) {
2290 int end_of_slice_flag = ff_hevc_end_of_slice_flag_decode(s);
2291 return !end_of_slice_flag;
2300 static void hls_decode_neighbour(HEVCContext *s, int x_ctb, int y_ctb,
2303 HEVCLocalContext *lc = s->HEVClc;
2304 int ctb_size = 1 << s->ps.sps->log2_ctb_size;
2305 int ctb_addr_rs = s->ps.pps->ctb_addr_ts_to_rs[ctb_addr_ts];
2306 int ctb_addr_in_slice = ctb_addr_rs - s->sh.slice_addr;
2308 s->tab_slice_address[ctb_addr_rs] = s->sh.slice_addr;
2310 if (s->ps.pps->entropy_coding_sync_enabled_flag) {
2311 if (x_ctb == 0 && (y_ctb & (ctb_size - 1)) == 0)
2312 lc->first_qp_group = 1;
2313 lc->end_of_tiles_x = s->ps.sps->width;
2314 } else if (s->ps.pps->tiles_enabled_flag) {
2315 if (ctb_addr_ts && s->ps.pps->tile_id[ctb_addr_ts] != s->ps.pps->tile_id[ctb_addr_ts - 1]) {
2316 int idxX = s->ps.pps->col_idxX[x_ctb >> s->ps.sps->log2_ctb_size];
2317 lc->end_of_tiles_x = x_ctb + (s->ps.pps->column_width[idxX] << s->ps.sps->log2_ctb_size);
2318 lc->first_qp_group = 1;
2321 lc->end_of_tiles_x = s->ps.sps->width;
2324 lc->end_of_tiles_y = FFMIN(y_ctb + ctb_size, s->ps.sps->height);
2326 lc->boundary_flags = 0;
2327 if (s->ps.pps->tiles_enabled_flag) {
2328 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]])
2329 lc->boundary_flags |= BOUNDARY_LEFT_TILE;
2330 if (x_ctb > 0 && s->tab_slice_address[ctb_addr_rs] != s->tab_slice_address[ctb_addr_rs - 1])
2331 lc->boundary_flags |= BOUNDARY_LEFT_SLICE;
2332 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]])
2333 lc->boundary_flags |= BOUNDARY_UPPER_TILE;
2334 if (y_ctb > 0 && s->tab_slice_address[ctb_addr_rs] != s->tab_slice_address[ctb_addr_rs - s->ps.sps->ctb_width])
2335 lc->boundary_flags |= BOUNDARY_UPPER_SLICE;
2337 if (ctb_addr_in_slice <= 0)
2338 lc->boundary_flags |= BOUNDARY_LEFT_SLICE;
2339 if (ctb_addr_in_slice < s->ps.sps->ctb_width)
2340 lc->boundary_flags |= BOUNDARY_UPPER_SLICE;
2343 lc->ctb_left_flag = ((x_ctb > 0) && (ctb_addr_in_slice > 0) && !(lc->boundary_flags & BOUNDARY_LEFT_TILE));
2344 lc->ctb_up_flag = ((y_ctb > 0) && (ctb_addr_in_slice >= s->ps.sps->ctb_width) && !(lc->boundary_flags & BOUNDARY_UPPER_TILE));
2345 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]]));
2346 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]]));
2349 static int hls_decode_entry(AVCodecContext *avctxt, void *isFilterThread)
2351 HEVCContext *s = avctxt->priv_data;
2352 int ctb_size = 1 << s->ps.sps->log2_ctb_size;
2356 int ctb_addr_ts = s->ps.pps->ctb_addr_rs_to_ts[s->sh.slice_ctb_addr_rs];
2359 if (!ctb_addr_ts && s->sh.dependent_slice_segment_flag) {
2360 av_log(s->avctx, AV_LOG_ERROR, "Impossible initial tile.\n");
2361 return AVERROR_INVALIDDATA;
2364 if (s->sh.dependent_slice_segment_flag) {
2365 int prev_rs = s->ps.pps->ctb_addr_ts_to_rs[ctb_addr_ts - 1];
2366 if (s->tab_slice_address[prev_rs] != s->sh.slice_addr) {
2367 av_log(s->avctx, AV_LOG_ERROR, "Previous slice segment missing\n");
2368 return AVERROR_INVALIDDATA;
2372 while (more_data && ctb_addr_ts < s->ps.sps->ctb_size) {
2373 int ctb_addr_rs = s->ps.pps->ctb_addr_ts_to_rs[ctb_addr_ts];
2375 x_ctb = (ctb_addr_rs % ((s->ps.sps->width + ctb_size - 1) >> s->ps.sps->log2_ctb_size)) << s->ps.sps->log2_ctb_size;
2376 y_ctb = (ctb_addr_rs / ((s->ps.sps->width + ctb_size - 1) >> s->ps.sps->log2_ctb_size)) << s->ps.sps->log2_ctb_size;
2377 hls_decode_neighbour(s, x_ctb, y_ctb, ctb_addr_ts);
2379 ret = ff_hevc_cabac_init(s, ctb_addr_ts);
2381 s->tab_slice_address[ctb_addr_rs] = -1;
2385 hls_sao_param(s, x_ctb >> s->ps.sps->log2_ctb_size, y_ctb >> s->ps.sps->log2_ctb_size);
2387 s->deblock[ctb_addr_rs].beta_offset = s->sh.beta_offset;
2388 s->deblock[ctb_addr_rs].tc_offset = s->sh.tc_offset;
2389 s->filter_slice_edges[ctb_addr_rs] = s->sh.slice_loop_filter_across_slices_enabled_flag;
2391 more_data = hls_coding_quadtree(s, x_ctb, y_ctb, s->ps.sps->log2_ctb_size, 0);
2392 if (more_data < 0) {
2393 s->tab_slice_address[ctb_addr_rs] = -1;
2399 ff_hevc_save_states(s, ctb_addr_ts);
2400 ff_hevc_hls_filters(s, x_ctb, y_ctb, ctb_size);
2403 if (x_ctb + ctb_size >= s->ps.sps->width &&
2404 y_ctb + ctb_size >= s->ps.sps->height)
2405 ff_hevc_hls_filter(s, x_ctb, y_ctb, ctb_size);
2410 static int hls_slice_data(HEVCContext *s)
2418 s->avctx->execute(s->avctx, hls_decode_entry, arg, ret , 1, sizeof(int));
2421 static int hls_decode_entry_wpp(AVCodecContext *avctxt, void *input_ctb_row, int job, int self_id)
2423 HEVCContext *s1 = avctxt->priv_data, *s;
2424 HEVCLocalContext *lc;
2425 int ctb_size = 1<< s1->ps.sps->log2_ctb_size;
2427 int *ctb_row_p = input_ctb_row;
2428 int ctb_row = ctb_row_p[job];
2429 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);
2430 int ctb_addr_ts = s1->ps.pps->ctb_addr_rs_to_ts[ctb_addr_rs];
2431 int thread = ctb_row % s1->threads_number;
2434 s = s1->sList[self_id];
2438 ret = init_get_bits8(&lc->gb, s->data + s->sh.offset[ctb_row - 1], s->sh.size[ctb_row - 1]);
2441 ff_init_cabac_decoder(&lc->cc, s->data + s->sh.offset[(ctb_row)-1], s->sh.size[ctb_row - 1]);
2444 while(more_data && ctb_addr_ts < s->ps.sps->ctb_size) {
2445 int x_ctb = (ctb_addr_rs % s->ps.sps->ctb_width) << s->ps.sps->log2_ctb_size;
2446 int y_ctb = (ctb_addr_rs / s->ps.sps->ctb_width) << s->ps.sps->log2_ctb_size;
2448 hls_decode_neighbour(s, x_ctb, y_ctb, ctb_addr_ts);
2450 ff_thread_await_progress2(s->avctx, ctb_row, thread, SHIFT_CTB_WPP);
2452 if (atomic_load(&s1->wpp_err)) {
2453 ff_thread_report_progress2(s->avctx, ctb_row , thread, SHIFT_CTB_WPP);
2457 ret = ff_hevc_cabac_init(s, ctb_addr_ts);
2460 hls_sao_param(s, x_ctb >> s->ps.sps->log2_ctb_size, y_ctb >> s->ps.sps->log2_ctb_size);
2461 more_data = hls_coding_quadtree(s, x_ctb, y_ctb, s->ps.sps->log2_ctb_size, 0);
2463 if (more_data < 0) {
2470 ff_hevc_save_states(s, ctb_addr_ts);
2471 ff_thread_report_progress2(s->avctx, ctb_row, thread, 1);
2472 ff_hevc_hls_filters(s, x_ctb, y_ctb, ctb_size);
2474 if (!more_data && (x_ctb+ctb_size) < s->ps.sps->width && ctb_row != s->sh.num_entry_point_offsets) {
2475 atomic_store(&s1->wpp_err, 1);
2476 ff_thread_report_progress2(s->avctx, ctb_row ,thread, SHIFT_CTB_WPP);
2480 if ((x_ctb+ctb_size) >= s->ps.sps->width && (y_ctb+ctb_size) >= s->ps.sps->height ) {
2481 ff_hevc_hls_filter(s, x_ctb, y_ctb, ctb_size);
2482 ff_thread_report_progress2(s->avctx, ctb_row , thread, SHIFT_CTB_WPP);
2485 ctb_addr_rs = s->ps.pps->ctb_addr_ts_to_rs[ctb_addr_ts];
2488 if(x_ctb >= s->ps.sps->width) {
2492 ff_thread_report_progress2(s->avctx, ctb_row ,thread, SHIFT_CTB_WPP);
2496 s->tab_slice_address[ctb_addr_rs] = -1;
2497 atomic_store(&s1->wpp_err, 1);
2498 ff_thread_report_progress2(s->avctx, ctb_row ,thread, SHIFT_CTB_WPP);
2502 static int hls_slice_data_wpp(HEVCContext *s, const H2645NAL *nal)
2504 const uint8_t *data = nal->data;
2505 int length = nal->size;
2506 HEVCLocalContext *lc = s->HEVClc;
2507 int *ret = av_malloc_array(s->sh.num_entry_point_offsets + 1, sizeof(int));
2508 int *arg = av_malloc_array(s->sh.num_entry_point_offsets + 1, sizeof(int));
2510 int64_t startheader, cmpt = 0;
2516 return AVERROR(ENOMEM);
2519 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) {
2520 av_log(s->avctx, AV_LOG_ERROR, "WPP ctb addresses are wrong (%d %d %d %d)\n",
2521 s->sh.slice_ctb_addr_rs, s->sh.num_entry_point_offsets,
2522 s->ps.sps->ctb_width, s->ps.sps->ctb_height
2524 res = AVERROR_INVALIDDATA;
2528 ff_alloc_entries(s->avctx, s->sh.num_entry_point_offsets + 1);
2531 for (i = 1; i < s->threads_number; i++) {
2532 s->sList[i] = av_malloc(sizeof(HEVCContext));
2533 memcpy(s->sList[i], s, sizeof(HEVCContext));
2534 s->HEVClcList[i] = av_mallocz(sizeof(HEVCLocalContext));
2535 s->sList[i]->HEVClc = s->HEVClcList[i];
2539 offset = (lc->gb.index >> 3);
2541 for (j = 0, cmpt = 0, startheader = offset + s->sh.entry_point_offset[0]; j < nal->skipped_bytes; j++) {
2542 if (nal->skipped_bytes_pos[j] >= offset && nal->skipped_bytes_pos[j] < startheader) {
2548 for (i = 1; i < s->sh.num_entry_point_offsets; i++) {
2549 offset += (s->sh.entry_point_offset[i - 1] - cmpt);
2550 for (j = 0, cmpt = 0, startheader = offset
2551 + s->sh.entry_point_offset[i]; j < nal->skipped_bytes; j++) {
2552 if (nal->skipped_bytes_pos[j] >= offset && nal->skipped_bytes_pos[j] < startheader) {
2557 s->sh.size[i - 1] = s->sh.entry_point_offset[i] - cmpt;
2558 s->sh.offset[i - 1] = offset;
2561 if (s->sh.num_entry_point_offsets != 0) {
2562 offset += s->sh.entry_point_offset[s->sh.num_entry_point_offsets - 1] - cmpt;
2563 if (length < offset) {
2564 av_log(s->avctx, AV_LOG_ERROR, "entry_point_offset table is corrupted\n");
2565 res = AVERROR_INVALIDDATA;
2568 s->sh.size[s->sh.num_entry_point_offsets - 1] = length - offset;
2569 s->sh.offset[s->sh.num_entry_point_offsets - 1] = offset;
2574 for (i = 1; i < s->threads_number; i++) {
2575 s->sList[i]->HEVClc->first_qp_group = 1;
2576 s->sList[i]->HEVClc->qp_y = s->sList[0]->HEVClc->qp_y;
2577 memcpy(s->sList[i], s, sizeof(HEVCContext));
2578 s->sList[i]->HEVClc = s->HEVClcList[i];
2581 atomic_store(&s->wpp_err, 0);
2582 ff_reset_entries(s->avctx);
2584 for (i = 0; i <= s->sh.num_entry_point_offsets; i++) {
2589 if (s->ps.pps->entropy_coding_sync_enabled_flag)
2590 s->avctx->execute2(s->avctx, hls_decode_entry_wpp, arg, ret, s->sh.num_entry_point_offsets + 1);
2592 for (i = 0; i <= s->sh.num_entry_point_offsets; i++)
2600 static int set_side_data(HEVCContext *s)
2602 AVFrame *out = s->ref->frame;
2604 if (s->sei.frame_packing.present &&
2605 s->sei.frame_packing.arrangement_type >= 3 &&
2606 s->sei.frame_packing.arrangement_type <= 5 &&
2607 s->sei.frame_packing.content_interpretation_type > 0 &&
2608 s->sei.frame_packing.content_interpretation_type < 3) {
2609 AVStereo3D *stereo = av_stereo3d_create_side_data(out);
2611 return AVERROR(ENOMEM);
2613 switch (s->sei.frame_packing.arrangement_type) {
2615 if (s->sei.frame_packing.quincunx_subsampling)
2616 stereo->type = AV_STEREO3D_SIDEBYSIDE_QUINCUNX;
2618 stereo->type = AV_STEREO3D_SIDEBYSIDE;
2621 stereo->type = AV_STEREO3D_TOPBOTTOM;
2624 stereo->type = AV_STEREO3D_FRAMESEQUENCE;
2628 if (s->sei.frame_packing.content_interpretation_type == 2)
2629 stereo->flags = AV_STEREO3D_FLAG_INVERT;
2632 if (s->sei.display_orientation.present &&
2633 (s->sei.display_orientation.anticlockwise_rotation ||
2634 s->sei.display_orientation.hflip || s->sei.display_orientation.vflip)) {
2635 double angle = s->sei.display_orientation.anticlockwise_rotation * 360 / (double) (1 << 16);
2636 AVFrameSideData *rotation = av_frame_new_side_data(out,
2637 AV_FRAME_DATA_DISPLAYMATRIX,
2638 sizeof(int32_t) * 9);
2640 return AVERROR(ENOMEM);
2642 av_display_rotation_set((int32_t *)rotation->data, angle);
2643 av_display_matrix_flip((int32_t *)rotation->data,
2644 s->sei.display_orientation.hflip,
2645 s->sei.display_orientation.vflip);
2648 // Decrement the mastering display flag when IRAP frame has no_rasl_output_flag=1
2649 // so the side data persists for the entire coded video sequence.
2650 if (s->sei.mastering_display.present > 0 &&
2651 IS_IRAP(s) && s->no_rasl_output_flag) {
2652 s->sei.mastering_display.present--;
2654 if (s->sei.mastering_display.present) {
2655 // HEVC uses a g,b,r ordering, which we convert to a more natural r,g,b
2656 const int mapping[3] = {2, 0, 1};
2657 const int chroma_den = 50000;
2658 const int luma_den = 10000;
2660 AVMasteringDisplayMetadata *metadata =
2661 av_mastering_display_metadata_create_side_data(out);
2663 return AVERROR(ENOMEM);
2665 for (i = 0; i < 3; i++) {
2666 const int j = mapping[i];
2667 metadata->display_primaries[i][0].num = s->sei.mastering_display.display_primaries[j][0];
2668 metadata->display_primaries[i][0].den = chroma_den;
2669 metadata->display_primaries[i][1].num = s->sei.mastering_display.display_primaries[j][1];
2670 metadata->display_primaries[i][1].den = chroma_den;
2672 metadata->white_point[0].num = s->sei.mastering_display.white_point[0];
2673 metadata->white_point[0].den = chroma_den;
2674 metadata->white_point[1].num = s->sei.mastering_display.white_point[1];
2675 metadata->white_point[1].den = chroma_den;
2677 metadata->max_luminance.num = s->sei.mastering_display.max_luminance;
2678 metadata->max_luminance.den = luma_den;
2679 metadata->min_luminance.num = s->sei.mastering_display.min_luminance;
2680 metadata->min_luminance.den = luma_den;
2681 metadata->has_luminance = 1;
2682 metadata->has_primaries = 1;
2684 av_log(s->avctx, AV_LOG_DEBUG, "Mastering Display Metadata:\n");
2685 av_log(s->avctx, AV_LOG_DEBUG,
2686 "r(%5.4f,%5.4f) g(%5.4f,%5.4f) b(%5.4f %5.4f) wp(%5.4f, %5.4f)\n",
2687 av_q2d(metadata->display_primaries[0][0]),
2688 av_q2d(metadata->display_primaries[0][1]),
2689 av_q2d(metadata->display_primaries[1][0]),
2690 av_q2d(metadata->display_primaries[1][1]),
2691 av_q2d(metadata->display_primaries[2][0]),
2692 av_q2d(metadata->display_primaries[2][1]),
2693 av_q2d(metadata->white_point[0]), av_q2d(metadata->white_point[1]));
2694 av_log(s->avctx, AV_LOG_DEBUG,
2695 "min_luminance=%f, max_luminance=%f\n",
2696 av_q2d(metadata->min_luminance), av_q2d(metadata->max_luminance));
2698 // Decrement the mastering display flag when IRAP frame has no_rasl_output_flag=1
2699 // so the side data persists for the entire coded video sequence.
2700 if (s->sei.content_light.present > 0 &&
2701 IS_IRAP(s) && s->no_rasl_output_flag) {
2702 s->sei.content_light.present--;
2704 if (s->sei.content_light.present) {
2705 AVContentLightMetadata *metadata =
2706 av_content_light_metadata_create_side_data(out);
2708 return AVERROR(ENOMEM);
2709 metadata->MaxCLL = s->sei.content_light.max_content_light_level;
2710 metadata->MaxFALL = s->sei.content_light.max_pic_average_light_level;
2712 av_log(s->avctx, AV_LOG_DEBUG, "Content Light Level Metadata:\n");
2713 av_log(s->avctx, AV_LOG_DEBUG, "MaxCLL=%d, MaxFALL=%d\n",
2714 metadata->MaxCLL, metadata->MaxFALL);
2717 if (s->sei.a53_caption.a53_caption) {
2718 AVFrameSideData* sd = av_frame_new_side_data(out,
2719 AV_FRAME_DATA_A53_CC,
2720 s->sei.a53_caption.a53_caption_size);
2722 memcpy(sd->data, s->sei.a53_caption.a53_caption, s->sei.a53_caption.a53_caption_size);
2723 av_freep(&s->sei.a53_caption.a53_caption);
2724 s->sei.a53_caption.a53_caption_size = 0;
2725 s->avctx->properties |= FF_CODEC_PROPERTY_CLOSED_CAPTIONS;
2728 if (s->sei.alternative_transfer.present &&
2729 av_color_transfer_name(s->sei.alternative_transfer.preferred_transfer_characteristics) &&
2730 s->sei.alternative_transfer.preferred_transfer_characteristics != AVCOL_TRC_UNSPECIFIED) {
2731 s->avctx->color_trc = s->sei.alternative_transfer.preferred_transfer_characteristics;
2737 static int hevc_frame_start(HEVCContext *s)
2739 HEVCLocalContext *lc = s->HEVClc;
2740 int pic_size_in_ctb = ((s->ps.sps->width >> s->ps.sps->log2_min_cb_size) + 1) *
2741 ((s->ps.sps->height >> s->ps.sps->log2_min_cb_size) + 1);
2744 memset(s->horizontal_bs, 0, s->bs_width * s->bs_height);
2745 memset(s->vertical_bs, 0, s->bs_width * s->bs_height);
2746 memset(s->cbf_luma, 0, s->ps.sps->min_tb_width * s->ps.sps->min_tb_height);
2747 memset(s->is_pcm, 0, (s->ps.sps->min_pu_width + 1) * (s->ps.sps->min_pu_height + 1));
2748 memset(s->tab_slice_address, -1, pic_size_in_ctb * sizeof(*s->tab_slice_address));
2751 s->first_nal_type = s->nal_unit_type;
2753 s->no_rasl_output_flag = IS_IDR(s) || IS_BLA(s) || (s->nal_unit_type == HEVC_NAL_CRA_NUT && s->last_eos);
2755 if (s->ps.pps->tiles_enabled_flag)
2756 lc->end_of_tiles_x = s->ps.pps->column_width[0] << s->ps.sps->log2_ctb_size;
2758 ret = ff_hevc_set_new_ref(s, &s->frame, s->poc);
2762 ret = ff_hevc_frame_rps(s);
2764 av_log(s->avctx, AV_LOG_ERROR, "Error constructing the frame RPS.\n");
2768 s->ref->frame->key_frame = IS_IRAP(s);
2770 ret = set_side_data(s);
2774 s->frame->pict_type = 3 - s->sh.slice_type;
2777 ff_hevc_bump_frame(s);
2779 av_frame_unref(s->output_frame);
2780 ret = ff_hevc_output_frame(s, s->output_frame, 0);
2784 if (!s->avctx->hwaccel)
2785 ff_thread_finish_setup(s->avctx);
2791 ff_hevc_unref_frame(s, s->ref, ~0);
2796 static int decode_nal_unit(HEVCContext *s, const H2645NAL *nal)
2798 HEVCLocalContext *lc = s->HEVClc;
2799 GetBitContext *gb = &lc->gb;
2800 int ctb_addr_ts, ret;
2803 s->nal_unit_type = nal->type;
2804 s->temporal_id = nal->temporal_id;
2806 switch (s->nal_unit_type) {
2808 ret = ff_hevc_decode_nal_vps(gb, s->avctx, &s->ps);
2813 ret = ff_hevc_decode_nal_sps(gb, s->avctx, &s->ps,
2814 s->apply_defdispwin);
2819 ret = ff_hevc_decode_nal_pps(gb, s->avctx, &s->ps);
2823 case HEVC_NAL_SEI_PREFIX:
2824 case HEVC_NAL_SEI_SUFFIX:
2825 ret = ff_hevc_decode_nal_sei(gb, s->avctx, &s->sei, &s->ps, s->nal_unit_type);
2829 case HEVC_NAL_TRAIL_R:
2830 case HEVC_NAL_TRAIL_N:
2831 case HEVC_NAL_TSA_N:
2832 case HEVC_NAL_TSA_R:
2833 case HEVC_NAL_STSA_N:
2834 case HEVC_NAL_STSA_R:
2835 case HEVC_NAL_BLA_W_LP:
2836 case HEVC_NAL_BLA_W_RADL:
2837 case HEVC_NAL_BLA_N_LP:
2838 case HEVC_NAL_IDR_W_RADL:
2839 case HEVC_NAL_IDR_N_LP:
2840 case HEVC_NAL_CRA_NUT:
2841 case HEVC_NAL_RADL_N:
2842 case HEVC_NAL_RADL_R:
2843 case HEVC_NAL_RASL_N:
2844 case HEVC_NAL_RASL_R:
2845 ret = hls_slice_header(s);
2849 if (s->sh.first_slice_in_pic_flag) {
2850 if (s->max_ra == INT_MAX) {
2851 if (s->nal_unit_type == HEVC_NAL_CRA_NUT || IS_BLA(s)) {
2855 s->max_ra = INT_MIN;
2859 if ((s->nal_unit_type == HEVC_NAL_RASL_R || s->nal_unit_type == HEVC_NAL_RASL_N) &&
2860 s->poc <= s->max_ra) {
2864 if (s->nal_unit_type == HEVC_NAL_RASL_R && s->poc > s->max_ra)
2865 s->max_ra = INT_MIN;
2868 ret = hevc_frame_start(s);
2871 } else if (!s->ref) {
2872 av_log(s->avctx, AV_LOG_ERROR, "First slice in a frame missing.\n");
2876 if (s->nal_unit_type != s->first_nal_type) {
2877 av_log(s->avctx, AV_LOG_ERROR,
2878 "Non-matching NAL types of the VCL NALUs: %d %d\n",
2879 s->first_nal_type, s->nal_unit_type);
2880 return AVERROR_INVALIDDATA;
2883 if (!s->sh.dependent_slice_segment_flag &&
2884 s->sh.slice_type != HEVC_SLICE_I) {
2885 ret = ff_hevc_slice_rpl(s);
2887 av_log(s->avctx, AV_LOG_WARNING,
2888 "Error constructing the reference lists for the current slice.\n");
2893 if (s->sh.first_slice_in_pic_flag && s->avctx->hwaccel) {
2894 ret = s->avctx->hwaccel->start_frame(s->avctx, NULL, 0);
2899 if (s->avctx->hwaccel) {
2900 ret = s->avctx->hwaccel->decode_slice(s->avctx, nal->raw_data, nal->raw_size);
2904 if (s->threads_number > 1 && s->sh.num_entry_point_offsets > 0)
2905 ctb_addr_ts = hls_slice_data_wpp(s, nal);
2907 ctb_addr_ts = hls_slice_data(s);
2908 if (ctb_addr_ts >= (s->ps.sps->ctb_width * s->ps.sps->ctb_height)) {
2912 if (ctb_addr_ts < 0) {
2918 case HEVC_NAL_EOS_NUT:
2919 case HEVC_NAL_EOB_NUT:
2920 s->seq_decode = (s->seq_decode + 1) & 0xff;
2921 s->max_ra = INT_MAX;
2924 case HEVC_NAL_FD_NUT:
2927 av_log(s->avctx, AV_LOG_INFO,
2928 "Skipping NAL unit %d\n", s->nal_unit_type);
2933 if (s->avctx->err_recognition & AV_EF_EXPLODE)
2938 static int decode_nal_units(HEVCContext *s, const uint8_t *buf, int length)
2941 int eos_at_start = 1;
2944 s->last_eos = s->eos;
2947 /* split the input packet into NAL units, so we know the upper bound on the
2948 * number of slices in the frame */
2949 ret = ff_h2645_packet_split(&s->pkt, buf, length, s->avctx, s->is_nalff,
2950 s->nal_length_size, s->avctx->codec_id, 1);
2952 av_log(s->avctx, AV_LOG_ERROR,
2953 "Error splitting the input into NAL units.\n");
2957 for (i = 0; i < s->pkt.nb_nals; i++) {
2958 if (s->pkt.nals[i].type == HEVC_NAL_EOB_NUT ||
2959 s->pkt.nals[i].type == HEVC_NAL_EOS_NUT) {
2970 /* decode the NAL units */
2971 for (i = 0; i < s->pkt.nb_nals; i++) {
2972 ret = decode_nal_unit(s, &s->pkt.nals[i]);
2974 av_log(s->avctx, AV_LOG_WARNING,
2975 "Error parsing NAL unit #%d.\n", i);
2981 if (s->ref && s->threads_type == FF_THREAD_FRAME)
2982 ff_thread_report_progress(&s->ref->tf, INT_MAX, 0);
2987 static void print_md5(void *log_ctx, int level, uint8_t md5[16])
2990 for (i = 0; i < 16; i++)
2991 av_log(log_ctx, level, "%02"PRIx8, md5[i]);
2994 static int verify_md5(HEVCContext *s, AVFrame *frame)
2996 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(frame->format);
3001 return AVERROR(EINVAL);
3003 pixel_shift = desc->comp[0].depth > 8;
3005 av_log(s->avctx, AV_LOG_DEBUG, "Verifying checksum for frame with POC %d: ",
3008 /* the checksums are LE, so we have to byteswap for >8bpp formats
3011 if (pixel_shift && !s->checksum_buf) {
3012 av_fast_malloc(&s->checksum_buf, &s->checksum_buf_size,
3013 FFMAX3(frame->linesize[0], frame->linesize[1],
3014 frame->linesize[2]));
3015 if (!s->checksum_buf)
3016 return AVERROR(ENOMEM);
3020 for (i = 0; frame->data[i]; i++) {
3021 int width = s->avctx->coded_width;
3022 int height = s->avctx->coded_height;
3023 int w = (i == 1 || i == 2) ? (width >> desc->log2_chroma_w) : width;
3024 int h = (i == 1 || i == 2) ? (height >> desc->log2_chroma_h) : height;
3027 av_md5_init(s->sei.picture_hash.md5_ctx);
3028 for (j = 0; j < h; j++) {
3029 const uint8_t *src = frame->data[i] + j * frame->linesize[i];
3032 s->bdsp.bswap16_buf((uint16_t *) s->checksum_buf,
3033 (const uint16_t *) src, w);
3034 src = s->checksum_buf;
3037 av_md5_update(s->sei.picture_hash.md5_ctx, src, w << pixel_shift);
3039 av_md5_final(s->sei.picture_hash.md5_ctx, md5);
3041 if (!memcmp(md5, s->sei.picture_hash.md5[i], 16)) {
3042 av_log (s->avctx, AV_LOG_DEBUG, "plane %d - correct ", i);
3043 print_md5(s->avctx, AV_LOG_DEBUG, md5);
3044 av_log (s->avctx, AV_LOG_DEBUG, "; ");
3046 av_log (s->avctx, AV_LOG_ERROR, "mismatching checksum of plane %d - ", i);
3047 print_md5(s->avctx, AV_LOG_ERROR, md5);
3048 av_log (s->avctx, AV_LOG_ERROR, " != ");
3049 print_md5(s->avctx, AV_LOG_ERROR, s->sei.picture_hash.md5[i]);
3050 av_log (s->avctx, AV_LOG_ERROR, "\n");
3051 return AVERROR_INVALIDDATA;
3055 av_log(s->avctx, AV_LOG_DEBUG, "\n");
3060 static int hevc_decode_extradata(HEVCContext *s, uint8_t *buf, int length)
3064 ret = ff_hevc_decode_extradata(buf, length, &s->ps, &s->sei, &s->is_nalff,
3065 &s->nal_length_size, s->avctx->err_recognition,
3066 s->apply_defdispwin, s->avctx);
3070 /* export stream parameters from the first SPS */
3071 for (i = 0; i < FF_ARRAY_ELEMS(s->ps.sps_list); i++) {
3072 if (s->ps.sps_list[i]) {
3073 const HEVCSPS *sps = (const HEVCSPS*)s->ps.sps_list[i]->data;
3074 export_stream_params(s->avctx, &s->ps, sps);
3082 static int hevc_decode_frame(AVCodecContext *avctx, void *data, int *got_output,
3086 int new_extradata_size;
3087 uint8_t *new_extradata;
3088 HEVCContext *s = avctx->priv_data;
3091 ret = ff_hevc_output_frame(s, data, 1);
3099 new_extradata = av_packet_get_side_data(avpkt, AV_PKT_DATA_NEW_EXTRADATA,
3100 &new_extradata_size);
3101 if (new_extradata && new_extradata_size > 0) {
3102 ret = hevc_decode_extradata(s, new_extradata, new_extradata_size);
3108 ret = decode_nal_units(s, avpkt->data, avpkt->size);
3112 if (avctx->hwaccel) {
3113 if (s->ref && (ret = avctx->hwaccel->end_frame(avctx)) < 0) {
3114 av_log(avctx, AV_LOG_ERROR,
3115 "hardware accelerator failed to decode picture\n");
3116 ff_hevc_unref_frame(s, s->ref, ~0);
3120 /* verify the SEI checksum */
3121 if (avctx->err_recognition & AV_EF_CRCCHECK && s->is_decoded &&
3122 s->sei.picture_hash.is_md5) {
3123 ret = verify_md5(s, s->ref->frame);
3124 if (ret < 0 && avctx->err_recognition & AV_EF_EXPLODE) {
3125 ff_hevc_unref_frame(s, s->ref, ~0);
3130 s->sei.picture_hash.is_md5 = 0;
3132 if (s->is_decoded) {
3133 av_log(avctx, AV_LOG_DEBUG, "Decoded frame with POC %d.\n", s->poc);
3137 if (s->output_frame->buf[0]) {
3138 av_frame_move_ref(data, s->output_frame);
3145 static int hevc_ref_frame(HEVCContext *s, HEVCFrame *dst, HEVCFrame *src)
3149 ret = ff_thread_ref_frame(&dst->tf, &src->tf);
3153 dst->tab_mvf_buf = av_buffer_ref(src->tab_mvf_buf);
3154 if (!dst->tab_mvf_buf)
3156 dst->tab_mvf = src->tab_mvf;
3158 dst->rpl_tab_buf = av_buffer_ref(src->rpl_tab_buf);
3159 if (!dst->rpl_tab_buf)
3161 dst->rpl_tab = src->rpl_tab;
3163 dst->rpl_buf = av_buffer_ref(src->rpl_buf);
3167 dst->poc = src->poc;
3168 dst->ctb_count = src->ctb_count;
3169 dst->flags = src->flags;
3170 dst->sequence = src->sequence;
3172 if (src->hwaccel_picture_private) {
3173 dst->hwaccel_priv_buf = av_buffer_ref(src->hwaccel_priv_buf);
3174 if (!dst->hwaccel_priv_buf)
3176 dst->hwaccel_picture_private = dst->hwaccel_priv_buf->data;
3181 ff_hevc_unref_frame(s, dst, ~0);
3182 return AVERROR(ENOMEM);
3185 static av_cold int hevc_decode_free(AVCodecContext *avctx)
3187 HEVCContext *s = avctx->priv_data;
3192 av_freep(&s->sei.picture_hash.md5_ctx);
3194 av_freep(&s->cabac_state);
3196 for (i = 0; i < 3; i++) {
3197 av_freep(&s->sao_pixel_buffer_h[i]);
3198 av_freep(&s->sao_pixel_buffer_v[i]);
3200 av_frame_free(&s->output_frame);
3202 for (i = 0; i < FF_ARRAY_ELEMS(s->DPB); i++) {
3203 ff_hevc_unref_frame(s, &s->DPB[i], ~0);
3204 av_frame_free(&s->DPB[i].frame);
3207 for (i = 0; i < FF_ARRAY_ELEMS(s->ps.vps_list); i++)
3208 av_buffer_unref(&s->ps.vps_list[i]);
3209 for (i = 0; i < FF_ARRAY_ELEMS(s->ps.sps_list); i++)
3210 av_buffer_unref(&s->ps.sps_list[i]);
3211 for (i = 0; i < FF_ARRAY_ELEMS(s->ps.pps_list); i++)
3212 av_buffer_unref(&s->ps.pps_list[i]);
3217 av_freep(&s->sh.entry_point_offset);
3218 av_freep(&s->sh.offset);
3219 av_freep(&s->sh.size);
3221 for (i = 1; i < s->threads_number; i++) {
3222 HEVCLocalContext *lc = s->HEVClcList[i];
3224 av_freep(&s->HEVClcList[i]);
3225 av_freep(&s->sList[i]);
3228 if (s->HEVClc == s->HEVClcList[0])
3230 av_freep(&s->HEVClcList[0]);
3232 ff_h2645_packet_uninit(&s->pkt);
3237 static av_cold int hevc_init_context(AVCodecContext *avctx)
3239 HEVCContext *s = avctx->priv_data;
3244 s->HEVClc = av_mallocz(sizeof(HEVCLocalContext));
3247 s->HEVClcList[0] = s->HEVClc;
3250 s->cabac_state = av_malloc(HEVC_CONTEXTS);
3251 if (!s->cabac_state)
3254 s->output_frame = av_frame_alloc();
3255 if (!s->output_frame)
3258 for (i = 0; i < FF_ARRAY_ELEMS(s->DPB); i++) {
3259 s->DPB[i].frame = av_frame_alloc();
3260 if (!s->DPB[i].frame)
3262 s->DPB[i].tf.f = s->DPB[i].frame;
3265 s->max_ra = INT_MAX;
3267 s->sei.picture_hash.md5_ctx = av_md5_alloc();
3268 if (!s->sei.picture_hash.md5_ctx)
3271 ff_bswapdsp_init(&s->bdsp);
3273 s->context_initialized = 1;
3276 ff_hevc_reset_sei(&s->sei);
3281 hevc_decode_free(avctx);
3282 return AVERROR(ENOMEM);
3285 static int hevc_update_thread_context(AVCodecContext *dst,
3286 const AVCodecContext *src)
3288 HEVCContext *s = dst->priv_data;
3289 HEVCContext *s0 = src->priv_data;
3292 if (!s->context_initialized) {
3293 ret = hevc_init_context(dst);
3298 for (i = 0; i < FF_ARRAY_ELEMS(s->DPB); i++) {
3299 ff_hevc_unref_frame(s, &s->DPB[i], ~0);
3300 if (s0->DPB[i].frame->buf[0]) {
3301 ret = hevc_ref_frame(s, &s->DPB[i], &s0->DPB[i]);
3307 if (s->ps.sps != s0->ps.sps)
3309 for (i = 0; i < FF_ARRAY_ELEMS(s->ps.vps_list); i++) {
3310 av_buffer_unref(&s->ps.vps_list[i]);
3311 if (s0->ps.vps_list[i]) {
3312 s->ps.vps_list[i] = av_buffer_ref(s0->ps.vps_list[i]);
3313 if (!s->ps.vps_list[i])
3314 return AVERROR(ENOMEM);
3318 for (i = 0; i < FF_ARRAY_ELEMS(s->ps.sps_list); i++) {
3319 av_buffer_unref(&s->ps.sps_list[i]);
3320 if (s0->ps.sps_list[i]) {
3321 s->ps.sps_list[i] = av_buffer_ref(s0->ps.sps_list[i]);
3322 if (!s->ps.sps_list[i])
3323 return AVERROR(ENOMEM);
3327 for (i = 0; i < FF_ARRAY_ELEMS(s->ps.pps_list); i++) {
3328 av_buffer_unref(&s->ps.pps_list[i]);
3329 if (s0->ps.pps_list[i]) {
3330 s->ps.pps_list[i] = av_buffer_ref(s0->ps.pps_list[i]);
3331 if (!s->ps.pps_list[i])
3332 return AVERROR(ENOMEM);
3336 if (s->ps.sps != s0->ps.sps)
3337 if ((ret = set_sps(s, s0->ps.sps, src->pix_fmt)) < 0)
3340 s->seq_decode = s0->seq_decode;
3341 s->seq_output = s0->seq_output;
3342 s->pocTid0 = s0->pocTid0;
3343 s->max_ra = s0->max_ra;
3345 s->no_rasl_output_flag = s0->no_rasl_output_flag;
3347 s->is_nalff = s0->is_nalff;
3348 s->nal_length_size = s0->nal_length_size;
3350 s->threads_number = s0->threads_number;
3351 s->threads_type = s0->threads_type;
3354 s->seq_decode = (s->seq_decode + 1) & 0xff;
3355 s->max_ra = INT_MAX;
3361 static av_cold int hevc_decode_init(AVCodecContext *avctx)
3363 HEVCContext *s = avctx->priv_data;
3366 avctx->internal->allocate_progress = 1;
3368 ret = hevc_init_context(avctx);
3372 s->enable_parallel_tiles = 0;
3373 s->sei.picture_timing.picture_struct = 0;
3376 atomic_init(&s->wpp_err, 0);
3378 if(avctx->active_thread_type & FF_THREAD_SLICE)
3379 s->threads_number = avctx->thread_count;
3381 s->threads_number = 1;
3383 if (avctx->extradata_size > 0 && avctx->extradata) {
3384 ret = hevc_decode_extradata(s, avctx->extradata, avctx->extradata_size);
3386 hevc_decode_free(avctx);
3391 if((avctx->active_thread_type & FF_THREAD_FRAME) && avctx->thread_count > 1)
3392 s->threads_type = FF_THREAD_FRAME;
3394 s->threads_type = FF_THREAD_SLICE;
3399 static av_cold int hevc_init_thread_copy(AVCodecContext *avctx)
3401 HEVCContext *s = avctx->priv_data;
3404 memset(s, 0, sizeof(*s));
3406 ret = hevc_init_context(avctx);
3413 static void hevc_decode_flush(AVCodecContext *avctx)
3415 HEVCContext *s = avctx->priv_data;
3416 ff_hevc_flush_dpb(s);
3417 s->max_ra = INT_MAX;
3421 #define OFFSET(x) offsetof(HEVCContext, x)
3422 #define PAR (AV_OPT_FLAG_DECODING_PARAM | AV_OPT_FLAG_VIDEO_PARAM)
3424 static const AVOption options[] = {
3425 { "apply_defdispwin", "Apply default display window from VUI", OFFSET(apply_defdispwin),
3426 AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, PAR },
3427 { "strict-displaywin", "stricly apply default display window size", OFFSET(apply_defdispwin),
3428 AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, PAR },
3432 static const AVClass hevc_decoder_class = {
3433 .class_name = "HEVC decoder",
3434 .item_name = av_default_item_name,
3436 .version = LIBAVUTIL_VERSION_INT,
3439 AVCodec ff_hevc_decoder = {
3441 .long_name = NULL_IF_CONFIG_SMALL("HEVC (High Efficiency Video Coding)"),
3442 .type = AVMEDIA_TYPE_VIDEO,
3443 .id = AV_CODEC_ID_HEVC,
3444 .priv_data_size = sizeof(HEVCContext),
3445 .priv_class = &hevc_decoder_class,
3446 .init = hevc_decode_init,
3447 .close = hevc_decode_free,
3448 .decode = hevc_decode_frame,
3449 .flush = hevc_decode_flush,
3450 .update_thread_context = hevc_update_thread_context,
3451 .init_thread_copy = hevc_init_thread_copy,
3452 .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DELAY |
3453 AV_CODEC_CAP_SLICE_THREADS | AV_CODEC_CAP_FRAME_THREADS,
3454 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE | FF_CODEC_CAP_EXPORTS_CROPPING,
3455 .profiles = NULL_IF_CONFIG_SMALL(ff_hevc_profiles),