FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/h264dec.c
Date: 2026-04-11 18:33:02
Exec Total Coverage
Lines: 459 637 72.1%
Functions: 20 23 87.0%
Branches: 255 443 57.6%

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 6612 int avpriv_h264_has_num_reorder_frames(AVCodecContext *avctx)
63 {
64 6612 H264Context *h = avctx->priv_data;
65
3/4
✓ Branch 0 taken 6612 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6484 times.
✓ Branch 3 taken 128 times.
6612 return h && h->ps.sps ? h->ps.sps->num_reorder_frames : 0;
66 }
67
68 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 const H264Context *h = opaque;
73 H264SliceContext *sl = &h->slice_ctx[0];
74
75 sl->mb_x = mb_x;
76 sl->mb_y = mb_y;
77 sl->mb_xy = mb_x + mb_y * h->mb_stride;
78 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 if (ref >= sl->ref_count[0])
85 ref = 0;
86 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 if ((sl->ref_list[0][ref].reference&3) != 3) {
91 av_log(h->avctx, AV_LOG_DEBUG, "Reference invalid\n");
92 return;
93 }
94 fill_rectangle(&h->cur_pic.ref_index[0][4 * sl->mb_xy],
95 2, 2, 2, ref, 1);
96 fill_rectangle(&sl->ref_cache[0][scan8[0]], 4, 4, 8, ref, 1);
97 fill_rectangle(sl->mv_cache[0][scan8[0]], 4, 4, 8,
98 pack16to32((*mv)[0][0][0], (*mv)[0][0][1]), 4);
99 sl->mb_mbaff =
100 sl->mb_field_decoding_flag = 0;
101 ff_h264_hl_decode_mb(h, &h->slice_ctx[0]);
102 }
103
104 387864 void ff_h264_draw_horiz_band(const H264Context *h, H264SliceContext *sl,
105 int y, int height)
106 {
107 387864 AVCodecContext *avctx = h->avctx;
108 387864 const AVFrame *src = h->cur_pic.f;
109 const AVPixFmtDescriptor *desc;
110 int offset[AV_NUM_DATA_POINTERS];
111 int vshift;
112 387864 const int field_pic = h->picture_structure != PICT_FRAME;
113
114
1/2
✓ Branch 0 taken 387864 times.
✗ Branch 1 not taken.
387864 if (!avctx->draw_horiz_band)
115 387864 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 1264 void ff_h264_free_tables(H264Context *h)
143 {
144 int i;
145
146 1264 av_freep(&h->intra4x4_pred_mode);
147 1264 av_freep(&h->chroma_pred_mode_table);
148 1264 av_freep(&h->cbp_table);
149 1264 av_freep(&h->mvd_table[0]);
150 1264 av_freep(&h->mvd_table[1]);
151 1264 av_freep(&h->direct_table);
152 1264 av_freep(&h->non_zero_count);
153 1264 av_freep(&h->slice_table_base);
154 1264 h->slice_table = NULL;
155 1264 av_freep(&h->list_counts);
156
157 1264 av_freep(&h->mb2b_xy);
158 1264 av_freep(&h->mb2br_xy);
159
160 1264 av_refstruct_pool_uninit(&h->qscale_table_pool);
161 1264 av_refstruct_pool_uninit(&h->mb_type_pool);
162 1264 av_refstruct_pool_uninit(&h->motion_val_pool);
163 1264 av_refstruct_pool_uninit(&h->ref_index_pool);
164
165 #if CONFIG_ERROR_RESILIENCE
166 1264 av_freep(&h->er.mb_index2xy);
167 1264 av_freep(&h->er.error_status_table);
168 1264 av_freep(&h->er.er_temp_buffer);
169 1264 av_freep(&h->dc_val_base);
170 #endif
171
172
2/2
✓ Branch 0 taken 1266 times.
✓ Branch 1 taken 1264 times.
2530 for (i = 0; i < h->nb_slice_ctx; i++) {
173 1266 H264SliceContext *sl = &h->slice_ctx[i];
174
175 1266 av_freep(&sl->bipred_scratchpad);
176 1266 av_freep(&sl->edge_emu_buffer);
177 1266 av_freep(&sl->top_borders[0]);
178 1266 av_freep(&sl->top_borders[1]);
179
180 1266 sl->bipred_scratchpad_allocated = 0;
181 1266 sl->edge_emu_buffer_allocated = 0;
182 1266 sl->top_borders_allocated[0] = 0;
183 1266 sl->top_borders_allocated[1] = 0;
184 }
185 1264 }
186
187 625 int ff_h264_alloc_tables(H264Context *h)
188 {
189 625 ERContext *const er = &h->er;
190 625 const int big_mb_num = h->mb_stride * (h->mb_height + 1);
191 625 const int row_mb_num = 2*h->mb_stride*FFMAX(h->nb_slice_ctx, 1);
192 625 const int st_size = big_mb_num + h->mb_stride;
193 int x, y;
194
195
1/2
✓ Branch 1 taken 625 times.
✗ Branch 2 not taken.
625 if (!FF_ALLOCZ_TYPED_ARRAY(h->intra4x4_pred_mode, row_mb_num * 8) ||
196
1/2
✓ Branch 1 taken 625 times.
✗ Branch 2 not taken.
625 !FF_ALLOCZ_TYPED_ARRAY(h->non_zero_count, big_mb_num) ||
197
1/2
✓ Branch 1 taken 625 times.
✗ Branch 2 not taken.
625 !FF_ALLOCZ_TYPED_ARRAY(h->slice_table_base, st_size) ||
198
1/2
✓ Branch 1 taken 625 times.
✗ Branch 2 not taken.
625 !FF_ALLOCZ_TYPED_ARRAY(h->cbp_table, big_mb_num) ||
199
1/2
✓ Branch 1 taken 625 times.
✗ Branch 2 not taken.
625 !FF_ALLOCZ_TYPED_ARRAY(h->chroma_pred_mode_table, big_mb_num) ||
200
1/2
✓ Branch 1 taken 625 times.
✗ Branch 2 not taken.
625 !FF_ALLOCZ_TYPED_ARRAY(h->mvd_table[0], row_mb_num * 8) ||
201
1/2
✓ Branch 1 taken 625 times.
✗ Branch 2 not taken.
625 !FF_ALLOCZ_TYPED_ARRAY(h->mvd_table[1], row_mb_num * 8) ||
202
1/2
✓ Branch 1 taken 625 times.
✗ Branch 2 not taken.
625 !FF_ALLOCZ_TYPED_ARRAY(h->direct_table, big_mb_num * 4) ||
203
1/2
✓ Branch 1 taken 625 times.
✗ Branch 2 not taken.
625 !FF_ALLOCZ_TYPED_ARRAY(h->list_counts, big_mb_num) ||
204
1/2
✓ Branch 1 taken 625 times.
✗ Branch 2 not taken.
625 !FF_ALLOCZ_TYPED_ARRAY(h->mb2b_xy, big_mb_num) ||
205
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 625 times.
625 !FF_ALLOCZ_TYPED_ARRAY(h->mb2br_xy, big_mb_num))
206 return AVERROR(ENOMEM);
207 625 h->slice_ctx[0].intra4x4_pred_mode = h->intra4x4_pred_mode;
208 625 h->slice_ctx[0].mvd_table[0] = h->mvd_table[0];
209 625 h->slice_ctx[0].mvd_table[1] = h->mvd_table[1];
210 625 memset(h->slice_table_base, -1,
211 st_size * sizeof(*h->slice_table_base));
212 625 h->slice_table = h->slice_table_base + h->mb_stride * 2 + 1;
213
2/2
✓ Branch 0 taken 15793 times.
✓ Branch 1 taken 625 times.
16418 for (y = 0; y < h->mb_height; y++)
214
2/2
✓ Branch 0 taken 967044 times.
✓ Branch 1 taken 15793 times.
982837 for (x = 0; x < h->mb_width; x++) {
215 967044 const int mb_xy = x + y * h->mb_stride;
216 967044 const int b_xy = 4 * x + 4 * y * h->b_stride;
217
218 967044 h->mb2b_xy[mb_xy] = b_xy;
219 967044 h->mb2br_xy[mb_xy] = 8 * (FMO ? mb_xy : (mb_xy % (2 * h->mb_stride)));
220 }
221
222 if (CONFIG_ERROR_RESILIENCE) {
223 625 int y_size = (2 * h->mb_width + 1) * (2 * h->mb_height + 1);
224 625 int yc_size = y_size + 2 * big_mb_num;
225
226 /* init ER */
227 625 er->avctx = h->avctx;
228 625 er->decode_mb = h264_er_decode_mb;
229 625 er->opaque = h;
230 625 er->quarter_sample = 1;
231
232 625 er->mb_num = h->mb_num;
233 625 er->mb_width = h->mb_width;
234 625 er->mb_height = h->mb_height;
235 625 er->mb_stride = h->mb_stride;
236 625 er->b8_stride = h->mb_width * 2 + 1;
237
238 // error resilience code looks cleaner with this
239
1/2
✓ Branch 1 taken 625 times.
✗ Branch 2 not taken.
625 if (!FF_ALLOCZ_TYPED_ARRAY(er->mb_index2xy, h->mb_num + 1) ||
240
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 625 times.
625 !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 15793 times.
✓ Branch 1 taken 625 times.
16418 for (y = 0; y < h->mb_height; y++)
244
2/2
✓ Branch 0 taken 967044 times.
✓ Branch 1 taken 15793 times.
982837 for (x = 0; x < h->mb_width; x++)
245 967044 er->mb_index2xy[x + y * h->mb_width] = x + y * h->mb_stride;
246
247 625 er->mb_index2xy[h->mb_height * h->mb_width] = (h->mb_height - 1) *
248 625 h->mb_stride + h->mb_width;
249 625 er->dc_val[0] = h->dc_val_base + h->mb_width * 2 + 2;
250 625 er->dc_val[1] = h->dc_val_base + y_size + h->mb_stride + 1;
251 625 er->dc_val[2] = er->dc_val[1] + big_mb_num;
252
2/2
✓ Branch 0 taken 5961103 times.
✓ Branch 1 taken 625 times.
5961728 for (int i = 0; i < yc_size; i++)
253 5961103 h->dc_val_base[i] = 1024;
254
255 625 return ff_er_init(er);
256 }
257
258 return 0;
259 }
260
261 /**
262 * Init slice context
263 */
264 626 void ff_h264_slice_context_init(H264Context *h, H264SliceContext *sl)
265 {
266 626 sl->ref_cache[0][scan8[5] + 1] =
267 626 sl->ref_cache[0][scan8[7] + 1] =
268 626 sl->ref_cache[0][scan8[13] + 1] =
269 626 sl->ref_cache[1][scan8[5] + 1] =
270 626 sl->ref_cache[1][scan8[7] + 1] =
271 626 sl->ref_cache[1][scan8[13] + 1] = PART_NOT_AVAILABLE;
272
273 626 sl->er = &h->er;
274 626 }
275
276 24168 static int h264_init_pic(H264Picture *pic)
277 {
278 24168 pic->f = av_frame_alloc();
279
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 24168 times.
24168 if (!pic->f)
280 return AVERROR(ENOMEM);
281
282 24168 pic->f_grain = av_frame_alloc();
283
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 24168 times.
24168 if (!pic->f_grain)
284 return AVERROR(ENOMEM);
285
286 24168 return 0;
287 }
288
289 636 static int h264_init_context(AVCodecContext *avctx, H264Context *h)
290 {
291 int i, ret;
292
293 636 h->avctx = avctx;
294 636 h->cur_chroma_format_idc = -1;
295
296 636 h->width_from_caller = avctx->width;
297 636 h->height_from_caller = avctx->height;
298
299 636 h->workaround_bugs = avctx->workaround_bugs;
300 636 h->flags = avctx->flags;
301 636 h->poc.prev_poc_msb = 1 << 16;
302 636 h->recovery_frame = -1;
303 636 h->frame_recovered = 0;
304 636 h->poc.prev_frame_num = -1;
305 636 h->sei.common.frame_packing.arrangement_cancel_flag = -1;
306 636 h->sei.common.unregistered.x264_build = -1;
307
308 636 h->next_outputed_poc = INT_MIN;
309
2/2
✓ Branch 0 taken 10176 times.
✓ Branch 1 taken 636 times.
10812 for (i = 0; i < FF_ARRAY_ELEMS(h->last_pocs); i++)
310 10176 h->last_pocs[i] = INT_MIN;
311
312 636 ff_h264_sei_uninit(&h->sei);
313
314
2/2
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 618 times.
636 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 635 times.
636 h->nb_slice_ctx = (avctx->active_thread_type & FF_THREAD_SLICE) ? avctx->thread_count : 1;
321 636 h->slice_ctx = av_calloc(h->nb_slice_ctx, sizeof(*h->slice_ctx));
322
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 636 times.
636 if (!h->slice_ctx) {
323 h->nb_slice_ctx = 0;
324 return AVERROR(ENOMEM);
325 }
326
327
2/2
✓ Branch 0 taken 22896 times.
✓ Branch 1 taken 636 times.
23532 for (i = 0; i < H264_MAX_PICTURE_COUNT; i++) {
328
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 22896 times.
22896 if ((ret = h264_init_pic(&h->DPB[i])) < 0)
329 return ret;
330 }
331
332
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 636 times.
636 if ((ret = h264_init_pic(&h->cur_pic)) < 0)
333 return ret;
334
335
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 636 times.
636 if ((ret = h264_init_pic(&h->last_pic_for_ec)) < 0)
336 return ret;
337
338
2/2
✓ Branch 0 taken 637 times.
✓ Branch 1 taken 636 times.
1273 for (i = 0; i < h->nb_slice_ctx; i++)
339 637 h->slice_ctx[i].h264 = h;
340
341 636 return 0;
342 }
343
344 24168 static void h264_free_pic(H264Context *h, H264Picture *pic)
345 {
346 24168 ff_h264_unref_picture(pic);
347 24168 av_frame_free(&pic->f);
348 24168 av_frame_free(&pic->f_grain);
349 24168 }
350
351 636 static av_cold int h264_decode_end(AVCodecContext *avctx)
352 {
353 636 H264Context *h = avctx->priv_data;
354 int i;
355
356 636 ff_h264_remove_all_refs(h);
357 636 ff_h264_free_tables(h);
358
359
2/2
✓ Branch 0 taken 22896 times.
✓ Branch 1 taken 636 times.
23532 for (i = 0; i < H264_MAX_PICTURE_COUNT; i++) {
360 22896 h264_free_pic(h, &h->DPB[i]);
361 }
362 636 memset(h->delayed_pic, 0, sizeof(h->delayed_pic));
363
364 636 h->cur_pic_ptr = NULL;
365
366 636 av_refstruct_pool_uninit(&h->decode_error_flags_pool);
367
368 636 av_freep(&h->slice_ctx);
369 636 h->nb_slice_ctx = 0;
370
371 636 ff_h264_sei_uninit(&h->sei);
372 636 ff_h264_ps_uninit(&h->ps);
373
374 636 ff_h2645_packet_uninit(&h->pkt);
375
376 636 h264_free_pic(h, &h->cur_pic);
377 636 h264_free_pic(h, &h->last_pic_for_ec);
378
379 636 return 0;
380 }
381
382 static AVOnce h264_vlc_init = AV_ONCE_INIT;
383
384 636 static av_cold int h264_decode_init(AVCodecContext *avctx)
385 {
386 636 H264Context *h = avctx->priv_data;
387 int ret;
388
389 636 ret = h264_init_context(avctx, h);
390
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 636 times.
636 if (ret < 0)
391 return ret;
392
393 636 ret = ff_thread_once(&h264_vlc_init, ff_h264_decode_init_vlc);
394
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 636 times.
636 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 620 times.
✓ Branch 1 taken 16 times.
636 if (!avctx->internal->is_copy) {
400
3/4
✓ Branch 0 taken 386 times.
✓ Branch 1 taken 234 times.
✓ Branch 2 taken 386 times.
✗ Branch 3 not taken.
620 if (avctx->extradata_size > 0 && avctx->extradata) {
401 386 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 386 times.
386 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 636 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
636 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 636 ff_h264_flush_change(h);
422
423
3/4
✓ Branch 0 taken 636 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 635 times.
636 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 635 times.
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 635 times.
636 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 636 return 0;
433 }
434
435 /**
436 * instantaneous decoder refresh.
437 */
438 1818 static void idr(H264Context *h)
439 {
440 int i;
441 1818 ff_h264_remove_all_refs(h);
442 1818 h->poc.prev_frame_num =
443 1818 h->poc.prev_frame_num_offset = 0;
444 1818 h->poc.prev_poc_msb = 1<<16;
445 1818 h->poc.prev_poc_lsb = -1;
446
2/2
✓ Branch 0 taken 29088 times.
✓ Branch 1 taken 1818 times.
30906 for (i = 0; i < FF_ARRAY_ELEMS(h->last_pocs); i++)
447 29088 h->last_pocs[i] = INT_MIN;
448 1818 }
449
450 /* forget old pics after a seek */
451 647 void ff_h264_flush_change(H264Context *h)
452 {
453 int i, j;
454
455 647 h->next_outputed_poc = INT_MIN;
456 647 h->prev_interlaced_frame = 1;
457 647 idr(h);
458
459 647 h->poc.prev_frame_num = -1;
460
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 647 times.
647 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 647 ff_h264_unref_picture(&h->last_pic_for_ec);
468
469 647 h->first_field = 0;
470 647 h->recovery_frame = -1;
471 647 h->frame_recovered = 0;
472 647 h->current_slice = 0;
473 647 h->mmco_reset = 1;
474 647 }
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 29916 static int decode_nal_units(H264Context *h, AVBufferRef *buf_ref,
584 const uint8_t *buf, int buf_size)
585 {
586 29916 AVCodecContext *const avctx = h->avctx;
587 29916 int nals_needed = 0; ///< number of NALs that need decoding before the next frame thread starts
588 29916 int idr_cleared=0;
589 29916 int i, ret = 0;
590
591 29916 h->has_slice = 0;
592 29916 h->nal_unit_type= 0;
593
594
2/2
✓ Branch 0 taken 29160 times.
✓ Branch 1 taken 756 times.
29916 if (!(avctx->flags2 & AV_CODEC_FLAG2_CHUNKS)) {
595 29160 h->current_slice = 0;
596
2/2
✓ Branch 0 taken 25773 times.
✓ Branch 1 taken 3387 times.
29160 if (!h->first_field) {
597 25773 h->cur_pic_ptr = NULL;
598 25773 ff_h264_sei_uninit(&h->sei);
599 }
600 }
601
602
2/2
✓ Branch 0 taken 3385 times.
✓ Branch 1 taken 26531 times.
29916 if (h->nal_length_size == 4) {
603
2/6
✓ Branch 0 taken 3385 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 3385 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
3385 if (buf_size > 8 && AV_RB32(buf) == 1 && AV_RB32(buf+5) > (unsigned)buf_size) {
604 h->is_avc = 0;
605
3/6
✓ Branch 0 taken 3385 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3385 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 3385 times.
✗ Branch 5 not taken.
3385 }else if(buf_size > 3 && AV_RB32(buf) > 1 && AV_RB32(buf) <= (unsigned)buf_size)
606 3385 h->is_avc = 1;
607 }
608
609 29916 ret = ff_h2645_packet_split(&h->pkt, buf, buf_size, avctx, h->nal_length_size,
610 29916 avctx->codec_id, !!h->is_avc * H2645_FLAG_IS_NALFF);
611
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 29915 times.
29916 if (ret < 0) {
612 1 av_log(avctx, AV_LOG_ERROR,
613 "Error splitting the input into NAL units.\n");
614 1 return ret;
615 }
616
617
2/2
✓ Branch 0 taken 42 times.
✓ Branch 1 taken 29873 times.
29915 if (avctx->active_thread_type & FF_THREAD_FRAME)
618 42 nals_needed = get_last_needed_nal(h);
619
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 29915 times.
29915 if (nals_needed < 0)
620 return nals_needed;
621
622
2/2
✓ Branch 0 taken 52731 times.
✓ Branch 1 taken 29915 times.
82646 for (i = 0; i < h->pkt.nb_nals; i++) {
623 52731 H2645NAL *nal = &h->pkt.nals[i];
624 int max_slice_ctx, err;
625
626
2/2
✓ Branch 0 taken 928 times.
✓ Branch 1 taken 51803 times.
52731 if (avctx->skip_frame >= AVDISCARD_NONREF &&
627
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)
628 504 continue;
629
630 // FIXME these should stop being context-global variables
631 52227 h->nal_ref_idc = nal->ref_idc;
632 52227 h->nal_unit_type = nal->type;
633
634 52227 err = 0;
635
6/8
✓ Branch 0 taken 2350 times.
✓ Branch 1 taken 36135 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 3047 times.
✓ Branch 4 taken 1078 times.
✓ Branch 5 taken 6952 times.
✓ Branch 6 taken 2665 times.
✗ Branch 7 not taken.
52227 switch (nal->type) {
636 2350 case H264_NAL_IDR_SLICE:
637
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2350 times.
2350 if ((nal->data[1] & 0xFC) == 0x98) {
638 av_log(h->avctx, AV_LOG_ERROR, "Invalid inter IDR frame\n");
639 h->next_outputed_poc = INT_MIN;
640 ret = -1;
641 goto end;
642 }
643
2/2
✓ Branch 0 taken 1171 times.
✓ Branch 1 taken 1179 times.
2350 if(!idr_cleared) {
644 1171 idr(h); // FIXME ensure we don't lose some frames if there is reordering
645 }
646 2350 idr_cleared = 1;
647 2350 h->has_recovery_point = 1;
648 38485 case H264_NAL_SLICE:
649 38485 h->has_slice = 1;
650
651
2/2
✓ Branch 1 taken 167 times.
✓ Branch 2 taken 38318 times.
38485 if ((err = ff_h264_queue_decode_slice(h, nal))) {
652 167 H264SliceContext *sl = h->slice_ctx + h->nb_slice_ctx_queued;
653 167 sl->ref_count[0] = sl->ref_count[1] = 0;
654 167 break;
655 }
656
657
2/2
✓ Branch 0 taken 29064 times.
✓ Branch 1 taken 9254 times.
38318 if (h->current_slice == 1) {
658
3/4
✓ Branch 0 taken 42 times.
✓ Branch 1 taken 29022 times.
✓ Branch 2 taken 42 times.
✗ Branch 3 not taken.
29064 if (avctx->active_thread_type & FF_THREAD_FRAME &&
659
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) {
660 42 ff_thread_finish_setup(avctx);
661 42 h->setup_finished = 1;
662 }
663
664
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 29064 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
29064 if (h->avctx->hwaccel &&
665 (ret = FF_HW_CALL(h->avctx, start_frame, buf_ref,
666 buf, buf_size)) < 0)
667 goto end;
668 }
669
670
1/2
✓ Branch 0 taken 38318 times.
✗ Branch 1 not taken.
38318 max_slice_ctx = avctx->hwaccel ? 1 : h->nb_slice_ctx;
671
2/2
✓ Branch 0 taken 37488 times.
✓ Branch 1 taken 830 times.
38318 if (h->nb_slice_ctx_queued == max_slice_ctx) {
672
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 37488 times.
37488 if (h->avctx->hwaccel) {
673 ret = FF_HW_CALL(avctx, decode_slice, nal->raw_data, nal->raw_size);
674 h->nb_slice_ctx_queued = 0;
675 } else
676 37488 ret = ff_h264_execute_decode_slices(h);
677
3/4
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 37486 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
37488 if (ret < 0 && (h->avctx->err_recognition & AV_EF_EXPLODE))
678 goto end;
679 }
680 38318 break;
681 case H264_NAL_DPA:
682 case H264_NAL_DPB:
683 case H264_NAL_DPC:
684 avpriv_request_sample(avctx, "data partitioning");
685 break;
686 3047 case H264_NAL_SEI:
687
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3047 times.
3047 if (h->setup_finished) {
688 avpriv_request_sample(avctx, "Late SEI");
689 break;
690 }
691 3047 ret = ff_h264_sei_decode(&h->sei, &nal->gb, &h->ps, avctx);
692
4/4
✓ Branch 0 taken 326 times.
✓ Branch 1 taken 2721 times.
✓ Branch 2 taken 30 times.
✓ Branch 3 taken 296 times.
3047 h->has_recovery_point = h->has_recovery_point || h->sei.recovery_point.recovery_frame_cnt != -1;
693
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3047 times.
3047 if (avctx->debug & FF_DEBUG_GREEN_MD)
694 debug_green_metadata(&h->sei.green_metadata, h->avctx);
695
3/4
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 3040 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 7 times.
3047 if (ret < 0 && (h->avctx->err_recognition & AV_EF_EXPLODE))
696 goto end;
697 3047 break;
698 1078 case H264_NAL_SPS: {
699 1078 GetBitContext tmp_gb = nal->gb;
700
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 1078 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
1078 if (FF_HW_HAS_CB(avctx, decode_params)) {
701 ret = FF_HW_CALL(avctx, decode_params,
702 nal->type, nal->raw_data, nal->raw_size);
703 if (ret < 0)
704 goto end;
705 }
706
2/2
✓ Branch 1 taken 1076 times.
✓ Branch 2 taken 2 times.
1078 if (ff_h264_decode_seq_parameter_set(&tmp_gb, avctx, &h->ps, 0) >= 0)
707 1078 break;
708 2 av_log(h->avctx, AV_LOG_DEBUG,
709 "SPS decoding failure, trying again with the complete NAL\n");
710 2 init_get_bits8(&tmp_gb, nal->raw_data + 1, nal->raw_size - 1);
711
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)
712 2 break;
713 ff_h264_decode_seq_parameter_set(&nal->gb, avctx, &h->ps, 1);
714 break;
715 }
716 6952 case H264_NAL_PPS:
717
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 6952 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
6952 if (FF_HW_HAS_CB(avctx, decode_params)) {
718 ret = FF_HW_CALL(avctx, decode_params,
719 nal->type, nal->raw_data, nal->raw_size);
720 if (ret < 0)
721 goto end;
722 }
723 6952 ret = ff_h264_decode_picture_parameter_set(&nal->gb, avctx, &h->ps,
724 nal->size_bits);
725
3/4
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 6945 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 7 times.
6952 if (ret < 0 && (h->avctx->err_recognition & AV_EF_EXPLODE))
726 goto end;
727 6952 break;
728 2665 case H264_NAL_AUD:
729 case H264_NAL_END_SEQUENCE:
730 case H264_NAL_END_STREAM:
731 case H264_NAL_FILLER_DATA:
732 case H264_NAL_SPS_EXT:
733 case H264_NAL_AUXILIARY_SLICE:
734 2665 break;
735 default:
736 av_log(avctx, AV_LOG_DEBUG, "Unknown NAL code: %d (%d bits)\n",
737 nal->type, nal->size_bits);
738 }
739
740
3/4
✓ Branch 0 taken 167 times.
✓ Branch 1 taken 52060 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 167 times.
52227 if (err < 0 && (h->avctx->err_recognition & AV_EF_EXPLODE)) {
741 av_log(h->avctx, AV_LOG_ERROR, "decode_slice_header error\n");
742 ret = err;
743 goto end;
744 }
745 }
746
747 29915 ret = ff_h264_execute_decode_slices(h);
748
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 29915 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
29915 if (ret < 0 && (h->avctx->err_recognition & AV_EF_EXPLODE))
749 goto end;
750
751 // set decode_error_flags to allow users to detect concealed decoding errors
752
4/6
✓ Branch 0 taken 29915 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 29913 times.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
29915 if ((ret < 0 || h->er.error_occurred) && h->cur_pic_ptr) {
753
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (h->cur_pic_ptr->decode_error_flags) {
754 /* Frame-threading in use */
755 atomic_int *decode_error = h->cur_pic_ptr->decode_error_flags;
756 /* Using atomics here is not supposed to provide synchronisation;
757 * they are merely used to allow to set decode_error from both
758 * decoding threads in case of coded slices. */
759 atomic_fetch_or_explicit(decode_error, FF_DECODE_ERROR_DECODE_SLICES,
760 memory_order_relaxed);
761 } else
762 2 h->cur_pic_ptr->f->decode_error_flags |= FF_DECODE_ERROR_DECODE_SLICES;
763 }
764
765 29915 ret = 0;
766 29915 end:
767
768 #if CONFIG_ERROR_RESILIENCE
769 /*
770 * FIXME: Error handling code does not seem to support interlaced
771 * when slices span multiple rows
772 * The ff_er_add_slice calls don't work right for bottom
773 * fields; they cause massive erroneous error concealing
774 * Error marking covers both fields (top and bottom).
775 * This causes a mismatched s->error_count
776 * and a bad error table. Further, the error count goes to
777 * INT_MAX when called for bottom field, because mb_y is
778 * past end by one (callers fault) and resync_mb_y != 0
779 * causes problems for the first MB line, too.
780 */
781
6/6
✓ Branch 0 taken 22711 times.
✓ Branch 1 taken 7204 times.
✓ Branch 2 taken 22672 times.
✓ Branch 3 taken 39 times.
✓ Branch 4 taken 21922 times.
✓ Branch 5 taken 750 times.
29915 if (!FIELD_PICTURE(h) && h->current_slice && h->enable_er) {
782
783 21922 H264SliceContext *sl = h->slice_ctx;
784
3/4
✓ Branch 0 taken 576 times.
✓ Branch 1 taken 21346 times.
✓ Branch 2 taken 576 times.
✗ Branch 3 not taken.
21922 int use_last_pic = h->last_pic_for_ec.f->buf[0] && !sl->ref_count[0];
785 21922 int decode_error_flags = 0;
786
787 21922 ff_h264_set_erpic(&h->er.cur_pic, h->cur_pic_ptr);
788
789
2/2
✓ Branch 0 taken 576 times.
✓ Branch 1 taken 21346 times.
21922 if (use_last_pic) {
790 576 ff_h264_set_erpic(&h->er.last_pic, &h->last_pic_for_ec);
791 576 sl->ref_list[0][0].parent = &h->last_pic_for_ec;
792 576 memcpy(sl->ref_list[0][0].data, h->last_pic_for_ec.f->data, sizeof(sl->ref_list[0][0].data));
793 576 memcpy(sl->ref_list[0][0].linesize, h->last_pic_for_ec.f->linesize, sizeof(sl->ref_list[0][0].linesize));
794 576 sl->ref_list[0][0].reference = h->last_pic_for_ec.reference;
795
2/2
✓ Branch 0 taken 19351 times.
✓ Branch 1 taken 1995 times.
21346 } else if (sl->ref_count[0]) {
796 19351 ff_h264_set_erpic(&h->er.last_pic, sl->ref_list[0][0].parent);
797 } else
798 1995 ff_h264_set_erpic(&h->er.last_pic, NULL);
799
800
2/2
✓ Branch 0 taken 19351 times.
✓ Branch 1 taken 2571 times.
21922 if (sl->ref_count[1])
801 19351 ff_h264_set_erpic(&h->er.next_pic, sl->ref_list[1][0].parent);
802
803 21922 ff_er_frame_end(&h->er, &decode_error_flags);
804
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 21921 times.
21922 if (decode_error_flags) {
805
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (h->cur_pic_ptr->decode_error_flags) {
806 atomic_int *decode_error = h->cur_pic_ptr->decode_error_flags;
807 atomic_fetch_or_explicit(decode_error, decode_error_flags,
808 memory_order_relaxed);
809 } else
810 1 h->cur_pic_ptr->f->decode_error_flags |= decode_error_flags;
811 }
812
2/2
✓ Branch 0 taken 576 times.
✓ Branch 1 taken 21346 times.
21922 if (use_last_pic)
813 576 memset(&sl->ref_list[0][0], 0, sizeof(sl->ref_list[0][0]));
814 }
815 #endif /* CONFIG_ERROR_RESILIENCE */
816 /* clean up */
817
6/6
✓ Branch 0 taken 29487 times.
✓ Branch 1 taken 428 times.
✓ Branch 2 taken 19597 times.
✓ Branch 3 taken 9890 times.
✓ Branch 4 taken 19431 times.
✓ Branch 5 taken 166 times.
29915 if (h->cur_pic_ptr && !h->droppable && h->has_slice) {
818 19431 ff_thread_report_progress(&h->cur_pic_ptr->tf, INT_MAX,
819 19431 h->picture_structure == PICT_BOTTOM_FIELD);
820 }
821
822
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 29915 times.
29915 return (ret < 0) ? ret : buf_size;
823 }
824
825 1 static int h264_export_enc_params(AVFrame *f, const H264Picture *p)
826 {
827 AVVideoEncParams *par;
828 1 unsigned int nb_mb = p->mb_height * p->mb_width;
829 unsigned int x, y;
830
831 1 par = av_video_enc_params_create_side_data(f, AV_VIDEO_ENC_PARAMS_H264, nb_mb);
832
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!par)
833 return AVERROR(ENOMEM);
834
835 1 par->qp = p->pps->init_qp;
836
837 1 par->delta_qp[1][0] = p->pps->chroma_qp_index_offset[0];
838 1 par->delta_qp[1][1] = p->pps->chroma_qp_index_offset[0];
839 1 par->delta_qp[2][0] = p->pps->chroma_qp_index_offset[1];
840 1 par->delta_qp[2][1] = p->pps->chroma_qp_index_offset[1];
841
842
2/2
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 1 times.
19 for (y = 0; y < p->mb_height; y++)
843
2/2
✓ Branch 0 taken 396 times.
✓ Branch 1 taken 18 times.
414 for (x = 0; x < p->mb_width; x++) {
844 396 const unsigned int block_idx = y * p->mb_width + x;
845 396 const unsigned int mb_xy = y * p->mb_stride + x;
846 396 AVVideoBlockParams *b = av_video_enc_params_block(par, block_idx);
847
848 396 b->src_x = x * 16;
849 396 b->src_y = y * 16;
850 396 b->w = 16;
851 396 b->h = 16;
852
853 396 b->delta_qp = p->qscale_table[mb_xy] - par->qp;
854 }
855
856 1 return 0;
857 }
858
859 24959 static int output_frame(H264Context *h, AVFrame *dst, H264Picture *srcp)
860 {
861 int ret;
862
863
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 24959 times.
24959 ret = av_frame_ref(dst, srcp->needs_fg ? srcp->f_grain : srcp->f);
864
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 24959 times.
24959 if (ret < 0)
865 return ret;
866
867
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 24959 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
24959 if (srcp->needs_fg && (ret = av_frame_copy_props(dst, srcp->f)) < 0)
868 return ret;
869
870
2/2
✓ Branch 0 taken 42 times.
✓ Branch 1 taken 24917 times.
24959 if (srcp->decode_error_flags) {
871 42 atomic_int *decode_error = srcp->decode_error_flags;
872 /* The following is not supposed to provide synchronisation at all:
873 * given that srcp has already finished decoding, decode_error
874 * has already been set to its final value. */
875 42 dst->decode_error_flags |= atomic_load_explicit(decode_error, memory_order_relaxed);
876 }
877
878 24959 av_dict_set(&dst->metadata, "stereo_mode", ff_h264_sei_stereo_mode(&h->sei.common.frame_packing), 0);
879
880
2/2
✓ Branch 0 taken 34 times.
✓ Branch 1 taken 24925 times.
24959 if (srcp->sei_recovery_frame_cnt == 0)
881 34 dst->flags |= AV_FRAME_FLAG_KEY;
882
883
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 24958 times.
24959 if (h->avctx->export_side_data & AV_CODEC_EXPORT_DATA_VIDEO_ENC_PARAMS) {
884 1 ret = h264_export_enc_params(dst, srcp);
885
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (ret < 0)
886 goto fail;
887 }
888
889
1/2
✓ Branch 0 taken 24959 times.
✗ Branch 1 not taken.
24959 if (!(h->avctx->export_side_data & AV_CODEC_EXPORT_DATA_FILM_GRAIN))
890 24959 av_frame_remove_side_data(dst, AV_FRAME_DATA_FILM_GRAIN_PARAMS);
891
892 24959 return 0;
893 fail:
894 av_frame_unref(dst);
895 return ret;
896 }
897
898 static int is_avcc_extradata(const uint8_t *buf, int buf_size)
899 {
900 int cnt= buf[5]&0x1f;
901 const uint8_t *p= buf+6;
902 if (!cnt)
903 return 0;
904 while(cnt--){
905 int nalsize= AV_RB16(p) + 2;
906 if(nalsize > buf_size - (p-buf) || (p[2] & 0x9F) != 7)
907 return 0;
908 p += nalsize;
909 }
910 cnt = *(p++);
911 if(!cnt)
912 return 0;
913 while(cnt--){
914 int nalsize= AV_RB16(p) + 2;
915 if(nalsize > buf_size - (p-buf) || (p[2] & 0x9F) != 8)
916 return 0;
917 p += nalsize;
918 }
919 return 1;
920 }
921
922 24959 static int finalize_frame(H264Context *h, AVFrame *dst, H264Picture *out, int *got_frame)
923 {
924 int ret;
925
926
1/2
✓ Branch 0 taken 24959 times.
✗ Branch 1 not taken.
24959 if (((h->avctx->flags & AV_CODEC_FLAG_OUTPUT_CORRUPT) ||
927
1/2
✓ Branch 0 taken 24959 times.
✗ Branch 1 not taken.
24959 (h->avctx->flags2 & AV_CODEC_FLAG2_SHOW_ALL) ||
928
1/2
✓ Branch 0 taken 24959 times.
✗ Branch 1 not taken.
24959 out->recovered)) {
929
930
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 24959 times.
24959 if (h->skip_gray > 0 &&
931 h->non_gray && out->gray &&
932 !(h->avctx->flags2 & AV_CODEC_FLAG2_SHOW_ALL)
933 )
934 return 0;
935
936
1/2
✓ Branch 0 taken 24959 times.
✗ Branch 1 not taken.
24959 if (!h->avctx->hwaccel &&
937
1/2
✓ Branch 0 taken 24959 times.
✗ Branch 1 not taken.
24959 (out->field_poc[0] == INT_MAX ||
938
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 24959 times.
24959 out->field_poc[1] == INT_MAX)
939 ) {
940 int p;
941 AVFrame *f = out->f;
942 int field = out->field_poc[0] == INT_MAX;
943 uint8_t *dst_data[4];
944 int linesizes[4];
945 const uint8_t *src_data[4];
946
947 av_log(h->avctx, AV_LOG_DEBUG, "Duplicating field %d to fill missing\n", field);
948
949 for (p = 0; p<4; p++) {
950 dst_data[p] = f->data[p] + (field^1)*f->linesize[p];
951 src_data[p] = f->data[p] + field *f->linesize[p];
952 linesizes[p] = 2*f->linesize[p];
953 }
954
955 av_image_copy(dst_data, linesizes, src_data, linesizes,
956 f->format, f->width, f->height>>1);
957 }
958
959 24959 ret = output_frame(h, dst, out);
960
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 24959 times.
24959 if (ret < 0)
961 return ret;
962
963 24959 *got_frame = 1;
964
965 if (CONFIG_MPEGVIDEODEC) {
966 24959 ff_print_debug_info2(h->avctx, dst,
967 24959 out->mb_type,
968 24959 out->qscale_table,
969 24959 out->motion_val,
970 out->mb_width, out->mb_height, out->mb_stride, 1);
971 }
972 }
973
974 24959 return 0;
975 }
976
977 580 static int send_next_delayed_frame(H264Context *h, AVFrame *dst_frame,
978 int *got_frame, int buf_index)
979 {
980 int ret, i, out_idx;
981 H264Picture *out;
982
983 580 h->cur_pic_ptr = NULL;
984 580 h->first_field = 0;
985
986
2/2
✓ Branch 0 taken 294 times.
✓ Branch 1 taken 286 times.
1160 while (h->delayed_pic[0]) {
987 294 out = h->delayed_pic[0];
988 294 out_idx = 0;
989 294 for (i = 1;
990 1482 h->delayed_pic[i] &&
991
4/4
✓ Branch 0 taken 598 times.
✓ Branch 1 taken 286 times.
✓ Branch 2 taken 590 times.
✓ Branch 3 taken 8 times.
884 !(h->delayed_pic[i]->f->flags & AV_FRAME_FLAG_KEY) &&
992
1/2
✓ Branch 0 taken 590 times.
✗ Branch 1 not taken.
590 !h->delayed_pic[i]->mmco_reset;
993 590 i++)
994
2/2
✓ Branch 0 taken 116 times.
✓ Branch 1 taken 474 times.
590 if (h->delayed_pic[i]->poc < out->poc) {
995 116 out = h->delayed_pic[i];
996 116 out_idx = i;
997 }
998
999
2/2
✓ Branch 0 taken 844 times.
✓ Branch 1 taken 294 times.
1138 for (i = out_idx; h->delayed_pic[i]; i++)
1000 844 h->delayed_pic[i] = h->delayed_pic[i + 1];
1001
1002
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 294 times.
294 if (out) {
1003 294 h->frame_recovered |= out->recovered;
1004 294 out->recovered |= h->frame_recovered & FRAME_RECOVERED_SEI;
1005
1006 294 out->reference &= ~DELAYED_PIC_REF;
1007 294 ret = finalize_frame(h, dst_frame, out, got_frame);
1008
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 294 times.
294 if (ret < 0)
1009 return ret;
1010
1/2
✓ Branch 0 taken 294 times.
✗ Branch 1 not taken.
294 if (*got_frame)
1011 294 break;
1012 }
1013 }
1014
1015 580 return buf_index;
1016 }
1017
1018 30496 static int h264_decode_frame(AVCodecContext *avctx, AVFrame *pict,
1019 int *got_frame, AVPacket *avpkt)
1020 {
1021 30496 const uint8_t *buf = avpkt->data;
1022 30496 int buf_size = avpkt->size;
1023 30496 H264Context *h = avctx->priv_data;
1024 int buf_index;
1025 int ret;
1026
1027 30496 h->flags = avctx->flags;
1028 30496 h->setup_finished = 0;
1029 30496 h->nb_slice_ctx_queued = 0;
1030
1031 30496 ff_h264_unref_picture(&h->last_pic_for_ec);
1032
1033 /* end of stream, output what is still in the buffers */
1034
2/2
✓ Branch 0 taken 580 times.
✓ Branch 1 taken 29916 times.
30496 if (buf_size == 0)
1035 580 return send_next_delayed_frame(h, pict, got_frame, 0);
1036
1037
2/2
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 29912 times.
29916 if (av_packet_get_side_data(avpkt, AV_PKT_DATA_NEW_EXTRADATA, NULL)) {
1038 size_t side_size;
1039 4 uint8_t *side = av_packet_get_side_data(avpkt, AV_PKT_DATA_NEW_EXTRADATA, &side_size);
1040 4 ff_h264_decode_extradata(side, side_size,
1041 &h->ps, &h->is_avc, &h->nal_length_size,
1042 avctx->err_recognition, avctx);
1043 }
1044
4/10
✓ Branch 0 taken 3385 times.
✓ Branch 1 taken 26531 times.
✓ Branch 2 taken 3385 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 3385 times.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
29916 if (h->is_avc && buf_size >= 9 && buf[0]==1 && buf[2]==0 && (buf[4]&0xFC)==0xFC) {
1045 if (is_avcc_extradata(buf, buf_size))
1046 return ff_h264_decode_extradata(buf, buf_size,
1047 &h->ps, &h->is_avc, &h->nal_length_size,
1048 avctx->err_recognition, avctx);
1049 }
1050
1051 29916 buf_index = decode_nal_units(h, avpkt->buf, buf, buf_size);
1052
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 29915 times.
29916 if (buf_index < 0)
1053 1 return AVERROR_INVALIDDATA;
1054
1055
3/4
✓ Branch 0 taken 428 times.
✓ Branch 1 taken 29487 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 428 times.
29915 if (!h->cur_pic_ptr && h->nal_unit_type == H264_NAL_END_SEQUENCE) {
1056 av_assert0(buf_index <= buf_size);
1057 return send_next_delayed_frame(h, pict, got_frame, buf_index);
1058 }
1059
1060
6/6
✓ Branch 0 taken 29159 times.
✓ Branch 1 taken 756 times.
✓ Branch 2 taken 28733 times.
✓ Branch 3 taken 426 times.
✓ Branch 4 taken 164 times.
✓ Branch 5 taken 28569 times.
29915 if (!(avctx->flags2 & AV_CODEC_FLAG2_CHUNKS) && (!h->cur_pic_ptr || !h->has_slice)) {
1061
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 ||
1062
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 390 times.
390 buf_size >= 4 && !memcmp("Q264", buf, 4))
1063 200 return buf_size;
1064 390 av_log(avctx, AV_LOG_ERROR, "no frame!\n");
1065 390 return AVERROR_INVALIDDATA;
1066 }
1067
1068
2/2
✓ Branch 0 taken 756 times.
✓ Branch 1 taken 28569 times.
29325 if (!(avctx->flags2 & AV_CODEC_FLAG2_CHUNKS) ||
1069
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)) {
1070
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 28869 times.
28869 if ((ret = ff_h264_field_end(h, &h->slice_ctx[0], 0)) < 0)
1071 return ret;
1072
1073 /* Wait for second field. */
1074
2/2
✓ Branch 0 taken 24665 times.
✓ Branch 1 taken 4204 times.
28869 if (h->next_output_pic) {
1075 24665 ret = finalize_frame(h, pict, h->next_output_pic, got_frame);
1076
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 24665 times.
24665 if (ret < 0)
1077 return ret;
1078 }
1079 }
1080
1081
3/4
✓ Branch 0 taken 4660 times.
✓ Branch 1 taken 24665 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 4660 times.
29325 av_assert0(pict->buf[0] || !*got_frame);
1082
1083 29325 ff_h264_unref_picture(&h->last_pic_for_ec);
1084
1085 29325 return buf_size;
1086 }
1087
1088 #define OFFSET(x) offsetof(H264Context, x)
1089 #define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
1090 #define VDX VD | AV_OPT_FLAG_EXPORT
1091 static const AVOption h264_options[] = {
1092 { "is_avc", "is avc", OFFSET(is_avc), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, VDX },
1093 { "nal_length_size", "nal_length_size", OFFSET(nal_length_size), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 4, VDX },
1094 { "enable_er", "Enable error resilience on damaged frames (unsafe)", OFFSET(enable_er), AV_OPT_TYPE_BOOL, { .i64 = -1 }, -1, 1, VD },
1095 { "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 },
1096 { "skip_gray", "Do not return gray gap frames", OFFSET(skip_gray), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, VD },
1097 { "noref_gray", "Avoid using gray gap frames as references", OFFSET(noref_gray), AV_OPT_TYPE_BOOL, {.i64 = 1}, 0, 1, VD },
1098 { NULL },
1099 };
1100
1101 static const AVClass h264_class = {
1102 .class_name = "H264 Decoder",
1103 .item_name = av_default_item_name,
1104 .option = h264_options,
1105 .version = LIBAVUTIL_VERSION_INT,
1106 };
1107
1108 const FFCodec ff_h264_decoder = {
1109 .p.name = "h264",
1110 CODEC_LONG_NAME("H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10"),
1111 .p.type = AVMEDIA_TYPE_VIDEO,
1112 .p.id = AV_CODEC_ID_H264,
1113 .priv_data_size = sizeof(H264Context),
1114 .init = h264_decode_init,
1115 .close = h264_decode_end,
1116 FF_CODEC_DECODE_CB(h264_decode_frame),
1117 .p.capabilities = AV_CODEC_CAP_DR1 |
1118 AV_CODEC_CAP_DELAY | AV_CODEC_CAP_SLICE_THREADS |
1119 AV_CODEC_CAP_FRAME_THREADS,
1120 .hw_configs = (const AVCodecHWConfigInternal *const []) {
1121 #if CONFIG_H264_DXVA2_HWACCEL
1122 HWACCEL_DXVA2(h264),
1123 #endif
1124 #if CONFIG_H264_D3D11VA_HWACCEL
1125 HWACCEL_D3D11VA(h264),
1126 #endif
1127 #if CONFIG_H264_D3D11VA2_HWACCEL
1128 HWACCEL_D3D11VA2(h264),
1129 #endif
1130 #if CONFIG_H264_D3D12VA_HWACCEL
1131 HWACCEL_D3D12VA(h264),
1132 #endif
1133 #if CONFIG_H264_NVDEC_HWACCEL
1134 HWACCEL_NVDEC(h264),
1135 #endif
1136 #if CONFIG_H264_VAAPI_HWACCEL
1137 HWACCEL_VAAPI(h264),
1138 #endif
1139 #if CONFIG_H264_VDPAU_HWACCEL
1140 HWACCEL_VDPAU(h264),
1141 #endif
1142 #if CONFIG_H264_VIDEOTOOLBOX_HWACCEL
1143 HWACCEL_VIDEOTOOLBOX(h264),
1144 #endif
1145 #if CONFIG_H264_VULKAN_HWACCEL
1146 HWACCEL_VULKAN(h264),
1147 #endif
1148 NULL
1149 },
1150 .caps_internal = FF_CODEC_CAP_EXPORTS_CROPPING |
1151 FF_CODEC_CAP_INIT_CLEANUP,
1152 .flush = h264_decode_flush,
1153 UPDATE_THREAD_CONTEXT(ff_h264_update_thread_context),
1154 UPDATE_THREAD_CONTEXT_FOR_USER(ff_h264_update_thread_context_for_user),
1155 .p.profiles = NULL_IF_CONFIG_SMALL(ff_h264_profiles),
1156 .p.priv_class = &h264_class,
1157 };
1158