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 + \
356 CONFIG_HEVC_D3D11VA_HWACCEL * 2 + \
357 CONFIG_HEVC_NVDEC_HWACCEL + \
358 CONFIG_HEVC_VAAPI_HWACCEL + \
359 CONFIG_HEVC_VIDEOTOOLBOX_HWACCEL + \
360 CONFIG_HEVC_VDPAU_HWACCEL)
361 enum AVPixelFormat pix_fmts[HWACCEL_MAX + 2], *fmt = pix_fmts;
363 switch (sps->pix_fmt) {
364 case AV_PIX_FMT_YUV420P:
365 case AV_PIX_FMT_YUVJ420P:
366 #if CONFIG_HEVC_DXVA2_HWACCEL
367 *fmt++ = AV_PIX_FMT_DXVA2_VLD;
369 #if CONFIG_HEVC_D3D11VA_HWACCEL
370 *fmt++ = AV_PIX_FMT_D3D11VA_VLD;
371 *fmt++ = AV_PIX_FMT_D3D11;
373 #if CONFIG_HEVC_VAAPI_HWACCEL
374 *fmt++ = AV_PIX_FMT_VAAPI;
376 #if CONFIG_HEVC_VDPAU_HWACCEL
377 *fmt++ = AV_PIX_FMT_VDPAU;
379 #if CONFIG_HEVC_NVDEC_HWACCEL
380 *fmt++ = AV_PIX_FMT_CUDA;
382 #if CONFIG_HEVC_VIDEOTOOLBOX_HWACCEL
383 *fmt++ = AV_PIX_FMT_VIDEOTOOLBOX;
386 case AV_PIX_FMT_YUV420P10:
387 #if CONFIG_HEVC_DXVA2_HWACCEL
388 *fmt++ = AV_PIX_FMT_DXVA2_VLD;
390 #if CONFIG_HEVC_D3D11VA_HWACCEL
391 *fmt++ = AV_PIX_FMT_D3D11VA_VLD;
392 *fmt++ = AV_PIX_FMT_D3D11;
394 #if CONFIG_HEVC_VAAPI_HWACCEL
395 *fmt++ = AV_PIX_FMT_VAAPI;
397 #if CONFIG_HEVC_VIDEOTOOLBOX_HWACCEL
398 *fmt++ = AV_PIX_FMT_VIDEOTOOLBOX;
400 #if CONFIG_HEVC_NVDEC_HWACCEL
401 *fmt++ = AV_PIX_FMT_CUDA;
406 *fmt++ = sps->pix_fmt;
407 *fmt = AV_PIX_FMT_NONE;
409 return ff_thread_get_format(s->avctx, pix_fmts);
412 static int set_sps(HEVCContext *s, const HEVCSPS *sps,
413 enum AVPixelFormat pix_fmt)
424 ret = pic_arrays_init(s, sps);
428 export_stream_params(s->avctx, &s->ps, sps);
430 s->avctx->pix_fmt = pix_fmt;
432 ff_hevc_pred_init(&s->hpc, sps->bit_depth);
433 ff_hevc_dsp_init (&s->hevcdsp, sps->bit_depth);
434 ff_videodsp_init (&s->vdsp, sps->bit_depth);
436 for (i = 0; i < 3; i++) {
437 av_freep(&s->sao_pixel_buffer_h[i]);
438 av_freep(&s->sao_pixel_buffer_v[i]);
441 if (sps->sao_enabled && !s->avctx->hwaccel) {
442 int c_count = (sps->chroma_format_idc != 0) ? 3 : 1;
445 for(c_idx = 0; c_idx < c_count; c_idx++) {
446 int w = sps->width >> sps->hshift[c_idx];
447 int h = sps->height >> sps->vshift[c_idx];
448 s->sao_pixel_buffer_h[c_idx] =
449 av_malloc((w * 2 * sps->ctb_height) <<
451 s->sao_pixel_buffer_v[c_idx] =
452 av_malloc((h * 2 * sps->ctb_width) <<
458 s->ps.vps = (HEVCVPS*) s->ps.vps_list[s->ps.sps->vps_id]->data;
468 static int hls_slice_header(HEVCContext *s)
470 GetBitContext *gb = &s->HEVClc->gb;
471 SliceHeader *sh = &s->sh;
475 sh->first_slice_in_pic_flag = get_bits1(gb);
476 if ((IS_IDR(s) || IS_BLA(s)) && sh->first_slice_in_pic_flag) {
477 s->seq_decode = (s->seq_decode + 1) & 0xff;
480 ff_hevc_clear_refs(s);
482 sh->no_output_of_prior_pics_flag = 0;
484 sh->no_output_of_prior_pics_flag = get_bits1(gb);
486 sh->pps_id = get_ue_golomb_long(gb);
487 if (sh->pps_id >= HEVC_MAX_PPS_COUNT || !s->ps.pps_list[sh->pps_id]) {
488 av_log(s->avctx, AV_LOG_ERROR, "PPS id out of range: %d\n", sh->pps_id);
489 return AVERROR_INVALIDDATA;
491 if (!sh->first_slice_in_pic_flag &&
492 s->ps.pps != (HEVCPPS*)s->ps.pps_list[sh->pps_id]->data) {
493 av_log(s->avctx, AV_LOG_ERROR, "PPS changed between slices.\n");
494 return AVERROR_INVALIDDATA;
496 s->ps.pps = (HEVCPPS*)s->ps.pps_list[sh->pps_id]->data;
497 if (s->nal_unit_type == HEVC_NAL_CRA_NUT && s->last_eos == 1)
498 sh->no_output_of_prior_pics_flag = 1;
500 if (s->ps.sps != (HEVCSPS*)s->ps.sps_list[s->ps.pps->sps_id]->data) {
501 const HEVCSPS *sps = (HEVCSPS*)s->ps.sps_list[s->ps.pps->sps_id]->data;
502 const HEVCSPS *last_sps = s->ps.sps;
503 enum AVPixelFormat pix_fmt;
505 if (last_sps && IS_IRAP(s) && s->nal_unit_type != HEVC_NAL_CRA_NUT) {
506 if (sps->width != last_sps->width || sps->height != last_sps->height ||
507 sps->temporal_layer[sps->max_sub_layers - 1].max_dec_pic_buffering !=
508 last_sps->temporal_layer[last_sps->max_sub_layers - 1].max_dec_pic_buffering)
509 sh->no_output_of_prior_pics_flag = 0;
511 ff_hevc_clear_refs(s);
513 ret = set_sps(s, sps, sps->pix_fmt);
517 pix_fmt = get_format(s, sps);
520 s->avctx->pix_fmt = pix_fmt;
522 s->seq_decode = (s->seq_decode + 1) & 0xff;
526 sh->dependent_slice_segment_flag = 0;
527 if (!sh->first_slice_in_pic_flag) {
528 int slice_address_length;
530 if (s->ps.pps->dependent_slice_segments_enabled_flag)
531 sh->dependent_slice_segment_flag = get_bits1(gb);
533 slice_address_length = av_ceil_log2(s->ps.sps->ctb_width *
534 s->ps.sps->ctb_height);
535 sh->slice_segment_addr = get_bitsz(gb, slice_address_length);
536 if (sh->slice_segment_addr >= s->ps.sps->ctb_width * s->ps.sps->ctb_height) {
537 av_log(s->avctx, AV_LOG_ERROR,
538 "Invalid slice segment address: %u.\n",
539 sh->slice_segment_addr);
540 return AVERROR_INVALIDDATA;
543 if (!sh->dependent_slice_segment_flag) {
544 sh->slice_addr = sh->slice_segment_addr;
548 sh->slice_segment_addr = sh->slice_addr = 0;
550 s->slice_initialized = 0;
553 if (!sh->dependent_slice_segment_flag) {
554 s->slice_initialized = 0;
556 for (i = 0; i < s->ps.pps->num_extra_slice_header_bits; i++)
557 skip_bits(gb, 1); // slice_reserved_undetermined_flag[]
559 sh->slice_type = get_ue_golomb_long(gb);
560 if (!(sh->slice_type == HEVC_SLICE_I ||
561 sh->slice_type == HEVC_SLICE_P ||
562 sh->slice_type == HEVC_SLICE_B)) {
563 av_log(s->avctx, AV_LOG_ERROR, "Unknown slice type: %d.\n",
565 return AVERROR_INVALIDDATA;
567 if (IS_IRAP(s) && sh->slice_type != HEVC_SLICE_I) {
568 av_log(s->avctx, AV_LOG_ERROR, "Inter slices in an IRAP frame.\n");
569 return AVERROR_INVALIDDATA;
572 // when flag is not present, picture is inferred to be output
573 sh->pic_output_flag = 1;
574 if (s->ps.pps->output_flag_present_flag)
575 sh->pic_output_flag = get_bits1(gb);
577 if (s->ps.sps->separate_colour_plane_flag)
578 sh->colour_plane_id = get_bits(gb, 2);
583 sh->pic_order_cnt_lsb = get_bits(gb, s->ps.sps->log2_max_poc_lsb);
584 poc = ff_hevc_compute_poc(s->ps.sps, s->pocTid0, sh->pic_order_cnt_lsb, s->nal_unit_type);
585 if (!sh->first_slice_in_pic_flag && poc != s->poc) {
586 av_log(s->avctx, AV_LOG_WARNING,
587 "Ignoring POC change between slices: %d -> %d\n", s->poc, poc);
588 if (s->avctx->err_recognition & AV_EF_EXPLODE)
589 return AVERROR_INVALIDDATA;
594 sh->short_term_ref_pic_set_sps_flag = get_bits1(gb);
595 pos = get_bits_left(gb);
596 if (!sh->short_term_ref_pic_set_sps_flag) {
597 ret = ff_hevc_decode_short_term_rps(gb, s->avctx, &sh->slice_rps, s->ps.sps, 1);
601 sh->short_term_rps = &sh->slice_rps;
603 int numbits, rps_idx;
605 if (!s->ps.sps->nb_st_rps) {
606 av_log(s->avctx, AV_LOG_ERROR, "No ref lists in the SPS.\n");
607 return AVERROR_INVALIDDATA;
610 numbits = av_ceil_log2(s->ps.sps->nb_st_rps);
611 rps_idx = numbits > 0 ? get_bits(gb, numbits) : 0;
612 sh->short_term_rps = &s->ps.sps->st_rps[rps_idx];
614 sh->short_term_ref_pic_set_size = pos - get_bits_left(gb);
616 pos = get_bits_left(gb);
617 ret = decode_lt_rps(s, &sh->long_term_rps, gb);
619 av_log(s->avctx, AV_LOG_WARNING, "Invalid long term RPS.\n");
620 if (s->avctx->err_recognition & AV_EF_EXPLODE)
621 return AVERROR_INVALIDDATA;
623 sh->long_term_ref_pic_set_size = pos - get_bits_left(gb);
625 if (s->ps.sps->sps_temporal_mvp_enabled_flag)
626 sh->slice_temporal_mvp_enabled_flag = get_bits1(gb);
628 sh->slice_temporal_mvp_enabled_flag = 0;
630 s->sh.short_term_rps = NULL;
635 if (sh->first_slice_in_pic_flag && s->temporal_id == 0 &&
636 s->nal_unit_type != HEVC_NAL_TRAIL_N &&
637 s->nal_unit_type != HEVC_NAL_TSA_N &&
638 s->nal_unit_type != HEVC_NAL_STSA_N &&
639 s->nal_unit_type != HEVC_NAL_RADL_N &&
640 s->nal_unit_type != HEVC_NAL_RADL_R &&
641 s->nal_unit_type != HEVC_NAL_RASL_N &&
642 s->nal_unit_type != HEVC_NAL_RASL_R)
645 if (s->ps.sps->sao_enabled) {
646 sh->slice_sample_adaptive_offset_flag[0] = get_bits1(gb);
647 if (s->ps.sps->chroma_format_idc) {
648 sh->slice_sample_adaptive_offset_flag[1] =
649 sh->slice_sample_adaptive_offset_flag[2] = get_bits1(gb);
652 sh->slice_sample_adaptive_offset_flag[0] = 0;
653 sh->slice_sample_adaptive_offset_flag[1] = 0;
654 sh->slice_sample_adaptive_offset_flag[2] = 0;
657 sh->nb_refs[L0] = sh->nb_refs[L1] = 0;
658 if (sh->slice_type == HEVC_SLICE_P || sh->slice_type == HEVC_SLICE_B) {
661 sh->nb_refs[L0] = s->ps.pps->num_ref_idx_l0_default_active;
662 if (sh->slice_type == HEVC_SLICE_B)
663 sh->nb_refs[L1] = s->ps.pps->num_ref_idx_l1_default_active;
665 if (get_bits1(gb)) { // num_ref_idx_active_override_flag
666 sh->nb_refs[L0] = get_ue_golomb_long(gb) + 1;
667 if (sh->slice_type == HEVC_SLICE_B)
668 sh->nb_refs[L1] = get_ue_golomb_long(gb) + 1;
670 if (sh->nb_refs[L0] > HEVC_MAX_REFS || sh->nb_refs[L1] > HEVC_MAX_REFS) {
671 av_log(s->avctx, AV_LOG_ERROR, "Too many refs: %d/%d.\n",
672 sh->nb_refs[L0], sh->nb_refs[L1]);
673 return AVERROR_INVALIDDATA;
676 sh->rpl_modification_flag[0] = 0;
677 sh->rpl_modification_flag[1] = 0;
678 nb_refs = ff_hevc_frame_nb_refs(s);
680 av_log(s->avctx, AV_LOG_ERROR, "Zero refs for a frame with P or B slices.\n");
681 return AVERROR_INVALIDDATA;
684 if (s->ps.pps->lists_modification_present_flag && nb_refs > 1) {
685 sh->rpl_modification_flag[0] = get_bits1(gb);
686 if (sh->rpl_modification_flag[0]) {
687 for (i = 0; i < sh->nb_refs[L0]; i++)
688 sh->list_entry_lx[0][i] = get_bits(gb, av_ceil_log2(nb_refs));
691 if (sh->slice_type == HEVC_SLICE_B) {
692 sh->rpl_modification_flag[1] = get_bits1(gb);
693 if (sh->rpl_modification_flag[1] == 1)
694 for (i = 0; i < sh->nb_refs[L1]; i++)
695 sh->list_entry_lx[1][i] = get_bits(gb, av_ceil_log2(nb_refs));
699 if (sh->slice_type == HEVC_SLICE_B)
700 sh->mvd_l1_zero_flag = get_bits1(gb);
702 if (s->ps.pps->cabac_init_present_flag)
703 sh->cabac_init_flag = get_bits1(gb);
705 sh->cabac_init_flag = 0;
707 sh->collocated_ref_idx = 0;
708 if (sh->slice_temporal_mvp_enabled_flag) {
709 sh->collocated_list = L0;
710 if (sh->slice_type == HEVC_SLICE_B)
711 sh->collocated_list = !get_bits1(gb);
713 if (sh->nb_refs[sh->collocated_list] > 1) {
714 sh->collocated_ref_idx = get_ue_golomb_long(gb);
715 if (sh->collocated_ref_idx >= sh->nb_refs[sh->collocated_list]) {
716 av_log(s->avctx, AV_LOG_ERROR,
717 "Invalid collocated_ref_idx: %d.\n",
718 sh->collocated_ref_idx);
719 return AVERROR_INVALIDDATA;
724 if ((s->ps.pps->weighted_pred_flag && sh->slice_type == HEVC_SLICE_P) ||
725 (s->ps.pps->weighted_bipred_flag && sh->slice_type == HEVC_SLICE_B)) {
726 int ret = pred_weight_table(s, gb);
731 sh->max_num_merge_cand = 5 - get_ue_golomb_long(gb);
732 if (sh->max_num_merge_cand < 1 || sh->max_num_merge_cand > 5) {
733 av_log(s->avctx, AV_LOG_ERROR,
734 "Invalid number of merging MVP candidates: %d.\n",
735 sh->max_num_merge_cand);
736 return AVERROR_INVALIDDATA;
740 sh->slice_qp_delta = get_se_golomb(gb);
742 if (s->ps.pps->pic_slice_level_chroma_qp_offsets_present_flag) {
743 sh->slice_cb_qp_offset = get_se_golomb(gb);
744 sh->slice_cr_qp_offset = get_se_golomb(gb);
746 sh->slice_cb_qp_offset = 0;
747 sh->slice_cr_qp_offset = 0;
750 if (s->ps.pps->chroma_qp_offset_list_enabled_flag)
751 sh->cu_chroma_qp_offset_enabled_flag = get_bits1(gb);
753 sh->cu_chroma_qp_offset_enabled_flag = 0;
755 if (s->ps.pps->deblocking_filter_control_present_flag) {
756 int deblocking_filter_override_flag = 0;
758 if (s->ps.pps->deblocking_filter_override_enabled_flag)
759 deblocking_filter_override_flag = get_bits1(gb);
761 if (deblocking_filter_override_flag) {
762 sh->disable_deblocking_filter_flag = get_bits1(gb);
763 if (!sh->disable_deblocking_filter_flag) {
764 int beta_offset_div2 = get_se_golomb(gb);
765 int tc_offset_div2 = get_se_golomb(gb) ;
766 if (beta_offset_div2 < -6 || beta_offset_div2 > 6 ||
767 tc_offset_div2 < -6 || tc_offset_div2 > 6) {
768 av_log(s->avctx, AV_LOG_ERROR,
769 "Invalid deblock filter offsets: %d, %d\n",
770 beta_offset_div2, tc_offset_div2);
771 return AVERROR_INVALIDDATA;
773 sh->beta_offset = beta_offset_div2 * 2;
774 sh->tc_offset = tc_offset_div2 * 2;
777 sh->disable_deblocking_filter_flag = s->ps.pps->disable_dbf;
778 sh->beta_offset = s->ps.pps->beta_offset;
779 sh->tc_offset = s->ps.pps->tc_offset;
782 sh->disable_deblocking_filter_flag = 0;
787 if (s->ps.pps->seq_loop_filter_across_slices_enabled_flag &&
788 (sh->slice_sample_adaptive_offset_flag[0] ||
789 sh->slice_sample_adaptive_offset_flag[1] ||
790 !sh->disable_deblocking_filter_flag)) {
791 sh->slice_loop_filter_across_slices_enabled_flag = get_bits1(gb);
793 sh->slice_loop_filter_across_slices_enabled_flag = s->ps.pps->seq_loop_filter_across_slices_enabled_flag;
795 } else if (!s->slice_initialized) {
796 av_log(s->avctx, AV_LOG_ERROR, "Independent slice segment missing.\n");
797 return AVERROR_INVALIDDATA;
800 sh->num_entry_point_offsets = 0;
801 if (s->ps.pps->tiles_enabled_flag || s->ps.pps->entropy_coding_sync_enabled_flag) {
802 unsigned num_entry_point_offsets = get_ue_golomb_long(gb);
803 // It would be possible to bound this tighter but this here is simpler
804 if (num_entry_point_offsets > get_bits_left(gb)) {
805 av_log(s->avctx, AV_LOG_ERROR, "num_entry_point_offsets %d is invalid\n", num_entry_point_offsets);
806 return AVERROR_INVALIDDATA;
809 sh->num_entry_point_offsets = num_entry_point_offsets;
810 if (sh->num_entry_point_offsets > 0) {
811 int offset_len = get_ue_golomb_long(gb) + 1;
813 if (offset_len < 1 || offset_len > 32) {
814 sh->num_entry_point_offsets = 0;
815 av_log(s->avctx, AV_LOG_ERROR, "offset_len %d is invalid\n", offset_len);
816 return AVERROR_INVALIDDATA;
819 av_freep(&sh->entry_point_offset);
820 av_freep(&sh->offset);
822 sh->entry_point_offset = av_malloc_array(sh->num_entry_point_offsets, sizeof(unsigned));
823 sh->offset = av_malloc_array(sh->num_entry_point_offsets, sizeof(int));
824 sh->size = av_malloc_array(sh->num_entry_point_offsets, sizeof(int));
825 if (!sh->entry_point_offset || !sh->offset || !sh->size) {
826 sh->num_entry_point_offsets = 0;
827 av_log(s->avctx, AV_LOG_ERROR, "Failed to allocate memory\n");
828 return AVERROR(ENOMEM);
830 for (i = 0; i < sh->num_entry_point_offsets; i++) {
831 unsigned val = get_bits_long(gb, offset_len);
832 sh->entry_point_offset[i] = val + 1; // +1; // +1 to get the size
834 if (s->threads_number > 1 && (s->ps.pps->num_tile_rows > 1 || s->ps.pps->num_tile_columns > 1)) {
835 s->enable_parallel_tiles = 0; // TODO: you can enable tiles in parallel here
836 s->threads_number = 1;
838 s->enable_parallel_tiles = 0;
840 s->enable_parallel_tiles = 0;
843 if (s->ps.pps->slice_header_extension_present_flag) {
844 unsigned int length = get_ue_golomb_long(gb);
845 if (length*8LL > get_bits_left(gb)) {
846 av_log(s->avctx, AV_LOG_ERROR, "too many slice_header_extension_data_bytes\n");
847 return AVERROR_INVALIDDATA;
849 for (i = 0; i < length; i++)
850 skip_bits(gb, 8); // slice_header_extension_data_byte
853 // Inferred parameters
854 sh->slice_qp = 26U + s->ps.pps->pic_init_qp_minus26 + sh->slice_qp_delta;
855 if (sh->slice_qp > 51 ||
856 sh->slice_qp < -s->ps.sps->qp_bd_offset) {
857 av_log(s->avctx, AV_LOG_ERROR,
858 "The slice_qp %d is outside the valid range "
861 -s->ps.sps->qp_bd_offset);
862 return AVERROR_INVALIDDATA;
865 sh->slice_ctb_addr_rs = sh->slice_segment_addr;
867 if (!s->sh.slice_ctb_addr_rs && s->sh.dependent_slice_segment_flag) {
868 av_log(s->avctx, AV_LOG_ERROR, "Impossible slice segment.\n");
869 return AVERROR_INVALIDDATA;
872 if (get_bits_left(gb) < 0) {
873 av_log(s->avctx, AV_LOG_ERROR,
874 "Overread slice header by %d bits\n", -get_bits_left(gb));
875 return AVERROR_INVALIDDATA;
878 s->HEVClc->first_qp_group = !s->sh.dependent_slice_segment_flag;
880 if (!s->ps.pps->cu_qp_delta_enabled_flag)
881 s->HEVClc->qp_y = s->sh.slice_qp;
883 s->slice_initialized = 1;
884 s->HEVClc->tu.cu_qp_offset_cb = 0;
885 s->HEVClc->tu.cu_qp_offset_cr = 0;
890 #define CTB(tab, x, y) ((tab)[(y) * s->ps.sps->ctb_width + (x)])
892 #define SET_SAO(elem, value) \
894 if (!sao_merge_up_flag && !sao_merge_left_flag) \
896 else if (sao_merge_left_flag) \
897 sao->elem = CTB(s->sao, rx-1, ry).elem; \
898 else if (sao_merge_up_flag) \
899 sao->elem = CTB(s->sao, rx, ry-1).elem; \
904 static void hls_sao_param(HEVCContext *s, int rx, int ry)
906 HEVCLocalContext *lc = s->HEVClc;
907 int sao_merge_left_flag = 0;
908 int sao_merge_up_flag = 0;
909 SAOParams *sao = &CTB(s->sao, rx, ry);
912 if (s->sh.slice_sample_adaptive_offset_flag[0] ||
913 s->sh.slice_sample_adaptive_offset_flag[1]) {
915 if (lc->ctb_left_flag)
916 sao_merge_left_flag = ff_hevc_sao_merge_flag_decode(s);
918 if (ry > 0 && !sao_merge_left_flag) {
920 sao_merge_up_flag = ff_hevc_sao_merge_flag_decode(s);
924 for (c_idx = 0; c_idx < (s->ps.sps->chroma_format_idc ? 3 : 1); c_idx++) {
925 int log2_sao_offset_scale = c_idx == 0 ? s->ps.pps->log2_sao_offset_scale_luma :
926 s->ps.pps->log2_sao_offset_scale_chroma;
928 if (!s->sh.slice_sample_adaptive_offset_flag[c_idx]) {
929 sao->type_idx[c_idx] = SAO_NOT_APPLIED;
934 sao->type_idx[2] = sao->type_idx[1];
935 sao->eo_class[2] = sao->eo_class[1];
937 SET_SAO(type_idx[c_idx], ff_hevc_sao_type_idx_decode(s));
940 if (sao->type_idx[c_idx] == SAO_NOT_APPLIED)
943 for (i = 0; i < 4; i++)
944 SET_SAO(offset_abs[c_idx][i], ff_hevc_sao_offset_abs_decode(s));
946 if (sao->type_idx[c_idx] == SAO_BAND) {
947 for (i = 0; i < 4; i++) {
948 if (sao->offset_abs[c_idx][i]) {
949 SET_SAO(offset_sign[c_idx][i],
950 ff_hevc_sao_offset_sign_decode(s));
952 sao->offset_sign[c_idx][i] = 0;
955 SET_SAO(band_position[c_idx], ff_hevc_sao_band_position_decode(s));
956 } else if (c_idx != 2) {
957 SET_SAO(eo_class[c_idx], ff_hevc_sao_eo_class_decode(s));
960 // Inferred parameters
961 sao->offset_val[c_idx][0] = 0;
962 for (i = 0; i < 4; i++) {
963 sao->offset_val[c_idx][i + 1] = sao->offset_abs[c_idx][i];
964 if (sao->type_idx[c_idx] == SAO_EDGE) {
966 sao->offset_val[c_idx][i + 1] = -sao->offset_val[c_idx][i + 1];
967 } else if (sao->offset_sign[c_idx][i]) {
968 sao->offset_val[c_idx][i + 1] = -sao->offset_val[c_idx][i + 1];
970 sao->offset_val[c_idx][i + 1] *= 1 << log2_sao_offset_scale;
978 static int hls_cross_component_pred(HEVCContext *s, int idx) {
979 HEVCLocalContext *lc = s->HEVClc;
980 int log2_res_scale_abs_plus1 = ff_hevc_log2_res_scale_abs(s, idx);
982 if (log2_res_scale_abs_plus1 != 0) {
983 int res_scale_sign_flag = ff_hevc_res_scale_sign_flag(s, idx);
984 lc->tu.res_scale_val = (1 << (log2_res_scale_abs_plus1 - 1)) *
985 (1 - 2 * res_scale_sign_flag);
987 lc->tu.res_scale_val = 0;
994 static int hls_transform_unit(HEVCContext *s, int x0, int y0,
995 int xBase, int yBase, int cb_xBase, int cb_yBase,
996 int log2_cb_size, int log2_trafo_size,
997 int blk_idx, int cbf_luma, int *cbf_cb, int *cbf_cr)
999 HEVCLocalContext *lc = s->HEVClc;
1000 const int log2_trafo_size_c = log2_trafo_size - s->ps.sps->hshift[1];
1003 if (lc->cu.pred_mode == MODE_INTRA) {
1004 int trafo_size = 1 << log2_trafo_size;
1005 ff_hevc_set_neighbour_available(s, x0, y0, trafo_size, trafo_size);
1007 s->hpc.intra_pred[log2_trafo_size - 2](s, x0, y0, 0);
1010 if (cbf_luma || cbf_cb[0] || cbf_cr[0] ||
1011 (s->ps.sps->chroma_format_idc == 2 && (cbf_cb[1] || cbf_cr[1]))) {
1012 int scan_idx = SCAN_DIAG;
1013 int scan_idx_c = SCAN_DIAG;
1014 int cbf_chroma = cbf_cb[0] || cbf_cr[0] ||
1015 (s->ps.sps->chroma_format_idc == 2 &&
1016 (cbf_cb[1] || cbf_cr[1]));
1018 if (s->ps.pps->cu_qp_delta_enabled_flag && !lc->tu.is_cu_qp_delta_coded) {
1019 lc->tu.cu_qp_delta = ff_hevc_cu_qp_delta_abs(s);
1020 if (lc->tu.cu_qp_delta != 0)
1021 if (ff_hevc_cu_qp_delta_sign_flag(s) == 1)
1022 lc->tu.cu_qp_delta = -lc->tu.cu_qp_delta;
1023 lc->tu.is_cu_qp_delta_coded = 1;
1025 if (lc->tu.cu_qp_delta < -(26 + s->ps.sps->qp_bd_offset / 2) ||
1026 lc->tu.cu_qp_delta > (25 + s->ps.sps->qp_bd_offset / 2)) {
1027 av_log(s->avctx, AV_LOG_ERROR,
1028 "The cu_qp_delta %d is outside the valid range "
1031 -(26 + s->ps.sps->qp_bd_offset / 2),
1032 (25 + s->ps.sps->qp_bd_offset / 2));
1033 return AVERROR_INVALIDDATA;
1036 ff_hevc_set_qPy(s, cb_xBase, cb_yBase, log2_cb_size);
1039 if (s->sh.cu_chroma_qp_offset_enabled_flag && cbf_chroma &&
1040 !lc->cu.cu_transquant_bypass_flag && !lc->tu.is_cu_chroma_qp_offset_coded) {
1041 int cu_chroma_qp_offset_flag = ff_hevc_cu_chroma_qp_offset_flag(s);
1042 if (cu_chroma_qp_offset_flag) {
1043 int cu_chroma_qp_offset_idx = 0;
1044 if (s->ps.pps->chroma_qp_offset_list_len_minus1 > 0) {
1045 cu_chroma_qp_offset_idx = ff_hevc_cu_chroma_qp_offset_idx(s);
1046 av_log(s->avctx, AV_LOG_ERROR,
1047 "cu_chroma_qp_offset_idx not yet tested.\n");
1049 lc->tu.cu_qp_offset_cb = s->ps.pps->cb_qp_offset_list[cu_chroma_qp_offset_idx];
1050 lc->tu.cu_qp_offset_cr = s->ps.pps->cr_qp_offset_list[cu_chroma_qp_offset_idx];
1052 lc->tu.cu_qp_offset_cb = 0;
1053 lc->tu.cu_qp_offset_cr = 0;
1055 lc->tu.is_cu_chroma_qp_offset_coded = 1;
1058 if (lc->cu.pred_mode == MODE_INTRA && log2_trafo_size < 4) {
1059 if (lc->tu.intra_pred_mode >= 6 &&
1060 lc->tu.intra_pred_mode <= 14) {
1061 scan_idx = SCAN_VERT;
1062 } else if (lc->tu.intra_pred_mode >= 22 &&
1063 lc->tu.intra_pred_mode <= 30) {
1064 scan_idx = SCAN_HORIZ;
1067 if (lc->tu.intra_pred_mode_c >= 6 &&
1068 lc->tu.intra_pred_mode_c <= 14) {
1069 scan_idx_c = SCAN_VERT;
1070 } else if (lc->tu.intra_pred_mode_c >= 22 &&
1071 lc->tu.intra_pred_mode_c <= 30) {
1072 scan_idx_c = SCAN_HORIZ;
1076 lc->tu.cross_pf = 0;
1079 ff_hevc_hls_residual_coding(s, x0, y0, log2_trafo_size, scan_idx, 0);
1080 if (s->ps.sps->chroma_format_idc && (log2_trafo_size > 2 || s->ps.sps->chroma_format_idc == 3)) {
1081 int trafo_size_h = 1 << (log2_trafo_size_c + s->ps.sps->hshift[1]);
1082 int trafo_size_v = 1 << (log2_trafo_size_c + s->ps.sps->vshift[1]);
1083 lc->tu.cross_pf = (s->ps.pps->cross_component_prediction_enabled_flag && cbf_luma &&
1084 (lc->cu.pred_mode == MODE_INTER ||
1085 (lc->tu.chroma_mode_c == 4)));
1087 if (lc->tu.cross_pf) {
1088 hls_cross_component_pred(s, 0);
1090 for (i = 0; i < (s->ps.sps->chroma_format_idc == 2 ? 2 : 1); i++) {
1091 if (lc->cu.pred_mode == MODE_INTRA) {
1092 ff_hevc_set_neighbour_available(s, x0, y0 + (i << log2_trafo_size_c), trafo_size_h, trafo_size_v);
1093 s->hpc.intra_pred[log2_trafo_size_c - 2](s, x0, y0 + (i << log2_trafo_size_c), 1);
1096 ff_hevc_hls_residual_coding(s, x0, y0 + (i << log2_trafo_size_c),
1097 log2_trafo_size_c, scan_idx_c, 1);
1099 if (lc->tu.cross_pf) {
1100 ptrdiff_t stride = s->frame->linesize[1];
1101 int hshift = s->ps.sps->hshift[1];
1102 int vshift = s->ps.sps->vshift[1];
1103 int16_t *coeffs_y = (int16_t*)lc->edge_emu_buffer;
1104 int16_t *coeffs = (int16_t*)lc->edge_emu_buffer2;
1105 int size = 1 << log2_trafo_size_c;
1107 uint8_t *dst = &s->frame->data[1][(y0 >> vshift) * stride +
1108 ((x0 >> hshift) << s->ps.sps->pixel_shift)];
1109 for (i = 0; i < (size * size); i++) {
1110 coeffs[i] = ((lc->tu.res_scale_val * coeffs_y[i]) >> 3);
1112 s->hevcdsp.add_residual[log2_trafo_size_c-2](dst, coeffs, stride);
1116 if (lc->tu.cross_pf) {
1117 hls_cross_component_pred(s, 1);
1119 for (i = 0; i < (s->ps.sps->chroma_format_idc == 2 ? 2 : 1); i++) {
1120 if (lc->cu.pred_mode == MODE_INTRA) {
1121 ff_hevc_set_neighbour_available(s, x0, y0 + (i << log2_trafo_size_c), trafo_size_h, trafo_size_v);
1122 s->hpc.intra_pred[log2_trafo_size_c - 2](s, x0, y0 + (i << log2_trafo_size_c), 2);
1125 ff_hevc_hls_residual_coding(s, x0, y0 + (i << log2_trafo_size_c),
1126 log2_trafo_size_c, scan_idx_c, 2);
1128 if (lc->tu.cross_pf) {
1129 ptrdiff_t stride = s->frame->linesize[2];
1130 int hshift = s->ps.sps->hshift[2];
1131 int vshift = s->ps.sps->vshift[2];
1132 int16_t *coeffs_y = (int16_t*)lc->edge_emu_buffer;
1133 int16_t *coeffs = (int16_t*)lc->edge_emu_buffer2;
1134 int size = 1 << log2_trafo_size_c;
1136 uint8_t *dst = &s->frame->data[2][(y0 >> vshift) * stride +
1137 ((x0 >> hshift) << s->ps.sps->pixel_shift)];
1138 for (i = 0; i < (size * size); i++) {
1139 coeffs[i] = ((lc->tu.res_scale_val * coeffs_y[i]) >> 3);
1141 s->hevcdsp.add_residual[log2_trafo_size_c-2](dst, coeffs, stride);
1144 } else if (s->ps.sps->chroma_format_idc && blk_idx == 3) {
1145 int trafo_size_h = 1 << (log2_trafo_size + 1);
1146 int trafo_size_v = 1 << (log2_trafo_size + s->ps.sps->vshift[1]);
1147 for (i = 0; i < (s->ps.sps->chroma_format_idc == 2 ? 2 : 1); i++) {
1148 if (lc->cu.pred_mode == MODE_INTRA) {
1149 ff_hevc_set_neighbour_available(s, xBase, yBase + (i << log2_trafo_size),
1150 trafo_size_h, trafo_size_v);
1151 s->hpc.intra_pred[log2_trafo_size - 2](s, xBase, yBase + (i << log2_trafo_size), 1);
1154 ff_hevc_hls_residual_coding(s, xBase, yBase + (i << log2_trafo_size),
1155 log2_trafo_size, scan_idx_c, 1);
1157 for (i = 0; i < (s->ps.sps->chroma_format_idc == 2 ? 2 : 1); i++) {
1158 if (lc->cu.pred_mode == MODE_INTRA) {
1159 ff_hevc_set_neighbour_available(s, xBase, yBase + (i << log2_trafo_size),
1160 trafo_size_h, trafo_size_v);
1161 s->hpc.intra_pred[log2_trafo_size - 2](s, xBase, yBase + (i << log2_trafo_size), 2);
1164 ff_hevc_hls_residual_coding(s, xBase, yBase + (i << log2_trafo_size),
1165 log2_trafo_size, scan_idx_c, 2);
1168 } else if (s->ps.sps->chroma_format_idc && lc->cu.pred_mode == MODE_INTRA) {
1169 if (log2_trafo_size > 2 || s->ps.sps->chroma_format_idc == 3) {
1170 int trafo_size_h = 1 << (log2_trafo_size_c + s->ps.sps->hshift[1]);
1171 int trafo_size_v = 1 << (log2_trafo_size_c + s->ps.sps->vshift[1]);
1172 ff_hevc_set_neighbour_available(s, x0, y0, trafo_size_h, trafo_size_v);
1173 s->hpc.intra_pred[log2_trafo_size_c - 2](s, x0, y0, 1);
1174 s->hpc.intra_pred[log2_trafo_size_c - 2](s, x0, y0, 2);
1175 if (s->ps.sps->chroma_format_idc == 2) {
1176 ff_hevc_set_neighbour_available(s, x0, y0 + (1 << log2_trafo_size_c),
1177 trafo_size_h, trafo_size_v);
1178 s->hpc.intra_pred[log2_trafo_size_c - 2](s, x0, y0 + (1 << log2_trafo_size_c), 1);
1179 s->hpc.intra_pred[log2_trafo_size_c - 2](s, x0, y0 + (1 << log2_trafo_size_c), 2);
1181 } else if (blk_idx == 3) {
1182 int trafo_size_h = 1 << (log2_trafo_size + 1);
1183 int trafo_size_v = 1 << (log2_trafo_size + s->ps.sps->vshift[1]);
1184 ff_hevc_set_neighbour_available(s, xBase, yBase,
1185 trafo_size_h, trafo_size_v);
1186 s->hpc.intra_pred[log2_trafo_size - 2](s, xBase, yBase, 1);
1187 s->hpc.intra_pred[log2_trafo_size - 2](s, xBase, yBase, 2);
1188 if (s->ps.sps->chroma_format_idc == 2) {
1189 ff_hevc_set_neighbour_available(s, xBase, yBase + (1 << (log2_trafo_size)),
1190 trafo_size_h, trafo_size_v);
1191 s->hpc.intra_pred[log2_trafo_size - 2](s, xBase, yBase + (1 << (log2_trafo_size)), 1);
1192 s->hpc.intra_pred[log2_trafo_size - 2](s, xBase, yBase + (1 << (log2_trafo_size)), 2);
1200 static void set_deblocking_bypass(HEVCContext *s, int x0, int y0, int log2_cb_size)
1202 int cb_size = 1 << log2_cb_size;
1203 int log2_min_pu_size = s->ps.sps->log2_min_pu_size;
1205 int min_pu_width = s->ps.sps->min_pu_width;
1206 int x_end = FFMIN(x0 + cb_size, s->ps.sps->width);
1207 int y_end = FFMIN(y0 + cb_size, s->ps.sps->height);
1210 for (j = (y0 >> log2_min_pu_size); j < (y_end >> log2_min_pu_size); j++)
1211 for (i = (x0 >> log2_min_pu_size); i < (x_end >> log2_min_pu_size); i++)
1212 s->is_pcm[i + j * min_pu_width] = 2;
1215 static int hls_transform_tree(HEVCContext *s, int x0, int y0,
1216 int xBase, int yBase, int cb_xBase, int cb_yBase,
1217 int log2_cb_size, int log2_trafo_size,
1218 int trafo_depth, int blk_idx,
1219 const int *base_cbf_cb, const int *base_cbf_cr)
1221 HEVCLocalContext *lc = s->HEVClc;
1222 uint8_t split_transform_flag;
1227 cbf_cb[0] = base_cbf_cb[0];
1228 cbf_cb[1] = base_cbf_cb[1];
1229 cbf_cr[0] = base_cbf_cr[0];
1230 cbf_cr[1] = base_cbf_cr[1];
1232 if (lc->cu.intra_split_flag) {
1233 if (trafo_depth == 1) {
1234 lc->tu.intra_pred_mode = lc->pu.intra_pred_mode[blk_idx];
1235 if (s->ps.sps->chroma_format_idc == 3) {
1236 lc->tu.intra_pred_mode_c = lc->pu.intra_pred_mode_c[blk_idx];
1237 lc->tu.chroma_mode_c = lc->pu.chroma_mode_c[blk_idx];
1239 lc->tu.intra_pred_mode_c = lc->pu.intra_pred_mode_c[0];
1240 lc->tu.chroma_mode_c = lc->pu.chroma_mode_c[0];
1244 lc->tu.intra_pred_mode = lc->pu.intra_pred_mode[0];
1245 lc->tu.intra_pred_mode_c = lc->pu.intra_pred_mode_c[0];
1246 lc->tu.chroma_mode_c = lc->pu.chroma_mode_c[0];
1249 if (log2_trafo_size <= s->ps.sps->log2_max_trafo_size &&
1250 log2_trafo_size > s->ps.sps->log2_min_tb_size &&
1251 trafo_depth < lc->cu.max_trafo_depth &&
1252 !(lc->cu.intra_split_flag && trafo_depth == 0)) {
1253 split_transform_flag = ff_hevc_split_transform_flag_decode(s, log2_trafo_size);
1255 int inter_split = s->ps.sps->max_transform_hierarchy_depth_inter == 0 &&
1256 lc->cu.pred_mode == MODE_INTER &&
1257 lc->cu.part_mode != PART_2Nx2N &&
1260 split_transform_flag = log2_trafo_size > s->ps.sps->log2_max_trafo_size ||
1261 (lc->cu.intra_split_flag && trafo_depth == 0) ||
1265 if (s->ps.sps->chroma_format_idc && (log2_trafo_size > 2 || s->ps.sps->chroma_format_idc == 3)) {
1266 if (trafo_depth == 0 || cbf_cb[0]) {
1267 cbf_cb[0] = ff_hevc_cbf_cb_cr_decode(s, trafo_depth);
1268 if (s->ps.sps->chroma_format_idc == 2 && (!split_transform_flag || log2_trafo_size == 3)) {
1269 cbf_cb[1] = ff_hevc_cbf_cb_cr_decode(s, trafo_depth);
1273 if (trafo_depth == 0 || cbf_cr[0]) {
1274 cbf_cr[0] = ff_hevc_cbf_cb_cr_decode(s, trafo_depth);
1275 if (s->ps.sps->chroma_format_idc == 2 && (!split_transform_flag || log2_trafo_size == 3)) {
1276 cbf_cr[1] = ff_hevc_cbf_cb_cr_decode(s, trafo_depth);
1281 if (split_transform_flag) {
1282 const int trafo_size_split = 1 << (log2_trafo_size - 1);
1283 const int x1 = x0 + trafo_size_split;
1284 const int y1 = y0 + trafo_size_split;
1286 #define SUBDIVIDE(x, y, idx) \
1288 ret = hls_transform_tree(s, x, y, x0, y0, cb_xBase, cb_yBase, log2_cb_size, \
1289 log2_trafo_size - 1, trafo_depth + 1, idx, \
1295 SUBDIVIDE(x0, y0, 0);
1296 SUBDIVIDE(x1, y0, 1);
1297 SUBDIVIDE(x0, y1, 2);
1298 SUBDIVIDE(x1, y1, 3);
1302 int min_tu_size = 1 << s->ps.sps->log2_min_tb_size;
1303 int log2_min_tu_size = s->ps.sps->log2_min_tb_size;
1304 int min_tu_width = s->ps.sps->min_tb_width;
1307 if (lc->cu.pred_mode == MODE_INTRA || trafo_depth != 0 ||
1308 cbf_cb[0] || cbf_cr[0] ||
1309 (s->ps.sps->chroma_format_idc == 2 && (cbf_cb[1] || cbf_cr[1]))) {
1310 cbf_luma = ff_hevc_cbf_luma_decode(s, trafo_depth);
1313 ret = hls_transform_unit(s, x0, y0, xBase, yBase, cb_xBase, cb_yBase,
1314 log2_cb_size, log2_trafo_size,
1315 blk_idx, cbf_luma, cbf_cb, cbf_cr);
1318 // TODO: store cbf_luma somewhere else
1321 for (i = 0; i < (1 << log2_trafo_size); i += min_tu_size)
1322 for (j = 0; j < (1 << log2_trafo_size); j += min_tu_size) {
1323 int x_tu = (x0 + j) >> log2_min_tu_size;
1324 int y_tu = (y0 + i) >> log2_min_tu_size;
1325 s->cbf_luma[y_tu * min_tu_width + x_tu] = 1;
1328 if (!s->sh.disable_deblocking_filter_flag) {
1329 ff_hevc_deblocking_boundary_strengths(s, x0, y0, log2_trafo_size);
1330 if (s->ps.pps->transquant_bypass_enable_flag &&
1331 lc->cu.cu_transquant_bypass_flag)
1332 set_deblocking_bypass(s, x0, y0, log2_trafo_size);
1338 static int hls_pcm_sample(HEVCContext *s, int x0, int y0, int log2_cb_size)
1340 HEVCLocalContext *lc = s->HEVClc;
1342 int cb_size = 1 << log2_cb_size;
1343 ptrdiff_t stride0 = s->frame->linesize[0];
1344 ptrdiff_t stride1 = s->frame->linesize[1];
1345 ptrdiff_t stride2 = s->frame->linesize[2];
1346 uint8_t *dst0 = &s->frame->data[0][y0 * stride0 + (x0 << s->ps.sps->pixel_shift)];
1347 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)];
1348 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)];
1350 int length = cb_size * cb_size * s->ps.sps->pcm.bit_depth +
1351 (((cb_size >> s->ps.sps->hshift[1]) * (cb_size >> s->ps.sps->vshift[1])) +
1352 ((cb_size >> s->ps.sps->hshift[2]) * (cb_size >> s->ps.sps->vshift[2]))) *
1353 s->ps.sps->pcm.bit_depth_chroma;
1354 const uint8_t *pcm = skip_bytes(&lc->cc, (length + 7) >> 3);
1357 if (!s->sh.disable_deblocking_filter_flag)
1358 ff_hevc_deblocking_boundary_strengths(s, x0, y0, log2_cb_size);
1360 ret = init_get_bits(&gb, pcm, length);
1364 s->hevcdsp.put_pcm(dst0, stride0, cb_size, cb_size, &gb, s->ps.sps->pcm.bit_depth);
1365 if (s->ps.sps->chroma_format_idc) {
1366 s->hevcdsp.put_pcm(dst1, stride1,
1367 cb_size >> s->ps.sps->hshift[1],
1368 cb_size >> s->ps.sps->vshift[1],
1369 &gb, s->ps.sps->pcm.bit_depth_chroma);
1370 s->hevcdsp.put_pcm(dst2, stride2,
1371 cb_size >> s->ps.sps->hshift[2],
1372 cb_size >> s->ps.sps->vshift[2],
1373 &gb, s->ps.sps->pcm.bit_depth_chroma);
1380 * 8.5.3.2.2.1 Luma sample unidirectional interpolation process
1382 * @param s HEVC decoding context
1383 * @param dst target buffer for block data at block position
1384 * @param dststride stride of the dst buffer
1385 * @param ref reference picture buffer at origin (0, 0)
1386 * @param mv motion vector (relative to block position) to get pixel data from
1387 * @param x_off horizontal position of block from origin (0, 0)
1388 * @param y_off vertical position of block from origin (0, 0)
1389 * @param block_w width of block
1390 * @param block_h height of block
1391 * @param luma_weight weighting factor applied to the luma prediction
1392 * @param luma_offset additive offset applied to the luma prediction value
1395 static void luma_mc_uni(HEVCContext *s, uint8_t *dst, ptrdiff_t dststride,
1396 AVFrame *ref, const Mv *mv, int x_off, int y_off,
1397 int block_w, int block_h, int luma_weight, int luma_offset)
1399 HEVCLocalContext *lc = s->HEVClc;
1400 uint8_t *src = ref->data[0];
1401 ptrdiff_t srcstride = ref->linesize[0];
1402 int pic_width = s->ps.sps->width;
1403 int pic_height = s->ps.sps->height;
1406 int weight_flag = (s->sh.slice_type == HEVC_SLICE_P && s->ps.pps->weighted_pred_flag) ||
1407 (s->sh.slice_type == HEVC_SLICE_B && s->ps.pps->weighted_bipred_flag);
1408 int idx = ff_hevc_pel_weight[block_w];
1410 x_off += mv->x >> 2;
1411 y_off += mv->y >> 2;
1412 src += y_off * srcstride + (x_off * (1 << s->ps.sps->pixel_shift));
1414 if (x_off < QPEL_EXTRA_BEFORE || y_off < QPEL_EXTRA_AFTER ||
1415 x_off >= pic_width - block_w - QPEL_EXTRA_AFTER ||
1416 y_off >= pic_height - block_h - QPEL_EXTRA_AFTER) {
1417 const ptrdiff_t edge_emu_stride = EDGE_EMU_BUFFER_STRIDE << s->ps.sps->pixel_shift;
1418 int offset = QPEL_EXTRA_BEFORE * srcstride + (QPEL_EXTRA_BEFORE << s->ps.sps->pixel_shift);
1419 int buf_offset = QPEL_EXTRA_BEFORE * edge_emu_stride + (QPEL_EXTRA_BEFORE << s->ps.sps->pixel_shift);
1421 s->vdsp.emulated_edge_mc(lc->edge_emu_buffer, src - offset,
1422 edge_emu_stride, srcstride,
1423 block_w + QPEL_EXTRA,
1424 block_h + QPEL_EXTRA,
1425 x_off - QPEL_EXTRA_BEFORE, y_off - QPEL_EXTRA_BEFORE,
1426 pic_width, pic_height);
1427 src = lc->edge_emu_buffer + buf_offset;
1428 srcstride = edge_emu_stride;
1432 s->hevcdsp.put_hevc_qpel_uni[idx][!!my][!!mx](dst, dststride, src, srcstride,
1433 block_h, mx, my, block_w);
1435 s->hevcdsp.put_hevc_qpel_uni_w[idx][!!my][!!mx](dst, dststride, src, srcstride,
1436 block_h, s->sh.luma_log2_weight_denom,
1437 luma_weight, luma_offset, mx, my, block_w);
1441 * 8.5.3.2.2.1 Luma sample bidirectional interpolation process
1443 * @param s HEVC decoding context
1444 * @param dst target buffer for block data at block position
1445 * @param dststride stride of the dst buffer
1446 * @param ref0 reference picture0 buffer at origin (0, 0)
1447 * @param mv0 motion vector0 (relative to block position) to get pixel data from
1448 * @param x_off horizontal position of block from origin (0, 0)
1449 * @param y_off vertical position of block from origin (0, 0)
1450 * @param block_w width of block
1451 * @param block_h height of block
1452 * @param ref1 reference picture1 buffer at origin (0, 0)
1453 * @param mv1 motion vector1 (relative to block position) to get pixel data from
1454 * @param current_mv current motion vector structure
1456 static void luma_mc_bi(HEVCContext *s, uint8_t *dst, ptrdiff_t dststride,
1457 AVFrame *ref0, const Mv *mv0, int x_off, int y_off,
1458 int block_w, int block_h, AVFrame *ref1, const Mv *mv1, struct MvField *current_mv)
1460 HEVCLocalContext *lc = s->HEVClc;
1461 ptrdiff_t src0stride = ref0->linesize[0];
1462 ptrdiff_t src1stride = ref1->linesize[0];
1463 int pic_width = s->ps.sps->width;
1464 int pic_height = s->ps.sps->height;
1465 int mx0 = mv0->x & 3;
1466 int my0 = mv0->y & 3;
1467 int mx1 = mv1->x & 3;
1468 int my1 = mv1->y & 3;
1469 int weight_flag = (s->sh.slice_type == HEVC_SLICE_P && s->ps.pps->weighted_pred_flag) ||
1470 (s->sh.slice_type == HEVC_SLICE_B && s->ps.pps->weighted_bipred_flag);
1471 int x_off0 = x_off + (mv0->x >> 2);
1472 int y_off0 = y_off + (mv0->y >> 2);
1473 int x_off1 = x_off + (mv1->x >> 2);
1474 int y_off1 = y_off + (mv1->y >> 2);
1475 int idx = ff_hevc_pel_weight[block_w];
1477 uint8_t *src0 = ref0->data[0] + y_off0 * src0stride + (int)((unsigned)x_off0 << s->ps.sps->pixel_shift);
1478 uint8_t *src1 = ref1->data[0] + y_off1 * src1stride + (int)((unsigned)x_off1 << s->ps.sps->pixel_shift);
1480 if (x_off0 < QPEL_EXTRA_BEFORE || y_off0 < QPEL_EXTRA_AFTER ||
1481 x_off0 >= pic_width - block_w - QPEL_EXTRA_AFTER ||
1482 y_off0 >= pic_height - block_h - QPEL_EXTRA_AFTER) {
1483 const ptrdiff_t edge_emu_stride = EDGE_EMU_BUFFER_STRIDE << s->ps.sps->pixel_shift;
1484 int offset = QPEL_EXTRA_BEFORE * src0stride + (QPEL_EXTRA_BEFORE << s->ps.sps->pixel_shift);
1485 int buf_offset = QPEL_EXTRA_BEFORE * edge_emu_stride + (QPEL_EXTRA_BEFORE << s->ps.sps->pixel_shift);
1487 s->vdsp.emulated_edge_mc(lc->edge_emu_buffer, src0 - offset,
1488 edge_emu_stride, src0stride,
1489 block_w + QPEL_EXTRA,
1490 block_h + QPEL_EXTRA,
1491 x_off0 - QPEL_EXTRA_BEFORE, y_off0 - QPEL_EXTRA_BEFORE,
1492 pic_width, pic_height);
1493 src0 = lc->edge_emu_buffer + buf_offset;
1494 src0stride = edge_emu_stride;
1497 if (x_off1 < QPEL_EXTRA_BEFORE || y_off1 < QPEL_EXTRA_AFTER ||
1498 x_off1 >= pic_width - block_w - QPEL_EXTRA_AFTER ||
1499 y_off1 >= pic_height - block_h - QPEL_EXTRA_AFTER) {
1500 const ptrdiff_t edge_emu_stride = EDGE_EMU_BUFFER_STRIDE << s->ps.sps->pixel_shift;
1501 int offset = QPEL_EXTRA_BEFORE * src1stride + (QPEL_EXTRA_BEFORE << s->ps.sps->pixel_shift);
1502 int buf_offset = QPEL_EXTRA_BEFORE * edge_emu_stride + (QPEL_EXTRA_BEFORE << s->ps.sps->pixel_shift);
1504 s->vdsp.emulated_edge_mc(lc->edge_emu_buffer2, src1 - offset,
1505 edge_emu_stride, src1stride,
1506 block_w + QPEL_EXTRA,
1507 block_h + QPEL_EXTRA,
1508 x_off1 - QPEL_EXTRA_BEFORE, y_off1 - QPEL_EXTRA_BEFORE,
1509 pic_width, pic_height);
1510 src1 = lc->edge_emu_buffer2 + buf_offset;
1511 src1stride = edge_emu_stride;
1514 s->hevcdsp.put_hevc_qpel[idx][!!my0][!!mx0](lc->tmp, src0, src0stride,
1515 block_h, mx0, my0, block_w);
1517 s->hevcdsp.put_hevc_qpel_bi[idx][!!my1][!!mx1](dst, dststride, src1, src1stride, lc->tmp,
1518 block_h, mx1, my1, block_w);
1520 s->hevcdsp.put_hevc_qpel_bi_w[idx][!!my1][!!mx1](dst, dststride, src1, src1stride, lc->tmp,
1521 block_h, s->sh.luma_log2_weight_denom,
1522 s->sh.luma_weight_l0[current_mv->ref_idx[0]],
1523 s->sh.luma_weight_l1[current_mv->ref_idx[1]],
1524 s->sh.luma_offset_l0[current_mv->ref_idx[0]],
1525 s->sh.luma_offset_l1[current_mv->ref_idx[1]],
1531 * 8.5.3.2.2.2 Chroma sample uniprediction interpolation process
1533 * @param s HEVC decoding context
1534 * @param dst1 target buffer for block data at block position (U plane)
1535 * @param dst2 target buffer for block data at block position (V plane)
1536 * @param dststride stride of the dst1 and dst2 buffers
1537 * @param ref reference picture buffer at origin (0, 0)
1538 * @param mv motion vector (relative to block position) to get pixel data from
1539 * @param x_off horizontal position of block from origin (0, 0)
1540 * @param y_off vertical position of block from origin (0, 0)
1541 * @param block_w width of block
1542 * @param block_h height of block
1543 * @param chroma_weight weighting factor applied to the chroma prediction
1544 * @param chroma_offset additive offset applied to the chroma prediction value
1547 static void chroma_mc_uni(HEVCContext *s, uint8_t *dst0,
1548 ptrdiff_t dststride, uint8_t *src0, ptrdiff_t srcstride, int reflist,
1549 int x_off, int y_off, int block_w, int block_h, struct MvField *current_mv, int chroma_weight, int chroma_offset)
1551 HEVCLocalContext *lc = s->HEVClc;
1552 int pic_width = s->ps.sps->width >> s->ps.sps->hshift[1];
1553 int pic_height = s->ps.sps->height >> s->ps.sps->vshift[1];
1554 const Mv *mv = ¤t_mv->mv[reflist];
1555 int weight_flag = (s->sh.slice_type == HEVC_SLICE_P && s->ps.pps->weighted_pred_flag) ||
1556 (s->sh.slice_type == HEVC_SLICE_B && s->ps.pps->weighted_bipred_flag);
1557 int idx = ff_hevc_pel_weight[block_w];
1558 int hshift = s->ps.sps->hshift[1];
1559 int vshift = s->ps.sps->vshift[1];
1560 intptr_t mx = av_mod_uintp2(mv->x, 2 + hshift);
1561 intptr_t my = av_mod_uintp2(mv->y, 2 + vshift);
1562 intptr_t _mx = mx << (1 - hshift);
1563 intptr_t _my = my << (1 - vshift);
1565 x_off += mv->x >> (2 + hshift);
1566 y_off += mv->y >> (2 + vshift);
1567 src0 += y_off * srcstride + (x_off * (1 << s->ps.sps->pixel_shift));
1569 if (x_off < EPEL_EXTRA_BEFORE || y_off < EPEL_EXTRA_AFTER ||
1570 x_off >= pic_width - block_w - EPEL_EXTRA_AFTER ||
1571 y_off >= pic_height - block_h - EPEL_EXTRA_AFTER) {
1572 const int edge_emu_stride = EDGE_EMU_BUFFER_STRIDE << s->ps.sps->pixel_shift;
1573 int offset0 = EPEL_EXTRA_BEFORE * (srcstride + (1 << s->ps.sps->pixel_shift));
1574 int buf_offset0 = EPEL_EXTRA_BEFORE *
1575 (edge_emu_stride + (1 << s->ps.sps->pixel_shift));
1576 s->vdsp.emulated_edge_mc(lc->edge_emu_buffer, src0 - offset0,
1577 edge_emu_stride, srcstride,
1578 block_w + EPEL_EXTRA, block_h + EPEL_EXTRA,
1579 x_off - EPEL_EXTRA_BEFORE,
1580 y_off - EPEL_EXTRA_BEFORE,
1581 pic_width, pic_height);
1583 src0 = lc->edge_emu_buffer + buf_offset0;
1584 srcstride = edge_emu_stride;
1587 s->hevcdsp.put_hevc_epel_uni[idx][!!my][!!mx](dst0, dststride, src0, srcstride,
1588 block_h, _mx, _my, block_w);
1590 s->hevcdsp.put_hevc_epel_uni_w[idx][!!my][!!mx](dst0, dststride, src0, srcstride,
1591 block_h, s->sh.chroma_log2_weight_denom,
1592 chroma_weight, chroma_offset, _mx, _my, block_w);
1596 * 8.5.3.2.2.2 Chroma sample bidirectional interpolation process
1598 * @param s HEVC decoding context
1599 * @param dst target buffer for block data at block position
1600 * @param dststride stride of the dst buffer
1601 * @param ref0 reference picture0 buffer at origin (0, 0)
1602 * @param mv0 motion vector0 (relative to block position) to get pixel data from
1603 * @param x_off horizontal position of block from origin (0, 0)
1604 * @param y_off vertical position of block from origin (0, 0)
1605 * @param block_w width of block
1606 * @param block_h height of block
1607 * @param ref1 reference picture1 buffer at origin (0, 0)
1608 * @param mv1 motion vector1 (relative to block position) to get pixel data from
1609 * @param current_mv current motion vector structure
1610 * @param cidx chroma component(cb, cr)
1612 static void chroma_mc_bi(HEVCContext *s, uint8_t *dst0, ptrdiff_t dststride, AVFrame *ref0, AVFrame *ref1,
1613 int x_off, int y_off, int block_w, int block_h, struct MvField *current_mv, int cidx)
1615 HEVCLocalContext *lc = s->HEVClc;
1616 uint8_t *src1 = ref0->data[cidx+1];
1617 uint8_t *src2 = ref1->data[cidx+1];
1618 ptrdiff_t src1stride = ref0->linesize[cidx+1];
1619 ptrdiff_t src2stride = ref1->linesize[cidx+1];
1620 int weight_flag = (s->sh.slice_type == HEVC_SLICE_P && s->ps.pps->weighted_pred_flag) ||
1621 (s->sh.slice_type == HEVC_SLICE_B && s->ps.pps->weighted_bipred_flag);
1622 int pic_width = s->ps.sps->width >> s->ps.sps->hshift[1];
1623 int pic_height = s->ps.sps->height >> s->ps.sps->vshift[1];
1624 Mv *mv0 = ¤t_mv->mv[0];
1625 Mv *mv1 = ¤t_mv->mv[1];
1626 int hshift = s->ps.sps->hshift[1];
1627 int vshift = s->ps.sps->vshift[1];
1629 intptr_t mx0 = av_mod_uintp2(mv0->x, 2 + hshift);
1630 intptr_t my0 = av_mod_uintp2(mv0->y, 2 + vshift);
1631 intptr_t mx1 = av_mod_uintp2(mv1->x, 2 + hshift);
1632 intptr_t my1 = av_mod_uintp2(mv1->y, 2 + vshift);
1633 intptr_t _mx0 = mx0 << (1 - hshift);
1634 intptr_t _my0 = my0 << (1 - vshift);
1635 intptr_t _mx1 = mx1 << (1 - hshift);
1636 intptr_t _my1 = my1 << (1 - vshift);
1638 int x_off0 = x_off + (mv0->x >> (2 + hshift));
1639 int y_off0 = y_off + (mv0->y >> (2 + vshift));
1640 int x_off1 = x_off + (mv1->x >> (2 + hshift));
1641 int y_off1 = y_off + (mv1->y >> (2 + vshift));
1642 int idx = ff_hevc_pel_weight[block_w];
1643 src1 += y_off0 * src1stride + (int)((unsigned)x_off0 << s->ps.sps->pixel_shift);
1644 src2 += y_off1 * src2stride + (int)((unsigned)x_off1 << s->ps.sps->pixel_shift);
1646 if (x_off0 < EPEL_EXTRA_BEFORE || y_off0 < EPEL_EXTRA_AFTER ||
1647 x_off0 >= pic_width - block_w - EPEL_EXTRA_AFTER ||
1648 y_off0 >= pic_height - block_h - EPEL_EXTRA_AFTER) {
1649 const int edge_emu_stride = EDGE_EMU_BUFFER_STRIDE << s->ps.sps->pixel_shift;
1650 int offset1 = EPEL_EXTRA_BEFORE * (src1stride + (1 << s->ps.sps->pixel_shift));
1651 int buf_offset1 = EPEL_EXTRA_BEFORE *
1652 (edge_emu_stride + (1 << s->ps.sps->pixel_shift));
1654 s->vdsp.emulated_edge_mc(lc->edge_emu_buffer, src1 - offset1,
1655 edge_emu_stride, src1stride,
1656 block_w + EPEL_EXTRA, block_h + EPEL_EXTRA,
1657 x_off0 - EPEL_EXTRA_BEFORE,
1658 y_off0 - EPEL_EXTRA_BEFORE,
1659 pic_width, pic_height);
1661 src1 = lc->edge_emu_buffer + buf_offset1;
1662 src1stride = edge_emu_stride;
1665 if (x_off1 < EPEL_EXTRA_BEFORE || y_off1 < EPEL_EXTRA_AFTER ||
1666 x_off1 >= pic_width - block_w - EPEL_EXTRA_AFTER ||
1667 y_off1 >= pic_height - block_h - EPEL_EXTRA_AFTER) {
1668 const int edge_emu_stride = EDGE_EMU_BUFFER_STRIDE << s->ps.sps->pixel_shift;
1669 int offset1 = EPEL_EXTRA_BEFORE * (src2stride + (1 << s->ps.sps->pixel_shift));
1670 int buf_offset1 = EPEL_EXTRA_BEFORE *
1671 (edge_emu_stride + (1 << s->ps.sps->pixel_shift));
1673 s->vdsp.emulated_edge_mc(lc->edge_emu_buffer2, src2 - offset1,
1674 edge_emu_stride, src2stride,
1675 block_w + EPEL_EXTRA, block_h + EPEL_EXTRA,
1676 x_off1 - EPEL_EXTRA_BEFORE,
1677 y_off1 - EPEL_EXTRA_BEFORE,
1678 pic_width, pic_height);
1680 src2 = lc->edge_emu_buffer2 + buf_offset1;
1681 src2stride = edge_emu_stride;
1684 s->hevcdsp.put_hevc_epel[idx][!!my0][!!mx0](lc->tmp, src1, src1stride,
1685 block_h, _mx0, _my0, block_w);
1687 s->hevcdsp.put_hevc_epel_bi[idx][!!my1][!!mx1](dst0, s->frame->linesize[cidx+1],
1688 src2, src2stride, lc->tmp,
1689 block_h, _mx1, _my1, block_w);
1691 s->hevcdsp.put_hevc_epel_bi_w[idx][!!my1][!!mx1](dst0, s->frame->linesize[cidx+1],
1692 src2, src2stride, lc->tmp,
1694 s->sh.chroma_log2_weight_denom,
1695 s->sh.chroma_weight_l0[current_mv->ref_idx[0]][cidx],
1696 s->sh.chroma_weight_l1[current_mv->ref_idx[1]][cidx],
1697 s->sh.chroma_offset_l0[current_mv->ref_idx[0]][cidx],
1698 s->sh.chroma_offset_l1[current_mv->ref_idx[1]][cidx],
1699 _mx1, _my1, block_w);
1702 static void hevc_await_progress(HEVCContext *s, HEVCFrame *ref,
1703 const Mv *mv, int y0, int height)
1705 if (s->threads_type == FF_THREAD_FRAME ) {
1706 int y = FFMAX(0, (mv->y >> 2) + y0 + height + 9);
1708 ff_thread_await_progress(&ref->tf, y, 0);
1712 static void hevc_luma_mv_mvp_mode(HEVCContext *s, int x0, int y0, int nPbW,
1713 int nPbH, int log2_cb_size, int part_idx,
1714 int merge_idx, MvField *mv)
1716 HEVCLocalContext *lc = s->HEVClc;
1717 enum InterPredIdc inter_pred_idc = PRED_L0;
1720 ff_hevc_set_neighbour_available(s, x0, y0, nPbW, nPbH);
1722 if (s->sh.slice_type == HEVC_SLICE_B)
1723 inter_pred_idc = ff_hevc_inter_pred_idc_decode(s, nPbW, nPbH);
1725 if (inter_pred_idc != PRED_L1) {
1726 if (s->sh.nb_refs[L0])
1727 mv->ref_idx[0]= ff_hevc_ref_idx_lx_decode(s, s->sh.nb_refs[L0]);
1729 mv->pred_flag = PF_L0;
1730 ff_hevc_hls_mvd_coding(s, x0, y0, 0);
1731 mvp_flag = ff_hevc_mvp_lx_flag_decode(s);
1732 ff_hevc_luma_mv_mvp_mode(s, x0, y0, nPbW, nPbH, log2_cb_size,
1733 part_idx, merge_idx, mv, mvp_flag, 0);
1734 mv->mv[0].x += lc->pu.mvd.x;
1735 mv->mv[0].y += lc->pu.mvd.y;
1738 if (inter_pred_idc != PRED_L0) {
1739 if (s->sh.nb_refs[L1])
1740 mv->ref_idx[1]= ff_hevc_ref_idx_lx_decode(s, s->sh.nb_refs[L1]);
1742 if (s->sh.mvd_l1_zero_flag == 1 && inter_pred_idc == PRED_BI) {
1743 AV_ZERO32(&lc->pu.mvd);
1745 ff_hevc_hls_mvd_coding(s, x0, y0, 1);
1748 mv->pred_flag += PF_L1;
1749 mvp_flag = ff_hevc_mvp_lx_flag_decode(s);
1750 ff_hevc_luma_mv_mvp_mode(s, x0, y0, nPbW, nPbH, log2_cb_size,
1751 part_idx, merge_idx, mv, mvp_flag, 1);
1752 mv->mv[1].x += lc->pu.mvd.x;
1753 mv->mv[1].y += lc->pu.mvd.y;
1757 static void hls_prediction_unit(HEVCContext *s, int x0, int y0,
1759 int log2_cb_size, int partIdx, int idx)
1761 #define POS(c_idx, x, y) \
1762 &s->frame->data[c_idx][((y) >> s->ps.sps->vshift[c_idx]) * s->frame->linesize[c_idx] + \
1763 (((x) >> s->ps.sps->hshift[c_idx]) << s->ps.sps->pixel_shift)]
1764 HEVCLocalContext *lc = s->HEVClc;
1766 struct MvField current_mv = {{{ 0 }}};
1768 int min_pu_width = s->ps.sps->min_pu_width;
1770 MvField *tab_mvf = s->ref->tab_mvf;
1771 RefPicList *refPicList = s->ref->refPicList;
1772 HEVCFrame *ref0 = NULL, *ref1 = NULL;
1773 uint8_t *dst0 = POS(0, x0, y0);
1774 uint8_t *dst1 = POS(1, x0, y0);
1775 uint8_t *dst2 = POS(2, x0, y0);
1776 int log2_min_cb_size = s->ps.sps->log2_min_cb_size;
1777 int min_cb_width = s->ps.sps->min_cb_width;
1778 int x_cb = x0 >> log2_min_cb_size;
1779 int y_cb = y0 >> log2_min_cb_size;
1783 int skip_flag = SAMPLE_CTB(s->skip_flag, x_cb, y_cb);
1786 lc->pu.merge_flag = ff_hevc_merge_flag_decode(s);
1788 if (skip_flag || lc->pu.merge_flag) {
1789 if (s->sh.max_num_merge_cand > 1)
1790 merge_idx = ff_hevc_merge_idx_decode(s);
1794 ff_hevc_luma_mv_merge_mode(s, x0, y0, nPbW, nPbH, log2_cb_size,
1795 partIdx, merge_idx, ¤t_mv);
1797 hevc_luma_mv_mvp_mode(s, x0, y0, nPbW, nPbH, log2_cb_size,
1798 partIdx, merge_idx, ¤t_mv);
1801 x_pu = x0 >> s->ps.sps->log2_min_pu_size;
1802 y_pu = y0 >> s->ps.sps->log2_min_pu_size;
1804 for (j = 0; j < nPbH >> s->ps.sps->log2_min_pu_size; j++)
1805 for (i = 0; i < nPbW >> s->ps.sps->log2_min_pu_size; i++)
1806 tab_mvf[(y_pu + j) * min_pu_width + x_pu + i] = current_mv;
1808 if (current_mv.pred_flag & PF_L0) {
1809 ref0 = refPicList[0].ref[current_mv.ref_idx[0]];
1812 hevc_await_progress(s, ref0, ¤t_mv.mv[0], y0, nPbH);
1814 if (current_mv.pred_flag & PF_L1) {
1815 ref1 = refPicList[1].ref[current_mv.ref_idx[1]];
1818 hevc_await_progress(s, ref1, ¤t_mv.mv[1], y0, nPbH);
1821 if (current_mv.pred_flag == PF_L0) {
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], ref0->frame,
1828 ¤t_mv.mv[0], x0, y0, nPbW, nPbH,
1829 s->sh.luma_weight_l0[current_mv.ref_idx[0]],
1830 s->sh.luma_offset_l0[current_mv.ref_idx[0]]);
1832 if (s->ps.sps->chroma_format_idc) {
1833 chroma_mc_uni(s, dst1, s->frame->linesize[1], ref0->frame->data[1], ref0->frame->linesize[1],
1834 0, x0_c, y0_c, nPbW_c, nPbH_c, ¤t_mv,
1835 s->sh.chroma_weight_l0[current_mv.ref_idx[0]][0], s->sh.chroma_offset_l0[current_mv.ref_idx[0]][0]);
1836 chroma_mc_uni(s, dst2, s->frame->linesize[2], ref0->frame->data[2], ref0->frame->linesize[2],
1837 0, x0_c, y0_c, nPbW_c, nPbH_c, ¤t_mv,
1838 s->sh.chroma_weight_l0[current_mv.ref_idx[0]][1], s->sh.chroma_offset_l0[current_mv.ref_idx[0]][1]);
1840 } else if (current_mv.pred_flag == PF_L1) {
1841 int x0_c = x0 >> s->ps.sps->hshift[1];
1842 int y0_c = y0 >> s->ps.sps->vshift[1];
1843 int nPbW_c = nPbW >> s->ps.sps->hshift[1];
1844 int nPbH_c = nPbH >> s->ps.sps->vshift[1];
1846 luma_mc_uni(s, dst0, s->frame->linesize[0], ref1->frame,
1847 ¤t_mv.mv[1], x0, y0, nPbW, nPbH,
1848 s->sh.luma_weight_l1[current_mv.ref_idx[1]],
1849 s->sh.luma_offset_l1[current_mv.ref_idx[1]]);
1851 if (s->ps.sps->chroma_format_idc) {
1852 chroma_mc_uni(s, dst1, s->frame->linesize[1], ref1->frame->data[1], ref1->frame->linesize[1],
1853 1, x0_c, y0_c, nPbW_c, nPbH_c, ¤t_mv,
1854 s->sh.chroma_weight_l1[current_mv.ref_idx[1]][0], s->sh.chroma_offset_l1[current_mv.ref_idx[1]][0]);
1856 chroma_mc_uni(s, dst2, s->frame->linesize[2], ref1->frame->data[2], ref1->frame->linesize[2],
1857 1, x0_c, y0_c, nPbW_c, nPbH_c, ¤t_mv,
1858 s->sh.chroma_weight_l1[current_mv.ref_idx[1]][1], s->sh.chroma_offset_l1[current_mv.ref_idx[1]][1]);
1860 } else if (current_mv.pred_flag == PF_BI) {
1861 int x0_c = x0 >> s->ps.sps->hshift[1];
1862 int y0_c = y0 >> s->ps.sps->vshift[1];
1863 int nPbW_c = nPbW >> s->ps.sps->hshift[1];
1864 int nPbH_c = nPbH >> s->ps.sps->vshift[1];
1866 luma_mc_bi(s, dst0, s->frame->linesize[0], ref0->frame,
1867 ¤t_mv.mv[0], x0, y0, nPbW, nPbH,
1868 ref1->frame, ¤t_mv.mv[1], ¤t_mv);
1870 if (s->ps.sps->chroma_format_idc) {
1871 chroma_mc_bi(s, dst1, s->frame->linesize[1], ref0->frame, ref1->frame,
1872 x0_c, y0_c, nPbW_c, nPbH_c, ¤t_mv, 0);
1874 chroma_mc_bi(s, dst2, s->frame->linesize[2], ref0->frame, ref1->frame,
1875 x0_c, y0_c, nPbW_c, nPbH_c, ¤t_mv, 1);
1883 static int luma_intra_pred_mode(HEVCContext *s, int x0, int y0, int pu_size,
1884 int prev_intra_luma_pred_flag)
1886 HEVCLocalContext *lc = s->HEVClc;
1887 int x_pu = x0 >> s->ps.sps->log2_min_pu_size;
1888 int y_pu = y0 >> s->ps.sps->log2_min_pu_size;
1889 int min_pu_width = s->ps.sps->min_pu_width;
1890 int size_in_pus = pu_size >> s->ps.sps->log2_min_pu_size;
1891 int x0b = av_mod_uintp2(x0, s->ps.sps->log2_ctb_size);
1892 int y0b = av_mod_uintp2(y0, s->ps.sps->log2_ctb_size);
1894 int cand_up = (lc->ctb_up_flag || y0b) ?
1895 s->tab_ipm[(y_pu - 1) * min_pu_width + x_pu] : INTRA_DC;
1896 int cand_left = (lc->ctb_left_flag || x0b) ?
1897 s->tab_ipm[y_pu * min_pu_width + x_pu - 1] : INTRA_DC;
1899 int y_ctb = (y0 >> (s->ps.sps->log2_ctb_size)) << (s->ps.sps->log2_ctb_size);
1901 MvField *tab_mvf = s->ref->tab_mvf;
1902 int intra_pred_mode;
1906 // intra_pred_mode prediction does not cross vertical CTB boundaries
1907 if ((y0 - 1) < y_ctb)
1910 if (cand_left == cand_up) {
1911 if (cand_left < 2) {
1912 candidate[0] = INTRA_PLANAR;
1913 candidate[1] = INTRA_DC;
1914 candidate[2] = INTRA_ANGULAR_26;
1916 candidate[0] = cand_left;
1917 candidate[1] = 2 + ((cand_left - 2 - 1 + 32) & 31);
1918 candidate[2] = 2 + ((cand_left - 2 + 1) & 31);
1921 candidate[0] = cand_left;
1922 candidate[1] = cand_up;
1923 if (candidate[0] != INTRA_PLANAR && candidate[1] != INTRA_PLANAR) {
1924 candidate[2] = INTRA_PLANAR;
1925 } else if (candidate[0] != INTRA_DC && candidate[1] != INTRA_DC) {
1926 candidate[2] = INTRA_DC;
1928 candidate[2] = INTRA_ANGULAR_26;
1932 if (prev_intra_luma_pred_flag) {
1933 intra_pred_mode = candidate[lc->pu.mpm_idx];
1935 if (candidate[0] > candidate[1])
1936 FFSWAP(uint8_t, candidate[0], candidate[1]);
1937 if (candidate[0] > candidate[2])
1938 FFSWAP(uint8_t, candidate[0], candidate[2]);
1939 if (candidate[1] > candidate[2])
1940 FFSWAP(uint8_t, candidate[1], candidate[2]);
1942 intra_pred_mode = lc->pu.rem_intra_luma_pred_mode;
1943 for (i = 0; i < 3; i++)
1944 if (intra_pred_mode >= candidate[i])
1948 /* write the intra prediction units into the mv array */
1951 for (i = 0; i < size_in_pus; i++) {
1952 memset(&s->tab_ipm[(y_pu + i) * min_pu_width + x_pu],
1953 intra_pred_mode, size_in_pus);
1955 for (j = 0; j < size_in_pus; j++) {
1956 tab_mvf[(y_pu + j) * min_pu_width + x_pu + i].pred_flag = PF_INTRA;
1960 return intra_pred_mode;
1963 static av_always_inline void set_ct_depth(HEVCContext *s, int x0, int y0,
1964 int log2_cb_size, int ct_depth)
1966 int length = (1 << log2_cb_size) >> s->ps.sps->log2_min_cb_size;
1967 int x_cb = x0 >> s->ps.sps->log2_min_cb_size;
1968 int y_cb = y0 >> s->ps.sps->log2_min_cb_size;
1971 for (y = 0; y < length; y++)
1972 memset(&s->tab_ct_depth[(y_cb + y) * s->ps.sps->min_cb_width + x_cb],
1976 static const uint8_t tab_mode_idx[] = {
1977 0, 1, 2, 2, 2, 2, 3, 5, 7, 8, 10, 12, 13, 15, 17, 18, 19, 20,
1978 21, 22, 23, 23, 24, 24, 25, 25, 26, 27, 27, 28, 28, 29, 29, 30, 31};
1980 static void intra_prediction_unit(HEVCContext *s, int x0, int y0,
1983 HEVCLocalContext *lc = s->HEVClc;
1984 static const uint8_t intra_chroma_table[4] = { 0, 26, 10, 1 };
1985 uint8_t prev_intra_luma_pred_flag[4];
1986 int split = lc->cu.part_mode == PART_NxN;
1987 int pb_size = (1 << log2_cb_size) >> split;
1988 int side = split + 1;
1992 for (i = 0; i < side; i++)
1993 for (j = 0; j < side; j++)
1994 prev_intra_luma_pred_flag[2 * i + j] = ff_hevc_prev_intra_luma_pred_flag_decode(s);
1996 for (i = 0; i < side; i++) {
1997 for (j = 0; j < side; j++) {
1998 if (prev_intra_luma_pred_flag[2 * i + j])
1999 lc->pu.mpm_idx = ff_hevc_mpm_idx_decode(s);
2001 lc->pu.rem_intra_luma_pred_mode = ff_hevc_rem_intra_luma_pred_mode_decode(s);
2003 lc->pu.intra_pred_mode[2 * i + j] =
2004 luma_intra_pred_mode(s, x0 + pb_size * j, y0 + pb_size * i, pb_size,
2005 prev_intra_luma_pred_flag[2 * i + j]);
2009 if (s->ps.sps->chroma_format_idc == 3) {
2010 for (i = 0; i < side; i++) {
2011 for (j = 0; j < side; j++) {
2012 lc->pu.chroma_mode_c[2 * i + j] = chroma_mode = ff_hevc_intra_chroma_pred_mode_decode(s);
2013 if (chroma_mode != 4) {
2014 if (lc->pu.intra_pred_mode[2 * i + j] == intra_chroma_table[chroma_mode])
2015 lc->pu.intra_pred_mode_c[2 * i + j] = 34;
2017 lc->pu.intra_pred_mode_c[2 * i + j] = intra_chroma_table[chroma_mode];
2019 lc->pu.intra_pred_mode_c[2 * i + j] = lc->pu.intra_pred_mode[2 * i + j];
2023 } else if (s->ps.sps->chroma_format_idc == 2) {
2025 lc->pu.chroma_mode_c[0] = chroma_mode = ff_hevc_intra_chroma_pred_mode_decode(s);
2026 if (chroma_mode != 4) {
2027 if (lc->pu.intra_pred_mode[0] == intra_chroma_table[chroma_mode])
2030 mode_idx = intra_chroma_table[chroma_mode];
2032 mode_idx = lc->pu.intra_pred_mode[0];
2034 lc->pu.intra_pred_mode_c[0] = tab_mode_idx[mode_idx];
2035 } else if (s->ps.sps->chroma_format_idc != 0) {
2036 chroma_mode = ff_hevc_intra_chroma_pred_mode_decode(s);
2037 if (chroma_mode != 4) {
2038 if (lc->pu.intra_pred_mode[0] == intra_chroma_table[chroma_mode])
2039 lc->pu.intra_pred_mode_c[0] = 34;
2041 lc->pu.intra_pred_mode_c[0] = intra_chroma_table[chroma_mode];
2043 lc->pu.intra_pred_mode_c[0] = lc->pu.intra_pred_mode[0];
2048 static void intra_prediction_unit_default_value(HEVCContext *s,
2052 HEVCLocalContext *lc = s->HEVClc;
2053 int pb_size = 1 << log2_cb_size;
2054 int size_in_pus = pb_size >> s->ps.sps->log2_min_pu_size;
2055 int min_pu_width = s->ps.sps->min_pu_width;
2056 MvField *tab_mvf = s->ref->tab_mvf;
2057 int x_pu = x0 >> s->ps.sps->log2_min_pu_size;
2058 int y_pu = y0 >> s->ps.sps->log2_min_pu_size;
2061 if (size_in_pus == 0)
2063 for (j = 0; j < size_in_pus; j++)
2064 memset(&s->tab_ipm[(y_pu + j) * min_pu_width + x_pu], INTRA_DC, size_in_pus);
2065 if (lc->cu.pred_mode == MODE_INTRA)
2066 for (j = 0; j < size_in_pus; j++)
2067 for (k = 0; k < size_in_pus; k++)
2068 tab_mvf[(y_pu + j) * min_pu_width + x_pu + k].pred_flag = PF_INTRA;
2071 static int hls_coding_unit(HEVCContext *s, int x0, int y0, int log2_cb_size)
2073 int cb_size = 1 << log2_cb_size;
2074 HEVCLocalContext *lc = s->HEVClc;
2075 int log2_min_cb_size = s->ps.sps->log2_min_cb_size;
2076 int length = cb_size >> log2_min_cb_size;
2077 int min_cb_width = s->ps.sps->min_cb_width;
2078 int x_cb = x0 >> log2_min_cb_size;
2079 int y_cb = y0 >> log2_min_cb_size;
2080 int idx = log2_cb_size - 2;
2081 int qp_block_mask = (1<<(s->ps.sps->log2_ctb_size - s->ps.pps->diff_cu_qp_delta_depth)) - 1;
2086 lc->cu.pred_mode = MODE_INTRA;
2087 lc->cu.part_mode = PART_2Nx2N;
2088 lc->cu.intra_split_flag = 0;
2090 SAMPLE_CTB(s->skip_flag, x_cb, y_cb) = 0;
2091 for (x = 0; x < 4; x++)
2092 lc->pu.intra_pred_mode[x] = 1;
2093 if (s->ps.pps->transquant_bypass_enable_flag) {
2094 lc->cu.cu_transquant_bypass_flag = ff_hevc_cu_transquant_bypass_flag_decode(s);
2095 if (lc->cu.cu_transquant_bypass_flag)
2096 set_deblocking_bypass(s, x0, y0, log2_cb_size);
2098 lc->cu.cu_transquant_bypass_flag = 0;
2100 if (s->sh.slice_type != HEVC_SLICE_I) {
2101 uint8_t skip_flag = ff_hevc_skip_flag_decode(s, x0, y0, x_cb, y_cb);
2103 x = y_cb * min_cb_width + x_cb;
2104 for (y = 0; y < length; y++) {
2105 memset(&s->skip_flag[x], skip_flag, length);
2108 lc->cu.pred_mode = skip_flag ? MODE_SKIP : MODE_INTER;
2110 x = y_cb * min_cb_width + x_cb;
2111 for (y = 0; y < length; y++) {
2112 memset(&s->skip_flag[x], 0, length);
2117 if (SAMPLE_CTB(s->skip_flag, x_cb, y_cb)) {
2118 hls_prediction_unit(s, x0, y0, cb_size, cb_size, log2_cb_size, 0, idx);
2119 intra_prediction_unit_default_value(s, x0, y0, log2_cb_size);
2121 if (!s->sh.disable_deblocking_filter_flag)
2122 ff_hevc_deblocking_boundary_strengths(s, x0, y0, log2_cb_size);
2126 if (s->sh.slice_type != HEVC_SLICE_I)
2127 lc->cu.pred_mode = ff_hevc_pred_mode_decode(s);
2128 if (lc->cu.pred_mode != MODE_INTRA ||
2129 log2_cb_size == s->ps.sps->log2_min_cb_size) {
2130 lc->cu.part_mode = ff_hevc_part_mode_decode(s, log2_cb_size);
2131 lc->cu.intra_split_flag = lc->cu.part_mode == PART_NxN &&
2132 lc->cu.pred_mode == MODE_INTRA;
2135 if (lc->cu.pred_mode == MODE_INTRA) {
2136 if (lc->cu.part_mode == PART_2Nx2N && s->ps.sps->pcm_enabled_flag &&
2137 log2_cb_size >= s->ps.sps->pcm.log2_min_pcm_cb_size &&
2138 log2_cb_size <= s->ps.sps->pcm.log2_max_pcm_cb_size) {
2139 pcm_flag = ff_hevc_pcm_flag_decode(s);
2142 intra_prediction_unit_default_value(s, x0, y0, log2_cb_size);
2143 ret = hls_pcm_sample(s, x0, y0, log2_cb_size);
2144 if (s->ps.sps->pcm.loop_filter_disable_flag)
2145 set_deblocking_bypass(s, x0, y0, log2_cb_size);
2150 intra_prediction_unit(s, x0, y0, log2_cb_size);
2153 intra_prediction_unit_default_value(s, x0, y0, log2_cb_size);
2154 switch (lc->cu.part_mode) {
2156 hls_prediction_unit(s, x0, y0, cb_size, cb_size, log2_cb_size, 0, idx);
2159 hls_prediction_unit(s, x0, y0, cb_size, cb_size / 2, log2_cb_size, 0, idx);
2160 hls_prediction_unit(s, x0, y0 + cb_size / 2, cb_size, cb_size / 2, log2_cb_size, 1, idx);
2163 hls_prediction_unit(s, x0, y0, cb_size / 2, cb_size, log2_cb_size, 0, idx - 1);
2164 hls_prediction_unit(s, x0 + cb_size / 2, y0, cb_size / 2, cb_size, log2_cb_size, 1, idx - 1);
2167 hls_prediction_unit(s, x0, y0, cb_size, cb_size / 4, log2_cb_size, 0, idx);
2168 hls_prediction_unit(s, x0, y0 + cb_size / 4, cb_size, cb_size * 3 / 4, log2_cb_size, 1, idx);
2171 hls_prediction_unit(s, x0, y0, cb_size, cb_size * 3 / 4, log2_cb_size, 0, idx);
2172 hls_prediction_unit(s, x0, y0 + cb_size * 3 / 4, cb_size, cb_size / 4, log2_cb_size, 1, idx);
2175 hls_prediction_unit(s, x0, y0, cb_size / 4, cb_size, log2_cb_size, 0, idx - 2);
2176 hls_prediction_unit(s, x0 + cb_size / 4, y0, cb_size * 3 / 4, cb_size, log2_cb_size, 1, idx - 2);
2179 hls_prediction_unit(s, x0, y0, cb_size * 3 / 4, cb_size, log2_cb_size, 0, idx - 2);
2180 hls_prediction_unit(s, x0 + cb_size * 3 / 4, y0, cb_size / 4, cb_size, log2_cb_size, 1, idx - 2);
2183 hls_prediction_unit(s, x0, y0, cb_size / 2, cb_size / 2, log2_cb_size, 0, idx - 1);
2184 hls_prediction_unit(s, x0 + cb_size / 2, y0, cb_size / 2, cb_size / 2, log2_cb_size, 1, idx - 1);
2185 hls_prediction_unit(s, x0, y0 + cb_size / 2, cb_size / 2, cb_size / 2, log2_cb_size, 2, idx - 1);
2186 hls_prediction_unit(s, x0 + cb_size / 2, y0 + cb_size / 2, cb_size / 2, cb_size / 2, log2_cb_size, 3, idx - 1);
2192 int rqt_root_cbf = 1;
2194 if (lc->cu.pred_mode != MODE_INTRA &&
2195 !(lc->cu.part_mode == PART_2Nx2N && lc->pu.merge_flag)) {
2196 rqt_root_cbf = ff_hevc_no_residual_syntax_flag_decode(s);
2199 const static int cbf[2] = { 0 };
2200 lc->cu.max_trafo_depth = lc->cu.pred_mode == MODE_INTRA ?
2201 s->ps.sps->max_transform_hierarchy_depth_intra + lc->cu.intra_split_flag :
2202 s->ps.sps->max_transform_hierarchy_depth_inter;
2203 ret = hls_transform_tree(s, x0, y0, x0, y0, x0, y0,
2205 log2_cb_size, 0, 0, cbf, cbf);
2209 if (!s->sh.disable_deblocking_filter_flag)
2210 ff_hevc_deblocking_boundary_strengths(s, x0, y0, log2_cb_size);
2215 if (s->ps.pps->cu_qp_delta_enabled_flag && lc->tu.is_cu_qp_delta_coded == 0)
2216 ff_hevc_set_qPy(s, x0, y0, log2_cb_size);
2218 x = y_cb * min_cb_width + x_cb;
2219 for (y = 0; y < length; y++) {
2220 memset(&s->qp_y_tab[x], lc->qp_y, length);
2224 if(((x0 + (1<<log2_cb_size)) & qp_block_mask) == 0 &&
2225 ((y0 + (1<<log2_cb_size)) & qp_block_mask) == 0) {
2226 lc->qPy_pred = lc->qp_y;
2229 set_ct_depth(s, x0, y0, log2_cb_size, lc->ct_depth);
2234 static int hls_coding_quadtree(HEVCContext *s, int x0, int y0,
2235 int log2_cb_size, int cb_depth)
2237 HEVCLocalContext *lc = s->HEVClc;
2238 const int cb_size = 1 << log2_cb_size;
2242 lc->ct_depth = cb_depth;
2243 if (x0 + cb_size <= s->ps.sps->width &&
2244 y0 + cb_size <= s->ps.sps->height &&
2245 log2_cb_size > s->ps.sps->log2_min_cb_size) {
2246 split_cu = ff_hevc_split_coding_unit_flag_decode(s, cb_depth, x0, y0);
2248 split_cu = (log2_cb_size > s->ps.sps->log2_min_cb_size);
2250 if (s->ps.pps->cu_qp_delta_enabled_flag &&
2251 log2_cb_size >= s->ps.sps->log2_ctb_size - s->ps.pps->diff_cu_qp_delta_depth) {
2252 lc->tu.is_cu_qp_delta_coded = 0;
2253 lc->tu.cu_qp_delta = 0;
2256 if (s->sh.cu_chroma_qp_offset_enabled_flag &&
2257 log2_cb_size >= s->ps.sps->log2_ctb_size - s->ps.pps->diff_cu_chroma_qp_offset_depth) {
2258 lc->tu.is_cu_chroma_qp_offset_coded = 0;
2262 int qp_block_mask = (1<<(s->ps.sps->log2_ctb_size - s->ps.pps->diff_cu_qp_delta_depth)) - 1;
2263 const int cb_size_split = cb_size >> 1;
2264 const int x1 = x0 + cb_size_split;
2265 const int y1 = y0 + cb_size_split;
2269 more_data = hls_coding_quadtree(s, x0, y0, log2_cb_size - 1, cb_depth + 1);
2273 if (more_data && x1 < s->ps.sps->width) {
2274 more_data = hls_coding_quadtree(s, x1, y0, log2_cb_size - 1, cb_depth + 1);
2278 if (more_data && y1 < s->ps.sps->height) {
2279 more_data = hls_coding_quadtree(s, x0, y1, log2_cb_size - 1, cb_depth + 1);
2283 if (more_data && x1 < s->ps.sps->width &&
2284 y1 < s->ps.sps->height) {
2285 more_data = hls_coding_quadtree(s, x1, y1, log2_cb_size - 1, cb_depth + 1);
2290 if(((x0 + (1<<log2_cb_size)) & qp_block_mask) == 0 &&
2291 ((y0 + (1<<log2_cb_size)) & qp_block_mask) == 0)
2292 lc->qPy_pred = lc->qp_y;
2295 return ((x1 + cb_size_split) < s->ps.sps->width ||
2296 (y1 + cb_size_split) < s->ps.sps->height);
2300 ret = hls_coding_unit(s, x0, y0, log2_cb_size);
2303 if ((!((x0 + cb_size) %
2304 (1 << (s->ps.sps->log2_ctb_size))) ||
2305 (x0 + cb_size >= s->ps.sps->width)) &&
2307 (1 << (s->ps.sps->log2_ctb_size))) ||
2308 (y0 + cb_size >= s->ps.sps->height))) {
2309 int end_of_slice_flag = ff_hevc_end_of_slice_flag_decode(s);
2310 return !end_of_slice_flag;
2319 static void hls_decode_neighbour(HEVCContext *s, int x_ctb, int y_ctb,
2322 HEVCLocalContext *lc = s->HEVClc;
2323 int ctb_size = 1 << s->ps.sps->log2_ctb_size;
2324 int ctb_addr_rs = s->ps.pps->ctb_addr_ts_to_rs[ctb_addr_ts];
2325 int ctb_addr_in_slice = ctb_addr_rs - s->sh.slice_addr;
2327 s->tab_slice_address[ctb_addr_rs] = s->sh.slice_addr;
2329 if (s->ps.pps->entropy_coding_sync_enabled_flag) {
2330 if (x_ctb == 0 && (y_ctb & (ctb_size - 1)) == 0)
2331 lc->first_qp_group = 1;
2332 lc->end_of_tiles_x = s->ps.sps->width;
2333 } else if (s->ps.pps->tiles_enabled_flag) {
2334 if (ctb_addr_ts && s->ps.pps->tile_id[ctb_addr_ts] != s->ps.pps->tile_id[ctb_addr_ts - 1]) {
2335 int idxX = s->ps.pps->col_idxX[x_ctb >> s->ps.sps->log2_ctb_size];
2336 lc->end_of_tiles_x = x_ctb + (s->ps.pps->column_width[idxX] << s->ps.sps->log2_ctb_size);
2337 lc->first_qp_group = 1;
2340 lc->end_of_tiles_x = s->ps.sps->width;
2343 lc->end_of_tiles_y = FFMIN(y_ctb + ctb_size, s->ps.sps->height);
2345 lc->boundary_flags = 0;
2346 if (s->ps.pps->tiles_enabled_flag) {
2347 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]])
2348 lc->boundary_flags |= BOUNDARY_LEFT_TILE;
2349 if (x_ctb > 0 && s->tab_slice_address[ctb_addr_rs] != s->tab_slice_address[ctb_addr_rs - 1])
2350 lc->boundary_flags |= BOUNDARY_LEFT_SLICE;
2351 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]])
2352 lc->boundary_flags |= BOUNDARY_UPPER_TILE;
2353 if (y_ctb > 0 && s->tab_slice_address[ctb_addr_rs] != s->tab_slice_address[ctb_addr_rs - s->ps.sps->ctb_width])
2354 lc->boundary_flags |= BOUNDARY_UPPER_SLICE;
2356 if (ctb_addr_in_slice <= 0)
2357 lc->boundary_flags |= BOUNDARY_LEFT_SLICE;
2358 if (ctb_addr_in_slice < s->ps.sps->ctb_width)
2359 lc->boundary_flags |= BOUNDARY_UPPER_SLICE;
2362 lc->ctb_left_flag = ((x_ctb > 0) && (ctb_addr_in_slice > 0) && !(lc->boundary_flags & BOUNDARY_LEFT_TILE));
2363 lc->ctb_up_flag = ((y_ctb > 0) && (ctb_addr_in_slice >= s->ps.sps->ctb_width) && !(lc->boundary_flags & BOUNDARY_UPPER_TILE));
2364 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]]));
2365 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]]));
2368 static int hls_decode_entry(AVCodecContext *avctxt, void *isFilterThread)
2370 HEVCContext *s = avctxt->priv_data;
2371 int ctb_size = 1 << s->ps.sps->log2_ctb_size;
2375 int ctb_addr_ts = s->ps.pps->ctb_addr_rs_to_ts[s->sh.slice_ctb_addr_rs];
2378 if (!ctb_addr_ts && s->sh.dependent_slice_segment_flag) {
2379 av_log(s->avctx, AV_LOG_ERROR, "Impossible initial tile.\n");
2380 return AVERROR_INVALIDDATA;
2383 if (s->sh.dependent_slice_segment_flag) {
2384 int prev_rs = s->ps.pps->ctb_addr_ts_to_rs[ctb_addr_ts - 1];
2385 if (s->tab_slice_address[prev_rs] != s->sh.slice_addr) {
2386 av_log(s->avctx, AV_LOG_ERROR, "Previous slice segment missing\n");
2387 return AVERROR_INVALIDDATA;
2391 while (more_data && ctb_addr_ts < s->ps.sps->ctb_size) {
2392 int ctb_addr_rs = s->ps.pps->ctb_addr_ts_to_rs[ctb_addr_ts];
2394 x_ctb = (ctb_addr_rs % ((s->ps.sps->width + ctb_size - 1) >> s->ps.sps->log2_ctb_size)) << s->ps.sps->log2_ctb_size;
2395 y_ctb = (ctb_addr_rs / ((s->ps.sps->width + ctb_size - 1) >> s->ps.sps->log2_ctb_size)) << s->ps.sps->log2_ctb_size;
2396 hls_decode_neighbour(s, x_ctb, y_ctb, ctb_addr_ts);
2398 ret = ff_hevc_cabac_init(s, ctb_addr_ts);
2400 s->tab_slice_address[ctb_addr_rs] = -1;
2404 hls_sao_param(s, x_ctb >> s->ps.sps->log2_ctb_size, y_ctb >> s->ps.sps->log2_ctb_size);
2406 s->deblock[ctb_addr_rs].beta_offset = s->sh.beta_offset;
2407 s->deblock[ctb_addr_rs].tc_offset = s->sh.tc_offset;
2408 s->filter_slice_edges[ctb_addr_rs] = s->sh.slice_loop_filter_across_slices_enabled_flag;
2410 more_data = hls_coding_quadtree(s, x_ctb, y_ctb, s->ps.sps->log2_ctb_size, 0);
2411 if (more_data < 0) {
2412 s->tab_slice_address[ctb_addr_rs] = -1;
2418 ff_hevc_save_states(s, ctb_addr_ts);
2419 ff_hevc_hls_filters(s, x_ctb, y_ctb, ctb_size);
2422 if (x_ctb + ctb_size >= s->ps.sps->width &&
2423 y_ctb + ctb_size >= s->ps.sps->height)
2424 ff_hevc_hls_filter(s, x_ctb, y_ctb, ctb_size);
2429 static int hls_slice_data(HEVCContext *s)
2437 s->avctx->execute(s->avctx, hls_decode_entry, arg, ret , 1, sizeof(int));
2440 static int hls_decode_entry_wpp(AVCodecContext *avctxt, void *input_ctb_row, int job, int self_id)
2442 HEVCContext *s1 = avctxt->priv_data, *s;
2443 HEVCLocalContext *lc;
2444 int ctb_size = 1<< s1->ps.sps->log2_ctb_size;
2446 int *ctb_row_p = input_ctb_row;
2447 int ctb_row = ctb_row_p[job];
2448 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);
2449 int ctb_addr_ts = s1->ps.pps->ctb_addr_rs_to_ts[ctb_addr_rs];
2450 int thread = ctb_row % s1->threads_number;
2453 s = s1->sList[self_id];
2457 ret = init_get_bits8(&lc->gb, s->data + s->sh.offset[ctb_row - 1], s->sh.size[ctb_row - 1]);
2460 ff_init_cabac_decoder(&lc->cc, s->data + s->sh.offset[(ctb_row)-1], s->sh.size[ctb_row - 1]);
2463 while(more_data && ctb_addr_ts < s->ps.sps->ctb_size) {
2464 int x_ctb = (ctb_addr_rs % s->ps.sps->ctb_width) << s->ps.sps->log2_ctb_size;
2465 int y_ctb = (ctb_addr_rs / s->ps.sps->ctb_width) << s->ps.sps->log2_ctb_size;
2467 hls_decode_neighbour(s, x_ctb, y_ctb, ctb_addr_ts);
2469 ff_thread_await_progress2(s->avctx, ctb_row, thread, SHIFT_CTB_WPP);
2471 if (atomic_load(&s1->wpp_err)) {
2472 ff_thread_report_progress2(s->avctx, ctb_row , thread, SHIFT_CTB_WPP);
2476 ret = ff_hevc_cabac_init(s, ctb_addr_ts);
2479 hls_sao_param(s, x_ctb >> s->ps.sps->log2_ctb_size, y_ctb >> s->ps.sps->log2_ctb_size);
2480 more_data = hls_coding_quadtree(s, x_ctb, y_ctb, s->ps.sps->log2_ctb_size, 0);
2482 if (more_data < 0) {
2489 ff_hevc_save_states(s, ctb_addr_ts);
2490 ff_thread_report_progress2(s->avctx, ctb_row, thread, 1);
2491 ff_hevc_hls_filters(s, x_ctb, y_ctb, ctb_size);
2493 if (!more_data && (x_ctb+ctb_size) < s->ps.sps->width && ctb_row != s->sh.num_entry_point_offsets) {
2494 atomic_store(&s1->wpp_err, 1);
2495 ff_thread_report_progress2(s->avctx, ctb_row ,thread, SHIFT_CTB_WPP);
2499 if ((x_ctb+ctb_size) >= s->ps.sps->width && (y_ctb+ctb_size) >= s->ps.sps->height ) {
2500 ff_hevc_hls_filter(s, x_ctb, y_ctb, ctb_size);
2501 ff_thread_report_progress2(s->avctx, ctb_row , thread, SHIFT_CTB_WPP);
2504 ctb_addr_rs = s->ps.pps->ctb_addr_ts_to_rs[ctb_addr_ts];
2507 if(x_ctb >= s->ps.sps->width) {
2511 ff_thread_report_progress2(s->avctx, ctb_row ,thread, SHIFT_CTB_WPP);
2515 s->tab_slice_address[ctb_addr_rs] = -1;
2516 atomic_store(&s1->wpp_err, 1);
2517 ff_thread_report_progress2(s->avctx, ctb_row ,thread, SHIFT_CTB_WPP);
2521 static int hls_slice_data_wpp(HEVCContext *s, const H2645NAL *nal)
2523 const uint8_t *data = nal->data;
2524 int length = nal->size;
2525 HEVCLocalContext *lc = s->HEVClc;
2526 int *ret = av_malloc_array(s->sh.num_entry_point_offsets + 1, sizeof(int));
2527 int *arg = av_malloc_array(s->sh.num_entry_point_offsets + 1, sizeof(int));
2529 int64_t startheader, cmpt = 0;
2535 return AVERROR(ENOMEM);
2538 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) {
2539 av_log(s->avctx, AV_LOG_ERROR, "WPP ctb addresses are wrong (%d %d %d %d)\n",
2540 s->sh.slice_ctb_addr_rs, s->sh.num_entry_point_offsets,
2541 s->ps.sps->ctb_width, s->ps.sps->ctb_height
2543 res = AVERROR_INVALIDDATA;
2547 ff_alloc_entries(s->avctx, s->sh.num_entry_point_offsets + 1);
2550 for (i = 1; i < s->threads_number; i++) {
2551 s->sList[i] = av_malloc(sizeof(HEVCContext));
2552 memcpy(s->sList[i], s, sizeof(HEVCContext));
2553 s->HEVClcList[i] = av_mallocz(sizeof(HEVCLocalContext));
2554 s->sList[i]->HEVClc = s->HEVClcList[i];
2558 offset = (lc->gb.index >> 3);
2560 for (j = 0, cmpt = 0, startheader = offset + s->sh.entry_point_offset[0]; j < nal->skipped_bytes; j++) {
2561 if (nal->skipped_bytes_pos[j] >= offset && nal->skipped_bytes_pos[j] < startheader) {
2567 for (i = 1; i < s->sh.num_entry_point_offsets; i++) {
2568 offset += (s->sh.entry_point_offset[i - 1] - cmpt);
2569 for (j = 0, cmpt = 0, startheader = offset
2570 + s->sh.entry_point_offset[i]; j < nal->skipped_bytes; j++) {
2571 if (nal->skipped_bytes_pos[j] >= offset && nal->skipped_bytes_pos[j] < startheader) {
2576 s->sh.size[i - 1] = s->sh.entry_point_offset[i] - cmpt;
2577 s->sh.offset[i - 1] = offset;
2580 if (s->sh.num_entry_point_offsets != 0) {
2581 offset += s->sh.entry_point_offset[s->sh.num_entry_point_offsets - 1] - cmpt;
2582 if (length < offset) {
2583 av_log(s->avctx, AV_LOG_ERROR, "entry_point_offset table is corrupted\n");
2584 res = AVERROR_INVALIDDATA;
2587 s->sh.size[s->sh.num_entry_point_offsets - 1] = length - offset;
2588 s->sh.offset[s->sh.num_entry_point_offsets - 1] = offset;
2593 for (i = 1; i < s->threads_number; i++) {
2594 s->sList[i]->HEVClc->first_qp_group = 1;
2595 s->sList[i]->HEVClc->qp_y = s->sList[0]->HEVClc->qp_y;
2596 memcpy(s->sList[i], s, sizeof(HEVCContext));
2597 s->sList[i]->HEVClc = s->HEVClcList[i];
2600 atomic_store(&s->wpp_err, 0);
2601 ff_reset_entries(s->avctx);
2603 for (i = 0; i <= s->sh.num_entry_point_offsets; i++) {
2608 if (s->ps.pps->entropy_coding_sync_enabled_flag)
2609 s->avctx->execute2(s->avctx, hls_decode_entry_wpp, arg, ret, s->sh.num_entry_point_offsets + 1);
2611 for (i = 0; i <= s->sh.num_entry_point_offsets; i++)
2619 static int set_side_data(HEVCContext *s)
2621 AVFrame *out = s->ref->frame;
2623 if (s->sei.frame_packing.present &&
2624 s->sei.frame_packing.arrangement_type >= 3 &&
2625 s->sei.frame_packing.arrangement_type <= 5 &&
2626 s->sei.frame_packing.content_interpretation_type > 0 &&
2627 s->sei.frame_packing.content_interpretation_type < 3) {
2628 AVStereo3D *stereo = av_stereo3d_create_side_data(out);
2630 return AVERROR(ENOMEM);
2632 switch (s->sei.frame_packing.arrangement_type) {
2634 if (s->sei.frame_packing.quincunx_subsampling)
2635 stereo->type = AV_STEREO3D_SIDEBYSIDE_QUINCUNX;
2637 stereo->type = AV_STEREO3D_SIDEBYSIDE;
2640 stereo->type = AV_STEREO3D_TOPBOTTOM;
2643 stereo->type = AV_STEREO3D_FRAMESEQUENCE;
2647 if (s->sei.frame_packing.content_interpretation_type == 2)
2648 stereo->flags = AV_STEREO3D_FLAG_INVERT;
2651 if (s->sei.display_orientation.present &&
2652 (s->sei.display_orientation.anticlockwise_rotation ||
2653 s->sei.display_orientation.hflip || s->sei.display_orientation.vflip)) {
2654 double angle = s->sei.display_orientation.anticlockwise_rotation * 360 / (double) (1 << 16);
2655 AVFrameSideData *rotation = av_frame_new_side_data(out,
2656 AV_FRAME_DATA_DISPLAYMATRIX,
2657 sizeof(int32_t) * 9);
2659 return AVERROR(ENOMEM);
2661 av_display_rotation_set((int32_t *)rotation->data, angle);
2662 av_display_matrix_flip((int32_t *)rotation->data,
2663 s->sei.display_orientation.hflip,
2664 s->sei.display_orientation.vflip);
2667 // Decrement the mastering display flag when IRAP frame has no_rasl_output_flag=1
2668 // so the side data persists for the entire coded video sequence.
2669 if (s->sei.mastering_display.present > 0 &&
2670 IS_IRAP(s) && s->no_rasl_output_flag) {
2671 s->sei.mastering_display.present--;
2673 if (s->sei.mastering_display.present) {
2674 // HEVC uses a g,b,r ordering, which we convert to a more natural r,g,b
2675 const int mapping[3] = {2, 0, 1};
2676 const int chroma_den = 50000;
2677 const int luma_den = 10000;
2679 AVMasteringDisplayMetadata *metadata =
2680 av_mastering_display_metadata_create_side_data(out);
2682 return AVERROR(ENOMEM);
2684 for (i = 0; i < 3; i++) {
2685 const int j = mapping[i];
2686 metadata->display_primaries[i][0].num = s->sei.mastering_display.display_primaries[j][0];
2687 metadata->display_primaries[i][0].den = chroma_den;
2688 metadata->display_primaries[i][1].num = s->sei.mastering_display.display_primaries[j][1];
2689 metadata->display_primaries[i][1].den = chroma_den;
2691 metadata->white_point[0].num = s->sei.mastering_display.white_point[0];
2692 metadata->white_point[0].den = chroma_den;
2693 metadata->white_point[1].num = s->sei.mastering_display.white_point[1];
2694 metadata->white_point[1].den = chroma_den;
2696 metadata->max_luminance.num = s->sei.mastering_display.max_luminance;
2697 metadata->max_luminance.den = luma_den;
2698 metadata->min_luminance.num = s->sei.mastering_display.min_luminance;
2699 metadata->min_luminance.den = luma_den;
2700 metadata->has_luminance = 1;
2701 metadata->has_primaries = 1;
2703 av_log(s->avctx, AV_LOG_DEBUG, "Mastering Display Metadata:\n");
2704 av_log(s->avctx, AV_LOG_DEBUG,
2705 "r(%5.4f,%5.4f) g(%5.4f,%5.4f) b(%5.4f %5.4f) wp(%5.4f, %5.4f)\n",
2706 av_q2d(metadata->display_primaries[0][0]),
2707 av_q2d(metadata->display_primaries[0][1]),
2708 av_q2d(metadata->display_primaries[1][0]),
2709 av_q2d(metadata->display_primaries[1][1]),
2710 av_q2d(metadata->display_primaries[2][0]),
2711 av_q2d(metadata->display_primaries[2][1]),
2712 av_q2d(metadata->white_point[0]), av_q2d(metadata->white_point[1]));
2713 av_log(s->avctx, AV_LOG_DEBUG,
2714 "min_luminance=%f, max_luminance=%f\n",
2715 av_q2d(metadata->min_luminance), av_q2d(metadata->max_luminance));
2717 // Decrement the mastering display flag when IRAP frame has no_rasl_output_flag=1
2718 // so the side data persists for the entire coded video sequence.
2719 if (s->sei.content_light.present > 0 &&
2720 IS_IRAP(s) && s->no_rasl_output_flag) {
2721 s->sei.content_light.present--;
2723 if (s->sei.content_light.present) {
2724 AVContentLightMetadata *metadata =
2725 av_content_light_metadata_create_side_data(out);
2727 return AVERROR(ENOMEM);
2728 metadata->MaxCLL = s->sei.content_light.max_content_light_level;
2729 metadata->MaxFALL = s->sei.content_light.max_pic_average_light_level;
2731 av_log(s->avctx, AV_LOG_DEBUG, "Content Light Level Metadata:\n");
2732 av_log(s->avctx, AV_LOG_DEBUG, "MaxCLL=%d, MaxFALL=%d\n",
2733 metadata->MaxCLL, metadata->MaxFALL);
2736 if (s->sei.a53_caption.a53_caption) {
2737 AVFrameSideData* sd = av_frame_new_side_data(out,
2738 AV_FRAME_DATA_A53_CC,
2739 s->sei.a53_caption.a53_caption_size);
2741 memcpy(sd->data, s->sei.a53_caption.a53_caption, s->sei.a53_caption.a53_caption_size);
2742 av_freep(&s->sei.a53_caption.a53_caption);
2743 s->sei.a53_caption.a53_caption_size = 0;
2744 s->avctx->properties |= FF_CODEC_PROPERTY_CLOSED_CAPTIONS;
2747 if (s->sei.alternative_transfer.present &&
2748 av_color_transfer_name(s->sei.alternative_transfer.preferred_transfer_characteristics) &&
2749 s->sei.alternative_transfer.preferred_transfer_characteristics != AVCOL_TRC_UNSPECIFIED) {
2750 s->avctx->color_trc = out->color_trc = s->sei.alternative_transfer.preferred_transfer_characteristics;
2756 static int hevc_frame_start(HEVCContext *s)
2758 HEVCLocalContext *lc = s->HEVClc;
2759 int pic_size_in_ctb = ((s->ps.sps->width >> s->ps.sps->log2_min_cb_size) + 1) *
2760 ((s->ps.sps->height >> s->ps.sps->log2_min_cb_size) + 1);
2763 memset(s->horizontal_bs, 0, s->bs_width * s->bs_height);
2764 memset(s->vertical_bs, 0, s->bs_width * s->bs_height);
2765 memset(s->cbf_luma, 0, s->ps.sps->min_tb_width * s->ps.sps->min_tb_height);
2766 memset(s->is_pcm, 0, (s->ps.sps->min_pu_width + 1) * (s->ps.sps->min_pu_height + 1));
2767 memset(s->tab_slice_address, -1, pic_size_in_ctb * sizeof(*s->tab_slice_address));
2770 s->first_nal_type = s->nal_unit_type;
2772 s->no_rasl_output_flag = IS_IDR(s) || IS_BLA(s) || (s->nal_unit_type == HEVC_NAL_CRA_NUT && s->last_eos);
2774 if (s->ps.pps->tiles_enabled_flag)
2775 lc->end_of_tiles_x = s->ps.pps->column_width[0] << s->ps.sps->log2_ctb_size;
2777 ret = ff_hevc_set_new_ref(s, &s->frame, s->poc);
2781 ret = ff_hevc_frame_rps(s);
2783 av_log(s->avctx, AV_LOG_ERROR, "Error constructing the frame RPS.\n");
2787 s->ref->frame->key_frame = IS_IRAP(s);
2789 ret = set_side_data(s);
2793 s->frame->pict_type = 3 - s->sh.slice_type;
2796 ff_hevc_bump_frame(s);
2798 av_frame_unref(s->output_frame);
2799 ret = ff_hevc_output_frame(s, s->output_frame, 0);
2803 if (!s->avctx->hwaccel)
2804 ff_thread_finish_setup(s->avctx);
2810 ff_hevc_unref_frame(s, s->ref, ~0);
2815 static int decode_nal_unit(HEVCContext *s, const H2645NAL *nal)
2817 HEVCLocalContext *lc = s->HEVClc;
2818 GetBitContext *gb = &lc->gb;
2819 int ctb_addr_ts, ret;
2822 s->nal_unit_type = nal->type;
2823 s->temporal_id = nal->temporal_id;
2825 switch (s->nal_unit_type) {
2827 if (s->avctx->hwaccel && s->avctx->hwaccel->decode_params) {
2828 ret = s->avctx->hwaccel->decode_params(s->avctx,
2835 ret = ff_hevc_decode_nal_vps(gb, s->avctx, &s->ps);
2840 if (s->avctx->hwaccel && s->avctx->hwaccel->decode_params) {
2841 ret = s->avctx->hwaccel->decode_params(s->avctx,
2848 ret = ff_hevc_decode_nal_sps(gb, s->avctx, &s->ps,
2849 s->apply_defdispwin);
2854 if (s->avctx->hwaccel && s->avctx->hwaccel->decode_params) {
2855 ret = s->avctx->hwaccel->decode_params(s->avctx,
2862 ret = ff_hevc_decode_nal_pps(gb, s->avctx, &s->ps);
2866 case HEVC_NAL_SEI_PREFIX:
2867 case HEVC_NAL_SEI_SUFFIX:
2868 if (s->avctx->hwaccel && s->avctx->hwaccel->decode_params) {
2869 ret = s->avctx->hwaccel->decode_params(s->avctx,
2876 ret = ff_hevc_decode_nal_sei(gb, s->avctx, &s->sei, &s->ps, s->nal_unit_type);
2880 case HEVC_NAL_TRAIL_R:
2881 case HEVC_NAL_TRAIL_N:
2882 case HEVC_NAL_TSA_N:
2883 case HEVC_NAL_TSA_R:
2884 case HEVC_NAL_STSA_N:
2885 case HEVC_NAL_STSA_R:
2886 case HEVC_NAL_BLA_W_LP:
2887 case HEVC_NAL_BLA_W_RADL:
2888 case HEVC_NAL_BLA_N_LP:
2889 case HEVC_NAL_IDR_W_RADL:
2890 case HEVC_NAL_IDR_N_LP:
2891 case HEVC_NAL_CRA_NUT:
2892 case HEVC_NAL_RADL_N:
2893 case HEVC_NAL_RADL_R:
2894 case HEVC_NAL_RASL_N:
2895 case HEVC_NAL_RASL_R:
2896 ret = hls_slice_header(s);
2900 if (s->sh.first_slice_in_pic_flag) {
2901 if (s->max_ra == INT_MAX) {
2902 if (s->nal_unit_type == HEVC_NAL_CRA_NUT || IS_BLA(s)) {
2906 s->max_ra = INT_MIN;
2910 if ((s->nal_unit_type == HEVC_NAL_RASL_R || s->nal_unit_type == HEVC_NAL_RASL_N) &&
2911 s->poc <= s->max_ra) {
2915 if (s->nal_unit_type == HEVC_NAL_RASL_R && s->poc > s->max_ra)
2916 s->max_ra = INT_MIN;
2919 ret = hevc_frame_start(s);
2922 } else if (!s->ref) {
2923 av_log(s->avctx, AV_LOG_ERROR, "First slice in a frame missing.\n");
2927 if (s->nal_unit_type != s->first_nal_type) {
2928 av_log(s->avctx, AV_LOG_ERROR,
2929 "Non-matching NAL types of the VCL NALUs: %d %d\n",
2930 s->first_nal_type, s->nal_unit_type);
2931 return AVERROR_INVALIDDATA;
2934 if (!s->sh.dependent_slice_segment_flag &&
2935 s->sh.slice_type != HEVC_SLICE_I) {
2936 ret = ff_hevc_slice_rpl(s);
2938 av_log(s->avctx, AV_LOG_WARNING,
2939 "Error constructing the reference lists for the current slice.\n");
2944 if (s->sh.first_slice_in_pic_flag && s->avctx->hwaccel) {
2945 ret = s->avctx->hwaccel->start_frame(s->avctx, NULL, 0);
2950 if (s->avctx->hwaccel) {
2951 ret = s->avctx->hwaccel->decode_slice(s->avctx, nal->raw_data, nal->raw_size);
2955 if (s->threads_number > 1 && s->sh.num_entry_point_offsets > 0)
2956 ctb_addr_ts = hls_slice_data_wpp(s, nal);
2958 ctb_addr_ts = hls_slice_data(s);
2959 if (ctb_addr_ts >= (s->ps.sps->ctb_width * s->ps.sps->ctb_height)) {
2963 if (ctb_addr_ts < 0) {
2969 case HEVC_NAL_EOS_NUT:
2970 case HEVC_NAL_EOB_NUT:
2971 s->seq_decode = (s->seq_decode + 1) & 0xff;
2972 s->max_ra = INT_MAX;
2975 case HEVC_NAL_FD_NUT:
2978 av_log(s->avctx, AV_LOG_INFO,
2979 "Skipping NAL unit %d\n", s->nal_unit_type);
2984 if (s->avctx->err_recognition & AV_EF_EXPLODE)
2989 static int decode_nal_units(HEVCContext *s, const uint8_t *buf, int length)
2992 int eos_at_start = 1;
2995 s->last_eos = s->eos;
2998 /* split the input packet into NAL units, so we know the upper bound on the
2999 * number of slices in the frame */
3000 ret = ff_h2645_packet_split(&s->pkt, buf, length, s->avctx, s->is_nalff,
3001 s->nal_length_size, s->avctx->codec_id, 1);
3003 av_log(s->avctx, AV_LOG_ERROR,
3004 "Error splitting the input into NAL units.\n");
3008 for (i = 0; i < s->pkt.nb_nals; i++) {
3009 if (s->pkt.nals[i].type == HEVC_NAL_EOB_NUT ||
3010 s->pkt.nals[i].type == HEVC_NAL_EOS_NUT) {
3021 /* decode the NAL units */
3022 for (i = 0; i < s->pkt.nb_nals; i++) {
3023 ret = decode_nal_unit(s, &s->pkt.nals[i]);
3025 av_log(s->avctx, AV_LOG_WARNING,
3026 "Error parsing NAL unit #%d.\n", i);
3032 if (s->ref && s->threads_type == FF_THREAD_FRAME)
3033 ff_thread_report_progress(&s->ref->tf, INT_MAX, 0);
3038 static void print_md5(void *log_ctx, int level, uint8_t md5[16])
3041 for (i = 0; i < 16; i++)
3042 av_log(log_ctx, level, "%02"PRIx8, md5[i]);
3045 static int verify_md5(HEVCContext *s, AVFrame *frame)
3047 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(frame->format);
3052 return AVERROR(EINVAL);
3054 pixel_shift = desc->comp[0].depth > 8;
3056 av_log(s->avctx, AV_LOG_DEBUG, "Verifying checksum for frame with POC %d: ",
3059 /* the checksums are LE, so we have to byteswap for >8bpp formats
3062 if (pixel_shift && !s->checksum_buf) {
3063 av_fast_malloc(&s->checksum_buf, &s->checksum_buf_size,
3064 FFMAX3(frame->linesize[0], frame->linesize[1],
3065 frame->linesize[2]));
3066 if (!s->checksum_buf)
3067 return AVERROR(ENOMEM);
3071 for (i = 0; frame->data[i]; i++) {
3072 int width = s->avctx->coded_width;
3073 int height = s->avctx->coded_height;
3074 int w = (i == 1 || i == 2) ? (width >> desc->log2_chroma_w) : width;
3075 int h = (i == 1 || i == 2) ? (height >> desc->log2_chroma_h) : height;
3078 av_md5_init(s->md5_ctx);
3079 for (j = 0; j < h; j++) {
3080 const uint8_t *src = frame->data[i] + j * frame->linesize[i];
3083 s->bdsp.bswap16_buf((uint16_t *) s->checksum_buf,
3084 (const uint16_t *) src, w);
3085 src = s->checksum_buf;
3088 av_md5_update(s->md5_ctx, src, w << pixel_shift);
3090 av_md5_final(s->md5_ctx, md5);
3092 if (!memcmp(md5, s->sei.picture_hash.md5[i], 16)) {
3093 av_log (s->avctx, AV_LOG_DEBUG, "plane %d - correct ", i);
3094 print_md5(s->avctx, AV_LOG_DEBUG, md5);
3095 av_log (s->avctx, AV_LOG_DEBUG, "; ");
3097 av_log (s->avctx, AV_LOG_ERROR, "mismatching checksum of plane %d - ", i);
3098 print_md5(s->avctx, AV_LOG_ERROR, md5);
3099 av_log (s->avctx, AV_LOG_ERROR, " != ");
3100 print_md5(s->avctx, AV_LOG_ERROR, s->sei.picture_hash.md5[i]);
3101 av_log (s->avctx, AV_LOG_ERROR, "\n");
3102 return AVERROR_INVALIDDATA;
3106 av_log(s->avctx, AV_LOG_DEBUG, "\n");
3111 static int hevc_decode_extradata(HEVCContext *s, uint8_t *buf, int length, int first)
3115 ret = ff_hevc_decode_extradata(buf, length, &s->ps, &s->sei, &s->is_nalff,
3116 &s->nal_length_size, s->avctx->err_recognition,
3117 s->apply_defdispwin, s->avctx);
3121 /* export stream parameters from the first SPS */
3122 for (i = 0; i < FF_ARRAY_ELEMS(s->ps.sps_list); i++) {
3123 if (first && s->ps.sps_list[i]) {
3124 const HEVCSPS *sps = (const HEVCSPS*)s->ps.sps_list[i]->data;
3125 export_stream_params(s->avctx, &s->ps, sps);
3133 static int hevc_decode_frame(AVCodecContext *avctx, void *data, int *got_output,
3137 int new_extradata_size;
3138 uint8_t *new_extradata;
3139 HEVCContext *s = avctx->priv_data;
3142 ret = ff_hevc_output_frame(s, data, 1);
3150 new_extradata = av_packet_get_side_data(avpkt, AV_PKT_DATA_NEW_EXTRADATA,
3151 &new_extradata_size);
3152 if (new_extradata && new_extradata_size > 0) {
3153 ret = hevc_decode_extradata(s, new_extradata, new_extradata_size, 0);
3159 ret = decode_nal_units(s, avpkt->data, avpkt->size);
3163 if (avctx->hwaccel) {
3164 if (s->ref && (ret = avctx->hwaccel->end_frame(avctx)) < 0) {
3165 av_log(avctx, AV_LOG_ERROR,
3166 "hardware accelerator failed to decode picture\n");
3167 ff_hevc_unref_frame(s, s->ref, ~0);
3171 /* verify the SEI checksum */
3172 if (avctx->err_recognition & AV_EF_CRCCHECK && s->is_decoded &&
3173 s->sei.picture_hash.is_md5) {
3174 ret = verify_md5(s, s->ref->frame);
3175 if (ret < 0 && avctx->err_recognition & AV_EF_EXPLODE) {
3176 ff_hevc_unref_frame(s, s->ref, ~0);
3181 s->sei.picture_hash.is_md5 = 0;
3183 if (s->is_decoded) {
3184 av_log(avctx, AV_LOG_DEBUG, "Decoded frame with POC %d.\n", s->poc);
3188 if (s->output_frame->buf[0]) {
3189 av_frame_move_ref(data, s->output_frame);
3196 static int hevc_ref_frame(HEVCContext *s, HEVCFrame *dst, HEVCFrame *src)
3200 ret = ff_thread_ref_frame(&dst->tf, &src->tf);
3204 dst->tab_mvf_buf = av_buffer_ref(src->tab_mvf_buf);
3205 if (!dst->tab_mvf_buf)
3207 dst->tab_mvf = src->tab_mvf;
3209 dst->rpl_tab_buf = av_buffer_ref(src->rpl_tab_buf);
3210 if (!dst->rpl_tab_buf)
3212 dst->rpl_tab = src->rpl_tab;
3214 dst->rpl_buf = av_buffer_ref(src->rpl_buf);
3218 dst->poc = src->poc;
3219 dst->ctb_count = src->ctb_count;
3220 dst->flags = src->flags;
3221 dst->sequence = src->sequence;
3223 if (src->hwaccel_picture_private) {
3224 dst->hwaccel_priv_buf = av_buffer_ref(src->hwaccel_priv_buf);
3225 if (!dst->hwaccel_priv_buf)
3227 dst->hwaccel_picture_private = dst->hwaccel_priv_buf->data;
3232 ff_hevc_unref_frame(s, dst, ~0);
3233 return AVERROR(ENOMEM);
3236 static av_cold int hevc_decode_free(AVCodecContext *avctx)
3238 HEVCContext *s = avctx->priv_data;
3243 av_freep(&s->md5_ctx);
3245 av_freep(&s->cabac_state);
3247 for (i = 0; i < 3; i++) {
3248 av_freep(&s->sao_pixel_buffer_h[i]);
3249 av_freep(&s->sao_pixel_buffer_v[i]);
3251 av_frame_free(&s->output_frame);
3253 for (i = 0; i < FF_ARRAY_ELEMS(s->DPB); i++) {
3254 ff_hevc_unref_frame(s, &s->DPB[i], ~0);
3255 av_frame_free(&s->DPB[i].frame);
3258 for (i = 0; i < FF_ARRAY_ELEMS(s->ps.vps_list); i++)
3259 av_buffer_unref(&s->ps.vps_list[i]);
3260 for (i = 0; i < FF_ARRAY_ELEMS(s->ps.sps_list); i++)
3261 av_buffer_unref(&s->ps.sps_list[i]);
3262 for (i = 0; i < FF_ARRAY_ELEMS(s->ps.pps_list); i++)
3263 av_buffer_unref(&s->ps.pps_list[i]);
3268 av_freep(&s->sh.entry_point_offset);
3269 av_freep(&s->sh.offset);
3270 av_freep(&s->sh.size);
3272 for (i = 1; i < s->threads_number; i++) {
3273 HEVCLocalContext *lc = s->HEVClcList[i];
3275 av_freep(&s->HEVClcList[i]);
3276 av_freep(&s->sList[i]);
3279 if (s->HEVClc == s->HEVClcList[0])
3281 av_freep(&s->HEVClcList[0]);
3283 ff_h2645_packet_uninit(&s->pkt);
3288 static av_cold int hevc_init_context(AVCodecContext *avctx)
3290 HEVCContext *s = avctx->priv_data;
3295 s->HEVClc = av_mallocz(sizeof(HEVCLocalContext));
3298 s->HEVClcList[0] = s->HEVClc;
3301 s->cabac_state = av_malloc(HEVC_CONTEXTS);
3302 if (!s->cabac_state)
3305 s->output_frame = av_frame_alloc();
3306 if (!s->output_frame)
3309 for (i = 0; i < FF_ARRAY_ELEMS(s->DPB); i++) {
3310 s->DPB[i].frame = av_frame_alloc();
3311 if (!s->DPB[i].frame)
3313 s->DPB[i].tf.f = s->DPB[i].frame;
3316 s->max_ra = INT_MAX;
3318 s->md5_ctx = av_md5_alloc();
3322 ff_bswapdsp_init(&s->bdsp);
3324 s->context_initialized = 1;
3327 ff_hevc_reset_sei(&s->sei);
3332 hevc_decode_free(avctx);
3333 return AVERROR(ENOMEM);
3336 static int hevc_update_thread_context(AVCodecContext *dst,
3337 const AVCodecContext *src)
3339 HEVCContext *s = dst->priv_data;
3340 HEVCContext *s0 = src->priv_data;
3343 if (!s->context_initialized) {
3344 ret = hevc_init_context(dst);
3349 for (i = 0; i < FF_ARRAY_ELEMS(s->DPB); i++) {
3350 ff_hevc_unref_frame(s, &s->DPB[i], ~0);
3351 if (s0->DPB[i].frame->buf[0]) {
3352 ret = hevc_ref_frame(s, &s->DPB[i], &s0->DPB[i]);
3358 if (s->ps.sps != s0->ps.sps)
3360 for (i = 0; i < FF_ARRAY_ELEMS(s->ps.vps_list); i++) {
3361 av_buffer_unref(&s->ps.vps_list[i]);
3362 if (s0->ps.vps_list[i]) {
3363 s->ps.vps_list[i] = av_buffer_ref(s0->ps.vps_list[i]);
3364 if (!s->ps.vps_list[i])
3365 return AVERROR(ENOMEM);
3369 for (i = 0; i < FF_ARRAY_ELEMS(s->ps.sps_list); i++) {
3370 av_buffer_unref(&s->ps.sps_list[i]);
3371 if (s0->ps.sps_list[i]) {
3372 s->ps.sps_list[i] = av_buffer_ref(s0->ps.sps_list[i]);
3373 if (!s->ps.sps_list[i])
3374 return AVERROR(ENOMEM);
3378 for (i = 0; i < FF_ARRAY_ELEMS(s->ps.pps_list); i++) {
3379 av_buffer_unref(&s->ps.pps_list[i]);
3380 if (s0->ps.pps_list[i]) {
3381 s->ps.pps_list[i] = av_buffer_ref(s0->ps.pps_list[i]);
3382 if (!s->ps.pps_list[i])
3383 return AVERROR(ENOMEM);
3387 if (s->ps.sps != s0->ps.sps)
3388 if ((ret = set_sps(s, s0->ps.sps, src->pix_fmt)) < 0)
3391 s->seq_decode = s0->seq_decode;
3392 s->seq_output = s0->seq_output;
3393 s->pocTid0 = s0->pocTid0;
3394 s->max_ra = s0->max_ra;
3396 s->no_rasl_output_flag = s0->no_rasl_output_flag;
3398 s->is_nalff = s0->is_nalff;
3399 s->nal_length_size = s0->nal_length_size;
3401 s->threads_number = s0->threads_number;
3402 s->threads_type = s0->threads_type;
3405 s->seq_decode = (s->seq_decode + 1) & 0xff;
3406 s->max_ra = INT_MAX;
3409 s->sei.frame_packing = s0->sei.frame_packing;
3410 s->sei.display_orientation = s0->sei.display_orientation;
3411 s->sei.mastering_display = s0->sei.mastering_display;
3412 s->sei.content_light = s0->sei.content_light;
3413 s->sei.alternative_transfer = s0->sei.alternative_transfer;
3418 static av_cold int hevc_decode_init(AVCodecContext *avctx)
3420 HEVCContext *s = avctx->priv_data;
3423 avctx->internal->allocate_progress = 1;
3425 ret = hevc_init_context(avctx);
3429 s->enable_parallel_tiles = 0;
3430 s->sei.picture_timing.picture_struct = 0;
3433 atomic_init(&s->wpp_err, 0);
3435 if(avctx->active_thread_type & FF_THREAD_SLICE)
3436 s->threads_number = avctx->thread_count;
3438 s->threads_number = 1;
3440 if (avctx->extradata_size > 0 && avctx->extradata) {
3441 ret = hevc_decode_extradata(s, avctx->extradata, avctx->extradata_size, 1);
3443 hevc_decode_free(avctx);
3448 if((avctx->active_thread_type & FF_THREAD_FRAME) && avctx->thread_count > 1)
3449 s->threads_type = FF_THREAD_FRAME;
3451 s->threads_type = FF_THREAD_SLICE;
3456 static av_cold int hevc_init_thread_copy(AVCodecContext *avctx)
3458 HEVCContext *s = avctx->priv_data;
3461 memset(s, 0, sizeof(*s));
3463 ret = hevc_init_context(avctx);
3470 static void hevc_decode_flush(AVCodecContext *avctx)
3472 HEVCContext *s = avctx->priv_data;
3473 ff_hevc_flush_dpb(s);
3474 s->max_ra = INT_MAX;
3478 #define OFFSET(x) offsetof(HEVCContext, x)
3479 #define PAR (AV_OPT_FLAG_DECODING_PARAM | AV_OPT_FLAG_VIDEO_PARAM)
3481 static const AVOption options[] = {
3482 { "apply_defdispwin", "Apply default display window from VUI", OFFSET(apply_defdispwin),
3483 AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, PAR },
3484 { "strict-displaywin", "stricly apply default display window size", OFFSET(apply_defdispwin),
3485 AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, PAR },
3489 static const AVClass hevc_decoder_class = {
3490 .class_name = "HEVC decoder",
3491 .item_name = av_default_item_name,
3493 .version = LIBAVUTIL_VERSION_INT,
3496 AVCodec ff_hevc_decoder = {
3498 .long_name = NULL_IF_CONFIG_SMALL("HEVC (High Efficiency Video Coding)"),
3499 .type = AVMEDIA_TYPE_VIDEO,
3500 .id = AV_CODEC_ID_HEVC,
3501 .priv_data_size = sizeof(HEVCContext),
3502 .priv_class = &hevc_decoder_class,
3503 .init = hevc_decode_init,
3504 .close = hevc_decode_free,
3505 .decode = hevc_decode_frame,
3506 .flush = hevc_decode_flush,
3507 .update_thread_context = hevc_update_thread_context,
3508 .init_thread_copy = hevc_init_thread_copy,
3509 .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DELAY |
3510 AV_CODEC_CAP_SLICE_THREADS | AV_CODEC_CAP_FRAME_THREADS,
3511 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE | FF_CODEC_CAP_EXPORTS_CROPPING,
3512 .profiles = NULL_IF_CONFIG_SMALL(ff_hevc_profiles),