FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/h264_slice.c
Date: 2024-02-29 09:57:37
Exec Total Coverage
Lines: 1374 1670 82.3%
Functions: 32 33 97.0%
Branches: 915 1251 73.1%

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 #include "config_components.h"
29
30 #include "libavutil/avassert.h"
31 #include "libavutil/pixdesc.h"
32 #include "libavutil/timecode.h"
33 #include "decode.h"
34 #include "cabac.h"
35 #include "cabac_functions.h"
36 #include "error_resilience.h"
37 #include "avcodec.h"
38 #include "h264.h"
39 #include "h264dec.h"
40 #include "h264data.h"
41 #include "h264chroma.h"
42 #include "h264_ps.h"
43 #include "golomb.h"
44 #include "mathops.h"
45 #include "mpegutils.h"
46 #include "rectangle.h"
47 #include "refstruct.h"
48 #include "thread.h"
49 #include "threadframe.h"
50
51 static const uint8_t field_scan[16+1] = {
52 0 + 0 * 4, 0 + 1 * 4, 1 + 0 * 4, 0 + 2 * 4,
53 0 + 3 * 4, 1 + 1 * 4, 1 + 2 * 4, 1 + 3 * 4,
54 2 + 0 * 4, 2 + 1 * 4, 2 + 2 * 4, 2 + 3 * 4,
55 3 + 0 * 4, 3 + 1 * 4, 3 + 2 * 4, 3 + 3 * 4,
56 };
57
58 static const uint8_t field_scan8x8[64+1] = {
59 0 + 0 * 8, 0 + 1 * 8, 0 + 2 * 8, 1 + 0 * 8,
60 1 + 1 * 8, 0 + 3 * 8, 0 + 4 * 8, 1 + 2 * 8,
61 2 + 0 * 8, 1 + 3 * 8, 0 + 5 * 8, 0 + 6 * 8,
62 0 + 7 * 8, 1 + 4 * 8, 2 + 1 * 8, 3 + 0 * 8,
63 2 + 2 * 8, 1 + 5 * 8, 1 + 6 * 8, 1 + 7 * 8,
64 2 + 3 * 8, 3 + 1 * 8, 4 + 0 * 8, 3 + 2 * 8,
65 2 + 4 * 8, 2 + 5 * 8, 2 + 6 * 8, 2 + 7 * 8,
66 3 + 3 * 8, 4 + 1 * 8, 5 + 0 * 8, 4 + 2 * 8,
67 3 + 4 * 8, 3 + 5 * 8, 3 + 6 * 8, 3 + 7 * 8,
68 4 + 3 * 8, 5 + 1 * 8, 6 + 0 * 8, 5 + 2 * 8,
69 4 + 4 * 8, 4 + 5 * 8, 4 + 6 * 8, 4 + 7 * 8,
70 5 + 3 * 8, 6 + 1 * 8, 6 + 2 * 8, 5 + 4 * 8,
71 5 + 5 * 8, 5 + 6 * 8, 5 + 7 * 8, 6 + 3 * 8,
72 7 + 0 * 8, 7 + 1 * 8, 6 + 4 * 8, 6 + 5 * 8,
73 6 + 6 * 8, 6 + 7 * 8, 7 + 2 * 8, 7 + 3 * 8,
74 7 + 4 * 8, 7 + 5 * 8, 7 + 6 * 8, 7 + 7 * 8,
75 };
76
77 static const uint8_t field_scan8x8_cavlc[64+1] = {
78 0 + 0 * 8, 1 + 1 * 8, 2 + 0 * 8, 0 + 7 * 8,
79 2 + 2 * 8, 2 + 3 * 8, 2 + 4 * 8, 3 + 3 * 8,
80 3 + 4 * 8, 4 + 3 * 8, 4 + 4 * 8, 5 + 3 * 8,
81 5 + 5 * 8, 7 + 0 * 8, 6 + 6 * 8, 7 + 4 * 8,
82 0 + 1 * 8, 0 + 3 * 8, 1 + 3 * 8, 1 + 4 * 8,
83 1 + 5 * 8, 3 + 1 * 8, 2 + 5 * 8, 4 + 1 * 8,
84 3 + 5 * 8, 5 + 1 * 8, 4 + 5 * 8, 6 + 1 * 8,
85 5 + 6 * 8, 7 + 1 * 8, 6 + 7 * 8, 7 + 5 * 8,
86 0 + 2 * 8, 0 + 4 * 8, 0 + 5 * 8, 2 + 1 * 8,
87 1 + 6 * 8, 4 + 0 * 8, 2 + 6 * 8, 5 + 0 * 8,
88 3 + 6 * 8, 6 + 0 * 8, 4 + 6 * 8, 6 + 2 * 8,
89 5 + 7 * 8, 6 + 4 * 8, 7 + 2 * 8, 7 + 6 * 8,
90 1 + 0 * 8, 1 + 2 * 8, 0 + 6 * 8, 3 + 0 * 8,
91 1 + 7 * 8, 3 + 2 * 8, 2 + 7 * 8, 4 + 2 * 8,
92 3 + 7 * 8, 5 + 2 * 8, 4 + 7 * 8, 5 + 4 * 8,
93 6 + 3 * 8, 6 + 5 * 8, 7 + 3 * 8, 7 + 7 * 8,
94 };
95
96 // zigzag_scan8x8_cavlc[i] = zigzag_scan8x8[(i/4) + 16*(i%4)]
97 static const uint8_t zigzag_scan8x8_cavlc[64+1] = {
98 0 + 0 * 8, 1 + 1 * 8, 1 + 2 * 8, 2 + 2 * 8,
99 4 + 1 * 8, 0 + 5 * 8, 3 + 3 * 8, 7 + 0 * 8,
100 3 + 4 * 8, 1 + 7 * 8, 5 + 3 * 8, 6 + 3 * 8,
101 2 + 7 * 8, 6 + 4 * 8, 5 + 6 * 8, 7 + 5 * 8,
102 1 + 0 * 8, 2 + 0 * 8, 0 + 3 * 8, 3 + 1 * 8,
103 3 + 2 * 8, 0 + 6 * 8, 4 + 2 * 8, 6 + 1 * 8,
104 2 + 5 * 8, 2 + 6 * 8, 6 + 2 * 8, 5 + 4 * 8,
105 3 + 7 * 8, 7 + 3 * 8, 4 + 7 * 8, 7 + 6 * 8,
106 0 + 1 * 8, 3 + 0 * 8, 0 + 4 * 8, 4 + 0 * 8,
107 2 + 3 * 8, 1 + 5 * 8, 5 + 1 * 8, 5 + 2 * 8,
108 1 + 6 * 8, 3 + 5 * 8, 7 + 1 * 8, 4 + 5 * 8,
109 4 + 6 * 8, 7 + 4 * 8, 5 + 7 * 8, 6 + 7 * 8,
110 0 + 2 * 8, 2 + 1 * 8, 1 + 3 * 8, 5 + 0 * 8,
111 1 + 4 * 8, 2 + 4 * 8, 6 + 0 * 8, 4 + 3 * 8,
112 0 + 7 * 8, 4 + 4 * 8, 7 + 2 * 8, 3 + 6 * 8,
113 5 + 5 * 8, 6 + 5 * 8, 6 + 6 * 8, 7 + 7 * 8,
114 };
115
116 28976 static void release_unused_pictures(H264Context *h, int remove_current)
117 {
118 int i;
119
120 /* release non reference frames */
121
2/2
✓ Branch 0 taken 1043136 times.
✓ Branch 1 taken 28976 times.
1072112 for (i = 0; i < H264_MAX_PICTURE_COUNT; i++) {
122
6/6
✓ Branch 0 taken 145923 times.
✓ Branch 1 taken 897213 times.
✓ Branch 2 taken 25351 times.
✓ Branch 3 taken 120572 times.
✓ Branch 4 taken 2610 times.
✓ Branch 5 taken 22741 times.
1043136 if (h->DPB[i].f->buf[0] && !h->DPB[i].reference &&
123
2/2
✓ Branch 0 taken 723 times.
✓ Branch 1 taken 1887 times.
2610 (remove_current || &h->DPB[i] != h->cur_pic_ptr)) {
124 23464 ff_h264_unref_picture(&h->DPB[i]);
125 }
126 }
127 28976 }
128
129 37364 static int alloc_scratch_buffers(H264SliceContext *sl, int linesize)
130 {
131 37364 const H264Context *h = sl->h264;
132 37364 int alloc_size = FFALIGN(FFABS(linesize) + 32, 32);
133
134 37364 av_fast_malloc(&sl->bipred_scratchpad, &sl->bipred_scratchpad_allocated, 16 * 6 * alloc_size);
135 // edge emu needs blocksize + filter length - 1
136 // (= 21x21 for H.264)
137 37364 av_fast_malloc(&sl->edge_emu_buffer, &sl->edge_emu_buffer_allocated, alloc_size * 2 * 21);
138
139 37364 av_fast_mallocz(&sl->top_borders[0], &sl->top_borders_allocated[0],
140 37364 h->mb_width * 16 * 3 * sizeof(uint8_t) * 2);
141 37364 av_fast_mallocz(&sl->top_borders[1], &sl->top_borders_allocated[1],
142 37364 h->mb_width * 16 * 3 * sizeof(uint8_t) * 2);
143
144
2/4
✓ Branch 0 taken 37364 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 37364 times.
✗ Branch 3 not taken.
37364 if (!sl->bipred_scratchpad || !sl->edge_emu_buffer ||
145
2/4
✓ Branch 0 taken 37364 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 37364 times.
37364 !sl->top_borders[0] || !sl->top_borders[1]) {
146 av_freep(&sl->bipred_scratchpad);
147 av_freep(&sl->edge_emu_buffer);
148 av_freep(&sl->top_borders[0]);
149 av_freep(&sl->top_borders[1]);
150
151 sl->bipred_scratchpad_allocated = 0;
152 sl->edge_emu_buffer_allocated = 0;
153 sl->top_borders_allocated[0] = 0;
154 sl->top_borders_allocated[1] = 0;
155 return AVERROR(ENOMEM);
156 }
157
158 37364 return 0;
159 }
160
161 601 static int init_table_pools(H264Context *h)
162 {
163 601 const int big_mb_num = h->mb_stride * (h->mb_height + 1) + 1;
164 601 const int mb_array_size = h->mb_stride * h->mb_height;
165 601 const int b4_stride = h->mb_width * 4 + 1;
166 601 const int b4_array_size = b4_stride * h->mb_height * 4;
167
168 601 h->qscale_table_pool = ff_refstruct_pool_alloc(big_mb_num + h->mb_stride, 0);
169 601 h->mb_type_pool = ff_refstruct_pool_alloc((big_mb_num + h->mb_stride) *
170 sizeof(uint32_t), 0);
171 601 h->motion_val_pool = ff_refstruct_pool_alloc(2 * (b4_array_size + 4) *
172 sizeof(int16_t), 0);
173 601 h->ref_index_pool = ff_refstruct_pool_alloc(4 * mb_array_size, 0);
174
175
3/6
✓ Branch 0 taken 601 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 601 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 601 times.
✗ Branch 5 not taken.
601 if (!h->qscale_table_pool || !h->mb_type_pool || !h->motion_val_pool ||
176
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 601 times.
601 !h->ref_index_pool) {
177 ff_refstruct_pool_uninit(&h->qscale_table_pool);
178 ff_refstruct_pool_uninit(&h->mb_type_pool);
179 ff_refstruct_pool_uninit(&h->motion_val_pool);
180 ff_refstruct_pool_uninit(&h->ref_index_pool);
181 return AVERROR(ENOMEM);
182 }
183
184 601 return 0;
185 }
186
187 25614 static int alloc_picture(H264Context *h, H264Picture *pic)
188 {
189 25614 int i, ret = 0;
190
191
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 25614 times.
25614 av_assert0(!pic->f->data[0]);
192
193 25614 pic->tf.f = pic->f;
194 25614 ret = ff_thread_get_ext_buffer(h->avctx, &pic->tf,
195 25614 pic->reference ? AV_GET_BUFFER_FLAG_REF : 0);
196
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 25614 times.
25614 if (ret < 0)
197 goto fail;
198
199
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 25614 times.
25614 if (pic->needs_fg) {
200 pic->f_grain->format = pic->f->format;
201 pic->f_grain->width = pic->f->width;
202 pic->f_grain->height = pic->f->height;
203 ret = ff_thread_get_buffer(h->avctx, pic->f_grain, 0);
204 if (ret < 0)
205 goto fail;
206 }
207
208 25614 ret = ff_hwaccel_frame_priv_alloc(h->avctx, &pic->hwaccel_picture_private);
209
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 25614 times.
25614 if (ret < 0)
210 goto fail;
211
212
2/2
✓ Branch 0 taken 42 times.
✓ Branch 1 taken 25572 times.
25614 if (h->decode_error_flags_pool) {
213 42 pic->decode_error_flags = ff_refstruct_pool_get(h->decode_error_flags_pool);
214
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 42 times.
42 if (!pic->decode_error_flags)
215 goto fail;
216 42 atomic_init(pic->decode_error_flags, 0);
217 }
218
219 if (CONFIG_GRAY && !h->avctx->hwaccel && h->flags & AV_CODEC_FLAG_GRAY && pic->f->data[2]) {
220 int h_chroma_shift, v_chroma_shift;
221 av_pix_fmt_get_chroma_sub_sample(pic->f->format,
222 &h_chroma_shift, &v_chroma_shift);
223
224 for(i=0; i<AV_CEIL_RSHIFT(pic->f->height, v_chroma_shift); i++) {
225 memset(pic->f->data[1] + pic->f->linesize[1]*i,
226 0x80, AV_CEIL_RSHIFT(pic->f->width, h_chroma_shift));
227 memset(pic->f->data[2] + pic->f->linesize[2]*i,
228 0x80, AV_CEIL_RSHIFT(pic->f->width, h_chroma_shift));
229 }
230 }
231
232
2/2
✓ Branch 0 taken 601 times.
✓ Branch 1 taken 25013 times.
25614 if (!h->qscale_table_pool) {
233 601 ret = init_table_pools(h);
234
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 601 times.
601 if (ret < 0)
235 goto fail;
236 }
237
238 25614 pic->qscale_table_base = ff_refstruct_pool_get(h->qscale_table_pool);
239 25614 pic->mb_type_base = ff_refstruct_pool_get(h->mb_type_pool);
240
2/4
✓ Branch 0 taken 25614 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 25614 times.
25614 if (!pic->qscale_table_base || !pic->mb_type_base)
241 goto fail;
242
243 25614 pic->mb_type = pic->mb_type_base + 2 * h->mb_stride + 1;
244 25614 pic->qscale_table = pic->qscale_table_base + 2 * h->mb_stride + 1;
245
246
2/2
✓ Branch 0 taken 51228 times.
✓ Branch 1 taken 25614 times.
76842 for (i = 0; i < 2; i++) {
247 51228 pic->motion_val_base[i] = ff_refstruct_pool_get(h->motion_val_pool);
248 51228 pic->ref_index[i] = ff_refstruct_pool_get(h->ref_index_pool);
249
2/4
✓ Branch 0 taken 51228 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 51228 times.
51228 if (!pic->motion_val_base[i] || !pic->ref_index[i])
250 goto fail;
251
252 51228 pic->motion_val[i] = pic->motion_val_base[i] + 4;
253 }
254
255 25614 pic->pps = ff_refstruct_ref_c(h->ps.pps);
256
257 25614 pic->mb_width = h->mb_width;
258 25614 pic->mb_height = h->mb_height;
259 25614 pic->mb_stride = h->mb_stride;
260
261 25614 return 0;
262 fail:
263 ff_h264_unref_picture(pic);
264 return (ret < 0) ? ret : AVERROR(ENOMEM);
265 }
266
267 25614 static int find_unused_picture(const H264Context *h)
268 {
269 int i;
270
271
1/2
✓ Branch 0 taken 86989 times.
✗ Branch 1 not taken.
86989 for (i = 0; i < H264_MAX_PICTURE_COUNT; i++) {
272
2/2
✓ Branch 0 taken 25614 times.
✓ Branch 1 taken 61375 times.
86989 if (!h->DPB[i].f->buf[0])
273 25614 return i;
274 }
275 return AVERROR_INVALIDDATA;
276 }
277
278
279 #define IN_RANGE(a, b, size) (((void*)(a) >= (void*)(b)) && ((void*)(a) < (void*)((b) + (size))))
280
281 #define REBASE_PICTURE(pic, new_ctx, old_ctx) \
282 (((pic) && (pic) >= (old_ctx)->DPB && \
283 (pic) < (old_ctx)->DPB + H264_MAX_PICTURE_COUNT) ? \
284 &(new_ctx)->DPB[(pic) - (old_ctx)->DPB] : NULL)
285
286 186 static void copy_picture_range(H264Picture **to, H264Picture *const *from, int count,
287 H264Context *new_base, const H264Context *old_base)
288 {
289 int i;
290
291
2/2
✓ Branch 0 taken 5084 times.
✓ Branch 1 taken 186 times.
5270 for (i = 0; i < count; i++) {
292 av_assert1(!from[i] ||
293 IN_RANGE(from[i], old_base, 1) ||
294 IN_RANGE(from[i], old_base->DPB, H264_MAX_PICTURE_COUNT));
295
4/6
✓ Branch 0 taken 265 times.
✓ Branch 1 taken 4819 times.
✓ Branch 2 taken 265 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 265 times.
✗ Branch 5 not taken.
5084 to[i] = REBASE_PICTURE(from[i], new_base, old_base);
296 }
297 186 }
298
299 24 static void color_frame(AVFrame *frame, const int c[4])
300 {
301 24 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(frame->format);
302
303
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 24 times.
24 av_assert0(desc->flags & AV_PIX_FMT_FLAG_PLANAR);
304
305
2/2
✓ Branch 0 taken 72 times.
✓ Branch 1 taken 24 times.
96 for (int p = 0; p < desc->nb_components; p++) {
306 72 uint8_t *dst = frame->data[p];
307
4/4
✓ Branch 0 taken 48 times.
✓ Branch 1 taken 24 times.
✓ Branch 2 taken 24 times.
✓ Branch 3 taken 24 times.
72 int is_chroma = p == 1 || p == 2;
308
2/2
✓ Branch 0 taken 48 times.
✓ Branch 1 taken 24 times.
72 int bytes = is_chroma ? AV_CEIL_RSHIFT(frame->width, desc->log2_chroma_w) : frame->width;
309
2/2
✓ Branch 0 taken 48 times.
✓ Branch 1 taken 24 times.
72 int height = is_chroma ? AV_CEIL_RSHIFT(frame->height, desc->log2_chroma_h) : frame->height;
310
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 72 times.
72 if (desc->comp[0].depth >= 9) {
311 ((uint16_t*)dst)[0] = c[p];
312 av_memcpy_backptr(dst + 2, 2, bytes - 2);
313 dst += frame->linesize[p];
314 for (int y = 1; y < height; y++) {
315 memcpy(dst, frame->data[p], 2*bytes);
316 dst += frame->linesize[p];
317 }
318 } else {
319
2/2
✓ Branch 0 taken 30208 times.
✓ Branch 1 taken 72 times.
30280 for (int y = 0; y < height; y++) {
320 30208 memset(dst, c[p], bytes);
321 30208 dst += frame->linesize[p];
322 }
323 }
324 }
325 24 }
326
327 static int h264_slice_header_init(H264Context *h);
328
329 62 int ff_h264_update_thread_context(AVCodecContext *dst,
330 const AVCodecContext *src)
331 {
332 62 H264Context *h = dst->priv_data, *h1 = src->priv_data;
333 62 int inited = h->context_initialized, err = 0;
334 62 int need_reinit = 0;
335 int i, ret;
336
337
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 62 times.
62 if (dst == src)
338 return 0;
339
340
3/4
✓ Branch 0 taken 46 times.
✓ Branch 1 taken 16 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 46 times.
62 if (inited && !h1->ps.sps)
341 return AVERROR_INVALIDDATA;
342
343
2/2
✓ Branch 0 taken 46 times.
✓ Branch 1 taken 16 times.
62 if (inited &&
344
1/2
✓ Branch 0 taken 46 times.
✗ Branch 1 not taken.
46 (h->width != h1->width ||
345
1/2
✓ Branch 0 taken 46 times.
✗ Branch 1 not taken.
46 h->height != h1->height ||
346
1/2
✓ Branch 0 taken 46 times.
✗ Branch 1 not taken.
46 h->mb_width != h1->mb_width ||
347
1/2
✓ Branch 0 taken 46 times.
✗ Branch 1 not taken.
46 h->mb_height != h1->mb_height ||
348
1/2
✓ Branch 0 taken 46 times.
✗ Branch 1 not taken.
46 !h->ps.sps ||
349
1/2
✓ Branch 0 taken 46 times.
✗ Branch 1 not taken.
46 h->ps.sps->bit_depth_luma != h1->ps.sps->bit_depth_luma ||
350
1/2
✓ Branch 0 taken 46 times.
✗ Branch 1 not taken.
46 h->ps.sps->chroma_format_idc != h1->ps.sps->chroma_format_idc ||
351
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 46 times.
46 h->ps.sps->vui.matrix_coeffs != h1->ps.sps->vui.matrix_coeffs)) {
352 need_reinit = 1;
353 }
354
355 /* copy block_offset since frame_start may not be called */
356 62 memcpy(h->block_offset, h1->block_offset, sizeof(h->block_offset));
357
358 // SPS/PPS
359
2/2
✓ Branch 0 taken 1984 times.
✓ Branch 1 taken 62 times.
2046 for (int i = 0; i < FF_ARRAY_ELEMS(h->ps.sps_list); i++)
360 1984 ff_refstruct_replace(&h->ps.sps_list[i], h1->ps.sps_list[i]);
361
2/2
✓ Branch 0 taken 15872 times.
✓ Branch 1 taken 62 times.
15934 for (int i = 0; i < FF_ARRAY_ELEMS(h->ps.pps_list); i++)
362 15872 ff_refstruct_replace(&h->ps.pps_list[i], h1->ps.pps_list[i]);
363
364 62 ff_refstruct_replace(&h->ps.pps, h1->ps.pps);
365 62 h->ps.sps = h1->ps.sps;
366
367
3/4
✓ Branch 0 taken 62 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 16 times.
✓ Branch 3 taken 46 times.
62 if (need_reinit || !inited) {
368 16 h->width = h1->width;
369 16 h->height = h1->height;
370 16 h->mb_height = h1->mb_height;
371 16 h->mb_width = h1->mb_width;
372 16 h->mb_num = h1->mb_num;
373 16 h->mb_stride = h1->mb_stride;
374 16 h->b_stride = h1->b_stride;
375 16 h->x264_build = h1->x264_build;
376
377
2/4
✓ Branch 0 taken 16 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 16 times.
✗ Branch 3 not taken.
16 if (h->context_initialized || h1->context_initialized) {
378
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 16 times.
16 if ((err = h264_slice_header_init(h)) < 0) {
379 av_log(h->avctx, AV_LOG_ERROR, "h264_slice_header_init() failed");
380 return err;
381 }
382 }
383
384 /* copy block_offset since frame_start may not be called */
385 16 memcpy(h->block_offset, h1->block_offset, sizeof(h->block_offset));
386 }
387
388 62 h->width_from_caller = h1->width_from_caller;
389 62 h->height_from_caller = h1->height_from_caller;
390 62 h->coded_picture_number = h1->coded_picture_number;
391 62 h->first_field = h1->first_field;
392 62 h->picture_structure = h1->picture_structure;
393 62 h->mb_aff_frame = h1->mb_aff_frame;
394 62 h->droppable = h1->droppable;
395
396
2/2
✓ Branch 0 taken 2232 times.
✓ Branch 1 taken 62 times.
2294 for (i = 0; i < H264_MAX_PICTURE_COUNT; i++) {
397 2232 ret = ff_h264_replace_picture(&h->DPB[i], &h1->DPB[i]);
398
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2232 times.
2232 if (ret < 0)
399 return ret;
400 }
401
402
4/6
✓ Branch 0 taken 42 times.
✓ Branch 1 taken 20 times.
✓ Branch 2 taken 42 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 42 times.
✗ Branch 5 not taken.
62 h->cur_pic_ptr = REBASE_PICTURE(h1->cur_pic_ptr, h, h1);
403 62 ret = ff_h264_replace_picture(&h->cur_pic, &h1->cur_pic);
404
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 62 times.
62 if (ret < 0)
405 return ret;
406
407 62 h->enable_er = h1->enable_er;
408 62 h->workaround_bugs = h1->workaround_bugs;
409 62 h->droppable = h1->droppable;
410
411 // extradata/NAL handling
412 62 h->is_avc = h1->is_avc;
413 62 h->nal_length_size = h1->nal_length_size;
414
415 62 memcpy(&h->poc, &h1->poc, sizeof(h->poc));
416
417 62 memcpy(h->short_ref, h1->short_ref, sizeof(h->short_ref));
418 62 memcpy(h->long_ref, h1->long_ref, sizeof(h->long_ref));
419 62 memcpy(h->delayed_pic, h1->delayed_pic, sizeof(h->delayed_pic));
420 62 memcpy(h->last_pocs, h1->last_pocs, sizeof(h->last_pocs));
421
422 62 h->next_output_pic = h1->next_output_pic;
423 62 h->next_outputed_poc = h1->next_outputed_poc;
424 62 h->poc_offset = h1->poc_offset;
425
426 62 memcpy(h->mmco, h1->mmco, sizeof(h->mmco));
427 62 h->nb_mmco = h1->nb_mmco;
428 62 h->mmco_reset = h1->mmco_reset;
429 62 h->explicit_ref_marking = h1->explicit_ref_marking;
430 62 h->long_ref_count = h1->long_ref_count;
431 62 h->short_ref_count = h1->short_ref_count;
432
433 62 copy_picture_range(h->short_ref, h1->short_ref, 32, h, h1);
434 62 copy_picture_range(h->long_ref, h1->long_ref, 32, h, h1);
435 62 copy_picture_range(h->delayed_pic, h1->delayed_pic,
436 FF_ARRAY_ELEMS(h->delayed_pic), h, h1);
437
438 62 h->frame_recovered = h1->frame_recovered;
439
440 62 ret = ff_h264_sei_ctx_replace(&h->sei, &h1->sei);
441
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 62 times.
62 if (ret < 0)
442 return ret;
443
444 62 h->sei.common.unregistered.x264_build = h1->sei.common.unregistered.x264_build;
445 62 h->sei.common.mastering_display = h1->sei.common.mastering_display;
446 62 h->sei.common.content_light = h1->sei.common.content_light;
447
448
2/2
✓ Branch 0 taken 20 times.
✓ Branch 1 taken 42 times.
62 if (!h->cur_pic_ptr)
449 20 return 0;
450
451
2/2
✓ Branch 0 taken 27 times.
✓ Branch 1 taken 15 times.
42 if (!h->droppable) {
452 27 err = ff_h264_execute_ref_pic_marking(h);
453 27 h->poc.prev_poc_msb = h->poc.poc_msb;
454 27 h->poc.prev_poc_lsb = h->poc.poc_lsb;
455 }
456 42 h->poc.prev_frame_num_offset = h->poc.frame_num_offset;
457 42 h->poc.prev_frame_num = h->poc.frame_num;
458
459 42 h->recovery_frame = h1->recovery_frame;
460 42 h->non_gray = h1->non_gray;
461
462 42 return err;
463 }
464
465 50 int ff_h264_update_thread_context_for_user(AVCodecContext *dst,
466 const AVCodecContext *src)
467 {
468 50 H264Context *h = dst->priv_data;
469 50 const H264Context *h1 = src->priv_data;
470
471 50 h->is_avc = h1->is_avc;
472 50 h->nal_length_size = h1->nal_length_size;
473
474 50 return 0;
475 }
476
477 25614 static int h264_frame_start(H264Context *h)
478 {
479 H264Picture *pic;
480 int i, ret;
481 25614 const int pixel_shift = h->pixel_shift;
482
483
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 25614 times.
25614 if (!ff_thread_can_start_frame(h->avctx)) {
484 av_log(h->avctx, AV_LOG_ERROR, "Attempt to start a frame outside SETUP state\n");
485 return -1;
486 }
487
488 25614 release_unused_pictures(h, 1);
489 25614 h->cur_pic_ptr = NULL;
490
491 25614 i = find_unused_picture(h);
492
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 25614 times.
25614 if (i < 0) {
493 av_log(h->avctx, AV_LOG_ERROR, "no frame buffer available\n");
494 return i;
495 }
496 25614 pic = &h->DPB[i];
497
498
2/2
✓ Branch 0 taken 17668 times.
✓ Branch 1 taken 7946 times.
25614 pic->reference = h->droppable ? 0 : h->picture_structure;
499 #if FF_API_FRAME_PICTURE_NUMBER
500 FF_DISABLE_DEPRECATION_WARNINGS
501 25614 pic->f->coded_picture_number = h->coded_picture_number++;
502 FF_ENABLE_DEPRECATION_WARNINGS
503 #endif
504 25614 pic->field_picture = h->picture_structure != PICT_FRAME;
505 25614 pic->frame_num = h->poc.frame_num;
506 /*
507 * Zero key_frame here; IDR markings per slice in frame or fields are ORed
508 * in later.
509 * See decode_nal_units().
510 */
511 25614 pic->f->flags &= ~AV_FRAME_FLAG_KEY;
512 25614 pic->mmco_reset = 0;
513 25614 pic->recovered = 0;
514 25614 pic->invalid_gap = 0;
515 25614 pic->sei_recovery_frame_cnt = h->sei.recovery_point.recovery_frame_cnt;
516
517 25614 pic->f->pict_type = h->slice_ctx[0].slice_type;
518
519 25614 pic->f->crop_left = h->crop_left;
520 25614 pic->f->crop_right = h->crop_right;
521 25614 pic->f->crop_top = h->crop_top;
522 25614 pic->f->crop_bottom = h->crop_bottom;
523
524
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 25614 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
25614 pic->needs_fg = h->sei.common.film_grain_characteristics.present && !h->avctx->hwaccel &&
525 !(h->avctx->export_side_data & AV_CODEC_EXPORT_DATA_FILM_GRAIN);
526
527
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 25614 times.
25614 if ((ret = alloc_picture(h, pic)) < 0)
528 return ret;
529
530 25614 h->cur_pic_ptr = pic;
531 25614 ff_h264_unref_picture(&h->cur_pic);
532 if (CONFIG_ERROR_RESILIENCE) {
533 25614 ff_h264_set_erpic(&h->er.cur_pic, NULL);
534 }
535
536
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 25614 times.
25614 if ((ret = ff_h264_ref_picture(&h->cur_pic, h->cur_pic_ptr)) < 0)
537 return ret;
538
539
2/2
✓ Branch 0 taken 25914 times.
✓ Branch 1 taken 25614 times.
51528 for (i = 0; i < h->nb_slice_ctx; i++) {
540 25914 h->slice_ctx[i].linesize = h->cur_pic_ptr->f->linesize[0];
541 25914 h->slice_ctx[i].uvlinesize = h->cur_pic_ptr->f->linesize[1];
542 }
543
544
2/2
✓ Branch 0 taken 25314 times.
✓ Branch 1 taken 300 times.
25614 if (CONFIG_ERROR_RESILIENCE && h->enable_er) {
545 25314 ff_er_frame_start(&h->er);
546 25314 ff_h264_set_erpic(&h->er.last_pic, NULL);
547 25314 ff_h264_set_erpic(&h->er.next_pic, NULL);
548 }
549
550
2/2
✓ Branch 0 taken 409824 times.
✓ Branch 1 taken 25614 times.
435438 for (i = 0; i < 16; i++) {
551 409824 h->block_offset[i] = (4 * ((scan8[i] - scan8[0]) & 7) << pixel_shift) + 4 * pic->f->linesize[0] * ((scan8[i] - scan8[0]) >> 3);
552 409824 h->block_offset[48 + i] = (4 * ((scan8[i] - scan8[0]) & 7) << pixel_shift) + 8 * pic->f->linesize[0] * ((scan8[i] - scan8[0]) >> 3);
553 }
554
2/2
✓ Branch 0 taken 409824 times.
✓ Branch 1 taken 25614 times.
435438 for (i = 0; i < 16; i++) {
555 409824 h->block_offset[16 + i] =
556 409824 h->block_offset[32 + i] = (4 * ((scan8[i] - scan8[0]) & 7) << pixel_shift) + 4 * pic->f->linesize[1] * ((scan8[i] - scan8[0]) >> 3);
557 409824 h->block_offset[48 + 16 + i] =
558 409824 h->block_offset[48 + 32 + i] = (4 * ((scan8[i] - scan8[0]) & 7) << pixel_shift) + 8 * pic->f->linesize[1] * ((scan8[i] - scan8[0]) >> 3);
559 }
560
561 /* We mark the current picture as non-reference after allocating it, so
562 * that if we break out due to an error it can be released automatically
563 * in the next ff_mpv_frame_start().
564 */
565 25614 h->cur_pic_ptr->reference = 0;
566
567 25614 h->cur_pic_ptr->field_poc[0] = h->cur_pic_ptr->field_poc[1] = INT_MAX;
568
569 25614 h->next_output_pic = NULL;
570
571 25614 h->postpone_filter = 0;
572
573
4/4
✓ Branch 0 taken 2863 times.
✓ Branch 1 taken 22751 times.
✓ Branch 2 taken 2355 times.
✓ Branch 3 taken 508 times.
25614 h->mb_aff_frame = h->ps.sps->mb_aff && (h->picture_structure == PICT_FRAME);
574
575
2/2
✓ Branch 0 taken 2064 times.
✓ Branch 1 taken 23550 times.
25614 if (h->sei.common.unregistered.x264_build >= 0)
576 2064 h->x264_build = h->sei.common.unregistered.x264_build;
577
578 assert(h->cur_pic_ptr->long_ref == 0);
579
580 25614 return 0;
581 }
582
583 12578836 static av_always_inline void backup_mb_border(const H264Context *h, H264SliceContext *sl,
584 const uint8_t *src_y,
585 const uint8_t *src_cb, const uint8_t *src_cr,
586 int linesize, int uvlinesize,
587 int simple)
588 {
589 uint8_t *top_border;
590 12578836 int top_idx = 1;
591 12578836 const int pixel_shift = h->pixel_shift;
592 12578836 int chroma444 = CHROMA444(h);
593 12578836 int chroma422 = CHROMA422(h);
594
595 12578836 src_y -= linesize;
596 12578836 src_cb -= uvlinesize;
597 12578836 src_cr -= uvlinesize;
598
599
3/4
✓ Branch 0 taken 12578836 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2022816 times.
✓ Branch 3 taken 10556020 times.
12578836 if (!simple && FRAME_MBAFF(h)) {
600
2/2
✓ Branch 0 taken 1011408 times.
✓ Branch 1 taken 1011408 times.
2022816 if (sl->mb_y & 1) {
601
2/2
✓ Branch 0 taken 747670 times.
✓ Branch 1 taken 263738 times.
1011408 if (!MB_MBAFF(sl)) {
602 747670 top_border = sl->top_borders[0][sl->mb_x];
603 747670 AV_COPY128(top_border, src_y + 15 * linesize);
604
2/2
✓ Branch 0 taken 213139 times.
✓ Branch 1 taken 534531 times.
747670 if (pixel_shift)
605 213139 AV_COPY128(top_border + 16, src_y + 15 * linesize + 16);
606 if (simple || !CONFIG_GRAY || !(h->flags & AV_CODEC_FLAG_GRAY)) {
607
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 747670 times.
747670 if (chroma444) {
608 if (pixel_shift) {
609 AV_COPY128(top_border + 32, src_cb + 15 * uvlinesize);
610 AV_COPY128(top_border + 48, src_cb + 15 * uvlinesize + 16);
611 AV_COPY128(top_border + 64, src_cr + 15 * uvlinesize);
612 AV_COPY128(top_border + 80, src_cr + 15 * uvlinesize + 16);
613 } else {
614 AV_COPY128(top_border + 16, src_cb + 15 * uvlinesize);
615 AV_COPY128(top_border + 32, src_cr + 15 * uvlinesize);
616 }
617
2/2
✓ Branch 0 taken 106510 times.
✓ Branch 1 taken 641160 times.
747670 } else if (chroma422) {
618
1/2
✓ Branch 0 taken 106510 times.
✗ Branch 1 not taken.
106510 if (pixel_shift) {
619 106510 AV_COPY128(top_border + 32, src_cb + 15 * uvlinesize);
620 106510 AV_COPY128(top_border + 48, src_cr + 15 * uvlinesize);
621 } else {
622 AV_COPY64(top_border + 16, src_cb + 15 * uvlinesize);
623 AV_COPY64(top_border + 24, src_cr + 15 * uvlinesize);
624 }
625 } else {
626
2/2
✓ Branch 0 taken 106629 times.
✓ Branch 1 taken 534531 times.
641160 if (pixel_shift) {
627 106629 AV_COPY128(top_border + 32, src_cb + 7 * uvlinesize);
628 106629 AV_COPY128(top_border + 48, src_cr + 7 * uvlinesize);
629 } else {
630 534531 AV_COPY64(top_border + 16, src_cb + 7 * uvlinesize);
631 534531 AV_COPY64(top_border + 24, src_cr + 7 * uvlinesize);
632 }
633 }
634 }
635 }
636
2/2
✓ Branch 0 taken 263738 times.
✓ Branch 1 taken 747670 times.
1011408 } else if (MB_MBAFF(sl)) {
637 263738 top_idx = 0;
638 } else
639 747670 return;
640 }
641
642 11831166 top_border = sl->top_borders[top_idx][sl->mb_x];
643 /* There are two lines saved, the line above the top macroblock
644 * of a pair, and the line above the bottom macroblock. */
645 11831166 AV_COPY128(top_border, src_y + 16 * linesize);
646
2/2
✓ Branch 0 taken 1381381 times.
✓ Branch 1 taken 10449785 times.
11831166 if (pixel_shift)
647 1381381 AV_COPY128(top_border + 16, src_y + 16 * linesize + 16);
648
649 if (simple || !CONFIG_GRAY || !(h->flags & AV_CODEC_FLAG_GRAY)) {
650
2/2
✓ Branch 0 taken 80328 times.
✓ Branch 1 taken 11750838 times.
11831166 if (chroma444) {
651
2/2
✓ Branch 0 taken 19800 times.
✓ Branch 1 taken 60528 times.
80328 if (pixel_shift) {
652 19800 AV_COPY128(top_border + 32, src_cb + 16 * linesize);
653 19800 AV_COPY128(top_border + 48, src_cb + 16 * linesize + 16);
654 19800 AV_COPY128(top_border + 64, src_cr + 16 * linesize);
655 19800 AV_COPY128(top_border + 80, src_cr + 16 * linesize + 16);
656 } else {
657 60528 AV_COPY128(top_border + 16, src_cb + 16 * linesize);
658 60528 AV_COPY128(top_border + 32, src_cr + 16 * linesize);
659 }
660
2/2
✓ Branch 0 taken 588635 times.
✓ Branch 1 taken 11162203 times.
11750838 } else if (chroma422) {
661
2/2
✓ Branch 0 taken 584675 times.
✓ Branch 1 taken 3960 times.
588635 if (pixel_shift) {
662 584675 AV_COPY128(top_border + 32, src_cb + 16 * uvlinesize);
663 584675 AV_COPY128(top_border + 48, src_cr + 16 * uvlinesize);
664 } else {
665 3960 AV_COPY64(top_border + 16, src_cb + 16 * uvlinesize);
666 3960 AV_COPY64(top_border + 24, src_cr + 16 * uvlinesize);
667 }
668 } else {
669
2/2
✓ Branch 0 taken 776906 times.
✓ Branch 1 taken 10385297 times.
11162203 if (pixel_shift) {
670 776906 AV_COPY128(top_border + 32, src_cb + 8 * uvlinesize);
671 776906 AV_COPY128(top_border + 48, src_cr + 8 * uvlinesize);
672 } else {
673 10385297 AV_COPY64(top_border + 16, src_cb + 8 * uvlinesize);
674 10385297 AV_COPY64(top_border + 24, src_cr + 8 * uvlinesize);
675 }
676 }
677 }
678 }
679
680 /**
681 * Initialize implicit_weight table.
682 * @param field 0/1 initialize the weight for interlaced MBAFF
683 * -1 initializes the rest
684 */
685 3844 static void implicit_weight_table(const H264Context *h, H264SliceContext *sl, int field)
686 {
687 int ref0, ref1, i, cur_poc, ref_start, ref_count0, ref_count1;
688
689
2/2
✓ Branch 0 taken 7688 times.
✓ Branch 1 taken 3844 times.
11532 for (i = 0; i < 2; i++) {
690 7688 sl->pwt.luma_weight_flag[i] = 0;
691 7688 sl->pwt.chroma_weight_flag[i] = 0;
692 }
693
694
2/2
✓ Branch 0 taken 2130 times.
✓ Branch 1 taken 1714 times.
3844 if (field < 0) {
695
1/2
✓ Branch 0 taken 2130 times.
✗ Branch 1 not taken.
2130 if (h->picture_structure == PICT_FRAME) {
696 2130 cur_poc = h->cur_pic_ptr->poc;
697 } else {
698 cur_poc = h->cur_pic_ptr->field_poc[h->picture_structure - 1];
699 }
700
6/6
✓ Branch 0 taken 953 times.
✓ Branch 1 taken 1177 times.
✓ Branch 2 taken 643 times.
✓ Branch 3 taken 310 times.
✓ Branch 4 taken 633 times.
✓ Branch 5 taken 10 times.
2130 if (sl->ref_count[0] == 1 && sl->ref_count[1] == 1 && !FRAME_MBAFF(h) &&
701
2/2
✓ Branch 0 taken 416 times.
✓ Branch 1 taken 217 times.
633 sl->ref_list[0][0].poc + (int64_t)sl->ref_list[1][0].poc == 2LL * cur_poc) {
702 416 sl->pwt.use_weight = 0;
703 416 sl->pwt.use_weight_chroma = 0;
704 416 return;
705 }
706 1714 ref_start = 0;
707 1714 ref_count0 = sl->ref_count[0];
708 1714 ref_count1 = sl->ref_count[1];
709 } else {
710 1714 cur_poc = h->cur_pic_ptr->field_poc[field];
711 1714 ref_start = 16;
712 1714 ref_count0 = 16 + 2 * sl->ref_count[0];
713 1714 ref_count1 = 16 + 2 * sl->ref_count[1];
714 }
715
716 3428 sl->pwt.use_weight = 2;
717 3428 sl->pwt.use_weight_chroma = 2;
718 3428 sl->pwt.luma_log2_weight_denom = 5;
719 3428 sl->pwt.chroma_log2_weight_denom = 5;
720
721
2/2
✓ Branch 0 taken 11805 times.
✓ Branch 1 taken 3428 times.
15233 for (ref0 = ref_start; ref0 < ref_count0; ref0++) {
722 11805 int64_t poc0 = sl->ref_list[0][ref0].poc;
723
2/2
✓ Branch 0 taken 21932 times.
✓ Branch 1 taken 11805 times.
33737 for (ref1 = ref_start; ref1 < ref_count1; ref1++) {
724 21932 int w = 32;
725
2/4
✓ Branch 0 taken 21932 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 21932 times.
✗ Branch 3 not taken.
21932 if (!sl->ref_list[0][ref0].parent->long_ref && !sl->ref_list[1][ref1].parent->long_ref) {
726 21932 int poc1 = sl->ref_list[1][ref1].poc;
727 21932 int td = av_clip_int8(poc1 - poc0);
728
2/2
✓ Branch 0 taken 21865 times.
✓ Branch 1 taken 67 times.
21932 if (td) {
729 21865 int tb = av_clip_int8(cur_poc - poc0);
730 21865 int tx = (16384 + (FFABS(td) >> 1)) / td;
731 21865 int dist_scale_factor = (tb * tx + 32) >> 8;
732
3/4
✓ Branch 0 taken 21855 times.
✓ Branch 1 taken 10 times.
✓ Branch 2 taken 21855 times.
✗ Branch 3 not taken.
21865 if (dist_scale_factor >= -64 && dist_scale_factor <= 128)
733 21855 w = 64 - dist_scale_factor;
734 }
735 }
736
2/2
✓ Branch 0 taken 5996 times.
✓ Branch 1 taken 15936 times.
21932 if (field < 0) {
737 5996 sl->pwt.implicit_weight[ref0][ref1][0] =
738 5996 sl->pwt.implicit_weight[ref0][ref1][1] = w;
739 } else {
740 15936 sl->pwt.implicit_weight[ref0][ref1][field] = w;
741 }
742 }
743 }
744 }
745
746 /**
747 * initialize scan tables
748 */
749 601 static void init_scan_tables(H264Context *h)
750 {
751 int i;
752
2/2
✓ Branch 0 taken 9616 times.
✓ Branch 1 taken 601 times.
10217 for (i = 0; i < 16; i++) {
753 #define TRANSPOSE(x) ((x) >> 2) | (((x) << 2) & 0xF)
754 9616 h->zigzag_scan[i] = TRANSPOSE(ff_zigzag_scan[i]);
755 9616 h->field_scan[i] = TRANSPOSE(field_scan[i]);
756 #undef TRANSPOSE
757 }
758
2/2
✓ Branch 0 taken 38464 times.
✓ Branch 1 taken 601 times.
39065 for (i = 0; i < 64; i++) {
759 #define TRANSPOSE(x) ((x) >> 3) | (((x) & 7) << 3)
760 38464 h->zigzag_scan8x8[i] = TRANSPOSE(ff_zigzag_direct[i]);
761 38464 h->zigzag_scan8x8_cavlc[i] = TRANSPOSE(zigzag_scan8x8_cavlc[i]);
762 38464 h->field_scan8x8[i] = TRANSPOSE(field_scan8x8[i]);
763 38464 h->field_scan8x8_cavlc[i] = TRANSPOSE(field_scan8x8_cavlc[i]);
764 #undef TRANSPOSE
765 }
766
2/2
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 587 times.
601 if (h->ps.sps->transform_bypass) { // FIXME same ugly
767 14 memcpy(h->zigzag_scan_q0 , ff_zigzag_scan , sizeof(h->zigzag_scan_q0 ));
768 14 memcpy(h->zigzag_scan8x8_q0 , ff_zigzag_direct , sizeof(h->zigzag_scan8x8_q0 ));
769 14 memcpy(h->zigzag_scan8x8_cavlc_q0 , zigzag_scan8x8_cavlc , sizeof(h->zigzag_scan8x8_cavlc_q0));
770 14 memcpy(h->field_scan_q0 , field_scan , sizeof(h->field_scan_q0 ));
771 14 memcpy(h->field_scan8x8_q0 , field_scan8x8 , sizeof(h->field_scan8x8_q0 ));
772 14 memcpy(h->field_scan8x8_cavlc_q0 , field_scan8x8_cavlc , sizeof(h->field_scan8x8_cavlc_q0 ));
773 } else {
774 587 memcpy(h->zigzag_scan_q0 , h->zigzag_scan , sizeof(h->zigzag_scan_q0 ));
775 587 memcpy(h->zigzag_scan8x8_q0 , h->zigzag_scan8x8 , sizeof(h->zigzag_scan8x8_q0 ));
776 587 memcpy(h->zigzag_scan8x8_cavlc_q0 , h->zigzag_scan8x8_cavlc , sizeof(h->zigzag_scan8x8_cavlc_q0));
777 587 memcpy(h->field_scan_q0 , h->field_scan , sizeof(h->field_scan_q0 ));
778 587 memcpy(h->field_scan8x8_q0 , h->field_scan8x8 , sizeof(h->field_scan8x8_q0 ));
779 587 memcpy(h->field_scan8x8_cavlc_q0 , h->field_scan8x8_cavlc , sizeof(h->field_scan8x8_cavlc_q0 ));
780 }
781 601 }
782
783 28550 static enum AVPixelFormat get_pixel_format(H264Context *h, int force_callback)
784 {
785 #define HWACCEL_MAX (CONFIG_H264_DXVA2_HWACCEL + \
786 (CONFIG_H264_D3D11VA_HWACCEL * 2) + \
787 CONFIG_H264_D3D12VA_HWACCEL + \
788 CONFIG_H264_NVDEC_HWACCEL + \
789 CONFIG_H264_VAAPI_HWACCEL + \
790 CONFIG_H264_VIDEOTOOLBOX_HWACCEL + \
791 CONFIG_H264_VDPAU_HWACCEL + \
792 CONFIG_H264_VULKAN_HWACCEL)
793 28550 enum AVPixelFormat pix_fmts[HWACCEL_MAX + 2], *fmt = pix_fmts;
794
795
3/6
✓ Branch 0 taken 155 times.
✓ Branch 1 taken 372 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 28023 times.
✗ Branch 5 not taken.
28550 switch (h->ps.sps->bit_depth_luma) {
796 155 case 9:
797
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 155 times.
155 if (CHROMA444(h)) {
798 if (h->avctx->colorspace == AVCOL_SPC_RGB) {
799 *fmt++ = AV_PIX_FMT_GBRP9;
800 } else
801 *fmt++ = AV_PIX_FMT_YUV444P9;
802
2/2
✓ Branch 0 taken 52 times.
✓ Branch 1 taken 103 times.
155 } else if (CHROMA422(h))
803 52 *fmt++ = AV_PIX_FMT_YUV422P9;
804 else
805 103 *fmt++ = AV_PIX_FMT_YUV420P9;
806 155 break;
807 372 case 10:
808 #if CONFIG_H264_VIDEOTOOLBOX_HWACCEL
809 if (h->avctx->colorspace != AVCOL_SPC_RGB)
810 *fmt++ = AV_PIX_FMT_VIDEOTOOLBOX;
811 #endif
812 #if CONFIG_H264_VULKAN_HWACCEL
813 *fmt++ = AV_PIX_FMT_VULKAN;
814 #endif
815
2/2
✓ Branch 0 taken 51 times.
✓ Branch 1 taken 321 times.
372 if (CHROMA444(h)) {
816
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 51 times.
51 if (h->avctx->colorspace == AVCOL_SPC_RGB) {
817 *fmt++ = AV_PIX_FMT_GBRP10;
818 } else
819 51 *fmt++ = AV_PIX_FMT_YUV444P10;
820
2/2
✓ Branch 0 taken 164 times.
✓ Branch 1 taken 157 times.
321 } else if (CHROMA422(h))
821 164 *fmt++ = AV_PIX_FMT_YUV422P10;
822 else {
823 #if CONFIG_H264_VAAPI_HWACCEL
824 // Just add as candidate. Whether VAProfileH264High10 usable or
825 // not is decided by vaapi_decode_make_config() defined in FFmpeg
826 // and vaQueryCodingProfile() defined in libva.
827 157 *fmt++ = AV_PIX_FMT_VAAPI;
828 #endif
829 157 *fmt++ = AV_PIX_FMT_YUV420P10;
830 }
831 372 break;
832 case 12:
833 #if CONFIG_H264_VULKAN_HWACCEL
834 *fmt++ = AV_PIX_FMT_VULKAN;
835 #endif
836 if (CHROMA444(h)) {
837 if (h->avctx->colorspace == AVCOL_SPC_RGB) {
838 *fmt++ = AV_PIX_FMT_GBRP12;
839 } else
840 *fmt++ = AV_PIX_FMT_YUV444P12;
841 } else if (CHROMA422(h))
842 *fmt++ = AV_PIX_FMT_YUV422P12;
843 else
844 *fmt++ = AV_PIX_FMT_YUV420P12;
845 break;
846 case 14:
847 if (CHROMA444(h)) {
848 if (h->avctx->colorspace == AVCOL_SPC_RGB) {
849 *fmt++ = AV_PIX_FMT_GBRP14;
850 } else
851 *fmt++ = AV_PIX_FMT_YUV444P14;
852 } else if (CHROMA422(h))
853 *fmt++ = AV_PIX_FMT_YUV422P14;
854 else
855 *fmt++ = AV_PIX_FMT_YUV420P14;
856 break;
857 28023 case 8:
858 #if CONFIG_H264_VDPAU_HWACCEL
859 28023 *fmt++ = AV_PIX_FMT_VDPAU;
860 #endif
861 #if CONFIG_H264_VULKAN_HWACCEL
862 *fmt++ = AV_PIX_FMT_VULKAN;
863 #endif
864 #if CONFIG_H264_NVDEC_HWACCEL
865 *fmt++ = AV_PIX_FMT_CUDA;
866 #endif
867 #if CONFIG_H264_VIDEOTOOLBOX_HWACCEL
868 if (h->avctx->colorspace != AVCOL_SPC_RGB)
869 *fmt++ = AV_PIX_FMT_VIDEOTOOLBOX;
870 #endif
871
2/2
✓ Branch 0 taken 484 times.
✓ Branch 1 taken 27539 times.
28023 if (CHROMA444(h)) {
872
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 484 times.
484 if (h->avctx->colorspace == AVCOL_SPC_RGB)
873 *fmt++ = AV_PIX_FMT_GBRP;
874
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 482 times.
484 else if (h->avctx->color_range == AVCOL_RANGE_JPEG)
875 2 *fmt++ = AV_PIX_FMT_YUVJ444P;
876 else
877 482 *fmt++ = AV_PIX_FMT_YUV444P;
878
2/2
✓ Branch 0 taken 22 times.
✓ Branch 1 taken 27517 times.
27539 } else if (CHROMA422(h)) {
879
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 22 times.
22 if (h->avctx->color_range == AVCOL_RANGE_JPEG)
880 *fmt++ = AV_PIX_FMT_YUVJ422P;
881 else
882 22 *fmt++ = AV_PIX_FMT_YUV422P;
883 } else {
884 #if CONFIG_H264_DXVA2_HWACCEL
885 *fmt++ = AV_PIX_FMT_DXVA2_VLD;
886 #endif
887 #if CONFIG_H264_D3D11VA_HWACCEL
888 *fmt++ = AV_PIX_FMT_D3D11VA_VLD;
889 *fmt++ = AV_PIX_FMT_D3D11;
890 #endif
891 #if CONFIG_H264_D3D12VA_HWACCEL
892 *fmt++ = AV_PIX_FMT_D3D12;
893 #endif
894 #if CONFIG_H264_VAAPI_HWACCEL
895 27517 *fmt++ = AV_PIX_FMT_VAAPI;
896 #endif
897
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 27514 times.
27517 if (h->avctx->color_range == AVCOL_RANGE_JPEG)
898 3 *fmt++ = AV_PIX_FMT_YUVJ420P;
899 else
900 27514 *fmt++ = AV_PIX_FMT_YUV420P;
901 }
902 28023 break;
903 default:
904 av_log(h->avctx, AV_LOG_ERROR,
905 "Unsupported bit depth %d\n", h->ps.sps->bit_depth_luma);
906 return AVERROR_INVALIDDATA;
907 }
908
909 28550 *fmt = AV_PIX_FMT_NONE;
910
911
2/2
✓ Branch 0 taken 84247 times.
✓ Branch 1 taken 588 times.
84835 for (int i = 0; pix_fmts[i] != AV_PIX_FMT_NONE; i++)
912
4/4
✓ Branch 0 taken 28218 times.
✓ Branch 1 taken 56029 times.
✓ Branch 2 taken 27962 times.
✓ Branch 3 taken 256 times.
84247 if (pix_fmts[i] == h->avctx->pix_fmt && !force_callback)
913 27962 return pix_fmts[i];
914 588 return ff_get_format(h->avctx, pix_fmts);
915 }
916
917 /* export coded and cropped frame dimensions to AVCodecContext */
918 28291 static void init_dimensions(H264Context *h)
919 {
920 28291 const SPS *sps = h->ps.sps;
921 28291 int cr = sps->crop_right;
922 28291 int cl = sps->crop_left;
923 28291 int ct = sps->crop_top;
924 28291 int cb = sps->crop_bottom;
925 28291 int width = h->width - (cr + cl);
926 28291 int height = h->height - (ct + cb);
927
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 28291 times.
28291 av_assert0(sps->crop_right + sps->crop_left < (unsigned)h->width);
928
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 28291 times.
28291 av_assert0(sps->crop_top + sps->crop_bottom < (unsigned)h->height);
929
930 /* handle container cropping */
931
3/4
✓ Branch 0 taken 26050 times.
✓ Branch 1 taken 2241 times.
✓ Branch 2 taken 26050 times.
✗ Branch 3 not taken.
28291 if (h->width_from_caller > 0 && h->height_from_caller > 0 &&
932
3/4
✓ Branch 0 taken 26049 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 26049 times.
✗ Branch 3 not taken.
26050 !sps->crop_top && !sps->crop_left &&
933
2/2
✓ Branch 0 taken 26040 times.
✓ Branch 1 taken 9 times.
26049 FFALIGN(h->width_from_caller, 16) == FFALIGN(width, 16) &&
934
1/2
✓ Branch 0 taken 26040 times.
✗ Branch 1 not taken.
26040 FFALIGN(h->height_from_caller, 16) == FFALIGN(height, 16) &&
935
2/2
✓ Branch 0 taken 26039 times.
✓ Branch 1 taken 1 times.
26040 h->width_from_caller <= width &&
936
2/2
✓ Branch 0 taken 26037 times.
✓ Branch 1 taken 2 times.
26039 h->height_from_caller <= height) {
937 26037 width = h->width_from_caller;
938 26037 height = h->height_from_caller;
939 26037 cl = 0;
940 26037 ct = 0;
941 26037 cr = h->width - width;
942 26037 cb = h->height - height;
943 } else {
944 2254 h->width_from_caller = 0;
945 2254 h->height_from_caller = 0;
946 }
947
948 28291 h->avctx->coded_width = h->width;
949 28291 h->avctx->coded_height = h->height;
950 28291 h->avctx->width = width;
951 28291 h->avctx->height = height;
952 28291 h->crop_right = cr;
953 28291 h->crop_left = cl;
954 28291 h->crop_top = ct;
955 28291 h->crop_bottom = cb;
956 28291 }
957
958 601 static int h264_slice_header_init(H264Context *h)
959 {
960 601 const SPS *sps = h->ps.sps;
961 int i, ret;
962
963
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 601 times.
601 if (!sps) {
964 ret = AVERROR_INVALIDDATA;
965 goto fail;
966 }
967
968 601 ff_set_sar(h->avctx, sps->vui.sar);
969 601 av_pix_fmt_get_chroma_sub_sample(h->avctx->pix_fmt,
970 &h->chroma_x_shift, &h->chroma_y_shift);
971
972
2/2
✓ Branch 0 taken 183 times.
✓ Branch 1 taken 418 times.
601 if (sps->timing_info_present_flag) {
973 183 int64_t den = sps->time_scale;
974
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 183 times.
183 if (h->x264_build < 44U)
975 den *= 2;
976 183 av_reduce(&h->avctx->framerate.den, &h->avctx->framerate.num,
977 183 sps->num_units_in_tick * 2, den, 1 << 30);
978 }
979
980 601 ff_h264_free_tables(h);
981
982 601 h->first_field = 0;
983 601 h->prev_interlaced_frame = 1;
984
985 601 init_scan_tables(h);
986 601 ret = ff_h264_alloc_tables(h);
987
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 601 times.
601 if (ret < 0) {
988 av_log(h->avctx, AV_LOG_ERROR, "Could not allocate memory\n");
989 goto fail;
990 }
991
992
2/4
✓ Branch 0 taken 601 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 601 times.
✗ Branch 3 not taken.
601 if (sps->bit_depth_luma < 8 || sps->bit_depth_luma > 14 ||
993
2/4
✓ Branch 0 taken 601 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 601 times.
601 sps->bit_depth_luma == 11 || sps->bit_depth_luma == 13
994 ) {
995 av_log(h->avctx, AV_LOG_ERROR, "Unsupported bit depth %d\n",
996 sps->bit_depth_luma);
997 ret = AVERROR_INVALIDDATA;
998 goto fail;
999 }
1000
1001 601 h->cur_bit_depth_luma =
1002 601 h->avctx->bits_per_raw_sample = sps->bit_depth_luma;
1003 601 h->cur_chroma_format_idc = sps->chroma_format_idc;
1004 601 h->pixel_shift = sps->bit_depth_luma > 8;
1005 601 h->chroma_format_idc = sps->chroma_format_idc;
1006 601 h->bit_depth_luma = sps->bit_depth_luma;
1007
1008 601 ff_h264dsp_init(&h->h264dsp, sps->bit_depth_luma,
1009 601 sps->chroma_format_idc);
1010 601 ff_h264chroma_init(&h->h264chroma, sps->bit_depth_chroma);
1011 601 ff_h264qpel_init(&h->h264qpel, sps->bit_depth_luma);
1012 601 ff_h264_pred_init(&h->hpc, AV_CODEC_ID_H264, sps->bit_depth_luma,
1013 601 sps->chroma_format_idc);
1014 601 ff_videodsp_init(&h->vdsp, sps->bit_depth_luma);
1015
1016
2/2
✓ Branch 0 taken 600 times.
✓ Branch 1 taken 1 times.
601 if (!HAVE_THREADS || !(h->avctx->active_thread_type & FF_THREAD_SLICE)) {
1017 600 ff_h264_slice_context_init(h, &h->slice_ctx[0]);
1018 } else {
1019
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 times.
3 for (i = 0; i < h->nb_slice_ctx; i++) {
1020 2 H264SliceContext *sl = &h->slice_ctx[i];
1021
1022 2 sl->h264 = h;
1023 2 sl->intra4x4_pred_mode = h->intra4x4_pred_mode + i * 8 * 2 * h->mb_stride;
1024 2 sl->mvd_table[0] = h->mvd_table[0] + i * 8 * 2 * h->mb_stride;
1025 2 sl->mvd_table[1] = h->mvd_table[1] + i * 8 * 2 * h->mb_stride;
1026
1027 2 ff_h264_slice_context_init(h, sl);
1028 }
1029 }
1030
1031 601 h->context_initialized = 1;
1032
1033 601 return 0;
1034 fail:
1035 ff_h264_free_tables(h);
1036 h->context_initialized = 0;
1037 return ret;
1038 }
1039
1040 55930 static enum AVPixelFormat non_j_pixfmt(enum AVPixelFormat a)
1041 {
1042
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 55928 times.
55930 switch (a) {
1043 2 case AV_PIX_FMT_YUVJ420P: return AV_PIX_FMT_YUV420P;
1044 case AV_PIX_FMT_YUVJ422P: return AV_PIX_FMT_YUV422P;
1045 case AV_PIX_FMT_YUVJ444P: return AV_PIX_FMT_YUV444P;
1046 55928 default:
1047 55928 return a;
1048 }
1049 }
1050
1051 28291 static int h264_init_ps(H264Context *h, const H264SliceContext *sl, int first_slice)
1052 {
1053 const SPS *sps;
1054 28291 int needs_reinit = 0, must_reinit, ret;
1055
1056
2/2
✓ Branch 0 taken 28097 times.
✓ Branch 1 taken 194 times.
28291 if (first_slice)
1057 28097 ff_refstruct_replace(&h->ps.pps, h->ps.pps_list[sl->pps_id]);
1058
1059
2/2
✓ Branch 0 taken 587 times.
✓ Branch 1 taken 27704 times.
28291 if (h->ps.sps != h->ps.pps->sps) {
1060 587 h->ps.sps = h->ps.pps->sps;
1061
1062
2/2
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 580 times.
587 if (h->mb_width != h->ps.sps->mb_width ||
1063
1/2
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
7 h->mb_height != h->ps.sps->mb_height ||
1064
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 1 times.
7 h->cur_bit_depth_luma != h->ps.sps->bit_depth_luma ||
1065
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 5 times.
6 h->cur_chroma_format_idc != h->ps.sps->chroma_format_idc
1066 )
1067 582 needs_reinit = 1;
1068
1069
2/2
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 576 times.
587 if (h->bit_depth_luma != h->ps.sps->bit_depth_luma ||
1070
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 10 times.
11 h->chroma_format_idc != h->ps.sps->chroma_format_idc)
1071 577 needs_reinit = 1;
1072 }
1073 28291 sps = h->ps.sps;
1074
1075
2/2
✓ Branch 0 taken 27714 times.
✓ Branch 1 taken 577 times.
56005 must_reinit = (h->context_initialized &&
1076
2/2
✓ Branch 0 taken 27708 times.
✓ Branch 1 taken 6 times.
27714 ( 16*sps->mb_width != h->avctx->coded_width
1077
1/2
✓ Branch 0 taken 27708 times.
✗ Branch 1 not taken.
27708 || 16*sps->mb_height != h->avctx->coded_height
1078
2/2
✓ Branch 0 taken 27707 times.
✓ Branch 1 taken 1 times.
27708 || h->cur_bit_depth_luma != sps->bit_depth_luma
1079
2/2
✓ Branch 0 taken 27706 times.
✓ Branch 1 taken 1 times.
27707 || h->cur_chroma_format_idc != sps->chroma_format_idc
1080
1/2
✓ Branch 0 taken 27706 times.
✗ Branch 1 not taken.
27706 || h->mb_width != sps->mb_width
1081
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 27706 times.
27706 || h->mb_height != sps->mb_height
1082 ));
1083
2/2
✓ Branch 0 taken 27965 times.
✓ Branch 1 taken 326 times.
28291 if (h->avctx->pix_fmt == AV_PIX_FMT_NONE
1084
2/2
✓ Branch 3 taken 3 times.
✓ Branch 4 taken 27962 times.
27965 || (non_j_pixfmt(h->avctx->pix_fmt) != non_j_pixfmt(get_pixel_format(h, 0))))
1085 329 must_reinit = 1;
1086
1087
4/4
✓ Branch 0 taken 28097 times.
✓ Branch 1 taken 194 times.
✓ Branch 3 taken 71 times.
✓ Branch 4 taken 28026 times.
28291 if (first_slice && av_cmp_q(sps->vui.sar, h->avctx->sample_aspect_ratio))
1088 71 must_reinit = 1;
1089
1090
1/2
✓ Branch 0 taken 28291 times.
✗ Branch 1 not taken.
28291 if (!h->setup_finished) {
1091 28291 h->avctx->profile = ff_h264_get_profile(sps);
1092 28291 h->avctx->level = sps->level_idc;
1093 28291 h->avctx->refs = sps->ref_frame_count;
1094
1095 28291 h->mb_width = sps->mb_width;
1096 28291 h->mb_height = sps->mb_height;
1097 28291 h->mb_num = h->mb_width * h->mb_height;
1098 28291 h->mb_stride = h->mb_width + 1;
1099
1100 28291 h->b_stride = h->mb_width * 4;
1101
1102 28291 h->chroma_y_shift = sps->chroma_format_idc <= 1; // 400 uses yuv420p
1103
1104 28291 h->width = 16 * h->mb_width;
1105 28291 h->height = 16 * h->mb_height;
1106
1107 28291 init_dimensions(h);
1108
1109
2/2
✓ Branch 0 taken 1287 times.
✓ Branch 1 taken 27004 times.
28291 if (sps->vui.video_signal_type_present_flag) {
1110 2574 h->avctx->color_range = sps->vui.video_full_range_flag > 0 ? AVCOL_RANGE_JPEG
1111
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1285 times.
1287 : AVCOL_RANGE_MPEG;
1112
2/2
✓ Branch 0 taken 1189 times.
✓ Branch 1 taken 98 times.
1287 if (sps->vui.colour_description_present_flag) {
1113
2/2
✓ Branch 0 taken 19 times.
✓ Branch 1 taken 1170 times.
1189 if (h->avctx->colorspace != sps->vui.matrix_coeffs)
1114 19 needs_reinit = 1;
1115 1189 h->avctx->color_primaries = sps->vui.colour_primaries;
1116 1189 h->avctx->color_trc = sps->vui.transfer_characteristics;
1117 1189 h->avctx->colorspace = sps->vui.matrix_coeffs;
1118 }
1119 }
1120
1121
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 28291 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
28291 if (h->sei.common.alternative_transfer.present &&
1122 av_color_transfer_name(h->sei.common.alternative_transfer.preferred_transfer_characteristics) &&
1123 h->sei.common.alternative_transfer.preferred_transfer_characteristics != AVCOL_TRC_UNSPECIFIED) {
1124 h->avctx->color_trc = h->sei.common.alternative_transfer.preferred_transfer_characteristics;
1125 }
1126 }
1127 28291 h->avctx->chroma_sample_location = sps->vui.chroma_location;
1128
1129
5/6
✓ Branch 0 taken 27714 times.
✓ Branch 1 taken 577 times.
✓ Branch 2 taken 27706 times.
✓ Branch 3 taken 8 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 27706 times.
28291 if (!h->context_initialized || must_reinit || needs_reinit) {
1130 585 int flush_changes = h->context_initialized;
1131 585 h->context_initialized = 0;
1132
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 585 times.
585 if (sl != h->slice_ctx) {
1133 av_log(h->avctx, AV_LOG_ERROR,
1134 "changing width %d -> %d / height %d -> %d on "
1135 "slice %d\n",
1136 h->width, h->avctx->coded_width,
1137 h->height, h->avctx->coded_height,
1138 h->current_slice + 1);
1139 return AVERROR_INVALIDDATA;
1140 }
1141
1142 av_assert1(first_slice);
1143
1144
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 577 times.
585 if (flush_changes)
1145 8 ff_h264_flush_change(h);
1146
1147
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 585 times.
585 if ((ret = get_pixel_format(h, 1)) < 0)
1148 return ret;
1149 585 h->avctx->pix_fmt = ret;
1150
1151 585 av_log(h->avctx, AV_LOG_VERBOSE, "Reinit context to %dx%d, "
1152 585 "pix_fmt: %s\n", h->width, h->height, av_get_pix_fmt_name(h->avctx->pix_fmt));
1153
1154
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 585 times.
585 if ((ret = h264_slice_header_init(h)) < 0) {
1155 av_log(h->avctx, AV_LOG_ERROR,
1156 "h264_slice_header_init() failed\n");
1157 return ret;
1158 }
1159 }
1160
1161 28291 return 0;
1162 }
1163
1164 24924 static int h264_export_frame_props(H264Context *h)
1165 {
1166 24924 const SPS *sps = h->ps.sps;
1167 24924 H264Picture *cur = h->cur_pic_ptr;
1168 24924 AVFrame *out = cur->f;
1169 24924 int interlaced_frame = 0, top_field_first = 0;
1170 int ret;
1171
1172 24924 out->flags &= ~AV_FRAME_FLAG_INTERLACED;
1173 24924 out->repeat_pict = 0;
1174
1175 /* Signal interlacing information externally. */
1176 /* Prioritize picture timing SEI information over used
1177 * decoding process if it exists. */
1178
2/2
✓ Branch 0 taken 1428 times.
✓ Branch 1 taken 23496 times.
24924 if (h->sei.picture_timing.present) {
1179 1428 int ret = ff_h264_sei_process_picture_timing(&h->sei.picture_timing, sps,
1180 1428 h->avctx);
1181
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1428 times.
1428 if (ret < 0) {
1182 av_log(h->avctx, AV_LOG_ERROR, "Error processing a picture timing SEI\n");
1183 if (h->avctx->err_recognition & AV_EF_EXPLODE)
1184 return ret;
1185 h->sei.picture_timing.present = 0;
1186 }
1187 }
1188
1189
4/4
✓ Branch 0 taken 1506 times.
✓ Branch 1 taken 23418 times.
✓ Branch 2 taken 1420 times.
✓ Branch 3 taken 86 times.
26344 if (sps->pic_struct_present_flag && h->sei.picture_timing.present) {
1190 1420 const H264SEIPictureTiming *pt = &h->sei.picture_timing;
1191
4/7
✓ Branch 0 taken 132 times.
✓ Branch 1 taken 377 times.
✓ Branch 2 taken 744 times.
✓ Branch 3 taken 167 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
1420 switch (pt->pic_struct) {
1192 132 case H264_SEI_PIC_STRUCT_FRAME:
1193 132 break;
1194 377 case H264_SEI_PIC_STRUCT_TOP_FIELD:
1195 case H264_SEI_PIC_STRUCT_BOTTOM_FIELD:
1196 377 interlaced_frame = 1;
1197 377 break;
1198 744 case H264_SEI_PIC_STRUCT_TOP_BOTTOM:
1199 case H264_SEI_PIC_STRUCT_BOTTOM_TOP:
1200
3/4
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 740 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 4 times.
744 if (FIELD_OR_MBAFF_PICTURE(h))
1201 740 interlaced_frame = 1;
1202 else
1203 // try to flag soft telecine progressive
1204 4 interlaced_frame = !!h->prev_interlaced_frame;
1205 744 break;
1206 167 case H264_SEI_PIC_STRUCT_TOP_BOTTOM_TOP:
1207 case H264_SEI_PIC_STRUCT_BOTTOM_TOP_BOTTOM:
1208 /* Signal the possibility of telecined film externally
1209 * (pic_struct 5,6). From these hints, let the applications
1210 * decide if they apply deinterlacing. */
1211 167 out->repeat_pict = 1;
1212 167 break;
1213 case H264_SEI_PIC_STRUCT_FRAME_DOUBLING:
1214 out->repeat_pict = 2;
1215 break;
1216 case H264_SEI_PIC_STRUCT_FRAME_TRIPLING:
1217 out->repeat_pict = 4;
1218 break;
1219 }
1220
1221
2/2
✓ Branch 0 taken 652 times.
✓ Branch 1 taken 768 times.
1420 if ((pt->ct_type & 3) &&
1222
2/2
✓ Branch 0 taken 488 times.
✓ Branch 1 taken 164 times.
652 pt->pic_struct <= H264_SEI_PIC_STRUCT_BOTTOM_TOP)
1223 488 interlaced_frame = ((pt->ct_type & (1 << 1)) != 0);
1224 } else {
1225 /* Derive interlacing flag from used decoding process. */
1226
4/4
✓ Branch 0 taken 21917 times.
✓ Branch 1 taken 1587 times.
✓ Branch 2 taken 2821 times.
✓ Branch 3 taken 19096 times.
23504 interlaced_frame = !!FIELD_OR_MBAFF_PICTURE(h);
1227 }
1228 24924 h->prev_interlaced_frame = interlaced_frame;
1229
1230
2/2
✓ Branch 0 taken 3338 times.
✓ Branch 1 taken 21586 times.
24924 if (cur->field_poc[0] != cur->field_poc[1]) {
1231 /* Derive top_field_first from field pocs. */
1232 3338 top_field_first = (cur->field_poc[0] < cur->field_poc[1]);
1233 } else {
1234
4/4
✓ Branch 0 taken 225 times.
✓ Branch 1 taken 21361 times.
✓ Branch 2 taken 139 times.
✓ Branch 3 taken 86 times.
21586 if (sps->pic_struct_present_flag && h->sei.picture_timing.present) {
1235 /* Use picture timing SEI information. Even if it is a
1236 * information of a past frame, better than nothing. */
1237
2/2
✓ Branch 0 taken 138 times.
✓ Branch 1 taken 1 times.
139 if (h->sei.picture_timing.pic_struct == H264_SEI_PIC_STRUCT_TOP_BOTTOM ||
1238
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 136 times.
138 h->sei.picture_timing.pic_struct == H264_SEI_PIC_STRUCT_TOP_BOTTOM_TOP)
1239 3 top_field_first = 1;
1240
2/2
✓ Branch 0 taken 2554 times.
✓ Branch 1 taken 18893 times.
21447 } else if (interlaced_frame) {
1241 /* Default to top field first when pic_struct_present_flag
1242 * is not set but interlaced frame detected */
1243 2554 top_field_first = 1;
1244 } // else
1245 /* Most likely progressive */
1246 }
1247
1248 24924 out->flags |= (AV_FRAME_FLAG_INTERLACED * interlaced_frame) |
1249 24924 (AV_FRAME_FLAG_TOP_FIELD_FIRST * top_field_first);
1250
1251 24924 ret = ff_h2645_sei_to_frame(out, &h->sei.common, AV_CODEC_ID_H264, h->avctx,
1252 24924 &sps->vui, sps->bit_depth_luma, sps->bit_depth_chroma,
1253 24924 cur->poc + (unsigned)(h->poc_offset << 5));
1254
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 24924 times.
24924 if (ret < 0)
1255 return ret;
1256
1257
2/2
✓ Branch 0 taken 652 times.
✓ Branch 1 taken 24272 times.
24924 if (h->sei.picture_timing.timecode_cnt > 0) {
1258 uint32_t *tc_sd;
1259 char tcbuf[AV_TIMECODE_STR_SIZE];
1260
1261 652 AVFrameSideData *tcside = av_frame_new_side_data(out,
1262 AV_FRAME_DATA_S12M_TIMECODE,
1263 sizeof(uint32_t)*4);
1264
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 652 times.
652 if (!tcside)
1265 return AVERROR(ENOMEM);
1266
1267 652 tc_sd = (uint32_t*)tcside->data;
1268 652 tc_sd[0] = h->sei.picture_timing.timecode_cnt;
1269
1270
2/2
✓ Branch 0 taken 953 times.
✓ Branch 1 taken 652 times.
1605 for (int i = 0; i < tc_sd[0]; i++) {
1271 953 int drop = h->sei.picture_timing.timecode[i].dropframe;
1272 953 int hh = h->sei.picture_timing.timecode[i].hours;
1273 953 int mm = h->sei.picture_timing.timecode[i].minutes;
1274 953 int ss = h->sei.picture_timing.timecode[i].seconds;
1275 953 int ff = h->sei.picture_timing.timecode[i].frame;
1276
1277 953 tc_sd[i + 1] = av_timecode_get_smpte(h->avctx->framerate, drop, hh, mm, ss, ff);
1278 953 av_timecode_make_smpte_tc_string2(tcbuf, h->avctx->framerate, tc_sd[i + 1], 0, 0);
1279 953 av_dict_set(&out->metadata, "timecode", tcbuf, 0);
1280 }
1281 652 h->sei.picture_timing.timecode_cnt = 0;
1282 }
1283
1284 24924 return 0;
1285 }
1286
1287 24924 static int h264_select_output_frame(H264Context *h)
1288 {
1289 24924 const SPS *sps = h->ps.sps;
1290 24924 H264Picture *out = h->cur_pic_ptr;
1291 24924 H264Picture *cur = h->cur_pic_ptr;
1292 int i, pics, out_of_order, out_idx;
1293
1294 24924 cur->mmco_reset = h->mmco_reset;
1295 24924 h->mmco_reset = 0;
1296
1297
2/2
✓ Branch 0 taken 20019 times.
✓ Branch 1 taken 4905 times.
24924 if (sps->bitstream_restriction_flag ||
1298
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 20019 times.
20019 h->avctx->strict_std_compliance >= FF_COMPLIANCE_STRICT) {
1299 4905 h->avctx->has_b_frames = FFMAX(h->avctx->has_b_frames, sps->num_reorder_frames);
1300 }
1301
1302 408707 for (i = 0; 1; i++) {
1303
4/4
✓ Branch 0 taken 392792 times.
✓ Branch 1 taken 15915 times.
✓ Branch 2 taken 9009 times.
✓ Branch 3 taken 383783 times.
408707 if(i == H264_MAX_DPB_FRAMES || cur->poc < h->last_pocs[i]){
1304
1/2
✓ Branch 0 taken 24924 times.
✗ Branch 1 not taken.
24924 if(i)
1305 24924 h->last_pocs[i-1] = cur->poc;
1306 24924 break;
1307
2/2
✓ Branch 0 taken 358859 times.
✓ Branch 1 taken 24924 times.
383783 } else if(i) {
1308 358859 h->last_pocs[i-1]= h->last_pocs[i];
1309 }
1310 }
1311 24924 out_of_order = H264_MAX_DPB_FRAMES - i;
1312
2/2
✓ Branch 0 taken 16799 times.
✓ Branch 1 taken 8125 times.
24924 if( cur->f->pict_type == AV_PICTURE_TYPE_B
1313
4/4
✓ Branch 0 taken 15678 times.
✓ Branch 1 taken 1121 times.
✓ Branch 2 taken 5063 times.
✓ Branch 3 taken 10615 times.
16799 || (h->last_pocs[H264_MAX_DPB_FRAMES-2] > INT_MIN && h->last_pocs[H264_MAX_DPB_FRAMES-1] - (int64_t)h->last_pocs[H264_MAX_DPB_FRAMES-2] > 2))
1314 13188 out_of_order = FFMAX(out_of_order, 1);
1315
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 24924 times.
24924 if (out_of_order == H264_MAX_DPB_FRAMES) {
1316 av_log(h->avctx, AV_LOG_VERBOSE, "Invalid POC %d<%d\n", cur->poc, h->last_pocs[0]);
1317 for (i = 1; i < H264_MAX_DPB_FRAMES; i++)
1318 h->last_pocs[i] = INT_MIN;
1319 h->last_pocs[0] = cur->poc;
1320 cur->mmco_reset = 1;
1321
3/4
✓ Branch 0 taken 140 times.
✓ Branch 1 taken 24784 times.
✓ Branch 2 taken 140 times.
✗ Branch 3 not taken.
24924 } else if(h->avctx->has_b_frames < out_of_order && !sps->bitstream_restriction_flag){
1322
2/2
✓ Branch 0 taken 33 times.
✓ Branch 1 taken 107 times.
140 int loglevel = h->avctx->frame_num > 1 ? AV_LOG_WARNING : AV_LOG_VERBOSE;
1323 140 av_log(h->avctx, loglevel, "Increasing reorder buffer to %d\n", out_of_order);
1324 140 h->avctx->has_b_frames = out_of_order;
1325 }
1326
1327 24924 pics = 0;
1328
2/2
✓ Branch 0 taken 30542 times.
✓ Branch 1 taken 24924 times.
55466 while (h->delayed_pic[pics])
1329 30542 pics++;
1330
1331
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 24924 times.
24924 av_assert0(pics <= H264_MAX_DPB_FRAMES);
1332
1333 24924 h->delayed_pic[pics++] = cur;
1334
2/2
✓ Branch 0 taken 23449 times.
✓ Branch 1 taken 1475 times.
24924 if (cur->reference == 0)
1335 23449 cur->reference = DELAYED_PIC_REF;
1336
1337 24924 out = h->delayed_pic[0];
1338 24924 out_idx = 0;
1339 24924 for (i = 1; h->delayed_pic[i] &&
1340
4/4
✓ Branch 0 taken 28091 times.
✓ Branch 1 taken 24457 times.
✓ Branch 2 taken 27711 times.
✓ Branch 3 taken 380 times.
52548 !(h->delayed_pic[i]->f->flags & AV_FRAME_FLAG_KEY) &&
1341
2/2
✓ Branch 0 taken 27624 times.
✓ Branch 1 taken 87 times.
27711 !h->delayed_pic[i]->mmco_reset;
1342 27624 i++)
1343
2/2
✓ Branch 0 taken 10309 times.
✓ Branch 1 taken 17315 times.
27624 if (h->delayed_pic[i]->poc < out->poc) {
1344 10309 out = h->delayed_pic[i];
1345 10309 out_idx = i;
1346 }
1347
2/2
✓ Branch 0 taken 10258 times.
✓ Branch 1 taken 14666 times.
24924 if (h->avctx->has_b_frames == 0 &&
1348
4/4
✓ Branch 0 taken 9466 times.
✓ Branch 1 taken 792 times.
✓ Branch 2 taken 12 times.
✓ Branch 3 taken 9454 times.
10258 ((h->delayed_pic[0]->f->flags & AV_FRAME_FLAG_KEY) || h->delayed_pic[0]->mmco_reset))
1349 804 h->next_outputed_poc = INT_MIN;
1350 24924 out_of_order = out->poc < h->next_outputed_poc;
1351
1352
4/4
✓ Branch 0 taken 24811 times.
✓ Branch 1 taken 113 times.
✓ Branch 2 taken 24286 times.
✓ Branch 3 taken 525 times.
24924 if (out_of_order || pics > h->avctx->has_b_frames) {
1353 24399 out->reference &= ~DELAYED_PIC_REF;
1354
2/2
✓ Branch 0 taken 39545 times.
✓ Branch 1 taken 24399 times.
63944 for (i = out_idx; h->delayed_pic[i]; i++)
1355 39545 h->delayed_pic[i] = h->delayed_pic[i + 1];
1356 }
1357
4/4
✓ Branch 0 taken 24811 times.
✓ Branch 1 taken 113 times.
✓ Branch 2 taken 24286 times.
✓ Branch 3 taken 525 times.
24924 if (!out_of_order && pics > h->avctx->has_b_frames) {
1358 24286 h->next_output_pic = out;
1359
8/8
✓ Branch 0 taken 15498 times.
✓ Branch 1 taken 8788 times.
✓ Branch 2 taken 5241 times.
✓ Branch 3 taken 10257 times.
✓ Branch 4 taken 5157 times.
✓ Branch 5 taken 84 times.
✓ Branch 6 taken 21 times.
✓ Branch 7 taken 5136 times.
24286 if (out_idx == 0 && h->delayed_pic[0] && ((h->delayed_pic[0]->f->flags & AV_FRAME_FLAG_KEY) || h->delayed_pic[0]->mmco_reset)) {
1360 105 h->next_outputed_poc = INT_MIN;
1361 } else
1362 24181 h->next_outputed_poc = out->poc;
1363
1364 // We have reached an recovery point and all frames after it in
1365 // display order are "recovered".
1366 24286 h->frame_recovered |= out->recovered;
1367
1368 24286 out->recovered |= h->frame_recovered & FRAME_RECOVERED_SEI;
1369
1370
2/2
✓ Branch 0 taken 249 times.
✓ Branch 1 taken 24037 times.
24286 if (!out->recovered) {
1371
1/2
✓ Branch 0 taken 249 times.
✗ Branch 1 not taken.
249 if (!(h->avctx->flags & AV_CODEC_FLAG_OUTPUT_CORRUPT) &&
1372
1/2
✓ Branch 0 taken 249 times.
✗ Branch 1 not taken.
249 !(h->avctx->flags2 & AV_CODEC_FLAG2_SHOW_ALL)) {
1373 249 h->next_output_pic = NULL;
1374 } else {
1375 out->f->flags |= AV_FRAME_FLAG_CORRUPT;
1376 }
1377 }
1378 } else {
1379
2/2
✓ Branch 0 taken 113 times.
✓ Branch 1 taken 525 times.
638 av_log(h->avctx, AV_LOG_DEBUG, "no picture %s\n", out_of_order ? "ooo" : "");
1380 }
1381
1382 24924 return 0;
1383 }
1384
1385 /* This function is called right after decoding the slice header for a first
1386 * slice in a field (or a frame). It decides whether we are decoding a new frame
1387 * or a second field in a pair and does the necessary setup.
1388 */
1389 28291 static int h264_field_start(H264Context *h, const H264SliceContext *sl,
1390 const H2645NAL *nal, int first_slice)
1391 {
1392 int i;
1393 const SPS *sps;
1394
1395 int last_pic_structure, last_pic_droppable, ret;
1396
1397 28291 ret = h264_init_ps(h, sl, first_slice);
1398
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 28291 times.
28291 if (ret < 0)
1399 return ret;
1400
1401 28291 sps = h->ps.sps;
1402
1403
3/4
✓ Branch 0 taken 28291 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 5238 times.
✓ Branch 3 taken 23053 times.
28291 if (sps && sps->bitstream_restriction_flag &&
1404
2/2
✓ Branch 0 taken 29 times.
✓ Branch 1 taken 5209 times.
5238 h->avctx->has_b_frames < sps->num_reorder_frames) {
1405 29 h->avctx->has_b_frames = sps->num_reorder_frames;
1406 }
1407
1408 28291 last_pic_droppable = h->droppable;
1409 28291 last_pic_structure = h->picture_structure;
1410 28291 h->droppable = (nal->ref_idc == 0);
1411 28291 h->picture_structure = sl->picture_structure;
1412
1413 28291 h->poc.frame_num = sl->frame_num;
1414 28291 h->poc.poc_lsb = sl->poc_lsb;
1415 28291 h->poc.delta_poc_bottom = sl->delta_poc_bottom;
1416 28291 h->poc.delta_poc[0] = sl->delta_poc[0];
1417 28291 h->poc.delta_poc[1] = sl->delta_poc[1];
1418
1419
2/2
✓ Branch 0 taken 1077 times.
✓ Branch 1 taken 27214 times.
28291 if (nal->type == H264_NAL_IDR_SLICE)
1420 1077 h->poc_offset = sl->idr_pic_id;
1421
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 27214 times.
27214 else if (h->picture_intra_only)
1422 h->poc_offset = 0;
1423
1424 /* Shorten frame num gaps so we don't have to allocate reference
1425 * frames just to throw them away */
1426
2/2
✓ Branch 0 taken 16147 times.
✓ Branch 1 taken 12144 times.
28291 if (h->poc.frame_num != h->poc.prev_frame_num) {
1427 16147 int unwrap_prev_frame_num = h->poc.prev_frame_num;
1428 16147 int max_frame_num = 1 << sps->log2_max_frame_num;
1429
1430
2/2
✓ Branch 0 taken 193 times.
✓ Branch 1 taken 15954 times.
16147 if (unwrap_prev_frame_num > h->poc.frame_num)
1431 193 unwrap_prev_frame_num -= max_frame_num;
1432
1433
2/2
✓ Branch 0 taken 74 times.
✓ Branch 1 taken 16073 times.
16147 if ((h->poc.frame_num - unwrap_prev_frame_num) > sps->ref_frame_count) {
1434 74 unwrap_prev_frame_num = (h->poc.frame_num - sps->ref_frame_count) - 1;
1435
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 72 times.
74 if (unwrap_prev_frame_num < 0)
1436 2 unwrap_prev_frame_num += max_frame_num;
1437
1438 74 h->poc.prev_frame_num = unwrap_prev_frame_num;
1439 }
1440 }
1441
1442 /* See if we have a decoded first field looking for a pair...
1443 * Here, we're using that to see if we should mark previously
1444 * decode frames as "finished".
1445 * We have to do that before the "dummy" in-between frame allocation,
1446 * since that can modify h->cur_pic_ptr. */
1447
2/2
✓ Branch 0 taken 3362 times.
✓ Branch 1 taken 24929 times.
28291 if (h->first_field) {
1448 3362 int last_field = last_pic_structure == PICT_BOTTOM_FIELD;
1449
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3362 times.
3362 av_assert0(h->cur_pic_ptr);
1450
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3362 times.
3362 av_assert0(h->cur_pic_ptr->f->buf[0]);
1451 assert(h->cur_pic_ptr->reference != DELAYED_PIC_REF);
1452
1453 /* Mark old field/frame as completed */
1454
1/2
✓ Branch 0 taken 3362 times.
✗ Branch 1 not taken.
3362 if (h->cur_pic_ptr->tf.owner[last_field] == h->avctx) {
1455 3362 ff_thread_report_progress(&h->cur_pic_ptr->tf, INT_MAX, last_field);
1456 }
1457
1458 /* figure out if we have a complementary field pair */
1459
2/4
✓ Branch 0 taken 3362 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 3362 times.
3362 if (!FIELD_PICTURE(h) || h->picture_structure == last_pic_structure) {
1460 /* Previous field is unmatched. Don't display it, but let it
1461 * remain for reference if marked as such. */
1462 if (last_pic_structure != PICT_FRAME) {
1463 ff_thread_report_progress(&h->cur_pic_ptr->tf, INT_MAX,
1464 last_pic_structure == PICT_TOP_FIELD);
1465 }
1466 } else {
1467
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3362 times.
3362 if (h->cur_pic_ptr->frame_num != h->poc.frame_num) {
1468 /* This and previous field were reference, but had
1469 * different frame_nums. Consider this field first in
1470 * pair. Throw away previous field except for reference
1471 * purposes. */
1472 if (last_pic_structure != PICT_FRAME) {
1473 ff_thread_report_progress(&h->cur_pic_ptr->tf, INT_MAX,
1474 last_pic_structure == PICT_TOP_FIELD);
1475 }
1476 } else {
1477 /* Second field in complementary pair */
1478
3/4
✓ Branch 0 taken 3354 times.
✓ Branch 1 taken 8 times.
✓ Branch 2 taken 8 times.
✗ Branch 3 not taken.
3362 if (!((last_pic_structure == PICT_TOP_FIELD &&
1479
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3354 times.
3354 h->picture_structure == PICT_BOTTOM_FIELD) ||
1480 8 (last_pic_structure == PICT_BOTTOM_FIELD &&
1481
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
8 h->picture_structure == PICT_TOP_FIELD))) {
1482 av_log(h->avctx, AV_LOG_ERROR,
1483 "Invalid field mode combination %d/%d\n",
1484 last_pic_structure, h->picture_structure);
1485 h->picture_structure = last_pic_structure;
1486 h->droppable = last_pic_droppable;
1487 return AVERROR_INVALIDDATA;
1488
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3362 times.
3362 } else if (last_pic_droppable != h->droppable) {
1489 avpriv_request_sample(h->avctx,
1490 "Found reference and non-reference fields in the same frame, which");
1491 h->picture_structure = last_pic_structure;
1492 h->droppable = last_pic_droppable;
1493 return AVERROR_PATCHWELCOME;
1494 }
1495 }
1496 }
1497 }
1498
1499
3/4
✓ Branch 0 taken 16832 times.
✓ Branch 1 taken 12144 times.
✓ Branch 2 taken 16832 times.
✗ Branch 3 not taken.
57267 while (h->poc.frame_num != h->poc.prev_frame_num && !h->first_field &&
1500
2/2
✓ Branch 0 taken 685 times.
✓ Branch 1 taken 16147 times.
16832 h->poc.frame_num != (h->poc.prev_frame_num + 1) % (1 << sps->log2_max_frame_num)) {
1501
2/2
✓ Branch 0 taken 653 times.
✓ Branch 1 taken 32 times.
685 const H264Picture *prev = h->short_ref_count ? h->short_ref[0] : NULL;
1502 685 av_log(h->avctx, AV_LOG_DEBUG, "Frame num gap %d %d\n",
1503 h->poc.frame_num, h->poc.prev_frame_num);
1504
2/2
✓ Branch 0 taken 108 times.
✓ Branch 1 taken 577 times.
685 if (!sps->gaps_in_frame_num_allowed_flag)
1505
2/2
✓ Branch 0 taken 1728 times.
✓ Branch 1 taken 108 times.
1836 for(i=0; i<FF_ARRAY_ELEMS(h->last_pocs); i++)
1506 1728 h->last_pocs[i] = INT_MIN;
1507 685 ret = h264_frame_start(h);
1508
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 685 times.
685 if (ret < 0) {
1509 h->first_field = 0;
1510 return ret;
1511 }
1512
1513 685 h->poc.prev_frame_num++;
1514 685 h->poc.prev_frame_num %= 1 << sps->log2_max_frame_num;
1515 685 h->cur_pic_ptr->frame_num = h->poc.prev_frame_num;
1516 685 h->cur_pic_ptr->invalid_gap = !sps->gaps_in_frame_num_allowed_flag;
1517 685 ff_thread_report_progress(&h->cur_pic_ptr->tf, INT_MAX, 0);
1518 685 ff_thread_report_progress(&h->cur_pic_ptr->tf, INT_MAX, 1);
1519
1520 685 h->explicit_ref_marking = 0;
1521 685 ret = ff_h264_execute_ref_pic_marking(h);
1522
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 685 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
685 if (ret < 0 && (h->avctx->err_recognition & AV_EF_EXPLODE))
1523 return ret;
1524 /* Error concealment: If a ref is missing, copy the previous ref
1525 * in its place.
1526 * FIXME: Avoiding a memcpy would be nice, but ref handling makes
1527 * many assumptions about there being no actual duplicates.
1528 * FIXME: This does not copy padding for out-of-frame motion
1529 * vectors. Given we are concealing a lost frame, this probably
1530 * is not noticeable by comparison, but it should be fixed. */
1531
1/2
✓ Branch 0 taken 685 times.
✗ Branch 1 not taken.
685 if (h->short_ref_count) {
1532 685 int c[4] = {
1533 685 1<<(h->ps.sps->bit_depth_luma-1),
1534 685 1<<(h->ps.sps->bit_depth_chroma-1),
1535 685 1<<(h->ps.sps->bit_depth_chroma-1),
1536 -1
1537 };
1538
1539
2/2
✓ Branch 0 taken 653 times.
✓ Branch 1 taken 32 times.
685 if (prev &&
1540
1/2
✓ Branch 0 taken 653 times.
✗ Branch 1 not taken.
653 h->short_ref[0]->f->width == prev->f->width &&
1541
1/2
✓ Branch 0 taken 653 times.
✗ Branch 1 not taken.
653 h->short_ref[0]->f->height == prev->f->height &&
1542
1/2
✓ Branch 0 taken 653 times.
✗ Branch 1 not taken.
653 h->short_ref[0]->f->format == prev->f->format) {
1543 653 ff_thread_await_progress(&prev->tf, INT_MAX, 0);
1544
2/2
✓ Branch 0 taken 28 times.
✓ Branch 1 taken 625 times.
653 if (prev->field_picture)
1545 28 ff_thread_await_progress(&prev->tf, INT_MAX, 1);
1546 653 ff_thread_release_ext_buffer(&h->short_ref[0]->tf);
1547 653 h->short_ref[0]->tf.f = h->short_ref[0]->f;
1548 653 ret = ff_thread_ref_frame(&h->short_ref[0]->tf, &prev->tf);
1549
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 653 times.
653 if (ret < 0)
1550 return ret;
1551 653 h->short_ref[0]->poc = prev->poc + 2U;
1552 653 h->short_ref[0]->gray = prev->gray;
1553 653 ff_thread_report_progress(&h->short_ref[0]->tf, INT_MAX, 0);
1554
2/2
✓ Branch 0 taken 28 times.
✓ Branch 1 taken 625 times.
653 if (h->short_ref[0]->field_picture)
1555 28 ff_thread_report_progress(&h->short_ref[0]->tf, INT_MAX, 1);
1556
2/2
✓ Branch 0 taken 24 times.
✓ Branch 1 taken 8 times.
32 } else if (!h->frame_recovered) {
1557
1/2
✓ Branch 0 taken 24 times.
✗ Branch 1 not taken.
24 if (!h->avctx->hwaccel)
1558 24 color_frame(h->short_ref[0]->f, c);
1559 24 h->short_ref[0]->gray = 1;
1560 }
1561 685 h->short_ref[0]->frame_num = h->poc.prev_frame_num;
1562 }
1563 }
1564
1565 /* See if we have a decoded first field looking for a pair...
1566 * We're using that to see whether to continue decoding in that
1567 * frame, or to allocate a new one. */
1568
2/2
✓ Branch 0 taken 3362 times.
✓ Branch 1 taken 24929 times.
28291 if (h->first_field) {
1569
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3362 times.
3362 av_assert0(h->cur_pic_ptr);
1570
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3362 times.
3362 av_assert0(h->cur_pic_ptr->f->buf[0]);
1571 assert(h->cur_pic_ptr->reference != DELAYED_PIC_REF);
1572
1573 /* figure out if we have a complementary field pair */
1574
2/4
✓ Branch 0 taken 3362 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 3362 times.
3362 if (!FIELD_PICTURE(h) || h->picture_structure == last_pic_structure) {
1575 /* Previous field is unmatched. Don't display it, but let it
1576 * remain for reference if marked as such. */
1577 h->missing_fields ++;
1578 h->cur_pic_ptr = NULL;
1579 h->first_field = FIELD_PICTURE(h);
1580 } else {
1581 3362 h->missing_fields = 0;
1582
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3362 times.
3362 if (h->cur_pic_ptr->frame_num != h->poc.frame_num) {
1583 ff_thread_report_progress(&h->cur_pic_ptr->tf, INT_MAX,
1584 h->picture_structure==PICT_BOTTOM_FIELD);
1585 /* This and the previous field had different frame_nums.
1586 * Consider this field first in pair. Throw away previous
1587 * one except for reference purposes. */
1588 h->first_field = 1;
1589 h->cur_pic_ptr = NULL;
1590
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3362 times.
3362 } else if (h->cur_pic_ptr->reference & DELAYED_PIC_REF) {
1591 /* This frame was already output, we cannot draw into it
1592 * anymore.
1593 */
1594 h->first_field = 1;
1595 h->cur_pic_ptr = NULL;
1596 } else {
1597 /* Second field in complementary pair */
1598 3362 h->first_field = 0;
1599 }
1600 }
1601 } else {
1602 /* Frame or first field in a potentially complementary pair */
1603 24929 h->first_field = FIELD_PICTURE(h);
1604 }
1605
1606
4/4
✓ Branch 0 taken 6729 times.
✓ Branch 1 taken 21562 times.
✓ Branch 2 taken 3367 times.
✓ Branch 3 taken 3362 times.
28291 if (!FIELD_PICTURE(h) || h->first_field) {
1607
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 24929 times.
24929 if (h264_frame_start(h) < 0) {
1608 h->first_field = 0;
1609 return AVERROR_INVALIDDATA;
1610 }
1611 } else {
1612 3362 int field = h->picture_structure == PICT_BOTTOM_FIELD;
1613 3362 release_unused_pictures(h, 0);
1614 3362 h->cur_pic_ptr->tf.owner[field] = h->avctx;
1615 }
1616 /* Some macroblocks can be accessed before they're available in case
1617 * of lost slices, MBAFF or threading. */
1618
2/2
✓ Branch 0 taken 6729 times.
✓ Branch 1 taken 21562 times.
28291 if (FIELD_PICTURE(h)) {
1619
2/2
✓ Branch 0 taken 168342 times.
✓ Branch 1 taken 6729 times.
175071 for(i = (h->picture_structure == PICT_BOTTOM_FIELD); i<h->mb_height; i++)
1620 168342 memset(h->slice_table + i*h->mb_stride, -1, (h->mb_stride - (i+1==h->mb_height)) * sizeof(*h->slice_table));
1621 } else {
1622 21562 memset(h->slice_table, -1,
1623 21562 (h->mb_height * h->mb_stride - 1) * sizeof(*h->slice_table));
1624 }
1625
1626 28291 ret = ff_h264_init_poc(h->cur_pic_ptr->field_poc, &h->cur_pic_ptr->poc,
1627 28291 h->ps.sps, &h->poc, h->picture_structure, nal->ref_idc);
1628
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 28291 times.
28291 if (ret < 0)
1629 return ret;
1630
1631 28291 memcpy(h->mmco, sl->mmco, sl->nb_mmco * sizeof(*h->mmco));
1632 28291 h->nb_mmco = sl->nb_mmco;
1633 28291 h->explicit_ref_marking = sl->explicit_ref_marking;
1634
1635 28291 h->picture_idr = nal->type == H264_NAL_IDR_SLICE;
1636
1637
2/2
✓ Branch 0 taken 90 times.
✓ Branch 1 taken 28201 times.
28291 if (h->sei.recovery_point.recovery_frame_cnt >= 0) {
1638 90 const int sei_recovery_frame_cnt = h->sei.recovery_point.recovery_frame_cnt;
1639
1640
4/4
✓ Branch 0 taken 25 times.
✓ Branch 1 taken 65 times.
✓ Branch 2 taken 11 times.
✓ Branch 3 taken 14 times.
90 if (h->poc.frame_num != sei_recovery_frame_cnt || sl->slice_type_nos != AV_PICTURE_TYPE_I)
1641 76 h->valid_recovery_point = 1;
1642
1643
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 86 times.
90 if ( h->recovery_frame < 0
1644
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 || av_mod_uintp2(h->recovery_frame - h->poc.frame_num, h->ps.sps->log2_max_frame_num) > sei_recovery_frame_cnt) {
1645 86 h->recovery_frame = av_mod_uintp2(h->poc.frame_num + sei_recovery_frame_cnt, h->ps.sps->log2_max_frame_num);
1646
1647
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 80 times.
86 if (!h->valid_recovery_point)
1648 6 h->recovery_frame = h->poc.frame_num;
1649 }
1650 }
1651
1652
2/2
✓ Branch 0 taken 1077 times.
✓ Branch 1 taken 27214 times.
28291 h->cur_pic_ptr->f->flags |= AV_FRAME_FLAG_KEY * !!(nal->type == H264_NAL_IDR_SLICE);
1653
1654
2/2
✓ Branch 0 taken 1077 times.
✓ Branch 1 taken 27214 times.
28291 if (nal->type == H264_NAL_IDR_SLICE) {
1655 1077 h->cur_pic_ptr->recovered |= FRAME_RECOVERED_IDR;
1656 // If we have an IDR, all frames after it in decoded order are
1657 // "recovered".
1658 1077 h->frame_recovered |= FRAME_RECOVERED_IDR;
1659 }
1660
1661
4/4
✓ Branch 0 taken 99 times.
✓ Branch 1 taken 28192 times.
✓ Branch 2 taken 85 times.
✓ Branch 3 taken 14 times.
28291 if (h->recovery_frame == h->poc.frame_num && nal->ref_idc) {
1662 85 h->recovery_frame = -1;
1663 85 h->cur_pic_ptr->recovered |= FRAME_RECOVERED_SEI;
1664 }
1665
1666 #if 1
1667 28291 h->cur_pic_ptr->recovered |= h->frame_recovered;
1668 #else
1669 h->cur_pic_ptr->recovered |= !!(h->frame_recovered & FRAME_RECOVERED_IDR);
1670 #endif
1671
1672 /* Set the frame properties/side data. Only done for the second field in
1673 * field coded frames, since some SEI information is present for each field
1674 * and is merged by the SEI parsing code. */
1675
5/6
✓ Branch 0 taken 6729 times.
✓ Branch 1 taken 21562 times.
✓ Branch 2 taken 3367 times.
✓ Branch 3 taken 3362 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 3367 times.
28291 if (!FIELD_PICTURE(h) || !h->first_field || h->missing_fields > 1) {
1676 24924 ret = h264_export_frame_props(h);
1677
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 24924 times.
24924 if (ret < 0)
1678 return ret;
1679
1680 24924 ret = h264_select_output_frame(h);
1681
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 24924 times.
24924 if (ret < 0)
1682 return ret;
1683 }
1684
1685 28291 return 0;
1686 }
1687
1688 37611 static int h264_slice_header_parse(const H264Context *h, H264SliceContext *sl,
1689 const H2645NAL *nal)
1690 {
1691 const SPS *sps;
1692 const PPS *pps;
1693 int ret;
1694 unsigned int slice_type, tmp, i;
1695 int field_pic_flag, bottom_field_flag;
1696
4/4
✓ Branch 0 taken 37161 times.
✓ Branch 1 taken 450 times.
✓ Branch 2 taken 28340 times.
✓ Branch 3 taken 8821 times.
37611 int first_slice = sl == h->slice_ctx && !h->current_slice;
1697 int picture_structure;
1698
1699
2/2
✓ Branch 0 taken 28340 times.
✓ Branch 1 taken 9271 times.
37611 if (first_slice)
1700
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 28340 times.
28340 av_assert0(!h->setup_finished);
1701
1702 37611 sl->first_mb_addr = get_ue_golomb_long(&sl->gb);
1703
1704 37611 slice_type = get_ue_golomb_31(&sl->gb);
1705
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 37611 times.
37611 if (slice_type > 9) {
1706 av_log(h->avctx, AV_LOG_ERROR,
1707 "slice type %d too large at %d\n",
1708 slice_type, sl->first_mb_addr);
1709 return AVERROR_INVALIDDATA;
1710 }
1711
2/2
✓ Branch 0 taken 23887 times.
✓ Branch 1 taken 13724 times.
37611 if (slice_type > 4) {
1712 23887 slice_type -= 5;
1713 23887 sl->slice_type_fixed = 1;
1714 } else
1715 13724 sl->slice_type_fixed = 0;
1716
1717 37611 slice_type = ff_h264_golomb_to_pict_type[slice_type];
1718 37611 sl->slice_type = slice_type;
1719 37611 sl->slice_type_nos = slice_type & 3;
1720
1721
2/2
✓ Branch 0 taken 2263 times.
✓ Branch 1 taken 35348 times.
37611 if (nal->type == H264_NAL_IDR_SLICE &&
1722
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2263 times.
2263 sl->slice_type_nos != AV_PICTURE_TYPE_I) {
1723 av_log(h->avctx, AV_LOG_ERROR, "A non-intra slice in an IDR NAL unit.\n");
1724 return AVERROR_INVALIDDATA;
1725 }
1726
1727 37611 sl->pps_id = get_ue_golomb(&sl->gb);
1728
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 37611 times.
37611 if (sl->pps_id >= MAX_PPS_COUNT) {
1729 av_log(h->avctx, AV_LOG_ERROR, "pps_id %u out of range\n", sl->pps_id);
1730 return AVERROR_INVALIDDATA;
1731 }
1732
2/2
✓ Branch 0 taken 167 times.
✓ Branch 1 taken 37444 times.
37611 if (!h->ps.pps_list[sl->pps_id]) {
1733 167 av_log(h->avctx, AV_LOG_ERROR,
1734 "non-existing PPS %u referenced\n",
1735 sl->pps_id);
1736 167 return AVERROR_INVALIDDATA;
1737 }
1738 37444 pps = h->ps.pps_list[sl->pps_id];
1739 37444 sps = pps->sps;
1740
1741 37444 sl->frame_num = get_bits(&sl->gb, sps->log2_max_frame_num);
1742
2/2
✓ Branch 0 taken 9267 times.
✓ Branch 1 taken 28177 times.
37444 if (!first_slice) {
1743
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9267 times.
9267 if (h->poc.frame_num != sl->frame_num) {
1744 av_log(h->avctx, AV_LOG_ERROR, "Frame num change from %d to %d\n",
1745 h->poc.frame_num, sl->frame_num);
1746 return AVERROR_INVALIDDATA;
1747 }
1748 }
1749
1750 37444 sl->mb_mbaff = 0;
1751
1752
2/2
✓ Branch 0 taken 22948 times.
✓ Branch 1 taken 14496 times.
37444 if (sps->frame_mbs_only_flag) {
1753 22948 picture_structure = PICT_FRAME;
1754 } else {
1755
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 14496 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
14496 if (!sps->direct_8x8_inference_flag && slice_type == AV_PICTURE_TYPE_B) {
1756 av_log(h->avctx, AV_LOG_ERROR, "This stream was generated by a broken encoder, invalid 8x8 inference\n");
1757 return -1;
1758 }
1759 14496 field_pic_flag = get_bits1(&sl->gb);
1760
2/2
✓ Branch 0 taken 9026 times.
✓ Branch 1 taken 5470 times.
14496 if (field_pic_flag) {
1761 9026 bottom_field_flag = get_bits1(&sl->gb);
1762 9026 picture_structure = PICT_TOP_FIELD + bottom_field_flag;
1763 } else {
1764 5470 picture_structure = PICT_FRAME;
1765 }
1766 }
1767 37444 sl->picture_structure = picture_structure;
1768 37444 sl->mb_field_decoding_flag = picture_structure != PICT_FRAME;
1769
1770
2/2
✓ Branch 0 taken 28418 times.
✓ Branch 1 taken 9026 times.
37444 if (picture_structure == PICT_FRAME) {
1771 28418 sl->curr_pic_num = sl->frame_num;
1772 28418 sl->max_pic_num = 1 << sps->log2_max_frame_num;
1773 } else {
1774 9026 sl->curr_pic_num = 2 * sl->frame_num + 1;
1775 9026 sl->max_pic_num = 1 << (sps->log2_max_frame_num + 1);
1776 }
1777
1778
2/2
✓ Branch 0 taken 2258 times.
✓ Branch 1 taken 35186 times.
37444 if (nal->type == H264_NAL_IDR_SLICE) {
1779 2258 unsigned idr_pic_id = get_ue_golomb_long(&sl->gb);
1780
1/2
✓ Branch 0 taken 2258 times.
✗ Branch 1 not taken.
2258 if (idr_pic_id < 65536) {
1781 2258 sl->idr_pic_id = idr_pic_id;
1782 } else
1783 av_log(h->avctx, AV_LOG_WARNING, "idr_pic_id is invalid\n");
1784 }
1785
1786 37444 sl->poc_lsb = 0;
1787 37444 sl->delta_poc_bottom = 0;
1788
2/2
✓ Branch 0 taken 29664 times.
✓ Branch 1 taken 7780 times.
37444 if (sps->poc_type == 0) {
1789 29664 sl->poc_lsb = get_bits(&sl->gb, sps->log2_max_poc_lsb);
1790
1791
4/4
✓ Branch 0 taken 8525 times.
✓ Branch 1 taken 21139 times.
✓ Branch 2 taken 6633 times.
✓ Branch 3 taken 1892 times.
29664 if (pps->pic_order_present == 1 && picture_structure == PICT_FRAME)
1792 6633 sl->delta_poc_bottom = get_se_golomb(&sl->gb);
1793 }
1794
1795 37444 sl->delta_poc[0] = sl->delta_poc[1] = 0;
1796
4/4
✓ Branch 0 taken 3055 times.
✓ Branch 1 taken 34389 times.
✓ Branch 2 taken 2848 times.
✓ Branch 3 taken 207 times.
37444 if (sps->poc_type == 1 && !sps->delta_pic_order_always_zero_flag) {
1797 2848 sl->delta_poc[0] = get_se_golomb(&sl->gb);
1798
1799
4/4
✓ Branch 0 taken 770 times.
✓ Branch 1 taken 2078 times.
✓ Branch 2 taken 453 times.
✓ Branch 3 taken 317 times.
2848 if (pps->pic_order_present == 1 && picture_structure == PICT_FRAME)
1800 453 sl->delta_poc[1] = get_se_golomb(&sl->gb);
1801 }
1802
1803 37444 sl->redundant_pic_count = 0;
1804
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 37444 times.
37444 if (pps->redundant_pic_cnt_present)
1805 sl->redundant_pic_count = get_ue_golomb(&sl->gb);
1806
1807
2/2
✓ Branch 0 taken 13151 times.
✓ Branch 1 taken 24293 times.
37444 if (sl->slice_type_nos == AV_PICTURE_TYPE_B)
1808 13151 sl->direct_spatial_mv_pred = get_bits1(&sl->gb);
1809
1810 37444 ret = ff_h264_parse_ref_count(&sl->list_count, sl->ref_count,
1811 &sl->gb, pps, sl->slice_type_nos,
1812 37444 picture_structure, h->avctx);
1813
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 37444 times.
37444 if (ret < 0)
1814 return ret;
1815
1816
2/2
✓ Branch 0 taken 32978 times.
✓ Branch 1 taken 4466 times.
37444 if (sl->slice_type_nos != AV_PICTURE_TYPE_I) {
1817 32978 ret = ff_h264_decode_ref_pic_list_reordering(sl, h->avctx);
1818
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 32978 times.
32978 if (ret < 0) {
1819 sl->ref_count[1] = sl->ref_count[0] = 0;
1820 return ret;
1821 }
1822 }
1823
1824 37444 sl->pwt.use_weight = 0;
1825
2/2
✓ Branch 0 taken 74888 times.
✓ Branch 1 taken 37444 times.
112332 for (i = 0; i < 2; i++) {
1826 74888 sl->pwt.luma_weight_flag[i] = 0;
1827 74888 sl->pwt.chroma_weight_flag[i] = 0;
1828 }
1829
4/4
✓ Branch 0 taken 4311 times.
✓ Branch 1 taken 33133 times.
✓ Branch 2 taken 1631 times.
✓ Branch 3 taken 2680 times.
37444 if ((pps->weighted_pred && sl->slice_type_nos == AV_PICTURE_TYPE_P) ||
1830
2/2
✓ Branch 0 taken 386 times.
✓ Branch 1 taken 34378 times.
34764 (pps->weighted_bipred_idc == 1 &&
1831
1/2
✓ Branch 0 taken 386 times.
✗ Branch 1 not taken.
386 sl->slice_type_nos == AV_PICTURE_TYPE_B)) {
1832 3066 ret = ff_h264_pred_weight_table(&sl->gb, sps, sl->ref_count,
1833 sl->slice_type_nos, &sl->pwt,
1834 3066 picture_structure, h->avctx);
1835
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3066 times.
3066 if (ret < 0)
1836 return ret;
1837 }
1838
1839 37444 sl->explicit_ref_marking = 0;
1840
2/2
✓ Branch 0 taken 24954 times.
✓ Branch 1 taken 12490 times.
37444 if (nal->ref_idc) {
1841 24954 ret = ff_h264_decode_ref_pic_marking(sl, &sl->gb, nal, h->avctx);
1842
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 24954 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
24954 if (ret < 0 && (h->avctx->err_recognition & AV_EF_EXPLODE))
1843 return AVERROR_INVALIDDATA;
1844 }
1845
1846
4/4
✓ Branch 0 taken 32978 times.
✓ Branch 1 taken 4466 times.
✓ Branch 2 taken 18166 times.
✓ Branch 3 taken 14812 times.
37444 if (sl->slice_type_nos != AV_PICTURE_TYPE_I && pps->cabac) {
1847 18166 tmp = get_ue_golomb_31(&sl->gb);
1848
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 18166 times.
18166 if (tmp > 2) {
1849 av_log(h->avctx, AV_LOG_ERROR, "cabac_init_idc %u overflow\n", tmp);
1850 return AVERROR_INVALIDDATA;
1851 }
1852 18166 sl->cabac_init_idc = tmp;
1853 }
1854
1855 37444 sl->last_qscale_diff = 0;
1856 37444 tmp = pps->init_qp + (unsigned)get_se_golomb(&sl->gb);
1857
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 37444 times.
37444 if (tmp > 51 + 6 * (sps->bit_depth_luma - 8)) {
1858 av_log(h->avctx, AV_LOG_ERROR, "QP %u out of range\n", tmp);
1859 return AVERROR_INVALIDDATA;
1860 }
1861 37444 sl->qscale = tmp;
1862 37444 sl->chroma_qp[0] = get_chroma_qp(pps, 0, sl->qscale);
1863 37444 sl->chroma_qp[1] = get_chroma_qp(pps, 1, sl->qscale);
1864 // FIXME qscale / qp ... stuff
1865
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 37444 times.
37444 if (sl->slice_type == AV_PICTURE_TYPE_SP)
1866 get_bits1(&sl->gb); /* sp_for_switch_flag */
1867
1/2
✓ Branch 0 taken 37444 times.
✗ Branch 1 not taken.
37444 if (sl->slice_type == AV_PICTURE_TYPE_SP ||
1868
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 37444 times.
37444 sl->slice_type == AV_PICTURE_TYPE_SI)
1869 get_se_golomb(&sl->gb); /* slice_qs_delta */
1870
1871 37444 sl->deblocking_filter = 1;
1872 37444 sl->slice_alpha_c0_offset = 0;
1873 37444 sl->slice_beta_offset = 0;
1874
2/2
✓ Branch 0 taken 22661 times.
✓ Branch 1 taken 14783 times.
37444 if (pps->deblocking_filter_parameters_present) {
1875 22661 tmp = get_ue_golomb_31(&sl->gb);
1876
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 22661 times.
22661 if (tmp > 2)