FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/hevc/refs.c
Date: 2025-07-14 21:09:40
Exec Total Coverage
Lines: 318 368 86.4%
Functions: 17 17 100.0%
Branches: 231 300 77.0%

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 399844 void ff_hevc_unref_frame(HEVCFrame *frame, int flags)
36 {
37 399844 frame->flags &= ~flags;
38
2/2
✓ Branch 0 taken 341220 times.
✓ Branch 1 taken 58624 times.
399844 if (!(frame->flags & ~HEVC_FRAME_FLAG_CORRUPT))
39 341220 frame->flags = 0;
40
2/2
✓ Branch 0 taken 341220 times.
✓ Branch 1 taken 58624 times.
399844 if (!frame->flags) {
41 341220 ff_progress_frame_unref(&frame->tf);
42 341220 av_frame_unref(frame->frame_grain);
43 341220 frame->needs_fg = 0;
44
45 341220 av_refstruct_unref(&frame->pps);
46 341220 av_refstruct_unref(&frame->tab_mvf);
47
48 341220 av_refstruct_unref(&frame->rpl);
49 341220 frame->nb_rpl_elems = 0;
50 341220 av_refstruct_unref(&frame->rpl_tab);
51 341220 frame->refPicList = NULL;
52
53 341220 av_refstruct_unref(&frame->hwaccel_picture_private);
54 }
55 399844 }
56
57 4716834 const RefPicList *ff_hevc_get_ref_list(const HEVCFrame *ref, int x0, int y0)
58 {
59 4716834 const HEVCSPS *sps = ref->pps->sps;
60 4716834 int x_cb = x0 >> sps->log2_ctb_size;
61 4716834 int y_cb = y0 >> sps->log2_ctb_size;
62 4716834 int pic_width_cb = sps->ctb_width;
63 4716834 int ctb_addr_ts = ref->pps->ctb_addr_rs_to_ts[y_cb * pic_width_cb + x_cb];
64 4716834 return &ref->rpl_tab[ctb_addr_ts]->refPicList[0];
65 }
66
67 851 void ff_hevc_clear_refs(HEVCLayerContext *l)
68 {
69 int i;
70
2/2
✓ Branch 0 taken 27232 times.
✓ Branch 1 taken 851 times.
28083 for (i = 0; i < FF_ARRAY_ELEMS(l->DPB); i++)
71 27232 ff_hevc_unref_frame(&l->DPB[i],
72 HEVC_FRAME_FLAG_SHORT_REF |
73 HEVC_FRAME_FLAG_LONG_REF);
74 851 }
75
76 9 void ff_hevc_flush_dpb(HEVCContext *s)
77 {
78
2/2
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 9 times.
27 for (int layer = 0; layer < FF_ARRAY_ELEMS(s->layers); layer++) {
79 18 HEVCLayerContext *l = &s->layers[layer];
80
2/2
✓ Branch 0 taken 576 times.
✓ Branch 1 taken 18 times.
594 for (int i = 0; i < FF_ARRAY_ELEMS(l->DPB); i++)
81 576 ff_hevc_unref_frame(&l->DPB[i], ~0);
82 }
83 9 }
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 10342 static HEVCFrame *alloc_frame(HEVCContext *s, HEVCLayerContext *l)
111 {
112 10342 const HEVCVPS *vps = l->sps->vps;
113 10342 const int view_id = vps->view_id[s->cur_layer];
114 int i, j, ret;
115
1/2
✓ Branch 0 taken 36253 times.
✗ Branch 1 not taken.
36253 for (i = 0; i < FF_ARRAY_ELEMS(l->DPB); i++) {
116 36253 HEVCFrame *frame = &l->DPB[i];
117
2/2
✓ Branch 0 taken 25911 times.
✓ Branch 1 taken 10342 times.
36253 if (frame->f)
118 25911 continue;
119
120 10342 ret = ff_progress_frame_alloc(s->avctx, &frame->tf);
121
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10342 times.
10342 if (ret < 0)
122 return NULL;
123
124 // Add LCEVC SEI metadata here, as it's needed in get_buffer()
125
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10342 times.
10342 if (s->sei.common.lcevc.info) {
126 HEVCSEILCEVC *lcevc = &s->sei.common.lcevc;
127 ret = ff_frame_new_side_data_from_buf(s->avctx, frame->tf.f,
128 AV_FRAME_DATA_LCEVC, &lcevc->info);
129 if (ret < 0)
130 goto fail;
131 }
132
133 // add view ID side data if it's nontrivial
134
5/6
✓ Branch 1 taken 10242 times.
✓ Branch 2 taken 100 times.
✓ Branch 3 taken 9683 times.
✓ Branch 4 taken 559 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 9683 times.
10342 if (!ff_hevc_is_alpha_video(s) && (vps->nb_layers > 1 || view_id)) {
135 559 HEVCSEITDRDI *tdrdi = &s->sei.tdrdi;
136 559 AVFrameSideData *sd = av_frame_side_data_new(&frame->f->side_data,
137 559 &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 559 times.
559 if (!sd)
141 goto fail;
142 559 *(int*)sd->data = view_id;
143
144
2/2
✓ Branch 0 taken 22 times.
✓ Branch 1 taken 537 times.
559 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 10342 ret = ff_thread_get_buffer(s->avctx, frame->f, AV_GET_BUFFER_FLAG_REF);
162
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10342 times.
10342 if (ret < 0)
163 goto fail;
164
165 10342 frame->rpl = av_refstruct_allocz(s->pkt.nb_nals * sizeof(*frame->rpl));
166
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10342 times.
10342 if (!frame->rpl)
167 goto fail;
168 10342 frame->nb_rpl_elems = s->pkt.nb_nals;
169
170 10342 frame->tab_mvf = av_refstruct_pool_get(l->tab_mvf_pool);
171
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10342 times.
10342 if (!frame->tab_mvf)
172 goto fail;
173
174 10342 frame->rpl_tab = av_refstruct_pool_get(l->rpl_tab_pool);
175
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10342 times.
10342 if (!frame->rpl_tab)
176 goto fail;
177 10342 frame->ctb_count = l->sps->ctb_width * l->sps->ctb_height;
178
2/2
✓ Branch 0 taken 1673057 times.
✓ Branch 1 taken 10342 times.
1683399 for (j = 0; j < frame->ctb_count; j++)
179 1673057 frame->rpl_tab[j] = frame->rpl;
180
181
2/2
✓ Branch 0 taken 15 times.
✓ Branch 1 taken 10327 times.
10342 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 10327 times.
✓ Branch 1 taken 15 times.
10342 if ((s->sei.picture_timing.picture_struct == AV_PICTURE_STRUCTURE_TOP_FIELD) ||
184
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 10325 times.
10327 (s->sei.picture_timing.picture_struct == AV_PICTURE_STRUCTURE_BOTTOM_FIELD))
185 17 frame->f->flags |= AV_FRAME_FLAG_INTERLACED;
186
187 10342 ret = ff_hwaccel_frame_priv_alloc(s->avctx, &frame->hwaccel_picture_private);
188
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10342 times.
10342 if (ret < 0)
189 goto fail;
190
191 10342 frame->pps = av_refstruct_ref_c(s->pps);
192
4/4
✓ Branch 0 taken 327 times.
✓ Branch 1 taken 10015 times.
✓ Branch 3 taken 50 times.
✓ Branch 4 taken 277 times.
10342 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 10342 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 10284 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 329088 times.
✓ Branch 1 taken 10284 times.
339372 for (i = 0; i < FF_ARRAY_ELEMS(l->DPB); i++) {
217 329088 HEVCFrame *frame = &l->DPB[i];
218
219
3/4
✓ Branch 0 taken 47457 times.
✓ Branch 1 taken 281631 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 47457 times.
329088 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 10284 ref = alloc_frame(s, l);
227
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10284 times.
10284 if (!ref)
228 return AVERROR(ENOMEM);
229
230 10284 s->cur_frame = ref;
231 10284 l->cur_frame = ref;
232 10284 s->collocated_ref = NULL;
233
234
3/4
✓ Branch 0 taken 327 times.
✓ Branch 1 taken 9957 times.
✓ Branch 2 taken 327 times.
✗ Branch 3 not taken.
10284 ref->base_layer_frame = (l != &s->layers[0] && s->layers[0].cur_frame) ?
235 327 s->layers[0].cur_frame - s->layers[0].DPB : -1;
236
237
3/4
✗ Branch 0 not taken.
✓ Branch 1 taken 730 times.
✓ Branch 2 taken 11 times.
✓ Branch 3 taken 9543 times.
10284 no_output = !IS_IRAP(s) && (s->poc < s->recovery_poc) &&
238
3/4
✓ Branch 0 taken 730 times.
✓ Branch 1 taken 9554 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 11 times.
20568 !(s->avctx->flags & AV_CODEC_FLAG_OUTPUT_CORRUPT) &&
239 !(s->avctx->flags2 & AV_CODEC_FLAG2_SHOW_ALL);
240
3/4
✓ Branch 0 taken 10278 times.
✓ Branch 1 taken 6 times.
✓ Branch 2 taken 10278 times.
✗ Branch 3 not taken.
10284 if (s->sh.pic_output_flag && !no_output)
241 10278 ref->flags = HEVC_FRAME_FLAG_OUTPUT | HEVC_FRAME_FLAG_SHORT_REF;
242 else
243 6 ref->flags = HEVC_FRAME_FLAG_SHORT_REF;
244
245 10284 ref->poc = poc;
246 10284 ref->f->crop_left = l->sps->output_window.left_offset;
247 10284 ref->f->crop_right = l->sps->output_window.right_offset;
248 10284 ref->f->crop_top = l->sps->output_window.top_offset;
249 10284 ref->f->crop_bottom = l->sps->output_window.bottom_offset;
250
251 10284 return 0;
252 }
253
254 10284 static void unref_missing_refs(HEVCLayerContext *l)
255 {
256
2/2
✓ Branch 0 taken 329088 times.
✓ Branch 1 taken 10284 times.
339372 for (int i = 0; i < FF_ARRAY_ELEMS(l->DPB); i++) {
257 329088 HEVCFrame *frame = &l->DPB[i];
258
2/2
✓ Branch 0 taken 37 times.
✓ Branch 1 taken 329051 times.
329088 if (frame->flags & HEVC_FRAME_FLAG_UNAVAILABLE) {
259 37 ff_hevc_unref_frame(frame, ~0);
260 }
261 }
262 10284 }
263
264 11030 int ff_hevc_output_frames(HEVCContext *s,
265 unsigned layers_active_decode, unsigned layers_active_output,
266 unsigned max_output, unsigned max_dpb, int discard)
267 {
268 10143 while (1) {
269 21173 int nb_dpb[HEVC_VPS_MAX_LAYERS] = { 0 };
270 21173 int nb_output = 0;
271 21173 int min_poc = INT_MAX;
272 21173 int min_layer = -1;
273 21173 int min_idx, ret = 0;
274
275
2/2
✓ Branch 0 taken 42346 times.
✓ Branch 1 taken 21173 times.
63519 for (int layer = 0; layer < FF_ARRAY_ELEMS(s->layers); layer++) {
276 42346 HEVCLayerContext *l = &s->layers[layer];
277
278
2/2
✓ Branch 0 taken 20249 times.
✓ Branch 1 taken 22097 times.
42346 if (!(layers_active_decode & (1 << layer)))
279 20249 continue;
280
281
2/2
✓ Branch 0 taken 707104 times.
✓ Branch 1 taken 22097 times.
729201 for (int i = 0; i < FF_ARRAY_ELEMS(l->DPB); i++) {
282 707104 HEVCFrame *frame = &l->DPB[i];
283
2/2
✓ Branch 0 taken 56722 times.
✓ Branch 1 taken 650382 times.
707104 if (frame->flags & HEVC_FRAME_FLAG_OUTPUT) {
284 // nb_output counts AUs with an output-pending frame
285 // in at least one layer
286
2/2
✓ Branch 0 taken 2357 times.
✓ Branch 1 taken 54365 times.
56722 if (!(frame->base_layer_frame >= 0 &&
287
2/2
✓ Branch 0 taken 339 times.
✓ Branch 1 taken 2018 times.
2357 (s->layers[0].DPB[frame->base_layer_frame].flags & HEVC_FRAME_FLAG_OUTPUT)))
288 54704 nb_output++;
289
4/4
✓ Branch 0 taken 39085 times.
✓ Branch 1 taken 17637 times.
✓ Branch 2 taken 12831 times.
✓ Branch 3 taken 26254 times.
56722 if (min_layer < 0 || frame->poc < min_poc) {
290 30468 min_poc = frame->poc;
291 30468 min_idx = i;
292 30468 min_layer = layer;
293 }
294 }
295 707104 nb_dpb[layer] += !!frame->flags;
296 }
297 }
298
299
4/4
✓ Branch 0 taken 11479 times.
✓ Branch 1 taken 9694 times.
✓ Branch 2 taken 7943 times.
✓ Branch 3 taken 3536 times.
21173 if (nb_output > max_output ||
300 7943 (nb_output &&
301
4/4
✓ Branch 0 taken 7506 times.
✓ Branch 1 taken 437 times.
✓ Branch 2 taken 12 times.
✓ Branch 3 taken 7494 times.
7943 (nb_dpb[0] > max_dpb || nb_dpb[1] > max_dpb))) {
302 10143 HEVCFrame *frame = &s->layers[min_layer].DPB[min_idx];
303
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10143 times.
10143 AVFrame *f = frame->needs_fg ? frame->frame_grain : frame->f;
304
4/4
✓ Branch 0 taken 10114 times.
✓ Branch 1 taken 29 times.
✓ Branch 2 taken 10064 times.
✓ Branch 3 taken 50 times.
10143 int output = !discard && (layers_active_output & (1 << min_layer));
305
306
2/2
✓ Branch 0 taken 10064 times.
✓ Branch 1 taken 79 times.
10143 if (output) {
307
2/2
✓ Branch 0 taken 51 times.
✓ Branch 1 taken 10013 times.
10064 if (frame->flags & HEVC_FRAME_FLAG_CORRUPT)
308 51 f->flags |= AV_FRAME_FLAG_CORRUPT;
309 10064 f->pkt_dts = s->pkt_dts;
310 10064 ret = av_container_fifo_write(s->output_fifo, f, AV_CONTAINER_FIFO_FLAG_REF);
311 }
312 10143 ff_hevc_unref_frame(frame, HEVC_FRAME_FLAG_OUTPUT);
313
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10143 times.
10143 if (ret < 0)
314 11030 return ret;
315
316
2/2
✓ Branch 0 taken 10064 times.
✓ Branch 1 taken 79 times.
10143 av_log(s->avctx, AV_LOG_DEBUG, "%s frame with POC %d/%d.\n",
317 output ? "Output" : "Discarded", min_layer, frame->poc);
318 10143 continue;
319 }
320 11030 return 0;
321 }
322 }
323
324 19078 static int init_slice_rpl(HEVCContext *s)
325 {
326 19078 HEVCFrame *frame = s->cur_frame;
327 19078 int ctb_count = frame->ctb_count;
328 19078 int ctb_addr_ts = s->pps->ctb_addr_rs_to_ts[s->sh.slice_segment_addr];
329 int i;
330
331
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 19078 times.
19078 if (s->slice_idx >= frame->nb_rpl_elems)
332 return AVERROR_INVALIDDATA;
333
334
2/2
✓ Branch 0 taken 2122272 times.
✓ Branch 1 taken 19078 times.
2141350 for (i = ctb_addr_ts; i < ctb_count; i++)
335 2122272 frame->rpl_tab[i] = frame->rpl + s->slice_idx;
336
337 19078 frame->refPicList = (RefPicList *)frame->rpl_tab[ctb_addr_ts];
338
339 19078 return 0;
340 }
341
342 19078 int ff_hevc_slice_rpl(HEVCContext *s)
343 {
344 19078 SliceHeader *sh = &s->sh;
345
346
2/2
✓ Branch 0 taken 15151 times.
✓ Branch 1 taken 3927 times.
19078 uint8_t nb_list = sh->slice_type == HEVC_SLICE_B ? 2 : 1;
347 uint8_t list_idx;
348 int i, j, ret;
349
350 19078 ret = init_slice_rpl(s);
351
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 19078 times.
19078 if (ret < 0)
352 return ret;
353
354 19078 if (!(s->rps[ST_CURR_BEF].nb_refs + s->rps[ST_CURR_AFT].nb_refs +
355 19078 s->rps[LT_CURR].nb_refs +
356
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 19078 times.
19078 s->rps[INTER_LAYER0].nb_refs + s->rps[INTER_LAYER1].nb_refs) &&
357 !s->pps->pps_curr_pic_ref_enabled_flag) {
358 av_log(s->avctx, AV_LOG_ERROR, "Zero refs in the frame RPS.\n");
359 return AVERROR_INVALIDDATA;
360 }
361
362
2/2
✓ Branch 0 taken 34229 times.
✓ Branch 1 taken 19078 times.
53307 for (list_idx = 0; list_idx < nb_list; list_idx++) {
363 34229 RefPicList rpl_tmp = { { 0 } };
364 34229 RefPicList *rpl = &s->cur_frame->refPicList[list_idx];
365
366 /* The order of the elements is
367 * ST_CURR_BEF - INTER_LAYER0 - ST_CURR_AFT - LT_CURR - INTER_LAYER1 for the L0 and
368 * ST_CURR_AFT - INTER_LAYER1 - ST_CURR_BEF - LT_CURR - INTER_LAYER0 for the L1 */
369 102687 int cand_lists[] = { list_idx ? ST_CURR_AFT : ST_CURR_BEF,
370
2/2
✓ Branch 0 taken 15151 times.
✓ Branch 1 taken 19078 times.
34229 list_idx ? INTER_LAYER1 : INTER_LAYER0,
371 34229 list_idx ? ST_CURR_BEF : ST_CURR_AFT,
372 LT_CURR,
373
2/2
✓ Branch 0 taken 15151 times.
✓ Branch 1 taken 19078 times.
34229 list_idx ? INTER_LAYER0 : INTER_LAYER1
374 };
375
376 /* concatenate the candidate lists for the current frame */
377
2/2
✓ Branch 0 taken 34247 times.
✓ Branch 1 taken 34229 times.
68476 while (rpl_tmp.nb_refs < sh->nb_refs[list_idx]) {
378
2/2
✓ Branch 0 taken 171235 times.
✓ Branch 1 taken 34247 times.
205482 for (i = 0; i < FF_ARRAY_ELEMS(cand_lists); i++) {
379 171235 RefPicList *rps = &s->rps[cand_lists[i]];
380
3/4
✓ Branch 0 taken 123633 times.
✓ Branch 1 taken 171235 times.
✓ Branch 2 taken 123633 times.
✗ Branch 3 not taken.
294868 for (j = 0; j < rps->nb_refs && rpl_tmp.nb_refs < HEVC_MAX_REFS; j++) {
381 123633 rpl_tmp.list[rpl_tmp.nb_refs] = rps->list[j];
382 123633 rpl_tmp.ref[rpl_tmp.nb_refs] = rps->ref[j];
383 // multiview inter-layer refs are treated as long-term here,
384 // cf. G.8.1.3
385 369859 rpl_tmp.isLongTerm[rpl_tmp.nb_refs] = cand_lists[i] == LT_CURR ||
386
4/4
✓ Branch 0 taken 122593 times.
✓ Branch 1 taken 1040 times.
✓ Branch 2 taken 122204 times.
✓ Branch 3 taken 389 times.
245837 cand_lists[i] == INTER_LAYER0 ||
387
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 122204 times.
122204 cand_lists[i] == INTER_LAYER1;
388 123633 rpl_tmp.nb_refs++;
389 }
390 }
391 // Construct RefPicList0, RefPicList1 (8-8, 8-10)
392
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 34247 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
34247 if (s->pps->pps_curr_pic_ref_enabled_flag && rpl_tmp.nb_refs < HEVC_MAX_REFS) {
393 rpl_tmp.list[rpl_tmp.nb_refs] = s->cur_frame->poc;
394 rpl_tmp.ref[rpl_tmp.nb_refs] = s->cur_frame;
395 rpl_tmp.isLongTerm[rpl_tmp.nb_refs] = 1;
396 rpl_tmp.nb_refs++;
397 }
398 }
399
400 /* reorder the references if necessary */
401
2/2
✓ Branch 0 taken 1900 times.
✓ Branch 1 taken 32329 times.
34229 if (sh->rpl_modification_flag[list_idx]) {
402
2/2
✓ Branch 0 taken 4554 times.
✓ Branch 1 taken 1900 times.
6454 for (i = 0; i < sh->nb_refs[list_idx]; i++) {
403 4554 int idx = sh->list_entry_lx[list_idx][i];
404
405
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4554 times.
4554 if (idx >= rpl_tmp.nb_refs) {
406 av_log(s->avctx, AV_LOG_ERROR, "Invalid reference index.\n");
407 return AVERROR_INVALIDDATA;
408 }
409
410 4554 rpl->list[i] = rpl_tmp.list[idx];
411 4554 rpl->ref[i] = rpl_tmp.ref[idx];
412 4554 rpl->isLongTerm[i] = rpl_tmp.isLongTerm[idx];
413 4554 rpl->nb_refs++;
414 }
415 } else {
416 32329 memcpy(rpl, &rpl_tmp, sizeof(*rpl));
417 32329 rpl->nb_refs = FFMIN(rpl->nb_refs, sh->nb_refs[list_idx]);
418 }
419
420 // 8-9
421
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 34229 times.
34229 if (s->pps->pps_curr_pic_ref_enabled_flag &&
422 !sh->rpl_modification_flag[list_idx] &&
423 rpl_tmp.nb_refs > sh->nb_refs[L0]) {
424 rpl->list[sh->nb_refs[L0] - 1] = s->cur_frame->poc;
425 rpl->ref[sh->nb_refs[L0] - 1] = s->cur_frame;
426 }
427
428
2/2
✓ Branch 0 taken 19077 times.
✓ Branch 1 taken 15152 times.
34229 if (sh->collocated_list == list_idx &&
429
1/2
✓ Branch 0 taken 19077 times.
✗ Branch 1 not taken.
19077 sh->collocated_ref_idx < rpl->nb_refs)
430 19077 s->collocated_ref = rpl->ref[sh->collocated_ref_idx];
431 }
432
433 19078 return 0;
434 }
435
436 37293 static HEVCFrame *find_ref_idx(HEVCContext *s, HEVCLayerContext *l,
437 int poc, uint8_t use_msb)
438 {
439
2/2
✓ Branch 0 taken 1513 times.
✓ Branch 1 taken 35780 times.
37293 int mask = use_msb ? ~0 : (1 << l->sps->log2_max_poc_lsb) - 1;
440 int i;
441
442
2/2
✓ Branch 0 taken 152319 times.
✓ Branch 1 taken 58 times.
152377 for (i = 0; i < FF_ARRAY_ELEMS(l->DPB); i++) {
443 152319 HEVCFrame *ref = &l->DPB[i];
444
2/2
✓ Branch 0 taken 147529 times.
✓ Branch 1 taken 4790 times.
152319 if (ref->f) {
445
5/6
✓ Branch 0 taken 37235 times.
✓ Branch 1 taken 110294 times.
✓ Branch 2 taken 1512 times.
✓ Branch 3 taken 35723 times.
✓ Branch 4 taken 1512 times.
✗ Branch 5 not taken.
147529 if ((ref->poc & mask) == poc && (use_msb || ref->poc != s->poc))
446 37235 return ref;
447 }
448 }
449
450
7/8
✓ Branch 0 taken 23 times.
✓ Branch 1 taken 35 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.
58 if (s->nal_unit_type != HEVC_NAL_CRA_NUT && !IS_BLA(s))
451 20 av_log(s->avctx, AV_LOG_ERROR,
452 "Could not find ref with POC %d\n", poc);
453 58 return NULL;
454 }
455
456 356097 static void mark_ref(HEVCFrame *frame, int flag)
457 {
458 356097 frame->flags &= ~(HEVC_FRAME_FLAG_LONG_REF | HEVC_FRAME_FLAG_SHORT_REF);
459 356097 frame->flags |= flag;
460 356097 }
461
462 58 static HEVCFrame *generate_missing_ref(HEVCContext *s, HEVCLayerContext *l, int poc)
463 {
464 HEVCFrame *frame;
465 int i, y;
466
467 58 frame = alloc_frame(s, l);
468
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 58 times.
58 if (!frame)
469 return NULL;
470
471
1/2
✓ Branch 0 taken 58 times.
✗ Branch 1 not taken.
58 if (!s->avctx->hwaccel) {
472
2/2
✓ Branch 0 taken 38 times.
✓ Branch 1 taken 20 times.
58 if (!l->sps->pixel_shift) {
473
2/2
✓ Branch 0 taken 114 times.
✓ Branch 1 taken 38 times.
152 for (i = 0; frame->f->data[i]; i++)
474 114 memset(frame->f->data[i], 1 << (l->sps->bit_depth - 1),
475 114 frame->f->linesize[i] * AV_CEIL_RSHIFT(l->sps->height, l->sps->vshift[i]));
476 } else {
477
2/2
✓ Branch 0 taken 60 times.
✓ Branch 1 taken 20 times.
80 for (i = 0; frame->f->data[i]; i++)
478
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++) {
479 71424 uint8_t *dst = frame->f->data[i] + y * frame->f->linesize[i];
480 71424 AV_WN16(dst, 1 << (l->sps->bit_depth - 1));
481 71424 av_memcpy_backptr(dst + 2, 2, 2*(l->sps->width >> l->sps->hshift[i]) - 2);
482 }
483 }
484 }
485
486 58 frame->poc = poc;
487 58 frame->flags = HEVC_FRAME_FLAG_UNAVAILABLE;
488
489
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 58 times.
58 if (s->avctx->active_thread_type == FF_THREAD_FRAME)
490 ff_progress_frame_report(&frame->tf, INT_MAX);
491
492 58 return frame;
493 }
494
495 /* add a reference with the given poc to the list and mark it as used in DPB */
496 37293 static int add_candidate_ref(HEVCContext *s, HEVCLayerContext *l,
497 RefPicList *list,
498 int poc, int ref_flag, uint8_t use_msb)
499 {
500 37293 HEVCFrame *ref = find_ref_idx(s, l, poc, use_msb);
501
502
2/4
✓ Branch 0 taken 37293 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 37293 times.
37293 if (ref == s->cur_frame || list->nb_refs >= HEVC_MAX_REFS)
503 return AVERROR_INVALIDDATA;
504
505
3/4
✓ Branch 0 taken 723 times.
✓ Branch 1 taken 36570 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 723 times.
37293 if (!IS_IRAP(s)) {
506
4/4
✓ Branch 0 taken 36550 times.
✓ Branch 1 taken 20 times.
✓ Branch 2 taken 117 times.
✓ Branch 3 taken 36433 times.
36570 int ref_corrupt = !ref || ref->flags & (HEVC_FRAME_FLAG_CORRUPT |
507 HEVC_FRAME_FLAG_UNAVAILABLE);
508
3/4
✓ Branch 0 taken 36547 times.
✓ Branch 1 taken 23 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 36547 times.
36570 int recovering = HEVC_IS_RECOVERING(s);
509
510
3/4
✓ Branch 0 taken 137 times.
✓ Branch 1 taken 36433 times.
✓ Branch 2 taken 137 times.
✗ Branch 3 not taken.
36570 if (ref_corrupt && !recovering) {
511
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 137 times.
137 if (!(s->avctx->flags & AV_CODEC_FLAG_OUTPUT_CORRUPT) &&
512 !(s->avctx->flags2 & AV_CODEC_FLAG2_SHOW_ALL))
513 return AVERROR_INVALIDDATA;
514
515 137 s->cur_frame->flags |= HEVC_FRAME_FLAG_CORRUPT;
516 }
517 }
518
519
2/2
✓ Branch 0 taken 58 times.
✓ Branch 1 taken 37235 times.
37293 if (!ref) {
520 58 ref = generate_missing_ref(s, l, poc);
521
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 58 times.
58 if (!ref)
522 return AVERROR(ENOMEM);
523 }
524
525 37293 list->list[list->nb_refs] = ref->poc;
526 37293 list->ref[list->nb_refs] = ref;
527 37293 list->nb_refs++;
528
529 37293 mark_ref(ref, ref_flag);
530 37293 return 0;
531 }
532
533 10284 int ff_hevc_frame_rps(HEVCContext *s, HEVCLayerContext *l)
534 {
535 10284 const ShortTermRPS *short_rps = s->sh.short_term_rps;
536 10284 const LongTermRPS *long_rps = &s->sh.long_term_rps;
537 10284 RefPicList *rps = s->rps;
538 10284 int i, ret = 0;
539
540 10284 unref_missing_refs(l);
541
542 /* clear the reference flags on all frames except the current one */
543
2/2
✓ Branch 0 taken 329088 times.
✓ Branch 1 taken 10284 times.
339372 for (i = 0; i < FF_ARRAY_ELEMS(l->DPB); i++) {
544 329088 HEVCFrame *frame = &l->DPB[i];
545
546
2/2
✓ Branch 0 taken 10284 times.
✓ Branch 1 taken 318804 times.
329088 if (frame == s->cur_frame)
547 10284 continue;
548
549 318804 mark_ref(frame, 0);
550 }
551
552
2/2
✓ Branch 0 taken 71988 times.
✓ Branch 1 taken 10284 times.
82272 for (i = 0; i < NB_RPS_TYPE; i++)
553 71988 rps[i].nb_refs = 0;
554
555
2/2
✓ Branch 0 taken 428 times.
✓ Branch 1 taken 9856 times.
10284 if (!short_rps)
556 428 goto inter_layer;
557
558 /* add the short refs */
559
2/2
✓ Branch 0 taken 35500 times.
✓ Branch 1 taken 9856 times.
45356 for (i = 0; i < short_rps->num_delta_pocs; i++) {
560 35500 int poc = s->poc + short_rps->delta_poc[i];
561 int list;
562
563
2/2
✓ Branch 0 taken 1766 times.
✓ Branch 1 taken 33734 times.
35500 if (!(short_rps->used & (1 << i)))
564 1766 list = ST_FOLL;
565
2/2
✓ Branch 0 taken 24462 times.
✓ Branch 1 taken 9272 times.
33734 else if (i < short_rps->num_negative_pics)
566 24462 list = ST_CURR_BEF;
567 else
568 9272 list = ST_CURR_AFT;
569
570 35500 ret = add_candidate_ref(s, l, &rps[list], poc,
571 HEVC_FRAME_FLAG_SHORT_REF, 1);
572
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 35500 times.
35500 if (ret < 0)
573 goto fail;
574 }
575
576 /* add the long refs */
577
2/2
✓ Branch 0 taken 1561 times.
✓ Branch 1 taken 9856 times.
11417 for (i = 0; i < long_rps->nb_refs; i++) {
578 1561 int poc = long_rps->poc[i];
579
2/2
✓ Branch 0 taken 574 times.
✓ Branch 1 taken 987 times.
1561 int list = long_rps->used[i] ? LT_CURR : LT_FOLL;
580
581 1561 ret = add_candidate_ref(s, l, &rps[list], poc,
582 1561 HEVC_FRAME_FLAG_LONG_REF, long_rps->poc_msb_present[i]);
583
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1561 times.
1561 if (ret < 0)
584 goto fail;
585 }
586
587 9856 inter_layer:
588 /* add inter-layer refs */
589
2/2
✓ Branch 0 taken 10052 times.
✓ Branch 1 taken 232 times.
10284 if (s->sh.inter_layer_pred) {
590 232 HEVCLayerContext *l0 = &s->layers[0];
591
592
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 232 times.
232 av_assert0(l != l0);
593
594 /* Given the assumption of at most two layers, refPicSet0Flag is
595 * always 1, so only RefPicSetInterLayer0 can ever contain a frame. */
596
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 232 times.
232 if (l0->cur_frame) {
597 // inter-layer refs are treated as short-term here, cf. F.8.1.6
598 232 ret = add_candidate_ref(s, l0, &rps[INTER_LAYER0], l0->cur_frame->poc,
599 HEVC_FRAME_FLAG_SHORT_REF, 1);
600
1/2
✓ Branch 0 taken 232 times.
✗ Branch 1 not taken.
232 if (ret < 0)
601 goto fail;
602 }
603 }
604
605 10284 fail:
606 /* release any frames that are now unused */
607
2/2
✓ Branch 0 taken 329088 times.
✓ Branch 1 taken 10284 times.
339372 for (i = 0; i < FF_ARRAY_ELEMS(l->DPB); i++)
608 329088 ff_hevc_unref_frame(&l->DPB[i], 0);
609
610 10284 return ret;
611 }
612
613 19164 int ff_hevc_frame_nb_refs(const SliceHeader *sh, const HEVCPPS *pps,
614 unsigned layer_idx)
615 {
616 19164 int ret = 0;
617 int i;
618 19164 const ShortTermRPS *rps = sh->short_term_rps;
619 19164 const LongTermRPS *long_rps = &sh->long_term_rps;
620
621
2/2
✓ Branch 0 taken 19157 times.
✓ Branch 1 taken 7 times.
19164 if (rps) {
622
2/2
✓ Branch 0 taken 46692 times.
✓ Branch 1 taken 19157 times.
65849 for (i = 0; i < rps->num_negative_pics; i++)
623 46692 ret += !!(rps->used & (1 << i));
624
2/2
✓ Branch 0 taken 21074 times.
✓ Branch 1 taken 19157 times.
40231 for (; i < rps->num_delta_pocs; i++)
625 21074 ret += !!(rps->used & (1 << i));
626 }
627
628
1/2
✓ Branch 0 taken 19164 times.
✗ Branch 1 not taken.
19164 if (long_rps) {
629
2/2
✓ Branch 0 taken 1421 times.
✓ Branch 1 taken 19164 times.
20585 for (i = 0; i < long_rps->nb_refs; i++)
630 1421 ret += !!long_rps->used[i];
631 }
632
633
2/2
✓ Branch 0 taken 232 times.
✓ Branch 1 taken 18932 times.
19164 if (sh->inter_layer_pred) {
634
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 232 times.
232 av_assert0(pps->sps->vps->num_direct_ref_layers[layer_idx] < 2);
635 232 ret++;
636 }
637
638
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 19164 times.
19164 if (pps->pps_curr_pic_ref_enabled_flag)
639 ret++;
640
641 19164 return ret;
642 }
643