FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/hevc/refs.c
Date: 2026-06-16 12:54:33
Exec Total Coverage
Lines: 317 370 85.7%
Functions: 16 17 94.1%
Branches: 230 306 75.2%

Line Branch Exec Source
1 /*
2 * HEVC video decoder
3 *
4 * Copyright (C) 2012 - 2013 Guillaume Martres
5 * Copyright (C) 2012 - 2013 Gildas Cocherel
6 *
7 * This file is part of FFmpeg.
8 *
9 * FFmpeg is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
13 *
14 * FFmpeg is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with FFmpeg; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 */
23
24 #include "libavutil/container_fifo.h"
25 #include "libavutil/mem.h"
26 #include "libavutil/stereo3d.h"
27
28 #include "libavcodec/decode.h"
29 #include "hevc.h"
30 #include "hevcdec.h"
31 #include "libavcodec/progressframe.h"
32 #include "libavcodec/thread.h"
33 #include "libavutil/refstruct.h"
34
35 410605 void ff_hevc_unref_frame(HEVCFrame *frame, int flags)
36 {
37 410605 frame->flags &= ~flags;
38
2/2
✓ Branch 0 taken 351401 times.
✓ Branch 1 taken 59204 times.
410605 if (!(frame->flags & ~HEVC_FRAME_FLAG_CORRUPT))
39 351401 frame->flags = 0;
40
2/2
✓ Branch 0 taken 351401 times.
✓ Branch 1 taken 59204 times.
410605 if (!frame->flags) {
41 351401 ff_progress_frame_unref(&frame->tf);
42 351401 av_frame_unref(frame->frame_grain);
43 351401 frame->needs_fg = 0;
44
45 351401 av_refstruct_unref(&frame->pps);
46 351401 av_refstruct_unref(&frame->tab_mvf);
47
48 351401 av_refstruct_unref(&frame->rpl);
49 351401 frame->nb_rpl_elems = 0;
50 351401 av_refstruct_unref(&frame->rpl_tab);
51 351401 frame->refPicList = NULL;
52
53 351401 av_refstruct_unref(&frame->hwaccel_picture_private);
54 }
55 410605 }
56
57 5148683 const RefPicList *ff_hevc_get_ref_list(const HEVCFrame *ref, int x0, int y0)
58 {
59 5148683 const HEVCSPS *sps = ref->pps->sps;
60 5148683 int x_cb = x0 >> sps->log2_ctb_size;
61 5148683 int y_cb = y0 >> sps->log2_ctb_size;
62 5148683 int pic_width_cb = sps->ctb_width;
63 5148683 int ctb_addr_ts = ref->pps->ctb_addr_rs_to_ts[y_cb * pic_width_cb + x_cb];
64 5148683 return &ref->rpl_tab[ctb_addr_ts]->refPicList[0];
65 }
66
67 891 void ff_hevc_clear_refs(HEVCLayerContext *l)
68 {
69 int i;
70
2/2
✓ Branch 0 taken 28512 times.
✓ Branch 1 taken 891 times.
29403 for (i = 0; i < FF_ARRAY_ELEMS(l->DPB); i++)
71 28512 ff_hevc_unref_frame(&l->DPB[i],
72 HEVC_FRAME_FLAG_SHORT_REF |
73 HEVC_FRAME_FLAG_LONG_REF);
74 891 }
75
76 void ff_hevc_flush_dpb(HEVCContext *s)
77 {
78 for (int layer = 0; layer < FF_ARRAY_ELEMS(s->layers); layer++) {
79 HEVCLayerContext *l = &s->layers[layer];
80 for (int i = 0; i < FF_ARRAY_ELEMS(l->DPB); i++)
81 ff_hevc_unref_frame(&l->DPB[i], ~0);
82 }
83 }
84
85 50 static int replace_alpha_plane(AVFrame *alpha, AVFrame *base)
86 {
87 50 AVBufferRef *base_a = av_frame_get_plane_buffer(base, 3);
88 50 uintptr_t data = (uintptr_t)alpha->data[0];
89 int ret;
90
91
2/4
✓ Branch 0 taken 50 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 50 times.
✗ Branch 3 not taken.
50 for (int i = 0; i < FF_ARRAY_ELEMS(alpha->buf) && alpha->buf[i]; i++) {
92 50 AVBufferRef *buf = alpha->buf[i];
93 50 uintptr_t buf_begin = (uintptr_t)buf->data;
94
95
2/4
✓ Branch 0 taken 50 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 50 times.
✗ Branch 3 not taken.
50 if (data >= buf_begin && data < buf_begin + buf->size) {
96 50 ret = av_buffer_replace(&alpha->buf[i], base_a);
97
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 50 times.
50 if (ret < 0)
98 return ret;
99
100 50 alpha->linesize[0] = base->linesize[3];
101 50 alpha->data[0] = base->data[3];
102
103 50 return 0;
104 }
105 }
106
107 return AVERROR_BUG;
108 }
109
110 10523 static HEVCFrame *alloc_frame(HEVCContext *s, HEVCLayerContext *l)
111 {
112 10523 const HEVCVPS *vps = l->sps->vps;
113 10523 const int view_id = vps->view_id[s->cur_layer];
114 int i, j, ret;
115
1/2
✓ Branch 0 taken 36645 times.
✗ Branch 1 not taken.
36645 for (i = 0; i < FF_ARRAY_ELEMS(l->DPB); i++) {
116 36645 HEVCFrame *frame = &l->DPB[i];
117
2/2
✓ Branch 0 taken 26122 times.
✓ Branch 1 taken 10523 times.
36645 if (frame->f)
118 26122 continue;
119
120 10523 ret = ff_progress_frame_alloc(s->avctx, &frame->tf);
121
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10523 times.
10523 if (ret < 0)
122 10523 return NULL;
123
124 // Add LCEVC SEI metadata here, as it's needed in get_buffer()
125
2/2
✓ Branch 0 taken 32 times.
✓ Branch 1 taken 10491 times.
10523 if (s->sei.common.itut_t35.lcevc) {
126 32 ret = ff_frame_new_side_data_from_buf(s->avctx, frame->tf.f,
127 AV_FRAME_DATA_LCEVC, &s->sei.common.itut_t35.lcevc);
128
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 32 times.
32 if (ret < 0)
129 goto fail;
130 }
131
132 // add view ID side data if it's nontrivial
133
5/6
✓ Branch 1 taken 10423 times.
✓ Branch 2 taken 100 times.
✓ Branch 3 taken 9738 times.
✓ Branch 4 taken 685 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 9738 times.
10523 if (!ff_hevc_is_alpha_video(s) && (vps->nb_layers > 1 || view_id)) {
134 685 HEVCSEITDRDI *tdrdi = &s->sei.tdrdi;
135 685 AVFrameSideData *sd = av_frame_side_data_new(&frame->f->side_data,
136 685 &frame->f->nb_side_data,
137 AV_FRAME_DATA_VIEW_ID,
138 sizeof(int), 0);
139
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 685 times.
685 if (!sd)
140 goto fail;
141 685 *(int*)sd->data = view_id;
142
143
2/2
✓ Branch 0 taken 22 times.
✓ Branch 1 taken 663 times.
685 if (tdrdi->num_ref_displays) {
144 AVStereo3D *stereo_3d;
145
146 22 stereo_3d = av_stereo3d_create_side_data(frame->f);
147
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 22 times.
22 if (!stereo_3d)
148 goto fail;
149
150 22 stereo_3d->type = AV_STEREO3D_FRAMESEQUENCE;
151
2/2
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 11 times.
22 if (tdrdi->left_view_id[0] == view_id)
152 11 stereo_3d->view = AV_STEREO3D_VIEW_LEFT;
153
1/2
✓ Branch 0 taken 11 times.
✗ Branch 1 not taken.
11 else if (tdrdi->right_view_id[0] == view_id)
154 11 stereo_3d->view = AV_STEREO3D_VIEW_RIGHT;
155 else
156 stereo_3d->view = AV_STEREO3D_VIEW_UNSPEC;
157 }
158 }
159
160 10523 ret = ff_thread_get_buffer(s->avctx, frame->f, AV_GET_BUFFER_FLAG_REF);
161
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10523 times.
10523 if (ret < 0)
162 goto fail;
163
164 size_t rpl_bytes;
165
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 10523 times.
10523 if (av_size_mult(s->pkt.nb_nals, sizeof(*frame->rpl), &rpl_bytes) < 0)
166 goto fail;
167 10523 frame->rpl = av_refstruct_allocz(rpl_bytes);
168
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10523 times.
10523 if (!frame->rpl)
169 goto fail;
170 10523 frame->nb_rpl_elems = s->pkt.nb_nals;
171
172 10523 frame->tab_mvf = av_refstruct_pool_get(l->tab_mvf_pool);
173
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10523 times.
10523 if (!frame->tab_mvf)
174 goto fail;
175
176 10523 frame->rpl_tab = av_refstruct_pool_get(l->rpl_tab_pool);
177
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10523 times.
10523 if (!frame->rpl_tab)
178 goto fail;
179 10523 frame->ctb_count = l->sps->ctb_width * l->sps->ctb_height;
180
2/2
✓ Branch 0 taken 2079797 times.
✓ Branch 1 taken 10523 times.
2090320 for (j = 0; j < frame->ctb_count; j++)
181 2079797 frame->rpl_tab[j] = frame->rpl;
182
183
2/2
✓ Branch 0 taken 15 times.
✓ Branch 1 taken 10508 times.
10523 if (s->sei.picture_timing.picture_struct == AV_PICTURE_STRUCTURE_TOP_FIELD)
184 15 frame->f->flags |= AV_FRAME_FLAG_TOP_FIELD_FIRST;
185
2/2
✓ Branch 0 taken 10508 times.
✓ Branch 1 taken 15 times.
10523 if ((s->sei.picture_timing.picture_struct == AV_PICTURE_STRUCTURE_TOP_FIELD) ||
186
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 10506 times.
10508 (s->sei.picture_timing.picture_struct == AV_PICTURE_STRUCTURE_BOTTOM_FIELD))
187 17 frame->f->flags |= AV_FRAME_FLAG_INTERLACED;
188
189 10523 ret = ff_hwaccel_frame_priv_alloc(s->avctx, &frame->hwaccel_picture_private);
190
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10523 times.
10523 if (ret < 0)
191 goto fail;
192
193 10523 frame->pps = av_refstruct_ref_c(s->pps);
194
4/4
✓ Branch 0 taken 390 times.
✓ Branch 1 taken 10133 times.
✓ Branch 3 taken 50 times.
✓ Branch 4 taken 340 times.
10523 if (l != &s->layers[0] && ff_hevc_is_alpha_video(s)) {
195 50 AVFrame *alpha = frame->f;
196 50 AVFrame *base = s->layers[0].cur_frame->f;
197 50 ret = replace_alpha_plane(alpha, base);
198
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 50 times.
50 if (ret < 0)
199 goto fail;
200 }
201
202 10523 return frame;
203 fail:
204 ff_hevc_unref_frame(frame, ~0);
205 return NULL;
206 }
207 av_log(s->avctx, AV_LOG_ERROR, "Error allocating frame, DPB full.\n");
208 return NULL;
209 }
210
211 10461 int ff_hevc_set_new_ref(HEVCContext *s, HEVCLayerContext *l, int poc)
212 {
213 HEVCFrame *ref;
214 int i;
215 int no_output;
216
217 /* check that this POC doesn't already exist */
218
2/2
✓ Branch 0 taken 334752 times.
✓ Branch 1 taken 10461 times.
345213 for (i = 0; i < FF_ARRAY_ELEMS(l->DPB); i++) {
219 334752 HEVCFrame *frame = &l->DPB[i];
220
221
3/4
✓ Branch 0 taken 47830 times.
✓ Branch 1 taken 286922 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 47830 times.
334752 if (frame->f && frame->poc == poc) {
222 av_log(s->avctx, AV_LOG_ERROR, "Duplicate POC in a sequence: %d.\n",
223 poc);
224 return AVERROR_INVALIDDATA;
225 }
226 }
227
228 10461 ref = alloc_frame(s, l);
229
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10461 times.
10461 if (!ref)
230 return AVERROR(ENOMEM);
231
232 10461 s->cur_frame = ref;
233 10461 l->cur_frame = ref;
234 10461 s->collocated_ref = NULL;
235
236
3/4
✓ Branch 0 taken 390 times.
✓ Branch 1 taken 10071 times.
✓ Branch 2 taken 390 times.
✗ Branch 3 not taken.
10461 ref->base_layer_frame = (l != &s->layers[0] && s->layers[0].cur_frame) ?
237 390 s->layers[0].cur_frame - s->layers[0].DPB : -1;
238
239
3/4
✗ Branch 0 not taken.
✓ Branch 1 taken 747 times.
✓ Branch 2 taken 20 times.
✓ Branch 3 taken 9694 times.
10461 no_output = !IS_IRAP(s) && (s->poc < s->recovery_poc) &&
240
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
20 HEVC_IS_RECOVERING(s) &&
241
2/4
✓ Branch 0 taken 747 times.
✓ Branch 1 taken 9714 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
20922 !(s->avctx->flags & AV_CODEC_FLAG_OUTPUT_CORRUPT) &&
242 !(s->avctx->flags2 & AV_CODEC_FLAG2_SHOW_ALL);
243
3/4
✓ Branch 0 taken 10455 times.
✓ Branch 1 taken 6 times.
✓ Branch 2 taken 10455 times.
✗ Branch 3 not taken.
10461 if (s->sh.pic_output_flag && !no_output)
244 10455 ref->flags = HEVC_FRAME_FLAG_OUTPUT | HEVC_FRAME_FLAG_SHORT_REF;
245 else
246 6 ref->flags = HEVC_FRAME_FLAG_SHORT_REF;
247
248 10461 ref->poc = poc;
249 10461 ref->f->crop_left = l->sps->output_window.left_offset;
250 10461 ref->f->crop_right = l->sps->output_window.right_offset;
251 10461 ref->f->crop_top = l->sps->output_window.top_offset;
252 10461 ref->f->crop_bottom = l->sps->output_window.bottom_offset;
253
254 10461 return 0;
255 }
256
257 10461 static void unref_missing_refs(HEVCLayerContext *l)
258 {
259
2/2
✓ Branch 0 taken 334752 times.
✓ Branch 1 taken 10461 times.
345213 for (int i = 0; i < FF_ARRAY_ELEMS(l->DPB); i++) {
260 334752 HEVCFrame *frame = &l->DPB[i];
261
2/2
✓ Branch 0 taken 37 times.
✓ Branch 1 taken 334715 times.
334752 if (frame->flags & HEVC_FRAME_FLAG_UNAVAILABLE) {
262 37 ff_hevc_unref_frame(frame, ~0);
263 }
264 }
265 10461 }
266
267 11247 int ff_hevc_output_frames(HEVCContext *s,
268 unsigned layers_active_decode, unsigned layers_active_output,
269 unsigned max_output, unsigned max_dpb, int discard)
270 {
271 10312 while (1) {
272 21559 int nb_dpb[HEVC_VPS_MAX_LAYERS] = { 0 };
273 21559 int nb_output = 0;
274 21559 int min_poc = INT_MAX;
275 21559 int min_layer = -1;
276 21559 int min_idx, ret = 0;
277
278
2/2
✓ Branch 0 taken 43118 times.
✓ Branch 1 taken 21559 times.
64677 for (int layer = 0; layer < FF_ARRAY_ELEMS(s->layers); layer++) {
279 43118 HEVCLayerContext *l = &s->layers[layer];
280
281
2/2
✓ Branch 0 taken 20404 times.
✓ Branch 1 taken 22714 times.
43118 if (!(layers_active_decode & (1 << layer)))
282 20404 continue;
283
284
2/2
✓ Branch 0 taken 726848 times.
✓ Branch 1 taken 22714 times.
749562 for (int i = 0; i < FF_ARRAY_ELEMS(l->DPB); i++) {
285 726848 HEVCFrame *frame = &l->DPB[i];
286
2/2
✓ Branch 0 taken 57015 times.
✓ Branch 1 taken 669833 times.
726848 if (frame->flags & HEVC_FRAME_FLAG_OUTPUT) {
287 // nb_output counts AUs with an output-pending frame
288 // in at least one layer
289
2/2
✓ Branch 0 taken 2420 times.
✓ Branch 1 taken 54595 times.
57015 if (!(frame->base_layer_frame >= 0 &&
290
2/2
✓ Branch 0 taken 402 times.
✓ Branch 1 taken 2018 times.
2420 (s->layers[0].DPB[frame->base_layer_frame].flags & HEVC_FRAME_FLAG_OUTPUT)))
291 54997 nb_output++;
292
4/4
✓ Branch 0 taken 39171 times.
✓ Branch 1 taken 17844 times.
✓ Branch 2 taken 12887 times.
✓ Branch 3 taken 26284 times.
57015 if (min_layer < 0 || frame->poc < min_poc) {
293 30731 min_poc = frame->poc;
294 30731 min_idx = i;
295 30731 min_layer = layer;
296 }
297 }
298 726848 nb_dpb[layer] += !!frame->flags;
299 }
300 }
301
302
4/4
✓ Branch 0 taken 11696 times.
✓ Branch 1 taken 9863 times.
✓ Branch 2 taken 7981 times.
✓ Branch 3 taken 3715 times.
21559 if (nb_output > max_output ||
303 7981 (nb_output &&
304
4/4
✓ Branch 0 taken 7544 times.
✓ Branch 1 taken 437 times.
✓ Branch 2 taken 12 times.
✓ Branch 3 taken 7532 times.
7981 (nb_dpb[0] > max_dpb || nb_dpb[1] > max_dpb))) {
305 10312 HEVCFrame *frame = &s->layers[min_layer].DPB[min_idx];
306
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10312 times.
10312 AVFrame *f = frame->needs_fg ? frame->frame_grain : frame->f;
307
4/4
✓ Branch 0 taken 10283 times.
✓ Branch 1 taken 29 times.
✓ Branch 2 taken 10170 times.
✓ Branch 3 taken 113 times.
10312 int output = !discard && (layers_active_output & (1 << min_layer));
308
309
2/2
✓ Branch 0 taken 10170 times.
✓ Branch 1 taken 142 times.
10312 if (output) {
310
2/2
✓ Branch 0 taken 51 times.
✓ Branch 1 taken 10119 times.
10170 if (frame->flags & HEVC_FRAME_FLAG_CORRUPT)
311 51 f->flags |= AV_FRAME_FLAG_CORRUPT;
312 10170 f->pkt_dts = s->pkt_dts;
313 10170 ret = av_container_fifo_write(s->output_fifo, f, AV_CONTAINER_FIFO_FLAG_REF);
314 }
315 10312 ff_hevc_unref_frame(frame, HEVC_FRAME_FLAG_OUTPUT);
316
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10312 times.
10312 if (ret < 0)
317 11247 return ret;
318
319
2/2
✓ Branch 0 taken 10170 times.
✓ Branch 1 taken 142 times.
10312 av_log(s->avctx, AV_LOG_DEBUG, "%s frame with POC %d/%d.\n",
320 output ? "Output" : "Discarded", min_layer, frame->poc);
321 10312 continue;
322 }
323 11247 return 0;
324 }
325 }
326
327 19479 static int init_slice_rpl(HEVCContext *s)
328 {
329 19479 HEVCFrame *frame = s->cur_frame;
330 19479 int ctb_count = frame->ctb_count;
331 19479 int ctb_addr_ts = s->pps->ctb_addr_rs_to_ts[s->sh.slice_segment_addr];
332 int i;
333
334
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 19479 times.
19479 if (s->slice_idx >= frame->nb_rpl_elems)
335 return AVERROR_INVALIDDATA;
336
337
2/2
✓ Branch 0 taken 2838252 times.
✓ Branch 1 taken 19479 times.
2857731 for (i = ctb_addr_ts; i < ctb_count; i++)
338 2838252 frame->rpl_tab[i] = frame->rpl + s->slice_idx;
339
340 19479 frame->refPicList = (RefPicList *)frame->rpl_tab[ctb_addr_ts];
341
342 19479 return 0;
343 }
344
345 19479 int ff_hevc_slice_rpl(HEVCContext *s)
346 {
347 19479 SliceHeader *sh = &s->sh;
348
349
2/2
✓ Branch 0 taken 15172 times.
✓ Branch 1 taken 4307 times.
19479 uint8_t nb_list = sh->slice_type == HEVC_SLICE_B ? 2 : 1;
350 uint8_t list_idx;
351 int i, j, ret;
352
353 19479 ret = init_slice_rpl(s);
354
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 19479 times.
19479 if (ret < 0)
355 return ret;
356
357 19479 if (!(s->rps[ST_CURR_BEF].nb_refs + s->rps[ST_CURR_AFT].nb_refs +
358 19479 s->rps[LT_CURR].nb_refs +
359
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 19479 times.
19479 s->rps[INTER_LAYER0].nb_refs + s->rps[INTER_LAYER1].nb_refs) &&
360 !s->pps->pps_curr_pic_ref_enabled_flag) {
361 av_log(s->avctx, AV_LOG_ERROR, "Zero refs in the frame RPS.\n");
362 return AVERROR_INVALIDDATA;
363 }
364
365
2/2
✓ Branch 0 taken 34651 times.
✓ Branch 1 taken 19479 times.
54130 for (list_idx = 0; list_idx < nb_list; list_idx++) {
366 34651 RefPicList rpl_tmp = { { 0 } };
367 34651 RefPicList *rpl = &s->cur_frame->refPicList[list_idx];
368
369 /* The order of the elements is
370 * ST_CURR_BEF - INTER_LAYER0 - ST_CURR_AFT - LT_CURR - INTER_LAYER1 for the L0 and
371 * ST_CURR_AFT - INTER_LAYER1 - ST_CURR_BEF - LT_CURR - INTER_LAYER0 for the L1 */
372 103953 int cand_lists[] = { list_idx ? ST_CURR_AFT : ST_CURR_BEF,
373
2/2
✓ Branch 0 taken 15172 times.
✓ Branch 1 taken 19479 times.
34651 list_idx ? INTER_LAYER1 : INTER_LAYER0,
374 34651 list_idx ? ST_CURR_BEF : ST_CURR_AFT,
375 LT_CURR,
376
2/2
✓ Branch 0 taken 15172 times.
✓ Branch 1 taken 19479 times.
34651 list_idx ? INTER_LAYER0 : INTER_LAYER1
377 };
378
379 /* concatenate the candidate lists for the current frame */
380
2/2
✓ Branch 0 taken 34669 times.
✓ Branch 1 taken 34651 times.
69320 while (rpl_tmp.nb_refs < sh->nb_refs[list_idx]) {
381
2/2
✓ Branch 0 taken 173345 times.
✓ Branch 1 taken 34669 times.
208014 for (i = 0; i < FF_ARRAY_ELEMS(cand_lists); i++) {
382 173345 RefPicList *rps = &s->rps[cand_lists[i]];
383
3/4
✓ Branch 0 taken 124376 times.
✓ Branch 1 taken 173345 times.
✓ Branch 2 taken 124376 times.
✗ Branch 3 not taken.
297721 for (j = 0; j < rps->nb_refs && rpl_tmp.nb_refs < HEVC_MAX_REFS; j++) {
384 124376 rpl_tmp.list[rpl_tmp.nb_refs] = rps->list[j];
385 124376 rpl_tmp.ref[rpl_tmp.nb_refs] = rps->ref[j];
386 // multiview inter-layer refs are treated as long-term here,
387 // cf. G.8.1.3
388 371722 rpl_tmp.isLongTerm[rpl_tmp.nb_refs] = cand_lists[i] == LT_CURR ||
389
4/4
✓ Branch 0 taken 122970 times.
✓ Branch 1 taken 1406 times.
✓ Branch 2 taken 122392 times.
✓ Branch 3 taken 578 times.
246768 cand_lists[i] == INTER_LAYER0 ||
390
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 122392 times.
122392 cand_lists[i] == INTER_LAYER1;
391 124376 rpl_tmp.nb_refs++;
392 }
393 }
394 // Construct RefPicList0, RefPicList1 (8-8, 8-10)
395
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 34669 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
34669 if (s->pps->pps_curr_pic_ref_enabled_flag && rpl_tmp.nb_refs < HEVC_MAX_REFS) {
396 rpl_tmp.list[rpl_tmp.nb_refs] = s->cur_frame->poc;
397 rpl_tmp.ref[rpl_tmp.nb_refs] = s->cur_frame;
398 rpl_tmp.isLongTerm[rpl_tmp.nb_refs] = 1;
399 rpl_tmp.nb_refs++;
400 }
401 }
402
403 /* reorder the references if necessary */
404
2/2
✓ Branch 0 taken 2083 times.
✓ Branch 1 taken 32568 times.
34651 if (sh->rpl_modification_flag[list_idx]) {
405
2/2
✓ Branch 0 taken 4920 times.
✓ Branch 1 taken 2083 times.
7003 for (i = 0; i < sh->nb_refs[list_idx]; i++) {
406 4920 int idx = sh->list_entry_lx[list_idx][i];
407
408
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4920 times.
4920 if (idx >= rpl_tmp.nb_refs) {
409 av_log(s->avctx, AV_LOG_ERROR, "Invalid reference index.\n");
410 return AVERROR_INVALIDDATA;
411 }
412
413 4920 rpl->list[i] = rpl_tmp.list[idx];
414 4920 rpl->ref[i] = rpl_tmp.ref[idx];
415 4920 rpl->isLongTerm[i] = rpl_tmp.isLongTerm[idx];
416 4920 rpl->nb_refs++;
417 }
418 } else {
419 32568 memcpy(rpl, &rpl_tmp, sizeof(*rpl));
420 32568 rpl->nb_refs = FFMIN(rpl->nb_refs, sh->nb_refs[list_idx]);
421 }
422
423 // 8-9
424
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 34651 times.
34651 if (s->pps->pps_curr_pic_ref_enabled_flag &&
425 !sh->rpl_modification_flag[list_idx] &&
426 rpl_tmp.nb_refs > sh->nb_refs[L0]) {
427 rpl->list[sh->nb_refs[L0] - 1] = s->cur_frame->poc;
428 rpl->ref[sh->nb_refs[L0] - 1] = s->cur_frame;
429 }
430
431
2/2
✓ Branch 0 taken 19478 times.
✓ Branch 1 taken 15173 times.
34651 if (sh->collocated_list == list_idx &&
432
1/2
✓ Branch 0 taken 19478 times.
✗ Branch 1 not taken.
19478 sh->collocated_ref_idx < rpl->nb_refs)
433 19478 s->collocated_ref = rpl->ref[sh->collocated_ref_idx];
434 }
435
436 19479 return 0;
437 }
438
439 37590 static HEVCFrame *find_ref_idx(HEVCContext *s, HEVCLayerContext *l,
440 int poc, uint8_t use_msb)
441 {
442
2/2
✓ Branch 0 taken 1635 times.
✓ Branch 1 taken 35955 times.
37590 int mask = use_msb ? ~0 : (1 << l->sps->log2_max_poc_lsb) - 1;
443 int i;
444
445
2/2
✓ Branch 0 taken 153167 times.
✓ Branch 1 taken 62 times.
153229 for (i = 0; i < FF_ARRAY_ELEMS(l->DPB); i++) {
446 153167 HEVCFrame *ref = &l->DPB[i];
447
2/2
✓ Branch 0 taken 148238 times.
✓ Branch 1 taken 4929 times.
153167 if (ref->f) {
448
5/6
✓ Branch 0 taken 37528 times.
✓ Branch 1 taken 110710 times.
✓ Branch 2 taken 1634 times.
✓ Branch 3 taken 35894 times.
✓ Branch 4 taken 1634 times.
✗ Branch 5 not taken.
148238 if ((ref->poc & mask) == poc && (use_msb || ref->poc != s->poc))
449 37528 return ref;
450 }
451 }
452
453
7/8
✓ Branch 0 taken 23 times.
✓ Branch 1 taken 39 times.
✓ Branch 2 taken 22 times.
✓ Branch 3 taken 1 times.
✓ Branch 4 taken 22 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 20 times.
✓ Branch 7 taken 2 times.
62 if (s->nal_unit_type != HEVC_NAL_CRA_NUT && !IS_BLA(s))
454 20 av_log(s->avctx, AV_LOG_ERROR,
455 "Could not find ref with POC %d\n", poc);
456 62 return NULL;
457 }
458
459 361881 static void mark_ref(HEVCFrame *frame, int flag)
460 {
461 361881 frame->flags &= ~(HEVC_FRAME_FLAG_LONG_REF | HEVC_FRAME_FLAG_SHORT_REF);
462 361881 frame->flags |= flag;
463 361881 }
464
465 62 static HEVCFrame *generate_missing_ref(HEVCContext *s, HEVCLayerContext *l, int poc)
466 {
467 HEVCFrame *frame;
468
469 62 frame = alloc_frame(s, l);
470
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 62 times.
62 if (!frame)
471 return NULL;
472
473
1/2
✓ Branch 0 taken 62 times.
✗ Branch 1 not taken.
62 if (!s->avctx->hwaccel) {
474
1/2
✓ Branch 0 taken 62 times.
✗ Branch 1 not taken.
62 int nb_planes = l->sps->chroma_format_idc ? 3 : 1;
475
2/2
✓ Branch 0 taken 42 times.
✓ Branch 1 taken 20 times.
62 if (!l->sps->pixel_shift) {
476
2/2
✓ Branch 0 taken 126 times.
✓ Branch 1 taken 42 times.
168 for (int i = 0; i < nb_planes; i++)
477 126 memset(frame->f->data[i], 1 << (l->sps->bit_depth - 1),
478 126 frame->f->linesize[i] * AV_CEIL_RSHIFT(l->sps->height, l->sps->vshift[i]));
479 } else {
480
2/2
✓ Branch 0 taken 60 times.
✓ Branch 1 taken 20 times.
80 for (int i = 0; i < nb_planes; i++)
481
2/2
✓ Branch 0 taken 71424 times.
✓ Branch 1 taken 60 times.
71484 for (int y = 0; y < (l->sps->height >> l->sps->vshift[i]); y++) {
482 71424 uint8_t *dst = frame->f->data[i] + y * frame->f->linesize[i];
483 71424 AV_WN16(dst, 1 << (l->sps->bit_depth - 1));
484 71424 av_memcpy_backptr(dst + 2, 2, 2*(l->sps->width >> l->sps->hshift[i]) - 2);
485 }
486 }
487 }
488
489 62 frame->poc = poc;
490 62 frame->flags = HEVC_FRAME_FLAG_UNAVAILABLE;
491
492
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 62 times.
62 if (s->avctx->active_thread_type == FF_THREAD_FRAME)
493 ff_progress_frame_report(&frame->tf, INT_MAX);
494
495 62 return frame;
496 }
497
498 /* add a reference with the given poc to the list and mark it as used in DPB */
499 37590 static int add_candidate_ref(HEVCContext *s, HEVCLayerContext *l,
500 RefPicList *list,
501 int poc, int ref_flag, uint8_t use_msb)
502 {
503 37590 HEVCFrame *ref = find_ref_idx(s, l, poc, use_msb);
504
505
2/4
✓ Branch 0 taken 37590 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 37590 times.
37590 if (ref == s->cur_frame || list->nb_refs >= HEVC_MAX_REFS)
506 return AVERROR_INVALIDDATA;
507
508
3/4
✓ Branch 0 taken 729 times.
✓ Branch 1 taken 36861 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 729 times.
37590 if (!IS_IRAP(s)) {
509
4/4
✓ Branch 0 taken 36841 times.
✓ Branch 1 taken 20 times.
✓ Branch 2 taken 117 times.
✓ Branch 3 taken 36724 times.
36861 int ref_corrupt = !ref || ref->flags & (HEVC_FRAME_FLAG_CORRUPT |
510 HEVC_FRAME_FLAG_UNAVAILABLE);
511
3/4
✓ Branch 0 taken 36838 times.
✓ Branch 1 taken 23 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 36838 times.
36861 int recovering = HEVC_IS_RECOVERING(s);
512
513
3/4
✓ Branch 0 taken 137 times.
✓ Branch 1 taken 36724 times.
✓ Branch 2 taken 137 times.
✗ Branch 3 not taken.
36861 if (ref_corrupt && !recovering) {
514
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 137 times.
137 if (!(s->avctx->flags & AV_CODEC_FLAG_OUTPUT_CORRUPT) &&
515 !(s->avctx->flags2 & AV_CODEC_FLAG2_SHOW_ALL))
516 return AVERROR_INVALIDDATA;
517
518 137 s->cur_frame->flags |= HEVC_FRAME_FLAG_CORRUPT;
519 }
520 }
521
522
2/2
✓ Branch 0 taken 62 times.
✓ Branch 1 taken 37528 times.
37590 if (!ref) {
523 62 ref = generate_missing_ref(s, l, poc);
524
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 62 times.
62 if (!ref)
525 return AVERROR(ENOMEM);
526 }
527
528 37590 list->list[list->nb_refs] = ref->poc;
529 37590 list->ref[list->nb_refs] = ref;
530 37590 list->nb_refs++;
531
532 37590 mark_ref(ref, ref_flag);
533 37590 return 0;
534 }
535
536 10461 int ff_hevc_frame_rps(HEVCContext *s, HEVCLayerContext *l)
537 {
538 10461 const ShortTermRPS *short_rps = s->sh.short_term_rps;
539 10461 const LongTermRPS *long_rps = &s->sh.long_term_rps;
540 10461 RefPicList *rps = s->rps;
541 10461 int i, ret = 0;
542
543 10461 unref_missing_refs(l);
544
545 /* clear the reference flags on all frames except the current one */
546
2/2
✓ Branch 0 taken 334752 times.
✓ Branch 1 taken 10461 times.
345213 for (i = 0; i < FF_ARRAY_ELEMS(l->DPB); i++) {
547 334752 HEVCFrame *frame = &l->DPB[i];
548
549
2/2
✓ Branch 0 taken 10461 times.
✓ Branch 1 taken 324291 times.
334752 if (frame == s->cur_frame)
550 10461 continue;
551
552 324291 mark_ref(frame, 0);
553 }
554
555
2/2
✓ Branch 0 taken 73227 times.
✓ Branch 1 taken 10461 times.
83688 for (i = 0; i < NB_RPS_TYPE; i++)
556 73227 rps[i].nb_refs = 0;
557
558
2/2
✓ Branch 0 taken 444 times.
✓ Branch 1 taken 10017 times.
10461 if (!short_rps)
559 444 goto inter_layer;
560
561 /* add the short refs */
562
2/2
✓ Branch 0 taken 35612 times.
✓ Branch 1 taken 10017 times.
45629 for (i = 0; i < short_rps->num_delta_pocs; i++) {
563 35612 int poc = s->poc + short_rps->delta_poc[i];
564 int list;
565
566
2/2
✓ Branch 0 taken 1770 times.
✓ Branch 1 taken 33842 times.
35612 if (!(short_rps->used & (1 << i)))
567 1770 list = ST_FOLL;
568
2/2
✓ Branch 0 taken 24540 times.
✓ Branch 1 taken 9302 times.
33842 else if (i < short_rps->num_negative_pics)
569 24540 list = ST_CURR_BEF;
570 else
571 9302 list = ST_CURR_AFT;
572
573 35612 ret = add_candidate_ref(s, l, &rps[list], poc,
574 HEVC_FRAME_FLAG_SHORT_REF, 1);
575
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 35612 times.
35612 if (ret < 0)
576 goto fail;
577 }
578
579 /* add the long refs */
580
2/2
✓ Branch 0 taken 1683 times.
✓ Branch 1 taken 10017 times.
11700 for (i = 0; i < long_rps->nb_refs; i++) {
581 1683 int poc = long_rps->poc[i];
582
2/2
✓ Branch 0 taken 696 times.
✓ Branch 1 taken 987 times.
1683 int list = long_rps->used[i] ? LT_CURR : LT_FOLL;
583
584 1683 ret = add_candidate_ref(s, l, &rps[list], poc,
585 1683 HEVC_FRAME_FLAG_LONG_REF, long_rps->poc_msb_present[i]);
586
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1683 times.
1683 if (ret < 0)
587 goto fail;
588 }
589
590 10017 inter_layer:
591 /* add inter-layer refs */
592
2/2
✓ Branch 0 taken 10166 times.
✓ Branch 1 taken 295 times.
10461 if (s->sh.inter_layer_pred) {
593 295 HEVCLayerContext *l0 = &s->layers[0];
594
595
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 295 times.
295 av_assert0(l != l0);
596
597 /* Given the assumption of at most two layers, refPicSet0Flag is
598 * always 1, so only RefPicSetInterLayer0 can ever contain a frame. */
599
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 295 times.
295 if (l0->cur_frame) {
600 // inter-layer refs are treated as short-term here, cf. F.8.1.6
601 295 ret = add_candidate_ref(s, l0, &rps[INTER_LAYER0], l0->cur_frame->poc,
602 HEVC_FRAME_FLAG_SHORT_REF, 1);
603
1/2
✓ Branch 0 taken 295 times.
✗ Branch 1 not taken.
295 if (ret < 0)
604 goto fail;
605 }
606 }
607
608 10461 fail:
609 /* release any frames that are now unused */
610
2/2
✓ Branch 0 taken 334752 times.
✓ Branch 1 taken 10461 times.
345213 for (i = 0; i < FF_ARRAY_ELEMS(l->DPB); i++)
611 334752 ff_hevc_unref_frame(&l->DPB[i], 0);
612
613 10461 return ret;
614 }
615
616 19565 int ff_hevc_frame_nb_refs(const SliceHeader *sh, const HEVCPPS *pps,
617 unsigned layer_idx)
618 {
619 19565 int ret = 0;
620 int i;
621 19565 const ShortTermRPS *rps = sh->short_term_rps;
622 19565 const LongTermRPS *long_rps = &sh->long_term_rps;
623
624
2/2
✓ Branch 0 taken 19552 times.
✓ Branch 1 taken 13 times.
19565 if (rps) {
625
2/2
✓ Branch 0 taken 46770 times.
✓ Branch 1 taken 19552 times.
66322 for (i = 0; i < rps->num_negative_pics; i++)
626 46770 ret += !!(rps->used & (1 << i));
627
2/2
✓ Branch 0 taken 21104 times.
✓ Branch 1 taken 19552 times.
40656 for (; i < rps->num_delta_pocs; i++)
628 21104 ret += !!(rps->used & (1 << i));
629 }
630
631
2/2
✓ Branch 0 taken 1787 times.
✓ Branch 1 taken 19565 times.
21352 for (i = 0; i < long_rps->nb_refs; i++)
632 1787 ret += !!long_rps->used[i];
633
634
2/2
✓ Branch 0 taken 421 times.
✓ Branch 1 taken 19144 times.
19565 if (sh->inter_layer_pred) {
635
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 421 times.
421 av_assert0(pps->sps->vps->num_direct_ref_layers[layer_idx] < 2);
636 421 ret++;
637 }
638
639
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 19565 times.
19565 if (pps->pps_curr_pic_ref_enabled_flag)
640 ret++;
641
642 19565 return ret;
643 }
644