FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/h264_slice.c
Date: 2025-04-25 22:50:00
Exec Total Coverage
Lines: 1374 1675 82.0%
Functions: 32 33 97.0%
Branches: 917 1257 73.0%

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