FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/vvc/refs.c
Date: 2025-06-01 09:29:47
Exec Total Coverage
Lines: 352 415 84.8%
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 47211 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 47211 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 36528 times.
✓ Branch 3 taken 10683 times.
47211 if (!frame->frame || !frame->frame->buf[0])
48 36528 return;
49
50 10683 frame->flags &= ~flags;
51
2/2
✓ Branch 0 taken 5419 times.
✓ Branch 1 taken 5264 times.
10683 if (!(frame->flags & ~VVC_FRAME_FLAG_CORRUPT))
52 5419 frame->flags = 0;
53
2/2
✓ Branch 0 taken 5419 times.
✓ Branch 1 taken 5264 times.
10683 if (!frame->flags) {
54 5419 av_frame_unref(frame->frame);
55
56
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5419 times.
5419 if (frame->needs_fg) {
57 av_frame_unref(frame->frame_grain);
58 frame->needs_fg = 0;
59 }
60
61 5419 av_refstruct_unref(&frame->sps);
62 5419 av_refstruct_unref(&frame->pps);
63 5419 av_refstruct_unref(&frame->progress);
64
65 5419 av_refstruct_unref(&frame->tab_dmvr_mvf);
66
67 5419 av_refstruct_unref(&frame->rpl);
68 5419 frame->nb_rpl_elems = 0;
69 5419 av_refstruct_unref(&frame->rpl_tab);
70
71 5419 frame->collocated_ref = NULL;
72 5419 av_refstruct_unref(&frame->hwaccel_picture_private);
73 }
74 }
75
76 2402201 const RefPicList *ff_vvc_get_ref_list(const VVCFrameContext *fc, const VVCFrame *ref, int x0, int y0)
77 {
78 2402201 const int x_cb = x0 >> fc->ps.sps->ctb_log2_size_y;
79 2402201 const int y_cb = y0 >> fc->ps.sps->ctb_log2_size_y;
80 2402201 const int pic_width_cb = fc->ps.pps->ctb_width;
81 2402201 const int ctb_addr_rs = y_cb * pic_width_cb + x_cb;
82
83 2402201 return (const RefPicList *)ref->rpl_tab[ctb_addr_rs];
84 }
85
86 97 void ff_vvc_clear_refs(VVCFrameContext *fc)
87 {
88
2/2
✓ Branch 0 taken 1649 times.
✓ Branch 1 taken 97 times.
1746 for (int i = 0; i < FF_ARRAY_ELEMS(fc->DPB); i++)
89 1649 ff_vvc_unref_frame(fc, &fc->DPB[i],
90 VVC_FRAME_FLAG_SHORT_REF | VVC_FRAME_FLAG_LONG_REF);
91 97 }
92
93 88 void ff_vvc_flush_dpb(VVCFrameContext *fc)
94 {
95
2/2
✓ Branch 0 taken 1496 times.
✓ Branch 1 taken 88 times.
1584 for (int i = 0; i < FF_ARRAY_ELEMS(fc->DPB); i++)
96 1496 ff_vvc_unref_frame(fc, &fc->DPB[i], ~0);
97 88 }
98
99 978 static void free_progress(AVRefStructOpaque unused, void *obj)
100 {
101 978 FrameProgress *p = (FrameProgress *)obj;
102
103
1/2
✓ Branch 0 taken 978 times.
✗ Branch 1 not taken.
978 if (p->has_cond)
104 978 ff_cond_destroy(&p->cond);
105
1/2
✓ Branch 0 taken 978 times.
✗ Branch 1 not taken.
978 if (p->has_lock)
106 978 ff_mutex_destroy(&p->lock);
107 978 }
108
109 978 static FrameProgress *alloc_progress(void)
110 {
111 978 FrameProgress *p = av_refstruct_alloc_ext(sizeof(*p), 0, NULL, free_progress);
112
113
1/2
✓ Branch 0 taken 978 times.
✗ Branch 1 not taken.
978 if (p) {
114 978 p->has_lock = !ff_mutex_init(&p->lock, NULL);
115 978 p->has_cond = !ff_cond_init(&p->cond, NULL);
116
2/4
✓ Branch 0 taken 978 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 978 times.
978 if (!p->has_lock || !p->has_cond)
117 av_refstruct_unref(&p);
118 }
119 978 return p;
120 }
121
122 978 static VVCFrame *alloc_frame(VVCContext *s, VVCFrameContext *fc)
123 {
124 978 const VVCSPS *sps = fc->ps.sps;
125 978 const VVCPPS *pps = fc->ps.pps;
126
1/2
✓ Branch 0 taken 3900 times.
✗ Branch 1 not taken.
3900 for (int i = 0; i < FF_ARRAY_ELEMS(fc->DPB); i++) {
127 int ret;
128 3900 VVCFrame *frame = &fc->DPB[i];
129 3900 VVCWindow *win = &frame->scaling_win;
130
2/2
✓ Branch 0 taken 2922 times.
✓ Branch 1 taken 978 times.
3900 if (frame->frame->buf[0])
131 2922 continue;
132
133 978 frame->sps = av_refstruct_ref_c(fc->ps.sps);
134 978 frame->pps = av_refstruct_ref_c(fc->ps.pps);
135
136 978 ret = ff_thread_get_buffer(s->avctx, frame->frame, AV_GET_BUFFER_FLAG_REF);
137
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 978 times.
978 if (ret < 0)
138 return NULL;
139
140 978 frame->rpl = av_refstruct_allocz(s->current_frame.nb_units * sizeof(RefPicListTab));
141
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 978 times.
978 if (!frame->rpl)
142 goto fail;
143 978 frame->nb_rpl_elems = s->current_frame.nb_units;
144
145 978 frame->tab_dmvr_mvf = av_refstruct_pool_get(fc->tab_dmvr_mvf_pool);
146
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 978 times.
978 if (!frame->tab_dmvr_mvf)
147 goto fail;
148
149 978 frame->rpl_tab = av_refstruct_pool_get(fc->rpl_tab_pool);
150
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 978 times.
978 if (!frame->rpl_tab)
151 goto fail;
152 978 frame->ctb_count = pps->ctb_width * pps->ctb_height;
153
2/2
✓ Branch 0 taken 47051 times.
✓ Branch 1 taken 978 times.
48029 for (int j = 0; j < frame->ctb_count; j++)
154 47051 frame->rpl_tab[j] = frame->rpl;
155
156 978 win->left_offset = pps->r->pps_scaling_win_left_offset * (1 << sps->hshift[CHROMA]);
157 978 win->right_offset = pps->r->pps_scaling_win_right_offset * (1 << sps->hshift[CHROMA]);
158 978 win->top_offset = pps->r->pps_scaling_win_top_offset * (1 << sps->vshift[CHROMA]);
159 978 win->bottom_offset = pps->r->pps_scaling_win_bottom_offset * (1 << sps->vshift[CHROMA]);
160 978 frame->ref_width = pps->r->pps_pic_width_in_luma_samples - win->left_offset - win->right_offset;
161 978 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 978 times.
978 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 978 frame->progress = alloc_progress();
172
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 978 times.
978 if (!frame->progress)
173 goto fail;
174
175 978 ret = ff_hwaccel_frame_priv_alloc(s->avctx, &frame->hwaccel_picture_private);
176
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 978 times.
978 if (ret < 0)
177 goto fail;
178
179 978 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 961 static void set_pict_type(AVFrame *frame, const VVCContext *s, const VVCFrameContext *fc)
189 {
190 961 bool has_b = false, has_inter = false;
191
192
6/6
✓ Branch 0 taken 954 times.
✓ Branch 1 taken 7 times.
✓ Branch 2 taken 864 times.
✓ Branch 3 taken 90 times.
✓ Branch 4 taken 11 times.
✓ Branch 5 taken 853 times.
961 if (IS_IRAP(s)) {
193 108 frame->pict_type = AV_PICTURE_TYPE_I;
194 108 frame->flags |= AV_FRAME_FLAG_KEY;
195 108 return;
196 }
197
198
2/2
✓ Branch 0 taken 850 times.
✓ Branch 1 taken 3 times.
853 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 850 const CodedBitstreamFragment *current = &s->current_frame;
201
4/4
✓ Branch 0 taken 2053 times.
✓ Branch 1 taken 50 times.
✓ Branch 2 taken 1253 times.
✓ Branch 3 taken 800 times.
2103 for (int i = 0; i < current->nb_units && !has_b; i++) {
202 1253 const CodedBitstreamUnit *unit = current->units + i;
203
3/4
✓ Branch 0 taken 1253 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 850 times.
✓ Branch 3 taken 403 times.
1253 if (unit->content_ref && unit->type <= VVC_RSV_IRAP_11) {
204 850 const H266RawSliceHeader *rsh = unit->content_ref;
205 850 has_inter |= !IS_I(rsh);
206 850 has_b |= IS_B(rsh);
207 }
208 }
209 }
210
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 850 times.
853 if (!has_inter)
211 3 frame->pict_type = AV_PICTURE_TYPE_I;
212
2/2
✓ Branch 0 taken 828 times.
✓ Branch 1 taken 22 times.
850 else if (has_b)
213 828 frame->pict_type = AV_PICTURE_TYPE_B;
214 else
215 22 frame->pict_type = AV_PICTURE_TYPE_P;
216 }
217
218 961 int ff_vvc_set_new_ref(VVCContext *s, VVCFrameContext *fc, AVFrame **frame)
219 {
220 961 const VVCPH *ph= &fc->ps.ph;
221 961 const int poc = ph->poc;
222 VVCFrame *ref;
223
224 /* check that this POC doesn't already exist */
225
2/2
✓ Branch 0 taken 16337 times.
✓ Branch 1 taken 961 times.
17298 for (int i = 0; i < FF_ARRAY_ELEMS(fc->DPB); i++) {
226 16337 VVCFrame *frame = &fc->DPB[i];
227
228
4/4
✓ Branch 0 taken 4416 times.
✓ Branch 1 taken 11921 times.
✓ Branch 2 taken 4307 times.
✓ Branch 3 taken 109 times.
16337 if (frame->frame->buf[0] && frame->sequence == s->seq_decode &&
229
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4307 times.
4307 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 961 ref = alloc_frame(s, fc);
236
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 961 times.
961 if (!ref)
237 return AVERROR(ENOMEM);
238
239 961 set_pict_type(ref->frame, s, fc);
240 961 *frame = ref->frame;
241 961 fc->ref = ref;
242
243
5/6
✓ Branch 0 taken 776 times.
✓ Branch 1 taken 185 times.
✓ Branch 2 taken 761 times.
✓ Branch 3 taken 15 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 761 times.
961 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 946 times.
✗ Branch 1 not taken.
946 else if (ph->r->ph_pic_output_flag)
246 946 ref->flags = VVC_FRAME_FLAG_OUTPUT | VVC_FRAME_FLAG_SHORT_REF;
247
248
2/2
✓ Branch 0 taken 949 times.
✓ Branch 1 taken 12 times.
961 if (!ph->r->ph_non_ref_pic_flag)
249 949 ref->flags |= VVC_FRAME_FLAG_SHORT_REF;
250
251 961 ref->poc = poc;
252 961 ref->sequence = s->seq_decode;
253 961 ref->frame->crop_left = fc->ps.pps->r->pps_conf_win_left_offset << fc->ps.sps->hshift[CHROMA];
254 961 ref->frame->crop_right = fc->ps.pps->r->pps_conf_win_right_offset << fc->ps.sps->hshift[CHROMA];
255 961 ref->frame->crop_top = fc->ps.pps->r->pps_conf_win_top_offset << fc->ps.sps->vshift[CHROMA];
256 961 ref->frame->crop_bottom = fc->ps.pps->r->pps_conf_win_bottom_offset << fc->ps.sps->vshift[CHROMA];
257
258 961 return 0;
259 }
260
261 1073 int ff_vvc_output_frame(VVCContext *s, VVCFrameContext *fc, AVFrame *out, const int no_output_of_prior_pics_flag, int flush)
262 {
263 1073 const VVCSPS *sps = fc->ps.sps;
264 97 do {
265 1170 int nb_output = 0;
266 1170 int min_poc = INT_MAX;
267 int min_idx, ret;
268
269
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1170 times.
1170 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 19890 times.
✓ Branch 1 taken 1170 times.
21060 for (int i = 0; i < FF_ARRAY_ELEMS(fc->DPB); i++) {
280 19890 VVCFrame *frame = &fc->DPB[i];
281
2/2
✓ Branch 0 taken 4441 times.
✓ Branch 1 taken 15449 times.
19890 if ((frame->flags & VVC_FRAME_FLAG_OUTPUT) &&
282
2/2
✓ Branch 0 taken 4194 times.
✓ Branch 1 taken 247 times.
4441 frame->sequence == s->seq_output) {
283 4194 nb_output++;
284
3/4
✓ Branch 0 taken 2029 times.
✓ Branch 1 taken 2165 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 2029 times.
4194 if (frame->poc < min_poc || nb_output == 1) {
285 2165 min_poc = frame->poc;
286 2165 min_idx = i;
287 }
288 }
289 }
290
291 /* wait for more frames before output */
292
5/6
✓ Branch 0 taken 1056 times.
✓ Branch 1 taken 114 times.
✓ Branch 2 taken 918 times.
✓ Branch 3 taken 138 times.
✓ Branch 4 taken 918 times.
✗ Branch 5 not taken.
1170 if (!flush && s->seq_output == s->seq_decode && sps &&
293
2/2
✓ Branch 0 taken 139 times.
✓ Branch 1 taken 779 times.
918 nb_output <= sps->r->sps_dpb_params.dpb_max_num_reorder_pics[sps->r->sps_max_sublayers_minus1])
294 139 return 0;
295
296
2/2
✓ Branch 0 taken 905 times.
✓ Branch 1 taken 126 times.
1031 if (nb_output) {
297 905 VVCFrame *frame = &fc->DPB[min_idx];
298
299
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 905 times.
905 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 905 times.
905 ret = av_frame_ref(out, frame->needs_fg ? frame->frame_grain : frame->frame);
303
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 905 times.
905 if (ret < 0)
304 return ret;
305
306
1/2
✓ Branch 0 taken 905 times.
✗ Branch 1 not taken.
905 if (!(s->avctx->export_side_data & AV_CODEC_EXPORT_DATA_FILM_GRAIN))
307 905 av_frame_remove_side_data(out, AV_FRAME_DATA_FILM_GRAIN_PARAMS);
308
309
2/2
✓ Branch 0 taken 580 times.
✓ Branch 1 taken 325 times.
905 if (frame->flags & VVC_FRAME_FLAG_BUMPING)
310 580 ff_vvc_unref_frame(fc, frame, VVC_FRAME_FLAG_OUTPUT | VVC_FRAME_FLAG_BUMPING);
311 else
312 325 ff_vvc_unref_frame(fc, frame, VVC_FRAME_FLAG_OUTPUT);
313
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 905 times.
905 if (ret < 0)
314 return ret;
315
316 905 av_log(s->avctx, AV_LOG_DEBUG,
317 "Output frame with POC %d.\n", frame->poc);
318 905 return 1;
319 }
320
321
2/2
✓ Branch 0 taken 97 times.
✓ Branch 1 taken 29 times.
126 if (s->seq_output != s->seq_decode)
322 97 s->seq_output = (s->seq_output + 1) & 0xff;
323 else
324 29 break;
325 } while (1);
326 29 return 0;
327 }
328
329 864 void ff_vvc_bump_frame(VVCContext *s, VVCFrameContext *fc)
330 {
331 864 const VVCSPS *sps = fc->ps.sps;
332 864 const int poc = fc->ps.ph.poc;
333 864 int dpb = 0;
334 864 int min_poc = INT_MAX;
335
336
2/2
✓ Branch 0 taken 14688 times.
✓ Branch 1 taken 864 times.
15552 for (int i = 0; i < FF_ARRAY_ELEMS(fc->DPB); i++) {
337 14688 VVCFrame *frame = &fc->DPB[i];
338
2/2
✓ Branch 0 taken 5237 times.
✓ Branch 1 taken 9451 times.
14688 if ((frame->flags) &&
339
2/2
✓ Branch 0 taken 5115 times.
✓ Branch 1 taken 122 times.
5237 frame->sequence == s->seq_output &&
340
2/2
✓ Branch 0 taken 4279 times.
✓ Branch 1 taken 836 times.
5115 frame->poc != poc) {
341 4279 dpb++;
342 }
343 }
344
345
3/4
✓ Branch 0 taken 864 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 390 times.
✓ Branch 3 taken 474 times.
864 if (sps && dpb >= sps->r->sps_dpb_params.dpb_max_dec_pic_buffering_minus1[sps->r->sps_max_sublayers_minus1] + 1) {
346
2/2
✓ Branch 0 taken 6630 times.
✓ Branch 1 taken 390 times.
7020 for (int i = 0; i < FF_ARRAY_ELEMS(fc->DPB); i++) {
347 6630 VVCFrame *frame = &fc->DPB[i];
348
2/2
✓ Branch 0 taken 2678 times.
✓ Branch 1 taken 3952 times.
6630 if ((frame->flags) &&
349
1/2
✓ Branch 0 taken 2678 times.
✗ Branch 1 not taken.
2678 frame->sequence == s->seq_output &&
350
2/2
✓ Branch 0 taken 2288 times.
✓ Branch 1 taken 390 times.
2678 frame->poc != poc) {
351
4/4
✓ Branch 0 taken 58 times.
✓ Branch 1 taken 2230 times.
✓ Branch 2 taken 50 times.
✓ Branch 3 taken 8 times.
2288 if (frame->flags == VVC_FRAME_FLAG_OUTPUT && frame->poc < min_poc) {
352 50 min_poc = frame->poc;
353 }
354 }
355 }
356
357
2/2
✓ Branch 0 taken 6630 times.
✓ Branch 1 taken 390 times.
7020 for (int i = 0; i < FF_ARRAY_ELEMS(fc->DPB); i++) {
358 6630 VVCFrame *frame = &fc->DPB[i];
359
2/2
✓ Branch 0 taken 1835 times.
✓ Branch 1 taken 4795 times.
6630 if (frame->flags & VVC_FRAME_FLAG_OUTPUT &&
360
1/2
✓ Branch 0 taken 1835 times.
✗ Branch 1 not taken.
1835 frame->sequence == s->seq_output &&
361
2/2
✓ Branch 0 taken 1639 times.
✓ Branch 1 taken 196 times.
1835 frame->poc <= min_poc) {
362 1639 frame->flags |= VVC_FRAME_FLAG_BUMPING;
363 }
364 }
365
366 390 dpb--;
367 }
368 864 }
369
370 6882 static VVCFrame *find_ref_idx(VVCContext *s, VVCFrameContext *fc, int poc, uint8_t use_msb)
371 {
372
2/2
✓ Branch 0 taken 394 times.
✓ Branch 1 taken 6488 times.
6882 const unsigned mask = use_msb ? ~0 : fc->ps.sps->max_pic_order_cnt_lsb - 1;
373
374
2/2
✓ Branch 0 taken 25119 times.
✓ Branch 1 taken 28 times.
25147 for (int i = 0; i < FF_ARRAY_ELEMS(fc->DPB); i++) {
375 25119 VVCFrame *ref = &fc->DPB[i];
376
4/4
✓ Branch 0 taken 22391 times.
✓ Branch 1 taken 2728 times.
✓ Branch 2 taken 21463 times.
✓ Branch 3 taken 928 times.
25119 if (ref->frame->buf[0] && ref->sequence == s->seq_decode) {
377
2/2
✓ Branch 0 taken 6854 times.
✓ Branch 1 taken 14609 times.
21463 if ((ref->poc & mask) == poc)
378 6854 return ref;
379 }
380 }
381 28 return NULL;
382 }
383
384 22243 static void mark_ref(VVCFrame *frame, int flag)
385 {
386 22243 frame->flags &= ~(VVC_FRAME_FLAG_LONG_REF | VVC_FRAME_FLAG_SHORT_REF);
387 22243 frame->flags |= flag;
388 22243 }
389
390 17 static VVCFrame *generate_missing_ref(VVCContext *s, VVCFrameContext *fc, int poc)
391 {
392 17 const VVCSPS *sps = fc->ps.sps;
393 17 const VVCPPS *pps = fc->ps.pps;
394 VVCFrame *frame;
395
396 17 frame = alloc_frame(s, fc);
397
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 17 times.
17 if (!frame)
398 return NULL;
399
400
1/2
✓ Branch 0 taken 17 times.
✗ Branch 1 not taken.
17 if (!s->avctx->hwaccel) {
401
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 17 times.
17 if (!sps->pixel_shift) {
402 for (int i = 0; frame->frame->buf[i]; i++)
403 memset(frame->frame->buf[i]->data, 1 << (sps->bit_depth - 1),
404 frame->frame->buf[i]->size);
405 } else {
406
2/2
✓ Branch 0 taken 51 times.
✓ Branch 1 taken 17 times.
68 for (int i = 0; frame->frame->data[i]; i++)
407
2/2
✓ Branch 0 taken 10688 times.
✓ Branch 1 taken 51 times.
10739 for (int y = 0; y < (pps->height >> sps->vshift[i]); y++) {
408 10688 uint8_t *dst = frame->frame->data[i] + y * frame->frame->linesize[i];
409 10688 AV_WN16(dst, 1 << (sps->bit_depth - 1));
410 10688 av_memcpy_backptr(dst + 2, 2, 2*(pps->width >> sps->hshift[i]) - 2);
411 }
412 }
413 }
414
415 17 frame->poc = poc;
416 17 frame->sequence = s->seq_decode;
417 17 frame->flags = VVC_FRAME_FLAG_CORRUPT;
418
419 17 ff_vvc_report_frame_finished(frame);
420
421 17 return frame;
422 }
423
424 #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))
425 #define CHECK_SAMPLES(d) (frame->pps->r->pps_pic_##d##_in_luma_samples == ref->pps->r->pps_pic_##d##_in_luma_samples)
426 6867 static int check_candidate_ref(const VVCFrame *frame, const VVCRefPic *refp)
427 {
428 6867 const VVCFrame *ref = refp->ref;
429
430
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 6859 times.
6867 if (refp->is_scaled) {
431 8 const int max = FFMAX(8, frame->sps->min_cb_size_y);
432 16 return frame->ref_width * 2 >= ref->ref_width &&
433
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
8 frame->ref_height * 2 >= ref->ref_height &&
434
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
8 frame->ref_width <= ref->ref_width * 8 &&
435
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
8 frame->ref_height <= ref->ref_height * 8 &&
436
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);
437 }
438
2/4
✓ Branch 0 taken 6859 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6859 times.
✗ Branch 3 not taken.
6859 return CHECK_SAMPLES(width) && CHECK_SAMPLES(height);
439 }
440
441 #define RPR_SCALE(f) (((ref->f << 14) + (fc->ref->f >> 1)) / fc->ref->f)
442 /* add a reference with the given poc to the list and mark it as used in DPB */
443 6882 static int add_candidate_ref(VVCContext *s, VVCFrameContext *fc, RefPicList *list,
444 int poc, int ref_flag, uint8_t use_msb)
445 {
446 6882 VVCFrame *ref = find_ref_idx(s, fc, poc, use_msb);
447 6882 VVCRefPic *refp = &list->refs[list->nb_refs];
448
449
2/4
✓ Branch 0 taken 6882 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 6882 times.
6882 if (ref == fc->ref || list->nb_refs >= VVC_MAX_REF_ENTRIES)
450 return AVERROR_INVALIDDATA;
451
452
6/8
✓ Branch 0 taken 6882 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6868 times.
✓ Branch 3 taken 14 times.
✓ Branch 4 taken 6827 times.
✓ Branch 5 taken 41 times.
✓ Branch 6 taken 6827 times.
✗ Branch 7 not taken.
6882 if (!IS_CVSS(s)) {
453
4/4
✓ Branch 0 taken 6816 times.
✓ Branch 1 taken 11 times.
✓ Branch 2 taken 4 times.
✓ Branch 3 taken 6812 times.
6827 const bool ref_corrupt = !ref || (ref->flags & VVC_FRAME_FLAG_CORRUPT);
454
3/4
✓ Branch 0 taken 6036 times.
✓ Branch 1 taken 791 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 6036 times.
6827 const bool recovering = s->no_output_before_recovery_flag && !GDR_IS_RECOVERED(s);
455
456
3/4
✓ Branch 0 taken 15 times.
✓ Branch 1 taken 6812 times.
✓ Branch 2 taken 15 times.
✗ Branch 3 not taken.
6827 if (ref_corrupt && !recovering) {
457
1/2
✓ Branch 0 taken 15 times.
✗ Branch 1 not taken.
15 if (!(s->avctx->flags & AV_CODEC_FLAG_OUTPUT_CORRUPT) &&
458
1/2
✓ Branch 0 taken 15 times.
✗ Branch 1 not taken.
15 !(s->avctx->flags2 & AV_CODEC_FLAG2_SHOW_ALL))
459 15 return AVERROR_INVALIDDATA;
460
461 fc->ref->flags |= VVC_FRAME_FLAG_CORRUPT;
462 }
463 }
464
465
2/2
✓ Branch 0 taken 17 times.
✓ Branch 1 taken 6850 times.
6867 if (!ref) {
466 17 ref = generate_missing_ref(s, fc, poc);
467
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 17 times.
17 if (!ref)
468 return AVERROR(ENOMEM);
469 }
470
471 6867 refp->poc = poc;
472 6867 refp->ref = ref;
473 6867 refp->is_lt = ref_flag & VVC_FRAME_FLAG_LONG_REF;
474 20601 refp->is_scaled = ref->sps->r->sps_num_subpics_minus1 != fc->ref->sps->r->sps_num_subpics_minus1||
475
1/2
✓ Branch 0 taken 6867 times.
✗ Branch 1 not taken.
6867 memcmp(&ref->scaling_win, &fc->ref->scaling_win, sizeof(ref->scaling_win)) ||
476
3/4
✓ Branch 0 taken 6867 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6859 times.
✓ Branch 3 taken 8 times.
20593 ref->pps->r->pps_pic_width_in_luma_samples != fc->ref->pps->r->pps_pic_width_in_luma_samples ||
477
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6859 times.
6859 ref->pps->r->pps_pic_height_in_luma_samples != fc->ref->pps->r->pps_pic_height_in_luma_samples;
478
479
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 6867 times.
6867 if (!check_candidate_ref(fc->ref, refp))
480 return AVERROR_INVALIDDATA;
481
482
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 6859 times.
6867 if (refp->is_scaled) {
483 8 refp->scale[0] = RPR_SCALE(ref_width);
484 8 refp->scale[1] = RPR_SCALE(ref_height);
485 }
486 6867 list->nb_refs++;
487
488 6867 mark_ref(ref, ref_flag);
489 6867 return 0;
490 }
491
492 1560 static int init_slice_rpl(const VVCFrameContext *fc, SliceContext *sc)
493 {
494 1560 VVCFrame *frame = fc->ref;
495 1560 const VVCSH *sh = &sc->sh;
496
497
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1560 times.
1560 if (sc->slice_idx >= frame->nb_rpl_elems)
498 return AVERROR_INVALIDDATA;
499
500
2/2
✓ Branch 0 taken 45158 times.
✓ Branch 1 taken 1560 times.
46718 for (int i = 0; i < sh->num_ctus_in_curr_slice; i++) {
501 45158 const int rs = sh->ctb_addr_in_curr_slice[i];
502 45158 frame->rpl_tab[rs] = frame->rpl + sc->slice_idx;
503 }
504
505 1560 sc->rpl = frame->rpl_tab[sh->ctb_addr_in_curr_slice[0]]->refPicList;
506
507 1560 return 0;
508 }
509
510 6488 static int delta_poc_st(const H266RefPicListStruct *rpls,
511 const int lx, const int i, const VVCSPS *sps)
512 {
513 6488 int abs_delta_poc_st = rpls->abs_delta_poc_st[i];
514
4/4
✓ Branch 0 taken 5969 times.
✓ Branch 1 taken 519 times.
✓ Branch 2 taken 72 times.
✓ Branch 3 taken 447 times.
6488 if (!((sps->r->sps_weighted_pred_flag ||
515
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5969 times.
5969 sps->r->sps_weighted_bipred_flag) && i != 0))
516 6041 abs_delta_poc_st++;
517 6488 return (1 - 2 * rpls->strp_entry_sign_flag[i]) * abs_delta_poc_st;
518 }
519
520 394 static int poc_lt(int *prev_delta_poc_msb, const int poc, const H266RefPicLists *ref_lists,
521 const int lx, const int j, const int max_poc_lsb)
522 {
523 394 const H266RefPicListStruct *rpls = ref_lists->rpl_ref_list + lx;
524
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];
525
526
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 394 times.
394 if (ref_lists->delta_poc_msb_cycle_present_flag[lx][j]) {
527 const uint32_t delta = ref_lists->delta_poc_msb_cycle_lt[lx][j] + *prev_delta_poc_msb;
528 lt_poc += poc - delta * max_poc_lsb - (poc & (max_poc_lsb - 1));
529 *prev_delta_poc_msb = delta;
530 }
531 394 return lt_poc;
532 }
533
534 1560 int ff_vvc_slice_rpl(VVCContext *s, VVCFrameContext *fc, SliceContext *sc)
535 {
536 1560 const VVCSPS *sps = fc->ps.sps;
537 1560 const H266RawPPS *pps = fc->ps.pps->r;
538 1560 const VVCPH *ph = &fc->ps.ph;
539 1560 const H266RawSliceHeader *rsh = sc->sh.r;
540 1560 const int max_poc_lsb = sps->max_pic_order_cnt_lsb;
541 1560 const H266RefPicLists *ref_lists =
542
2/2
✓ Branch 0 taken 253 times.
✓ Branch 1 taken 1307 times.
1560 pps->pps_rpl_info_in_ph_flag ? &ph->r->ph_ref_pic_lists : &rsh->sh_ref_pic_lists;
543 1560 int ret = 0;
544
545 1560 ret = init_slice_rpl(fc, sc);
546
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1560 times.
1560 if (ret < 0)
547 return ret;
548
549
2/2
✓ Branch 0 taken 3105 times.
✓ Branch 1 taken 1545 times.
4650 for (int lx = L0; lx <= L1; lx++) {
550 3105 const H266RefPicListStruct *rpls = ref_lists->rpl_ref_list + lx;
551 3105 RefPicList *rpl = sc->rpl + lx;
552 3105 int poc_base = ph->poc;
553 3105 int prev_delta_poc_msb = 0;
554
555 3105 rpl->nb_refs = 0;
556
2/2
✓ Branch 0 taken 6882 times.
✓ Branch 1 taken 3090 times.
9972 for (int i = 0, j = 0; i < rpls->num_ref_entries; i++) {
557 int poc;
558
1/2
✓ Branch 0 taken 6882 times.
✗ Branch 1 not taken.
6882 if (!rpls->inter_layer_ref_pic_flag[i]) {
559 6882 int use_msb = 1;
560 int ref_flag;
561
2/2
✓ Branch 0 taken 6488 times.
✓ Branch 1 taken 394 times.
6882 if (rpls->st_ref_pic_flag[i]) {
562 6488 poc = poc_base + delta_poc_st(rpls, lx, i, sps);
563 6488 poc_base = poc;
564 6488 ref_flag = VVC_FRAME_FLAG_SHORT_REF;
565 } else {
566 394 use_msb = ref_lists->delta_poc_msb_cycle_present_flag[lx][j];
567 394 poc = poc_lt(&prev_delta_poc_msb, ph->poc, ref_lists, lx, j, max_poc_lsb);
568 394 ref_flag = VVC_FRAME_FLAG_LONG_REF;
569 394 j++;
570 }
571 6882 ret = add_candidate_ref(s, fc, rpl, poc, ref_flag, use_msb);
572
2/2
✓ Branch 0 taken 15 times.
✓ Branch 1 taken 6867 times.
6882 if (ret < 0)
573 15 return ret;
574 } else {
575 // OPI_B_3.bit and VPS_A_3.bit should cover this
576 avpriv_report_missing_feature(fc->log_ctx, "Inter layer ref");
577 ret = AVERROR_PATCHWELCOME;
578 return ret;
579 }
580 }
581
2/2
✓ Branch 0 taken 2866 times.
✓ Branch 1 taken 224 times.
3090 if (ph->r->ph_temporal_mvp_enabled_flag &&
582
2/2
✓ Branch 0 taken 1433 times.
✓ Branch 1 taken 1433 times.
2866 (!rsh->sh_collocated_from_l0_flag) == lx &&
583
1/2
✓ Branch 0 taken 1433 times.
✗ Branch 1 not taken.
1433 rsh->sh_collocated_ref_idx < rpl->nb_refs) {
584 1433 const VVCRefPic *refp = rpl->refs + rsh->sh_collocated_ref_idx;
585
2/4
✓ Branch 0 taken 1433 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1433 times.
1433 if (refp->is_scaled || refp->ref->sps->ctb_log2_size_y != sps->ctb_log2_size_y)
586 return AVERROR_INVALIDDATA;
587 1433 fc->ref->collocated_ref = refp->ref;
588 }
589 }
590 1545 return 0;
591 }
592
593 961 int ff_vvc_frame_rpl(VVCContext *s, VVCFrameContext *fc, SliceContext *sc)
594 {
595 961 int ret = 0;
596
597 /* clear the reference flags on all frames except the current one */
598
2/2
✓ Branch 0 taken 16337 times.
✓ Branch 1 taken 961 times.
17298 for (int i = 0; i < FF_ARRAY_ELEMS(fc->DPB); i++) {
599 16337 VVCFrame *frame = &fc->DPB[i];
600
601
2/2
✓ Branch 0 taken 961 times.
✓ Branch 1 taken 15376 times.
16337 if (frame == fc->ref)
602 961 continue;
603
604 15376 mark_ref(frame, 0);
605 }
606
607
2/2
✓ Branch 1 taken 946 times.
✓ Branch 2 taken 15 times.
961 if ((ret = ff_vvc_slice_rpl(s, fc, sc)) < 0)
608 15 goto fail;
609
610 946 fail:
611 /* release any frames that are now unused */
612
2/2
✓ Branch 0 taken 16337 times.
✓ Branch 1 taken 961 times.
17298 for (int i = 0; i < FF_ARRAY_ELEMS(fc->DPB); i++)
613 16337 ff_vvc_unref_frame(fc, &fc->DPB[i], 0);
614 961 return ret;
615 }
616
617 963 void ff_vvc_report_frame_finished(VVCFrame *frame)
618 {
619 963 ff_vvc_report_progress(frame, VVC_PROGRESS_MV, INT_MAX);
620 963 ff_vvc_report_progress(frame, VVC_PROGRESS_PIXEL, INT_MAX);
621 963 }
622
623 88284 static int is_progress_done(const FrameProgress *p, const VVCProgressListener *l)
624 {
625 88284 return p->progress[l->vp] > l->y;
626 }
627
628 static void add_listener(VVCProgressListener **prev, VVCProgressListener *l)
629 {
630 l->next = *prev;
631 *prev = l;
632 }
633
634 static VVCProgressListener* remove_listener(VVCProgressListener **prev, VVCProgressListener *l)
635 {
636 *prev = l->next;
637 l->next = NULL;
638 return l;
639 }
640
641 8433 static VVCProgressListener* get_done_listener(FrameProgress *p, const VVCProgress vp)
642 {
643 8433 VVCProgressListener *list = NULL;
644 8433 VVCProgressListener **prev = &p->listener[vp];
645
646
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8433 times.
8433 while (*prev) {
647 if (is_progress_done(p, *prev)) {
648 VVCProgressListener *l = remove_listener(prev, *prev);
649 add_listener(&list, l);
650 } else {
651 prev = &(*prev)->next;
652 }
653 }
654 8433 return list;
655 }
656
657 10325 void ff_vvc_report_progress(VVCFrame *frame, const VVCProgress vp, const int y)
658 {
659 10325 FrameProgress *p = frame->progress;
660 10325 VVCProgressListener *l = NULL;
661
662 10325 ff_mutex_lock(&p->lock);
663
2/2
✓ Branch 0 taken 8433 times.
✓ Branch 1 taken 1892 times.
10325 if (p->progress[vp] < y) {
664 // Due to the nature of thread scheduling, later progress may reach this point before earlier progress.
665 // Therefore, we only update the progress when p->progress[vp] < y.
666 8433 p->progress[vp] = y;
667 8433 l = get_done_listener(p, vp);
668 8433 ff_cond_signal(&p->cond);
669 }
670 10325 ff_mutex_unlock(&p->lock);
671
672
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10325 times.
10325 while (l) {
673 l->progress_done(l);
674 l = l->next;
675 }
676 10325 }
677
678 88284 void ff_vvc_add_progress_listener(VVCFrame *frame, VVCProgressListener *l)
679 {
680 88284 FrameProgress *p = frame->progress;
681
682 88284 ff_mutex_lock(&p->lock);
683
684
1/2
✓ Branch 1 taken 88284 times.
✗ Branch 2 not taken.
88284 if (is_progress_done(p, l)) {
685 88284 ff_mutex_unlock(&p->lock);
686 88284 l->progress_done(l);
687 } else {
688 add_listener(p->listener + l->vp, l);
689 ff_mutex_unlock(&p->lock);
690 }
691 88284 }
692