FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/h264dec.c
Date: 2025-02-27 15:37:21
Exec Total Coverage
Lines: 469 648 72.4%
Functions: 21 24 87.5%
Branches: 260 451 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/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 "libavutil/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 6687 int avpriv_h264_has_num_reorder_frames(AVCodecContext *avctx)
62 {
63 6687 H264Context *h = avctx->priv_data;
64
4/4
✓ Branch 0 taken 6563 times.
✓ Branch 1 taken 124 times.
✓ Branch 2 taken 6437 times.
✓ Branch 3 taken 126 times.
6687 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 377356 void ff_h264_draw_horiz_band(const H264Context *h, H264SliceContext *sl,
104 int y, int height)
105 {
106 377356 AVCodecContext *avctx = h->avctx;
107 377356 const AVFrame *src = h->cur_pic.f;
108 const AVPixFmtDescriptor *desc;
109 int offset[AV_NUM_DATA_POINTERS];
110 int vshift;
111 377356 const int field_pic = h->picture_structure != PICT_FRAME;
112
113
1/2
✓ Branch 0 taken 377356 times.
✗ Branch 1 not taken.
377356 if (!avctx->draw_horiz_band)
114 377356 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 1238 void ff_h264_free_tables(H264Context *h)
142 {
143 int i;
144
145 1238 av_freep(&h->intra4x4_pred_mode);
146 1238 av_freep(&h->chroma_pred_mode_table);
147 1238 av_freep(&h->cbp_table);
148 1238 av_freep(&h->mvd_table[0]);
149 1238 av_freep(&h->mvd_table[1]);
150 1238 av_freep(&h->direct_table);
151 1238 av_freep(&h->non_zero_count);
152 1238 av_freep(&h->slice_table_base);
153 1238 h->slice_table = NULL;
154 1238 av_freep(&h->list_counts);
155
156 1238 av_freep(&h->mb2b_xy);
157 1238 av_freep(&h->mb2br_xy);
158
159 1238 av_refstruct_pool_uninit(&h->qscale_table_pool);
160 1238 av_refstruct_pool_uninit(&h->mb_type_pool);
161 1238 av_refstruct_pool_uninit(&h->motion_val_pool);
162 1238 av_refstruct_pool_uninit(&h->ref_index_pool);
163
164 #if CONFIG_ERROR_RESILIENCE
165 1238 av_freep(&h->er.mb_index2xy);
166 1238 av_freep(&h->er.error_status_table);
167 1238 av_freep(&h->er.er_temp_buffer);
168 1238 av_freep(&h->dc_val_base);
169 #endif
170
171
2/2
✓ Branch 0 taken 1240 times.
✓ Branch 1 taken 1238 times.
2478 for (i = 0; i < h->nb_slice_ctx; i++) {
172 1240 H264SliceContext *sl = &h->slice_ctx[i];
173
174 1240 av_freep(&sl->bipred_scratchpad);
175 1240 av_freep(&sl->edge_emu_buffer);
176 1240 av_freep(&sl->top_borders[0]);
177 1240 av_freep(&sl->top_borders[1]);
178
179 1240 sl->bipred_scratchpad_allocated = 0;
180 1240 sl->edge_emu_buffer_allocated = 0;
181 1240 sl->top_borders_allocated[0] = 0;
182 1240 sl->top_borders_allocated[1] = 0;
183 }
184 1238 }
185
186 611 int ff_h264_alloc_tables(H264Context *h)
187 {
188 611 ERContext *const er = &h->er;
189 611 const int big_mb_num = h->mb_stride * (h->mb_height + 1);
190 611 const int row_mb_num = 2*h->mb_stride*FFMAX(h->nb_slice_ctx, 1);
191 611 const int st_size = big_mb_num + h->mb_stride;
192 int x, y;
193
194
1/2
✓ Branch 1 taken 611 times.
✗ Branch 2 not taken.
611 if (!FF_ALLOCZ_TYPED_ARRAY(h->intra4x4_pred_mode, row_mb_num * 8) ||
195
1/2
✓ Branch 1 taken 611 times.
✗ Branch 2 not taken.
611 !FF_ALLOCZ_TYPED_ARRAY(h->non_zero_count, big_mb_num) ||
196
1/2
✓ Branch 1 taken 611 times.
✗ Branch 2 not taken.
611 !FF_ALLOCZ_TYPED_ARRAY(h->slice_table_base, st_size) ||
197
1/2
✓ Branch 1 taken 611 times.
✗ Branch 2 not taken.
611 !FF_ALLOCZ_TYPED_ARRAY(h->cbp_table, big_mb_num) ||
198
1/2
✓ Branch 1 taken 611 times.
✗ Branch 2 not taken.
611 !FF_ALLOCZ_TYPED_ARRAY(h->chroma_pred_mode_table, big_mb_num) ||
199
1/2
✓ Branch 1 taken 611 times.
✗ Branch 2 not taken.
611 !FF_ALLOCZ_TYPED_ARRAY(h->mvd_table[0], row_mb_num * 8) ||
200
1/2
✓ Branch 1 taken 611 times.
✗ Branch 2 not taken.
611 !FF_ALLOCZ_TYPED_ARRAY(h->mvd_table[1], row_mb_num * 8) ||
201
1/2
✓ Branch 1 taken 611 times.
✗ Branch 2 not taken.
611 !FF_ALLOCZ_TYPED_ARRAY(h->direct_table, big_mb_num * 4) ||
202
1/2
✓ Branch 1 taken 611 times.
✗ Branch 2 not taken.
611 !FF_ALLOCZ_TYPED_ARRAY(h->list_counts, big_mb_num) ||
203
1/2
✓ Branch 1 taken 611 times.
✗ Branch 2 not taken.
611 !FF_ALLOCZ_TYPED_ARRAY(h->mb2b_xy, big_mb_num) ||
204
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 611 times.
611 !FF_ALLOCZ_TYPED_ARRAY(h->mb2br_xy, big_mb_num))
205 return AVERROR(ENOMEM);
206 611 h->slice_ctx[0].intra4x4_pred_mode = h->intra4x4_pred_mode;
207 611 h->slice_ctx[0].mvd_table[0] = h->mvd_table[0];
208 611 h->slice_ctx[0].mvd_table[1] = h->mvd_table[1];
209 611 memset(h->slice_table_base, -1,
210 st_size * sizeof(*h->slice_table_base));
211 611 h->slice_table = h->slice_table_base + h->mb_stride * 2 + 1;
212
2/2
✓ Branch 0 taken 15555 times.
✓ Branch 1 taken 611 times.
16166 for (y = 0; y < h->mb_height; y++)
213
2/2
✓ Branch 0 taken 958276 times.
✓ Branch 1 taken 15555 times.
973831 for (x = 0; x < h->mb_width; x++) {
214 958276 const int mb_xy = x + y * h->mb_stride;
215 958276 const int b_xy = 4 * x + 4 * y * h->b_stride;
216
217 958276 h->mb2b_xy[mb_xy] = b_xy;
218 958276 h->mb2br_xy[mb_xy] = 8 * (FMO ? mb_xy : (mb_xy % (2 * h->mb_stride)));
219 }
220
221 if (CONFIG_ERROR_RESILIENCE) {
222 611 const int er_size = h->mb_height * h->mb_stride * (4*sizeof(int) + 1);
223 611 int mb_array_size = h->mb_height * h->mb_stride;
224 611 int y_size = (2 * h->mb_width + 1) * (2 * h->mb_height + 1);
225 611 int yc_size = y_size + 2 * big_mb_num;
226
227 /* init ER */
228 611 er->avctx = h->avctx;
229 611 er->decode_mb = h264_er_decode_mb;
230 611 er->opaque = h;
231 611 er->quarter_sample = 1;
232
233 611 er->mb_num = h->mb_num;
234 611 er->mb_width = h->mb_width;
235 611 er->mb_height = h->mb_height;
236 611 er->mb_stride = h->mb_stride;
237 611 er->b8_stride = h->mb_width * 2 + 1;
238
239 // error resilience code looks cleaner with this
240
1/2
✓ Branch 1 taken 611 times.
✗ Branch 2 not taken.
611 if (!FF_ALLOCZ_TYPED_ARRAY(er->mb_index2xy, h->mb_num + 1) ||
241
1/2
✓ Branch 1 taken 611 times.
✗ Branch 2 not taken.
611 !FF_ALLOCZ_TYPED_ARRAY(er->error_status_table, mb_array_size) ||
242
1/2
✓ Branch 1 taken 611 times.
✗ Branch 2 not taken.
611 !FF_ALLOCZ_TYPED_ARRAY(er->er_temp_buffer, er_size) ||
243
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 611 times.
611 !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 15555 times.
✓ Branch 1 taken 611 times.
16166 for (y = 0; y < h->mb_height; y++)
247
2/2
✓ Branch 0 taken 958276 times.
✓ Branch 1 taken 15555 times.
973831 for (x = 0; x < h->mb_width; x++)
248 958276 er->mb_index2xy[x + y * h->mb_width] = x + y * h->mb_stride;
249
250 611 er->mb_index2xy[h->mb_height * h->mb_width] = (h->mb_height - 1) *
251 611 h->mb_stride + h->mb_width;
252 611 er->dc_val[0] = h->dc_val_base + h->mb_width * 2 + 2;
253 611 er->dc_val[1] = h->dc_val_base + y_size + h->mb_stride + 1;
254 611 er->dc_val[2] = er->dc_val[1] + big_mb_num;
255
2/2
✓ Branch 0 taken 5905965 times.
✓ Branch 1 taken 611 times.
5906576 for (int i = 0; i < yc_size; i++)
256 5905965 h->dc_val_base[i] = 1024;
257 }
258
259 611 return 0;
260 }
261
262 /**
263 * Init slice context
264 */
265 612 void ff_h264_slice_context_init(H264Context *h, H264SliceContext *sl)
266 {
267 612 sl->ref_cache[0][scan8[5] + 1] =
268 612 sl->ref_cache[0][scan8[7] + 1] =
269 612 sl->ref_cache[0][scan8[13] + 1] =
270 612 sl->ref_cache[1][scan8[5] + 1] =
271 612 sl->ref_cache[1][scan8[7] + 1] =
272 612 sl->ref_cache[1][scan8[13] + 1] = PART_NOT_AVAILABLE;
273
274 612 sl->er = &h->er;
275 612 }
276
277 23598 static int h264_init_pic(H264Picture *pic)
278 {
279 23598 pic->f = av_frame_alloc();
280
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 23598 times.
23598 if (!pic->f)
281 return AVERROR(ENOMEM);
282
283 23598 pic->f_grain = av_frame_alloc();
284
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 23598 times.
23598 if (!pic->f_grain)
285 return AVERROR(ENOMEM);
286
287 23598 return 0;
288 }
289
290 621 static int h264_init_context(AVCodecContext *avctx, H264Context *h)
291 {
292 int i, ret;
293
294 621 h->avctx = avctx;
295 621 h->cur_chroma_format_idc = -1;
296
297 621 h->width_from_caller = avctx->width;
298 621 h->height_from_caller = avctx->height;
299
300 621 h->workaround_bugs = avctx->workaround_bugs;
301 621 h->flags = avctx->flags;
302 621 h->poc.prev_poc_msb = 1 << 16;
303 621 h->recovery_frame = -1;
304 621 h->frame_recovered = 0;
305 621 h->poc.prev_frame_num = -1;
306 621 h->sei.common.frame_packing.arrangement_cancel_flag = -1;
307 621 h->sei.common.unregistered.x264_build = -1;
308
309 621 h->next_outputed_poc = INT_MIN;
310
2/2
✓ Branch 0 taken 9936 times.
✓ Branch 1 taken 621 times.
10557 for (i = 0; i < FF_ARRAY_ELEMS(h->last_pocs); i++)
311 9936 h->last_pocs[i] = INT_MIN;
312
313 621 ff_h264_sei_uninit(&h->sei);
314
315
2/2
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 603 times.
621 if (avctx->active_thread_type & FF_THREAD_FRAME) {
316 18 h->decode_error_flags_pool = av_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 620 times.
621 h->nb_slice_ctx = (avctx->active_thread_type & FF_THREAD_SLICE) ? avctx->thread_count : 1;
322 621 h->slice_ctx = av_calloc(h->nb_slice_ctx, sizeof(*h->slice_ctx));
323
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 621 times.
621 if (!h->slice_ctx) {
324 h->nb_slice_ctx = 0;
325 return AVERROR(ENOMEM);
326 }
327
328
2/2
✓ Branch 0 taken 22356 times.
✓ Branch 1 taken 621 times.
22977 for (i = 0; i < H264_MAX_PICTURE_COUNT; i++) {
329
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 22356 times.
22356 if ((ret = h264_init_pic(&h->DPB[i])) < 0)
330 return ret;
331 }
332
333
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 621 times.
621 if ((ret = h264_init_pic(&h->cur_pic)) < 0)
334 return ret;
335
336
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 621 times.
621 if ((ret = h264_init_pic(&h->last_pic_for_ec)) < 0)
337 return ret;
338
339
2/2
✓ Branch 0 taken 622 times.
✓ Branch 1 taken 621 times.
1243 for (i = 0; i < h->nb_slice_ctx; i++)
340 622 h->slice_ctx[i].h264 = h;
341
342 621 return 0;
343 }
344
345 23598 static void h264_free_pic(H264Context *h, H264Picture *pic)
346 {
347 23598 ff_h264_unref_picture(pic);
348 23598 av_frame_free(&pic->f);
349 23598 av_frame_free(&pic->f_grain);
350 23598 }
351
352 621 static av_cold int h264_decode_end(AVCodecContext *avctx)
353 {
354 621 H264Context *h = avctx->priv_data;
355 int i;
356
357 621 ff_h264_remove_all_refs(h);
358 621 ff_h264_free_tables(h);
359
360
2/2
✓ Branch 0 taken 22356 times.
✓ Branch 1 taken 621 times.
22977 for (i = 0; i < H264_MAX_PICTURE_COUNT; i++) {
361 22356 h264_free_pic(h, &h->DPB[i]);
362 }
363 621 memset(h->delayed_pic, 0, sizeof(h->delayed_pic));
364
365 621 h->cur_pic_ptr = NULL;
366
367 621 av_refstruct_pool_uninit(&h->decode_error_flags_pool);
368
369 621 av_freep(&h->slice_ctx);
370 621 h->nb_slice_ctx = 0;
371
372 621 ff_h264_sei_uninit(&h->sei);
373 621 ff_h264_ps_uninit(&h->ps);
374
375 621 ff_h2645_packet_uninit(&h->pkt);
376
377 621 h264_free_pic(h, &h->cur_pic);
378 621 h264_free_pic(h, &h->last_pic_for_ec);
379
380 621 return 0;
381 }
382
383 static AVOnce h264_vlc_init = AV_ONCE_INIT;
384
385 621 static av_cold int h264_decode_init(AVCodecContext *avctx)
386 {
387 621 H264Context *h = avctx->priv_data;
388 int ret;
389
390 621 ret = h264_init_context(avctx, h);
391
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 621 times.
621 if (ret < 0)
392 return ret;
393
394 621 ret = ff_thread_once(&h264_vlc_init, ff_h264_decode_init_vlc);
395
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 621 times.
621 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 621 avctx->ticks_per_frame = 2;
403 FF_ENABLE_DEPRECATION_WARNINGS
404 #endif
405
406
2/2
✓ Branch 0 taken 605 times.
✓ Branch 1 taken 16 times.
621 if (!avctx->internal->is_copy) {
407
3/4
✓ Branch 0 taken 372 times.
✓ Branch 1 taken 233 times.
✓ Branch 2 taken 372 times.
✗ Branch 3 not taken.
605 if (avctx->extradata_size > 0 && avctx->extradata) {
408 372 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 372 times.
372 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 621 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
621 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 621 ff_h264_flush_change(h);
429
430
3/4
✓ Branch 0 taken 621 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 620 times.
621 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 620 times.
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 620 times.
621 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 621 return 0;
440 }
441
442 /**
443 * instantaneous decoder refresh.
444 */
445 1731 static void idr(H264Context *h)
446 {
447 int i;
448 1731 ff_h264_remove_all_refs(h);
449 1731 h->poc.prev_frame_num =
450 1731 h->poc.prev_frame_num_offset = 0;
451 1731 h->poc.prev_poc_msb = 1<<16;
452 1731 h->poc.prev_poc_lsb = -1;
453
2/2
✓ Branch 0 taken 27696 times.
✓ Branch 1 taken 1731 times.
29427 for (i = 0; i < FF_ARRAY_ELEMS(h->last_pocs); i++)
454 27696 h->last_pocs[i] = INT_MIN;
455 1731 }
456
457 /* forget old pics after a seek */
458 635 void ff_h264_flush_change(H264Context *h)
459 {
460 int i, j;
461
462 635 h->next_outputed_poc = INT_MIN;
463 635 h->prev_interlaced_frame = 1;
464 635 idr(h);
465
466 635 h->poc.prev_frame_num = -1;
467
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 635 times.
635 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 635 ff_h264_unref_picture(&h->last_pic_for_ec);
475
476 635 h->first_field = 0;
477 635 h->recovery_frame = -1;
478 635 h->frame_recovered = 0;
479 635 h->current_slice = 0;
480 635 h->mmco_reset = 1;
481 635 }
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 29261 static int decode_nal_units(H264Context *h, const uint8_t *buf, int buf_size)
591 {
592 29261 AVCodecContext *const avctx = h->avctx;
593 29261 int nals_needed = 0; ///< number of NALs that need decoding before the next frame thread starts
594 29261 int idr_cleared=0;
595 29261 int i, ret = 0;
596
597 29261 h->has_slice = 0;
598 29261 h->nal_unit_type= 0;
599
600
2/2
✓ Branch 0 taken 28505 times.
✓ Branch 1 taken 756 times.
29261 if (!(avctx->flags2 & AV_CODEC_FLAG2_CHUNKS)) {
601 28505 h->current_slice = 0;
602
2/2
✓ Branch 0 taken 25118 times.
✓ Branch 1 taken 3387 times.
28505 if (!h->first_field) {
603 25118 h->cur_pic_ptr = NULL;
604 25118 ff_h264_sei_uninit(&h->sei);
605 }
606 }
607
608
2/2
✓ Branch 0 taken 3182 times.
✓ Branch 1 taken 26079 times.
29261 if (h->nal_length_size == 4) {
609
2/6
✓ Branch 0 taken 3182 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 3182 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
3182 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 3182 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3182 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 3182 times.
✗ Branch 5 not taken.
3182 }else if(buf_size > 3 && AV_RB32(buf) > 1 && AV_RB32(buf) <= (unsigned)buf_size)
612 3182 h->is_avc = 1;
613 }
614
615 29261 ret = ff_h2645_packet_split(&h->pkt, buf, buf_size, avctx, h->nal_length_size,
616 29261 avctx->codec_id, !!h->is_avc * H2645_FLAG_IS_NALFF);
617
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 29260 times.
29261 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 29218 times.
29260 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 29260 times.
29260 if (nals_needed < 0)
626 return nals_needed;
627
628
2/2
✓ Branch 0 taken 51864 times.
✓ Branch 1 taken 29260 times.
81124 for (i = 0; i < h->pkt.nb_nals; i++) {
629 51864 H2645NAL *nal = &h->pkt.nals[i];
630 int max_slice_ctx, err;
631
632
2/2
✓ Branch 0 taken 928 times.
✓ Branch 1 taken 50936 times.
51864 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 51360 h->nal_ref_idc = nal->ref_idc;
638 51360 h->nal_unit_type = nal->type;
639
640 51360 err = 0;
641
6/8
✓ Branch 0 taken 2275 times.
✓ Branch 1 taken 35554 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 2865 times.
✓ Branch 4 taken 1065 times.
✓ Branch 5 taken 6939 times.
✓ Branch 6 taken 2662 times.
✗ Branch 7 not taken.
51360 switch (nal->type) {
642 2275 case H264_NAL_IDR_SLICE:
643
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2275 times.
2275 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 1096 times.
✓ Branch 1 taken 1179 times.
2275 if(!idr_cleared) {
650 1096 idr(h); // FIXME ensure we don't lose some frames if there is reordering
651 }
652 2275 idr_cleared = 1;
653 2275 h->has_recovery_point = 1;
654 37829 case H264_NAL_SLICE:
655 37829 h->has_slice = 1;
656
657
2/2
✓ Branch 1 taken 167 times.
✓ Branch 2 taken 37662 times.
37829 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 28408 times.
✓ Branch 1 taken 9254 times.
37662 if (h->current_slice == 1) {
664
3/4
✓ Branch 0 taken 42 times.
✓ Branch 1 taken 28366 times.
✓ Branch 2 taken 42 times.
✗ Branch 3 not taken.
28408 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 28408 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
28408 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 37662 times.
✗ Branch 1 not taken.
37662 max_slice_ctx = avctx->hwaccel ? 1 : h->nb_slice_ctx;
676
2/2
✓ Branch 0 taken 36832 times.
✓ Branch 1 taken 830 times.
37662 if (h->nb_slice_ctx_queued == max_slice_ctx) {
677
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 36832 times.
36832 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 36832 ret = ff_h264_execute_decode_slices(h);
682
3/4
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 36830 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
36832 if (ret < 0 && (h->avctx->err_recognition & AV_EF_EXPLODE))
683 goto end;
684 }
685 37662 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 2865 case H264_NAL_SEI:
692
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2865 times.
2865 if (h->setup_finished) {
693 avpriv_request_sample(avctx, "Late SEI");
694 break;
695 }
696 2865 ret = ff_h264_sei_decode(&h->sei, &nal->gb, &h->ps, avctx);
697
4/4
✓ Branch 0 taken 313 times.
✓ Branch 1 taken 2552 times.
✓ Branch 2 taken 29 times.
✓ Branch 3 taken 284 times.
2865 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 2865 times.
2865 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 2858 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 7 times.
2865 if (ret < 0 && (h->avctx->err_recognition & AV_EF_EXPLODE))
701 goto end;
702 2865 break;
703 1065 case H264_NAL_SPS: {
704 1065 GetBitContext tmp_gb = nal->gb;
705
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 1065 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
1065 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 1063 times.
✓ Branch 2 taken 2 times.
1065 if (ff_h264_decode_seq_parameter_set(&tmp_gb, avctx, &h->ps, 0) >= 0)
712 1065 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 6939 case H264_NAL_PPS:
722
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 6939 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
6939 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 6939 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 6932 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 7 times.
6939 if (ret < 0 && (h->avctx->err_recognition & AV_EF_EXPLODE))
731 goto end;
732 6939 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
3/4
✓ Branch 0 taken 167 times.
✓ Branch 1 taken 51193 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 167 times.
51360 if (err < 0 && (h->avctx->err_recognition & AV_EF_EXPLODE)) {
746 av_log(h->avctx, AV_LOG_ERROR, "decode_slice_header error\n");
747 ret = err;
748 goto end;
749 }
750 }
751
752 29260 ret = ff_h264_execute_decode_slices(h);
753
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 29260 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
29260 if (ret < 0 && (h->avctx->err_recognition & AV_EF_EXPLODE))
754 goto end;
755
756 // set decode_error_flags to allow users to detect concealed decoding errors
757
4/6
✓ Branch 0 taken 29260 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 29258 times.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
29260 if ((ret < 0 || h->er.error_occurred) && h->cur_pic_ptr) {
758
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (h->cur_pic_ptr->decode_error_flags) {
759 /* Frame-threading in use */
760 atomic_int *decode_error = h->cur_pic_ptr->decode_error_flags;
761 /* Using atomics here is not supposed to provide syncronisation;
762 * they are merely used to allow to set decode_error from both
763 * decoding threads in case of coded slices. */
764 atomic_fetch_or_explicit(decode_error, FF_DECODE_ERROR_DECODE_SLICES,
765 memory_order_relaxed);
766 } else
767 2 h->cur_pic_ptr->f->decode_error_flags |= FF_DECODE_ERROR_DECODE_SLICES;
768 }
769
770 29260 ret = 0;
771 29260 end:
772
773 #if CONFIG_ERROR_RESILIENCE
774 /*
775 * FIXME: Error handling code does not seem to support interlaced
776 * when slices span multiple rows
777 * The ff_er_add_slice calls don't work right for bottom
778 * fields; they cause massive erroneous error concealing
779 * Error marking covers both fields (top and bottom).
780 * This causes a mismatched s->error_count
781 * and a bad error table. Further, the error count goes to
782 * INT_MAX when called for bottom field, because mb_y is
783 * past end by one (callers fault) and resync_mb_y != 0
784 * causes problems for the first MB line, too.
785 */
786
6/6
✓ Branch 0 taken 22057 times.
✓ Branch 1 taken 7203 times.
✓ Branch 2 taken 22018 times.
✓ Branch 3 taken 39 times.
✓ Branch 4 taken 21268 times.
✓ Branch 5 taken 750 times.
29260 if (!FIELD_PICTURE(h) && h->current_slice && h->enable_er) {
787
788 21268 H264SliceContext *sl = h->slice_ctx;
789
3/4
✓ Branch 0 taken 513 times.
✓ Branch 1 taken 20755 times.
✓ Branch 2 taken 513 times.
✗ Branch 3 not taken.
21268 int use_last_pic = h->last_pic_for_ec.f->buf[0] && !sl->ref_count[0];
790 21268 int decode_error_flags = 0;
791
792 21268 ff_h264_set_erpic(&h->er.cur_pic, h->cur_pic_ptr);
793
794
2/2
✓ Branch 0 taken 513 times.
✓ Branch 1 taken 20755 times.
21268 if (use_last_pic) {
795 513 ff_h264_set_erpic(&h->er.last_pic, &h->last_pic_for_ec);
796 513 sl->ref_list[0][0].parent = &h->last_pic_for_ec;
797 513 memcpy(sl->ref_list[0][0].data, h->last_pic_for_ec.f->data, sizeof(sl->ref_list[0][0].data));
798 513 memcpy(sl->ref_list[0][0].linesize, h->last_pic_for_ec.f->linesize, sizeof(sl->ref_list[0][0].linesize));
799 513 sl->ref_list[0][0].reference = h->last_pic_for_ec.reference;
800
2/2
✓ Branch 0 taken 18772 times.
✓ Branch 1 taken 1983 times.
20755 } else if (sl->ref_count[0]) {
801 18772 ff_h264_set_erpic(&h->er.last_pic, sl->ref_list[0][0].parent);
802 } else
803 1983 ff_h264_set_erpic(&h->er.last_pic, NULL);
804
805
2/2
✓ Branch 0 taken 18772 times.
✓ Branch 1 taken 2496 times.
21268 if (sl->ref_count[1])
806 18772 ff_h264_set_erpic(&h->er.next_pic, sl->ref_list[1][0].parent);
807
808 21268 ff_er_frame_end(&h->er, &decode_error_flags);
809
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 21267 times.
21268 if (decode_error_flags) {
810
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (h->cur_pic_ptr->decode_error_flags) {
811 atomic_int *decode_error = h->cur_pic_ptr->decode_error_flags;
812 atomic_fetch_or_explicit(decode_error, decode_error_flags,
813 memory_order_relaxed);
814 } else
815 1 h->cur_pic_ptr->f->decode_error_flags |= decode_error_flags;
816 }
817
2/2
✓ Branch 0 taken 513 times.
✓ Branch 1 taken 20755 times.
21268 if (use_last_pic)
818 513 memset(&sl->ref_list[0][0], 0, sizeof(sl->ref_list[0][0]));
819 }
820 #endif /* CONFIG_ERROR_RESILIENCE */
821 /* clean up */
822
6/6
✓ Branch 0 taken 28832 times.
✓ Branch 1 taken 428 times.
✓ Branch 2 taken 19146 times.
✓ Branch 3 taken 9686 times.
✓ Branch 4 taken 18980 times.
✓ Branch 5 taken 166 times.
29260 if (h->cur_pic_ptr && !h->droppable && h->has_slice) {
823 18980 ff_thread_report_progress(&h->cur_pic_ptr->tf, INT_MAX,
824 18980 h->picture_structure == PICT_BOTTOM_FIELD);
825 }
826
827
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 29260 times.
29260 return (ret < 0) ? ret : buf_size;
828 }
829
830 /**
831 * Return the number of bytes consumed for building the current frame.
832 */
833 28670 static int get_consumed_bytes(int pos, int buf_size)
834 {
835
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 28670 times.
28670 if (pos == 0)
836 pos = 1; // avoid infinite loops (I doubt that is needed but...)
837
1/2
✓ Branch 0 taken 28670 times.
✗ Branch 1 not taken.
28670 if (pos + 10 > buf_size)
838 28670 pos = buf_size; // oops ;)
839
840 28670 return pos;
841 }
842
843 1 static int h264_export_enc_params(AVFrame *f, const H264Picture *p)
844 {
845 AVVideoEncParams *par;
846 1 unsigned int nb_mb = p->mb_height * p->mb_width;
847 unsigned int x, y;
848
849 1 par = av_video_enc_params_create_side_data(f, AV_VIDEO_ENC_PARAMS_H264, nb_mb);
850
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!par)
851 return AVERROR(ENOMEM);
852
853 1 par->qp = p->pps->init_qp;
854
855 1 par->delta_qp[1][0] = p->pps->chroma_qp_index_offset[0];
856 1 par->delta_qp[1][1] = p->pps->chroma_qp_index_offset[0];
857 1 par->delta_qp[2][0] = p->pps->chroma_qp_index_offset[1];
858 1 par->delta_qp[2][1] = p->pps->chroma_qp_index_offset[1];
859
860
2/2
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 1 times.
19 for (y = 0; y < p->mb_height; y++)
861
2/2
✓ Branch 0 taken 396 times.
✓ Branch 1 taken 18 times.
414 for (x = 0; x < p->mb_width; x++) {
862 396 const unsigned int block_idx = y * p->mb_width + x;
863 396 const unsigned int mb_xy = y * p->mb_stride + x;
864 396 AVVideoBlockParams *b = av_video_enc_params_block(par, block_idx);
865
866 396 b->src_x = x * 16;
867 396 b->src_y = y * 16;
868 396 b->w = 16;
869 396 b->h = 16;
870
871 396 b->delta_qp = p->qscale_table[mb_xy] - par->qp;
872 }
873
874 1 return 0;
875 }
876
877 24366 static int output_frame(H264Context *h, AVFrame *dst, H264Picture *srcp)
878 {
879 int ret;
880
881
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 24366 times.
24366 ret = av_frame_ref(dst, srcp->needs_fg ? srcp->f_grain : srcp->f);
882
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 24366 times.
24366 if (ret < 0)
883 return ret;
884
885
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 24366 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
24366 if (srcp->needs_fg && (ret = av_frame_copy_props(dst, srcp->f)) < 0)
886 return ret;
887
888
2/2
✓ Branch 0 taken 42 times.
✓ Branch 1 taken 24324 times.
24366 if (srcp->decode_error_flags) {
889 42 atomic_int *decode_error = srcp->decode_error_flags;
890 /* The following is not supposed to provide synchronisation at all:
891 * given that srcp has already finished decoding, decode_error
892 * has already been set to its final value. */
893 42 dst->decode_error_flags |= atomic_load_explicit(decode_error, memory_order_relaxed);
894 }
895
896 24366 av_dict_set(&dst->metadata, "stereo_mode", ff_h264_sei_stereo_mode(&h->sei.common.frame_packing), 0);
897
898
2/2
✓ Branch 0 taken 34 times.
✓ Branch 1 taken 24332 times.
24366 if (srcp->sei_recovery_frame_cnt == 0)
899 34 dst->flags |= AV_FRAME_FLAG_KEY;
900
901
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 24365 times.
24366 if (h->avctx->export_side_data & AV_CODEC_EXPORT_DATA_VIDEO_ENC_PARAMS) {
902 1 ret = h264_export_enc_params(dst, srcp);
903
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (ret < 0)
904 goto fail;
905 }
906
907
1/2
✓ Branch 0 taken 24366 times.
✗ Branch 1 not taken.
24366 if (!(h->avctx->export_side_data & AV_CODEC_EXPORT_DATA_FILM_GRAIN))
908 24366 av_frame_remove_side_data(dst, AV_FRAME_DATA_FILM_GRAIN_PARAMS);
909
910 24366 return 0;
911 fail:
912 av_frame_unref(dst);
913 return ret;
914 }
915
916 static int is_avcc_extradata(const uint8_t *buf, int buf_size)
917 {
918 int cnt= buf[5]&0x1f;
919 const uint8_t *p= buf+6;
920 if (!cnt)
921 return 0;
922 while(cnt--){
923 int nalsize= AV_RB16(p) + 2;
924 if(nalsize > buf_size - (p-buf) || (p[2] & 0x9F) != 7)
925 return 0;
926 p += nalsize;
927 }
928 cnt = *(p++);
929 if(!cnt)
930 return 0;
931 while(cnt--){
932 int nalsize= AV_RB16(p) + 2;
933 if(nalsize > buf_size - (p-buf) || (p[2] & 0x9F) != 8)
934 return 0;
935 p += nalsize;
936 }
937 return 1;
938 }
939
940 24366 static int finalize_frame(H264Context *h, AVFrame *dst, H264Picture *out, int *got_frame)
941 {
942 int ret;
943
944
1/2
✓ Branch 0 taken 24366 times.
✗ Branch 1 not taken.
24366 if (((h->avctx->flags & AV_CODEC_FLAG_OUTPUT_CORRUPT) ||
945
1/2
✓ Branch 0 taken 24366 times.
✗ Branch 1 not taken.
24366 (h->avctx->flags2 & AV_CODEC_FLAG2_SHOW_ALL) ||
946
1/2
✓ Branch 0 taken 24366 times.
✗ Branch 1 not taken.
24366 out->recovered)) {
947
948
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 24366 times.
24366 if (h->skip_gray > 0 &&
949 h->non_gray && out->gray &&
950 !(h->avctx->flags2 & AV_CODEC_FLAG2_SHOW_ALL)
951 )
952 return 0;
953
954
1/2
✓ Branch 0 taken 24366 times.
✗ Branch 1 not taken.
24366 if (!h->avctx->hwaccel &&
955
1/2
✓ Branch 0 taken 24366 times.
✗ Branch 1 not taken.
24366 (out->field_poc[0] == INT_MAX ||
956
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 24366 times.
24366 out->field_poc[1] == INT_MAX)
957 ) {
958 int p;
959 AVFrame *f = out->f;
960 int field = out->field_poc[0] == INT_MAX;
961 uint8_t *dst_data[4];
962 int linesizes[4];
963 const uint8_t *src_data[4];
964
965 av_log(h->avctx, AV_LOG_DEBUG, "Duplicating field %d to fill missing\n", field);
966
967 for (p = 0; p<4; p++) {
968 dst_data[p] = f->data[p] + (field^1)*f->linesize[p];
969 src_data[p] = f->data[p] + field *f->linesize[p];
970 linesizes[p] = 2*f->linesize[p];
971 }
972
973 av_image_copy(dst_data, linesizes, src_data, linesizes,
974 f->format, f->width, f->height>>1);
975 }
976
977 24366 ret = output_frame(h, dst, out);
978
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 24366 times.
24366 if (ret < 0)
979 return ret;
980
981 24366 *got_frame = 1;
982
983 if (CONFIG_MPEGVIDEODEC) {
984 24366 ff_print_debug_info2(h->avctx, dst,
985 24366 out->mb_type,
986 24366 out->qscale_table,
987 24366 out->motion_val,
988 out->mb_width, out->mb_height, out->mb_stride, 1);
989 }
990 }
991
992 24366 return 0;
993 }
994
995 562 static int send_next_delayed_frame(H264Context *h, AVFrame *dst_frame,
996 int *got_frame, int buf_index)
997 {
998 int ret, i, out_idx;
999 H264Picture *out;
1000
1001 562 h->cur_pic_ptr = NULL;
1002 562 h->first_field = 0;
1003
1004
2/2
✓ Branch 0 taken 280 times.
✓ Branch 1 taken 282 times.
1124 while (h->delayed_pic[0]) {
1005 280 out = h->delayed_pic[0];
1006 280 out_idx = 0;
1007 280 for (i = 1;
1008 1335 h->delayed_pic[i] &&
1009
4/4
✓ Branch 0 taken 531 times.
✓ Branch 1 taken 273 times.
✓ Branch 2 taken 524 times.
✓ Branch 3 taken 7 times.
804 !(h->delayed_pic[i]->f->flags & AV_FRAME_FLAG_KEY) &&
1010
1/2
✓ Branch 0 taken 524 times.
✗ Branch 1 not taken.
524 !h->delayed_pic[i]->mmco_reset;
1011 524 i++)
1012
2/2
✓ Branch 0 taken 106 times.
✓ Branch 1 taken 418 times.
524 if (h->delayed_pic[i]->poc < out->poc) {
1013 106 out = h->delayed_pic[i];
1014 106 out_idx = i;
1015 }
1016
1017
2/2
✓ Branch 0 taken 762 times.
✓ Branch 1 taken 280 times.
1042 for (i = out_idx; h->delayed_pic[i]; i++)
1018 762 h->delayed_pic[i] = h->delayed_pic[i + 1];
1019
1020
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 280 times.
280 if (out) {
1021 280 h->frame_recovered |= out->recovered;
1022 280 out->recovered |= h->frame_recovered & FRAME_RECOVERED_SEI;
1023
1024 280 out->reference &= ~DELAYED_PIC_REF;
1025 280 ret = finalize_frame(h, dst_frame, out, got_frame);
1026
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 280 times.
280 if (ret < 0)
1027 return ret;
1028
1/2
✓ Branch 0 taken 280 times.
✗ Branch 1 not taken.
280 if (*got_frame)
1029 280 break;
1030 }
1031 }
1032
1033 562 return buf_index;
1034 }
1035
1036 29823 static int h264_decode_frame(AVCodecContext *avctx, AVFrame *pict,
1037 int *got_frame, AVPacket *avpkt)
1038 {
1039 29823 const uint8_t *buf = avpkt->data;
1040 29823 int buf_size = avpkt->size;
1041 29823 H264Context *h = avctx->priv_data;
1042 int buf_index;
1043 int ret;
1044
1045 29823 h->flags = avctx->flags;
1046 29823 h->setup_finished = 0;
1047 29823 h->nb_slice_ctx_queued = 0;
1048
1049 29823 ff_h264_unref_picture(&h->last_pic_for_ec);
1050
1051 /* end of stream, output what is still in the buffers */
1052
2/2
✓ Branch 0 taken 562 times.
✓ Branch 1 taken 29261 times.
29823 if (buf_size == 0)
1053 562 return send_next_delayed_frame(h, pict, got_frame, 0);
1054
1055
2/2
✓ Branch 1 taken 7 times.
✓ Branch 2 taken 29254 times.
29261 if (av_packet_get_side_data(avpkt, AV_PKT_DATA_NEW_EXTRADATA, NULL)) {
1056 size_t side_size;
1057 7 uint8_t *side = av_packet_get_side_data(avpkt, AV_PKT_DATA_NEW_EXTRADATA, &side_size);
1058 7 ff_h264_decode_extradata(side, side_size,
1059 &h->ps, &h->is_avc, &h->nal_length_size,
1060 avctx->err_recognition, avctx);
1061 }
1062
4/10
✓ Branch 0 taken 3182 times.
✓ Branch 1 taken 26079 times.
✓ Branch 2 taken 3182 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 3182 times.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
29261 if (h->is_avc && buf_size >= 9 && buf[0]==1 && buf[2]==0 && (buf[4]&0xFC)==0xFC) {
1063 if (is_avcc_extradata(buf, buf_size))
1064 return ff_h264_decode_extradata(buf, buf_size,
1065 &h->ps, &h->is_avc, &h->nal_length_size,
1066 avctx->err_recognition, avctx);
1067 }
1068
1069 29261 buf_index = decode_nal_units(h, buf, buf_size);
1070
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 29260 times.
29261 if (buf_index < 0)
1071 1 return AVERROR_INVALIDDATA;
1072
1073
3/4
✓ Branch 0 taken 428 times.
✓ Branch 1 taken 28832 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 428 times.
29260 if (!h->cur_pic_ptr && h->nal_unit_type == H264_NAL_END_SEQUENCE) {
1074 av_assert0(buf_index <= buf_size);
1075 return send_next_delayed_frame(h, pict, got_frame, buf_index);
1076 }
1077
1078
6/6
✓ Branch 0 taken 28504 times.
✓ Branch 1 taken 756 times.
✓ Branch 2 taken 28078 times.
✓ Branch 3 taken 426 times.
✓ Branch 4 taken 164 times.
✓ Branch 5 taken 27914 times.
29260 if (!(avctx->flags2 & AV_CODEC_FLAG2_CHUNKS) && (!h->cur_pic_ptr || !h->has_slice)) {
1079
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 ||
1080
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 390 times.
390 buf_size >= 4 && !memcmp("Q264", buf, 4))
1081 200 return buf_size;
1082 390 av_log(avctx, AV_LOG_ERROR, "no frame!\n");
1083 390 return AVERROR_INVALIDDATA;
1084 }
1085
1086
2/2
✓ Branch 0 taken 756 times.
✓ Branch 1 taken 27914 times.
28670 if (!(avctx->flags2 & AV_CODEC_FLAG2_CHUNKS) ||
1087
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)) {
1088
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 28214 times.
28214 if ((ret = ff_h264_field_end(h, &h->slice_ctx[0], 0)) < 0)
1089 return ret;
1090
1091 /* Wait for second field. */
1092
2/2
✓ Branch 0 taken 24086 times.
✓ Branch 1 taken 4128 times.
28214 if (h->next_output_pic) {
1093 24086 ret = finalize_frame(h, pict, h->next_output_pic, got_frame);
1094
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 24086 times.
24086 if (ret < 0)
1095 return ret;
1096 }
1097 }
1098
1099
3/4
✓ Branch 0 taken 4584 times.
✓ Branch 1 taken 24086 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 4584 times.
28670 av_assert0(pict->buf[0] || !*got_frame);
1100
1101 28670 ff_h264_unref_picture(&h->last_pic_for_ec);
1102
1103 28670 return get_consumed_bytes(buf_index, buf_size);
1104 }
1105
1106 #define OFFSET(x) offsetof(H264Context, x)
1107 #define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
1108 #define VDX VD | AV_OPT_FLAG_EXPORT
1109 static const AVOption h264_options[] = {
1110 { "is_avc", "is avc", OFFSET(is_avc), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, VDX },
1111 { "nal_length_size", "nal_length_size", OFFSET(nal_length_size), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 4, VDX },
1112 { "enable_er", "Enable error resilience on damaged frames (unsafe)", OFFSET(enable_er), AV_OPT_TYPE_BOOL, { .i64 = -1 }, -1, 1, VD },
1113 { "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 },
1114 { "skip_gray", "Do not return gray gap frames", OFFSET(skip_gray), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, VD },
1115 { "noref_gray", "Avoid using gray gap frames as references", OFFSET(noref_gray), AV_OPT_TYPE_BOOL, {.i64 = 1}, 0, 1, VD },
1116 { NULL },
1117 };
1118
1119 static const AVClass h264_class = {
1120 .class_name = "H264 Decoder",
1121 .item_name = av_default_item_name,
1122 .option = h264_options,
1123 .version = LIBAVUTIL_VERSION_INT,
1124 };
1125
1126 const FFCodec ff_h264_decoder = {
1127 .p.name = "h264",
1128 CODEC_LONG_NAME("H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10"),
1129 .p.type = AVMEDIA_TYPE_VIDEO,
1130 .p.id = AV_CODEC_ID_H264,
1131 .priv_data_size = sizeof(H264Context),
1132 .init = h264_decode_init,
1133 .close = h264_decode_end,
1134 FF_CODEC_DECODE_CB(h264_decode_frame),
1135 .p.capabilities = AV_CODEC_CAP_DR1 |
1136 AV_CODEC_CAP_DELAY | AV_CODEC_CAP_SLICE_THREADS |
1137 AV_CODEC_CAP_FRAME_THREADS,
1138 .hw_configs = (const AVCodecHWConfigInternal *const []) {
1139 #if CONFIG_H264_DXVA2_HWACCEL
1140 HWACCEL_DXVA2(h264),
1141 #endif
1142 #if CONFIG_H264_D3D11VA_HWACCEL
1143 HWACCEL_D3D11VA(h264),
1144 #endif
1145 #if CONFIG_H264_D3D11VA2_HWACCEL
1146 HWACCEL_D3D11VA2(h264),
1147 #endif
1148 #if CONFIG_H264_D3D12VA_HWACCEL
1149 HWACCEL_D3D12VA(h264),
1150 #endif
1151 #if CONFIG_H264_NVDEC_HWACCEL
1152 HWACCEL_NVDEC(h264),
1153 #endif
1154 #if CONFIG_H264_VAAPI_HWACCEL
1155 HWACCEL_VAAPI(h264),
1156 #endif
1157 #if CONFIG_H264_VDPAU_HWACCEL
1158 HWACCEL_VDPAU(h264),
1159 #endif
1160 #if CONFIG_H264_VIDEOTOOLBOX_HWACCEL
1161 HWACCEL_VIDEOTOOLBOX(h264),
1162 #endif
1163 #if CONFIG_H264_VULKAN_HWACCEL
1164 HWACCEL_VULKAN(h264),
1165 #endif
1166 NULL
1167 },
1168 .caps_internal = FF_CODEC_CAP_EXPORTS_CROPPING |
1169 FF_CODEC_CAP_INIT_CLEANUP,
1170 .flush = h264_decode_flush,
1171 UPDATE_THREAD_CONTEXT(ff_h264_update_thread_context),
1172 UPDATE_THREAD_CONTEXT_FOR_USER(ff_h264_update_thread_context_for_user),
1173 .p.profiles = NULL_IF_CONFIG_SMALL(ff_h264_profiles),
1174 .p.priv_class = &h264_class,
1175 };
1176