FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/h264_slice.c
Date: 2026-04-23 02:20:26
Exec Total Coverage
Lines: 1375 1678 81.9%
Functions: 32 33 97.0%
Branches: 922 1263 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 29752 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 1071072 times.
✓ Branch 1 taken 29752 times.
1100824 for (i = 0; i < H264_MAX_PICTURE_COUNT; i++) {
123
6/6
✓ Branch 0 taken 149149 times.
✓ Branch 1 taken 921923 times.
✓ Branch 2 taken 26017 times.
✓ Branch 3 taken 123132 times.
✓ Branch 4 taken 2627 times.
✓ Branch 5 taken 23390 times.
1071072 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 24127 ff_h264_unref_picture(&h->DPB[i]);
126 }
127 }
128 29752 }
129
130 38237 static int alloc_scratch_buffers(H264SliceContext *sl, int linesize)
131 {
132 38237 const H264Context *h = sl->h264;
133 38237 int alloc_size = FFALIGN(FFABS(linesize) + 32, 32);
134
135 38237 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 38237 av_fast_malloc(&sl->edge_emu_buffer, &sl->edge_emu_buffer_allocated, alloc_size * 2 * 21);
139
140 38237 av_fast_mallocz(&sl->top_borders[0], &sl->top_borders_allocated[0],
141 38237 h->mb_width * 16 * 3 * sizeof(uint8_t) * 2);
142 38237 av_fast_mallocz(&sl->top_borders[1], &sl->top_borders_allocated[1],
143 38237 h->mb_width * 16 * 3 * sizeof(uint8_t) * 2);
144
145
2/4
✓ Branch 0 taken 38237 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 38237 times.
✗ Branch 3 not taken.
38237 if (!sl->bipred_scratchpad || !sl->edge_emu_buffer ||
146
2/4
✓ Branch 0 taken 38237 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 38237 times.
38237 !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 38237 return 0;
160 }
161
162 625 static int init_table_pools(H264Context *h)
163 {
164 625 const int big_mb_num = h->mb_stride * (h->mb_height + 1) + 1;
165 625 const int mb_array_size = h->mb_stride * h->mb_height;
166 625 const int b4_stride = h->mb_width * 4 + 1;
167 625 const int b4_array_size = b4_stride * h->mb_height * 4;
168
169 625 h->qscale_table_pool = av_refstruct_pool_alloc(big_mb_num + h->mb_stride, 0);
170 625 h->mb_type_pool = av_refstruct_pool_alloc((big_mb_num + h->mb_stride) *
171 sizeof(uint32_t), 0);
172 625 h->motion_val_pool = av_refstruct_pool_alloc(2 * (b4_array_size + 4) *
173 sizeof(int16_t), 0);
174 625 h->ref_index_pool = av_refstruct_pool_alloc(4 * mb_array_size, 0);
175
176
3/6
✓ Branch 0 taken 625 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 625 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 625 times.
✗ Branch 5 not taken.
625 if (!h->qscale_table_pool || !h->mb_type_pool || !h->motion_val_pool ||
177
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 625 times.
625 !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 625 return 0;
186 }
187
188 26334 static int alloc_picture(H264Context *h, H264Picture *pic)
189 {
190 26334 int i, ret = 0;
191
192
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 26334 times.
26334 av_assert0(!pic->f->data[0]);
193
194
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 26334 times.
26334 if (h->sei.common.lcevc.info) {
195 HEVCSEILCEVC *lcevc = &h->sei.common.lcevc;
196 ret = ff_frame_new_side_data_from_buf(h->avctx, pic->f, AV_FRAME_DATA_LCEVC, &lcevc->info);
197 if (ret < 0)
198 return ret;
199 }
200
201 26334 pic->tf.f = pic->f;
202 26334 ret = ff_thread_get_ext_buffer(h->avctx, &pic->tf,
203 26334 pic->reference ? AV_GET_BUFFER_FLAG_REF : 0);
204
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 26334 times.
26334 if (ret < 0)
205 goto fail;
206
207
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 26334 times.
26334 if (pic->needs_fg) {
208 pic->f_grain->format = pic->f->format;
209 pic->f_grain->width = pic->f->width;
210 pic->f_grain->height = pic->f->height;
211 ret = ff_thread_get_buffer(h->avctx, pic->f_grain, 0);
212 if (ret < 0)
213 goto fail;
214 }
215
216 26334 ret = ff_hwaccel_frame_priv_alloc(h->avctx, &pic->hwaccel_picture_private);
217
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 26334 times.
26334 if (ret < 0)
218 goto fail;
219
220
2/2
✓ Branch 0 taken 42 times.
✓ Branch 1 taken 26292 times.
26334 if (h->decode_error_flags_pool) {
221 42 pic->decode_error_flags = av_refstruct_pool_get(h->decode_error_flags_pool);
222
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 42 times.
42 if (!pic->decode_error_flags)
223 goto fail;
224 42 atomic_init(pic->decode_error_flags, 0);
225 }
226
227 if (CONFIG_GRAY && !h->avctx->hwaccel && h->flags & AV_CODEC_FLAG_GRAY && pic->f->data[2]) {
228 int h_chroma_shift, v_chroma_shift;
229 av_pix_fmt_get_chroma_sub_sample(pic->f->format,
230 &h_chroma_shift, &v_chroma_shift);
231
232 for(i=0; i<AV_CEIL_RSHIFT(pic->f->height, v_chroma_shift); i++) {
233 memset(pic->f->data[1] + pic->f->linesize[1]*i,
234 0x80, AV_CEIL_RSHIFT(pic->f->width, h_chroma_shift));
235 memset(pic->f->data[2] + pic->f->linesize[2]*i,
236 0x80, AV_CEIL_RSHIFT(pic->f->width, h_chroma_shift));
237 }
238 }
239
240
2/2
✓ Branch 0 taken 625 times.
✓ Branch 1 taken 25709 times.
26334 if (!h->qscale_table_pool) {
241 625 ret = init_table_pools(h);
242
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 625 times.
625 if (ret < 0)
243 goto fail;
244 }
245
246 26334 pic->qscale_table_base = av_refstruct_pool_get(h->qscale_table_pool);
247 26334 pic->mb_type_base = av_refstruct_pool_get(h->mb_type_pool);
248
2/4
✓ Branch 0 taken 26334 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 26334 times.
26334 if (!pic->qscale_table_base || !pic->mb_type_base)
249 goto fail;
250
251 26334 pic->mb_type = pic->mb_type_base + 2 * h->mb_stride + 1;
252 26334 pic->qscale_table = pic->qscale_table_base + 2 * h->mb_stride + 1;
253
254
2/2
✓ Branch 0 taken 52668 times.
✓ Branch 1 taken 26334 times.
79002 for (i = 0; i < 2; i++) {
255 52668 pic->motion_val_base[i] = av_refstruct_pool_get(h->motion_val_pool);
256 52668 pic->ref_index[i] = av_refstruct_pool_get(h->ref_index_pool);
257
2/4
✓ Branch 0 taken 52668 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 52668 times.
52668 if (!pic->motion_val_base[i] || !pic->ref_index[i])
258 goto fail;
259
260 52668 pic->motion_val[i] = pic->motion_val_base[i] + 4;
261 }
262
263 26334 pic->pps = av_refstruct_ref_c(h->ps.pps);
264
265 26334 pic->mb_width = h->mb_width;
266 26334 pic->mb_height = h->mb_height;
267 26334 pic->mb_stride = h->mb_stride;
268
269 26334 return 0;
270 fail:
271 ff_h264_unref_picture(pic);
272 return (ret < 0) ? ret : AVERROR(ENOMEM);
273 }
274
275 26334 static int find_unused_picture(const H264Context *h)
276 {
277 int i;
278
279
1/2
✓ Branch 0 taken 88915 times.
✗ Branch 1 not taken.
88915 for (i = 0; i < H264_MAX_PICTURE_COUNT; i++) {
280
2/2
✓ Branch 0 taken 26334 times.
✓ Branch 1 taken 62581 times.
88915 if (!h->DPB[i].f->buf[0])
281 26334 return i;
282 }
283 return AVERROR_INVALIDDATA;
284 }
285
286
287 #define IN_RANGE(a, b, size) (((void*)(a) >= (void*)(b)) && ((void*)(a) < (void*)((b) + (size))))
288
289 #define REBASE_PICTURE(pic, new_ctx, old_ctx) \
290 (((pic) && (pic) >= (old_ctx)->DPB && \
291 (pic) < (old_ctx)->DPB + H264_MAX_PICTURE_COUNT) ? \
292 &(new_ctx)->DPB[(pic) - (old_ctx)->DPB] : NULL)
293
294 174 static void copy_picture_range(H264Picture **to, H264Picture *const *from, int count,
295 H264Context *new_base, const H264Context *old_base)
296 {
297 int i;
298
299
2/2
✓ Branch 0 taken 4756 times.
✓ Branch 1 taken 174 times.
4930 for (i = 0; i < count; i++) {
300 av_assert1(!from[i] ||
301 IN_RANGE(from[i], old_base, 1) ||
302 IN_RANGE(from[i], old_base->DPB, H264_MAX_PICTURE_COUNT));
303
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);
304 }
305 174 }
306
307 26 static void color_frame(AVFrame *frame, const int c[4])
308 {
309 26 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(frame->format);
310
311
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 26 times.
26 av_assert0(desc->flags & AV_PIX_FMT_FLAG_PLANAR);
312
313
2/2
✓ Branch 0 taken 78 times.
✓ Branch 1 taken 26 times.
104 for (int p = 0; p < desc->nb_components; p++) {
314 78 uint8_t *dst = frame->data[p];
315
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;
316
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;
317
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;
318
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 78 times.
78 if (desc->comp[0].depth >= 9) {
319 ((uint16_t*)dst)[0] = c[p];
320 av_memcpy_backptr(dst + 2, 2, bytes - 2);
321 dst += frame->linesize[p];
322 for (int y = 1; y < height; y++) {
323 memcpy(dst, frame->data[p], 2*bytes);
324 dst += frame->linesize[p];
325 }
326 } else {
327
2/2
✓ Branch 0 taken 31168 times.
✓ Branch 1 taken 78 times.
31246 for (int y = 0; y < height; y++) {
328 31168 memset(dst, c[p], bytes);
329 31168 dst += frame->linesize[p];
330 }
331 }
332 }
333 26 }
334
335 static int h264_slice_header_init(H264Context *h);
336
337 58 int ff_h264_update_thread_context(AVCodecContext *dst,
338 const AVCodecContext *src)
339 {
340 58 H264Context *h = dst->priv_data, *h1 = src->priv_data;
341 58 int inited = h->context_initialized, err = 0;
342 58 int need_reinit = 0;
343 int i, ret;
344
345
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 58 times.
58 if (dst == src)
346 return 0;
347
348
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)
349 return AVERROR_INVALIDDATA;
350
351
2/2
✓ Branch 0 taken 42 times.
✓ Branch 1 taken 16 times.
58 if (inited &&
352
1/2
✓ Branch 0 taken 42 times.
✗ Branch 1 not taken.
42 (h->width != h1->width ||
353
1/2
✓ Branch 0 taken 42 times.
✗ Branch 1 not taken.
42 h->height != h1->height ||
354
1/2
✓ Branch 0 taken 42 times.
✗ Branch 1 not taken.
42 h->mb_width != h1->mb_width ||
355
1/2
✓ Branch 0 taken 42 times.
✗ Branch 1 not taken.
42 h->mb_height != h1->mb_height ||
356
1/2
✓ Branch 0 taken 42 times.
✗ Branch 1 not taken.
42 !h->ps.sps ||
357
1/2
✓ Branch 0 taken 42 times.
✗ Branch 1 not taken.
42 h->ps.sps->bit_depth_luma != h1->ps.sps->bit_depth_luma ||
358
1/2
✓ Branch 0 taken 42 times.
✗ Branch 1 not taken.
42 h->ps.sps->chroma_format_idc != h1->ps.sps->chroma_format_idc ||
359
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 42 times.
42 h->ps.sps->vui.matrix_coeffs != h1->ps.sps->vui.matrix_coeffs)) {
360 need_reinit = 1;
361 }
362
363 /* copy block_offset since frame_start may not be called */
364 58 memcpy(h->block_offset, h1->block_offset, sizeof(h->block_offset));
365
366 // SPS/PPS
367
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++)
368 1856 av_refstruct_replace(&h->ps.sps_list[i], h1->ps.sps_list[i]);
369
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++)
370 14848 av_refstruct_replace(&h->ps.pps_list[i], h1->ps.pps_list[i]);
371
372 58 av_refstruct_replace(&h->ps.pps, h1->ps.pps);
373 58 h->ps.sps = h1->ps.sps;
374
375
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) {
376 16 h->width = h1->width;
377 16 h->height = h1->height;
378 16 h->mb_height = h1->mb_height;
379 16 h->mb_width = h1->mb_width;
380 16 h->mb_num = h1->mb_num;
381 16 h->mb_stride = h1->mb_stride;
382 16 h->b_stride = h1->b_stride;
383 16 h->x264_build = h1->x264_build;
384
385
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) {
386
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 16 times.
16 if ((err = h264_slice_header_init(h)) < 0) {
387 av_log(h->avctx, AV_LOG_ERROR, "h264_slice_header_init() failed");
388 return err;
389 }
390 }
391
392 /* copy block_offset since frame_start may not be called */
393 16 memcpy(h->block_offset, h1->block_offset, sizeof(h->block_offset));
394 }
395
396 58 h->width_from_caller = h1->width_from_caller;
397 58 h->height_from_caller = h1->height_from_caller;
398 58 h->first_field = h1->first_field;
399 58 h->picture_structure = h1->picture_structure;
400 58 h->mb_aff_frame = h1->mb_aff_frame;
401 58 h->droppable = h1->droppable;
402
403
2/2
✓ Branch 0 taken 2088 times.
✓ Branch 1 taken 58 times.
2146 for (i = 0; i < H264_MAX_PICTURE_COUNT; i++) {
404 2088 ret = ff_h264_replace_picture(&h->DPB[i], &h1->DPB[i]);
405
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2088 times.
2088 if (ret < 0)
406 return ret;
407 }
408
409
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);
410 58 ret = ff_h264_replace_picture(&h->cur_pic, &h1->cur_pic);
411
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 58 times.
58 if (ret < 0)
412 return ret;
413
414 58 h->enable_er = h1->enable_er;
415 58 h->workaround_bugs = h1->workaround_bugs;
416 58 h->droppable = h1->droppable;
417
418 // extradata/NAL handling
419 58 h->is_avc = h1->is_avc;
420 58 h->nal_length_size = h1->nal_length_size;
421
422 58 memcpy(&h->poc, &h1->poc, sizeof(h->poc));
423
424 58 memcpy(h->short_ref, h1->short_ref, sizeof(h->short_ref));
425 58 memcpy(h->long_ref, h1->long_ref, sizeof(h->long_ref));
426 58 memcpy(h->delayed_pic, h1->delayed_pic, sizeof(h->delayed_pic));
427 58 memcpy(h->last_pocs, h1->last_pocs, sizeof(h->last_pocs));
428
429 58 h->next_output_pic = h1->next_output_pic;
430 58 h->next_outputed_poc = h1->next_outputed_poc;
431 58 h->poc_offset = h1->poc_offset;
432
433 58 memcpy(h->mmco, h1->mmco, sizeof(h->mmco));
434 58 h->nb_mmco = h1->nb_mmco;
435 58 h->mmco_reset = h1->mmco_reset;
436 58 h->explicit_ref_marking = h1->explicit_ref_marking;
437 58 h->long_ref_count = h1->long_ref_count;
438 58 h->short_ref_count = h1->short_ref_count;
439
440 58 copy_picture_range(h->short_ref, h1->short_ref, 32, h, h1);
441 58 copy_picture_range(h->long_ref, h1->long_ref, 32, h, h1);
442 58 copy_picture_range(h->delayed_pic, h1->delayed_pic,
443 FF_ARRAY_ELEMS(h->delayed_pic), h, h1);
444
445 58 h->frame_recovered = h1->frame_recovered;
446
447 58 ret = ff_h2645_sei_ctx_replace(&h->sei.common, &h1->sei.common);
448
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 58 times.
58 if (ret < 0)
449 return ret;
450
451 58 h->sei.common.unregistered.x264_build = h1->sei.common.unregistered.x264_build;
452
453
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 42 times.
58 if (!h->cur_pic_ptr)
454 16 return 0;
455
456
2/2
✓ Branch 0 taken 27 times.
✓ Branch 1 taken 15 times.
42 if (!h->droppable) {
457 27 err = ff_h264_execute_ref_pic_marking(h);
458 27 h->poc.prev_poc_msb = h->poc.poc_msb;
459 27 h->poc.prev_poc_lsb = h->poc.poc_lsb;
460 }
461 42 h->poc.prev_frame_num_offset = h->poc.frame_num_offset;
462 42 h->poc.prev_frame_num = h->poc.frame_num;
463
464 42 h->recovery_frame = h1->recovery_frame;
465 42 h->non_gray = h1->non_gray;
466
467 42 return err;
468 }
469
470 46 int ff_h264_update_thread_context_for_user(AVCodecContext *dst,
471 const AVCodecContext *src)
472 {
473 46 H264Context *h = dst->priv_data;
474 46 const H264Context *h1 = src->priv_data;
475
476 46 h->is_avc = h1->is_avc;
477 46 h->nal_length_size = h1->nal_length_size;
478
479 46 return 0;
480 }
481
482 26334 static int h264_frame_start(H264Context *h)
483 {
484 H264Picture *pic;
485 int i, ret;
486 26334 const int pixel_shift = h->pixel_shift;
487
488
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 26334 times.
26334 if (!ff_thread_can_start_frame(h->avctx)) {
489 av_log(h->avctx, AV_LOG_ERROR, "Attempt to start a frame outside SETUP state\n");
490 return AVERROR_BUG;
491 }
492
493 26334 release_unused_pictures(h, 1);
494 26334 h->cur_pic_ptr = NULL;
495
496 26334 i = find_unused_picture(h);
497
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 26334 times.
26334 if (i < 0) {
498 av_log(h->avctx, AV_LOG_ERROR, "no frame buffer available\n");
499 return i;
500 }
501 26334 pic = &h->DPB[i];
502
503
2/2
✓ Branch 0 taken 18178 times.
✓ Branch 1 taken 8156 times.
26334 pic->reference = h->droppable ? 0 : h->picture_structure;
504 26334 pic->field_picture = h->picture_structure != PICT_FRAME;
505 26334 pic->frame_num = h->poc.frame_num;
506 /*
507 * Zero key_frame here; IDR markings per slice in frame or fields are ORed
508 * in later.
509 * See decode_nal_units().
510 */
511 26334 pic->f->flags &= ~AV_FRAME_FLAG_KEY;
512 26334 pic->mmco_reset = 0;
513 26334 pic->recovered = 0;
514 26334 pic->invalid_gap = 0;
515 26334 pic->sei_recovery_frame_cnt = h->sei.recovery_point.recovery_frame_cnt;
516
517 26334 pic->f->pict_type = h->slice_ctx[0].slice_type;
518
519 26334 pic->f->crop_left = h->crop_left;
520 26334 pic->f->crop_right = h->crop_right;
521 26334 pic->f->crop_top = h->crop_top;
522 26334 pic->f->crop_bottom = h->crop_bottom;
523
524 26334 pic->needs_fg =
525 26334 h->sei.common.film_grain_characteristics &&
526 h->sei.common.film_grain_characteristics->present &&
527
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 26334 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
26334 !h->avctx->hwaccel &&
528 !(h->avctx->export_side_data & AV_CODEC_EXPORT_DATA_FILM_GRAIN);
529
530
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 26334 times.
26334 if ((ret = alloc_picture(h, pic)) < 0)
531 return ret;
532
533 26334 h->cur_pic_ptr = pic;
534 26334 ff_h264_unref_picture(&h->cur_pic);
535 if (CONFIG_ERROR_RESILIENCE) {
536 26334 ff_h264_set_erpic(&h->er.cur_pic, NULL);
537 }
538
539
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 26334 times.
26334 if ((ret = ff_h264_ref_picture(&h->cur_pic, h->cur_pic_ptr)) < 0)
540 return ret;
541
542
2/2
✓ Branch 0 taken 26634 times.
✓ Branch 1 taken 26334 times.
52968 for (i = 0; i < h->nb_slice_ctx; i++) {
543 26634 h->slice_ctx[i].linesize = h->cur_pic_ptr->f->linesize[0];
544 26634 h->slice_ctx[i].uvlinesize = h->cur_pic_ptr->f->linesize[1];
545 }
546
547
2/2
✓ Branch 0 taken 26034 times.
✓ Branch 1 taken 300 times.
26334 if (CONFIG_ERROR_RESILIENCE && h->enable_er) {
548 26034 ff_er_frame_start(&h->er);
549 26034 ff_h264_set_erpic(&h->er.last_pic, NULL);
550 26034 ff_h264_set_erpic(&h->er.next_pic, NULL);
551 }
552
553
2/2
✓ Branch 0 taken 421344 times.
✓ Branch 1 taken 26334 times.
447678 for (i = 0; i < 16; i++) {
554 421344 h->block_offset[i] = (4 * ((scan8[i] - scan8[0]) & 7) << pixel_shift) + 4 * pic->f->linesize[0] * ((scan8[i] - scan8[0]) >> 3);
555 421344 h->block_offset[48 + i] = (4 * ((scan8[i] - scan8[0]) & 7) << pixel_shift) + 8 * pic->f->linesize[0] * ((scan8[i] - scan8[0]) >> 3);
556 }
557
2/2
✓ Branch 0 taken 421344 times.
✓ Branch 1 taken 26334 times.
447678 for (i = 0; i < 16; i++) {
558 421344 h->block_offset[16 + i] =
559 421344 h->block_offset[32 + i] = (4 * ((scan8[i] - scan8[0]) & 7) << pixel_shift) + 4 * pic->f->linesize[1] * ((scan8[i] - scan8[0]) >> 3);
560 421344 h->block_offset[48 + 16 + i] =
561 421344 h->block_offset[48 + 32 + i] = (4 * ((scan8[i] - scan8[0]) & 7) << pixel_shift) + 8 * pic->f->linesize[1] * ((scan8[i] - scan8[0]) >> 3);
562 }
563
564 /* We mark the current picture as non-reference after allocating it, so
565 * that if we break out due to an error it can be released automatically
566 * in the next ff_mpv_frame_start().
567 */
568 26334 h->cur_pic_ptr->reference = 0;
569
570 26334 h->cur_pic_ptr->field_poc[0] = h->cur_pic_ptr->field_poc[1] = INT_MAX;
571
572 26334 h->next_output_pic = NULL;
573
574 26334 h->postpone_filter = 0;
575
576
4/4
✓ Branch 0 taken 2871 times.
✓ Branch 1 taken 23463 times.
✓ Branch 2 taken 2358 times.
✓ Branch 3 taken 513 times.
26334 h->mb_aff_frame = h->ps.sps->mb_aff && (h->picture_structure == PICT_FRAME);
577
578
2/2
✓ Branch 0 taken 2079 times.
✓ Branch 1 taken 24255 times.
26334 if (h->sei.common.unregistered.x264_build >= 0)
579 2079 h->x264_build = h->sei.common.unregistered.x264_build;
580
581 assert(h->cur_pic_ptr->long_ref == 0);
582
583 26334 return 0;
584 }
585
586 12993478 static av_always_inline void backup_mb_border(const H264Context *h, H264SliceContext *sl,
587 const uint8_t *src_y,
588 const uint8_t *src_cb, const uint8_t *src_cr,
589 int linesize, int uvlinesize,
590 int simple)
591 {
592 uint8_t *top_border;
593 12993478 int top_idx = 1;
594 12993478 const int pixel_shift = h->pixel_shift;
595 12993478 int chroma444 = CHROMA444(h);
596 12993478 int chroma422 = CHROMA422(h);
597
598 12993478 src_y -= linesize;
599 12993478 src_cb -= uvlinesize;
600 12993478 src_cr -= uvlinesize;
601
602
3/4
✓ Branch 0 taken 12993478 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2023440 times.
✓ Branch 3 taken 10970038 times.
12993478 if (!simple && FRAME_MBAFF(h)) {
603
2/2
✓ Branch 0 taken 1011720 times.
✓ Branch 1 taken 1011720 times.
2023440 if (sl->mb_y & 1) {
604
2/2
✓ Branch 0 taken 748050 times.
✓ Branch 1 taken 263670 times.
1011720 if (!MB_MBAFF(sl)) {
605 748050 top_border = sl->top_borders[0][sl->mb_x];
606 748050 AV_COPY128(top_border, src_y + 15 * linesize);
607
2/2
✓ Branch 0 taken 213139 times.
✓ Branch 1 taken 534911 times.
748050 if (pixel_shift)
608 213139 AV_COPY128(top_border + 16, src_y + 15 * linesize + 16);
609 if (simple || !CONFIG_GRAY || !(h->flags & AV_CODEC_FLAG_GRAY)) {
610
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 748050 times.
748050 if (chroma444) {
611 if (pixel_shift) {
612 AV_COPY128(top_border + 32, src_cb + 15 * uvlinesize);
613 AV_COPY128(top_border + 48, src_cb + 15 * uvlinesize + 16);
614 AV_COPY128(top_border + 64, src_cr + 15 * uvlinesize);
615 AV_COPY128(top_border + 80, src_cr + 15 * uvlinesize + 16);
616 } else {
617 AV_COPY128(top_border + 16, src_cb + 15 * uvlinesize);
618 AV_COPY128(top_border + 32, src_cr + 15 * uvlinesize);
619 }
620
2/2
✓ Branch 0 taken 106510 times.
✓ Branch 1 taken 641540 times.
748050 } else if (chroma422) {
621
1/2
✓ Branch 0 taken 106510 times.
✗ Branch 1 not taken.
106510 if (pixel_shift) {
622 106510 AV_COPY128(top_border + 32, src_cb + 15 * uvlinesize);
623 106510 AV_COPY128(top_border + 48, src_cr + 15 * uvlinesize);
624 } else {
625 AV_COPY64(top_border + 16, src_cb + 15 * uvlinesize);
626 AV_COPY64(top_border + 24, src_cr + 15 * uvlinesize);
627 }
628 } else {
629
2/2
✓ Branch 0 taken 106629 times.
✓ Branch 1 taken 534911 times.
641540 if (pixel_shift) {
630 106629 AV_COPY128(top_border + 32, src_cb + 7 * uvlinesize);
631 106629 AV_COPY128(top_border + 48, src_cr + 7 * uvlinesize);
632 } else {
633 534911 AV_COPY64(top_border + 16, src_cb + 7 * uvlinesize);
634 534911 AV_COPY64(top_border + 24, src_cr + 7 * uvlinesize);
635 }
636 }
637 }
638 }
639
2/2
✓ Branch 0 taken 263670 times.
✓ Branch 1 taken 748050 times.
1011720 } else if (MB_MBAFF(sl)) {
640 263670 top_idx = 0;
641 } else
642 748050 return;
643 }
644
645 12245428 top_border = sl->top_borders[top_idx][sl->mb_x];
646 /* There are two lines saved, the line above the top macroblock
647 * of a pair, and the line above the bottom macroblock. */
648 12245428 AV_COPY128(top_border, src_y + 16 * linesize);
649
2/2
✓ Branch 0 taken 1389541 times.
✓ Branch 1 taken 10855887 times.
12245428 if (pixel_shift)
650 1389541 AV_COPY128(top_border + 16, src_y + 16 * linesize + 16);
651
652 if (simple || !CONFIG_GRAY || !(h->flags & AV_CODEC_FLAG_GRAY)) {
653
2/2
✓ Branch 0 taken 80328 times.
✓ Branch 1 taken 12165100 times.
12245428 if (chroma444) {
654
2/2
✓ Branch 0 taken 19800 times.
✓ Branch 1 taken 60528 times.
80328 if (pixel_shift) {
655 19800 AV_COPY128(top_border + 32, src_cb + 16 * linesize);
656 19800 AV_COPY128(top_border + 48, src_cb + 16 * linesize + 16);
657 19800 AV_COPY128(top_border + 64, src_cr + 16 * linesize);
658 19800 AV_COPY128(top_border + 80, src_cr + 16 * linesize + 16);
659 } else {
660 60528 AV_COPY128(top_border + 16, src_cb + 16 * linesize);
661 60528 AV_COPY128(top_border + 32, src_cr + 16 * linesize);
662 }
663
2/2
✓ Branch 0 taken 588635 times.
✓ Branch 1 taken 11576465 times.
12165100 } else if (chroma422) {
664
2/2
✓ Branch 0 taken 584675 times.
✓ Branch 1 taken 3960 times.
588635 if (pixel_shift) {
665 584675 AV_COPY128(top_border + 32, src_cb + 16 * uvlinesize);
666 584675 AV_COPY128(top_border + 48, src_cr + 16 * uvlinesize);
667 } else {
668 3960 AV_COPY64(top_border + 16, src_cb + 16 * uvlinesize);
669 3960 AV_COPY64(top_border + 24, src_cr + 16 * uvlinesize);
670 }
671 } else {
672
2/2
✓ Branch 0 taken 785066 times.
✓ Branch 1 taken 10791399 times.
11576465 if (pixel_shift) {
673 785066 AV_COPY128(top_border + 32, src_cb + 8 * uvlinesize);
674 785066 AV_COPY128(top_border + 48, src_cr + 8 * uvlinesize);
675 } else {
676 10791399 AV_COPY64(top_border + 16, src_cb + 8 * uvlinesize);
677 10791399 AV_COPY64(top_border + 24, src_cr + 8 * uvlinesize);
678 }
679 }
680 }
681 }
682
683 /**
684 * Initialize implicit_weight table.
685 * @param field 0/1 initialize the weight for interlaced MBAFF
686 * -1 initializes the rest
687 */
688 4061 static void implicit_weight_table(const H264Context *h, H264SliceContext *sl, int field)
689 {
690 int ref0, ref1, i, cur_poc, ref_start, ref_count0, ref_count1;
691
692
2/2
✓ Branch 0 taken 8122 times.
✓ Branch 1 taken 4061 times.
12183 for (i = 0; i < 2; i++) {
693 8122 sl->pwt.luma_weight_flag[i] = 0;
694 8122 sl->pwt.chroma_weight_flag[i] = 0;
695 }
696
697
2/2
✓ Branch 0 taken 2349 times.
✓ Branch 1 taken 1712 times.
4061 if (field < 0) {
698
1/2
✓ Branch 0 taken 2349 times.
✗ Branch 1 not taken.
2349 if (h->picture_structure == PICT_FRAME) {
699 2349 cur_poc = h->cur_pic_ptr->poc;
700 } else {
701 cur_poc = h->cur_pic_ptr->field_poc[h->picture_structure - 1];
702 }
703
6/6
✓ Branch 0 taken 1143 times.
✓ Branch 1 taken 1206 times.
✓ Branch 2 taken 827 times.
✓ Branch 3 taken 316 times.
✓ Branch 4 taken 817 times.
✓ Branch 5 taken 10 times.
2349 if (sl->ref_count[0] == 1 && sl->ref_count[1] == 1 && !FRAME_MBAFF(h) &&
704
2/2
✓ Branch 0 taken 543 times.
✓ Branch 1 taken 274 times.
817 sl->ref_list[0][0].poc + (int64_t)sl->ref_list[1][0].poc == 2LL * cur_poc) {
705 543 sl->pwt.use_weight = 0;
706 543 sl->pwt.use_weight_chroma = 0;
707 543 return;
708 }
709 1806 ref_start = 0;
710 1806 ref_count0 = sl->ref_count[0];
711 1806 ref_count1 = sl->ref_count[1];
712 } else {
713 1712 cur_poc = h->cur_pic_ptr->field_poc[field];
714 1712 ref_start = 16;
715 1712 ref_count0 = 16 + 2 * sl->ref_count[0];
716 1712 ref_count1 = 16 + 2 * sl->ref_count[1];
717 }
718
719 3518 sl->pwt.use_weight = 2;
720 3518 sl->pwt.use_weight_chroma = 2;
721 3518 sl->pwt.luma_log2_weight_denom = 5;
722 3518 sl->pwt.chroma_log2_weight_denom = 5;
723
724
2/2
✓ Branch 0 taken 11930 times.
✓ Branch 1 taken 3518 times.
15448 for (ref0 = ref_start; ref0 < ref_count0; ref0++) {
725 11930 int64_t poc0 = sl->ref_list[0][ref0].poc;
726
2/2
✓ Branch 0 taken 22061 times.
✓ Branch 1 taken 11930 times.
33991 for (ref1 = ref_start; ref1 < ref_count1; ref1++) {
727 22061 int w = 32;
728
2/4
✓ Branch 0 taken 22061 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 22061 times.
✗ Branch 3 not taken.
22061 if (!sl->ref_list[0][ref0].parent->long_ref && !sl->ref_list[1][ref1].parent->long_ref) {
729 22061 int poc1 = sl->ref_list[1][ref1].poc;
730 22061 int td = av_clip_int8(poc1 - poc0);
731
2/2
✓ Branch 0 taken 21994 times.
✓ Branch 1 taken 67 times.
22061 if (td) {
732 21994 int tb = av_clip_int8(cur_poc - poc0);
733 21994 int tx = (16384 + (FFABS(td) >> 1)) / td;
734 21994 int dist_scale_factor = (tb * tx + 32) >> 8;
735
3/4
✓ Branch 0 taken 21982 times.
✓ Branch 1 taken 12 times.
✓ Branch 2 taken 21982 times.
✗ Branch 3 not taken.
21994 if (dist_scale_factor >= -64 && dist_scale_factor <= 128)
736 21982 w = 64 - dist_scale_factor;
737 }
738 }
739
2/2
✓ Branch 0 taken 6157 times.
✓ Branch 1 taken 15904 times.
22061 if (field < 0) {
740 6157 sl->pwt.implicit_weight[ref0][ref1][0] =
741 6157 sl->pwt.implicit_weight[ref0][ref1][1] = w;
742 } else {
743 15904 sl->pwt.implicit_weight[ref0][ref1][field] = w;
744 }
745 }
746 }
747 }
748
749 /**
750 * initialize scan tables
751 */
752 625 static void init_scan_tables(H264Context *h)
753 {
754 int i;
755
2/2
✓ Branch 0 taken 10000 times.
✓ Branch 1 taken 625 times.
10625 for (i = 0; i < 16; i++) {
756 #define TRANSPOSE(x) ((x) >> 2) | (((x) << 2) & 0xF)
757 10000 h->zigzag_scan[i] = TRANSPOSE(ff_zigzag_scan[i]);
758 10000 h->field_scan[i] = TRANSPOSE(field_scan[i]);
759 #undef TRANSPOSE
760 }
761
2/2
✓ Branch 0 taken 40000 times.
✓ Branch 1 taken 625 times.
40625 for (i = 0; i < 64; i++) {
762 #define TRANSPOSE(x) ((x) >> 3) | (((x) & 7) << 3)
763 40000 h->zigzag_scan8x8[i] = TRANSPOSE(ff_zigzag_direct[i]);
764 40000 h->zigzag_scan8x8_cavlc[i] = TRANSPOSE(zigzag_scan8x8_cavlc[i]);
765 40000 h->field_scan8x8[i] = TRANSPOSE(field_scan8x8[i]);
766 40000 h->field_scan8x8_cavlc[i] = TRANSPOSE(field_scan8x8_cavlc[i]);
767 #undef TRANSPOSE
768 }
769
2/2
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 611 times.
625 if (h->ps.sps->transform_bypass) { // FIXME same ugly
770 14 memcpy(h->zigzag_scan_q0 , ff_zigzag_scan , sizeof(h->zigzag_scan_q0 ));
771 14 memcpy(h->zigzag_scan8x8_q0 , ff_zigzag_direct , sizeof(h->zigzag_scan8x8_q0 ));
772 14 memcpy(h->zigzag_scan8x8_cavlc_q0 , zigzag_scan8x8_cavlc , sizeof(h->zigzag_scan8x8_cavlc_q0));
773 14 memcpy(h->field_scan_q0 , field_scan , sizeof(h->field_scan_q0 ));
774 14 memcpy(h->field_scan8x8_q0 , field_scan8x8 , sizeof(h->field_scan8x8_q0 ));
775 14 memcpy(h->field_scan8x8_cavlc_q0 , field_scan8x8_cavlc , sizeof(h->field_scan8x8_cavlc_q0 ));
776 } else {
777 611 memcpy(h->zigzag_scan_q0 , h->zigzag_scan , sizeof(h->zigzag_scan_q0 ));
778 611 memcpy(h->zigzag_scan8x8_q0 , h->zigzag_scan8x8 , sizeof(h->zigzag_scan8x8_q0 ));
779 611 memcpy(h->zigzag_scan8x8_cavlc_q0 , h->zigzag_scan8x8_cavlc , sizeof(h->zigzag_scan8x8_cavlc_q0));
780 611 memcpy(h->field_scan_q0 , h->field_scan , sizeof(h->field_scan_q0 ));
781 611 memcpy(h->field_scan8x8_q0 , h->field_scan8x8 , sizeof(h->field_scan8x8_q0 ));
782 611 memcpy(h->field_scan8x8_cavlc_q0 , h->field_scan8x8_cavlc , sizeof(h->field_scan8x8_cavlc_q0 ));
783 }
784 625 }
785
786 29328 static enum AVPixelFormat get_pixel_format(H264Context *h, int force_callback)
787 {
788 #define HWACCEL_MAX (CONFIG_H264_DXVA2_HWACCEL + \
789 (CONFIG_H264_D3D11VA_HWACCEL * 2) + \
790 CONFIG_H264_D3D12VA_HWACCEL + \
791 CONFIG_H264_NVDEC_HWACCEL + \
792 CONFIG_H264_VAAPI_HWACCEL + \
793 CONFIG_H264_VIDEOTOOLBOX_HWACCEL + \
794 CONFIG_H264_VDPAU_HWACCEL + \
795 CONFIG_H264_VULKAN_HWACCEL)
796 29328 enum AVPixelFormat pix_fmts[HWACCEL_MAX + 2], *fmt = pix_fmts;
797
798
3/6
✓ Branch 0 taken 155 times.
✓ Branch 1 taken 373 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 28800 times.
✗ Branch 5 not taken.
29328 switch (h->ps.sps->bit_depth_luma) {
799 155 case 9:
800
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 155 times.
155 if (CHROMA444(h)) {
801 if (h->avctx->colorspace == AVCOL_SPC_RGB) {
802 *fmt++ = AV_PIX_FMT_GBRP9;
803 } else
804 *fmt++ = AV_PIX_FMT_YUV444P9;
805
2/2
✓ Branch 0 taken 52 times.
✓ Branch 1 taken 103 times.
155 } else if (CHROMA422(h))
806 52 *fmt++ = AV_PIX_FMT_YUV422P9;
807 else
808 103 *fmt++ = AV_PIX_FMT_YUV420P9;
809 155 break;
810 373 case 10:
811 #if CONFIG_H264_VIDEOTOOLBOX_HWACCEL
812 if (h->avctx->colorspace != AVCOL_SPC_RGB)
813 *fmt++ = AV_PIX_FMT_VIDEOTOOLBOX;
814 #endif
815 #if CONFIG_H264_VULKAN_HWACCEL
816 *fmt++ = AV_PIX_FMT_VULKAN;
817 #endif
818 #if CONFIG_H264_NVDEC_HWACCEL
819 *fmt++ = AV_PIX_FMT_CUDA;
820 #endif
821
2/2
✓ Branch 0 taken 51 times.
✓ Branch 1 taken 322 times.
373 if (CHROMA444(h)) {
822
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 51 times.
51 if (h->avctx->colorspace == AVCOL_SPC_RGB) {
823 *fmt++ = AV_PIX_FMT_GBRP10;
824 } else
825 51 *fmt++ = AV_PIX_FMT_YUV444P10;
826
2/2
✓ Branch 0 taken 164 times.
✓ Branch 1 taken 158 times.
322 } else if (CHROMA422(h))
827 164 *fmt++ = AV_PIX_FMT_YUV422P10;
828 else {
829 #if CONFIG_H264_VAAPI_HWACCEL
830 // Just add as candidate. Whether VAProfileH264High10 usable or
831 // not is decided by vaapi_decode_make_config() defined in FFmpeg
832 // and vaQueryCodingProfile() defined in libva.
833 158 *fmt++ = AV_PIX_FMT_VAAPI;
834 #endif
835 158 *fmt++ = AV_PIX_FMT_YUV420P10;
836 }
837 373 break;
838 case 12:
839 #if CONFIG_H264_VULKAN_HWACCEL
840 *fmt++ = AV_PIX_FMT_VULKAN;
841 #endif
842 if (CHROMA444(h)) {
843 if (h->avctx->colorspace == AVCOL_SPC_RGB) {
844 *fmt++ = AV_PIX_FMT_GBRP12;
845 } else
846 *fmt++ = AV_PIX_FMT_YUV444P12;
847 } else if (CHROMA422(h))
848 *fmt++ = AV_PIX_FMT_YUV422P12;
849 else
850 *fmt++ = AV_PIX_FMT_YUV420P12;
851 break;
852 case 14:
853 if (CHROMA444(h)) {
854 if (h->avctx->colorspace == AVCOL_SPC_RGB) {
855 *fmt++ = AV_PIX_FMT_GBRP14;
856 } else
857 *fmt++ = AV_PIX_FMT_YUV444P14;
858 } else if (CHROMA422(h))
859 *fmt++ = AV_PIX_FMT_YUV422P14;
860 else
861 *fmt++ = AV_PIX_FMT_YUV420P14;
862 break;
863 28800 case 8:
864 #if CONFIG_H264_VDPAU_HWACCEL
865 28800 *fmt++ = AV_PIX_FMT_VDPAU;
866 #endif
867 #if CONFIG_H264_VULKAN_HWACCEL
868 *fmt++ = AV_PIX_FMT_VULKAN;
869 #endif
870 #if CONFIG_H264_NVDEC_HWACCEL
871 *fmt++ = AV_PIX_FMT_CUDA;
872 #endif
873 #if CONFIG_H264_VIDEOTOOLBOX_HWACCEL
874 if (h->avctx->colorspace != AVCOL_SPC_RGB)
875 *fmt++ = AV_PIX_FMT_VIDEOTOOLBOX;
876 #endif
877
2/2
✓ Branch 0 taken 484 times.
✓ Branch 1 taken 28316 times.
28800 if (CHROMA444(h)) {
878
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 484 times.
484 if (h->avctx->colorspace == AVCOL_SPC_RGB)
879 *fmt++ = AV_PIX_FMT_GBRP;
880
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 482 times.
484 else if (h->avctx->color_range == AVCOL_RANGE_JPEG)
881 2 *fmt++ = AV_PIX_FMT_YUVJ444P;
882 else
883 482 *fmt++ = AV_PIX_FMT_YUV444P;
884
2/2
✓ Branch 0 taken 22 times.
✓ Branch 1 taken 28294 times.
28316 } else if (CHROMA422(h)) {
885
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 22 times.
22 if (h->avctx->color_range == AVCOL_RANGE_JPEG)
886 *fmt++ = AV_PIX_FMT_YUVJ422P;
887 else
888 22 *fmt++ = AV_PIX_FMT_YUV422P;
889 } else {
890 #if CONFIG_H264_DXVA2_HWACCEL
891 *fmt++ = AV_PIX_FMT_DXVA2_VLD;
892 #endif
893 #if CONFIG_H264_D3D11VA_HWACCEL
894 *fmt++ = AV_PIX_FMT_D3D11VA_VLD;
895 *fmt++ = AV_PIX_FMT_D3D11;
896 #endif
897 #if CONFIG_H264_D3D12VA_HWACCEL
898 *fmt++ = AV_PIX_FMT_D3D12;
899 #endif
900 #if CONFIG_H264_VAAPI_HWACCEL
901 28294 *fmt++ = AV_PIX_FMT_VAAPI;
902 #endif
903
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 28291 times.
28294 if (h->avctx->color_range == AVCOL_RANGE_JPEG)
904 3 *fmt++ = AV_PIX_FMT_YUVJ420P;
905 else
906 28291 *fmt++ = AV_PIX_FMT_YUV420P;
907 }
908 28800 break;
909 default:
910 av_log(h->avctx, AV_LOG_ERROR,
911 "Unsupported bit depth %d\n", h->ps.sps->bit_depth_luma);
912 return AVERROR_INVALIDDATA;
913 }
914
915 29328 *fmt = AV_PIX_FMT_NONE;
916
917
2/2
✓ Branch 0 taken 86580 times.
✓ Branch 1 taken 609 times.
87189 for (int i = 0; pix_fmts[i] != AV_PIX_FMT_NONE; i++)
918
4/4
✓ Branch 0 taken 28978 times.
✓ Branch 1 taken 57602 times.
✓ Branch 2 taken 28719 times.
✓ Branch 3 taken 259 times.
86580 if (pix_fmts[i] == h->avctx->pix_fmt && !force_callback)
919 28719 return pix_fmts[i];
920 609 return ff_get_format(h->avctx, pix_fmts);
921 }
922
923 /* export coded and cropped frame dimensions to AVCodecContext */
924 29063 static void init_dimensions(H264Context *h)
925 {
926 29063 const SPS *sps = h->ps.sps;
927 29063 int cr = sps->crop_right;
928 29063 int cl = sps->crop_left;
929 29063 int ct = sps->crop_top;
930 29063 int cb = sps->crop_bottom;
931 29063 int width = h->width - (cr + cl);
932 29063 int height = h->height - (ct + cb);
933
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 29063 times.
29063 av_assert0(sps->crop_right + sps->crop_left < (unsigned)h->width);
934
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 29063 times.
29063 av_assert0(sps->crop_top + sps->crop_bottom < (unsigned)h->height);
935
936 /* handle container cropping */
937
3/4
✓ Branch 0 taken 26805 times.
✓ Branch 1 taken 2258 times.
✓ Branch 2 taken 26805 times.
✗ Branch 3 not taken.
29063 if (h->width_from_caller > 0 && h->height_from_caller > 0 &&
938
3/4
✓ Branch 0 taken 26804 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 26804 times.
✗ Branch 3 not taken.
26805 !sps->crop_top && !sps->crop_left &&
939
2/2
✓ Branch 0 taken 26793 times.
✓ Branch 1 taken 11 times.
26804 FFALIGN(h->width_from_caller, 16) == FFALIGN(width, 16) &&
940
1/2
✓ Branch 0 taken 26793 times.
✗ Branch 1 not taken.
26793 FFALIGN(h->height_from_caller, 16) == FFALIGN(height, 16) &&
941
2/2
✓ Branch 0 taken 26792 times.
✓ Branch 1 taken 1 times.
26793 h->width_from_caller <= width &&
942
2/2
✓ Branch 0 taken 26784 times.
✓ Branch 1 taken 8 times.
26792 h->height_from_caller <= height) {
943 26784 width = h->width_from_caller;
944 26784 height = h->height_from_caller;
945 26784 cl = 0;
946 26784 ct = 0;
947 26784 cr = h->width - width;
948 26784 cb = h->height - height;
949 } else {
950 2279 h->width_from_caller = 0;
951 2279 h->height_from_caller = 0;
952 }
953
954 29063 h->avctx->coded_width = h->width;
955 29063 h->avctx->coded_height = h->height;
956 29063 h->avctx->width = width;
957 29063 h->avctx->height = height;
958 29063 h->crop_right = cr;
959 29063 h->crop_left = cl;
960 29063 h->crop_top = ct;
961 29063 h->crop_bottom = cb;
962 29063 }
963
964 625 static int h264_slice_header_init(H264Context *h)
965 {
966 625 const SPS *sps = h->ps.sps;
967 int i, ret;
968
969
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 625 times.
625 if (!sps) {
970 ret = AVERROR_INVALIDDATA;
971 goto fail;
972 }
973
974 625 ff_set_sar(h->avctx, sps->vui.sar);
975 625 av_pix_fmt_get_chroma_sub_sample(h->avctx->pix_fmt,
976 &h->chroma_x_shift, &h->chroma_y_shift);
977
978
2/2
✓ Branch 0 taken 205 times.
✓ Branch 1 taken 420 times.
625 if (sps->timing_info_present_flag) {
979 205 int64_t den = sps->time_scale;
980
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 205 times.
205 if (h->x264_build < 44U)
981 den *= 2;
982 205 av_reduce(&h->avctx->framerate.den, &h->avctx->framerate.num,
983 205 sps->num_units_in_tick * 2, den, 1 << 30);
984 }
985
986 625 ff_h264_free_tables(h);
987
988 625 h->first_field = 0;
989 625 h->prev_interlaced_frame = 1;
990
991 625 init_scan_tables(h);
992 625 ret = ff_h264_alloc_tables(h);
993
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 625 times.
625 if (ret < 0) {
994 av_log(h->avctx, AV_LOG_ERROR, "Could not allocate memory\n");
995 goto fail;
996 }
997
998
2/4
✓ Branch 0 taken 625 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 625 times.
✗ Branch 3 not taken.
625 if (sps->bit_depth_luma < 8 || sps->bit_depth_luma > 14 ||
999
2/4
✓ Branch 0 taken 625 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 625 times.
625 sps->bit_depth_luma == 11 || sps->bit_depth_luma == 13
1000 ) {
1001 av_log(h->avctx, AV_LOG_ERROR, "Unsupported bit depth %d\n",
1002 sps->bit_depth_luma);
1003 ret = AVERROR_INVALIDDATA;
1004 goto fail;
1005 }
1006
1007 625 h->cur_bit_depth_luma =
1008 625 h->avctx->bits_per_raw_sample = sps->bit_depth_luma;
1009 625 h->cur_chroma_format_idc = sps->chroma_format_idc;
1010 625 h->pixel_shift = sps->bit_depth_luma > 8;
1011 625 h->chroma_format_idc = sps->chroma_format_idc;
1012 625 h->bit_depth_luma = sps->bit_depth_luma;
1013
1014 625 ff_h264dsp_init(&h->h264dsp, sps->bit_depth_luma,
1015 625 sps->chroma_format_idc);
1016 625 ff_h264chroma_init(&h->h264chroma, sps->bit_depth_chroma);
1017 625 ff_h264qpel_init(&h->h264qpel, sps->bit_depth_luma);
1018 625 ff_h264_pred_init(&h->hpc, AV_CODEC_ID_H264, sps->bit_depth_luma,
1019 625 sps->chroma_format_idc);
1020 625 ff_videodsp_init(&h->vdsp, sps->bit_depth_luma);
1021
1022
2/2
✓ Branch 0 taken 624 times.
✓ Branch 1 taken 1 times.
625 if (!HAVE_THREADS || !(h->avctx->active_thread_type & FF_THREAD_SLICE)) {
1023 624 ff_h264_slice_context_init(h, &h->slice_ctx[0]);
1024 } else {
1025
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 times.
3 for (i = 0; i < h->nb_slice_ctx; i++) {
1026 2 H264SliceContext *sl = &h->slice_ctx[i];
1027
1028 2 sl->h264 = h;
1029 2 sl->intra4x4_pred_mode = h->intra4x4_pred_mode + i * 8 * 2 * h->mb_stride;
1030 2 sl->mvd_table[0] = h->mvd_table[0] + i * 8 * 2 * h->mb_stride;
1031 2 sl->mvd_table[1] = h->mvd_table[1] + i * 8 * 2 * h->mb_stride;
1032
1033 2 ff_h264_slice_context_init(h, sl);
1034 }
1035 }
1036
1037 625 h->context_initialized = 1;
1038
1039 625 return 0;
1040 fail:
1041 ff_h264_free_tables(h);
1042 h->context_initialized = 0;
1043 return ret;
1044 }
1045
1046 57438 static enum AVPixelFormat non_j_pixfmt(enum AVPixelFormat a)
1047 {
1048
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 57436 times.
57438 switch (a) {
1049 2 case AV_PIX_FMT_YUVJ420P: return AV_PIX_FMT_YUV420P;
1050 case AV_PIX_FMT_YUVJ422P: return AV_PIX_FMT_YUV422P;
1051 case AV_PIX_FMT_YUVJ444P: return AV_PIX_FMT_YUV444P;
1052 57436 default:
1053 57436 return a;
1054 }
1055 }
1056
1057 29063 static int h264_init_ps(H264Context *h, const H264SliceContext *sl, int first_slice)
1058 {
1059 const SPS *sps;
1060 29063 int needs_reinit = 0, must_reinit, ret;
1061
1062
2/2
✓ Branch 0 taken 28868 times.
✓ Branch 1 taken 195 times.
29063 if (first_slice)
1063 28868 av_refstruct_replace(&h->ps.pps, h->ps.pps_list[sl->pps_id]);
1064
1065
2/2
✓ Branch 0 taken 611 times.
✓ Branch 1 taken 28452 times.
29063 if (h->ps.sps != h->ps.pps->sps) {
1066 611 h->ps.sps = h->ps.pps->sps;
1067
1068
2/2
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 604 times.
611 if (h->mb_width != h->ps.sps->mb_width ||
1069
1/2
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
7 h->mb_height != h->ps.sps->mb_height ||
1070
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 1 times.
7 h->cur_bit_depth_luma != h->ps.sps->bit_depth_luma ||
1071
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 5 times.
6 h->cur_chroma_format_idc != h->ps.sps->chroma_format_idc
1072 )
1073 606 needs_reinit = 1;
1074
1075
2/2
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 600 times.
611 if (h->bit_depth_luma != h->ps.sps->bit_depth_luma ||
1076
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 10 times.
11 h->chroma_format_idc != h->ps.sps->chroma_format_idc)
1077 601 needs_reinit = 1;
1078 }
1079 29063 sps = h->ps.sps;
1080
1081
2/2
✓ Branch 0 taken 28462 times.
✓ Branch 1 taken 601 times.
57525 must_reinit = (h->context_initialized &&
1082
2/2
✓ Branch 0 taken 28456 times.
✓ Branch 1 taken 6 times.
28462 ( 16*sps->mb_width != h->avctx->coded_width
1083
1/2
✓ Branch 0 taken 28456 times.
✗ Branch 1 not taken.
28456 || 16*sps->mb_height != h->avctx->coded_height
1084
2/2
✓ Branch 0 taken 28455 times.
✓ Branch 1 taken 1 times.
28456 || h->cur_bit_depth_luma != sps->bit_depth_luma
1085
2/2
✓ Branch 0 taken 28454 times.
✓ Branch 1 taken 1 times.
28455 || h->cur_chroma_format_idc != sps->chroma_format_idc
1086
1/2
✓ Branch 0 taken 28454 times.
✗ Branch 1 not taken.
28454 || h->mb_width != sps->mb_width
1087
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 28454 times.
28454 || h->mb_height != sps->mb_height
1088 ));
1089
2/2
✓ Branch 0 taken 28719 times.
✓ Branch 1 taken 344 times.
29063 if (h->avctx->pix_fmt == AV_PIX_FMT_NONE
1090
2/2
✓ Branch 3 taken 3 times.
✓ Branch 4 taken 28716 times.
28719 || (non_j_pixfmt(h->avctx->pix_fmt) != non_j_pixfmt(get_pixel_format(h, 0))))
1091 347 must_reinit = 1;
1092
1093
4/4
✓ Branch 0 taken 28868 times.
✓ Branch 1 taken 195 times.
✓ Branch 3 taken 84 times.
✓ Branch 4 taken 28784 times.
29063 if (first_slice && av_cmp_q(sps->vui.sar, h->avctx->sample_aspect_ratio))
1094 84 must_reinit = 1;
1095
1096
1/2
✓ Branch 0 taken 29063 times.
✗ Branch 1 not taken.
29063 if (!h->setup_finished) {
1097 29063 h->avctx->profile = ff_h264_get_profile(sps);
1098 29063 h->avctx->level = sps->level_idc;
1099 29063 h->avctx->refs = sps->ref_frame_count;
1100
1101 29063 h->mb_width = sps->mb_width;
1102 29063 h->mb_height = sps->mb_height;
1103 29063 h->mb_num = h->mb_width * h->mb_height;
1104 29063 h->mb_stride = h->mb_width + 1;
1105
1106 29063 h->b_stride = h->mb_width * 4;
1107
1108 29063 h->chroma_y_shift = sps->chroma_format_idc <= 1; // 400 uses yuv420p
1109
1110 29063 h->width = 16 * h->mb_width;
1111 29063 h->height = 16 * h->mb_height;
1112
1113 29063 init_dimensions(h);
1114
1115
2/2
✓ Branch 0 taken 1288 times.
✓ Branch 1 taken 27775 times.
29063 if (sps->vui.video_signal_type_present_flag) {
1116 2576 h->avctx->color_range = sps->vui.video_full_range_flag > 0 ? AVCOL_RANGE_JPEG
1117
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1286 times.
1288 : AVCOL_RANGE_MPEG;
1118
2/2
✓ Branch 0 taken 1190 times.
✓ Branch 1 taken 98 times.
1288 if (sps->vui.colour_description_present_flag) {
1119
2/2
✓ Branch 0 taken 19 times.
✓ Branch 1 taken 1171 times.
1190 if (h->avctx->colorspace != sps->vui.matrix_coeffs)
1120 19 needs_reinit = 1;
1121 1190 h->avctx->color_primaries = sps->vui.colour_primaries;
1122 1190 h->avctx->color_trc = sps->vui.transfer_characteristics;
1123 1190 h->avctx->colorspace = sps->vui.matrix_coeffs;
1124 }
1125 }
1126
1127
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 29063 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
29063 if (h->sei.common.alternative_transfer.present &&
1128 av_color_transfer_name(h->sei.common.alternative_transfer.preferred_transfer_characteristics) &&
1129 h->sei.common.alternative_transfer.preferred_transfer_characteristics != AVCOL_TRC_UNSPECIFIED) {
1130 h->avctx->color_trc = h->sei.common.alternative_transfer.preferred_transfer_characteristics;
1131 }
1132 }
1133 29063 h->avctx->chroma_sample_location = sps->vui.chroma_location;
1134
1135
5/6
✓ Branch 0 taken 28462 times.
✓ Branch 1 taken 601 times.
✓ Branch 2 taken 28454 times.
✓ Branch 3 taken 8 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 28454 times.
29063 if (!h->context_initialized || must_reinit || needs_reinit) {
1136 609 int flush_changes = h->context_initialized;
1137 609 h->context_initialized = 0;
1138
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 609 times.
609 if (sl != h->slice_ctx) {
1139 av_log(h->avctx, AV_LOG_ERROR,
1140 "changing width %d -> %d / height %d -> %d on "
1141 "slice %d\n",
1142 h->width, h->avctx->coded_width,
1143 h->height, h->avctx->coded_height,
1144 h->current_slice + 1);
1145 return AVERROR_INVALIDDATA;
1146 }
1147
1148 av_assert1(first_slice);
1149
1150
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 601 times.
609 if (flush_changes)
1151 8 ff_h264_flush_change(h);
1152
1153
5/6
✓ Branch 0 taken 254 times.
✓ Branch 1 taken 355 times.
✓ Branch 2 taken 251 times.
✓ Branch 3 taken 3 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 609 times.
609 if ((ret = get_pixel_format(h, must_reinit || needs_reinit)) < 0)
1154 return ret;
1155 609 h->avctx->pix_fmt = ret;
1156
1157 609 av_log(h->avctx, AV_LOG_VERBOSE, "Reinit context to %dx%d, "
1158 609 "pix_fmt: %s\n", h->width, h->height, av_get_pix_fmt_name(h->avctx->pix_fmt));
1159
1160
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 609 times.
609 if ((ret = h264_slice_header_init(h)) < 0) {
1161 av_log(h->avctx, AV_LOG_ERROR,
1162 "h264_slice_header_init() failed\n");
1163 return ret;
1164 }
1165 }
1166
1167 29063 return 0;
1168 }
1169
1170 25639 static int h264_export_frame_props(H264Context *h)
1171 {
1172 25639 const SPS *sps = h->ps.sps;
1173 25639 H264Picture *cur = h->cur_pic_ptr;
1174 25639 AVFrame *out = cur->f;
1175 25639 int interlaced_frame = 0, top_field_first = 0;
1176 int ret;
1177
1178 25639 out->flags &= ~AV_FRAME_FLAG_INTERLACED;
1179 25639 out->repeat_pict = 0;
1180
1181 /* Signal interlacing information externally. */
1182 /* Prioritize picture timing SEI information over used
1183 * decoding process if it exists. */
1184
2/2
✓ Branch 0 taken 1538 times.
✓ Branch 1 taken 24101 times.
25639 if (h->sei.picture_timing.present) {
1185 1538 int ret = ff_h264_sei_process_picture_timing(&h->sei.picture_timing, sps,
1186 1538 h->avctx);
1187
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1538 times.
1538 if (ret < 0) {
1188 av_log(h->avctx, AV_LOG_ERROR, "Error processing a picture timing SEI\n");
1189 if (h->avctx->err_recognition & AV_EF_EXPLODE)
1190 return ret;
1191 h->sei.picture_timing.present = 0;
1192 }
1193 }
1194
1195
4/4
✓ Branch 0 taken 1616 times.
✓ Branch 1 taken 24023 times.
✓ Branch 2 taken 1530 times.
✓ Branch 3 taken 86 times.
27169 if (sps->pic_struct_present_flag && h->sei.picture_timing.present) {
1196 1530 const H264SEIPictureTiming *pt = &h->sei.picture_timing;
1197
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) {
1198 242 case H264_SEI_PIC_STRUCT_FRAME:
1199 242 break;
1200 378 case H264_SEI_PIC_STRUCT_TOP_FIELD:
1201 case H264_SEI_PIC_STRUCT_BOTTOM_FIELD:
1202 378 interlaced_frame = 1;
1203 378 break;
1204 743 case H264_SEI_PIC_STRUCT_TOP_BOTTOM:
1205 case H264_SEI_PIC_STRUCT_BOTTOM_TOP:
1206
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))
1207 739 interlaced_frame = 1;
1208 else
1209 // try to flag soft telecine progressive
1210 4 interlaced_frame = !!h->prev_interlaced_frame;
1211 743 break;
1212 167 case H264_SEI_PIC_STRUCT_TOP_BOTTOM_TOP:
1213 case H264_SEI_PIC_STRUCT_BOTTOM_TOP_BOTTOM:
1214 /* Signal the possibility of telecined film externally
1215 * (pic_struct 5,6). From these hints, let the applications
1216 * decide if they apply deinterlacing. */
1217 167 out->repeat_pict = 1;
1218 167 break;
1219 case H264_SEI_PIC_STRUCT_FRAME_DOUBLING:
1220 out->repeat_pict = 2;
1221 break;
1222 case H264_SEI_PIC_STRUCT_FRAME_TRIPLING:
1223 out->repeat_pict = 4;
1224 break;
1225 }
1226
1227
2/2
✓ Branch 0 taken 653 times.
✓ Branch 1 taken 877 times.
1530 if ((pt->ct_type & 3) &&
1228
2/2
✓ Branch 0 taken 489 times.
✓ Branch 1 taken 164 times.
653 pt->pic_struct <= H264_SEI_PIC_STRUCT_BOTTOM_TOP)
1229 489 interlaced_frame = ((pt->ct_type & (1 << 1)) != 0);
1230 } else {
1231 /* Derive interlacing flag from used decoding process. */
1232
4/4
✓ Branch 0 taken 22518 times.
✓ Branch 1 taken 1591 times.
✓ Branch 2 taken 2876 times.
✓ Branch 3 taken 19642 times.
24109 interlaced_frame = !!FIELD_OR_MBAFF_PICTURE(h);
1233 }
1234 25639 h->prev_interlaced_frame = interlaced_frame;
1235
1236
2/2
✓ Branch 0 taken 3393 times.
✓ Branch 1 taken 22246 times.
25639 if (cur->field_poc[0] != cur->field_poc[1]) {
1237 /* Derive top_field_first from field pocs. */
1238 3393 top_field_first = (cur->field_poc[0] < cur->field_poc[1]);
1239 } else {
1240
4/4
✓ Branch 0 taken 335 times.
✓ Branch 1 taken 21911 times.
✓ Branch 2 taken 249 times.
✓ Branch 3 taken 86 times.
22246 if (sps->pic_struct_present_flag && h->sei.picture_timing.present) {
1241 /* Use picture timing SEI information. Even if it is a
1242 * information of a past frame, better than nothing. */
1243
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 ||
1244
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)
1245 3 top_field_first = 1;
1246
2/2
✓ Branch 0 taken 2558 times.
✓ Branch 1 taken 19439 times.
21997 } else if (interlaced_frame) {
1247 /* Default to top field first when pic_struct_present_flag
1248 * is not set but interlaced frame detected */
1249 2558 top_field_first = 1;
1250 } // else
1251 /* Most likely progressive */
1252 }
1253
1254 25639 out->flags |= (AV_FRAME_FLAG_INTERLACED * interlaced_frame) |
1255 25639 (AV_FRAME_FLAG_TOP_FIELD_FIRST * top_field_first);
1256
1257 25639 ret = ff_h2645_sei_to_frame(out, &h->sei.common, AV_CODEC_ID_H264, h->avctx,
1258 25639 &sps->vui, sps->bit_depth_luma, sps->bit_depth_chroma,
1259 25639 cur->poc + (unsigned)(h->poc_offset << 5));
1260
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 25639 times.
25639 if (ret < 0)
1261 return ret;
1262
1263
2/2
✓ Branch 0 taken 653 times.
✓ Branch 1 taken 24986 times.
25639 if (h->sei.picture_timing.timecode_cnt > 0) {
1264 uint32_t *tc_sd;
1265 char tcbuf[AV_TIMECODE_STR_SIZE];
1266 AVFrameSideData *tcside;
1267 653 ret = ff_frame_new_side_data(h->avctx, out, AV_FRAME_DATA_S12M_TIMECODE,
1268 sizeof(uint32_t)*4, &tcside);
1269
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 653 times.
653 if (ret < 0)
1270 return ret;
1271
1272
1/2
✓ Branch 0 taken 653 times.
✗ Branch 1 not taken.
653 if (tcside) {
1273 653 tc_sd = (uint32_t*)tcside->data;
1274 653 tc_sd[0] = h->sei.picture_timing.timecode_cnt;
1275
1276
2/2
✓ Branch 0 taken 954 times.
✓ Branch 1 taken 653 times.
1607 for (int i = 0; i < tc_sd[0]; i++) {
1277 954 int drop = h->sei.picture_timing.timecode[i].dropframe;
1278 954 int hh = h->sei.picture_timing.timecode[i].hours;
1279 954 int mm = h->sei.picture_timing.timecode[i].minutes;
1280 954 int ss = h->sei.picture_timing.timecode[i].seconds;
1281 954 int ff = h->sei.picture_timing.timecode[i].frame;
1282
1283 954 tc_sd[i + 1] = av_timecode_get_smpte(h->avctx->framerate, drop, hh, mm, ss, ff);
1284 954 av_timecode_make_smpte_tc_string2(tcbuf, h->avctx->framerate, tc_sd[i + 1], 0, 0);
1285 954 av_dict_set(&out->metadata, "timecode", tcbuf, 0);
1286 }
1287 }
1288 653 h->sei.picture_timing.timecode_cnt = 0;
1289 }
1290
1291 25639 return 0;
1292 }
1293
1294 25639 static int h264_select_output_frame(H264Context *h)
1295 {
1296 25639 const SPS *sps = h->ps.sps;
1297 25639 H264Picture *out = h->cur_pic_ptr;
1298 25639 H264Picture *cur = h->cur_pic_ptr;
1299 int i, pics, out_of_order, out_idx;
1300
1301 25639 cur->mmco_reset = h->mmco_reset;
1302 25639 h->mmco_reset = 0;
1303
1304
2/2
✓ Branch 0 taken 20029 times.
✓ Branch 1 taken 5610 times.
25639 if (sps->bitstream_restriction_flag ||
1305
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 20029 times.
20029 h->avctx->strict_std_compliance >= FF_COMPLIANCE_STRICT) {
1306 5610 h->avctx->has_b_frames = FFMAX(h->avctx->has_b_frames, sps->num_reorder_frames);
1307 }
1308
1309 420527 for (i = 0; 1; i++) {
1310
4/4
✓ Branch 0 taken 404164 times.
✓ Branch 1 taken 16363 times.
✓ Branch 2 taken 9276 times.
✓ Branch 3 taken 394888 times.
420527 if(i == H264_MAX_DPB_FRAMES || cur->poc < h->last_pocs[i]){
1311
1/2
✓ Branch 0 taken 25639 times.
✗ Branch 1 not taken.
25639 if(i)
1312 25639 h->last_pocs[i-1] = cur->poc;
1313 25639 break;
1314
2/2
✓ Branch 0 taken 369249 times.
✓ Branch 1 taken 25639 times.
394888 } else if(i) {
1315 369249 h->last_pocs[i-1]= h->last_pocs[i];
1316 }
1317 }
1318 25639 out_of_order = H264_MAX_DPB_FRAMES - i;
1319
2/2
✓ Branch 0 taken 17245 times.
✓ Branch 1 taken 8394 times.
25639 if( cur->f->pict_type == AV_PICTURE_TYPE_B
1320
4/4
✓ Branch 0 taken 16039 times.
✓ Branch 1 taken 1206 times.
✓ Branch 2 taken 5232 times.
✓ Branch 3 taken 10807 times.
17245 || (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))
1321 13626 out_of_order = FFMAX(out_of_order, 1);
1322
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 25639 times.
25639 if (out_of_order == H264_MAX_DPB_FRAMES) {
1323 av_log(h->avctx, AV_LOG_VERBOSE, "Invalid POC %d<%d\n", cur->poc, h->last_pocs[0]);
1324 for (i = 1; i < H264_MAX_DPB_FRAMES; i++)
1325 h->last_pocs[i] = INT_MIN;
1326 h->last_pocs[0] = cur->poc;
1327 cur->mmco_reset = 1;
1328
3/4
✓ Branch 0 taken 141 times.
✓ Branch 1 taken 25498 times.
✓ Branch 2 taken 141 times.
✗ Branch 3 not taken.
25639 } else if(h->avctx->has_b_frames < out_of_order && !sps->bitstream_restriction_flag){
1329
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;
1330 141 av_log(h->avctx, loglevel, "Increasing reorder buffer to %d\n", out_of_order);
1331 141 h->avctx->has_b_frames = out_of_order;
1332 }
1333
1334 25639 pics = 0;
1335
2/2
✓ Branch 0 taken 32057 times.
✓ Branch 1 taken 25639 times.
57696 while (h->delayed_pic[pics])
1336 32057 pics++;
1337
1338
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 25639 times.
25639 av_assert0(pics <= H264_MAX_DPB_FRAMES);
1339
1340 25639 h->delayed_pic[pics++] = cur;
1341
2/2
✓ Branch 0 taken 24111 times.
✓ Branch 1 taken 1528 times.
25639 if (cur->reference == 0)
1342 24111 cur->reference = DELAYED_PIC_REF;
1343
1344 25639 out = h->delayed_pic[0];
1345 25639 out_idx = 0;
1346 25639 for (i = 1; h->delayed_pic[i] &&
1347
4/4
✓ Branch 0 taken 29204 times.
✓ Branch 1 taken 25107 times.
✓ Branch 2 taken 28759 times.
✓ Branch 3 taken 445 times.
54311 !(h->delayed_pic[i]->f->flags & AV_FRAME_FLAG_KEY) &&
1348
2/2
✓ Branch 0 taken 28672 times.
✓ Branch 1 taken 87 times.
28759 !h->delayed_pic[i]->mmco_reset;
1349 28672 i++)
1350
2/2
✓ Branch 0 taken 10633 times.
✓ Branch 1 taken 18039 times.
28672 if (h->delayed_pic[i]->poc < out->poc) {
1351 10633 out = h->delayed_pic[i];
1352 10633 out_idx = i;
1353 }
1354
2/2
✓ Branch 0 taken 10381 times.
✓ Branch 1 taken 15258 times.
25639 if (h->avctx->has_b_frames == 0 &&
1355
4/4
✓ Branch 0 taken 9528 times.
✓ Branch 1 taken 853 times.
✓ Branch 2 taken 12 times.
✓ Branch 3 taken 9516 times.
10381 ((h->delayed_pic[0]->f->flags & AV_FRAME_FLAG_KEY) || h->delayed_pic[0]->mmco_reset))
1356 865 h->next_outputed_poc = INT_MIN;
1357 25639 out_of_order = out->poc < h->next_outputed_poc;
1358
1359
4/4
✓ Branch 0 taken 25526 times.
✓ Branch 1 taken 113 times.
✓ Branch 2 taken 24970 times.
✓ Branch 3 taken 556 times.
25639 if (out_of_order || pics > h->avctx->has_b_frames) {
1360 25083 out->reference &= ~DELAYED_PIC_REF;
1361
2/2
✓ Branch 0 taken 41336 times.
✓ Branch 1 taken 25083 times.
66419 for (i = out_idx; h->delayed_pic[i]; i++)
1362 41336 h->delayed_pic[i] = h->delayed_pic[i + 1];
1363 }
1364
4/4
✓ Branch 0 taken 25526 times.
✓ Branch 1 taken 113 times.
✓ Branch 2 taken 24970 times.
✓ Branch 3 taken 556 times.
25639 if (!out_of_order && pics > h->avctx->has_b_frames) {
1365 24970 h->next_output_pic = out;
1366
8/8
✓ Branch 0 taken 15923 times.
✓ Branch 1 taken 9047 times.
✓ Branch 2 taken 5543 times.
✓ Branch 3 taken 10380 times.
✓ Branch 4 taken 5450 times.
✓ Branch 5 taken 93 times.
✓ Branch 6 taken 21 times.
✓ Branch 7 taken 5429 times.
24970 if (out_idx == 0 && h->delayed_pic[0] && ((h->delayed_pic[0]->f->flags & AV_FRAME_FLAG_KEY) || h->delayed_pic[0]->mmco_reset)) {
1367 114 h->next_outputed_poc = INT_MIN;
1368 } else
1369 24856 h->next_outputed_poc = out->poc;
1370
1371 // We have reached an recovery point and all frames after it in
1372 // display order are "recovered".
1373 24970 h->frame_recovered |= out->recovered;
1374
1375 24970 out->recovered |= h->frame_recovered & FRAME_RECOVERED_SEI;
1376
1377
2/2
✓ Branch 0 taken 306 times.
✓ Branch 1 taken 24664 times.
24970 if (!out->recovered) {
1378
1/2
✓ Branch 0 taken 306 times.
✗ Branch 1 not taken.
306 if (!(h->avctx->flags & AV_CODEC_FLAG_OUTPUT_CORRUPT) &&
1379
1/2
✓ Branch 0 taken 306 times.
✗ Branch 1 not taken.
306 !(h->avctx->flags2 & AV_CODEC_FLAG2_SHOW_ALL)) {
1380 306 h->next_output_pic = NULL;
1381 } else {
1382 out->f->flags |= AV_FRAME_FLAG_CORRUPT;
1383 }
1384 }
1385 } else {
1386
2/2
✓ Branch 0 taken 113 times.
✓ Branch 1 taken 556 times.
669 av_log(h->avctx, AV_LOG_DEBUG, "no picture %s\n", out_of_order ? "ooo" : "");
1387 }
1388
1389 25639 return 0;
1390 }
1391
1392 /* This function is called right after decoding the slice header for a first
1393 * slice in a field (or a frame). It decides whether we are decoding a new frame
1394 * or a second field in a pair and does the necessary setup.
1395 */
1396 29063 static int h264_field_start(H264Context *h, const H264SliceContext *sl,
1397 const H2645NAL *nal, int first_slice)
1398 {
1399 int i;
1400 const SPS *sps;
1401
1402 int last_pic_structure, last_pic_droppable, ret;
1403
1404 29063 ret = h264_init_ps(h, sl, first_slice);
1405
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 29063 times.
29063 if (ret < 0)
1406 return ret;
1407
1408 29063 sps = h->ps.sps;
1409
1410
2/2
✓ Branch 0 taken 5995 times.
✓ Branch 1 taken 23068 times.
29063 if (sps->bitstream_restriction_flag &&
1411
2/2
✓ Branch 0 taken 38 times.
✓ Branch 1 taken 5957 times.
5995 h->avctx->has_b_frames < sps->num_reorder_frames) {
1412 38 h->avctx->has_b_frames = sps->num_reorder_frames;
1413 }
1414
1415 29063 last_pic_droppable = h->droppable;
1416 29063 last_pic_structure = h->picture_structure;
1417 29063 h->droppable = (nal->ref_idc == 0);
1418 29063 h->picture_structure = sl->picture_structure;
1419
1420 29063 h->poc.frame_num = sl->frame_num;
1421 29063 h->poc.poc_lsb = sl->poc_lsb;
1422 29063 h->poc.delta_poc_bottom = sl->delta_poc_bottom;
1423 29063 h->poc.delta_poc[0] = sl->delta_poc[0];
1424 29063 h->poc.delta_poc[1] = sl->delta_poc[1];
1425
1426
2/2
✓ Branch 0 taken 1162 times.
✓ Branch 1 taken 27901 times.
29063 if (nal->type == H264_NAL_IDR_SLICE)
1427 1162 h->poc_offset = sl->idr_pic_id;
1428
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 27901 times.
27901 else if (h->picture_intra_only)
1429 h->poc_offset = 0;
1430
1431 /* Shorten frame num gaps so we don't have to allocate reference
1432 * frames just to throw them away */
1433
2/2
✓ Branch 0 taken 16578 times.
✓ Branch 1 taken 12485 times.
29063 if (h->poc.frame_num != h->poc.prev_frame_num) {
1434 16578 int unwrap_prev_frame_num = h->poc.prev_frame_num;
1435 16578 int max_frame_num = 1 << sps->log2_max_frame_num;
1436
1437
2/2
✓ Branch 0 taken 201 times.
✓ Branch 1 taken 16377 times.
16578 if (unwrap_prev_frame_num > h->poc.frame_num)
1438 201 unwrap_prev_frame_num -= max_frame_num;
1439
1440
2/2
✓ Branch 0 taken 76 times.
✓ Branch 1 taken 16502 times.
16578 if ((h->poc.frame_num - unwrap_prev_frame_num) > sps->ref_frame_count) {
1441 76 unwrap_prev_frame_num = (h->poc.frame_num - sps->ref_frame_count) - 1;
1442
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 74 times.
76 if (unwrap_prev_frame_num < 0)
1443 2 unwrap_prev_frame_num += max_frame_num;
1444
1445 76 h->poc.prev_frame_num = unwrap_prev_frame_num;
1446 }
1447 }
1448
1449 /* See if we have a decoded first field looking for a pair...
1450 * Here, we're using that to see if we should mark previously
1451 * decode frames as "finished".
1452 * We have to do that before the "dummy" in-between frame allocation,
1453 * since that can modify h->cur_pic_ptr. */
1454
2/2
✓ Branch 0 taken 3418 times.
✓ Branch 1 taken 25645 times.
29063 if (h->first_field) {
1455 3418 int last_field = last_pic_structure == PICT_BOTTOM_FIELD;
1456
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3418 times.
3418 av_assert0(h->cur_pic_ptr);
1457
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3418 times.
3418 av_assert0(h->cur_pic_ptr->f->buf[0]);
1458 assert(h->cur_pic_ptr->reference != DELAYED_PIC_REF);
1459
1460 /* Mark old field/frame as completed */
1461
1/2
✓ Branch 0 taken 3418 times.
✗ Branch 1 not taken.
3418 if (h->cur_pic_ptr->tf.owner[last_field] == h->avctx) {
1462 3418 ff_thread_report_progress(&h->cur_pic_ptr->tf, INT_MAX, last_field);
1463 }
1464
1465 /* figure out if we have a complementary field pair */
1466
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) {
1467 /* Previous field is unmatched. Don't display it, but let it
1468 * remain for reference if marked as such. */
1469 if (last_pic_structure != PICT_FRAME) {
1470 ff_thread_report_progress(&h->cur_pic_ptr->tf, INT_MAX,
1471 last_pic_structure == PICT_TOP_FIELD);
1472 }
1473 } else {
1474
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3418 times.
3418 if (h->cur_pic_ptr->frame_num != h->poc.frame_num) {
1475 /* This and previous field were reference, but had
1476 * different frame_nums. Consider this field first in
1477 * pair. Throw away previous field except for reference
1478 * purposes. */
1479 if (last_pic_structure != PICT_FRAME) {
1480 ff_thread_report_progress(&h->cur_pic_ptr->tf, INT_MAX,
1481 last_pic_structure == PICT_TOP_FIELD);
1482 }
1483 } else {
1484 /* Second field in complementary pair */
1485
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 &&
1486
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3410 times.
3410 h->picture_structure == PICT_BOTTOM_FIELD) ||
1487 8 (last_pic_structure == PICT_BOTTOM_FIELD &&
1488
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
8 h->picture_structure == PICT_TOP_FIELD))) {
1489 av_log(h->avctx, AV_LOG_ERROR,
1490 "Invalid field mode combination %d/%d\n",
1491 last_pic_structure, h->picture_structure);
1492 h->picture_structure = last_pic_structure;
1493 h->droppable = last_pic_droppable;
1494 return AVERROR_INVALIDDATA;
1495
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3418 times.
3418 } else if (last_pic_droppable != h->droppable) {
1496 avpriv_request_sample(h->avctx,
1497 "Found reference and non-reference fields in the same frame, which");
1498 h->picture_structure = last_pic_structure;
1499 h->droppable = last_pic_droppable;
1500 return AVERROR_PATCHWELCOME;
1501 }
1502 }
1503 }
1504 }
1505
1506
3/4
✓ Branch 0 taken 17267 times.
✓ Branch 1 taken 12485 times.
✓ Branch 2 taken 17267 times.
✗ Branch 3 not taken.
58815 while (h->poc.frame_num != h->poc.prev_frame_num && !h->first_field &&
1507
2/2
✓ Branch 0 taken 689 times.
✓ Branch 1 taken 16578 times.
17267 h->poc.frame_num != (h->poc.prev_frame_num + 1) % (1 << sps->log2_max_frame_num)) {
1508
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;
1509 689 av_log(h->avctx, AV_LOG_DEBUG, "Frame num gap %d %d\n",
1510 h->poc.frame_num, h->poc.prev_frame_num);
1511
2/2
✓ Branch 0 taken 112 times.
✓ Branch 1 taken 577 times.
689 if (!sps->gaps_in_frame_num_allowed_flag)
1512
2/2
✓ Branch 0 taken 1792 times.
✓ Branch 1 taken 112 times.
1904 for(i=0; i<FF_ARRAY_ELEMS(h->last_pocs); i++)
1513 1792 h->last_pocs[i] = INT_MIN;
1514 689 ret = h264_frame_start(h);
1515
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 689 times.
689 if (ret < 0) {
1516 h->first_field = 0;
1517 return ret;
1518 }
1519
1520 689 h->poc.prev_frame_num++;
1521 689 h->poc.prev_frame_num %= 1 << sps->log2_max_frame_num;
1522 689 h->cur_pic_ptr->frame_num = h->poc.prev_frame_num;
1523 689 h->cur_pic_ptr->invalid_gap = !sps->gaps_in_frame_num_allowed_flag;
1524 689 ff_thread_report_progress(&h->cur_pic_ptr->tf, INT_MAX, 0);
1525 689 ff_thread_report_progress(&h->cur_pic_ptr->tf, INT_MAX, 1);
1526
1527 689 h->explicit_ref_marking = 0;
1528 689 ret = ff_h264_execute_ref_pic_marking(h);
1529
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))
1530 return ret;
1531 /* Error concealment: If a ref is missing, copy the previous ref
1532 * in its place.
1533 * FIXME: Avoiding a memcpy would be nice, but ref handling makes
1534 * many assumptions about there being no actual duplicates.
1535 * FIXME: This does not copy padding for out-of-frame motion
1536 * vectors. Given we are concealing a lost frame, this probably
1537 * is not noticeable by comparison, but it should be fixed. */
1538
1/2
✓ Branch 0 taken 689 times.
✗ Branch 1 not taken.
689 if (h->short_ref_count) {
1539 689 int c[4] = {
1540 689 1<<(h->ps.sps->bit_depth_luma-1),
1541 689 1<<(h->ps.sps->bit_depth_chroma-1),
1542 689 1<<(h->ps.sps->bit_depth_chroma-1),
1543 -1
1544 };
1545
1546
2/2
✓ Branch 0 taken 655 times.
✓ Branch 1 taken 34 times.
689 if (prev &&
1547
1/2
✓ Branch 0 taken 655 times.
✗ Branch 1 not taken.
655 h->short_ref[0]->f->width == prev->f->width &&
1548
1/2
✓ Branch 0 taken 655 times.
✗ Branch 1 not taken.
655 h->short_ref[0]->f->height == prev->f->height &&
1549
1/2
✓ Branch 0 taken 655 times.
✗ Branch 1 not taken.
655 h->short_ref[0]->f->format == prev->f->format) {
1550 655 ff_thread_await_progress(&prev->tf, INT_MAX, 0);
1551
2/2
✓ Branch 0 taken 28 times.
✓ Branch 1 taken 627 times.
655 if (prev->field_picture)
1552 28 ff_thread_await_progress(&prev->tf, INT_MAX, 1);
1553 655 ff_thread_release_ext_buffer(&h->short_ref[0]->tf);
1554 655 h->short_ref[0]->tf.f = h->short_ref[0]->f;
1555 655 ret = ff_thread_ref_frame(&h->short_ref[0]->tf, &prev->tf);
1556
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 655 times.
655 if (ret < 0)
1557 return ret;
1558 655 h->short_ref[0]->poc = prev->poc + 2U;
1559 655 h->short_ref[0]->gray = prev->gray;
1560 655 ff_thread_report_progress(&h->short_ref[0]->tf, INT_MAX, 0);
1561
2/2
✓ Branch 0 taken 28 times.
✓ Branch 1 taken 627 times.
655 if (h->short_ref[0]->field_picture)
1562 28 ff_thread_report_progress(&h->short_ref[0]->tf, INT_MAX, 1);
1563
2/2
✓ Branch 0 taken 26 times.
✓ Branch 1 taken 8 times.
34 } else if (!h->frame_recovered) {
1564
1/2
✓ Branch 0 taken 26 times.
✗ Branch 1 not taken.
26 if (!h->avctx->hwaccel)
1565 26 color_frame(h->short_ref[0]->f, c);
1566 26 h->short_ref[0]->gray = 1;
1567 }
1568 689 h->short_ref[0]->frame_num = h->poc.prev_frame_num;
1569 }
1570 }
1571
1572 /* See if we have a decoded first field looking for a pair...
1573 * We're using that to see whether to continue decoding in that
1574 * frame, or to allocate a new one. */
1575
2/2
✓ Branch 0 taken 3418 times.
✓ Branch 1 taken 25645 times.
29063 if (h->first_field) {
1576
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3418 times.
3418 av_assert0(h->cur_pic_ptr);
1577
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3418 times.
3418 av_assert0(h->cur_pic_ptr->f->buf[0]);
1578 assert(h->cur_pic_ptr->reference != DELAYED_PIC_REF);
1579
1580 /* figure out if we have a complementary field pair */
1581
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) {
1582 /* Previous field is unmatched. Don't display it, but let it
1583 * remain for reference if marked as such. */
1584 h->missing_fields ++;
1585 h->cur_pic_ptr = NULL;
1586 h->first_field = FIELD_PICTURE(h);
1587 } else {
1588 3418 h->missing_fields = 0;
1589
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3418 times.
3418 if (h->cur_pic_ptr->frame_num != h->poc.frame_num) {
1590 ff_thread_report_progress(&h->cur_pic_ptr->tf, INT_MAX,
1591 h->picture_structure==PICT_BOTTOM_FIELD);
1592 /* This and the previous field had different frame_nums.
1593 * Consider this field first in pair. Throw away previous
1594 * one except for reference purposes. */
1595 h->first_field = 1;
1596 h->cur_pic_ptr = NULL;
1597
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3418 times.
3418 } else if (h->cur_pic_ptr->reference & DELAYED_PIC_REF) {
1598 /* This frame was already output, we cannot draw into it
1599 * anymore.
1600 */
1601 h->first_field = 1;
1602 h->cur_pic_ptr = NULL;
1603 } else {
1604 /* Second field in complementary pair */
1605 3418 h->first_field = 0;
1606 }
1607 }
1608 } else {
1609 /* Frame or first field in a potentially complementary pair */
1610 25645 h->first_field = FIELD_PICTURE(h);
1611 }
1612
1613
4/4
✓ Branch 0 taken 6842 times.
✓ Branch 1 taken 22221 times.
✓ Branch 2 taken 3424 times.
✓ Branch 3 taken 3418 times.
29063 if (!FIELD_PICTURE(h) || h->first_field) {
1614
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 25645 times.
25645 if (h264_frame_start(h) < 0) {
1615 h->first_field = 0;
1616 return AVERROR_INVALIDDATA;
1617 }
1618 } else {
1619 3418 int field = h->picture_structure == PICT_BOTTOM_FIELD;
1620 3418 release_unused_pictures(h, 0);
1621 3418 h->cur_pic_ptr->tf.owner[field] = h->avctx;
1622 }
1623 /* Some macroblocks can be accessed before they're available in case
1624 * of lost slices, MBAFF or threading. */
1625
2/2
✓ Branch 0 taken 6842 times.
✓ Branch 1 taken 22221 times.
29063 if (FIELD_PICTURE(h)) {
1626
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++)
1627 170420 memset(h->slice_table + i*h->mb_stride, -1, (h->mb_stride - (i+1==h->mb_height)) * sizeof(*h->slice_table));
1628 } else {
1629 22221 memset(h->slice_table, -1,
1630 22221 (h->mb_height * h->mb_stride - 1) * sizeof(*h->slice_table));
1631 }
1632
1633 29063 ret = ff_h264_init_poc(h->cur_pic_ptr->field_poc, &h->cur_pic_ptr->poc,
1634 29063 h->ps.sps, &h->poc, h->picture_structure, nal->ref_idc);
1635
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 29063 times.
29063 if (ret < 0)
1636 return ret;
1637
1638 29063 memcpy(h->mmco, sl->mmco, sl->nb_mmco * sizeof(*h->mmco));
1639 29063 h->nb_mmco = sl->nb_mmco;
1640 29063 h->explicit_ref_marking = sl->explicit_ref_marking;
1641
1642 29063 h->picture_idr = nal->type == H264_NAL_IDR_SLICE;
1643
1644
2/2
✓ Branch 0 taken 98 times.
✓ Branch 1 taken 28965 times.
29063 if (h->sei.recovery_point.recovery_frame_cnt >= 0) {
1645 98 const int sei_recovery_frame_cnt = h->sei.recovery_point.recovery_frame_cnt;
1646
1647
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)
1648 84 h->valid_recovery_point = 1;
1649
1650
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 93 times.
98 if ( h->recovery_frame < 0
1651
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) {
1652 93 h->recovery_frame = av_zero_extend(h->poc.frame_num + sei_recovery_frame_cnt, h->ps.sps->log2_max_frame_num);
1653
1654
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 87 times.
93 if (!h->valid_recovery_point)
1655 6 h->recovery_frame = h->poc.frame_num;
1656 }
1657 }
1658
1659
2/2
✓ Branch 0 taken 1162 times.
✓ Branch 1 taken 27901 times.
29063 h->cur_pic_ptr->f->flags |= AV_FRAME_FLAG_KEY * !!(nal->type == H264_NAL_IDR_SLICE);
1660
1661
2/2
✓ Branch 0 taken 1162 times.
✓ Branch 1 taken 27901 times.
29063 if (nal->type == H264_NAL_IDR_SLICE) {
1662 1162 h->cur_pic_ptr->recovered |= FRAME_RECOVERED_IDR;
1663 // If we have an IDR, all frames after it in decoded order are
1664 // "recovered".
1665 1162 h->frame_recovered |= FRAME_RECOVERED_IDR;
1666 }
1667
1668
4/4
✓ Branch 0 taken 110 times.
✓ Branch 1 taken 28953 times.
✓ Branch 2 taken 92 times.
✓ Branch 3 taken 18 times.
29063 if (h->recovery_frame == h->poc.frame_num && nal->ref_idc) {
1669 92 h->recovery_frame = -1;
1670 92 h->cur_pic_ptr->recovered |= FRAME_RECOVERED_SEI;
1671 }
1672
1673 #if 1
1674 29063 h->cur_pic_ptr->recovered |= h->frame_recovered;
1675 #else
1676 h->cur_pic_ptr->recovered |= !!(h->frame_recovered & FRAME_RECOVERED_IDR);
1677 #endif
1678
1679 /* Set the frame properties/side data. Only done for the second field in
1680 * field coded frames, since some SEI information is present for each field
1681 * and is merged by the SEI parsing code. */
1682
5/6
✓ Branch 0 taken 6842 times.
✓ Branch 1 taken 22221 times.
✓ Branch 2 taken 3424 times.
✓ Branch 3 taken 3418 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 3424 times.
29063 if (!FIELD_PICTURE(h) || !h->first_field || h->missing_fields > 1) {
1683 25639 ret = h264_export_frame_props(h);
1684
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 25639 times.
25639 if (ret < 0)
1685 return ret;
1686
1687 25639 ret = h264_select_output_frame(h);
1688
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 25639 times.
25639 if (ret < 0)
1689 return ret;
1690 }
1691
1692 29063 return 0;
1693 }
1694
1695 38484 static int h264_slice_header_parse(const H264Context *h, H264SliceContext *sl,
1696 const H2645NAL *nal)
1697 {
1698 const SPS *sps;
1699 const PPS *pps;
1700 int ret;
1701 unsigned int slice_type, tmp, i;
1702 int field_pic_flag, bottom_field_flag;
1703
4/4
✓ Branch 0 taken 38034 times.
✓ Branch 1 taken 450 times.
✓ Branch 2 taken 29111 times.
✓ Branch 3 taken 8923 times.
38484 int first_slice = sl == h->slice_ctx && !h->current_slice;
1704 int picture_structure;
1705
1706
2/2
✓ Branch 0 taken 29111 times.
✓ Branch 1 taken 9373 times.
38484 if (first_slice)
1707
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 29111 times.
29111 av_assert0(!h->setup_finished);
1708
1709 38484 sl->first_mb_addr = get_ue_golomb_long(&sl->gb);
1710
1711 38484 slice_type = get_ue_golomb_31(&sl->gb);
1712
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 38484 times.
38484 if (slice_type > 9) {
1713 av_log(h->avctx, AV_LOG_ERROR,
1714 "slice type %d too large at %d\n",
1715 slice_type, sl->first_mb_addr);
1716 return AVERROR_INVALIDDATA;
1717 }
1718
2/2
✓ Branch 0 taken 24650 times.
✓ Branch 1 taken 13834 times.
38484 if (slice_type > 4) {
1719 24650 slice_type -= 5;
1720 24650 sl->slice_type_fixed = 1;
1721 } else
1722 13834 sl->slice_type_fixed = 0;
1723
1724 38484 slice_type = ff_h264_golomb_to_pict_type[slice_type];
1725 38484 sl->slice_type = slice_type;
1726 38484 sl->slice_type_nos = slice_type & 3;
1727
1728
2/2
✓ Branch 0 taken 2350 times.
✓ Branch 1 taken 36134 times.
38484 if (nal->type == H264_NAL_IDR_SLICE &&
1729
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2350 times.
2350 sl->slice_type_nos != AV_PICTURE_TYPE_I) {
1730 av_log(h->avctx, AV_LOG_ERROR, "A non-intra slice in an IDR NAL unit.\n");
1731 return AVERROR_INVALIDDATA;
1732 }
1733
1734 38484 sl->pps_id = get_ue_golomb(&sl->gb);
1735
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 38484 times.
38484 if (sl->pps_id >= MAX_PPS_COUNT) {
1736 av_log(h->avctx, AV_LOG_ERROR, "pps_id %u out of range\n", sl->pps_id);
1737 return AVERROR_INVALIDDATA;
1738 }
1739
2/2
✓ Branch 0 taken 167 times.
✓ Branch 1 taken 38317 times.
38484 if (!h->ps.pps_list[sl->pps_id]) {
1740 167 av_log(h->avctx, AV_LOG_ERROR,
1741 "non-existing PPS %u referenced\n",
1742 sl->pps_id);
1743 167 return AVERROR_INVALIDDATA;
1744 }
1745 38317 pps = h->ps.pps_list[sl->pps_id];
1746 38317 sps = pps->sps;
1747
1748 38317 sl->frame_num = get_bits(&sl->gb, sps->log2_max_frame_num);
1749
2/2
✓ Branch 0 taken 9369 times.
✓ Branch 1 taken 28948 times.
38317 if (!first_slice) {
1750
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9369 times.
9369 if (h->poc.frame_num != sl->frame_num) {
1751 av_log(h->avctx, AV_LOG_ERROR, "Frame num change from %d to %d\n",
1752 h->poc.frame_num, sl->frame_num);
1753 return AVERROR_INVALIDDATA;
1754 }
1755 }
1756
1757 38317 sl->mb_mbaff = 0;
1758
1759
2/2
✓ Branch 0 taken 23604 times.
✓ Branch 1 taken 14713 times.
38317 if (sps->frame_mbs_only_flag) {
1760 23604 picture_structure = PICT_FRAME;
1761 } else {
1762
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) {
1763 av_log(h->avctx, AV_LOG_ERROR, "This stream was generated by a broken encoder, invalid 8x8 inference\n");
1764 return -1;
1765 }
1766 14713 field_pic_flag = get_bits1(&sl->gb);
1767
2/2
✓ Branch 0 taken 9240 times.
✓ Branch 1 taken 5473 times.
14713 if (field_pic_flag) {
1768 9240 bottom_field_flag = get_bits1(&sl->gb);
1769 9240 picture_structure = PICT_TOP_FIELD + bottom_field_flag;
1770 } else {
1771 5473 picture_structure = PICT_FRAME;
1772 }
1773 }
1774 38317 sl->picture_structure = picture_structure;
1775 38317 sl->mb_field_decoding_flag = picture_structure != PICT_FRAME;
1776
1777
2/2
✓ Branch 0 taken 29077 times.
✓ Branch 1 taken 9240 times.
38317 if (picture_structure == PICT_FRAME) {
1778 29077 sl->curr_pic_num = sl->frame_num;
1779 29077 sl->max_pic_num = 1 << sps->log2_max_frame_num;
1780 } else {
1781 9240 sl->curr_pic_num = 2 * sl->frame_num + 1;
1782 9240 sl->max_pic_num = 1 << (sps->log2_max_frame_num + 1);
1783 }
1784
1785
2/2
✓ Branch 0 taken 2345 times.
✓ Branch 1 taken 35972 times.
38317 if (nal->type == H264_NAL_IDR_SLICE) {
1786 2345 unsigned idr_pic_id = get_ue_golomb_long(&sl->gb);
1787
1/2
✓ Branch 0 taken 2345 times.
✗ Branch 1 not taken.
2345 if (idr_pic_id < 65536) {
1788 2345 sl->idr_pic_id = idr_pic_id;
1789 } else
1790 av_log(h->avctx, AV_LOG_WARNING, "idr_pic_id is invalid\n");
1791 }
1792
1793 38317 sl->poc_lsb = 0;
1794 38317 sl->delta_poc_bottom = 0;
1795
2/2
✓ Branch 0 taken 30416 times.
✓ Branch 1 taken 7901 times.
38317 if (sps->poc_type == 0) {
1796 30416 sl->poc_lsb = get_bits(&sl->gb, sps->log2_max_poc_lsb);
1797
1798
4/4
✓ Branch 0 taken 8524 times.
✓ Branch 1 taken 21892 times.
✓ Branch 2 taken 6632 times.
✓ Branch 3 taken 1892 times.
30416 if (pps->pic_order_present == 1 && picture_structure == PICT_FRAME)
1799 6632 sl->delta_poc_bottom = get_se_golomb(&sl->gb);
1800 }
1801
1802 38317 sl->delta_poc[0] = sl->delta_poc[1] = 0;
1803
4/4
✓ Branch 0 taken 3055 times.
✓ Branch 1 taken 35262 times.
✓ Branch 2 taken 2848 times.
✓ Branch 3 taken 207 times.
38317 if (sps->poc_type == 1 && !sps->delta_pic_order_always_zero_flag) {
1804 2848 sl->delta_poc[0] = get_se_golomb(&sl->gb);
1805
1806
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)
1807 453 sl->delta_poc[1] = get_se_golomb(&sl->gb);
1808 }
1809
1810 38317 sl->redundant_pic_count = 0;
1811
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 38317 times.
38317 if (pps->redundant_pic_cnt_present)
1812 sl->redundant_pic_count = get_ue_golomb(&sl->gb);
1813
1814
2/2
✓ Branch 0 taken 13558 times.
✓ Branch 1 taken 24759 times.
38317 if (sl->slice_type_nos == AV_PICTURE_TYPE_B)
1815 13558 sl->direct_spatial_mv_pred = get_bits1(&sl->gb);
1816
1817 38317 ret = ff_h264_parse_ref_count(&sl->list_count, sl->ref_count,
1818 &sl->gb, pps, sl->slice_type_nos,
1819 38317 picture_structure, h->avctx);
1820
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 38317 times.
38317 if (ret < 0)
1821 return ret;
1822
1823
2/2
✓ Branch 0 taken 33764 times.
✓ Branch 1 taken 4553 times.
38317 if (sl->slice_type_nos != AV_PICTURE_TYPE_I) {
1824 33764 ret = ff_h264_decode_ref_pic_list_reordering(sl, h->avctx);
1825
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 33764 times.
33764 if (ret < 0) {
1826 sl->ref_count[1] = sl->ref_count[0] = 0;
1827 return ret;
1828 }
1829 }
1830
1831 38317 sl->pwt.use_weight = 0;
1832
2/2
✓ Branch 0 taken 76634 times.
✓ Branch 1 taken 38317 times.
114951 for (i = 0; i < 2; i++) {
1833 76634 sl->pwt.luma_weight_flag[i] = 0;
1834 76634 sl->pwt.chroma_weight_flag[i] = 0;
1835 }
1836
4/4
✓ Branch 0 taken 4850 times.
✓ Branch 1 taken 33467 times.
✓ Branch 2 taken 1877 times.
✓ Branch 3 taken 2973 times.
38317 if ((pps->weighted_pred && sl->slice_type_nos == AV_PICTURE_TYPE_P) ||
1837
2/2
✓ Branch 0 taken 390 times.
✓ Branch 1 taken 34954 times.
35344 (pps->weighted_bipred_idc == 1 &&
1838
2/2
✓ Branch 0 taken 386 times.
✓ Branch 1 taken 4 times.
390 sl->slice_type_nos == AV_PICTURE_TYPE_B)) {
1839 3359 ret = ff_h264_pred_weight_table(&sl->gb, sps, sl->ref_count,
1840 sl->slice_type_nos, &sl->pwt,
1841 3359 picture_structure, h->avctx);
1842
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3359 times.
3359 if (ret < 0)
1843 return ret;
1844 }
1845
1846 38317 sl->explicit_ref_marking = 0;
1847
2/2
✓ Branch 0 taken 25618 times.
✓ Branch 1 taken 12699 times.
38317 if (nal->ref_idc) {
1848 25618 ret = ff_h264_decode_ref_pic_marking(sl, &sl->gb, nal, h->avctx);
1849
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 25618 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
25618 if (ret < 0 && (h->avctx->err_recognition & AV_EF_EXPLODE))
1850 return AVERROR_INVALIDDATA;
1851 }
1852
1853
4/4
✓ Branch 0 taken 33764 times.
✓ Branch 1 taken 4553 times.
✓ Branch 2 taken 18944 times.
✓ Branch 3 taken 14820 times.
38317 if (sl->slice_type_nos != AV_PICTURE_TYPE_I && pps->cabac) {
1854 18944 tmp = get_ue_golomb_31(&sl->gb);
1855
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 18944 times.
18944 if (tmp > 2) {
1856 av_log(h->avctx, AV_LOG_ERROR, "cabac_init_idc %u overflow\n", tmp);
1857 return AVERROR_INVALIDDATA;
1858 }
1859 18944 sl->cabac_init_idc = tmp;
1860 }
1861
1862 38317 sl->last_qscale_diff = 0;
1863 38317 tmp = pps->init_qp + (unsigned)get_se_golomb(&sl->gb);
1864
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 38317 times.
38317 if (tmp > 51 + 6 * (sps->bit_depth_luma - 8)) {
1865 av_log(h->avctx, AV_LOG_ERROR, "QP %u out of range\n", tmp);
1866 return AVERROR_INVALIDDATA;
1867 }
1868 38317 sl->qscale = tmp;
1869 38317 sl->chroma_qp[0] = get_chroma_qp(pps, 0, sl->qscale);
1870 38317 sl->chroma_qp[1] = get_chroma_qp(pps, 1, sl->qscale);
1871 // FIXME qscale / qp ... stuff
1872
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 38317 times.
38317 if (sl->slice_type == AV_PICTURE_TYPE_SP)
1873 get_bits1(&sl->gb); /* sp_for_switch_flag */
1874
1/2
✓ Branch 0 taken 38317 times.
✗ Branch 1 not taken.
38317 if (sl->slice_type == AV_PICTURE_TYPE_SP ||
1875
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 38317 times.
38317 sl->slice_type == AV_PICTURE_TYPE_SI)
1876 get_se_golomb(&sl->gb); /* slice_qs_delta */
1877
1878 38317 sl->deblocking_filter = 1;
1879 38317 sl->slice_alpha_c0_offset = 0;
1880 38317 sl->slice_beta_offset = 0;
1881
2/2
✓ Branch 0 taken 23520 times.
✓ Branch 1 taken 14797 times.
38317 if (pps->deblocking_filter_parameters_present) {
1882 23520 tmp = get_ue_golomb_31(&sl->gb);
1883
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 23520 times.
23520 if (tmp > 2) {
1884 av_log(h->avctx, AV_LOG_ERROR,
1885 "deblocking_filter_idc %u out of range\n", tmp);
1886 return AVERROR_INVALIDDATA;
1887 }
1888 23520 sl->deblocking_filter = tmp;
1889
2/2
✓ Branch 0 taken 20817 times.
✓ Branch 1 taken 2703 times.
23520 if (sl->deblocking_filter < 2)
1890 20817 sl->deblocking_filter ^= 1; // 1<->0
1891
1892
2/2
✓ Branch 0 taken 15799 times.
✓ Branch 1 taken 7721 times.
23520 if (sl->deblocking_filter) {
1893 15799 int slice_alpha_c0_offset_div2 = get_se_golomb(&sl->gb);
1894 15799 int slice_beta_offset_div2 = get_se_golomb(&sl->gb);
1895
2/4
✓ Branch 0 taken 15799 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 15799 times.
✗ Branch 3 not taken.
15799 if (slice_alpha_c0_offset_div2 > 6 ||
1896
1/2
✓ Branch 0 taken 15799 times.
✗ Branch 1 not taken.
15799 slice_alpha_c0_offset_div2 < -6 ||
1897
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 15799 times.
15799 slice_beta_offset_div2 > 6 ||
1898 slice_beta_offset_div2 < -6) {
1899 av_log(h->avctx, AV_LOG_ERROR,
1900 "deblocking filter parameters %d %d out of range\n",
1901 slice_alpha_c0_offset_div2, slice_beta_offset_div2);
1902 return AVERROR_INVALIDDATA;
1903 }
1904 15799 sl->slice_alpha_c0_offset = slice_alpha_c0_offset_div2 * 2;
1905 15799 sl->slice_beta_offset = slice_beta_offset_div2 * 2;
1906 }
1907 }
1908
1909 38317 return 0;
1910 }
1911
1912 /* do all the per-slice initialization needed before we can start decoding the
1913 * actual MBs */
1914 38237 static int h264_slice_init(H264Context *h, H264SliceContext *sl,
1915 const H2645NAL *nal)
1916 {
1917 38237 int i, j, ret = 0;
1918
1919
3/4
✓ Branch 0 taken 2345 times.
✓ Branch 1 taken 35892 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 2345 times.
38237 if (h->picture_idr && nal->type != H264_NAL_IDR_SLICE) {
1920 av_log(h->avctx, AV_LOG_ERROR, "Invalid mix of IDR and non-IDR slices\n");
1921 return AVERROR_INVALIDDATA;
1922 }
1923
1924 av_assert1(h->mb_num == h->mb_width * h->mb_height);
1925
5/6
✓ Branch 0 taken 33261 times.
✓ Branch 1 taken 4976 times.
✓ Branch 2 taken 9160 times.
✓ Branch 3 taken 24101 times.
✓ Branch 4 taken 38237 times.
✗ Branch 5 not taken.
38237 if (sl->first_mb_addr << FIELD_OR_MBAFF_PICTURE(h) >= h->mb_num ||
1926
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 38237 times.
38237 sl->first_mb_addr >= h->mb_num) {
1927 av_log(h->avctx, AV_LOG_ERROR, "first_mb_in_slice overflow\n");
1928 return AVERROR_INVALIDDATA;
1929 }
1930 38237 sl->resync_mb_x = sl->mb_x = sl->first_mb_addr % h->mb_width;
1931 76474 sl->resync_mb_y = sl->mb_y = (sl->first_mb_addr / h->mb_width) <<
1932
4/4
✓ Branch 0 taken 33261 times.
✓ Branch 1 taken 4976 times.
✓ Branch 2 taken 9160 times.
✓ Branch 3 taken 24101 times.
38237 FIELD_OR_MBAFF_PICTURE(h);
1933
2/2
✓ Branch 0 taken 4576 times.
✓ Branch 1 taken 33661 times.
38237 if (h->picture_structure == PICT_BOTTOM_FIELD)
1934 4576 sl->resync_mb_y = sl->mb_y = sl->mb_y + 1;
1935 av_assert1(sl->mb_y < h->mb_height);
1936
1937 38237 ret = ff_h264_build_ref_list(h, sl);
1938
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 38237 times.
38237 if (ret < 0)
1939 return ret;
1940
1941
2/2
✓ Branch 0 taken 4348 times.
✓ Branch 1 taken 33889 times.
38237 if (h->ps.pps->weighted_bipred_idc == 2 &&
1942
2/2
✓ Branch 0 taken 2349 times.
✓ Branch 1 taken 1999 times.
4348 sl->slice_type_nos == AV_PICTURE_TYPE_B) {
1943 2349 implicit_weight_table(h, sl, -1);
1944
2/2
✓ Branch 0 taken 856 times.
✓ Branch 1 taken 1493 times.
2349 if (FRAME_MBAFF(h)) {
1945 856 implicit_weight_table(h, sl, 0);
1946 856 implicit_weight_table(h, sl, 1);
1947 }
1948 }
1949
1950
4/4
✓ Branch 0 taken 13514 times.
✓ Branch 1 taken 24723 times.
✓ Branch 2 taken 7710 times.
✓ Branch 3 taken 5804 times.
38237 if (sl->slice_type_nos == AV_PICTURE_TYPE_B && !sl->direct_spatial_mv_pred)
1951 7710 ff_h264_direct_dist_scale_factor(h, sl);
1952
1/2
✓ Branch 0 taken 38237 times.
✗ Branch 1 not taken.
38237 if (!h->setup_finished)
1953 38237 ff_h264_direct_ref_list_init(h, sl);
1954
1955
1/2
✓ Branch 0 taken 38237 times.
✗ Branch 1 not taken.
38237 if (h->avctx->skip_loop_filter >= AVDISCARD_ALL ||
1956
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 38237 times.
38237 (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 38237 times.
38237 (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 38237 times.
38237 (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 38237 times.
38237 (h->avctx->skip_loop_filter >= AVDISCARD_NONREF &&
1963 nal->ref_idc == 0))
1964 sl->deblocking_filter = 0;
1965
1966
3/4
✓ Branch 0 taken 27813 times.
✓ Branch 1 taken 10424 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 27813 times.
38237 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 38237 sl->qp_thresh = 15 -
1976 38237 FFMIN(sl->slice_alpha_c0_offset, sl->slice_beta_offset) -
1977 38237 FFMAX3(0,
1978 h->ps.pps->chroma_qp_index_offset[0],
1979 38237 h->ps.pps->chroma_qp_index_offset[1]) +
1980 38237 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 38237 times.
38237 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 38237 sl->slice_num = ++h->current_slice;
1989
1990
1/2
✓ Branch 0 taken 38237 times.
✗ Branch 1 not taken.
38237 if (sl->slice_num)
1991 38237 h->slice_row[(sl->slice_num-1)&(MAX_SLICES-1)]= sl->resync_mb_y;
1992
2/2
✓ Branch 0 taken 35264 times.
✓ Branch 1 taken 2973 times.
38237 if ( h->slice_row[sl->slice_num&(MAX_SLICES-1)] + 3 >= sl->resync_mb_y
1993
2/2
✓ Branch 0 taken 26426 times.
✓ Branch 1 taken 8838 times.
35264 && h->slice_row[sl->slice_num&(MAX_SLICES-1)] <= sl->resync_mb_y
1994
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 26426 times.
26426 && 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 76474 times.
✓ Branch 1 taken 38237 times.
114711 for (j = 0; j < 2; j++) {
2000 int id_list[16];
2001 76474 int *ref2frm = h->ref2frm[sl->slice_num & (MAX_SLICES - 1)][j];
2002
2/2
✓ Branch 0 taken 1223584 times.
✓ Branch 1 taken 76474 times.
1300058 for (i = 0; i < 16; i++) {
2003 1223584 id_list[i] = 60;
2004
4/4
✓ Branch 0 taken 755168 times.
✓ Branch 1 taken 468416 times.
✓ Branch 2 taken 142288 times.
✓ Branch 3 taken 612880 times.
1223584 if (j < sl->list_count && i < sl->ref_count[j] &&
2005
1/2
✓ Branch 0 taken 142288 times.
✗ Branch 1 not taken.
142288 sl->ref_list[j][i].parent->f->buf[0]) {
2006 int k;
2007 142288 const AVBuffer *buf = sl->ref_list[j][i].parent->f->buf[0]->buffer;
2008
2/2
✓ Branch 0 taken 472688 times.
✓ Branch 1 taken 1933 times.
474621 for (k = 0; k < h->short_ref_count; k++)
2009
2/2
✓ Branch 0 taken 140355 times.
✓ Branch 1 taken 332333 times.
472688 if (h->short_ref[k]->f->buf[0]->buffer == buf) {
2010 140355 id_list[i] = k;
2011 140355 break;
2012 }
2013
2/2
✓ Branch 0 taken 11870 times.
✓ Branch 1 taken 140496 times.
152366 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 76474 ref2frm[0] =
2022 76474 ref2frm[1] = -1;
2023
2/2
✓ Branch 0 taken 1223584 times.
✓ Branch 1 taken 76474 times.
1300058 for (i = 0; i < 16; i++)
2024 1223584 ref2frm[i + 2] = 4 * id_list[i] + (sl->ref_list[j][i].reference & 3);
2025 76474 ref2frm[18 + 0] =
2026 76474 ref2frm[18 + 1] = -1;
2027
2/2
✓ Branch 0 taken 2447168 times.
✓ Branch 1 taken 76474 times.
2523642 for (i = 16; i < 48; i++)
2028 2447168 ref2frm[i + 4] = 4 * id_list[(i - 16) >> 1] +
2029 2447168 (sl->ref_list[j][i].reference & 3);
2030 }
2031
2032
2/2
✓ Branch 0 taken 4553 times.
✓ Branch 1 taken 33684 times.
38237 if (sl->slice_type_nos == AV_PICTURE_TYPE_I) {
2033 4553 h->cur_pic_ptr->gray = 0;
2034 4553 h->non_gray = 1;
2035 } else {
2036 33684 int gray = 0;
2037
2/2
✓ Branch 0 taken 47198 times.
✓ Branch 1 taken 33684 times.
80882 for (j = 0; j < sl->list_count; j++) {
2038
2/2
✓ Branch 0 taken 142288 times.
✓ Branch 1 taken 47198 times.
189486 for (i = 0; i < sl->ref_count[j]; i++) {
2039 142288 gray |= sl->ref_list[j][i].parent->gray;
2040 }
2041 }
2042 33684 h->cur_pic_ptr->gray = gray;
2043 }
2044
2045
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 38237 times.
38237 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 38237 return 0;
2067 }
2068
2069 38484 int ff_h264_queue_decode_slice(H264Context *h, const H2645NAL *nal)
2070 {
2071 38484 H264SliceContext *sl = h->slice_ctx + h->nb_slice_ctx_queued;
2072
4/4
✓ Branch 0 taken 38034 times.
✓ Branch 1 taken 450 times.
✓ Branch 2 taken 29111 times.
✓ Branch 3 taken 8923 times.
38484 int first_slice = sl == h->slice_ctx && !h->current_slice;
2073 int ret;
2074
2075 38484 sl->gb = nal->gb;
2076
2077 38484 ret = h264_slice_header_parse(h, sl, nal);
2078
2/2
✓ Branch 0 taken 167 times.
✓ Branch 1 taken 38317 times.
38484 if (ret < 0)
2079 167 return ret;
2080
2081 // discard redundant pictures
2082
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 38317 times.
38317 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 29143 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 9174 times.
38317 if (sl->first_mb_addr == 0 || !h->current_slice) {
2088
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 29143 times.
29143 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 29143 times.
✓ Branch 1 taken 9174 times.
38317 if (sl->first_mb_addr == 0) { // FIXME better field boundary detection
2095
2/2
✓ Branch 0 taken 195 times.
✓ Branch 1 taken 28948 times.
29143 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 25725 times.
✓ Branch 1 taken 3418 times.
29143 if (!h->first_field) {
2128
3/4
✓ Branch 0 taken 299 times.
✓ Branch 1 taken 25426 times.
✓ Branch 2 taken 299 times.
✗ Branch 3 not taken.
25725 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 25725 h->cur_pic_ptr = NULL;
2133 }
2134 }
2135
2136
2/2
✓ Branch 0 taken 29143 times.
✓ Branch 1 taken 9174 times.
38317 if (!h->current_slice)
2137
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 29143 times.
29143 av_assert0(sl == h->slice_ctx);
2138
2139
4/4
✓ Branch 0 taken 29143 times.
✓ Branch 1 taken 9174 times.
✓ Branch 2 taken 25725 times.
✓ Branch 3 taken 3418 times.
38317 if (h->current_slice == 0 && !h->first_field) {
2140 25725 if (
2141
3/4
✓ Branch 0 taken 84 times.
✓ Branch 1 taken 25641 times.
✓ Branch 2 taken 84 times.
✗ Branch 3 not taken.
25725 (h->avctx->skip_frame >= AVDISCARD_NONREF && !h->nal_ref_idc) ||
2142
4/4
✓ Branch 0 taken 84 times.
✓ Branch 1 taken 25641 times.
✓ Branch 2 taken 40 times.
✓ Branch 3 taken 44 times.
25725 (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 25641 times.
✓ Branch 2 taken 4 times.
✓ Branch 3 taken 36 times.
25681 (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 25643 times.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
25645 (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 25645 times.
25645 h->avctx->skip_frame >= AVDISCARD_ALL) {
2146 80 return 0;
2147 }
2148 }
2149
2150
2/2
✓ Branch 0 taken 9369 times.
✓ Branch 1 taken 28868 times.
38237 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 29063 times.
✓ Branch 1 taken 9174 times.
38237 if (h->current_slice == 0) {
2167 29063 ret = h264_field_start(h, sl, nal, first_slice);
2168
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 29063 times.
29063 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 38237 ret = h264_slice_init(h, sl, nal);
2186
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 38237 times.
38237 if (ret < 0)
2187 return ret;
2188
2189 38237 h->nb_slice_ctx_queued++;
2190
2191 38237 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 12792835 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 12792835 int b_stride = h->b_stride;
2221 12792835 int16_t(*mv_dst)[2] = &sl->mv_cache[list][scan8[0]];
2222 12792835 int8_t *ref_cache = &sl->ref_cache[list][scan8[0]];
2223
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 12792835 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
12792835 if (IS_INTER(mb_type) || IS_DIRECT(mb_type)) {
2224
2/2
✓ Branch 0 taken 10314707 times.
✓ Branch 1 taken 2478128 times.
12792835 if (USES_LIST(top_type, list)) {
2225 10314707 const int b_xy = h->mb2b_xy[top_xy] + 3 * b_stride;
2226 10314707 const int b8_xy = 4 * top_xy + 2;
2227
2/2
✓ Branch 0 taken 343379 times.
✓ Branch 1 taken 9971328 times.
10314707 const int *ref2frm = &h->ref2frm[h->slice_table[top_xy] & (MAX_SLICES - 1)][list][(MB_MBAFF(sl) ? 20 : 2)];
2228 10314707 AV_COPY128(mv_dst - 1 * 8, h->cur_pic.motion_val[list][b_xy + 0]);
2229 10314707 ref_cache[0 - 1 * 8] =
2230 10314707 ref_cache[1 - 1 * 8] = ref2frm[h->cur_pic.ref_index[list][b8_xy + 0]];
2231 10314707 ref_cache[2 - 1 * 8] =
2232 10314707 ref_cache[3 - 1 * 8] = ref2frm[h->cur_pic.ref_index[list][b8_xy + 1]];
2233 } else {
2234 2478128 AV_ZERO128(mv_dst - 1 * 8);
2235 2478128 AV_WN32A(&ref_cache[0 - 1 * 8], ((LIST_NOT_USED) & 0xFF) * 0x01010101u);
2236 }
2237
2238
2/2
✓ Branch 0 taken 12358122 times.
✓ Branch 1 taken 434713 times.
12792835 if (!IS_INTERLACED(mb_type ^ left_type[LTOP])) {
2239
2/2
✓ Branch 0 taken 10346468 times.
✓ Branch 1 taken 2011654 times.
12358122 if (USES_LIST(left_type[LTOP], list)) {
2240 10346468 const int b_xy = h->mb2b_xy[left_xy[LTOP]] + 3;
2241 10346468 const int b8_xy = 4 * left_xy[LTOP] + 1;
2242
2/2
✓ Branch 0 taken 240429 times.
✓ Branch 1 taken 10106039 times.
10346468 const int *ref2frm = &h->ref2frm[h->slice_table[left_xy[LTOP]] & (MAX_SLICES - 1)][list][(MB_MBAFF(sl) ? 20 : 2)];
2243 10346468 AV_COPY32(mv_dst - 1 + 0, h->cur_pic.motion_val[list][b_xy + b_stride * 0]);
2244 10346468 AV_COPY32(mv_dst - 1 + 8, h->cur_pic.motion_val[list][b_xy + b_stride * 1]);
2245 10346468 AV_COPY32(mv_dst - 1 + 16, h->cur_pic.motion_val[list][b_xy + b_stride * 2]);
2246 10346468 AV_COPY32(mv_dst - 1 + 24, h->cur_pic.motion_val[list][b_xy + b_stride * 3]);
2247 10346468 ref_cache[-1 + 0] =
2248 10346468 ref_cache[-1 + 8] = ref2frm[h->cur_pic.ref_index[list][b8_xy + 2 * 0]];
2249 10346468 ref_cache[-1 + 16] =
2250 10346468 ref_cache[-1 + 24] = ref2frm[h->cur_pic.ref_index[list][b8_xy + 2 * 1]];
2251 } else {
2252 2011654 AV_ZERO32(mv_dst - 1 + 0);
2253 2011654 AV_ZERO32(mv_dst - 1 + 8);
2254 2011654 AV_ZERO32(mv_dst - 1 + 16);
2255 2011654 AV_ZERO32(mv_dst - 1 + 24);
2256 2011654 ref_cache[-1 + 0] =
2257 2011654 ref_cache[-1 + 8] =
2258 2011654 ref_cache[-1 + 16] =
2259 2011654 ref_cache[-1 + 24] = LIST_NOT_USED;
2260 }
2261 }
2262 }
2263
2264
2/2
✓ Branch 0 taken 1406230 times.
✓ Branch 1 taken 11386605 times.
12792835 if (!USES_LIST(mb_type, list)) {
2265 1406230 fill_rectangle(mv_dst, 4, 4, 8, pack16to32(0, 0), 4);
2266 1406230 AV_WN32A(&ref_cache[0 * 8], ((LIST_NOT_USED) & 0xFF) * 0x01010101u);
2267 1406230 AV_WN32A(&ref_cache[1 * 8], ((LIST_NOT_USED) & 0xFF) * 0x01010101u);
2268 1406230 AV_WN32A(&ref_cache[2 * 8], ((LIST_NOT_USED) & 0xFF) * 0x01010101u);
2269 1406230 AV_WN32A(&ref_cache[3 * 8], ((LIST_NOT_USED) & 0xFF) * 0x01010101u);
2270 1406230 return;
2271 }
2272
2273 {
2274 11386605 const int8_t *ref = &h->cur_pic.ref_index[list][4 * mb_xy];
2275
2/2
✓ Branch 0 taken 414359 times.
✓ Branch 1 taken 10972246 times.
11386605 const int *ref2frm = &h->ref2frm[sl->slice_num & (MAX_SLICES - 1)][list][(MB_MBAFF(sl) ? 20 : 2)];
2276 11386605 uint32_t ref01 = (pack16to32(ref2frm[ref[0]], ref2frm[ref[1]]) & 0x00FF00FF) * 0x0101;
2277 11386605 uint32_t ref23 = (pack16to32(ref2frm[ref[2]], ref2frm[ref[3]]) & 0x00FF00FF) * 0x0101;
2278 11386605 AV_WN32A(&ref_cache[0 * 8], ref01);
2279 11386605 AV_WN32A(&ref_cache[1 * 8], ref01);
2280 11386605 AV_WN32A(&ref_cache[2 * 8], ref23);
2281 11386605 AV_WN32A(&ref_cache[3 * 8], ref23);
2282 }
2283
2284 {
2285 11386605 int16_t(*mv_src)[2] = &h->cur_pic.motion_val[list][4 * sl->mb_x + 4 * sl->mb_y * b_stride];
2286 11386605 AV_COPY128(mv_dst + 8 * 0, mv_src + 0 * b_stride);
2287 11386605 AV_COPY128(mv_dst + 8 * 1, mv_src + 1 * b_stride);
2288 11386605 AV_COPY128(mv_dst + 8 * 2, mv_src + 2 * b_stride);
2289 11386605 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 12993478 static int fill_filter_caches(const H264Context *h, H264SliceContext *sl, int mb_type)
2297 {
2298 12993478 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 12993478 top_xy = mb_xy - (h->mb_stride << MB_FIELD(sl));
2305
2306 12993478 left_xy[LBOT] = left_xy[LTOP] = mb_xy - 1;
2307
2/2
✓ Branch 0 taken 2023440 times.
✓ Branch 1 taken 10970038 times.
12993478 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 12993478 sl->top_mb_xy = top_xy;
2323 12993478 sl->left_mb_xy[LTOP] = left_xy[LTOP];
2324 12993478 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 12993478 int qp_thresh = sl->qp_thresh; // FIXME strictly we should store qp_thresh for each mb of a slice
2330 12993478 int qp = h->cur_pic.qscale_table[mb_xy];
2331
2/2
✓ Branch 0 taken 1837876 times.
✓ Branch 1 taken 11155602 times.
12993478 if (qp <= qp_thresh &&
2332
2/2
✓ Branch 0 taken 1835897 times.
✓ Branch 1 taken 1979 times.
1837876 (left_xy[LTOP] < 0 ||
2333
4/4
✓ Branch 0 taken 1774984 times.
✓ Branch 1 taken 60913 times.
✓ Branch 2 taken 1686357 times.
✓ Branch 3 taken 90606 times.
1837876 ((qp + h->cur_pic.qscale_table[left_xy[LTOP]] + 1) >> 1) <= qp_thresh) &&
2334 1686357 (top_xy < 0 ||
2335
2/2
✓ Branch 0 taken 1489956 times.
✓ Branch 1 taken 196401 times.
1686357 ((qp + h->cur_pic.qscale_table[top_xy] + 1) >> 1) <= qp_thresh)) {
2336
2/2
✓ Branch 0 taken 1264751 times.
✓ Branch 1 taken 315811 times.
1580562 if (!FRAME_MBAFF(h))
2337 1264751 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 11436556 top_type = h->cur_pic.mb_type[top_xy];
2347 11436556 left_type[LTOP] = h->cur_pic.mb_type[left_xy[LTOP]];
2348 11436556 left_type[LBOT] = h->cur_pic.mb_type[left_xy[LBOT]];
2349
2/2
✓ Branch 0 taken 490374 times.
✓ Branch 1 taken 10946182 times.
11436556 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 485168 times.
✓ Branch 1 taken 10461014 times.
10946182 if (h->slice_table[top_xy] == 0xFFFF)
2356 485168 top_type = 0;
2357
2/2
✓ Branch 0 taken 314311 times.
✓ Branch 1 taken 10631871 times.
10946182 if (h->slice_table[left_xy[LBOT]] == 0xFFFF)
2358 314311 left_type[LTOP] = left_type[LBOT] = 0;
2359 }
2360 11436556 sl->top_type = top_type;
2361 11436556 sl->left_type[LTOP] = left_type[LTOP];
2362 11436556 sl->left_type[LBOT] = left_type[LBOT];
2363
2364
2/2
✓ Branch 0 taken 3018854 times.
✓ Branch 1 taken 8417702 times.
11436556 if (IS_INTRA(mb_type))
2365 3018854 return 0;
2366
2367 8417702 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 4375133 times.
✓ Branch 1 taken 4042569 times.
8417702 if (sl->list_count == 2)
2370 4375133 fill_filter_caches_inter(h, sl, mb_type, top_xy, left_xy,
2371 top_type, left_type, mb_xy, 1);
2372
2373 8417702 nnz = h->non_zero_count[mb_xy];
2374 8417702 nnz_cache = sl->non_zero_count_cache;
2375 8417702 AV_COPY32(&nnz_cache[4 + 8 * 1], &nnz[0]);
2376 8417702 AV_COPY32(&nnz_cache[4 + 8 * 2], &nnz[4]);
2377 8417702 AV_COPY32(&nnz_cache[4 + 8 * 3], &nnz[8]);
2378 8417702 AV_COPY32(&nnz_cache[4 + 8 * 4], &nnz[12]);
2379 8417702 sl->cbp = h->cbp_table[mb_xy];
2380
2381
2/2
✓ Branch 0 taken 7976366 times.
✓ Branch 1 taken 441336 times.
8417702 if (top_type) {
2382 7976366 nnz = h->non_zero_count[top_xy];
2383 7976366 AV_COPY32(&nnz_cache[4 + 8 * 0], &nnz[3 * 4]);
2384 }
2385
2386
2/2
✓ Branch 0 taken 8143997 times.
✓ Branch 1 taken 273705 times.
8417702 if (left_type[LTOP]) {
2387 8143997 nnz = h->non_zero_count[left_xy[LTOP]];
2388 8143997 nnz_cache[3 + 8 * 1] = nnz[3 + 0 * 4];
2389 8143997 nnz_cache[3 + 8 * 2] = nnz[3 + 1 * 4];
2390 8143997 nnz_cache[3 + 8 * 3] = nnz[3 + 2 * 4];
2391 8143997 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 6346734 times.
✓ Branch 2 taken 246166 times.
✓ Branch 3 taken 1824802 times.
8417702 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 8417702 return 0;
2436 }
2437
2438 415610 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 415610 const int end_mb_y = sl->mb_y + FRAME_MBAFF(h);
2443 415610 const int old_slice_type = sl->slice_type;
2444 415610 const int pixel_shift = h->pixel_shift;
2445 415610 const int block_h = 16 >> h->chroma_y_shift;
2446
2447
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 415610 times.
415610 if (h->postpone_filter)
2448 return;
2449
2450
2/2
✓ Branch 0 taken 338492 times.
✓ Branch 1 taken 77118 times.
415610 if (sl->deblocking_filter) {
2451
2/2
✓ Branch 0 taken 11981758 times.
✓ Branch 1 taken 338492 times.
12320250 for (mb_x = start_x; mb_x < end_x; mb_x++)
2452
2/2
✓ Branch 0 taken 12993478 times.
✓ Branch 1 taken 11981758 times.
24975236 for (mb_y = end_mb_y - FRAME_MBAFF(h); mb_y <= end_mb_y; mb_y++) {
2453 int mb_xy, mb_type;
2454 12993478 mb_xy = sl->mb_xy = mb_x + mb_y * h->mb_stride;
2455 12993478 mb_type = h->cur_pic.mb_type[mb_xy];
2456
2457
2/2
✓ Branch 0 taken 2023440 times.
✓ Branch 1 taken 10970038 times.
12993478 if (FRAME_MBAFF(h))
2458 2023440 sl->mb_mbaff =
2459 2023440 sl->mb_field_decoding_flag = !!IS_INTERLACED(mb_type);
2460
2461 12993478 sl->mb_x = mb_x;
2462 12993478 sl->mb_y = mb_y;
2463 12993478 dest_y = h->cur_pic.f->data[0] +
2464 12993478 ((mb_x << pixel_shift) + mb_y * sl->linesize) * 16;
2465 25986956 dest_cb = h->cur_pic.f->data[1] +
2466
2/2
✓ Branch 0 taken 80328 times.
✓ Branch 1 taken 12913150 times.
12993478 (mb_x << pixel_shift) * (8 << CHROMA444(h)) +
2467 12993478 mb_y * sl->uvlinesize * block_h;
2468 25986956 dest_cr = h->cur_pic.f->data[2] +
2469
2/2
✓ Branch 0 taken 80328 times.
✓ Branch 1 taken 12913150 times.
12993478 (mb_x << pixel_shift) * (8 << CHROMA444(h)) +
2470 12993478 mb_y * sl->uvlinesize * block_h;
2471 // FIXME simplify above
2472
2473
2/2
✓ Branch 0 taken 4679076 times.
✓ Branch 1 taken 8314402 times.
12993478 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 8314402 linesize = sl->mb_linesize = sl->linesize;
2483 8314402 uvlinesize = sl->mb_uvlinesize = sl->uvlinesize;
2484 }
2485 12993478 backup_mb_border(h, sl, dest_y, dest_cb, dest_cr, linesize,
2486 uvlinesize, 0);
2487
2/2
✓ Branch 1 taken 1556922 times.
✓ Branch 2 taken 11436556 times.
12993478 if (fill_filter_caches(h, sl, mb_type))
2488 1556922 continue;
2489 11436556 sl->chroma_qp[0] = get_chroma_qp(h->ps.pps, 0, h->cur_pic.qscale_table[mb_xy]);
2490 11436556 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 9705287 times.
11436556 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 9705287 ff_h264_filter_mb_fast(h, sl, mb_x, mb_y, dest_y, dest_cb,
2497 dest_cr, linesize, uvlinesize);
2498 }
2499 }
2500 }
2501 415610 sl->slice_type = old_slice_type;
2502 415610 sl->mb_x = end_x;
2503 415610 sl->mb_y = end_mb_y - FRAME_MBAFF(h);
2504 415610 sl->chroma_qp[0] = get_chroma_qp(h->ps.pps, 0, sl->qscale);
2505 415610 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 411050 static void decode_finish_row(const H264Context *h, H264SliceContext *sl)
2522 {
2523 411050 int top = 16 * (sl->mb_y >> FIELD_PICTURE(h));
2524 411050 int pic_height = 16 * h->mb_height >> FIELD_PICTURE(h);
2525 411050 int height = 16 << FRAME_MBAFF(h);
2526 411050 int deblock_border = (16 + 4) << FRAME_MBAFF(h);
2527
2528
2/2
✓ Branch 0 taken 335400 times.
✓ Branch 1 taken 75650 times.
411050 if (sl->deblocking_filter) {
2529
2/2
✓ Branch 0 taken 23200 times.
✓ Branch 1 taken 312200 times.
335400 if ((top + height) >= pic_height)
2530 23200 height += deblock_border;
2531 335400 top -= deblock_border;
2532 }
2533
2534
3/4
✓ Branch 0 taken 411050 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 23201 times.
✓ Branch 3 taken 387849 times.
411050 if (top >= pic_height || (top + height) < 0)
2535 23201 return;
2536
2537 387849 height = FFMIN(height, pic_height - top);
2538
2/2
✓ Branch 0 taken 23201 times.
✓ Branch 1 taken 364648 times.
387849 if (top < 0) {
2539 23201 height = top + height;
2540 23201 top = 0;
2541 }
2542
2543 387849 ff_h264_draw_horiz_band(h, sl, top, height);
2544
2545
3/4
✓ Branch 0 taken 251034 times.
✓ Branch 1 taken 136815 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 251034 times.
387849 if (h->droppable || h->er.error_occurred)
2546 136815 return;
2547
2548 251034 ff_thread_report_progress(&h->cur_pic_ptr->tf, top + height - 1,
2549 251034 h->picture_structure == PICT_BOTTOM_FIELD);
2550 }
2551
2552 38237 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 37037 times.
38237 if (!sl->h264->enable_er)
2557 1200 return;
2558
2559 if (CONFIG_ERROR_RESILIENCE) {
2560 37037 ff_er_add_slice(sl->er, startx, starty, endx, endy, status);
2561 }
2562 }
2563
2564 38237 static int decode_slice(struct AVCodecContext *avctx, void *arg)
2565 {
2566 38237 H264SliceContext *sl = arg;
2567 38237 const H264Context *h = sl->h264;
2568 38237 int lf_x_start = sl->mb_x;
2569 38237 int orig_deblock = sl->deblocking_filter;
2570 int ret;
2571
2572 38237 sl->linesize = h->cur_pic_ptr->f->linesize[0];
2573 38237 sl->uvlinesize = h->cur_pic_ptr->f->linesize[1];
2574
2575 38237 ret = alloc_scratch_buffers(sl, sl->linesize);
2576
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 38237 times.
38237 if (ret < 0)
2577 return ret;
2578
2579 38237 sl->mb_skip_run = -1;
2580
2581
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 38237 times.
38237 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 38237 times.
38237 if (h->postpone_filter)
2584 sl->deblocking_filter = 0;
2585
2586
4/4
✓ Branch 0 taken 33261 times.
✓ Branch 1 taken 4976 times.
✓ Branch 2 taken 9160 times.
✓ Branch 3 taken 24101 times.
38237 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 37037 times.
✓ Branch 1 taken 1200 times.
✓ Branch 2 taken 27877 times.
✓ Branch 3 taken 9160 times.
✓ Branch 4 taken 27877 times.
✗ Branch 5 not taken.
38237 if (!(h->avctx->active_thread_type & FF_THREAD_SLICE) && h->picture_structure == PICT_FRAME && sl->er->error_status_table) {
2590 27877 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 21921 times.
27877 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 21486 times.
✓ Branch 1 taken 16751 times.
38237 if (h->ps.pps->cabac) {
2600 /* realign */
2601 21486 align_get_bits(&sl->gb);
2602
2603 /* init cabac */
2604 42972 ret = ff_init_cabac_decoder(&sl->cabac,
2605 21486 sl->gb.buffer + get_bits_count(&sl->gb) / 8,
2606 21486 (get_bits_left(&sl->gb) + 7) / 8);
2607
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 21486 times.
21486 if (ret < 0)
2608 return ret;
2609
2610 21486 ff_h264_init_cabac_states(h, sl);
2611
2612 9771909 for (;;) {
2613 int ret, eos;
2614
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9793395 times.
9793395 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 9793395 ret = ff_h264_decode_mb_cabac(h, sl);
2623
2624
2/2
✓ Branch 0 taken 9793394 times.
✓ Branch 1 taken 1 times.
9793395 if (ret >= 0)
2625 9793394 ff_h264_hl_decode_mb(h, sl);
2626
2627 // FIXME optimal? or let mb_decode decode 16x32 ?
2628
4/4
✓ Branch 0 taken 9793394 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 903372 times.
✓ Branch 3 taken 8890022 times.
9793395 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 9793395 eos = get_cabac_terminate(&sl->cabac);
2638
2639
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9793395 times.
9793395 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 9793394 times.
9793395 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 9793394 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 9793393 times.
9793395 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 249936 times.
✓ Branch 1 taken 9543457 times.
9793393 if (++sl->mb_x >= h->mb_width) {
2660 249936 loop_filter(h, sl, lf_x_start, sl->mb_x);
2661 249936 sl->mb_x = lf_x_start = 0;
2662 249936 decode_finish_row(h, sl);
2663 249936 ++sl->mb_y;
2664
4/4
✓ Branch 0 taken 229012 times.
✓ Branch 1 taken 20924 times.
✓ Branch 2 taken 60241 times.
✓ Branch 3 taken 168771 times.
249936 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 9771909 times.
✓ Branch 1 taken 21484 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 9771909 times.
9793393 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 21484 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 20102 times.
21484 if (sl->mb_x > lf_x_start)
2677 1382 loop_filter(h, sl, lf_x_start, sl->mb_x);
2678 21484 goto finish;
2679 }
2680 }
2681 } else {
2682 4578647 for (;;) {
2683 int ret;
2684
2685
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4595398 times.
4595398 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 4595398 ret = ff_h264_decode_mb_cavlc(h, sl);
2694
2695
1/2
✓ Branch 0 taken 4595398 times.
✗ Branch 1 not taken.
4595398 if (ret >= 0)
2696 4595398 ff_h264_hl_decode_mb(h, sl);
2697
2698 // FIXME optimal? or let mb_decode decode 16x32 ?
2699
3/4
✓ Branch 0 taken 4595398 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 254472 times.
✓ Branch 3 taken 4340926 times.
4595398 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 4595398 times.
4595398 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 161114 times.
✓ Branch 1 taken 4434284 times.
4595398 if (++sl->mb_x >= h->mb_width) {
2717 161114 loop_filter(h, sl, lf_x_start, sl->mb_x);
2718 161114 sl->mb_x = lf_x_start = 0;
2719 161114 decode_finish_row(h, sl);
2720 161114 ++sl->mb_y;
2721
4/4
✓ Branch 0 taken 155637 times.
✓ Branch 1 taken 5477 times.
✓ Branch 2 taken 26666 times.
✓ Branch 3 taken 128971 times.
161114 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 12661 times.
✓ Branch 1 taken 148453 times.
161114 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 12661 times.
12661 if ( get_bits_left(&sl->gb) == 0
2731 || get_bits_left(&sl->gb) > 0 && !(h->avctx->err_recognition & AV_EF_AGGRESSIVE)) {
2732 12661 er_add_slice(sl, sl->resync_mb_x, sl->resync_mb_y,
2733 12661 sl->mb_x - 1, sl->mb_y, ER_MB_END);
2734
2735 12661 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 38235 finish:
2767 38235 sl->deblocking_filter = orig_deblock;
2768 38235 return 0;
2769 }
2770
2771 /**
2772 * Call decode_slice() for each context.
2773 *
2774 * @param h h264 master context
2775 */
2776 67401 int ff_h264_execute_decode_slices(H264Context *h)
2777 {
2778 67401 AVCodecContext *const avctx = h->avctx;
2779 H264SliceContext *sl;
2780 67401 int context_count = h->nb_slice_ctx_queued;
2781 67401 int ret = 0;
2782 int i, j;
2783
2784 67401 h->slice_ctx[0].next_slice_idx = INT_MAX;
2785
2786
3/4
✓ Branch 0 taken 67401 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 29614 times.
✓ Branch 3 taken 37787 times.
67401 if (h->avctx->hwaccel || context_count < 1)
2787 29614 return 0;
2788
2789
2/4
✓ Branch 0 taken 37787 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 37787 times.
37787 av_assert0(context_count && h->slice_ctx[context_count - 1].mb_y < h->mb_height);
2790
2791
2/2
✓ Branch 0 taken 37337 times.
✓ Branch 1 taken 450 times.
37787 if (context_count == 1) {
2792
2793 37337 h->slice_ctx[0].next_slice_idx = h->mb_width * h->mb_height;
2794 37337 h->postpone_filter = 0;
2795
2796 37337 ret = decode_slice(avctx, &h->slice_ctx[0]);
2797 37337 h->mb_y = h->slice_ctx[0].mb_y;
2798
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 37335 times.
37337 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 37787 h->nb_slice_ctx_queued = 0;
2849 37787 return ret;
2850 }
2851