FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/h264_slice.c
Date: 2026-06-16 12:54:33
Exec Total Coverage
Lines: 1376 1678 82.0%
Functions: 32 33 97.0%
Branches: 923 1265 73.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 #include "config_components.h"
29
30 #include "libavutil/avassert.h"
31 #include "libavutil/mem.h"
32 #include "libavutil/pixdesc.h"
33 #include "libavutil/timecode.h"
34 #include "decode.h"
35 #include "cabac.h"
36 #include "cabac_functions.h"
37 #include "error_resilience.h"
38 #include "avcodec.h"
39 #include "h264.h"
40 #include "h264dec.h"
41 #include "h264data.h"
42 #include "h264chroma.h"
43 #include "h264_ps.h"
44 #include "golomb.h"
45 #include "mathops.h"
46 #include "mpegutils.h"
47 #include "rectangle.h"
48 #include "libavutil/refstruct.h"
49 #include "thread.h"
50 #include "threadframe.h"
51
52 static const uint8_t field_scan[16+1] = {
53 0 + 0 * 4, 0 + 1 * 4, 1 + 0 * 4, 0 + 2 * 4,
54 0 + 3 * 4, 1 + 1 * 4, 1 + 2 * 4, 1 + 3 * 4,
55 2 + 0 * 4, 2 + 1 * 4, 2 + 2 * 4, 2 + 3 * 4,
56 3 + 0 * 4, 3 + 1 * 4, 3 + 2 * 4, 3 + 3 * 4,
57 };
58
59 static const uint8_t field_scan8x8[64+1] = {
60 0 + 0 * 8, 0 + 1 * 8, 0 + 2 * 8, 1 + 0 * 8,
61 1 + 1 * 8, 0 + 3 * 8, 0 + 4 * 8, 1 + 2 * 8,
62 2 + 0 * 8, 1 + 3 * 8, 0 + 5 * 8, 0 + 6 * 8,
63 0 + 7 * 8, 1 + 4 * 8, 2 + 1 * 8, 3 + 0 * 8,
64 2 + 2 * 8, 1 + 5 * 8, 1 + 6 * 8, 1 + 7 * 8,
65 2 + 3 * 8, 3 + 1 * 8, 4 + 0 * 8, 3 + 2 * 8,
66 2 + 4 * 8, 2 + 5 * 8, 2 + 6 * 8, 2 + 7 * 8,
67 3 + 3 * 8, 4 + 1 * 8, 5 + 0 * 8, 4 + 2 * 8,
68 3 + 4 * 8, 3 + 5 * 8, 3 + 6 * 8, 3 + 7 * 8,
69 4 + 3 * 8, 5 + 1 * 8, 6 + 0 * 8, 5 + 2 * 8,
70 4 + 4 * 8, 4 + 5 * 8, 4 + 6 * 8, 4 + 7 * 8,
71 5 + 3 * 8, 6 + 1 * 8, 6 + 2 * 8, 5 + 4 * 8,
72 5 + 5 * 8, 5 + 6 * 8, 5 + 7 * 8, 6 + 3 * 8,
73 7 + 0 * 8, 7 + 1 * 8, 6 + 4 * 8, 6 + 5 * 8,
74 6 + 6 * 8, 6 + 7 * 8, 7 + 2 * 8, 7 + 3 * 8,
75 7 + 4 * 8, 7 + 5 * 8, 7 + 6 * 8, 7 + 7 * 8,
76 };
77
78 static const uint8_t field_scan8x8_cavlc[64+1] = {
79 0 + 0 * 8, 1 + 1 * 8, 2 + 0 * 8, 0 + 7 * 8,
80 2 + 2 * 8, 2 + 3 * 8, 2 + 4 * 8, 3 + 3 * 8,
81 3 + 4 * 8, 4 + 3 * 8, 4 + 4 * 8, 5 + 3 * 8,
82 5 + 5 * 8, 7 + 0 * 8, 6 + 6 * 8, 7 + 4 * 8,
83 0 + 1 * 8, 0 + 3 * 8, 1 + 3 * 8, 1 + 4 * 8,
84 1 + 5 * 8, 3 + 1 * 8, 2 + 5 * 8, 4 + 1 * 8,
85 3 + 5 * 8, 5 + 1 * 8, 4 + 5 * 8, 6 + 1 * 8,
86 5 + 6 * 8, 7 + 1 * 8, 6 + 7 * 8, 7 + 5 * 8,
87 0 + 2 * 8, 0 + 4 * 8, 0 + 5 * 8, 2 + 1 * 8,
88 1 + 6 * 8, 4 + 0 * 8, 2 + 6 * 8, 5 + 0 * 8,
89 3 + 6 * 8, 6 + 0 * 8, 4 + 6 * 8, 6 + 2 * 8,
90 5 + 7 * 8, 6 + 4 * 8, 7 + 2 * 8, 7 + 6 * 8,
91 1 + 0 * 8, 1 + 2 * 8, 0 + 6 * 8, 3 + 0 * 8,
92 1 + 7 * 8, 3 + 2 * 8, 2 + 7 * 8, 4 + 2 * 8,
93 3 + 7 * 8, 5 + 2 * 8, 4 + 7 * 8, 5 + 4 * 8,
94 6 + 3 * 8, 6 + 5 * 8, 7 + 3 * 8, 7 + 7 * 8,
95 };
96
97 // zigzag_scan8x8_cavlc[i] = zigzag_scan8x8[(i/4) + 16*(i%4)]
98 static const uint8_t zigzag_scan8x8_cavlc[64+1] = {
99 0 + 0 * 8, 1 + 1 * 8, 1 + 2 * 8, 2 + 2 * 8,
100 4 + 1 * 8, 0 + 5 * 8, 3 + 3 * 8, 7 + 0 * 8,
101 3 + 4 * 8, 1 + 7 * 8, 5 + 3 * 8, 6 + 3 * 8,
102 2 + 7 * 8, 6 + 4 * 8, 5 + 6 * 8, 7 + 5 * 8,
103 1 + 0 * 8, 2 + 0 * 8, 0 + 3 * 8, 3 + 1 * 8,
104 3 + 2 * 8, 0 + 6 * 8, 4 + 2 * 8, 6 + 1 * 8,
105 2 + 5 * 8, 2 + 6 * 8, 6 + 2 * 8, 5 + 4 * 8,
106 3 + 7 * 8, 7 + 3 * 8, 4 + 7 * 8, 7 + 6 * 8,
107 0 + 1 * 8, 3 + 0 * 8, 0 + 4 * 8, 4 + 0 * 8,
108 2 + 3 * 8, 1 + 5 * 8, 5 + 1 * 8, 5 + 2 * 8,
109 1 + 6 * 8, 3 + 5 * 8, 7 + 1 * 8, 4 + 5 * 8,
110 4 + 6 * 8, 7 + 4 * 8, 5 + 7 * 8, 6 + 7 * 8,
111 0 + 2 * 8, 2 + 1 * 8, 1 + 3 * 8, 5 + 0 * 8,
112 1 + 4 * 8, 2 + 4 * 8, 6 + 0 * 8, 4 + 3 * 8,
113 0 + 7 * 8, 4 + 4 * 8, 7 + 2 * 8, 3 + 6 * 8,
114 5 + 5 * 8, 6 + 5 * 8, 6 + 6 * 8, 7 + 7 * 8,
115 };
116
117 29792 static void release_unused_pictures(H264Context *h, int remove_current)
118 {
119 int i;
120
121 /* release non reference frames */
122
2/2
✓ Branch 0 taken 1072512 times.
✓ Branch 1 taken 29792 times.
1102304 for (i = 0; i < H264_MAX_PICTURE_COUNT; i++) {
123
6/6
✓ Branch 0 taken 149322 times.
✓ Branch 1 taken 923190 times.
✓ Branch 2 taken 26046 times.
✓ Branch 3 taken 123276 times.
✓ Branch 4 taken 2627 times.
✓ Branch 5 taken 23419 times.
1072512 if (h->DPB[i].f->buf[0] && !h->DPB[i].reference &&
124
2/2
✓ Branch 0 taken 737 times.
✓ Branch 1 taken 1890 times.
2627 (remove_current || &h->DPB[i] != h->cur_pic_ptr)) {
125 24156 ff_h264_unref_picture(&h->DPB[i]);
126 }
127 }
128 29792 }
129
130 38277 static int alloc_scratch_buffers(H264SliceContext *sl, int linesize)
131 {
132 38277 const H264Context *h = sl->h264;
133 38277 int alloc_size = FFALIGN(FFABS(linesize) + 32, 32);
134
135 38277 av_fast_malloc(&sl->bipred_scratchpad, &sl->bipred_scratchpad_allocated, 16 * 6 * alloc_size);
136 // edge emu needs blocksize + filter length - 1
137 // (= 21x21 for H.264)
138 38277 av_fast_malloc(&sl->edge_emu_buffer, &sl->edge_emu_buffer_allocated, alloc_size * 2 * 21);
139
140 38277 av_fast_mallocz(&sl->top_borders[0], &sl->top_borders_allocated[0],
141 38277 h->mb_width * 16 * 3 * sizeof(uint8_t) * 2);
142 38277 av_fast_mallocz(&sl->top_borders[1], &sl->top_borders_allocated[1],
143 38277 h->mb_width * 16 * 3 * sizeof(uint8_t) * 2);
144
145
2/4
✓ Branch 0 taken 38277 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 38277 times.
✗ Branch 3 not taken.
38277 if (!sl->bipred_scratchpad || !sl->edge_emu_buffer ||
146
2/4
✓ Branch 0 taken 38277 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 38277 times.
38277 !sl->top_borders[0] || !sl->top_borders[1]) {
147 av_freep(&sl->bipred_scratchpad);
148 av_freep(&sl->edge_emu_buffer);
149 av_freep(&sl->top_borders[0]);
150 av_freep(&sl->top_borders[1]);
151
152 sl->bipred_scratchpad_allocated = 0;
153 sl->edge_emu_buffer_allocated = 0;
154 sl->top_borders_allocated[0] = 0;
155 sl->top_borders_allocated[1] = 0;
156 return AVERROR(ENOMEM);
157 }
158
159 38277 return 0;
160 }
161
162 631 static int init_table_pools(H264Context *h)
163 {
164 631 const int big_mb_num = h->mb_stride * (h->mb_height + 1) + 1;
165 631 const int mb_array_size = h->mb_stride * h->mb_height;
166 631 const int b4_stride = h->mb_width * 4 + 1;
167 631 const int b4_array_size = b4_stride * h->mb_height * 4;
168
169 631 h->qscale_table_pool = av_refstruct_pool_alloc(big_mb_num + h->mb_stride, 0);
170 631 h->mb_type_pool = av_refstruct_pool_alloc((big_mb_num + h->mb_stride) *
171 sizeof(uint32_t), 0);
172 631 h->motion_val_pool = av_refstruct_pool_alloc(2 * (b4_array_size + 4) *
173 sizeof(int16_t), 0);
174 631 h->ref_index_pool = av_refstruct_pool_alloc(4 * mb_array_size, 0);
175
176
3/6
✓ Branch 0 taken 631 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 631 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 631 times.
✗ Branch 5 not taken.
631 if (!h->qscale_table_pool || !h->mb_type_pool || !h->motion_val_pool ||
177
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 631 times.
631 !h->ref_index_pool) {
178 av_refstruct_pool_uninit(&h->qscale_table_pool);
179 av_refstruct_pool_uninit(&h->mb_type_pool);
180 av_refstruct_pool_uninit(&h->motion_val_pool);
181 av_refstruct_pool_uninit(&h->ref_index_pool);
182 return AVERROR(ENOMEM);
183 }
184
185 631 return 0;
186 }
187
188 26374 static int alloc_picture(H264Context *h, H264Picture *pic)
189 {
190 26374 int i, ret = 0;
191
192
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 26374 times.
26374 av_assert0(!pic->f->data[0]);
193
194
2/2
✓ Branch 0 taken 32 times.
✓ Branch 1 taken 26342 times.
26374 if (h->sei.common.itut_t35.lcevc) {
195 32 ret = ff_frame_new_side_data_from_buf(h->avctx, pic->f, AV_FRAME_DATA_LCEVC, &h->sei.common.itut_t35.lcevc);
196
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 32 times.
32 if (ret < 0)
197 return ret;
198 }
199
200 26374 pic->tf.f = pic->f;
201 26374 ret = ff_thread_get_ext_buffer(h->avctx, &pic->tf,
202 26374 pic->reference ? AV_GET_BUFFER_FLAG_REF : 0);
203
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 26374 times.
26374 if (ret < 0)
204 goto fail;
205
206
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 26374 times.
26374 if (pic->needs_fg) {
207 pic->f_grain->format = pic->f->format;
208 pic->f_grain->width = pic->f->width;
209 pic->f_grain->height = pic->f->height;
210 ret = ff_thread_get_buffer(h->avctx, pic->f_grain, 0);
211 if (ret < 0)
212 goto fail;
213 }
214
215 26374 ret = ff_hwaccel_frame_priv_alloc(h->avctx, &pic->hwaccel_picture_private);
216
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 26374 times.
26374 if (ret < 0)
217 goto fail;
218
219
2/2
✓ Branch 0 taken 42 times.
✓ Branch 1 taken 26332 times.
26374 if (h->decode_error_flags_pool) {
220 42 pic->decode_error_flags = av_refstruct_pool_get(h->decode_error_flags_pool);
221
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 42 times.
42 if (!pic->decode_error_flags)
222 goto fail;
223 42 atomic_init(pic->decode_error_flags, 0);
224 }
225
226 if (CONFIG_GRAY && !h->avctx->hwaccel && h->flags & AV_CODEC_FLAG_GRAY && pic->f->data[2]) {
227 int h_chroma_shift, v_chroma_shift;
228 av_pix_fmt_get_chroma_sub_sample(pic->f->format,
229 &h_chroma_shift, &v_chroma_shift);
230
231 for(i=0; i<AV_CEIL_RSHIFT(pic->f->height, v_chroma_shift); i++) {
232 memset(pic->f->data[1] + pic->f->linesize[1]*i,
233 0x80, AV_CEIL_RSHIFT(pic->f->width, h_chroma_shift));
234 memset(pic->f->data[2] + pic->f->linesize[2]*i,
235 0x80, AV_CEIL_RSHIFT(pic->f->width, h_chroma_shift));
236 }
237 }
238
239
2/2
✓ Branch 0 taken 631 times.
✓ Branch 1 taken 25743 times.
26374 if (!h->qscale_table_pool) {
240 631 ret = init_table_pools(h);
241
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 631 times.
631 if (ret < 0)
242 goto fail;
243 }
244
245 26374 pic->qscale_table_base = av_refstruct_pool_get(h->qscale_table_pool);
246 26374 pic->mb_type_base = av_refstruct_pool_get(h->mb_type_pool);
247
2/4
✓ Branch 0 taken 26374 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 26374 times.
26374 if (!pic->qscale_table_base || !pic->mb_type_base)
248 goto fail;
249
250 26374 pic->mb_type = pic->mb_type_base + 2 * h->mb_stride + 1;
251 26374 pic->qscale_table = pic->qscale_table_base + 2 * h->mb_stride + 1;
252
253
2/2
✓ Branch 0 taken 52748 times.
✓ Branch 1 taken 26374 times.
79122 for (i = 0; i < 2; i++) {
254 52748 pic->motion_val_base[i] = av_refstruct_pool_get(h->motion_val_pool);
255 52748 pic->ref_index[i] = av_refstruct_pool_get(h->ref_index_pool);
256
2/4
✓ Branch 0 taken 52748 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 52748 times.
52748 if (!pic->motion_val_base[i] || !pic->ref_index[i])
257 goto fail;
258
259 52748 pic->motion_val[i] = pic->motion_val_base[i] + 4;
260 }
261
262 26374 pic->pps = av_refstruct_ref_c(h->ps.pps);
263
264 26374 pic->mb_width = h->mb_width;
265 26374 pic->mb_height = h->mb_height;
266 26374 pic->mb_stride = h->mb_stride;
267
268 26374 return 0;
269 fail:
270 ff_h264_unref_picture(pic);
271 return (ret < 0) ? ret : AVERROR(ENOMEM);
272 }
273
274 26374 static int find_unused_picture(const H264Context *h)
275 {
276 int i;
277
278
1/2
✓ Branch 0 taken 89042 times.
✗ Branch 1 not taken.
89042 for (i = 0; i < H264_MAX_PICTURE_COUNT; i++) {
279
2/2
✓ Branch 0 taken 26374 times.
✓ Branch 1 taken 62668 times.
89042 if (!h->DPB[i].f->buf[0])
280 26374 return i;
281 }
282 return AVERROR_INVALIDDATA;
283 }
284
285
286 #define IN_RANGE(a, b, size) (((void*)(a) >= (void*)(b)) && ((void*)(a) < (void*)((b) + (size))))
287
288 #define REBASE_PICTURE(pic, new_ctx, old_ctx) \
289 (((pic) && (pic) >= (old_ctx)->DPB && \
290 (pic) < (old_ctx)->DPB + H264_MAX_PICTURE_COUNT) ? \
291 &(new_ctx)->DPB[(pic) - (old_ctx)->DPB] : NULL)
292
293 174 static void copy_picture_range(H264Picture **to, H264Picture *const *from, int count,
294 H264Context *new_base, const H264Context *old_base)
295 {
296 int i;
297
298
2/2
✓ Branch 0 taken 4756 times.
✓ Branch 1 taken 174 times.
4930 for (i = 0; i < count; i++) {
299 av_assert1(!from[i] ||
300 IN_RANGE(from[i], old_base, 1) ||
301 IN_RANGE(from[i], old_base->DPB, H264_MAX_PICTURE_COUNT));
302
4/6
✓ Branch 0 taken 251 times.
✓ Branch 1 taken 4505 times.
✓ Branch 2 taken 251 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 251 times.
✗ Branch 5 not taken.
4756 to[i] = REBASE_PICTURE(from[i], new_base, old_base);
303 }
304 174 }
305
306 26 static void color_frame(AVFrame *frame, const int c[4])
307 {
308 26 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(frame->format);
309
310
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 26 times.
26 av_assert0(desc->flags & AV_PIX_FMT_FLAG_PLANAR);
311
312
2/2
✓ Branch 0 taken 78 times.
✓ Branch 1 taken 26 times.
104 for (int p = 0; p < desc->nb_components; p++) {
313 78 uint8_t *dst = frame->data[p];
314
4/4
✓ Branch 0 taken 52 times.
✓ Branch 1 taken 26 times.
✓ Branch 2 taken 26 times.
✓ Branch 3 taken 26 times.
78 int is_chroma = p == 1 || p == 2;
315
2/2
✓ Branch 0 taken 52 times.
✓ Branch 1 taken 26 times.
78 int bytes = is_chroma ? AV_CEIL_RSHIFT(frame->width, desc->log2_chroma_w) : frame->width;
316
2/2
✓ Branch 0 taken 52 times.
✓ Branch 1 taken 26 times.
78 int height = is_chroma ? AV_CEIL_RSHIFT(frame->height, desc->log2_chroma_h) : frame->height;
317
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 78 times.
78 if (desc->comp[0].depth >= 9) {
318 if (bytes >= 1)
319 ((uint16_t*)dst)[0] = c[p];
320 if (bytes >= 2)
321 av_memcpy_backptr(dst + 2, 2, 2 * (bytes - 1));
322 dst += frame->linesize[p];
323 for (int y = 1; y < height; y++) {
324 memcpy(dst, frame->data[p], 2*bytes);
325 dst += frame->linesize[p];
326 }
327 } else {
328
2/2
✓ Branch 0 taken 31168 times.
✓ Branch 1 taken 78 times.
31246 for (int y = 0; y < height; y++) {
329 31168 memset(dst, c[p], bytes);
330 31168 dst += frame->linesize[p];
331 }
332 }
333 }
334 26 }
335
336 static int h264_slice_header_init(H264Context *h);
337
338 58 int ff_h264_update_thread_context(AVCodecContext *dst,
339 const AVCodecContext *src)
340 {
341 58 H264Context *h = dst->priv_data, *h1 = src->priv_data;
342 58 int inited = h->context_initialized, err = 0;
343 58 int need_reinit = 0;
344 int i, ret;
345
346
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 58 times.
58 if (dst == src)
347 return 0;
348
349
3/4
✓ Branch 0 taken 42 times.
✓ Branch 1 taken 16 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 42 times.
58 if (inited && !h1->ps.sps)
350 return AVERROR_INVALIDDATA;
351
352
2/2
✓ Branch 0 taken 42 times.
✓ Branch 1 taken 16 times.
58 if (inited &&
353
1/2
✓ Branch 0 taken 42 times.
✗ Branch 1 not taken.
42 (h->width != h1->width ||
354
1/2
✓ Branch 0 taken 42 times.
✗ Branch 1 not taken.
42 h->height != h1->height ||
355
1/2
✓ Branch 0 taken 42 times.
✗ Branch 1 not taken.
42 h->mb_width != h1->mb_width ||
356
1/2
✓ Branch 0 taken 42 times.
✗ Branch 1 not taken.
42 h->mb_height != h1->mb_height ||
357
1/2
✓ Branch 0 taken 42 times.
✗ Branch 1 not taken.
42 !h->ps.sps ||
358
1/2
✓ Branch 0 taken 42 times.
✗ Branch 1 not taken.
42 h->ps.sps->bit_depth_luma != h1->ps.sps->bit_depth_luma ||
359
1/2
✓ Branch 0 taken 42 times.
✗ Branch 1 not taken.
42 h->ps.sps->chroma_format_idc != h1->ps.sps->chroma_format_idc ||
360
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 42 times.
42 h->ps.sps->vui.matrix_coeffs != h1->ps.sps->vui.matrix_coeffs)) {
361 need_reinit = 1;
362 }
363
364 /* copy block_offset since frame_start may not be called */
365 58 memcpy(h->block_offset, h1->block_offset, sizeof(h->block_offset));
366
367 // SPS/PPS
368
2/2
✓ Branch 0 taken 1856 times.
✓ Branch 1 taken 58 times.
1914 for (int i = 0; i < FF_ARRAY_ELEMS(h->ps.sps_list); i++)
369 1856 av_refstruct_replace(&h->ps.sps_list[i], h1->ps.sps_list[i]);
370
2/2
✓ Branch 0 taken 14848 times.
✓ Branch 1 taken 58 times.
14906 for (int i = 0; i < FF_ARRAY_ELEMS(h->ps.pps_list); i++)
371 14848 av_refstruct_replace(&h->ps.pps_list[i], h1->ps.pps_list[i]);
372
373 58 av_refstruct_replace(&h->ps.pps, h1->ps.pps);
374 58 h->ps.sps = h1->ps.sps;
375
376
3/4
✓ Branch 0 taken 58 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 16 times.
✓ Branch 3 taken 42 times.
58 if (need_reinit || !inited) {
377 16 h->width = h1->width;
378 16 h->height = h1->height;
379 16 h->mb_height = h1->mb_height;
380 16 h->mb_width = h1->mb_width;
381 16 h->mb_num = h1->mb_num;
382 16 h->mb_stride = h1->mb_stride;
383 16 h->b_stride = h1->b_stride;
384 16 h->x264_build = h1->x264_build;
385
386
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) {
387
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 16 times.
16 if ((err = h264_slice_header_init(h)) < 0) {
388 av_log(h->avctx, AV_LOG_ERROR, "h264_slice_header_init() failed");
389 return err;
390 }
391 }
392
393 /* copy block_offset since frame_start may not be called */
394 16 memcpy(h->block_offset, h1->block_offset, sizeof(h->block_offset));
395 }
396
397 58 h->width_from_caller = h1->width_from_caller;
398 58 h->height_from_caller = h1->height_from_caller;
399 58 h->first_field = h1->first_field;
400 58 h->picture_structure = h1->picture_structure;
401 58 h->mb_aff_frame = h1->mb_aff_frame;
402 58 h->droppable = h1->droppable;
403
404
2/2
✓ Branch 0 taken 2088 times.
✓ Branch 1 taken 58 times.
2146 for (i = 0; i < H264_MAX_PICTURE_COUNT; i++) {
405 2088 ret = ff_h264_replace_picture(&h->DPB[i], &h1->DPB[i]);
406
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2088 times.
2088 if (ret < 0)
407 return ret;
408 }
409
410
4/6
✓ Branch 0 taken 42 times.
✓ Branch 1 taken 16 times.
✓ Branch 2 taken 42 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 42 times.
✗ Branch 5 not taken.
58 h->cur_pic_ptr = REBASE_PICTURE(h1->cur_pic_ptr, h, h1);
411 58 ret = ff_h264_replace_picture(&h->cur_pic, &h1->cur_pic);
412
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 58 times.
58 if (ret < 0)
413 return ret;
414
415 58 h->enable_er = h1->enable_er;
416 58 h->workaround_bugs = h1->workaround_bugs;
417 58 h->droppable = h1->droppable;
418
419 // extradata/NAL handling
420 58 h->is_avc = h1->is_avc;
421 58 h->nal_length_size = h1->nal_length_size;
422
423 58 memcpy(&h->poc, &h1->poc, sizeof(h->poc));
424
425 58 memcpy(h->short_ref, h1->short_ref, sizeof(h->short_ref));
426 58 memcpy(h->long_ref, h1->long_ref, sizeof(h->long_ref));
427 58 memcpy(h->delayed_pic, h1->delayed_pic, sizeof(h->delayed_pic));
428 58 memcpy(h->last_pocs, h1->last_pocs, sizeof(h->last_pocs));
429
430 58 h->next_output_pic = h1->next_output_pic;
431 58 h->next_outputed_poc = h1->next_outputed_poc;
432 58 h->poc_offset = h1->poc_offset;
433
434 58 memcpy(h->mmco, h1->mmco, sizeof(h->mmco));
435 58 h->nb_mmco = h1->nb_mmco;
436 58 h->mmco_reset = h1->mmco_reset;
437 58 h->explicit_ref_marking = h1->explicit_ref_marking;
438 58 h->long_ref_count = h1->long_ref_count;
439 58 h->short_ref_count = h1->short_ref_count;
440
441 58 copy_picture_range(h->short_ref, h1->short_ref, 32, h, h1);
442 58 copy_picture_range(h->long_ref, h1->long_ref, 32, h, h1);
443 58 copy_picture_range(h->delayed_pic, h1->delayed_pic,
444 FF_ARRAY_ELEMS(h->delayed_pic), h, h1);
445
446 58 h->frame_recovered = h1->frame_recovered;
447
448 58 ret = ff_h2645_sei_ctx_replace(&h->sei.common, &h1->sei.common);
449
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 58 times.
58 if (ret < 0)
450 return ret;
451
452 58 h->sei.common.unregistered.x264_build = h1->sei.common.unregistered.x264_build;
453
454
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 42 times.
58 if (!h->cur_pic_ptr)
455 16 return 0;
456
457
2/2
✓ Branch 0 taken 27 times.
✓ Branch 1 taken 15 times.
42 if (!h->droppable) {
458 27 err = ff_h264_execute_ref_pic_marking(h);
459 27 h->poc.prev_poc_msb = h->poc.poc_msb;
460 27 h->poc.prev_poc_lsb = h->poc.poc_lsb;
461 }
462 42 h->poc.prev_frame_num_offset = h->poc.frame_num_offset;
463 42 h->poc.prev_frame_num = h->poc.frame_num;
464
465 42 h->recovery_frame = h1->recovery_frame;
466 42 h->non_gray = h1->non_gray;
467
468 42 return err;
469 }
470
471 46 int ff_h264_update_thread_context_for_user(AVCodecContext *dst,
472 const AVCodecContext *src)
473 {
474 46 H264Context *h = dst->priv_data;
475 46 const H264Context *h1 = src->priv_data;
476
477 46 h->is_avc = h1->is_avc;
478 46 h->nal_length_size = h1->nal_length_size;
479
480 46 return 0;
481 }
482
483 26374 static int h264_frame_start(H264Context *h)
484 {
485 H264Picture *pic;
486 int i, ret;
487 26374 const int pixel_shift = h->pixel_shift;
488
489
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 26374 times.
26374 if (!ff_thread_can_start_frame(h->avctx)) {
490 av_log(h->avctx, AV_LOG_ERROR, "Attempt to start a frame outside SETUP state\n");
491 return AVERROR_BUG;
492 }
493
494 26374 release_unused_pictures(h, 1);
495 26374 h->cur_pic_ptr = NULL;
496
497 26374 i = find_unused_picture(h);
498
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 26374 times.
26374 if (i < 0) {
499 av_log(h->avctx, AV_LOG_ERROR, "no frame buffer available\n");
500 return i;
501 }
502 26374 pic = &h->DPB[i];
503
504
2/2
✓ Branch 0 taken 18201 times.
✓ Branch 1 taken 8173 times.
26374 pic->reference = h->droppable ? 0 : h->picture_structure;
505 26374 pic->field_picture = h->picture_structure != PICT_FRAME;
506 26374 pic->frame_num = h->poc.frame_num;
507 /*
508 * Zero key_frame here; IDR markings per slice in frame or fields are ORed
509 * in later.
510 * See decode_nal_units().
511 */
512 26374 pic->f->flags &= ~AV_FRAME_FLAG_KEY;
513 26374 pic->mmco_reset = 0;
514 26374 pic->recovered = 0;
515 26374 pic->invalid_gap = 0;
516 26374 pic->sei_recovery_frame_cnt = h->sei.recovery_point.recovery_frame_cnt;
517
518 26374 pic->f->pict_type = h->slice_ctx[0].slice_type;
519
520 26374 pic->f->crop_left = h->crop_left;
521 26374 pic->f->crop_right = h->crop_right;
522 26374 pic->f->crop_top = h->crop_top;
523 26374 pic->f->crop_bottom = h->crop_bottom;
524
525 26374 pic->needs_fg =
526 26374 h->sei.common.film_grain_characteristics &&
527 h->sei.common.film_grain_characteristics->present &&
528
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 26374 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
26374 !h->avctx->hwaccel &&
529 !(h->avctx->export_side_data & AV_CODEC_EXPORT_DATA_FILM_GRAIN);
530
531
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 26374 times.
26374 if ((ret = alloc_picture(h, pic)) < 0)
532 return ret;
533
534 26374 h->cur_pic_ptr = pic;
535 26374 ff_h264_unref_picture(&h->cur_pic);
536 if (CONFIG_ERROR_RESILIENCE) {
537 26374 ff_h264_set_erpic(&h->er.cur_pic, NULL);
538 }
539
540
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 26374 times.
26374 if ((ret = ff_h264_ref_picture(&h->cur_pic, h->cur_pic_ptr)) < 0)
541 return ret;
542
543
2/2
✓ Branch 0 taken 26674 times.
✓ Branch 1 taken 26374 times.
53048 for (i = 0; i < h->nb_slice_ctx; i++) {
544 26674 h->slice_ctx[i].linesize = h->cur_pic_ptr->f->linesize[0];
545 26674 h->slice_ctx[i].uvlinesize = h->cur_pic_ptr->f->linesize[1];
546 }
547
548
2/2
✓ Branch 0 taken 26074 times.
✓ Branch 1 taken 300 times.
26374 if (CONFIG_ERROR_RESILIENCE && h->enable_er) {
549 26074 ff_er_frame_start(&h->er);
550 26074 ff_h264_set_erpic(&h->er.last_pic, NULL);
551 26074 ff_h264_set_erpic(&h->er.next_pic, NULL);
552 }
553
554
2/2
✓ Branch 0 taken 421984 times.
✓ Branch 1 taken 26374 times.
448358 for (i = 0; i < 16; i++) {
555 421984 h->block_offset[i] = (4 * ((scan8[i] - scan8[0]) & 7) << pixel_shift) + 4 * pic->f->linesize[0] * ((scan8[i] - scan8[0]) >> 3);
556 421984 h->block_offset[48 + i] = (4 * ((scan8[i] - scan8[0]) & 7) << pixel_shift) + 8 * pic->f->linesize[0] * ((scan8[i] - scan8[0]) >> 3);
557 }
558
2/2
✓ Branch 0 taken 421984 times.
✓ Branch 1 taken 26374 times.
448358 for (i = 0; i < 16; i++) {
559 421984 h->block_offset[16 + i] =
560 421984 h->block_offset[32 + i] = (4 * ((scan8[i] - scan8[0]) & 7) << pixel_shift) + 4 * pic->f->linesize[1] * ((scan8[i] - scan8[0]) >> 3);
561 421984 h->block_offset[48 + 16 + i] =
562 421984 h->block_offset[48 + 32 + i] = (4 * ((scan8[i] - scan8[0]) & 7) << pixel_shift) + 8 * pic->f->linesize[1] * ((scan8[i] - scan8[0]) >> 3);
563 }
564
565 /* We mark the current picture as non-reference after allocating it, so
566 * that if we break out due to an error it can be released automatically
567 * in the next ff_mpv_frame_start().
568 */
569 26374 h->cur_pic_ptr->reference = 0;
570
571 26374 h->cur_pic_ptr->field_poc[0] = h->cur_pic_ptr->field_poc[1] = INT_MAX;
572
573 26374 h->next_output_pic = NULL;
574
575 26374 h->postpone_filter = 0;
576
577
4/4
✓ Branch 0 taken 2871 times.
✓ Branch 1 taken 23503 times.
✓ Branch 2 taken 2358 times.
✓ Branch 3 taken 513 times.
26374 h->mb_aff_frame = h->ps.sps->mb_aff && (h->picture_structure == PICT_FRAME);
578
579
2/2
✓ Branch 0 taken 2118 times.
✓ Branch 1 taken 24256 times.
26374 if (h->sei.common.unregistered.x264_build >= 0)
580 2118 h->x264_build = h->sei.common.unregistered.x264_build;
581
582 assert(h->cur_pic_ptr->long_ref == 0);
583
584 26374 return 0;
585 }
586
587 13001858 static av_always_inline void backup_mb_border(const H264Context *h, H264SliceContext *sl,
588 const uint8_t *src_y,
589 const uint8_t *src_cb, const uint8_t *src_cr,
590 int linesize, int uvlinesize,
591 int simple)
592 {
593 uint8_t *top_border;
594 13001858 int top_idx = 1;
595 13001858 const int pixel_shift = h->pixel_shift;
596 13001858 int chroma444 = CHROMA444(h);
597 13001858 int chroma422 = CHROMA422(h);
598
599 13001858 src_y -= linesize;
600 13001858 src_cb -= uvlinesize;
601 13001858 src_cr -= uvlinesize;
602
603
3/4
✓ Branch 0 taken 13001858 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2023440 times.
✓ Branch 3 taken 10978418 times.
13001858 if (!simple && FRAME_MBAFF(h)) {
604
2/2
✓ Branch 0 taken 1011720 times.
✓ Branch 1 taken 1011720 times.
2023440 if (sl->mb_y & 1) {
605
2/2
✓ Branch 0 taken 748050 times.
✓ Branch 1 taken 263670 times.
1011720 if (!MB_MBAFF(sl)) {
606 748050 top_border = sl->top_borders[0][sl->mb_x];
607 748050 AV_COPY128(top_border, src_y + 15 * linesize);
608
2/2
✓ Branch 0 taken 213139 times.
✓ Branch 1 taken 534911 times.
748050 if (pixel_shift)
609 213139 AV_COPY128(top_border + 16, src_y + 15 * linesize + 16);
610 if (simple || !CONFIG_GRAY || !(h->flags & AV_CODEC_FLAG_GRAY)) {
611
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 748050 times.
748050 if (chroma444) {
612 if (pixel_shift) {
613 AV_COPY128(top_border + 32, src_cb + 15 * uvlinesize);
614 AV_COPY128(top_border + 48, src_cb + 15 * uvlinesize + 16);
615 AV_COPY128(top_border + 64, src_cr + 15 * uvlinesize);
616 AV_COPY128(top_border + 80, src_cr + 15 * uvlinesize + 16);
617 } else {
618 AV_COPY128(top_border + 16, src_cb + 15 * uvlinesize);
619 AV_COPY128(top_border + 32, src_cr + 15 * uvlinesize);
620 }
621
2/2
✓ Branch 0 taken 106510 times.
✓ Branch 1 taken 641540 times.
748050 } else if (chroma422) {
622
1/2
✓ Branch 0 taken 106510 times.
✗ Branch 1 not taken.
106510 if (pixel_shift) {
623 106510 AV_COPY128(top_border + 32, src_cb + 15 * uvlinesize);
624 106510 AV_COPY128(top_border + 48, src_cr + 15 * uvlinesize);
625 } else {
626 AV_COPY64(top_border + 16, src_cb + 15 * uvlinesize);
627 AV_COPY64(top_border + 24, src_cr + 15 * uvlinesize);
628 }
629 } else {
630
2/2
✓ Branch 0 taken 106629 times.
✓ Branch 1 taken 534911 times.
641540 if (pixel_shift) {
631 106629 AV_COPY128(top_border + 32, src_cb + 7 * uvlinesize);
632 106629 AV_COPY128(top_border + 48, src_cr + 7 * uvlinesize);
633 } else {
634 534911 AV_COPY64(top_border + 16, src_cb + 7 * uvlinesize);
635 534911 AV_COPY64(top_border + 24, src_cr + 7 * uvlinesize);
636 }
637 }
638 }
639 }
640
2/2
✓ Branch 0 taken 263670 times.
✓ Branch 1 taken 748050 times.
1011720 } else if (MB_MBAFF(sl)) {
641 263670 top_idx = 0;
642 } else
643 748050 return;
644 }
645
646 12253808 top_border = sl->top_borders[top_idx][sl->mb_x];
647 /* There are two lines saved, the line above the top macroblock
648 * of a pair, and the line above the bottom macroblock. */
649 12253808 AV_COPY128(top_border, src_y + 16 * linesize);
650
2/2
✓ Branch 0 taken 1389541 times.
✓ Branch 1 taken 10864267 times.
12253808 if (pixel_shift)
651 1389541 AV_COPY128(top_border + 16, src_y + 16 * linesize + 16);
652
653 if (simple || !CONFIG_GRAY || !(h->flags & AV_CODEC_FLAG_GRAY)) {
654
2/2
✓ Branch 0 taken 80328 times.
✓ Branch 1 taken 12173480 times.
12253808 if (chroma444) {
655
2/2
✓ Branch 0 taken 19800 times.
✓ Branch 1 taken 60528 times.
80328 if (pixel_shift) {
656 19800 AV_COPY128(top_border + 32, src_cb + 16 * linesize);
657 19800 AV_COPY128(top_border + 48, src_cb + 16 * linesize + 16);
658 19800 AV_COPY128(top_border + 64, src_cr + 16 * linesize);
659 19800 AV_COPY128(top_border + 80, src_cr + 16 * linesize + 16);
660 } else {
661 60528 AV_COPY128(top_border + 16, src_cb + 16 * linesize);
662 60528 AV_COPY128(top_border + 32, src_cr + 16 * linesize);
663 }
664
2/2
✓ Branch 0 taken 588635 times.
✓ Branch 1 taken 11584845 times.
12173480 } else if (chroma422) {
665
2/2
✓ Branch 0 taken 584675 times.
✓ Branch 1 taken 3960 times.
588635 if (pixel_shift) {
666 584675 AV_COPY128(top_border + 32, src_cb + 16 * uvlinesize);
667 584675 AV_COPY128(top_border + 48, src_cr + 16 * uvlinesize);
668 } else {
669 3960 AV_COPY64(top_border + 16, src_cb + 16 * uvlinesize);
670 3960 AV_COPY64(top_border + 24, src_cr + 16 * uvlinesize);
671 }
672 } else {
673
2/2
✓ Branch 0 taken 785066 times.
✓ Branch 1 taken 10799779 times.
11584845 if (pixel_shift) {
674 785066 AV_COPY128(top_border + 32, src_cb + 8 * uvlinesize);
675 785066 AV_COPY128(top_border + 48, src_cr + 8 * uvlinesize);
676 } else {
677 10799779 AV_COPY64(top_border + 16, src_cb + 8 * uvlinesize);
678 10799779 AV_COPY64(top_border + 24, src_cr + 8 * uvlinesize);
679 }
680 }
681 }
682 }
683
684 /**
685 * Initialize implicit_weight table.
686 * @param field 0/1 initialize the weight for interlaced MBAFF
687 * -1 initializes the rest
688 */
689 4084 static void implicit_weight_table(const H264Context *h, H264SliceContext *sl, int field)
690 {
691 int ref0, ref1, i, cur_poc, ref_start, ref_count0, ref_count1;
692
693
2/2
✓ Branch 0 taken 8168 times.
✓ Branch 1 taken 4084 times.
12252 for (i = 0; i < 2; i++) {
694 8168 sl->pwt.luma_weight_flag[i] = 0;
695 8168 sl->pwt.chroma_weight_flag[i] = 0;
696 }
697
698
2/2
✓ Branch 0 taken 2372 times.
✓ Branch 1 taken 1712 times.
4084 if (field < 0) {
699
1/2
✓ Branch 0 taken 2372 times.
✗ Branch 1 not taken.
2372 if (h->picture_structure == PICT_FRAME) {
700 2372 cur_poc = h->cur_pic_ptr->poc;
701 } else {
702 cur_poc = h->cur_pic_ptr->field_poc[h->picture_structure - 1];
703 }
704
6/6
✓ Branch 0 taken 1145 times.
✓ Branch 1 taken 1227 times.
✓ Branch 2 taken 828 times.
✓ Branch 3 taken 317 times.
✓ Branch 4 taken 818 times.
✓ Branch 5 taken 10 times.
2372 if (sl->ref_count[0] == 1 && sl->ref_count[1] == 1 && !FRAME_MBAFF(h) &&
705
2/2
✓ Branch 0 taken 543 times.
✓ Branch 1 taken 275 times.
818 sl->ref_list[0][0].poc + (int64_t)sl->ref_list[1][0].poc == 2LL * cur_poc) {
706 543 sl->pwt.use_weight = 0;
707 543 sl->pwt.use_weight_chroma = 0;
708 543 return;
709 }
710 1829 ref_start = 0;
711 1829 ref_count0 = sl->ref_count[0];
712 1829 ref_count1 = sl->ref_count[1];
713 } else {
714 1712 cur_poc = h->cur_pic_ptr->field_poc[field];
715 1712 ref_start = 16;
716 1712 ref_count0 = 16 + 2 * sl->ref_count[0];
717 1712 ref_count1 = 16 + 2 * sl->ref_count[1];
718 }
719
720 3541 sl->pwt.use_weight = 2;
721 3541 sl->pwt.use_weight_chroma = 2;
722 3541 sl->pwt.luma_log2_weight_denom = 5;
723 3541 sl->pwt.chroma_log2_weight_denom = 5;
724
725
2/2
✓ Branch 0 taken 12010 times.
✓ Branch 1 taken 3541 times.
15551 for (ref0 = ref_start; ref0 < ref_count0; ref0++) {
726 12010 int64_t poc0 = sl->ref_list[0][ref0].poc;
727
2/2
✓ Branch 0 taken 22157 times.
✓ Branch 1 taken 12010 times.
34167 for (ref1 = ref_start; ref1 < ref_count1; ref1++) {
728 22157 int w = 32;
729
2/4
✓ Branch 0 taken 22157 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 22157 times.
✗ Branch 3 not taken.
22157 if (!sl->ref_list[0][ref0].parent->long_ref && !sl->ref_list[1][ref1].parent->long_ref) {
730 22157 int poc1 = sl->ref_list[1][ref1].poc;
731 22157 int td = av_clip_int8(poc1 - poc0);
732
2/2
✓ Branch 0 taken 22090 times.
✓ Branch 1 taken 67 times.
22157 if (td) {
733 22090 int tb = av_clip_int8(cur_poc - poc0);
734 22090 int tx = (16384 + (FFABS(td) >> 1)) / td;
735 22090 int dist_scale_factor = (tb * tx + 32) >> 8;
736
3/4
✓ Branch 0 taken 22078 times.
✓ Branch 1 taken 12 times.
✓ Branch 2 taken 22078 times.
✗ Branch 3 not taken.
22090 if (dist_scale_factor >= -64 && dist_scale_factor <= 128)
737 22078 w = 64 - dist_scale_factor;
738 }
739 }
740
2/2
✓ Branch 0 taken 6253 times.
✓ Branch 1 taken 15904 times.
22157 if (field < 0) {
741 6253 sl->pwt.implicit_weight[ref0][ref1][0] =
742 6253 sl->pwt.implicit_weight[ref0][ref1][1] = w;
743 } else {
744 15904 sl->pwt.implicit_weight[ref0][ref1][field] = w;
745 }
746 }
747 }
748 }
749
750 /**
751 * initialize scan tables
752 */
753 631 static void init_scan_tables(H264Context *h)
754 {
755 int i;
756
2/2
✓ Branch 0 taken 10096 times.
✓ Branch 1 taken 631 times.
10727 for (i = 0; i < 16; i++) {
757 #define TRANSPOSE(x) ((x) >> 2) | (((x) << 2) & 0xF)
758 10096 h->zigzag_scan[i] = TRANSPOSE(ff_zigzag_scan[i]);
759 10096 h->field_scan[i] = TRANSPOSE(field_scan[i]);
760 #undef TRANSPOSE
761 }
762
2/2
✓ Branch 0 taken 40384 times.
✓ Branch 1 taken 631 times.
41015 for (i = 0; i < 64; i++) {
763 #define TRANSPOSE(x) ((x) >> 3) | (((x) & 7) << 3)
764 40384 h->zigzag_scan8x8[i] = TRANSPOSE(ff_zigzag_direct[i]);
765 40384 h->zigzag_scan8x8_cavlc[i] = TRANSPOSE(zigzag_scan8x8_cavlc[i]);
766 40384 h->field_scan8x8[i] = TRANSPOSE(field_scan8x8[i]);
767 40384 h->field_scan8x8_cavlc[i] = TRANSPOSE(field_scan8x8_cavlc[i]);
768 #undef TRANSPOSE
769 }
770
2/2
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 617 times.
631 if (h->ps.sps->transform_bypass) { // FIXME same ugly
771 14 memcpy(h->zigzag_scan_q0 , ff_zigzag_scan , sizeof(h->zigzag_scan_q0 ));
772 14 memcpy(h->zigzag_scan8x8_q0 , ff_zigzag_direct , sizeof(h->zigzag_scan8x8_q0 ));
773 14 memcpy(h->zigzag_scan8x8_cavlc_q0 , zigzag_scan8x8_cavlc , sizeof(h->zigzag_scan8x8_cavlc_q0));
774 14 memcpy(h->field_scan_q0 , field_scan , sizeof(h->field_scan_q0 ));
775 14 memcpy(h->field_scan8x8_q0 , field_scan8x8 , sizeof(h->field_scan8x8_q0 ));
776 14 memcpy(h->field_scan8x8_cavlc_q0 , field_scan8x8_cavlc , sizeof(h->field_scan8x8_cavlc_q0 ));
777 } else {
778 617 memcpy(h->zigzag_scan_q0 , h->zigzag_scan , sizeof(h->zigzag_scan_q0 ));
779 617 memcpy(h->zigzag_scan8x8_q0 , h->zigzag_scan8x8 , sizeof(h->zigzag_scan8x8_q0 ));
780 617 memcpy(h->zigzag_scan8x8_cavlc_q0 , h->zigzag_scan8x8_cavlc , sizeof(h->zigzag_scan8x8_cavlc_q0));
781 617 memcpy(h->field_scan_q0 , h->field_scan , sizeof(h->field_scan_q0 ));
782 617 memcpy(h->field_scan8x8_q0 , h->field_scan8x8 , sizeof(h->field_scan8x8_q0 ));
783 617 memcpy(h->field_scan8x8_cavlc_q0 , h->field_scan8x8_cavlc , sizeof(h->field_scan8x8_cavlc_q0 ));
784 }
785 631 }
786
787 29369 static enum AVPixelFormat get_pixel_format(H264Context *h, int force_callback)
788 {
789 #define HWACCEL_MAX (CONFIG_H264_DXVA2_HWACCEL + \
790 (CONFIG_H264_D3D11VA_HWACCEL * 2) + \
791 CONFIG_H264_D3D12VA_HWACCEL + \
792 CONFIG_H264_NVDEC_HWACCEL + \
793 CONFIG_H264_VAAPI_HWACCEL + \
794 CONFIG_H264_VIDEOTOOLBOX_HWACCEL + \
795 CONFIG_H264_VDPAU_HWACCEL + \
796 CONFIG_H264_VULKAN_HWACCEL)
797 29369 enum AVPixelFormat pix_fmts[HWACCEL_MAX + 2], *fmt = pix_fmts;
798
799
3/6
✓ Branch 0 taken 155 times.
✓ Branch 1 taken 373 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 28841 times.
✗ Branch 5 not taken.
29369 switch (h->ps.sps->bit_depth_luma) {
800 155 case 9:
801
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 155 times.
155 if (CHROMA444(h)) {
802 if (h->avctx->colorspace == AVCOL_SPC_RGB) {
803 *fmt++ = AV_PIX_FMT_GBRP9;
804 } else
805 *fmt++ = AV_PIX_FMT_YUV444P9;
806
2/2
✓ Branch 0 taken 52 times.
✓ Branch 1 taken 103 times.
155 } else if (CHROMA422(h))
807 52 *fmt++ = AV_PIX_FMT_YUV422P9;
808 else
809 103 *fmt++ = AV_PIX_FMT_YUV420P9;
810 155 break;
811 373 case 10:
812 #if CONFIG_H264_VIDEOTOOLBOX_HWACCEL
813 if (h->avctx->colorspace != AVCOL_SPC_RGB)
814 *fmt++ = AV_PIX_FMT_VIDEOTOOLBOX;
815 #endif
816 #if CONFIG_H264_VULKAN_HWACCEL
817 *fmt++ = AV_PIX_FMT_VULKAN;
818 #endif
819 #if CONFIG_H264_NVDEC_HWACCEL
820 *fmt++ = AV_PIX_FMT_CUDA;
821 #endif
822
2/2
✓ Branch 0 taken 51 times.
✓ Branch 1 taken 322 times.
373 if (CHROMA444(h)) {
823
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 51 times.
51 if (h->avctx->colorspace == AVCOL_SPC_RGB) {
824 *fmt++ = AV_PIX_FMT_GBRP10;
825 } else
826 51 *fmt++ = AV_PIX_FMT_YUV444P10;
827
2/2
✓ Branch 0 taken 164 times.
✓ Branch 1 taken 158 times.
322 } else if (CHROMA422(h))
828 164 *fmt++ = AV_PIX_FMT_YUV422P10;
829 else {
830 #if CONFIG_H264_VAAPI_HWACCEL
831 // Just add as candidate. Whether VAProfileH264High10 usable or
832 // not is decided by vaapi_decode_make_config() defined in FFmpeg
833 // and vaQueryCodingProfile() defined in libva.
834 158 *fmt++ = AV_PIX_FMT_VAAPI;
835 #endif
836 158 *fmt++ = AV_PIX_FMT_YUV420P10;
837 }
838 373 break;
839 case 12:
840 #if CONFIG_H264_VULKAN_HWACCEL
841 *fmt++ = AV_PIX_FMT_VULKAN;
842 #endif
843 if (CHROMA444(h)) {
844 if (h->avctx->colorspace == AVCOL_SPC_RGB) {
845 *fmt++ = AV_PIX_FMT_GBRP12;
846 } else
847 *fmt++ = AV_PIX_FMT_YUV444P12;
848 } else if (CHROMA422(h))
849 *fmt++ = AV_PIX_FMT_YUV422P12;
850 else
851 *fmt++ = AV_PIX_FMT_YUV420P12;
852 break;
853 case 14:
854 if (CHROMA444(h)) {
855 if (h->avctx->colorspace == AVCOL_SPC_RGB) {
856 *fmt++ = AV_PIX_FMT_GBRP14;
857 } else
858 *fmt++ = AV_PIX_FMT_YUV444P14;
859 } else if (CHROMA422(h))
860 *fmt++ = AV_PIX_FMT_YUV422P14;
861 else
862 *fmt++ = AV_PIX_FMT_YUV420P14;
863 break;
864 28841 case 8:
865 #if CONFIG_H264_VDPAU_HWACCEL
866 28841 *fmt++ = AV_PIX_FMT_VDPAU;
867 #endif
868 #if CONFIG_H264_VULKAN_HWACCEL
869 *fmt++ = AV_PIX_FMT_VULKAN;
870 #endif
871 #if CONFIG_H264_NVDEC_HWACCEL
872 *fmt++ = AV_PIX_FMT_CUDA;
873 #endif
874 #if CONFIG_H264_VIDEOTOOLBOX_HWACCEL
875 if (h->avctx->colorspace != AVCOL_SPC_RGB)
876 *fmt++ = AV_PIX_FMT_VIDEOTOOLBOX;
877 #endif
878
2/2
✓ Branch 0 taken 484 times.
✓ Branch 1 taken 28357 times.
28841 if (CHROMA444(h)) {
879
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 484 times.
484 if (h->avctx->colorspace == AVCOL_SPC_RGB)
880 *fmt++ = AV_PIX_FMT_GBRP;
881
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 482 times.
484 else if (h->avctx->color_range == AVCOL_RANGE_JPEG)
882 2 *fmt++ = AV_PIX_FMT_YUVJ444P;
883 else
884 482 *fmt++ = AV_PIX_FMT_YUV444P;
885
2/2
✓ Branch 0 taken 22 times.
✓ Branch 1 taken 28335 times.
28357 } else if (CHROMA422(h)) {
886
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 22 times.
22 if (h->avctx->color_range == AVCOL_RANGE_JPEG)
887 *fmt++ = AV_PIX_FMT_YUVJ422P;
888 else
889 22 *fmt++ = AV_PIX_FMT_YUV422P;
890 } else {
891 #if CONFIG_H264_DXVA2_HWACCEL
892 *fmt++ = AV_PIX_FMT_DXVA2_VLD;
893 #endif
894 #if CONFIG_H264_D3D11VA_HWACCEL
895 *fmt++ = AV_PIX_FMT_D3D11VA_VLD;
896 *fmt++ = AV_PIX_FMT_D3D11;
897 #endif
898 #if CONFIG_H264_D3D12VA_HWACCEL
899 *fmt++ = AV_PIX_FMT_D3D12;
900 #endif
901 #if CONFIG_H264_VAAPI_HWACCEL
902 28335 *fmt++ = AV_PIX_FMT_VAAPI;
903 #endif
904
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 28332 times.
28335 if (h->avctx->color_range == AVCOL_RANGE_JPEG)
905 3 *fmt++ = AV_PIX_FMT_YUVJ420P;
906 else
907 28332 *fmt++ = AV_PIX_FMT_YUV420P;
908 }
909 28841 break;
910 default:
911 av_log(h->avctx, AV_LOG_ERROR,
912 "Unsupported bit depth %d\n", h->ps.sps->bit_depth_luma);
913 return AVERROR_INVALIDDATA;
914 }
915
916 29369 *fmt = AV_PIX_FMT_NONE;
917
918
2/2
✓ Branch 0 taken 86703 times.
✓ Branch 1 taken 615 times.
87318 for (int i = 0; pix_fmts[i] != AV_PIX_FMT_NONE; i++)
919
4/4
✓ Branch 0 taken 29014 times.
✓ Branch 1 taken 57689 times.
✓ Branch 2 taken 28754 times.
✓ Branch 3 taken 260 times.
86703 if (pix_fmts[i] == h->avctx->pix_fmt && !force_callback)
920 28754 return pix_fmts[i];
921 615 return ff_get_format(h->avctx, pix_fmts);
922 }
923
924 /* export coded and cropped frame dimensions to AVCodecContext */
925 29103 static void init_dimensions(H264Context *h)
926 {
927 29103 const SPS *sps = h->ps.sps;
928 29103 int cr = sps->crop_right;
929 29103 int cl = sps->crop_left;
930 29103 int ct = sps->crop_top;
931 29103 int cb = sps->crop_bottom;
932 29103 int width = h->width - (cr + cl);
933 29103 int height = h->height - (ct + cb);
934
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 29103 times.
29103 av_assert0(sps->crop_right + sps->crop_left < (unsigned)h->width);
935
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 29103 times.
29103 av_assert0(sps->crop_top + sps->crop_bottom < (unsigned)h->height);
936
937 /* handle container cropping */
938
3/4
✓ Branch 0 taken 26836 times.
✓ Branch 1 taken 2267 times.
✓ Branch 2 taken 26836 times.
✗ Branch 3 not taken.
29103 if (h->width_from_caller > 0 && h->height_from_caller > 0 &&
939
3/4
✓ Branch 0 taken 26835 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 26835 times.
✗ Branch 3 not taken.
26836 !sps->crop_top && !sps->crop_left &&
940
2/2
✓ Branch 0 taken 26824 times.
✓ Branch 1 taken 11 times.
26835 FFALIGN(h->width_from_caller, 16) == FFALIGN(width, 16) &&
941
1/2
✓ Branch 0 taken 26824 times.
✗ Branch 1 not taken.
26824 FFALIGN(h->height_from_caller, 16) == FFALIGN(height, 16) &&
942
2/2
✓ Branch 0 taken 26823 times.
✓ Branch 1 taken 1 times.
26824 h->width_from_caller <= width &&
943
2/2
✓ Branch 0 taken 26815 times.
✓ Branch 1 taken 8 times.
26823 h->height_from_caller <= height) {
944 26815 width = h->width_from_caller;
945 26815 height = h->height_from_caller;
946 26815 cl = 0;
947 26815 ct = 0;
948 26815 cr = h->width - width;
949 26815 cb = h->height - height;
950 } else {
951 2288 h->width_from_caller = 0;
952 2288 h->height_from_caller = 0;
953 }
954
955 29103 h->avctx->coded_width = h->width;
956 29103 h->avctx->coded_height = h->height;
957 29103 h->avctx->width = width;
958 29103 h->avctx->height = height;
959 29103 h->crop_right = cr;
960 29103 h->crop_left = cl;
961 29103 h->crop_top = ct;
962 29103 h->crop_bottom = cb;
963 29103 }
964
965 631 static int h264_slice_header_init(H264Context *h)
966 {
967 631 const SPS *sps = h->ps.sps;
968 int i, ret;
969
970
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 631 times.
631 if (!sps) {
971 ret = AVERROR_INVALIDDATA;
972 goto fail;
973 }
974
975 631 ff_set_sar(h->avctx, sps->vui.sar);
976 631 av_pix_fmt_get_chroma_sub_sample(h->avctx->pix_fmt,
977 &h->chroma_x_shift, &h->chroma_y_shift);
978
979
2/2
✓ Branch 0 taken 211 times.
✓ Branch 1 taken 420 times.
631 if (sps->timing_info_present_flag) {
980 211 int64_t den = sps->time_scale;
981
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 211 times.
211 if (h->x264_build < 44U)
982 den *= 2;
983 211 av_reduce(&h->avctx->framerate.den, &h->avctx->framerate.num,
984 211 sps->num_units_in_tick * 2, den, 1 << 30);
985 }
986
987 631 ff_h264_free_tables(h);
988
989 631 h->first_field = 0;
990 631 h->prev_interlaced_frame = 1;
991
992 631 init_scan_tables(h);
993 631 ret = ff_h264_alloc_tables(h);
994
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 631 times.
631 if (ret < 0) {
995 av_log(h->avctx, AV_LOG_ERROR, "Could not allocate memory\n");
996 goto fail;
997 }
998
999
2/4
✓ Branch 0 taken 631 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 631 times.
✗ Branch 3 not taken.
631 if (sps->bit_depth_luma < 8 || sps->bit_depth_luma > 14 ||
1000
2/4
✓ Branch 0 taken 631 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 631 times.
631 sps->bit_depth_luma == 11 || sps->bit_depth_luma == 13
1001 ) {
1002 av_log(h->avctx, AV_LOG_ERROR, "Unsupported bit depth %d\n",
1003 sps->bit_depth_luma);
1004 ret = AVERROR_INVALIDDATA;
1005 goto fail;
1006 }
1007
1008 631 h->cur_bit_depth_luma =
1009 631 h->avctx->bits_per_raw_sample = sps->bit_depth_luma;
1010 631 h->cur_chroma_format_idc = sps->chroma_format_idc;
1011 631 h->pixel_shift = sps->bit_depth_luma > 8;
1012 631 h->chroma_format_idc = sps->chroma_format_idc;
1013 631 h->bit_depth_luma = sps->bit_depth_luma;
1014
1015 631 ff_h264dsp_init(&h->h264dsp, sps->bit_depth_luma,
1016 631 sps->chroma_format_idc);
1017 631 ff_h264chroma_init(&h->h264chroma, sps->bit_depth_chroma);
1018 631 ff_h264qpel_init(&h->h264qpel, sps->bit_depth_luma);
1019 631 ff_h264_pred_init(&h->hpc, AV_CODEC_ID_H264, sps->bit_depth_luma,
1020 631 sps->chroma_format_idc);
1021 631 ff_videodsp_init(&h->vdsp, sps->bit_depth_luma);
1022
1023
2/2
✓ Branch 0 taken 630 times.
✓ Branch 1 taken 1 times.
631 if (!HAVE_THREADS || !(h->avctx->active_thread_type & FF_THREAD_SLICE)) {
1024 630 ff_h264_slice_context_init(h, &h->slice_ctx[0]);
1025 } else {
1026
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 times.
3 for (i = 0; i < h->nb_slice_ctx; i++) {
1027 2 H264SliceContext *sl = &h->slice_ctx[i];
1028
1029 2 sl->h264 = h;
1030 2 sl->intra4x4_pred_mode = h->intra4x4_pred_mode + i * 8 * 2 * h->mb_stride;
1031 2 sl->mvd_table[0] = h->mvd_table[0] + i * 8 * 2 * h->mb_stride;
1032 2 sl->mvd_table[1] = h->mvd_table[1] + i * 8 * 2 * h->mb_stride;
1033
1034 2 ff_h264_slice_context_init(h, sl);
1035 }
1036 }
1037
1038 631 h->context_initialized = 1;
1039
1040 631 return 0;
1041 fail:
1042 ff_h264_free_tables(h);
1043 h->context_initialized = 0;
1044 return ret;
1045 }
1046
1047 57508 static enum AVPixelFormat non_j_pixfmt(enum AVPixelFormat a)
1048 {
1049
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 57506 times.
57508 switch (a) {
1050 2 case AV_PIX_FMT_YUVJ420P: return AV_PIX_FMT_YUV420P;
1051 case AV_PIX_FMT_YUVJ422P: return AV_PIX_FMT_YUV422P;
1052 case AV_PIX_FMT_YUVJ444P: return AV_PIX_FMT_YUV444P;
1053 57506 default:
1054 57506 return a;
1055 }
1056 }
1057
1058 29103 static int h264_init_ps(H264Context *h, const H264SliceContext *sl, int first_slice)
1059 {
1060 const SPS *sps;
1061 29103 int needs_reinit = 0, must_reinit, ret;
1062
1063
2/2
✓ Branch 0 taken 28908 times.
✓ Branch 1 taken 195 times.
29103 if (first_slice)
1064 28908 av_refstruct_replace(&h->ps.pps, h->ps.pps_list[sl->pps_id]);
1065
1066
2/2
✓ Branch 0 taken 617 times.
✓ Branch 1 taken 28486 times.
29103 if (h->ps.sps != h->ps.pps->sps) {
1067 617 h->ps.sps = h->ps.pps->sps;
1068
1069
2/2
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 610 times.
617 if (h->mb_width != h->ps.sps->mb_width ||
1070
1/2
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
7 h->mb_height != h->ps.sps->mb_height ||
1071
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 1 times.
7 h->cur_bit_depth_luma != h->ps.sps->bit_depth_luma ||
1072
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 5 times.
6 h->cur_chroma_format_idc != h->ps.sps->chroma_format_idc
1073 )
1074 612 needs_reinit = 1;
1075
1076
2/2
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 606 times.
617 if (h->bit_depth_luma != h->ps.sps->bit_depth_luma ||
1077
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 10 times.
11 h->chroma_format_idc != h->ps.sps->chroma_format_idc)
1078 607 needs_reinit = 1;
1079 }
1080 29103 sps = h->ps.sps;
1081
1082
2/2
✓ Branch 0 taken 28496 times.
✓ Branch 1 taken 607 times.
57599 must_reinit = (h->context_initialized &&
1083
2/2
✓ Branch 0 taken 28490 times.
✓ Branch 1 taken 6 times.
28496 ( 16*sps->mb_width != h->avctx->coded_width
1084
1/2
✓ Branch 0 taken 28490 times.
✗ Branch 1 not taken.
28490 || 16*sps->mb_height != h->avctx->coded_height
1085
2/2
✓ Branch 0 taken 28489 times.
✓ Branch 1 taken 1 times.
28490 || h->cur_bit_depth_luma != sps->bit_depth_luma
1086
2/2
✓ Branch 0 taken 28488 times.
✓ Branch 1 taken 1 times.
28489 || h->cur_chroma_format_idc != sps->chroma_format_idc
1087
1/2
✓ Branch 0 taken 28488 times.
✗ Branch 1 not taken.
28488 || h->mb_width != sps->mb_width
1088
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 28488 times.
28488 || h->mb_height != sps->mb_height
1089 ));
1090
2/2
✓ Branch 0 taken 28754 times.
✓ Branch 1 taken 349 times.
29103 if (h->avctx->pix_fmt == AV_PIX_FMT_NONE
1091
2/2
✓ Branch 3 taken 3 times.
✓ Branch 4 taken 28751 times.
28754 || (non_j_pixfmt(h->avctx->pix_fmt) != non_j_pixfmt(get_pixel_format(h, 0))))
1092 352 must_reinit = 1;
1093
1094
4/4
✓ Branch 0 taken 28908 times.
✓ Branch 1 taken 195 times.
✓ Branch 3 taken 89 times.
✓ Branch 4 taken 28819 times.
29103 if (first_slice && av_cmp_q(sps->vui.sar, h->avctx->sample_aspect_ratio))
1095 89 must_reinit = 1;
1096
1097
1/2
✓ Branch 0 taken 29103 times.
✗ Branch 1 not taken.
29103 if (!h->setup_finished) {
1098 29103 h->avctx->profile = ff_h264_get_profile(sps);
1099 29103 h->avctx->level = sps->level_idc;
1100 29103 h->avctx->refs = sps->ref_frame_count;
1101
1102 29103 h->mb_width = sps->mb_width;
1103 29103 h->mb_height = sps->mb_height;
1104 29103 h->mb_num = h->mb_width * h->mb_height;
1105 29103 h->mb_stride = h->mb_width + 1;
1106
1107 29103 h->b_stride = h->mb_width * 4;
1108
1109 29103 h->chroma_y_shift = sps->chroma_format_idc <= 1; // 400 uses yuv420p
1110
1111 29103 h->width = 16 * h->mb_width;
1112 29103 h->height = 16 * h->mb_height;
1113
1114 29103 init_dimensions(h);
1115
1116
2/2
✓ Branch 0 taken 1288 times.
✓ Branch 1 taken 27815 times.
29103 if (sps->vui.video_signal_type_present_flag) {
1117 2576 h->avctx->color_range = sps->vui.video_full_range_flag > 0 ? AVCOL_RANGE_JPEG
1118
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1286 times.
1288 : AVCOL_RANGE_MPEG;
1119
2/2
✓ Branch 0 taken 1190 times.
✓ Branch 1 taken 98 times.
1288 if (sps->vui.colour_description_present_flag) {
1120
2/2
✓ Branch 0 taken 19 times.
✓ Branch 1 taken 1171 times.
1190 if (h->avctx->colorspace != sps->vui.matrix_coeffs)
1121 19 needs_reinit = 1;
1122 1190 h->avctx->color_primaries = sps->vui.colour_primaries;
1123 1190 h->avctx->color_trc = sps->vui.transfer_characteristics;
1124 1190 h->avctx->colorspace = sps->vui.matrix_coeffs;
1125 }
1126 }
1127
1128
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 29103 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
29103 if (h->sei.common.alternative_transfer.present &&
1129 av_color_transfer_name(h->sei.common.alternative_transfer.preferred_transfer_characteristics) &&
1130 h->sei.common.alternative_transfer.preferred_transfer_characteristics != AVCOL_TRC_UNSPECIFIED) {
1131 h->avctx->color_trc = h->sei.common.alternative_transfer.preferred_transfer_characteristics;
1132 }
1133 }
1134 29103 h->avctx->chroma_sample_location = sps->vui.chroma_location;
1135
1136
5/6
✓ Branch 0 taken 28496 times.
✓ Branch 1 taken 607 times.
✓ Branch 2 taken 28488 times.
✓ Branch 3 taken 8 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 28488 times.
29103 if (!h->context_initialized || must_reinit || needs_reinit) {
1137 615 int flush_changes = h->context_initialized;
1138 615 h->context_initialized = 0;
1139
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 615 times.
615 if (sl != h->slice_ctx) {
1140 av_log(h->avctx, AV_LOG_ERROR,
1141 "changing width %d -> %d / height %d -> %d on "
1142 "slice %d\n",
1143 h->width, h->avctx->coded_width,
1144 h->height, h->avctx->coded_height,
1145 h->current_slice + 1);
1146 return AVERROR_INVALIDDATA;
1147 }
1148
1149 av_assert1(first_slice);
1150
1151
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 607 times.
615 if (flush_changes)
1152 8 ff_h264_flush_change(h);
1153
1154
5/6
✓ Branch 0 taken 255 times.
✓ Branch 1 taken 360 times.
✓ Branch 2 taken 252 times.
✓ Branch 3 taken 3 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 615 times.
615 if ((ret = get_pixel_format(h, must_reinit || needs_reinit)) < 0)
1155 return ret;
1156 615 h->avctx->pix_fmt = ret;
1157
1158 615 av_log(h->avctx, AV_LOG_VERBOSE, "Reinit context to %dx%d, "
1159 615 "pix_fmt: %s\n", h->width, h->height, av_get_pix_fmt_name(h->avctx->pix_fmt));
1160
1161
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 615 times.
615 if ((ret = h264_slice_header_init(h)) < 0) {
1162 av_log(h->avctx, AV_LOG_ERROR,
1163 "h264_slice_header_init() failed\n");
1164 return ret;
1165 }
1166 }
1167
1168 29103 return 0;
1169 }
1170
1171 25679 static int h264_export_frame_props(H264Context *h)
1172 {
1173 25679 const SPS *sps = h->ps.sps;
1174 25679 H264Picture *cur = h->cur_pic_ptr;
1175 25679 AVFrame *out = cur->f;
1176 25679 int interlaced_frame = 0, top_field_first = 0;
1177 int ret;
1178
1179 25679 out->flags &= ~AV_FRAME_FLAG_INTERLACED;
1180 25679 out->repeat_pict = 0;
1181
1182 /* Signal interlacing information externally. */
1183 /* Prioritize picture timing SEI information over used
1184 * decoding process if it exists. */
1185
2/2
✓ Branch 0 taken 1538 times.
✓ Branch 1 taken 24141 times.
25679 if (h->sei.picture_timing.present) {
1186 1538 int ret = ff_h264_sei_process_picture_timing(&h->sei.picture_timing, sps,
1187 1538 h->avctx);
1188
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1538 times.
1538 if (ret < 0) {
1189 av_log(h->avctx, AV_LOG_ERROR, "Error processing a picture timing SEI\n");
1190 if (h->avctx->err_recognition & AV_EF_EXPLODE)
1191 return ret;
1192 h->sei.picture_timing.present = 0;
1193 }
1194 }
1195
1196
4/4
✓ Branch 0 taken 1616 times.
✓ Branch 1 taken 24063 times.
✓ Branch 2 taken 1530 times.
✓ Branch 3 taken 86 times.
27209 if (sps->pic_struct_present_flag && h->sei.picture_timing.present) {
1197 1530 const H264SEIPictureTiming *pt = &h->sei.picture_timing;
1198
4/7
✓ Branch 0 taken 242 times.
✓ Branch 1 taken 378 times.
✓ Branch 2 taken 743 times.
✓ Branch 3 taken 167 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
1530 switch (pt->pic_struct) {
1199 242 case H264_SEI_PIC_STRUCT_FRAME:
1200 242 break;
1201 378 case H264_SEI_PIC_STRUCT_TOP_FIELD:
1202 case H264_SEI_PIC_STRUCT_BOTTOM_FIELD:
1203 378 interlaced_frame = 1;
1204 378 break;
1205 743 case H264_SEI_PIC_STRUCT_TOP_BOTTOM:
1206 case H264_SEI_PIC_STRUCT_BOTTOM_TOP:
1207
3/4
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 739 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 4 times.
743 if (FIELD_OR_MBAFF_PICTURE(h))
1208 739 interlaced_frame = 1;
1209 else
1210 // try to flag soft telecine progressive
1211 4 interlaced_frame = !!h->prev_interlaced_frame;
1212 743 break;
1213 167 case H264_SEI_PIC_STRUCT_TOP_BOTTOM_TOP:
1214 case H264_SEI_PIC_STRUCT_BOTTOM_TOP_BOTTOM:
1215 /* Signal the possibility of telecined film externally
1216 * (pic_struct 5,6). From these hints, let the applications
1217 * decide if they apply deinterlacing. */
1218 167 out->repeat_pict = 1;
1219 167 break;
1220 case H264_SEI_PIC_STRUCT_FRAME_DOUBLING:
1221 out->repeat_pict = 2;
1222 break;
1223 case H264_SEI_PIC_STRUCT_FRAME_TRIPLING:
1224 out->repeat_pict = 4;
1225 break;
1226 }
1227
1228
2/2
✓ Branch 0 taken 653 times.
✓ Branch 1 taken 877 times.
1530 if ((pt->ct_type & 3) &&
1229
2/2
✓ Branch 0 taken 489 times.
✓ Branch 1 taken 164 times.
653 pt->pic_struct <= H264_SEI_PIC_STRUCT_BOTTOM_TOP)
1230 489 interlaced_frame = ((pt->ct_type & (1 << 1)) != 0);
1231 } else {
1232 /* Derive interlacing flag from used decoding process. */
1233
4/4
✓ Branch 0 taken 22558 times.
✓ Branch 1 taken 1591 times.
✓ Branch 2 taken 2876 times.
✓ Branch 3 taken 19682 times.
24149 interlaced_frame = !!FIELD_OR_MBAFF_PICTURE(h);
1234 }
1235 25679 h->prev_interlaced_frame = interlaced_frame;
1236
1237
2/2
✓ Branch 0 taken 3393 times.
✓ Branch 1 taken 22286 times.
25679 if (cur->field_poc[0] != cur->field_poc[1]) {
1238 /* Derive top_field_first from field pocs. */
1239 3393 top_field_first = (cur->field_poc[0] < cur->field_poc[1]);
1240 } else {
1241
4/4
✓ Branch 0 taken 335 times.
✓ Branch 1 taken 21951 times.
✓ Branch 2 taken 249 times.
✓ Branch 3 taken 86 times.
22286 if (sps->pic_struct_present_flag && h->sei.picture_timing.present) {
1242 /* Use picture timing SEI information. Even if it is a
1243 * information of a past frame, better than nothing. */
1244
2/2
✓ Branch 0 taken 248 times.
✓ Branch 1 taken 1 times.
249 if (h->sei.picture_timing.pic_struct == H264_SEI_PIC_STRUCT_TOP_BOTTOM ||
1245
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 246 times.
248 h->sei.picture_timing.pic_struct == H264_SEI_PIC_STRUCT_TOP_BOTTOM_TOP)
1246 3 top_field_first = 1;
1247
2/2
✓ Branch 0 taken 2558 times.
✓ Branch 1 taken 19479 times.
22037 } else if (interlaced_frame) {
1248 /* Default to top field first when pic_struct_present_flag
1249 * is not set but interlaced frame detected */
1250 2558 top_field_first = 1;
1251 } // else
1252 /* Most likely progressive */
1253 }
1254
1255 25679 out->flags |= (AV_FRAME_FLAG_INTERLACED * interlaced_frame) |
1256 25679 (AV_FRAME_FLAG_TOP_FIELD_FIRST * top_field_first);
1257
1258 25679 ret = ff_h2645_sei_to_frame(out, &h->sei.common, AV_CODEC_ID_H264, h->avctx,
1259 25679 &sps->vui, sps->bit_depth_luma, sps->bit_depth_chroma,
1260 25679 cur->poc + (unsigned)(h->poc_offset << 5));
1261
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 25679 times.
25679 if (ret < 0)
1262 return ret;
1263
1264
2/2
✓ Branch 0 taken 653 times.
✓ Branch 1 taken 25026 times.
25679 if (h->sei.picture_timing.timecode_cnt > 0) {
1265 uint32_t *tc_sd;
1266 char tcbuf[AV_TIMECODE_STR_SIZE];
1267 AVFrameSideData *tcside;
1268 653 ret = ff_frame_new_side_data(h->avctx, out, AV_FRAME_DATA_S12M_TIMECODE,
1269 sizeof(uint32_t)*4, &tcside);
1270
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 653 times.
653 if (ret < 0)
1271 return ret;
1272
1273
1/2
✓ Branch 0 taken 653 times.
✗ Branch 1 not taken.
653 if (tcside) {
1274 653 tc_sd = (uint32_t*)tcside->data;
1275 653 tc_sd[0] = h->sei.picture_timing.timecode_cnt;
1276
1277
2/2
✓ Branch 0 taken 954 times.
✓ Branch 1 taken 653 times.
1607 for (int i = 0; i < tc_sd[0]; i++) {
1278 954 int drop = h->sei.picture_timing.timecode[i].dropframe;
1279 954 int hh = h->sei.picture_timing.timecode[i].hours;
1280 954 int mm = h->sei.picture_timing.timecode[i].minutes;
1281 954 int ss = h->sei.picture_timing.timecode[i].seconds;
1282 954 int ff = h->sei.picture_timing.timecode[i].frame;
1283
1284 954 tc_sd[i + 1] = av_timecode_get_smpte(h->avctx->framerate, drop, hh, mm, ss, ff);
1285 954 av_timecode_make_smpte_tc_string2(tcbuf, h->avctx->framerate, tc_sd[i + 1], 0, 0);
1286 954 av_dict_set(&out->metadata, "timecode", tcbuf, 0);
1287 }
1288 }
1289 653 h->sei.picture_timing.timecode_cnt = 0;
1290 }
1291
1292 25679 return 0;
1293 }
1294
1295 25679 static int h264_select_output_frame(H264Context *h)
1296 {
1297 25679 const SPS *sps = h->ps.sps;
1298 25679 H264Picture *out = h->cur_pic_ptr;
1299 25679 H264Picture *cur = h->cur_pic_ptr;
1300 int i, pics, out_of_order, out_idx;
1301
1302 25679 cur->mmco_reset = h->mmco_reset;
1303 25679 h->mmco_reset = 0;
1304
1305
2/2
✓ Branch 0 taken 20030 times.
✓ Branch 1 taken 5649 times.
25679 if (sps->bitstream_restriction_flag ||
1306
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 20030 times.
20030 h->avctx->strict_std_compliance >= FF_COMPLIANCE_STRICT) {
1307 5649 h->avctx->has_b_frames = FFMAX(h->avctx->has_b_frames, sps->num_reorder_frames);
1308 }
1309
1310 421178 for (i = 0; 1; i++) {
1311
4/4
✓ Branch 0 taken 404798 times.
✓ Branch 1 taken 16380 times.
✓ Branch 2 taken 9299 times.
✓ Branch 3 taken 395499 times.
421178 if(i == H264_MAX_DPB_FRAMES || cur->poc < h->last_pocs[i]){
1312
1/2
✓ Branch 0 taken 25679 times.
✗ Branch 1 not taken.
25679 if(i)
1313 25679 h->last_pocs[i-1] = cur->poc;
1314 25679 break;
1315
2/2
✓ Branch 0 taken 369820 times.
✓ Branch 1 taken 25679 times.
395499 } else if(i) {
1316 369820 h->last_pocs[i-1]= h->last_pocs[i];
1317 }
1318 }
1319 25679 out_of_order = H264_MAX_DPB_FRAMES - i;
1320
2/2
✓ Branch 0 taken 17262 times.
✓ Branch 1 taken 8417 times.
25679 if( cur->f->pict_type == AV_PICTURE_TYPE_B
1321
4/4
✓ Branch 0 taken 16046 times.
✓ Branch 1 taken 1216 times.
✓ Branch 2 taken 5238 times.
✓ Branch 3 taken 10808 times.
17262 || (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))
1322 13655 out_of_order = FFMAX(out_of_order, 1);
1323
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 25679 times.
25679 if (out_of_order == H264_MAX_DPB_FRAMES) {
1324 av_log(h->avctx, AV_LOG_VERBOSE, "Invalid POC %d<%d\n", cur->poc, h->last_pocs[0]);
1325 for (i = 1; i < H264_MAX_DPB_FRAMES; i++)
1326 h->last_pocs[i] = INT_MIN;
1327 h->last_pocs[0] = cur->poc;
1328 cur->mmco_reset = 1;
1329
3/4
✓ Branch 0 taken 141 times.
✓ Branch 1 taken 25538 times.
✓ Branch 2 taken 141 times.
✗ Branch 3 not taken.
25679 } else if(h->avctx->has_b_frames < out_of_order && !sps->bitstream_restriction_flag){
1330
2/2
✓ Branch 0 taken 33 times.
✓ Branch 1 taken 108 times.
141 int loglevel = h->avctx->frame_num > 1 ? AV_LOG_WARNING : AV_LOG_VERBOSE;
1331 141 av_log(h->avctx, loglevel, "Increasing reorder buffer to %d\n", out_of_order);
1332 141 h->avctx->has_b_frames = out_of_order;
1333 }
1334
1335 25679 pics = 0;
1336
2/2
✓ Branch 0 taken 32114 times.
✓ Branch 1 taken 25679 times.
57793 while (h->delayed_pic[pics])
1337 32114 pics++;
1338
1339
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 25679 times.
25679 av_assert0(pics <= H264_MAX_DPB_FRAMES);
1340
1341 25679 h->delayed_pic[pics++] = cur;
1342
2/2
✓ Branch 0 taken 24151 times.
✓ Branch 1 taken 1528 times.
25679 if (cur->reference == 0)
1343 24151 cur->reference = DELAYED_PIC_REF;
1344
1345 25679 out = h->delayed_pic[0];
1346 25679 out_idx = 0;
1347 25679 for (i = 1; h->delayed_pic[i] &&
1348
4/4
✓ Branch 0 taken 29261 times.
✓ Branch 1 taken 25147 times.
✓ Branch 2 taken 28816 times.
✓ Branch 3 taken 445 times.
54408 !(h->delayed_pic[i]->f->flags & AV_FRAME_FLAG_KEY) &&
1349
2/2
✓ Branch 0 taken 28729 times.
✓ Branch 1 taken 87 times.
28816 !h->delayed_pic[i]->mmco_reset;
1350 28729 i++)
1351
2/2
✓ Branch 0 taken 10661 times.
✓ Branch 1 taken 18068 times.
28729 if (h->delayed_pic[i]->poc < out->poc) {
1352 10661 out = h->delayed_pic[i];
1353 10661 out_idx = i;
1354 }
1355
2/2
✓ Branch 0 taken 10387 times.
✓ Branch 1 taken 15292 times.
25679 if (h->avctx->has_b_frames == 0 &&
1356
4/4
✓ Branch 0 taken 9529 times.
✓ Branch 1 taken 858 times.
✓ Branch 2 taken 12 times.
✓ Branch 3 taken 9517 times.
10387 ((h->delayed_pic[0]->f->flags & AV_FRAME_FLAG_KEY) || h->delayed_pic[0]->mmco_reset))
1357 870 h->next_outputed_poc = INT_MIN;
1358 25679 out_of_order = out->poc < h->next_outputed_poc;
1359
1360
4/4
✓ Branch 0 taken 25566 times.
✓ Branch 1 taken 113 times.
✓ Branch 2 taken 25004 times.
✓ Branch 3 taken 562 times.
25679 if (out_of_order || pics > h->avctx->has_b_frames) {
1361 25117 out->reference &= ~DELAYED_PIC_REF;
1362
2/2
✓ Branch 0 taken 41398 times.
✓ Branch 1 taken 25117 times.
66515 for (i = out_idx; h->delayed_pic[i]; i++)
1363 41398 h->delayed_pic[i] = h->delayed_pic[i + 1];
1364 }
1365
4/4
✓ Branch 0 taken 25566 times.
✓ Branch 1 taken 113 times.
✓ Branch 2 taken 25004 times.
✓ Branch 3 taken 562 times.
25679 if (!out_of_order && pics > h->avctx->has_b_frames) {
1366 25004 h->next_output_pic = out;
1367
8/8
✓ Branch 0 taken 15935 times.
✓ Branch 1 taken 9069 times.
✓ Branch 2 taken 5549 times.
✓ Branch 3 taken 10386 times.
✓ Branch 4 taken 5456 times.
✓ Branch 5 taken 93 times.
✓ Branch 6 taken 21 times.
✓ Branch 7 taken 5435 times.
25004 if (out_idx == 0 && h->delayed_pic[0] && ((h->delayed_pic[0]->f->flags & AV_FRAME_FLAG_KEY) || h->delayed_pic[0]->mmco_reset)) {
1368 114 h->next_outputed_poc = INT_MIN;
1369 } else
1370 24890 h->next_outputed_poc = out->poc;
1371
1372 // We have reached an recovery point and all frames after it in
1373 // display order are "recovered".
1374 25004 h->frame_recovered |= out->recovered;
1375
1376 25004 out->recovered |= h->frame_recovered & FRAME_RECOVERED_SEI;
1377
1378
2/2
✓ Branch 0 taken 306 times.
✓ Branch 1 taken 24698 times.
25004 if (!out->recovered) {
1379
1/2
✓ Branch 0 taken 306 times.
✗ Branch 1 not taken.
306 if (!(h->avctx->flags & AV_CODEC_FLAG_OUTPUT_CORRUPT) &&
1380
1/2
✓ Branch 0 taken 306 times.
✗ Branch 1 not taken.
306 !(h->avctx->flags2 & AV_CODEC_FLAG2_SHOW_ALL)) {
1381 306 h->next_output_pic = NULL;
1382 } else {
1383 out->f->flags |= AV_FRAME_FLAG_CORRUPT;
1384 }
1385 }
1386 } else {
1387
2/2
✓ Branch 0 taken 113 times.
✓ Branch 1 taken 562 times.
675 av_log(h->avctx, AV_LOG_DEBUG, "no picture %s\n", out_of_order ? "ooo" : "");
1388 }
1389
1390 25679 return 0;
1391 }
1392
1393 /* This function is called right after decoding the slice header for a first
1394 * slice in a field (or a frame). It decides whether we are decoding a new frame
1395 * or a second field in a pair and does the necessary setup.
1396 */
1397 29103 static int h264_field_start(H264Context *h, const H264SliceContext *sl,
1398 const H2645NAL *nal, int first_slice)
1399 {
1400 int i;
1401 const SPS *sps;
1402
1403 int last_pic_structure, last_pic_droppable, ret;
1404
1405 29103 ret = h264_init_ps(h, sl, first_slice);
1406
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 29103 times.
29103 if (ret < 0)
1407 return ret;
1408
1409 29103 sps = h->ps.sps;
1410
1411
2/2
✓ Branch 0 taken 6034 times.
✓ Branch 1 taken 23069 times.
29103 if (sps->bitstream_restriction_flag &&
1412
2/2
✓ Branch 0 taken 42 times.
✓ Branch 1 taken 5992 times.
6034 h->avctx->has_b_frames < sps->num_reorder_frames) {
1413 42 h->avctx->has_b_frames = sps->num_reorder_frames;
1414 }
1415
1416 29103 last_pic_droppable = h->droppable;
1417 29103 last_pic_structure = h->picture_structure;
1418 29103 h->droppable = (nal->ref_idc == 0);
1419 29103 h->picture_structure = sl->picture_structure;
1420
1421 29103 h->poc.frame_num = sl->frame_num;
1422 29103 h->poc.poc_lsb = sl->poc_lsb;
1423 29103 h->poc.delta_poc_bottom = sl->delta_poc_bottom;
1424 29103 h->poc.delta_poc[0] = sl->delta_poc[0];
1425 29103 h->poc.delta_poc[1] = sl->delta_poc[1];
1426
1427
2/2
✓ Branch 0 taken 1172 times.
✓ Branch 1 taken 27931 times.
29103 if (nal->type == H264_NAL_IDR_SLICE)
1428 1172 h->poc_offset = sl->idr_pic_id;
1429
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 27931 times.
27931 else if (h->picture_intra_only)
1430 h->poc_offset = 0;
1431
1432 /* Shorten frame num gaps so we don't have to allocate reference
1433 * frames just to throw them away */
1434
2/2
✓ Branch 0 taken 16592 times.
✓ Branch 1 taken 12511 times.
29103 if (h->poc.frame_num != h->poc.prev_frame_num) {
1435 16592 int unwrap_prev_frame_num = h->poc.prev_frame_num;
1436 16592 int max_frame_num = 1 << sps->log2_max_frame_num;
1437
1438
2/2
✓ Branch 0 taken 201 times.
✓ Branch 1 taken 16391 times.
16592 if (unwrap_prev_frame_num > h->poc.frame_num)
1439 201 unwrap_prev_frame_num -= max_frame_num;
1440
1441
2/2
✓ Branch 0 taken 76 times.
✓ Branch 1 taken 16516 times.
16592 if ((h->poc.frame_num - unwrap_prev_frame_num) > sps->ref_frame_count) {
1442 76 unwrap_prev_frame_num = (h->poc.frame_num - sps->ref_frame_count) - 1;
1443
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 74 times.
76 if (unwrap_prev_frame_num < 0)
1444 2 unwrap_prev_frame_num += max_frame_num;
1445
1446 76 h->poc.prev_frame_num = unwrap_prev_frame_num;
1447 }
1448 }
1449
1450 /* See if we have a decoded first field looking for a pair...
1451 * Here, we're using that to see if we should mark previously
1452 * decode frames as "finished".
1453 * We have to do that before the "dummy" in-between frame allocation,
1454 * since that can modify h->cur_pic_ptr. */
1455
2/2
✓ Branch 0 taken 3418 times.
✓ Branch 1 taken 25685 times.
29103 if (h->first_field) {
1456 3418 int last_field = last_pic_structure == PICT_BOTTOM_FIELD;
1457
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3418 times.
3418 av_assert0(h->cur_pic_ptr);
1458
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3418 times.
3418 av_assert0(h->cur_pic_ptr->f->buf[0]);
1459 assert(h->cur_pic_ptr->reference != DELAYED_PIC_REF);
1460
1461 /* Mark old field/frame as completed */
1462
1/2
✓ Branch 0 taken 3418 times.
✗ Branch 1 not taken.
3418 if (h->cur_pic_ptr->tf.owner[last_field] == h->avctx) {
1463 3418 ff_thread_report_progress(&h->cur_pic_ptr->tf, INT_MAX, last_field);
1464 }
1465
1466 /* figure out if we have a complementary field pair */
1467
2/4
✓ Branch 0 taken 3418 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 3418 times.
3418 if (!FIELD_PICTURE(h) || h->picture_structure == last_pic_structure) {
1468 /* Previous field is unmatched. Don't display it, but let it
1469 * remain for reference if marked as such. */
1470 if (last_pic_structure != PICT_FRAME) {
1471 ff_thread_report_progress(&h->cur_pic_ptr->tf, INT_MAX,
1472 last_pic_structure == PICT_TOP_FIELD);
1473 }
1474 } else {
1475
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3418 times.
3418 if (h->cur_pic_ptr->frame_num != h->poc.frame_num) {
1476 /* This and previous field were reference, but had
1477 * different frame_nums. Consider this field first in
1478 * pair. Throw away previous field except for reference
1479 * purposes. */
1480 if (last_pic_structure != PICT_FRAME) {
1481 ff_thread_report_progress(&h->cur_pic_ptr->tf, INT_MAX,
1482 last_pic_structure == PICT_TOP_FIELD);
1483 }
1484 } else {
1485 /* Second field in complementary pair */
1486
3/4
✓ Branch 0 taken 3410 times.
✓ Branch 1 taken 8 times.
✓ Branch 2 taken 8 times.
✗ Branch 3 not taken.
3418 if (!((last_pic_structure == PICT_TOP_FIELD &&
1487
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3410 times.
3410 h->picture_structure == PICT_BOTTOM_FIELD) ||
1488 8 (last_pic_structure == PICT_BOTTOM_FIELD &&
1489
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
8 h->picture_structure == PICT_TOP_FIELD))) {
1490 av_log(h->avctx, AV_LOG_ERROR,
1491 "Invalid field mode combination %d/%d\n",
1492 last_pic_structure, h->picture_structure);
1493 h->picture_structure = last_pic_structure;
1494 h->droppable = last_pic_droppable;
1495 return AVERROR_INVALIDDATA;
1496
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3418 times.
3418 } else if (last_pic_droppable != h->droppable) {
1497 avpriv_request_sample(h->avctx,
1498 "Found reference and non-reference fields in the same frame, which");
1499 h->picture_structure = last_pic_structure;
1500 h->droppable = last_pic_droppable;
1501 return AVERROR_PATCHWELCOME;
1502 }
1503 }
1504 }
1505 }
1506
1507
3/4
✓ Branch 0 taken 17281 times.
✓ Branch 1 taken 12511 times.
✓ Branch 2 taken 17281 times.
✗ Branch 3 not taken.
58895 while (h->poc.frame_num != h->poc.prev_frame_num && !h->first_field &&
1508
2/2
✓ Branch 0 taken 689 times.
✓ Branch 1 taken 16592 times.
17281 h->poc.frame_num != (h->poc.prev_frame_num + 1) % (1 << sps->log2_max_frame_num)) {
1509
2/2
✓ Branch 0 taken 655 times.
✓ Branch 1 taken 34 times.
689 const H264Picture *prev = h->short_ref_count ? h->short_ref[0] : NULL;
1510 689 av_log(h->avctx, AV_LOG_DEBUG, "Frame num gap %d %d\n",
1511 h->poc.frame_num, h->poc.prev_frame_num);
1512
2/2
✓ Branch 0 taken 112 times.
✓ Branch 1 taken 577 times.
689 if (!sps->gaps_in_frame_num_allowed_flag)
1513
2/2
✓ Branch 0 taken 1792 times.
✓ Branch 1 taken 112 times.
1904 for(i=0; i<FF_ARRAY_ELEMS(h->last_pocs); i++)
1514 1792 h->last_pocs[i] = INT_MIN;
1515 689 ret = h264_frame_start(h);
1516
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 689 times.
689 if (ret < 0) {
1517 h->first_field = 0;
1518 return ret;
1519 }
1520
1521 689 h->poc.prev_frame_num++;
1522 689 h->poc.prev_frame_num %= 1 << sps->log2_max_frame_num;
1523 689 h->cur_pic_ptr->frame_num = h->poc.prev_frame_num;
1524 689 h->cur_pic_ptr->invalid_gap = !sps->gaps_in_frame_num_allowed_flag;
1525 689 ff_thread_report_progress(&h->cur_pic_ptr->tf, INT_MAX, 0);
1526 689 ff_thread_report_progress(&h->cur_pic_ptr->tf, INT_MAX, 1);
1527
1528 689 h->explicit_ref_marking = 0;
1529 689 ret = ff_h264_execute_ref_pic_marking(h);
1530
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 689 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
689 if (ret < 0 && (h->avctx->err_recognition & AV_EF_EXPLODE))
1531 return ret;
1532 /* Error concealment: If a ref is missing, copy the previous ref
1533 * in its place.
1534 * FIXME: Avoiding a memcpy would be nice, but ref handling makes
1535 * many assumptions about there being no actual duplicates.
1536 * FIXME: This does not copy padding for out-of-frame motion
1537 * vectors. Given we are concealing a lost frame, this probably
1538 * is not noticeable by comparison, but it should be fixed. */
1539
1/2
✓ Branch 0 taken 689 times.
✗ Branch 1 not taken.
689 if (h->short_ref_count) {
1540 689 int c[4] = {
1541 689 1<<(h->ps.sps->bit_depth_luma-1),
1542 689 1<<(h->ps.sps->bit_depth_chroma-1),
1543 689 1<<(h->ps.sps->bit_depth_chroma-1),
1544 -1
1545 };
1546
1547
2/2
✓ Branch 0 taken 655 times.
✓ Branch 1 taken 34 times.
689 if (prev &&
1548
1/2
✓ Branch 0 taken 655 times.
✗ Branch 1 not taken.
655 h->short_ref[0]->f->width == prev->f->width &&
1549
1/2
✓ Branch 0 taken 655 times.
✗ Branch 1 not taken.
655 h->short_ref[0]->f->height == prev->f->height &&
1550
1/2
✓ Branch 0 taken 655 times.
✗ Branch 1 not taken.
655 h->short_ref[0]->f->format == prev->f->format) {
1551 655 ff_thread_await_progress(&prev->tf, INT_MAX, 0);
1552
2/2
✓ Branch 0 taken 28 times.
✓ Branch 1 taken 627 times.
655 if (prev->field_picture)
1553 28 ff_thread_await_progress(&prev->tf, INT_MAX, 1);
1554 655 ff_thread_release_ext_buffer(&h->short_ref[0]->tf);
1555 655 h->short_ref[0]->tf.f = h->short_ref[0]->f;
1556 655 ret = ff_thread_ref_frame(&h->short_ref[0]->tf, &prev->tf);
1557
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 655 times.
655 if (ret < 0)
1558 return ret;
1559 655 h->short_ref[0]->poc = prev->poc + 2U;
1560 655 h->short_ref[0]->gray = prev->gray;
1561 655 ff_thread_report_progress(&h->short_ref[0]->tf, INT_MAX, 0);
1562
2/2
✓ Branch 0 taken 28 times.
✓ Branch 1 taken 627 times.
655 if (h->short_ref[0]->field_picture)
1563 28 ff_thread_report_progress(&h->short_ref[0]->tf, INT_MAX, 1);
1564
2/2
✓ Branch 0 taken 26 times.
✓ Branch 1 taken 8 times.
34 } else if (!h->frame_recovered) {
1565
1/2
✓ Branch 0 taken 26 times.
✗ Branch 1 not taken.
26 if (!h->avctx->hwaccel)
1566 26 color_frame(h->short_ref[0]->f, c);
1567 26 h->short_ref[0]->gray = 1;
1568 }
1569 689 h->short_ref[0]->frame_num = h->poc.prev_frame_num;
1570 }
1571 }
1572
1573 /* See if we have a decoded first field looking for a pair...
1574 * We're using that to see whether to continue decoding in that
1575 * frame, or to allocate a new one. */
1576
2/2
✓ Branch 0 taken 3418 times.
✓ Branch 1 taken 25685 times.
29103 if (h->first_field) {
1577
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3418 times.
3418 av_assert0(h->cur_pic_ptr);
1578
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3418 times.
3418 av_assert0(h->cur_pic_ptr->f->buf[0]);
1579 assert(h->cur_pic_ptr->reference != DELAYED_PIC_REF);
1580
1581 /* figure out if we have a complementary field pair */
1582
2/4
✓ Branch 0 taken 3418 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 3418 times.
3418 if (!FIELD_PICTURE(h) || h->picture_structure == last_pic_structure) {
1583 /* Previous field is unmatched. Don't display it, but let it
1584 * remain for reference if marked as such. */
1585 h->missing_fields ++;
1586 h->cur_pic_ptr = NULL;
1587 h->first_field = FIELD_PICTURE(h);
1588 } else {
1589 3418 h->missing_fields = 0;
1590
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3418 times.
3418 if (h->cur_pic_ptr->frame_num != h->poc.frame_num) {
1591 ff_thread_report_progress(&h->cur_pic_ptr->tf, INT_MAX,
1592 h->picture_structure==PICT_BOTTOM_FIELD);
1593 /* This and the previous field had different frame_nums.
1594 * Consider this field first in pair. Throw away previous
1595 * one except for reference purposes. */
1596 h->first_field = 1;
1597 h->cur_pic_ptr = NULL;
1598
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3418 times.
3418 } else if (h->cur_pic_ptr->reference & DELAYED_PIC_REF) {
1599 /* This frame was already output, we cannot draw into it
1600 * anymore.
1601 */
1602 h->first_field = 1;
1603 h->cur_pic_ptr = NULL;
1604 } else {
1605 /* Second field in complementary pair */
1606 3418 h->first_field = 0;
1607 }
1608 }
1609 } else {
1610 /* Frame or first field in a potentially complementary pair */
1611 25685 h->first_field = FIELD_PICTURE(h);
1612 }
1613
1614
4/4
✓ Branch 0 taken 6842 times.
✓ Branch 1 taken 22261 times.
✓ Branch 2 taken 3424 times.
✓ Branch 3 taken 3418 times.
29103 if (!FIELD_PICTURE(h) || h->first_field) {
1615
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 25685 times.
25685 if (h264_frame_start(h) < 0) {
1616 h->first_field = 0;
1617 return AVERROR_INVALIDDATA;
1618 }
1619 } else {
1620 3418 int field = h->picture_structure == PICT_BOTTOM_FIELD;
1621 3418 release_unused_pictures(h, 0);
1622 3418 h->cur_pic_ptr->tf.owner[field] = h->avctx;
1623 }
1624 /* Some macroblocks can be accessed before they're available in case
1625 * of lost slices, MBAFF or threading. */
1626
2/2
✓ Branch 0 taken 6842 times.
✓ Branch 1 taken 22261 times.
29103 if (FIELD_PICTURE(h)) {
1627
2/2
✓ Branch 0 taken 170420 times.
✓ Branch 1 taken 6842 times.
177262 for(i = (h->picture_structure == PICT_BOTTOM_FIELD); i<h->mb_height; i++)
1628 170420 memset(h->slice_table + i*h->mb_stride, -1, (h->mb_stride - (i+1==h->mb_height)) * sizeof(*h->slice_table));
1629 } else {
1630 22261 memset(h->slice_table, -1,
1631 22261 (h->mb_height * h->mb_stride - 1) * sizeof(*h->slice_table));
1632 }
1633
1634 29103 ret = ff_h264_init_poc(h->cur_pic_ptr->field_poc, &h->cur_pic_ptr->poc,
1635 29103 h->ps.sps, &h->poc, h->picture_structure, nal->ref_idc);
1636
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 29103 times.
29103 if (ret < 0)
1637 return ret;
1638
1639 29103 memcpy(h->mmco, sl->mmco, sl->nb_mmco * sizeof(*h->mmco));
1640 29103 h->nb_mmco = sl->nb_mmco;
1641 29103 h->explicit_ref_marking = sl->explicit_ref_marking;
1642
1643 29103 h->picture_idr = nal->type == H264_NAL_IDR_SLICE;
1644
1645
2/2
✓ Branch 0 taken 98 times.
✓ Branch 1 taken 29005 times.
29103 if (h->sei.recovery_point.recovery_frame_cnt >= 0) {
1646 98 const int sei_recovery_frame_cnt = h->sei.recovery_point.recovery_frame_cnt;
1647
1648
4/4
✓ Branch 0 taken 25 times.
✓ Branch 1 taken 73 times.
✓ Branch 2 taken 11 times.
✓ Branch 3 taken 14 times.
98 if (h->poc.frame_num != sei_recovery_frame_cnt || sl->slice_type_nos != AV_PICTURE_TYPE_I)
1649 84 h->valid_recovery_point = 1;
1650
1651
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 93 times.
98 if ( h->recovery_frame < 0
1652
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
5 || av_zero_extend(h->recovery_frame - h->poc.frame_num, h->ps.sps->log2_max_frame_num) > sei_recovery_frame_cnt) {
1653 93 h->recovery_frame = av_zero_extend(h->poc.frame_num + sei_recovery_frame_cnt, h->ps.sps->log2_max_frame_num);
1654
1655
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 87 times.
93 if (!h->valid_recovery_point)
1656 6 h->recovery_frame = h->poc.frame_num;
1657 }
1658 }
1659
1660
2/2
✓ Branch 0 taken 1172 times.
✓ Branch 1 taken 27931 times.
29103 h->cur_pic_ptr->f->flags |= AV_FRAME_FLAG_KEY * !!(nal->type == H264_NAL_IDR_SLICE);
1661
1662
2/2
✓ Branch 0 taken 1172 times.
✓ Branch 1 taken 27931 times.
29103 if (nal->type == H264_NAL_IDR_SLICE) {
1663 1172 h->cur_pic_ptr->recovered |= FRAME_RECOVERED_IDR;
1664 // If we have an IDR, all frames after it in decoded order are
1665 // "recovered".
1666 1172 h->frame_recovered |= FRAME_RECOVERED_IDR;
1667 }
1668
1669
4/4
✓ Branch 0 taken 110 times.
✓ Branch 1 taken 28993 times.
✓ Branch 2 taken 92 times.
✓ Branch 3 taken 18 times.
29103 if (h->recovery_frame == h->poc.frame_num && nal->ref_idc) {
1670 92 h->recovery_frame = -1;
1671 92 h->cur_pic_ptr->recovered |= FRAME_RECOVERED_SEI;
1672 }
1673
1674 #if 1
1675 29103 h->cur_pic_ptr->recovered |= h->frame_recovered;
1676 #else
1677 h->cur_pic_ptr->recovered |= !!(h->frame_recovered & FRAME_RECOVERED_IDR);
1678 #endif
1679
1680 /* Set the frame properties/side data. Only done for the second field in
1681 * field coded frames, since some SEI information is present for each field
1682 * and is merged by the SEI parsing code. */
1683
5/6
✓ Branch 0 taken 6842 times.
✓ Branch 1 taken 22261 times.
✓ Branch 2 taken 3424 times.
✓ Branch 3 taken 3418 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 3424 times.
29103 if (!FIELD_PICTURE(h) || !h->first_field || h->missing_fields > 1) {
1684 25679 ret = h264_export_frame_props(h);
1685
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 25679 times.
25679 if (ret < 0)
1686 return ret;
1687
1688 25679 ret = h264_select_output_frame(h);
1689
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 25679 times.
25679 if (ret < 0)
1690 return ret;
1691 }
1692
1693 29103 return 0;
1694 }
1695
1696 38524 static int h264_slice_header_parse(const H264Context *h, H264SliceContext *sl,
1697 const H2645NAL *nal)
1698 {
1699 const SPS *sps;
1700 const PPS *pps;
1701 int ret;
1702 unsigned int slice_type, tmp, i;
1703 int field_pic_flag, bottom_field_flag;
1704
4/4
✓ Branch 0 taken 38074 times.
✓ Branch 1 taken 450 times.
✓ Branch 2 taken 29151 times.
✓ Branch 3 taken 8923 times.
38524 int first_slice = sl == h->slice_ctx && !h->current_slice;
1705 int picture_structure;
1706
1707
2/2
✓ Branch 0 taken 29151 times.
✓ Branch 1 taken 9373 times.
38524 if (first_slice)
1708
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 29151 times.
29151 av_assert0(!h->setup_finished);
1709
1710 38524 sl->first_mb_addr = get_ue_golomb_long(&sl->gb);
1711
1712 38524 slice_type = get_ue_golomb_31(&sl->gb);
1713
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 38524 times.
38524 if (slice_type > 9) {
1714 av_log(h->avctx, AV_LOG_ERROR,
1715 "slice type %d too large at %d\n",
1716 slice_type, sl->first_mb_addr);
1717 return AVERROR_INVALIDDATA;
1718 }
1719
2/2
✓ Branch 0 taken 24690 times.
✓ Branch 1 taken 13834 times.
38524 if (slice_type > 4) {
1720 24690 slice_type -= 5;
1721 24690 sl->slice_type_fixed = 1;
1722 } else
1723 13834 sl->slice_type_fixed = 0;
1724
1725 38524 slice_type = ff_h264_golomb_to_pict_type[slice_type];
1726 38524 sl->slice_type = slice_type;
1727 38524 sl->slice_type_nos = slice_type & 3;
1728
1729
2/2
✓ Branch 0 taken 2360 times.
✓ Branch 1 taken 36164 times.
38524 if (nal->type == H264_NAL_IDR_SLICE &&
1730
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2360 times.
2360 sl->slice_type_nos != AV_PICTURE_TYPE_I) {
1731 av_log(h->avctx, AV_LOG_ERROR, "A non-intra slice in an IDR NAL unit.\n");
1732 return AVERROR_INVALIDDATA;
1733 }
1734
1735 38524 sl->pps_id = get_ue_golomb(&sl->gb);
1736
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 38524 times.
38524 if (sl->pps_id >= MAX_PPS_COUNT) {
1737 av_log(h->avctx, AV_LOG_ERROR, "pps_id %u out of range\n", sl->pps_id);
1738 return AVERROR_INVALIDDATA;
1739 }
1740
2/2
✓ Branch 0 taken 167 times.
✓ Branch 1 taken 38357 times.
38524 if (!h->ps.pps_list[sl->pps_id]) {
1741 167 av_log(h->avctx, AV_LOG_ERROR,
1742 "non-existing PPS %u referenced\n",
1743 sl->pps_id);
1744 167 return AVERROR_INVALIDDATA;
1745 }
1746 38357 pps = h->ps.pps_list[sl->pps_id];
1747 38357 sps = pps->sps;
1748
1749 38357 sl->frame_num = get_bits(&sl->gb, sps->log2_max_frame_num);
1750
2/2
✓ Branch 0 taken 9369 times.
✓ Branch 1 taken 28988 times.
38357 if (!first_slice) {
1751
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9369 times.
9369 if (h->poc.frame_num != sl->frame_num) {
1752 av_log(h->avctx, AV_LOG_ERROR, "Frame num change from %d to %d\n",
1753 h->poc.frame_num, sl->frame_num);
1754 return AVERROR_INVALIDDATA;
1755 }
1756 }
1757
1758 38357 sl->mb_mbaff = 0;
1759
1760
2/2
✓ Branch 0 taken 23644 times.
✓ Branch 1 taken 14713 times.
38357 if (sps->frame_mbs_only_flag) {
1761 23644 picture_structure = PICT_FRAME;
1762 } else {
1763
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 14713 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
14713 if (!sps->direct_8x8_inference_flag && slice_type == AV_PICTURE_TYPE_B) {
1764 av_log(h->avctx, AV_LOG_ERROR, "This stream was generated by a broken encoder, invalid 8x8 inference\n");
1765 return -1;
1766 }
1767 14713 field_pic_flag = get_bits1(&sl->gb);
1768
2/2
✓ Branch 0 taken 9240 times.
✓ Branch 1 taken 5473 times.
14713 if (field_pic_flag) {
1769 9240 bottom_field_flag = get_bits1(&sl->gb);
1770 9240 picture_structure = PICT_TOP_FIELD + bottom_field_flag;
1771 } else {
1772 5473 picture_structure = PICT_FRAME;
1773 }
1774 }
1775 38357 sl->picture_structure = picture_structure;
1776 38357 sl->mb_field_decoding_flag = picture_structure != PICT_FRAME;
1777
1778
2/2
✓ Branch 0 taken 29117 times.
✓ Branch 1 taken 9240 times.
38357 if (picture_structure == PICT_FRAME) {
1779 29117 sl->curr_pic_num = sl->frame_num;
1780 29117 sl->max_pic_num = 1 << sps->log2_max_frame_num;
1781 } else {
1782 9240 sl->curr_pic_num = 2 * sl->frame_num + 1;
1783 9240 sl->max_pic_num = 1 << (sps->log2_max_frame_num + 1);
1784 }
1785
1786
2/2
✓ Branch 0 taken 2355 times.
✓ Branch 1 taken 36002 times.
38357 if (nal->type == H264_NAL_IDR_SLICE) {
1787 2355 unsigned idr_pic_id = get_ue_golomb_long(&sl->gb);
1788
1/2
✓ Branch 0 taken 2355 times.
✗ Branch 1 not taken.
2355 if (idr_pic_id < 65536) {
1789 2355 sl->idr_pic_id = idr_pic_id;
1790 } else
1791 av_log(h->avctx, AV_LOG_WARNING, "idr_pic_id is invalid\n");
1792 }
1793
1794 38357 sl->poc_lsb = 0;
1795 38357 sl->delta_poc_bottom = 0;
1796
2/2
✓ Branch 0 taken 30451 times.
✓ Branch 1 taken 7906 times.
38357 if (sps->poc_type == 0) {
1797 30451 sl->poc_lsb = get_bits(&sl->gb, sps->log2_max_poc_lsb);
1798
1799
4/4
✓ Branch 0 taken 8524 times.
✓ Branch 1 taken 21927 times.
✓ Branch 2 taken 6632 times.
✓ Branch 3 taken 1892 times.
30451 if (pps->pic_order_present == 1 && picture_structure == PICT_FRAME)
1800 6632 sl->delta_poc_bottom = get_se_golomb(&sl->gb);
1801 }
1802
1803 38357 sl->delta_poc[0] = sl->delta_poc[1] = 0;
1804
4/4
✓ Branch 0 taken 3055 times.
✓ Branch 1 taken 35302 times.
✓ Branch 2 taken 2848 times.
✓ Branch 3 taken 207 times.
38357 if (sps->poc_type == 1 && !sps->delta_pic_order_always_zero_flag) {
1805 2848 sl->delta_poc[0] = get_se_golomb(&sl->gb);
1806
1807
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)
1808 453 sl->delta_poc[1] = get_se_golomb(&sl->gb);
1809 }
1810
1811 38357 sl->redundant_pic_count = 0;
1812
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 38357 times.
38357 if (pps->redundant_pic_cnt_present)
1813 sl->redundant_pic_count = get_ue_golomb(&sl->gb);
1814
1815
2/2
✓ Branch 0 taken 13581 times.
✓ Branch 1 taken 24776 times.
38357 if (sl->slice_type_nos == AV_PICTURE_TYPE_B)
1816 13581 sl->direct_spatial_mv_pred = get_bits1(&sl->gb);
1817
1818 38357 ret = ff_h264_parse_ref_count(&sl->list_count, sl->ref_count,
1819 &sl->gb, pps, sl->slice_type_nos,
1820 38357 picture_structure, h->avctx);
1821
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 38357 times.
38357 if (ret < 0)
1822 return ret;
1823
1824
2/2
✓ Branch 0 taken 33794 times.
✓ Branch 1 taken 4563 times.
38357 if (sl->slice_type_nos != AV_PICTURE_TYPE_I) {
1825 33794 ret = ff_h264_decode_ref_pic_list_reordering(sl, h->avctx);
1826
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 33794 times.
33794 if (ret < 0) {
1827 sl->ref_count[1] = sl->ref_count[0] = 0;
1828 return ret;
1829 }
1830 }
1831
1832 38357 sl->pwt.use_weight = 0;
1833
2/2
✓ Branch 0 taken 76714 times.
✓ Branch 1 taken 38357 times.
115071 for (i = 0; i < 2; i++) {
1834 76714 sl->pwt.luma_weight_flag[i] = 0;
1835 76714 sl->pwt.chroma_weight_flag[i] = 0;
1836 }
1837
4/4
✓ Branch 0 taken 4884 times.
✓ Branch 1 taken 33473 times.
✓ Branch 2 taken 1905 times.
✓ Branch 3 taken 2979 times.
38357 if ((pps->weighted_pred && sl->slice_type_nos == AV_PICTURE_TYPE_P) ||
1838
2/2
✓ Branch 0 taken 390 times.
✓ Branch 1 taken 34988 times.
35378 (pps->weighted_bipred_idc == 1 &&
1839
2/2
✓ Branch 0 taken 386 times.
✓ Branch 1 taken 4 times.
390 sl->slice_type_nos == AV_PICTURE_TYPE_B)) {
1840 3365 ret = ff_h264_pred_weight_table(&sl->gb, sps, sl->ref_count,
1841 sl->slice_type_nos, &sl->pwt,
1842 3365 picture_structure, h->avctx);
1843
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3365 times.
3365 if (ret < 0)
1844 return ret;
1845 }
1846
1847 38357 sl->explicit_ref_marking = 0;
1848
2/2
✓ Branch 0 taken 25641 times.
✓ Branch 1 taken 12716 times.
38357 if (nal->ref_idc) {
1849 25641 ret = ff_h264_decode_ref_pic_marking(sl, &sl->gb, nal, h->avctx);
1850
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 25641 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
25641 if (ret < 0 && (h->avctx->err_recognition & AV_EF_EXPLODE))
1851 return AVERROR_INVALIDDATA;
1852 }
1853
1854
4/4
✓ Branch 0 taken 33794 times.
✓ Branch 1 taken 4563 times.
✓ Branch 2 taken 18974 times.
✓ Branch 3 taken 14820 times.
38357 if (sl->slice_type_nos != AV_PICTURE_TYPE_I && pps->cabac) {
1855 18974 tmp = get_ue_golomb_31(&sl->gb);
1856
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 18974 times.
18974 if (tmp > 2) {
1857 av_log(h->avctx, AV_LOG_ERROR, "cabac_init_idc %u overflow\n", tmp);
1858 return AVERROR_INVALIDDATA;
1859 }
1860 18974 sl->cabac_init_idc = tmp;
1861 }
1862
1863 38357 sl->last_qscale_diff = 0;
1864 38357 tmp = pps->init_qp + (unsigned)get_se_golomb(&sl->gb);
1865
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 38357 times.
38357 if (tmp > 51 + 6 * (sps->bit_depth_luma - 8)) {
1866 av_log(h->avctx, AV_LOG_ERROR, "QP %u out of range\n", tmp);
1867 return AVERROR_INVALIDDATA;
1868 }
1869 38357 sl->qscale = tmp;
1870 38357 sl->chroma_qp[0] = get_chroma_qp(pps, 0, sl->qscale);
1871 38357 sl->chroma_qp[1] = get_chroma_qp(pps, 1, sl->qscale);
1872 // FIXME qscale / qp ... stuff
1873
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 38357 times.
38357 if (sl->slice_type == AV_PICTURE_TYPE_SP)
1874 get_bits1(&sl->gb); /* sp_for_switch_flag */
1875
1/2
✓ Branch 0 taken 38357 times.
✗ Branch 1 not taken.
38357 if (sl->slice_type == AV_PICTURE_TYPE_SP ||
1876
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 38357 times.
38357 sl->slice_type == AV_PICTURE_TYPE_SI)
1877 get_se_golomb(&sl->gb); /* slice_qs_delta */
1878
1879 38357 sl->deblocking_filter = 1;
1880 38357 sl->slice_alpha_c0_offset = 0;
1881 38357 sl->slice_beta_offset = 0;
1882
2/2
✓ Branch 0 taken 23560 times.
✓ Branch 1 taken 14797 times.
38357 if (pps->deblocking_filter_parameters_present) {
1883 23560 tmp = get_ue_golomb_31(&sl->gb);
1884
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 23560 times.
23560 if (tmp > 2) {
1885 av_log(h->avctx, AV_LOG_ERROR,
1886 "deblocking_filter_idc %u out of range\n", tmp);
1887 return AVERROR_INVALIDDATA;
1888 }
1889 23560 sl->deblocking_filter = tmp;
1890
2/2
✓ Branch 0 taken 20857 times.
✓ Branch 1 taken 2703 times.
23560 if (sl->deblocking_filter < 2)
1891 20857 sl->deblocking_filter ^= 1; // 1<->0
1892
1893
2/2
✓ Branch 0 taken 15834 times.
✓ Branch 1 taken 7726 times.
23560 if (sl->deblocking_filter) {
1894 15834 int slice_alpha_c0_offset_div2 = get_se_golomb(&sl->gb);
1895 15834 int slice_beta_offset_div2 = get_se_golomb(&sl->gb);
1896
2/4
✓ Branch 0 taken 15834 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 15834 times.
✗ Branch 3 not taken.
15834 if (slice_alpha_c0_offset_div2 > 6 ||
1897
1/2
✓ Branch 0 taken 15834 times.
✗ Branch 1 not taken.
15834 slice_alpha_c0_offset_div2 < -6 ||
1898
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 15834 times.
15834 slice_beta_offset_div2 > 6 ||
1899 slice_beta_offset_div2 < -6) {
1900 av_log(h->avctx, AV_LOG_ERROR,
1901 "deblocking filter parameters %d %d out of range\n",
1902 slice_alpha_c0_offset_div2, slice_beta_offset_div2);
1903 return AVERROR_INVALIDDATA;
1904 }
1905 15834 sl->slice_alpha_c0_offset = slice_alpha_c0_offset_div2 * 2;
1906 15834 sl->slice_beta_offset = slice_beta_offset_div2 * 2;
1907 }
1908 }
1909
1910 38357 return 0;
1911 }
1912
1913 /* do all the per-slice initialization needed before we can start decoding the
1914 * actual MBs */
1915 38277 static int h264_slice_init(H264Context *h, H264SliceContext *sl,
1916 const H2645NAL *nal)
1917 {
1918 38277 int i, j, ret = 0;
1919
1920
3/4
✓ Branch 0 taken 2355 times.
✓ Branch 1 taken 35922 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 2355 times.
38277 if (h->picture_idr && nal->type != H264_NAL_IDR_SLICE) {
1921 av_log(h->avctx, AV_LOG_ERROR, "Invalid mix of IDR and non-IDR slices\n");
1922 return AVERROR_INVALIDDATA;
1923 }
1924
1925 av_assert1(h->mb_num == h->mb_width * h->mb_height);
1926
5/6
✓ Branch 0 taken 33301 times.
✓ Branch 1 taken 4976 times.
✓ Branch 2 taken 9160 times.
✓ Branch 3 taken 24141 times.
✓ Branch 4 taken 38277 times.
✗ Branch 5 not taken.
38277 if (sl->first_mb_addr << FIELD_OR_MBAFF_PICTURE(h) >= h->mb_num ||
1927
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 38277 times.
38277 sl->first_mb_addr >= h->mb_num) {
1928 av_log(h->avctx, AV_LOG_ERROR, "first_mb_in_slice overflow\n");
1929 return AVERROR_INVALIDDATA;
1930 }
1931 38277 sl->resync_mb_x = sl->mb_x = sl->first_mb_addr % h->mb_width;
1932 76554 sl->resync_mb_y = sl->mb_y = (sl->first_mb_addr / h->mb_width) <<
1933
4/4
✓ Branch 0 taken 33301 times.
✓ Branch 1 taken 4976 times.
✓ Branch 2 taken 9160 times.
✓ Branch 3 taken 24141 times.
38277 FIELD_OR_MBAFF_PICTURE(h);
1934
2/2
✓ Branch 0 taken 4576 times.
✓ Branch 1 taken 33701 times.
38277 if (h->picture_structure == PICT_BOTTOM_FIELD)
1935 4576 sl->resync_mb_y = sl->mb_y = sl->mb_y + 1;
1936 av_assert1(sl->mb_y < h->mb_height);
1937
1938 38277 ret = ff_h264_build_ref_list(h, sl);
1939
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 38277 times.
38277 if (ret < 0)
1940 return ret;
1941
1942
2/2
✓ Branch 0 taken 4382 times.
✓ Branch 1 taken 33895 times.
38277 if (h->ps.pps->weighted_bipred_idc == 2 &&
1943
2/2
✓ Branch 0 taken 2372 times.
✓ Branch 1 taken 2010 times.
4382 sl->slice_type_nos == AV_PICTURE_TYPE_B) {
1944 2372 implicit_weight_table(h, sl, -1);
1945
2/2
✓ Branch 0 taken 856 times.
✓ Branch 1 taken 1516 times.
2372 if (FRAME_MBAFF(h)) {
1946 856 implicit_weight_table(h, sl, 0);
1947 856 implicit_weight_table(h, sl, 1);
1948 }
1949 }
1950
1951
4/4
✓ Branch 0 taken 13537 times.
✓ Branch 1 taken 24740 times.
✓ Branch 2 taken 7710 times.
✓ Branch 3 taken 5827 times.
38277 if (sl->slice_type_nos == AV_PICTURE_TYPE_B && !sl->direct_spatial_mv_pred)
1952 7710 ff_h264_direct_dist_scale_factor(h, sl);
1953 38277 ff_h264_direct_ref_list_init(h, sl);
1954
1955
1/2
✓ Branch 0 taken 38277 times.
✗ Branch 1 not taken.
38277 if (h->avctx->skip_loop_filter >= AVDISCARD_ALL ||
1956
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 38277 times.
38277 (h->avctx->skip_loop_filter >= AVDISCARD_NONKEY &&
1957 h->nal_unit_type != H264_NAL_IDR_SLICE) ||
1958
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 38277 times.
38277 (h->avctx->skip_loop_filter >= AVDISCARD_NONINTRA &&
1959 sl->slice_type_nos != AV_PICTURE_TYPE_I) ||
1960
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 38277 times.
38277 (h->avctx->skip_loop_filter >= AVDISCARD_BIDIR &&
1961 sl->slice_type_nos == AV_PICTURE_TYPE_B) ||
1962
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 38277 times.
38277 (h->avctx->skip_loop_filter >= AVDISCARD_NONREF &&
1963 nal->ref_idc == 0))
1964 sl->deblocking_filter = 0;
1965
1966
3/4
✓ Branch 0 taken 27848 times.
✓ Branch 1 taken 10429 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 27848 times.
38277 if (sl->deblocking_filter == 1 && h->nb_slice_ctx > 1) {
1967 if (h->avctx->flags2 & AV_CODEC_FLAG2_FAST) {
1968 /* Cheat slightly for speed:
1969 * Do not bother to deblock across slices. */
1970 sl->deblocking_filter = 2;
1971 } else {
1972 h->postpone_filter = 1;
1973 }
1974 }
1975 38277 sl->qp_thresh = 15 -
1976 38277 FFMIN(sl->slice_alpha_c0_offset, sl->slice_beta_offset) -
1977 38277 FFMAX3(0,
1978 h->ps.pps->chroma_qp_index_offset[0],
1979 38277 h->ps.pps->chroma_qp_index_offset[1]) +
1980 38277 6 * (h->ps.sps->bit_depth_luma - 8);
1981
1982 // slice_table is uint16_t initialized to 0xFFFF as a sentinel.
1983
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 38277 times.
38277 if (h->current_slice >= 0xFFFE) {
1984 av_log(h->avctx, AV_LOG_ERROR, "Too many slices (%d)\n", h->current_slice + 1);
1985 return AVERROR_PATCHWELCOME;
1986 }
1987
1988 38277 sl->slice_num = ++h->current_slice;
1989
1990
1/2
✓ Branch 0 taken 38277 times.
✗ Branch 1 not taken.
38277 if (sl->slice_num)
1991 38277 h->slice_row[(sl->slice_num-1)&(MAX_SLICES-1)]= sl->resync_mb_y;
1992
2/2
✓ Branch 0 taken 35304 times.
✓ Branch 1 taken 2973 times.
38277 if ( h->slice_row[sl->slice_num&(MAX_SLICES-1)] + 3 >= sl->resync_mb_y
1993
2/2
✓ Branch 0 taken 26466 times.
✓ Branch 1 taken 8838 times.
35304 && h->slice_row[sl->slice_num&(MAX_SLICES-1)] <= sl->resync_mb_y
1994
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 26466 times.
26466 && sl->slice_num >= MAX_SLICES) {
1995 //in case of ASO this check needs to be updated depending on how we decide to assign slice numbers in this case
1996 av_log(h->avctx, AV_LOG_WARNING, "Possibly too many slices (%d >= %d), increase MAX_SLICES and recompile if there are artifacts\n", sl->slice_num, MAX_SLICES);
1997 }
1998
1999
2/2
✓ Branch 0 taken 76554 times.
✓ Branch 1 taken 38277 times.
114831 for (j = 0; j < 2; j++) {
2000 int id_list[16];
2001 76554 int *ref2frm = h->ref2frm[sl->slice_num & (MAX_SLICES - 1)][j];
2002
2/2
✓ Branch 0 taken 1224864 times.
✓ Branch 1 taken 76554 times.
1301418 for (i = 0; i < 16; i++) {
2003 1224864 id_list[i] = 60;
2004
4/4
✓ Branch 0 taken 756016 times.
✓ Branch 1 taken 468848 times.
✓ Branch 2 taken 142427 times.
✓ Branch 3 taken 613589 times.
1224864 if (j < sl->list_count && i < sl->ref_count[j] &&
2005
1/2
✓ Branch 0 taken 142427 times.
✗ Branch 1 not taken.
142427 sl->ref_list[j][i].parent->f->buf[0]) {
2006 int k;
2007 142427 const AVBuffer *buf = sl->ref_list[j][i].parent->f->buf[0]->buffer;
2008
2/2
✓ Branch 0 taken 473091 times.
✓ Branch 1 taken 1933 times.
475024 for (k = 0; k < h->short_ref_count; k++)
2009
2/2
✓ Branch 0 taken 140494 times.
✓ Branch 1 taken 332597 times.
473091 if (h->short_ref[k]->f->buf[0]->buffer == buf) {
2010 140494 id_list[i] = k;
2011 140494 break;
2012 }
2013
2/2
✓ Branch 0 taken 11870 times.
✓ Branch 1 taken 140635 times.
152505 for (k = 0; k < h->long_ref_count; k++)
2014
4/4
✓ Branch 0 taken 11269 times.
✓ Branch 1 taken 601 times.
✓ Branch 2 taken 1792 times.
✓ Branch 3 taken 9477 times.
11870 if (h->long_ref[k] && h->long_ref[k]->f->buf[0]->buffer == buf) {
2015 1792 id_list[i] = h->short_ref_count + k;
2016 1792 break;
2017 }
2018 }
2019 }
2020
2021 76554 ref2frm[0] =
2022 76554 ref2frm[1] = -1;
2023
2/2
✓ Branch 0 taken 1224864 times.
✓ Branch 1 taken 76554 times.
1301418 for (i = 0; i < 16; i++)
2024 1224864 ref2frm[i + 2] = 4 * id_list[i] + (sl->ref_list[j][i].reference & 3);
2025 76554 ref2frm[18 + 0] =
2026 76554 ref2frm[18 + 1] = -1;
2027
2/2
✓ Branch 0 taken 2449728 times.
✓ Branch 1 taken 76554 times.
2526282 for (i = 16; i < 48; i++)
2028 2449728 ref2frm[i + 4] = 4 * id_list[(i - 16) >> 1] +
2029 2449728 (sl->ref_list[j][i].reference & 3);
2030 }
2031
2032
2/2
✓ Branch 0 taken 4563 times.
✓ Branch 1 taken 33714 times.
38277 if (sl->slice_type_nos == AV_PICTURE_TYPE_I) {
2033 4563 h->cur_pic_ptr->gray = 0;
2034 4563 h->non_gray = 1;
2035 } else {
2036 33714 int gray = 0;
2037
2/2
✓ Branch 0 taken 47251 times.
✓ Branch 1 taken 33714 times.
80965 for (j = 0; j < sl->list_count; j++) {
2038
2/2
✓ Branch 0 taken 142427 times.
✓ Branch 1 taken 47251 times.
189678 for (i = 0; i < sl->ref_count[j]; i++) {
2039 142427 gray |= sl->ref_list[j][i].parent->gray;
2040 }
2041 }
2042 33714 h->cur_pic_ptr->gray = gray;
2043 }
2044
2045
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 38277 times.
38277 if (h->avctx->debug & FF_DEBUG_PICT_INFO) {
2046 av_log(h->avctx, AV_LOG_DEBUG,
2047 "slice:%d %c mb:%d %c%s%s frame:%d poc:%d/%d ref:%d/%d qp:%d loop:%d:%d:%d weight:%d%s %s\n",
2048 sl->slice_num,
2049 (h->picture_structure == PICT_FRAME ? 'F' : h->picture_structure == PICT_TOP_FIELD ? 'T' : 'B'),
2050 sl->mb_y * h->mb_width + sl->mb_x,
2051 av_get_picture_type_char(sl->slice_type),
2052 sl->slice_type_fixed ? " fix" : "",
2053 nal->type == H264_NAL_IDR_SLICE ? " IDR" : "",
2054 h->poc.frame_num,
2055 h->cur_pic_ptr->field_poc[0],
2056 h->cur_pic_ptr->field_poc[1],
2057 sl->ref_count[0], sl->ref_count[1],
2058 sl->qscale,
2059 sl->deblocking_filter,
2060 sl->slice_alpha_c0_offset, sl->slice_beta_offset,
2061 sl->pwt.use_weight,
2062 sl->pwt.use_weight == 1 && sl->pwt.use_weight_chroma ? "c" : "",
2063 sl->slice_type == AV_PICTURE_TYPE_B ? (sl->direct_spatial_mv_pred ? "SPAT" : "TEMP") : "");
2064 }
2065
2066 38277 return 0;
2067 }
2068
2069 38524 int ff_h264_queue_decode_slice(H264Context *h, const H2645NAL *nal)
2070 {
2071 38524 H264SliceContext *sl = h->slice_ctx + h->nb_slice_ctx_queued;
2072
4/4
✓ Branch 0 taken 38074 times.
✓ Branch 1 taken 450 times.
✓ Branch 2 taken 29151 times.
✓ Branch 3 taken 8923 times.
38524 int first_slice = sl == h->slice_ctx && !h->current_slice;
2073 int ret;
2074
2075 38524 sl->gb = nal->gb;
2076
2077 38524 ret = h264_slice_header_parse(h, sl, nal);
2078
2/2
✓ Branch 0 taken 167 times.
✓ Branch 1 taken 38357 times.
38524 if (ret < 0)
2079 167 return ret;
2080
2081 // discard redundant pictures
2082
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 38357 times.
38357 if (sl->redundant_pic_count > 0) {
2083 sl->ref_count[0] = sl->ref_count[1] = 0;
2084 return 0;
2085 }
2086
2087
3/4
✓ Branch 0 taken 9174 times.
✓ Branch 1 taken 29183 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 9174 times.
38357 if (sl->first_mb_addr == 0 || !h->current_slice) {
2088
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 29183 times.
29183 if (h->setup_finished) {
2089 av_log(h->avctx, AV_LOG_ERROR, "Too many fields\n");
2090 return AVERROR_INVALIDDATA;
2091 }
2092 }
2093
2094
2/2
✓ Branch 0 taken 29183 times.
✓ Branch 1 taken 9174 times.
38357 if (sl->first_mb_addr == 0) { // FIXME better field boundary detection
2095
2/2
✓ Branch 0 taken 195 times.
✓ Branch 1 taken 28988 times.
29183 if (h->current_slice) {
2096 // this slice starts a new field
2097 // first decode any pending queued slices
2098
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 195 times.
195 if (h->nb_slice_ctx_queued) {
2099 H264SliceContext tmp_ctx;
2100
2101 ret = ff_h264_execute_decode_slices(h);
2102 if (ret < 0 && (h->avctx->err_recognition & AV_EF_EXPLODE))
2103 return ret;
2104
2105 memcpy(&tmp_ctx, h->slice_ctx, sizeof(tmp_ctx));
2106 memcpy(h->slice_ctx, sl, sizeof(tmp_ctx));
2107 memcpy(sl, &tmp_ctx, sizeof(tmp_ctx));
2108 sl = h->slice_ctx;
2109 }
2110
2111
3/6
✓ Branch 0 taken 195 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 195 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 195 times.
✗ Branch 5 not taken.
195 if (h->cur_pic_ptr && FIELD_PICTURE(h) && h->first_field) {
2112 195 ret = ff_h264_field_end(h, h->slice_ctx, 1);
2113
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 195 times.
195 if (ret < 0)
2114 return ret;
2115 } else if (h->cur_pic_ptr && !FIELD_PICTURE(h) && !h->first_field && h->nal_unit_type == H264_NAL_IDR_SLICE) {
2116 av_log(h->avctx, AV_LOG_WARNING, "Broken frame packetizing\n");
2117 ret = ff_h264_field_end(h, h->slice_ctx, 1);
2118 ff_thread_report_progress(&h->cur_pic_ptr->tf, INT_MAX, 0);
2119 ff_thread_report_progress(&h->cur_pic_ptr->tf, INT_MAX, 1);
2120 h->cur_pic_ptr = NULL;
2121 if (ret < 0)
2122 return ret;
2123 } else
2124 return AVERROR_INVALIDDATA;
2125 }
2126
2127
2/2
✓ Branch 0 taken 25765 times.
✓ Branch 1 taken 3418 times.
29183 if (!h->first_field) {
2128
3/4
✓ Branch 0 taken 299 times.
✓ Branch 1 taken 25466 times.
✓ Branch 2 taken 299 times.
✗ Branch 3 not taken.
25765 if (h->cur_pic_ptr && !h->droppable) {
2129 299 ff_thread_report_progress(&h->cur_pic_ptr->tf, INT_MAX,
2130 299 h->picture_structure == PICT_BOTTOM_FIELD);
2131 }
2132 25765 h->cur_pic_ptr = NULL;
2133 }
2134 }
2135
2136
2/2
✓ Branch 0 taken 29183 times.
✓ Branch 1 taken 9174 times.
38357 if (!h->current_slice)
2137
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 29183 times.
29183 av_assert0(sl == h->slice_ctx);
2138
2139
4/4
✓ Branch 0 taken 29183 times.
✓ Branch 1 taken 9174 times.
✓ Branch 2 taken 25765 times.
✓ Branch 3 taken 3418 times.
38357 if (h->current_slice == 0 && !h->first_field) {
2140 25765 if (
2141
3/4
✓ Branch 0 taken 84 times.
✓ Branch 1 taken 25681 times.
✓ Branch 2 taken 84 times.
✗ Branch 3 not taken.
25765 (h->avctx->skip_frame >= AVDISCARD_NONREF && !h->nal_ref_idc) ||
2142
4/4
✓ Branch 0 taken 84 times.
✓ Branch 1 taken 25681 times.
✓ Branch 2 taken 40 times.
✓ Branch 3 taken 44 times.
25765 (h->avctx->skip_frame >= AVDISCARD_BIDIR && sl->slice_type_nos == AV_PICTURE_TYPE_B) ||
2143
4/4
✓ Branch 0 taken 40 times.
✓ Branch 1 taken 25681 times.
✓ Branch 2 taken 4 times.
✓ Branch 3 taken 36 times.
25721 (h->avctx->skip_frame >= AVDISCARD_NONINTRA && sl->slice_type_nos != AV_PICTURE_TYPE_I) ||
2144
4/6
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 25683 times.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
25685 (h->avctx->skip_frame >= AVDISCARD_NONKEY && h->nal_unit_type != H264_NAL_IDR_SLICE && h->sei.recovery_point.recovery_frame_cnt < 0) ||
2145
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 25685 times.
25685 h->avctx->skip_frame >= AVDISCARD_ALL) {
2146 80 return 0;
2147 }
2148 }
2149
2150
2/2
✓ Branch 0 taken 9369 times.
✓ Branch 1 taken 28908 times.
38277 if (!first_slice) {
2151 9369 const PPS *pps = h->ps.pps_list[sl->pps_id];
2152
2153
1/2
✓ Branch 0 taken 9369 times.
✗ Branch 1 not taken.
9369 if (h->ps.pps->sps_id != pps->sps_id ||
2154
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9369 times.
9369 h->ps.pps->transform_8x8_mode != pps->transform_8x8_mode /*||
2155 (h->setup_finished && h->ps.pps != pps)*/) {
2156 av_log(h->avctx, AV_LOG_ERROR, "PPS changed between slices\n");
2157 return AVERROR_INVALIDDATA;
2158 }
2159
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9369 times.
9369 if (h->ps.sps != pps->sps) {
2160 av_log(h->avctx, AV_LOG_ERROR,
2161 "SPS changed in the middle of the frame\n");
2162 return AVERROR_INVALIDDATA;
2163 }
2164 }
2165
2166
2/2
✓ Branch 0 taken 29103 times.
✓ Branch 1 taken 9174 times.
38277 if (h->current_slice == 0) {
2167 29103 ret = h264_field_start(h, sl, nal, first_slice);
2168
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 29103 times.
29103 if (ret < 0)
2169 return ret;
2170 } else {
2171
1/2
✓ Branch 0 taken 9174 times.
✗ Branch 1 not taken.
9174 if (h->picture_structure != sl->picture_structure ||
2172
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9174 times.
9174 h->droppable != (nal->ref_idc == 0)) {
2173 av_log(h->avctx, AV_LOG_ERROR,
2174 "Changing field mode (%d -> %d) between slices is not allowed\n",
2175 h->picture_structure, sl->picture_structure);
2176 return AVERROR_INVALIDDATA;
2177
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9174 times.
9174 } else if (!h->cur_pic_ptr) {
2178 av_log(h->avctx, AV_LOG_ERROR,
2179 "unset cur_pic_ptr on slice %d\n",
2180 h->current_slice + 1);
2181 return AVERROR_INVALIDDATA;
2182 }
2183 }
2184
2185 38277 ret = h264_slice_init(h, sl, nal);
2186
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 38277 times.
38277 if (ret < 0)
2187 return ret;
2188
2189 38277 h->nb_slice_ctx_queued++;
2190
2191 38277 return 0;
2192 }
2193
2194 int ff_h264_get_slice_type(const H264SliceContext *sl)
2195 {
2196 switch (sl->slice_type) {
2197 case AV_PICTURE_TYPE_P:
2198 return 0;
2199 case AV_PICTURE_TYPE_B:
2200 return 1;
2201 case AV_PICTURE_TYPE_I:
2202 return 2;
2203 case AV_PICTURE_TYPE_SP:
2204 return 3;
2205 case AV_PICTURE_TYPE_SI:
2206 return 4;
2207 default:
2208 return AVERROR_INVALIDDATA;
2209 }
2210 }
2211
2212 12805472 static av_always_inline void fill_filter_caches_inter(const H264Context *h,
2213 H264SliceContext *sl,
2214 int mb_type, int top_xy,
2215 const int left_xy[LEFT_MBS],
2216 int top_type,
2217 const int left_type[LEFT_MBS],
2218 int mb_xy, int list)
2219 {
2220 12805472 int b_stride = h->b_stride;
2221 12805472 int16_t(*mv_dst)[2] = &sl->mv_cache[list][scan8[0]];
2222 12805472 int8_t *ref_cache = &sl->ref_cache[list][scan8[0]];
2223
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 12805472 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
12805472 if (IS_INTER(mb_type) || IS_DIRECT(mb_type)) {
2224
2/2
✓ Branch 0 taken 10325416 times.
✓ Branch 1 taken 2480056 times.
12805472 if (USES_LIST(top_type, list)) {
2225 10325416 const int b_xy = h->mb2b_xy[top_xy] + 3 * b_stride;
2226 10325416 const int b8_xy = 4 * top_xy + 2;
2227
2/2
✓ Branch 0 taken 343379 times.
✓ Branch 1 taken 9982037 times.
10325416 const int *ref2frm = &h->ref2frm[h->slice_table[top_xy] & (MAX_SLICES - 1)][list][(MB_MBAFF(sl) ? 20 : 2)];
2228 10325416 AV_COPY128(mv_dst - 1 * 8, h->cur_pic.motion_val[list][b_xy + 0]);
2229 10325416 ref_cache[0 - 1 * 8] =
2230 10325416 ref_cache[1 - 1 * 8] = ref2frm[h->cur_pic.ref_index[list][b8_xy + 0]];
2231 10325416 ref_cache[2 - 1 * 8] =
2232 10325416 ref_cache[3 - 1 * 8] = ref2frm[h->cur_pic.ref_index[list][b8_xy + 1]];
2233 } else {
2234 2480056 AV_ZERO128(mv_dst - 1 * 8);
2235 2480056 AV_WN32A(&ref_cache[0 - 1 * 8], ((LIST_NOT_USED) & 0xFF) * 0x01010101u);
2236 }
2237
2238
2/2
✓ Branch 0 taken 12370759 times.
✓ Branch 1 taken 434713 times.
12805472 if (!IS_INTERLACED(mb_type ^ left_type[LTOP])) {
2239
2/2
✓ Branch 0 taken 10357494 times.
✓ Branch 1 taken 2013265 times.
12370759 if (USES_LIST(left_type[LTOP], list)) {
2240 10357494 const int b_xy = h->mb2b_xy[left_xy[LTOP]] + 3;
2241 10357494 const int b8_xy = 4 * left_xy[LTOP] + 1;
2242
2/2
✓ Branch 0 taken 240429 times.
✓ Branch 1 taken 10117065 times.
10357494 const int *ref2frm = &h->ref2frm[h->slice_table[left_xy[LTOP]] & (MAX_SLICES - 1)][list][(MB_MBAFF(sl) ? 20 : 2)];
2243 10357494 AV_COPY32(mv_dst - 1 + 0, h->cur_pic.motion_val[list][b_xy + b_stride * 0]);
2244 10357494 AV_COPY32(mv_dst - 1 + 8, h->cur_pic.motion_val[list][b_xy + b_stride * 1]);
2245 10357494 AV_COPY32(mv_dst - 1 + 16, h->cur_pic.motion_val[list][b_xy + b_stride * 2]);
2246 10357494 AV_COPY32(mv_dst - 1 + 24, h->cur_pic.motion_val[list][b_xy + b_stride * 3]);
2247 10357494 ref_cache[-1 + 0] =
2248 10357494 ref_cache[-1 + 8] = ref2frm[h->cur_pic.ref_index[list][b8_xy + 2 * 0]];
2249 10357494 ref_cache[-1 + 16] =
2250 10357494 ref_cache[-1 + 24] = ref2frm[h->cur_pic.ref_index[list][b8_xy + 2 * 1]];
2251 } else {
2252 2013265 AV_ZERO32(mv_dst - 1 + 0);
2253 2013265 AV_ZERO32(mv_dst - 1 + 8);
2254 2013265 AV_ZERO32(mv_dst - 1 + 16);
2255 2013265 AV_ZERO32(mv_dst - 1 + 24);
2256 2013265 ref_cache[-1 + 0] =
2257 2013265 ref_cache[-1 + 8] =
2258 2013265 ref_cache[-1 + 16] =
2259 2013265 ref_cache[-1 + 24] = LIST_NOT_USED;
2260 }
2261 }
2262 }
2263
2264
2/2
✓ Branch 0 taken 1407268 times.
✓ Branch 1 taken 11398204 times.
12805472 if (!USES_LIST(mb_type, list)) {
2265 1407268 fill_rectangle(mv_dst, 4, 4, 8, pack16to32(0, 0), 4);
2266 1407268 AV_WN32A(&ref_cache[0 * 8], ((LIST_NOT_USED) & 0xFF) * 0x01010101u);
2267 1407268 AV_WN32A(&ref_cache[1 * 8], ((LIST_NOT_USED) & 0xFF) * 0x01010101u);
2268 1407268 AV_WN32A(&ref_cache[2 * 8], ((LIST_NOT_USED) & 0xFF) * 0x01010101u);
2269 1407268 AV_WN32A(&ref_cache[3 * 8], ((LIST_NOT_USED) & 0xFF) * 0x01010101u);
2270 1407268 return;
2271 }
2272
2273 {
2274 11398204 const int8_t *ref = &h->cur_pic.ref_index[list][4 * mb_xy];
2275
2/2
✓ Branch 0 taken 414359 times.
✓ Branch 1 taken 10983845 times.
11398204 const int *ref2frm = &h->ref2frm[sl->slice_num & (MAX_SLICES - 1)][list][(MB_MBAFF(sl) ? 20 : 2)];
2276 11398204 uint32_t ref01 = (pack16to32(ref2frm[ref[0]], ref2frm[ref[1]]) & 0x00FF00FF) * 0x0101;
2277 11398204 uint32_t ref23 = (pack16to32(ref2frm[ref[2]], ref2frm[ref[3]]) & 0x00FF00FF) * 0x0101;
2278 11398204 AV_WN32A(&ref_cache[0 * 8], ref01);
2279 11398204 AV_WN32A(&ref_cache[1 * 8], ref01);
2280 11398204 AV_WN32A(&ref_cache[2 * 8], ref23);
2281 11398204 AV_WN32A(&ref_cache[3 * 8], ref23);
2282 }
2283
2284 {
2285 11398204 int16_t(*mv_src)[2] = &h->cur_pic.motion_val[list][4 * sl->mb_x + 4 * sl->mb_y * b_stride];
2286 11398204 AV_COPY128(mv_dst + 8 * 0, mv_src + 0 * b_stride);
2287 11398204 AV_COPY128(mv_dst + 8 * 1, mv_src + 1 * b_stride);
2288 11398204 AV_COPY128(mv_dst + 8 * 2, mv_src + 2 * b_stride);
2289 11398204 AV_COPY128(mv_dst + 8 * 3, mv_src + 3 * b_stride);
2290 }
2291 }
2292
2293 /**
2294 * @return non zero if the loop filter can be skipped
2295 */
2296 13001858 static int fill_filter_caches(const H264Context *h, H264SliceContext *sl, int mb_type)
2297 {
2298 13001858 const int mb_xy = sl->mb_xy;
2299 int top_xy, left_xy[LEFT_MBS];
2300 int top_type, left_type[LEFT_MBS];
2301 const uint8_t *nnz;
2302 uint8_t *nnz_cache;
2303
2304 13001858 top_xy = mb_xy - (h->mb_stride << MB_FIELD(sl));
2305
2306 13001858 left_xy[LBOT] = left_xy[LTOP] = mb_xy - 1;
2307
2/2
✓ Branch 0 taken 2023440 times.
✓ Branch 1 taken 10978418 times.
13001858 if (FRAME_MBAFF(h)) {
2308 2023440 const int left_mb_field_flag = IS_INTERLACED(h->cur_pic.mb_type[mb_xy - 1]);
2309 2023440 const int curr_mb_field_flag = IS_INTERLACED(mb_type);
2310
2/2
✓ Branch 0 taken 1011720 times.
✓ Branch 1 taken 1011720 times.
2023440 if (sl->mb_y & 1) {
2311
2/2
✓ Branch 0 taken 205032 times.
✓ Branch 1 taken 806688 times.
1011720 if (left_mb_field_flag != curr_mb_field_flag)
2312 205032 left_xy[LTOP] -= h->mb_stride;
2313 } else {
2314
2/2
✓ Branch 0 taken 263670 times.
✓ Branch 1 taken 748050 times.
1011720 if (curr_mb_field_flag)
2315 263670 top_xy += h->mb_stride &
2316 263670 (((h->cur_pic.mb_type[top_xy] >> 7) & 1) - 1);
2317
2/2
✓ Branch 0 taken 205032 times.
✓ Branch 1 taken 806688 times.
1011720 if (left_mb_field_flag != curr_mb_field_flag)
2318 205032 left_xy[LBOT] += h->mb_stride;
2319 }
2320 }
2321
2322 13001858 sl->top_mb_xy = top_xy;
2323 13001858 sl->left_mb_xy[LTOP] = left_xy[LTOP];
2324 13001858 sl->left_mb_xy[LBOT] = left_xy[LBOT];
2325 {
2326 /* For sufficiently low qp, filtering wouldn't do anything.
2327 * This is a conservative estimate: could also check beta_offset
2328 * and more accurate chroma_qp. */
2329 13001858 int qp_thresh = sl->qp_thresh; // FIXME strictly we should store qp_thresh for each mb of a slice
2330 13001858 int qp = h->cur_pic.qscale_table[mb_xy];
2331
2/2
✓ Branch 0 taken 1837936 times.
✓ Branch 1 taken 11163922 times.
13001858 if (qp <= qp_thresh &&
2332
2/2
✓ Branch 0 taken 1835957 times.
✓ Branch 1 taken 1979 times.
1837936 (left_xy[LTOP] < 0 ||
2333
4/4
✓ Branch 0 taken 1775042 times.
✓ Branch 1 taken 60915 times.
✓ Branch 2 taken 1686411 times.
✓ Branch 3 taken 90610 times.
1837936 ((qp + h->cur_pic.qscale_table[left_xy[LTOP]] + 1) >> 1) <= qp_thresh) &&
2334 1686411 (top_xy < 0 ||
2335
2/2
✓ Branch 0 taken 1489991 times.
✓ Branch 1 taken 196420 times.
1686411 ((qp + h->cur_pic.qscale_table[top_xy] + 1) >> 1) <= qp_thresh)) {
2336
2/2
✓ Branch 0 taken 1264790 times.
✓ Branch 1 taken 315811 times.
1580601 if (!FRAME_MBAFF(h))
2337 1264790 return 1;
2338
2/2
✓ Branch 0 taken 315680 times.
✓ Branch 1 taken 131 times.
315811 if ((left_xy[LTOP] < 0 ||
2339
2/2
✓ Branch 0 taken 315151 times.
✓ Branch 1 taken 529 times.
315680 ((qp + h->cur_pic.qscale_table[left_xy[LBOT]] + 1) >> 1) <= qp_thresh) &&
2340
2/2
✓ Branch 0 taken 294673 times.
✓ Branch 1 taken 20609 times.
315282 (top_xy < h->mb_stride ||
2341
2/2
✓ Branch 0 taken 271562 times.
✓ Branch 1 taken 23111 times.
294673 ((qp + h->cur_pic.qscale_table[top_xy - h->mb_stride] + 1) >> 1) <= qp_thresh))
2342 292171 return 1;
2343 }
2344 }
2345
2346 11444897 top_type = h->cur_pic.mb_type[top_xy];
2347 11444897 left_type[LTOP] = h->cur_pic.mb_type[left_xy[LTOP]];
2348 11444897 left_type[LBOT] = h->cur_pic.mb_type[left_xy[LBOT]];
2349
2/2
✓ Branch 0 taken 490374 times.
✓ Branch 1 taken 10954523 times.
11444897 if (sl->deblocking_filter == 2) {
2350
2/2
✓ Branch 0 taken 61322 times.
✓ Branch 1 taken 429052 times.
490374 if (h->slice_table[top_xy] != sl->slice_num)
2351 61322 top_type = 0;
2352
2/2
✓ Branch 0 taken 14783 times.
✓ Branch 1 taken 475591 times.
490374 if (h->slice_table[left_xy[LBOT]] != sl->slice_num)
2353 14783 left_type[LTOP] = left_type[LBOT] = 0;
2354 } else {
2355
2/2
✓ Branch 0 taken 485864 times.
✓ Branch 1 taken 10468659 times.
10954523 if (h->slice_table[top_xy] == 0xFFFF)
2356 485864 top_type = 0;
2357
2/2
✓ Branch 0 taken 314728 times.
✓ Branch 1 taken 10639795 times.
10954523 if (h->slice_table[left_xy[LBOT]] == 0xFFFF)
2358 314728 left_type[LTOP] = left_type[LBOT] = 0;
2359 }
2360 11444897 sl->top_type = top_type;
2361 11444897 sl->left_type[LTOP] = left_type[LTOP];
2362 11444897 sl->left_type[LBOT] = left_type[LBOT];
2363
2364
2/2
✓ Branch 0 taken 3020078 times.
✓ Branch 1 taken 8424819 times.
11444897 if (IS_INTRA(mb_type))
2365 3020078 return 0;
2366
2367 8424819 fill_filter_caches_inter(h, sl, mb_type, top_xy, left_xy,
2368 top_type, left_type, mb_xy, 0);
2369
2/2
✓ Branch 0 taken 4380653 times.
✓ Branch 1 taken 4044166 times.
8424819 if (sl->list_count == 2)
2370 4380653 fill_filter_caches_inter(h, sl, mb_type, top_xy, left_xy,
2371 top_type, left_type, mb_xy, 1);
2372
2373 8424819 nnz = h->non_zero_count[mb_xy];
2374 8424819 nnz_cache = sl->non_zero_count_cache;
2375 8424819 AV_COPY32(&nnz_cache[4 + 8 * 1], &nnz[0]);
2376 8424819 AV_COPY32(&nnz_cache[4 + 8 * 2], &nnz[4]);
2377 8424819 AV_COPY32(&nnz_cache[4 + 8 * 3], &nnz[8]);
2378 8424819 AV_COPY32(&nnz_cache[4 + 8 * 4], &nnz[12]);
2379 8424819 sl->cbp = h->cbp_table[mb_xy];
2380
2381
2/2
✓ Branch 0 taken 7982887 times.
✓ Branch 1 taken 441932 times.
8424819 if (top_type) {
2382 7982887 nnz = h->non_zero_count[top_xy];
2383 7982887 AV_COPY32(&nnz_cache[4 + 8 * 0], &nnz[3 * 4]);
2384 }
2385
2386
2/2
✓ Branch 0 taken 8150758 times.
✓ Branch 1 taken 274061 times.
8424819 if (left_type[LTOP]) {
2387 8150758 nnz = h->non_zero_count[left_xy[LTOP]];
2388 8150758 nnz_cache[3 + 8 * 1] = nnz[3 + 0 * 4];
2389 8150758 nnz_cache[3 + 8 * 2] = nnz[3 + 1 * 4];
2390 8150758 nnz_cache[3 + 8 * 3] = nnz[3 + 2 * 4];
2391 8150758 nnz_cache[3 + 8 * 4] = nnz[3 + 3 * 4];
2392 }
2393
2394 /* CAVLC 8x8dct requires NNZ values for residual decoding that differ
2395 * from what the loop filter needs */
2396
4/4
✓ Branch 0 taken 2070968 times.
✓ Branch 1 taken 6353851 times.
✓ Branch 2 taken 246166 times.
✓ Branch 3 taken 1824802 times.
8424819 if (!CABAC(h) && h->ps.pps->transform_8x8_mode) {
2397
2/2
✓ Branch 0 taken 107789 times.
✓ Branch 1 taken 138377 times.
246166 if (IS_8x8DCT(top_type)) {
2398 107789 nnz_cache[4 + 8 * 0] =
2399 107789 nnz_cache[5 + 8 * 0] = (h->cbp_table[top_xy] & 0x4000) >> 12;
2400 107789 nnz_cache[6 + 8 * 0] =
2401 107789 nnz_cache[7 + 8 * 0] = (h->cbp_table[top_xy] & 0x8000) >> 12;
2402 }
2403
2/2
✓ Branch 0 taken 110456 times.
✓ Branch 1 taken 135710 times.
246166 if (IS_8x8DCT(left_type[LTOP])) {
2404 110456 nnz_cache[3 + 8 * 1] =
2405 110456 nnz_cache[3 + 8 * 2] = (h->cbp_table[left_xy[LTOP]] & 0x2000) >> 12; // FIXME check MBAFF
2406 }
2407
2/2
✓ Branch 0 taken 110556 times.
✓ Branch 1 taken 135610 times.
246166 if (IS_8x8DCT(left_type[LBOT])) {
2408 110556 nnz_cache[3 + 8 * 3] =
2409 110556 nnz_cache[3 + 8 * 4] = (h->cbp_table[left_xy[LBOT]] & 0x8000) >> 12; // FIXME check MBAFF
2410 }
2411
2412
2/2
✓ Branch 0 taken 104872 times.
✓ Branch 1 taken 141294 times.
246166 if (IS_8x8DCT(mb_type)) {
2413 104872 nnz_cache[scan8[0]] =
2414 104872 nnz_cache[scan8[1]] =
2415 104872 nnz_cache[scan8[2]] =
2416 104872 nnz_cache[scan8[3]] = (sl->cbp & 0x1000) >> 12;
2417
2418 104872 nnz_cache[scan8[0 + 4]] =
2419 104872 nnz_cache[scan8[1 + 4]] =
2420 104872 nnz_cache[scan8[2 + 4]] =
2421 104872 nnz_cache[scan8[3 + 4]] = (sl->cbp & 0x2000) >> 12;
2422
2423 104872 nnz_cache[scan8[0 + 8]] =
2424 104872 nnz_cache[scan8[1 + 8]] =
2425 104872 nnz_cache[scan8[2 + 8]] =
2426 104872 nnz_cache[scan8[3 + 8]] = (sl->cbp & 0x4000) >> 12;
2427
2428 104872 nnz_cache[scan8[0 + 12]] =
2429 104872 nnz_cache[scan8[1 + 12]] =
2430 104872 nnz_cache[scan8[2 + 12]] =
2431 104872 nnz_cache[scan8[3 + 12]] = (sl->cbp & 0x8000) >> 12;
2432 }
2433 }
2434
2435 8424819 return 0;
2436 }
2437
2438 416034 static void loop_filter(const H264Context *h, H264SliceContext *sl, int start_x, int end_x)
2439 {
2440 uint8_t *dest_y, *dest_cb, *dest_cr;
2441 int linesize, uvlinesize, mb_x, mb_y;
2442 416034 const int end_mb_y = sl->mb_y + FRAME_MBAFF(h);
2443 416034 const int old_slice_type = sl->slice_type;
2444 416034 const int pixel_shift = h->pixel_shift;
2445 416034 const int block_h = 16 >> h->chroma_y_shift;
2446
2447
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 416034 times.
416034 if (h->postpone_filter)
2448 return;
2449
2450
2/2
✓ Branch 0 taken 338911 times.
✓ Branch 1 taken 77123 times.
416034 if (sl->deblocking_filter) {
2451
2/2
✓ Branch 0 taken 11990138 times.
✓ Branch 1 taken 338911 times.
12329049 for (mb_x = start_x; mb_x < end_x; mb_x++)
2452
2/2
✓ Branch 0 taken 13001858 times.
✓ Branch 1 taken 11990138 times.
24991996 for (mb_y = end_mb_y - FRAME_MBAFF(h); mb_y <= end_mb_y; mb_y++) {
2453 int mb_xy, mb_type;
2454 13001858 mb_xy = sl->mb_xy = mb_x + mb_y * h->mb_stride;
2455 13001858 mb_type = h->cur_pic.mb_type[mb_xy];
2456
2457
2/2
✓ Branch 0 taken 2023440 times.
✓ Branch 1 taken 10978418 times.
13001858 if (FRAME_MBAFF(h))
2458 2023440 sl->mb_mbaff =
2459 2023440 sl->mb_field_decoding_flag = !!IS_INTERLACED(mb_type);
2460
2461 13001858 sl->mb_x = mb_x;
2462 13001858 sl->mb_y = mb_y;
2463 13001858 dest_y = h->cur_pic.f->data[0] +
2464 13001858 ((mb_x << pixel_shift) + mb_y * sl->linesize) * 16;
2465 26003716 dest_cb = h->cur_pic.f->data[1] +
2466
2/2
✓ Branch 0 taken 80328 times.
✓ Branch 1 taken 12921530 times.
13001858 (mb_x << pixel_shift) * (8 << CHROMA444(h)) +
2467 13001858 mb_y * sl->uvlinesize * block_h;
2468 26003716 dest_cr = h->cur_pic.f->data[2] +
2469
2/2
✓ Branch 0 taken 80328 times.
✓ Branch 1 taken 12921530 times.
13001858 (mb_x << pixel_shift) * (8 << CHROMA444(h)) +
2470 13001858 mb_y * sl->uvlinesize * block_h;
2471 // FIXME simplify above
2472
2473
2/2
✓ Branch 0 taken 4679076 times.
✓ Branch 1 taken 8322782 times.
13001858 if (MB_FIELD(sl)) {
2474 4679076 linesize = sl->mb_linesize = sl->linesize * 2;
2475 4679076 uvlinesize = sl->mb_uvlinesize = sl->uvlinesize * 2;
2476
2/2
✓ Branch 0 taken 2336700 times.
✓ Branch 1 taken 2342376 times.
4679076 if (mb_y & 1) { // FIXME move out of this function?
2477 2336700 dest_y -= sl->linesize * 15;
2478 2336700 dest_cb -= sl->uvlinesize * (block_h - 1);
2479 2336700 dest_cr -= sl->uvlinesize * (block_h - 1);
2480 }
2481 } else {
2482 8322782 linesize = sl->mb_linesize = sl->linesize;
2483 8322782 uvlinesize = sl->mb_uvlinesize = sl->uvlinesize;
2484 }
2485 13001858 backup_mb_border(h, sl, dest_y, dest_cb, dest_cr, linesize,
2486 uvlinesize, 0);
2487
2/2
✓ Branch 1 taken 1556961 times.
✓ Branch 2 taken 11444897 times.
13001858 if (fill_filter_caches(h, sl, mb_type))
2488 1556961 continue;
2489 11444897 sl->chroma_qp[0] = get_chroma_qp(h->ps.pps, 0, h->cur_pic.qscale_table[mb_xy]);
2490 11444897 sl->chroma_qp[1] = get_chroma_qp(h->ps.pps, 1, h->cur_pic.qscale_table[mb_xy]);
2491
2492
2/2
✓ Branch 0 taken 1731269 times.
✓ Branch 1 taken 9713628 times.
11444897 if (FRAME_MBAFF(h)) {
2493 1731269 ff_h264_filter_mb(h, sl, mb_x, mb_y, dest_y, dest_cb, dest_cr,
2494 linesize, uvlinesize);
2495 } else {
2496 9713628 ff_h264_filter_mb_fast(h, sl, mb_x, mb_y, dest_y, dest_cb,
2497 dest_cr, linesize, uvlinesize);
2498 }
2499 }
2500 }
2501 416034 sl->slice_type = old_slice_type;
2502 416034 sl->mb_x = end_x;
2503 416034 sl->mb_y = end_mb_y - FRAME_MBAFF(h);
2504 416034 sl->chroma_qp[0] = get_chroma_qp(h->ps.pps, 0, sl->qscale);
2505 416034 sl->chroma_qp[1] = get_chroma_qp(h->ps.pps, 1, sl->qscale);
2506 }
2507
2508 24071 static void predict_field_decoding_flag(const H264Context *h, H264SliceContext *sl)
2509 {
2510 24071 const int mb_xy = sl->mb_x + sl->mb_y * h->mb_stride;
2511 48142 int mb_type = (h->slice_table[mb_xy - 1] == sl->slice_num) ?
2512
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 24071 times.
48142 h->cur_pic.mb_type[mb_xy - 1] :
2513
2/2
✓ Branch 0 taken 23935 times.
✓ Branch 1 taken 136 times.
24071 (h->slice_table[mb_xy - h->mb_stride] == sl->slice_num) ?
2514 23935 h->cur_pic.mb_type[mb_xy - h->mb_stride] : 0;
2515 24071 sl->mb_mbaff = sl->mb_field_decoding_flag = IS_INTERLACED(mb_type) ? 1 : 0;
2516 24071 }
2517
2518 /**
2519 * Draw edges and report progress for the last MB row.
2520 */
2521 411474 static void decode_finish_row(const H264Context *h, H264SliceContext *sl)
2522 {
2523 411474 int top = 16 * (sl->mb_y >> FIELD_PICTURE(h));
2524 411474 int pic_height = 16 * h->mb_height >> FIELD_PICTURE(h);
2525 411474 int height = 16 << FRAME_MBAFF(h);
2526 411474 int deblock_border = (16 + 4) << FRAME_MBAFF(h);
2527
2528
2/2
✓ Branch 0 taken 335819 times.
✓ Branch 1 taken 75655 times.
411474 if (sl->deblocking_filter) {
2529
2/2
✓ Branch 0 taken 23235 times.
✓ Branch 1 taken 312584 times.
335819 if ((top + height) >= pic_height)
2530 23235 height += deblock_border;
2531 335819 top -= deblock_border;
2532 }
2533
2534
3/4
✓ Branch 0 taken 411474 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 23236 times.
✓ Branch 3 taken 388238 times.
411474 if (top >= pic_height || (top + height) < 0)
2535 23236 return;
2536
2537 388238 height = FFMIN(height, pic_height - top);
2538
2/2
✓ Branch 0 taken 23236 times.
✓ Branch 1 taken 365002 times.
388238 if (top < 0) {
2539 23236 height = top + height;
2540 23236 top = 0;
2541 }
2542
2543 388238 ff_h264_draw_horiz_band(h, sl, top, height);
2544
2545
3/4
✓ Branch 0 taken 251236 times.
✓ Branch 1 taken 137002 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 251236 times.
388238 if (h->droppable || h->er.error_occurred)
2546 137002 return;
2547
2548 251236 ff_thread_report_progress(&h->cur_pic_ptr->tf, top + height - 1,
2549 251236 h->picture_structure == PICT_BOTTOM_FIELD);
2550 }
2551
2552 38277 static void er_add_slice(H264SliceContext *sl,
2553 int startx, int starty,
2554 int endx, int endy, int status)
2555 {
2556
2/2
✓ Branch 0 taken 1200 times.
✓ Branch 1 taken 37077 times.
38277 if (!sl->h264->enable_er)
2557 1200 return;
2558
2559 if (CONFIG_ERROR_RESILIENCE) {
2560 37077 ff_er_add_slice(sl->er, startx, starty, endx, endy, status);
2561 }
2562 }
2563
2564 38277 static int decode_slice(struct AVCodecContext *avctx, void *arg)
2565 {
2566 38277 H264SliceContext *sl = arg;
2567 38277 const H264Context *h = sl->h264;
2568 38277 int lf_x_start = sl->mb_x;
2569 38277 int orig_deblock = sl->deblocking_filter;
2570 int ret;
2571
2572 38277 sl->linesize = h->cur_pic_ptr->f->linesize[0];
2573 38277 sl->uvlinesize = h->cur_pic_ptr->f->linesize[1];
2574
2575 38277 ret = alloc_scratch_buffers(sl, sl->linesize);
2576
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 38277 times.
38277 if (ret < 0)
2577 return ret;
2578
2579 38277 sl->mb_skip_run = -1;
2580
2581
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 38277 times.
38277 av_assert0(h->block_offset[15] == (4 * ((scan8[15] - scan8[0]) & 7) << h->pixel_shift) + 4 * sl->linesize * ((scan8[15] - scan8[0]) >> 3));
2582
2583
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 38277 times.
38277 if (h->postpone_filter)
2584 sl->deblocking_filter = 0;
2585
2586
4/4
✓ Branch 0 taken 33301 times.
✓ Branch 1 taken 4976 times.
✓ Branch 2 taken 9160 times.
✓ Branch 3 taken 24141 times.
38277 sl->is_complex = FRAME_MBAFF(h) || h->picture_structure != PICT_FRAME ||
2587 (CONFIG_GRAY && (h->flags & AV_CODEC_FLAG_GRAY));
2588
2589
5/6
✓ Branch 0 taken 37077 times.
✓ Branch 1 taken 1200 times.
✓ Branch 2 taken 27917 times.
✓ Branch 3 taken 9160 times.
✓ Branch 4 taken 27917 times.
✗ Branch 5 not taken.
38277 if (!(h->avctx->active_thread_type & FF_THREAD_SLICE) && h->picture_structure == PICT_FRAME && sl->er->error_status_table) {
2590 27917 const int start_i = av_clip(sl->resync_mb_x + sl->resync_mb_y * h->mb_width, 0, h->mb_num - 1);
2591
2/2
✓ Branch 0 taken 5956 times.
✓ Branch 1 taken 21961 times.
27917 if (start_i) {
2592 5956 int prev_status = sl->er->error_status_table[sl->er->mb_index2xy[start_i - 1]];
2593 5956 prev_status &= ~ VP_START;
2594
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5956 times.
5956 if (prev_status != (ER_MV_END | ER_DC_END | ER_AC_END))
2595 sl->er->error_occurred = 1;
2596 }
2597 }
2598
2599
2/2
✓ Branch 0 taken 21521 times.
✓ Branch 1 taken 16756 times.
38277 if (h->ps.pps->cabac) {
2600 /* realign */
2601 21521 align_get_bits(&sl->gb);
2602
2603 /* init cabac */
2604 43042 ret = ff_init_cabac_decoder(&sl->cabac,
2605 21521 sl->gb.buffer + get_bits_count(&sl->gb) / 8,
2606 21521 (get_bits_left(&sl->gb) + 7) / 8);
2607
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 21521 times.
21521 if (ret < 0)
2608 return ret;
2609
2610 21521 ff_h264_init_cabac_states(h, sl);
2611
2612 9780254 for (;;) {
2613 int ret, eos;
2614
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9801775 times.
9801775 if (sl->mb_x + sl->mb_y * h->mb_width >= sl->next_slice_idx) {
2615 av_log(h->avctx, AV_LOG_ERROR, "Slice overlaps with next at %d\n",
2616 sl->next_slice_idx);
2617 er_add_slice(sl, sl->resync_mb_x, sl->resync_mb_y, sl->mb_x,
2618 sl->mb_y, ER_MB_ERROR);
2619 return AVERROR_INVALIDDATA;
2620 }
2621
2622 9801775 ret = ff_h264_decode_mb_cabac(h, sl);
2623
2624
2/2
✓ Branch 0 taken 9801774 times.
✓ Branch 1 taken 1 times.
9801775 if (ret >= 0)
2625 9801774 ff_h264_hl_decode_mb(h, sl);
2626
2627 // FIXME optimal? or let mb_decode decode 16x32 ?
2628
4/4
✓ Branch 0 taken 9801774 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 903372 times.
✓ Branch 3 taken 8898402 times.
9801775 if (ret >= 0 && FRAME_MBAFF(h)) {
2629 903372 sl->mb_y++;
2630
2631 903372 ret = ff_h264_decode_mb_cabac(h, sl);
2632
2633
1/2
✓ Branch 0 taken 903372 times.
✗ Branch 1 not taken.
903372 if (ret >= 0)
2634 903372 ff_h264_hl_decode_mb(h, sl);
2635 903372 sl->mb_y--;
2636 }
2637 9801775 eos = get_cabac_terminate(&sl->cabac);
2638
2639
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9801775 times.
9801775 if ((h->workaround_bugs & FF_BUG_TRUNCATED) &&
2640 sl->cabac.bytestream > sl->cabac.bytestream_end + 2) {
2641 er_add_slice(sl, sl->resync_mb_x, sl->resync_mb_y, sl->mb_x - 1,
2642 sl->mb_y, ER_MB_END);
2643 if (sl->mb_x >= lf_x_start)
2644 loop_filter(h, sl, lf_x_start, sl->mb_x + 1);
2645 goto finish;
2646 }
2647
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 9801774 times.
9801775 if (sl->cabac.bytestream > sl->cabac.bytestream_end + 2 )
2648 1 av_log(h->avctx, AV_LOG_DEBUG, "bytestream overread %td\n", sl->cabac.bytestream_end - sl->cabac.bytestream);
2649
4/4
✓ Branch 0 taken 9801774 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 9801773 times.
9801775 if (ret < 0 || sl->cabac.bytestream > sl->cabac.bytestream_end + 4) {
2650 2 av_log(h->avctx, AV_LOG_ERROR,
2651 "error while decoding MB %d %d, bytestream %td\n",
2652 sl->mb_x, sl->mb_y,
2653 2 sl->cabac.bytestream_end - sl->cabac.bytestream);
2654 2 er_add_slice(sl, sl->resync_mb_x, sl->resync_mb_y, sl->mb_x,
2655 sl->mb_y, ER_MB_ERROR);
2656 2 return AVERROR_INVALIDDATA;
2657 }
2658
2659
2/2
✓ Branch 0 taken 250355 times.
✓ Branch 1 taken 9551418 times.
9801773 if (++sl->mb_x >= h->mb_width) {
2660 250355 loop_filter(h, sl, lf_x_start, sl->mb_x);
2661 250355 sl->mb_x = lf_x_start = 0;
2662 250355 decode_finish_row(h, sl);
2663 250355 ++sl->mb_y;
2664
4/4
✓ Branch 0 taken 229431 times.
✓ Branch 1 taken 20924 times.
✓ Branch 2 taken 60241 times.
✓ Branch 3 taken 169190 times.
250355 if (FIELD_OR_MBAFF_PICTURE(h)) {
2665 81165 ++sl->mb_y;
2666
4/4
✓ Branch 0 taken 20924 times.
✓ Branch 1 taken 60241 times.
✓ Branch 2 taken 19093 times.
✓ Branch 3 taken 1831 times.
81165 if (FRAME_MBAFF(h) && sl->mb_y < h->mb_height)
2667 19093 predict_field_decoding_flag(h, sl);
2668 }
2669 }
2670
2671
3/4
✓ Branch 0 taken 9780254 times.
✓ Branch 1 taken 21519 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 9780254 times.
9801773 if (eos || sl->mb_y >= h->mb_height) {
2672 ff_tlog(h->avctx, "slice end %d %d\n",
2673 get_bits_count(&sl->gb), sl->gb.size_in_bits);
2674 21519 er_add_slice(sl, sl->resync_mb_x, sl->resync_mb_y, sl->mb_x - 1,
2675 sl->mb_y, ER_MB_END);
2676
2/2
✓ Branch 0 taken 1382 times.
✓ Branch 1 taken 20137 times.
21519 if (sl->mb_x > lf_x_start)
2677 1382 loop_filter(h, sl, lf_x_start, sl->mb_x);
2678 21519 goto finish;
2679 }
2680 }
2681 } else {
2682 4578647 for (;;) {
2683 int ret;
2684
2685
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4595403 times.
4595403 if (sl->mb_x + sl->mb_y * h->mb_width >= sl->next_slice_idx) {
2686 av_log(h->avctx, AV_LOG_ERROR, "Slice overlaps with next at %d\n",
2687 sl->next_slice_idx);
2688 er_add_slice(sl, sl->resync_mb_x, sl->resync_mb_y, sl->mb_x,
2689 sl->mb_y, ER_MB_ERROR);
2690 return AVERROR_INVALIDDATA;
2691 }
2692
2693 4595403 ret = ff_h264_decode_mb_cavlc(h, sl);
2694
2695
1/2
✓ Branch 0 taken 4595403 times.
✗ Branch 1 not taken.
4595403 if (ret >= 0)
2696 4595403 ff_h264_hl_decode_mb(h, sl);
2697
2698 // FIXME optimal? or let mb_decode decode 16x32 ?
2699
3/4
✓ Branch 0 taken 4595403 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 254472 times.
✓ Branch 3 taken 4340931 times.
4595403 if (ret >= 0 && FRAME_MBAFF(h)) {
2700 254472 sl->mb_y++;
2701 254472 ret = ff_h264_decode_mb_cavlc(h, sl);
2702
2703
1/2
✓ Branch 0 taken 254472 times.
✗ Branch 1 not taken.
254472 if (ret >= 0)
2704 254472 ff_h264_hl_decode_mb(h, sl);
2705 254472 sl->mb_y--;
2706 }
2707
2708
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4595403 times.
4595403 if (ret < 0) {
2709 av_log(h->avctx, AV_LOG_ERROR,
2710 "error while decoding MB %d %d\n", sl->mb_x, sl->mb_y);
2711 er_add_slice(sl, sl->resync_mb_x, sl->resync_mb_y, sl->mb_x,
2712 sl->mb_y, ER_MB_ERROR);
2713 return ret;
2714 }
2715
2716
2/2
✓ Branch 0 taken 161119 times.
✓ Branch 1 taken 4434284 times.
4595403 if (++sl->mb_x >= h->mb_width) {
2717 161119 loop_filter(h, sl, lf_x_start, sl->mb_x);
2718 161119 sl->mb_x = lf_x_start = 0;
2719 161119 decode_finish_row(h, sl);
2720 161119 ++sl->mb_y;
2721
4/4
✓ Branch 0 taken 155642 times.
✓ Branch 1 taken 5477 times.
✓ Branch 2 taken 26666 times.
✓ Branch 3 taken 128976 times.
161119 if (FIELD_OR_MBAFF_PICTURE(h)) {
2722 32143 ++sl->mb_y;
2723
4/4
✓ Branch 0 taken 5477 times.
✓ Branch 1 taken 26666 times.
✓ Branch 2 taken 4978 times.
✓ Branch 3 taken 499 times.
32143 if (FRAME_MBAFF(h) && sl->mb_y < h->mb_height)
2724 4978 predict_field_decoding_flag(h, sl);
2725 }
2726
2/2
✓ Branch 0 taken 12666 times.
✓ Branch 1 taken 148453 times.
161119 if (sl->mb_y >= h->mb_height) {
2727 ff_tlog(h->avctx, "slice end %d %d\n",
2728 get_bits_count(&sl->gb), sl->gb.size_in_bits);
2729
2730
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 12666 times.
12666 if ( get_bits_left(&sl->gb) == 0
2731 || get_bits_left(&sl->gb) > 0 && !(h->avctx->err_recognition & AV_EF_AGGRESSIVE)) {
2732 12666 er_add_slice(sl, sl->resync_mb_x, sl->resync_mb_y,
2733 12666 sl->mb_x - 1, sl->mb_y, ER_MB_END);
2734
2735 12666 goto finish;
2736 } else {
2737 er_add_slice(sl, sl->resync_mb_x, sl->resync_mb_y,
2738 sl->mb_x, sl->mb_y, ER_MB_END);
2739
2740 return AVERROR_INVALIDDATA;
2741 }
2742 }
2743 }
2744
2745
4/4
✓ Branch 1 taken 238762 times.
✓ Branch 2 taken 4343975 times.
✓ Branch 3 taken 4090 times.
✓ Branch 4 taken 234672 times.
4582737 if (get_bits_left(&sl->gb) <= 0 && sl->mb_skip_run <= 0) {
2746 ff_tlog(h->avctx, "slice end %d %d\n",
2747 get_bits_count(&sl->gb), sl->gb.size_in_bits);
2748
2749
1/2
✓ Branch 1 taken 4090 times.
✗ Branch 2 not taken.
4090 if (get_bits_left(&sl->gb) == 0) {
2750 4090 er_add_slice(sl, sl->resync_mb_x, sl->resync_mb_y,
2751 4090 sl->mb_x - 1, sl->mb_y, ER_MB_END);
2752
2/2
✓ Branch 0 taken 3178 times.
✓ Branch 1 taken 912 times.
4090 if (sl->mb_x > lf_x_start)
2753 3178 loop_filter(h, sl, lf_x_start, sl->mb_x);
2754
2755 4090 goto finish;
2756 } else {
2757 er_add_slice(sl, sl->resync_mb_x, sl->resync_mb_y, sl->mb_x,
2758 sl->mb_y, ER_MB_ERROR);
2759
2760 return AVERROR_INVALIDDATA;
2761 }
2762 }
2763 }
2764 }
2765
2766 38275 finish:
2767 38275 sl->deblocking_filter = orig_deblock;
2768 38275 return 0;
2769 }
2770
2771 /**
2772 * Call decode_slice() for each context.
2773 *
2774 * @param h h264 master context
2775 */
2776 67481 int ff_h264_execute_decode_slices(H264Context *h)
2777 {
2778 67481 AVCodecContext *const avctx = h->avctx;
2779 H264SliceContext *sl;
2780 67481 int context_count = h->nb_slice_ctx_queued;
2781 67481 int ret = 0;
2782 int i, j;
2783
2784 67481 h->slice_ctx[0].next_slice_idx = INT_MAX;
2785
2786
3/4
✓ Branch 0 taken 67481 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 29654 times.
✓ Branch 3 taken 37827 times.
67481 if (h->avctx->hwaccel || context_count < 1)
2787 29654 return 0;
2788
2789
2/4
✓ Branch 0 taken 37827 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 37827 times.
37827 av_assert0(context_count && h->slice_ctx[context_count - 1].mb_y < h->mb_height);
2790
2791
2/2
✓ Branch 0 taken 37377 times.
✓ Branch 1 taken 450 times.
37827 if (context_count == 1) {
2792
2793 37377 h->slice_ctx[0].next_slice_idx = h->mb_width * h->mb_height;
2794 37377 h->postpone_filter = 0;
2795
2796 37377 ret = decode_slice(avctx, &h->slice_ctx[0]);
2797 37377 h->mb_y = h->slice_ctx[0].mb_y;
2798
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 37375 times.
37377 if (ret < 0)
2799 2 goto finish;
2800 } else {
2801
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 450 times.
450 av_assert0(context_count > 0);
2802
2/2
✓ Branch 0 taken 900 times.
✓ Branch 1 taken 450 times.
1350 for (i = 0; i < context_count; i++) {
2803 900 int next_slice_idx = h->mb_width * h->mb_height;
2804 int slice_idx;
2805
2806 900 sl = &h->slice_ctx[i];
2807
2808 /* make sure none of those slices overlap */
2809 900 slice_idx = sl->mb_y * h->mb_width + sl->mb_x;
2810
2/2
✓ Branch 0 taken 1800 times.
✓ Branch 1 taken 900 times.
2700 for (j = 0; j < context_count; j++) {
2811 1800 H264SliceContext *sl2 = &h->slice_ctx[j];
2812 1800 int slice_idx2 = sl2->mb_y * h->mb_width + sl2->mb_x;
2813
2814
4/4
✓ Branch 0 taken 900 times.
✓ Branch 1 taken 900 times.
✓ Branch 2 taken 450 times.
✓ Branch 3 taken 450 times.
1800 if (i == j || slice_idx2 < slice_idx)
2815 1350 continue;
2816 450 next_slice_idx = FFMIN(next_slice_idx, slice_idx2);
2817 }
2818 900 sl->next_slice_idx = next_slice_idx;
2819 }
2820
2821 450 avctx->execute(avctx, decode_slice, h->slice_ctx,
2822 NULL, context_count, sizeof(h->slice_ctx[0]));
2823
2824 /* pull back stuff from slices to master context */
2825 450 sl = &h->slice_ctx[context_count - 1];
2826 450 h->mb_y = sl->mb_y;
2827
2828
1/2
✓ Branch 0 taken 450 times.
✗ Branch 1 not taken.
450 if (h->postpone_filter) {
2829 h->postpone_filter = 0;
2830
2831 for (i = 0; i < context_count; i++) {
2832 int y_end, x_end;
2833
2834 sl = &h->slice_ctx[i];
2835 y_end = FFMIN(sl->mb_y + 1, h->mb_height);
2836 x_end = (sl->mb_y >= h->mb_height) ? h->mb_width : sl->mb_x;
2837
2838 for (j = sl->resync_mb_y; j < y_end; j += 1 + FIELD_OR_MBAFF_PICTURE(h)) {
2839 sl->mb_y = j;
2840 loop_filter(h, sl, j > sl->resync_mb_y ? 0 : sl->resync_mb_x,
2841 j == y_end - 1 ? x_end : h->mb_width);
2842 }
2843 }
2844 }
2845 }
2846
2847 450 finish:
2848 37827 h->nb_slice_ctx_queued = 0;
2849 37827 return ret;
2850 }
2851