FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/hevc/refs.c
Date: 2024-07-16 12:46:59
Exec Total Coverage
Lines: 277 313 88.5%
Functions: 17 17 100.0%
Branches: 198 246 80.5%

Line Branch Exec Source
1 /*
2 * HEVC video decoder
3 *
4 * Copyright (C) 2012 - 2013 Guillaume Martres
5 * Copyright (C) 2012 - 2013 Gildas Cocherel
6 *
7 * This file is part of FFmpeg.
8 *
9 * FFmpeg is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
13 *
14 * FFmpeg is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with FFmpeg; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 */
23
24 #include "libavutil/mem.h"
25
26 #include "decode.h"
27 #include "hevc.h"
28 #include "hevcdec.h"
29 #include "progressframe.h"
30 #include "refstruct.h"
31
32 342244 void ff_hevc_unref_frame(HEVCFrame *frame, int flags)
33 {
34 342244 frame->flags &= ~flags;
35
2/2
✓ Branch 0 taken 286757 times.
✓ Branch 1 taken 55487 times.
342244 if (!frame->flags) {
36 286757 ff_progress_frame_unref(&frame->tf);
37 286757 av_frame_unref(frame->frame_grain);
38 286757 frame->needs_fg = 0;
39
40 286757 ff_refstruct_unref(&frame->tab_mvf);
41
42 286757 ff_refstruct_unref(&frame->rpl);
43 286757 frame->nb_rpl_elems = 0;
44 286757 ff_refstruct_unref(&frame->rpl_tab);
45 286757 frame->refPicList = NULL;
46
47 286757 ff_refstruct_unref(&frame->hwaccel_picture_private);
48 }
49 342244 }
50
51 4579640 const RefPicList *ff_hevc_get_ref_list(const HEVCContext *s,
52 const HEVCFrame *ref, int x0, int y0)
53 {
54 4579640 int x_cb = x0 >> s->ps.sps->log2_ctb_size;
55 4579640 int y_cb = y0 >> s->ps.sps->log2_ctb_size;
56 4579640 int pic_width_cb = s->ps.sps->ctb_width;
57 4579640 int ctb_addr_ts = s->pps->ctb_addr_rs_to_ts[y_cb * pic_width_cb + x_cb];
58 4579640 return &ref->rpl_tab[ctb_addr_ts]->refPicList[0];
59 }
60
61 767 void ff_hevc_clear_refs(HEVCContext *s)
62 {
63 int i;
64
2/2
✓ Branch 0 taken 24544 times.
✓ Branch 1 taken 767 times.
25311 for (i = 0; i < FF_ARRAY_ELEMS(s->DPB); i++)
65 24544 ff_hevc_unref_frame(&s->DPB[i],
66 HEVC_FRAME_FLAG_SHORT_REF |
67 HEVC_FRAME_FLAG_LONG_REF);
68 767 }
69
70 8 void ff_hevc_flush_dpb(HEVCContext *s)
71 {
72 int i;
73
2/2
✓ Branch 0 taken 256 times.
✓ Branch 1 taken 8 times.
264 for (i = 0; i < FF_ARRAY_ELEMS(s->DPB); i++)
74 256 ff_hevc_unref_frame(&s->DPB[i], ~0);
75 8 }
76
77 9603 static HEVCFrame *alloc_frame(HEVCContext *s)
78 {
79 int i, j, ret;
80
1/2
✓ Branch 0 taken 33864 times.
✗ Branch 1 not taken.
33864 for (i = 0; i < FF_ARRAY_ELEMS(s->DPB); i++) {
81 33864 HEVCFrame *frame = &s->DPB[i];
82
2/2
✓ Branch 0 taken 24261 times.
✓ Branch 1 taken 9603 times.
33864 if (frame->f)
83 24261 continue;
84
85 9603 ret = ff_progress_frame_get_buffer(s->avctx, &frame->tf,
86 AV_GET_BUFFER_FLAG_REF);
87
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9603 times.
9603 if (ret < 0)
88 return NULL;
89
90 9603 frame->rpl = ff_refstruct_allocz(s->pkt.nb_nals * sizeof(*frame->rpl));
91
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9603 times.
9603 if (!frame->rpl)
92 goto fail;
93 9603 frame->nb_rpl_elems = s->pkt.nb_nals;
94
95 9603 frame->tab_mvf = ff_refstruct_pool_get(s->tab_mvf_pool);
96
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9603 times.
9603 if (!frame->tab_mvf)
97 goto fail;
98
99 9603 frame->rpl_tab = ff_refstruct_pool_get(s->rpl_tab_pool);
100
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9603 times.
9603 if (!frame->rpl_tab)
101 goto fail;
102 9603 frame->ctb_count = s->ps.sps->ctb_width * s->ps.sps->ctb_height;
103
2/2
✓ Branch 0 taken 1492503 times.
✓ Branch 1 taken 9603 times.
1502106 for (j = 0; j < frame->ctb_count; j++)
104 1492503 frame->rpl_tab[j] = frame->rpl;
105
106
2/2
✓ Branch 0 taken 15 times.
✓ Branch 1 taken 9588 times.
9603 if (s->sei.picture_timing.picture_struct == AV_PICTURE_STRUCTURE_TOP_FIELD)
107 15 frame->f->flags |= AV_FRAME_FLAG_TOP_FIELD_FIRST;
108
2/2
✓ Branch 0 taken 9588 times.
✓ Branch 1 taken 15 times.
9603 if ((s->sei.picture_timing.picture_struct == AV_PICTURE_STRUCTURE_TOP_FIELD) ||
109
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 9586 times.
9588 (s->sei.picture_timing.picture_struct == AV_PICTURE_STRUCTURE_BOTTOM_FIELD))
110 17 frame->f->flags |= AV_FRAME_FLAG_INTERLACED;
111
112 9603 ret = ff_hwaccel_frame_priv_alloc(s->avctx, &frame->hwaccel_picture_private);
113
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9603 times.
9603 if (ret < 0)
114 goto fail;
115
116 9603 return frame;
117 fail:
118 ff_hevc_unref_frame(frame, ~0);
119 return NULL;
120 }
121 av_log(s->avctx, AV_LOG_ERROR, "Error allocating frame, DPB full.\n");
122 return NULL;
123 }
124
125 9539 int ff_hevc_set_new_ref(HEVCContext *s, int poc)
126 {
127 HEVCFrame *ref;
128 int i;
129
130 /* check that this POC doesn't already exist */
131
2/2
✓ Branch 0 taken 305248 times.
✓ Branch 1 taken 9539 times.
314787 for (i = 0; i < FF_ARRAY_ELEMS(s->DPB); i++) {
132 305248 HEVCFrame *frame = &s->DPB[i];
133
134
4/4
✓ Branch 0 taken 45609 times.
✓ Branch 1 taken 259639 times.
✓ Branch 2 taken 45432 times.
✓ Branch 3 taken 177 times.
305248 if (frame->f && frame->sequence == s->seq_decode &&
135
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 45432 times.
45432 frame->poc == poc) {
136 av_log(s->avctx, AV_LOG_ERROR, "Duplicate POC in a sequence: %d.\n",
137 poc);
138 return AVERROR_INVALIDDATA;
139 }
140 }
141
142 9539 ref = alloc_frame(s);
143
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9539 times.
9539 if (!ref)
144 return AVERROR(ENOMEM);
145
146 9539 s->cur_frame = ref;
147 9539 s->collocated_ref = NULL;
148
149
2/2
✓ Branch 0 taken 9533 times.
✓ Branch 1 taken 6 times.
9539 if (s->sh.pic_output_flag)
150 9533 ref->flags = HEVC_FRAME_FLAG_OUTPUT | HEVC_FRAME_FLAG_SHORT_REF;
151 else
152 6 ref->flags = HEVC_FRAME_FLAG_SHORT_REF;
153
154 9539 ref->poc = poc;
155 9539 ref->sequence = s->seq_decode;
156 9539 ref->f->crop_left = s->ps.sps->output_window.left_offset;
157 9539 ref->f->crop_right = s->ps.sps->output_window.right_offset;
158 9539 ref->f->crop_top = s->ps.sps->output_window.top_offset;
159 9539 ref->f->crop_bottom = s->ps.sps->output_window.bottom_offset;
160
161 9539 return 0;
162 }
163
164 9157 static void unref_missing_refs(HEVCContext *s)
165 {
166
2/2
✓ Branch 0 taken 293024 times.
✓ Branch 1 taken 9157 times.
302181 for (int i = 0; i < FF_ARRAY_ELEMS(s->DPB); i++) {
167 293024 HEVCFrame *frame = &s->DPB[i];
168
2/2
✓ Branch 0 taken 164 times.
✓ Branch 1 taken 292860 times.
293024 if (frame->sequence == HEVC_SEQUENCE_COUNTER_INVALID) {
169 164 ff_hevc_unref_frame(frame, ~0);
170 }
171 }
172 9157 }
173
174 10027 int ff_hevc_output_frame(HEVCContext *s, AVFrame *out, int flush)
175 {
176
6/6
✓ Branch 0 taken 9143 times.
✓ Branch 1 taken 884 times.
✓ Branch 2 taken 327 times.
✓ Branch 3 taken 557 times.
✓ Branch 4 taken 149 times.
✓ Branch 5 taken 408 times.
10027 if (IS_IRAP(s) && s->no_rasl_output_flag == 1) {
177 const static int mask = HEVC_FRAME_FLAG_BUMPING | HEVC_FRAME_FLAG_OUTPUT;
178
2/2
✓ Branch 0 taken 408 times.
✓ Branch 1 taken 13056 times.
13464 for (int i = 0; i < FF_ARRAY_ELEMS(s->DPB); i++) {
179 13056 HEVCFrame *frame = &s->DPB[i];
180
2/2
✓ Branch 0 taken 467 times.
✓ Branch 1 taken 12589 times.
13056 if ((frame->flags & mask) == HEVC_FRAME_FLAG_OUTPUT &&
181
2/2
✓ Branch 0 taken 65 times.
✓ Branch 1 taken 402 times.
467 frame->sequence != s->seq_decode) {
182
2/2
✓ Branch 0 taken 26 times.
✓ Branch 1 taken 39 times.
65 if (s->sh.no_output_of_prior_pics_flag == 1)
183 26 ff_hevc_unref_frame(frame, HEVC_FRAME_FLAG_OUTPUT);
184 else
185 39 frame->flags |= HEVC_FRAME_FLAG_BUMPING;
186 }
187 }
188 }
189 781 do {
190 10808 int nb_output = 0;
191 10808 int min_poc = INT_MAX;
192 int i, min_idx, ret;
193
194
2/2
✓ Branch 0 taken 345856 times.
✓ Branch 1 taken 10808 times.
356664 for (i = 0; i < FF_ARRAY_ELEMS(s->DPB); i++) {
195 345856 HEVCFrame *frame = &s->DPB[i];
196
2/2
✓ Branch 0 taken 32966 times.
✓ Branch 1 taken 312890 times.
345856 if ((frame->flags & HEVC_FRAME_FLAG_OUTPUT) &&
197
2/2
✓ Branch 0 taken 32035 times.
✓ Branch 1 taken 931 times.
32966 frame->sequence == s->seq_output) {
198 32035 nb_output++;
199
3/4
✓ Branch 0 taken 15123 times.
✓ Branch 1 taken 16912 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 15123 times.
32035 if (frame->poc < min_poc || nb_output == 1) {
200 16912 min_poc = frame->poc;
201 16912 min_idx = i;
202 }
203 }
204 }
205
206 /* wait for more frames before output */
207
5/6
✓ Branch 0 taken 10314 times.
✓ Branch 1 taken 494 times.
✓ Branch 2 taken 9495 times.
✓ Branch 3 taken 819 times.
✓ Branch 4 taken 9495 times.
✗ Branch 5 not taken.
10808 if (!flush && s->seq_output == s->seq_decode && s->ps.sps &&
208
2/2
✓ Branch 0 taken 468 times.
✓ Branch 1 taken 9027 times.
9495 nb_output <= s->ps.sps->temporal_layer[s->ps.sps->max_sub_layers - 1].num_reorder_pics)
209 468 return 0;
210
211
2/2
✓ Branch 0 taken 9382 times.
✓ Branch 1 taken 958 times.
10340 if (nb_output) {
212 9382 HEVCFrame *frame = &s->DPB[min_idx];
213
214
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9382 times.
9382 ret = av_frame_ref(out, frame->needs_fg ? frame->frame_grain : frame->f);
215
2/2
✓ Branch 0 taken 2078 times.
✓ Branch 1 taken 7304 times.
9382 if (frame->flags & HEVC_FRAME_FLAG_BUMPING)
216 2078 ff_hevc_unref_frame(frame, HEVC_FRAME_FLAG_OUTPUT | HEVC_FRAME_FLAG_BUMPING);
217 else
218 7304 ff_hevc_unref_frame(frame, HEVC_FRAME_FLAG_OUTPUT);
219
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9382 times.
9382 if (ret < 0)
220 return ret;
221
222
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 9382 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
9382 if (frame->needs_fg && (ret = av_frame_copy_props(out, frame->f)) < 0)
223 return ret;
224
225
1/2
✓ Branch 0 taken 9382 times.
✗ Branch 1 not taken.
9382 if (!(s->avctx->export_side_data & AV_CODEC_EXPORT_DATA_FILM_GRAIN))
226 9382 av_frame_remove_side_data(out, AV_FRAME_DATA_FILM_GRAIN_PARAMS);
227
228 9382 av_log(s->avctx, AV_LOG_DEBUG,
229 "Output frame with POC %d.\n", frame->poc);
230 9382 return 1;
231 }
232
233
2/2
✓ Branch 0 taken 781 times.
✓ Branch 1 taken 177 times.
958 if (s->seq_output != s->seq_decode)
234 781 s->seq_output = (s->seq_output + 1) & HEVC_SEQUENCE_COUNTER_MASK;
235 else
236 177 break;
237 } while (1);
238
239 177 return 0;
240 }
241
242 8990 void ff_hevc_bump_frame(HEVCContext *s)
243 {
244 8990 int dpb = 0;
245 8990 int min_poc = INT_MAX;
246 int i;
247
248
2/2
✓ Branch 0 taken 287680 times.
✓ Branch 1 taken 8990 times.
296670 for (i = 0; i < FF_ARRAY_ELEMS(s->DPB); i++) {
249 287680 HEVCFrame *frame = &s->DPB[i];
250
2/2
✓ Branch 0 taken 47711 times.
✓ Branch 1 taken 239969 times.
287680 if ((frame->flags) &&
251
2/2
✓ Branch 0 taken 47580 times.
✓ Branch 1 taken 131 times.
47711 frame->sequence == s->seq_output &&
252
2/2
✓ Branch 0 taken 38628 times.
✓ Branch 1 taken 8952 times.
47580 frame->poc != s->poc) {
253 38628 dpb++;
254 }
255 }
256
257
3/4
✓ Branch 0 taken 8990 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1697 times.
✓ Branch 3 taken 7293 times.
8990 if (s->ps.sps && dpb >= s->ps.sps->temporal_layer[s->ps.sps->max_sub_layers - 1].max_dec_pic_buffering) {
258
2/2
✓ Branch 0 taken 54304 times.
✓ Branch 1 taken 1697 times.
56001 for (i = 0; i < FF_ARRAY_ELEMS(s->DPB); i++) {
259 54304 HEVCFrame *frame = &s->DPB[i];
260
2/2
✓ Branch 0 taken 10318 times.
✓ Branch 1 taken 43986 times.
54304 if ((frame->flags) &&
261
1/2
✓ Branch 0 taken 10318 times.
✗ Branch 1 not taken.
10318 frame->sequence == s->seq_output &&
262
2/2
✓ Branch 0 taken 8621 times.
✓ Branch 1 taken 1697 times.
10318 frame->poc != s->poc) {
263
4/4
✓ Branch 0 taken 1446 times.
✓ Branch 1 taken 7175 times.
✓ Branch 2 taken 1444 times.
✓ Branch 3 taken 2 times.
8621 if (frame->flags == HEVC_FRAME_FLAG_OUTPUT && frame->poc < min_poc) {
264 1444 min_poc = frame->poc;
265 }
266 }
267 }
268
269
2/2
✓ Branch 0 taken 54304 times.
✓ Branch 1 taken 1697 times.
56001 for (i = 0; i < FF_ARRAY_ELEMS(s->DPB); i++) {
270 54304 HEVCFrame *frame = &s->DPB[i];
271
2/2
✓ Branch 0 taken 6790 times.
✓ Branch 1 taken 47514 times.
54304 if (frame->flags & HEVC_FRAME_FLAG_OUTPUT &&
272
1/2
✓ Branch 0 taken 6790 times.
✗ Branch 1 not taken.
6790 frame->sequence == s->seq_output &&
273
2/2
✓ Branch 0 taken 2789 times.
✓ Branch 1 taken 4001 times.
6790 frame->poc <= min_poc) {
274 2789 frame->flags |= HEVC_FRAME_FLAG_BUMPING;
275 }
276 }
277
278 1697 dpb--;
279 }
280 8990 }
281
282 18439 static int init_slice_rpl(HEVCContext *s)
283 {
284 18439 HEVCFrame *frame = s->cur_frame;
285 18439 int ctb_count = frame->ctb_count;
286 18439 int ctb_addr_ts = s->pps->ctb_addr_rs_to_ts[s->sh.slice_segment_addr];
287 int i;
288
289
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 18439 times.
18439 if (s->slice_idx >= frame->nb_rpl_elems)
290 return AVERROR_INVALIDDATA;
291
292
2/2
✓ Branch 0 taken 1967924 times.
✓ Branch 1 taken 18439 times.
1986363 for (i = ctb_addr_ts; i < ctb_count; i++)
293 1967924 frame->rpl_tab[i] = frame->rpl + s->slice_idx;
294
295 18439 frame->refPicList = (RefPicList *)frame->rpl_tab[ctb_addr_ts];
296
297 18439 return 0;
298 }
299
300 18439 int ff_hevc_slice_rpl(HEVCContext *s)
301 {
302 18439 SliceHeader *sh = &s->sh;
303
304
2/2
✓ Branch 0 taken 14689 times.
✓ Branch 1 taken 3750 times.
18439 uint8_t nb_list = sh->slice_type == HEVC_SLICE_B ? 2 : 1;
305 uint8_t list_idx;
306 int i, j, ret;
307
308 18439 ret = init_slice_rpl(s);
309
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 18439 times.
18439 if (ret < 0)
310 return ret;
311
312 18439 if (!(s->rps[ST_CURR_BEF].nb_refs + s->rps[ST_CURR_AFT].nb_refs +
313
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 18439 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
18439 s->rps[LT_CURR].nb_refs) && !s->pps->pps_curr_pic_ref_enabled_flag) {
314 av_log(s->avctx, AV_LOG_ERROR, "Zero refs in the frame RPS.\n");
315 return AVERROR_INVALIDDATA;
316 }
317
318
2/2
✓ Branch 0 taken 33128 times.
✓ Branch 1 taken 18439 times.
51567 for (list_idx = 0; list_idx < nb_list; list_idx++) {
319 33128 RefPicList rpl_tmp = { { 0 } };
320 33128 RefPicList *rpl = &s->cur_frame->refPicList[list_idx];
321
322 /* The order of the elements is
323 * ST_CURR_BEF - ST_CURR_AFT - LT_CURR for the L0 and
324 * ST_CURR_AFT - ST_CURR_BEF - LT_CURR for the L1 */
325 33128 int cand_lists[3] = { list_idx ? ST_CURR_AFT : ST_CURR_BEF,
326 33128 list_idx ? ST_CURR_BEF : ST_CURR_AFT,
327 LT_CURR };
328
329 /* concatenate the candidate lists for the current frame */
330
2/2
✓ Branch 0 taken 33146 times.
✓ Branch 1 taken 33128 times.
66274 while (rpl_tmp.nb_refs < sh->nb_refs[list_idx]) {
331
2/2
✓ Branch 0 taken 99438 times.
✓ Branch 1 taken 33146 times.
132584 for (i = 0; i < FF_ARRAY_ELEMS(cand_lists); i++) {
332 99438 RefPicList *rps = &s->rps[cand_lists[i]];
333
3/4
✓ Branch 0 taken 119806 times.
✓ Branch 1 taken 99438 times.
✓ Branch 2 taken 119806 times.
✗ Branch 3 not taken.
219244 for (j = 0; j < rps->nb_refs && rpl_tmp.nb_refs < HEVC_MAX_REFS; j++) {
334 119806 rpl_tmp.list[rpl_tmp.nb_refs] = rps->list[j];
335 119806 rpl_tmp.ref[rpl_tmp.nb_refs] = rps->ref[j];
336 119806 rpl_tmp.isLongTerm[rpl_tmp.nb_refs] = i == 2;
337 119806 rpl_tmp.nb_refs++;
338 }
339 }
340 // Construct RefPicList0, RefPicList1 (8-8, 8-10)
341
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 33146 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
33146 if (s->pps->pps_curr_pic_ref_enabled_flag && rpl_tmp.nb_refs < HEVC_MAX_REFS) {
342 rpl_tmp.list[rpl_tmp.nb_refs] = s->cur_frame->poc;
343 rpl_tmp.ref[rpl_tmp.nb_refs] = s->cur_frame;
344 rpl_tmp.isLongTerm[rpl_tmp.nb_refs] = 1;
345 rpl_tmp.nb_refs++;
346 }
347 }
348
349 /* reorder the references if necessary */
350
2/2
✓ Branch 0 taken 1827 times.
✓ Branch 1 taken 31301 times.
33128 if (sh->rpl_modification_flag[list_idx]) {
351
2/2
✓ Branch 0 taken 4333 times.
✓ Branch 1 taken 1827 times.
6160 for (i = 0; i < sh->nb_refs[list_idx]; i++) {
352 4333 int idx = sh->list_entry_lx[list_idx][i];
353
354
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4333 times.
4333 if (idx >= rpl_tmp.nb_refs) {
355 av_log(s->avctx, AV_LOG_ERROR, "Invalid reference index.\n");
356 return AVERROR_INVALIDDATA;
357 }
358
359 4333 rpl->list[i] = rpl_tmp.list[idx];
360 4333 rpl->ref[i] = rpl_tmp.ref[idx];
361 4333 rpl->isLongTerm[i] = rpl_tmp.isLongTerm[idx];
362 4333 rpl->nb_refs++;
363 }
364 } else {
365 31301 memcpy(rpl, &rpl_tmp, sizeof(*rpl));
366 31301 rpl->nb_refs = FFMIN(rpl->nb_refs, sh->nb_refs[list_idx]);
367 }
368
369 // 8-9
370
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 33128 times.
33128 if (s->pps->pps_curr_pic_ref_enabled_flag &&
371 !sh->rpl_modification_flag[list_idx] &&
372 rpl_tmp.nb_refs > sh->nb_refs[L0]) {
373 rpl->list[sh->nb_refs[L0] - 1] = s->cur_frame->poc;
374 rpl->ref[sh->nb_refs[L0] - 1] = s->cur_frame;
375 }
376
377
2/2
✓ Branch 0 taken 18439 times.
✓ Branch 1 taken 14689 times.
33128 if (sh->collocated_list == list_idx &&
378
1/2
✓ Branch 0 taken 18439 times.
✗ Branch 1 not taken.
18439 sh->collocated_ref_idx < rpl->nb_refs)
379 18439 s->collocated_ref = rpl->ref[sh->collocated_ref_idx];
380 }
381
382 18439 return 0;
383 }
384
385 35142 static HEVCFrame *find_ref_idx(HEVCContext *s, int poc, uint8_t use_msb)
386 {
387
2/2
✓ Branch 0 taken 1513 times.
✓ Branch 1 taken 33629 times.
35142 int mask = use_msb ? ~0 : (1 << s->ps.sps->log2_max_poc_lsb) - 1;
388 int i;
389
390
2/2
✓ Branch 0 taken 152541 times.
✓ Branch 1 taken 64 times.
152605 for (i = 0; i < FF_ARRAY_ELEMS(s->DPB); i++) {
391 152541 HEVCFrame *ref = &s->DPB[i];
392
4/4
✓ Branch 0 taken 144766 times.
✓ Branch 1 taken 7775 times.
✓ Branch 2 taken 144591 times.
✓ Branch 3 taken 175 times.
152541 if (ref->f && ref->sequence == s->seq_decode) {
393
5/6
✓ Branch 0 taken 35078 times.
✓ Branch 1 taken 109513 times.
✓ Branch 2 taken 1512 times.
✓ Branch 3 taken 33566 times.
✓ Branch 4 taken 1512 times.
✗ Branch 5 not taken.
144591 if ((ref->poc & mask) == poc && (use_msb || ref->poc != s->poc))
394 35078 return ref;
395 }
396 }
397
398
7/8
✓ Branch 0 taken 25 times.
✓ Branch 1 taken 39 times.
✓ Branch 2 taken 22 times.
✓ Branch 3 taken 3 times.
✓ Branch 4 taken 22 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 20 times.
✓ Branch 7 taken 2 times.
64 if (s->nal_unit_type != HEVC_NAL_CRA_NUT && !IS_BLA(s))
399 20 av_log(s->avctx, AV_LOG_ERROR,
400 "Could not find ref with POC %d\n", poc);
401 64 return NULL;
402 }
403
404 319009 static void mark_ref(HEVCFrame *frame, int flag)
405 {
406 319009 frame->flags &= ~(HEVC_FRAME_FLAG_LONG_REF | HEVC_FRAME_FLAG_SHORT_REF);
407 319009 frame->flags |= flag;
408 319009 }
409
410 64 static HEVCFrame *generate_missing_ref(HEVCContext *s, int poc)
411 {
412 HEVCFrame *frame;
413 int i, y;
414
415 64 frame = alloc_frame(s);
416
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 64 times.
64 if (!frame)
417 return NULL;
418
419
1/2
✓ Branch 0 taken 64 times.
✗ Branch 1 not taken.
64 if (!s->avctx->hwaccel) {
420
2/2
✓ Branch 0 taken 44 times.
✓ Branch 1 taken 20 times.
64 if (!s->ps.sps->pixel_shift) {
421
2/2
✓ Branch 0 taken 132 times.
✓ Branch 1 taken 44 times.
176 for (i = 0; frame->f->data[i]; i++)
422 132 memset(frame->f->data[i], 1 << (s->ps.sps->bit_depth - 1),
423 132 frame->f->linesize[i] * AV_CEIL_RSHIFT(s->ps.sps->height, s->ps.sps->vshift[i]));
424 } else {
425
2/2
✓ Branch 0 taken 60 times.
✓ Branch 1 taken 20 times.
80 for (i = 0; frame->f->data[i]; i++)
426
2/2
✓ Branch 0 taken 71424 times.
✓ Branch 1 taken 60 times.
71484 for (y = 0; y < (s->ps.sps->height >> s->ps.sps->vshift[i]); y++) {
427 71424 uint8_t *dst = frame->f->data[i] + y * frame->f->linesize[i];
428 71424 AV_WN16(dst, 1 << (s->ps.sps->bit_depth - 1));
429 71424 av_memcpy_backptr(dst + 2, 2, 2*(s->ps.sps->width >> s->ps.sps->hshift[i]) - 2);
430 }
431 }
432 }
433
434 64 frame->poc = poc;
435 64 frame->sequence = HEVC_SEQUENCE_COUNTER_INVALID;
436 64 frame->flags = 0;
437
438
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 64 times.
64 if (s->avctx->active_thread_type == FF_THREAD_FRAME)
439 ff_progress_frame_report(&frame->tf, INT_MAX);
440
441 64 return frame;
442 }
443
444 /* add a reference with the given poc to the list and mark it as used in DPB */
445 35142 static int add_candidate_ref(HEVCContext *s, RefPicList *list,
446 int poc, int ref_flag, uint8_t use_msb)
447 {
448 35142 HEVCFrame *ref = find_ref_idx(s, poc, use_msb);
449
450
2/4
✓ Branch 0 taken 35142 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 35142 times.
35142 if (ref == s->cur_frame || list->nb_refs >= HEVC_MAX_REFS)
451 return AVERROR_INVALIDDATA;
452
453
2/2
✓ Branch 0 taken 64 times.
✓ Branch 1 taken 35078 times.
35142 if (!ref) {
454 64 ref = generate_missing_ref(s, poc);
455
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 64 times.
64 if (!ref)
456 return AVERROR(ENOMEM);
457 }
458
459 35142 list->list[list->nb_refs] = ref->poc;
460 35142 list->ref[list->nb_refs] = ref;
461 35142 list->nb_refs++;
462
463 35142 mark_ref(ref, ref_flag);
464 35142 return 0;
465 }
466
467 9539 int ff_hevc_frame_rps(HEVCContext *s)
468 {
469 9539 const ShortTermRPS *short_rps = s->sh.short_term_rps;
470 9539 const LongTermRPS *long_rps = &s->sh.long_term_rps;
471 9539 RefPicList *rps = s->rps;
472 9539 int i, ret = 0;
473
474
2/2
✓ Branch 0 taken 382 times.
✓ Branch 1 taken 9157 times.
9539 if (!short_rps) {
475 382 rps[0].nb_refs = rps[1].nb_refs = 0;
476 382 return 0;
477 }
478
479 9157 unref_missing_refs(s);
480
481 /* clear the reference flags on all frames except the current one */
482
2/2
✓ Branch 0 taken 293024 times.
✓ Branch 1 taken 9157 times.
302181 for (i = 0; i < FF_ARRAY_ELEMS(s->DPB); i++) {
483 293024 HEVCFrame *frame = &s->DPB[i];
484
485
2/2
✓ Branch 0 taken 9157 times.
✓ Branch 1 taken 283867 times.
293024 if (frame == s->cur_frame)
486 9157 continue;
487
488 283867 mark_ref(frame, 0);
489 }
490
491
2/2
✓ Branch 0 taken 45785 times.
✓ Branch 1 taken 9157 times.
54942 for (i = 0; i < NB_RPS_TYPE; i++)
492 45785 rps[i].nb_refs = 0;
493
494 /* add the short refs */
495
2/2
✓ Branch 0 taken 33581 times.
✓ Branch 1 taken 9157 times.
42738 for (i = 0; i < short_rps->num_delta_pocs; i++) {
496 33581 int poc = s->poc + short_rps->delta_poc[i];
497 int list;
498
499
2/2
✓ Branch 0 taken 1726 times.
✓ Branch 1 taken 31855 times.
33581 if (!(short_rps->used & (1 << i)))
500 1726 list = ST_FOLL;
501
2/2
✓ Branch 0 taken 23225 times.
✓ Branch 1 taken 8630 times.
31855 else if (i < short_rps->num_negative_pics)
502 23225 list = ST_CURR_BEF;
503 else
504 8630 list = ST_CURR_AFT;
505
506 33581 ret = add_candidate_ref(s, &rps[list], poc, HEVC_FRAME_FLAG_SHORT_REF, 1);
507
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 33581 times.
33581 if (ret < 0)
508 goto fail;
509 }
510
511 /* add the long refs */
512
2/2
✓ Branch 0 taken 1561 times.
✓ Branch 1 taken 9157 times.
10718 for (i = 0; i < long_rps->nb_refs; i++) {
513 1561 int poc = long_rps->poc[i];
514
2/2
✓ Branch 0 taken 574 times.
✓ Branch 1 taken 987 times.
1561 int list = long_rps->used[i] ? LT_CURR : LT_FOLL;
515
516 1561 ret = add_candidate_ref(s, &rps[list], poc, HEVC_FRAME_FLAG_LONG_REF, long_rps->poc_msb_present[i]);
517
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1561 times.
1561 if (ret < 0)
518 goto fail;
519 }
520
521 9157 fail:
522 /* release any frames that are now unused */
523
2/2
✓ Branch 0 taken 293024 times.
✓ Branch 1 taken 9157 times.
302181 for (i = 0; i < FF_ARRAY_ELEMS(s->DPB); i++)
524 293024 ff_hevc_unref_frame(&s->DPB[i], 0);
525
526 9157 return ret;
527 }
528
529 18525 int ff_hevc_frame_nb_refs(const SliceHeader *sh, const HEVCPPS *pps)
530 {
531 18525 int ret = 0;
532 int i;
533 18525 const ShortTermRPS *rps = sh->short_term_rps;
534 18525 const LongTermRPS *long_rps = &sh->long_term_rps;
535
536
1/2
✓ Branch 0 taken 18525 times.
✗ Branch 1 not taken.
18525 if (rps) {
537
2/2
✓ Branch 0 taken 45435 times.
✓ Branch 1 taken 18525 times.
63960 for (i = 0; i < rps->num_negative_pics; i++)
538 45435 ret += !!(rps->used & (1 << i));
539
2/2
✓ Branch 0 taken 20428 times.
✓ Branch 1 taken 18525 times.
38953 for (; i < rps->num_delta_pocs; i++)
540 20428 ret += !!(rps->used & (1 << i));
541 }
542
543
1/2
✓ Branch 0 taken 18525 times.
✗ Branch 1 not taken.
18525 if (long_rps) {
544
2/2
✓ Branch 0 taken 1421 times.
✓ Branch 1 taken 18525 times.
19946 for (i = 0; i < long_rps->nb_refs; i++)
545 1421 ret += !!long_rps->used[i];
546 }
547
548
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 18525 times.
18525 if (pps->pps_curr_pic_ref_enabled_flag)
549 ret++;
550
551 18525 return ret;
552 }
553