FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/h264dec.c
Date: 2021-09-26 18:22:30
Exec Total Coverage
Lines: 447 618 72.3%
Branches: 255 425 60.0%

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