FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/h264_slice.c
Date: 2026-09-25 23:13:28
Exec Total Coverage
Lines: 1426 1740 82.0%
Functions: 34 35 97.1%
Branches: 946 1301 72.7%

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