FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/h264dec.c
Date: 2024-11-20 23:03:26
Exec Total Coverage
Lines: 470 646 72.8%
Functions: 21 24 87.5%
Branches: 259 449 57.7%

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