FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/h264_refs.c
Date: 2025-04-25 22:50:00
Exec Total Coverage
Lines: 416 478 87.0%
Functions: 22 22 100.0%
Branches: 315 409 77.0%

Line Branch Exec Source
1 /*
2 * H.26L/H.264/AVC/JVT/14496-10/... reference picture handling
3 * Copyright (c) 2003 Michael Niedermayer <michaelni@gmx.at>
4 *
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 /**
23 * @file
24 * H.264 / AVC / MPEG-4 part10 reference picture handling.
25 * @author Michael Niedermayer <michaelni@gmx.at>
26 */
27
28 #include <inttypes.h>
29
30 #include "libavutil/avassert.h"
31 #include "avcodec.h"
32 #include "h264.h"
33 #include "h264dec.h"
34 #include "golomb.h"
35 #include "mpegutils.h"
36
37 #include <assert.h>
38
39 67860 static void pic_as_field(H264Ref *pic, const int parity)
40 {
41
2/2
✓ Branch 0 taken 203580 times.
✓ Branch 1 taken 67860 times.
271440 for (int i = 0; i < FF_ARRAY_ELEMS(pic->data); ++i) {
42
2/2
✓ Branch 0 taken 97497 times.
✓ Branch 1 taken 106083 times.
203580 if (parity == PICT_BOTTOM_FIELD)
43 97497 pic->data[i] += pic->linesize[i];
44 203580 pic->reference = parity;
45 203580 pic->linesize[i] *= 2;
46 }
47 67860 pic->poc = pic->parent->field_poc[parity == PICT_BOTTOM_FIELD];
48 67860 }
49
50 215317 static void ref_from_h264pic(H264Ref *dst, const H264Picture *src)
51 {
52 215317 memcpy(dst->data, src->f->data, sizeof(dst->data));
53 215317 memcpy(dst->linesize, src->f->linesize, sizeof(dst->linesize));
54 215317 dst->reference = src->reference;
55 215317 dst->poc = src->poc;
56 215317 dst->pic_id = src->pic_id;
57 215317 dst->parent = src;
58 215317 }
59
60 202566 static int split_field_copy(H264Ref *dest, const H264Picture *src,
61 int parity, int id_add)
62 {
63 202566 int match = !!(src->reference & parity);
64
65
1/2
✓ Branch 0 taken 202566 times.
✗ Branch 1 not taken.
202566 if (match) {
66 202566 ref_from_h264pic(dest, src);
67
2/2
✓ Branch 0 taken 66245 times.
✓ Branch 1 taken 136321 times.
202566 if (parity != PICT_FRAME) {
68 66245 pic_as_field(dest, parity);
69 66245 dest->pic_id *= 2;
70 66245 dest->pic_id += id_add;
71 }
72 }
73
74 202566 return match;
75 }
76
77 101752 static int build_def_list(H264Ref *def, int def_len,
78 H264Picture * const *in, int len, int is_long, int sel)
79 {
80 101752 int i[2] = { 0 };
81 101752 int index = 0;
82
83
4/4
✓ Branch 0 taken 219778 times.
✓ Branch 1 taken 103623 times.
✓ Branch 2 taken 1871 times.
✓ Branch 3 taken 101752 times.
425153 while (i[0] < len || i[1] < len) {
84
6/6
✓ Branch 0 taken 982783 times.
✓ Branch 1 taken 53431 times.
✓ Branch 2 taken 812010 times.
✓ Branch 3 taken 170773 times.
✓ Branch 4 taken 2555 times.
✓ Branch 5 taken 168218 times.
1036214 while (i[0] < len && !(in[i[0]] && (in[i[0]]->reference & sel)))
85 814565 i[0]++;
86
6/6
✓ Branch 0 taken 982783 times.
✓ Branch 1 taken 187301 times.
✓ Branch 2 taken 812010 times.
✓ Branch 3 taken 170773 times.
✓ Branch 4 taken 136425 times.
✓ Branch 5 taken 34348 times.
1170084 while (i[1] < len && !(in[i[1]] && (in[i[1]]->reference & (sel ^ 3))))
87 948435 i[1]++;
88
2/2
✓ Branch 0 taken 168218 times.
✓ Branch 1 taken 53431 times.
221649 if (i[0] < len) {
89
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 168218 times.
168218 av_assert0(index < def_len);
90
2/2
✓ Branch 0 taken 2006 times.
✓ Branch 1 taken 166212 times.
168218 in[i[0]]->pic_id = is_long ? i[0] : in[i[0]]->frame_num;
91 168218 split_field_copy(&def[index++], in[i[0]++], sel, 1);
92 }
93
2/2
✓ Branch 0 taken 187301 times.
✓ Branch 1 taken 34348 times.
221649 if (i[1] < len) {
94
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 34348 times.
34348 av_assert0(index < def_len);
95
2/2
✓ Branch 0 taken 577 times.
✓ Branch 1 taken 33771 times.
34348 in[i[1]]->pic_id = is_long ? i[1] : in[i[1]]->frame_num;
96 34348 split_field_copy(&def[index++], in[i[1]++], sel ^ 3, 0);
97 }
98 }
99
100 101752 return index;
101 }
102
103 53164 static int add_sorted(H264Picture **sorted, H264Picture * const *src,
104 int len, int limit, int dir)
105 {
106 53164 int out_i = 0;
107
108 81316 for (;;) {
109
2/2
✓ Branch 0 taken 73412 times.
✓ Branch 1 taken 61068 times.
134480 int best_poc = dir ? INT_MIN : INT_MAX;
110
111
2/2
✓ Branch 0 taken 547180 times.
✓ Branch 1 taken 134480 times.
681660 for (int i = 0; i < len; i++) {
112 547180 const int poc = src[i]->poc;
113
4/4
✓ Branch 0 taken 159548 times.
✓ Branch 1 taken 387632 times.
✓ Branch 2 taken 93760 times.
✓ Branch 3 taken 65788 times.
547180 if (((poc > limit) ^ dir) && ((poc < best_poc) ^ dir)) {
114 93760 best_poc = poc;
115 93760 sorted[out_i] = src[i];
116 }
117 }
118
4/4
✓ Branch 0 taken 73412 times.
✓ Branch 1 taken 61068 times.
✓ Branch 2 taken 53164 times.
✓ Branch 3 taken 81316 times.
134480 if (best_poc == (dir ? INT_MIN : INT_MAX))
119 53164 break;
120 81316 limit = sorted[out_i++]->poc - dir;
121 }
122 53164 return out_i;
123 }
124
125 152517 static int mismatches_ref(const H264Context *h, const H264Picture *pic)
126 {
127 152517 const AVFrame *f = pic->f;
128 305034 return (h->cur_pic_ptr->f->width != f->width ||
129
2/4
✓ Branch 0 taken 152517 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 152517 times.
✗ Branch 3 not taken.
305034 h->cur_pic_ptr->f->height != f->height ||
130
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 152517 times.
152517 h->cur_pic_ptr->f->format != f->format);
131 }
132
133 37585 static void h264_initialise_ref_list(H264Context *h, H264SliceContext *sl)
134 {
135 int len;
136
137
2/2
✓ Branch 0 taken 13291 times.
✓ Branch 1 taken 24294 times.
37585 if (sl->slice_type_nos == AV_PICTURE_TYPE_B) {
138 H264Picture *sorted[32];
139 int cur_poc;
140 int lens[2];
141
142
2/2
✓ Branch 0 taken 4993 times.
✓ Branch 1 taken 8298 times.
13291 if (FIELD_PICTURE(h))
143 4993 cur_poc = h->cur_pic_ptr->field_poc[h->picture_structure == PICT_BOTTOM_FIELD];
144 else
145 8298 cur_poc = h->cur_pic_ptr->poc;
146
147
2/2
✓ Branch 0 taken 26582 times.
✓ Branch 1 taken 13291 times.
39873 for (int list = 0; list < 2; list++) {
148 26582 len = add_sorted(sorted, h->short_ref, h->short_ref_count, cur_poc, 1 ^ list);
149 26582 len += add_sorted(sorted + len, h->short_ref, h->short_ref_count, cur_poc, 0 ^ list);
150
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 26582 times.
26582 av_assert0(len <= 32);
151
152 26582 len = build_def_list(sl->ref_list[list], FF_ARRAY_ELEMS(sl->ref_list[0]),
153 sorted, len, 0, h->picture_structure);
154 53164 len += build_def_list(sl->ref_list[list] + len,
155 26582 FF_ARRAY_ELEMS(sl->ref_list[0]) - len,
156 26582 h->long_ref, 16, 1, h->picture_structure);
157
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 26582 times.
26582 av_assert0(len <= 32);
158
159
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 26577 times.
26582 if (len < sl->ref_count[list])
160 5 memset(&sl->ref_list[list][len], 0, sizeof(H264Ref) * (sl->ref_count[list] - len));
161 26582 lens[list] = len;
162 }
163
164
3/4
✓ Branch 0 taken 13291 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 11772 times.
✓ Branch 3 taken 1519 times.
13291 if (lens[0] == lens[1] && lens[1] > 1) {
165 int i;
166
2/2
✓ Branch 0 taken 13817 times.
✓ Branch 1 taken 1934 times.
15751 for (i = 0; i < lens[0] &&
167 13817 sl->ref_list[0][i].parent->f->buf[0]->buffer ==
168
2/2
✓ Branch 0 taken 3979 times.
✓ Branch 1 taken 9838 times.
13817 sl->ref_list[1][i].parent->f->buf[0]->buffer; i++);
169
2/2
✓ Branch 0 taken 1934 times.
✓ Branch 1 taken 9838 times.
11772 if (i == lens[0]) {
170 1934 FFSWAP(H264Ref, sl->ref_list[1][0], sl->ref_list[1][1]);
171 }
172 }
173 } else {
174 24294 len = build_def_list(sl->ref_list[0], FF_ARRAY_ELEMS(sl->ref_list[0]),
175 24294 h->short_ref, h->short_ref_count, 0, h->picture_structure);
176 48588 len += build_def_list(sl->ref_list[0] + len,
177 24294 FF_ARRAY_ELEMS(sl->ref_list[0]) - len,
178 24294 h-> long_ref, 16, 1, h->picture_structure);
179
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 24294 times.
24294 av_assert0(len <= 32);
180
181
2/2
✓ Branch 0 taken 904 times.
✓ Branch 1 taken 23390 times.
24294 if (len < sl->ref_count[0])
182 904 memset(&sl->ref_list[0][len], 0, sizeof(H264Ref) * (sl->ref_count[0] - len));
183 }
184 #ifdef TRACE
185 for (int i = 0; i < sl->ref_count[0]; i++) {
186 ff_tlog(h->avctx, "List0: %s fn:%d 0x%p\n",
187 (sl->ref_list[0][i].parent ? (sl->ref_list[0][i].parent->long_ref ? "LT" : "ST") : "??"),
188 sl->ref_list[0][i].pic_id,
189 sl->ref_list[0][i].data[0]);
190 }
191 if (sl->slice_type_nos == AV_PICTURE_TYPE_B) {
192 for (int i = 0; i < sl->ref_count[1]; i++) {
193 ff_tlog(h->avctx, "List1: %s fn:%d 0x%p\n",
194 (sl->ref_list[1][i].parent ? (sl->ref_list[1][i].parent->long_ref ? "LT" : "ST") : "??"),
195 sl->ref_list[1][i].pic_id,
196 sl->ref_list[1][i].data[0]);
197 }
198 }
199 #endif
200
201
4/4
✓ Branch 0 taken 39873 times.
✓ Branch 1 taken 48588 times.
✓ Branch 2 taken 50876 times.
✓ Branch 3 taken 37585 times.
88461 for (int j = 0; j < 1 + (sl->slice_type_nos == AV_PICTURE_TYPE_B); j++) {
202
2/2
✓ Branch 0 taken 141366 times.
✓ Branch 1 taken 50876 times.
192242 for (int i = 0; i < sl->ref_count[j]; i++) {
203
2/2
✓ Branch 0 taken 139766 times.
✓ Branch 1 taken 1600 times.
141366 if (sl->ref_list[j][i].parent) {
204
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 139766 times.
139766 if (mismatches_ref(h, sl->ref_list[j][i].parent)) {
205 av_log(h->avctx, AV_LOG_ERROR, "Discarding mismatching reference\n");
206 memset(&sl->ref_list[j][i], 0, sizeof(sl->ref_list[j][i]));
207 }
208 }
209 }
210 }
211
2/2
✓ Branch 0 taken 46398 times.
✓ Branch 1 taken 37585 times.
83983 for (int i = 0; i < sl->list_count; i++)
212 46398 h->default_ref[i] = sl->ref_list[i][0];
213 37585 }
214
215 /**
216 * print short term list
217 */
218 56871 static void print_short_term(const H264Context *h)
219 {
220
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 56871 times.
56871 if (h->avctx->debug & FF_DEBUG_MMCO) {
221 av_log(h->avctx, AV_LOG_DEBUG, "short term list:\n");
222 for (uint32_t i = 0; i < h->short_ref_count; i++) {
223 H264Picture *pic = h->short_ref[i];
224 av_log(h->avctx, AV_LOG_DEBUG, "%"PRIu32" fn:%d poc:%d %p\n",
225 i, pic->frame_num, pic->poc, pic->f->data[0]);
226 }
227 }
228 56871 }
229
230 /**
231 * print long term list
232 */
233 56871 static void print_long_term(const H264Context *h)
234 {
235
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 56871 times.
56871 if (h->avctx->debug & FF_DEBUG_MMCO) {
236 av_log(h->avctx, AV_LOG_DEBUG, "long term list:\n");
237 for (uint32_t i = 0; i < 16; i++) {
238 H264Picture *pic = h->long_ref[i];
239 if (pic) {
240 av_log(h->avctx, AV_LOG_DEBUG, "%"PRIu32" fn:%d poc:%d %p\n",
241 i, pic->frame_num, pic->poc, pic->f->data[0]);
242 }
243 }
244 }
245 56871 }
246
247 /**
248 * Extract structure information about the picture described by pic_num in
249 * the current decoding context (frame or field). Note that pic_num is
250 * picture number without wrapping (so, 0<=pic_num<max_pic_num).
251 * @param pic_num picture number for which to extract structure information
252 * @param structure one of PICT_XXX describing structure of picture
253 * with pic_num
254 * @return frame number (short term) or long term index of picture
255 * described by pic_num
256 */
257 29267 static int pic_num_extract(const H264Context *h, int pic_num, int *structure)
258 {
259 29267 *structure = h->picture_structure;
260
2/2
✓ Branch 0 taken 4131 times.
✓ Branch 1 taken 25136 times.
29267 if (FIELD_PICTURE(h)) {
261
2/2
✓ Branch 0 taken 2180 times.
✓ Branch 1 taken 1951 times.
4131 if (!(pic_num & 1))
262 /* opposite field */
263 2180 *structure ^= PICT_FRAME;
264 4131 pic_num >>= 1;
265 }
266
267 29267 return pic_num;
268 }
269
270 4975 static void h264_fill_mbaff_ref_list(H264SliceContext *sl)
271 {
272
2/2
✓ Branch 0 taken 6671 times.
✓ Branch 1 taken 4975 times.
11646 for (int list = 0; list < sl->list_count; list++) {
273
2/2
✓ Branch 0 taken 16042 times.
✓ Branch 1 taken 6671 times.
22713 for (int i = 0; i < sl->ref_count[list]; i++) {
274 16042 const H264Ref *frame = &sl->ref_list[list][i];
275 16042 H264Ref *field = &sl->ref_list[list][16 + 2 * i];
276
277 16042 field[0] = *frame;
278
279
2/2
✓ Branch 0 taken 48126 times.
✓ Branch 1 taken 16042 times.
64168 for (int j = 0; j < 3; j++)
280 48126 field[0].linesize[j] <<= 1;
281 16042 field[0].reference = PICT_TOP_FIELD;
282 16042 field[0].poc = field[0].parent->field_poc[0];
283
284 16042 field[1] = field[0];
285
286
2/2
✓ Branch 0 taken 48126 times.
✓ Branch 1 taken 16042 times.
64168 for (int j = 0; j < 3; j++)
287 48126 field[1].data[j] += frame->parent->f->linesize[j];
288 16042 field[1].reference = PICT_BOTTOM_FIELD;
289 16042 field[1].poc = field[1].parent->field_poc[1];
290 }
291 }
292 4975 }
293
294 37585 int ff_h264_build_ref_list(H264Context *h, H264SliceContext *sl)
295 {
296 37585 print_short_term(h);
297 37585 print_long_term(h);
298
299 37585 h264_initialise_ref_list(h, sl);
300
301
2/2
✓ Branch 0 taken 46398 times.
✓ Branch 1 taken 37585 times.
83983 for (int list = 0; list < sl->list_count; list++) {
302 46398 int pred = sl->curr_pic_num;
303
304
2/2
✓ Branch 0 taken 12751 times.
✓ Branch 1 taken 46398 times.
59149 for (int index = 0; index < sl->nb_ref_modifications[list]; index++) {
305 12751 unsigned int modification_of_pic_nums_idc = sl->ref_modifications[list][index].op;
306 12751 unsigned int val = sl->ref_modifications[list][index].val;
307 unsigned int pic_id;
308 int i, pic_structure;
309 12751 H264Picture *ref = NULL;
310
311
2/3
✓ Branch 0 taken 12188 times.
✓ Branch 1 taken 563 times.
✗ Branch 2 not taken.
12751 switch (modification_of_pic_nums_idc) {
312 12188 case 0:
313 case 1: {
314 12188 const unsigned int abs_diff_pic_num = val + 1;
315 int frame_num;
316
317
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12188 times.
12188 if (abs_diff_pic_num > sl->max_pic_num) {
318 av_log(h->avctx, AV_LOG_ERROR,
319 "abs_diff_pic_num overflow\n");
320 return AVERROR_INVALIDDATA;
321 }
322
323
2/2
✓ Branch 0 taken 9889 times.
✓ Branch 1 taken 2299 times.
12188 if (modification_of_pic_nums_idc == 0)
324 9889 pred -= abs_diff_pic_num;
325 else
326 2299 pred += abs_diff_pic_num;
327 12188 pred &= sl->max_pic_num - 1;
328
329 12188 frame_num = pic_num_extract(h, pred, &pic_structure);
330
331
1/2
✓ Branch 0 taken 39971 times.
✗ Branch 1 not taken.
39971 for (i = h->short_ref_count - 1; i >= 0; i--) {
332 39971 ref = h->short_ref[i];
333 assert(ref->reference);
334 assert(!ref->long_ref);
335
2/2
✓ Branch 0 taken 12188 times.
✓ Branch 1 taken 27783 times.
39971 if (ref->frame_num == frame_num &&
336
1/2
✓ Branch 0 taken 12188 times.
✗ Branch 1 not taken.
12188 (ref->reference & pic_structure))
337 12188 break;
338 }
339
1/2
✓ Branch 0 taken 12188 times.
✗ Branch 1 not taken.
12188 if (i >= 0)
340 12188 pic_id = pred;
341 12188 break;
342 }
343 563 case 2: {
344 int long_idx;
345 563 pic_id = val; // long_term_pic_idx
346
347 563 long_idx = pic_num_extract(h, pic_id, &pic_structure);
348
349
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 563 times.
563 if (long_idx > 31U) {
350 av_log(h->avctx, AV_LOG_ERROR,
351 "long_term_pic_idx overflow\n");
352 return AVERROR_INVALIDDATA;
353 }
354 563 ref = h->long_ref[long_idx];
355 assert(!(ref && !ref->reference));
356
2/4
✓ Branch 0 taken 563 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 563 times.
✗ Branch 3 not taken.
563 if (ref && (ref->reference & pic_structure)) {
357 assert(ref->long_ref);
358 563 i = 0;
359 } else {
360 i = -1;
361 }
362 563 break;
363 }
364 default:
365 av_assert0(0);
366 }
367
368
2/4
✓ Branch 0 taken 12751 times.
✗ Branch 1 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 12751 times.
12751 if (i < 0 || mismatches_ref(h, ref)) {
369 av_log(h->avctx, AV_LOG_ERROR,
370 i < 0 ? "reference picture missing during reorder\n" :
371 "mismatching reference\n"
372 );
373 memset(&sl->ref_list[list][index], 0, sizeof(sl->ref_list[0][0])); // FIXME
374 } else {
375
2/2
✓ Branch 0 taken 19438 times.
✓ Branch 1 taken 5372 times.
24810 for (i = index; i + 1 < sl->ref_count[list]; i++) {
376
2/2
✓ Branch 0 taken 17757 times.
✓ Branch 1 taken 1681 times.
19438 if (sl->ref_list[list][i].parent &&
377
2/2
✓ Branch 0 taken 15818 times.
✓ Branch 1 taken 1939 times.
17757 ref->long_ref == sl->ref_list[list][i].parent->long_ref &&
378
2/2
✓ Branch 0 taken 7379 times.
✓ Branch 1 taken 8439 times.
15818 pic_id == sl->ref_list[list][i].pic_id)
379 7379 break;
380 }
381
2/2
✓ Branch 0 taken 12059 times.
✓ Branch 1 taken 12751 times.
24810 for (; i > index; i--) {
382 12059 sl->ref_list[list][i] = sl->ref_list[list][i - 1];
383 }
384 12751 ref_from_h264pic(&sl->ref_list[list][index], ref);
385
2/2
✓ Branch 0 taken 1615 times.
✓ Branch 1 taken 11136 times.
12751 if (FIELD_PICTURE(h)) {
386 1615 pic_as_field(&sl->ref_list[list][index], pic_structure);
387 }
388 }
389 }
390 }
391
2/2
✓ Branch 0 taken 46398 times.
✓ Branch 1 taken 37585 times.
83983 for (int list = 0; list < sl->list_count; list++) {
392
2/2
✓ Branch 0 taken 141366 times.
✓ Branch 1 taken 46398 times.
187764 for (int index = 0; index < sl->ref_count[list]; index++) {
393
2/2
✓ Branch 0 taken 141351 times.
✓ Branch 1 taken 15 times.
141366 if ( !sl->ref_list[list][index].parent
394
3/4
✓ Branch 0 taken 99290 times.
✓ Branch 1 taken 42061 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 99290 times.
141351 || (!FIELD_PICTURE(h) && (sl->ref_list[list][index].reference&3) != 3)) {
395 15 av_log(h->avctx, AV_LOG_ERROR, "Missing reference picture, default is %d\n", h->default_ref[list].poc);
396
397
2/2
✓ Branch 0 taken 240 times.
✓ Branch 1 taken 15 times.
255 for (int i = 0; i < FF_ARRAY_ELEMS(h->last_pocs); i++)
398 240 h->last_pocs[i] = INT_MIN;
399
1/2
✓ Branch 0 taken 15 times.
✗ Branch 1 not taken.
15 if (h->default_ref[list].parent
400
2/4
✓ Branch 0 taken 15 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 15 times.
✗ Branch 3 not taken.
15 && !(!FIELD_PICTURE(h) && (h->default_ref[list].reference&3) != 3))
401 15 sl->ref_list[list][index] = h->default_ref[list];
402 else
403 return -1;
404 }
405
5/6
✓ Branch 0 taken 141366 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 943 times.
✓ Branch 3 taken 140423 times.
✓ Branch 4 taken 42 times.
✓ Branch 5 taken 901 times.
141366 if (h->noref_gray>0 && sl->ref_list[list][index].parent->gray && h->non_gray) {
406
1/2
✓ Branch 0 taken 84 times.
✗ Branch 1 not taken.
84 for (int j=0; j<sl->list_count; j++) {
407 84 int list2 = (list+j)&1;
408
3/4
✓ Branch 0 taken 84 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 42 times.
✓ Branch 3 taken 42 times.
84 if (h->default_ref[list2].parent && !h->default_ref[list2].parent->gray
409
3/4
✓ Branch 0 taken 36 times.
✓ Branch 1 taken 6 times.
✓ Branch 2 taken 36 times.
✗ Branch 3 not taken.
42 && !(!FIELD_PICTURE(h) && (h->default_ref[list2].reference&3) != 3)) {
410 42 sl->ref_list[list][index] = h->default_ref[list2];
411 42 av_log(h->avctx, AV_LOG_DEBUG, "replacement of gray gap frame\n");
412 42 break;
413 }
414 }
415 }
416
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 141366 times.
141366 av_assert0(av_buffer_get_ref_count(sl->ref_list[list][index].parent->f->buf[0]) > 0);
417 }
418 }
419
420
2/2
✓ Branch 0 taken 4975 times.
✓ Branch 1 taken 32610 times.
37585 if (FRAME_MBAFF(h))
421 4975 h264_fill_mbaff_ref_list(sl);
422
423 37585 return 0;
424 }
425
426 33187 int ff_h264_decode_ref_pic_list_reordering(H264SliceContext *sl, void *logctx)
427 {
428 33187 sl->nb_ref_modifications[0] = 0;
429 33187 sl->nb_ref_modifications[1] = 0;
430
431
2/2
✓ Branch 0 taken 46522 times.
✓ Branch 1 taken 33187 times.
79709 for (int list = 0; list < sl->list_count; list++) {
432
2/2
✓ Branch 1 taken 42861 times.
✓ Branch 2 taken 3661 times.
46522 if (!get_bits1(&sl->gb)) // ref_pic_list_modification_flag_l[01]
433 42861 continue;
434
435 3661 for (int index = 0; ; index++) {
436 16484 unsigned int op = get_ue_golomb_31(&sl->gb);
437
438
2/2
✓ Branch 0 taken 3661 times.
✓ Branch 1 taken 12823 times.
16484 if (op == 3)
439 3661 break;
440
441
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12823 times.
12823 if (index >= sl->ref_count[list]) {
442 av_log(logctx, AV_LOG_ERROR, "reference count overflow\n");
443 return AVERROR_INVALIDDATA;
444
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12823 times.
12823 } else if (op > 2) {
445 av_log(logctx, AV_LOG_ERROR,
446 "illegal modification_of_pic_nums_idc %u\n",
447 op);
448 return AVERROR_INVALIDDATA;
449 }
450 12823 sl->ref_modifications[list][index].val = get_ue_golomb_long(&sl->gb);
451 12823 sl->ref_modifications[list][index].op = op;
452 12823 sl->nb_ref_modifications[list]++;
453 }
454 }
455
456 33187 return 0;
457 }
458
459 /**
460 * Mark a picture as no longer needed for reference. The refmask
461 * argument allows unreferencing of individual fields or the whole frame.
462 * If the picture becomes entirely unreferenced, but is being held for
463 * display purposes, it is marked as such.
464 * @param refmask mask of fields to unreference; the mask is bitwise
465 * anded with the reference marking of pic
466 * @return non-zero if pic becomes entirely unreferenced (except possibly
467 * for display purposes) zero if one of the fields remains in
468 * reference
469 */
470 19032 static inline int unreference_pic(H264Context *h, H264Picture *pic, int refmask)
471 {
472
2/2
✓ Branch 0 taken 1226 times.
✓ Branch 1 taken 17806 times.
19032 if (pic->reference &= refmask) {
473 1226 return 0;
474 } else {
475
2/2
✓ Branch 0 taken 19409 times.
✓ Branch 1 taken 16327 times.
35736 for (int i = 0; h->delayed_pic[i]; i++)
476
2/2
✓ Branch 0 taken 1479 times.
✓ Branch 1 taken 17930 times.
19409 if(pic == h->delayed_pic[i]){
477 1479 pic->reference = DELAYED_PIC_REF;
478 1479 break;
479 }
480 17806 return 1;
481 }
482 }
483
484 /**
485 * Find a H264Picture in the short term reference list by frame number.
486 * @param frame_num frame number to search for
487 * @param idx the index into h->short_ref where returned picture is found
488 * undefined if no picture found.
489 * @return pointer to the found picture, or NULL if no pic with the provided
490 * frame number is found
491 */
492 50318 static H264Picture *find_short(H264Context *h, int frame_num, int *idx)
493 {
494
2/2
✓ Branch 0 taken 195462 times.
✓ Branch 1 taken 17780 times.
213242 for (int i = 0; i < h->short_ref_count; i++) {
495 195462 H264Picture *pic = h->short_ref[i];
496
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 195462 times.
195462 if (h->avctx->debug & FF_DEBUG_MMCO)
497 av_log(h->avctx, AV_LOG_DEBUG, "%d %d %p\n", i, pic->frame_num, pic);
498
2/2
✓ Branch 0 taken 32538 times.
✓ Branch 1 taken 162924 times.
195462 if (pic->frame_num == frame_num) {
499 32538 *idx = i;
500 32538 return pic;
501 }
502 }
503 17780 return NULL;
504 }
505
506 /**
507 * Remove a picture from the short term reference list by its index in
508 * that list. This does no checking on the provided index; it is assumed
509 * to be valid. Other list entries are shifted down.
510 * @param i index into h->short_ref of picture to remove.
511 */
512 15242 static void remove_short_at_index(H264Context *h, int i)
513 {
514 assert(i >= 0 && i < h->short_ref_count);
515 15242 h->short_ref[i] = NULL;
516
2/2
✓ Branch 0 taken 11715 times.
✓ Branch 1 taken 3527 times.
15242 if (--h->short_ref_count)
517 11715 memmove(&h->short_ref[i], &h->short_ref[i + 1],
518 11715 (h->short_ref_count - i) * sizeof(H264Picture*));
519 15242 }
520
521 /**
522 * @return the removed picture or NULL if an error occurs
523 */
524 33884 static H264Picture *remove_short(H264Context *h, int frame_num, int ref_mask)
525 {
526 H264Picture *pic;
527 int i;
528
529
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 33884 times.
33884 if (h->avctx->debug & FF_DEBUG_MMCO)
530 av_log(h->avctx, AV_LOG_DEBUG, "remove short %d count %d\n", frame_num, h->short_ref_count);
531
532 33884 pic = find_short(h, frame_num, &i);
533
2/2
✓ Branch 0 taken 16147 times.
✓ Branch 1 taken 17737 times.
33884 if (pic) {
534
2/2
✓ Branch 1 taken 14921 times.
✓ Branch 2 taken 1226 times.
16147 if (unreference_pic(h, pic, ref_mask))
535 14921 remove_short_at_index(h, i);
536 }
537
538 33884 return pic;
539 }
540
541 /**
542 * Remove a picture from the long term reference list by its index in
543 * that list.
544 * @return the removed picture or NULL if an error occurs
545 */
546 44367 static H264Picture *remove_long(H264Context *h, int i, int ref_mask)
547 {
548 H264Picture *pic;
549
550 44367 pic = h->long_ref[i];
551
2/2
✓ Branch 0 taken 342 times.
✓ Branch 1 taken 44025 times.
44367 if (pic) {
552
1/2
✓ Branch 1 taken 342 times.
✗ Branch 2 not taken.
342 if (unreference_pic(h, pic, ref_mask)) {
553 assert(h->long_ref[i]->long_ref == 1);
554 342 h->long_ref[i]->long_ref = 0;
555 342 h->long_ref[i] = NULL;
556 342 h->long_ref_count--;
557 }
558 }
559
560 44367 return pic;
561 }
562
563 2352 void ff_h264_remove_all_refs(H264Context *h)
564 {
565
2/2
✓ Branch 0 taken 37632 times.
✓ Branch 1 taken 2352 times.
39984 for (int i = 0; i < 16; i++)
566 37632 remove_long(h, i, 0);
567 assert(h->long_ref_count == 0);
568
569
3/4
✓ Branch 0 taken 1134 times.
✓ Branch 1 taken 1218 times.
✓ Branch 2 taken 1134 times.
✗ Branch 3 not taken.
2352 if (h->short_ref_count && !h->last_pic_for_ec.f->data[0]) {
570 1134 ff_h264_unref_picture(&h->last_pic_for_ec);
571 1134 ff_h264_ref_picture(&h->last_pic_for_ec, h->short_ref[0]);
572 }
573
574
2/2
✓ Branch 0 taken 2543 times.
✓ Branch 1 taken 2352 times.
4895 for (int i = 0; i < h->short_ref_count; i++) {
575 2543 unreference_pic(h, h->short_ref[i], 0);
576 2543 h->short_ref[i] = NULL;
577 }
578 2352 h->short_ref_count = 0;
579
580 2352 memset(h->default_ref, 0, sizeof(h->default_ref));
581 2352 }
582
583 16603 static void generate_sliding_window_mmcos(H264Context *h)
584 {
585 16603 MMCO *mmco = h->mmco;
586 16603 int nb_mmco = 0;
587
588
2/2
✓ Branch 0 taken 16569 times.
✓ Branch 1 taken 34 times.
16603 if (h->short_ref_count &&
589
2/2
✓ Branch 0 taken 14004 times.
✓ Branch 1 taken 2565 times.
16569 h->long_ref_count + h->short_ref_count >= h->ps.sps->ref_frame_count &&
590
5/6
✓ Branch 0 taken 2243 times.
✓ Branch 1 taken 11761 times.
✓ Branch 2 taken 1198 times.
✓ Branch 3 taken 1045 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 1198 times.
14004 !(FIELD_PICTURE(h) && !h->first_field && h->cur_pic_ptr->reference)) {
591 12806 mmco[0].opcode = MMCO_SHORT2UNUSED;
592 12806 mmco[0].short_pic_num = h->short_ref[h->short_ref_count - 1]->frame_num;
593 12806 nb_mmco = 1;
594
2/2
✓ Branch 0 taken 1045 times.
✓ Branch 1 taken 11761 times.
12806 if (FIELD_PICTURE(h)) {
595 1045 mmco[0].short_pic_num *= 2;
596 1045 mmco[1].opcode = MMCO_SHORT2UNUSED;
597 1045 mmco[1].short_pic_num = mmco[0].short_pic_num + 1;
598 1045 nb_mmco = 2;
599 }
600 }
601
602 16603 h->nb_mmco = nb_mmco;
603 16603 }
604
605 19286 int ff_h264_execute_ref_pic_marking(H264Context *h)
606 {
607 19286 MMCO *mmco = h->mmco;
608 int mmco_count;
609 19286 int pps_ref_count[2] = {0};
610 19286 int current_ref_assigned = 0, err = 0;
611
612
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 19286 times.
19286 if (!h->ps.sps) {
613 av_log(h->avctx, AV_LOG_ERROR, "SPS is unset\n");
614 err = AVERROR_INVALIDDATA;
615 goto out;
616 }
617
618
2/2
✓ Branch 0 taken 16603 times.
✓ Branch 1 taken 2683 times.
19286 if (!h->explicit_ref_marking)
619 16603 generate_sliding_window_mmcos(h);
620 19286 mmco_count = h->nb_mmco;
621
622
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 19286 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
19286 if ((h->avctx->debug & FF_DEBUG_MMCO) && mmco_count == 0)
623 av_log(h->avctx, AV_LOG_DEBUG, "no mmco here\n");
624
625
2/2
✓ Branch 0 taken 16959 times.
✓ Branch 1 taken 19286 times.
36245 for (int i = 0; i < mmco_count; i++) {
626
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 16959 times.
16959 if (h->avctx->debug & FF_DEBUG_MMCO)
627 av_log(h->avctx, AV_LOG_DEBUG, "mmco:%d %d %d\n", h->mmco[i].opcode,
628 h->mmco[i].short_pic_num, h->mmco[i].long_arg);
629
630
5/6
✓ Branch 0 taken 16434 times.
✓ Branch 1 taken 82 times.
✓ Branch 2 taken 21 times.
✓ Branch 3 taken 407 times.
✓ Branch 4 taken 15 times.
✗ Branch 5 not taken.
16959 switch (mmco[i].opcode) {
631 16434 case MMCO_SHORT2UNUSED:
632 case MMCO_SHORT2LONG: {
633 int structure, j;
634 16434 int frame_num = pic_num_extract(h, mmco[i].short_pic_num, &structure);
635 16434 H264Picture *pic = find_short(h, frame_num, &j);
636
637
2/2
✓ Branch 0 taken 43 times.
✓ Branch 1 taken 16391 times.
16434 if (!pic) {
638
2/2
✓ Branch 0 taken 21 times.
✓ Branch 1 taken 22 times.
43 if (mmco[i].opcode != MMCO_SHORT2LONG ||
639
1/2
✓ Branch 0 taken 21 times.
✗ Branch 1 not taken.
21 !h->long_ref[mmco[i].long_arg] ||
640
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 21 times.
21 h->long_ref[mmco[i].long_arg]->frame_num != frame_num) {
641
1/2
✓ Branch 0 taken 22 times.
✗ Branch 1 not taken.
22 av_log(h->avctx, h->short_ref_count ? AV_LOG_ERROR : AV_LOG_DEBUG, "mmco: unref short failure\n");
642 22 err = AVERROR_INVALIDDATA;
643 }
644 43 continue;
645 }
646
2/2
✓ Branch 0 taken 16070 times.
✓ Branch 1 taken 321 times.
16391 if (mmco[i].opcode == MMCO_SHORT2UNUSED) {
647
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 16070 times.
16070 if (h->avctx->debug & FF_DEBUG_MMCO)
648 av_log(h->avctx, AV_LOG_DEBUG, "mmco: unref short %d count %d\n",
649 h->mmco[i].short_pic_num, h->short_ref_count);
650 16070 remove_short(h, frame_num, structure ^ PICT_FRAME);
651 } else {
652
1/2
✓ Branch 0 taken 321 times.
✗ Branch 1 not taken.
321 if (h->long_ref[mmco[i].long_arg] != pic)
653 321 remove_long(h, mmco[i].long_arg, 0);
654
655 321 remove_short_at_index(h, j);
656 321 h->long_ref[ mmco[i].long_arg ] = pic;
657
1/2
✓ Branch 0 taken 321 times.
✗ Branch 1 not taken.
321 if (h->long_ref[mmco[i].long_arg]) {
658 321 h->long_ref[mmco[i].long_arg]->long_ref = 1;
659 321 h->long_ref_count++;
660 }
661 }
662 16391 break;
663 }
664 82 case MMCO_LONG2UNUSED: {
665 82 int structure, j = pic_num_extract(h, mmco[i].long_arg, &structure);
666 82 H264Picture *pic = h->long_ref[j];
667
1/2
✓ Branch 0 taken 82 times.
✗ Branch 1 not taken.
82 if (pic) {
668 82 remove_long(h, j, structure ^ PICT_FRAME);
669 } else if (h->avctx->debug & FF_DEBUG_MMCO)
670 av_log(h->avctx, AV_LOG_DEBUG, "mmco: unref long failure\n");
671 82 break;
672 }
673 21 case MMCO_LONG:
674 // Comment below left from previous code as it is an interesting note.
675 /* First field in pair is in short term list or
676 * at a different long term index.
677 * This is not allowed; see 7.4.3.3, notes 2 and 3.
678 * Report the problem and keep the pair where it is,
679 * and mark this field valid.
680 */
681
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 21 times.
21 if (h->short_ref[0] == h->cur_pic_ptr) {
682 av_log(h->avctx, AV_LOG_ERROR, "mmco: cannot assign current picture to short and long at the same time\n");
683 remove_short_at_index(h, 0);
684 }
685
686 /* make sure the current picture is not already assigned as a long ref */
687
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 21 times.
21 if (h->cur_pic_ptr->long_ref) {
688 for (int j = 0; j < FF_ARRAY_ELEMS(h->long_ref); j++) {
689 if (h->long_ref[j] == h->cur_pic_ptr) {
690 if (j != mmco[i].long_arg)
691 av_log(h->avctx, AV_LOG_ERROR, "mmco: cannot assign current picture to 2 long term references\n");
692 remove_long(h, j, 0);
693 }
694 }
695 }
696
697
1/2
✓ Branch 0 taken 21 times.
✗ Branch 1 not taken.
21 if (h->long_ref[mmco[i].long_arg] != h->cur_pic_ptr) {
698
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 21 times.
21 av_assert0(!h->cur_pic_ptr->long_ref);
699 21 remove_long(h, mmco[i].long_arg, 0);
700
701 21 h->long_ref[mmco[i].long_arg] = h->cur_pic_ptr;
702 21 h->long_ref[mmco[i].long_arg]->long_ref = 1;
703 21 h->long_ref_count++;
704 }
705
706 21 h->cur_pic_ptr->reference |= h->picture_structure;
707 21 current_ref_assigned = 1;
708 21 break;
709 407 case MMCO_SET_MAX_LONG:
710 assert(mmco[i].long_arg <= 16);
711 // just remove the long term which index is greater than new max
712
2/2
✓ Branch 0 taken 6071 times.
✓ Branch 1 taken 407 times.
6478 for (int j = mmco[i].long_arg; j < 16; j++)
713 6071 remove_long(h, j, 0);
714 407 break;
715 15 case MMCO_RESET:
716
2/2
✓ Branch 0 taken 53 times.
✓ Branch 1 taken 15 times.
68 while (h->short_ref_count) {
717 53 remove_short(h, h->short_ref[0]->frame_num, 0);
718 }
719
2/2
✓ Branch 0 taken 240 times.
✓ Branch 1 taken 15 times.
255 for (int j = 0; j < 16; j++)
720 240 remove_long(h, j, 0);
721 15 h->poc.frame_num = h->cur_pic_ptr->frame_num = 0;
722 15 h->mmco_reset = 1;
723 15 h->cur_pic_ptr->mmco_reset = 1;
724
2/2
✓ Branch 0 taken 240 times.
✓ Branch 1 taken 15 times.
255 for (int j = 0; j < FF_ARRAY_ELEMS(h->last_pocs); j++)
725 240 h->last_pocs[j] = INT_MIN;
726 15 break;
727 default: av_assert0(0);
728 }
729 }
730
731
2/2
✓ Branch 0 taken 19265 times.
✓ Branch 1 taken 21 times.
19286 if (!current_ref_assigned) {
732 /* Second field of complementary field pair; the first field of
733 * which is already referenced. If short referenced, it
734 * should be first entry in short_ref. If not, it must exist
735 * in long_ref; trying to put it on the short list here is an
736 * error in the encoded bit stream (ref: 7.4.3.3, NOTE 2 and 3).
737 */
738
4/4
✓ Branch 0 taken 14620 times.
✓ Branch 1 taken 4645 times.
✓ Branch 2 taken 1528 times.
✓ Branch 3 taken 13092 times.
19265 if (h->short_ref_count && h->short_ref[0] == h->cur_pic_ptr) {
739 /* Just mark the second field valid */
740 1528 h->cur_pic_ptr->reference |= h->picture_structure;
741
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 17737 times.
17737 } else if (h->cur_pic_ptr->long_ref) {
742 av_log(h->avctx, AV_LOG_ERROR, "illegal short term reference "
743 "assignment for second field "
744 "in complementary field pair "
745 "(first field is long term)\n");
746 err = AVERROR_INVALIDDATA;
747 } else {
748 17737 H264Picture *pic = remove_short(h, h->cur_pic_ptr->frame_num, 0);
749
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 17737 times.
17737 if (pic) {
750 av_log(h->avctx, AV_LOG_ERROR, "illegal short term buffer state detected\n");
751 err = AVERROR_INVALIDDATA;
752 }
753
754
2/2
✓ Branch 0 taken 13092 times.
✓ Branch 1 taken 4645 times.
17737 if (h->short_ref_count)
755 13092 memmove(&h->short_ref[1], &h->short_ref[0],
756 13092 h->short_ref_count * sizeof(H264Picture*));
757
758 17737 h->short_ref[0] = h->cur_pic_ptr;
759 17737 h->short_ref_count++;
760 17737 h->cur_pic_ptr->reference |= h->picture_structure;
761 }
762 }
763
764
2/2
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 19275 times.
19286 if (h->long_ref_count + h->short_ref_count > FFMAX(h->ps.sps->ref_frame_count, 1)) {
765
766 /* We have too many reference frames, probably due to corrupted
767 * stream. Need to discard one frame. Prevents overrun of the
768 * short_ref and long_ref buffers.
769 */
770 11 av_log(h->avctx, AV_LOG_ERROR,
771 "number of reference frames (%d+%d) exceeds max (%d; probably "
772 "corrupt input), discarding one\n",
773 11 h->long_ref_count, h->short_ref_count, h->ps.sps->ref_frame_count);
774 11 err = AVERROR_INVALIDDATA;
775
776
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 11 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
11 if (h->long_ref_count && !h->short_ref_count) {
777 int i;
778 for (i = 0; i < 16; ++i)
779 if (h->long_ref[i])
780 break;
781
782 assert(i < 16);
783 remove_long(h, i, 0);
784 } else {
785 11 H264Picture *pic = h->short_ref[h->short_ref_count - 1];
786 11 remove_short(h, pic->frame_num, 0);
787 }
788 }
789
790
2/2
✓ Branch 0 taken 81079 times.
✓ Branch 1 taken 19286 times.
100365 for (int i = 0; i < h->short_ref_count; i++) {
791 81079 H264Picture *pic = h->short_ref[i];
792
2/2
✓ Branch 0 taken 347 times.
✓ Branch 1 taken 80732 times.
81079 if (pic->invalid_gap) {
793 347 int d = av_zero_extend(h->cur_pic_ptr->frame_num - pic->frame_num, h->ps.sps->log2_max_frame_num);
794
2/2
✓ Branch 0 taken 13 times.
✓ Branch 1 taken 334 times.
347 if (d > h->ps.sps->ref_frame_count)
795 13 remove_short(h, pic->frame_num, 0);
796 }
797 }
798
799 19286 print_short_term(h);
800 19286 print_long_term(h);
801
802
2/2
✓ Branch 0 taken 4937216 times.
✓ Branch 1 taken 19286 times.
4956502 for (int i = 0; i < FF_ARRAY_ELEMS(h->ps.pps_list); i++) {
803
2/2
✓ Branch 0 taken 19650 times.
✓ Branch 1 taken 4917566 times.
4937216 if (h->ps.pps_list[i]) {
804 19650 const PPS *pps = h->ps.pps_list[i];
805 19650 pps_ref_count[0] = FFMAX(pps_ref_count[0], pps->ref_count[0]);
806 19650 pps_ref_count[1] = FFMAX(pps_ref_count[1], pps->ref_count[1]);
807 }
808 }
809
810 // Detect unmarked random access points
811
2/2
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 19275 times.
19286 if ( err >= 0
812
2/2
✓ Branch 0 taken 1045 times.
✓ Branch 1 taken 18230 times.
19275 && h->long_ref_count==0
813
2/2
✓ Branch 0 taken 10161 times.
✓ Branch 1 taken 8069 times.
18230 && ( h->short_ref_count<=2
814
6/6
✓ Branch 0 taken 1760 times.
✓ Branch 1 taken 8401 times.
✓ Branch 2 taken 1643 times.
✓ Branch 3 taken 117 times.
✓ Branch 4 taken 739 times.
✓ Branch 5 taken 904 times.
10161 || pps_ref_count[0] <= 2 && pps_ref_count[1] <= 1 && h->avctx->has_b_frames
815
6/6
✓ Branch 0 taken 887 times.
✓ Branch 1 taken 8370 times.
✓ Branch 2 taken 8456 times.
✓ Branch 3 taken 801 times.
✓ Branch 4 taken 96 times.
✓ Branch 5 taken 705 times.
9257 || pps_ref_count[0] <= 1 + (h->picture_structure != PICT_FRAME) && pps_ref_count[1] <= 1)
816
6/6
✓ Branch 0 taken 2404 times.
✓ Branch 1 taken 7274 times.
✓ Branch 2 taken 32 times.
✓ Branch 3 taken 9646 times.
✓ Branch 4 taken 1210 times.
✓ Branch 5 taken 8468 times.
9678 && pps_ref_count[0]<=2 + (h->picture_structure != PICT_FRAME) + (2*!h->has_recovery_point)
817
2/2
✓ Branch 0 taken 6194 times.
✓ Branch 1 taken 2274 times.
8468 && h->cur_pic_ptr->f->pict_type == AV_PICTURE_TYPE_I){
818 2274 h->cur_pic_ptr->recovered |= FRAME_RECOVERED_HEURISTIC;
819
2/2
✓ Branch 0 taken 741 times.
✓ Branch 1 taken 1533 times.
2274 if(!h->avctx->has_b_frames)
820 1533 h->frame_recovered |= FRAME_RECOVERED_HEURISTIC;
821 }
822
823 17753 out:
824
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 19286 times.
19286 return (h->avctx->err_recognition & AV_EF_EXPLODE) ? err : 0;
825 }
826
827 25170 int ff_h264_decode_ref_pic_marking(H264SliceContext *sl, GetBitContext *gb,
828 const H2645NAL *nal, void *logctx)
829 {
830 25170 MMCO *mmco = sl->mmco;
831 25170 int nb_mmco = 0;
832
833
2/2
✓ Branch 0 taken 2270 times.
✓ Branch 1 taken 22900 times.
25170 if (nal->type == H264_NAL_IDR_SLICE) { // FIXME fields
834 2270 skip_bits1(gb); // broken_link
835
2/2
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 2267 times.
2270 if (get_bits1(gb)) {
836 3 mmco[0].opcode = MMCO_LONG;
837 3 mmco[0].long_arg = 0;
838 3 nb_mmco = 1;
839 }
840 2270 sl->explicit_ref_marking = 1;
841 } else {
842 22900 sl->explicit_ref_marking = get_bits1(gb);
843
2/2
✓ Branch 0 taken 2370 times.
✓ Branch 1 taken 20530 times.
22900 if (sl->explicit_ref_marking) {
844 int i;
845
1/2
✓ Branch 0 taken 6944 times.
✗ Branch 1 not taken.
6944 for (i = 0; i < FF_ARRAY_ELEMS(sl->mmco); i++) {
846 6944 MMCOOpcode opcode = get_ue_golomb_31(gb);
847
848 6944 mmco[i].opcode = opcode;
849
4/4
✓ Branch 0 taken 3260 times.
✓ Branch 1 taken 3684 times.
✓ Branch 2 taken 365 times.
✓ Branch 3 taken 2895 times.
6944 if (opcode == MMCO_SHORT2UNUSED || opcode == MMCO_SHORT2LONG) {
850 4049 mmco[i].short_pic_num =
851 4049 (sl->curr_pic_num - get_ue_golomb_long(gb) - 1) &
852 4049 (sl->max_pic_num - 1);
853 }
854
6/6
✓ Branch 0 taken 6579 times.
✓ Branch 1 taken 365 times.
✓ Branch 2 taken 6497 times.
✓ Branch 3 taken 82 times.
✓ Branch 4 taken 6479 times.
✓ Branch 5 taken 18 times.
6944 if (opcode == MMCO_SHORT2LONG || opcode == MMCO_LONG2UNUSED ||
855
2/2
✓ Branch 0 taken 410 times.
✓ Branch 1 taken 6069 times.
6479 opcode == MMCO_LONG || opcode == MMCO_SET_MAX_LONG) {
856 875 unsigned int long_arg = get_ue_golomb_31(gb);
857
2/4
✓ Branch 0 taken 875 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 875 times.
875 if (long_arg >= 32 ||
858 (long_arg >= 16 && !(opcode == MMCO_SET_MAX_LONG &&
859 long_arg == 16) &&
860 !(opcode == MMCO_LONG2UNUSED && FIELD_PICTURE(sl)))) {
861 av_log(logctx, AV_LOG_ERROR,
862 "illegal long ref in memory management control "
863 "operation %d\n", opcode);
864 sl->nb_mmco = i;
865 return -1;
866 }
867 875 mmco[i].long_arg = long_arg;
868 }
869
870
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6944 times.
6944 if (opcode > (unsigned) MMCO_LONG) {
871 av_log(logctx, AV_LOG_ERROR,
872 "illegal memory management control operation %d\n",
873 opcode);
874 sl->nb_mmco = i;
875 return -1;
876 }
877
2/2
✓ Branch 0 taken 2370 times.
✓ Branch 1 taken 4574 times.
6944 if (opcode == MMCO_END)
878 2370 break;
879 }
880 2370 nb_mmco = i;
881 }
882 }
883
884 25170 sl->nb_mmco = nb_mmco;
885
886 25170 return 0;
887 }
888