FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/h264_slice.c
Date: 2025-03-08 20:38:41
Exec Total Coverage
Lines: 1373 1670 82.2%
Functions: 32 33 97.0%
Branches: 916 1253 73.1%

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