FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/hevc/refs.c
Date: 2026-05-01 13:04:54
Exec Total Coverage
Lines: 315 368 85.6%
Functions: 16 17 94.1%
Branches: 228 302 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 "decode.h"
29 #include "hevc.h"
30 #include "hevcdec.h"
31 #include "progressframe.h"
32 #include "thread.h"
33 #include "libavutil/refstruct.h"
34
35 409069 void ff_hevc_unref_frame(HEVCFrame *frame, int flags)
36 {
37 409069 frame->flags &= ~flags;
38
2/2
✓ Branch 0 taken 349865 times.
✓ Branch 1 taken 59204 times.
409069 if (!(frame->flags & ~HEVC_FRAME_FLAG_CORRUPT))
39 349865 frame->flags = 0;
40
2/2
✓ Branch 0 taken 349865 times.
✓ Branch 1 taken 59204 times.
409069 if (!frame->flags) {
41 349865 ff_progress_frame_unref(&frame->tf);
42 349865 av_frame_unref(frame->frame_grain);
43 349865 frame->needs_fg = 0;
44
45 349865 av_refstruct_unref(&frame->pps);
46 349865 av_refstruct_unref(&frame->tab_mvf);
47
48 349865 av_refstruct_unref(&frame->rpl);
49 349865 frame->nb_rpl_elems = 0;
50 349865 av_refstruct_unref(&frame->rpl_tab);
51 349865 frame->refPicList = NULL;
52
53 349865 av_refstruct_unref(&frame->hwaccel_picture_private);
54 }
55 409069 }
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 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.lcevc.info) {
126 32 HEVCSEILCEVC *lcevc = &s->sei.common.lcevc;
127 32 ret = ff_frame_new_side_data_from_buf(s->avctx, frame->tf.f,
128 AV_FRAME_DATA_LCEVC, &lcevc->info);
129
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 32 times.
32 if (ret < 0)
130 goto fail;
131 }
132
133 // add view ID side data if it's nontrivial
134
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)) {
135 685 HEVCSEITDRDI *tdrdi = &s->sei.tdrdi;
136 685 AVFrameSideData *sd = av_frame_side_data_new(&frame->f->side_data,
137 685 &frame->f->nb_side_data,
138 AV_FRAME_DATA_VIEW_ID,
139 sizeof(int), 0);
140
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 685 times.
685 if (!sd)
141 goto fail;
142 685 *(int*)sd->data = view_id;
143
144
2/2
✓ Branch 0 taken 22 times.
✓ Branch 1 taken 663 times.
685 if (tdrdi->num_ref_displays) {
145 AVStereo3D *stereo_3d;
146
147 22 stereo_3d = av_stereo3d_create_side_data(frame->f);
148
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 22 times.
22 if (!stereo_3d)
149 goto fail;
150
151 22 stereo_3d->type = AV_STEREO3D_FRAMESEQUENCE;
152
2/2
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 11 times.
22 if (tdrdi->left_view_id[0] == view_id)
153 11 stereo_3d->view = AV_STEREO3D_VIEW_LEFT;
154
1/2
✓ Branch 0 taken 11 times.
✗ Branch 1 not taken.
11 else if (tdrdi->right_view_id[0] == view_id)
155 11 stereo_3d->view = AV_STEREO3D_VIEW_RIGHT;
156 else
157 stereo_3d->view = AV_STEREO3D_VIEW_UNSPEC;
158 }
159 }
160
161 10523 ret = ff_thread_get_buffer(s->avctx, frame->f, AV_GET_BUFFER_FLAG_REF);
162
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10523 times.
10523 if (ret < 0)
163 goto fail;
164
165 10523 frame->rpl = av_refstruct_allocz(s->pkt.nb_nals * sizeof(*frame->rpl));
166
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10523 times.
10523 if (!frame->rpl)
167 goto fail;
168 10523 frame->nb_rpl_elems = s->pkt.nb_nals;
169
170 10523 frame->tab_mvf = av_refstruct_pool_get(l->tab_mvf_pool);
171
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10523 times.
10523 if (!frame->tab_mvf)
172 goto fail;
173
174 10523 frame->rpl_tab = av_refstruct_pool_get(l->rpl_tab_pool);
175
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10523 times.
10523 if (!frame->rpl_tab)
176 goto fail;
177 10523 frame->ctb_count = l->sps->ctb_width * l->sps->ctb_height;
178
2/2
✓ Branch 0 taken 2079797 times.
✓ Branch 1 taken 10523 times.
2090320 for (j = 0; j < frame->ctb_count; j++)
179 2079797 frame->rpl_tab[j] = frame->rpl;
180
181
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)
182 15 frame->f->flags |= AV_FRAME_FLAG_TOP_FIELD_FIRST;
183
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) ||
184
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 10506 times.
10508 (s->sei.picture_timing.picture_struct == AV_PICTURE_STRUCTURE_BOTTOM_FIELD))
185 17 frame->f->flags |= AV_FRAME_FLAG_INTERLACED;
186
187 10523 ret = ff_hwaccel_frame_priv_alloc(s->avctx, &frame->hwaccel_picture_private);
188
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10523 times.
10523 if (ret < 0)
189 goto fail;
190
191 10523 frame->pps = av_refstruct_ref_c(s->pps);
192
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)) {
193 50 AVFrame *alpha = frame->f;
194 50 AVFrame *base = s->layers[0].cur_frame->f;
195 50 ret = replace_alpha_plane(alpha, base);
196
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 50 times.
50 if (ret < 0)
197 goto fail;
198 }
199
200 10523 return frame;
201 fail:
202 ff_hevc_unref_frame(frame, ~0);
203 return NULL;
204 }
205 av_log(s->avctx, AV_LOG_ERROR, "Error allocating frame, DPB full.\n");
206 return NULL;
207 }
208
209 10461 int ff_hevc_set_new_ref(HEVCContext *s, HEVCLayerContext *l, int poc)
210 {
211 HEVCFrame *ref;
212 int i;
213 int no_output;
214
215 /* check that this POC doesn't already exist */
216
2/2
✓ Branch 0 taken 334752 times.
✓ Branch 1 taken 10461 times.
345213 for (i = 0; i < FF_ARRAY_ELEMS(l->DPB); i++) {
217 334752 HEVCFrame *frame = &l->DPB[i];
218
219
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) {
220 av_log(s->avctx, AV_LOG_ERROR, "Duplicate POC in a sequence: %d.\n",
221 poc);
222 return AVERROR_INVALIDDATA;
223 }
224 }
225
226 10461 ref = alloc_frame(s, l);
227
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10461 times.
10461 if (!ref)
228 return AVERROR(ENOMEM);
229
230 10461 s->cur_frame = ref;
231 10461 l->cur_frame = ref;
232 10461 s->collocated_ref = NULL;
233
234
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) ?
235 390 s->layers[0].cur_frame - s->layers[0].DPB : -1;
236
237
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) &&
238
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
20 HEVC_IS_RECOVERING(s) &&
239
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) &&
240 !(s->avctx->flags2 & AV_CODEC_FLAG2_SHOW_ALL);
241
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)
242 10455 ref->flags = HEVC_FRAME_FLAG_OUTPUT | HEVC_FRAME_FLAG_SHORT_REF;
243 else
244 6 ref->flags = HEVC_FRAME_FLAG_SHORT_REF;
245
246 10461 ref->poc = poc;
247 10461 ref->f->crop_left = l->sps->output_window.left_offset;
248 10461 ref->f->crop_right = l->sps->output_window.right_offset;
249 10461 ref->f->crop_top = l->sps->output_window.top_offset;
250 10461 ref->f->crop_bottom = l->sps->output_window.bottom_offset;
251
252 10461 return 0;
253 }
254
255 10461 static void unref_missing_refs(HEVCLayerContext *l)
256 {
257
2/2
✓ Branch 0 taken 334752 times.
✓ Branch 1 taken 10461 times.
345213 for (int i = 0; i < FF_ARRAY_ELEMS(l->DPB); i++) {
258 334752 HEVCFrame *frame = &l->DPB[i];
259
2/2
✓ Branch 0 taken 37 times.
✓ Branch 1 taken 334715 times.
334752 if (frame->flags & HEVC_FRAME_FLAG_UNAVAILABLE) {
260 37 ff_hevc_unref_frame(frame, ~0);
261 }
262 }
263 10461 }
264
265 11247 int ff_hevc_output_frames(HEVCContext *s,
266 unsigned layers_active_decode, unsigned layers_active_output,
267 unsigned max_output, unsigned max_dpb, int discard)
268 {
269 10312 while (1) {
270 21559 int nb_dpb[HEVC_VPS_MAX_LAYERS] = { 0 };
271 21559 int nb_output = 0;
272 21559 int min_poc = INT_MAX;
273 21559 int min_layer = -1;
274 21559 int min_idx, ret = 0;
275
276
2/2
✓ Branch 0 taken 43118 times.
✓ Branch 1 taken 21559 times.
64677 for (int layer = 0; layer < FF_ARRAY_ELEMS(s->layers); layer++) {
277 43118 HEVCLayerContext *l = &s->layers[layer];
278
279
2/2
✓ Branch 0 taken 20404 times.
✓ Branch 1 taken 22714 times.
43118 if (!(layers_active_decode & (1 << layer)))
280 20404 continue;
281
282
2/2
✓ Branch 0 taken 726848 times.
✓ Branch 1 taken 22714 times.
749562 for (int i = 0; i < FF_ARRAY_ELEMS(l->DPB); i++) {
283 726848 HEVCFrame *frame = &l->DPB[i];
284
2/2
✓ Branch 0 taken 57015 times.
✓ Branch 1 taken 669833 times.
726848 if (frame->flags & HEVC_FRAME_FLAG_OUTPUT) {
285 // nb_output counts AUs with an output-pending frame
286 // in at least one layer
287
2/2
✓ Branch 0 taken 2420 times.
✓ Branch 1 taken 54595 times.
57015 if (!(frame->base_layer_frame >= 0 &&
288
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)))
289 54997 nb_output++;
290
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) {
291 30731 min_poc = frame->poc;
292 30731 min_idx = i;
293 30731 min_layer = layer;
294 }
295 }
296 726848 nb_dpb[layer] += !!frame->flags;
297 }
298 }
299
300
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 ||
301 7981 (nb_output &&
302
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))) {
303 10312 HEVCFrame *frame = &s->layers[min_layer].DPB[min_idx];
304
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10312 times.
10312 AVFrame *f = frame->needs_fg ? frame->frame_grain : frame->f;
305
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));
306
307
2/2
✓ Branch 0 taken 10170 times.
✓ Branch 1 taken 142 times.
10312 if (output) {
308
2/2
✓ Branch 0 taken 51 times.
✓ Branch 1 taken 10119 times.
10170 if (frame->flags & HEVC_FRAME_FLAG_CORRUPT)
309 51 f->flags |= AV_FRAME_FLAG_CORRUPT;
310 10170 f->pkt_dts = s->pkt_dts;
311 10170 ret = av_container_fifo_write(s->output_fifo, f, AV_CONTAINER_FIFO_FLAG_REF);
312 }
313 10312 ff_hevc_unref_frame(frame, HEVC_FRAME_FLAG_OUTPUT);
314
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10312 times.
10312 if (ret < 0)
315 11247 return ret;
316
317
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",
318 output ? "Output" : "Discarded", min_layer, frame->poc);
319 10312 continue;
320 }
321 11247 return 0;
322 }
323 }
324
325 19479 static int init_slice_rpl(HEVCContext *s)
326 {
327 19479 HEVCFrame *frame = s->cur_frame;
328 19479 int ctb_count = frame->ctb_count;
329 19479 int ctb_addr_ts = s->pps->ctb_addr_rs_to_ts[s->sh.slice_segment_addr];
330 int i;
331
332
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 19479 times.
19479 if (s->slice_idx >= frame->nb_rpl_elems)
333 return AVERROR_INVALIDDATA;
334
335
2/2
✓ Branch 0 taken 2838252 times.
✓ Branch 1 taken 19479 times.
2857731 for (i = ctb_addr_ts; i < ctb_count; i++)
336 2838252 frame->rpl_tab[i] = frame->rpl + s->slice_idx;
337
338 19479 frame->refPicList = (RefPicList *)frame->rpl_tab[ctb_addr_ts];
339
340 19479 return 0;
341 }
342
343 19479 int ff_hevc_slice_rpl(HEVCContext *s)
344 {
345 19479 SliceHeader *sh = &s->sh;
346
347
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;
348 uint8_t list_idx;
349 int i, j, ret;
350
351 19479 ret = init_slice_rpl(s);
352
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 19479 times.
19479 if (ret < 0)
353 return ret;
354
355 19479 if (!(s->rps[ST_CURR_BEF].nb_refs + s->rps[ST_CURR_AFT].nb_refs +
356 19479 s->rps[LT_CURR].nb_refs +
357
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 19479 times.
19479 s->rps[INTER_LAYER0].nb_refs + s->rps[INTER_LAYER1].nb_refs) &&
358 !s->pps->pps_curr_pic_ref_enabled_flag) {
359 av_log(s->avctx, AV_LOG_ERROR, "Zero refs in the frame RPS.\n");
360 return AVERROR_INVALIDDATA;
361 }
362
363
2/2
✓ Branch 0 taken 34651 times.
✓ Branch 1 taken 19479 times.
54130 for (list_idx = 0; list_idx < nb_list; list_idx++) {
364 34651 RefPicList rpl_tmp = { { 0 } };
365 34651 RefPicList *rpl = &s->cur_frame->refPicList[list_idx];
366
367 /* The order of the elements is
368 * ST_CURR_BEF - INTER_LAYER0 - ST_CURR_AFT - LT_CURR - INTER_LAYER1 for the L0 and
369 * ST_CURR_AFT - INTER_LAYER1 - ST_CURR_BEF - LT_CURR - INTER_LAYER0 for the L1 */
370 103953 int cand_lists[] = { list_idx ? ST_CURR_AFT : ST_CURR_BEF,
371
2/2
✓ Branch 0 taken 15172 times.
✓ Branch 1 taken 19479 times.
34651 list_idx ? INTER_LAYER1 : INTER_LAYER0,
372 34651 list_idx ? ST_CURR_BEF : ST_CURR_AFT,
373 LT_CURR,
374
2/2
✓ Branch 0 taken 15172 times.
✓ Branch 1 taken 19479 times.
34651 list_idx ? INTER_LAYER0 : INTER_LAYER1
375 };
376
377 /* concatenate the candidate lists for the current frame */
378
2/2
✓ Branch 0 taken 34669 times.
✓ Branch 1 taken 34651 times.
69320 while (rpl_tmp.nb_refs < sh->nb_refs[list_idx]) {
379
2/2
✓ Branch 0 taken 173345 times.
✓ Branch 1 taken 34669 times.
208014 for (i = 0; i < FF_ARRAY_ELEMS(cand_lists); i++) {
380 173345 RefPicList *rps = &s->rps[cand_lists[i]];
381
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++) {
382 124376 rpl_tmp.list[rpl_tmp.nb_refs] = rps->list[j];
383 124376 rpl_tmp.ref[rpl_tmp.nb_refs] = rps->ref[j];
384 // multiview inter-layer refs are treated as long-term here,
385 // cf. G.8.1.3
386 371722 rpl_tmp.isLongTerm[rpl_tmp.nb_refs] = cand_lists[i] == LT_CURR ||
387
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 ||
388
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 122392 times.
122392 cand_lists[i] == INTER_LAYER1;
389 124376 rpl_tmp.nb_refs++;
390 }
391 }
392 // Construct RefPicList0, RefPicList1 (8-8, 8-10)
393
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) {
394 rpl_tmp.list[rpl_tmp.nb_refs] = s->cur_frame->poc;
395 rpl_tmp.ref[rpl_tmp.nb_refs] = s->cur_frame;
396 rpl_tmp.isLongTerm[rpl_tmp.nb_refs] = 1;
397 rpl_tmp.nb_refs++;
398 }
399 }
400
401 /* reorder the references if necessary */
402
2/2
✓ Branch 0 taken 2083 times.
✓ Branch 1 taken 32568 times.
34651 if (sh->rpl_modification_flag[list_idx]) {
403
2/2
✓ Branch 0 taken 4920 times.
✓ Branch 1 taken 2083 times.
7003 for (i = 0; i < sh->nb_refs[list_idx]; i++) {
404 4920 int idx = sh->list_entry_lx[list_idx][i];
405
406
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4920 times.
4920 if (idx >= rpl_tmp.nb_refs) {
407 av_log(s->avctx, AV_LOG_ERROR, "Invalid reference index.\n");
408 return AVERROR_INVALIDDATA;
409 }
410
411 4920 rpl->list[i] = rpl_tmp.list[idx];
412 4920 rpl->ref[i] = rpl_tmp.ref[idx];
413 4920 rpl->isLongTerm[i] = rpl_tmp.isLongTerm[idx];
414 4920 rpl->nb_refs++;
415 }
416 } else {
417 32568 memcpy(rpl, &rpl_tmp, sizeof(*rpl));
418 32568 rpl->nb_refs = FFMIN(rpl->nb_refs, sh->nb_refs[list_idx]);
419 }
420
421 // 8-9
422
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 34651 times.
34651 if (s->pps->pps_curr_pic_ref_enabled_flag &&
423 !sh->rpl_modification_flag[list_idx] &&
424 rpl_tmp.nb_refs > sh->nb_refs[L0]) {
425 rpl->list[sh->nb_refs[L0] - 1] = s->cur_frame->poc;
426 rpl->ref[sh->nb_refs[L0] - 1] = s->cur_frame;
427 }
428
429
2/2
✓ Branch 0 taken 19478 times.
✓ Branch 1 taken 15173 times.
34651 if (sh->collocated_list == list_idx &&
430
1/2
✓ Branch 0 taken 19478 times.
✗ Branch 1 not taken.
19478 sh->collocated_ref_idx < rpl->nb_refs)
431 19478 s->collocated_ref = rpl->ref[sh->collocated_ref_idx];
432 }
433
434 19479 return 0;
435 }
436
437 37590 static HEVCFrame *find_ref_idx(HEVCContext *s, HEVCLayerContext *l,
438 int poc, uint8_t use_msb)
439 {
440
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;
441 int i;
442
443
2/2
✓ Branch 0 taken 153167 times.
✓ Branch 1 taken 62 times.
153229 for (i = 0; i < FF_ARRAY_ELEMS(l->DPB); i++) {
444 153167 HEVCFrame *ref = &l->DPB[i];
445
2/2
✓ Branch 0 taken 148238 times.
✓ Branch 1 taken 4929 times.
153167 if (ref->f) {
446
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))
447 37528 return ref;
448 }
449 }
450
451
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))
452 20 av_log(s->avctx, AV_LOG_ERROR,
453 "Could not find ref with POC %d\n", poc);
454 62 return NULL;
455 }
456
457 361881 static void mark_ref(HEVCFrame *frame, int flag)
458 {
459 361881 frame->flags &= ~(HEVC_FRAME_FLAG_LONG_REF | HEVC_FRAME_FLAG_SHORT_REF);
460 361881 frame->flags |= flag;
461 361881 }
462
463 62 static HEVCFrame *generate_missing_ref(HEVCContext *s, HEVCLayerContext *l, int poc)
464 {
465 HEVCFrame *frame;
466 int i, y;
467
468 62 frame = alloc_frame(s, l);
469
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 62 times.
62 if (!frame)
470 return NULL;
471
472
1/2
✓ Branch 0 taken 62 times.
✗ Branch 1 not taken.
62 if (!s->avctx->hwaccel) {
473
2/2
✓ Branch 0 taken 42 times.
✓ Branch 1 taken 20 times.
62 if (!l->sps->pixel_shift) {
474
2/2
✓ Branch 0 taken 126 times.
✓ Branch 1 taken 42 times.
168 for (i = 0; frame->f->data[i]; i++)
475 126 memset(frame->f->data[i], 1 << (l->sps->bit_depth - 1),
476 126 frame->f->linesize[i] * AV_CEIL_RSHIFT(l->sps->height, l->sps->vshift[i]));
477 } else {
478
2/2
✓ Branch 0 taken 60 times.
✓ Branch 1 taken 20 times.
80 for (i = 0; frame->f->data[i]; i++)
479
2/2
✓ Branch 0 taken 71424 times.
✓ Branch 1 taken 60 times.
71484 for (y = 0; y < (l->sps->height >> l->sps->vshift[i]); y++) {
480 71424 uint8_t *dst = frame->f->data[i] + y * frame->f->linesize[i];
481 71424 AV_WN16(dst, 1 << (l->sps->bit_depth - 1));
482 71424 av_memcpy_backptr(dst + 2, 2, 2*(l->sps->width >> l->sps->hshift[i]) - 2);
483 }
484 }
485 }
486
487 62 frame->poc = poc;
488 62 frame->flags = HEVC_FRAME_FLAG_UNAVAILABLE;
489
490
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 62 times.
62 if (s->avctx->active_thread_type == FF_THREAD_FRAME)
491 ff_progress_frame_report(&frame->tf, INT_MAX);
492
493 62 return frame;
494 }
495
496 /* add a reference with the given poc to the list and mark it as used in DPB */
497 37590 static int add_candidate_ref(HEVCContext *s, HEVCLayerContext *l,
498 RefPicList *list,
499 int poc, int ref_flag, uint8_t use_msb)
500 {
501 37590 HEVCFrame *ref = find_ref_idx(s, l, poc, use_msb);
502
503
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)
504 return AVERROR_INVALIDDATA;
505
506
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)) {
507
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 |
508 HEVC_FRAME_FLAG_UNAVAILABLE);
509
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);
510
511
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) {
512
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 137 times.
137 if (!(s->avctx->flags & AV_CODEC_FLAG_OUTPUT_CORRUPT) &&
513 !(s->avctx->flags2 & AV_CODEC_FLAG2_SHOW_ALL))
514 return AVERROR_INVALIDDATA;
515
516 137 s->cur_frame->flags |= HEVC_FRAME_FLAG_CORRUPT;
517 }
518 }
519
520
2/2
✓ Branch 0 taken 62 times.
✓ Branch 1 taken 37528 times.
37590 if (!ref) {
521 62 ref = generate_missing_ref(s, l, poc);
522
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 62 times.
62 if (!ref)
523 return AVERROR(ENOMEM);
524 }
525
526 37590 list->list[list->nb_refs] = ref->poc;
527 37590 list->ref[list->nb_refs] = ref;
528 37590 list->nb_refs++;
529
530 37590 mark_ref(ref, ref_flag);
531 37590 return 0;
532 }
533
534 10461 int ff_hevc_frame_rps(HEVCContext *s, HEVCLayerContext *l)
535 {
536 10461 const ShortTermRPS *short_rps = s->sh.short_term_rps;
537 10461 const LongTermRPS *long_rps = &s->sh.long_term_rps;
538 10461 RefPicList *rps = s->rps;
539 10461 int i, ret = 0;
540
541 10461 unref_missing_refs(l);
542
543 /* clear the reference flags on all frames except the current one */
544
2/2
✓ Branch 0 taken 334752 times.
✓ Branch 1 taken 10461 times.
345213 for (i = 0; i < FF_ARRAY_ELEMS(l->DPB); i++) {
545 334752 HEVCFrame *frame = &l->DPB[i];
546
547
2/2
✓ Branch 0 taken 10461 times.
✓ Branch 1 taken 324291 times.
334752 if (frame == s->cur_frame)
548 10461 continue;
549
550 324291 mark_ref(frame, 0);
551 }
552
553
2/2
✓ Branch 0 taken 73227 times.
✓ Branch 1 taken 10461 times.
83688 for (i = 0; i < NB_RPS_TYPE; i++)
554 73227 rps[i].nb_refs = 0;
555
556
2/2
✓ Branch 0 taken 444 times.
✓ Branch 1 taken 10017 times.
10461 if (!short_rps)
557 444 goto inter_layer;
558
559 /* add the short refs */
560
2/2
✓ Branch 0 taken 35612 times.
✓ Branch 1 taken 10017 times.
45629 for (i = 0; i < short_rps->num_delta_pocs; i++) {
561 35612 int poc = s->poc + short_rps->delta_poc[i];
562 int list;
563
564
2/2
✓ Branch 0 taken 1770 times.
✓ Branch 1 taken 33842 times.
35612 if (!(short_rps->used & (1 << i)))
565 1770 list = ST_FOLL;
566
2/2
✓ Branch 0 taken 24540 times.
✓ Branch 1 taken 9302 times.
33842 else if (i < short_rps->num_negative_pics)
567 24540 list = ST_CURR_BEF;
568 else
569 9302 list = ST_CURR_AFT;
570
571 35612 ret = add_candidate_ref(s, l, &rps[list], poc,
572 HEVC_FRAME_FLAG_SHORT_REF, 1);
573
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 35612 times.
35612 if (ret < 0)
574 goto fail;
575 }
576
577 /* add the long refs */
578
2/2
✓ Branch 0 taken 1683 times.
✓ Branch 1 taken 10017 times.
11700 for (i = 0; i < long_rps->nb_refs; i++) {
579 1683 int poc = long_rps->poc[i];
580
2/2
✓ Branch 0 taken 696 times.
✓ Branch 1 taken 987 times.
1683 int list = long_rps->used[i] ? LT_CURR : LT_FOLL;
581
582 1683 ret = add_candidate_ref(s, l, &rps[list], poc,
583 1683 HEVC_FRAME_FLAG_LONG_REF, long_rps->poc_msb_present[i]);
584
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1683 times.
1683 if (ret < 0)
585 goto fail;
586 }
587
588 10017 inter_layer:
589 /* add inter-layer refs */
590
2/2
✓ Branch 0 taken 10166 times.
✓ Branch 1 taken 295 times.
10461 if (s->sh.inter_layer_pred) {
591 295 HEVCLayerContext *l0 = &s->layers[0];
592
593
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 295 times.
295 av_assert0(l != l0);
594
595 /* Given the assumption of at most two layers, refPicSet0Flag is
596 * always 1, so only RefPicSetInterLayer0 can ever contain a frame. */
597
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 295 times.
295 if (l0->cur_frame) {
598 // inter-layer refs are treated as short-term here, cf. F.8.1.6
599 295 ret = add_candidate_ref(s, l0, &rps[INTER_LAYER0], l0->cur_frame->poc,
600 HEVC_FRAME_FLAG_SHORT_REF, 1);
601
1/2
✓ Branch 0 taken 295 times.
✗ Branch 1 not taken.
295 if (ret < 0)
602 goto fail;
603 }
604 }
605
606 10461 fail:
607 /* release any frames that are now unused */
608
2/2
✓ Branch 0 taken 334752 times.
✓ Branch 1 taken 10461 times.
345213 for (i = 0; i < FF_ARRAY_ELEMS(l->DPB); i++)
609 334752 ff_hevc_unref_frame(&l->DPB[i], 0);
610
611 10461 return ret;
612 }
613
614 19565 int ff_hevc_frame_nb_refs(const SliceHeader *sh, const HEVCPPS *pps,
615 unsigned layer_idx)
616 {
617 19565 int ret = 0;
618 int i;
619 19565 const ShortTermRPS *rps = sh->short_term_rps;
620 19565 const LongTermRPS *long_rps = &sh->long_term_rps;
621
622
2/2
✓ Branch 0 taken 19552 times.
✓ Branch 1 taken 13 times.
19565 if (rps) {
623
2/2
✓ Branch 0 taken 46770 times.
✓ Branch 1 taken 19552 times.
66322 for (i = 0; i < rps->num_negative_pics; i++)
624 46770 ret += !!(rps->used & (1 << i));
625
2/2
✓ Branch 0 taken 21104 times.
✓ Branch 1 taken 19552 times.
40656 for (; i < rps->num_delta_pocs; i++)
626 21104 ret += !!(rps->used & (1 << i));
627 }
628
629
2/2
✓ Branch 0 taken 1787 times.
✓ Branch 1 taken 19565 times.
21352 for (i = 0; i < long_rps->nb_refs; i++)
630 1787 ret += !!long_rps->used[i];
631
632
2/2
✓ Branch 0 taken 421 times.
✓ Branch 1 taken 19144 times.
19565 if (sh->inter_layer_pred) {
633
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 421 times.
421 av_assert0(pps->sps->vps->num_direct_ref_layers[layer_idx] < 2);
634 421 ret++;
635 }
636
637
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 19565 times.
19565 if (pps->pps_curr_pic_ref_enabled_flag)
638 ret++;
639
640 19565 return ret;
641 }
642