FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/h264_slice.c
Date: 2026-09-05 12:44:52
Exec Total Coverage
Lines: 1422 1736 81.9%
Functions: 34 35 97.1%
Branches: 946 1301 72.7%

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