../../../ffmpeg/
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
506 / 680
74.4%
Branches:
282 / 485
58.1%
Functions:
24 / 26
92.3%

libavcodec/h264dec.c
Line Branch Exec Source
1 /*
2 * H.26L/H.264/AVC/JVT/14496-10/... decoder
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 codec.
25 * @author Michael Niedermayer <michaelni@gmx.at>
26 */
27
28 #define UNCHECKED_BITSTREAM_READER 1
29
30 #include "config_components.h"
31
32 #include "libavutil/attributes.h"
33 #include "libavutil/avassert.h"
34 #include "libavutil/imgutils.h"
35 #include "libavutil/mem.h"
36 #include "libavutil/opt.h"
37 #include "libavutil/thread.h"
38 #include "libavutil/video_enc_params.h"
39
40 #include "codec_internal.h"
41 #include "internal.h"
42 #include "error_resilience.h"
43 #include "avcodec.h"
44 #include "decode.h"
45 #include "h264.h"
46 #include "h264dec.h"
47 #include "h2645_parse.h"
48 #include "h264data.h"
49 #include "h264_ps.h"
50 #include "golomb.h"
51 #include "hwaccel_internal.h"
52 #include "hwconfig.h"
53 #include "mpegutils.h"
54 #include "profiles.h"
55 #include "rectangle.h"
56 #include "libavutil/refstruct.h"
57 #include "thread.h"
58 #include "threadframe.h"
59
60 const uint16_t ff_h264_mb_sizes[4] = { 256, 384, 512, 768 };
61
62 7617 int avpriv_h264_has_num_reorder_frames(AVCodecContext *avctx)
63 {
64 7617 H264Context *h = avctx->priv_data;
65
3/4
✓ Branch 2 → 3 taken 7617 times.
✗ Branch 2 → 5 not taken.
✓ Branch 3 → 4 taken 7466 times.
✓ Branch 3 → 5 taken 151 times.
7617 return h && h->ps.sps ? h->ps.sps->num_reorder_frames : 0;
66 }
67
68 1485 static void h264_er_decode_mb(void *opaque, int ref, int mv_dir, int mv_type,
69 int (*mv)[2][4][2],
70 int mb_x, int mb_y, int mb_intra, int mb_skipped)
71 {
72 1485 const H264Context *h = opaque;
73 1485 H264SliceContext *sl = &h->slice_ctx[0];
74
75 1485 sl->mb_x = mb_x;
76 1485 sl->mb_y = mb_y;
77 1485 sl->mb_xy = mb_x + mb_y * h->mb_stride;
78 1485 memset(sl->non_zero_count_cache, 0, sizeof(sl->non_zero_count_cache));
79 av_assert1(ref >= 0);
80 /* FIXME: It is possible albeit uncommon that slice references
81 * differ between slices. We take the easy approach and ignore
82 * it for now. If this turns out to have any relevance in
83 * practice then correct remapping should be added. */
84
1/2
✗ Branch 2 → 3 not taken.
✓ Branch 2 → 4 taken 1485 times.
1485 if (ref >= sl->ref_count[0])
85 ✗ ref = 0;
86
1/2
✗ Branch 4 → 5 not taken.
✓ Branch 4 → 7 taken 1485 times.
1485 if (!sl->ref_list[0][ref].data[0]) {
87 ✗ av_log(h->avctx, AV_LOG_DEBUG, "Reference not available for error concealing\n");
88 ✗ ref = 0;
89 }
90
1/2
✗ Branch 7 → 8 not taken.
✓ Branch 7 → 10 taken 1485 times.
1485 if ((sl->ref_list[0][ref].reference&3) != 3) {
91 ✗ av_log(h->avctx, AV_LOG_DEBUG, "Reference invalid\n");
92 ✗ return;
93 }
94 1485 fill_rectangle(&h->cur_pic.ref_index[0][4 * sl->mb_xy],
95 2, 2, 2, ref, 1);
96 1485 fill_rectangle(&sl->ref_cache[0][scan8[0]], 4, 4, 8, ref, 1);
97 1485 fill_rectangle(sl->mv_cache[0][scan8[0]], 4, 4, 8,
98 1485 pack16to32((*mv)[0][0][0], (*mv)[0][0][1]), 4);
99 1485 sl->mb_mbaff =
100 1485 sl->mb_field_decoding_flag = 0;
101 1485 ff_h264_hl_decode_mb(h, &h->slice_ctx[0]);
102 }
103
104 392952 void ff_h264_draw_horiz_band(const H264Context *h, H264SliceContext *sl,
105 int y, int height)
106 {
107 392952 AVCodecContext *avctx = h->avctx;
108 392952 const AVFrame *src = h->cur_pic.f;
109 const AVPixFmtDescriptor *desc;
110 int offset[AV_NUM_DATA_POINTERS];
111 int vshift;
112 392952 const int field_pic = h->picture_structure != PICT_FRAME;
113
114
1/2
✓ Branch 2 → 3 taken 392952 times.
✗ Branch 2 → 4 not taken.
392952 if (!avctx->draw_horiz_band)
115 392952 return;
116
117
0/6
✗ Branch 4 → 5 not taken.
✗ Branch 4 → 8 not taken.
✗ Branch 5 → 6 not taken.
✗ Branch 5 → 8 not taken.
✗ Branch 6 → 7 not taken.
✗ Branch 6 → 8 not taken.
✗ if (field_pic && h->first_field && !(avctx->slice_flags & SLICE_FLAG_ALLOW_FIELD))
118 ✗ return;
119
120
0/2
✗ Branch 8 → 9 not taken.
✗ Branch 8 → 10 not taken.
✗ if (field_pic) {
121 ✗ height <<= 1;
122 ✗ y <<= 1;
123 }
124
125 ✗ height = FFMIN(height, avctx->height - y);
126
127 ✗ desc = av_pix_fmt_desc_get(avctx->pix_fmt);
128 ✗ vshift = desc->log2_chroma_h;
129
130 ✗ offset[0] = y * src->linesize[0];
131 ✗ offset[1] =
132 ✗ offset[2] = (y >> vshift) * src->linesize[1];
133
0/2
✗ Branch 13 → 12 not taken.
✗ Branch 13 → 14 not taken.
✗ for (int i = 3; i < AV_NUM_DATA_POINTERS; i++)
134 ✗ offset[i] = 0;
135
136 ✗ avctx->draw_horiz_band(avctx, src, offset,
137 ✗ y, h->picture_structure, height);
138 }
139
140 1414 void ff_h264_free_tables(H264Context *h)
141 {
142 int i;
143
144 1414 av_freep(&h->intra4x4_pred_mode);
145 1414 av_freep(&h->chroma_pred_mode_table);
146 1414 av_freep(&h->cbp_table);
147 1414 av_freep(&h->mvd_table[0]);
148 1414 av_freep(&h->mvd_table[1]);
149 1414 av_freep(&h->direct_table);
150 1414 av_freep(&h->non_zero_count);
151 1414 av_freep(&h->slice_table_base);
152 1414 h->slice_table = NULL;
153 1414 av_freep(&h->list_counts);
154
155 1414 av_freep(&h->mb2b_xy);
156 1414 av_freep(&h->mb2br_xy);
157
158 1414 av_refstruct_pool_uninit(&h->qscale_table_pool);
159 1414 av_refstruct_pool_uninit(&h->mb_type_pool);
160 1414 av_refstruct_pool_uninit(&h->motion_val_pool);
161 1414 av_refstruct_pool_uninit(&h->ref_index_pool);
162
163 #if CONFIG_ERROR_RESILIENCE
164 1414 av_freep(&h->er.mb_index2xy);
165 1414 av_freep(&h->er.error_status_table);
166 1414 av_freep(&h->er.er_temp_buffer);
167 1414 av_freep(&h->dc_val_base);
168 #endif
169
170
2/2
✓ Branch 27 → 22 taken 1416 times.
✓ Branch 27 → 28 taken 1414 times.
2830 for (i = 0; i < h->nb_slice_ctx; i++) {
171 1416 H264SliceContext *sl = &h->slice_ctx[i];
172
173 1416 av_freep(&sl->bipred_scratchpad);
174 1416 av_freep(&sl->edge_emu_buffer);
175 1416 av_freep(&sl->top_borders[0]);
176 1416 av_freep(&sl->top_borders[1]);
177
178 1416 sl->bipred_scratchpad_allocated = 0;
179 1416 sl->edge_emu_buffer_allocated = 0;
180 1416 sl->top_borders_allocated[0] = 0;
181 1416 sl->top_borders_allocated[1] = 0;
182 }
183 1414 }
184
185 693 int ff_h264_alloc_tables(H264Context *h)
186 {
187 693 ERContext *const er = &h->er;
188 693 const int big_mb_num = h->mb_stride * (h->mb_height + 1);
189 693 const int row_mb_num = 2*h->mb_stride*FFMAX(h->nb_slice_ctx, 1);
190 693 const int st_size = big_mb_num + h->mb_stride;
191 int x, y;
192
193
1/2
✓ Branch 3 → 4 taken 693 times.
✗ Branch 3 → 24 not taken.
693 if (!FF_ALLOCZ_TYPED_ARRAY(h->intra4x4_pred_mode, row_mb_num * 8) ||
194
1/2
✓ Branch 5 → 6 taken 693 times.
✗ Branch 5 → 24 not taken.
693 !FF_ALLOCZ_TYPED_ARRAY(h->non_zero_count, big_mb_num) ||
195
1/2
✓ Branch 7 → 8 taken 693 times.
✗ Branch 7 → 24 not taken.
693 !FF_ALLOCZ_TYPED_ARRAY(h->slice_table_base, st_size) ||
196
1/2
✓ Branch 9 → 10 taken 693 times.
✗ Branch 9 → 24 not taken.
693 !FF_ALLOCZ_TYPED_ARRAY(h->cbp_table, big_mb_num) ||
197
1/2
✓ Branch 11 → 12 taken 693 times.
✗ Branch 11 → 24 not taken.
693 !FF_ALLOCZ_TYPED_ARRAY(h->chroma_pred_mode_table, big_mb_num) ||
198
1/2
✓ Branch 13 → 14 taken 693 times.
✗ Branch 13 → 24 not taken.
693 !FF_ALLOCZ_TYPED_ARRAY(h->mvd_table[0], row_mb_num * 8) ||
199
1/2
✓ Branch 15 → 16 taken 693 times.
✗ Branch 15 → 24 not taken.
693 !FF_ALLOCZ_TYPED_ARRAY(h->mvd_table[1], row_mb_num * 8) ||
200
1/2
✓ Branch 17 → 18 taken 693 times.
✗ Branch 17 → 24 not taken.
693 !FF_ALLOCZ_TYPED_ARRAY(h->direct_table, big_mb_num * 4) ||
201
1/2
✓ Branch 19 → 20 taken 693 times.
✗ Branch 19 → 24 not taken.
693 !FF_ALLOCZ_TYPED_ARRAY(h->list_counts, big_mb_num) ||
202
1/2
✓ Branch 21 → 22 taken 693 times.
✗ Branch 21 → 24 not taken.
693 !FF_ALLOCZ_TYPED_ARRAY(h->mb2b_xy, big_mb_num) ||
203
1/2
✗ Branch 23 → 24 not taken.
✓ Branch 23 → 25 taken 693 times.
693 !FF_ALLOCZ_TYPED_ARRAY(h->mb2br_xy, big_mb_num))
204 ✗ return AVERROR(ENOMEM);
205 693 h->slice_ctx[0].intra4x4_pred_mode = h->intra4x4_pred_mode;
206 693 h->slice_ctx[0].mvd_table[0] = h->mvd_table[0];
207 693 h->slice_ctx[0].mvd_table[1] = h->mvd_table[1];
208 693 memset(h->slice_table_base, -1,
209 693 st_size * sizeof(*h->slice_table_base));
210 693 h->slice_table = h->slice_table_base + h->mb_stride * 2 + 1;
211
2/2
✓ Branch 30 → 26 taken 17398 times.
✓ Branch 30 → 31 taken 693 times.
18091 for (y = 0; y < h->mb_height; y++)
212
2/2
✓ Branch 28 → 27 taken 1078848 times.
✓ Branch 28 → 29 taken 17398 times.
1096246 for (x = 0; x < h->mb_width; x++) {
213 1078848 const int mb_xy = x + y * h->mb_stride;
214 1078848 const int b_xy = 4 * x + 4 * y * h->b_stride;
215
216 1078848 h->mb2b_xy[mb_xy] = b_xy;
217 1078848 h->mb2br_xy[mb_xy] = 8 * (FMO ? mb_xy : (mb_xy % (2 * h->mb_stride)));
218 }
219
220 if (CONFIG_ERROR_RESILIENCE) {
221 693 int y_size = (2 * h->mb_width + 1) * (2 * h->mb_height + 1);
222 693 int yc_size = y_size + 2 * big_mb_num;
223
224 /* init ER */
225 693 er->avctx = h->avctx;
226 693 er->decode_mb = h264_er_decode_mb;
227 693 er->opaque = h;
228 693 er->quarter_sample = 1;
229
230 693 er->mb_num = h->mb_num;
231 693 er->mb_width = h->mb_width;
232 693 er->mb_height = h->mb_height;
233 693 er->mb_stride = h->mb_stride;
234 693 er->b8_stride = h->mb_width * 2 + 1;
235
236 // error resilience code looks cleaner with this
237
1/2
✓ Branch 32 → 33 taken 693 times.
✗ Branch 32 → 35 not taken.
693 if (!FF_ALLOCZ_TYPED_ARRAY(er->mb_index2xy, h->mb_num + 1) ||
238
1/2
✗ Branch 34 → 35 not taken.
✓ Branch 34 → 36 taken 693 times.
693 !FF_ALLOCZ_TYPED_ARRAY(h->dc_val_base, yc_size))
239 ✗ return AVERROR(ENOMEM); // ff_h264_free_tables will clean up for us
240
241
2/2
✓ Branch 41 → 37 taken 17398 times.
✓ Branch 41 → 42 taken 693 times.
18091 for (y = 0; y < h->mb_height; y++)
242
2/2
✓ Branch 39 → 38 taken 1078848 times.
✓ Branch 39 → 40 taken 17398 times.
1096246 for (x = 0; x < h->mb_width; x++)
243 1078848 er->mb_index2xy[x + y * h->mb_width] = x + y * h->mb_stride;
244
245 693 er->mb_index2xy[h->mb_height * h->mb_width] = (h->mb_height - 1) *
246 693 h->mb_stride + h->mb_width;
247 693 er->dc_val[0] = h->dc_val_base + h->mb_width * 2 + 2;
248 693 er->dc_val[1] = h->dc_val_base + y_size + h->mb_stride + 1;
249 693 er->dc_val[2] = er->dc_val[1] + big_mb_num;
250
2/2
✓ Branch 44 → 43 taken 6649247 times.
✓ Branch 44 → 45 taken 693 times.
6649940 for (int i = 0; i < yc_size; i++)
251 6649247 h->dc_val_base[i] = 1024;
252
253 693 return ff_er_init(er);
254 }
255
256 return 0;
257 }
258
259 /**
260 * Init slice context
261 */
262 694 void ff_h264_slice_context_init(H264Context *h, H264SliceContext *sl)
263 {
264 694 sl->ref_cache[0][scan8[5] + 1] =
265 694 sl->ref_cache[0][scan8[7] + 1] =
266 694 sl->ref_cache[0][scan8[13] + 1] =
267 694 sl->ref_cache[1][scan8[5] + 1] =
268 694 sl->ref_cache[1][scan8[7] + 1] =
269 694 sl->ref_cache[1][scan8[13] + 1] = PART_NOT_AVAILABLE;
270
271 694 sl->er = &h->er;
272 694 }
273
274 27284 static int h264_init_pic(H264Picture *pic)
275 {
276 27284 pic->f = av_frame_alloc();
277
1/2
✗ Branch 3 → 4 not taken.
✓ Branch 3 → 5 taken 27284 times.
27284 if (!pic->f)
278 ✗ return AVERROR(ENOMEM);
279
280 27284 pic->f_grain = av_frame_alloc();
281
1/2
✗ Branch 6 → 7 not taken.
✓ Branch 6 → 8 taken 27284 times.
27284 if (!pic->f_grain)
282 ✗ return AVERROR(ENOMEM);
283
284 27284 return 0;
285 }
286
287 718 static int h264_init_context(AVCodecContext *avctx, H264Context *h)
288 {
289 int i, ret;
290
291 718 h->avctx = avctx;
292 718 h->cur_chroma_format_idc = -1;
293
294 718 h->width_from_caller = avctx->width;
295 718 h->height_from_caller = avctx->height;
296
297 718 h->workaround_bugs = avctx->workaround_bugs;
298 718 h->flags = avctx->flags;
299 718 h->poc.prev_poc_msb = 1 << 16;
300 718 h->recovery_frame = -1;
301 718 h->frame_recovered = 0;
302 718 h->poc.prev_frame_num = -1;
303 718 h->sei.common.frame_packing.arrangement_cancel_flag = -1;
304 718 h->sei.common.unregistered.x264_build = -1;
305
306 718 h->next_outputed_poc = INT_MIN;
307
2/2
✓ Branch 4 → 3 taken 11488 times.
✓ Branch 4 → 5 taken 718 times.
12206 for (i = 0; i < FF_ARRAY_ELEMS(h->last_pocs); i++)
308 11488 h->last_pocs[i] = INT_MIN;
309
310 718 ff_h264_sei_uninit(&h->sei);
311
312
2/2
✓ Branch 6 → 7 taken 18 times.
✓ Branch 6 → 10 taken 700 times.
718 if (avctx->active_thread_type & FF_THREAD_FRAME) {
313 18 h->decode_error_flags_pool = av_refstruct_pool_alloc(sizeof(atomic_int), 0);
314
1/2
✗ Branch 8 → 9 not taken.
✓ Branch 8 → 10 taken 18 times.
18 if (!h->decode_error_flags_pool)
315 ✗ return AVERROR(ENOMEM);
316 }
317
318
2/2
✓ Branch 10 → 11 taken 1 time.
✓ Branch 10 → 12 taken 717 times.
718 h->nb_slice_ctx = (avctx->active_thread_type & FF_THREAD_SLICE) ? avctx->thread_count : 1;
319 718 h->slice_ctx = av_calloc(h->nb_slice_ctx, sizeof(*h->slice_ctx));
320
1/2
✗ Branch 14 → 15 not taken.
✓ Branch 14 → 16 taken 718 times.
718 if (!h->slice_ctx) {
321 ✗ h->nb_slice_ctx = 0;
322 ✗ return AVERROR(ENOMEM);
323 }
324
325
2/2
✓ Branch 21 → 17 taken 25848 times.
✓ Branch 21 → 22 taken 718 times.
26566 for (i = 0; i < H264_MAX_PICTURE_COUNT; i++) {
326
1/2
✗ Branch 18 → 19 not taken.
✓ Branch 18 → 20 taken 25848 times.
25848 if ((ret = h264_init_pic(&h->DPB[i])) < 0)
327 ✗ return ret;
328 }
329
330
1/2
✗ Branch 23 → 24 not taken.
✓ Branch 23 → 25 taken 718 times.
718 if ((ret = h264_init_pic(&h->cur_pic)) < 0)
331 ✗ return ret;
332
333
1/2
✗ Branch 26 → 27 not taken.
✓ Branch 26 → 28 taken 718 times.
718 if ((ret = h264_init_pic(&h->last_pic_for_ec)) < 0)
334 ✗ return ret;
335
336
2/2
✓ Branch 30 → 29 taken 719 times.
✓ Branch 30 → 31 taken 718 times.
1437 for (i = 0; i < h->nb_slice_ctx; i++)
337 719 h->slice_ctx[i].h264 = h;
338
339 718 return 0;
340 }
341
342 27284 static void h264_free_pic(H264Context *h, H264Picture *pic)
343 {
344 27284 ff_h264_unref_picture(pic);
345 27284 av_frame_free(&pic->f);
346 27284 av_frame_free(&pic->f_grain);
347 27284 }
348
349 718 static av_cold int h264_decode_end(AVCodecContext *avctx)
350 {
351 718 H264Context *h = avctx->priv_data;
352 int i;
353
354 718 ff_h264_remove_all_refs(h);
355 718 ff_h264_free_tables(h);
356
357
2/2
✓ Branch 7 → 5 taken 25848 times.
✓ Branch 7 → 8 taken 718 times.
26566 for (i = 0; i < H264_MAX_PICTURE_COUNT; i++) {
358 25848 h264_free_pic(h, &h->DPB[i]);
359 }
360 718 memset(h->delayed_pic, 0, sizeof(h->delayed_pic));
361
362 718 h->cur_pic_ptr = NULL;
363
364 718 av_refstruct_pool_uninit(&h->decode_error_flags_pool);
365
366 718 av_freep(&h->slice_ctx);
367 718 h->nb_slice_ctx = 0;
368
369 718 ff_h264_sei_uninit(&h->sei);
370 718 ff_h264_ps_uninit(&h->ps);
371
372 718 ff_h2645_packet_uninit(&h->pkt);
373
374 718 h264_free_pic(h, &h->cur_pic);
375 718 h264_free_pic(h, &h->last_pic_for_ec);
376
377 718 return 0;
378 }
379
380 static AVOnce h264_vlc_init = AV_ONCE_INIT;
381
382 718 static av_cold int h264_decode_init(AVCodecContext *avctx)
383 {
384 718 H264Context *h = avctx->priv_data;
385 int ret;
386
387 718 ret = h264_init_context(avctx, h);
388
1/2
✗ Branch 3 → 4 not taken.
✓ Branch 3 → 5 taken 718 times.
718 if (ret < 0)
389 ✗ return ret;
390
391 718 ret = ff_thread_once(&h264_vlc_init, ff_h264_decode_init_vlc);
392
1/2
✗ Branch 6 → 7 not taken.
✓ Branch 6 → 9 taken 718 times.
718 if (ret != 0) {
393 ✗ av_log(avctx, AV_LOG_ERROR, "pthread_once has failed.");
394 ✗ return AVERROR_UNKNOWN;
395 }
396
397
2/2
✓ Branch 9 → 10 taken 702 times.
✓ Branch 9 → 21 taken 16 times.
718 if (!avctx->internal->is_copy) {
398
3/4
✓ Branch 10 → 11 taken 442 times.
✓ Branch 10 → 21 taken 260 times.
✓ Branch 11 → 12 taken 442 times.
✗ Branch 11 → 21 not taken.
702 if (avctx->extradata_size > 0 && avctx->extradata) {
399 442 ret = ff_h264_decode_extradata(avctx->extradata, avctx->extradata_size,
400 &h->ps, &h->is_avc, &h->nal_length_size,
401 avctx->err_recognition, avctx);
402
1/2
✗ Branch 13 → 14 not taken.
✓ Branch 13 → 21 taken 442 times.
442 if (ret < 0) {
403 ✗ int explode = avctx->err_recognition & AV_EF_EXPLODE;
404
0/2
✗ Branch 14 → 15 not taken.
✗ Branch 14 → 16 not taken.
✗ av_log(avctx, explode ? AV_LOG_ERROR: AV_LOG_WARNING,
405 "Error decoding the extradata\n");
406
0/2
✗ Branch 18 → 19 not taken.
✗ Branch 18 → 20 not taken.
✗ if (explode) {
407 ✗ return ret;
408 }
409 ✗ ret = 0;
410 }
411 }
412 }
413
414
1/4
✗ Branch 21 → 22 not taken.
✓ Branch 21 → 25 taken 718 times.
✗ Branch 22 → 23 not taken.
✗ Branch 22 → 25 not taken.
718 if (h->ps.sps && h->ps.sps->bitstream_restriction_flag &&
415
0/2
✗ Branch 23 → 24 not taken.
✗ Branch 23 → 25 not taken.
✗ h->avctx->has_b_frames < h->ps.sps->num_reorder_frames) {
416 ✗ h->avctx->has_b_frames = h->ps.sps->num_reorder_frames;
417 }
418
419 718 ff_h264_flush_change(h);
420
421
3/4
✓ Branch 26 → 27 taken 718 times.
✗ Branch 26 → 29 not taken.
✓ Branch 27 → 28 taken 1 time.
✓ Branch 27 → 29 taken 717 times.
718 if (h->enable_er < 0 && (avctx->active_thread_type & FF_THREAD_SLICE))
422 1 h->enable_er = 0;
423
424
3/4
✓ Branch 29 → 30 taken 717 times.
✓ Branch 29 → 32 taken 1 time.
✗ Branch 30 → 31 not taken.
✓ Branch 30 → 32 taken 717 times.
718 if (h->enable_er && (avctx->active_thread_type & FF_THREAD_SLICE)) {
425 ✗ av_log(avctx, AV_LOG_WARNING,
426 "Error resilience with slice threads is enabled. It is unsafe and unsupported and may crash. "
427 "Use it at your own risk\n");
428 }
429
430 718 return 0;
431 }
432
433 /**
434 * instantaneous decoder refresh.
435 */
436 1974 static void idr(H264Context *h)
437 {
438 int i;
439 1974 ff_h264_remove_all_refs(h);
440 1974 h->poc.prev_frame_num =
441 1974 h->poc.prev_frame_num_offset = 0;
442 1974 h->poc.prev_poc_msb = 1<<16;
443 1974 h->poc.prev_poc_lsb = -1;
444
2/2
✓ Branch 5 → 4 taken 31584 times.
✓ Branch 5 → 6 taken 1974 times.
33558 for (i = 0; i < FF_ARRAY_ELEMS(h->last_pocs); i++)
445 31584 h->last_pocs[i] = INT_MIN;
446 1974 }
447
448 /* forget old pics after a seek */
449 729 void ff_h264_flush_change(H264Context *h)
450 {
451 int i, j;
452
453 729 h->next_outputed_poc = INT_MIN;
454 729 h->prev_interlaced_frame = 1;
455 729 idr(h);
456
457 729 h->poc.prev_frame_num = -1;
458
1/2
✗ Branch 3 → 4 not taken.
✓ Branch 3 → 10 taken 729 times.
729 if (h->cur_pic_ptr) {
459 ✗ h->cur_pic_ptr->reference = 0;
460
0/2
✗ Branch 8 → 5 not taken.
✗ Branch 8 → 9 not taken.
✗ for (j=i=0; h->delayed_pic[i]; i++)
461
0/2
✗ Branch 5 → 6 not taken.
✗ Branch 5 → 7 not taken.
✗ if (h->delayed_pic[i] != h->cur_pic_ptr)
462 ✗ h->delayed_pic[j++] = h->delayed_pic[i];
463 ✗ h->delayed_pic[j] = NULL;
464 }
465 729 ff_h264_unref_picture(&h->last_pic_for_ec);
466
467 729 h->first_field = 0;
468 729 h->recovery_frame = -1;
469 729 h->frame_recovered = 0;
470 729 h->current_slice = 0;
471 729 h->mmco_reset = 1;
472 729 }
473
474 3 static av_cold void h264_decode_flush(AVCodecContext *avctx)
475 {
476 3 H264Context *h = avctx->priv_data;
477 int i;
478
479 3 memset(h->delayed_pic, 0, sizeof(h->delayed_pic));
480
481 3 ff_h264_flush_change(h);
482 3 ff_h264_sei_uninit(&h->sei);
483
484
2/2
✓ Branch 7 → 5 taken 108 times.
✓ Branch 7 → 8 taken 3 times.
111 for (i = 0; i < H264_MAX_PICTURE_COUNT; i++)
485 108 ff_h264_unref_picture(&h->DPB[i]);
486 3 h->cur_pic_ptr = NULL;
487 3 ff_h264_unref_picture(&h->cur_pic);
488
489 3 h->mb_y = 0;
490 3 h->non_gray = 0;
491
492 3 ff_h264_free_tables(h);
493 3 h->context_initialized = 0;
494
495
1/4
✗ Branch 10 → 11 not taken.
✓ Branch 10 → 15 taken 3 times.
✗ Branch 12 → 13 not taken.
✗ Branch 12 → 15 not taken.
3 if (FF_HW_HAS_CB(avctx, flush))
496 ✗ FF_HW_SIMPLE_CALL(avctx, flush);
497 3 }
498
499 42 static int get_last_needed_nal(H264Context *h)
500 {
501 42 int nals_needed = 0;
502 42 int slice_type = 0;
503 42 int picture_intra_only = 1;
504 42 int first_slice = 0;
505 int i, ret;
506
507
2/2
✓ Branch 24 → 3 taken 44 times.
✓ Branch 24 → 25 taken 42 times.
86 for (i = 0; i < h->pkt.nb_nals; i++) {
508 44 H2645NAL *nal = &h->pkt.nals[i];
509 GetBitContext gb;
510
511 /* packets can sometimes contain multiple PPS/SPS,
512 * e.g. two PAFF field pictures in one packet, or a demuxer
513 * which splits NALs strangely if so, when frame threading we
514 * can't start the next thread until we've read all of them */
515
2/3
✗ Branch 3 → 4 not taken.
✓ Branch 3 → 5 taken 42 times.
✓ Branch 3 → 23 taken 2 times.
44 switch (nal->type) {
516 ✗ case H264_NAL_SPS:
517 case H264_NAL_PPS:
518 ✗ nals_needed = i;
519 ✗ break;
520 42 case H264_NAL_DPA:
521 case H264_NAL_IDR_SLICE:
522 case H264_NAL_SLICE:
523 42 ret = init_get_bits8(&gb, nal->data + 1, nal->size - 1);
524
1/2
✗ Branch 6 → 7 not taken.
✓ Branch 6 → 11 taken 42 times.
42 if (ret < 0) {
525 ✗ av_log(h->avctx, AV_LOG_ERROR, "Invalid zero-sized VCL NAL unit\n");
526
0/2
✗ Branch 8 → 9 not taken.
✗ Branch 8 → 10 not taken.
✗ if (h->avctx->err_recognition & AV_EF_EXPLODE)
527 ✗ return ret;
528
529 ✗ break;
530 }
531
1/4
✗ Branch 12 → 13 not taken.
✓ Branch 12 → 15 taken 42 times.
✗ Branch 13 → 14 not taken.
✗ Branch 13 → 15 not taken.
42 if (!get_ue_golomb_long(&gb) || // first_mb_in_slice
532 ✗ !first_slice ||
533
0/2
✗ Branch 14 → 15 not taken.
✗ Branch 14 → 16 not taken.
✗ first_slice != nal->type)
534 42 nals_needed = i;
535 42 slice_type = get_ue_golomb_31(&gb);
536
1/2
✗ Branch 17 → 18 not taken.
✓ Branch 17 → 19 taken 42 times.
42 if (slice_type > 9)
537 ✗ slice_type = 0;
538
1/2
✓ Branch 19 → 20 taken 42 times.
✗ Branch 19 → 21 not taken.
42 if (slice_type > 4)
539 42 slice_type -= 5;
540
541 42 slice_type = ff_h264_golomb_to_pict_type[slice_type];
542 42 picture_intra_only &= (slice_type & 3) == AV_PICTURE_TYPE_I;
543
1/2
✓ Branch 21 → 22 taken 42 times.
✗ Branch 21 → 23 not taken.
42 if (!first_slice)
544 42 first_slice = nal->type;
545 }
546 }
547
548 42 h->picture_intra_only = picture_intra_only;
549
550 42 return nals_needed;
551 }
552
553 ✗ static void debug_green_metadata(const H264SEIGreenMetaData *gm, void *logctx)
554 {
555 ✗ av_log(logctx, AV_LOG_DEBUG, "Green Metadata Info SEI message\n");
556 ✗ av_log(logctx, AV_LOG_DEBUG, " green_metadata_type: %d\n", gm->green_metadata_type);
557
558
0/2
✗ Branch 4 → 5 not taken.
✗ Branch 4 → 11 not taken.
✗ if (gm->green_metadata_type == 0) {
559 ✗ av_log(logctx, AV_LOG_DEBUG, " green_metadata_period_type: %d\n", gm->period_type);
560
561
0/2
✗ Branch 6 → 7 not taken.
✗ Branch 6 → 8 not taken.
✗ if (gm->period_type == 2)
562 ✗ av_log(logctx, AV_LOG_DEBUG, " green_metadata_num_seconds: %d\n", gm->num_seconds);
563
0/2
✗ Branch 8 → 9 not taken.
✗ Branch 8 → 10 not taken.
✗ else if (gm->period_type == 3)
564 ✗ av_log(logctx, AV_LOG_DEBUG, " green_metadata_num_pictures: %d\n", gm->num_pictures);
565
566 ✗ av_log(logctx, AV_LOG_DEBUG, " SEI GREEN Complexity Metrics: %f %f %f %f\n",
567 ✗ (float)gm->percent_non_zero_macroblocks/255,
568 ✗ (float)gm->percent_intra_coded_macroblocks/255,
569 ✗ (float)gm->percent_six_tap_filtering/255,
570 ✗ (float)gm->percent_alpha_point_deblocking_instance/255);
571
572
0/2
✗ Branch 11 → 12 not taken.
✗ Branch 11 → 15 not taken.
✗ } else if (gm->green_metadata_type == 1) {
573 ✗ av_log(logctx, AV_LOG_DEBUG, " xsd_metric_type: %d\n", gm->xsd_metric_type);
574
575
0/2
✗ Branch 13 → 14 not taken.
✗ Branch 13 → 15 not taken.
✗ if (gm->xsd_metric_type == 0)
576 ✗ av_log(logctx, AV_LOG_DEBUG, " xsd_metric_value: %f\n",
577 ✗ (float)gm->xsd_metric_value/100);
578 }
579 ✗ }
580
581 /**
582 * Attach the partitions B and C immediately following the partition A at idx.
583 * Arbitrary slice order may separate them (7.4.1.2.5); that is not supported.
584 *
585 * @return index of the last NAL absorbed, or idx if there were none.
586 */
587 60 static int h264_attach_partitions(const H264Context *h, H264SliceContext *sl,
588 int idx)
589 {
590
2/2
✓ Branch 10 → 3 taken 105 times.
✓ Branch 10 → 11 taken 60 times.
165 while (idx + 1 < h->pkt.nb_nals) {
591 105 const H2645NAL *nal = &h->pkt.nals[idx + 1];
592
593
3/4
✓ Branch 3 → 4 taken 45 times.
✓ Branch 3 → 6 taken 60 times.
✗ Branch 4 → 5 not taken.
✓ Branch 4 → 6 taken 45 times.
105 if (nal->type != H264_NAL_DPB && nal->type != H264_NAL_DPC)
594 ✗ break;
595
1/2
✗ Branch 7 → 8 not taken.
✓ Branch 7 → 9 taken 105 times.
105 if (ff_h264_attach_slice_partition(h, sl, nal) < 0)
596 ✗ break;
597 105 idx++;
598 }
599
600 60 return idx;
601 }
602
603 3126 static int h264_decode_sei(H264Context *h, H2645NAL *nal)
604 {
605 3126 AVCodecContext *const avctx = h->avctx;
606 int ret;
607
608 3126 ret = ff_h264_sei_decode(&h->sei, &nal->gb, &h->ps, avctx);
609
4/4
✓ Branch 3 → 4 taken 369 times.
✓ Branch 3 → 5 taken 2757 times.
✓ Branch 4 → 5 taken 34 times.
✓ Branch 4 → 6 taken 335 times.
3126 h->has_recovery_point = h->has_recovery_point || h->sei.recovery_point.recovery_frame_cnt != -1;
610
1/2
✗ Branch 7 → 8 not taken.
✓ Branch 7 → 9 taken 3126 times.
3126 if (avctx->debug & FF_DEBUG_GREEN_MD)
611 ✗ debug_green_metadata(&h->sei.green_metadata, avctx);
612
3/4
✓ Branch 9 → 10 taken 7 times.
✓ Branch 9 → 12 taken 3119 times.
✗ Branch 10 → 11 not taken.
✓ Branch 10 → 12 taken 7 times.
3126 if (ret < 0 && (avctx->err_recognition & AV_EF_EXPLODE))
613 ✗ return ret;
614
615 3126 return 0;
616 }
617
618 /* Decode the SEI NAL units following the slice at idx. */
619 42 static int h264_decode_late_sei(H264Context *h, int idx)
620 {
621
1/2
✗ Branch 9 → 3 not taken.
✓ Branch 9 → 10 taken 42 times.
42 for (int i = idx + 1; i < h->pkt.nb_nals; i++) {
622 ✗ H2645NAL *nal = &h->pkt.nals[i];
623 int ret;
624
625
0/2
✗ Branch 3 → 4 not taken.
✗ Branch 3 → 5 not taken.
✗ if (nal->type != H264_NAL_SEI)
626 ✗ continue;
627
628 ✗ ret = h264_decode_sei(h, nal);
629
0/2
✗ Branch 6 → 7 not taken.
✗ Branch 6 → 8 not taken.
✗ if (ret < 0)
630 ✗ return ret;
631 }
632
633 42 return 0;
634 }
635
636 30379 static int decode_nal_units(H264Context *h, AVBufferRef *buf_ref,
637 const uint8_t *buf, int buf_size)
638 {
639 30379 AVCodecContext *const avctx = h->avctx;
640 30379 int nals_needed = 0; ///< number of NALs that need decoding before the next frame thread starts
641 30379 int idr_cleared=0;
642 30379 int dp_attached_to = -1; ///< index of the last partition B/C claimed
643 30379 int i, ret = 0;
644
645 30379 h->has_slice = 0;
646 30379 h->nal_unit_type= 0;
647
648
2/2
✓ Branch 2 → 3 taken 29623 times.
✓ Branch 2 → 5 taken 756 times.
30379 if (!(avctx->flags2 & AV_CODEC_FLAG2_CHUNKS)) {
649 29623 h->current_slice = 0;
650
2/2
✓ Branch 3 → 4 taken 26208 times.
✓ Branch 3 → 5 taken 3415 times.
29623 if (!h->first_field) {
651 26208 h->cur_pic_ptr = NULL;
652 26208 ff_h264_sei_uninit(&h->sei);
653 }
654 }
655
656
2/2
✓ Branch 5 → 6 taken 3503 times.
✓ Branch 5 → 14 taken 26876 times.
30379 if (h->nal_length_size == 4) {
657
2/6
✓ Branch 6 → 7 taken 3503 times.
✗ Branch 6 → 10 not taken.
✗ Branch 7 → 8 not taken.
✓ Branch 7 → 10 taken 3503 times.
✗ Branch 8 → 9 not taken.
✗ Branch 8 → 10 not taken.
3503 if (buf_size > 8 && AV_RB32(buf) == 1 && AV_RB32(buf+5) > (unsigned)buf_size) {
658 ✗ h->is_avc = 0;
659
3/6
✓ Branch 10 → 11 taken 3503 times.
✗ Branch 10 → 14 not taken.
✓ Branch 11 → 12 taken 3503 times.
✗ Branch 11 → 14 not taken.
✓ Branch 12 → 13 taken 3503 times.
✗ Branch 12 → 14 not taken.
3503 }else if(buf_size > 3 && AV_RB32(buf) > 1 && AV_RB32(buf) <= (unsigned)buf_size)
660 3503 h->is_avc = 1;
661 }
662
663 30379 ret = ff_h2645_packet_split(&h->pkt, buf, buf_size, avctx, h->nal_length_size,
664 30379 avctx->codec_id, !!h->is_avc * H2645_FLAG_IS_NALFF);
665
2/2
✓ Branch 15 → 16 taken 1 time.
✓ Branch 15 → 18 taken 30378 times.
30379 if (ret < 0) {
666 1 av_log(avctx, AV_LOG_ERROR,
667 "Error splitting the input into NAL units.\n");
668 1 return ret;
669 }
670
671
2/2
✓ Branch 18 → 19 taken 42 times.
✓ Branch 18 → 20 taken 30336 times.
30378 if (avctx->active_thread_type & FF_THREAD_FRAME)
672 42 nals_needed = get_last_needed_nal(h);
673
1/2
✗ Branch 20 → 21 not taken.
✓ Branch 20 → 22 taken 30378 times.
30378 if (nals_needed < 0)
674 ✗ return nals_needed;
675
676
2/2
✓ Branch 129 → 23 taken 54215 times.
✓ Branch 129 → 130 taken 30378 times.
84593 for (i = 0; i < h->pkt.nb_nals; i++) {
677 54215 H2645NAL *nal = &h->pkt.nals[i];
678 H264SliceContext *queued;
679 int max_slice_ctx, err;
680
681
2/2
✓ Branch 23 → 24 taken 928 times.
✓ Branch 23 → 27 taken 53287 times.
54215 if (avctx->skip_frame >= AVDISCARD_NONREF &&
682
4/4
✓ Branch 24 → 25 taken 824 times.
✓ Branch 24 → 27 taken 104 times.
✓ Branch 25 → 26 taken 504 times.
✓ Branch 25 → 27 taken 320 times.
928 nal->ref_idc == 0 && nal->type != H264_NAL_SEI)
683 504 continue;
684
685 // FIXME these should stop being context-global variables
686 53711 h->nal_ref_idc = nal->ref_idc;
687 53711 h->nal_unit_type = nal->type;
688
689 53711 err = 0;
690
7/8
✓ Branch 27 → 28 taken 2494 times.
✓ Branch 27 → 34 taken 37085 times.
✓ Branch 27 → 77 taken 105 times.
✓ Branch 27 → 83 taken 3126 times.
✓ Branch 27 → 89 taken 1119 times.
✓ Branch 27 → 107 taken 7065 times.
✓ Branch 27 → 119 taken 2717 times.
✗ Branch 27 → 120 not taken.
53711 switch (nal->type) {
691 2494 case H264_NAL_IDR_SLICE:
692
1/2
✗ Branch 28 → 29 not taken.
✓ Branch 28 → 31 taken 2494 times.
2494 if ((nal->data[1] & 0xFC) == 0x98) {
693 ✗ av_log(h->avctx, AV_LOG_ERROR, "Invalid inter IDR frame\n");
694 ✗ h->next_outputed_poc = INT_MIN;
695 ✗ ret = -1;
696 ✗ goto end;
697 }
698
2/2
✓ Branch 31 → 32 taken 1245 times.
✓ Branch 31 → 33 taken 1249 times.
2494 if(!idr_cleared) {
699 1245 idr(h); // FIXME ensure we don't lose some frames if there is reordering
700 }
701 2494 idr_cleared = 1;
702 2494 h->has_recovery_point = 1;
703 av_fallthrough;
704 39579 case H264_NAL_SLICE:
705 case H264_NAL_DPA:
706 39579 h->has_slice = 1;
707
708
2/2
✓ Branch 34 → 35 taken 60 times.
✓ Branch 34 → 43 taken 39519 times.
39579 if (nal->type == H264_NAL_DPA) {
709 /* partitioned streams all come from JM or a derivative */
710
2/2
✓ Branch 35 → 36 taken 30 times.
✓ Branch 35 → 37 taken 30 times.
60 if (h->workaround_bugs & FF_BUG_AUTODETECT)
711 30 h->workaround_bugs |= FF_BUG_H264_DP_NNZ;
712
713 /* hwaccels take one self-contained slice NAL, not three */
714
1/2
✗ Branch 37 → 38 not taken.
✓ Branch 37 → 40 taken 60 times.
60 if (avctx->hwaccel) {
715 ✗ avpriv_request_sample(avctx, "hardware accelerated data partitioning");
716 ✗ ret = AVERROR_PATCHWELCOME;
717 ✗ goto end;
718 }
719 /* the lookahead needs all three partitions in one packet */
720
1/2
✗ Branch 40 → 41 not taken.
✓ Branch 40 → 43 taken 60 times.
60 if (avctx->flags2 & AV_CODEC_FLAG2_CHUNKS) {
721 ✗ av_log(avctx, AV_LOG_ERROR, "Decoding in chunks is not "
722 "supported for partitioned slices\n");
723 ✗ ret = AVERROR(ENOSYS);
724 ✗ goto end;
725 }
726 }
727
728
2/2
✓ Branch 44 → 45 taken 167 times.
✓ Branch 44 → 46 taken 39412 times.
39579 if ((err = ff_h264_queue_decode_slice(h, nal, &queued))) {
729 167 H264SliceContext *sl = h->slice_ctx + h->nb_slice_ctx_queued;
730 167 sl->ref_count[0] = sl->ref_count[1] = 0;
731 167 break;
732 }
733
734
3/4
✓ Branch 46 → 47 taken 60 times.
✓ Branch 46 → 49 taken 39352 times.
✓ Branch 47 → 48 taken 60 times.
✗ Branch 47 → 49 not taken.
39412 if (nal->type == H264_NAL_DPA && queued)
735 60 dp_attached_to = h264_attach_partitions(h, queued, i);
736
737
2/2
✓ Branch 49 → 50 taken 29531 times.
✓ Branch 49 → 64 taken 9881 times.
39412 if (h->current_slice == 1) {
738
3/4
✓ Branch 50 → 51 taken 42 times.
✓ Branch 50 → 59 taken 29489 times.
✓ Branch 51 → 52 taken 42 times.
✗ Branch 51 → 59 not taken.
29531 if (avctx->active_thread_type & FF_THREAD_FRAME &&
739
2/4
✓ Branch 52 → 53 taken 42 times.
✗ Branch 52 → 59 not taken.
✓ Branch 53 → 54 taken 42 times.
✗ Branch 53 → 59 not taken.
42 i >= nals_needed && !h->setup_finished && h->cur_pic_ptr) {
740 42 ret = h264_decode_late_sei(h, i);
741
1/2
✗ Branch 55 → 56 not taken.
✓ Branch 55 → 57 taken 42 times.
42 if (ret < 0)
742 ✗ goto end;
743 42 ff_thread_finish_setup(avctx);
744 42 h->setup_finished = 1;
745 }
746
747
1/4
✗ Branch 59 → 60 not taken.
✓ Branch 59 → 64 taken 29531 times.
✗ Branch 62 → 63 not taken.
✗ Branch 62 → 64 not taken.
29531 if (h->avctx->hwaccel &&
748 ✗ (ret = FF_HW_CALL(h->avctx, start_frame, buf_ref,
749 buf, buf_size)) < 0)
750 ✗ goto end;
751 }
752
753
1/2
✓ Branch 64 → 65 taken 39412 times.
✗ Branch 64 → 66 not taken.
39412 max_slice_ctx = avctx->hwaccel ? 1 : h->nb_slice_ctx;
754
2/2
✓ Branch 67 → 68 taken 38582 times.
✓ Branch 67 → 76 taken 830 times.
39412 if (h->nb_slice_ctx_queued == max_slice_ctx) {
755
1/2
✗ Branch 68 → 69 not taken.
✓ Branch 68 → 72 taken 38582 times.
38582 if (h->avctx->hwaccel) {
756 ✗ ret = FF_HW_CALL(avctx, decode_slice, nal->raw_data, nal->raw_size);
757 ✗ h->nb_slice_ctx_queued = 0;
758 } else
759 38582 ret = ff_h264_execute_decode_slices(h);
760
3/4
✓ Branch 73 → 74 taken 18 times.
✓ Branch 73 → 76 taken 38564 times.
✗ Branch 74 → 75 not taken.
✓ Branch 74 → 76 taken 18 times.
38582 if (ret < 0 && (h->avctx->err_recognition & AV_EF_EXPLODE))
761 ✗ goto end;
762 }
763 39412 break;
764 105 case H264_NAL_DPB:
765 case H264_NAL_DPC:
766 /* not claimed by the lookahead above, so it has no partition A */
767
1/2
✗ Branch 77 → 78 not taken.
✓ Branch 77 → 82 taken 105 times.
105 if (i > dp_attached_to)
768 ✗ av_log(avctx, AV_LOG_WARNING, "Ignoring slice data partition "
769 "%c without a matching partition A\n",
770
0/2
✗ Branch 78 → 79 not taken.
✗ Branch 78 → 80 not taken.
✗ nal->type == H264_NAL_DPB ? 'B' : 'C');
771 105 break;
772 3126 case H264_NAL_SEI:
773 /* already decoded by h264_decode_late_sei() */
774
1/2
✗ Branch 83 → 84 not taken.
✓ Branch 83 → 85 taken 3126 times.
3126 if (h->setup_finished)
775 ✗ break;
776 3126 ret = h264_decode_sei(h, nal);
777
1/2
✗ Branch 86 → 87 not taken.
✓ Branch 86 → 88 taken 3126 times.
3126 if (ret < 0)
778 ✗ goto end;
779 3126 break;
780 1119 case H264_NAL_SPS: {
781 1119 GetBitContext tmp_gb = nal->gb;
782
1/4
✗ Branch 89 → 90 not taken.
✓ Branch 89 → 96 taken 1119 times.
✗ Branch 91 → 92 not taken.
✗ Branch 91 → 96 not taken.
1119 if (FF_HW_HAS_CB(avctx, decode_params)) {
783 ✗ ret = FF_HW_CALL(avctx, decode_params,
784 nal->type, nal->raw_data, nal->raw_size);
785
0/2
✗ Branch 94 → 95 not taken.
✗ Branch 94 → 96 not taken.
✗ if (ret < 0)
786 ✗ goto end;
787 }
788
2/2
✓ Branch 97 → 98 taken 1117 times.
✓ Branch 97 → 99 taken 2 times.
1119 if (ff_h264_decode_seq_parameter_set(&tmp_gb, avctx, &h->ps, 0) >= 0)
789 1119 break;
790 2 av_log(h->avctx, AV_LOG_DEBUG,
791 "SPS decoding failure, trying again with the complete NAL\n");
792 2 init_get_bits8(&tmp_gb, nal->raw_data + 1, nal->raw_size - 1);
793
1/2
✓ Branch 102 → 103 taken 2 times.
✗ Branch 102 → 104 not taken.
2 if (ff_h264_decode_seq_parameter_set(&tmp_gb, avctx, &h->ps, 0) >= 0)
794 2 break;
795 ✗ ff_h264_decode_seq_parameter_set(&nal->gb, avctx, &h->ps, 1);
796 ✗ break;
797 }
798 7065 case H264_NAL_PPS:
799
1/4
✗ Branch 107 → 108 not taken.
✓ Branch 107 → 114 taken 7065 times.
✗ Branch 109 → 110 not taken.
✗ Branch 109 → 114 not taken.
7065 if (FF_HW_HAS_CB(avctx, decode_params)) {
800 ✗ ret = FF_HW_CALL(avctx, decode_params,
801 nal->type, nal->raw_data, nal->raw_size);
802
0/2
✗ Branch 112 → 113 not taken.
✗ Branch 112 → 114 not taken.
✗ if (ret < 0)
803 ✗ goto end;
804 }
805 7065 ret = ff_h264_decode_picture_parameter_set(&nal->gb, avctx, &h->ps,
806 nal->size_bits);
807
3/4
✓ Branch 115 → 116 taken 7 times.
✓ Branch 115 → 118 taken 7058 times.
✗ Branch 116 → 117 not taken.
✓ Branch 116 → 118 taken 7 times.
7065 if (ret < 0 && (h->avctx->err_recognition & AV_EF_EXPLODE))
808 ✗ goto end;
809 7065 break;
810 2717 case H264_NAL_AUD:
811 case H264_NAL_END_SEQUENCE:
812 case H264_NAL_END_STREAM:
813 case H264_NAL_FILLER_DATA:
814 case H264_NAL_SPS_EXT:
815 case H264_NAL_AUXILIARY_SLICE:
816 2717 break;
817 ✗ default:
818 ✗ av_log(avctx, AV_LOG_DEBUG, "Unknown NAL code: %d (%d bits)\n",
819 nal->type, nal->size_bits);
820 }
821
822
3/4
✓ Branch 121 → 122 taken 167 times.
✓ Branch 121 → 125 taken 53544 times.
✗ Branch 122 → 123 not taken.
✓ Branch 122 → 125 taken 167 times.
53711 if (err < 0 && (h->avctx->err_recognition & AV_EF_EXPLODE)) {
823 ✗ av_log(h->avctx, AV_LOG_ERROR, "decode_slice_header error\n");
824 ✗ ret = err;
825 ✗ goto end;
826 }
827 }
828
829 30378 ret = ff_h264_execute_decode_slices(h);
830
1/4
✗ Branch 131 → 132 not taken.
✓ Branch 131 → 134 taken 30378 times.
✗ Branch 132 → 133 not taken.
✗ Branch 132 → 134 not taken.
30378 if (ret < 0 && (h->avctx->err_recognition & AV_EF_EXPLODE))
831 ✗ goto end;
832
833 // set decode_error_flags to allow users to detect concealed decoding errors
834
4/6
✓ Branch 134 → 135 taken 30378 times.
✗ Branch 134 → 136 not taken.
✓ Branch 135 → 136 taken 18 times.
✓ Branch 135 → 140 taken 30360 times.
✓ Branch 136 → 137 taken 18 times.
✗ Branch 136 → 140 not taken.
30378 if ((ret < 0 || h->er.error_occurred) && h->cur_pic_ptr) {
835
1/2
✗ Branch 137 → 138 not taken.
✓ Branch 137 → 139 taken 18 times.
18 if (h->cur_pic_ptr->decode_error_flags) {
836 /* Frame-threading in use */
837 ✗ atomic_int *decode_error = h->cur_pic_ptr->decode_error_flags;
838 /* Using atomics here is not supposed to provide synchronisation;
839 * they are merely used to allow to set decode_error from both
840 * decoding threads in case of coded slices. */
841 ✗ atomic_fetch_or_explicit(decode_error, FF_DECODE_ERROR_DECODE_SLICES,
842 memory_order_relaxed);
843 } else
844 18 h->cur_pic_ptr->f->decode_error_flags |= FF_DECODE_ERROR_DECODE_SLICES;
845 }
846
847 30378 ret = 0;
848 30378 end:
849
850 #if CONFIG_ERROR_RESILIENCE
851 /*
852 * FIXME: Error handling code does not seem to support interlaced
853 * when slices span multiple rows
854 * The ff_er_add_slice calls don't work right for bottom
855 * fields; they cause massive erroneous error concealing
856 * Error marking covers both fields (top and bottom).
857 * This causes a mismatched s->error_count
858 * and a bad error table. Further, the error count goes to
859 * INT_MAX when called for bottom field, because mb_y is
860 * past end by one (callers fault) and resync_mb_y != 0
861 * causes problems for the first MB line, too.
862 */
863
8/8
✓ Branch 141 → 142 taken 23114 times.
✓ Branch 141 → 167 taken 7264 times.
✓ Branch 142 → 143 taken 23075 times.
✓ Branch 142 → 167 taken 39 times.
✓ Branch 143 → 144 taken 22325 times.
✓ Branch 143 → 167 taken 750 times.
✓ Branch 145 → 146 taken 22274 times.
✓ Branch 145 → 167 taken 51 times.
52703 if (!FIELD_PICTURE(h) && h->current_slice && h->enable_er &&
864 22325 !ff_decode_skip_all_pixels(h->avctx)) {
865
866 22274 H264SliceContext *sl = h->slice_ctx;
867
3/4
✓ Branch 146 → 147 taken 582 times.
✓ Branch 146 → 149 taken 21692 times.
✓ Branch 147 → 148 taken 582 times.
✗ Branch 147 → 149 not taken.
22274 int use_last_pic = h->last_pic_for_ec.f->buf[0] && !sl->ref_count[0];
868 22274 int decode_error_flags = 0;
869
870 22274 ff_h264_set_erpic(&h->er.cur_pic, h->cur_pic_ptr);
871
872
2/2
✓ Branch 151 → 152 taken 582 times.
✓ Branch 151 → 154 taken 21692 times.
22274 if (use_last_pic) {
873 582 ff_h264_set_erpic(&h->er.last_pic, &h->last_pic_for_ec);
874 582 sl->ref_list[0][0].parent = &h->last_pic_for_ec;
875 582 memcpy(sl->ref_list[0][0].data, h->last_pic_for_ec.f->data, sizeof(sl->ref_list[0][0].data));
876 582 memcpy(sl->ref_list[0][0].linesize, h->last_pic_for_ec.f->linesize, sizeof(sl->ref_list[0][0].linesize));
877 582 sl->ref_list[0][0].reference = h->last_pic_for_ec.reference;
878
2/2
✓ Branch 154 → 155 taken 19624 times.
✓ Branch 154 → 156 taken 2068 times.
21692 } else if (sl->ref_count[0]) {
879 19624 ff_h264_set_erpic(&h->er.last_pic, sl->ref_list[0][0].parent);
880 } else
881 2068 ff_h264_set_erpic(&h->er.last_pic, NULL);
882
883
2/2
✓ Branch 157 → 158 taken 19624 times.
✓ Branch 157 → 159 taken 2650 times.
22274 if (sl->ref_count[1])
884 19624 ff_h264_set_erpic(&h->er.next_pic, sl->ref_list[1][0].parent);
885
886 22274 ff_er_frame_end(&h->er, &decode_error_flags);
887
2/2
✓ Branch 160 → 161 taken 17 times.
✓ Branch 160 → 164 taken 22257 times.
22274 if (decode_error_flags) {
888
1/2
✗ Branch 161 → 162 not taken.
✓ Branch 161 → 163 taken 17 times.
17 if (h->cur_pic_ptr->decode_error_flags) {
889 ✗ atomic_int *decode_error = h->cur_pic_ptr->decode_error_flags;
890 ✗ atomic_fetch_or_explicit(decode_error, decode_error_flags,
891 memory_order_relaxed);
892 } else
893 17 h->cur_pic_ptr->f->decode_error_flags |= decode_error_flags;
894 }
895
2/2
✓ Branch 164 → 165 taken 582 times.
✓ Branch 164 → 166 taken 21692 times.
22274 if (use_last_pic)
896 582 memset(&sl->ref_list[0][0], 0, sizeof(sl->ref_list[0][0]));
897 }
898 #endif /* CONFIG_ERROR_RESILIENCE */
899 /* clean up */
900
6/6
✓ Branch 167 → 168 taken 29950 times.
✓ Branch 167 → 171 taken 428 times.
✓ Branch 168 → 169 taken 19945 times.
✓ Branch 168 → 171 taken 10005 times.
✓ Branch 169 → 170 taken 19779 times.
✓ Branch 169 → 171 taken 166 times.
30378 if (h->cur_pic_ptr && !h->droppable && h->has_slice) {
901 19779 ff_thread_report_progress(&h->cur_pic_ptr->tf, INT_MAX,
902 19779 h->picture_structure == PICT_BOTTOM_FIELD);
903 }
904
905
1/2
✗ Branch 171 → 172 not taken.
✓ Branch 171 → 173 taken 30378 times.
30378 return (ret < 0) ? ret : buf_size;
906 }
907
908 1 static int h264_export_enc_params(AVFrame *f, const H264Picture *p)
909 {
910 AVVideoEncParams *par;
911 1 unsigned int nb_mb = p->mb_height * p->mb_width;
912 unsigned int x, y;
913
914 1 par = av_video_enc_params_create_side_data(f, AV_VIDEO_ENC_PARAMS_H264, nb_mb);
915
1/2
✗ Branch 3 → 4 not taken.
✓ Branch 3 → 5 taken 1 time.
1 if (!par)
916 ✗ return AVERROR(ENOMEM);
917
918 1 par->qp = p->pps->init_qp;
919
920 1 par->delta_qp[1][0] = p->pps->chroma_qp_index_offset[0];
921 1 par->delta_qp[1][1] = p->pps->chroma_qp_index_offset[0];
922 1 par->delta_qp[2][0] = p->pps->chroma_qp_index_offset[1];
923 1 par->delta_qp[2][1] = p->pps->chroma_qp_index_offset[1];
924
925
2/2
✓ Branch 11 → 6 taken 18 times.
✓ Branch 11 → 12 taken 1 time.
19 for (y = 0; y < p->mb_height; y++)
926
2/2
✓ Branch 9 → 7 taken 396 times.
✓ Branch 9 → 10 taken 18 times.
414 for (x = 0; x < p->mb_width; x++) {
927 396 const unsigned int block_idx = y * p->mb_width + x;
928 396 const unsigned int mb_xy = y * p->mb_stride + x;
929 396 AVVideoBlockParams *b = av_video_enc_params_block(par, block_idx);
930
931 396 b->src_x = x * 16;
932 396 b->src_y = y * 16;
933 396 b->w = 16;
934 396 b->h = 16;
935
936 396 b->delta_qp = p->qscale_table[mb_xy] - par->qp;
937 }
938
939 1 return 0;
940 }
941
942 25351 static int output_frame(H264Context *h, AVFrame *dst, H264Picture *srcp)
943 {
944 int ret;
945
946
1/2
✗ Branch 2 → 3 not taken.
✓ Branch 2 → 4 taken 25351 times.
25351 ret = av_frame_ref(dst, srcp->needs_fg ? srcp->f_grain : srcp->f);
947
1/2
✗ Branch 6 → 7 not taken.
✓ Branch 6 → 8 taken 25351 times.
25351 if (ret < 0)
948 ✗ return ret;
949
950
1/4
✗ Branch 8 → 9 not taken.
✓ Branch 8 → 12 taken 25351 times.
✗ Branch 10 → 11 not taken.
✗ Branch 10 → 12 not taken.
25351 if (srcp->needs_fg && (ret = av_frame_copy_props(dst, srcp->f)) < 0)
951 ✗ return ret;
952
953
2/2
✓ Branch 12 → 13 taken 42 times.
✓ Branch 12 → 14 taken 25309 times.
25351 if (srcp->decode_error_flags) {
954 42 atomic_int *decode_error = srcp->decode_error_flags;
955 /* The following is not supposed to provide synchronisation at all:
956 * given that srcp has already finished decoding, decode_error
957 * has already been set to its final value. */
958 42 dst->decode_error_flags |= atomic_load_explicit(decode_error, memory_order_relaxed);
959 }
960
961 25351 av_dict_set(&dst->metadata, "stereo_mode", ff_h264_sei_stereo_mode(&h->sei.common.frame_packing), 0);
962
963
2/2
✓ Branch 16 → 17 taken 34 times.
✓ Branch 16 → 18 taken 25317 times.
25351 if (srcp->sei_recovery_frame_cnt == 0)
964 34 dst->flags |= AV_FRAME_FLAG_KEY;
965
966
2/2
✓ Branch 18 → 19 taken 1 time.
✓ Branch 18 → 22 taken 25350 times.
25351 if (h->avctx->export_side_data & AV_CODEC_EXPORT_DATA_VIDEO_ENC_PARAMS) {
967 1 ret = h264_export_enc_params(dst, srcp);
968
1/2
✗ Branch 20 → 21 not taken.
✓ Branch 20 → 22 taken 1 time.
1 if (ret < 0)
969 ✗ goto fail;
970 }
971
972
1/2
✓ Branch 22 → 23 taken 25351 times.
✗ Branch 22 → 24 not taken.
25351 if (!(h->avctx->export_side_data & AV_CODEC_EXPORT_DATA_FILM_GRAIN))
973 25351 av_frame_remove_side_data(dst, AV_FRAME_DATA_FILM_GRAIN_PARAMS);
974
975 25351 return 0;
976 ✗ fail:
977 ✗ av_frame_unref(dst);
978 ✗ return ret;
979 }
980
981 ✗ static int is_avcc_extradata(const uint8_t *buf, int buf_size)
982 {
983 ✗ int cnt= buf[5]&0x1f;
984 ✗ const uint8_t *p= buf+6;
985
0/2
✗ Branch 2 → 3 not taken.
✗ Branch 2 → 4 not taken.
✗ if (!cnt)
986 ✗ return 0;
987
0/2
✗ Branch 9 → 5 not taken.
✗ Branch 9 → 10 not taken.
✗ while(cnt--){
988 ✗ int nalsize= AV_RB16(p) + 2;
989
0/4
✗ Branch 5 → 6 not taken.
✗ Branch 5 → 7 not taken.
✗ Branch 6 → 7 not taken.
✗ Branch 6 → 8 not taken.
✗ if(nalsize > buf_size - (p-buf) || (p[2] & 0x9F) != 7)
990 ✗ return 0;
991 ✗ p += nalsize;
992 }
993 ✗ cnt = *(p++);
994
0/2
✗ Branch 10 → 11 not taken.
✗ Branch 10 → 12 not taken.
✗ if(!cnt)
995 ✗ return 0;
996
0/2
✗ Branch 17 → 13 not taken.
✗ Branch 17 → 18 not taken.
✗ while(cnt--){
997 ✗ int nalsize= AV_RB16(p) + 2;
998
0/4
✗ Branch 13 → 14 not taken.
✗ Branch 13 → 15 not taken.
✗ Branch 14 → 15 not taken.
✗ Branch 14 → 16 not taken.
✗ if(nalsize > buf_size - (p-buf) || (p[2] & 0x9F) != 8)
999 ✗ return 0;
1000 ✗ p += nalsize;
1001 }
1002 ✗ return 1;
1003 }
1004
1005 25351 static int finalize_frame(H264Context *h, AVFrame *dst, H264Picture *out, int *got_frame)
1006 {
1007 int ret;
1008
1009
1/2
✓ Branch 2 → 3 taken 25351 times.
✗ Branch 2 → 5 not taken.
25351 if (((h->avctx->flags & AV_CODEC_FLAG_OUTPUT_CORRUPT) ||
1010
1/2
✓ Branch 3 → 4 taken 25351 times.
✗ Branch 3 → 5 not taken.
25351 (h->avctx->flags2 & AV_CODEC_FLAG2_SHOW_ALL) ||
1011
1/2
✓ Branch 4 → 5 taken 25351 times.
✗ Branch 4 → 31 not taken.
25351 out->recovered)) {
1012
1013
1/2
✗ Branch 5 → 6 not taken.
✓ Branch 5 → 10 taken 25351 times.
25351 if (h->skip_gray > 0 &&
1014
0/4
✗ Branch 6 → 7 not taken.
✗ Branch 6 → 10 not taken.
✗ Branch 7 → 8 not taken.
✗ Branch 7 → 10 not taken.
✗ h->non_gray && out->gray &&
1015
0/2
✗ Branch 8 → 9 not taken.
✗ Branch 8 → 10 not taken.
✗ !(h->avctx->flags2 & AV_CODEC_FLAG2_SHOW_ALL)
1016 )
1017 ✗ return 0;
1018
1019
3/4
✓ Branch 10 → 11 taken 25351 times.
✗ Branch 10 → 27 not taken.
✓ Branch 12 → 13 taken 25277 times.
✓ Branch 12 → 27 taken 74 times.
25351 if (!h->avctx->hwaccel && !ff_decode_skip_all_pixels(h->avctx) &&
1020
1/2
✓ Branch 13 → 14 taken 25277 times.
✗ Branch 13 → 15 not taken.
25277 (out->field_poc[0] == INT_MAX ||
1021
1/2
✗ Branch 14 → 15 not taken.
✓ Branch 14 → 27 taken 25277 times.
25277 out->field_poc[1] == INT_MAX)
1022 ) {
1023 int p;
1024 ✗ AVFrame *f = out->f;
1025 ✗ int field = out->field_poc[0] == INT_MAX;
1026 uint8_t *dst_data[4];
1027 int linesizes[4];
1028 const uint8_t *src_data[4];
1029
1030 ✗ av_log(h->avctx, AV_LOG_DEBUG, "Duplicating field %d to fill missing\n", field);
1031
1032
0/2
✗ Branch 24 → 17 not taken.
✗ Branch 24 → 25 not taken.
✗ for (p = 0; p<4; p++) {
1033
0/2
✗ Branch 17 → 18 not taken.
✗ Branch 17 → 19 not taken.
✗ dst_data[p] = FF_PTR_ADD(f->data[p], (field^1)*f->linesize[p]);
1034
0/2
✗ Branch 20 → 21 not taken.
✗ Branch 20 → 22 not taken.
✗ src_data[p] = FF_PTR_ADD(f->data[p], field *f->linesize[p]);
1035 ✗ linesizes[p] = 2*f->linesize[p];
1036 }
1037
1038 ✗ av_image_copy(dst_data, linesizes, src_data, linesizes,
1039 ✗ f->format, f->width, f->height>>1);
1040 }
1041
1042 25351 ret = output_frame(h, dst, out);
1043
1/2
✗ Branch 28 → 29 not taken.
✓ Branch 28 → 30 taken 25351 times.
25351 if (ret < 0)
1044 ✗ return ret;
1045
1046 25351 *got_frame = 1;
1047
1048 if (CONFIG_MPEGVIDEODEC) {
1049 25351 ff_print_debug_info2(h->avctx, dst,
1050 25351 out->mb_type,
1051 25351 out->qscale_table,
1052 25351 out->motion_val,
1053 out->mb_width, out->mb_height, out->mb_stride, 1);
1054 }
1055 }
1056
1057 25351 return 0;
1058 }
1059
1060 625 static int send_next_delayed_frame(H264Context *h, AVFrame *dst_frame,
1061 int *got_frame, int buf_index)
1062 {
1063 int ret, i, out_idx;
1064 H264Picture *out;
1065
1066 625 h->cur_pic_ptr = NULL;
1067 625 h->first_field = 0;
1068
1069
2/2
✓ Branch 22 → 3 taken 321 times.
✓ Branch 22 → 23 taken 304 times.
1250 while (h->delayed_pic[0]) {
1070 321 out = h->delayed_pic[0];
1071 321 out_idx = 0;
1072 321 for (i = 1;
1073 1729 h->delayed_pic[i] &&
1074
4/4
✓ Branch 7 → 8 taken 708 times.
✓ Branch 7 → 10 taken 313 times.
✓ Branch 8 → 9 taken 700 times.
✓ Branch 8 → 10 taken 8 times.
1021 !(h->delayed_pic[i]->f->flags & AV_FRAME_FLAG_KEY) &&
1075
1/2
✓ Branch 9 → 4 taken 700 times.
✗ Branch 9 → 10 not taken.
700 !h->delayed_pic[i]->mmco_reset;
1076 700 i++)
1077
2/2
✓ Branch 4 → 5 taken 118 times.
✓ Branch 4 → 6 taken 582 times.
700 if (h->delayed_pic[i]->poc < out->poc) {
1078 118 out = h->delayed_pic[i];
1079 118 out_idx = i;
1080 }
1081
1082
2/2
✓ Branch 12 → 11 taken 979 times.
✓ Branch 12 → 13 taken 321 times.
1300 for (i = out_idx; h->delayed_pic[i]; i++)
1083 979 h->delayed_pic[i] = h->delayed_pic[i + 1];
1084
1085
1/2
✗ Branch 13 → 14 not taken.
✓ Branch 13 → 15 taken 321 times.
321 if (out) {
1086 321 h->frame_recovered |= out->recovered;
1087 321 out->recovered |= h->frame_recovered & FRAME_RECOVERED_SEI;
1088
1089 321 out->reference &= ~DELAYED_PIC_REF;
1090 321 ret = finalize_frame(h, dst_frame, out, got_frame);
1091
1/2
✗ Branch 16 → 17 not taken.
✓ Branch 16 → 18 taken 321 times.
321 if (ret < 0)
1092 ✗ return ret;
1093
1/2
✓ Branch 18 → 19 taken 321 times.
✗ Branch 18 → 20 not taken.
321 if (*got_frame)
1094 321 break;
1095 }
1096 }
1097
1098 625 return buf_index;
1099 }
1100
1101 31004 static int h264_decode_frame(AVCodecContext *avctx, AVFrame *pict,
1102 int *got_frame, AVPacket *avpkt)
1103 {
1104 31004 const uint8_t *buf = avpkt->data;
1105 31004 int buf_size = avpkt->size;
1106 31004 H264Context *h = avctx->priv_data;
1107 int buf_index;
1108 int ret;
1109
1110 31004 h->flags = avctx->flags;
1111 31004 h->setup_finished = 0;
1112 31004 h->nb_slice_ctx_queued = 0;
1113
1114 31004 ff_h264_unref_picture(&h->last_pic_for_ec);
1115
1116 /* end of stream, output what is still in the buffers */
1117
2/2
✓ Branch 3 → 4 taken 625 times.
✓ Branch 3 → 6 taken 30379 times.
31004 if (buf_size == 0)
1118 625 return send_next_delayed_frame(h, pict, got_frame, 0);
1119
1120
2/2
✓ Branch 7 → 8 taken 4 times.
✓ Branch 7 → 11 taken 30375 times.
30379 if (av_packet_get_side_data(avpkt, AV_PKT_DATA_NEW_EXTRADATA, NULL)) {
1121 size_t side_size;
1122 4 uint8_t *side = av_packet_get_side_data(avpkt, AV_PKT_DATA_NEW_EXTRADATA, &side_size);
1123 4 ff_h264_decode_extradata(side, side_size,
1124 &h->ps, &h->is_avc, &h->nal_length_size,
1125 avctx->err_recognition, avctx);
1126 }
1127
4/10
✓ Branch 11 → 12 taken 3503 times.
✓ Branch 11 → 20 taken 26876 times.
✓ Branch 12 → 13 taken 3503 times.
✗ Branch 12 → 20 not taken.
✗ Branch 13 → 14 not taken.
✓ Branch 13 → 20 taken 3503 times.
✗ Branch 14 → 15 not taken.
✗ Branch 14 → 20 not taken.
✗ Branch 15 → 16 not taken.
✗ Branch 15 → 20 not taken.
30379 if (h->is_avc && buf_size >= 9 && buf[0]==1 && buf[2]==0 && (buf[4]&0xFC)==0xFC) {
1128
0/2
✗ Branch 17 → 18 not taken.
✗ Branch 17 → 20 not taken.
✗ if (is_avcc_extradata(buf, buf_size))
1129 ✗ return ff_h264_decode_extradata(buf, buf_size,
1130 &h->ps, &h->is_avc, &h->nal_length_size,
1131 avctx->err_recognition, avctx);
1132 }
1133
1134 30379 buf_index = decode_nal_units(h, avpkt->buf, buf, buf_size);
1135
2/2
✓ Branch 21 → 22 taken 1 time.
✓ Branch 21 → 23 taken 30378 times.
30379 if (buf_index < 0)
1136 1 return AVERROR_INVALIDDATA;
1137
1138
3/4
✓ Branch 23 → 24 taken 428 times.
✓ Branch 23 → 30 taken 29950 times.
✗ Branch 24 → 25 not taken.
✓ Branch 24 → 30 taken 428 times.
30378 if (!h->cur_pic_ptr && h->nal_unit_type == H264_NAL_END_SEQUENCE) {
1139
0/2
✗ Branch 25 → 26 not taken.
✗ Branch 25 → 28 not taken.
✗ av_assert0(buf_index <= buf_size);
1140 ✗ return send_next_delayed_frame(h, pict, got_frame, buf_index);
1141 }
1142
1143
6/6
✓ Branch 30 → 31 taken 29622 times.
✓ Branch 30 → 39 taken 756 times.
✓ Branch 31 → 32 taken 29196 times.
✓ Branch 31 → 33 taken 426 times.
✓ Branch 32 → 33 taken 164 times.
✓ Branch 32 → 39 taken 29032 times.
30378 if (!(avctx->flags2 & AV_CODEC_FLAG2_CHUNKS) && (!h->cur_pic_ptr || !h->has_slice)) {
1144
3/4
✓ Branch 33 → 34 taken 390 times.
✓ Branch 33 → 36 taken 200 times.
✓ Branch 34 → 35 taken 390 times.
✗ Branch 34 → 37 not taken.
590 if (avctx->skip_frame >= AVDISCARD_NONREF ||
1145
1/2
✗ Branch 35 → 36 not taken.
✓ Branch 35 → 37 taken 390 times.
390 buf_size >= 4 && !memcmp("Q264", buf, 4))
1146 200 return buf_size;
1147 390 av_log(avctx, AV_LOG_ERROR, "no frame!\n");
1148 390 return AVERROR_INVALIDDATA;
1149 }
1150
1151
2/2
✓ Branch 39 → 40 taken 756 times.
✓ Branch 39 → 42 taken 29032 times.
29788 if (!(avctx->flags2 & AV_CODEC_FLAG2_CHUNKS) ||
1152
4/4
✓ Branch 40 → 41 taken 302 times.
✓ Branch 40 → 49 taken 454 times.
✓ Branch 41 → 42 taken 300 times.
✓ Branch 41 → 49 taken 2 times.
756 (h->mb_y >= h->mb_height && h->mb_height)) {
1153
1/2
✗ Branch 43 → 44 not taken.
✓ Branch 43 → 45 taken 29332 times.
29332 if ((ret = ff_h264_field_end(h, &h->slice_ctx[0], 0)) < 0)
1154 ✗ return ret;
1155
1156 /* Wait for second field. */
1157
2/2
✓ Branch 45 → 46 taken 25030 times.
✓ Branch 45 → 49 taken 4302 times.
29332 if (h->next_output_pic) {
1158 25030 ret = finalize_frame(h, pict, h->next_output_pic, got_frame);
1159
1/2
✗ Branch 47 → 48 not taken.
✓ Branch 47 → 49 taken 25030 times.
25030 if (ret < 0)
1160 ✗ return ret;
1161 }
1162 }
1163
1164
3/4
✓ Branch 49 → 50 taken 4758 times.
✓ Branch 49 → 53 taken 25030 times.
✗ Branch 50 → 51 not taken.
✓ Branch 50 → 53 taken 4758 times.
29788 av_assert0(pict->buf[0] || !*got_frame);
1165
1166 29788 ff_h264_unref_picture(&h->last_pic_for_ec);
1167
1168 29788 return buf_size;
1169 }
1170
1171 #define OFFSET(x) offsetof(H264Context, x)
1172 #define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
1173 #define VDX VD | AV_OPT_FLAG_EXPORT
1174 static const AVOption h264_options[] = {
1175 { "is_avc", "is avc", OFFSET(is_avc), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, VDX },
1176 { "nal_length_size", "nal_length_size", OFFSET(nal_length_size), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 4, VDX },
1177 { "enable_er", "Enable error resilience on damaged frames (unsafe)", OFFSET(enable_er), AV_OPT_TYPE_BOOL, { .i64 = -1 }, -1, 1, VD },
1178 { "x264_build", "Assume this x264 version if no x264 version found in any SEI", OFFSET(x264_build), AV_OPT_TYPE_INT, {.i64 = -1}, -1, INT_MAX, VD },
1179 { "skip_gray", "Do not return gray gap frames", OFFSET(skip_gray), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, VD },
1180 { "noref_gray", "Avoid using gray gap frames as references", OFFSET(noref_gray), AV_OPT_TYPE_BOOL, {.i64 = 1}, 0, 1, VD },
1181 { NULL },
1182 };
1183
1184 static const AVClass h264_class = {
1185 .class_name = "H264 Decoder",
1186 .item_name = av_default_item_name,
1187 .option = h264_options,
1188 .version = LIBAVUTIL_VERSION_INT,
1189 };
1190
1191 const FFCodec ff_h264_decoder = {
1192 .p.name = "h264",
1193 CODEC_LONG_NAME("H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10"),
1194 .p.type = AVMEDIA_TYPE_VIDEO,
1195 .p.id = AV_CODEC_ID_H264,
1196 .priv_data_size = sizeof(H264Context),
1197 .init = h264_decode_init,
1198 .close = h264_decode_end,
1199 FF_CODEC_DECODE_CB(h264_decode_frame),
1200 .p.capabilities = AV_CODEC_CAP_DR1 |
1201 AV_CODEC_CAP_DELAY | AV_CODEC_CAP_SLICE_THREADS |
1202 AV_CODEC_CAP_FRAME_THREADS,
1203 .hw_configs = (const AVCodecHWConfigInternal *const []) {
1204 #if CONFIG_H264_DXVA2_HWACCEL
1205 HWACCEL_DXVA2(h264),
1206 #endif
1207 #if CONFIG_H264_D3D11VA_HWACCEL
1208 HWACCEL_D3D11VA(h264),
1209 #endif
1210 #if CONFIG_H264_D3D11VA2_HWACCEL
1211 HWACCEL_D3D11VA2(h264),
1212 #endif
1213 #if CONFIG_H264_D3D12VA_HWACCEL
1214 HWACCEL_D3D12VA(h264),
1215 #endif
1216 #if CONFIG_H264_NVDEC_HWACCEL
1217 HWACCEL_NVDEC(h264),
1218 #endif
1219 #if CONFIG_H264_NVDEC_CUARRAY_HWACCEL
1220 HWACCEL_NVDEC_CUARRAY(h264),
1221 #endif
1222 #if CONFIG_H264_VAAPI_HWACCEL
1223 HWACCEL_VAAPI(h264),
1224 #endif
1225 #if CONFIG_H264_VDPAU_HWACCEL
1226 HWACCEL_VDPAU(h264),
1227 #endif
1228 #if CONFIG_H264_VIDEOTOOLBOX_HWACCEL
1229 HWACCEL_VIDEOTOOLBOX(h264),
1230 #endif
1231 #if CONFIG_H264_VULKAN_HWACCEL
1232 HWACCEL_VULKAN(h264),
1233 #endif
1234 NULL
1235 },
1236 .caps_internal = FF_CODEC_CAP_EXPORTS_CROPPING |
1237 FF_CODEC_CAP_INIT_CLEANUP,
1238 .flush = h264_decode_flush,
1239 UPDATE_THREAD_CONTEXT(ff_h264_update_thread_context),
1240 UPDATE_THREAD_CONTEXT_FOR_USER(ff_h264_update_thread_context_for_user),
1241 .p.profiles = NULL_IF_CONFIG_SMALL(ff_h264_profiles),
1242 .p.priv_class = &h264_class,
1243 };
1244