FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/hevc/hevcdec.c
Date: 2026-09-12 04:54:02
Exec Total Coverage
Lines: 1989 2473 80.4%
Functions: 51 56 91.1%
Branches: 1404 1853 75.8%

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 1620 static void pic_arrays_free(HEVCLayerContext *l)
75 {
76 1620 av_freep(&l->sao);
77 1620 av_freep(&l->deblock);
78
79 1620 av_freep(&l->skip_flag);
80 1620 av_freep(&l->tab_ct_depth);
81
82 1620 av_freep(&l->tab_ipm);
83 1620 av_freep(&l->cbf_luma);
84 1620 av_freep(&l->is_pcm);
85
86 1620 av_freep(&l->qp_y_tab);
87 1620 av_freep(&l->tab_slice_address);
88 1620 av_freep(&l->filter_slice_edges);
89
90 1620 av_freep(&l->horizontal_bs);
91 1620 av_freep(&l->vertical_bs);
92
93
2/2
✓ Branch 0 taken 4860 times.
✓ Branch 1 taken 1620 times.
6480 for (int i = 0; i < 3; i++) {
94 4860 av_freep(&l->sao_pixel_buffer_h[i]);
95 4860 av_freep(&l->sao_pixel_buffer_v[i]);
96 }
97
98 1620 av_refstruct_pool_uninit(&l->tab_mvf_pool);
99 1620 av_refstruct_pool_uninit(&l->rpl_tab_pool);
100 1620 }
101
102 /* allocate arrays that depend on frame dimensions */
103 466 static int pic_arrays_init(HEVCLayerContext *l, const HEVCSPS *sps)
104 {
105 466 int log2_min_cb_size = sps->log2_min_cb_size;
106 466 int width = sps->width;
107 466 int height = sps->height;
108 466 int pic_size_in_ctb = ((width >> log2_min_cb_size) + 1) *
109 466 ((height >> log2_min_cb_size) + 1);
110 466 int ctb_count = sps->ctb_width * sps->ctb_height;
111 466 int min_pu_size = sps->min_pu_width * sps->min_pu_height;
112
113 466 l->bs_width = (width >> 2) + 1;
114 466 l->bs_height = (height >> 2) + 1;
115
116 466 l->sao = av_calloc(ctb_count, sizeof(*l->sao));
117 466 l->deblock = av_calloc(ctb_count, sizeof(*l->deblock));
118
2/4
✓ Branch 0 taken 466 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 466 times.
466 if (!l->sao || !l->deblock)
119 goto fail;
120
121 466 l->skip_flag = av_malloc_array(sps->min_cb_height, sps->min_cb_width);
122 466 l->tab_ct_depth = av_malloc_array(sps->min_cb_height, sps->min_cb_width);
123
2/4
✓ Branch 0 taken 466 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 466 times.
466 if (!l->skip_flag || !l->tab_ct_depth)
124 goto fail;
125
126 466 l->cbf_luma = av_malloc_array(sps->min_tb_width, sps->min_tb_height);
127 466 l->tab_ipm = av_mallocz(min_pu_size);
128 466 l->is_pcm = av_malloc_array(sps->min_pu_width + 1, sps->min_pu_height + 1);
129
3/6
✓ Branch 0 taken 466 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 466 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 466 times.
466 if (!l->tab_ipm || !l->cbf_luma || !l->is_pcm)
130 goto fail;
131
132 466 l->filter_slice_edges = av_mallocz(ctb_count);
133 466 l->tab_slice_address = av_malloc_array(pic_size_in_ctb,
134 sizeof(*l->tab_slice_address));
135 466 l->qp_y_tab = av_calloc(pic_size_in_ctb,
136 sizeof(*l->qp_y_tab));
137
3/6
✓ Branch 0 taken 466 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 466 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 466 times.
466 if (!l->qp_y_tab || !l->filter_slice_edges || !l->tab_slice_address)
138 goto fail;
139
140 466 l->horizontal_bs = av_calloc(l->bs_width, l->bs_height);
141 466 l->vertical_bs = av_calloc(l->bs_width, l->bs_height);
142
2/4
✓ Branch 0 taken 466 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 466 times.
466 if (!l->horizontal_bs || !l->vertical_bs)
143 goto fail;
144
145 466 l->tab_mvf_pool = av_refstruct_pool_alloc(min_pu_size * sizeof(MvField), 0);
146 466 l->rpl_tab_pool = av_refstruct_pool_alloc(ctb_count * sizeof(RefPicListTab), 0);
147
2/4
✓ Branch 0 taken 466 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 466 times.
466 if (!l->tab_mvf_pool || !l->rpl_tab_pool)
148 goto fail;
149
150
2/2
✓ Branch 0 taken 404 times.
✓ Branch 1 taken 62 times.
466 if (sps->sao_enabled) {
151
2/2
✓ Branch 0 taken 402 times.
✓ Branch 1 taken 2 times.
404 int c_count = (sps->chroma_format_idc != 0) ? 3 : 1;
152
153
2/2
✓ Branch 0 taken 1208 times.
✓ Branch 1 taken 404 times.
1612 for (int c_idx = 0; c_idx < c_count; c_idx++) {
154 1208 int w = sps->width >> sps->hshift[c_idx];
155 1208 int h = sps->height >> sps->vshift[c_idx];
156 1208 l->sao_pixel_buffer_h[c_idx] =
157 1208 av_mallocz((w * 2 * sps->ctb_height) <<
158 1208 sps->pixel_shift);
159 1208 l->sao_pixel_buffer_v[c_idx] =
160 1208 av_mallocz((h * 2 * sps->ctb_width) <<
161 1208 sps->pixel_shift);
162
1/2
✓ Branch 0 taken 1208 times.
✗ Branch 1 not taken.
1208 if (!l->sao_pixel_buffer_h[c_idx] ||
163
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1208 times.
1208 !l->sao_pixel_buffer_v[c_idx])
164 goto fail;
165 }
166 }
167
168 466 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 20253 static int decode_lt_rps(const HEVCSPS *sps, LongTermRPS *rps,
276 GetBitContext *gb, int cur_poc, int poc_lsb)
277 {
278 20253 int max_poc_lsb = 1 << sps->log2_max_poc_lsb;
279 20253 int prev_delta_msb = 0;
280 20253 unsigned int nb_sps = 0, nb_sh;
281 int i;
282
283 20253 rps->nb_refs = 0;
284
2/2
✓ Branch 0 taken 18851 times.
✓ Branch 1 taken 1402 times.
20253 if (!sps->long_term_ref_pics_present)
285 18851 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 808 static void export_stream_params(HEVCContext *s, const HEVCSPS *sps)
333 {
334 808 AVCodecContext *avctx = s->avctx;
335 808 const HEVCVPS *vps = sps->vps;
336 808 const HEVCWindow *ow = &sps->output_window;
337 808 unsigned int num = 0, den = 0;
338
339 808 avctx->pix_fmt = sps->pix_fmt;
340 808 avctx->coded_width = sps->width;
341 808 avctx->coded_height = sps->height;
342 808 avctx->width = sps->width - ow->left_offset - ow->right_offset;
343 808 avctx->height = sps->height - ow->top_offset - ow->bottom_offset;
344 808 avctx->has_b_frames = sps->temporal_layer[sps->max_sub_layers - 1].num_reorder_pics;
345 808 avctx->profile = sps->ptl.general_ptl.profile_idc;
346 808 avctx->level = sps->ptl.general_ptl.level_idc;
347
348 808 ff_set_sar(avctx, sps->vui.common.sar);
349
350
2/2
✓ Branch 0 taken 128 times.
✓ Branch 1 taken 680 times.
808 if (sps->vui.common.video_signal_type_present_flag)
351 128 avctx->color_range = sps->vui.common.video_full_range_flag ? AVCOL_RANGE_JPEG
352
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 118 times.
128 : AVCOL_RANGE_MPEG;
353 else
354 680 avctx->color_range = AVCOL_RANGE_MPEG;
355
356
2/2
✓ Branch 0 taken 121 times.
✓ Branch 1 taken 687 times.
808 if (sps->vui.common.colour_description_present_flag) {
357 121 avctx->color_primaries = sps->vui.common.colour_primaries;
358 121 avctx->color_trc = sps->vui.common.transfer_characteristics;
359 121 avctx->colorspace = sps->vui.common.matrix_coeffs;
360 } else {
361 687 avctx->color_primaries = AVCOL_PRI_UNSPECIFIED;
362 687 avctx->color_trc = AVCOL_TRC_UNSPECIFIED;
363 687 avctx->colorspace = AVCOL_SPC_UNSPECIFIED;
364 }
365
366 808 avctx->chroma_sample_location = AVCHROMA_LOC_UNSPECIFIED;
367
2/2
✓ Branch 0 taken 767 times.
✓ Branch 1 taken 41 times.
808 if (sps->chroma_format_idc == 1) {
368
2/2
✓ Branch 0 taken 43 times.
✓ Branch 1 taken 724 times.
767 if (sps->vui.common.chroma_loc_info_present_flag) {
369
1/2
✓ Branch 0 taken 43 times.
✗ Branch 1 not taken.
43 if (sps->vui.common.chroma_sample_loc_type_top_field <= 5)
370 43 avctx->chroma_sample_location = sps->vui.common.chroma_sample_loc_type_top_field + 1;
371 } else
372 724 avctx->chroma_sample_location = AVCHROMA_LOC_LEFT;
373 }
374
375
2/2
✓ Branch 0 taken 46 times.
✓ Branch 1 taken 762 times.
808 if (vps->vps_timing_info_present_flag) {
376 46 num = vps->vps_num_units_in_tick;
377 46 den = vps->vps_time_scale;
378
2/2
✓ Branch 0 taken 105 times.
✓ Branch 1 taken 657 times.
762 } else if (sps->vui.vui_timing_info_present_flag) {
379 105 num = sps->vui.vui_num_units_in_tick;
380 105 den = sps->vui.vui_time_scale;
381 }
382
383
3/4
✓ Branch 0 taken 151 times.
✓ Branch 1 taken 657 times.
✓ Branch 2 taken 151 times.
✗ Branch 3 not taken.
808 if (num > 0 && den > 0)
384 151 av_reduce(&avctx->framerate.den, &avctx->framerate.num,
385 num, den, 1 << 30);
386 808 }
387
388 10927 static int export_stream_params_from_sei(HEVCContext *s)
389 {
390 10927 AVCodecContext *avctx = s->avctx;
391
392
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 10927 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
10927 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 10927 return 0;
399 }
400
401 808 static int export_multilayer(HEVCContext *s, const HEVCVPS *vps)
402 {
403 808 const HEVCSEITDRDI *tdrdi = &s->sei.tdrdi;
404
405 808 av_freep(&s->view_ids_available);
406 808 s->nb_view_ids_available = 0;
407 808 av_freep(&s->view_pos_available);
408 808 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 780 times.
✓ Branch 1 taken 28 times.
✓ Branch 2 taken 780 times.
✗ Branch 3 not taken.
808 if (vps->nb_layers < 2 && !vps->view_id[0])
412 780 return 0;
413
414 28 s->view_ids_available = av_calloc(vps->nb_layers, sizeof(*s->view_ids_available));
415
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 28 times.
28 if (!s->view_ids_available)
416 return AVERROR(ENOMEM);
417
418
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 23 times.
28 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 56 times.
✓ Branch 1 taken 28 times.
84 for (int i = 0; i < vps->nb_layers; i++) {
425 56 s->view_ids_available[i] = vps->view_id[i];
426
427
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 46 times.
56 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 28 s->nb_view_ids_available = vps->nb_layers;
435
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 23 times.
28 s->nb_view_pos_available = s->view_pos_available ? vps->nb_layers : 0;
436
437 28 return 0;
438 }
439
440 11948 int ff_hevc_is_alpha_video(const HEVCContext *s)
441 {
442 11948 const HEVCVPS *vps = s->vps;
443 11948 int ret = 0;
444
445
3/4
✓ Branch 0 taken 1215 times.
✓ Branch 1 taken 10733 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1215 times.
11948 if (vps->nb_layers != 2 || !vps->layer_id_in_nuh[1])
446 10733 return 0;
447
448 /* decode_vps_ext() guarantees that SCALABILITY_AUXILIARY with AuxId other
449 * than alpha cannot reach here.
450 */
451 1215 ret = (s->vps->scalability_mask_flag & HEVC_SCALABILITY_AUXILIARY);
452
453
2/2
✓ Branch 0 taken 153 times.
✓ Branch 1 taken 1062 times.
1215 av_log(s->avctx, AV_LOG_DEBUG, "Multi layer video, %s alpha video\n",
454 ret ? "is" : "not");
455
456 1215 return ret;
457 }
458
459 455 static int setup_multilayer(HEVCContext *s, const HEVCVPS *vps)
460 {
461 455 unsigned layers_active_output = 0, highest_layer;
462
463 455 s->layers_active_output = 1;
464 455 s->layers_active_decode = 1;
465
466
2/2
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 454 times.
455 if (ff_hevc_is_alpha_video(s)) {
467 1 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(s->avctx->pix_fmt);
468
469
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!(desc->flags & AV_PIX_FMT_FLAG_ALPHA))
470 return 0;
471
472 1 s->layers_active_decode = (1 << vps->nb_layers) - 1;
473 1 s->layers_active_output = 1;
474
475 1 return 0;
476 }
477
478 // nothing requested - decode base layer only
479
2/2
✓ Branch 0 taken 443 times.
✓ Branch 1 taken 11 times.
454 if (!s->nb_view_ids)
480 443 return 0;
481
482
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) {
483 layers_active_output = (1 << vps->nb_layers) - 1;
484 } else {
485
2/2
✓ Branch 0 taken 19 times.
✓ Branch 1 taken 11 times.
30 for (int i = 0; i < s->nb_view_ids; i++) {
486 19 int view_id = s->view_ids[i];
487 19 int layer_idx = -1;
488
489
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 19 times.
19 if (view_id < 0) {
490 av_log(s->avctx, AV_LOG_ERROR,
491 "Invalid view ID requested: %d\n", view_id);
492 return AVERROR(EINVAL);
493 }
494
495
1/2
✓ Branch 0 taken 28 times.
✗ Branch 1 not taken.
28 for (int j = 0; j < vps->nb_layers; j++) {
496
2/2
✓ Branch 0 taken 19 times.
✓ Branch 1 taken 9 times.
28 if (vps->view_id[j] == view_id) {
497 19 layer_idx = j;
498 19 break;
499 }
500 }
501
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 19 times.
19 if (layer_idx < 0) {
502 av_log(s->avctx, AV_LOG_ERROR,
503 "View ID %d not present in VPS\n", view_id);
504 return AVERROR(EINVAL);
505 }
506 19 layers_active_output |= 1 << layer_idx;
507 }
508 }
509
510
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11 times.
11 if (!layers_active_output) {
511 av_log(s->avctx, AV_LOG_ERROR, "No layers selected\n");
512 return AVERROR_BUG;
513 }
514
515 11 highest_layer = ff_log2(layers_active_output);
516
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11 times.
11 if (highest_layer >= FF_ARRAY_ELEMS(s->layers)) {
517 av_log(s->avctx, AV_LOG_ERROR,
518 "Too many layers requested: %u\n", layers_active_output);
519 return AVERROR(EINVAL);
520 }
521
522 /* Assume a higher layer depends on all the lower ones.
523 * This is enforced in VPS parsing currently, this logic will need
524 * to be changed if we want to support more complex dependency structures.
525 */
526 11 s->layers_active_decode = (1 << (highest_layer + 1)) - 1;
527 11 s->layers_active_output = layers_active_output;
528
529 11 av_log(s->avctx, AV_LOG_DEBUG, "decode/output layers: %x/%x\n",
530 s->layers_active_decode, s->layers_active_output);
531
532 11 return 0;
533 }
534
535 1 static enum AVPixelFormat map_to_alpha_format(HEVCContext *s,
536 enum AVPixelFormat pix_fmt)
537 {
538
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) {
539 1 case AV_PIX_FMT_YUV420P:
540 case AV_PIX_FMT_YUVJ420P:
541 1 return AV_PIX_FMT_YUVA420P;
542 case AV_PIX_FMT_YUV420P10:
543 return AV_PIX_FMT_YUVA420P10;
544 case AV_PIX_FMT_YUV444P:
545 return AV_PIX_FMT_YUVA444P;
546 case AV_PIX_FMT_YUV422P:
547 return AV_PIX_FMT_YUVA422P;
548 case AV_PIX_FMT_YUV422P10LE:
549 return AV_PIX_FMT_YUVA422P10LE;
550 case AV_PIX_FMT_YUV444P10:
551 return AV_PIX_FMT_YUVA444P10;
552 case AV_PIX_FMT_YUV444P12:
553 return AV_PIX_FMT_YUVA444P12;
554 case AV_PIX_FMT_YUV422P12:
555 return AV_PIX_FMT_YUVA422P12;
556 default:
557 av_log(s->avctx, AV_LOG_WARNING, "No alpha pixel format map for %s\n",
558 av_get_pix_fmt_name(pix_fmt));
559 return AV_PIX_FMT_NONE;
560 }
561 }
562
563 455 static enum AVPixelFormat get_format(HEVCContext *s, const HEVCSPS *sps)
564 {
565 #define HWACCEL_MAX (CONFIG_HEVC_DXVA2_HWACCEL + \
566 CONFIG_HEVC_D3D11VA_HWACCEL * 2 + \
567 CONFIG_HEVC_D3D12VA_HWACCEL + \
568 CONFIG_HEVC_NVDEC_HWACCEL + \
569 CONFIG_HEVC_NVDEC_CUARRAY_HWACCEL + \
570 CONFIG_HEVC_VAAPI_HWACCEL + \
571 CONFIG_HEVC_VIDEOTOOLBOX_HWACCEL + \
572 CONFIG_HEVC_VDPAU_HWACCEL + \
573 CONFIG_HEVC_VULKAN_HWACCEL)
574 455 enum AVPixelFormat pix_fmts[HWACCEL_MAX + 3], *fmt = pix_fmts;
575 455 enum AVPixelFormat alpha_fmt = AV_PIX_FMT_NONE;
576 int ret;
577
578
2/2
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 454 times.
455 if (ff_hevc_is_alpha_video(s))
579 1 alpha_fmt = map_to_alpha_format(s, sps->pix_fmt);
580
581
6/7
✓ Branch 0 taken 379 times.
✓ Branch 1 taken 48 times.
✓ Branch 2 taken 4 times.
✓ Branch 3 taken 12 times.
✓ Branch 4 taken 10 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 2 times.
455 switch (sps->pix_fmt) {
582 379 case AV_PIX_FMT_YUV420P:
583 case AV_PIX_FMT_YUVJ420P:
584 #if CONFIG_HEVC_DXVA2_HWACCEL
585 *fmt++ = AV_PIX_FMT_DXVA2_VLD;
586 #endif
587 #if CONFIG_HEVC_D3D11VA_HWACCEL
588 *fmt++ = AV_PIX_FMT_D3D11VA_VLD;
589 *fmt++ = AV_PIX_FMT_D3D11;
590 #endif
591 #if CONFIG_HEVC_D3D12VA_HWACCEL
592 *fmt++ = AV_PIX_FMT_D3D12;
593 #endif
594 #if CONFIG_HEVC_VAAPI_HWACCEL
595 379 *fmt++ = AV_PIX_FMT_VAAPI;
596 #endif
597 #if CONFIG_HEVC_VDPAU_HWACCEL
598 379 *fmt++ = AV_PIX_FMT_VDPAU;
599 #endif
600 #if CONFIG_HEVC_NVDEC_HWACCEL
601 *fmt++ = AV_PIX_FMT_CUDA;
602 #endif
603 #if CONFIG_HEVC_NVDEC_CUARRAY_HWACCEL
604 *fmt++ = AV_PIX_FMT_CUARRAY;
605 #endif
606 #if CONFIG_HEVC_VIDEOTOOLBOX_HWACCEL
607 *fmt++ = AV_PIX_FMT_VIDEOTOOLBOX;
608 #endif
609 #if CONFIG_HEVC_VULKAN_HWACCEL
610 *fmt++ = AV_PIX_FMT_VULKAN;
611 #endif
612 379 break;
613 48 case AV_PIX_FMT_YUV420P10:
614 #if CONFIG_HEVC_DXVA2_HWACCEL
615 *fmt++ = AV_PIX_FMT_DXVA2_VLD;
616 #endif
617 #if CONFIG_HEVC_D3D11VA_HWACCEL
618 *fmt++ = AV_PIX_FMT_D3D11VA_VLD;
619 *fmt++ = AV_PIX_FMT_D3D11;
620 #endif
621 #if CONFIG_HEVC_D3D12VA_HWACCEL
622 *fmt++ = AV_PIX_FMT_D3D12;
623 #endif
624 #if CONFIG_HEVC_VAAPI_HWACCEL
625 48 *fmt++ = AV_PIX_FMT_VAAPI;
626 #endif
627 #if CONFIG_HEVC_VIDEOTOOLBOX_HWACCEL
628 *fmt++ = AV_PIX_FMT_VIDEOTOOLBOX;
629 #endif
630 #if CONFIG_HEVC_VULKAN_HWACCEL
631 *fmt++ = AV_PIX_FMT_VULKAN;
632 #endif
633 #if CONFIG_HEVC_VDPAU_HWACCEL
634 48 *fmt++ = AV_PIX_FMT_VDPAU;
635 #endif
636 #if CONFIG_HEVC_NVDEC_HWACCEL
637 *fmt++ = AV_PIX_FMT_CUDA;
638 #endif
639 #if CONFIG_HEVC_NVDEC_CUARRAY_HWACCEL
640 *fmt++ = AV_PIX_FMT_CUARRAY;
641 #endif
642 48 break;
643 4 case AV_PIX_FMT_YUV444P:
644 #if CONFIG_HEVC_VAAPI_HWACCEL
645 4 *fmt++ = AV_PIX_FMT_VAAPI;
646 #endif
647 #if CONFIG_HEVC_VDPAU_HWACCEL
648 4 *fmt++ = AV_PIX_FMT_VDPAU;
649 #endif
650 #if CONFIG_HEVC_NVDEC_HWACCEL
651 *fmt++ = AV_PIX_FMT_CUDA;
652 #endif
653 #if CONFIG_HEVC_NVDEC_CUARRAY_HWACCEL
654 *fmt++ = AV_PIX_FMT_CUARRAY;
655 #endif
656 #if CONFIG_HEVC_VIDEOTOOLBOX_HWACCEL
657 *fmt++ = AV_PIX_FMT_VIDEOTOOLBOX;
658 #endif
659 #if CONFIG_HEVC_VULKAN_HWACCEL
660 *fmt++ = AV_PIX_FMT_VULKAN;
661 #endif
662 4 break;
663 12 case AV_PIX_FMT_YUV422P:
664 case AV_PIX_FMT_YUV422P10LE:
665 #if CONFIG_HEVC_VAAPI_HWACCEL
666 12 *fmt++ = AV_PIX_FMT_VAAPI;
667 #endif
668 #if CONFIG_HEVC_VIDEOTOOLBOX_HWACCEL
669 *fmt++ = AV_PIX_FMT_VIDEOTOOLBOX;
670 #endif
671 #if CONFIG_HEVC_VULKAN_HWACCEL
672 *fmt++ = AV_PIX_FMT_VULKAN;
673 #endif
674 #if CONFIG_HEVC_NVDEC_HWACCEL
675 *fmt++ = AV_PIX_FMT_CUDA;
676 #endif
677 #if CONFIG_HEVC_NVDEC_CUARRAY_HWACCEL
678 *fmt++ = AV_PIX_FMT_CUARRAY;
679 #endif
680 12 break;
681 10 case AV_PIX_FMT_YUV444P10:
682 #if CONFIG_HEVC_VIDEOTOOLBOX_HWACCEL
683 *fmt++ = AV_PIX_FMT_VIDEOTOOLBOX;
684 #endif
685 av_fallthrough;
686 case AV_PIX_FMT_YUV420P12:
687 case AV_PIX_FMT_YUV444P12:
688 #if CONFIG_HEVC_VAAPI_HWACCEL
689 10 *fmt++ = AV_PIX_FMT_VAAPI;
690 #endif
691 #if CONFIG_HEVC_VDPAU_HWACCEL
692 10 *fmt++ = AV_PIX_FMT_VDPAU;
693 #endif
694 #if CONFIG_HEVC_VULKAN_HWACCEL
695 *fmt++ = AV_PIX_FMT_VULKAN;
696 #endif
697 #if CONFIG_HEVC_NVDEC_HWACCEL
698 *fmt++ = AV_PIX_FMT_CUDA;
699 #endif
700 #if CONFIG_HEVC_NVDEC_CUARRAY_HWACCEL
701 *fmt++ = AV_PIX_FMT_CUARRAY;
702 #endif
703 10 break;
704 case AV_PIX_FMT_YUV422P12:
705 #if CONFIG_HEVC_VAAPI_HWACCEL
706 *fmt++ = AV_PIX_FMT_VAAPI;
707 #endif
708 #if CONFIG_HEVC_VULKAN_HWACCEL
709 *fmt++ = AV_PIX_FMT_VULKAN;
710 #endif
711 #if CONFIG_HEVC_NVDEC_HWACCEL
712 *fmt++ = AV_PIX_FMT_CUDA;
713 #endif
714 #if CONFIG_HEVC_NVDEC_CUARRAY_HWACCEL
715 *fmt++ = AV_PIX_FMT_CUARRAY;
716 #endif
717 break;
718 }
719
720
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 454 times.
455 if (alpha_fmt != AV_PIX_FMT_NONE)
721 1 *fmt++ = alpha_fmt;
722 455 *fmt++ = sps->pix_fmt;
723 455 *fmt = AV_PIX_FMT_NONE;
724
725 // export multilayer information from active VPS to the caller,
726 // so it is available in get_format()
727 455 ret = export_multilayer(s, sps->vps);
728
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 455 times.
455 if (ret < 0)
729 return ret;
730
731 455 ret = ff_get_format(s->avctx, pix_fmts);
732
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 455 times.
455 if (ret < 0)
733 return ret;
734 455 s->avctx->pix_fmt = ret;
735
736 // set up multilayer decoding, if requested by caller
737 455 ret = setup_multilayer(s, sps->vps);
738
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 455 times.
455 if (ret < 0)
739 return ret;
740
741 455 return 0;
742 }
743
744 466 static int set_sps(HEVCContext *s, HEVCLayerContext *l, const HEVCSPS *sps)
745 {
746 int ret;
747
748 466 pic_arrays_free(l);
749 466 av_refstruct_unref(&l->sps);
750 466 av_refstruct_unref(&s->vps);
751
752
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 466 times.
466 if (!sps)
753 return 0;
754
755 466 ret = pic_arrays_init(l, sps);
756
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 466 times.
466 if (ret < 0)
757 goto fail;
758
759 466 ff_hevc_pred_init(&s->hpc, sps->bit_depth);
760 466 ff_hevc_dsp_init (&s->hevcdsp, sps->bit_depth);
761 466 ff_videodsp_init (&s->vdsp, sps->bit_depth);
762
763 466 l->sps = av_refstruct_ref_c(sps);
764 466 s->vps = av_refstruct_ref_c(sps->vps);
765
766 466 return 0;
767
768 fail:
769 pic_arrays_free(l);
770 av_refstruct_unref(&l->sps);
771 return ret;
772 }
773
774 29013 static int hls_slice_header(SliceHeader *sh, const HEVCContext *s, GetBitContext *gb)
775 {
776 const HEVCPPS *pps;
777 const HEVCSPS *sps;
778 const HEVCVPS *vps;
779 unsigned pps_id, layer_idx;
780 int i, ret;
781
782 // Coded parameters
783 29013 sh->first_slice_in_pic_flag = get_bits1(gb);
784
785 29013 sh->no_output_of_prior_pics_flag = 0;
786
3/4
✓ Branch 0 taken 1601 times.
✓ Branch 1 taken 27412 times.
✓ Branch 2 taken 1601 times.
✗ Branch 3 not taken.
29013 if (IS_IRAP(s))
787 1601 sh->no_output_of_prior_pics_flag = get_bits1(gb);
788
789 29013 pps_id = get_ue_golomb_long(gb);
790
3/4
✓ Branch 0 taken 29013 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 33 times.
✓ Branch 3 taken 28980 times.
29013 if (pps_id >= HEVC_MAX_PPS_COUNT || !s->ps.pps_list[pps_id]) {
791 33 av_log(s->avctx, AV_LOG_ERROR, "PPS id out of range: %d\n", pps_id);
792 33 return AVERROR_INVALIDDATA;
793 }
794
3/4
✓ Branch 0 taken 18337 times.
✓ Branch 1 taken 10643 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 18337 times.
28980 if (!sh->first_slice_in_pic_flag && s->ps.pps_list[pps_id] != s->pps) {
795 av_log(s->avctx, AV_LOG_ERROR, "PPS changed between slices.\n");
796 return AVERROR_INVALIDDATA;
797 }
798 28980 sh->pps_id = pps_id;
799
800 28980 pps = s->ps.pps_list[pps_id];
801 28980 sps = pps->sps;
802 28980 vps = sps->vps;
803 28980 layer_idx = vps->layer_idx[s->nuh_layer_id];
804
805
4/4
✓ Branch 0 taken 497 times.
✓ Branch 1 taken 28483 times.
✓ Branch 2 taken 16 times.
✓ Branch 3 taken 481 times.
28980 if (s->nal_unit_type == HEVC_NAL_CRA_NUT && s->last_eos == 1)
806 16 sh->no_output_of_prior_pics_flag = 1;
807
808 28980 sh->dependent_slice_segment_flag = 0;
809
2/2
✓ Branch 0 taken 18337 times.
✓ Branch 1 taken 10643 times.
28980 if (!sh->first_slice_in_pic_flag) {
810 int slice_address_length;
811
812
2/2
✓ Branch 0 taken 9140 times.
✓ Branch 1 taken 9197 times.
18337 if (pps->dependent_slice_segments_enabled_flag)
813 9140 sh->dependent_slice_segment_flag = get_bits1(gb);
814
3/4
✓ Branch 0 taken 7989 times.
✓ Branch 1 taken 10348 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 7989 times.
18337 if (sh->dependent_slice_segment_flag && !s->slice_initialized) {
815 av_log(s->avctx, AV_LOG_ERROR, "Independent slice segment missing.\n");
816 return AVERROR_INVALIDDATA;
817 }
818
819 18337 slice_address_length = av_ceil_log2(sps->ctb_width *
820 18337 sps->ctb_height);
821 18337 sh->slice_segment_addr = get_bitsz(gb, slice_address_length);
822
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 18337 times.
18337 if (sh->slice_segment_addr >= sps->ctb_width * sps->ctb_height) {
823 av_log(s->avctx, AV_LOG_ERROR,
824 "Invalid slice segment address: %u.\n",
825 sh->slice_segment_addr);
826 return AVERROR_INVALIDDATA;
827 }
828
829
2/2
✓ Branch 0 taken 10348 times.
✓ Branch 1 taken 7989 times.
18337 if (!sh->dependent_slice_segment_flag) {
830 10348 sh->slice_addr = sh->slice_segment_addr;
831 }
832 } else {
833 10643 sh->slice_segment_addr = sh->slice_addr = 0;
834 }
835
836
2/2
✓ Branch 0 taken 20991 times.
✓ Branch 1 taken 7989 times.
28980 if (!sh->dependent_slice_segment_flag) {
837
2/2
✓ Branch 0 taken 1834 times.
✓ Branch 1 taken 20991 times.
22825 for (i = 0; i < pps->num_extra_slice_header_bits; i++)
838 1834 skip_bits(gb, 1); // slice_reserved_undetermined_flag[]
839
840 20991 sh->slice_type = get_ue_golomb_long(gb);
841
2/2
✓ Branch 0 taken 19642 times.
✓ Branch 1 taken 1349 times.
20991 if (!(sh->slice_type == HEVC_SLICE_I ||
842
2/2
✓ Branch 0 taken 15309 times.
✓ Branch 1 taken 4333 times.
19642 sh->slice_type == HEVC_SLICE_P ||
843
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 15309 times.
15309 sh->slice_type == HEVC_SLICE_B)) {
844 av_log(s->avctx, AV_LOG_ERROR, "Unknown slice type: %d.\n",
845 sh->slice_type);
846 return AVERROR_INVALIDDATA;
847 }
848
5/6
✓ Branch 0 taken 1170 times.
✓ Branch 1 taken 19821 times.
✓ Branch 2 taken 1170 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 81 times.
✓ Branch 5 taken 1089 times.
20991 if (IS_IRAP(s) && sh->slice_type != HEVC_SLICE_I &&
849
1/2
✓ Branch 0 taken 81 times.
✗ Branch 1 not taken.
81 !pps->pps_curr_pic_ref_enabled_flag &&
850
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 81 times.
81 s->nuh_layer_id == 0) {
851 av_log(s->avctx, AV_LOG_ERROR, "Inter slices in an IRAP frame.\n");
852 return AVERROR_INVALIDDATA;
853 }
854
855 // when flag is not present, picture is inferred to be output
856 20991 sh->pic_output_flag = 1;
857
2/2
✓ Branch 0 taken 703 times.
✓ Branch 1 taken 20288 times.
20991 if (pps->output_flag_present_flag)
858 703 sh->pic_output_flag = get_bits1(gb);
859
860
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 20991 times.
20991 if (sps->separate_colour_plane)
861 sh->colour_plane_id = get_bits(gb, 2);
862
863
4/4
✓ Branch 0 taken 20301 times.
✓ Branch 1 taken 690 times.
✓ Branch 2 taken 48 times.
✓ Branch 3 taken 20253 times.
20991 if (!IS_IDR(s) ||
864
2/2
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 720 times.
738 (s->nuh_layer_id > 0 &&
865
2/2
✓ Branch 0 taken 13 times.
✓ Branch 1 taken 5 times.
18 !(vps->poc_lsb_not_present & (1 << layer_idx)))) {
866 int poc;
867
868 20266 sh->pic_order_cnt_lsb = get_bits(gb, sps->log2_max_poc_lsb);
869 20266 poc = ff_hevc_compute_poc(sps, s->poc_tid0, sh->pic_order_cnt_lsb, s->nal_unit_type);
870
3/4
✓ Branch 0 taken 10076 times.
✓ Branch 1 taken 10190 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 10076 times.
20266 if (!sh->first_slice_in_pic_flag && poc != sh->poc) {
871 av_log(s->avctx, AV_LOG_WARNING,
872 "Ignoring POC change between slices: %d -> %d\n", poc, sh->poc);
873 if (s->avctx->err_recognition & AV_EF_EXPLODE)
874 return AVERROR_INVALIDDATA;
875 poc = sh->poc;
876 }
877 20266 sh->poc = poc;
878 }
879
880
4/4
✓ Branch 0 taken 20301 times.
✓ Branch 1 taken 690 times.
✓ Branch 2 taken 20253 times.
✓ Branch 3 taken 48 times.
41244 if (!IS_IDR(s)) {
881 int pos;
882
883 20253 sh->short_term_ref_pic_set_sps_flag = get_bits1(gb);
884 20253 pos = get_bits_left(gb);
885
2/2
✓ Branch 0 taken 3948 times.
✓ Branch 1 taken 16305 times.
20253 if (!sh->short_term_ref_pic_set_sps_flag) {
886 3948 ret = ff_hevc_decode_short_term_rps(gb, s->avctx, &sh->slice_rps, sps, 1);
887
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3948 times.
3948 if (ret < 0)
888 return ret;
889
890 3948 sh->short_term_rps = &sh->slice_rps;
891 } else {
892 int numbits, rps_idx;
893
894
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 16305 times.
16305 if (!sps->nb_st_rps) {
895 av_log(s->avctx, AV_LOG_ERROR, "No ref lists in the SPS.\n");
896 return AVERROR_INVALIDDATA;
897 }
898
899 16305 numbits = av_ceil_log2(sps->nb_st_rps);
900
2/2
✓ Branch 0 taken 16277 times.
✓ Branch 1 taken 28 times.
16305 rps_idx = numbits > 0 ? get_bits(gb, numbits) : 0;
901 16305 sh->short_term_rps = &sps->st_rps[rps_idx];
902 }
903 20253 sh->short_term_ref_pic_set_size = pos - get_bits_left(gb);
904
905 20253 pos = get_bits_left(gb);
906 20253 ret = decode_lt_rps(sps, &sh->long_term_rps, gb, sh->poc, sh->pic_order_cnt_lsb);
907
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 20253 times.
20253 if (ret < 0) {
908 av_log(s->avctx, AV_LOG_WARNING, "Invalid long term RPS.\n");
909 if (s->avctx->err_recognition & AV_EF_EXPLODE)
910 return AVERROR_INVALIDDATA;
911 }
912 20253 sh->long_term_ref_pic_set_size = pos - get_bits_left(gb);
913
914
2/2
✓ Branch 0 taken 17818 times.
✓ Branch 1 taken 2435 times.
20253 if (sps->temporal_mvp_enabled)
915 17818 sh->slice_temporal_mvp_enabled_flag = get_bits1(gb);
916 else
917 2435 sh->slice_temporal_mvp_enabled_flag = 0;
918 } else {
919 738 sh->poc = 0;
920 738 sh->pic_order_cnt_lsb = 0;
921 738 sh->short_term_ref_pic_set_sps_flag = 0;
922 738 sh->short_term_ref_pic_set_size = 0;
923 738 sh->short_term_rps = NULL;
924 738 sh->long_term_ref_pic_set_size = 0;
925 738 sh->long_term_rps.nb_refs = 0;
926 738 sh->slice_temporal_mvp_enabled_flag = 0;
927 }
928
929 20991 sh->inter_layer_pred = 0;
930
2/2
✓ Branch 0 taken 516 times.
✓ Branch 1 taken 20475 times.
20991 if (s->nuh_layer_id > 0) {
931 516 int num_direct_ref_layers = vps->num_direct_ref_layers[layer_idx];
932
933
2/2
✓ Branch 0 taken 418 times.
✓ Branch 1 taken 98 times.
516 if (vps->default_ref_layers_active)
934 418 sh->inter_layer_pred = !!num_direct_ref_layers;
935
2/2
✓ Branch 0 taken 48 times.
✓ Branch 1 taken 50 times.
98 else if (num_direct_ref_layers) {
936 48 sh->inter_layer_pred = get_bits1(gb);
937
938
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) {
939 av_log(s->avctx, AV_LOG_ERROR,
940 "NumDirectRefLayers>1 not supported\n");
941 return AVERROR_PATCHWELCOME;
942 }
943 }
944 }
945
946
2/2
✓ Branch 0 taken 17468 times.
✓ Branch 1 taken 3523 times.
20991 if (sps->sao_enabled) {
947 17468 sh->slice_sample_adaptive_offset_flag[0] = get_bits1(gb);
948
2/2
✓ Branch 0 taken 17466 times.
✓ Branch 1 taken 2 times.
17468 if (sps->chroma_format_idc) {
949 17466 sh->slice_sample_adaptive_offset_flag[1] =
950 17466 sh->slice_sample_adaptive_offset_flag[2] = get_bits1(gb);
951 }
952 } else {
953 3523 sh->slice_sample_adaptive_offset_flag[0] = 0;
954 3523 sh->slice_sample_adaptive_offset_flag[1] = 0;
955 3523 sh->slice_sample_adaptive_offset_flag[2] = 0;
956 }
957
958 20991 sh->nb_refs[L0] = sh->nb_refs[L1] = 0;
959
4/4
✓ Branch 0 taken 16658 times.
✓ Branch 1 taken 4333 times.
✓ Branch 2 taken 15309 times.
✓ Branch 3 taken 1349 times.
20991 if (sh->slice_type == HEVC_SLICE_P || sh->slice_type == HEVC_SLICE_B) {
960 int nb_refs;
961
962 19642 sh->nb_refs[L0] = pps->num_ref_idx_l0_default_active;
963
2/2
✓ Branch 0 taken 15309 times.
✓ Branch 1 taken 4333 times.
19642 if (sh->slice_type == HEVC_SLICE_B)
964 15309 sh->nb_refs[L1] = pps->num_ref_idx_l1_default_active;
965
966
2/2
✓ Branch 1 taken 5430 times.
✓ Branch 2 taken 14212 times.
19642 if (get_bits1(gb)) { // num_ref_idx_active_override_flag
967 5430 sh->nb_refs[L0] = get_ue_golomb_31(gb) + 1;
968
2/2
✓ Branch 0 taken 2862 times.
✓ Branch 1 taken 2568 times.
5430 if (sh->slice_type == HEVC_SLICE_B)
969 2862 sh->nb_refs[L1] = get_ue_golomb_31(gb) + 1;
970 }
971
2/4
✓ Branch 0 taken 19642 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 19642 times.
19642 if (sh->nb_refs[L0] >= HEVC_MAX_REFS || sh->nb_refs[L1] >= HEVC_MAX_REFS) {
972 av_log(s->avctx, AV_LOG_ERROR, "Too many refs: %d/%d.\n",
973 sh->nb_refs[L0], sh->nb_refs[L1]);
974 return AVERROR_INVALIDDATA;
975 }
976
977 19642 sh->rpl_modification_flag[0] = 0;
978 19642 sh->rpl_modification_flag[1] = 0;
979 19642 nb_refs = ff_hevc_frame_nb_refs(sh, pps, layer_idx);
980
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 19642 times.
19642 if (!nb_refs) {
981 av_log(s->avctx, AV_LOG_ERROR, "Zero refs for a frame with P or B slices.\n");
982 return AVERROR_INVALIDDATA;
983 }
984
985
4/4
✓ Branch 0 taken 2302 times.
✓ Branch 1 taken 17340 times.
✓ Branch 2 taken 1978 times.
✓ Branch 3 taken 324 times.
19642 if (pps->lists_modification_present_flag && nb_refs > 1) {
986 1978 sh->rpl_modification_flag[0] = get_bits1(gb);
987
2/2
✓ Branch 0 taken 1432 times.
✓ Branch 1 taken 546 times.
1978 if (sh->rpl_modification_flag[0]) {
988
2/2
✓ Branch 0 taken 3366 times.
✓ Branch 1 taken 1432 times.
4798 for (i = 0; i < sh->nb_refs[L0]; i++)
989 3366 sh->list_entry_lx[0][i] = get_bits(gb, av_ceil_log2(nb_refs));
990 }
991
992
2/2
✓ Branch 0 taken 1795 times.
✓ Branch 1 taken 183 times.
1978 if (sh->slice_type == HEVC_SLICE_B) {
993 1795 sh->rpl_modification_flag[1] = get_bits1(gb);
994
2/2
✓ Branch 0 taken 652 times.
✓ Branch 1 taken 1143 times.
1795 if (sh->rpl_modification_flag[1] == 1)
995
2/2
✓ Branch 0 taken 1555 times.
✓ Branch 1 taken 652 times.
2207 for (i = 0; i < sh->nb_refs[L1]; i++)
996 1555 sh->list_entry_lx[1][i] = get_bits(gb, av_ceil_log2(nb_refs));
997 }
998 }
999
1000
2/2
✓ Branch 0 taken 15309 times.
✓ Branch 1 taken 4333 times.
19642 if (sh->slice_type == HEVC_SLICE_B)
1001 15309 sh->mvd_l1_zero_flag = get_bits1(gb);
1002
1003
2/2
✓ Branch 0 taken 16714 times.
✓ Branch 1 taken 2928 times.
19642 if (pps->cabac_init_present_flag)
1004 16714 sh->cabac_init_flag = get_bits1(gb);
1005 else
1006 2928 sh->cabac_init_flag = 0;
1007
1008 19642 sh->collocated_ref_idx = 0;
1009
2/2
✓ Branch 0 taken 17157 times.
✓ Branch 1 taken 2485 times.
19642 if (sh->slice_temporal_mvp_enabled_flag) {
1010 17157 sh->collocated_list = L0;
1011
2/2
✓ Branch 0 taken 14772 times.
✓ Branch 1 taken 2385 times.
17157 if (sh->slice_type == HEVC_SLICE_B)
1012 14772 sh->collocated_list = !get_bits1(gb);
1013
1014
2/2
✓ Branch 0 taken 15090 times.
✓ Branch 1 taken 2067 times.
17157 if (sh->nb_refs[sh->collocated_list] > 1) {
1015 15090 sh->collocated_ref_idx = get_ue_golomb_long(gb);
1016
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 15090 times.
15090 if (sh->collocated_ref_idx >= sh->nb_refs[sh->collocated_list]) {
1017 av_log(s->avctx, AV_LOG_ERROR,
1018 "Invalid collocated_ref_idx: %d.\n",
1019 sh->collocated_ref_idx);
1020 return AVERROR_INVALIDDATA;
1021 }
1022 }
1023 }
1024
1025
4/4
✓ Branch 0 taken 1547 times.
✓ Branch 1 taken 18095 times.
✓ Branch 2 taken 742 times.
✓ Branch 3 taken 805 times.
19642 if ((pps->weighted_pred_flag && sh->slice_type == HEVC_SLICE_P) ||
1026
3/4
✓ Branch 0 taken 696 times.
✓ Branch 1 taken 18141 times.
✓ Branch 2 taken 696 times.
✗ Branch 3 not taken.
18837 (pps->weighted_bipred_flag && sh->slice_type == HEVC_SLICE_B)) {
1027 1501 ret = pred_weight_table(sh, s->avctx, sps, gb);
1028
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1501 times.
1501 if (ret < 0)
1029 return ret;
1030 }
1031
1032 19642 sh->max_num_merge_cand = 5 - get_ue_golomb_long(gb);
1033
2/4
✓ Branch 0 taken 19642 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 19642 times.
19642 if (sh->max_num_merge_cand < 1 || sh->max_num_merge_cand > 5) {
1034 av_log(s->avctx, AV_LOG_ERROR,
1035 "Invalid number of merging MVP candidates: %d.\n",
1036 sh->max_num_merge_cand);
1037 return AVERROR_INVALIDDATA;
1038 }
1039
1040 // Syntax in 7.3.6.1
1041
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 19642 times.
19642 if (sps->motion_vector_resolution_control_idc == 2)
1042 sh->use_integer_mv_flag = get_bits1(gb);
1043 else
1044 // Inferred to be equal to motion_vector_resolution_control_idc if not present
1045 19642 sh->use_integer_mv_flag = sps->motion_vector_resolution_control_idc;
1046
1047 }
1048
1049 20991 sh->slice_qp_delta = get_se_golomb(gb);
1050
1051
2/2
✓ Branch 0 taken 271 times.
✓ Branch 1 taken 20720 times.
20991 if (pps->pic_slice_level_chroma_qp_offsets_present_flag) {
1052 271 sh->slice_cb_qp_offset = get_se_golomb(gb);
1053 271 sh->slice_cr_qp_offset = get_se_golomb(gb);
1054
2/4
✓ Branch 0 taken 271 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 271 times.
✗ Branch 3 not taken.
271 if (sh->slice_cb_qp_offset < -12 || sh->slice_cb_qp_offset > 12 ||
1055
2/4
✓ Branch 0 taken 271 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 271 times.
271 sh->slice_cr_qp_offset < -12 || sh->slice_cr_qp_offset > 12) {
1056 av_log(s->avctx, AV_LOG_ERROR, "Invalid slice cx qp offset.\n");
1057 return AVERROR_INVALIDDATA;
1058 }
1059 } else {
1060 20720 sh->slice_cb_qp_offset = 0;
1061 20720 sh->slice_cr_qp_offset = 0;
1062 }
1063
1064
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 20991 times.
20991 if (pps->pps_slice_act_qp_offsets_present_flag) {
1065 sh->slice_act_y_qp_offset = get_se_golomb(gb);
1066 sh->slice_act_cb_qp_offset = get_se_golomb(gb);
1067 sh->slice_act_cr_qp_offset = get_se_golomb(gb);
1068 }
1069
1070
2/2
✓ Branch 0 taken 43 times.
✓ Branch 1 taken 20948 times.
20991 if (pps->chroma_qp_offset_list_enabled_flag)
1071 43 sh->cu_chroma_qp_offset_enabled_flag = get_bits1(gb);
1072 else
1073 20948 sh->cu_chroma_qp_offset_enabled_flag = 0;
1074
1075
2/2
✓ Branch 0 taken 3405 times.
✓ Branch 1 taken 17586 times.
20991 if (pps->deblocking_filter_control_present_flag) {
1076 3405 int deblocking_filter_override_flag = 0;
1077
1078
2/2
✓ Branch 0 taken 1038 times.
✓ Branch 1 taken 2367 times.
3405 if (pps->deblocking_filter_override_enabled_flag)
1079 1038 deblocking_filter_override_flag = get_bits1(gb);
1080
1081
2/2
✓ Branch 0 taken 440 times.
✓ Branch 1 taken 2965 times.
3405 if (deblocking_filter_override_flag) {
1082 440 sh->disable_deblocking_filter_flag = get_bits1(gb);
1083
2/2
✓ Branch 0 taken 319 times.
✓ Branch 1 taken 121 times.
440 if (!sh->disable_deblocking_filter_flag) {
1084 319 int beta_offset_div2 = get_se_golomb(gb);
1085 319 int tc_offset_div2 = get_se_golomb(gb) ;
1086
3/6
✓ Branch 0 taken 319 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 319 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 319 times.
✗ Branch 5 not taken.
319 if (beta_offset_div2 < -6 || beta_offset_div2 > 6 ||
1087
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 319 times.
319 tc_offset_div2 < -6 || tc_offset_div2 > 6) {
1088 av_log(s->avctx, AV_LOG_ERROR,
1089 "Invalid deblock filter offsets: %d, %d\n",
1090 beta_offset_div2, tc_offset_div2);
1091 return AVERROR_INVALIDDATA;
1092 }
1093 319 sh->beta_offset = beta_offset_div2 * 2;
1094 319 sh->tc_offset = tc_offset_div2 * 2;
1095 }
1096 } else {
1097 2965 sh->disable_deblocking_filter_flag = pps->disable_dbf;
1098 2965 sh->beta_offset = pps->beta_offset;
1099 2965 sh->tc_offset = pps->tc_offset;
1100 }
1101 } else {
1102 17586 sh->disable_deblocking_filter_flag = 0;
1103 17586 sh->beta_offset = 0;
1104 17586 sh->tc_offset = 0;
1105 }
1106
1107
2/2
✓ Branch 0 taken 18323 times.
✓ Branch 1 taken 2668 times.
20991 if (pps->seq_loop_filter_across_slices_enabled_flag &&
1108
2/2
✓ Branch 0 taken 11754 times.
✓ Branch 1 taken 6569 times.
18323 (sh->slice_sample_adaptive_offset_flag[0] ||
1109
2/2
✓ Branch 0 taken 11736 times.
✓ Branch 1 taken 18 times.
11754 sh->slice_sample_adaptive_offset_flag[1] ||
1110
2/2
✓ Branch 0 taken 11672 times.
✓ Branch 1 taken 64 times.
11736 !sh->disable_deblocking_filter_flag)) {
1111 18259 sh->slice_loop_filter_across_slices_enabled_flag = get_bits1(gb);
1112 } else {
1113 2732 sh->slice_loop_filter_across_slices_enabled_flag = pps->seq_loop_filter_across_slices_enabled_flag;
1114 }
1115 }
1116
1117 28980 sh->num_entry_point_offsets = 0;
1118
4/4
✓ Branch 0 taken 25729 times.
✓ Branch 1 taken 3251 times.
✓ Branch 2 taken 4871 times.
✓ Branch 3 taken 20858 times.
28980 if (pps->tiles_enabled_flag || pps->entropy_coding_sync_enabled_flag) {
1119 8122 unsigned num_entry_point_offsets = get_ue_golomb_long(gb);
1120 // It would be possible to bound this tighter but this here is simpler
1121
2/4
✓ Branch 1 taken 8122 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 8122 times.
8122 if (num_entry_point_offsets > get_bits_left(gb) || num_entry_point_offsets > UINT16_MAX) {
1122 av_log(s->avctx, AV_LOG_ERROR, "num_entry_point_offsets %d is invalid\n", num_entry_point_offsets);
1123 return AVERROR_INVALIDDATA;
1124 }
1125
1126 8122 sh->num_entry_point_offsets = num_entry_point_offsets;
1127
2/2
✓ Branch 0 taken 2266 times.
✓ Branch 1 taken 5856 times.
8122 if (sh->num_entry_point_offsets > 0) {
1128 2266 int offset_len = get_ue_golomb_long(gb) + 1;
1129
1130
2/4
✓ Branch 0 taken 2266 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 2266 times.
2266 if (offset_len < 1 || offset_len > 32) {
1131 sh->num_entry_point_offsets = 0;
1132 av_log(s->avctx, AV_LOG_ERROR, "offset_len %d is invalid\n", offset_len);
1133 return AVERROR_INVALIDDATA;
1134 }
1135
1136 2266 av_freep(&sh->entry_point_offset);
1137 2266 av_freep(&sh->offset);
1138 2266 av_freep(&sh->size);
1139 2266 sh->entry_point_offset = av_malloc_array(sh->num_entry_point_offsets, sizeof(unsigned));
1140 2266 sh->offset = av_malloc_array(sh->num_entry_point_offsets + 1, sizeof(int));
1141 2266 sh->size = av_malloc_array(sh->num_entry_point_offsets + 1, sizeof(int));
1142
3/6
✓ Branch 0 taken 2266 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2266 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 2266 times.
2266 if (!sh->entry_point_offset || !sh->offset || !sh->size) {
1143 sh->num_entry_point_offsets = 0;
1144 av_log(s->avctx, AV_LOG_ERROR, "Failed to allocate memory\n");
1145 return AVERROR(ENOMEM);
1146 }
1147
2/2
✓ Branch 0 taken 10186 times.
✓ Branch 1 taken 2266 times.
12452 for (i = 0; i < sh->num_entry_point_offsets; i++) {
1148 10186 unsigned val = get_bits_long(gb, offset_len);
1149 10186 sh->entry_point_offset[i] = val + 1; // +1; // +1 to get the size
1150 }
1151 }
1152 }
1153
1154
2/2
✓ Branch 0 taken 2769 times.
✓ Branch 1 taken 26211 times.
28980 if (pps->slice_header_extension_present_flag) {
1155 2769 unsigned int length = get_ue_golomb_long(gb);
1156
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2769 times.
2769 if (length*8LL > get_bits_left(gb)) {
1157 av_log(s->avctx, AV_LOG_ERROR, "too many slice_header_extension_data_bytes\n");
1158 return AVERROR_INVALIDDATA;
1159 }
1160
2/2
✓ Branch 0 taken 20292 times.
✓ Branch 1 taken 2769 times.
23061 for (i = 0; i < length; i++)
1161 20292 skip_bits(gb, 8); // slice_header_extension_data_byte
1162 }
1163
1164 28980 ret = get_bits1(gb);
1165
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 28980 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
28980 if (!ret && get_bits_left(gb) >= 0) {
1166 av_log(s->avctx, AV_LOG_ERROR, "alignment_bit_equal_to_one=0\n");
1167 return AVERROR_INVALIDDATA;
1168 }
1169 28980 sh->data_offset = align_get_bits(gb) - gb->buffer;
1170
1171
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 28980 times.
28980 if (get_bits_left(gb) < 0) {
1172 av_log(s->avctx, AV_LOG_ERROR,
1173 "Overread slice header by %d bits\n", -get_bits_left(gb));
1174 return AVERROR_INVALIDDATA;
1175 }
1176
1177 // Inferred parameters
1178 28980 sh->slice_qp = 26U + pps->pic_init_qp_minus26 + sh->slice_qp_delta;
1179
1/2
✓ Branch 0 taken 28980 times.
✗ Branch 1 not taken.
28980 if (sh->slice_qp > 51 ||
1180
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 28980 times.
28980 sh->slice_qp < -sps->qp_bd_offset) {
1181 av_log(s->avctx, AV_LOG_ERROR,
1182 "The slice_qp %d is outside the valid range "
1183 "[%d, 51].\n",
1184 sh->slice_qp,
1185 -sps->qp_bd_offset);
1186 return AVERROR_INVALIDDATA;
1187 }
1188
1189 28980 sh->slice_ctb_addr_rs = sh->slice_segment_addr;
1190
1191
2/2
✓ Branch 0 taken 7989 times.
✓ Branch 1 taken 20991 times.
28980 if (sh->dependent_slice_segment_flag &&
1192
2/4
✓ Branch 0 taken 7989 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 7989 times.
7989 (!sh->slice_ctb_addr_rs || !pps->ctb_addr_rs_to_ts[sh->slice_ctb_addr_rs])) {
1193 av_log(s->avctx, AV_LOG_ERROR, "Impossible slice segment.\n");
1194 return AVERROR_INVALIDDATA;
1195 }
1196
1197 28980 return 0;
1198 }
1199
1200 #define CTB(tab, x, y) ((tab)[(y) * sps->ctb_width + (x)])
1201
1202 #define SET_SAO(elem, value) \
1203 do { \
1204 if (!sao_merge_up_flag && !sao_merge_left_flag) \
1205 sao->elem = value; \
1206 else if (sao_merge_left_flag) \
1207 sao->elem = CTB(l->sao, rx-1, ry).elem; \
1208 else if (sao_merge_up_flag) \
1209 sao->elem = CTB(l->sao, rx, ry-1).elem; \
1210 else \
1211 sao->elem = 0; \
1212 } while (0)
1213
1214 2010654 static void hls_sao_param(HEVCLocalContext *lc, const HEVCLayerContext *l,
1215 const HEVCPPS *pps, const HEVCSPS *sps,
1216 int rx, int ry)
1217 {
1218 2010654 const HEVCContext *const s = lc->parent;
1219 2010654 int sao_merge_left_flag = 0;
1220 2010654 int sao_merge_up_flag = 0;
1221 2010654 SAOParams *sao = &CTB(l->sao, rx, ry);
1222 int c_idx, i;
1223
1224
2/2
✓ Branch 0 taken 877838 times.
✓ Branch 1 taken 1132816 times.
2010654 if (s->sh.slice_sample_adaptive_offset_flag[0] ||
1225
2/2
✓ Branch 0 taken 1644 times.
✓ Branch 1 taken 876194 times.
877838 s->sh.slice_sample_adaptive_offset_flag[1]) {
1226
2/2
✓ Branch 0 taken 1090525 times.
✓ Branch 1 taken 43935 times.
1134460 if (rx > 0) {
1227
2/2
✓ Branch 0 taken 1069848 times.
✓ Branch 1 taken 20677 times.
1090525 if (lc->ctb_left_flag)
1228 1069848 sao_merge_left_flag = ff_hevc_sao_merge_flag_decode(lc);
1229 }
1230
4/4
✓ Branch 0 taken 1065444 times.
✓ Branch 1 taken 69016 times.
✓ Branch 2 taken 449038 times.
✓ Branch 3 taken 616406 times.
1134460 if (ry > 0 && !sao_merge_left_flag) {
1231
2/2
✓ Branch 0 taken 435585 times.
✓ Branch 1 taken 13453 times.
449038 if (lc->ctb_up_flag)
1232 435585 sao_merge_up_flag = ff_hevc_sao_merge_flag_decode(lc);
1233 }
1234 }
1235
1236
4/4
✓ Branch 0 taken 8042424 times.
✓ Branch 1 taken 96 times.
✓ Branch 2 taken 6031866 times.
✓ Branch 3 taken 2010654 times.
8042520 for (c_idx = 0; c_idx < (sps->chroma_format_idc ? 3 : 1); c_idx++) {
1237
2/2
✓ Branch 0 taken 2010654 times.
✓ Branch 1 taken 4021212 times.
6031866 int log2_sao_offset_scale = c_idx == 0 ? pps->log2_sao_offset_scale_luma :
1238 4021212 pps->log2_sao_offset_scale_chroma;
1239
1240
2/2
✓ Branch 0 taken 3179514 times.
✓ Branch 1 taken 2852352 times.
6031866 if (!s->sh.slice_sample_adaptive_offset_flag[c_idx]) {
1241 3179514 sao->type_idx[c_idx] = SAO_NOT_APPLIED;
1242 3179514 continue;
1243 }
1244
1245
2/2
✓ Branch 0 taken 859768 times.
✓ Branch 1 taken 1992584 times.
2852352 if (c_idx == 2) {
1246 859768 sao->type_idx[2] = sao->type_idx[1];
1247 859768 sao->eo_class[2] = sao->eo_class[1];
1248 } else {
1249
7/8
✓ Branch 0 taken 1734704 times.
✓ Branch 1 taken 257880 times.
✓ Branch 2 taken 600438 times.
✓ Branch 3 taken 1134266 times.
✓ Branch 5 taken 1134266 times.
✓ Branch 6 taken 257880 times.
✓ Branch 7 taken 257880 times.
✗ Branch 8 not taken.
1992584 SET_SAO(type_idx[c_idx], ff_hevc_sao_type_idx_decode(lc));
1250 }
1251
1252
2/2
✓ Branch 0 taken 2083356 times.
✓ Branch 1 taken 768996 times.
2852352 if (sao->type_idx[c_idx] == SAO_NOT_APPLIED)
1253 2083356 continue;
1254
1255
2/2
✓ Branch 0 taken 3075984 times.
✓ Branch 1 taken 768996 times.
3844980 for (i = 0; i < 4; i++)
1256
7/8
✓ Branch 0 taken 2519696 times.
✓ Branch 1 taken 556288 times.
✓ Branch 2 taken 1030292 times.
✓ Branch 3 taken 1489404 times.
✓ Branch 5 taken 1489404 times.
✓ Branch 6 taken 556288 times.
✓ Branch 7 taken 556288 times.
✗ Branch 8 not taken.
3075984 SET_SAO(offset_abs[c_idx][i], ff_hevc_sao_offset_abs_decode(lc, sps->bit_depth));
1257
1258
2/2
✓ Branch 0 taken 125490 times.
✓ Branch 1 taken 643506 times.
768996 if (sao->type_idx[c_idx] == SAO_BAND) {
1259
2/2
✓ Branch 0 taken 501960 times.
✓ Branch 1 taken 125490 times.
627450 for (i = 0; i < 4; i++) {
1260
2/2
✓ Branch 0 taken 368926 times.
✓ Branch 1 taken 133034 times.
501960 if (sao->offset_abs[c_idx][i]) {
1261
7/8
✓ Branch 0 taken 342359 times.
✓ Branch 1 taken 26567 times.
✓ Branch 2 taken 252831 times.
✓ Branch 3 taken 89528 times.
✓ Branch 5 taken 89528 times.
✓ Branch 6 taken 26567 times.
✓ Branch 7 taken 26567 times.
✗ Branch 8 not taken.
368926 SET_SAO(offset_sign[c_idx][i],
1262 ff_hevc_sao_offset_sign_decode(lc));
1263 } else {
1264 133034 sao->offset_sign[c_idx][i] = 0;
1265 }
1266 }
1267
7/8
✓ Branch 0 taken 114619 times.
✓ Branch 1 taken 10871 times.
✓ Branch 2 taken 82705 times.
✓ Branch 3 taken 31914 times.
✓ Branch 5 taken 31914 times.
✓ Branch 6 taken 10871 times.
✓ Branch 7 taken 10871 times.
✗ Branch 8 not taken.
125490 SET_SAO(band_position[c_idx], ff_hevc_sao_band_position_decode(lc));
1268
2/2
✓ Branch 0 taken 555245 times.
✓ Branch 1 taken 88261 times.
643506 } else if (c_idx != 2) {
1269
7/8
✓ Branch 0 taken 444792 times.
✓ Branch 1 taken 110453 times.
✓ Branch 2 taken 140460 times.
✓ Branch 3 taken 304332 times.
✓ Branch 5 taken 304332 times.
✓ Branch 6 taken 110453 times.
✓ Branch 7 taken 110453 times.
✗ Branch 8 not taken.
555245 SET_SAO(eo_class[c_idx], ff_hevc_sao_eo_class_decode(lc));
1270 }
1271
1272 // Inferred parameters
1273 768996 sao->offset_val[c_idx][0] = 0;
1274
2/2
✓ Branch 0 taken 3075984 times.
✓ Branch 1 taken 768996 times.
3844980 for (i = 0; i < 4; i++) {
1275 3075984 sao->offset_val[c_idx][i + 1] = sao->offset_abs[c_idx][i];
1276
2/2
✓ Branch 0 taken 2574024 times.
✓ Branch 1 taken 501960 times.
3075984 if (sao->type_idx[c_idx] == SAO_EDGE) {
1277
2/2
✓ Branch 0 taken 1287012 times.
✓ Branch 1 taken 1287012 times.
2574024 if (i > 1)
1278 1287012 sao->offset_val[c_idx][i + 1] = -sao->offset_val[c_idx][i + 1];
1279
2/2
✓ Branch 0 taken 163562 times.
✓ Branch 1 taken 338398 times.
501960 } else if (sao->offset_sign[c_idx][i]) {
1280 163562 sao->offset_val[c_idx][i + 1] = -sao->offset_val[c_idx][i + 1];
1281 }
1282 3075984 sao->offset_val[c_idx][i + 1] *= 1 << log2_sao_offset_scale;
1283 }
1284 }
1285 2010654 }
1286
1287 #undef SET_SAO
1288 #undef CTB
1289
1290 384850 static int hls_cross_component_pred(HEVCLocalContext *lc, int idx)
1291 {
1292 384850 int log2_res_scale_abs_plus1 = ff_hevc_log2_res_scale_abs(lc, idx);
1293
1294
2/2
✓ Branch 0 taken 235843 times.
✓ Branch 1 taken 149007 times.
384850 if (log2_res_scale_abs_plus1 != 0) {
1295 235843 int res_scale_sign_flag = ff_hevc_res_scale_sign_flag(lc, idx);
1296 235843 lc->tu.res_scale_val = (1 << (log2_res_scale_abs_plus1 - 1)) *
1297 235843 (1 - 2 * res_scale_sign_flag);
1298 } else {
1299 149007 lc->tu.res_scale_val = 0;
1300 }
1301
1302
1303 384850 return 0;
1304 }
1305
1306 17700421 static int hls_transform_unit(HEVCLocalContext *lc,
1307 const HEVCLayerContext *l,
1308 const HEVCPPS *pps, const HEVCSPS *sps,
1309 int x0, int y0,
1310 int xBase, int yBase, int cb_xBase, int cb_yBase,
1311 int log2_cb_size, int log2_trafo_size,
1312 int blk_idx, int cbf_luma, int *cbf_cb, int *cbf_cr)
1313 {
1314 17700421 const HEVCContext *const s = lc->parent;
1315 17700421 const int log2_trafo_size_c = log2_trafo_size - sps->hshift[1];
1316 int i;
1317
1318
2/2
✓ Branch 0 taken 11038256 times.
✓ Branch 1 taken 6662165 times.
17700421 if (lc->cu.pred_mode == MODE_INTRA) {
1319 11038256 int trafo_size = 1 << log2_trafo_size;
1320 11038256 ff_hevc_set_neighbour_available(lc, x0, y0, trafo_size, trafo_size, sps->log2_ctb_size);
1321
1322 11038256 s->hpc.intra_pred[log2_trafo_size - 2](lc, pps, x0, y0, 0);
1323 }
1324
1325
6/6
✓ Branch 0 taken 6254954 times.
✓ Branch 1 taken 11445467 times.
✓ Branch 2 taken 5517191 times.
✓ Branch 3 taken 737763 times.
✓ Branch 4 taken 5041380 times.
✓ Branch 5 taken 475811 times.
17700421 if (cbf_luma || cbf_cb[0] || cbf_cr[0] ||
1326
6/6
✓ Branch 0 taken 155767 times.
✓ Branch 1 taken 4885613 times.
✓ Branch 2 taken 146778 times.
✓ Branch 3 taken 8989 times.
✓ Branch 4 taken 52826 times.
✓ Branch 5 taken 93952 times.
17762235 (sps->chroma_format_idc == 2 && (cbf_cb[1] || cbf_cr[1]))) {
1327 12720856 int scan_idx = SCAN_DIAG;
1328 12720856 int scan_idx_c = SCAN_DIAG;
1329
4/4
✓ Branch 0 taken 8627537 times.
✓ Branch 1 taken 4093319 times.
✓ Branch 2 taken 6957564 times.
✓ Branch 3 taken 1669973 times.
19678420 int cbf_chroma = cbf_cb[0] || cbf_cr[0] ||
1330
2/2
✓ Branch 0 taken 231710 times.
✓ Branch 1 taken 6725854 times.
6957564 (sps->chroma_format_idc == 2 &&
1331
4/4
✓ Branch 0 taken 197852 times.
✓ Branch 1 taken 33858 times.
✓ Branch 2 taken 102075 times.
✓ Branch 3 taken 95777 times.
231710 (cbf_cb[1] || cbf_cr[1]));
1332
1333
4/4
✓ Branch 0 taken 3135859 times.
✓ Branch 1 taken 9584997 times.
✓ Branch 2 taken 1021402 times.
✓ Branch 3 taken 2114457 times.
12720856 if (pps->cu_qp_delta_enabled_flag && !lc->tu.is_cu_qp_delta_coded) {
1334 1021402 lc->tu.cu_qp_delta = ff_hevc_cu_qp_delta_abs(lc);
1335
2/2
✓ Branch 0 taken 529266 times.
✓ Branch 1 taken 492136 times.
1021402 if (lc->tu.cu_qp_delta != 0)
1336
2/2
✓ Branch 1 taken 286140 times.
✓ Branch 2 taken 243126 times.
529266 if (ff_hevc_cu_qp_delta_sign_flag(lc) == 1)
1337 286140 lc->tu.cu_qp_delta = -lc->tu.cu_qp_delta;
1338 1021402 lc->tu.is_cu_qp_delta_coded = 1;
1339
1340
2/2
✓ Branch 0 taken 1021401 times.
✓ Branch 1 taken 1 times.
1021402 if (lc->tu.cu_qp_delta < -(26 + sps->qp_bd_offset / 2) ||
1341
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1021401 times.
1021401 lc->tu.cu_qp_delta > (25 + sps->qp_bd_offset / 2)) {
1342 1 av_log(s->avctx, AV_LOG_ERROR,
1343 "The cu_qp_delta %d is outside the valid range "
1344 "[%d, %d].\n",
1345 lc->tu.cu_qp_delta,
1346 1 -(26 + sps->qp_bd_offset / 2),
1347 1 (25 + sps->qp_bd_offset / 2));
1348 1 return AVERROR_INVALIDDATA;
1349 }
1350
1351 1021401 ff_hevc_set_qPy(lc, l, pps, cb_xBase, cb_yBase, log2_cb_size);
1352 }
1353
1354
4/4
✓ Branch 0 taken 891951 times.
✓ Branch 1 taken 11828904 times.
✓ Branch 2 taken 841055 times.
✓ Branch 3 taken 50896 times.
12720855 if (s->sh.cu_chroma_qp_offset_enabled_flag && cbf_chroma &&
1355
3/4
✓ Branch 0 taken 841055 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 266275 times.
✓ Branch 3 taken 574780 times.
841055 !lc->cu.cu_transquant_bypass_flag && !lc->tu.is_cu_chroma_qp_offset_coded) {
1356 266275 int cu_chroma_qp_offset_flag = ff_hevc_cu_chroma_qp_offset_flag(lc);
1357
2/2
✓ Branch 0 taken 48966 times.
✓ Branch 1 taken 217309 times.
266275 if (cu_chroma_qp_offset_flag) {
1358 48966 int cu_chroma_qp_offset_idx = 0;
1359
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 48966 times.
48966 if (pps->chroma_qp_offset_list_len_minus1 > 0) {
1360 cu_chroma_qp_offset_idx = ff_hevc_cu_chroma_qp_offset_idx(lc, pps->chroma_qp_offset_list_len_minus1);
1361 av_log(s->avctx, AV_LOG_ERROR,
1362 "cu_chroma_qp_offset_idx not yet tested.\n");
1363 }
1364 48966 lc->tu.cu_qp_offset_cb = pps->cb_qp_offset_list[cu_chroma_qp_offset_idx];
1365 48966 lc->tu.cu_qp_offset_cr = pps->cr_qp_offset_list[cu_chroma_qp_offset_idx];
1366 } else {
1367 217309 lc->tu.cu_qp_offset_cb = 0;
1368 217309 lc->tu.cu_qp_offset_cr = 0;
1369 }
1370 266275 lc->tu.is_cu_chroma_qp_offset_coded = 1;
1371 }
1372
1373
4/4
✓ Branch 0 taken 8173055 times.
✓ Branch 1 taken 4547800 times.
✓ Branch 2 taken 7087254 times.
✓ Branch 3 taken 1085801 times.
12720855 if (lc->cu.pred_mode == MODE_INTRA && log2_trafo_size < 4) {
1374
2/2
✓ Branch 0 taken 4706039 times.
✓ Branch 1 taken 2381215 times.
7087254 if (lc->tu.intra_pred_mode >= 6 &&
1375
2/2
✓ Branch 0 taken 1703885 times.
✓ Branch 1 taken 3002154 times.
4706039 lc->tu.intra_pred_mode <= 14) {
1376 1703885 scan_idx = SCAN_VERT;
1377
2/2
✓ Branch 0 taken 2316579 times.
✓ Branch 1 taken 3066790 times.
5383369 } else if (lc->tu.intra_pred_mode >= 22 &&
1378
2/2
✓ Branch 0 taken 1951522 times.
✓ Branch 1 taken 365057 times.
2316579 lc->tu.intra_pred_mode <= 30) {
1379 1951522 scan_idx = SCAN_HORIZ;
1380 }
1381
1382
2/2
✓ Branch 0 taken 4233941 times.
✓ Branch 1 taken 2853313 times.
7087254 if (lc->tu.intra_pred_mode_c >= 6 &&
1383
2/2
✓ Branch 0 taken 1605944 times.
✓ Branch 1 taken 2627997 times.
4233941 lc->tu.intra_pred_mode_c <= 14) {
1384 1605944 scan_idx_c = SCAN_VERT;
1385
2/2
✓ Branch 0 taken 2203870 times.
✓ Branch 1 taken 3277440 times.
5481310 } else if (lc->tu.intra_pred_mode_c >= 22 &&
1386
2/2
✓ Branch 0 taken 1848696 times.
✓ Branch 1 taken 355174 times.
2203870 lc->tu.intra_pred_mode_c <= 30) {
1387 1848696 scan_idx_c = SCAN_HORIZ;
1388 }
1389 }
1390
1391 12720855 lc->tu.cross_pf = 0;
1392
1393
2/2
✓ Branch 0 taken 11445466 times.
✓ Branch 1 taken 1275389 times.
12720855 if (cbf_luma)
1394 11445466 ff_hevc_hls_residual_coding(lc, pps, x0, y0, log2_trafo_size, scan_idx, 0);
1395
6/6
✓ Branch 0 taken 12720663 times.
✓ Branch 1 taken 192 times.
✓ Branch 2 taken 6719141 times.
✓ Branch 3 taken 6001522 times.
✓ Branch 4 taken 209563 times.
✓ Branch 5 taken 6509578 times.
18931940 if (sps->chroma_format_idc && (log2_trafo_size > 2 || sps->chroma_format_idc == 3)) {
1396 6211085 int trafo_size_h = 1 << (log2_trafo_size_c + sps->hshift[1]);
1397 6211085 int trafo_size_v = 1 << (log2_trafo_size_c + sps->vshift[1]);
1398
4/4
✓ Branch 0 taken 336841 times.
✓ Branch 1 taken 5874244 times.
✓ Branch 2 taken 217816 times.
✓ Branch 3 taken 119025 times.
6428901 lc->tu.cross_pf = (pps->cross_component_prediction_enabled_flag && cbf_luma &&
1399
2/2
✓ Branch 0 taken 168873 times.
✓ Branch 1 taken 48943 times.
217816 (lc->cu.pred_mode == MODE_INTER ||
1400
2/2
✓ Branch 0 taken 143482 times.
✓ Branch 1 taken 25391 times.
168873 (lc->tu.chroma_mode_c == 4)));
1401
1402
2/2
✓ Branch 0 taken 192425 times.
✓ Branch 1 taken 6018660 times.
6211085 if (lc->tu.cross_pf) {
1403 192425 hls_cross_component_pred(lc, 0);
1404 }
1405
4/4
✓ Branch 0 taken 1359606 times.
✓ Branch 1 taken 11515766 times.
✓ Branch 2 taken 6664287 times.
✓ Branch 3 taken 6211085 times.
12875372 for (i = 0; i < (sps->chroma_format_idc == 2 ? 2 : 1); i++) {
1406
2/2
✓ Branch 0 taken 3479716 times.
✓ Branch 1 taken 3184571 times.
6664287 if (lc->cu.pred_mode == MODE_INTRA) {
1407 3479716 ff_hevc_set_neighbour_available(lc, x0, y0 + (i << log2_trafo_size_c),
1408 3479716 trafo_size_h, trafo_size_v, sps->log2_ctb_size);
1409 3479716 s->hpc.intra_pred[log2_trafo_size_c - 2](lc, pps, x0, y0 + (i << log2_trafo_size_c), 1);
1410 }
1411
2/2
✓ Branch 0 taken 1798375 times.
✓ Branch 1 taken 4865912 times.
6664287 if (cbf_cb[i])
1412 1798375 ff_hevc_hls_residual_coding(lc, pps, x0, y0 + (i << log2_trafo_size_c),
1413 log2_trafo_size_c, scan_idx_c, 1);
1414 else
1415
2/2
✓ Branch 0 taken 39658 times.
✓ Branch 1 taken 4826254 times.
4865912 if (lc->tu.cross_pf) {
1416 39658 ptrdiff_t stride = s->cur_frame->f->linesize[1];
1417 39658 int hshift = sps->hshift[1];
1418 39658 int vshift = sps->vshift[1];
1419 39658 const int16_t *coeffs_y = (int16_t*)lc->edge_emu_buffer;
1420 39658 int16_t *coeffs = (int16_t*)lc->edge_emu_buffer2;
1421 39658 int size = 1 << log2_trafo_size_c;
1422
1423 39658 uint8_t *dst = &s->cur_frame->f->data[1][(y0 >> vshift) * stride +
1424 39658 ((x0 >> hshift) << sps->pixel_shift)];
1425
2/2
✓ Branch 0 taken 1831504 times.
✓ Branch 1 taken 39658 times.
1871162 for (i = 0; i < (size * size); i++) {
1426 1831504 coeffs[i] = ((lc->tu.res_scale_val * coeffs_y[i]) >> 3);
1427 }
1428 39658 s->hevcdsp.add_residual[log2_trafo_size_c-2](dst, coeffs, stride);
1429 }
1430 }
1431
1432
2/2
✓ Branch 0 taken 192425 times.
✓ Branch 1 taken 6018660 times.
6211085 if (lc->tu.cross_pf) {
1433 192425 hls_cross_component_pred(lc, 1);
1434 }
1435
4/4
✓ Branch 0 taken 1359606 times.
✓ Branch 1 taken 11515766 times.
✓ Branch 2 taken 6664287 times.
✓ Branch 3 taken 6211085 times.
12875372 for (i = 0; i < (sps->chroma_format_idc == 2 ? 2 : 1); i++) {
1436
2/2
✓ Branch 0 taken 3479716 times.
✓ Branch 1 taken 3184571 times.
6664287 if (lc->cu.pred_mode == MODE_INTRA) {
1437 3479716 ff_hevc_set_neighbour_available(lc, x0, y0 + (i << log2_trafo_size_c),
1438 3479716 trafo_size_h, trafo_size_v, sps->log2_ctb_size);
1439 3479716 s->hpc.intra_pred[log2_trafo_size_c - 2](lc, pps, x0, y0 + (i << log2_trafo_size_c), 2);
1440 }
1441
2/2
✓ Branch 0 taken 1841805 times.
✓ Branch 1 taken 4822482 times.
6664287 if (cbf_cr[i])
1442 1841805 ff_hevc_hls_residual_coding(lc, pps, x0, y0 + (i << log2_trafo_size_c),
1443 log2_trafo_size_c, scan_idx_c, 2);
1444 else
1445
2/2
✓ Branch 0 taken 75924 times.
✓ Branch 1 taken 4746558 times.
4822482 if (lc->tu.cross_pf) {
1446 75924 ptrdiff_t stride = s->cur_frame->f->linesize[2];
1447 75924 int hshift = sps->hshift[2];
1448 75924 int vshift = sps->vshift[2];
1449 75924 const int16_t *coeffs_y = (int16_t*)lc->edge_emu_buffer;
1450 75924 int16_t *coeffs = (int16_t*)lc->edge_emu_buffer2;
1451 75924 int size = 1 << log2_trafo_size_c;
1452
1453 75924 uint8_t *dst = &s->cur_frame->f->data[2][(y0 >> vshift) * stride +
1454 75924 ((x0 >> hshift) << sps->pixel_shift)];
1455
2/2
✓ Branch 0 taken 3615552 times.
✓ Branch 1 taken 75924 times.
3691476 for (i = 0; i < (size * size); i++) {
1456 3615552 coeffs[i] = ((lc->tu.res_scale_val * coeffs_y[i]) >> 3);
1457 }
1458 75924 s->hevcdsp.add_residual[log2_trafo_size_c-2](dst, coeffs, stride);
1459 }
1460 }
1461
4/4
✓ Branch 0 taken 6509578 times.
✓ Branch 1 taken 192 times.
✓ Branch 2 taken 1673370 times.
✓ Branch 3 taken 4836208 times.
6509770 } else if (sps->chroma_format_idc && blk_idx == 3) {
1462 1673370 int trafo_size_h = 1 << (log2_trafo_size + 1);
1463 1673370 int trafo_size_v = 1 << (log2_trafo_size + sps->vshift[1]);
1464
4/4
✓ Branch 0 taken 500607 times.
✓ Branch 1 taken 3013002 times.
✓ Branch 2 taken 1840239 times.
✓ Branch 3 taken 1673370 times.
3513609 for (i = 0; i < (sps->chroma_format_idc == 2 ? 2 : 1); i++) {
1465
2/2
✓ Branch 0 taken 1340892 times.
✓ Branch 1 taken 499347 times.
1840239 if (lc->cu.pred_mode == MODE_INTRA) {
1466 1340892 ff_hevc_set_neighbour_available(lc, xBase, yBase + (i << log2_trafo_size),
1467 1340892 trafo_size_h, trafo_size_v, sps->log2_ctb_size);
1468 1340892 s->hpc.intra_pred[log2_trafo_size - 2](lc, pps, xBase, yBase + (i << log2_trafo_size), 1);
1469 }
1470
2/2
✓ Branch 0 taken 688330 times.
✓ Branch 1 taken 1151909 times.
1840239 if (cbf_cb[i])
1471 688330 ff_hevc_hls_residual_coding(lc, pps, xBase, yBase + (i << log2_trafo_size),
1472 log2_trafo_size, scan_idx_c, 1);
1473 }
1474
4/4
✓ Branch 0 taken 500607 times.
✓ Branch 1 taken 3013002 times.
✓ Branch 2 taken 1840239 times.
✓ Branch 3 taken 1673370 times.
3513609 for (i = 0; i < (sps->chroma_format_idc == 2 ? 2 : 1); i++) {
1475
2/2
✓ Branch 0 taken 1340892 times.
✓ Branch 1 taken 499347 times.
1840239 if (lc->cu.pred_mode == MODE_INTRA) {
1476 1340892 ff_hevc_set_neighbour_available(lc, xBase, yBase + (i << log2_trafo_size),
1477 1340892 trafo_size_h, trafo_size_v, sps->log2_ctb_size);
1478 1340892 s->hpc.intra_pred[log2_trafo_size - 2](lc, pps, xBase, yBase + (i << log2_trafo_size), 2);
1479 }
1480
2/2
✓ Branch 0 taken 775446 times.
✓ Branch 1 taken 1064793 times.
1840239 if (cbf_cr[i])
1481 775446 ff_hevc_hls_residual_coding(lc, pps, xBase, yBase + (i << log2_trafo_size),
1482 log2_trafo_size, scan_idx_c, 2);
1483 }
1484 }
1485
3/4
✓ Branch 0 taken 4979565 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2865200 times.
✓ Branch 3 taken 2114365 times.
4979565 } else if (sps->chroma_format_idc && lc->cu.pred_mode == MODE_INTRA) {
1486
4/4
✓ Branch 0 taken 1726595 times.
✓ Branch 1 taken 1138605 times.
✓ Branch 2 taken 60588 times.
✓ Branch 3 taken 1666007 times.
4064393 if (log2_trafo_size > 2 || sps->chroma_format_idc == 3) {
1487 1199193 int trafo_size_h = 1 << (log2_trafo_size_c + sps->hshift[1]);
1488 1199193 int trafo_size_v = 1 << (log2_trafo_size_c + sps->vshift[1]);
1489 1199193 ff_hevc_set_neighbour_available(lc, x0, y0, trafo_size_h, trafo_size_v,
1490 1199193 sps->log2_ctb_size);
1491 1199193 s->hpc.intra_pred[log2_trafo_size_c - 2](lc, pps, x0, y0, 1);
1492 1199193 s->hpc.intra_pred[log2_trafo_size_c - 2](lc, pps, x0, y0, 2);
1493
2/2
✓ Branch 0 taken 12467 times.
✓ Branch 1 taken 1186726 times.
1199193 if (sps->chroma_format_idc == 2) {
1494 12467 ff_hevc_set_neighbour_available(lc, x0, y0 + (1 << log2_trafo_size_c),
1495 12467 trafo_size_h, trafo_size_v, sps->log2_ctb_size);
1496 12467 s->hpc.intra_pred[log2_trafo_size_c - 2](lc, pps, x0, y0 + (1 << log2_trafo_size_c), 1);
1497 12467 s->hpc.intra_pred[log2_trafo_size_c - 2](lc, pps, x0, y0 + (1 << log2_trafo_size_c), 2);
1498 }
1499
2/2
✓ Branch 0 taken 376492 times.
✓ Branch 1 taken 1289515 times.
1666007 } else if (blk_idx == 3) {
1500 376492 int trafo_size_h = 1 << (log2_trafo_size + 1);
1501 376492 int trafo_size_v = 1 << (log2_trafo_size + sps->vshift[1]);
1502 376492 ff_hevc_set_neighbour_available(lc, xBase, yBase,
1503 376492 trafo_size_h, trafo_size_v, sps->log2_ctb_size);
1504 376492 s->hpc.intra_pred[log2_trafo_size - 2](lc, pps, xBase, yBase, 1);
1505 376492 s->hpc.intra_pred[log2_trafo_size - 2](lc, pps, xBase, yBase, 2);
1506
2/2
✓ Branch 0 taken 4196 times.
✓ Branch 1 taken 372296 times.
376492 if (sps->chroma_format_idc == 2) {
1507 4196 ff_hevc_set_neighbour_available(lc, xBase, yBase + (1 << log2_trafo_size),
1508 4196 trafo_size_h, trafo_size_v, sps->log2_ctb_size);
1509 4196 s->hpc.intra_pred[log2_trafo_size - 2](lc, pps, xBase, yBase + (1 << log2_trafo_size), 1);
1510 4196 s->hpc.intra_pred[log2_trafo_size - 2](lc, pps, xBase, yBase + (1 << log2_trafo_size), 2);
1511 }
1512 }
1513 }
1514
1515 17700420 return 0;
1516 }
1517
1518 403332 static void set_deblocking_bypass(uint8_t *is_pcm, const HEVCSPS *sps,
1519 int x0, int y0, int log2_cb_size)
1520 {
1521 403332 int cb_size = 1 << log2_cb_size;
1522 403332 int log2_min_pu_size = sps->log2_min_pu_size;
1523
1524 403332 int min_pu_width = sps->min_pu_width;
1525 403332 int x_end = FFMIN(x0 + cb_size, sps->width);
1526 403332 int y_end = FFMIN(y0 + cb_size, sps->height);
1527 int i, j;
1528
1529
2/2
✓ Branch 0 taken 562404 times.
✓ Branch 1 taken 403332 times.
965736 for (j = (y0 >> log2_min_pu_size); j < (y_end >> log2_min_pu_size); j++)
1530
2/2
✓ Branch 0 taken 1073856 times.
✓ Branch 1 taken 562404 times.
1636260 for (i = (x0 >> log2_min_pu_size); i < (x_end >> log2_min_pu_size); i++)
1531 1073856 is_pcm[i + j * min_pu_width] = 2;
1532 403332 }
1533
1534 21212838 static int hls_transform_tree(HEVCLocalContext *lc,
1535 const HEVCLayerContext *l,
1536 const HEVCPPS *pps, const HEVCSPS *sps,
1537 int x0, int y0,
1538 int xBase, int yBase, int cb_xBase, int cb_yBase,
1539 int log2_cb_size, int log2_trafo_size,
1540 int trafo_depth, int blk_idx,
1541 const int *base_cbf_cb, const int *base_cbf_cr)
1542 {
1543 21212838 const HEVCContext *const s = lc->parent;
1544 uint8_t split_transform_flag;
1545 int cbf_cb[2];
1546 int cbf_cr[2];
1547 int ret;
1548
1549 21212838 cbf_cb[0] = base_cbf_cb[0];
1550 21212838 cbf_cb[1] = base_cbf_cb[1];
1551 21212838 cbf_cr[0] = base_cbf_cr[0];
1552 21212838 cbf_cr[1] = base_cbf_cr[1];
1553
1554
2/2
✓ Branch 0 taken 7122643 times.
✓ Branch 1 taken 14090195 times.
21212838 if (lc->cu.intra_split_flag) {
1555
2/2
✓ Branch 0 taken 5342460 times.
✓ Branch 1 taken 1780183 times.
7122643 if (trafo_depth == 1) {
1556 5342460 lc->tu.intra_pred_mode = lc->pu.intra_pred_mode[blk_idx];
1557
2/2
✓ Branch 0 taken 135404 times.
✓ Branch 1 taken 5207056 times.
5342460 if (sps->chroma_format_idc == 3) {
1558 135404 lc->tu.intra_pred_mode_c = lc->pu.intra_pred_mode_c[blk_idx];
1559 135404 lc->tu.chroma_mode_c = lc->pu.chroma_mode_c[blk_idx];
1560 } else {
1561 5207056 lc->tu.intra_pred_mode_c = lc->pu.intra_pred_mode_c[0];
1562 5207056 lc->tu.chroma_mode_c = lc->pu.chroma_mode_c[0];
1563 }
1564 }
1565 } else {
1566 14090195 lc->tu.intra_pred_mode = lc->pu.intra_pred_mode[0];
1567 14090195 lc->tu.intra_pred_mode_c = lc->pu.intra_pred_mode_c[0];
1568 14090195 lc->tu.chroma_mode_c = lc->pu.chroma_mode_c[0];
1569 }
1570
1571
2/2
✓ Branch 0 taken 21087874 times.
✓ Branch 1 taken 124964 times.
21212838 if (log2_trafo_size <= sps->log2_max_trafo_size &&
1572
2/2
✓ Branch 0 taken 11550546 times.
✓ Branch 1 taken 9537328 times.
21087874 log2_trafo_size > sps->log2_min_tb_size &&
1573
2/2
✓ Branch 0 taken 9046131 times.
✓ Branch 1 taken 2504415 times.
11550546 trafo_depth < lc->cu.max_trafo_depth &&
1574
4/4
✓ Branch 0 taken 1912540 times.
✓ Branch 1 taken 7133591 times.
✓ Branch 2 taken 584312 times.
✓ Branch 3 taken 1328228 times.
9046131 !(lc->cu.intra_split_flag && trafo_depth == 0)) {
1575 7717903 split_transform_flag = ff_hevc_split_transform_flag_decode(lc, log2_trafo_size);
1576 } else {
1577 29589068 int inter_split = sps->max_transform_hierarchy_depth_inter == 0 &&
1578
2/2
✓ Branch 0 taken 1552658 times.
✓ Branch 1 taken 1046540 times.
2599198 lc->cu.pred_mode == MODE_INTER &&
1579
6/6
✓ Branch 0 taken 2599198 times.
✓ Branch 1 taken 10895737 times.
✓ Branch 2 taken 930859 times.
✓ Branch 3 taken 621799 times.
✓ Branch 4 taken 184863 times.
✓ Branch 5 taken 745996 times.
16094133 lc->cu.part_mode != PART_2Nx2N &&
1580 trafo_depth == 0;
1581
1582 13494935 split_transform_flag = log2_trafo_size > sps->log2_max_trafo_size ||
1583
8/8
✓ Branch 0 taken 13369971 times.
✓ Branch 1 taken 124964 times.
✓ Branch 2 taken 6530284 times.
✓ Branch 3 taken 6839687 times.
✓ Branch 4 taken 5202056 times.
✓ Branch 5 taken 1328228 times.
✓ Branch 6 taken 169700 times.
✓ Branch 7 taken 11872043 times.
13494935 (lc->cu.intra_split_flag && trafo_depth == 0) ||
1584 inter_split;
1585 }
1586
1587
6/6
✓ Branch 0 taken 21212646 times.
✓ Branch 1 taken 192 times.
✓ Branch 2 taken 9409248 times.
✓ Branch 3 taken 11803398 times.
✓ Branch 4 taken 314416 times.
✓ Branch 5 taken 9094832 times.
21212838 if (sps->chroma_format_idc && (log2_trafo_size > 2 || sps->chroma_format_idc == 3)) {
1588
4/4
✓ Branch 0 taken 4954836 times.
✓ Branch 1 taken 7162978 times.
✓ Branch 2 taken 1375388 times.
✓ Branch 3 taken 3579448 times.
12117814 if (trafo_depth == 0 || cbf_cb[0]) {
1589 8538366 cbf_cb[0] = ff_hevc_cbf_cb_cr_decode(lc, trafo_depth);
1590
6/6
✓ Branch 0 taken 598266 times.
✓ Branch 1 taken 7940100 times.
✓ Branch 2 taken 211307 times.
✓ Branch 3 taken 386959 times.
✓ Branch 4 taken 140037 times.
✓ Branch 5 taken 71270 times.
8538366 if (sps->chroma_format_idc == 2 && (!split_transform_flag || log2_trafo_size == 3)) {
1591 526996 cbf_cb[1] = ff_hevc_cbf_cb_cr_decode(lc, trafo_depth);
1592 }
1593 }
1594
1595
4/4
✓ Branch 0 taken 4954836 times.
✓ Branch 1 taken 7162978 times.
✓ Branch 2 taken 1283320 times.
✓ Branch 3 taken 3671516 times.
12117814 if (trafo_depth == 0 || cbf_cr[0]) {
1596 8446298 cbf_cr[0] = ff_hevc_cbf_cb_cr_decode(lc, trafo_depth);
1597
6/6
✓ Branch 0 taken 745306 times.
✓ Branch 1 taken 7700992 times.
✓ Branch 2 taken 259088 times.
✓ Branch 3 taken 486218 times.
✓ Branch 4 taken 175743 times.
✓ Branch 5 taken 83345 times.
8446298 if (sps->chroma_format_idc == 2 && (!split_transform_flag || log2_trafo_size == 3)) {
1598 661961 cbf_cr[1] = ff_hevc_cbf_cb_cr_decode(lc, trafo_depth);
1599 }
1600 }
1601 }
1602
1603
2/2
✓ Branch 0 taken 3512417 times.
✓ Branch 1 taken 17700421 times.
21212838 if (split_transform_flag) {
1604 3512417 const int trafo_size_split = 1 << (log2_trafo_size - 1);
1605 3512417 const int x1 = x0 + trafo_size_split;
1606 3512417 const int y1 = y0 + trafo_size_split;
1607
1608 #define SUBDIVIDE(x, y, idx) \
1609 do { \
1610 ret = hls_transform_tree(lc, l, pps, sps, \
1611 x, y, x0, y0, cb_xBase, cb_yBase, log2_cb_size, \
1612 log2_trafo_size - 1, trafo_depth + 1, idx, \
1613 cbf_cb, cbf_cr); \
1614 if (ret < 0) \
1615 return ret; \
1616 } while (0)
1617
1618
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 3512417 times.
3512417 SUBDIVIDE(x0, y0, 0);
1619
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 3512417 times.
3512417 SUBDIVIDE(x1, y0, 1);
1620
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 3512417 times.
3512417 SUBDIVIDE(x0, y1, 2);
1621
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 3512417 times.
3512417 SUBDIVIDE(x1, y1, 3);
1622
1623 #undef SUBDIVIDE
1624 } else {
1625 17700421 int min_tu_size = 1 << sps->log2_min_tb_size;
1626 17700421 int log2_min_tu_size = sps->log2_min_tb_size;
1627 17700421 int min_tu_width = sps->min_tb_width;
1628 17700421 int cbf_luma = 1;
1629
1630
4/4
✓ Branch 0 taken 6662165 times.
✓ Branch 1 taken 11038256 times.
✓ Branch 2 taken 1589494 times.
✓ Branch 3 taken 5072671 times.
17700421 if (lc->cu.pred_mode == MODE_INTRA || trafo_depth != 0 ||
1631
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] ||
1632
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]))) {
1633 16622705 cbf_luma = ff_hevc_cbf_luma_decode(lc, trafo_depth);
1634 }
1635
1636 17700421 ret = hls_transform_unit(lc, l, pps, sps,
1637 x0, y0, xBase, yBase, cb_xBase, cb_yBase,
1638 log2_cb_size, log2_trafo_size,
1639 blk_idx, cbf_luma, cbf_cb, cbf_cr);
1640
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 17700420 times.
17700421 if (ret < 0)
1641 1 return ret;
1642 // TODO: store cbf_luma somewhere else
1643
2/2
✓ Branch 0 taken 11445466 times.
✓ Branch 1 taken 6254954 times.
17700420 if (cbf_luma) {
1644 int i, j;
1645
2/2
✓ Branch 0 taken 25061386 times.
✓ Branch 1 taken 11445466 times.
36506852 for (i = 0; i < (1 << log2_trafo_size); i += min_tu_size)
1646
2/2
✓ Branch 0 taken 96203200 times.
✓ Branch 1 taken 25061386 times.
121264586 for (j = 0; j < (1 << log2_trafo_size); j += min_tu_size) {
1647 96203200 int x_tu = (x0 + j) >> log2_min_tu_size;
1648 96203200 int y_tu = (y0 + i) >> log2_min_tu_size;
1649 96203200 l->cbf_luma[y_tu * min_tu_width + x_tu] = 1;
1650 }
1651 }
1652
2/2
✓ Branch 0 taken 17165404 times.
✓ Branch 1 taken 535016 times.
17700420 if (!s->sh.disable_deblocking_filter_flag) {
1653 17165404 ff_hevc_deblocking_boundary_strengths(lc, l, pps, x0, y0, log2_trafo_size);
1654
2/2
✓ Branch 0 taken 409146 times.
✓ Branch 1 taken 16756258 times.
17165404 if (pps->transquant_bypass_enable_flag &&
1655
2/2
✓ Branch 0 taken 311137 times.
✓ Branch 1 taken 98009 times.
409146 lc->cu.cu_transquant_bypass_flag)
1656 311137 set_deblocking_bypass(l->is_pcm, sps, x0, y0, log2_trafo_size);
1657 }
1658 }
1659 21212837 return 0;
1660 }
1661
1662 12433 static int hls_pcm_sample(HEVCLocalContext *lc, const HEVCLayerContext *l,
1663 const HEVCPPS *pps, int x0, int y0, int log2_cb_size)
1664 {
1665 12433 const HEVCContext *const s = lc->parent;
1666 12433 const HEVCSPS *const sps = pps->sps;
1667 GetBitContext gb;
1668 12433 int cb_size = 1 << log2_cb_size;
1669 12433 ptrdiff_t stride0 = s->cur_frame->f->linesize[0];
1670 12433 uint8_t *dst0 = &s->cur_frame->f->data[0][y0 * stride0 + (x0 << sps->pixel_shift)];
1671
1672 24866 int length = cb_size * cb_size * sps->pcm.bit_depth + (sps->chroma_format_idc != 0 ?
1673 12433 (((cb_size >> sps->hshift[1]) * (cb_size >> sps->vshift[1])) +
1674 12433 ((cb_size >> sps->hshift[2]) * (cb_size >> sps->vshift[2]))) *
1675
1/2
✓ Branch 0 taken 12433 times.
✗ Branch 1 not taken.
12433 sps->pcm.bit_depth_chroma : 0);
1676 12433 const uint8_t *pcm = skip_bytes(&lc->cc, (length + 7) >> 3);
1677 int ret;
1678
1679
2/2
✓ Branch 0 taken 6937 times.
✓ Branch 1 taken 5496 times.
12433 if (!s->sh.disable_deblocking_filter_flag)
1680 6937 ff_hevc_deblocking_boundary_strengths(lc, l, pps, x0, y0, log2_cb_size);
1681
1682 12433 ret = init_get_bits(&gb, pcm, length);
1683
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12433 times.
12433 if (ret < 0)
1684 return ret;
1685
1686 12433 s->hevcdsp.put_pcm(dst0, stride0, cb_size, cb_size, &gb, sps->pcm.bit_depth);
1687
1/2
✓ Branch 0 taken 12433 times.
✗ Branch 1 not taken.
12433 if (sps->chroma_format_idc) {
1688 12433 ptrdiff_t stride1 = s->cur_frame->f->linesize[1];
1689 12433 ptrdiff_t stride2 = s->cur_frame->f->linesize[2];
1690 12433 uint8_t *dst1 = &s->cur_frame->f->data[1][(y0 >> sps->vshift[1]) * stride1 + ((x0 >> sps->hshift[1]) << sps->pixel_shift)];
1691 12433 uint8_t *dst2 = &s->cur_frame->f->data[2][(y0 >> sps->vshift[2]) * stride2 + ((x0 >> sps->hshift[2]) << sps->pixel_shift)];
1692
1693 12433 s->hevcdsp.put_pcm(dst1, stride1,
1694 12433 cb_size >> sps->hshift[1],
1695 12433 cb_size >> sps->vshift[1],
1696 12433 &gb, sps->pcm.bit_depth_chroma);
1697 12433 s->hevcdsp.put_pcm(dst2, stride2,
1698 12433 cb_size >> sps->hshift[2],
1699 12433 cb_size >> sps->vshift[2],
1700 12433 &gb, sps->pcm.bit_depth_chroma);
1701 }
1702
1703 12433 return 0;
1704 }
1705
1706 /**
1707 * 8.5.3.2.2.1 Luma sample unidirectional interpolation process
1708 *
1709 * @param s HEVC decoding context
1710 * @param dst target buffer for block data at block position
1711 * @param dststride stride of the dst buffer
1712 * @param ref reference picture buffer at origin (0, 0)
1713 * @param mv motion vector (relative to block position) to get pixel data from
1714 * @param x_off horizontal position of block from origin (0, 0)
1715 * @param y_off vertical position of block from origin (0, 0)
1716 * @param block_w width of block
1717 * @param block_h height of block
1718 * @param luma_weight weighting factor applied to the luma prediction
1719 * @param luma_offset additive offset applied to the luma prediction value
1720 */
1721
1722 6342554 static void luma_mc_uni(HEVCLocalContext *lc,
1723 const HEVCPPS *pps, const HEVCSPS *sps,
1724 uint8_t *dst, ptrdiff_t dststride,
1725 const AVFrame *ref, const Mv *mv, int x_off, int y_off,
1726 int block_w, int block_h, int luma_weight, int luma_offset)
1727 {
1728 6342554 const HEVCContext *const s = lc->parent;
1729 6342554 const uint8_t *src = ref->data[0];
1730 6342554 ptrdiff_t srcstride = ref->linesize[0];
1731 6342554 int pic_width = sps->width;
1732 6342554 int pic_height = sps->height;
1733 6342554 int mx = mv->x & 3;
1734 6342554 int my = mv->y & 3;
1735
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) ||
1736
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);
1737 6342554 int idx = hevc_pel_weight[block_w];
1738
1739 6342554 x_off += mv->x >> 2;
1740 6342554 y_off += mv->y >> 2;
1741 6342554 src += y_off * srcstride + (x_off * (1 << sps->pixel_shift));
1742
1743
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 ||
1744
2/2
✓ Branch 0 taken 6078917 times.
✓ Branch 1 taken 75976 times.
6154893 x_off >= pic_width - block_w - QPEL_EXTRA_AFTER ||
1745
2/2
✓ Branch 0 taken 5860745 times.
✓ Branch 1 taken 218172 times.
6078917 y_off >= pic_height - block_h - QPEL_EXTRA_AFTER ||
1746
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5860745 times.
5860745 ref == s->cur_frame->f) {
1747 481809 const ptrdiff_t edge_emu_stride = EDGE_EMU_BUFFER_STRIDE << sps->pixel_shift;
1748 481809 int offset = QPEL_EXTRA_BEFORE * srcstride + (QPEL_EXTRA_BEFORE << sps->pixel_shift);
1749 481809 int buf_offset = QPEL_EXTRA_BEFORE * edge_emu_stride + (QPEL_EXTRA_BEFORE << sps->pixel_shift);
1750
1751 481809 s->vdsp.emulated_edge_mc(lc->edge_emu_buffer, src - offset,
1752 edge_emu_stride, srcstride,
1753 block_w + QPEL_EXTRA,
1754 block_h + QPEL_EXTRA,
1755 x_off - QPEL_EXTRA_BEFORE, y_off - QPEL_EXTRA_BEFORE,
1756 pic_width, pic_height);
1757 481809 src = lc->edge_emu_buffer + buf_offset;
1758 481809 srcstride = edge_emu_stride;
1759 }
1760
1761
2/2
✓ Branch 0 taken 6208690 times.
✓ Branch 1 taken 133864 times.
6342554 if (!weight_flag)
1762 6208690 s->hevcdsp.put_hevc_qpel_uni[idx][!!my][!!mx](dst, dststride, src, srcstride,
1763 block_h, mx, my, block_w);
1764 else
1765 133864 s->hevcdsp.put_hevc_qpel_uni_w[idx][!!my][!!mx](dst, dststride, src, srcstride,
1766 133864 block_h, s->sh.luma_log2_weight_denom,
1767 luma_weight, luma_offset, mx, my, block_w);
1768 6342554 }
1769
1770 /**
1771 * 8.5.3.2.2.1 Luma sample bidirectional interpolation process
1772 *
1773 * @param s HEVC decoding context
1774 * @param dst target buffer for block data at block position
1775 * @param dststride stride of the dst buffer
1776 * @param ref0 reference picture0 buffer at origin (0, 0)
1777 * @param mv0 motion vector0 (relative to block position) to get pixel data from
1778 * @param x_off horizontal position of block from origin (0, 0)
1779 * @param y_off vertical position of block from origin (0, 0)
1780 * @param block_w width of block
1781 * @param block_h height of block
1782 * @param ref1 reference picture1 buffer at origin (0, 0)
1783 * @param mv1 motion vector1 (relative to block position) to get pixel data from
1784 * @param current_mv current motion vector structure
1785 */
1786 3992083 static void luma_mc_bi(HEVCLocalContext *lc,
1787 const HEVCPPS *pps, const HEVCSPS *sps,
1788 uint8_t *dst, ptrdiff_t dststride,
1789 const AVFrame *ref0, const Mv *mv0, int x_off, int y_off,
1790 int block_w, int block_h, const AVFrame *ref1,
1791 const Mv *mv1, struct MvField *current_mv)
1792 {
1793 3992083 const HEVCContext *const s = lc->parent;
1794 3992083 ptrdiff_t src0stride = ref0->linesize[0];
1795 3992083 ptrdiff_t src1stride = ref1->linesize[0];
1796 3992083 int pic_width = sps->width;
1797 3992083 int pic_height = sps->height;
1798 3992083 int mx0 = mv0->x & 3;
1799 3992083 int my0 = mv0->y & 3;
1800 3992083 int mx1 = mv1->x & 3;
1801 3992083 int my1 = mv1->y & 3;
1802
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) ||
1803
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);
1804 3992083 int x_off0 = x_off + (mv0->x >> 2);
1805 3992083 int y_off0 = y_off + (mv0->y >> 2);
1806 3992083 int x_off1 = x_off + (mv1->x >> 2);
1807 3992083 int y_off1 = y_off + (mv1->y >> 2);
1808 3992083 int idx = hevc_pel_weight[block_w];
1809
1810 3992083 const uint8_t *src0 = ref0->data[0] + y_off0 * src0stride + (int)((unsigned)x_off0 << sps->pixel_shift);
1811 3992083 const uint8_t *src1 = ref1->data[0] + y_off1 * src1stride + (int)((unsigned)x_off1 << sps->pixel_shift);
1812
1813
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 ||
1814
2/2
✓ Branch 0 taken 3757951 times.
✓ Branch 1 taken 69048 times.
3826999 x_off0 >= pic_width - block_w - QPEL_EXTRA_AFTER ||
1815
2/2
✓ Branch 0 taken 209680 times.
✓ Branch 1 taken 3548271 times.
3757951 y_off0 >= pic_height - block_h - QPEL_EXTRA_AFTER) {
1816 443812 const ptrdiff_t edge_emu_stride = EDGE_EMU_BUFFER_STRIDE << sps->pixel_shift;
1817 443812 int offset = QPEL_EXTRA_BEFORE * src0stride + (QPEL_EXTRA_BEFORE << sps->pixel_shift);
1818 443812 int buf_offset = QPEL_EXTRA_BEFORE * edge_emu_stride + (QPEL_EXTRA_BEFORE << sps->pixel_shift);
1819
1820 443812 s->vdsp.emulated_edge_mc(lc->edge_emu_buffer, src0 - offset,
1821 edge_emu_stride, src0stride,
1822 block_w + QPEL_EXTRA,
1823 block_h + QPEL_EXTRA,
1824 x_off0 - QPEL_EXTRA_BEFORE, y_off0 - QPEL_EXTRA_BEFORE,
1825 pic_width, pic_height);
1826 443812 src0 = lc->edge_emu_buffer + buf_offset;
1827 443812 src0stride = edge_emu_stride;
1828 }
1829
1830
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 ||
1831
2/2
✓ Branch 0 taken 3750497 times.
✓ Branch 1 taken 70955 times.
3821452 x_off1 >= pic_width - block_w - QPEL_EXTRA_AFTER ||
1832
2/2
✓ Branch 0 taken 212853 times.
✓ Branch 1 taken 3537644 times.
3750497 y_off1 >= pic_height - block_h - QPEL_EXTRA_AFTER) {
1833 454439 const ptrdiff_t edge_emu_stride = EDGE_EMU_BUFFER_STRIDE << sps->pixel_shift;
1834 454439 int offset = QPEL_EXTRA_BEFORE * src1stride + (QPEL_EXTRA_BEFORE << sps->pixel_shift);
1835 454439 int buf_offset = QPEL_EXTRA_BEFORE * edge_emu_stride + (QPEL_EXTRA_BEFORE << sps->pixel_shift);
1836
1837 454439 s->vdsp.emulated_edge_mc(lc->edge_emu_buffer2, src1 - offset,
1838 edge_emu_stride, src1stride,
1839 block_w + QPEL_EXTRA,
1840 block_h + QPEL_EXTRA,
1841 x_off1 - QPEL_EXTRA_BEFORE, y_off1 - QPEL_EXTRA_BEFORE,
1842 pic_width, pic_height);
1843 454439 src1 = lc->edge_emu_buffer2 + buf_offset;
1844 454439 src1stride = edge_emu_stride;
1845 }
1846
1847 3992083 s->hevcdsp.put_hevc_qpel[idx][!!my0][!!mx0](lc->tmp, src0, src0stride,
1848 block_h, mx0, my0, block_w);
1849
2/2
✓ Branch 0 taken 3918585 times.
✓ Branch 1 taken 73498 times.
3992083 if (!weight_flag)
1850 3918585 s->hevcdsp.put_hevc_qpel_bi[idx][!!my1][!!mx1](dst, dststride, src1, src1stride, lc->tmp,
1851 block_h, mx1, my1, block_w);
1852 else
1853 73498 s->hevcdsp.put_hevc_qpel_bi_w[idx][!!my1][!!mx1](dst, dststride, src1, src1stride, lc->tmp,
1854 73498 block_h, s->sh.luma_log2_weight_denom,
1855 73498 s->sh.luma_weight_l0[current_mv->ref_idx[0]],
1856 73498 s->sh.luma_weight_l1[current_mv->ref_idx[1]],
1857 73498 s->sh.luma_offset_l0[current_mv->ref_idx[0]] +
1858 73498 s->sh.luma_offset_l1[current_mv->ref_idx[1]],
1859 mx1, my1, block_w);
1860
1861 3992083 }
1862
1863 /**
1864 * 8.5.3.2.2.2 Chroma sample uniprediction interpolation process
1865 *
1866 * @param s HEVC decoding context
1867 * @param dst1 target buffer for block data at block position (U plane)
1868 * @param dst2 target buffer for block data at block position (V plane)
1869 * @param dststride stride of the dst1 and dst2 buffers
1870 * @param ref reference picture buffer at origin (0, 0)
1871 * @param mv motion vector (relative to block position) to get pixel data from
1872 * @param x_off horizontal position of block from origin (0, 0)
1873 * @param y_off vertical position of block from origin (0, 0)
1874 * @param block_w width of block
1875 * @param block_h height of block
1876 * @param chroma_weight weighting factor applied to the chroma prediction
1877 * @param chroma_offset additive offset applied to the chroma prediction value
1878 */
1879
1880 12685108 static void chroma_mc_uni(HEVCLocalContext *lc,
1881 const HEVCPPS *pps, const HEVCSPS *sps,
1882 uint8_t *dst0,
1883 ptrdiff_t dststride, const uint8_t *src0, ptrdiff_t srcstride, int reflist,
1884 int x_off, int y_off, int block_w, int block_h,
1885 const struct MvField *current_mv, int chroma_weight, int chroma_offset)
1886 {
1887 12685108 const HEVCContext *const s = lc->parent;
1888 12685108 int pic_width = sps->width >> sps->hshift[1];
1889 12685108 int pic_height = sps->height >> sps->vshift[1];
1890 12685108 const Mv *mv = &current_mv->mv[reflist];
1891
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) ||
1892
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);
1893 12685108 int idx = hevc_pel_weight[block_w];
1894 12685108 int hshift = sps->hshift[1];
1895 12685108 int vshift = sps->vshift[1];
1896 12685108 intptr_t mx = av_zero_extend(mv->x, 2 + hshift);
1897 12685108 intptr_t my = av_zero_extend(mv->y, 2 + vshift);
1898 12685108 intptr_t _mx = mx << (1 - hshift);
1899 12685108 intptr_t _my = my << (1 - vshift);
1900
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];
1901
1902 12685108 x_off += mv->x >> (2 + hshift);
1903 12685108 y_off += mv->y >> (2 + vshift);
1904 12685108 src0 += y_off * srcstride + (x_off * (1 << sps->pixel_shift));
1905
1906
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 ||
1907
2/2
✓ Branch 0 taken 12170872 times.
✓ Branch 1 taken 151944 times.
12322816 x_off >= pic_width - block_w - EPEL_EXTRA_AFTER ||
1908
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 ||
1909 emu) {
1910 950658 const int edge_emu_stride = EDGE_EMU_BUFFER_STRIDE << sps->pixel_shift;
1911 950658 int offset0 = EPEL_EXTRA_BEFORE * (srcstride + (1 << sps->pixel_shift));
1912 950658 int buf_offset0 = EPEL_EXTRA_BEFORE *
1913 950658 (edge_emu_stride + (1 << sps->pixel_shift));
1914 950658 s->vdsp.emulated_edge_mc(lc->edge_emu_buffer, src0 - offset0,
1915 edge_emu_stride, srcstride,
1916 block_w + EPEL_EXTRA, block_h + EPEL_EXTRA,
1917 x_off - EPEL_EXTRA_BEFORE,
1918 y_off - EPEL_EXTRA_BEFORE,
1919 pic_width, pic_height);
1920
1921 950658 src0 = lc->edge_emu_buffer + buf_offset0;
1922 950658 srcstride = edge_emu_stride;
1923 }
1924
2/2
✓ Branch 0 taken 12417380 times.
✓ Branch 1 taken 267728 times.
12685108 if (!weight_flag)
1925 12417380 s->hevcdsp.put_hevc_epel_uni[idx][!!my][!!mx](dst0, dststride, src0, srcstride,
1926 block_h, _mx, _my, block_w);
1927 else
1928 267728 s->hevcdsp.put_hevc_epel_uni_w[idx][!!my][!!mx](dst0, dststride, src0, srcstride,
1929 267728 block_h, s->sh.chroma_log2_weight_denom,
1930 chroma_weight, chroma_offset, _mx, _my, block_w);
1931 12685108 }
1932
1933 /**
1934 * 8.5.3.2.2.2 Chroma sample bidirectional interpolation process
1935 *
1936 * @param s HEVC decoding context
1937 * @param dst target buffer for block data at block position
1938 * @param dststride stride of the dst buffer
1939 * @param ref0 reference picture0 buffer at origin (0, 0)
1940 * @param mv0 motion vector0 (relative to block position) to get pixel data from
1941 * @param x_off horizontal position of block from origin (0, 0)
1942 * @param y_off vertical position of block from origin (0, 0)
1943 * @param block_w width of block
1944 * @param block_h height of block
1945 * @param ref1 reference picture1 buffer at origin (0, 0)
1946 * @param mv1 motion vector1 (relative to block position) to get pixel data from
1947 * @param current_mv current motion vector structure
1948 * @param cidx chroma component(cb, cr)
1949 */
1950 7984166 static void chroma_mc_bi(HEVCLocalContext *lc,
1951 const HEVCPPS *pps, const HEVCSPS *sps,
1952 uint8_t *dst0, ptrdiff_t dststride,
1953 const AVFrame *ref0, const AVFrame *ref1,
1954 int x_off, int y_off, int block_w, int block_h, const MvField *current_mv, int cidx)
1955 {
1956 7984166 const HEVCContext *const s = lc->parent;
1957 7984166 const uint8_t *src1 = ref0->data[cidx+1];
1958 7984166 const uint8_t *src2 = ref1->data[cidx+1];
1959 7984166 ptrdiff_t src1stride = ref0->linesize[cidx+1];
1960 7984166 ptrdiff_t src2stride = ref1->linesize[cidx+1];
1961
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) ||
1962
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);
1963 7984166 int pic_width = sps->width >> sps->hshift[1];
1964 7984166 int pic_height = sps->height >> sps->vshift[1];
1965 7984166 const Mv *const mv0 = &current_mv->mv[0];
1966 7984166 const Mv *const mv1 = &current_mv->mv[1];
1967 7984166 int hshift = sps->hshift[1];
1968 7984166 int vshift = sps->vshift[1];
1969
1970 7984166 intptr_t mx0 = av_zero_extend(mv0->x, 2 + hshift);
1971 7984166 intptr_t my0 = av_zero_extend(mv0->y, 2 + vshift);
1972 7984166 intptr_t mx1 = av_zero_extend(mv1->x, 2 + hshift);
1973 7984166 intptr_t my1 = av_zero_extend(mv1->y, 2 + vshift);
1974 7984166 intptr_t _mx0 = mx0 << (1 - hshift);
1975 7984166 intptr_t _my0 = my0 << (1 - vshift);
1976 7984166 intptr_t _mx1 = mx1 << (1 - hshift);
1977 7984166 intptr_t _my1 = my1 << (1 - vshift);
1978
1979 7984166 int x_off0 = x_off + (mv0->x >> (2 + hshift));
1980 7984166 int y_off0 = y_off + (mv0->y >> (2 + vshift));
1981 7984166 int x_off1 = x_off + (mv1->x >> (2 + hshift));
1982 7984166 int y_off1 = y_off + (mv1->y >> (2 + vshift));
1983 7984166 int idx = hevc_pel_weight[block_w];
1984 7984166 src1 += y_off0 * src1stride + (int)((unsigned)x_off0 << sps->pixel_shift);
1985 7984166 src2 += y_off1 * src2stride + (int)((unsigned)x_off1 << sps->pixel_shift);
1986
1987
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 ||
1988
2/2
✓ Branch 0 taken 7522052 times.
✓ Branch 1 taken 138092 times.
7660144 x_off0 >= pic_width - block_w - EPEL_EXTRA_AFTER ||
1989
2/2
✓ Branch 0 taken 418772 times.
✓ Branch 1 taken 7103280 times.
7522052 y_off0 >= pic_height - block_h - EPEL_EXTRA_AFTER) {
1990 880886 const int edge_emu_stride = EDGE_EMU_BUFFER_STRIDE << sps->pixel_shift;
1991 880886 int offset1 = EPEL_EXTRA_BEFORE * (src1stride + (1 << sps->pixel_shift));
1992 880886 int buf_offset1 = EPEL_EXTRA_BEFORE *
1993 880886 (edge_emu_stride + (1 << sps->pixel_shift));
1994
1995 880886 s->vdsp.emulated_edge_mc(lc->edge_emu_buffer, src1 - offset1,
1996 edge_emu_stride, src1stride,
1997 block_w + EPEL_EXTRA, block_h + EPEL_EXTRA,
1998 x_off0 - EPEL_EXTRA_BEFORE,
1999 y_off0 - EPEL_EXTRA_BEFORE,
2000 pic_width, pic_height);
2001
2002 880886 src1 = lc->edge_emu_buffer + buf_offset1;
2003 880886 src1stride = edge_emu_stride;
2004 }
2005
2006
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 ||
2007
2/2
✓ Branch 0 taken 7506674 times.
✓ Branch 1 taken 141904 times.
7648578 x_off1 >= pic_width - block_w - EPEL_EXTRA_AFTER ||
2008
2/2
✓ Branch 0 taken 425380 times.
✓ Branch 1 taken 7081294 times.
7506674 y_off1 >= pic_height - block_h - EPEL_EXTRA_AFTER) {
2009 902872 const int edge_emu_stride = EDGE_EMU_BUFFER_STRIDE << sps->pixel_shift;
2010 902872 int offset1 = EPEL_EXTRA_BEFORE * (src2stride + (1 << sps->pixel_shift));
2011 902872 int buf_offset1 = EPEL_EXTRA_BEFORE *
2012 902872 (edge_emu_stride + (1 << sps->pixel_shift));
2013
2014 902872 s->vdsp.emulated_edge_mc(lc->edge_emu_buffer2, src2 - offset1,
2015 edge_emu_stride, src2stride,
2016 block_w + EPEL_EXTRA, block_h + EPEL_EXTRA,
2017 x_off1 - EPEL_EXTRA_BEFORE,
2018 y_off1 - EPEL_EXTRA_BEFORE,
2019 pic_width, pic_height);
2020
2021 902872 src2 = lc->edge_emu_buffer2 + buf_offset1;
2022 902872 src2stride = edge_emu_stride;
2023 }
2024
2025 7984166 s->hevcdsp.put_hevc_epel[idx][!!my0][!!mx0](lc->tmp, src1, src1stride,
2026 block_h, _mx0, _my0, block_w);
2027
2/2
✓ Branch 0 taken 7837170 times.
✓ Branch 1 taken 146996 times.
7984166 if (!weight_flag)
2028 7837170 s->hevcdsp.put_hevc_epel_bi[idx][!!my1][!!mx1](dst0, s->cur_frame->f->linesize[cidx+1],
2029 7837170 src2, src2stride, lc->tmp,
2030 block_h, _mx1, _my1, block_w);
2031 else
2032 146996 s->hevcdsp.put_hevc_epel_bi_w[idx][!!my1][!!mx1](dst0, s->cur_frame->f->linesize[cidx+1],
2033 146996 src2, src2stride, lc->tmp,
2034 block_h,
2035 146996 s->sh.chroma_log2_weight_denom,
2036 146996 s->sh.chroma_weight_l0[current_mv->ref_idx[0]][cidx],
2037 146996 s->sh.chroma_weight_l1[current_mv->ref_idx[1]][cidx],
2038 146996 s->sh.chroma_offset_l0[current_mv->ref_idx[0]][cidx] +
2039 146996 s->sh.chroma_offset_l1[current_mv->ref_idx[1]][cidx],
2040 _mx1, _my1, block_w);
2041 7984166 }
2042
2043 14326720 static void hevc_await_progress(const HEVCContext *s, const HEVCFrame *ref,
2044 const Mv *mv, int y0, int height)
2045 {
2046
2/2
✓ Branch 0 taken 111132 times.
✓ Branch 1 taken 14215588 times.
14326720 if (s->avctx->active_thread_type == FF_THREAD_FRAME ) {
2047 111132 int y = FFMAX(0, (mv->y >> 2) + y0 + height + 9);
2048
2049 111132 ff_progress_frame_await(&ref->tf, y);
2050 }
2051 14326720 }
2052
2053 2833077 static void hevc_luma_mv_mvp_mode(HEVCLocalContext *lc,
2054 const HEVCPPS *pps, const HEVCSPS *sps,
2055 int x0, int y0, int nPbW,
2056 int nPbH, int log2_cb_size, int part_idx,
2057 int merge_idx, MvField *mv)
2058 {
2059 2833077 const HEVCContext *const s = lc->parent;
2060 2833077 enum InterPredIdc inter_pred_idc = PRED_L0;
2061 int mvp_flag;
2062
2063 2833077 ff_hevc_set_neighbour_available(lc, x0, y0, nPbW, nPbH, sps->log2_ctb_size);
2064 2833077 mv->pred_flag = 0;
2065
2/2
✓ Branch 0 taken 1922844 times.
✓ Branch 1 taken 910233 times.
2833077 if (s->sh.slice_type == HEVC_SLICE_B)
2066 1922844 inter_pred_idc = ff_hevc_inter_pred_idc_decode(lc, nPbW, nPbH);
2067
2068
2/2
✓ Branch 0 taken 2423759 times.
✓ Branch 1 taken 409318 times.
2833077 if (inter_pred_idc != PRED_L1) {
2069
1/2
✓ Branch 0 taken 2423759 times.
✗ Branch 1 not taken.
2423759 if (s->sh.nb_refs[L0])
2070 2423759 mv->ref_idx[0]= ff_hevc_ref_idx_lx_decode(lc, s->sh.nb_refs[L0]);
2071
2072 2423759 mv->pred_flag = PF_L0;
2073 2423759 ff_hevc_hls_mvd_coding(lc, x0, y0, 0);
2074 2423759 mvp_flag = ff_hevc_mvp_lx_flag_decode(lc);
2075 2423759 ff_hevc_luma_mv_mvp_mode(lc, pps, x0, y0, nPbW, nPbH, log2_cb_size,
2076 part_idx, merge_idx, mv, mvp_flag, 0);
2077 2423759 mv->mv[0].x += lc->pu.mvd.x;
2078 2423759 mv->mv[0].y += lc->pu.mvd.y;
2079 }
2080
2081
2/2
✓ Branch 0 taken 834243 times.
✓ Branch 1 taken 1998834 times.
2833077 if (inter_pred_idc != PRED_L0) {
2082
1/2
✓ Branch 0 taken 834243 times.
✗ Branch 1 not taken.
834243 if (s->sh.nb_refs[L1])
2083 834243 mv->ref_idx[1]= ff_hevc_ref_idx_lx_decode(lc, s->sh.nb_refs[L1]);
2084
2085
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) {
2086 163605 AV_ZERO32(&lc->pu.mvd);
2087 } else {
2088 670638 ff_hevc_hls_mvd_coding(lc, x0, y0, 1);
2089 }
2090
2091 834243 mv->pred_flag += PF_L1;
2092 834243 mvp_flag = ff_hevc_mvp_lx_flag_decode(lc);
2093 834243 ff_hevc_luma_mv_mvp_mode(lc, pps, x0, y0, nPbW, nPbH, log2_cb_size,
2094 part_idx, merge_idx, mv, mvp_flag, 1);
2095 834243 mv->mv[1].x += lc->pu.mvd.x;
2096 834243 mv->mv[1].y += lc->pu.mvd.y;
2097 }
2098 2833077 }
2099
2100 10334637 static void hls_prediction_unit(HEVCLocalContext *lc,
2101 const HEVCLayerContext *l,
2102 const HEVCPPS *pps, const HEVCSPS *sps,
2103 int x0, int y0, int nPbW, int nPbH,
2104 int log2_cb_size, int partIdx, int idx)
2105 {
2106 #define POS(c_idx, x, y) \
2107 s->cur_frame->f->data[c_idx] ? \
2108 &s->cur_frame->f->data[c_idx][((y) >> sps->vshift[c_idx]) * linesize[c_idx] + \
2109 (((x) >> sps->hshift[c_idx]) << sps->pixel_shift)] : NULL
2110 10334637 const HEVCContext *const s = lc->parent;
2111 10334637 int merge_idx = 0;
2112 10334637 struct MvField current_mv = {{{ 0 }}};
2113
2114 10334637 int min_pu_width = sps->min_pu_width;
2115
2116 10334637 MvField *tab_mvf = s->cur_frame->tab_mvf;
2117 10334637 const RefPicList *refPicList = s->cur_frame->refPicList;
2118 10334637 const HEVCFrame *ref0 = NULL, *ref1 = NULL;
2119 10334637 const int *linesize = s->cur_frame->f->linesize;
2120 10334637 uint8_t *dst0 = s->cur_frame->f->data[0] + y0 * linesize[0] + (x0 << sps->pixel_shift);
2121
1/2
✓ Branch 0 taken 10334637 times.
✗ Branch 1 not taken.
10334637 uint8_t *dst1 = POS(1, x0, y0);
2122
1/2
✓ Branch 0 taken 10334637 times.
✗ Branch 1 not taken.
10334637 uint8_t *dst2 = POS(2, x0, y0);
2123 10334637 int log2_min_cb_size = sps->log2_min_cb_size;
2124 10334637 int min_cb_width = sps->min_cb_width;
2125 10334637 int x_cb = x0 >> log2_min_cb_size;
2126 10334637 int y_cb = y0 >> log2_min_cb_size;
2127 int x_pu, y_pu;
2128 int i, j;
2129
2130 10334637 int skip_flag = SAMPLE_CTB(l->skip_flag, x_cb, y_cb);
2131
2132
2/2
✓ Branch 0 taken 5769030 times.
✓ Branch 1 taken 4565607 times.
10334637 if (!skip_flag)
2133 5769030 lc->pu.merge_flag = ff_hevc_merge_flag_decode(lc);
2134
2135
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) {
2136
2/2
✓ Branch 0 taken 7424707 times.
✓ Branch 1 taken 76853 times.
7501560 if (s->sh.max_num_merge_cand > 1)
2137 7424707 merge_idx = ff_hevc_merge_idx_decode(lc);
2138 else
2139 76853 merge_idx = 0;
2140
2141 7501560 ff_hevc_luma_mv_merge_mode(lc, pps, x0, y0, nPbW, nPbH, log2_cb_size,
2142 partIdx, merge_idx, &current_mv);
2143 } else {
2144 2833077 hevc_luma_mv_mvp_mode(lc, pps, sps, x0, y0, nPbW, nPbH, log2_cb_size,
2145 partIdx, merge_idx, &current_mv);
2146 }
2147
2148 10334637 x_pu = x0 >> sps->log2_min_pu_size;
2149 10334637 y_pu = y0 >> sps->log2_min_pu_size;
2150
2151
2/2
✓ Branch 0 taken 40422698 times.
✓ Branch 1 taken 10334637 times.
50757335 for (j = 0; j < nPbH >> sps->log2_min_pu_size; j++)
2152
2/2
✓ Branch 0 taken 258353932 times.
✓ Branch 1 taken 40422698 times.
298776630 for (i = 0; i < nPbW >> sps->log2_min_pu_size; i++)
2153 258353932 tab_mvf[(y_pu + j) * min_pu_width + x_pu + i] = current_mv;
2154
2155
2/2
✓ Branch 0 taken 9515439 times.
✓ Branch 1 taken 819198 times.
10334637 if (current_mv.pred_flag & PF_L0) {
2156 9515439 ref0 = refPicList[0].ref[current_mv.ref_idx[0]];
2157
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)
2158 return;
2159 9515439 hevc_await_progress(s, ref0, &current_mv.mv[0], y0, nPbH);
2160 }
2161
2/2
✓ Branch 0 taken 4811281 times.
✓ Branch 1 taken 5523356 times.
10334637 if (current_mv.pred_flag & PF_L1) {
2162 4811281 ref1 = refPicList[1].ref[current_mv.ref_idx[1]];
2163
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)
2164 return;
2165 4811281 hevc_await_progress(s, ref1, &current_mv.mv[1], y0, nPbH);
2166 }
2167
2168
2/2
✓ Branch 0 taken 5523356 times.
✓ Branch 1 taken 4811281 times.
10334637 if (current_mv.pred_flag == PF_L0) {
2169 5523356 int x0_c = x0 >> sps->hshift[1];
2170 5523356 int y0_c = y0 >> sps->vshift[1];
2171 5523356 int nPbW_c = nPbW >> sps->hshift[1];
2172 5523356 int nPbH_c = nPbH >> sps->vshift[1];
2173
2174 5523356 luma_mc_uni(lc, pps, sps, dst0, linesize[0], ref0->f,
2175 &current_mv.mv[0], x0, y0, nPbW, nPbH,
2176 5523356 s->sh.luma_weight_l0[current_mv.ref_idx[0]],
2177 5523356 s->sh.luma_offset_l0[current_mv.ref_idx[0]]);
2178
2179
1/2
✓ Branch 0 taken 5523356 times.
✗ Branch 1 not taken.
5523356 if (sps->chroma_format_idc) {
2180 5523356 chroma_mc_uni(lc, pps, sps, dst1, linesize[1], ref0->f->data[1], ref0->f->linesize[1],
2181 0, x0_c, y0_c, nPbW_c, nPbH_c, &current_mv,
2182 5523356 s->sh.chroma_weight_l0[current_mv.ref_idx[0]][0], s->sh.chroma_offset_l0[current_mv.ref_idx[0]][0]);
2183 5523356 chroma_mc_uni(lc, pps, sps, dst2, linesize[2], ref0->f->data[2], ref0->f->linesize[2],
2184 0, x0_c, y0_c, nPbW_c, nPbH_c, &current_mv,
2185 5523356 s->sh.chroma_weight_l0[current_mv.ref_idx[0]][1], s->sh.chroma_offset_l0[current_mv.ref_idx[0]][1]);
2186 }
2187
2/2
✓ Branch 0 taken 819198 times.
✓ Branch 1 taken 3992083 times.
4811281 } else if (current_mv.pred_flag == PF_L1) {
2188 819198 int x0_c = x0 >> sps->hshift[1];
2189 819198 int y0_c = y0 >> sps->vshift[1];
2190 819198 int nPbW_c = nPbW >> sps->hshift[1];
2191 819198 int nPbH_c = nPbH >> sps->vshift[1];
2192
2193 819198 luma_mc_uni(lc, pps, sps, dst0, linesize[0], ref1->f,
2194 &current_mv.mv[1], x0, y0, nPbW, nPbH,
2195 819198 s->sh.luma_weight_l1[current_mv.ref_idx[1]],
2196 819198 s->sh.luma_offset_l1[current_mv.ref_idx[1]]);
2197
2198
1/2
✓ Branch 0 taken 819198 times.
✗ Branch 1 not taken.
819198 if (sps->chroma_format_idc) {
2199 819198 chroma_mc_uni(lc, pps, sps, dst1, linesize[1], ref1->f->data[1], ref1->f->linesize[1],
2200 1, x0_c, y0_c, nPbW_c, nPbH_c, &current_mv,
2201 819198 s->sh.chroma_weight_l1[current_mv.ref_idx[1]][0], s->sh.chroma_offset_l1[current_mv.ref_idx[1]][0]);
2202
2203 819198 chroma_mc_uni(lc, pps, sps, dst2, linesize[2], ref1->f->data[2], ref1->f->linesize[2],
2204 1, x0_c, y0_c, nPbW_c, nPbH_c, &current_mv,
2205 819198 s->sh.chroma_weight_l1[current_mv.ref_idx[1]][1], s->sh.chroma_offset_l1[current_mv.ref_idx[1]][1]);
2206 }
2207
1/2
✓ Branch 0 taken 3992083 times.
✗ Branch 1 not taken.
3992083 } else if (current_mv.pred_flag == PF_BI) {
2208 3992083 int x0_c = x0 >> sps->hshift[1];
2209 3992083 int y0_c = y0 >> sps->vshift[1];
2210 3992083 int nPbW_c = nPbW >> sps->hshift[1];
2211 3992083 int nPbH_c = nPbH >> sps->vshift[1];
2212
2213 3992083 luma_mc_bi(lc, pps, sps, dst0, linesize[0], ref0->f,
2214 &current_mv.mv[0], x0, y0, nPbW, nPbH,
2215 3992083 ref1->f, &current_mv.mv[1], &current_mv);
2216
2217
1/2
✓ Branch 0 taken 3992083 times.
✗ Branch 1 not taken.
3992083 if (sps->chroma_format_idc) {
2218 3992083 chroma_mc_bi(lc, pps, sps, dst1, linesize[1], ref0->f, ref1->f,
2219 x0_c, y0_c, nPbW_c, nPbH_c, &current_mv, 0);
2220
2221 3992083 chroma_mc_bi(lc, pps, sps, dst2, linesize[2], ref0->f, ref1->f,
2222 x0_c, y0_c, nPbW_c, nPbH_c, &current_mv, 1);
2223 }
2224 }
2225 }
2226
2227 /**
2228 * 8.4.1
2229 */
2230 8652641 static int luma_intra_pred_mode(HEVCLocalContext *lc, const HEVCLayerContext *l,
2231 const HEVCSPS *sps,
2232 int x0, int y0, int pu_size,
2233 int prev_intra_luma_pred_flag)
2234 {
2235 8652641 const HEVCContext *const s = lc->parent;
2236 8652641 int x_pu = x0 >> sps->log2_min_pu_size;
2237 8652641 int y_pu = y0 >> sps->log2_min_pu_size;
2238 8652641 int min_pu_width = sps->min_pu_width;
2239 8652641 int size_in_pus = pu_size >> sps->log2_min_pu_size;
2240 8652641 int x0b = av_zero_extend(x0, sps->log2_ctb_size);
2241 8652641 int y0b = av_zero_extend(y0, sps->log2_ctb_size);
2242
2243
2/2
✓ Branch 0 taken 1103907 times.
✓ Branch 1 taken 131068 times.
1234975 int cand_up = (lc->ctb_up_flag || y0b) ?
2244
2/2
✓ Branch 0 taken 1234975 times.
✓ Branch 1 taken 7417666 times.
9887616 l->tab_ipm[(y_pu - 1) * min_pu_width + x_pu] : INTRA_DC;
2245
2/2
✓ Branch 0 taken 617350 times.
✓ Branch 1 taken 86418 times.
703768 int cand_left = (lc->ctb_left_flag || x0b) ?
2246
2/2
✓ Branch 0 taken 703768 times.
✓ Branch 1 taken 7948873 times.
9356409 l->tab_ipm[y_pu * min_pu_width + x_pu - 1] : INTRA_DC;
2247
2248 8652641 int y_ctb = (y0 >> (sps->log2_ctb_size)) << (sps->log2_ctb_size);
2249
2250 8652641 MvField *tab_mvf = s->cur_frame->tab_mvf;
2251 int intra_pred_mode;
2252 int candidate[3];
2253 int i, j;
2254
2255 // intra_pred_mode prediction does not cross vertical CTB boundaries
2256
2/2
✓ Branch 0 taken 1166898 times.
✓ Branch 1 taken 7485743 times.
8652641 if ((y0 - 1) < y_ctb)
2257 1166898 cand_up = INTRA_DC;
2258
2259
2/2
✓ Branch 0 taken 1860334 times.
✓ Branch 1 taken 6792307 times.
8652641 if (cand_left == cand_up) {
2260
2/2
✓ Branch 0 taken 1137941 times.
✓ Branch 1 taken 722393 times.
1860334 if (cand_left < 2) {
2261 1137941 candidate[0] = INTRA_PLANAR;
2262 1137941 candidate[1] = INTRA_DC;
2263 1137941 candidate[2] = INTRA_ANGULAR_26;
2264 } else {
2265 722393 candidate[0] = cand_left;
2266 722393 candidate[1] = 2 + ((cand_left - 2 - 1 + 32) & 31);
2267 722393 candidate[2] = 2 + ((cand_left - 2 + 1) & 31);
2268 }
2269 } else {
2270 6792307 candidate[0] = cand_left;
2271 6792307 candidate[1] = cand_up;
2272
4/4
✓ Branch 0 taken 5622697 times.
✓ Branch 1 taken 1169610 times.
✓ Branch 2 taken 4638865 times.
✓ Branch 3 taken 983832 times.
6792307 if (candidate[0] != INTRA_PLANAR && candidate[1] != INTRA_PLANAR) {
2273 4638865 candidate[2] = INTRA_PLANAR;
2274
4/4
✓ Branch 0 taken 1883263 times.
✓ Branch 1 taken 270179 times.
✓ Branch 2 taken 1410472 times.
✓ Branch 3 taken 472791 times.
2153442 } else if (candidate[0] != INTRA_DC && candidate[1] != INTRA_DC) {
2275 1410472 candidate[2] = INTRA_DC;
2276 } else {
2277 742970 candidate[2] = INTRA_ANGULAR_26;
2278 }
2279 }
2280
2281
2/2
✓ Branch 0 taken 5189654 times.
✓ Branch 1 taken 3462987 times.
8652641 if (prev_intra_luma_pred_flag) {
2282 5189654 intra_pred_mode = candidate[lc->pu.mpm_idx];
2283 } else {
2284
2/2
✓ Branch 0 taken 1637219 times.
✓ Branch 1 taken 1825768 times.
3462987 if (candidate[0] > candidate[1])
2285 1637219 FFSWAP(uint8_t, candidate[0], candidate[1]);
2286
2/2
✓ Branch 0 taken 2030968 times.
✓ Branch 1 taken 1432019 times.
3462987 if (candidate[0] > candidate[2])
2287 2030968 FFSWAP(uint8_t, candidate[0], candidate[2]);
2288
2/2
✓ Branch 0 taken 2553476 times.
✓ Branch 1 taken 909511 times.
3462987 if (candidate[1] > candidate[2])
2289 2553476 FFSWAP(uint8_t, candidate[1], candidate[2]);
2290
2291 3462987 intra_pred_mode = lc->pu.rem_intra_luma_pred_mode;
2292
2/2
✓ Branch 0 taken 10388961 times.
✓ Branch 1 taken 3462987 times.
13851948 for (i = 0; i < 3; i++)
2293
2/2
✓ Branch 0 taken 7566549 times.
✓ Branch 1 taken 2822412 times.
10388961 if (intra_pred_mode >= candidate[i])
2294 7566549 intra_pred_mode++;
2295 }
2296
2297 /* write the intra prediction units into the mv array */
2298
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8652641 times.
8652641 if (!size_in_pus)
2299 size_in_pus = 1;
2300
2/2
✓ Branch 0 taken 15667948 times.
✓ Branch 1 taken 8652641 times.
24320589 for (i = 0; i < size_in_pus; i++) {
2301 15667948 memset(&l->tab_ipm[(y_pu + i) * min_pu_width + x_pu],
2302 intra_pred_mode, size_in_pus);
2303
2304
2/2
✓ Branch 0 taken 51841868 times.
✓ Branch 1 taken 15667948 times.
67509816 for (j = 0; j < size_in_pus; j++) {
2305 51841868 tab_mvf[(y_pu + j) * min_pu_width + x_pu + i].pred_flag = PF_INTRA;
2306 }
2307 }
2308
2309 8652641 return intra_pred_mode;
2310 }
2311
2312 13257642 static av_always_inline void set_ct_depth(const HEVCSPS *sps, uint8_t *tab_ct_depth,
2313 int x0, int y0,
2314 int log2_cb_size, int ct_depth)
2315 {
2316 13257642 int length = (1 << log2_cb_size) >> sps->log2_min_cb_size;
2317 13257642 int x_cb = x0 >> sps->log2_min_cb_size;
2318 13257642 int y_cb = y0 >> sps->log2_min_cb_size;
2319 int y;
2320
2321
2/2
✓ Branch 0 taken 24995131 times.
✓ Branch 1 taken 13257642 times.
38252773 for (y = 0; y < length; y++)
2322 24995131 memset(&tab_ct_depth[(y_cb + y) * sps->min_cb_width + x_cb],
2323 ct_depth, length);
2324 13257642 }
2325
2326 static const uint8_t tab_mode_idx[] = {
2327 0, 1, 2, 2, 2, 2, 3, 5, 7, 8, 10, 12, 13, 15, 17, 18, 19, 20,
2328 21, 22, 23, 23, 24, 24, 25, 25, 26, 27, 27, 28, 28, 29, 29, 30, 31};
2329
2330 4645796 static void intra_prediction_unit(HEVCLocalContext *lc,
2331 const HEVCLayerContext *l, const HEVCSPS *sps,
2332 int x0, int y0,
2333 int log2_cb_size)
2334 {
2335 static const uint8_t intra_chroma_table[4] = { 0, 26, 10, 1 };
2336 uint8_t prev_intra_luma_pred_flag[4];
2337 4645796 int split = lc->cu.part_mode == PART_NxN;
2338 4645796 int pb_size = (1 << log2_cb_size) >> split;
2339 4645796 int side = split + 1;
2340 int chroma_mode;
2341 int i, j;
2342
2343
2/2
✓ Branch 0 taken 5981411 times.
✓ Branch 1 taken 4645796 times.
10627207 for (i = 0; i < side; i++)
2344
2/2
✓ Branch 0 taken 8652641 times.
✓ Branch 1 taken 5981411 times.
14634052 for (j = 0; j < side; j++)
2345 8652641 prev_intra_luma_pred_flag[2 * i + j] = ff_hevc_prev_intra_luma_pred_flag_decode(lc);
2346
2347
2/2
✓ Branch 0 taken 5981411 times.
✓ Branch 1 taken 4645796 times.
10627207 for (i = 0; i < side; i++) {
2348
2/2
✓ Branch 0 taken 8652641 times.
✓ Branch 1 taken 5981411 times.
14634052 for (j = 0; j < side; j++) {
2349
2/2
✓ Branch 0 taken 5189654 times.
✓ Branch 1 taken 3462987 times.
8652641 if (prev_intra_luma_pred_flag[2 * i + j])
2350 5189654 lc->pu.mpm_idx = ff_hevc_mpm_idx_decode(lc);
2351 else
2352 3462987 lc->pu.rem_intra_luma_pred_mode = ff_hevc_rem_intra_luma_pred_mode_decode(lc);
2353
2354 8652641 lc->pu.intra_pred_mode[2 * i + j] =
2355 8652641 luma_intra_pred_mode(lc, l, sps,
2356 8652641 x0 + pb_size * j, y0 + pb_size * i, pb_size,
2357 8652641 prev_intra_luma_pred_flag[2 * i + j]);
2358 }
2359 }
2360
2361
2/2
✓ Branch 0 taken 105006 times.
✓ Branch 1 taken 4540790 times.
4645796 if (sps->chroma_format_idc == 3) {
2362
2/2
✓ Branch 0 taken 138857 times.
✓ Branch 1 taken 105006 times.
243863 for (i = 0; i < side; i++) {
2363
2/2
✓ Branch 0 taken 206559 times.
✓ Branch 1 taken 138857 times.
345416 for (j = 0; j < side; j++) {
2364 206559 lc->pu.chroma_mode_c[2 * i + j] = chroma_mode = ff_hevc_intra_chroma_pred_mode_decode(lc);
2365
2/2
✓ Branch 0 taken 34248 times.
✓ Branch 1 taken 172311 times.
206559 if (chroma_mode != 4) {
2366
2/2
✓ Branch 0 taken 2343 times.
✓ Branch 1 taken 31905 times.
34248 if (lc->pu.intra_pred_mode[2 * i + j] == intra_chroma_table[chroma_mode])
2367 2343 lc->pu.intra_pred_mode_c[2 * i + j] = 34;
2368 else
2369 31905 lc->pu.intra_pred_mode_c[2 * i + j] = intra_chroma_table[chroma_mode];
2370 } else {
2371 172311 lc->pu.intra_pred_mode_c[2 * i + j] = lc->pu.intra_pred_mode[2 * i + j];
2372 }
2373 }
2374 }
2375
2/2
✓ Branch 0 taken 247106 times.
✓ Branch 1 taken 4293684 times.
4540790 } else if (sps->chroma_format_idc == 2) {
2376 int mode_idx;
2377 247106 lc->pu.chroma_mode_c[0] = chroma_mode = ff_hevc_intra_chroma_pred_mode_decode(lc);
2378
2/2
✓ Branch 0 taken 73995 times.
✓ Branch 1 taken 173111 times.
247106 if (chroma_mode != 4) {
2379
2/2
✓ Branch 0 taken 3515 times.
✓ Branch 1 taken 70480 times.
73995 if (lc->pu.intra_pred_mode[0] == intra_chroma_table[chroma_mode])
2380 3515 mode_idx = 34;
2381 else
2382 70480 mode_idx = intra_chroma_table[chroma_mode];
2383 } else {
2384 173111 mode_idx = lc->pu.intra_pred_mode[0];
2385 }
2386 247106 lc->pu.intra_pred_mode_c[0] = tab_mode_idx[mode_idx];
2387
2/2
✓ Branch 0 taken 4293492 times.
✓ Branch 1 taken 192 times.
4293684 } else if (sps->chroma_format_idc != 0) {
2388 4293492 chroma_mode = ff_hevc_intra_chroma_pred_mode_decode(lc);
2389
2/2
✓ Branch 0 taken 1033448 times.
✓ Branch 1 taken 3260044 times.
4293492 if (chroma_mode != 4) {
2390
2/2
✓ Branch 0 taken 77019 times.
✓ Branch 1 taken 956429 times.
1033448 if (lc->pu.intra_pred_mode[0] == intra_chroma_table[chroma_mode])
2391 77019 lc->pu.intra_pred_mode_c[0] = 34;
2392 else
2393 956429 lc->pu.intra_pred_mode_c[0] = intra_chroma_table[chroma_mode];
2394 } else {
2395 3260044 lc->pu.intra_pred_mode_c[0] = lc->pu.intra_pred_mode[0];
2396 }
2397 }
2398 4645796 }
2399
2400 8611847 static void intra_prediction_unit_default_value(HEVCLocalContext *lc,
2401 const HEVCLayerContext *l,
2402 const HEVCSPS *sps,
2403 int x0, int y0,
2404 int log2_cb_size)
2405 {
2406 8611847 const HEVCContext *const s = lc->parent;
2407 8611847 int pb_size = 1 << log2_cb_size;
2408 8611847 int size_in_pus = pb_size >> sps->log2_min_pu_size;
2409 8611847 int min_pu_width = sps->min_pu_width;
2410 8611847 MvField *tab_mvf = s->cur_frame->tab_mvf;
2411 8611847 int x_pu = x0 >> sps->log2_min_pu_size;
2412 8611847 int y_pu = y0 >> sps->log2_min_pu_size;
2413 int j, k;
2414
2415
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8611847 times.
8611847 if (size_in_pus == 0)
2416 size_in_pus = 1;
2417
2/2
✓ Branch 0 taken 36993546 times.
✓ Branch 1 taken 8611847 times.
45605393 for (j = 0; j < size_in_pus; j++)
2418 36993546 memset(&l->tab_ipm[(y_pu + j) * min_pu_width + x_pu], INTRA_DC, size_in_pus);
2419
2/2
✓ Branch 0 taken 12433 times.
✓ Branch 1 taken 8599414 times.
8611847 if (lc->cu.pred_mode == MODE_INTRA)
2420
2/2
✓ Branch 0 taken 40908 times.
✓ Branch 1 taken 12433 times.
53341 for (j = 0; j < size_in_pus; j++)
2421
2/2
✓ Branch 0 taken 182512 times.
✓ Branch 1 taken 40908 times.
223420 for (k = 0; k < size_in_pus; k++)
2422 182512 tab_mvf[(y_pu + j) * min_pu_width + x_pu + k].pred_flag = PF_INTRA;
2423 8611847 }
2424
2425 13257643 static int hls_coding_unit(HEVCLocalContext *lc, const HEVCContext *s,
2426 const HEVCLayerContext *l,
2427 const HEVCPPS *pps, const HEVCSPS *sps,
2428 int x0, int y0, int log2_cb_size)
2429 {
2430 13257643 int cb_size = 1 << log2_cb_size;
2431 13257643 int log2_min_cb_size = sps->log2_min_cb_size;
2432 13257643 int length = cb_size >> log2_min_cb_size;
2433 13257643 int min_cb_width = sps->min_cb_width;
2434 13257643 int x_cb = x0 >> log2_min_cb_size;
2435 13257643 int y_cb = y0 >> log2_min_cb_size;
2436 13257643 int idx = log2_cb_size - 2;
2437 13257643 int qp_block_mask = (1 << (sps->log2_ctb_size - pps->diff_cu_qp_delta_depth)) - 1;
2438 int x, y, ret;
2439
2440 13257643 lc->cu.x = x0;
2441 13257643 lc->cu.y = y0;
2442 13257643 lc->cu.pred_mode = MODE_INTRA;
2443 13257643 lc->cu.part_mode = PART_2Nx2N;
2444 13257643 lc->cu.intra_split_flag = 0;
2445
2446 13257643 SAMPLE_CTB(l->skip_flag, x_cb, y_cb) = 0;
2447
2/2
✓ Branch 0 taken 53030572 times.
✓ Branch 1 taken 13257643 times.
66288215 for (x = 0; x < 4; x++)
2448 53030572 lc->pu.intra_pred_mode[x] = 1;
2449
2/2
✓ Branch 0 taken 211788 times.
✓ Branch 1 taken 13045855 times.
13257643 if (pps->transquant_bypass_enable_flag) {
2450 211788 lc->cu.cu_transquant_bypass_flag = ff_hevc_cu_transquant_bypass_flag_decode(lc);
2451
2/2
✓ Branch 0 taken 92187 times.
✓ Branch 1 taken 119601 times.
211788 if (lc->cu.cu_transquant_bypass_flag)
2452 92187 set_deblocking_bypass(l->is_pcm, sps, x0, y0, log2_cb_size);
2453 } else
2454 13045855 lc->cu.cu_transquant_bypass_flag = 0;
2455
2456
2/2
✓ Branch 0 taken 9866641 times.
✓ Branch 1 taken 3391002 times.
13257643 if (s->sh.slice_type != HEVC_SLICE_I) {
2457 9866641 const int x0b = av_zero_extend(x0, sps->log2_ctb_size);
2458 9866641 const int y0b = av_zero_extend(y0, sps->log2_ctb_size);
2459 9866641 uint8_t skip_flag = ff_hevc_skip_flag_decode(lc, l->skip_flag,
2460 x0b, y0b, x_cb, y_cb,
2461 min_cb_width);
2462
2463 9866641 x = y_cb * min_cb_width + x_cb;
2464
2/2
✓ Branch 0 taken 20398545 times.
✓ Branch 1 taken 9866641 times.
30265186 for (y = 0; y < length; y++) {
2465 20398545 memset(&l->skip_flag[x], skip_flag, length);
2466 20398545 x += min_cb_width;
2467 }
2468
2/2
✓ Branch 0 taken 4565607 times.
✓ Branch 1 taken 5301034 times.
9866641 lc->cu.pred_mode = skip_flag ? MODE_SKIP : MODE_INTER;
2469 } else {
2470 3391002 x = y_cb * min_cb_width + x_cb;
2471
2/2
✓ Branch 0 taken 4596587 times.
✓ Branch 1 taken 3391002 times.
7987589 for (y = 0; y < length; y++) {
2472 4596587 memset(&l->skip_flag[x], 0, length);
2473 4596587 x += min_cb_width;
2474 }
2475 }
2476
2477
2/2
✓ Branch 0 taken 4565607 times.
✓ Branch 1 taken 8692036 times.
13257643 if (SAMPLE_CTB(l->skip_flag, x_cb, y_cb)) {
2478 4565607 hls_prediction_unit(lc, l, pps, sps,
2479 x0, y0, cb_size, cb_size, log2_cb_size, 0, idx);
2480 4565607 intra_prediction_unit_default_value(lc, l, sps, x0, y0, log2_cb_size);
2481
2482
2/2
✓ Branch 0 taken 4419094 times.
✓ Branch 1 taken 146513 times.
4565607 if (!s->sh.disable_deblocking_filter_flag)
2483 4419094 ff_hevc_deblocking_boundary_strengths(lc, l, pps, x0, y0, log2_cb_size);
2484 } else {
2485 8692036 int pcm_flag = 0;
2486
2487
2/2
✓ Branch 0 taken 5301034 times.
✓ Branch 1 taken 3391002 times.
8692036 if (s->sh.slice_type != HEVC_SLICE_I)
2488 5301034 lc->cu.pred_mode = ff_hevc_pred_mode_decode(lc);
2489
2/2
✓ Branch 0 taken 4658229 times.
✓ Branch 1 taken 4033807 times.
8692036 if (lc->cu.pred_mode != MODE_INTRA ||
2490
2/2
✓ Branch 0 taken 3510115 times.
✓ Branch 1 taken 1148114 times.
4658229 log2_cb_size == sps->log2_min_cb_size) {
2491 7543922 lc->cu.part_mode = ff_hevc_part_mode_decode(lc, sps, log2_cb_size);
2492
2/2
✓ Branch 0 taken 1443770 times.
✓ Branch 1 taken 6100152 times.
8987692 lc->cu.intra_split_flag = lc->cu.part_mode == PART_NxN &&
2493
2/2
✓ Branch 0 taken 1335615 times.
✓ Branch 1 taken 108155 times.
1443770 lc->cu.pred_mode == MODE_INTRA;
2494 }
2495
2496
2/2
✓ Branch 0 taken 4658229 times.
✓ Branch 1 taken 4033807 times.
8692036 if (lc->cu.pred_mode == MODE_INTRA) {
2497
4/4
✓ Branch 0 taken 3322614 times.
✓ Branch 1 taken 1335615 times.
✓ Branch 2 taken 163417 times.
✓ Branch 3 taken 3159197 times.
4658229 if (lc->cu.part_mode == PART_2Nx2N && sps->pcm_enabled &&
2498
2/2
✓ Branch 0 taken 161548 times.
✓ Branch 1 taken 1869 times.
163417 log2_cb_size >= sps->pcm.log2_min_pcm_cb_size &&
2499
2/2
✓ Branch 0 taken 153915 times.
✓ Branch 1 taken 7633 times.
161548 log2_cb_size <= sps->pcm.log2_max_pcm_cb_size) {
2500 153915 pcm_flag = ff_hevc_pcm_flag_decode(lc);
2501 }
2502
2/2
✓ Branch 0 taken 12433 times.
✓ Branch 1 taken 4645796 times.
4658229 if (pcm_flag) {
2503 12433 intra_prediction_unit_default_value(lc, l, sps, x0, y0, log2_cb_size);
2504 12433 ret = hls_pcm_sample(lc, l, pps, x0, y0, log2_cb_size);
2505
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 12425 times.
12433 if (sps->pcm_loop_filter_disabled)
2506 8 set_deblocking_bypass(l->is_pcm, sps, x0, y0, log2_cb_size);
2507
2508
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12433 times.
12433 if (ret < 0)
2509 return ret;
2510 } else {
2511 4645796 intra_prediction_unit(lc, l, sps, x0, y0, log2_cb_size);
2512 }
2513 } else {
2514 4033807 intra_prediction_unit_default_value(lc, l, sps, x0, y0, log2_cb_size);
2515
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) {
2516 2514894 case PART_2Nx2N:
2517 2514894 hls_prediction_unit(lc, l, pps, sps,
2518 x0, y0, cb_size, cb_size, log2_cb_size, 0, idx);
2519 2514894 break;
2520 499267 case PART_2NxN:
2521 499267 hls_prediction_unit(lc, l, pps, sps,
2522 x0, y0, cb_size, cb_size / 2, log2_cb_size, 0, idx);
2523 499267 hls_prediction_unit(lc, l, pps, sps,
2524 499267 x0, y0 + cb_size / 2, cb_size, cb_size / 2, log2_cb_size, 1, idx);
2525 499267 break;
2526 599864 case PART_Nx2N:
2527 599864 hls_prediction_unit(lc, l, pps, sps,
2528 x0, y0, cb_size / 2, cb_size, log2_cb_size, 0, idx - 1);
2529 599864 hls_prediction_unit(lc, l, pps, sps,
2530 599864 x0 + cb_size / 2, y0, cb_size / 2, cb_size, log2_cb_size, 1, idx - 1);
2531 599864 break;
2532 67350 case PART_2NxnU:
2533 67350 hls_prediction_unit(lc, l, pps, sps,
2534 x0, y0, cb_size, cb_size / 4, log2_cb_size, 0, idx);
2535 67350 hls_prediction_unit(lc, l, pps, sps,
2536 67350 x0, y0 + cb_size / 4, cb_size, cb_size * 3 / 4, log2_cb_size, 1, idx);
2537 67350 break;
2538 58153 case PART_2NxnD:
2539 58153 hls_prediction_unit(lc, l, pps, sps,
2540 58153 x0, y0, cb_size, cb_size * 3 / 4, log2_cb_size, 0, idx);
2541 58153 hls_prediction_unit(lc, l, pps, sps,
2542 58153 x0, y0 + cb_size * 3 / 4, cb_size, cb_size / 4, log2_cb_size, 1, idx);
2543 58153 break;
2544 100281 case PART_nLx2N:
2545 100281 hls_prediction_unit(lc, l, pps, sps,
2546 x0, y0, cb_size / 4, cb_size, log2_cb_size, 0, idx - 2);
2547 100281 hls_prediction_unit(lc, l, pps, sps,
2548 100281 x0 + cb_size / 4, y0, cb_size * 3 / 4, cb_size, log2_cb_size, 1, idx - 2);
2549 100281 break;
2550 85843 case PART_nRx2N:
2551 85843 hls_prediction_unit(lc, l, pps, sps,
2552 85843 x0, y0, cb_size * 3 / 4, cb_size, log2_cb_size, 0, idx - 2);
2553 85843 hls_prediction_unit(lc, l, pps, sps,
2554 85843 x0 + cb_size * 3 / 4, y0, cb_size / 4, cb_size, log2_cb_size, 1, idx - 2);
2555 85843 break;
2556 108155 case PART_NxN:
2557 108155 hls_prediction_unit(lc, l, pps, sps,
2558 x0, y0, cb_size / 2, cb_size / 2, log2_cb_size, 0, idx - 1);
2559 108155 hls_prediction_unit(lc, l, pps, sps,
2560 108155 x0 + cb_size / 2, y0, cb_size / 2, cb_size / 2, log2_cb_size, 1, idx - 1);
2561 108155 hls_prediction_unit(lc, l, pps, sps,
2562 108155 x0, y0 + cb_size / 2, cb_size / 2, cb_size / 2, log2_cb_size, 2, idx - 1);
2563 108155 hls_prediction_unit(lc, l, pps, sps,
2564 108155 x0 + cb_size / 2, y0 + cb_size / 2, cb_size / 2, cb_size / 2, log2_cb_size, 3, idx - 1);
2565 108155 break;
2566 }
2567 }
2568
2569
2/2
✓ Branch 0 taken 8679603 times.
✓ Branch 1 taken 12433 times.
8692036 if (!pcm_flag) {
2570 8679603 int rqt_root_cbf = 1;
2571
2572
2/2
✓ Branch 0 taken 4033807 times.
✓ Branch 1 taken 4645796 times.
8679603 if (lc->cu.pred_mode != MODE_INTRA &&
2573
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)) {
2574 2826655 rqt_root_cbf = ff_hevc_no_residual_syntax_flag_decode(lc);
2575 }
2576
2/2
✓ Branch 0 taken 7163170 times.
✓ Branch 1 taken 1516433 times.
8679603 if (rqt_root_cbf) {
2577 const static int cbf[2] = { 0 };
2578
2/2
✓ Branch 0 taken 4645796 times.
✓ Branch 1 taken 2517374 times.
7163170 lc->cu.max_trafo_depth = lc->cu.pred_mode == MODE_INTRA ?
2579 4645796 sps->max_transform_hierarchy_depth_intra + lc->cu.intra_split_flag :
2580 2517374 sps->max_transform_hierarchy_depth_inter;
2581 7163170 ret = hls_transform_tree(lc, l, pps, sps, x0, y0, x0, y0, x0, y0,
2582 log2_cb_size,
2583 log2_cb_size, 0, 0, cbf, cbf);
2584
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 7163169 times.
7163170 if (ret < 0)
2585 1 return ret;
2586 } else {
2587
2/2
✓ Branch 0 taken 1460001 times.
✓ Branch 1 taken 56432 times.
1516433 if (!s->sh.disable_deblocking_filter_flag)
2588 1460001 ff_hevc_deblocking_boundary_strengths(lc, l, pps, x0, y0, log2_cb_size);
2589 }
2590 }
2591 }
2592
2593
4/4
✓ Branch 0 taken 3427516 times.
✓ Branch 1 taken 9830126 times.
✓ Branch 2 taken 1190439 times.
✓ Branch 3 taken 2237077 times.
13257642 if (pps->cu_qp_delta_enabled_flag && lc->tu.is_cu_qp_delta_coded == 0)
2594 1190439 ff_hevc_set_qPy(lc, l, pps, x0, y0, log2_cb_size);
2595
2596 13257642 x = y_cb * min_cb_width + x_cb;
2597
2/2
✓ Branch 0 taken 24995131 times.
✓ Branch 1 taken 13257642 times.
38252773 for (y = 0; y < length; y++) {
2598 24995131 memset(&l->qp_y_tab[x], lc->qp_y, length);
2599 24995131 x += min_cb_width;
2600 }
2601
2602
2/2
✓ Branch 0 taken 4775181 times.
✓ Branch 1 taken 8482461 times.
13257642 if(((x0 + (1<<log2_cb_size)) & qp_block_mask) == 0 &&
2603
2/2
✓ Branch 0 taken 2872031 times.
✓ Branch 1 taken 1903150 times.
4775181 ((y0 + (1<<log2_cb_size)) & qp_block_mask) == 0) {
2604 2872031 lc->qPy_pred = lc->qp_y;
2605 }
2606
2607 13257642 set_ct_depth(sps, l->tab_ct_depth, x0, y0, log2_cb_size, lc->ct_depth);
2608
2609 13257642 return 0;
2610 }
2611
2612 17169233 static int hls_coding_quadtree(HEVCLocalContext *lc,
2613 const HEVCLayerContext *l,
2614 const HEVCPPS *pps, const HEVCSPS *sps,
2615 int x0, int y0,
2616 int log2_cb_size, int cb_depth)
2617 {
2618 17169233 const HEVCContext *const s = lc->parent;
2619 17169233 const int cb_size = 1 << log2_cb_size;
2620 int ret;
2621 int split_cu;
2622
2623 17169233 lc->ct_depth = cb_depth;
2624
2/2
✓ Branch 0 taken 17138762 times.
✓ Branch 1 taken 30471 times.
17169233 if (x0 + cb_size <= sps->width &&
2625
2/2
✓ Branch 0 taken 16812384 times.
✓ Branch 1 taken 326378 times.
17138762 y0 + cb_size <= sps->height &&
2626
2/2
✓ Branch 0 taken 9273274 times.
✓ Branch 1 taken 7539110 times.
16812384 log2_cb_size > sps->log2_min_cb_size) {
2627 9273274 split_cu = ff_hevc_split_coding_unit_flag_decode(lc, l->tab_ct_depth,
2628 sps, cb_depth, x0, y0);
2629 } else {
2630 7895959 split_cu = (log2_cb_size > sps->log2_min_cb_size);
2631 }
2632
2/2
✓ Branch 0 taken 4349409 times.
✓ Branch 1 taken 12819824 times.
17169233 if (pps->cu_qp_delta_enabled_flag &&
2633
2/2
✓ Branch 0 taken 2030275 times.
✓ Branch 1 taken 2319134 times.
4349409 log2_cb_size >= sps->log2_ctb_size - pps->diff_cu_qp_delta_depth) {
2634 2030275 lc->tu.is_cu_qp_delta_coded = 0;
2635 2030275 lc->tu.cu_qp_delta = 0;
2636 }
2637
2638
2/2
✓ Branch 0 taken 519326 times.
✓ Branch 1 taken 16649907 times.
17169233 if (s->sh.cu_chroma_qp_offset_enabled_flag &&
2639
1/2
✓ Branch 0 taken 519326 times.
✗ Branch 1 not taken.
519326 log2_cb_size >= sps->log2_ctb_size - pps->diff_cu_chroma_qp_offset_depth) {
2640 519326 lc->tu.is_cu_chroma_qp_offset_coded = 0;
2641 }
2642
2643
2/2
✓ Branch 0 taken 3911590 times.
✓ Branch 1 taken 13257643 times.
17169233 if (split_cu) {
2644 3911590 int qp_block_mask = (1 << (sps->log2_ctb_size - pps->diff_cu_qp_delta_depth)) - 1;
2645 3911590 const int cb_size_split = cb_size >> 1;
2646 3911590 const int x1 = x0 + cb_size_split;
2647 3911590 const int y1 = y0 + cb_size_split;
2648
2649 3911590 int more_data = 0;
2650
2651 3911590 more_data = hls_coding_quadtree(lc, l, pps, sps,
2652 x0, y0, log2_cb_size - 1, cb_depth + 1);
2653
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 3911588 times.
3911590 if (more_data < 0)
2654 2 return more_data;
2655
2656
4/4
✓ Branch 0 taken 3911579 times.
✓ Branch 1 taken 9 times.
✓ Branch 2 taken 3882630 times.
✓ Branch 3 taken 28949 times.
3911588 if (more_data && x1 < sps->width) {
2657 3882630 more_data = hls_coding_quadtree(lc, l, pps, sps,
2658 x1, y0, log2_cb_size - 1, cb_depth + 1);
2659
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3882630 times.
3882630 if (more_data < 0)
2660 return more_data;
2661 }
2662
4/4
✓ Branch 0 taken 3901096 times.
✓ Branch 1 taken 10492 times.
✓ Branch 2 taken 3696654 times.
✓ Branch 3 taken 204442 times.
3911588 if (more_data && y1 < sps->height) {
2663 3696654 more_data = hls_coding_quadtree(lc, l, pps, sps,
2664 x0, y1, log2_cb_size - 1, cb_depth + 1);
2665
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3696654 times.
3696654 if (more_data < 0)
2666 return more_data;
2667 }
2668
4/4
✓ Branch 0 taken 3894871 times.
✓ Branch 1 taken 16717 times.
✓ Branch 2 taken 3872147 times.
✓ Branch 3 taken 22724 times.
3911588 if (more_data && x1 < sps->width &&
2669
2/2
✓ Branch 0 taken 3667705 times.
✓ Branch 1 taken 204442 times.
3872147 y1 < sps->height) {
2670 3667705 more_data = hls_coding_quadtree(lc, l, pps, sps,
2671 x1, y1, log2_cb_size - 1, cb_depth + 1);
2672
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 3667704 times.
3667705 if (more_data < 0)
2673 1 return more_data;
2674 }
2675
2676
2/2
✓ Branch 0 taken 2267229 times.
✓ Branch 1 taken 1644358 times.
3911587 if(((x0 + (1<<log2_cb_size)) & qp_block_mask) == 0 &&
2677
2/2
✓ Branch 0 taken 1716742 times.
✓ Branch 1 taken 550487 times.
2267229 ((y0 + (1<<log2_cb_size)) & qp_block_mask) == 0)
2678 1716742 lc->qPy_pred = lc->qp_y;
2679
2680
2/2
✓ Branch 0 taken 3874942 times.
✓ Branch 1 taken 36645 times.
3911587 if (more_data)
2681
2/2
✓ Branch 0 taken 101528 times.
✓ Branch 1 taken 3773414 times.
3976470 return ((x1 + cb_size_split) < sps->width ||
2682
2/2
✓ Branch 0 taken 101527 times.
✓ Branch 1 taken 1 times.
101528 (y1 + cb_size_split) < sps->height);
2683 else
2684 36645 return 0;
2685 } else {
2686 13257643 ret = hls_coding_unit(lc, s, l, pps, sps, x0, y0, log2_cb_size);
2687
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 13257642 times.
13257643 if (ret < 0)
2688 1 return ret;
2689 13257642 if ((!((x0 + cb_size) %
2690
2/2
✓ Branch 0 taken 9328853 times.
✓ Branch 1 taken 3928789 times.
13257642 (1 << (sps->log2_ctb_size))) ||
2691
2/2
✓ Branch 0 taken 73530 times.
✓ Branch 1 taken 9255323 times.
9328853 (x0 + cb_size >= sps->width)) &&
2692 4002319 (!((y0 + cb_size) %
2693
2/2
✓ Branch 0 taken 2091438 times.
✓ Branch 1 taken 1910881 times.
4002319 (1 << (sps->log2_ctb_size))) ||
2694
2/2
✓ Branch 0 taken 99772 times.
✓ Branch 1 taken 1991666 times.
2091438 (y0 + cb_size >= sps->height))) {
2695 2010653 int end_of_slice_flag = ff_hevc_end_of_slice_flag_decode(lc);
2696 2010653 return !end_of_slice_flag;
2697 } else {
2698 11246989 return 1;
2699 }
2700 }
2701
2702 return 0;
2703 }
2704
2705 2010654 static void hls_decode_neighbour(HEVCLocalContext *lc,
2706 const HEVCLayerContext *l,
2707 const HEVCPPS *pps, const HEVCSPS *sps,
2708 int x_ctb, int y_ctb, int ctb_addr_ts)
2709 {
2710 2010654 const HEVCContext *const s = lc->parent;
2711 2010654 int ctb_size = 1 << sps->log2_ctb_size;
2712 2010654 int ctb_addr_rs = pps->ctb_addr_ts_to_rs[ctb_addr_ts];
2713 2010654 int ctb_addr_in_slice = ctb_addr_rs - s->sh.slice_addr;
2714
2715 2010654 l->tab_slice_address[ctb_addr_rs] = s->sh.slice_addr;
2716
2717
2/2
✓ Branch 0 taken 173854 times.
✓ Branch 1 taken 1836800 times.
2010654 if (pps->entropy_coding_sync_enabled_flag) {
2718
3/4
✓ Branch 0 taken 8173 times.
✓ Branch 1 taken 165681 times.
✓ Branch 2 taken 8173 times.
✗ Branch 3 not taken.
173854 if (x_ctb == 0 && (y_ctb & (ctb_size - 1)) == 0)
2719 8173 lc->first_qp_group = 1;
2720 173854 lc->end_of_tiles_x = sps->width;
2721
2/2
✓ Branch 0 taken 680802 times.
✓ Branch 1 taken 1155998 times.
1836800 } else if (pps->tiles_enabled_flag) {
2722
4/4
✓ Branch 0 taken 680048 times.
✓ Branch 1 taken 754 times.
✓ Branch 2 taken 6541 times.
✓ Branch 3 taken 673507 times.
680802 if (ctb_addr_ts && pps->tile_id[ctb_addr_ts] != pps->tile_id[ctb_addr_ts - 1]) {
2723 6541 int idxX = pps->col_idxX[x_ctb >> sps->log2_ctb_size];
2724 6541 lc->end_of_tiles_x = x_ctb + (pps->column_width[idxX] << sps->log2_ctb_size);
2725 6541 lc->first_qp_group = 1;
2726 }
2727 } else {
2728 1155998 lc->end_of_tiles_x = sps->width;
2729 }
2730
2731 2010654 lc->end_of_tiles_y = FFMIN(y_ctb + ctb_size, sps->height);
2732
2733 2010654 lc->boundary_flags = 0;
2734
2/2
✓ Branch 0 taken 680802 times.
✓ Branch 1 taken 1329852 times.
2010654 if (pps->tiles_enabled_flag) {
2735
4/4
✓ Branch 0 taken 662754 times.
✓ Branch 1 taken 18048 times.
✓ Branch 2 taken 42895 times.
✓ Branch 3 taken 619859 times.
680802 if (x_ctb > 0 && pps->tile_id[ctb_addr_ts] != pps->tile_id[pps->ctb_addr_rs_to_ts[ctb_addr_rs - 1]])
2736 42895 lc->boundary_flags |= BOUNDARY_LEFT_TILE;
2737
4/4
✓ Branch 0 taken 662754 times.
✓ Branch 1 taken 18048 times.
✓ Branch 2 taken 15883 times.
✓ Branch 3 taken 646871 times.
680802 if (x_ctb > 0 && l->tab_slice_address[ctb_addr_rs] != l->tab_slice_address[ctb_addr_rs - 1])
2738 15883 lc->boundary_flags |= BOUNDARY_LEFT_SLICE;
2739
4/4
✓ Branch 0 taken 658010 times.
✓ Branch 1 taken 22792 times.
✓ Branch 2 taken 33781 times.
✓ Branch 3 taken 624229 times.
680802 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]])
2740 33781 lc->boundary_flags |= BOUNDARY_UPPER_TILE;
2741
4/4
✓ Branch 0 taken 658010 times.
✓ Branch 1 taken 22792 times.
✓ Branch 2 taken 14490 times.
✓ Branch 3 taken 643520 times.
680802 if (y_ctb > 0 && l->tab_slice_address[ctb_addr_rs] != l->tab_slice_address[ctb_addr_rs - sps->ctb_width])
2742 14490 lc->boundary_flags |= BOUNDARY_UPPER_SLICE;
2743 } else {
2744
2/2
✓ Branch 0 taken 18559 times.
✓ Branch 1 taken 1311293 times.
1329852 if (ctb_addr_in_slice <= 0)
2745 18559 lc->boundary_flags |= BOUNDARY_LEFT_SLICE;
2746
2/2
✓ Branch 0 taken 200092 times.
✓ Branch 1 taken 1129760 times.
1329852 if (ctb_addr_in_slice < sps->ctb_width)
2747 200092 lc->boundary_flags |= BOUNDARY_UPPER_SLICE;
2748 }
2749
2750
6/6
✓ Branch 0 taken 1921315 times.
✓ Branch 1 taken 89339 times.
✓ Branch 2 taken 1913140 times.
✓ Branch 3 taken 8175 times.
✓ Branch 4 taken 1871527 times.
✓ Branch 5 taken 41613 times.
2010654 lc->ctb_left_flag = ((x_ctb > 0) && (ctb_addr_in_slice > 0) && !(lc->boundary_flags & BOUNDARY_LEFT_TILE));
2751
6/6
✓ Branch 0 taken 1868619 times.
✓ Branch 1 taken 142035 times.
✓ Branch 2 taken 1774109 times.
✓ Branch 3 taken 94510 times.
✓ Branch 4 taken 1753989 times.
✓ Branch 5 taken 20120 times.
2010654 lc->ctb_up_flag = ((y_ctb > 0) && (ctb_addr_in_slice >= sps->ctb_width) && !(lc->boundary_flags & BOUNDARY_UPPER_TILE));
2752
6/6
✓ Branch 0 taken 1868619 times.
✓ Branch 1 taken 142035 times.
✓ Branch 2 taken 1776707 times.
✓ Branch 3 taken 91912 times.
✓ Branch 4 taken 1703101 times.
✓ Branch 5 taken 73606 times.
2010654 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]]));
2753
8/8
✓ Branch 0 taken 1921315 times.
✓ Branch 1 taken 89339 times.
✓ Branch 2 taken 1789852 times.
✓ Branch 3 taken 131463 times.
✓ Branch 4 taken 1700175 times.
✓ Branch 5 taken 89677 times.
✓ Branch 6 taken 1644309 times.
✓ Branch 7 taken 55866 times.
2010654 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]]));
2754 2010654 }
2755
2756 28909 static int hls_decode_entry(HEVCContext *s, GetBitContext *gb)
2757 {
2758 28909 HEVCLocalContext *const lc = &s->local_ctx[0];
2759 28909 const HEVCLayerContext *const l = &s->layers[s->cur_layer];
2760 28909 const HEVCPPS *const pps = s->pps;
2761 28909 const HEVCSPS *const sps = pps->sps;
2762 28909 const uint8_t *slice_data = gb->buffer + s->sh.data_offset;
2763 28909 const size_t slice_size = get_bits_bytesize(gb, 1) - s->sh.data_offset;
2764 28909 int ctb_size = 1 << sps->log2_ctb_size;
2765 28909 int more_data = 1;
2766 28909 int x_ctb = 0;
2767 28909 int y_ctb = 0;
2768 28909 int ctb_addr_ts = pps->ctb_addr_rs_to_ts[s->sh.slice_ctb_addr_rs];
2769 int ret;
2770
2771
3/4
✓ Branch 0 taken 2010654 times.
✓ Branch 1 taken 28908 times.
✓ Branch 2 taken 2010654 times.
✗ Branch 3 not taken.
2039562 while (more_data && ctb_addr_ts < sps->ctb_size) {
2772 2010654 int ctb_addr_rs = pps->ctb_addr_ts_to_rs[ctb_addr_ts];
2773
2774 2010654 x_ctb = (ctb_addr_rs % ((sps->width + ctb_size - 1) >> sps->log2_ctb_size)) << sps->log2_ctb_size;
2775 2010654 y_ctb = (ctb_addr_rs / ((sps->width + ctb_size - 1) >> sps->log2_ctb_size)) << sps->log2_ctb_size;
2776 2010654 hls_decode_neighbour(lc, l, pps, sps, x_ctb, y_ctb, ctb_addr_ts);
2777
2778 2010654 ret = ff_hevc_cabac_init(lc, pps, ctb_addr_ts, slice_data, slice_size, 0);
2779
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2010654 times.
2010654 if (ret < 0) {
2780 l->tab_slice_address[ctb_addr_rs] = -1;
2781 return ret;
2782 }
2783
2784 2010654 hls_sao_param(lc, l, pps, sps,
2785 2010654 x_ctb >> sps->log2_ctb_size, y_ctb >> sps->log2_ctb_size);
2786
2787 2010654 l->deblock[ctb_addr_rs].beta_offset = s->sh.beta_offset;
2788 2010654 l->deblock[ctb_addr_rs].tc_offset = s->sh.tc_offset;
2789 2010654 l->filter_slice_edges[ctb_addr_rs] = s->sh.slice_loop_filter_across_slices_enabled_flag;
2790
2791 2010654 more_data = hls_coding_quadtree(lc, l, pps, sps, x_ctb, y_ctb, sps->log2_ctb_size, 0);
2792
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 2010653 times.
2010654 if (more_data < 0) {
2793 1 l->tab_slice_address[ctb_addr_rs] = -1;
2794 1 return more_data;
2795 }
2796
2797
2798 2010653 ctb_addr_ts++;
2799 2010653 ff_hevc_save_states(lc, pps, ctb_addr_ts);
2800 2010653 ff_hevc_hls_filters(lc, l, pps, x_ctb, y_ctb, ctb_size);
2801 }
2802
2803
2/2
✓ Branch 0 taken 15112 times.
✓ Branch 1 taken 13796 times.
28908 if (x_ctb + ctb_size >= sps->width &&
2804
2/2
✓ Branch 0 taken 10571 times.
✓ Branch 1 taken 4541 times.
15112 y_ctb + ctb_size >= sps->height)
2805 10571 ff_hevc_hls_filter(lc, l, pps, x_ctb, y_ctb, ctb_size);
2806
2807 28908 return ctb_addr_ts;
2808 }
2809
2810 static int hls_decode_entry_wpp(AVCodecContext *avctx, void *hevc_lclist,
2811 int job, int thread)
2812 {
2813 HEVCLocalContext *lc = &((HEVCLocalContext*)hevc_lclist)[thread];
2814 const HEVCContext *const s = lc->parent;
2815 const HEVCLayerContext *const l = &s->layers[s->cur_layer];
2816 const HEVCPPS *const pps = s->pps;
2817 const HEVCSPS *const sps = pps->sps;
2818 int ctb_size = 1 << sps->log2_ctb_size;
2819 int more_data = 1;
2820 int ctb_row = job;
2821 int ctb_addr_rs = s->sh.slice_ctb_addr_rs + ctb_row * ((sps->width + ctb_size - 1) >> sps->log2_ctb_size);
2822 int ctb_addr_ts = pps->ctb_addr_rs_to_ts[ctb_addr_rs];
2823
2824 const uint8_t *data = s->data + s->sh.offset[ctb_row];
2825 const size_t data_size = s->sh.size[ctb_row];
2826
2827 int progress = 0;
2828
2829 int ret;
2830
2831 if (ctb_row)
2832 ff_init_cabac_decoder(&lc->cc, data, data_size);
2833
2834 while(more_data && ctb_addr_ts < sps->ctb_size) {
2835 int x_ctb = (ctb_addr_rs % sps->ctb_width) << sps->log2_ctb_size;
2836 int y_ctb = (ctb_addr_rs / sps->ctb_width) << sps->log2_ctb_size;
2837
2838 hls_decode_neighbour(lc, l, pps, sps, x_ctb, y_ctb, ctb_addr_ts);
2839
2840 if (ctb_row)
2841 ff_thread_progress_await(&s->wpp_progress[ctb_row - 1],
2842 progress + SHIFT_CTB_WPP + 1);
2843
2844 /* atomic_load's prototype requires a pointer to non-const atomic variable
2845 * (due to implementations via mutexes, where reads involve writes).
2846 * Of course, casting const away here is nevertheless safe. */
2847 if (atomic_load((atomic_int*)&s->wpp_err)) {
2848 ff_thread_progress_report(&s->wpp_progress[ctb_row], INT_MAX);
2849 return 0;
2850 }
2851
2852 ret = ff_hevc_cabac_init(lc, pps, ctb_addr_ts, data, data_size, 1);
2853 if (ret < 0)
2854 goto error;
2855 hls_sao_param(lc, l, pps, sps,
2856 x_ctb >> sps->log2_ctb_size, y_ctb >> sps->log2_ctb_size);
2857
2858 l->deblock[ctb_addr_rs].beta_offset = s->sh.beta_offset;
2859 l->deblock[ctb_addr_rs].tc_offset = s->sh.tc_offset;
2860 l->filter_slice_edges[ctb_addr_rs] = s->sh.slice_loop_filter_across_slices_enabled_flag;
2861
2862 more_data = hls_coding_quadtree(lc, l, pps, sps, x_ctb, y_ctb, sps->log2_ctb_size, 0);
2863
2864 if (more_data < 0) {
2865 ret = more_data;
2866 goto error;
2867 }
2868
2869 ctb_addr_ts++;
2870
2871 ff_hevc_save_states(lc, pps, ctb_addr_ts);
2872 ff_thread_progress_report(&s->wpp_progress[ctb_row], ++progress);
2873 ff_hevc_hls_filters(lc, l, pps, x_ctb, y_ctb, ctb_size);
2874
2875 if (!more_data && (x_ctb+ctb_size) < sps->width && ctb_row != s->sh.num_entry_point_offsets) {
2876 /* Casting const away here is safe, because it is an atomic operation. */
2877 atomic_store((atomic_int*)&s->wpp_err, 1);
2878 ff_thread_progress_report(&s->wpp_progress[ctb_row], INT_MAX);
2879 return 0;
2880 }
2881
2882 if ((x_ctb+ctb_size) >= sps->width && (y_ctb+ctb_size) >= sps->height ) {
2883 ff_hevc_hls_filter(lc, l, pps, x_ctb, y_ctb, ctb_size);
2884 ff_thread_progress_report(&s->wpp_progress[ctb_row], INT_MAX);
2885 return ctb_addr_ts;
2886 }
2887 ctb_addr_rs = pps->ctb_addr_ts_to_rs[ctb_addr_ts];
2888 x_ctb+=ctb_size;
2889
2890 if(x_ctb >= sps->width) {
2891 break;
2892 }
2893 }
2894 ff_thread_progress_report(&s->wpp_progress[ctb_row], INT_MAX);
2895
2896 return 0;
2897 error:
2898 l->tab_slice_address[ctb_addr_rs] = -1;
2899 /* Casting const away here is safe, because it is an atomic operation. */
2900 atomic_store((atomic_int*)&s->wpp_err, 1);
2901 ff_thread_progress_report(&s->wpp_progress[ctb_row], INT_MAX);
2902 return ret;
2903 }
2904
2905 static int wpp_progress_init(HEVCContext *s, unsigned count)
2906 {
2907 if (s->nb_wpp_progress < count) {
2908 void *tmp = av_realloc_array(s->wpp_progress, count,
2909 sizeof(*s->wpp_progress));
2910 if (!tmp)
2911 return AVERROR(ENOMEM);
2912
2913 s->wpp_progress = tmp;
2914 memset(s->wpp_progress + s->nb_wpp_progress, 0,
2915 (count - s->nb_wpp_progress) * sizeof(*s->wpp_progress));
2916
2917 for (int i = s->nb_wpp_progress; i < count; i++) {
2918 int ret = ff_thread_progress_init(&s->wpp_progress[i], 1);
2919 if (ret < 0)
2920 return ret;
2921 s->nb_wpp_progress = i + 1;
2922 }
2923 }
2924
2925 for (int i = 0; i < count; i++)
2926 ff_thread_progress_reset(&s->wpp_progress[i]);
2927
2928 return 0;
2929 }
2930
2931 static int hls_slice_data_wpp(HEVCContext *s, const H2645NAL *nal)
2932 {
2933 const HEVCPPS *const pps = s->pps;
2934 const HEVCSPS *const sps = pps->sps;
2935 const uint8_t *data = nal->data;
2936 int length = nal->size;
2937 int *ret;
2938 int64_t offset;
2939 int64_t startheader, cmpt = 0;
2940 int j, res = 0;
2941
2942 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) {
2943 av_log(s->avctx, AV_LOG_ERROR, "WPP ctb addresses are wrong (%d %d %d %d)\n",
2944 s->sh.slice_ctb_addr_rs, s->sh.num_entry_point_offsets,
2945 sps->ctb_width, sps->ctb_height
2946 );
2947 return AVERROR_INVALIDDATA;
2948 }
2949
2950 if (s->avctx->thread_count > s->nb_local_ctx) {
2951 HEVCLocalContext *tmp = av_malloc_array(s->avctx->thread_count, sizeof(*s->local_ctx));
2952
2953 if (!tmp)
2954 return AVERROR(ENOMEM);
2955
2956 memcpy(tmp, s->local_ctx, sizeof(*s->local_ctx) * s->nb_local_ctx);
2957 av_free(s->local_ctx);
2958 s->local_ctx = tmp;
2959
2960 for (unsigned i = s->nb_local_ctx; i < s->avctx->thread_count; i++) {
2961 tmp = &s->local_ctx[i];
2962
2963 memset(tmp, 0, sizeof(*tmp));
2964
2965 tmp->logctx = s->avctx;
2966 tmp->parent = s;
2967 tmp->common_cabac_state = &s->cabac;
2968 }
2969
2970 s->nb_local_ctx = s->avctx->thread_count;
2971 }
2972
2973 offset = s->sh.data_offset;
2974
2975 for (j = 0, cmpt = 0, startheader = offset + s->sh.entry_point_offset[0]; j < nal->skipped_bytes; j++) {
2976 if (nal->skipped_bytes_pos[j] >= offset && nal->skipped_bytes_pos[j] < startheader) {
2977 startheader--;
2978 cmpt++;
2979 }
2980 }
2981
2982 for (int i = 1; i < s->sh.num_entry_point_offsets; i++) {
2983 offset += (s->sh.entry_point_offset[i - 1] - cmpt);
2984 for (j = 0, cmpt = 0, startheader = offset
2985 + s->sh.entry_point_offset[i]; j < nal->skipped_bytes; j++) {
2986 if (nal->skipped_bytes_pos[j] >= offset && nal->skipped_bytes_pos[j] < startheader) {
2987 startheader--;
2988 cmpt++;
2989 }
2990 }
2991 s->sh.size[i] = s->sh.entry_point_offset[i] - cmpt;
2992 s->sh.offset[i] = offset;
2993
2994 }
2995
2996 offset += s->sh.entry_point_offset[s->sh.num_entry_point_offsets - 1] - cmpt;
2997 if (length < offset) {
2998 av_log(s->avctx, AV_LOG_ERROR, "entry_point_offset table is corrupted\n");
2999 return AVERROR_INVALIDDATA;
3000 }
3001 s->sh.size [s->sh.num_entry_point_offsets] = length - offset;
3002 s->sh.offset[s->sh.num_entry_point_offsets] = offset;
3003
3004 s->sh.offset[0] = s->sh.data_offset;
3005 s->sh.size[0] = s->sh.offset[1] - s->sh.offset[0];
3006
3007 s->data = data;
3008
3009 for (unsigned i = 1; i < s->nb_local_ctx; i++) {
3010 s->local_ctx[i].first_qp_group = 1;
3011 s->local_ctx[i].qp_y = s->local_ctx[0].qp_y;
3012 }
3013
3014 atomic_store(&s->wpp_err, 0);
3015 res = wpp_progress_init(s, s->sh.num_entry_point_offsets + 1);
3016 if (res < 0)
3017 return res;
3018
3019 ret = av_calloc(s->sh.num_entry_point_offsets + 1, sizeof(*ret));
3020 if (!ret)
3021 return AVERROR(ENOMEM);
3022
3023 if (pps->entropy_coding_sync_enabled_flag)
3024 s->avctx->execute2(s->avctx, hls_decode_entry_wpp, s->local_ctx, ret, s->sh.num_entry_point_offsets + 1);
3025
3026 for (int i = 0; i <= s->sh.num_entry_point_offsets; i++)
3027 res += ret[i];
3028
3029 av_free(ret);
3030 return res;
3031 }
3032
3033 28909 static int decode_slice_data(HEVCContext *s, const HEVCLayerContext *l,
3034 const H2645NAL *nal, GetBitContext *gb)
3035 {
3036 28909 const HEVCPPS *pps = s->pps;
3037 int ret;
3038
3039
2/2
✓ Branch 0 taken 18337 times.
✓ Branch 1 taken 10572 times.
28909 if (!s->sh.first_slice_in_pic_flag)
3040 18337 s->slice_idx += !s->sh.dependent_slice_segment_flag;
3041
3042
4/4
✓ Branch 0 taken 20920 times.
✓ Branch 1 taken 7989 times.
✓ Branch 2 taken 19571 times.
✓ Branch 3 taken 1349 times.
28909 if (!s->sh.dependent_slice_segment_flag && s->sh.slice_type != HEVC_SLICE_I) {
3043 19571 ret = ff_hevc_slice_rpl(s);
3044
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 19571 times.
19571 if (ret < 0) {
3045 av_log(s->avctx, AV_LOG_WARNING,
3046 "Error constructing the reference lists for the current slice.\n");
3047 return ret;
3048 }
3049 }
3050
3051 28909 s->slice_initialized = 1;
3052
3053
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 28909 times.
28909 if (s->avctx->hwaccel)
3054 return FF_HW_CALL(s->avctx, decode_slice, nal->raw_data, nal->raw_size);
3055
3056
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 28909 times.
28909 if (s->avctx->profile == AV_PROFILE_HEVC_SCC) {
3057 av_log(s->avctx, AV_LOG_ERROR,
3058 "SCC profile is not yet implemented in hevc native decoder.\n");
3059 return AVERROR_PATCHWELCOME;
3060 }
3061
3062
2/2
✓ Branch 0 taken 7989 times.
✓ Branch 1 taken 20920 times.
28909 if (s->sh.dependent_slice_segment_flag) {
3063 7989 int ctb_addr_ts = pps->ctb_addr_rs_to_ts[s->sh.slice_ctb_addr_rs];
3064 7989 int prev_rs = pps->ctb_addr_ts_to_rs[ctb_addr_ts - 1];
3065
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7989 times.
7989 if (l->tab_slice_address[prev_rs] != s->sh.slice_addr) {
3066 av_log(s->avctx, AV_LOG_ERROR, "Previous slice segment missing\n");
3067 return AVERROR_INVALIDDATA;
3068 }
3069 }
3070
3071 28909 s->local_ctx[0].first_qp_group = !s->sh.dependent_slice_segment_flag;
3072
3073
2/2
✓ Branch 0 taken 23298 times.
✓ Branch 1 taken 5611 times.
28909 if (!pps->cu_qp_delta_enabled_flag)
3074 23298 s->local_ctx[0].qp_y = s->sh.slice_qp;
3075
3076 28909 s->local_ctx[0].tu.cu_qp_offset_cb = 0;
3077 28909 s->local_ctx[0].tu.cu_qp_offset_cr = 0;
3078
3079
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 28909 times.
28909 if (s->avctx->active_thread_type == FF_THREAD_SLICE &&
3080 s->sh.num_entry_point_offsets > 0 &&
3081 pps->num_tile_rows == 1 && pps->num_tile_columns == 1)
3082 return hls_slice_data_wpp(s, nal);
3083
3084 28909 return hls_decode_entry(s, gb);
3085 }
3086
3087 10572 static int set_side_data(HEVCContext *s)
3088 {
3089 10572 const HEVCSPS *sps = s->cur_frame->pps->sps;
3090 10572 AVFrame *out = s->cur_frame->f;
3091 int ret;
3092
3093 // Decrement the mastering display and content light level flag when IRAP
3094 // frame has no_rasl_output_flag=1 so the side data persists for the entire
3095 // coded video sequence.
3096
5/6
✓ Branch 0 taken 766 times.
✓ Branch 1 taken 9806 times.
✓ Branch 2 taken 766 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 482 times.
✓ Branch 5 taken 284 times.
10572 if (IS_IRAP(s) && s->no_rasl_output_flag) {
3097
2/2
✓ Branch 0 taken 20 times.
✓ Branch 1 taken 462 times.
482 if (s->sei.common.mastering_display.present > 0)
3098 20 s->sei.common.mastering_display.present--;
3099
3100
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 474 times.
482 if (s->sei.common.content_light.present > 0)
3101 8 s->sei.common.content_light.present--;
3102 }
3103
3104 10572 ret = ff_h2645_sei_to_frame(out, &s->sei.common, AV_CODEC_ID_HEVC, s->avctx,
3105 &sps->vui.common,
3106 10572 sps->bit_depth, sps->bit_depth_chroma,
3107 10572 s->cur_frame->poc /* no poc_offset in HEVC */);
3108
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10572 times.
10572 if (ret < 0)
3109 return ret;
3110
3111
2/2
✓ Branch 0 taken 23 times.
✓ Branch 1 taken 10549 times.
10572 if (s->sei.timecode.present) {
3112 uint32_t *tc_sd;
3113 char tcbuf[AV_TIMECODE_STR_SIZE];
3114 AVFrameSideData *tcside;
3115 23 ret = ff_frame_new_side_data(s->avctx, out, AV_FRAME_DATA_S12M_TIMECODE,
3116 sizeof(uint32_t) * 4, &tcside);
3117
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 23 times.
23 if (ret < 0)
3118 return ret;
3119
3120
1/2
✓ Branch 0 taken 23 times.
✗ Branch 1 not taken.
23 if (tcside) {
3121 23 tc_sd = (uint32_t*)tcside->data;
3122 23 tc_sd[0] = s->sei.timecode.num_clock_ts;
3123
3124
2/2
✓ Branch 0 taken 23 times.
✓ Branch 1 taken 23 times.
46 for (int i = 0; i < tc_sd[0]; i++) {
3125 23 int drop = s->sei.timecode.cnt_dropped_flag[i];
3126 23 int hh = s->sei.timecode.hours_value[i];
3127 23 int mm = s->sei.timecode.minutes_value[i];
3128 23 int ss = s->sei.timecode.seconds_value[i];
3129 23 int ff = s->sei.timecode.n_frames[i];
3130
3131 23 tc_sd[i + 1] = av_timecode_get_smpte(s->avctx->framerate, drop, hh, mm, ss, ff);
3132 23 av_timecode_make_smpte_tc_string2(tcbuf, s->avctx->framerate, tc_sd[i + 1], 0, 0);
3133 23 av_dict_set(&out->metadata, "timecode", tcbuf, 0);
3134 }
3135 }
3136
3137 23 s->sei.timecode.num_clock_ts = 0;
3138 }
3139
3140
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 10566 times.
10572 if (s->sei.common.itut_t35.hdr_plus) {
3141 6 AVBufferRef *info_ref = av_buffer_ref(s->sei.common.itut_t35.hdr_plus);
3142
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (!info_ref)
3143 return AVERROR(ENOMEM);
3144
3145 6 ret = ff_frame_new_side_data_from_buf(s->avctx, out, AV_FRAME_DATA_DYNAMIC_HDR_PLUS, &info_ref);
3146
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (ret < 0)
3147 return ret;
3148 }
3149
3150
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10572 times.
10572 if (s->sei.common.itut_t35.hdr_smpte2094_app5) {
3151 AVBufferRef *info_ref = av_buffer_ref(s->sei.common.itut_t35.hdr_smpte2094_app5);
3152 if (!info_ref)
3153 return AVERROR(ENOMEM);
3154
3155 ret = ff_frame_new_side_data_from_buf(s->avctx, out, AV_FRAME_DATA_DYNAMIC_HDR_SMPTE_2094_APP5, &info_ref);
3156 if (ret < 0)
3157 return ret;
3158 }
3159
3160
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 10570 times.
10572 if (s->rpu_buf) {
3161 2 AVFrameSideData *rpu = av_frame_new_side_data_from_buf(out, AV_FRAME_DATA_DOVI_RPU_BUFFER, s->rpu_buf);
3162
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (!rpu)
3163 return AVERROR(ENOMEM);
3164
3165 2 s->rpu_buf = NULL;
3166 }
3167
3168
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 10572 times.
10572 if ((ret = ff_dovi_attach_side_data(&s->dovi_ctx, out)) < 0)
3169 return ret;
3170
3171
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 10570 times.
10572 if (s->sei.common.itut_t35.hdr_vivid) {
3172
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 if (!av_frame_side_data_add(&out->side_data, &out->nb_side_data,
3173 AV_FRAME_DATA_DYNAMIC_HDR_VIVID,
3174 &s->sei.common.itut_t35.hdr_vivid,
3175 AV_FRAME_SIDE_DATA_FLAG_NEW_REF))
3176 return AVERROR(ENOMEM);
3177 }
3178
3179 10572 return 0;
3180 }
3181
3182 10182 static int find_finish_setup_nal(const HEVCContext *s)
3183 {
3184 10182 int nal_idx = 0;
3185
3186
2/2
✓ Branch 0 taken 39679 times.
✓ Branch 1 taken 10182 times.
49861 for (int i = nal_idx; i < s->pkt.nb_nals; i++) {
3187 39679 const H2645NAL *nal = &s->pkt.nals[i];
3188 39679 const int layer_id = nal->nuh_layer_id;
3189 39679 GetBitContext gb = nal->gb;
3190
3191
2/4
✓ Branch 0 taken 39679 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 39679 times.
✗ Branch 3 not taken.
39679 if (layer_id > HEVC_MAX_NUH_LAYER_ID || s->vps->layer_idx[layer_id] < 0 ||
3192
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 39663 times.
39679 !(s->layers_active_decode & (1 << s->vps->layer_idx[layer_id])))
3193 18353 continue;
3194
3195
3/3
✓ Branch 0 taken 28910 times.
✓ Branch 1 taken 2331 times.
✓ Branch 2 taken 8422 times.
39663 switch (nal->type) {
3196 28910 case HEVC_NAL_TRAIL_R:
3197 case HEVC_NAL_TRAIL_N:
3198 case HEVC_NAL_TSA_N:
3199 case HEVC_NAL_TSA_R:
3200 case HEVC_NAL_STSA_N:
3201 case HEVC_NAL_STSA_R:
3202 case HEVC_NAL_BLA_W_LP:
3203 case HEVC_NAL_BLA_W_RADL:
3204 case HEVC_NAL_BLA_N_LP:
3205 case HEVC_NAL_IDR_W_RADL:
3206 case HEVC_NAL_IDR_N_LP:
3207 case HEVC_NAL_CRA_NUT:
3208 case HEVC_NAL_RADL_N:
3209 case HEVC_NAL_RADL_R:
3210 case HEVC_NAL_RASL_N:
3211 case HEVC_NAL_RASL_R:
3212
2/2
✓ Branch 1 taken 18337 times.
✓ Branch 2 taken 10573 times.
28910 if (!get_bits1(&gb)) // first_slice_segment_in_pic_flag
3213 18337 continue;
3214 av_fallthrough;
3215 case HEVC_NAL_VPS:
3216 case HEVC_NAL_SPS:
3217 case HEVC_NAL_PPS:
3218 12904 nal_idx = i;
3219 12904 break;
3220 }
3221 }
3222
3223 10182 return nal_idx;
3224 }
3225
3226 10572 static int hevc_frame_start(HEVCContext *s, HEVCLayerContext *l,
3227 unsigned nal_idx)
3228 {
3229 10572 const HEVCPPS *const pps = s->ps.pps_list[s->sh.pps_id];
3230 10572 const HEVCSPS *const sps = pps->sps;
3231 10572 int pic_size_in_ctb = ((sps->width >> sps->log2_min_cb_size) + 1) *
3232 10572 ((sps->height >> sps->log2_min_cb_size) + 1);
3233
2/2
✓ Branch 0 taken 10182 times.
✓ Branch 1 taken 390 times.
20754 int new_sequence = (l == &s->layers[0]) &&
3234
12/12
✓ Branch 0 taken 9774 times.
✓ Branch 1 taken 408 times.
✓ Branch 2 taken 9734 times.
✓ Branch 3 taken 40 times.
✓ Branch 4 taken 9732 times.
✓ Branch 5 taken 2 times.
✓ Branch 6 taken 9725 times.
✓ Branch 7 taken 7 times.
✓ Branch 8 taken 9724 times.
✓ Branch 9 taken 1 times.
✓ Branch 10 taken 19 times.
✓ Branch 11 taken 9705 times.
10182 (IS_IDR(s) || IS_BLA(s) || s->last_eos);
3235 10572 int prev_layers_active_decode = s->layers_active_decode;
3236 10572 int prev_layers_active_output = s->layers_active_output;
3237 int ret;
3238
3239
3/4
✓ Branch 0 taken 449 times.
✓ Branch 1 taken 10123 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 449 times.
10572 if (sps->vps != s->vps && l != &s->layers[0]) {
3240 av_log(s->avctx, AV_LOG_ERROR, "VPS changed in a non-base layer\n");
3241 set_sps(s, l, NULL);
3242 return AVERROR_INVALIDDATA;
3243 }
3244
3245 10572 av_refstruct_replace(&s->pps, pps);
3246
2/2
✓ Branch 0 taken 465 times.
✓ Branch 1 taken 10107 times.
10572 if (l->sps != sps) {
3247 465 const HEVCSPS *sps_base = s->layers[0].sps;
3248 465 enum AVPixelFormat pix_fmt = sps->pix_fmt;
3249
3250
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 455 times.
465 if (l != &s->layers[0]) {
3251
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10 times.
10 if (!sps_base) {
3252 av_log(s->avctx, AV_LOG_ERROR,
3253 "Access unit starts with a non-base layer frame\n");
3254 return AVERROR_INVALIDDATA;
3255 }
3256
3257 // Files produced by Vision Pro lack VPS extension VUI,
3258 // so the secondary layer has no range information.
3259 // This check avoids failing in such a case.
3260
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10 times.
10 if (sps_base->pix_fmt == AV_PIX_FMT_YUVJ420P &&
3261 sps->pix_fmt == AV_PIX_FMT_YUV420P &&
3262 !sps->vui.common.video_signal_type_present_flag)
3263 pix_fmt = sps_base->pix_fmt;
3264
3265 // Ignore range mismatch between base layer and alpha layer
3266
2/2
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 9 times.
10 if (ff_hevc_is_alpha_video(s) &&
3267
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 &&
3268 pix_fmt == AV_PIX_FMT_YUVJ420P)
3269 1 pix_fmt = sps_base->pix_fmt;
3270
3271
1/2
✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
10 if (pix_fmt != sps_base->pix_fmt ||
3272
1/2
✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
10 sps->width != sps_base->width ||
3273
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10 times.
10 sps->height != sps_base->height) {
3274 av_log(s->avctx, AV_LOG_ERROR,
3275 "Base/non-base layer SPS have unsupported parameter combination\n");
3276 return AVERROR(ENOSYS);
3277 }
3278 }
3279
3280 465 ff_hevc_clear_refs(l);
3281
3282 465 ret = set_sps(s, l, sps);
3283
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 465 times.
465 if (ret < 0)
3284 return ret;
3285
3286
2/2
✓ Branch 0 taken 455 times.
✓ Branch 1 taken 10 times.
465 if (l == &s->layers[0]) {
3287 455 export_stream_params(s, sps);
3288
3289 455 ret = get_format(s, sps);
3290
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 455 times.
455 if (ret < 0) {
3291 set_sps(s, l, NULL);
3292 return ret;
3293 }
3294
3295 455 new_sequence = 1;
3296 }
3297 }
3298
3299 10572 memset(l->horizontal_bs, 0, l->bs_width * l->bs_height);
3300 10572 memset(l->vertical_bs, 0, l->bs_width * l->bs_height);
3301 10572 memset(l->cbf_luma, 0, sps->min_tb_width * sps->min_tb_height);
3302 10572 memset(l->is_pcm, 0, (sps->min_pu_width + 1) * (sps->min_pu_height + 1));
3303 10572 memset(l->tab_slice_address, -1, pic_size_in_ctb * sizeof(*l->tab_slice_address));
3304
3305
4/4
✓ Branch 0 taken 10155 times.
✓ Branch 1 taken 417 times.
✓ Branch 2 taken 45 times.
✓ Branch 3 taken 10110 times.
10572 if (IS_IDR(s))
3306 462 ff_hevc_clear_refs(l);
3307
3308 10572 s->slice_idx = 0;
3309 10572 s->first_nal_type = s->nal_unit_type;
3310 10572 s->poc = s->sh.poc;
3311
3312
3/4
✓ Branch 0 taken 766 times.
✓ Branch 1 taken 9806 times.
✓ Branch 2 taken 766 times.
✗ Branch 3 not taken.
10572 if (IS_IRAP(s)) {
3313
10/10
✓ Branch 0 taken 349 times.
✓ Branch 1 taken 417 times.
✓ Branch 2 taken 304 times.
✓ Branch 3 taken 45 times.
✓ Branch 4 taken 302 times.
✓ Branch 5 taken 2 times.
✓ Branch 6 taken 295 times.
✓ Branch 7 taken 7 times.
✓ Branch 8 taken 294 times.
✓ Branch 9 taken 1 times.
1060 s->no_rasl_output_flag = IS_IDR(s) || IS_BLA(s) ||
3314
3/4
✓ Branch 0 taken 294 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 10 times.
✓ Branch 3 taken 284 times.
294 (s->nal_unit_type == HEVC_NAL_CRA_NUT && s->last_eos);
3315 766 s->recovery_poc = HEVC_RECOVERY_END;
3316 }
3317
3318
2/2
✓ Branch 0 taken 20 times.
✓ Branch 1 taken 10552 times.
10572 if (s->recovery_poc != HEVC_RECOVERY_END &&
3319
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 20 times.
20 s->sei.recovery_point.has_recovery_poc) {
3320 if (s->recovery_poc == HEVC_RECOVERY_UNSPECIFIED)
3321 s->recovery_poc = s->poc + s->sei.recovery_point.recovery_poc_cnt;
3322 else if (s->poc >= s->recovery_poc)
3323 s->recovery_poc = HEVC_RECOVERY_END;
3324 }
3325
3326 /* 8.3.1 */
3327
2/2
✓ Branch 0 taken 10230 times.
✓ Branch 1 taken 342 times.
10572 if (s->temporal_id == 0 &&
3328
2/2
✓ Branch 0 taken 7493 times.
✓ Branch 1 taken 2737 times.
10230 s->nal_unit_type != HEVC_NAL_TRAIL_N &&
3329
1/2
✓ Branch 0 taken 7493 times.
✗ Branch 1 not taken.
7493 s->nal_unit_type != HEVC_NAL_TSA_N &&
3330
1/2
✓ Branch 0 taken 7493 times.
✗ Branch 1 not taken.
7493 s->nal_unit_type != HEVC_NAL_STSA_N &&
3331
2/2
✓ Branch 0 taken 7467 times.
✓ Branch 1 taken 26 times.
7493 s->nal_unit_type != HEVC_NAL_RADL_N &&
3332
2/2
✓ Branch 0 taken 7445 times.
✓ Branch 1 taken 22 times.
7467 s->nal_unit_type != HEVC_NAL_RADL_R &&
3333
2/2
✓ Branch 0 taken 7075 times.
✓ Branch 1 taken 370 times.
7445 s->nal_unit_type != HEVC_NAL_RASL_N &&
3334
2/2
✓ Branch 0 taken 6601 times.
✓ Branch 1 taken 474 times.
7075 s->nal_unit_type != HEVC_NAL_RASL_R)
3335 6601 s->poc_tid0 = s->poc;
3336
3337
2/2
✓ Branch 0 taken 754 times.
✓ Branch 1 taken 9818 times.
10572 if (pps->tiles_enabled_flag)
3338 754 s->local_ctx[0].end_of_tiles_x = pps->column_width[0] << sps->log2_ctb_size;
3339
3340
2/2
✓ Branch 0 taken 480 times.
✓ Branch 1 taken 10092 times.
10572 if (new_sequence) {
3341 480 ret = ff_hevc_output_frames(s, prev_layers_active_decode, prev_layers_active_output,
3342 480 0, 0, s->sh.no_output_of_prior_pics_flag);
3343
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 480 times.
480 if (ret < 0)
3344 return ret;
3345 }
3346
3347 10572 ret = export_stream_params_from_sei(s);
3348
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10572 times.
10572 if (ret < 0)
3349 return ret;
3350
3351 10572 ret = ff_hevc_set_new_ref(s, l, s->poc);
3352
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10572 times.
10572 if (ret < 0)
3353 goto fail;
3354
3355 10572 ret = ff_hevc_frame_rps(s, l);
3356
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10572 times.
10572 if (ret < 0) {
3357 av_log(s->avctx, AV_LOG_ERROR, "Error constructing the frame RPS.\n");
3358 goto fail;
3359 }
3360
3361
3/4
✓ Branch 0 taken 766 times.
✓ Branch 1 taken 9806 times.
✓ Branch 2 taken 766 times.
✗ Branch 3 not taken.
10572 if (IS_IRAP(s))
3362 766 s->cur_frame->f->flags |= AV_FRAME_FLAG_KEY;
3363 else
3364 9806 s->cur_frame->f->flags &= ~AV_FRAME_FLAG_KEY;
3365
3366 21144 s->cur_frame->needs_fg = ((s->sei.common.film_grain_characteristics &&
3367 s->sei.common.film_grain_characteristics->present) ||
3368
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10572 times.
10572 s->sei.common.itut_t35.aom_film_grain.enable) &&
3369
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 10572 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
21144 !(s->avctx->export_side_data & AV_CODEC_EXPORT_DATA_FILM_GRAIN) &&
3370 !s->avctx->hwaccel;
3371
3372 10572 ret = set_side_data(s);
3373
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10572 times.
10572 if (ret < 0)
3374 goto fail;
3375
3376
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10572 times.
10572 if (s->cur_frame->needs_fg &&
3377 (s->sei.common.film_grain_characteristics && s->sei.common.film_grain_characteristics->present &&
3378 !ff_h274_film_grain_params_supported(s->sei.common.film_grain_characteristics->model_id,
3379 s->cur_frame->f->format) ||
3380 !av_film_grain_params_select(s->cur_frame->f))) {
3381 av_log_once(s->avctx, AV_LOG_WARNING, AV_LOG_DEBUG, &s->film_grain_warning_shown,
3382 "Unsupported film grain parameters. Ignoring film grain.\n");
3383 s->cur_frame->needs_fg = 0;
3384 }
3385
3386
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10572 times.
10572 if (s->cur_frame->needs_fg) {
3387 s->cur_frame->frame_grain->format = s->cur_frame->f->format;
3388 s->cur_frame->frame_grain->width = s->cur_frame->f->width;
3389 s->cur_frame->frame_grain->height = s->cur_frame->f->height;
3390 if ((ret = ff_thread_get_buffer(s->avctx, s->cur_frame->frame_grain, 0)) < 0)
3391 goto fail;
3392
3393 ret = av_frame_copy_props(s->cur_frame->frame_grain, s->cur_frame->f);
3394 if (ret < 0)
3395 goto fail;
3396 }
3397
3398 10572 s->cur_frame->f->pict_type = 3 - s->sh.slice_type;
3399
3400 10572 ret = ff_hevc_output_frames(s, s->layers_active_decode, s->layers_active_output,
3401 10572 sps->temporal_layer[sps->max_sub_layers - 1].num_reorder_pics,
3402 10572 sps->temporal_layer[sps->max_sub_layers - 1].max_dec_pic_buffering, 0);
3403
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10572 times.
10572 if (ret < 0)
3404 goto fail;
3405
3406
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10572 times.
10572 if (s->avctx->hwaccel) {
3407 AVCodecInternal *avci = s->avctx->internal;
3408 AVPacket *avpkt = avci->in_pkt;
3409 ret = FF_HW_CALL(s->avctx, start_frame,
3410 avpkt->buf, NULL, 0);
3411 if (ret < 0)
3412 goto fail;
3413 }
3414
3415 // after starting the base-layer frame we know which layers will be decoded,
3416 // so we can now figure out which NALUs to wait for before we can call
3417 // ff_thread_finish_setup()
3418
2/2
✓ Branch 0 taken 10182 times.
✓ Branch 1 taken 390 times.
10572 if (l == &s->layers[0])
3419 10182 s->finish_setup_nal_idx = find_finish_setup_nal(s);
3420
3421
2/2
✓ Branch 0 taken 10181 times.
✓ Branch 1 taken 391 times.
10572 if (nal_idx >= s->finish_setup_nal_idx)
3422 10181 ff_thread_finish_setup(s->avctx);
3423
3424 10572 return 0;
3425
3426 fail:
3427 if (l->cur_frame)
3428 ff_hevc_unref_frame(l->cur_frame, ~0);
3429 l->cur_frame = NULL;
3430 s->cur_frame = s->collocated_ref = NULL;
3431 return ret;
3432 }
3433
3434 static int verify_md5(HEVCContext *s, AVFrame *frame)
3435 {
3436 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(frame->format);
3437 char msg_buf[4 * (50 + 2 * 2 * 16 /* MD5-size */)];
3438 int pixel_shift;
3439 int err = 0;
3440 int i, j;
3441
3442 if (!desc)
3443 return AVERROR(EINVAL);
3444
3445 pixel_shift = desc->comp[0].depth > 8;
3446
3447 /* the checksums are LE, so we have to byteswap for >8bpp formats
3448 * on BE arches */
3449 #if HAVE_BIGENDIAN
3450 if (pixel_shift && !s->checksum_buf) {
3451 av_fast_malloc(&s->checksum_buf, &s->checksum_buf_size,
3452 FFMAX3(frame->linesize[0], frame->linesize[1],
3453 frame->linesize[2]));
3454 if (!s->checksum_buf)
3455 return AVERROR(ENOMEM);
3456 }
3457 #endif
3458
3459 msg_buf[0] = '\0';
3460 for (i = 0; frame->data[i]; i++) {
3461 int width = s->avctx->coded_width;
3462 int height = s->avctx->coded_height;
3463 int w = (i == 1 || i == 2) ? (width >> desc->log2_chroma_w) : width;
3464 int h = (i == 1 || i == 2) ? (height >> desc->log2_chroma_h) : height;
3465 uint8_t md5[16];
3466
3467 av_md5_init(s->md5_ctx);
3468 for (j = 0; j < h; j++) {
3469 const uint8_t *src = frame->data[i] + j * frame->linesize[i];
3470 #if HAVE_BIGENDIAN
3471 if (pixel_shift) {
3472 s->bdsp.bswap16_buf((uint16_t *) s->checksum_buf,
3473 (const uint16_t *) src, w);
3474 src = s->checksum_buf;
3475 }
3476 #endif
3477 av_md5_update(s->md5_ctx, src, w << pixel_shift);
3478 }
3479 av_md5_final(s->md5_ctx, md5);
3480
3481 #define MD5_PRI "%016" PRIx64 "%016" PRIx64
3482 #define MD5_PRI_ARG(buf) AV_RB64(buf), AV_RB64((const uint8_t*)(buf) + 8)
3483
3484 if (!memcmp(md5, s->sei.picture_hash.md5[i], 16)) {
3485 av_strlcatf(msg_buf, sizeof(msg_buf),
3486 "plane %d - correct " MD5_PRI "; ",
3487 i, MD5_PRI_ARG(md5));
3488 } else {
3489 av_strlcatf(msg_buf, sizeof(msg_buf),
3490 "mismatching checksum of plane %d - " MD5_PRI " != " MD5_PRI "; ",
3491 i, MD5_PRI_ARG(md5), MD5_PRI_ARG(s->sei.picture_hash.md5[i]));
3492 err = AVERROR_INVALIDDATA;
3493 }
3494 }
3495
3496 av_log(s->avctx, err < 0 ? AV_LOG_ERROR : AV_LOG_DEBUG,
3497 "Verifying checksum for frame with POC %d: %s\n",
3498 s->poc, msg_buf);
3499
3500 return err;
3501 }
3502
3503 10572 static int hevc_frame_end(HEVCContext *s, HEVCLayerContext *l)
3504 {
3505 10572 HEVCFrame *out = l->cur_frame;
3506 const AVFilmGrainParams *fgp;
3507 av_unused int ret;
3508
3509
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10572 times.
10572 if (out->needs_fg) {
3510 av_assert0(out->frame_grain->buf[0]);
3511 fgp = av_film_grain_params_select(out->f);
3512 switch (fgp->type) {
3513 case AV_FILM_GRAIN_PARAMS_NONE:
3514 av_assert0(0);
3515 return AVERROR_BUG;
3516 case AV_FILM_GRAIN_PARAMS_H274:
3517 ret = ff_h274_apply_film_grain(out->frame_grain, out->f, fgp);
3518 break;
3519 case AV_FILM_GRAIN_PARAMS_AV1:
3520 ret = ff_aom_apply_film_grain(out->frame_grain, out->f, fgp);
3521 break;
3522 }
3523 av_assert1(ret >= 0);
3524 }
3525
3526
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10572 times.
10572 if (s->avctx->hwaccel) {
3527 ret = FF_HW_SIMPLE_CALL(s->avctx, end_frame);
3528 if (ret < 0) {
3529 av_log(s->avctx, AV_LOG_ERROR,
3530 "hardware accelerator failed to decode picture\n");
3531 return ret;
3532 }
3533 } else {
3534
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10572 times.
10572 if (s->avctx->err_recognition & AV_EF_CRCCHECK &&
3535 s->sei.picture_hash.is_md5) {
3536 ret = verify_md5(s, out->f);
3537 if (ret < 0 && s->avctx->err_recognition & AV_EF_EXPLODE)
3538 return ret;
3539 }
3540 }
3541 10572 s->sei.picture_hash.is_md5 = 0;
3542
3543 10572 av_log(s->avctx, AV_LOG_DEBUG, "Decoded frame with POC %zu/%d.\n",
3544 10572 l - s->layers, s->poc);
3545
3546 10572 return 0;
3547 }
3548
3549 29504 static int decode_slice(HEVCContext *s, unsigned nal_idx, GetBitContext *gb)
3550 {
3551
2/2
✓ Branch 0 taken 29028 times.
✓ Branch 1 taken 476 times.
29504 const int layer_idx = s->vps ? s->vps->layer_idx[s->nuh_layer_id] : 0;
3552 HEVCLayerContext *l;
3553 int ret;
3554
3555 // skip layers not requested to be decoded
3556 // layers_active_decode can only change while decoding a base-layer frame,
3557 // so we can check it for non-base layers
3558
1/2
✓ Branch 0 taken 29504 times.
✗ Branch 1 not taken.
29504 if (layer_idx < 0 ||
3559
4/4
✓ Branch 0 taken 521 times.
✓ Branch 1 taken 28983 times.
✓ Branch 2 taken 5 times.
✓ Branch 3 taken 516 times.
29504 (s->nuh_layer_id > 0 && !(s->layers_active_decode & (1 << layer_idx))))
3560 5 return 0;
3561
3562 // the first slice of this picture was skipped, so drop the remaining
3563 // slices before parsing, the context is stale for them
3564
4/4
✓ Branch 0 taken 553 times.
✓ Branch 1 taken 28946 times.
✓ Branch 3 taken 486 times.
✓ Branch 4 taken 67 times.
29499 if (s->skipping_frame && !show_bits1(gb))
3565 486 return 0;
3566
3567 29013 ret = hls_slice_header(&s->sh, s, gb);
3568 // Once hls_slice_header has been called, the context is inconsistent with the slice header
3569 // until the context is reinitialized according to the contents of the new slice header
3570 // at the start of decode_slice_data.
3571 29013 s->slice_initialized = 0;
3572
2/2
✓ Branch 0 taken 33 times.
✓ Branch 1 taken 28980 times.
29013 if (ret < 0) {
3573 33 return ret;
3574 }
3575
3576
4/4
✓ Branch 0 taken 118 times.
✓ Branch 1 taken 28862 times.
✓ Branch 2 taken 92 times.
✓ Branch 3 taken 26 times.
28980 if ((s->avctx->skip_frame >= AVDISCARD_BIDIR && s->sh.slice_type == HEVC_SLICE_B) ||
3577
4/4
✓ Branch 0 taken 92 times.
✓ Branch 1 taken 28862 times.
✓ Branch 2 taken 91 times.
✓ Branch 3 taken 1 times.
28954 (s->avctx->skip_frame >= AVDISCARD_NONINTRA && s->sh.slice_type != HEVC_SLICE_I) ||
3578
1/6
✗ Branch 0 not taken.
✓ Branch 1 taken 28953 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
28953 (s->avctx->skip_frame >= AVDISCARD_NONKEY && !IS_IRAP(s)) ||
3579
4/4
✓ Branch 0 taken 27829 times.
✓ Branch 1 taken 1124 times.
✓ Branch 2 taken 1045 times.
✓ Branch 3 taken 26784 times.
28953 ((s->nal_unit_type == HEVC_NAL_RASL_R || s->nal_unit_type == HEVC_NAL_RASL_N) &&
3580
2/2
✓ Branch 0 taken 43 times.
✓ Branch 1 taken 2126 times.
2169 s->no_rasl_output_flag)) {
3581
1/2
✓ Branch 0 taken 70 times.
✗ Branch 1 not taken.
70 if (s->sh.first_slice_in_pic_flag)
3582 70 s->skipping_frame = 1;
3583 70 return 0;
3584 }
3585
2/2
✓ Branch 0 taken 10573 times.
✓ Branch 1 taken 18337 times.
28910 if (s->sh.first_slice_in_pic_flag)
3586 10573 s->skipping_frame = 0;
3587
3588 // switching to a new layer, mark previous layer's frame (if any) as done
3589
2/2
✓ Branch 0 taken 771 times.
✓ Branch 1 taken 28139 times.
28910 if (s->cur_layer != layer_idx &&
3590
2/2
✓ Branch 0 taken 390 times.
✓ Branch 1 taken 381 times.
771 s->layers[s->cur_layer].cur_frame &&
3591
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 390 times.
390 s->avctx->active_thread_type == FF_THREAD_FRAME)
3592 ff_progress_frame_report(&s->layers[s->cur_layer].cur_frame->tf, INT_MAX);
3593
3594 28910 s->cur_layer = layer_idx;
3595 28910 l = &s->layers[s->cur_layer];
3596
3597
2/2
✓ Branch 0 taken 10573 times.
✓ Branch 1 taken 18337 times.
28910 if (s->sh.first_slice_in_pic_flag) {
3598
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 10572 times.
10573 if (l->cur_frame) {
3599 1 av_log(s->avctx, AV_LOG_ERROR, "Two slices reporting being the first in the same frame.\n");
3600 1 return AVERROR_INVALIDDATA;
3601 }
3602
3603 10572 ret = hevc_frame_start(s, l, nal_idx);
3604
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10572 times.
10572 if (ret < 0)
3605 return ret;
3606
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 18337 times.
18337 } else if (!l->cur_frame) {
3607 av_log(s->avctx, AV_LOG_ERROR, "First slice in a frame missing.\n");
3608 return AVERROR_INVALIDDATA;
3609 }
3610
3611
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 28909 times.
28909 if (s->nal_unit_type != s->first_nal_type) {
3612 av_log(s->avctx, AV_LOG_ERROR,
3613 "Non-matching NAL types of the VCL NALUs: %d %d\n",
3614 s->first_nal_type, s->nal_unit_type);
3615 return AVERROR_INVALIDDATA;
3616 }
3617
3618 28909 ret = decode_slice_data(s, l, &s->pkt.nals[nal_idx], gb);
3619
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 28908 times.
28909 if (ret < 0)
3620 1 return ret;
3621
3622 28908 return 0;
3623 }
3624
3625 40512 static int decode_nal_unit(HEVCContext *s, unsigned nal_idx)
3626 {
3627 40512 H2645NAL *nal = &s->pkt.nals[nal_idx];
3628 40512 GetBitContext gb = nal->gb;
3629 int ret;
3630
3631 40512 s->nal_unit_type = nal->type;
3632 40512 s->nuh_layer_id = nal->nuh_layer_id;
3633 40512 s->temporal_id = nal->temporal_id;
3634
3635
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 40512 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
40512 if (FF_HW_HAS_CB(s->avctx, decode_params) &&
3636 (s->nal_unit_type == HEVC_NAL_VPS ||
3637 s->nal_unit_type == HEVC_NAL_SPS ||
3638 s->nal_unit_type == HEVC_NAL_PPS ||
3639 s->nal_unit_type == HEVC_NAL_SEI_PREFIX ||
3640 s->nal_unit_type == HEVC_NAL_SEI_SUFFIX)) {
3641 ret = FF_HW_CALL(s->avctx, decode_params,
3642 nal->type, nal->raw_data, nal->raw_size);
3643 if (ret < 0)
3644 goto fail;
3645 }
3646
3647
6/7
✓ Branch 0 taken 454 times.
✓ Branch 1 taken 464 times.
✓ Branch 2 taken 1423 times.
✓ Branch 3 taken 7950 times.
✓ Branch 4 taken 29504 times.
✓ Branch 5 taken 717 times.
✗ Branch 6 not taken.
40512 switch (s->nal_unit_type) {
3648 454 case HEVC_NAL_VPS:
3649 454 ret = ff_hevc_decode_nal_vps(&gb, s->avctx, &s->ps);
3650
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 454 times.
454 if (ret < 0)
3651 goto fail;
3652 454 break;
3653 464 case HEVC_NAL_SPS:
3654 464 ret = ff_hevc_decode_nal_sps(&gb, s->avctx, &s->ps,
3655 464 nal->nuh_layer_id, s->apply_defdispwin);
3656
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 464 times.
464 if (ret < 0)
3657 goto fail;
3658 464 break;
3659 1423 case HEVC_NAL_PPS:
3660 1423 ret = ff_hevc_decode_nal_pps(&gb, s->avctx, &s->ps);
3661
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1421 times.
1423 if (ret < 0)
3662 2 goto fail;
3663 1421 break;
3664 7950 case HEVC_NAL_SEI_PREFIX:
3665 case HEVC_NAL_SEI_SUFFIX:
3666 7950 ret = ff_hevc_decode_nal_sei(&gb, s->avctx, &s->sei, &s->ps, s->nal_unit_type);
3667
2/2
✓ Branch 0 taken 33 times.
✓ Branch 1 taken 7917 times.
7950 if (ret < 0)
3668 33 goto fail;
3669 7917 break;
3670 29504 case HEVC_NAL_TRAIL_R:
3671 case HEVC_NAL_TRAIL_N:
3672 case HEVC_NAL_TSA_N:
3673 case HEVC_NAL_TSA_R:
3674 case HEVC_NAL_STSA_N:
3675 case HEVC_NAL_STSA_R:
3676 case HEVC_NAL_BLA_W_LP:
3677 case HEVC_NAL_BLA_W_RADL:
3678 case HEVC_NAL_BLA_N_LP:
3679 case HEVC_NAL_IDR_W_RADL:
3680 case HEVC_NAL_IDR_N_LP:
3681 case HEVC_NAL_CRA_NUT:
3682 case HEVC_NAL_RADL_N:
3683 case HEVC_NAL_RADL_R:
3684 case HEVC_NAL_RASL_N:
3685 case HEVC_NAL_RASL_R:
3686 29504 ret = decode_slice(s, nal_idx, &gb);
3687
2/2
✓ Branch 0 taken 35 times.
✓ Branch 1 taken 29469 times.
29504 if (ret < 0)
3688 35 goto fail;
3689 29469 break;
3690 717 case HEVC_NAL_EOS_NUT:
3691 case HEVC_NAL_EOB_NUT:
3692 case HEVC_NAL_AUD:
3693 case HEVC_NAL_FD_NUT:
3694 case HEVC_NAL_UNSPEC62: // Dolby Vision RPU
3695 case HEVC_NAL_UNSPEC63: // Dolby Vision EL
3696 717 break;
3697 default:
3698 av_log(s->avctx, AV_LOG_VERBOSE,
3699 "Skipping NAL unit %d\n", s->nal_unit_type);
3700 }
3701
3702 40442 return 0;
3703 70 fail:
3704
1/2
✓ Branch 0 taken 70 times.
✗ Branch 1 not taken.
70 if (ret == AVERROR_INVALIDDATA &&
3705
1/2
✓ Branch 0 taken 70 times.
✗ Branch 1 not taken.
70 !(s->avctx->err_recognition & AV_EF_EXPLODE)) {
3706 70 av_log(s->avctx, AV_LOG_WARNING,
3707 70 "Skipping invalid undecodable NALU: %d\n", s->nal_unit_type);
3708 70 return 0;
3709 }
3710 return ret;
3711 }
3712
3713 447 static void decode_reset_recovery_point(HEVCContext *s)
3714 {
3715 447 s->recovery_poc = HEVC_RECOVERY_UNSPECIFIED;
3716 447 s->sei.recovery_point.has_recovery_poc = 0;
3717 447 }
3718
3719 10315 static int decode_nal_units(HEVCContext *s, const uint8_t *buf, int length)
3720 {
3721 10315 int ret = 0;
3722 10315 int eos_at_start = 1;
3723
2/2
✓ Branch 0 taken 350 times.
✓ Branch 1 taken 9965 times.
10315 int flags = (H2645_FLAG_IS_NALFF * !!s->is_nalff) | H2645_FLAG_SMALL_PADDING;
3724
3725 10315 s->cur_frame = s->collocated_ref = NULL;
3726 10315 s->last_eos = s->eos;
3727 10315 s->eos = 0;
3728 10315 s->slice_initialized = 0;
3729
2/2
✓ Branch 0 taken 443 times.
✓ Branch 1 taken 9872 times.
10315 if (s->last_eos)
3730 443 decode_reset_recovery_point(s);
3731
3732
2/2
✓ Branch 0 taken 20630 times.
✓ Branch 1 taken 10315 times.
30945 for (int i = 0; i < FF_ARRAY_ELEMS(s->layers); i++) {
3733 20630 HEVCLayerContext *l = &s->layers[i];
3734 20630 l->cur_frame = NULL;
3735 }
3736
3737 /* split the input packet into NAL units, so we know the upper bound on the
3738 * number of slices in the frame */
3739 10315 ret = ff_h2645_packet_split(&s->pkt, buf, length, s->avctx,
3740 10315 s->nal_length_size, s->avctx->codec_id, flags);
3741
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10315 times.
10315 if (ret < 0) {
3742 av_log(s->avctx, AV_LOG_ERROR,
3743 "Error splitting the input into NAL units.\n");
3744 return ret;
3745 }
3746
3747
2/2
✓ Branch 0 taken 41017 times.
✓ Branch 1 taken 10315 times.
51332 for (int i = 0; i < s->pkt.nb_nals; i++) {
3748
2/2
✓ Branch 0 taken 41016 times.
✓ Branch 1 taken 1 times.
41017 if (s->pkt.nals[i].type == HEVC_NAL_EOB_NUT ||
3749
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 41013 times.
41016 s->pkt.nals[i].type == HEVC_NAL_EOS_NUT) {
3750
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 if (eos_at_start) {
3751 4 s->last_eos = 1;
3752 4 decode_reset_recovery_point(s);
3753 } else {
3754 s->eos = 1;
3755 }
3756 } else {
3757 41013 eos_at_start = 0;
3758 }
3759 }
3760
3761 /*
3762 * Check for RPU delimiter.
3763 *
3764 * Dolby Vision RPUs masquerade as unregistered NALs of type 62.
3765 *
3766 * We have to do this check here an create the rpu buffer, since RPUs are appended
3767 * to the end of an AU; they are normally the last non-EOB/EOS NAL in the AU.
3768 */
3769 10315 H2645NAL *rpu_nal = NULL;
3770
2/2
✓ Branch 0 taken 30701 times.
✓ Branch 1 taken 10313 times.
41014 for (int i = s->pkt.nb_nals - 1; i > 0 ; i--) {
3771
3/4
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 30699 times.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
30701 if (s->pkt.nals[i].type == HEVC_NAL_UNSPEC62 && s->pkt.nals[i].size > 2
3772
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) {
3773 2 rpu_nal = &s->pkt.nals[i];
3774 2 break;
3775 }
3776 }
3777
3778
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 10313 times.
10315 if (rpu_nal) {
3779
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (s->rpu_buf) {
3780 av_buffer_unref(&s->rpu_buf);
3781 av_log(s->avctx, AV_LOG_WARNING, "Multiple Dolby Vision RPUs found in one AU. Skipping previous.\n");
3782 }
3783
3784 2 s->rpu_buf = av_buffer_alloc(rpu_nal->raw_size - 2);
3785
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (!s->rpu_buf) {
3786 ret = AVERROR(ENOMEM);
3787 goto fail;
3788 }
3789 2 memcpy(s->rpu_buf->data, rpu_nal->raw_data + 2, rpu_nal->raw_size - 2);
3790
3791 2 ret = ff_dovi_rpu_parse(&s->dovi_ctx, rpu_nal->data + 2, rpu_nal->size - 2,
3792 2 s->avctx->err_recognition);
3793
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (ret < 0) {
3794 av_buffer_unref(&s->rpu_buf);
3795 av_log(s->avctx, AV_LOG_WARNING, "Error parsing DOVI NAL unit.\n");
3796 /* ignore */
3797 }
3798 }
3799
3800 /* decode the NAL units */
3801
2/2
✓ Branch 0 taken 41017 times.
✓ Branch 1 taken 10315 times.
51332 for (int i = 0; i < s->pkt.nb_nals; i++) {
3802 41017 H2645NAL *nal = &s->pkt.nals[i];
3803
3804
1/2
✓ Branch 0 taken 41017 times.
✗ Branch 1 not taken.
41017 if (s->avctx->skip_frame >= AVDISCARD_ALL ||
3805
4/4
✓ Branch 0 taken 1140 times.
✓ Branch 1 taken 39877 times.
✓ Branch 3 taken 505 times.
✓ Branch 4 taken 635 times.
41017 (s->avctx->skip_frame >= AVDISCARD_NONREF && ff_hevc_nal_is_nonref(nal->type)))
3806 505 continue;
3807
3808 40512 ret = decode_nal_unit(s, i);
3809
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 40512 times.
40512 if (ret < 0) {
3810 av_log(s->avctx, AV_LOG_WARNING,
3811 "Error parsing NAL unit #%d.\n", i);
3812 goto fail;
3813 }
3814 }
3815
3816 10315 fail:
3817
2/2
✓ Branch 0 taken 20630 times.
✓ Branch 1 taken 10315 times.
30945 for (int i = 0; i < FF_ARRAY_ELEMS(s->layers); i++) {
3818 20630 HEVCLayerContext *l = &s->layers[i];
3819
3820
2/2
✓ Branch 0 taken 10058 times.
✓ Branch 1 taken 10572 times.
20630 if (!l->cur_frame)
3821 10058 continue;
3822
3823
1/2
✓ Branch 0 taken 10572 times.
✗ Branch 1 not taken.
10572 if (ret >= 0)
3824 10572 ret = hevc_frame_end(s, l);
3825
3826
2/2
✓ Branch 0 taken 33 times.
✓ Branch 1 taken 10539 times.
10572 if (s->avctx->active_thread_type == FF_THREAD_FRAME)
3827 33 ff_progress_frame_report(&l->cur_frame->tf, INT_MAX);
3828 }
3829
3830 10315 return ret;
3831 }
3832
3833 355 static int hevc_decode_extradata(HEVCContext *s, uint8_t *buf, int length, int first)
3834 {
3835 int ret, i;
3836
3837 355 ret = ff_hevc_decode_extradata(buf, length, &s->ps, &s->sei, &s->is_nalff,
3838 355 &s->nal_length_size, s->avctx->err_recognition,
3839 355 s->apply_defdispwin, s->avctx);
3840
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 355 times.
355 if (ret < 0)
3841 return ret;
3842
3843 /* export stream parameters from the first SPS */
3844
2/2
✓ Branch 0 taken 385 times.
✓ Branch 1 taken 2 times.
387 for (i = 0; i < FF_ARRAY_ELEMS(s->ps.sps_list); i++) {
3845
4/4
✓ Branch 0 taken 369 times.
✓ Branch 1 taken 16 times.
✓ Branch 2 taken 353 times.
✓ Branch 3 taken 16 times.
385 if (first && s->ps.sps_list[i]) {
3846 353 const HEVCSPS *sps = s->ps.sps_list[i];
3847 353 export_stream_params(s, sps);
3848
3849 353 ret = export_multilayer(s, sps->vps);
3850
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 353 times.
353 if (ret < 0)
3851 return ret;
3852
3853 353 break;
3854 }
3855 }
3856
3857 /* export stream parameters from SEI */
3858 355 ret = export_stream_params_from_sei(s);
3859
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 355 times.
355 if (ret < 0)
3860 return ret;
3861
3862 355 return 0;
3863 }
3864
3865 21862 static int hevc_receive_frame(AVCodecContext *avctx, AVFrame *frame)
3866 {
3867 21862 HEVCContext *s = avctx->priv_data;
3868 21862 AVCodecInternal *avci = avctx->internal;
3869 21862 AVPacket *avpkt = avci->in_pkt;
3870
3871 int ret;
3872 uint8_t *sd;
3873 size_t sd_size;
3874
3875 21862 s->pkt_dts = AV_NOPTS_VALUE;
3876
3877
2/2
✓ Branch 1 taken 961 times.
✓ Branch 2 taken 20901 times.
21862 if (av_container_fifo_can_read(s->output_fifo))
3878 961 goto do_output;
3879
3880 20901 av_packet_unref(avpkt);
3881 20901 ret = ff_decode_get_packet(avctx, avpkt);
3882
2/2
✓ Branch 0 taken 350 times.
✓ Branch 1 taken 20551 times.
20901 if (ret == AVERROR_EOF) {
3883 350 ret = ff_hevc_output_frames(s, s->layers_active_decode,
3884 s->layers_active_output, 0, 0, 0);
3885
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 350 times.
350 if (ret < 0)
3886 return ret;
3887 350 goto do_output;
3888
2/2
✓ Branch 0 taken 10236 times.
✓ Branch 1 taken 10315 times.
20551 } else if (ret < 0)
3889 10236 return ret;
3890
3891 10315 s->pkt_dts = avpkt->dts;
3892
3893 10315 sd = av_packet_get_side_data(avpkt, AV_PKT_DATA_NEW_EXTRADATA, &sd_size);
3894
3/4
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 10314 times.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
10315 if (sd && sd_size > 0) {
3895 1 ret = hevc_decode_extradata(s, sd, sd_size, 0);
3896
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (ret < 0)
3897 return ret;
3898 }
3899
3900 10315 sd = av_packet_get_side_data(avpkt, AV_PKT_DATA_DOVI_CONF, &sd_size);
3901
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 10315 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
10315 if (sd && sd_size >= sizeof(s->dovi_ctx.cfg)) {
3902 int old = s->dovi_ctx.cfg.dv_profile;
3903 s->dovi_ctx.cfg = *(AVDOVIDecoderConfigurationRecord *) sd;
3904 if (old)
3905 av_log(avctx, AV_LOG_DEBUG,
3906 "New DOVI configuration record from input packet (profile %d -> %u).\n",
3907 old, s->dovi_ctx.cfg.dv_profile);
3908 }
3909
3910 10315 ret = decode_nal_units(s, avpkt->data, avpkt->size);
3911
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10315 times.
10315 if (ret < 0)
3912 return ret;
3913
3914 10315 do_output:
3915
2/2
✓ Branch 1 taken 10278 times.
✓ Branch 2 taken 1348 times.
11626 if (av_container_fifo_read(s->output_fifo, frame, 0) >= 0) {
3916
1/2
✓ Branch 0 taken 10278 times.
✗ Branch 1 not taken.
10278 if (!(avctx->export_side_data & AV_CODEC_EXPORT_DATA_FILM_GRAIN))
3917 10278 av_frame_remove_side_data(frame, AV_FRAME_DATA_FILM_GRAIN_PARAMS);
3918
3919 10278 return 0;
3920 }
3921
3922
2/2
✓ Branch 0 taken 219 times.
✓ Branch 1 taken 1129 times.
1348 return avci->draining ? AVERROR_EOF : AVERROR(EAGAIN);
3923 }
3924
3925 67 static int hevc_ref_frame(HEVCFrame *dst, const HEVCFrame *src)
3926 {
3927 int ret;
3928
3929 67 ff_progress_frame_ref(&dst->tf, &src->tf);
3930
3931
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 67 times.
67 if (src->needs_fg) {
3932 ret = av_frame_ref(dst->frame_grain, src->frame_grain);
3933 if (ret < 0) {
3934 ff_hevc_unref_frame(dst, ~0);
3935 return ret;
3936 }
3937 dst->needs_fg = 1;
3938 }
3939
3940 67 dst->pps = av_refstruct_ref_c(src->pps);
3941 67 dst->tab_mvf = av_refstruct_ref(src->tab_mvf);
3942 67 dst->rpl_tab = av_refstruct_ref(src->rpl_tab);
3943 67 dst->rpl = av_refstruct_ref(src->rpl);
3944 67 dst->nb_rpl_elems = src->nb_rpl_elems;
3945
3946 67 dst->poc = src->poc;
3947 67 dst->ctb_count = src->ctb_count;
3948 67 dst->flags = src->flags;
3949
3950 67 dst->base_layer_frame = src->base_layer_frame;
3951
3952 67 av_refstruct_replace(&dst->hwaccel_picture_private,
3953 67 src->hwaccel_picture_private);
3954
3955 67 return 0;
3956 }
3957
3958 577 static av_cold int hevc_decode_free(AVCodecContext *avctx)
3959 {
3960 577 HEVCContext *s = avctx->priv_data;
3961
3962
2/2
✓ Branch 0 taken 1154 times.
✓ Branch 1 taken 577 times.
1731 for (int i = 0; i < FF_ARRAY_ELEMS(s->layers); i++) {
3963 1154 pic_arrays_free(&s->layers[i]);
3964 1154 av_refstruct_unref(&s->layers[i].sps);
3965 }
3966
3967 577 av_refstruct_unref(&s->vps);
3968 577 av_refstruct_unref(&s->pps);
3969
3970 577 ff_dovi_ctx_unref(&s->dovi_ctx);
3971 577 av_buffer_unref(&s->rpu_buf);
3972
3973 577 av_freep(&s->md5_ctx);
3974
3975 577 av_container_fifo_free(&s->output_fifo);
3976
3977
2/2
✓ Branch 0 taken 1154 times.
✓ Branch 1 taken 577 times.
1731 for (int layer = 0; layer < FF_ARRAY_ELEMS(s->layers); layer++) {
3978 1154 HEVCLayerContext *l = &s->layers[layer];
3979
2/2
✓ Branch 0 taken 36928 times.
✓ Branch 1 taken 1154 times.
38082 for (int i = 0; i < FF_ARRAY_ELEMS(l->DPB); i++) {
3980 36928 ff_hevc_unref_frame(&l->DPB[i], ~0);
3981 36928 av_frame_free(&l->DPB[i].frame_grain);
3982 }
3983 }
3984
3985 577 ff_hevc_ps_uninit(&s->ps);
3986
3987
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 577 times.
577 for (int i = 0; i < s->nb_wpp_progress; i++)
3988 ff_thread_progress_destroy(&s->wpp_progress[i]);
3989 577 av_freep(&s->wpp_progress);
3990
3991 577 av_freep(&s->sh.entry_point_offset);
3992 577 av_freep(&s->sh.offset);
3993 577 av_freep(&s->sh.size);
3994
3995 577 av_freep(&s->local_ctx);
3996
3997 577 ff_h2645_packet_uninit(&s->pkt);
3998
3999 577 ff_hevc_reset_sei(&s->sei);
4000
4001 577 return 0;
4002 }
4003
4004 577 static av_cold int hevc_init_context(AVCodecContext *avctx)
4005 {
4006 577 HEVCContext *s = avctx->priv_data;
4007
4008 577 s->avctx = avctx;
4009
4010 577 s->local_ctx = av_mallocz(sizeof(*s->local_ctx));
4011
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 577 times.
577 if (!s->local_ctx)
4012 return AVERROR(ENOMEM);
4013 577 s->nb_local_ctx = 1;
4014
4015 577 s->local_ctx[0].parent = s;
4016 577 s->local_ctx[0].logctx = avctx;
4017 577 s->local_ctx[0].common_cabac_state = &s->cabac;
4018
4019 577 s->output_fifo = av_container_fifo_alloc_avframe(0);
4020
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 577 times.
577 if (!s->output_fifo)
4021 return AVERROR(ENOMEM);
4022
4023
2/2
✓ Branch 0 taken 1154 times.
✓ Branch 1 taken 577 times.
1731 for (int layer = 0; layer < FF_ARRAY_ELEMS(s->layers); layer++) {
4024 1154 HEVCLayerContext *l = &s->layers[layer];
4025
2/2
✓ Branch 0 taken 36928 times.
✓ Branch 1 taken 1154 times.
38082 for (int i = 0; i < FF_ARRAY_ELEMS(l->DPB); i++) {
4026 36928 l->DPB[i].frame_grain = av_frame_alloc();
4027
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 36928 times.
36928 if (!l->DPB[i].frame_grain)
4028 return AVERROR(ENOMEM);
4029 }
4030 }
4031
4032 577 s->md5_ctx = av_md5_alloc();
4033
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 577 times.
577 if (!s->md5_ctx)
4034 return AVERROR(ENOMEM);
4035
4036 577 ff_bswapdsp_init(&s->bdsp);
4037
4038 577 s->dovi_ctx.logctx = avctx;
4039 577 s->eos = 0;
4040
4041 577 ff_hevc_reset_sei(&s->sei);
4042
4043 577 return 0;
4044 }
4045
4046 #if HAVE_THREADS
4047 32 static int hevc_update_thread_context(AVCodecContext *dst,
4048 const AVCodecContext *src)
4049 {
4050 32 HEVCContext *s = dst->priv_data;
4051 32 HEVCContext *s0 = src->priv_data;
4052 int ret;
4053
4054
2/2
✓ Branch 0 taken 64 times.
✓ Branch 1 taken 32 times.
96 for (int layer = 0; layer < FF_ARRAY_ELEMS(s->layers); layer++) {
4055 64 HEVCLayerContext *l = &s->layers[layer];
4056 64 const HEVCLayerContext *l0 = &s0->layers[layer];
4057
2/2
✓ Branch 0 taken 2048 times.
✓ Branch 1 taken 64 times.
2112 for (int i = 0; i < FF_ARRAY_ELEMS(l->DPB); i++) {
4058 2048 ff_hevc_unref_frame(&l->DPB[i], ~0);
4059
2/2
✓ Branch 0 taken 67 times.
✓ Branch 1 taken 1981 times.
2048 if (l0->DPB[i].f) {
4060 67 ret = hevc_ref_frame(&l->DPB[i], &l0->DPB[i]);
4061
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 67 times.
67 if (ret < 0)
4062 return ret;
4063 }
4064 }
4065
4066
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 63 times.
64 if (l->sps != l0->sps) {
4067 1 ret = set_sps(s, l, l0->sps);
4068
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (ret < 0)
4069 return ret;
4070 }
4071 }
4072
4073
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++)
4074 512 av_refstruct_replace(&s->ps.vps_list[i], s0->ps.vps_list[i]);
4075
4076
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++)
4077 512 av_refstruct_replace(&s->ps.sps_list[i], s0->ps.sps_list[i]);
4078
4079
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++)
4080 2048 av_refstruct_replace(&s->ps.pps_list[i], s0->ps.pps_list[i]);
4081
4082 // PPS do not persist between frames
4083 32 av_refstruct_unref(&s->pps);
4084
4085 32 s->poc_tid0 = s0->poc_tid0;
4086 32 s->eos = s0->eos;
4087 32 s->no_rasl_output_flag = s0->no_rasl_output_flag;
4088 32 s->skipping_frame = s0->skipping_frame;
4089
4090 32 s->is_nalff = s0->is_nalff;
4091 32 s->nal_length_size = s0->nal_length_size;
4092 32 s->layers_active_decode = s0->layers_active_decode;
4093 32 s->layers_active_output = s0->layers_active_output;
4094
4095 32 s->film_grain_warning_shown = s0->film_grain_warning_shown;
4096
4097
1/2
✓ Branch 0 taken 32 times.
✗ Branch 1 not taken.
32 if (s->nb_view_ids != s0->nb_view_ids ||
4098
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)) {
4099 av_freep(&s->view_ids);
4100 s->nb_view_ids = 0;
4101
4102 if (s0->nb_view_ids) {
4103 s->view_ids = av_memdup(s0->view_ids, s0->nb_view_ids * sizeof(*s0->view_ids));
4104 if (!s->view_ids)
4105 return AVERROR(ENOMEM);
4106 s->nb_view_ids = s0->nb_view_ids;
4107 }
4108 }
4109
4110 32 ret = ff_h2645_sei_ctx_replace(&s->sei.common, &s0->sei.common);
4111
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 32 times.
32 if (ret < 0)
4112 return ret;
4113
4114 32 ret = av_buffer_replace(&s->sei.common.itut_t35.hdr_plus,
4115 32 s0->sei.common.itut_t35.hdr_plus);
4116
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 32 times.
32 if (ret < 0)
4117 return ret;
4118
4119 32 ret = av_buffer_replace(&s->rpu_buf, s0->rpu_buf);
4120
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 32 times.
32 if (ret < 0)
4121 return ret;
4122
4123 32 ff_dovi_ctx_replace(&s->dovi_ctx, &s0->dovi_ctx);
4124
4125 32 ret = av_buffer_replace(&s->sei.common.itut_t35.hdr_vivid,
4126 32 s0->sei.common.itut_t35.hdr_vivid);
4127
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 32 times.
32 if (ret < 0)
4128 return ret;
4129
4130 32 s->sei.common.frame_packing = s0->sei.common.frame_packing;
4131 32 s->sei.common.display_orientation = s0->sei.common.display_orientation;
4132 32 s->sei.common.alternative_transfer = s0->sei.common.alternative_transfer;
4133 32 s->sei.tdrdi = s0->sei.tdrdi;
4134 32 s->sei.recovery_point = s0->sei.recovery_point;
4135 32 s->recovery_poc = s0->recovery_poc;
4136
4137 32 return 0;
4138 }
4139 #endif
4140
4141 354 static int hevc_sei_to_context(AVCodecContext *avctx, HEVCSEI *sei)
4142 {
4143 int ret;
4144
4145
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 350 times.
354 if (sei->tdrdi.present) {
4146 AVBufferRef *buf;
4147 size_t size;
4148 4 AV3DReferenceDisplaysInfo *tdrdi = av_tdrdi_alloc(sei->tdrdi.num_ref_displays, &size);
4149
4150
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (!tdrdi)
4151 return AVERROR(ENOMEM);
4152
4153 4 buf = av_buffer_create((uint8_t *)tdrdi, size, NULL, NULL, 0);
4154
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (!buf) {
4155 av_free(tdrdi);
4156 return AVERROR(ENOMEM);
4157 }
4158
4159 4 tdrdi->prec_ref_display_width = sei->tdrdi.prec_ref_display_width;
4160 4 tdrdi->ref_viewing_distance_flag = sei->tdrdi.ref_viewing_distance_flag;
4161 4 tdrdi->prec_ref_viewing_dist = sei->tdrdi.prec_ref_viewing_dist;
4162 4 tdrdi->num_ref_displays = sei->tdrdi.num_ref_displays;
4163
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 4 times.
8 for (int i = 0; i < sei->tdrdi.num_ref_displays; i++) {
4164 4 AV3DReferenceDisplay *display = av_tdrdi_get_display(tdrdi, i);
4165
4166 4 display->left_view_id = sei->tdrdi.left_view_id[i];
4167 4 display->right_view_id = sei->tdrdi.right_view_id[i];
4168 4 display->exponent_ref_display_width = sei->tdrdi.exponent_ref_display_width[i];
4169 4 display->mantissa_ref_display_width = sei->tdrdi.mantissa_ref_display_width[i];
4170 4 display->exponent_ref_viewing_distance = sei->tdrdi.exponent_ref_viewing_distance[i];
4171 4 display->mantissa_ref_viewing_distance = sei->tdrdi.mantissa_ref_viewing_distance[i];
4172 4 display->additional_shift_present_flag = sei->tdrdi.additional_shift_present_flag[i];
4173 4 display->num_sample_shift = sei->tdrdi.num_sample_shift[i];
4174 }
4175 4 ret = ff_frame_new_side_data_from_buf_ext(avctx, &avctx->decoded_side_data, &avctx->nb_decoded_side_data,
4176 AV_FRAME_DATA_3D_REFERENCE_DISPLAYS, &buf);
4177
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (ret < 0) {
4178 av_buffer_unref(&buf);
4179 return ret;
4180 }
4181 }
4182
4183 354 ret = ff_h2645_sei_to_context(avctx, &sei->common);
4184
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 354 times.
354 if (ret < 0)
4185 return ret;
4186
4187 354 return 0;
4188 }
4189
4190 577 static av_cold int hevc_decode_init(AVCodecContext *avctx)
4191 {
4192 577 HEVCContext *s = avctx->priv_data;
4193 int ret;
4194
4195 577 ret = hevc_init_context(avctx);
4196
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 577 times.
577 if (ret < 0)
4197 return ret;
4198
4199 577 s->sei.picture_timing.picture_struct = 0;
4200 577 s->eos = 1;
4201
4202 577 atomic_init(&s->wpp_err, 0);
4203
4204
2/2
✓ Branch 0 taken 576 times.
✓ Branch 1 taken 1 times.
577 if (!avctx->internal->is_copy) {
4205 const AVPacketSideData *sd;
4206
4207
3/4
✓ Branch 0 taken 354 times.
✓ Branch 1 taken 222 times.
✓ Branch 2 taken 354 times.
✗ Branch 3 not taken.
576 if (avctx->extradata_size > 0 && avctx->extradata) {
4208 354 ret = hevc_decode_extradata(s, avctx->extradata, avctx->extradata_size, 1);
4209
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 354 times.
354 if (ret < 0) {
4210 return ret;
4211 }
4212
4213 354 ret = hevc_sei_to_context(avctx, &s->sei);
4214
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 354 times.
354 if (ret < 0)
4215 return ret;
4216 }
4217
4218 576 sd = ff_get_coded_side_data(avctx, AV_PKT_DATA_DOVI_CONF);
4219
3/4
✓ Branch 0 taken 35 times.
✓ Branch 1 taken 541 times.
✓ Branch 2 taken 35 times.
✗ Branch 3 not taken.
576 if (sd && sd->size >= sizeof(s->dovi_ctx.cfg))
4220 35 s->dovi_ctx.cfg = *(AVDOVIDecoderConfigurationRecord *) sd->data;
4221 }
4222
4223 577 return 0;
4224 }
4225
4226 static av_cold void hevc_decode_flush(AVCodecContext *avctx)
4227 {
4228 HEVCContext *s = avctx->priv_data;
4229 ff_hevc_flush_dpb(s);
4230 ff_hevc_reset_sei(&s->sei);
4231 ff_dovi_ctx_flush(&s->dovi_ctx);
4232 av_buffer_unref(&s->rpu_buf);
4233 s->eos = 1;
4234 s->skipping_frame = 0;
4235
4236 if (FF_HW_HAS_CB(avctx, flush))
4237 FF_HW_SIMPLE_CALL(avctx, flush);
4238 }
4239
4240 #define OFFSET(x) offsetof(HEVCContext, x)
4241 #define PAR (AV_OPT_FLAG_DECODING_PARAM | AV_OPT_FLAG_VIDEO_PARAM)
4242
4243 static const AVOption options[] = {
4244 { "apply_defdispwin", "Apply default display window from VUI", OFFSET(apply_defdispwin),
4245 AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, PAR },
4246 { "strict-displaywin", "strictly apply default display window size", OFFSET(apply_defdispwin),
4247 AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, PAR },
4248 { "view_ids", "Array of view IDs that should be decoded and output; a single -1 to decode all views",
4249 .offset = OFFSET(view_ids), .type = AV_OPT_TYPE_INT | AV_OPT_TYPE_FLAG_ARRAY,
4250 .min = -1, .max = INT_MAX, .flags = PAR },
4251 { "view_ids_available", "Array of available view IDs is exported here",
4252 .offset = OFFSET(view_ids_available), .type = AV_OPT_TYPE_UINT | AV_OPT_TYPE_FLAG_ARRAY,
4253 .flags = PAR | AV_OPT_FLAG_EXPORT | AV_OPT_FLAG_READONLY },
4254 { "view_pos_available", "Array of view positions for view_ids_available is exported here, as AVStereo3DView",
4255 .offset = OFFSET(view_pos_available), .type = AV_OPT_TYPE_UINT | AV_OPT_TYPE_FLAG_ARRAY,
4256 .flags = PAR | AV_OPT_FLAG_EXPORT | AV_OPT_FLAG_READONLY, .unit = "view_pos" },
4257 { "unspecified", .type = AV_OPT_TYPE_CONST, .default_val = { .i64 = AV_STEREO3D_VIEW_UNSPEC }, .unit = "view_pos" },
4258 { "left", .type = AV_OPT_TYPE_CONST, .default_val = { .i64 = AV_STEREO3D_VIEW_LEFT }, .unit = "view_pos" },
4259 { "right", .type = AV_OPT_TYPE_CONST, .default_val = { .i64 = AV_STEREO3D_VIEW_RIGHT }, .unit = "view_pos" },
4260
4261 { NULL },
4262 };
4263
4264 static const AVClass hevc_decoder_class = {
4265 .class_name = "HEVC decoder",
4266 .item_name = av_default_item_name,
4267 .option = options,
4268 .version = LIBAVUTIL_VERSION_INT,
4269 };
4270
4271 const FFCodec ff_hevc_decoder = {
4272 .p.name = "hevc",
4273 CODEC_LONG_NAME("HEVC (High Efficiency Video Coding)"),
4274 .p.type = AVMEDIA_TYPE_VIDEO,
4275 .p.id = AV_CODEC_ID_HEVC,
4276 .priv_data_size = sizeof(HEVCContext),
4277 .p.priv_class = &hevc_decoder_class,
4278 .init = hevc_decode_init,
4279 .close = hevc_decode_free,
4280 FF_CODEC_RECEIVE_FRAME_CB(hevc_receive_frame),
4281 .flush = hevc_decode_flush,
4282 UPDATE_THREAD_CONTEXT(hevc_update_thread_context),
4283 .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DELAY |
4284 AV_CODEC_CAP_SLICE_THREADS | AV_CODEC_CAP_FRAME_THREADS,
4285 .caps_internal = FF_CODEC_CAP_EXPORTS_CROPPING |
4286 FF_CODEC_CAP_USES_PROGRESSFRAMES |
4287 FF_CODEC_CAP_INIT_CLEANUP,
4288 .p.profiles = NULL_IF_CONFIG_SMALL(ff_hevc_profiles),
4289 .hw_configs = (const AVCodecHWConfigInternal *const []) {
4290 #if CONFIG_HEVC_DXVA2_HWACCEL
4291 HWACCEL_DXVA2(hevc),
4292 #endif
4293 #if CONFIG_HEVC_D3D11VA_HWACCEL
4294 HWACCEL_D3D11VA(hevc),
4295 #endif
4296 #if CONFIG_HEVC_D3D11VA2_HWACCEL
4297 HWACCEL_D3D11VA2(hevc),
4298 #endif
4299 #if CONFIG_HEVC_D3D12VA_HWACCEL
4300 HWACCEL_D3D12VA(hevc),
4301 #endif
4302 #if CONFIG_HEVC_NVDEC_HWACCEL
4303 HWACCEL_NVDEC(hevc),
4304 #endif
4305 #if CONFIG_HEVC_NVDEC_CUARRAY_HWACCEL
4306 HWACCEL_NVDEC_CUARRAY(hevc),
4307 #endif
4308 #if CONFIG_HEVC_VAAPI_HWACCEL
4309 HWACCEL_VAAPI(hevc),
4310 #endif
4311 #if CONFIG_HEVC_VDPAU_HWACCEL
4312 HWACCEL_VDPAU(hevc),
4313 #endif
4314 #if CONFIG_HEVC_VIDEOTOOLBOX_HWACCEL
4315 HWACCEL_VIDEOTOOLBOX(hevc),
4316 #endif
4317 #if CONFIG_HEVC_VULKAN_HWACCEL
4318 HWACCEL_VULKAN(hevc),
4319 #endif
4320 NULL
4321 },
4322 };
4323