FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/vvc/refs.c
Date: 2026-11-08 03:48:22
Exec Total Coverage
Lines: 363 424 85.6%
Functions: 26 28 92.9%
Branches: 234 320 73.1%

Line Branch Exec Source
1 /*
2 * VVC reference management
3 *
4 * Copyright (C) 2023 Nuo Mi
5 *
6 * This file is part of FFmpeg.
7 *
8 * FFmpeg is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * FFmpeg is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with FFmpeg; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22
23 #include <stdatomic.h>
24 #include <stdbool.h>
25
26 #include "libavutil/mem.h"
27 #include "libavutil/thread.h"
28 #include "libavutil/refstruct.h"
29 #include "libavcodec/thread.h"
30 #include "libavcodec/decode.h"
31
32 #include "refs.h"
33
34
35 typedef struct FrameProgress {
36 atomic_int progress[VVC_PROGRESS_LAST];
37 VVCProgressListener *listener[VVC_PROGRESS_LAST];
38 AVMutex lock;
39 AVCond cond;
40 uint8_t has_lock;
41 uint8_t has_cond;
42 } FrameProgress;
43
44 56066 void ff_vvc_unref_frame(VVCFrameContext *fc, VVCFrame *frame, int flags)
45 {
46 /* frame->frame can be NULL if context init failed */
47
3/4
✓ Branch 0 taken 56066 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 43378 times.
✓ Branch 3 taken 12688 times.
56066 if (!frame->frame || !frame->frame->buf[0])
48 43378 return;
49
50 12688 frame->flags &= ~flags;
51
2/2
✓ Branch 0 taken 6405 times.
✓ Branch 1 taken 6283 times.
12688 if (!(frame->flags & ~VVC_FRAME_FLAG_CORRUPT))
52 6405 frame->flags = 0;
53
2/2
✓ Branch 0 taken 6405 times.
✓ Branch 1 taken 6283 times.
12688 if (!frame->flags) {
54 6405 av_refstruct_unref(&frame->hwaccel_picture_private);
55 6405 av_frame_unref(frame->frame);
56
57
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6405 times.
6405 if (frame->needs_fg) {
58 ✗ av_frame_unref(frame->frame_grain);
59 ✗ frame->needs_fg = 0;
60 }
61
62 6405 av_refstruct_unref(&frame->sps);
63 6405 av_refstruct_unref(&frame->pps);
64 6405 av_refstruct_unref(&frame->progress);
65
66 6405 av_refstruct_unref(&frame->tab_dmvr_mvf);
67
68 6405 av_refstruct_unref(&frame->rpl);
69 6405 frame->nb_rpl_elems = 0;
70 6405 av_refstruct_unref(&frame->rpl_tab);
71
72 6405 frame->collocated_ref = NULL;
73 }
74 }
75
76 3060629 const RefPicList *ff_vvc_get_ref_list(const VVCFrameContext *fc, const VVCFrame *ref, int x0, int y0)
77 {
78 3060629 const int x_cb = x0 >> fc->ps.sps->ctb_log2_size_y;
79 3060629 const int y_cb = y0 >> fc->ps.sps->ctb_log2_size_y;
80 3060629 const int pic_width_cb = fc->ps.pps->ctb_width;
81 3060629 const int ctb_addr_rs = y_cb * pic_width_cb + x_cb;
82
83 3060629 return (const RefPicList *)ref->rpl_tab[ctb_addr_rs];
84 }
85
86 114 void ff_vvc_clear_refs(VVCFrameContext *fc)
87 {
88
2/2
✓ Branch 0 taken 1938 times.
✓ Branch 1 taken 114 times.
2052 for (int i = 0; i < FF_ARRAY_ELEMS(fc->DPB); i++)
89 1938 ff_vvc_unref_frame(fc, &fc->DPB[i],
90 VVC_FRAME_FLAG_SHORT_REF | VVC_FRAME_FLAG_LONG_REF);
91 114 }
92
93 106 void ff_vvc_flush_dpb(VVCFrameContext *fc)
94 {
95
2/2
✓ Branch 0 taken 1802 times.
✓ Branch 1 taken 106 times.
1908 for (int i = 0; i < FF_ARRAY_ELEMS(fc->DPB); i++)
96 1802 ff_vvc_unref_frame(fc, &fc->DPB[i], ~0);
97 106 }
98
99 1153 static void free_progress(AVRefStructOpaque unused, void *obj)
100 {
101 1153 FrameProgress *p = (FrameProgress *)obj;
102
103
1/2
✓ Branch 0 taken 1153 times.
✗ Branch 1 not taken.
1153 if (p->has_cond)
104 1153 ff_cond_destroy(&p->cond);
105
1/2
✓ Branch 0 taken 1153 times.
✗ Branch 1 not taken.
1153 if (p->has_lock)
106 1153 ff_mutex_destroy(&p->lock);
107 1153 }
108
109 1153 static FrameProgress *alloc_progress(void)
110 {
111 1153 FrameProgress *p = av_refstruct_alloc_ext(sizeof(*p), 0, NULL, free_progress);
112
113
1/2
✓ Branch 0 taken 1153 times.
✗ Branch 1 not taken.
1153 if (p) {
114 1153 p->has_lock = !ff_mutex_init(&p->lock, NULL);
115 1153 p->has_cond = !ff_cond_init(&p->cond, NULL);
116
2/4
✓ Branch 0 taken 1153 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1153 times.
1153 if (!p->has_lock || !p->has_cond)
117 ✗ av_refstruct_unref(&p);
118 }
119 1153 return p;
120 }
121
122 1153 static VVCFrame *alloc_frame(VVCContext *s, VVCFrameContext *fc)
123 {
124 1153 const VVCSPS *sps = fc->ps.sps;
125 1153 const VVCPPS *pps = fc->ps.pps;
126
1/2
✓ Branch 0 taken 4591 times.
✗ Branch 1 not taken.
4591 for (int i = 0; i < FF_ARRAY_ELEMS(fc->DPB); i++) {
127 int ret;
128 4591 VVCFrame *frame = &fc->DPB[i];
129 4591 VVCWindow *win = &frame->scaling_win;
130
2/2
✓ Branch 0 taken 3438 times.
✓ Branch 1 taken 1153 times.
4591 if (frame->frame->buf[0])
131 3438 continue;
132
133 // Add LCEVC SEI metadata here, as it's needed in get_buffer()
134
2/2
✓ Branch 0 taken 32 times.
✓ Branch 1 taken 1121 times.
1153 if (fc->sei.common.itut_t35.lcevc) {
135 32 ret = ff_frame_new_side_data_from_buf(s->avctx, frame->frame,
136 AV_FRAME_DATA_LCEVC, &fc->sei.common.itut_t35.lcevc);
137
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 32 times.
32 if (ret < 0)
138 ✗ goto fail;
139 }
140
141 1153 ret = ff_thread_get_buffer(s->avctx, frame->frame, AV_GET_BUFFER_FLAG_REF);
142
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1153 times.
1153 if (ret < 0)
143 1153 return NULL;
144
145 1153 frame->sps = av_refstruct_ref_c(fc->ps.sps);
146 1153 frame->pps = av_refstruct_ref_c(fc->ps.pps);
147
148 size_t rpl_size;
149
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1153 times.
1153 if (av_size_mult(s->current_frame.nb_units, sizeof(RefPicListTab), &rpl_size) < 0)
150 ✗ goto fail;
151 1153 frame->rpl = av_refstruct_allocz(rpl_size);
152
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1153 times.
1153 if (!frame->rpl)
153 ✗ goto fail;
154 1153 frame->nb_rpl_elems = s->current_frame.nb_units;
155
156 1153 frame->tab_dmvr_mvf = av_refstruct_pool_get(fc->tab_dmvr_mvf_pool);
157
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1153 times.
1153 if (!frame->tab_dmvr_mvf)
158 ✗ goto fail;
159
160 1153 frame->rpl_tab = av_refstruct_pool_get(fc->rpl_tab_pool);
161
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1153 times.
1153 if (!frame->rpl_tab)
162 ✗ goto fail;
163 1153 frame->ctb_count = pps->ctb_width * pps->ctb_height;
164
2/2
✓ Branch 0 taken 56052 times.
✓ Branch 1 taken 1153 times.
57205 for (int j = 0; j < frame->ctb_count; j++)
165 56052 frame->rpl_tab[j] = frame->rpl;
166
167 1153 win->left_offset = pps->r->pps_scaling_win_left_offset * (1 << sps->hshift[CHROMA]);
168 1153 win->right_offset = pps->r->pps_scaling_win_right_offset * (1 << sps->hshift[CHROMA]);
169 1153 win->top_offset = pps->r->pps_scaling_win_top_offset * (1 << sps->vshift[CHROMA]);
170 1153 win->bottom_offset = pps->r->pps_scaling_win_bottom_offset * (1 << sps->vshift[CHROMA]);
171 1153 frame->ref_width = pps->r->pps_pic_width_in_luma_samples - win->left_offset - win->right_offset;
172 1153 frame->ref_height = pps->r->pps_pic_height_in_luma_samples - win->bottom_offset - win->top_offset;
173
174
2/2
✓ Branch 0 taken 21 times.
✓ Branch 1 taken 1132 times.
1153 if (fc->sei.frame_field_info.present) {
175
2/2
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 10 times.
21 if (fc->sei.frame_field_info.picture_struct == AV_PICTURE_STRUCTURE_TOP_FIELD)
176 11 frame->frame->flags |= AV_FRAME_FLAG_TOP_FIELD_FIRST;
177
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 11 times.
21 if (fc->sei.frame_field_info.picture_struct == AV_PICTURE_STRUCTURE_TOP_FIELD ||
178
1/2
✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
10 fc->sei.frame_field_info.picture_struct == AV_PICTURE_STRUCTURE_BOTTOM_FIELD)
179 21 frame->frame->flags |= AV_FRAME_FLAG_INTERLACED;
180 }
181
182 1153 frame->progress = alloc_progress();
183
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1153 times.
1153 if (!frame->progress)
184 ✗ goto fail;
185
186 1153 ret = ff_hwaccel_frame_priv_alloc(s->avctx, &frame->hwaccel_picture_private);
187
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1153 times.
1153 if (ret < 0)
188 ✗ goto fail;
189
190 1153 return frame;
191 ✗ fail:
192 ✗ ff_vvc_unref_frame(fc, frame, ~0);
193 ✗ return NULL;
194 }
195 ✗ av_log(s->avctx, AV_LOG_ERROR, "Error allocating frame, DPB full.\n");
196 ✗ return NULL;
197 }
198
199 1136 static void set_pict_type(AVFrame *frame, const VVCContext *s, const VVCFrameContext *fc)
200 {
201 1136 bool has_b = false, has_inter = false;
202
203
6/6
✓ Branch 0 taken 1129 times.
✓ Branch 1 taken 7 times.
✓ Branch 2 taken 1022 times.
✓ Branch 3 taken 107 times.
✓ Branch 4 taken 12 times.
✓ Branch 5 taken 1010 times.
1136 if (IS_IRAP(s)) {
204 126 frame->pict_type = AV_PICTURE_TYPE_I;
205 126 frame->flags |= AV_FRAME_FLAG_KEY;
206 126 return;
207 }
208
209
2/2
✓ Branch 0 taken 1007 times.
✓ Branch 1 taken 3 times.
1010 if (fc->ps.ph.r->ph_inter_slice_allowed_flag) {
210 // At this point, fc->slices is not fully initialized; we need to inspect the CBS directly.
211 1007 const CodedBitstreamFragment *current = &s->current_frame;
212
4/4
✓ Branch 0 taken 2458 times.
✓ Branch 1 taken 109 times.
✓ Branch 2 taken 1560 times.
✓ Branch 3 taken 898 times.
2567 for (int i = 0; i < current->nb_units && !has_b; i++) {
213 1560 const CodedBitstreamUnit *unit = current->units + i;
214
3/4
✓ Branch 0 taken 1560 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1007 times.
✓ Branch 3 taken 553 times.
1560 if (unit->content_ref && unit->type <= VVC_RSV_IRAP_11) {
215 1007 const H266RawSliceHeader *rsh = unit->content_ref;
216 1007 has_inter |= !IS_I(rsh);
217 1007 has_b |= IS_B(rsh);
218 }
219 }
220 }
221
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 1007 times.
1010 if (!has_inter)
222 3 frame->pict_type = AV_PICTURE_TYPE_I;
223
2/2
✓ Branch 0 taken 951 times.
✓ Branch 1 taken 56 times.
1007 else if (has_b)
224 951 frame->pict_type = AV_PICTURE_TYPE_B;
225 else
226 56 frame->pict_type = AV_PICTURE_TYPE_P;
227 }
228
229 1136 int ff_vvc_set_new_ref(VVCContext *s, VVCFrameContext *fc, AVFrame **frame)
230 {
231 1136 const VVCPH *ph= &fc->ps.ph;
232 1136 const int poc = ph->poc;
233 VVCFrame *ref;
234
235 /* check that this POC doesn't already exist */
236
2/2
✓ Branch 0 taken 19312 times.
✓ Branch 1 taken 1136 times.
20448 for (int i = 0; i < FF_ARRAY_ELEMS(fc->DPB); i++) {
237 19312 VVCFrame *frame = &fc->DPB[i];
238
239
4/4
✓ Branch 0 taken 5227 times.
✓ Branch 1 taken 14085 times.
✓ Branch 2 taken 5118 times.
✓ Branch 3 taken 109 times.
19312 if (frame->frame->buf[0] && frame->sequence == s->seq_decode &&
240
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5118 times.
5118 frame->poc == poc) {
241 ✗ av_log(s->avctx, AV_LOG_ERROR, "Duplicate POC in a sequence: %d.\n", poc);
242 ✗ return AVERROR_INVALIDDATA;
243 }
244 }
245
246 1136 ref = alloc_frame(s, fc);
247
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1136 times.
1136 if (!ref)
248 ✗ return AVERROR(ENOMEM);
249
250 1136 set_pict_type(ref->frame, s, fc);
251 1136 *frame = ref->frame;
252 1136 fc->ref = ref;
253
254
5/6
✓ Branch 0 taken 935 times.
✓ Branch 1 taken 201 times.
✓ Branch 2 taken 920 times.
✓ Branch 3 taken 15 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 920 times.
1136 if (s->no_output_before_recovery_flag && (IS_RASL(s) || !GDR_IS_RECOVERED(s)))
255 15 ref->flags = VVC_FRAME_FLAG_SHORT_REF;
256
1/2
✓ Branch 0 taken 1121 times.
✗ Branch 1 not taken.
1121 else if (ph->r->ph_pic_output_flag)
257 1121 ref->flags = VVC_FRAME_FLAG_OUTPUT | VVC_FRAME_FLAG_SHORT_REF;
258
259
2/2
✓ Branch 0 taken 1124 times.
✓ Branch 1 taken 12 times.
1136 if (!ph->r->ph_non_ref_pic_flag)
260 1124 ref->flags |= VVC_FRAME_FLAG_SHORT_REF;
261
262 1136 ref->poc = poc;
263 1136 ref->sequence = s->seq_decode;
264 1136 ref->frame->crop_left = fc->ps.pps->r->pps_conf_win_left_offset << fc->ps.sps->hshift[CHROMA];
265 1136 ref->frame->crop_right = fc->ps.pps->r->pps_conf_win_right_offset << fc->ps.sps->hshift[CHROMA];
266 1136 ref->frame->crop_top = fc->ps.pps->r->pps_conf_win_top_offset << fc->ps.sps->vshift[CHROMA];
267 1136 ref->frame->crop_bottom = fc->ps.pps->r->pps_conf_win_bottom_offset << fc->ps.sps->vshift[CHROMA];
268
269 1136 return 0;
270 }
271
272 1273 int ff_vvc_output_frame(VVCContext *s, VVCFrameContext *fc, AVFrame *out, const int no_output_of_prior_pics_flag, int flush)
273 {
274 1273 const VVCSPS *sps = fc->ps.sps;
275 120 do {
276 1393 int nb_output = 0;
277 1393 int min_poc = INT_MAX;
278 int min_idx, ret;
279
280
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1393 times.
1393 if (no_output_of_prior_pics_flag) {
281 ✗ for (int i = 0; i < FF_ARRAY_ELEMS(fc->DPB); i++) {
282 ✗ VVCFrame *frame = &fc->DPB[i];
283 ✗ if (!(frame->flags & VVC_FRAME_FLAG_BUMPING) && frame->poc != fc->ps.ph.poc &&
284 ✗ frame->sequence == s->seq_output) {
285 ✗ ff_vvc_unref_frame(fc, frame, VVC_FRAME_FLAG_OUTPUT);
286 }
287 }
288 }
289
290
2/2
✓ Branch 0 taken 23681 times.
✓ Branch 1 taken 1393 times.
25074 for (int i = 0; i < FF_ARRAY_ELEMS(fc->DPB); i++) {
291 23681 VVCFrame *frame = &fc->DPB[i];
292
2/2
✓ Branch 0 taken 5217 times.
✓ Branch 1 taken 18464 times.
23681 if ((frame->flags & VVC_FRAME_FLAG_OUTPUT) &&
293
2/2
✓ Branch 0 taken 4947 times.
✓ Branch 1 taken 270 times.
5217 frame->sequence == s->seq_output) {
294 4947 nb_output++;
295
3/4
✓ Branch 0 taken 2409 times.
✓ Branch 1 taken 2538 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 2409 times.
4947 if (frame->poc < min_poc || nb_output == 1) {
296 2538 min_poc = frame->poc;
297 2538 min_idx = i;
298 }
299 }
300 }
301
302 /* wait for more frames before output */
303
5/6
✓ Branch 0 taken 1254 times.
✓ Branch 1 taken 139 times.
✓ Branch 2 taken 1093 times.
✓ Branch 3 taken 161 times.
✓ Branch 4 taken 1093 times.
✗ Branch 5 not taken.
1393 if (!flush && s->seq_output == s->seq_decode && sps &&
304
2/2
✓ Branch 0 taken 167 times.
✓ Branch 1 taken 926 times.
1093 nb_output <= sps->r->sps_dpb_params.dpb_max_num_reorder_pics[sps->r->sps_max_sublayers_minus1])
305 167 return 0;
306
307
2/2
✓ Branch 0 taken 1071 times.
✓ Branch 1 taken 155 times.
1226 if (nb_output) {
308 1071 VVCFrame *frame = &fc->DPB[min_idx];
309
310
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1071 times.
1071 if (frame->flags & VVC_FRAME_FLAG_CORRUPT)
311 ✗ frame->frame->flags |= AV_FRAME_FLAG_CORRUPT;
312
313
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1071 times.
1071 ret = av_frame_ref(out, frame->needs_fg ? frame->frame_grain : frame->frame);
314
315
2/4
✓ Branch 0 taken 1071 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1071 times.
✗ Branch 3 not taken.
1071 if (!ret && !(s->avctx->export_side_data & AV_CODEC_EXPORT_DATA_FILM_GRAIN))
316 1071 av_frame_remove_side_data(out, AV_FRAME_DATA_FILM_GRAIN_PARAMS);
317
318
2/2
✓ Branch 0 taken 716 times.
✓ Branch 1 taken 355 times.
1071 if (frame->flags & VVC_FRAME_FLAG_BUMPING)
319 716 ff_vvc_unref_frame(fc, frame, VVC_FRAME_FLAG_OUTPUT | VVC_FRAME_FLAG_BUMPING);
320 else
321 355 ff_vvc_unref_frame(fc, frame, VVC_FRAME_FLAG_OUTPUT);
322
323
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1071 times.
1071 if (ret < 0)
324 ✗ return ret;
325
326 1071 av_log(s->avctx, AV_LOG_DEBUG,
327 "Output frame with POC %d.\n", frame->poc);
328 1071 return 1;
329 }
330
331
2/2
✓ Branch 0 taken 120 times.
✓ Branch 1 taken 35 times.
155 if (s->seq_output != s->seq_decode)
332 120 s->seq_output = (s->seq_output + 1) & 0xff;
333 else
334 35 break;
335 } while (1);
336 35 return 0;
337 }
338
339 1022 void ff_vvc_bump_frame(VVCContext *s, VVCFrameContext *fc)
340 {
341 1022 const VVCSPS *sps = fc->ps.sps;
342 1022 const int poc = fc->ps.ph.poc;
343 1022 int dpb = 0;
344 1022 int min_poc = INT_MAX;
345
346
2/2
✓ Branch 0 taken 17374 times.
✓ Branch 1 taken 1022 times.
18396 for (int i = 0; i < FF_ARRAY_ELEMS(fc->DPB); i++) {
347 17374 VVCFrame *frame = &fc->DPB[i];
348
2/2
✓ Branch 0 taken 6206 times.
✓ Branch 1 taken 11168 times.
17374 if ((frame->flags) &&
349
2/2
✓ Branch 0 taken 6078 times.
✓ Branch 1 taken 128 times.
6206 frame->sequence == s->seq_output &&
350
2/2
✓ Branch 0 taken 5090 times.
✓ Branch 1 taken 988 times.
6078 frame->poc != poc) {
351 5090 dpb++;
352 }
353 }
354
355
3/4
✓ Branch 0 taken 1022 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 501 times.
✓ Branch 3 taken 521 times.
1022 if (sps && dpb >= sps->r->sps_dpb_params.dpb_max_dec_pic_buffering_minus1[sps->r->sps_max_sublayers_minus1] + 1) {
356
2/2
✓ Branch 0 taken 8517 times.
✓ Branch 1 taken 501 times.
9018 for (int i = 0; i < FF_ARRAY_ELEMS(fc->DPB); i++) {
357 8517 VVCFrame *frame = &fc->DPB[i];
358
2/2
✓ Branch 0 taken 3345 times.
✓ Branch 1 taken 5172 times.
8517 if ((frame->flags) &&
359
1/2
✓ Branch 0 taken 3345 times.
✗ Branch 1 not taken.
3345 frame->sequence == s->seq_output &&
360
2/2
✓ Branch 0 taken 2844 times.
✓ Branch 1 taken 501 times.
3345 frame->poc != poc) {
361
4/4
✓ Branch 0 taken 60 times.
✓ Branch 1 taken 2784 times.
✓ Branch 2 taken 51 times.
✓ Branch 3 taken 9 times.
2844 if (frame->flags == VVC_FRAME_FLAG_OUTPUT && frame->poc < min_poc) {
362 51 min_poc = frame->poc;
363 }
364 }
365 }
366
367
2/2
✓ Branch 0 taken 8517 times.
✓ Branch 1 taken 501 times.
9018 for (int i = 0; i < FF_ARRAY_ELEMS(fc->DPB); i++) {
368 8517 VVCFrame *frame = &fc->DPB[i];
369
2/2
✓ Branch 0 taken 2225 times.
✓ Branch 1 taken 6292 times.
8517 if (frame->flags & VVC_FRAME_FLAG_OUTPUT &&
370
1/2
✓ Branch 0 taken 2225 times.
✗ Branch 1 not taken.
2225 frame->sequence == s->seq_output &&
371
2/2
✓ Branch 0 taken 2021 times.
✓ Branch 1 taken 204 times.
2225 frame->poc <= min_poc) {
372 2021 frame->flags |= VVC_FRAME_FLAG_BUMPING;
373 }
374 }
375
376 501 dpb--;
377 }
378 1022 }
379
380 7628 static VVCFrame *find_ref_idx(VVCContext *s, VVCFrameContext *fc, int poc, uint8_t use_msb)
381 {
382
2/2
✓ Branch 0 taken 395 times.
✓ Branch 1 taken 7233 times.
7628 const unsigned mask = use_msb ? ~0 : fc->ps.sps->max_pic_order_cnt_lsb - 1;
383
384
2/2
✓ Branch 0 taken 27760 times.
✓ Branch 1 taken 17 times.
27777 for (int i = 0; i < FF_ARRAY_ELEMS(fc->DPB); i++) {
385 27760 VVCFrame *ref = &fc->DPB[i];
386
387 // In the specification, the current frame is not considered part of
388 // the DPB at this stage, therefore we treat it as if it's not there.
389
2/2
✓ Branch 0 taken 2369 times.
✓ Branch 1 taken 25391 times.
27760 if (ref == fc->ref)
390 2369 continue;
391
392
4/4
✓ Branch 0 taken 22644 times.
✓ Branch 1 taken 2747 times.
✓ Branch 2 taken 21716 times.
✓ Branch 3 taken 928 times.
25391 if (ref->frame->buf[0] && ref->sequence == s->seq_decode) {
393
2/2
✓ Branch 0 taken 7611 times.
✓ Branch 1 taken 14105 times.
21716 if ((ref->poc & mask) == poc)
394 7611 return ref;
395 }
396 }
397 17 return NULL;
398 }
399
400 25804 static void mark_ref(VVCFrame *frame, int flag)
401 {
402 25804 frame->flags &= ~(VVC_FRAME_FLAG_LONG_REF | VVC_FRAME_FLAG_SHORT_REF);
403 25804 frame->flags |= flag;
404 25804 }
405
406 17 static VVCFrame *generate_missing_ref(VVCContext *s, VVCFrameContext *fc, int poc)
407 {
408 17 const VVCSPS *sps = fc->ps.sps;
409 17 const VVCPPS *pps = fc->ps.pps;
410 VVCFrame *frame;
411
412 17 frame = alloc_frame(s, fc);
413
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 17 times.
17 if (!frame)
414 ✗ return NULL;
415
416
1/2
✓ Branch 0 taken 17 times.
✗ Branch 1 not taken.
17 if (!s->avctx->hwaccel) {
417
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 17 times.
17 if (!sps->pixel_shift) {
418 ✗ for (int i = 0; frame->frame->buf[i]; i++)
419 ✗ memset(frame->frame->buf[i]->data, 1 << (sps->bit_depth - 1),
420 ✗ frame->frame->buf[i]->size);
421 } else {
422
2/2
✓ Branch 0 taken 51 times.
✓ Branch 1 taken 17 times.
68 for (int i = 0; frame->frame->data[i]; i++)
423
2/2
✓ Branch 0 taken 10688 times.
✓ Branch 1 taken 51 times.
10739 for (int y = 0; y < (pps->height >> sps->vshift[i]); y++) {
424 10688 uint8_t *dst = frame->frame->data[i] + y * frame->frame->linesize[i];
425 10688 AV_WN16(dst, 1 << (sps->bit_depth - 1));
426 10688 av_memcpy_backptr(dst + 2, 2, 2*(pps->width >> sps->hshift[i]) - 2);
427 }
428 }
429 }
430
431 17 frame->poc = poc;
432 17 frame->sequence = s->seq_decode;
433 17 frame->flags = VVC_FRAME_FLAG_CORRUPT;
434
435 17 ff_vvc_report_frame_finished(frame);
436
437 17 return frame;
438 }
439
440 #define CHECK_MAX(d) (frame->ref_##d * frame->sps->r->sps_pic_##d##_max_in_luma_samples >= ref->ref_##d * (frame->pps->r->pps_pic_##d##_in_luma_samples - max))
441 #define CHECK_SAMPLES(d) (frame->pps->r->pps_pic_##d##_in_luma_samples == ref->pps->r->pps_pic_##d##_in_luma_samples)
442 7628 static int check_candidate_ref(const VVCFrame *frame, const VVCRefPic *refp)
443 {
444 7628 const VVCFrame *ref = refp->ref;
445
446
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 7620 times.
7628 if (refp->is_scaled) {
447 8 const int max = FFMAX(8, frame->sps->min_cb_size_y);
448 16 return frame->ref_width * 2 >= ref->ref_width &&
449
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
8 frame->ref_height * 2 >= ref->ref_height &&
450
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
8 frame->ref_width <= ref->ref_width * 8 &&
451
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
8 frame->ref_height <= ref->ref_height * 8 &&
452
3/6
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 8 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 8 times.
✗ Branch 5 not taken.
16 CHECK_MAX(width) && CHECK_MAX(height);
453 }
454
2/4
✓ Branch 0 taken 7620 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 7620 times.
✗ Branch 3 not taken.
7620 return CHECK_SAMPLES(width) && CHECK_SAMPLES(height);
455 }
456
457 #define RPR_SCALE(f) (((ref->f << 14) + (fc->ref->f >> 1)) / fc->ref->f)
458 /* add a reference with the given poc to the list and mark it as used in DPB */
459 7628 static int add_candidate_ref(VVCContext *s, VVCFrameContext *fc, RefPicList *list,
460 int poc, int ref_flag, uint8_t use_msb)
461 {
462 7628 VVCRefPic *refp = &list->refs[list->nb_refs];
463 VVCFrame *ref;
464
465
4/6
✓ Branch 0 taken 7233 times.
✓ Branch 1 taken 395 times.
✓ Branch 2 taken 7233 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 7628 times.
7628 if (use_msb && poc == fc->ref->poc || list->nb_refs >= VVC_MAX_REF_ENTRIES)
466 ✗ return AVERROR_INVALIDDATA;
467
468 7628 ref = find_ref_idx(s, fc, poc, use_msb);
469
470
6/8
✓ Branch 0 taken 7628 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 7614 times.
✓ Branch 3 taken 14 times.
✓ Branch 4 taken 7568 times.
✓ Branch 5 taken 46 times.
✓ Branch 6 taken 7568 times.
✗ Branch 7 not taken.
7628 if (!IS_CVSS(s)) {
471
3/4
✓ Branch 0 taken 7568 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 19 times.
✓ Branch 3 taken 7549 times.
7568 const bool ref_corrupt = !ref || (ref->flags & VVC_FRAME_FLAG_CORRUPT);
472
2/2
✓ Branch 0 taken 6705 times.
✓ Branch 1 taken 863 times.
14273 const bool recovering = s->no_output_before_recovery_flag &&
473
3/4
✓ Branch 0 taken 6633 times.
✓ Branch 1 taken 72 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 6633 times.
6705 (IS_RASL(s) || !GDR_IS_RECOVERED(s));
474
475
3/4
✓ Branch 0 taken 19 times.
✓ Branch 1 taken 7549 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 19 times.
7568 if (ref_corrupt && !recovering) {
476 ✗ if (!(s->avctx->flags & AV_CODEC_FLAG_OUTPUT_CORRUPT) &&
477 ✗ !(s->avctx->flags2 & AV_CODEC_FLAG2_SHOW_ALL))
478 ✗ return AVERROR_INVALIDDATA;
479
480 ✗ fc->ref->flags |= VVC_FRAME_FLAG_CORRUPT;
481 }
482 }
483
484
2/2
✓ Branch 0 taken 17 times.
✓ Branch 1 taken 7611 times.
7628 if (!ref) {
485 17 ref = generate_missing_ref(s, fc, poc);
486
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 17 times.
17 if (!ref)
487 ✗ return AVERROR(ENOMEM);
488 }
489
490 7628 refp->poc = poc;
491 7628 refp->ref = ref;
492 7628 refp->is_lt = ref_flag & VVC_FRAME_FLAG_LONG_REF;
493 22884 refp->is_scaled = ref->sps->r->sps_num_subpics_minus1 != fc->ref->sps->r->sps_num_subpics_minus1||
494
1/2
✓ Branch 0 taken 7628 times.
✗ Branch 1 not taken.
7628 memcmp(&ref->scaling_win, &fc->ref->scaling_win, sizeof(ref->scaling_win)) ||
495
3/4
✓ Branch 0 taken 7628 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 7620 times.
✓ Branch 3 taken 8 times.
22876 ref->pps->r->pps_pic_width_in_luma_samples != fc->ref->pps->r->pps_pic_width_in_luma_samples ||
496
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7620 times.
7620 ref->pps->r->pps_pic_height_in_luma_samples != fc->ref->pps->r->pps_pic_height_in_luma_samples;
497
498
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 7628 times.
7628 if (!check_candidate_ref(fc->ref, refp))
499 ✗ return AVERROR_INVALIDDATA;
500
501
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 7620 times.
7628 if (refp->is_scaled) {
502 8 refp->scale[0] = RPR_SCALE(ref_width);
503 8 refp->scale[1] = RPR_SCALE(ref_height);
504 }
505 7628 list->nb_refs++;
506
507 7628 mark_ref(ref, ref_flag);
508 7628 return 0;
509 }
510
511 1735 static int init_slice_rpl(const VVCFrameContext *fc, SliceContext *sc)
512 {
513 1735 VVCFrame *frame = fc->ref;
514 1735 const VVCSH *sh = &sc->sh;
515
516
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1735 times.
1735 if (sc->slice_idx >= frame->nb_rpl_elems)
517 ✗ return AVERROR_INVALIDDATA;
518
519
2/2
✓ Branch 0 taken 54159 times.
✓ Branch 1 taken 1735 times.
55894 for (int i = 0; i < sh->num_ctus_in_curr_slice; i++) {
520 54159 const int rs = sh->ctb_addr_in_curr_slice[i];
521 54159 frame->rpl_tab[rs] = frame->rpl + sc->slice_idx;
522 }
523
524 1735 sc->rpl = frame->rpl_tab[sh->ctb_addr_in_curr_slice[0]]->refPicList;
525
526 1735 return 0;
527 }
528
529 7219 static int delta_poc_st(const H266RefPicListStruct *rpls,
530 const int lx, const int i, const VVCSPS *sps)
531 {
532 7219 int abs_delta_poc_st = rpls->abs_delta_poc_st[i];
533
4/4
✓ Branch 0 taken 6700 times.
✓ Branch 1 taken 519 times.
✓ Branch 2 taken 72 times.
✓ Branch 3 taken 447 times.
7219 if (!((sps->r->sps_weighted_pred_flag ||
534
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6700 times.
6700 sps->r->sps_weighted_bipred_flag) && i != 0))
535 6772 abs_delta_poc_st++;
536 7219 return (1 - 2 * rpls->strp_entry_sign_flag[i]) * abs_delta_poc_st;
537 }
538
539 409 static int poc_lt(int *prev_delta_poc_msb, const int poc, const H266RefPicLists *ref_lists,
540 const int lx, const int j, const int max_poc_lsb)
541 {
542 409 const H266RefPicListStruct *rpls = ref_lists->rpl_ref_list + lx;
543
1/2
✓ Branch 0 taken 409 times.
✗ Branch 1 not taken.
409 int lt_poc = rpls->ltrp_in_header_flag ? ref_lists->poc_lsb_lt[lx][j] : rpls->rpls_poc_lsb_lt[j];
544
545
2/2
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 395 times.
409 if (ref_lists->delta_poc_msb_cycle_present_flag[lx][j]) {
546 14 const uint32_t delta = ref_lists->delta_poc_msb_cycle_lt[lx][j] + *prev_delta_poc_msb;
547 14 lt_poc += poc - delta * max_poc_lsb - (poc & (max_poc_lsb - 1));
548 14 *prev_delta_poc_msb = delta;
549 }
550 409 return lt_poc;
551 }
552
553 1735 int ff_vvc_slice_rpl(VVCContext *s, VVCFrameContext *fc, SliceContext *sc)
554 {
555 1735 const VVCSPS *sps = fc->ps.sps;
556 1735 const H266RawPPS *pps = fc->ps.pps->r;
557 1735 const VVCPH *ph = &fc->ps.ph;
558 1735 const H266RawSliceHeader *rsh = sc->sh.r;
559 1735 const int max_poc_lsb = sps->max_pic_order_cnt_lsb;
560 1735 const H266RefPicLists *ref_lists =
561
2/2
✓ Branch 0 taken 253 times.
✓ Branch 1 taken 1482 times.
1735 pps->pps_rpl_info_in_ph_flag ? &ph->r->ph_ref_pic_lists : &rsh->sh_ref_pic_lists;
562 1735 int ret = 0;
563
564 1735 ret = init_slice_rpl(fc, sc);
565
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1735 times.
1735 if (ret < 0)
566 ✗ return ret;
567
568
2/2
✓ Branch 0 taken 3470 times.
✓ Branch 1 taken 1735 times.
5205 for (int lx = L0; lx <= L1; lx++) {
569 3470 const H266RefPicListStruct *rpls = ref_lists->rpl_ref_list + lx;
570 3470 RefPicList *rpl = sc->rpl + lx;
571 3470 int poc_base = ph->poc;
572 3470 int prev_delta_poc_msb = 0;
573
574 3470 rpl->nb_refs = 0;
575
2/2
✓ Branch 0 taken 7628 times.
✓ Branch 1 taken 3470 times.
11098 for (int i = 0, j = 0; i < rpls->num_ref_entries; i++) {
576 int poc;
577
1/2
✓ Branch 0 taken 7628 times.
✗ Branch 1 not taken.
7628 if (!rpls->inter_layer_ref_pic_flag[i]) {
578 7628 int use_msb = 1;
579 int ref_flag;
580
2/2
✓ Branch 0 taken 7219 times.
✓ Branch 1 taken 409 times.
7628 if (rpls->st_ref_pic_flag[i]) {
581 7219 poc = poc_base + delta_poc_st(rpls, lx, i, sps);
582 7219 poc_base = poc;
583 7219 ref_flag = VVC_FRAME_FLAG_SHORT_REF;
584 } else {
585 409 use_msb = ref_lists->delta_poc_msb_cycle_present_flag[lx][j];
586 409 poc = poc_lt(&prev_delta_poc_msb, ph->poc, ref_lists, lx, j, max_poc_lsb);
587 409 ref_flag = VVC_FRAME_FLAG_LONG_REF;
588 409 j++;
589 }
590 7628 ret = add_candidate_ref(s, fc, rpl, poc, ref_flag, use_msb);
591
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7628 times.
7628 if (ret < 0)
592 ✗ return ret;
593 } else {
594 // OPI_B_3.bit and VPS_A_3.bit should cover this
595 ✗ avpriv_report_missing_feature(fc->log_ctx, "Inter layer ref");
596 ✗ ret = AVERROR_PATCHWELCOME;
597 ✗ return ret;
598 }
599 }
600
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3470 times.
3470 if (rsh->num_ref_idx_active[lx] > rpl->nb_refs)
601 ✗ return AVERROR_INVALIDDATA;
602
2/2
✓ Branch 0 taken 3184 times.
✓ Branch 1 taken 286 times.
3470 if (ph->r->ph_temporal_mvp_enabled_flag &&
603
2/2
✓ Branch 0 taken 1592 times.
✓ Branch 1 taken 1592 times.
3184 (!rsh->sh_collocated_from_l0_flag) == lx &&
604
1/2
✓ Branch 0 taken 1592 times.
✗ Branch 1 not taken.
1592 rsh->sh_collocated_ref_idx < rpl->nb_refs) {
605 1592 const VVCRefPic *refp = rpl->refs + rsh->sh_collocated_ref_idx;
606
2/4
✓ Branch 0 taken 1592 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1592 times.
1592 if (refp->is_scaled || refp->ref->sps->ctb_log2_size_y != sps->ctb_log2_size_y)
607 ✗ return AVERROR_INVALIDDATA;
608 1592 fc->ref->collocated_ref = refp->ref;
609 }
610 }
611 1735 return 0;
612 }
613
614 1136 int ff_vvc_frame_rpl(VVCContext *s, VVCFrameContext *fc, SliceContext *sc)
615 {
616 1136 int ret = 0;
617
618 /* clear the reference flags on all frames except the current one */
619
2/2
✓ Branch 0 taken 19312 times.
✓ Branch 1 taken 1136 times.
20448 for (int i = 0; i < FF_ARRAY_ELEMS(fc->DPB); i++) {
620 19312 VVCFrame *frame = &fc->DPB[i];
621
622
2/2
✓ Branch 0 taken 1136 times.
✓ Branch 1 taken 18176 times.
19312 if (frame == fc->ref)
623 1136 continue;
624
625 18176 mark_ref(frame, 0);
626 }
627
628
1/2
✓ Branch 1 taken 1136 times.
✗ Branch 2 not taken.
1136 if ((ret = ff_vvc_slice_rpl(s, fc, sc)) < 0)
629 ✗ goto fail;
630
631 1136 fail:
632 /* release any frames that are now unused */
633
2/2
✓ Branch 0 taken 19312 times.
✓ Branch 1 taken 1136 times.
20448 for (int i = 0; i < FF_ARRAY_ELEMS(fc->DPB); i++)
634 19312 ff_vvc_unref_frame(fc, &fc->DPB[i], 0);
635 1136 return ret;
636 }
637
638 1153 void ff_vvc_report_frame_finished(VVCFrame *frame)
639 {
640 1153 ff_vvc_report_progress(frame, VVC_PROGRESS_MV, INT_MAX);
641 1153 ff_vvc_report_progress(frame, VVC_PROGRESS_PIXEL, INT_MAX);
642 1153 }
643
644 103257 static int is_progress_done(const FrameProgress *p, const VVCProgressListener *l)
645 {
646 103257 return p->progress[l->vp] > l->y;
647 }
648
649 ✗ static void add_listener(VVCProgressListener **prev, VVCProgressListener *l)
650 {
651 ✗ l->next = *prev;
652 ✗ *prev = l;
653 ✗ }
654
655 ✗ static VVCProgressListener* remove_listener(VVCProgressListener **prev, VVCProgressListener *l)
656 {
657 ✗ *prev = l->next;
658 ✗ l->next = NULL;
659 ✗ return l;
660 }
661
662 10217 static VVCProgressListener* get_done_listener(FrameProgress *p, const VVCProgress vp)
663 {
664 10217 VVCProgressListener *list = NULL;
665 10217 VVCProgressListener **prev = &p->listener[vp];
666
667
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10217 times.
10217 while (*prev) {
668 ✗ if (is_progress_done(p, *prev)) {
669 ✗ VVCProgressListener *l = remove_listener(prev, *prev);
670 ✗ add_listener(&list, l);
671 } else {
672 ✗ prev = &(*prev)->next;
673 }
674 }
675 10217 return list;
676 }
677
678 12489 void ff_vvc_report_progress(VVCFrame *frame, const VVCProgress vp, const int y)
679 {
680 12489 FrameProgress *p = frame->progress;
681 12489 VVCProgressListener *l = NULL;
682
683 12489 ff_mutex_lock(&p->lock);
684
2/2
✓ Branch 0 taken 10217 times.
✓ Branch 1 taken 2272 times.
12489 if (p->progress[vp] < y) {
685 // Due to the nature of thread scheduling, later progress may reach this point before earlier progress.
686 // Therefore, we only update the progress when p->progress[vp] < y.
687 10217 p->progress[vp] = y;
688 10217 l = get_done_listener(p, vp);
689 10217 ff_cond_signal(&p->cond);
690 }
691 12489 ff_mutex_unlock(&p->lock);
692
693
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12489 times.
12489 while (l) {
694 ✗ l->progress_done(l);
695 ✗ l = l->next;
696 }
697 12489 }
698
699 103257 void ff_vvc_add_progress_listener(VVCFrame *frame, VVCProgressListener *l)
700 {
701 103257 FrameProgress *p = frame->progress;
702
703 103257 ff_mutex_lock(&p->lock);
704
705
1/2
✓ Branch 1 taken 103257 times.
✗ Branch 2 not taken.
103257 if (is_progress_done(p, l)) {
706 103257 ff_mutex_unlock(&p->lock);
707 103257 l->progress_done(l);
708 } else {
709 ✗ add_listener(p->listener + l->vp, l);
710 ✗ ff_mutex_unlock(&p->lock);
711 }
712 103257 }
713