FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/hevc_refs.c
Date: 2022-07-07 01:21:54
Exec Total Coverage
Lines: 266 299 89.0%
Branches: 186 230 80.9%

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/avassert.h"
25
26 #include "thread.h"
27 #include "hevc.h"
28 #include "hevcdec.h"
29 #include "threadframe.h"
30
31 338726 void ff_hevc_unref_frame(HEVCContext *s, HEVCFrame *frame, int flags)
32 {
33 /* frame->frame can be NULL if context init failed */
34
3/4
✓ Branch 0 taken 338726 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 273818 times.
✓ Branch 3 taken 64908 times.
338726 if (!frame->frame || !frame->frame->buf[0])
35 273818 return;
36
37 64908 frame->flags &= ~flags;
38
2/2
✓ Branch 0 taken 9605 times.
✓ Branch 1 taken 55303 times.
64908 if (!frame->flags) {
39 9605 ff_thread_release_ext_buffer(s->avctx, &frame->tf);
40 9605 ff_thread_release_buffer(s->avctx, frame->frame_grain);
41 9605 frame->needs_fg = 0;
42
43 9605 av_buffer_unref(&frame->tab_mvf_buf);
44 9605 frame->tab_mvf = NULL;
45
46 9605 av_buffer_unref(&frame->rpl_buf);
47 9605 av_buffer_unref(&frame->rpl_tab_buf);
48 9605 frame->rpl_tab = NULL;
49 9605 frame->refPicList = NULL;
50
51 9605 frame->collocated_ref = NULL;
52
53 9605 av_buffer_unref(&frame->hwaccel_priv_buf);
54 9605 frame->hwaccel_picture_private = NULL;
55 }
56 }
57
58 4570925 RefPicList *ff_hevc_get_ref_list(HEVCContext *s, HEVCFrame *ref, int x0, int y0)
59 {
60 4570925 int x_cb = x0 >> s->ps.sps->log2_ctb_size;
61 4570925 int y_cb = y0 >> s->ps.sps->log2_ctb_size;
62 4570925 int pic_width_cb = s->ps.sps->ctb_width;
63 4570925 int ctb_addr_ts = s->ps.pps->ctb_addr_rs_to_ts[y_cb * pic_width_cb + x_cb];
64 4570925 return (RefPicList *)ref->rpl_tab[ctb_addr_ts];
65 }
66
67 747 void ff_hevc_clear_refs(HEVCContext *s)
68 {
69 int i;
70
2/2
✓ Branch 0 taken 23904 times.
✓ Branch 1 taken 747 times.
24651 for (i = 0; i < FF_ARRAY_ELEMS(s->DPB); i++)
71 23904 ff_hevc_unref_frame(s, &s->DPB[i],
72 HEVC_FRAME_FLAG_SHORT_REF |
73 HEVC_FRAME_FLAG_LONG_REF);
74 747 }
75
76 5 void ff_hevc_flush_dpb(HEVCContext *s)
77 {
78 int i;
79
2/2
✓ Branch 0 taken 160 times.
✓ Branch 1 taken 5 times.
165 for (i = 0; i < FF_ARRAY_ELEMS(s->DPB); i++)
80 160 ff_hevc_unref_frame(s, &s->DPB[i], ~0);
81 5 }
82
83 9538 static HEVCFrame *alloc_frame(HEVCContext *s)
84 {
85 int i, j, ret;
86
1/2
✓ Branch 0 taken 33653 times.
✗ Branch 1 not taken.
33653 for (i = 0; i < FF_ARRAY_ELEMS(s->DPB); i++) {
87 33653 HEVCFrame *frame = &s->DPB[i];
88
2/2
✓ Branch 0 taken 24115 times.
✓ Branch 1 taken 9538 times.
33653 if (frame->frame->buf[0])
89 24115 continue;
90
91 9538 ret = ff_thread_get_ext_buffer(s->avctx, &frame->tf,
92 AV_GET_BUFFER_FLAG_REF);
93
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9538 times.
9538 if (ret < 0)
94 return NULL;
95
96 9538 frame->rpl_buf = av_buffer_allocz(s->pkt.nb_nals * sizeof(RefPicListTab));
97
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9538 times.
9538 if (!frame->rpl_buf)
98 goto fail;
99
100 9538 frame->tab_mvf_buf = av_buffer_pool_get(s->tab_mvf_pool);
101
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9538 times.
9538 if (!frame->tab_mvf_buf)
102 goto fail;
103 9538 frame->tab_mvf = (MvField *)frame->tab_mvf_buf->data;
104
105 9538 frame->rpl_tab_buf = av_buffer_pool_get(s->rpl_tab_pool);
106
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9538 times.
9538 if (!frame->rpl_tab_buf)
107 goto fail;
108 9538 frame->rpl_tab = (RefPicListTab **)frame->rpl_tab_buf->data;
109 9538 frame->ctb_count = s->ps.sps->ctb_width * s->ps.sps->ctb_height;
110
2/2
✓ Branch 0 taken 1474745 times.
✓ Branch 1 taken 9538 times.
1484283 for (j = 0; j < frame->ctb_count; j++)
111 1474745 frame->rpl_tab[j] = (RefPicListTab *)frame->rpl_buf->data;
112
113 9538 frame->frame->top_field_first = s->sei.picture_timing.picture_struct == AV_PICTURE_STRUCTURE_TOP_FIELD;
114
4/4
✓ Branch 0 taken 9523 times.
✓ Branch 1 taken 15 times.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 9521 times.
9538 frame->frame->interlaced_frame = (s->sei.picture_timing.picture_struct == AV_PICTURE_STRUCTURE_TOP_FIELD) || (s->sei.picture_timing.picture_struct == AV_PICTURE_STRUCTURE_BOTTOM_FIELD);
115
116
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9538 times.
9538 if (s->avctx->hwaccel) {
117 const AVHWAccel *hwaccel = s->avctx->hwaccel;
118 av_assert0(!frame->hwaccel_picture_private);
119 if (hwaccel->frame_priv_data_size) {
120 frame->hwaccel_priv_buf = av_buffer_allocz(hwaccel->frame_priv_data_size);
121 if (!frame->hwaccel_priv_buf)
122 goto fail;
123 frame->hwaccel_picture_private = frame->hwaccel_priv_buf->data;
124 }
125 }
126
127 9538 return frame;
128 fail:
129 ff_hevc_unref_frame(s, frame, ~0);
130 return NULL;
131 }
132 av_log(s->avctx, AV_LOG_ERROR, "Error allocating frame, DPB full.\n");
133 return NULL;
134 }
135
136 9488 int ff_hevc_set_new_ref(HEVCContext *s, AVFrame **frame, int poc)
137 {
138 HEVCFrame *ref;
139 int i;
140
141 /* check that this POC doesn't already exist */
142
2/2
✓ Branch 0 taken 303616 times.
✓ Branch 1 taken 9488 times.
313104 for (i = 0; i < FF_ARRAY_ELEMS(s->DPB); i++) {
143 303616 HEVCFrame *frame = &s->DPB[i];
144
145
4/4
✓ Branch 0 taken 45449 times.
✓ Branch 1 taken 258167 times.
✓ Branch 2 taken 45312 times.
✓ Branch 3 taken 137 times.
303616 if (frame->frame->buf[0] && frame->sequence == s->seq_decode &&
146
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 45312 times.
45312 frame->poc == poc) {
147 av_log(s->avctx, AV_LOG_ERROR, "Duplicate POC in a sequence: %d.\n",
148 poc);
149 return AVERROR_INVALIDDATA;
150 }
151 }
152
153 9488 ref = alloc_frame(s);
154
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9488 times.
9488 if (!ref)
155 return AVERROR(ENOMEM);
156
157 9488 *frame = ref->frame;
158 9488 s->ref = ref;
159
160
2/2
✓ Branch 0 taken 9482 times.
✓ Branch 1 taken 6 times.
9488 if (s->sh.pic_output_flag)
161 9482 ref->flags = HEVC_FRAME_FLAG_OUTPUT | HEVC_FRAME_FLAG_SHORT_REF;
162 else
163 6 ref->flags = HEVC_FRAME_FLAG_SHORT_REF;
164
165 9488 ref->poc = poc;
166 9488 ref->sequence = s->seq_decode;
167 9488 ref->frame->crop_left = s->ps.sps->output_window.left_offset;
168 9488 ref->frame->crop_right = s->ps.sps->output_window.right_offset;
169 9488 ref->frame->crop_top = s->ps.sps->output_window.top_offset;
170 9488 ref->frame->crop_bottom = s->ps.sps->output_window.bottom_offset;
171
172 9488 return 0;
173 }
174
175 10725 int ff_hevc_output_frame(HEVCContext *s, AVFrame *out, int flush)
176 {
177 761 do {
178 10725 int nb_output = 0;
179 10725 int min_poc = INT_MAX;
180 int i, min_idx, ret;
181
182
3/4
✓ Branch 0 taken 39 times.
✓ Branch 1 taken 10686 times.
✓ Branch 2 taken 39 times.
✗ Branch 3 not taken.
10725 if (s->sh.no_output_of_prior_pics_flag == 1 && s->no_rasl_output_flag == 1) {
183
2/2
✓ Branch 0 taken 1248 times.
✓ Branch 1 taken 39 times.
1287 for (i = 0; i < FF_ARRAY_ELEMS(s->DPB); i++) {
184 1248 HEVCFrame *frame = &s->DPB[i];
185
4/4
✓ Branch 0 taken 1241 times.
✓ Branch 1 taken 7 times.
✓ Branch 2 taken 780 times.
✓ Branch 3 taken 461 times.
1248 if (!(frame->flags & HEVC_FRAME_FLAG_BUMPING) && frame->poc != s->poc &&
186
2/2
✓ Branch 0 taken 238 times.
✓ Branch 1 taken 542 times.
780 frame->sequence == s->seq_output) {
187 238 ff_hevc_unref_frame(s, frame, HEVC_FRAME_FLAG_OUTPUT);
188 }
189 }
190 }
191
192
2/2
✓ Branch 0 taken 343200 times.
✓ Branch 1 taken 10725 times.
353925 for (i = 0; i < FF_ARRAY_ELEMS(s->DPB); i++) {
193 343200 HEVCFrame *frame = &s->DPB[i];
194
2/2
✓ Branch 0 taken 32755 times.
✓ Branch 1 taken 310445 times.
343200 if ((frame->flags & HEVC_FRAME_FLAG_OUTPUT) &&
195
2/2
✓ Branch 0 taken 31844 times.
✓ Branch 1 taken 911 times.
32755 frame->sequence == s->seq_output) {
196 31844 nb_output++;
197
3/4
✓ Branch 0 taken 15048 times.
✓ Branch 1 taken 16796 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 15048 times.
31844 if (frame->poc < min_poc || nb_output == 1) {
198 16796 min_poc = frame->poc;
199 16796 min_idx = i;
200 }
201 }
202 }
203
204 /* wait for more frames before output */
205
5/6
✓ Branch 0 taken 10243 times.
✓ Branch 1 taken 482 times.
✓ Branch 2 taken 9444 times.
✓ Branch 3 taken 799 times.
✓ Branch 4 taken 9444 times.
✗ Branch 5 not taken.
10725 if (!flush && s->seq_output == s->seq_decode && s->ps.sps &&
206
2/2
✓ Branch 0 taken 454 times.
✓ Branch 1 taken 8990 times.
9444 nb_output <= s->ps.sps->temporal_layer[s->ps.sps->max_sub_layers - 1].num_reorder_pics)
207 454 return 0;
208
209
2/2
✓ Branch 0 taken 9336 times.
✓ Branch 1 taken 935 times.
10271 if (nb_output) {
210 9336 HEVCFrame *frame = &s->DPB[min_idx];
211
212
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9336 times.
9336 ret = av_frame_ref(out, frame->needs_fg ? frame->frame_grain : frame->frame);
213
2/2
✓ Branch 0 taken 2039 times.
✓ Branch 1 taken 7297 times.
9336 if (frame->flags & HEVC_FRAME_FLAG_BUMPING)
214 2039 ff_hevc_unref_frame(s, frame, HEVC_FRAME_FLAG_OUTPUT | HEVC_FRAME_FLAG_BUMPING);
215 else
216 7297 ff_hevc_unref_frame(s, frame, HEVC_FRAME_FLAG_OUTPUT);
217
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9336 times.
9336 if (ret < 0)
218 return ret;
219
220
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 9336 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
9336 if (frame->needs_fg && (ret = av_frame_copy_props(out, frame->frame)) < 0)
221 return ret;
222
223
1/2
✓ Branch 0 taken 9336 times.
✗ Branch 1 not taken.
9336 if (!(s->avctx->export_side_data & AV_CODEC_EXPORT_DATA_FILM_GRAIN))
224 9336 av_frame_remove_side_data(out, AV_FRAME_DATA_FILM_GRAIN_PARAMS);
225
226 9336 av_log(s->avctx, AV_LOG_DEBUG,
227 "Output frame with POC %d.\n", frame->poc);
228 9336 return 1;
229 }
230
231
2/2
✓ Branch 0 taken 761 times.
✓ Branch 1 taken 174 times.
935 if (s->seq_output != s->seq_decode)
232 761 s->seq_output = (s->seq_output + 1) & 0xff;
233 else
234 174 break;
235 } while (1);
236
237 174 return 0;
238 }
239
240 8950 void ff_hevc_bump_frame(HEVCContext *s)
241 {
242 8950 int dpb = 0;
243 8950 int min_poc = INT_MAX;
244 int i;
245
246
2/2
✓ Branch 0 taken 286400 times.
✓ Branch 1 taken 8950 times.
295350 for (i = 0; i < FF_ARRAY_ELEMS(s->DPB); i++) {
247 286400 HEVCFrame *frame = &s->DPB[i];
248
2/2
✓ Branch 0 taken 47523 times.
✓ Branch 1 taken 238877 times.
286400 if ((frame->flags) &&
249
2/2
✓ Branch 0 taken 47407 times.
✓ Branch 1 taken 116 times.
47523 frame->sequence == s->seq_output &&
250
2/2
✓ Branch 0 taken 38494 times.
✓ Branch 1 taken 8913 times.
47407 frame->poc != s->poc) {
251 38494 dpb++;
252 }
253 }
254
255
3/4
✓ Branch 0 taken 8950 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1697 times.
✓ Branch 3 taken 7253 times.
8950 if (s->ps.sps && dpb >= s->ps.sps->temporal_layer[s->ps.sps->max_sub_layers - 1].max_dec_pic_buffering) {
256
2/2
✓ Branch 0 taken 54304 times.
✓ Branch 1 taken 1697 times.
56001 for (i = 0; i < FF_ARRAY_ELEMS(s->DPB); i++) {
257 54304 HEVCFrame *frame = &s->DPB[i];
258
2/2
✓ Branch 0 taken 10318 times.
✓ Branch 1 taken 43986 times.
54304 if ((frame->flags) &&
259
1/2
✓ Branch 0 taken 10318 times.
✗ Branch 1 not taken.
10318 frame->sequence == s->seq_output &&
260
2/2
✓ Branch 0 taken 8621 times.
✓ Branch 1 taken 1697 times.
10318 frame->poc != s->poc) {
261
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) {
262 1444 min_poc = frame->poc;
263 }
264 }
265 }
266
267
2/2
✓ Branch 0 taken 54304 times.
✓ Branch 1 taken 1697 times.
56001 for (i = 0; i < FF_ARRAY_ELEMS(s->DPB); i++) {
268 54304 HEVCFrame *frame = &s->DPB[i];
269
2/2
✓ Branch 0 taken 6790 times.
✓ Branch 1 taken 47514 times.
54304 if (frame->flags & HEVC_FRAME_FLAG_OUTPUT &&
270
1/2
✓ Branch 0 taken 6790 times.
✗ Branch 1 not taken.
6790 frame->sequence == s->seq_output &&
271
2/2
✓ Branch 0 taken 2789 times.
✓ Branch 1 taken 4001 times.
6790 frame->poc <= min_poc) {
272 2789 frame->flags |= HEVC_FRAME_FLAG_BUMPING;
273 }
274 }
275
276 1697 dpb--;
277 }
278 8950 }
279
280 18399 static int init_slice_rpl(HEVCContext *s)
281 {
282 18399 HEVCFrame *frame = s->ref;
283 18399 int ctb_count = frame->ctb_count;
284 18399 int ctb_addr_ts = s->ps.pps->ctb_addr_rs_to_ts[s->sh.slice_segment_addr];
285 int i;
286
287
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 18399 times.
18399 if (s->slice_idx >= frame->rpl_buf->size / sizeof(RefPicListTab))
288 return AVERROR_INVALIDDATA;
289
290
2/2
✓ Branch 0 taken 1956530 times.
✓ Branch 1 taken 18399 times.
1974929 for (i = ctb_addr_ts; i < ctb_count; i++)
291 1956530 frame->rpl_tab[i] = (RefPicListTab *)frame->rpl_buf->data + s->slice_idx;
292
293 18399 frame->refPicList = (RefPicList *)frame->rpl_tab[ctb_addr_ts];
294
295 18399 return 0;
296 }
297
298 18399 int ff_hevc_slice_rpl(HEVCContext *s)
299 {
300 18399 SliceHeader *sh = &s->sh;
301
302
2/2
✓ Branch 0 taken 14660 times.
✓ Branch 1 taken 3739 times.
18399 uint8_t nb_list = sh->slice_type == HEVC_SLICE_B ? 2 : 1;
303 uint8_t list_idx;
304 int i, j, ret;
305
306 18399 ret = init_slice_rpl(s);
307
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 18399 times.
18399 if (ret < 0)
308 return ret;
309
310 18399 if (!(s->rps[ST_CURR_BEF].nb_refs + s->rps[ST_CURR_AFT].nb_refs +
311
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 18399 times.
18399 s->rps[LT_CURR].nb_refs)) {
312 av_log(s->avctx, AV_LOG_ERROR, "Zero refs in the frame RPS.\n");
313 return AVERROR_INVALIDDATA;
314 }
315
316
2/2
✓ Branch 0 taken 33059 times.
✓ Branch 1 taken 18399 times.
51458 for (list_idx = 0; list_idx < nb_list; list_idx++) {
317 33059 RefPicList rpl_tmp = { { 0 } };
318 33059 RefPicList *rpl = &s->ref->refPicList[list_idx];
319
320 /* The order of the elements is
321 * ST_CURR_BEF - ST_CURR_AFT - LT_CURR for the L0 and
322 * ST_CURR_AFT - ST_CURR_BEF - LT_CURR for the L1 */
323 33059 int cand_lists[3] = { list_idx ? ST_CURR_AFT : ST_CURR_BEF,
324 33059 list_idx ? ST_CURR_BEF : ST_CURR_AFT,
325 LT_CURR };
326
327 /* concatenate the candidate lists for the current frame */
328
2/2
✓ Branch 0 taken 33077 times.
✓ Branch 1 taken 33059 times.
66136 while (rpl_tmp.nb_refs < sh->nb_refs[list_idx]) {
329
2/2
✓ Branch 0 taken 99231 times.
✓ Branch 1 taken 33077 times.
132308 for (i = 0; i < FF_ARRAY_ELEMS(cand_lists); i++) {
330 99231 RefPicList *rps = &s->rps[cand_lists[i]];
331
3/4
✓ Branch 0 taken 119642 times.
✓ Branch 1 taken 99231 times.
✓ Branch 2 taken 119642 times.
✗ Branch 3 not taken.
218873 for (j = 0; j < rps->nb_refs && rpl_tmp.nb_refs < HEVC_MAX_REFS; j++) {
332 119642 rpl_tmp.list[rpl_tmp.nb_refs] = rps->list[j];
333 119642 rpl_tmp.ref[rpl_tmp.nb_refs] = rps->ref[j];
334 119642 rpl_tmp.isLongTerm[rpl_tmp.nb_refs] = i == 2;
335 119642 rpl_tmp.nb_refs++;
336 }
337 }
338 }
339
340 /* reorder the references if necessary */
341
2/2
✓ Branch 0 taken 1827 times.
✓ Branch 1 taken 31232 times.
33059 if (sh->rpl_modification_flag[list_idx]) {
342
2/2
✓ Branch 0 taken 4333 times.
✓ Branch 1 taken 1827 times.
6160 for (i = 0; i < sh->nb_refs[list_idx]; i++) {
343 4333 int idx = sh->list_entry_lx[list_idx][i];
344
345
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4333 times.
4333 if (idx >= rpl_tmp.nb_refs) {
346 av_log(s->avctx, AV_LOG_ERROR, "Invalid reference index.\n");
347 return AVERROR_INVALIDDATA;
348 }
349
350 4333 rpl->list[i] = rpl_tmp.list[idx];
351 4333 rpl->ref[i] = rpl_tmp.ref[idx];
352 4333 rpl->isLongTerm[i] = rpl_tmp.isLongTerm[idx];
353 4333 rpl->nb_refs++;
354 }
355 } else {
356 31232 memcpy(rpl, &rpl_tmp, sizeof(*rpl));
357 31232 rpl->nb_refs = FFMIN(rpl->nb_refs, sh->nb_refs[list_idx]);
358 }
359
360
2/2
✓ Branch 0 taken 18399 times.
✓ Branch 1 taken 14660 times.
33059 if (sh->collocated_list == list_idx &&
361
1/2
✓ Branch 0 taken 18399 times.
✗ Branch 1 not taken.
18399 sh->collocated_ref_idx < rpl->nb_refs)
362 18399 s->ref->collocated_ref = rpl->ref[sh->collocated_ref_idx];
363 }
364
365 18399 return 0;
366 }
367
368 35045 static HEVCFrame *find_ref_idx(HEVCContext *s, int poc, uint8_t use_msb)
369 {
370
2/2
✓ Branch 0 taken 1512 times.
✓ Branch 1 taken 33533 times.
35045 int mask = use_msb ? ~0 : (1 << s->ps.sps->log2_max_poc_lsb) - 1;
371 int i;
372
373
2/2
✓ Branch 0 taken 151905 times.
✓ Branch 1 taken 50 times.
151955 for (i = 0; i < FF_ARRAY_ELEMS(s->DPB); i++) {
374 151905 HEVCFrame *ref = &s->DPB[i];
375
4/4
✓ Branch 0 taken 144536 times.
✓ Branch 1 taken 7369 times.
✓ Branch 2 taken 144436 times.
✓ Branch 3 taken 100 times.
151905 if (ref->frame->buf[0] && ref->sequence == s->seq_decode) {
376
2/2
✓ Branch 0 taken 34995 times.
✓ Branch 1 taken 109441 times.
144436 if ((ref->poc & mask) == poc)
377 34995 return ref;
378 }
379 }
380
381
7/8
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 38 times.
✓ Branch 2 taken 9 times.
✓ Branch 3 taken 3 times.
✓ Branch 4 taken 9 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 7 times.
✓ Branch 7 taken 2 times.
50 if (s->nal_unit_type != HEVC_NAL_CRA_NUT && !IS_BLA(s))
382 7 av_log(s->avctx, AV_LOG_ERROR,
383 "Could not find ref with POC %d\n", poc);
384 50 return NULL;
385 }
386
387 317610 static void mark_ref(HEVCFrame *frame, int flag)
388 {
389 317610 frame->flags &= ~(HEVC_FRAME_FLAG_LONG_REF | HEVC_FRAME_FLAG_SHORT_REF);
390 317610 frame->flags |= flag;
391 317610 }
392
393 50 static HEVCFrame *generate_missing_ref(HEVCContext *s, int poc)
394 {
395 HEVCFrame *frame;
396 int i, y;
397
398 50 frame = alloc_frame(s);
399
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 50 times.
50 if (!frame)
400 return NULL;
401
402
1/2
✓ Branch 0 taken 50 times.
✗ Branch 1 not taken.
50 if (!s->avctx->hwaccel) {
403
2/2
✓ Branch 0 taken 30 times.
✓ Branch 1 taken 20 times.
50 if (!s->ps.sps->pixel_shift) {
404
2/2
✓ Branch 0 taken 90 times.
✓ Branch 1 taken 30 times.
120 for (i = 0; frame->frame->data[i]; i++)
405 90 memset(frame->frame->data[i], 1 << (s->ps.sps->bit_depth - 1),
406 90 frame->frame->linesize[i] * AV_CEIL_RSHIFT(s->ps.sps->height, s->ps.sps->vshift[i]));
407 } else {
408
2/2
✓ Branch 0 taken 60 times.
✓ Branch 1 taken 20 times.
80 for (i = 0; frame->frame->data[i]; i++)
409
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++) {
410 71424 uint8_t *dst = frame->frame->data[i] + y * frame->frame->linesize[i];
411 71424 AV_WN16(dst, 1 << (s->ps.sps->bit_depth - 1));
412 71424 av_memcpy_backptr(dst + 2, 2, 2*(s->ps.sps->width >> s->ps.sps->hshift[i]) - 2);
413 }
414 }
415 }
416
417 50 frame->poc = poc;
418 50 frame->sequence = s->seq_decode;
419 50 frame->flags = 0;
420
421
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 50 times.
50 if (s->threads_type == FF_THREAD_FRAME)
422 ff_thread_report_progress(&frame->tf, INT_MAX, 0);
423
424 50 return frame;
425 }
426
427 /* add a reference with the given poc to the list and mark it as used in DPB */
428 35045 static int add_candidate_ref(HEVCContext *s, RefPicList *list,
429 int poc, int ref_flag, uint8_t use_msb)
430 {
431 35045 HEVCFrame *ref = find_ref_idx(s, poc, use_msb);
432
433
2/4
✓ Branch 0 taken 35045 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 35045 times.
35045 if (ref == s->ref || list->nb_refs >= HEVC_MAX_REFS)
434 return AVERROR_INVALIDDATA;
435
436
2/2
✓ Branch 0 taken 50 times.
✓ Branch 1 taken 34995 times.
35045 if (!ref) {
437 50 ref = generate_missing_ref(s, poc);
438
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 50 times.
50 if (!ref)
439 return AVERROR(ENOMEM);
440 }
441
442 35045 list->list[list->nb_refs] = ref->poc;
443 35045 list->ref[list->nb_refs] = ref;
444 35045 list->nb_refs++;
445
446 35045 mark_ref(ref, ref_flag);
447 35045 return 0;
448 }
449
450 9488 int ff_hevc_frame_rps(HEVCContext *s)
451 {
452 9488 const ShortTermRPS *short_rps = s->sh.short_term_rps;
453 9488 const LongTermRPS *long_rps = &s->sh.long_term_rps;
454 9488 RefPicList *rps = s->rps;
455 9488 int i, ret = 0;
456
457
2/2
✓ Branch 0 taken 373 times.
✓ Branch 1 taken 9115 times.
9488 if (!short_rps) {
458 373 rps[0].nb_refs = rps[1].nb_refs = 0;
459 373 return 0;
460 }
461
462 /* clear the reference flags on all frames except the current one */
463
2/2
✓ Branch 0 taken 291680 times.
✓ Branch 1 taken 9115 times.
300795 for (i = 0; i < FF_ARRAY_ELEMS(s->DPB); i++) {
464 291680 HEVCFrame *frame = &s->DPB[i];
465
466
2/2
✓ Branch 0 taken 9115 times.
✓ Branch 1 taken 282565 times.
291680 if (frame == s->ref)
467 9115 continue;
468
469 282565 mark_ref(frame, 0);
470 }
471
472
2/2
✓ Branch 0 taken 45575 times.
✓ Branch 1 taken 9115 times.
54690 for (i = 0; i < NB_RPS_TYPE; i++)
473 45575 rps[i].nb_refs = 0;
474
475 /* add the short refs */
476
2/2
✓ Branch 0 taken 33485 times.
✓ Branch 1 taken 9115 times.
42600 for (i = 0; i < short_rps->num_delta_pocs; i++) {
477 33485 int poc = s->poc + short_rps->delta_poc[i];
478 int list;
479
480
2/2
✓ Branch 0 taken 1719 times.
✓ Branch 1 taken 31766 times.
33485 if (!short_rps->used[i])
481 1719 list = ST_FOLL;
482
2/2
✓ Branch 0 taken 23171 times.
✓ Branch 1 taken 8595 times.
31766 else if (i < short_rps->num_negative_pics)
483 23171 list = ST_CURR_BEF;
484 else
485 8595 list = ST_CURR_AFT;
486
487 33485 ret = add_candidate_ref(s, &rps[list], poc, HEVC_FRAME_FLAG_SHORT_REF, 1);
488
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 33485 times.
33485 if (ret < 0)
489 goto fail;
490 }
491
492 /* add the long refs */
493
2/2
✓ Branch 0 taken 1560 times.
✓ Branch 1 taken 9115 times.
10675 for (i = 0; i < long_rps->nb_refs; i++) {
494 1560 int poc = long_rps->poc[i];
495
2/2
✓ Branch 0 taken 573 times.
✓ Branch 1 taken 987 times.
1560 int list = long_rps->used[i] ? LT_CURR : LT_FOLL;
496
497 1560 ret = add_candidate_ref(s, &rps[list], poc, HEVC_FRAME_FLAG_LONG_REF, long_rps->poc_msb_present[i]);
498
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1560 times.
1560 if (ret < 0)
499 goto fail;
500 }
501
502 9115 fail:
503 /* release any frames that are now unused */
504
2/2
✓ Branch 0 taken 291680 times.
✓ Branch 1 taken 9115 times.
300795 for (i = 0; i < FF_ARRAY_ELEMS(s->DPB); i++)
505 291680 ff_hevc_unref_frame(s, &s->DPB[i], 0);
506
507 9115 return ret;
508 }
509
510 18484 int ff_hevc_frame_nb_refs(const HEVCContext *s)
511 {
512 18484 int ret = 0;
513 int i;
514 18484 const ShortTermRPS *rps = s->sh.short_term_rps;
515 18484 const LongTermRPS *long_rps = &s->sh.long_term_rps;
516
517
1/2
✓ Branch 0 taken 18484 times.
✗ Branch 1 not taken.
18484 if (rps) {
518
2/2
✓ Branch 0 taken 45381 times.
✓ Branch 1 taken 18484 times.
63865 for (i = 0; i < rps->num_negative_pics; i++)
519 45381 ret += !!rps->used[i];
520
2/2
✓ Branch 0 taken 20388 times.
✓ Branch 1 taken 18484 times.
38872 for (; i < rps->num_delta_pocs; i++)
521 20388 ret += !!rps->used[i];
522 }
523
524
1/2
✓ Branch 0 taken 18484 times.
✗ Branch 1 not taken.
18484 if (long_rps) {
525
2/2
✓ Branch 0 taken 1419 times.
✓ Branch 1 taken 18484 times.
19903 for (i = 0; i < long_rps->nb_refs; i++)
526 1419 ret += !!long_rps->used[i];
527 }
528 18484 return ret;
529 }
530