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 Libav.
11 * Libav 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 * Libav 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 Libav; 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/md5.h"
31 #include "libavutil/opt.h"
32 #include "libavutil/pixdesc.h"
33 #include "libavutil/stereo3d.h"
36 #include "bytestream.h"
37 #include "cabac_functions.h"
41 const uint8_t ff_hevc_qpel_extra_before[4] = { 0, 3, 3, 2 };
42 const uint8_t ff_hevc_qpel_extra_after[4] = { 0, 3, 4, 4 };
43 const uint8_t ff_hevc_qpel_extra[4] = { 0, 6, 7, 6 };
45 static const uint8_t scan_1x1[1] = { 0 };
47 static const uint8_t horiz_scan2x2_x[4] = { 0, 1, 0, 1 };
49 static const uint8_t horiz_scan2x2_y[4] = { 0, 0, 1, 1 };
51 static const uint8_t horiz_scan4x4_x[16] = {
58 static const uint8_t horiz_scan4x4_y[16] = {
65 static const uint8_t horiz_scan8x8_inv[8][8] = {
66 { 0, 1, 2, 3, 16, 17, 18, 19, },
67 { 4, 5, 6, 7, 20, 21, 22, 23, },
68 { 8, 9, 10, 11, 24, 25, 26, 27, },
69 { 12, 13, 14, 15, 28, 29, 30, 31, },
70 { 32, 33, 34, 35, 48, 49, 50, 51, },
71 { 36, 37, 38, 39, 52, 53, 54, 55, },
72 { 40, 41, 42, 43, 56, 57, 58, 59, },
73 { 44, 45, 46, 47, 60, 61, 62, 63, },
76 static const uint8_t diag_scan2x2_x[4] = { 0, 0, 1, 1 };
78 static const uint8_t diag_scan2x2_y[4] = { 0, 1, 0, 1 };
80 static const uint8_t diag_scan2x2_inv[2][2] = {
85 static const uint8_t diag_scan4x4_inv[4][4] = {
92 static const uint8_t diag_scan8x8_inv[8][8] = {
93 { 0, 2, 5, 9, 14, 20, 27, 35, },
94 { 1, 4, 8, 13, 19, 26, 34, 42, },
95 { 3, 7, 12, 18, 25, 33, 41, 48, },
96 { 6, 11, 17, 24, 32, 40, 47, 53, },
97 { 10, 16, 23, 31, 39, 46, 52, 57, },
98 { 15, 22, 30, 38, 45, 51, 56, 60, },
99 { 21, 29, 37, 44, 50, 55, 59, 62, },
100 { 28, 36, 43, 49, 54, 58, 61, 63, },
104 * NOTE: Each function hls_foo correspond to the function foo in the
105 * specification (HLS stands for High Level Syntax).
112 /* free everything allocated by pic_arrays_init() */
113 static void pic_arrays_free(HEVCContext *s)
116 av_freep(&s->deblock);
118 av_freep(&s->skip_flag);
119 av_freep(&s->tab_ct_depth);
121 av_freep(&s->tab_ipm);
122 av_freep(&s->cbf_luma);
123 av_freep(&s->is_pcm);
125 av_freep(&s->qp_y_tab);
126 av_freep(&s->tab_slice_address);
127 av_freep(&s->filter_slice_edges);
129 av_freep(&s->horizontal_bs);
130 av_freep(&s->vertical_bs);
132 av_buffer_pool_uninit(&s->tab_mvf_pool);
133 av_buffer_pool_uninit(&s->rpl_tab_pool);
136 /* allocate arrays that depend on frame dimensions */
137 static int pic_arrays_init(HEVCContext *s, const HEVCSPS *sps)
139 int log2_min_cb_size = sps->log2_min_cb_size;
140 int width = sps->width;
141 int height = sps->height;
142 int pic_size_in_ctb = ((width >> log2_min_cb_size) + 1) *
143 ((height >> log2_min_cb_size) + 1);
144 int ctb_count = sps->ctb_width * sps->ctb_height;
145 int min_pu_size = sps->min_pu_width * sps->min_pu_height;
147 s->bs_width = width >> 3;
148 s->bs_height = height >> 3;
150 s->sao = av_mallocz_array(ctb_count, sizeof(*s->sao));
151 s->deblock = av_mallocz_array(ctb_count, sizeof(*s->deblock));
152 if (!s->sao || !s->deblock)
155 s->skip_flag = av_malloc(pic_size_in_ctb);
156 s->tab_ct_depth = av_malloc(sps->min_cb_height * sps->min_cb_width);
157 if (!s->skip_flag || !s->tab_ct_depth)
160 s->cbf_luma = av_malloc(sps->min_tb_width * sps->min_tb_height);
161 s->tab_ipm = av_mallocz(min_pu_size);
162 s->is_pcm = av_malloc(min_pu_size);
163 if (!s->tab_ipm || !s->cbf_luma || !s->is_pcm)
166 s->filter_slice_edges = av_malloc(ctb_count);
167 s->tab_slice_address = av_malloc(pic_size_in_ctb *
168 sizeof(*s->tab_slice_address));
169 s->qp_y_tab = av_malloc(pic_size_in_ctb *
170 sizeof(*s->qp_y_tab));
171 if (!s->qp_y_tab || !s->filter_slice_edges || !s->tab_slice_address)
174 s->horizontal_bs = av_mallocz(2 * s->bs_width * (s->bs_height + 1));
175 s->vertical_bs = av_mallocz(2 * s->bs_width * (s->bs_height + 1));
176 if (!s->horizontal_bs || !s->vertical_bs)
179 s->tab_mvf_pool = av_buffer_pool_init(min_pu_size * sizeof(MvField),
181 s->rpl_tab_pool = av_buffer_pool_init(ctb_count * sizeof(RefPicListTab),
183 if (!s->tab_mvf_pool || !s->rpl_tab_pool)
190 return AVERROR(ENOMEM);
193 static void pred_weight_table(HEVCContext *s, GetBitContext *gb)
197 uint8_t luma_weight_l0_flag[16];
198 uint8_t chroma_weight_l0_flag[16];
199 uint8_t luma_weight_l1_flag[16];
200 uint8_t chroma_weight_l1_flag[16];
202 s->sh.luma_log2_weight_denom = av_clip(get_ue_golomb_long(gb), 0, 7);
203 if (s->ps.sps->chroma_format_idc != 0) {
204 int delta = get_se_golomb(gb);
205 s->sh.chroma_log2_weight_denom = av_clip(s->sh.luma_log2_weight_denom + delta, 0, 7);
208 for (i = 0; i < s->sh.nb_refs[L0]; i++) {
209 luma_weight_l0_flag[i] = get_bits1(gb);
210 if (!luma_weight_l0_flag[i]) {
211 s->sh.luma_weight_l0[i] = 1 << s->sh.luma_log2_weight_denom;
212 s->sh.luma_offset_l0[i] = 0;
215 if (s->ps.sps->chroma_format_idc != 0) { // FIXME: invert "if" and "for"
216 for (i = 0; i < s->sh.nb_refs[L0]; i++)
217 chroma_weight_l0_flag[i] = get_bits1(gb);
219 for (i = 0; i < s->sh.nb_refs[L0]; i++)
220 chroma_weight_l0_flag[i] = 0;
222 for (i = 0; i < s->sh.nb_refs[L0]; i++) {
223 if (luma_weight_l0_flag[i]) {
224 int delta_luma_weight_l0 = get_se_golomb(gb);
225 s->sh.luma_weight_l0[i] = (1 << s->sh.luma_log2_weight_denom) + delta_luma_weight_l0;
226 s->sh.luma_offset_l0[i] = get_se_golomb(gb);
228 if (chroma_weight_l0_flag[i]) {
229 for (j = 0; j < 2; j++) {
230 int delta_chroma_weight_l0 = get_se_golomb(gb);
231 int delta_chroma_offset_l0 = get_se_golomb(gb);
232 s->sh.chroma_weight_l0[i][j] = (1 << s->sh.chroma_log2_weight_denom) + delta_chroma_weight_l0;
233 s->sh.chroma_offset_l0[i][j] = av_clip((delta_chroma_offset_l0 - ((128 * s->sh.chroma_weight_l0[i][j])
234 >> s->sh.chroma_log2_weight_denom) + 128), -128, 127);
237 s->sh.chroma_weight_l0[i][0] = 1 << s->sh.chroma_log2_weight_denom;
238 s->sh.chroma_offset_l0[i][0] = 0;
239 s->sh.chroma_weight_l0[i][1] = 1 << s->sh.chroma_log2_weight_denom;
240 s->sh.chroma_offset_l0[i][1] = 0;
243 if (s->sh.slice_type == B_SLICE) {
244 for (i = 0; i < s->sh.nb_refs[L1]; i++) {
245 luma_weight_l1_flag[i] = get_bits1(gb);
246 if (!luma_weight_l1_flag[i]) {
247 s->sh.luma_weight_l1[i] = 1 << s->sh.luma_log2_weight_denom;
248 s->sh.luma_offset_l1[i] = 0;
251 if (s->ps.sps->chroma_format_idc != 0) {
252 for (i = 0; i < s->sh.nb_refs[L1]; i++)
253 chroma_weight_l1_flag[i] = get_bits1(gb);
255 for (i = 0; i < s->sh.nb_refs[L1]; i++)
256 chroma_weight_l1_flag[i] = 0;
258 for (i = 0; i < s->sh.nb_refs[L1]; i++) {
259 if (luma_weight_l1_flag[i]) {
260 int delta_luma_weight_l1 = get_se_golomb(gb);
261 s->sh.luma_weight_l1[i] = (1 << s->sh.luma_log2_weight_denom) + delta_luma_weight_l1;
262 s->sh.luma_offset_l1[i] = get_se_golomb(gb);
264 if (chroma_weight_l1_flag[i]) {
265 for (j = 0; j < 2; j++) {
266 int delta_chroma_weight_l1 = get_se_golomb(gb);
267 int delta_chroma_offset_l1 = get_se_golomb(gb);
268 s->sh.chroma_weight_l1[i][j] = (1 << s->sh.chroma_log2_weight_denom) + delta_chroma_weight_l1;
269 s->sh.chroma_offset_l1[i][j] = av_clip((delta_chroma_offset_l1 - ((128 * s->sh.chroma_weight_l1[i][j])
270 >> s->sh.chroma_log2_weight_denom) + 128), -128, 127);
273 s->sh.chroma_weight_l1[i][0] = 1 << s->sh.chroma_log2_weight_denom;
274 s->sh.chroma_offset_l1[i][0] = 0;
275 s->sh.chroma_weight_l1[i][1] = 1 << s->sh.chroma_log2_weight_denom;
276 s->sh.chroma_offset_l1[i][1] = 0;
282 static int decode_lt_rps(HEVCContext *s, LongTermRPS *rps, GetBitContext *gb)
284 const HEVCSPS *sps = s->ps.sps;
285 int max_poc_lsb = 1 << sps->log2_max_poc_lsb;
286 int prev_delta_msb = 0;
287 unsigned int nb_sps = 0, nb_sh;
291 if (!sps->long_term_ref_pics_present_flag)
294 if (sps->num_long_term_ref_pics_sps > 0)
295 nb_sps = get_ue_golomb_long(gb);
296 nb_sh = get_ue_golomb_long(gb);
298 if (nb_sh + nb_sps > FF_ARRAY_ELEMS(rps->poc))
299 return AVERROR_INVALIDDATA;
301 rps->nb_refs = nb_sh + nb_sps;
303 for (i = 0; i < rps->nb_refs; i++) {
304 uint8_t delta_poc_msb_present;
307 uint8_t lt_idx_sps = 0;
309 if (sps->num_long_term_ref_pics_sps > 1)
310 lt_idx_sps = get_bits(gb, av_ceil_log2(sps->num_long_term_ref_pics_sps));
312 rps->poc[i] = sps->lt_ref_pic_poc_lsb_sps[lt_idx_sps];
313 rps->used[i] = sps->used_by_curr_pic_lt_sps_flag[lt_idx_sps];
315 rps->poc[i] = get_bits(gb, sps->log2_max_poc_lsb);
316 rps->used[i] = get_bits1(gb);
319 delta_poc_msb_present = get_bits1(gb);
320 if (delta_poc_msb_present) {
321 int delta = get_ue_golomb_long(gb);
323 if (i && i != nb_sps)
324 delta += prev_delta_msb;
326 rps->poc[i] += s->poc - delta * max_poc_lsb - s->sh.pic_order_cnt_lsb;
327 prev_delta_msb = delta;
334 static void export_stream_params(AVCodecContext *avctx, const HEVCParamSets *ps,
337 const HEVCVPS *vps = (const HEVCVPS*)ps->vps_list[sps->vps_id]->data;
338 unsigned int num = 0, den = 0;
340 avctx->pix_fmt = sps->pix_fmt;
341 avctx->coded_width = sps->width;
342 avctx->coded_height = sps->height;
343 avctx->width = sps->output_width;
344 avctx->height = sps->output_height;
345 avctx->has_b_frames = sps->temporal_layer[sps->max_sub_layers - 1].num_reorder_pics;
346 avctx->profile = sps->ptl.general_ptl.profile_idc;
347 avctx->level = sps->ptl.general_ptl.level_idc;
349 ff_set_sar(avctx, sps->vui.sar);
351 if (sps->vui.video_signal_type_present_flag)
352 avctx->color_range = sps->vui.video_full_range_flag ? AVCOL_RANGE_JPEG
355 avctx->color_range = AVCOL_RANGE_MPEG;
357 if (sps->vui.colour_description_present_flag) {
358 avctx->color_primaries = sps->vui.colour_primaries;
359 avctx->color_trc = sps->vui.transfer_characteristic;
360 avctx->colorspace = sps->vui.matrix_coeffs;
362 avctx->color_primaries = AVCOL_PRI_UNSPECIFIED;
363 avctx->color_trc = AVCOL_TRC_UNSPECIFIED;
364 avctx->colorspace = AVCOL_SPC_UNSPECIFIED;
367 if (vps->vps_timing_info_present_flag) {
368 num = vps->vps_num_units_in_tick;
369 den = vps->vps_time_scale;
370 } else if (sps->vui.vui_timing_info_present_flag) {
371 num = sps->vui.vui_num_units_in_tick;
372 den = sps->vui.vui_time_scale;
375 if (num != 0 && den != 0)
376 av_reduce(&avctx->framerate.den, &avctx->framerate.num,
380 static int set_sps(HEVCContext *s, const HEVCSPS *sps)
382 #define HWACCEL_MAX (CONFIG_HEVC_DXVA2_HWACCEL + CONFIG_HEVC_D3D11VA_HWACCEL)
383 enum AVPixelFormat pix_fmts[HWACCEL_MAX + 2], *fmt = pix_fmts;
393 ret = pic_arrays_init(s, sps);
397 export_stream_params(s->avctx, &s->ps, sps);
399 if (sps->pix_fmt == AV_PIX_FMT_YUV420P || sps->pix_fmt == AV_PIX_FMT_YUVJ420P) {
400 #if CONFIG_HEVC_DXVA2_HWACCEL
401 *fmt++ = AV_PIX_FMT_DXVA2_VLD;
403 #if CONFIG_HEVC_D3D11VA_HWACCEL
404 *fmt++ = AV_PIX_FMT_D3D11VA_VLD;
408 *fmt++ = sps->pix_fmt;
409 *fmt = AV_PIX_FMT_NONE;
411 ret = ff_get_format(s->avctx, pix_fmts);
414 s->avctx->pix_fmt = ret;
416 ff_hevc_pred_init(&s->hpc, sps->bit_depth);
417 ff_hevc_dsp_init (&s->hevcdsp, sps->bit_depth);
418 ff_videodsp_init (&s->vdsp, sps->bit_depth);
420 if (sps->sao_enabled && !s->avctx->hwaccel) {
421 av_frame_unref(s->tmp_frame);
422 ret = ff_get_buffer(s->avctx, s->tmp_frame, AV_GET_BUFFER_FLAG_REF);
425 s->frame = s->tmp_frame;
429 s->ps.vps = (HEVCVPS*) s->ps.vps_list[s->ps.sps->vps_id]->data;
439 static int hls_slice_header(HEVCContext *s)
441 GetBitContext *gb = &s->HEVClc.gb;
442 SliceHeader *sh = &s->sh;
446 sh->first_slice_in_pic_flag = get_bits1(gb);
447 if ((IS_IDR(s) || IS_BLA(s)) && sh->first_slice_in_pic_flag) {
448 s->seq_decode = (s->seq_decode + 1) & 0xff;
451 ff_hevc_clear_refs(s);
454 sh->no_output_of_prior_pics_flag = get_bits1(gb);
456 sh->pps_id = get_ue_golomb_long(gb);
457 if (sh->pps_id >= MAX_PPS_COUNT || !s->ps.pps_list[sh->pps_id]) {
458 av_log(s->avctx, AV_LOG_ERROR, "PPS id out of range: %d\n", sh->pps_id);
459 return AVERROR_INVALIDDATA;
461 if (!sh->first_slice_in_pic_flag &&
462 s->ps.pps != (HEVCPPS*)s->ps.pps_list[sh->pps_id]->data) {
463 av_log(s->avctx, AV_LOG_ERROR, "PPS changed between slices.\n");
464 return AVERROR_INVALIDDATA;
466 s->ps.pps = (HEVCPPS*)s->ps.pps_list[sh->pps_id]->data;
468 if (s->ps.sps != (HEVCSPS*)s->ps.sps_list[s->ps.pps->sps_id]->data) {
469 s->ps.sps = (HEVCSPS*)s->ps.sps_list[s->ps.pps->sps_id]->data;
471 ff_hevc_clear_refs(s);
472 ret = set_sps(s, s->ps.sps);
476 s->seq_decode = (s->seq_decode + 1) & 0xff;
480 sh->dependent_slice_segment_flag = 0;
481 if (!sh->first_slice_in_pic_flag) {
482 int slice_address_length;
484 if (s->ps.pps->dependent_slice_segments_enabled_flag)
485 sh->dependent_slice_segment_flag = get_bits1(gb);
487 slice_address_length = av_ceil_log2(s->ps.sps->ctb_width *
488 s->ps.sps->ctb_height);
489 sh->slice_segment_addr = slice_address_length ? get_bits(gb, slice_address_length) : 0;
490 if (sh->slice_segment_addr >= s->ps.sps->ctb_width * s->ps.sps->ctb_height) {
491 av_log(s->avctx, AV_LOG_ERROR,
492 "Invalid slice segment address: %u.\n",
493 sh->slice_segment_addr);
494 return AVERROR_INVALIDDATA;
497 if (!sh->dependent_slice_segment_flag) {
498 sh->slice_addr = sh->slice_segment_addr;
502 sh->slice_segment_addr = sh->slice_addr = 0;
504 s->slice_initialized = 0;
507 if (!sh->dependent_slice_segment_flag) {
508 s->slice_initialized = 0;
510 for (i = 0; i < s->ps.pps->num_extra_slice_header_bits; i++)
511 skip_bits(gb, 1); // slice_reserved_undetermined_flag[]
513 sh->slice_type = get_ue_golomb_long(gb);
514 if (!(sh->slice_type == I_SLICE ||
515 sh->slice_type == P_SLICE ||
516 sh->slice_type == B_SLICE)) {
517 av_log(s->avctx, AV_LOG_ERROR, "Unknown slice type: %d.\n",
519 return AVERROR_INVALIDDATA;
521 if (IS_IRAP(s) && sh->slice_type != I_SLICE) {
522 av_log(s->avctx, AV_LOG_ERROR, "Inter slices in an IRAP frame.\n");
523 return AVERROR_INVALIDDATA;
526 // when flag is not present, picture is inferred to be output
527 sh->pic_output_flag = 1;
528 if (s->ps.pps->output_flag_present_flag)
529 sh->pic_output_flag = get_bits1(gb);
531 if (s->ps.sps->separate_colour_plane_flag)
532 sh->colour_plane_id = get_bits(gb, 2);
537 sh->pic_order_cnt_lsb = get_bits(gb, s->ps.sps->log2_max_poc_lsb);
538 poc = ff_hevc_compute_poc(s, sh->pic_order_cnt_lsb);
539 if (!sh->first_slice_in_pic_flag && poc != s->poc) {
540 av_log(s->avctx, AV_LOG_WARNING,
541 "Ignoring POC change between slices: %d -> %d\n", s->poc, poc);
542 if (s->avctx->err_recognition & AV_EF_EXPLODE)
543 return AVERROR_INVALIDDATA;
548 sh->short_term_ref_pic_set_sps_flag = get_bits1(gb);
549 if (!sh->short_term_ref_pic_set_sps_flag) {
550 int pos = get_bits_left(gb);
551 ret = ff_hevc_decode_short_term_rps(gb, s->avctx, &sh->slice_rps, s->ps.sps, 1);
555 sh->short_term_ref_pic_set_size = pos - get_bits_left(gb);
556 sh->short_term_rps = &sh->slice_rps;
558 int numbits, rps_idx;
560 if (!s->ps.sps->nb_st_rps) {
561 av_log(s->avctx, AV_LOG_ERROR, "No ref lists in the SPS.\n");
562 return AVERROR_INVALIDDATA;
565 numbits = av_ceil_log2(s->ps.sps->nb_st_rps);
566 rps_idx = numbits > 0 ? get_bits(gb, numbits) : 0;
567 sh->short_term_rps = &s->ps.sps->st_rps[rps_idx];
570 ret = decode_lt_rps(s, &sh->long_term_rps, gb);
572 av_log(s->avctx, AV_LOG_WARNING, "Invalid long term RPS.\n");
573 if (s->avctx->err_recognition & AV_EF_EXPLODE)
574 return AVERROR_INVALIDDATA;
577 if (s->ps.sps->sps_temporal_mvp_enabled_flag)
578 sh->slice_temporal_mvp_enabled_flag = get_bits1(gb);
580 sh->slice_temporal_mvp_enabled_flag = 0;
582 s->sh.short_term_rps = NULL;
587 if (s->temporal_id == 0 &&
588 s->nal_unit_type != NAL_TRAIL_N &&
589 s->nal_unit_type != NAL_TSA_N &&
590 s->nal_unit_type != NAL_STSA_N &&
591 s->nal_unit_type != NAL_RADL_N &&
592 s->nal_unit_type != NAL_RADL_R &&
593 s->nal_unit_type != NAL_RASL_N &&
594 s->nal_unit_type != NAL_RASL_R)
597 if (s->ps.sps->sao_enabled) {
598 sh->slice_sample_adaptive_offset_flag[0] = get_bits1(gb);
599 sh->slice_sample_adaptive_offset_flag[1] =
600 sh->slice_sample_adaptive_offset_flag[2] = get_bits1(gb);
602 sh->slice_sample_adaptive_offset_flag[0] = 0;
603 sh->slice_sample_adaptive_offset_flag[1] = 0;
604 sh->slice_sample_adaptive_offset_flag[2] = 0;
607 sh->nb_refs[L0] = sh->nb_refs[L1] = 0;
608 if (sh->slice_type == P_SLICE || sh->slice_type == B_SLICE) {
611 sh->nb_refs[L0] = s->ps.pps->num_ref_idx_l0_default_active;
612 if (sh->slice_type == B_SLICE)
613 sh->nb_refs[L1] = s->ps.pps->num_ref_idx_l1_default_active;
615 if (get_bits1(gb)) { // num_ref_idx_active_override_flag
616 sh->nb_refs[L0] = get_ue_golomb_long(gb) + 1;
617 if (sh->slice_type == B_SLICE)
618 sh->nb_refs[L1] = get_ue_golomb_long(gb) + 1;
620 if (sh->nb_refs[L0] > MAX_REFS || sh->nb_refs[L1] > MAX_REFS) {
621 av_log(s->avctx, AV_LOG_ERROR, "Too many refs: %d/%d.\n",
622 sh->nb_refs[L0], sh->nb_refs[L1]);
623 return AVERROR_INVALIDDATA;
626 sh->rpl_modification_flag[0] = 0;
627 sh->rpl_modification_flag[1] = 0;
628 nb_refs = ff_hevc_frame_nb_refs(s);
630 av_log(s->avctx, AV_LOG_ERROR, "Zero refs for a frame with P or B slices.\n");
631 return AVERROR_INVALIDDATA;
634 if (s->ps.pps->lists_modification_present_flag && nb_refs > 1) {
635 sh->rpl_modification_flag[0] = get_bits1(gb);
636 if (sh->rpl_modification_flag[0]) {
637 for (i = 0; i < sh->nb_refs[L0]; i++)
638 sh->list_entry_lx[0][i] = get_bits(gb, av_ceil_log2(nb_refs));
641 if (sh->slice_type == B_SLICE) {
642 sh->rpl_modification_flag[1] = get_bits1(gb);
643 if (sh->rpl_modification_flag[1] == 1)
644 for (i = 0; i < sh->nb_refs[L1]; i++)
645 sh->list_entry_lx[1][i] = get_bits(gb, av_ceil_log2(nb_refs));
649 if (sh->slice_type == B_SLICE)
650 sh->mvd_l1_zero_flag = get_bits1(gb);
652 if (s->ps.pps->cabac_init_present_flag)
653 sh->cabac_init_flag = get_bits1(gb);
655 sh->cabac_init_flag = 0;
657 sh->collocated_ref_idx = 0;
658 if (sh->slice_temporal_mvp_enabled_flag) {
659 sh->collocated_list = L0;
660 if (sh->slice_type == B_SLICE)
661 sh->collocated_list = !get_bits1(gb);
663 if (sh->nb_refs[sh->collocated_list] > 1) {
664 sh->collocated_ref_idx = get_ue_golomb_long(gb);
665 if (sh->collocated_ref_idx >= sh->nb_refs[sh->collocated_list]) {
666 av_log(s->avctx, AV_LOG_ERROR,
667 "Invalid collocated_ref_idx: %d.\n",
668 sh->collocated_ref_idx);
669 return AVERROR_INVALIDDATA;
674 if ((s->ps.pps->weighted_pred_flag && sh->slice_type == P_SLICE) ||
675 (s->ps.pps->weighted_bipred_flag && sh->slice_type == B_SLICE)) {
676 pred_weight_table(s, gb);
679 sh->max_num_merge_cand = 5 - get_ue_golomb_long(gb);
680 if (sh->max_num_merge_cand < 1 || sh->max_num_merge_cand > 5) {
681 av_log(s->avctx, AV_LOG_ERROR,
682 "Invalid number of merging MVP candidates: %d.\n",
683 sh->max_num_merge_cand);
684 return AVERROR_INVALIDDATA;
688 sh->slice_qp_delta = get_se_golomb(gb);
690 if (s->ps.pps->pic_slice_level_chroma_qp_offsets_present_flag) {
691 sh->slice_cb_qp_offset = get_se_golomb(gb);
692 sh->slice_cr_qp_offset = get_se_golomb(gb);
694 sh->slice_cb_qp_offset = 0;
695 sh->slice_cr_qp_offset = 0;
698 if (s->ps.pps->deblocking_filter_control_present_flag) {
699 int deblocking_filter_override_flag = 0;
701 if (s->ps.pps->deblocking_filter_override_enabled_flag)
702 deblocking_filter_override_flag = get_bits1(gb);
704 if (deblocking_filter_override_flag) {
705 sh->disable_deblocking_filter_flag = get_bits1(gb);
706 if (!sh->disable_deblocking_filter_flag) {
707 sh->beta_offset = get_se_golomb(gb) * 2;
708 sh->tc_offset = get_se_golomb(gb) * 2;
711 sh->disable_deblocking_filter_flag = s->ps.pps->disable_dbf;
712 sh->beta_offset = s->ps.pps->beta_offset;
713 sh->tc_offset = s->ps.pps->tc_offset;
716 sh->disable_deblocking_filter_flag = 0;
721 if (s->ps.pps->seq_loop_filter_across_slices_enabled_flag &&
722 (sh->slice_sample_adaptive_offset_flag[0] ||
723 sh->slice_sample_adaptive_offset_flag[1] ||
724 !sh->disable_deblocking_filter_flag)) {
725 sh->slice_loop_filter_across_slices_enabled_flag = get_bits1(gb);
727 sh->slice_loop_filter_across_slices_enabled_flag = s->ps.pps->seq_loop_filter_across_slices_enabled_flag;
729 } else if (!s->slice_initialized) {
730 av_log(s->avctx, AV_LOG_ERROR, "Independent slice segment missing.\n");
731 return AVERROR_INVALIDDATA;
734 sh->num_entry_point_offsets = 0;
735 if (s->ps.pps->tiles_enabled_flag || s->ps.pps->entropy_coding_sync_enabled_flag) {
736 sh->num_entry_point_offsets = get_ue_golomb_long(gb);
737 if (sh->num_entry_point_offsets > 0) {
738 int offset_len = get_ue_golomb_long(gb) + 1;
740 for (i = 0; i < sh->num_entry_point_offsets; i++)
741 skip_bits(gb, offset_len);
745 if (s->ps.pps->slice_header_extension_present_flag) {
746 unsigned int length = get_ue_golomb_long(gb);
747 for (i = 0; i < length; i++)
748 skip_bits(gb, 8); // slice_header_extension_data_byte
751 // Inferred parameters
752 sh->slice_qp = 26 + s->ps.pps->pic_init_qp_minus26 + sh->slice_qp_delta;
753 if (sh->slice_qp > 51 ||
754 sh->slice_qp < -s->ps.sps->qp_bd_offset) {
755 av_log(s->avctx, AV_LOG_ERROR,
756 "The slice_qp %d is outside the valid range "
759 -s->ps.sps->qp_bd_offset);
760 return AVERROR_INVALIDDATA;
763 sh->slice_ctb_addr_rs = sh->slice_segment_addr;
765 if (!s->sh.slice_ctb_addr_rs && s->sh.dependent_slice_segment_flag) {
766 av_log(s->avctx, AV_LOG_ERROR, "Impossible slice segment.\n");
767 return AVERROR_INVALIDDATA;
770 s->HEVClc.first_qp_group = !s->sh.dependent_slice_segment_flag;
772 if (!s->ps.pps->cu_qp_delta_enabled_flag)
773 s->HEVClc.qp_y = FFUMOD(s->sh.slice_qp + 52 + 2 * s->ps.sps->qp_bd_offset,
774 52 + s->ps.sps->qp_bd_offset) - s->ps.sps->qp_bd_offset;
776 s->slice_initialized = 1;
781 #define CTB(tab, x, y) ((tab)[(y) * s->ps.sps->ctb_width + (x)])
783 #define SET_SAO(elem, value) \
785 if (!sao_merge_up_flag && !sao_merge_left_flag) \
787 else if (sao_merge_left_flag) \
788 sao->elem = CTB(s->sao, rx-1, ry).elem; \
789 else if (sao_merge_up_flag) \
790 sao->elem = CTB(s->sao, rx, ry-1).elem; \
795 static void hls_sao_param(HEVCContext *s, int rx, int ry)
797 HEVCLocalContext *lc = &s->HEVClc;
798 int sao_merge_left_flag = 0;
799 int sao_merge_up_flag = 0;
800 int shift = s->ps.sps->bit_depth - FFMIN(s->ps.sps->bit_depth, 10);
801 SAOParams *sao = &CTB(s->sao, rx, ry);
804 if (s->sh.slice_sample_adaptive_offset_flag[0] ||
805 s->sh.slice_sample_adaptive_offset_flag[1]) {
807 if (lc->ctb_left_flag)
808 sao_merge_left_flag = ff_hevc_sao_merge_flag_decode(s);
810 if (ry > 0 && !sao_merge_left_flag) {
812 sao_merge_up_flag = ff_hevc_sao_merge_flag_decode(s);
816 for (c_idx = 0; c_idx < 3; c_idx++) {
817 if (!s->sh.slice_sample_adaptive_offset_flag[c_idx]) {
818 sao->type_idx[c_idx] = SAO_NOT_APPLIED;
823 sao->type_idx[2] = sao->type_idx[1];
824 sao->eo_class[2] = sao->eo_class[1];
826 SET_SAO(type_idx[c_idx], ff_hevc_sao_type_idx_decode(s));
829 if (sao->type_idx[c_idx] == SAO_NOT_APPLIED)
832 for (i = 0; i < 4; i++)
833 SET_SAO(offset_abs[c_idx][i], ff_hevc_sao_offset_abs_decode(s));
835 if (sao->type_idx[c_idx] == SAO_BAND) {
836 for (i = 0; i < 4; i++) {
837 if (sao->offset_abs[c_idx][i]) {
838 SET_SAO(offset_sign[c_idx][i],
839 ff_hevc_sao_offset_sign_decode(s));
841 sao->offset_sign[c_idx][i] = 0;
844 SET_SAO(band_position[c_idx], ff_hevc_sao_band_position_decode(s));
845 } else if (c_idx != 2) {
846 SET_SAO(eo_class[c_idx], ff_hevc_sao_eo_class_decode(s));
849 // Inferred parameters
850 sao->offset_val[c_idx][0] = 0;
851 for (i = 0; i < 4; i++) {
852 sao->offset_val[c_idx][i + 1] = sao->offset_abs[c_idx][i] << shift;
853 if (sao->type_idx[c_idx] == SAO_EDGE) {
855 sao->offset_val[c_idx][i + 1] = -sao->offset_val[c_idx][i + 1];
856 } else if (sao->offset_sign[c_idx][i]) {
857 sao->offset_val[c_idx][i + 1] = -sao->offset_val[c_idx][i + 1];
866 static void hls_residual_coding(HEVCContext *s, int x0, int y0,
867 int log2_trafo_size, enum ScanType scan_idx,
870 #define GET_COORD(offset, n) \
872 x_c = (scan_x_cg[offset >> 4] << 2) + scan_x_off[n]; \
873 y_c = (scan_y_cg[offset >> 4] << 2) + scan_y_off[n]; \
875 HEVCLocalContext *lc = &s->HEVClc;
876 int transform_skip_flag = 0;
878 int last_significant_coeff_x, last_significant_coeff_y;
882 int greater1_ctx = 1;
885 int x_cg_last_sig, y_cg_last_sig;
887 const uint8_t *scan_x_cg, *scan_y_cg, *scan_x_off, *scan_y_off;
889 ptrdiff_t stride = s->frame->linesize[c_idx];
890 int hshift = s->ps.sps->hshift[c_idx];
891 int vshift = s->ps.sps->vshift[c_idx];
892 uint8_t *dst = &s->frame->data[c_idx][(y0 >> vshift) * stride +
893 ((x0 >> hshift) << s->ps.sps->pixel_shift)];
894 DECLARE_ALIGNED(16, int16_t, coeffs[MAX_TB_SIZE * MAX_TB_SIZE]) = { 0 };
895 DECLARE_ALIGNED(8, uint8_t, significant_coeff_group_flag[8][8]) = { { 0 } };
897 int trafo_size = 1 << log2_trafo_size;
898 int i, qp, shift, add, scale, scale_m;
899 const uint8_t level_scale[] = { 40, 45, 51, 57, 64, 72 };
900 const uint8_t *scale_matrix;
903 // Derive QP for dequant
904 if (!lc->cu.cu_transquant_bypass_flag) {
905 static const int qp_c[] = {
906 29, 30, 31, 32, 33, 33, 34, 34, 35, 35, 36, 36, 37, 37
909 static const uint8_t rem6[51 + 2 * 6 + 1] = {
910 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2,
911 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5,
912 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3,
915 static const uint8_t div6[51 + 2 * 6 + 1] = {
916 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 3, 3, 3,
917 3, 3, 3, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6,
918 7, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 10, 10, 10, 10,
923 qp = qp_y + s->ps.sps->qp_bd_offset;
928 offset = s->ps.pps->cb_qp_offset + s->sh.slice_cb_qp_offset;
930 offset = s->ps.pps->cr_qp_offset + s->sh.slice_cr_qp_offset;
932 qp_i = av_clip(qp_y + offset, -s->ps.sps->qp_bd_offset, 57);
938 qp = qp_c[qp_i - 30];
940 qp += s->ps.sps->qp_bd_offset;
943 shift = s->ps.sps->bit_depth + log2_trafo_size - 5;
944 add = 1 << (shift - 1);
945 scale = level_scale[rem6[qp]] << (div6[qp]);
946 scale_m = 16; // default when no custom scaling lists.
949 if (s->ps.sps->scaling_list_enable_flag) {
950 const ScalingList *sl = s->ps.pps->scaling_list_data_present_flag ?
951 &s->ps.pps->scaling_list : &s->ps.sps->scaling_list;
952 int matrix_id = lc->cu.pred_mode != MODE_INTRA;
954 if (log2_trafo_size != 5)
955 matrix_id = 3 * matrix_id + c_idx;
957 scale_matrix = sl->sl[log2_trafo_size - 2][matrix_id];
958 if (log2_trafo_size >= 4)
959 dc_scale = sl->sl_dc[log2_trafo_size - 4][matrix_id];
963 if (s->ps.pps->transform_skip_enabled_flag &&
964 !lc->cu.cu_transquant_bypass_flag &&
965 log2_trafo_size == 2) {
966 transform_skip_flag = ff_hevc_transform_skip_flag_decode(s, c_idx);
969 last_significant_coeff_x =
970 ff_hevc_last_significant_coeff_x_prefix_decode(s, c_idx, log2_trafo_size);
971 last_significant_coeff_y =
972 ff_hevc_last_significant_coeff_y_prefix_decode(s, c_idx, log2_trafo_size);
974 if (last_significant_coeff_x > 3) {
975 int suffix = ff_hevc_last_significant_coeff_suffix_decode(s, last_significant_coeff_x);
976 last_significant_coeff_x = (1 << ((last_significant_coeff_x >> 1) - 1)) *
977 (2 + (last_significant_coeff_x & 1)) +
981 if (last_significant_coeff_y > 3) {
982 int suffix = ff_hevc_last_significant_coeff_suffix_decode(s, last_significant_coeff_y);
983 last_significant_coeff_y = (1 << ((last_significant_coeff_y >> 1) - 1)) *
984 (2 + (last_significant_coeff_y & 1)) +
988 if (scan_idx == SCAN_VERT)
989 FFSWAP(int, last_significant_coeff_x, last_significant_coeff_y);
991 x_cg_last_sig = last_significant_coeff_x >> 2;
992 y_cg_last_sig = last_significant_coeff_y >> 2;
996 int last_x_c = last_significant_coeff_x & 3;
997 int last_y_c = last_significant_coeff_y & 3;
999 scan_x_off = ff_hevc_diag_scan4x4_x;
1000 scan_y_off = ff_hevc_diag_scan4x4_y;
1001 num_coeff = diag_scan4x4_inv[last_y_c][last_x_c];
1002 if (trafo_size == 4) {
1003 scan_x_cg = scan_1x1;
1004 scan_y_cg = scan_1x1;
1005 } else if (trafo_size == 8) {
1006 num_coeff += diag_scan2x2_inv[y_cg_last_sig][x_cg_last_sig] << 4;
1007 scan_x_cg = diag_scan2x2_x;
1008 scan_y_cg = diag_scan2x2_y;
1009 } else if (trafo_size == 16) {
1010 num_coeff += diag_scan4x4_inv[y_cg_last_sig][x_cg_last_sig] << 4;
1011 scan_x_cg = ff_hevc_diag_scan4x4_x;
1012 scan_y_cg = ff_hevc_diag_scan4x4_y;
1013 } else { // trafo_size == 32
1014 num_coeff += diag_scan8x8_inv[y_cg_last_sig][x_cg_last_sig] << 4;
1015 scan_x_cg = ff_hevc_diag_scan8x8_x;
1016 scan_y_cg = ff_hevc_diag_scan8x8_y;
1021 scan_x_cg = horiz_scan2x2_x;
1022 scan_y_cg = horiz_scan2x2_y;
1023 scan_x_off = horiz_scan4x4_x;
1024 scan_y_off = horiz_scan4x4_y;
1025 num_coeff = horiz_scan8x8_inv[last_significant_coeff_y][last_significant_coeff_x];
1027 default: //SCAN_VERT
1028 scan_x_cg = horiz_scan2x2_y;
1029 scan_y_cg = horiz_scan2x2_x;
1030 scan_x_off = horiz_scan4x4_y;
1031 scan_y_off = horiz_scan4x4_x;
1032 num_coeff = horiz_scan8x8_inv[last_significant_coeff_x][last_significant_coeff_y];
1036 num_last_subset = (num_coeff - 1) >> 4;
1038 for (i = num_last_subset; i >= 0; i--) {
1040 int x_cg, y_cg, x_c, y_c;
1041 int implicit_non_zero_coeff = 0;
1042 int64_t trans_coeff_level;
1044 int offset = i << 4;
1046 uint8_t significant_coeff_flag_idx[16];
1047 uint8_t nb_significant_coeff_flag = 0;
1049 x_cg = scan_x_cg[i];
1050 y_cg = scan_y_cg[i];
1052 if (i < num_last_subset && i > 0) {
1054 if (x_cg < (1 << (log2_trafo_size - 2)) - 1)
1055 ctx_cg += significant_coeff_group_flag[x_cg + 1][y_cg];
1056 if (y_cg < (1 << (log2_trafo_size - 2)) - 1)
1057 ctx_cg += significant_coeff_group_flag[x_cg][y_cg + 1];
1059 significant_coeff_group_flag[x_cg][y_cg] =
1060 ff_hevc_significant_coeff_group_flag_decode(s, c_idx, ctx_cg);
1061 implicit_non_zero_coeff = 1;
1063 significant_coeff_group_flag[x_cg][y_cg] =
1064 ((x_cg == x_cg_last_sig && y_cg == y_cg_last_sig) ||
1065 (x_cg == 0 && y_cg == 0));
1068 last_scan_pos = num_coeff - offset - 1;
1070 if (i == num_last_subset) {
1071 n_end = last_scan_pos - 1;
1072 significant_coeff_flag_idx[0] = last_scan_pos;
1073 nb_significant_coeff_flag = 1;
1078 if (x_cg < ((1 << log2_trafo_size) - 1) >> 2)
1079 prev_sig = significant_coeff_group_flag[x_cg + 1][y_cg];
1080 if (y_cg < ((1 << log2_trafo_size) - 1) >> 2)
1081 prev_sig += significant_coeff_group_flag[x_cg][y_cg + 1] << 1;
1083 for (n = n_end; n >= 0; n--) {
1084 GET_COORD(offset, n);
1086 if (significant_coeff_group_flag[x_cg][y_cg] &&
1087 (n > 0 || implicit_non_zero_coeff == 0)) {
1088 if (ff_hevc_significant_coeff_flag_decode(s, c_idx, x_c, y_c,
1092 significant_coeff_flag_idx[nb_significant_coeff_flag] = n;
1093 nb_significant_coeff_flag++;
1094 implicit_non_zero_coeff = 0;
1097 int last_cg = (x_c == (x_cg << 2) && y_c == (y_cg << 2));
1098 if (last_cg && implicit_non_zero_coeff && significant_coeff_group_flag[x_cg][y_cg]) {
1099 significant_coeff_flag_idx[nb_significant_coeff_flag] = n;
1100 nb_significant_coeff_flag++;
1105 n_end = nb_significant_coeff_flag;
1108 int first_nz_pos_in_cg = 16;
1109 int last_nz_pos_in_cg = -1;
1110 int c_rice_param = 0;
1111 int first_greater1_coeff_idx = -1;
1112 uint8_t coeff_abs_level_greater1_flag[16] = { 0 };
1113 uint16_t coeff_sign_flag;
1115 int sign_hidden = 0;
1117 // initialize first elem of coeff_bas_level_greater1_flag
1118 int ctx_set = (i > 0 && c_idx == 0) ? 2 : 0;
1120 if (!(i == num_last_subset) && greater1_ctx == 0)
1123 last_nz_pos_in_cg = significant_coeff_flag_idx[0];
1125 for (m = 0; m < (n_end > 8 ? 8 : n_end); m++) {
1126 int n_idx = significant_coeff_flag_idx[m];
1127 int inc = (ctx_set << 2) + greater1_ctx;
1128 coeff_abs_level_greater1_flag[n_idx] =
1129 ff_hevc_coeff_abs_level_greater1_flag_decode(s, c_idx, inc);
1130 if (coeff_abs_level_greater1_flag[n_idx]) {
1132 } else if (greater1_ctx > 0 && greater1_ctx < 3) {
1136 if (coeff_abs_level_greater1_flag[n_idx] &&
1137 first_greater1_coeff_idx == -1)
1138 first_greater1_coeff_idx = n_idx;
1140 first_nz_pos_in_cg = significant_coeff_flag_idx[n_end - 1];
1141 sign_hidden = last_nz_pos_in_cg - first_nz_pos_in_cg >= 4 &&
1142 !lc->cu.cu_transquant_bypass_flag;
1144 if (first_greater1_coeff_idx != -1) {
1145 coeff_abs_level_greater1_flag[first_greater1_coeff_idx] += ff_hevc_coeff_abs_level_greater2_flag_decode(s, c_idx, ctx_set);
1147 if (!s->ps.pps->sign_data_hiding_flag || !sign_hidden) {
1148 coeff_sign_flag = ff_hevc_coeff_sign_flag(s, nb_significant_coeff_flag) << (16 - nb_significant_coeff_flag);
1150 coeff_sign_flag = ff_hevc_coeff_sign_flag(s, nb_significant_coeff_flag - 1) << (16 - (nb_significant_coeff_flag - 1));
1153 for (m = 0; m < n_end; m++) {
1154 n = significant_coeff_flag_idx[m];
1155 GET_COORD(offset, n);
1156 trans_coeff_level = 1 + coeff_abs_level_greater1_flag[n];
1157 if (trans_coeff_level == ((m < 8) ?
1158 ((n == first_greater1_coeff_idx) ? 3 : 2) : 1)) {
1159 int last_coeff_abs_level_remaining = ff_hevc_coeff_abs_level_remaining(s, trans_coeff_level, c_rice_param);
1161 trans_coeff_level += last_coeff_abs_level_remaining;
1162 if ((trans_coeff_level) > (3 * (1 << c_rice_param)))
1163 c_rice_param = FFMIN(c_rice_param + 1, 4);
1165 if (s->ps.pps->sign_data_hiding_flag && sign_hidden) {
1166 sum_abs += trans_coeff_level;
1167 if (n == first_nz_pos_in_cg && ((sum_abs & 1) == 1))
1168 trans_coeff_level = -trans_coeff_level;
1170 if (coeff_sign_flag >> 15)
1171 trans_coeff_level = -trans_coeff_level;
1172 coeff_sign_flag <<= 1;
1173 if (!lc->cu.cu_transquant_bypass_flag) {
1174 if (s->ps.sps->scaling_list_enable_flag) {
1175 if (y_c || x_c || log2_trafo_size < 4) {
1177 switch (log2_trafo_size) {
1178 case 3: pos = (y_c << 3) + x_c; break;
1179 case 4: pos = ((y_c >> 1) << 3) + (x_c >> 1); break;
1180 case 5: pos = ((y_c >> 2) << 3) + (x_c >> 2); break;
1181 default: pos = (y_c << 2) + x_c;
1183 scale_m = scale_matrix[pos];
1188 trans_coeff_level = (trans_coeff_level * (int64_t)scale * (int64_t)scale_m + add) >> shift;
1189 if(trans_coeff_level < 0) {
1190 if((~trans_coeff_level) & 0xFffffffffff8000)
1191 trans_coeff_level = -32768;
1193 if (trans_coeff_level & 0xffffffffffff8000)
1194 trans_coeff_level = 32767;
1197 coeffs[y_c * trafo_size + x_c] = trans_coeff_level;
1202 if (lc->cu.cu_transquant_bypass_flag) {
1203 s->hevcdsp.transquant_bypass[log2_trafo_size - 2](dst, coeffs, stride);
1205 if (transform_skip_flag)
1206 s->hevcdsp.transform_skip(dst, coeffs, stride);
1207 else if (lc->cu.pred_mode == MODE_INTRA && c_idx == 0 &&
1208 log2_trafo_size == 2)
1209 s->hevcdsp.transform_4x4_luma_add(dst, coeffs, stride);
1211 s->hevcdsp.transform_add[log2_trafo_size - 2](dst, coeffs, stride);
1215 static int hls_transform_unit(HEVCContext *s, int x0, int y0,
1216 int xBase, int yBase, int cb_xBase, int cb_yBase,
1217 int log2_cb_size, int log2_trafo_size,
1218 int blk_idx, int cbf_luma, int cbf_cb, int cbf_cr)
1220 HEVCLocalContext *lc = &s->HEVClc;
1222 if (lc->cu.pred_mode == MODE_INTRA) {
1223 int trafo_size = 1 << log2_trafo_size;
1224 ff_hevc_set_neighbour_available(s, x0, y0, trafo_size, trafo_size);
1226 s->hpc.intra_pred[log2_trafo_size - 2](s, x0, y0, 0);
1227 if (log2_trafo_size > 2) {
1228 trafo_size = trafo_size << (s->ps.sps->hshift[1] - 1);
1229 ff_hevc_set_neighbour_available(s, x0, y0, trafo_size, trafo_size);
1230 s->hpc.intra_pred[log2_trafo_size - 3](s, x0, y0, 1);
1231 s->hpc.intra_pred[log2_trafo_size - 3](s, x0, y0, 2);
1232 } else if (blk_idx == 3) {
1233 trafo_size = trafo_size << s->ps.sps->hshift[1];
1234 ff_hevc_set_neighbour_available(s, xBase, yBase,
1235 trafo_size, trafo_size);
1236 s->hpc.intra_pred[log2_trafo_size - 2](s, xBase, yBase, 1);
1237 s->hpc.intra_pred[log2_trafo_size - 2](s, xBase, yBase, 2);
1241 if (cbf_luma || cbf_cb || cbf_cr) {
1242 int scan_idx = SCAN_DIAG;
1243 int scan_idx_c = SCAN_DIAG;
1245 if (s->ps.pps->cu_qp_delta_enabled_flag && !lc->tu.is_cu_qp_delta_coded) {
1246 lc->tu.cu_qp_delta = ff_hevc_cu_qp_delta_abs(s);
1247 if (lc->tu.cu_qp_delta != 0)
1248 if (ff_hevc_cu_qp_delta_sign_flag(s) == 1)
1249 lc->tu.cu_qp_delta = -lc->tu.cu_qp_delta;
1250 lc->tu.is_cu_qp_delta_coded = 1;
1252 if (lc->tu.cu_qp_delta < -(26 + s->ps.sps->qp_bd_offset / 2) ||
1253 lc->tu.cu_qp_delta > (25 + s->ps.sps->qp_bd_offset / 2)) {
1254 av_log(s->avctx, AV_LOG_ERROR,
1255 "The cu_qp_delta %d is outside the valid range "
1258 -(26 + s->ps.sps->qp_bd_offset / 2),
1259 (25 + s->ps.sps->qp_bd_offset / 2));
1260 return AVERROR_INVALIDDATA;
1263 ff_hevc_set_qPy(s, x0, y0, cb_xBase, cb_yBase, log2_cb_size);
1266 if (lc->cu.pred_mode == MODE_INTRA && log2_trafo_size < 4) {
1267 if (lc->tu.cur_intra_pred_mode >= 6 &&
1268 lc->tu.cur_intra_pred_mode <= 14) {
1269 scan_idx = SCAN_VERT;
1270 } else if (lc->tu.cur_intra_pred_mode >= 22 &&
1271 lc->tu.cur_intra_pred_mode <= 30) {
1272 scan_idx = SCAN_HORIZ;
1275 if (lc->pu.intra_pred_mode_c >= 6 &&
1276 lc->pu.intra_pred_mode_c <= 14) {
1277 scan_idx_c = SCAN_VERT;
1278 } else if (lc->pu.intra_pred_mode_c >= 22 &&
1279 lc->pu.intra_pred_mode_c <= 30) {
1280 scan_idx_c = SCAN_HORIZ;
1285 hls_residual_coding(s, x0, y0, log2_trafo_size, scan_idx, 0);
1286 if (log2_trafo_size > 2) {
1288 hls_residual_coding(s, x0, y0, log2_trafo_size - 1, scan_idx_c, 1);
1290 hls_residual_coding(s, x0, y0, log2_trafo_size - 1, scan_idx_c, 2);
1291 } else if (blk_idx == 3) {
1293 hls_residual_coding(s, xBase, yBase, log2_trafo_size, scan_idx_c, 1);
1295 hls_residual_coding(s, xBase, yBase, log2_trafo_size, scan_idx_c, 2);
1301 static void set_deblocking_bypass(HEVCContext *s, int x0, int y0, int log2_cb_size)
1303 int cb_size = 1 << log2_cb_size;
1304 int log2_min_pu_size = s->ps.sps->log2_min_pu_size;
1306 int min_pu_width = s->ps.sps->min_pu_width;
1307 int x_end = FFMIN(x0 + cb_size, s->ps.sps->width);
1308 int y_end = FFMIN(y0 + cb_size, s->ps.sps->height);
1311 for (j = (y0 >> log2_min_pu_size); j < (y_end >> log2_min_pu_size); j++)
1312 for (i = (x0 >> log2_min_pu_size); i < (x_end >> log2_min_pu_size); i++)
1313 s->is_pcm[i + j * min_pu_width] = 2;
1316 static int hls_transform_tree(HEVCContext *s, int x0, int y0,
1317 int xBase, int yBase, int cb_xBase, int cb_yBase,
1318 int log2_cb_size, int log2_trafo_size,
1319 int trafo_depth, int blk_idx,
1320 int cbf_cb, int cbf_cr)
1322 HEVCLocalContext *lc = &s->HEVClc;
1323 uint8_t split_transform_flag;
1326 if (lc->cu.intra_split_flag) {
1327 if (trafo_depth == 1)
1328 lc->tu.cur_intra_pred_mode = lc->pu.intra_pred_mode[blk_idx];
1330 lc->tu.cur_intra_pred_mode = lc->pu.intra_pred_mode[0];
1333 if (log2_trafo_size <= s->ps.sps->log2_max_trafo_size &&
1334 log2_trafo_size > s->ps.sps->log2_min_tb_size &&
1335 trafo_depth < lc->cu.max_trafo_depth &&
1336 !(lc->cu.intra_split_flag && trafo_depth == 0)) {
1337 split_transform_flag = ff_hevc_split_transform_flag_decode(s, log2_trafo_size);
1339 int inter_split = s->ps.sps->max_transform_hierarchy_depth_inter == 0 &&
1340 lc->cu.pred_mode == MODE_INTER &&
1341 lc->cu.part_mode != PART_2Nx2N &&
1344 split_transform_flag = log2_trafo_size > s->ps.sps->log2_max_trafo_size ||
1345 (lc->cu.intra_split_flag && trafo_depth == 0) ||
1349 if (log2_trafo_size > 2 && (trafo_depth == 0 || cbf_cb))
1350 cbf_cb = ff_hevc_cbf_cb_cr_decode(s, trafo_depth);
1351 else if (log2_trafo_size > 2 || trafo_depth == 0)
1353 if (log2_trafo_size > 2 && (trafo_depth == 0 || cbf_cr))
1354 cbf_cr = ff_hevc_cbf_cb_cr_decode(s, trafo_depth);
1355 else if (log2_trafo_size > 2 || trafo_depth == 0)
1358 if (split_transform_flag) {
1359 const int trafo_size_split = 1 << (log2_trafo_size - 1);
1360 const int x1 = x0 + trafo_size_split;
1361 const int y1 = y0 + trafo_size_split;
1363 #define SUBDIVIDE(x, y, idx) \
1365 ret = hls_transform_tree(s, x, y, x0, y0, cb_xBase, cb_yBase, log2_cb_size, \
1366 log2_trafo_size - 1, trafo_depth + 1, idx, \
1372 SUBDIVIDE(x0, y0, 0);
1373 SUBDIVIDE(x1, y0, 1);
1374 SUBDIVIDE(x0, y1, 2);
1375 SUBDIVIDE(x1, y1, 3);
1379 int min_tu_size = 1 << s->ps.sps->log2_min_tb_size;
1380 int log2_min_tu_size = s->ps.sps->log2_min_tb_size;
1381 int min_tu_width = s->ps.sps->min_tb_width;
1384 if (lc->cu.pred_mode == MODE_INTRA || trafo_depth != 0 ||
1386 cbf_luma = ff_hevc_cbf_luma_decode(s, trafo_depth);
1388 ret = hls_transform_unit(s, x0, y0, xBase, yBase, cb_xBase, cb_yBase,
1389 log2_cb_size, log2_trafo_size,
1390 blk_idx, cbf_luma, cbf_cb, cbf_cr);
1393 // TODO: store cbf_luma somewhere else
1396 for (i = 0; i < (1 << log2_trafo_size); i += min_tu_size)
1397 for (j = 0; j < (1 << log2_trafo_size); j += min_tu_size) {
1398 int x_tu = (x0 + j) >> log2_min_tu_size;
1399 int y_tu = (y0 + i) >> log2_min_tu_size;
1400 s->cbf_luma[y_tu * min_tu_width + x_tu] = 1;
1403 if (!s->sh.disable_deblocking_filter_flag) {
1404 ff_hevc_deblocking_boundary_strengths(s, x0, y0, log2_trafo_size);
1405 if (s->ps.pps->transquant_bypass_enable_flag &&
1406 lc->cu.cu_transquant_bypass_flag)
1407 set_deblocking_bypass(s, x0, y0, log2_trafo_size);
1413 static int hls_pcm_sample(HEVCContext *s, int x0, int y0, int log2_cb_size)
1415 //TODO: non-4:2:0 support
1416 HEVCLocalContext *lc = &s->HEVClc;
1418 int cb_size = 1 << log2_cb_size;
1419 int stride0 = s->frame->linesize[0];
1420 uint8_t *dst0 = &s->frame->data[0][y0 * stride0 + (x0 << s->ps.sps->pixel_shift)];
1421 int stride1 = s->frame->linesize[1];
1422 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)];
1423 int stride2 = s->frame->linesize[2];
1424 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)];
1426 int length = cb_size * cb_size * s->ps.sps->pcm.bit_depth + ((cb_size * cb_size) >> 1) * s->ps.sps->pcm.bit_depth_chroma;
1427 const uint8_t *pcm = skip_bytes(&lc->cc, (length + 7) >> 3);
1430 if (!s->sh.disable_deblocking_filter_flag)
1431 ff_hevc_deblocking_boundary_strengths(s, x0, y0, log2_cb_size);
1433 ret = init_get_bits(&gb, pcm, length);
1437 s->hevcdsp.put_pcm(dst0, stride0, cb_size, &gb, s->ps.sps->pcm.bit_depth);
1438 s->hevcdsp.put_pcm(dst1, stride1, cb_size / 2, &gb, s->ps.sps->pcm.bit_depth_chroma);
1439 s->hevcdsp.put_pcm(dst2, stride2, cb_size / 2, &gb, s->ps.sps->pcm.bit_depth_chroma);
1443 static void hls_mvd_coding(HEVCContext *s, int x0, int y0, int log2_cb_size)
1445 HEVCLocalContext *lc = &s->HEVClc;
1446 int x = ff_hevc_abs_mvd_greater0_flag_decode(s);
1447 int y = ff_hevc_abs_mvd_greater0_flag_decode(s);
1450 x += ff_hevc_abs_mvd_greater1_flag_decode(s);
1452 y += ff_hevc_abs_mvd_greater1_flag_decode(s);
1455 case 2: lc->pu.mvd.x = ff_hevc_mvd_decode(s); break;
1456 case 1: lc->pu.mvd.x = ff_hevc_mvd_sign_flag_decode(s); break;
1457 case 0: lc->pu.mvd.x = 0; break;
1461 case 2: lc->pu.mvd.y = ff_hevc_mvd_decode(s); break;
1462 case 1: lc->pu.mvd.y = ff_hevc_mvd_sign_flag_decode(s); break;
1463 case 0: lc->pu.mvd.y = 0; break;
1468 * 8.5.3.2.2.1 Luma sample interpolation process
1470 * @param s HEVC decoding context
1471 * @param dst target buffer for block data at block position
1472 * @param dststride stride of the dst buffer
1473 * @param ref reference picture buffer at origin (0, 0)
1474 * @param mv motion vector (relative to block position) to get pixel data from
1475 * @param x_off horizontal position of block from origin (0, 0)
1476 * @param y_off vertical position of block from origin (0, 0)
1477 * @param block_w width of block
1478 * @param block_h height of block
1480 static void luma_mc(HEVCContext *s, int16_t *dst, ptrdiff_t dststride,
1481 AVFrame *ref, const Mv *mv, int x_off, int y_off,
1482 int block_w, int block_h, int pred_idx)
1484 HEVCLocalContext *lc = &s->HEVClc;
1485 uint8_t *src = ref->data[0];
1486 ptrdiff_t srcstride = ref->linesize[0];
1487 int pic_width = s->ps.sps->width;
1488 int pic_height = s->ps.sps->height;
1492 int extra_left = ff_hevc_qpel_extra_before[mx];
1493 int extra_top = ff_hevc_qpel_extra_before[my];
1495 x_off += mv->x >> 2;
1496 y_off += mv->y >> 2;
1497 src += y_off * srcstride + (x_off * (1 << s->ps.sps->pixel_shift));
1499 if (x_off < extra_left || y_off < extra_top ||
1500 x_off >= pic_width - block_w - ff_hevc_qpel_extra_after[mx] ||
1501 y_off >= pic_height - block_h - ff_hevc_qpel_extra_after[my]) {
1502 const int edge_emu_stride = EDGE_EMU_BUFFER_STRIDE << s->ps.sps->pixel_shift;
1503 int offset = extra_top * srcstride + (extra_left << s->ps.sps->pixel_shift);
1504 int buf_offset = extra_top *
1505 edge_emu_stride + (extra_left << s->ps.sps->pixel_shift);
1507 s->vdsp.emulated_edge_mc(lc->edge_emu_buffer, src - offset,
1508 edge_emu_stride, srcstride,
1509 block_w + ff_hevc_qpel_extra[mx],
1510 block_h + ff_hevc_qpel_extra[my],
1511 x_off - extra_left, y_off - extra_top,
1512 pic_width, pic_height);
1513 src = lc->edge_emu_buffer + buf_offset;
1514 srcstride = edge_emu_stride;
1516 s->hevcdsp.put_hevc_qpel[!!my][!!mx][pred_idx](dst, dststride, src, srcstride,
1517 block_h, mx, my, lc->mc_buffer);
1521 * 8.5.3.2.2.2 Chroma sample interpolation process
1523 * @param s HEVC decoding context
1524 * @param dst1 target buffer for block data at block position (U plane)
1525 * @param dst2 target buffer for block data at block position (V plane)
1526 * @param dststride stride of the dst1 and dst2 buffers
1527 * @param ref reference picture buffer at origin (0, 0)
1528 * @param mv motion vector (relative to block position) to get pixel data from
1529 * @param x_off horizontal position of block from origin (0, 0)
1530 * @param y_off vertical position of block from origin (0, 0)
1531 * @param block_w width of block
1532 * @param block_h height of block
1534 static void chroma_mc(HEVCContext *s, int16_t *dst1, int16_t *dst2,
1535 ptrdiff_t dststride, AVFrame *ref, const Mv *mv,
1536 int x_off, int y_off, int block_w, int block_h)
1538 HEVCLocalContext *lc = &s->HEVClc;
1539 uint8_t *src1 = ref->data[1];
1540 uint8_t *src2 = ref->data[2];
1541 ptrdiff_t src1stride = ref->linesize[1];
1542 ptrdiff_t src2stride = ref->linesize[2];
1543 int pic_width = s->ps.sps->width >> 1;
1544 int pic_height = s->ps.sps->height >> 1;
1549 x_off += mv->x >> 3;
1550 y_off += mv->y >> 3;
1551 src1 += y_off * src1stride + (x_off * (1 << s->ps.sps->pixel_shift));
1552 src2 += y_off * src2stride + (x_off * (1 << s->ps.sps->pixel_shift));
1554 if (x_off < EPEL_EXTRA_BEFORE || y_off < EPEL_EXTRA_AFTER ||
1555 x_off >= pic_width - block_w - EPEL_EXTRA_AFTER ||
1556 y_off >= pic_height - block_h - EPEL_EXTRA_AFTER) {
1557 const int edge_emu_stride = EDGE_EMU_BUFFER_STRIDE << s->ps.sps->pixel_shift;
1558 int offset1 = EPEL_EXTRA_BEFORE * (src1stride + (1 << s->ps.sps->pixel_shift));
1559 int buf_offset1 = EPEL_EXTRA_BEFORE *
1560 (edge_emu_stride + (1 << s->ps.sps->pixel_shift));
1561 int offset2 = EPEL_EXTRA_BEFORE * (src2stride + (1 << s->ps.sps->pixel_shift));
1562 int buf_offset2 = EPEL_EXTRA_BEFORE *
1563 (edge_emu_stride + (1 << s->ps.sps->pixel_shift));
1565 s->vdsp.emulated_edge_mc(lc->edge_emu_buffer, src1 - offset1,
1566 edge_emu_stride, src1stride,
1567 block_w + EPEL_EXTRA, block_h + EPEL_EXTRA,
1568 x_off - EPEL_EXTRA_BEFORE,
1569 y_off - EPEL_EXTRA_BEFORE,
1570 pic_width, pic_height);
1572 src1 = lc->edge_emu_buffer + buf_offset1;
1573 src1stride = edge_emu_stride;
1574 s->hevcdsp.put_hevc_epel[!!my][!!mx](dst1, dststride, src1, src1stride,
1575 block_w, block_h, mx, my, lc->mc_buffer);
1577 s->vdsp.emulated_edge_mc(lc->edge_emu_buffer, src2 - offset2,
1578 edge_emu_stride, src2stride,
1579 block_w + EPEL_EXTRA, block_h + EPEL_EXTRA,
1580 x_off - EPEL_EXTRA_BEFORE,
1581 y_off - EPEL_EXTRA_BEFORE,
1582 pic_width, pic_height);
1583 src2 = lc->edge_emu_buffer + buf_offset2;
1584 src2stride = edge_emu_stride;
1586 s->hevcdsp.put_hevc_epel[!!my][!!mx](dst2, dststride, src2, src2stride,
1587 block_w, block_h, mx, my,
1590 s->hevcdsp.put_hevc_epel[!!my][!!mx](dst1, dststride, src1, src1stride,
1591 block_w, block_h, mx, my,
1593 s->hevcdsp.put_hevc_epel[!!my][!!mx](dst2, dststride, src2, src2stride,
1594 block_w, block_h, mx, my,
1599 static void hevc_await_progress(HEVCContext *s, HEVCFrame *ref,
1600 const Mv *mv, int y0, int height)
1602 int y = (mv->y >> 2) + y0 + height + 9;
1603 ff_thread_await_progress(&ref->tf, y, 0);
1606 static void hevc_luma_mv_mpv_mode(HEVCContext *s, int x0, int y0, int nPbW,
1607 int nPbH, int log2_cb_size, int part_idx,
1608 int merge_idx, MvField *mv)
1610 HEVCLocalContext *lc = &s->HEVClc;
1611 enum InterPredIdc inter_pred_idc = PRED_L0;
1614 ff_hevc_set_neighbour_available(s, x0, y0, nPbW, nPbH);
1615 if (s->sh.slice_type == B_SLICE)
1616 inter_pred_idc = ff_hevc_inter_pred_idc_decode(s, nPbW, nPbH);
1618 if (inter_pred_idc != PRED_L1) {
1619 if (s->sh.nb_refs[L0])
1620 mv->ref_idx[0]= ff_hevc_ref_idx_lx_decode(s, s->sh.nb_refs[L0]);
1622 mv->pred_flag[0] = 1;
1623 hls_mvd_coding(s, x0, y0, 0);
1624 mvp_flag = ff_hevc_mvp_lx_flag_decode(s);
1625 ff_hevc_luma_mv_mvp_mode(s, x0, y0, nPbW, nPbH, log2_cb_size,
1626 part_idx, merge_idx, mv, mvp_flag, 0);
1627 mv->mv[0].x += lc->pu.mvd.x;
1628 mv->mv[0].y += lc->pu.mvd.y;
1631 if (inter_pred_idc != PRED_L0) {
1632 if (s->sh.nb_refs[L1])
1633 mv->ref_idx[1]= ff_hevc_ref_idx_lx_decode(s, s->sh.nb_refs[L1]);
1635 if (s->sh.mvd_l1_zero_flag == 1 && inter_pred_idc == PRED_BI) {
1636 AV_ZERO32(&lc->pu.mvd);
1638 hls_mvd_coding(s, x0, y0, 1);
1641 mv->pred_flag[1] = 1;
1642 mvp_flag = ff_hevc_mvp_lx_flag_decode(s);
1643 ff_hevc_luma_mv_mvp_mode(s, x0, y0, nPbW, nPbH, log2_cb_size,
1644 part_idx, merge_idx, mv, mvp_flag, 1);
1645 mv->mv[1].x += lc->pu.mvd.x;
1646 mv->mv[1].y += lc->pu.mvd.y;
1650 static void hls_prediction_unit(HEVCContext *s, int x0, int y0,
1652 int log2_cb_size, int partIdx)
1654 static const int pred_indices[] = {
1655 [4] = 0, [8] = 1, [12] = 2, [16] = 3, [24] = 4, [32] = 5, [48] = 6, [64] = 7,
1657 const int pred_idx = pred_indices[nPbW];
1659 #define POS(c_idx, x, y) \
1660 &s->frame->data[c_idx][((y) >> s->ps.sps->vshift[c_idx]) * s->frame->linesize[c_idx] + \
1661 (((x) >> s->ps.sps->hshift[c_idx]) << s->ps.sps->pixel_shift)]
1662 HEVCLocalContext *lc = &s->HEVClc;
1664 struct MvField current_mv = {{{ 0 }}};
1666 int min_pu_width = s->ps.sps->min_pu_width;
1668 MvField *tab_mvf = s->ref->tab_mvf;
1669 RefPicList *refPicList = s->ref->refPicList;
1670 HEVCFrame *ref0, *ref1;
1672 int tmpstride = MAX_PB_SIZE;
1674 uint8_t *dst0 = POS(0, x0, y0);
1675 uint8_t *dst1 = POS(1, x0, y0);
1676 uint8_t *dst2 = POS(2, x0, y0);
1677 int log2_min_cb_size = s->ps.sps->log2_min_cb_size;
1678 int min_cb_width = s->ps.sps->min_cb_width;
1679 int x_cb = x0 >> log2_min_cb_size;
1680 int y_cb = y0 >> log2_min_cb_size;
1684 int skip_flag = SAMPLE_CTB(s->skip_flag, x_cb, y_cb);
1687 lc->pu.merge_flag = ff_hevc_merge_flag_decode(s);
1689 if (skip_flag || lc->pu.merge_flag) {
1690 if (s->sh.max_num_merge_cand > 1)
1691 merge_idx = ff_hevc_merge_idx_decode(s);
1695 ff_hevc_luma_mv_merge_mode(s, x0, y0, nPbW, nPbH, log2_cb_size,
1696 partIdx, merge_idx, ¤t_mv);
1698 hevc_luma_mv_mpv_mode(s, x0, y0, nPbW, nPbH, log2_cb_size,
1699 partIdx, merge_idx, ¤t_mv);
1702 x_pu = x0 >> s->ps.sps->log2_min_pu_size;
1703 y_pu = y0 >> s->ps.sps->log2_min_pu_size;
1705 for (j = 0; j < nPbH >> s->ps.sps->log2_min_pu_size; j++)
1706 for (i = 0; i < nPbW >> s->ps.sps->log2_min_pu_size; i++)
1707 tab_mvf[(y_pu + j) * min_pu_width + x_pu + i] = current_mv;
1709 if (current_mv.pred_flag[0]) {
1710 ref0 = refPicList[0].ref[current_mv.ref_idx[0]];
1713 hevc_await_progress(s, ref0, ¤t_mv.mv[0], y0, nPbH);
1715 if (current_mv.pred_flag[1]) {
1716 ref1 = refPicList[1].ref[current_mv.ref_idx[1]];
1719 hevc_await_progress(s, ref1, ¤t_mv.mv[1], y0, nPbH);
1722 if (current_mv.pred_flag[0] && !current_mv.pred_flag[1]) {
1723 DECLARE_ALIGNED(16, int16_t, tmp[MAX_PB_SIZE * MAX_PB_SIZE]);
1724 DECLARE_ALIGNED(16, int16_t, tmp2[MAX_PB_SIZE * MAX_PB_SIZE]);
1726 luma_mc(s, tmp, tmpstride, ref0->frame,
1727 ¤t_mv.mv[0], x0, y0, nPbW, nPbH, pred_idx);
1729 if ((s->sh.slice_type == P_SLICE && s->ps.pps->weighted_pred_flag) ||
1730 (s->sh.slice_type == B_SLICE && s->ps.pps->weighted_bipred_flag)) {
1731 s->hevcdsp.weighted_pred(s->sh.luma_log2_weight_denom,
1732 s->sh.luma_weight_l0[current_mv.ref_idx[0]],
1733 s->sh.luma_offset_l0[current_mv.ref_idx[0]],
1734 dst0, s->frame->linesize[0], tmp,
1735 tmpstride, nPbW, nPbH);
1737 s->hevcdsp.put_unweighted_pred(dst0, s->frame->linesize[0], tmp, tmpstride, nPbW, nPbH);
1739 chroma_mc(s, tmp, tmp2, tmpstride, ref0->frame,
1740 ¤t_mv.mv[0], x0 / 2, y0 / 2, nPbW / 2, nPbH / 2);
1742 if ((s->sh.slice_type == P_SLICE && s->ps.pps->weighted_pred_flag) ||
1743 (s->sh.slice_type == B_SLICE && s->ps.pps->weighted_bipred_flag)) {
1744 s->hevcdsp.weighted_pred(s->sh.chroma_log2_weight_denom,
1745 s->sh.chroma_weight_l0[current_mv.ref_idx[0]][0],
1746 s->sh.chroma_offset_l0[current_mv.ref_idx[0]][0],
1747 dst1, s->frame->linesize[1], tmp, tmpstride,
1748 nPbW / 2, nPbH / 2);
1749 s->hevcdsp.weighted_pred(s->sh.chroma_log2_weight_denom,
1750 s->sh.chroma_weight_l0[current_mv.ref_idx[0]][1],
1751 s->sh.chroma_offset_l0[current_mv.ref_idx[0]][1],
1752 dst2, s->frame->linesize[2], tmp2, tmpstride,
1753 nPbW / 2, nPbH / 2);
1755 s->hevcdsp.put_unweighted_pred(dst1, s->frame->linesize[1], tmp, tmpstride, nPbW/2, nPbH/2);
1756 s->hevcdsp.put_unweighted_pred(dst2, s->frame->linesize[2], tmp2, tmpstride, nPbW/2, nPbH/2);
1758 } else if (!current_mv.pred_flag[0] && current_mv.pred_flag[1]) {
1759 DECLARE_ALIGNED(16, int16_t, tmp [MAX_PB_SIZE * MAX_PB_SIZE]);
1760 DECLARE_ALIGNED(16, int16_t, tmp2[MAX_PB_SIZE * MAX_PB_SIZE]);
1762 luma_mc(s, tmp, tmpstride, ref1->frame,
1763 ¤t_mv.mv[1], x0, y0, nPbW, nPbH, pred_idx);
1765 if ((s->sh.slice_type == P_SLICE && s->ps.pps->weighted_pred_flag) ||
1766 (s->sh.slice_type == B_SLICE && s->ps.pps->weighted_bipred_flag)) {
1767 s->hevcdsp.weighted_pred(s->sh.luma_log2_weight_denom,
1768 s->sh.luma_weight_l1[current_mv.ref_idx[1]],
1769 s->sh.luma_offset_l1[current_mv.ref_idx[1]],
1770 dst0, s->frame->linesize[0], tmp, tmpstride,
1773 s->hevcdsp.put_unweighted_pred(dst0, s->frame->linesize[0], tmp, tmpstride, nPbW, nPbH);
1776 chroma_mc(s, tmp, tmp2, tmpstride, ref1->frame,
1777 ¤t_mv.mv[1], x0/2, y0/2, nPbW/2, nPbH/2);
1779 if ((s->sh.slice_type == P_SLICE && s->ps.pps->weighted_pred_flag) ||
1780 (s->sh.slice_type == B_SLICE && s->ps.pps->weighted_bipred_flag)) {
1781 s->hevcdsp.weighted_pred(s->sh.chroma_log2_weight_denom,
1782 s->sh.chroma_weight_l1[current_mv.ref_idx[1]][0],
1783 s->sh.chroma_offset_l1[current_mv.ref_idx[1]][0],
1784 dst1, s->frame->linesize[1], tmp, tmpstride, nPbW/2, nPbH/2);
1785 s->hevcdsp.weighted_pred(s->sh.chroma_log2_weight_denom,
1786 s->sh.chroma_weight_l1[current_mv.ref_idx[1]][1],
1787 s->sh.chroma_offset_l1[current_mv.ref_idx[1]][1],
1788 dst2, s->frame->linesize[2], tmp2, tmpstride, nPbW/2, nPbH/2);
1790 s->hevcdsp.put_unweighted_pred(dst1, s->frame->linesize[1], tmp, tmpstride, nPbW/2, nPbH/2);
1791 s->hevcdsp.put_unweighted_pred(dst2, s->frame->linesize[2], tmp2, tmpstride, nPbW/2, nPbH/2);
1793 } else if (current_mv.pred_flag[0] && current_mv.pred_flag[1]) {
1794 DECLARE_ALIGNED(16, int16_t, tmp [MAX_PB_SIZE * MAX_PB_SIZE]);
1795 DECLARE_ALIGNED(16, int16_t, tmp2[MAX_PB_SIZE * MAX_PB_SIZE]);
1796 DECLARE_ALIGNED(16, int16_t, tmp3[MAX_PB_SIZE * MAX_PB_SIZE]);
1797 DECLARE_ALIGNED(16, int16_t, tmp4[MAX_PB_SIZE * MAX_PB_SIZE]);
1799 luma_mc(s, tmp, tmpstride, ref0->frame,
1800 ¤t_mv.mv[0], x0, y0, nPbW, nPbH, pred_idx);
1801 luma_mc(s, tmp2, tmpstride, ref1->frame,
1802 ¤t_mv.mv[1], x0, y0, nPbW, nPbH, pred_idx);
1804 if ((s->sh.slice_type == P_SLICE && s->ps.pps->weighted_pred_flag) ||
1805 (s->sh.slice_type == B_SLICE && s->ps.pps->weighted_bipred_flag)) {
1806 s->hevcdsp.weighted_pred_avg(s->sh.luma_log2_weight_denom,
1807 s->sh.luma_weight_l0[current_mv.ref_idx[0]],
1808 s->sh.luma_weight_l1[current_mv.ref_idx[1]],
1809 s->sh.luma_offset_l0[current_mv.ref_idx[0]],
1810 s->sh.luma_offset_l1[current_mv.ref_idx[1]],
1811 dst0, s->frame->linesize[0],
1812 tmp, tmp2, tmpstride, nPbW, nPbH);
1814 s->hevcdsp.put_unweighted_pred_avg(dst0, s->frame->linesize[0],
1815 tmp, tmp2, tmpstride, nPbW, nPbH);
1818 chroma_mc(s, tmp, tmp2, tmpstride, ref0->frame,
1819 ¤t_mv.mv[0], x0 / 2, y0 / 2, nPbW / 2, nPbH / 2);
1820 chroma_mc(s, tmp3, tmp4, tmpstride, ref1->frame,
1821 ¤t_mv.mv[1], x0 / 2, y0 / 2, nPbW / 2, nPbH / 2);
1823 if ((s->sh.slice_type == P_SLICE && s->ps.pps->weighted_pred_flag) ||
1824 (s->sh.slice_type == B_SLICE && s->ps.pps->weighted_bipred_flag)) {
1825 s->hevcdsp.weighted_pred_avg(s->sh.chroma_log2_weight_denom,
1826 s->sh.chroma_weight_l0[current_mv.ref_idx[0]][0],
1827 s->sh.chroma_weight_l1[current_mv.ref_idx[1]][0],
1828 s->sh.chroma_offset_l0[current_mv.ref_idx[0]][0],
1829 s->sh.chroma_offset_l1[current_mv.ref_idx[1]][0],
1830 dst1, s->frame->linesize[1], tmp, tmp3,
1831 tmpstride, nPbW / 2, nPbH / 2);
1832 s->hevcdsp.weighted_pred_avg(s->sh.chroma_log2_weight_denom,
1833 s->sh.chroma_weight_l0[current_mv.ref_idx[0]][1],
1834 s->sh.chroma_weight_l1[current_mv.ref_idx[1]][1],
1835 s->sh.chroma_offset_l0[current_mv.ref_idx[0]][1],
1836 s->sh.chroma_offset_l1[current_mv.ref_idx[1]][1],
1837 dst2, s->frame->linesize[2], tmp2, tmp4,
1838 tmpstride, nPbW / 2, nPbH / 2);
1840 s->hevcdsp.put_unweighted_pred_avg(dst1, s->frame->linesize[1], tmp, tmp3, tmpstride, nPbW/2, nPbH/2);
1841 s->hevcdsp.put_unweighted_pred_avg(dst2, s->frame->linesize[2], tmp2, tmp4, tmpstride, nPbW/2, nPbH/2);
1849 static int luma_intra_pred_mode(HEVCContext *s, int x0, int y0, int pu_size,
1850 int prev_intra_luma_pred_flag)
1852 HEVCLocalContext *lc = &s->HEVClc;
1853 int x_pu = x0 >> s->ps.sps->log2_min_pu_size;
1854 int y_pu = y0 >> s->ps.sps->log2_min_pu_size;
1855 int min_pu_width = s->ps.sps->min_pu_width;
1856 int size_in_pus = pu_size >> s->ps.sps->log2_min_pu_size;
1857 int x0b = x0 & ((1 << s->ps.sps->log2_ctb_size) - 1);
1858 int y0b = y0 & ((1 << s->ps.sps->log2_ctb_size) - 1);
1860 int cand_up = (lc->ctb_up_flag || y0b) ?
1861 s->tab_ipm[(y_pu - 1) * min_pu_width + x_pu] : INTRA_DC;
1862 int cand_left = (lc->ctb_left_flag || x0b) ?
1863 s->tab_ipm[y_pu * min_pu_width + x_pu - 1] : INTRA_DC;
1865 int y_ctb = (y0 >> (s->ps.sps->log2_ctb_size)) << (s->ps.sps->log2_ctb_size);
1867 MvField *tab_mvf = s->ref->tab_mvf;
1868 int intra_pred_mode;
1872 // intra_pred_mode prediction does not cross vertical CTB boundaries
1873 if ((y0 - 1) < y_ctb)
1876 if (cand_left == cand_up) {
1877 if (cand_left < 2) {
1878 candidate[0] = INTRA_PLANAR;
1879 candidate[1] = INTRA_DC;
1880 candidate[2] = INTRA_ANGULAR_26;
1882 candidate[0] = cand_left;
1883 candidate[1] = 2 + ((cand_left - 2 - 1 + 32) & 31);
1884 candidate[2] = 2 + ((cand_left - 2 + 1) & 31);
1887 candidate[0] = cand_left;
1888 candidate[1] = cand_up;
1889 if (candidate[0] != INTRA_PLANAR && candidate[1] != INTRA_PLANAR) {
1890 candidate[2] = INTRA_PLANAR;
1891 } else if (candidate[0] != INTRA_DC && candidate[1] != INTRA_DC) {
1892 candidate[2] = INTRA_DC;
1894 candidate[2] = INTRA_ANGULAR_26;
1898 if (prev_intra_luma_pred_flag) {
1899 intra_pred_mode = candidate[lc->pu.mpm_idx];
1901 if (candidate[0] > candidate[1])
1902 FFSWAP(uint8_t, candidate[0], candidate[1]);
1903 if (candidate[0] > candidate[2])
1904 FFSWAP(uint8_t, candidate[0], candidate[2]);
1905 if (candidate[1] > candidate[2])
1906 FFSWAP(uint8_t, candidate[1], candidate[2]);
1908 intra_pred_mode = lc->pu.rem_intra_luma_pred_mode;
1909 for (i = 0; i < 3; i++)
1910 if (intra_pred_mode >= candidate[i])
1914 /* write the intra prediction units into the mv array */
1917 for (i = 0; i < size_in_pus; i++) {
1918 memset(&s->tab_ipm[(y_pu + i) * min_pu_width + x_pu],
1919 intra_pred_mode, size_in_pus);
1921 for (j = 0; j < size_in_pus; j++) {
1922 tab_mvf[(y_pu + j) * min_pu_width + x_pu + i].is_intra = 1;
1923 tab_mvf[(y_pu + j) * min_pu_width + x_pu + i].pred_flag[0] = 0;
1924 tab_mvf[(y_pu + j) * min_pu_width + x_pu + i].pred_flag[1] = 0;
1925 tab_mvf[(y_pu + j) * min_pu_width + x_pu + i].ref_idx[0] = 0;
1926 tab_mvf[(y_pu + j) * min_pu_width + x_pu + i].ref_idx[1] = 0;
1927 tab_mvf[(y_pu + j) * min_pu_width + x_pu + i].mv[0].x = 0;
1928 tab_mvf[(y_pu + j) * min_pu_width + x_pu + i].mv[0].y = 0;
1929 tab_mvf[(y_pu + j) * min_pu_width + x_pu + i].mv[1].x = 0;
1930 tab_mvf[(y_pu + j) * min_pu_width + x_pu + i].mv[1].y = 0;
1934 return intra_pred_mode;
1937 static av_always_inline void set_ct_depth(HEVCContext *s, int x0, int y0,
1938 int log2_cb_size, int ct_depth)
1940 int length = (1 << log2_cb_size) >> s->ps.sps->log2_min_cb_size;
1941 int x_cb = x0 >> s->ps.sps->log2_min_cb_size;
1942 int y_cb = y0 >> s->ps.sps->log2_min_cb_size;
1945 for (y = 0; y < length; y++)
1946 memset(&s->tab_ct_depth[(y_cb + y) * s->ps.sps->min_cb_width + x_cb],
1950 static void intra_prediction_unit(HEVCContext *s, int x0, int y0,
1953 HEVCLocalContext *lc = &s->HEVClc;
1954 static const uint8_t intra_chroma_table[4] = { 0, 26, 10, 1 };
1955 uint8_t prev_intra_luma_pred_flag[4];
1956 int split = lc->cu.part_mode == PART_NxN;
1957 int pb_size = (1 << log2_cb_size) >> split;
1958 int side = split + 1;
1962 for (i = 0; i < side; i++)
1963 for (j = 0; j < side; j++)
1964 prev_intra_luma_pred_flag[2 * i + j] = ff_hevc_prev_intra_luma_pred_flag_decode(s);
1966 for (i = 0; i < side; i++) {
1967 for (j = 0; j < side; j++) {
1968 if (prev_intra_luma_pred_flag[2 * i + j])
1969 lc->pu.mpm_idx = ff_hevc_mpm_idx_decode(s);
1971 lc->pu.rem_intra_luma_pred_mode = ff_hevc_rem_intra_luma_pred_mode_decode(s);
1973 lc->pu.intra_pred_mode[2 * i + j] =
1974 luma_intra_pred_mode(s, x0 + pb_size * j, y0 + pb_size * i, pb_size,
1975 prev_intra_luma_pred_flag[2 * i + j]);
1979 chroma_mode = ff_hevc_intra_chroma_pred_mode_decode(s);
1980 if (chroma_mode != 4) {
1981 if (lc->pu.intra_pred_mode[0] == intra_chroma_table[chroma_mode])
1982 lc->pu.intra_pred_mode_c = 34;
1984 lc->pu.intra_pred_mode_c = intra_chroma_table[chroma_mode];
1986 lc->pu.intra_pred_mode_c = lc->pu.intra_pred_mode[0];
1990 static void intra_prediction_unit_default_value(HEVCContext *s,
1994 HEVCLocalContext *lc = &s->HEVClc;
1995 int pb_size = 1 << log2_cb_size;
1996 int size_in_pus = pb_size >> s->ps.sps->log2_min_pu_size;
1997 int min_pu_width = s->ps.sps->min_pu_width;
1998 MvField *tab_mvf = s->ref->tab_mvf;
1999 int x_pu = x0 >> s->ps.sps->log2_min_pu_size;
2000 int y_pu = y0 >> s->ps.sps->log2_min_pu_size;
2003 if (size_in_pus == 0)
2005 for (j = 0; j < size_in_pus; j++) {
2006 memset(&s->tab_ipm[(y_pu + j) * min_pu_width + x_pu], INTRA_DC, size_in_pus);
2007 for (k = 0; k < size_in_pus; k++)
2008 tab_mvf[(y_pu + j) * min_pu_width + x_pu + k].is_intra = lc->cu.pred_mode == MODE_INTRA;
2012 static int hls_coding_unit(HEVCContext *s, int x0, int y0, int log2_cb_size)
2014 int cb_size = 1 << log2_cb_size;
2015 HEVCLocalContext *lc = &s->HEVClc;
2016 int log2_min_cb_size = s->ps.sps->log2_min_cb_size;
2017 int length = cb_size >> log2_min_cb_size;
2018 int min_cb_width = s->ps.sps->min_cb_width;
2019 int x_cb = x0 >> log2_min_cb_size;
2020 int y_cb = y0 >> log2_min_cb_size;
2025 lc->cu.pred_mode = MODE_INTRA;
2026 lc->cu.part_mode = PART_2Nx2N;
2027 lc->cu.intra_split_flag = 0;
2029 SAMPLE_CTB(s->skip_flag, x_cb, y_cb) = 0;
2030 for (x = 0; x < 4; x++)
2031 lc->pu.intra_pred_mode[x] = 1;
2032 if (s->ps.pps->transquant_bypass_enable_flag) {
2033 lc->cu.cu_transquant_bypass_flag = ff_hevc_cu_transquant_bypass_flag_decode(s);
2034 if (lc->cu.cu_transquant_bypass_flag)
2035 set_deblocking_bypass(s, x0, y0, log2_cb_size);
2037 lc->cu.cu_transquant_bypass_flag = 0;
2039 if (s->sh.slice_type != I_SLICE) {
2040 uint8_t skip_flag = ff_hevc_skip_flag_decode(s, x0, y0, x_cb, y_cb);
2042 x = y_cb * min_cb_width + x_cb;
2043 for (y = 0; y < length; y++) {
2044 memset(&s->skip_flag[x], skip_flag, length);
2047 lc->cu.pred_mode = skip_flag ? MODE_SKIP : MODE_INTER;
2050 if (SAMPLE_CTB(s->skip_flag, x_cb, y_cb)) {
2051 hls_prediction_unit(s, x0, y0, cb_size, cb_size, log2_cb_size, 0);
2052 intra_prediction_unit_default_value(s, x0, y0, log2_cb_size);
2054 if (!s->sh.disable_deblocking_filter_flag)
2055 ff_hevc_deblocking_boundary_strengths(s, x0, y0, log2_cb_size);
2059 if (s->sh.slice_type != I_SLICE)
2060 lc->cu.pred_mode = ff_hevc_pred_mode_decode(s);
2061 if (lc->cu.pred_mode != MODE_INTRA ||
2062 log2_cb_size == s->ps.sps->log2_min_cb_size) {
2063 lc->cu.part_mode = ff_hevc_part_mode_decode(s, log2_cb_size);
2064 lc->cu.intra_split_flag = lc->cu.part_mode == PART_NxN &&
2065 lc->cu.pred_mode == MODE_INTRA;
2068 if (lc->cu.pred_mode == MODE_INTRA) {
2069 if (lc->cu.part_mode == PART_2Nx2N && s->ps.sps->pcm_enabled_flag &&
2070 log2_cb_size >= s->ps.sps->pcm.log2_min_pcm_cb_size &&
2071 log2_cb_size <= s->ps.sps->pcm.log2_max_pcm_cb_size) {
2072 pcm_flag = ff_hevc_pcm_flag_decode(s);
2075 intra_prediction_unit_default_value(s, x0, y0, log2_cb_size);
2076 ret = hls_pcm_sample(s, x0, y0, log2_cb_size);
2077 if (s->ps.sps->pcm.loop_filter_disable_flag)
2078 set_deblocking_bypass(s, x0, y0, log2_cb_size);
2083 intra_prediction_unit(s, x0, y0, log2_cb_size);
2086 intra_prediction_unit_default_value(s, x0, y0, log2_cb_size);
2087 switch (lc->cu.part_mode) {
2089 hls_prediction_unit(s, x0, y0, cb_size, cb_size, log2_cb_size, 0);
2092 hls_prediction_unit(s, x0, y0, cb_size, cb_size / 2, log2_cb_size, 0);
2093 hls_prediction_unit(s, x0, y0 + cb_size / 2, cb_size, cb_size / 2, log2_cb_size, 1);
2096 hls_prediction_unit(s, x0, y0, cb_size / 2, cb_size, log2_cb_size, 0);
2097 hls_prediction_unit(s, x0 + cb_size / 2, y0, cb_size / 2, cb_size, log2_cb_size, 1);
2100 hls_prediction_unit(s, x0, y0, cb_size, cb_size / 4, log2_cb_size, 0);
2101 hls_prediction_unit(s, x0, y0 + cb_size / 4, cb_size, cb_size * 3 / 4, log2_cb_size, 1);
2104 hls_prediction_unit(s, x0, y0, cb_size, cb_size * 3 / 4, log2_cb_size, 0);
2105 hls_prediction_unit(s, x0, y0 + cb_size * 3 / 4, cb_size, cb_size / 4, log2_cb_size, 1);
2108 hls_prediction_unit(s, x0, y0, cb_size / 4, cb_size, log2_cb_size, 0);
2109 hls_prediction_unit(s, x0 + cb_size / 4, y0, cb_size * 3 / 4, cb_size, log2_cb_size, 1);
2112 hls_prediction_unit(s, x0, y0, cb_size * 3 / 4, cb_size, log2_cb_size, 0);
2113 hls_prediction_unit(s, x0 + cb_size * 3 / 4, y0, cb_size / 4, cb_size, log2_cb_size, 1);
2116 hls_prediction_unit(s, x0, y0, cb_size / 2, cb_size / 2, log2_cb_size, 0);
2117 hls_prediction_unit(s, x0 + cb_size / 2, y0, cb_size / 2, cb_size / 2, log2_cb_size, 1);
2118 hls_prediction_unit(s, x0, y0 + cb_size / 2, cb_size / 2, cb_size / 2, log2_cb_size, 2);
2119 hls_prediction_unit(s, x0 + cb_size / 2, y0 + cb_size / 2, cb_size / 2, cb_size / 2, log2_cb_size, 3);
2125 int rqt_root_cbf = 1;
2127 if (lc->cu.pred_mode != MODE_INTRA &&
2128 !(lc->cu.part_mode == PART_2Nx2N && lc->pu.merge_flag)) {
2129 rqt_root_cbf = ff_hevc_no_residual_syntax_flag_decode(s);
2132 lc->cu.max_trafo_depth = lc->cu.pred_mode == MODE_INTRA ?
2133 s->ps.sps->max_transform_hierarchy_depth_intra + lc->cu.intra_split_flag :
2134 s->ps.sps->max_transform_hierarchy_depth_inter;
2135 ret = hls_transform_tree(s, x0, y0, x0, y0, x0, y0,
2137 log2_cb_size, 0, 0, 0, 0);
2141 if (!s->sh.disable_deblocking_filter_flag)
2142 ff_hevc_deblocking_boundary_strengths(s, x0, y0, log2_cb_size);
2147 if (s->ps.pps->cu_qp_delta_enabled_flag && lc->tu.is_cu_qp_delta_coded == 0)
2148 ff_hevc_set_qPy(s, x0, y0, x0, y0, log2_cb_size);
2150 x = y_cb * min_cb_width + x_cb;
2151 for (y = 0; y < length; y++) {
2152 memset(&s->qp_y_tab[x], lc->qp_y, length);
2156 set_ct_depth(s, x0, y0, log2_cb_size, lc->ct.depth);
2161 static int hls_coding_quadtree(HEVCContext *s, int x0, int y0,
2162 int log2_cb_size, int cb_depth)
2164 HEVCLocalContext *lc = &s->HEVClc;
2165 const int cb_size = 1 << log2_cb_size;
2168 lc->ct.depth = cb_depth;
2169 if (x0 + cb_size <= s->ps.sps->width &&
2170 y0 + cb_size <= s->ps.sps->height &&
2171 log2_cb_size > s->ps.sps->log2_min_cb_size) {
2172 split_cu = ff_hevc_split_coding_unit_flag_decode(s, cb_depth, x0, y0);
2174 split_cu = (log2_cb_size > s->ps.sps->log2_min_cb_size);
2176 if (s->ps.pps->cu_qp_delta_enabled_flag &&
2177 log2_cb_size >= s->ps.sps->log2_ctb_size - s->ps.pps->diff_cu_qp_delta_depth) {
2178 lc->tu.is_cu_qp_delta_coded = 0;
2179 lc->tu.cu_qp_delta = 0;
2183 const int cb_size_split = cb_size >> 1;
2184 const int x1 = x0 + cb_size_split;
2185 const int y1 = y0 + cb_size_split;
2190 #define SUBDIVIDE(x, y) \
2192 if (x < s->ps.sps->width && y < s->ps.sps->height) { \
2193 int ret = hls_coding_quadtree(s, x, y, log2_cb_size, cb_depth);\
2204 int ret = hls_coding_unit(s, x0, y0, log2_cb_size);
2212 static void hls_decode_neighbour(HEVCContext *s, int x_ctb, int y_ctb,
2215 HEVCLocalContext *lc = &s->HEVClc;
2216 int ctb_size = 1 << s->ps.sps->log2_ctb_size;
2217 int ctb_addr_rs = s->ps.pps->ctb_addr_ts_to_rs[ctb_addr_ts];
2218 int ctb_addr_in_slice = ctb_addr_rs - s->sh.slice_addr;
2220 s->tab_slice_address[ctb_addr_rs] = s->sh.slice_addr;
2222 if (s->ps.pps->entropy_coding_sync_enabled_flag) {
2223 if (x_ctb == 0 && (y_ctb & (ctb_size - 1)) == 0)
2224 lc->first_qp_group = 1;
2225 lc->end_of_tiles_x = s->ps.sps->width;
2226 } else if (s->ps.pps->tiles_enabled_flag) {
2227 if (ctb_addr_ts && s->ps.pps->tile_id[ctb_addr_ts] != s->ps.pps->tile_id[ctb_addr_ts - 1]) {
2228 int idxX = s->ps.pps->col_idxX[x_ctb >> s->ps.sps->log2_ctb_size];
2229 lc->start_of_tiles_x = x_ctb;
2230 lc->end_of_tiles_x = x_ctb + (s->ps.pps->column_width[idxX] << s->ps.sps->log2_ctb_size);
2231 lc->first_qp_group = 1;
2234 lc->end_of_tiles_x = s->ps.sps->width;
2237 lc->end_of_tiles_y = FFMIN(y_ctb + ctb_size, s->ps.sps->height);
2239 lc->boundary_flags = 0;
2240 if (s->ps.pps->tiles_enabled_flag) {
2241 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]])
2242 lc->boundary_flags |= BOUNDARY_LEFT_TILE;
2243 if (x_ctb > 0 && s->tab_slice_address[ctb_addr_rs] != s->tab_slice_address[ctb_addr_rs - 1])
2244 lc->boundary_flags |= BOUNDARY_LEFT_SLICE;
2245 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]])
2246 lc->boundary_flags |= BOUNDARY_UPPER_TILE;
2247 if (y_ctb > 0 && s->tab_slice_address[ctb_addr_rs] != s->tab_slice_address[ctb_addr_rs - s->ps.sps->ctb_width])
2248 lc->boundary_flags |= BOUNDARY_UPPER_SLICE;
2250 if (!ctb_addr_in_slice)
2251 lc->boundary_flags |= BOUNDARY_LEFT_SLICE;
2252 if (ctb_addr_in_slice < s->ps.sps->ctb_width)
2253 lc->boundary_flags |= BOUNDARY_UPPER_SLICE;
2256 lc->ctb_left_flag = ((x_ctb > 0) && (ctb_addr_in_slice > 0) && !(lc->boundary_flags & BOUNDARY_LEFT_TILE));
2257 lc->ctb_up_flag = ((y_ctb > 0) && (ctb_addr_in_slice >= s->ps.sps->ctb_width) && !(lc->boundary_flags & BOUNDARY_UPPER_TILE));
2258 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]]));
2259 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]]));
2262 static int hls_slice_data(HEVCContext *s)
2264 int ctb_size = 1 << s->ps.sps->log2_ctb_size;
2268 int ctb_addr_ts = s->ps.pps->ctb_addr_rs_to_ts[s->sh.slice_ctb_addr_rs];
2271 while (more_data && ctb_addr_ts < s->ps.sps->ctb_size) {
2272 int ctb_addr_rs = s->ps.pps->ctb_addr_ts_to_rs[ctb_addr_ts];
2274 x_ctb = (ctb_addr_rs % ((s->ps.sps->width + ctb_size - 1) >> s->ps.sps->log2_ctb_size)) << s->ps.sps->log2_ctb_size;
2275 y_ctb = (ctb_addr_rs / ((s->ps.sps->width + ctb_size - 1) >> s->ps.sps->log2_ctb_size)) << s->ps.sps->log2_ctb_size;
2276 hls_decode_neighbour(s, x_ctb, y_ctb, ctb_addr_ts);
2278 ff_hevc_cabac_init(s, ctb_addr_ts);
2280 hls_sao_param(s, x_ctb >> s->ps.sps->log2_ctb_size, y_ctb >> s->ps.sps->log2_ctb_size);
2282 s->deblock[ctb_addr_rs].beta_offset = s->sh.beta_offset;
2283 s->deblock[ctb_addr_rs].tc_offset = s->sh.tc_offset;
2284 s->filter_slice_edges[ctb_addr_rs] = s->sh.slice_loop_filter_across_slices_enabled_flag;
2286 ret = hls_coding_quadtree(s, x_ctb, y_ctb, s->ps.sps->log2_ctb_size, 0);
2289 more_data = !ff_hevc_end_of_slice_flag_decode(s);
2292 ff_hevc_save_states(s, ctb_addr_ts);
2293 ff_hevc_hls_filters(s, x_ctb, y_ctb, ctb_size);
2296 if (x_ctb + ctb_size >= s->ps.sps->width &&
2297 y_ctb + ctb_size >= s->ps.sps->height)
2298 ff_hevc_hls_filter(s, x_ctb, y_ctb);
2303 static void restore_tqb_pixels(HEVCContext *s)
2305 int min_pu_size = 1 << s->ps.sps->log2_min_pu_size;
2308 for (c_idx = 0; c_idx < 3; c_idx++) {
2309 ptrdiff_t stride = s->frame->linesize[c_idx];
2310 int hshift = s->ps.sps->hshift[c_idx];
2311 int vshift = s->ps.sps->vshift[c_idx];
2312 for (y = 0; y < s->ps.sps->min_pu_height; y++) {
2313 for (x = 0; x < s->ps.sps->min_pu_width; x++) {
2314 if (s->is_pcm[y * s->ps.sps->min_pu_width + x]) {
2316 int len = min_pu_size >> hshift;
2317 uint8_t *src = &s->frame->data[c_idx][((y << s->ps.sps->log2_min_pu_size) >> vshift) * stride + (((x << s->ps.sps->log2_min_pu_size) >> hshift) << s->ps.sps->pixel_shift)];
2318 uint8_t *dst = &s->sao_frame->data[c_idx][((y << s->ps.sps->log2_min_pu_size) >> vshift) * stride + (((x << s->ps.sps->log2_min_pu_size) >> hshift) << s->ps.sps->pixel_shift)];
2319 for (n = 0; n < (min_pu_size >> vshift); n++) {
2320 memcpy(dst, src, len);
2330 static int set_side_data(HEVCContext *s)
2332 AVFrame *out = s->ref->frame;
2334 if (s->sei_frame_packing_present &&
2335 s->frame_packing_arrangement_type >= 3 &&
2336 s->frame_packing_arrangement_type <= 5 &&
2337 s->content_interpretation_type > 0 &&
2338 s->content_interpretation_type < 3) {
2339 AVStereo3D *stereo = av_stereo3d_create_side_data(out);
2341 return AVERROR(ENOMEM);
2343 switch (s->frame_packing_arrangement_type) {
2345 if (s->quincunx_subsampling)
2346 stereo->type = AV_STEREO3D_SIDEBYSIDE_QUINCUNX;
2348 stereo->type = AV_STEREO3D_SIDEBYSIDE;
2351 stereo->type = AV_STEREO3D_TOPBOTTOM;
2354 stereo->type = AV_STEREO3D_FRAMESEQUENCE;
2358 if (s->content_interpretation_type == 2)
2359 stereo->flags = AV_STEREO3D_FLAG_INVERT;
2362 if (s->sei_display_orientation_present &&
2363 (s->sei_anticlockwise_rotation || s->sei_hflip || s->sei_vflip)) {
2364 double angle = s->sei_anticlockwise_rotation * 360 / (double) (1 << 16);
2365 AVFrameSideData *rotation = av_frame_new_side_data(out,
2366 AV_FRAME_DATA_DISPLAYMATRIX,
2367 sizeof(int32_t) * 9);
2369 return AVERROR(ENOMEM);
2371 av_display_rotation_set((int32_t *)rotation->data, angle);
2372 av_display_matrix_flip((int32_t *)rotation->data,
2373 s->sei_hflip, s->sei_vflip);
2379 static int hevc_frame_start(HEVCContext *s)
2381 HEVCLocalContext *lc = &s->HEVClc;
2384 memset(s->horizontal_bs, 0, 2 * s->bs_width * (s->bs_height + 1));
2385 memset(s->vertical_bs, 0, 2 * s->bs_width * (s->bs_height + 1));
2386 memset(s->cbf_luma, 0, s->ps.sps->min_tb_width * s->ps.sps->min_tb_height);
2387 memset(s->is_pcm, 0, s->ps.sps->min_pu_width * s->ps.sps->min_pu_height);
2389 lc->start_of_tiles_x = 0;
2391 s->first_nal_type = s->nal_unit_type;
2393 if (s->ps.pps->tiles_enabled_flag)
2394 lc->end_of_tiles_x = s->ps.pps->column_width[0] << s->ps.sps->log2_ctb_size;
2396 ret = ff_hevc_set_new_ref(s, s->ps.sps->sao_enabled ? &s->sao_frame : &s->frame,
2401 ret = ff_hevc_frame_rps(s);
2403 av_log(s->avctx, AV_LOG_ERROR, "Error constructing the frame RPS.\n");
2407 s->ref->frame->key_frame = IS_IRAP(s);
2409 ret = set_side_data(s);
2413 av_frame_unref(s->output_frame);
2414 ret = ff_hevc_output_frame(s, s->output_frame, 0);
2418 ff_thread_finish_setup(s->avctx);
2424 ff_hevc_unref_frame(s, s->ref, ~0);
2429 static int decode_nal_unit(HEVCContext *s, const HEVCNAL *nal)
2431 HEVCLocalContext *lc = &s->HEVClc;
2432 GetBitContext *gb = &lc->gb;
2433 int ctb_addr_ts, ret;
2436 s->nal_unit_type = nal->type;
2437 s->temporal_id = nal->temporal_id;
2439 switch (s->nal_unit_type) {
2441 ret = ff_hevc_decode_nal_vps(gb, s->avctx, &s->ps);
2446 ret = ff_hevc_decode_nal_sps(gb, s->avctx, &s->ps,
2447 s->apply_defdispwin);
2452 ret = ff_hevc_decode_nal_pps(gb, s->avctx, &s->ps);
2456 case NAL_SEI_PREFIX:
2457 case NAL_SEI_SUFFIX:
2458 ret = ff_hevc_decode_nal_sei(s);
2469 case NAL_BLA_W_RADL:
2471 case NAL_IDR_W_RADL:
2478 ret = hls_slice_header(s);
2482 if (s->max_ra == INT_MAX) {
2483 if (s->nal_unit_type == NAL_CRA_NUT || IS_BLA(s)) {
2487 s->max_ra = INT_MIN;
2491 if ((s->nal_unit_type == NAL_RASL_R || s->nal_unit_type == NAL_RASL_N) &&
2492 s->poc <= s->max_ra) {
2496 if (s->nal_unit_type == NAL_RASL_R && s->poc > s->max_ra)
2497 s->max_ra = INT_MIN;
2500 if (s->sh.first_slice_in_pic_flag) {
2501 ret = hevc_frame_start(s);
2504 } else if (!s->ref) {
2505 av_log(s->avctx, AV_LOG_ERROR, "First slice in a frame missing.\n");
2509 if (s->nal_unit_type != s->first_nal_type) {
2510 av_log(s->avctx, AV_LOG_ERROR,
2511 "Non-matching NAL types of the VCL NALUs: %d %d\n",
2512 s->first_nal_type, s->nal_unit_type);
2513 return AVERROR_INVALIDDATA;
2516 if (!s->sh.dependent_slice_segment_flag &&
2517 s->sh.slice_type != I_SLICE) {
2518 ret = ff_hevc_slice_rpl(s);
2520 av_log(s->avctx, AV_LOG_WARNING,
2521 "Error constructing the reference lists for the current slice.\n");
2526 if (s->sh.first_slice_in_pic_flag && s->avctx->hwaccel) {
2527 ret = s->avctx->hwaccel->start_frame(s->avctx, NULL, 0);
2532 if (s->avctx->hwaccel) {
2533 ret = s->avctx->hwaccel->decode_slice(s->avctx, nal->raw_data, nal->raw_size);
2537 ctb_addr_ts = hls_slice_data(s);
2538 if (ctb_addr_ts >= (s->ps.sps->ctb_width * s->ps.sps->ctb_height)) {
2540 if ((s->ps.pps->transquant_bypass_enable_flag ||
2541 (s->ps.sps->pcm.loop_filter_disable_flag && s->ps.sps->pcm_enabled_flag)) &&
2542 s->ps.sps->sao_enabled)
2543 restore_tqb_pixels(s);
2546 if (ctb_addr_ts < 0) {
2554 s->seq_decode = (s->seq_decode + 1) & 0xff;
2555 s->max_ra = INT_MAX;
2561 av_log(s->avctx, AV_LOG_INFO,
2562 "Skipping NAL unit %d\n", s->nal_unit_type);
2567 if (s->avctx->err_recognition & AV_EF_EXPLODE)
2572 static int decode_nal_units(HEVCContext *s, const uint8_t *buf, int length)
2579 /* split the input packet into NAL units, so we know the upper bound on the
2580 * number of slices in the frame */
2581 ret = ff_hevc_split_packet(&s->pkt, buf, length, s->avctx, s->is_nalff,
2582 s->nal_length_size);
2584 av_log(s->avctx, AV_LOG_ERROR,
2585 "Error splitting the input into NAL units.\n");
2589 for (i = 0; i < s->pkt.nb_nals; i++) {
2590 if (s->pkt.nals[i].type == NAL_EOB_NUT ||
2591 s->pkt.nals[i].type == NAL_EOS_NUT)
2595 /* decode the NAL units */
2596 for (i = 0; i < s->pkt.nb_nals; i++) {
2597 ret = decode_nal_unit(s, &s->pkt.nals[i]);
2599 av_log(s->avctx, AV_LOG_WARNING,
2600 "Error parsing NAL unit #%d.\n", i);
2607 ff_thread_report_progress(&s->ref->tf, INT_MAX, 0);
2612 static void print_md5(void *log_ctx, int level, uint8_t md5[16])
2615 for (i = 0; i < 16; i++)
2616 av_log(log_ctx, level, "%02"PRIx8, md5[i]);
2619 static int verify_md5(HEVCContext *s, AVFrame *frame)
2621 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(frame->format);
2626 return AVERROR(EINVAL);
2628 pixel_shift = desc->comp[0].depth > 8;
2630 av_log(s->avctx, AV_LOG_DEBUG, "Verifying checksum for frame with POC %d: ",
2633 /* the checksums are LE, so we have to byteswap for >8bpp formats
2636 if (pixel_shift && !s->checksum_buf) {
2637 av_fast_malloc(&s->checksum_buf, &s->checksum_buf_size,
2638 FFMAX3(frame->linesize[0], frame->linesize[1],
2639 frame->linesize[2]));
2640 if (!s->checksum_buf)
2641 return AVERROR(ENOMEM);
2645 for (i = 0; frame->data[i]; i++) {
2646 int width = s->avctx->coded_width;
2647 int height = s->avctx->coded_height;
2648 int w = (i == 1 || i == 2) ? (width >> desc->log2_chroma_w) : width;
2649 int h = (i == 1 || i == 2) ? (height >> desc->log2_chroma_h) : height;
2652 av_md5_init(s->md5_ctx);
2653 for (j = 0; j < h; j++) {
2654 const uint8_t *src = frame->data[i] + j * frame->linesize[i];
2657 s->bdsp.bswap16_buf((uint16_t *) s->checksum_buf,
2658 (const uint16_t *) src, w);
2659 src = s->checksum_buf;
2662 av_md5_update(s->md5_ctx, src, w << pixel_shift);
2664 av_md5_final(s->md5_ctx, md5);
2666 if (!memcmp(md5, s->md5[i], 16)) {
2667 av_log (s->avctx, AV_LOG_DEBUG, "plane %d - correct ", i);
2668 print_md5(s->avctx, AV_LOG_DEBUG, md5);
2669 av_log (s->avctx, AV_LOG_DEBUG, "; ");
2671 av_log (s->avctx, AV_LOG_ERROR, "mismatching checksum of plane %d - ", i);
2672 print_md5(s->avctx, AV_LOG_ERROR, md5);
2673 av_log (s->avctx, AV_LOG_ERROR, " != ");
2674 print_md5(s->avctx, AV_LOG_ERROR, s->md5[i]);
2675 av_log (s->avctx, AV_LOG_ERROR, "\n");
2676 return AVERROR_INVALIDDATA;
2680 av_log(s->avctx, AV_LOG_DEBUG, "\n");
2685 static int hevc_decode_frame(AVCodecContext *avctx, void *data, int *got_output,
2689 HEVCContext *s = avctx->priv_data;
2692 ret = ff_hevc_output_frame(s, data, 1);
2701 ret = decode_nal_units(s, avpkt->data, avpkt->size);
2705 if (avctx->hwaccel) {
2706 if (s->ref && avctx->hwaccel->end_frame(avctx) < 0)
2707 av_log(avctx, AV_LOG_ERROR,
2708 "hardware accelerator failed to decode picture\n");
2710 /* verify the SEI checksum */
2711 if (avctx->err_recognition & AV_EF_CRCCHECK && s->is_decoded &&
2713 ret = verify_md5(s, s->ref->frame);
2714 if (ret < 0 && avctx->err_recognition & AV_EF_EXPLODE) {
2715 ff_hevc_unref_frame(s, s->ref, ~0);
2722 if (s->is_decoded) {
2723 av_log(avctx, AV_LOG_DEBUG, "Decoded frame with POC %d.\n", s->poc);
2727 if (s->output_frame->buf[0]) {
2728 av_frame_move_ref(data, s->output_frame);
2735 static int hevc_ref_frame(HEVCContext *s, HEVCFrame *dst, HEVCFrame *src)
2737 int ret = ff_thread_ref_frame(&dst->tf, &src->tf);
2741 dst->tab_mvf_buf = av_buffer_ref(src->tab_mvf_buf);
2742 if (!dst->tab_mvf_buf)
2744 dst->tab_mvf = src->tab_mvf;
2746 dst->rpl_tab_buf = av_buffer_ref(src->rpl_tab_buf);
2747 if (!dst->rpl_tab_buf)
2749 dst->rpl_tab = src->rpl_tab;
2751 dst->rpl_buf = av_buffer_ref(src->rpl_buf);
2755 dst->poc = src->poc;
2756 dst->ctb_count = src->ctb_count;
2757 dst->window = src->window;
2758 dst->flags = src->flags;
2759 dst->sequence = src->sequence;
2761 if (src->hwaccel_picture_private) {
2762 dst->hwaccel_priv_buf = av_buffer_ref(src->hwaccel_priv_buf);
2763 if (!dst->hwaccel_priv_buf)
2765 dst->hwaccel_picture_private = dst->hwaccel_priv_buf->data;
2770 ff_hevc_unref_frame(s, dst, ~0);
2771 return AVERROR(ENOMEM);
2774 static av_cold int hevc_decode_free(AVCodecContext *avctx)
2776 HEVCContext *s = avctx->priv_data;
2781 av_freep(&s->md5_ctx);
2783 av_frame_free(&s->tmp_frame);
2784 av_frame_free(&s->output_frame);
2786 for (i = 0; i < FF_ARRAY_ELEMS(s->DPB); i++) {
2787 ff_hevc_unref_frame(s, &s->DPB[i], ~0);
2788 av_frame_free(&s->DPB[i].frame);
2791 for (i = 0; i < FF_ARRAY_ELEMS(s->ps.vps_list); i++)
2792 av_buffer_unref(&s->ps.vps_list[i]);
2793 for (i = 0; i < FF_ARRAY_ELEMS(s->ps.sps_list); i++)
2794 av_buffer_unref(&s->ps.sps_list[i]);
2795 for (i = 0; i < FF_ARRAY_ELEMS(s->ps.pps_list); i++)
2796 av_buffer_unref(&s->ps.pps_list[i]);
2798 for (i = 0; i < s->pkt.nals_allocated; i++)
2799 av_freep(&s->pkt.nals[i].rbsp_buffer);
2800 av_freep(&s->pkt.nals);
2801 s->pkt.nals_allocated = 0;
2806 static av_cold int hevc_init_context(AVCodecContext *avctx)
2808 HEVCContext *s = avctx->priv_data;
2813 s->tmp_frame = av_frame_alloc();
2817 s->output_frame = av_frame_alloc();
2818 if (!s->output_frame)
2821 for (i = 0; i < FF_ARRAY_ELEMS(s->DPB); i++) {
2822 s->DPB[i].frame = av_frame_alloc();
2823 if (!s->DPB[i].frame)
2825 s->DPB[i].tf.f = s->DPB[i].frame;
2828 s->max_ra = INT_MAX;
2830 s->md5_ctx = av_md5_alloc();
2834 ff_bswapdsp_init(&s->bdsp);
2836 s->context_initialized = 1;
2841 hevc_decode_free(avctx);
2842 return AVERROR(ENOMEM);
2845 static int hevc_update_thread_context(AVCodecContext *dst,
2846 const AVCodecContext *src)
2848 HEVCContext *s = dst->priv_data;
2849 HEVCContext *s0 = src->priv_data;
2852 if (!s->context_initialized) {
2853 ret = hevc_init_context(dst);
2858 for (i = 0; i < FF_ARRAY_ELEMS(s->DPB); i++) {
2859 ff_hevc_unref_frame(s, &s->DPB[i], ~0);
2860 if (s0->DPB[i].frame->buf[0]) {
2861 ret = hevc_ref_frame(s, &s->DPB[i], &s0->DPB[i]);
2867 for (i = 0; i < FF_ARRAY_ELEMS(s->ps.vps_list); i++) {
2868 av_buffer_unref(&s->ps.vps_list[i]);
2869 if (s0->ps.vps_list[i]) {
2870 s->ps.vps_list[i] = av_buffer_ref(s0->ps.vps_list[i]);
2871 if (!s->ps.vps_list[i])
2872 return AVERROR(ENOMEM);
2876 for (i = 0; i < FF_ARRAY_ELEMS(s->ps.sps_list); i++) {
2877 av_buffer_unref(&s->ps.sps_list[i]);
2878 if (s0->ps.sps_list[i]) {
2879 s->ps.sps_list[i] = av_buffer_ref(s0->ps.sps_list[i]);
2880 if (!s->ps.sps_list[i])
2881 return AVERROR(ENOMEM);
2885 for (i = 0; i < FF_ARRAY_ELEMS(s->ps.pps_list); i++) {
2886 av_buffer_unref(&s->ps.pps_list[i]);
2887 if (s0->ps.pps_list[i]) {
2888 s->ps.pps_list[i] = av_buffer_ref(s0->ps.pps_list[i]);
2889 if (!s->ps.pps_list[i])
2890 return AVERROR(ENOMEM);
2894 if (s->ps.sps != s0->ps.sps)
2895 ret = set_sps(s, s0->ps.sps);
2897 s->seq_decode = s0->seq_decode;
2898 s->seq_output = s0->seq_output;
2899 s->pocTid0 = s0->pocTid0;
2900 s->max_ra = s0->max_ra;
2902 s->is_nalff = s0->is_nalff;
2903 s->nal_length_size = s0->nal_length_size;
2906 s->seq_decode = (s->seq_decode + 1) & 0xff;
2907 s->max_ra = INT_MAX;
2913 static int hevc_decode_extradata(HEVCContext *s)
2915 AVCodecContext *avctx = s->avctx;
2919 bytestream2_init(&gb, avctx->extradata, avctx->extradata_size);
2921 if (avctx->extradata_size > 3 &&
2922 (avctx->extradata[0] || avctx->extradata[1] ||
2923 avctx->extradata[2] > 1)) {
2924 /* It seems the extradata is encoded as hvcC format.
2925 * Temporarily, we support configurationVersion==0 until 14496-15 3rd
2926 * is finalized. When finalized, configurationVersion will be 1 and we
2927 * can recognize hvcC by checking if avctx->extradata[0]==1 or not. */
2928 int i, j, num_arrays, nal_len_size;
2932 bytestream2_skip(&gb, 21);
2933 nal_len_size = (bytestream2_get_byte(&gb) & 3) + 1;
2934 num_arrays = bytestream2_get_byte(&gb);
2936 /* nal units in the hvcC always have length coded with 2 bytes,
2937 * so put a fake nal_length_size = 2 while parsing them */
2938 s->nal_length_size = 2;
2940 /* Decode nal units from hvcC. */
2941 for (i = 0; i < num_arrays; i++) {
2942 int type = bytestream2_get_byte(&gb) & 0x3f;
2943 int cnt = bytestream2_get_be16(&gb);
2945 for (j = 0; j < cnt; j++) {
2946 // +2 for the nal size field
2947 int nalsize = bytestream2_peek_be16(&gb) + 2;
2948 if (bytestream2_get_bytes_left(&gb) < nalsize) {
2949 av_log(s->avctx, AV_LOG_ERROR,
2950 "Invalid NAL unit size in extradata.\n");
2951 return AVERROR_INVALIDDATA;
2954 ret = decode_nal_units(s, gb.buffer, nalsize);
2956 av_log(avctx, AV_LOG_ERROR,
2957 "Decoding nal unit %d %d from hvcC failed\n",
2961 bytestream2_skip(&gb, nalsize);
2965 /* Now store right nal length size, that will be used to parse
2967 s->nal_length_size = nal_len_size;
2970 ret = decode_nal_units(s, avctx->extradata, avctx->extradata_size);
2975 /* export stream parameters from the first SPS */
2976 for (i = 0; i < FF_ARRAY_ELEMS(s->ps.sps_list); i++) {
2977 if (s->ps.sps_list[i]) {
2978 const HEVCSPS *sps = (const HEVCSPS*)s->ps.sps_list[i]->data;
2979 export_stream_params(s->avctx, &s->ps, sps);
2987 static av_cold int hevc_decode_init(AVCodecContext *avctx)
2989 HEVCContext *s = avctx->priv_data;
2992 avctx->internal->allocate_progress = 1;
2994 ret = hevc_init_context(avctx);
2998 if (avctx->extradata_size > 0 && avctx->extradata) {
2999 ret = hevc_decode_extradata(s);
3001 hevc_decode_free(avctx);
3009 static av_cold int hevc_init_thread_copy(AVCodecContext *avctx)
3011 HEVCContext *s = avctx->priv_data;
3014 memset(s, 0, sizeof(*s));
3016 ret = hevc_init_context(avctx);
3023 static void hevc_decode_flush(AVCodecContext *avctx)
3025 HEVCContext *s = avctx->priv_data;
3026 ff_hevc_flush_dpb(s);
3027 s->max_ra = INT_MAX;
3030 #define OFFSET(x) offsetof(HEVCContext, x)
3031 #define PAR (AV_OPT_FLAG_DECODING_PARAM | AV_OPT_FLAG_VIDEO_PARAM)
3033 static const AVProfile profiles[] = {
3034 { FF_PROFILE_HEVC_MAIN, "Main" },
3035 { FF_PROFILE_HEVC_MAIN_10, "Main 10" },
3036 { FF_PROFILE_HEVC_MAIN_STILL_PICTURE, "Main Still Picture" },
3037 { FF_PROFILE_UNKNOWN },
3040 static const AVOption options[] = {
3041 { "apply_defdispwin", "Apply default display window from VUI", OFFSET(apply_defdispwin),
3042 AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1, PAR },
3046 static const AVClass hevc_decoder_class = {
3047 .class_name = "HEVC decoder",
3048 .item_name = av_default_item_name,
3050 .version = LIBAVUTIL_VERSION_INT,
3053 AVCodec ff_hevc_decoder = {
3055 .long_name = NULL_IF_CONFIG_SMALL("HEVC (High Efficiency Video Coding)"),
3056 .type = AVMEDIA_TYPE_VIDEO,
3057 .id = AV_CODEC_ID_HEVC,
3058 .priv_data_size = sizeof(HEVCContext),
3059 .priv_class = &hevc_decoder_class,
3060 .init = hevc_decode_init,
3061 .close = hevc_decode_free,
3062 .decode = hevc_decode_frame,
3063 .flush = hevc_decode_flush,
3064 .update_thread_context = hevc_update_thread_context,
3065 .init_thread_copy = hevc_init_thread_copy,
3066 .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DELAY |
3067 AV_CODEC_CAP_FRAME_THREADS,
3068 .profiles = NULL_IF_CONFIG_SMALL(profiles),