FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/hevc/refs.c
Date: 2026-11-08 03:48:22
Exec Total Coverage
Lines: 318 371 85.7%
Functions: 16 17 94.1%
Branches: 236 312 75.6%

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 416058 void ff_hevc_unref_frame(HEVCFrame *frame, int flags)
36 {
37 416058 frame->flags &= ~flags;
38
2/2
✓ Branch 0 taken 354753 times.
✓ Branch 1 taken 61305 times.
416058 if (!(frame->flags & ~HEVC_FRAME_FLAG_CORRUPT))
39 354753 frame->flags = 0;
40
2/2
✓ Branch 0 taken 354753 times.
✓ Branch 1 taken 61305 times.
416058 if (!frame->flags) {
41 354753 av_refstruct_unref(&frame->hwaccel_picture_private);
42
43 354753 ff_progress_frame_unref(&frame->tf);
44 354753 av_frame_unref(frame->frame_grain);
45 354753 frame->needs_fg = 0;
46
47 354753 av_refstruct_unref(&frame->pps);
48 354753 av_refstruct_unref(&frame->tab_mvf);
49
50 354753 av_refstruct_unref(&frame->rpl);
51 354753 frame->nb_rpl_elems = 0;
52 354753 av_refstruct_unref(&frame->rpl_tab);
53 354753 frame->refPicList = NULL;
54 }
55 416058 }
56
57 5163852 const RefPicList *ff_hevc_get_ref_list(const HEVCFrame *ref, int x0, int y0)
58 {
59 5163852 const HEVCSPS *sps = ref->pps->sps;
60 5163852 int x_cb = x0 >> sps->log2_ctb_size;
61 5163852 int y_cb = y0 >> sps->log2_ctb_size;
62 5163852 int pic_width_cb = sps->ctb_width;
63 5163852 int ctb_addr_ts = ref->pps->ctb_addr_rs_to_ts[y_cb * pic_width_cb + x_cb];
64 5163852 return &ref->rpl_tab[ctb_addr_ts]->refPicList[0];
65 }
66
67 535 void ff_hevc_clear_refs(HEVCLayerContext *l)
68 {
69 int i;
70
2/2
✓ Branch 0 taken 17120 times.
✓ Branch 1 taken 535 times.
17655 for (i = 0; i < FF_ARRAY_ELEMS(l->DPB); i++)
71 17120 ff_hevc_unref_frame(&l->DPB[i],
72 HEVC_FRAME_FLAG_SHORT_REF |
73 HEVC_FRAME_FLAG_LONG_REF);
74 535 }
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 10859 static HEVCFrame *alloc_frame(HEVCContext *s, HEVCLayerContext *l)
111 {
112 10859 const HEVCVPS *vps = l->sps->vps;
113 10859 const int view_id = vps->view_id[s->cur_layer];
114 int i, j, ret;
115
1/2
✓ Branch 0 taken 38046 times.
✗ Branch 1 not taken.
38046 for (i = 0; i < FF_ARRAY_ELEMS(l->DPB); i++) {
116 38046 HEVCFrame *frame = &l->DPB[i];
117
2/2
✓ Branch 0 taken 27187 times.
✓ Branch 1 taken 10859 times.
38046 if (frame->f)
118 27187 continue;
119
120 10859 ret = ff_progress_frame_alloc(s->avctx, &frame->tf);
121
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10859 times.
10859 if (ret < 0)
122 10859 return NULL;
123
124 // Add LCEVC SEI metadata here, as it's needed in get_buffer()
125
2/2
✓ Branch 0 taken 30 times.
✓ Branch 1 taken 10829 times.
10859 if (s->sei.common.itut_t35.lcevc) {
126 30 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 30 times.
30 if (ret < 0)
129 ✗ goto fail;
130 }
131
132 // add view ID side data if it's nontrivial
133
5/6
✓ Branch 1 taken 10759 times.
✓ Branch 2 taken 100 times.
✓ Branch 3 taken 10079 times.
✓ Branch 4 taken 680 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 10079 times.
10859 if (!ff_hevc_is_alpha_video(s) && (vps->nb_layers > 1 || view_id)) {
134 680 HEVCSEITDRDI *tdrdi = &s->sei.tdrdi;
135 680 AVFrameSideData *sd = av_frame_side_data_new(&frame->f->side_data,
136 680 &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 680 times.
680 if (!sd)
140 ✗ goto fail;
141 680 *(int*)sd->data = view_id;
142
143
2/2
✓ Branch 0 taken 22 times.
✓ Branch 1 taken 658 times.
680 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 10859 ret = ff_thread_get_buffer(s->avctx, frame->f, AV_GET_BUFFER_FLAG_REF);
161
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10859 times.
10859 if (ret < 0)
162 ✗ goto fail;
163
164 size_t rpl_bytes;
165
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 10859 times.
10859 if (av_size_mult(s->pkt.nb_nals, sizeof(*frame->rpl), &rpl_bytes) < 0)
166 ✗ goto fail;
167 10859 frame->rpl = av_refstruct_allocz(rpl_bytes);
168
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10859 times.
10859 if (!frame->rpl)
169 ✗ goto fail;
170 10859 frame->nb_rpl_elems = s->pkt.nb_nals;
171
172 10859 frame->tab_mvf = av_refstruct_pool_get(l->tab_mvf_pool);
173
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10859 times.
10859 if (!frame->tab_mvf)
174 ✗ goto fail;
175
176 10859 frame->rpl_tab = av_refstruct_pool_get(l->rpl_tab_pool);
177
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10859 times.
10859 if (!frame->rpl_tab)
178 ✗ goto fail;
179 10859 frame->ctb_count = l->sps->ctb_width * l->sps->ctb_height;
180
2/2
✓ Branch 0 taken 2147225 times.
✓ Branch 1 taken 10859 times.
2158084 for (j = 0; j < frame->ctb_count; j++)
181 2147225 frame->rpl_tab[j] = frame->rpl;
182
183
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 10843 times.
10859 if (s->sei.picture_timing.picture_struct == AV_PICTURE_STRUCTURE_TOP_FIELD)
184 16 frame->f->flags |= AV_FRAME_FLAG_TOP_FIELD_FIRST;
185
2/2
✓ Branch 0 taken 10843 times.
✓ Branch 1 taken 16 times.
10859 if ((s->sei.picture_timing.picture_struct == AV_PICTURE_STRUCTURE_TOP_FIELD) ||
186
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 10839 times.
10843 (s->sei.picture_timing.picture_struct == AV_PICTURE_STRUCTURE_BOTTOM_FIELD))
187 20 frame->f->flags |= AV_FRAME_FLAG_INTERLACED;
188
189 10859 ret = ff_hwaccel_frame_priv_alloc(s->avctx, &frame->hwaccel_picture_private);
190
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10859 times.
10859 if (ret < 0)
191 ✗ goto fail;
192
193 10859 frame->pps = av_refstruct_ref_c(s->pps);
194
4/4
✓ Branch 0 taken 390 times.
✓ Branch 1 taken 10469 times.
✓ Branch 3 taken 50 times.
✓ Branch 4 taken 340 times.
10859 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 10859 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 10804 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 345728 times.
✓ Branch 1 taken 10804 times.
356532 for (i = 0; i < FF_ARRAY_ELEMS(l->DPB); i++) {
219 345728 HEVCFrame *frame = &l->DPB[i];
220
221
3/4
✓ Branch 0 taken 49568 times.
✓ Branch 1 taken 296160 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 49568 times.
345728 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 10804 ref = alloc_frame(s, l);
229
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10804 times.
10804 if (!ref)
230 ✗ return AVERROR(ENOMEM);
231
232 10804 s->cur_frame = ref;
233 10804 l->cur_frame = ref;
234 10804 s->collocated_ref = NULL;
235
236
3/4
✓ Branch 0 taken 390 times.
✓ Branch 1 taken 10414 times.
✓ Branch 2 taken 390 times.
✗ Branch 3 not taken.
10804 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 581 times.
✓ Branch 2 taken 20 times.
✓ Branch 3 taken 10203 times.
10804 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 581 times.
✓ Branch 1 taken 10223 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
21608 !(s->avctx->flags & AV_CODEC_FLAG_OUTPUT_CORRUPT) &&
242 ✗ !(s->avctx->flags2 & AV_CODEC_FLAG2_SHOW_ALL);
243
3/4
✓ Branch 0 taken 10798 times.
✓ Branch 1 taken 6 times.
✓ Branch 2 taken 10798 times.
✗ Branch 3 not taken.
10804 if (s->sh.pic_output_flag && !no_output)
244 10798 ref->flags = HEVC_FRAME_FLAG_OUTPUT | HEVC_FRAME_FLAG_SHORT_REF;
245 else
246 6 ref->flags = HEVC_FRAME_FLAG_SHORT_REF;
247
248 10804 ref->poc = poc;
249 10804 ref->f->crop_left = l->sps->output_window.left_offset;
250 10804 ref->f->crop_right = l->sps->output_window.right_offset;
251 10804 ref->f->crop_top = l->sps->output_window.top_offset;
252 10804 ref->f->crop_bottom = l->sps->output_window.bottom_offset;
253
254 10804 return 0;
255 }
256
257 10804 static void unref_missing_refs(HEVCLayerContext *l)
258 {
259
2/2
✓ Branch 0 taken 345728 times.
✓ Branch 1 taken 10804 times.
356532 for (int i = 0; i < FF_ARRAY_ELEMS(l->DPB); i++) {
260 345728 HEVCFrame *frame = &l->DPB[i];
261
2/2
✓ Branch 0 taken 47 times.
✓ Branch 1 taken 345681 times.
345728 if (frame->flags & HEVC_FRAME_FLAG_UNAVAILABLE) {
262 47 ff_hevc_unref_frame(frame, ~0);
263 }
264 }
265 10804 }
266
267 11472 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 10795 while (1) {
272 22267 int nb_dpb[HEVC_VPS_MAX_LAYERS] = { 0 };
273 22267 int nb_output = 0;
274 22267 int min_poc = INT_MAX;
275 22267 int min_layer = -1;
276 22267 int min_idx, ret = 0;
277
278
2/2
✓ Branch 0 taken 44534 times.
✓ Branch 1 taken 22267 times.
66801 for (int layer = 0; layer < FF_ARRAY_ELEMS(s->layers); layer++) {
279 44534 HEVCLayerContext *l = &s->layers[layer];
280
281
2/2
✓ Branch 0 taken 20928 times.
✓ Branch 1 taken 23606 times.
44534 if (!(layers_active_decode & (1 << layer)))
282 20928 continue;
283
284
2/2
✓ Branch 0 taken 755392 times.
✓ Branch 1 taken 23606 times.
778998 for (int i = 0; i < FF_ARRAY_ELEMS(l->DPB); i++) {
285 755392 HEVCFrame *frame = &l->DPB[i];
286
2/2
✓ Branch 0 taken 58519 times.
✓ Branch 1 taken 696873 times.
755392 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 56099 times.
58519 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 56501 nb_output++;
292
4/4
✓ Branch 0 taken 40079 times.
✓ Branch 1 taken 18440 times.
✓ Branch 2 taken 13283 times.
✓ Branch 3 taken 26796 times.
58519 if (min_layer < 0 || frame->poc < min_poc) {
293 31723 min_poc = frame->poc;
294 31723 min_idx = i;
295 31723 min_layer = layer;
296 }
297 }
298 755392 nb_dpb[layer] += !!frame->flags;
299 }
300 }
301
302
4/4
✓ Branch 0 taken 11927 times.
✓ Branch 1 taken 10340 times.
✓ Branch 2 taken 8100 times.
✓ Branch 3 taken 3827 times.
22267 if (nb_output > max_output ||
303 8100 (nb_output &&
304
4/4
✓ Branch 0 taken 7657 times.
✓ Branch 1 taken 443 times.
✓ Branch 2 taken 12 times.
✓ Branch 3 taken 7645 times.
8100 (nb_dpb[0] > max_dpb || nb_dpb[1] > max_dpb))) {
305 10795 HEVCFrame *frame = &s->layers[min_layer].DPB[min_idx];
306
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10795 times.
10795 AVFrame *f = frame->needs_fg ? frame->frame_grain : frame->f;
307
4/4
✓ Branch 0 taken 10763 times.
✓ Branch 1 taken 32 times.
✓ Branch 2 taken 10650 times.
✓ Branch 3 taken 113 times.
10795 int output = !discard && (layers_active_output & (1 << min_layer));
308
309
2/2
✓ Branch 0 taken 10650 times.
✓ Branch 1 taken 145 times.
10795 if (output) {
310
2/2
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 10636 times.
10650 if (frame->flags & HEVC_FRAME_FLAG_CORRUPT)
311 14 f->flags |= AV_FRAME_FLAG_CORRUPT;
312 10650 f->pkt_dts = s->pkt_dts;
313 10650 ret = av_container_fifo_write(s->output_fifo, f, AV_CONTAINER_FIFO_FLAG_REF);
314 }
315 10795 ff_hevc_unref_frame(frame, HEVC_FRAME_FLAG_OUTPUT);
316
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10795 times.
10795 if (ret < 0)
317 11472 return ret;
318
319
2/2
✓ Branch 0 taken 10650 times.
✓ Branch 1 taken 145 times.
10795 av_log(s->avctx, AV_LOG_DEBUG, "%s frame with POC %d/%d.\n",
320 output ? "Output" : "Discarded", min_layer, frame->poc);
321 10795 continue;
322 }
323 11472 return 0;
324 }
325 }
326
327 21281 static int init_slice_rpl(HEVCContext *s)
328 {
329 21281 HEVCFrame *frame = s->cur_frame;
330 21281 int ctb_count = frame->ctb_count;
331 21281 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 21281 times.
21281 if (s->slice_idx >= frame->nb_rpl_elems)
335 ✗ return AVERROR_INVALIDDATA;
336
337
2/2
✓ Branch 0 taken 3222713 times.
✓ Branch 1 taken 21281 times.
3243994 for (i = ctb_addr_ts; i < ctb_count; i++)
338 3222713 frame->rpl_tab[i] = frame->rpl + s->slice_idx;
339
340 21281 frame->refPicList = (RefPicList *)frame->rpl_tab[ctb_addr_ts];
341
342 21281 return 0;
343 }
344
345 21281 int ff_hevc_slice_rpl(HEVCContext *s)
346 {
347 21281 SliceHeader *sh = &s->sh;
348
349
2/2
✓ Branch 0 taken 15407 times.
✓ Branch 1 taken 5874 times.
21281 uint8_t nb_list = sh->slice_type == HEVC_SLICE_B ? 2 : 1;
350 uint8_t list_idx;
351 int i, j, ret;
352
353 21281 ret = init_slice_rpl(s);
354
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 21281 times.
21281 if (ret < 0)
355 ✗ return ret;
356
357 21281 if (!(s->rps[ST_CURR_BEF].nb_refs + s->rps[ST_CURR_AFT].nb_refs +
358 21281 s->rps[LT_CURR].nb_refs +
359
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 21281 times.
21281 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 36688 times.
✓ Branch 1 taken 21281 times.
57969 for (list_idx = 0; list_idx < nb_list; list_idx++) {
366 36688 RefPicList rpl_tmp = { { 0 } };
367 36688 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 110064 int cand_lists[] = { list_idx ? ST_CURR_AFT : ST_CURR_BEF,
373
2/2
✓ Branch 0 taken 15407 times.
✓ Branch 1 taken 21281 times.
36688 list_idx ? INTER_LAYER1 : INTER_LAYER0,
374 36688 list_idx ? ST_CURR_BEF : ST_CURR_AFT,
375 LT_CURR,
376
2/2
✓ Branch 0 taken 15407 times.
✓ Branch 1 taken 21281 times.
36688 list_idx ? INTER_LAYER0 : INTER_LAYER1
377 };
378
379 /* concatenate the candidate lists for the current frame */
380
2/2
✓ Branch 0 taken 36706 times.
✓ Branch 1 taken 36688 times.
73394 while (rpl_tmp.nb_refs < sh->nb_refs[list_idx]) {
381
2/2
✓ Branch 0 taken 183530 times.
✓ Branch 1 taken 36706 times.
220236 for (i = 0; i < FF_ARRAY_ELEMS(cand_lists); i++) {
382 183530 RefPicList *rps = &s->rps[cand_lists[i]];
383
3/4
✓ Branch 0 taken 129048 times.
✓ Branch 1 taken 183530 times.
✓ Branch 2 taken 129048 times.
✗ Branch 3 not taken.
312578 for (j = 0; j < rps->nb_refs && rpl_tmp.nb_refs < HEVC_MAX_REFS; j++) {
384 129048 rpl_tmp.list[rpl_tmp.nb_refs] = rps->list[j];
385 129048 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 385738 rpl_tmp.isLongTerm[rpl_tmp.nb_refs] = cand_lists[i] == LT_CURR ||
389
4/4
✓ Branch 0 taken 127642 times.
✓ Branch 1 taken 1406 times.
✓ Branch 2 taken 127064 times.
✓ Branch 3 taken 578 times.
256112 cand_lists[i] == INTER_LAYER0 ||
390
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 127064 times.
127064 cand_lists[i] == INTER_LAYER1;
391 129048 rpl_tmp.nb_refs++;
392 }
393 }
394 // Construct RefPicList0, RefPicList1 (8-8, 8-10)
395
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 36706 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
36706 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 2085 times.
✓ Branch 1 taken 34603 times.
36688 if (sh->rpl_modification_flag[list_idx]) {
405
2/2
✓ Branch 0 taken 4922 times.
✓ Branch 1 taken 2085 times.
7007 for (i = 0; i < sh->nb_refs[list_idx]; i++) {
406 4922 int idx = sh->list_entry_lx[list_idx][i];
407
408
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4922 times.
4922 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 4922 rpl->list[i] = rpl_tmp.list[idx];
414 4922 rpl->ref[i] = rpl_tmp.ref[idx];
415 4922 rpl->isLongTerm[i] = rpl_tmp.isLongTerm[idx];
416 4922 rpl->nb_refs++;
417 }
418 } else {
419 34603 memcpy(rpl, &rpl_tmp, sizeof(*rpl));
420 34603 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 36688 times.
36688 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 21280 times.
✓ Branch 1 taken 15408 times.
36688 if (sh->collocated_list == list_idx &&
432
1/2
✓ Branch 0 taken 21280 times.
✗ Branch 1 not taken.
21280 sh->collocated_ref_idx < rpl->nb_refs)
433 21280 s->collocated_ref = rpl->ref[sh->collocated_ref_idx];
434 }
435
436 21281 return 0;
437 }
438
439 38830 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 37195 times.
38830 int mask = use_msb ? ~0 : (1 << l->sps->log2_max_poc_lsb) - 1;
443 int i;
444
445
2/2
✓ Branch 0 taken 156238 times.
✓ Branch 1 taken 55 times.
156293 for (i = 0; i < FF_ARRAY_ELEMS(l->DPB); i++) {
446 156238 HEVCFrame *ref = &l->DPB[i];
447
2/2
✓ Branch 0 taken 151473 times.
✓ Branch 1 taken 4765 times.
156238 if (ref->f) {
448
5/6
✓ Branch 0 taken 38775 times.
✓ Branch 1 taken 112698 times.
✓ Branch 2 taken 1634 times.
✓ Branch 3 taken 37141 times.
✓ Branch 4 taken 1634 times.
✗ Branch 5 not taken.
151473 if ((ref->poc & mask) == poc && (use_msb || ref->poc != s->poc))
449 38775 return ref;
450 }
451 }
452
453
7/8
✓ Branch 0 taken 23 times.
✓ Branch 1 taken 32 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.
55 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 55 return NULL;
457 }
458
459 373754 static void mark_ref(HEVCFrame *frame, int flag)
460 {
461 373754 frame->flags &= ~(HEVC_FRAME_FLAG_LONG_REF | HEVC_FRAME_FLAG_SHORT_REF);
462 373754 frame->flags |= flag;
463 373754 }
464
465 55 static HEVCFrame *generate_missing_ref(HEVCContext *s, HEVCLayerContext *l, int poc)
466 {
467 HEVCFrame *frame;
468
469 55 frame = alloc_frame(s, l);
470
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 55 times.
55 if (!frame)
471 ✗ return NULL;
472
473
3/4
✓ Branch 0 taken 55 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 45 times.
✓ Branch 4 taken 10 times.
55 if (!s->avctx->hwaccel && !ff_decode_skip_all_pixels(s->avctx)) {
474
1/2
✓ Branch 0 taken 45 times.
✗ Branch 1 not taken.
45 int nb_planes = l->sps->chroma_format_idc ? 3 : 1;
475
2/2
✓ Branch 0 taken 35 times.
✓ Branch 1 taken 10 times.
45 if (!l->sps->pixel_shift) {
476
2/2
✓ Branch 0 taken 105 times.
✓ Branch 1 taken 35 times.
140 for (int i = 0; i < nb_planes; i++)
477 105 memset(frame->f->data[i], 1 << (l->sps->bit_depth - 1),
478 105 frame->f->linesize[i] * AV_CEIL_RSHIFT(l->sps->height, l->sps->vshift[i]));
479 } else {
480
2/2
✓ Branch 0 taken 30 times.
✓ Branch 1 taken 10 times.
40 for (int i = 0; i < nb_planes; i++)
481
2/2
✓ Branch 0 taken 35712 times.
✓ Branch 1 taken 30 times.
35742 for (int y = 0; y < (l->sps->height >> l->sps->vshift[i]); y++) {
482 35712 uint8_t *dst = frame->f->data[i] + y * frame->f->linesize[i];
483 35712 AV_WN16(dst, 1 << (l->sps->bit_depth - 1));
484 35712 av_memcpy_backptr(dst + 2, 2, 2*(l->sps->width >> l->sps->hshift[i]) - 2);
485 }
486 }
487 }
488
489 55 frame->poc = poc;
490 55 frame->flags = HEVC_FRAME_FLAG_UNAVAILABLE;
491
492
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 55 times.
55 if (s->avctx->active_thread_type == FF_THREAD_FRAME)
493 ✗ ff_progress_frame_report(&frame->tf, INT_MAX);
494
495 55 return frame;
496 }
497
498 /* add a reference with the given poc to the list and mark it as used in DPB */
499 38830 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 38830 RefPicList *list = &rps[list_idx];
504 38830 HEVCFrame *ref = find_ref_idx(s, l, poc, use_msb);
505
506
2/4
✓ Branch 0 taken 38830 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 38830 times.
38830 if (ref == s->cur_frame || list->nb_refs >= HEVC_MAX_REFS)
507 ✗ return AVERROR_INVALIDDATA;
508
509
7/8
✓ Branch 0 taken 738 times.
✓ Branch 1 taken 38092 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 738 times.
✓ Branch 4 taken 36832 times.
✓ Branch 5 taken 1260 times.
✓ Branch 6 taken 35986 times.
✓ Branch 7 taken 846 times.
38830 if (!IS_IRAP(s) && list_idx != ST_FOLL && list_idx != LT_FOLL) {
510
4/4
✓ Branch 0 taken 35979 times.
✓ Branch 1 taken 7 times.
✓ Branch 2 taken 17 times.
✓ Branch 3 taken 35962 times.
35986 int ref_corrupt = !ref || ref->flags & (HEVC_FRAME_FLAG_CORRUPT |
511 HEVC_FRAME_FLAG_UNAVAILABLE);
512
3/4
✓ Branch 0 taken 35966 times.
✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 35966 times.
35986 int recovering = HEVC_IS_RECOVERING(s);
513
514
3/4
✓ Branch 0 taken 24 times.
✓ Branch 1 taken 35962 times.
✓ Branch 2 taken 24 times.
✗ Branch 3 not taken.
35986 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 55 times.
✓ Branch 1 taken 38775 times.
38830 if (!ref) {
524 55 ref = generate_missing_ref(s, l, poc);
525
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 55 times.
55 if (!ref)
526 ✗ return AVERROR(ENOMEM);
527 }
528
529 38830 list->list[list->nb_refs] = ref->poc;
530 38830 list->ref[list->nb_refs] = ref;
531 38830 list->nb_refs++;
532
533 38830 mark_ref(ref, ref_flag);
534 38830 return 0;
535 }
536
537 10804 int ff_hevc_frame_rps(HEVCContext *s, HEVCLayerContext *l)
538 {
539 10804 const ShortTermRPS *short_rps = s->sh.short_term_rps;
540 10804 const LongTermRPS *long_rps = &s->sh.long_term_rps;
541 10804 RefPicList *rps = s->rps;
542 10804 int i, ret = 0;
543
544 10804 unref_missing_refs(l);
545
546 /* clear the reference flags on all frames except the current one */
547
2/2
✓ Branch 0 taken 345728 times.
✓ Branch 1 taken 10804 times.
356532 for (i = 0; i < FF_ARRAY_ELEMS(l->DPB); i++) {
548 345728 HEVCFrame *frame = &l->DPB[i];
549
550
2/2
✓ Branch 0 taken 10804 times.
✓ Branch 1 taken 334924 times.
345728 if (frame == s->cur_frame)
551 10804 continue;
552
553 334924 mark_ref(frame, 0);
554 }
555
556
2/2
✓ Branch 0 taken 75628 times.
✓ Branch 1 taken 10804 times.
86432 for (i = 0; i < NB_RPS_TYPE; i++)
557 75628 rps[i].nb_refs = 0;
558
559
2/2
✓ Branch 0 taken 269 times.
✓ Branch 1 taken 10535 times.
10804 if (!short_rps)
560 269 goto inter_layer;
561
562 /* add the short refs */
563
2/2
✓ Branch 0 taken 36852 times.
✓ Branch 1 taken 10535 times.
47387 for (i = 0; i < short_rps->num_delta_pocs; i++) {
564 36852 int poc = s->poc + short_rps->delta_poc[i];
565 int list;
566
567
2/2
✓ Branch 0 taken 1780 times.
✓ Branch 1 taken 35072 times.
36852 if (!(short_rps->used & (1 << i)))
568 1780 list = ST_FOLL;
569
2/2
✓ Branch 0 taken 25513 times.
✓ Branch 1 taken 9559 times.
35072 else if (i < short_rps->num_negative_pics)
570 25513 list = ST_CURR_BEF;
571 else
572 9559 list = ST_CURR_AFT;
573
574 36852 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 36852 times.
36852 if (ret < 0)
577 ✗ goto fail;
578 }
579
580 /* add the long refs */
581
2/2
✓ Branch 0 taken 1683 times.
✓ Branch 1 taken 10535 times.
12218 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 10535 inter_layer:
592 /* add inter-layer refs */
593
2/2
✓ Branch 0 taken 10509 times.
✓ Branch 1 taken 295 times.
10804 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 10804 fail:
610 /* release any frames that are now unused */
611
2/2
✓ Branch 0 taken 345728 times.
✓ Branch 1 taken 10804 times.
356532 for (i = 0; i < FF_ARRAY_ELEMS(l->DPB); i++)
612 345728 ff_hevc_unref_frame(&l->DPB[i], 0);
613
614 10804 return ret;
615 }
616
617 21365 int ff_hevc_frame_nb_refs(const SliceHeader *sh, const HEVCPPS *pps,
618 unsigned layer_idx)
619 {
620 21365 int ret = 0;
621 int i;
622 21365 const ShortTermRPS *rps = sh->short_term_rps;
623 21365 const LongTermRPS *long_rps = &sh->long_term_rps;
624
625
2/2
✓ Branch 0 taken 21352 times.
✓ Branch 1 taken 13 times.
21365 if (rps) {
626
2/2
✓ Branch 0 taken 50297 times.
✓ Branch 1 taken 21352 times.
71649 for (i = 0; i < rps->num_negative_pics; i++)
627 50297 ret += !!(rps->used & (1 << i));
628
2/2
✓ Branch 0 taken 21469 times.
✓ Branch 1 taken 21352 times.
42821 for (; i < rps->num_delta_pocs; i++)
629 21469 ret += !!(rps->used & (1 << i));
630 }
631
632
2/2
✓ Branch 0 taken 1787 times.
✓ Branch 1 taken 21365 times.
23152 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 20944 times.
21365 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 21365 times.
21365 if (pps->pps_curr_pic_ref_enabled_flag)
641 ✗ ret++;
642
643 21365 return ret;
644 }
645