FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/h264dec.c
Date: 2022-07-04 19:11:22
Exec Total Coverage
Lines: 456 615 74.1%
Branches: 255 423 60.3%

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