FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/hevc/refs.c
Date: 2026-07-22 00:55:30
Exec Total Coverage
Lines: 318 371 85.7%
Functions: 16 17 94.1%
Branches: 234 310 75.5%

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 416373 void ff_hevc_unref_frame(HEVCFrame *frame, int flags)
36 {
37 416373 frame->flags &= ~flags;
38
2/2
✓ Branch 0 taken 356696 times.
✓ Branch 1 taken 59677 times.
416373 if (!(frame->flags & ~HEVC_FRAME_FLAG_CORRUPT))
39 356696 frame->flags = 0;
40
2/2
✓ Branch 0 taken 356696 times.
✓ Branch 1 taken 59677 times.
416373 if (!frame->flags) {
41 356696 av_refstruct_unref(&frame->hwaccel_picture_private);
42
43 356696 ff_progress_frame_unref(&frame->tf);
44 356696 av_frame_unref(frame->frame_grain);
45 356696 frame->needs_fg = 0;
46
47 356696 av_refstruct_unref(&frame->pps);
48 356696 av_refstruct_unref(&frame->tab_mvf);
49
50 356696 av_refstruct_unref(&frame->rpl);
51 356696 frame->nb_rpl_elems = 0;
52 356696 av_refstruct_unref(&frame->rpl_tab);
53 356696 frame->refPicList = NULL;
54 }
55 416373 }
56
57 5153058 const RefPicList *ff_hevc_get_ref_list(const HEVCFrame *ref, int x0, int y0)
58 {
59 5153058 const HEVCSPS *sps = ref->pps->sps;
60 5153058 int x_cb = x0 >> sps->log2_ctb_size;
61 5153058 int y_cb = y0 >> sps->log2_ctb_size;
62 5153058 int pic_width_cb = sps->ctb_width;
63 5153058 int ctb_addr_ts = ref->pps->ctb_addr_rs_to_ts[y_cb * pic_width_cb + x_cb];
64 5153058 return &ref->rpl_tab[ctb_addr_ts]->refPicList[0];
65 }
66
67 917 void ff_hevc_clear_refs(HEVCLayerContext *l)
68 {
69 int i;
70
2/2
✓ Branch 0 taken 29344 times.
✓ Branch 1 taken 917 times.
30261 for (i = 0; i < FF_ARRAY_ELEMS(l->DPB); i++)
71 29344 ff_hevc_unref_frame(&l->DPB[i],
72 HEVC_FRAME_FLAG_SHORT_REF |
73 HEVC_FRAME_FLAG_LONG_REF);
74 917 }
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 10628 static HEVCFrame *alloc_frame(HEVCContext *s, HEVCLayerContext *l)
111 {
112 10628 const HEVCVPS *vps = l->sps->vps;
113 10628 const int view_id = vps->view_id[s->cur_layer];
114 int i, j, ret;
115
1/2
✓ Branch 0 taken 37042 times.
✗ Branch 1 not taken.
37042 for (i = 0; i < FF_ARRAY_ELEMS(l->DPB); i++) {
116 37042 HEVCFrame *frame = &l->DPB[i];
117
2/2
✓ Branch 0 taken 26414 times.
✓ Branch 1 taken 10628 times.
37042 if (frame->f)
118 26414 continue;
119
120 10628 ret = ff_progress_frame_alloc(s->avctx, &frame->tf);
121
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10628 times.
10628 if (ret < 0)
122 10628 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 10596 times.
10628 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 10528 times.
✓ Branch 2 taken 100 times.
✓ Branch 3 taken 9843 times.
✓ Branch 4 taken 685 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 9843 times.
10628 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 10628 ret = ff_thread_get_buffer(s->avctx, frame->f, AV_GET_BUFFER_FLAG_REF);
161
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10628 times.
10628 if (ret < 0)
162 goto fail;
163
164 size_t rpl_bytes;
165
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 10628 times.
10628 if (av_size_mult(s->pkt.nb_nals, sizeof(*frame->rpl), &rpl_bytes) < 0)
166 goto fail;
167 10628 frame->rpl = av_refstruct_allocz(rpl_bytes);
168
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10628 times.
10628 if (!frame->rpl)
169 goto fail;
170 10628 frame->nb_rpl_elems = s->pkt.nb_nals;
171
172 10628 frame->tab_mvf = av_refstruct_pool_get(l->tab_mvf_pool);
173
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10628 times.
10628 if (!frame->tab_mvf)
174 goto fail;
175
176 10628 frame->rpl_tab = av_refstruct_pool_get(l->rpl_tab_pool);
177
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10628 times.
10628 if (!frame->rpl_tab)
178 goto fail;
179 10628 frame->ctb_count = l->sps->ctb_width * l->sps->ctb_height;
180
2/2
✓ Branch 0 taken 2084325 times.
✓ Branch 1 taken 10628 times.
2094953 for (j = 0; j < frame->ctb_count; j++)
181 2084325 frame->rpl_tab[j] = frame->rpl;
182
183
2/2
✓ Branch 0 taken 15 times.
✓ Branch 1 taken 10613 times.
10628 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 10613 times.
✓ Branch 1 taken 15 times.
10628 if ((s->sei.picture_timing.picture_struct == AV_PICTURE_STRUCTURE_TOP_FIELD) ||
186
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 10611 times.
10613 (s->sei.picture_timing.picture_struct == AV_PICTURE_STRUCTURE_BOTTOM_FIELD))
187 17 frame->f->flags |= AV_FRAME_FLAG_INTERLACED;
188
189 10628 ret = ff_hwaccel_frame_priv_alloc(s->avctx, &frame->hwaccel_picture_private);
190
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10628 times.
10628 if (ret < 0)
191 goto fail;
192
193 10628 frame->pps = av_refstruct_ref_c(s->pps);
194
4/4
✓ Branch 0 taken 390 times.
✓ Branch 1 taken 10238 times.
✓ Branch 3 taken 50 times.
✓ Branch 4 taken 340 times.
10628 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 10628 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 10566 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 338112 times.
✓ Branch 1 taken 10566 times.
348678 for (i = 0; i < FF_ARRAY_ELEMS(l->DPB); i++) {
219 338112 HEVCFrame *frame = &l->DPB[i];
220
221
3/4
✓ Branch 0 taken 48142 times.
✓ Branch 1 taken 289970 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 48142 times.
338112 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 10566 ref = alloc_frame(s, l);
229
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10566 times.
10566 if (!ref)
230 return AVERROR(ENOMEM);
231
232 10566 s->cur_frame = ref;
233 10566 l->cur_frame = ref;
234 10566 s->collocated_ref = NULL;
235
236
3/4
✓ Branch 0 taken 390 times.
✓ Branch 1 taken 10176 times.
✓ Branch 2 taken 390 times.
✗ Branch 3 not taken.
10566 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 760 times.
✓ Branch 2 taken 20 times.
✓ Branch 3 taken 9786 times.
10566 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 760 times.
✓ Branch 1 taken 9806 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
21132 !(s->avctx->flags & AV_CODEC_FLAG_OUTPUT_CORRUPT) &&
242 !(s->avctx->flags2 & AV_CODEC_FLAG2_SHOW_ALL);
243
3/4
✓ Branch 0 taken 10560 times.
✓ Branch 1 taken 6 times.
✓ Branch 2 taken 10560 times.
✗ Branch 3 not taken.
10566 if (s->sh.pic_output_flag && !no_output)
244 10560 ref->flags = HEVC_FRAME_FLAG_OUTPUT | HEVC_FRAME_FLAG_SHORT_REF;
245 else
246 6 ref->flags = HEVC_FRAME_FLAG_SHORT_REF;
247
248 10566 ref->poc = poc;
249 10566 ref->f->crop_left = l->sps->output_window.left_offset;
250 10566 ref->f->crop_right = l->sps->output_window.right_offset;
251 10566 ref->f->crop_top = l->sps->output_window.top_offset;
252 10566 ref->f->crop_bottom = l->sps->output_window.bottom_offset;
253
254 10566 return 0;
255 }
256
257 10566 static void unref_missing_refs(HEVCLayerContext *l)
258 {
259
2/2
✓ Branch 0 taken 338112 times.
✓ Branch 1 taken 10566 times.
348678 for (int i = 0; i < FF_ARRAY_ELEMS(l->DPB); i++) {
260 338112 HEVCFrame *frame = &l->DPB[i];
261
2/2
✓ Branch 0 taken 37 times.
✓ Branch 1 taken 338075 times.
338112 if (frame->flags & HEVC_FRAME_FLAG_UNAVAILABLE) {
262 37 ff_hevc_unref_frame(frame, ~0);
263 }
264 }
265 10566 }
266
267 11387 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 10416 while (1) {
272 21803 int nb_dpb[HEVC_VPS_MAX_LAYERS] = { 0 };
273 21803 int nb_output = 0;
274 21803 int min_poc = INT_MAX;
275 21803 int min_layer = -1;
276 21803 int min_idx, ret = 0;
277
278
2/2
✓ Branch 0 taken 43606 times.
✓ Branch 1 taken 21803 times.
65409 for (int layer = 0; layer < FF_ARRAY_ELEMS(s->layers); layer++) {
279 43606 HEVCLayerContext *l = &s->layers[layer];
280
281
2/2
✓ Branch 0 taken 20660 times.
✓ Branch 1 taken 22946 times.
43606 if (!(layers_active_decode & (1 << layer)))
282 20660 continue;
283
284
2/2
✓ Branch 0 taken 734272 times.
✓ Branch 1 taken 22946 times.
757218 for (int i = 0; i < FF_ARRAY_ELEMS(l->DPB); i++) {
285 734272 HEVCFrame *frame = &l->DPB[i];
286
2/2
✓ Branch 0 taken 57488 times.
✓ Branch 1 taken 676784 times.
734272 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 55068 times.
57488 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 55470 nb_output++;
292
4/4
✓ Branch 0 taken 39435 times.
✓ Branch 1 taken 18053 times.
✓ Branch 2 taken 13057 times.
✓ Branch 3 taken 26378 times.
57488 if (min_layer < 0 || frame->poc < min_poc) {
293 31110 min_poc = frame->poc;
294 31110 min_idx = i;
295 31110 min_layer = layer;
296 }
297 }
298 734272 nb_dpb[layer] += !!frame->flags;
299 }
300 }
301
302
4/4
✓ Branch 0 taken 11836 times.
✓ Branch 1 taken 9967 times.
✓ Branch 2 taken 8086 times.
✓ Branch 3 taken 3750 times.
21803 if (nb_output > max_output ||
303 8086 (nb_output &&
304
4/4
✓ Branch 0 taken 7649 times.
✓ Branch 1 taken 437 times.
✓ Branch 2 taken 12 times.
✓ Branch 3 taken 7637 times.
8086 (nb_dpb[0] > max_dpb || nb_dpb[1] > max_dpb))) {
305 10416 HEVCFrame *frame = &s->layers[min_layer].DPB[min_idx];
306
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10416 times.
10416 AVFrame *f = frame->needs_fg ? frame->frame_grain : frame->f;
307
4/4
✓ Branch 0 taken 10387 times.
✓ Branch 1 taken 29 times.
✓ Branch 2 taken 10274 times.
✓ Branch 3 taken 113 times.
10416 int output = !discard && (layers_active_output & (1 << min_layer));
308
309
2/2
✓ Branch 0 taken 10274 times.
✓ Branch 1 taken 142 times.
10416 if (output) {
310
2/2
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 10260 times.
10274 if (frame->flags & HEVC_FRAME_FLAG_CORRUPT)
311 14 f->flags |= AV_FRAME_FLAG_CORRUPT;
312 10274 f->pkt_dts = s->pkt_dts;
313 10274 ret = av_container_fifo_write(s->output_fifo, f, AV_CONTAINER_FIFO_FLAG_REF);
314 }
315 10416 ff_hevc_unref_frame(frame, HEVC_FRAME_FLAG_OUTPUT);
316
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10416 times.
10416 if (ret < 0)
317 11387 return ret;
318
319
2/2
✓ Branch 0 taken 10274 times.
✓ Branch 1 taken 142 times.
10416 av_log(s->avctx, AV_LOG_DEBUG, "%s frame with POC %d/%d.\n",
320 output ? "Output" : "Discarded", min_layer, frame->poc);
321 10416 continue;
322 }
323 11387 return 0;
324 }
325 }
326
327 19571 static int init_slice_rpl(HEVCContext *s)
328 {
329 19571 HEVCFrame *frame = s->cur_frame;
330 19571 int ctb_count = frame->ctb_count;
331 19571 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 19571 times.
19571 if (s->slice_idx >= frame->nb_rpl_elems)
335 return AVERROR_INVALIDDATA;
336
337
2/2
✓ Branch 0 taken 2842314 times.
✓ Branch 1 taken 19571 times.
2861885 for (i = ctb_addr_ts; i < ctb_count; i++)
338 2842314 frame->rpl_tab[i] = frame->rpl + s->slice_idx;
339
340 19571 frame->refPicList = (RefPicList *)frame->rpl_tab[ctb_addr_ts];
341
342 19571 return 0;
343 }
344
345 19571 int ff_hevc_slice_rpl(HEVCContext *s)
346 {
347 19571 SliceHeader *sh = &s->sh;
348
349
2/2
✓ Branch 0 taken 15242 times.
✓ Branch 1 taken 4329 times.
19571 uint8_t nb_list = sh->slice_type == HEVC_SLICE_B ? 2 : 1;
350 uint8_t list_idx;
351 int i, j, ret;
352
353 19571 ret = init_slice_rpl(s);
354
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 19571 times.
19571 if (ret < 0)
355 return ret;
356
357 19571 if (!(s->rps[ST_CURR_BEF].nb_refs + s->rps[ST_CURR_AFT].nb_refs +
358 19571 s->rps[LT_CURR].nb_refs +
359
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 19571 times.
19571 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 34813 times.
✓ Branch 1 taken 19571 times.
54384 for (list_idx = 0; list_idx < nb_list; list_idx++) {
366 34813 RefPicList rpl_tmp = { { 0 } };
367 34813 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 104439 int cand_lists[] = { list_idx ? ST_CURR_AFT : ST_CURR_BEF,
373
2/2
✓ Branch 0 taken 15242 times.
✓ Branch 1 taken 19571 times.
34813 list_idx ? INTER_LAYER1 : INTER_LAYER0,
374 34813 list_idx ? ST_CURR_BEF : ST_CURR_AFT,
375 LT_CURR,
376
2/2
✓ Branch 0 taken 15242 times.
✓ Branch 1 taken 19571 times.
34813 list_idx ? INTER_LAYER0 : INTER_LAYER1
377 };
378
379 /* concatenate the candidate lists for the current frame */
380
2/2
✓ Branch 0 taken 34831 times.
✓ Branch 1 taken 34813 times.
69644 while (rpl_tmp.nb_refs < sh->nb_refs[list_idx]) {
381
2/2
✓ Branch 0 taken 174155 times.
✓ Branch 1 taken 34831 times.
208986 for (i = 0; i < FF_ARRAY_ELEMS(cand_lists); i++) {
382 174155 RefPicList *rps = &s->rps[cand_lists[i]];
383
3/4
✓ Branch 0 taken 124858 times.
✓ Branch 1 taken 174155 times.
✓ Branch 2 taken 124858 times.
✗ Branch 3 not taken.
299013 for (j = 0; j < rps->nb_refs && rpl_tmp.nb_refs < HEVC_MAX_REFS; j++) {
384 124858 rpl_tmp.list[rpl_tmp.nb_refs] = rps->list[j];
385 124858 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 373168 rpl_tmp.isLongTerm[rpl_tmp.nb_refs] = cand_lists[i] == LT_CURR ||
389
4/4
✓ Branch 0 taken 123452 times.
✓ Branch 1 taken 1406 times.
✓ Branch 2 taken 122874 times.
✓ Branch 3 taken 578 times.
247732 cand_lists[i] == INTER_LAYER0 ||
390
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 122874 times.
122874 cand_lists[i] == INTER_LAYER1;
391 124858 rpl_tmp.nb_refs++;
392 }
393 }
394 // Construct RefPicList0, RefPicList1 (8-8, 8-10)
395
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 34831 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
34831 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 32730 times.
34813 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 32730 memcpy(rpl, &rpl_tmp, sizeof(*rpl));
420 32730 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 34813 times.
34813 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 19570 times.
✓ Branch 1 taken 15243 times.
34813 if (sh->collocated_list == list_idx &&
432
1/2
✓ Branch 0 taken 19570 times.
✗ Branch 1 not taken.
19570 sh->collocated_ref_idx < rpl->nb_refs)
433 19570 s->collocated_ref = rpl->ref[sh->collocated_ref_idx];
434 }
435
436 19571 return 0;
437 }
438
439 37852 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 36217 times.
37852 int mask = use_msb ? ~0 : (1 << l->sps->log2_max_poc_lsb) - 1;
443 int i;
444
445
2/2
✓ Branch 0 taken 153749 times.
✓ Branch 1 taken 62 times.
153811 for (i = 0; i < FF_ARRAY_ELEMS(l->DPB); i++) {
446 153749 HEVCFrame *ref = &l->DPB[i];
447
2/2
✓ Branch 0 taken 148820 times.
✓ Branch 1 taken 4929 times.
153749 if (ref->f) {
448
5/6
✓ Branch 0 taken 37790 times.
✓ Branch 1 taken 111030 times.
✓ Branch 2 taken 1634 times.
✓ Branch 3 taken 36156 times.
✓ Branch 4 taken 1634 times.
✗ Branch 5 not taken.
148820 if ((ref->poc & mask) == poc && (use_msb || ref->poc != s->poc))
449 37790 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 365398 static void mark_ref(HEVCFrame *frame, int flag)
460 {
461 365398 frame->flags &= ~(HEVC_FRAME_FLAG_LONG_REF | HEVC_FRAME_FLAG_SHORT_REF);
462 365398 frame->flags |= flag;
463 365398 }
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 37852 static int add_candidate_ref(HEVCContext *s, HEVCLayerContext *l,
500 RefPicList *rps, int list_idx,
501 int poc, int ref_flag, uint8_t use_msb)
502 {
503 37852 RefPicList *list = &rps[list_idx];
504 37852 HEVCFrame *ref = find_ref_idx(s, l, poc, use_msb);
505
506
2/4
✓ Branch 0 taken 37852 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 37852 times.
37852 if (ref == s->cur_frame || list->nb_refs >= HEVC_MAX_REFS)
507 return AVERROR_INVALIDDATA;
508
509
7/8
✓ Branch 0 taken 729 times.
✓ Branch 1 taken 37123 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 729 times.
✓ Branch 4 taken 35864 times.
✓ Branch 5 taken 1259 times.
✓ Branch 6 taken 35018 times.
✓ Branch 7 taken 846 times.
37852 if (!IS_IRAP(s) && list_idx != ST_FOLL && list_idx != LT_FOLL) {
510
4/4
✓ Branch 0 taken 35011 times.
✓ Branch 1 taken 7 times.
✓ Branch 2 taken 17 times.
✓ Branch 3 taken 34994 times.
35018 int ref_corrupt = !ref || ref->flags & (HEVC_FRAME_FLAG_CORRUPT |
511 HEVC_FRAME_FLAG_UNAVAILABLE);
512
3/4
✓ Branch 0 taken 34998 times.
✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 34998 times.
35018 int recovering = HEVC_IS_RECOVERING(s);
513
514
3/4
✓ Branch 0 taken 24 times.
✓ Branch 1 taken 34994 times.
✓ Branch 2 taken 24 times.
✗ Branch 3 not taken.
35018 if (ref_corrupt && !recovering) {
515
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 24 times.
24 if (!(s->avctx->flags & AV_CODEC_FLAG_OUTPUT_CORRUPT) &&
516 !(s->avctx->flags2 & AV_CODEC_FLAG2_SHOW_ALL))
517 return AVERROR_INVALIDDATA;
518
519 24 s->cur_frame->flags |= HEVC_FRAME_FLAG_CORRUPT;
520 }
521 }
522
523
2/2
✓ Branch 0 taken 62 times.
✓ Branch 1 taken 37790 times.
37852 if (!ref) {
524 62 ref = generate_missing_ref(s, l, poc);
525
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 62 times.
62 if (!ref)
526 return AVERROR(ENOMEM);
527 }
528
529 37852 list->list[list->nb_refs] = ref->poc;
530 37852 list->ref[list->nb_refs] = ref;
531 37852 list->nb_refs++;
532
533 37852 mark_ref(ref, ref_flag);
534 37852 return 0;
535 }
536
537 10566 int ff_hevc_frame_rps(HEVCContext *s, HEVCLayerContext *l)
538 {
539 10566 const ShortTermRPS *short_rps = s->sh.short_term_rps;
540 10566 const LongTermRPS *long_rps = &s->sh.long_term_rps;
541 10566 RefPicList *rps = s->rps;
542 10566 int i, ret = 0;
543
544 10566 unref_missing_refs(l);
545
546 /* clear the reference flags on all frames except the current one */
547
2/2
✓ Branch 0 taken 338112 times.
✓ Branch 1 taken 10566 times.
348678 for (i = 0; i < FF_ARRAY_ELEMS(l->DPB); i++) {
548 338112 HEVCFrame *frame = &l->DPB[i];
549
550
2/2
✓ Branch 0 taken 10566 times.
✓ Branch 1 taken 327546 times.
338112 if (frame == s->cur_frame)
551 10566 continue;
552
553 327546 mark_ref(frame, 0);
554 }
555
556
2/2
✓ Branch 0 taken 73962 times.
✓ Branch 1 taken 10566 times.
84528 for (i = 0; i < NB_RPS_TYPE; i++)
557 73962 rps[i].nb_refs = 0;
558
559
2/2
✓ Branch 0 taken 457 times.
✓ Branch 1 taken 10109 times.
10566 if (!short_rps)
560 457 goto inter_layer;
561
562 /* add the short refs */
563
2/2
✓ Branch 0 taken 35874 times.
✓ Branch 1 taken 10109 times.
45983 for (i = 0; i < short_rps->num_delta_pocs; i++) {
564 35874 int poc = s->poc + short_rps->delta_poc[i];
565 int list;
566
567
2/2
✓ Branch 0 taken 1770 times.
✓ Branch 1 taken 34104 times.
35874 if (!(short_rps->used & (1 << i)))
568 1770 list = ST_FOLL;
569
2/2
✓ Branch 0 taken 24702 times.
✓ Branch 1 taken 9402 times.
34104 else if (i < short_rps->num_negative_pics)
570 24702 list = ST_CURR_BEF;
571 else
572 9402 list = ST_CURR_AFT;
573
574 35874 ret = add_candidate_ref(s, l, rps, list, poc,
575 HEVC_FRAME_FLAG_SHORT_REF, 1);
576
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 35874 times.
35874 if (ret < 0)
577 goto fail;
578 }
579
580 /* add the long refs */
581
2/2
✓ Branch 0 taken 1683 times.
✓ Branch 1 taken 10109 times.
11792 for (i = 0; i < long_rps->nb_refs; i++) {
582 1683 int poc = long_rps->poc[i];
583
2/2
✓ Branch 0 taken 696 times.
✓ Branch 1 taken 987 times.
1683 int list = long_rps->used[i] ? LT_CURR : LT_FOLL;
584
585 1683 ret = add_candidate_ref(s, l, rps, list, poc,
586 1683 HEVC_FRAME_FLAG_LONG_REF, long_rps->poc_msb_present[i]);
587
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1683 times.
1683 if (ret < 0)
588 goto fail;
589 }
590
591 10109 inter_layer:
592 /* add inter-layer refs */
593
2/2
✓ Branch 0 taken 10271 times.
✓ Branch 1 taken 295 times.
10566 if (s->sh.inter_layer_pred) {
594 295 HEVCLayerContext *l0 = &s->layers[0];
595
596
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 295 times.
295 av_assert0(l != l0);
597
598 /* Given the assumption of at most two layers, refPicSet0Flag is
599 * always 1, so only RefPicSetInterLayer0 can ever contain a frame. */
600
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 295 times.
295 if (l0->cur_frame) {
601 // inter-layer refs are treated as short-term here, cf. F.8.1.6
602 295 ret = add_candidate_ref(s, l0, rps, INTER_LAYER0, l0->cur_frame->poc,
603 HEVC_FRAME_FLAG_SHORT_REF, 1);
604
1/2
✓ Branch 0 taken 295 times.
✗ Branch 1 not taken.
295 if (ret < 0)
605 goto fail;
606 }
607 }
608
609 10566 fail:
610 /* release any frames that are now unused */
611
2/2
✓ Branch 0 taken 338112 times.
✓ Branch 1 taken 10566 times.
348678 for (i = 0; i < FF_ARRAY_ELEMS(l->DPB); i++)
612 338112 ff_hevc_unref_frame(&l->DPB[i], 0);
613
614 10566 return ret;
615 }
616
617 19657 int ff_hevc_frame_nb_refs(const SliceHeader *sh, const HEVCPPS *pps,
618 unsigned layer_idx)
619 {
620 19657 int ret = 0;
621 int i;
622 19657 const ShortTermRPS *rps = sh->short_term_rps;
623 19657 const LongTermRPS *long_rps = &sh->long_term_rps;
624
625
2/2
✓ Branch 0 taken 19644 times.
✓ Branch 1 taken 13 times.
19657 if (rps) {
626
2/2
✓ Branch 0 taken 46932 times.
✓ Branch 1 taken 19644 times.
66576 for (i = 0; i < rps->num_negative_pics; i++)
627 46932 ret += !!(rps->used & (1 << i));
628
2/2
✓ Branch 0 taken 21204 times.
✓ Branch 1 taken 19644 times.
40848 for (; i < rps->num_delta_pocs; i++)
629 21204 ret += !!(rps->used & (1 << i));
630 }
631
632
2/2
✓ Branch 0 taken 1787 times.
✓ Branch 1 taken 19657 times.
21444 for (i = 0; i < long_rps->nb_refs; i++)
633 1787 ret += !!long_rps->used[i];
634
635
2/2
✓ Branch 0 taken 421 times.
✓ Branch 1 taken 19236 times.
19657 if (sh->inter_layer_pred) {
636
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 421 times.
421 av_assert0(pps->sps->vps->num_direct_ref_layers[layer_idx] < 2);
637 421 ret++;
638 }
639
640
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 19657 times.
19657 if (pps->pps_curr_pic_ref_enabled_flag)
641 ret++;
642
643 19657 return ret;
644 }
645