FFmpeg coverage


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