FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/h264_slice.c
Date: 2024-04-25 05:10:44
Exec Total Coverage
Lines: 1374 1670 82.3%
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 "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 28972 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 1042992 times.
✓ Branch 1 taken 28972 times.
1071964 for (i = 0; i < H264_MAX_PICTURE_COUNT; i++) {
123
6/6
✓ Branch 0 taken 145907 times.
✓ Branch 1 taken 897085 times.
✓ Branch 2 taken 25347 times.
✓ Branch 3 taken 120560 times.
✓ Branch 4 taken 2610 times.
✓ Branch 5 taken 22737 times.
1042992 if (h->DPB[i].f->buf[0] && !h->DPB[i].reference &&
124
2/2
✓ Branch 0 taken 723 times.
✓ Branch 1 taken 1887 times.
2610 (remove_current || &h->DPB[i] != h->cur_pic_ptr)) {
125 23460 ff_h264_unref_picture(&h->DPB[i]);
126 }
127 }
128 28972 }
129
130 37360 static int alloc_scratch_buffers(H264SliceContext *sl, int linesize)
131 {
132 37360 const H264Context *h = sl->h264;
133 37360 int alloc_size = FFALIGN(FFABS(linesize) + 32, 32);
134
135 37360 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 37360 av_fast_malloc(&sl->edge_emu_buffer, &sl->edge_emu_buffer_allocated, alloc_size * 2 * 21);
139
140 37360 av_fast_mallocz(&sl->top_borders[0], &sl->top_borders_allocated[0],
141 37360 h->mb_width * 16 * 3 * sizeof(uint8_t) * 2);
142 37360 av_fast_mallocz(&sl->top_borders[1], &sl->top_borders_allocated[1],
143 37360 h->mb_width * 16 * 3 * sizeof(uint8_t) * 2);
144
145
2/4
✓ Branch 0 taken 37360 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 37360 times.
✗ Branch 3 not taken.
37360 if (!sl->bipred_scratchpad || !sl->edge_emu_buffer ||
146
2/4
✓ Branch 0 taken 37360 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 37360 times.
37360 !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 37360 return 0;
160 }
161
162 601 static int init_table_pools(H264Context *h)
163 {
164 601 const int big_mb_num = h->mb_stride * (h->mb_height + 1) + 1;
165 601 const int mb_array_size = h->mb_stride * h->mb_height;
166 601 const int b4_stride = h->mb_width * 4 + 1;
167 601 const int b4_array_size = b4_stride * h->mb_height * 4;
168
169 601 h->qscale_table_pool = ff_refstruct_pool_alloc(big_mb_num + h->mb_stride, 0);
170 601 h->mb_type_pool = ff_refstruct_pool_alloc((big_mb_num + h->mb_stride) *
171 sizeof(uint32_t), 0);
172 601 h->motion_val_pool = ff_refstruct_pool_alloc(2 * (b4_array_size + 4) *
173 sizeof(int16_t), 0);
174 601 h->ref_index_pool = ff_refstruct_pool_alloc(4 * mb_array_size, 0);
175
176
3/6
✓ Branch 0 taken 601 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 601 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 601 times.
✗ Branch 5 not taken.
601 if (!h->qscale_table_pool || !h->mb_type_pool || !h->motion_val_pool ||
177
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 601 times.
601 !h->ref_index_pool) {
178 ff_refstruct_pool_uninit(&h->qscale_table_pool);
179 ff_refstruct_pool_uninit(&h->mb_type_pool);
180 ff_refstruct_pool_uninit(&h->motion_val_pool);
181 ff_refstruct_pool_uninit(&h->ref_index_pool);
182 return AVERROR(ENOMEM);
183 }
184
185 601 return 0;
186 }
187
188 25610 static int alloc_picture(H264Context *h, H264Picture *pic)
189 {
190 25610 int i, ret = 0;
191
192
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 25610 times.
25610 av_assert0(!pic->f->data[0]);
193
194 25610 pic->tf.f = pic->f;
195 25610 ret = ff_thread_get_ext_buffer(h->avctx, &pic->tf,
196 25610 pic->reference ? AV_GET_BUFFER_FLAG_REF : 0);
197
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 25610 times.
25610 if (ret < 0)
198 goto fail;
199
200
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 25610 times.
25610 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 25610 ret = ff_hwaccel_frame_priv_alloc(h->avctx, &pic->hwaccel_picture_private);
210
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 25610 times.
25610 if (ret < 0)
211 goto fail;
212
213
2/2
✓ Branch 0 taken 42 times.
✓ Branch 1 taken 25568 times.
25610 if (h->decode_error_flags_pool) {
214 42 pic->decode_error_flags = ff_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 601 times.
✓ Branch 1 taken 25009 times.
25610 if (!h->qscale_table_pool) {
234 601 ret = init_table_pools(h);
235
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 601 times.
601 if (ret < 0)
236 goto fail;
237 }
238
239 25610 pic->qscale_table_base = ff_refstruct_pool_get(h->qscale_table_pool);
240 25610 pic->mb_type_base = ff_refstruct_pool_get(h->mb_type_pool);
241
2/4
✓ Branch 0 taken 25610 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 25610 times.
25610 if (!pic->qscale_table_base || !pic->mb_type_base)
242 goto fail;
243
244 25610 pic->mb_type = pic->mb_type_base + 2 * h->mb_stride + 1;
245 25610 pic->qscale_table = pic->qscale_table_base + 2 * h->mb_stride + 1;
246
247
2/2
✓ Branch 0 taken 51220 times.
✓ Branch 1 taken 25610 times.
76830 for (i = 0; i < 2; i++) {
248 51220 pic->motion_val_base[i] = ff_refstruct_pool_get(h->motion_val_pool);
249 51220 pic->ref_index[i] = ff_refstruct_pool_get(h->ref_index_pool);
250
2/4
✓ Branch 0 taken 51220 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 51220 times.
51220 if (!pic->motion_val_base[i] || !pic->ref_index[i])
251 goto fail;
252
253 51220 pic->motion_val[i] = pic->motion_val_base[i] + 4;
254 }
255
256 25610 pic->pps = ff_refstruct_ref_c(h->ps.pps);
257
258 25610 pic->mb_width = h->mb_width;
259 25610 pic->mb_height = h->mb_height;
260 25610 pic->mb_stride = h->mb_stride;
261
262 25610 return 0;
263 fail:
264 ff_h264_unref_picture(pic);
265 return (ret < 0) ? ret : AVERROR(ENOMEM);
266 }
267
268 25610 static int find_unused_picture(const H264Context *h)
269 {
270 int i;
271
272
1/2
✓ Branch 0 taken 86982 times.
✗ Branch 1 not taken.
86982 for (i = 0; i < H264_MAX_PICTURE_COUNT; i++) {
273
2/2
✓ Branch 0 taken 25610 times.
✓ Branch 1 taken 61372 times.
86982 if (!h->DPB[i].f->buf[0])
274 25610 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 186 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 5084 times.
✓ Branch 1 taken 186 times.
5270 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 265 times.
✓ Branch 1 taken 4819 times.
✓ Branch 2 taken 265 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 265 times.
✗ Branch 5 not taken.
5084 to[i] = REBASE_PICTURE(from[i], new_base, old_base);
297 }
298 186 }
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 62 int ff_h264_update_thread_context(AVCodecContext *dst,
331 const AVCodecContext *src)
332 {
333 62 H264Context *h = dst->priv_data, *h1 = src->priv_data;
334 62 int inited = h->context_initialized, err = 0;
335 62 int need_reinit = 0;
336 int i, ret;
337
338
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 62 times.
62 if (dst == src)
339 return 0;
340
341
3/4
✓ Branch 0 taken 46 times.
✓ Branch 1 taken 16 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 46 times.
62 if (inited && !h1->ps.sps)
342 return AVERROR_INVALIDDATA;
343
344
2/2
✓ Branch 0 taken 46 times.
✓ Branch 1 taken 16 times.
62 if (inited &&
345
1/2
✓ Branch 0 taken 46 times.
✗ Branch 1 not taken.
46 (h->width != h1->width ||
346
1/2
✓ Branch 0 taken 46 times.
✗ Branch 1 not taken.
46 h->height != h1->height ||
347
1/2
✓ Branch 0 taken 46 times.
✗ Branch 1 not taken.
46 h->mb_width != h1->mb_width ||
348
1/2
✓ Branch 0 taken 46 times.
✗ Branch 1 not taken.
46 h->mb_height != h1->mb_height ||
349
1/2
✓ Branch 0 taken 46 times.
✗ Branch 1 not taken.
46 !h->ps.sps ||
350
1/2
✓ Branch 0 taken 46 times.
✗ Branch 1 not taken.
46 h->ps.sps->bit_depth_luma != h1->ps.sps->bit_depth_luma ||
351
1/2
✓ Branch 0 taken 46 times.
✗ Branch 1 not taken.
46 h->ps.sps->chroma_format_idc != h1->ps.sps->chroma_format_idc ||
352
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 46 times.
46 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 62 memcpy(h->block_offset, h1->block_offset, sizeof(h->block_offset));
358
359 // SPS/PPS
360
2/2
✓ Branch 0 taken 1984 times.
✓ Branch 1 taken 62 times.
2046 for (int i = 0; i < FF_ARRAY_ELEMS(h->ps.sps_list); i++)
361 1984 ff_refstruct_replace(&h->ps.sps_list[i], h1->ps.sps_list[i]);
362
2/2
✓ Branch 0 taken 15872 times.
✓ Branch 1 taken 62 times.
15934 for (int i = 0; i < FF_ARRAY_ELEMS(h->ps.pps_list); i++)
363 15872 ff_refstruct_replace(&h->ps.pps_list[i], h1->ps.pps_list[i]);
364
365 62 ff_refstruct_replace(&h->ps.pps, h1->ps.pps);
366 62 h->ps.sps = h1->ps.sps;
367
368
3/4
✓ Branch 0 taken 62 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 16 times.
✓ Branch 3 taken 46 times.
62 if (need_reinit || !inited) {
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 62 h->width_from_caller = h1->width_from_caller;
390 62 h->height_from_caller = h1->height_from_caller;
391 62 h->coded_picture_number = h1->coded_picture_number;
392 62 h->first_field = h1->first_field;
393 62 h->picture_structure = h1->picture_structure;
394 62 h->mb_aff_frame = h1->mb_aff_frame;
395 62 h->droppable = h1->droppable;
396
397
2/2
✓ Branch 0 taken 2232 times.
✓ Branch 1 taken 62 times.
2294 for (i = 0; i < H264_MAX_PICTURE_COUNT; i++) {
398 2232 ret = ff_h264_replace_picture(&h->DPB[i], &h1->DPB[i]);
399
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2232 times.
2232 if (ret < 0)
400 return ret;
401 }
402
403
4/6
✓ Branch 0 taken 42 times.
✓ Branch 1 taken 20 times.
✓ Branch 2 taken 42 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 42 times.
✗ Branch 5 not taken.
62 h->cur_pic_ptr = REBASE_PICTURE(h1->cur_pic_ptr, h, h1);
404 62 ret = ff_h264_replace_picture(&h->cur_pic, &h1->cur_pic);
405
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 62 times.
62 if (ret < 0)
406 return ret;
407
408 62 h->enable_er = h1->enable_er;
409 62 h->workaround_bugs = h1->workaround_bugs;
410 62 h->droppable = h1->droppable;
411
412 // extradata/NAL handling
413 62 h->is_avc = h1->is_avc;
414 62 h->nal_length_size = h1->nal_length_size;
415
416 62 memcpy(&h->poc, &h1->poc, sizeof(h->poc));
417
418 62 memcpy(h->short_ref, h1->short_ref, sizeof(h->short_ref));
419 62 memcpy(h->long_ref, h1->long_ref, sizeof(h->long_ref));
420 62 memcpy(h->delayed_pic, h1->delayed_pic, sizeof(h->delayed_pic));
421 62 memcpy(h->last_pocs, h1->last_pocs, sizeof(h->last_pocs));
422
423 62 h->next_output_pic = h1->next_output_pic;
424 62 h->next_outputed_poc = h1->next_outputed_poc;
425 62 h->poc_offset = h1->poc_offset;
426
427 62 memcpy(h->mmco, h1->mmco, sizeof(h->mmco));
428 62 h->nb_mmco = h1->nb_mmco;
429 62 h->mmco_reset = h1->mmco_reset;
430 62 h->explicit_ref_marking = h1->explicit_ref_marking;
431 62 h->long_ref_count = h1->long_ref_count;
432 62 h->short_ref_count = h1->short_ref_count;
433
434 62 copy_picture_range(h->short_ref, h1->short_ref, 32, h, h1);
435 62 copy_picture_range(h->long_ref, h1->long_ref, 32, h, h1);
436 62 copy_picture_range(h->delayed_pic, h1->delayed_pic,
437 FF_ARRAY_ELEMS(h->delayed_pic), h, h1);
438
439 62 h->frame_recovered = h1->frame_recovered;
440
441 62 ret = ff_h264_sei_ctx_replace(&h->sei, &h1->sei);
442
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 62 times.
62 if (ret < 0)
443 return ret;
444
445 62 h->sei.common.unregistered.x264_build = h1->sei.common.unregistered.x264_build;
446 62 h->sei.common.mastering_display = h1->sei.common.mastering_display;
447 62 h->sei.common.content_light = h1->sei.common.content_light;
448
449
2/2
✓ Branch 0 taken 20 times.
✓ Branch 1 taken 42 times.
62 if (!h->cur_pic_ptr)
450 20 return 0;
451
452
2/2
✓ Branch 0 taken 27 times.
✓ Branch 1 taken 15 times.
42 if (!h->droppable) {
453 27 err = ff_h264_execute_ref_pic_marking(h);
454 27 h->poc.prev_poc_msb = h->poc.poc_msb;
455 27 h->poc.prev_poc_lsb = h->poc.poc_lsb;
456 }
457 42 h->poc.prev_frame_num_offset = h->poc.frame_num_offset;
458 42 h->poc.prev_frame_num = h->poc.frame_num;
459
460 42 h->recovery_frame = h1->recovery_frame;
461 42 h->non_gray = h1->non_gray;
462
463 42 return err;
464 }
465
466 50 int ff_h264_update_thread_context_for_user(AVCodecContext *dst,
467 const AVCodecContext *src)
468 {
469 50 H264Context *h = dst->priv_data;
470 50 const H264Context *h1 = src->priv_data;
471
472 50 h->is_avc = h1->is_avc;
473 50 h->nal_length_size = h1->nal_length_size;
474
475 50 return 0;
476 }
477
478 25610 static int h264_frame_start(H264Context *h)
479 {
480 H264Picture *pic;
481 int i, ret;
482 25610 const int pixel_shift = h->pixel_shift;
483
484
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 25610 times.
25610 if (!ff_thread_can_start_frame(h->avctx)) {
485 av_log(h->avctx, AV_LOG_ERROR, "Attempt to start a frame outside SETUP state\n");
486 return AVERROR_BUG;
487 }
488
489 25610 release_unused_pictures(h, 1);
490 25610 h->cur_pic_ptr = NULL;
491
492 25610 i = find_unused_picture(h);
493
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 25610 times.
25610 if (i < 0) {
494 av_log(h->avctx, AV_LOG_ERROR, "no frame buffer available\n");
495 return i;
496 }
497 25610 pic = &h->DPB[i];
498
499
2/2
✓ Branch 0 taken 17666 times.
✓ Branch 1 taken 7944 times.
25610 pic->reference = h->droppable ? 0 : h->picture_structure;
500 25610 pic->field_picture = h->picture_structure != PICT_FRAME;
501 25610 pic->frame_num = h->poc.frame_num;
502 /*
503 * Zero key_frame here; IDR markings per slice in frame or fields are ORed
504 * in later.
505 * See decode_nal_units().
506 */
507 25610 pic->f->flags &= ~AV_FRAME_FLAG_KEY;
508 25610 pic->mmco_reset = 0;
509 25610 pic->recovered = 0;
510 25610 pic->invalid_gap = 0;
511 25610 pic->sei_recovery_frame_cnt = h->sei.recovery_point.recovery_frame_cnt;
512
513 25610 pic->f->pict_type = h->slice_ctx[0].slice_type;
514
515 25610 pic->f->crop_left = h->crop_left;
516 25610 pic->f->crop_right = h->crop_right;
517 25610 pic->f->crop_top = h->crop_top;
518 25610 pic->f->crop_bottom = h->crop_bottom;
519
520
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 25610 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
25610 pic->needs_fg = h->sei.common.film_grain_characteristics.present && !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 25610 times.
25610 if ((ret = alloc_picture(h, pic)) < 0)
524 return ret;
525
526 25610 h->cur_pic_ptr = pic;
527 25610 ff_h264_unref_picture(&h->cur_pic);
528 if (CONFIG_ERROR_RESILIENCE) {
529 25610 ff_h264_set_erpic(&h->er.cur_pic, NULL);
530 }
531
532
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 25610 times.
25610 if ((ret = ff_h264_ref_picture(&h->cur_pic, h->cur_pic_ptr)) < 0)
533 return ret;
534
535
2/2
✓ Branch 0 taken 25910 times.
✓ Branch 1 taken 25610 times.
51520 for (i = 0; i < h->nb_slice_ctx; i++) {
536 25910 h->slice_ctx[i].linesize = h->cur_pic_ptr->f->linesize[0];
537 25910 h->slice_ctx[i].uvlinesize = h->cur_pic_ptr->f->linesize[1];
538 }
539
540
2/2
✓ Branch 0 taken 25310 times.
✓ Branch 1 taken 300 times.
25610 if (CONFIG_ERROR_RESILIENCE && h->enable_er) {
541 25310 ff_er_frame_start(&h->er);
542 25310 ff_h264_set_erpic(&h->er.last_pic, NULL);
543 25310 ff_h264_set_erpic(&h->er.next_pic, NULL);
544 }
545
546
2/2
✓ Branch 0 taken 409760 times.
✓ Branch 1 taken 25610 times.
435370 for (i = 0; i < 16; i++) {
547 409760 h->block_offset[i] = (4 * ((scan8[i] - scan8[0]) & 7) << pixel_shift) + 4 * pic->f->linesize[0] * ((scan8[i] - scan8[0]) >> 3);
548 409760 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 409760 times.
✓ Branch 1 taken 25610 times.
435370 for (i = 0; i < 16; i++) {
551 409760 h->block_offset[16 + i] =
552 409760 h->block_offset[32 + i] = (4 * ((scan8[i] - scan8[0]) & 7) << pixel_shift) + 4 * pic->f->linesize[1] * ((scan8[i] - scan8[0]) >> 3);
553 409760 h->block_offset[48 + 16 + i] =
554 409760 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 25610 h->cur_pic_ptr->reference = 0;
562
563 25610 h->cur_pic_ptr->field_poc[0] = h->cur_pic_ptr->field_poc[1] = INT_MAX;
564
565 25610 h->next_output_pic = NULL;
566
567 25610 h->postpone_filter = 0;
568
569
4/4
✓ Branch 0 taken 2861 times.
✓ Branch 1 taken 22749 times.
✓ Branch 2 taken 2353 times.
✓ Branch 3 taken 508 times.
25610 h->mb_aff_frame = h->ps.sps->mb_aff && (h->picture_structure == PICT_FRAME);
570
571
2/2
✓ Branch 0 taken 2062 times.
✓ Branch 1 taken 23548 times.
25610 if (h->sei.common.unregistered.x264_build >= 0)
572 2062 h->x264_build = h->sei.common.unregistered.x264_build;
573
574 assert(h->cur_pic_ptr->long_ref == 0);
575
576 25610 return 0;
577 }
578
579 12576316 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 12576316 int top_idx = 1;
587 12576316 const int pixel_shift = h->pixel_shift;
588 12576316 int chroma444 = CHROMA444(h);
589 12576316 int chroma422 = CHROMA422(h);
590
591 12576316 src_y -= linesize;
592 12576316 src_cb -= uvlinesize;
593 12576316 src_cr -= uvlinesize;
594
595
3/4
✓ Branch 0 taken 12576316 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2020896 times.
✓ Branch 3 taken 10555420 times.
12576316 if (!simple && FRAME_MBAFF(h)) {
596
2/2
✓ Branch 0 taken 1010448 times.
✓ Branch 1 taken 1010448 times.
2020896 if (sl->mb_y & 1) {
597
2/2
✓ Branch 0 taken 747670 times.
✓ Branch 1 taken 262778 times.
1010448 if (!MB_MBAFF(sl)) {
598 747670 top_border = sl->top_borders[0][sl->mb_x];
599 747670 AV_COPY128(top_border, src_y + 15 * linesize);
600
2/2
✓ Branch 0 taken 213139 times.
✓ Branch 1 taken 534531 times.
747670 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 747670 times.
747670 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 641160 times.
747670 } 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 534531 times.
641160 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 534531 AV_COPY64(top_border + 16, src_cb + 7 * uvlinesize);
627 534531 AV_COPY64(top_border + 24, src_cr + 7 * uvlinesize);
628 }
629 }
630 }
631 }
632
2/2
✓ Branch 0 taken 262778 times.
✓ Branch 1 taken 747670 times.
1010448 } else if (MB_MBAFF(sl)) {
633 262778 top_idx = 0;
634 } else
635 747670 return;
636 }
637
638 11828646 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 11828646 AV_COPY128(top_border, src_y + 16 * linesize);
642
2/2
✓ Branch 0 taken 1381381 times.
✓ Branch 1 taken 10447265 times.
11828646 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 11748318 times.
11828646 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 11159683 times.
11748318 } 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 10382777 times.
11159683 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 10382777 AV_COPY64(top_border + 16, src_cb + 8 * uvlinesize);
670 10382777 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 601 static void init_scan_tables(H264Context *h)
746 {
747 int i;
748
2/2
✓ Branch 0 taken 9616 times.
✓ Branch 1 taken 601 times.
10217 for (i = 0; i < 16; i++) {
749 #define TRANSPOSE(x) ((x) >> 2) | (((x) << 2) & 0xF)
750 9616 h->zigzag_scan[i] = TRANSPOSE(ff_zigzag_scan[i]);
751 9616 h->field_scan[i] = TRANSPOSE(field_scan[i]);
752 #undef TRANSPOSE
753 }
754
2/2
✓ Branch 0 taken 38464 times.
✓ Branch 1 taken 601 times.
39065 for (i = 0; i < 64; i++) {
755 #define TRANSPOSE(x) ((x) >> 3) | (((x) & 7) << 3)
756 38464 h->zigzag_scan8x8[i] = TRANSPOSE(ff_zigzag_direct[i]);
757 38464 h->zigzag_scan8x8_cavlc[i] = TRANSPOSE(zigzag_scan8x8_cavlc[i]);
758 38464 h->field_scan8x8[i] = TRANSPOSE(field_scan8x8[i]);
759 38464 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 587 times.
601 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 587 memcpy(h->zigzag_scan_q0 , h->zigzag_scan , sizeof(h->zigzag_scan_q0 ));
771 587 memcpy(h->zigzag_scan8x8_q0 , h->zigzag_scan8x8 , sizeof(h->zigzag_scan8x8_q0 ));
772 587 memcpy(h->zigzag_scan8x8_cavlc_q0 , h->zigzag_scan8x8_cavlc , sizeof(h->zigzag_scan8x8_cavlc_q0));
773 587 memcpy(h->field_scan_q0 , h->field_scan , sizeof(h->field_scan_q0 ));
774 587 memcpy(h->field_scan8x8_q0 , h->field_scan8x8 , sizeof(h->field_scan8x8_q0 ));
775 587 memcpy(h->field_scan8x8_cavlc_q0 , h->field_scan8x8_cavlc , sizeof(h->field_scan8x8_cavlc_q0 ));
776 }
777 601 }
778
779 28546 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 28546 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 28019 times.
✗ Branch 5 not taken.
28546 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
2/2
✓ Branch 0 taken 51 times.
✓ Branch 1 taken 321 times.
372 if (CHROMA444(h)) {
812
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 51 times.
51 if (h->avctx->colorspace == AVCOL_SPC_RGB) {
813 *fmt++ = AV_PIX_FMT_GBRP10;
814 } else
815 51 *fmt++ = AV_PIX_FMT_YUV444P10;
816
2/2
✓ Branch 0 taken 164 times.
✓ Branch 1 taken 157 times.
321 } else if (CHROMA422(h))
817 164 *fmt++ = AV_PIX_FMT_YUV422P10;
818 else {
819 #if CONFIG_H264_VAAPI_HWACCEL
820 // Just add as candidate. Whether VAProfileH264High10 usable or
821 // not is decided by vaapi_decode_make_config() defined in FFmpeg
822 // and vaQueryCodingProfile() defined in libva.
823 157 *fmt++ = AV_PIX_FMT_VAAPI;
824 #endif
825 157 *fmt++ = AV_PIX_FMT_YUV420P10;
826 }
827 372 break;
828 case 12:
829 #if CONFIG_H264_VULKAN_HWACCEL
830 *fmt++ = AV_PIX_FMT_VULKAN;
831 #endif
832 if (CHROMA444(h)) {
833 if (h->avctx->colorspace == AVCOL_SPC_RGB) {
834 *fmt++ = AV_PIX_FMT_GBRP12;
835 } else
836 *fmt++ = AV_PIX_FMT_YUV444P12;
837 } else if (CHROMA422(h))
838 *fmt++ = AV_PIX_FMT_YUV422P12;
839 else
840 *fmt++ = AV_PIX_FMT_YUV420P12;
841 break;
842 case 14:
843 if (CHROMA444(h)) {
844 if (h->avctx->colorspace == AVCOL_SPC_RGB) {
845 *fmt++ = AV_PIX_FMT_GBRP14;
846 } else
847 *fmt++ = AV_PIX_FMT_YUV444P14;
848 } else if (CHROMA422(h))
849 *fmt++ = AV_PIX_FMT_YUV422P14;
850 else
851 *fmt++ = AV_PIX_FMT_YUV420P14;
852 break;
853 28019 case 8:
854 #if CONFIG_H264_VDPAU_HWACCEL
855 28019 *fmt++ = AV_PIX_FMT_VDPAU;
856 #endif
857 #if CONFIG_H264_VULKAN_HWACCEL
858 *fmt++ = AV_PIX_FMT_VULKAN;
859 #endif
860 #if CONFIG_H264_NVDEC_HWACCEL
861 *fmt++ = AV_PIX_FMT_CUDA;
862 #endif
863 #if CONFIG_H264_VIDEOTOOLBOX_HWACCEL
864 if (h->avctx->colorspace != AVCOL_SPC_RGB)
865 *fmt++ = AV_PIX_FMT_VIDEOTOOLBOX;
866 #endif
867
2/2
✓ Branch 0 taken 484 times.
✓ Branch 1 taken 27535 times.
28019 if (CHROMA444(h)) {
868
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 484 times.
484 if (h->avctx->colorspace == AVCOL_SPC_RGB)
869 *fmt++ = AV_PIX_FMT_GBRP;
870
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 482 times.
484 else if (h->avctx->color_range == AVCOL_RANGE_JPEG)
871 2 *fmt++ = AV_PIX_FMT_YUVJ444P;
872 else
873 482 *fmt++ = AV_PIX_FMT_YUV444P;
874
2/2
✓ Branch 0 taken 22 times.
✓ Branch 1 taken 27513 times.
27535 } else if (CHROMA422(h)) {
875
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 22 times.
22 if (h->avctx->color_range == AVCOL_RANGE_JPEG)
876 *fmt++ = AV_PIX_FMT_YUVJ422P;
877 else
878 22 *fmt++ = AV_PIX_FMT_YUV422P;
879 } else {
880 #if CONFIG_H264_DXVA2_HWACCEL
881 *fmt++ = AV_PIX_FMT_DXVA2_VLD;
882 #endif
883 #if CONFIG_H264_D3D11VA_HWACCEL
884 *fmt++ = AV_PIX_FMT_D3D11VA_VLD;
885 *fmt++ = AV_PIX_FMT_D3D11;
886 #endif
887 #if CONFIG_H264_D3D12VA_HWACCEL
888 *fmt++ = AV_PIX_FMT_D3D12;
889 #endif
890 #if CONFIG_H264_VAAPI_HWACCEL
891 27513 *fmt++ = AV_PIX_FMT_VAAPI;
892 #endif
893
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 27510 times.
27513 if (h->avctx->color_range == AVCOL_RANGE_JPEG)
894 3 *fmt++ = AV_PIX_FMT_YUVJ420P;
895 else
896 27510 *fmt++ = AV_PIX_FMT_YUV420P;
897 }
898 28019 break;
899 default:
900 av_log(h->avctx, AV_LOG_ERROR,
901 "Unsupported bit depth %d\n", h->ps.sps->bit_depth_luma);
902 return AVERROR_INVALIDDATA;
903 }
904
905 28546 *fmt = AV_PIX_FMT_NONE;
906
907
2/2
✓ Branch 0 taken 84235 times.
✓ Branch 1 taken 588 times.
84823 for (int i = 0; pix_fmts[i] != AV_PIX_FMT_NONE; i++)
908
4/4
✓ Branch 0 taken 28214 times.
✓ Branch 1 taken 56021 times.
✓ Branch 2 taken 27958 times.
✓ Branch 3 taken 256 times.
84235 if (pix_fmts[i] == h->avctx->pix_fmt && !force_callback)
909 27958 return pix_fmts[i];
910 588 return ff_get_format(h->avctx, pix_fmts);
911 }
912
913 /* export coded and cropped frame dimensions to AVCodecContext */
914 28287 static void init_dimensions(H264Context *h)
915 {
916 28287 const SPS *sps = h->ps.sps;
917 28287 int cr = sps->crop_right;
918 28287 int cl = sps->crop_left;
919 28287 int ct = sps->crop_top;
920 28287 int cb = sps->crop_bottom;
921 28287 int width = h->width - (cr + cl);
922 28287 int height = h->height - (ct + cb);
923
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 28287 times.
28287 av_assert0(sps->crop_right + sps->crop_left < (unsigned)h->width);
924
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 28287 times.
28287 av_assert0(sps->crop_top + sps->crop_bottom < (unsigned)h->height);
925
926 /* handle container cropping */
927
3/4
✓ Branch 0 taken 26046 times.
✓ Branch 1 taken 2241 times.
✓ Branch 2 taken 26046 times.
✗ Branch 3 not taken.
28287 if (h->width_from_caller > 0 && h->height_from_caller > 0 &&
928
3/4
✓ Branch 0 taken 26045 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 26045 times.
✗ Branch 3 not taken.
26046 !sps->crop_top && !sps->crop_left &&
929
2/2
✓ Branch 0 taken 26036 times.
✓ Branch 1 taken 9 times.
26045 FFALIGN(h->width_from_caller, 16) == FFALIGN(width, 16) &&
930
1/2
✓ Branch 0 taken 26036 times.
✗ Branch 1 not taken.
26036 FFALIGN(h->height_from_caller, 16) == FFALIGN(height, 16) &&
931
2/2
✓ Branch 0 taken 26035 times.
✓ Branch 1 taken 1 times.
26036 h->width_from_caller <= width &&
932
2/2
✓ Branch 0 taken 26033 times.
✓ Branch 1 taken 2 times.
26035 h->height_from_caller <= height) {
933 26033 width = h->width_from_caller;
934 26033 height = h->height_from_caller;
935 26033 cl = 0;
936 26033 ct = 0;
937 26033 cr = h->width - width;
938 26033 cb = h->height - height;
939 } else {
940 2254 h->width_from_caller = 0;
941 2254 h->height_from_caller = 0;
942 }
943
944 28287 h->avctx->coded_width = h->width;
945 28287 h->avctx->coded_height = h->height;
946 28287 h->avctx->width = width;
947 28287 h->avctx->height = height;
948 28287 h->crop_right = cr;
949 28287 h->crop_left = cl;
950 28287 h->crop_top = ct;
951 28287 h->crop_bottom = cb;
952 28287 }
953
954 601 static int h264_slice_header_init(H264Context *h)
955 {
956 601 const SPS *sps = h->ps.sps;
957 int i, ret;
958
959
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 601 times.
601 if (!sps) {
960 ret = AVERROR_INVALIDDATA;
961 goto fail;
962 }
963
964 601 ff_set_sar(h->avctx, sps->vui.sar);
965 601 av_pix_fmt_get_chroma_sub_sample(h->avctx->pix_fmt,
966 &h->chroma_x_shift, &h->chroma_y_shift);
967
968
2/2
✓ Branch 0 taken 183 times.
✓ Branch 1 taken 418 times.
601 if (sps->timing_info_present_flag) {
969 183 int64_t den = sps->time_scale;
970
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 183 times.
183 if (h->x264_build < 44U)
971 den *= 2;
972 183 av_reduce(&h->avctx->framerate.den, &h->avctx->framerate.num,
973 183 sps->num_units_in_tick * 2, den, 1 << 30);
974 }
975
976 601 ff_h264_free_tables(h);
977
978 601 h->first_field = 0;
979 601 h->prev_interlaced_frame = 1;
980
981 601 init_scan_tables(h);
982 601 ret = ff_h264_alloc_tables(h);
983
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 601 times.
601 if (ret < 0) {
984 av_log(h->avctx, AV_LOG_ERROR, "Could not allocate memory\n");
985 goto fail;
986 }
987
988
2/4
✓ Branch 0 taken 601 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 601 times.
✗ Branch 3 not taken.
601 if (sps->bit_depth_luma < 8 || sps->bit_depth_luma > 14 ||
989
2/4
✓ Branch 0 taken 601 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 601 times.
601 sps->bit_depth_luma == 11 || sps->bit_depth_luma == 13
990 ) {
991 av_log(h->avctx, AV_LOG_ERROR, "Unsupported bit depth %d\n",
992 sps->bit_depth_luma);
993 ret = AVERROR_INVALIDDATA;
994 goto fail;
995 }
996
997 601 h->cur_bit_depth_luma =
998 601 h->avctx->bits_per_raw_sample = sps->bit_depth_luma;
999 601 h->cur_chroma_format_idc = sps->chroma_format_idc;
1000 601 h->pixel_shift = sps->bit_depth_luma > 8;
1001 601 h->chroma_format_idc = sps->chroma_format_idc;
1002 601 h->bit_depth_luma = sps->bit_depth_luma;
1003
1004 601 ff_h264dsp_init(&h->h264dsp, sps->bit_depth_luma,
1005 601 sps->chroma_format_idc);
1006 601 ff_h264chroma_init(&h->h264chroma, sps->bit_depth_chroma);
1007 601 ff_h264qpel_init(&h->h264qpel, sps->bit_depth_luma);
1008 601 ff_h264_pred_init(&h->hpc, AV_CODEC_ID_H264, sps->bit_depth_luma,
1009 601 sps->chroma_format_idc);
1010 601 ff_videodsp_init(&h->vdsp, sps->bit_depth_luma);
1011
1012
2/2
✓ Branch 0 taken 600 times.
✓ Branch 1 taken 1 times.
601 if (!HAVE_THREADS || !(h->avctx->active_thread_type & FF_THREAD_SLICE)) {
1013 600 ff_h264_slice_context_init(h, &h->slice_ctx[0]);
1014 } else {
1015
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 times.
3 for (i = 0; i < h->nb_slice_ctx; i++) {
1016 2 H264SliceContext *sl = &h->slice_ctx[i];
1017
1018 2 sl->h264 = h;
1019 2 sl->intra4x4_pred_mode = h->intra4x4_pred_mode + i * 8 * 2 * h->mb_stride;
1020 2 sl->mvd_table[0] = h->mvd_table[0] + i * 8 * 2 * h->mb_stride;
1021 2 sl->mvd_table[1] = h->mvd_table[1] + i * 8 * 2 * h->mb_stride;
1022
1023 2 ff_h264_slice_context_init(h, sl);
1024 }
1025 }
1026
1027 601 h->context_initialized = 1;
1028
1029 601 return 0;
1030 fail:
1031 ff_h264_free_tables(h);
1032 h->context_initialized = 0;
1033 return ret;
1034 }
1035
1036 55922 static enum AVPixelFormat non_j_pixfmt(enum AVPixelFormat a)
1037 {
1038
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 55920 times.
55922 switch (a) {
1039 2 case AV_PIX_FMT_YUVJ420P: return AV_PIX_FMT_YUV420P;
1040 case AV_PIX_FMT_YUVJ422P: return AV_PIX_FMT_YUV422P;
1041 case AV_PIX_FMT_YUVJ444P: return AV_PIX_FMT_YUV444P;
1042 55920 default:
1043 55920 return a;
1044 }
1045 }
1046
1047 28287 static int h264_init_ps(H264Context *h, const H264SliceContext *sl, int first_slice)
1048 {
1049 const SPS *sps;
1050 28287 int needs_reinit = 0, must_reinit, ret;
1051
1052
2/2
✓ Branch 0 taken 28093 times.
✓ Branch 1 taken 194 times.
28287 if (first_slice)
1053 28093 ff_refstruct_replace(&h->ps.pps, h->ps.pps_list[sl->pps_id]);
1054
1055
2/2
✓ Branch 0 taken 587 times.
✓ Branch 1 taken 27700 times.
28287 if (h->ps.sps != h->ps.pps->sps) {
1056 587 h->ps.sps = h->ps.pps->sps;
1057
1058
2/2
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 580 times.
587 if (h->mb_width != h->ps.sps->mb_width ||
1059
1/2
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
7 h->mb_height != h->ps.sps->mb_height ||
1060
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 1 times.
7 h->cur_bit_depth_luma != h->ps.sps->bit_depth_luma ||
1061
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 5 times.
6 h->cur_chroma_format_idc != h->ps.sps->chroma_format_idc
1062 )
1063 582 needs_reinit = 1;
1064
1065
2/2
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 576 times.
587 if (h->bit_depth_luma != h->ps.sps->bit_depth_luma ||
1066
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 10 times.
11 h->chroma_format_idc != h->ps.sps->chroma_format_idc)
1067 577 needs_reinit = 1;
1068 }
1069 28287 sps = h->ps.sps;
1070
1071
2/2
✓ Branch 0 taken 27710 times.
✓ Branch 1 taken 577 times.
55997 must_reinit = (h->context_initialized &&
1072
2/2
✓ Branch 0 taken 27704 times.
✓ Branch 1 taken 6 times.
27710 ( 16*sps->mb_width != h->avctx->coded_width
1073
1/2
✓ Branch 0 taken 27704 times.
✗ Branch 1 not taken.
27704 || 16*sps->mb_height != h->avctx->coded_height
1074
2/2
✓ Branch 0 taken 27703 times.
✓ Branch 1 taken 1 times.
27704 || h->cur_bit_depth_luma != sps->bit_depth_luma
1075
2/2
✓ Branch 0 taken 27702 times.
✓ Branch 1 taken 1 times.
27703 || h->cur_chroma_format_idc != sps->chroma_format_idc
1076
1/2
✓ Branch 0 taken 27702 times.
✗ Branch 1 not taken.
27702 || h->mb_width != sps->mb_width
1077
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 27702 times.
27702 || h->mb_height != sps->mb_height
1078 ));
1079
2/2
✓ Branch 0 taken 27961 times.
✓ Branch 1 taken 326 times.
28287 if (h->avctx->pix_fmt == AV_PIX_FMT_NONE
1080
2/2
✓ Branch 3 taken 3 times.
✓ Branch 4 taken 27958 times.
27961 || (non_j_pixfmt(h->avctx->pix_fmt) != non_j_pixfmt(get_pixel_format(h, 0))))
1081 329 must_reinit = 1;
1082
1083
4/4
✓ Branch 0 taken 28093 times.
✓ Branch 1 taken 194 times.
✓ Branch 3 taken 71 times.
✓ Branch 4 taken 28022 times.
28287 if (first_slice && av_cmp_q(sps->vui.sar, h->avctx->sample_aspect_ratio))
1084 71 must_reinit = 1;
1085
1086
1/2
✓ Branch 0 taken 28287 times.
✗ Branch 1 not taken.
28287 if (!h->setup_finished) {
1087 28287 h->avctx->profile = ff_h264_get_profile(sps);
1088 28287 h->avctx->level = sps->level_idc;
1089 28287 h->avctx->refs = sps->ref_frame_count;
1090
1091 28287 h->mb_width = sps->mb_width;
1092 28287 h->mb_height = sps->mb_height;
1093 28287 h->mb_num = h->mb_width * h->mb_height;
1094 28287 h->mb_stride = h->mb_width + 1;
1095
1096 28287 h->b_stride = h->mb_width * 4;
1097
1098 28287 h->chroma_y_shift = sps->chroma_format_idc <= 1; // 400 uses yuv420p
1099
1100 28287 h->width = 16 * h->mb_width;
1101 28287 h->height = 16 * h->mb_height;
1102
1103 28287 init_dimensions(h);
1104
1105
2/2
✓ Branch 0 taken 1287 times.
✓ Branch 1 taken 27000 times.
28287 if (sps->vui.video_signal_type_present_flag) {
1106 2574 h->avctx->color_range = sps->vui.video_full_range_flag > 0 ? AVCOL_RANGE_JPEG
1107
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1285 times.
1287 : AVCOL_RANGE_MPEG;
1108
2/2
✓ Branch 0 taken 1189 times.
✓ Branch 1 taken 98 times.
1287 if (sps->vui.colour_description_present_flag) {
1109
2/2
✓ Branch 0 taken 19 times.
✓ Branch 1 taken 1170 times.
1189 if (h->avctx->colorspace != sps->vui.matrix_coeffs)
1110 19 needs_reinit = 1;
1111 1189 h->avctx->color_primaries = sps->vui.colour_primaries;
1112 1189 h->avctx->color_trc = sps->vui.transfer_characteristics;
1113 1189 h->avctx->colorspace = sps->vui.matrix_coeffs;
1114 }
1115 }
1116
1117
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 28287 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
28287 if (h->sei.common.alternative_transfer.present &&
1118 av_color_transfer_name(h->sei.common.alternative_transfer.preferred_transfer_characteristics) &&
1119 h->sei.common.alternative_transfer.preferred_transfer_characteristics != AVCOL_TRC_UNSPECIFIED) {
1120 h->avctx->color_trc = h->sei.common.alternative_transfer.preferred_transfer_characteristics;
1121 }
1122 }
1123 28287 h->avctx->chroma_sample_location = sps->vui.chroma_location;
1124
1125
5/6
✓ Branch 0 taken 27710 times.
✓ Branch 1 taken 577 times.
✓ Branch 2 taken 27702 times.
✓ Branch 3 taken 8 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 27702 times.
28287 if (!h->context_initialized || must_reinit || needs_reinit) {
1126 585 int flush_changes = h->context_initialized;
1127 585 h->context_initialized = 0;
1128
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 585 times.
585 if (sl != h->slice_ctx) {
1129 av_log(h->avctx, AV_LOG_ERROR,
1130 "changing width %d -> %d / height %d -> %d on "
1131 "slice %d\n",
1132 h->width, h->avctx->coded_width,
1133 h->height, h->avctx->coded_height,
1134 h->current_slice + 1);
1135 return AVERROR_INVALIDDATA;
1136 }
1137
1138 av_assert1(first_slice);
1139
1140
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 577 times.
585 if (flush_changes)
1141 8 ff_h264_flush_change(h);
1142
1143
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 585 times.
585 if ((ret = get_pixel_format(h, 1)) < 0)
1144 return ret;
1145 585 h->avctx->pix_fmt = ret;
1146
1147 585 av_log(h->avctx, AV_LOG_VERBOSE, "Reinit context to %dx%d, "
1148 585 "pix_fmt: %s\n", h->width, h->height, av_get_pix_fmt_name(h->avctx->pix_fmt));
1149
1150
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 585 times.
585 if ((ret = h264_slice_header_init(h)) < 0) {
1151 av_log(h->avctx, AV_LOG_ERROR,
1152 "h264_slice_header_init() failed\n");
1153 return ret;
1154 }
1155 }
1156
1157 28287 return 0;
1158 }
1159
1160 24920 static int h264_export_frame_props(H264Context *h)
1161 {
1162 24920 const SPS *sps = h->ps.sps;
1163 24920 H264Picture *cur = h->cur_pic_ptr;
1164 24920 AVFrame *out = cur->f;
1165 24920 int interlaced_frame = 0, top_field_first = 0;
1166 int ret;
1167
1168 24920 out->flags &= ~AV_FRAME_FLAG_INTERLACED;
1169 24920 out->repeat_pict = 0;
1170
1171 /* Signal interlacing information externally. */
1172 /* Prioritize picture timing SEI information over used
1173 * decoding process if it exists. */
1174
2/2
✓ Branch 0 taken 1426 times.
✓ Branch 1 taken 23494 times.
24920 if (h->sei.picture_timing.present) {
1175 1426 int ret = ff_h264_sei_process_picture_timing(&h->sei.picture_timing, sps,
1176 1426 h->avctx);
1177
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1426 times.
1426 if (ret < 0) {
1178 av_log(h->avctx, AV_LOG_ERROR, "Error processing a picture timing SEI\n");
1179 if (h->avctx->err_recognition & AV_EF_EXPLODE)
1180 return ret;
1181 h->sei.picture_timing.present = 0;
1182 }
1183 }
1184
1185
4/4
✓ Branch 0 taken 1504 times.
✓ Branch 1 taken 23416 times.
✓ Branch 2 taken 1418 times.
✓ Branch 3 taken 86 times.
26338 if (sps->pic_struct_present_flag && h->sei.picture_timing.present) {
1186 1418 const H264SEIPictureTiming *pt = &h->sei.picture_timing;
1187
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) {
1188 132 case H264_SEI_PIC_STRUCT_FRAME:
1189 132 break;
1190 377 case H264_SEI_PIC_STRUCT_TOP_FIELD:
1191 case H264_SEI_PIC_STRUCT_BOTTOM_FIELD:
1192 377 interlaced_frame = 1;
1193 377 break;
1194 742 case H264_SEI_PIC_STRUCT_TOP_BOTTOM:
1195 case H264_SEI_PIC_STRUCT_BOTTOM_TOP:
1196
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))
1197 738 interlaced_frame = 1;
1198 else
1199 // try to flag soft telecine progressive
1200 4 interlaced_frame = !!h->prev_interlaced_frame;
1201 742 break;
1202 167 case H264_SEI_PIC_STRUCT_TOP_BOTTOM_TOP:
1203 case H264_SEI_PIC_STRUCT_BOTTOM_TOP_BOTTOM:
1204 /* Signal the possibility of telecined film externally
1205 * (pic_struct 5,6). From these hints, let the applications
1206 * decide if they apply deinterlacing. */
1207 167 out->repeat_pict = 1;
1208 167 break;
1209 case H264_SEI_PIC_STRUCT_FRAME_DOUBLING:
1210 out->repeat_pict = 2;
1211 break;
1212 case H264_SEI_PIC_STRUCT_FRAME_TRIPLING:
1213 out->repeat_pict = 4;
1214 break;
1215 }
1216
1217
2/2
✓ Branch 0 taken 652 times.
✓ Branch 1 taken 766 times.
1418 if ((pt->ct_type & 3) &&
1218
2/2
✓ Branch 0 taken 488 times.
✓ Branch 1 taken 164 times.
652 pt->pic_struct <= H264_SEI_PIC_STRUCT_BOTTOM_TOP)
1219 488 interlaced_frame = ((pt->ct_type & (1 << 1)) != 0);
1220 } else {
1221 /* Derive interlacing flag from used decoding process. */
1222
4/4
✓ Branch 0 taken 21915 times.
✓ Branch 1 taken 1587 times.
✓ Branch 2 taken 2821 times.
✓ Branch 3 taken 19094 times.
23502 interlaced_frame = !!FIELD_OR_MBAFF_PICTURE(h);
1223 }
1224 24920 h->prev_interlaced_frame = interlaced_frame;
1225
1226
2/2
✓ Branch 0 taken 3336 times.
✓ Branch 1 taken 21584 times.
24920 if (cur->field_poc[0] != cur->field_poc[1]) {
1227 /* Derive top_field_first from field pocs. */
1228 3336 top_field_first = (cur->field_poc[0] < cur->field_poc[1]);
1229 } else {
1230
4/4
✓ Branch 0 taken 225 times.
✓ Branch 1 taken 21359 times.
✓ Branch 2 taken 139 times.
✓ Branch 3 taken 86 times.
21584 if (sps->pic_struct_present_flag && h->sei.picture_timing.present) {
1231 /* Use picture timing SEI information. Even if it is a
1232 * information of a past frame, better than nothing. */
1233
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 ||
1234
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)
1235 3 top_field_first = 1;
1236
2/2
✓ Branch 0 taken 2554 times.
✓ Branch 1 taken 18891 times.
21445 } else if (interlaced_frame) {
1237 /* Default to top field first when pic_struct_present_flag
1238 * is not set but interlaced frame detected */
1239 2554 top_field_first = 1;
1240 } // else
1241 /* Most likely progressive */
1242 }
1243
1244 24920 out->flags |= (AV_FRAME_FLAG_INTERLACED * interlaced_frame) |
1245 24920 (AV_FRAME_FLAG_TOP_FIELD_FIRST * top_field_first);
1246
1247 24920 ret = ff_h2645_sei_to_frame(out, &h->sei.common, AV_CODEC_ID_H264, h->avctx,
1248 24920 &sps->vui, sps->bit_depth_luma, sps->bit_depth_chroma,
1249 24920 cur->poc + (unsigned)(h->poc_offset << 5));
1250
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 24920 times.
24920 if (ret < 0)
1251 return ret;
1252
1253
2/2
✓ Branch 0 taken 652 times.
✓ Branch 1 taken 24268 times.
24920 if (h->sei.picture_timing.timecode_cnt > 0) {
1254 uint32_t *tc_sd;
1255 char tcbuf[AV_TIMECODE_STR_SIZE];
1256 AVFrameSideData *tcside;
1257 652 ret = ff_frame_new_side_data(h->avctx, out, AV_FRAME_DATA_S12M_TIMECODE,
1258 sizeof(uint32_t)*4, &tcside);
1259
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 652 times.
652 if (ret < 0)
1260 return ret;
1261
1262
1/2
✓ Branch 0 taken 652 times.
✗ Branch 1 not taken.
652 if (tcside) {
1263 652 tc_sd = (uint32_t*)tcside->data;
1264 652 tc_sd[0] = h->sei.picture_timing.timecode_cnt;
1265
1266
2/2
✓ Branch 0 taken 953 times.
✓ Branch 1 taken 652 times.
1605 for (int i = 0; i < tc_sd[0]; i++) {
1267 953 int drop = h->sei.picture_timing.timecode[i].dropframe;
1268 953 int hh = h->sei.picture_timing.timecode[i].hours;
1269 953 int mm = h->sei.picture_timing.timecode[i].minutes;
1270 953 int ss = h->sei.picture_timing.timecode[i].seconds;
1271 953 int ff = h->sei.picture_timing.timecode[i].frame;
1272
1273 953 tc_sd[i + 1] = av_timecode_get_smpte(h->avctx->framerate, drop, hh, mm, ss, ff);
1274 953 av_timecode_make_smpte_tc_string2(tcbuf, h->avctx->framerate, tc_sd[i + 1], 0, 0);
1275 953 av_dict_set(&out->metadata, "timecode", tcbuf, 0);
1276 }
1277 }
1278 652 h->sei.picture_timing.timecode_cnt = 0;
1279 }
1280
1281 24920 return 0;
1282 }
1283
1284 24920 static int h264_select_output_frame(H264Context *h)
1285 {
1286 24920 const SPS *sps = h->ps.sps;
1287 24920 H264Picture *out = h->cur_pic_ptr;
1288 24920 H264Picture *cur = h->cur_pic_ptr;
1289 int i, pics, out_of_order, out_idx;
1290
1291 24920 cur->mmco_reset = h->mmco_reset;
1292 24920 h->mmco_reset = 0;
1293
1294
2/2
✓ Branch 0 taken 20019 times.
✓ Branch 1 taken 4901 times.
24920 if (sps->bitstream_restriction_flag ||
1295
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 20019 times.
20019 h->avctx->strict_std_compliance >= FF_COMPLIANCE_STRICT) {
1296 4901 h->avctx->has_b_frames = FFMAX(h->avctx->has_b_frames, sps->num_reorder_frames);
1297 }
1298
1299 408643 for (i = 0; 1; i++) {
1300
4/4
✓ Branch 0 taken 392729 times.
✓ Branch 1 taken 15914 times.
✓ Branch 2 taken 9006 times.
✓ Branch 3 taken 383723 times.
408643 if(i == H264_MAX_DPB_FRAMES || cur->poc < h->last_pocs[i]){
1301
1/2
✓ Branch 0 taken 24920 times.
✗ Branch 1 not taken.
24920 if(i)
1302 24920 h->last_pocs[i-1] = cur->poc;
1303 24920 break;
1304
2/2
✓ Branch 0 taken 358803 times.
✓ Branch 1 taken 24920 times.
383723 } else if(i) {
1305 358803 h->last_pocs[i-1]= h->last_pocs[i];
1306 }
1307 }
1308 24920 out_of_order = H264_MAX_DPB_FRAMES - i;
1309
2/2
✓ Branch 0 taken 16798 times.
✓ Branch 1 taken 8122 times.
24920 if( cur->f->pict_type == AV_PICTURE_TYPE_B
1310
4/4
✓ Branch 0 taken 15677 times.
✓ Branch 1 taken 1121 times.
✓ Branch 2 taken 5062 times.
✓ Branch 3 taken 10615 times.
16798 || (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))
1311 13184 out_of_order = FFMAX(out_of_order, 1);
1312
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 24920 times.
24920 if (out_of_order == H264_MAX_DPB_FRAMES) {
1313 av_log(h->avctx, AV_LOG_VERBOSE, "Invalid POC %d<%d\n", cur->poc, h->last_pocs[0]);
1314 for (i = 1; i < H264_MAX_DPB_FRAMES; i++)
1315 h->last_pocs[i] = INT_MIN;
1316 h->last_pocs[0] = cur->poc;
1317 cur->mmco_reset = 1;
1318
3/4
✓ Branch 0 taken 140 times.
✓ Branch 1 taken 24780 times.
✓ Branch 2 taken 140 times.
✗ Branch 3 not taken.
24920 } else if(h->avctx->has_b_frames < out_of_order && !sps->bitstream_restriction_flag){
1319
2/2
✓ Branch 0 taken 33 times.
✓ Branch 1 taken 107 times.
140 int loglevel = h->avctx->frame_num > 1 ? AV_LOG_WARNING : AV_LOG_VERBOSE;
1320 140 av_log(h->avctx, loglevel, "Increasing reorder buffer to %d\n", out_of_order);
1321 140 h->avctx->has_b_frames = out_of_order;
1322 }
1323
1324 24920 pics = 0;
1325
2/2
✓ Branch 0 taken 30536 times.
✓ Branch 1 taken 24920 times.
55456 while (h->delayed_pic[pics])
1326 30536 pics++;
1327
1328
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 24920 times.
24920 av_assert0(pics <= H264_MAX_DPB_FRAMES);
1329
1330 24920 h->delayed_pic[pics++] = cur;
1331
2/2
✓ Branch 0 taken 23445 times.
✓ Branch 1 taken 1475 times.
24920 if (cur->reference == 0)
1332 23445 cur->reference = DELAYED_PIC_REF;
1333
1334 24920 out = h->delayed_pic[0];
1335 24920 out_idx = 0;
1336 24920 for (i = 1; h->delayed_pic[i] &&
1337
4/4
✓ Branch 0 taken 28085 times.
✓ Branch 1 taken 24453 times.
✓ Branch 2 taken 27705 times.
✓ Branch 3 taken 380 times.
52538 !(h->delayed_pic[i]->f->flags & AV_FRAME_FLAG_KEY) &&
1338
2/2
✓ Branch 0 taken 27618 times.
✓ Branch 1 taken 87 times.
27705 !h->delayed_pic[i]->mmco_reset;
1339 27618 i++)
1340
2/2
✓ Branch 0 taken 10306 times.
✓ Branch 1 taken 17312 times.
27618 if (h->delayed_pic[i]->poc < out->poc) {
1341 10306 out = h->delayed_pic[i];
1342 10306 out_idx = i;
1343 }
1344
2/2
✓ Branch 0 taken 10258 times.
✓ Branch 1 taken 14662 times.
24920 if (h->avctx->has_b_frames == 0 &&
1345
4/4
✓ Branch 0 taken 9466 times.
✓ Branch 1 taken 792 times.
✓ Branch 2 taken 12 times.
✓ Branch 3 taken 9454 times.
10258 ((h->delayed_pic[0]->f->flags & AV_FRAME_FLAG_KEY) || h->delayed_pic[0]->mmco_reset))
1346 804 h->next_outputed_poc = INT_MIN;
1347 24920 out_of_order = out->poc < h->next_outputed_poc;
1348
1349
4/4
✓ Branch 0 taken 24807 times.
✓ Branch 1 taken 113 times.
✓ Branch 2 taken 24282 times.
✓ Branch 3 taken 525 times.
24920 if (out_of_order || pics > h->avctx->has_b_frames) {
1350 24395 out->reference &= ~DELAYED_PIC_REF;
1351
2/2
✓ Branch 0 taken 39538 times.
✓ Branch 1 taken 24395 times.
63933 for (i = out_idx; h->delayed_pic[i]; i++)
1352 39538 h->delayed_pic[i] = h->delayed_pic[i + 1];
1353 }
1354
4/4
✓ Branch 0 taken 24807 times.
✓ Branch 1 taken 113 times.
✓ Branch 2 taken 24282 times.
✓ Branch 3 taken 525 times.
24920 if (!out_of_order && pics > h->avctx->has_b_frames) {
1355 24282 h->next_output_pic = out;
1356
8/8
✓ Branch 0 taken 15496 times.
✓ Branch 1 taken 8786 times.
✓ Branch 2 taken 5239 times.
✓ Branch 3 taken 10257 times.
✓ Branch 4 taken 5155 times.
✓ Branch 5 taken 84 times.
✓ Branch 6 taken 21 times.
✓ Branch 7 taken 5134 times.
24282 if (out_idx == 0 && h->delayed_pic[0] && ((h->delayed_pic[0]->f->flags & AV_FRAME_FLAG_KEY) || h->delayed_pic[0]->mmco_reset)) {
1357 105 h->next_outputed_poc = INT_MIN;
1358 } else
1359 24177 h->next_outputed_poc = out->poc;
1360
1361 // We have reached an recovery point and all frames after it in
1362 // display order are "recovered".
1363 24282 h->frame_recovered |= out->recovered;
1364
1365 24282 out->recovered |= h->frame_recovered & FRAME_RECOVERED_SEI;
1366
1367
2/2
✓ Branch 0 taken 249 times.
✓ Branch 1 taken 24033 times.
24282 if (!out->recovered) {
1368
1/2
✓ Branch 0 taken 249 times.
✗ Branch 1 not taken.
249 if (!(h->avctx->flags & AV_CODEC_FLAG_OUTPUT_CORRUPT) &&
1369
1/2
✓ Branch 0 taken 249 times.
✗ Branch 1 not taken.
249 !(h->avctx->flags2 & AV_CODEC_FLAG2_SHOW_ALL)) {
1370 249 h->next_output_pic = NULL;
1371 } else {
1372 out->f->flags |= AV_FRAME_FLAG_CORRUPT;
1373 }
1374 }
1375 } else {
1376
2/2
✓ Branch 0 taken 113 times.
✓ Branch 1 taken 525 times.
638 av_log(h->avctx, AV_LOG_DEBUG, "no picture %s\n", out_of_order ? "ooo" : "");
1377 }
1378
1379 24920 return 0;
1380 }
1381
1382 /* This function is called right after decoding the slice header for a first
1383 * slice in a field (or a frame). It decides whether we are decoding a new frame
1384 * or a second field in a pair and does the necessary setup.
1385 */
1386 28287 static int h264_field_start(H264Context *h, const H264SliceContext *sl,
1387 const H2645NAL *nal, int first_slice)
1388 {
1389 int i;
1390 const SPS *sps;
1391
1392 int last_pic_structure, last_pic_droppable, ret;
1393
1394 28287 ret = h264_init_ps(h, sl, first_slice);
1395
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 28287 times.
28287 if (ret < 0)
1396 return ret;
1397
1398 28287 sps = h->ps.sps;
1399
1400
3/4
✓ Branch 0 taken 28287 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 5234 times.
✓ Branch 3 taken 23053 times.
28287 if (sps && sps->bitstream_restriction_flag &&
1401
2/2
✓ Branch 0 taken 29 times.
✓ Branch 1 taken 5205 times.
5234 h->avctx->has_b_frames < sps->num_reorder_frames) {
1402 29 h->avctx->has_b_frames = sps->num_reorder_frames;
1403 }
1404
1405 28287 last_pic_droppable = h->droppable;
1406 28287 last_pic_structure = h->picture_structure;
1407 28287 h->droppable = (nal->ref_idc == 0);
1408 28287 h->picture_structure = sl->picture_structure;
1409
1410 28287 h->poc.frame_num = sl->frame_num;
1411 28287 h->poc.poc_lsb = sl->poc_lsb;
1412 28287 h->poc.delta_poc_bottom = sl->delta_poc_bottom;
1413 28287 h->poc.delta_poc[0] = sl->delta_poc[0];
1414 28287 h->poc.delta_poc[1] = sl->delta_poc[1];
1415
1416
2/2
✓ Branch 0 taken 1077 times.
✓ Branch 1 taken 27210 times.
28287 if (nal->type == H264_NAL_IDR_SLICE)
1417 1077 h->poc_offset = sl->idr_pic_id;
1418
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 27210 times.
27210 else if (h->picture_intra_only)
1419 h->poc_offset = 0;
1420
1421 /* Shorten frame num gaps so we don't have to allocate reference
1422 * frames just to throw them away */
1423
2/2
✓ Branch 0 taken 16145 times.
✓ Branch 1 taken 12142 times.
28287 if (h->poc.frame_num != h->poc.prev_frame_num) {
1424 16145 int unwrap_prev_frame_num = h->poc.prev_frame_num;
1425 16145 int max_frame_num = 1 << sps->log2_max_frame_num;
1426
1427
2/2
✓ Branch 0 taken 193 times.
✓ Branch 1 taken 15952 times.
16145 if (unwrap_prev_frame_num > h->poc.frame_num)
1428 193 unwrap_prev_frame_num -= max_frame_num;
1429
1430
2/2
✓ Branch 0 taken 74 times.
✓ Branch 1 taken 16071 times.
16145 if ((h->poc.frame_num - unwrap_prev_frame_num) > sps->ref_frame_count) {
1431 74 unwrap_prev_frame_num = (h->poc.frame_num - sps->ref_frame_count) - 1;
1432
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 72 times.
74 if (unwrap_prev_frame_num < 0)
1433 2 unwrap_prev_frame_num += max_frame_num;
1434
1435 74 h->poc.prev_frame_num = unwrap_prev_frame_num;
1436 }
1437 }
1438
1439 /* See if we have a decoded first field looking for a pair...
1440 * Here, we're using that to see if we should mark previously
1441 * decode frames as "finished".
1442 * We have to do that before the "dummy" in-between frame allocation,
1443 * since that can modify h->cur_pic_ptr. */
1444
2/2
✓ Branch 0 taken 3362 times.
✓ Branch 1 taken 24925 times.
28287 if (h->first_field) {
1445 3362 int last_field = last_pic_structure == PICT_BOTTOM_FIELD;
1446
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3362 times.
3362 av_assert0(h->cur_pic_ptr);
1447
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3362 times.
3362 av_assert0(h->cur_pic_ptr->f->buf[0]);
1448 assert(h->cur_pic_ptr->reference != DELAYED_PIC_REF);
1449
1450 /* Mark old field/frame as completed */
1451
1/2
✓ Branch 0 taken 3362 times.
✗ Branch 1 not taken.
3362 if (h->cur_pic_ptr->tf.owner[last_field] == h->avctx) {
1452 3362 ff_thread_report_progress(&h->cur_pic_ptr->tf, INT_MAX, last_field);
1453 }
1454
1455 /* figure out if we have a complementary field pair */
1456
2/4
✓ Branch 0 taken 3362 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 3362 times.
3362 if (!FIELD_PICTURE(h) || h->picture_structure == last_pic_structure) {
1457 /* Previous field is unmatched. Don't display it, but let it
1458 * remain for reference if marked as such. */
1459 if (last_pic_structure != PICT_FRAME) {
1460 ff_thread_report_progress(&h->cur_pic_ptr->tf, INT_MAX,
1461 last_pic_structure == PICT_TOP_FIELD);
1462 }
1463 } else {
1464
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3362 times.
3362 if (h->cur_pic_ptr->frame_num != h->poc.frame_num) {
1465 /* This and previous field were reference, but had
1466 * different frame_nums. Consider this field first in
1467 * pair. Throw away previous field except for reference
1468 * purposes. */
1469 if (last_pic_structure != PICT_FRAME) {
1470 ff_thread_report_progress(&h->cur_pic_ptr->tf, INT_MAX,
1471 last_pic_structure == PICT_TOP_FIELD);
1472 }
1473 } else {
1474 /* Second field in complementary pair */
1475
3/4
✓ Branch 0 taken 3354 times.
✓ Branch 1 taken 8 times.
✓ Branch 2 taken 8 times.
✗ Branch 3 not taken.
3362 if (!((last_pic_structure == PICT_TOP_FIELD &&
1476
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3354 times.
3354 h->picture_structure == PICT_BOTTOM_FIELD) ||
1477 8 (last_pic_structure == PICT_BOTTOM_FIELD &&
1478
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
8 h->picture_structure == PICT_TOP_FIELD))) {
1479 av_log(h->avctx, AV_LOG_ERROR,
1480 "Invalid field mode combination %d/%d\n",
1481 last_pic_structure, h->picture_structure);
1482 h->picture_structure = last_pic_structure;
1483 h->droppable = last_pic_droppable;
1484 return AVERROR_INVALIDDATA;
1485
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3362 times.
3362 } else if (last_pic_droppable != h->droppable) {
1486 avpriv_request_sample(h->avctx,
1487 "Found reference and non-reference fields in the same frame, which");
1488 h->picture_structure = last_pic_structure;
1489 h->droppable = last_pic_droppable;
1490 return AVERROR_PATCHWELCOME;
1491 }
1492 }
1493 }
1494 }
1495
1496
3/4
✓ Branch 0 taken 16830 times.
✓ Branch 1 taken 12142 times.
✓ Branch 2 taken 16830 times.
✗ Branch 3 not taken.
57259 while (h->poc.frame_num != h->poc.prev_frame_num && !h->first_field &&
1497
2/2
✓ Branch 0 taken 685 times.
✓ Branch 1 taken 16145 times.
16830 h->poc.frame_num != (h->poc.prev_frame_num + 1) % (1 << sps->log2_max_frame_num)) {
1498
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;
1499 685 av_log(h->avctx, AV_LOG_DEBUG, "Frame num gap %d %d\n",
1500 h->poc.frame_num, h->poc.prev_frame_num);
1501
2/2
✓ Branch 0 taken 108 times.
✓ Branch 1 taken 577 times.
685 if (!sps->gaps_in_frame_num_allowed_flag)
1502
2/2
✓ Branch 0 taken 1728 times.
✓ Branch 1 taken 108 times.
1836 for(i=0; i<FF_ARRAY_ELEMS(h->last_pocs); i++)
1503 1728 h->last_pocs[i] = INT_MIN;
1504 685 ret = h264_frame_start(h);
1505
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 685 times.
685 if (ret < 0) {
1506 h->first_field = 0;
1507 return ret;
1508 }
1509
1510 685 h->poc.prev_frame_num++;
1511 685 h->poc.prev_frame_num %= 1 << sps->log2_max_frame_num;
1512 685 h->cur_pic_ptr->frame_num = h->poc.prev_frame_num;
1513 685 h->cur_pic_ptr->invalid_gap = !sps->gaps_in_frame_num_allowed_flag;
1514 685 ff_thread_report_progress(&h->cur_pic_ptr->tf, INT_MAX, 0);
1515 685 ff_thread_report_progress(&h->cur_pic_ptr->tf, INT_MAX, 1);
1516
1517 685 h->explicit_ref_marking = 0;
1518 685 ret = ff_h264_execute_ref_pic_marking(h);
1519
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))
1520 return ret;
1521 /* Error concealment: If a ref is missing, copy the previous ref
1522 * in its place.
1523 * FIXME: Avoiding a memcpy would be nice, but ref handling makes
1524 * many assumptions about there being no actual duplicates.
1525 * FIXME: This does not copy padding for out-of-frame motion
1526 * vectors. Given we are concealing a lost frame, this probably
1527 * is not noticeable by comparison, but it should be fixed. */
1528
1/2
✓ Branch 0 taken 685 times.
✗ Branch 1 not taken.
685 if (h->short_ref_count) {
1529 685 int c[4] = {
1530 685 1<<(h->ps.sps->bit_depth_luma-1),
1531 685 1<<(h->ps.sps->bit_depth_chroma-1),
1532 685 1<<(h->ps.sps->bit_depth_chroma-1),
1533 -1
1534 };
1535
1536
2/2
✓ Branch 0 taken 653 times.
✓ Branch 1 taken 32 times.
685 if (prev &&
1537
1/2
✓ Branch 0 taken 653 times.
✗ Branch 1 not taken.
653 h->short_ref[0]->f->width == prev->f->width &&
1538
1/2
✓ Branch 0 taken 653 times.
✗ Branch 1 not taken.
653 h->short_ref[0]->f->height == prev->f->height &&
1539
1/2
✓ Branch 0 taken 653 times.
✗ Branch 1 not taken.
653 h->short_ref[0]->f->format == prev->f->format) {
1540 653 ff_thread_await_progress(&prev->tf, INT_MAX, 0);
1541
2/2
✓ Branch 0 taken 28 times.
✓ Branch 1 taken 625 times.
653 if (prev->field_picture)
1542 28 ff_thread_await_progress(&prev->tf, INT_MAX, 1);
1543 653 ff_thread_release_ext_buffer(&h->short_ref[0]->tf);
1544 653 h->short_ref[0]->tf.f = h->short_ref[0]->f;
1545 653 ret = ff_thread_ref_frame(&h->short_ref[0]->tf, &prev->tf);
1546
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 653 times.
653 if (ret < 0)
1547 return ret;
1548 653 h->short_ref[0]->poc = prev->poc + 2U;
1549 653 h->short_ref[0]->gray = prev->gray;
1550 653 ff_thread_report_progress(&h->short_ref[0]->tf, INT_MAX, 0);
1551
2/2
✓ Branch 0 taken 28 times.
✓ Branch 1 taken 625 times.
653 if (h->short_ref[0]->field_picture)
1552 28 ff_thread_report_progress(&h->short_ref[0]->tf, INT_MAX, 1);
1553
2/2
✓ Branch 0 taken 24 times.
✓ Branch 1 taken 8 times.
32 } else if (!h->frame_recovered) {
1554
1/2
✓ Branch 0 taken 24 times.
✗ Branch 1 not taken.
24 if (!h->avctx->hwaccel)
1555 24 color_frame(h->short_ref[0]->f, c);
1556 24 h->short_ref[0]->gray = 1;
1557 }
1558 685 h->short_ref[0]->frame_num = h->poc.prev_frame_num;
1559 }
1560 }
1561
1562 /* See if we have a decoded first field looking for a pair...
1563 * We're using that to see whether to continue decoding in that
1564 * frame, or to allocate a new one. */
1565
2/2
✓ Branch 0 taken 3362 times.
✓ Branch 1 taken 24925 times.
28287 if (h->first_field) {
1566
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3362 times.
3362 av_assert0(h->cur_pic_ptr);
1567
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3362 times.
3362 av_assert0(h->cur_pic_ptr->f->buf[0]);
1568 assert(h->cur_pic_ptr->reference != DELAYED_PIC_REF);
1569
1570 /* figure out if we have a complementary field pair */
1571
2/4
✓ Branch 0 taken 3362 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 3362 times.
3362 if (!FIELD_PICTURE(h) || h->picture_structure == last_pic_structure) {
1572 /* Previous field is unmatched. Don't display it, but let it
1573 * remain for reference if marked as such. */
1574 h->missing_fields ++;
1575 h->cur_pic_ptr = NULL;
1576 h->first_field = FIELD_PICTURE(h);
1577 } else {
1578 3362 h->missing_fields = 0;
1579
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3362 times.
3362 if (h->cur_pic_ptr->frame_num != h->poc.frame_num) {
1580 ff_thread_report_progress(&h->cur_pic_ptr->tf, INT_MAX,
1581 h->picture_structure==PICT_BOTTOM_FIELD);
1582 /* This and the previous field had different frame_nums.
1583 * Consider this field first in pair. Throw away previous
1584 * one except for reference purposes. */
1585 h->first_field = 1;
1586 h->cur_pic_ptr = NULL;
1587
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3362 times.
3362 } else if (h->cur_pic_ptr->reference & DELAYED_PIC_REF) {
1588 /* This frame was already output, we cannot draw into it
1589 * anymore.
1590 */
1591 h->first_field = 1;
1592 h->cur_pic_ptr = NULL;
1593 } else {
1594 /* Second field in complementary pair */
1595 3362 h->first_field = 0;
1596 }
1597 }
1598 } else {
1599 /* Frame or first field in a potentially complementary pair */
1600 24925 h->first_field = FIELD_PICTURE(h);
1601 }
1602
1603
4/4
✓ Branch 0 taken 6729 times.
✓ Branch 1 taken 21558 times.
✓ Branch 2 taken 3367 times.
✓ Branch 3 taken 3362 times.
28287 if (!FIELD_PICTURE(h) || h->first_field) {
1604
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 24925 times.
24925 if (h264_frame_start(h) < 0) {
1605 h->first_field = 0;
1606 return AVERROR_INVALIDDATA;
1607 }
1608 } else {
1609 3362 int field = h->picture_structure == PICT_BOTTOM_FIELD;
1610 3362 release_unused_pictures(h, 0);
1611 3362 h->cur_pic_ptr->tf.owner[field] = h->avctx;
1612 }
1613 /* Some macroblocks can be accessed before they're available in case
1614 * of lost slices, MBAFF or threading. */
1615
2/2
✓ Branch 0 taken 6729 times.
✓ Branch 1 taken 21558 times.
28287 if (FIELD_PICTURE(h)) {
1616
2/2
✓ Branch 0 taken 168342 times.
✓ Branch 1 taken 6729 times.
175071 for(i = (h->picture_structure == PICT_BOTTOM_FIELD); i<h->mb_height; i++)
1617 168342 memset(h->slice_table + i*h->mb_stride, -1, (h->mb_stride - (i+1==h->mb_height)) * sizeof(*h->slice_table));
1618 } else {
1619 21558 memset(h->slice_table, -1,
1620 21558 (h->mb_height * h->mb_stride - 1) * sizeof(*h->slice_table));
1621 }
1622
1623 28287 ret = ff_h264_init_poc(h->cur_pic_ptr->field_poc, &h->cur_pic_ptr->poc,
1624 28287 h->ps.sps, &h->poc, h->picture_structure, nal->ref_idc);
1625
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 28287 times.
28287 if (ret < 0)
1626 return ret;
1627
1628 28287 memcpy(h->mmco, sl->mmco, sl->nb_mmco * sizeof(*h->mmco));
1629 28287 h->nb_mmco = sl->nb_mmco;
1630 28287 h->explicit_ref_marking = sl->explicit_ref_marking;
1631
1632 28287 h->picture_idr = nal->type == H264_NAL_IDR_SLICE;
1633
1634
2/2
✓ Branch 0 taken 90 times.
✓ Branch 1 taken 28197 times.
28287 if (h->sei.recovery_point.recovery_frame_cnt >= 0) {
1635 90 const int sei_recovery_frame_cnt = h->sei.recovery_point.recovery_frame_cnt;
1636
1637
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)
1638 76 h->valid_recovery_point = 1;
1639
1640
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 86 times.
90 if ( h->recovery_frame < 0
1641
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 || av_mod_uintp2(h->recovery_frame - h->poc.frame_num, h->ps.sps->log2_max_frame_num) > sei_recovery_frame_cnt) {
1642 86 h->recovery_frame = av_mod_uintp2(h->poc.frame_num + sei_recovery_frame_cnt, h->ps.sps->log2_max_frame_num);
1643
1644
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 80 times.
86 if (!h->valid_recovery_point)
1645 6 h->recovery_frame = h->poc.frame_num;
1646 }
1647 }
1648
1649
2/2
✓ Branch 0 taken 1077 times.
✓ Branch 1 taken 27210 times.
28287 h->cur_pic_ptr->f->flags |= AV_FRAME_FLAG_KEY * !!(nal->type == H264_NAL_IDR_SLICE);
1650
1651
2/2
✓ Branch 0 taken 1077 times.
✓ Branch 1 taken 27210 times.
28287 if (nal->type == H264_NAL_IDR_SLICE) {
1652 1077 h->cur_pic_ptr->recovered |= FRAME_RECOVERED_IDR;
1653 // If we have an IDR, all frames after it in decoded order are
1654 // "recovered".
1655 1077 h->frame_recovered |= FRAME_RECOVERED_IDR;
1656 }
1657
1658
4/4
✓ Branch 0 taken 99 times.
✓ Branch 1 taken 28188 times.
✓ Branch 2 taken 85 times.
✓ Branch 3 taken 14 times.
28287 if (h->recovery_frame == h->poc.frame_num && nal->ref_idc) {
1659 85 h->recovery_frame = -1;
1660 85 h->cur_pic_ptr->recovered |= FRAME_RECOVERED_SEI;
1661 }
1662
1663 #if 1
1664 28287 h->cur_pic_ptr->recovered |= h->frame_recovered;
1665 #else
1666 h->cur_pic_ptr->recovered |= !!(h->frame_recovered & FRAME_RECOVERED_IDR);
1667 #endif
1668
1669 /* Set the frame properties/side data. Only done for the second field in
1670 * field coded frames, since some SEI information is present for each field
1671 * and is merged by the SEI parsing code. */
1672
5/6
✓ Branch 0 taken 6729 times.
✓ Branch 1 taken 21558 times.
✓ Branch 2 taken 3367 times.
✓ Branch 3 taken 3362 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 3367 times.
28287 if (!FIELD_PICTURE(h) || !h->first_field || h->missing_fields > 1) {
1673 24920 ret = h264_export_frame_props(h);
1674
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 24920 times.
24920 if (ret < 0)
1675 return ret;
1676
1677 24920 ret = h264_select_output_frame(h);
1678
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 24920 times.
24920 if (ret < 0)
1679 return ret;
1680 }
1681
1682 28287 return 0;
1683 }
1684
1685 37607 static int h264_slice_header_parse(const H264Context *h, H264SliceContext *sl,
1686 const H2645NAL *nal)
1687 {
1688 const SPS *sps;
1689 const PPS *pps;
1690 int ret;
1691 unsigned int slice_type, tmp, i;
1692 int field_pic_flag, bottom_field_flag;
1693
4/4
✓ Branch 0 taken 37157 times.
✓ Branch 1 taken 450 times.
✓ Branch 2 taken 28336 times.
✓ Branch 3 taken 8821 times.
37607 int first_slice = sl == h->slice_ctx && !h->current_slice;
1694 int picture_structure;
1695
1696
2/2
✓ Branch 0 taken 28336 times.
✓ Branch 1 taken 9271 times.
37607 if (first_slice)
1697
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 28336 times.
28336 av_assert0(!h->setup_finished);
1698
1699 37607 sl->first_mb_addr = get_ue_golomb_long(&sl->gb);
1700
1701 37607 slice_type = get_ue_golomb_31(&sl->gb);
1702
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 37607 times.
37607 if (slice_type > 9) {
1703 av_log(h->avctx, AV_LOG_ERROR,
1704 "slice type %d too large at %d\n",
1705 slice_type, sl->first_mb_addr);
1706 return AVERROR_INVALIDDATA;
1707 }
1708
2/2
✓ Branch 0 taken 23883 times.
✓ Branch 1 taken 13724 times.
37607 if (slice_type > 4) {
1709 23883 slice_type -= 5;
1710 23883 sl->slice_type_fixed = 1;
1711 } else
1712 13724 sl->slice_type_fixed = 0;
1713
1714 37607 slice_type = ff_h264_golomb_to_pict_type[slice_type];
1715 37607 sl->slice_type = slice_type;
1716 37607 sl->slice_type_nos = slice_type & 3;
1717
1718
2/2
✓ Branch 0 taken 2263 times.
✓ Branch 1 taken 35344 times.
37607 if (nal->type == H264_NAL_IDR_SLICE &&
1719
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2263 times.
2263 sl->slice_type_nos != AV_PICTURE_TYPE_I) {
1720 av_log(h->avctx, AV_LOG_ERROR, "A non-intra slice in an IDR NAL unit.\n");
1721 return AVERROR_INVALIDDATA;
1722 }
1723
1724 37607 sl->pps_id = get_ue_golomb(&sl->gb);
1725
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 37607 times.
37607 if (sl->pps_id >= MAX_PPS_COUNT) {
1726 av_log(h->avctx, AV_LOG_ERROR, "pps_id %u out of range\n", sl->pps_id);
1727 return AVERROR_INVALIDDATA;
1728 }
1729
2/2
✓ Branch 0 taken 167 times.
✓ Branch 1 taken 37440 times.
37607 if (!h->ps.pps_list[sl->pps_id]) {
1730 167 av_log(h->avctx, AV_LOG_ERROR,
1731 "non-existing PPS %u referenced\n",
1732 sl->pps_id);
1733 167 return AVERROR_INVALIDDATA;
1734 }
1735 37440 pps = h->ps.pps_list[sl->pps_id];
1736 37440 sps = pps->sps;
1737
1738 37440 sl->frame_num = get_bits(&sl->gb, sps->log2_max_frame_num);
1739
2/2
✓ Branch 0 taken 9267 times.
✓ Branch 1 taken 28173 times.
37440 if (!first_slice) {
1740
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9267 times.
9267 if (h->poc.frame_num != sl->frame_num) {
1741 av_log(h->avctx, AV_LOG_ERROR, "Frame num change from %d to %d\n",
1742 h->poc.frame_num, sl->frame_num);
1743 return AVERROR_INVALIDDATA;
1744 }
1745 }
1746
1747 37440 sl->mb_mbaff = 0;
1748
1749
2/2
✓ Branch 0 taken 22946 times.
✓ Branch 1 taken 14494 times.
37440 if (sps->frame_mbs_only_flag) {
1750 22946 picture_structure = PICT_FRAME;
1751 } else {
1752
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 14494 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
14494 if (!sps->direct_8x8_inference_flag && slice_type == AV_PICTURE_TYPE_B) {
1753 av_log(h->avctx, AV_LOG_ERROR, "This stream was generated by a broken encoder, invalid 8x8 inference\n");
1754 return -1;
1755 }
1756 14494 field_pic_flag = get_bits1(&sl->gb);
1757
2/2
✓ Branch 0 taken 9026 times.
✓ Branch 1 taken 5468 times.
14494 if (field_pic_flag) {
1758 9026 bottom_field_flag = get_bits1(&sl->gb);
1759 9026 picture_structure = PICT_TOP_FIELD + bottom_field_flag;
1760 } else {
1761 5468 picture_structure = PICT_FRAME;
1762 }
1763 }
1764 37440 sl->picture_structure = picture_structure;
1765 37440 sl->mb_field_decoding_flag = picture_structure != PICT_FRAME;
1766
1767
2/2
✓ Branch 0 taken 28414 times.
✓ Branch 1 taken 9026 times.
37440 if (picture_structure == PICT_FRAME) {
1768 28414 sl->curr_pic_num = sl->frame_num;
1769 28414 sl->max_pic_num = 1 << sps->log2_max_frame_num;
1770 } else {
1771 9026 sl->curr_pic_num = 2 * sl->frame_num + 1;
1772 9026 sl->max_pic_num = 1 << (sps->log2_max_frame_num + 1);
1773 }
1774
1775
2/2
✓ Branch 0 taken 2258 times.
✓ Branch 1 taken 35182 times.
37440 if (nal->type == H264_NAL_IDR_SLICE) {
1776 2258 unsigned idr_pic_id = get_ue_golomb_long(&sl->gb);
1777
1/2
✓ Branch 0 taken 2258 times.
✗ Branch 1 not taken.
2258 if (idr_pic_id < 65536) {
1778 2258 sl->idr_pic_id = idr_pic_id;
1779 } else
1780 av_log(h->avctx, AV_LOG_WARNING, "idr_pic_id is invalid\n");
1781 }
1782
1783 37440 sl->poc_lsb = 0;
1784 37440 sl->delta_poc_bottom = 0;
1785
2/2
✓ Branch 0 taken 29660 times.
✓ Branch 1 taken 7780 times.
37440 if (sps->poc_type == 0) {
1786 29660 sl->poc_lsb = get_bits(&sl->gb, sps->log2_max_poc_lsb);
1787
1788
4/4
✓ Branch 0 taken 8523 times.
✓ Branch 1 taken 21137 times.
✓ Branch 2 taken 6631 times.
✓ Branch 3 taken 1892 times.
29660 if (pps->pic_order_present == 1 && picture_structure == PICT_FRAME)
1789 6631 sl->delta_poc_bottom = get_se_golomb(&sl->gb);
1790 }
1791
1792 37440 sl->delta_poc[0] = sl->delta_poc[1] = 0;
1793
4/4
✓ Branch 0 taken 3055 times.
✓ Branch 1 taken 34385 times.
✓ Branch 2 taken 2848 times.
✓ Branch 3 taken 207 times.
37440 if (sps->poc_type == 1 && !sps->delta_pic_order_always_zero_flag) {
1794 2848 sl->delta_poc[0] = get_se_golomb(&sl->gb);
1795
1796
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)
1797 453 sl->delta_poc[1] = get_se_golomb(&sl->gb);
1798 }
1799
1800 37440 sl->redundant_pic_count = 0;
1801
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 37440 times.
37440 if (pps->redundant_pic_cnt_present)
1802 sl->redundant_pic_count = get_ue_golomb(&sl->gb);
1803
1804
2/2
✓ Branch 0 taken 13148 times.
✓ Branch 1 taken 24292 times.
37440 if (sl->slice_type_nos == AV_PICTURE_TYPE_B)
1805 13148 sl->direct_spatial_mv_pred = get_bits1(&sl->gb);
1806
1807 37440 ret = ff_h264_parse_ref_count(&sl->list_count, sl->ref_count,
1808 &sl->gb, pps, sl->slice_type_nos,
1809 37440 picture_structure, h->avctx);
1810
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 37440 times.
37440 if (ret < 0)
1811 return ret;
1812
1813
2/2
✓ Branch 0 taken 32974 times.
✓ Branch 1 taken 4466 times.
37440 if (sl->slice_type_nos != AV_PICTURE_TYPE_I) {
1814 32974 ret = ff_h264_decode_ref_pic_list_reordering(sl, h->avctx);
1815
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 32974 times.
32974 if (ret < 0) {
1816 sl->ref_count[1] = sl->ref_count[0] = 0;
1817 return ret;
1818 }
1819 }
1820
1821 37440 sl->pwt.use_weight = 0;
1822
2/2
✓ Branch 0 taken 74880 times.
✓ Branch 1 taken 37440 times.
112320 for (i = 0; i < 2; i++) {
1823 74880 sl->pwt.luma_weight_flag[i] = 0;
1824 74880 sl->pwt.chroma_weight_flag[i] = 0;
1825 }
1826
4/4
✓ Branch 0 taken 4309 times.
✓ Branch 1 taken 33131 times.
✓ Branch 2 taken 1630 times.
✓ Branch 3 taken 2679 times.
37440 if ((pps->weighted_pred && sl->slice_type_nos == AV_PICTURE_TYPE_P) ||
1827
2/2
✓ Branch 0 taken 386 times.
✓ Branch 1 taken 34375 times.
34761 (pps->weighted_bipred_idc == 1 &&
1828
1/2
✓ Branch 0 taken 386 times.
✗ Branch 1 not taken.
386 sl->slice_type_nos == AV_PICTURE_TYPE_B)) {
1829 3065 ret = ff_h264_pred_weight_table(&sl->gb, sps, sl->ref_count,
1830 sl->slice_type_nos, &sl->pwt,
1831 3065 picture_structure, h->avctx);
1832
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3065 times.
3065 if (ret < 0)
1833 return ret;
1834 }
1835
1836 37440 sl->explicit_ref_marking = 0;
1837
2/2
✓ Branch 0 taken 24952 times.
✓ Branch 1 taken 12488 times.
37440 if (nal->ref_idc) {
1838 24952 ret = ff_h264_decode_ref_pic_marking(sl, &sl->gb, nal, h->avctx);
1839
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 24952 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
24952 if (ret < 0 && (h->avctx->err_recognition & AV_EF_EXPLODE))
1840 return AVERROR_INVALIDDATA;
1841 }
1842
1843
4/4
✓ Branch 0 taken 32974 times.
✓ Branch 1 taken 4466 times.
✓ Branch 2 taken 18162 times.
✓ Branch 3 taken 14812 times.
37440 if (sl->slice_type_nos != AV_PICTURE_TYPE_I && pps->cabac) {
1844 18162 tmp = get_ue_golomb_31(&sl->gb);
1845
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 18162 times.
18162 if (tmp > 2) {
1846 av_log(h->avctx, AV_LOG_ERROR, "cabac_init_idc %u overflow\n", tmp);
1847 return AVERROR_INVALIDDATA;
1848 }
1849 18162 sl->cabac_init_idc = tmp;
1850 }
1851
1852 37440 sl->last_qscale_diff = 0;
1853 37440 tmp = pps->init_qp + (unsigned)get_se_golomb(&sl->gb);
1854
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 37440 times.
37440 if (tmp > 51 + 6 * (sps->bit_depth_luma - 8)) {
1855 av_log(h->avctx, AV_LOG_ERROR, "QP %u out of range\n", tmp);
1856 return AVERROR_INVALIDDATA;
1857 }
1858 37440 sl->qscale = tmp;
1859 37440 sl->chroma_qp[0] = get_chroma_qp(pps, 0, sl->qscale);
1860 37440 sl->chroma_qp[1] = get_chroma_qp(pps, 1, sl->qscale);
1861 // FIXME qscale / qp ... stuff
1862
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 37440 times.
37440 if (sl->slice_type == AV_PICTURE_TYPE_SP)
1863 get_bits1(&sl->gb); /* sp_for_switch_flag */
1864
1/2
✓ Branch 0 taken 37440 times.
✗ Branch 1 not taken.
37440 if (sl->slice_type == AV_PICTURE_TYPE_SP ||
1865
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 37440 times.
37440 sl->slice_type == AV_PICTURE_TYPE_SI)
1866 get_se_golomb(&sl->gb); /* slice_qs_delta */
1867
1868 37440 sl->deblocking_filter = 1;
1869 37440 sl->slice_alpha_c0_offset = 0;
1870 37440 sl->slice_beta_offset = 0;
1871
2/2
✓ Branch 0 taken 22657 times.
✓ Branch 1 taken 14783 times.
37440 if (pps->deblocking_filter_parameters_present) {
1872 22657 tmp = get_ue_golomb_31(&sl->gb);
1873
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 22657 times.
22657 if (tmp > 2) {
1874 av_log(h->avctx, AV_LOG_ERROR,
1875 "deblocking_filter_idc %u out of range\n", tmp);
1876 return AVERROR_INVALIDDATA;
1877 }
1878 22657 sl->deblocking_filter = tmp;
1879
2/2
✓ Branch 0 taken 20156 times.
✓ Branch 1 taken 2501 times.
22657 if (sl->deblocking_filter < 2)
1880 20156 sl->deblocking_filter ^= 1; // 1<->0
1881
1882
2/2
✓ Branch 0 taken 14946 times.
✓ Branch 1 taken 7711 times.
22657 if (sl->deblocking_filter) {
1883 14946 int slice_alpha_c0_offset_div2 = get_se_golomb(&sl->gb);
1884 14946 int slice_beta_offset_div2 = get_se_golomb(&sl->gb);
1885
2/4
✓ Branch 0 taken 14946 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 14946 times.
✗ Branch 3 not taken.
14946 if (slice_alpha_c0_offset_div2 > 6 ||
1886
1/2
✓ Branch 0 taken 14946 times.
✗ Branch 1 not taken.
14946 slice_alpha_c0_offset_div2 < -6 ||
1887
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14946 times.
14946 slice_beta_offset_div2 > 6 ||
1888 slice_beta_offset_div2 < -6) {
1889 av_log(h->avctx, AV_LOG_ERROR,
1890 "deblocking filter parameters %d %d out of range\n",
1891 slice_alpha_c0_offset_div2, slice_beta_offset_div2);
1892 return AVERROR_INVALIDDATA;
1893 }
1894 14946 sl->slice_alpha_c0_offset = slice_alpha_c0_offset_div2 * 2;
1895 14946 sl->slice_beta_offset = slice_beta_offset_div2 * 2;
1896 }
1897 }
1898
1899 37440 return 0;
1900 }
1901
1902 /* do all the per-slice initialization needed before we can start decoding the
1903 * actual MBs */
1904 37360 static int h264_slice_init(H264Context *h, H264SliceContext *sl,
1905 const H2645NAL *nal)
1906 {
1907 37360 int i, j, ret = 0;
1908
1909
3/4
✓ Branch 0 taken 2258 times.
✓ Branch 1 taken 35102 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 2258 times.
37360 if (h->picture_idr && nal->type != H264_NAL_IDR_SLICE) {
1910 av_log(h->avctx, AV_LOG_ERROR, "Invalid mix of IDR and non-IDR slices\n");
1911 return AVERROR_INVALIDDATA;
1912 }
1913
1914 av_assert1(h->mb_num == h->mb_width * h->mb_height);
1915
5/6
✓ Branch 0 taken 32389 times.
✓ Branch 1 taken 4971 times.
✓ Branch 2 taken 8946 times.
✓ Branch 3 taken 23443 times.
✓ Branch 4 taken 37360 times.
✗ Branch 5 not taken.
37360 if (sl->first_mb_addr << FIELD_OR_MBAFF_PICTURE(h) >= h->mb_num ||
1916
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 37360 times.
37360 sl->first_mb_addr >= h->mb_num) {
1917 av_log(h->avctx, AV_LOG_ERROR, "first_mb_in_slice overflow\n");
1918 return AVERROR_INVALIDDATA;
1919 }
1920 37360 sl->resync_mb_x = sl->mb_x = sl->first_mb_addr % h->mb_width;
1921 74720 sl->resync_mb_y = sl->mb_y = (sl->first_mb_addr / h->mb_width) <<
1922
4/4
✓ Branch 0 taken 32389 times.
✓ Branch 1 taken 4971 times.
✓ Branch 2 taken 8946 times.
✓ Branch 3 taken 23443 times.
37360 FIELD_OR_MBAFF_PICTURE(h);
1923
2/2
✓ Branch 0 taken 4470 times.
✓ Branch 1 taken 32890 times.
37360 if (h->picture_structure == PICT_BOTTOM_FIELD)
1924 4470 sl->resync_mb_y = sl->mb_y = sl->mb_y + 1;
1925 av_assert1(sl->mb_y < h->mb_height);
1926
1927 37360 ret = ff_h264_build_ref_list(h, sl);
1928
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 37360 times.
37360 if (ret < 0)
1929 return ret;
1930
1931
2/2
✓ Branch 0 taken 3811 times.
✓ Branch 1 taken 33549 times.
37360 if (h->ps.pps->weighted_bipred_idc == 2 &&
1932
2/2
✓ Branch 0 taken 2127 times.
✓ Branch 1 taken 1684 times.
3811 sl->slice_type_nos == AV_PICTURE_TYPE_B) {
1933 2127 implicit_weight_table(h, sl, -1);
1934
2/2
✓ Branch 0 taken 855 times.
✓ Branch 1 taken 1272 times.
2127 if (FRAME_MBAFF(h)) {
1935 855 implicit_weight_table(h, sl, 0);
1936 855 implicit_weight_table(h, sl, 1);
1937 }
1938 }
1939
1940
4/4
✓ Branch 0 taken 13104 times.
✓ Branch 1 taken 24256 times.
✓ Branch 2 taken 7703 times.
✓ Branch 3 taken 5401 times.
37360 if (sl->slice_type_nos == AV_PICTURE_TYPE_B && !sl->direct_spatial_mv_pred)
1941 7703 ff_h264_direct_dist_scale_factor(h, sl);
1942
1/2
✓ Branch 0 taken 37360 times.
✗ Branch 1 not taken.
37360 if (!h->setup_finished)
1943 37360 ff_h264_direct_ref_list_init(h, sl);
1944
1945
1/2
✓ Branch 0 taken 37360 times.
✗ Branch 1 not taken.
37360 if (h->avctx->skip_loop_filter >= AVDISCARD_ALL ||
1946
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 37360 times.
37360 (h->avctx->skip_loop_filter >= AVDISCARD_NONKEY &&
1947 h->nal_unit_type != H264_NAL_IDR_SLICE) ||
1948
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 37360 times.
37360 (h->avctx->skip_loop_filter >= AVDISCARD_NONINTRA &&
1949 sl->slice_type_nos != AV_PICTURE_TYPE_I) ||
1950
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 37360 times.
37360 (h->avctx->skip_loop_filter >= AVDISCARD_BIDIR &&
1951 sl->slice_type_nos == AV_PICTURE_TYPE_B) ||
1952
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 37360 times.
37360 (h->avctx->skip_loop_filter >= AVDISCARD_NONREF &&
1953 nal->ref_idc == 0))
1954 sl->deblocking_filter = 0;
1955
1956
3/4
✓ Branch 0 taken 27148 times.
✓ Branch 1 taken 10212 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 27148 times.
37360 if (sl->deblocking_filter == 1 && h->nb_slice_ctx > 1) {
1957 if (h->avctx->flags2 & AV_CODEC_FLAG2_FAST) {
1958 /* Cheat slightly for speed:
1959 * Do not bother to deblock across slices. */
1960 sl->deblocking_filter = 2;
1961 } else {
1962 h->postpone_filter = 1;
1963 }
1964 }
1965 37360 sl->qp_thresh = 15 -
1966 37360 FFMIN(sl->slice_alpha_c0_offset, sl->slice_beta_offset) -
1967 37360 FFMAX3(0,
1968 h->ps.pps->chroma_qp_index_offset[0],
1969 37360 h->ps.pps->chroma_qp_index_offset[1]) +
1970 37360 6 * (h->ps.sps->bit_depth_luma - 8);
1971
1972 37360 sl->slice_num = ++h->current_slice;
1973
1974
1/2
✓ Branch 0 taken 37360 times.
✗ Branch 1 not taken.
37360 if (sl->slice_num)
1975 37360 h->slice_row[(sl->slice_num-1)&(MAX_SLICES-1)]= sl->resync_mb_y;
1976
2/2
✓ Branch 0 taken 34488 times.
✓ Branch 1 taken 2872 times.
37360 if ( h->slice_row[sl->slice_num&(MAX_SLICES-1)] + 3 >= sl->resync_mb_y
1977
2/2
✓ Branch 0 taken 25749 times.
✓ Branch 1 taken 8739 times.
34488 && h->slice_row[sl->slice_num&(MAX_SLICES-1)] <= sl->resync_mb_y
1978
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 25749 times.
25749 && sl->slice_num >= MAX_SLICES) {
1979 //in case of ASO this check needs to be updated depending on how we decide to assign slice numbers in this case
1980 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);
1981 }
1982
1983
2/2
✓ Branch 0 taken 74720 times.
✓ Branch 1 taken 37360 times.
112080 for (j = 0; j < 2; j++) {
1984 int id_list[16];
1985 74720 int *ref2frm = h->ref2frm[sl->slice_num & (MAX_SLICES - 1)][j];
1986
2/2
✓ Branch 0 taken 1195520 times.
✓ Branch 1 taken 74720 times.
1270240 for (i = 0; i < 16; i++) {
1987 1195520 id_list[i] = 60;
1988
4/4
✓ Branch 0 taken 735968 times.
✓ Branch 1 taken 459552 times.
✓ Branch 2 taken 140481 times.
✓ Branch 3 taken 595487 times.
1195520 if (j < sl->list_count && i < sl->ref_count[j] &&
1989
1/2
✓ Branch 0 taken 140481 times.
✗ Branch 1 not taken.
140481 sl->ref_list[j][i].parent->f->buf[0]) {
1990 int k;
1991 140481 const AVBuffer *buf = sl->ref_list[j][i].parent->f->buf[0]->buffer;
1992
2/2
✓ Branch 0 taken 469009 times.
✓ Branch 1 taken 1933 times.
470942 for (k = 0; k < h->short_ref_count; k++)
1993
2/2
✓ Branch 0 taken 138548 times.
✓ Branch 1 taken 330461 times.
469009 if (h->short_ref[k]->f->buf[0]->buffer == buf) {
1994 138548 id_list[i] = k;
1995 138548 break;
1996 }
1997
2/2
✓ Branch 0 taken 11870 times.
✓ Branch 1 taken 138689 times.
150559 for (k = 0; k < h->long_ref_count; k++)
1998
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) {
1999 1792 id_list[i] = h->short_ref_count + k;
2000 1792 break;
2001 }
2002 }
2003 }
2004
2005 74720 ref2frm[0] =
2006 74720 ref2frm[1] = -1;
2007
2/2
✓ Branch 0 taken 1195520 times.
✓ Branch 1 taken 74720 times.
1270240 for (i = 0; i < 16; i++)
2008 1195520 ref2frm[i + 2] = 4 * id_list[i] + (sl->ref_list[j][i].reference & 3);
2009 74720 ref2frm[18 + 0] =
2010 74720 ref2frm[18 + 1] = -1;
2011
2/2
✓ Branch 0 taken 2391040 times.
✓ Branch 1 taken 74720 times.
2465760 for (i = 16; i < 48; i++)
2012 2391040 ref2frm[i + 4] = 4 * id_list[(i - 16) >> 1] +
2013 2391040 (sl->ref_list[j][i].reference & 3);
2014 }
2015
2016
2/2
✓ Branch 0 taken 4466 times.
✓ Branch 1 taken 32894 times.
37360 if (sl->slice_type_nos == AV_PICTURE_TYPE_I) {
2017 4466 h->cur_pic_ptr->gray = 0;
2018 4466 h->non_gray = 1;
2019 } else {
2020 32894 int gray = 0;
2021
2/2
✓ Branch 0 taken 45998 times.
✓ Branch 1 taken 32894 times.
78892 for (j = 0; j < sl->list_count; j++) {
2022
2/2
✓ Branch 0 taken 140481 times.
✓ Branch 1 taken 45998 times.
186479 for (i = 0; i < sl->ref_count[j]; i++) {
2023 140481 gray |= sl->ref_list[j][i].parent->gray;
2024 }
2025 }
2026 32894 h->cur_pic_ptr->gray = gray;
2027 }
2028
2029
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 37360 times.
37360 if (h->avctx->debug & FF_DEBUG_PICT_INFO) {
2030 av_log(h->avctx, AV_LOG_DEBUG,
2031 "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",
2032 sl->slice_num,
2033 (h->picture_structure == PICT_FRAME ? 'F' : h->picture_structure == PICT_TOP_FIELD ? 'T' : 'B'),
2034 sl->mb_y * h->mb_width + sl->mb_x,
2035 av_get_picture_type_char(sl->slice_type),
2036 sl->slice_type_fixed ? " fix" : "",
2037 nal->type == H264_NAL_IDR_SLICE ? " IDR" : "",
2038 h->poc.frame_num,
2039 h->cur_pic_ptr->field_poc[0],
2040 h->cur_pic_ptr->field_poc[1],
2041 sl->ref_count[0], sl->ref_count[1],
2042 sl->qscale,
2043 sl->deblocking_filter,
2044 sl->slice_alpha_c0_offset, sl->slice_beta_offset,
2045 sl->pwt.use_weight,
2046 sl->pwt.use_weight == 1 && sl->pwt.use_weight_chroma ? "c" : "",
2047 sl->slice_type == AV_PICTURE_TYPE_B ? (sl->direct_spatial_mv_pred ? "SPAT" : "TEMP") : "");
2048 }
2049
2050 37360 return 0;
2051 }
2052
2053 37607 int ff_h264_queue_decode_slice(H264Context *h, const H2645NAL *nal)
2054 {
2055 37607 H264SliceContext *sl = h->slice_ctx + h->nb_slice_ctx_queued;
2056
4/4
✓ Branch 0 taken 37157 times.
✓ Branch 1 taken 450 times.
✓ Branch 2 taken 28336 times.
✓ Branch 3 taken 8821 times.
37607 int first_slice = sl == h->slice_ctx && !h->current_slice;
2057 int ret;
2058
2059 37607 sl->gb = nal->gb;
2060
2061 37607 ret = h264_slice_header_parse(h, sl, nal);
2062
2/2
✓ Branch 0 taken 167 times.
✓ Branch 1 taken 37440 times.
37607 if (ret < 0)
2063 167 return ret;
2064
2065 // discard redundant pictures
2066
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 37440 times.
37440 if (sl->redundant_pic_count > 0) {
2067 sl->ref_count[0] = sl->ref_count[1] = 0;
2068 return 0;
2069 }
2070
2071
3/4
✓ Branch 0 taken 9073 times.
✓ Branch 1 taken 28367 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 9073 times.
37440 if (sl->first_mb_addr == 0 || !h->current_slice) {
2072
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 28367 times.
28367 if (h->setup_finished) {
2073 av_log(h->avctx, AV_LOG_ERROR, "Too many fields\n");
2074 return AVERROR_INVALIDDATA;
2075 }
2076 }
2077
2078
2/2
✓ Branch 0 taken 28367 times.
✓ Branch 1 taken 9073 times.
37440 if (sl->first_mb_addr == 0) { // FIXME better field boundary detection
2079
2/2
✓ Branch 0 taken 194 times.
✓ Branch 1 taken 28173 times.
28367 if (h->current_slice) {
2080 // this slice starts a new field
2081 // first decode any pending queued slices
2082
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 194 times.
194 if (h->nb_slice_ctx_queued) {
2083 H264SliceContext tmp_ctx;
2084
2085 ret = ff_h264_execute_decode_slices(h);
2086 if (ret < 0 && (h->avctx->err_recognition & AV_EF_EXPLODE))
2087 return ret;
2088
2089 memcpy(&tmp_ctx, h->slice_ctx, sizeof(tmp_ctx));
2090 memcpy(h->slice_ctx, sl, sizeof(tmp_ctx));
2091 memcpy(sl, &tmp_ctx, sizeof(tmp_ctx));
2092 sl = h->slice_ctx;
2093 }
2094
2095
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) {
2096 194 ret = ff_h264_field_end(h, h->slice_ctx, 1);
2097
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 194 times.
194 if (ret < 0)
2098 return ret;
2099 } else if (h->cur_pic_ptr && !FIELD_PICTURE(h) && !h->first_field && h->nal_unit_type == H264_NAL_IDR_SLICE) {
2100 av_log(h, AV_LOG_WARNING, "Broken frame packetizing\n");
2101 ret = ff_h264_field_end(h, h->slice_ctx, 1);
2102 ff_thread_report_progress(&h->cur_pic_ptr->tf, INT_MAX, 0);
2103 ff_thread_report_progress(&h->cur_pic_ptr->tf, INT_MAX, 1);
2104 h->cur_pic_ptr = NULL;
2105 if (ret < 0)
2106 return ret;
2107 } else
2108 return AVERROR_INVALIDDATA;
2109 }
2110
2111
2/2
✓ Branch 0 taken 25005 times.
✓ Branch 1 taken 3362 times.
28367 if (!h->first_field) {
2112
3/4
✓ Branch 0 taken 299 times.
✓ Branch 1 taken 24706 times.
✓ Branch 2 taken 299 times.
✗ Branch 3 not taken.
25005 if (h->cur_pic_ptr && !h->droppable) {
2113 299 ff_thread_report_progress(&h->cur_pic_ptr->tf, INT_MAX,
2114 299 h->picture_structure == PICT_BOTTOM_FIELD);
2115 }
2116 25005 h->cur_pic_ptr = NULL;
2117 }
2118 }
2119
2120
2/2
✓ Branch 0 taken 28367 times.
✓ Branch 1 taken 9073 times.
37440 if (!h->current_slice)
2121
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 28367 times.
28367 av_assert0(sl == h->slice_ctx);
2122
2123
4/4
✓ Branch 0 taken 28367 times.
✓ Branch 1 taken 9073 times.
✓ Branch 2 taken 25005 times.
✓ Branch 3 taken 3362 times.
37440 if (h->current_slice == 0 && !h->first_field) {
2124 25005 if (
2125
3/4
✓ Branch 0 taken 84 times.
✓ Branch 1 taken 24921 times.
✓ Branch 2 taken 84 times.
✗ Branch 3 not taken.
25005 (h->avctx->skip_frame >= AVDISCARD_NONREF && !h->nal_ref_idc) ||
2126
4/4
✓ Branch 0 taken 84 times.
✓ Branch 1 taken 24921 times.
✓ Branch 2 taken 40 times.
✓ Branch 3 taken 44 times.
25005 (h->avctx->skip_frame >= AVDISCARD_BIDIR && sl->slice_type_nos == AV_PICTURE_TYPE_B) ||
2127
4/4
✓ Branch 0 taken 40 times.
✓ Branch 1 taken 24921 times.
✓ Branch 2 taken 4 times.
✓ Branch 3 taken 36 times.
24961 (h->avctx->skip_frame >= AVDISCARD_NONINTRA && sl->slice_type_nos != AV_PICTURE_TYPE_I) ||
2128
4/6
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 24923 times.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
24925 (h->avctx->skip_frame >= AVDISCARD_NONKEY && h->nal_unit_type != H264_NAL_IDR_SLICE && h->sei.recovery_point.recovery_frame_cnt < 0) ||
2129
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 24925 times.
24925 h->avctx->skip_frame >= AVDISCARD_ALL) {
2130 80 return 0;
2131 }
2132 }
2133
2134
2/2
✓ Branch 0 taken 9267 times.
✓ Branch 1 taken 28093 times.
37360 if (!first_slice) {
2135 9267 const PPS *pps = h->ps.pps_list[sl->pps_id];
2136
2137
1/2
✓ Branch 0 taken 9267 times.
✗ Branch 1 not taken.
9267 if (h->ps.pps->sps_id != pps->sps_id ||
2138
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9267 times.
9267 h->ps.pps->transform_8x8_mode != pps->transform_8x8_mode /*||
2139 (h->setup_finished && h->ps.pps != pps)*/) {
2140 av_log(h->avctx, AV_LOG_ERROR, "PPS changed between slices\n");
2141 return AVERROR_INVALIDDATA;
2142 }
2143
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9267 times.
9267 if (h->ps.sps != pps->sps) {
2144 av_log(h->avctx, AV_LOG_ERROR,
2145 "SPS changed in the middle of the frame\n");
2146 return AVERROR_INVALIDDATA;
2147 }
2148 }
2149
2150
2/2
✓ Branch 0 taken 28287 times.
✓ Branch 1 taken 9073 times.
37360 if (h->current_slice == 0) {
2151 28287 ret = h264_field_start(h, sl, nal, first_slice);
2152
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 28287 times.
28287 if (ret < 0)
2153 return ret;
2154 } else {
2155
1/2
✓ Branch 0 taken 9073 times.
✗ Branch 1 not taken.
9073 if (h->picture_structure != sl->picture_structure ||
2156
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9073 times.
9073 h->droppable != (nal->ref_idc == 0)) {
2157 av_log(h->avctx, AV_LOG_ERROR,
2158 "Changing field mode (%d -> %d) between slices is not allowed\n",
2159 h->picture_structure, sl->picture_structure);
2160 return AVERROR_INVALIDDATA;
2161
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9073 times.
9073 } else if (!h->cur_pic_ptr) {
2162 av_log(h->avctx, AV_LOG_ERROR,
2163 "unset cur_pic_ptr on slice %d\n",
2164 h->current_slice + 1);
2165 return AVERROR_INVALIDDATA;
2166 }
2167 }
2168
2169 37360 ret = h264_slice_init(h, sl, nal);
2170
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 37360 times.
37360 if (ret < 0)
2171 return ret;
2172
2173 37360 h->nb_slice_ctx_queued++;
2174
2175 37360 return 0;
2176 }
2177
2178 int ff_h264_get_slice_type(const H264SliceContext *sl)
2179 {
2180 switch (sl->slice_type) {
2181 case AV_PICTURE_TYPE_P:
2182 return 0;
2183 case AV_PICTURE_TYPE_B:
2184 return 1;
2185 case AV_PICTURE_TYPE_I:
2186 return 2;
2187 case AV_PICTURE_TYPE_SP:
2188 return 3;
2189 case AV_PICTURE_TYPE_SI:
2190 return 4;
2191 default:
2192 return AVERROR_INVALIDDATA;
2193 }
2194 }
2195
2196 12357261 static av_always_inline void fill_filter_caches_inter(const H264Context *h,
2197 H264SliceContext *sl,
2198 int mb_type, int top_xy,
2199 const int left_xy[LEFT_MBS],
2200 int top_type,
2201 const int left_type[LEFT_MBS],
2202 int mb_xy, int list)
2203 {
2204 12357261 int b_stride = h->b_stride;
2205 12357261 int16_t(*mv_dst)[2] = &sl->mv_cache[list][scan8[0]];
2206 12357261 int8_t *ref_cache = &sl->ref_cache[list][scan8[0]];
2207
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 12357261 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
12357261 if (IS_INTER(mb_type) || IS_DIRECT(mb_type)) {
2208
2/2
✓ Branch 0 taken 9936403 times.
✓ Branch 1 taken 2420858 times.
12357261 if (USES_LIST(top_type, list)) {
2209 9936403 const int b_xy = h->mb2b_xy[top_xy] + 3 * b_stride;
2210 9936403 const int b8_xy = 4 * top_xy + 2;
2211
2/2
✓ Branch 0 taken 341706 times.
✓ Branch 1 taken 9594697 times.
9936403 const int *ref2frm = &h->ref2frm[h->slice_table[top_xy] & (MAX_SLICES - 1)][list][(MB_MBAFF(sl) ? 20 : 2)];
2212 9936403 AV_COPY128(mv_dst - 1 * 8, h->cur_pic.motion_val[list][b_xy + 0]);
2213 9936403 ref_cache[0 - 1 * 8] =
2214 9936403 ref_cache[1 - 1 * 8] = ref2frm[h->cur_pic.ref_index[list][b8_xy + 0]];
2215 9936403 ref_cache[2 - 1 * 8] =
2216 9936403 ref_cache[3 - 1 * 8] = ref2frm[h->cur_pic.ref_index[list][b8_xy + 1]];
2217 } else {
2218 2420858 AV_ZERO128(mv_dst - 1 * 8);
2219 2420858 AV_WN32A(&ref_cache[0 - 1 * 8], ((LIST_NOT_USED) & 0xFF) * 0x01010101u);
2220 }
2221
2222
2/2
✓ Branch 0 taken 11925124 times.
✓ Branch 1 taken 432137 times.
12357261 if (!IS_INTERLACED(mb_type ^ left_type[LTOP])) {
2223
2/2
✓ Branch 0 taken 9961500 times.
✓ Branch 1 taken 1963624 times.
11925124 if (USES_LIST(left_type[LTOP], list)) {
2224 9961500 const int b_xy = h->mb2b_xy[left_xy[LTOP]] + 3;
2225 9961500 const int b8_xy = 4 * left_xy[LTOP] + 1;
2226
2/2
✓ Branch 0 taken 238836 times.
✓ Branch 1 taken 9722664 times.
9961500 const int *ref2frm = &h->ref2frm[h->slice_table[left_xy[LTOP]] & (MAX_SLICES - 1)][list][(MB_MBAFF(sl) ? 20 : 2)];
2227 9961500 AV_COPY32(mv_dst - 1 + 0, h->cur_pic.motion_val[list][b_xy + b_stride * 0]);
2228 9961500 AV_COPY32(mv_dst - 1 + 8, h->cur_pic.motion_val[list][b_xy + b_stride * 1]);
2229 9961500 AV_COPY32(mv_dst - 1 + 16, h->cur_pic.motion_val[list][b_xy + b_stride * 2]);
2230 9961500 AV_COPY32(mv_dst - 1 + 24, h->cur_pic.motion_val[list][b_xy + b_stride * 3]);
2231 9961500 ref_cache[-1 + 0] =
2232 9961500 ref_cache[-1 + 8] = ref2frm[h->cur_pic.ref_index[list][b8_xy + 2 * 0]];
2233 9961500 ref_cache[-1 + 16] =
2234 9961500 ref_cache[-1 + 24] = ref2frm[h->cur_pic.ref_index[list][b8_xy + 2 * 1]];
2235 } else {
2236 1963624 AV_ZERO32(mv_dst - 1 + 0);
2237 1963624 AV_ZERO32(mv_dst - 1 + 8);
2238 1963624 AV_ZERO32(mv_dst - 1 + 16);
2239 1963624 AV_ZERO32(mv_dst - 1 + 24);
2240 1963624 ref_cache[-1 + 0] =
2241 1963624 ref_cache[-1 + 8] =
2242 1963624 ref_cache[-1 + 16] =
2243 1963624 ref_cache[-1 + 24] = LIST_NOT_USED;
2244 }
2245 }
2246 }
2247
2248
2/2
✓ Branch 0 taken 1379189 times.
✓ Branch 1 taken 10978072 times.
12357261 if (!USES_LIST(mb_type, list)) {
2249 1379189 fill_rectangle(mv_dst, 4, 4, 8, pack16to32(0, 0), 4);
2250 1379189 AV_WN32A(&ref_cache[0 * 8], ((LIST_NOT_USED) & 0xFF) * 0x01010101u);
2251 1379189 AV_WN32A(&ref_cache[1 * 8], ((LIST_NOT_USED) & 0xFF) * 0x01010101u);
2252 1379189 AV_WN32A(&ref_cache[2 * 8], ((LIST_NOT_USED) & 0xFF) * 0x01010101u);
2253 1379189 AV_WN32A(&ref_cache[3 * 8], ((LIST_NOT_USED) & 0xFF) * 0x01010101u);
2254 1379189 return;
2255 }
2256
2257 {
2258 10978072 const int8_t *ref = &h->cur_pic.ref_index[list][4 * mb_xy];
2259
2/2
✓ Branch 0 taken 412390 times.
✓ Branch 1 taken 10565682 times.
10978072 const int *ref2frm = &h->ref2frm[sl->slice_num & (MAX_SLICES - 1)][list][(MB_MBAFF(sl) ? 20 : 2)];
2260 10978072 uint32_t ref01 = (pack16to32(ref2frm[ref[0]], ref2frm[ref[1]]) & 0x00FF00FF) * 0x0101;
2261 10978072 uint32_t ref23 = (pack16to32(ref2frm[ref[2]], ref2frm[ref[3]]) & 0x00FF00FF) * 0x0101;
2262 10978072 AV_WN32A(&ref_cache[0 * 8], ref01);
2263 10978072 AV_WN32A(&ref_cache[1 * 8], ref01);
2264 10978072 AV_WN32A(&ref_cache[2 * 8], ref23);
2265 10978072 AV_WN32A(&ref_cache[3 * 8], ref23);
2266 }
2267
2268 {
2269 10978072 int16_t(*mv_src)[2] = &h->cur_pic.motion_val[list][4 * sl->mb_x + 4 * sl->mb_y * b_stride];
2270 10978072 AV_COPY128(mv_dst + 8 * 0, mv_src + 0 * b_stride);
2271 10978072 AV_COPY128(mv_dst + 8 * 1, mv_src + 1 * b_stride);
2272 10978072 AV_COPY128(mv_dst + 8 * 2, mv_src + 2 * b_stride);
2273 10978072 AV_COPY128(mv_dst + 8 * 3, mv_src + 3 * b_stride);
2274 }
2275 }
2276
2277 /**
2278 * @return non zero if the loop filter can be skipped
2279 */
2280 12576316 static int fill_filter_caches(const H264Context *h, H264SliceContext *sl, int mb_type)
2281 {
2282 12576316 const int mb_xy = sl->mb_xy;
2283 int top_xy, left_xy[LEFT_MBS];
2284 int top_type, left_type[LEFT_MBS];
2285 const uint8_t *nnz;
2286 uint8_t *nnz_cache;
2287
2288 12576316 top_xy = mb_xy - (h->mb_stride << MB_FIELD(sl));
2289
2290 12576316 left_xy[LBOT] = left_xy[LTOP] = mb_xy - 1;
2291
2/2
✓ Branch 0 taken 2020896 times.
✓ Branch 1 taken 10555420 times.
12576316 if (FRAME_MBAFF(h)) {
2292 2020896 const int left_mb_field_flag = IS_INTERLACED(h->cur_pic.mb_type[mb_xy - 1]);
2293 2020896 const int curr_mb_field_flag = IS_INTERLACED(mb_type);
2294
2/2
✓ Branch 0 taken 1010448 times.
✓ Branch 1 taken 1010448 times.
2020896 if (sl->mb_y & 1) {
2295
2/2
✓ Branch 0 taken 204638 times.
✓ Branch 1 taken 805810 times.
1010448 if (left_mb_field_flag != curr_mb_field_flag)
2296 204638 left_xy[LTOP] -= h->mb_stride;
2297 } else {
2298
2/2
✓ Branch 0 taken 262778 times.
✓ Branch 1 taken 747670 times.
1010448 if (curr_mb_field_flag)
2299 262778 top_xy += h->mb_stride &
2300 262778 (((h->cur_pic.mb_type[top_xy] >> 7) & 1) - 1);
2301
2/2
✓ Branch 0 taken 204638 times.
✓ Branch 1 taken 805810 times.
1010448 if (left_mb_field_flag != curr_mb_field_flag)
2302 204638 left_xy[LBOT] += h->mb_stride;
2303 }
2304 }
2305
2306 12576316 sl->top_mb_xy = top_xy;
2307 12576316 sl->left_mb_xy[LTOP] = left_xy[LTOP];
2308 12576316 sl->left_mb_xy[LBOT] = left_xy[LBOT];
2309 {
2310 /* For sufficiently low qp, filtering wouldn't do anything.
2311 * This is a conservative estimate: could also check beta_offset
2312 * and more accurate chroma_qp. */
2313 12576316 int qp_thresh = sl->qp_thresh; // FIXME strictly we should store qp_thresh for each mb of a slice
2314 12576316 int qp = h->cur_pic.qscale_table[mb_xy];
2315
2/2
✓ Branch 0 taken 1768651 times.
✓ Branch 1 taken 10807665 times.
12576316 if (qp <= qp_thresh &&
2316
2/2
✓ Branch 0 taken 1766943 times.
✓ Branch 1 taken 1708 times.
1768651 (left_xy[LTOP] < 0 ||
2317
4/4
✓ Branch 0 taken 1706887 times.
✓ Branch 1 taken 60056 times.
✓ Branch 2 taken 1624009 times.
✓ Branch 3 taken 84586 times.
1768651 ((qp + h->cur_pic.qscale_table[left_xy[LTOP]] + 1) >> 1) <= qp_thresh) &&
2318 1624009 (top_xy < 0 ||
2319
2/2
✓ Branch 0 taken 1429028 times.
✓ Branch 1 taken 194981 times.
1624009 ((qp + h->cur_pic.qscale_table[top_xy] + 1) >> 1) <= qp_thresh)) {
2320
2/2
✓ Branch 0 taken 1197803 times.
✓ Branch 1 taken 315811 times.
1513614 if (!FRAME_MBAFF(h))
2321 1197803 return 1;
2322
2/2
✓ Branch 0 taken 315680 times.
✓ Branch 1 taken 131 times.
315811 if ((left_xy[LTOP] < 0 ||
2323
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) &&
2324
2/2
✓ Branch 0 taken 294673 times.
✓ Branch 1 taken 20609 times.
315282 (top_xy < h->mb_stride ||
2325
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))
2326 292171 return 1;
2327 }
2328 }
2329
2330 11086342 top_type = h->cur_pic.mb_type[top_xy];
2331 11086342 left_type[LTOP] = h->cur_pic.mb_type[left_xy[LTOP]];
2332 11086342 left_type[LBOT] = h->cur_pic.mb_type[left_xy[LBOT]];
2333
2/2
✓ Branch 0 taken 470376 times.
✓ Branch 1 taken 10615966 times.
11086342 if (sl->deblocking_filter == 2) {
2334
2/2
✓ Branch 0 taken 56878 times.
✓ Branch 1 taken 413498 times.
470376 if (h->slice_table[top_xy] != sl->slice_num)
2335 56878 top_type = 0;
2336
2/2
✓ Branch 0 taken 13874 times.
✓ Branch 1 taken 456502 times.
470376 if (h->slice_table[left_xy[LBOT]] != sl->slice_num)
2337 13874 left_type[LTOP] = left_type[LBOT] = 0;
2338 } else {
2339
2/2
✓ Branch 0 taken 473599 times.
✓ Branch 1 taken 10142367 times.
10615966 if (h->slice_table[top_xy] == 0xFFFF)
2340 473599 top_type = 0;
2341
2/2
✓ Branch 0 taken 304091 times.
✓ Branch 1 taken 10311875 times.
10615966 if (h->slice_table[left_xy[LBOT]] == 0xFFFF)
2342 304091 left_type[LTOP] = left_type[LBOT] = 0;
2343 }
2344 11086342 sl->top_type = top_type;
2345 11086342 sl->left_type[LTOP] = left_type[LTOP];
2346 11086342 sl->left_type[LBOT] = left_type[LBOT];
2347
2348
2/2
✓ Branch 0 taken 2983528 times.
✓ Branch 1 taken 8102814 times.
11086342 if (IS_INTRA(mb_type))
2349 2983528 return 0;
2350
2351 8102814 fill_filter_caches_inter(h, sl, mb_type, top_xy, left_xy,
2352 top_type, left_type, mb_xy, 0);
2353
2/2
✓ Branch 0 taken 4254447 times.
✓ Branch 1 taken 3848367 times.
8102814 if (sl->list_count == 2)
2354 4254447 fill_filter_caches_inter(h, sl, mb_type, top_xy, left_xy,
2355 top_type, left_type, mb_xy, 1);
2356
2357 8102814 nnz = h->non_zero_count[mb_xy];
2358 8102814 nnz_cache = sl->non_zero_count_cache;
2359 8102814 AV_COPY32(&nnz_cache[4 + 8 * 1], &nnz[0]);
2360 8102814 AV_COPY32(&nnz_cache[4 + 8 * 2], &nnz[4]);
2361 8102814 AV_COPY32(&nnz_cache[4 + 8 * 3], &nnz[8]);
2362 8102814 AV_COPY32(&nnz_cache[4 + 8 * 4], &nnz[12]);
2363 8102814 sl->cbp = h->cbp_table[mb_xy];
2364
2365
2/2
✓ Branch 0 taken 7676113 times.
✓ Branch 1 taken 426701 times.
8102814 if (top_type) {
2366 7676113 nnz = h->non_zero_count[top_xy];
2367 7676113 AV_COPY32(&nnz_cache[4 + 8 * 0], &nnz[3 * 4]);
2368 }
2369
2370
2/2
✓ Branch 0 taken 7839300 times.
✓ Branch 1 taken 263514 times.
8102814 if (left_type[LTOP]) {
2371 7839300 nnz = h->non_zero_count[left_xy[LTOP]];
2372 7839300 nnz_cache[3 + 8 * 1] = nnz[3 + 0 * 4];
2373 7839300 nnz_cache[3 + 8 * 2] = nnz[3 + 1 * 4];
2374 7839300 nnz_cache[3 + 8 * 3] = nnz[3 + 2 * 4];
2375 7839300 nnz_cache[3 + 8 * 4] = nnz[3 + 3 * 4];
2376 }
2377
2378 /* CAVLC 8x8dct requires NNZ values for residual decoding that differ
2379 * from what the loop filter needs */
2380
4/4
✓ Branch 0 taken 2070968 times.
✓ Branch 1 taken 6031846 times.
✓ Branch 2 taken 246166 times.
✓ Branch 3 taken 1824802 times.
8102814 if (!CABAC(h) && h->ps.pps->transform_8x8_mode) {
2381
2/2
✓ Branch 0 taken 107789 times.
✓ Branch 1 taken 138377 times.
246166 if (IS_8x8DCT(top_type)) {
2382 107789 nnz_cache[4 + 8 * 0] =
2383 107789 nnz_cache[5 + 8 * 0] = (h->cbp_table[top_xy] & 0x4000) >> 12;
2384 107789 nnz_cache[6 + 8 * 0] =
2385 107789 nnz_cache[7 + 8 * 0] = (h->cbp_table[top_xy] & 0x8000) >> 12;
2386 }
2387
2/2
✓ Branch 0 taken 110456 times.
✓ Branch 1 taken 135710 times.
246166 if (IS_8x8DCT(left_type[LTOP])) {
2388 110456 nnz_cache[3 + 8 * 1] =
2389 110456 nnz_cache[3 + 8 * 2] = (h->cbp_table[left_xy[LTOP]] & 0x2000) >> 12; // FIXME check MBAFF
2390 }
2391
2/2
✓ Branch 0 taken 110556 times.
✓ Branch 1 taken 135610 times.
246166 if (IS_8x8DCT(left_type[LBOT])) {
2392 110556 nnz_cache[3 + 8 * 3] =
2393 110556 nnz_cache[3 + 8 * 4] = (h->cbp_table[left_xy[LBOT]] & 0x8000) >> 12; // FIXME check MBAFF
2394 }
2395
2396
2/2
✓ Branch 0 taken 104872 times.
✓ Branch 1 taken 141294 times.
246166 if (IS_8x8DCT(mb_type)) {
2397 104872 nnz_cache[scan8[0]] =
2398 104872 nnz_cache[scan8[1]] =
2399 104872 nnz_cache[scan8[2]] =
2400 104872 nnz_cache[scan8[3]] = (sl->cbp & 0x1000) >> 12;
2401
2402 104872 nnz_cache[scan8[0 + 4]] =
2403 104872 nnz_cache[scan8[1 + 4]] =
2404 104872 nnz_cache[scan8[2 + 4]] =
2405 104872 nnz_cache[scan8[3 + 4]] = (sl->cbp & 0x2000) >> 12;
2406
2407 104872 nnz_cache[scan8[0 + 8]] =
2408 104872 nnz_cache[scan8[1 + 8]] =
2409 104872 nnz_cache[scan8[2 + 8]] =
2410 104872 nnz_cache[scan8[3 + 8]] = (sl->cbp & 0x4000) >> 12;
2411
2412 104872 nnz_cache[scan8[0 + 12]] =
2413 104872 nnz_cache[scan8[1 + 12]] =
2414 104872 nnz_cache[scan8[2 + 12]] =
2415 104872 nnz_cache[scan8[3 + 12]] = (sl->cbp & 0x8000) >> 12;
2416 }
2417 }
2418
2419 8102814 return 0;
2420 }
2421
2422 403029 static void loop_filter(const H264Context *h, H264SliceContext *sl, int start_x, int end_x)
2423 {
2424 uint8_t *dest_y, *dest_cb, *dest_cr;
2425 int linesize, uvlinesize, mb_x, mb_y;
2426 403029 const int end_mb_y = sl->mb_y + FRAME_MBAFF(h);
2427 403029 const int old_slice_type = sl->slice_type;
2428 403029 const int pixel_shift = h->pixel_shift;
2429 403029 const int block_h = 16 >> h->chroma_y_shift;
2430
2431
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 403029 times.
403029 if (h->postpone_filter)
2432 return;
2433
2434
2/2
✓ Branch 0 taken 326061 times.
✓ Branch 1 taken 76968 times.
403029 if (sl->deblocking_filter) {
2435
2/2
✓ Branch 0 taken 11565868 times.
✓ Branch 1 taken 326061 times.
11891929 for (mb_x = start_x; mb_x < end_x; mb_x++)
2436
2/2
✓ Branch 0 taken 12576316 times.
✓ Branch 1 taken 11565868 times.
24142184 for (mb_y = end_mb_y - FRAME_MBAFF(h); mb_y <= end_mb_y; mb_y++) {
2437 int mb_xy, mb_type;
2438 12576316 mb_xy = sl->mb_xy = mb_x + mb_y * h->mb_stride;
2439 12576316 mb_type = h->cur_pic.mb_type[mb_xy];
2440
2441
2/2
✓ Branch 0 taken 2020896 times.
✓ Branch 1 taken 10555420 times.
12576316 if (FRAME_MBAFF(h))
2442 2020896 sl->mb_mbaff =
2443 2020896 sl->mb_field_decoding_flag = !!IS_INTERLACED(mb_type);
2444
2445 12576316 sl->mb_x = mb_x;
2446 12576316 sl->mb_y = mb_y;
2447 12576316 dest_y = h->cur_pic.f->data[0] +
2448 12576316 ((mb_x << pixel_shift) + mb_y * sl->linesize) * 16;
2449 25152632 dest_cb = h->cur_pic.f->data[1] +
2450
2/2
✓ Branch 0 taken 80328 times.
✓ Branch 1 taken 12495988 times.
12576316 (mb_x << pixel_shift) * (8 << CHROMA444(h)) +
2451 12576316 mb_y * sl->uvlinesize * block_h;
2452 25152632 dest_cr = h->cur_pic.f->data[2] +
2453
2/2
✓ Branch 0 taken 80328 times.
✓ Branch 1 taken 12495988 times.
12576316 (mb_x << pixel_shift) * (8 << CHROMA444(h)) +
2454 12576316 mb_y * sl->uvlinesize * block_h;
2455 // FIXME simplify above
2456
2457
2/2
✓ Branch 0 taken 4647154 times.
✓ Branch 1 taken 7929162 times.
12576316 if (MB_FIELD(sl)) {
2458 4647154 linesize = sl->mb_linesize = sl->linesize * 2;
2459 4647154 uvlinesize = sl->mb_uvlinesize = sl->uvlinesize * 2;
2460
2/2
✓ Branch 0 taken 2320838 times.
✓ Branch 1 taken 2326316 times.
4647154 if (mb_y & 1) { // FIXME move out of this function?
2461 2320838 dest_y -= sl->linesize * 15;
2462 2320838 dest_cb -= sl->uvlinesize * (block_h - 1);
2463 2320838 dest_cr -= sl->uvlinesize * (block_h - 1);
2464 }
2465 } else {
2466 7929162 linesize = sl->mb_linesize = sl->linesize;
2467 7929162 uvlinesize = sl->mb_uvlinesize = sl->uvlinesize;
2468 }
2469 12576316 backup_mb_border(h, sl, dest_y, dest_cb, dest_cr, linesize,
2470 uvlinesize, 0);
2471
2/2
✓ Branch 1 taken 1489974 times.
✓ Branch 2 taken 11086342 times.
12576316 if (fill_filter_caches(h, sl, mb_type))
2472 1489974 continue;
2473 11086342 sl->chroma_qp[0] = get_chroma_qp(h->ps.pps, 0, h->cur_pic.qscale_table[mb_xy]);
2474 11086342 sl->chroma_qp[1] = get_chroma_qp(h->ps.pps, 1, h->cur_pic.qscale_table[mb_xy]);
2475
2476
2/2
✓ Branch 0 taken 1728725 times.
✓ Branch 1 taken 9357617 times.
11086342 if (FRAME_MBAFF(h)) {
2477 1728725 ff_h264_filter_mb(h, sl, mb_x, mb_y, dest_y, dest_cb, dest_cr,
2478 linesize, uvlinesize);
2479 } else {
2480 9357617 ff_h264_filter_mb_fast(h, sl, mb_x, mb_y, dest_y, dest_cb,
2481 dest_cr, linesize, uvlinesize);
2482 }
2483 }
2484 }
2485 403029 sl->slice_type = old_slice_type;
2486 403029 sl->mb_x = end_x;
2487 403029 sl->mb_y = end_mb_y - FRAME_MBAFF(h);
2488 403029 sl->chroma_qp[0] = get_chroma_qp(h->ps.pps, 0, sl->qscale);
2489 403029 sl->chroma_qp[1] = get_chroma_qp(h->ps.pps, 1, sl->qscale);
2490 }
2491
2492 24028 static void predict_field_decoding_flag(const H264Context *h, H264SliceContext *sl)
2493 {
2494 24028 const int mb_xy = sl->mb_x + sl->mb_y * h->mb_stride;
2495 48056 int mb_type = (h->slice_table[mb_xy - 1] == sl->slice_num) ?
2496
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 24028 times.
48056 h->cur_pic.mb_type[mb_xy - 1] :
2497
2/2
✓ Branch 0 taken 23892 times.
✓ Branch 1 taken 136 times.
24028 (h->slice_table[mb_xy - h->mb_stride] == sl->slice_num) ?
2498 23892 h->cur_pic.mb_type[mb_xy - h->mb_stride] : 0;
2499 24028 sl->mb_mbaff = sl->mb_field_decoding_flag = IS_INTERLACED(mb_type) ? 1 : 0;
2500 24028 }
2501
2502 /**
2503 * Draw edges and report progress for the last MB row.
2504 */
2505 398469 static void decode_finish_row(const H264Context *h, H264SliceContext *sl)
2506 {
2507 398469 int top = 16 * (sl->mb_y >> FIELD_PICTURE(h));
2508 398469 int pic_height = 16 * h->mb_height >> FIELD_PICTURE(h);
2509 398469 int height = 16 << FRAME_MBAFF(h);
2510 398469 int deblock_border = (16 + 4) << FRAME_MBAFF(h);
2511
2512
2/2
✓ Branch 0 taken 322969 times.
✓ Branch 1 taken 75500 times.
398469 if (sl->deblocking_filter) {
2513
2/2
✓ Branch 0 taken 22434 times.
✓ Branch 1 taken 300535 times.
322969 if ((top + height) >= pic_height)
2514 22434 height += deblock_border;
2515 322969 top -= deblock_border;
2516 }
2517
2518
3/4
✓ Branch 0 taken 398469 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 22435 times.
✓ Branch 3 taken 376034 times.
398469 if (top >= pic_height || (top + height) < 0)
2519 22435 return;
2520
2521 376034 height = FFMIN(height, pic_height - top);
2522
2/2
✓ Branch 0 taken 22435 times.
✓ Branch 1 taken 353599 times.
376034 if (top < 0) {
2523 22435 height = top + height;
2524 22435 top = 0;
2525 }
2526
2527 376034 ff_h264_draw_horiz_band(h, sl, top, height);
2528
2529
3/4
✓ Branch 0 taken 242535 times.
✓ Branch 1 taken 133499 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 242535 times.
376034 if (h->droppable || h->er.error_occurred)
2530 133499 return;
2531
2532 242535 ff_thread_report_progress(&h->cur_pic_ptr->tf, top + height - 1,
2533 242535 h->picture_structure == PICT_BOTTOM_FIELD);
2534 }
2535
2536 37360 static void er_add_slice(H264SliceContext *sl,
2537 int startx, int starty,
2538 int endx, int endy, int status)
2539 {
2540
2/2
✓ Branch 0 taken 1200 times.
✓ Branch 1 taken 36160 times.
37360 if (!sl->h264->enable_er)
2541 1200 return;
2542
2543 if (CONFIG_ERROR_RESILIENCE) {
2544 36160 ff_er_add_slice(sl->er, startx, starty, endx, endy, status);
2545 }
2546 }
2547
2548 37360 static int decode_slice(struct AVCodecContext *avctx, void *arg)
2549 {
2550 37360 H264SliceContext *sl = arg;
2551 37360 const H264Context *h = sl->h264;
2552 37360 int lf_x_start = sl->mb_x;
2553 37360 int orig_deblock = sl->deblocking_filter;
2554 int ret;
2555
2556 37360 sl->linesize = h->cur_pic_ptr->f->linesize[0];
2557 37360 sl->uvlinesize = h->cur_pic_ptr->f->linesize[1];
2558
2559 37360 ret = alloc_scratch_buffers(sl, sl->linesize);
2560
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 37360 times.
37360 if (ret < 0)
2561 return ret;
2562
2563 37360 sl->mb_skip_run = -1;
2564
2565
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 37360 times.
37360 av_assert0(h->block_offset[15] == (4 * ((scan8[15] - scan8[0]) & 7) << h->pixel_shift) + 4 * sl->linesize * ((scan8[15] - scan8[0]) >> 3));
2566
2567
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 37360 times.
37360 if (h->postpone_filter)
2568 sl->deblocking_filter = 0;
2569
2570
4/4
✓ Branch 0 taken 32389 times.
✓ Branch 1 taken 4971 times.
✓ Branch 2 taken 8946 times.
✓ Branch 3 taken 23443 times.
37360 sl->is_complex = FRAME_MBAFF(h) || h->picture_structure != PICT_FRAME ||
2571 (CONFIG_GRAY && (h->flags & AV_CODEC_FLAG_GRAY));
2572
2573
5/6
✓ Branch 0 taken 36160 times.
✓ Branch 1 taken 1200 times.
✓ Branch 2 taken 27214 times.
✓ Branch 3 taken 8946 times.
✓ Branch 4 taken 27214 times.
✗ Branch 5 not taken.
37360 if (!(h->avctx->active_thread_type & FF_THREAD_SLICE) && h->picture_structure == PICT_FRAME && sl->er->error_status_table) {
2574 27214 const int start_i = av_clip(sl->resync_mb_x + sl->resync_mb_y * h->mb_width, 0, h->mb_num - 1);
2575
2/2
✓ Branch 0 taken 5956 times.
✓ Branch 1 taken 21258 times.
27214 if (start_i) {
2576 5956 int prev_status = sl->er->error_status_table[sl->er->mb_index2xy[start_i - 1]];
2577 5956 prev_status &= ~ VP_START;
2578
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5956 times.
5956 if (prev_status != (ER_MV_END | ER_DC_END | ER_AC_END))
2579 sl->er->error_occurred = 1;
2580 }
2581 }
2582
2583
2/2
✓ Branch 0 taken 20619 times.
✓ Branch 1 taken 16741 times.
37360 if (h->ps.pps->cabac) {
2584 /* realign */
2585 20619 align_get_bits(&sl->gb);
2586
2587 /* init cabac */
2588 41238 ret = ff_init_cabac_decoder(&sl->cabac,
2589 20619 sl->gb.buffer + get_bits_count(&sl->gb) / 8,
2590 20619 (get_bits_left(&sl->gb) + 7) / 8);
2591
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 20619 times.
20619 if (ret < 0)
2592 return ret;
2593
2594 20619 ff_h264_init_cabac_states(h, sl);
2595
2596 9356886 for (;;) {
2597 int ret, eos;
2598
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9377505 times.
9377505 if (sl->mb_x + sl->mb_y * h->mb_width >= sl->next_slice_idx) {
2599 av_log(h->avctx, AV_LOG_ERROR, "Slice overlaps with next at %d\n",
2600 sl->next_slice_idx);
2601 er_add_slice(sl, sl->resync_mb_x, sl->resync_mb_y, sl->mb_x,
2602 sl->mb_y, ER_MB_ERROR);
2603 return AVERROR_INVALIDDATA;
2604 }
2605
2606 9377505 ret = ff_h264_decode_mb_cabac(h, sl);
2607
2608
2/2
✓ Branch 0 taken 9377504 times.
✓ Branch 1 taken 1 times.
9377505 if (ret >= 0)
2609 9377504 ff_h264_hl_decode_mb(h, sl);
2610
2611 // FIXME optimal? or let mb_decode decode 16x32 ?
2612
4/4
✓ Branch 0 taken 9377504 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 902100 times.
✓ Branch 3 taken 8475404 times.
9377505 if (ret >= 0 && FRAME_MBAFF(h)) {
2613 902100 sl->mb_y++;
2614
2615 902100 ret = ff_h264_decode_mb_cabac(h, sl);
2616
2617
1/2
✓ Branch 0 taken 902100 times.
✗ Branch 1 not taken.
902100 if (ret >= 0)
2618 902100 ff_h264_hl_decode_mb(h, sl);
2619 902100 sl->mb_y--;
2620 }
2621 9377505 eos = get_cabac_terminate(&sl->cabac);
2622
2623
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9377505 times.
9377505 if ((h->workaround_bugs & FF_BUG_TRUNCATED) &&
2624 sl->cabac.bytestream > sl->cabac.bytestream_end + 2) {
2625 er_add_slice(sl, sl->resync_mb_x, sl->resync_mb_y, sl->mb_x - 1,
2626 sl->mb_y, ER_MB_END);
2627 if (sl->mb_x >= lf_x_start)
2628 loop_filter(h, sl, lf_x_start, sl->mb_x + 1);
2629 goto finish;
2630 }
2631
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 9377504 times.
9377505 if (sl->cabac.bytestream > sl->cabac.bytestream_end + 2 )
2632 1 av_log(h->avctx, AV_LOG_DEBUG, "bytestream overread %"PTRDIFF_SPECIFIER"\n", sl->cabac.bytestream_end - sl->cabac.bytestream);
2633
4/4
✓ Branch 0 taken 9377504 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 9377503 times.
9377505 if (ret < 0 || sl->cabac.bytestream > sl->cabac.bytestream_end + 4) {
2634 2 av_log(h->avctx, AV_LOG_ERROR,
2635 "error while decoding MB %d %d, bytestream %"PTRDIFF_SPECIFIER"\n",
2636 sl->mb_x, sl->mb_y,
2637 2 sl->cabac.bytestream_end - sl->cabac.bytestream);
2638 2 er_add_slice(sl, sl->resync_mb_x, sl->resync_mb_y, sl->mb_x,
2639 sl->mb_y, ER_MB_ERROR);
2640 2 return AVERROR_INVALIDDATA;
2641 }
2642
2643
2/2
✓ Branch 0 taken 237505 times.
✓ Branch 1 taken 9139998 times.
9377503 if (++sl->mb_x >= h->mb_width) {
2644 237505 loop_filter(h, sl, lf_x_start, sl->mb_x);
2645 237505 sl->mb_x = lf_x_start = 0;
2646 237505 decode_finish_row(h, sl);
2647 237505 ++sl->mb_y;
2648
4/4
✓ Branch 0 taken 216629 times.
✓ Branch 1 taken 20876 times.
✓ Branch 2 taken 59174 times.
✓ Branch 3 taken 157455 times.
237505 if (FIELD_OR_MBAFF_PICTURE(h)) {
2649 80050 ++sl->mb_y;
2650
4/4
✓ Branch 0 taken 20876 times.
✓ Branch 1 taken 59174 times.
✓ Branch 2 taken 19050 times.
✓ Branch 3 taken 1826 times.
80050 if (FRAME_MBAFF(h) && sl->mb_y < h->mb_height)
2651 19050 predict_field_decoding_flag(h, sl);
2652 }
2653 }
2654
2655
3/4
✓ Branch 0 taken 9356886 times.
✓ Branch 1 taken 20617 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 9356886 times.
9377503 if (eos || sl->mb_y >= h->mb_height) {
2656 ff_tlog(h->avctx, "slice end %d %d\n",
2657 get_bits_count(&sl->gb), sl->gb.size_in_bits);
2658 20617 er_add_slice(sl, sl->resync_mb_x, sl->resync_mb_y, sl->mb_x - 1,
2659 sl->mb_y, ER_MB_END);
2660
2/2
✓ Branch 0 taken 1382 times.
✓ Branch 1 taken 19235 times.
20617 if (sl->mb_x > lf_x_start)
2661 1382 loop_filter(h, sl, lf_x_start, sl->mb_x);
2662 20617 goto finish;
2663 }
2664 }
2665 } else {
2666 4575657 for (;;) {
2667 int ret;
2668
2669
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) {
2670 av_log(h->avctx, AV_LOG_ERROR, "Slice overlaps with next at %d\n",
2671 sl->next_slice_idx);
2672 er_add_slice(sl, sl->resync_mb_x, sl->resync_mb_y, sl->mb_x,
2673 sl->mb_y, ER_MB_ERROR);
2674 return AVERROR_INVALIDDATA;
2675 }
2676
2677 4592398 ret = ff_h264_decode_mb_cavlc(h, sl);
2678
2679
1/2
✓ Branch 0 taken 4592398 times.
✗ Branch 1 not taken.
4592398 if (ret >= 0)
2680 4592398 ff_h264_hl_decode_mb(h, sl);
2681
2682 // FIXME optimal? or let mb_decode decode 16x32 ?
2683
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)) {
2684 254472 sl->mb_y++;
2685 254472 ret = ff_h264_decode_mb_cavlc(h, sl);
2686
2687
1/2
✓ Branch 0 taken 254472 times.
✗ Branch 1 not taken.
254472 if (ret >= 0)
2688 254472 ff_h264_hl_decode_mb(h, sl);
2689 254472 sl->mb_y--;
2690 }
2691
2692
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4592398 times.
4592398 if (ret < 0) {
2693 av_log(h->avctx, AV_LOG_ERROR,
2694 "error while decoding MB %d %d\n", sl->mb_x, sl->mb_y);
2695 er_add_slice(sl, sl->resync_mb_x, sl->resync_mb_y, sl->mb_x,
2696 sl->mb_y, ER_MB_ERROR);
2697 return ret;
2698 }
2699
2700
2/2
✓ Branch 0 taken 160964 times.
✓ Branch 1 taken 4431434 times.
4592398 if (++sl->mb_x >= h->mb_width) {
2701 160964 loop_filter(h, sl, lf_x_start, sl->mb_x);
2702 160964 sl->mb_x = lf_x_start = 0;
2703 160964 decode_finish_row(h, sl);
2704 160964 ++sl->mb_y;
2705
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)) {
2706 32143 ++sl->mb_y;
2707
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)
2708 4978 predict_field_decoding_flag(h, sl);
2709 }
2710
2/2
✓ Branch 0 taken 12651 times.
✓ Branch 1 taken 148313 times.
160964 if (sl->mb_y >= h->mb_height) {
2711 ff_tlog(h->avctx, "slice end %d %d\n",
2712 get_bits_count(&sl->gb), sl->gb.size_in_bits);
2713
2714
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 12651 times.
12651 if ( get_bits_left(&sl->gb) == 0
2715 || get_bits_left(&sl->gb) > 0 && !(h->avctx->err_recognition & AV_EF_AGGRESSIVE)) {
2716 12651 er_add_slice(sl, sl->resync_mb_x, sl->resync_mb_y,
2717 12651 sl->mb_x - 1, sl->mb_y, ER_MB_END);
2718
2719 12651 goto finish;
2720 } else {
2721 er_add_slice(sl, sl->resync_mb_x, sl->resync_mb_y,
2722 sl->mb_x, sl->mb_y, ER_MB_END);
2723
2724 return AVERROR_INVALIDDATA;
2725 }
2726 }
2727 }
2728
2729
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) {
2730 ff_tlog(h->avctx, "slice end %d %d\n",
2731 get_bits_count(&sl->gb), sl->gb.size_in_bits);
2732
2733
1/2
✓ Branch 1 taken 4090 times.
✗ Branch 2 not taken.
4090 if (get_bits_left(&sl->gb) == 0) {
2734 4090 er_add_slice(sl, sl->resync_mb_x, sl->resync_mb_y,
2735 4090 sl->mb_x - 1, sl->mb_y, ER_MB_END);
2736
2/2
✓ Branch 0 taken 3178 times.
✓ Branch 1 taken 912 times.
4090 if (sl->mb_x > lf_x_start)
2737 3178 loop_filter(h, sl, lf_x_start, sl->mb_x);
2738
2739 4090 goto finish;
2740 } else {
2741 er_add_slice(sl, sl->resync_mb_x, sl->resync_mb_y, sl->mb_x,
2742 sl->mb_y, ER_MB_ERROR);
2743
2744 return AVERROR_INVALIDDATA;
2745 }
2746 }
2747 }
2748 }
2749
2750 37358 finish:
2751 37358 sl->deblocking_filter = orig_deblock;
2752 37358 return 0;
2753 }
2754
2755 /**
2756 * Call decode_slice() for each context.
2757 *
2758 * @param h h264 master context
2759 */
2760 65749 int ff_h264_execute_decode_slices(H264Context *h)
2761 {
2762 65749 AVCodecContext *const avctx = h->avctx;
2763 H264SliceContext *sl;
2764 65749 int context_count = h->nb_slice_ctx_queued;
2765 65749 int ret = 0;
2766 int i, j;
2767
2768 65749 h->slice_ctx[0].next_slice_idx = INT_MAX;
2769
2770
3/4
✓ Branch 0 taken 65749 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 28839 times.
✓ Branch 3 taken 36910 times.
65749 if (h->avctx->hwaccel || context_count < 1)
2771 28839 return 0;
2772
2773
2/4
✓ Branch 0 taken 36910 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 36910 times.
36910 av_assert0(context_count && h->slice_ctx[context_count - 1].mb_y < h->mb_height);
2774
2775
2/2
✓ Branch 0 taken 36460 times.
✓ Branch 1 taken 450 times.
36910 if (context_count == 1) {
2776
2777 36460 h->slice_ctx[0].next_slice_idx = h->mb_width * h->mb_height;
2778 36460 h->postpone_filter = 0;
2779
2780 36460 ret = decode_slice(avctx, &h->slice_ctx[0]);
2781 36460 h->mb_y = h->slice_ctx[0].mb_y;
2782
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 36458 times.
36460 if (ret < 0)
2783 2 goto finish;
2784 } else {
2785
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 450 times.
450 av_assert0(context_count > 0);
2786
2/2
✓ Branch 0 taken 900 times.
✓ Branch 1 taken 450 times.
1350 for (i = 0; i < context_count; i++) {
2787 900 int next_slice_idx = h->mb_width * h->mb_height;
2788 int slice_idx;
2789
2790 900 sl = &h->slice_ctx[i];
2791
2792 /* make sure none of those slices overlap */
2793 900 slice_idx = sl->mb_y * h->mb_width + sl->mb_x;
2794
2/2
✓ Branch 0 taken 1800 times.
✓ Branch 1 taken 900 times.
2700 for (j = 0; j < context_count; j++) {
2795 1800 H264SliceContext *sl2 = &h->slice_ctx[j];
2796 1800 int slice_idx2 = sl2->mb_y * h->mb_width + sl2->mb_x;
2797
2798
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)
2799 1350 continue;
2800 450 next_slice_idx = FFMIN(next_slice_idx, slice_idx2);
2801 }
2802 900 sl->next_slice_idx = next_slice_idx;
2803 }
2804
2805 450 avctx->execute(avctx, decode_slice, h->slice_ctx,
2806 NULL, context_count, sizeof(h->slice_ctx[0]));
2807
2808 /* pull back stuff from slices to master context */
2809 450 sl = &h->slice_ctx[context_count - 1];
2810 450 h->mb_y = sl->mb_y;
2811
2812
1/2
✓ Branch 0 taken 450 times.
✗ Branch 1 not taken.
450 if (h->postpone_filter) {
2813 h->postpone_filter = 0;
2814
2815 for (i = 0; i < context_count; i++) {
2816 int y_end, x_end;
2817
2818 sl = &h->slice_ctx[i];
2819 y_end = FFMIN(sl->mb_y + 1, h->mb_height);
2820 x_end = (sl->mb_y >= h->mb_height) ? h->mb_width : sl->mb_x;
2821
2822 for (j = sl->resync_mb_y; j < y_end; j += 1 + FIELD_OR_MBAFF_PICTURE(h)) {
2823 sl->mb_y = j;
2824 loop_filter(h, sl, j > sl->resync_mb_y ? 0 : sl->resync_mb_x,
2825 j == y_end - 1 ? x_end : h->mb_width);
2826 }
2827 }
2828 }
2829 }
2830
2831 450 finish:
2832 36910 h->nb_slice_ctx_queued = 0;
2833 36910 return ret;
2834 }
2835