FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/h264dec.c
Date: 2026-09-16 07:09:49
Exec Total Coverage
Lines: 495 663 74.7%
Functions: 22 24 91.7%
Branches: 279 471 59.2%

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