FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/hevc/hevcdec.c
Date: 2024-10-17 22:24:08
Exec Total Coverage
Lines: 1936 2382 81.3%
Functions: 48 52 92.3%
Branches: 1365 1796 76.0%

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/film_grain_params.h"
32 #include "libavutil/internal.h"
33 #include "libavutil/md5.h"
34 #include "libavutil/mem.h"
35 #include "libavutil/opt.h"
36 #include "libavutil/pixdesc.h"
37 #include "libavutil/stereo3d.h"
38 #include "libavutil/timecode.h"
39
40 #include "aom_film_grain.h"
41 #include "bswapdsp.h"
42 #include "cabac_functions.h"
43 #include "codec_internal.h"
44 #include "container_fifo.h"
45 #include "decode.h"
46 #include "golomb.h"
47 #include "hevc.h"
48 #include "parse.h"
49 #include "hevcdec.h"
50 #include "hwaccel_internal.h"
51 #include "hwconfig.h"
52 #include "internal.h"
53 #include "profiles.h"
54 #include "progressframe.h"
55 #include "refstruct.h"
56 #include "thread.h"
57 #include "threadprogress.h"
58
59 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 };
60
61 /**
62 * NOTE: Each function hls_foo correspond to the function foo in the
63 * specification (HLS stands for High Level Syntax).
64 */
65
66 /**
67 * Section 5.7
68 */
69
70 /* free everything allocated by pic_arrays_init() */
71 1326 static void pic_arrays_free(HEVCLayerContext *l)
72 {
73 1326 av_freep(&l->sao);
74 1326 av_freep(&l->deblock);
75
76 1326 av_freep(&l->skip_flag);
77 1326 av_freep(&l->tab_ct_depth);
78
79 1326 av_freep(&l->tab_ipm);
80 1326 av_freep(&l->cbf_luma);
81 1326 av_freep(&l->is_pcm);
82
83 1326 av_freep(&l->qp_y_tab);
84 1326 av_freep(&l->tab_slice_address);
85 1326 av_freep(&l->filter_slice_edges);
86
87 1326 av_freep(&l->horizontal_bs);
88 1326 av_freep(&l->vertical_bs);
89
90
2/2
✓ Branch 0 taken 3978 times.
✓ Branch 1 taken 1326 times.
5304 for (int i = 0; i < 3; i++) {
91 3978 av_freep(&l->sao_pixel_buffer_h[i]);
92 3978 av_freep(&l->sao_pixel_buffer_v[i]);
93 }
94
95 1326 ff_refstruct_pool_uninit(&l->tab_mvf_pool);
96 1326 ff_refstruct_pool_uninit(&l->rpl_tab_pool);
97 1326 }
98
99 /* allocate arrays that depend on frame dimensions */
100 414 static int pic_arrays_init(HEVCLayerContext *l, const HEVCSPS *sps)
101 {
102 414 int log2_min_cb_size = sps->log2_min_cb_size;
103 414 int width = sps->width;
104 414 int height = sps->height;
105 414 int pic_size_in_ctb = ((width >> log2_min_cb_size) + 1) *
106 414 ((height >> log2_min_cb_size) + 1);
107 414 int ctb_count = sps->ctb_width * sps->ctb_height;
108 414 int min_pu_size = sps->min_pu_width * sps->min_pu_height;
109
110 414 l->bs_width = (width >> 2) + 1;
111 414 l->bs_height = (height >> 2) + 1;
112
113 414 l->sao = av_calloc(ctb_count, sizeof(*l->sao));
114 414 l->deblock = av_calloc(ctb_count, sizeof(*l->deblock));
115
2/4
✓ Branch 0 taken 414 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 414 times.
414 if (!l->sao || !l->deblock)
116 goto fail;
117
118 414 l->skip_flag = av_malloc_array(sps->min_cb_height, sps->min_cb_width);
119 414 l->tab_ct_depth = av_malloc_array(sps->min_cb_height, sps->min_cb_width);
120
2/4
✓ Branch 0 taken 414 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 414 times.
414 if (!l->skip_flag || !l->tab_ct_depth)
121 goto fail;
122
123 414 l->cbf_luma = av_malloc_array(sps->min_tb_width, sps->min_tb_height);
124 414 l->tab_ipm = av_mallocz(min_pu_size);
125 414 l->is_pcm = av_malloc_array(sps->min_pu_width + 1, sps->min_pu_height + 1);
126
3/6
✓ Branch 0 taken 414 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 414 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 414 times.
414 if (!l->tab_ipm || !l->cbf_luma || !l->is_pcm)
127 goto fail;
128
129 414 l->filter_slice_edges = av_mallocz(ctb_count);
130 414 l->tab_slice_address = av_malloc_array(pic_size_in_ctb,
131 sizeof(*l->tab_slice_address));
132 414 l->qp_y_tab = av_malloc_array(pic_size_in_ctb,
133 sizeof(*l->qp_y_tab));
134
3/6
✓ Branch 0 taken 414 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 414 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 414 times.
414 if (!l->qp_y_tab || !l->filter_slice_edges || !l->tab_slice_address)
135 goto fail;
136
137 414 l->horizontal_bs = av_calloc(l->bs_width, l->bs_height);
138 414 l->vertical_bs = av_calloc(l->bs_width, l->bs_height);
139
2/4
✓ Branch 0 taken 414 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 414 times.
414 if (!l->horizontal_bs || !l->vertical_bs)
140 goto fail;
141
142 414 l->tab_mvf_pool = ff_refstruct_pool_alloc(min_pu_size * sizeof(MvField), 0);
143 414 l->rpl_tab_pool = ff_refstruct_pool_alloc(ctb_count * sizeof(RefPicListTab), 0);
144
2/4
✓ Branch 0 taken 414 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 414 times.
414 if (!l->tab_mvf_pool || !l->rpl_tab_pool)
145 goto fail;
146
147
2/2
✓ Branch 0 taken 359 times.
✓ Branch 1 taken 55 times.
414 if (sps->sao_enabled) {
148
2/2
✓ Branch 0 taken 357 times.
✓ Branch 1 taken 2 times.
359 int c_count = (sps->chroma_format_idc != 0) ? 3 : 1;
149
150
2/2
✓ Branch 0 taken 1073 times.
✓ Branch 1 taken 359 times.
1432 for (int c_idx = 0; c_idx < c_count; c_idx++) {
151 1073 int w = sps->width >> sps->hshift[c_idx];
152 1073 int h = sps->height >> sps->vshift[c_idx];
153 1073 l->sao_pixel_buffer_h[c_idx] =
154 1073 av_malloc((w * 2 * sps->ctb_height) <<
155 1073 sps->pixel_shift);
156 1073 l->sao_pixel_buffer_v[c_idx] =
157 1073 av_malloc((h * 2 * sps->ctb_width) <<
158 1073 sps->pixel_shift);
159
1/2
✓ Branch 0 taken 1073 times.
✗ Branch 1 not taken.
1073 if (!l->sao_pixel_buffer_h[c_idx] ||
160
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1073 times.
1073 !l->sao_pixel_buffer_v[c_idx])
161 goto fail;
162 }
163 }
164
165 414 return 0;
166
167 fail:
168 pic_arrays_free(l);
169 return AVERROR(ENOMEM);
170 }
171
172 1443 static int pred_weight_table(SliceHeader *sh, void *logctx,
173 const HEVCSPS *sps, GetBitContext *gb)
174 {
175 1443 int i = 0;
176 1443 int j = 0;
177 uint8_t luma_weight_l0_flag[16];
178 uint8_t chroma_weight_l0_flag[16];
179 uint8_t luma_weight_l1_flag[16];
180 uint8_t chroma_weight_l1_flag[16];
181 int luma_log2_weight_denom;
182
183 1443 luma_log2_weight_denom = get_ue_golomb_long(gb);
184
2/4
✓ Branch 0 taken 1443 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1443 times.
1443 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 1443 sh->luma_log2_weight_denom = av_clip_uintp2(luma_log2_weight_denom, 3);
189
1/2
✓ Branch 0 taken 1443 times.
✗ Branch 1 not taken.
1443 if (sps->chroma_format_idc != 0) {
190 1443 int64_t chroma_log2_weight_denom = luma_log2_weight_denom + (int64_t)get_se_golomb(gb);
191
2/4
✓ Branch 0 taken 1443 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1443 times.
1443 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 1443 sh->chroma_log2_weight_denom = chroma_log2_weight_denom;
196 }
197
198
2/2
✓ Branch 0 taken 4407 times.
✓ Branch 1 taken 1443 times.
5850 for (i = 0; i < sh->nb_refs[L0]; i++) {
199 4407 luma_weight_l0_flag[i] = get_bits1(gb);
200
2/2
✓ Branch 0 taken 1290 times.
✓ Branch 1 taken 3117 times.
4407 if (!luma_weight_l0_flag[i]) {
201 1290 sh->luma_weight_l0[i] = 1 << sh->luma_log2_weight_denom;
202 1290 sh->luma_offset_l0[i] = 0;
203 }
204 }
205
1/2
✓ Branch 0 taken 1443 times.
✗ Branch 1 not taken.
1443 if (sps->chroma_format_idc != 0) {
206
2/2
✓ Branch 0 taken 4407 times.
✓ Branch 1 taken 1443 times.
5850 for (i = 0; i < sh->nb_refs[L0]; i++)
207 4407 chroma_weight_l0_flag[i] = get_bits1(gb);
208 } else {
209 for (i = 0; i < sh->nb_refs[L0]; i++)
210 chroma_weight_l0_flag[i] = 0;
211 }
212
2/2
✓ Branch 0 taken 4407 times.
✓ Branch 1 taken 1443 times.
5850 for (i = 0; i < sh->nb_refs[L0]; i++) {
213
2/2
✓ Branch 0 taken 3117 times.
✓ Branch 1 taken 1290 times.
4407 if (luma_weight_l0_flag[i]) {
214 3117 int delta_luma_weight_l0 = get_se_golomb(gb);
215
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3117 times.
3117 if ((int8_t)delta_luma_weight_l0 != delta_luma_weight_l0)
216 return AVERROR_INVALIDDATA;
217 3117 sh->luma_weight_l0[i] = (1 << sh->luma_log2_weight_denom) + delta_luma_weight_l0;
218 3117 sh->luma_offset_l0[i] = get_se_golomb(gb);
219 }
220
2/2
✓ Branch 0 taken 1131 times.
✓ Branch 1 taken 3276 times.
4407 if (chroma_weight_l0_flag[i]) {
221
2/2
✓ Branch 0 taken 2262 times.
✓ Branch 1 taken 1131 times.
3393 for (j = 0; j < 2; j++) {
222 2262 int delta_chroma_weight_l0 = get_se_golomb(gb);
223 2262 int delta_chroma_offset_l0 = get_se_golomb(gb);
224
225
1/2
✓ Branch 0 taken 2262 times.
✗ Branch 1 not taken.
2262 if ( (int8_t)delta_chroma_weight_l0 != delta_chroma_weight_l0
226
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)) {
227 return AVERROR_INVALIDDATA;
228 }
229
230 2262 sh->chroma_weight_l0[i][j] = (1 << sh->chroma_log2_weight_denom) + delta_chroma_weight_l0;
231 2262 sh->chroma_offset_l0[i][j] = av_clip((delta_chroma_offset_l0 - ((128 * sh->chroma_weight_l0[i][j])
232 2262 >> sh->chroma_log2_weight_denom) + 128), -128, 127);
233 }
234 } else {
235 3276 sh->chroma_weight_l0[i][0] = 1 << sh->chroma_log2_weight_denom;
236 3276 sh->chroma_offset_l0[i][0] = 0;
237 3276 sh->chroma_weight_l0[i][1] = 1 << sh->chroma_log2_weight_denom;
238 3276 sh->chroma_offset_l0[i][1] = 0;
239 }
240 }
241
2/2
✓ Branch 0 taken 668 times.
✓ Branch 1 taken 775 times.
1443 if (sh->slice_type == HEVC_SLICE_B) {
242
2/2
✓ Branch 0 taken 1347 times.
✓ Branch 1 taken 668 times.
2015 for (i = 0; i < sh->nb_refs[L1]; i++) {
243 1347 luma_weight_l1_flag[i] = get_bits1(gb);
244
2/2
✓ Branch 0 taken 601 times.
✓ Branch 1 taken 746 times.
1347 if (!luma_weight_l1_flag[i]) {
245 601 sh->luma_weight_l1[i] = 1 << sh->luma_log2_weight_denom;
246 601 sh->luma_offset_l1[i] = 0;
247 }
248 }
249
1/2
✓ Branch 0 taken 668 times.
✗ Branch 1 not taken.
668 if (sps->chroma_format_idc != 0) {
250
2/2
✓ Branch 0 taken 1347 times.
✓ Branch 1 taken 668 times.
2015 for (i = 0; i < sh->nb_refs[L1]; i++)
251 1347 chroma_weight_l1_flag[i] = get_bits1(gb);
252 } else {
253 for (i = 0; i < sh->nb_refs[L1]; i++)
254 chroma_weight_l1_flag[i] = 0;
255 }
256
2/2
✓ Branch 0 taken 1347 times.
✓ Branch 1 taken 668 times.
2015 for (i = 0; i < sh->nb_refs[L1]; i++) {
257
2/2
✓ Branch 0 taken 746 times.
✓ Branch 1 taken 601 times.
1347 if (luma_weight_l1_flag[i]) {
258 746 int delta_luma_weight_l1 = get_se_golomb(gb);
259
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 746 times.
746 if ((int8_t)delta_luma_weight_l1 != delta_luma_weight_l1)
260 return AVERROR_INVALIDDATA;
261 746 sh->luma_weight_l1[i] = (1 << sh->luma_log2_weight_denom) + delta_luma_weight_l1;
262 746 sh->luma_offset_l1[i] = get_se_golomb(gb);
263 }
264
2/2
✓ Branch 0 taken 236 times.
✓ Branch 1 taken 1111 times.
1347 if (chroma_weight_l1_flag[i]) {
265
2/2
✓ Branch 0 taken 472 times.
✓ Branch 1 taken 236 times.
708 for (j = 0; j < 2; j++) {
266 472 int delta_chroma_weight_l1 = get_se_golomb(gb);
267 472 int delta_chroma_offset_l1 = get_se_golomb(gb);
268
269
1/2
✓ Branch 0 taken 472 times.
✗ Branch 1 not taken.
472 if ( (int8_t)delta_chroma_weight_l1 != delta_chroma_weight_l1
270
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)) {
271 return AVERROR_INVALIDDATA;
272 }
273
274 472 sh->chroma_weight_l1[i][j] = (1 << sh->chroma_log2_weight_denom) + delta_chroma_weight_l1;
275 472 sh->chroma_offset_l1[i][j] = av_clip((delta_chroma_offset_l1 - ((128 * sh->chroma_weight_l1[i][j])
276 472 >> sh->chroma_log2_weight_denom) + 128), -128, 127);
277 }
278 } else {
279 1111 sh->chroma_weight_l1[i][0] = 1 << sh->chroma_log2_weight_denom;
280 1111 sh->chroma_offset_l1[i][0] = 0;
281 1111 sh->chroma_weight_l1[i][1] = 1 << sh->chroma_log2_weight_denom;
282 1111 sh->chroma_offset_l1[i][1] = 0;
283 }
284 }
285 }
286 1443 return 0;
287 }
288
289 19673 static int decode_lt_rps(const HEVCSPS *sps, LongTermRPS *rps,
290 GetBitContext *gb, int cur_poc, int poc_lsb)
291 {
292 19673 int max_poc_lsb = 1 << sps->log2_max_poc_lsb;
293 19673 int prev_delta_msb = 0;
294 19673 unsigned int nb_sps = 0, nb_sh;
295 int i;
296
297 19673 rps->nb_refs = 0;
298
2/2
✓ Branch 0 taken 18637 times.
✓ Branch 1 taken 1036 times.
19673 if (!sps->long_term_ref_pics_present)
299 18637 return 0;
300
301
2/2
✓ Branch 0 taken 499 times.
✓ Branch 1 taken 537 times.
1036 if (sps->num_long_term_ref_pics_sps > 0)
302 499 nb_sps = get_ue_golomb_long(gb);
303 1036 nb_sh = get_ue_golomb_long(gb);
304
305
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1036 times.
1036 if (nb_sps > sps->num_long_term_ref_pics_sps)
306 return AVERROR_INVALIDDATA;
307
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1036 times.
1036 if (nb_sh + (uint64_t)nb_sps > FF_ARRAY_ELEMS(rps->poc))
308 return AVERROR_INVALIDDATA;
309
310 1036 rps->nb_refs = nb_sh + nb_sps;
311
312
2/2
✓ Branch 0 taken 1562 times.
✓ Branch 1 taken 1036 times.
2598 for (i = 0; i < rps->nb_refs; i++) {
313
314
2/2
✓ Branch 0 taken 184 times.
✓ Branch 1 taken 1378 times.
1562 if (i < nb_sps) {
315 184 uint8_t lt_idx_sps = 0;
316
317
1/2
✓ Branch 0 taken 184 times.
✗ Branch 1 not taken.
184 if (sps->num_long_term_ref_pics_sps > 1)
318 184 lt_idx_sps = get_bits(gb, av_ceil_log2(sps->num_long_term_ref_pics_sps));
319
320 184 rps->poc[i] = sps->lt_ref_pic_poc_lsb_sps[lt_idx_sps];
321 184 rps->used[i] = !!(sps->used_by_curr_pic_lt & (1U << lt_idx_sps));
322 } else {
323 1378 rps->poc[i] = get_bits(gb, sps->log2_max_poc_lsb);
324 1378 rps->used[i] = get_bits1(gb);
325 }
326
327 1562 rps->poc_msb_present[i] = get_bits1(gb);
328
2/2
✓ Branch 0 taken 48 times.
✓ Branch 1 taken 1514 times.
1562 if (rps->poc_msb_present[i]) {
329 48 int64_t delta = get_ue_golomb_long(gb);
330 int64_t poc;
331
332
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)
333 31 delta += prev_delta_msb;
334
335 48 poc = rps->poc[i] + cur_poc - delta * max_poc_lsb - poc_lsb;
336
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 48 times.
48 if (poc != (int32_t)poc)
337 return AVERROR_INVALIDDATA;
338 48 rps->poc[i] = poc;
339 48 prev_delta_msb = delta;
340 }
341 }
342
343 1036 return 0;
344 }
345
346 652 static void export_stream_params(HEVCContext *s, const HEVCSPS *sps)
347 {
348 652 AVCodecContext *avctx = s->avctx;
349 652 const HEVCVPS *vps = sps->vps;
350 652 const HEVCWindow *ow = &sps->output_window;
351 652 unsigned int num = 0, den = 0;
352
353 652 avctx->pix_fmt = sps->pix_fmt;
354 652 avctx->coded_width = sps->width;
355 652 avctx->coded_height = sps->height;
356 652 avctx->width = sps->width - ow->left_offset - ow->right_offset;
357 652 avctx->height = sps->height - ow->top_offset - ow->bottom_offset;
358 652 avctx->has_b_frames = sps->temporal_layer[sps->max_sub_layers - 1].num_reorder_pics;
359 652 avctx->profile = sps->ptl.general_ptl.profile_idc;
360 652 avctx->level = sps->ptl.general_ptl.level_idc;
361
362 652 ff_set_sar(avctx, sps->vui.common.sar);
363
364
2/2
✓ Branch 0 taken 52 times.
✓ Branch 1 taken 600 times.
652 if (sps->vui.common.video_signal_type_present_flag)
365 52 avctx->color_range = sps->vui.common.video_full_range_flag ? AVCOL_RANGE_JPEG
366
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 46 times.
52 : AVCOL_RANGE_MPEG;
367 else
368 600 avctx->color_range = AVCOL_RANGE_MPEG;
369
370
2/2
✓ Branch 0 taken 49 times.
✓ Branch 1 taken 603 times.
652 if (sps->vui.common.colour_description_present_flag) {
371 49 avctx->color_primaries = sps->vui.common.colour_primaries;
372 49 avctx->color_trc = sps->vui.common.transfer_characteristics;
373 49 avctx->colorspace = sps->vui.common.matrix_coeffs;
374 } else {
375 603 avctx->color_primaries = AVCOL_PRI_UNSPECIFIED;
376 603 avctx->color_trc = AVCOL_TRC_UNSPECIFIED;
377 603 avctx->colorspace = AVCOL_SPC_UNSPECIFIED;
378 }
379
380 652 avctx->chroma_sample_location = AVCHROMA_LOC_UNSPECIFIED;
381
2/2
✓ Branch 0 taken 611 times.
✓ Branch 1 taken 41 times.
652 if (sps->chroma_format_idc == 1) {
382
2/2
✓ Branch 0 taken 15 times.
✓ Branch 1 taken 596 times.
611 if (sps->vui.common.chroma_loc_info_present_flag) {
383
1/2
✓ Branch 0 taken 15 times.
✗ Branch 1 not taken.
15 if (sps->vui.common.chroma_sample_loc_type_top_field <= 5)
384 15 avctx->chroma_sample_location = sps->vui.common.chroma_sample_loc_type_top_field + 1;
385 } else
386 596 avctx->chroma_sample_location = AVCHROMA_LOC_LEFT;
387 }
388
389
2/2
✓ Branch 0 taken 22 times.
✓ Branch 1 taken 630 times.
652 if (vps->vps_timing_info_present_flag) {
390 22 num = vps->vps_num_units_in_tick;
391 22 den = vps->vps_time_scale;
392
2/2
✓ Branch 0 taken 52 times.
✓ Branch 1 taken 578 times.
630 } else if (sps->vui.vui_timing_info_present_flag) {
393 52 num = sps->vui.vui_num_units_in_tick;
394 52 den = sps->vui.vui_time_scale;
395 }
396
397
3/4
✓ Branch 0 taken 74 times.
✓ Branch 1 taken 578 times.
✓ Branch 2 taken 74 times.
✗ Branch 3 not taken.
652 if (num > 0 && den > 0)
398 74 av_reduce(&avctx->framerate.den, &avctx->framerate.num,
399 num, den, 1 << 30);
400 652 }
401
402 10420 static int export_stream_params_from_sei(HEVCContext *s)
403 {
404 10420 AVCodecContext *avctx = s->avctx;
405
406
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10420 times.
10420 if (s->sei.common.a53_caption.buf_ref)
407 s->avctx->properties |= FF_CODEC_PROPERTY_CLOSED_CAPTIONS;
408
409
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 10420 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
10420 if (s->sei.common.alternative_transfer.present &&
410 av_color_transfer_name(s->sei.common.alternative_transfer.preferred_transfer_characteristics) &&
411 s->sei.common.alternative_transfer.preferred_transfer_characteristics != AVCOL_TRC_UNSPECIFIED) {
412 avctx->color_trc = s->sei.common.alternative_transfer.preferred_transfer_characteristics;
413 }
414
415
1/2
✓ Branch 0 taken 10420 times.
✗ Branch 1 not taken.
10420 if (s->sei.common.film_grain_characteristics.present ||
416
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10420 times.
10420 s->sei.common.aom_film_grain.enable)
417 avctx->properties |= FF_CODEC_PROPERTY_FILM_GRAIN;
418
419 10420 return 0;
420 }
421
422 652 static int export_multilayer(HEVCContext *s, const HEVCVPS *vps)
423 {
424 652 const HEVCSEITDRDI *tdrdi = &s->sei.tdrdi;
425
426 652 av_freep(&s->view_ids_available);
427 652 s->nb_view_ids_available = 0;
428 652 av_freep(&s->view_pos_available);
429 652 s->nb_view_pos_available = 0;
430
431 // don't export anything in the trivial case (1 layer, view id=0)
432
3/4
✓ Branch 0 taken 630 times.
✓ Branch 1 taken 22 times.
✓ Branch 2 taken 630 times.
✗ Branch 3 not taken.
652 if (vps->nb_layers < 2 && !vps->view_id[0])
433 630 return 0;
434
435 22 s->view_ids_available = av_calloc(vps->nb_layers, sizeof(*s->view_ids_available));
436
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 22 times.
22 if (!s->view_ids_available)
437 return AVERROR(ENOMEM);
438
439
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 17 times.
22 if (tdrdi->num_ref_displays) {
440 5 s->view_pos_available = av_calloc(vps->nb_layers, sizeof(*s->view_pos_available));
441
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
5 if (!s->view_pos_available)
442 return AVERROR(ENOMEM);
443 }
444
445
2/2
✓ Branch 0 taken 44 times.
✓ Branch 1 taken 22 times.
66 for (int i = 0; i < vps->nb_layers; i++) {
446 44 s->view_ids_available[i] = vps->view_id[i];
447
448
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 34 times.
44 if (s->view_pos_available) {
449 10 s->view_pos_available[i] = vps->view_id[i] == tdrdi->left_view_id[0] ?
450
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 5 times.
15 AV_STEREO3D_VIEW_LEFT :
451
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
5 vps->view_id[i] == tdrdi->right_view_id[0] ?
452 AV_STEREO3D_VIEW_RIGHT : AV_STEREO3D_VIEW_UNSPEC;
453 }
454 }
455 22 s->nb_view_ids_available = vps->nb_layers;
456
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 17 times.
22 s->nb_view_pos_available = s->view_pos_available ? vps->nb_layers : 0;
457
458 22 return 0;
459 }
460
461 405 static int setup_multilayer(HEVCContext *s, const HEVCVPS *vps)
462 {
463 405 unsigned layers_active_output = 0, highest_layer;
464
465 405 s->layers_active_output = 1;
466 405 s->layers_active_decode = 1;
467
468 // nothing requested - decode base layer only
469
2/2
✓ Branch 0 taken 395 times.
✓ Branch 1 taken 10 times.
405 if (!s->nb_view_ids)
470 395 return 0;
471
472
3/4
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 8 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
10 if (s->nb_view_ids == 1 && s->view_ids[0] == -1) {
473 layers_active_output = (1 << vps->nb_layers) - 1;
474 } else {
475
2/2
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 10 times.
28 for (int i = 0; i < s->nb_view_ids; i++) {
476 18 int view_id = s->view_ids[i];
477 18 int layer_idx = -1;
478
479
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 18 times.
18 if (view_id < 0) {
480 av_log(s->avctx, AV_LOG_ERROR,
481 "Invalid view ID requested: %d\n", view_id);
482 return AVERROR(EINVAL);
483 }
484
485
1/2
✓ Branch 0 taken 26 times.
✗ Branch 1 not taken.
26 for (int j = 0; j < vps->nb_layers; j++) {
486
2/2
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 8 times.
26 if (vps->view_id[j] == view_id) {
487 18 layer_idx = j;
488 18 break;
489 }
490 }
491
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 18 times.
18 if (layer_idx < 0) {
492 av_log(s->avctx, AV_LOG_ERROR,
493 "View ID %d not present in VPS\n", view_id);
494 return AVERROR(EINVAL);
495 }
496 18 layers_active_output |= 1 << layer_idx;
497 }
498 }
499
500
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10 times.
10 if (!layers_active_output) {
501 av_log(s->avctx, AV_LOG_ERROR, "No layers selected\n");
502 return AVERROR_BUG;
503 }
504
505 10 highest_layer = ff_log2(layers_active_output);
506
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10 times.
10 if (highest_layer >= FF_ARRAY_ELEMS(s->layers)) {
507 av_log(s->avctx, AV_LOG_ERROR,
508 "Too many layers requested: %u\n", layers_active_output);
509 return AVERROR(EINVAL);
510 }
511
512 /* Assume a higher layer depends on all the lower ones.
513 * This is enforced in VPS parsing currently, this logic will need
514 * to be changed if we want to support more complex dependency structures.
515 */
516 10 s->layers_active_decode = (1 << (highest_layer + 1)) - 1;
517 10 s->layers_active_output = layers_active_output;
518
519 10 av_log(s->avctx, AV_LOG_DEBUG, "decode/output layers: %x/%x\n",
520 s->layers_active_decode, s->layers_active_output);
521
522 10 return 0;
523 }
524
525 405 static enum AVPixelFormat get_format(HEVCContext *s, const HEVCSPS *sps)
526 {
527 #define HWACCEL_MAX (CONFIG_HEVC_DXVA2_HWACCEL + \
528 CONFIG_HEVC_D3D11VA_HWACCEL * 2 + \
529 CONFIG_HEVC_D3D12VA_HWACCEL + \
530 CONFIG_HEVC_NVDEC_HWACCEL + \
531 CONFIG_HEVC_VAAPI_HWACCEL + \
532 CONFIG_HEVC_VIDEOTOOLBOX_HWACCEL + \
533 CONFIG_HEVC_VDPAU_HWACCEL + \
534 CONFIG_HEVC_VULKAN_HWACCEL)
535 405 enum AVPixelFormat pix_fmts[HWACCEL_MAX + 2], *fmt = pix_fmts;
536 int ret;
537
538
6/7
✓ Branch 0 taken 342 times.
✓ Branch 1 taken 35 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.
405 switch (sps->pix_fmt) {
539 342 case AV_PIX_FMT_YUV420P:
540 case AV_PIX_FMT_YUVJ420P:
541 #if CONFIG_HEVC_DXVA2_HWACCEL
542 *fmt++ = AV_PIX_FMT_DXVA2_VLD;
543 #endif
544 #if CONFIG_HEVC_D3D11VA_HWACCEL
545 *fmt++ = AV_PIX_FMT_D3D11VA_VLD;
546 *fmt++ = AV_PIX_FMT_D3D11;
547 #endif
548 #if CONFIG_HEVC_D3D12VA_HWACCEL
549 *fmt++ = AV_PIX_FMT_D3D12;
550 #endif
551 #if CONFIG_HEVC_VAAPI_HWACCEL
552 342 *fmt++ = AV_PIX_FMT_VAAPI;
553 #endif
554 #if CONFIG_HEVC_VDPAU_HWACCEL
555 342 *fmt++ = AV_PIX_FMT_VDPAU;
556 #endif
557 #if CONFIG_HEVC_NVDEC_HWACCEL
558 *fmt++ = AV_PIX_FMT_CUDA;
559 #endif
560 #if CONFIG_HEVC_VIDEOTOOLBOX_HWACCEL
561 *fmt++ = AV_PIX_FMT_VIDEOTOOLBOX;
562 #endif
563 #if CONFIG_HEVC_VULKAN_HWACCEL
564 *fmt++ = AV_PIX_FMT_VULKAN;
565 #endif
566 342 break;
567 35 case AV_PIX_FMT_YUV420P10:
568 #if CONFIG_HEVC_DXVA2_HWACCEL
569 *fmt++ = AV_PIX_FMT_DXVA2_VLD;
570 #endif
571 #if CONFIG_HEVC_D3D11VA_HWACCEL
572 *fmt++ = AV_PIX_FMT_D3D11VA_VLD;
573 *fmt++ = AV_PIX_FMT_D3D11;
574 #endif
575 #if CONFIG_HEVC_D3D12VA_HWACCEL
576 *fmt++ = AV_PIX_FMT_D3D12;
577 #endif
578 #if CONFIG_HEVC_VAAPI_HWACCEL
579 35 *fmt++ = AV_PIX_FMT_VAAPI;
580 #endif
581 #if CONFIG_HEVC_VIDEOTOOLBOX_HWACCEL
582 *fmt++ = AV_PIX_FMT_VIDEOTOOLBOX;
583 #endif
584 #if CONFIG_HEVC_VULKAN_HWACCEL
585 *fmt++ = AV_PIX_FMT_VULKAN;
586 #endif
587 #if CONFIG_HEVC_VDPAU_HWACCEL
588 35 *fmt++ = AV_PIX_FMT_VDPAU;
589 #endif
590 #if CONFIG_HEVC_NVDEC_HWACCEL
591 *fmt++ = AV_PIX_FMT_CUDA;
592 #endif
593 35 break;
594 4 case AV_PIX_FMT_YUV444P:
595 #if CONFIG_HEVC_VAAPI_HWACCEL
596 4 *fmt++ = AV_PIX_FMT_VAAPI;
597 #endif
598 #if CONFIG_HEVC_VDPAU_HWACCEL
599 4 *fmt++ = AV_PIX_FMT_VDPAU;
600 #endif
601 #if CONFIG_HEVC_NVDEC_HWACCEL
602 *fmt++ = AV_PIX_FMT_CUDA;
603 #endif
604 #if CONFIG_HEVC_VIDEOTOOLBOX_HWACCEL
605 *fmt++ = AV_PIX_FMT_VIDEOTOOLBOX;
606 #endif
607 #if CONFIG_HEVC_VULKAN_HWACCEL
608 *fmt++ = AV_PIX_FMT_VULKAN;
609 #endif
610 4 break;
611 12 case AV_PIX_FMT_YUV422P:
612 case AV_PIX_FMT_YUV422P10LE:
613 #if CONFIG_HEVC_VAAPI_HWACCEL
614 12 *fmt++ = AV_PIX_FMT_VAAPI;
615 #endif
616 #if CONFIG_HEVC_VIDEOTOOLBOX_HWACCEL
617 *fmt++ = AV_PIX_FMT_VIDEOTOOLBOX;
618 #endif
619 #if CONFIG_HEVC_VULKAN_HWACCEL
620 *fmt++ = AV_PIX_FMT_VULKAN;
621 #endif
622 12 break;
623 10 case AV_PIX_FMT_YUV444P10:
624 #if CONFIG_HEVC_VIDEOTOOLBOX_HWACCEL
625 *fmt++ = AV_PIX_FMT_VIDEOTOOLBOX;
626 #endif
627 /* NOTE: fallthrough */
628 case AV_PIX_FMT_YUV420P12:
629 case AV_PIX_FMT_YUV444P12:
630 #if CONFIG_HEVC_VAAPI_HWACCEL
631 10 *fmt++ = AV_PIX_FMT_VAAPI;
632 #endif
633 #if CONFIG_HEVC_VDPAU_HWACCEL
634 10 *fmt++ = AV_PIX_FMT_VDPAU;
635 #endif
636 #if CONFIG_HEVC_VULKAN_HWACCEL
637 *fmt++ = AV_PIX_FMT_VULKAN;
638 #endif
639 #if CONFIG_HEVC_NVDEC_HWACCEL
640 *fmt++ = AV_PIX_FMT_CUDA;
641 #endif
642 10 break;
643 case AV_PIX_FMT_YUV422P12:
644 #if CONFIG_HEVC_VAAPI_HWACCEL
645 *fmt++ = AV_PIX_FMT_VAAPI;
646 #endif
647 #if CONFIG_HEVC_VULKAN_HWACCEL
648 *fmt++ = AV_PIX_FMT_VULKAN;
649 #endif
650 break;
651 }
652
653 405 *fmt++ = sps->pix_fmt;
654 405 *fmt = AV_PIX_FMT_NONE;
655
656 // export multilayer information from active VPS to the caller,
657 // so it is available in get_format()
658 405 ret = export_multilayer(s, sps->vps);
659
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 405 times.
405 if (ret < 0)
660 return ret;
661
662 405 ret = ff_get_format(s->avctx, pix_fmts);
663
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 405 times.
405 if (ret < 0)
664 return ret;
665 405 s->avctx->pix_fmt = ret;
666
667 // set up multilayer decoding, if requested by caller
668 405 ret = setup_multilayer(s, sps->vps);
669
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 405 times.
405 if (ret < 0)
670 return ret;
671
672 405 return 0;
673 }
674
675 414 static int set_sps(HEVCContext *s, HEVCLayerContext *l, const HEVCSPS *sps)
676 {
677 int ret;
678
679 414 pic_arrays_free(l);
680 414 ff_refstruct_unref(&l->sps);
681 414 ff_refstruct_unref(&s->vps);
682
683
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 414 times.
414 if (!sps)
684 return 0;
685
686 414 ret = pic_arrays_init(l, sps);
687
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 414 times.
414 if (ret < 0)
688 goto fail;
689
690 414 ff_hevc_pred_init(&s->hpc, sps->bit_depth);
691 414 ff_hevc_dsp_init (&s->hevcdsp, sps->bit_depth);
692 414 ff_videodsp_init (&s->vdsp, sps->bit_depth);
693
694 414 l->sps = ff_refstruct_ref_c(sps);
695 414 s->vps = ff_refstruct_ref_c(sps->vps);
696
697 414 return 0;
698
699 fail:
700 pic_arrays_free(l);
701 ff_refstruct_unref(&l->sps);
702 return ret;
703 }
704
705 28284 static int hls_slice_header(SliceHeader *sh, const HEVCContext *s, GetBitContext *gb)
706 {
707 const HEVCPPS *pps;
708 const HEVCSPS *sps;
709 const HEVCVPS *vps;
710 unsigned pps_id, layer_idx;
711 int i, ret;
712
713 // Coded parameters
714 28284 sh->first_slice_in_pic_flag = get_bits1(gb);
715
716 28284 sh->no_output_of_prior_pics_flag = 0;
717
3/4
✓ Branch 0 taken 1448 times.
✓ Branch 1 taken 26836 times.
✓ Branch 2 taken 1448 times.
✗ Branch 3 not taken.
28284 if (IS_IRAP(s))
718 1448 sh->no_output_of_prior_pics_flag = get_bits1(gb);
719
720 28284 pps_id = get_ue_golomb_long(gb);
721
3/4
✓ Branch 0 taken 28284 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 33 times.
✓ Branch 3 taken 28251 times.
28284 if (pps_id >= HEVC_MAX_PPS_COUNT || !s->ps.pps_list[pps_id]) {
722 33 av_log(s->avctx, AV_LOG_ERROR, "PPS id out of range: %d\n", pps_id);
723 33 return AVERROR_INVALIDDATA;
724 }
725
3/4
✓ Branch 0 taken 18036 times.
✓ Branch 1 taken 10215 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 18036 times.
28251 if (!sh->first_slice_in_pic_flag && s->ps.pps_list[pps_id] != s->pps) {
726 av_log(s->avctx, AV_LOG_ERROR, "PPS changed between slices.\n");
727 return AVERROR_INVALIDDATA;
728 }
729 28251 sh->pps_id = pps_id;
730
731 28251 pps = s->ps.pps_list[pps_id];
732 28251 sps = pps->sps;
733 28251 vps = sps->vps;
734 28251 layer_idx = vps->layer_idx[s->nuh_layer_id];
735
736
4/4
✓ Branch 0 taken 479 times.
✓ Branch 1 taken 27772 times.
✓ Branch 2 taken 15 times.
✓ Branch 3 taken 464 times.
28251 if (s->nal_unit_type == HEVC_NAL_CRA_NUT && s->last_eos == 1)
737 15 sh->no_output_of_prior_pics_flag = 1;
738
739 28251 sh->dependent_slice_segment_flag = 0;
740
2/2
✓ Branch 0 taken 18036 times.
✓ Branch 1 taken 10215 times.
28251 if (!sh->first_slice_in_pic_flag) {
741 int slice_address_length;
742
743
2/2
✓ Branch 0 taken 9092 times.
✓ Branch 1 taken 8944 times.
18036 if (pps->dependent_slice_segments_enabled_flag)
744 9092 sh->dependent_slice_segment_flag = get_bits1(gb);
745
3/4
✓ Branch 0 taken 7947 times.
✓ Branch 1 taken 10089 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 7947 times.
18036 if (sh->dependent_slice_segment_flag && !s->slice_initialized) {
746 av_log(s->avctx, AV_LOG_ERROR, "Independent slice segment missing.\n");
747 return AVERROR_INVALIDDATA;
748 }
749
750 18036 slice_address_length = av_ceil_log2(sps->ctb_width *
751 18036 sps->ctb_height);
752 18036 sh->slice_segment_addr = get_bitsz(gb, slice_address_length);
753
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 18036 times.
18036 if (sh->slice_segment_addr >= sps->ctb_width * sps->ctb_height) {
754 av_log(s->avctx, AV_LOG_ERROR,
755 "Invalid slice segment address: %u.\n",
756 sh->slice_segment_addr);
757 return AVERROR_INVALIDDATA;
758 }
759
760
2/2
✓ Branch 0 taken 10089 times.
✓ Branch 1 taken 7947 times.
18036 if (!sh->dependent_slice_segment_flag) {
761 10089 sh->slice_addr = sh->slice_segment_addr;
762 }
763 } else {
764 10215 sh->slice_segment_addr = sh->slice_addr = 0;
765 }
766
767
2/2
✓ Branch 0 taken 20304 times.
✓ Branch 1 taken 7947 times.
28251 if (!sh->dependent_slice_segment_flag) {
768
2/2
✓ Branch 0 taken 1078 times.
✓ Branch 1 taken 20304 times.
21382 for (i = 0; i < pps->num_extra_slice_header_bits; i++)
769 1078 skip_bits(gb, 1); // slice_reserved_undetermined_flag[]
770
771 20304 sh->slice_type = get_ue_golomb_long(gb);
772
2/2
✓ Branch 0 taken 19069 times.
✓ Branch 1 taken 1235 times.
20304 if (!(sh->slice_type == HEVC_SLICE_I ||
773
2/2
✓ Branch 0 taken 15229 times.
✓ Branch 1 taken 3840 times.
19069 sh->slice_type == HEVC_SLICE_P ||
774
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 15229 times.
15229 sh->slice_type == HEVC_SLICE_B)) {
775 av_log(s->avctx, AV_LOG_ERROR, "Unknown slice type: %d.\n",
776 sh->slice_type);
777 return AVERROR_INVALIDDATA;
778 }
779
5/6
✓ Branch 0 taken 1059 times.
✓ Branch 1 taken 19245 times.
✓ Branch 2 taken 1059 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 75 times.
✓ Branch 5 taken 984 times.
20304 if (IS_IRAP(s) && sh->slice_type != HEVC_SLICE_I &&
780
1/2
✓ Branch 0 taken 75 times.
✗ Branch 1 not taken.
75 !pps->pps_curr_pic_ref_enabled_flag &&
781
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 75 times.
75 s->nuh_layer_id == 0) {
782 av_log(s->avctx, AV_LOG_ERROR, "Inter slices in an IRAP frame.\n");
783 return AVERROR_INVALIDDATA;
784 }
785
786 // when flag is not present, picture is inferred to be output
787 20304 sh->pic_output_flag = 1;
788
2/2
✓ Branch 0 taken 703 times.
✓ Branch 1 taken 19601 times.
20304 if (pps->output_flag_present_flag)
789 703 sh->pic_output_flag = get_bits1(gb);
790
791
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 20304 times.
20304 if (sps->separate_colour_plane)
792 sh->colour_plane_id = get_bits(gb, 2);
793
794
4/4
✓ Branch 0 taken 19692 times.
✓ Branch 1 taken 612 times.
✓ Branch 2 taken 19 times.
✓ Branch 3 taken 19673 times.
20304 if (!IS_IDR(s) ||
795
2/2
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 624 times.
631 (s->nuh_layer_id > 0 &&
796
1/2
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
7 !(vps->poc_lsb_not_present & (1 << layer_idx)))) {
797 int poc;
798
799 19680 sh->pic_order_cnt_lsb = get_bits(gb, sps->log2_max_poc_lsb);
800 19680 poc = ff_hevc_compute_poc(sps, s->poc_tid0, sh->pic_order_cnt_lsb, s->nal_unit_type);
801
3/4
✓ Branch 0 taken 9868 times.
✓ Branch 1 taken 9812 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 9868 times.
19680 if (!sh->first_slice_in_pic_flag && poc != sh->poc) {
802 av_log(s->avctx, AV_LOG_WARNING,
803 "Ignoring POC change between slices: %d -> %d\n", poc, sh->poc);
804 if (s->avctx->err_recognition & AV_EF_EXPLODE)
805 return AVERROR_INVALIDDATA;
806 poc = sh->poc;
807 }
808 19680 sh->poc = poc;
809 }
810
811
4/4
✓ Branch 0 taken 19692 times.
✓ Branch 1 taken 612 times.
✓ Branch 2 taken 19673 times.
✓ Branch 3 taken 19 times.
39977 if (!IS_IDR(s)) {
812 int pos;
813
814 19673 sh->short_term_ref_pic_set_sps_flag = get_bits1(gb);
815 19673 pos = get_bits_left(gb);
816
2/2
✓ Branch 0 taken 3499 times.
✓ Branch 1 taken 16174 times.
19673 if (!sh->short_term_ref_pic_set_sps_flag) {
817 3499 ret = ff_hevc_decode_short_term_rps(gb, s->avctx, &sh->slice_rps, sps, 1);
818
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3499 times.
3499 if (ret < 0)
819 return ret;
820
821 3499 sh->short_term_rps = &sh->slice_rps;
822 } else {
823 int numbits, rps_idx;
824
825
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 16174 times.
16174 if (!sps->nb_st_rps) {
826 av_log(s->avctx, AV_LOG_ERROR, "No ref lists in the SPS.\n");
827 return AVERROR_INVALIDDATA;
828 }
829
830 16174 numbits = av_ceil_log2(sps->nb_st_rps);
831
2/2
✓ Branch 0 taken 16146 times.
✓ Branch 1 taken 28 times.
16174 rps_idx = numbits > 0 ? get_bits(gb, numbits) : 0;
832 16174 sh->short_term_rps = &sps->st_rps[rps_idx];
833 }
834 19673 sh->short_term_ref_pic_set_size = pos - get_bits_left(gb);
835
836 19673 pos = get_bits_left(gb);
837 19673 ret = decode_lt_rps(sps, &sh->long_term_rps, gb, sh->poc, sh->pic_order_cnt_lsb);
838
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 19673 times.
19673 if (ret < 0) {
839 av_log(s->avctx, AV_LOG_WARNING, "Invalid long term RPS.\n");
840 if (s->avctx->err_recognition & AV_EF_EXPLODE)
841 return AVERROR_INVALIDDATA;
842 }
843 19673 sh->long_term_ref_pic_set_size = pos - get_bits_left(gb);
844
845
2/2
✓ Branch 0 taken 17242 times.
✓ Branch 1 taken 2431 times.
19673 if (sps->temporal_mvp_enabled)
846 17242 sh->slice_temporal_mvp_enabled_flag = get_bits1(gb);
847 else
848 2431 sh->slice_temporal_mvp_enabled_flag = 0;
849 } else {
850 631 sh->poc = 0;
851 631 sh->pic_order_cnt_lsb = 0;
852 631 sh->short_term_ref_pic_set_sps_flag = 0;
853 631 sh->short_term_ref_pic_set_size = 0;
854 631 sh->short_term_rps = NULL;
855 631 sh->long_term_ref_pic_set_size = 0;
856 631 sh->slice_temporal_mvp_enabled_flag = 0;
857 }
858
859 20304 sh->inter_layer_pred = 0;
860
2/2
✓ Branch 0 taken 277 times.
✓ Branch 1 taken 20027 times.
20304 if (s->nuh_layer_id > 0) {
861 277 int num_direct_ref_layers = vps->num_direct_ref_layers[layer_idx];
862
863
2/2
✓ Branch 0 taken 229 times.
✓ Branch 1 taken 48 times.
277 if (vps->default_ref_layers_active)
864 229 sh->inter_layer_pred = !!num_direct_ref_layers;
865
1/2
✓ Branch 0 taken 48 times.
✗ Branch 1 not taken.
48 else if (num_direct_ref_layers) {
866 48 sh->inter_layer_pred = get_bits1(gb);
867
868
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) {
869 av_log(s->avctx, AV_LOG_ERROR,
870 "NumDirectRefLayers>1 not supported\n");
871 return AVERROR_PATCHWELCOME;
872 }
873 }
874 }
875
876
2/2
✓ Branch 0 taken 16791 times.
✓ Branch 1 taken 3513 times.
20304 if (sps->sao_enabled) {
877 16791 sh->slice_sample_adaptive_offset_flag[0] = get_bits1(gb);
878
2/2
✓ Branch 0 taken 16789 times.
✓ Branch 1 taken 2 times.
16791 if (sps->chroma_format_idc) {
879 16789 sh->slice_sample_adaptive_offset_flag[1] =
880 16789 sh->slice_sample_adaptive_offset_flag[2] = get_bits1(gb);
881 }
882 } else {
883 3513 sh->slice_sample_adaptive_offset_flag[0] = 0;
884 3513 sh->slice_sample_adaptive_offset_flag[1] = 0;
885 3513 sh->slice_sample_adaptive_offset_flag[2] = 0;
886 }
887
888 20304 sh->nb_refs[L0] = sh->nb_refs[L1] = 0;
889
4/4
✓ Branch 0 taken 16464 times.
✓ Branch 1 taken 3840 times.
✓ Branch 2 taken 15229 times.
✓ Branch 3 taken 1235 times.
20304 if (sh->slice_type == HEVC_SLICE_P || sh->slice_type == HEVC_SLICE_B) {
890 int nb_refs;
891
892 19069 sh->nb_refs[L0] = pps->num_ref_idx_l0_default_active;
893
2/2
✓ Branch 0 taken 15229 times.
✓ Branch 1 taken 3840 times.
19069 if (sh->slice_type == HEVC_SLICE_B)
894 15229 sh->nb_refs[L1] = pps->num_ref_idx_l1_default_active;
895
896
2/2
✓ Branch 1 taken 4954 times.
✓ Branch 2 taken 14115 times.
19069 if (get_bits1(gb)) { // num_ref_idx_active_override_flag
897 4954 sh->nb_refs[L0] = get_ue_golomb_31(gb) + 1;
898
2/2
✓ Branch 0 taken 2776 times.
✓ Branch 1 taken 2178 times.
4954 if (sh->slice_type == HEVC_SLICE_B)
899 2776 sh->nb_refs[L1] = get_ue_golomb_31(gb) + 1;
900 }
901
2/4
✓ Branch 0 taken 19069 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 19069 times.
19069 if (sh->nb_refs[L0] >= HEVC_MAX_REFS || sh->nb_refs[L1] >= HEVC_MAX_REFS) {
902 av_log(s->avctx, AV_LOG_ERROR, "Too many refs: %d/%d.\n",
903 sh->nb_refs[L0], sh->nb_refs[L1]);
904 return AVERROR_INVALIDDATA;
905 }
906
907 19069 sh->rpl_modification_flag[0] = 0;
908 19069 sh->rpl_modification_flag[1] = 0;
909 19069 nb_refs = ff_hevc_frame_nb_refs(sh, pps, layer_idx);
910
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 19069 times.
19069 if (!nb_refs) {
911 av_log(s->avctx, AV_LOG_ERROR, "Zero refs for a frame with P or B slices.\n");
912 return AVERROR_INVALIDDATA;
913 }
914
915
4/4
✓ Branch 0 taken 1930 times.
✓ Branch 1 taken 17139 times.
✓ Branch 2 taken 1795 times.
✓ Branch 3 taken 135 times.
19069 if (pps->lists_modification_present_flag && nb_refs > 1) {
916 1795 sh->rpl_modification_flag[0] = get_bits1(gb);
917
2/2
✓ Branch 0 taken 1249 times.
✓ Branch 1 taken 546 times.
1795 if (sh->rpl_modification_flag[0]) {
918
2/2
✓ Branch 0 taken 3000 times.
✓ Branch 1 taken 1249 times.
4249 for (i = 0; i < sh->nb_refs[L0]; i++)
919 3000 sh->list_entry_lx[0][i] = get_bits(gb, av_ceil_log2(nb_refs));
920 }
921
922
1/2
✓ Branch 0 taken 1795 times.
✗ Branch 1 not taken.
1795 if (sh->slice_type == HEVC_SLICE_B) {
923 1795 sh->rpl_modification_flag[1] = get_bits1(gb);
924
2/2
✓ Branch 0 taken 652 times.
✓ Branch 1 taken 1143 times.
1795 if (sh->rpl_modification_flag[1] == 1)
925
2/2
✓ Branch 0 taken 1555 times.
✓ Branch 1 taken 652 times.
2207 for (i = 0; i < sh->nb_refs[L1]; i++)
926 1555 sh->list_entry_lx[1][i] = get_bits(gb, av_ceil_log2(nb_refs));
927 }
928 }
929
930
2/2
✓ Branch 0 taken 15229 times.
✓ Branch 1 taken 3840 times.
19069 if (sh->slice_type == HEVC_SLICE_B)
931 15229 sh->mvd_l1_zero_flag = get_bits1(gb);
932
933
2/2
✓ Branch 0 taken 16310 times.
✓ Branch 1 taken 2759 times.
19069 if (pps->cabac_init_present_flag)
934 16310 sh->cabac_init_flag = get_bits1(gb);
935 else
936 2759 sh->cabac_init_flag = 0;
937
938 19069 sh->collocated_ref_idx = 0;
939
2/2
✓ Branch 0 taken 16684 times.
✓ Branch 1 taken 2385 times.
19069 if (sh->slice_temporal_mvp_enabled_flag) {
940 16684 sh->collocated_list = L0;
941
2/2
✓ Branch 0 taken 14695 times.
✓ Branch 1 taken 1989 times.
16684 if (sh->slice_type == HEVC_SLICE_B)
942 14695 sh->collocated_list = !get_bits1(gb);
943
944
2/2
✓ Branch 0 taken 14826 times.
✓ Branch 1 taken 1858 times.
16684 if (sh->nb_refs[sh->collocated_list] > 1) {
945 14826 sh->collocated_ref_idx = get_ue_golomb_long(gb);
946
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14826 times.
14826 if (sh->collocated_ref_idx >= sh->nb_refs[sh->collocated_list]) {
947 av_log(s->avctx, AV_LOG_ERROR,
948 "Invalid collocated_ref_idx: %d.\n",
949 sh->collocated_ref_idx);
950 return AVERROR_INVALIDDATA;
951 }
952 }
953 }
954
955
4/4
✓ Branch 0 taken 1468 times.
✓ Branch 1 taken 17601 times.
✓ Branch 2 taken 693 times.
✓ Branch 3 taken 775 times.
19069 if ((pps->weighted_pred_flag && sh->slice_type == HEVC_SLICE_P) ||
956
3/4
✓ Branch 0 taken 668 times.
✓ Branch 1 taken 17626 times.
✓ Branch 2 taken 668 times.
✗ Branch 3 not taken.
18294 (pps->weighted_bipred_flag && sh->slice_type == HEVC_SLICE_B)) {
957 1443 int ret = pred_weight_table(sh, s->avctx, sps, gb);
958
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1443 times.
1443 if (ret < 0)
959 return ret;
960 }
961
962 19069 sh->max_num_merge_cand = 5 - get_ue_golomb_long(gb);
963
2/4
✓ Branch 0 taken 19069 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 19069 times.
19069 if (sh->max_num_merge_cand < 1 || sh->max_num_merge_cand > 5) {
964 av_log(s->avctx, AV_LOG_ERROR,
965 "Invalid number of merging MVP candidates: %d.\n",
966 sh->max_num_merge_cand);
967 return AVERROR_INVALIDDATA;
968 }
969
970 // Syntax in 7.3.6.1
971
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 19069 times.
19069 if (sps->motion_vector_resolution_control_idc == 2)
972 sh->use_integer_mv_flag = get_bits1(gb);
973 else
974 // Inferred to be equal to motion_vector_resolution_control_idc if not present
975 19069 sh->use_integer_mv_flag = sps->motion_vector_resolution_control_idc;
976
977 }
978
979 20304 sh->slice_qp_delta = get_se_golomb(gb);
980
981
2/2
✓ Branch 0 taken 271 times.
✓ Branch 1 taken 20033 times.
20304 if (pps->pic_slice_level_chroma_qp_offsets_present_flag) {
982 271 sh->slice_cb_qp_offset = get_se_golomb(gb);
983 271 sh->slice_cr_qp_offset = get_se_golomb(gb);
984
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 ||
985
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) {
986 av_log(s->avctx, AV_LOG_ERROR, "Invalid slice cx qp offset.\n");
987 return AVERROR_INVALIDDATA;
988 }
989 } else {
990 20033 sh->slice_cb_qp_offset = 0;
991 20033 sh->slice_cr_qp_offset = 0;
992 }
993
994
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 20304 times.
20304 if (pps->pps_slice_act_qp_offsets_present_flag) {
995 sh->slice_act_y_qp_offset = get_se_golomb(gb);
996 sh->slice_act_cb_qp_offset = get_se_golomb(gb);
997 sh->slice_act_cr_qp_offset = get_se_golomb(gb);
998 }
999
1000
2/2
✓ Branch 0 taken 43 times.
✓ Branch 1 taken 20261 times.
20304 if (pps->chroma_qp_offset_list_enabled_flag)
1001 43 sh->cu_chroma_qp_offset_enabled_flag = get_bits1(gb);
1002 else
1003 20261 sh->cu_chroma_qp_offset_enabled_flag = 0;
1004
1005
2/2
✓ Branch 0 taken 2959 times.
✓ Branch 1 taken 17345 times.
20304 if (pps->deblocking_filter_control_present_flag) {
1006 2959 int deblocking_filter_override_flag = 0;
1007
1008
2/2
✓ Branch 0 taken 696 times.
✓ Branch 1 taken 2263 times.
2959 if (pps->deblocking_filter_override_enabled_flag)
1009 696 deblocking_filter_override_flag = get_bits1(gb);
1010
1011
2/2
✓ Branch 0 taken 477 times.
✓ Branch 1 taken 2482 times.
2959 if (deblocking_filter_override_flag) {
1012 477 sh->disable_deblocking_filter_flag = get_bits1(gb);
1013
2/2
✓ Branch 0 taken 357 times.
✓ Branch 1 taken 120 times.
477 if (!sh->disable_deblocking_filter_flag) {
1014 357 int beta_offset_div2 = get_se_golomb(gb);
1015 357 int tc_offset_div2 = get_se_golomb(gb) ;
1016
3/6
✓ Branch 0 taken 357 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 357 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 357 times.
✗ Branch 5 not taken.
357 if (beta_offset_div2 < -6 || beta_offset_div2 > 6 ||
1017
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 357 times.
357 tc_offset_div2 < -6 || tc_offset_div2 > 6) {
1018 av_log(s->avctx, AV_LOG_ERROR,
1019 "Invalid deblock filter offsets: %d, %d\n",
1020 beta_offset_div2, tc_offset_div2);
1021 return AVERROR_INVALIDDATA;
1022 }
1023 357 sh->beta_offset = beta_offset_div2 * 2;
1024 357 sh->tc_offset = tc_offset_div2 * 2;
1025 }
1026 } else {
1027 2482 sh->disable_deblocking_filter_flag = pps->disable_dbf;
1028 2482 sh->beta_offset = pps->beta_offset;
1029 2482 sh->tc_offset = pps->tc_offset;
1030 }
1031 } else {
1032 17345 sh->disable_deblocking_filter_flag = 0;
1033 17345 sh->beta_offset = 0;
1034 17345 sh->tc_offset = 0;
1035 }
1036
1037
2/2
✓ Branch 0 taken 17736 times.
✓ Branch 1 taken 2568 times.
20304 if (pps->seq_loop_filter_across_slices_enabled_flag &&
1038
2/2
✓ Branch 0 taken 11732 times.
✓ Branch 1 taken 6004 times.
17736 (sh->slice_sample_adaptive_offset_flag[0] ||
1039
2/2
✓ Branch 0 taken 11714 times.
✓ Branch 1 taken 18 times.
11732 sh->slice_sample_adaptive_offset_flag[1] ||
1040
2/2
✓ Branch 0 taken 11651 times.
✓ Branch 1 taken 63 times.
11714 !sh->disable_deblocking_filter_flag)) {
1041 17673 sh->slice_loop_filter_across_slices_enabled_flag = get_bits1(gb);
1042 } else {
1043 2631 sh->slice_loop_filter_across_slices_enabled_flag = pps->seq_loop_filter_across_slices_enabled_flag;
1044 }
1045 }
1046
1047 28251 sh->num_entry_point_offsets = 0;
1048
4/4
✓ Branch 0 taken 25340 times.
✓ Branch 1 taken 2911 times.
✓ Branch 2 taken 4637 times.
✓ Branch 3 taken 20703 times.
28251 if (pps->tiles_enabled_flag || pps->entropy_coding_sync_enabled_flag) {
1049 7548 unsigned num_entry_point_offsets = get_ue_golomb_long(gb);
1050 // It would be possible to bound this tighter but this here is simpler
1051
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 7548 times.
7548 if (num_entry_point_offsets > get_bits_left(gb)) {
1052 av_log(s->avctx, AV_LOG_ERROR, "num_entry_point_offsets %d is invalid\n", num_entry_point_offsets);
1053 return AVERROR_INVALIDDATA;
1054 }
1055
1056 7548 sh->num_entry_point_offsets = num_entry_point_offsets;
1057
2/2
✓ Branch 0 taken 2074 times.
✓ Branch 1 taken 5474 times.
7548 if (sh->num_entry_point_offsets > 0) {
1058 2074 int offset_len = get_ue_golomb_long(gb) + 1;
1059
1060
2/4
✓ Branch 0 taken 2074 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 2074 times.
2074 if (offset_len < 1 || offset_len > 32) {
1061 sh->num_entry_point_offsets = 0;
1062 av_log(s->avctx, AV_LOG_ERROR, "offset_len %d is invalid\n", offset_len);
1063 return AVERROR_INVALIDDATA;
1064 }
1065
1066 2074 av_freep(&sh->entry_point_offset);
1067 2074 av_freep(&sh->offset);
1068 2074 av_freep(&sh->size);
1069 2074 sh->entry_point_offset = av_malloc_array(sh->num_entry_point_offsets, sizeof(unsigned));
1070 2074 sh->offset = av_malloc_array(sh->num_entry_point_offsets + 1, sizeof(int));
1071 2074 sh->size = av_malloc_array(sh->num_entry_point_offsets + 1, sizeof(int));
1072
3/6
✓ Branch 0 taken 2074 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2074 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 2074 times.
2074 if (!sh->entry_point_offset || !sh->offset || !sh->size) {
1073 sh->num_entry_point_offsets = 0;
1074 av_log(s->avctx, AV_LOG_ERROR, "Failed to allocate memory\n");
1075 return AVERROR(ENOMEM);
1076 }
1077
2/2
✓ Branch 0 taken 8942 times.
✓ Branch 1 taken 2074 times.
11016 for (i = 0; i < sh->num_entry_point_offsets; i++) {
1078 8942 unsigned val = get_bits_long(gb, offset_len);
1079 8942 sh->entry_point_offset[i] = val + 1; // +1; // +1 to get the size
1080 }
1081 }
1082 }
1083
1084
2/2
✓ Branch 0 taken 2769 times.
✓ Branch 1 taken 25482 times.
28251 if (pps->slice_header_extension_present_flag) {
1085 2769 unsigned int length = get_ue_golomb_long(gb);
1086
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2769 times.
2769 if (length*8LL > get_bits_left(gb)) {
1087 av_log(s->avctx, AV_LOG_ERROR, "too many slice_header_extension_data_bytes\n");
1088 return AVERROR_INVALIDDATA;
1089 }
1090
2/2
✓ Branch 0 taken 20292 times.
✓ Branch 1 taken 2769 times.
23061 for (i = 0; i < length; i++)
1091 20292 skip_bits(gb, 8); // slice_header_extension_data_byte
1092 }
1093
1094 28251 ret = get_bits1(gb);
1095
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 28251 times.
28251 if (!ret) {
1096 av_log(s->avctx, AV_LOG_ERROR, "alignment_bit_equal_to_one=0\n");
1097 return AVERROR_INVALIDDATA;
1098 }
1099 28251 sh->data_offset = align_get_bits(gb) - gb->buffer;
1100
1101 // Inferred parameters
1102 28251 sh->slice_qp = 26U + pps->pic_init_qp_minus26 + sh->slice_qp_delta;
1103
1/2
✓ Branch 0 taken 28251 times.
✗ Branch 1 not taken.
28251 if (sh->slice_qp > 51 ||
1104
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 28251 times.
28251 sh->slice_qp < -sps->qp_bd_offset) {
1105 av_log(s->avctx, AV_LOG_ERROR,
1106 "The slice_qp %d is outside the valid range "
1107 "[%d, 51].\n",
1108 sh->slice_qp,
1109 -sps->qp_bd_offset);
1110 return AVERROR_INVALIDDATA;
1111 }
1112
1113 28251 sh->slice_ctb_addr_rs = sh->slice_segment_addr;
1114
1115
2/2
✓ Branch 0 taken 7947 times.
✓ Branch 1 taken 20304 times.
28251 if (sh->dependent_slice_segment_flag &&
1116
2/4
✓ Branch 0 taken 7947 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 7947 times.
7947 (!sh->slice_ctb_addr_rs || !pps->ctb_addr_rs_to_ts[sh->slice_ctb_addr_rs])) {
1117 av_log(s->avctx, AV_LOG_ERROR, "Impossible slice segment.\n");
1118 return AVERROR_INVALIDDATA;
1119 }
1120
1121
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 28251 times.
28251 if (get_bits_left(gb) < 0) {
1122 av_log(s->avctx, AV_LOG_ERROR,
1123 "Overread slice header by %d bits\n", -get_bits_left(gb));
1124 return AVERROR_INVALIDDATA;
1125 }
1126
1127 28251 return 0;
1128 }
1129
1130 #define CTB(tab, x, y) ((tab)[(y) * sps->ctb_width + (x)])
1131
1132 #define SET_SAO(elem, value) \
1133 do { \
1134 if (!sao_merge_up_flag && !sao_merge_left_flag) \
1135 sao->elem = value; \
1136 else if (sao_merge_left_flag) \
1137 sao->elem = CTB(l->sao, rx-1, ry).elem; \
1138 else if (sao_merge_up_flag) \
1139 sao->elem = CTB(l->sao, rx, ry-1).elem; \
1140 else \
1141 sao->elem = 0; \
1142 } while (0)
1143
1144 1573720 static void hls_sao_param(HEVCLocalContext *lc, const HEVCLayerContext *l,
1145 const HEVCPPS *pps, const HEVCSPS *sps,
1146 int rx, int ry)
1147 {
1148 1573720 const HEVCContext *const s = lc->parent;
1149 1573720 int sao_merge_left_flag = 0;
1150 1573720 int sao_merge_up_flag = 0;
1151 1573720 SAOParams *sao = &CTB(l->sao, rx, ry);
1152 int c_idx, i;
1153
1154
2/2
✓ Branch 0 taken 863042 times.
✓ Branch 1 taken 710678 times.
1573720 if (s->sh.slice_sample_adaptive_offset_flag[0] ||
1155
2/2
✓ Branch 0 taken 1644 times.
✓ Branch 1 taken 861398 times.
863042 s->sh.slice_sample_adaptive_offset_flag[1]) {
1156
2/2
✓ Branch 0 taken 676534 times.
✓ Branch 1 taken 35788 times.
712322 if (rx > 0) {
1157
2/2
✓ Branch 0 taken 667999 times.
✓ Branch 1 taken 8535 times.
676534 if (lc->ctb_left_flag)
1158 667999 sao_merge_left_flag = ff_hevc_sao_merge_flag_decode(lc);
1159 }
1160
4/4
✓ Branch 0 taken 654917 times.
✓ Branch 1 taken 57405 times.
✓ Branch 2 taken 342185 times.
✓ Branch 3 taken 312732 times.
712322 if (ry > 0 && !sao_merge_left_flag) {
1161
2/2
✓ Branch 0 taken 328816 times.
✓ Branch 1 taken 13369 times.
342185 if (lc->ctb_up_flag)
1162 328816 sao_merge_up_flag = ff_hevc_sao_merge_flag_decode(lc);
1163 }
1164 }
1165
1166
4/4
✓ Branch 0 taken 6294688 times.
✓ Branch 1 taken 96 times.
✓ Branch 2 taken 4721064 times.
✓ Branch 3 taken 1573720 times.
6294784 for (c_idx = 0; c_idx < (sps->chroma_format_idc ? 3 : 1); c_idx++) {
1167
2/2
✓ Branch 0 taken 1573720 times.
✓ Branch 1 taken 3147344 times.
4721064 int log2_sao_offset_scale = c_idx == 0 ? pps->log2_sao_offset_scale_luma :
1168 3147344 pps->log2_sao_offset_scale_chroma;
1169
1170
2/2
✓ Branch 0 taken 3127126 times.
✓ Branch 1 taken 1593938 times.
4721064 if (!s->sh.slice_sample_adaptive_offset_flag[c_idx]) {
1171 3127126 sao->type_idx[c_idx] = SAO_NOT_APPLIED;
1172 3127126 continue;
1173 }
1174
1175
2/2
✓ Branch 0 taken 441630 times.
✓ Branch 1 taken 1152308 times.
1593938 if (c_idx == 2) {
1176 441630 sao->type_idx[2] = sao->type_idx[1];
1177 441630 sao->eo_class[2] = sao->eo_class[1];
1178 } else {
1179
7/8
✓ Branch 0 taken 1013958 times.
✓ Branch 1 taken 138350 times.
✓ Branch 2 taken 497980 times.
✓ Branch 3 taken 515978 times.
✓ Branch 5 taken 515978 times.
✓ Branch 6 taken 138350 times.
✓ Branch 7 taken 138350 times.
✗ Branch 8 not taken.
1152308 SET_SAO(type_idx[c_idx], ff_hevc_sao_type_idx_decode(lc));
1180 }
1181
1182
2/2
✓ Branch 0 taken 1057774 times.
✓ Branch 1 taken 536164 times.
1593938 if (sao->type_idx[c_idx] == SAO_NOT_APPLIED)
1183 1057774 continue;
1184
1185
2/2
✓ Branch 0 taken 2144656 times.
✓ Branch 1 taken 536164 times.
2680820 for (i = 0; i < 4; i++)
1186
7/8
✓ Branch 0 taken 1766928 times.
✓ Branch 1 taken 377728 times.
✓ Branch 2 taken 909988 times.
✓ Branch 3 taken 856940 times.
✓ Branch 5 taken 856940 times.
✓ Branch 6 taken 377728 times.
✓ Branch 7 taken 377728 times.
✗ Branch 8 not taken.
2144656 SET_SAO(offset_abs[c_idx][i], ff_hevc_sao_offset_abs_decode(lc, sps->bit_depth));
1187
1188
2/2
✓ Branch 0 taken 119646 times.
✓ Branch 1 taken 416518 times.
536164 if (sao->type_idx[c_idx] == SAO_BAND) {
1189
2/2
✓ Branch 0 taken 478584 times.
✓ Branch 1 taken 119646 times.
598230 for (i = 0; i < 4; i++) {
1190
2/2
✓ Branch 0 taken 358111 times.
✓ Branch 1 taken 120473 times.
478584 if (sao->offset_abs[c_idx][i]) {
1191
7/8
✓ Branch 0 taken 332770 times.
✓ Branch 1 taken 25341 times.
✓ Branch 2 taken 246136 times.
✓ Branch 3 taken 86634 times.
✓ Branch 5 taken 86634 times.
✓ Branch 6 taken 25341 times.
✓ Branch 7 taken 25341 times.
✗ Branch 8 not taken.
358111 SET_SAO(offset_sign[c_idx][i],
1192 ff_hevc_sao_offset_sign_decode(lc));
1193 } else {
1194 120473 sao->offset_sign[c_idx][i] = 0;
1195 }
1196 }
1197
7/8
✓ Branch 0 taken 109384 times.
✓ Branch 1 taken 10262 times.
✓ Branch 2 taken 79308 times.
✓ Branch 3 taken 30076 times.
✓ Branch 5 taken 30076 times.
✓ Branch 6 taken 10262 times.
✓ Branch 7 taken 10262 times.
✗ Branch 8 not taken.
119646 SET_SAO(band_position[c_idx], ff_hevc_sao_band_position_decode(lc));
1198
2/2
✓ Branch 0 taken 335144 times.
✓ Branch 1 taken 81374 times.
416518 } else if (c_idx != 2) {
1199
7/8
✓ Branch 0 taken 267336 times.
✓ Branch 1 taken 67808 times.
✓ Branch 2 taken 115628 times.
✓ Branch 3 taken 151708 times.
✓ Branch 5 taken 151708 times.
✓ Branch 6 taken 67808 times.
✓ Branch 7 taken 67808 times.
✗ Branch 8 not taken.
335144 SET_SAO(eo_class[c_idx], ff_hevc_sao_eo_class_decode(lc));
1200 }
1201
1202 // Inferred parameters
1203 536164 sao->offset_val[c_idx][0] = 0;
1204
2/2
✓ Branch 0 taken 2144656 times.
✓ Branch 1 taken 536164 times.
2680820 for (i = 0; i < 4; i++) {
1205 2144656 sao->offset_val[c_idx][i + 1] = sao->offset_abs[c_idx][i];
1206
2/2
✓ Branch 0 taken 1666072 times.
✓ Branch 1 taken 478584 times.
2144656 if (sao->type_idx[c_idx] == SAO_EDGE) {
1207
2/2
✓ Branch 0 taken 833036 times.
✓ Branch 1 taken 833036 times.
1666072 if (i > 1)
1208 833036 sao->offset_val[c_idx][i + 1] = -sao->offset_val[c_idx][i + 1];
1209
2/2
✓ Branch 0 taken 158152 times.
✓ Branch 1 taken 320432 times.
478584 } else if (sao->offset_sign[c_idx][i]) {
1210 158152 sao->offset_val[c_idx][i + 1] = -sao->offset_val[c_idx][i + 1];
1211 }
1212 2144656 sao->offset_val[c_idx][i + 1] *= 1 << log2_sao_offset_scale;
1213 }
1214 }
1215 1573720 }
1216
1217 #undef SET_SAO
1218 #undef CTB
1219
1220 384850 static int hls_cross_component_pred(HEVCLocalContext *lc, int idx)
1221 {
1222 384850 int log2_res_scale_abs_plus1 = ff_hevc_log2_res_scale_abs(lc, idx);
1223
1224
2/2
✓ Branch 0 taken 235843 times.
✓ Branch 1 taken 149007 times.
384850 if (log2_res_scale_abs_plus1 != 0) {
1225 235843 int res_scale_sign_flag = ff_hevc_res_scale_sign_flag(lc, idx);
1226 235843 lc->tu.res_scale_val = (1 << (log2_res_scale_abs_plus1 - 1)) *
1227 235843 (1 - 2 * res_scale_sign_flag);
1228 } else {
1229 149007 lc->tu.res_scale_val = 0;
1230 }
1231
1232
1233 384850 return 0;
1234 }
1235
1236 16059232 static int hls_transform_unit(HEVCLocalContext *lc,
1237 const HEVCLayerContext *l,
1238 const HEVCPPS *pps, const HEVCSPS *sps,
1239 int x0, int y0,
1240 int xBase, int yBase, int cb_xBase, int cb_yBase,
1241 int log2_cb_size, int log2_trafo_size,
1242 int blk_idx, int cbf_luma, int *cbf_cb, int *cbf_cr)
1243 {
1244 16059232 const HEVCContext *const s = lc->parent;
1245 16059232 const int log2_trafo_size_c = log2_trafo_size - sps->hshift[1];
1246 int i;
1247
1248
2/2
✓ Branch 0 taken 10175157 times.
✓ Branch 1 taken 5884075 times.
16059232 if (lc->cu.pred_mode == MODE_INTRA) {
1249 10175157 int trafo_size = 1 << log2_trafo_size;
1250 10175157 ff_hevc_set_neighbour_available(lc, x0, y0, trafo_size, trafo_size, sps->log2_ctb_size);
1251
1252 10175157 s->hpc.intra_pred[log2_trafo_size - 2](lc, pps, x0, y0, 0);
1253 }
1254
1255
6/6
✓ Branch 0 taken 5836378 times.
✓ Branch 1 taken 10222854 times.
✓ Branch 2 taken 5189985 times.
✓ Branch 3 taken 646393 times.
✓ Branch 4 taken 4745748 times.
✓ Branch 5 taken 444237 times.
16059232 if (cbf_luma || cbf_cb[0] || cbf_cr[0] ||
1256
6/6
✓ Branch 0 taken 155767 times.
✓ Branch 1 taken 4589981 times.
✓ Branch 2 taken 146778 times.
✓ Branch 3 taken 8989 times.
✓ Branch 4 taken 52826 times.
✓ Branch 5 taken 93952 times.
16121046 (sps->chroma_format_idc == 2 && (cbf_cb[1] || cbf_cr[1]))) {
1257 11375299 int scan_idx = SCAN_DIAG;
1258 11375299 int scan_idx_c = SCAN_DIAG;
1259
4/4
✓ Branch 0 taken 7663737 times.
✓ Branch 1 taken 3711562 times.
✓ Branch 2 taken 6115356 times.
✓ Branch 3 taken 1548381 times.
17490655 int cbf_chroma = cbf_cb[0] || cbf_cr[0] ||
1260
2/2
✓ Branch 0 taken 231710 times.
✓ Branch 1 taken 5883646 times.
6115356 (sps->chroma_format_idc == 2 &&
1261
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]));
1262
1263
4/4
✓ Branch 0 taken 2273880 times.
✓ Branch 1 taken 9101419 times.
✓ Branch 2 taken 661320 times.
✓ Branch 3 taken 1612560 times.
11375299 if (pps->cu_qp_delta_enabled_flag && !lc->tu.is_cu_qp_delta_coded) {
1264 661320 lc->tu.cu_qp_delta = ff_hevc_cu_qp_delta_abs(lc);
1265
2/2
✓ Branch 0 taken 374341 times.
✓ Branch 1 taken 286979 times.
661320 if (lc->tu.cu_qp_delta != 0)
1266
2/2
✓ Branch 1 taken 207150 times.
✓ Branch 2 taken 167191 times.
374341 if (ff_hevc_cu_qp_delta_sign_flag(lc) == 1)
1267 207150 lc->tu.cu_qp_delta = -lc->tu.cu_qp_delta;
1268 661320 lc->tu.is_cu_qp_delta_coded = 1;
1269
1270
2/2
✓ Branch 0 taken 661319 times.
✓ Branch 1 taken 1 times.
661320 if (lc->tu.cu_qp_delta < -(26 + sps->qp_bd_offset / 2) ||
1271
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 661319 times.
661319 lc->tu.cu_qp_delta > (25 + sps->qp_bd_offset / 2)) {
1272 1 av_log(s->avctx, AV_LOG_ERROR,
1273 "The cu_qp_delta %d is outside the valid range "
1274 "[%d, %d].\n",
1275 lc->tu.cu_qp_delta,
1276 1 -(26 + sps->qp_bd_offset / 2),
1277 1 (25 + sps->qp_bd_offset / 2));
1278 1 return AVERROR_INVALIDDATA;
1279 }
1280
1281 661319 ff_hevc_set_qPy(lc, l, pps, cb_xBase, cb_yBase, log2_cb_size);
1282 }
1283
1284
4/4
✓ Branch 0 taken 891951 times.
✓ Branch 1 taken 10483347 times.
✓ Branch 2 taken 841055 times.
✓ Branch 3 taken 50896 times.
11375298 if (s->sh.cu_chroma_qp_offset_enabled_flag && cbf_chroma &&
1285
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) {
1286 266275 int cu_chroma_qp_offset_flag = ff_hevc_cu_chroma_qp_offset_flag(lc);
1287
2/2
✓ Branch 0 taken 48966 times.
✓ Branch 1 taken 217309 times.
266275 if (cu_chroma_qp_offset_flag) {
1288 48966 int cu_chroma_qp_offset_idx = 0;
1289
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 48966 times.
48966 if (pps->chroma_qp_offset_list_len_minus1 > 0) {
1290 cu_chroma_qp_offset_idx = ff_hevc_cu_chroma_qp_offset_idx(lc, pps->chroma_qp_offset_list_len_minus1);
1291 av_log(s->avctx, AV_LOG_ERROR,
1292 "cu_chroma_qp_offset_idx not yet tested.\n");
1293 }
1294 48966 lc->tu.cu_qp_offset_cb = pps->cb_qp_offset_list[cu_chroma_qp_offset_idx];
1295 48966 lc->tu.cu_qp_offset_cr = pps->cr_qp_offset_list[cu_chroma_qp_offset_idx];
1296 } else {
1297 217309 lc->tu.cu_qp_offset_cb = 0;
1298 217309 lc->tu.cu_qp_offset_cr = 0;
1299 }
1300 266275 lc->tu.is_cu_chroma_qp_offset_coded = 1;
1301 }
1302
1303
4/4
✓ Branch 0 taken 7486771 times.
✓ Branch 1 taken 3888527 times.
✓ Branch 2 taken 6500521 times.
✓ Branch 3 taken 986250 times.
11375298 if (lc->cu.pred_mode == MODE_INTRA && log2_trafo_size < 4) {
1304
2/2
✓ Branch 0 taken 4329402 times.
✓ Branch 1 taken 2171119 times.
6500521 if (lc->tu.intra_pred_mode >= 6 &&
1305
2/2
✓ Branch 0 taken 1551675 times.
✓ Branch 1 taken 2777727 times.
4329402 lc->tu.intra_pred_mode <= 14) {
1306 1551675 scan_idx = SCAN_VERT;
1307
2/2
✓ Branch 0 taken 2142003 times.
✓ Branch 1 taken 2806843 times.
4948846 } else if (lc->tu.intra_pred_mode >= 22 &&
1308
2/2
✓ Branch 0 taken 1802346 times.
✓ Branch 1 taken 339657 times.
2142003 lc->tu.intra_pred_mode <= 30) {
1309 1802346 scan_idx = SCAN_HORIZ;
1310 }
1311
1312
2/2
✓ Branch 0 taken 3889316 times.
✓ Branch 1 taken 2611205 times.
6500521 if (lc->tu.intra_pred_mode_c >= 6 &&
1313
2/2
✓ Branch 0 taken 1463056 times.
✓ Branch 1 taken 2426260 times.
3889316 lc->tu.intra_pred_mode_c <= 14) {
1314 1463056 scan_idx_c = SCAN_VERT;
1315
2/2
✓ Branch 0 taken 2034714 times.
✓ Branch 1 taken 3002751 times.
5037465 } else if (lc->tu.intra_pred_mode_c >= 22 &&
1316
2/2
✓ Branch 0 taken 1705423 times.
✓ Branch 1 taken 329291 times.
2034714 lc->tu.intra_pred_mode_c <= 30) {
1317 1705423 scan_idx_c = SCAN_HORIZ;
1318 }
1319 }
1320
1321 11375298 lc->tu.cross_pf = 0;
1322
1323
2/2
✓ Branch 0 taken 10222853 times.
✓ Branch 1 taken 1152445 times.
11375298 if (cbf_luma)
1324 10222853 ff_hevc_hls_residual_coding(lc, pps, x0, y0, log2_trafo_size, scan_idx, 0);
1325
6/6
✓ Branch 0 taken 11375106 times.
✓ Branch 1 taken 192 times.
✓ Branch 2 taken 6287255 times.
✓ Branch 3 taken 5087851 times.
✓ Branch 4 taken 209563 times.
✓ Branch 5 taken 6077692 times.
16672712 if (sps->chroma_format_idc && (log2_trafo_size > 2 || sps->chroma_format_idc == 3)) {
1326 5297414 int trafo_size_h = 1 << (log2_trafo_size_c + sps->hshift[1]);
1327 5297414 int trafo_size_v = 1 << (log2_trafo_size_c + sps->vshift[1]);
1328
4/4
✓ Branch 0 taken 336841 times.
✓ Branch 1 taken 4960573 times.
✓ Branch 2 taken 217816 times.
✓ Branch 3 taken 119025 times.
5515230 lc->tu.cross_pf = (pps->cross_component_prediction_enabled_flag && cbf_luma &&
1329
2/2
✓ Branch 0 taken 168873 times.
✓ Branch 1 taken 48943 times.
217816 (lc->cu.pred_mode == MODE_INTER ||
1330
2/2
✓ Branch 0 taken 143482 times.
✓ Branch 1 taken 25391 times.
168873 (lc->tu.chroma_mode_c == 4)));
1331
1332
2/2
✓ Branch 0 taken 192425 times.
✓ Branch 1 taken 5104989 times.
5297414 if (lc->tu.cross_pf) {
1333 192425 hls_cross_component_pred(lc, 0);
1334 }
1335
4/4
✓ Branch 0 taken 1359606 times.
✓ Branch 1 taken 9688424 times.
✓ Branch 2 taken 5750616 times.
✓ Branch 3 taken 5297414 times.
11048030 for (i = 0; i < (sps->chroma_format_idc == 2 ? 2 : 1); i++) {
1336
2/2
✓ Branch 0 taken 3176481 times.
✓ Branch 1 taken 2574135 times.
5750616 if (lc->cu.pred_mode == MODE_INTRA) {
1337 3176481 ff_hevc_set_neighbour_available(lc, x0, y0 + (i << log2_trafo_size_c),
1338 3176481 trafo_size_h, trafo_size_v, sps->log2_ctb_size);
1339 3176481 s->hpc.intra_pred[log2_trafo_size_c - 2](lc, pps, x0, y0 + (i << log2_trafo_size_c), 1);
1340 }
1341
2/2
✓ Branch 0 taken 1560246 times.
✓ Branch 1 taken 4190370 times.
5750616 if (cbf_cb[i])
1342 1560246 ff_hevc_hls_residual_coding(lc, pps, x0, y0 + (i << log2_trafo_size_c),
1343 log2_trafo_size_c, scan_idx_c, 1);
1344 else
1345
2/2
✓ Branch 0 taken 39658 times.
✓ Branch 1 taken 4150712 times.
4190370 if (lc->tu.cross_pf) {
1346 39658 ptrdiff_t stride = s->cur_frame->f->linesize[1];
1347 39658 int hshift = sps->hshift[1];
1348 39658 int vshift = sps->vshift[1];
1349 39658 const int16_t *coeffs_y = (int16_t*)lc->edge_emu_buffer;
1350 39658 int16_t *coeffs = (int16_t*)lc->edge_emu_buffer2;
1351 39658 int size = 1 << log2_trafo_size_c;
1352
1353 39658 uint8_t *dst = &s->cur_frame->f->data[1][(y0 >> vshift) * stride +
1354 39658 ((x0 >> hshift) << sps->pixel_shift)];
1355
2/2
✓ Branch 0 taken 1831504 times.
✓ Branch 1 taken 39658 times.
1871162 for (i = 0; i < (size * size); i++) {
1356 1831504 coeffs[i] = ((lc->tu.res_scale_val * coeffs_y[i]) >> 3);
1357 }
1358 39658 s->hevcdsp.add_residual[log2_trafo_size_c-2](dst, coeffs, stride);
1359 }
1360 }
1361
1362
2/2
✓ Branch 0 taken 192425 times.
✓ Branch 1 taken 5104989 times.
5297414 if (lc->tu.cross_pf) {
1363 192425 hls_cross_component_pred(lc, 1);
1364 }
1365
4/4
✓ Branch 0 taken 1359606 times.
✓ Branch 1 taken 9688424 times.
✓ Branch 2 taken 5750616 times.
✓ Branch 3 taken 5297414 times.
11048030 for (i = 0; i < (sps->chroma_format_idc == 2 ? 2 : 1); i++) {
1366
2/2
✓ Branch 0 taken 3176481 times.
✓ Branch 1 taken 2574135 times.
5750616 if (lc->cu.pred_mode == MODE_INTRA) {
1367 3176481 ff_hevc_set_neighbour_available(lc, x0, y0 + (i << log2_trafo_size_c),
1368 3176481 trafo_size_h, trafo_size_v, sps->log2_ctb_size);
1369 3176481 s->hpc.intra_pred[log2_trafo_size_c - 2](lc, pps, x0, y0 + (i << log2_trafo_size_c), 2);
1370 }
1371
2/2
✓ Branch 0 taken 1708790 times.
✓ Branch 1 taken 4041826 times.
5750616 if (cbf_cr[i])
1372 1708790 ff_hevc_hls_residual_coding(lc, pps, x0, y0 + (i << log2_trafo_size_c),
1373 log2_trafo_size_c, scan_idx_c, 2);
1374 else
1375
2/2
✓ Branch 0 taken 75924 times.
✓ Branch 1 taken 3965902 times.
4041826 if (lc->tu.cross_pf) {
1376 75924 ptrdiff_t stride = s->cur_frame->f->linesize[2];
1377 75924 int hshift = sps->hshift[2];
1378 75924 int vshift = sps->vshift[2];
1379 75924 const int16_t *coeffs_y = (int16_t*)lc->edge_emu_buffer;
1380 75924 int16_t *coeffs = (int16_t*)lc->edge_emu_buffer2;
1381 75924 int size = 1 << log2_trafo_size_c;
1382
1383 75924 uint8_t *dst = &s->cur_frame->f->data[2][(y0 >> vshift) * stride +
1384 75924 ((x0 >> hshift) << sps->pixel_shift)];
1385
2/2
✓ Branch 0 taken 3615552 times.
✓ Branch 1 taken 75924 times.
3691476 for (i = 0; i < (size * size); i++) {
1386 3615552 coeffs[i] = ((lc->tu.res_scale_val * coeffs_y[i]) >> 3);
1387 }
1388 75924 s->hevcdsp.add_residual[log2_trafo_size_c-2](dst, coeffs, stride);
1389 }
1390 }
1391
4/4
✓ Branch 0 taken 6077692 times.
✓ Branch 1 taken 192 times.
✓ Branch 2 taken 1561978 times.
✓ Branch 3 taken 4515714 times.
6077884 } else if (sps->chroma_format_idc && blk_idx == 3) {
1392 1561978 int trafo_size_h = 1 << (log2_trafo_size + 1);
1393 1561978 int trafo_size_v = 1 << (log2_trafo_size + sps->vshift[1]);
1394
4/4
✓ Branch 0 taken 500607 times.
✓ Branch 1 taken 2790218 times.
✓ Branch 2 taken 1728847 times.
✓ Branch 3 taken 1561978 times.
3290825 for (i = 0; i < (sps->chroma_format_idc == 2 ? 2 : 1); i++) {
1395
2/2
✓ Branch 0 taken 1242036 times.
✓ Branch 1 taken 486811 times.
1728847 if (lc->cu.pred_mode == MODE_INTRA) {
1396 1242036 ff_hevc_set_neighbour_available(lc, xBase, yBase + (i << log2_trafo_size),
1397 1242036 trafo_size_h, trafo_size_v, sps->log2_ctb_size);
1398 1242036 s->hpc.intra_pred[log2_trafo_size - 2](lc, pps, xBase, yBase + (i << log2_trafo_size), 1);
1399 }
1400
2/2
✓ Branch 0 taken 652423 times.
✓ Branch 1 taken 1076424 times.
1728847 if (cbf_cb[i])
1401 652423 ff_hevc_hls_residual_coding(lc, pps, xBase, yBase + (i << log2_trafo_size),
1402 log2_trafo_size, scan_idx_c, 1);
1403 }
1404
4/4
✓ Branch 0 taken 500607 times.
✓ Branch 1 taken 2790218 times.
✓ Branch 2 taken 1728847 times.
✓ Branch 3 taken 1561978 times.
3290825 for (i = 0; i < (sps->chroma_format_idc == 2 ? 2 : 1); i++) {
1405
2/2
✓ Branch 0 taken 1242036 times.
✓ Branch 1 taken 486811 times.
1728847 if (lc->cu.pred_mode == MODE_INTRA) {
1406 1242036 ff_hevc_set_neighbour_available(lc, xBase, yBase + (i << log2_trafo_size),
1407 1242036 trafo_size_h, trafo_size_v, sps->log2_ctb_size);
1408 1242036 s->hpc.intra_pred[log2_trafo_size - 2](lc, pps, xBase, yBase + (i << log2_trafo_size), 2);
1409 }
1410
2/2
✓ Branch 0 taken 741276 times.
✓ Branch 1 taken 987571 times.
1728847 if (cbf_cr[i])
1411 741276 ff_hevc_hls_residual_coding(lc, pps, xBase, yBase + (i << log2_trafo_size),
1412 log2_trafo_size, scan_idx_c, 2);
1413 }
1414 }
1415
3/4
✓ Branch 0 taken 4683933 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2688385 times.
✓ Branch 3 taken 1995548 times.
4683933 } else if (sps->chroma_format_idc && lc->cu.pred_mode == MODE_INTRA) {
1416
4/4
✓ Branch 0 taken 1650024 times.
✓ Branch 1 taken 1038361 times.
✓ Branch 2 taken 60588 times.
✓ Branch 3 taken 1589436 times.
3787334 if (log2_trafo_size > 2 || sps->chroma_format_idc == 3) {
1417 1098949 int trafo_size_h = 1 << (log2_trafo_size_c + sps->hshift[1]);
1418 1098949 int trafo_size_v = 1 << (log2_trafo_size_c + sps->vshift[1]);
1419 1098949 ff_hevc_set_neighbour_available(lc, x0, y0, trafo_size_h, trafo_size_v,
1420 1098949 sps->log2_ctb_size);
1421 1098949 s->hpc.intra_pred[log2_trafo_size_c - 2](lc, pps, x0, y0, 1);
1422 1098949 s->hpc.intra_pred[log2_trafo_size_c - 2](lc, pps, x0, y0, 2);
1423
2/2
✓ Branch 0 taken 12467 times.
✓ Branch 1 taken 1086482 times.
1098949 if (sps->chroma_format_idc == 2) {
1424 12467 ff_hevc_set_neighbour_available(lc, x0, y0 + (1 << log2_trafo_size_c),
1425 12467 trafo_size_h, trafo_size_v, sps->log2_ctb_size);
1426 12467 s->hpc.intra_pred[log2_trafo_size_c - 2](lc, pps, x0, y0 + (1 << log2_trafo_size_c), 1);
1427 12467 s->hpc.intra_pred[log2_trafo_size_c - 2](lc, pps, x0, y0 + (1 << log2_trafo_size_c), 2);
1428 }
1429
2/2
✓ Branch 0 taken 360443 times.
✓ Branch 1 taken 1228993 times.
1589436 } else if (blk_idx == 3) {
1430 360443 int trafo_size_h = 1 << (log2_trafo_size + 1);
1431 360443 int trafo_size_v = 1 << (log2_trafo_size + sps->vshift[1]);
1432 360443 ff_hevc_set_neighbour_available(lc, xBase, yBase,
1433 360443 trafo_size_h, trafo_size_v, sps->log2_ctb_size);
1434 360443 s->hpc.intra_pred[log2_trafo_size - 2](lc, pps, xBase, yBase, 1);
1435 360443 s->hpc.intra_pred[log2_trafo_size - 2](lc, pps, xBase, yBase, 2);
1436
2/2
✓ Branch 0 taken 4196 times.
✓ Branch 1 taken 356247 times.
360443 if (sps->chroma_format_idc == 2) {
1437 4196 ff_hevc_set_neighbour_available(lc, xBase, yBase + (1 << log2_trafo_size),
1438 4196 trafo_size_h, trafo_size_v, sps->log2_ctb_size);
1439 4196 s->hpc.intra_pred[log2_trafo_size - 2](lc, pps, xBase, yBase + (1 << log2_trafo_size), 1);
1440 4196 s->hpc.intra_pred[log2_trafo_size - 2](lc, pps, xBase, yBase + (1 << log2_trafo_size), 2);
1441 }
1442 }
1443 }
1444
1445 16059231 return 0;
1446 }
1447
1448 403332 static void set_deblocking_bypass(uint8_t *is_pcm, const HEVCSPS *sps,
1449 int x0, int y0, int log2_cb_size)
1450 {
1451 403332 int cb_size = 1 << log2_cb_size;
1452 403332 int log2_min_pu_size = sps->log2_min_pu_size;
1453
1454 403332 int min_pu_width = sps->min_pu_width;
1455 403332 int x_end = FFMIN(x0 + cb_size, sps->width);
1456 403332 int y_end = FFMIN(y0 + cb_size, sps->height);
1457 int i, j;
1458
1459
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++)
1460
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++)
1461 1073856 is_pcm[i + j * min_pu_width] = 2;
1462 403332 }
1463
1464 19346139 static int hls_transform_tree(HEVCLocalContext *lc,
1465 const HEVCLayerContext *l,
1466 const HEVCPPS *pps, const HEVCSPS *sps,
1467 int x0, int y0,
1468 int xBase, int yBase, int cb_xBase, int cb_yBase,
1469 int log2_cb_size, int log2_trafo_size,
1470 int trafo_depth, int blk_idx,
1471 const int *base_cbf_cb, const int *base_cbf_cr)
1472 {
1473 19346139 const HEVCContext *const s = lc->parent;
1474 uint8_t split_transform_flag;
1475 int cbf_cb[2];
1476 int cbf_cr[2];
1477 int ret;
1478
1479 19346139 cbf_cb[0] = base_cbf_cb[0];
1480 19346139 cbf_cb[1] = base_cbf_cb[1];
1481 19346139 cbf_cr[0] = base_cbf_cr[0];
1482 19346139 cbf_cr[1] = base_cbf_cr[1];
1483
1484
2/2
✓ Branch 0 taken 6547826 times.
✓ Branch 1 taken 12798313 times.
19346139 if (lc->cu.intra_split_flag) {
1485
2/2
✓ Branch 0 taken 4936200 times.
✓ Branch 1 taken 1611626 times.
6547826 if (trafo_depth == 1) {
1486 4936200 lc->tu.intra_pred_mode = lc->pu.intra_pred_mode[blk_idx];
1487
2/2
✓ Branch 0 taken 135404 times.
✓ Branch 1 taken 4800796 times.
4936200 if (sps->chroma_format_idc == 3) {
1488 135404 lc->tu.intra_pred_mode_c = lc->pu.intra_pred_mode_c[blk_idx];
1489 135404 lc->tu.chroma_mode_c = lc->pu.chroma_mode_c[blk_idx];
1490 } else {
1491 4800796 lc->tu.intra_pred_mode_c = lc->pu.intra_pred_mode_c[0];
1492 4800796 lc->tu.chroma_mode_c = lc->pu.chroma_mode_c[0];
1493 }
1494 }
1495 } else {
1496 12798313 lc->tu.intra_pred_mode = lc->pu.intra_pred_mode[0];
1497 12798313 lc->tu.intra_pred_mode_c = lc->pu.intra_pred_mode_c[0];
1498 12798313 lc->tu.chroma_mode_c = lc->pu.chroma_mode_c[0];
1499 }
1500
1501
2/2
✓ Branch 0 taken 19221944 times.
✓ Branch 1 taken 124195 times.
19346139 if (log2_trafo_size <= sps->log2_max_trafo_size &&
1502
2/2
✓ Branch 0 taken 10212060 times.
✓ Branch 1 taken 9009884 times.
19221944 log2_trafo_size > sps->log2_min_tb_size &&
1503
2/2
✓ Branch 0 taken 8594084 times.
✓ Branch 1 taken 1617976 times.
10212060 trafo_depth < lc->cu.max_trafo_depth &&
1504
4/4
✓ Branch 0 taken 1717275 times.
✓ Branch 1 taken 6876809 times.
✓ Branch 2 taken 490612 times.
✓ Branch 3 taken 1226663 times.
8594084 !(lc->cu.intra_split_flag && trafo_depth == 0)) {
1505 7367421 split_transform_flag = ff_hevc_split_transform_flag_decode(lc, log2_trafo_size);
1506 } else {
1507 25638351 int inter_split = sps->max_transform_hierarchy_depth_inter == 0 &&
1508
2/2
✓ Branch 0 taken 836848 times.
✓ Branch 1 taken 844067 times.
1680915 lc->cu.pred_mode == MODE_INTER &&
1509
6/6
✓ Branch 0 taken 1680915 times.
✓ Branch 1 taken 10297803 times.
✓ Branch 2 taken 675549 times.
✓ Branch 3 taken 161299 times.
✓ Branch 4 taken 133801 times.
✓ Branch 5 taken 541748 times.
13659633 lc->cu.part_mode != PART_2Nx2N &&
1510 trafo_depth == 0;
1511
1512 11978718 split_transform_flag = log2_trafo_size > sps->log2_max_trafo_size ||
1513
8/8
✓ Branch 0 taken 11854523 times.
✓ Branch 1 taken 124195 times.
✓ Branch 2 taken 6049167 times.
✓ Branch 3 taken 5805356 times.
✓ Branch 4 taken 4822504 times.
✓ Branch 5 taken 1226663 times.
✓ Branch 6 taken 118638 times.
✓ Branch 7 taken 10509222 times.
11978718 (lc->cu.intra_split_flag && trafo_depth == 0) ||
1514 inter_split;
1515 }
1516
1517
6/6
✓ Branch 0 taken 19345947 times.
✓ Branch 1 taken 192 times.
✓ Branch 2 taken 8881804 times.
✓ Branch 3 taken 10464143 times.
✓ Branch 4 taken 314416 times.
✓ Branch 5 taken 8567388 times.
19346139 if (sps->chroma_format_idc && (log2_trafo_size > 2 || sps->chroma_format_idc == 3)) {
1518
4/4
✓ Branch 0 taken 4580240 times.
✓ Branch 1 taken 6198319 times.
✓ Branch 2 taken 1259672 times.
✓ Branch 3 taken 3320568 times.
10778559 if (trafo_depth == 0 || cbf_cb[0]) {
1519 7457991 cbf_cb[0] = ff_hevc_cbf_cb_cr_decode(lc, trafo_depth);
1520
6/6
✓ Branch 0 taken 598266 times.
✓ Branch 1 taken 6859725 times.
✓ Branch 2 taken 211307 times.
✓ Branch 3 taken 386959 times.
✓ Branch 4 taken 140037 times.
✓ Branch 5 taken 71270 times.
7457991 if (sps->chroma_format_idc == 2 && (!split_transform_flag || log2_trafo_size == 3)) {
1521 526996 cbf_cb[1] = ff_hevc_cbf_cb_cr_decode(lc, trafo_depth);
1522 }
1523 }
1524
1525
4/4
✓ Branch 0 taken 4580240 times.
✓ Branch 1 taken 6198319 times.
✓ Branch 2 taken 1221820 times.
✓ Branch 3 taken 3358420 times.
10778559 if (trafo_depth == 0 || cbf_cr[0]) {
1526 7420139 cbf_cr[0] = ff_hevc_cbf_cb_cr_decode(lc, trafo_depth);
1527
6/6
✓ Branch 0 taken 745306 times.
✓ Branch 1 taken 6674833 times.
✓ Branch 2 taken 259088 times.
✓ Branch 3 taken 486218 times.
✓ Branch 4 taken 175743 times.
✓ Branch 5 taken 83345 times.
7420139 if (sps->chroma_format_idc == 2 && (!split_transform_flag || log2_trafo_size == 3)) {
1528 661961 cbf_cr[1] = ff_hevc_cbf_cb_cr_decode(lc, trafo_depth);
1529 }
1530 }
1531 }
1532
1533
2/2
✓ Branch 0 taken 3286907 times.
✓ Branch 1 taken 16059232 times.
19346139 if (split_transform_flag) {
1534 3286907 const int trafo_size_split = 1 << (log2_trafo_size - 1);
1535 3286907 const int x1 = x0 + trafo_size_split;
1536 3286907 const int y1 = y0 + trafo_size_split;
1537
1538 #define SUBDIVIDE(x, y, idx) \
1539 do { \
1540 ret = hls_transform_tree(lc, l, pps, sps, \
1541 x, y, x0, y0, cb_xBase, cb_yBase, log2_cb_size, \
1542 log2_trafo_size - 1, trafo_depth + 1, idx, \
1543 cbf_cb, cbf_cr); \
1544 if (ret < 0) \
1545 return ret; \
1546 } while (0)
1547
1548
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 3286907 times.
3286907 SUBDIVIDE(x0, y0, 0);
1549
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 3286907 times.
3286907 SUBDIVIDE(x1, y0, 1);
1550
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 3286907 times.
3286907 SUBDIVIDE(x0, y1, 2);
1551
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 3286907 times.
3286907 SUBDIVIDE(x1, y1, 3);
1552
1553 #undef SUBDIVIDE
1554 } else {
1555 16059232 int min_tu_size = 1 << sps->log2_min_tb_size;
1556 16059232 int log2_min_tu_size = sps->log2_min_tb_size;
1557 16059232 int min_tu_width = sps->min_tb_width;
1558 16059232 int cbf_luma = 1;
1559
1560
4/4
✓ Branch 0 taken 5884075 times.
✓ Branch 1 taken 10175157 times.
✓ Branch 2 taken 1105689 times.
✓ Branch 3 taken 4778386 times.
16059232 if (lc->cu.pred_mode == MODE_INTRA || trafo_depth != 0 ||
1561
4/4
✓ Branch 0 taken 909623 times.
✓ Branch 1 taken 196066 times.
✓ Branch 2 taken 779586 times.
✓ Branch 3 taken 130037 times.
1105689 cbf_cb[0] || cbf_cr[0] ||
1562
6/6
✓ Branch 0 taken 24676 times.
✓ Branch 1 taken 754910 times.
✓ Branch 2 taken 23413 times.
✓ Branch 3 taken 1263 times.
✓ Branch 4 taken 11383 times.
✓ Branch 5 taken 12030 times.
779586 (sps->chroma_format_idc == 2 && (cbf_cb[1] || cbf_cr[1]))) {
1563 15292292 cbf_luma = ff_hevc_cbf_luma_decode(lc, trafo_depth);
1564 }
1565
1566 16059232 ret = hls_transform_unit(lc, l, pps, sps,
1567 x0, y0, xBase, yBase, cb_xBase, cb_yBase,
1568 log2_cb_size, log2_trafo_size,
1569 blk_idx, cbf_luma, cbf_cb, cbf_cr);
1570
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 16059231 times.
16059232 if (ret < 0)
1571 1 return ret;
1572 // TODO: store cbf_luma somewhere else
1573
2/2
✓ Branch 0 taken 10222853 times.
✓ Branch 1 taken 5836378 times.
16059231 if (cbf_luma) {
1574 int i, j;
1575
2/2
✓ Branch 0 taken 21688627 times.
✓ Branch 1 taken 10222853 times.
31911480 for (i = 0; i < (1 << log2_trafo_size); i += min_tu_size)
1576
2/2
✓ Branch 0 taken 81671243 times.
✓ Branch 1 taken 21688627 times.
103359870 for (j = 0; j < (1 << log2_trafo_size); j += min_tu_size) {
1577 81671243 int x_tu = (x0 + j) >> log2_min_tu_size;
1578 81671243 int y_tu = (y0 + i) >> log2_min_tu_size;
1579 81671243 l->cbf_luma[y_tu * min_tu_width + x_tu] = 1;
1580 }
1581 }
1582
2/2
✓ Branch 0 taken 15526411 times.
✓ Branch 1 taken 532820 times.
16059231 if (!s->sh.disable_deblocking_filter_flag) {
1583 15526411 ff_hevc_deblocking_boundary_strengths(lc, l, pps, x0, y0, log2_trafo_size);
1584
2/2
✓ Branch 0 taken 409146 times.
✓ Branch 1 taken 15117265 times.
15526411 if (pps->transquant_bypass_enable_flag &&
1585
2/2
✓ Branch 0 taken 311137 times.
✓ Branch 1 taken 98009 times.
409146 lc->cu.cu_transquant_bypass_flag)
1586 311137 set_deblocking_bypass(l->is_pcm, sps, x0, y0, log2_trafo_size);
1587 }
1588 }
1589 19346138 return 0;
1590 }
1591
1592 12433 static int hls_pcm_sample(HEVCLocalContext *lc, const HEVCLayerContext *l,
1593 const HEVCPPS *pps, int x0, int y0, int log2_cb_size)
1594 {
1595 12433 const HEVCContext *const s = lc->parent;
1596 12433 const HEVCSPS *const sps = pps->sps;
1597 GetBitContext gb;
1598 12433 int cb_size = 1 << log2_cb_size;
1599 12433 ptrdiff_t stride0 = s->cur_frame->f->linesize[0];
1600 12433 ptrdiff_t stride1 = s->cur_frame->f->linesize[1];
1601 12433 ptrdiff_t stride2 = s->cur_frame->f->linesize[2];
1602 12433 uint8_t *dst0 = &s->cur_frame->f->data[0][y0 * stride0 + (x0 << sps->pixel_shift)];
1603 12433 uint8_t *dst1 = &s->cur_frame->f->data[1][(y0 >> sps->vshift[1]) * stride1 + ((x0 >> sps->hshift[1]) << sps->pixel_shift)];
1604 12433 uint8_t *dst2 = &s->cur_frame->f->data[2][(y0 >> sps->vshift[2]) * stride2 + ((x0 >> sps->hshift[2]) << sps->pixel_shift)];
1605
1606 12433 int length = cb_size * cb_size * sps->pcm.bit_depth +
1607 12433 (((cb_size >> sps->hshift[1]) * (cb_size >> sps->vshift[1])) +
1608 12433 ((cb_size >> sps->hshift[2]) * (cb_size >> sps->vshift[2]))) *
1609 12433 sps->pcm.bit_depth_chroma;
1610 12433 const uint8_t *pcm = skip_bytes(&lc->cc, (length + 7) >> 3);
1611 int ret;
1612
1613
2/2
✓ Branch 0 taken 6937 times.
✓ Branch 1 taken 5496 times.
12433 if (!s->sh.disable_deblocking_filter_flag)
1614 6937 ff_hevc_deblocking_boundary_strengths(lc, l, pps, x0, y0, log2_cb_size);
1615
1616 12433 ret = init_get_bits(&gb, pcm, length);
1617
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12433 times.
12433 if (ret < 0)
1618 return ret;
1619
1620 12433 s->hevcdsp.put_pcm(dst0, stride0, cb_size, cb_size, &gb, sps->pcm.bit_depth);
1621
1/2
✓ Branch 0 taken 12433 times.
✗ Branch 1 not taken.
12433 if (sps->chroma_format_idc) {
1622 12433 s->hevcdsp.put_pcm(dst1, stride1,
1623 12433 cb_size >> sps->hshift[1],
1624 12433 cb_size >> sps->vshift[1],
1625 12433 &gb, sps->pcm.bit_depth_chroma);
1626 12433 s->hevcdsp.put_pcm(dst2, stride2,
1627 12433 cb_size >> sps->hshift[2],
1628 12433 cb_size >> sps->vshift[2],
1629 12433 &gb, sps->pcm.bit_depth_chroma);
1630 }
1631
1632 12433 return 0;
1633 }
1634
1635 /**
1636 * 8.5.3.2.2.1 Luma sample unidirectional interpolation process
1637 *
1638 * @param s HEVC decoding context
1639 * @param dst target buffer for block data at block position
1640 * @param dststride stride of the dst buffer
1641 * @param ref reference picture buffer at origin (0, 0)
1642 * @param mv motion vector (relative to block position) to get pixel data from
1643 * @param x_off horizontal position of block from origin (0, 0)
1644 * @param y_off vertical position of block from origin (0, 0)
1645 * @param block_w width of block
1646 * @param block_h height of block
1647 * @param luma_weight weighting factor applied to the luma prediction
1648 * @param luma_offset additive offset applied to the luma prediction value
1649 */
1650
1651 5095556 static void luma_mc_uni(HEVCLocalContext *lc,
1652 const HEVCPPS *pps, const HEVCSPS *sps,
1653 uint8_t *dst, ptrdiff_t dststride,
1654 const AVFrame *ref, const Mv *mv, int x_off, int y_off,
1655 int block_w, int block_h, int luma_weight, int luma_offset)
1656 {
1657 5095556 const HEVCContext *const s = lc->parent;
1658 5095556 const uint8_t *src = ref->data[0];
1659 5095556 ptrdiff_t srcstride = ref->linesize[0];
1660 5095556 int pic_width = sps->width;
1661 5095556 int pic_height = sps->height;
1662 5095556 int mx = mv->x & 3;
1663 5095556 int my = mv->y & 3;
1664
4/4
✓ Branch 0 taken 2067314 times.
✓ Branch 1 taken 3028242 times.
✓ Branch 2 taken 2013138 times.
✓ Branch 3 taken 54176 times.
10136936 int weight_flag = (s->sh.slice_type == HEVC_SLICE_P && pps->weighted_pred_flag) ||
1665
4/4
✓ Branch 0 taken 3028242 times.
✓ Branch 1 taken 2013138 times.
✓ Branch 2 taken 70226 times.
✓ Branch 3 taken 2958016 times.
5041380 (s->sh.slice_type == HEVC_SLICE_B && pps->weighted_bipred_flag);
1666 5095556 int idx = hevc_pel_weight[block_w];
1667
1668 5095556 x_off += mv->x >> 2;
1669 5095556 y_off += mv->y >> 2;
1670 5095556 src += y_off * srcstride + (x_off * (1 << sps->pixel_shift));
1671
1672
4/4
✓ Branch 0 taken 5036595 times.
✓ Branch 1 taken 58961 times.
✓ Branch 2 taken 4929898 times.
✓ Branch 3 taken 106697 times.
5095556 if (x_off < QPEL_EXTRA_BEFORE || y_off < QPEL_EXTRA_AFTER ||
1673
2/2
✓ Branch 0 taken 4864534 times.
✓ Branch 1 taken 65364 times.
4929898 x_off >= pic_width - block_w - QPEL_EXTRA_AFTER ||
1674
2/2
✓ Branch 0 taken 4659765 times.
✓ Branch 1 taken 204769 times.
4864534 y_off >= pic_height - block_h - QPEL_EXTRA_AFTER ||
1675
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4659765 times.
4659765 ref == s->cur_frame->f) {
1676 435791 const ptrdiff_t edge_emu_stride = EDGE_EMU_BUFFER_STRIDE << sps->pixel_shift;
1677 435791 int offset = QPEL_EXTRA_BEFORE * srcstride + (QPEL_EXTRA_BEFORE << sps->pixel_shift);
1678 435791 int buf_offset = QPEL_EXTRA_BEFORE * edge_emu_stride + (QPEL_EXTRA_BEFORE << sps->pixel_shift);
1679
1680 435791 s->vdsp.emulated_edge_mc(lc->edge_emu_buffer, src - offset,
1681 edge_emu_stride, srcstride,
1682 block_w + QPEL_EXTRA,
1683 block_h + QPEL_EXTRA,
1684 x_off - QPEL_EXTRA_BEFORE, y_off - QPEL_EXTRA_BEFORE,
1685 pic_width, pic_height);
1686 435791 src = lc->edge_emu_buffer + buf_offset;
1687 435791 srcstride = edge_emu_stride;
1688 }
1689
1690
2/2
✓ Branch 0 taken 4971154 times.
✓ Branch 1 taken 124402 times.
5095556 if (!weight_flag)
1691 4971154 s->hevcdsp.put_hevc_qpel_uni[idx][!!my][!!mx](dst, dststride, src, srcstride,
1692 block_h, mx, my, block_w);
1693 else
1694 124402 s->hevcdsp.put_hevc_qpel_uni_w[idx][!!my][!!mx](dst, dststride, src, srcstride,
1695 124402 block_h, s->sh.luma_log2_weight_denom,
1696 luma_weight, luma_offset, mx, my, block_w);
1697 5095556 }
1698
1699 /**
1700 * 8.5.3.2.2.1 Luma sample bidirectional interpolation process
1701 *
1702 * @param s HEVC decoding context
1703 * @param dst target buffer for block data at block position
1704 * @param dststride stride of the dst buffer
1705 * @param ref0 reference picture0 buffer at origin (0, 0)
1706 * @param mv0 motion vector0 (relative to block position) to get pixel data from
1707 * @param x_off horizontal position of block from origin (0, 0)
1708 * @param y_off vertical position of block from origin (0, 0)
1709 * @param block_w width of block
1710 * @param block_h height of block
1711 * @param ref1 reference picture1 buffer at origin (0, 0)
1712 * @param mv1 motion vector1 (relative to block position) to get pixel data from
1713 * @param current_mv current motion vector structure
1714 */
1715 3960193 static void luma_mc_bi(HEVCLocalContext *lc,
1716 const HEVCPPS *pps, const HEVCSPS *sps,
1717 uint8_t *dst, ptrdiff_t dststride,
1718 const AVFrame *ref0, const Mv *mv0, int x_off, int y_off,
1719 int block_w, int block_h, const AVFrame *ref1,
1720 const Mv *mv1, struct MvField *current_mv)
1721 {
1722 3960193 const HEVCContext *const s = lc->parent;
1723 3960193 ptrdiff_t src0stride = ref0->linesize[0];
1724 3960193 ptrdiff_t src1stride = ref1->linesize[0];
1725 3960193 int pic_width = sps->width;
1726 3960193 int pic_height = sps->height;
1727 3960193 int mx0 = mv0->x & 3;
1728 3960193 int my0 = mv0->y & 3;
1729 3960193 int mx1 = mv1->x & 3;
1730 3960193 int my1 = mv1->y & 3;
1731
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 3960193 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
7920386 int weight_flag = (s->sh.slice_type == HEVC_SLICE_P && pps->weighted_pred_flag) ||
1732
3/4
✓ Branch 0 taken 3960193 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 68518 times.
✓ Branch 3 taken 3891675 times.
3960193 (s->sh.slice_type == HEVC_SLICE_B && pps->weighted_bipred_flag);
1733 3960193 int x_off0 = x_off + (mv0->x >> 2);
1734 3960193 int y_off0 = y_off + (mv0->y >> 2);
1735 3960193 int x_off1 = x_off + (mv1->x >> 2);
1736 3960193 int y_off1 = y_off + (mv1->y >> 2);
1737 3960193 int idx = hevc_pel_weight[block_w];
1738
1739 3960193 const uint8_t *src0 = ref0->data[0] + y_off0 * src0stride + (int)((unsigned)x_off0 << sps->pixel_shift);
1740 3960193 const uint8_t *src1 = ref1->data[0] + y_off1 * src1stride + (int)((unsigned)x_off1 << sps->pixel_shift);
1741
1742
4/4
✓ Branch 0 taken 3903009 times.
✓ Branch 1 taken 57184 times.
✓ Branch 2 taken 3796710 times.
✓ Branch 3 taken 106299 times.
3960193 if (x_off0 < QPEL_EXTRA_BEFORE || y_off0 < QPEL_EXTRA_AFTER ||
1743
2/2
✓ Branch 0 taken 3728624 times.
✓ Branch 1 taken 68086 times.
3796710 x_off0 >= pic_width - block_w - QPEL_EXTRA_AFTER ||
1744
2/2
✓ Branch 0 taken 208159 times.
✓ Branch 1 taken 3520465 times.
3728624 y_off0 >= pic_height - block_h - QPEL_EXTRA_AFTER) {
1745 439728 const ptrdiff_t edge_emu_stride = EDGE_EMU_BUFFER_STRIDE << sps->pixel_shift;
1746 439728 int offset = QPEL_EXTRA_BEFORE * src0stride + (QPEL_EXTRA_BEFORE << sps->pixel_shift);
1747 439728 int buf_offset = QPEL_EXTRA_BEFORE * edge_emu_stride + (QPEL_EXTRA_BEFORE << sps->pixel_shift);
1748
1749 439728 s->vdsp.emulated_edge_mc(lc->edge_emu_buffer, src0 - offset,
1750 edge_emu_stride, src0stride,
1751 block_w + QPEL_EXTRA,
1752 block_h + QPEL_EXTRA,
1753 x_off0 - QPEL_EXTRA_BEFORE, y_off0 - QPEL_EXTRA_BEFORE,
1754 pic_width, pic_height);
1755 439728 src0 = lc->edge_emu_buffer + buf_offset;
1756 439728 src0stride = edge_emu_stride;
1757 }
1758
1759
4/4
✓ Branch 0 taken 3896806 times.
✓ Branch 1 taken 63387 times.
✓ Branch 2 taken 3791189 times.
✓ Branch 3 taken 105617 times.
3960193 if (x_off1 < QPEL_EXTRA_BEFORE || y_off1 < QPEL_EXTRA_AFTER ||
1760
2/2
✓ Branch 0 taken 3721188 times.
✓ Branch 1 taken 70001 times.
3791189 x_off1 >= pic_width - block_w - QPEL_EXTRA_AFTER ||
1761
2/2
✓ Branch 0 taken 211344 times.
✓ Branch 1 taken 3509844 times.
3721188 y_off1 >= pic_height - block_h - QPEL_EXTRA_AFTER) {
1762 450349 const ptrdiff_t edge_emu_stride = EDGE_EMU_BUFFER_STRIDE << sps->pixel_shift;
1763 450349 int offset = QPEL_EXTRA_BEFORE * src1stride + (QPEL_EXTRA_BEFORE << sps->pixel_shift);
1764 450349 int buf_offset = QPEL_EXTRA_BEFORE * edge_emu_stride + (QPEL_EXTRA_BEFORE << sps->pixel_shift);
1765
1766 450349 s->vdsp.emulated_edge_mc(lc->edge_emu_buffer2, src1 - offset,
1767 edge_emu_stride, src1stride,
1768 block_w + QPEL_EXTRA,
1769 block_h + QPEL_EXTRA,
1770 x_off1 - QPEL_EXTRA_BEFORE, y_off1 - QPEL_EXTRA_BEFORE,
1771 pic_width, pic_height);
1772 450349 src1 = lc->edge_emu_buffer2 + buf_offset;
1773 450349 src1stride = edge_emu_stride;
1774 }
1775
1776 3960193 s->hevcdsp.put_hevc_qpel[idx][!!my0][!!mx0](lc->tmp, src0, src0stride,
1777 block_h, mx0, my0, block_w);
1778
2/2
✓ Branch 0 taken 3891675 times.
✓ Branch 1 taken 68518 times.
3960193 if (!weight_flag)
1779 3891675 s->hevcdsp.put_hevc_qpel_bi[idx][!!my1][!!mx1](dst, dststride, src1, src1stride, lc->tmp,
1780 block_h, mx1, my1, block_w);
1781 else
1782 68518 s->hevcdsp.put_hevc_qpel_bi_w[idx][!!my1][!!mx1](dst, dststride, src1, src1stride, lc->tmp,
1783 68518 block_h, s->sh.luma_log2_weight_denom,
1784 68518 s->sh.luma_weight_l0[current_mv->ref_idx[0]],
1785 68518 s->sh.luma_weight_l1[current_mv->ref_idx[1]],
1786 68518 s->sh.luma_offset_l0[current_mv->ref_idx[0]],
1787 68518 s->sh.luma_offset_l1[current_mv->ref_idx[1]],
1788 mx1, my1, block_w);
1789
1790 3960193 }
1791
1792 /**
1793 * 8.5.3.2.2.2 Chroma sample uniprediction interpolation process
1794 *
1795 * @param s HEVC decoding context
1796 * @param dst1 target buffer for block data at block position (U plane)
1797 * @param dst2 target buffer for block data at block position (V plane)
1798 * @param dststride stride of the dst1 and dst2 buffers
1799 * @param ref reference picture buffer at origin (0, 0)
1800 * @param mv motion vector (relative to block position) to get pixel data from
1801 * @param x_off horizontal position of block from origin (0, 0)
1802 * @param y_off vertical position of block from origin (0, 0)
1803 * @param block_w width of block
1804 * @param block_h height of block
1805 * @param chroma_weight weighting factor applied to the chroma prediction
1806 * @param chroma_offset additive offset applied to the chroma prediction value
1807 */
1808
1809 10191112 static void chroma_mc_uni(HEVCLocalContext *lc,
1810 const HEVCPPS *pps, const HEVCSPS *sps,
1811 uint8_t *dst0,
1812 ptrdiff_t dststride, const uint8_t *src0, ptrdiff_t srcstride, int reflist,
1813 int x_off, int y_off, int block_w, int block_h,
1814 const struct MvField *current_mv, int chroma_weight, int chroma_offset)
1815 {
1816 10191112 const HEVCContext *const s = lc->parent;
1817 10191112 int pic_width = sps->width >> sps->hshift[1];
1818 10191112 int pic_height = sps->height >> sps->vshift[1];
1819 10191112 const Mv *mv = &current_mv->mv[reflist];
1820
4/4
✓ Branch 0 taken 4134628 times.
✓ Branch 1 taken 6056484 times.
✓ Branch 2 taken 4026276 times.
✓ Branch 3 taken 108352 times.
20273872 int weight_flag = (s->sh.slice_type == HEVC_SLICE_P && pps->weighted_pred_flag) ||
1821
4/4
✓ Branch 0 taken 6056484 times.
✓ Branch 1 taken 4026276 times.
✓ Branch 2 taken 140452 times.
✓ Branch 3 taken 5916032 times.
10082760 (s->sh.slice_type == HEVC_SLICE_B && pps->weighted_bipred_flag);
1822 10191112 int idx = hevc_pel_weight[block_w];
1823 10191112 int hshift = sps->hshift[1];
1824 10191112 int vshift = sps->vshift[1];
1825 10191112 intptr_t mx = av_zero_extend(mv->x, 2 + hshift);
1826 10191112 intptr_t my = av_zero_extend(mv->y, 2 + vshift);
1827 10191112 intptr_t _mx = mx << (1 - hshift);
1828 10191112 intptr_t _my = my << (1 - vshift);
1829
2/4
✓ Branch 0 taken 10191112 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 10191112 times.
10191112 int emu = src0 == s->cur_frame->f->data[1] || src0 == s->cur_frame->f->data[2];
1830
1831 10191112 x_off += mv->x >> (2 + hshift);
1832 10191112 y_off += mv->y >> (2 + vshift);
1833 10191112 src0 += y_off * srcstride + (x_off * (1 << sps->pixel_shift));
1834
1835
4/4
✓ Branch 0 taken 10083642 times.
✓ Branch 1 taken 107470 times.
✓ Branch 2 taken 9870108 times.
✓ Branch 3 taken 213534 times.
10191112 if (x_off < EPEL_EXTRA_BEFORE || y_off < EPEL_EXTRA_AFTER ||
1836
2/2
✓ Branch 0 taken 9739388 times.
✓ Branch 1 taken 130720 times.
9870108 x_off >= pic_width - block_w - EPEL_EXTRA_AFTER ||
1837
3/4
✓ Branch 0 taken 9329792 times.
✓ Branch 1 taken 409596 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 9329792 times.
9739388 y_off >= pic_height - block_h - EPEL_EXTRA_AFTER ||
1838 emu) {
1839 861320 const int edge_emu_stride = EDGE_EMU_BUFFER_STRIDE << sps->pixel_shift;
1840 861320 int offset0 = EPEL_EXTRA_BEFORE * (srcstride + (1 << sps->pixel_shift));
1841 861320 int buf_offset0 = EPEL_EXTRA_BEFORE *
1842 861320 (edge_emu_stride + (1 << sps->pixel_shift));
1843 861320 s->vdsp.emulated_edge_mc(lc->edge_emu_buffer, src0 - offset0,
1844 edge_emu_stride, srcstride,
1845 block_w + EPEL_EXTRA, block_h + EPEL_EXTRA,
1846 x_off - EPEL_EXTRA_BEFORE,
1847 y_off - EPEL_EXTRA_BEFORE,
1848 pic_width, pic_height);
1849
1850 861320 src0 = lc->edge_emu_buffer + buf_offset0;
1851 861320 srcstride = edge_emu_stride;
1852 }
1853
2/2
✓ Branch 0 taken 9942308 times.
✓ Branch 1 taken 248804 times.
10191112 if (!weight_flag)
1854 9942308 s->hevcdsp.put_hevc_epel_uni[idx][!!my][!!mx](dst0, dststride, src0, srcstride,
1855 block_h, _mx, _my, block_w);
1856 else
1857 248804 s->hevcdsp.put_hevc_epel_uni_w[idx][!!my][!!mx](dst0, dststride, src0, srcstride,
1858 248804 block_h, s->sh.chroma_log2_weight_denom,
1859 chroma_weight, chroma_offset, _mx, _my, block_w);
1860 10191112 }
1861
1862 /**
1863 * 8.5.3.2.2.2 Chroma sample bidirectional interpolation process
1864 *
1865 * @param s HEVC decoding context
1866 * @param dst target buffer for block data at block position
1867 * @param dststride stride of the dst buffer
1868 * @param ref0 reference picture0 buffer at origin (0, 0)
1869 * @param mv0 motion vector0 (relative to block position) to get pixel data from
1870 * @param x_off horizontal position of block from origin (0, 0)
1871 * @param y_off vertical position of block from origin (0, 0)
1872 * @param block_w width of block
1873 * @param block_h height of block
1874 * @param ref1 reference picture1 buffer at origin (0, 0)
1875 * @param mv1 motion vector1 (relative to block position) to get pixel data from
1876 * @param current_mv current motion vector structure
1877 * @param cidx chroma component(cb, cr)
1878 */
1879 7920386 static void chroma_mc_bi(HEVCLocalContext *lc,
1880 const HEVCPPS *pps, const HEVCSPS *sps,
1881 uint8_t *dst0, ptrdiff_t dststride,
1882 const AVFrame *ref0, const AVFrame *ref1,
1883 int x_off, int y_off, int block_w, int block_h, const MvField *current_mv, int cidx)
1884 {
1885 7920386 const HEVCContext *const s = lc->parent;
1886 7920386 const uint8_t *src1 = ref0->data[cidx+1];
1887 7920386 const uint8_t *src2 = ref1->data[cidx+1];
1888 7920386 ptrdiff_t src1stride = ref0->linesize[cidx+1];
1889 7920386 ptrdiff_t src2stride = ref1->linesize[cidx+1];
1890
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 7920386 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
15840772 int weight_flag = (s->sh.slice_type == HEVC_SLICE_P && pps->weighted_pred_flag) ||
1891
3/4
✓ Branch 0 taken 7920386 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 137036 times.
✓ Branch 3 taken 7783350 times.
7920386 (s->sh.slice_type == HEVC_SLICE_B && pps->weighted_bipred_flag);
1892 7920386 int pic_width = sps->width >> sps->hshift[1];
1893 7920386 int pic_height = sps->height >> sps->vshift[1];
1894 7920386 const Mv *const mv0 = &current_mv->mv[0];
1895 7920386 const Mv *const mv1 = &current_mv->mv[1];
1896 7920386 int hshift = sps->hshift[1];
1897 7920386 int vshift = sps->vshift[1];
1898
1899 7920386 intptr_t mx0 = av_zero_extend(mv0->x, 2 + hshift);
1900 7920386 intptr_t my0 = av_zero_extend(mv0->y, 2 + vshift);
1901 7920386 intptr_t mx1 = av_zero_extend(mv1->x, 2 + hshift);
1902 7920386 intptr_t my1 = av_zero_extend(mv1->y, 2 + vshift);
1903 7920386 intptr_t _mx0 = mx0 << (1 - hshift);
1904 7920386 intptr_t _my0 = my0 << (1 - vshift);
1905 7920386 intptr_t _mx1 = mx1 << (1 - hshift);
1906 7920386 intptr_t _my1 = my1 << (1 - vshift);
1907
1908 7920386 int x_off0 = x_off + (mv0->x >> (2 + hshift));
1909 7920386 int y_off0 = y_off + (mv0->y >> (2 + vshift));
1910 7920386 int x_off1 = x_off + (mv1->x >> (2 + hshift));
1911 7920386 int y_off1 = y_off + (mv1->y >> (2 + vshift));
1912 7920386 int idx = hevc_pel_weight[block_w];
1913 7920386 src1 += y_off0 * src1stride + (int)((unsigned)x_off0 << sps->pixel_shift);
1914 7920386 src2 += y_off1 * src2stride + (int)((unsigned)x_off1 << sps->pixel_shift);
1915
1916
4/4
✓ Branch 0 taken 7812248 times.
✓ Branch 1 taken 108138 times.
✓ Branch 2 taken 7599538 times.
✓ Branch 3 taken 212710 times.
7920386 if (x_off0 < EPEL_EXTRA_BEFORE || y_off0 < EPEL_EXTRA_AFTER ||
1917
2/2
✓ Branch 0 taken 7463370 times.
✓ Branch 1 taken 136168 times.
7599538 x_off0 >= pic_width - block_w - EPEL_EXTRA_AFTER ||
1918
2/2
✓ Branch 0 taken 415730 times.
✓ Branch 1 taken 7047640 times.
7463370 y_off0 >= pic_height - block_h - EPEL_EXTRA_AFTER) {
1919 872746 const int edge_emu_stride = EDGE_EMU_BUFFER_STRIDE << sps->pixel_shift;
1920 872746 int offset1 = EPEL_EXTRA_BEFORE * (src1stride + (1 << sps->pixel_shift));
1921 872746 int buf_offset1 = EPEL_EXTRA_BEFORE *
1922 872746 (edge_emu_stride + (1 << sps->pixel_shift));
1923
1924 872746 s->vdsp.emulated_edge_mc(lc->edge_emu_buffer, src1 - offset1,
1925 edge_emu_stride, src1stride,
1926 block_w + EPEL_EXTRA, block_h + EPEL_EXTRA,
1927 x_off0 - EPEL_EXTRA_BEFORE,
1928 y_off0 - EPEL_EXTRA_BEFORE,
1929 pic_width, pic_height);
1930
1931 872746 src1 = lc->edge_emu_buffer + buf_offset1;
1932 872746 src1stride = edge_emu_stride;
1933 }
1934
1935
4/4
✓ Branch 0 taken 7799008 times.
✓ Branch 1 taken 121378 times.
✓ Branch 2 taken 7588052 times.
✓ Branch 3 taken 210956 times.
7920386 if (x_off1 < EPEL_EXTRA_BEFORE || y_off1 < EPEL_EXTRA_AFTER ||
1936
2/2
✓ Branch 0 taken 7448056 times.
✓ Branch 1 taken 139996 times.
7588052 x_off1 >= pic_width - block_w - EPEL_EXTRA_AFTER ||
1937
2/2
✓ Branch 0 taken 422362 times.
✓ Branch 1 taken 7025694 times.
7448056 y_off1 >= pic_height - block_h - EPEL_EXTRA_AFTER) {
1938 894692 const int edge_emu_stride = EDGE_EMU_BUFFER_STRIDE << sps->pixel_shift;
1939 894692 int offset1 = EPEL_EXTRA_BEFORE * (src2stride + (1 << sps->pixel_shift));
1940 894692 int buf_offset1 = EPEL_EXTRA_BEFORE *
1941 894692 (edge_emu_stride + (1 << sps->pixel_shift));
1942
1943 894692 s->vdsp.emulated_edge_mc(lc->edge_emu_buffer2, src2 - offset1,
1944 edge_emu_stride, src2stride,
1945 block_w + EPEL_EXTRA, block_h + EPEL_EXTRA,
1946 x_off1 - EPEL_EXTRA_BEFORE,
1947 y_off1 - EPEL_EXTRA_BEFORE,
1948 pic_width, pic_height);
1949
1950 894692 src2 = lc->edge_emu_buffer2 + buf_offset1;
1951 894692 src2stride = edge_emu_stride;
1952 }
1953
1954 7920386 s->hevcdsp.put_hevc_epel[idx][!!my0][!!mx0](lc->tmp, src1, src1stride,
1955 block_h, _mx0, _my0, block_w);
1956
2/2
✓ Branch 0 taken 7783350 times.
✓ Branch 1 taken 137036 times.
7920386 if (!weight_flag)
1957 7783350 s->hevcdsp.put_hevc_epel_bi[idx][!!my1][!!mx1](dst0, s->cur_frame->f->linesize[cidx+1],
1958 7783350 src2, src2stride, lc->tmp,
1959 block_h, _mx1, _my1, block_w);
1960 else
1961 137036 s->hevcdsp.put_hevc_epel_bi_w[idx][!!my1][!!mx1](dst0, s->cur_frame->f->linesize[cidx+1],
1962 137036 src2, src2stride, lc->tmp,
1963 block_h,
1964 137036 s->sh.chroma_log2_weight_denom,
1965 137036 s->sh.chroma_weight_l0[current_mv->ref_idx[0]][cidx],
1966 137036 s->sh.chroma_weight_l1[current_mv->ref_idx[1]][cidx],
1967 137036 s->sh.chroma_offset_l0[current_mv->ref_idx[0]][cidx],
1968 137036 s->sh.chroma_offset_l1[current_mv->ref_idx[1]][cidx],
1969 _mx1, _my1, block_w);
1970 7920386 }
1971
1972 13015942 static void hevc_await_progress(const HEVCContext *s, const HEVCFrame *ref,
1973 const Mv *mv, int y0, int height)
1974 {
1975
2/2
✓ Branch 0 taken 111132 times.
✓ Branch 1 taken 12904810 times.
13015942 if (s->avctx->active_thread_type == FF_THREAD_FRAME ) {
1976 111132 int y = FFMAX(0, (mv->y >> 2) + y0 + height + 9);
1977
1978 111132 ff_progress_frame_await(&ref->tf, y);
1979 }
1980 13015942 }
1981
1982 2498802 static void hevc_luma_mv_mvp_mode(HEVCLocalContext *lc,
1983 const HEVCPPS *pps, const HEVCSPS *sps,
1984 int x0, int y0, int nPbW,
1985 int nPbH, int log2_cb_size, int part_idx,
1986 int merge_idx, MvField *mv)
1987 {
1988 2498802 const HEVCContext *const s = lc->parent;
1989 2498802 enum InterPredIdc inter_pred_idc = PRED_L0;
1990 int mvp_flag;
1991
1992 2498802 ff_hevc_set_neighbour_available(lc, x0, y0, nPbW, nPbH, sps->log2_ctb_size);
1993 2498802 mv->pred_flag = 0;
1994
2/2
✓ Branch 0 taken 1900740 times.
✓ Branch 1 taken 598062 times.
2498802 if (s->sh.slice_type == HEVC_SLICE_B)
1995 1900740 inter_pred_idc = ff_hevc_inter_pred_idc_decode(lc, nPbW, nPbH);
1996
1997
2/2
✓ Branch 0 taken 2091484 times.
✓ Branch 1 taken 407318 times.
2498802 if (inter_pred_idc != PRED_L1) {
1998
1/2
✓ Branch 0 taken 2091484 times.
✗ Branch 1 not taken.
2091484 if (s->sh.nb_refs[L0])
1999 2091484 mv->ref_idx[0]= ff_hevc_ref_idx_lx_decode(lc, s->sh.nb_refs[L0]);
2000
2001 2091484 mv->pred_flag = PF_L0;
2002 2091484 ff_hevc_hls_mvd_coding(lc, x0, y0, 0);
2003 2091484 mvp_flag = ff_hevc_mvp_lx_flag_decode(lc);
2004 2091484 ff_hevc_luma_mv_mvp_mode(lc, pps, x0, y0, nPbW, nPbH, log2_cb_size,
2005 part_idx, merge_idx, mv, mvp_flag, 0);
2006 2091484 mv->mv[0].x += lc->pu.mvd.x;
2007 2091484 mv->mv[0].y += lc->pu.mvd.y;
2008 }
2009
2010
2/2
✓ Branch 0 taken 829430 times.
✓ Branch 1 taken 1669372 times.
2498802 if (inter_pred_idc != PRED_L0) {
2011
1/2
✓ Branch 0 taken 829430 times.
✗ Branch 1 not taken.
829430 if (s->sh.nb_refs[L1])
2012 829430 mv->ref_idx[1]= ff_hevc_ref_idx_lx_decode(lc, s->sh.nb_refs[L1]);
2013
2014
4/4
✓ Branch 0 taken 192341 times.
✓ Branch 1 taken 637089 times.
✓ Branch 2 taken 162774 times.
✓ Branch 3 taken 29567 times.
829430 if (s->sh.mvd_l1_zero_flag == 1 && inter_pred_idc == PRED_BI) {
2015 162774 AV_ZERO32(&lc->pu.mvd);
2016 } else {
2017 666656 ff_hevc_hls_mvd_coding(lc, x0, y0, 1);
2018 }
2019
2020 829430 mv->pred_flag += PF_L1;
2021 829430 mvp_flag = ff_hevc_mvp_lx_flag_decode(lc);
2022 829430 ff_hevc_luma_mv_mvp_mode(lc, pps, x0, y0, nPbW, nPbH, log2_cb_size,
2023 part_idx, merge_idx, mv, mvp_flag, 1);
2024 829430 mv->mv[1].x += lc->pu.mvd.x;
2025 829430 mv->mv[1].y += lc->pu.mvd.y;
2026 }
2027 2498802 }
2028
2029 9055749 static void hls_prediction_unit(HEVCLocalContext *lc,
2030 const HEVCLayerContext *l,
2031 const HEVCPPS *pps, const HEVCSPS *sps,
2032 int x0, int y0, int nPbW, int nPbH,
2033 int log2_cb_size, int partIdx, int idx)
2034 {
2035 #define POS(c_idx, x, y) \
2036 &s->cur_frame->f->data[c_idx][((y) >> sps->vshift[c_idx]) * linesize[c_idx] + \
2037 (((x) >> sps->hshift[c_idx]) << sps->pixel_shift)]
2038 9055749 const HEVCContext *const s = lc->parent;
2039 9055749 int merge_idx = 0;
2040 9055749 struct MvField current_mv = {{{ 0 }}};
2041
2042 9055749 int min_pu_width = sps->min_pu_width;
2043
2044 9055749 MvField *tab_mvf = s->cur_frame->tab_mvf;
2045 9055749 const RefPicList *refPicList = s->cur_frame->refPicList;
2046 9055749 const HEVCFrame *ref0 = NULL, *ref1 = NULL;
2047 9055749 const int *linesize = s->cur_frame->f->linesize;
2048 9055749 uint8_t *dst0 = POS(0, x0, y0);
2049 9055749 uint8_t *dst1 = POS(1, x0, y0);
2050 9055749 uint8_t *dst2 = POS(2, x0, y0);
2051 9055749 int log2_min_cb_size = sps->log2_min_cb_size;
2052 9055749 int min_cb_width = sps->min_cb_width;
2053 9055749 int x_cb = x0 >> log2_min_cb_size;
2054 9055749 int y_cb = y0 >> log2_min_cb_size;
2055 int x_pu, y_pu;
2056 int i, j;
2057
2058 9055749 int skip_flag = SAMPLE_CTB(l->skip_flag, x_cb, y_cb);
2059
2060
2/2
✓ Branch 0 taken 4973963 times.
✓ Branch 1 taken 4081786 times.
9055749 if (!skip_flag)
2061 4973963 lc->pu.merge_flag = ff_hevc_merge_flag_decode(lc);
2062
2063
4/4
✓ Branch 0 taken 4973963 times.
✓ Branch 1 taken 4081786 times.
✓ Branch 2 taken 2475161 times.
✓ Branch 3 taken 2498802 times.
9055749 if (skip_flag || lc->pu.merge_flag) {
2064
2/2
✓ Branch 0 taken 6480094 times.
✓ Branch 1 taken 76853 times.
6556947 if (s->sh.max_num_merge_cand > 1)
2065 6480094 merge_idx = ff_hevc_merge_idx_decode(lc);
2066 else
2067 76853 merge_idx = 0;
2068
2069 6556947 ff_hevc_luma_mv_merge_mode(lc, pps, x0, y0, nPbW, nPbH, log2_cb_size,
2070 partIdx, merge_idx, &current_mv);
2071 } else {
2072 2498802 hevc_luma_mv_mvp_mode(lc, pps, sps, x0, y0, nPbW, nPbH, log2_cb_size,
2073 partIdx, merge_idx, &current_mv);
2074 }
2075
2076 9055749 x_pu = x0 >> sps->log2_min_pu_size;
2077 9055749 y_pu = y0 >> sps->log2_min_pu_size;
2078
2079
2/2
✓ Branch 0 taken 35407858 times.
✓ Branch 1 taken 9055749 times.
44463607 for (j = 0; j < nPbH >> sps->log2_min_pu_size; j++)
2080
2/2
✓ Branch 0 taken 233212748 times.
✓ Branch 1 taken 35407858 times.
268620606 for (i = 0; i < nPbW >> sps->log2_min_pu_size; i++)
2081 233212748 tab_mvf[(y_pu + j) * min_pu_width + x_pu + i] = current_mv;
2082
2083
2/2
✓ Branch 0 taken 8241622 times.
✓ Branch 1 taken 814127 times.
9055749 if (current_mv.pred_flag & PF_L0) {
2084 8241622 ref0 = refPicList[0].ref[current_mv.ref_idx[0]];
2085
2/4
✓ Branch 0 taken 8241622 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 8241622 times.
8241622 if (!ref0 || !ref0->f)
2086 return;
2087 8241622 hevc_await_progress(s, ref0, &current_mv.mv[0], y0, nPbH);
2088 }
2089
2/2
✓ Branch 0 taken 4774320 times.
✓ Branch 1 taken 4281429 times.
9055749 if (current_mv.pred_flag & PF_L1) {
2090 4774320 ref1 = refPicList[1].ref[current_mv.ref_idx[1]];
2091
2/4
✓ Branch 0 taken 4774320 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 4774320 times.
4774320 if (!ref1 || !ref1->f)
2092 return;
2093 4774320 hevc_await_progress(s, ref1, &current_mv.mv[1], y0, nPbH);
2094 }
2095
2096
2/2
✓ Branch 0 taken 4281429 times.
✓ Branch 1 taken 4774320 times.
9055749 if (current_mv.pred_flag == PF_L0) {
2097 4281429 int x0_c = x0 >> sps->hshift[1];
2098 4281429 int y0_c = y0 >> sps->vshift[1];
2099 4281429 int nPbW_c = nPbW >> sps->hshift[1];
2100 4281429 int nPbH_c = nPbH >> sps->vshift[1];
2101
2102 4281429 luma_mc_uni(lc, pps, sps, dst0, linesize[0], ref0->f,
2103 &current_mv.mv[0], x0, y0, nPbW, nPbH,
2104 4281429 s->sh.luma_weight_l0[current_mv.ref_idx[0]],
2105 4281429 s->sh.luma_offset_l0[current_mv.ref_idx[0]]);
2106
2107
1/2
✓ Branch 0 taken 4281429 times.
✗ Branch 1 not taken.
4281429 if (sps->chroma_format_idc) {
2108 4281429 chroma_mc_uni(lc, pps, sps, dst1, linesize[1], ref0->f->data[1], ref0->f->linesize[1],
2109 0, x0_c, y0_c, nPbW_c, nPbH_c, &current_mv,
2110 4281429 s->sh.chroma_weight_l0[current_mv.ref_idx[0]][0], s->sh.chroma_offset_l0[current_mv.ref_idx[0]][0]);
2111 4281429 chroma_mc_uni(lc, pps, sps, dst2, linesize[2], ref0->f->data[2], ref0->f->linesize[2],
2112 0, x0_c, y0_c, nPbW_c, nPbH_c, &current_mv,
2113 4281429 s->sh.chroma_weight_l0[current_mv.ref_idx[0]][1], s->sh.chroma_offset_l0[current_mv.ref_idx[0]][1]);
2114 }
2115
2/2
✓ Branch 0 taken 814127 times.
✓ Branch 1 taken 3960193 times.
4774320 } else if (current_mv.pred_flag == PF_L1) {
2116 814127 int x0_c = x0 >> sps->hshift[1];
2117 814127 int y0_c = y0 >> sps->vshift[1];
2118 814127 int nPbW_c = nPbW >> sps->hshift[1];
2119 814127 int nPbH_c = nPbH >> sps->vshift[1];
2120
2121 814127 luma_mc_uni(lc, pps, sps, dst0, linesize[0], ref1->f,
2122 &current_mv.mv[1], x0, y0, nPbW, nPbH,
2123 814127 s->sh.luma_weight_l1[current_mv.ref_idx[1]],
2124 814127 s->sh.luma_offset_l1[current_mv.ref_idx[1]]);
2125
2126
1/2
✓ Branch 0 taken 814127 times.
✗ Branch 1 not taken.
814127 if (sps->chroma_format_idc) {
2127 814127 chroma_mc_uni(lc, pps, sps, dst1, linesize[1], ref1->f->data[1], ref1->f->linesize[1],
2128 1, x0_c, y0_c, nPbW_c, nPbH_c, &current_mv,
2129 814127 s->sh.chroma_weight_l1[current_mv.ref_idx[1]][0], s->sh.chroma_offset_l1[current_mv.ref_idx[1]][0]);
2130
2131 814127 chroma_mc_uni(lc, pps, sps, dst2, linesize[2], ref1->f->data[2], ref1->f->linesize[2],
2132 1, x0_c, y0_c, nPbW_c, nPbH_c, &current_mv,
2133 814127 s->sh.chroma_weight_l1[current_mv.ref_idx[1]][1], s->sh.chroma_offset_l1[current_mv.ref_idx[1]][1]);
2134 }
2135
1/2
✓ Branch 0 taken 3960193 times.
✗ Branch 1 not taken.
3960193 } else if (current_mv.pred_flag == PF_BI) {
2136 3960193 int x0_c = x0 >> sps->hshift[1];
2137 3960193 int y0_c = y0 >> sps->vshift[1];
2138 3960193 int nPbW_c = nPbW >> sps->hshift[1];
2139 3960193 int nPbH_c = nPbH >> sps->vshift[1];
2140
2141 3960193 luma_mc_bi(lc, pps, sps, dst0, linesize[0], ref0->f,
2142 &current_mv.mv[0], x0, y0, nPbW, nPbH,
2143 3960193 ref1->f, &current_mv.mv[1], &current_mv);
2144
2145
1/2
✓ Branch 0 taken 3960193 times.
✗ Branch 1 not taken.
3960193 if (sps->chroma_format_idc) {
2146 3960193 chroma_mc_bi(lc, pps, sps, dst1, linesize[1], ref0->f, ref1->f,
2147 x0_c, y0_c, nPbW_c, nPbH_c, &current_mv, 0);
2148
2149 3960193 chroma_mc_bi(lc, pps, sps, dst2, linesize[2], ref0->f, ref1->f,
2150 x0_c, y0_c, nPbW_c, nPbH_c, &current_mv, 1);
2151 }
2152 }
2153 }
2154
2155 /**
2156 * 8.4.1
2157 */
2158 7937817 static int luma_intra_pred_mode(HEVCLocalContext *lc, const HEVCLayerContext *l,
2159 const HEVCSPS *sps,
2160 int x0, int y0, int pu_size,
2161 int prev_intra_luma_pred_flag)
2162 {
2163 7937817 const HEVCContext *const s = lc->parent;
2164 7937817 int x_pu = x0 >> sps->log2_min_pu_size;
2165 7937817 int y_pu = y0 >> sps->log2_min_pu_size;
2166 7937817 int min_pu_width = sps->min_pu_width;
2167 7937817 int size_in_pus = pu_size >> sps->log2_min_pu_size;
2168 7937817 int x0b = av_zero_extend(x0, sps->log2_ctb_size);
2169 7937817 int y0b = av_zero_extend(y0, sps->log2_ctb_size);
2170
2171
2/2
✓ Branch 0 taken 1064812 times.
✓ Branch 1 taken 124209 times.
1189021 int cand_up = (lc->ctb_up_flag || y0b) ?
2172
2/2
✓ Branch 0 taken 1189021 times.
✓ Branch 1 taken 6748796 times.
9126838 l->tab_ipm[(y_pu - 1) * min_pu_width + x_pu] : INTRA_DC;
2173
2/2
✓ Branch 0 taken 589762 times.
✓ Branch 1 taken 80835 times.
670597 int cand_left = (lc->ctb_left_flag || x0b) ?
2174
2/2
✓ Branch 0 taken 670597 times.
✓ Branch 1 taken 7267220 times.
8608414 l->tab_ipm[y_pu * min_pu_width + x_pu - 1] : INTRA_DC;
2175
2176 7937817 int y_ctb = (y0 >> (sps->log2_ctb_size)) << (sps->log2_ctb_size);
2177
2178 7937817 MvField *tab_mvf = s->cur_frame->tab_mvf;
2179 int intra_pred_mode;
2180 int candidate[3];
2181 int i, j;
2182
2183 // intra_pred_mode prediction does not cross vertical CTB boundaries
2184
2/2
✓ Branch 0 taken 1033091 times.
✓ Branch 1 taken 6904726 times.
7937817 if ((y0 - 1) < y_ctb)
2185 1033091 cand_up = INTRA_DC;
2186
2187
2/2
✓ Branch 0 taken 1672482 times.
✓ Branch 1 taken 6265335 times.
7937817 if (cand_left == cand_up) {
2188
2/2
✓ Branch 0 taken 990780 times.
✓ Branch 1 taken 681702 times.
1672482 if (cand_left < 2) {
2189 990780 candidate[0] = INTRA_PLANAR;
2190 990780 candidate[1] = INTRA_DC;
2191 990780 candidate[2] = INTRA_ANGULAR_26;
2192 } else {
2193 681702 candidate[0] = cand_left;
2194 681702 candidate[1] = 2 + ((cand_left - 2 - 1 + 32) & 31);
2195 681702 candidate[2] = 2 + ((cand_left - 2 + 1) & 31);
2196 }
2197 } else {
2198 6265335 candidate[0] = cand_left;
2199 6265335 candidate[1] = cand_up;
2200
4/4
✓ Branch 0 taken 5196254 times.
✓ Branch 1 taken 1069081 times.
✓ Branch 2 taken 4288799 times.
✓ Branch 3 taken 907455 times.
6265335 if (candidate[0] != INTRA_PLANAR && candidate[1] != INTRA_PLANAR) {
2201 4288799 candidate[2] = INTRA_PLANAR;
2202
4/4
✓ Branch 0 taken 1727771 times.
✓ Branch 1 taken 248765 times.
✓ Branch 2 taken 1301443 times.
✓ Branch 3 taken 426328 times.
1976536 } else if (candidate[0] != INTRA_DC && candidate[1] != INTRA_DC) {
2203 1301443 candidate[2] = INTRA_DC;
2204 } else {
2205 675093 candidate[2] = INTRA_ANGULAR_26;
2206 }
2207 }
2208
2209
2/2
✓ Branch 0 taken 4775607 times.
✓ Branch 1 taken 3162210 times.
7937817 if (prev_intra_luma_pred_flag) {
2210 4775607 intra_pred_mode = candidate[lc->pu.mpm_idx];
2211 } else {
2212
2/2
✓ Branch 0 taken 1508646 times.
✓ Branch 1 taken 1653564 times.
3162210 if (candidate[0] > candidate[1])
2213 1508646 FFSWAP(uint8_t, candidate[0], candidate[1]);
2214
2/2
✓ Branch 0 taken 1875221 times.
✓ Branch 1 taken 1286989 times.
3162210 if (candidate[0] > candidate[2])
2215 1875221 FFSWAP(uint8_t, candidate[0], candidate[2]);
2216
2/2
✓ Branch 0 taken 2353015 times.
✓ Branch 1 taken 809195 times.
3162210 if (candidate[1] > candidate[2])
2217 2353015 FFSWAP(uint8_t, candidate[1], candidate[2]);
2218
2219 3162210 intra_pred_mode = lc->pu.rem_intra_luma_pred_mode;
2220
2/2
✓ Branch 0 taken 9486630 times.
✓ Branch 1 taken 3162210 times.
12648840 for (i = 0; i < 3; i++)
2221
2/2
✓ Branch 0 taken 6910867 times.
✓ Branch 1 taken 2575763 times.
9486630 if (intra_pred_mode >= candidate[i])
2222 6910867 intra_pred_mode++;
2223 }
2224
2225 /* write the intra prediction units into the mv array */
2226
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7937817 times.
7937817 if (!size_in_pus)
2227 size_in_pus = 1;
2228
2/2
✓ Branch 0 taken 14343066 times.
✓ Branch 1 taken 7937817 times.
22280883 for (i = 0; i < size_in_pus; i++) {
2229 14343066 memset(&l->tab_ipm[(y_pu + i) * min_pu_width + x_pu],
2230 intra_pred_mode, size_in_pus);
2231
2232
2/2
✓ Branch 0 taken 47697996 times.
✓ Branch 1 taken 14343066 times.
62041062 for (j = 0; j < size_in_pus; j++) {
2233 47697996 tab_mvf[(y_pu + j) * min_pu_width + x_pu + i].pred_flag = PF_INTRA;
2234 }
2235 }
2236
2237 7937817 return intra_pred_mode;
2238 }
2239
2240 11662466 static av_always_inline void set_ct_depth(const HEVCSPS *sps, uint8_t *tab_ct_depth,
2241 int x0, int y0,
2242 int log2_cb_size, int ct_depth)
2243 {
2244 11662466 int length = (1 << log2_cb_size) >> sps->log2_min_cb_size;
2245 11662466 int x_cb = x0 >> sps->log2_min_cb_size;
2246 11662466 int y_cb = y0 >> sps->log2_min_cb_size;
2247 int y;
2248
2249
2/2
✓ Branch 0 taken 22049211 times.
✓ Branch 1 taken 11662466 times.
33711677 for (y = 0; y < length; y++)
2250 22049211 memset(&tab_ct_depth[(y_cb + y) * sps->min_cb_width + x_cb],
2251 ct_depth, length);
2252 11662466 }
2253
2254 static const uint8_t tab_mode_idx[] = {
2255 0, 1, 2, 2, 2, 2, 3, 5, 7, 8, 10, 12, 13, 15, 17, 18, 19, 20,
2256 21, 22, 23, 23, 24, 24, 25, 25, 26, 27, 27, 28, 28, 29, 29, 30, 31};
2257
2258 4235667 static void intra_prediction_unit(HEVCLocalContext *lc,
2259 const HEVCLayerContext *l, const HEVCSPS *sps,
2260 int x0, int y0,
2261 int log2_cb_size)
2262 {
2263 static const uint8_t intra_chroma_table[4] = { 0, 26, 10, 1 };
2264 uint8_t prev_intra_luma_pred_flag[4];
2265 4235667 int split = lc->cu.part_mode == PART_NxN;
2266 4235667 int pb_size = (1 << log2_cb_size) >> split;
2267 4235667 int side = split + 1;
2268 int chroma_mode;
2269 int i, j;
2270
2271
2/2
✓ Branch 0 taken 5469717 times.
✓ Branch 1 taken 4235667 times.
9705384 for (i = 0; i < side; i++)
2272
2/2
✓ Branch 0 taken 7937817 times.
✓ Branch 1 taken 5469717 times.
13407534 for (j = 0; j < side; j++)
2273 7937817 prev_intra_luma_pred_flag[2 * i + j] = ff_hevc_prev_intra_luma_pred_flag_decode(lc);
2274
2275
2/2
✓ Branch 0 taken 5469717 times.
✓ Branch 1 taken 4235667 times.
9705384 for (i = 0; i < side; i++) {
2276
2/2
✓ Branch 0 taken 7937817 times.
✓ Branch 1 taken 5469717 times.
13407534 for (j = 0; j < side; j++) {
2277
2/2
✓ Branch 0 taken 4775607 times.
✓ Branch 1 taken 3162210 times.
7937817 if (prev_intra_luma_pred_flag[2 * i + j])
2278 4775607 lc->pu.mpm_idx = ff_hevc_mpm_idx_decode(lc);
2279 else
2280 3162210 lc->pu.rem_intra_luma_pred_mode = ff_hevc_rem_intra_luma_pred_mode_decode(lc);
2281
2282 7937817 lc->pu.intra_pred_mode[2 * i + j] =
2283 7937817 luma_intra_pred_mode(lc, l, sps,
2284 7937817 x0 + pb_size * j, y0 + pb_size * i, pb_size,
2285 7937817 prev_intra_luma_pred_flag[2 * i + j]);
2286 }
2287 }
2288
2289
2/2
✓ Branch 0 taken 105006 times.
✓ Branch 1 taken 4130661 times.
4235667 if (sps->chroma_format_idc == 3) {
2290
2/2
✓ Branch 0 taken 138857 times.
✓ Branch 1 taken 105006 times.
243863 for (i = 0; i < side; i++) {
2291
2/2
✓ Branch 0 taken 206559 times.
✓ Branch 1 taken 138857 times.
345416 for (j = 0; j < side; j++) {
2292 206559 lc->pu.chroma_mode_c[2 * i + j] = chroma_mode = ff_hevc_intra_chroma_pred_mode_decode(lc);
2293
2/2
✓ Branch 0 taken 34248 times.
✓ Branch 1 taken 172311 times.
206559 if (chroma_mode != 4) {
2294
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])
2295 2343 lc->pu.intra_pred_mode_c[2 * i + j] = 34;
2296 else
2297 31905 lc->pu.intra_pred_mode_c[2 * i + j] = intra_chroma_table[chroma_mode];
2298 } else {
2299 172311 lc->pu.intra_pred_mode_c[2 * i + j] = lc->pu.intra_pred_mode[2 * i + j];
2300 }
2301 }
2302 }
2303
2/2
✓ Branch 0 taken 247106 times.
✓ Branch 1 taken 3883555 times.
4130661 } else if (sps->chroma_format_idc == 2) {
2304 int mode_idx;
2305 247106 lc->pu.chroma_mode_c[0] = chroma_mode = ff_hevc_intra_chroma_pred_mode_decode(lc);
2306
2/2
✓ Branch 0 taken 73995 times.
✓ Branch 1 taken 173111 times.
247106 if (chroma_mode != 4) {
2307
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])
2308 3515 mode_idx = 34;
2309 else
2310 70480 mode_idx = intra_chroma_table[chroma_mode];
2311 } else {
2312 173111 mode_idx = lc->pu.intra_pred_mode[0];
2313 }
2314 247106 lc->pu.intra_pred_mode_c[0] = tab_mode_idx[mode_idx];
2315
2/2
✓ Branch 0 taken 3883363 times.
✓ Branch 1 taken 192 times.
3883555 } else if (sps->chroma_format_idc != 0) {
2316 3883363 chroma_mode = ff_hevc_intra_chroma_pred_mode_decode(lc);
2317
2/2
✓ Branch 0 taken 964565 times.
✓ Branch 1 taken 2918798 times.
3883363 if (chroma_mode != 4) {
2318
2/2
✓ Branch 0 taken 71726 times.
✓ Branch 1 taken 892839 times.
964565 if (lc->pu.intra_pred_mode[0] == intra_chroma_table[chroma_mode])
2319 71726 lc->pu.intra_pred_mode_c[0] = 34;
2320 else
2321 892839 lc->pu.intra_pred_mode_c[0] = intra_chroma_table[chroma_mode];
2322 } else {
2323 2918798 lc->pu.intra_pred_mode_c[0] = lc->pu.intra_pred_mode[0];
2324 }
2325 }
2326 4235667 }
2327
2328 7426800 static void intra_prediction_unit_default_value(HEVCLocalContext *lc,
2329 const HEVCLayerContext *l,
2330 const HEVCSPS *sps,
2331 int x0, int y0,
2332 int log2_cb_size)
2333 {
2334 7426800 const HEVCContext *const s = lc->parent;
2335 7426800 int pb_size = 1 << log2_cb_size;
2336 7426800 int size_in_pus = pb_size >> sps->log2_min_pu_size;
2337 7426800 int min_pu_width = sps->min_pu_width;
2338 7426800 MvField *tab_mvf = s->cur_frame->tab_mvf;
2339 7426800 int x_pu = x0 >> sps->log2_min_pu_size;
2340 7426800 int y_pu = y0 >> sps->log2_min_pu_size;
2341 int j, k;
2342
2343
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7426800 times.
7426800 if (size_in_pus == 0)
2344 size_in_pus = 1;
2345
2/2
✓ Branch 0 taken 32223458 times.
✓ Branch 1 taken 7426800 times.
39650258 for (j = 0; j < size_in_pus; j++)
2346 32223458 memset(&l->tab_ipm[(y_pu + j) * min_pu_width + x_pu], INTRA_DC, size_in_pus);
2347
2/2
✓ Branch 0 taken 12433 times.
✓ Branch 1 taken 7414367 times.
7426800 if (lc->cu.pred_mode == MODE_INTRA)
2348
2/2
✓ Branch 0 taken 40908 times.
✓ Branch 1 taken 12433 times.
53341 for (j = 0; j < size_in_pus; j++)
2349
2/2
✓ Branch 0 taken 182512 times.
✓ Branch 1 taken 40908 times.
223420 for (k = 0; k < size_in_pus; k++)
2350 182512 tab_mvf[(y_pu + j) * min_pu_width + x_pu + k].pred_flag = PF_INTRA;
2351 7426800 }
2352
2353 11662467 static int hls_coding_unit(HEVCLocalContext *lc, const HEVCContext *s,
2354 const HEVCLayerContext *l,
2355 const HEVCPPS *pps, const HEVCSPS *sps,
2356 int x0, int y0, int log2_cb_size)
2357 {
2358 11662467 int cb_size = 1 << log2_cb_size;
2359 11662467 int log2_min_cb_size = sps->log2_min_cb_size;
2360 11662467 int length = cb_size >> log2_min_cb_size;
2361 11662467 int min_cb_width = sps->min_cb_width;
2362 11662467 int x_cb = x0 >> log2_min_cb_size;
2363 11662467 int y_cb = y0 >> log2_min_cb_size;
2364 11662467 int idx = log2_cb_size - 2;
2365 11662467 int qp_block_mask = (1 << (sps->log2_ctb_size - pps->diff_cu_qp_delta_depth)) - 1;
2366 int x, y, ret;
2367
2368 11662467 lc->cu.x = x0;
2369 11662467 lc->cu.y = y0;
2370 11662467 lc->cu.pred_mode = MODE_INTRA;
2371 11662467 lc->cu.part_mode = PART_2Nx2N;
2372 11662467 lc->cu.intra_split_flag = 0;
2373
2374 11662467 SAMPLE_CTB(l->skip_flag, x_cb, y_cb) = 0;
2375
2/2
✓ Branch 0 taken 46649868 times.
✓ Branch 1 taken 11662467 times.
58312335 for (x = 0; x < 4; x++)
2376 46649868 lc->pu.intra_pred_mode[x] = 1;
2377
2/2
✓ Branch 0 taken 211788 times.
✓ Branch 1 taken 11450679 times.
11662467 if (pps->transquant_bypass_enable_flag) {
2378 211788 lc->cu.cu_transquant_bypass_flag = ff_hevc_cu_transquant_bypass_flag_decode(lc);
2379
2/2
✓ Branch 0 taken 92187 times.
✓ Branch 1 taken 119601 times.
211788 if (lc->cu.cu_transquant_bypass_flag)
2380 92187 set_deblocking_bypass(l->is_pcm, sps, x0, y0, log2_cb_size);
2381 } else
2382 11450679 lc->cu.cu_transquant_bypass_flag = 0;
2383
2384
2/2
✓ Branch 0 taken 8550547 times.
✓ Branch 1 taken 3111920 times.
11662467 if (s->sh.slice_type != HEVC_SLICE_I) {
2385 8550547 const int x0b = av_zero_extend(x0, sps->log2_ctb_size);
2386 8550547 const int y0b = av_zero_extend(y0, sps->log2_ctb_size);
2387 8550547 uint8_t skip_flag = ff_hevc_skip_flag_decode(lc, l->skip_flag,
2388 x0b, y0b, x_cb, y_cb,
2389 min_cb_width);
2390
2391 8550547 x = y_cb * min_cb_width + x_cb;
2392
2/2
✓ Branch 0 taken 17829469 times.
✓ Branch 1 taken 8550547 times.
26380016 for (y = 0; y < length; y++) {
2393 17829469 memset(&l->skip_flag[x], skip_flag, length);
2394 17829469 x += min_cb_width;
2395 }
2396
2/2
✓ Branch 0 taken 4081786 times.
✓ Branch 1 taken 4468761 times.
8550547 lc->cu.pred_mode = skip_flag ? MODE_SKIP : MODE_INTER;
2397 } else {
2398 3111920 x = y_cb * min_cb_width + x_cb;
2399
2/2
✓ Branch 0 taken 4219743 times.
✓ Branch 1 taken 3111920 times.
7331663 for (y = 0; y < length; y++) {
2400 4219743 memset(&l->skip_flag[x], 0, length);
2401 4219743 x += min_cb_width;
2402 }
2403 }
2404
2405
2/2
✓ Branch 0 taken 4081786 times.
✓ Branch 1 taken 7580681 times.
11662467 if (SAMPLE_CTB(l->skip_flag, x_cb, y_cb)) {
2406 4081786 hls_prediction_unit(lc, l, pps, sps,
2407 x0, y0, cb_size, cb_size, log2_cb_size, 0, idx);
2408 4081786 intra_prediction_unit_default_value(lc, l, sps, x0, y0, log2_cb_size);
2409
2410
2/2
✓ Branch 0 taken 3935273 times.
✓ Branch 1 taken 146513 times.
4081786 if (!s->sh.disable_deblocking_filter_flag)
2411 3935273 ff_hevc_deblocking_boundary_strengths(lc, l, pps, x0, y0, log2_cb_size);
2412 } else {
2413 7580681 int pcm_flag = 0;
2414
2415
2/2
✓ Branch 0 taken 4468761 times.
✓ Branch 1 taken 3111920 times.
7580681 if (s->sh.slice_type != HEVC_SLICE_I)
2416 4468761 lc->cu.pred_mode = ff_hevc_pred_mode_decode(lc);
2417
2/2
✓ Branch 0 taken 4248100 times.
✓ Branch 1 taken 3332581 times.
7580681 if (lc->cu.pred_mode != MODE_INTRA ||
2418
2/2
✓ Branch 0 taken 3196179 times.
✓ Branch 1 taken 1051921 times.
4248100 log2_cb_size == sps->log2_min_cb_size) {
2419 6528760 lc->cu.part_mode = ff_hevc_part_mode_decode(lc, sps, log2_cb_size);
2420
2/2
✓ Branch 0 taken 1339686 times.
✓ Branch 1 taken 5189074 times.
7868446 lc->cu.intra_split_flag = lc->cu.part_mode == PART_NxN &&
2421
2/2
✓ Branch 0 taken 1234050 times.
✓ Branch 1 taken 105636 times.
1339686 lc->cu.pred_mode == MODE_INTRA;
2422 }
2423
2424
2/2
✓ Branch 0 taken 4248100 times.
✓ Branch 1 taken 3332581 times.
7580681 if (lc->cu.pred_mode == MODE_INTRA) {
2425
4/4
✓ Branch 0 taken 3014050 times.
✓ Branch 1 taken 1234050 times.
✓ Branch 2 taken 162684 times.
✓ Branch 3 taken 2851366 times.
4248100 if (lc->cu.part_mode == PART_2Nx2N && sps->pcm_enabled &&
2426
2/2
✓ Branch 0 taken 160815 times.
✓ Branch 1 taken 1869 times.
162684 log2_cb_size >= sps->pcm.log2_min_pcm_cb_size &&
2427
2/2
✓ Branch 0 taken 153183 times.
✓ Branch 1 taken 7632 times.
160815 log2_cb_size <= sps->pcm.log2_max_pcm_cb_size) {
2428 153183 pcm_flag = ff_hevc_pcm_flag_decode(lc);
2429 }
2430
2/2
✓ Branch 0 taken 12433 times.
✓ Branch 1 taken 4235667 times.
4248100 if (pcm_flag) {
2431 12433 intra_prediction_unit_default_value(lc, l, sps, x0, y0, log2_cb_size);
2432 12433 ret = hls_pcm_sample(lc, l, pps, x0, y0, log2_cb_size);
2433
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 12425 times.
12433 if (sps->pcm_loop_filter_disabled)
2434 8 set_deblocking_bypass(l->is_pcm, sps, x0, y0, log2_cb_size);
2435
2436
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12433 times.
12433 if (ret < 0)
2437 return ret;
2438 } else {
2439 4235667 intra_prediction_unit(lc, l, sps, x0, y0, log2_cb_size);
2440 }
2441 } else {
2442 3332581 intra_prediction_unit_default_value(lc, l, sps, x0, y0, log2_cb_size);
2443
8/9
✓ Branch 0 taken 1902471 times.
✓ Branch 1 taken 456818 times.
✓ Branch 2 taken 557201 times.
✓ Branch 3 taken 67005 times.
✓ Branch 4 taken 57888 times.
✓ Branch 5 taken 99971 times.
✓ Branch 6 taken 85591 times.
✓ Branch 7 taken 105636 times.
✗ Branch 8 not taken.
3332581 switch (lc->cu.part_mode) {
2444 1902471 case PART_2Nx2N:
2445 1902471 hls_prediction_unit(lc, l, pps, sps,
2446 x0, y0, cb_size, cb_size, log2_cb_size, 0, idx);
2447 1902471 break;
2448 456818 case PART_2NxN:
2449 456818 hls_prediction_unit(lc, l, pps, sps,
2450 x0, y0, cb_size, cb_size / 2, log2_cb_size, 0, idx);
2451 456818 hls_prediction_unit(lc, l, pps, sps,
2452 456818 x0, y0 + cb_size / 2, cb_size, cb_size / 2, log2_cb_size, 1, idx);
2453 456818 break;
2454 557201 case PART_Nx2N:
2455 557201 hls_prediction_unit(lc, l, pps, sps,
2456 x0, y0, cb_size / 2, cb_size, log2_cb_size, 0, idx - 1);
2457 557201 hls_prediction_unit(lc, l, pps, sps,
2458 557201 x0 + cb_size / 2, y0, cb_size / 2, cb_size, log2_cb_size, 1, idx - 1);
2459 557201 break;
2460 67005 case PART_2NxnU:
2461 67005 hls_prediction_unit(lc, l, pps, sps,
2462 x0, y0, cb_size, cb_size / 4, log2_cb_size, 0, idx);
2463 67005 hls_prediction_unit(lc, l, pps, sps,
2464 67005 x0, y0 + cb_size / 4, cb_size, cb_size * 3 / 4, log2_cb_size, 1, idx);
2465 67005 break;
2466 57888 case PART_2NxnD:
2467 57888 hls_prediction_unit(lc, l, pps, sps,
2468 57888 x0, y0, cb_size, cb_size * 3 / 4, log2_cb_size, 0, idx);
2469 57888 hls_prediction_unit(lc, l, pps, sps,
2470 57888 x0, y0 + cb_size * 3 / 4, cb_size, cb_size / 4, log2_cb_size, 1, idx);
2471 57888 break;
2472 99971 case PART_nLx2N:
2473 99971 hls_prediction_unit(lc, l, pps, sps,
2474 x0, y0, cb_size / 4, cb_size, log2_cb_size, 0, idx - 2);
2475 99971 hls_prediction_unit(lc, l, pps, sps,
2476 99971 x0 + cb_size / 4, y0, cb_size * 3 / 4, cb_size, log2_cb_size, 1, idx - 2);
2477 99971 break;
2478 85591 case PART_nRx2N:
2479 85591 hls_prediction_unit(lc, l, pps, sps,
2480 85591 x0, y0, cb_size * 3 / 4, cb_size, log2_cb_size, 0, idx - 2);
2481 85591 hls_prediction_unit(lc, l, pps, sps,
2482 85591 x0 + cb_size * 3 / 4, y0, cb_size / 4, cb_size, log2_cb_size, 1, idx - 2);
2483 85591 break;
2484 105636 case PART_NxN:
2485 105636 hls_prediction_unit(lc, l, pps, sps,
2486 x0, y0, cb_size / 2, cb_size / 2, log2_cb_size, 0, idx - 1);
2487 105636 hls_prediction_unit(lc, l, pps, sps,
2488 105636 x0 + cb_size / 2, y0, cb_size / 2, cb_size / 2, log2_cb_size, 1, idx - 1);
2489 105636 hls_prediction_unit(lc, l, pps, sps,
2490 105636 x0, y0 + cb_size / 2, cb_size / 2, cb_size / 2, log2_cb_size, 2, idx - 1);
2491 105636 hls_prediction_unit(lc, l, pps, sps,
2492 105636 x0 + cb_size / 2, y0 + cb_size / 2, cb_size / 2, cb_size / 2, log2_cb_size, 3, idx - 1);
2493 105636 break;
2494 }
2495 }
2496
2497
2/2
✓ Branch 0 taken 7568248 times.
✓ Branch 1 taken 12433 times.
7580681 if (!pcm_flag) {
2498 7568248 int rqt_root_cbf = 1;
2499
2500
2/2
✓ Branch 0 taken 3332581 times.
✓ Branch 1 taken 4235667 times.
7568248 if (lc->cu.pred_mode != MODE_INTRA &&
2501
4/4
✓ Branch 0 taken 1902471 times.
✓ Branch 1 taken 1430110 times.
✓ Branch 2 taken 1040595 times.
✓ Branch 3 taken 861876 times.
3332581 !(lc->cu.part_mode == PART_2Nx2N && lc->pu.merge_flag)) {
2502 2470705 rqt_root_cbf = ff_hevc_no_residual_syntax_flag_decode(lc);
2503 }
2504
2/2
✓ Branch 0 taken 6198511 times.
✓ Branch 1 taken 1369737 times.
7568248 if (rqt_root_cbf) {
2505 const static int cbf[2] = { 0 };
2506
2/2
✓ Branch 0 taken 4235667 times.
✓ Branch 1 taken 1962844 times.
6198511 lc->cu.max_trafo_depth = lc->cu.pred_mode == MODE_INTRA ?
2507 4235667 sps->max_transform_hierarchy_depth_intra + lc->cu.intra_split_flag :
2508 1962844 sps->max_transform_hierarchy_depth_inter;
2509 6198511 ret = hls_transform_tree(lc, l, pps, sps, x0, y0, x0, y0, x0, y0,
2510 log2_cb_size,
2511 log2_cb_size, 0, 0, cbf, cbf);
2512
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 6198510 times.
6198511 if (ret < 0)
2513 1 return ret;
2514 } else {
2515
2/2
✓ Branch 0 taken 1313305 times.
✓ Branch 1 taken 56432 times.
1369737 if (!s->sh.disable_deblocking_filter_flag)
2516 1313305 ff_hevc_deblocking_boundary_strengths(lc, l, pps, x0, y0, log2_cb_size);
2517 }
2518 }
2519 }
2520
2521
4/4
✓ Branch 0 taken 2077208 times.
✓ Branch 1 taken 9585258 times.
✓ Branch 2 taken 818343 times.
✓ Branch 3 taken 1258865 times.
11662466 if (pps->cu_qp_delta_enabled_flag && lc->tu.is_cu_qp_delta_coded == 0)
2522 818343 ff_hevc_set_qPy(lc, l, pps, x0, y0, log2_cb_size);
2523
2524 11662466 x = y_cb * min_cb_width + x_cb;
2525
2/2
✓ Branch 0 taken 22049211 times.
✓ Branch 1 taken 11662466 times.
33711677 for (y = 0; y < length; y++) {
2526 22049211 memset(&l->qp_y_tab[x], lc->qp_y, length);
2527 22049211 x += min_cb_width;
2528 }
2529
2530
2/2
✓ Branch 0 taken 4021512 times.
✓ Branch 1 taken 7640954 times.
11662466 if(((x0 + (1<<log2_cb_size)) & qp_block_mask) == 0 &&
2531
2/2
✓ Branch 0 taken 2380064 times.
✓ Branch 1 taken 1641448 times.
4021512 ((y0 + (1<<log2_cb_size)) & qp_block_mask) == 0) {
2532 2380064 lc->qPy_pred = lc->qp_y;
2533 }
2534
2535 11662466 set_ct_depth(sps, l->tab_ct_depth, x0, y0, log2_cb_size, lc->ct_depth);
2536
2537 11662466 return 0;
2538 }
2539
2540 15184963 static int hls_coding_quadtree(HEVCLocalContext *lc,
2541 const HEVCLayerContext *l,
2542 const HEVCPPS *pps, const HEVCSPS *sps,
2543 int x0, int y0,
2544 int log2_cb_size, int cb_depth)
2545 {
2546 15184963 const HEVCContext *const s = lc->parent;
2547 15184963 const int cb_size = 1 << log2_cb_size;
2548 int ret;
2549 int split_cu;
2550
2551 15184963 lc->ct_depth = cb_depth;
2552
2/2
✓ Branch 0 taken 15156000 times.
✓ Branch 1 taken 28963 times.
15184963 if (x0 + cb_size <= sps->width &&
2553
2/2
✓ Branch 0 taken 14833756 times.
✓ Branch 1 taken 322244 times.
15156000 y0 + cb_size <= sps->height &&
2554
2/2
✓ Branch 0 taken 8075142 times.
✓ Branch 1 taken 6758614 times.
14833756 log2_cb_size > sps->log2_min_cb_size) {
2555 8075142 split_cu = ff_hevc_split_coding_unit_flag_decode(lc, l->tab_ct_depth,
2556 sps, cb_depth, x0, y0);
2557 } else {
2558 7109821 split_cu = (log2_cb_size > sps->log2_min_cb_size);
2559 }
2560
2/2
✓ Branch 0 taken 2686255 times.
✓ Branch 1 taken 12498708 times.
15184963 if (pps->cu_qp_delta_enabled_flag &&
2561
2/2
✓ Branch 0 taken 1536373 times.
✓ Branch 1 taken 1149882 times.
2686255 log2_cb_size >= sps->log2_ctb_size - pps->diff_cu_qp_delta_depth) {
2562 1536373 lc->tu.is_cu_qp_delta_coded = 0;
2563 1536373 lc->tu.cu_qp_delta = 0;
2564 }
2565
2566
2/2
✓ Branch 0 taken 519326 times.
✓ Branch 1 taken 14665637 times.
15184963 if (s->sh.cu_chroma_qp_offset_enabled_flag &&
2567
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) {
2568 519326 lc->tu.is_cu_chroma_qp_offset_coded = 0;
2569 }
2570
2571
2/2
✓ Branch 0 taken 3522496 times.
✓ Branch 1 taken 11662467 times.
15184963 if (split_cu) {
2572 3522496 int qp_block_mask = (1 << (sps->log2_ctb_size - pps->diff_cu_qp_delta_depth)) - 1;
2573 3522496 const int cb_size_split = cb_size >> 1;
2574 3522496 const int x1 = x0 + cb_size_split;
2575 3522496 const int y1 = y0 + cb_size_split;
2576
2577 3522496 int more_data = 0;
2578
2579 3522496 more_data = hls_coding_quadtree(lc, l, pps, sps,
2580 x0, y0, log2_cb_size - 1, cb_depth + 1);
2581
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 3522494 times.
3522496 if (more_data < 0)
2582 2 return more_data;
2583
2584
4/4
✓ Branch 0 taken 3522485 times.
✓ Branch 1 taken 9 times.
✓ Branch 2 taken 3494544 times.
✓ Branch 3 taken 27941 times.
3522494 if (more_data && x1 < sps->width) {
2585 3494544 more_data = hls_coding_quadtree(lc, l, pps, sps,
2586 x1, y0, log2_cb_size - 1, cb_depth + 1);
2587
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3494544 times.
3494544 if (more_data < 0)
2588 return more_data;
2589 }
2590
4/4
✓ Branch 0 taken 3512179 times.
✓ Branch 1 taken 10315 times.
✓ Branch 2 taken 3311072 times.
✓ Branch 3 taken 201107 times.
3522494 if (more_data && y1 < sps->height) {
2591 3311072 more_data = hls_coding_quadtree(lc, l, pps, sps,
2592 x0, y1, log2_cb_size - 1, cb_depth + 1);
2593
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3311072 times.
3311072 if (more_data < 0)
2594 return more_data;
2595 }
2596
4/4
✓ Branch 0 taken 3506056 times.
✓ Branch 1 taken 16438 times.
✓ Branch 2 taken 3484238 times.
✓ Branch 3 taken 21818 times.
3522494 if (more_data && x1 < sps->width &&
2597
2/2
✓ Branch 0 taken 3283131 times.
✓ Branch 1 taken 201107 times.
3484238 y1 < sps->height) {
2598 3283131 more_data = hls_coding_quadtree(lc, l, pps, sps,
2599 x1, y1, log2_cb_size - 1, cb_depth + 1);
2600
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 3283130 times.
3283131 if (more_data < 0)
2601 1 return more_data;
2602 }
2603
2604
2/2
✓ Branch 0 taken 1984526 times.
✓ Branch 1 taken 1537967 times.
3522493 if(((x0 + (1<<log2_cb_size)) & qp_block_mask) == 0 &&
2605
2/2
✓ Branch 0 taken 1480040 times.
✓ Branch 1 taken 504486 times.
1984526 ((y0 + (1<<log2_cb_size)) & qp_block_mask) == 0)
2606 1480040 lc->qPy_pred = lc->qp_y;
2607
2608
2/2
✓ Branch 0 taken 3486566 times.
✓ Branch 1 taken 35927 times.
3522493 if (more_data)
2609
2/2
✓ Branch 0 taken 95186 times.
✓ Branch 1 taken 3391380 times.
3581752 return ((x1 + cb_size_split) < sps->width ||
2610
2/2
✓ Branch 0 taken 95185 times.
✓ Branch 1 taken 1 times.
95186 (y1 + cb_size_split) < sps->height);
2611 else
2612 35927 return 0;
2613 } else {
2614 11662467 ret = hls_coding_unit(lc, s, l, pps, sps, x0, y0, log2_cb_size);
2615
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 11662466 times.
11662467 if (ret < 0)
2616 1 return ret;
2617 11662466 if ((!((x0 + cb_size) %
2618
2/2
✓ Branch 0 taken 8441724 times.
✓ Branch 1 taken 3220742 times.
11662466 (1 << (sps->log2_ctb_size))) ||
2619
2/2
✓ Branch 0 taken 71449 times.
✓ Branch 1 taken 8370275 times.
8441724 (x0 + cb_size >= sps->width)) &&
2620 3292191 (!((y0 + cb_size) %
2621
2/2
✓ Branch 0 taken 1815954 times.
✓ Branch 1 taken 1476237 times.
3292191 (1 << (sps->log2_ctb_size))) ||
2622
2/2
✓ Branch 0 taken 97482 times.
✓ Branch 1 taken 1718472 times.
1815954 (y0 + cb_size >= sps->height))) {
2623 1573719 int end_of_slice_flag = ff_hevc_end_of_slice_flag_decode(lc);
2624 1573719 return !end_of_slice_flag;
2625 } else {
2626 10088747 return 1;
2627 }
2628 }
2629
2630 return 0;
2631 }
2632
2633 1573720 static void hls_decode_neighbour(HEVCLocalContext *lc,
2634 const HEVCLayerContext *l,
2635 const HEVCPPS *pps, const HEVCSPS *sps,
2636 int x_ctb, int y_ctb, int ctb_addr_ts)
2637 {
2638 1573720 const HEVCContext *const s = lc->parent;
2639 1573720 int ctb_size = 1 << sps->log2_ctb_size;
2640 1573720 int ctb_addr_rs = pps->ctb_addr_ts_to_rs[ctb_addr_ts];
2641 1573720 int ctb_addr_in_slice = ctb_addr_rs - s->sh.slice_addr;
2642
2643 1573720 l->tab_slice_address[ctb_addr_rs] = s->sh.slice_addr;
2644
2645
2/2
✓ Branch 0 taken 153814 times.
✓ Branch 1 taken 1419906 times.
1573720 if (pps->entropy_coding_sync_enabled_flag) {
2646
3/4
✓ Branch 0 taken 6569 times.
✓ Branch 1 taken 147245 times.
✓ Branch 2 taken 6569 times.
✗ Branch 3 not taken.
153814 if (x_ctb == 0 && (y_ctb & (ctb_size - 1)) == 0)
2647 6569 lc->first_qp_group = 1;
2648 153814 lc->end_of_tiles_x = sps->width;
2649
2/2
✓ Branch 0 taken 291890 times.
✓ Branch 1 taken 1128016 times.
1419906 } else if (pps->tiles_enabled_flag) {
2650
4/4
✓ Branch 0 taken 291264 times.
✓ Branch 1 taken 626 times.
✓ Branch 2 taken 6287 times.
✓ Branch 3 taken 284977 times.
291890 if (ctb_addr_ts && pps->tile_id[ctb_addr_ts] != pps->tile_id[ctb_addr_ts - 1]) {
2651 6287 int idxX = pps->col_idxX[x_ctb >> sps->log2_ctb_size];
2652 6287 lc->end_of_tiles_x = x_ctb + (pps->column_width[idxX] << sps->log2_ctb_size);
2653 6287 lc->first_qp_group = 1;
2654 }
2655 } else {
2656 1128016 lc->end_of_tiles_x = sps->width;
2657 }
2658
2659 1573720 lc->end_of_tiles_y = FFMIN(y_ctb + ctb_size, sps->height);
2660
2661 1573720 lc->boundary_flags = 0;
2662
2/2
✓ Branch 0 taken 291890 times.
✓ Branch 1 taken 1281830 times.
1573720 if (pps->tiles_enabled_flag) {
2663
4/4
✓ Branch 0 taken 279936 times.
✓ Branch 1 taken 11954 times.
✓ Branch 2 taken 30753 times.
✓ Branch 3 taken 249183 times.
291890 if (x_ctb > 0 && pps->tile_id[ctb_addr_ts] != pps->tile_id[pps->ctb_addr_rs_to_ts[ctb_addr_rs - 1]])
2664 30753 lc->boundary_flags |= BOUNDARY_LEFT_TILE;
2665
4/4
✓ Branch 0 taken 279936 times.
✓ Branch 1 taken 11954 times.
✓ Branch 2 taken 3741 times.
✓ Branch 3 taken 276195 times.
291890 if (x_ctb > 0 && l->tab_slice_address[ctb_addr_rs] != l->tab_slice_address[ctb_addr_rs - 1])
2666 3741 lc->boundary_flags |= BOUNDARY_LEFT_SLICE;
2667
4/4
✓ Branch 0 taken 277242 times.
✓ Branch 1 taken 14648 times.
✓ Branch 2 taken 33781 times.
✓ Branch 3 taken 243461 times.
291890 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]])
2668 33781 lc->boundary_flags |= BOUNDARY_UPPER_TILE;
2669
4/4
✓ Branch 0 taken 277242 times.
✓ Branch 1 taken 14648 times.
✓ Branch 2 taken 14490 times.
✓ Branch 3 taken 262752 times.
291890 if (y_ctb > 0 && l->tab_slice_address[ctb_addr_rs] != l->tab_slice_address[ctb_addr_rs - sps->ctb_width])
2670 14490 lc->boundary_flags |= BOUNDARY_UPPER_SLICE;
2671 } else {
2672
2/2
✓ Branch 0 taken 18239 times.
✓ Branch 1 taken 1263591 times.
1281830 if (ctb_addr_in_slice <= 0)
2673 18239 lc->boundary_flags |= BOUNDARY_LEFT_SLICE;
2674
2/2
✓ Branch 0 taken 195692 times.
✓ Branch 1 taken 1086138 times.
1281830 if (ctb_addr_in_slice < sps->ctb_width)
2675 195692 lc->boundary_flags |= BOUNDARY_UPPER_SLICE;
2676 }
2677
2678
6/6
✓ Branch 0 taken 1492704 times.
✓ Branch 1 taken 81016 times.
✓ Branch 2 taken 1484815 times.
✓ Branch 3 taken 7889 times.
✓ Branch 4 taken 1455090 times.
✓ Branch 5 taken 29725 times.
1573720 lc->ctb_left_flag = ((x_ctb > 0) && (ctb_addr_in_slice > 0) && !(lc->boundary_flags & BOUNDARY_LEFT_TILE));
2679
6/6
✓ Branch 0 taken 1443549 times.
✓ Branch 1 taken 130171 times.
✓ Branch 2 taken 1349719 times.
✓ Branch 3 taken 93830 times.
✓ Branch 4 taken 1329599 times.
✓ Branch 5 taken 20120 times.
1573720 lc->ctb_up_flag = ((y_ctb > 0) && (ctb_addr_in_slice >= sps->ctb_width) && !(lc->boundary_flags & BOUNDARY_UPPER_TILE));
2680
6/6
✓ Branch 0 taken 1443549 times.
✓ Branch 1 taken 130171 times.
✓ Branch 2 taken 1352308 times.
✓ Branch 3 taken 91241 times.
✓ Branch 4 taken 1296556 times.
✓ Branch 5 taken 55752 times.
1573720 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]]));
2681
8/8
✓ Branch 0 taken 1492704 times.
✓ Branch 1 taken 81016 times.
✓ Branch 2 taken 1372704 times.
✓ Branch 3 taken 120000 times.
✓ Branch 4 taken 1283938 times.
✓ Branch 5 taken 88766 times.
✓ Branch 6 taken 1239706 times.
✓ Branch 7 taken 44232 times.
1573720 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]]));
2682 1573720 }
2683
2684 28165 static int hls_decode_entry(HEVCContext *s, GetBitContext *gb)
2685 {
2686 28165 HEVCLocalContext *const lc = &s->local_ctx[0];
2687 28165 const HEVCLayerContext *const l = &s->layers[s->cur_layer];
2688 28165 const HEVCPPS *const pps = s->pps;
2689 28165 const HEVCSPS *const sps = pps->sps;
2690 28165 const uint8_t *slice_data = gb->buffer + s->sh.data_offset;
2691 28165 const size_t slice_size = gb->buffer_end - gb->buffer - s->sh.data_offset;
2692 28165 int ctb_size = 1 << sps->log2_ctb_size;
2693 28165 int more_data = 1;
2694 28165 int x_ctb = 0;
2695 28165 int y_ctb = 0;
2696 28165 int ctb_addr_ts = pps->ctb_addr_rs_to_ts[s->sh.slice_ctb_addr_rs];
2697 int ret;
2698
2699
3/4
✓ Branch 0 taken 1573720 times.
✓ Branch 1 taken 28164 times.
✓ Branch 2 taken 1573720 times.
✗ Branch 3 not taken.
1601884 while (more_data && ctb_addr_ts < sps->ctb_size) {
2700 1573720 int ctb_addr_rs = pps->ctb_addr_ts_to_rs[ctb_addr_ts];
2701
2702 1573720 x_ctb = (ctb_addr_rs % ((sps->width + ctb_size - 1) >> sps->log2_ctb_size)) << sps->log2_ctb_size;
2703 1573720 y_ctb = (ctb_addr_rs / ((sps->width + ctb_size - 1) >> sps->log2_ctb_size)) << sps->log2_ctb_size;
2704 1573720 hls_decode_neighbour(lc, l, pps, sps, x_ctb, y_ctb, ctb_addr_ts);
2705
2706 1573720 ret = ff_hevc_cabac_init(lc, pps, ctb_addr_ts, slice_data, slice_size, 0);
2707
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1573720 times.
1573720 if (ret < 0) {
2708 l->tab_slice_address[ctb_addr_rs] = -1;
2709 return ret;
2710 }
2711
2712 1573720 hls_sao_param(lc, l, pps, sps,
2713 1573720 x_ctb >> sps->log2_ctb_size, y_ctb >> sps->log2_ctb_size);
2714
2715 1573720 l->deblock[ctb_addr_rs].beta_offset = s->sh.beta_offset;
2716 1573720 l->deblock[ctb_addr_rs].tc_offset = s->sh.tc_offset;
2717 1573720 l->filter_slice_edges[ctb_addr_rs] = s->sh.slice_loop_filter_across_slices_enabled_flag;
2718
2719 1573720 more_data = hls_coding_quadtree(lc, l, pps, sps, x_ctb, y_ctb, sps->log2_ctb_size, 0);
2720
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1573719 times.
1573720 if (more_data < 0) {
2721 1 l->tab_slice_address[ctb_addr_rs] = -1;
2722 1 return more_data;
2723 }
2724
2725
2726 1573719 ctb_addr_ts++;
2727 1573719 ff_hevc_save_states(lc, pps, ctb_addr_ts);
2728 1573719 ff_hevc_hls_filters(lc, l, pps, x_ctb, y_ctb, ctb_size);
2729 }
2730
2731
2/2
✓ Branch 0 taken 14654 times.
✓ Branch 1 taken 13510 times.
28164 if (x_ctb + ctb_size >= sps->width &&
2732
2/2
✓ Branch 0 taken 10170 times.
✓ Branch 1 taken 4484 times.
14654 y_ctb + ctb_size >= sps->height)
2733 10170 ff_hevc_hls_filter(lc, l, pps, x_ctb, y_ctb, ctb_size);
2734
2735 28164 return ctb_addr_ts;
2736 }
2737
2738 static int hls_decode_entry_wpp(AVCodecContext *avctx, void *hevc_lclist,
2739 int job, int thread)
2740 {
2741 HEVCLocalContext *lc = &((HEVCLocalContext*)hevc_lclist)[thread];
2742 const HEVCContext *const s = lc->parent;
2743 const HEVCLayerContext *const l = &s->layers[s->cur_layer];
2744 const HEVCPPS *const pps = s->pps;
2745 const HEVCSPS *const sps = pps->sps;
2746 int ctb_size = 1 << sps->log2_ctb_size;
2747 int more_data = 1;
2748 int ctb_row = job;
2749 int ctb_addr_rs = s->sh.slice_ctb_addr_rs + ctb_row * ((sps->width + ctb_size - 1) >> sps->log2_ctb_size);
2750 int ctb_addr_ts = pps->ctb_addr_rs_to_ts[ctb_addr_rs];
2751
2752 const uint8_t *data = s->data + s->sh.offset[ctb_row];
2753 const size_t data_size = s->sh.size[ctb_row];
2754
2755 int progress = 0;
2756
2757 int ret;
2758
2759 if (ctb_row)
2760 ff_init_cabac_decoder(&lc->cc, data, data_size);
2761
2762 while(more_data && ctb_addr_ts < sps->ctb_size) {
2763 int x_ctb = (ctb_addr_rs % sps->ctb_width) << sps->log2_ctb_size;
2764 int y_ctb = (ctb_addr_rs / sps->ctb_width) << sps->log2_ctb_size;
2765
2766 hls_decode_neighbour(lc, l, pps, sps, x_ctb, y_ctb, ctb_addr_ts);
2767
2768 if (ctb_row)
2769 ff_thread_progress_await(&s->wpp_progress[ctb_row - 1],
2770 progress + SHIFT_CTB_WPP + 1);
2771
2772 /* atomic_load's prototype requires a pointer to non-const atomic variable
2773 * (due to implementations via mutexes, where reads involve writes).
2774 * Of course, casting const away here is nevertheless safe. */
2775 if (atomic_load((atomic_int*)&s->wpp_err)) {
2776 ff_thread_progress_report(&s->wpp_progress[ctb_row], INT_MAX);
2777 return 0;
2778 }
2779
2780 ret = ff_hevc_cabac_init(lc, pps, ctb_addr_ts, data, data_size, 1);
2781 if (ret < 0)
2782 goto error;
2783 hls_sao_param(lc, l, pps, sps,
2784 x_ctb >> sps->log2_ctb_size, y_ctb >> sps->log2_ctb_size);
2785
2786 l->deblock[ctb_addr_rs].beta_offset = s->sh.beta_offset;
2787 l->deblock[ctb_addr_rs].tc_offset = s->sh.tc_offset;
2788 l->filter_slice_edges[ctb_addr_rs] = s->sh.slice_loop_filter_across_slices_enabled_flag;
2789
2790 more_data = hls_coding_quadtree(lc, l, pps, sps, x_ctb, y_ctb, sps->log2_ctb_size, 0);
2791
2792 if (more_data < 0) {
2793 ret = more_data;
2794 goto error;
2795 }
2796
2797 ctb_addr_ts++;
2798
2799 ff_hevc_save_states(lc, pps, ctb_addr_ts);
2800 ff_thread_progress_report(&s->wpp_progress[ctb_row], ++progress);
2801 ff_hevc_hls_filters(lc, l, pps, x_ctb, y_ctb, ctb_size);
2802
2803 if (!more_data && (x_ctb+ctb_size) < sps->width && ctb_row != s->sh.num_entry_point_offsets) {
2804 /* Casting const away here is safe, because it is an atomic operation. */
2805 atomic_store((atomic_int*)&s->wpp_err, 1);
2806 ff_thread_progress_report(&s->wpp_progress[ctb_row], INT_MAX);
2807 return 0;
2808 }
2809
2810 if ((x_ctb+ctb_size) >= sps->width && (y_ctb+ctb_size) >= sps->height ) {
2811 ff_hevc_hls_filter(lc, l, pps, x_ctb, y_ctb, ctb_size);
2812 ff_thread_progress_report(&s->wpp_progress[ctb_row], INT_MAX);
2813 return ctb_addr_ts;
2814 }
2815 ctb_addr_rs = pps->ctb_addr_ts_to_rs[ctb_addr_ts];
2816 x_ctb+=ctb_size;
2817
2818 if(x_ctb >= sps->width) {
2819 break;
2820 }
2821 }
2822 ff_thread_progress_report(&s->wpp_progress[ctb_row], INT_MAX);
2823
2824 return 0;
2825 error:
2826 l->tab_slice_address[ctb_addr_rs] = -1;
2827 /* Casting const away here is safe, because it is an atomic operation. */
2828 atomic_store((atomic_int*)&s->wpp_err, 1);
2829 ff_thread_progress_report(&s->wpp_progress[ctb_row], INT_MAX);
2830 return ret;
2831 }
2832
2833 static int wpp_progress_init(HEVCContext *s, unsigned count)
2834 {
2835 if (s->nb_wpp_progress < count) {
2836 void *tmp = av_realloc_array(s->wpp_progress, count,
2837 sizeof(*s->wpp_progress));
2838 if (!tmp)
2839 return AVERROR(ENOMEM);
2840
2841 s->wpp_progress = tmp;
2842 memset(s->wpp_progress + s->nb_wpp_progress, 0,
2843 (count - s->nb_wpp_progress) * sizeof(*s->wpp_progress));
2844
2845 for (int i = s->nb_wpp_progress; i < count; i++) {
2846 int ret = ff_thread_progress_init(&s->wpp_progress[i], 1);
2847 if (ret < 0)
2848 return ret;
2849 s->nb_wpp_progress = i + 1;
2850 }
2851 }
2852
2853 for (int i = 0; i < count; i++)
2854 ff_thread_progress_reset(&s->wpp_progress[i]);
2855
2856 return 0;
2857 }
2858
2859 static int hls_slice_data_wpp(HEVCContext *s, const H2645NAL *nal)
2860 {
2861 const HEVCPPS *const pps = s->pps;
2862 const HEVCSPS *const sps = pps->sps;
2863 const uint8_t *data = nal->data;
2864 int length = nal->size;
2865 int *ret;
2866 int64_t offset;
2867 int64_t startheader, cmpt = 0;
2868 int i, j, res = 0;
2869
2870 if (s->sh.slice_ctb_addr_rs + s->sh.num_entry_point_offsets * sps->ctb_width >= sps->ctb_width * sps->ctb_height) {
2871 av_log(s->avctx, AV_LOG_ERROR, "WPP ctb addresses are wrong (%d %d %d %d)\n",
2872 s->sh.slice_ctb_addr_rs, s->sh.num_entry_point_offsets,
2873 sps->ctb_width, sps->ctb_height
2874 );
2875 return AVERROR_INVALIDDATA;
2876 }
2877
2878 if (s->avctx->thread_count > s->nb_local_ctx) {
2879 HEVCLocalContext *tmp = av_malloc_array(s->avctx->thread_count, sizeof(*s->local_ctx));
2880
2881 if (!tmp)
2882 return AVERROR(ENOMEM);
2883
2884 memcpy(tmp, s->local_ctx, sizeof(*s->local_ctx) * s->nb_local_ctx);
2885 av_free(s->local_ctx);
2886 s->local_ctx = tmp;
2887
2888 for (unsigned i = s->nb_local_ctx; i < s->avctx->thread_count; i++) {
2889 tmp = &s->local_ctx[i];
2890
2891 memset(tmp, 0, sizeof(*tmp));
2892
2893 tmp->logctx = s->avctx;
2894 tmp->parent = s;
2895 tmp->common_cabac_state = &s->cabac;
2896 }
2897
2898 s->nb_local_ctx = s->avctx->thread_count;
2899 }
2900
2901 offset = s->sh.data_offset;
2902
2903 for (j = 0, cmpt = 0, startheader = offset + s->sh.entry_point_offset[0]; j < nal->skipped_bytes; j++) {
2904 if (nal->skipped_bytes_pos[j] >= offset && nal->skipped_bytes_pos[j] < startheader) {
2905 startheader--;
2906 cmpt++;
2907 }
2908 }
2909
2910 for (i = 1; i < s->sh.num_entry_point_offsets; i++) {
2911 offset += (s->sh.entry_point_offset[i - 1] - cmpt);
2912 for (j = 0, cmpt = 0, startheader = offset
2913 + s->sh.entry_point_offset[i]; j < nal->skipped_bytes; j++) {
2914 if (nal->skipped_bytes_pos[j] >= offset && nal->skipped_bytes_pos[j] < startheader) {
2915 startheader--;
2916 cmpt++;
2917 }
2918 }
2919 s->sh.size[i] = s->sh.entry_point_offset[i] - cmpt;
2920 s->sh.offset[i] = offset;
2921
2922 }
2923
2924 offset += s->sh.entry_point_offset[s->sh.num_entry_point_offsets - 1] - cmpt;
2925 if (length < offset) {
2926 av_log(s->avctx, AV_LOG_ERROR, "entry_point_offset table is corrupted\n");
2927 return AVERROR_INVALIDDATA;
2928 }
2929 s->sh.size [s->sh.num_entry_point_offsets] = length - offset;
2930 s->sh.offset[s->sh.num_entry_point_offsets] = offset;
2931
2932 s->sh.offset[0] = s->sh.data_offset;
2933 s->sh.size[0] = s->sh.offset[1] - s->sh.offset[0];
2934
2935 s->data = data;
2936
2937 for (i = 1; i < s->nb_local_ctx; i++) {
2938 s->local_ctx[i].first_qp_group = 1;
2939 s->local_ctx[i].qp_y = s->local_ctx[0].qp_y;
2940 }
2941
2942 atomic_store(&s->wpp_err, 0);
2943 res = wpp_progress_init(s, s->sh.num_entry_point_offsets + 1);
2944 if (res < 0)
2945 return res;
2946
2947 ret = av_calloc(s->sh.num_entry_point_offsets + 1, sizeof(*ret));
2948 if (!ret)
2949 return AVERROR(ENOMEM);
2950
2951 if (pps->entropy_coding_sync_enabled_flag)
2952 s->avctx->execute2(s->avctx, hls_decode_entry_wpp, s->local_ctx, ret, s->sh.num_entry_point_offsets + 1);
2953
2954 for (i = 0; i <= s->sh.num_entry_point_offsets; i++)
2955 res += ret[i];
2956
2957 av_free(ret);
2958 return res;
2959 }
2960
2961 28165 static int decode_slice_data(HEVCContext *s, const HEVCLayerContext *l,
2962 const H2645NAL *nal, GetBitContext *gb)
2963 {
2964 28165 const HEVCPPS *pps = s->pps;
2965 int ret;
2966
2967
2/2
✓ Branch 0 taken 17994 times.
✓ Branch 1 taken 10171 times.
28165 if (!s->sh.first_slice_in_pic_flag)
2968 17994 s->slice_idx += !s->sh.dependent_slice_segment_flag;
2969
2970
4/4
✓ Branch 0 taken 20218 times.
✓ Branch 1 taken 7947 times.
✓ Branch 2 taken 18983 times.
✓ Branch 3 taken 1235 times.
28165 if (!s->sh.dependent_slice_segment_flag && s->sh.slice_type != HEVC_SLICE_I) {
2971 18983 ret = ff_hevc_slice_rpl(s);
2972
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 18983 times.
18983 if (ret < 0) {
2973 av_log(s->avctx, AV_LOG_WARNING,
2974 "Error constructing the reference lists for the current slice.\n");
2975 return ret;
2976 }
2977 }
2978
2979 28165 s->slice_initialized = 1;
2980
2981
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 28165 times.
28165 if (s->avctx->hwaccel)
2982 return FF_HW_CALL(s->avctx, decode_slice, nal->raw_data, nal->raw_size);
2983
2984
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 28165 times.
28165 if (s->avctx->profile == AV_PROFILE_HEVC_SCC) {
2985 av_log(s->avctx, AV_LOG_ERROR,
2986 "SCC profile is not yet implemented in hevc native decoder.\n");
2987 return AVERROR_PATCHWELCOME;
2988 }
2989
2990
2/2
✓ Branch 0 taken 7947 times.
✓ Branch 1 taken 20218 times.
28165 if (s->sh.dependent_slice_segment_flag) {
2991 7947 int ctb_addr_ts = pps->ctb_addr_rs_to_ts[s->sh.slice_ctb_addr_rs];
2992 7947 int prev_rs = pps->ctb_addr_ts_to_rs[ctb_addr_ts - 1];
2993
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7947 times.
7947 if (l->tab_slice_address[prev_rs] != s->sh.slice_addr) {
2994 av_log(s->avctx, AV_LOG_ERROR, "Previous slice segment missing\n");
2995 return AVERROR_INVALIDDATA;
2996 }
2997 }
2998
2999 28165 s->local_ctx[0].first_qp_group = !s->sh.dependent_slice_segment_flag;
3000
3001
2/2
✓ Branch 0 taken 23130 times.
✓ Branch 1 taken 5035 times.
28165 if (!pps->cu_qp_delta_enabled_flag)
3002 23130 s->local_ctx[0].qp_y = s->sh.slice_qp;
3003
3004 28165 s->local_ctx[0].tu.cu_qp_offset_cb = 0;
3005 28165 s->local_ctx[0].tu.cu_qp_offset_cr = 0;
3006
3007
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 28165 times.
28165 if (s->avctx->active_thread_type == FF_THREAD_SLICE &&
3008 s->sh.num_entry_point_offsets > 0 &&
3009 pps->num_tile_rows == 1 && pps->num_tile_columns == 1)
3010 return hls_slice_data_wpp(s, nal);
3011
3012 28165 return hls_decode_entry(s, gb);
3013 }
3014
3015 10171 static int set_side_data(HEVCContext *s)
3016 {
3017 10171 const HEVCSPS *sps = s->cur_frame->pps->sps;
3018 10171 AVFrame *out = s->cur_frame->f;
3019 int ret;
3020
3021 // Decrement the mastering display and content light level flag when IRAP
3022 // frame has no_rasl_output_flag=1 so the side data persists for the entire
3023 // coded video sequence.
3024
5/6
✓ Branch 0 taken 712 times.
✓ Branch 1 taken 9459 times.
✓ Branch 2 taken 712 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 429 times.
✓ Branch 5 taken 283 times.
10171 if (IS_IRAP(s) && s->no_rasl_output_flag) {
3025
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 421 times.
429 if (s->sei.common.mastering_display.present > 0)
3026 8 s->sei.common.mastering_display.present--;
3027
3028
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 423 times.
429 if (s->sei.common.content_light.present > 0)
3029 6 s->sei.common.content_light.present--;
3030 }
3031
3032 10171 ret = ff_h2645_sei_to_frame(out, &s->sei.common, AV_CODEC_ID_HEVC, s->avctx,
3033 &sps->vui.common,
3034 10171 sps->bit_depth, sps->bit_depth_chroma,
3035 10171 s->cur_frame->poc /* no poc_offset in HEVC */);
3036
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10171 times.
10171 if (ret < 0)
3037 return ret;
3038
3039
2/2
✓ Branch 0 taken 23 times.
✓ Branch 1 taken 10148 times.
10171 if (s->sei.timecode.present) {
3040 uint32_t *tc_sd;
3041 char tcbuf[AV_TIMECODE_STR_SIZE];
3042 AVFrameSideData *tcside;
3043 23 ret = ff_frame_new_side_data(s->avctx, out, AV_FRAME_DATA_S12M_TIMECODE,
3044 sizeof(uint32_t) * 4, &tcside);
3045
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 23 times.
23 if (ret < 0)
3046 return ret;
3047
3048
1/2
✓ Branch 0 taken 23 times.
✗ Branch 1 not taken.
23 if (tcside) {
3049 23 tc_sd = (uint32_t*)tcside->data;
3050 23 tc_sd[0] = s->sei.timecode.num_clock_ts;
3051
3052
2/2
✓ Branch 0 taken 23 times.
✓ Branch 1 taken 23 times.
46 for (int i = 0; i < tc_sd[0]; i++) {
3053 23 int drop = s->sei.timecode.cnt_dropped_flag[i];
3054 23 int hh = s->sei.timecode.hours_value[i];
3055 23 int mm = s->sei.timecode.minutes_value[i];
3056 23 int ss = s->sei.timecode.seconds_value[i];
3057 23 int ff = s->sei.timecode.n_frames[i];
3058
3059 23 tc_sd[i + 1] = av_timecode_get_smpte(s->avctx->framerate, drop, hh, mm, ss, ff);
3060 23 av_timecode_make_smpte_tc_string2(tcbuf, s->avctx->framerate, tc_sd[i + 1], 0, 0);
3061 23 av_dict_set(&out->metadata, "timecode", tcbuf, 0);
3062 }
3063 }
3064
3065 23 s->sei.timecode.num_clock_ts = 0;
3066 }
3067
3068
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 10167 times.
10171 if (s->sei.common.dynamic_hdr_plus.info) {
3069 4 AVBufferRef *info_ref = av_buffer_ref(s->sei.common.dynamic_hdr_plus.info);
3070
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (!info_ref)
3071 return AVERROR(ENOMEM);
3072
3073 4 ret = ff_frame_new_side_data_from_buf(s->avctx, out, AV_FRAME_DATA_DYNAMIC_HDR_PLUS, &info_ref);
3074
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (ret < 0)
3075 return ret;
3076 }
3077
3078
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 10169 times.
10171 if (s->rpu_buf) {
3079 2 AVFrameSideData *rpu = av_frame_new_side_data_from_buf(out, AV_FRAME_DATA_DOVI_RPU_BUFFER, s->rpu_buf);
3080
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (!rpu)
3081 return AVERROR(ENOMEM);
3082
3083 2 s->rpu_buf = NULL;
3084 }
3085
3086
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 10171 times.
10171 if ((ret = ff_dovi_attach_side_data(&s->dovi_ctx, out)) < 0)
3087 return ret;
3088
3089
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 10169 times.
10171 if (s->sei.common.dynamic_hdr_vivid.info) {
3090 2 AVBufferRef *info_ref = av_buffer_ref(s->sei.common.dynamic_hdr_vivid.info);
3091
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (!info_ref)
3092 return AVERROR(ENOMEM);
3093
3094
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 if (!av_frame_new_side_data_from_buf(out, AV_FRAME_DATA_DYNAMIC_HDR_VIVID, info_ref)) {
3095 av_buffer_unref(&info_ref);
3096 return AVERROR(ENOMEM);
3097 }
3098 }
3099
3100 10171 return 0;
3101 }
3102
3103 9894 static int find_finish_setup_nal(const HEVCContext *s)
3104 {
3105 9894 int nal_idx = 0;
3106
3107
2/2
✓ Branch 0 taken 38764 times.
✓ Branch 1 taken 9894 times.
48658 for (int i = nal_idx; i < s->pkt.nb_nals; i++) {
3108 38764 const H2645NAL *nal = &s->pkt.nals[i];
3109 38764 const int layer_id = nal->nuh_layer_id;
3110 38764 GetBitContext gb = nal->gb;
3111
3112
2/4
✓ Branch 0 taken 38764 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 38764 times.
✗ Branch 3 not taken.
38764 if (layer_id > HEVC_MAX_NUH_LAYER_ID || s->vps->layer_idx[layer_id] < 0 ||
3113
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 38748 times.
38764 !(s->layers_active_decode & (1 << s->vps->layer_idx[layer_id])))
3114 18010 continue;
3115
3116
3/3
✓ Branch 0 taken 28166 times.
✓ Branch 1 taken 2264 times.
✓ Branch 2 taken 8318 times.
38748 switch (nal->type) {
3117 28166 case HEVC_NAL_TRAIL_R:
3118 case HEVC_NAL_TRAIL_N:
3119 case HEVC_NAL_TSA_N:
3120 case HEVC_NAL_TSA_R:
3121 case HEVC_NAL_STSA_N:
3122 case HEVC_NAL_STSA_R:
3123 case HEVC_NAL_BLA_W_LP:
3124 case HEVC_NAL_BLA_W_RADL:
3125 case HEVC_NAL_BLA_N_LP:
3126 case HEVC_NAL_IDR_W_RADL:
3127 case HEVC_NAL_IDR_N_LP:
3128 case HEVC_NAL_CRA_NUT:
3129 case HEVC_NAL_RADL_N:
3130 case HEVC_NAL_RADL_R:
3131 case HEVC_NAL_RASL_N:
3132 case HEVC_NAL_RASL_R:
3133
2/2
✓ Branch 1 taken 17994 times.
✓ Branch 2 taken 10172 times.
28166 if (!get_bits1(&gb)) // first_slice_segment_in_pic_flag
3134 17994 continue;
3135 case HEVC_NAL_VPS:
3136 case HEVC_NAL_SPS:
3137 case HEVC_NAL_PPS:
3138 12436 nal_idx = i;
3139 12436 break;
3140 }
3141 }
3142
3143 9894 return nal_idx;
3144 }
3145
3146 10171 static int hevc_frame_start(HEVCContext *s, HEVCLayerContext *l,
3147 unsigned nal_idx)
3148 {
3149 10171 const HEVCPPS *const pps = s->ps.pps_list[s->sh.pps_id];
3150 10171 const HEVCSPS *const sps = pps->sps;
3151 10171 int pic_size_in_ctb = ((sps->width >> sps->log2_min_cb_size) + 1) *
3152 10171 ((sps->height >> sps->log2_min_cb_size) + 1);
3153
2/2
✓ Branch 0 taken 9894 times.
✓ Branch 1 taken 277 times.
20065 int new_sequence = (l == &s->layers[0]) &&
3154
12/12
✓ Branch 0 taken 9510 times.
✓ Branch 1 taken 384 times.
✓ Branch 2 taken 9491 times.
✓ Branch 3 taken 19 times.
✓ Branch 4 taken 9489 times.
✓ Branch 5 taken 2 times.
✓ Branch 6 taken 9482 times.
✓ Branch 7 taken 7 times.
✓ Branch 8 taken 9481 times.
✓ Branch 9 taken 1 times.
✓ Branch 10 taken 9 times.
✓ Branch 11 taken 9472 times.
9894 (IS_IDR(s) || IS_BLA(s) || s->last_eos);
3155 10171 int prev_layers_active_decode = s->layers_active_decode;
3156 10171 int prev_layers_active_output = s->layers_active_output;
3157 int ret;
3158
3159
3/4
✓ Branch 0 taken 399 times.
✓ Branch 1 taken 9772 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 399 times.
10171 if (sps->vps != s->vps && l != &s->layers[0]) {
3160 av_log(s->avctx, AV_LOG_ERROR, "VPS changed in a non-base layer\n");
3161 set_sps(s, l, NULL);
3162 return AVERROR_INVALIDDATA;
3163 }
3164
3165 10171 ff_refstruct_replace(&s->pps, pps);
3166
2/2
✓ Branch 0 taken 413 times.
✓ Branch 1 taken 9758 times.
10171 if (l->sps != sps) {
3167 413 const HEVCSPS *sps_base = s->layers[0].sps;
3168 413 enum AVPixelFormat pix_fmt = sps->pix_fmt;
3169
3170
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 405 times.
413 if (l != &s->layers[0]) {
3171
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
8 if (!sps_base) {
3172 av_log(s->avctx, AV_LOG_ERROR,
3173 "Access unit starts with a non-base layer frame\n");
3174 return AVERROR_INVALIDDATA;
3175 }
3176
3177 // Files produced by Vision Pro lack VPS extension VUI,
3178 // so the secondary layer has no range information.
3179 // This check avoids failing in such a case.
3180
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
8 if (sps_base->pix_fmt == AV_PIX_FMT_YUVJ420P &&
3181 sps->pix_fmt == AV_PIX_FMT_YUV420P &&
3182 !sps->vui.common.video_signal_type_present_flag)
3183 pix_fmt = sps_base->pix_fmt;
3184
3185
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
8 if (pix_fmt != sps_base->pix_fmt ||
3186
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
8 sps->width != sps_base->width ||
3187
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
8 sps->height != sps_base->height) {
3188 av_log(s->avctx, AV_LOG_ERROR,
3189 "Base/non-base layer SPS have unsupported parameter combination\n");
3190 return AVERROR(ENOSYS);
3191 }
3192 }
3193
3194 413 ff_hevc_clear_refs(l);
3195
3196 413 ret = set_sps(s, l, sps);
3197
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 413 times.
413 if (ret < 0)
3198 return ret;
3199
3200
2/2
✓ Branch 0 taken 405 times.
✓ Branch 1 taken 8 times.
413 if (l == &s->layers[0]) {
3201 405 export_stream_params(s, sps);
3202
3203 405 ret = get_format(s, sps);
3204
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 405 times.
405 if (ret < 0) {
3205 set_sps(s, l, NULL);
3206 return ret;
3207 }
3208
3209 405 new_sequence = 1;
3210 }
3211 }
3212
3213 10171 memset(l->horizontal_bs, 0, l->bs_width * l->bs_height);
3214 10171 memset(l->vertical_bs, 0, l->bs_width * l->bs_height);
3215 10171 memset(l->cbf_luma, 0, sps->min_tb_width * sps->min_tb_height);
3216 10171 memset(l->is_pcm, 0, (sps->min_pu_width + 1) * (sps->min_pu_height + 1));
3217 10171 memset(l->tab_slice_address, -1, pic_size_in_ctb * sizeof(*l->tab_slice_address));
3218
3219
4/4
✓ Branch 0 taken 9780 times.
✓ Branch 1 taken 391 times.
✓ Branch 2 taken 19 times.
✓ Branch 3 taken 9761 times.
10171 if (IS_IDR(s))
3220 410 ff_hevc_clear_refs(l);
3221
3222 10171 s->slice_idx = 0;
3223 10171 s->first_nal_type = s->nal_unit_type;
3224 10171 s->poc = s->sh.poc;
3225
3226
3/4
✓ Branch 0 taken 712 times.
✓ Branch 1 taken 9459 times.
✓ Branch 2 taken 712 times.
✗ Branch 3 not taken.
10171 if (IS_IRAP(s))
3227
10/10
✓ Branch 0 taken 321 times.
✓ Branch 1 taken 391 times.
✓ Branch 2 taken 302 times.
✓ Branch 3 taken 19 times.
✓ Branch 4 taken 300 times.
✓ Branch 5 taken 2 times.
✓ Branch 6 taken 293 times.
✓ Branch 7 taken 7 times.
✓ Branch 8 taken 292 times.
✓ Branch 9 taken 1 times.
1004 s->no_rasl_output_flag = IS_IDR(s) || IS_BLA(s) ||
3228
3/4
✓ Branch 0 taken 292 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 9 times.
✓ Branch 3 taken 283 times.
292 (s->nal_unit_type == HEVC_NAL_CRA_NUT && s->last_eos);
3229
3230 /* 8.3.1 */
3231
2/2
✓ Branch 0 taken 9829 times.
✓ Branch 1 taken 342 times.
10171 if (s->temporal_id == 0 &&
3232
2/2
✓ Branch 0 taken 7159 times.
✓ Branch 1 taken 2670 times.
9829 s->nal_unit_type != HEVC_NAL_TRAIL_N &&
3233
1/2
✓ Branch 0 taken 7159 times.
✗ Branch 1 not taken.
7159 s->nal_unit_type != HEVC_NAL_TSA_N &&
3234
1/2
✓ Branch 0 taken 7159 times.
✗ Branch 1 not taken.
7159 s->nal_unit_type != HEVC_NAL_STSA_N &&
3235
2/2
✓ Branch 0 taken 7133 times.
✓ Branch 1 taken 26 times.
7159 s->nal_unit_type != HEVC_NAL_RADL_N &&
3236
2/2
✓ Branch 0 taken 7111 times.
✓ Branch 1 taken 22 times.
7133 s->nal_unit_type != HEVC_NAL_RADL_R &&
3237
2/2
✓ Branch 0 taken 6741 times.
✓ Branch 1 taken 370 times.
7111 s->nal_unit_type != HEVC_NAL_RASL_N &&
3238
2/2
✓ Branch 0 taken 6267 times.
✓ Branch 1 taken 474 times.
6741 s->nal_unit_type != HEVC_NAL_RASL_R)
3239 6267 s->poc_tid0 = s->poc;
3240
3241
2/2
✓ Branch 0 taken 626 times.
✓ Branch 1 taken 9545 times.
10171 if (pps->tiles_enabled_flag)
3242 626 s->local_ctx[0].end_of_tiles_x = pps->column_width[0] << sps->log2_ctb_size;
3243
3244
2/2
✓ Branch 0 taken 425 times.
✓ Branch 1 taken 9746 times.
10171 if (new_sequence) {
3245 425 ret = ff_hevc_output_frames(s, prev_layers_active_decode, prev_layers_active_output,
3246 425 0, 0, s->sh.no_output_of_prior_pics_flag);
3247
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 425 times.
425 if (ret < 0)
3248 return ret;
3249 }
3250
3251 10171 ret = export_stream_params_from_sei(s);
3252
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10171 times.
10171 if (ret < 0)
3253 return ret;
3254
3255 10171 ret = ff_hevc_set_new_ref(s, l, s->poc);
3256
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10171 times.
10171 if (ret < 0)
3257 goto fail;
3258
3259 10171 ret = ff_hevc_frame_rps(s, l);
3260
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10171 times.
10171 if (ret < 0) {
3261 av_log(s->avctx, AV_LOG_ERROR, "Error constructing the frame RPS.\n");
3262 goto fail;
3263 }
3264
3265
3/4
✓ Branch 0 taken 712 times.
✓ Branch 1 taken 9459 times.
✓ Branch 2 taken 712 times.
✗ Branch 3 not taken.
10171 if (IS_IRAP(s))
3266 712 s->cur_frame->f->flags |= AV_FRAME_FLAG_KEY;
3267 else
3268 9459 s->cur_frame->f->flags &= ~AV_FRAME_FLAG_KEY;
3269
3270 30513 s->cur_frame->needs_fg = (s->sei.common.film_grain_characteristics.present ||
3271
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10171 times.
10171 s->sei.common.aom_film_grain.enable) &&
3272
1/4
✓ Branch 0 taken 10171 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
20342 !(s->avctx->export_side_data & AV_CODEC_EXPORT_DATA_FILM_GRAIN) &&
3273 !s->avctx->hwaccel;
3274
3275 10171 ret = set_side_data(s);
3276
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10171 times.
10171 if (ret < 0)
3277 goto fail;
3278
3279
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10171 times.
10171 if (s->cur_frame->needs_fg &&
3280 (s->sei.common.film_grain_characteristics.present &&
3281 !ff_h274_film_grain_params_supported(s->sei.common.film_grain_characteristics.model_id,
3282 s->cur_frame->f->format) ||
3283 !av_film_grain_params_select(s->cur_frame->f))) {
3284 av_log_once(s->avctx, AV_LOG_WARNING, AV_LOG_DEBUG, &s->film_grain_warning_shown,
3285 "Unsupported film grain parameters. Ignoring film grain.\n");
3286 s->cur_frame->needs_fg = 0;
3287 }
3288
3289
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10171 times.
10171 if (s->cur_frame->needs_fg) {
3290 s->cur_frame->frame_grain->format = s->cur_frame->f->format;
3291 s->cur_frame->frame_grain->width = s->cur_frame->f->width;
3292 s->cur_frame->frame_grain->height = s->cur_frame->f->height;
3293 if ((ret = ff_thread_get_buffer(s->avctx, s->cur_frame->frame_grain, 0)) < 0)
3294 goto fail;
3295
3296 ret = av_frame_copy_props(s->cur_frame->frame_grain, s->cur_frame->f);
3297 if (ret < 0)
3298 goto fail;
3299 }
3300
3301 10171 s->cur_frame->f->pict_type = 3 - s->sh.slice_type;
3302
3303 10171 ret = ff_hevc_output_frames(s, s->layers_active_decode, s->layers_active_output,
3304 10171 sps->temporal_layer[sps->max_sub_layers - 1].num_reorder_pics,
3305 10171 sps->temporal_layer[sps->max_sub_layers - 1].max_dec_pic_buffering, 0);
3306
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10171 times.
10171 if (ret < 0)
3307 goto fail;
3308
3309
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10171 times.
10171 if (s->avctx->hwaccel) {
3310 ret = FF_HW_CALL(s->avctx, start_frame, NULL, 0);
3311 if (ret < 0)
3312 goto fail;
3313 }
3314
3315 // after starting the base-layer frame we know which layers will be decoded,
3316 // so we can now figure out which NALUs to wait for before we can call
3317 // ff_thread_finish_setup()
3318
2/2
✓ Branch 0 taken 9894 times.
✓ Branch 1 taken 277 times.
10171 if (l == &s->layers[0])
3319 9894 s->finish_setup_nal_idx = find_finish_setup_nal(s);
3320
3321
2/2
✓ Branch 0 taken 9893 times.
✓ Branch 1 taken 278 times.
10171 if (nal_idx >= s->finish_setup_nal_idx)
3322 9893 ff_thread_finish_setup(s->avctx);
3323
3324 10171 return 0;
3325
3326 fail:
3327 if (l->cur_frame)
3328 ff_hevc_unref_frame(l->cur_frame, ~0);
3329 l->cur_frame = NULL;
3330 s->cur_frame = s->collocated_ref = NULL;
3331 s->slice_initialized = 0;
3332 return ret;
3333 }
3334
3335 static int verify_md5(HEVCContext *s, AVFrame *frame)
3336 {
3337 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(frame->format);
3338 char msg_buf[4 * (50 + 2 * 2 * 16 /* MD5-size */)];
3339 int pixel_shift;
3340 int err = 0;
3341 int i, j;
3342
3343 if (!desc)
3344 return AVERROR(EINVAL);
3345
3346 pixel_shift = desc->comp[0].depth > 8;
3347
3348 /* the checksums are LE, so we have to byteswap for >8bpp formats
3349 * on BE arches */
3350 #if HAVE_BIGENDIAN
3351 if (pixel_shift && !s->checksum_buf) {
3352 av_fast_malloc(&s->checksum_buf, &s->checksum_buf_size,
3353 FFMAX3(frame->linesize[0], frame->linesize[1],
3354 frame->linesize[2]));
3355 if (!s->checksum_buf)
3356 return AVERROR(ENOMEM);
3357 }
3358 #endif
3359
3360 msg_buf[0] = '\0';
3361 for (i = 0; frame->data[i]; i++) {
3362 int width = s->avctx->coded_width;
3363 int height = s->avctx->coded_height;
3364 int w = (i == 1 || i == 2) ? (width >> desc->log2_chroma_w) : width;
3365 int h = (i == 1 || i == 2) ? (height >> desc->log2_chroma_h) : height;
3366 uint8_t md5[16];
3367
3368 av_md5_init(s->md5_ctx);
3369 for (j = 0; j < h; j++) {
3370 const uint8_t *src = frame->data[i] + j * frame->linesize[i];
3371 #if HAVE_BIGENDIAN
3372 if (pixel_shift) {
3373 s->bdsp.bswap16_buf((uint16_t *) s->checksum_buf,
3374 (const uint16_t *) src, w);
3375 src = s->checksum_buf;
3376 }
3377 #endif
3378 av_md5_update(s->md5_ctx, src, w << pixel_shift);
3379 }
3380 av_md5_final(s->md5_ctx, md5);
3381
3382 #define MD5_PRI "%016" PRIx64 "%016" PRIx64
3383 #define MD5_PRI_ARG(buf) AV_RB64(buf), AV_RB64((const uint8_t*)(buf) + 8)
3384
3385 if (!memcmp(md5, s->sei.picture_hash.md5[i], 16)) {
3386 av_strlcatf(msg_buf, sizeof(msg_buf),
3387 "plane %d - correct " MD5_PRI "; ",
3388 i, MD5_PRI_ARG(md5));
3389 } else {
3390 av_strlcatf(msg_buf, sizeof(msg_buf),
3391 "mismatching checksum of plane %d - " MD5_PRI " != " MD5_PRI "; ",
3392 i, MD5_PRI_ARG(md5), MD5_PRI_ARG(s->sei.picture_hash.md5[i]));
3393 err = AVERROR_INVALIDDATA;
3394 }
3395 }
3396
3397 av_log(s->avctx, err < 0 ? AV_LOG_ERROR : AV_LOG_DEBUG,
3398 "Verifying checksum for frame with POC %d: %s\n",
3399 s->poc, msg_buf);
3400
3401 return err;
3402 }
3403
3404 10171 static int hevc_frame_end(HEVCContext *s, HEVCLayerContext *l)
3405 {
3406 10171 HEVCFrame *out = l->cur_frame;
3407 const AVFilmGrainParams *fgp;
3408 av_unused int ret;
3409
3410
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10171 times.
10171 if (out->needs_fg) {
3411 av_assert0(out->frame_grain->buf[0]);
3412 fgp = av_film_grain_params_select(out->f);
3413 switch (fgp->type) {
3414 case AV_FILM_GRAIN_PARAMS_NONE:
3415 av_assert0(0);
3416 return AVERROR_BUG;
3417 case AV_FILM_GRAIN_PARAMS_H274:
3418 ret = ff_h274_apply_film_grain(out->frame_grain, out->f,
3419 &s->h274db, fgp);
3420 break;
3421 case AV_FILM_GRAIN_PARAMS_AV1:
3422 ret = ff_aom_apply_film_grain(out->frame_grain, out->f, fgp);
3423 break;
3424 }
3425 av_assert1(ret >= 0);
3426 }
3427
3428
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10171 times.
10171 if (s->avctx->hwaccel) {
3429 ret = FF_HW_SIMPLE_CALL(s->avctx, end_frame);
3430 if (ret < 0) {
3431 av_log(s->avctx, AV_LOG_ERROR,
3432 "hardware accelerator failed to decode picture\n");
3433 return ret;
3434 }
3435 } else {
3436
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10171 times.
10171 if (s->avctx->err_recognition & AV_EF_CRCCHECK &&
3437 s->sei.picture_hash.is_md5) {
3438 ret = verify_md5(s, out->f);
3439 if (ret < 0 && s->avctx->err_recognition & AV_EF_EXPLODE)
3440 return ret;
3441 }
3442 }
3443 10171 s->sei.picture_hash.is_md5 = 0;
3444
3445 10171 av_log(s->avctx, AV_LOG_DEBUG, "Decoded frame with POC %zu/%d.\n",
3446 10171 l - s->layers, s->poc);
3447
3448 10171 return 0;
3449 }
3450
3451 28289 static int decode_slice(HEVCContext *s, unsigned nal_idx, GetBitContext *gb)
3452 {
3453
2/2
✓ Branch 0 taken 27862 times.
✓ Branch 1 taken 427 times.
28289 const int layer_idx = s->vps ? s->vps->layer_idx[s->nuh_layer_id] : 0;
3454 HEVCLayerContext *l;
3455 int ret;
3456
3457 // skip layers not requested to be decoded
3458 // layers_active_decode can only change while decoding a base-layer frame,
3459 // so we can check it for non-base layers
3460
1/2
✓ Branch 0 taken 28289 times.
✗ Branch 1 not taken.
28289 if (layer_idx < 0 ||
3461
4/4
✓ Branch 0 taken 282 times.
✓ Branch 1 taken 28007 times.
✓ Branch 2 taken 5 times.
✓ Branch 3 taken 277 times.
28289 (s->nuh_layer_id > 0 && !(s->layers_active_decode & (1 << layer_idx))))
3462 5 return 0;
3463
3464 28284 ret = hls_slice_header(&s->sh, s, gb);
3465
2/2
✓ Branch 0 taken 33 times.
✓ Branch 1 taken 28251 times.
28284 if (ret < 0) {
3466 // hls_slice_header() does not cleanup on failure thus the state now is inconsistant so we cannot use it on depandant slices
3467 33 s->slice_initialized = 0;
3468 33 return ret;
3469 }
3470
3471
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 28251 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
28251 if ((s->avctx->skip_frame >= AVDISCARD_BIDIR && s->sh.slice_type == HEVC_SLICE_B) ||
3472
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 28251 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
28251 (s->avctx->skip_frame >= AVDISCARD_NONINTRA && s->sh.slice_type != HEVC_SLICE_I) ||
3473
1/6
✗ Branch 0 not taken.
✓ Branch 1 taken 28251 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
28251 (s->avctx->skip_frame >= AVDISCARD_NONKEY && !IS_IRAP(s)) ||
3474
4/4
✓ Branch 0 taken 27106 times.
✓ Branch 1 taken 1145 times.
✓ Branch 2 taken 1066 times.
✓ Branch 3 taken 26040 times.
28251 ((s->nal_unit_type == HEVC_NAL_RASL_R || s->nal_unit_type == HEVC_NAL_RASL_N) &&
3475
2/2
✓ Branch 0 taken 85 times.
✓ Branch 1 taken 2126 times.
2211 s->no_rasl_output_flag)) {
3476 85 return 0;
3477 }
3478
3479 // switching to a new layer, mark previous layer's frame (if any) as done
3480
2/2
✓ Branch 0 taken 547 times.
✓ Branch 1 taken 27619 times.
28166 if (s->cur_layer != layer_idx &&
3481
2/2
✓ Branch 0 taken 277 times.
✓ Branch 1 taken 270 times.
547 s->layers[s->cur_layer].cur_frame &&
3482
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 277 times.
277 s->avctx->active_thread_type == FF_THREAD_FRAME)
3483 ff_progress_frame_report(&s->layers[s->cur_layer].cur_frame->tf, INT_MAX);
3484
3485 28166 s->cur_layer = layer_idx;
3486 28166 l = &s->layers[s->cur_layer];
3487
3488
2/2
✓ Branch 0 taken 10172 times.
✓ Branch 1 taken 17994 times.
28166 if (s->sh.first_slice_in_pic_flag) {
3489
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 10171 times.
10172 if (l->cur_frame) {
3490 1 av_log(s->avctx, AV_LOG_ERROR, "Two slices reporting being the first in the same frame.\n");
3491 1 return AVERROR_INVALIDDATA;
3492 }
3493
3494 10171 ret = hevc_frame_start(s, l, nal_idx);
3495
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10171 times.
10171 if (ret < 0)
3496 return ret;
3497
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 17994 times.
17994 } else if (!l->cur_frame) {
3498 av_log(s->avctx, AV_LOG_ERROR, "First slice in a frame missing.\n");
3499 return AVERROR_INVALIDDATA;
3500 }
3501
3502
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 28165 times.
28165 if (s->nal_unit_type != s->first_nal_type) {
3503 av_log(s->avctx, AV_LOG_ERROR,
3504 "Non-matching NAL types of the VCL NALUs: %d %d\n",
3505 s->first_nal_type, s->nal_unit_type);
3506 return AVERROR_INVALIDDATA;
3507 }
3508
3509 28165 ret = decode_slice_data(s, l, &s->pkt.nals[nal_idx], gb);
3510
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 28164 times.
28165 if (ret < 0)
3511 1 return ret;
3512
3513 28164 return 0;
3514 }
3515
3516 39070 static int decode_nal_unit(HEVCContext *s, unsigned nal_idx)
3517 {
3518 39070 H2645NAL *nal = &s->pkt.nals[nal_idx];
3519 39070 GetBitContext gb = nal->gb;
3520 int ret;
3521
3522 39070 s->nal_unit_type = nal->type;
3523 39070 s->nuh_layer_id = nal->nuh_layer_id;
3524 39070 s->temporal_id = nal->temporal_id;
3525
3526
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 39070 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
39070 if (FF_HW_HAS_CB(s->avctx, decode_params) &&
3527 (s->nal_unit_type == HEVC_NAL_VPS ||
3528 s->nal_unit_type == HEVC_NAL_SPS ||
3529 s->nal_unit_type == HEVC_NAL_PPS ||
3530 s->nal_unit_type == HEVC_NAL_SEI_PREFIX ||
3531 s->nal_unit_type == HEVC_NAL_SEI_SUFFIX)) {
3532 ret = FF_HW_CALL(s->avctx, decode_params,
3533 nal->type, nal->raw_data, nal->raw_size);
3534 if (ret < 0)
3535 goto fail;
3536 }
3537
3538
6/7
✓ Branch 0 taken 432 times.
✓ Branch 1 taken 441 times.
✓ Branch 2 taken 1401 times.
✓ Branch 3 taken 7825 times.
✓ Branch 4 taken 28289 times.
✓ Branch 5 taken 682 times.
✗ Branch 6 not taken.
39070 switch (s->nal_unit_type) {
3539 432 case HEVC_NAL_VPS:
3540 432 ret = ff_hevc_decode_nal_vps(&gb, s->avctx, &s->ps);
3541
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 432 times.
432 if (ret < 0)
3542 goto fail;
3543 432 break;
3544 441 case HEVC_NAL_SPS:
3545 441 ret = ff_hevc_decode_nal_sps(&gb, s->avctx, &s->ps,
3546 441 nal->nuh_layer_id, s->apply_defdispwin);
3547
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 441 times.
441 if (ret < 0)
3548 goto fail;
3549 441 break;
3550 1401 case HEVC_NAL_PPS:
3551 1401 ret = ff_hevc_decode_nal_pps(&gb, s->avctx, &s->ps);
3552
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1399 times.
1401 if (ret < 0)
3553 2 goto fail;
3554 1399 break;
3555 7825 case HEVC_NAL_SEI_PREFIX:
3556 case HEVC_NAL_SEI_SUFFIX:
3557 7825 ret = ff_hevc_decode_nal_sei(&gb, s->avctx, &s->sei, &s->ps, s->nal_unit_type);
3558
2/2
✓ Branch 0 taken 33 times.
✓ Branch 1 taken 7792 times.
7825 if (ret < 0)
3559 33 goto fail;
3560 7792 break;
3561 28289 case HEVC_NAL_TRAIL_R:
3562 case HEVC_NAL_TRAIL_N:
3563 case HEVC_NAL_TSA_N:
3564 case HEVC_NAL_TSA_R:
3565 case HEVC_NAL_STSA_N:
3566 case HEVC_NAL_STSA_R:
3567 case HEVC_NAL_BLA_W_LP:
3568 case HEVC_NAL_BLA_W_RADL:
3569 case HEVC_NAL_BLA_N_LP:
3570 case HEVC_NAL_IDR_W_RADL:
3571 case HEVC_NAL_IDR_N_LP:
3572 case HEVC_NAL_CRA_NUT:
3573 case HEVC_NAL_RADL_N:
3574 case HEVC_NAL_RADL_R:
3575 case HEVC_NAL_RASL_N:
3576 case HEVC_NAL_RASL_R:
3577 28289 ret = decode_slice(s, nal_idx, &gb);
3578
2/2
✓ Branch 0 taken 35 times.
✓ Branch 1 taken 28254 times.
28289 if (ret < 0)
3579 35 goto fail;
3580 28254 break;
3581 682 case HEVC_NAL_EOS_NUT:
3582 case HEVC_NAL_EOB_NUT:
3583 case HEVC_NAL_AUD:
3584 case HEVC_NAL_FD_NUT:
3585 case HEVC_NAL_UNSPEC62:
3586 682 break;
3587 default:
3588 av_log(s->avctx, AV_LOG_INFO,
3589 "Skipping NAL unit %d\n", s->nal_unit_type);
3590 }
3591
3592 39000 return 0;
3593 70 fail:
3594
1/2
✓ Branch 0 taken 70 times.
✗ Branch 1 not taken.
70 if (ret == AVERROR_INVALIDDATA &&
3595
1/2
✓ Branch 0 taken 70 times.
✗ Branch 1 not taken.
70 !(s->avctx->err_recognition & AV_EF_EXPLODE)) {
3596 70 av_log(s->avctx, AV_LOG_WARNING,
3597 70 "Skipping invalid undecodable NALU: %d\n", s->nal_unit_type);
3598 70 return 0;
3599 }
3600 return ret;
3601 }
3602
3603 9971 static int decode_nal_units(HEVCContext *s, const uint8_t *buf, int length)
3604 {
3605 9971 int i, ret = 0;
3606 9971 int eos_at_start = 1;
3607
2/2
✓ Branch 0 taken 120 times.
✓ Branch 1 taken 9851 times.
9971 int flags = (H2645_FLAG_IS_NALFF * !!s->is_nalff) | H2645_FLAG_SMALL_PADDING;
3608
3609 9971 s->cur_frame = s->collocated_ref = NULL;
3610 9971 s->last_eos = s->eos;
3611 9971 s->eos = 0;
3612 9971 s->slice_initialized = 0;
3613
3614
2/2
✓ Branch 0 taken 19942 times.
✓ Branch 1 taken 9971 times.
29913 for (int i = 0; i < FF_ARRAY_ELEMS(s->layers); i++) {
3615 19942 HEVCLayerContext *l = &s->layers[i];
3616 19942 l->cur_frame = NULL;
3617 }
3618
3619 /* split the input packet into NAL units, so we know the upper bound on the
3620 * number of slices in the frame */
3621 9971 ret = ff_h2645_packet_split(&s->pkt, buf, length, s->avctx,
3622 9971 s->nal_length_size, s->avctx->codec_id, flags);
3623
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9971 times.
9971 if (ret < 0) {
3624 av_log(s->avctx, AV_LOG_ERROR,
3625 "Error splitting the input into NAL units.\n");
3626 return ret;
3627 }
3628
3629
2/2
✓ Branch 0 taken 39070 times.
✓ Branch 1 taken 9971 times.
49041 for (i = 0; i < s->pkt.nb_nals; i++) {
3630
2/2
✓ Branch 0 taken 39069 times.
✓ Branch 1 taken 1 times.
39070 if (s->pkt.nals[i].type == HEVC_NAL_EOB_NUT ||
3631
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 39066 times.
39069 s->pkt.nals[i].type == HEVC_NAL_EOS_NUT) {
3632
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 if (eos_at_start) {
3633 4 s->last_eos = 1;
3634 } else {
3635 s->eos = 1;
3636 }
3637 } else {
3638 39066 eos_at_start = 0;
3639 }
3640 }
3641
3642 /*
3643 * Check for RPU delimiter.
3644 *
3645 * Dolby Vision RPUs masquerade as unregistered NALs of type 62.
3646 *
3647 * We have to do this check here an create the rpu buffer, since RPUs are appended
3648 * to the end of an AU; they are the last non-EOB/EOS NAL in the AU.
3649 */
3650
4/4
✓ Branch 0 taken 8051 times.
✓ Branch 1 taken 1920 times.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 8049 times.
9971 if (s->pkt.nb_nals > 1 && s->pkt.nals[s->pkt.nb_nals - 1].type == HEVC_NAL_UNSPEC62 &&
3651
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
2 s->pkt.nals[s->pkt.nb_nals - 1].size > 2 && !s->pkt.nals[s->pkt.nb_nals - 1].nuh_layer_id
3652
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 && !s->pkt.nals[s->pkt.nb_nals - 1].temporal_id) {
3653 2 H2645NAL *nal = &s->pkt.nals[s->pkt.nb_nals - 1];
3654
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (s->rpu_buf) {
3655 av_buffer_unref(&s->rpu_buf);
3656 av_log(s->avctx, AV_LOG_WARNING, "Multiple Dolby Vision RPUs found in one AU. Skipping previous.\n");
3657 }
3658
3659 2 s->rpu_buf = av_buffer_alloc(nal->raw_size - 2);
3660
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (!s->rpu_buf)
3661 return AVERROR(ENOMEM);
3662 2 memcpy(s->rpu_buf->data, nal->raw_data + 2, nal->raw_size - 2);
3663
3664 2 ret = ff_dovi_rpu_parse(&s->dovi_ctx, nal->data + 2, nal->size - 2,
3665 2 s->avctx->err_recognition);
3666
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (ret < 0) {
3667 av_buffer_unref(&s->rpu_buf);
3668 av_log(s->avctx, AV_LOG_WARNING, "Error parsing DOVI NAL unit.\n");
3669 /* ignore */
3670 }
3671 }
3672
3673 /* decode the NAL units */
3674
2/2
✓ Branch 0 taken 39070 times.
✓ Branch 1 taken 9971 times.
49041 for (i = 0; i < s->pkt.nb_nals; i++) {
3675 39070 H2645NAL *nal = &s->pkt.nals[i];
3676
3677
1/2
✓ Branch 0 taken 39070 times.
✗ Branch 1 not taken.
39070 if (s->avctx->skip_frame >= AVDISCARD_ALL ||
3678
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 39070 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
39070 (s->avctx->skip_frame >= AVDISCARD_NONREF && ff_hevc_nal_is_nonref(nal->type)))
3679 continue;
3680
3681 39070 ret = decode_nal_unit(s, i);
3682
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 39070 times.
39070 if (ret < 0) {
3683 av_log(s->avctx, AV_LOG_WARNING,
3684 "Error parsing NAL unit #%d.\n", i);
3685 goto fail;
3686 }
3687 }
3688
3689 9971 fail:
3690
2/2
✓ Branch 0 taken 19942 times.
✓ Branch 1 taken 9971 times.
29913 for (int i = 0; i < FF_ARRAY_ELEMS(s->layers); i++) {
3691 19942 HEVCLayerContext *l = &s->layers[i];
3692
3693
2/2
✓ Branch 0 taken 9771 times.
✓ Branch 1 taken 10171 times.
19942 if (!l->cur_frame)
3694 9771 continue;
3695
3696
1/2
✓ Branch 0 taken 10171 times.
✗ Branch 1 not taken.
10171 if (ret >= 0)
3697 10171 ret = hevc_frame_end(s, l);
3698
3699
2/2
✓ Branch 0 taken 33 times.
✓ Branch 1 taken 10138 times.
10171 if (s->avctx->active_thread_type == FF_THREAD_FRAME)
3700 33 ff_progress_frame_report(&l->cur_frame->tf, INT_MAX);
3701 }
3702
3703 9971 return ret;
3704 }
3705
3706 249 static int hevc_decode_extradata(HEVCContext *s, uint8_t *buf, int length, int first)
3707 {
3708 int ret, i;
3709
3710 249 ret = ff_hevc_decode_extradata(buf, length, &s->ps, &s->sei, &s->is_nalff,
3711 249 &s->nal_length_size, s->avctx->err_recognition,
3712 249 s->apply_defdispwin, s->avctx);
3713
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 249 times.
249 if (ret < 0)
3714 return ret;
3715
3716 /* export stream parameters from the first SPS */
3717
2/2
✓ Branch 0 taken 279 times.
✓ Branch 1 taken 2 times.
281 for (i = 0; i < FF_ARRAY_ELEMS(s->ps.sps_list); i++) {
3718
4/4
✓ Branch 0 taken 263 times.
✓ Branch 1 taken 16 times.
✓ Branch 2 taken 247 times.
✓ Branch 3 taken 16 times.
279 if (first && s->ps.sps_list[i]) {
3719 247 const HEVCSPS *sps = s->ps.sps_list[i];
3720 247 export_stream_params(s, sps);
3721
3722 247 ret = export_multilayer(s, sps->vps);
3723
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 247 times.
247 if (ret < 0)
3724 return ret;
3725
3726 247 break;
3727 }
3728 }
3729
3730 /* export stream parameters from SEI */
3731 249 ret = export_stream_params_from_sei(s);
3732
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 249 times.
249 if (ret < 0)
3733 return ret;
3734
3735 249 return 0;
3736 }
3737
3738 21117 static int hevc_receive_frame(AVCodecContext *avctx, AVFrame *frame)
3739 {
3740 21117 HEVCContext *s = avctx->priv_data;
3741 21117 AVCodecInternal *avci = avctx->internal;
3742 21117 AVPacket *avpkt = avci->in_pkt;
3743
3744 int ret;
3745 uint8_t *sd;
3746 size_t sd_size;
3747
3748 21117 s->pkt_dts = AV_NOPTS_VALUE;
3749
3750
2/2
✓ Branch 1 taken 948 times.
✓ Branch 2 taken 20169 times.
21117 if (ff_container_fifo_can_read(s->output_fifo))
3751 948 goto do_output;
3752
3753 20169 av_packet_unref(avpkt);
3754 20169 ret = ff_decode_get_packet(avctx, avpkt);
3755
2/2
✓ Branch 0 taken 303 times.
✓ Branch 1 taken 19866 times.
20169 if (ret == AVERROR_EOF) {
3756 303 ret = ff_hevc_output_frames(s, s->layers_active_decode,
3757 s->layers_active_output, 0, 0, 0);
3758
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 303 times.
303 if (ret < 0)
3759 return ret;
3760 303 goto do_output;
3761
2/2
✓ Branch 0 taken 9895 times.
✓ Branch 1 taken 9971 times.
19866 } else if (ret < 0)
3762 9895 return ret;
3763
3764 9971 s->pkt_dts = avpkt->dts;
3765
3766 9971 sd = av_packet_get_side_data(avpkt, AV_PKT_DATA_NEW_EXTRADATA, &sd_size);
3767
3/4
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 9970 times.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
9971 if (sd && sd_size > 0) {
3768 1 ret = hevc_decode_extradata(s, sd, sd_size, 0);
3769
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (ret < 0)
3770 return ret;
3771 }
3772
3773 9971 sd = av_packet_get_side_data(avpkt, AV_PKT_DATA_DOVI_CONF, &sd_size);
3774
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 9971 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
9971 if (sd && sd_size >= sizeof(s->dovi_ctx.cfg)) {
3775 int old = s->dovi_ctx.cfg.dv_profile;
3776 s->dovi_ctx.cfg = *(AVDOVIDecoderConfigurationRecord *) sd;
3777 if (old)
3778 av_log(avctx, AV_LOG_DEBUG,
3779 "New DOVI configuration record from input packet (profile %d -> %u).\n",
3780 old, s->dovi_ctx.cfg.dv_profile);
3781 }
3782
3783 9971 ret = decode_nal_units(s, avpkt->data, avpkt->size);
3784
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9971 times.
9971 if (ret < 0)
3785 return ret;
3786
3787 9971 do_output:
3788
2/2
✓ Branch 1 taken 10006 times.
✓ Branch 2 taken 1216 times.
11222 if (ff_container_fifo_read(s->output_fifo, frame) >= 0) {
3789
1/2
✓ Branch 0 taken 10006 times.
✗ Branch 1 not taken.
10006 if (!(avctx->export_side_data & AV_CODEC_EXPORT_DATA_FILM_GRAIN))
3790 10006 av_frame_remove_side_data(frame, AV_FRAME_DATA_FILM_GRAIN_PARAMS);
3791
3792 10006 return 0;
3793 }
3794
3795
2/2
✓ Branch 0 taken 186 times.
✓ Branch 1 taken 1030 times.
1216 return avci->draining ? AVERROR_EOF : AVERROR(EAGAIN);
3796 }
3797
3798 67 static int hevc_ref_frame(HEVCFrame *dst, const HEVCFrame *src)
3799 {
3800 int ret;
3801
3802 67 ff_progress_frame_ref(&dst->tf, &src->tf);
3803
3804
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 67 times.
67 if (src->needs_fg) {
3805 ret = av_frame_ref(dst->frame_grain, src->frame_grain);
3806 if (ret < 0) {
3807 ff_hevc_unref_frame(dst, ~0);
3808 return ret;
3809 }
3810 dst->needs_fg = 1;
3811 }
3812
3813 67 dst->pps = ff_refstruct_ref_c(src->pps);
3814 67 dst->tab_mvf = ff_refstruct_ref(src->tab_mvf);
3815 67 dst->rpl_tab = ff_refstruct_ref(src->rpl_tab);
3816 67 dst->rpl = ff_refstruct_ref(src->rpl);
3817 67 dst->nb_rpl_elems = src->nb_rpl_elems;
3818
3819 67 dst->poc = src->poc;
3820 67 dst->ctb_count = src->ctb_count;
3821 67 dst->flags = src->flags;
3822
3823 67 dst->base_layer_frame = src->base_layer_frame;
3824
3825 67 ff_refstruct_replace(&dst->hwaccel_picture_private,
3826 67 src->hwaccel_picture_private);
3827
3828 67 return 0;
3829 }
3830
3831 456 static av_cold int hevc_decode_free(AVCodecContext *avctx)
3832 {
3833 456 HEVCContext *s = avctx->priv_data;
3834
3835
2/2
✓ Branch 0 taken 912 times.
✓ Branch 1 taken 456 times.
1368 for (int i = 0; i < FF_ARRAY_ELEMS(s->layers); i++) {
3836 912 pic_arrays_free(&s->layers[i]);
3837 912 ff_refstruct_unref(&s->layers[i].sps);
3838 }
3839
3840 456 ff_refstruct_unref(&s->vps);
3841 456 ff_refstruct_unref(&s->pps);
3842
3843 456 ff_dovi_ctx_unref(&s->dovi_ctx);
3844 456 av_buffer_unref(&s->rpu_buf);
3845
3846 456 av_freep(&s->md5_ctx);
3847
3848 456 ff_container_fifo_free(&s->output_fifo);
3849
3850
2/2
✓ Branch 0 taken 912 times.
✓ Branch 1 taken 456 times.
1368 for (int layer = 0; layer < FF_ARRAY_ELEMS(s->layers); layer++) {
3851 912 HEVCLayerContext *l = &s->layers[layer];
3852
2/2
✓ Branch 0 taken 29184 times.
✓ Branch 1 taken 912 times.
30096 for (int i = 0; i < FF_ARRAY_ELEMS(l->DPB); i++) {
3853 29184 ff_hevc_unref_frame(&l->DPB[i], ~0);
3854 29184 av_frame_free(&l->DPB[i].frame_grain);
3855 }
3856 }
3857
3858 456 ff_hevc_ps_uninit(&s->ps);
3859
3860
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 456 times.
456 for (int i = 0; i < s->nb_wpp_progress; i++)
3861 ff_thread_progress_destroy(&s->wpp_progress[i]);
3862 456 av_freep(&s->wpp_progress);
3863
3864 456 av_freep(&s->sh.entry_point_offset);
3865 456 av_freep(&s->sh.offset);
3866 456 av_freep(&s->sh.size);
3867
3868 456 av_freep(&s->local_ctx);
3869
3870 456 ff_h2645_packet_uninit(&s->pkt);
3871
3872 456 ff_hevc_reset_sei(&s->sei);
3873
3874 456 return 0;
3875 }
3876
3877 456 static av_cold int hevc_init_context(AVCodecContext *avctx)
3878 {
3879 456 HEVCContext *s = avctx->priv_data;
3880
3881 456 s->avctx = avctx;
3882
3883 456 s->local_ctx = av_mallocz(sizeof(*s->local_ctx));
3884
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 456 times.
456 if (!s->local_ctx)
3885 return AVERROR(ENOMEM);
3886 456 s->nb_local_ctx = 1;
3887
3888 456 s->local_ctx[0].parent = s;
3889 456 s->local_ctx[0].logctx = avctx;
3890 456 s->local_ctx[0].common_cabac_state = &s->cabac;
3891
3892 456 s->output_fifo = ff_container_fifo_alloc_avframe(0);
3893
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 456 times.
456 if (!s->output_fifo)
3894 return AVERROR(ENOMEM);
3895
3896
2/2
✓ Branch 0 taken 912 times.
✓ Branch 1 taken 456 times.
1368 for (int layer = 0; layer < FF_ARRAY_ELEMS(s->layers); layer++) {
3897 912 HEVCLayerContext *l = &s->layers[layer];
3898
2/2
✓ Branch 0 taken 29184 times.
✓ Branch 1 taken 912 times.
30096 for (int i = 0; i < FF_ARRAY_ELEMS(l->DPB); i++) {
3899 29184 l->DPB[i].frame_grain = av_frame_alloc();
3900
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 29184 times.
29184 if (!l->DPB[i].frame_grain)
3901 return AVERROR(ENOMEM);
3902 }
3903 }
3904
3905 456 s->md5_ctx = av_md5_alloc();
3906
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 456 times.
456 if (!s->md5_ctx)
3907 return AVERROR(ENOMEM);
3908
3909 456 ff_bswapdsp_init(&s->bdsp);
3910
3911 456 s->dovi_ctx.logctx = avctx;
3912 456 s->eos = 0;
3913
3914 456 ff_hevc_reset_sei(&s->sei);
3915
3916 456 return 0;
3917 }
3918
3919 #if HAVE_THREADS
3920 32 static int hevc_update_thread_context(AVCodecContext *dst,
3921 const AVCodecContext *src)
3922 {
3923 32 HEVCContext *s = dst->priv_data;
3924 32 HEVCContext *s0 = src->priv_data;
3925 int ret;
3926
3927
2/2
✓ Branch 0 taken 64 times.
✓ Branch 1 taken 32 times.
96 for (int layer = 0; layer < FF_ARRAY_ELEMS(s->layers); layer++) {
3928 64 HEVCLayerContext *l = &s->layers[layer];
3929 64 const HEVCLayerContext *l0 = &s0->layers[layer];
3930
2/2
✓ Branch 0 taken 2048 times.
✓ Branch 1 taken 64 times.
2112 for (int i = 0; i < FF_ARRAY_ELEMS(l->DPB); i++) {
3931 2048 ff_hevc_unref_frame(&l->DPB[i], ~0);
3932
2/2
✓ Branch 0 taken 67 times.
✓ Branch 1 taken 1981 times.
2048 if (l0->DPB[i].f) {
3933 67 ret = hevc_ref_frame(&l->DPB[i], &l0->DPB[i]);
3934
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 67 times.
67 if (ret < 0)
3935 return ret;
3936 }
3937 }
3938
3939
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 63 times.
64 if (l->sps != l0->sps) {
3940 1 ret = set_sps(s, l, l0->sps);
3941
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (ret < 0)
3942 return ret;
3943 }
3944 }
3945
3946
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++)
3947 512 ff_refstruct_replace(&s->ps.vps_list[i], s0->ps.vps_list[i]);
3948
3949
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++)
3950 512 ff_refstruct_replace(&s->ps.sps_list[i], s0->ps.sps_list[i]);
3951
3952
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++)
3953 2048 ff_refstruct_replace(&s->ps.pps_list[i], s0->ps.pps_list[i]);
3954
3955 // PPS do not persist between frames
3956 32 ff_refstruct_unref(&s->pps);
3957
3958 32 s->poc_tid0 = s0->poc_tid0;
3959 32 s->eos = s0->eos;
3960 32 s->no_rasl_output_flag = s0->no_rasl_output_flag;
3961
3962 32 s->is_nalff = s0->is_nalff;
3963 32 s->nal_length_size = s0->nal_length_size;
3964 32 s->layers_active_decode = s0->layers_active_decode;
3965 32 s->layers_active_output = s0->layers_active_output;
3966
3967 32 s->film_grain_warning_shown = s0->film_grain_warning_shown;
3968
3969
1/2
✓ Branch 0 taken 32 times.
✗ Branch 1 not taken.
32 if (s->nb_view_ids != s0->nb_view_ids ||
3970
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)) {
3971 av_freep(&s->view_ids);
3972 s->nb_view_ids = 0;
3973
3974 if (s0->nb_view_ids) {
3975 s->view_ids = av_memdup(s0->view_ids, s0->nb_view_ids * sizeof(*s0->view_ids));
3976 if (!s->view_ids)
3977 return AVERROR(ENOMEM);
3978 s->nb_view_ids = s0->nb_view_ids;
3979 }
3980 }
3981
3982 32 ret = ff_h2645_sei_ctx_replace(&s->sei.common, &s0->sei.common);
3983
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 32 times.
32 if (ret < 0)
3984 return ret;
3985
3986 32 ret = av_buffer_replace(&s->sei.common.dynamic_hdr_plus.info,
3987 32 s0->sei.common.dynamic_hdr_plus.info);
3988
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 32 times.
32 if (ret < 0)
3989 return ret;
3990
3991 32 ret = av_buffer_replace(&s->rpu_buf, s0->rpu_buf);
3992
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 32 times.
32 if (ret < 0)
3993 return ret;
3994
3995 32 ff_dovi_ctx_replace(&s->dovi_ctx, &s0->dovi_ctx);
3996
3997 32 ret = av_buffer_replace(&s->sei.common.dynamic_hdr_vivid.info,
3998 32 s0->sei.common.dynamic_hdr_vivid.info);
3999
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 32 times.
32 if (ret < 0)
4000 return ret;
4001
4002 32 s->sei.common.frame_packing = s0->sei.common.frame_packing;
4003 32 s->sei.common.display_orientation = s0->sei.common.display_orientation;
4004 32 s->sei.common.alternative_transfer = s0->sei.common.alternative_transfer;
4005 32 s->sei.common.mastering_display = s0->sei.common.mastering_display;
4006 32 s->sei.common.content_light = s0->sei.common.content_light;
4007 32 s->sei.common.aom_film_grain = s0->sei.common.aom_film_grain;
4008 32 s->sei.tdrdi = s0->sei.tdrdi;
4009
4010 32 return 0;
4011 }
4012 #endif
4013
4014 456 static av_cold int hevc_decode_init(AVCodecContext *avctx)
4015 {
4016 456 HEVCContext *s = avctx->priv_data;
4017 int ret;
4018
4019 456 ret = hevc_init_context(avctx);
4020
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 456 times.
456 if (ret < 0)
4021 return ret;
4022
4023 456 s->sei.picture_timing.picture_struct = 0;
4024 456 s->eos = 1;
4025
4026 456 atomic_init(&s->wpp_err, 0);
4027
4028
2/2
✓ Branch 0 taken 455 times.
✓ Branch 1 taken 1 times.
456 if (!avctx->internal->is_copy) {
4029 const AVPacketSideData *sd;
4030
4031
3/4
✓ Branch 0 taken 248 times.
✓ Branch 1 taken 207 times.
✓ Branch 2 taken 248 times.
✗ Branch 3 not taken.
455 if (avctx->extradata_size > 0 && avctx->extradata) {
4032 248 ret = hevc_decode_extradata(s, avctx->extradata, avctx->extradata_size, 1);
4033
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 248 times.
248 if (ret < 0) {
4034 return ret;
4035 }
4036
4037 248 ret = ff_h2645_sei_to_context(avctx, &s->sei.common);
4038
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 248 times.
248 if (ret < 0)
4039 return ret;
4040 }
4041
4042 455 sd = ff_get_coded_side_data(avctx, AV_PKT_DATA_DOVI_CONF);
4043
3/4
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 444 times.
✓ Branch 2 taken 11 times.
✗ Branch 3 not taken.
455 if (sd && sd->size >= sizeof(s->dovi_ctx.cfg))
4044 11 s->dovi_ctx.cfg = *(AVDOVIDecoderConfigurationRecord *) sd->data;
4045 }
4046
4047 456 return 0;
4048 }
4049
4050 8 static void hevc_decode_flush(AVCodecContext *avctx)
4051 {
4052 8 HEVCContext *s = avctx->priv_data;
4053 8 ff_hevc_flush_dpb(s);
4054 8 ff_hevc_reset_sei(&s->sei);
4055 8 ff_dovi_ctx_flush(&s->dovi_ctx);
4056 8 av_buffer_unref(&s->rpu_buf);
4057 8 s->eos = 1;
4058
4059
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
8 if (FF_HW_HAS_CB(avctx, flush))
4060 FF_HW_SIMPLE_CALL(avctx, flush);
4061 8 }
4062
4063 #define OFFSET(x) offsetof(HEVCContext, x)
4064 #define PAR (AV_OPT_FLAG_DECODING_PARAM | AV_OPT_FLAG_VIDEO_PARAM)
4065
4066 static const AVOption options[] = {
4067 { "apply_defdispwin", "Apply default display window from VUI", OFFSET(apply_defdispwin),
4068 AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, PAR },
4069 { "strict-displaywin", "stricly apply default display window size", OFFSET(apply_defdispwin),
4070 AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, PAR },
4071 { "view_ids", "Array of view IDs that should be decoded and output; a single -1 to decode all views",
4072 .offset = OFFSET(view_ids), .type = AV_OPT_TYPE_INT | AV_OPT_TYPE_FLAG_ARRAY,
4073 .min = -1, .max = INT_MAX, .flags = PAR },
4074 { "view_ids_available", "Array of available view IDs is exported here",
4075 .offset = OFFSET(view_ids_available), .type = AV_OPT_TYPE_UINT | AV_OPT_TYPE_FLAG_ARRAY,
4076 .flags = PAR | AV_OPT_FLAG_EXPORT | AV_OPT_FLAG_READONLY },
4077 { "view_pos_available", "Array of view positions for view_ids_available is exported here, as AVStereo3DView",
4078 .offset = OFFSET(view_pos_available), .type = AV_OPT_TYPE_UINT | AV_OPT_TYPE_FLAG_ARRAY,
4079 .flags = PAR | AV_OPT_FLAG_EXPORT | AV_OPT_FLAG_READONLY, .unit = "view_pos" },
4080 { "unspecified", .type = AV_OPT_TYPE_CONST, .default_val = { .i64 = AV_STEREO3D_VIEW_UNSPEC }, .unit = "view_pos" },
4081 { "left", .type = AV_OPT_TYPE_CONST, .default_val = { .i64 = AV_STEREO3D_VIEW_LEFT }, .unit = "view_pos" },
4082 { "right", .type = AV_OPT_TYPE_CONST, .default_val = { .i64 = AV_STEREO3D_VIEW_RIGHT }, .unit = "view_pos" },
4083
4084 { NULL },
4085 };
4086
4087 static const AVClass hevc_decoder_class = {
4088 .class_name = "HEVC decoder",
4089 .item_name = av_default_item_name,
4090 .option = options,
4091 .version = LIBAVUTIL_VERSION_INT,
4092 };
4093
4094 const FFCodec ff_hevc_decoder = {
4095 .p.name = "hevc",
4096 CODEC_LONG_NAME("HEVC (High Efficiency Video Coding)"),
4097 .p.type = AVMEDIA_TYPE_VIDEO,
4098 .p.id = AV_CODEC_ID_HEVC,
4099 .priv_data_size = sizeof(HEVCContext),
4100 .p.priv_class = &hevc_decoder_class,
4101 .init = hevc_decode_init,
4102 .close = hevc_decode_free,
4103 FF_CODEC_RECEIVE_FRAME_CB(hevc_receive_frame),
4104 .flush = hevc_decode_flush,
4105 UPDATE_THREAD_CONTEXT(hevc_update_thread_context),
4106 .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DELAY |
4107 AV_CODEC_CAP_SLICE_THREADS | AV_CODEC_CAP_FRAME_THREADS,
4108 .caps_internal = FF_CODEC_CAP_EXPORTS_CROPPING |
4109 FF_CODEC_CAP_USES_PROGRESSFRAMES |
4110 FF_CODEC_CAP_INIT_CLEANUP,
4111 .p.profiles = NULL_IF_CONFIG_SMALL(ff_hevc_profiles),
4112 .hw_configs = (const AVCodecHWConfigInternal *const []) {
4113 #if CONFIG_HEVC_DXVA2_HWACCEL
4114 HWACCEL_DXVA2(hevc),
4115 #endif
4116 #if CONFIG_HEVC_D3D11VA_HWACCEL
4117 HWACCEL_D3D11VA(hevc),
4118 #endif
4119 #if CONFIG_HEVC_D3D11VA2_HWACCEL
4120 HWACCEL_D3D11VA2(hevc),
4121 #endif
4122 #if CONFIG_HEVC_D3D12VA_HWACCEL
4123 HWACCEL_D3D12VA(hevc),
4124 #endif
4125 #if CONFIG_HEVC_NVDEC_HWACCEL
4126 HWACCEL_NVDEC(hevc),
4127 #endif
4128 #if CONFIG_HEVC_VAAPI_HWACCEL
4129 HWACCEL_VAAPI(hevc),
4130 #endif
4131 #if CONFIG_HEVC_VDPAU_HWACCEL
4132 HWACCEL_VDPAU(hevc),
4133 #endif
4134 #if CONFIG_HEVC_VIDEOTOOLBOX_HWACCEL
4135 HWACCEL_VIDEOTOOLBOX(hevc),
4136 #endif
4137 #if CONFIG_HEVC_VULKAN_HWACCEL
4138 HWACCEL_VULKAN(hevc),
4139 #endif
4140 NULL
4141 },
4142 };
4143