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"
47 const uint8_t ff_hevc_pel_weight[65] = { [2] = 0, [4] = 1, [6] = 2, [8] = 3, [12] = 4, [16] = 5, [24] = 6, [32] = 7, [48] = 8, [64] = 9 };
50 * NOTE: Each function hls_foo correspond to the function foo in the
51 * specification (HLS stands for High Level Syntax).
58 /* free everything allocated by pic_arrays_init() */
59 static void pic_arrays_free(HEVCContext *s)
62 av_freep(&s->deblock);
64 av_freep(&s->skip_flag);
65 av_freep(&s->tab_ct_depth);
67 av_freep(&s->tab_ipm);
68 av_freep(&s->cbf_luma);
71 av_freep(&s->qp_y_tab);
72 av_freep(&s->tab_slice_address);
73 av_freep(&s->filter_slice_edges);
75 av_freep(&s->horizontal_bs);
76 av_freep(&s->vertical_bs);
78 av_freep(&s->sh.entry_point_offset);
79 av_freep(&s->sh.size);
80 av_freep(&s->sh.offset);
82 av_buffer_pool_uninit(&s->tab_mvf_pool);
83 av_buffer_pool_uninit(&s->rpl_tab_pool);
86 /* allocate arrays that depend on frame dimensions */
87 static int pic_arrays_init(HEVCContext *s, const HEVCSPS *sps)
89 int log2_min_cb_size = sps->log2_min_cb_size;
90 int width = sps->width;
91 int height = sps->height;
92 int pic_size_in_ctb = ((width >> log2_min_cb_size) + 1) *
93 ((height >> log2_min_cb_size) + 1);
94 int ctb_count = sps->ctb_width * sps->ctb_height;
95 int min_pu_size = sps->min_pu_width * sps->min_pu_height;
97 s->bs_width = (width >> 2) + 1;
98 s->bs_height = (height >> 2) + 1;
100 s->sao = av_mallocz_array(ctb_count, sizeof(*s->sao));
101 s->deblock = av_mallocz_array(ctb_count, sizeof(*s->deblock));
102 if (!s->sao || !s->deblock)
105 s->skip_flag = av_malloc_array(sps->min_cb_height, sps->min_cb_width);
106 s->tab_ct_depth = av_malloc_array(sps->min_cb_height, sps->min_cb_width);
107 if (!s->skip_flag || !s->tab_ct_depth)
110 s->cbf_luma = av_malloc_array(sps->min_tb_width, sps->min_tb_height);
111 s->tab_ipm = av_mallocz(min_pu_size);
112 s->is_pcm = av_malloc_array(sps->min_pu_width + 1, sps->min_pu_height + 1);
113 if (!s->tab_ipm || !s->cbf_luma || !s->is_pcm)
116 s->filter_slice_edges = av_mallocz(ctb_count);
117 s->tab_slice_address = av_malloc_array(pic_size_in_ctb,
118 sizeof(*s->tab_slice_address));
119 s->qp_y_tab = av_malloc_array(pic_size_in_ctb,
120 sizeof(*s->qp_y_tab));
121 if (!s->qp_y_tab || !s->filter_slice_edges || !s->tab_slice_address)
124 s->horizontal_bs = av_mallocz_array(s->bs_width, s->bs_height);
125 s->vertical_bs = av_mallocz_array(s->bs_width, s->bs_height);
126 if (!s->horizontal_bs || !s->vertical_bs)
129 s->tab_mvf_pool = av_buffer_pool_init(min_pu_size * sizeof(MvField),
131 s->rpl_tab_pool = av_buffer_pool_init(ctb_count * sizeof(RefPicListTab),
133 if (!s->tab_mvf_pool || !s->rpl_tab_pool)
140 return AVERROR(ENOMEM);
143 static int pred_weight_table(HEVCContext *s, GetBitContext *gb)
147 uint8_t luma_weight_l0_flag[16];
148 uint8_t chroma_weight_l0_flag[16];
149 uint8_t luma_weight_l1_flag[16];
150 uint8_t chroma_weight_l1_flag[16];
151 int luma_log2_weight_denom;
153 luma_log2_weight_denom = get_ue_golomb_long(gb);
154 if (luma_log2_weight_denom < 0 || luma_log2_weight_denom > 7) {
155 av_log(s->avctx, AV_LOG_ERROR, "luma_log2_weight_denom %d is invalid\n", luma_log2_weight_denom);
156 return AVERROR_INVALIDDATA;
158 s->sh.luma_log2_weight_denom = av_clip_uintp2(luma_log2_weight_denom, 3);
159 if (s->ps.sps->chroma_format_idc != 0) {
160 int64_t chroma_log2_weight_denom = luma_log2_weight_denom + (int64_t)get_se_golomb(gb);
161 if (chroma_log2_weight_denom < 0 || chroma_log2_weight_denom > 7) {
162 av_log(s->avctx, AV_LOG_ERROR, "chroma_log2_weight_denom %"PRId64" is invalid\n", chroma_log2_weight_denom);
163 return AVERROR_INVALIDDATA;
165 s->sh.chroma_log2_weight_denom = chroma_log2_weight_denom;
168 for (i = 0; i < s->sh.nb_refs[L0]; i++) {
169 luma_weight_l0_flag[i] = get_bits1(gb);
170 if (!luma_weight_l0_flag[i]) {
171 s->sh.luma_weight_l0[i] = 1 << s->sh.luma_log2_weight_denom;
172 s->sh.luma_offset_l0[i] = 0;
175 if (s->ps.sps->chroma_format_idc != 0) {
176 for (i = 0; i < s->sh.nb_refs[L0]; i++)
177 chroma_weight_l0_flag[i] = get_bits1(gb);
179 for (i = 0; i < s->sh.nb_refs[L0]; i++)
180 chroma_weight_l0_flag[i] = 0;
182 for (i = 0; i < s->sh.nb_refs[L0]; i++) {
183 if (luma_weight_l0_flag[i]) {
184 int delta_luma_weight_l0 = get_se_golomb(gb);
185 if ((int8_t)delta_luma_weight_l0 != delta_luma_weight_l0)
186 return AVERROR_INVALIDDATA;
187 s->sh.luma_weight_l0[i] = (1 << s->sh.luma_log2_weight_denom) + delta_luma_weight_l0;
188 s->sh.luma_offset_l0[i] = get_se_golomb(gb);
190 if (chroma_weight_l0_flag[i]) {
191 for (j = 0; j < 2; j++) {
192 int delta_chroma_weight_l0 = get_se_golomb(gb);
193 int delta_chroma_offset_l0 = get_se_golomb(gb);
195 if ( (int8_t)delta_chroma_weight_l0 != delta_chroma_weight_l0
196 || delta_chroma_offset_l0 < -(1<<17) || delta_chroma_offset_l0 > (1<<17)) {
197 return AVERROR_INVALIDDATA;
200 s->sh.chroma_weight_l0[i][j] = (1 << s->sh.chroma_log2_weight_denom) + delta_chroma_weight_l0;
201 s->sh.chroma_offset_l0[i][j] = av_clip((delta_chroma_offset_l0 - ((128 * s->sh.chroma_weight_l0[i][j])
202 >> s->sh.chroma_log2_weight_denom) + 128), -128, 127);
205 s->sh.chroma_weight_l0[i][0] = 1 << s->sh.chroma_log2_weight_denom;
206 s->sh.chroma_offset_l0[i][0] = 0;
207 s->sh.chroma_weight_l0[i][1] = 1 << s->sh.chroma_log2_weight_denom;
208 s->sh.chroma_offset_l0[i][1] = 0;
211 if (s->sh.slice_type == HEVC_SLICE_B) {
212 for (i = 0; i < s->sh.nb_refs[L1]; i++) {
213 luma_weight_l1_flag[i] = get_bits1(gb);
214 if (!luma_weight_l1_flag[i]) {
215 s->sh.luma_weight_l1[i] = 1 << s->sh.luma_log2_weight_denom;
216 s->sh.luma_offset_l1[i] = 0;
219 if (s->ps.sps->chroma_format_idc != 0) {
220 for (i = 0; i < s->sh.nb_refs[L1]; i++)
221 chroma_weight_l1_flag[i] = get_bits1(gb);
223 for (i = 0; i < s->sh.nb_refs[L1]; i++)
224 chroma_weight_l1_flag[i] = 0;
226 for (i = 0; i < s->sh.nb_refs[L1]; i++) {
227 if (luma_weight_l1_flag[i]) {
228 int delta_luma_weight_l1 = get_se_golomb(gb);
229 if ((int8_t)delta_luma_weight_l1 != delta_luma_weight_l1)
230 return AVERROR_INVALIDDATA;
231 s->sh.luma_weight_l1[i] = (1 << s->sh.luma_log2_weight_denom) + delta_luma_weight_l1;
232 s->sh.luma_offset_l1[i] = get_se_golomb(gb);
234 if (chroma_weight_l1_flag[i]) {
235 for (j = 0; j < 2; j++) {
236 int delta_chroma_weight_l1 = get_se_golomb(gb);
237 int delta_chroma_offset_l1 = get_se_golomb(gb);
239 if ( (int8_t)delta_chroma_weight_l1 != delta_chroma_weight_l1
240 || delta_chroma_offset_l1 < -(1<<17) || delta_chroma_offset_l1 > (1<<17)) {
241 return AVERROR_INVALIDDATA;
244 s->sh.chroma_weight_l1[i][j] = (1 << s->sh.chroma_log2_weight_denom) + delta_chroma_weight_l1;
245 s->sh.chroma_offset_l1[i][j] = av_clip((delta_chroma_offset_l1 - ((128 * s->sh.chroma_weight_l1[i][j])
246 >> s->sh.chroma_log2_weight_denom) + 128), -128, 127);
249 s->sh.chroma_weight_l1[i][0] = 1 << s->sh.chroma_log2_weight_denom;
250 s->sh.chroma_offset_l1[i][0] = 0;
251 s->sh.chroma_weight_l1[i][1] = 1 << s->sh.chroma_log2_weight_denom;
252 s->sh.chroma_offset_l1[i][1] = 0;
259 static int decode_lt_rps(HEVCContext *s, LongTermRPS *rps, GetBitContext *gb)
261 const HEVCSPS *sps = s->ps.sps;
262 int max_poc_lsb = 1 << sps->log2_max_poc_lsb;
263 int prev_delta_msb = 0;
264 unsigned int nb_sps = 0, nb_sh;
268 if (!sps->long_term_ref_pics_present_flag)
271 if (sps->num_long_term_ref_pics_sps > 0)
272 nb_sps = get_ue_golomb_long(gb);
273 nb_sh = get_ue_golomb_long(gb);
275 if (nb_sps > sps->num_long_term_ref_pics_sps)
276 return AVERROR_INVALIDDATA;
277 if (nb_sh + (uint64_t)nb_sps > FF_ARRAY_ELEMS(rps->poc))
278 return AVERROR_INVALIDDATA;
280 rps->nb_refs = nb_sh + nb_sps;
282 for (i = 0; i < rps->nb_refs; i++) {
283 uint8_t delta_poc_msb_present;
286 uint8_t lt_idx_sps = 0;
288 if (sps->num_long_term_ref_pics_sps > 1)
289 lt_idx_sps = get_bits(gb, av_ceil_log2(sps->num_long_term_ref_pics_sps));
291 rps->poc[i] = sps->lt_ref_pic_poc_lsb_sps[lt_idx_sps];
292 rps->used[i] = sps->used_by_curr_pic_lt_sps_flag[lt_idx_sps];
294 rps->poc[i] = get_bits(gb, sps->log2_max_poc_lsb);
295 rps->used[i] = get_bits1(gb);
298 delta_poc_msb_present = get_bits1(gb);
299 if (delta_poc_msb_present) {
300 int64_t delta = get_ue_golomb_long(gb);
303 if (i && i != nb_sps)
304 delta += prev_delta_msb;
306 poc = rps->poc[i] + s->poc - delta * max_poc_lsb - s->sh.pic_order_cnt_lsb;
307 if (poc != (int32_t)poc)
308 return AVERROR_INVALIDDATA;
310 prev_delta_msb = delta;
317 static void export_stream_params(HEVCContext *s, const HEVCSPS *sps)
319 AVCodecContext *avctx = s->avctx;
320 const HEVCParamSets *ps = &s->ps;
321 const HEVCVPS *vps = (const HEVCVPS*)ps->vps_list[sps->vps_id]->data;
322 const HEVCWindow *ow = &sps->output_window;
323 unsigned int num = 0, den = 0;
325 avctx->pix_fmt = sps->pix_fmt;
326 avctx->coded_width = sps->width;
327 avctx->coded_height = sps->height;
328 avctx->width = sps->width - ow->left_offset - ow->right_offset;
329 avctx->height = sps->height - ow->top_offset - ow->bottom_offset;
330 avctx->has_b_frames = sps->temporal_layer[sps->max_sub_layers - 1].num_reorder_pics;
331 avctx->profile = sps->ptl.general_ptl.profile_idc;
332 avctx->level = sps->ptl.general_ptl.level_idc;
334 ff_set_sar(avctx, sps->vui.sar);
336 if (sps->vui.video_signal_type_present_flag)
337 avctx->color_range = sps->vui.video_full_range_flag ? AVCOL_RANGE_JPEG
340 avctx->color_range = AVCOL_RANGE_MPEG;
342 if (sps->vui.colour_description_present_flag) {
343 avctx->color_primaries = sps->vui.colour_primaries;
344 avctx->color_trc = sps->vui.transfer_characteristic;
345 avctx->colorspace = sps->vui.matrix_coeffs;
347 avctx->color_primaries = AVCOL_PRI_UNSPECIFIED;
348 avctx->color_trc = AVCOL_TRC_UNSPECIFIED;
349 avctx->colorspace = AVCOL_SPC_UNSPECIFIED;
352 if (vps->vps_timing_info_present_flag) {
353 num = vps->vps_num_units_in_tick;
354 den = vps->vps_time_scale;
355 } else if (sps->vui.vui_timing_info_present_flag) {
356 num = sps->vui.vui_num_units_in_tick;
357 den = sps->vui.vui_time_scale;
360 if (num != 0 && den != 0)
361 av_reduce(&avctx->framerate.den, &avctx->framerate.num,
364 if (s->sei.alternative_transfer.present &&
365 av_color_transfer_name(s->sei.alternative_transfer.preferred_transfer_characteristics) &&
366 s->sei.alternative_transfer.preferred_transfer_characteristics != AVCOL_TRC_UNSPECIFIED) {
367 avctx->color_trc = s->sei.alternative_transfer.preferred_transfer_characteristics;
371 static enum AVPixelFormat get_format(HEVCContext *s, const HEVCSPS *sps)
373 #define HWACCEL_MAX (CONFIG_HEVC_DXVA2_HWACCEL + \
374 CONFIG_HEVC_D3D11VA_HWACCEL * 2 + \
375 CONFIG_HEVC_NVDEC_HWACCEL + \
376 CONFIG_HEVC_VAAPI_HWACCEL + \
377 CONFIG_HEVC_VIDEOTOOLBOX_HWACCEL + \
378 CONFIG_HEVC_VDPAU_HWACCEL)
379 enum AVPixelFormat pix_fmts[HWACCEL_MAX + 2], *fmt = pix_fmts;
381 switch (sps->pix_fmt) {
382 case AV_PIX_FMT_YUV420P:
383 case AV_PIX_FMT_YUVJ420P:
384 #if CONFIG_HEVC_DXVA2_HWACCEL
385 *fmt++ = AV_PIX_FMT_DXVA2_VLD;
387 #if CONFIG_HEVC_D3D11VA_HWACCEL
388 *fmt++ = AV_PIX_FMT_D3D11VA_VLD;
389 *fmt++ = AV_PIX_FMT_D3D11;
391 #if CONFIG_HEVC_VAAPI_HWACCEL
392 *fmt++ = AV_PIX_FMT_VAAPI;
394 #if CONFIG_HEVC_VDPAU_HWACCEL
395 *fmt++ = AV_PIX_FMT_VDPAU;
397 #if CONFIG_HEVC_NVDEC_HWACCEL
398 *fmt++ = AV_PIX_FMT_CUDA;
400 #if CONFIG_HEVC_VIDEOTOOLBOX_HWACCEL
401 *fmt++ = AV_PIX_FMT_VIDEOTOOLBOX;
404 case AV_PIX_FMT_YUV420P10:
405 #if CONFIG_HEVC_DXVA2_HWACCEL
406 *fmt++ = AV_PIX_FMT_DXVA2_VLD;
408 #if CONFIG_HEVC_D3D11VA_HWACCEL
409 *fmt++ = AV_PIX_FMT_D3D11VA_VLD;
410 *fmt++ = AV_PIX_FMT_D3D11;
412 #if CONFIG_HEVC_VAAPI_HWACCEL
413 *fmt++ = AV_PIX_FMT_VAAPI;
415 #if CONFIG_HEVC_VIDEOTOOLBOX_HWACCEL
416 *fmt++ = AV_PIX_FMT_VIDEOTOOLBOX;
418 #if CONFIG_HEVC_NVDEC_HWACCEL
419 *fmt++ = AV_PIX_FMT_CUDA;
422 case AV_PIX_FMT_YUV444P:
423 #if CONFIG_HEVC_VDPAU_HWACCEL
424 *fmt++ = AV_PIX_FMT_VDPAU;
426 #if CONFIG_HEVC_NVDEC_HWACCEL
427 *fmt++ = AV_PIX_FMT_CUDA;
430 case AV_PIX_FMT_YUV420P12:
431 case AV_PIX_FMT_YUV444P10:
432 case AV_PIX_FMT_YUV444P12:
433 #if CONFIG_HEVC_NVDEC_HWACCEL
434 *fmt++ = AV_PIX_FMT_CUDA;
439 *fmt++ = sps->pix_fmt;
440 *fmt = AV_PIX_FMT_NONE;
442 return ff_thread_get_format(s->avctx, pix_fmts);
445 static int set_sps(HEVCContext *s, const HEVCSPS *sps,
446 enum AVPixelFormat pix_fmt)
457 ret = pic_arrays_init(s, sps);
461 export_stream_params(s, sps);
463 s->avctx->pix_fmt = pix_fmt;
465 ff_hevc_pred_init(&s->hpc, sps->bit_depth);
466 ff_hevc_dsp_init (&s->hevcdsp, sps->bit_depth);
467 ff_videodsp_init (&s->vdsp, sps->bit_depth);
469 for (i = 0; i < 3; i++) {
470 av_freep(&s->sao_pixel_buffer_h[i]);
471 av_freep(&s->sao_pixel_buffer_v[i]);
474 if (sps->sao_enabled && !s->avctx->hwaccel) {
475 int c_count = (sps->chroma_format_idc != 0) ? 3 : 1;
478 for(c_idx = 0; c_idx < c_count; c_idx++) {
479 int w = sps->width >> sps->hshift[c_idx];
480 int h = sps->height >> sps->vshift[c_idx];
481 s->sao_pixel_buffer_h[c_idx] =
482 av_malloc((w * 2 * sps->ctb_height) <<
484 s->sao_pixel_buffer_v[c_idx] =
485 av_malloc((h * 2 * sps->ctb_width) <<
491 s->ps.vps = (HEVCVPS*) s->ps.vps_list[s->ps.sps->vps_id]->data;
501 static int hls_slice_header(HEVCContext *s)
503 GetBitContext *gb = &s->HEVClc->gb;
504 SliceHeader *sh = &s->sh;
508 sh->first_slice_in_pic_flag = get_bits1(gb);
509 if (s->ref && sh->first_slice_in_pic_flag) {
510 av_log(s->avctx, AV_LOG_ERROR, "Two slices reporting being the first in the same frame.\n");
511 return 1; // This slice will be skiped later, do not corrupt state
514 if ((IS_IDR(s) || IS_BLA(s)) && sh->first_slice_in_pic_flag) {
515 s->seq_decode = (s->seq_decode + 1) & 0xff;
518 ff_hevc_clear_refs(s);
520 sh->no_output_of_prior_pics_flag = 0;
522 sh->no_output_of_prior_pics_flag = get_bits1(gb);
524 sh->pps_id = get_ue_golomb_long(gb);
525 if (sh->pps_id >= HEVC_MAX_PPS_COUNT || !s->ps.pps_list[sh->pps_id]) {
526 av_log(s->avctx, AV_LOG_ERROR, "PPS id out of range: %d\n", sh->pps_id);
527 return AVERROR_INVALIDDATA;
529 if (!sh->first_slice_in_pic_flag &&
530 s->ps.pps != (HEVCPPS*)s->ps.pps_list[sh->pps_id]->data) {
531 av_log(s->avctx, AV_LOG_ERROR, "PPS changed between slices.\n");
532 return AVERROR_INVALIDDATA;
534 s->ps.pps = (HEVCPPS*)s->ps.pps_list[sh->pps_id]->data;
535 if (s->nal_unit_type == HEVC_NAL_CRA_NUT && s->last_eos == 1)
536 sh->no_output_of_prior_pics_flag = 1;
538 if (s->ps.sps != (HEVCSPS*)s->ps.sps_list[s->ps.pps->sps_id]->data) {
539 const HEVCSPS *sps = (HEVCSPS*)s->ps.sps_list[s->ps.pps->sps_id]->data;
540 const HEVCSPS *last_sps = s->ps.sps;
541 enum AVPixelFormat pix_fmt;
543 if (last_sps && IS_IRAP(s) && s->nal_unit_type != HEVC_NAL_CRA_NUT) {
544 if (sps->width != last_sps->width || sps->height != last_sps->height ||
545 sps->temporal_layer[sps->max_sub_layers - 1].max_dec_pic_buffering !=
546 last_sps->temporal_layer[last_sps->max_sub_layers - 1].max_dec_pic_buffering)
547 sh->no_output_of_prior_pics_flag = 0;
549 ff_hevc_clear_refs(s);
551 ret = set_sps(s, sps, sps->pix_fmt);
555 pix_fmt = get_format(s, sps);
558 s->avctx->pix_fmt = pix_fmt;
560 s->seq_decode = (s->seq_decode + 1) & 0xff;
564 sh->dependent_slice_segment_flag = 0;
565 if (!sh->first_slice_in_pic_flag) {
566 int slice_address_length;
568 if (s->ps.pps->dependent_slice_segments_enabled_flag)
569 sh->dependent_slice_segment_flag = get_bits1(gb);
571 slice_address_length = av_ceil_log2(s->ps.sps->ctb_width *
572 s->ps.sps->ctb_height);
573 sh->slice_segment_addr = get_bitsz(gb, slice_address_length);
574 if (sh->slice_segment_addr >= s->ps.sps->ctb_width * s->ps.sps->ctb_height) {
575 av_log(s->avctx, AV_LOG_ERROR,
576 "Invalid slice segment address: %u.\n",
577 sh->slice_segment_addr);
578 return AVERROR_INVALIDDATA;
581 if (!sh->dependent_slice_segment_flag) {
582 sh->slice_addr = sh->slice_segment_addr;
586 sh->slice_segment_addr = sh->slice_addr = 0;
588 s->slice_initialized = 0;
591 if (!sh->dependent_slice_segment_flag) {
592 s->slice_initialized = 0;
594 for (i = 0; i < s->ps.pps->num_extra_slice_header_bits; i++)
595 skip_bits(gb, 1); // slice_reserved_undetermined_flag[]
597 sh->slice_type = get_ue_golomb_long(gb);
598 if (!(sh->slice_type == HEVC_SLICE_I ||
599 sh->slice_type == HEVC_SLICE_P ||
600 sh->slice_type == HEVC_SLICE_B)) {
601 av_log(s->avctx, AV_LOG_ERROR, "Unknown slice type: %d.\n",
603 return AVERROR_INVALIDDATA;
605 if (IS_IRAP(s) && sh->slice_type != HEVC_SLICE_I) {
606 av_log(s->avctx, AV_LOG_ERROR, "Inter slices in an IRAP frame.\n");
607 return AVERROR_INVALIDDATA;
610 // when flag is not present, picture is inferred to be output
611 sh->pic_output_flag = 1;
612 if (s->ps.pps->output_flag_present_flag)
613 sh->pic_output_flag = get_bits1(gb);
615 if (s->ps.sps->separate_colour_plane_flag)
616 sh->colour_plane_id = get_bits(gb, 2);
621 sh->pic_order_cnt_lsb = get_bits(gb, s->ps.sps->log2_max_poc_lsb);
622 poc = ff_hevc_compute_poc(s->ps.sps, s->pocTid0, sh->pic_order_cnt_lsb, s->nal_unit_type);
623 if (!sh->first_slice_in_pic_flag && poc != s->poc) {
624 av_log(s->avctx, AV_LOG_WARNING,
625 "Ignoring POC change between slices: %d -> %d\n", s->poc, poc);
626 if (s->avctx->err_recognition & AV_EF_EXPLODE)
627 return AVERROR_INVALIDDATA;
632 sh->short_term_ref_pic_set_sps_flag = get_bits1(gb);
633 pos = get_bits_left(gb);
634 if (!sh->short_term_ref_pic_set_sps_flag) {
635 ret = ff_hevc_decode_short_term_rps(gb, s->avctx, &sh->slice_rps, s->ps.sps, 1);
639 sh->short_term_rps = &sh->slice_rps;
641 int numbits, rps_idx;
643 if (!s->ps.sps->nb_st_rps) {
644 av_log(s->avctx, AV_LOG_ERROR, "No ref lists in the SPS.\n");
645 return AVERROR_INVALIDDATA;
648 numbits = av_ceil_log2(s->ps.sps->nb_st_rps);
649 rps_idx = numbits > 0 ? get_bits(gb, numbits) : 0;
650 sh->short_term_rps = &s->ps.sps->st_rps[rps_idx];
652 sh->short_term_ref_pic_set_size = pos - get_bits_left(gb);
654 pos = get_bits_left(gb);
655 ret = decode_lt_rps(s, &sh->long_term_rps, gb);
657 av_log(s->avctx, AV_LOG_WARNING, "Invalid long term RPS.\n");
658 if (s->avctx->err_recognition & AV_EF_EXPLODE)
659 return AVERROR_INVALIDDATA;
661 sh->long_term_ref_pic_set_size = pos - get_bits_left(gb);
663 if (s->ps.sps->sps_temporal_mvp_enabled_flag)
664 sh->slice_temporal_mvp_enabled_flag = get_bits1(gb);
666 sh->slice_temporal_mvp_enabled_flag = 0;
668 s->sh.short_term_rps = NULL;
673 if (sh->first_slice_in_pic_flag && s->temporal_id == 0 &&
674 s->nal_unit_type != HEVC_NAL_TRAIL_N &&
675 s->nal_unit_type != HEVC_NAL_TSA_N &&
676 s->nal_unit_type != HEVC_NAL_STSA_N &&
677 s->nal_unit_type != HEVC_NAL_RADL_N &&
678 s->nal_unit_type != HEVC_NAL_RADL_R &&
679 s->nal_unit_type != HEVC_NAL_RASL_N &&
680 s->nal_unit_type != HEVC_NAL_RASL_R)
683 if (s->ps.sps->sao_enabled) {
684 sh->slice_sample_adaptive_offset_flag[0] = get_bits1(gb);
685 if (s->ps.sps->chroma_format_idc) {
686 sh->slice_sample_adaptive_offset_flag[1] =
687 sh->slice_sample_adaptive_offset_flag[2] = get_bits1(gb);
690 sh->slice_sample_adaptive_offset_flag[0] = 0;
691 sh->slice_sample_adaptive_offset_flag[1] = 0;
692 sh->slice_sample_adaptive_offset_flag[2] = 0;
695 sh->nb_refs[L0] = sh->nb_refs[L1] = 0;
696 if (sh->slice_type == HEVC_SLICE_P || sh->slice_type == HEVC_SLICE_B) {
699 sh->nb_refs[L0] = s->ps.pps->num_ref_idx_l0_default_active;
700 if (sh->slice_type == HEVC_SLICE_B)
701 sh->nb_refs[L1] = s->ps.pps->num_ref_idx_l1_default_active;
703 if (get_bits1(gb)) { // num_ref_idx_active_override_flag
704 sh->nb_refs[L0] = get_ue_golomb_long(gb) + 1;
705 if (sh->slice_type == HEVC_SLICE_B)
706 sh->nb_refs[L1] = get_ue_golomb_long(gb) + 1;
708 if (sh->nb_refs[L0] > HEVC_MAX_REFS || sh->nb_refs[L1] > HEVC_MAX_REFS) {
709 av_log(s->avctx, AV_LOG_ERROR, "Too many refs: %d/%d.\n",
710 sh->nb_refs[L0], sh->nb_refs[L1]);
711 return AVERROR_INVALIDDATA;
714 sh->rpl_modification_flag[0] = 0;
715 sh->rpl_modification_flag[1] = 0;
716 nb_refs = ff_hevc_frame_nb_refs(s);
718 av_log(s->avctx, AV_LOG_ERROR, "Zero refs for a frame with P or B slices.\n");
719 return AVERROR_INVALIDDATA;
722 if (s->ps.pps->lists_modification_present_flag && nb_refs > 1) {
723 sh->rpl_modification_flag[0] = get_bits1(gb);
724 if (sh->rpl_modification_flag[0]) {
725 for (i = 0; i < sh->nb_refs[L0]; i++)
726 sh->list_entry_lx[0][i] = get_bits(gb, av_ceil_log2(nb_refs));
729 if (sh->slice_type == HEVC_SLICE_B) {
730 sh->rpl_modification_flag[1] = get_bits1(gb);
731 if (sh->rpl_modification_flag[1] == 1)
732 for (i = 0; i < sh->nb_refs[L1]; i++)
733 sh->list_entry_lx[1][i] = get_bits(gb, av_ceil_log2(nb_refs));
737 if (sh->slice_type == HEVC_SLICE_B)
738 sh->mvd_l1_zero_flag = get_bits1(gb);
740 if (s->ps.pps->cabac_init_present_flag)
741 sh->cabac_init_flag = get_bits1(gb);
743 sh->cabac_init_flag = 0;
745 sh->collocated_ref_idx = 0;
746 if (sh->slice_temporal_mvp_enabled_flag) {
747 sh->collocated_list = L0;
748 if (sh->slice_type == HEVC_SLICE_B)
749 sh->collocated_list = !get_bits1(gb);
751 if (sh->nb_refs[sh->collocated_list] > 1) {
752 sh->collocated_ref_idx = get_ue_golomb_long(gb);
753 if (sh->collocated_ref_idx >= sh->nb_refs[sh->collocated_list]) {
754 av_log(s->avctx, AV_LOG_ERROR,
755 "Invalid collocated_ref_idx: %d.\n",
756 sh->collocated_ref_idx);
757 return AVERROR_INVALIDDATA;
762 if ((s->ps.pps->weighted_pred_flag && sh->slice_type == HEVC_SLICE_P) ||
763 (s->ps.pps->weighted_bipred_flag && sh->slice_type == HEVC_SLICE_B)) {
764 int ret = pred_weight_table(s, gb);
769 sh->max_num_merge_cand = 5 - get_ue_golomb_long(gb);
770 if (sh->max_num_merge_cand < 1 || sh->max_num_merge_cand > 5) {
771 av_log(s->avctx, AV_LOG_ERROR,
772 "Invalid number of merging MVP candidates: %d.\n",
773 sh->max_num_merge_cand);
774 return AVERROR_INVALIDDATA;
778 sh->slice_qp_delta = get_se_golomb(gb);
780 if (s->ps.pps->pic_slice_level_chroma_qp_offsets_present_flag) {
781 sh->slice_cb_qp_offset = get_se_golomb(gb);
782 sh->slice_cr_qp_offset = get_se_golomb(gb);
784 sh->slice_cb_qp_offset = 0;
785 sh->slice_cr_qp_offset = 0;
788 if (s->ps.pps->chroma_qp_offset_list_enabled_flag)
789 sh->cu_chroma_qp_offset_enabled_flag = get_bits1(gb);
791 sh->cu_chroma_qp_offset_enabled_flag = 0;
793 if (s->ps.pps->deblocking_filter_control_present_flag) {
794 int deblocking_filter_override_flag = 0;
796 if (s->ps.pps->deblocking_filter_override_enabled_flag)
797 deblocking_filter_override_flag = get_bits1(gb);
799 if (deblocking_filter_override_flag) {
800 sh->disable_deblocking_filter_flag = get_bits1(gb);
801 if (!sh->disable_deblocking_filter_flag) {
802 int beta_offset_div2 = get_se_golomb(gb);
803 int tc_offset_div2 = get_se_golomb(gb) ;
804 if (beta_offset_div2 < -6 || beta_offset_div2 > 6 ||
805 tc_offset_div2 < -6 || tc_offset_div2 > 6) {
806 av_log(s->avctx, AV_LOG_ERROR,
807 "Invalid deblock filter offsets: %d, %d\n",
808 beta_offset_div2, tc_offset_div2);
809 return AVERROR_INVALIDDATA;
811 sh->beta_offset = beta_offset_div2 * 2;
812 sh->tc_offset = tc_offset_div2 * 2;
815 sh->disable_deblocking_filter_flag = s->ps.pps->disable_dbf;
816 sh->beta_offset = s->ps.pps->beta_offset;
817 sh->tc_offset = s->ps.pps->tc_offset;
820 sh->disable_deblocking_filter_flag = 0;
825 if (s->ps.pps->seq_loop_filter_across_slices_enabled_flag &&
826 (sh->slice_sample_adaptive_offset_flag[0] ||
827 sh->slice_sample_adaptive_offset_flag[1] ||
828 !sh->disable_deblocking_filter_flag)) {
829 sh->slice_loop_filter_across_slices_enabled_flag = get_bits1(gb);
831 sh->slice_loop_filter_across_slices_enabled_flag = s->ps.pps->seq_loop_filter_across_slices_enabled_flag;
833 } else if (!s->slice_initialized) {
834 av_log(s->avctx, AV_LOG_ERROR, "Independent slice segment missing.\n");
835 return AVERROR_INVALIDDATA;
838 sh->num_entry_point_offsets = 0;
839 if (s->ps.pps->tiles_enabled_flag || s->ps.pps->entropy_coding_sync_enabled_flag) {
840 unsigned num_entry_point_offsets = get_ue_golomb_long(gb);
841 // It would be possible to bound this tighter but this here is simpler
842 if (num_entry_point_offsets > get_bits_left(gb)) {
843 av_log(s->avctx, AV_LOG_ERROR, "num_entry_point_offsets %d is invalid\n", num_entry_point_offsets);
844 return AVERROR_INVALIDDATA;
847 sh->num_entry_point_offsets = num_entry_point_offsets;
848 if (sh->num_entry_point_offsets > 0) {
849 int offset_len = get_ue_golomb_long(gb) + 1;
851 if (offset_len < 1 || offset_len > 32) {
852 sh->num_entry_point_offsets = 0;
853 av_log(s->avctx, AV_LOG_ERROR, "offset_len %d is invalid\n", offset_len);
854 return AVERROR_INVALIDDATA;
857 av_freep(&sh->entry_point_offset);
858 av_freep(&sh->offset);
860 sh->entry_point_offset = av_malloc_array(sh->num_entry_point_offsets, sizeof(unsigned));
861 sh->offset = av_malloc_array(sh->num_entry_point_offsets, sizeof(int));
862 sh->size = av_malloc_array(sh->num_entry_point_offsets, sizeof(int));
863 if (!sh->entry_point_offset || !sh->offset || !sh->size) {
864 sh->num_entry_point_offsets = 0;
865 av_log(s->avctx, AV_LOG_ERROR, "Failed to allocate memory\n");
866 return AVERROR(ENOMEM);
868 for (i = 0; i < sh->num_entry_point_offsets; i++) {
869 unsigned val = get_bits_long(gb, offset_len);
870 sh->entry_point_offset[i] = val + 1; // +1; // +1 to get the size
872 if (s->threads_number > 1 && (s->ps.pps->num_tile_rows > 1 || s->ps.pps->num_tile_columns > 1)) {
873 s->enable_parallel_tiles = 0; // TODO: you can enable tiles in parallel here
874 s->threads_number = 1;
876 s->enable_parallel_tiles = 0;
878 s->enable_parallel_tiles = 0;
881 if (s->ps.pps->slice_header_extension_present_flag) {
882 unsigned int length = get_ue_golomb_long(gb);
883 if (length*8LL > get_bits_left(gb)) {
884 av_log(s->avctx, AV_LOG_ERROR, "too many slice_header_extension_data_bytes\n");
885 return AVERROR_INVALIDDATA;
887 for (i = 0; i < length; i++)
888 skip_bits(gb, 8); // slice_header_extension_data_byte
891 // Inferred parameters
892 sh->slice_qp = 26U + s->ps.pps->pic_init_qp_minus26 + sh->slice_qp_delta;
893 if (sh->slice_qp > 51 ||
894 sh->slice_qp < -s->ps.sps->qp_bd_offset) {
895 av_log(s->avctx, AV_LOG_ERROR,
896 "The slice_qp %d is outside the valid range "
899 -s->ps.sps->qp_bd_offset);
900 return AVERROR_INVALIDDATA;
903 sh->slice_ctb_addr_rs = sh->slice_segment_addr;
905 if (!s->sh.slice_ctb_addr_rs && s->sh.dependent_slice_segment_flag) {
906 av_log(s->avctx, AV_LOG_ERROR, "Impossible slice segment.\n");
907 return AVERROR_INVALIDDATA;
910 if (get_bits_left(gb) < 0) {
911 av_log(s->avctx, AV_LOG_ERROR,
912 "Overread slice header by %d bits\n", -get_bits_left(gb));
913 return AVERROR_INVALIDDATA;
916 s->HEVClc->first_qp_group = !s->sh.dependent_slice_segment_flag;
918 if (!s->ps.pps->cu_qp_delta_enabled_flag)
919 s->HEVClc->qp_y = s->sh.slice_qp;
921 s->slice_initialized = 1;
922 s->HEVClc->tu.cu_qp_offset_cb = 0;
923 s->HEVClc->tu.cu_qp_offset_cr = 0;
928 #define CTB(tab, x, y) ((tab)[(y) * s->ps.sps->ctb_width + (x)])
930 #define SET_SAO(elem, value) \
932 if (!sao_merge_up_flag && !sao_merge_left_flag) \
934 else if (sao_merge_left_flag) \
935 sao->elem = CTB(s->sao, rx-1, ry).elem; \
936 else if (sao_merge_up_flag) \
937 sao->elem = CTB(s->sao, rx, ry-1).elem; \
942 static void hls_sao_param(HEVCContext *s, int rx, int ry)
944 HEVCLocalContext *lc = s->HEVClc;
945 int sao_merge_left_flag = 0;
946 int sao_merge_up_flag = 0;
947 SAOParams *sao = &CTB(s->sao, rx, ry);
950 if (s->sh.slice_sample_adaptive_offset_flag[0] ||
951 s->sh.slice_sample_adaptive_offset_flag[1]) {
953 if (lc->ctb_left_flag)
954 sao_merge_left_flag = ff_hevc_sao_merge_flag_decode(s);
956 if (ry > 0 && !sao_merge_left_flag) {
958 sao_merge_up_flag = ff_hevc_sao_merge_flag_decode(s);
962 for (c_idx = 0; c_idx < (s->ps.sps->chroma_format_idc ? 3 : 1); c_idx++) {
963 int log2_sao_offset_scale = c_idx == 0 ? s->ps.pps->log2_sao_offset_scale_luma :
964 s->ps.pps->log2_sao_offset_scale_chroma;
966 if (!s->sh.slice_sample_adaptive_offset_flag[c_idx]) {
967 sao->type_idx[c_idx] = SAO_NOT_APPLIED;
972 sao->type_idx[2] = sao->type_idx[1];
973 sao->eo_class[2] = sao->eo_class[1];
975 SET_SAO(type_idx[c_idx], ff_hevc_sao_type_idx_decode(s));
978 if (sao->type_idx[c_idx] == SAO_NOT_APPLIED)
981 for (i = 0; i < 4; i++)
982 SET_SAO(offset_abs[c_idx][i], ff_hevc_sao_offset_abs_decode(s));
984 if (sao->type_idx[c_idx] == SAO_BAND) {
985 for (i = 0; i < 4; i++) {
986 if (sao->offset_abs[c_idx][i]) {
987 SET_SAO(offset_sign[c_idx][i],
988 ff_hevc_sao_offset_sign_decode(s));
990 sao->offset_sign[c_idx][i] = 0;
993 SET_SAO(band_position[c_idx], ff_hevc_sao_band_position_decode(s));
994 } else if (c_idx != 2) {
995 SET_SAO(eo_class[c_idx], ff_hevc_sao_eo_class_decode(s));
998 // Inferred parameters
999 sao->offset_val[c_idx][0] = 0;
1000 for (i = 0; i < 4; i++) {
1001 sao->offset_val[c_idx][i + 1] = sao->offset_abs[c_idx][i];
1002 if (sao->type_idx[c_idx] == SAO_EDGE) {
1004 sao->offset_val[c_idx][i + 1] = -sao->offset_val[c_idx][i + 1];
1005 } else if (sao->offset_sign[c_idx][i]) {
1006 sao->offset_val[c_idx][i + 1] = -sao->offset_val[c_idx][i + 1];
1008 sao->offset_val[c_idx][i + 1] *= 1 << log2_sao_offset_scale;
1016 static int hls_cross_component_pred(HEVCContext *s, int idx) {
1017 HEVCLocalContext *lc = s->HEVClc;
1018 int log2_res_scale_abs_plus1 = ff_hevc_log2_res_scale_abs(s, idx);
1020 if (log2_res_scale_abs_plus1 != 0) {
1021 int res_scale_sign_flag = ff_hevc_res_scale_sign_flag(s, idx);
1022 lc->tu.res_scale_val = (1 << (log2_res_scale_abs_plus1 - 1)) *
1023 (1 - 2 * res_scale_sign_flag);
1025 lc->tu.res_scale_val = 0;
1032 static int hls_transform_unit(HEVCContext *s, int x0, int y0,
1033 int xBase, int yBase, int cb_xBase, int cb_yBase,
1034 int log2_cb_size, int log2_trafo_size,
1035 int blk_idx, int cbf_luma, int *cbf_cb, int *cbf_cr)
1037 HEVCLocalContext *lc = s->HEVClc;
1038 const int log2_trafo_size_c = log2_trafo_size - s->ps.sps->hshift[1];
1041 if (lc->cu.pred_mode == MODE_INTRA) {
1042 int trafo_size = 1 << log2_trafo_size;
1043 ff_hevc_set_neighbour_available(s, x0, y0, trafo_size, trafo_size);
1045 s->hpc.intra_pred[log2_trafo_size - 2](s, x0, y0, 0);
1048 if (cbf_luma || cbf_cb[0] || cbf_cr[0] ||
1049 (s->ps.sps->chroma_format_idc == 2 && (cbf_cb[1] || cbf_cr[1]))) {
1050 int scan_idx = SCAN_DIAG;
1051 int scan_idx_c = SCAN_DIAG;
1052 int cbf_chroma = cbf_cb[0] || cbf_cr[0] ||
1053 (s->ps.sps->chroma_format_idc == 2 &&
1054 (cbf_cb[1] || cbf_cr[1]));
1056 if (s->ps.pps->cu_qp_delta_enabled_flag && !lc->tu.is_cu_qp_delta_coded) {
1057 lc->tu.cu_qp_delta = ff_hevc_cu_qp_delta_abs(s);
1058 if (lc->tu.cu_qp_delta != 0)
1059 if (ff_hevc_cu_qp_delta_sign_flag(s) == 1)
1060 lc->tu.cu_qp_delta = -lc->tu.cu_qp_delta;
1061 lc->tu.is_cu_qp_delta_coded = 1;
1063 if (lc->tu.cu_qp_delta < -(26 + s->ps.sps->qp_bd_offset / 2) ||
1064 lc->tu.cu_qp_delta > (25 + s->ps.sps->qp_bd_offset / 2)) {
1065 av_log(s->avctx, AV_LOG_ERROR,
1066 "The cu_qp_delta %d is outside the valid range "
1069 -(26 + s->ps.sps->qp_bd_offset / 2),
1070 (25 + s->ps.sps->qp_bd_offset / 2));
1071 return AVERROR_INVALIDDATA;
1074 ff_hevc_set_qPy(s, cb_xBase, cb_yBase, log2_cb_size);
1077 if (s->sh.cu_chroma_qp_offset_enabled_flag && cbf_chroma &&
1078 !lc->cu.cu_transquant_bypass_flag && !lc->tu.is_cu_chroma_qp_offset_coded) {
1079 int cu_chroma_qp_offset_flag = ff_hevc_cu_chroma_qp_offset_flag(s);
1080 if (cu_chroma_qp_offset_flag) {
1081 int cu_chroma_qp_offset_idx = 0;
1082 if (s->ps.pps->chroma_qp_offset_list_len_minus1 > 0) {
1083 cu_chroma_qp_offset_idx = ff_hevc_cu_chroma_qp_offset_idx(s);
1084 av_log(s->avctx, AV_LOG_ERROR,
1085 "cu_chroma_qp_offset_idx not yet tested.\n");
1087 lc->tu.cu_qp_offset_cb = s->ps.pps->cb_qp_offset_list[cu_chroma_qp_offset_idx];
1088 lc->tu.cu_qp_offset_cr = s->ps.pps->cr_qp_offset_list[cu_chroma_qp_offset_idx];
1090 lc->tu.cu_qp_offset_cb = 0;
1091 lc->tu.cu_qp_offset_cr = 0;
1093 lc->tu.is_cu_chroma_qp_offset_coded = 1;
1096 if (lc->cu.pred_mode == MODE_INTRA && log2_trafo_size < 4) {
1097 if (lc->tu.intra_pred_mode >= 6 &&
1098 lc->tu.intra_pred_mode <= 14) {
1099 scan_idx = SCAN_VERT;
1100 } else if (lc->tu.intra_pred_mode >= 22 &&
1101 lc->tu.intra_pred_mode <= 30) {
1102 scan_idx = SCAN_HORIZ;
1105 if (lc->tu.intra_pred_mode_c >= 6 &&
1106 lc->tu.intra_pred_mode_c <= 14) {
1107 scan_idx_c = SCAN_VERT;
1108 } else if (lc->tu.intra_pred_mode_c >= 22 &&
1109 lc->tu.intra_pred_mode_c <= 30) {
1110 scan_idx_c = SCAN_HORIZ;
1114 lc->tu.cross_pf = 0;
1117 ff_hevc_hls_residual_coding(s, x0, y0, log2_trafo_size, scan_idx, 0);
1118 if (s->ps.sps->chroma_format_idc && (log2_trafo_size > 2 || s->ps.sps->chroma_format_idc == 3)) {
1119 int trafo_size_h = 1 << (log2_trafo_size_c + s->ps.sps->hshift[1]);
1120 int trafo_size_v = 1 << (log2_trafo_size_c + s->ps.sps->vshift[1]);
1121 lc->tu.cross_pf = (s->ps.pps->cross_component_prediction_enabled_flag && cbf_luma &&
1122 (lc->cu.pred_mode == MODE_INTER ||
1123 (lc->tu.chroma_mode_c == 4)));
1125 if (lc->tu.cross_pf) {
1126 hls_cross_component_pred(s, 0);
1128 for (i = 0; i < (s->ps.sps->chroma_format_idc == 2 ? 2 : 1); i++) {
1129 if (lc->cu.pred_mode == MODE_INTRA) {
1130 ff_hevc_set_neighbour_available(s, x0, y0 + (i << log2_trafo_size_c), trafo_size_h, trafo_size_v);
1131 s->hpc.intra_pred[log2_trafo_size_c - 2](s, x0, y0 + (i << log2_trafo_size_c), 1);
1134 ff_hevc_hls_residual_coding(s, x0, y0 + (i << log2_trafo_size_c),
1135 log2_trafo_size_c, scan_idx_c, 1);
1137 if (lc->tu.cross_pf) {
1138 ptrdiff_t stride = s->frame->linesize[1];
1139 int hshift = s->ps.sps->hshift[1];
1140 int vshift = s->ps.sps->vshift[1];
1141 int16_t *coeffs_y = (int16_t*)lc->edge_emu_buffer;
1142 int16_t *coeffs = (int16_t*)lc->edge_emu_buffer2;
1143 int size = 1 << log2_trafo_size_c;
1145 uint8_t *dst = &s->frame->data[1][(y0 >> vshift) * stride +
1146 ((x0 >> hshift) << s->ps.sps->pixel_shift)];
1147 for (i = 0; i < (size * size); i++) {
1148 coeffs[i] = ((lc->tu.res_scale_val * coeffs_y[i]) >> 3);
1150 s->hevcdsp.add_residual[log2_trafo_size_c-2](dst, coeffs, stride);
1154 if (lc->tu.cross_pf) {
1155 hls_cross_component_pred(s, 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, x0, y0 + (i << log2_trafo_size_c), trafo_size_h, trafo_size_v);
1160 s->hpc.intra_pred[log2_trafo_size_c - 2](s, x0, y0 + (i << log2_trafo_size_c), 2);
1163 ff_hevc_hls_residual_coding(s, x0, y0 + (i << log2_trafo_size_c),
1164 log2_trafo_size_c, scan_idx_c, 2);
1166 if (lc->tu.cross_pf) {
1167 ptrdiff_t stride = s->frame->linesize[2];
1168 int hshift = s->ps.sps->hshift[2];
1169 int vshift = s->ps.sps->vshift[2];
1170 int16_t *coeffs_y = (int16_t*)lc->edge_emu_buffer;
1171 int16_t *coeffs = (int16_t*)lc->edge_emu_buffer2;
1172 int size = 1 << log2_trafo_size_c;
1174 uint8_t *dst = &s->frame->data[2][(y0 >> vshift) * stride +
1175 ((x0 >> hshift) << s->ps.sps->pixel_shift)];
1176 for (i = 0; i < (size * size); i++) {
1177 coeffs[i] = ((lc->tu.res_scale_val * coeffs_y[i]) >> 3);
1179 s->hevcdsp.add_residual[log2_trafo_size_c-2](dst, coeffs, stride);
1182 } else if (s->ps.sps->chroma_format_idc && blk_idx == 3) {
1183 int trafo_size_h = 1 << (log2_trafo_size + 1);
1184 int trafo_size_v = 1 << (log2_trafo_size + s->ps.sps->vshift[1]);
1185 for (i = 0; i < (s->ps.sps->chroma_format_idc == 2 ? 2 : 1); i++) {
1186 if (lc->cu.pred_mode == MODE_INTRA) {
1187 ff_hevc_set_neighbour_available(s, xBase, yBase + (i << log2_trafo_size),
1188 trafo_size_h, trafo_size_v);
1189 s->hpc.intra_pred[log2_trafo_size - 2](s, xBase, yBase + (i << log2_trafo_size), 1);
1192 ff_hevc_hls_residual_coding(s, xBase, yBase + (i << log2_trafo_size),
1193 log2_trafo_size, scan_idx_c, 1);
1195 for (i = 0; i < (s->ps.sps->chroma_format_idc == 2 ? 2 : 1); i++) {
1196 if (lc->cu.pred_mode == MODE_INTRA) {
1197 ff_hevc_set_neighbour_available(s, xBase, yBase + (i << log2_trafo_size),
1198 trafo_size_h, trafo_size_v);
1199 s->hpc.intra_pred[log2_trafo_size - 2](s, xBase, yBase + (i << log2_trafo_size), 2);
1202 ff_hevc_hls_residual_coding(s, xBase, yBase + (i << log2_trafo_size),
1203 log2_trafo_size, scan_idx_c, 2);
1206 } else if (s->ps.sps->chroma_format_idc && lc->cu.pred_mode == MODE_INTRA) {
1207 if (log2_trafo_size > 2 || s->ps.sps->chroma_format_idc == 3) {
1208 int trafo_size_h = 1 << (log2_trafo_size_c + s->ps.sps->hshift[1]);
1209 int trafo_size_v = 1 << (log2_trafo_size_c + s->ps.sps->vshift[1]);
1210 ff_hevc_set_neighbour_available(s, x0, y0, trafo_size_h, trafo_size_v);
1211 s->hpc.intra_pred[log2_trafo_size_c - 2](s, x0, y0, 1);
1212 s->hpc.intra_pred[log2_trafo_size_c - 2](s, x0, y0, 2);
1213 if (s->ps.sps->chroma_format_idc == 2) {
1214 ff_hevc_set_neighbour_available(s, x0, y0 + (1 << log2_trafo_size_c),
1215 trafo_size_h, trafo_size_v);
1216 s->hpc.intra_pred[log2_trafo_size_c - 2](s, x0, y0 + (1 << log2_trafo_size_c), 1);
1217 s->hpc.intra_pred[log2_trafo_size_c - 2](s, x0, y0 + (1 << log2_trafo_size_c), 2);
1219 } else if (blk_idx == 3) {
1220 int trafo_size_h = 1 << (log2_trafo_size + 1);
1221 int trafo_size_v = 1 << (log2_trafo_size + s->ps.sps->vshift[1]);
1222 ff_hevc_set_neighbour_available(s, xBase, yBase,
1223 trafo_size_h, trafo_size_v);
1224 s->hpc.intra_pred[log2_trafo_size - 2](s, xBase, yBase, 1);
1225 s->hpc.intra_pred[log2_trafo_size - 2](s, xBase, yBase, 2);
1226 if (s->ps.sps->chroma_format_idc == 2) {
1227 ff_hevc_set_neighbour_available(s, xBase, yBase + (1 << (log2_trafo_size)),
1228 trafo_size_h, trafo_size_v);
1229 s->hpc.intra_pred[log2_trafo_size - 2](s, xBase, yBase + (1 << (log2_trafo_size)), 1);
1230 s->hpc.intra_pred[log2_trafo_size - 2](s, xBase, yBase + (1 << (log2_trafo_size)), 2);
1238 static void set_deblocking_bypass(HEVCContext *s, int x0, int y0, int log2_cb_size)
1240 int cb_size = 1 << log2_cb_size;
1241 int log2_min_pu_size = s->ps.sps->log2_min_pu_size;
1243 int min_pu_width = s->ps.sps->min_pu_width;
1244 int x_end = FFMIN(x0 + cb_size, s->ps.sps->width);
1245 int y_end = FFMIN(y0 + cb_size, s->ps.sps->height);
1248 for (j = (y0 >> log2_min_pu_size); j < (y_end >> log2_min_pu_size); j++)
1249 for (i = (x0 >> log2_min_pu_size); i < (x_end >> log2_min_pu_size); i++)
1250 s->is_pcm[i + j * min_pu_width] = 2;
1253 static int hls_transform_tree(HEVCContext *s, int x0, int y0,
1254 int xBase, int yBase, int cb_xBase, int cb_yBase,
1255 int log2_cb_size, int log2_trafo_size,
1256 int trafo_depth, int blk_idx,
1257 const int *base_cbf_cb, const int *base_cbf_cr)
1259 HEVCLocalContext *lc = s->HEVClc;
1260 uint8_t split_transform_flag;
1265 cbf_cb[0] = base_cbf_cb[0];
1266 cbf_cb[1] = base_cbf_cb[1];
1267 cbf_cr[0] = base_cbf_cr[0];
1268 cbf_cr[1] = base_cbf_cr[1];
1270 if (lc->cu.intra_split_flag) {
1271 if (trafo_depth == 1) {
1272 lc->tu.intra_pred_mode = lc->pu.intra_pred_mode[blk_idx];
1273 if (s->ps.sps->chroma_format_idc == 3) {
1274 lc->tu.intra_pred_mode_c = lc->pu.intra_pred_mode_c[blk_idx];
1275 lc->tu.chroma_mode_c = lc->pu.chroma_mode_c[blk_idx];
1277 lc->tu.intra_pred_mode_c = lc->pu.intra_pred_mode_c[0];
1278 lc->tu.chroma_mode_c = lc->pu.chroma_mode_c[0];
1282 lc->tu.intra_pred_mode = lc->pu.intra_pred_mode[0];
1283 lc->tu.intra_pred_mode_c = lc->pu.intra_pred_mode_c[0];
1284 lc->tu.chroma_mode_c = lc->pu.chroma_mode_c[0];
1287 if (log2_trafo_size <= s->ps.sps->log2_max_trafo_size &&
1288 log2_trafo_size > s->ps.sps->log2_min_tb_size &&
1289 trafo_depth < lc->cu.max_trafo_depth &&
1290 !(lc->cu.intra_split_flag && trafo_depth == 0)) {
1291 split_transform_flag = ff_hevc_split_transform_flag_decode(s, log2_trafo_size);
1293 int inter_split = s->ps.sps->max_transform_hierarchy_depth_inter == 0 &&
1294 lc->cu.pred_mode == MODE_INTER &&
1295 lc->cu.part_mode != PART_2Nx2N &&
1298 split_transform_flag = log2_trafo_size > s->ps.sps->log2_max_trafo_size ||
1299 (lc->cu.intra_split_flag && trafo_depth == 0) ||
1303 if (s->ps.sps->chroma_format_idc && (log2_trafo_size > 2 || s->ps.sps->chroma_format_idc == 3)) {
1304 if (trafo_depth == 0 || cbf_cb[0]) {
1305 cbf_cb[0] = ff_hevc_cbf_cb_cr_decode(s, trafo_depth);
1306 if (s->ps.sps->chroma_format_idc == 2 && (!split_transform_flag || log2_trafo_size == 3)) {
1307 cbf_cb[1] = ff_hevc_cbf_cb_cr_decode(s, trafo_depth);
1311 if (trafo_depth == 0 || cbf_cr[0]) {
1312 cbf_cr[0] = ff_hevc_cbf_cb_cr_decode(s, trafo_depth);
1313 if (s->ps.sps->chroma_format_idc == 2 && (!split_transform_flag || log2_trafo_size == 3)) {
1314 cbf_cr[1] = ff_hevc_cbf_cb_cr_decode(s, trafo_depth);
1319 if (split_transform_flag) {
1320 const int trafo_size_split = 1 << (log2_trafo_size - 1);
1321 const int x1 = x0 + trafo_size_split;
1322 const int y1 = y0 + trafo_size_split;
1324 #define SUBDIVIDE(x, y, idx) \
1326 ret = hls_transform_tree(s, x, y, x0, y0, cb_xBase, cb_yBase, log2_cb_size, \
1327 log2_trafo_size - 1, trafo_depth + 1, idx, \
1333 SUBDIVIDE(x0, y0, 0);
1334 SUBDIVIDE(x1, y0, 1);
1335 SUBDIVIDE(x0, y1, 2);
1336 SUBDIVIDE(x1, y1, 3);
1340 int min_tu_size = 1 << s->ps.sps->log2_min_tb_size;
1341 int log2_min_tu_size = s->ps.sps->log2_min_tb_size;
1342 int min_tu_width = s->ps.sps->min_tb_width;
1345 if (lc->cu.pred_mode == MODE_INTRA || trafo_depth != 0 ||
1346 cbf_cb[0] || cbf_cr[0] ||
1347 (s->ps.sps->chroma_format_idc == 2 && (cbf_cb[1] || cbf_cr[1]))) {
1348 cbf_luma = ff_hevc_cbf_luma_decode(s, trafo_depth);
1351 ret = hls_transform_unit(s, x0, y0, xBase, yBase, cb_xBase, cb_yBase,
1352 log2_cb_size, log2_trafo_size,
1353 blk_idx, cbf_luma, cbf_cb, cbf_cr);
1356 // TODO: store cbf_luma somewhere else
1359 for (i = 0; i < (1 << log2_trafo_size); i += min_tu_size)
1360 for (j = 0; j < (1 << log2_trafo_size); j += min_tu_size) {
1361 int x_tu = (x0 + j) >> log2_min_tu_size;
1362 int y_tu = (y0 + i) >> log2_min_tu_size;
1363 s->cbf_luma[y_tu * min_tu_width + x_tu] = 1;
1366 if (!s->sh.disable_deblocking_filter_flag) {
1367 ff_hevc_deblocking_boundary_strengths(s, x0, y0, log2_trafo_size);
1368 if (s->ps.pps->transquant_bypass_enable_flag &&
1369 lc->cu.cu_transquant_bypass_flag)
1370 set_deblocking_bypass(s, x0, y0, log2_trafo_size);
1376 static int hls_pcm_sample(HEVCContext *s, int x0, int y0, int log2_cb_size)
1378 HEVCLocalContext *lc = s->HEVClc;
1380 int cb_size = 1 << log2_cb_size;
1381 ptrdiff_t stride0 = s->frame->linesize[0];
1382 ptrdiff_t stride1 = s->frame->linesize[1];
1383 ptrdiff_t stride2 = s->frame->linesize[2];
1384 uint8_t *dst0 = &s->frame->data[0][y0 * stride0 + (x0 << s->ps.sps->pixel_shift)];
1385 uint8_t *dst1 = &s->frame->data[1][(y0 >> s->ps.sps->vshift[1]) * stride1 + ((x0 >> s->ps.sps->hshift[1]) << s->ps.sps->pixel_shift)];
1386 uint8_t *dst2 = &s->frame->data[2][(y0 >> s->ps.sps->vshift[2]) * stride2 + ((x0 >> s->ps.sps->hshift[2]) << s->ps.sps->pixel_shift)];
1388 int length = cb_size * cb_size * s->ps.sps->pcm.bit_depth +
1389 (((cb_size >> s->ps.sps->hshift[1]) * (cb_size >> s->ps.sps->vshift[1])) +
1390 ((cb_size >> s->ps.sps->hshift[2]) * (cb_size >> s->ps.sps->vshift[2]))) *
1391 s->ps.sps->pcm.bit_depth_chroma;
1392 const uint8_t *pcm = skip_bytes(&lc->cc, (length + 7) >> 3);
1395 if (!s->sh.disable_deblocking_filter_flag)
1396 ff_hevc_deblocking_boundary_strengths(s, x0, y0, log2_cb_size);
1398 ret = init_get_bits(&gb, pcm, length);
1402 s->hevcdsp.put_pcm(dst0, stride0, cb_size, cb_size, &gb, s->ps.sps->pcm.bit_depth);
1403 if (s->ps.sps->chroma_format_idc) {
1404 s->hevcdsp.put_pcm(dst1, stride1,
1405 cb_size >> s->ps.sps->hshift[1],
1406 cb_size >> s->ps.sps->vshift[1],
1407 &gb, s->ps.sps->pcm.bit_depth_chroma);
1408 s->hevcdsp.put_pcm(dst2, stride2,
1409 cb_size >> s->ps.sps->hshift[2],
1410 cb_size >> s->ps.sps->vshift[2],
1411 &gb, s->ps.sps->pcm.bit_depth_chroma);
1418 * 8.5.3.2.2.1 Luma sample unidirectional interpolation process
1420 * @param s HEVC decoding context
1421 * @param dst target buffer for block data at block position
1422 * @param dststride stride of the dst buffer
1423 * @param ref reference picture buffer at origin (0, 0)
1424 * @param mv motion vector (relative to block position) to get pixel data from
1425 * @param x_off horizontal position of block from origin (0, 0)
1426 * @param y_off vertical position of block from origin (0, 0)
1427 * @param block_w width of block
1428 * @param block_h height of block
1429 * @param luma_weight weighting factor applied to the luma prediction
1430 * @param luma_offset additive offset applied to the luma prediction value
1433 static void luma_mc_uni(HEVCContext *s, uint8_t *dst, ptrdiff_t dststride,
1434 AVFrame *ref, const Mv *mv, int x_off, int y_off,
1435 int block_w, int block_h, int luma_weight, int luma_offset)
1437 HEVCLocalContext *lc = s->HEVClc;
1438 uint8_t *src = ref->data[0];
1439 ptrdiff_t srcstride = ref->linesize[0];
1440 int pic_width = s->ps.sps->width;
1441 int pic_height = s->ps.sps->height;
1444 int weight_flag = (s->sh.slice_type == HEVC_SLICE_P && s->ps.pps->weighted_pred_flag) ||
1445 (s->sh.slice_type == HEVC_SLICE_B && s->ps.pps->weighted_bipred_flag);
1446 int idx = ff_hevc_pel_weight[block_w];
1448 x_off += mv->x >> 2;
1449 y_off += mv->y >> 2;
1450 src += y_off * srcstride + (x_off * (1 << s->ps.sps->pixel_shift));
1452 if (x_off < QPEL_EXTRA_BEFORE || y_off < QPEL_EXTRA_AFTER ||
1453 x_off >= pic_width - block_w - QPEL_EXTRA_AFTER ||
1454 y_off >= pic_height - block_h - QPEL_EXTRA_AFTER) {
1455 const ptrdiff_t edge_emu_stride = EDGE_EMU_BUFFER_STRIDE << s->ps.sps->pixel_shift;
1456 int offset = QPEL_EXTRA_BEFORE * srcstride + (QPEL_EXTRA_BEFORE << s->ps.sps->pixel_shift);
1457 int buf_offset = QPEL_EXTRA_BEFORE * edge_emu_stride + (QPEL_EXTRA_BEFORE << s->ps.sps->pixel_shift);
1459 s->vdsp.emulated_edge_mc(lc->edge_emu_buffer, src - offset,
1460 edge_emu_stride, srcstride,
1461 block_w + QPEL_EXTRA,
1462 block_h + QPEL_EXTRA,
1463 x_off - QPEL_EXTRA_BEFORE, y_off - QPEL_EXTRA_BEFORE,
1464 pic_width, pic_height);
1465 src = lc->edge_emu_buffer + buf_offset;
1466 srcstride = edge_emu_stride;
1470 s->hevcdsp.put_hevc_qpel_uni[idx][!!my][!!mx](dst, dststride, src, srcstride,
1471 block_h, mx, my, block_w);
1473 s->hevcdsp.put_hevc_qpel_uni_w[idx][!!my][!!mx](dst, dststride, src, srcstride,
1474 block_h, s->sh.luma_log2_weight_denom,
1475 luma_weight, luma_offset, mx, my, block_w);
1479 * 8.5.3.2.2.1 Luma sample bidirectional interpolation process
1481 * @param s HEVC decoding context
1482 * @param dst target buffer for block data at block position
1483 * @param dststride stride of the dst buffer
1484 * @param ref0 reference picture0 buffer at origin (0, 0)
1485 * @param mv0 motion vector0 (relative to block position) to get pixel data from
1486 * @param x_off horizontal position of block from origin (0, 0)
1487 * @param y_off vertical position of block from origin (0, 0)
1488 * @param block_w width of block
1489 * @param block_h height of block
1490 * @param ref1 reference picture1 buffer at origin (0, 0)
1491 * @param mv1 motion vector1 (relative to block position) to get pixel data from
1492 * @param current_mv current motion vector structure
1494 static void luma_mc_bi(HEVCContext *s, uint8_t *dst, ptrdiff_t dststride,
1495 AVFrame *ref0, const Mv *mv0, int x_off, int y_off,
1496 int block_w, int block_h, AVFrame *ref1, const Mv *mv1, struct MvField *current_mv)
1498 HEVCLocalContext *lc = s->HEVClc;
1499 ptrdiff_t src0stride = ref0->linesize[0];
1500 ptrdiff_t src1stride = ref1->linesize[0];
1501 int pic_width = s->ps.sps->width;
1502 int pic_height = s->ps.sps->height;
1503 int mx0 = mv0->x & 3;
1504 int my0 = mv0->y & 3;
1505 int mx1 = mv1->x & 3;
1506 int my1 = mv1->y & 3;
1507 int weight_flag = (s->sh.slice_type == HEVC_SLICE_P && s->ps.pps->weighted_pred_flag) ||
1508 (s->sh.slice_type == HEVC_SLICE_B && s->ps.pps->weighted_bipred_flag);
1509 int x_off0 = x_off + (mv0->x >> 2);
1510 int y_off0 = y_off + (mv0->y >> 2);
1511 int x_off1 = x_off + (mv1->x >> 2);
1512 int y_off1 = y_off + (mv1->y >> 2);
1513 int idx = ff_hevc_pel_weight[block_w];
1515 uint8_t *src0 = ref0->data[0] + y_off0 * src0stride + (int)((unsigned)x_off0 << s->ps.sps->pixel_shift);
1516 uint8_t *src1 = ref1->data[0] + y_off1 * src1stride + (int)((unsigned)x_off1 << s->ps.sps->pixel_shift);
1518 if (x_off0 < QPEL_EXTRA_BEFORE || y_off0 < QPEL_EXTRA_AFTER ||
1519 x_off0 >= pic_width - block_w - QPEL_EXTRA_AFTER ||
1520 y_off0 >= pic_height - block_h - QPEL_EXTRA_AFTER) {
1521 const ptrdiff_t edge_emu_stride = EDGE_EMU_BUFFER_STRIDE << s->ps.sps->pixel_shift;
1522 int offset = QPEL_EXTRA_BEFORE * src0stride + (QPEL_EXTRA_BEFORE << s->ps.sps->pixel_shift);
1523 int buf_offset = QPEL_EXTRA_BEFORE * edge_emu_stride + (QPEL_EXTRA_BEFORE << s->ps.sps->pixel_shift);
1525 s->vdsp.emulated_edge_mc(lc->edge_emu_buffer, src0 - offset,
1526 edge_emu_stride, src0stride,
1527 block_w + QPEL_EXTRA,
1528 block_h + QPEL_EXTRA,
1529 x_off0 - QPEL_EXTRA_BEFORE, y_off0 - QPEL_EXTRA_BEFORE,
1530 pic_width, pic_height);
1531 src0 = lc->edge_emu_buffer + buf_offset;
1532 src0stride = edge_emu_stride;
1535 if (x_off1 < QPEL_EXTRA_BEFORE || y_off1 < QPEL_EXTRA_AFTER ||
1536 x_off1 >= pic_width - block_w - QPEL_EXTRA_AFTER ||
1537 y_off1 >= pic_height - block_h - QPEL_EXTRA_AFTER) {
1538 const ptrdiff_t edge_emu_stride = EDGE_EMU_BUFFER_STRIDE << s->ps.sps->pixel_shift;
1539 int offset = QPEL_EXTRA_BEFORE * src1stride + (QPEL_EXTRA_BEFORE << s->ps.sps->pixel_shift);
1540 int buf_offset = QPEL_EXTRA_BEFORE * edge_emu_stride + (QPEL_EXTRA_BEFORE << s->ps.sps->pixel_shift);
1542 s->vdsp.emulated_edge_mc(lc->edge_emu_buffer2, src1 - offset,
1543 edge_emu_stride, src1stride,
1544 block_w + QPEL_EXTRA,
1545 block_h + QPEL_EXTRA,
1546 x_off1 - QPEL_EXTRA_BEFORE, y_off1 - QPEL_EXTRA_BEFORE,
1547 pic_width, pic_height);
1548 src1 = lc->edge_emu_buffer2 + buf_offset;
1549 src1stride = edge_emu_stride;
1552 s->hevcdsp.put_hevc_qpel[idx][!!my0][!!mx0](lc->tmp, src0, src0stride,
1553 block_h, mx0, my0, block_w);
1555 s->hevcdsp.put_hevc_qpel_bi[idx][!!my1][!!mx1](dst, dststride, src1, src1stride, lc->tmp,
1556 block_h, mx1, my1, block_w);
1558 s->hevcdsp.put_hevc_qpel_bi_w[idx][!!my1][!!mx1](dst, dststride, src1, src1stride, lc->tmp,
1559 block_h, s->sh.luma_log2_weight_denom,
1560 s->sh.luma_weight_l0[current_mv->ref_idx[0]],
1561 s->sh.luma_weight_l1[current_mv->ref_idx[1]],
1562 s->sh.luma_offset_l0[current_mv->ref_idx[0]],
1563 s->sh.luma_offset_l1[current_mv->ref_idx[1]],
1569 * 8.5.3.2.2.2 Chroma sample uniprediction interpolation process
1571 * @param s HEVC decoding context
1572 * @param dst1 target buffer for block data at block position (U plane)
1573 * @param dst2 target buffer for block data at block position (V plane)
1574 * @param dststride stride of the dst1 and dst2 buffers
1575 * @param ref reference picture buffer at origin (0, 0)
1576 * @param mv motion vector (relative to block position) to get pixel data from
1577 * @param x_off horizontal position of block from origin (0, 0)
1578 * @param y_off vertical position of block from origin (0, 0)
1579 * @param block_w width of block
1580 * @param block_h height of block
1581 * @param chroma_weight weighting factor applied to the chroma prediction
1582 * @param chroma_offset additive offset applied to the chroma prediction value
1585 static void chroma_mc_uni(HEVCContext *s, uint8_t *dst0,
1586 ptrdiff_t dststride, uint8_t *src0, ptrdiff_t srcstride, int reflist,
1587 int x_off, int y_off, int block_w, int block_h, struct MvField *current_mv, int chroma_weight, int chroma_offset)
1589 HEVCLocalContext *lc = s->HEVClc;
1590 int pic_width = s->ps.sps->width >> s->ps.sps->hshift[1];
1591 int pic_height = s->ps.sps->height >> s->ps.sps->vshift[1];
1592 const Mv *mv = ¤t_mv->mv[reflist];
1593 int weight_flag = (s->sh.slice_type == HEVC_SLICE_P && s->ps.pps->weighted_pred_flag) ||
1594 (s->sh.slice_type == HEVC_SLICE_B && s->ps.pps->weighted_bipred_flag);
1595 int idx = ff_hevc_pel_weight[block_w];
1596 int hshift = s->ps.sps->hshift[1];
1597 int vshift = s->ps.sps->vshift[1];
1598 intptr_t mx = av_mod_uintp2(mv->x, 2 + hshift);
1599 intptr_t my = av_mod_uintp2(mv->y, 2 + vshift);
1600 intptr_t _mx = mx << (1 - hshift);
1601 intptr_t _my = my << (1 - vshift);
1603 x_off += mv->x >> (2 + hshift);
1604 y_off += mv->y >> (2 + vshift);
1605 src0 += y_off * srcstride + (x_off * (1 << s->ps.sps->pixel_shift));
1607 if (x_off < EPEL_EXTRA_BEFORE || y_off < EPEL_EXTRA_AFTER ||
1608 x_off >= pic_width - block_w - EPEL_EXTRA_AFTER ||
1609 y_off >= pic_height - block_h - EPEL_EXTRA_AFTER) {
1610 const int edge_emu_stride = EDGE_EMU_BUFFER_STRIDE << s->ps.sps->pixel_shift;
1611 int offset0 = EPEL_EXTRA_BEFORE * (srcstride + (1 << s->ps.sps->pixel_shift));
1612 int buf_offset0 = EPEL_EXTRA_BEFORE *
1613 (edge_emu_stride + (1 << s->ps.sps->pixel_shift));
1614 s->vdsp.emulated_edge_mc(lc->edge_emu_buffer, src0 - offset0,
1615 edge_emu_stride, srcstride,
1616 block_w + EPEL_EXTRA, block_h + EPEL_EXTRA,
1617 x_off - EPEL_EXTRA_BEFORE,
1618 y_off - EPEL_EXTRA_BEFORE,
1619 pic_width, pic_height);
1621 src0 = lc->edge_emu_buffer + buf_offset0;
1622 srcstride = edge_emu_stride;
1625 s->hevcdsp.put_hevc_epel_uni[idx][!!my][!!mx](dst0, dststride, src0, srcstride,
1626 block_h, _mx, _my, block_w);
1628 s->hevcdsp.put_hevc_epel_uni_w[idx][!!my][!!mx](dst0, dststride, src0, srcstride,
1629 block_h, s->sh.chroma_log2_weight_denom,
1630 chroma_weight, chroma_offset, _mx, _my, block_w);
1634 * 8.5.3.2.2.2 Chroma sample bidirectional interpolation process
1636 * @param s HEVC decoding context
1637 * @param dst target buffer for block data at block position
1638 * @param dststride stride of the dst buffer
1639 * @param ref0 reference picture0 buffer at origin (0, 0)
1640 * @param mv0 motion vector0 (relative to block position) to get pixel data from
1641 * @param x_off horizontal position of block from origin (0, 0)
1642 * @param y_off vertical position of block from origin (0, 0)
1643 * @param block_w width of block
1644 * @param block_h height of block
1645 * @param ref1 reference picture1 buffer at origin (0, 0)
1646 * @param mv1 motion vector1 (relative to block position) to get pixel data from
1647 * @param current_mv current motion vector structure
1648 * @param cidx chroma component(cb, cr)
1650 static void chroma_mc_bi(HEVCContext *s, uint8_t *dst0, ptrdiff_t dststride, AVFrame *ref0, AVFrame *ref1,
1651 int x_off, int y_off, int block_w, int block_h, struct MvField *current_mv, int cidx)
1653 HEVCLocalContext *lc = s->HEVClc;
1654 uint8_t *src1 = ref0->data[cidx+1];
1655 uint8_t *src2 = ref1->data[cidx+1];
1656 ptrdiff_t src1stride = ref0->linesize[cidx+1];
1657 ptrdiff_t src2stride = ref1->linesize[cidx+1];
1658 int weight_flag = (s->sh.slice_type == HEVC_SLICE_P && s->ps.pps->weighted_pred_flag) ||
1659 (s->sh.slice_type == HEVC_SLICE_B && s->ps.pps->weighted_bipred_flag);
1660 int pic_width = s->ps.sps->width >> s->ps.sps->hshift[1];
1661 int pic_height = s->ps.sps->height >> s->ps.sps->vshift[1];
1662 Mv *mv0 = ¤t_mv->mv[0];
1663 Mv *mv1 = ¤t_mv->mv[1];
1664 int hshift = s->ps.sps->hshift[1];
1665 int vshift = s->ps.sps->vshift[1];
1667 intptr_t mx0 = av_mod_uintp2(mv0->x, 2 + hshift);
1668 intptr_t my0 = av_mod_uintp2(mv0->y, 2 + vshift);
1669 intptr_t mx1 = av_mod_uintp2(mv1->x, 2 + hshift);
1670 intptr_t my1 = av_mod_uintp2(mv1->y, 2 + vshift);
1671 intptr_t _mx0 = mx0 << (1 - hshift);
1672 intptr_t _my0 = my0 << (1 - vshift);
1673 intptr_t _mx1 = mx1 << (1 - hshift);
1674 intptr_t _my1 = my1 << (1 - vshift);
1676 int x_off0 = x_off + (mv0->x >> (2 + hshift));
1677 int y_off0 = y_off + (mv0->y >> (2 + vshift));
1678 int x_off1 = x_off + (mv1->x >> (2 + hshift));
1679 int y_off1 = y_off + (mv1->y >> (2 + vshift));
1680 int idx = ff_hevc_pel_weight[block_w];
1681 src1 += y_off0 * src1stride + (int)((unsigned)x_off0 << s->ps.sps->pixel_shift);
1682 src2 += y_off1 * src2stride + (int)((unsigned)x_off1 << s->ps.sps->pixel_shift);
1684 if (x_off0 < EPEL_EXTRA_BEFORE || y_off0 < EPEL_EXTRA_AFTER ||
1685 x_off0 >= pic_width - block_w - EPEL_EXTRA_AFTER ||
1686 y_off0 >= pic_height - block_h - EPEL_EXTRA_AFTER) {
1687 const int edge_emu_stride = EDGE_EMU_BUFFER_STRIDE << s->ps.sps->pixel_shift;
1688 int offset1 = EPEL_EXTRA_BEFORE * (src1stride + (1 << s->ps.sps->pixel_shift));
1689 int buf_offset1 = EPEL_EXTRA_BEFORE *
1690 (edge_emu_stride + (1 << s->ps.sps->pixel_shift));
1692 s->vdsp.emulated_edge_mc(lc->edge_emu_buffer, src1 - offset1,
1693 edge_emu_stride, src1stride,
1694 block_w + EPEL_EXTRA, block_h + EPEL_EXTRA,
1695 x_off0 - EPEL_EXTRA_BEFORE,
1696 y_off0 - EPEL_EXTRA_BEFORE,
1697 pic_width, pic_height);
1699 src1 = lc->edge_emu_buffer + buf_offset1;
1700 src1stride = edge_emu_stride;
1703 if (x_off1 < EPEL_EXTRA_BEFORE || y_off1 < EPEL_EXTRA_AFTER ||
1704 x_off1 >= pic_width - block_w - EPEL_EXTRA_AFTER ||
1705 y_off1 >= pic_height - block_h - EPEL_EXTRA_AFTER) {
1706 const int edge_emu_stride = EDGE_EMU_BUFFER_STRIDE << s->ps.sps->pixel_shift;
1707 int offset1 = EPEL_EXTRA_BEFORE * (src2stride + (1 << s->ps.sps->pixel_shift));
1708 int buf_offset1 = EPEL_EXTRA_BEFORE *
1709 (edge_emu_stride + (1 << s->ps.sps->pixel_shift));
1711 s->vdsp.emulated_edge_mc(lc->edge_emu_buffer2, src2 - offset1,
1712 edge_emu_stride, src2stride,
1713 block_w + EPEL_EXTRA, block_h + EPEL_EXTRA,
1714 x_off1 - EPEL_EXTRA_BEFORE,
1715 y_off1 - EPEL_EXTRA_BEFORE,
1716 pic_width, pic_height);
1718 src2 = lc->edge_emu_buffer2 + buf_offset1;
1719 src2stride = edge_emu_stride;
1722 s->hevcdsp.put_hevc_epel[idx][!!my0][!!mx0](lc->tmp, src1, src1stride,
1723 block_h, _mx0, _my0, block_w);
1725 s->hevcdsp.put_hevc_epel_bi[idx][!!my1][!!mx1](dst0, s->frame->linesize[cidx+1],
1726 src2, src2stride, lc->tmp,
1727 block_h, _mx1, _my1, block_w);
1729 s->hevcdsp.put_hevc_epel_bi_w[idx][!!my1][!!mx1](dst0, s->frame->linesize[cidx+1],
1730 src2, src2stride, lc->tmp,
1732 s->sh.chroma_log2_weight_denom,
1733 s->sh.chroma_weight_l0[current_mv->ref_idx[0]][cidx],
1734 s->sh.chroma_weight_l1[current_mv->ref_idx[1]][cidx],
1735 s->sh.chroma_offset_l0[current_mv->ref_idx[0]][cidx],
1736 s->sh.chroma_offset_l1[current_mv->ref_idx[1]][cidx],
1737 _mx1, _my1, block_w);
1740 static void hevc_await_progress(HEVCContext *s, HEVCFrame *ref,
1741 const Mv *mv, int y0, int height)
1743 if (s->threads_type == FF_THREAD_FRAME ) {
1744 int y = FFMAX(0, (mv->y >> 2) + y0 + height + 9);
1746 ff_thread_await_progress(&ref->tf, y, 0);
1750 static void hevc_luma_mv_mvp_mode(HEVCContext *s, int x0, int y0, int nPbW,
1751 int nPbH, int log2_cb_size, int part_idx,
1752 int merge_idx, MvField *mv)
1754 HEVCLocalContext *lc = s->HEVClc;
1755 enum InterPredIdc inter_pred_idc = PRED_L0;
1758 ff_hevc_set_neighbour_available(s, x0, y0, nPbW, nPbH);
1760 if (s->sh.slice_type == HEVC_SLICE_B)
1761 inter_pred_idc = ff_hevc_inter_pred_idc_decode(s, nPbW, nPbH);
1763 if (inter_pred_idc != PRED_L1) {
1764 if (s->sh.nb_refs[L0])
1765 mv->ref_idx[0]= ff_hevc_ref_idx_lx_decode(s, s->sh.nb_refs[L0]);
1767 mv->pred_flag = PF_L0;
1768 ff_hevc_hls_mvd_coding(s, x0, y0, 0);
1769 mvp_flag = ff_hevc_mvp_lx_flag_decode(s);
1770 ff_hevc_luma_mv_mvp_mode(s, x0, y0, nPbW, nPbH, log2_cb_size,
1771 part_idx, merge_idx, mv, mvp_flag, 0);
1772 mv->mv[0].x += lc->pu.mvd.x;
1773 mv->mv[0].y += lc->pu.mvd.y;
1776 if (inter_pred_idc != PRED_L0) {
1777 if (s->sh.nb_refs[L1])
1778 mv->ref_idx[1]= ff_hevc_ref_idx_lx_decode(s, s->sh.nb_refs[L1]);
1780 if (s->sh.mvd_l1_zero_flag == 1 && inter_pred_idc == PRED_BI) {
1781 AV_ZERO32(&lc->pu.mvd);
1783 ff_hevc_hls_mvd_coding(s, x0, y0, 1);
1786 mv->pred_flag += PF_L1;
1787 mvp_flag = ff_hevc_mvp_lx_flag_decode(s);
1788 ff_hevc_luma_mv_mvp_mode(s, x0, y0, nPbW, nPbH, log2_cb_size,
1789 part_idx, merge_idx, mv, mvp_flag, 1);
1790 mv->mv[1].x += lc->pu.mvd.x;
1791 mv->mv[1].y += lc->pu.mvd.y;
1795 static void hls_prediction_unit(HEVCContext *s, int x0, int y0,
1797 int log2_cb_size, int partIdx, int idx)
1799 #define POS(c_idx, x, y) \
1800 &s->frame->data[c_idx][((y) >> s->ps.sps->vshift[c_idx]) * s->frame->linesize[c_idx] + \
1801 (((x) >> s->ps.sps->hshift[c_idx]) << s->ps.sps->pixel_shift)]
1802 HEVCLocalContext *lc = s->HEVClc;
1804 struct MvField current_mv = {{{ 0 }}};
1806 int min_pu_width = s->ps.sps->min_pu_width;
1808 MvField *tab_mvf = s->ref->tab_mvf;
1809 RefPicList *refPicList = s->ref->refPicList;
1810 HEVCFrame *ref0 = NULL, *ref1 = NULL;
1811 uint8_t *dst0 = POS(0, x0, y0);
1812 uint8_t *dst1 = POS(1, x0, y0);
1813 uint8_t *dst2 = POS(2, x0, y0);
1814 int log2_min_cb_size = s->ps.sps->log2_min_cb_size;
1815 int min_cb_width = s->ps.sps->min_cb_width;
1816 int x_cb = x0 >> log2_min_cb_size;
1817 int y_cb = y0 >> log2_min_cb_size;
1821 int skip_flag = SAMPLE_CTB(s->skip_flag, x_cb, y_cb);
1824 lc->pu.merge_flag = ff_hevc_merge_flag_decode(s);
1826 if (skip_flag || lc->pu.merge_flag) {
1827 if (s->sh.max_num_merge_cand > 1)
1828 merge_idx = ff_hevc_merge_idx_decode(s);
1832 ff_hevc_luma_mv_merge_mode(s, x0, y0, nPbW, nPbH, log2_cb_size,
1833 partIdx, merge_idx, ¤t_mv);
1835 hevc_luma_mv_mvp_mode(s, x0, y0, nPbW, nPbH, log2_cb_size,
1836 partIdx, merge_idx, ¤t_mv);
1839 x_pu = x0 >> s->ps.sps->log2_min_pu_size;
1840 y_pu = y0 >> s->ps.sps->log2_min_pu_size;
1842 for (j = 0; j < nPbH >> s->ps.sps->log2_min_pu_size; j++)
1843 for (i = 0; i < nPbW >> s->ps.sps->log2_min_pu_size; i++)
1844 tab_mvf[(y_pu + j) * min_pu_width + x_pu + i] = current_mv;
1846 if (current_mv.pred_flag & PF_L0) {
1847 ref0 = refPicList[0].ref[current_mv.ref_idx[0]];
1850 hevc_await_progress(s, ref0, ¤t_mv.mv[0], y0, nPbH);
1852 if (current_mv.pred_flag & PF_L1) {
1853 ref1 = refPicList[1].ref[current_mv.ref_idx[1]];
1856 hevc_await_progress(s, ref1, ¤t_mv.mv[1], y0, nPbH);
1859 if (current_mv.pred_flag == PF_L0) {
1860 int x0_c = x0 >> s->ps.sps->hshift[1];
1861 int y0_c = y0 >> s->ps.sps->vshift[1];
1862 int nPbW_c = nPbW >> s->ps.sps->hshift[1];
1863 int nPbH_c = nPbH >> s->ps.sps->vshift[1];
1865 luma_mc_uni(s, dst0, s->frame->linesize[0], ref0->frame,
1866 ¤t_mv.mv[0], x0, y0, nPbW, nPbH,
1867 s->sh.luma_weight_l0[current_mv.ref_idx[0]],
1868 s->sh.luma_offset_l0[current_mv.ref_idx[0]]);
1870 if (s->ps.sps->chroma_format_idc) {
1871 chroma_mc_uni(s, dst1, s->frame->linesize[1], ref0->frame->data[1], ref0->frame->linesize[1],
1872 0, x0_c, y0_c, nPbW_c, nPbH_c, ¤t_mv,
1873 s->sh.chroma_weight_l0[current_mv.ref_idx[0]][0], s->sh.chroma_offset_l0[current_mv.ref_idx[0]][0]);
1874 chroma_mc_uni(s, dst2, s->frame->linesize[2], ref0->frame->data[2], ref0->frame->linesize[2],
1875 0, x0_c, y0_c, nPbW_c, nPbH_c, ¤t_mv,
1876 s->sh.chroma_weight_l0[current_mv.ref_idx[0]][1], s->sh.chroma_offset_l0[current_mv.ref_idx[0]][1]);
1878 } else if (current_mv.pred_flag == PF_L1) {
1879 int x0_c = x0 >> s->ps.sps->hshift[1];
1880 int y0_c = y0 >> s->ps.sps->vshift[1];
1881 int nPbW_c = nPbW >> s->ps.sps->hshift[1];
1882 int nPbH_c = nPbH >> s->ps.sps->vshift[1];
1884 luma_mc_uni(s, dst0, s->frame->linesize[0], ref1->frame,
1885 ¤t_mv.mv[1], x0, y0, nPbW, nPbH,
1886 s->sh.luma_weight_l1[current_mv.ref_idx[1]],
1887 s->sh.luma_offset_l1[current_mv.ref_idx[1]]);
1889 if (s->ps.sps->chroma_format_idc) {
1890 chroma_mc_uni(s, dst1, s->frame->linesize[1], ref1->frame->data[1], ref1->frame->linesize[1],
1891 1, x0_c, y0_c, nPbW_c, nPbH_c, ¤t_mv,
1892 s->sh.chroma_weight_l1[current_mv.ref_idx[1]][0], s->sh.chroma_offset_l1[current_mv.ref_idx[1]][0]);
1894 chroma_mc_uni(s, dst2, s->frame->linesize[2], ref1->frame->data[2], ref1->frame->linesize[2],
1895 1, x0_c, y0_c, nPbW_c, nPbH_c, ¤t_mv,
1896 s->sh.chroma_weight_l1[current_mv.ref_idx[1]][1], s->sh.chroma_offset_l1[current_mv.ref_idx[1]][1]);
1898 } else if (current_mv.pred_flag == PF_BI) {
1899 int x0_c = x0 >> s->ps.sps->hshift[1];
1900 int y0_c = y0 >> s->ps.sps->vshift[1];
1901 int nPbW_c = nPbW >> s->ps.sps->hshift[1];
1902 int nPbH_c = nPbH >> s->ps.sps->vshift[1];
1904 luma_mc_bi(s, dst0, s->frame->linesize[0], ref0->frame,
1905 ¤t_mv.mv[0], x0, y0, nPbW, nPbH,
1906 ref1->frame, ¤t_mv.mv[1], ¤t_mv);
1908 if (s->ps.sps->chroma_format_idc) {
1909 chroma_mc_bi(s, dst1, s->frame->linesize[1], ref0->frame, ref1->frame,
1910 x0_c, y0_c, nPbW_c, nPbH_c, ¤t_mv, 0);
1912 chroma_mc_bi(s, dst2, s->frame->linesize[2], ref0->frame, ref1->frame,
1913 x0_c, y0_c, nPbW_c, nPbH_c, ¤t_mv, 1);
1921 static int luma_intra_pred_mode(HEVCContext *s, int x0, int y0, int pu_size,
1922 int prev_intra_luma_pred_flag)
1924 HEVCLocalContext *lc = s->HEVClc;
1925 int x_pu = x0 >> s->ps.sps->log2_min_pu_size;
1926 int y_pu = y0 >> s->ps.sps->log2_min_pu_size;
1927 int min_pu_width = s->ps.sps->min_pu_width;
1928 int size_in_pus = pu_size >> s->ps.sps->log2_min_pu_size;
1929 int x0b = av_mod_uintp2(x0, s->ps.sps->log2_ctb_size);
1930 int y0b = av_mod_uintp2(y0, s->ps.sps->log2_ctb_size);
1932 int cand_up = (lc->ctb_up_flag || y0b) ?
1933 s->tab_ipm[(y_pu - 1) * min_pu_width + x_pu] : INTRA_DC;
1934 int cand_left = (lc->ctb_left_flag || x0b) ?
1935 s->tab_ipm[y_pu * min_pu_width + x_pu - 1] : INTRA_DC;
1937 int y_ctb = (y0 >> (s->ps.sps->log2_ctb_size)) << (s->ps.sps->log2_ctb_size);
1939 MvField *tab_mvf = s->ref->tab_mvf;
1940 int intra_pred_mode;
1944 // intra_pred_mode prediction does not cross vertical CTB boundaries
1945 if ((y0 - 1) < y_ctb)
1948 if (cand_left == cand_up) {
1949 if (cand_left < 2) {
1950 candidate[0] = INTRA_PLANAR;
1951 candidate[1] = INTRA_DC;
1952 candidate[2] = INTRA_ANGULAR_26;
1954 candidate[0] = cand_left;
1955 candidate[1] = 2 + ((cand_left - 2 - 1 + 32) & 31);
1956 candidate[2] = 2 + ((cand_left - 2 + 1) & 31);
1959 candidate[0] = cand_left;
1960 candidate[1] = cand_up;
1961 if (candidate[0] != INTRA_PLANAR && candidate[1] != INTRA_PLANAR) {
1962 candidate[2] = INTRA_PLANAR;
1963 } else if (candidate[0] != INTRA_DC && candidate[1] != INTRA_DC) {
1964 candidate[2] = INTRA_DC;
1966 candidate[2] = INTRA_ANGULAR_26;
1970 if (prev_intra_luma_pred_flag) {
1971 intra_pred_mode = candidate[lc->pu.mpm_idx];
1973 if (candidate[0] > candidate[1])
1974 FFSWAP(uint8_t, candidate[0], candidate[1]);
1975 if (candidate[0] > candidate[2])
1976 FFSWAP(uint8_t, candidate[0], candidate[2]);
1977 if (candidate[1] > candidate[2])
1978 FFSWAP(uint8_t, candidate[1], candidate[2]);
1980 intra_pred_mode = lc->pu.rem_intra_luma_pred_mode;
1981 for (i = 0; i < 3; i++)
1982 if (intra_pred_mode >= candidate[i])
1986 /* write the intra prediction units into the mv array */
1989 for (i = 0; i < size_in_pus; i++) {
1990 memset(&s->tab_ipm[(y_pu + i) * min_pu_width + x_pu],
1991 intra_pred_mode, size_in_pus);
1993 for (j = 0; j < size_in_pus; j++) {
1994 tab_mvf[(y_pu + j) * min_pu_width + x_pu + i].pred_flag = PF_INTRA;
1998 return intra_pred_mode;
2001 static av_always_inline void set_ct_depth(HEVCContext *s, int x0, int y0,
2002 int log2_cb_size, int ct_depth)
2004 int length = (1 << log2_cb_size) >> s->ps.sps->log2_min_cb_size;
2005 int x_cb = x0 >> s->ps.sps->log2_min_cb_size;
2006 int y_cb = y0 >> s->ps.sps->log2_min_cb_size;
2009 for (y = 0; y < length; y++)
2010 memset(&s->tab_ct_depth[(y_cb + y) * s->ps.sps->min_cb_width + x_cb],
2014 static const uint8_t tab_mode_idx[] = {
2015 0, 1, 2, 2, 2, 2, 3, 5, 7, 8, 10, 12, 13, 15, 17, 18, 19, 20,
2016 21, 22, 23, 23, 24, 24, 25, 25, 26, 27, 27, 28, 28, 29, 29, 30, 31};
2018 static void intra_prediction_unit(HEVCContext *s, int x0, int y0,
2021 HEVCLocalContext *lc = s->HEVClc;
2022 static const uint8_t intra_chroma_table[4] = { 0, 26, 10, 1 };
2023 uint8_t prev_intra_luma_pred_flag[4];
2024 int split = lc->cu.part_mode == PART_NxN;
2025 int pb_size = (1 << log2_cb_size) >> split;
2026 int side = split + 1;
2030 for (i = 0; i < side; i++)
2031 for (j = 0; j < side; j++)
2032 prev_intra_luma_pred_flag[2 * i + j] = ff_hevc_prev_intra_luma_pred_flag_decode(s);
2034 for (i = 0; i < side; i++) {
2035 for (j = 0; j < side; j++) {
2036 if (prev_intra_luma_pred_flag[2 * i + j])
2037 lc->pu.mpm_idx = ff_hevc_mpm_idx_decode(s);
2039 lc->pu.rem_intra_luma_pred_mode = ff_hevc_rem_intra_luma_pred_mode_decode(s);
2041 lc->pu.intra_pred_mode[2 * i + j] =
2042 luma_intra_pred_mode(s, x0 + pb_size * j, y0 + pb_size * i, pb_size,
2043 prev_intra_luma_pred_flag[2 * i + j]);
2047 if (s->ps.sps->chroma_format_idc == 3) {
2048 for (i = 0; i < side; i++) {
2049 for (j = 0; j < side; j++) {
2050 lc->pu.chroma_mode_c[2 * i + j] = chroma_mode = ff_hevc_intra_chroma_pred_mode_decode(s);
2051 if (chroma_mode != 4) {
2052 if (lc->pu.intra_pred_mode[2 * i + j] == intra_chroma_table[chroma_mode])
2053 lc->pu.intra_pred_mode_c[2 * i + j] = 34;
2055 lc->pu.intra_pred_mode_c[2 * i + j] = intra_chroma_table[chroma_mode];
2057 lc->pu.intra_pred_mode_c[2 * i + j] = lc->pu.intra_pred_mode[2 * i + j];
2061 } else if (s->ps.sps->chroma_format_idc == 2) {
2063 lc->pu.chroma_mode_c[0] = chroma_mode = ff_hevc_intra_chroma_pred_mode_decode(s);
2064 if (chroma_mode != 4) {
2065 if (lc->pu.intra_pred_mode[0] == intra_chroma_table[chroma_mode])
2068 mode_idx = intra_chroma_table[chroma_mode];
2070 mode_idx = lc->pu.intra_pred_mode[0];
2072 lc->pu.intra_pred_mode_c[0] = tab_mode_idx[mode_idx];
2073 } else if (s->ps.sps->chroma_format_idc != 0) {
2074 chroma_mode = ff_hevc_intra_chroma_pred_mode_decode(s);
2075 if (chroma_mode != 4) {
2076 if (lc->pu.intra_pred_mode[0] == intra_chroma_table[chroma_mode])
2077 lc->pu.intra_pred_mode_c[0] = 34;
2079 lc->pu.intra_pred_mode_c[0] = intra_chroma_table[chroma_mode];
2081 lc->pu.intra_pred_mode_c[0] = lc->pu.intra_pred_mode[0];
2086 static void intra_prediction_unit_default_value(HEVCContext *s,
2090 HEVCLocalContext *lc = s->HEVClc;
2091 int pb_size = 1 << log2_cb_size;
2092 int size_in_pus = pb_size >> s->ps.sps->log2_min_pu_size;
2093 int min_pu_width = s->ps.sps->min_pu_width;
2094 MvField *tab_mvf = s->ref->tab_mvf;
2095 int x_pu = x0 >> s->ps.sps->log2_min_pu_size;
2096 int y_pu = y0 >> s->ps.sps->log2_min_pu_size;
2099 if (size_in_pus == 0)
2101 for (j = 0; j < size_in_pus; j++)
2102 memset(&s->tab_ipm[(y_pu + j) * min_pu_width + x_pu], INTRA_DC, size_in_pus);
2103 if (lc->cu.pred_mode == MODE_INTRA)
2104 for (j = 0; j < size_in_pus; j++)
2105 for (k = 0; k < size_in_pus; k++)
2106 tab_mvf[(y_pu + j) * min_pu_width + x_pu + k].pred_flag = PF_INTRA;
2109 static int hls_coding_unit(HEVCContext *s, int x0, int y0, int log2_cb_size)
2111 int cb_size = 1 << log2_cb_size;
2112 HEVCLocalContext *lc = s->HEVClc;
2113 int log2_min_cb_size = s->ps.sps->log2_min_cb_size;
2114 int length = cb_size >> log2_min_cb_size;
2115 int min_cb_width = s->ps.sps->min_cb_width;
2116 int x_cb = x0 >> log2_min_cb_size;
2117 int y_cb = y0 >> log2_min_cb_size;
2118 int idx = log2_cb_size - 2;
2119 int qp_block_mask = (1<<(s->ps.sps->log2_ctb_size - s->ps.pps->diff_cu_qp_delta_depth)) - 1;
2124 lc->cu.pred_mode = MODE_INTRA;
2125 lc->cu.part_mode = PART_2Nx2N;
2126 lc->cu.intra_split_flag = 0;
2128 SAMPLE_CTB(s->skip_flag, x_cb, y_cb) = 0;
2129 for (x = 0; x < 4; x++)
2130 lc->pu.intra_pred_mode[x] = 1;
2131 if (s->ps.pps->transquant_bypass_enable_flag) {
2132 lc->cu.cu_transquant_bypass_flag = ff_hevc_cu_transquant_bypass_flag_decode(s);
2133 if (lc->cu.cu_transquant_bypass_flag)
2134 set_deblocking_bypass(s, x0, y0, log2_cb_size);
2136 lc->cu.cu_transquant_bypass_flag = 0;
2138 if (s->sh.slice_type != HEVC_SLICE_I) {
2139 uint8_t skip_flag = ff_hevc_skip_flag_decode(s, x0, y0, x_cb, y_cb);
2141 x = y_cb * min_cb_width + x_cb;
2142 for (y = 0; y < length; y++) {
2143 memset(&s->skip_flag[x], skip_flag, length);
2146 lc->cu.pred_mode = skip_flag ? MODE_SKIP : MODE_INTER;
2148 x = y_cb * min_cb_width + x_cb;
2149 for (y = 0; y < length; y++) {
2150 memset(&s->skip_flag[x], 0, length);
2155 if (SAMPLE_CTB(s->skip_flag, x_cb, y_cb)) {
2156 hls_prediction_unit(s, x0, y0, cb_size, cb_size, log2_cb_size, 0, idx);
2157 intra_prediction_unit_default_value(s, x0, y0, log2_cb_size);
2159 if (!s->sh.disable_deblocking_filter_flag)
2160 ff_hevc_deblocking_boundary_strengths(s, x0, y0, log2_cb_size);
2164 if (s->sh.slice_type != HEVC_SLICE_I)
2165 lc->cu.pred_mode = ff_hevc_pred_mode_decode(s);
2166 if (lc->cu.pred_mode != MODE_INTRA ||
2167 log2_cb_size == s->ps.sps->log2_min_cb_size) {
2168 lc->cu.part_mode = ff_hevc_part_mode_decode(s, log2_cb_size);
2169 lc->cu.intra_split_flag = lc->cu.part_mode == PART_NxN &&
2170 lc->cu.pred_mode == MODE_INTRA;
2173 if (lc->cu.pred_mode == MODE_INTRA) {
2174 if (lc->cu.part_mode == PART_2Nx2N && s->ps.sps->pcm_enabled_flag &&
2175 log2_cb_size >= s->ps.sps->pcm.log2_min_pcm_cb_size &&
2176 log2_cb_size <= s->ps.sps->pcm.log2_max_pcm_cb_size) {
2177 pcm_flag = ff_hevc_pcm_flag_decode(s);
2180 intra_prediction_unit_default_value(s, x0, y0, log2_cb_size);
2181 ret = hls_pcm_sample(s, x0, y0, log2_cb_size);
2182 if (s->ps.sps->pcm.loop_filter_disable_flag)
2183 set_deblocking_bypass(s, x0, y0, log2_cb_size);
2188 intra_prediction_unit(s, x0, y0, log2_cb_size);
2191 intra_prediction_unit_default_value(s, x0, y0, log2_cb_size);
2192 switch (lc->cu.part_mode) {
2194 hls_prediction_unit(s, x0, y0, cb_size, cb_size, log2_cb_size, 0, idx);
2197 hls_prediction_unit(s, x0, y0, cb_size, cb_size / 2, log2_cb_size, 0, idx);
2198 hls_prediction_unit(s, x0, y0 + cb_size / 2, cb_size, cb_size / 2, log2_cb_size, 1, idx);
2201 hls_prediction_unit(s, x0, y0, cb_size / 2, cb_size, log2_cb_size, 0, idx - 1);
2202 hls_prediction_unit(s, x0 + cb_size / 2, y0, cb_size / 2, cb_size, log2_cb_size, 1, idx - 1);
2205 hls_prediction_unit(s, x0, y0, cb_size, cb_size / 4, log2_cb_size, 0, idx);
2206 hls_prediction_unit(s, x0, y0 + cb_size / 4, cb_size, cb_size * 3 / 4, log2_cb_size, 1, idx);
2209 hls_prediction_unit(s, x0, y0, cb_size, cb_size * 3 / 4, log2_cb_size, 0, idx);
2210 hls_prediction_unit(s, x0, y0 + cb_size * 3 / 4, cb_size, cb_size / 4, log2_cb_size, 1, idx);
2213 hls_prediction_unit(s, x0, y0, cb_size / 4, cb_size, log2_cb_size, 0, idx - 2);
2214 hls_prediction_unit(s, x0 + cb_size / 4, y0, cb_size * 3 / 4, cb_size, log2_cb_size, 1, idx - 2);
2217 hls_prediction_unit(s, x0, y0, cb_size * 3 / 4, cb_size, log2_cb_size, 0, idx - 2);
2218 hls_prediction_unit(s, x0 + cb_size * 3 / 4, y0, cb_size / 4, cb_size, log2_cb_size, 1, idx - 2);
2221 hls_prediction_unit(s, x0, y0, cb_size / 2, cb_size / 2, log2_cb_size, 0, idx - 1);
2222 hls_prediction_unit(s, x0 + cb_size / 2, y0, cb_size / 2, cb_size / 2, log2_cb_size, 1, idx - 1);
2223 hls_prediction_unit(s, x0, y0 + cb_size / 2, cb_size / 2, cb_size / 2, log2_cb_size, 2, idx - 1);
2224 hls_prediction_unit(s, x0 + cb_size / 2, y0 + cb_size / 2, cb_size / 2, cb_size / 2, log2_cb_size, 3, idx - 1);
2230 int rqt_root_cbf = 1;
2232 if (lc->cu.pred_mode != MODE_INTRA &&
2233 !(lc->cu.part_mode == PART_2Nx2N && lc->pu.merge_flag)) {
2234 rqt_root_cbf = ff_hevc_no_residual_syntax_flag_decode(s);
2237 const static int cbf[2] = { 0 };
2238 lc->cu.max_trafo_depth = lc->cu.pred_mode == MODE_INTRA ?
2239 s->ps.sps->max_transform_hierarchy_depth_intra + lc->cu.intra_split_flag :
2240 s->ps.sps->max_transform_hierarchy_depth_inter;
2241 ret = hls_transform_tree(s, x0, y0, x0, y0, x0, y0,
2243 log2_cb_size, 0, 0, cbf, cbf);
2247 if (!s->sh.disable_deblocking_filter_flag)
2248 ff_hevc_deblocking_boundary_strengths(s, x0, y0, log2_cb_size);
2253 if (s->ps.pps->cu_qp_delta_enabled_flag && lc->tu.is_cu_qp_delta_coded == 0)
2254 ff_hevc_set_qPy(s, x0, y0, log2_cb_size);
2256 x = y_cb * min_cb_width + x_cb;
2257 for (y = 0; y < length; y++) {
2258 memset(&s->qp_y_tab[x], lc->qp_y, length);
2262 if(((x0 + (1<<log2_cb_size)) & qp_block_mask) == 0 &&
2263 ((y0 + (1<<log2_cb_size)) & qp_block_mask) == 0) {
2264 lc->qPy_pred = lc->qp_y;
2267 set_ct_depth(s, x0, y0, log2_cb_size, lc->ct_depth);
2272 static int hls_coding_quadtree(HEVCContext *s, int x0, int y0,
2273 int log2_cb_size, int cb_depth)
2275 HEVCLocalContext *lc = s->HEVClc;
2276 const int cb_size = 1 << log2_cb_size;
2280 lc->ct_depth = cb_depth;
2281 if (x0 + cb_size <= s->ps.sps->width &&
2282 y0 + cb_size <= s->ps.sps->height &&
2283 log2_cb_size > s->ps.sps->log2_min_cb_size) {
2284 split_cu = ff_hevc_split_coding_unit_flag_decode(s, cb_depth, x0, y0);
2286 split_cu = (log2_cb_size > s->ps.sps->log2_min_cb_size);
2288 if (s->ps.pps->cu_qp_delta_enabled_flag &&
2289 log2_cb_size >= s->ps.sps->log2_ctb_size - s->ps.pps->diff_cu_qp_delta_depth) {
2290 lc->tu.is_cu_qp_delta_coded = 0;
2291 lc->tu.cu_qp_delta = 0;
2294 if (s->sh.cu_chroma_qp_offset_enabled_flag &&
2295 log2_cb_size >= s->ps.sps->log2_ctb_size - s->ps.pps->diff_cu_chroma_qp_offset_depth) {
2296 lc->tu.is_cu_chroma_qp_offset_coded = 0;
2300 int qp_block_mask = (1<<(s->ps.sps->log2_ctb_size - s->ps.pps->diff_cu_qp_delta_depth)) - 1;
2301 const int cb_size_split = cb_size >> 1;
2302 const int x1 = x0 + cb_size_split;
2303 const int y1 = y0 + cb_size_split;
2307 more_data = hls_coding_quadtree(s, x0, y0, log2_cb_size - 1, cb_depth + 1);
2311 if (more_data && x1 < s->ps.sps->width) {
2312 more_data = hls_coding_quadtree(s, x1, y0, log2_cb_size - 1, cb_depth + 1);
2316 if (more_data && y1 < s->ps.sps->height) {
2317 more_data = hls_coding_quadtree(s, x0, y1, log2_cb_size - 1, cb_depth + 1);
2321 if (more_data && x1 < s->ps.sps->width &&
2322 y1 < s->ps.sps->height) {
2323 more_data = hls_coding_quadtree(s, x1, y1, log2_cb_size - 1, cb_depth + 1);
2328 if(((x0 + (1<<log2_cb_size)) & qp_block_mask) == 0 &&
2329 ((y0 + (1<<log2_cb_size)) & qp_block_mask) == 0)
2330 lc->qPy_pred = lc->qp_y;
2333 return ((x1 + cb_size_split) < s->ps.sps->width ||
2334 (y1 + cb_size_split) < s->ps.sps->height);
2338 ret = hls_coding_unit(s, x0, y0, log2_cb_size);
2341 if ((!((x0 + cb_size) %
2342 (1 << (s->ps.sps->log2_ctb_size))) ||
2343 (x0 + cb_size >= s->ps.sps->width)) &&
2345 (1 << (s->ps.sps->log2_ctb_size))) ||
2346 (y0 + cb_size >= s->ps.sps->height))) {
2347 int end_of_slice_flag = ff_hevc_end_of_slice_flag_decode(s);
2348 return !end_of_slice_flag;
2357 static void hls_decode_neighbour(HEVCContext *s, int x_ctb, int y_ctb,
2360 HEVCLocalContext *lc = s->HEVClc;
2361 int ctb_size = 1 << s->ps.sps->log2_ctb_size;
2362 int ctb_addr_rs = s->ps.pps->ctb_addr_ts_to_rs[ctb_addr_ts];
2363 int ctb_addr_in_slice = ctb_addr_rs - s->sh.slice_addr;
2365 s->tab_slice_address[ctb_addr_rs] = s->sh.slice_addr;
2367 if (s->ps.pps->entropy_coding_sync_enabled_flag) {
2368 if (x_ctb == 0 && (y_ctb & (ctb_size - 1)) == 0)
2369 lc->first_qp_group = 1;
2370 lc->end_of_tiles_x = s->ps.sps->width;
2371 } else if (s->ps.pps->tiles_enabled_flag) {
2372 if (ctb_addr_ts && s->ps.pps->tile_id[ctb_addr_ts] != s->ps.pps->tile_id[ctb_addr_ts - 1]) {
2373 int idxX = s->ps.pps->col_idxX[x_ctb >> s->ps.sps->log2_ctb_size];
2374 lc->end_of_tiles_x = x_ctb + (s->ps.pps->column_width[idxX] << s->ps.sps->log2_ctb_size);
2375 lc->first_qp_group = 1;
2378 lc->end_of_tiles_x = s->ps.sps->width;
2381 lc->end_of_tiles_y = FFMIN(y_ctb + ctb_size, s->ps.sps->height);
2383 lc->boundary_flags = 0;
2384 if (s->ps.pps->tiles_enabled_flag) {
2385 if (x_ctb > 0 && s->ps.pps->tile_id[ctb_addr_ts] != s->ps.pps->tile_id[s->ps.pps->ctb_addr_rs_to_ts[ctb_addr_rs - 1]])
2386 lc->boundary_flags |= BOUNDARY_LEFT_TILE;
2387 if (x_ctb > 0 && s->tab_slice_address[ctb_addr_rs] != s->tab_slice_address[ctb_addr_rs - 1])
2388 lc->boundary_flags |= BOUNDARY_LEFT_SLICE;
2389 if (y_ctb > 0 && s->ps.pps->tile_id[ctb_addr_ts] != s->ps.pps->tile_id[s->ps.pps->ctb_addr_rs_to_ts[ctb_addr_rs - s->ps.sps->ctb_width]])
2390 lc->boundary_flags |= BOUNDARY_UPPER_TILE;
2391 if (y_ctb > 0 && s->tab_slice_address[ctb_addr_rs] != s->tab_slice_address[ctb_addr_rs - s->ps.sps->ctb_width])
2392 lc->boundary_flags |= BOUNDARY_UPPER_SLICE;
2394 if (ctb_addr_in_slice <= 0)
2395 lc->boundary_flags |= BOUNDARY_LEFT_SLICE;
2396 if (ctb_addr_in_slice < s->ps.sps->ctb_width)
2397 lc->boundary_flags |= BOUNDARY_UPPER_SLICE;
2400 lc->ctb_left_flag = ((x_ctb > 0) && (ctb_addr_in_slice > 0) && !(lc->boundary_flags & BOUNDARY_LEFT_TILE));
2401 lc->ctb_up_flag = ((y_ctb > 0) && (ctb_addr_in_slice >= s->ps.sps->ctb_width) && !(lc->boundary_flags & BOUNDARY_UPPER_TILE));
2402 lc->ctb_up_right_flag = ((y_ctb > 0) && (ctb_addr_in_slice+1 >= s->ps.sps->ctb_width) && (s->ps.pps->tile_id[ctb_addr_ts] == s->ps.pps->tile_id[s->ps.pps->ctb_addr_rs_to_ts[ctb_addr_rs+1 - s->ps.sps->ctb_width]]));
2403 lc->ctb_up_left_flag = ((x_ctb > 0) && (y_ctb > 0) && (ctb_addr_in_slice-1 >= s->ps.sps->ctb_width) && (s->ps.pps->tile_id[ctb_addr_ts] == s->ps.pps->tile_id[s->ps.pps->ctb_addr_rs_to_ts[ctb_addr_rs-1 - s->ps.sps->ctb_width]]));
2406 static int hls_decode_entry(AVCodecContext *avctxt, void *isFilterThread)
2408 HEVCContext *s = avctxt->priv_data;
2409 int ctb_size = 1 << s->ps.sps->log2_ctb_size;
2413 int ctb_addr_ts = s->ps.pps->ctb_addr_rs_to_ts[s->sh.slice_ctb_addr_rs];
2416 if (!ctb_addr_ts && s->sh.dependent_slice_segment_flag) {
2417 av_log(s->avctx, AV_LOG_ERROR, "Impossible initial tile.\n");
2418 return AVERROR_INVALIDDATA;
2421 if (s->sh.dependent_slice_segment_flag) {
2422 int prev_rs = s->ps.pps->ctb_addr_ts_to_rs[ctb_addr_ts - 1];
2423 if (s->tab_slice_address[prev_rs] != s->sh.slice_addr) {
2424 av_log(s->avctx, AV_LOG_ERROR, "Previous slice segment missing\n");
2425 return AVERROR_INVALIDDATA;
2429 while (more_data && ctb_addr_ts < s->ps.sps->ctb_size) {
2430 int ctb_addr_rs = s->ps.pps->ctb_addr_ts_to_rs[ctb_addr_ts];
2432 x_ctb = (ctb_addr_rs % ((s->ps.sps->width + ctb_size - 1) >> s->ps.sps->log2_ctb_size)) << s->ps.sps->log2_ctb_size;
2433 y_ctb = (ctb_addr_rs / ((s->ps.sps->width + ctb_size - 1) >> s->ps.sps->log2_ctb_size)) << s->ps.sps->log2_ctb_size;
2434 hls_decode_neighbour(s, x_ctb, y_ctb, ctb_addr_ts);
2436 ret = ff_hevc_cabac_init(s, ctb_addr_ts);
2438 s->tab_slice_address[ctb_addr_rs] = -1;
2442 hls_sao_param(s, x_ctb >> s->ps.sps->log2_ctb_size, y_ctb >> s->ps.sps->log2_ctb_size);
2444 s->deblock[ctb_addr_rs].beta_offset = s->sh.beta_offset;
2445 s->deblock[ctb_addr_rs].tc_offset = s->sh.tc_offset;
2446 s->filter_slice_edges[ctb_addr_rs] = s->sh.slice_loop_filter_across_slices_enabled_flag;
2448 more_data = hls_coding_quadtree(s, x_ctb, y_ctb, s->ps.sps->log2_ctb_size, 0);
2449 if (more_data < 0) {
2450 s->tab_slice_address[ctb_addr_rs] = -1;
2456 ff_hevc_save_states(s, ctb_addr_ts);
2457 ff_hevc_hls_filters(s, x_ctb, y_ctb, ctb_size);
2460 if (x_ctb + ctb_size >= s->ps.sps->width &&
2461 y_ctb + ctb_size >= s->ps.sps->height)
2462 ff_hevc_hls_filter(s, x_ctb, y_ctb, ctb_size);
2467 static int hls_slice_data(HEVCContext *s)
2475 s->avctx->execute(s->avctx, hls_decode_entry, arg, ret , 1, sizeof(int));
2478 static int hls_decode_entry_wpp(AVCodecContext *avctxt, void *input_ctb_row, int job, int self_id)
2480 HEVCContext *s1 = avctxt->priv_data, *s;
2481 HEVCLocalContext *lc;
2482 int ctb_size = 1<< s1->ps.sps->log2_ctb_size;
2484 int *ctb_row_p = input_ctb_row;
2485 int ctb_row = ctb_row_p[job];
2486 int ctb_addr_rs = s1->sh.slice_ctb_addr_rs + ctb_row * ((s1->ps.sps->width + ctb_size - 1) >> s1->ps.sps->log2_ctb_size);
2487 int ctb_addr_ts = s1->ps.pps->ctb_addr_rs_to_ts[ctb_addr_rs];
2488 int thread = ctb_row % s1->threads_number;
2491 s = s1->sList[self_id];
2495 ret = init_get_bits8(&lc->gb, s->data + s->sh.offset[ctb_row - 1], s->sh.size[ctb_row - 1]);
2498 ff_init_cabac_decoder(&lc->cc, s->data + s->sh.offset[(ctb_row)-1], s->sh.size[ctb_row - 1]);
2501 while(more_data && ctb_addr_ts < s->ps.sps->ctb_size) {
2502 int x_ctb = (ctb_addr_rs % s->ps.sps->ctb_width) << s->ps.sps->log2_ctb_size;
2503 int y_ctb = (ctb_addr_rs / s->ps.sps->ctb_width) << s->ps.sps->log2_ctb_size;
2505 hls_decode_neighbour(s, x_ctb, y_ctb, ctb_addr_ts);
2507 ff_thread_await_progress2(s->avctx, ctb_row, thread, SHIFT_CTB_WPP);
2509 if (atomic_load(&s1->wpp_err)) {
2510 ff_thread_report_progress2(s->avctx, ctb_row , thread, SHIFT_CTB_WPP);
2514 ret = ff_hevc_cabac_init(s, ctb_addr_ts);
2517 hls_sao_param(s, x_ctb >> s->ps.sps->log2_ctb_size, y_ctb >> s->ps.sps->log2_ctb_size);
2518 more_data = hls_coding_quadtree(s, x_ctb, y_ctb, s->ps.sps->log2_ctb_size, 0);
2520 if (more_data < 0) {
2527 ff_hevc_save_states(s, ctb_addr_ts);
2528 ff_thread_report_progress2(s->avctx, ctb_row, thread, 1);
2529 ff_hevc_hls_filters(s, x_ctb, y_ctb, ctb_size);
2531 if (!more_data && (x_ctb+ctb_size) < s->ps.sps->width && ctb_row != s->sh.num_entry_point_offsets) {
2532 atomic_store(&s1->wpp_err, 1);
2533 ff_thread_report_progress2(s->avctx, ctb_row ,thread, SHIFT_CTB_WPP);
2537 if ((x_ctb+ctb_size) >= s->ps.sps->width && (y_ctb+ctb_size) >= s->ps.sps->height ) {
2538 ff_hevc_hls_filter(s, x_ctb, y_ctb, ctb_size);
2539 ff_thread_report_progress2(s->avctx, ctb_row , thread, SHIFT_CTB_WPP);
2542 ctb_addr_rs = s->ps.pps->ctb_addr_ts_to_rs[ctb_addr_ts];
2545 if(x_ctb >= s->ps.sps->width) {
2549 ff_thread_report_progress2(s->avctx, ctb_row ,thread, SHIFT_CTB_WPP);
2553 s->tab_slice_address[ctb_addr_rs] = -1;
2554 atomic_store(&s1->wpp_err, 1);
2555 ff_thread_report_progress2(s->avctx, ctb_row ,thread, SHIFT_CTB_WPP);
2559 static int hls_slice_data_wpp(HEVCContext *s, const H2645NAL *nal)
2561 const uint8_t *data = nal->data;
2562 int length = nal->size;
2563 HEVCLocalContext *lc = s->HEVClc;
2564 int *ret = av_malloc_array(s->sh.num_entry_point_offsets + 1, sizeof(int));
2565 int *arg = av_malloc_array(s->sh.num_entry_point_offsets + 1, sizeof(int));
2567 int64_t startheader, cmpt = 0;
2573 return AVERROR(ENOMEM);
2576 if (s->sh.slice_ctb_addr_rs + s->sh.num_entry_point_offsets * s->ps.sps->ctb_width >= s->ps.sps->ctb_width * s->ps.sps->ctb_height) {
2577 av_log(s->avctx, AV_LOG_ERROR, "WPP ctb addresses are wrong (%d %d %d %d)\n",
2578 s->sh.slice_ctb_addr_rs, s->sh.num_entry_point_offsets,
2579 s->ps.sps->ctb_width, s->ps.sps->ctb_height
2581 res = AVERROR_INVALIDDATA;
2585 ff_alloc_entries(s->avctx, s->sh.num_entry_point_offsets + 1);
2588 for (i = 1; i < s->threads_number; i++) {
2589 s->sList[i] = av_malloc(sizeof(HEVCContext));
2590 memcpy(s->sList[i], s, sizeof(HEVCContext));
2591 s->HEVClcList[i] = av_mallocz(sizeof(HEVCLocalContext));
2592 s->sList[i]->HEVClc = s->HEVClcList[i];
2596 offset = (lc->gb.index >> 3);
2598 for (j = 0, cmpt = 0, startheader = offset + s->sh.entry_point_offset[0]; j < nal->skipped_bytes; j++) {
2599 if (nal->skipped_bytes_pos[j] >= offset && nal->skipped_bytes_pos[j] < startheader) {
2605 for (i = 1; i < s->sh.num_entry_point_offsets; i++) {
2606 offset += (s->sh.entry_point_offset[i - 1] - cmpt);
2607 for (j = 0, cmpt = 0, startheader = offset
2608 + s->sh.entry_point_offset[i]; j < nal->skipped_bytes; j++) {
2609 if (nal->skipped_bytes_pos[j] >= offset && nal->skipped_bytes_pos[j] < startheader) {
2614 s->sh.size[i - 1] = s->sh.entry_point_offset[i] - cmpt;
2615 s->sh.offset[i - 1] = offset;
2618 if (s->sh.num_entry_point_offsets != 0) {
2619 offset += s->sh.entry_point_offset[s->sh.num_entry_point_offsets - 1] - cmpt;
2620 if (length < offset) {
2621 av_log(s->avctx, AV_LOG_ERROR, "entry_point_offset table is corrupted\n");
2622 res = AVERROR_INVALIDDATA;
2625 s->sh.size[s->sh.num_entry_point_offsets - 1] = length - offset;
2626 s->sh.offset[s->sh.num_entry_point_offsets - 1] = offset;
2631 for (i = 1; i < s->threads_number; i++) {
2632 s->sList[i]->HEVClc->first_qp_group = 1;
2633 s->sList[i]->HEVClc->qp_y = s->sList[0]->HEVClc->qp_y;
2634 memcpy(s->sList[i], s, sizeof(HEVCContext));
2635 s->sList[i]->HEVClc = s->HEVClcList[i];
2638 atomic_store(&s->wpp_err, 0);
2639 ff_reset_entries(s->avctx);
2641 for (i = 0; i <= s->sh.num_entry_point_offsets; i++) {
2646 if (s->ps.pps->entropy_coding_sync_enabled_flag)
2647 s->avctx->execute2(s->avctx, hls_decode_entry_wpp, arg, ret, s->sh.num_entry_point_offsets + 1);
2649 for (i = 0; i <= s->sh.num_entry_point_offsets; i++)
2657 static int set_side_data(HEVCContext *s)
2659 AVFrame *out = s->ref->frame;
2661 if (s->sei.frame_packing.present &&
2662 s->sei.frame_packing.arrangement_type >= 3 &&
2663 s->sei.frame_packing.arrangement_type <= 5 &&
2664 s->sei.frame_packing.content_interpretation_type > 0 &&
2665 s->sei.frame_packing.content_interpretation_type < 3) {
2666 AVStereo3D *stereo = av_stereo3d_create_side_data(out);
2668 return AVERROR(ENOMEM);
2670 switch (s->sei.frame_packing.arrangement_type) {
2672 if (s->sei.frame_packing.quincunx_subsampling)
2673 stereo->type = AV_STEREO3D_SIDEBYSIDE_QUINCUNX;
2675 stereo->type = AV_STEREO3D_SIDEBYSIDE;
2678 stereo->type = AV_STEREO3D_TOPBOTTOM;
2681 stereo->type = AV_STEREO3D_FRAMESEQUENCE;
2685 if (s->sei.frame_packing.content_interpretation_type == 2)
2686 stereo->flags = AV_STEREO3D_FLAG_INVERT;
2688 if (s->sei.frame_packing.arrangement_type == 5) {
2689 if (s->sei.frame_packing.current_frame_is_frame0_flag)
2690 stereo->view = AV_STEREO3D_VIEW_LEFT;
2692 stereo->view = AV_STEREO3D_VIEW_RIGHT;
2696 if (s->sei.display_orientation.present &&
2697 (s->sei.display_orientation.anticlockwise_rotation ||
2698 s->sei.display_orientation.hflip || s->sei.display_orientation.vflip)) {
2699 double angle = s->sei.display_orientation.anticlockwise_rotation * 360 / (double) (1 << 16);
2700 AVFrameSideData *rotation = av_frame_new_side_data(out,
2701 AV_FRAME_DATA_DISPLAYMATRIX,
2702 sizeof(int32_t) * 9);
2704 return AVERROR(ENOMEM);
2706 av_display_rotation_set((int32_t *)rotation->data, angle);
2707 av_display_matrix_flip((int32_t *)rotation->data,
2708 s->sei.display_orientation.hflip,
2709 s->sei.display_orientation.vflip);
2712 // Decrement the mastering display flag when IRAP frame has no_rasl_output_flag=1
2713 // so the side data persists for the entire coded video sequence.
2714 if (s->sei.mastering_display.present > 0 &&
2715 IS_IRAP(s) && s->no_rasl_output_flag) {
2716 s->sei.mastering_display.present--;
2718 if (s->sei.mastering_display.present) {
2719 // HEVC uses a g,b,r ordering, which we convert to a more natural r,g,b
2720 const int mapping[3] = {2, 0, 1};
2721 const int chroma_den = 50000;
2722 const int luma_den = 10000;
2724 AVMasteringDisplayMetadata *metadata =
2725 av_mastering_display_metadata_create_side_data(out);
2727 return AVERROR(ENOMEM);
2729 for (i = 0; i < 3; i++) {
2730 const int j = mapping[i];
2731 metadata->display_primaries[i][0].num = s->sei.mastering_display.display_primaries[j][0];
2732 metadata->display_primaries[i][0].den = chroma_den;
2733 metadata->display_primaries[i][1].num = s->sei.mastering_display.display_primaries[j][1];
2734 metadata->display_primaries[i][1].den = chroma_den;
2736 metadata->white_point[0].num = s->sei.mastering_display.white_point[0];
2737 metadata->white_point[0].den = chroma_den;
2738 metadata->white_point[1].num = s->sei.mastering_display.white_point[1];
2739 metadata->white_point[1].den = chroma_den;
2741 metadata->max_luminance.num = s->sei.mastering_display.max_luminance;
2742 metadata->max_luminance.den = luma_den;
2743 metadata->min_luminance.num = s->sei.mastering_display.min_luminance;
2744 metadata->min_luminance.den = luma_den;
2745 metadata->has_luminance = 1;
2746 metadata->has_primaries = 1;
2748 av_log(s->avctx, AV_LOG_DEBUG, "Mastering Display Metadata:\n");
2749 av_log(s->avctx, AV_LOG_DEBUG,
2750 "r(%5.4f,%5.4f) g(%5.4f,%5.4f) b(%5.4f %5.4f) wp(%5.4f, %5.4f)\n",
2751 av_q2d(metadata->display_primaries[0][0]),
2752 av_q2d(metadata->display_primaries[0][1]),
2753 av_q2d(metadata->display_primaries[1][0]),
2754 av_q2d(metadata->display_primaries[1][1]),
2755 av_q2d(metadata->display_primaries[2][0]),
2756 av_q2d(metadata->display_primaries[2][1]),
2757 av_q2d(metadata->white_point[0]), av_q2d(metadata->white_point[1]));
2758 av_log(s->avctx, AV_LOG_DEBUG,
2759 "min_luminance=%f, max_luminance=%f\n",
2760 av_q2d(metadata->min_luminance), av_q2d(metadata->max_luminance));
2762 // Decrement the mastering display flag when IRAP frame has no_rasl_output_flag=1
2763 // so the side data persists for the entire coded video sequence.
2764 if (s->sei.content_light.present > 0 &&
2765 IS_IRAP(s) && s->no_rasl_output_flag) {
2766 s->sei.content_light.present--;
2768 if (s->sei.content_light.present) {
2769 AVContentLightMetadata *metadata =
2770 av_content_light_metadata_create_side_data(out);
2772 return AVERROR(ENOMEM);
2773 metadata->MaxCLL = s->sei.content_light.max_content_light_level;
2774 metadata->MaxFALL = s->sei.content_light.max_pic_average_light_level;
2776 av_log(s->avctx, AV_LOG_DEBUG, "Content Light Level Metadata:\n");
2777 av_log(s->avctx, AV_LOG_DEBUG, "MaxCLL=%d, MaxFALL=%d\n",
2778 metadata->MaxCLL, metadata->MaxFALL);
2781 if (s->sei.a53_caption.a53_caption) {
2782 AVFrameSideData* sd = av_frame_new_side_data(out,
2783 AV_FRAME_DATA_A53_CC,
2784 s->sei.a53_caption.a53_caption_size);
2786 memcpy(sd->data, s->sei.a53_caption.a53_caption, s->sei.a53_caption.a53_caption_size);
2787 av_freep(&s->sei.a53_caption.a53_caption);
2788 s->sei.a53_caption.a53_caption_size = 0;
2789 s->avctx->properties |= FF_CODEC_PROPERTY_CLOSED_CAPTIONS;
2795 static int hevc_frame_start(HEVCContext *s)
2797 HEVCLocalContext *lc = s->HEVClc;
2798 int pic_size_in_ctb = ((s->ps.sps->width >> s->ps.sps->log2_min_cb_size) + 1) *
2799 ((s->ps.sps->height >> s->ps.sps->log2_min_cb_size) + 1);
2802 memset(s->horizontal_bs, 0, s->bs_width * s->bs_height);
2803 memset(s->vertical_bs, 0, s->bs_width * s->bs_height);
2804 memset(s->cbf_luma, 0, s->ps.sps->min_tb_width * s->ps.sps->min_tb_height);
2805 memset(s->is_pcm, 0, (s->ps.sps->min_pu_width + 1) * (s->ps.sps->min_pu_height + 1));
2806 memset(s->tab_slice_address, -1, pic_size_in_ctb * sizeof(*s->tab_slice_address));
2809 s->first_nal_type = s->nal_unit_type;
2811 s->no_rasl_output_flag = IS_IDR(s) || IS_BLA(s) || (s->nal_unit_type == HEVC_NAL_CRA_NUT && s->last_eos);
2813 if (s->ps.pps->tiles_enabled_flag)
2814 lc->end_of_tiles_x = s->ps.pps->column_width[0] << s->ps.sps->log2_ctb_size;
2816 ret = ff_hevc_set_new_ref(s, &s->frame, s->poc);
2820 ret = ff_hevc_frame_rps(s);
2822 av_log(s->avctx, AV_LOG_ERROR, "Error constructing the frame RPS.\n");
2826 s->ref->frame->key_frame = IS_IRAP(s);
2828 ret = set_side_data(s);
2832 s->frame->pict_type = 3 - s->sh.slice_type;
2835 ff_hevc_bump_frame(s);
2837 av_frame_unref(s->output_frame);
2838 ret = ff_hevc_output_frame(s, s->output_frame, 0);
2842 if (!s->avctx->hwaccel)
2843 ff_thread_finish_setup(s->avctx);
2849 ff_hevc_unref_frame(s, s->ref, ~0);
2854 static int decode_nal_unit(HEVCContext *s, const H2645NAL *nal)
2856 HEVCLocalContext *lc = s->HEVClc;
2857 GetBitContext *gb = &lc->gb;
2858 int ctb_addr_ts, ret;
2861 s->nal_unit_type = nal->type;
2862 s->temporal_id = nal->temporal_id;
2864 switch (s->nal_unit_type) {
2866 if (s->avctx->hwaccel && s->avctx->hwaccel->decode_params) {
2867 ret = s->avctx->hwaccel->decode_params(s->avctx,
2874 ret = ff_hevc_decode_nal_vps(gb, s->avctx, &s->ps);
2879 if (s->avctx->hwaccel && s->avctx->hwaccel->decode_params) {
2880 ret = s->avctx->hwaccel->decode_params(s->avctx,
2887 ret = ff_hevc_decode_nal_sps(gb, s->avctx, &s->ps,
2888 s->apply_defdispwin);
2893 if (s->avctx->hwaccel && s->avctx->hwaccel->decode_params) {
2894 ret = s->avctx->hwaccel->decode_params(s->avctx,
2901 ret = ff_hevc_decode_nal_pps(gb, s->avctx, &s->ps);
2905 case HEVC_NAL_SEI_PREFIX:
2906 case HEVC_NAL_SEI_SUFFIX:
2907 if (s->avctx->hwaccel && s->avctx->hwaccel->decode_params) {
2908 ret = s->avctx->hwaccel->decode_params(s->avctx,
2915 ret = ff_hevc_decode_nal_sei(gb, s->avctx, &s->sei, &s->ps, s->nal_unit_type);
2919 case HEVC_NAL_TRAIL_R:
2920 case HEVC_NAL_TRAIL_N:
2921 case HEVC_NAL_TSA_N:
2922 case HEVC_NAL_TSA_R:
2923 case HEVC_NAL_STSA_N:
2924 case HEVC_NAL_STSA_R:
2925 case HEVC_NAL_BLA_W_LP:
2926 case HEVC_NAL_BLA_W_RADL:
2927 case HEVC_NAL_BLA_N_LP:
2928 case HEVC_NAL_IDR_W_RADL:
2929 case HEVC_NAL_IDR_N_LP:
2930 case HEVC_NAL_CRA_NUT:
2931 case HEVC_NAL_RADL_N:
2932 case HEVC_NAL_RADL_R:
2933 case HEVC_NAL_RASL_N:
2934 case HEVC_NAL_RASL_R:
2935 ret = hls_slice_header(s);
2939 ret = AVERROR_INVALIDDATA;
2945 (s->avctx->skip_frame >= AVDISCARD_BIDIR && s->sh.slice_type == HEVC_SLICE_B) ||
2946 (s->avctx->skip_frame >= AVDISCARD_NONINTRA && s->sh.slice_type != HEVC_SLICE_I) ||
2947 (s->avctx->skip_frame >= AVDISCARD_NONKEY && !IS_IRAP(s))) {
2951 if (s->sh.first_slice_in_pic_flag) {
2952 if (s->max_ra == INT_MAX) {
2953 if (s->nal_unit_type == HEVC_NAL_CRA_NUT || IS_BLA(s)) {
2957 s->max_ra = INT_MIN;
2961 if ((s->nal_unit_type == HEVC_NAL_RASL_R || s->nal_unit_type == HEVC_NAL_RASL_N) &&
2962 s->poc <= s->max_ra) {
2966 if (s->nal_unit_type == HEVC_NAL_RASL_R && s->poc > s->max_ra)
2967 s->max_ra = INT_MIN;
2971 ret = hevc_frame_start(s);
2974 } else if (!s->ref) {
2975 av_log(s->avctx, AV_LOG_ERROR, "First slice in a frame missing.\n");
2979 if (s->nal_unit_type != s->first_nal_type) {
2980 av_log(s->avctx, AV_LOG_ERROR,
2981 "Non-matching NAL types of the VCL NALUs: %d %d\n",
2982 s->first_nal_type, s->nal_unit_type);
2983 return AVERROR_INVALIDDATA;
2986 if (!s->sh.dependent_slice_segment_flag &&
2987 s->sh.slice_type != HEVC_SLICE_I) {
2988 ret = ff_hevc_slice_rpl(s);
2990 av_log(s->avctx, AV_LOG_WARNING,
2991 "Error constructing the reference lists for the current slice.\n");
2996 if (s->sh.first_slice_in_pic_flag && s->avctx->hwaccel) {
2997 ret = s->avctx->hwaccel->start_frame(s->avctx, NULL, 0);
3002 if (s->avctx->hwaccel) {
3003 ret = s->avctx->hwaccel->decode_slice(s->avctx, nal->raw_data, nal->raw_size);
3007 if (s->threads_number > 1 && s->sh.num_entry_point_offsets > 0)
3008 ctb_addr_ts = hls_slice_data_wpp(s, nal);
3010 ctb_addr_ts = hls_slice_data(s);
3011 if (ctb_addr_ts >= (s->ps.sps->ctb_width * s->ps.sps->ctb_height)) {
3015 if (ctb_addr_ts < 0) {
3021 case HEVC_NAL_EOS_NUT:
3022 case HEVC_NAL_EOB_NUT:
3023 s->seq_decode = (s->seq_decode + 1) & 0xff;
3024 s->max_ra = INT_MAX;
3027 case HEVC_NAL_FD_NUT:
3030 av_log(s->avctx, AV_LOG_INFO,
3031 "Skipping NAL unit %d\n", s->nal_unit_type);
3036 if (s->avctx->err_recognition & AV_EF_EXPLODE)
3041 static int decode_nal_units(HEVCContext *s, const uint8_t *buf, int length)
3044 int eos_at_start = 1;
3047 s->last_eos = s->eos;
3051 /* split the input packet into NAL units, so we know the upper bound on the
3052 * number of slices in the frame */
3053 ret = ff_h2645_packet_split(&s->pkt, buf, length, s->avctx, s->is_nalff,
3054 s->nal_length_size, s->avctx->codec_id, 1, 0);
3056 av_log(s->avctx, AV_LOG_ERROR,
3057 "Error splitting the input into NAL units.\n");
3061 for (i = 0; i < s->pkt.nb_nals; i++) {
3062 if (s->pkt.nals[i].type == HEVC_NAL_EOB_NUT ||
3063 s->pkt.nals[i].type == HEVC_NAL_EOS_NUT) {
3074 /* decode the NAL units */
3075 for (i = 0; i < s->pkt.nb_nals; i++) {
3076 H2645NAL *nal = &s->pkt.nals[i];
3078 if (s->avctx->skip_frame >= AVDISCARD_ALL ||
3079 (s->avctx->skip_frame >= AVDISCARD_NONREF
3080 && ff_hevc_nal_is_nonref(nal->type)))
3083 ret = decode_nal_unit(s, nal);
3084 if (ret >= 0 && s->overlap > 2)
3085 ret = AVERROR_INVALIDDATA;
3087 av_log(s->avctx, AV_LOG_WARNING,
3088 "Error parsing NAL unit #%d.\n", i);
3094 if (s->ref && s->threads_type == FF_THREAD_FRAME)
3095 ff_thread_report_progress(&s->ref->tf, INT_MAX, 0);
3100 static void print_md5(void *log_ctx, int level, uint8_t md5[16])
3103 for (i = 0; i < 16; i++)
3104 av_log(log_ctx, level, "%02"PRIx8, md5[i]);
3107 static int verify_md5(HEVCContext *s, AVFrame *frame)
3109 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(frame->format);
3114 return AVERROR(EINVAL);
3116 pixel_shift = desc->comp[0].depth > 8;
3118 av_log(s->avctx, AV_LOG_DEBUG, "Verifying checksum for frame with POC %d: ",
3121 /* the checksums are LE, so we have to byteswap for >8bpp formats
3124 if (pixel_shift && !s->checksum_buf) {
3125 av_fast_malloc(&s->checksum_buf, &s->checksum_buf_size,
3126 FFMAX3(frame->linesize[0], frame->linesize[1],
3127 frame->linesize[2]));
3128 if (!s->checksum_buf)
3129 return AVERROR(ENOMEM);
3133 for (i = 0; frame->data[i]; i++) {
3134 int width = s->avctx->coded_width;
3135 int height = s->avctx->coded_height;
3136 int w = (i == 1 || i == 2) ? (width >> desc->log2_chroma_w) : width;
3137 int h = (i == 1 || i == 2) ? (height >> desc->log2_chroma_h) : height;
3140 av_md5_init(s->md5_ctx);
3141 for (j = 0; j < h; j++) {
3142 const uint8_t *src = frame->data[i] + j * frame->linesize[i];
3145 s->bdsp.bswap16_buf((uint16_t *) s->checksum_buf,
3146 (const uint16_t *) src, w);
3147 src = s->checksum_buf;
3150 av_md5_update(s->md5_ctx, src, w << pixel_shift);
3152 av_md5_final(s->md5_ctx, md5);
3154 if (!memcmp(md5, s->sei.picture_hash.md5[i], 16)) {
3155 av_log (s->avctx, AV_LOG_DEBUG, "plane %d - correct ", i);
3156 print_md5(s->avctx, AV_LOG_DEBUG, md5);
3157 av_log (s->avctx, AV_LOG_DEBUG, "; ");
3159 av_log (s->avctx, AV_LOG_ERROR, "mismatching checksum of plane %d - ", i);
3160 print_md5(s->avctx, AV_LOG_ERROR, md5);
3161 av_log (s->avctx, AV_LOG_ERROR, " != ");
3162 print_md5(s->avctx, AV_LOG_ERROR, s->sei.picture_hash.md5[i]);
3163 av_log (s->avctx, AV_LOG_ERROR, "\n");
3164 return AVERROR_INVALIDDATA;
3168 av_log(s->avctx, AV_LOG_DEBUG, "\n");
3173 static int hevc_decode_extradata(HEVCContext *s, uint8_t *buf, int length, int first)
3177 ret = ff_hevc_decode_extradata(buf, length, &s->ps, &s->sei, &s->is_nalff,
3178 &s->nal_length_size, s->avctx->err_recognition,
3179 s->apply_defdispwin, s->avctx);
3183 /* export stream parameters from the first SPS */
3184 for (i = 0; i < FF_ARRAY_ELEMS(s->ps.sps_list); i++) {
3185 if (first && s->ps.sps_list[i]) {
3186 const HEVCSPS *sps = (const HEVCSPS*)s->ps.sps_list[i]->data;
3187 export_stream_params(s, sps);
3195 static int hevc_decode_frame(AVCodecContext *avctx, void *data, int *got_output,
3199 int new_extradata_size;
3200 uint8_t *new_extradata;
3201 HEVCContext *s = avctx->priv_data;
3204 ret = ff_hevc_output_frame(s, data, 1);
3212 new_extradata = av_packet_get_side_data(avpkt, AV_PKT_DATA_NEW_EXTRADATA,
3213 &new_extradata_size);
3214 if (new_extradata && new_extradata_size > 0) {
3215 ret = hevc_decode_extradata(s, new_extradata, new_extradata_size, 0);
3221 ret = decode_nal_units(s, avpkt->data, avpkt->size);
3225 if (avctx->hwaccel) {
3226 if (s->ref && (ret = avctx->hwaccel->end_frame(avctx)) < 0) {
3227 av_log(avctx, AV_LOG_ERROR,
3228 "hardware accelerator failed to decode picture\n");
3229 ff_hevc_unref_frame(s, s->ref, ~0);
3233 /* verify the SEI checksum */
3234 if (avctx->err_recognition & AV_EF_CRCCHECK && s->is_decoded &&
3235 s->sei.picture_hash.is_md5) {
3236 ret = verify_md5(s, s->ref->frame);
3237 if (ret < 0 && avctx->err_recognition & AV_EF_EXPLODE) {
3238 ff_hevc_unref_frame(s, s->ref, ~0);
3243 s->sei.picture_hash.is_md5 = 0;
3245 if (s->is_decoded) {
3246 av_log(avctx, AV_LOG_DEBUG, "Decoded frame with POC %d.\n", s->poc);
3250 if (s->output_frame->buf[0]) {
3251 av_frame_move_ref(data, s->output_frame);
3258 static int hevc_ref_frame(HEVCContext *s, HEVCFrame *dst, HEVCFrame *src)
3262 ret = ff_thread_ref_frame(&dst->tf, &src->tf);
3266 dst->tab_mvf_buf = av_buffer_ref(src->tab_mvf_buf);
3267 if (!dst->tab_mvf_buf)
3269 dst->tab_mvf = src->tab_mvf;
3271 dst->rpl_tab_buf = av_buffer_ref(src->rpl_tab_buf);
3272 if (!dst->rpl_tab_buf)
3274 dst->rpl_tab = src->rpl_tab;
3276 dst->rpl_buf = av_buffer_ref(src->rpl_buf);
3280 dst->poc = src->poc;
3281 dst->ctb_count = src->ctb_count;
3282 dst->flags = src->flags;
3283 dst->sequence = src->sequence;
3285 if (src->hwaccel_picture_private) {
3286 dst->hwaccel_priv_buf = av_buffer_ref(src->hwaccel_priv_buf);
3287 if (!dst->hwaccel_priv_buf)
3289 dst->hwaccel_picture_private = dst->hwaccel_priv_buf->data;
3294 ff_hevc_unref_frame(s, dst, ~0);
3295 return AVERROR(ENOMEM);
3298 static av_cold int hevc_decode_free(AVCodecContext *avctx)
3300 HEVCContext *s = avctx->priv_data;
3305 av_freep(&s->md5_ctx);
3307 av_freep(&s->cabac_state);
3309 for (i = 0; i < 3; i++) {
3310 av_freep(&s->sao_pixel_buffer_h[i]);
3311 av_freep(&s->sao_pixel_buffer_v[i]);
3313 av_frame_free(&s->output_frame);
3315 for (i = 0; i < FF_ARRAY_ELEMS(s->DPB); i++) {
3316 ff_hevc_unref_frame(s, &s->DPB[i], ~0);
3317 av_frame_free(&s->DPB[i].frame);
3320 ff_hevc_ps_uninit(&s->ps);
3322 av_freep(&s->sh.entry_point_offset);
3323 av_freep(&s->sh.offset);
3324 av_freep(&s->sh.size);
3326 for (i = 1; i < s->threads_number; i++) {
3327 HEVCLocalContext *lc = s->HEVClcList[i];
3329 av_freep(&s->HEVClcList[i]);
3330 av_freep(&s->sList[i]);
3333 if (s->HEVClc == s->HEVClcList[0])
3335 av_freep(&s->HEVClcList[0]);
3337 ff_h2645_packet_uninit(&s->pkt);
3342 static av_cold int hevc_init_context(AVCodecContext *avctx)
3344 HEVCContext *s = avctx->priv_data;
3349 s->HEVClc = av_mallocz(sizeof(HEVCLocalContext));
3352 s->HEVClcList[0] = s->HEVClc;
3355 s->cabac_state = av_malloc(HEVC_CONTEXTS);
3356 if (!s->cabac_state)
3359 s->output_frame = av_frame_alloc();
3360 if (!s->output_frame)
3363 for (i = 0; i < FF_ARRAY_ELEMS(s->DPB); i++) {
3364 s->DPB[i].frame = av_frame_alloc();
3365 if (!s->DPB[i].frame)
3367 s->DPB[i].tf.f = s->DPB[i].frame;
3370 s->max_ra = INT_MAX;
3372 s->md5_ctx = av_md5_alloc();
3376 ff_bswapdsp_init(&s->bdsp);
3378 s->context_initialized = 1;
3381 ff_hevc_reset_sei(&s->sei);
3386 hevc_decode_free(avctx);
3387 return AVERROR(ENOMEM);
3391 static int hevc_update_thread_context(AVCodecContext *dst,
3392 const AVCodecContext *src)
3394 HEVCContext *s = dst->priv_data;
3395 HEVCContext *s0 = src->priv_data;
3398 if (!s->context_initialized) {
3399 ret = hevc_init_context(dst);
3404 for (i = 0; i < FF_ARRAY_ELEMS(s->DPB); i++) {
3405 ff_hevc_unref_frame(s, &s->DPB[i], ~0);
3406 if (s0->DPB[i].frame->buf[0]) {
3407 ret = hevc_ref_frame(s, &s->DPB[i], &s0->DPB[i]);
3413 if (s->ps.sps != s0->ps.sps)
3415 for (i = 0; i < FF_ARRAY_ELEMS(s->ps.vps_list); i++) {
3416 av_buffer_unref(&s->ps.vps_list[i]);
3417 if (s0->ps.vps_list[i]) {
3418 s->ps.vps_list[i] = av_buffer_ref(s0->ps.vps_list[i]);
3419 if (!s->ps.vps_list[i])
3420 return AVERROR(ENOMEM);
3424 for (i = 0; i < FF_ARRAY_ELEMS(s->ps.sps_list); i++) {
3425 av_buffer_unref(&s->ps.sps_list[i]);
3426 if (s0->ps.sps_list[i]) {
3427 s->ps.sps_list[i] = av_buffer_ref(s0->ps.sps_list[i]);
3428 if (!s->ps.sps_list[i])
3429 return AVERROR(ENOMEM);
3433 for (i = 0; i < FF_ARRAY_ELEMS(s->ps.pps_list); i++) {
3434 av_buffer_unref(&s->ps.pps_list[i]);
3435 if (s0->ps.pps_list[i]) {
3436 s->ps.pps_list[i] = av_buffer_ref(s0->ps.pps_list[i]);
3437 if (!s->ps.pps_list[i])
3438 return AVERROR(ENOMEM);
3442 if (s->ps.sps != s0->ps.sps)
3443 if ((ret = set_sps(s, s0->ps.sps, src->pix_fmt)) < 0)
3446 s->seq_decode = s0->seq_decode;
3447 s->seq_output = s0->seq_output;
3448 s->pocTid0 = s0->pocTid0;
3449 s->max_ra = s0->max_ra;
3451 s->no_rasl_output_flag = s0->no_rasl_output_flag;
3453 s->is_nalff = s0->is_nalff;
3454 s->nal_length_size = s0->nal_length_size;
3456 s->threads_number = s0->threads_number;
3457 s->threads_type = s0->threads_type;
3460 s->seq_decode = (s->seq_decode + 1) & 0xff;
3461 s->max_ra = INT_MAX;
3464 s->sei.frame_packing = s0->sei.frame_packing;
3465 s->sei.display_orientation = s0->sei.display_orientation;
3466 s->sei.mastering_display = s0->sei.mastering_display;
3467 s->sei.content_light = s0->sei.content_light;
3468 s->sei.alternative_transfer = s0->sei.alternative_transfer;
3474 static av_cold int hevc_decode_init(AVCodecContext *avctx)
3476 HEVCContext *s = avctx->priv_data;
3479 avctx->internal->allocate_progress = 1;
3481 ret = hevc_init_context(avctx);
3485 s->enable_parallel_tiles = 0;
3486 s->sei.picture_timing.picture_struct = 0;
3489 atomic_init(&s->wpp_err, 0);
3491 if(avctx->active_thread_type & FF_THREAD_SLICE)
3492 s->threads_number = avctx->thread_count;
3494 s->threads_number = 1;
3496 if (avctx->extradata_size > 0 && avctx->extradata) {
3497 ret = hevc_decode_extradata(s, avctx->extradata, avctx->extradata_size, 1);
3499 hevc_decode_free(avctx);
3504 if((avctx->active_thread_type & FF_THREAD_FRAME) && avctx->thread_count > 1)
3505 s->threads_type = FF_THREAD_FRAME;
3507 s->threads_type = FF_THREAD_SLICE;
3513 static av_cold int hevc_init_thread_copy(AVCodecContext *avctx)
3515 HEVCContext *s = avctx->priv_data;
3518 memset(s, 0, sizeof(*s));
3520 ret = hevc_init_context(avctx);
3528 static void hevc_decode_flush(AVCodecContext *avctx)
3530 HEVCContext *s = avctx->priv_data;
3531 ff_hevc_flush_dpb(s);
3532 s->max_ra = INT_MAX;
3536 #define OFFSET(x) offsetof(HEVCContext, x)
3537 #define PAR (AV_OPT_FLAG_DECODING_PARAM | AV_OPT_FLAG_VIDEO_PARAM)
3539 static const AVOption options[] = {
3540 { "apply_defdispwin", "Apply default display window from VUI", OFFSET(apply_defdispwin),
3541 AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, PAR },
3542 { "strict-displaywin", "stricly apply default display window size", OFFSET(apply_defdispwin),
3543 AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, PAR },
3547 static const AVClass hevc_decoder_class = {
3548 .class_name = "HEVC decoder",
3549 .item_name = av_default_item_name,
3551 .version = LIBAVUTIL_VERSION_INT,
3554 AVCodec ff_hevc_decoder = {
3556 .long_name = NULL_IF_CONFIG_SMALL("HEVC (High Efficiency Video Coding)"),
3557 .type = AVMEDIA_TYPE_VIDEO,
3558 .id = AV_CODEC_ID_HEVC,
3559 .priv_data_size = sizeof(HEVCContext),
3560 .priv_class = &hevc_decoder_class,
3561 .init = hevc_decode_init,
3562 .close = hevc_decode_free,
3563 .decode = hevc_decode_frame,
3564 .flush = hevc_decode_flush,
3565 .update_thread_context = ONLY_IF_THREADS_ENABLED(hevc_update_thread_context),
3566 .init_thread_copy = ONLY_IF_THREADS_ENABLED(hevc_init_thread_copy),
3567 .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DELAY |
3568 AV_CODEC_CAP_SLICE_THREADS | AV_CODEC_CAP_FRAME_THREADS,
3569 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE | FF_CODEC_CAP_EXPORTS_CROPPING,
3570 .profiles = NULL_IF_CONFIG_SMALL(ff_hevc_profiles),
3571 .hw_configs = (const AVCodecHWConfigInternal*[]) {
3572 #if CONFIG_HEVC_DXVA2_HWACCEL
3573 HWACCEL_DXVA2(hevc),
3575 #if CONFIG_HEVC_D3D11VA_HWACCEL
3576 HWACCEL_D3D11VA(hevc),
3578 #if CONFIG_HEVC_D3D11VA2_HWACCEL
3579 HWACCEL_D3D11VA2(hevc),
3581 #if CONFIG_HEVC_NVDEC_HWACCEL
3582 HWACCEL_NVDEC(hevc),
3584 #if CONFIG_HEVC_VAAPI_HWACCEL
3585 HWACCEL_VAAPI(hevc),
3587 #if CONFIG_HEVC_VDPAU_HWACCEL
3588 HWACCEL_VDPAU(hevc),
3590 #if CONFIG_HEVC_VIDEOTOOLBOX_HWACCEL
3591 HWACCEL_VIDEOTOOLBOX(hevc),