FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/hevc_refs.c
Date: 2022-01-16 20:33:26
Exec Total Coverage
Lines: 262 298 87.9%
Branches: 184 230 80.0%

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