FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/h264_slice.c
Date: 2026-09-04 22:37:26
Exec Total Coverage
Lines: 1419 1732 81.9%
Functions: 34 35 97.1%
Branches: 943 1295 72.8%

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