FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/h264_slice.c
Date: 2024-12-12 01:08:13
Exec Total Coverage
Lines: 1373 1670 82.2%
Functions: 32 33 97.0%
Branches: 915 1253 73.0%

Line Branch Exec Source
1 /*
2 * H.26L/H.264/AVC/JVT/14496-10/... decoder
3 * Copyright (c) 2003 Michael Niedermayer <michaelni@gmx.at>
4 *
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 /**
23 * @file
24 * H.264 / AVC / MPEG-4 part10 codec.
25 * @author Michael Niedermayer <michaelni@gmx.at>
26 */
27
28 #include "config_components.h"
29
30 #include "libavutil/avassert.h"
31 #include "libavutil/mem.h"
32 #include "libavutil/pixdesc.h"
33 #include "libavutil/timecode.h"
34 #include "decode.h"
35 #include "cabac.h"
36 #include "cabac_functions.h"
37 #include "error_resilience.h"
38 #include "avcodec.h"
39 #include "h264.h"
40 #include "h264dec.h"
41 #include "h264data.h"
42 #include "h264chroma.h"
43 #include "h264_ps.h"
44 #include "golomb.h"
45 #include "mathops.h"
46 #include "mpegutils.h"
47 #include "rectangle.h"
48 #include "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 28986 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 1043496 times.
✓ Branch 1 taken 28986 times.
1072482 for (i = 0; i < H264_MAX_PICTURE_COUNT; i++) {
123
6/6
✓ Branch 0 taken 145943 times.
✓ Branch 1 taken 897553 times.
✓ Branch 2 taken 25353 times.
✓ Branch 3 taken 120590 times.
✓ Branch 4 taken 2612 times.
✓ Branch 5 taken 22741 times.
1043496 if (h->DPB[i].f->buf[0] && !h->DPB[i].reference &&
124
2/2
✓ Branch 0 taken 723 times.
✓ Branch 1 taken 1889 times.
2612 (remove_current || &h->DPB[i] != h->cur_pic_ptr)) {
125 23464 ff_h264_unref_picture(&h->DPB[i]);
126 }
127 }
128 28986 }
129
130 37374 static int alloc_scratch_buffers(H264SliceContext *sl, int linesize)
131 {
132 37374 const H264Context *h = sl->h264;
133 37374 int alloc_size = FFALIGN(FFABS(linesize) + 32, 32);
134
135 37374 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 37374 av_fast_malloc(&sl->edge_emu_buffer, &sl->edge_emu_buffer_allocated, alloc_size * 2 * 21);
139
140 37374 av_fast_mallocz(&sl->top_borders[0], &sl->top_borders_allocated[0],
141 37374 h->mb_width * 16 * 3 * sizeof(uint8_t) * 2);
142 37374 av_fast_mallocz(&sl->top_borders[1], &sl->top_borders_allocated[1],
143 37374 h->mb_width * 16 * 3 * sizeof(uint8_t) * 2);
144
145
2/4
✓ Branch 0 taken 37374 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 37374 times.
✗ Branch 3 not taken.
37374 if (!sl->bipred_scratchpad || !sl->edge_emu_buffer ||
146
2/4
✓ Branch 0 taken 37374 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 37374 times.
37374 !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 37374 return 0;
160 }
161
162 603 static int init_table_pools(H264Context *h)
163 {
164 603 const int big_mb_num = h->mb_stride * (h->mb_height + 1) + 1;
165 603 const int mb_array_size = h->mb_stride * h->mb_height;
166 603 const int b4_stride = h->mb_width * 4 + 1;
167 603 const int b4_array_size = b4_stride * h->mb_height * 4;
168
169 603 h->qscale_table_pool = ff_refstruct_pool_alloc(big_mb_num + h->mb_stride, 0);
170 603 h->mb_type_pool = ff_refstruct_pool_alloc((big_mb_num + h->mb_stride) *
171 sizeof(uint32_t), 0);
172 603 h->motion_val_pool = ff_refstruct_pool_alloc(2 * (b4_array_size + 4) *
173 sizeof(int16_t), 0);
174 603 h->ref_index_pool = ff_refstruct_pool_alloc(4 * mb_array_size, 0);
175
176
3/6
✓ Branch 0 taken 603 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 603 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 603 times.
✗ Branch 5 not taken.
603 if (!h->qscale_table_pool || !h->mb_type_pool || !h->motion_val_pool ||
177
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 603 times.
603 !h->ref_index_pool) {
178 ff_refstruct_pool_uninit(&h->qscale_table_pool);
179 ff_refstruct_pool_uninit(&h->mb_type_pool);
180 ff_refstruct_pool_uninit(&h->motion_val_pool);
181 ff_refstruct_pool_uninit(&h->ref_index_pool);
182 return AVERROR(ENOMEM);
183 }
184
185 603 return 0;
186 }
187
188 25619 static int alloc_picture(H264Context *h, H264Picture *pic)
189 {
190 25619 int i, ret = 0;
191
192
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 25619 times.
25619 av_assert0(!pic->f->data[0]);
193
194 25619 pic->tf.f = pic->f;
195 25619 ret = ff_thread_get_ext_buffer(h->avctx, &pic->tf,
196 25619 pic->reference ? AV_GET_BUFFER_FLAG_REF : 0);
197
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 25619 times.
25619 if (ret < 0)
198 goto fail;
199
200
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 25619 times.
25619 if (pic->needs_fg) {
201 pic->f_grain->format = pic->f->format;
202 pic->f_grain->width = pic->f->width;
203 pic->f_grain->height = pic->f->height;
204 ret = ff_thread_get_buffer(h->avctx, pic->f_grain, 0);
205 if (ret < 0)
206 goto fail;
207 }
208
209 25619 ret = ff_hwaccel_frame_priv_alloc(h->avctx, &pic->hwaccel_picture_private);
210
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 25619 times.
25619 if (ret < 0)
211 goto fail;
212
213
2/2
✓ Branch 0 taken 42 times.
✓ Branch 1 taken 25577 times.
25619 if (h->decode_error_flags_pool) {
214 42 pic->decode_error_flags = ff_refstruct_pool_get(h->decode_error_flags_pool);
215
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 42 times.
42 if (!pic->decode_error_flags)
216 goto fail;
217 42 atomic_init(pic->decode_error_flags, 0);
218 }
219
220 if (CONFIG_GRAY && !h->avctx->hwaccel && h->flags & AV_CODEC_FLAG_GRAY && pic->f->data[2]) {
221 int h_chroma_shift, v_chroma_shift;
222 av_pix_fmt_get_chroma_sub_sample(pic->f->format,
223 &h_chroma_shift, &v_chroma_shift);
224
225 for(i=0; i<AV_CEIL_RSHIFT(pic->f->height, v_chroma_shift); i++) {
226 memset(pic->f->data[1] + pic->f->linesize[1]*i,
227 0x80, AV_CEIL_RSHIFT(pic->f->width, h_chroma_shift));
228 memset(pic->f->data[2] + pic->f->linesize[2]*i,
229 0x80, AV_CEIL_RSHIFT(pic->f->width, h_chroma_shift));
230 }
231 }
232
233
2/2
✓ Branch 0 taken 603 times.
✓ Branch 1 taken 25016 times.
25619 if (!h->qscale_table_pool) {
234 603 ret = init_table_pools(h);
235
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 603 times.
603 if (ret < 0)
236 goto fail;
237 }
238
239 25619 pic->qscale_table_base = ff_refstruct_pool_get(h->qscale_table_pool);
240 25619 pic->mb_type_base = ff_refstruct_pool_get(h->mb_type_pool);
241
2/4
✓ Branch 0 taken 25619 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 25619 times.
25619 if (!pic->qscale_table_base || !pic->mb_type_base)
242 goto fail;
243
244 25619 pic->mb_type = pic->mb_type_base + 2 * h->mb_stride + 1;
245 25619 pic->qscale_table = pic->qscale_table_base + 2 * h->mb_stride + 1;
246
247
2/2
✓ Branch 0 taken 51238 times.
✓ Branch 1 taken 25619 times.
76857 for (i = 0; i < 2; i++) {
248 51238 pic->motion_val_base[i] = ff_refstruct_pool_get(h->motion_val_pool);
249 51238 pic->ref_index[i] = ff_refstruct_pool_get(h->ref_index_pool);
250
2/4
✓ Branch 0 taken 51238 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 51238 times.
51238 if (!pic->motion_val_base[i] || !pic->ref_index[i])
251 goto fail;
252
253 51238 pic->motion_val[i] = pic->motion_val_base[i] + 4;
254 }
255
256 25619 pic->pps = ff_refstruct_ref_c(h->ps.pps);
257
258 25619 pic->mb_width = h->mb_width;
259 25619 pic->mb_height = h->mb_height;
260 25619 pic->mb_stride = h->mb_stride;
261
262 25619 return 0;
263 fail:
264 ff_h264_unref_picture(pic);
265 return (ret < 0) ? ret : AVERROR(ENOMEM);
266 }
267
268 25619 static int find_unused_picture(const H264Context *h)
269 {
270 int i;
271
272
1/2
✓ Branch 0 taken 87007 times.
✗ Branch 1 not taken.
87007 for (i = 0; i < H264_MAX_PICTURE_COUNT; i++) {
273
2/2
✓ Branch 0 taken 25619 times.
✓ Branch 1 taken 61388 times.
87007 if (!h->DPB[i].f->buf[0])
274 25619 return i;
275 }
276 return AVERROR_INVALIDDATA;
277 }
278
279
280 #define IN_RANGE(a, b, size) (((void*)(a) >= (void*)(b)) && ((void*)(a) < (void*)((b) + (size))))
281
282 #define REBASE_PICTURE(pic, new_ctx, old_ctx) \
283 (((pic) && (pic) >= (old_ctx)->DPB && \
284 (pic) < (old_ctx)->DPB + H264_MAX_PICTURE_COUNT) ? \
285 &(new_ctx)->DPB[(pic) - (old_ctx)->DPB] : NULL)
286
287 174 static void copy_picture_range(H264Picture **to, H264Picture *const *from, int count,
288 H264Context *new_base, const H264Context *old_base)
289 {
290 int i;
291
292
2/2
✓ Branch 0 taken 4756 times.
✓ Branch 1 taken 174 times.
4930 for (i = 0; i < count; i++) {
293 av_assert1(!from[i] ||
294 IN_RANGE(from[i], old_base, 1) ||
295 IN_RANGE(from[i], old_base->DPB, H264_MAX_PICTURE_COUNT));
296
4/6
✓ Branch 0 taken 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);
297 }
298 174 }
299
300 24 static void color_frame(AVFrame *frame, const int c[4])
301 {
302 24 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(frame->format);
303
304
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 24 times.
24 av_assert0(desc->flags & AV_PIX_FMT_FLAG_PLANAR);
305
306
2/2
✓ Branch 0 taken 72 times.
✓ Branch 1 taken 24 times.
96 for (int p = 0; p < desc->nb_components; p++) {
307 72 uint8_t *dst = frame->data[p];
308
4/4
✓ Branch 0 taken 48 times.
✓ Branch 1 taken 24 times.
✓ Branch 2 taken 24 times.
✓ Branch 3 taken 24 times.
72 int is_chroma = p == 1 || p == 2;
309
2/2
✓ Branch 0 taken 48 times.
✓ Branch 1 taken 24 times.
72 int bytes = is_chroma ? AV_CEIL_RSHIFT(frame->width, desc->log2_chroma_w) : frame->width;
310
2/2
✓ Branch 0 taken 48 times.
✓ Branch 1 taken 24 times.
72 int height = is_chroma ? AV_CEIL_RSHIFT(frame->height, desc->log2_chroma_h) : frame->height;
311
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 72 times.
72 if (desc->comp[0].depth >= 9) {
312 ((uint16_t*)dst)[0] = c[p];
313 av_memcpy_backptr(dst + 2, 2, bytes - 2);
314 dst += frame->linesize[p];
315 for (int y = 1; y < height; y++) {
316 memcpy(dst, frame->data[p], 2*bytes);
317 dst += frame->linesize[p];
318 }
319 } else {
320
2/2
✓ Branch 0 taken 30208 times.
✓ Branch 1 taken 72 times.
30280 for (int y = 0; y < height; y++) {
321 30208 memset(dst, c[p], bytes);
322 30208 dst += frame->linesize[p];
323 }
324 }
325 }
326 24 }
327
328 static int h264_slice_header_init(H264Context *h);
329
330 58 int ff_h264_update_thread_context(AVCodecContext *dst,
331 const AVCodecContext *src)
332 {
333 58 H264Context *h = dst->priv_data, *h1 = src->priv_data;
334 58 int inited = h->context_initialized, err = 0;
335 58 int need_reinit = 0;
336 int i, ret;
337
338
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 58 times.
58 if (dst == src)
339 return 0;
340
341
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)
342 return AVERROR_INVALIDDATA;
343
344
2/2
✓ Branch 0 taken 42 times.
✓ Branch 1 taken 16 times.
58 if (inited &&
345
1/2
✓ Branch 0 taken 42 times.
✗ Branch 1 not taken.
42 (h->width != h1->width ||
346
1/2
✓ Branch 0 taken 42 times.
✗ Branch 1 not taken.
42 h->height != h1->height ||
347
1/2
✓ Branch 0 taken 42 times.
✗ Branch 1 not taken.
42 h->mb_width != h1->mb_width ||
348
1/2
✓ Branch 0 taken 42 times.
✗ Branch 1 not taken.
42 h->mb_height != h1->mb_height ||
349
1/2
✓ Branch 0 taken 42 times.
✗ Branch 1 not taken.
42 !h->ps.sps ||
350
1/2
✓ Branch 0 taken 42 times.
✗ Branch 1 not taken.
42 h->ps.sps->bit_depth_luma != h1->ps.sps->bit_depth_luma ||
351
1/2
✓ Branch 0 taken 42 times.
✗ Branch 1 not taken.
42 h->ps.sps->chroma_format_idc != h1->ps.sps->chroma_format_idc ||
352
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 42 times.
42 h->ps.sps->vui.matrix_coeffs != h1->ps.sps->vui.matrix_coeffs)) {
353 need_reinit = 1;
354 }
355
356 /* copy block_offset since frame_start may not be called */
357 58 memcpy(h->block_offset, h1->block_offset, sizeof(h->block_offset));
358
359 // SPS/PPS
360
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++)
361 1856 ff_refstruct_replace(&h->ps.sps_list[i], h1->ps.sps_list[i]);
362
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++)
363 14848 ff_refstruct_replace(&h->ps.pps_list[i], h1->ps.pps_list[i]);
364
365 58 ff_refstruct_replace(&h->ps.pps, h1->ps.pps);
366 58 h->ps.sps = h1->ps.sps;
367
368
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) {
369 16 h->width = h1->width;
370 16 h->height = h1->height;
371 16 h->mb_height = h1->mb_height;
372 16 h->mb_width = h1->mb_width;
373 16 h->mb_num = h1->mb_num;
374 16 h->mb_stride = h1->mb_stride;
375 16 h->b_stride = h1->b_stride;
376 16 h->x264_build = h1->x264_build;
377
378
2/4
✓ Branch 0 taken 16 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 16 times.
✗ Branch 3 not taken.
16 if (h->context_initialized || h1->context_initialized) {
379
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 16 times.
16 if ((err = h264_slice_header_init(h)) < 0) {
380 av_log(h->avctx, AV_LOG_ERROR, "h264_slice_header_init() failed");
381 return err;
382 }
383 }
384
385 /* copy block_offset since frame_start may not be called */
386 16 memcpy(h->block_offset, h1->block_offset, sizeof(h->block_offset));
387 }
388
389 58 h->width_from_caller = h1->width_from_caller;
390 58 h->height_from_caller = h1->height_from_caller;
391 58 h->first_field = h1->first_field;
392 58 h->picture_structure = h1->picture_structure;
393 58 h->mb_aff_frame = h1->mb_aff_frame;
394 58 h->droppable = h1->droppable;
395
396
2/2
✓ Branch 0 taken 2088 times.
✓ Branch 1 taken 58 times.
2146 for (i = 0; i < H264_MAX_PICTURE_COUNT; i++) {
397 2088 ret = ff_h264_replace_picture(&h->DPB[i], &h1->DPB[i]);
398
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2088 times.
2088 if (ret < 0)
399 return ret;
400 }
401
402
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);
403 58 ret = ff_h264_replace_picture(&h->cur_pic, &h1->cur_pic);
404
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 58 times.
58 if (ret < 0)
405 return ret;
406
407 58 h->enable_er = h1->enable_er;
408 58 h->workaround_bugs = h1->workaround_bugs;
409 58 h->droppable = h1->droppable;
410
411 // extradata/NAL handling
412 58 h->is_avc = h1->is_avc;
413 58 h->nal_length_size = h1->nal_length_size;
414
415 58 memcpy(&h->poc, &h1->poc, sizeof(h->poc));
416
417 58 memcpy(h->short_ref, h1->short_ref, sizeof(h->short_ref));
418 58 memcpy(h->long_ref, h1->long_ref, sizeof(h->long_ref));
419 58 memcpy(h->delayed_pic, h1->delayed_pic, sizeof(h->delayed_pic));
420 58 memcpy(h->last_pocs, h1->last_pocs, sizeof(h->last_pocs));
421
422 58 h->next_output_pic = h1->next_output_pic;
423 58 h->next_outputed_poc = h1->next_outputed_poc;
424 58 h->poc_offset = h1->poc_offset;
425
426 58 memcpy(h->mmco, h1->mmco, sizeof(h->mmco));
427 58 h->nb_mmco = h1->nb_mmco;
428 58 h->mmco_reset = h1->mmco_reset;
429 58 h->explicit_ref_marking = h1->explicit_ref_marking;
430 58 h->long_ref_count = h1->long_ref_count;
431 58 h->short_ref_count = h1->short_ref_count;
432
433 58 copy_picture_range(h->short_ref, h1->short_ref, 32, h, h1);
434 58 copy_picture_range(h->long_ref, h1->long_ref, 32, h, h1);
435 58 copy_picture_range(h->delayed_pic, h1->delayed_pic,
436 FF_ARRAY_ELEMS(h->delayed_pic), h, h1);
437
438 58 h->frame_recovered = h1->frame_recovered;
439
440 58 ret = ff_h2645_sei_ctx_replace(&h->sei.common, &h1->sei.common);
441
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 58 times.
58 if (ret < 0)
442 return ret;
443
444 58 h->sei.common.unregistered.x264_build = h1->sei.common.unregistered.x264_build;
445
446
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 42 times.
58 if (!h->cur_pic_ptr)
447 16 return 0;
448
449
2/2
✓ Branch 0 taken 27 times.
✓ Branch 1 taken 15 times.
42 if (!h->droppable) {
450 27 err = ff_h264_execute_ref_pic_marking(h);
451 27 h->poc.prev_poc_msb = h->poc.poc_msb;
452 27 h->poc.prev_poc_lsb = h->poc.poc_lsb;
453 }
454 42 h->poc.prev_frame_num_offset = h->poc.frame_num_offset;
455 42 h->poc.prev_frame_num = h->poc.frame_num;
456
457 42 h->recovery_frame = h1->recovery_frame;
458 42 h->non_gray = h1->non_gray;
459
460 42 return err;
461 }
462
463 46 int ff_h264_update_thread_context_for_user(AVCodecContext *dst,
464 const AVCodecContext *src)
465 {
466 46 H264Context *h = dst->priv_data;
467 46 const H264Context *h1 = src->priv_data;
468
469 46 h->is_avc = h1->is_avc;
470 46 h->nal_length_size = h1->nal_length_size;
471
472 46 return 0;
473 }
474
475 25619 static int h264_frame_start(H264Context *h)
476 {
477 H264Picture *pic;
478 int i, ret;
479 25619 const int pixel_shift = h->pixel_shift;
480
481
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 25619 times.
25619 if (!ff_thread_can_start_frame(h->avctx)) {
482 av_log(h->avctx, AV_LOG_ERROR, "Attempt to start a frame outside SETUP state\n");
483 return AVERROR_BUG;
484 }
485
486 25619 release_unused_pictures(h, 1);
487 25619 h->cur_pic_ptr = NULL;
488
489 25619 i = find_unused_picture(h);
490
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 25619 times.
25619 if (i < 0) {
491 av_log(h->avctx, AV_LOG_ERROR, "no frame buffer available\n");
492 return i;
493 }
494 25619 pic = &h->DPB[i];
495
496
2/2
✓ Branch 0 taken 17671 times.
✓ Branch 1 taken 7948 times.
25619 pic->reference = h->droppable ? 0 : h->picture_structure;
497 25619 pic->field_picture = h->picture_structure != PICT_FRAME;
498 25619 pic->frame_num = h->poc.frame_num;
499 /*
500 * Zero key_frame here; IDR markings per slice in frame or fields are ORed
501 * in later.
502 * See decode_nal_units().
503 */
504 25619 pic->f->flags &= ~AV_FRAME_FLAG_KEY;
505 25619 pic->mmco_reset = 0;
506 25619 pic->recovered = 0;
507 25619 pic->invalid_gap = 0;
508 25619 pic->sei_recovery_frame_cnt = h->sei.recovery_point.recovery_frame_cnt;
509
510 25619 pic->f->pict_type = h->slice_ctx[0].slice_type;
511
512 25619 pic->f->crop_left = h->crop_left;
513 25619 pic->f->crop_right = h->crop_right;
514 25619 pic->f->crop_top = h->crop_top;
515 25619 pic->f->crop_bottom = h->crop_bottom;
516
517 25619 pic->needs_fg =
518 25619 h->sei.common.film_grain_characteristics &&
519 h->sei.common.film_grain_characteristics->present &&
520
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 25619 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
25619 !h->avctx->hwaccel &&
521 !(h->avctx->export_side_data & AV_CODEC_EXPORT_DATA_FILM_GRAIN);
522
523
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 25619 times.
25619 if ((ret = alloc_picture(h, pic)) < 0)
524 return ret;
525
526 25619 h->cur_pic_ptr = pic;
527 25619 ff_h264_unref_picture(&h->cur_pic);
528 if (CONFIG_ERROR_RESILIENCE) {
529 25619 ff_h264_set_erpic(&h->er.cur_pic, NULL);
530 }
531
532
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 25619 times.
25619 if ((ret = ff_h264_ref_picture(&h->cur_pic, h->cur_pic_ptr)) < 0)
533 return ret;
534
535
2/2
✓ Branch 0 taken 25919 times.
✓ Branch 1 taken 25619 times.
51538 for (i = 0; i < h->nb_slice_ctx; i++) {
536 25919 h->slice_ctx[i].linesize = h->cur_pic_ptr->f->linesize[0];
537 25919 h->slice_ctx[i].uvlinesize = h->cur_pic_ptr->f->linesize[1];
538 }
539
540
2/2
✓ Branch 0 taken 25319 times.
✓ Branch 1 taken 300 times.
25619 if (CONFIG_ERROR_RESILIENCE && h->enable_er) {
541 25319 ff_er_frame_start(&h->er);
542 25319 ff_h264_set_erpic(&h->er.last_pic, NULL);
543 25319 ff_h264_set_erpic(&h->er.next_pic, NULL);
544 }
545
546
2/2
✓ Branch 0 taken 409904 times.
✓ Branch 1 taken 25619 times.
435523 for (i = 0; i < 16; i++) {
547 409904 h->block_offset[i] = (4 * ((scan8[i] - scan8[0]) & 7) << pixel_shift) + 4 * pic->f->linesize[0] * ((scan8[i] - scan8[0]) >> 3);
548 409904 h->block_offset[48 + i] = (4 * ((scan8[i] - scan8[0]) & 7) << pixel_shift) + 8 * pic->f->linesize[0] * ((scan8[i] - scan8[0]) >> 3);
549 }
550
2/2
✓ Branch 0 taken 409904 times.
✓ Branch 1 taken 25619 times.
435523 for (i = 0; i < 16; i++) {
551 409904 h->block_offset[16 + i] =
552 409904 h->block_offset[32 + i] = (4 * ((scan8[i] - scan8[0]) & 7) << pixel_shift) + 4 * pic->f->linesize[1] * ((scan8[i] - scan8[0]) >> 3);
553 409904 h->block_offset[48 + 16 + i] =
554 409904 h->block_offset[48 + 32 + i] = (4 * ((scan8[i] - scan8[0]) & 7) << pixel_shift) + 8 * pic->f->linesize[1] * ((scan8[i] - scan8[0]) >> 3);
555 }
556
557 /* We mark the current picture as non-reference after allocating it, so
558 * that if we break out due to an error it can be released automatically
559 * in the next ff_mpv_frame_start().
560 */
561 25619 h->cur_pic_ptr->reference = 0;
562
563 25619 h->cur_pic_ptr->field_poc[0] = h->cur_pic_ptr->field_poc[1] = INT_MAX;
564
565 25619 h->next_output_pic = NULL;
566
567 25619 h->postpone_filter = 0;
568
569
4/4
✓ Branch 0 taken 2870 times.
✓ Branch 1 taken 22749 times.
✓ Branch 2 taken 2357 times.
✓ Branch 3 taken 513 times.
25619 h->mb_aff_frame = h->ps.sps->mb_aff && (h->picture_structure == PICT_FRAME);
570
571
2/2
✓ Branch 0 taken 2062 times.
✓ Branch 1 taken 23557 times.
25619 if (h->sei.common.unregistered.x264_build >= 0)
572 2062 h->x264_build = h->sei.common.unregistered.x264_build;
573
574 assert(h->cur_pic_ptr->long_ref == 0);
575
576 25619 return 0;
577 }
578
579 12579880 static av_always_inline void backup_mb_border(const H264Context *h, H264SliceContext *sl,
580 const uint8_t *src_y,
581 const uint8_t *src_cb, const uint8_t *src_cr,
582 int linesize, int uvlinesize,
583 int simple)
584 {
585 uint8_t *top_border;
586 12579880 int top_idx = 1;
587 12579880 const int pixel_shift = h->pixel_shift;
588 12579880 int chroma444 = CHROMA444(h);
589 12579880 int chroma422 = CHROMA422(h);
590
591 12579880 src_y -= linesize;
592 12579880 src_cb -= uvlinesize;
593 12579880 src_cr -= uvlinesize;
594
595
3/4
✓ Branch 0 taken 12579880 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2022480 times.
✓ Branch 3 taken 10557400 times.
12579880 if (!simple && FRAME_MBAFF(h)) {
596
2/2
✓ Branch 0 taken 1011240 times.
✓ Branch 1 taken 1011240 times.
2022480 if (sl->mb_y & 1) {
597
2/2
✓ Branch 0 taken 748050 times.
✓ Branch 1 taken 263190 times.
1011240 if (!MB_MBAFF(sl)) {
598 748050 top_border = sl->top_borders[0][sl->mb_x];
599 748050 AV_COPY128(top_border, src_y + 15 * linesize);
600
2/2
✓ Branch 0 taken 213139 times.
✓ Branch 1 taken 534911 times.
748050 if (pixel_shift)
601 213139 AV_COPY128(top_border + 16, src_y + 15 * linesize + 16);
602 if (simple || !CONFIG_GRAY || !(h->flags & AV_CODEC_FLAG_GRAY)) {
603
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 748050 times.
748050 if (chroma444) {
604 if (pixel_shift) {
605 AV_COPY128(top_border + 32, src_cb + 15 * uvlinesize);
606 AV_COPY128(top_border + 48, src_cb + 15 * uvlinesize + 16);
607 AV_COPY128(top_border + 64, src_cr + 15 * uvlinesize);
608 AV_COPY128(top_border + 80, src_cr + 15 * uvlinesize + 16);
609 } else {
610 AV_COPY128(top_border + 16, src_cb + 15 * uvlinesize);
611 AV_COPY128(top_border + 32, src_cr + 15 * uvlinesize);
612 }
613
2/2
✓ Branch 0 taken 106510 times.
✓ Branch 1 taken 641540 times.
748050 } else if (chroma422) {
614
1/2
✓ Branch 0 taken 106510 times.
✗ Branch 1 not taken.
106510 if (pixel_shift) {
615 106510 AV_COPY128(top_border + 32, src_cb + 15 * uvlinesize);
616 106510 AV_COPY128(top_border + 48, src_cr + 15 * uvlinesize);
617 } else {
618 AV_COPY64(top_border + 16, src_cb + 15 * uvlinesize);
619 AV_COPY64(top_border + 24, src_cr + 15 * uvlinesize);
620 }
621 } else {
622
2/2
✓ Branch 0 taken 106629 times.
✓ Branch 1 taken 534911 times.
641540 if (pixel_shift) {
623 106629 AV_COPY128(top_border + 32, src_cb + 7 * uvlinesize);
624 106629 AV_COPY128(top_border + 48, src_cr + 7 * uvlinesize);
625 } else {
626 534911 AV_COPY64(top_border + 16, src_cb + 7 * uvlinesize);
627 534911 AV_COPY64(top_border + 24, src_cr + 7 * uvlinesize);
628 }
629 }
630 }
631 }
632
2/2
✓ Branch 0 taken 263190 times.
✓ Branch 1 taken 748050 times.
1011240 } else if (MB_MBAFF(sl)) {
633 263190 top_idx = 0;
634 } else
635 748050 return;
636 }
637
638 11831830 top_border = sl->top_borders[top_idx][sl->mb_x];
639 /* There are two lines saved, the line above the top macroblock
640 * of a pair, and the line above the bottom macroblock. */
641 11831830 AV_COPY128(top_border, src_y + 16 * linesize);
642
2/2
✓ Branch 0 taken 1381381 times.
✓ Branch 1 taken 10450449 times.
11831830 if (pixel_shift)
643 1381381 AV_COPY128(top_border + 16, src_y + 16 * linesize + 16);
644
645 if (simple || !CONFIG_GRAY || !(h->flags & AV_CODEC_FLAG_GRAY)) {
646
2/2
✓ Branch 0 taken 80328 times.
✓ Branch 1 taken 11751502 times.
11831830 if (chroma444) {
647
2/2
✓ Branch 0 taken 19800 times.
✓ Branch 1 taken 60528 times.
80328 if (pixel_shift) {
648 19800 AV_COPY128(top_border + 32, src_cb + 16 * linesize);
649 19800 AV_COPY128(top_border + 48, src_cb + 16 * linesize + 16);
650 19800 AV_COPY128(top_border + 64, src_cr + 16 * linesize);
651 19800 AV_COPY128(top_border + 80, src_cr + 16 * linesize + 16);
652 } else {
653 60528 AV_COPY128(top_border + 16, src_cb + 16 * linesize);
654 60528 AV_COPY128(top_border + 32, src_cr + 16 * linesize);
655 }
656
2/2
✓ Branch 0 taken 588635 times.
✓ Branch 1 taken 11162867 times.
11751502 } else if (chroma422) {
657
2/2
✓ Branch 0 taken 584675 times.
✓ Branch 1 taken 3960 times.
588635 if (pixel_shift) {
658 584675 AV_COPY128(top_border + 32, src_cb + 16 * uvlinesize);
659 584675 AV_COPY128(top_border + 48, src_cr + 16 * uvlinesize);
660 } else {
661 3960 AV_COPY64(top_border + 16, src_cb + 16 * uvlinesize);
662 3960 AV_COPY64(top_border + 24, src_cr + 16 * uvlinesize);
663 }
664 } else {
665
2/2
✓ Branch 0 taken 776906 times.
✓ Branch 1 taken 10385961 times.
11162867 if (pixel_shift) {
666 776906 AV_COPY128(top_border + 32, src_cb + 8 * uvlinesize);
667 776906 AV_COPY128(top_border + 48, src_cr + 8 * uvlinesize);
668 } else {
669 10385961 AV_COPY64(top_border + 16, src_cb + 8 * uvlinesize);
670 10385961 AV_COPY64(top_border + 24, src_cr + 8 * uvlinesize);
671 }
672 }
673 }
674 }
675
676 /**
677 * Initialize implicit_weight table.
678 * @param field 0/1 initialize the weight for interlaced MBAFF
679 * -1 initializes the rest
680 */
681 3837 static void implicit_weight_table(const H264Context *h, H264SliceContext *sl, int field)
682 {
683 int ref0, ref1, i, cur_poc, ref_start, ref_count0, ref_count1;
684
685
2/2
✓ Branch 0 taken 7674 times.
✓ Branch 1 taken 3837 times.
11511 for (i = 0; i < 2; i++) {
686 7674 sl->pwt.luma_weight_flag[i] = 0;
687 7674 sl->pwt.chroma_weight_flag[i] = 0;
688 }
689
690
2/2
✓ Branch 0 taken 2127 times.
✓ Branch 1 taken 1710 times.
3837 if (field < 0) {
691
1/2
✓ Branch 0 taken 2127 times.
✗ Branch 1 not taken.
2127 if (h->picture_structure == PICT_FRAME) {
692 2127 cur_poc = h->cur_pic_ptr->poc;
693 } else {
694 cur_poc = h->cur_pic_ptr->field_poc[h->picture_structure - 1];
695 }
696
6/6
✓ Branch 0 taken 952 times.
✓ Branch 1 taken 1175 times.
✓ Branch 2 taken 642 times.
✓ Branch 3 taken 310 times.
✓ Branch 4 taken 632 times.
✓ Branch 5 taken 10 times.
2127 if (sl->ref_count[0] == 1 && sl->ref_count[1] == 1 && !FRAME_MBAFF(h) &&
697
2/2
✓ Branch 0 taken 416 times.
✓ Branch 1 taken 216 times.
632 sl->ref_list[0][0].poc + (int64_t)sl->ref_list[1][0].poc == 2LL * cur_poc) {
698 416 sl->pwt.use_weight = 0;
699 416 sl->pwt.use_weight_chroma = 0;
700 416 return;
701 }
702 1711 ref_start = 0;
703 1711 ref_count0 = sl->ref_count[0];
704 1711 ref_count1 = sl->ref_count[1];
705 } else {
706 1710 cur_poc = h->cur_pic_ptr->field_poc[field];
707 1710 ref_start = 16;
708 1710 ref_count0 = 16 + 2 * sl->ref_count[0];
709 1710 ref_count1 = 16 + 2 * sl->ref_count[1];
710 }
711
712 3421 sl->pwt.use_weight = 2;
713 3421 sl->pwt.use_weight_chroma = 2;
714 3421 sl->pwt.luma_log2_weight_denom = 5;
715 3421 sl->pwt.chroma_log2_weight_denom = 5;
716
717
2/2
✓ Branch 0 taken 11779 times.
✓ Branch 1 taken 3421 times.
15200 for (ref0 = ref_start; ref0 < ref_count0; ref0++) {
718 11779 int64_t poc0 = sl->ref_list[0][ref0].poc;
719
2/2
✓ Branch 0 taken 21868 times.
✓ Branch 1 taken 11779 times.
33647 for (ref1 = ref_start; ref1 < ref_count1; ref1++) {
720 21868 int w = 32;
721
2/4
✓ Branch 0 taken 21868 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 21868 times.
✗ Branch 3 not taken.
21868 if (!sl->ref_list[0][ref0].parent->long_ref && !sl->ref_list[1][ref1].parent->long_ref) {
722 21868 int poc1 = sl->ref_list[1][ref1].poc;
723 21868 int td = av_clip_int8(poc1 - poc0);
724
2/2
✓ Branch 0 taken 21801 times.
✓ Branch 1 taken 67 times.
21868 if (td) {
725 21801 int tb = av_clip_int8(cur_poc - poc0);
726 21801 int tx = (16384 + (FFABS(td) >> 1)) / td;
727 21801 int dist_scale_factor = (tb * tx + 32) >> 8;
728
3/4
✓ Branch 0 taken 21791 times.
✓ Branch 1 taken 10 times.
✓ Branch 2 taken 21791 times.
✗ Branch 3 not taken.
21801 if (dist_scale_factor >= -64 && dist_scale_factor <= 128)
729 21791 w = 64 - dist_scale_factor;
730 }
731 }
732
2/2
✓ Branch 0 taken 5988 times.
✓ Branch 1 taken 15880 times.
21868 if (field < 0) {
733 5988 sl->pwt.implicit_weight[ref0][ref1][0] =
734 5988 sl->pwt.implicit_weight[ref0][ref1][1] = w;
735 } else {
736 15880 sl->pwt.implicit_weight[ref0][ref1][field] = w;
737 }
738 }
739 }
740 }
741
742 /**
743 * initialize scan tables
744 */
745 603 static void init_scan_tables(H264Context *h)
746 {
747 int i;
748
2/2
✓ Branch 0 taken 9648 times.
✓ Branch 1 taken 603 times.
10251 for (i = 0; i < 16; i++) {
749 #define TRANSPOSE(x) ((x) >> 2) | (((x) << 2) & 0xF)
750 9648 h->zigzag_scan[i] = TRANSPOSE(ff_zigzag_scan[i]);
751 9648 h->field_scan[i] = TRANSPOSE(field_scan[i]);
752 #undef TRANSPOSE
753 }
754
2/2
✓ Branch 0 taken 38592 times.
✓ Branch 1 taken 603 times.
39195 for (i = 0; i < 64; i++) {
755 #define TRANSPOSE(x) ((x) >> 3) | (((x) & 7) << 3)
756 38592 h->zigzag_scan8x8[i] = TRANSPOSE(ff_zigzag_direct[i]);
757 38592 h->zigzag_scan8x8_cavlc[i] = TRANSPOSE(zigzag_scan8x8_cavlc[i]);
758 38592 h->field_scan8x8[i] = TRANSPOSE(field_scan8x8[i]);
759 38592 h->field_scan8x8_cavlc[i] = TRANSPOSE(field_scan8x8_cavlc[i]);
760 #undef TRANSPOSE
761 }
762
2/2
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 589 times.
603 if (h->ps.sps->transform_bypass) { // FIXME same ugly
763 14 memcpy(h->zigzag_scan_q0 , ff_zigzag_scan , sizeof(h->zigzag_scan_q0 ));
764 14 memcpy(h->zigzag_scan8x8_q0 , ff_zigzag_direct , sizeof(h->zigzag_scan8x8_q0 ));
765 14 memcpy(h->zigzag_scan8x8_cavlc_q0 , zigzag_scan8x8_cavlc , sizeof(h->zigzag_scan8x8_cavlc_q0));
766 14 memcpy(h->field_scan_q0 , field_scan , sizeof(h->field_scan_q0 ));
767 14 memcpy(h->field_scan8x8_q0 , field_scan8x8 , sizeof(h->field_scan8x8_q0 ));
768 14 memcpy(h->field_scan8x8_cavlc_q0 , field_scan8x8_cavlc , sizeof(h->field_scan8x8_cavlc_q0 ));
769 } else {
770 589 memcpy(h->zigzag_scan_q0 , h->zigzag_scan , sizeof(h->zigzag_scan_q0 ));
771 589 memcpy(h->zigzag_scan8x8_q0 , h->zigzag_scan8x8 , sizeof(h->zigzag_scan8x8_q0 ));
772 589 memcpy(h->zigzag_scan8x8_cavlc_q0 , h->zigzag_scan8x8_cavlc , sizeof(h->zigzag_scan8x8_cavlc_q0));
773 589 memcpy(h->field_scan_q0 , h->field_scan , sizeof(h->field_scan_q0 ));
774 589 memcpy(h->field_scan8x8_q0 , h->field_scan8x8 , sizeof(h->field_scan8x8_q0 ));
775 589 memcpy(h->field_scan8x8_cavlc_q0 , h->field_scan8x8_cavlc , sizeof(h->field_scan8x8_cavlc_q0 ));
776 }
777 603 }
778
779 28560 static enum AVPixelFormat get_pixel_format(H264Context *h, int force_callback)
780 {
781 #define HWACCEL_MAX (CONFIG_H264_DXVA2_HWACCEL + \
782 (CONFIG_H264_D3D11VA_HWACCEL * 2) + \
783 CONFIG_H264_D3D12VA_HWACCEL + \
784 CONFIG_H264_NVDEC_HWACCEL + \
785 CONFIG_H264_VAAPI_HWACCEL + \
786 CONFIG_H264_VIDEOTOOLBOX_HWACCEL + \
787 CONFIG_H264_VDPAU_HWACCEL + \
788 CONFIG_H264_VULKAN_HWACCEL)
789 28560 enum AVPixelFormat pix_fmts[HWACCEL_MAX + 2], *fmt = pix_fmts;
790
791
3/6
✓ Branch 0 taken 155 times.
✓ Branch 1 taken 372 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 28033 times.
✗ Branch 5 not taken.
28560 switch (h->ps.sps->bit_depth_luma) {
792 155 case 9:
793
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 155 times.
155 if (CHROMA444(h)) {
794 if (h->avctx->colorspace == AVCOL_SPC_RGB) {
795 *fmt++ = AV_PIX_FMT_GBRP9;
796 } else
797 *fmt++ = AV_PIX_FMT_YUV444P9;
798
2/2
✓ Branch 0 taken 52 times.
✓ Branch 1 taken 103 times.
155 } else if (CHROMA422(h))
799 52 *fmt++ = AV_PIX_FMT_YUV422P9;
800 else
801 103 *fmt++ = AV_PIX_FMT_YUV420P9;
802 155 break;
803 372 case 10:
804 #if CONFIG_H264_VIDEOTOOLBOX_HWACCEL
805 if (h->avctx->colorspace != AVCOL_SPC_RGB)
806 *fmt++ = AV_PIX_FMT_VIDEOTOOLBOX;
807 #endif
808 #if CONFIG_H264_VULKAN_HWACCEL
809 *fmt++ = AV_PIX_FMT_VULKAN;
810 #endif
811
2/2
✓ Branch 0 taken 51 times.
✓ Branch 1 taken 321 times.
372 if (CHROMA444(h)) {
812
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 51 times.
51 if (h->avctx->colorspace == AVCOL_SPC_RGB) {
813 *fmt++ = AV_PIX_FMT_GBRP10;
814 } else
815 51 *fmt++ = AV_PIX_FMT_YUV444P10;
816
2/2
✓ Branch 0 taken 164 times.
✓ Branch 1 taken 157 times.
321 } else if (CHROMA422(h))
817 164 *fmt++ = AV_PIX_FMT_YUV422P10;
818 else {
819 #if CONFIG_H264_VAAPI_HWACCEL
820 // Just add as candidate. Whether VAProfileH264High10 usable or
821 // not is decided by vaapi_decode_make_config() defined in FFmpeg
822 // and vaQueryCodingProfile() defined in libva.
823 157 *fmt++ = AV_PIX_FMT_VAAPI;
824 #endif
825 157 *fmt++ = AV_PIX_FMT_YUV420P10;
826 }
827 372 break;
828 case 12:
829 #if CONFIG_H264_VULKAN_HWACCEL
830 *fmt++ = AV_PIX_FMT_VULKAN;
831 #endif
832 if (CHROMA444(h)) {
833 if (h->avctx->colorspace == AVCOL_SPC_RGB) {
834 *fmt++ = AV_PIX_FMT_GBRP12;
835 } else
836 *fmt++ = AV_PIX_FMT_YUV444P12;
837 } else if (CHROMA422(h))
838 *fmt++ = AV_PIX_FMT_YUV422P12;
839 else
840 *fmt++ = AV_PIX_FMT_YUV420P12;
841 break;
842 case 14:
843 if (CHROMA444(h)) {
844 if (h->avctx->colorspace == AVCOL_SPC_RGB) {
845 *fmt++ = AV_PIX_FMT_GBRP14;
846 } else
847 *fmt++ = AV_PIX_FMT_YUV444P14;
848 } else if (CHROMA422(h))
849 *fmt++ = AV_PIX_FMT_YUV422P14;
850 else
851 *fmt++ = AV_PIX_FMT_YUV420P14;
852 break;
853 28033 case 8:
854 #if CONFIG_H264_VDPAU_HWACCEL
855 28033 *fmt++ = AV_PIX_FMT_VDPAU;
856 #endif
857 #if CONFIG_H264_VULKAN_HWACCEL
858 *fmt++ = AV_PIX_FMT_VULKAN;
859 #endif
860 #if CONFIG_H264_NVDEC_HWACCEL
861 *fmt++ = AV_PIX_FMT_CUDA;
862 #endif
863 #if CONFIG_H264_VIDEOTOOLBOX_HWACCEL
864 if (h->avctx->colorspace != AVCOL_SPC_RGB)
865 *fmt++ = AV_PIX_FMT_VIDEOTOOLBOX;
866 #endif
867
2/2
✓ Branch 0 taken 484 times.
✓ Branch 1 taken 27549 times.
28033 if (CHROMA444(h)) {
868
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 484 times.
484 if (h->avctx->colorspace == AVCOL_SPC_RGB)
869 *fmt++ = AV_PIX_FMT_GBRP;
870
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 482 times.
484 else if (h->avctx->color_range == AVCOL_RANGE_JPEG)
871 2 *fmt++ = AV_PIX_FMT_YUVJ444P;
872 else
873 482 *fmt++ = AV_PIX_FMT_YUV444P;
874
2/2
✓ Branch 0 taken 22 times.
✓ Branch 1 taken 27527 times.
27549 } else if (CHROMA422(h)) {
875
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 22 times.
22 if (h->avctx->color_range == AVCOL_RANGE_JPEG)
876 *fmt++ = AV_PIX_FMT_YUVJ422P;
877 else
878 22 *fmt++ = AV_PIX_FMT_YUV422P;
879 } else {
880 #if CONFIG_H264_DXVA2_HWACCEL
881 *fmt++ = AV_PIX_FMT_DXVA2_VLD;
882 #endif
883 #if CONFIG_H264_D3D11VA_HWACCEL
884 *fmt++ = AV_PIX_FMT_D3D11VA_VLD;
885 *fmt++ = AV_PIX_FMT_D3D11;
886 #endif
887 #if CONFIG_H264_D3D12VA_HWACCEL
888 *fmt++ = AV_PIX_FMT_D3D12;
889 #endif
890 #if CONFIG_H264_VAAPI_HWACCEL
891 27527 *fmt++ = AV_PIX_FMT_VAAPI;
892 #endif
893
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 27524 times.
27527 if (h->avctx->color_range == AVCOL_RANGE_JPEG)
894 3 *fmt++ = AV_PIX_FMT_YUVJ420P;
895 else
896 27524 *fmt++ = AV_PIX_FMT_YUV420P;
897 }
898 28033 break;
899 default:
900 av_log(h->avctx, AV_LOG_ERROR,
901 "Unsupported bit depth %d\n", h->ps.sps->bit_depth_luma);
902 return AVERROR_INVALIDDATA;
903 }
904
905 28560 *fmt = AV_PIX_FMT_NONE;
906
907
2/2
✓ Branch 0 taken 84277 times.
✓ Branch 1 taken 590 times.
84867 for (int i = 0; pix_fmts[i] != AV_PIX_FMT_NONE; i++)
908
4/4
✓ Branch 0 taken 28226 times.
✓ Branch 1 taken 56051 times.
✓ Branch 2 taken 27970 times.
✓ Branch 3 taken 256 times.
84277 if (pix_fmts[i] == h->avctx->pix_fmt && !force_callback)
909 27970 return pix_fmts[i];
910 590 return ff_get_format(h->avctx, pix_fmts);
911 }
912
913 /* export coded and cropped frame dimensions to AVCodecContext */
914 28301 static void init_dimensions(H264Context *h)
915 {
916 28301 const SPS *sps = h->ps.sps;
917 28301 int cr = sps->crop_right;
918 28301 int cl = sps->crop_left;
919 28301 int ct = sps->crop_top;
920 28301 int cb = sps->crop_bottom;
921 28301 int width = h->width - (cr + cl);
922 28301 int height = h->height - (ct + cb);
923
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 28301 times.
28301 av_assert0(sps->crop_right + sps->crop_left < (unsigned)h->width);
924
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 28301 times.
28301 av_assert0(sps->crop_top + sps->crop_bottom < (unsigned)h->height);
925
926 /* handle container cropping */
927
3/4
✓ Branch 0 taken 26047 times.
✓ Branch 1 taken 2254 times.
✓ Branch 2 taken 26047 times.
✗ Branch 3 not taken.
28301 if (h->width_from_caller > 0 && h->height_from_caller > 0 &&
928
3/4
✓ Branch 0 taken 26046 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 26046 times.
✗ Branch 3 not taken.
26047 !sps->crop_top && !sps->crop_left &&
929
2/2
✓ Branch 0 taken 26037 times.
✓ Branch 1 taken 9 times.
26046 FFALIGN(h->width_from_caller, 16) == FFALIGN(width, 16) &&
930
1/2
✓ Branch 0 taken 26037 times.
✗ Branch 1 not taken.
26037 FFALIGN(h->height_from_caller, 16) == FFALIGN(height, 16) &&
931
2/2
✓ Branch 0 taken 26036 times.
✓ Branch 1 taken 1 times.
26037 h->width_from_caller <= width &&
932
2/2
✓ Branch 0 taken 26034 times.
✓ Branch 1 taken 2 times.
26036 h->height_from_caller <= height) {
933 26034 width = h->width_from_caller;
934 26034 height = h->height_from_caller;
935 26034 cl = 0;
936 26034 ct = 0;
937 26034 cr = h->width - width;
938 26034 cb = h->height - height;
939 } else {
940 2267 h->width_from_caller = 0;
941 2267 h->height_from_caller = 0;
942 }
943
944 28301 h->avctx->coded_width = h->width;
945 28301 h->avctx->coded_height = h->height;
946 28301 h->avctx->width = width;
947 28301 h->avctx->height = height;
948 28301 h->crop_right = cr;
949 28301 h->crop_left = cl;
950 28301 h->crop_top = ct;
951 28301 h->crop_bottom = cb;
952 28301 }
953
954 603 static int h264_slice_header_init(H264Context *h)
955 {
956 603 const SPS *sps = h->ps.sps;
957 int i, ret;
958
959
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 603 times.
603 if (!sps) {
960 ret = AVERROR_INVALIDDATA;
961 goto fail;
962 }
963
964 603 ff_set_sar(h->avctx, sps->vui.sar);
965 603 av_pix_fmt_get_chroma_sub_sample(h->avctx->pix_fmt,
966 &h->chroma_x_shift, &h->chroma_y_shift);
967
968
2/2
✓ Branch 0 taken 183 times.
✓ Branch 1 taken 420 times.
603 if (sps->timing_info_present_flag) {
969 183 int64_t den = sps->time_scale;
970
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 183 times.
183 if (h->x264_build < 44U)
971 den *= 2;
972 183 av_reduce(&h->avctx->framerate.den, &h->avctx->framerate.num,
973 183 sps->num_units_in_tick * 2, den, 1 << 30);
974 }
975
976 603 ff_h264_free_tables(h);
977
978 603 h->first_field = 0;
979 603 h->prev_interlaced_frame = 1;
980
981 603 init_scan_tables(h);
982 603 ret = ff_h264_alloc_tables(h);
983
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 603 times.
603 if (ret < 0) {
984 av_log(h->avctx, AV_LOG_ERROR, "Could not allocate memory\n");
985 goto fail;
986 }
987
988
2/4
✓ Branch 0 taken 603 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 603 times.
✗ Branch 3 not taken.
603 if (sps->bit_depth_luma < 8 || sps->bit_depth_luma > 14 ||
989
2/4
✓ Branch 0 taken 603 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 603 times.
603 sps->bit_depth_luma == 11 || sps->bit_depth_luma == 13
990 ) {
991 av_log(h->avctx, AV_LOG_ERROR, "Unsupported bit depth %d\n",
992 sps->bit_depth_luma);
993 ret = AVERROR_INVALIDDATA;
994 goto fail;
995 }
996
997 603 h->cur_bit_depth_luma =
998 603 h->avctx->bits_per_raw_sample = sps->bit_depth_luma;
999 603 h->cur_chroma_format_idc = sps->chroma_format_idc;
1000 603 h->pixel_shift = sps->bit_depth_luma > 8;
1001 603 h->chroma_format_idc = sps->chroma_format_idc;
1002 603 h->bit_depth_luma = sps->bit_depth_luma;
1003
1004 603 ff_h264dsp_init(&h->h264dsp, sps->bit_depth_luma,
1005 603 sps->chroma_format_idc);
1006 603 ff_h264chroma_init(&h->h264chroma, sps->bit_depth_chroma);
1007 603 ff_h264qpel_init(&h->h264qpel, sps->bit_depth_luma);
1008 603 ff_h264_pred_init(&h->hpc, AV_CODEC_ID_H264, sps->bit_depth_luma,
1009 603 sps->chroma_format_idc);
1010 603 ff_videodsp_init(&h->vdsp, sps->bit_depth_luma);
1011
1012
2/2
✓ Branch 0 taken 602 times.
✓ Branch 1 taken 1 times.
603 if (!HAVE_THREADS || !(h->avctx->active_thread_type & FF_THREAD_SLICE)) {
1013 602 ff_h264_slice_context_init(h, &h->slice_ctx[0]);
1014 } else {
1015
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 times.
3 for (i = 0; i < h->nb_slice_ctx; i++) {
1016 2 H264SliceContext *sl = &h->slice_ctx[i];
1017
1018 2 sl->h264 = h;
1019 2 sl->intra4x4_pred_mode = h->intra4x4_pred_mode + i * 8 * 2 * h->mb_stride;
1020 2 sl->mvd_table[0] = h->mvd_table[0] + i * 8 * 2 * h->mb_stride;
1021 2 sl->mvd_table[1] = h->mvd_table[1] + i * 8 * 2 * h->mb_stride;
1022
1023 2 ff_h264_slice_context_init(h, sl);
1024 }
1025 }
1026
1027 603 h->context_initialized = 1;
1028
1029 603 return 0;
1030 fail:
1031 ff_h264_free_tables(h);
1032 h->context_initialized = 0;
1033 return ret;
1034 }
1035
1036 55946 static enum AVPixelFormat non_j_pixfmt(enum AVPixelFormat a)
1037 {
1038
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 55944 times.
55946 switch (a) {
1039 2 case AV_PIX_FMT_YUVJ420P: return AV_PIX_FMT_YUV420P;
1040 case AV_PIX_FMT_YUVJ422P: return AV_PIX_FMT_YUV422P;
1041 case AV_PIX_FMT_YUVJ444P: return AV_PIX_FMT_YUV444P;
1042 55944 default:
1043 55944 return a;
1044 }
1045 }
1046
1047 28301 static int h264_init_ps(H264Context *h, const H264SliceContext *sl, int first_slice)
1048 {
1049 const SPS *sps;
1050 28301 int needs_reinit = 0, must_reinit, ret;
1051
1052
2/2
✓ Branch 0 taken 28107 times.
✓ Branch 1 taken 194 times.
28301 if (first_slice)
1053 28107 ff_refstruct_replace(&h->ps.pps, h->ps.pps_list[sl->pps_id]);
1054
1055
2/2
✓ Branch 0 taken 589 times.
✓ Branch 1 taken 27712 times.
28301 if (h->ps.sps != h->ps.pps->sps) {
1056 589 h->ps.sps = h->ps.pps->sps;
1057
1058
2/2
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 582 times.
589 if (h->mb_width != h->ps.sps->mb_width ||
1059
1/2
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
7 h->mb_height != h->ps.sps->mb_height ||
1060
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 1 times.
7 h->cur_bit_depth_luma != h->ps.sps->bit_depth_luma ||
1061
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 5 times.
6 h->cur_chroma_format_idc != h->ps.sps->chroma_format_idc
1062 )
1063 584 needs_reinit = 1;
1064
1065
2/2
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 578 times.
589 if (h->bit_depth_luma != h->ps.sps->bit_depth_luma ||
1066
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 10 times.
11 h->chroma_format_idc != h->ps.sps->chroma_format_idc)
1067 579 needs_reinit = 1;
1068 }
1069 28301 sps = h->ps.sps;
1070
1071
2/2
✓ Branch 0 taken 27722 times.
✓ Branch 1 taken 579 times.
56023 must_reinit = (h->context_initialized &&
1072
2/2
✓ Branch 0 taken 27716 times.
✓ Branch 1 taken 6 times.
27722 ( 16*sps->mb_width != h->avctx->coded_width
1073
1/2
✓ Branch 0 taken 27716 times.
✗ Branch 1 not taken.
27716 || 16*sps->mb_height != h->avctx->coded_height
1074
2/2
✓ Branch 0 taken 27715 times.
✓ Branch 1 taken 1 times.
27716 || h->cur_bit_depth_luma != sps->bit_depth_luma
1075
2/2
✓ Branch 0 taken 27714 times.
✓ Branch 1 taken 1 times.
27715 || h->cur_chroma_format_idc != sps->chroma_format_idc
1076
1/2
✓ Branch 0 taken 27714 times.
✗ Branch 1 not taken.
27714 || h->mb_width != sps->mb_width
1077
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 27714 times.
27714 || h->mb_height != sps->mb_height
1078 ));
1079
2/2
✓ Branch 0 taken 27973 times.
✓ Branch 1 taken 328 times.
28301 if (h->avctx->pix_fmt == AV_PIX_FMT_NONE
1080
2/2
✓ Branch 3 taken 3 times.
✓ Branch 4 taken 27970 times.
27973 || (non_j_pixfmt(h->avctx->pix_fmt) != non_j_pixfmt(get_pixel_format(h, 0))))
1081 331 must_reinit = 1;
1082
1083
4/4
✓ Branch 0 taken 28107 times.
✓ Branch 1 taken 194 times.
✓ Branch 3 taken 71 times.
✓ Branch 4 taken 28036 times.
28301 if (first_slice && av_cmp_q(sps->vui.sar, h->avctx->sample_aspect_ratio))
1084 71 must_reinit = 1;
1085
1086
1/2
✓ Branch 0 taken 28301 times.
✗ Branch 1 not taken.
28301 if (!h->setup_finished) {
1087 28301 h->avctx->profile = ff_h264_get_profile(sps);
1088 28301 h->avctx->level = sps->level_idc;
1089 28301 h->avctx->refs = sps->ref_frame_count;
1090
1091 28301 h->mb_width = sps->mb_width;
1092 28301 h->mb_height = sps->mb_height;
1093 28301 h->mb_num = h->mb_width * h->mb_height;
1094 28301 h->mb_stride = h->mb_width + 1;
1095
1096 28301 h->b_stride = h->mb_width * 4;
1097
1098 28301 h->chroma_y_shift = sps->chroma_format_idc <= 1; // 400 uses yuv420p
1099
1100 28301 h->width = 16 * h->mb_width;
1101 28301 h->height = 16 * h->mb_height;
1102
1103 28301 init_dimensions(h);
1104
1105
2/2
✓ Branch 0 taken 1287 times.
✓ Branch 1 taken 27014 times.
28301 if (sps->vui.video_signal_type_present_flag) {
1106 2574 h->avctx->color_range = sps->vui.video_full_range_flag > 0 ? AVCOL_RANGE_JPEG
1107
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1285 times.
1287 : AVCOL_RANGE_MPEG;
1108
2/2
✓ Branch 0 taken 1189 times.
✓ Branch 1 taken 98 times.
1287 if (sps->vui.colour_description_present_flag) {
1109
2/2
✓ Branch 0 taken 19 times.
✓ Branch 1 taken 1170 times.
1189 if (h->avctx->colorspace != sps->vui.matrix_coeffs)
1110 19 needs_reinit = 1;
1111 1189 h->avctx->color_primaries = sps->vui.colour_primaries;
1112 1189 h->avctx->color_trc = sps->vui.transfer_characteristics;
1113 1189 h->avctx->colorspace = sps->vui.matrix_coeffs;
1114 }
1115 }
1116
1117
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 28301 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
28301 if (h->sei.common.alternative_transfer.present &&
1118 av_color_transfer_name(h->sei.common.alternative_transfer.preferred_transfer_characteristics) &&
1119 h->sei.common.alternative_transfer.preferred_transfer_characteristics != AVCOL_TRC_UNSPECIFIED) {
1120 h->avctx->color_trc = h->sei.common.alternative_transfer.preferred_transfer_characteristics;
1121 }
1122 }
1123 28301 h->avctx->chroma_sample_location = sps->vui.chroma_location;
1124
1125
5/6
✓ Branch 0 taken 27722 times.
✓ Branch 1 taken 579 times.
✓ Branch 2 taken 27714 times.
✓ Branch 3 taken 8 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 27714 times.
28301 if (!h->context_initialized || must_reinit || needs_reinit) {
1126 587 int flush_changes = h->context_initialized;
1127 587 h->context_initialized = 0;
1128
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 587 times.
587 if (sl != h->slice_ctx) {
1129 av_log(h->avctx, AV_LOG_ERROR,
1130 "changing width %d -> %d / height %d -> %d on "
1131 "slice %d\n",
1132 h->width, h->avctx->coded_width,
1133 h->height, h->avctx->coded_height,
1134 h->current_slice + 1);
1135 return AVERROR_INVALIDDATA;
1136 }
1137
1138 av_assert1(first_slice);
1139
1140
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 579 times.
587 if (flush_changes)
1141 8 ff_h264_flush_change(h);
1142
1143
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 587 times.
587 if ((ret = get_pixel_format(h, 1)) < 0)
1144 return ret;
1145 587 h->avctx->pix_fmt = ret;
1146
1147 587 av_log(h->avctx, AV_LOG_VERBOSE, "Reinit context to %dx%d, "
1148 587 "pix_fmt: %s\n", h->width, h->height, av_get_pix_fmt_name(h->avctx->pix_fmt));
1149
1150
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 587 times.
587 if ((ret = h264_slice_header_init(h)) < 0) {
1151 av_log(h->avctx, AV_LOG_ERROR,
1152 "h264_slice_header_init() failed\n");
1153 return ret;
1154 }
1155 }
1156
1157 28301 return 0;
1158 }
1159
1160 24929 static int h264_export_frame_props(H264Context *h)
1161 {
1162 24929 const SPS *sps = h->ps.sps;
1163 24929 H264Picture *cur = h->cur_pic_ptr;
1164 24929 AVFrame *out = cur->f;
1165 24929 int interlaced_frame = 0, top_field_first = 0;
1166 int ret;
1167
1168 24929 out->flags &= ~AV_FRAME_FLAG_INTERLACED;
1169 24929 out->repeat_pict = 0;
1170
1171 /* Signal interlacing information externally. */
1172 /* Prioritize picture timing SEI information over used
1173 * decoding process if it exists. */
1174
2/2
✓ Branch 0 taken 1426 times.
✓ Branch 1 taken 23503 times.
24929 if (h->sei.picture_timing.present) {
1175 1426 int ret = ff_h264_sei_process_picture_timing(&h->sei.picture_timing, sps,
1176 1426 h->avctx);
1177
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1426 times.
1426 if (ret < 0) {
1178 av_log(h->avctx, AV_LOG_ERROR, "Error processing a picture timing SEI\n");
1179 if (h->avctx->err_recognition & AV_EF_EXPLODE)
1180 return ret;
1181 h->sei.picture_timing.present = 0;
1182 }
1183 }
1184
1185
4/4
✓ Branch 0 taken 1504 times.
✓ Branch 1 taken 23425 times.
✓ Branch 2 taken 1418 times.
✓ Branch 3 taken 86 times.
26347 if (sps->pic_struct_present_flag && h->sei.picture_timing.present) {
1186 1418 const H264SEIPictureTiming *pt = &h->sei.picture_timing;
1187
4/7
✓ Branch 0 taken 132 times.
✓ Branch 1 taken 377 times.
✓ Branch 2 taken 742 times.
✓ Branch 3 taken 167 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
1418 switch (pt->pic_struct) {
1188 132 case H264_SEI_PIC_STRUCT_FRAME:
1189 132 break;
1190 377 case H264_SEI_PIC_STRUCT_TOP_FIELD:
1191 case H264_SEI_PIC_STRUCT_BOTTOM_FIELD:
1192 377 interlaced_frame = 1;
1193 377 break;
1194 742 case H264_SEI_PIC_STRUCT_TOP_BOTTOM:
1195 case H264_SEI_PIC_STRUCT_BOTTOM_TOP:
1196
3/4
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 738 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 4 times.
742 if (FIELD_OR_MBAFF_PICTURE(h))
1197 738 interlaced_frame = 1;
1198 else
1199 // try to flag soft telecine progressive
1200 4 interlaced_frame = !!h->prev_interlaced_frame;
1201 742 break;
1202 167 case H264_SEI_PIC_STRUCT_TOP_BOTTOM_TOP:
1203 case H264_SEI_PIC_STRUCT_BOTTOM_TOP_BOTTOM:
1204 /* Signal the possibility of telecined film externally
1205 * (pic_struct 5,6). From these hints, let the applications
1206 * decide if they apply deinterlacing. */
1207 167 out->repeat_pict = 1;
1208 167 break;
1209 case H264_SEI_PIC_STRUCT_FRAME_DOUBLING:
1210 out->repeat_pict = 2;
1211 break;
1212 case H264_SEI_PIC_STRUCT_FRAME_TRIPLING:
1213 out->repeat_pict = 4;
1214 break;
1215 }
1216
1217
2/2
✓ Branch 0 taken 652 times.
✓ Branch 1 taken 766 times.
1418 if ((pt->ct_type & 3) &&
1218
2/2
✓ Branch 0 taken 488 times.
✓ Branch 1 taken 164 times.
652 pt->pic_struct <= H264_SEI_PIC_STRUCT_BOTTOM_TOP)
1219 488 interlaced_frame = ((pt->ct_type & (1 << 1)) != 0);
1220 } else {
1221 /* Derive interlacing flag from used decoding process. */
1222
4/4
✓ Branch 0 taken 21920 times.
✓ Branch 1 taken 1591 times.
✓ Branch 2 taken 2826 times.
✓ Branch 3 taken 19094 times.
23511 interlaced_frame = !!FIELD_OR_MBAFF_PICTURE(h);
1223 }
1224 24929 h->prev_interlaced_frame = interlaced_frame;
1225
1226
2/2
✓ Branch 0 taken 3341 times.
✓ Branch 1 taken 21588 times.
24929 if (cur->field_poc[0] != cur->field_poc[1]) {
1227 /* Derive top_field_first from field pocs. */
1228 3341 top_field_first = (cur->field_poc[0] < cur->field_poc[1]);
1229 } else {
1230
4/4
✓ Branch 0 taken 225 times.
✓ Branch 1 taken 21363 times.
✓ Branch 2 taken 139 times.
✓ Branch 3 taken 86 times.
21588 if (sps->pic_struct_present_flag && h->sei.picture_timing.present) {
1231 /* Use picture timing SEI information. Even if it is a
1232 * information of a past frame, better than nothing. */
1233
2/2
✓ Branch 0 taken 138 times.
✓ Branch 1 taken 1 times.
139 if (h->sei.picture_timing.pic_struct == H264_SEI_PIC_STRUCT_TOP_BOTTOM ||
1234
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 136 times.
138 h->sei.picture_timing.pic_struct == H264_SEI_PIC_STRUCT_TOP_BOTTOM_TOP)
1235 3 top_field_first = 1;
1236
2/2
✓ Branch 0 taken 2558 times.
✓ Branch 1 taken 18891 times.
21449 } else if (interlaced_frame) {
1237 /* Default to top field first when pic_struct_present_flag
1238 * is not set but interlaced frame detected */
1239 2558 top_field_first = 1;
1240 } // else
1241 /* Most likely progressive */
1242 }
1243
1244 24929 out->flags |= (AV_FRAME_FLAG_INTERLACED * interlaced_frame) |
1245 24929 (AV_FRAME_FLAG_TOP_FIELD_FIRST * top_field_first);
1246
1247 24929 ret = ff_h2645_sei_to_frame(out, &h->sei.common, AV_CODEC_ID_H264, h->avctx,
1248 24929 &sps->vui, sps->bit_depth_luma, sps->bit_depth_chroma,
1249 24929 cur->poc + (unsigned)(h->poc_offset << 5));
1250
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 24929 times.
24929 if (ret < 0)
1251 return ret;
1252
1253
2/2
✓ Branch 0 taken 652 times.
✓ Branch 1 taken 24277 times.
24929 if (h->sei.picture_timing.timecode_cnt > 0) {
1254 uint32_t *tc_sd;
1255 char tcbuf[AV_TIMECODE_STR_SIZE];
1256 AVFrameSideData *tcside;
1257 652 ret = ff_frame_new_side_data(h->avctx, out, AV_FRAME_DATA_S12M_TIMECODE,
1258 sizeof(uint32_t)*4, &tcside);
1259
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 652 times.
652 if (ret < 0)
1260 return ret;
1261
1262
1/2
✓ Branch 0 taken 652 times.
✗ Branch 1 not taken.
652 if (tcside) {
1263 652 tc_sd = (uint32_t*)tcside->data;
1264 652 tc_sd[0] = h->sei.picture_timing.timecode_cnt;
1265
1266
2/2
✓ Branch 0 taken 953 times.
✓ Branch 1 taken 652 times.
1605 for (int i = 0; i < tc_sd[0]; i++) {
1267 953 int drop = h->sei.picture_timing.timecode[i].dropframe;
1268 953 int hh = h->sei.picture_timing.timecode[i].hours;
1269 953 int mm = h->sei.picture_timing.timecode[i].minutes;
1270 953 int ss = h->sei.picture_timing.timecode[i].seconds;
1271 953 int ff = h->sei.picture_timing.timecode[i].frame;
1272
1273 953 tc_sd[i + 1] = av_timecode_get_smpte(h->avctx->framerate, drop, hh, mm, ss, ff);
1274 953 av_timecode_make_smpte_tc_string2(tcbuf, h->avctx->framerate, tc_sd[i + 1], 0, 0);
1275 953 av_dict_set(&out->metadata, "timecode", tcbuf, 0);
1276 }
1277 }
1278 652 h->sei.picture_timing.timecode_cnt = 0;
1279 }
1280
1281 24929 return 0;
1282 }
1283
1284 24929 static int h264_select_output_frame(H264Context *h)
1285 {
1286 24929 const SPS *sps = h->ps.sps;
1287 24929 H264Picture *out = h->cur_pic_ptr;
1288 24929 H264Picture *cur = h->cur_pic_ptr;
1289 int i, pics, out_of_order, out_idx;
1290
1291 24929 cur->mmco_reset = h->mmco_reset;
1292 24929 h->mmco_reset = 0;
1293
1294
2/2
✓ Branch 0 taken 20028 times.
✓ Branch 1 taken 4901 times.
24929 if (sps->bitstream_restriction_flag ||
1295
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 20028 times.
20028 h->avctx->strict_std_compliance >= FF_COMPLIANCE_STRICT) {
1296 4901 h->avctx->has_b_frames = FFMAX(h->avctx->has_b_frames, sps->num_reorder_frames);
1297 }
1298
1299 408792 for (i = 0; 1; i++) {
1300
4/4
✓ Branch 0 taken 392873 times.
✓ Branch 1 taken 15919 times.
✓ Branch 2 taken 9010 times.
✓ Branch 3 taken 383863 times.
408792 if(i == H264_MAX_DPB_FRAMES || cur->poc < h->last_pocs[i]){
1301
1/2
✓ Branch 0 taken 24929 times.
✗ Branch 1 not taken.
24929 if(i)
1302 24929 h->last_pocs[i-1] = cur->poc;
1303 24929 break;
1304
2/2
✓ Branch 0 taken 358934 times.
✓ Branch 1 taken 24929 times.
383863 } else if(i) {
1305 358934 h->last_pocs[i-1]= h->last_pocs[i];
1306 }
1307 }
1308 24929 out_of_order = H264_MAX_DPB_FRAMES - i;
1309
2/2
✓ Branch 0 taken 16803 times.
✓ Branch 1 taken 8126 times.
24929 if( cur->f->pict_type == AV_PICTURE_TYPE_B
1310
4/4
✓ Branch 0 taken 15680 times.
✓ Branch 1 taken 1123 times.
✓ Branch 2 taken 5065 times.
✓ Branch 3 taken 10615 times.
16803 || (h->last_pocs[H264_MAX_DPB_FRAMES-2] > INT_MIN && h->last_pocs[H264_MAX_DPB_FRAMES-1] - (int64_t)h->last_pocs[H264_MAX_DPB_FRAMES-2] > 2))
1311 13191 out_of_order = FFMAX(out_of_order, 1);
1312
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 24929 times.
24929 if (out_of_order == H264_MAX_DPB_FRAMES) {
1313 av_log(h->avctx, AV_LOG_VERBOSE, "Invalid POC %d<%d\n", cur->poc, h->last_pocs[0]);
1314 for (i = 1; i < H264_MAX_DPB_FRAMES; i++)
1315 h->last_pocs[i] = INT_MIN;
1316 h->last_pocs[0] = cur->poc;
1317 cur->mmco_reset = 1;
1318
3/4
✓ Branch 0 taken 141 times.
✓ Branch 1 taken 24788 times.
✓ Branch 2 taken 141 times.
✗ Branch 3 not taken.
24929 } else if(h->avctx->has_b_frames < out_of_order && !sps->bitstream_restriction_flag){
1319
2/2
✓ Branch 0 taken 33 times.
✓ Branch 1 taken 108 times.
141 int loglevel = h->avctx->frame_num > 1 ? AV_LOG_WARNING : AV_LOG_VERBOSE;
1320 141 av_log(h->avctx, loglevel, "Increasing reorder buffer to %d\n", out_of_order);
1321 141 h->avctx->has_b_frames = out_of_order;
1322 }
1323
1324 24929 pics = 0;
1325
2/2
✓ Branch 0 taken 30542 times.
✓ Branch 1 taken 24929 times.
55471 while (h->delayed_pic[pics])
1326 30542 pics++;
1327
1328
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 24929 times.
24929 av_assert0(pics <= H264_MAX_DPB_FRAMES);
1329
1330 24929 h->delayed_pic[pics++] = cur;
1331
2/2
✓ Branch 0 taken 23451 times.
✓ Branch 1 taken 1478 times.
24929 if (cur->reference == 0)
1332 23451 cur->reference = DELAYED_PIC_REF;
1333
1334 24929 out = h->delayed_pic[0];
1335 24929 out_idx = 0;
1336 24929 for (i = 1; h->delayed_pic[i] &&
1337
4/4
✓ Branch 0 taken 28091 times.
✓ Branch 1 taken 24462 times.
✓ Branch 2 taken 27711 times.
✓ Branch 3 taken 380 times.
52553 !(h->delayed_pic[i]->f->flags & AV_FRAME_FLAG_KEY) &&
1338
2/2
✓ Branch 0 taken 27624 times.
✓ Branch 1 taken 87 times.
27711 !h->delayed_pic[i]->mmco_reset;
1339 27624 i++)
1340
2/2
✓ Branch 0 taken 10310 times.
✓ Branch 1 taken 17314 times.
27624 if (h->delayed_pic[i]->poc < out->poc) {
1341 10310 out = h->delayed_pic[i];
1342 10310 out_idx = i;
1343 }
1344
2/2
✓ Branch 0 taken 10259 times.
✓ Branch 1 taken 14670 times.
24929 if (h->avctx->has_b_frames == 0 &&
1345
4/4
✓ Branch 0 taken 9466 times.
✓ Branch 1 taken 793 times.
✓ Branch 2 taken 12 times.
✓ Branch 3 taken 9454 times.
10259 ((h->delayed_pic[0]->f->flags & AV_FRAME_FLAG_KEY) || h->delayed_pic[0]->mmco_reset))
1346 805 h->next_outputed_poc = INT_MIN;
1347 24929 out_of_order = out->poc < h->next_outputed_poc;
1348
1349
4/4
✓ Branch 0 taken 24816 times.
✓ Branch 1 taken 113 times.
✓ Branch 2 taken 24289 times.
✓ Branch 3 taken 527 times.
24929 if (out_of_order || pics > h->avctx->has_b_frames) {
1350 24402 out->reference &= ~DELAYED_PIC_REF;
1351
2/2
✓ Branch 0 taken 39547 times.
✓ Branch 1 taken 24402 times.
63949 for (i = out_idx; h->delayed_pic[i]; i++)
1352 39547 h->delayed_pic[i] = h->delayed_pic[i + 1];
1353 }
1354
4/4
✓ Branch 0 taken 24816 times.
✓ Branch 1 taken 113 times.
✓ Branch 2 taken 24289 times.
✓ Branch 3 taken 527 times.
24929 if (!out_of_order && pics > h->avctx->has_b_frames) {
1355 24289 h->next_output_pic = out;
1356
8/8
✓ Branch 0 taken 15499 times.
✓ Branch 1 taken 8790 times.
✓ Branch 2 taken 5241 times.
✓ Branch 3 taken 10258 times.
✓ Branch 4 taken 5157 times.
✓ Branch 5 taken 84 times.
✓ Branch 6 taken 21 times.
✓ Branch 7 taken 5136 times.
24289 if (out_idx == 0 && h->delayed_pic[0] && ((h->delayed_pic[0]->f->flags & AV_FRAME_FLAG_KEY) || h->delayed_pic[0]->mmco_reset)) {
1357 105 h->next_outputed_poc = INT_MIN;
1358 } else
1359 24184 h->next_outputed_poc = out->poc;
1360
1361 // We have reached an recovery point and all frames after it in
1362 // display order are "recovered".
1363 24289 h->frame_recovered |= out->recovered;
1364
1365 24289 out->recovered |= h->frame_recovered & FRAME_RECOVERED_SEI;
1366
1367
2/2
✓ Branch 0 taken 249 times.
✓ Branch 1 taken 24040 times.
24289 if (!out->recovered) {
1368
1/2
✓ Branch 0 taken 249 times.
✗ Branch 1 not taken.
249 if (!(h->avctx->flags & AV_CODEC_FLAG_OUTPUT_CORRUPT) &&
1369
1/2
✓ Branch 0 taken 249 times.
✗ Branch 1 not taken.
249 !(h->avctx->flags2 & AV_CODEC_FLAG2_SHOW_ALL)) {
1370 249 h->next_output_pic = NULL;
1371 } else {
1372 out->f->flags |= AV_FRAME_FLAG_CORRUPT;
1373 }
1374 }
1375 } else {
1376
2/2
✓ Branch 0 taken 113 times.
✓ Branch 1 taken 527 times.
640 av_log(h->avctx, AV_LOG_DEBUG, "no picture %s\n", out_of_order ? "ooo" : "");
1377 }
1378
1379 24929 return 0;
1380 }
1381
1382 /* This function is called right after decoding the slice header for a first
1383 * slice in a field (or a frame). It decides whether we are decoding a new frame
1384 * or a second field in a pair and does the necessary setup.
1385 */
1386 28301 static int h264_field_start(H264Context *h, const H264SliceContext *sl,
1387 const H2645NAL *nal, int first_slice)
1388 {
1389 int i;
1390 const SPS *sps;
1391
1392 int last_pic_structure, last_pic_droppable, ret;
1393
1394 28301 ret = h264_init_ps(h, sl, first_slice);
1395
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 28301 times.
28301 if (ret < 0)
1396 return ret;
1397
1398 28301 sps = h->ps.sps;
1399
1400
2/2
✓ Branch 0 taken 5234 times.
✓ Branch 1 taken 23067 times.
28301 if (sps->bitstream_restriction_flag &&
1401
2/2
✓ Branch 0 taken 29 times.
✓ Branch 1 taken 5205 times.
5234 h->avctx->has_b_frames < sps->num_reorder_frames) {
1402 29 h->avctx->has_b_frames = sps->num_reorder_frames;
1403 }
1404
1405 28301 last_pic_droppable = h->droppable;
1406 28301 last_pic_structure = h->picture_structure;
1407 28301 h->droppable = (nal->ref_idc == 0);
1408 28301 h->picture_structure = sl->picture_structure;
1409
1410 28301 h->poc.frame_num = sl->frame_num;
1411 28301 h->poc.poc_lsb = sl->poc_lsb;
1412 28301 h->poc.delta_poc_bottom = sl->delta_poc_bottom;
1413 28301 h->poc.delta_poc[0] = sl->delta_poc[0];
1414 28301 h->poc.delta_poc[1] = sl->delta_poc[1];
1415
1416
2/2
✓ Branch 0 taken 1079 times.
✓ Branch 1 taken 27222 times.
28301 if (nal->type == H264_NAL_IDR_SLICE)
1417 1079 h->poc_offset = sl->idr_pic_id;
1418
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 27222 times.
27222 else if (h->picture_intra_only)
1419 h->poc_offset = 0;
1420
1421 /* Shorten frame num gaps so we don't have to allocate reference
1422 * frames just to throw them away */
1423
2/2
✓ Branch 0 taken 16148 times.
✓ Branch 1 taken 12153 times.
28301 if (h->poc.frame_num != h->poc.prev_frame_num) {
1424 16148 int unwrap_prev_frame_num = h->poc.prev_frame_num;
1425 16148 int max_frame_num = 1 << sps->log2_max_frame_num;
1426
1427
2/2
✓ Branch 0 taken 193 times.
✓ Branch 1 taken 15955 times.
16148 if (unwrap_prev_frame_num > h->poc.frame_num)
1428 193 unwrap_prev_frame_num -= max_frame_num;
1429
1430
2/2
✓ Branch 0 taken 74 times.
✓ Branch 1 taken 16074 times.
16148 if ((h->poc.frame_num - unwrap_prev_frame_num) > sps->ref_frame_count) {
1431 74 unwrap_prev_frame_num = (h->poc.frame_num - sps->ref_frame_count) - 1;
1432
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 72 times.
74 if (unwrap_prev_frame_num < 0)
1433 2 unwrap_prev_frame_num += max_frame_num;
1434
1435 74 h->poc.prev_frame_num = unwrap_prev_frame_num;
1436 }
1437 }
1438
1439 /* See if we have a decoded first field looking for a pair...
1440 * Here, we're using that to see if we should mark previously
1441 * decode frames as "finished".
1442 * We have to do that before the "dummy" in-between frame allocation,
1443 * since that can modify h->cur_pic_ptr. */
1444
2/2
✓ Branch 0 taken 3367 times.
✓ Branch 1 taken 24934 times.
28301 if (h->first_field) {
1445 3367 int last_field = last_pic_structure == PICT_BOTTOM_FIELD;
1446
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3367 times.
3367 av_assert0(h->cur_pic_ptr);
1447
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3367 times.
3367 av_assert0(h->cur_pic_ptr->f->buf[0]);
1448 assert(h->cur_pic_ptr->reference != DELAYED_PIC_REF);
1449
1450 /* Mark old field/frame as completed */
1451
1/2
✓ Branch 0 taken 3367 times.
✗ Branch 1 not taken.
3367 if (h->cur_pic_ptr->tf.owner[last_field] == h->avctx) {
1452 3367 ff_thread_report_progress(&h->cur_pic_ptr->tf, INT_MAX, last_field);
1453 }
1454
1455 /* figure out if we have a complementary field pair */
1456
2/4
✓ Branch 0 taken 3367 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 3367 times.
3367 if (!FIELD_PICTURE(h) || h->picture_structure == last_pic_structure) {
1457 /* Previous field is unmatched. Don't display it, but let it
1458 * remain for reference if marked as such. */
1459 if (last_pic_structure != PICT_FRAME) {
1460 ff_thread_report_progress(&h->cur_pic_ptr->tf, INT_MAX,
1461 last_pic_structure == PICT_TOP_FIELD);
1462 }
1463 } else {
1464
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3367 times.
3367 if (h->cur_pic_ptr->frame_num != h->poc.frame_num) {
1465 /* This and previous field were reference, but had
1466 * different frame_nums. Consider this field first in
1467 * pair. Throw away previous field except for reference
1468 * purposes. */
1469 if (last_pic_structure != PICT_FRAME) {
1470 ff_thread_report_progress(&h->cur_pic_ptr->tf, INT_MAX,
1471 last_pic_structure == PICT_TOP_FIELD);
1472 }
1473 } else {
1474 /* Second field in complementary pair */
1475
3/4
✓ Branch 0 taken 3359 times.
✓ Branch 1 taken 8 times.
✓ Branch 2 taken 8 times.
✗ Branch 3 not taken.
3367 if (!((last_pic_structure == PICT_TOP_FIELD &&
1476
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3359 times.
3359 h->picture_structure == PICT_BOTTOM_FIELD) ||
1477 8 (last_pic_structure == PICT_BOTTOM_FIELD &&
1478
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
8 h->picture_structure == PICT_TOP_FIELD))) {
1479 av_log(h->avctx, AV_LOG_ERROR,
1480 "Invalid field mode combination %d/%d\n",
1481 last_pic_structure, h->picture_structure);
1482 h->picture_structure = last_pic_structure;
1483 h->droppable = last_pic_droppable;
1484 return AVERROR_INVALIDDATA;
1485
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3367 times.
3367 } else if (last_pic_droppable != h->droppable) {
1486 avpriv_request_sample(h->avctx,
1487 "Found reference and non-reference fields in the same frame, which");
1488 h->picture_structure = last_pic_structure;
1489 h->droppable = last_pic_droppable;
1490 return AVERROR_PATCHWELCOME;
1491 }
1492 }
1493 }
1494 }
1495
1496
3/4
✓ Branch 0 taken 16833 times.
✓ Branch 1 taken 12153 times.
✓ Branch 2 taken 16833 times.
✗ Branch 3 not taken.
57287 while (h->poc.frame_num != h->poc.prev_frame_num && !h->first_field &&
1497
2/2
✓ Branch 0 taken 685 times.
✓ Branch 1 taken 16148 times.
16833 h->poc.frame_num != (h->poc.prev_frame_num + 1) % (1 << sps->log2_max_frame_num)) {
1498
2/2
✓ Branch 0 taken 653 times.
✓ Branch 1 taken 32 times.
685 const H264Picture *prev = h->short_ref_count ? h->short_ref[0] : NULL;
1499 685 av_log(h->avctx, AV_LOG_DEBUG, "Frame num gap %d %d\n",
1500 h->poc.frame_num, h->poc.prev_frame_num);
1501
2/2
✓ Branch 0 taken 108 times.
✓ Branch 1 taken 577 times.
685 if (!sps->gaps_in_frame_num_allowed_flag)
1502
2/2
✓ Branch 0 taken 1728 times.
✓ Branch 1 taken 108 times.
1836 for(i=0; i<FF_ARRAY_ELEMS(h->last_pocs); i++)
1503 1728 h->last_pocs[i] = INT_MIN;
1504 685 ret = h264_frame_start(h);
1505
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 685 times.
685 if (ret < 0) {
1506 h->first_field = 0;
1507 return ret;
1508 }
1509
1510 685 h->poc.prev_frame_num++;
1511 685 h->poc.prev_frame_num %= 1 << sps->log2_max_frame_num;
1512 685 h->cur_pic_ptr->frame_num = h->poc.prev_frame_num;
1513 685 h->cur_pic_ptr->invalid_gap = !sps->gaps_in_frame_num_allowed_flag;
1514 685 ff_thread_report_progress(&h->cur_pic_ptr->tf, INT_MAX, 0);
1515 685 ff_thread_report_progress(&h->cur_pic_ptr->tf, INT_MAX, 1);
1516
1517 685 h->explicit_ref_marking = 0;
1518 685 ret = ff_h264_execute_ref_pic_marking(h);
1519
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 685 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
685 if (ret < 0 && (h->avctx->err_recognition & AV_EF_EXPLODE))
1520 return ret;
1521 /* Error concealment: If a ref is missing, copy the previous ref
1522 * in its place.
1523 * FIXME: Avoiding a memcpy would be nice, but ref handling makes
1524 * many assumptions about there being no actual duplicates.
1525 * FIXME: This does not copy padding for out-of-frame motion
1526 * vectors. Given we are concealing a lost frame, this probably
1527 * is not noticeable by comparison, but it should be fixed. */
1528
1/2
✓ Branch 0 taken 685 times.
✗ Branch 1 not taken.
685 if (h->short_ref_count) {
1529 685 int c[4] = {
1530 685 1<<(h->ps.sps->bit_depth_luma-1),
1531 685 1<<(h->ps.sps->bit_depth_chroma-1),
1532 685 1<<(h->ps.sps->bit_depth_chroma-1),
1533 -1
1534 };
1535
1536
2/2
✓ Branch 0 taken 653 times.
✓ Branch 1 taken 32 times.
685 if (prev &&
1537
1/2
✓ Branch 0 taken 653 times.
✗ Branch 1 not taken.
653 h->short_ref[0]->f->width == prev->f->width &&
1538
1/2
✓ Branch 0 taken 653 times.
✗ Branch 1 not taken.
653 h->short_ref[0]->f->height == prev->f->height &&
1539
1/2
✓ Branch 0 taken 653 times.
✗ Branch 1 not taken.
653 h->short_ref[0]->f->format == prev->f->format) {
1540 653 ff_thread_await_progress(&prev->tf, INT_MAX, 0);
1541
2/2
✓ Branch 0 taken 28 times.
✓ Branch 1 taken 625 times.
653 if (prev->field_picture)
1542 28 ff_thread_await_progress(&prev->tf, INT_MAX, 1);
1543 653 ff_thread_release_ext_buffer(&h->short_ref[0]->tf);
1544 653 h->short_ref[0]->tf.f = h->short_ref[0]->f;
1545 653 ret = ff_thread_ref_frame(&h->short_ref[0]->tf, &prev->tf);
1546
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 653 times.
653 if (ret < 0)
1547 return ret;
1548 653 h->short_ref[0]->poc = prev->poc + 2U;
1549 653 h->short_ref[0]->gray = prev->gray;
1550 653 ff_thread_report_progress(&h->short_ref[0]->tf, INT_MAX, 0);
1551
2/2
✓ Branch 0 taken 28 times.
✓ Branch 1 taken 625 times.
653 if (h->short_ref[0]->field_picture)
1552 28 ff_thread_report_progress(&h->short_ref[0]->tf, INT_MAX, 1);
1553
2/2
✓ Branch 0 taken 24 times.
✓ Branch 1 taken 8 times.
32 } else if (!h->frame_recovered) {
1554
1/2
✓ Branch 0 taken 24 times.
✗ Branch 1 not taken.
24 if (!h->avctx->hwaccel)
1555 24 color_frame(h->short_ref[0]->f, c);
1556 24 h->short_ref[0]->gray = 1;
1557 }
1558 685 h->short_ref[0]->frame_num = h->poc.prev_frame_num;
1559 }
1560 }
1561
1562 /* See if we have a decoded first field looking for a pair...
1563 * We're using that to see whether to continue decoding in that
1564 * frame, or to allocate a new one. */
1565
2/2
✓ Branch 0 taken 3367 times.
✓ Branch 1 taken 24934 times.
28301 if (h->first_field) {
1566
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3367 times.
3367 av_assert0(h->cur_pic_ptr);
1567
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3367 times.
3367 av_assert0(h->cur_pic_ptr->f->buf[0]);
1568 assert(h->cur_pic_ptr->reference != DELAYED_PIC_REF);
1569
1570 /* figure out if we have a complementary field pair */
1571
2/4
✓ Branch 0 taken 3367 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 3367 times.
3367 if (!FIELD_PICTURE(h) || h->picture_structure == last_pic_structure) {
1572 /* Previous field is unmatched. Don't display it, but let it
1573 * remain for reference if marked as such. */
1574 h->missing_fields ++;
1575 h->cur_pic_ptr = NULL;
1576 h->first_field = FIELD_PICTURE(h);
1577 } else {
1578 3367 h->missing_fields = 0;
1579
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3367 times.
3367 if (h->cur_pic_ptr->frame_num != h->poc.frame_num) {
1580 ff_thread_report_progress(&h->cur_pic_ptr->tf, INT_MAX,
1581 h->picture_structure==PICT_BOTTOM_FIELD);
1582 /* This and the previous field had different frame_nums.
1583 * Consider this field first in pair. Throw away previous
1584 * one except for reference purposes. */
1585 h->first_field = 1;
1586 h->cur_pic_ptr = NULL;
1587
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3367 times.
3367 } else if (h->cur_pic_ptr->reference & DELAYED_PIC_REF) {
1588 /* This frame was already output, we cannot draw into it
1589 * anymore.
1590 */
1591 h->first_field = 1;
1592 h->cur_pic_ptr = NULL;
1593 } else {
1594 /* Second field in complementary pair */
1595 3367 h->first_field = 0;
1596 }
1597 }
1598 } else {
1599 /* Frame or first field in a potentially complementary pair */
1600 24934 h->first_field = FIELD_PICTURE(h);
1601 }
1602
1603
4/4
✓ Branch 0 taken 6739 times.
✓ Branch 1 taken 21562 times.
✓ Branch 2 taken 3372 times.
✓ Branch 3 taken 3367 times.
28301 if (!FIELD_PICTURE(h) || h->first_field) {
1604
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 24934 times.
24934 if (h264_frame_start(h) < 0) {
1605 h->first_field = 0;
1606 return AVERROR_INVALIDDATA;
1607 }
1608 } else {
1609 3367 int field = h->picture_structure == PICT_BOTTOM_FIELD;
1610 3367 release_unused_pictures(h, 0);
1611 3367 h->cur_pic_ptr->tf.owner[field] = h->avctx;
1612 }
1613 /* Some macroblocks can be accessed before they're available in case
1614 * of lost slices, MBAFF or threading. */
1615
2/2
✓ Branch 0 taken 6739 times.
✓ Branch 1 taken 21562 times.
28301 if (FIELD_PICTURE(h)) {
1616
2/2
✓ Branch 0 taken 168517 times.
✓ Branch 1 taken 6739 times.
175256 for(i = (h->picture_structure == PICT_BOTTOM_FIELD); i<h->mb_height; i++)
1617 168517 memset(h->slice_table + i*h->mb_stride, -1, (h->mb_stride - (i+1==h->mb_height)) * sizeof(*h->slice_table));
1618 } else {
1619 21562 memset(h->slice_table, -1,
1620 21562 (h->mb_height * h->mb_stride - 1) * sizeof(*h->slice_table));
1621 }
1622
1623 28301 ret = ff_h264_init_poc(h->cur_pic_ptr->field_poc, &h->cur_pic_ptr->poc,
1624 28301 h->ps.sps, &h->poc, h->picture_structure, nal->ref_idc);
1625
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 28301 times.
28301 if (ret < 0)
1626 return ret;
1627
1628 28301 memcpy(h->mmco, sl->mmco, sl->nb_mmco * sizeof(*h->mmco));
1629 28301 h->nb_mmco = sl->nb_mmco;
1630 28301 h->explicit_ref_marking = sl->explicit_ref_marking;
1631
1632 28301 h->picture_idr = nal->type == H264_NAL_IDR_SLICE;
1633
1634
2/2
✓ Branch 0 taken 90 times.
✓ Branch 1 taken 28211 times.
28301 if (h->sei.recovery_point.recovery_frame_cnt >= 0) {
1635 90 const int sei_recovery_frame_cnt = h->sei.recovery_point.recovery_frame_cnt;
1636
1637
4/4
✓ Branch 0 taken 25 times.
✓ Branch 1 taken 65 times.
✓ Branch 2 taken 11 times.
✓ Branch 3 taken 14 times.
90 if (h->poc.frame_num != sei_recovery_frame_cnt || sl->slice_type_nos != AV_PICTURE_TYPE_I)
1638 76 h->valid_recovery_point = 1;
1639
1640
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 86 times.
90 if ( h->recovery_frame < 0
1641
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 || av_zero_extend(h->recovery_frame - h->poc.frame_num, h->ps.sps->log2_max_frame_num) > sei_recovery_frame_cnt) {
1642 86 h->recovery_frame = av_zero_extend(h->poc.frame_num + sei_recovery_frame_cnt, h->ps.sps->log2_max_frame_num);
1643
1644
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 80 times.
86 if (!h->valid_recovery_point)
1645 6 h->recovery_frame = h->poc.frame_num;
1646 }
1647 }
1648
1649
2/2
✓ Branch 0 taken 1079 times.
✓ Branch 1 taken 27222 times.
28301 h->cur_pic_ptr->f->flags |= AV_FRAME_FLAG_KEY * !!(nal->type == H264_NAL_IDR_SLICE);
1650
1651
2/2
✓ Branch 0 taken 1079 times.
✓ Branch 1 taken 27222 times.
28301 if (nal->type == H264_NAL_IDR_SLICE) {
1652 1079 h->cur_pic_ptr->recovered |= FRAME_RECOVERED_IDR;
1653 // If we have an IDR, all frames after it in decoded order are
1654 // "recovered".
1655 1079 h->frame_recovered |= FRAME_RECOVERED_IDR;
1656 }
1657
1658
4/4
✓ Branch 0 taken 99 times.
✓ Branch 1 taken 28202 times.
✓ Branch 2 taken 85 times.
✓ Branch 3 taken 14 times.
28301 if (h->recovery_frame == h->poc.frame_num && nal->ref_idc) {
1659 85 h->recovery_frame = -1;
1660 85 h->cur_pic_ptr->recovered |= FRAME_RECOVERED_SEI;
1661 }
1662
1663 #if 1
1664 28301 h->cur_pic_ptr->recovered |= h->frame_recovered;
1665 #else
1666 h->cur_pic_ptr->recovered |= !!(h->frame_recovered & FRAME_RECOVERED_IDR);
1667 #endif
1668
1669 /* Set the frame properties/side data. Only done for the second field in
1670 * field coded frames, since some SEI information is present for each field
1671 * and is merged by the SEI parsing code. */
1672
5/6
✓ Branch 0 taken 6739 times.
✓ Branch 1 taken 21562 times.
✓ Branch 2 taken 3372 times.
✓ Branch 3 taken 3367 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 3372 times.
28301 if (!FIELD_PICTURE(h) || !h->first_field || h->missing_fields > 1) {
1673 24929 ret = h264_export_frame_props(h);
1674
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 24929 times.
24929 if (ret < 0)
1675 return ret;
1676
1677 24929 ret = h264_select_output_frame(h);
1678
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 24929 times.
24929 if (ret < 0)
1679 return ret;
1680 }
1681
1682 28301 return 0;
1683 }
1684
1685 37621 static int h264_slice_header_parse(const H264Context *h, H264SliceContext *sl,
1686 const H2645NAL *nal)
1687 {
1688 const SPS *sps;
1689 const PPS *pps;
1690 int ret;
1691 unsigned int slice_type, tmp, i;
1692 int field_pic_flag, bottom_field_flag;
1693
4/4
✓ Branch 0 taken 37171 times.
✓ Branch 1 taken 450 times.
✓ Branch 2 taken 28350 times.
✓ Branch 3 taken 8821 times.
37621 int first_slice = sl == h->slice_ctx && !h->current_slice;
1694 int picture_structure;
1695
1696
2/2
✓ Branch 0 taken 28350 times.
✓ Branch 1 taken 9271 times.
37621 if (first_slice)
1697
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 28350 times.
28350 av_assert0(!h->setup_finished);
1698
1699 37621 sl->first_mb_addr = get_ue_golomb_long(&sl->gb);
1700
1701 37621 slice_type = get_ue_golomb_31(&sl->gb);
1702
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 37621 times.
37621 if (slice_type > 9) {
1703 av_log(h->avctx, AV_LOG_ERROR,
1704 "slice type %d too large at %d\n",
1705 slice_type, sl->first_mb_addr);
1706 return AVERROR_INVALIDDATA;
1707 }
1708
2/2
✓ Branch 0 taken 23897 times.
✓ Branch 1 taken 13724 times.
37621 if (slice_type > 4) {
1709 23897 slice_type -= 5;
1710 23897 sl->slice_type_fixed = 1;
1711 } else
1712 13724 sl->slice_type_fixed = 0;
1713
1714 37621 slice_type = ff_h264_golomb_to_pict_type[slice_type];
1715 37621 sl->slice_type = slice_type;
1716 37621 sl->slice_type_nos = slice_type & 3;
1717
1718
2/2
✓ Branch 0 taken 2265 times.
✓ Branch 1 taken 35356 times.
37621 if (nal->type == H264_NAL_IDR_SLICE &&
1719
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2265 times.
2265 sl->slice_type_nos != AV_PICTURE_TYPE_I) {
1720 av_log(h->avctx, AV_LOG_ERROR, "A non-intra slice in an IDR NAL unit.\n");
1721 return AVERROR_INVALIDDATA;
1722 }
1723
1724 37621 sl->pps_id = get_ue_golomb(&sl->gb);
1725
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 37621 times.
37621 if (sl->pps_id >= MAX_PPS_COUNT) {
1726 av_log(h->avctx, AV_LOG_ERROR, "pps_id %u out of range\n", sl->pps_id);
1727 return AVERROR_INVALIDDATA;
1728 }
1729
2/2
✓ Branch 0 taken 167 times.
✓ Branch 1 taken 37454 times.
37621 if (!h->ps.pps_list[sl->pps_id]) {
1730 167 av_log(h->avctx, AV_LOG_ERROR,
1731 "non-existing PPS %u referenced\n",
1732 sl->pps_id);
1733 167 return AVERROR_INVALIDDATA;
1734 }
1735 37454 pps = h->ps.pps_list[sl->pps_id];
1736 37454 sps = pps->sps;
1737
1738 37454 sl->frame_num = get_bits(&sl->gb, sps->log2_max_frame_num);
1739
2/2
✓ Branch 0 taken 9267 times.
✓ Branch 1 taken 28187 times.
37454 if (!first_slice) {
1740
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9267 times.
9267 if (h->poc.frame_num != sl->frame_num) {
1741 av_log(h->avctx, AV_LOG_ERROR, "Frame num change from %d to %d\n",
1742 h->poc.frame_num, sl->frame_num);
1743 return AVERROR_INVALIDDATA;
1744 }
1745 }
1746
1747 37454 sl->mb_mbaff = 0;
1748
1749
2/2
✓ Branch 0 taken 22946 times.
✓ Branch 1 taken 14508 times.
37454 if (sps->frame_mbs_only_flag) {
1750 22946 picture_structure = PICT_FRAME;
1751 } else {
1752
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 14508 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
14508 if (!sps->direct_8x8_inference_flag && slice_type == AV_PICTURE_TYPE_B) {
1753 av_log(h->avctx, AV_LOG_ERROR, "This stream was generated by a broken encoder, invalid 8x8 inference\n");
1754 return -1;
1755 }
1756 14508 field_pic_flag = get_bits1(&sl->gb);
1757
2/2
✓ Branch 0 taken 9036 times.
✓ Branch 1 taken 5472 times.
14508 if (field_pic_flag) {
1758 9036 bottom_field_flag = get_bits1(&sl->gb);
1759 9036 picture_structure = PICT_TOP_FIELD + bottom_field_flag;
1760 } else {
1761 5472 picture_structure = PICT_FRAME;
1762 }
1763 }
1764 37454 sl->picture_structure = picture_structure;
1765 37454 sl->mb_field_decoding_flag = picture_structure != PICT_FRAME;
1766
1767
2/2
✓ Branch 0 taken 28418 times.
✓ Branch 1 taken 9036 times.
37454 if (picture_structure == PICT_FRAME) {
1768 28418 sl->curr_pic_num = sl->frame_num;
1769 28418 sl->max_pic_num = 1 << sps->log2_max_frame_num;
1770 } else {
1771 9036 sl->curr_pic_num = 2 * sl->frame_num + 1;
1772 9036 sl->max_pic_num = 1 << (sps->log2_max_frame_num + 1);
1773 }
1774
1775
2/2
✓ Branch 0 taken 2260 times.
✓ Branch 1 taken 35194 times.
37454 if (nal->type == H264_NAL_IDR_SLICE) {
1776 2260 unsigned idr_pic_id = get_ue_golomb_long(&sl->gb);
1777
1/2
✓ Branch 0 taken 2260 times.
✗ Branch 1 not taken.
2260 if (idr_pic_id < 65536) {
1778 2260 sl->idr_pic_id = idr_pic_id;
1779 } else
1780 av_log(h->avctx, AV_LOG_WARNING, "idr_pic_id is invalid\n");
1781 }
1782
1783 37454 sl->poc_lsb = 0;
1784 37454 sl->delta_poc_bottom = 0;
1785
2/2
✓ Branch 0 taken 29674 times.
✓ Branch 1 taken 7780 times.
37454 if (sps->poc_type == 0) {
1786 29674 sl->poc_lsb = get_bits(&sl->gb, sps->log2_max_poc_lsb);
1787
1788
4/4
✓ Branch 0 taken 8523 times.
✓ Branch 1 taken 21151 times.
✓ Branch 2 taken 6631 times.
✓ Branch 3 taken 1892 times.
29674 if (pps->pic_order_present == 1 && picture_structure == PICT_FRAME)
1789 6631 sl->delta_poc_bottom = get_se_golomb(&sl->gb);
1790 }
1791
1792 37454 sl->delta_poc[0] = sl->delta_poc[1] = 0;
1793
4/4
✓ Branch 0 taken 3055 times.
✓ Branch 1 taken 34399 times.
✓ Branch 2 taken 2848 times.
✓ Branch 3 taken 207 times.
37454 if (sps->poc_type == 1 && !sps->delta_pic_order_always_zero_flag) {
1794 2848 sl->delta_poc[0] = get_se_golomb(&sl->gb);
1795
1796
4/4
✓ Branch 0 taken 770 times.
✓ Branch 1 taken 2078 times.
✓ Branch 2 taken 453 times.
✓ Branch 3 taken 317 times.
2848 if (pps->pic_order_present == 1 && picture_structure == PICT_FRAME)
1797 453 sl->delta_poc[1] = get_se_golomb(&sl->gb);
1798 }
1799
1800 37454 sl->redundant_pic_count = 0;
1801
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 37454 times.
37454 if (pps->redundant_pic_cnt_present)
1802 sl->redundant_pic_count = get_ue_golomb(&sl->gb);
1803
1804
2/2
✓ Branch 0 taken 13154 times.
✓ Branch 1 taken 24300 times.
37454 if (sl->slice_type_nos == AV_PICTURE_TYPE_B)
1805 13154 sl->direct_spatial_mv_pred = get_bits1(&sl->gb);
1806
1807 37454 ret = ff_h264_parse_ref_count(&sl->list_count, sl->ref_count,
1808 &sl->gb, pps, sl->slice_type_nos,
1809 37454 picture_structure, h->avctx);
1810
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 37454 times.
37454 if (ret < 0)
1811 return ret;
1812
1813
2/2
✓ Branch 0 taken 32986 times.
✓ Branch 1 taken 4468 times.
37454 if (sl->slice_type_nos != AV_PICTURE_TYPE_I) {
1814 32986 ret = ff_h264_decode_ref_pic_list_reordering(sl, h->avctx);
1815
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 32986 times.
32986 if (ret < 0) {
1816 sl->ref_count[1] = sl->ref_count[0] = 0;
1817 return ret;
1818 }
1819 }
1820
1821 37454 sl->pwt.use_weight = 0;
1822
2/2
✓ Branch 0 taken 74908 times.
✓ Branch 1 taken 37454 times.
112362 for (i = 0; i < 2; i++) {
1823 74908 sl->pwt.luma_weight_flag[i] = 0;
1824 74908 sl->pwt.chroma_weight_flag[i] = 0;
1825 }
1826
4/4
✓ Branch 0 taken 4309 times.
✓ Branch 1 taken 33145 times.
✓ Branch 2 taken 1630 times.
✓ Branch 3 taken 2679 times.
37454 if ((pps->weighted_pred && sl->slice_type_nos == AV_PICTURE_TYPE_P) ||
1827
2/2
✓ Branch 0 taken 386 times.
✓ Branch 1 taken 34389 times.
34775 (pps->weighted_bipred_idc == 1 &&
1828
1/2
✓ Branch 0 taken 386 times.
✗ Branch 1 not taken.
386 sl->slice_type_nos == AV_PICTURE_TYPE_B)) {
1829 3065 ret = ff_h264_pred_weight_table(&sl->gb, sps, sl->ref_count,
1830 sl->slice_type_nos, &sl->pwt,
1831 3065 picture_structure, h->avctx);
1832
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3065 times.
3065 if (ret < 0)
1833 return ret;
1834 }
1835
1836 37454 sl->explicit_ref_marking = 0;
1837
2/2
✓ Branch 0 taken 24960 times.
✓ Branch 1 taken 12494 times.
37454 if (nal->ref_idc) {
1838 24960 ret = ff_h264_decode_ref_pic_marking(sl, &sl->gb, nal, h->avctx);
1839
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 24960 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
24960 if (ret < 0 && (h->avctx->err_recognition & AV_EF_EXPLODE))
1840 return AVERROR_INVALIDDATA;
1841 }
1842
1843
4/4
✓ Branch 0 taken 32986 times.
✓ Branch 1 taken 4468 times.
✓ Branch 2 taken 18174 times.
✓ Branch 3 taken 14812 times.
37454 if (sl->slice_type_nos != AV_PICTURE_TYPE_I && pps->cabac) {
1844 18174 tmp = get_ue_golomb_31(&sl->gb);
1845
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 18174 times.
18174 if (tmp > 2) {
1846 av_log(h->avctx, AV_LOG_ERROR, "cabac_init_idc %u overflow\n", tmp);
1847 return AVERROR_INVALIDDATA;
1848 }
1849 18174 sl->cabac_init_idc = tmp;
1850 }
1851
1852 37454 sl->last_qscale_diff = 0;
1853 37454 tmp = pps->init_qp + (unsigned)get_se_golomb(&sl->gb);
1854
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 37454 times.
37454 if (tmp > 51 + 6 * (sps->bit_depth_luma - 8)) {
1855 av_log(h->avctx, AV_LOG_ERROR, "QP %u out of range\n", tmp);
1856 return AVERROR_INVALIDDATA;
1857 }
1858 37454 sl->qscale = tmp;
1859 37454 sl->chroma_qp[0] = get_chroma_qp(pps, 0, sl->qscale);
1860 37454 sl->chroma_qp[1] = get_chroma_qp(pps, 1, sl->qscale);
1861 // FIXME qscale / qp ... stuff
1862
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 37454 times.
37454 if (sl->slice_type == AV_PICTURE_TYPE_SP)
1863 get_bits1(&sl->gb); /* sp_for_switch_flag */
1864
1/2
✓ Branch 0 taken 37454 times.
✗ Branch 1 not taken.
37454 if (sl->slice_type == AV_PICTURE_TYPE_SP ||
1865
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 37454 times.
37454 sl->slice_type == AV_PICTURE_TYPE_SI)
1866 get_se_golomb(&sl->gb); /* slice_qs_delta */
1867
1868 37454 sl->deblocking_filter = 1;
1869 37454 sl->slice_alpha_c0_offset = 0;
1870 37454 sl->slice_beta_offset = 0;
1871
2/2
✓ Branch 0 taken 22657 times.
✓ Branch 1 taken 14797 times.
37454 if (pps->deblocking_filter_parameters_present) {
1872 22657 tmp = get_ue_golomb_31(&sl->gb);
1873
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 22657 times.
22657 if (tmp > 2) {
1874 av_log(h->avctx, AV_LOG_ERROR,
1875 "deblocking_filter_idc %u out of range\n", tmp);
1876 return AVERROR_INVALIDDATA;
1877 }
1878 22657 sl->deblocking_filter = tmp;