FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/hevc/hevcdec.c
Date: 2026-09-20 02:22:12
Exec Total Coverage
Lines: 2024 2518 80.4%
Functions: 53 58 91.4%
Branches: 1429 1893 75.5%

Line Branch Exec Source
1 /*
2 * HEVC video Decoder
3 *
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
8 *
9 * This file is part of FFmpeg.
10 *
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.
15 *
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.
20 *
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
24 */
25
26 #include "config_components.h"
27
28 #include "libavutil/attributes.h"
29 #include "libavutil/avstring.h"
30 #include "libavutil/common.h"
31 #include "libavutil/container_fifo.h"
32 #include "libavutil/film_grain_params.h"
33 #include "libavutil/internal.h"
34 #include "libavutil/md5.h"
35 #include "libavutil/mem.h"
36 #include "libavutil/opt.h"
37 #include "libavutil/pixdesc.h"
38 #include "libavutil/stereo3d.h"
39 #include "libavutil/tdrdi.h"
40 #include "libavutil/timecode.h"
41 #include "libavutil/refstruct.h"
42
43 #include "libavcodec/aom_film_grain.h"
44 #include "libavcodec/bswapdsp.h"
45 #include "libavcodec/cabac_functions.h"
46 #include "libavcodec/codec_internal.h"
47 #include "libavcodec/decode.h"
48 #include "libavcodec/golomb.h"
49 #include "libavcodec/h274.h"
50 #include "libavcodec/hwaccel_internal.h"
51 #include "libavcodec/hwconfig.h"
52 #include "libavcodec/internal.h"
53 #include "libavcodec/profiles.h"
54 #include "libavcodec/progressframe.h"
55 #include "libavcodec/thread.h"
56 #include "libavcodec/threadprogress.h"
57
58 #include "hevc.h"
59 #include "parse.h"
60 #include "hevcdec.h"
61
62 static const uint8_t hevc_pel_weight[65] = { [2] = 0, [4] = 1, [6] = 2, [8] = 3, [12] = 4, [16] = 5, [24] = 6, [32] = 7, [48] = 8, [64] = 9 };
63
64 /**
65 * NOTE: Each function hls_foo correspond to the function foo in the
66 * specification (HLS stands for High Level Syntax).
67 */
68
69 /**
70 * Section 5.7
71 */
72
73 /* free everything allocated by pic_arrays_init() */
74 1423 static void pic_arrays_free(HEVCLayerContext *l)
75 {
76 1423 av_freep(&l->sao);
77 1423 av_freep(&l->deblock);
78
79 1423 av_freep(&l->skip_flag);
80 1423 av_freep(&l->tab_ct_depth);
81
82 1423 av_freep(&l->tab_ipm);
83 1423 av_freep(&l->cbf_luma);
84 1423 av_freep(&l->is_pcm);
85
86 1423 av_freep(&l->qp_y_tab);
87 1423 av_freep(&l->tab_slice_address);
88 1423 av_freep(&l->filter_slice_edges);
89
90 1423 av_freep(&l->horizontal_bs);
91 1423 av_freep(&l->vertical_bs);
92
93
2/2
✓ Branch 0 taken 4269 times.
✓ Branch 1 taken 1423 times.
5692 for (int i = 0; i < 3; i++) {
94 4269 av_freep(&l->sao_pixel_buffer_h[i]);
95 4269 av_freep(&l->sao_pixel_buffer_v[i]);
96 }
97
98 1423 av_refstruct_pool_uninit(&l->tab_mvf_pool);
99 1423 av_refstruct_pool_uninit(&l->rpl_tab_pool);
100 1423 }
101
102 /* allocate arrays that depend on frame dimensions */
103 249 static int pic_arrays_init(HEVCLayerContext *l, const HEVCSPS *sps)
104 {
105 249 int log2_min_cb_size = sps->log2_min_cb_size;
106 249 int width = sps->width;
107 249 int height = sps->height;
108 249 int pic_size_in_ctb = ((width >> log2_min_cb_size) + 1) *
109 249 ((height >> log2_min_cb_size) + 1);
110 249 int ctb_count = sps->ctb_width * sps->ctb_height;
111 249 int min_pu_size = sps->min_pu_width * sps->min_pu_height;
112
113 249 l->bs_width = (width >> 2) + 1;
114 249 l->bs_height = (height >> 2) + 1;
115
116 249 l->sao = av_calloc(ctb_count, sizeof(*l->sao));
117 249 l->deblock = av_calloc(ctb_count, sizeof(*l->deblock));
118
2/4
✓ Branch 0 taken 249 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 249 times.
249 if (!l->sao || !l->deblock)
119 goto fail;
120
121 249 l->skip_flag = av_malloc_array(sps->min_cb_height, sps->min_cb_width);
122 249 l->tab_ct_depth = av_malloc_array(sps->min_cb_height, sps->min_cb_width);
123
2/4
✓ Branch 0 taken 249 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 249 times.
249 if (!l->skip_flag || !l->tab_ct_depth)
124 goto fail;
125
126 249 l->cbf_luma = av_malloc_array(sps->min_tb_width, sps->min_tb_height);
127 249 l->tab_ipm = av_mallocz(min_pu_size);
128 249 l->is_pcm = av_malloc_array(sps->min_pu_width + 1, sps->min_pu_height + 1);
129
3/6
✓ Branch 0 taken 249 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 249 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 249 times.
249 if (!l->tab_ipm || !l->cbf_luma || !l->is_pcm)
130 goto fail;
131
132 249 l->filter_slice_edges = av_mallocz(ctb_count);
133 249 l->tab_slice_address = av_malloc_array(pic_size_in_ctb,
134 sizeof(*l->tab_slice_address));
135 249 l->qp_y_tab = av_calloc(pic_size_in_ctb,
136 sizeof(*l->qp_y_tab));
137
3/6
✓ Branch 0 taken 249 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 249 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 249 times.
249 if (!l->qp_y_tab || !l->filter_slice_edges || !l->tab_slice_address)
138 goto fail;
139
140 249 l->horizontal_bs = av_calloc(l->bs_width, l->bs_height);
141 249 l->vertical_bs = av_calloc(l->bs_width, l->bs_height);
142
2/4
✓ Branch 0 taken 249 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 249 times.
249 if (!l->horizontal_bs || !l->vertical_bs)
143 goto fail;
144
145 249 l->tab_mvf_pool = av_refstruct_pool_alloc(min_pu_size * sizeof(MvField), 0);
146 249 l->rpl_tab_pool = av_refstruct_pool_alloc(ctb_count * sizeof(RefPicListTab), 0);
147
2/4
✓ Branch 0 taken 249 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 249 times.
249 if (!l->tab_mvf_pool || !l->rpl_tab_pool)
148 goto fail;
149
150
2/2
✓ Branch 0 taken 217 times.
✓ Branch 1 taken 32 times.
249 if (sps->sao_enabled) {
151
2/2
✓ Branch 0 taken 216 times.
✓ Branch 1 taken 1 times.
217 int c_count = (sps->chroma_format_idc != 0) ? 3 : 1;
152
153
2/2
✓ Branch 0 taken 649 times.
✓ Branch 1 taken 217 times.
866 for (int c_idx = 0; c_idx < c_count; c_idx++) {
154 649 int w = sps->width >> sps->hshift[c_idx];
155 649 int h = sps->height >> sps->vshift[c_idx];
156 649 l->sao_pixel_buffer_h[c_idx] =
157 649 av_mallocz((w * 2 * sps->ctb_height) <<
158 649 sps->pixel_shift);
159 649 l->sao_pixel_buffer_v[c_idx] =
160 649 av_mallocz((h * 2 * sps->ctb_width) <<
161 649 sps->pixel_shift);
162
1/2
✓ Branch 0 taken 649 times.
✗ Branch 1 not taken.
649 if (!l->sao_pixel_buffer_h[c_idx] ||
163
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 649 times.
649 !l->sao_pixel_buffer_v[c_idx])
164 goto fail;
165 }
166 }
167
168 249 return 0;
169
170 fail:
171 pic_arrays_free(l);
172 return AVERROR(ENOMEM);
173 }
174
175 1501 static int pred_weight_table(SliceHeader *sh, void *logctx,
176 const HEVCSPS *sps, GetBitContext *gb)
177 {
178 1501 int i = 0;
179 1501 int j = 0;
180 int luma_log2_weight_denom;
181 unsigned luma_weight_flags, chroma_weight_flags;
182
183 1501 luma_log2_weight_denom = get_ue_golomb_long(gb);
184
2/4
✓ Branch 0 taken 1501 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1501 times.
1501 if (luma_log2_weight_denom < 0 || luma_log2_weight_denom > 7) {
185 av_log(logctx, AV_LOG_ERROR, "luma_log2_weight_denom %d is invalid\n", luma_log2_weight_denom);
186 return AVERROR_INVALIDDATA;
187 }
188 1501 sh->luma_log2_weight_denom = luma_log2_weight_denom;
189
1/2
✓ Branch 0 taken 1501 times.
✗ Branch 1 not taken.
1501 if (sps->chroma_format_idc != 0) {
190 1501 int64_t chroma_log2_weight_denom = luma_log2_weight_denom + (int64_t)get_se_golomb(gb);
191
2/4
✓ Branch 0 taken 1501 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1501 times.
1501 if (chroma_log2_weight_denom < 0 || chroma_log2_weight_denom > 7) {
192 av_log(logctx, AV_LOG_ERROR, "chroma_log2_weight_denom %"PRId64" is invalid\n", chroma_log2_weight_denom);
193 return AVERROR_INVALIDDATA;
194 }
195 1501 sh->chroma_log2_weight_denom = chroma_log2_weight_denom;
196 }
197
198 1501 luma_weight_flags = get_bits(gb, sh->nb_refs[L0]);
199
1/2
✓ Branch 0 taken 1501 times.
✗ Branch 1 not taken.
1501 chroma_weight_flags = sps->chroma_format_idc != 0 ? get_bits(gb, sh->nb_refs[L0]) : 0;
200
2/2
✓ Branch 0 taken 4549 times.
✓ Branch 1 taken 1501 times.
6050 for (i = 0; i < sh->nb_refs[L0]; i++) {
201 4549 unsigned flag_bit = 1 << (sh->nb_refs[L0] - 1 - i);
202
203
2/2
✓ Branch 0 taken 3117 times.
✓ Branch 1 taken 1432 times.
4549 if (luma_weight_flags & flag_bit) {
204 3117 int delta_luma_weight_l0 = get_se_golomb(gb);
205
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3117 times.
3117 if ((int8_t)delta_luma_weight_l0 != delta_luma_weight_l0)
206 return AVERROR_INVALIDDATA;
207 3117 sh->luma_weight_l0[i] = (1 << sh->luma_log2_weight_denom) + delta_luma_weight_l0;
208 3117 sh->luma_offset_l0[i] = get_se_golomb(gb);
209 } else {
210 1432 sh->luma_weight_l0[i] = 1 << sh->luma_log2_weight_denom;
211 1432 sh->luma_offset_l0[i] = 0;
212 }
213
2/2
✓ Branch 0 taken 1131 times.
✓ Branch 1 taken 3418 times.
4549 if (chroma_weight_flags & flag_bit) {
214
2/2
✓ Branch 0 taken 2262 times.
✓ Branch 1 taken 1131 times.
3393 for (j = 0; j < 2; j++) {
215 2262 int delta_chroma_weight_l0 = get_se_golomb(gb);
216 2262 int delta_chroma_offset_l0 = get_se_golomb(gb);
217
218
1/2
✓ Branch 0 taken 2262 times.
✗ Branch 1 not taken.
2262 if ( (int8_t)delta_chroma_weight_l0 != delta_chroma_weight_l0
219
2/4
✓ Branch 0 taken 2262 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 2262 times.
2262 || delta_chroma_offset_l0 < -(1<<17) || delta_chroma_offset_l0 > (1<<17)) {
220 return AVERROR_INVALIDDATA;
221 }
222
223 2262 sh->chroma_weight_l0[i][j] = (1 << sh->chroma_log2_weight_denom) + delta_chroma_weight_l0;
224 2262 sh->chroma_offset_l0[i][j] = av_clip((delta_chroma_offset_l0 - ((128 * sh->chroma_weight_l0[i][j])
225 2262 >> sh->chroma_log2_weight_denom) + 128), -128, 127);
226 }
227 } else {
228 3418 sh->chroma_weight_l0[i][0] = 1 << sh->chroma_log2_weight_denom;
229 3418 sh->chroma_offset_l0[i][0] = 0;
230 3418 sh->chroma_weight_l0[i][1] = 1 << sh->chroma_log2_weight_denom;
231 3418 sh->chroma_offset_l0[i][1] = 0;
232 }
233 }
234
2/2
✓ Branch 0 taken 696 times.
✓ Branch 1 taken 805 times.
1501 if (sh->slice_type == HEVC_SLICE_B) {
235 696 luma_weight_flags = get_bits(gb, sh->nb_refs[L1]);
236
1/2
✓ Branch 0 taken 696 times.
✗ Branch 1 not taken.
696 chroma_weight_flags = sps->chroma_format_idc != 0 ? get_bits(gb, sh->nb_refs[L1]) : 0;
237
2/2
✓ Branch 0 taken 1405 times.
✓ Branch 1 taken 696 times.
2101 for (i = 0; i < sh->nb_refs[L1]; i++) {
238 1405 unsigned flag_bit = 1 << (sh->nb_refs[L1] - 1 - i);
239
240
2/2
✓ Branch 0 taken 746 times.
✓ Branch 1 taken 659 times.
1405 if (luma_weight_flags & flag_bit) {
241 746 int delta_luma_weight_l1 = get_se_golomb(gb);
242
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 746 times.
746 if ((int8_t)delta_luma_weight_l1 != delta_luma_weight_l1)
243 return AVERROR_INVALIDDATA;
244 746 sh->luma_weight_l1[i] = (1 << sh->luma_log2_weight_denom) + delta_luma_weight_l1;
245 746 sh->luma_offset_l1[i] = get_se_golomb(gb);
246 } else {
247 659 sh->luma_weight_l1[i] = 1 << sh->luma_log2_weight_denom;
248 659 sh->luma_offset_l1[i] = 0;
249 }
250
2/2
✓ Branch 0 taken 236 times.
✓ Branch 1 taken 1169 times.
1405 if (chroma_weight_flags & flag_bit) {
251
2/2
✓ Branch 0 taken 472 times.
✓ Branch 1 taken 236 times.
708 for (j = 0; j < 2; j++) {
252 472 int delta_chroma_weight_l1 = get_se_golomb(gb);
253 472 int delta_chroma_offset_l1 = get_se_golomb(gb);
254
255
1/2
✓ Branch 0 taken 472 times.
✗ Branch 1 not taken.
472 if ( (int8_t)delta_chroma_weight_l1 != delta_chroma_weight_l1
256
2/4
✓ Branch 0 taken 472 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 472 times.
472 || delta_chroma_offset_l1 < -(1<<17) || delta_chroma_offset_l1 > (1<<17)) {
257 return AVERROR_INVALIDDATA;
258 }
259
260 472 sh->chroma_weight_l1[i][j] = (1 << sh->chroma_log2_weight_denom) + delta_chroma_weight_l1;
261 472 sh->chroma_offset_l1[i][j] = av_clip((delta_chroma_offset_l1 - ((128 * sh->chroma_weight_l1[i][j])
262 472 >> sh->chroma_log2_weight_denom) + 128), -128, 127);
263 }
264 } else {
265 1169 sh->chroma_weight_l1[i][0] = 1 << sh->chroma_log2_weight_denom;
266 1169 sh->chroma_offset_l1[i][0] = 0;
267 1169 sh->chroma_weight_l1[i][1] = 1 << sh->chroma_log2_weight_denom;
268 1169 sh->chroma_offset_l1[i][1] = 0;
269 }
270 }
271 }
272 1501 return 0;
273 }
274
275 20365 static int decode_lt_rps(const HEVCSPS *sps, LongTermRPS *rps,
276 GetBitContext *gb, int cur_poc, int poc_lsb)
277 {
278 20365 int max_poc_lsb = 1 << sps->log2_max_poc_lsb;
279 20365 int prev_delta_msb = 0;
280 20365 unsigned int nb_sps = 0, nb_sh;
281 int i;
282
283 20365 rps->nb_refs = 0;
284
2/2
✓ Branch 0 taken 18963 times.
✓ Branch 1 taken 1402 times.
20365 if (!sps->long_term_ref_pics_present)
285 18963 return 0;
286
287
2/2
✓ Branch 0 taken 499 times.
✓ Branch 1 taken 903 times.
1402 if (sps->num_long_term_ref_pics_sps > 0)
288 499 nb_sps = get_ue_golomb_long(gb);
289 1402 nb_sh = get_ue_golomb_long(gb);
290
291
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1402 times.
1402 if (nb_sps > sps->num_long_term_ref_pics_sps)
292 return AVERROR_INVALIDDATA;
293
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1402 times.
1402 if (nb_sh + (uint64_t)nb_sps > FF_ARRAY_ELEMS(rps->poc))
294 return AVERROR_INVALIDDATA;
295
296 1402 rps->nb_refs = nb_sh + nb_sps;
297
298
2/2
✓ Branch 0 taken 1928 times.
✓ Branch 1 taken 1402 times.
3330 for (i = 0; i < rps->nb_refs; i++) {
299
300
2/2
✓ Branch 0 taken 184 times.
✓ Branch 1 taken 1744 times.
1928 if (i < nb_sps) {
301 184 uint8_t lt_idx_sps = 0;
302
303
1/2
✓ Branch 0 taken 184 times.
✗ Branch 1 not taken.
184 if (sps->num_long_term_ref_pics_sps > 1)
304 184 lt_idx_sps = get_bits(gb, av_ceil_log2(sps->num_long_term_ref_pics_sps));
305
306 184 rps->poc[i] = sps->lt_ref_pic_poc_lsb_sps[lt_idx_sps];
307 184 rps->used[i] = !!(sps->used_by_curr_pic_lt & (1U << lt_idx_sps));
308 } else {
309 1744 rps->poc[i] = get_bits(gb, sps->log2_max_poc_lsb);
310 1744 rps->used[i] = get_bits1(gb);
311 }
312
313 1928 rps->poc_msb_present[i] = get_bits1(gb);
314
2/2
✓ Branch 0 taken 48 times.
✓ Branch 1 taken 1880 times.
1928 if (rps->poc_msb_present[i]) {
315 48 int64_t delta = get_ue_golomb_long(gb);
316 int64_t poc;
317
318
3/4
✓ Branch 0 taken 31 times.
✓ Branch 1 taken 17 times.
✓ Branch 2 taken 31 times.
✗ Branch 3 not taken.
48 if (i && i != nb_sps)
319 31 delta += prev_delta_msb;
320
321 48 poc = rps->poc[i] + cur_poc - delta * max_poc_lsb - poc_lsb;
322
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 48 times.
48 if (poc != (int32_t)poc)
323 return AVERROR_INVALIDDATA;
324 48 rps->poc[i] = poc;
325 48 prev_delta_msb = delta;
326 }
327 }
328
329 1402 return 0;
330 }
331
332 823 static void export_stream_params(HEVCContext *s, const HEVCSPS *sps)
333 {
334 823 AVCodecContext *avctx = s->avctx;
335 823 const HEVCVPS *vps = sps->vps;
336 823 const HEVCWindow *ow = &sps->output_window;
337 823 unsigned int num = 0, den = 0;
338
339 823 avctx->pix_fmt = sps->pix_fmt;
340 823 avctx->coded_width = sps->width;
341 823 avctx->coded_height = sps->height;
342 823 avctx->width = sps->width - ow->left_offset - ow->right_offset;
343 823 avctx->height = sps->height - ow->top_offset - ow->bottom_offset;
344 823 avctx->has_b_frames = sps->temporal_layer[sps->max_sub_layers - 1].num_reorder_pics;
345 823 avctx->profile = sps->ptl.general_ptl.profile_idc;
346 823 avctx->level = sps->ptl.general_ptl.level_idc;
347
348 823 ff_set_sar(avctx, sps->vui.common.sar);
349
350
2/2
✓ Branch 0 taken 132 times.
✓ Branch 1 taken 691 times.
823 if (sps->vui.common.video_signal_type_present_flag)
351 132 avctx->color_range = sps->vui.common.video_full_range_flag ? AVCOL_RANGE_JPEG
352
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 122 times.
132 : AVCOL_RANGE_MPEG;
353 else
354 691 avctx->color_range = AVCOL_RANGE_MPEG;
355
356
2/2
✓ Branch 0 taken 125 times.
✓ Branch 1 taken 698 times.
823 if (sps->vui.common.colour_description_present_flag) {
357 125 avctx->color_primaries = sps->vui.common.colour_primaries;
358 125 avctx->color_trc = sps->vui.common.transfer_characteristics;
359 125 avctx->colorspace = sps->vui.common.matrix_coeffs;
360 } else {
361 698 avctx->color_primaries = AVCOL_PRI_UNSPECIFIED;
362 698 avctx->color_trc = AVCOL_TRC_UNSPECIFIED;
363 698 avctx->colorspace = AVCOL_SPC_UNSPECIFIED;
364 }
365
366 823 avctx->chroma_sample_location = AVCHROMA_LOC_UNSPECIFIED;
367
2/2
✓ Branch 0 taken 779 times.
✓ Branch 1 taken 44 times.
823 if (sps->chroma_format_idc == 1) {
368
2/2
✓ Branch 0 taken 47 times.
✓ Branch 1 taken 732 times.
779 if (sps->vui.common.chroma_loc_info_present_flag) {
369
1/2
✓ Branch 0 taken 47 times.
✗ Branch 1 not taken.
47 if (sps->vui.common.chroma_sample_loc_type_top_field <= 5)
370 47 avctx->chroma_sample_location = sps->vui.common.chroma_sample_loc_type_top_field + 1;
371 } else
372 732 avctx->chroma_sample_location = AVCHROMA_LOC_LEFT;
373 }
374
375
2/2
✓ Branch 0 taken 49 times.
✓ Branch 1 taken 774 times.
823 if (vps->vps_timing_info_present_flag) {
376 49 num = vps->vps_num_units_in_tick;
377 49 den = vps->vps_time_scale;
378
2/2
✓ Branch 0 taken 112 times.
✓ Branch 1 taken 662 times.
774 } else if (sps->vui.vui_timing_info_present_flag) {
379 112 num = sps->vui.vui_num_units_in_tick;
380 112 den = sps->vui.vui_time_scale;
381 }
382
383
3/4
✓ Branch 0 taken 161 times.
✓ Branch 1 taken 662 times.
✓ Branch 2 taken 161 times.
✗ Branch 3 not taken.
823 if (num > 0 && den > 0)
384 161 av_reduce(&avctx->framerate.den, &avctx->framerate.num,
385 num, den, 1 << 30);
386 823 }
387
388 11049 static int export_stream_params_from_sei(HEVCContext *s)
389 {
390 11049 AVCodecContext *avctx = s->avctx;
391
392
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 11049 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
11049 if (s->sei.common.alternative_transfer.present &&
393 av_color_transfer_name(s->sei.common.alternative_transfer.preferred_transfer_characteristics) &&
394 s->sei.common.alternative_transfer.preferred_transfer_characteristics != AVCOL_TRC_UNSPECIFIED) {
395 avctx->color_trc = s->sei.common.alternative_transfer.preferred_transfer_characteristics;
396 }
397
398 11049 return 0;
399 }
400
401 597 static int export_multilayer(HEVCContext *s, const HEVCVPS *vps)
402 {
403 597 const HEVCSEITDRDI *tdrdi = &s->sei.tdrdi;
404
405 597 av_freep(&s->view_ids_available);
406 597 s->nb_view_ids_available = 0;
407 597 av_freep(&s->view_pos_available);
408 597 s->nb_view_pos_available = 0;
409
410 // don't export anything in the trivial case (1 layer, view id=0)
411
3/4
✓ Branch 0 taken 574 times.
✓ Branch 1 taken 23 times.
✓ Branch 2 taken 574 times.
✗ Branch 3 not taken.
597 if (vps->nb_layers < 2 && !vps->view_id[0])
412 574 return 0;
413
414 23 s->view_ids_available = av_calloc(vps->nb_layers, sizeof(*s->view_ids_available));
415
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 23 times.
23 if (!s->view_ids_available)
416 return AVERROR(ENOMEM);
417
418
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 18 times.
23 if (tdrdi->num_ref_displays) {
419 5 s->view_pos_available = av_calloc(vps->nb_layers, sizeof(*s->view_pos_available));
420
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
5 if (!s->view_pos_available)
421 return AVERROR(ENOMEM);
422 }
423
424
2/2
✓ Branch 0 taken 46 times.
✓ Branch 1 taken 23 times.
69 for (int i = 0; i < vps->nb_layers; i++) {
425 46 s->view_ids_available[i] = vps->view_id[i];
426
427
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 36 times.
46 if (s->view_pos_available) {
428 10 s->view_pos_available[i] = vps->view_id[i] == tdrdi->left_view_id[0] ?
429
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 5 times.
15 AV_STEREO3D_VIEW_LEFT :
430
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
5 vps->view_id[i] == tdrdi->right_view_id[0] ?
431 AV_STEREO3D_VIEW_RIGHT : AV_STEREO3D_VIEW_UNSPEC;
432 }
433 }
434 23 s->nb_view_ids_available = vps->nb_layers;
435
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 18 times.
23 s->nb_view_pos_available = s->view_pos_available ? vps->nb_layers : 0;
436
437 23 return 0;
438 }
439
440 11393 int ff_hevc_is_alpha_video(const HEVCContext *s)
441 {
442 11393 const HEVCVPS *vps = s->vps;
443 11393 int ret = 0;
444
445
3/4
✓ Branch 0 taken 1200 times.
✓ Branch 1 taken 10193 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1200 times.
11393 if (vps->nb_layers != 2 || !vps->layer_id_in_nuh[1])
446 10193 return 0;
447
448 /* decode_vps_ext() guarantees that SCALABILITY_AUXILIARY with AuxId other
449 * than alpha cannot reach here.
450 */
451 1200 ret = (s->vps->scalability_mask_flag & HEVC_SCALABILITY_AUXILIARY);
452
453
2/2
✓ Branch 0 taken 153 times.
✓ Branch 1 taken 1047 times.
1200 av_log(s->avctx, AV_LOG_DEBUG, "Multi layer video, %s alpha video\n",
454 ret ? "is" : "not");
455
456 1200 return ret;
457 }
458
459 237 int ff_hevc_requested_layers(const HEVCContext *s, const HEVCVPS *vps,
460 unsigned *active_output)
461 {
462 237 unsigned layers_active_output = 0, highest_layer;
463
464 // nothing requested - decode base layer only
465
2/2
✓ Branch 0 taken 226 times.
✓ Branch 1 taken 11 times.
237 if (!s->nb_view_ids) {
466 226 *active_output = 1;
467 226 return 1;
468 }
469
470
3/4
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 8 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 3 times.
11 if (s->nb_view_ids == 1 && s->view_ids[0] == -1) {
471 layers_active_output = (1 << vps->nb_layers) - 1;
472 } else {
473
2/2
✓ Branch 0 taken 19 times.
✓ Branch 1 taken 11 times.
30 for (int i = 0; i < s->nb_view_ids; i++) {
474 19 int view_id = s->view_ids[i];
475 19 int layer_idx = -1;
476
477
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 19 times.
19 if (view_id < 0) {
478 av_log(s->avctx, AV_LOG_ERROR,
479 "Invalid view ID requested: %d\n", view_id);
480 return AVERROR(EINVAL);
481 }
482
483
1/2
✓ Branch 0 taken 28 times.
✗ Branch 1 not taken.
28 for (int j = 0; j < vps->nb_layers; j++) {
484
2/2
✓ Branch 0 taken 19 times.
✓ Branch 1 taken 9 times.
28 if (vps->view_id[j] == view_id) {
485 19 layer_idx = j;
486 19 break;
487 }
488 }
489
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 19 times.
19 if (layer_idx < 0) {
490 av_log(s->avctx, AV_LOG_ERROR,
491 "View ID %d not present in VPS\n", view_id);
492 return AVERROR(EINVAL);
493 }
494 19 layers_active_output |= 1 << layer_idx;
495 }
496 }
497
498
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11 times.
11 if (!layers_active_output) {
499 av_log(s->avctx, AV_LOG_ERROR, "No layers selected\n");
500 return AVERROR_BUG;
501 }
502
503 11 highest_layer = ff_log2(layers_active_output);
504
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11 times.
11 if (highest_layer >= FF_ARRAY_ELEMS(s->layers)) {
505 av_log(s->avctx, AV_LOG_ERROR,
506 "Too many layers requested: %u\n", layers_active_output);
507 return AVERROR(EINVAL);
508 }
509
510 11 *active_output = layers_active_output;
511
512 /* Assume a higher layer depends on all the lower ones.
513 * This is enforced in VPS parsing currently, this logic will need
514 * to be changed if we want to support more complex dependency structures.
515 */
516 11 return highest_layer + 1;
517 }
518
519 238 static int setup_multilayer(HEVCContext *s, const HEVCVPS *vps)
520 {
521 unsigned layers_active_output;
522 int nb_decode_layers;
523
524 238 s->layers_active_output = 1;
525 238 s->layers_active_decode = 1;
526
527
2/2
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 237 times.
238 if (ff_hevc_is_alpha_video(s)) {
528 1 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(s->avctx->pix_fmt);
529
530
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!(desc->flags & AV_PIX_FMT_FLAG_ALPHA))
531 return 0;
532
533 1 s->layers_active_decode = (1 << vps->nb_layers) - 1;
534 1 s->layers_active_output = 1;
535
536 1 return 0;
537 }
538
539 237 nb_decode_layers = ff_hevc_requested_layers(s, vps, &layers_active_output);
540
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 237 times.
237 if (nb_decode_layers < 0)
541 return nb_decode_layers;
542
543 237 s->layers_active_decode = (1 << nb_decode_layers) - 1;
544 237 s->layers_active_output = layers_active_output;
545
546 237 av_log(s->avctx, AV_LOG_DEBUG, "decode/output layers: %x/%x\n",
547 s->layers_active_decode, s->layers_active_output);
548
549 237 return 0;
550 }
551
552 1 static enum AVPixelFormat map_to_alpha_format(HEVCContext *s,
553 enum AVPixelFormat pix_fmt)
554 {
555
1/9
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
1 switch (pix_fmt) {
556 1 case AV_PIX_FMT_YUV420P:
557 case AV_PIX_FMT_YUVJ420P:
558 1 return AV_PIX_FMT_YUVA420P;
559 case AV_PIX_FMT_YUV420P10:
560 return AV_PIX_FMT_YUVA420P10;
561 case AV_PIX_FMT_YUV444P:
562 return AV_PIX_FMT_YUVA444P;
563 case AV_PIX_FMT_YUV422P:
564 return AV_PIX_FMT_YUVA422P;
565 case AV_PIX_FMT_YUV422P10LE:
566 return AV_PIX_FMT_YUVA422P10LE;
567 case AV_PIX_FMT_YUV444P10:
568 return AV_PIX_FMT_YUVA444P10;
569 case AV_PIX_FMT_YUV444P12:
570 return AV_PIX_FMT_YUVA444P12;
571 case AV_PIX_FMT_YUV422P12:
572 return AV_PIX_FMT_YUVA422P12;
573 default:
574 av_log(s->avctx, AV_LOG_WARNING, "No alpha pixel format map for %s\n",
575 av_get_pix_fmt_name(pix_fmt));
576 return AV_PIX_FMT_NONE;
577 }
578 }
579
580 238 static enum AVPixelFormat get_format(HEVCContext *s, const HEVCSPS *sps)
581 {
582 #define HWACCEL_MAX (CONFIG_HEVC_DXVA2_HWACCEL + \
583 CONFIG_HEVC_D3D11VA_HWACCEL * 2 + \
584 CONFIG_HEVC_D3D12VA_HWACCEL + \
585 CONFIG_HEVC_NVDEC_HWACCEL + \
586 CONFIG_HEVC_NVDEC_CUARRAY_HWACCEL + \
587 CONFIG_HEVC_VAAPI_HWACCEL + \
588 CONFIG_HEVC_VIDEOTOOLBOX_HWACCEL + \
589 CONFIG_HEVC_VDPAU_HWACCEL + \
590 CONFIG_HEVC_VULKAN_HWACCEL)
591 238 enum AVPixelFormat pix_fmts[HWACCEL_MAX + 3], *fmt = pix_fmts;
592 238 enum AVPixelFormat alpha_fmt = AV_PIX_FMT_NONE;
593 int ret;
594
595
2/2
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 237 times.
238 if (ff_hevc_is_alpha_video(s))
596 1 alpha_fmt = map_to_alpha_format(s, sps->pix_fmt);
597
598
6/7
✓ Branch 0 taken 193 times.
✓ Branch 1 taken 29 times.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 7 times.
✓ Branch 4 taken 6 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 1 times.
238 switch (sps->pix_fmt) {
599 193 case AV_PIX_FMT_YUV420P:
600 case AV_PIX_FMT_YUVJ420P:
601 #if CONFIG_HEVC_DXVA2_HWACCEL
602 *fmt++ = AV_PIX_FMT_DXVA2_VLD;
603 #endif
604 #if CONFIG_HEVC_D3D11VA_HWACCEL
605 *fmt++ = AV_PIX_FMT_D3D11VA_VLD;
606 *fmt++ = AV_PIX_FMT_D3D11;
607 #endif
608 #if CONFIG_HEVC_D3D12VA_HWACCEL
609 *fmt++ = AV_PIX_FMT_D3D12;
610 #endif
611 #if CONFIG_HEVC_VAAPI_HWACCEL
612 193 *fmt++ = AV_PIX_FMT_VAAPI;
613 #endif
614 #if CONFIG_HEVC_VDPAU_HWACCEL
615 193 *fmt++ = AV_PIX_FMT_VDPAU;
616 #endif
617 #if CONFIG_HEVC_NVDEC_HWACCEL
618 *fmt++ = AV_PIX_FMT_CUDA;
619 #endif
620 #if CONFIG_HEVC_NVDEC_CUARRAY_HWACCEL
621 *fmt++ = AV_PIX_FMT_CUARRAY;
622 #endif
623 #if CONFIG_HEVC_VIDEOTOOLBOX_HWACCEL
624 *fmt++ = AV_PIX_FMT_VIDEOTOOLBOX;
625 #endif
626 #if CONFIG_HEVC_VULKAN_HWACCEL
627 *fmt++ = AV_PIX_FMT_VULKAN;
628 #endif
629 193 break;
630 29 case AV_PIX_FMT_YUV420P10:
631 #if CONFIG_HEVC_DXVA2_HWACCEL
632 *fmt++ = AV_PIX_FMT_DXVA2_VLD;
633 #endif
634 #if CONFIG_HEVC_D3D11VA_HWACCEL
635 *fmt++ = AV_PIX_FMT_D3D11VA_VLD;
636 *fmt++ = AV_PIX_FMT_D3D11;
637 #endif
638 #if CONFIG_HEVC_D3D12VA_HWACCEL
639 *fmt++ = AV_PIX_FMT_D3D12;
640 #endif
641 #if CONFIG_HEVC_VAAPI_HWACCEL
642 29 *fmt++ = AV_PIX_FMT_VAAPI;
643 #endif
644 #if CONFIG_HEVC_VIDEOTOOLBOX_HWACCEL
645 *fmt++ = AV_PIX_FMT_VIDEOTOOLBOX;
646 #endif
647 #if CONFIG_HEVC_VULKAN_HWACCEL
648 *fmt++ = AV_PIX_FMT_VULKAN;
649 #endif
650 #if CONFIG_HEVC_VDPAU_HWACCEL
651 29 *fmt++ = AV_PIX_FMT_VDPAU;
652 #endif
653 #if CONFIG_HEVC_NVDEC_HWACCEL
654 *fmt++ = AV_PIX_FMT_CUDA;
655 #endif
656 #if CONFIG_HEVC_NVDEC_CUARRAY_HWACCEL
657 *fmt++ = AV_PIX_FMT_CUARRAY;
658 #endif
659 29 break;
660 2 case AV_PIX_FMT_YUV444P:
661 #if CONFIG_HEVC_VAAPI_HWACCEL
662 2 *fmt++ = AV_PIX_FMT_VAAPI;
663 #endif
664 #if CONFIG_HEVC_VDPAU_HWACCEL
665 2 *fmt++ = AV_PIX_FMT_VDPAU;
666 #endif
667 #if CONFIG_HEVC_NVDEC_HWACCEL
668 *fmt++ = AV_PIX_FMT_CUDA;
669 #endif
670 #if CONFIG_HEVC_NVDEC_CUARRAY_HWACCEL
671 *fmt++ = AV_PIX_FMT_CUARRAY;
672 #endif
673 #if CONFIG_HEVC_VIDEOTOOLBOX_HWACCEL
674 *fmt++ = AV_PIX_FMT_VIDEOTOOLBOX;
675 #endif
676 #if CONFIG_HEVC_VULKAN_HWACCEL
677 *fmt++ = AV_PIX_FMT_VULKAN;
678 #endif
679 2 break;
680 7 case AV_PIX_FMT_YUV422P:
681 case AV_PIX_FMT_YUV422P10LE:
682 #if CONFIG_HEVC_VAAPI_HWACCEL
683 7 *fmt++ = AV_PIX_FMT_VAAPI;
684 #endif
685 #if CONFIG_HEVC_VIDEOTOOLBOX_HWACCEL
686 *fmt++ = AV_PIX_FMT_VIDEOTOOLBOX;
687 #endif
688 #if CONFIG_HEVC_VULKAN_HWACCEL
689 *fmt++ = AV_PIX_FMT_VULKAN;
690 #endif
691 #if CONFIG_HEVC_NVDEC_HWACCEL
692 *fmt++ = AV_PIX_FMT_CUDA;
693 #endif
694 #if CONFIG_HEVC_NVDEC_CUARRAY_HWACCEL
695 *fmt++ = AV_PIX_FMT_CUARRAY;
696 #endif
697 7 break;
698 6 case AV_PIX_FMT_YUV444P10:
699 #if CONFIG_HEVC_VIDEOTOOLBOX_HWACCEL
700 *fmt++ = AV_PIX_FMT_VIDEOTOOLBOX;
701 #endif
702 av_fallthrough;
703 case AV_PIX_FMT_YUV420P12:
704 case AV_PIX_FMT_YUV444P12:
705 #if CONFIG_HEVC_VAAPI_HWACCEL
706 6 *fmt++ = AV_PIX_FMT_VAAPI;
707 #endif
708 #if CONFIG_HEVC_VDPAU_HWACCEL
709 6 *fmt++ = AV_PIX_FMT_VDPAU;
710 #endif
711 #if CONFIG_HEVC_VULKAN_HWACCEL
712 *fmt++ = AV_PIX_FMT_VULKAN;
713 #endif
714 #if CONFIG_HEVC_NVDEC_HWACCEL
715 *fmt++ = AV_PIX_FMT_CUDA;
716 #endif
717 #if CONFIG_HEVC_NVDEC_CUARRAY_HWACCEL
718 *fmt++ = AV_PIX_FMT_CUARRAY;
719 #endif
720 6 break;
721 case AV_PIX_FMT_YUV422P12:
722 #if CONFIG_HEVC_VAAPI_HWACCEL
723 *fmt++ = AV_PIX_FMT_VAAPI;
724 #endif
725 #if CONFIG_HEVC_VULKAN_HWACCEL
726 *fmt++ = AV_PIX_FMT_VULKAN;
727 #endif
728 #if CONFIG_HEVC_NVDEC_HWACCEL
729 *fmt++ = AV_PIX_FMT_CUDA;
730 #endif
731 #if CONFIG_HEVC_NVDEC_CUARRAY_HWACCEL
732 *fmt++ = AV_PIX_FMT_CUARRAY;
733 #endif
734 break;
735 }
736
737
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 237 times.
238 if (alpha_fmt != AV_PIX_FMT_NONE)
738 1 *fmt++ = alpha_fmt;
739 238 *fmt++ = sps->pix_fmt;
740 238 *fmt = AV_PIX_FMT_NONE;
741
742 // export multilayer information from active VPS to the caller,
743 // so it is available in get_format()
744 238 ret = export_multilayer(s, sps->vps);
745
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 238 times.
238 if (ret < 0)
746 return ret;
747
748 238 ret = ff_get_format(s->avctx, pix_fmts);
749
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 238 times.
238 if (ret < 0)
750 return ret;
751 238 s->avctx->pix_fmt = ret;
752
753 // set up multilayer decoding, if requested by caller
754 238 ret = setup_multilayer(s, sps->vps);
755
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 238 times.
238 if (ret < 0)
756 return ret;
757
758 238 return 0;
759 }
760
761 249 static int set_sps(HEVCContext *s, HEVCLayerContext *l, const HEVCSPS *sps)
762 {
763 int ret;
764
765 249 pic_arrays_free(l);
766 249 av_refstruct_unref(&l->sps);
767 249 av_refstruct_unref(&s->vps);
768
769
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 249 times.
249 if (!sps)
770 return 0;
771
772 249 ret = pic_arrays_init(l, sps);
773
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 249 times.
249 if (ret < 0)
774 goto fail;
775
776 249 ff_hevc_pred_init(&s->hpc, sps->bit_depth);
777 249 ff_hevc_dsp_init (&s->hevcdsp, sps->bit_depth);
778 249 ff_videodsp_init (&s->vdsp, sps->bit_depth);
779
780 249 l->sps = av_refstruct_ref_c(sps);
781 249 s->vps = av_refstruct_ref_c(sps->vps);
782
783 249 return 0;
784
785 fail:
786 pic_arrays_free(l);
787 av_refstruct_unref(&l->sps);
788 return ret;
789 }
790
791 28542 static int hls_slice_header(SliceHeader *sh, const HEVCContext *s, GetBitContext *gb)
792 {
793 const HEVCPPS *pps;
794 const HEVCSPS *sps;
795 const HEVCVPS *vps;
796 unsigned pps_id, layer_idx;
797 int i, ret;
798
799 // Coded parameters
800 28542 sh->first_slice_in_pic_flag = get_bits1(gb);
801
802 28542 sh->no_output_of_prior_pics_flag = 0;
803
3/4
✓ Branch 0 taken 1056 times.
✓ Branch 1 taken 27486 times.
✓ Branch 2 taken 1056 times.
✗ Branch 3 not taken.
28542 if (IS_IRAP(s))
804 1056 sh->no_output_of_prior_pics_flag = get_bits1(gb);
805
806 28542 pps_id = get_ue_golomb_long(gb);
807
2/4
✓ Branch 0 taken 28542 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 28542 times.
28542 if (pps_id >= HEVC_MAX_PPS_COUNT || !s->ps.pps_list[pps_id]) {
808 av_log(s->avctx, AV_LOG_ERROR, "PPS id out of range: %d\n", pps_id);
809 return AVERROR_INVALIDDATA;
810 }
811
3/4
✓ Branch 0 taken 17996 times.
✓ Branch 1 taken 10546 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 17996 times.
28542 if (!sh->first_slice_in_pic_flag && s->ps.pps_list[pps_id] != s->pps) {
812 av_log(s->avctx, AV_LOG_ERROR, "PPS changed between slices.\n");
813 return AVERROR_INVALIDDATA;
814 }
815 28542 sh->pps_id = pps_id;
816
817 28542 pps = s->ps.pps_list[pps_id];
818 28542 sps = pps->sps;
819 28542 vps = sps->vps;
820 28542 layer_idx = vps->layer_idx[s->nuh_layer_id];
821
822
4/4
✓ Branch 0 taken 502 times.
✓ Branch 1 taken 28040 times.
✓ Branch 2 taken 11 times.
✓ Branch 3 taken 491 times.
28542 if (s->nal_unit_type == HEVC_NAL_CRA_NUT && s->last_eos == 1)
823 11 sh->no_output_of_prior_pics_flag = 1;
824
825 28542 sh->dependent_slice_segment_flag = 0;
826
2/2
✓ Branch 0 taken 17996 times.
✓ Branch 1 taken 10546 times.
28542 if (!sh->first_slice_in_pic_flag) {
827 int slice_address_length;
828
829
2/2
✓ Branch 0 taken 8936 times.
✓ Branch 1 taken 9060 times.
17996 if (pps->dependent_slice_segments_enabled_flag)
830 8936 sh->dependent_slice_segment_flag = get_bits1(gb);
831
3/4
✓ Branch 0 taken 7794 times.
✓ Branch 1 taken 10202 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 7794 times.
17996 if (sh->dependent_slice_segment_flag && !s->slice_initialized) {
832 av_log(s->avctx, AV_LOG_ERROR, "Independent slice segment missing.\n");
833 return AVERROR_INVALIDDATA;
834 }
835
836 17996 slice_address_length = av_ceil_log2(sps->ctb_width *
837 17996 sps->ctb_height);
838 17996 sh->slice_segment_addr = get_bitsz(gb, slice_address_length);
839
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 17996 times.
17996 if (sh->slice_segment_addr >= sps->ctb_width * sps->ctb_height) {
840 av_log(s->avctx, AV_LOG_ERROR,
841 "Invalid slice segment address: %u.\n",
842 sh->slice_segment_addr);
843 return AVERROR_INVALIDDATA;
844 }
845
846
2/2
✓ Branch 0 taken 10202 times.
✓ Branch 1 taken 7794 times.
17996 if (!sh->dependent_slice_segment_flag) {
847 10202 sh->slice_addr = sh->slice_segment_addr;
848 }
849 } else {
850 10546 sh->slice_segment_addr = sh->slice_addr = 0;
851 }
852
853
2/2
✓ Branch 0 taken 20748 times.
✓ Branch 1 taken 7794 times.
28542 if (!sh->dependent_slice_segment_flag) {
854
2/2
✓ Branch 0 taken 1823 times.
✓ Branch 1 taken 20748 times.
22571 for (i = 0; i < pps->num_extra_slice_header_bits; i++)
855 1823 skip_bits(gb, 1); // slice_reserved_undetermined_flag[]
856
857 20748 sh->slice_type = get_ue_golomb_long(gb);
858
2/2
✓ Branch 0 taken 19749 times.
✓ Branch 1 taken 999 times.
20748 if (!(sh->slice_type == HEVC_SLICE_I ||
859
2/2
✓ Branch 0 taken 15416 times.
✓ Branch 1 taken 4333 times.
19749 sh->slice_type == HEVC_SLICE_P ||
860
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 15416 times.
15416 sh->slice_type == HEVC_SLICE_B)) {
861 av_log(s->avctx, AV_LOG_ERROR, "Unknown slice type: %d.\n",
862 sh->slice_type);
863 return AVERROR_INVALIDDATA;
864 }
865
5/6
✓ Branch 0 taken 820 times.
✓ Branch 1 taken 19928 times.
✓ Branch 2 taken 820 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 81 times.
✓ Branch 5 taken 739 times.
20748 if (IS_IRAP(s) && sh->slice_type != HEVC_SLICE_I &&
866
1/2
✓ Branch 0 taken 81 times.
✗ Branch 1 not taken.
81 !pps->pps_curr_pic_ref_enabled_flag &&
867
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 81 times.
81 s->nuh_layer_id == 0) {
868 av_log(s->avctx, AV_LOG_ERROR, "Inter slices in an IRAP frame.\n");
869 return AVERROR_INVALIDDATA;
870 }
871
872 // when flag is not present, picture is inferred to be output
873 20748 sh->pic_output_flag = 1;
874
2/2
✓ Branch 0 taken 700 times.
✓ Branch 1 taken 20048 times.
20748 if (pps->output_flag_present_flag)
875 700 sh->pic_output_flag = get_bits1(gb);
876
877
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 20748 times.
20748 if (sps->separate_colour_plane)
878 sh->colour_plane_id = get_bits(gb, 2);
879
880
4/4
✓ Branch 0 taken 20401 times.
✓ Branch 1 taken 347 times.
✓ Branch 2 taken 36 times.
✓ Branch 3 taken 20365 times.
20748 if (!IS_IDR(s) ||
881
2/2
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 365 times.
383 (s->nuh_layer_id > 0 &&
882
2/2
✓ Branch 0 taken 13 times.
✓ Branch 1 taken 5 times.
18 !(vps->poc_lsb_not_present & (1 << layer_idx)))) {
883 int poc;
884
885 20378 sh->pic_order_cnt_lsb = get_bits(gb, sps->log2_max_poc_lsb);
886 20378 poc = ff_hevc_compute_poc(sps, s->poc_tid0, sh->pic_order_cnt_lsb, s->nal_unit_type);
887
3/4
✓ Branch 0 taken 10073 times.
✓ Branch 1 taken 10305 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 10073 times.
20378 if (!sh->first_slice_in_pic_flag && poc != sh->poc) {
888 av_log(s->avctx, AV_LOG_WARNING,
889 "Ignoring POC change between slices: %d -> %d\n", poc, sh->poc);
890 if (s->avctx->err_recognition & AV_EF_EXPLODE)
891 return AVERROR_INVALIDDATA;
892 poc = sh->poc;
893 }
894 20378 sh->poc = poc;
895 }
896
897
4/4
✓ Branch 0 taken 20401 times.
✓ Branch 1 taken 347 times.
✓ Branch 2 taken 20365 times.
✓ Branch 3 taken 36 times.
41113 if (!IS_IDR(s)) {
898 int pos;
899
900 20365 sh->short_term_ref_pic_set_sps_flag = get_bits1(gb);
901 20365 pos = get_bits_left(gb);
902
2/2
✓ Branch 0 taken 3982 times.
✓ Branch 1 taken 16383 times.
20365 if (!sh->short_term_ref_pic_set_sps_flag) {
903 3982 ret = ff_hevc_decode_short_term_rps(gb, s->avctx, &sh->slice_rps, sps, 1);
904
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3982 times.
3982 if (ret < 0)
905 return ret;
906
907 3982 sh->short_term_rps = &sh->slice_rps;
908 } else {
909 int numbits, rps_idx;
910
911
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 16383 times.
16383 if (!sps->nb_st_rps) {
912 av_log(s->avctx, AV_LOG_ERROR, "No ref lists in the SPS.\n");
913 return AVERROR_INVALIDDATA;
914 }
915
916 16383 numbits = av_ceil_log2(sps->nb_st_rps);
917
2/2
✓ Branch 0 taken 16355 times.
✓ Branch 1 taken 28 times.
16383 rps_idx = numbits > 0 ? get_bits(gb, numbits) : 0;
918 16383 sh->short_term_rps = &sps->st_rps[rps_idx];
919 }
920 20365 sh->short_term_ref_pic_set_size = pos - get_bits_left(gb);
921
922 20365 pos = get_bits_left(gb);
923 20365 ret = decode_lt_rps(sps, &sh->long_term_rps, gb, sh->poc, sh->pic_order_cnt_lsb);
924
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 20365 times.
20365 if (ret < 0) {
925 av_log(s->avctx, AV_LOG_WARNING, "Invalid long term RPS.\n");
926 if (s->avctx->err_recognition & AV_EF_EXPLODE)
927 return AVERROR_INVALIDDATA;
928 }
929 20365 sh->long_term_ref_pic_set_size = pos - get_bits_left(gb);
930
931
2/2
✓ Branch 0 taken 17921 times.
✓ Branch 1 taken 2444 times.
20365 if (sps->temporal_mvp_enabled)
932 17921 sh->slice_temporal_mvp_enabled_flag = get_bits1(gb);
933 else
934 2444 sh->slice_temporal_mvp_enabled_flag = 0;
935 } else {
936 383 sh->poc = 0;
937 383 sh->pic_order_cnt_lsb = 0;
938 383 sh->short_term_ref_pic_set_sps_flag = 0;
939 383 sh->short_term_ref_pic_set_size = 0;
940 383 sh->short_term_rps = NULL;
941 383 sh->long_term_ref_pic_set_size = 0;
942 383 sh->long_term_rps.nb_refs = 0;
943 383 sh->slice_temporal_mvp_enabled_flag = 0;
944 }
945
946 20748 sh->inter_layer_pred = 0;
947
2/2
✓ Branch 0 taken 516 times.
✓ Branch 1 taken 20232 times.
20748 if (s->nuh_layer_id > 0) {
948 516 int num_direct_ref_layers = vps->num_direct_ref_layers[layer_idx];
949
950
2/2
✓ Branch 0 taken 418 times.
✓ Branch 1 taken 98 times.
516 if (vps->default_ref_layers_active)
951 418 sh->inter_layer_pred = !!num_direct_ref_layers;
952
2/2
✓ Branch 0 taken 48 times.
✓ Branch 1 taken 50 times.
98 else if (num_direct_ref_layers) {
953 48 sh->inter_layer_pred = get_bits1(gb);
954
955
3/4
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 45 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 3 times.
48 if (sh->inter_layer_pred && num_direct_ref_layers > 1) {
956 av_log(s->avctx, AV_LOG_ERROR,
957 "NumDirectRefLayers>1 not supported\n");
958 return AVERROR_PATCHWELCOME;
959 }
960 }
961 }
962
963
2/2
✓ Branch 0 taken 17307 times.
✓ Branch 1 taken 3441 times.
20748 if (sps->sao_enabled) {
964 17307 sh->slice_sample_adaptive_offset_flag[0] = get_bits1(gb);
965
2/2
✓ Branch 0 taken 17306 times.
✓ Branch 1 taken 1 times.
17307 if (sps->chroma_format_idc) {
966 17306 sh->slice_sample_adaptive_offset_flag[1] =
967 17306 sh->slice_sample_adaptive_offset_flag[2] = get_bits1(gb);
968 }
969 } else {
970 3441 sh->slice_sample_adaptive_offset_flag[0] = 0;
971 3441 sh->slice_sample_adaptive_offset_flag[1] = 0;
972 3441 sh->slice_sample_adaptive_offset_flag[2] = 0;
973 }
974
975 20748 sh->nb_refs[L0] = sh->nb_refs[L1] = 0;
976
4/4
✓ Branch 0 taken 16415 times.
✓ Branch 1 taken 4333 times.
✓ Branch 2 taken 15416 times.
✓ Branch 3 taken 999 times.
20748 if (sh->slice_type == HEVC_SLICE_P || sh->slice_type == HEVC_SLICE_B) {
977 int nb_refs;
978
979 19749 sh->nb_refs[L0] = pps->num_ref_idx_l0_default_active;
980
2/2
✓ Branch 0 taken 15416 times.
✓ Branch 1 taken 4333 times.
19749 if (sh->slice_type == HEVC_SLICE_B)
981 15416 sh->nb_refs[L1] = pps->num_ref_idx_l1_default_active;
982
983
2/2
✓ Branch 1 taken 5438 times.
✓ Branch 2 taken 14311 times.
19749 if (get_bits1(gb)) { // num_ref_idx_active_override_flag
984 5438 sh->nb_refs[L0] = get_ue_golomb_31(gb) + 1;
985
2/2
✓ Branch 0 taken 2870 times.
✓ Branch 1 taken 2568 times.
5438 if (sh->slice_type == HEVC_SLICE_B)
986 2870 sh->nb_refs[L1] = get_ue_golomb_31(gb) + 1;
987 }
988
2/4
✓ Branch 0 taken 19749 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 19749 times.
19749 if (sh->nb_refs[L0] >= HEVC_MAX_REFS || sh->nb_refs[L1] >= HEVC_MAX_REFS) {
989 av_log(s->avctx, AV_LOG_ERROR, "Too many refs: %d/%d.\n",
990 sh->nb_refs[L0], sh->nb_refs[L1]);
991 return AVERROR_INVALIDDATA;
992 }
993
994 19749 sh->rpl_modification_flag[0] = 0;
995 19749 sh->rpl_modification_flag[1] = 0;
996 19749 nb_refs = ff_hevc_frame_nb_refs(sh, pps, layer_idx);
997
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 19749 times.
19749 if (!nb_refs) {
998 av_log(s->avctx, AV_LOG_ERROR, "Zero refs for a frame with P or B slices.\n");
999 return AVERROR_INVALIDDATA;
1000 }
1001
1002
4/4
✓ Branch 0 taken 2311 times.
✓ Branch 1 taken 17438 times.
✓ Branch 2 taken 1986 times.
✓ Branch 3 taken 325 times.
19749 if (pps->lists_modification_present_flag && nb_refs > 1) {
1003 1986 sh->rpl_modification_flag[0] = get_bits1(gb);
1004
2/2
✓ Branch 0 taken 1432 times.
✓ Branch 1 taken 554 times.
1986 if (sh->rpl_modification_flag[0]) {
1005
2/2
✓ Branch 0 taken 3366 times.
✓ Branch 1 taken 1432 times.
4798 for (i = 0; i < sh->nb_refs[L0]; i++)
1006 3366 sh->list_entry_lx[0][i] = get_bits(gb, av_ceil_log2(nb_refs));
1007 }
1008
1009
2/2
✓ Branch 0 taken 1803 times.
✓ Branch 1 taken 183 times.
1986 if (sh->slice_type == HEVC_SLICE_B) {
1010 1803 sh->rpl_modification_flag[1] = get_bits1(gb);
1011
2/2
✓ Branch 0 taken 655 times.
✓ Branch 1 taken 1148 times.
1803 if (sh->rpl_modification_flag[1] == 1)
1012
2/2
✓ Branch 0 taken 1558 times.
✓ Branch 1 taken 655 times.
2213 for (i = 0; i < sh->nb_refs[L1]; i++)
1013 1558 sh->list_entry_lx[1][i] = get_bits(gb, av_ceil_log2(nb_refs));
1014 }
1015 }
1016
1017
2/2
✓ Branch 0 taken 15416 times.
✓ Branch 1 taken 4333 times.
19749 if (sh->slice_type == HEVC_SLICE_B)
1018 15416 sh->mvd_l1_zero_flag = get_bits1(gb);
1019
1020
2/2
✓ Branch 0 taken 16800 times.
✓ Branch 1 taken 2949 times.
19749 if (pps->cabac_init_present_flag)
1021 16800 sh->cabac_init_flag = get_bits1(gb);
1022 else
1023 2949 sh->cabac_init_flag = 0;
1024
1025 19749 sh->collocated_ref_idx = 0;
1026
2/2
✓ Branch 0 taken 17255 times.
✓ Branch 1 taken 2494 times.
19749 if (sh->slice_temporal_mvp_enabled_flag) {
1027 17255 sh->collocated_list = L0;
1028
2/2
✓ Branch 0 taken 14870 times.
✓ Branch 1 taken 2385 times.
17255 if (sh->slice_type == HEVC_SLICE_B)
1029 14870 sh->collocated_list = !get_bits1(gb);
1030
1031
2/2
✓ Branch 0 taken 15172 times.
✓ Branch 1 taken 2083 times.
17255 if (sh->nb_refs[sh->collocated_list] > 1) {
1032 15172 sh->collocated_ref_idx = get_ue_golomb_long(gb);
1033
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 15172 times.
15172 if (sh->collocated_ref_idx >= sh->nb_refs[sh->collocated_list]) {
1034 av_log(s->avctx, AV_LOG_ERROR,
1035 "Invalid collocated_ref_idx: %d.\n",
1036 sh->collocated_ref_idx);
1037 return AVERROR_INVALIDDATA;
1038 }
1039 }
1040 }
1041
1042
4/4
✓ Branch 0 taken 1559 times.
✓ Branch 1 taken 18190 times.
✓ Branch 2 taken 754 times.
✓ Branch 3 taken 805 times.
19749 if ((pps->weighted_pred_flag && sh->slice_type == HEVC_SLICE_P) ||
1043
3/4
✓ Branch 0 taken 696 times.
✓ Branch 1 taken 18248 times.
✓ Branch 2 taken 696 times.
✗ Branch 3 not taken.
18944 (pps->weighted_bipred_flag && sh->slice_type == HEVC_SLICE_B)) {
1044 1501 ret = pred_weight_table(sh, s->avctx, sps, gb);
1045
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1501 times.
1501 if (ret < 0)
1046 return ret;
1047 }
1048
1049 19749 sh->max_num_merge_cand = 5 - get_ue_golomb_long(gb);
1050
2/4
✓ Branch 0 taken 19749 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 19749 times.
19749 if (sh->max_num_merge_cand < 1 || sh->max_num_merge_cand > 5) {
1051 av_log(s->avctx, AV_LOG_ERROR,
1052 "Invalid number of merging MVP candidates: %d.\n",
1053 sh->max_num_merge_cand);
1054 return AVERROR_INVALIDDATA;
1055 }
1056
1057 // Syntax in 7.3.6.1
1058
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 19749 times.
19749 if (sps->motion_vector_resolution_control_idc == 2)
1059 sh->use_integer_mv_flag = get_bits1(gb);
1060 else
1061 // Inferred to be equal to motion_vector_resolution_control_idc if not present
1062 19749 sh->use_integer_mv_flag = sps->motion_vector_resolution_control_idc;
1063
1064 }
1065
1066 20748 sh->slice_qp_delta = get_se_golomb(gb);
1067
1068
2/2
✓ Branch 0 taken 260 times.
✓ Branch 1 taken 20488 times.
20748 if (pps->pic_slice_level_chroma_qp_offsets_present_flag) {
1069 260 sh->slice_cb_qp_offset = get_se_golomb(gb);
1070 260 sh->slice_cr_qp_offset = get_se_golomb(gb);
1071
2/4
✓ Branch 0 taken 260 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 260 times.
✗ Branch 3 not taken.
260 if (sh->slice_cb_qp_offset < -12 || sh->slice_cb_qp_offset > 12 ||
1072
2/4
✓ Branch 0 taken 260 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 260 times.
260 sh->slice_cr_qp_offset < -12 || sh->slice_cr_qp_offset > 12) {
1073 av_log(s->avctx, AV_LOG_ERROR, "Invalid slice cx qp offset.\n");
1074 return AVERROR_INVALIDDATA;
1075 }
1076 } else {
1077 20488 sh->slice_cb_qp_offset = 0;
1078 20488 sh->slice_cr_qp_offset = 0;
1079 }
1080
1081
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 20748 times.
20748 if (pps->pps_slice_act_qp_offsets_present_flag) {
1082 sh->slice_act_y_qp_offset = get_se_golomb(gb);
1083 sh->slice_act_cb_qp_offset = get_se_golomb(gb);
1084 sh->slice_act_cr_qp_offset = get_se_golomb(gb);
1085 }
1086
1087
2/2
✓ Branch 0 taken 41 times.
✓ Branch 1 taken 20707 times.
20748 if (pps->chroma_qp_offset_list_enabled_flag)
1088 41 sh->cu_chroma_qp_offset_enabled_flag = get_bits1(gb);
1089 else
1090 20707 sh->cu_chroma_qp_offset_enabled_flag = 0;
1091
1092
2/2
✓ Branch 0 taken 3397 times.
✓ Branch 1 taken 17351 times.
20748 if (pps->deblocking_filter_control_present_flag) {
1093 3397 int deblocking_filter_override_flag = 0;
1094
1095
2/2
✓ Branch 0 taken 1097 times.
✓ Branch 1 taken 2300 times.
3397 if (pps->deblocking_filter_override_enabled_flag)
1096 1097 deblocking_filter_override_flag = get_bits1(gb);
1097
1098
2/2
✓ Branch 0 taken 506 times.
✓ Branch 1 taken 2891 times.
3397 if (deblocking_filter_override_flag) {
1099 506 sh->disable_deblocking_filter_flag = get_bits1(gb);
1100
2/2
✓ Branch 0 taken 349 times.
✓ Branch 1 taken 157 times.
506 if (!sh->disable_deblocking_filter_flag) {
1101 349 int beta_offset_div2 = get_se_golomb(gb);
1102 349 int tc_offset_div2 = get_se_golomb(gb) ;
1103
3/6
✓ Branch 0 taken 349 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 349 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 349 times.
✗ Branch 5 not taken.
349 if (beta_offset_div2 < -6 || beta_offset_div2 > 6 ||
1104
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 349 times.
349 tc_offset_div2 < -6 || tc_offset_div2 > 6) {
1105 av_log(s->avctx, AV_LOG_ERROR,
1106 "Invalid deblock filter offsets: %d, %d\n",
1107 beta_offset_div2, tc_offset_div2);
1108 return AVERROR_INVALIDDATA;
1109 }
1110 349 sh->beta_offset = beta_offset_div2 * 2;
1111 349 sh->tc_offset = tc_offset_div2 * 2;
1112 }
1113 } else {
1114 2891 sh->disable_deblocking_filter_flag = pps->disable_dbf;
1115 2891 sh->beta_offset = pps->beta_offset;
1116 2891 sh->tc_offset = pps->tc_offset;
1117 }
1118 } else {
1119 17351 sh->disable_deblocking_filter_flag = 0;
1120 17351 sh->beta_offset = 0;
1121 17351 sh->tc_offset = 0;
1122 }
1123
1124
2/2
✓ Branch 0 taken 18156 times.
✓ Branch 1 taken 2592 times.
20748 if (pps->seq_loop_filter_across_slices_enabled_flag &&
1125
2/2
✓ Branch 0 taken 11758 times.
✓ Branch 1 taken 6398 times.
18156 (sh->slice_sample_adaptive_offset_flag[0] ||
1126
2/2
✓ Branch 0 taken 11741 times.
✓ Branch 1 taken 17 times.
11758 sh->slice_sample_adaptive_offset_flag[1] ||
1127
2/2
✓ Branch 0 taken 11641 times.
✓ Branch 1 taken 100 times.
11741 !sh->disable_deblocking_filter_flag)) {
1128 18056 sh->slice_loop_filter_across_slices_enabled_flag = get_bits1(gb);
1129 } else {
1130 2692 sh->slice_loop_filter_across_slices_enabled_flag = pps->seq_loop_filter_across_slices_enabled_flag;
1131 }
1132 }
1133
1134 28542 sh->num_entry_point_offsets = 0;
1135
4/4
✓ Branch 0 taken 25399 times.
✓ Branch 1 taken 3143 times.
✓ Branch 2 taken 4834 times.
✓ Branch 3 taken 20565 times.
28542 if (pps->tiles_enabled_flag || pps->entropy_coding_sync_enabled_flag) {
1136 7977 unsigned num_entry_point_offsets = get_ue_golomb_long(gb);
1137 // It would be possible to bound this tighter but this here is simpler
1138
2/4
✓ Branch 1 taken 7977 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 7977 times.
7977 if (num_entry_point_offsets > get_bits_left(gb) || num_entry_point_offsets > UINT16_MAX) {
1139 av_log(s->avctx, AV_LOG_ERROR, "num_entry_point_offsets %d is invalid\n", num_entry_point_offsets);
1140 return AVERROR_INVALIDDATA;
1141 }
1142
1143 7977 sh->num_entry_point_offsets = num_entry_point_offsets;
1144
2/2
✓ Branch 0 taken 2226 times.
✓ Branch 1 taken 5751 times.
7977 if (sh->num_entry_point_offsets > 0) {
1145 2226 int offset_len = get_ue_golomb_long(gb) + 1;
1146
1147
2/4
✓ Branch 0 taken 2226 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 2226 times.
2226 if (offset_len < 1 || offset_len > 32) {
1148 sh->num_entry_point_offsets = 0;
1149 av_log(s->avctx, AV_LOG_ERROR, "offset_len %d is invalid\n", offset_len);
1150 return AVERROR_INVALIDDATA;
1151 }
1152
1153 2226 av_freep(&sh->entry_point_offset);
1154 2226 av_freep(&sh->offset);
1155 2226 av_freep(&sh->size);
1156 2226 sh->entry_point_offset = av_malloc_array(sh->num_entry_point_offsets, sizeof(unsigned));
1157 2226 sh->offset = av_malloc_array(sh->num_entry_point_offsets + 1, sizeof(int));
1158 2226 sh->size = av_malloc_array(sh->num_entry_point_offsets + 1, sizeof(int));
1159
3/6
✓ Branch 0 taken 2226 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2226 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 2226 times.
2226 if (!sh->entry_point_offset || !sh->offset || !sh->size) {
1160 sh->num_entry_point_offsets = 0;
1161 av_log(s->avctx, AV_LOG_ERROR, "Failed to allocate memory\n");
1162 return AVERROR(ENOMEM);
1163 }
1164
2/2
✓ Branch 0 taken 9979 times.
✓ Branch 1 taken 2226 times.
12205 for (i = 0; i < sh->num_entry_point_offsets; i++) {
1165 9979 unsigned val = get_bits_long(gb, offset_len);
1166 9979 sh->entry_point_offset[i] = val + 1; // +1; // +1 to get the size
1167 }
1168 }
1169 }
1170
1171
2/2
✓ Branch 0 taken 2749 times.
✓ Branch 1 taken 25793 times.
28542 if (pps->slice_header_extension_present_flag) {
1172 2749 unsigned int length = get_ue_golomb_long(gb);
1173
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2749 times.
2749 if (length*8LL > get_bits_left(gb)) {
1174 av_log(s->avctx, AV_LOG_ERROR, "too many slice_header_extension_data_bytes\n");
1175 return AVERROR_INVALIDDATA;
1176 }
1177
2/2
✓ Branch 0 taken 19903 times.
✓ Branch 1 taken 2749 times.
22652 for (i = 0; i < length; i++)
1178 19903 skip_bits(gb, 8); // slice_header_extension_data_byte
1179 }
1180
1181 28542 ret = get_bits1(gb);
1182
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 28542 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
28542 if (!ret && get_bits_left(gb) >= 0) {
1183 av_log(s->avctx, AV_LOG_ERROR, "alignment_bit_equal_to_one=0\n");
1184 return AVERROR_INVALIDDATA;
1185 }
1186 28542 sh->data_offset = align_get_bits(gb) - gb->buffer;
1187
1188
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 28542 times.
28542 if (get_bits_left(gb) < 0) {
1189 av_log(s->avctx, AV_LOG_ERROR,
1190 "Overread slice header by %d bits\n", -get_bits_left(gb));
1191 return AVERROR_INVALIDDATA;
1192 }
1193
1194 // Inferred parameters
1195 28542 sh->slice_qp = 26U + pps->pic_init_qp_minus26 + sh->slice_qp_delta;
1196
1/2
✓ Branch 0 taken 28542 times.
✗ Branch 1 not taken.
28542 if (sh->slice_qp > 51 ||
1197
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 28542 times.
28542 sh->slice_qp < -sps->qp_bd_offset) {
1198 av_log(s->avctx, AV_LOG_ERROR,
1199 "The slice_qp %d is outside the valid range "
1200 "[%d, 51].\n",
1201 sh->slice_qp,
1202 -sps->qp_bd_offset);
1203 return AVERROR_INVALIDDATA;
1204 }
1205
1206 28542 sh->slice_ctb_addr_rs = sh->slice_segment_addr;
1207
1208
2/2
✓ Branch 0 taken 7794 times.
✓ Branch 1 taken 20748 times.
28542 if (sh->dependent_slice_segment_flag &&
1209
2/4
✓ Branch 0 taken 7794 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 7794 times.
7794 (!sh->slice_ctb_addr_rs || !pps->ctb_addr_rs_to_ts[sh->slice_ctb_addr_rs])) {
1210 av_log(s->avctx, AV_LOG_ERROR, "Impossible slice segment.\n");
1211 return AVERROR_INVALIDDATA;
1212 }
1213
1214 28542 return 0;
1215 }
1216
1217 #define CTB(tab, x, y) ((tab)[(y) * sps->ctb_width + (x)])
1218
1219 #define SET_SAO(elem, value) \
1220 do { \
1221 if (!sao_merge_up_flag && !sao_merge_left_flag) \
1222 sao->elem = value; \
1223 else if (sao_merge_left_flag) \
1224 sao->elem = CTB(l->sao, rx-1, ry).elem; \
1225 else if (sao_merge_up_flag) \
1226 sao->elem = CTB(l->sao, rx, ry-1).elem; \
1227 else \
1228 sao->elem = 0; \
1229 } while (0)
1230
1231 1941354 static void hls_sao_param(HEVCLocalContext *lc, const HEVCLayerContext *l,
1232 const HEVCPPS *pps, const HEVCSPS *sps,
1233 int rx, int ry)
1234 {
1235 1941354 const HEVCContext *const s = lc->parent;
1236 1941354 int sao_merge_left_flag = 0;
1237 1941354 int sao_merge_up_flag = 0;
1238 1941354 SAOParams *sao = &CTB(l->sao, rx, ry);
1239 int c_idx, i;
1240
1241
2/2
✓ Branch 0 taken 860644 times.
✓ Branch 1 taken 1080710 times.
1941354 if (s->sh.slice_sample_adaptive_offset_flag[0] ||
1242
2/2
✓ Branch 0 taken 1540 times.
✓ Branch 1 taken 859104 times.
860644 s->sh.slice_sample_adaptive_offset_flag[1]) {
1243
2/2
✓ Branch 0 taken 1040482 times.
✓ Branch 1 taken 41768 times.
1082250 if (rx > 0) {
1244
2/2
✓ Branch 0 taken 1021463 times.
✓ Branch 1 taken 19019 times.
1040482 if (lc->ctb_left_flag)
1245 1021463 sao_merge_left_flag = ff_hevc_sao_merge_flag_decode(lc);
1246 }
1247
4/4
✓ Branch 0 taken 1016391 times.
✓ Branch 1 taken 65859 times.
✓ Branch 2 taken 421766 times.
✓ Branch 3 taken 594625 times.
1082250 if (ry > 0 && !sao_merge_left_flag) {
1248
2/2
✓ Branch 0 taken 408759 times.
✓ Branch 1 taken 13007 times.
421766 if (lc->ctb_up_flag)
1249 408759 sao_merge_up_flag = ff_hevc_sao_merge_flag_decode(lc);
1250 }
1251 }
1252
1253
4/4
✓ Branch 0 taken 7765320 times.
✓ Branch 1 taken 48 times.
✓ Branch 2 taken 5824014 times.
✓ Branch 3 taken 1941354 times.
7765368 for (c_idx = 0; c_idx < (sps->chroma_format_idc ? 3 : 1); c_idx++) {
1254
2/2
✓ Branch 0 taken 1941354 times.
✓ Branch 1 taken 3882660 times.
5824014 int log2_sao_offset_scale = c_idx == 0 ? pps->log2_sao_offset_scale_luma :
1255 3882660 pps->log2_sao_offset_scale_chroma;
1256
1257
2/2
✓ Branch 0 taken 3127748 times.
✓ Branch 1 taken 2696266 times.
5824014 if (!s->sh.slice_sample_adaptive_offset_flag[c_idx]) {
1258 3127748 sao->type_idx[c_idx] = SAO_NOT_APPLIED;
1259 3127748 continue;
1260 }
1261
1262
2/2
✓ Branch 0 taken 807778 times.
✓ Branch 1 taken 1888488 times.
2696266 if (c_idx == 2) {
1263 807778 sao->type_idx[2] = sao->type_idx[1];
1264 807778 sao->eo_class[2] = sao->eo_class[1];
1265 } else {
1266
7/8
✓ Branch 0 taken 1649043 times.
✓ Branch 1 taken 239445 times.
✓ Branch 2 taken 561205 times.
✓ Branch 3 taken 1087838 times.
✓ Branch 5 taken 1087838 times.
✓ Branch 6 taken 239445 times.
✓ Branch 7 taken 239445 times.
✗ Branch 8 not taken.
1888488 SET_SAO(type_idx[c_idx], ff_hevc_sao_type_idx_decode(lc));
1267 }
1268
1269
2/2
✓ Branch 0 taken 2009626 times.
✓ Branch 1 taken 686640 times.
2696266 if (sao->type_idx[c_idx] == SAO_NOT_APPLIED)
1270 2009626 continue;
1271
1272
2/2
✓ Branch 0 taken 2746560 times.
✓ Branch 1 taken 686640 times.
3433200 for (i = 0; i < 4; i++)
1273
7/8
✓ Branch 0 taken 2251500 times.
✓ Branch 1 taken 495060 times.
✓ Branch 2 taken 875640 times.
✓ Branch 3 taken 1375860 times.
✓ Branch 5 taken 1375860 times.
✓ Branch 6 taken 495060 times.
✓ Branch 7 taken 495060 times.
✗ Branch 8 not taken.
2746560 SET_SAO(offset_abs[c_idx][i], ff_hevc_sao_offset_abs_decode(lc, sps->bit_depth));
1274
1275
2/2
✓ Branch 0 taken 108424 times.
✓ Branch 1 taken 578216 times.
686640 if (sao->type_idx[c_idx] == SAO_BAND) {
1276
2/2
✓ Branch 0 taken 433696 times.
✓ Branch 1 taken 108424 times.
542120 for (i = 0; i < 4; i++) {
1277
2/2
✓ Branch 0 taken 323582 times.
✓ Branch 1 taken 110114 times.
433696 if (sao->offset_abs[c_idx][i]) {
1278
7/8
✓ Branch 0 taken 300849 times.
✓ Branch 1 taken 22733 times.
✓ Branch 2 taken 223920 times.
✓ Branch 3 taken 76929 times.
✓ Branch 5 taken 76929 times.
✓ Branch 6 taken 22733 times.
✓ Branch 7 taken 22733 times.
✗ Branch 8 not taken.
323582 SET_SAO(offset_sign[c_idx][i],
1279 ff_hevc_sao_offset_sign_decode(lc));
1280 } else {
1281 110114 sao->offset_sign[c_idx][i] = 0;
1282 }
1283 }
1284
7/8
✓ Branch 0 taken 99262 times.
✓ Branch 1 taken 9162 times.
✓ Branch 2 taken 72036 times.
✓ Branch 3 taken 27226 times.
✓ Branch 5 taken 27226 times.
✓ Branch 6 taken 9162 times.
✓ Branch 7 taken 9162 times.
✗ Branch 8 not taken.
108424 SET_SAO(band_position[c_idx], ff_hevc_sao_band_position_decode(lc));
1285
2/2
✓ Branch 0 taken 506073 times.
✓ Branch 1 taken 72143 times.
578216 } else if (c_idx != 2) {
1286
7/8
✓ Branch 0 taken 405950 times.
✓ Branch 1 taken 100123 times.
✓ Branch 2 taken 119922 times.
✓ Branch 3 taken 286028 times.
✓ Branch 5 taken 286028 times.
✓ Branch 6 taken 100123 times.
✓ Branch 7 taken 100123 times.
✗ Branch 8 not taken.
506073 SET_SAO(eo_class[c_idx], ff_hevc_sao_eo_class_decode(lc));
1287 }
1288
1289 // Inferred parameters
1290 686640 sao->offset_val[c_idx][0] = 0;
1291
2/2
✓ Branch 0 taken 2746560 times.
✓ Branch 1 taken 686640 times.
3433200 for (i = 0; i < 4; i++) {
1292 2746560 sao->offset_val[c_idx][i + 1] = sao->offset_abs[c_idx][i];
1293
2/2
✓ Branch 0 taken 2312864 times.
✓ Branch 1 taken 433696 times.
2746560 if (sao->type_idx[c_idx] == SAO_EDGE) {
1294
2/2
✓ Branch 0 taken 1156432 times.
✓ Branch 1 taken 1156432 times.
2312864 if (i > 1)
1295 1156432 sao->offset_val[c_idx][i + 1] = -sao->offset_val[c_idx][i + 1];
1296
2/2
✓ Branch 0 taken 144543 times.
✓ Branch 1 taken 289153 times.
433696 } else if (sao->offset_sign[c_idx][i]) {
1297 144543 sao->offset_val[c_idx][i + 1] = -sao->offset_val[c_idx][i + 1];
1298 }
1299 2746560 sao->offset_val[c_idx][i + 1] *= 1 << log2_sao_offset_scale;
1300 }
1301 }
1302 1941354 }
1303
1304 #undef SET_SAO
1305 #undef CTB
1306
1307 251428 static int hls_cross_component_pred(HEVCLocalContext *lc, int idx)
1308 {
1309 251428 int log2_res_scale_abs_plus1 = ff_hevc_log2_res_scale_abs(lc, idx);
1310
1311
2/2
✓ Branch 0 taken 142161 times.
✓ Branch 1 taken 109267 times.
251428 if (log2_res_scale_abs_plus1 != 0) {
1312 142161 int res_scale_sign_flag = ff_hevc_res_scale_sign_flag(lc, idx);
1313 142161 lc->tu.res_scale_val = (1 << (log2_res_scale_abs_plus1 - 1)) *
1314 142161 (1 - 2 * res_scale_sign_flag);
1315 } else {
1316 109267 lc->tu.res_scale_val = 0;
1317 }
1318
1319
1320 251428 return 0;
1321 }
1322
1323 15386391 static int hls_transform_unit(HEVCLocalContext *lc,
1324 const HEVCLayerContext *l,
1325 const HEVCPPS *pps, const HEVCSPS *sps,
1326 int x0, int y0,
1327 int xBase, int yBase, int cb_xBase, int cb_yBase,
1328 int log2_cb_size, int log2_trafo_size,
1329 int blk_idx, int cbf_luma, int *cbf_cb, int *cbf_cr)
1330 {
1331 15386391 const HEVCContext *const s = lc->parent;
1332 15386391 const int log2_trafo_size_c = log2_trafo_size - sps->hshift[1];
1333 int i;
1334
1335
2/2
✓ Branch 0 taken 8724226 times.
✓ Branch 1 taken 6662165 times.
15386391 if (lc->cu.pred_mode == MODE_INTRA) {
1336 8724226 int trafo_size = 1 << log2_trafo_size;
1337 8724226 ff_hevc_set_neighbour_available(lc, x0, y0, trafo_size, trafo_size, sps->log2_ctb_size);
1338
1339 8724226 s->hpc.intra_pred[log2_trafo_size - 2](lc, pps, x0, y0, 0);
1340 }
1341
1342
6/6
✓ Branch 0 taken 5642279 times.
✓ Branch 1 taken 9744112 times.
✓ Branch 2 taken 4998066 times.
✓ Branch 3 taken 644213 times.
✓ Branch 4 taken 4565113 times.
✓ Branch 5 taken 432953 times.
15386391 if (cbf_luma || cbf_cb[0] || cbf_cr[0] ||
1343
6/6
✓ Branch 0 taken 140838 times.
✓ Branch 1 taken 4424275 times.
✓ Branch 2 taken 133247 times.
✓ Branch 3 taken 7591 times.
✓ Branch 4 taken 51975 times.
✓ Branch 5 taken 81272 times.
15445956 (sps->chroma_format_idc == 2 && (cbf_cb[1] || cbf_cr[1]))) {
1344 10880844 int scan_idx = SCAN_DIAG;
1345 10880844 int scan_idx_c = SCAN_DIAG;
1346
4/4
✓ Branch 0 taken 7612397 times.
✓ Branch 1 taken 3268447 times.
✓ Branch 2 taken 6157025 times.
✓ Branch 3 taken 1455372 times.
17037869 int cbf_chroma = cbf_cb[0] || cbf_cr[0] ||
1347
2/2
✓ Branch 0 taken 196699 times.
✓ Branch 1 taken 5960326 times.
6157025 (sps->chroma_format_idc == 2 &&
1348
4/4
✓ Branch 0 taken 170962 times.
✓ Branch 1 taken 25737 times.
✓ Branch 2 taken 97068 times.
✓ Branch 3 taken 73894 times.
196699 (cbf_cb[1] || cbf_cr[1]));
1349
1350
4/4
✓ Branch 0 taken 2714348 times.
✓ Branch 1 taken 8166496 times.
✓ Branch 2 taken 920242 times.
✓ Branch 3 taken 1794106 times.
10880844 if (pps->cu_qp_delta_enabled_flag && !lc->tu.is_cu_qp_delta_coded) {
1351 920242 lc->tu.cu_qp_delta = ff_hevc_cu_qp_delta_abs(lc);
1352
2/2
✓ Branch 0 taken 485048 times.
✓ Branch 1 taken 435194 times.
920242 if (lc->tu.cu_qp_delta != 0)
1353
2/2
✓ Branch 1 taken 262087 times.
✓ Branch 2 taken 222961 times.
485048 if (ff_hevc_cu_qp_delta_sign_flag(lc) == 1)
1354 262087 lc->tu.cu_qp_delta = -lc->tu.cu_qp_delta;
1355 920242 lc->tu.is_cu_qp_delta_coded = 1;
1356
1357
2/2
✓ Branch 0 taken 920241 times.
✓ Branch 1 taken 1 times.
920242 if (lc->tu.cu_qp_delta < -(26 + sps->qp_bd_offset / 2) ||
1358
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 920241 times.
920241 lc->tu.cu_qp_delta > (25 + sps->qp_bd_offset / 2)) {
1359 1 av_log(s->avctx, AV_LOG_ERROR,
1360 "The cu_qp_delta %d is outside the valid range "
1361 "[%d, %d].\n",
1362 lc->tu.cu_qp_delta,
1363 1 -(26 + sps->qp_bd_offset / 2),
1364 1 (25 + sps->qp_bd_offset / 2));
1365 1 return AVERROR_INVALIDDATA;
1366 }
1367
1368 920241 ff_hevc_set_qPy(lc, l, pps, cb_xBase, cb_yBase, log2_cb_size);
1369 }
1370
1371
4/4
✓ Branch 0 taken 779607 times.
✓ Branch 1 taken 10101236 times.
✓ Branch 2 taken 730220 times.
✓ Branch 3 taken 49387 times.
10880843 if (s->sh.cu_chroma_qp_offset_enabled_flag && cbf_chroma &&
1372
3/4
✓ Branch 0 taken 730220 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 228078 times.
✓ Branch 3 taken 502142 times.
730220 !lc->cu.cu_transquant_bypass_flag && !lc->tu.is_cu_chroma_qp_offset_coded) {
1373 228078 int cu_chroma_qp_offset_flag = ff_hevc_cu_chroma_qp_offset_flag(lc);
1374
2/2
✓ Branch 0 taken 35811 times.
✓ Branch 1 taken 192267 times.
228078 if (cu_chroma_qp_offset_flag) {
1375 35811 int cu_chroma_qp_offset_idx = 0;
1376
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 35811 times.
35811 if (pps->chroma_qp_offset_list_len_minus1 > 0) {
1377 cu_chroma_qp_offset_idx = ff_hevc_cu_chroma_qp_offset_idx(lc, pps->chroma_qp_offset_list_len_minus1);
1378 av_log(s->avctx, AV_LOG_ERROR,
1379 "cu_chroma_qp_offset_idx not yet tested.\n");
1380 }
1381 35811 lc->tu.cu_qp_offset_cb = pps->cb_qp_offset_list[cu_chroma_qp_offset_idx];
1382 35811 lc->tu.cu_qp_offset_cr = pps->cr_qp_offset_list[cu_chroma_qp_offset_idx];
1383 } else {
1384 192267 lc->tu.cu_qp_offset_cb = 0;
1385 192267 lc->tu.cu_qp_offset_cr = 0;
1386 }
1387 228078 lc->tu.is_cu_chroma_qp_offset_coded = 1;
1388 }
1389
1390
4/4
✓ Branch 0 taken 6333043 times.
✓ Branch 1 taken 4547800 times.
✓ Branch 2 taken 5489439 times.
✓ Branch 3 taken 843604 times.
10880843 if (lc->cu.pred_mode == MODE_INTRA && log2_trafo_size < 4) {
1391
2/2
✓ Branch 0 taken 3646895 times.
✓ Branch 1 taken 1842544 times.
5489439 if (lc->tu.intra_pred_mode >= 6 &&
1392
2/2
✓ Branch 0 taken 1267460 times.
✓ Branch 1 taken 2379435 times.
3646895 lc->tu.intra_pred_mode <= 14) {
1393 1267460 scan_idx = SCAN_VERT;
1394
2/2
✓ Branch 0 taken 1842352 times.
✓ Branch 1 taken 2379627 times.
4221979 } else if (lc->tu.intra_pred_mode >= 22 &&
1395
2/2
✓ Branch 0 taken 1555358 times.
✓ Branch 1 taken 286994 times.
1842352 lc->tu.intra_pred_mode <= 30) {
1396 1555358 scan_idx = SCAN_HORIZ;
1397 }
1398
1399
2/2
✓ Branch 0 taken 3304109 times.
✓ Branch 1 taken 2185330 times.
5489439 if (lc->tu.intra_pred_mode_c >= 6 &&
1400
2/2
✓ Branch 0 taken 1216445 times.
✓ Branch 1 taken 2087664 times.
3304109 lc->tu.intra_pred_mode_c <= 14) {
1401 1216445 scan_idx_c = SCAN_VERT;
1402
2/2
✓ Branch 0 taken 1753394 times.
✓ Branch 1 taken 2519600 times.
4272994 } else if (lc->tu.intra_pred_mode_c >= 22 &&
1403
2/2
✓ Branch 0 taken 1474007 times.
✓ Branch 1 taken 279387 times.
1753394 lc->tu.intra_pred_mode_c <= 30) {
1404 1474007 scan_idx_c = SCAN_HORIZ;
1405 }
1406 }
1407
1408 10880843 lc->tu.cross_pf = 0;
1409
1410
2/2
✓ Branch 0 taken 9744111 times.
✓ Branch 1 taken 1136732 times.
10880843 if (cbf_luma)
1411 9744111 ff_hevc_hls_residual_coding(lc, pps, x0, y0, log2_trafo_size, scan_idx, 0);
1412
6/6
✓ Branch 0 taken 10880747 times.
✓ Branch 1 taken 96 times.
✓ Branch 2 taken 5580703 times.
✓ Branch 3 taken 5300044 times.
✓ Branch 4 taken 148785 times.
✓ Branch 5 taken 5431918 times.
16329672 if (sps->chroma_format_idc && (log2_trafo_size > 2 || sps->chroma_format_idc == 3)) {
1413 5448829 int trafo_size_h = 1 << (log2_trafo_size_c + sps->hshift[1]);
1414 5448829 int trafo_size_v = 1 << (log2_trafo_size_c + sps->vshift[1]);
1415
4/4
✓ Branch 0 taken 246342 times.
✓ Branch 1 taken 5202487 times.
✓ Branch 2 taken 140024 times.
✓ Branch 3 taken 106318 times.
5588853 lc->tu.cross_pf = (pps->cross_component_prediction_enabled_flag && cbf_luma &&
1416
2/2
✓ Branch 0 taken 91081 times.
✓ Branch 1 taken 48943 times.
140024 (lc->cu.pred_mode == MODE_INTER ||
1417
2/2
✓ Branch 0 taken 76771 times.
✓ Branch 1 taken 14310 times.
91081 (lc->tu.chroma_mode_c == 4)));
1418
1419
2/2
✓ Branch 0 taken 125714 times.
✓ Branch 1 taken 5323115 times.
5448829 if (lc->tu.cross_pf) {
1420 125714 hls_cross_component_pred(lc, 0);
1421 }
1422
4/4
✓ Branch 0 taken 1078236 times.
✓ Branch 1 taken 10178834 times.
✓ Branch 2 taken 5808241 times.
✓ Branch 3 taken 5448829 times.
11257070 for (i = 0; i < (sps->chroma_format_idc == 2 ? 2 : 1); i++) {
1423
2/2
✓ Branch 0 taken 2623670 times.
✓ Branch 1 taken 3184571 times.
5808241 if (lc->cu.pred_mode == MODE_INTRA) {
1424 2623670 ff_hevc_set_neighbour_available(lc, x0, y0 + (i << log2_trafo_size_c),
1425 2623670 trafo_size_h, trafo_size_v, sps->log2_ctb_size);
1426 2623670 s->hpc.intra_pred[log2_trafo_size_c - 2](lc, pps, x0, y0 + (i << log2_trafo_size_c), 1);
1427 }
1428
2/2
✓ Branch 0 taken 1440397 times.
✓ Branch 1 taken 4367844 times.
5808241 if (cbf_cb[i])
1429 1440397 ff_hevc_hls_residual_coding(lc, pps, x0, y0 + (i << log2_trafo_size_c),
1430 log2_trafo_size_c, scan_idx_c, 1);
1431 else
1432
2/2
✓ Branch 0 taken 27018 times.
✓ Branch 1 taken 4340826 times.
4367844 if (lc->tu.cross_pf) {
1433 27018 ptrdiff_t stride = s->cur_frame->f->linesize[1];
1434 27018 int hshift = sps->hshift[1];
1435 27018 int vshift = sps->vshift[1];
1436 27018 const int16_t *coeffs_y = (int16_t*)lc->edge_emu_buffer;
1437 27018 int16_t *coeffs = (int16_t*)lc->edge_emu_buffer2;
1438 27018 int size = 1 << log2_trafo_size_c;
1439
1440 27018 uint8_t *dst = &s->cur_frame->f->data[1][(y0 >> vshift) * stride +
1441 27018 ((x0 >> hshift) << sps->pixel_shift)];
1442
2/2
✓ Branch 0 taken 1473408 times.
✓ Branch 1 taken 27018 times.
1500426 for (i = 0; i < (size * size); i++) {
1443 1473408 coeffs[i] = ((lc->tu.res_scale_val * coeffs_y[i]) >> 3);
1444 }
1445 27018 s->hevcdsp.add_residual[log2_trafo_size_c-2](dst, coeffs, stride);
1446 }
1447 }
1448
1449
2/2
✓ Branch 0 taken 125714 times.
✓ Branch 1 taken 5323115 times.
5448829 if (lc->tu.cross_pf) {
1450 125714 hls_cross_component_pred(lc, 1);
1451 }
1452
4/4
✓ Branch 0 taken 1078236 times.
✓ Branch 1 taken 10178834 times.
✓ Branch 2 taken 5808241 times.
✓ Branch 3 taken 5448829 times.
11257070 for (i = 0; i < (sps->chroma_format_idc == 2 ? 2 : 1); i++) {
1453
2/2
✓ Branch 0 taken 2623670 times.
✓ Branch 1 taken 3184571 times.
5808241 if (lc->cu.pred_mode == MODE_INTRA) {
1454 2623670 ff_hevc_set_neighbour_available(lc, x0, y0 + (i << log2_trafo_size_c),
1455 2623670 trafo_size_h, trafo_size_v, sps->log2_ctb_size);
1456 2623670 s->hpc.intra_pred[log2_trafo_size_c - 2](lc, pps, x0, y0 + (i << log2_trafo_size_c), 2);
1457 }
1458
2/2
✓ Branch 0 taken 1512457 times.
✓ Branch 1 taken 4295784 times.
5808241 if (cbf_cr[i])
1459 1512457 ff_hevc_hls_residual_coding(lc, pps, x0, y0 + (i << log2_trafo_size_c),
1460 log2_trafo_size_c, scan_idx_c, 2);
1461 else
1462
2/2
✓ Branch 0 taken 52981 times.
✓ Branch 1 taken 4242803 times.
4295784 if (lc->tu.cross_pf) {
1463 52981 ptrdiff_t stride = s->cur_frame->f->linesize[2];
1464 52981 int hshift = sps->hshift[2];
1465 52981 int vshift = sps->vshift[2];
1466 52981 const int16_t *coeffs_y = (int16_t*)lc->edge_emu_buffer;
1467 52981 int16_t *coeffs = (int16_t*)lc->edge_emu_buffer2;
1468 52981 int size = 1 << log2_trafo_size_c;
1469
1470 52981 uint8_t *dst = &s->cur_frame->f->data[2][(y0 >> vshift) * stride +
1471 52981 ((x0 >> hshift) << sps->pixel_shift)];
1472
2/2
✓ Branch 0 taken 2927920 times.
✓ Branch 1 taken 52981 times.
2980901 for (i = 0; i < (size * size); i++) {
1473 2927920 coeffs[i] = ((lc->tu.res_scale_val * coeffs_y[i]) >> 3);
1474 }
1475 52981 s->hevcdsp.add_residual[log2_trafo_size_c-2](dst, coeffs, stride);
1476 }
1477 }
1478
4/4
✓ Branch 0 taken 5431918 times.
✓ Branch 1 taken 96 times.
✓ Branch 2 taken 1396560 times.
✓ Branch 3 taken 4035358 times.
5432014 } else if (sps->chroma_format_idc && blk_idx == 3) {
1479 1396560 int trafo_size_h = 1 << (log2_trafo_size + 1);
1480 1396560 int trafo_size_v = 1 << (log2_trafo_size + sps->vshift[1]);
1481
4/4
✓ Branch 0 taken 412647 times.
✓ Branch 1 taken 2518022 times.
✓ Branch 2 taken 1534109 times.
✓ Branch 3 taken 1396560 times.
2930669 for (i = 0; i < (sps->chroma_format_idc == 2 ? 2 : 1); i++) {
1482
2/2
✓ Branch 0 taken 1034762 times.
✓ Branch 1 taken 499347 times.
1534109 if (lc->cu.pred_mode == MODE_INTRA) {
1483 1034762 ff_hevc_set_neighbour_available(lc, xBase, yBase + (i << log2_trafo_size),
1484 1034762 trafo_size_h, trafo_size_v, sps->log2_ctb_size);
1485 1034762 s->hpc.intra_pred[log2_trafo_size - 2](lc, pps, xBase, yBase + (i << log2_trafo_size), 1);
1486 }
1487
2/2
✓ Branch 0 taken 535908 times.
✓ Branch 1 taken 998201 times.
1534109 if (cbf_cb[i])
1488 535908 ff_hevc_hls_residual_coding(lc, pps, xBase, yBase + (i << log2_trafo_size),
1489 log2_trafo_size, scan_idx_c, 1);
1490 }
1491
4/4
✓ Branch 0 taken 412647 times.
✓ Branch 1 taken 2518022 times.
✓ Branch 2 taken 1534109 times.
✓ Branch 3 taken 1396560 times.
2930669 for (i = 0; i < (sps->chroma_format_idc == 2 ? 2 : 1); i++) {
1492
2/2
✓ Branch 0 taken 1034762 times.
✓ Branch 1 taken 499347 times.
1534109 if (lc->cu.pred_mode == MODE_INTRA) {
1493 1034762 ff_hevc_set_neighbour_available(lc, xBase, yBase + (i << log2_trafo_size),
1494 1034762 trafo_size_h, trafo_size_v, sps->log2_ctb_size);
1495 1034762 s->hpc.intra_pred[log2_trafo_size - 2](lc, pps, xBase, yBase + (i << log2_trafo_size), 2);
1496 }
1497
2/2
✓ Branch 0 taken 623929 times.
✓ Branch 1 taken 910180 times.
1534109 if (cbf_cr[i])
1498 623929 ff_hevc_hls_residual_coding(lc, pps, xBase, yBase + (i << log2_trafo_size),
1499 log2_trafo_size, scan_idx_c, 2);
1500 }
1501 }
1502
3/4
✓ Branch 0 taken 4505547 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2391182 times.
✓ Branch 3 taken 2114365 times.
4505547 } else if (sps->chroma_format_idc && lc->cu.pred_mode == MODE_INTRA) {
1503
4/4
✓ Branch 0 taken 1445233 times.
✓ Branch 1 taken 945949 times.
✓ Branch 2 taken 46806 times.
✓ Branch 3 taken 1398427 times.
3383937 if (log2_trafo_size > 2 || sps->chroma_format_idc == 3) {
1504 992755 int trafo_size_h = 1 << (log2_trafo_size_c + sps->hshift[1]);
1505 992755 int trafo_size_v = 1 << (log2_trafo_size_c + sps->vshift[1]);
1506 992755 ff_hevc_set_neighbour_available(lc, x0, y0, trafo_size_h, trafo_size_v,
1507 992755 sps->log2_ctb_size);
1508 992755 s->hpc.intra_pred[log2_trafo_size_c - 2](lc, pps, x0, y0, 1);
1509 992755 s->hpc.intra_pred[log2_trafo_size_c - 2](lc, pps, x0, y0, 2);
1510
2/2
✓ Branch 0 taken 7467 times.
✓ Branch 1 taken 985288 times.
992755 if (sps->chroma_format_idc == 2) {
1511 7467 ff_hevc_set_neighbour_available(lc, x0, y0 + (1 << log2_trafo_size_c),
1512 7467 trafo_size_h, trafo_size_v, sps->log2_ctb_size);
1513 7467 s->hpc.intra_pred[log2_trafo_size_c - 2](lc, pps, x0, y0 + (1 << log2_trafo_size_c), 1);
1514 7467 s->hpc.intra_pred[log2_trafo_size_c - 2](lc, pps, x0, y0 + (1 << log2_trafo_size_c), 2);
1515 }
1516
2/2
✓ Branch 0 taken 316992 times.
✓ Branch 1 taken 1081435 times.
1398427 } else if (blk_idx == 3) {
1517 316992 int trafo_size_h = 1 << (log2_trafo_size + 1);
1518 316992 int trafo_size_v = 1 << (log2_trafo_size + sps->vshift[1]);
1519 316992 ff_hevc_set_neighbour_available(lc, xBase, yBase,
1520 316992 trafo_size_h, trafo_size_v, sps->log2_ctb_size);
1521 316992 s->hpc.intra_pred[log2_trafo_size - 2](lc, pps, xBase, yBase, 1);
1522 316992 s->hpc.intra_pred[log2_trafo_size - 2](lc, pps, xBase, yBase, 2);
1523
2/2
✓ Branch 0 taken 2373 times.
✓ Branch 1 taken 314619 times.
316992 if (sps->chroma_format_idc == 2) {
1524 2373 ff_hevc_set_neighbour_available(lc, xBase, yBase + (1 << log2_trafo_size),
1525 2373 trafo_size_h, trafo_size_v, sps->log2_ctb_size);
1526 2373 s->hpc.intra_pred[log2_trafo_size - 2](lc, pps, xBase, yBase + (1 << log2_trafo_size), 1);
1527 2373 s->hpc.intra_pred[log2_trafo_size - 2](lc, pps, xBase, yBase + (1 << log2_trafo_size), 2);
1528 }
1529 }
1530 }
1531
1532 15386390 return 0;
1533 }
1534
1535 339768 static void set_deblocking_bypass(uint8_t *is_pcm, const HEVCSPS *sps,
1536 int x0, int y0, int log2_cb_size)
1537 {
1538 339768 int cb_size = 1 << log2_cb_size;
1539 339768 int log2_min_pu_size = sps->log2_min_pu_size;
1540
1541 339768 int min_pu_width = sps->min_pu_width;
1542 339768 int x_end = FFMIN(x0 + cb_size, sps->width);
1543 339768 int y_end = FFMIN(y0 + cb_size, sps->height);
1544 int i, j;
1545
1546
2/2
✓ Branch 0 taken 485180 times.
✓ Branch 1 taken 339768 times.
824948 for (j = (y0 >> log2_min_pu_size); j < (y_end >> log2_min_pu_size); j++)
1547
2/2
✓ Branch 0 taken 964500 times.
✓ Branch 1 taken 485180 times.
1449680 for (i = (x0 >> log2_min_pu_size); i < (x_end >> log2_min_pu_size); i++)
1548 964500 is_pcm[i + j * min_pu_width] = 2;
1549 339768 }
1550
1551 18463602 static int hls_transform_tree(HEVCLocalContext *lc,
1552 const HEVCLayerContext *l,
1553 const HEVCPPS *pps, const HEVCSPS *sps,
1554 int x0, int y0,
1555 int xBase, int yBase, int cb_xBase, int cb_yBase,
1556 int log2_cb_size, int log2_trafo_size,
1557 int trafo_depth, int blk_idx,
1558 const int *base_cbf_cb, const int *base_cbf_cr)
1559 {
1560 18463602 const HEVCContext *const s = lc->parent;
1561 uint8_t split_transform_flag;
1562 int cbf_cb[2];
1563 int cbf_cr[2];
1564 int ret;
1565
1566 18463602 cbf_cb[0] = base_cbf_cb[0];
1567 18463602 cbf_cb[1] = base_cbf_cb[1];
1568 18463602 cbf_cr[0] = base_cbf_cr[0];
1569 18463602 cbf_cr[1] = base_cbf_cr[1];
1570
1571
2/2
✓ Branch 0 taken 5559011 times.
✓ Branch 1 taken 12904591 times.
18463602 if (lc->cu.intra_split_flag) {
1572
2/2
✓ Branch 0 taken 4165244 times.
✓ Branch 1 taken 1393767 times.
5559011 if (trafo_depth == 1) {
1573 4165244 lc->tu.intra_pred_mode = lc->pu.intra_pred_mode[blk_idx];
1574
2/2
✓ Branch 0 taken 74576 times.
✓ Branch 1 taken 4090668 times.
4165244 if (sps->chroma_format_idc == 3) {
1575 74576 lc->tu.intra_pred_mode_c = lc->pu.intra_pred_mode_c[blk_idx];
1576 74576 lc->tu.chroma_mode_c = lc->pu.chroma_mode_c[blk_idx];
1577 } else {
1578 4090668 lc->tu.intra_pred_mode_c = lc->pu.intra_pred_mode_c[0];
1579 4090668 lc->tu.chroma_mode_c = lc->pu.chroma_mode_c[0];
1580 }
1581 }
1582 } else {
1583 12904591 lc->tu.intra_pred_mode = lc->pu.intra_pred_mode[0];
1584 12904591 lc->tu.intra_pred_mode_c = lc->pu.intra_pred_mode_c[0];
1585 12904591 lc->tu.chroma_mode_c = lc->pu.chroma_mode_c[0];
1586 }
1587
1588
2/2
✓ Branch 0 taken 18347658 times.
✓ Branch 1 taken 115944 times.
18463602 if (log2_trafo_size <= sps->log2_max_trafo_size &&
1589
2/2
✓ Branch 0 taken 10271714 times.
✓ Branch 1 taken 8075944 times.
18347658 log2_trafo_size > sps->log2_min_tb_size &&
1590
2/2
✓ Branch 0 taken 7850060 times.
✓ Branch 1 taken 2421654 times.
10271714 trafo_depth < lc->cu.max_trafo_depth &&
1591
4/4
✓ Branch 0 taken 1506116 times.
✓ Branch 1 taken 6343944 times.
✓ Branch 2 taken 469616 times.
✓ Branch 3 taken 1036500 times.
7850060 !(lc->cu.intra_split_flag && trafo_depth == 0)) {
1592 6813560 split_transform_flag = ff_hevc_split_transform_flag_decode(lc, log2_trafo_size);
1593 } else {
1594 25641873 int inter_split = sps->max_transform_hierarchy_depth_inter == 0 &&
1595
2/2
✓ Branch 0 taken 1552658 times.
✓ Branch 1 taken 789131 times.
2341789 lc->cu.pred_mode == MODE_INTER &&
1596
6/6
✓ Branch 0 taken 2341789 times.
✓ Branch 1 taken 9308253 times.
✓ Branch 2 taken 930859 times.
✓ Branch 3 taken 621799 times.
✓ Branch 4 taken 184863 times.
✓ Branch 5 taken 745996 times.
13991831 lc->cu.part_mode != PART_2Nx2N &&
1597 trafo_depth == 0;
1598
1599 11650042 split_transform_flag = log2_trafo_size > sps->log2_max_trafo_size ||
1600
8/8
✓ Branch 0 taken 11534098 times.
✓ Branch 1 taken 115944 times.
✓ Branch 2 taken 5084192 times.
✓ Branch 3 taken 6449906 times.
✓ Branch 4 taken 4047692 times.
✓ Branch 5 taken 1036500 times.
✓ Branch 6 taken 169700 times.
✓ Branch 7 taken 10327898 times.
11650042 (lc->cu.intra_split_flag && trafo_depth == 0) ||
1601 inter_split;
1602 }
1603
1604
6/6
✓ Branch 0 taken 18463506 times.
✓ Branch 1 taken 96 times.
✓ Branch 2 taken 7989448 times.
✓ Branch 3 taken 10474058 times.
✓ Branch 4 taken 239856 times.
✓ Branch 5 taken 7749592 times.
18463602 if (sps->chroma_format_idc && (log2_trafo_size > 2 || sps->chroma_format_idc == 3)) {
1605
4/4
✓ Branch 0 taken 4559252 times.
✓ Branch 1 taken 6154662 times.
✓ Branch 2 taken 1165872 times.
✓ Branch 3 taken 3393380 times.
10713914 if (trafo_depth == 0 || cbf_cb[0]) {
1606 7320534 cbf_cb[0] = ff_hevc_cbf_cb_cr_decode(lc, trafo_depth);
1607
6/6
✓ Branch 0 taken 458668 times.
✓ Branch 1 taken 6861866 times.
✓ Branch 2 taken 170236 times.
✓ Branch 3 taken 288432 times.
✓ Branch 4 taken 108956 times.
✓ Branch 5 taken 61280 times.
7320534 if (sps->chroma_format_idc == 2 && (!split_transform_flag || log2_trafo_size == 3)) {
1608 397388 cbf_cb[1] = ff_hevc_cbf_cb_cr_decode(lc, trafo_depth);
1609 }
1610 }
1611
1612
4/4
✓ Branch 0 taken 4559252 times.
✓ Branch 1 taken 6154662 times.
✓ Branch 2 taken 1098848 times.
✓ Branch 3 taken 3460404 times.
10713914 if (trafo_depth == 0 || cbf_cr[0]) {
1613 7253510 cbf_cr[0] = ff_hevc_cbf_cb_cr_decode(lc, trafo_depth);
1614
6/6
✓ Branch 0 taken 605644 times.
✓ Branch 1 taken 6647866 times.
✓ Branch 2 taken 217952 times.
✓ Branch 3 taken 387692 times.
✓ Branch 4 taken 144600 times.
✓ Branch 5 taken 73352 times.
7253510 if (sps->chroma_format_idc == 2 && (!split_transform_flag || log2_trafo_size == 3)) {
1615 532292 cbf_cr[1] = ff_hevc_cbf_cb_cr_decode(lc, trafo_depth);
1616 }
1617 }
1618 }
1619
1620
2/2
✓ Branch 0 taken 3077211 times.
✓ Branch 1 taken 15386391 times.
18463602 if (split_transform_flag) {
1621 3077211 const int trafo_size_split = 1 << (log2_trafo_size - 1);
1622 3077211 const int x1 = x0 + trafo_size_split;
1623 3077211 const int y1 = y0 + trafo_size_split;
1624
1625 #define SUBDIVIDE(x, y, idx) \
1626 do { \
1627 ret = hls_transform_tree(lc, l, pps, sps, \
1628 x, y, x0, y0, cb_xBase, cb_yBase, log2_cb_size, \
1629 log2_trafo_size - 1, trafo_depth + 1, idx, \
1630 cbf_cb, cbf_cr); \
1631 if (ret < 0) \
1632 return ret; \
1633 } while (0)
1634
1635
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 3077211 times.
3077211 SUBDIVIDE(x0, y0, 0);
1636
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 3077211 times.
3077211 SUBDIVIDE(x1, y0, 1);
1637
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 3077211 times.
3077211 SUBDIVIDE(x0, y1, 2);
1638
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 3077211 times.
3077211 SUBDIVIDE(x1, y1, 3);
1639
1640 #undef SUBDIVIDE
1641 } else {
1642 15386391 int min_tu_size = 1 << sps->log2_min_tb_size;
1643 15386391 int log2_min_tu_size = sps->log2_min_tb_size;
1644 15386391 int min_tu_width = sps->min_tb_width;
1645 15386391 int cbf_luma = 1;
1646
1647
4/4
✓ Branch 0 taken 6662165 times.
✓ Branch 1 taken 8724226 times.
✓ Branch 2 taken 1589494 times.
✓ Branch 3 taken 5072671 times.
15386391 if (lc->cu.pred_mode == MODE_INTRA || trafo_depth != 0 ||
1648
4/4
✓ Branch 0 taken 1257801 times.
✓ Branch 1 taken 331693 times.
✓ Branch 2 taken 1090362 times.
✓ Branch 3 taken 167439 times.
1589494 cbf_cb[0] || cbf_cr[0] ||
1649
6/6
✓ Branch 0 taken 24676 times.
✓ Branch 1 taken 1065686 times.
✓ Branch 2 taken 23413 times.
✓ Branch 3 taken 1263 times.
✓ Branch 4 taken 11383 times.
✓ Branch 5 taken 12030 times.
1090362 (sps->chroma_format_idc == 2 && (cbf_cb[1] || cbf_cr[1]))) {
1650 14308675 cbf_luma = ff_hevc_cbf_luma_decode(lc, trafo_depth);
1651 }
1652
1653 15386391 ret = hls_transform_unit(lc, l, pps, sps,
1654 x0, y0, xBase, yBase, cb_xBase, cb_yBase,
1655 log2_cb_size, log2_trafo_size,
1656 blk_idx, cbf_luma, cbf_cb, cbf_cr);
1657
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 15386390 times.
15386391 if (ret < 0)
1658 1 return ret;
1659 // TODO: store cbf_luma somewhere else
1660
2/2
✓ Branch 0 taken 9744111 times.
✓ Branch 1 taken 5642279 times.
15386390 if (cbf_luma) {
1661 int i, j;
1662
2/2
✓ Branch 0 taken 22006960 times.
✓ Branch 1 taken 9744111 times.
31751071 for (i = 0; i < (1 << log2_trafo_size); i += min_tu_size)
1663
2/2
✓ Branch 0 taken 86690898 times.
✓ Branch 1 taken 22006960 times.
108697858 for (j = 0; j < (1 << log2_trafo_size); j += min_tu_size) {
1664 86690898 int x_tu = (x0 + j) >> log2_min_tu_size;
1665 86690898 int y_tu = (y0 + i) >> log2_min_tu_size;
1666 86690898 l->cbf_luma[y_tu * min_tu_width + x_tu] = 1;
1667 }
1668 }
1669
2/2
✓ Branch 0 taken 14891329 times.
✓ Branch 1 taken 495061 times.
15386390 if (!s->sh.disable_deblocking_filter_flag) {
1670 14891329 ff_hevc_deblocking_boundary_strengths(lc, l, pps, x0, y0, log2_trafo_size);
1671
2/2
✓ Branch 0 taken 319726 times.
✓ Branch 1 taken 14571603 times.
14891329 if (pps->transquant_bypass_enable_flag &&
1672
2/2
✓ Branch 0 taken 260054 times.
✓ Branch 1 taken 59672 times.
319726 lc->cu.cu_transquant_bypass_flag)
1673 260054 set_deblocking_bypass(l->is_pcm, sps, x0, y0, log2_trafo_size);
1674 }
1675 }
1676 18463601 return 0;
1677 }
1678
1679 11207 static int hls_pcm_sample(HEVCLocalContext *lc, const HEVCLayerContext *l,
1680 const HEVCPPS *pps, int x0, int y0, int log2_cb_size)
1681 {
1682 11207 const HEVCContext *const s = lc->parent;
1683 11207 const HEVCSPS *const sps = pps->sps;
1684 GetBitContext gb;
1685 11207 int cb_size = 1 << log2_cb_size;
1686 11207 ptrdiff_t stride0 = s->cur_frame->f->linesize[0];
1687 11207 uint8_t *dst0 = &s->cur_frame->f->data[0][y0 * stride0 + (x0 << sps->pixel_shift)];
1688
1689 22414 int length = cb_size * cb_size * sps->pcm.bit_depth + (sps->chroma_format_idc != 0 ?
1690 11207 (((cb_size >> sps->hshift[1]) * (cb_size >> sps->vshift[1])) +
1691 11207 ((cb_size >> sps->hshift[2]) * (cb_size >> sps->vshift[2]))) *
1692
1/2
✓ Branch 0 taken 11207 times.
✗ Branch 1 not taken.
11207 sps->pcm.bit_depth_chroma : 0);
1693 11207 const uint8_t *pcm = skip_bytes(&lc->cc, (length + 7) >> 3);
1694 int ret;
1695
1696
2/2
✓ Branch 0 taken 6041 times.
✓ Branch 1 taken 5166 times.
11207 if (!s->sh.disable_deblocking_filter_flag)
1697 6041 ff_hevc_deblocking_boundary_strengths(lc, l, pps, x0, y0, log2_cb_size);
1698
1699 11207 ret = init_get_bits(&gb, pcm, length);
1700
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11207 times.
11207 if (ret < 0)
1701 return ret;
1702
1703 11207 s->hevcdsp.put_pcm(dst0, stride0, cb_size, cb_size, &gb, sps->pcm.bit_depth);
1704
1/2
✓ Branch 0 taken 11207 times.
✗ Branch 1 not taken.
11207 if (sps->chroma_format_idc) {
1705 11207 ptrdiff_t stride1 = s->cur_frame->f->linesize[1];
1706 11207 ptrdiff_t stride2 = s->cur_frame->f->linesize[2];
1707 11207 uint8_t *dst1 = &s->cur_frame->f->data[1][(y0 >> sps->vshift[1]) * stride1 + ((x0 >> sps->hshift[1]) << sps->pixel_shift)];
1708 11207 uint8_t *dst2 = &s->cur_frame->f->data[2][(y0 >> sps->vshift[2]) * stride2 + ((x0 >> sps->hshift[2]) << sps->pixel_shift)];
1709
1710 11207 s->hevcdsp.put_pcm(dst1, stride1,
1711 11207 cb_size >> sps->hshift[1],
1712 11207 cb_size >> sps->vshift[1],
1713 11207 &gb, sps->pcm.bit_depth_chroma);
1714 11207 s->hevcdsp.put_pcm(dst2, stride2,
1715 11207 cb_size >> sps->hshift[2],
1716 11207 cb_size >> sps->vshift[2],
1717 11207 &gb, sps->pcm.bit_depth_chroma);
1718 }
1719
1720 11207 return 0;
1721 }
1722
1723 /**
1724 * 8.5.3.2.2.1 Luma sample unidirectional interpolation process
1725 *
1726 * @param s HEVC decoding context
1727 * @param dst target buffer for block data at block position
1728 * @param dststride stride of the dst buffer
1729 * @param ref reference picture buffer at origin (0, 0)
1730 * @param mv motion vector (relative to block position) to get pixel data from
1731 * @param x_off horizontal position of block from origin (0, 0)
1732 * @param y_off vertical position of block from origin (0, 0)
1733 * @param block_w width of block
1734 * @param block_h height of block
1735 * @param luma_weight weighting factor applied to the luma prediction
1736 * @param luma_offset additive offset applied to the luma prediction value
1737 */
1738
1739 6342554 static void luma_mc_uni(HEVCLocalContext *lc,
1740 const HEVCPPS *pps, const HEVCSPS *sps,
1741 uint8_t *dst, ptrdiff_t dststride,
1742 const AVFrame *ref, const Mv *mv, int x_off, int y_off,
1743 int block_w, int block_h, int luma_weight, int luma_offset)
1744 {
1745 6342554 const HEVCContext *const s = lc->parent;
1746 6342554 const uint8_t *src = ref->data[0];
1747 6342554 ptrdiff_t srcstride = ref->linesize[0];
1748 6342554 int pic_width = sps->width;
1749 6342554 int pic_height = sps->height;
1750 6342554 int mx = mv->x & 3;
1751 6342554 int my = mv->y & 3;
1752
4/4
✓ Branch 0 taken 3280827 times.
✓ Branch 1 taken 3061727 times.
✓ Branch 2 taken 3220239 times.
✓ Branch 3 taken 60588 times.
12624520 int weight_flag = (s->sh.slice_type == HEVC_SLICE_P && pps->weighted_pred_flag) ||
1753
4/4
✓ Branch 0 taken 3061727 times.
✓ Branch 1 taken 3220239 times.
✓ Branch 2 taken 73276 times.
✓ Branch 3 taken 2988451 times.
6281966 (s->sh.slice_type == HEVC_SLICE_B && pps->weighted_bipred_flag);
1754 6342554 int idx = hevc_pel_weight[block_w];
1755
1756 6342554 x_off += mv->x >> 2;
1757 6342554 y_off += mv->y >> 2;
1758 6342554 src += y_off * srcstride + (x_off * (1 << sps->pixel_shift));
1759
1760
4/4
✓ Branch 0 taken 6274424 times.
✓ Branch 1 taken 68130 times.
✓ Branch 2 taken 6154893 times.
✓ Branch 3 taken 119531 times.
6342554 if (x_off < QPEL_EXTRA_BEFORE || y_off < QPEL_EXTRA_AFTER ||
1761
2/2
✓ Branch 0 taken 6078917 times.
✓ Branch 1 taken 75976 times.
6154893 x_off >= pic_width - block_w - QPEL_EXTRA_AFTER ||
1762
2/2
✓ Branch 0 taken 5860745 times.
✓ Branch 1 taken 218172 times.
6078917 y_off >= pic_height - block_h - QPEL_EXTRA_AFTER ||
1763
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5860745 times.
5860745 ref == s->cur_frame->f) {
1764 481809 const ptrdiff_t edge_emu_stride = EDGE_EMU_BUFFER_STRIDE << sps->pixel_shift;
1765 481809 int offset = QPEL_EXTRA_BEFORE * srcstride + (QPEL_EXTRA_BEFORE << sps->pixel_shift);
1766 481809 int buf_offset = QPEL_EXTRA_BEFORE * edge_emu_stride + (QPEL_EXTRA_BEFORE << sps->pixel_shift);
1767
1768 481809 s->vdsp.emulated_edge_mc(lc->edge_emu_buffer, src - offset,
1769 edge_emu_stride, srcstride,
1770 block_w + QPEL_EXTRA,
1771 block_h + QPEL_EXTRA,
1772 x_off - QPEL_EXTRA_BEFORE, y_off - QPEL_EXTRA_BEFORE,
1773 pic_width, pic_height);
1774 481809 src = lc->edge_emu_buffer + buf_offset;
1775 481809 srcstride = edge_emu_stride;
1776 }
1777
1778
2/2
✓ Branch 0 taken 6208690 times.
✓ Branch 1 taken 133864 times.
6342554 if (!weight_flag)
1779 6208690 s->hevcdsp.put_hevc_qpel_uni[idx][!!my][!!mx](dst, dststride, src, srcstride,
1780 block_h, mx, my, block_w);
1781 else
1782 133864 s->hevcdsp.put_hevc_qpel_uni_w[idx][!!my][!!mx](dst, dststride, src, srcstride,
1783 133864 block_h, s->sh.luma_log2_weight_denom,
1784 luma_weight, luma_offset, mx, my, block_w);
1785 6342554 }
1786
1787 /**
1788 * 8.5.3.2.2.1 Luma sample bidirectional interpolation process
1789 *
1790 * @param s HEVC decoding context
1791 * @param dst target buffer for block data at block position
1792 * @param dststride stride of the dst buffer
1793 * @param ref0 reference picture0 buffer at origin (0, 0)
1794 * @param mv0 motion vector0 (relative to block position) to get pixel data from
1795 * @param x_off horizontal position of block from origin (0, 0)
1796 * @param y_off vertical position of block from origin (0, 0)
1797 * @param block_w width of block
1798 * @param block_h height of block
1799 * @param ref1 reference picture1 buffer at origin (0, 0)
1800 * @param mv1 motion vector1 (relative to block position) to get pixel data from
1801 * @param current_mv current motion vector structure
1802 */
1803 3992083 static void luma_mc_bi(HEVCLocalContext *lc,
1804 const HEVCPPS *pps, const HEVCSPS *sps,
1805 uint8_t *dst, ptrdiff_t dststride,
1806 const AVFrame *ref0, const Mv *mv0, int x_off, int y_off,
1807 int block_w, int block_h, const AVFrame *ref1,
1808 const Mv *mv1, struct MvField *current_mv)
1809 {
1810 3992083 const HEVCContext *const s = lc->parent;
1811 3992083 ptrdiff_t src0stride = ref0->linesize[0];
1812 3992083 ptrdiff_t src1stride = ref1->linesize[0];
1813 3992083 int pic_width = sps->width;
1814 3992083 int pic_height = sps->height;
1815 3992083 int mx0 = mv0->x & 3;
1816 3992083 int my0 = mv0->y & 3;
1817 3992083 int mx1 = mv1->x & 3;
1818 3992083 int my1 = mv1->y & 3;
1819
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 3992083 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
7984166 int weight_flag = (s->sh.slice_type == HEVC_SLICE_P && pps->weighted_pred_flag) ||
1820
3/4
✓ Branch 0 taken 3992083 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 73498 times.
✓ Branch 3 taken 3918585 times.
3992083 (s->sh.slice_type == HEVC_SLICE_B && pps->weighted_bipred_flag);
1821 3992083 int x_off0 = x_off + (mv0->x >> 2);
1822 3992083 int y_off0 = y_off + (mv0->y >> 2);
1823 3992083 int x_off1 = x_off + (mv1->x >> 2);
1824 3992083 int y_off1 = y_off + (mv1->y >> 2);
1825 3992083 int idx = hevc_pel_weight[block_w];
1826
1827 3992083 const uint8_t *src0 = ref0->data[0] + y_off0 * src0stride + (int)((unsigned)x_off0 << sps->pixel_shift);
1828 3992083 const uint8_t *src1 = ref1->data[0] + y_off1 * src1stride + (int)((unsigned)x_off1 << sps->pixel_shift);
1829
1830
4/4
✓ Branch 0 taken 3934283 times.
✓ Branch 1 taken 57800 times.
✓ Branch 2 taken 3826999 times.
✓ Branch 3 taken 107284 times.
3992083 if (x_off0 < QPEL_EXTRA_BEFORE || y_off0 < QPEL_EXTRA_AFTER ||
1831
2/2
✓ Branch 0 taken 3757951 times.
✓ Branch 1 taken 69048 times.
3826999 x_off0 >= pic_width - block_w - QPEL_EXTRA_AFTER ||
1832
2/2
✓ Branch 0 taken 209680 times.
✓ Branch 1 taken 3548271 times.
3757951 y_off0 >= pic_height - block_h - QPEL_EXTRA_AFTER) {
1833 443812 const ptrdiff_t edge_emu_stride = EDGE_EMU_BUFFER_STRIDE << sps->pixel_shift;
1834 443812 int offset = QPEL_EXTRA_BEFORE * src0stride + (QPEL_EXTRA_BEFORE << sps->pixel_shift);
1835 443812 int buf_offset = QPEL_EXTRA_BEFORE * edge_emu_stride + (QPEL_EXTRA_BEFORE << sps->pixel_shift);
1836
1837 443812 s->vdsp.emulated_edge_mc(lc->edge_emu_buffer, src0 - offset,
1838 edge_emu_stride, src0stride,
1839 block_w + QPEL_EXTRA,
1840 block_h + QPEL_EXTRA,
1841 x_off0 - QPEL_EXTRA_BEFORE, y_off0 - QPEL_EXTRA_BEFORE,
1842 pic_width, pic_height);
1843 443812 src0 = lc->edge_emu_buffer + buf_offset;
1844 443812 src0stride = edge_emu_stride;
1845 }
1846
1847
4/4
✓ Branch 0 taken 3928081 times.
✓ Branch 1 taken 64002 times.
✓ Branch 2 taken 3821452 times.
✓ Branch 3 taken 106629 times.
3992083 if (x_off1 < QPEL_EXTRA_BEFORE || y_off1 < QPEL_EXTRA_AFTER ||
1848
2/2
✓ Branch 0 taken 3750497 times.
✓ Branch 1 taken 70955 times.
3821452 x_off1 >= pic_width - block_w - QPEL_EXTRA_AFTER ||
1849
2/2
✓ Branch 0 taken 212853 times.
✓ Branch 1 taken 3537644 times.
3750497 y_off1 >= pic_height - block_h - QPEL_EXTRA_AFTER) {
1850 454439 const ptrdiff_t edge_emu_stride = EDGE_EMU_BUFFER_STRIDE << sps->pixel_shift;
1851 454439 int offset = QPEL_EXTRA_BEFORE * src1stride + (QPEL_EXTRA_BEFORE << sps->pixel_shift);
1852 454439 int buf_offset = QPEL_EXTRA_BEFORE * edge_emu_stride + (QPEL_EXTRA_BEFORE << sps->pixel_shift);
1853
1854 454439 s->vdsp.emulated_edge_mc(lc->edge_emu_buffer2, src1 - offset,
1855 edge_emu_stride, src1stride,
1856 block_w + QPEL_EXTRA,
1857 block_h + QPEL_EXTRA,
1858 x_off1 - QPEL_EXTRA_BEFORE, y_off1 - QPEL_EXTRA_BEFORE,
1859 pic_width, pic_height);
1860 454439 src1 = lc->edge_emu_buffer2 + buf_offset;
1861 454439 src1stride = edge_emu_stride;
1862 }
1863
1864 3992083 s->hevcdsp.put_hevc_qpel[idx][!!my0][!!mx0](lc->tmp, src0, src0stride,
1865 block_h, mx0, my0, block_w);
1866
2/2
✓ Branch 0 taken 3918585 times.
✓ Branch 1 taken 73498 times.
3992083 if (!weight_flag)
1867 3918585 s->hevcdsp.put_hevc_qpel_bi[idx][!!my1][!!mx1](dst, dststride, src1, src1stride, lc->tmp,
1868 block_h, mx1, my1, block_w);
1869 else
1870 73498 s->hevcdsp.put_hevc_qpel_bi_w[idx][!!my1][!!mx1](dst, dststride, src1, src1stride, lc->tmp,
1871 73498 block_h, s->sh.luma_log2_weight_denom,
1872 73498 s->sh.luma_weight_l0[current_mv->ref_idx[0]],
1873 73498 s->sh.luma_weight_l1[current_mv->ref_idx[1]],
1874 73498 s->sh.luma_offset_l0[current_mv->ref_idx[0]] +
1875 73498 s->sh.luma_offset_l1[current_mv->ref_idx[1]],
1876 mx1, my1, block_w);
1877
1878 3992083 }
1879
1880 /**
1881 * 8.5.3.2.2.2 Chroma sample uniprediction interpolation process
1882 *
1883 * @param s HEVC decoding context
1884 * @param dst1 target buffer for block data at block position (U plane)
1885 * @param dst2 target buffer for block data at block position (V plane)
1886 * @param dststride stride of the dst1 and dst2 buffers
1887 * @param ref reference picture buffer at origin (0, 0)
1888 * @param mv motion vector (relative to block position) to get pixel data from
1889 * @param x_off horizontal position of block from origin (0, 0)
1890 * @param y_off vertical position of block from origin (0, 0)
1891 * @param block_w width of block
1892 * @param block_h height of block
1893 * @param chroma_weight weighting factor applied to the chroma prediction
1894 * @param chroma_offset additive offset applied to the chroma prediction value
1895 */
1896
1897 12685108 static void chroma_mc_uni(HEVCLocalContext *lc,
1898 const HEVCPPS *pps, const HEVCSPS *sps,
1899 uint8_t *dst0,
1900 ptrdiff_t dststride, const uint8_t *src0, ptrdiff_t srcstride, int reflist,
1901 int x_off, int y_off, int block_w, int block_h,
1902 const struct MvField *current_mv, int chroma_weight, int chroma_offset)
1903 {
1904 12685108 const HEVCContext *const s = lc->parent;
1905 12685108 int pic_width = sps->width >> sps->hshift[1];
1906 12685108 int pic_height = sps->height >> sps->vshift[1];
1907 12685108 const Mv *mv = &current_mv->mv[reflist];
1908
4/4
✓ Branch 0 taken 6561654 times.
✓ Branch 1 taken 6123454 times.
✓ Branch 2 taken 6440478 times.
✓ Branch 3 taken 121176 times.
25249040 int weight_flag = (s->sh.slice_type == HEVC_SLICE_P && pps->weighted_pred_flag) ||
1909
4/4
✓ Branch 0 taken 6123454 times.
✓ Branch 1 taken 6440478 times.
✓ Branch 2 taken 146552 times.
✓ Branch 3 taken 5976902 times.
12563932 (s->sh.slice_type == HEVC_SLICE_B && pps->weighted_bipred_flag);
1910 12685108 int idx = hevc_pel_weight[block_w];
1911 12685108 int hshift = sps->hshift[1];
1912 12685108 int vshift = sps->vshift[1];
1913 12685108 intptr_t mx = av_zero_extend(mv->x, 2 + hshift);
1914 12685108 intptr_t my = av_zero_extend(mv->y, 2 + vshift);
1915 12685108 intptr_t _mx = mx << (1 - hshift);
1916 12685108 intptr_t _my = my << (1 - vshift);
1917
2/4
✓ Branch 0 taken 12685108 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 12685108 times.
12685108 int emu = src0 == s->cur_frame->f->data[1] || src0 == s->cur_frame->f->data[2];
1918
1919 12685108 x_off += mv->x >> (2 + hshift);
1920 12685108 y_off += mv->y >> (2 + vshift);
1921 12685108 src0 += y_off * srcstride + (x_off * (1 << sps->pixel_shift));
1922
1923
4/4
✓ Branch 0 taken 12562024 times.
✓ Branch 1 taken 123084 times.
✓ Branch 2 taken 12322816 times.
✓ Branch 3 taken 239208 times.
12685108 if (x_off < EPEL_EXTRA_BEFORE || y_off < EPEL_EXTRA_AFTER ||
1924
2/2
✓ Branch 0 taken 12170872 times.
✓ Branch 1 taken 151944 times.
12322816 x_off >= pic_width - block_w - EPEL_EXTRA_AFTER ||
1925
3/4
✓ Branch 0 taken 11734450 times.
✓ Branch 1 taken 436422 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 11734450 times.
12170872 y_off >= pic_height - block_h - EPEL_EXTRA_AFTER ||
1926 emu) {
1927 950658 const int edge_emu_stride = EDGE_EMU_BUFFER_STRIDE << sps->pixel_shift;
1928 950658 int offset0 = EPEL_EXTRA_BEFORE * (srcstride + (1 << sps->pixel_shift));
1929 950658 int buf_offset0 = EPEL_EXTRA_BEFORE *
1930 950658 (edge_emu_stride + (1 << sps->pixel_shift));
1931 950658 s->vdsp.emulated_edge_mc(lc->edge_emu_buffer, src0 - offset0,
1932 edge_emu_stride, srcstride,
1933 block_w + EPEL_EXTRA, block_h + EPEL_EXTRA,
1934 x_off - EPEL_EXTRA_BEFORE,
1935 y_off - EPEL_EXTRA_BEFORE,
1936 pic_width, pic_height);
1937
1938 950658 src0 = lc->edge_emu_buffer + buf_offset0;
1939 950658 srcstride = edge_emu_stride;
1940 }
1941
2/2
✓ Branch 0 taken 12417380 times.
✓ Branch 1 taken 267728 times.
12685108 if (!weight_flag)
1942 12417380 s->hevcdsp.put_hevc_epel_uni[idx][!!my][!!mx](dst0, dststride, src0, srcstride,
1943 block_h, _mx, _my, block_w);
1944 else
1945 267728 s->hevcdsp.put_hevc_epel_uni_w[idx][!!my][!!mx](dst0, dststride, src0, srcstride,
1946 267728 block_h, s->sh.chroma_log2_weight_denom,
1947 chroma_weight, chroma_offset, _mx, _my, block_w);
1948 12685108 }
1949
1950 /**
1951 * 8.5.3.2.2.2 Chroma sample bidirectional interpolation process
1952 *
1953 * @param s HEVC decoding context
1954 * @param dst target buffer for block data at block position
1955 * @param dststride stride of the dst buffer
1956 * @param ref0 reference picture0 buffer at origin (0, 0)
1957 * @param mv0 motion vector0 (relative to block position) to get pixel data from
1958 * @param x_off horizontal position of block from origin (0, 0)
1959 * @param y_off vertical position of block from origin (0, 0)
1960 * @param block_w width of block
1961 * @param block_h height of block
1962 * @param ref1 reference picture1 buffer at origin (0, 0)
1963 * @param mv1 motion vector1 (relative to block position) to get pixel data from
1964 * @param current_mv current motion vector structure
1965 * @param cidx chroma component(cb, cr)
1966 */
1967 7984166 static void chroma_mc_bi(HEVCLocalContext *lc,
1968 const HEVCPPS *pps, const HEVCSPS *sps,
1969 uint8_t *dst0, ptrdiff_t dststride,
1970 const AVFrame *ref0, const AVFrame *ref1,
1971 int x_off, int y_off, int block_w, int block_h, const MvField *current_mv, int cidx)
1972 {
1973 7984166 const HEVCContext *const s = lc->parent;
1974 7984166 const uint8_t *src1 = ref0->data[cidx+1];
1975 7984166 const uint8_t *src2 = ref1->data[cidx+1];
1976 7984166 ptrdiff_t src1stride = ref0->linesize[cidx+1];
1977 7984166 ptrdiff_t src2stride = ref1->linesize[cidx+1];
1978
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 7984166 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
15968332 int weight_flag = (s->sh.slice_type == HEVC_SLICE_P && pps->weighted_pred_flag) ||
1979
3/4
✓ Branch 0 taken 7984166 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 146996 times.
✓ Branch 3 taken 7837170 times.
7984166 (s->sh.slice_type == HEVC_SLICE_B && pps->weighted_bipred_flag);
1980 7984166 int pic_width = sps->width >> sps->hshift[1];
1981 7984166 int pic_height = sps->height >> sps->vshift[1];
1982 7984166 const Mv *const mv0 = &current_mv->mv[0];
1983 7984166 const Mv *const mv1 = &current_mv->mv[1];
1984 7984166 int hshift = sps->hshift[1];
1985 7984166 int vshift = sps->vshift[1];
1986
1987 7984166 intptr_t mx0 = av_zero_extend(mv0->x, 2 + hshift);
1988 7984166 intptr_t my0 = av_zero_extend(mv0->y, 2 + vshift);
1989 7984166 intptr_t mx1 = av_zero_extend(mv1->x, 2 + hshift);
1990 7984166 intptr_t my1 = av_zero_extend(mv1->y, 2 + vshift);
1991 7984166 intptr_t _mx0 = mx0 << (1 - hshift);
1992 7984166 intptr_t _my0 = my0 << (1 - vshift);
1993 7984166 intptr_t _mx1 = mx1 << (1 - hshift);
1994 7984166 intptr_t _my1 = my1 << (1 - vshift);
1995
1996 7984166 int x_off0 = x_off + (mv0->x >> (2 + hshift));
1997 7984166 int y_off0 = y_off + (mv0->y >> (2 + vshift));
1998 7984166 int x_off1 = x_off + (mv1->x >> (2 + hshift));
1999 7984166 int y_off1 = y_off + (mv1->y >> (2 + vshift));
2000 7984166 int idx = hevc_pel_weight[block_w];
2001 7984166 src1 += y_off0 * src1stride + (int)((unsigned)x_off0 << sps->pixel_shift);
2002 7984166 src2 += y_off1 * src2stride + (int)((unsigned)x_off1 << sps->pixel_shift);
2003
2004
4/4
✓ Branch 0 taken 7874824 times.
✓ Branch 1 taken 109342 times.
✓ Branch 2 taken 7660144 times.
✓ Branch 3 taken 214680 times.
7984166 if (x_off0 < EPEL_EXTRA_BEFORE || y_off0 < EPEL_EXTRA_AFTER ||
2005
2/2
✓ Branch 0 taken 7522052 times.
✓ Branch 1 taken 138092 times.
7660144 x_off0 >= pic_width - block_w - EPEL_EXTRA_AFTER ||
2006
2/2
✓ Branch 0 taken 418772 times.
✓ Branch 1 taken 7103280 times.
7522052 y_off0 >= pic_height - block_h - EPEL_EXTRA_AFTER) {
2007 880886 const int edge_emu_stride = EDGE_EMU_BUFFER_STRIDE << sps->pixel_shift;
2008 880886 int offset1 = EPEL_EXTRA_BEFORE * (src1stride + (1 << sps->pixel_shift));
2009 880886 int buf_offset1 = EPEL_EXTRA_BEFORE *
2010 880886 (edge_emu_stride + (1 << sps->pixel_shift));
2011
2012 880886 s->vdsp.emulated_edge_mc(lc->edge_emu_buffer, src1 - offset1,
2013 edge_emu_stride, src1stride,
2014 block_w + EPEL_EXTRA, block_h + EPEL_EXTRA,
2015 x_off0 - EPEL_EXTRA_BEFORE,
2016 y_off0 - EPEL_EXTRA_BEFORE,
2017 pic_width, pic_height);
2018
2019 880886 src1 = lc->edge_emu_buffer + buf_offset1;
2020 880886 src1stride = edge_emu_stride;
2021 }
2022
2023
4/4
✓ Branch 0 taken 7861558 times.
✓ Branch 1 taken 122608 times.
✓ Branch 2 taken 7648578 times.
✓ Branch 3 taken 212980 times.
7984166 if (x_off1 < EPEL_EXTRA_BEFORE || y_off1 < EPEL_EXTRA_AFTER ||
2024
2/2
✓ Branch 0 taken 7506674 times.
✓ Branch 1 taken 141904 times.
7648578 x_off1 >= pic_width - block_w - EPEL_EXTRA_AFTER ||
2025
2/2
✓ Branch 0 taken 425380 times.
✓ Branch 1 taken 7081294 times.
7506674 y_off1 >= pic_height - block_h - EPEL_EXTRA_AFTER) {
2026 902872 const int edge_emu_stride = EDGE_EMU_BUFFER_STRIDE << sps->pixel_shift;
2027 902872 int offset1 = EPEL_EXTRA_BEFORE * (src2stride + (1 << sps->pixel_shift));
2028 902872 int buf_offset1 = EPEL_EXTRA_BEFORE *
2029 902872 (edge_emu_stride + (1 << sps->pixel_shift));
2030
2031 902872 s->vdsp.emulated_edge_mc(lc->edge_emu_buffer2, src2 - offset1,
2032 edge_emu_stride, src2stride,
2033 block_w + EPEL_EXTRA, block_h + EPEL_EXTRA,
2034 x_off1 - EPEL_EXTRA_BEFORE,
2035 y_off1 - EPEL_EXTRA_BEFORE,
2036 pic_width, pic_height);
2037
2038 902872 src2 = lc->edge_emu_buffer2 + buf_offset1;
2039 902872 src2stride = edge_emu_stride;
2040 }
2041
2042 7984166 s->hevcdsp.put_hevc_epel[idx][!!my0][!!mx0](lc->tmp, src1, src1stride,
2043 block_h, _mx0, _my0, block_w);
2044
2/2
✓ Branch 0 taken 7837170 times.
✓ Branch 1 taken 146996 times.
7984166 if (!weight_flag)
2045 7837170 s->hevcdsp.put_hevc_epel_bi[idx][!!my1][!!mx1](dst0, s->cur_frame->f->linesize[cidx+1],
2046 7837170 src2, src2stride, lc->tmp,
2047 block_h, _mx1, _my1, block_w);
2048 else
2049 146996 s->hevcdsp.put_hevc_epel_bi_w[idx][!!my1][!!mx1](dst0, s->cur_frame->f->linesize[cidx+1],
2050 146996 src2, src2stride, lc->tmp,
2051 block_h,
2052 146996 s->sh.chroma_log2_weight_denom,
2053 146996 s->sh.chroma_weight_l0[current_mv->ref_idx[0]][cidx],
2054 146996 s->sh.chroma_weight_l1[current_mv->ref_idx[1]][cidx],
2055 146996 s->sh.chroma_offset_l0[current_mv->ref_idx[0]][cidx] +
2056 146996 s->sh.chroma_offset_l1[current_mv->ref_idx[1]][cidx],
2057 _mx1, _my1, block_w);
2058 7984166 }
2059
2060 14326720 static void hevc_await_progress(const HEVCContext *s, const HEVCFrame *ref,
2061 const Mv *mv, int y0, int height)
2062 {
2063
2/2
✓ Branch 0 taken 111132 times.
✓ Branch 1 taken 14215588 times.
14326720 if (s->avctx->active_thread_type == FF_THREAD_FRAME ) {
2064 111132 int y = FFMAX(0, (mv->y >> 2) + y0 + height + 9);
2065
2066 111132 ff_progress_frame_await(&ref->tf, y);
2067 }
2068 14326720 }
2069
2070 2833077 static void hevc_luma_mv_mvp_mode(HEVCLocalContext *lc,
2071 const HEVCPPS *pps, const HEVCSPS *sps,
2072 int x0, int y0, int nPbW,
2073 int nPbH, int log2_cb_size, int part_idx,
2074 int merge_idx, MvField *mv)
2075 {
2076 2833077 const HEVCContext *const s = lc->parent;
2077 2833077 enum InterPredIdc inter_pred_idc = PRED_L0;
2078 int mvp_flag;
2079
2080 2833077 ff_hevc_set_neighbour_available(lc, x0, y0, nPbW, nPbH, sps->log2_ctb_size);
2081 2833077 mv->pred_flag = 0;
2082
2/2
✓ Branch 0 taken 1922844 times.
✓ Branch 1 taken 910233 times.
2833077 if (s->sh.slice_type == HEVC_SLICE_B)
2083 1922844 inter_pred_idc = ff_hevc_inter_pred_idc_decode(lc, nPbW, nPbH);
2084
2085
2/2
✓ Branch 0 taken 2423759 times.
✓ Branch 1 taken 409318 times.
2833077 if (inter_pred_idc != PRED_L1) {
2086
1/2
✓ Branch 0 taken 2423759 times.
✗ Branch 1 not taken.
2423759 if (s->sh.nb_refs[L0])
2087 2423759 mv->ref_idx[0]= ff_hevc_ref_idx_lx_decode(lc, s->sh.nb_refs[L0]);
2088
2089 2423759 mv->pred_flag = PF_L0;
2090 2423759 ff_hevc_hls_mvd_coding(lc, x0, y0, 0);
2091 2423759 mvp_flag = ff_hevc_mvp_lx_flag_decode(lc);
2092 2423759 ff_hevc_luma_mv_mvp_mode(lc, pps, x0, y0, nPbW, nPbH, log2_cb_size,
2093 part_idx, merge_idx, mv, mvp_flag, 0);
2094 2423759 mv->mv[0].x += lc->pu.mvd.x;
2095 2423759 mv->mv[0].y += lc->pu.mvd.y;
2096 }
2097
2098
2/2
✓ Branch 0 taken 834243 times.
✓ Branch 1 taken 1998834 times.
2833077 if (inter_pred_idc != PRED_L0) {
2099
1/2
✓ Branch 0 taken 834243 times.
✗ Branch 1 not taken.
834243 if (s->sh.nb_refs[L1])
2100 834243 mv->ref_idx[1]= ff_hevc_ref_idx_lx_decode(lc, s->sh.nb_refs[L1]);
2101
2102
4/4
✓ Branch 0 taken 193172 times.
✓ Branch 1 taken 641071 times.
✓ Branch 2 taken 163605 times.
✓ Branch 3 taken 29567 times.
834243 if (s->sh.mvd_l1_zero_flag == 1 && inter_pred_idc == PRED_BI) {
2103 163605 AV_ZERO32(&lc->pu.mvd);
2104 } else {
2105 670638 ff_hevc_hls_mvd_coding(lc, x0, y0, 1);
2106 }
2107
2108 834243 mv->pred_flag += PF_L1;
2109 834243 mvp_flag = ff_hevc_mvp_lx_flag_decode(lc);
2110 834243 ff_hevc_luma_mv_mvp_mode(lc, pps, x0, y0, nPbW, nPbH, log2_cb_size,
2111 part_idx, merge_idx, mv, mvp_flag, 1);
2112 834243 mv->mv[1].x += lc->pu.mvd.x;
2113 834243 mv->mv[1].y += lc->pu.mvd.y;
2114 }
2115 2833077 }
2116
2117 10334637 static void hls_prediction_unit(HEVCLocalContext *lc,
2118 const HEVCLayerContext *l,
2119 const HEVCPPS *pps, const HEVCSPS *sps,
2120 int x0, int y0, int nPbW, int nPbH,
2121 int log2_cb_size, int partIdx, int idx)
2122 {
2123 #define POS(c_idx, x, y) \
2124 s->cur_frame->f->data[c_idx] ? \
2125 &s->cur_frame->f->data[c_idx][((y) >> sps->vshift[c_idx]) * linesize[c_idx] + \
2126 (((x) >> sps->hshift[c_idx]) << sps->pixel_shift)] : NULL
2127 10334637 const HEVCContext *const s = lc->parent;
2128 10334637 int merge_idx = 0;
2129 10334637 struct MvField current_mv = {{{ 0 }}};
2130
2131 10334637 int min_pu_width = sps->min_pu_width;
2132
2133 10334637 MvField *tab_mvf = s->cur_frame->tab_mvf;
2134 10334637 const RefPicList *refPicList = s->cur_frame->refPicList;
2135 10334637 const HEVCFrame *ref0 = NULL, *ref1 = NULL;
2136 10334637 const int *linesize = s->cur_frame->f->linesize;
2137 10334637 uint8_t *dst0 = s->cur_frame->f->data[0] + y0 * linesize[0] + (x0 << sps->pixel_shift);
2138
1/2
✓ Branch 0 taken 10334637 times.
✗ Branch 1 not taken.
10334637 uint8_t *dst1 = POS(1, x0, y0);
2139
1/2
✓ Branch 0 taken 10334637 times.
✗ Branch 1 not taken.
10334637 uint8_t *dst2 = POS(2, x0, y0);
2140 10334637 int log2_min_cb_size = sps->log2_min_cb_size;
2141 10334637 int min_cb_width = sps->min_cb_width;
2142 10334637 int x_cb = x0 >> log2_min_cb_size;
2143 10334637 int y_cb = y0 >> log2_min_cb_size;
2144 int x_pu, y_pu;
2145 int i, j;
2146
2147 10334637 int skip_flag = SAMPLE_CTB(l->skip_flag, x_cb, y_cb);
2148
2149
2/2
✓ Branch 0 taken 5769030 times.
✓ Branch 1 taken 4565607 times.
10334637 if (!skip_flag)
2150 5769030 lc->pu.merge_flag = ff_hevc_merge_flag_decode(lc);
2151
2152
4/4
✓ Branch 0 taken 5769030 times.
✓ Branch 1 taken 4565607 times.
✓ Branch 2 taken 2935953 times.
✓ Branch 3 taken 2833077 times.
10334637 if (skip_flag || lc->pu.merge_flag) {
2153
2/2
✓ Branch 0 taken 7424707 times.
✓ Branch 1 taken 76853 times.
7501560 if (s->sh.max_num_merge_cand > 1)
2154 7424707 merge_idx = ff_hevc_merge_idx_decode(lc);
2155 else
2156 76853 merge_idx = 0;
2157
2158 7501560 ff_hevc_luma_mv_merge_mode(lc, pps, x0, y0, nPbW, nPbH, log2_cb_size,
2159 partIdx, merge_idx, &current_mv);
2160 } else {
2161 2833077 hevc_luma_mv_mvp_mode(lc, pps, sps, x0, y0, nPbW, nPbH, log2_cb_size,
2162 partIdx, merge_idx, &current_mv);
2163 }
2164
2165 10334637 x_pu = x0 >> sps->log2_min_pu_size;
2166 10334637 y_pu = y0 >> sps->log2_min_pu_size;
2167
2168
2/2
✓ Branch 0 taken 40422698 times.
✓ Branch 1 taken 10334637 times.
50757335 for (j = 0; j < nPbH >> sps->log2_min_pu_size; j++)
2169
2/2
✓ Branch 0 taken 258353932 times.
✓ Branch 1 taken 40422698 times.
298776630 for (i = 0; i < nPbW >> sps->log2_min_pu_size; i++)
2170 258353932 tab_mvf[(y_pu + j) * min_pu_width + x_pu + i] = current_mv;
2171
2172
2/2
✓ Branch 0 taken 9515439 times.
✓ Branch 1 taken 819198 times.
10334637 if (current_mv.pred_flag & PF_L0) {
2173 9515439 ref0 = refPicList[0].ref[current_mv.ref_idx[0]];
2174
2/4
✓ Branch 0 taken 9515439 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 9515439 times.
9515439 if (!ref0 || !ref0->f)
2175 return;
2176 9515439 hevc_await_progress(s, ref0, &current_mv.mv[0], y0, nPbH);
2177 }
2178
2/2
✓ Branch 0 taken 4811281 times.
✓ Branch 1 taken 5523356 times.
10334637 if (current_mv.pred_flag & PF_L1) {
2179 4811281 ref1 = refPicList[1].ref[current_mv.ref_idx[1]];
2180
2/4
✓ Branch 0 taken 4811281 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 4811281 times.
4811281 if (!ref1 || !ref1->f)
2181 return;
2182 4811281 hevc_await_progress(s, ref1, &current_mv.mv[1], y0, nPbH);
2183 }
2184
2185
2/2
✓ Branch 0 taken 5523356 times.
✓ Branch 1 taken 4811281 times.
10334637 if (current_mv.pred_flag == PF_L0) {
2186 5523356 int x0_c = x0 >> sps->hshift[1];
2187 5523356 int y0_c = y0 >> sps->vshift[1];
2188 5523356 int nPbW_c = nPbW >> sps->hshift[1];
2189 5523356 int nPbH_c = nPbH >> sps->vshift[1];
2190
2191 5523356 luma_mc_uni(lc, pps, sps, dst0, linesize[0], ref0->f,
2192 &current_mv.mv[0], x0, y0, nPbW, nPbH,
2193 5523356 s->sh.luma_weight_l0[current_mv.ref_idx[0]],
2194 5523356 s->sh.luma_offset_l0[current_mv.ref_idx[0]]);
2195
2196
1/2
✓ Branch 0 taken 5523356 times.
✗ Branch 1 not taken.
5523356 if (sps->chroma_format_idc) {
2197 5523356 chroma_mc_uni(lc, pps, sps, dst1, linesize[1], ref0->f->data[1], ref0->f->linesize[1],
2198 0, x0_c, y0_c, nPbW_c, nPbH_c, &current_mv,
2199 5523356 s->sh.chroma_weight_l0[current_mv.ref_idx[0]][0], s->sh.chroma_offset_l0[current_mv.ref_idx[0]][0]);
2200 5523356 chroma_mc_uni(lc, pps, sps, dst2, linesize[2], ref0->f->data[2], ref0->f->linesize[2],
2201 0, x0_c, y0_c, nPbW_c, nPbH_c, &current_mv,
2202 5523356 s->sh.chroma_weight_l0[current_mv.ref_idx[0]][1], s->sh.chroma_offset_l0[current_mv.ref_idx[0]][1]);
2203 }
2204
2/2
✓ Branch 0 taken 819198 times.
✓ Branch 1 taken 3992083 times.
4811281 } else if (current_mv.pred_flag == PF_L1) {
2205 819198 int x0_c = x0 >> sps->hshift[1];
2206 819198 int y0_c = y0 >> sps->vshift[1];
2207 819198 int nPbW_c = nPbW >> sps->hshift[1];
2208 819198 int nPbH_c = nPbH >> sps->vshift[1];
2209
2210 819198 luma_mc_uni(lc, pps, sps, dst0, linesize[0], ref1->f,
2211 &current_mv.mv[1], x0, y0, nPbW, nPbH,
2212 819198 s->sh.luma_weight_l1[current_mv.ref_idx[1]],
2213 819198 s->sh.luma_offset_l1[current_mv.ref_idx[1]]);
2214
2215
1/2
✓ Branch 0 taken 819198 times.
✗ Branch 1 not taken.
819198 if (sps->chroma_format_idc) {
2216 819198 chroma_mc_uni(lc, pps, sps, dst1, linesize[1], ref1->f->data[1], ref1->f->linesize[1],
2217 1, x0_c, y0_c, nPbW_c, nPbH_c, &current_mv,
2218 819198 s->sh.chroma_weight_l1[current_mv.ref_idx[1]][0], s->sh.chroma_offset_l1[current_mv.ref_idx[1]][0]);
2219
2220 819198 chroma_mc_uni(lc, pps, sps, dst2, linesize[2], ref1->f->data[2], ref1->f->linesize[2],
2221 1, x0_c, y0_c, nPbW_c, nPbH_c, &current_mv,
2222 819198 s->sh.chroma_weight_l1[current_mv.ref_idx[1]][1], s->sh.chroma_offset_l1[current_mv.ref_idx[1]][1]);
2223 }
2224
1/2
✓ Branch 0 taken 3992083 times.
✗ Branch 1 not taken.
3992083 } else if (current_mv.pred_flag == PF_BI) {
2225 3992083 int x0_c = x0 >> sps->hshift[1];
2226 3992083 int y0_c = y0 >> sps->vshift[1];
2227 3992083 int nPbW_c = nPbW >> sps->hshift[1];
2228 3992083 int nPbH_c = nPbH >> sps->vshift[1];
2229
2230 3992083 luma_mc_bi(lc, pps, sps, dst0, linesize[0], ref0->f,
2231 &current_mv.mv[0], x0, y0, nPbW, nPbH,
2232 3992083 ref1->f, &current_mv.mv[1], &current_mv);
2233
2234
1/2
✓ Branch 0 taken 3992083 times.
✗ Branch 1 not taken.
3992083 if (sps->chroma_format_idc) {
2235 3992083 chroma_mc_bi(lc, pps, sps, dst1, linesize[1], ref0->f, ref1->f,
2236 x0_c, y0_c, nPbW_c, nPbH_c, &current_mv, 0);
2237
2238 3992083 chroma_mc_bi(lc, pps, sps, dst2, linesize[2], ref0->f, ref1->f,
2239 x0_c, y0_c, nPbW_c, nPbH_c, &current_mv, 1);
2240 }
2241 }
2242 }
2243
2244 /**
2245 * 8.4.1
2246 */
2247 6761317 static int luma_intra_pred_mode(HEVCLocalContext *lc, const HEVCLayerContext *l,
2248 const HEVCSPS *sps,
2249 int x0, int y0, int pu_size,
2250 int prev_intra_luma_pred_flag)
2251 {
2252 6761317 const HEVCContext *const s = lc->parent;
2253 6761317 int x_pu = x0 >> sps->log2_min_pu_size;
2254 6761317 int y_pu = y0 >> sps->log2_min_pu_size;
2255 6761317 int min_pu_width = sps->min_pu_width;
2256 6761317 int size_in_pus = pu_size >> sps->log2_min_pu_size;
2257 6761317 int x0b = av_zero_extend(x0, sps->log2_ctb_size);
2258 6761317 int y0b = av_zero_extend(y0, sps->log2_ctb_size);
2259
2260
2/2
✓ Branch 0 taken 896869 times.
✓ Branch 1 taken 105287 times.
1002156 int cand_up = (lc->ctb_up_flag || y0b) ?
2261
2/2
✓ Branch 0 taken 1002156 times.
✓ Branch 1 taken 5759161 times.
7763473 l->tab_ipm[(y_pu - 1) * min_pu_width + x_pu] : INTRA_DC;
2262
2/2
✓ Branch 0 taken 485793 times.
✓ Branch 1 taken 68222 times.
554015 int cand_left = (lc->ctb_left_flag || x0b) ?
2263
2/2
✓ Branch 0 taken 554015 times.
✓ Branch 1 taken 6207302 times.
7315332 l->tab_ipm[y_pu * min_pu_width + x_pu - 1] : INTRA_DC;
2264
2265 6761317 int y_ctb = (y0 >> (sps->log2_ctb_size)) << (sps->log2_ctb_size);
2266
2267 6761317 MvField *tab_mvf = s->cur_frame->tab_mvf;
2268 int intra_pred_mode;
2269 int candidate[3];
2270 int i, j;
2271
2272 // intra_pred_mode prediction does not cross vertical CTB boundaries
2273
2/2
✓ Branch 0 taken 906591 times.
✓ Branch 1 taken 5854726 times.
6761317 if ((y0 - 1) < y_ctb)
2274 906591 cand_up = INTRA_DC;
2275
2276
2/2
✓ Branch 0 taken 1510566 times.
✓ Branch 1 taken 5250751 times.
6761317 if (cand_left == cand_up) {
2277
2/2
✓ Branch 0 taken 969747 times.
✓ Branch 1 taken 540819 times.
1510566 if (cand_left < 2) {
2278 969747 candidate[0] = INTRA_PLANAR;
2279 969747 candidate[1] = INTRA_DC;
2280 969747 candidate[2] = INTRA_ANGULAR_26;
2281 } else {
2282 540819 candidate[0] = cand_left;
2283 540819 candidate[1] = 2 + ((cand_left - 2 - 1 + 32) & 31);
2284 540819 candidate[2] = 2 + ((cand_left - 2 + 1) & 31);
2285 }
2286 } else {
2287 5250751 candidate[0] = cand_left;
2288 5250751 candidate[1] = cand_up;
2289
4/4
✓ Branch 0 taken 4356599 times.
✓ Branch 1 taken 894152 times.
✓ Branch 2 taken 3600619 times.
✓ Branch 3 taken 755980 times.
5250751 if (candidate[0] != INTRA_PLANAR && candidate[1] != INTRA_PLANAR) {
2290 3600619 candidate[2] = INTRA_PLANAR;
2291
4/4
✓ Branch 0 taken 1434773 times.
✓ Branch 1 taken 215359 times.
✓ Branch 2 taken 1069940 times.
✓ Branch 3 taken 364833 times.
1650132 } else if (candidate[0] != INTRA_DC && candidate[1] != INTRA_DC) {
2292 1069940 candidate[2] = INTRA_DC;
2293 } else {
2294 580192 candidate[2] = INTRA_ANGULAR_26;
2295 }
2296 }
2297
2298
2/2
✓ Branch 0 taken 4012676 times.
✓ Branch 1 taken 2748641 times.
6761317 if (prev_intra_luma_pred_flag) {
2299 4012676 intra_pred_mode = candidate[lc->pu.mpm_idx];
2300 } else {
2301
2/2
✓ Branch 0 taken 1262937 times.
✓ Branch 1 taken 1485704 times.
2748641 if (candidate[0] > candidate[1])
2302 1262937 FFSWAP(uint8_t, candidate[0], candidate[1]);
2303
2/2
✓ Branch 0 taken 1587166 times.
✓ Branch 1 taken 1161475 times.
2748641 if (candidate[0] > candidate[2])
2304 1587166 FFSWAP(uint8_t, candidate[0], candidate[2]);
2305
2/2
✓ Branch 0 taken 1982612 times.
✓ Branch 1 taken 766029 times.
2748641 if (candidate[1] > candidate[2])
2306 1982612 FFSWAP(uint8_t, candidate[1], candidate[2]);
2307
2308 2748641 intra_pred_mode = lc->pu.rem_intra_luma_pred_mode;
2309
2/2
✓ Branch 0 taken 8245923 times.
✓ Branch 1 taken 2748641 times.
10994564 for (i = 0; i < 3; i++)
2310
2/2
✓ Branch 0 taken 6015586 times.
✓ Branch 1 taken 2230337 times.
8245923 if (intra_pred_mode >= candidate[i])
2311 6015586 intra_pred_mode++;
2312 }
2313
2314 /* write the intra prediction units into the mv array */
2315
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6761317 times.
6761317 if (!size_in_pus)
2316 size_in_pus = 1;
2317
2/2
✓ Branch 0 taken 12295582 times.
✓ Branch 1 taken 6761317 times.
19056899 for (i = 0; i < size_in_pus; i++) {
2318 12295582 memset(&l->tab_ipm[(y_pu + i) * min_pu_width + x_pu],
2319 intra_pred_mode, size_in_pus);
2320
2321
2/2
✓ Branch 0 taken 41237824 times.
✓ Branch 1 taken 12295582 times.
53533406 for (j = 0; j < size_in_pus; j++) {
2322 41237824 tab_mvf[(y_pu + j) * min_pu_width + x_pu + i].pred_flag = PF_INTRA;
2323 }
2324 }
2325
2326 6761317 return intra_pred_mode;
2327 }
2328
2329 12248004 static av_always_inline void set_ct_depth(const HEVCSPS *sps, uint8_t *tab_ct_depth,
2330 int x0, int y0,
2331 int log2_cb_size, int ct_depth)
2332 {
2333 12248004 int length = (1 << log2_cb_size) >> sps->log2_min_cb_size;
2334 12248004 int x_cb = x0 >> sps->log2_min_cb_size;
2335 12248004 int y_cb = y0 >> sps->log2_min_cb_size;
2336 int y;
2337
2338
2/2
✓ Branch 0 taken 23601676 times.
✓ Branch 1 taken 12248004 times.
35849680 for (y = 0; y < length; y++)
2339 23601676 memset(&tab_ct_depth[(y_cb + y) * sps->min_cb_width + x_cb],
2340 ct_depth, length);
2341 12248004 }
2342
2343 static const uint8_t tab_mode_idx[] = {
2344 0, 1, 2, 2, 2, 2, 3, 5, 7, 8, 10, 12, 13, 15, 17, 18, 19, 20,
2345 21, 22, 23, 23, 24, 24, 25, 25, 26, 27, 27, 28, 28, 29, 29, 30, 31};
2346
2347 3637384 static void intra_prediction_unit(HEVCLocalContext *lc,
2348 const HEVCLayerContext *l, const HEVCSPS *sps,
2349 int x0, int y0,
2350 int log2_cb_size)
2351 {
2352 static const uint8_t intra_chroma_table[4] = { 0, 26, 10, 1 };
2353 uint8_t prev_intra_luma_pred_flag[4];
2354 3637384 int split = lc->cu.part_mode == PART_NxN;
2355 3637384 int pb_size = (1 << log2_cb_size) >> split;
2356 3637384 int side = split + 1;
2357 int chroma_mode;
2358 int i, j;
2359
2360
2/2
✓ Branch 0 taken 4678695 times.
✓ Branch 1 taken 3637384 times.
8316079 for (i = 0; i < side; i++)
2361
2/2
✓ Branch 0 taken 6761317 times.
✓ Branch 1 taken 4678695 times.
11440012 for (j = 0; j < side; j++)
2362 6761317 prev_intra_luma_pred_flag[2 * i + j] = ff_hevc_prev_intra_luma_pred_flag_decode(lc);
2363
2364
2/2
✓ Branch 0 taken 4678695 times.
✓ Branch 1 taken 3637384 times.
8316079 for (i = 0; i < side; i++) {
2365
2/2
✓ Branch 0 taken 6761317 times.
✓ Branch 1 taken 4678695 times.
11440012 for (j = 0; j < side; j++) {
2366
2/2
✓ Branch 0 taken 4012676 times.
✓ Branch 1 taken 2748641 times.
6761317 if (prev_intra_luma_pred_flag[2 * i + j])
2367 4012676 lc->pu.mpm_idx = ff_hevc_mpm_idx_decode(lc);
2368 else
2369 2748641 lc->pu.rem_intra_luma_pred_mode = ff_hevc_rem_intra_luma_pred_mode_decode(lc);
2370
2371 6761317 lc->pu.intra_pred_mode[2 * i + j] =
2372 6761317 luma_intra_pred_mode(lc, l, sps,
2373 6761317 x0 + pb_size * j, y0 + pb_size * i, pb_size,
2374 6761317 prev_intra_luma_pred_flag[2 * i + j]);
2375 }
2376 }
2377
2378
2/2
✓ Branch 0 taken 59336 times.
✓ Branch 1 taken 3578048 times.
3637384 if (sps->chroma_format_idc == 3) {
2379
2/2
✓ Branch 0 taken 77980 times.
✓ Branch 1 taken 59336 times.
137316 for (i = 0; i < side; i++) {
2380
2/2
✓ Branch 0 taken 115268 times.
✓ Branch 1 taken 77980 times.
193248 for (j = 0; j < side; j++) {
2381 115268 lc->pu.chroma_mode_c[2 * i + j] = chroma_mode = ff_hevc_intra_chroma_pred_mode_decode(lc);
2382
2/2
✓ Branch 0 taken 19491 times.
✓ Branch 1 taken 95777 times.
115268 if (chroma_mode != 4) {
2383
2/2
✓ Branch 0 taken 1462 times.
✓ Branch 1 taken 18029 times.
19491 if (lc->pu.intra_pred_mode[2 * i + j] == intra_chroma_table[chroma_mode])
2384 1462 lc->pu.intra_pred_mode_c[2 * i + j] = 34;
2385 else
2386 18029 lc->pu.intra_pred_mode_c[2 * i + j] = intra_chroma_table[chroma_mode];
2387 } else {
2388 95777 lc->pu.intra_pred_mode_c[2 * i + j] = lc->pu.intra_pred_mode[2 * i + j];
2389 }
2390 }
2391 }
2392
2/2
✓ Branch 0 taken 147152 times.
✓ Branch 1 taken 3430896 times.
3578048 } else if (sps->chroma_format_idc == 2) {
2393 int mode_idx;
2394 147152 lc->pu.chroma_mode_c[0] = chroma_mode = ff_hevc_intra_chroma_pred_mode_decode(lc);
2395
2/2
✓ Branch 0 taken 46479 times.
✓ Branch 1 taken 100673 times.
147152 if (chroma_mode != 4) {
2396
2/2
✓ Branch 0 taken 2238 times.
✓ Branch 1 taken 44241 times.
46479 if (lc->pu.intra_pred_mode[0] == intra_chroma_table[chroma_mode])
2397 2238 mode_idx = 34;
2398 else
2399 44241 mode_idx = intra_chroma_table[chroma_mode];
2400 } else {
2401 100673 mode_idx = lc->pu.intra_pred_mode[0];
2402 }
2403 147152 lc->pu.intra_pred_mode_c[0] = tab_mode_idx[mode_idx];
2404
2/2
✓ Branch 0 taken 3430800 times.
✓ Branch 1 taken 96 times.
3430896 } else if (sps->chroma_format_idc != 0) {
2405 3430800 chroma_mode = ff_hevc_intra_chroma_pred_mode_decode(lc);
2406
2/2
✓ Branch 0 taken 822839 times.
✓ Branch 1 taken 2607961 times.
3430800 if (chroma_mode != 4) {
2407
2/2
✓ Branch 0 taken 61000 times.
✓ Branch 1 taken 761839 times.
822839 if (lc->pu.intra_pred_mode[0] == intra_chroma_table[chroma_mode])
2408 61000 lc->pu.intra_pred_mode_c[0] = 34;
2409 else
2410 761839 lc->pu.intra_pred_mode_c[0] = intra_chroma_table[chroma_mode];
2411 } else {
2412 2607961 lc->pu.intra_pred_mode_c[0] = lc->pu.intra_pred_mode[0];
2413 }
2414 }
2415 3637384 }
2416
2417 8610621 static void intra_prediction_unit_default_value(HEVCLocalContext *lc,
2418 const HEVCLayerContext *l,
2419 const HEVCSPS *sps,
2420 int x0, int y0,
2421 int log2_cb_size)
2422 {
2423 8610621 const HEVCContext *const s = lc->parent;
2424 8610621 int pb_size = 1 << log2_cb_size;
2425 8610621 int size_in_pus = pb_size >> sps->log2_min_pu_size;
2426 8610621 int min_pu_width = sps->min_pu_width;
2427 8610621 MvField *tab_mvf = s->cur_frame->tab_mvf;
2428 8610621 int x_pu = x0 >> sps->log2_min_pu_size;
2429 8610621 int y_pu = y0 >> sps->log2_min_pu_size;
2430 int j, k;
2431
2432
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8610621 times.
8610621 if (size_in_pus == 0)
2433 size_in_pus = 1;
2434
2/2
✓ Branch 0 taken 36990394 times.
✓ Branch 1 taken 8610621 times.
45601015 for (j = 0; j < size_in_pus; j++)
2435 36990394 memset(&l->tab_ipm[(y_pu + j) * min_pu_width + x_pu], INTRA_DC, size_in_pus);
2436
2/2
✓ Branch 0 taken 11207 times.
✓ Branch 1 taken 8599414 times.
8610621 if (lc->cu.pred_mode == MODE_INTRA)
2437
2/2
✓ Branch 0 taken 37756 times.
✓ Branch 1 taken 11207 times.
48963 for (j = 0; j < size_in_pus; j++)
2438
2/2
✓ Branch 0 taken 172544 times.
✓ Branch 1 taken 37756 times.
210300 for (k = 0; k < size_in_pus; k++)
2439 172544 tab_mvf[(y_pu + j) * min_pu_width + x_pu + k].pred_flag = PF_INTRA;
2440 8610621 }
2441
2442 12248005 static int hls_coding_unit(HEVCLocalContext *lc, const HEVCContext *s,
2443 const HEVCLayerContext *l,
2444 const HEVCPPS *pps, const HEVCSPS *sps,
2445 int x0, int y0, int log2_cb_size)
2446 {
2447 12248005 int cb_size = 1 << log2_cb_size;
2448 12248005 int log2_min_cb_size = sps->log2_min_cb_size;
2449 12248005 int length = cb_size >> log2_min_cb_size;
2450 12248005 int min_cb_width = sps->min_cb_width;
2451 12248005 int x_cb = x0 >> log2_min_cb_size;
2452 12248005 int y_cb = y0 >> log2_min_cb_size;
2453 12248005 int idx = log2_cb_size - 2;
2454 12248005 int qp_block_mask = (1 << (sps->log2_ctb_size - pps->diff_cu_qp_delta_depth)) - 1;
2455 int x, y, ret;
2456
2457 12248005 lc->cu.x = x0;
2458 12248005 lc->cu.y = y0;
2459 12248005 lc->cu.pred_mode = MODE_INTRA;
2460 12248005 lc->cu.part_mode = PART_2Nx2N;
2461 12248005 lc->cu.intra_split_flag = 0;
2462
2463 12248005 SAMPLE_CTB(l->skip_flag, x_cb, y_cb) = 0;
2464
2/2
✓ Branch 0 taken 48992020 times.
✓ Branch 1 taken 12248005 times.
61240025 for (x = 0; x < 4; x++)
2465 48992020 lc->pu.intra_pred_mode[x] = 1;
2466
2/2
✓ Branch 0 taken 179649 times.
✓ Branch 1 taken 12068356 times.
12248005 if (pps->transquant_bypass_enable_flag) {
2467 179649 lc->cu.cu_transquant_bypass_flag = ff_hevc_cu_transquant_bypass_flag_decode(lc);
2468
2/2
✓ Branch 0 taken 79710 times.
✓ Branch 1 taken 99939 times.
179649 if (lc->cu.cu_transquant_bypass_flag)
2469 79710 set_deblocking_bypass(l->is_pcm, sps, x0, y0, log2_cb_size);
2470 } else
2471 12068356 lc->cu.cu_transquant_bypass_flag = 0;
2472
2473
2/2
✓ Branch 0 taken 9866641 times.
✓ Branch 1 taken 2381364 times.
12248005 if (s->sh.slice_type != HEVC_SLICE_I) {
2474 9866641 const int x0b = av_zero_extend(x0, sps->log2_ctb_size);
2475 9866641 const int y0b = av_zero_extend(y0, sps->log2_ctb_size);
2476 9866641 uint8_t skip_flag = ff_hevc_skip_flag_decode(lc, l->skip_flag,
2477 x0b, y0b, x_cb, y_cb,
2478 min_cb_width);
2479
2480 9866641 x = y_cb * min_cb_width + x_cb;
2481
2/2
✓ Branch 0 taken 20398545 times.
✓ Branch 1 taken 9866641 times.
30265186 for (y = 0; y < length; y++) {
2482 20398545 memset(&l->skip_flag[x], skip_flag, length);
2483 20398545 x += min_cb_width;
2484 }
2485
2/2
✓ Branch 0 taken 4565607 times.
✓ Branch 1 taken 5301034 times.
9866641 lc->cu.pred_mode = skip_flag ? MODE_SKIP : MODE_INTER;
2486 } else {
2487 2381364 x = y_cb * min_cb_width + x_cb;
2488
2/2
✓ Branch 0 taken 3203132 times.
✓ Branch 1 taken 2381364 times.
5584496 for (y = 0; y < length; y++) {
2489 3203132 memset(&l->skip_flag[x], 0, length);
2490 3203132 x += min_cb_width;
2491 }
2492 }
2493
2494
2/2
✓ Branch 0 taken 4565607 times.
✓ Branch 1 taken 7682398 times.
12248005 if (SAMPLE_CTB(l->skip_flag, x_cb, y_cb)) {
2495 4565607 hls_prediction_unit(lc, l, pps, sps,
2496 x0, y0, cb_size, cb_size, log2_cb_size, 0, idx);
2497 4565607 intra_prediction_unit_default_value(lc, l, sps, x0, y0, log2_cb_size);
2498
2499
2/2
✓ Branch 0 taken 4419094 times.
✓ Branch 1 taken 146513 times.
4565607 if (!s->sh.disable_deblocking_filter_flag)
2500 4419094 ff_hevc_deblocking_boundary_strengths(lc, l, pps, x0, y0, log2_cb_size);
2501 } else {
2502 7682398 int pcm_flag = 0;
2503
2504
2/2
✓ Branch 0 taken 5301034 times.
✓ Branch 1 taken 2381364 times.
7682398 if (s->sh.slice_type != HEVC_SLICE_I)
2505 5301034 lc->cu.pred_mode = ff_hevc_pred_mode_decode(lc);
2506
2/2
✓ Branch 0 taken 3648591 times.
✓ Branch 1 taken 4033807 times.
7682398 if (lc->cu.pred_mode != MODE_INTRA ||
2507
2/2
✓ Branch 0 taken 2742596 times.
✓ Branch 1 taken 905995 times.
3648591 log2_cb_size == sps->log2_min_cb_size) {
2508 6776403 lc->cu.part_mode = ff_hevc_part_mode_decode(lc, sps, log2_cb_size);
2509
2/2
✓ Branch 0 taken 1149466 times.
✓ Branch 1 taken 5626937 times.
7925869 lc->cu.intra_split_flag = lc->cu.part_mode == PART_NxN &&
2510
2/2
✓ Branch 0 taken 1041311 times.
✓ Branch 1 taken 108155 times.
1149466 lc->cu.pred_mode == MODE_INTRA;
2511 }
2512
2513
2/2
✓ Branch 0 taken 3648591 times.
✓ Branch 1 taken 4033807 times.
7682398 if (lc->cu.pred_mode == MODE_INTRA) {
2514
4/4
✓ Branch 0 taken 2607280 times.
✓ Branch 1 taken 1041311 times.
✓ Branch 2 taken 134413 times.
✓ Branch 3 taken 2472867 times.
3648591 if (lc->cu.part_mode == PART_2Nx2N && sps->pcm_enabled &&
2515
2/2
✓ Branch 0 taken 133790 times.
✓ Branch 1 taken 623 times.
134413 log2_cb_size >= sps->pcm.log2_min_pcm_cb_size &&
2516
2/2
✓ Branch 0 taken 126478 times.
✓ Branch 1 taken 7312 times.
133790 log2_cb_size <= sps->pcm.log2_max_pcm_cb_size) {
2517 126478 pcm_flag = ff_hevc_pcm_flag_decode(lc);
2518 }
2519
2/2
✓ Branch 0 taken 11207 times.
✓ Branch 1 taken 3637384 times.
3648591 if (pcm_flag) {
2520 11207 intra_prediction_unit_default_value(lc, l, sps, x0, y0, log2_cb_size);
2521 11207 ret = hls_pcm_sample(lc, l, pps, x0, y0, log2_cb_size);
2522
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 11203 times.
11207 if (sps->pcm_loop_filter_disabled)
2523 4 set_deblocking_bypass(l->is_pcm, sps, x0, y0, log2_cb_size);
2524
2525
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11207 times.
11207 if (ret < 0)
2526 return ret;
2527 } else {
2528 3637384 intra_prediction_unit(lc, l, sps, x0, y0, log2_cb_size);
2529 }
2530 } else {
2531 4033807 intra_prediction_unit_default_value(lc, l, sps, x0, y0, log2_cb_size);
2532
8/9
✓ Branch 0 taken 2514894 times.
✓ Branch 1 taken 499267 times.
✓ Branch 2 taken 599864 times.
✓ Branch 3 taken 67350 times.
✓ Branch 4 taken 58153 times.
✓ Branch 5 taken 100281 times.
✓ Branch 6 taken 85843 times.
✓ Branch 7 taken 108155 times.
✗ Branch 8 not taken.
4033807 switch (lc->cu.part_mode) {
2533 2514894 case PART_2Nx2N:
2534 2514894 hls_prediction_unit(lc, l, pps, sps,
2535 x0, y0, cb_size, cb_size, log2_cb_size, 0, idx);
2536 2514894 break;
2537 499267 case PART_2NxN:
2538 499267 hls_prediction_unit(lc, l, pps, sps,
2539 x0, y0, cb_size, cb_size / 2, log2_cb_size, 0, idx);
2540 499267 hls_prediction_unit(lc, l, pps, sps,
2541 499267 x0, y0 + cb_size / 2, cb_size, cb_size / 2, log2_cb_size, 1, idx);
2542 499267 break;
2543 599864 case PART_Nx2N:
2544 599864 hls_prediction_unit(lc, l, pps, sps,
2545 x0, y0, cb_size / 2, cb_size, log2_cb_size, 0, idx - 1);
2546 599864 hls_prediction_unit(lc, l, pps, sps,
2547 599864 x0 + cb_size / 2, y0, cb_size / 2, cb_size, log2_cb_size, 1, idx - 1);
2548 599864 break;
2549 67350 case PART_2NxnU:
2550 67350 hls_prediction_unit(lc, l, pps, sps,
2551 x0, y0, cb_size, cb_size / 4, log2_cb_size, 0, idx);
2552 67350 hls_prediction_unit(lc, l, pps, sps,
2553 67350 x0, y0 + cb_size / 4, cb_size, cb_size * 3 / 4, log2_cb_size, 1, idx);
2554 67350 break;
2555 58153 case PART_2NxnD:
2556 58153 hls_prediction_unit(lc, l, pps, sps,
2557 58153 x0, y0, cb_size, cb_size * 3 / 4, log2_cb_size, 0, idx);
2558 58153 hls_prediction_unit(lc, l, pps, sps,
2559 58153 x0, y0 + cb_size * 3 / 4, cb_size, cb_size / 4, log2_cb_size, 1, idx);
2560 58153 break;
2561 100281 case PART_nLx2N:
2562 100281 hls_prediction_unit(lc, l, pps, sps,
2563 x0, y0, cb_size / 4, cb_size, log2_cb_size, 0, idx - 2);
2564 100281 hls_prediction_unit(lc, l, pps, sps,
2565 100281 x0 + cb_size / 4, y0, cb_size * 3 / 4, cb_size, log2_cb_size, 1, idx - 2);
2566 100281 break;
2567 85843 case PART_nRx2N:
2568 85843 hls_prediction_unit(lc, l, pps, sps,
2569 85843 x0, y0, cb_size * 3 / 4, cb_size, log2_cb_size, 0, idx - 2);
2570 85843 hls_prediction_unit(lc, l, pps, sps,
2571 85843 x0 + cb_size * 3 / 4, y0, cb_size / 4, cb_size, log2_cb_size, 1, idx - 2);
2572 85843 break;
2573 108155 case PART_NxN:
2574 108155 hls_prediction_unit(lc, l, pps, sps,
2575 x0, y0, cb_size / 2, cb_size / 2, log2_cb_size, 0, idx - 1);
2576 108155 hls_prediction_unit(lc, l, pps, sps,
2577 108155 x0 + cb_size / 2, y0, cb_size / 2, cb_size / 2, log2_cb_size, 1, idx - 1);
2578 108155 hls_prediction_unit(lc, l, pps, sps,
2579 108155 x0, y0 + cb_size / 2, cb_size / 2, cb_size / 2, log2_cb_size, 2, idx - 1);
2580 108155 hls_prediction_unit(lc, l, pps, sps,
2581 108155 x0 + cb_size / 2, y0 + cb_size / 2, cb_size / 2, cb_size / 2, log2_cb_size, 3, idx - 1);
2582 108155 break;
2583 }
2584 }
2585
2586
2/2
✓ Branch 0 taken 7671191 times.
✓ Branch 1 taken 11207 times.
7682398 if (!pcm_flag) {
2587 7671191 int rqt_root_cbf = 1;
2588
2589
2/2
✓ Branch 0 taken 4033807 times.
✓ Branch 1 taken 3637384 times.
7671191 if (lc->cu.pred_mode != MODE_INTRA &&
2590
4/4
✓ Branch 0 taken 2514894 times.
✓ Branch 1 taken 1518913 times.
✓ Branch 2 taken 1307742 times.
✓ Branch 3 taken 1207152 times.
4033807 !(lc->cu.part_mode == PART_2Nx2N && lc->pu.merge_flag)) {
2591 2826655 rqt_root_cbf = ff_hevc_no_residual_syntax_flag_decode(lc);
2592 }
2593
2/2
✓ Branch 0 taken 6154758 times.
✓ Branch 1 taken 1516433 times.
7671191 if (rqt_root_cbf) {
2594 const static int cbf[2] = { 0 };
2595
2/2
✓ Branch 0 taken 3637384 times.
✓ Branch 1 taken 2517374 times.
6154758 lc->cu.max_trafo_depth = lc->cu.pred_mode == MODE_INTRA ?
2596 3637384 sps->max_transform_hierarchy_depth_intra + lc->cu.intra_split_flag :
2597 2517374 sps->max_transform_hierarchy_depth_inter;
2598 6154758 ret = hls_transform_tree(lc, l, pps, sps, x0, y0, x0, y0, x0, y0,
2599 log2_cb_size,
2600 log2_cb_size, 0, 0, cbf, cbf);
2601
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 6154757 times.
6154758 if (ret < 0)
2602 1 return ret;
2603 } else {
2604
2/2
✓ Branch 0 taken 1460001 times.
✓ Branch 1 taken 56432 times.
1516433 if (!s->sh.disable_deblocking_filter_flag)
2605 1460001 ff_hevc_deblocking_boundary_strengths(lc, l, pps, x0, y0, log2_cb_size);
2606 }
2607 }
2608 }
2609
2610
4/4
✓ Branch 0 taken 3206772 times.
✓ Branch 1 taken 9041232 times.
✓ Branch 2 taken 1172325 times.
✓ Branch 3 taken 2034447 times.
12248004 if (pps->cu_qp_delta_enabled_flag && lc->tu.is_cu_qp_delta_coded == 0)
2611 1172325 ff_hevc_set_qPy(lc, l, pps, x0, y0, log2_cb_size);
2612
2613 12248004 x = y_cb * min_cb_width + x_cb;
2614
2/2
✓ Branch 0 taken 23601676 times.
✓ Branch 1 taken 12248004 times.
35849680 for (y = 0; y < length; y++) {
2615 23601676 memset(&l->qp_y_tab[x], lc->qp_y, length);
2616 23601676 x += min_cb_width;
2617 }
2618
2619
2/2
✓ Branch 0 taken 4495773 times.
✓ Branch 1 taken 7752231 times.
12248004 if(((x0 + (1<<log2_cb_size)) & qp_block_mask) == 0 &&
2620
2/2
✓ Branch 0 taken 2720496 times.
✓ Branch 1 taken 1775277 times.
4495773 ((y0 + (1<<log2_cb_size)) & qp_block_mask) == 0) {
2621 2720496 lc->qPy_pred = lc->qp_y;
2622 }
2623
2624 12248004 set_ct_depth(sps, l->tab_ct_depth, x0, y0, log2_cb_size, lc->ct_depth);
2625
2626 12248004 return 0;
2627 }
2628
2629 15841410 static int hls_coding_quadtree(HEVCLocalContext *lc,
2630 const HEVCLayerContext *l,
2631 const HEVCPPS *pps, const HEVCSPS *sps,
2632 int x0, int y0,
2633 int log2_cb_size, int cb_depth)
2634 {
2635 15841410 const HEVCContext *const s = lc->parent;
2636 15841410 const int cb_size = 1 << log2_cb_size;
2637 int ret;
2638 int split_cu;
2639
2640 15841410 lc->ct_depth = cb_depth;
2641
2/2
✓ Branch 0 taken 15812212 times.
✓ Branch 1 taken 29198 times.
15841410 if (x0 + cb_size <= sps->width &&
2642
2/2
✓ Branch 0 taken 15494755 times.
✓ Branch 1 taken 317457 times.
15812212 y0 + cb_size <= sps->height &&
2643
2/2
✓ Branch 0 taken 8723164 times.
✓ Branch 1 taken 6771591 times.
15494755 log2_cb_size > sps->log2_min_cb_size) {
2644 8723164 split_cu = ff_hevc_split_coding_unit_flag_decode(lc, l->tab_ct_depth,
2645 sps, cb_depth, x0, y0);
2646 } else {
2647 7118246 split_cu = (log2_cb_size > sps->log2_min_cb_size);
2648 }
2649
2/2
✓ Branch 0 taken 4063509 times.
✓ Branch 1 taken 11777901 times.
15841410 if (pps->cu_qp_delta_enabled_flag &&
2650
2/2
✓ Branch 0 taken 1887190 times.
✓ Branch 1 taken 2176319 times.
4063509 log2_cb_size >= sps->log2_ctb_size - pps->diff_cu_qp_delta_depth) {
2651 1887190 lc->tu.is_cu_qp_delta_coded = 0;
2652 1887190 lc->tu.cu_qp_delta = 0;
2653 }
2654
2655
2/2
✓ Branch 0 taken 467556 times.
✓ Branch 1 taken 15373854 times.
15841410 if (s->sh.cu_chroma_qp_offset_enabled_flag &&
2656
1/2
✓ Branch 0 taken 467556 times.
✗ Branch 1 not taken.
467556 log2_cb_size >= sps->log2_ctb_size - pps->diff_cu_chroma_qp_offset_depth) {
2657 467556 lc->tu.is_cu_chroma_qp_offset_coded = 0;
2658 }
2659
2660
2/2
✓ Branch 0 taken 3593405 times.
✓ Branch 1 taken 12248005 times.
15841410 if (split_cu) {
2661 3593405 int qp_block_mask = (1 << (sps->log2_ctb_size - pps->diff_cu_qp_delta_depth)) - 1;
2662 3593405 const int cb_size_split = cb_size >> 1;
2663 3593405 const int x1 = x0 + cb_size_split;
2664 3593405 const int y1 = y0 + cb_size_split;
2665
2666 3593405 int more_data = 0;
2667
2668 3593405 more_data = hls_coding_quadtree(lc, l, pps, sps,
2669 x0, y0, log2_cb_size - 1, cb_depth + 1);
2670
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 3593403 times.
3593405 if (more_data < 0)
2671 2 return more_data;
2672
2673
4/4
✓ Branch 0 taken 3593395 times.
✓ Branch 1 taken 8 times.
✓ Branch 2 taken 3565589 times.
✓ Branch 3 taken 27806 times.
3593403 if (more_data && x1 < sps->width) {
2674 3565589 more_data = hls_coding_quadtree(lc, l, pps, sps,
2675 x1, y0, log2_cb_size - 1, cb_depth + 1);
2676
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3565589 times.
3565589 if (more_data < 0)
2677 return more_data;
2678 }
2679
4/4
✓ Branch 0 taken 3583129 times.
✓ Branch 1 taken 10274 times.
✓ Branch 2 taken 3384434 times.
✓ Branch 3 taken 198695 times.
3593403 if (more_data && y1 < sps->height) {
2680 3384434 more_data = hls_coding_quadtree(lc, l, pps, sps,
2681 x0, y1, log2_cb_size - 1, cb_depth + 1);
2682
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3384434 times.
3384434 if (more_data < 0)
2683 return more_data;
2684 }
2685
4/4
✓ Branch 0 taken 3577014 times.
✓ Branch 1 taken 16389 times.
✓ Branch 2 taken 3555323 times.
✓ Branch 3 taken 21691 times.
3593403 if (more_data && x1 < sps->width &&
2686
2/2
✓ Branch 0 taken 3356628 times.
✓ Branch 1 taken 198695 times.
3555323 y1 < sps->height) {
2687 3356628 more_data = hls_coding_quadtree(lc, l, pps, sps,
2688 x1, y1, log2_cb_size - 1, cb_depth + 1);
2689
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 3356627 times.
3356628 if (more_data < 0)
2690 1 return more_data;
2691 }
2692
2693
2/2
✓ Branch 0 taken 2109131 times.
✓ Branch 1 taken 1484271 times.
3593402 if(((x0 + (1<<log2_cb_size)) & qp_block_mask) == 0 &&
2694
2/2
✓ Branch 0 taken 1610254 times.
✓ Branch 1 taken 498877 times.
2109131 ((y0 + (1<<log2_cb_size)) & qp_block_mask) == 0)
2695 1610254 lc->qPy_pred = lc->qp_y;
2696
2697
2/2
✓ Branch 0 taken 3557913 times.
✓ Branch 1 taken 35489 times.
3593402 if (more_data)
2698
2/2
✓ Branch 0 taken 94847 times.
✓ Branch 1 taken 3463066 times.
3652760 return ((x1 + cb_size_split) < sps->width ||
2699
2/2
✓ Branch 0 taken 94846 times.
✓ Branch 1 taken 1 times.
94847 (y1 + cb_size_split) < sps->height);
2700 else
2701 35489 return 0;
2702 } else {
2703 12248005 ret = hls_coding_unit(lc, s, l, pps, sps, x0, y0, log2_cb_size);
2704
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 12248004 times.
12248005 if (ret < 0)
2705 1 return ret;
2706 12248004 if ((!((x0 + cb_size) %
2707
2/2
✓ Branch 0 taken 8527579 times.
✓ Branch 1 taken 3720425 times.
12248004 (1 << (sps->log2_ctb_size))) ||
2708
2/2
✓ Branch 0 taken 69669 times.
✓ Branch 1 taken 8457910 times.
8527579 (x0 + cb_size >= sps->width)) &&
2709 3790094 (!((y0 + cb_size) %
2710
2/2
✓ Branch 0 taken 1945728 times.
✓ Branch 1 taken 1844366 times.
3790094 (1 << (sps->log2_ctb_size))) ||
2711
2/2
✓ Branch 0 taken 96987 times.
✓ Branch 1 taken 1848741 times.
1945728 (y0 + cb_size >= sps->height))) {
2712 1941353 int end_of_slice_flag = ff_hevc_end_of_slice_flag_decode(lc);
2713 1941353 return !end_of_slice_flag;
2714 } else {
2715 10306651 return 1;
2716 }
2717 }
2718
2719 return 0;
2720 }
2721
2722 1941354 static void hls_decode_neighbour(HEVCLocalContext *lc,
2723 const HEVCLayerContext *l,
2724 const HEVCPPS *pps, const HEVCSPS *sps,
2725 int x_ctb, int y_ctb, int ctb_addr_ts)
2726 {
2727 1941354 const HEVCContext *const s = lc->parent;
2728 1941354 int ctb_size = 1 << sps->log2_ctb_size;
2729 1941354 int ctb_addr_rs = pps->ctb_addr_ts_to_rs[ctb_addr_ts];
2730 1941354 int ctb_addr_in_slice = ctb_addr_rs - s->sh.slice_addr;
2731
2732 1941354 l->tab_slice_address[ctb_addr_rs] = s->sh.slice_addr;
2733
2734
2/2
✓ Branch 0 taken 168556 times.
✓ Branch 1 taken 1772798 times.
1941354 if (pps->entropy_coding_sync_enabled_flag) {
2735
3/4
✓ Branch 0 taken 7942 times.
✓ Branch 1 taken 160614 times.
✓ Branch 2 taken 7942 times.
✗ Branch 3 not taken.
168556 if (x_ctb == 0 && (y_ctb & (ctb_size - 1)) == 0)
2736 7942 lc->first_qp_group = 1;
2737 168556 lc->end_of_tiles_x = sps->width;
2738
2/2
✓ Branch 0 taken 655296 times.
✓ Branch 1 taken 1117502 times.
1772798 } else if (pps->tiles_enabled_flag) {
2739
4/4
✓ Branch 0 taken 654565 times.
✓ Branch 1 taken 731 times.
✓ Branch 2 taken 6381 times.
✓ Branch 3 taken 648184 times.
655296 if (ctb_addr_ts && pps->tile_id[ctb_addr_ts] != pps->tile_id[ctb_addr_ts - 1]) {
2740 6381 int idxX = pps->col_idxX[x_ctb >> sps->log2_ctb_size];
2741 6381 lc->end_of_tiles_x = x_ctb + (pps->column_width[idxX] << sps->log2_ctb_size);
2742 6381 lc->first_qp_group = 1;
2743 }
2744 } else {
2745 1117502 lc->end_of_tiles_x = sps->width;
2746 }
2747
2748 1941354 lc->end_of_tiles_y = FFMIN(y_ctb + ctb_size, sps->height);
2749
2750 1941354 lc->boundary_flags = 0;
2751
2/2
✓ Branch 0 taken 655296 times.
✓ Branch 1 taken 1286058 times.
1941354 if (pps->tiles_enabled_flag) {
2752
4/4
✓ Branch 0 taken 638081 times.
✓ Branch 1 taken 17215 times.
✓ Branch 2 taken 40841 times.
✓ Branch 3 taken 597240 times.
655296 if (x_ctb > 0 && pps->tile_id[ctb_addr_ts] != pps->tile_id[pps->ctb_addr_rs_to_ts[ctb_addr_rs - 1]])
2753 40841 lc->boundary_flags |= BOUNDARY_LEFT_TILE;
2754
4/4
✓ Branch 0 taken 638081 times.
✓ Branch 1 taken 17215 times.
✓ Branch 2 taken 15689 times.
✓ Branch 3 taken 622392 times.
655296 if (x_ctb > 0 && l->tab_slice_address[ctb_addr_rs] != l->tab_slice_address[ctb_addr_rs - 1])
2755 15689 lc->boundary_flags |= BOUNDARY_LEFT_SLICE;
2756
4/4
✓ Branch 0 taken 633209 times.
✓ Branch 1 taken 22087 times.
✓ Branch 2 taken 32582 times.
✓ Branch 3 taken 600627 times.
655296 if (y_ctb > 0 && pps->tile_id[ctb_addr_ts] != pps->tile_id[pps->ctb_addr_rs_to_ts[ctb_addr_rs - sps->ctb_width]])
2757 32582 lc->boundary_flags |= BOUNDARY_UPPER_TILE;
2758
4/4
✓ Branch 0 taken 633209 times.
✓ Branch 1 taken 22087 times.
✓ Branch 2 taken 13570 times.
✓ Branch 3 taken 619639 times.
655296 if (y_ctb > 0 && l->tab_slice_address[ctb_addr_rs] != l->tab_slice_address[ctb_addr_rs - sps->ctb_width])
2759 13570 lc->boundary_flags |= BOUNDARY_UPPER_SLICE;
2760 } else {
2761
2/2
✓ Branch 0 taken 18267 times.
✓ Branch 1 taken 1267791 times.
1286058 if (ctb_addr_in_slice <= 0)
2762 18267 lc->boundary_flags |= BOUNDARY_LEFT_SLICE;
2763
2/2
✓ Branch 0 taken 196252 times.
✓ Branch 1 taken 1089806 times.
1286058 if (ctb_addr_in_slice < sps->ctb_width)
2764 196252 lc->boundary_flags |= BOUNDARY_UPPER_SLICE;
2765 }
2766
2767
6/6
✓ Branch 0 taken 1854551 times.
✓ Branch 1 taken 86803 times.
✓ Branch 2 taken 1846462 times.
✓ Branch 3 taken 8089 times.
✓ Branch 4 taken 1806867 times.
✓ Branch 5 taken 39595 times.
1941354 lc->ctb_left_flag = ((x_ctb > 0) && (ctb_addr_in_slice > 0) && !(lc->boundary_flags & BOUNDARY_LEFT_TILE));
2768
6/6
✓ Branch 0 taken 1803092 times.
✓ Branch 1 taken 138262 times.
✓ Branch 2 taken 1710266 times.
✓ Branch 3 taken 92826 times.
✓ Branch 4 taken 1690433 times.
✓ Branch 5 taken 19833 times.
1941354 lc->ctb_up_flag = ((y_ctb > 0) && (ctb_addr_in_slice >= sps->ctb_width) && !(lc->boundary_flags & BOUNDARY_UPPER_TILE));
2769
6/6
✓ Branch 0 taken 1803092 times.
✓ Branch 1 taken 138262 times.
✓ Branch 2 taken 1712813 times.
✓ Branch 3 taken 90279 times.
✓ Branch 4 taken 1642207 times.
✓ Branch 5 taken 70606 times.
1941354 lc->ctb_up_right_flag = ((y_ctb > 0) && (ctb_addr_in_slice+1 >= sps->ctb_width) && (pps->tile_id[ctb_addr_ts] == pps->tile_id[pps->ctb_addr_rs_to_ts[ctb_addr_rs+1 - sps->ctb_width]]));
2770
8/8
✓ Branch 0 taken 1854551 times.
✓ Branch 1 taken 86803 times.
✓ Branch 2 taken 1726639 times.
✓ Branch 3 taken 127912 times.
✓ Branch 4 taken 1638601 times.
✓ Branch 5 taken 88038 times.
✓ Branch 6 taken 1584903 times.
✓ Branch 7 taken 53698 times.
1941354 lc->ctb_up_left_flag = ((x_ctb > 0) && (y_ctb > 0) && (ctb_addr_in_slice-1 >= sps->ctb_width) && (pps->tile_id[ctb_addr_ts] == pps->tile_id[pps->ctb_addr_rs_to_ts[ctb_addr_rs-1 - sps->ctb_width]]));
2771 1941354 }
2772
2773 28346 static int hls_decode_entry(HEVCContext *s, GetBitContext *gb)
2774 {
2775 28346 HEVCLocalContext *const lc = &s->local_ctx[0];
2776 28346 const HEVCLayerContext *const l = &s->layers[s->cur_layer];
2777 28346 const HEVCPPS *const pps = s->pps;
2778 28346 const HEVCSPS *const sps = pps->sps;
2779 28346 const uint8_t *slice_data = gb->buffer + s->sh.data_offset;
2780 28346 const size_t slice_size = get_bits_bytesize(gb, 1) - s->sh.data_offset;
2781 28346 int ctb_size = 1 << sps->log2_ctb_size;
2782 28346 int more_data = 1;
2783 28346 int x_ctb = 0;
2784 28346 int y_ctb = 0;
2785 28346 int ctb_addr_ts = pps->ctb_addr_rs_to_ts[s->sh.slice_ctb_addr_rs];
2786 int ret;
2787
2788
3/4
✓ Branch 0 taken 1941354 times.
✓ Branch 1 taken 28345 times.
✓ Branch 2 taken 1941354 times.
✗ Branch 3 not taken.
1969699 while (more_data && ctb_addr_ts < sps->ctb_size) {
2789 1941354 int ctb_addr_rs = pps->ctb_addr_ts_to_rs[ctb_addr_ts];
2790
2791 1941354 x_ctb = (ctb_addr_rs % ((sps->width + ctb_size - 1) >> sps->log2_ctb_size)) << sps->log2_ctb_size;
2792 1941354 y_ctb = (ctb_addr_rs / ((sps->width + ctb_size - 1) >> sps->log2_ctb_size)) << sps->log2_ctb_size;
2793 1941354 hls_decode_neighbour(lc, l, pps, sps, x_ctb, y_ctb, ctb_addr_ts);
2794
2795 1941354 ret = ff_hevc_cabac_init(lc, pps, ctb_addr_ts, slice_data, slice_size, 0);
2796
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1941354 times.
1941354 if (ret < 0) {
2797 l->tab_slice_address[ctb_addr_rs] = -1;
2798 return ret;
2799 }
2800
2801 1941354 hls_sao_param(lc, l, pps, sps,
2802 1941354 x_ctb >> sps->log2_ctb_size, y_ctb >> sps->log2_ctb_size);
2803
2804 1941354 l->deblock[ctb_addr_rs].beta_offset = s->sh.beta_offset;
2805 1941354 l->deblock[ctb_addr_rs].tc_offset = s->sh.tc_offset;
2806 1941354 l->filter_slice_edges[ctb_addr_rs] = s->sh.slice_loop_filter_across_slices_enabled_flag;
2807
2808 1941354 more_data = hls_coding_quadtree(lc, l, pps, sps, x_ctb, y_ctb, sps->log2_ctb_size, 0);
2809
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1941353 times.
1941354 if (more_data < 0) {
2810 1 l->tab_slice_address[ctb_addr_rs] = -1;
2811 1 return more_data;
2812 }
2813
2814
2815 1941353 ctb_addr_ts++;
2816 1941353 ff_hevc_save_states(lc, pps, ctb_addr_ts);
2817 1941353 ff_hevc_hls_filters(lc, l, pps, x_ctb, y_ctb, ctb_size);
2818 }
2819
2820
2/2
✓ Branch 0 taken 14776 times.
✓ Branch 1 taken 13569 times.
28345 if (x_ctb + ctb_size >= sps->width &&
2821
2/2
✓ Branch 0 taken 10349 times.
✓ Branch 1 taken 4427 times.
14776 y_ctb + ctb_size >= sps->height)
2822 10349 ff_hevc_hls_filter(lc, l, pps, x_ctb, y_ctb, ctb_size);
2823
2824 28345 return ctb_addr_ts;
2825 }
2826
2827 static int hls_decode_entry_wpp(AVCodecContext *avctx, void *hevc_lclist,
2828 int job, int thread)
2829 {
2830 HEVCLocalContext *lc = &((HEVCLocalContext*)hevc_lclist)[thread];
2831 const HEVCContext *const s = lc->parent;
2832 const HEVCLayerContext *const l = &s->layers[s->cur_layer];
2833 const HEVCPPS *const pps = s->pps;
2834 const HEVCSPS *const sps = pps->sps;
2835 int ctb_size = 1 << sps->log2_ctb_size;
2836 int more_data = 1;
2837 int ctb_row = job;
2838 int ctb_addr_rs = s->sh.slice_ctb_addr_rs + ctb_row * ((sps->width + ctb_size - 1) >> sps->log2_ctb_size);
2839 int ctb_addr_ts = pps->ctb_addr_rs_to_ts[ctb_addr_rs];
2840
2841 const uint8_t *data = s->data + s->sh.offset[ctb_row];
2842 const size_t data_size = s->sh.size[ctb_row];
2843
2844 int progress = 0;
2845
2846 int ret;
2847
2848 if (ctb_row)
2849 ff_init_cabac_decoder(&lc->cc, data, data_size);
2850
2851 while(more_data && ctb_addr_ts < sps->ctb_size) {
2852 int x_ctb = (ctb_addr_rs % sps->ctb_width) << sps->log2_ctb_size;
2853 int y_ctb = (ctb_addr_rs / sps->ctb_width) << sps->log2_ctb_size;
2854
2855 hls_decode_neighbour(lc, l, pps, sps, x_ctb, y_ctb, ctb_addr_ts);
2856
2857 if (ctb_row)
2858 ff_thread_progress_await(&s->wpp_progress[ctb_row - 1],
2859 progress + SHIFT_CTB_WPP + 1);
2860
2861 /* atomic_load's prototype requires a pointer to non-const atomic variable
2862 * (due to implementations via mutexes, where reads involve writes).
2863 * Of course, casting const away here is nevertheless safe. */
2864 if (atomic_load((atomic_int*)&s->wpp_err)) {
2865 ff_thread_progress_report(&s->wpp_progress[ctb_row], INT_MAX);
2866 return 0;
2867 }
2868
2869 ret = ff_hevc_cabac_init(lc, pps, ctb_addr_ts, data, data_size, 1);
2870 if (ret < 0)
2871 goto error;
2872 hls_sao_param(lc, l, pps, sps,
2873 x_ctb >> sps->log2_ctb_size, y_ctb >> sps->log2_ctb_size);
2874
2875 l->deblock[ctb_addr_rs].beta_offset = s->sh.beta_offset;
2876 l->deblock[ctb_addr_rs].tc_offset = s->sh.tc_offset;
2877 l->filter_slice_edges[ctb_addr_rs] = s->sh.slice_loop_filter_across_slices_enabled_flag;
2878
2879 more_data = hls_coding_quadtree(lc, l, pps, sps, x_ctb, y_ctb, sps->log2_ctb_size, 0);
2880
2881 if (more_data < 0) {
2882 ret = more_data;
2883 goto error;
2884 }
2885
2886 ctb_addr_ts++;
2887
2888 ff_hevc_save_states(lc, pps, ctb_addr_ts);
2889 ff_thread_progress_report(&s->wpp_progress[ctb_row], ++progress);
2890 ff_hevc_hls_filters(lc, l, pps, x_ctb, y_ctb, ctb_size);
2891
2892 if (!more_data && (x_ctb+ctb_size) < sps->width && ctb_row != s->sh.num_entry_point_offsets) {
2893 /* Casting const away here is safe, because it is an atomic operation. */
2894 atomic_store((atomic_int*)&s->wpp_err, 1);
2895 ff_thread_progress_report(&s->wpp_progress[ctb_row], INT_MAX);
2896 return 0;
2897 }
2898
2899 if ((x_ctb+ctb_size) >= sps->width && (y_ctb+ctb_size) >= sps->height ) {
2900 ff_hevc_hls_filter(lc, l, pps, x_ctb, y_ctb, ctb_size);
2901 ff_thread_progress_report(&s->wpp_progress[ctb_row], INT_MAX);
2902 return ctb_addr_ts;
2903 }
2904 ctb_addr_rs = pps->ctb_addr_ts_to_rs[ctb_addr_ts];
2905 x_ctb+=ctb_size;
2906
2907 if(x_ctb >= sps->width) {
2908 break;
2909 }
2910 }
2911 ff_thread_progress_report(&s->wpp_progress[ctb_row], INT_MAX);
2912
2913 return 0;
2914 error:
2915 l->tab_slice_address[ctb_addr_rs] = -1;
2916 /* Casting const away here is safe, because it is an atomic operation. */
2917 atomic_store((atomic_int*)&s->wpp_err, 1);
2918 ff_thread_progress_report(&s->wpp_progress[ctb_row], INT_MAX);
2919 return ret;
2920 }
2921
2922 static int wpp_progress_init(HEVCContext *s, unsigned count)
2923 {
2924 if (s->nb_wpp_progress < count) {
2925 void *tmp = av_realloc_array(s->wpp_progress, count,
2926 sizeof(*s->wpp_progress));
2927 if (!tmp)
2928 return AVERROR(ENOMEM);
2929
2930 s->wpp_progress = tmp;
2931 memset(s->wpp_progress + s->nb_wpp_progress, 0,
2932 (count - s->nb_wpp_progress) * sizeof(*s->wpp_progress));
2933
2934 for (int i = s->nb_wpp_progress; i < count; i++) {
2935 int ret = ff_thread_progress_init(&s->wpp_progress[i], 1);
2936 if (ret < 0)
2937 return ret;
2938 s->nb_wpp_progress = i + 1;
2939 }
2940 }
2941
2942 for (int i = 0; i < count; i++)
2943 ff_thread_progress_reset(&s->wpp_progress[i]);
2944
2945 return 0;
2946 }
2947
2948 static int hls_slice_data_wpp(HEVCContext *s, const H2645NAL *nal)
2949 {
2950 const HEVCPPS *const pps = s->pps;
2951 const HEVCSPS *const sps = pps->sps;
2952 const uint8_t *data = nal->data;
2953 int length = nal->size;
2954 int *ret;
2955 int64_t offset;
2956 int64_t startheader, cmpt = 0;
2957 int j, res = 0;
2958
2959 if (s->sh.slice_ctb_addr_rs + s->sh.num_entry_point_offsets * (int64_t)sps->ctb_width >= sps->ctb_width * (int64_t)sps->ctb_height) {
2960 av_log(s->avctx, AV_LOG_ERROR, "WPP ctb addresses are wrong (%d %d %d %d)\n",
2961 s->sh.slice_ctb_addr_rs, s->sh.num_entry_point_offsets,
2962 sps->ctb_width, sps->ctb_height
2963 );
2964 return AVERROR_INVALIDDATA;
2965 }
2966
2967 if (s->avctx->thread_count > s->nb_local_ctx) {
2968 HEVCLocalContext *tmp = av_malloc_array(s->avctx->thread_count, sizeof(*s->local_ctx));
2969
2970 if (!tmp)
2971 return AVERROR(ENOMEM);
2972
2973 memcpy(tmp, s->local_ctx, sizeof(*s->local_ctx) * s->nb_local_ctx);
2974 av_free(s->local_ctx);
2975 s->local_ctx = tmp;
2976
2977 for (unsigned i = s->nb_local_ctx; i < s->avctx->thread_count; i++) {
2978 tmp = &s->local_ctx[i];
2979
2980 memset(tmp, 0, sizeof(*tmp));
2981
2982 tmp->logctx = s->avctx;
2983 tmp->parent = s;
2984 tmp->common_cabac_state = &s->cabac;
2985 }
2986
2987 s->nb_local_ctx = s->avctx->thread_count;
2988 }
2989
2990 offset = s->sh.data_offset;
2991
2992 for (j = 0, cmpt = 0, startheader = offset + s->sh.entry_point_offset[0]; j < nal->skipped_bytes; j++) {
2993 if (nal->skipped_bytes_pos[j] >= offset && nal->skipped_bytes_pos[j] < startheader) {
2994 startheader--;
2995 cmpt++;
2996 }
2997 }
2998
2999 for (int i = 1; i < s->sh.num_entry_point_offsets; i++) {
3000 offset += (s->sh.entry_point_offset[i - 1] - cmpt);
3001 for (j = 0, cmpt = 0, startheader = offset
3002 + s->sh.entry_point_offset[i]; j < nal->skipped_bytes; j++) {
3003 if (nal->skipped_bytes_pos[j] >= offset && nal->skipped_bytes_pos[j] < startheader) {
3004 startheader--;
3005 cmpt++;
3006 }
3007 }
3008 s->sh.size[i] = s->sh.entry_point_offset[i] - cmpt;
3009 s->sh.offset[i] = offset;
3010
3011 }
3012
3013 offset += s->sh.entry_point_offset[s->sh.num_entry_point_offsets - 1] - cmpt;
3014 if (length < offset) {
3015 av_log(s->avctx, AV_LOG_ERROR, "entry_point_offset table is corrupted\n");
3016 return AVERROR_INVALIDDATA;
3017 }
3018 s->sh.size [s->sh.num_entry_point_offsets] = length - offset;
3019 s->sh.offset[s->sh.num_entry_point_offsets] = offset;
3020
3021 s->sh.offset[0] = s->sh.data_offset;
3022 s->sh.size[0] = s->sh.offset[1] - s->sh.offset[0];
3023
3024 s->data = data;
3025
3026 for (unsigned i = 1; i < s->nb_local_ctx; i++) {
3027 s->local_ctx[i].first_qp_group = 1;
3028 s->local_ctx[i].qp_y = s->local_ctx[0].qp_y;
3029 }
3030
3031 atomic_store(&s->wpp_err, 0);
3032 res = wpp_progress_init(s, s->sh.num_entry_point_offsets + 1);
3033 if (res < 0)
3034 return res;
3035
3036 ret = av_calloc(s->sh.num_entry_point_offsets + 1, sizeof(*ret));
3037 if (!ret)
3038 return AVERROR(ENOMEM);
3039
3040 if (pps->entropy_coding_sync_enabled_flag)
3041 s->avctx->execute2(s->avctx, hls_decode_entry_wpp, s->local_ctx, ret, s->sh.num_entry_point_offsets + 1);
3042
3043 for (int i = 0; i <= s->sh.num_entry_point_offsets; i++)
3044 res += ret[i];
3045
3046 av_free(ret);
3047 return res;
3048 }
3049
3050 28458 static int decode_slice_data(HEVCContext *s, const HEVCLayerContext *l,
3051 const H2645NAL *nal, GetBitContext *gb)
3052 {
3053 28458 const HEVCPPS *pps = s->pps;
3054 int ret;
3055
3056
2/2
✓ Branch 0 taken 17996 times.
✓ Branch 1 taken 10462 times.
28458 if (!s->sh.first_slice_in_pic_flag)
3057 17996 s->slice_idx += !s->sh.dependent_slice_segment_flag;
3058
3059
4/4
✓ Branch 0 taken 20664 times.
✓ Branch 1 taken 7794 times.
✓ Branch 2 taken 19665 times.
✓ Branch 3 taken 999 times.
28458 if (!s->sh.dependent_slice_segment_flag && s->sh.slice_type != HEVC_SLICE_I) {
3060 19665 ret = ff_hevc_slice_rpl(s);
3061
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 19665 times.
19665 if (ret < 0) {
3062 av_log(s->avctx, AV_LOG_WARNING,
3063 "Error constructing the reference lists for the current slice.\n");
3064 return ret;
3065 }
3066 }
3067
3068 28458 s->slice_initialized = 1;
3069
3070
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 28458 times.
28458 if (s->avctx->hwaccel)
3071 return FF_HW_CALL(s->avctx, decode_slice, nal->raw_data, nal->raw_size);
3072
3073
2/2
✓ Branch 1 taken 112 times.
✓ Branch 2 taken 28346 times.
28458 if (ff_decode_skip_all_pixels(s->avctx))
3074 112 return 0;
3075
3076
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 28346 times.
28346 if (s->avctx->profile == AV_PROFILE_HEVC_SCC) {
3077 av_log(s->avctx, AV_LOG_ERROR,
3078 "SCC profile is not yet implemented in hevc native decoder.\n");
3079 return AVERROR_PATCHWELCOME;
3080 }
3081
3082
2/2
✓ Branch 0 taken 7794 times.
✓ Branch 1 taken 20552 times.
28346 if (s->sh.dependent_slice_segment_flag) {
3083 7794 int ctb_addr_ts = pps->ctb_addr_rs_to_ts[s->sh.slice_ctb_addr_rs];
3084 7794 int prev_rs = pps->ctb_addr_ts_to_rs[ctb_addr_ts - 1];
3085
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7794 times.
7794 if (l->tab_slice_address[prev_rs] != s->sh.slice_addr) {
3086 av_log(s->avctx, AV_LOG_ERROR, "Previous slice segment missing\n");
3087 return AVERROR_INVALIDDATA;
3088 }
3089 }
3090
3091 28346 s->local_ctx[0].first_qp_group = !s->sh.dependent_slice_segment_flag;
3092
3093
2/2
✓ Branch 0 taken 22847 times.
✓ Branch 1 taken 5499 times.
28346 if (!pps->cu_qp_delta_enabled_flag)
3094 22847 s->local_ctx[0].qp_y = s->sh.slice_qp;
3095
3096 28346 s->local_ctx[0].tu.cu_qp_offset_cb = 0;
3097 28346 s->local_ctx[0].tu.cu_qp_offset_cr = 0;
3098
3099
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 28346 times.
28346 if (s->avctx->active_thread_type == FF_THREAD_SLICE &&
3100 s->sh.num_entry_point_offsets > 0 &&
3101 pps->num_tile_rows == 1 && pps->num_tile_columns == 1)
3102 return hls_slice_data_wpp(s, nal);
3103
3104 28346 return hls_decode_entry(s, gb);
3105 }
3106
3107 10462 static int set_side_data(HEVCContext *s)
3108 {
3109 10462 const HEVCSPS *sps = s->cur_frame->pps->sps;
3110 10462 AVFrame *out = s->cur_frame->f;
3111 int ret;
3112
3113 // Decrement the mastering display and content light level flag when IRAP
3114 // frame has no_rasl_output_flag=1 so the side data persists for the entire
3115 // coded video sequence.
3116
5/6
✓ Branch 0 taken 562 times.
✓ Branch 1 taken 9900 times.
✓ Branch 2 taken 562 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 268 times.
✓ Branch 5 taken 294 times.
10462 if (IS_IRAP(s) && s->no_rasl_output_flag) {
3117
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 252 times.
268 if (s->sei.common.mastering_display.present > 0)
3118 16 s->sei.common.mastering_display.present--;
3119
3120
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 264 times.
268 if (s->sei.common.content_light.present > 0)
3121 4 s->sei.common.content_light.present--;
3122 }
3123
3124 10462 ret = ff_h2645_sei_to_frame(out, &s->sei.common, AV_CODEC_ID_HEVC, s->avctx,
3125 &sps->vui.common,
3126 10462 sps->bit_depth, sps->bit_depth_chroma,
3127 10462 s->cur_frame->poc /* no poc_offset in HEVC */);
3128
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10462 times.
10462 if (ret < 0)
3129 return ret;
3130
3131
2/2
✓ Branch 0 taken 20 times.
✓ Branch 1 taken 10442 times.
10462 if (s->sei.timecode.present) {
3132 uint32_t *tc_sd;
3133 char tcbuf[AV_TIMECODE_STR_SIZE];
3134 AVFrameSideData *tcside;
3135 20 ret = ff_frame_new_side_data(s->avctx, out, AV_FRAME_DATA_S12M_TIMECODE,
3136 sizeof(uint32_t) * 4, &tcside);
3137
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 20 times.
20 if (ret < 0)
3138 return ret;
3139
3140
1/2
✓ Branch 0 taken 20 times.
✗ Branch 1 not taken.
20 if (tcside) {
3141 20 tc_sd = (uint32_t*)tcside->data;
3142 20 tc_sd[0] = s->sei.timecode.num_clock_ts;
3143
3144
2/2
✓ Branch 0 taken 20 times.
✓ Branch 1 taken 20 times.
40 for (int i = 0; i < tc_sd[0]; i++) {
3145 20 int drop = s->sei.timecode.cnt_dropped_flag[i];
3146 20 int hh = s->sei.timecode.hours_value[i];
3147 20 int mm = s->sei.timecode.minutes_value[i];
3148 20 int ss = s->sei.timecode.seconds_value[i];
3149 20 int ff = s->sei.timecode.n_frames[i];
3150
3151 20 tc_sd[i + 1] = av_timecode_get_smpte(s->avctx->framerate, drop, hh, mm, ss, ff);
3152 20 av_timecode_make_smpte_tc_string2(tcbuf, s->avctx->framerate, tc_sd[i + 1], 0, 0);
3153 20 av_dict_set(&out->metadata, "timecode", tcbuf, 0);
3154 }
3155 }
3156
3157 20 s->sei.timecode.num_clock_ts = 0;
3158 }
3159
3160
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 10459 times.
10462 if (s->sei.common.itut_t35.hdr_plus) {
3161 3 AVBufferRef *info_ref = av_buffer_ref(s->sei.common.itut_t35.hdr_plus);
3162
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (!info_ref)
3163 return AVERROR(ENOMEM);
3164
3165 3 ret = ff_frame_new_side_data_from_buf(s->avctx, out, AV_FRAME_DATA_DYNAMIC_HDR_PLUS, &info_ref);
3166
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (ret < 0)
3167 return ret;
3168 }
3169
3170
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10462 times.
10462 if (s->sei.common.itut_t35.hdr_smpte2094_app5) {
3171 AVBufferRef *info_ref = av_buffer_ref(s->sei.common.itut_t35.hdr_smpte2094_app5);
3172 if (!info_ref)
3173 return AVERROR(ENOMEM);
3174
3175 ret = ff_frame_new_side_data_from_buf(s->avctx, out, AV_FRAME_DATA_DYNAMIC_HDR_SMPTE_2094_APP5, &info_ref);
3176 if (ret < 0)
3177 return ret;
3178 }
3179
3180
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 10460 times.
10462 if (s->rpu_buf) {
3181 2 AVFrameSideData *rpu = av_frame_new_side_data_from_buf(out, AV_FRAME_DATA_DOVI_RPU_BUFFER, s->rpu_buf);
3182
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (!rpu)
3183 return AVERROR(ENOMEM);
3184
3185 2 s->rpu_buf = NULL;
3186 }
3187
3188
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 10462 times.
10462 if ((ret = ff_dovi_attach_side_data(&s->dovi_ctx, out)) < 0)
3189 return ret;
3190
3191
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 10461 times.
10462 if (s->sei.common.itut_t35.hdr_vivid) {
3192
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 if (!av_frame_side_data_add(&out->side_data, &out->nb_side_data,
3193 AV_FRAME_DATA_DYNAMIC_HDR_VIVID,
3194 &s->sei.common.itut_t35.hdr_vivid,
3195 AV_FRAME_SIDE_DATA_FLAG_NEW_REF))
3196 return AVERROR(ENOMEM);
3197 }
3198
3199 10462 return 0;
3200 }
3201
3202 10072 static int find_finish_setup_nal(const HEVCContext *s)
3203 {
3204 10072 int nal_idx = 0;
3205
3206
2/2
✓ Branch 0 taken 38321 times.
✓ Branch 1 taken 10072 times.
48393 for (int i = nal_idx; i < s->pkt.nb_nals; i++) {
3207 38321 const H2645NAL *nal = &s->pkt.nals[i];
3208 38321 const int layer_id = nal->nuh_layer_id;
3209 38321 GetBitContext gb = nal->gb;
3210
3211
2/4
✓ Branch 0 taken 38321 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 38321 times.
✗ Branch 3 not taken.
38321 if (layer_id > HEVC_MAX_NUH_LAYER_ID || s->vps->layer_idx[layer_id] < 0 ||
3212
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 38321 times.
38321 !(s->layers_active_decode & (1 << s->vps->layer_idx[layer_id])))
3213 17996 continue;
3214
3215
3/3
✓ Branch 0 taken 28459 times.
✓ Branch 1 taken 1587 times.
✓ Branch 2 taken 8275 times.
38321 switch (nal->type) {
3216 28459 case HEVC_NAL_TRAIL_R:
3217 case HEVC_NAL_TRAIL_N:
3218 case HEVC_NAL_TSA_N:
3219 case HEVC_NAL_TSA_R:
3220 case HEVC_NAL_STSA_N:
3221 case HEVC_NAL_STSA_R:
3222 case HEVC_NAL_BLA_W_LP:
3223 case HEVC_NAL_BLA_W_RADL:
3224 case HEVC_NAL_BLA_N_LP:
3225 case HEVC_NAL_IDR_W_RADL:
3226 case HEVC_NAL_IDR_N_LP:
3227 case HEVC_NAL_CRA_NUT:
3228 case HEVC_NAL_RADL_N:
3229 case HEVC_NAL_RADL_R:
3230 case HEVC_NAL_RASL_N:
3231 case HEVC_NAL_RASL_R:
3232
2/2
✓ Branch 1 taken 17996 times.
✓ Branch 2 taken 10463 times.
28459 if (!get_bits1(&gb)) // first_slice_segment_in_pic_flag
3233 17996 continue;
3234 av_fallthrough;
3235 case HEVC_NAL_VPS:
3236 case HEVC_NAL_SPS:
3237 case HEVC_NAL_PPS:
3238 12050 nal_idx = i;
3239 12050 break;
3240 }
3241 }
3242
3243 10072 return nal_idx;
3244 }
3245
3246 10462 static int hevc_frame_start(HEVCContext *s, HEVCLayerContext *l,
3247 unsigned nal_idx)
3248 {
3249 10462 const HEVCPPS *const pps = s->ps.pps_list[s->sh.pps_id];
3250 10462 const HEVCSPS *const sps = pps->sps;
3251 10462 int pic_size_in_ctb = ((sps->width >> sps->log2_min_cb_size) + 1) *
3252 10462 ((sps->height >> sps->log2_min_cb_size) + 1);
3253
2/2
✓ Branch 0 taken 10072 times.
✓ Branch 1 taken 390 times.
20534 int new_sequence = (l == &s->layers[0]) &&
3254
12/12
✓ Branch 0 taken 9864 times.
✓ Branch 1 taken 208 times.
✓ Branch 2 taken 9836 times.
✓ Branch 3 taken 28 times.
✓ Branch 4 taken 9834 times.
✓ Branch 5 taken 2 times.
✓ Branch 6 taken 9827 times.
✓ Branch 7 taken 7 times.
✓ Branch 8 taken 9826 times.
✓ Branch 9 taken 1 times.
✓ Branch 10 taken 17 times.
✓ Branch 11 taken 9809 times.
10072 (IS_IDR(s) || IS_BLA(s) || s->last_eos);
3255 10462 int prev_layers_active_decode = s->layers_active_decode;
3256 10462 int prev_layers_active_output = s->layers_active_output;
3257 int ret;
3258
3259
3/4
✓ Branch 0 taken 230 times.
✓ Branch 1 taken 10232 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 230 times.
10462 if (sps->vps != s->vps && l != &s->layers[0]) {
3260 av_log(s->avctx, AV_LOG_ERROR, "VPS changed in a non-base layer\n");
3261 set_sps(s, l, NULL);
3262 return AVERROR_INVALIDDATA;
3263 }
3264
3265 10462 av_refstruct_replace(&s->pps, pps);
3266
2/2
✓ Branch 0 taken 248 times.
✓ Branch 1 taken 10214 times.
10462 if (l->sps != sps) {
3267 248 const HEVCSPS *sps_base = s->layers[0].sps;
3268 248 enum AVPixelFormat pix_fmt = sps->pix_fmt;
3269
3270
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 238 times.
248 if (l != &s->layers[0]) {
3271
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10 times.
10 if (!sps_base) {
3272 av_log(s->avctx, AV_LOG_ERROR,
3273 "Access unit starts with a non-base layer frame\n");
3274 return AVERROR_INVALIDDATA;
3275 }
3276
3277 // Files produced by Vision Pro lack VPS extension VUI,
3278 // so the secondary layer has no range information.
3279 // This check avoids failing in such a case.
3280
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10 times.
10 if (sps_base->pix_fmt == AV_PIX_FMT_YUVJ420P &&
3281 sps->pix_fmt == AV_PIX_FMT_YUV420P &&
3282 !sps->vui.common.video_signal_type_present_flag)
3283 pix_fmt = sps_base->pix_fmt;
3284
3285 // Ignore range mismatch between base layer and alpha layer
3286
2/2
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 9 times.
10 if (ff_hevc_is_alpha_video(s) &&
3287
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
1 sps_base->pix_fmt == AV_PIX_FMT_YUV420P &&
3288 pix_fmt == AV_PIX_FMT_YUVJ420P)
3289 1 pix_fmt = sps_base->pix_fmt;
3290
3291
1/2
✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
10 if (pix_fmt != sps_base->pix_fmt ||
3292
1/2
✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
10 sps->width != sps_base->width ||
3293
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10 times.
10 sps->height != sps_base->height) {
3294 av_log(s->avctx, AV_LOG_ERROR,
3295 "Base/non-base layer SPS have unsupported parameter combination\n");
3296 return AVERROR(ENOSYS);
3297 }
3298 }
3299
3300 248 ff_hevc_clear_refs(l);
3301
3302 248 ret = set_sps(s, l, sps);
3303
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 248 times.
248 if (ret < 0)
3304 return ret;
3305
3306
2/2
✓ Branch 0 taken 238 times.
✓ Branch 1 taken 10 times.
248 if (l == &s->layers[0]) {
3307 238 export_stream_params(s, sps);
3308
3309 238 ret = get_format(s, sps);
3310
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 238 times.
238 if (ret < 0) {
3311 set_sps(s, l, NULL);
3312 return ret;
3313 }
3314
3315 238 new_sequence = 1;
3316 }
3317 }
3318
3319 10462 memset(l->horizontal_bs, 0, l->bs_width * l->bs_height);
3320 10462 memset(l->vertical_bs, 0, l->bs_width * l->bs_height);
3321 10462 memset(l->cbf_luma, 0, sps->min_tb_width * sps->min_tb_height);
3322 10462 memset(l->is_pcm, 0, (sps->min_pu_width + 1) * (sps->min_pu_height + 1));
3323 10462 memset(l->tab_slice_address, -1, pic_size_in_ctb * sizeof(*l->tab_slice_address));
3324
3325
4/4
✓ Branch 0 taken 10245 times.
✓ Branch 1 taken 217 times.
✓ Branch 2 taken 33 times.
✓ Branch 3 taken 10212 times.
10462 if (IS_IDR(s))
3326 250 ff_hevc_clear_refs(l);
3327
3328 10462 s->slice_idx = 0;
3329 10462 s->first_nal_type = s->nal_unit_type;
3330 10462 s->poc = s->sh.poc;
3331
3332
3/4
✓ Branch 0 taken 562 times.
✓ Branch 1 taken 9900 times.
✓ Branch 2 taken 562 times.
✗ Branch 3 not taken.
10462 if (IS_IRAP(s)) {
3333
10/10
✓ Branch 0 taken 345 times.
✓ Branch 1 taken 217 times.
✓ Branch 2 taken 312 times.
✓ Branch 3 taken 33 times.
✓ Branch 4 taken 310 times.
✓ Branch 5 taken 2 times.
✓ Branch 6 taken 303 times.
✓ Branch 7 taken 7 times.
✓ Branch 8 taken 302 times.
✓ Branch 9 taken 1 times.
864 s->no_rasl_output_flag = IS_IDR(s) || IS_BLA(s) ||
3334
3/4
✓ Branch 0 taken 302 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 8 times.
✓ Branch 3 taken 294 times.
302 (s->nal_unit_type == HEVC_NAL_CRA_NUT && s->last_eos);
3335 562 s->recovery_poc = HEVC_RECOVERY_END;
3336 }
3337
3338
2/2
✓ Branch 0 taken 20 times.
✓ Branch 1 taken 10442 times.
10462 if (s->recovery_poc != HEVC_RECOVERY_END &&
3339
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 20 times.
20 s->sei.recovery_point.has_recovery_poc) {
3340 if (s->recovery_poc == HEVC_RECOVERY_UNSPECIFIED)
3341 s->recovery_poc = s->poc + s->sei.recovery_point.recovery_poc_cnt;
3342 else if (s->poc >= s->recovery_poc)
3343 s->recovery_poc = HEVC_RECOVERY_END;
3344 }
3345
3346 /* 8.3.1 */
3347
2/2
✓ Branch 0 taken 10120 times.
✓ Branch 1 taken 342 times.
10462 if (s->temporal_id == 0 &&
3348
2/2
✓ Branch 0 taken 7347 times.
✓ Branch 1 taken 2773 times.
10120 s->nal_unit_type != HEVC_NAL_TRAIL_N &&
3349
1/2
✓ Branch 0 taken 7347 times.
✗ Branch 1 not taken.
7347 s->nal_unit_type != HEVC_NAL_TSA_N &&
3350
1/2
✓ Branch 0 taken 7347 times.
✗ Branch 1 not taken.
7347 s->nal_unit_type != HEVC_NAL_STSA_N &&
3351
2/2
✓ Branch 0 taken 7321 times.
✓ Branch 1 taken 26 times.
7347 s->nal_unit_type != HEVC_NAL_RADL_N &&
3352
2/2
✓ Branch 0 taken 7299 times.
✓ Branch 1 taken 22 times.
7321 s->nal_unit_type != HEVC_NAL_RADL_R &&
3353
2/2
✓ Branch 0 taken 6913 times.
✓ Branch 1 taken 386 times.
7299 s->nal_unit_type != HEVC_NAL_RASL_N &&
3354
2/2
✓ Branch 0 taken 6436 times.
✓ Branch 1 taken 477 times.
6913 s->nal_unit_type != HEVC_NAL_RASL_R)
3355 6436 s->poc_tid0 = s->poc;
3356
3357
2/2
✓ Branch 0 taken 731 times.
✓ Branch 1 taken 9731 times.
10462 if (pps->tiles_enabled_flag)
3358 731 s->local_ctx[0].end_of_tiles_x = pps->column_width[0] << sps->log2_ctb_size;
3359
3360
2/2
✓ Branch 0 taken 263 times.
✓ Branch 1 taken 10199 times.
10462 if (new_sequence) {
3361 263 ret = ff_hevc_output_frames(s, prev_layers_active_decode, prev_layers_active_output,
3362 263 0, 0, s->sh.no_output_of_prior_pics_flag);
3363
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 263 times.
263 if (ret < 0)
3364 return ret;
3365 }
3366
3367 10462 ret = export_stream_params_from_sei(s);
3368
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10462 times.
10462 if (ret < 0)
3369 return ret;
3370
3371 10462 ret = ff_hevc_set_new_ref(s, l, s->poc);
3372
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10462 times.
10462 if (ret < 0)
3373 goto fail;
3374
3375 10462 ret = ff_hevc_frame_rps(s, l);
3376
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10462 times.
10462 if (ret < 0) {
3377 av_log(s->avctx, AV_LOG_ERROR, "Error constructing the frame RPS.\n");
3378 goto fail;
3379 }
3380
3381
3/4
✓ Branch 0 taken 562 times.
✓ Branch 1 taken 9900 times.
✓ Branch 2 taken 562 times.
✗ Branch 3 not taken.
10462 if (IS_IRAP(s))
3382 562 s->cur_frame->f->flags |= AV_FRAME_FLAG_KEY;
3383 else
3384 9900 s->cur_frame->f->flags &= ~AV_FRAME_FLAG_KEY;
3385
3386 20924 s->cur_frame->needs_fg = ((s->sei.common.film_grain_characteristics &&
3387 s->sei.common.film_grain_characteristics->present) ||
3388
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10462 times.
10462 s->sei.common.itut_t35.aom_film_grain.enable) &&
3389 !(s->avctx->export_side_data & AV_CODEC_EXPORT_DATA_FILM_GRAIN) &&
3390
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10462 times.
20924 !ff_decode_skip_all_pixels(s->avctx) &&
3391 !s->avctx->hwaccel;
3392
3393 10462 ret = set_side_data(s);
3394
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10462 times.
10462 if (ret < 0)
3395 goto fail;
3396
3397
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10462 times.
10462 if (s->cur_frame->needs_fg &&
3398 (s->sei.common.film_grain_characteristics && s->sei.common.film_grain_characteristics->present &&
3399 !ff_h274_film_grain_params_supported(s->sei.common.film_grain_characteristics->model_id,
3400 s->cur_frame->f->format) ||
3401 !av_film_grain_params_select(s->cur_frame->f))) {
3402 av_log_once(s->avctx, AV_LOG_WARNING, AV_LOG_DEBUG, &s->film_grain_warning_shown,
3403 "Unsupported film grain parameters. Ignoring film grain.\n");
3404 s->cur_frame->needs_fg = 0;
3405 }
3406
3407
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10462 times.
10462 if (s->cur_frame->needs_fg) {
3408 s->cur_frame->frame_grain->format = s->cur_frame->f->format;
3409 s->cur_frame->frame_grain->width = s->cur_frame->f->width;
3410 s->cur_frame->frame_grain->height = s->cur_frame->f->height;
3411 if ((ret = ff_thread_get_buffer(s->avctx, s->cur_frame->frame_grain, 0)) < 0)
3412 goto fail;
3413
3414 ret = av_frame_copy_props(s->cur_frame->frame_grain, s->cur_frame->f);
3415 if (ret < 0)
3416 goto fail;
3417 }
3418
3419 10462 s->cur_frame->f->pict_type = 3 - s->sh.slice_type;
3420
3421 10462 ret = ff_hevc_output_frames(s, s->layers_active_decode, s->layers_active_output,
3422 10462 sps->temporal_layer[sps->max_sub_layers - 1].num_reorder_pics,
3423 10462 sps->temporal_layer[sps->max_sub_layers - 1].max_dec_pic_buffering, 0);
3424
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10462 times.
10462 if (ret < 0)
3425 goto fail;
3426
3427
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10462 times.
10462 if (s->avctx->hwaccel) {
3428 AVCodecInternal *avci = s->avctx->internal;
3429 AVPacket *avpkt = avci->in_pkt;
3430 ret = FF_HW_CALL(s->avctx, start_frame,
3431 avpkt->buf, NULL, 0);
3432 if (ret < 0)
3433 goto fail;
3434 }
3435
3436 // after starting the base-layer frame we know which layers will be decoded,
3437 // so we can now figure out which NALUs to wait for before we can call
3438 // ff_thread_finish_setup()
3439
2/2
✓ Branch 0 taken 10072 times.
✓ Branch 1 taken 390 times.
10462 if (l == &s->layers[0])
3440 10072 s->finish_setup_nal_idx = find_finish_setup_nal(s);
3441
3442
2/2
✓ Branch 0 taken 10071 times.
✓ Branch 1 taken 391 times.
10462 if (nal_idx >= s->finish_setup_nal_idx)
3443 10071 ff_thread_finish_setup(s->avctx);
3444
3445 10462 return 0;
3446
3447 fail:
3448 if (l->cur_frame)
3449 ff_hevc_unref_frame(l->cur_frame, ~0);
3450 l->cur_frame = NULL;
3451 s->cur_frame = s->collocated_ref = NULL;
3452 return ret;
3453 }
3454
3455 static int verify_md5(HEVCContext *s, AVFrame *frame)
3456 {
3457 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(frame->format);
3458 char msg_buf[4 * (50 + 2 * 2 * 16 /* MD5-size */)];
3459 int pixel_shift;
3460 int err = 0;
3461 int i, j;
3462
3463 if (!desc)
3464 return AVERROR(EINVAL);
3465
3466 pixel_shift = desc->comp[0].depth > 8;
3467
3468 /* the checksums are LE, so we have to byteswap for >8bpp formats
3469 * on BE arches */
3470 #if HAVE_BIGENDIAN
3471 if (pixel_shift && !s->checksum_buf) {
3472 av_fast_malloc(&s->checksum_buf, &s->checksum_buf_size,
3473 FFMAX3(frame->linesize[0], frame->linesize[1],
3474 frame->linesize[2]));
3475 if (!s->checksum_buf)
3476 return AVERROR(ENOMEM);
3477 }
3478 #endif
3479
3480 msg_buf[0] = '\0';
3481 for (i = 0; frame->data[i]; i++) {
3482 int width = s->avctx->coded_width;
3483 int height = s->avctx->coded_height;
3484 int w = (i == 1 || i == 2) ? (width >> desc->log2_chroma_w) : width;
3485 int h = (i == 1 || i == 2) ? (height >> desc->log2_chroma_h) : height;
3486 uint8_t md5[16];
3487
3488 av_md5_init(s->md5_ctx);
3489 for (j = 0; j < h; j++) {
3490 const uint8_t *src = frame->data[i] + j * frame->linesize[i];
3491 #if HAVE_BIGENDIAN
3492 if (pixel_shift) {
3493 s->bdsp.bswap16_buf((uint16_t *) s->checksum_buf,
3494 (const uint16_t *) src, w);
3495 src = s->checksum_buf;
3496 }
3497 #endif
3498 av_md5_update(s->md5_ctx, src, w << pixel_shift);
3499 }
3500 av_md5_final(s->md5_ctx, md5);
3501
3502 #define MD5_PRI "%016" PRIx64 "%016" PRIx64
3503 #define MD5_PRI_ARG(buf) AV_RB64(buf), AV_RB64((const uint8_t*)(buf) + 8)
3504
3505 if (!memcmp(md5, s->sei.picture_hash.md5[i], 16)) {
3506 av_strlcatf(msg_buf, sizeof(msg_buf),
3507 "plane %d - correct " MD5_PRI "; ",
3508 i, MD5_PRI_ARG(md5));
3509 } else {
3510 av_strlcatf(msg_buf, sizeof(msg_buf),
3511 "mismatching checksum of plane %d - " MD5_PRI " != " MD5_PRI "; ",
3512 i, MD5_PRI_ARG(md5), MD5_PRI_ARG(s->sei.picture_hash.md5[i]));
3513 err = AVERROR_INVALIDDATA;
3514 }
3515 }
3516
3517 av_log(s->avctx, err < 0 ? AV_LOG_ERROR : AV_LOG_DEBUG,
3518 "Verifying checksum for frame with POC %d: %s\n",
3519 s->poc, msg_buf);
3520
3521 return err;
3522 }
3523
3524 10462 static int hevc_frame_end(HEVCContext *s, HEVCLayerContext *l)
3525 {
3526 10462 HEVCFrame *out = l->cur_frame;
3527 const AVFilmGrainParams *fgp;
3528 av_unused int ret;
3529
3530
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10462 times.
10462 if (out->needs_fg) {
3531 av_assert0(out->frame_grain->buf[0]);
3532 fgp = av_film_grain_params_select(out->f);
3533 switch (fgp->type) {
3534 case AV_FILM_GRAIN_PARAMS_NONE:
3535 av_assert0(0);
3536 return AVERROR_BUG;
3537 case AV_FILM_GRAIN_PARAMS_H274:
3538 ret = ff_h274_apply_film_grain(out->frame_grain, out->f, fgp);
3539 break;
3540 case AV_FILM_GRAIN_PARAMS_AV1:
3541 ret = ff_aom_apply_film_grain(out->frame_grain, out->f, fgp);
3542 break;
3543 }
3544 av_assert1(ret >= 0);
3545 }
3546
3547
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10462 times.
10462 if (s->avctx->hwaccel) {
3548 ret = FF_HW_SIMPLE_CALL(s->avctx, end_frame);
3549 if (ret < 0) {
3550 av_log(s->avctx, AV_LOG_ERROR,
3551 "hardware accelerator failed to decode picture\n");
3552 return ret;
3553 }
3554 } else {
3555
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10462 times.
10462 if (s->avctx->err_recognition & AV_EF_CRCCHECK &&
3556 s->sei.picture_hash.is_md5) {
3557 ret = verify_md5(s, out->f);
3558 if (ret < 0 && s->avctx->err_recognition & AV_EF_EXPLODE)
3559 return ret;
3560 }
3561 }
3562 10462 s->sei.picture_hash.is_md5 = 0;
3563
3564 10462 av_log(s->avctx, AV_LOG_DEBUG, "Decoded frame with POC %zu/%d.\n",
3565 10462 l - s->layers, s->poc);
3566
3567 10462 return 0;
3568 }
3569
3570 29028 static int decode_slice(HEVCContext *s, unsigned nal_idx, GetBitContext *gb)
3571 {
3572
2/2
✓ Branch 0 taken 28804 times.
✓ Branch 1 taken 224 times.
29028 const int layer_idx = s->vps ? s->vps->layer_idx[s->nuh_layer_id] : 0;
3573 HEVCLayerContext *l;
3574 int ret;
3575
3576 // skip layers not requested to be decoded
3577 // layers_active_decode can only change while decoding a base-layer frame,
3578 // so we can check it for non-base layers
3579
1/2
✓ Branch 0 taken 29028 times.
✗ Branch 1 not taken.
29028 if (layer_idx < 0 ||
3580
3/4
✓ Branch 0 taken 516 times.
✓ Branch 1 taken 28512 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 516 times.
29028 (s->nuh_layer_id > 0 && !(s->layers_active_decode & (1 << layer_idx))))
3581 return 0;
3582
3583 // the first slice of this picture was skipped, so drop the remaining
3584 // slices before parsing, the context is stale for them
3585
4/4
✓ Branch 0 taken 566 times.
✓ Branch 1 taken 28462 times.
✓ Branch 3 taken 486 times.
✓ Branch 4 taken 80 times.
29028 if (s->skipping_frame && !show_bits1(gb))
3586 486 return 0;
3587
3588 28542 ret = hls_slice_header(&s->sh, s, gb);
3589 // Once hls_slice_header has been called, the context is inconsistent with the slice header
3590 // until the context is reinitialized according to the contents of the new slice header
3591 // at the start of decode_slice_data.
3592 28542 s->slice_initialized = 0;
3593
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 28542 times.
28542 if (ret < 0) {
3594 return ret;
3595 }
3596
3597
4/4
✓ Branch 0 taken 81 times.
✓ Branch 1 taken 28461 times.
✓ Branch 2 taken 55 times.
✓ Branch 3 taken 26 times.
28542 if ((s->avctx->skip_frame >= AVDISCARD_BIDIR && s->sh.slice_type == HEVC_SLICE_B) ||
3598
4/4
✓ Branch 0 taken 55 times.
✓ Branch 1 taken 28461 times.
✓ Branch 2 taken 54 times.
✓ Branch 3 taken 1 times.
28516 (s->avctx->skip_frame >= AVDISCARD_NONINTRA && s->sh.slice_type != HEVC_SLICE_I) ||
3599
1/6
✗ Branch 0 not taken.
✓ Branch 1 taken 28515 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
28515 (s->avctx->skip_frame >= AVDISCARD_NONKEY && !IS_IRAP(s)) ||
3600
4/4
✓ Branch 0 taken 27381 times.
✓ Branch 1 taken 1134 times.
✓ Branch 2 taken 1067 times.
✓ Branch 3 taken 26314 times.
28515 ((s->nal_unit_type == HEVC_NAL_RASL_R || s->nal_unit_type == HEVC_NAL_RASL_N) &&
3601
2/2
✓ Branch 0 taken 56 times.
✓ Branch 1 taken 2145 times.
2201 s->no_rasl_output_flag)) {
3602
1/2
✓ Branch 0 taken 83 times.
✗ Branch 1 not taken.
83 if (s->sh.first_slice_in_pic_flag)
3603 83 s->skipping_frame = 1;
3604 83 return 0;
3605 }
3606
2/2
✓ Branch 0 taken 10463 times.
✓ Branch 1 taken 17996 times.
28459 if (s->sh.first_slice_in_pic_flag)
3607 10463 s->skipping_frame = 0;
3608
3609 // switching to a new layer, mark previous layer's frame (if any) as done
3610
2/2
✓ Branch 0 taken 771 times.
✓ Branch 1 taken 27688 times.
28459 if (s->cur_layer != layer_idx &&
3611
2/2
✓ Branch 0 taken 390 times.
✓ Branch 1 taken 381 times.
771 s->layers[s->cur_layer].cur_frame &&
3612
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 390 times.
390 s->avctx->active_thread_type == FF_THREAD_FRAME)
3613 ff_progress_frame_report(&s->layers[s->cur_layer].cur_frame->tf, INT_MAX);
3614
3615 28459 s->cur_layer = layer_idx;
3616 28459 l = &s->layers[s->cur_layer];
3617
3618
2/2
✓ Branch 0 taken 10463 times.
✓ Branch 1 taken 17996 times.
28459 if (s->sh.first_slice_in_pic_flag) {
3619
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 10462 times.
10463 if (l->cur_frame) {
3620 1 av_log(s->avctx, AV_LOG_ERROR, "Two slices reporting being the first in the same frame.\n");
3621 1 return AVERROR_INVALIDDATA;
3622 }
3623
3624 10462 ret = hevc_frame_start(s, l, nal_idx);
3625
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10462 times.
10462 if (ret < 0)
3626 return ret;
3627
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 17996 times.
17996 } else if (!l->cur_frame) {
3628 av_log(s->avctx, AV_LOG_ERROR, "First slice in a frame missing.\n");
3629 return AVERROR_INVALIDDATA;
3630 }
3631
3632
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 28458 times.
28458 if (s->nal_unit_type != s->first_nal_type) {
3633 av_log(s->avctx, AV_LOG_ERROR,
3634 "Non-matching NAL types of the VCL NALUs: %d %d\n",
3635 s->first_nal_type, s->nal_unit_type);
3636 return AVERROR_INVALIDDATA;
3637 }
3638
3639 28458 ret = decode_slice_data(s, l, &s->pkt.nals[nal_idx], gb);
3640
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 28457 times.
28458 if (ret < 0)
3641 1 return ret;
3642
3643 28457 return 0;
3644 }
3645
3646 40198 static int decode_nal_unit(HEVCContext *s, unsigned nal_idx)
3647 {
3648 40198 H2645NAL *nal = &s->pkt.nals[nal_idx];
3649 40198 GetBitContext gb = nal->gb;
3650 int ret;
3651
3652 40198 s->nal_unit_type = nal->type;
3653 40198 s->nuh_layer_id = nal->nuh_layer_id;
3654 40198 s->temporal_id = nal->temporal_id;
3655
3656
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 40198 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
40198 if (FF_HW_HAS_CB(s->avctx, decode_params) &&
3657 (s->nal_unit_type == HEVC_NAL_VPS ||
3658 s->nal_unit_type == HEVC_NAL_SPS ||
3659 s->nal_unit_type == HEVC_NAL_PPS ||
3660 s->nal_unit_type == HEVC_NAL_SEI_PREFIX ||
3661 s->nal_unit_type == HEVC_NAL_SEI_SUFFIX)) {
3662 ret = FF_HW_CALL(s->avctx, decode_params,
3663 nal->type, nal->raw_data, nal->raw_size);
3664 if (ret < 0)
3665 goto fail;
3666 }
3667
3668
6/7
✓ Branch 0 taken 463 times.
✓ Branch 1 taken 473 times.
✓ Branch 2 taken 1434 times.
✓ Branch 3 taken 8126 times.
✓ Branch 4 taken 29028 times.
✓ Branch 5 taken 674 times.
✗ Branch 6 not taken.
40198 switch (s->nal_unit_type) {
3669 463 case HEVC_NAL_VPS:
3670 463 ret = ff_hevc_decode_nal_vps(&gb, s->avctx, &s->ps);
3671
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 463 times.
463 if (ret < 0)
3672 goto fail;
3673 463 break;
3674 473 case HEVC_NAL_SPS:
3675 473 ret = ff_hevc_decode_nal_sps(&gb, s->avctx, &s->ps,
3676 473 nal->nuh_layer_id, s->apply_defdispwin);
3677
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 473 times.
473 if (ret < 0)
3678 goto fail;
3679 473 break;
3680 1434 case HEVC_NAL_PPS:
3681 1434 ret = ff_hevc_decode_nal_pps(&gb, s->avctx, &s->ps);
3682
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1432 times.
1434 if (ret < 0)
3683 2 goto fail;
3684 1432 break;
3685 8126 case HEVC_NAL_SEI_PREFIX:
3686 case HEVC_NAL_SEI_SUFFIX:
3687 8126 ret = ff_hevc_decode_nal_sei(&gb, s->avctx, &s->sei, &s->ps, s->nal_unit_type);
3688
2/2
✓ Branch 0 taken 55 times.
✓ Branch 1 taken 8071 times.
8126 if (ret < 0)
3689 55 goto fail;
3690 8071 break;
3691 29028 case HEVC_NAL_TRAIL_R:
3692 case HEVC_NAL_TRAIL_N:
3693 case HEVC_NAL_TSA_N:
3694 case HEVC_NAL_TSA_R:
3695 case HEVC_NAL_STSA_N:
3696 case HEVC_NAL_STSA_R:
3697 case HEVC_NAL_BLA_W_LP:
3698 case HEVC_NAL_BLA_W_RADL:
3699 case HEVC_NAL_BLA_N_LP:
3700 case HEVC_NAL_IDR_W_RADL:
3701 case HEVC_NAL_IDR_N_LP:
3702 case HEVC_NAL_CRA_NUT:
3703 case HEVC_NAL_RADL_N:
3704 case HEVC_NAL_RADL_R:
3705 case HEVC_NAL_RASL_N:
3706 case HEVC_NAL_RASL_R:
3707 29028 ret = decode_slice(s, nal_idx, &gb);
3708
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 29026 times.
29028 if (ret < 0)
3709 2 goto fail;
3710 29026 break;
3711 674 case HEVC_NAL_EOS_NUT:
3712 case HEVC_NAL_EOB_NUT:
3713 case HEVC_NAL_AUD:
3714 case HEVC_NAL_FD_NUT:
3715 case HEVC_NAL_UNSPEC62: // Dolby Vision RPU
3716 case HEVC_NAL_UNSPEC63: // Dolby Vision EL
3717 674 break;
3718 default:
3719 av_log(s->avctx, AV_LOG_VERBOSE,
3720 "Skipping NAL unit %d\n", s->nal_unit_type);
3721 }
3722
3723 40139 return 0;
3724 59 fail:
3725
1/2
✓ Branch 0 taken 59 times.
✗ Branch 1 not taken.
59 if (ret == AVERROR_INVALIDDATA &&
3726
1/2
✓ Branch 0 taken 59 times.
✗ Branch 1 not taken.
59 !(s->avctx->err_recognition & AV_EF_EXPLODE)) {
3727 59 av_log(s->avctx, AV_LOG_WARNING,
3728 59 "Skipping invalid undecodable NALU: %d\n", s->nal_unit_type);
3729 59 return 0;
3730 }
3731 return ret;
3732 }
3733
3734 455 static void decode_reset_recovery_point(HEVCContext *s)
3735 {
3736 455 s->recovery_poc = HEVC_RECOVERY_UNSPECIFIED;
3737 455 s->sei.recovery_point.has_recovery_poc = 0;
3738 455 }
3739
3740 359 static int export_stream_params_from_slice(HEVCContext *s, const H2645NAL *nal)
3741 {
3742 359 GetBitContext gb = nal->gb;
3743 const HEVCSPS *sps;
3744 const HEVCVPS *vps;
3745 unsigned pps_id;
3746
3747
2/2
✓ Branch 0 taken 304 times.
✓ Branch 1 taken 55 times.
663 int is_slice = nal->type <= HEVC_NAL_RASL_R ||
3748
1/2
✓ Branch 0 taken 304 times.
✗ Branch 1 not taken.
304 (nal->type >= HEVC_NAL_BLA_W_LP &&
3749
2/2
✓ Branch 0 taken 226 times.
✓ Branch 1 taken 78 times.
304 nal->type <= HEVC_NAL_CRA_NUT);
3750
3751
3/4
✓ Branch 0 taken 281 times.
✓ Branch 1 taken 78 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 281 times.
359 if (!is_slice || nal->nuh_layer_id)
3752 78 return 0;
3753
3754 281 skip_bits1(&gb); // first_slice_segment_in_pic_flag
3755
2/2
✓ Branch 0 taken 226 times.
✓ Branch 1 taken 55 times.
281 if (nal->type >= HEVC_NAL_BLA_W_LP)
3756 226 skip_bits1(&gb); // no_output_of_prior_pics_flag
3757 281 pps_id = get_ue_golomb_long(&gb);
3758
3/4
✓ Branch 0 taken 281 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 55 times.
✓ Branch 3 taken 226 times.
281 if (pps_id >= HEVC_MAX_PPS_COUNT || !s->ps.pps_list[pps_id])
3759 55 return 0;
3760 226 sps = s->ps.pps_list[pps_id]->sps;
3761 226 vps = sps->vps;
3762
3763 226 export_stream_params(s, sps);
3764
3765
3/4
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 221 times.
✓ Branch 2 taken 5 times.
✗ Branch 3 not taken.
226 if (vps->nb_layers == 2 && vps->layer_id_in_nuh[1] &&
3766
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
5 vps->scalability_mask_flag & HEVC_SCALABILITY_AUXILIARY) {
3767 enum AVPixelFormat alpha_fmt = map_to_alpha_format(s, sps->pix_fmt);
3768
3769 if (alpha_fmt != AV_PIX_FMT_NONE)
3770 s->avctx->pix_fmt = alpha_fmt;
3771 }
3772
3773 226 return 1;
3774 }
3775
3776 10466 static int decode_nal_units(HEVCContext *s, const uint8_t *buf, int length)
3777 {
3778 10466 int ret = 0;
3779 10466 int eos_at_start = 1;
3780 10466 int params_exported = 0;
3781
2/2
✓ Branch 0 taken 375 times.
✓ Branch 1 taken 10091 times.
10466 int flags = (H2645_FLAG_IS_NALFF * !!s->is_nalff) | H2645_FLAG_SMALL_PADDING;
3782
3783 10466 s->cur_frame = s->collocated_ref = NULL;
3784 10466 s->last_eos = s->eos;
3785 10466 s->eos = 0;
3786 10466 s->slice_initialized = 0;
3787
2/2
✓ Branch 0 taken 450 times.
✓ Branch 1 taken 10016 times.
10466 if (s->last_eos)
3788 450 decode_reset_recovery_point(s);
3789
3790
2/2
✓ Branch 0 taken 20932 times.
✓ Branch 1 taken 10466 times.
31398 for (int i = 0; i < FF_ARRAY_ELEMS(s->layers); i++) {
3791 20932 HEVCLayerContext *l = &s->layers[i];
3792 20932 l->cur_frame = NULL;
3793 }
3794
3795 /* split the input packet into NAL units, so we know the upper bound on the
3796 * number of slices in the frame */
3797 10466 ret = ff_h2645_packet_split(&s->pkt, buf, length, s->avctx,
3798 10466 s->nal_length_size, s->avctx->codec_id, flags);
3799
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10466 times.
10466 if (ret < 0) {
3800 av_log(s->avctx, AV_LOG_ERROR,
3801 "Error splitting the input into NAL units.\n");
3802 return ret;
3803 }
3804
3805
2/2
✓ Branch 0 taken 41409 times.
✓ Branch 1 taken 10466 times.
51875 for (int i = 0; i < s->pkt.nb_nals; i++) {
3806
2/2
✓ Branch 0 taken 41408 times.
✓ Branch 1 taken 1 times.
41409 if (s->pkt.nals[i].type == HEVC_NAL_EOB_NUT ||
3807
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 41404 times.
41408 s->pkt.nals[i].type == HEVC_NAL_EOS_NUT) {
3808
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
5 if (eos_at_start) {
3809 5 s->last_eos = 1;
3810 5 decode_reset_recovery_point(s);
3811 } else {
3812 s->eos = 1;
3813 }
3814 } else {
3815 41404 eos_at_start = 0;
3816 }
3817 }
3818
3819 /*
3820 * Check for RPU delimiter.
3821 *
3822 * Dolby Vision RPUs masquerade as unregistered NALs of type 62.
3823 *
3824 * We have to do this check here an create the rpu buffer, since RPUs are appended
3825 * to the end of an AU; they are normally the last non-EOB/EOS NAL in the AU.
3826 */
3827 10466 H2645NAL *rpu_nal = NULL;
3828
2/2
✓ Branch 0 taken 30942 times.
✓ Branch 1 taken 10464 times.
41406 for (int i = s->pkt.nb_nals - 1; i > 0 ; i--) {
3829
3/4
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 30940 times.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
30942 if (s->pkt.nals[i].type == HEVC_NAL_UNSPEC62 && s->pkt.nals[i].size > 2
3830
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
2 && !s->pkt.nals[i].nuh_layer_id && !s->pkt.nals[i].temporal_id) {
3831 2 rpu_nal = &s->pkt.nals[i];
3832 2 break;
3833 }
3834 }
3835
3836
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 10464 times.
10466 if (rpu_nal) {
3837
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (s->rpu_buf) {
3838 av_buffer_unref(&s->rpu_buf);
3839 av_log(s->avctx, AV_LOG_WARNING, "Multiple Dolby Vision RPUs found in one AU. Skipping previous.\n");
3840 }
3841
3842 2 s->rpu_buf = av_buffer_alloc(rpu_nal->raw_size - 2);
3843
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (!s->rpu_buf) {
3844 ret = AVERROR(ENOMEM);
3845 goto fail;
3846 }
3847 2 memcpy(s->rpu_buf->data, rpu_nal->raw_data + 2, rpu_nal->raw_size - 2);
3848
3849 2 ret = ff_dovi_rpu_parse(&s->dovi_ctx, rpu_nal->data + 2, rpu_nal->size - 2,
3850 2 s->avctx->err_recognition);
3851
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (ret < 0) {
3852 av_buffer_unref(&s->rpu_buf);
3853 av_log(s->avctx, AV_LOG_WARNING, "Error parsing DOVI NAL unit.\n");
3854 /* ignore */
3855 }
3856 }
3857
3858 /* decode the NAL units */
3859
2/2
✓ Branch 0 taken 41409 times.
✓ Branch 1 taken 10466 times.
51875 for (int i = 0; i < s->pkt.nb_nals; i++) {
3860 41409 H2645NAL *nal = &s->pkt.nals[i];
3861
3862
2/2
✓ Branch 0 taken 1883 times.
✓ Branch 1 taken 39526 times.
41409 if (s->avctx->skip_frame >= AVDISCARD_ALL) {
3863
2/2
✓ Branch 0 taken 1177 times.
✓ Branch 1 taken 706 times.
1883 switch (nal->type) {
3864 1177 case HEVC_NAL_VPS:
3865 case HEVC_NAL_SPS:
3866 case HEVC_NAL_PPS:
3867 case HEVC_NAL_SEI_PREFIX:
3868 case HEVC_NAL_SEI_SUFFIX:
3869 1177 break;
3870 706 default:
3871
3/4
✓ Branch 0 taken 706 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 359 times.
✓ Branch 3 taken 347 times.
706 if (!s->layers[0].sps && !params_exported)
3872 359 params_exported = export_stream_params_from_slice(s, nal);
3873 706 continue;
3874 }
3875
4/4
✓ Branch 0 taken 1095 times.
✓ Branch 1 taken 38431 times.
✓ Branch 2 taken 505 times.
✓ Branch 3 taken 590 times.
40621 } else if (s->avctx->skip_frame >= AVDISCARD_NONREF &&
3876 1095 ff_hevc_nal_is_nonref(nal->type))
3877 505 continue;
3878
3879 40198 ret = decode_nal_unit(s, i);
3880
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 40198 times.
40198 if (ret < 0) {
3881 av_log(s->avctx, AV_LOG_WARNING,
3882 "Error parsing NAL unit #%d.\n", i);
3883 goto fail;
3884 }
3885 }
3886
3887
2/2
✓ Branch 0 taken 10240 times.
✓ Branch 1 taken 226 times.
10466 if (params_exported) {
3888 226 ret = export_stream_params_from_sei(s);
3889
1/2
✓ Branch 0 taken 226 times.
✗ Branch 1 not taken.
226 if (ret < 0)
3890 goto fail;
3891 }
3892
3893 10466 fail:
3894
2/2
✓ Branch 0 taken 20932 times.
✓ Branch 1 taken 10466 times.
31398 for (int i = 0; i < FF_ARRAY_ELEMS(s->layers); i++) {
3895 20932 HEVCLayerContext *l = &s->layers[i];
3896
3897
2/2
✓ Branch 0 taken 10470 times.
✓ Branch 1 taken 10462 times.
20932 if (!l->cur_frame)
3898 10470 continue;
3899
3900
1/2
✓ Branch 0 taken 10462 times.
✗ Branch 1 not taken.
10462 if (ret >= 0)
3901 10462 ret = hevc_frame_end(s, l);
3902
3903
2/2
✓ Branch 0 taken 33 times.
✓ Branch 1 taken 10429 times.
10462 if (s->avctx->active_thread_type == FF_THREAD_FRAME)
3904 33 ff_progress_frame_report(&l->cur_frame->tf, INT_MAX);
3905 }
3906
3907 10466 return ret;
3908 }
3909
3910 361 static int hevc_decode_extradata(HEVCContext *s, uint8_t *buf, int length, int first)
3911 {
3912 int ret, i;
3913
3914 361 ret = ff_hevc_decode_extradata(buf, length, &s->ps, &s->sei, &s->is_nalff,
3915 361 &s->nal_length_size, s->avctx->err_recognition,
3916 361 s->apply_defdispwin, s->avctx);
3917
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 361 times.
361 if (ret < 0)
3918 return ret;
3919
3920 /* export stream parameters from the first SPS */
3921
2/2
✓ Branch 0 taken 391 times.
✓ Branch 1 taken 2 times.
393 for (i = 0; i < FF_ARRAY_ELEMS(s->ps.sps_list); i++) {
3922
4/4
✓ Branch 0 taken 375 times.
✓ Branch 1 taken 16 times.
✓ Branch 2 taken 359 times.
✓ Branch 3 taken 16 times.
391 if (first && s->ps.sps_list[i]) {
3923 359 const HEVCSPS *sps = s->ps.sps_list[i];
3924 359 export_stream_params(s, sps);
3925
3926 359 ret = export_multilayer(s, sps->vps);
3927
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 359 times.
359 if (ret < 0)
3928 return ret;
3929
3930 359 break;
3931 }
3932 }
3933
3934 /* export stream parameters from SEI */
3935 361 ret = export_stream_params_from_sei(s);
3936
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 361 times.
361 if (ret < 0)
3937 return ret;
3938
3939 361 return 0;
3940 }
3941
3942 22262 static int hevc_receive_frame(AVCodecContext *avctx, AVFrame *frame)
3943 {
3944 22262 HEVCContext *s = avctx->priv_data;
3945 22262 AVCodecInternal *avci = avctx->internal;
3946 22262 AVPacket *avpkt = avci->in_pkt;
3947
3948 int ret;
3949 uint8_t *sd;
3950 size_t sd_size;
3951
3952 22262 s->pkt_dts = AV_NOPTS_VALUE;
3953
3954
2/2
✓ Branch 1 taken 974 times.
✓ Branch 2 taken 21288 times.
22262 if (av_container_fifo_can_read(s->output_fifo))
3955 974 goto do_output;
3956
3957 21288 av_packet_unref(avpkt);
3958 21288 ret = ff_decode_get_packet(avctx, avpkt);
3959
2/2
✓ Branch 0 taken 356 times.
✓ Branch 1 taken 20932 times.
21288 if (ret == AVERROR_EOF) {
3960 356 ret = ff_hevc_output_frames(s, s->layers_active_decode,
3961 s->layers_active_output, 0, 0, 0);
3962
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 356 times.
356 if (ret < 0)
3963 return ret;
3964 356 goto do_output;
3965
2/2
✓ Branch 0 taken 10466 times.
✓ Branch 1 taken 10466 times.
20932 } else if (ret < 0)
3966 10466 return ret;
3967
3968 10466 s->pkt_dts = avpkt->dts;
3969
3970 10466 sd = av_packet_get_side_data(avpkt, AV_PKT_DATA_NEW_EXTRADATA, &sd_size);
3971
3/4
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 10465 times.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
10466 if (sd && sd_size > 0) {
3972 1 ret = hevc_decode_extradata(s, sd, sd_size, 0);
3973
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (ret < 0)
3974 return ret;
3975 }
3976
3977 10466 sd = av_packet_get_side_data(avpkt, AV_PKT_DATA_DOVI_CONF, &sd_size);
3978
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 10466 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
10466 if (sd && sd_size >= sizeof(s->dovi_ctx.cfg)) {
3979 int old = s->dovi_ctx.cfg.dv_profile;
3980 s->dovi_ctx.cfg = *(AVDOVIDecoderConfigurationRecord *) sd;
3981 if (old)
3982 av_log(avctx, AV_LOG_DEBUG,
3983 "New DOVI configuration record from input packet (profile %d -> %u).\n",
3984 old, s->dovi_ctx.cfg.dv_profile);
3985 }
3986
3987 10466 ret = decode_nal_units(s, avpkt->data, avpkt->size);
3988
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10466 times.
10466 if (ret < 0)
3989 return ret;
3990
3991 10466 do_output:
3992
2/2
✓ Branch 1 taken 10308 times.
✓ Branch 2 taken 1488 times.
11796 if (av_container_fifo_read(s->output_fifo, frame, 0) >= 0) {
3993
1/2
✓ Branch 0 taken 10308 times.
✗ Branch 1 not taken.
10308 if (!(avctx->export_side_data & AV_CODEC_EXPORT_DATA_FILM_GRAIN))
3994 10308 av_frame_remove_side_data(frame, AV_FRAME_DATA_FILM_GRAIN_PARAMS);
3995
3996 10308 return 0;
3997 }
3998
3999
2/2
✓ Branch 0 taken 222 times.
✓ Branch 1 taken 1266 times.
1488 return avci->draining ? AVERROR_EOF : AVERROR(EAGAIN);
4000 }
4001
4002 67 static int hevc_ref_frame(HEVCFrame *dst, const HEVCFrame *src)
4003 {
4004 int ret;
4005
4006 67 ff_progress_frame_ref(&dst->tf, &src->tf);
4007
4008
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 67 times.
67 if (src->needs_fg) {
4009 ret = av_frame_ref(dst->frame_grain, src->frame_grain);
4010 if (ret < 0) {
4011 ff_hevc_unref_frame(dst, ~0);
4012 return ret;
4013 }
4014 dst->needs_fg = 1;
4015 }
4016
4017 67 dst->pps = av_refstruct_ref_c(src->pps);
4018 67 dst->tab_mvf = av_refstruct_ref(src->tab_mvf);
4019 67 dst->rpl_tab = av_refstruct_ref(src->rpl_tab);
4020 67 dst->rpl = av_refstruct_ref(src->rpl);
4021 67 dst->nb_rpl_elems = src->nb_rpl_elems;
4022
4023 67 dst->poc = src->poc;
4024 67 dst->ctb_count = src->ctb_count;
4025 67 dst->flags = src->flags;
4026
4027 67 dst->base_layer_frame = src->base_layer_frame;
4028
4029 67 av_refstruct_replace(&dst->hwaccel_picture_private,
4030 67 src->hwaccel_picture_private);
4031
4032 67 return 0;
4033 }
4034
4035 587 static av_cold int hevc_decode_free(AVCodecContext *avctx)
4036 {
4037 587 HEVCContext *s = avctx->priv_data;
4038
4039
2/2
✓ Branch 0 taken 1174 times.
✓ Branch 1 taken 587 times.
1761 for (int i = 0; i < FF_ARRAY_ELEMS(s->layers); i++) {
4040 1174 pic_arrays_free(&s->layers[i]);
4041 1174 av_refstruct_unref(&s->layers[i].sps);
4042 }
4043
4044 587 av_refstruct_unref(&s->vps);
4045 587 av_refstruct_unref(&s->pps);
4046
4047 587 ff_dovi_ctx_unref(&s->dovi_ctx);
4048 587 av_buffer_unref(&s->rpu_buf);
4049
4050 587 av_freep(&s->md5_ctx);
4051
4052 587 av_container_fifo_free(&s->output_fifo);
4053
4054
2/2
✓ Branch 0 taken 1174 times.
✓ Branch 1 taken 587 times.
1761 for (int layer = 0; layer < FF_ARRAY_ELEMS(s->layers); layer++) {
4055 1174 HEVCLayerContext *l = &s->layers[layer];
4056
2/2
✓ Branch 0 taken 37568 times.
✓ Branch 1 taken 1174 times.
38742 for (int i = 0; i < FF_ARRAY_ELEMS(l->DPB); i++) {
4057 37568 ff_hevc_unref_frame(&l->DPB[i], ~0);
4058 37568 av_frame_free(&l->DPB[i].frame_grain);
4059 }
4060 }
4061
4062 587 ff_hevc_ps_uninit(&s->ps);
4063
4064
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 587 times.
587 for (int i = 0; i < s->nb_wpp_progress; i++)
4065 ff_thread_progress_destroy(&s->wpp_progress[i]);
4066 587 av_freep(&s->wpp_progress);
4067
4068 587 av_freep(&s->sh.entry_point_offset);
4069 587 av_freep(&s->sh.offset);
4070 587 av_freep(&s->sh.size);
4071
4072 587 av_freep(&s->local_ctx);
4073
4074 587 ff_h2645_packet_uninit(&s->pkt);
4075
4076 587 ff_hevc_reset_sei(&s->sei);
4077
4078 587 return 0;
4079 }
4080
4081 587 static av_cold int hevc_init_context(AVCodecContext *avctx)
4082 {
4083 587 HEVCContext *s = avctx->priv_data;
4084
4085 587 s->avctx = avctx;
4086
4087 587 s->local_ctx = av_mallocz(sizeof(*s->local_ctx));
4088
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 587 times.
587 if (!s->local_ctx)
4089 return AVERROR(ENOMEM);
4090 587 s->nb_local_ctx = 1;
4091
4092 587 s->local_ctx[0].parent = s;
4093 587 s->local_ctx[0].logctx = avctx;
4094 587 s->local_ctx[0].common_cabac_state = &s->cabac;
4095
4096 587 s->output_fifo = av_container_fifo_alloc_avframe(0);
4097
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 587 times.
587 if (!s->output_fifo)
4098 return AVERROR(ENOMEM);
4099
4100
2/2
✓ Branch 0 taken 1174 times.
✓ Branch 1 taken 587 times.
1761 for (int layer = 0; layer < FF_ARRAY_ELEMS(s->layers); layer++) {
4101 1174 HEVCLayerContext *l = &s->layers[layer];
4102
2/2
✓ Branch 0 taken 37568 times.
✓ Branch 1 taken 1174 times.
38742 for (int i = 0; i < FF_ARRAY_ELEMS(l->DPB); i++) {
4103 37568 l->DPB[i].frame_grain = av_frame_alloc();
4104
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 37568 times.
37568 if (!l->DPB[i].frame_grain)
4105 return AVERROR(ENOMEM);
4106 }
4107 }
4108
4109 587 s->md5_ctx = av_md5_alloc();
4110
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 587 times.
587 if (!s->md5_ctx)
4111 return AVERROR(ENOMEM);
4112
4113 587 ff_bswapdsp_init(&s->bdsp);
4114
4115 587 s->dovi_ctx.logctx = avctx;
4116 587 s->eos = 0;
4117
4118 587 ff_hevc_reset_sei(&s->sei);
4119
4120 587 return 0;
4121 }
4122
4123 #if HAVE_THREADS
4124 32 static int hevc_update_thread_context(AVCodecContext *dst,
4125 const AVCodecContext *src)
4126 {
4127 32 HEVCContext *s = dst->priv_data;
4128 32 HEVCContext *s0 = src->priv_data;
4129 int ret;
4130
4131
2/2
✓ Branch 0 taken 64 times.
✓ Branch 1 taken 32 times.
96 for (int layer = 0; layer < FF_ARRAY_ELEMS(s->layers); layer++) {
4132 64 HEVCLayerContext *l = &s->layers[layer];
4133 64 const HEVCLayerContext *l0 = &s0->layers[layer];
4134
2/2
✓ Branch 0 taken 2048 times.
✓ Branch 1 taken 64 times.
2112 for (int i = 0; i < FF_ARRAY_ELEMS(l->DPB); i++) {
4135 2048 ff_hevc_unref_frame(&l->DPB[i], ~0);
4136
2/2
✓ Branch 0 taken 67 times.
✓ Branch 1 taken 1981 times.
2048 if (l0->DPB[i].f) {
4137 67 ret = hevc_ref_frame(&l->DPB[i], &l0->DPB[i]);
4138
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 67 times.
67 if (ret < 0)
4139 return ret;
4140 }
4141 }
4142
4143
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 63 times.
64 if (l->sps != l0->sps) {
4144 1 ret = set_sps(s, l, l0->sps);
4145
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (ret < 0)
4146 return ret;
4147 }
4148 }
4149
4150
2/2
✓ Branch 0 taken 512 times.
✓ Branch 1 taken 32 times.
544 for (int i = 0; i < FF_ARRAY_ELEMS(s->ps.vps_list); i++)
4151 512 av_refstruct_replace(&s->ps.vps_list[i], s0->ps.vps_list[i]);
4152
4153
2/2
✓ Branch 0 taken 512 times.
✓ Branch 1 taken 32 times.
544 for (int i = 0; i < FF_ARRAY_ELEMS(s->ps.sps_list); i++)
4154 512 av_refstruct_replace(&s->ps.sps_list[i], s0->ps.sps_list[i]);
4155
4156
2/2
✓ Branch 0 taken 2048 times.
✓ Branch 1 taken 32 times.
2080 for (int i = 0; i < FF_ARRAY_ELEMS(s->ps.pps_list); i++)
4157 2048 av_refstruct_replace(&s->ps.pps_list[i], s0->ps.pps_list[i]);
4158
4159 // PPS do not persist between frames
4160 32 av_refstruct_unref(&s->pps);
4161
4162 32 s->poc_tid0 = s0->poc_tid0;
4163 32 s->eos = s0->eos;
4164 32 s->no_rasl_output_flag = s0->no_rasl_output_flag;
4165 32 s->skipping_frame = s0->skipping_frame;
4166
4167 32 s->is_nalff = s0->is_nalff;
4168 32 s->nal_length_size = s0->nal_length_size;
4169 32 s->layers_active_decode = s0->layers_active_decode;
4170 32 s->layers_active_output = s0->layers_active_output;
4171
4172 32 s->film_grain_warning_shown = s0->film_grain_warning_shown;
4173
4174
1/2
✓ Branch 0 taken 32 times.
✗ Branch 1 not taken.
32 if (s->nb_view_ids != s0->nb_view_ids ||
4175
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 32 times.
32 memcmp(s->view_ids, s0->view_ids, sizeof(*s->view_ids) * s->nb_view_ids)) {
4176 av_freep(&s->view_ids);
4177 s->nb_view_ids = 0;
4178
4179 if (s0->nb_view_ids) {
4180 s->view_ids = av_memdup(s0->view_ids, s0->nb_view_ids * sizeof(*s0->view_ids));
4181 if (!s->view_ids)
4182 return AVERROR(ENOMEM);
4183 s->nb_view_ids = s0->nb_view_ids;
4184 }
4185 }
4186
4187 32 ret = ff_h2645_sei_ctx_replace(&s->sei.common, &s0->sei.common);
4188
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 32 times.
32 if (ret < 0)
4189 return ret;
4190
4191 32 ret = av_buffer_replace(&s->sei.common.itut_t35.hdr_plus,
4192 32 s0->sei.common.itut_t35.hdr_plus);
4193
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 32 times.
32 if (ret < 0)
4194 return ret;
4195
4196 32 ret = av_buffer_replace(&s->rpu_buf, s0->rpu_buf);
4197
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 32 times.
32 if (ret < 0)
4198 return ret;
4199
4200 32 ff_dovi_ctx_replace(&s->dovi_ctx, &s0->dovi_ctx);
4201
4202 32 ret = av_buffer_replace(&s->sei.common.itut_t35.hdr_vivid,
4203 32 s0->sei.common.itut_t35.hdr_vivid);
4204
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 32 times.
32 if (ret < 0)
4205 return ret;
4206
4207 32 s->sei.common.frame_packing = s0->sei.common.frame_packing;
4208 32 s->sei.common.display_orientation = s0->sei.common.display_orientation;
4209 32 s->sei.common.alternative_transfer = s0->sei.common.alternative_transfer;
4210 32 s->sei.tdrdi = s0->sei.tdrdi;
4211 32 s->sei.recovery_point = s0->sei.recovery_point;
4212 32 s->recovery_poc = s0->recovery_poc;
4213
4214 32 return 0;
4215 }
4216 #endif
4217
4218 360 static int hevc_sei_to_context(AVCodecContext *avctx, HEVCSEI *sei)
4219 {
4220 int ret;
4221
4222
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 356 times.
360 if (sei->tdrdi.present) {
4223 AVBufferRef *buf;
4224 size_t size;
4225 4 AV3DReferenceDisplaysInfo *tdrdi = av_tdrdi_alloc(sei->tdrdi.num_ref_displays, &size);
4226
4227
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (!tdrdi)
4228 return AVERROR(ENOMEM);
4229
4230 4 buf = av_buffer_create((uint8_t *)tdrdi, size, NULL, NULL, 0);
4231
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (!buf) {
4232 av_free(tdrdi);
4233 return AVERROR(ENOMEM);
4234 }
4235
4236 4 tdrdi->prec_ref_display_width = sei->tdrdi.prec_ref_display_width;
4237 4 tdrdi->ref_viewing_distance_flag = sei->tdrdi.ref_viewing_distance_flag;
4238 4 tdrdi->prec_ref_viewing_dist = sei->tdrdi.prec_ref_viewing_dist;
4239 4 tdrdi->num_ref_displays = sei->tdrdi.num_ref_displays;
4240
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 4 times.
8 for (int i = 0; i < sei->tdrdi.num_ref_displays; i++) {
4241 4 AV3DReferenceDisplay *display = av_tdrdi_get_display(tdrdi, i);
4242
4243 4 display->left_view_id = sei->tdrdi.left_view_id[i];
4244 4 display->right_view_id = sei->tdrdi.right_view_id[i];
4245 4 display->exponent_ref_display_width = sei->tdrdi.exponent_ref_display_width[i];
4246 4 display->mantissa_ref_display_width = sei->tdrdi.mantissa_ref_display_width[i];
4247 4 display->exponent_ref_viewing_distance = sei->tdrdi.exponent_ref_viewing_distance[i];
4248 4 display->mantissa_ref_viewing_distance = sei->tdrdi.mantissa_ref_viewing_distance[i];
4249 4 display->additional_shift_present_flag = sei->tdrdi.additional_shift_present_flag[i];
4250 4 display->num_sample_shift = sei->tdrdi.num_sample_shift[i];
4251 }
4252 4 ret = ff_frame_new_side_data_from_buf_ext(avctx, &avctx->decoded_side_data, &avctx->nb_decoded_side_data,
4253 AV_FRAME_DATA_3D_REFERENCE_DISPLAYS, &buf);
4254
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (ret < 0) {
4255 av_buffer_unref(&buf);
4256 return ret;
4257 }
4258 }
4259
4260 360 ret = ff_h2645_sei_to_context(avctx, &sei->common);
4261
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 360 times.
360 if (ret < 0)
4262 return ret;
4263
4264 360 return 0;
4265 }
4266
4267 587 static av_cold int hevc_decode_init(AVCodecContext *avctx)
4268 {
4269 587 HEVCContext *s = avctx->priv_data;
4270 int ret;
4271
4272 587 ret = hevc_init_context(avctx);
4273
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 587 times.
587 if (ret < 0)
4274 return ret;
4275
4276 587 s->sei.picture_timing.picture_struct = 0;
4277 587 s->eos = 1;
4278
4279 587 atomic_init(&s->wpp_err, 0);
4280
4281
2/2
✓ Branch 0 taken 586 times.
✓ Branch 1 taken 1 times.
587 if (!avctx->internal->is_copy) {
4282 const AVPacketSideData *sd;
4283
4284
3/4
✓ Branch 0 taken 360 times.
✓ Branch 1 taken 226 times.
✓ Branch 2 taken 360 times.
✗ Branch 3 not taken.
586 if (avctx->extradata_size > 0 && avctx->extradata) {
4285 360 ret = hevc_decode_extradata(s, avctx->extradata, avctx->extradata_size, 1);
4286
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 360 times.
360 if (ret < 0) {
4287 return ret;
4288 }
4289
4290 360 ret = hevc_sei_to_context(avctx, &s->sei);
4291
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 360 times.
360 if (ret < 0)
4292 return ret;
4293 }
4294
4295 586 sd = ff_get_coded_side_data(avctx, AV_PKT_DATA_DOVI_CONF);
4296
3/4
✓ Branch 0 taken 35 times.
✓ Branch 1 taken 551 times.
✓ Branch 2 taken 35 times.
✗ Branch 3 not taken.
586 if (sd && sd->size >= sizeof(s->dovi_ctx.cfg))
4297 35 s->dovi_ctx.cfg = *(AVDOVIDecoderConfigurationRecord *) sd->data;
4298 }
4299
4300 587 return 0;
4301 }
4302
4303 static av_cold void hevc_decode_flush(AVCodecContext *avctx)
4304 {
4305 HEVCContext *s = avctx->priv_data;
4306 ff_hevc_flush_dpb(s);
4307 ff_hevc_reset_sei(&s->sei);
4308 ff_dovi_ctx_flush(&s->dovi_ctx);
4309 av_buffer_unref(&s->rpu_buf);
4310 s->eos = 1;
4311 s->skipping_frame = 0;
4312
4313 if (FF_HW_HAS_CB(avctx, flush))
4314 FF_HW_SIMPLE_CALL(avctx, flush);
4315 }
4316
4317 #define OFFSET(x) offsetof(HEVCContext, x)
4318 #define PAR (AV_OPT_FLAG_DECODING_PARAM | AV_OPT_FLAG_VIDEO_PARAM)
4319
4320 static const AVOption options[] = {
4321 { "apply_defdispwin", "Apply default display window from VUI", OFFSET(apply_defdispwin),
4322 AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, PAR },
4323 { "strict-displaywin", "strictly apply default display window size", OFFSET(apply_defdispwin),
4324 AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, PAR },
4325 { "view_ids", "Array of view IDs that should be decoded and output; a single -1 to decode all views",
4326 .offset = OFFSET(view_ids), .type = AV_OPT_TYPE_INT | AV_OPT_TYPE_FLAG_ARRAY,
4327 .min = -1, .max = INT_MAX, .flags = PAR },
4328 { "view_ids_available", "Array of available view IDs is exported here",
4329 .offset = OFFSET(view_ids_available), .type = AV_OPT_TYPE_UINT | AV_OPT_TYPE_FLAG_ARRAY,
4330 .flags = PAR | AV_OPT_FLAG_EXPORT | AV_OPT_FLAG_READONLY },
4331 { "view_pos_available", "Array of view positions for view_ids_available is exported here, as AVStereo3DView",
4332 .offset = OFFSET(view_pos_available), .type = AV_OPT_TYPE_UINT | AV_OPT_TYPE_FLAG_ARRAY,
4333 .flags = PAR | AV_OPT_FLAG_EXPORT | AV_OPT_FLAG_READONLY, .unit = "view_pos" },
4334 { "unspecified", .type = AV_OPT_TYPE_CONST, .default_val = { .i64 = AV_STEREO3D_VIEW_UNSPEC }, .unit = "view_pos" },
4335 { "left", .type = AV_OPT_TYPE_CONST, .default_val = { .i64 = AV_STEREO3D_VIEW_LEFT }, .unit = "view_pos" },
4336 { "right", .type = AV_OPT_TYPE_CONST, .default_val = { .i64 = AV_STEREO3D_VIEW_RIGHT }, .unit = "view_pos" },
4337
4338 { NULL },
4339 };
4340
4341 static const AVClass hevc_decoder_class = {
4342 .class_name = "HEVC decoder",
4343 .item_name = av_default_item_name,
4344 .option = options,
4345 .version = LIBAVUTIL_VERSION_INT,
4346 };
4347
4348 const FFCodec ff_hevc_decoder = {
4349 .p.name = "hevc",
4350 CODEC_LONG_NAME("HEVC (High Efficiency Video Coding)"),
4351 .p.type = AVMEDIA_TYPE_VIDEO,
4352 .p.id = AV_CODEC_ID_HEVC,
4353 .priv_data_size = sizeof(HEVCContext),
4354 .p.priv_class = &hevc_decoder_class,
4355 .init = hevc_decode_init,
4356 .close = hevc_decode_free,
4357 FF_CODEC_RECEIVE_FRAME_CB(hevc_receive_frame),
4358 .flush = hevc_decode_flush,
4359 UPDATE_THREAD_CONTEXT(hevc_update_thread_context),
4360 .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DELAY |
4361 AV_CODEC_CAP_SLICE_THREADS | AV_CODEC_CAP_FRAME_THREADS,
4362 .caps_internal = FF_CODEC_CAP_EXPORTS_CROPPING |
4363 FF_CODEC_CAP_SKIP_FRAME_FILL_PARAM |
4364 FF_CODEC_CAP_USES_PROGRESSFRAMES |
4365 FF_CODEC_CAP_INIT_CLEANUP,
4366 .p.profiles = NULL_IF_CONFIG_SMALL(ff_hevc_profiles),
4367 .hw_configs = (const AVCodecHWConfigInternal *const []) {
4368 #if CONFIG_HEVC_DXVA2_HWACCEL
4369 HWACCEL_DXVA2(hevc),
4370 #endif
4371 #if CONFIG_HEVC_D3D11VA_HWACCEL
4372 HWACCEL_D3D11VA(hevc),
4373 #endif
4374 #if CONFIG_HEVC_D3D11VA2_HWACCEL
4375 HWACCEL_D3D11VA2(hevc),
4376 #endif
4377 #if CONFIG_HEVC_D3D12VA_HWACCEL
4378 HWACCEL_D3D12VA(hevc),
4379 #endif
4380 #if CONFIG_HEVC_NVDEC_HWACCEL
4381 HWACCEL_NVDEC(hevc),
4382 #endif
4383 #if CONFIG_HEVC_NVDEC_CUARRAY_HWACCEL
4384 HWACCEL_NVDEC_CUARRAY(hevc),
4385 #endif
4386 #if CONFIG_HEVC_VAAPI_HWACCEL
4387 HWACCEL_VAAPI(hevc),
4388 #endif
4389 #if CONFIG_HEVC_VDPAU_HWACCEL
4390 HWACCEL_VDPAU(hevc),
4391 #endif
4392 #if CONFIG_HEVC_VIDEOTOOLBOX_HWACCEL
4393 HWACCEL_VIDEOTOOLBOX(hevc),
4394 #endif
4395 #if CONFIG_HEVC_VULKAN_HWACCEL
4396 HWACCEL_VULKAN(hevc),
4397 #endif
4398 NULL
4399 },
4400 };
4401