FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/vvc/refs.c
Date: 2025-09-01 20:07:09
Exec Total Coverage
Lines: 351 413 85.0%
Functions: 26 28 92.9%
Branches: 221 306 72.2%

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 51801 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 51801 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 39755 times.
✓ Branch 3 taken 12046 times.
51801 if (!frame->frame || !frame->frame->buf[0])
48 39755 return;
49
50 12046 frame->flags &= ~flags;
51
2/2
✓ Branch 0 taken 6104 times.
✓ Branch 1 taken 5942 times.
12046 if (!(frame->flags & ~VVC_FRAME_FLAG_CORRUPT))
52 6104 frame->flags = 0;
53
2/2
✓ Branch 0 taken 6104 times.
✓ Branch 1 taken 5942 times.
12046 if (!frame->flags) {
54 6104 av_frame_unref(frame->frame);
55
56
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6104 times.
6104 if (frame->needs_fg) {
57 av_frame_unref(frame->frame_grain);
58 frame->needs_fg = 0;
59 }
60
61 6104 av_refstruct_unref(&frame->sps);
62 6104 av_refstruct_unref(&frame->pps);
63 6104 av_refstruct_unref(&frame->progress);
64
65 6104 av_refstruct_unref(&frame->tab_dmvr_mvf);
66
67 6104 av_refstruct_unref(&frame->rpl);
68 6104 frame->nb_rpl_elems = 0;
69 6104 av_refstruct_unref(&frame->rpl_tab);
70
71 6104 frame->collocated_ref = NULL;
72 6104 av_refstruct_unref(&frame->hwaccel_picture_private);
73 }
74 }
75
76 3043433 const RefPicList *ff_vvc_get_ref_list(const VVCFrameContext *fc, const VVCFrame *ref, int x0, int y0)
77 {
78 3043433 const int x_cb = x0 >> fc->ps.sps->ctb_log2_size_y;
79 3043433 const int y_cb = y0 >> fc->ps.sps->ctb_log2_size_y;
80 3043433 const int pic_width_cb = fc->ps.pps->ctb_width;
81 3043433 const int ctb_addr_rs = y_cb * pic_width_cb + x_cb;
82
83 3043433 return (const RefPicList *)ref->rpl_tab[ctb_addr_rs];
84 }
85
86 103 void ff_vvc_clear_refs(VVCFrameContext *fc)
87 {
88
2/2
✓ Branch 0 taken 1751 times.
✓ Branch 1 taken 103 times.
1854 for (int i = 0; i < FF_ARRAY_ELEMS(fc->DPB); i++)
89 1751 ff_vvc_unref_frame(fc, &fc->DPB[i],
90 VVC_FRAME_FLAG_SHORT_REF | VVC_FRAME_FLAG_LONG_REF);
91 103 }
92
93 94 void ff_vvc_flush_dpb(VVCFrameContext *fc)
94 {
95
2/2
✓ Branch 0 taken 1598 times.
✓ Branch 1 taken 94 times.
1692 for (int i = 0; i < FF_ARRAY_ELEMS(fc->DPB); i++)
96 1598 ff_vvc_unref_frame(fc, &fc->DPB[i], ~0);
97 94 }
98
99 1083 static void free_progress(AVRefStructOpaque unused, void *obj)
100 {
101 1083 FrameProgress *p = (FrameProgress *)obj;
102
103
1/2
✓ Branch 0 taken 1083 times.
✗ Branch 1 not taken.
1083 if (p->has_cond)
104 1083 ff_cond_destroy(&p->cond);
105
1/2
✓ Branch 0 taken 1083 times.
✗ Branch 1 not taken.
1083 if (p->has_lock)
106 1083 ff_mutex_destroy(&p->lock);
107 1083 }
108
109 1083 static FrameProgress *alloc_progress(void)
110 {
111 1083 FrameProgress *p = av_refstruct_alloc_ext(sizeof(*p), 0, NULL, free_progress);
112
113
1/2
✓ Branch 0 taken 1083 times.
✗ Branch 1 not taken.
1083 if (p) {
114 1083 p->has_lock = !ff_mutex_init(&p->lock, NULL);
115 1083 p->has_cond = !ff_cond_init(&p->cond, NULL);
116
2/4
✓ Branch 0 taken 1083 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1083 times.
1083 if (!p->has_lock || !p->has_cond)
117 av_refstruct_unref(&p);
118 }
119 1083 return p;
120 }
121
122 1083 static VVCFrame *alloc_frame(VVCContext *s, VVCFrameContext *fc)
123 {
124 1083 const VVCSPS *sps = fc->ps.sps;
125 1083 const VVCPPS *pps = fc->ps.pps;
126
1/2
✓ Branch 0 taken 4406 times.
✗ Branch 1 not taken.
4406 for (int i = 0; i < FF_ARRAY_ELEMS(fc->DPB); i++) {
127 int ret;
128 4406 VVCFrame *frame = &fc->DPB[i];
129 4406 VVCWindow *win = &frame->scaling_win;
130
2/2
✓ Branch 0 taken 3323 times.
✓ Branch 1 taken 1083 times.
4406 if (frame->frame->buf[0])
131 3323 continue;
132
133 1083 frame->sps = av_refstruct_ref_c(fc->ps.sps);
134 1083 frame->pps = av_refstruct_ref_c(fc->ps.pps);
135
136 1083 ret = ff_thread_get_buffer(s->avctx, frame->frame, AV_GET_BUFFER_FLAG_REF);
137
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1083 times.
1083 if (ret < 0)
138 return NULL;
139
140 1083 frame->rpl = av_refstruct_allocz(s->current_frame.nb_units * sizeof(RefPicListTab));
141
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1083 times.
1083 if (!frame->rpl)
142 goto fail;
143 1083 frame->nb_rpl_elems = s->current_frame.nb_units;
144
145 1083 frame->tab_dmvr_mvf = av_refstruct_pool_get(fc->tab_dmvr_mvf_pool);
146
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1083 times.
1083 if (!frame->tab_dmvr_mvf)
147 goto fail;
148
149 1083 frame->rpl_tab = av_refstruct_pool_get(fc->rpl_tab_pool);
150
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1083 times.
1083 if (!frame->rpl_tab)
151 goto fail;
152 1083 frame->ctb_count = pps->ctb_width * pps->ctb_height;
153
2/2
✓ Branch 0 taken 53693 times.
✓ Branch 1 taken 1083 times.
54776 for (int j = 0; j < frame->ctb_count; j++)
154 53693 frame->rpl_tab[j] = frame->rpl;
155
156 1083 win->left_offset = pps->r->pps_scaling_win_left_offset * (1 << sps->hshift[CHROMA]);
157 1083 win->right_offset = pps->r->pps_scaling_win_right_offset * (1 << sps->hshift[CHROMA]);
158 1083 win->top_offset = pps->r->pps_scaling_win_top_offset * (1 << sps->vshift[CHROMA]);
159 1083 win->bottom_offset = pps->r->pps_scaling_win_bottom_offset * (1 << sps->vshift[CHROMA]);
160 1083 frame->ref_width = pps->r->pps_pic_width_in_luma_samples - win->left_offset - win->right_offset;
161 1083 frame->ref_height = pps->r->pps_pic_height_in_luma_samples - win->bottom_offset - win->top_offset;
162
163
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1083 times.
1083 if (fc->sei.frame_field_info.present) {
164 if (fc->sei.frame_field_info.picture_struct == AV_PICTURE_STRUCTURE_TOP_FIELD)
165 frame->frame->flags |= AV_FRAME_FLAG_TOP_FIELD_FIRST;
166 if (fc->sei.frame_field_info.picture_struct == AV_PICTURE_STRUCTURE_TOP_FIELD ||
167 fc->sei.frame_field_info.picture_struct == AV_PICTURE_STRUCTURE_BOTTOM_FIELD)
168 frame->frame->flags |= AV_FRAME_FLAG_INTERLACED;
169 }
170
171 1083 frame->progress = alloc_progress();
172
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1083 times.
1083 if (!frame->progress)
173 goto fail;
174
175 1083 ret = ff_hwaccel_frame_priv_alloc(s->avctx, &frame->hwaccel_picture_private);
176
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1083 times.
1083 if (ret < 0)
177 goto fail;
178
179 1083 return frame;
180 fail:
181 ff_vvc_unref_frame(fc, frame, ~0);
182 return NULL;
183 }
184 av_log(s->avctx, AV_LOG_ERROR, "Error allocating frame, DPB full.\n");
185 return NULL;
186 }
187
188 1066 static void set_pict_type(AVFrame *frame, const VVCContext *s, const VVCFrameContext *fc)
189 {
190 1066 bool has_b = false, has_inter = false;
191
192
6/6
✓ Branch 0 taken 1059 times.
✓ Branch 1 taken 7 times.
✓ Branch 2 taken 963 times.
✓ Branch 3 taken 96 times.
✓ Branch 4 taken 12 times.
✓ Branch 5 taken 951 times.
1066 if (IS_IRAP(s)) {
193 115 frame->pict_type = AV_PICTURE_TYPE_I;
194 115 frame->flags |= AV_FRAME_FLAG_KEY;
195 115 return;
196 }
197
198
2/2
✓ Branch 0 taken 948 times.
✓ Branch 1 taken 3 times.
951 if (fc->ps.ph.r->ph_inter_slice_allowed_flag) {
199 // At this point, fc->slices is not fully initialized; we need to inspect the CBS directly.
200 948 const CodedBitstreamFragment *current = &s->current_frame;
201
4/4
✓ Branch 0 taken 2264 times.
✓ Branch 1 taken 50 times.
✓ Branch 2 taken 1366 times.
✓ Branch 3 taken 898 times.
2314 for (int i = 0; i < current->nb_units && !has_b; i++) {
202 1366 const CodedBitstreamUnit *unit = current->units + i;
203
3/4
✓ Branch 0 taken 1366 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 948 times.
✓ Branch 3 taken 418 times.
1366 if (unit->content_ref && unit->type <= VVC_RSV_IRAP_11) {
204 948 const H266RawSliceHeader *rsh = unit->content_ref;
205 948 has_inter |= !IS_I(rsh);
206 948 has_b |= IS_B(rsh);
207 }
208 }
209 }
210
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 948 times.
951 if (!has_inter)
211 3 frame->pict_type = AV_PICTURE_TYPE_I;
212
2/2
✓ Branch 0 taken 926 times.
✓ Branch 1 taken 22 times.
948 else if (has_b)
213 926 frame->pict_type = AV_PICTURE_TYPE_B;
214 else
215 22 frame->pict_type = AV_PICTURE_TYPE_P;
216 }
217
218 1066 int ff_vvc_set_new_ref(VVCContext *s, VVCFrameContext *fc, AVFrame **frame)
219 {
220 1066 const VVCPH *ph= &fc->ps.ph;
221 1066 const int poc = ph->poc;
222 VVCFrame *ref;
223
224 /* check that this POC doesn't already exist */
225
2/2
✓ Branch 0 taken 18122 times.
✓ Branch 1 taken 1066 times.
19188 for (int i = 0; i < FF_ARRAY_ELEMS(fc->DPB); i++) {
226 18122 VVCFrame *frame = &fc->DPB[i];
227
228
4/4
✓ Branch 0 taken 4996 times.
✓ Branch 1 taken 13126 times.
✓ Branch 2 taken 4887 times.
✓ Branch 3 taken 109 times.
18122 if (frame->frame->buf[0] && frame->sequence == s->seq_decode &&
229
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4887 times.
4887 frame->poc == poc) {
230 av_log(s->avctx, AV_LOG_ERROR, "Duplicate POC in a sequence: %d.\n", poc);
231 return AVERROR_INVALIDDATA;
232 }
233 }
234
235 1066 ref = alloc_frame(s, fc);
236
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1066 times.
1066 if (!ref)
237 return AVERROR(ENOMEM);
238
239 1066 set_pict_type(ref->frame, s, fc);
240 1066 *frame = ref->frame;
241 1066 fc->ref = ref;
242
243
5/6
✓ Branch 0 taken 865 times.
✓ Branch 1 taken 201 times.
✓ Branch 2 taken 850 times.
✓ Branch 3 taken 15 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 850 times.
1066 if (s->no_output_before_recovery_flag && (IS_RASL(s) || !GDR_IS_RECOVERED(s)))
244 15 ref->flags = VVC_FRAME_FLAG_SHORT_REF;
245
1/2
✓ Branch 0 taken 1051 times.
✗ Branch 1 not taken.
1051 else if (ph->r->ph_pic_output_flag)
246 1051 ref->flags = VVC_FRAME_FLAG_OUTPUT | VVC_FRAME_FLAG_SHORT_REF;
247
248
2/2
✓ Branch 0 taken 1054 times.
✓ Branch 1 taken 12 times.
1066 if (!ph->r->ph_non_ref_pic_flag)
249 1054 ref->flags |= VVC_FRAME_FLAG_SHORT_REF;
250
251 1066 ref->poc = poc;
252 1066 ref->sequence = s->seq_decode;
253 1066 ref->frame->crop_left = fc->ps.pps->r->pps_conf_win_left_offset << fc->ps.sps->hshift[CHROMA];
254 1066 ref->frame->crop_right = fc->ps.pps->r->pps_conf_win_right_offset << fc->ps.sps->hshift[CHROMA];
255 1066 ref->frame->crop_top = fc->ps.pps->r->pps_conf_win_top_offset << fc->ps.sps->vshift[CHROMA];
256 1066 ref->frame->crop_bottom = fc->ps.pps->r->pps_conf_win_bottom_offset << fc->ps.sps->vshift[CHROMA];
257
258 1066 return 0;
259 }
260
261 1197 int ff_vvc_output_frame(VVCContext *s, VVCFrameContext *fc, AVFrame *out, const int no_output_of_prior_pics_flag, int flush)
262 {
263 1197 const VVCSPS *sps = fc->ps.sps;
264 103 do {
265 1300 int nb_output = 0;
266 1300 int min_poc = INT_MAX;
267 int min_idx, ret;
268
269
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1300 times.
1300 if (no_output_of_prior_pics_flag) {
270 for (int i = 0; i < FF_ARRAY_ELEMS(fc->DPB); i++) {
271 VVCFrame *frame = &fc->DPB[i];
272 if (!(frame->flags & VVC_FRAME_FLAG_BUMPING) && frame->poc != fc->ps.ph.poc &&
273 frame->sequence == s->seq_output) {
274 ff_vvc_unref_frame(fc, frame, VVC_FRAME_FLAG_OUTPUT);
275 }
276 }
277 }
278
279
2/2
✓ Branch 0 taken 22100 times.
✓ Branch 1 taken 1300 times.
23400 for (int i = 0; i < FF_ARRAY_ELEMS(fc->DPB); i++) {
280 22100 VVCFrame *frame = &fc->DPB[i];
281
2/2
✓ Branch 0 taken 5040 times.
✓ Branch 1 taken 17060 times.
22100 if ((frame->flags & VVC_FRAME_FLAG_OUTPUT) &&
282
2/2
✓ Branch 0 taken 4787 times.
✓ Branch 1 taken 253 times.
5040 frame->sequence == s->seq_output) {
283 4787 nb_output++;
284
3/4
✓ Branch 0 taken 2353 times.
✓ Branch 1 taken 2434 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 2353 times.
4787 if (frame->poc < min_poc || nb_output == 1) {
285 2434 min_poc = frame->poc;
286 2434 min_idx = i;
287 }
288 }
289 }
290
291 /* wait for more frames before output */
292
5/6
✓ Branch 0 taken 1167 times.
✓ Branch 1 taken 133 times.
✓ Branch 2 taken 1023 times.
✓ Branch 3 taken 144 times.
✓ Branch 4 taken 1023 times.
✗ Branch 5 not taken.
1300 if (!flush && s->seq_output == s->seq_decode && sps &&
293
2/2
✓ Branch 0 taken 158 times.
✓ Branch 1 taken 865 times.
1023 nb_output <= sps->r->sps_dpb_params.dpb_max_num_reorder_pics[sps->r->sps_max_sublayers_minus1])
294 158 return 0;
295
296
2/2
✓ Branch 0 taken 1007 times.
✓ Branch 1 taken 135 times.
1142 if (nb_output) {
297 1007 VVCFrame *frame = &fc->DPB[min_idx];
298
299
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1007 times.
1007 if (frame->flags & VVC_FRAME_FLAG_CORRUPT)
300 frame->frame->flags |= AV_FRAME_FLAG_CORRUPT;
301
302
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1007 times.
1007 ret = av_frame_ref(out, frame->needs_fg ? frame->frame_grain : frame->frame);
303
304
2/4
✓ Branch 0 taken 1007 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1007 times.
✗ Branch 3 not taken.
1007 if (!ret && !(s->avctx->export_side_data & AV_CODEC_EXPORT_DATA_FILM_GRAIN))
305 1007 av_frame_remove_side_data(out, AV_FRAME_DATA_FILM_GRAIN_PARAMS);
306
307
2/2
✓ Branch 0 taken 659 times.
✓ Branch 1 taken 348 times.
1007 if (frame->flags & VVC_FRAME_FLAG_BUMPING)
308 659 ff_vvc_unref_frame(fc, frame, VVC_FRAME_FLAG_OUTPUT | VVC_FRAME_FLAG_BUMPING);
309 else
310 348 ff_vvc_unref_frame(fc, frame, VVC_FRAME_FLAG_OUTPUT);
311
312
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1007 times.
1007 if (ret < 0)
313 return ret;
314
315 1007 av_log(s->avctx, AV_LOG_DEBUG,
316 "Output frame with POC %d.\n", frame->poc);
317 1007 return 1;
318 }
319
320
2/2
✓ Branch 0 taken 103 times.
✓ Branch 1 taken 32 times.
135 if (s->seq_output != s->seq_decode)
321 103 s->seq_output = (s->seq_output + 1) & 0xff;
322 else
323 32 break;
324 } while (1);
325 32 return 0;
326 }
327
328 963 void ff_vvc_bump_frame(VVCContext *s, VVCFrameContext *fc)
329 {
330 963 const VVCSPS *sps = fc->ps.sps;
331 963 const int poc = fc->ps.ph.poc;
332 963 int dpb = 0;
333 963 int min_poc = INT_MAX;
334
335
2/2
✓ Branch 0 taken 16371 times.
✓ Branch 1 taken 963 times.
17334 for (int i = 0; i < FF_ARRAY_ELEMS(fc->DPB); i++) {
336 16371 VVCFrame *frame = &fc->DPB[i];
337
2/2
✓ Branch 0 taken 5916 times.
✓ Branch 1 taken 10455 times.
16371 if ((frame->flags) &&
338
2/2
✓ Branch 0 taken 5794 times.
✓ Branch 1 taken 122 times.
5916 frame->sequence == s->seq_output &&
339
2/2
✓ Branch 0 taken 4859 times.
✓ Branch 1 taken 935 times.
5794 frame->poc != poc) {
340 4859 dpb++;
341 }
342 }
343
344
3/4
✓ Branch 0 taken 963 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 440 times.
✓ Branch 3 taken 523 times.
963 if (sps && dpb >= sps->r->sps_dpb_params.dpb_max_dec_pic_buffering_minus1[sps->r->sps_max_sublayers_minus1] + 1) {
345
2/2
✓ Branch 0 taken 7480 times.
✓ Branch 1 taken 440 times.
7920 for (int i = 0; i < FF_ARRAY_ELEMS(fc->DPB); i++) {
346 7480 VVCFrame *frame = &fc->DPB[i];
347
2/2
✓ Branch 0 taken 3037 times.
✓ Branch 1 taken 4443 times.
7480 if ((frame->flags) &&
348
1/2
✓ Branch 0 taken 3037 times.
✗ Branch 1 not taken.
3037 frame->sequence == s->seq_output &&
349
2/2
✓ Branch 0 taken 2597 times.
✓ Branch 1 taken 440 times.
3037 frame->poc != poc) {
350
4/4
✓ Branch 0 taken 60 times.
✓ Branch 1 taken 2537 times.
✓ Branch 2 taken 51 times.
✓ Branch 3 taken 9 times.
2597 if (frame->flags == VVC_FRAME_FLAG_OUTPUT && frame->poc < min_poc) {
351 51 min_poc = frame->poc;
352 }
353 }
354 }
355
356
2/2
✓ Branch 0 taken 7480 times.
✓ Branch 1 taken 440 times.
7920 for (int i = 0; i < FF_ARRAY_ELEMS(fc->DPB); i++) {
357 7480 VVCFrame *frame = &fc->DPB[i];
358
2/2
✓ Branch 0 taken 2089 times.
✓ Branch 1 taken 5391 times.
7480 if (frame->flags & VVC_FRAME_FLAG_OUTPUT &&
359
1/2
✓ Branch 0 taken 2089 times.
✗ Branch 1 not taken.
2089 frame->sequence == s->seq_output &&
360
2/2
✓ Branch 0 taken 1885 times.
✓ Branch 1 taken 204 times.
2089 frame->poc <= min_poc) {
361 1885 frame->flags |= VVC_FRAME_FLAG_BUMPING;
362 }
363 }
364
365 440 dpb--;
366 }
367 963 }
368
369 7416 static VVCFrame *find_ref_idx(VVCContext *s, VVCFrameContext *fc, int poc, uint8_t use_msb)
370 {
371
2/2
✓ Branch 0 taken 394 times.
✓ Branch 1 taken 7022 times.
7416 const unsigned mask = use_msb ? ~0 : fc->ps.sps->max_pic_order_cnt_lsb - 1;
372
373
2/2
✓ Branch 0 taken 27210 times.
✓ Branch 1 taken 28 times.
27238 for (int i = 0; i < FF_ARRAY_ELEMS(fc->DPB); i++) {
374 27210 VVCFrame *ref = &fc->DPB[i];
375
4/4
✓ Branch 0 taken 24362 times.
✓ Branch 1 taken 2848 times.
✓ Branch 2 taken 23434 times.
✓ Branch 3 taken 928 times.
27210 if (ref->frame->buf[0] && ref->sequence == s->seq_decode) {
376
2/2
✓ Branch 0 taken 7388 times.
✓ Branch 1 taken 16046 times.
23434 if ((ref->poc & mask) == poc)
377 7388 return ref;
378 }
379 }
380 28 return NULL;
381 }
382
383 24457 static void mark_ref(VVCFrame *frame, int flag)
384 {
385 24457 frame->flags &= ~(VVC_FRAME_FLAG_LONG_REF | VVC_FRAME_FLAG_SHORT_REF);
386 24457 frame->flags |= flag;
387 24457 }
388
389 17 static VVCFrame *generate_missing_ref(VVCContext *s, VVCFrameContext *fc, int poc)
390 {
391 17 const VVCSPS *sps = fc->ps.sps;
392 17 const VVCPPS *pps = fc->ps.pps;
393 VVCFrame *frame;
394
395 17 frame = alloc_frame(s, fc);
396
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 17 times.
17 if (!frame)
397 return NULL;
398
399
1/2
✓ Branch 0 taken 17 times.
✗ Branch 1 not taken.
17 if (!s->avctx->hwaccel) {
400
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 17 times.
17 if (!sps->pixel_shift) {
401 for (int i = 0; frame->frame->buf[i]; i++)
402 memset(frame->frame->buf[i]->data, 1 << (sps->bit_depth - 1),
403 frame->frame->buf[i]->size);
404 } else {
405
2/2
✓ Branch 0 taken 51 times.
✓ Branch 1 taken 17 times.
68 for (int i = 0; frame->frame->data[i]; i++)
406
2/2
✓ Branch 0 taken 10688 times.
✓ Branch 1 taken 51 times.
10739 for (int y = 0; y < (pps->height >> sps->vshift[i]); y++) {
407 10688 uint8_t *dst = frame->frame->data[i] + y * frame->frame->linesize[i];
408 10688 AV_WN16(dst, 1 << (sps->bit_depth - 1));
409 10688 av_memcpy_backptr(dst + 2, 2, 2*(pps->width >> sps->hshift[i]) - 2);
410 }
411 }
412 }
413
414 17 frame->poc = poc;
415 17 frame->sequence = s->seq_decode;
416 17 frame->flags = VVC_FRAME_FLAG_CORRUPT;
417
418 17 ff_vvc_report_frame_finished(frame);
419
420 17 return frame;
421 }
422
423 #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))
424 #define CHECK_SAMPLES(d) (frame->pps->r->pps_pic_##d##_in_luma_samples == ref->pps->r->pps_pic_##d##_in_luma_samples)
425 7401 static int check_candidate_ref(const VVCFrame *frame, const VVCRefPic *refp)
426 {
427 7401 const VVCFrame *ref = refp->ref;
428
429
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 7393 times.
7401 if (refp->is_scaled) {
430 8 const int max = FFMAX(8, frame->sps->min_cb_size_y);
431 16 return frame->ref_width * 2 >= ref->ref_width &&
432
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
8 frame->ref_height * 2 >= ref->ref_height &&
433
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
8 frame->ref_width <= ref->ref_width * 8 &&
434
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
8 frame->ref_height <= ref->ref_height * 8 &&
435
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);
436 }
437
2/4
✓ Branch 0 taken 7393 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 7393 times.
✗ Branch 3 not taken.
7393 return CHECK_SAMPLES(width) && CHECK_SAMPLES(height);
438 }
439
440 #define RPR_SCALE(f) (((ref->f << 14) + (fc->ref->f >> 1)) / fc->ref->f)
441 /* add a reference with the given poc to the list and mark it as used in DPB */
442 7416 static int add_candidate_ref(VVCContext *s, VVCFrameContext *fc, RefPicList *list,
443 int poc, int ref_flag, uint8_t use_msb)
444 {
445 7416 VVCFrame *ref = find_ref_idx(s, fc, poc, use_msb);
446 7416 VVCRefPic *refp = &list->refs[list->nb_refs];
447
448
2/4
✓ Branch 0 taken 7416 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 7416 times.
7416 if (ref == fc->ref || list->nb_refs >= VVC_MAX_REF_ENTRIES)
449 return AVERROR_INVALIDDATA;
450
451
6/8
✓ Branch 0 taken 7416 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 7402 times.
✓ Branch 3 taken 14 times.
✓ Branch 4 taken 7356 times.
✓ Branch 5 taken 46 times.
✓ Branch 6 taken 7356 times.
✗ Branch 7 not taken.
7416 if (!IS_CVSS(s)) {
452
4/4
✓ Branch 0 taken 7345 times.
✓ Branch 1 taken 11 times.
✓ Branch 2 taken 4 times.
✓ Branch 3 taken 7341 times.
7356 const bool ref_corrupt = !ref || (ref->flags & VVC_FRAME_FLAG_CORRUPT);
453
3/4
✓ Branch 0 taken 6493 times.
✓ Branch 1 taken 863 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 6493 times.
7356 const bool recovering = s->no_output_before_recovery_flag && !GDR_IS_RECOVERED(s);
454
455
3/4
✓ Branch 0 taken 15 times.
✓ Branch 1 taken 7341 times.
✓ Branch 2 taken 15 times.
✗ Branch 3 not taken.
7356 if (ref_corrupt && !recovering) {
456
1/2
✓ Branch 0 taken 15 times.
✗ Branch 1 not taken.
15 if (!(s->avctx->flags & AV_CODEC_FLAG_OUTPUT_CORRUPT) &&
457
1/2
✓ Branch 0 taken 15 times.
✗ Branch 1 not taken.
15 !(s->avctx->flags2 & AV_CODEC_FLAG2_SHOW_ALL))
458 15 return AVERROR_INVALIDDATA;
459
460 fc->ref->flags |= VVC_FRAME_FLAG_CORRUPT;
461 }
462 }
463
464
2/2
✓ Branch 0 taken 17 times.
✓ Branch 1 taken 7384 times.
7401 if (!ref) {
465 17 ref = generate_missing_ref(s, fc, poc);
466
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 17 times.
17 if (!ref)
467 return AVERROR(ENOMEM);
468 }
469
470 7401 refp->poc = poc;
471 7401 refp->ref = ref;
472 7401 refp->is_lt = ref_flag & VVC_FRAME_FLAG_LONG_REF;
473 22203 refp->is_scaled = ref->sps->r->sps_num_subpics_minus1 != fc->ref->sps->r->sps_num_subpics_minus1||
474
1/2
✓ Branch 0 taken 7401 times.
✗ Branch 1 not taken.
7401 memcmp(&ref->scaling_win, &fc->ref->scaling_win, sizeof(ref->scaling_win)) ||
475
3/4
✓ Branch 0 taken 7401 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 7393 times.
✓ Branch 3 taken 8 times.
22195 ref->pps->r->pps_pic_width_in_luma_samples != fc->ref->pps->r->pps_pic_width_in_luma_samples ||
476
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7393 times.
7393 ref->pps->r->pps_pic_height_in_luma_samples != fc->ref->pps->r->pps_pic_height_in_luma_samples;
477
478
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 7401 times.
7401 if (!check_candidate_ref(fc->ref, refp))
479 return AVERROR_INVALIDDATA;
480
481
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 7393 times.
7401 if (refp->is_scaled) {
482 8 refp->scale[0] = RPR_SCALE(ref_width);
483 8 refp->scale[1] = RPR_SCALE(ref_height);
484 }
485 7401 list->nb_refs++;
486
487 7401 mark_ref(ref, ref_flag);
488 7401 return 0;
489 }
490
491 1665 static int init_slice_rpl(const VVCFrameContext *fc, SliceContext *sc)
492 {
493 1665 VVCFrame *frame = fc->ref;
494 1665 const VVCSH *sh = &sc->sh;
495
496
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1665 times.
1665 if (sc->slice_idx >= frame->nb_rpl_elems)
497 return AVERROR_INVALIDDATA;
498
499
2/2
✓ Branch 0 taken 51800 times.
✓ Branch 1 taken 1665 times.
53465 for (int i = 0; i < sh->num_ctus_in_curr_slice; i++) {
500 51800 const int rs = sh->ctb_addr_in_curr_slice[i];
501 51800 frame->rpl_tab[rs] = frame->rpl + sc->slice_idx;
502 }
503
504 1665 sc->rpl = frame->rpl_tab[sh->ctb_addr_in_curr_slice[0]]->refPicList;
505
506 1665 return 0;
507 }
508
509 7022 static int delta_poc_st(const H266RefPicListStruct *rpls,
510 const int lx, const int i, const VVCSPS *sps)
511 {
512 7022 int abs_delta_poc_st = rpls->abs_delta_poc_st[i];
513
4/4
✓ Branch 0 taken 6503 times.
✓ Branch 1 taken 519 times.
✓ Branch 2 taken 72 times.
✓ Branch 3 taken 447 times.
7022 if (!((sps->r->sps_weighted_pred_flag ||
514
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6503 times.
6503 sps->r->sps_weighted_bipred_flag) && i != 0))
515 6575 abs_delta_poc_st++;
516 7022 return (1 - 2 * rpls->strp_entry_sign_flag[i]) * abs_delta_poc_st;
517 }
518
519 394 static int poc_lt(int *prev_delta_poc_msb, const int poc, const H266RefPicLists *ref_lists,
520 const int lx, const int j, const int max_poc_lsb)
521 {
522 394 const H266RefPicListStruct *rpls = ref_lists->rpl_ref_list + lx;
523
1/2
✓ Branch 0 taken 394 times.
✗ Branch 1 not taken.
394 int lt_poc = rpls->ltrp_in_header_flag ? ref_lists->poc_lsb_lt[lx][j] : rpls->rpls_poc_lsb_lt[j];
524
525
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 394 times.
394 if (ref_lists->delta_poc_msb_cycle_present_flag[lx][j]) {
526 const uint32_t delta = ref_lists->delta_poc_msb_cycle_lt[lx][j] + *prev_delta_poc_msb;
527 lt_poc += poc - delta * max_poc_lsb - (poc & (max_poc_lsb - 1));
528 *prev_delta_poc_msb = delta;
529 }
530 394 return lt_poc;
531 }
532
533 1665 int ff_vvc_slice_rpl(VVCContext *s, VVCFrameContext *fc, SliceContext *sc)
534 {
535 1665 const VVCSPS *sps = fc->ps.sps;
536 1665 const H266RawPPS *pps = fc->ps.pps->r;
537 1665 const VVCPH *ph = &fc->ps.ph;
538 1665 const H266RawSliceHeader *rsh = sc->sh.r;
539 1665 const int max_poc_lsb = sps->max_pic_order_cnt_lsb;
540 1665 const H266RefPicLists *ref_lists =
541
2/2
✓ Branch 0 taken 253 times.
✓ Branch 1 taken 1412 times.
1665 pps->pps_rpl_info_in_ph_flag ? &ph->r->ph_ref_pic_lists : &rsh->sh_ref_pic_lists;
542 1665 int ret = 0;
543
544 1665 ret = init_slice_rpl(fc, sc);
545
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1665 times.
1665 if (ret < 0)
546 return ret;
547
548
2/2
✓ Branch 0 taken 3315 times.
✓ Branch 1 taken 1650 times.
4965 for (int lx = L0; lx <= L1; lx++) {
549 3315 const H266RefPicListStruct *rpls = ref_lists->rpl_ref_list + lx;
550 3315 RefPicList *rpl = sc->rpl + lx;
551 3315 int poc_base = ph->poc;
552 3315 int prev_delta_poc_msb = 0;
553
554 3315 rpl->nb_refs = 0;
555
2/2
✓ Branch 0 taken 7416 times.
✓ Branch 1 taken 3300 times.
10716 for (int i = 0, j = 0; i < rpls->num_ref_entries; i++) {
556 int poc;
557
1/2
✓ Branch 0 taken 7416 times.
✗ Branch 1 not taken.
7416 if (!rpls->inter_layer_ref_pic_flag[i]) {
558 7416 int use_msb = 1;
559 int ref_flag;
560
2/2
✓ Branch 0 taken 7022 times.
✓ Branch 1 taken 394 times.
7416 if (rpls->st_ref_pic_flag[i]) {
561 7022 poc = poc_base + delta_poc_st(rpls, lx, i, sps);
562 7022 poc_base = poc;
563 7022 ref_flag = VVC_FRAME_FLAG_SHORT_REF;
564 } else {
565 394 use_msb = ref_lists->delta_poc_msb_cycle_present_flag[lx][j];
566 394 poc = poc_lt(&prev_delta_poc_msb, ph->poc, ref_lists, lx, j, max_poc_lsb);
567 394 ref_flag = VVC_FRAME_FLAG_LONG_REF;
568 394 j++;
569 }
570 7416 ret = add_candidate_ref(s, fc, rpl, poc, ref_flag, use_msb);
571
2/2
✓ Branch 0 taken 15 times.
✓ Branch 1 taken 7401 times.
7416 if (ret < 0)
572 15 return ret;
573 } else {
574 // OPI_B_3.bit and VPS_A_3.bit should cover this
575 avpriv_report_missing_feature(fc->log_ctx, "Inter layer ref");
576 ret = AVERROR_PATCHWELCOME;
577 return ret;
578 }
579 }
580
2/2
✓ Branch 0 taken 3062 times.
✓ Branch 1 taken 238 times.
3300 if (ph->r->ph_temporal_mvp_enabled_flag &&
581
2/2
✓ Branch 0 taken 1531 times.
✓ Branch 1 taken 1531 times.
3062 (!rsh->sh_collocated_from_l0_flag) == lx &&
582
1/2
✓ Branch 0 taken 1531 times.
✗ Branch 1 not taken.
1531 rsh->sh_collocated_ref_idx < rpl->nb_refs) {
583 1531 const VVCRefPic *refp = rpl->refs + rsh->sh_collocated_ref_idx;
584
2/4
✓ Branch 0 taken 1531 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1531 times.
1531 if (refp->is_scaled || refp->ref->sps->ctb_log2_size_y != sps->ctb_log2_size_y)
585 return AVERROR_INVALIDDATA;
586 1531 fc->ref->collocated_ref = refp->ref;
587 }
588 }
589 1650 return 0;
590 }
591
592 1066 int ff_vvc_frame_rpl(VVCContext *s, VVCFrameContext *fc, SliceContext *sc)
593 {
594 1066 int ret = 0;
595
596 /* clear the reference flags on all frames except the current one */
597
2/2
✓ Branch 0 taken 18122 times.
✓ Branch 1 taken 1066 times.
19188 for (int i = 0; i < FF_ARRAY_ELEMS(fc->DPB); i++) {
598 18122 VVCFrame *frame = &fc->DPB[i];
599
600
2/2
✓ Branch 0 taken 1066 times.
✓ Branch 1 taken 17056 times.
18122 if (frame == fc->ref)
601 1066 continue;
602
603 17056 mark_ref(frame, 0);
604 }
605
606
2/2
✓ Branch 1 taken 1051 times.
✓ Branch 2 taken 15 times.
1066 if ((ret = ff_vvc_slice_rpl(s, fc, sc)) < 0)
607 15 goto fail;
608
609 1051 fail:
610 /* release any frames that are now unused */
611
2/2
✓ Branch 0 taken 18122 times.
✓ Branch 1 taken 1066 times.
19188 for (int i = 0; i < FF_ARRAY_ELEMS(fc->DPB); i++)
612 18122 ff_vvc_unref_frame(fc, &fc->DPB[i], 0);
613 1066 return ret;
614 }
615
616 1068 void ff_vvc_report_frame_finished(VVCFrame *frame)
617 {
618 1068 ff_vvc_report_progress(frame, VVC_PROGRESS_MV, INT_MAX);
619 1068 ff_vvc_report_progress(frame, VVC_PROGRESS_PIXEL, INT_MAX);
620 1068 }
621
622 100321 static int is_progress_done(const FrameProgress *p, const VVCProgressListener *l)
623 {
624 100321 return p->progress[l->vp] > l->y;
625 }
626
627 static void add_listener(VVCProgressListener **prev, VVCProgressListener *l)
628 {
629 l->next = *prev;
630 *prev = l;
631 }
632
633 static VVCProgressListener* remove_listener(VVCProgressListener **prev, VVCProgressListener *l)
634 {
635 *prev = l->next;
636 l->next = NULL;
637 return l;
638 }
639
640 9633 static VVCProgressListener* get_done_listener(FrameProgress *p, const VVCProgress vp)
641 {
642 9633 VVCProgressListener *list = NULL;
643 9633 VVCProgressListener **prev = &p->listener[vp];
644
645
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9633 times.
9633 while (*prev) {
646 if (is_progress_done(p, *prev)) {
647 VVCProgressListener *l = remove_listener(prev, *prev);
648 add_listener(&list, l);
649 } else {
650 prev = &(*prev)->next;
651 }
652 }
653 9633 return list;
654 }
655
656 11735 void ff_vvc_report_progress(VVCFrame *frame, const VVCProgress vp, const int y)
657 {
658 11735 FrameProgress *p = frame->progress;
659 11735 VVCProgressListener *l = NULL;
660
661 11735 ff_mutex_lock(&p->lock);
662
2/2
✓ Branch 0 taken 9633 times.
✓ Branch 1 taken 2102 times.
11735 if (p->progress[vp] < y) {
663 // Due to the nature of thread scheduling, later progress may reach this point before earlier progress.
664 // Therefore, we only update the progress when p->progress[vp] < y.
665 9633 p->progress[vp] = y;
666 9633 l = get_done_listener(p, vp);
667 9633 ff_cond_signal(&p->cond);
668 }
669 11735 ff_mutex_unlock(&p->lock);
670
671
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11735 times.
11735 while (l) {
672 l->progress_done(l);
673 l = l->next;
674 }
675 11735 }
676
677 100321 void ff_vvc_add_progress_listener(VVCFrame *frame, VVCProgressListener *l)
678 {
679 100321 FrameProgress *p = frame->progress;
680
681 100321 ff_mutex_lock(&p->lock);
682
683
1/2
✓ Branch 1 taken 100321 times.
✗ Branch 2 not taken.
100321 if (is_progress_done(p, l)) {
684 100321 ff_mutex_unlock(&p->lock);
685 100321 l->progress_done(l);
686 } else {
687 add_listener(p->listener + l->vp, l);
688 ff_mutex_unlock(&p->lock);
689 }
690 100321 }
691