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/atomic.h"
27 #include "libavutil/attributes.h"
28 #include "libavutil/common.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"
35 #include "bytestream.h"
36 #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 };
46 * NOTE: Each function hls_foo correspond to the function foo in the
47 * specification (HLS stands for High Level Syntax).
54 /* free everything allocated by pic_arrays_init() */
55 static void pic_arrays_free(HEVCContext *s)
58 av_freep(&s->deblock);
59 av_freep(&s->split_cu_flag);
61 av_freep(&s->skip_flag);
62 av_freep(&s->tab_ct_depth);
64 av_freep(&s->tab_ipm);
65 av_freep(&s->cbf_luma);
68 av_freep(&s->qp_y_tab);
69 av_freep(&s->tab_slice_address);
70 av_freep(&s->filter_slice_edges);
72 av_freep(&s->horizontal_bs);
73 av_freep(&s->vertical_bs);
75 av_freep(&s->sh.entry_point_offset);
76 av_freep(&s->sh.size);
77 av_freep(&s->sh.offset);
79 av_buffer_pool_uninit(&s->tab_mvf_pool);
80 av_buffer_pool_uninit(&s->rpl_tab_pool);
83 /* allocate arrays that depend on frame dimensions */
84 static int pic_arrays_init(HEVCContext *s, const HEVCSPS *sps)
86 int log2_min_cb_size = sps->log2_min_cb_size;
87 int width = sps->width;
88 int height = sps->height;
89 int pic_size = width * height;
90 int pic_size_in_ctb = ((width >> log2_min_cb_size) + 1) *
91 ((height >> log2_min_cb_size) + 1);
92 int ctb_count = sps->ctb_width * sps->ctb_height;
93 int min_pu_size = sps->min_pu_width * sps->min_pu_height;
95 s->bs_width = width >> 3;
96 s->bs_height = height >> 3;
98 s->sao = av_mallocz_array(ctb_count, sizeof(*s->sao));
99 s->deblock = av_mallocz_array(ctb_count, sizeof(*s->deblock));
100 s->split_cu_flag = av_malloc(pic_size);
101 if (!s->sao || !s->deblock || !s->split_cu_flag)
104 s->skip_flag = av_malloc(pic_size_in_ctb);
105 s->tab_ct_depth = av_malloc(sps->min_cb_height * sps->min_cb_width);
106 if (!s->skip_flag || !s->tab_ct_depth)
109 s->cbf_luma = av_malloc(sps->min_tb_width * sps->min_tb_height);
110 s->tab_ipm = av_mallocz(min_pu_size);
111 s->is_pcm = av_malloc(min_pu_size);
112 if (!s->tab_ipm || !s->cbf_luma || !s->is_pcm)
115 s->filter_slice_edges = av_malloc(ctb_count);
116 s->tab_slice_address = av_malloc(pic_size_in_ctb *
117 sizeof(*s->tab_slice_address));
118 s->qp_y_tab = av_malloc(pic_size_in_ctb *
119 sizeof(*s->qp_y_tab));
120 if (!s->qp_y_tab || !s->filter_slice_edges || !s->tab_slice_address)
123 s->horizontal_bs = av_mallocz(2 * s->bs_width * (s->bs_height + 1));
124 s->vertical_bs = av_mallocz(2 * s->bs_width * (s->bs_height + 1));
125 if (!s->horizontal_bs || !s->vertical_bs)
128 s->tab_mvf_pool = av_buffer_pool_init(min_pu_size * sizeof(MvField),
130 s->rpl_tab_pool = av_buffer_pool_init(ctb_count * sizeof(RefPicListTab),
132 if (!s->tab_mvf_pool || !s->rpl_tab_pool)
139 return AVERROR(ENOMEM);
142 static void pred_weight_table(HEVCContext *s, GetBitContext *gb)
146 uint8_t luma_weight_l0_flag[16];
147 uint8_t chroma_weight_l0_flag[16];
148 uint8_t luma_weight_l1_flag[16];
149 uint8_t chroma_weight_l1_flag[16];
151 s->sh.luma_log2_weight_denom = get_ue_golomb_long(gb);
152 if (s->sps->chroma_format_idc != 0) {
153 int delta = get_se_golomb(gb);
154 s->sh.chroma_log2_weight_denom = av_clip_c(s->sh.luma_log2_weight_denom + delta, 0, 7);
157 for (i = 0; i < s->sh.nb_refs[L0]; i++) {
158 luma_weight_l0_flag[i] = get_bits1(gb);
159 if (!luma_weight_l0_flag[i]) {
160 s->sh.luma_weight_l0[i] = 1 << s->sh.luma_log2_weight_denom;
161 s->sh.luma_offset_l0[i] = 0;
164 if (s->sps->chroma_format_idc != 0) { // FIXME: invert "if" and "for"
165 for (i = 0; i < s->sh.nb_refs[L0]; i++)
166 chroma_weight_l0_flag[i] = get_bits1(gb);
168 for (i = 0; i < s->sh.nb_refs[L0]; i++)
169 chroma_weight_l0_flag[i] = 0;
171 for (i = 0; i < s->sh.nb_refs[L0]; i++) {
172 if (luma_weight_l0_flag[i]) {
173 int delta_luma_weight_l0 = get_se_golomb(gb);
174 s->sh.luma_weight_l0[i] = (1 << s->sh.luma_log2_weight_denom) + delta_luma_weight_l0;
175 s->sh.luma_offset_l0[i] = get_se_golomb(gb);
177 if (chroma_weight_l0_flag[i]) {
178 for (j = 0; j < 2; j++) {
179 int delta_chroma_weight_l0 = get_se_golomb(gb);
180 int delta_chroma_offset_l0 = get_se_golomb(gb);
181 s->sh.chroma_weight_l0[i][j] = (1 << s->sh.chroma_log2_weight_denom) + delta_chroma_weight_l0;
182 s->sh.chroma_offset_l0[i][j] = av_clip_c((delta_chroma_offset_l0 - ((128 * s->sh.chroma_weight_l0[i][j])
183 >> s->sh.chroma_log2_weight_denom) + 128), -128, 127);
186 s->sh.chroma_weight_l0[i][0] = 1 << s->sh.chroma_log2_weight_denom;
187 s->sh.chroma_offset_l0[i][0] = 0;
188 s->sh.chroma_weight_l0[i][1] = 1 << s->sh.chroma_log2_weight_denom;
189 s->sh.chroma_offset_l0[i][1] = 0;
192 if (s->sh.slice_type == B_SLICE) {
193 for (i = 0; i < s->sh.nb_refs[L1]; i++) {
194 luma_weight_l1_flag[i] = get_bits1(gb);
195 if (!luma_weight_l1_flag[i]) {
196 s->sh.luma_weight_l1[i] = 1 << s->sh.luma_log2_weight_denom;
197 s->sh.luma_offset_l1[i] = 0;
200 if (s->sps->chroma_format_idc != 0) {
201 for (i = 0; i < s->sh.nb_refs[L1]; i++)
202 chroma_weight_l1_flag[i] = get_bits1(gb);
204 for (i = 0; i < s->sh.nb_refs[L1]; i++)
205 chroma_weight_l1_flag[i] = 0;
207 for (i = 0; i < s->sh.nb_refs[L1]; i++) {
208 if (luma_weight_l1_flag[i]) {
209 int delta_luma_weight_l1 = get_se_golomb(gb);
210 s->sh.luma_weight_l1[i] = (1 << s->sh.luma_log2_weight_denom) + delta_luma_weight_l1;
211 s->sh.luma_offset_l1[i] = get_se_golomb(gb);
213 if (chroma_weight_l1_flag[i]) {
214 for (j = 0; j < 2; j++) {
215 int delta_chroma_weight_l1 = get_se_golomb(gb);
216 int delta_chroma_offset_l1 = get_se_golomb(gb);
217 s->sh.chroma_weight_l1[i][j] = (1 << s->sh.chroma_log2_weight_denom) + delta_chroma_weight_l1;
218 s->sh.chroma_offset_l1[i][j] = av_clip_c((delta_chroma_offset_l1 - ((128 * s->sh.chroma_weight_l1[i][j])
219 >> s->sh.chroma_log2_weight_denom) + 128), -128, 127);
222 s->sh.chroma_weight_l1[i][0] = 1 << s->sh.chroma_log2_weight_denom;
223 s->sh.chroma_offset_l1[i][0] = 0;
224 s->sh.chroma_weight_l1[i][1] = 1 << s->sh.chroma_log2_weight_denom;
225 s->sh.chroma_offset_l1[i][1] = 0;
231 static int decode_lt_rps(HEVCContext *s, LongTermRPS *rps, GetBitContext *gb)
233 const HEVCSPS *sps = s->sps;
234 int max_poc_lsb = 1 << sps->log2_max_poc_lsb;
235 int prev_delta_msb = 0;
236 unsigned int nb_sps = 0, nb_sh;
240 if (!sps->long_term_ref_pics_present_flag)
243 if (sps->num_long_term_ref_pics_sps > 0)
244 nb_sps = get_ue_golomb_long(gb);
245 nb_sh = get_ue_golomb_long(gb);
247 if (nb_sh + nb_sps > FF_ARRAY_ELEMS(rps->poc))
248 return AVERROR_INVALIDDATA;
250 rps->nb_refs = nb_sh + nb_sps;
252 for (i = 0; i < rps->nb_refs; i++) {
253 uint8_t delta_poc_msb_present;
256 uint8_t lt_idx_sps = 0;
258 if (sps->num_long_term_ref_pics_sps > 1)
259 lt_idx_sps = get_bits(gb, av_ceil_log2(sps->num_long_term_ref_pics_sps));
261 rps->poc[i] = sps->lt_ref_pic_poc_lsb_sps[lt_idx_sps];
262 rps->used[i] = sps->used_by_curr_pic_lt_sps_flag[lt_idx_sps];
264 rps->poc[i] = get_bits(gb, sps->log2_max_poc_lsb);
265 rps->used[i] = get_bits1(gb);
268 delta_poc_msb_present = get_bits1(gb);
269 if (delta_poc_msb_present) {
270 int delta = get_ue_golomb_long(gb);
272 if (i && i != nb_sps)
273 delta += prev_delta_msb;
275 rps->poc[i] += s->poc - delta * max_poc_lsb - s->sh.pic_order_cnt_lsb;
276 prev_delta_msb = delta;
283 static int set_sps(HEVCContext *s, const HEVCSPS *sps)
286 unsigned int num = 0, den = 0;
289 ret = pic_arrays_init(s, sps);
293 s->avctx->coded_width = sps->width;
294 s->avctx->coded_height = sps->height;
295 s->avctx->width = sps->output_width;
296 s->avctx->height = sps->output_height;
297 s->avctx->pix_fmt = sps->pix_fmt;
298 s->avctx->sample_aspect_ratio = sps->vui.sar;
299 s->avctx->has_b_frames = sps->temporal_layer[sps->max_sub_layers - 1].num_reorder_pics;
301 if (sps->vui.video_signal_type_present_flag)
302 s->avctx->color_range = sps->vui.video_full_range_flag ? AVCOL_RANGE_JPEG
305 s->avctx->color_range = AVCOL_RANGE_MPEG;
307 if (sps->vui.colour_description_present_flag) {
308 s->avctx->color_primaries = sps->vui.colour_primaries;
309 s->avctx->color_trc = sps->vui.transfer_characteristic;
310 s->avctx->colorspace = sps->vui.matrix_coeffs;
312 s->avctx->color_primaries = AVCOL_PRI_UNSPECIFIED;
313 s->avctx->color_trc = AVCOL_TRC_UNSPECIFIED;
314 s->avctx->colorspace = AVCOL_SPC_UNSPECIFIED;
317 ff_hevc_pred_init(&s->hpc, sps->bit_depth);
318 ff_hevc_dsp_init (&s->hevcdsp, sps->bit_depth);
319 ff_videodsp_init (&s->vdsp, sps->bit_depth);
321 if (sps->sao_enabled) {
322 av_frame_unref(s->tmp_frame);
323 ret = ff_get_buffer(s->avctx, s->tmp_frame, AV_GET_BUFFER_FLAG_REF);
326 s->frame = s->tmp_frame;
330 s->vps = (HEVCVPS*) s->vps_list[s->sps->vps_id]->data;
332 if (s->vps->vps_timing_info_present_flag) {
333 num = s->vps->vps_num_units_in_tick;
334 den = s->vps->vps_time_scale;
335 } else if (sps->vui.vui_timing_info_present_flag) {
336 num = sps->vui.vui_num_units_in_tick;
337 den = sps->vui.vui_time_scale;
340 if (num != 0 && den != 0)
341 av_reduce(&s->avctx->time_base.num, &s->avctx->time_base.den,
352 static int hls_slice_header(HEVCContext *s)
354 GetBitContext *gb = &s->HEVClc->gb;
355 SliceHeader *sh = &s->sh;
359 sh->first_slice_in_pic_flag = get_bits1(gb);
360 if ((IS_IDR(s) || IS_BLA(s)) && sh->first_slice_in_pic_flag) {
361 s->seq_decode = (s->seq_decode + 1) & 0xff;
364 ff_hevc_clear_refs(s);
366 if (s->nal_unit_type >= 16 && s->nal_unit_type <= 23)
367 sh->no_output_of_prior_pics_flag = get_bits1(gb);
369 sh->pps_id = get_ue_golomb_long(gb);
370 if (sh->pps_id >= MAX_PPS_COUNT || !s->pps_list[sh->pps_id]) {
371 av_log(s->avctx, AV_LOG_ERROR, "PPS id out of range: %d\n", sh->pps_id);
372 return AVERROR_INVALIDDATA;
374 if (!sh->first_slice_in_pic_flag &&
375 s->pps != (HEVCPPS*)s->pps_list[sh->pps_id]->data) {
376 av_log(s->avctx, AV_LOG_ERROR, "PPS changed between slices.\n");
377 return AVERROR_INVALIDDATA;
379 s->pps = (HEVCPPS*)s->pps_list[sh->pps_id]->data;
381 if (s->sps != (HEVCSPS*)s->sps_list[s->pps->sps_id]->data) {
382 s->sps = (HEVCSPS*)s->sps_list[s->pps->sps_id]->data;
383 ff_hevc_clear_refs(s);
384 ret = set_sps(s, s->sps);
388 s->seq_decode = (s->seq_decode + 1) & 0xff;
392 s->avctx->profile = s->sps->ptl.general_ptl.profile_idc;
393 s->avctx->level = s->sps->ptl.general_ptl.level_idc;
395 sh->dependent_slice_segment_flag = 0;
396 if (!sh->first_slice_in_pic_flag) {
397 int slice_address_length;
399 if (s->pps->dependent_slice_segments_enabled_flag)
400 sh->dependent_slice_segment_flag = get_bits1(gb);
402 slice_address_length = av_ceil_log2(s->sps->ctb_width *
404 sh->slice_segment_addr = get_bits(gb, slice_address_length);
405 if (sh->slice_segment_addr >= s->sps->ctb_width * s->sps->ctb_height) {
406 av_log(s->avctx, AV_LOG_ERROR,
407 "Invalid slice segment address: %u.\n",
408 sh->slice_segment_addr);
409 return AVERROR_INVALIDDATA;
412 if (!sh->dependent_slice_segment_flag) {
413 sh->slice_addr = sh->slice_segment_addr;
417 sh->slice_segment_addr = sh->slice_addr = 0;
419 s->slice_initialized = 0;
422 if (!sh->dependent_slice_segment_flag) {
423 s->slice_initialized = 0;
425 for (i = 0; i < s->pps->num_extra_slice_header_bits; i++)
426 skip_bits(gb, 1); // slice_reserved_undetermined_flag[]
428 sh->slice_type = get_ue_golomb_long(gb);
429 if (!(sh->slice_type == I_SLICE ||
430 sh->slice_type == P_SLICE ||
431 sh->slice_type == B_SLICE)) {
432 av_log(s->avctx, AV_LOG_ERROR, "Unknown slice type: %d.\n",
434 return AVERROR_INVALIDDATA;
436 if (IS_IRAP(s) && sh->slice_type != I_SLICE) {
437 av_log(s->avctx, AV_LOG_ERROR, "Inter slices in an IRAP frame.\n");
438 return AVERROR_INVALIDDATA;
441 if (s->pps->output_flag_present_flag)
442 sh->pic_output_flag = get_bits1(gb);
444 if (s->sps->separate_colour_plane_flag)
445 sh->colour_plane_id = get_bits(gb, 2);
448 int short_term_ref_pic_set_sps_flag, poc;
450 sh->pic_order_cnt_lsb = get_bits(gb, s->sps->log2_max_poc_lsb);
451 poc = ff_hevc_compute_poc(s, sh->pic_order_cnt_lsb);
452 if (!sh->first_slice_in_pic_flag && poc != s->poc) {
453 av_log(s->avctx, AV_LOG_WARNING,
454 "Ignoring POC change between slices: %d -> %d\n", s->poc, poc);
455 if (s->avctx->err_recognition & AV_EF_EXPLODE)
456 return AVERROR_INVALIDDATA;
461 short_term_ref_pic_set_sps_flag = get_bits1(gb);
462 if (!short_term_ref_pic_set_sps_flag) {
463 ret = ff_hevc_decode_short_term_rps(s, &sh->slice_rps, s->sps, 1);
467 sh->short_term_rps = &sh->slice_rps;
469 int numbits, rps_idx;
471 if (!s->sps->nb_st_rps) {
472 av_log(s->avctx, AV_LOG_ERROR, "No ref lists in the SPS.\n");
473 return AVERROR_INVALIDDATA;
476 numbits = av_ceil_log2(s->sps->nb_st_rps);
477 rps_idx = numbits > 0 ? get_bits(gb, numbits) : 0;
478 sh->short_term_rps = &s->sps->st_rps[rps_idx];
481 ret = decode_lt_rps(s, &sh->long_term_rps, gb);
483 av_log(s->avctx, AV_LOG_WARNING, "Invalid long term RPS.\n");
484 if (s->avctx->err_recognition & AV_EF_EXPLODE)
485 return AVERROR_INVALIDDATA;
488 if (s->sps->sps_temporal_mvp_enabled_flag)
489 sh->slice_temporal_mvp_enabled_flag = get_bits1(gb);
491 sh->slice_temporal_mvp_enabled_flag = 0;
493 s->sh.short_term_rps = NULL;
498 if (s->temporal_id == 0 &&
499 s->nal_unit_type != NAL_TRAIL_N &&
500 s->nal_unit_type != NAL_TSA_N &&
501 s->nal_unit_type != NAL_STSA_N &&
502 s->nal_unit_type != NAL_RADL_N &&
503 s->nal_unit_type != NAL_RADL_R &&
504 s->nal_unit_type != NAL_RASL_N &&
505 s->nal_unit_type != NAL_RASL_R)
508 if (s->sps->sao_enabled) {
509 sh->slice_sample_adaptive_offset_flag[0] = get_bits1(gb);
510 sh->slice_sample_adaptive_offset_flag[1] =
511 sh->slice_sample_adaptive_offset_flag[2] = get_bits1(gb);
513 sh->slice_sample_adaptive_offset_flag[0] = 0;
514 sh->slice_sample_adaptive_offset_flag[1] = 0;
515 sh->slice_sample_adaptive_offset_flag[2] = 0;
518 sh->nb_refs[L0] = sh->nb_refs[L1] = 0;
519 if (sh->slice_type == P_SLICE || sh->slice_type == B_SLICE) {
522 sh->nb_refs[L0] = s->pps->num_ref_idx_l0_default_active;
523 if (sh->slice_type == B_SLICE)
524 sh->nb_refs[L1] = s->pps->num_ref_idx_l1_default_active;
526 if (get_bits1(gb)) { // num_ref_idx_active_override_flag
527 sh->nb_refs[L0] = get_ue_golomb_long(gb) + 1;
528 if (sh->slice_type == B_SLICE)
529 sh->nb_refs[L1] = get_ue_golomb_long(gb) + 1;
531 if (sh->nb_refs[L0] > MAX_REFS || sh->nb_refs[L1] > MAX_REFS) {
532 av_log(s->avctx, AV_LOG_ERROR, "Too many refs: %d/%d.\n",
533 sh->nb_refs[L0], sh->nb_refs[L1]);
534 return AVERROR_INVALIDDATA;
537 sh->rpl_modification_flag[0] = 0;
538 sh->rpl_modification_flag[1] = 0;
539 nb_refs = ff_hevc_frame_nb_refs(s);
541 av_log(s->avctx, AV_LOG_ERROR, "Zero refs for a frame with P or B slices.\n");
542 return AVERROR_INVALIDDATA;
545 if (s->pps->lists_modification_present_flag && nb_refs > 1) {
546 sh->rpl_modification_flag[0] = get_bits1(gb);
547 if (sh->rpl_modification_flag[0]) {
548 for (i = 0; i < sh->nb_refs[L0]; i++)
549 sh->list_entry_lx[0][i] = get_bits(gb, av_ceil_log2(nb_refs));
552 if (sh->slice_type == B_SLICE) {
553 sh->rpl_modification_flag[1] = get_bits1(gb);
554 if (sh->rpl_modification_flag[1] == 1)
555 for (i = 0; i < sh->nb_refs[L1]; i++)
556 sh->list_entry_lx[1][i] = get_bits(gb, av_ceil_log2(nb_refs));
560 if (sh->slice_type == B_SLICE)
561 sh->mvd_l1_zero_flag = get_bits1(gb);
563 if (s->pps->cabac_init_present_flag)
564 sh->cabac_init_flag = get_bits1(gb);
566 sh->cabac_init_flag = 0;
568 sh->collocated_ref_idx = 0;
569 if (sh->slice_temporal_mvp_enabled_flag) {
570 sh->collocated_list = L0;
571 if (sh->slice_type == B_SLICE)
572 sh->collocated_list = !get_bits1(gb);
574 if (sh->nb_refs[sh->collocated_list] > 1) {
575 sh->collocated_ref_idx = get_ue_golomb_long(gb);
576 if (sh->collocated_ref_idx >= sh->nb_refs[sh->collocated_list]) {
577 av_log(s->avctx, AV_LOG_ERROR,
578 "Invalid collocated_ref_idx: %d.\n",
579 sh->collocated_ref_idx);
580 return AVERROR_INVALIDDATA;
585 if ((s->pps->weighted_pred_flag && sh->slice_type == P_SLICE) ||
586 (s->pps->weighted_bipred_flag && sh->slice_type == B_SLICE)) {
587 pred_weight_table(s, gb);
590 sh->max_num_merge_cand = 5 - get_ue_golomb_long(gb);
591 if (sh->max_num_merge_cand < 1 || sh->max_num_merge_cand > 5) {
592 av_log(s->avctx, AV_LOG_ERROR,
593 "Invalid number of merging MVP candidates: %d.\n",
594 sh->max_num_merge_cand);
595 return AVERROR_INVALIDDATA;
599 sh->slice_qp_delta = get_se_golomb(gb);
601 if (s->pps->pic_slice_level_chroma_qp_offsets_present_flag) {
602 sh->slice_cb_qp_offset = get_se_golomb(gb);
603 sh->slice_cr_qp_offset = get_se_golomb(gb);
605 sh->slice_cb_qp_offset = 0;
606 sh->slice_cr_qp_offset = 0;
609 if (s->pps->deblocking_filter_control_present_flag) {
610 int deblocking_filter_override_flag = 0;
612 if (s->pps->deblocking_filter_override_enabled_flag)
613 deblocking_filter_override_flag = get_bits1(gb);
615 if (deblocking_filter_override_flag) {
616 sh->disable_deblocking_filter_flag = get_bits1(gb);
617 if (!sh->disable_deblocking_filter_flag) {
618 sh->beta_offset = get_se_golomb(gb) * 2;
619 sh->tc_offset = get_se_golomb(gb) * 2;
622 sh->disable_deblocking_filter_flag = s->pps->disable_dbf;
623 sh->beta_offset = s->pps->beta_offset;
624 sh->tc_offset = s->pps->tc_offset;
627 sh->disable_deblocking_filter_flag = 0;
632 if (s->pps->seq_loop_filter_across_slices_enabled_flag &&
633 (sh->slice_sample_adaptive_offset_flag[0] ||
634 sh->slice_sample_adaptive_offset_flag[1] ||
635 !sh->disable_deblocking_filter_flag)) {
636 sh->slice_loop_filter_across_slices_enabled_flag = get_bits1(gb);
638 sh->slice_loop_filter_across_slices_enabled_flag = s->pps->seq_loop_filter_across_slices_enabled_flag;
640 } else if (!s->slice_initialized) {
641 av_log(s->avctx, AV_LOG_ERROR, "Independent slice segment missing.\n");
642 return AVERROR_INVALIDDATA;
645 sh->num_entry_point_offsets = 0;
646 if (s->pps->tiles_enabled_flag || s->pps->entropy_coding_sync_enabled_flag) {
647 sh->num_entry_point_offsets = get_ue_golomb_long(gb);
648 if (sh->num_entry_point_offsets > 0) {
649 int offset_len = get_ue_golomb_long(gb) + 1;
650 int segments = offset_len >> 4;
651 int rest = (offset_len & 15);
652 av_freep(&sh->entry_point_offset);
653 av_freep(&sh->offset);
655 sh->entry_point_offset = av_malloc(sh->num_entry_point_offsets * sizeof(int));
656 sh->offset = av_malloc(sh->num_entry_point_offsets * sizeof(int));
657 sh->size = av_malloc(sh->num_entry_point_offsets * sizeof(int));
658 if (!sh->entry_point_offset || !sh->offset || !sh->size) {
659 sh->num_entry_point_offsets = 0;
660 av_log(s->avctx, AV_LOG_ERROR, "Failed to allocate memory\n");
661 return AVERROR(ENOMEM);
663 for (i = 0; i < sh->num_entry_point_offsets; i++) {
665 for (j = 0; j < segments; j++) {
667 val += get_bits(gb, 16);
671 val += get_bits(gb, rest);
673 sh->entry_point_offset[i] = val + 1; // +1; // +1 to get the size
675 if (s->threads_number > 1 && (s->pps->num_tile_rows > 1 || s->pps->num_tile_columns > 1)) {
676 s->enable_parallel_tiles = 0; // TODO: you can enable tiles in parallel here
677 s->threads_number = 1;
679 s->enable_parallel_tiles = 0;
681 s->enable_parallel_tiles = 0;
684 if (s->pps->slice_header_extension_present_flag) {
685 unsigned int length = get_ue_golomb_long(gb);
686 for (i = 0; i < length; i++)
687 skip_bits(gb, 8); // slice_header_extension_data_byte
690 // Inferred parameters
691 sh->slice_qp = 26U + s->pps->pic_init_qp_minus26 + sh->slice_qp_delta;
692 if (sh->slice_qp > 51 ||
693 sh->slice_qp < -s->sps->qp_bd_offset) {
694 av_log(s->avctx, AV_LOG_ERROR,
695 "The slice_qp %d is outside the valid range "
698 -s->sps->qp_bd_offset);
699 return AVERROR_INVALIDDATA;
702 sh->slice_ctb_addr_rs = sh->slice_segment_addr;
704 if (!s->sh.slice_ctb_addr_rs && s->sh.dependent_slice_segment_flag) {
705 av_log(s->avctx, AV_LOG_ERROR, "Impossible slice segment.\n");
706 return AVERROR_INVALIDDATA;
709 s->HEVClc->first_qp_group = !s->sh.dependent_slice_segment_flag;
711 if (!s->pps->cu_qp_delta_enabled_flag)
712 s->HEVClc->qp_y = s->sh.slice_qp;
714 s->slice_initialized = 1;
719 #define CTB(tab, x, y) ((tab)[(y) * s->sps->ctb_width + (x)])
721 #define SET_SAO(elem, value) \
723 if (!sao_merge_up_flag && !sao_merge_left_flag) \
725 else if (sao_merge_left_flag) \
726 sao->elem = CTB(s->sao, rx-1, ry).elem; \
727 else if (sao_merge_up_flag) \
728 sao->elem = CTB(s->sao, rx, ry-1).elem; \
733 static void hls_sao_param(HEVCContext *s, int rx, int ry)
735 HEVCLocalContext *lc = s->HEVClc;
736 int sao_merge_left_flag = 0;
737 int sao_merge_up_flag = 0;
738 int shift = s->sps->bit_depth - FFMIN(s->sps->bit_depth, 10);
739 SAOParams *sao = &CTB(s->sao, rx, ry);
742 if (s->sh.slice_sample_adaptive_offset_flag[0] ||
743 s->sh.slice_sample_adaptive_offset_flag[1]) {
745 if (lc->ctb_left_flag)
746 sao_merge_left_flag = ff_hevc_sao_merge_flag_decode(s);
748 if (ry > 0 && !sao_merge_left_flag) {
750 sao_merge_up_flag = ff_hevc_sao_merge_flag_decode(s);
754 for (c_idx = 0; c_idx < 3; c_idx++) {
755 if (!s->sh.slice_sample_adaptive_offset_flag[c_idx]) {
756 sao->type_idx[c_idx] = SAO_NOT_APPLIED;
761 sao->type_idx[2] = sao->type_idx[1];
762 sao->eo_class[2] = sao->eo_class[1];
764 SET_SAO(type_idx[c_idx], ff_hevc_sao_type_idx_decode(s));
767 if (sao->type_idx[c_idx] == SAO_NOT_APPLIED)
770 for (i = 0; i < 4; i++)
771 SET_SAO(offset_abs[c_idx][i], ff_hevc_sao_offset_abs_decode(s));
773 if (sao->type_idx[c_idx] == SAO_BAND) {
774 for (i = 0; i < 4; i++) {
775 if (sao->offset_abs[c_idx][i]) {
776 SET_SAO(offset_sign[c_idx][i],
777 ff_hevc_sao_offset_sign_decode(s));
779 sao->offset_sign[c_idx][i] = 0;
782 SET_SAO(band_position[c_idx], ff_hevc_sao_band_position_decode(s));
783 } else if (c_idx != 2) {
784 SET_SAO(eo_class[c_idx], ff_hevc_sao_eo_class_decode(s));
787 // Inferred parameters
788 sao->offset_val[c_idx][0] = 0;
789 for (i = 0; i < 4; i++) {
790 sao->offset_val[c_idx][i + 1] = sao->offset_abs[c_idx][i] << shift;
791 if (sao->type_idx[c_idx] == SAO_EDGE) {
793 sao->offset_val[c_idx][i + 1] = -sao->offset_val[c_idx][i + 1];
794 } else if (sao->offset_sign[c_idx][i]) {
795 sao->offset_val[c_idx][i + 1] = -sao->offset_val[c_idx][i + 1];
804 static int hls_transform_unit(HEVCContext *s, int x0, int y0,
805 int xBase, int yBase, int cb_xBase, int cb_yBase,
806 int log2_cb_size, int log2_trafo_size,
807 int trafo_depth, int blk_idx)
809 HEVCLocalContext *lc = s->HEVClc;
811 if (lc->cu.pred_mode == MODE_INTRA) {
812 int trafo_size = 1 << log2_trafo_size;
813 ff_hevc_set_neighbour_available(s, x0, y0, trafo_size, trafo_size);
815 s->hpc.intra_pred(s, x0, y0, log2_trafo_size, 0);
816 if (log2_trafo_size > 2) {
817 trafo_size = trafo_size << (s->sps->hshift[1] - 1);
818 ff_hevc_set_neighbour_available(s, x0, y0, trafo_size, trafo_size);
819 s->hpc.intra_pred(s, x0, y0, log2_trafo_size - 1, 1);
820 s->hpc.intra_pred(s, x0, y0, log2_trafo_size - 1, 2);
821 } else if (blk_idx == 3) {
822 trafo_size = trafo_size << s->sps->hshift[1];
823 ff_hevc_set_neighbour_available(s, xBase, yBase,
824 trafo_size, trafo_size);
825 s->hpc.intra_pred(s, xBase, yBase, log2_trafo_size, 1);
826 s->hpc.intra_pred(s, xBase, yBase, log2_trafo_size, 2);
830 if (lc->tt.cbf_luma ||
831 SAMPLE_CBF(lc->tt.cbf_cb[trafo_depth], x0, y0) ||
832 SAMPLE_CBF(lc->tt.cbf_cr[trafo_depth], x0, y0)) {
833 int scan_idx = SCAN_DIAG;
834 int scan_idx_c = SCAN_DIAG;
836 if (s->pps->cu_qp_delta_enabled_flag && !lc->tu.is_cu_qp_delta_coded) {
837 lc->tu.cu_qp_delta = ff_hevc_cu_qp_delta_abs(s);
838 if (lc->tu.cu_qp_delta != 0)
839 if (ff_hevc_cu_qp_delta_sign_flag(s) == 1)
840 lc->tu.cu_qp_delta = -lc->tu.cu_qp_delta;
841 lc->tu.is_cu_qp_delta_coded = 1;
843 if (lc->tu.cu_qp_delta < -(26 + s->sps->qp_bd_offset / 2) ||
844 lc->tu.cu_qp_delta > (25 + s->sps->qp_bd_offset / 2)) {
845 av_log(s->avctx, AV_LOG_ERROR,
846 "The cu_qp_delta %d is outside the valid range "
849 -(26 + s->sps->qp_bd_offset / 2),
850 (25 + s->sps->qp_bd_offset / 2));
851 return AVERROR_INVALIDDATA;
854 ff_hevc_set_qPy(s, x0, y0, cb_xBase, cb_yBase, log2_cb_size);
857 if (lc->cu.pred_mode == MODE_INTRA && log2_trafo_size < 4) {
858 if (lc->tu.cur_intra_pred_mode >= 6 &&
859 lc->tu.cur_intra_pred_mode <= 14) {
860 scan_idx = SCAN_VERT;
861 } else if (lc->tu.cur_intra_pred_mode >= 22 &&
862 lc->tu.cur_intra_pred_mode <= 30) {
863 scan_idx = SCAN_HORIZ;
866 if (lc->pu.intra_pred_mode_c >= 6 &&
867 lc->pu.intra_pred_mode_c <= 14) {
868 scan_idx_c = SCAN_VERT;
869 } else if (lc->pu.intra_pred_mode_c >= 22 &&
870 lc->pu.intra_pred_mode_c <= 30) {
871 scan_idx_c = SCAN_HORIZ;
876 ff_hevc_hls_residual_coding(s, x0, y0, log2_trafo_size, scan_idx, 0);
877 if (log2_trafo_size > 2) {
878 if (SAMPLE_CBF(lc->tt.cbf_cb[trafo_depth], x0, y0))
879 ff_hevc_hls_residual_coding(s, x0, y0, log2_trafo_size - 1, scan_idx_c, 1);
880 if (SAMPLE_CBF(lc->tt.cbf_cr[trafo_depth], x0, y0))
881 ff_hevc_hls_residual_coding(s, x0, y0, log2_trafo_size - 1, scan_idx_c, 2);
882 } else if (blk_idx == 3) {
883 if (SAMPLE_CBF(lc->tt.cbf_cb[trafo_depth], xBase, yBase))
884 ff_hevc_hls_residual_coding(s, xBase, yBase, log2_trafo_size, scan_idx_c, 1);
885 if (SAMPLE_CBF(lc->tt.cbf_cr[trafo_depth], xBase, yBase))
886 ff_hevc_hls_residual_coding(s, xBase, yBase, log2_trafo_size, scan_idx_c, 2);
892 static void set_deblocking_bypass(HEVCContext *s, int x0, int y0, int log2_cb_size)
894 int cb_size = 1 << log2_cb_size;
895 int log2_min_pu_size = s->sps->log2_min_pu_size;
897 int min_pu_width = s->sps->min_pu_width;
898 int x_end = FFMIN(x0 + cb_size, s->sps->width);
899 int y_end = FFMIN(y0 + cb_size, s->sps->height);
902 for (j = (y0 >> log2_min_pu_size); j < (y_end >> log2_min_pu_size); j++)
903 for (i = (x0 >> log2_min_pu_size); i < (x_end >> log2_min_pu_size); i++)
904 s->is_pcm[i + j * min_pu_width] = 2;
907 static int hls_transform_tree(HEVCContext *s, int x0, int y0,
908 int xBase, int yBase, int cb_xBase, int cb_yBase,
909 int log2_cb_size, int log2_trafo_size,
910 int trafo_depth, int blk_idx)
912 HEVCLocalContext *lc = s->HEVClc;
913 uint8_t split_transform_flag;
916 if (trafo_depth > 0 && log2_trafo_size == 2) {
917 SAMPLE_CBF(lc->tt.cbf_cb[trafo_depth], x0, y0) =
918 SAMPLE_CBF(lc->tt.cbf_cb[trafo_depth - 1], xBase, yBase);
919 SAMPLE_CBF(lc->tt.cbf_cr[trafo_depth], x0, y0) =
920 SAMPLE_CBF(lc->tt.cbf_cr[trafo_depth - 1], xBase, yBase);
922 SAMPLE_CBF(lc->tt.cbf_cb[trafo_depth], x0, y0) =
923 SAMPLE_CBF(lc->tt.cbf_cr[trafo_depth], x0, y0) = 0;
926 if (lc->cu.intra_split_flag) {
927 if (trafo_depth == 1)
928 lc->tu.cur_intra_pred_mode = lc->pu.intra_pred_mode[blk_idx];
930 lc->tu.cur_intra_pred_mode = lc->pu.intra_pred_mode[0];
935 lc->tt.inter_split_flag = s->sps->max_transform_hierarchy_depth_inter == 0 &&
936 lc->cu.pred_mode == MODE_INTER &&
937 lc->cu.part_mode != PART_2Nx2N &&
940 if (log2_trafo_size <= s->sps->log2_max_trafo_size &&
941 log2_trafo_size > s->sps->log2_min_tb_size &&
942 trafo_depth < lc->cu.max_trafo_depth &&
943 !(lc->cu.intra_split_flag && trafo_depth == 0)) {
944 split_transform_flag = ff_hevc_split_transform_flag_decode(s, log2_trafo_size);
946 split_transform_flag = log2_trafo_size > s->sps->log2_max_trafo_size ||
947 (lc->cu.intra_split_flag && trafo_depth == 0) ||
948 lc->tt.inter_split_flag;
951 if (log2_trafo_size > 2) {
952 if (trafo_depth == 0 ||
953 SAMPLE_CBF(lc->tt.cbf_cb[trafo_depth - 1], xBase, yBase)) {
954 SAMPLE_CBF(lc->tt.cbf_cb[trafo_depth], x0, y0) =
955 ff_hevc_cbf_cb_cr_decode(s, trafo_depth);
958 if (trafo_depth == 0 ||
959 SAMPLE_CBF(lc->tt.cbf_cr[trafo_depth - 1], xBase, yBase)) {
960 SAMPLE_CBF(lc->tt.cbf_cr[trafo_depth], x0, y0) =
961 ff_hevc_cbf_cb_cr_decode(s, trafo_depth);
965 if (split_transform_flag) {
966 int x1 = x0 + ((1 << log2_trafo_size) >> 1);
967 int y1 = y0 + ((1 << log2_trafo_size) >> 1);
969 ret = hls_transform_tree(s, x0, y0, x0, y0, cb_xBase, cb_yBase,
970 log2_cb_size, log2_trafo_size - 1,
974 ret = hls_transform_tree(s, x1, y0, x0, y0, cb_xBase, cb_yBase,
975 log2_cb_size, log2_trafo_size - 1,
979 ret = hls_transform_tree(s, x0, y1, x0, y0, cb_xBase, cb_yBase,
980 log2_cb_size, log2_trafo_size - 1,
984 ret = hls_transform_tree(s, x1, y1, x0, y0, cb_xBase, cb_yBase,
985 log2_cb_size, log2_trafo_size - 1,
990 int min_tu_size = 1 << s->sps->log2_min_tb_size;
991 int log2_min_tu_size = s->sps->log2_min_tb_size;
992 int min_tu_width = s->sps->min_tb_width;
994 if (lc->cu.pred_mode == MODE_INTRA || trafo_depth != 0 ||
995 SAMPLE_CBF(lc->tt.cbf_cb[trafo_depth], x0, y0) ||
996 SAMPLE_CBF(lc->tt.cbf_cr[trafo_depth], x0, y0)) {
997 lc->tt.cbf_luma = ff_hevc_cbf_luma_decode(s, trafo_depth);
1000 ret = hls_transform_unit(s, x0, y0, xBase, yBase, cb_xBase, cb_yBase,
1001 log2_cb_size, log2_trafo_size, trafo_depth,
1005 // TODO: store cbf_luma somewhere else
1006 if (lc->tt.cbf_luma) {
1008 for (i = 0; i < (1 << log2_trafo_size); i += min_tu_size)
1009 for (j = 0; j < (1 << log2_trafo_size); j += min_tu_size) {
1010 int x_tu = (x0 + j) >> log2_min_tu_size;
1011 int y_tu = (y0 + i) >> log2_min_tu_size;
1012 s->cbf_luma[y_tu * min_tu_width + x_tu] = 1;
1015 if (!s->sh.disable_deblocking_filter_flag) {
1016 ff_hevc_deblocking_boundary_strengths(s, x0, y0, log2_trafo_size,
1017 lc->slice_or_tiles_up_boundary,
1018 lc->slice_or_tiles_left_boundary);
1019 if (s->pps->transquant_bypass_enable_flag &&
1020 lc->cu.cu_transquant_bypass_flag)
1021 set_deblocking_bypass(s, x0, y0, log2_trafo_size);
1027 static int hls_pcm_sample(HEVCContext *s, int x0, int y0, int log2_cb_size)
1029 //TODO: non-4:2:0 support
1030 HEVCLocalContext *lc = s->HEVClc;
1032 int cb_size = 1 << log2_cb_size;
1033 int stride0 = s->frame->linesize[0];
1034 uint8_t *dst0 = &s->frame->data[0][y0 * stride0 + (x0 << s->sps->pixel_shift)];
1035 int stride1 = s->frame->linesize[1];
1036 uint8_t *dst1 = &s->frame->data[1][(y0 >> s->sps->vshift[1]) * stride1 + ((x0 >> s->sps->hshift[1]) << s->sps->pixel_shift)];
1037 int stride2 = s->frame->linesize[2];
1038 uint8_t *dst2 = &s->frame->data[2][(y0 >> s->sps->vshift[2]) * stride2 + ((x0 >> s->sps->hshift[2]) << s->sps->pixel_shift)];
1040 int length = cb_size * cb_size * s->sps->pcm.bit_depth + ((cb_size * cb_size) >> 1) * s->sps->pcm.bit_depth_chroma;
1041 const uint8_t *pcm = skip_bytes(&s->HEVClc->cc, (length + 7) >> 3);
1044 ff_hevc_deblocking_boundary_strengths(s, x0, y0, log2_cb_size,
1045 lc->slice_or_tiles_up_boundary,
1046 lc->slice_or_tiles_left_boundary);
1048 ret = init_get_bits(&gb, pcm, length);
1052 s->hevcdsp.put_pcm(dst0, stride0, cb_size, &gb, s->sps->pcm.bit_depth);
1053 s->hevcdsp.put_pcm(dst1, stride1, cb_size / 2, &gb, s->sps->pcm.bit_depth_chroma);
1054 s->hevcdsp.put_pcm(dst2, stride2, cb_size / 2, &gb, s->sps->pcm.bit_depth_chroma);
1059 * 8.5.3.2.2.1 Luma sample interpolation process
1061 * @param s HEVC decoding context
1062 * @param dst target buffer for block data at block position
1063 * @param dststride stride of the dst buffer
1064 * @param ref reference picture buffer at origin (0, 0)
1065 * @param mv motion vector (relative to block position) to get pixel data from
1066 * @param x_off horizontal position of block from origin (0, 0)
1067 * @param y_off vertical position of block from origin (0, 0)
1068 * @param block_w width of block
1069 * @param block_h height of block
1071 static void luma_mc(HEVCContext *s, int16_t *dst, ptrdiff_t dststride,
1072 AVFrame *ref, const Mv *mv, int x_off, int y_off,
1073 int block_w, int block_h)
1075 HEVCLocalContext *lc = s->HEVClc;
1076 uint8_t *src = ref->data[0];
1077 ptrdiff_t srcstride = ref->linesize[0];
1078 int pic_width = s->sps->width;
1079 int pic_height = s->sps->height;
1083 int extra_left = ff_hevc_qpel_extra_before[mx];
1084 int extra_top = ff_hevc_qpel_extra_before[my];
1086 x_off += mv->x >> 2;
1087 y_off += mv->y >> 2;
1088 src += y_off * srcstride + (x_off << s->sps->pixel_shift);
1090 if (x_off < extra_left || y_off < extra_top ||
1091 x_off >= pic_width - block_w - ff_hevc_qpel_extra_after[mx] ||
1092 y_off >= pic_height - block_h - ff_hevc_qpel_extra_after[my]) {
1093 const int edge_emu_stride = EDGE_EMU_BUFFER_STRIDE << s->sps->pixel_shift;
1094 int offset = extra_top * srcstride + (extra_left << s->sps->pixel_shift);
1095 int buf_offset = extra_top *
1096 edge_emu_stride + (extra_left << s->sps->pixel_shift);
1098 s->vdsp.emulated_edge_mc(lc->edge_emu_buffer, src - offset,
1099 edge_emu_stride, srcstride,
1100 block_w + ff_hevc_qpel_extra[mx],
1101 block_h + ff_hevc_qpel_extra[my],
1102 x_off - extra_left, y_off - extra_top,
1103 pic_width, pic_height);
1104 src = lc->edge_emu_buffer + buf_offset;
1105 srcstride = edge_emu_stride;
1107 s->hevcdsp.put_hevc_qpel[my][mx](dst, dststride, src, srcstride, block_w,
1108 block_h, lc->mc_buffer);
1112 * 8.5.3.2.2.2 Chroma sample interpolation process
1114 * @param s HEVC decoding context
1115 * @param dst1 target buffer for block data at block position (U plane)
1116 * @param dst2 target buffer for block data at block position (V plane)
1117 * @param dststride stride of the dst1 and dst2 buffers
1118 * @param ref reference picture buffer at origin (0, 0)
1119 * @param mv motion vector (relative to block position) to get pixel data from
1120 * @param x_off horizontal position of block from origin (0, 0)
1121 * @param y_off vertical position of block from origin (0, 0)
1122 * @param block_w width of block
1123 * @param block_h height of block
1125 static void chroma_mc(HEVCContext *s, int16_t *dst1, int16_t *dst2,
1126 ptrdiff_t dststride, AVFrame *ref, const Mv *mv,
1127 int x_off, int y_off, int block_w, int block_h)
1129 HEVCLocalContext *lc = s->HEVClc;
1130 uint8_t *src1 = ref->data[1];
1131 uint8_t *src2 = ref->data[2];
1132 ptrdiff_t src1stride = ref->linesize[1];
1133 ptrdiff_t src2stride = ref->linesize[2];
1134 int pic_width = s->sps->width >> 1;
1135 int pic_height = s->sps->height >> 1;
1140 x_off += mv->x >> 3;
1141 y_off += mv->y >> 3;
1142 src1 += y_off * src1stride + (x_off << s->sps->pixel_shift);
1143 src2 += y_off * src2stride + (x_off << s->sps->pixel_shift);
1145 if (x_off < EPEL_EXTRA_BEFORE || y_off < EPEL_EXTRA_AFTER ||
1146 x_off >= pic_width - block_w - EPEL_EXTRA_AFTER ||
1147 y_off >= pic_height - block_h - EPEL_EXTRA_AFTER) {
1148 const int edge_emu_stride = EDGE_EMU_BUFFER_STRIDE << s->sps->pixel_shift;
1149 int offset1 = EPEL_EXTRA_BEFORE * (src1stride + (1 << s->sps->pixel_shift));
1150 int buf_offset1 = EPEL_EXTRA_BEFORE *
1151 (edge_emu_stride + (1 << s->sps->pixel_shift));
1152 int offset2 = EPEL_EXTRA_BEFORE * (src2stride + (1 << s->sps->pixel_shift));
1153 int buf_offset2 = EPEL_EXTRA_BEFORE *
1154 (edge_emu_stride + (1 << s->sps->pixel_shift));
1156 s->vdsp.emulated_edge_mc(lc->edge_emu_buffer, src1 - offset1,
1157 edge_emu_stride, src1stride,
1158 block_w + EPEL_EXTRA, block_h + EPEL_EXTRA,
1159 x_off - EPEL_EXTRA_BEFORE,
1160 y_off - EPEL_EXTRA_BEFORE,
1161 pic_width, pic_height);
1163 src1 = lc->edge_emu_buffer + buf_offset1;
1164 src1stride = edge_emu_stride;
1165 s->hevcdsp.put_hevc_epel[!!my][!!mx](dst1, dststride, src1, src1stride,
1166 block_w, block_h, mx, my, lc->mc_buffer);
1168 s->vdsp.emulated_edge_mc(lc->edge_emu_buffer, src2 - offset2,
1169 edge_emu_stride, src2stride,
1170 block_w + EPEL_EXTRA, block_h + EPEL_EXTRA,
1171 x_off - EPEL_EXTRA_BEFORE,
1172 y_off - EPEL_EXTRA_BEFORE,
1173 pic_width, pic_height);
1174 src2 = lc->edge_emu_buffer + buf_offset2;
1175 src2stride = edge_emu_stride;
1177 s->hevcdsp.put_hevc_epel[!!my][!!mx](dst2, dststride, src2, src2stride,
1178 block_w, block_h, mx, my,
1181 s->hevcdsp.put_hevc_epel[!!my][!!mx](dst1, dststride, src1, src1stride,
1182 block_w, block_h, mx, my,
1184 s->hevcdsp.put_hevc_epel[!!my][!!mx](dst2, dststride, src2, src2stride,
1185 block_w, block_h, mx, my,
1190 static void hevc_await_progress(HEVCContext *s, HEVCFrame *ref,
1191 const Mv *mv, int y0, int height)
1193 int y = (mv->y >> 2) + y0 + height + 9;
1195 if (s->threads_type == FF_THREAD_FRAME )
1196 ff_thread_await_progress(&ref->tf, y, 0);
1199 static void hls_prediction_unit(HEVCContext *s, int x0, int y0,
1201 int log2_cb_size, int partIdx)
1203 #define POS(c_idx, x, y) \
1204 &s->frame->data[c_idx][((y) >> s->sps->vshift[c_idx]) * s->frame->linesize[c_idx] + \
1205 (((x) >> s->sps->hshift[c_idx]) << s->sps->pixel_shift)]
1206 HEVCLocalContext *lc = s->HEVClc;
1208 struct MvField current_mv = {{{ 0 }}};
1210 int min_pu_width = s->sps->min_pu_width;
1212 MvField *tab_mvf = s->ref->tab_mvf;
1213 RefPicList *refPicList = s->ref->refPicList;
1214 HEVCFrame *ref0, *ref1;
1216 int tmpstride = MAX_PB_SIZE;
1218 uint8_t *dst0 = POS(0, x0, y0);
1219 uint8_t *dst1 = POS(1, x0, y0);
1220 uint8_t *dst2 = POS(2, x0, y0);
1221 int log2_min_cb_size = s->sps->log2_min_cb_size;
1222 int min_cb_width = s->sps->min_cb_width;
1223 int x_cb = x0 >> log2_min_cb_size;
1224 int y_cb = y0 >> log2_min_cb_size;
1230 if (SAMPLE_CTB(s->skip_flag, x_cb, y_cb)) {
1231 if (s->sh.max_num_merge_cand > 1)
1232 merge_idx = ff_hevc_merge_idx_decode(s);
1236 ff_hevc_luma_mv_merge_mode(s, x0, y0,
1239 log2_cb_size, partIdx,
1240 merge_idx, ¤t_mv);
1241 x_pu = x0 >> s->sps->log2_min_pu_size;
1242 y_pu = y0 >> s->sps->log2_min_pu_size;
1244 for (i = 0; i < nPbW >> s->sps->log2_min_pu_size; i++)
1245 for (j = 0; j < nPbH >> s->sps->log2_min_pu_size; j++)
1246 tab_mvf[(y_pu + j) * min_pu_width + x_pu + i] = current_mv;
1247 } else { /* MODE_INTER */
1248 lc->pu.merge_flag = ff_hevc_merge_flag_decode(s);
1249 if (lc->pu.merge_flag) {
1250 if (s->sh.max_num_merge_cand > 1)
1251 merge_idx = ff_hevc_merge_idx_decode(s);
1255 ff_hevc_luma_mv_merge_mode(s, x0, y0, nPbW, nPbH, log2_cb_size,
1256 partIdx, merge_idx, ¤t_mv);
1257 x_pu = x0 >> s->sps->log2_min_pu_size;
1258 y_pu = y0 >> s->sps->log2_min_pu_size;
1260 for (i = 0; i < nPbW >> s->sps->log2_min_pu_size; i++)
1261 for (j = 0; j < nPbH >> s->sps->log2_min_pu_size; j++)
1262 tab_mvf[(y_pu + j) * min_pu_width + x_pu + i] = current_mv;
1264 enum InterPredIdc inter_pred_idc = PRED_L0;
1265 ff_hevc_set_neighbour_available(s, x0, y0, nPbW, nPbH);
1266 if (s->sh.slice_type == B_SLICE)
1267 inter_pred_idc = ff_hevc_inter_pred_idc_decode(s, nPbW, nPbH);
1269 if (inter_pred_idc != PRED_L1) {
1270 if (s->sh.nb_refs[L0]) {
1271 ref_idx[0] = ff_hevc_ref_idx_lx_decode(s, s->sh.nb_refs[L0]);
1272 current_mv.ref_idx[0] = ref_idx[0];
1274 current_mv.pred_flag[0] = 1;
1275 ff_hevc_hls_mvd_coding(s, x0, y0, 0);
1276 mvp_flag[0] = ff_hevc_mvp_lx_flag_decode(s);
1277 ff_hevc_luma_mv_mvp_mode(s, x0, y0, nPbW, nPbH, log2_cb_size,
1278 partIdx, merge_idx, ¤t_mv,
1280 current_mv.mv[0].x += lc->pu.mvd.x;
1281 current_mv.mv[0].y += lc->pu.mvd.y;
1284 if (inter_pred_idc != PRED_L0) {
1285 if (s->sh.nb_refs[L1]) {
1286 ref_idx[1] = ff_hevc_ref_idx_lx_decode(s, s->sh.nb_refs[L1]);
1287 current_mv.ref_idx[1] = ref_idx[1];
1290 if (s->sh.mvd_l1_zero_flag == 1 && inter_pred_idc == PRED_BI) {
1294 ff_hevc_hls_mvd_coding(s, x0, y0, 1);
1297 current_mv.pred_flag[1] = 1;
1298 mvp_flag[1] = ff_hevc_mvp_lx_flag_decode(s);
1299 ff_hevc_luma_mv_mvp_mode(s, x0, y0, nPbW, nPbH, log2_cb_size,
1300 partIdx, merge_idx, ¤t_mv,
1302 current_mv.mv[1].x += lc->pu.mvd.x;
1303 current_mv.mv[1].y += lc->pu.mvd.y;
1306 x_pu = x0 >> s->sps->log2_min_pu_size;
1307 y_pu = y0 >> s->sps->log2_min_pu_size;
1309 for (i = 0; i < nPbW >> s->sps->log2_min_pu_size; i++)
1310 for(j = 0; j < nPbH >> s->sps->log2_min_pu_size; j++)
1311 tab_mvf[(y_pu + j) * min_pu_width + x_pu + i] = current_mv;
1315 if (current_mv.pred_flag[0]) {
1316 ref0 = refPicList[0].ref[current_mv.ref_idx[0]];
1319 hevc_await_progress(s, ref0, ¤t_mv.mv[0], y0, nPbH);
1321 if (current_mv.pred_flag[1]) {
1322 ref1 = refPicList[1].ref[current_mv.ref_idx[1]];
1325 hevc_await_progress(s, ref1, ¤t_mv.mv[1], y0, nPbH);
1328 if (current_mv.pred_flag[0] && !current_mv.pred_flag[1]) {
1329 DECLARE_ALIGNED(16, int16_t, tmp[MAX_PB_SIZE * MAX_PB_SIZE]);
1330 DECLARE_ALIGNED(16, int16_t, tmp2[MAX_PB_SIZE * MAX_PB_SIZE]);
1332 luma_mc(s, tmp, tmpstride, ref0->frame,
1333 ¤t_mv.mv[0], x0, y0, nPbW, nPbH);
1335 if ((s->sh.slice_type == P_SLICE && s->pps->weighted_pred_flag) ||
1336 (s->sh.slice_type == B_SLICE && s->pps->weighted_bipred_flag)) {
1337 s->hevcdsp.weighted_pred(s->sh.luma_log2_weight_denom,
1338 s->sh.luma_weight_l0[current_mv.ref_idx[0]],
1339 s->sh.luma_offset_l0[current_mv.ref_idx[0]],
1340 dst0, s->frame->linesize[0], tmp,
1341 tmpstride, nPbW, nPbH);
1343 s->hevcdsp.put_unweighted_pred(dst0, s->frame->linesize[0], tmp, tmpstride, nPbW, nPbH);
1345 chroma_mc(s, tmp, tmp2, tmpstride, ref0->frame,
1346 ¤t_mv.mv[0], x0 / 2, y0 / 2, nPbW / 2, nPbH / 2);
1348 if ((s->sh.slice_type == P_SLICE && s->pps->weighted_pred_flag) ||
1349 (s->sh.slice_type == B_SLICE && s->pps->weighted_bipred_flag)) {
1350 s->hevcdsp.weighted_pred(s->sh.chroma_log2_weight_denom,
1351 s->sh.chroma_weight_l0[current_mv.ref_idx[0]][0],
1352 s->sh.chroma_offset_l0[current_mv.ref_idx[0]][0],
1353 dst1, s->frame->linesize[1], tmp, tmpstride,
1354 nPbW / 2, nPbH / 2);
1355 s->hevcdsp.weighted_pred(s->sh.chroma_log2_weight_denom,
1356 s->sh.chroma_weight_l0[current_mv.ref_idx[0]][1],
1357 s->sh.chroma_offset_l0[current_mv.ref_idx[0]][1],
1358 dst2, s->frame->linesize[2], tmp2, tmpstride,
1359 nPbW / 2, nPbH / 2);
1361 s->hevcdsp.put_unweighted_pred(dst1, s->frame->linesize[1], tmp, tmpstride, nPbW/2, nPbH/2);
1362 s->hevcdsp.put_unweighted_pred(dst2, s->frame->linesize[2], tmp2, tmpstride, nPbW/2, nPbH/2);
1364 } else if (!current_mv.pred_flag[0] && current_mv.pred_flag[1]) {
1365 DECLARE_ALIGNED(16, int16_t, tmp [MAX_PB_SIZE * MAX_PB_SIZE]);
1366 DECLARE_ALIGNED(16, int16_t, tmp2[MAX_PB_SIZE * MAX_PB_SIZE]);
1371 luma_mc(s, tmp, tmpstride, ref1->frame,
1372 ¤t_mv.mv[1], x0, y0, nPbW, nPbH);
1374 if ((s->sh.slice_type == P_SLICE && s->pps->weighted_pred_flag) ||
1375 (s->sh.slice_type == B_SLICE && s->pps->weighted_bipred_flag)) {
1376 s->hevcdsp.weighted_pred(s->sh.luma_log2_weight_denom,
1377 s->sh.luma_weight_l1[current_mv.ref_idx[1]],
1378 s->sh.luma_offset_l1[current_mv.ref_idx[1]],
1379 dst0, s->frame->linesize[0], tmp, tmpstride,
1382 s->hevcdsp.put_unweighted_pred(dst0, s->frame->linesize[0], tmp, tmpstride, nPbW, nPbH);
1385 chroma_mc(s, tmp, tmp2, tmpstride, ref1->frame,
1386 ¤t_mv.mv[1], x0/2, y0/2, nPbW/2, nPbH/2);
1388 if ((s->sh.slice_type == P_SLICE && s->pps->weighted_pred_flag) ||
1389 (s->sh.slice_type == B_SLICE && s->pps->weighted_bipred_flag)) {
1390 s->hevcdsp.weighted_pred(s->sh.chroma_log2_weight_denom,
1391 s->sh.chroma_weight_l1[current_mv.ref_idx[1]][0],
1392 s->sh.chroma_offset_l1[current_mv.ref_idx[1]][0],
1393 dst1, s->frame->linesize[1], tmp, tmpstride, nPbW/2, nPbH/2);
1394 s->hevcdsp.weighted_pred(s->sh.chroma_log2_weight_denom,
1395 s->sh.chroma_weight_l1[current_mv.ref_idx[1]][1],
1396 s->sh.chroma_offset_l1[current_mv.ref_idx[1]][1],
1397 dst2, s->frame->linesize[2], tmp2, tmpstride, nPbW/2, nPbH/2);
1399 s->hevcdsp.put_unweighted_pred(dst1, s->frame->linesize[1], tmp, tmpstride, nPbW/2, nPbH/2);
1400 s->hevcdsp.put_unweighted_pred(dst2, s->frame->linesize[2], tmp2, tmpstride, nPbW/2, nPbH/2);
1402 } else if (current_mv.pred_flag[0] && current_mv.pred_flag[1]) {
1403 DECLARE_ALIGNED(16, int16_t, tmp [MAX_PB_SIZE * MAX_PB_SIZE]);
1404 DECLARE_ALIGNED(16, int16_t, tmp2[MAX_PB_SIZE * MAX_PB_SIZE]);
1405 DECLARE_ALIGNED(16, int16_t, tmp3[MAX_PB_SIZE * MAX_PB_SIZE]);
1406 DECLARE_ALIGNED(16, int16_t, tmp4[MAX_PB_SIZE * MAX_PB_SIZE]);
1407 HEVCFrame *ref0 = refPicList[0].ref[current_mv.ref_idx[0]];
1408 HEVCFrame *ref1 = refPicList[1].ref[current_mv.ref_idx[1]];
1413 luma_mc(s, tmp, tmpstride, ref0->frame,
1414 ¤t_mv.mv[0], x0, y0, nPbW, nPbH);
1415 luma_mc(s, tmp2, tmpstride, ref1->frame,
1416 ¤t_mv.mv[1], x0, y0, nPbW, nPbH);
1418 if ((s->sh.slice_type == P_SLICE && s->pps->weighted_pred_flag) ||
1419 (s->sh.slice_type == B_SLICE && s->pps->weighted_bipred_flag)) {
1420 s->hevcdsp.weighted_pred_avg(s->sh.luma_log2_weight_denom,
1421 s->sh.luma_weight_l0[current_mv.ref_idx[0]],
1422 s->sh.luma_weight_l1[current_mv.ref_idx[1]],
1423 s->sh.luma_offset_l0[current_mv.ref_idx[0]],
1424 s->sh.luma_offset_l1[current_mv.ref_idx[1]],
1425 dst0, s->frame->linesize[0],
1426 tmp, tmp2, tmpstride, nPbW, nPbH);
1428 s->hevcdsp.put_weighted_pred_avg(dst0, s->frame->linesize[0],
1429 tmp, tmp2, tmpstride, nPbW, nPbH);
1432 chroma_mc(s, tmp, tmp2, tmpstride, ref0->frame,
1433 ¤t_mv.mv[0], x0 / 2, y0 / 2, nPbW / 2, nPbH / 2);
1434 chroma_mc(s, tmp3, tmp4, tmpstride, ref1->frame,
1435 ¤t_mv.mv[1], x0 / 2, y0 / 2, nPbW / 2, nPbH / 2);
1437 if ((s->sh.slice_type == P_SLICE && s->pps->weighted_pred_flag) ||
1438 (s->sh.slice_type == B_SLICE && s->pps->weighted_bipred_flag)) {
1439 s->hevcdsp.weighted_pred_avg(s->sh.chroma_log2_weight_denom,
1440 s->sh.chroma_weight_l0[current_mv.ref_idx[0]][0],
1441 s->sh.chroma_weight_l1[current_mv.ref_idx[1]][0],
1442 s->sh.chroma_offset_l0[current_mv.ref_idx[0]][0],
1443 s->sh.chroma_offset_l1[current_mv.ref_idx[1]][0],
1444 dst1, s->frame->linesize[1], tmp, tmp3,
1445 tmpstride, nPbW / 2, nPbH / 2);
1446 s->hevcdsp.weighted_pred_avg(s->sh.chroma_log2_weight_denom,
1447 s->sh.chroma_weight_l0[current_mv.ref_idx[0]][1],
1448 s->sh.chroma_weight_l1[current_mv.ref_idx[1]][1],
1449 s->sh.chroma_offset_l0[current_mv.ref_idx[0]][1],
1450 s->sh.chroma_offset_l1[current_mv.ref_idx[1]][1],
1451 dst2, s->frame->linesize[2], tmp2, tmp4,
1452 tmpstride, nPbW / 2, nPbH / 2);
1454 s->hevcdsp.put_weighted_pred_avg(dst1, s->frame->linesize[1], tmp, tmp3, tmpstride, nPbW/2, nPbH/2);
1455 s->hevcdsp.put_weighted_pred_avg(dst2, s->frame->linesize[2], tmp2, tmp4, tmpstride, nPbW/2, nPbH/2);
1463 static int luma_intra_pred_mode(HEVCContext *s, int x0, int y0, int pu_size,
1464 int prev_intra_luma_pred_flag)
1466 HEVCLocalContext *lc = s->HEVClc;
1467 int x_pu = x0 >> s->sps->log2_min_pu_size;
1468 int y_pu = y0 >> s->sps->log2_min_pu_size;
1469 int min_pu_width = s->sps->min_pu_width;
1470 int size_in_pus = pu_size >> s->sps->log2_min_pu_size;
1471 int x0b = x0 & ((1 << s->sps->log2_ctb_size) - 1);
1472 int y0b = y0 & ((1 << s->sps->log2_ctb_size) - 1);
1474 int cand_up = (lc->ctb_up_flag || y0b) ?
1475 s->tab_ipm[(y_pu - 1) * min_pu_width + x_pu] : INTRA_DC;
1476 int cand_left = (lc->ctb_left_flag || x0b) ?
1477 s->tab_ipm[y_pu * min_pu_width + x_pu - 1] : INTRA_DC;
1479 int y_ctb = (y0 >> (s->sps->log2_ctb_size)) << (s->sps->log2_ctb_size);
1481 MvField *tab_mvf = s->ref->tab_mvf;
1482 int intra_pred_mode;
1486 // intra_pred_mode prediction does not cross vertical CTB boundaries
1487 if ((y0 - 1) < y_ctb)
1490 if (cand_left == cand_up) {
1491 if (cand_left < 2) {
1492 candidate[0] = INTRA_PLANAR;
1493 candidate[1] = INTRA_DC;
1494 candidate[2] = INTRA_ANGULAR_26;
1496 candidate[0] = cand_left;
1497 candidate[1] = 2 + ((cand_left - 2 - 1 + 32) & 31);
1498 candidate[2] = 2 + ((cand_left - 2 + 1) & 31);
1501 candidate[0] = cand_left;
1502 candidate[1] = cand_up;
1503 if (candidate[0] != INTRA_PLANAR && candidate[1] != INTRA_PLANAR) {
1504 candidate[2] = INTRA_PLANAR;
1505 } else if (candidate[0] != INTRA_DC && candidate[1] != INTRA_DC) {
1506 candidate[2] = INTRA_DC;
1508 candidate[2] = INTRA_ANGULAR_26;
1512 if (prev_intra_luma_pred_flag) {
1513 intra_pred_mode = candidate[lc->pu.mpm_idx];
1515 if (candidate[0] > candidate[1])
1516 FFSWAP(uint8_t, candidate[0], candidate[1]);
1517 if (candidate[0] > candidate[2])
1518 FFSWAP(uint8_t, candidate[0], candidate[2]);
1519 if (candidate[1] > candidate[2])
1520 FFSWAP(uint8_t, candidate[1], candidate[2]);
1522 intra_pred_mode = lc->pu.rem_intra_luma_pred_mode;
1523 for (i = 0; i < 3; i++)
1524 if (intra_pred_mode >= candidate[i])
1528 /* write the intra prediction units into the mv array */
1531 for (i = 0; i < size_in_pus; i++) {
1532 memset(&s->tab_ipm[(y_pu + i) * min_pu_width + x_pu],
1533 intra_pred_mode, size_in_pus);
1535 for (j = 0; j < size_in_pus; j++) {
1536 tab_mvf[(y_pu + j) * min_pu_width + x_pu + i].is_intra = 1;
1537 tab_mvf[(y_pu + j) * min_pu_width + x_pu + i].pred_flag[0] = 0;
1538 tab_mvf[(y_pu + j) * min_pu_width + x_pu + i].pred_flag[1] = 0;
1539 tab_mvf[(y_pu + j) * min_pu_width + x_pu + i].ref_idx[0] = 0;
1540 tab_mvf[(y_pu + j) * min_pu_width + x_pu + i].ref_idx[1] = 0;
1541 tab_mvf[(y_pu + j) * min_pu_width + x_pu + i].mv[0].x = 0;
1542 tab_mvf[(y_pu + j) * min_pu_width + x_pu + i].mv[0].y = 0;
1543 tab_mvf[(y_pu + j) * min_pu_width + x_pu + i].mv[1].x = 0;
1544 tab_mvf[(y_pu + j) * min_pu_width + x_pu + i].mv[1].y = 0;
1548 return intra_pred_mode;
1551 static av_always_inline void set_ct_depth(HEVCContext *s, int x0, int y0,
1552 int log2_cb_size, int ct_depth)
1554 int length = (1 << log2_cb_size) >> s->sps->log2_min_cb_size;
1555 int x_cb = x0 >> s->sps->log2_min_cb_size;
1556 int y_cb = y0 >> s->sps->log2_min_cb_size;
1559 for (y = 0; y < length; y++)
1560 memset(&s->tab_ct_depth[(y_cb + y) * s->sps->min_cb_width + x_cb],
1564 static void intra_prediction_unit(HEVCContext *s, int x0, int y0,
1567 HEVCLocalContext *lc = s->HEVClc;
1568 static const uint8_t intra_chroma_table[4] = { 0, 26, 10, 1 };
1569 uint8_t prev_intra_luma_pred_flag[4];
1570 int split = lc->cu.part_mode == PART_NxN;
1571 int pb_size = (1 << log2_cb_size) >> split;
1572 int side = split + 1;
1576 for (i = 0; i < side; i++)
1577 for (j = 0; j < side; j++)
1578 prev_intra_luma_pred_flag[2 * i + j] = ff_hevc_prev_intra_luma_pred_flag_decode(s);
1580 for (i = 0; i < side; i++) {
1581 for (j = 0; j < side; j++) {
1582 if (prev_intra_luma_pred_flag[2 * i + j])
1583 lc->pu.mpm_idx = ff_hevc_mpm_idx_decode(s);
1585 lc->pu.rem_intra_luma_pred_mode = ff_hevc_rem_intra_luma_pred_mode_decode(s);
1587 lc->pu.intra_pred_mode[2 * i + j] =
1588 luma_intra_pred_mode(s, x0 + pb_size * j, y0 + pb_size * i, pb_size,
1589 prev_intra_luma_pred_flag[2 * i + j]);
1593 chroma_mode = ff_hevc_intra_chroma_pred_mode_decode(s);
1594 if (chroma_mode != 4) {
1595 if (lc->pu.intra_pred_mode[0] == intra_chroma_table[chroma_mode])
1596 lc->pu.intra_pred_mode_c = 34;
1598 lc->pu.intra_pred_mode_c = intra_chroma_table[chroma_mode];
1600 lc->pu.intra_pred_mode_c = lc->pu.intra_pred_mode[0];
1604 static void intra_prediction_unit_default_value(HEVCContext *s,
1608 HEVCLocalContext *lc = s->HEVClc;
1609 int pb_size = 1 << log2_cb_size;
1610 int size_in_pus = pb_size >> s->sps->log2_min_pu_size;
1611 int min_pu_width = s->sps->min_pu_width;
1612 MvField *tab_mvf = s->ref->tab_mvf;
1613 int x_pu = x0 >> s->sps->log2_min_pu_size;
1614 int y_pu = y0 >> s->sps->log2_min_pu_size;
1617 if (size_in_pus == 0)
1619 for (j = 0; j < size_in_pus; j++) {
1620 memset(&s->tab_ipm[(y_pu + j) * min_pu_width + x_pu], INTRA_DC, size_in_pus);
1621 for (k = 0; k < size_in_pus; k++)
1622 tab_mvf[(y_pu + j) * min_pu_width + x_pu + k].is_intra = lc->cu.pred_mode == MODE_INTRA;
1626 static int hls_coding_unit(HEVCContext *s, int x0, int y0, int log2_cb_size)
1628 int cb_size = 1 << log2_cb_size;
1629 HEVCLocalContext *lc = s->HEVClc;
1630 int log2_min_cb_size = s->sps->log2_min_cb_size;
1631 int length = cb_size >> log2_min_cb_size;
1632 int min_cb_width = s->sps->min_cb_width;
1633 int x_cb = x0 >> log2_min_cb_size;
1634 int y_cb = y0 >> log2_min_cb_size;
1636 int qp_block_mask = (1<<(s->sps->log2_ctb_size - s->pps->diff_cu_qp_delta_depth)) - 1;
1640 lc->cu.rqt_root_cbf = 1;
1641 lc->cu.pred_mode = MODE_INTRA;
1642 lc->cu.part_mode = PART_2Nx2N;
1643 lc->cu.intra_split_flag = 0;
1644 lc->cu.pcm_flag = 0;
1646 SAMPLE_CTB(s->skip_flag, x_cb, y_cb) = 0;
1647 for (x = 0; x < 4; x++)
1648 lc->pu.intra_pred_mode[x] = 1;
1649 if (s->pps->transquant_bypass_enable_flag) {
1650 lc->cu.cu_transquant_bypass_flag = ff_hevc_cu_transquant_bypass_flag_decode(s);
1651 if (lc->cu.cu_transquant_bypass_flag)
1652 set_deblocking_bypass(s, x0, y0, log2_cb_size);
1654 lc->cu.cu_transquant_bypass_flag = 0;
1656 if (s->sh.slice_type != I_SLICE) {
1657 uint8_t skip_flag = ff_hevc_skip_flag_decode(s, x0, y0, x_cb, y_cb);
1659 lc->cu.pred_mode = MODE_SKIP;
1660 x = y_cb * min_cb_width + x_cb;
1661 for (y = 0; y < length; y++) {
1662 memset(&s->skip_flag[x], skip_flag, length);
1665 lc->cu.pred_mode = skip_flag ? MODE_SKIP : MODE_INTER;
1668 if (SAMPLE_CTB(s->skip_flag, x_cb, y_cb)) {
1669 hls_prediction_unit(s, x0, y0, cb_size, cb_size, log2_cb_size, 0);
1670 intra_prediction_unit_default_value(s, x0, y0, log2_cb_size);
1672 if (!s->sh.disable_deblocking_filter_flag)
1673 ff_hevc_deblocking_boundary_strengths(s, x0, y0, log2_cb_size,
1674 lc->slice_or_tiles_up_boundary,
1675 lc->slice_or_tiles_left_boundary);
1677 if (s->sh.slice_type != I_SLICE)
1678 lc->cu.pred_mode = ff_hevc_pred_mode_decode(s);
1679 if (lc->cu.pred_mode != MODE_INTRA ||
1680 log2_cb_size == s->sps->log2_min_cb_size) {
1681 lc->cu.part_mode = ff_hevc_part_mode_decode(s, log2_cb_size);
1682 lc->cu.intra_split_flag = lc->cu.part_mode == PART_NxN &&
1683 lc->cu.pred_mode == MODE_INTRA;
1686 if (lc->cu.pred_mode == MODE_INTRA) {
1687 if (lc->cu.part_mode == PART_2Nx2N && s->sps->pcm_enabled_flag &&
1688 log2_cb_size >= s->sps->pcm.log2_min_pcm_cb_size &&
1689 log2_cb_size <= s->sps->pcm.log2_max_pcm_cb_size) {
1690 lc->cu.pcm_flag = ff_hevc_pcm_flag_decode(s);
1692 if (lc->cu.pcm_flag) {
1693 intra_prediction_unit_default_value(s, x0, y0, log2_cb_size);
1694 ret = hls_pcm_sample(s, x0, y0, log2_cb_size);
1695 if (s->sps->pcm.loop_filter_disable_flag)
1696 set_deblocking_bypass(s, x0, y0, log2_cb_size);
1701 intra_prediction_unit(s, x0, y0, log2_cb_size);
1704 intra_prediction_unit_default_value(s, x0, y0, log2_cb_size);
1705 switch (lc->cu.part_mode) {
1707 hls_prediction_unit(s, x0, y0, cb_size, cb_size, log2_cb_size, 0);
1710 hls_prediction_unit(s, x0, y0, cb_size, cb_size / 2, log2_cb_size, 0);
1711 hls_prediction_unit(s, x0, y0 + cb_size / 2, cb_size, cb_size / 2, log2_cb_size, 1);
1714 hls_prediction_unit(s, x0, y0, cb_size / 2, cb_size, log2_cb_size, 0);
1715 hls_prediction_unit(s, x0 + cb_size / 2, y0, cb_size / 2, cb_size, log2_cb_size, 1);
1718 hls_prediction_unit(s, x0, y0, cb_size, cb_size / 4, log2_cb_size, 0);
1719 hls_prediction_unit(s, x0, y0 + cb_size / 4, cb_size, cb_size * 3 / 4, log2_cb_size, 1);
1722 hls_prediction_unit(s, x0, y0, cb_size, cb_size * 3 / 4, log2_cb_size, 0);
1723 hls_prediction_unit(s, x0, y0 + cb_size * 3 / 4, cb_size, cb_size / 4, log2_cb_size, 1);
1726 hls_prediction_unit(s, x0, y0, cb_size / 4, cb_size, log2_cb_size, 0);
1727 hls_prediction_unit(s, x0 + cb_size / 4, y0, cb_size * 3 / 4, cb_size, log2_cb_size, 1);
1730 hls_prediction_unit(s, x0, y0, cb_size * 3 / 4, cb_size, log2_cb_size, 0);
1731 hls_prediction_unit(s, x0 + cb_size * 3 / 4, y0, cb_size / 4, cb_size, log2_cb_size, 1);
1734 hls_prediction_unit(s, x0, y0, cb_size / 2, cb_size / 2, log2_cb_size, 0);
1735 hls_prediction_unit(s, x0 + cb_size / 2, y0, cb_size / 2, cb_size / 2, log2_cb_size, 1);
1736 hls_prediction_unit(s, x0, y0 + cb_size / 2, cb_size / 2, cb_size / 2, log2_cb_size, 2);
1737 hls_prediction_unit(s, x0 + cb_size / 2, y0 + cb_size / 2, cb_size / 2, cb_size / 2, log2_cb_size, 3);
1742 if (!lc->cu.pcm_flag) {
1743 if (lc->cu.pred_mode != MODE_INTRA &&
1744 !(lc->cu.part_mode == PART_2Nx2N && lc->pu.merge_flag)) {
1745 lc->cu.rqt_root_cbf = ff_hevc_no_residual_syntax_flag_decode(s);
1747 if (lc->cu.rqt_root_cbf) {
1748 lc->cu.max_trafo_depth = lc->cu.pred_mode == MODE_INTRA ?
1749 s->sps->max_transform_hierarchy_depth_intra + lc->cu.intra_split_flag :
1750 s->sps->max_transform_hierarchy_depth_inter;
1751 ret = hls_transform_tree(s, x0, y0, x0, y0, x0, y0,
1753 log2_cb_size, 0, 0);
1757 if (!s->sh.disable_deblocking_filter_flag)
1758 ff_hevc_deblocking_boundary_strengths(s, x0, y0, log2_cb_size,
1759 lc->slice_or_tiles_up_boundary,
1760 lc->slice_or_tiles_left_boundary);
1765 if (s->pps->cu_qp_delta_enabled_flag && lc->tu.is_cu_qp_delta_coded == 0)
1766 ff_hevc_set_qPy(s, x0, y0, x0, y0, log2_cb_size);
1768 x = y_cb * min_cb_width + x_cb;
1769 for (y = 0; y < length; y++) {
1770 memset(&s->qp_y_tab[x], lc->qp_y, length);
1774 if(((x0 + (1<<log2_cb_size)) & qp_block_mask) == 0 &&
1775 ((y0 + (1<<log2_cb_size)) & qp_block_mask) == 0) {
1776 lc->qPy_pred = lc->qp_y;
1779 set_ct_depth(s, x0, y0, log2_cb_size, lc->ct.depth);
1784 static int hls_coding_quadtree(HEVCContext *s, int x0, int y0,
1785 int log2_cb_size, int cb_depth)
1787 HEVCLocalContext *lc = s->HEVClc;
1788 const int cb_size = 1 << log2_cb_size;
1790 int qp_block_mask = (1<<(s->sps->log2_ctb_size - s->pps->diff_cu_qp_delta_depth)) - 1;
1792 lc->ct.depth = cb_depth;
1793 if (x0 + cb_size <= s->sps->width &&
1794 y0 + cb_size <= s->sps->height &&
1795 log2_cb_size > s->sps->log2_min_cb_size) {
1796 SAMPLE(s->split_cu_flag, x0, y0) =
1797 ff_hevc_split_coding_unit_flag_decode(s, cb_depth, x0, y0);
1799 SAMPLE(s->split_cu_flag, x0, y0) =
1800 (log2_cb_size > s->sps->log2_min_cb_size);
1802 if (s->pps->cu_qp_delta_enabled_flag &&
1803 log2_cb_size >= s->sps->log2_ctb_size - s->pps->diff_cu_qp_delta_depth) {
1804 lc->tu.is_cu_qp_delta_coded = 0;
1805 lc->tu.cu_qp_delta = 0;
1808 if (SAMPLE(s->split_cu_flag, x0, y0)) {
1809 const int cb_size_split = cb_size >> 1;
1810 const int x1 = x0 + cb_size_split;
1811 const int y1 = y0 + cb_size_split;
1815 more_data = hls_coding_quadtree(s, x0, y0, log2_cb_size - 1, cb_depth + 1);
1819 if (more_data && x1 < s->sps->width) {
1820 more_data = hls_coding_quadtree(s, x1, y0, log2_cb_size - 1, cb_depth + 1);
1824 if (more_data && y1 < s->sps->height) {
1825 more_data = hls_coding_quadtree(s, x0, y1, log2_cb_size - 1, cb_depth + 1);
1829 if (more_data && x1 < s->sps->width &&
1830 y1 < s->sps->height) {
1831 more_data = hls_coding_quadtree(s, x1, y1, log2_cb_size - 1, cb_depth + 1);
1836 if(((x0 + (1<<log2_cb_size)) & qp_block_mask) == 0 &&
1837 ((y0 + (1<<log2_cb_size)) & qp_block_mask) == 0)
1838 lc->qPy_pred = lc->qp_y;
1841 return ((x1 + cb_size_split) < s->sps->width ||
1842 (y1 + cb_size_split) < s->sps->height);
1846 ret = hls_coding_unit(s, x0, y0, log2_cb_size);
1849 if ((!((x0 + cb_size) %
1850 (1 << (s->sps->log2_ctb_size))) ||
1851 (x0 + cb_size >= s->sps->width)) &&
1853 (1 << (s->sps->log2_ctb_size))) ||
1854 (y0 + cb_size >= s->sps->height))) {
1855 int end_of_slice_flag = ff_hevc_end_of_slice_flag_decode(s);
1856 return !end_of_slice_flag;
1865 static void hls_decode_neighbour(HEVCContext *s, int x_ctb, int y_ctb,
1868 HEVCLocalContext *lc = s->HEVClc;
1869 int ctb_size = 1 << s->sps->log2_ctb_size;
1870 int ctb_addr_rs = s->pps->ctb_addr_ts_to_rs[ctb_addr_ts];
1871 int ctb_addr_in_slice = ctb_addr_rs - s->sh.slice_addr;
1873 int tile_left_boundary, tile_up_boundary;
1874 int slice_left_boundary, slice_up_boundary;
1876 s->tab_slice_address[ctb_addr_rs] = s->sh.slice_addr;
1878 if (s->pps->entropy_coding_sync_enabled_flag) {
1879 if (x_ctb == 0 && (y_ctb & (ctb_size - 1)) == 0)
1880 lc->first_qp_group = 1;
1881 lc->end_of_tiles_x = s->sps->width;
1882 } else if (s->pps->tiles_enabled_flag) {
1883 if (ctb_addr_ts && s->pps->tile_id[ctb_addr_ts] != s->pps->tile_id[ctb_addr_ts - 1]) {
1884 int idxX = s->pps->col_idxX[x_ctb >> s->sps->log2_ctb_size];
1885 lc->end_of_tiles_x = x_ctb + (s->pps->column_width[idxX] << s->sps->log2_ctb_size);
1886 lc->first_qp_group = 1;
1889 lc->end_of_tiles_x = s->sps->width;
1892 lc->end_of_tiles_y = FFMIN(y_ctb + ctb_size, s->sps->height);
1894 if (s->pps->tiles_enabled_flag) {
1895 tile_left_boundary = x_ctb > 0 &&
1896 s->pps->tile_id[ctb_addr_ts] != s->pps->tile_id[s->pps->ctb_addr_rs_to_ts[ctb_addr_rs-1]];
1897 slice_left_boundary = x_ctb > 0 &&
1898 s->tab_slice_address[ctb_addr_rs] != s->tab_slice_address[ctb_addr_rs - 1];
1899 tile_up_boundary = y_ctb > 0 &&
1900 s->pps->tile_id[ctb_addr_ts] != s->pps->tile_id[s->pps->ctb_addr_rs_to_ts[ctb_addr_rs - s->sps->ctb_width]];
1901 slice_up_boundary = y_ctb > 0 &&
1902 s->tab_slice_address[ctb_addr_rs] != s->tab_slice_address[ctb_addr_rs - s->sps->ctb_width];
1904 tile_left_boundary =
1905 tile_up_boundary = 0;
1906 slice_left_boundary = ctb_addr_in_slice <= 0;
1907 slice_up_boundary = ctb_addr_in_slice < s->sps->ctb_width;
1909 lc->slice_or_tiles_left_boundary = slice_left_boundary + (tile_left_boundary << 1);
1910 lc->slice_or_tiles_up_boundary = slice_up_boundary + (tile_up_boundary << 1);
1911 lc->ctb_left_flag = ((x_ctb > 0) && (ctb_addr_in_slice > 0) && !tile_left_boundary);
1912 lc->ctb_up_flag = ((y_ctb > 0) && (ctb_addr_in_slice >= s->sps->ctb_width) && !tile_up_boundary);
1913 lc->ctb_up_right_flag = ((y_ctb > 0) && (ctb_addr_in_slice+1 >= s->sps->ctb_width) && (s->pps->tile_id[ctb_addr_ts] == s->pps->tile_id[s->pps->ctb_addr_rs_to_ts[ctb_addr_rs+1 - s->sps->ctb_width]]));
1914 lc->ctb_up_left_flag = ((x_ctb > 0) && (y_ctb > 0) && (ctb_addr_in_slice-1 >= s->sps->ctb_width) && (s->pps->tile_id[ctb_addr_ts] == s->pps->tile_id[s->pps->ctb_addr_rs_to_ts[ctb_addr_rs-1 - s->sps->ctb_width]]));
1917 static int hls_decode_entry(AVCodecContext *avctxt, void *isFilterThread)
1919 HEVCContext *s = avctxt->priv_data;
1920 int ctb_size = 1 << s->sps->log2_ctb_size;
1924 int ctb_addr_ts = s->pps->ctb_addr_rs_to_ts[s->sh.slice_ctb_addr_rs];
1926 if (!ctb_addr_ts && s->sh.dependent_slice_segment_flag) {
1927 av_log(s->avctx, AV_LOG_ERROR, "Impossible initial tile.\n");
1928 return AVERROR_INVALIDDATA;
1931 if (s->sh.dependent_slice_segment_flag) {
1932 int prev_rs = s->pps->ctb_addr_ts_to_rs[ctb_addr_ts - 1];
1933 if (s->tab_slice_address[prev_rs] != s->sh.slice_addr) {
1934 av_log(s->avctx, AV_LOG_ERROR, "Previous slice segment missing\n");
1935 return AVERROR_INVALIDDATA;
1939 while (more_data && ctb_addr_ts < s->sps->ctb_size) {
1940 int ctb_addr_rs = s->pps->ctb_addr_ts_to_rs[ctb_addr_ts];
1942 x_ctb = (ctb_addr_rs % ((s->sps->width + ctb_size - 1) >> s->sps->log2_ctb_size)) << s->sps->log2_ctb_size;
1943 y_ctb = (ctb_addr_rs / ((s->sps->width + ctb_size - 1) >> s->sps->log2_ctb_size)) << s->sps->log2_ctb_size;
1944 hls_decode_neighbour(s, x_ctb, y_ctb, ctb_addr_ts);
1946 ff_hevc_cabac_init(s, ctb_addr_ts);
1948 hls_sao_param(s, x_ctb >> s->sps->log2_ctb_size, y_ctb >> s->sps->log2_ctb_size);
1950 s->deblock[ctb_addr_rs].beta_offset = s->sh.beta_offset;
1951 s->deblock[ctb_addr_rs].tc_offset = s->sh.tc_offset;
1952 s->filter_slice_edges[ctb_addr_rs] = s->sh.slice_loop_filter_across_slices_enabled_flag;
1954 more_data = hls_coding_quadtree(s, x_ctb, y_ctb, s->sps->log2_ctb_size, 0);
1955 if (more_data < 0) {
1956 s->tab_slice_address[ctb_addr_rs] = -1;
1962 ff_hevc_save_states(s, ctb_addr_ts);
1963 ff_hevc_hls_filters(s, x_ctb, y_ctb, ctb_size);
1966 if (x_ctb + ctb_size >= s->sps->width &&
1967 y_ctb + ctb_size >= s->sps->height)
1968 ff_hevc_hls_filter(s, x_ctb, y_ctb);
1973 static int hls_slice_data(HEVCContext *s)
1981 s->avctx->execute(s->avctx, hls_decode_entry, arg, ret , 1, sizeof(int));
1984 static int hls_decode_entry_wpp(AVCodecContext *avctxt, void *input_ctb_row, int job, int self_id)
1986 HEVCContext *s1 = avctxt->priv_data, *s;
1987 HEVCLocalContext *lc;
1988 int ctb_size = 1<< s1->sps->log2_ctb_size;
1990 int *ctb_row_p = input_ctb_row;
1991 int ctb_row = ctb_row_p[job];
1992 int ctb_addr_rs = s1->sh.slice_ctb_addr_rs + ctb_row * ((s1->sps->width + ctb_size - 1) >> s1->sps->log2_ctb_size);
1993 int ctb_addr_ts = s1->pps->ctb_addr_rs_to_ts[ctb_addr_rs];
1994 int thread = ctb_row % s1->threads_number;
1997 s = s1->sList[self_id];
2001 ret = init_get_bits8(&lc->gb, s->data + s->sh.offset[ctb_row - 1], s->sh.size[ctb_row - 1]);
2005 ff_init_cabac_decoder(&lc->cc, s->data + s->sh.offset[(ctb_row)-1], s->sh.size[ctb_row - 1]);
2008 while(more_data && ctb_addr_ts < s->sps->ctb_size) {
2009 int x_ctb = (ctb_addr_rs % s->sps->ctb_width) << s->sps->log2_ctb_size;
2010 int y_ctb = (ctb_addr_rs / s->sps->ctb_width) << s->sps->log2_ctb_size;
2012 hls_decode_neighbour(s, x_ctb, y_ctb, ctb_addr_ts);
2014 ff_thread_await_progress2(s->avctx, ctb_row, thread, SHIFT_CTB_WPP);
2016 if (avpriv_atomic_int_get(&s1->wpp_err)){
2017 ff_thread_report_progress2(s->avctx, ctb_row , thread, SHIFT_CTB_WPP);
2021 ff_hevc_cabac_init(s, ctb_addr_ts);
2022 hls_sao_param(s, x_ctb >> s->sps->log2_ctb_size, y_ctb >> s->sps->log2_ctb_size);
2023 more_data = hls_coding_quadtree(s, x_ctb, y_ctb, s->sps->log2_ctb_size, 0);
2025 if (more_data < 0) {
2026 s->tab_slice_address[ctb_addr_rs] = -1;
2032 ff_hevc_save_states(s, ctb_addr_ts);
2033 ff_thread_report_progress2(s->avctx, ctb_row, thread, 1);
2034 ff_hevc_hls_filters(s, x_ctb, y_ctb, ctb_size);
2036 if (!more_data && (x_ctb+ctb_size) < s->sps->width && ctb_row != s->sh.num_entry_point_offsets) {
2037 avpriv_atomic_int_set(&s1->wpp_err, 1);
2038 ff_thread_report_progress2(s->avctx, ctb_row ,thread, SHIFT_CTB_WPP);
2042 if ((x_ctb+ctb_size) >= s->sps->width && (y_ctb+ctb_size) >= s->sps->height ) {
2043 ff_hevc_hls_filter(s, x_ctb, y_ctb);
2044 ff_thread_report_progress2(s->avctx, ctb_row , thread, SHIFT_CTB_WPP);
2047 ctb_addr_rs = s->pps->ctb_addr_ts_to_rs[ctb_addr_ts];
2050 if(x_ctb >= s->sps->width) {
2054 ff_thread_report_progress2(s->avctx, ctb_row ,thread, SHIFT_CTB_WPP);
2059 static int hls_slice_data_wpp(HEVCContext *s, const uint8_t *nal, int length)
2061 HEVCLocalContext *lc = s->HEVClc;
2062 int *ret = av_malloc((s->sh.num_entry_point_offsets + 1) * sizeof(int));
2063 int *arg = av_malloc((s->sh.num_entry_point_offsets + 1) * sizeof(int));
2065 int startheader, cmpt = 0;
2070 ff_alloc_entries(s->avctx, s->sh.num_entry_point_offsets + 1);
2073 for (i = 1; i < s->threads_number; i++) {
2074 s->sList[i] = av_malloc(sizeof(HEVCContext));
2075 memcpy(s->sList[i], s, sizeof(HEVCContext));
2076 s->HEVClcList[i] = av_malloc(sizeof(HEVCLocalContext));
2077 s->sList[i]->HEVClc = s->HEVClcList[i];
2081 offset = (lc->gb.index >> 3);
2083 for (j = 0, cmpt = 0, startheader = offset + s->sh.entry_point_offset[0]; j < s->skipped_bytes; j++) {
2084 if (s->skipped_bytes_pos[j] >= offset && s->skipped_bytes_pos[j] < startheader) {
2090 for (i = 1; i < s->sh.num_entry_point_offsets; i++) {
2091 offset += (s->sh.entry_point_offset[i - 1] - cmpt);
2092 for (j = 0, cmpt = 0, startheader = offset
2093 + s->sh.entry_point_offset[i]; j < s->skipped_bytes; j++) {
2094 if (s->skipped_bytes_pos[j] >= offset && s->skipped_bytes_pos[j] < startheader) {
2099 s->sh.size[i - 1] = s->sh.entry_point_offset[i] - cmpt;
2100 s->sh.offset[i - 1] = offset;
2103 if (s->sh.num_entry_point_offsets != 0) {
2104 offset += s->sh.entry_point_offset[s->sh.num_entry_point_offsets - 1] - cmpt;
2105 s->sh.size[s->sh.num_entry_point_offsets - 1] = length - offset;
2106 s->sh.offset[s->sh.num_entry_point_offsets - 1] = offset;
2111 for (i = 1; i < s->threads_number; i++) {
2112 s->sList[i]->HEVClc->first_qp_group = 1;
2113 s->sList[i]->HEVClc->qp_y = s->sList[0]->HEVClc->qp_y;
2114 memcpy(s->sList[i], s, sizeof(HEVCContext));
2115 s->sList[i]->HEVClc = s->HEVClcList[i];
2118 avpriv_atomic_int_set(&s->wpp_err, 0);
2119 ff_reset_entries(s->avctx);
2121 for (i = 0; i <= s->sh.num_entry_point_offsets; i++) {
2126 if (s->pps->entropy_coding_sync_enabled_flag)
2127 s->avctx->execute2(s->avctx, (void *) hls_decode_entry_wpp, arg, ret, s->sh.num_entry_point_offsets + 1);
2129 for (i = 0; i <= s->sh.num_entry_point_offsets; i++)
2137 * @return AVERROR_INVALIDDATA if the packet is not a valid NAL unit,
2138 * 0 if the unit should be skipped, 1 otherwise
2140 static int hls_nal_unit(HEVCContext *s)
2142 GetBitContext *gb = &s->HEVClc->gb;
2145 if (get_bits1(gb) != 0)
2146 return AVERROR_INVALIDDATA;
2148 s->nal_unit_type = get_bits(gb, 6);
2150 nuh_layer_id = get_bits(gb, 6);
2151 s->temporal_id = get_bits(gb, 3) - 1;
2152 if (s->temporal_id < 0)
2153 return AVERROR_INVALIDDATA;
2155 av_log(s->avctx, AV_LOG_DEBUG,
2156 "nal_unit_type: %d, nuh_layer_id: %dtemporal_id: %d\n",
2157 s->nal_unit_type, nuh_layer_id, s->temporal_id);
2159 return nuh_layer_id == 0;
2162 static void restore_tqb_pixels(HEVCContext *s)
2164 int min_pu_size = 1 << s->sps->log2_min_pu_size;
2167 for (c_idx = 0; c_idx < 3; c_idx++) {
2168 ptrdiff_t stride = s->frame->linesize[c_idx];
2169 int hshift = s->sps->hshift[c_idx];
2170 int vshift = s->sps->vshift[c_idx];
2171 for (y = 0; y < s->sps->min_pu_height; y++) {
2172 for (x = 0; x < s->sps->min_pu_width; x++) {
2173 if (s->is_pcm[y * s->sps->min_pu_width + x]) {
2175 int len = min_pu_size >> hshift;
2176 uint8_t *src = &s->frame->data[c_idx][((y << s->sps->log2_min_pu_size) >> vshift) * stride + (((x << s->sps->log2_min_pu_size) >> hshift) << s->sps->pixel_shift)];
2177 uint8_t *dst = &s->sao_frame->data[c_idx][((y << s->sps->log2_min_pu_size) >> vshift) * stride + (((x << s->sps->log2_min_pu_size) >> hshift) << s->sps->pixel_shift)];
2178 for (n = 0; n < (min_pu_size >> vshift); n++) {
2179 memcpy(dst, src, len);
2189 static int set_side_data(HEVCContext *s)
2191 AVFrame *out = s->ref->frame;
2193 if (s->sei_frame_packing_present &&
2194 s->frame_packing_arrangement_type >= 3 &&
2195 s->frame_packing_arrangement_type <= 5 &&
2196 s->content_interpretation_type > 0 &&
2197 s->content_interpretation_type < 3) {
2198 AVStereo3D *stereo = av_stereo3d_create_side_data(out);
2200 return AVERROR(ENOMEM);
2202 switch (s->frame_packing_arrangement_type) {
2204 if (s->quincunx_subsampling)
2205 stereo->type = AV_STEREO3D_SIDEBYSIDE_QUINCUNX;
2207 stereo->type = AV_STEREO3D_SIDEBYSIDE;
2210 stereo->type = AV_STEREO3D_TOPBOTTOM;
2213 stereo->type = AV_STEREO3D_FRAMESEQUENCE;
2217 if (s->content_interpretation_type == 2)
2218 stereo->flags = AV_STEREO3D_FLAG_INVERT;
2224 static int hevc_frame_start(HEVCContext *s)
2226 HEVCLocalContext *lc = s->HEVClc;
2227 int pic_size_in_ctb = ((s->sps->width >> s->sps->log2_min_cb_size) + 1) *
2228 ((s->sps->height >> s->sps->log2_min_cb_size) + 1);
2232 memset(s->horizontal_bs, 0, 2 * s->bs_width * (s->bs_height + 1));
2233 memset(s->vertical_bs, 0, 2 * s->bs_width * (s->bs_height + 1));
2234 memset(s->cbf_luma, 0, s->sps->min_tb_width * s->sps->min_tb_height);
2235 memset(s->is_pcm, 0, s->sps->min_pu_width * s->sps->min_pu_height);
2236 memset(s->tab_slice_address, -1, pic_size_in_ctb * sizeof(*s->tab_slice_address));
2239 s->first_nal_type = s->nal_unit_type;
2241 if (s->pps->tiles_enabled_flag)
2242 lc->end_of_tiles_x = s->pps->column_width[0] << s->sps->log2_ctb_size;
2244 ret = ff_hevc_set_new_ref(s, s->sps->sao_enabled ? &s->sao_frame : &s->frame,
2249 ret = ff_hevc_frame_rps(s);
2251 av_log(s->avctx, AV_LOG_ERROR, "Error constructing the frame RPS.\n");
2255 ret = set_side_data(s);
2259 cur_frame = s->sps->sao_enabled ? s->sao_frame : s->frame;
2260 cur_frame->pict_type = 3 - s->sh.slice_type;
2262 av_frame_unref(s->output_frame);
2263 ret = ff_hevc_output_frame(s, s->output_frame, 0);
2267 ff_thread_finish_setup(s->avctx);
2272 if (s->ref && s->threads_type == FF_THREAD_FRAME)
2273 ff_thread_report_progress(&s->ref->tf, INT_MAX, 0);
2278 static int decode_nal_unit(HEVCContext *s, const uint8_t *nal, int length)
2280 HEVCLocalContext *lc = s->HEVClc;
2281 GetBitContext *gb = &lc->gb;
2282 int ctb_addr_ts, ret;
2284 ret = init_get_bits8(gb, nal, length);
2288 ret = hls_nal_unit(s);
2290 av_log(s->avctx, AV_LOG_ERROR, "Invalid NAL unit %d, skipping.\n",
2296 switch (s->nal_unit_type) {
2298 ret = ff_hevc_decode_nal_vps(s);
2303 ret = ff_hevc_decode_nal_sps(s);
2308 ret = ff_hevc_decode_nal_pps(s);
2312 case NAL_SEI_PREFIX:
2313 case NAL_SEI_SUFFIX:
2314 ret = ff_hevc_decode_nal_sei(s);
2325 case NAL_BLA_W_RADL:
2327 case NAL_IDR_W_RADL:
2334 ret = hls_slice_header(s);
2338 if (s->max_ra == INT_MAX) {
2339 if (s->nal_unit_type == NAL_CRA_NUT || IS_BLA(s)) {
2343 s->max_ra = INT_MIN;
2347 if ((s->nal_unit_type == NAL_RASL_R || s->nal_unit_type == NAL_RASL_N) &&
2348 s->poc <= s->max_ra) {
2352 if (s->nal_unit_type == NAL_RASL_R && s->poc > s->max_ra)
2353 s->max_ra = INT_MIN;
2356 if (s->sh.first_slice_in_pic_flag) {
2357 ret = hevc_frame_start(s);
2360 } else if (!s->ref) {
2361 av_log(s->avctx, AV_LOG_ERROR, "First slice in a frame missing.\n");
2365 if (s->nal_unit_type != s->first_nal_type) {
2366 av_log(s->avctx, AV_LOG_ERROR,
2367 "Non-matching NAL types of the VCL NALUs: %d %d\n",
2368 s->first_nal_type, s->nal_unit_type);
2369 return AVERROR_INVALIDDATA;
2372 if (!s->sh.dependent_slice_segment_flag &&
2373 s->sh.slice_type != I_SLICE) {
2374 ret = ff_hevc_slice_rpl(s);
2376 av_log(s->avctx, AV_LOG_WARNING,
2377 "Error constructing the reference lists for the current slice.\n");
2382 if (s->threads_number > 1 && s->sh.num_entry_point_offsets > 0)
2383 ctb_addr_ts = hls_slice_data_wpp(s, nal, length);
2385 ctb_addr_ts = hls_slice_data(s);
2386 if (ctb_addr_ts >= (s->sps->ctb_width * s->sps->ctb_height)) {
2388 if ((s->pps->transquant_bypass_enable_flag ||
2389 (s->sps->pcm.loop_filter_disable_flag && s->sps->pcm_enabled_flag)) &&
2390 s->sps->sao_enabled)
2391 restore_tqb_pixels(s);
2394 if (ctb_addr_ts < 0) {
2401 s->seq_decode = (s->seq_decode + 1) & 0xff;
2402 s->max_ra = INT_MAX;
2408 av_log(s->avctx, AV_LOG_INFO,
2409 "Skipping NAL unit %d\n", s->nal_unit_type);
2414 if (s->avctx->err_recognition & AV_EF_EXPLODE)
2419 /* FIXME: This is adapted from ff_h264_decode_nal, avoiding duplication
2420 * between these functions would be nice. */
2421 int ff_hevc_extract_rbsp(HEVCContext *s, const uint8_t *src, int length,
2427 s->skipped_bytes = 0;
2428 #define STARTCODE_TEST \
2429 if (i + 2 < length && src[i + 1] == 0 && src[i + 2] <= 3) { \
2430 if (src[i + 2] != 3) { \
2431 /* startcode, so we must be past the end */ \
2436 #if HAVE_FAST_UNALIGNED
2437 #define FIND_FIRST_ZERO \
2438 if (i > 0 && !src[i]) \
2443 for (i = 0; i + 1 < length; i += 9) {
2444 if (!((~AV_RN64A(src + i) &
2445 (AV_RN64A(src + i) - 0x0100010001000101ULL)) &
2446 0x8000800080008080ULL))
2453 for (i = 0; i + 1 < length; i += 5) {
2454 if (!((~AV_RN32A(src + i) &
2455 (AV_RN32A(src + i) - 0x01000101U)) &
2462 #endif /* HAVE_FAST_64BIT */
2464 for (i = 0; i + 1 < length; i += 2) {
2467 if (i > 0 && src[i - 1] == 0)
2471 #endif /* HAVE_FAST_UNALIGNED */
2473 if (i >= length - 1) { // no escaped 0
2479 av_fast_malloc(&nal->rbsp_buffer, &nal->rbsp_buffer_size,
2480 length + FF_INPUT_BUFFER_PADDING_SIZE);
2481 if (!nal->rbsp_buffer)
2482 return AVERROR(ENOMEM);
2484 dst = nal->rbsp_buffer;
2486 memcpy(dst, src, i);
2488 while (si + 2 < length) {
2489 // remove escapes (very rare 1:2^22)
2490 if (src[si + 2] > 3) {
2491 dst[di++] = src[si++];
2492 dst[di++] = src[si++];
2493 } else if (src[si] == 0 && src[si + 1] == 0) {
2494 if (src[si + 2] == 3) { // escape
2500 if (s->skipped_bytes_pos_size < s->skipped_bytes) {
2501 s->skipped_bytes_pos_size *= 2;
2502 av_reallocp_array(&s->skipped_bytes_pos,
2503 s->skipped_bytes_pos_size,
2504 sizeof(*s->skipped_bytes_pos));
2505 if (!s->skipped_bytes_pos)
2506 return AVERROR(ENOMEM);
2508 if (s->skipped_bytes_pos)
2509 s->skipped_bytes_pos[s->skipped_bytes-1] = di - 1;
2511 } else // next start code
2515 dst[di++] = src[si++];
2518 dst[di++] = src[si++];
2521 memset(dst + di, 0, FF_INPUT_BUFFER_PADDING_SIZE);
2528 static int decode_nal_units(HEVCContext *s, const uint8_t *buf, int length)
2530 int i, consumed, ret = 0;
2535 /* split the input packet into NAL units, so we know the upper bound on the
2536 * number of slices in the frame */
2538 while (length >= 4) {
2540 int extract_length = 0;
2544 for (i = 0; i < s->nal_length_size; i++)
2545 extract_length = (extract_length << 8) | buf[i];
2546 buf += s->nal_length_size;
2547 length -= s->nal_length_size;
2549 if (extract_length > length) {
2550 av_log(s->avctx, AV_LOG_ERROR, "Invalid NAL unit size.\n");
2551 ret = AVERROR_INVALIDDATA;
2555 /* search start code */
2556 while (buf[0] != 0 || buf[1] != 0 || buf[2] != 1) {
2560 av_log(s->avctx, AV_LOG_ERROR, "No start code is found.\n");
2561 ret = AVERROR_INVALIDDATA;
2571 extract_length = length;
2573 if (s->nals_allocated < s->nb_nals + 1) {
2574 int new_size = s->nals_allocated + 1;
2575 HEVCNAL *tmp = av_realloc_array(s->nals, new_size, sizeof(*tmp));
2577 ret = AVERROR(ENOMEM);
2581 memset(s->nals + s->nals_allocated, 0,
2582 (new_size - s->nals_allocated) * sizeof(*tmp));
2583 av_reallocp_array(&s->skipped_bytes_nal, new_size, sizeof(*s->skipped_bytes_nal));
2584 av_reallocp_array(&s->skipped_bytes_pos_size_nal, new_size, sizeof(*s->skipped_bytes_pos_size_nal));
2585 av_reallocp_array(&s->skipped_bytes_pos_nal, new_size, sizeof(*s->skipped_bytes_pos_nal));
2586 s->skipped_bytes_pos_size_nal[s->nals_allocated] = 1024; // initial buffer size
2587 s->skipped_bytes_pos_nal[s->nals_allocated] = av_malloc_array(s->skipped_bytes_pos_size_nal[s->nals_allocated], sizeof(*s->skipped_bytes_pos));
2588 s->nals_allocated = new_size;
2590 s->skipped_bytes_pos_size = s->skipped_bytes_pos_size_nal[s->nb_nals];
2591 s->skipped_bytes_pos = s->skipped_bytes_pos_nal[s->nb_nals];
2592 nal = &s->nals[s->nb_nals];
2594 consumed = ff_hevc_extract_rbsp(s, buf, extract_length, nal);
2596 s->skipped_bytes_nal[s->nb_nals] = s->skipped_bytes;
2597 s->skipped_bytes_pos_size_nal[s->nb_nals] = s->skipped_bytes_pos_size;
2598 s->skipped_bytes_pos_nal[s->nb_nals++] = s->skipped_bytes_pos;
2606 ret = init_get_bits8(&s->HEVClc->gb, nal->data, nal->size);
2611 if (s->nal_unit_type == NAL_EOB_NUT ||
2612 s->nal_unit_type == NAL_EOS_NUT)
2619 /* parse the NAL units */
2620 for (i = 0; i < s->nb_nals; i++) {
2622 s->skipped_bytes = s->skipped_bytes_nal[i];
2623 s->skipped_bytes_pos = s->skipped_bytes_pos_nal[i];
2625 ret = decode_nal_unit(s, s->nals[i].data, s->nals[i].size);
2627 av_log(s->avctx, AV_LOG_WARNING,
2628 "Error parsing NAL unit #%d.\n", i);
2634 if (s->ref && s->threads_type == FF_THREAD_FRAME)
2635 ff_thread_report_progress(&s->ref->tf, INT_MAX, 0);
2640 static void print_md5(void *log_ctx, int level, uint8_t md5[16])
2643 for (i = 0; i < 16; i++)
2644 av_log(log_ctx, level, "%02"PRIx8, md5[i]);
2647 static int verify_md5(HEVCContext *s, AVFrame *frame)
2649 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(frame->format);
2654 return AVERROR(EINVAL);
2656 pixel_shift = desc->comp[0].depth_minus1 > 7;
2658 av_log(s->avctx, AV_LOG_DEBUG, "Verifying checksum for frame with POC %d: ",
2661 /* the checksums are LE, so we have to byteswap for >8bpp formats
2664 if (pixel_shift && !s->checksum_buf) {
2665 av_fast_malloc(&s->checksum_buf, &s->checksum_buf_size,
2666 FFMAX3(frame->linesize[0], frame->linesize[1],
2667 frame->linesize[2]));
2668 if (!s->checksum_buf)
2669 return AVERROR(ENOMEM);
2673 for (i = 0; frame->data[i]; i++) {
2674 int width = s->avctx->coded_width;
2675 int height = s->avctx->coded_height;
2676 int w = (i == 1 || i == 2) ? (width >> desc->log2_chroma_w) : width;
2677 int h = (i == 1 || i == 2) ? (height >> desc->log2_chroma_h) : height;
2680 av_md5_init(s->md5_ctx);
2681 for (j = 0; j < h; j++) {
2682 const uint8_t *src = frame->data[i] + j * frame->linesize[i];
2685 s->dsp.bswap16_buf((uint16_t*)s->checksum_buf,
2686 (const uint16_t*)src, w);
2687 src = s->checksum_buf;
2690 av_md5_update(s->md5_ctx, src, w << pixel_shift);
2692 av_md5_final(s->md5_ctx, md5);
2694 if (!memcmp(md5, s->md5[i], 16)) {
2695 av_log (s->avctx, AV_LOG_DEBUG, "plane %d - correct ", i);
2696 print_md5(s->avctx, AV_LOG_DEBUG, md5);
2697 av_log (s->avctx, AV_LOG_DEBUG, "; ");
2699 av_log (s->avctx, AV_LOG_ERROR, "mismatching checksum of plane %d - ", i);
2700 print_md5(s->avctx, AV_LOG_ERROR, md5);
2701 av_log (s->avctx, AV_LOG_ERROR, " != ");
2702 print_md5(s->avctx, AV_LOG_ERROR, s->md5[i]);
2703 av_log (s->avctx, AV_LOG_ERROR, "\n");
2704 return AVERROR_INVALIDDATA;
2708 av_log(s->avctx, AV_LOG_DEBUG, "\n");
2713 static int hevc_decode_frame(AVCodecContext *avctx, void *data, int *got_output,
2717 HEVCContext *s = avctx->priv_data;
2720 ret = ff_hevc_output_frame(s, data, 1);
2729 ret = decode_nal_units(s, avpkt->data, avpkt->size);
2733 /* verify the SEI checksum */
2734 if (avctx->err_recognition & AV_EF_CRCCHECK && s->is_decoded &&
2736 ret = verify_md5(s, s->ref->frame);
2737 if (ret < 0 && avctx->err_recognition & AV_EF_EXPLODE) {
2738 ff_hevc_unref_frame(s, s->ref, ~0);
2744 if (s->is_decoded) {
2745 av_log(avctx, AV_LOG_DEBUG, "Decoded frame with POC %d.\n", s->poc);
2749 if (s->output_frame->buf[0]) {
2750 av_frame_move_ref(data, s->output_frame);
2757 static int hevc_ref_frame(HEVCContext *s, HEVCFrame *dst, HEVCFrame *src)
2761 ret = ff_thread_ref_frame(&dst->tf, &src->tf);
2765 dst->tab_mvf_buf = av_buffer_ref(src->tab_mvf_buf);
2766 if (!dst->tab_mvf_buf)
2768 dst->tab_mvf = src->tab_mvf;
2770 dst->rpl_tab_buf = av_buffer_ref(src->rpl_tab_buf);
2771 if (!dst->rpl_tab_buf)
2773 dst->rpl_tab = src->rpl_tab;
2775 dst->rpl_buf = av_buffer_ref(src->rpl_buf);
2779 dst->poc = src->poc;
2780 dst->ctb_count = src->ctb_count;
2781 dst->window = src->window;
2782 dst->flags = src->flags;
2783 dst->sequence = src->sequence;
2787 ff_hevc_unref_frame(s, dst, ~0);
2788 return AVERROR(ENOMEM);
2791 static av_cold int hevc_decode_free(AVCodecContext *avctx)
2793 HEVCContext *s = avctx->priv_data;
2794 HEVCLocalContext *lc = s->HEVClc;
2799 av_freep(&s->md5_ctx);
2801 for(i=0; i < s->nals_allocated; i++) {
2802 av_freep(&s->skipped_bytes_pos_nal[i]);
2804 av_freep(&s->skipped_bytes_pos_size_nal);
2805 av_freep(&s->skipped_bytes_nal);
2806 av_freep(&s->skipped_bytes_pos_nal);
2808 av_freep(&s->cabac_state);
2810 av_frame_free(&s->tmp_frame);
2811 av_frame_free(&s->output_frame);
2813 for (i = 0; i < FF_ARRAY_ELEMS(s->DPB); i++) {
2814 ff_hevc_unref_frame(s, &s->DPB[i], ~0);
2815 av_frame_free(&s->DPB[i].frame);
2818 for (i = 0; i < FF_ARRAY_ELEMS(s->vps_list); i++)
2819 av_buffer_unref(&s->vps_list[i]);
2820 for (i = 0; i < FF_ARRAY_ELEMS(s->sps_list); i++)
2821 av_buffer_unref(&s->sps_list[i]);
2822 for (i = 0; i < FF_ARRAY_ELEMS(s->pps_list); i++)
2823 av_buffer_unref(&s->pps_list[i]);
2825 av_freep(&s->sh.entry_point_offset);
2826 av_freep(&s->sh.offset);
2827 av_freep(&s->sh.size);
2829 for (i = 1; i < s->threads_number; i++) {
2830 lc = s->HEVClcList[i];
2832 av_freep(&s->HEVClcList[i]);
2833 av_freep(&s->sList[i]);
2836 if (s->HEVClc == s->HEVClcList[0])
2838 av_freep(&s->HEVClcList[0]);
2840 for (i = 0; i < s->nals_allocated; i++)
2841 av_freep(&s->nals[i].rbsp_buffer);
2843 s->nals_allocated = 0;
2848 static av_cold int hevc_init_context(AVCodecContext *avctx)
2850 HEVCContext *s = avctx->priv_data;
2855 s->HEVClc = av_mallocz(sizeof(HEVCLocalContext));
2858 s->HEVClcList[0] = s->HEVClc;
2861 s->cabac_state = av_malloc(HEVC_CONTEXTS);
2862 if (!s->cabac_state)
2865 s->tmp_frame = av_frame_alloc();
2869 s->output_frame = av_frame_alloc();
2870 if (!s->output_frame)
2873 for (i = 0; i < FF_ARRAY_ELEMS(s->DPB); i++) {
2874 s->DPB[i].frame = av_frame_alloc();
2875 if (!s->DPB[i].frame)
2877 s->DPB[i].tf.f = s->DPB[i].frame;
2880 s->max_ra = INT_MAX;
2882 s->md5_ctx = av_md5_alloc();
2886 ff_dsputil_init(&s->dsp, avctx);
2888 s->context_initialized = 1;
2893 hevc_decode_free(avctx);
2894 return AVERROR(ENOMEM);
2897 static int hevc_update_thread_context(AVCodecContext *dst,
2898 const AVCodecContext *src)
2900 HEVCContext *s = dst->priv_data;
2901 HEVCContext *s0 = src->priv_data;
2904 if (!s->context_initialized) {
2905 ret = hevc_init_context(dst);
2910 for (i = 0; i < FF_ARRAY_ELEMS(s->DPB); i++) {
2911 ff_hevc_unref_frame(s, &s->DPB[i], ~0);
2912 if (s0->DPB[i].frame->buf[0]) {
2913 ret = hevc_ref_frame(s, &s->DPB[i], &s0->DPB[i]);
2919 for (i = 0; i < FF_ARRAY_ELEMS(s->vps_list); i++) {
2920 av_buffer_unref(&s->vps_list[i]);
2921 if (s0->vps_list[i]) {
2922 s->vps_list[i] = av_buffer_ref(s0->vps_list[i]);
2923 if (!s->vps_list[i])
2924 return AVERROR(ENOMEM);
2928 for (i = 0; i < FF_ARRAY_ELEMS(s->sps_list); i++) {
2929 av_buffer_unref(&s->sps_list[i]);
2930 if (s0->sps_list[i]) {
2931 s->sps_list[i] = av_buffer_ref(s0->sps_list[i]);
2932 if (!s->sps_list[i])
2933 return AVERROR(ENOMEM);
2937 for (i = 0; i < FF_ARRAY_ELEMS(s->pps_list); i++) {
2938 av_buffer_unref(&s->pps_list[i]);
2939 if (s0->pps_list[i]) {
2940 s->pps_list[i] = av_buffer_ref(s0->pps_list[i]);
2941 if (!s->pps_list[i])
2942 return AVERROR(ENOMEM);
2946 if (s->sps != s0->sps)
2947 ret = set_sps(s, s0->sps);
2949 s->seq_decode = s0->seq_decode;
2950 s->seq_output = s0->seq_output;
2951 s->pocTid0 = s0->pocTid0;
2952 s->max_ra = s0->max_ra;
2954 s->is_nalff = s0->is_nalff;
2955 s->nal_length_size = s0->nal_length_size;
2957 s->threads_number = s0->threads_number;
2958 s->threads_type = s0->threads_type;
2961 s->seq_decode = (s->seq_decode + 1) & 0xff;
2962 s->max_ra = INT_MAX;
2968 static int hevc_decode_extradata(HEVCContext *s)
2970 AVCodecContext *avctx = s->avctx;
2974 bytestream2_init(&gb, avctx->extradata, avctx->extradata_size);
2976 if (avctx->extradata_size > 3 &&
2977 (avctx->extradata[0] || avctx->extradata[1] ||
2978 avctx->extradata[2] > 1)) {
2979 /* It seems the extradata is encoded as hvcC format.
2980 * Temporarily, we support configurationVersion==0 until 14496-15 3rd
2981 * is finalized. When finalized, configurationVersion will be 1 and we
2982 * can recognize hvcC by checking if avctx->extradata[0]==1 or not. */
2983 int i, j, num_arrays, nal_len_size;
2987 bytestream2_skip(&gb, 21);
2988 nal_len_size = (bytestream2_get_byte(&gb) & 3) + 1;
2989 num_arrays = bytestream2_get_byte(&gb);
2991 /* nal units in the hvcC always have length coded with 2 bytes,
2992 * so put a fake nal_length_size = 2 while parsing them */
2993 s->nal_length_size = 2;
2995 /* Decode nal units from hvcC. */
2996 for (i = 0; i < num_arrays; i++) {
2997 int type = bytestream2_get_byte(&gb) & 0x3f;
2998 int cnt = bytestream2_get_be16(&gb);
3000 for (j = 0; j < cnt; j++) {
3001 // +2 for the nal size field
3002 int nalsize = bytestream2_peek_be16(&gb) + 2;
3003 if (bytestream2_get_bytes_left(&gb) < nalsize) {
3004 av_log(s->avctx, AV_LOG_ERROR,
3005 "Invalid NAL unit size in extradata.\n");
3006 return AVERROR_INVALIDDATA;
3009 ret = decode_nal_units(s, gb.buffer, nalsize);
3011 av_log(avctx, AV_LOG_ERROR,
3012 "Decoding nal unit %d %d from hvcC failed\n",
3016 bytestream2_skip(&gb, nalsize);
3020 /* Now store right nal length size, that will be used to parse
3022 s->nal_length_size = nal_len_size;
3025 ret = decode_nal_units(s, avctx->extradata, avctx->extradata_size);
3032 static av_cold int hevc_decode_init(AVCodecContext *avctx)
3034 HEVCContext *s = avctx->priv_data;
3037 ff_init_cabac_states();
3039 avctx->internal->allocate_progress = 1;
3041 ret = hevc_init_context(avctx);
3045 s->enable_parallel_tiles = 0;
3046 s->picture_struct = 0;
3048 if(avctx->active_thread_type & FF_THREAD_SLICE)
3049 s->threads_number = avctx->thread_count;
3051 s->threads_number = 1;
3053 if (avctx->extradata_size > 0 && avctx->extradata) {
3054 ret = hevc_decode_extradata(s);
3056 hevc_decode_free(avctx);
3061 if((avctx->active_thread_type & FF_THREAD_FRAME) && avctx->thread_count > 1)
3062 s->threads_type = FF_THREAD_FRAME;
3064 s->threads_type = FF_THREAD_SLICE;
3069 static av_cold int hevc_init_thread_copy(AVCodecContext *avctx)
3071 HEVCContext *s = avctx->priv_data;
3074 memset(s, 0, sizeof(*s));
3076 ret = hevc_init_context(avctx);
3083 static void hevc_decode_flush(AVCodecContext *avctx)
3085 HEVCContext *s = avctx->priv_data;
3086 ff_hevc_flush_dpb(s);
3087 s->max_ra = INT_MAX;
3090 #define OFFSET(x) offsetof(HEVCContext, x)
3091 #define PAR (AV_OPT_FLAG_DECODING_PARAM | AV_OPT_FLAG_VIDEO_PARAM)
3093 static const AVProfile profiles[] = {
3094 { FF_PROFILE_HEVC_MAIN, "Main" },
3095 { FF_PROFILE_HEVC_MAIN_10, "Main 10" },
3096 { FF_PROFILE_HEVC_MAIN_STILL_PICTURE, "Main Still Picture" },
3097 { FF_PROFILE_UNKNOWN },
3100 static const AVOption options[] = {
3101 { "apply_defdispwin", "Apply default display window from VUI", OFFSET(apply_defdispwin),
3102 AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1, PAR },
3103 { "strict-displaywin", "stricly apply default display window size", OFFSET(apply_defdispwin),
3104 AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1, PAR },
3108 static const AVClass hevc_decoder_class = {
3109 .class_name = "HEVC decoder",
3110 .item_name = av_default_item_name,
3112 .version = LIBAVUTIL_VERSION_INT,
3115 AVCodec ff_hevc_decoder = {
3117 .long_name = NULL_IF_CONFIG_SMALL("HEVC (High Efficiency Video Coding)"),
3118 .type = AVMEDIA_TYPE_VIDEO,
3119 .id = AV_CODEC_ID_HEVC,
3120 .priv_data_size = sizeof(HEVCContext),
3121 .priv_class = &hevc_decoder_class,
3122 .init = hevc_decode_init,
3123 .close = hevc_decode_free,
3124 .decode = hevc_decode_frame,
3125 .flush = hevc_decode_flush,
3126 .update_thread_context = hevc_update_thread_context,
3127 .init_thread_copy = hevc_init_thread_copy,
3128 .capabilities = CODEC_CAP_DR1 | CODEC_CAP_DELAY |
3129 CODEC_CAP_SLICE_THREADS | CODEC_CAP_FRAME_THREADS,
3130 .profiles = NULL_IF_CONFIG_SMALL(profiles),