FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/h264_slice.c
Date: 2022-12-09 07:38:14
Exec Total Coverage
Lines: 1361 1662 81.9%
Functions: 31 32 96.9%
Branches: 900 1245 72.3%

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/display.h"
32 #include "libavutil/film_grain_params.h"
33 #include "libavutil/pixdesc.h"
34 #include "libavutil/timecode.h"
35 #include "internal.h"
36 #include "cabac.h"
37 #include "cabac_functions.h"
38 #include "decode.h"
39 #include "error_resilience.h"
40 #include "avcodec.h"
41 #include "h264.h"
42 #include "h264dec.h"
43 #include "h264data.h"
44 #include "h264chroma.h"
45 #include "h264_ps.h"
46 #include "golomb.h"
47 #include "mathops.h"
48 #include "mpegutils.h"
49 #include "rectangle.h"
50 #include "thread.h"
51 #include "threadframe.h"
52
53 static const uint8_t field_scan[16+1] = {
54 0 + 0 * 4, 0 + 1 * 4, 1 + 0 * 4, 0 + 2 * 4,
55 0 + 3 * 4, 1 + 1 * 4, 1 + 2 * 4, 1 + 3 * 4,
56 2 + 0 * 4, 2 + 1 * 4, 2 + 2 * 4, 2 + 3 * 4,
57 3 + 0 * 4, 3 + 1 * 4, 3 + 2 * 4, 3 + 3 * 4,
58 };
59
60 static const uint8_t field_scan8x8[64+1] = {
61 0 + 0 * 8, 0 + 1 * 8, 0 + 2 * 8, 1 + 0 * 8,
62 1 + 1 * 8, 0 + 3 * 8, 0 + 4 * 8, 1 + 2 * 8,
63 2 + 0 * 8, 1 + 3 * 8, 0 + 5 * 8, 0 + 6 * 8,
64 0 + 7 * 8, 1 + 4 * 8, 2 + 1 * 8, 3 + 0 * 8,
65 2 + 2 * 8, 1 + 5 * 8, 1 + 6 * 8, 1 + 7 * 8,
66 2 + 3 * 8, 3 + 1 * 8, 4 + 0 * 8, 3 + 2 * 8,
67 2 + 4 * 8, 2 + 5 * 8, 2 + 6 * 8, 2 + 7 * 8,
68 3 + 3 * 8, 4 + 1 * 8, 5 + 0 * 8, 4 + 2 * 8,
69 3 + 4 * 8, 3 + 5 * 8, 3 + 6 * 8, 3 + 7 * 8,
70 4 + 3 * 8, 5 + 1 * 8, 6 + 0 * 8, 5 + 2 * 8,
71 4 + 4 * 8, 4 + 5 * 8, 4 + 6 * 8, 4 + 7 * 8,
72 5 + 3 * 8, 6 + 1 * 8, 6 + 2 * 8, 5 + 4 * 8,
73 5 + 5 * 8, 5 + 6 * 8, 5 + 7 * 8, 6 + 3 * 8,
74 7 + 0 * 8, 7 + 1 * 8, 6 + 4 * 8, 6 + 5 * 8,
75 6 + 6 * 8, 6 + 7 * 8, 7 + 2 * 8, 7 + 3 * 8,
76 7 + 4 * 8, 7 + 5 * 8, 7 + 6 * 8, 7 + 7 * 8,
77 };
78
79 static const uint8_t field_scan8x8_cavlc[64+1] = {
80 0 + 0 * 8, 1 + 1 * 8, 2 + 0 * 8, 0 + 7 * 8,
81 2 + 2 * 8, 2 + 3 * 8, 2 + 4 * 8, 3 + 3 * 8,
82 3 + 4 * 8, 4 + 3 * 8, 4 + 4 * 8, 5 + 3 * 8,
83 5 + 5 * 8, 7 + 0 * 8, 6 + 6 * 8, 7 + 4 * 8,
84 0 + 1 * 8, 0 + 3 * 8, 1 + 3 * 8, 1 + 4 * 8,
85 1 + 5 * 8, 3 + 1 * 8, 2 + 5 * 8, 4 + 1 * 8,
86 3 + 5 * 8, 5 + 1 * 8, 4 + 5 * 8, 6 + 1 * 8,
87 5 + 6 * 8, 7 + 1 * 8, 6 + 7 * 8, 7 + 5 * 8,
88 0 + 2 * 8, 0 + 4 * 8, 0 + 5 * 8, 2 + 1 * 8,
89 1 + 6 * 8, 4 + 0 * 8, 2 + 6 * 8, 5 + 0 * 8,
90 3 + 6 * 8, 6 + 0 * 8, 4 + 6 * 8, 6 + 2 * 8,
91 5 + 7 * 8, 6 + 4 * 8, 7 + 2 * 8, 7 + 6 * 8,
92 1 + 0 * 8, 1 + 2 * 8, 0 + 6 * 8, 3 + 0 * 8,
93 1 + 7 * 8, 3 + 2 * 8, 2 + 7 * 8, 4 + 2 * 8,
94 3 + 7 * 8, 5 + 2 * 8, 4 + 7 * 8, 5 + 4 * 8,
95 6 + 3 * 8, 6 + 5 * 8, 7 + 3 * 8, 7 + 7 * 8,
96 };
97
98 // zigzag_scan8x8_cavlc[i] = zigzag_scan8x8[(i/4) + 16*(i%4)]
99 static const uint8_t zigzag_scan8x8_cavlc[64+1] = {
100 0 + 0 * 8, 1 + 1 * 8, 1 + 2 * 8, 2 + 2 * 8,
101 4 + 1 * 8, 0 + 5 * 8, 3 + 3 * 8, 7 + 0 * 8,
102 3 + 4 * 8, 1 + 7 * 8, 5 + 3 * 8, 6 + 3 * 8,
103 2 + 7 * 8, 6 + 4 * 8, 5 + 6 * 8, 7 + 5 * 8,
104 1 + 0 * 8, 2 + 0 * 8, 0 + 3 * 8, 3 + 1 * 8,
105 3 + 2 * 8, 0 + 6 * 8, 4 + 2 * 8, 6 + 1 * 8,
106 2 + 5 * 8, 2 + 6 * 8, 6 + 2 * 8, 5 + 4 * 8,
107 3 + 7 * 8, 7 + 3 * 8, 4 + 7 * 8, 7 + 6 * 8,
108 0 + 1 * 8, 3 + 0 * 8, 0 + 4 * 8, 4 + 0 * 8,
109 2 + 3 * 8, 1 + 5 * 8, 5 + 1 * 8, 5 + 2 * 8,
110 1 + 6 * 8, 3 + 5 * 8, 7 + 1 * 8, 4 + 5 * 8,
111 4 + 6 * 8, 7 + 4 * 8, 5 + 7 * 8, 6 + 7 * 8,
112 0 + 2 * 8, 2 + 1 * 8, 1 + 3 * 8, 5 + 0 * 8,
113 1 + 4 * 8, 2 + 4 * 8, 6 + 0 * 8, 4 + 3 * 8,
114 0 + 7 * 8, 4 + 4 * 8, 7 + 2 * 8, 3 + 6 * 8,
115 5 + 5 * 8, 6 + 5 * 8, 6 + 6 * 8, 7 + 7 * 8,
116 };
117
118 27557 static void release_unused_pictures(H264Context *h, int remove_current)
119 {
120 int i;
121
122 /* release non reference frames */
123
2/2
✓ Branch 0 taken 992052 times.
✓ Branch 1 taken 27557 times.
1019609 for (i = 0; i < H264_MAX_PICTURE_COUNT; i++) {
124
6/6
✓ Branch 0 taken 141872 times.
✓ Branch 1 taken 850180 times.
✓ Branch 2 taken 23987 times.
✓ Branch 3 taken 117885 times.
✓ Branch 4 taken 2609 times.
✓ Branch 5 taken 21378 times.
992052 if (h->DPB[i].f->buf[0] && !h->DPB[i].reference &&
125
2/2
✓ Branch 0 taken 723 times.
✓ Branch 1 taken 1886 times.
2609 (remove_current || &h->DPB[i] != h->cur_pic_ptr)) {
126 22101 ff_h264_unref_picture(h, &h->DPB[i]);
127 }
128 }
129 27557 }
130
131 35528 static int alloc_scratch_buffers(H264SliceContext *sl, int linesize)
132 {
133 35528 const H264Context *h = sl->h264;
134 35528 int alloc_size = FFALIGN(FFABS(linesize) + 32, 32);
135
136 35528 av_fast_malloc(&sl->bipred_scratchpad, &sl->bipred_scratchpad_allocated, 16 * 6 * alloc_size);
137 // edge emu needs blocksize + filter length - 1
138 // (= 21x21 for H.264)
139 35528 av_fast_malloc(&sl->edge_emu_buffer, &sl->edge_emu_buffer_allocated, alloc_size * 2 * 21);
140
141 35528 av_fast_mallocz(&sl->top_borders[0], &sl->top_borders_allocated[0],
142 35528 h->mb_width * 16 * 3 * sizeof(uint8_t) * 2);
143 35528 av_fast_mallocz(&sl->top_borders[1], &sl->top_borders_allocated[1],
144 35528 h->mb_width * 16 * 3 * sizeof(uint8_t) * 2);
145
146
2/4
✓ Branch 0 taken 35528 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 35528 times.
✗ Branch 3 not taken.
35528 if (!sl->bipred_scratchpad || !sl->edge_emu_buffer ||
147
2/4
✓ Branch 0 taken 35528 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 35528 times.
35528 !sl->top_borders[0] || !sl->top_borders[1]) {
148 av_freep(&sl->bipred_scratchpad);
149 av_freep(&sl->edge_emu_buffer);
150 av_freep(&sl->top_borders[0]);
151 av_freep(&sl->top_borders[1]);
152
153 sl->bipred_scratchpad_allocated = 0;
154 sl->edge_emu_buffer_allocated = 0;
155 sl->top_borders_allocated[0] = 0;
156 sl->top_borders_allocated[1] = 0;
157 return AVERROR(ENOMEM);
158 }
159
160 35528 return 0;
161 }
162
163 574 static int init_table_pools(H264Context *h)
164 {
165 574 const int big_mb_num = h->mb_stride * (h->mb_height + 1) + 1;
166 574 const int mb_array_size = h->mb_stride * h->mb_height;
167 574 const int b4_stride = h->mb_width * 4 + 1;
168 574 const int b4_array_size = b4_stride * h->mb_height * 4;
169
170 574 h->qscale_table_pool = av_buffer_pool_init(big_mb_num + h->mb_stride,
171 av_buffer_allocz);
172 574 h->mb_type_pool = av_buffer_pool_init((big_mb_num + h->mb_stride) *
173 sizeof(uint32_t), av_buffer_allocz);
174 574 h->motion_val_pool = av_buffer_pool_init(2 * (b4_array_size + 4) *
175 sizeof(int16_t), av_buffer_allocz);
176 574 h->ref_index_pool = av_buffer_pool_init(4 * mb_array_size, av_buffer_allocz);
177
178
3/6
✓ Branch 0 taken 574 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 574 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 574 times.
✗ Branch 5 not taken.
574 if (!h->qscale_table_pool || !h->mb_type_pool || !h->motion_val_pool ||
179
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 574 times.
574 !h->ref_index_pool) {
180 av_buffer_pool_uninit(&h->qscale_table_pool);
181 av_buffer_pool_uninit(&h->mb_type_pool);
182 av_buffer_pool_uninit(&h->motion_val_pool);
183 av_buffer_pool_uninit(&h->ref_index_pool);
184 return AVERROR(ENOMEM);
185 }
186
187 574 return 0;
188 }
189
190 24196 static int alloc_picture(H264Context *h, H264Picture *pic)
191 {
192 24196 int i, ret = 0;
193
194
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 24196 times.
24196 av_assert0(!pic->f->data[0]);
195
196 24196 pic->tf.f = pic->f;
197 24196 ret = ff_thread_get_ext_buffer(h->avctx, &pic->tf,
198 24196 pic->reference ? AV_GET_BUFFER_FLAG_REF : 0);
199
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 24196 times.
24196 if (ret < 0)
200 goto fail;
201
202
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 24196 times.
24196 if (pic->needs_fg) {
203 pic->f_grain->format = pic->f->format;
204 pic->f_grain->width = pic->f->width;
205 pic->f_grain->height = pic->f->height;
206 ret = ff_thread_get_buffer(h->avctx, pic->f_grain, 0);
207 if (ret < 0)
208 goto fail;
209 }
210
211
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 24196 times.
24196 if (h->avctx->hwaccel) {
212 const AVHWAccel *hwaccel = h->avctx->hwaccel;
213 av_assert0(!pic->hwaccel_picture_private);
214 if (hwaccel->frame_priv_data_size) {
215 pic->hwaccel_priv_buf = av_buffer_allocz(hwaccel->frame_priv_data_size);
216 if (!pic->hwaccel_priv_buf)
217 return AVERROR(ENOMEM);
218 pic->hwaccel_picture_private = pic->hwaccel_priv_buf->data;
219 }
220 }
221 if (CONFIG_GRAY && !h->avctx->hwaccel && h->flags & AV_CODEC_FLAG_GRAY && pic->f->data[2]) {
222 int h_chroma_shift, v_chroma_shift;
223 av_pix_fmt_get_chroma_sub_sample(pic->f->format,
224 &h_chroma_shift, &v_chroma_shift);
225
226 for(i=0; i<AV_CEIL_RSHIFT(pic->f->height, v_chroma_shift); i++) {
227 memset(pic->f->data[1] + pic->f->linesize[1]*i,
228 0x80, AV_CEIL_RSHIFT(pic->f->width, h_chroma_shift));
229 memset(pic->f->data[2] + pic->f->linesize[2]*i,
230 0x80, AV_CEIL_RSHIFT(pic->f->width, h_chroma_shift));
231 }
232 }
233
234
2/2
✓ Branch 0 taken 574 times.
✓ Branch 1 taken 23622 times.
24196 if (!h->qscale_table_pool) {
235 574 ret = init_table_pools(h);
236
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 574 times.
574 if (ret < 0)
237 goto fail;
238 }
239
240 24196 pic->qscale_table_buf = av_buffer_pool_get(h->qscale_table_pool);
241 24196 pic->mb_type_buf = av_buffer_pool_get(h->mb_type_pool);
242
2/4
✓ Branch 0 taken 24196 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 24196 times.
24196 if (!pic->qscale_table_buf || !pic->mb_type_buf)
243 goto fail;
244
245 24196 pic->mb_type = (uint32_t*)pic->mb_type_buf->data + 2 * h->mb_stride + 1;
246 24196 pic->qscale_table = pic->qscale_table_buf->data + 2 * h->mb_stride + 1;
247
248
2/2
✓ Branch 0 taken 48392 times.
✓ Branch 1 taken 24196 times.
72588 for (i = 0; i < 2; i++) {
249 48392 pic->motion_val_buf[i] = av_buffer_pool_get(h->motion_val_pool);
250 48392 pic->ref_index_buf[i] = av_buffer_pool_get(h->ref_index_pool);
251
2/4
✓ Branch 0 taken 48392 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 48392 times.
48392 if (!pic->motion_val_buf[i] || !pic->ref_index_buf[i])
252 goto fail;
253
254 48392 pic->motion_val[i] = (int16_t (*)[2])pic->motion_val_buf[i]->data + 4;
255 48392 pic->ref_index[i] = pic->ref_index_buf[i]->data;
256 }
257
258 24196 pic->pps_buf = av_buffer_ref(h->ps.pps_ref);
259
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 24196 times.
24196 if (!pic->pps_buf)
260 goto fail;
261 24196 pic->pps = (const PPS*)pic->pps_buf->data;
262
263 24196 pic->mb_width = h->mb_width;
264 24196 pic->mb_height = h->mb_height;
265 24196 pic->mb_stride = h->mb_stride;
266
267 24196 return 0;
268 fail:
269 ff_h264_unref_picture(h, pic);
270 return (ret < 0) ? ret : AVERROR(ENOMEM);
271 }
272
273 24196 static int find_unused_picture(H264Context *h)
274 {
275 int i;
276
277
1/2
✓ Branch 0 taken 84191 times.
✗ Branch 1 not taken.
84191 for (i = 0; i < H264_MAX_PICTURE_COUNT; i++) {
278
2/2
✓ Branch 0 taken 24196 times.
✓ Branch 1 taken 59995 times.
84191 if (!h->DPB[i].f->buf[0])
279 24196 return i;
280 }
281 return AVERROR_INVALIDDATA;
282 }
283
284
285 #define IN_RANGE(a, b, size) (((void*)(a) >= (void*)(b)) && ((void*)(a) < (void*)((b) + (size))))
286
287 #define REBASE_PICTURE(pic, new_ctx, old_ctx) \
288 (((pic) && (pic) >= (old_ctx)->DPB && \
289 (pic) < (old_ctx)->DPB + H264_MAX_PICTURE_COUNT) ? \
290 &(new_ctx)->DPB[(pic) - (old_ctx)->DPB] : NULL)
291
292 186 static void copy_picture_range(H264Picture **to, H264Picture **from, int count,
293 H264Context *new_base,
294 H264Context *old_base)
295 {
296 int i;
297
298
2/2
✓ Branch 0 taken 5084 times.
✓ Branch 1 taken 186 times.
5270 for (i = 0; i < count; i++) {
299 av_assert1(!from[i] ||
300 IN_RANGE(from[i], old_base, 1) ||
301 IN_RANGE(from[i], old_base->DPB, H264_MAX_PICTURE_COUNT));
302
4/6
✓ Branch 0 taken 265 times.
✓ Branch 1 taken 4819 times.
✓ Branch 2 taken 265 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 265 times.
✗ Branch 5 not taken.
5084 to[i] = REBASE_PICTURE(from[i], new_base, old_base);
303 }
304 186 }
305
306 static int h264_slice_header_init(H264Context *h);
307
308 62 int ff_h264_update_thread_context(AVCodecContext *dst,
309 const AVCodecContext *src)
310 {
311 62 H264Context *h = dst->priv_data, *h1 = src->priv_data;
312 62 int inited = h->context_initialized, err = 0;
313 62 int need_reinit = 0;
314 int i, ret;
315
316
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 62 times.
62 if (dst == src)
317 return 0;
318
319
3/4
✓ Branch 0 taken 46 times.
✓ Branch 1 taken 16 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 46 times.
62 if (inited && !h1->ps.sps)
320 return AVERROR_INVALIDDATA;
321
322
2/2
✓ Branch 0 taken 46 times.
✓ Branch 1 taken 16 times.
62 if (inited &&
323
1/2
✓ Branch 0 taken 46 times.
✗ Branch 1 not taken.
46 (h->width != h1->width ||
324
1/2
✓ Branch 0 taken 46 times.
✗ Branch 1 not taken.
46 h->height != h1->height ||
325
1/2
✓ Branch 0 taken 46 times.
✗ Branch 1 not taken.
46 h->mb_width != h1->mb_width ||
326
1/2
✓ Branch 0 taken 46 times.
✗ Branch 1 not taken.
46 h->mb_height != h1->mb_height ||
327
1/2
✓ Branch 0 taken 46 times.
✗ Branch 1 not taken.
46 !h->ps.sps ||
328
1/2
✓ Branch 0 taken 46 times.
✗ Branch 1 not taken.
46 h->ps.sps->bit_depth_luma != h1->ps.sps->bit_depth_luma ||
329
1/2
✓ Branch 0 taken 46 times.
✗ Branch 1 not taken.
46 h->ps.sps->chroma_format_idc != h1->ps.sps->chroma_format_idc ||
330
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 46 times.
46 h->ps.sps->vui.matrix_coeffs != h1->ps.sps->vui.matrix_coeffs)) {
331 need_reinit = 1;
332 }
333
334 /* copy block_offset since frame_start may not be called */
335 62 memcpy(h->block_offset, h1->block_offset, sizeof(h->block_offset));
336
337 // SPS/PPS
338
2/2
✓ Branch 0 taken 1984 times.
✓ Branch 1 taken 62 times.
2046 for (i = 0; i < FF_ARRAY_ELEMS(h->ps.sps_list); i++) {
339 1984 ret = av_buffer_replace(&h->ps.sps_list[i], h1->ps.sps_list[i]);
340
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1984 times.
1984 if (ret < 0)
341 return ret;
342 }
343
2/2
✓ Branch 0 taken 15872 times.
✓ Branch 1 taken 62 times.
15934 for (i = 0; i < FF_ARRAY_ELEMS(h->ps.pps_list); i++) {
344 15872 ret = av_buffer_replace(&h->ps.pps_list[i], h1->ps.pps_list[i]);
345
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 15872 times.
15872 if (ret < 0)
346 return ret;
347 }
348
349 62 ret = av_buffer_replace(&h->ps.pps_ref, h1->ps.pps_ref);
350
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 62 times.
62 if (ret < 0)
351 return ret;
352 62 h->ps.pps = NULL;
353 62 h->ps.sps = NULL;
354
1/2
✓ Branch 0 taken 62 times.
✗ Branch 1 not taken.
62 if (h1->ps.pps_ref) {
355 62 h->ps.pps = (const PPS*)h->ps.pps_ref->data;
356 62 h->ps.sps = h->ps.pps->sps;
357 }
358
359
3/4
✓ Branch 0 taken 62 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 16 times.
✓ Branch 3 taken 46 times.
62 if (need_reinit || !inited) {
360 16 h->width = h1->width;
361 16 h->height = h1->height;
362 16 h->mb_height = h1->mb_height;
363 16 h->mb_width = h1->mb_width;
364 16 h->mb_num = h1->mb_num;
365 16 h->mb_stride = h1->mb_stride;
366 16 h->b_stride = h1->b_stride;
367 16 h->x264_build = h1->x264_build;
368
369
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) {
370
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 16 times.
16 if ((err = h264_slice_header_init(h)) < 0) {
371 av_log(h->avctx, AV_LOG_ERROR, "h264_slice_header_init() failed");
372 return err;
373 }
374 }
375
376 /* copy block_offset since frame_start may not be called */
377 16 memcpy(h->block_offset, h1->block_offset, sizeof(h->block_offset));
378 }
379
380 62 h->avctx->coded_height = h1->avctx->coded_height;
381 62 h->avctx->coded_width = h1->avctx->coded_width;
382 62 h->avctx->width = h1->avctx->width;
383 62 h->avctx->height = h1->avctx->height;
384 62 h->width_from_caller = h1->width_from_caller;
385 62 h->height_from_caller = h1->height_from_caller;
386 62 h->coded_picture_number = h1->coded_picture_number;
387 62 h->first_field = h1->first_field;
388 62 h->picture_structure = h1->picture_structure;
389 62 h->mb_aff_frame = h1->mb_aff_frame;
390 62 h->droppable = h1->droppable;
391
392
2/2
✓ Branch 0 taken 2232 times.
✓ Branch 1 taken 62 times.
2294 for (i = 0; i < H264_MAX_PICTURE_COUNT; i++) {
393 2232 ret = ff_h264_replace_picture(h, &h->DPB[i], &h1->DPB[i]);
394
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2232 times.
2232 if (ret < 0)
395 return ret;
396 }
397
398
4/6
✓ Branch 0 taken 42 times.
✓ Branch 1 taken 20 times.
✓ Branch 2 taken 42 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 42 times.
✗ Branch 5 not taken.
62 h->cur_pic_ptr = REBASE_PICTURE(h1->cur_pic_ptr, h, h1);
399 62 ret = ff_h264_replace_picture(h, &h->cur_pic, &h1->cur_pic);
400
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 62 times.
62 if (ret < 0)
401 return ret;
402
403 62 h->enable_er = h1->enable_er;
404 62 h->workaround_bugs = h1->workaround_bugs;
405 62 h->droppable = h1->droppable;
406
407 // extradata/NAL handling
408 62 h->is_avc = h1->is_avc;
409 62 h->nal_length_size = h1->nal_length_size;
410
411 62 memcpy(&h->poc, &h1->poc, sizeof(h->poc));
412
413 62 memcpy(h->short_ref, h1->short_ref, sizeof(h->short_ref));
414 62 memcpy(h->long_ref, h1->long_ref, sizeof(h->long_ref));
415 62 memcpy(h->delayed_pic, h1->delayed_pic, sizeof(h->delayed_pic));
416 62 memcpy(h->last_pocs, h1->last_pocs, sizeof(h->last_pocs));
417
418 62 h->next_output_pic = h1->next_output_pic;
419 62 h->next_outputed_poc = h1->next_outputed_poc;
420 62 h->poc_offset = h1->poc_offset;
421
422 62 memcpy(h->mmco, h1->mmco, sizeof(h->mmco));
423 62 h->nb_mmco = h1->nb_mmco;
424 62 h->mmco_reset = h1->mmco_reset;
425 62 h->explicit_ref_marking = h1->explicit_ref_marking;
426 62 h->long_ref_count = h1->long_ref_count;
427 62 h->short_ref_count = h1->short_ref_count;
428
429 62 copy_picture_range(h->short_ref, h1->short_ref, 32, h, h1);
430 62 copy_picture_range(h->long_ref, h1->long_ref, 32, h, h1);
431 62 copy_picture_range(h->delayed_pic, h1->delayed_pic,
432 FF_ARRAY_ELEMS(h->delayed_pic), h, h1);
433
434 62 h->frame_recovered = h1->frame_recovered;
435
436 62 ret = ff_h264_sei_ctx_replace(&h->sei, &h1->sei);
437
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 62 times.
62 if (ret < 0)
438 return ret;
439
440 62 h->sei.common.unregistered.x264_build = h1->sei.common.unregistered.x264_build;
441
442
2/2
✓ Branch 0 taken 20 times.
✓ Branch 1 taken 42 times.
62 if (!h->cur_pic_ptr)
443 20 return 0;
444
445
2/2
✓ Branch 0 taken 27 times.
✓ Branch 1 taken 15 times.
42 if (!h->droppable) {
446 27 err = ff_h264_execute_ref_pic_marking(h);
447 27 h->poc.prev_poc_msb = h->poc.poc_msb;
448 27 h->poc.prev_poc_lsb = h->poc.poc_lsb;
449 }
450 42 h->poc.prev_frame_num_offset = h->poc.frame_num_offset;
451 42 h->poc.prev_frame_num = h->poc.frame_num;
452
453 42 h->recovery_frame = h1->recovery_frame;
454
455 42 return err;
456 }
457
458 50 int ff_h264_update_thread_context_for_user(AVCodecContext *dst,
459 const AVCodecContext *src)
460 {
461 50 H264Context *h = dst->priv_data;
462 50 const H264Context *h1 = src->priv_data;
463
464 50 h->is_avc = h1->is_avc;
465 50 h->nal_length_size = h1->nal_length_size;
466
467 50 return 0;
468 }
469
470 24196 static int h264_frame_start(H264Context *h)
471 {
472 H264Picture *pic;
473 int i, ret;
474 24196 const int pixel_shift = h->pixel_shift;
475
476
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 24196 times.
24196 if (!ff_thread_can_start_frame(h->avctx)) {
477 av_log(h->avctx, AV_LOG_ERROR, "Attempt to start a frame outside SETUP state\n");
478 return -1;
479 }
480
481 24196 release_unused_pictures(h, 1);
482 24196 h->cur_pic_ptr = NULL;
483
484 24196 i = find_unused_picture(h);
485
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 24196 times.
24196 if (i < 0) {
486 av_log(h->avctx, AV_LOG_ERROR, "no frame buffer available\n");
487 return i;
488 }
489 24196 pic = &h->DPB[i];
490
491
2/2
✓ Branch 0 taken 16828 times.
✓ Branch 1 taken 7368 times.
24196 pic->reference = h->droppable ? 0 : h->picture_structure;
492 24196 pic->f->coded_picture_number = h->coded_picture_number++;
493 24196 pic->field_picture = h->picture_structure != PICT_FRAME;
494 24196 pic->frame_num = h->poc.frame_num;
495 /*
496 * Zero key_frame here; IDR markings per slice in frame or fields are ORed
497 * in later.
498 * See decode_nal_units().
499 */
500 24196 pic->f->key_frame = 0;
501 24196 pic->mmco_reset = 0;
502 24196 pic->recovered = 0;
503 24196 pic->invalid_gap = 0;
504 24196 pic->sei_recovery_frame_cnt = h->sei.recovery_point.recovery_frame_cnt;
505
506 24196 pic->f->pict_type = h->slice_ctx[0].slice_type;
507
508 24196 pic->f->crop_left = h->crop_left;
509 24196 pic->f->crop_right = h->crop_right;
510 24196 pic->f->crop_top = h->crop_top;
511 24196 pic->f->crop_bottom = h->crop_bottom;
512
513
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 24196 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
24196 pic->needs_fg = h->sei.common.film_grain_characteristics.present && !h->avctx->hwaccel &&
514 !(h->avctx->export_side_data & AV_CODEC_EXPORT_DATA_FILM_GRAIN);
515
516
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 24196 times.
24196 if ((ret = alloc_picture(h, pic)) < 0)
517 return ret;
518
519 24196 h->cur_pic_ptr = pic;
520 24196 ff_h264_unref_picture(h, &h->cur_pic);
521 if (CONFIG_ERROR_RESILIENCE) {
522 24196 ff_h264_set_erpic(&h->er.cur_pic, NULL);
523 }
524
525
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 24196 times.
24196 if ((ret = ff_h264_ref_picture(h, &h->cur_pic, h->cur_pic_ptr)) < 0)
526 return ret;
527
528
2/2
✓ Branch 0 taken 24496 times.
✓ Branch 1 taken 24196 times.
48692 for (i = 0; i < h->nb_slice_ctx; i++) {
529 24496 h->slice_ctx[i].linesize = h->cur_pic_ptr->f->linesize[0];
530 24496 h->slice_ctx[i].uvlinesize = h->cur_pic_ptr->f->linesize[1];
531 }
532
533
2/2
✓ Branch 0 taken 23896 times.
✓ Branch 1 taken 300 times.
24196 if (CONFIG_ERROR_RESILIENCE && h->enable_er) {
534 23896 ff_er_frame_start(&h->er);
535 23896 ff_h264_set_erpic(&h->er.last_pic, NULL);
536 23896 ff_h264_set_erpic(&h->er.next_pic, NULL);
537 }
538
539
2/2
✓ Branch 0 taken 387136 times.
✓ Branch 1 taken 24196 times.
411332 for (i = 0; i < 16; i++) {
540 387136 h->block_offset[i] = (4 * ((scan8[i] - scan8[0]) & 7) << pixel_shift) + 4 * pic->f->linesize[0] * ((scan8[i] - scan8[0]) >> 3);
541 387136 h->block_offset[48 + i] = (4 * ((scan8[i] - scan8[0]) & 7) << pixel_shift) + 8 * pic->f->linesize[0] * ((scan8[i] - scan8[0]) >> 3);
542 }
543
2/2
✓ Branch 0 taken 387136 times.
✓ Branch 1 taken 24196 times.
411332 for (i = 0; i < 16; i++) {
544 387136 h->block_offset[16 + i] =
545 387136 h->block_offset[32 + i] = (4 * ((scan8[i] - scan8[0]) & 7) << pixel_shift) + 4 * pic->f->linesize[1] * ((scan8[i] - scan8[0]) >> 3);
546 387136 h->block_offset[48 + 16 + i] =
547 387136 h->block_offset[48 + 32 + i] = (4 * ((scan8[i] - scan8[0]) & 7) << pixel_shift) + 8 * pic->f->linesize[1] * ((scan8[i] - scan8[0]) >> 3);
548 }
549
550 /* We mark the current picture as non-reference after allocating it, so
551 * that if we break out due to an error it can be released automatically
552 * in the next ff_mpv_frame_start().
553 */
554 24196 h->cur_pic_ptr->reference = 0;
555
556 24196 h->cur_pic_ptr->field_poc[0] = h->cur_pic_ptr->field_poc[1] = INT_MAX;
557
558 24196 h->next_output_pic = NULL;
559
560 24196 h->postpone_filter = 0;
561
562
4/4
✓ Branch 0 taken 2856 times.
✓ Branch 1 taken 21340 times.
✓ Branch 2 taken 2348 times.
✓ Branch 3 taken 508 times.
24196 h->mb_aff_frame = h->ps.sps->mb_aff && (h->picture_structure == PICT_FRAME);
563
564
2/2
✓ Branch 0 taken 2046 times.
✓ Branch 1 taken 22150 times.
24196 if (h->sei.common.unregistered.x264_build >= 0)
565 2046 h->x264_build = h->sei.common.unregistered.x264_build;
566
567 assert(h->cur_pic_ptr->long_ref == 0);
568
569 24196 return 0;
570 }
571
572 12143479 static av_always_inline void backup_mb_border(const H264Context *h, H264SliceContext *sl,
573 uint8_t *src_y,
574 uint8_t *src_cb, uint8_t *src_cr,
575 int linesize, int uvlinesize,
576 int simple)
577 {
578 uint8_t *top_border;
579 12143479 int top_idx = 1;
580 12143479 const int pixel_shift = h->pixel_shift;
581 12143479 int chroma444 = CHROMA444(h);
582 12143479 int chroma422 = CHROMA422(h);
583
584 12143479 src_y -= linesize;
585 12143479 src_cb -= uvlinesize;
586 12143479 src_cr -= uvlinesize;
587
588
3/4
✓ Branch 0 taken 12143479 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2016096 times.
✓ Branch 3 taken 10127383 times.
12143479 if (!simple && FRAME_MBAFF(h)) {
589
2/2
✓ Branch 0 taken 1008048 times.
✓ Branch 1 taken 1008048 times.
2016096 if (sl->mb_y & 1) {
590
2/2
✓ Branch 0 taken 747670 times.
✓ Branch 1 taken 260378 times.
1008048 if (!MB_MBAFF(sl)) {
591 747670 top_border = sl->top_borders[0][sl->mb_x];
592 747670 AV_COPY128(top_border, src_y + 15 * linesize);
593
2/2
✓ Branch 0 taken 213139 times.
✓ Branch 1 taken 534531 times.
747670 if (pixel_shift)
594 213139 AV_COPY128(top_border + 16, src_y + 15 * linesize + 16);
595 if (simple || !CONFIG_GRAY || !(h->flags & AV_CODEC_FLAG_GRAY)) {
596
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 747670 times.
747670 if (chroma444) {
597 if (pixel_shift) {
598 AV_COPY128(top_border + 32, src_cb + 15 * uvlinesize);
599 AV_COPY128(top_border + 48, src_cb + 15 * uvlinesize + 16);
600 AV_COPY128(top_border + 64, src_cr + 15 * uvlinesize);
601 AV_COPY128(top_border + 80, src_cr + 15 * uvlinesize + 16);
602 } else {
603 AV_COPY128(top_border + 16, src_cb + 15 * uvlinesize);
604 AV_COPY128(top_border + 32, src_cr + 15 * uvlinesize);
605 }
606
2/2
✓ Branch 0 taken 106510 times.
✓ Branch 1 taken 641160 times.
747670 } else if (chroma422) {
607
1/2
✓ Branch 0 taken 106510 times.
✗ Branch 1 not taken.
106510 if (pixel_shift) {
608 106510 AV_COPY128(top_border + 32, src_cb + 15 * uvlinesize);
609 106510 AV_COPY128(top_border + 48, src_cr + 15 * uvlinesize);
610 } else {
611 AV_COPY64(top_border + 16, src_cb + 15 * uvlinesize);
612 AV_COPY64(top_border + 24, src_cr + 15 * uvlinesize);
613 }
614 } else {
615
2/2
✓ Branch 0 taken 106629 times.
✓ Branch 1 taken 534531 times.
641160 if (pixel_shift) {
616 106629 AV_COPY128(top_border + 32, src_cb + 7 * uvlinesize);
617 106629 AV_COPY128(top_border + 48, src_cr + 7 * uvlinesize);
618 } else {
619 534531 AV_COPY64(top_border + 16, src_cb + 7 * uvlinesize);
620 534531 AV_COPY64(top_border + 24, src_cr + 7 * uvlinesize);
621 }
622 }
623 }
624 }
625
2/2
✓ Branch 0 taken 260378 times.
✓ Branch 1 taken 747670 times.
1008048 } else if (MB_MBAFF(sl)) {
626 260378 top_idx = 0;
627 } else
628 747670 return;
629 }
630
631 11395809 top_border = sl->top_borders[top_idx][sl->mb_x];
632 /* There are two lines saved, the line above the top macroblock
633 * of a pair, and the line above the bottom macroblock. */
634 11395809 AV_COPY128(top_border, src_y + 16 * linesize);
635
2/2
✓ Branch 0 taken 1369541 times.
✓ Branch 1 taken 10026268 times.
11395809 if (pixel_shift)
636 1369541 AV_COPY128(top_border + 16, src_y + 16 * linesize + 16);
637
638 if (simple || !CONFIG_GRAY || !(h->flags & AV_CODEC_FLAG_GRAY)) {
639
2/2
✓ Branch 0 taken 80328 times.
✓ Branch 1 taken 11315481 times.
11395809 if (chroma444) {
640
2/2
✓ Branch 0 taken 19800 times.
✓ Branch 1 taken 60528 times.
80328 if (pixel_shift) {
641 19800 AV_COPY128(top_border + 32, src_cb + 16 * linesize);
642 19800 AV_COPY128(top_border + 48, src_cb + 16 * linesize + 16);
643 19800 AV_COPY128(top_border + 64, src_cr + 16 * linesize);
644 19800 AV_COPY128(top_border + 80, src_cr + 16 * linesize + 16);
645 } else {
646 60528 AV_COPY128(top_border + 16, src_cb + 16 * linesize);
647 60528 AV_COPY128(top_border + 32, src_cr + 16 * linesize);
648 }
649
2/2
✓ Branch 0 taken 588635 times.
✓ Branch 1 taken 10726846 times.
11315481 } else if (chroma422) {
650
2/2
✓ Branch 0 taken 584675 times.
✓ Branch 1 taken 3960 times.
588635 if (pixel_shift) {
651 584675 AV_COPY128(top_border + 32, src_cb + 16 * uvlinesize);
652 584675 AV_COPY128(top_border + 48, src_cr + 16 * uvlinesize);
653 } else {
654 3960 AV_COPY64(top_border + 16, src_cb + 16 * uvlinesize);
655 3960 AV_COPY64(top_border + 24, src_cr + 16 * uvlinesize);
656 }
657 } else {
658
2/2
✓ Branch 0 taken 765066 times.
✓ Branch 1 taken 9961780 times.
10726846 if (pixel_shift) {
659 765066 AV_COPY128(top_border + 32, src_cb + 8 * uvlinesize);
660 765066 AV_COPY128(top_border + 48, src_cr + 8 * uvlinesize);
661 } else {
662 9961780 AV_COPY64(top_border + 16, src_cb + 8 * uvlinesize);
663 9961780 AV_COPY64(top_border + 24, src_cr + 8 * uvlinesize);
664 }
665 }
666 }
667 }
668
669 /**
670 * Initialize implicit_weight table.
671 * @param field 0/1 initialize the weight for interlaced MBAFF
672 * -1 initializes the rest
673 */
674 3301 static void implicit_weight_table(const H264Context *h, H264SliceContext *sl, int field)
675 {
676 int ref0, ref1, i, cur_poc, ref_start, ref_count0, ref_count1;
677
678
2/2
✓ Branch 0 taken 6602 times.
✓ Branch 1 taken 3301 times.
9903 for (i = 0; i < 2; i++) {
679 6602 sl->pwt.luma_weight_flag[i] = 0;
680 6602 sl->pwt.chroma_weight_flag[i] = 0;
681 }
682
683
2/2
✓ Branch 0 taken 1591 times.
✓ Branch 1 taken 1710 times.
3301 if (field < 0) {
684
1/2
✓ Branch 0 taken 1591 times.
✗ Branch 1 not taken.
1591 if (h->picture_structure == PICT_FRAME) {
685 1591 cur_poc = h->cur_pic_ptr->poc;
686 } else {
687 cur_poc = h->cur_pic_ptr->field_poc[h->picture_structure - 1];
688 }
689
6/6
✓ Branch 0 taken 417 times.
✓ Branch 1 taken 1174 times.
✓ Branch 2 taken 107 times.
✓ Branch 3 taken 310 times.
✓ Branch 4 taken 97 times.
✓ Branch 5 taken 10 times.
1591 if (sl->ref_count[0] == 1 && sl->ref_count[1] == 1 && !FRAME_MBAFF(h) &&
690
2/2
✓ Branch 0 taken 34 times.
✓ Branch 1 taken 63 times.
97 sl->ref_list[0][0].poc + (int64_t)sl->ref_list[1][0].poc == 2LL * cur_poc) {
691 34 sl->pwt.use_weight = 0;
692 34 sl->pwt.use_weight_chroma = 0;
693 34 return;
694 }
695 1557 ref_start = 0;
696 1557 ref_count0 = sl->ref_count[0];
697 1557 ref_count1 = sl->ref_count[1];
698 } else {
699 1710 cur_poc = h->cur_pic_ptr->field_poc[field];
700 1710 ref_start = 16;
701 1710 ref_count0 = 16 + 2 * sl->ref_count[0];
702 1710 ref_count1 = 16 + 2 * sl->ref_count[1];
703 }
704
705 3267 sl->pwt.use_weight = 2;
706 3267 sl->pwt.use_weight_chroma = 2;
707 3267 sl->pwt.luma_log2_weight_denom = 5;
708 3267 sl->pwt.chroma_log2_weight_denom = 5;
709
710
2/2
✓ Branch 0 taken 11618 times.
✓ Branch 1 taken 3267 times.
14885 for (ref0 = ref_start; ref0 < ref_count0; ref0++) {
711 11618 int64_t poc0 = sl->ref_list[0][ref0].poc;
712
2/2
✓ Branch 0 taken 21707 times.
✓ Branch 1 taken 11618 times.
33325 for (ref1 = ref_start; ref1 < ref_count1; ref1++) {
713 21707 int w = 32;
714
2/4
✓ Branch 0 taken 21707 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 21707 times.
✗ Branch 3 not taken.
21707 if (!sl->ref_list[0][ref0].parent->long_ref && !sl->ref_list[1][ref1].parent->long_ref) {
715 21707 int poc1 = sl->ref_list[1][ref1].poc;
716 21707 int td = av_clip_int8(poc1 - poc0);
717
2/2
✓ Branch 0 taken 21641 times.
✓ Branch 1 taken 66 times.
21707 if (td) {
718 21641 int tb = av_clip_int8(cur_poc - poc0);
719 21641 int tx = (16384 + (FFABS(td) >> 1)) / td;
720 21641 int dist_scale_factor = (tb * tx + 32) >> 8;
721
3/4
✓ Branch 0 taken 21637 times.
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 21637 times.
✗ Branch 3 not taken.
21641 if (dist_scale_factor >= -64 && dist_scale_factor <= 128)
722 21637 w = 64 - dist_scale_factor;
723 }
724 }
725
2/2
✓ Branch 0 taken 5827 times.
✓ Branch 1 taken 15880 times.
21707 if (field < 0) {
726 5827 sl->pwt.implicit_weight[ref0][ref1][0] =
727 5827 sl->pwt.implicit_weight[ref0][ref1][1] = w;
728 } else {
729 15880 sl->pwt.implicit_weight[ref0][ref1][field] = w;
730 }
731 }
732 }
733 }
734
735 /**
736 * initialize scan tables
737 */
738 574 static void init_scan_tables(H264Context *h)
739 {
740 int i;
741
2/2
✓ Branch 0 taken 9184 times.
✓ Branch 1 taken 574 times.
9758 for (i = 0; i < 16; i++) {
742 #define TRANSPOSE(x) ((x) >> 2) | (((x) << 2) & 0xF)
743 9184 h->zigzag_scan[i] = TRANSPOSE(ff_zigzag_scan[i]);
744 9184 h->field_scan[i] = TRANSPOSE(field_scan[i]);
745 #undef TRANSPOSE
746 }
747
2/2
✓ Branch 0 taken 36736 times.
✓ Branch 1 taken 574 times.
37310 for (i = 0; i < 64; i++) {
748 #define TRANSPOSE(x) ((x) >> 3) | (((x) & 7) << 3)
749 36736 h->zigzag_scan8x8[i] = TRANSPOSE(ff_zigzag_direct[i]);
750 36736 h->zigzag_scan8x8_cavlc[i] = TRANSPOSE(zigzag_scan8x8_cavlc[i]);
751 36736 h->field_scan8x8[i] = TRANSPOSE(field_scan8x8[i]);
752 36736 h->field_scan8x8_cavlc[i] = TRANSPOSE(field_scan8x8_cavlc[i]);
753 #undef TRANSPOSE
754 }
755
2/2
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 560 times.
574 if (h->ps.sps->transform_bypass) { // FIXME same ugly
756 14 memcpy(h->zigzag_scan_q0 , ff_zigzag_scan , sizeof(h->zigzag_scan_q0 ));
757 14 memcpy(h->zigzag_scan8x8_q0 , ff_zigzag_direct , sizeof(h->zigzag_scan8x8_q0 ));
758 14 memcpy(h->zigzag_scan8x8_cavlc_q0 , zigzag_scan8x8_cavlc , sizeof(h->zigzag_scan8x8_cavlc_q0));
759 14 memcpy(h->field_scan_q0 , field_scan , sizeof(h->field_scan_q0 ));
760 14 memcpy(h->field_scan8x8_q0 , field_scan8x8 , sizeof(h->field_scan8x8_q0 ));
761 14 memcpy(h->field_scan8x8_cavlc_q0 , field_scan8x8_cavlc , sizeof(h->field_scan8x8_cavlc_q0 ));
762 } else {
763 560 memcpy(h->zigzag_scan_q0 , h->zigzag_scan , sizeof(h->zigzag_scan_q0 ));
764 560 memcpy(h->zigzag_scan8x8_q0 , h->zigzag_scan8x8 , sizeof(h->zigzag_scan8x8_q0 ));
765 560 memcpy(h->zigzag_scan8x8_cavlc_q0 , h->zigzag_scan8x8_cavlc , sizeof(h->zigzag_scan8x8_cavlc_q0));
766 560 memcpy(h->field_scan_q0 , h->field_scan , sizeof(h->field_scan_q0 ));
767 560 memcpy(h->field_scan8x8_q0 , h->field_scan8x8 , sizeof(h->field_scan8x8_q0 ));
768 560 memcpy(h->field_scan8x8_cavlc_q0 , h->field_scan8x8_cavlc , sizeof(h->field_scan8x8_cavlc_q0 ));
769 }
770 574 }
771
772 27106 static enum AVPixelFormat get_pixel_format(H264Context *h, int force_callback)
773 {
774 #define HWACCEL_MAX (CONFIG_H264_DXVA2_HWACCEL + \
775 (CONFIG_H264_D3D11VA_HWACCEL * 2) + \
776 CONFIG_H264_NVDEC_HWACCEL + \
777 CONFIG_H264_VAAPI_HWACCEL + \
778 CONFIG_H264_VIDEOTOOLBOX_HWACCEL + \
779 CONFIG_H264_VDPAU_HWACCEL)
780 27106 enum AVPixelFormat pix_fmts[HWACCEL_MAX + 2], *fmt = pix_fmts;
781 27106 const enum AVPixelFormat *choices = pix_fmts;
782 int i;
783
784
3/6
✓ Branch 0 taken 155 times.
✓ Branch 1 taken 367 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 26584 times.
✗ Branch 5 not taken.
27106 switch (h->ps.sps->bit_depth_luma) {
785 155 case 9:
786
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 155 times.
155 if (CHROMA444(h)) {
787 if (h->avctx->colorspace == AVCOL_SPC_RGB) {
788 *fmt++ = AV_PIX_FMT_GBRP9;
789 } else
790 *fmt++ = AV_PIX_FMT_YUV444P9;
791
2/2
✓ Branch 0 taken 52 times.
✓ Branch 1 taken 103 times.
155 } else if (CHROMA422(h))
792 52 *fmt++ = AV_PIX_FMT_YUV422P9;
793 else
794 103 *fmt++ = AV_PIX_FMT_YUV420P9;
795 155 break;
796 367 case 10:
797 #if CONFIG_H264_VIDEOTOOLBOX_HWACCEL
798 if (h->avctx->colorspace != AVCOL_SPC_RGB)
799 *fmt++ = AV_PIX_FMT_VIDEOTOOLBOX;
800 #endif
801
2/2
✓ Branch 0 taken 51 times.
✓ Branch 1 taken 316 times.
367 if (CHROMA444(h)) {
802
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 51 times.
51 if (h->avctx->colorspace == AVCOL_SPC_RGB) {
803 *fmt++ = AV_PIX_FMT_GBRP10;
804 } else
805 51 *fmt++ = AV_PIX_FMT_YUV444P10;
806
2/2
✓ Branch 0 taken 164 times.
✓ Branch 1 taken 152 times.
316 } else if (CHROMA422(h))
807 164 *fmt++ = AV_PIX_FMT_YUV422P10;
808 else
809 152 *fmt++ = AV_PIX_FMT_YUV420P10;
810 367 break;
811 case 12:
812 if (CHROMA444(h)) {
813 if (h->avctx->colorspace == AVCOL_SPC_RGB) {
814 *fmt++ = AV_PIX_FMT_GBRP12;
815 } else
816 *fmt++ = AV_PIX_FMT_YUV444P12;
817 } else if (CHROMA422(h))
818 *fmt++ = AV_PIX_FMT_YUV422P12;
819 else
820 *fmt++ = AV_PIX_FMT_YUV420P12;
821 break;
822 case 14:
823 if (CHROMA444(h)) {
824 if (h->avctx->colorspace == AVCOL_SPC_RGB) {
825 *fmt++ = AV_PIX_FMT_GBRP14;
826 } else
827 *fmt++ = AV_PIX_FMT_YUV444P14;
828 } else if (CHROMA422(h))
829 *fmt++ = AV_PIX_FMT_YUV422P14;
830 else
831 *fmt++ = AV_PIX_FMT_YUV420P14;
832 break;
833 26584 case 8:
834 #if CONFIG_H264_VDPAU_HWACCEL
835 26584 *fmt++ = AV_PIX_FMT_VDPAU;
836 #endif
837 #if CONFIG_H264_NVDEC_HWACCEL
838 *fmt++ = AV_PIX_FMT_CUDA;
839 #endif
840 #if CONFIG_H264_VIDEOTOOLBOX_HWACCEL
841 if (h->avctx->colorspace != AVCOL_SPC_RGB)
842 *fmt++ = AV_PIX_FMT_VIDEOTOOLBOX;
843 #endif
844
2/2
✓ Branch 0 taken 484 times.
✓ Branch 1 taken 26100 times.
26584 if (CHROMA444(h)) {
845
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 484 times.
484 if (h->avctx->colorspace == AVCOL_SPC_RGB)
846 *fmt++ = AV_PIX_FMT_GBRP;
847
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 482 times.
484 else if (h->avctx->color_range == AVCOL_RANGE_JPEG)
848 2 *fmt++ = AV_PIX_FMT_YUVJ444P;
849 else
850 482 *fmt++ = AV_PIX_FMT_YUV444P;
851
2/2
✓ Branch 0 taken 22 times.
✓ Branch 1 taken 26078 times.
26100 } else if (CHROMA422(h)) {
852
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 22 times.
22 if (h->avctx->color_range == AVCOL_RANGE_JPEG)
853 *fmt++ = AV_PIX_FMT_YUVJ422P;
854 else
855 22 *fmt++ = AV_PIX_FMT_YUV422P;
856 } else {
857 #if CONFIG_H264_DXVA2_HWACCEL
858 *fmt++ = AV_PIX_FMT_DXVA2_VLD;
859 #endif
860 #if CONFIG_H264_D3D11VA_HWACCEL
861 *fmt++ = AV_PIX_FMT_D3D11VA_VLD;
862 *fmt++ = AV_PIX_FMT_D3D11;
863 #endif
864 #if CONFIG_H264_VAAPI_HWACCEL
865 26078 *fmt++ = AV_PIX_FMT_VAAPI;
866 #endif
867
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 26078 times.
26078 if (h->avctx->codec->pix_fmts)
868 choices = h->avctx->codec->pix_fmts;
869
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 26075 times.
26078 else if (h->avctx->color_range == AVCOL_RANGE_JPEG)
870 3 *fmt++ = AV_PIX_FMT_YUVJ420P;
871 else
872 26075 *fmt++ = AV_PIX_FMT_YUV420P;
873 }
874 26584 break;
875 default:
876 av_log(h->avctx, AV_LOG_ERROR,
877 "Unsupported bit depth %d\n", h->ps.sps->bit_depth_luma);
878 return AVERROR_INVALIDDATA;
879 }
880
881 27106 *fmt = AV_PIX_FMT_NONE;
882
883
2/2
✓ Branch 0 taken 79768 times.
✓ Branch 1 taken 561 times.
80329 for (i=0; choices[i] != AV_PIX_FMT_NONE; i++)
884
4/4
✓ Branch 0 taken 26792 times.
✓ Branch 1 taken 52976 times.
✓ Branch 2 taken 26545 times.
✓ Branch 3 taken 247 times.
79768 if (choices[i] == h->avctx->pix_fmt && !force_callback)
885 26545 return choices[i];
886 561 return ff_thread_get_format(h->avctx, choices);
887 }
888
889 /* export coded and cropped frame dimensions to AVCodecContext */
890 26856 static void init_dimensions(H264Context *h)
891 {
892 26856 const SPS *sps = (const SPS*)h->ps.sps;
893 26856 int cr = sps->crop_right;
894 26856 int cl = sps->crop_left;
895 26856 int ct = sps->crop_top;
896 26856 int cb = sps->crop_bottom;
897 26856 int width = h->width - (cr + cl);
898 26856 int height = h->height - (ct + cb);
899
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 26856 times.
26856 av_assert0(sps->crop_right + sps->crop_left < (unsigned)h->width);
900
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 26856 times.
26856 av_assert0(sps->crop_top + sps->crop_bottom < (unsigned)h->height);
901
902 /* handle container cropping */
903
3/4
✓ Branch 0 taken 24603 times.
✓ Branch 1 taken 2253 times.
✓ Branch 2 taken 24603 times.
✗ Branch 3 not taken.
26856 if (h->width_from_caller > 0 && h->height_from_caller > 0 &&
904
3/4
✓ Branch 0 taken 24602 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 24602 times.
✗ Branch 3 not taken.
24603 !sps->crop_top && !sps->crop_left &&
905
2/2
✓ Branch 0 taken 24595 times.
✓ Branch 1 taken 7 times.
24602 FFALIGN(h->width_from_caller, 16) == FFALIGN(width, 16) &&
906
1/2
✓ Branch 0 taken 24595 times.
✗ Branch 1 not taken.
24595 FFALIGN(h->height_from_caller, 16) == FFALIGN(height, 16) &&
907
1/2
✓ Branch 0 taken 24595 times.
✗ Branch 1 not taken.
24595 h->width_from_caller <= width &&
908
2/2
✓ Branch 0 taken 24594 times.
✓ Branch 1 taken 1 times.
24595 h->height_from_caller <= height) {
909 24594 width = h->width_from_caller;
910 24594 height = h->height_from_caller;
911 24594 cl = 0;
912 24594 ct = 0;
913 24594 cr = h->width - width;
914 24594 cb = h->height - height;
915 } else {
916 2262 h->width_from_caller = 0;
917 2262 h->height_from_caller = 0;
918 }
919
920 26856 h->avctx->coded_width = h->width;
921 26856 h->avctx->coded_height = h->height;
922 26856 h->avctx->width = width;
923 26856 h->avctx->height = height;
924 26856 h->crop_right = cr;
925 26856 h->crop_left = cl;
926 26856 h->crop_top = ct;
927 26856 h->crop_bottom = cb;
928 26856 }
929
930 574 static int h264_slice_header_init(H264Context *h)
931 {
932 574 const SPS *sps = h->ps.sps;
933 int i, ret;
934
935
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 574 times.
574 if (!sps) {
936 ret = AVERROR_INVALIDDATA;
937 goto fail;
938 }
939
940 574 ff_set_sar(h->avctx, sps->vui.sar);
941 574 av_pix_fmt_get_chroma_sub_sample(h->avctx->pix_fmt,
942 &h->chroma_x_shift, &h->chroma_y_shift);
943
944
2/2
✓ Branch 0 taken 164 times.
✓ Branch 1 taken 410 times.
574 if (sps->timing_info_present_flag) {
945 164 int64_t den = sps->time_scale;
946
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 164 times.
164 if (h->x264_build < 44U)
947 den *= 2;
948 164 av_reduce(&h->avctx->framerate.den, &h->avctx->framerate.num,
949 164 sps->num_units_in_tick * h->avctx->ticks_per_frame, den, 1 << 30);
950 }
951
952 574 ff_h264_free_tables(h);
953
954 574 h->first_field = 0;
955 574 h->prev_interlaced_frame = 1;
956
957 574 init_scan_tables(h);
958 574 ret = ff_h264_alloc_tables(h);
959
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 574 times.
574 if (ret < 0) {
960 av_log(h->avctx, AV_LOG_ERROR, "Could not allocate memory\n");
961 goto fail;
962 }
963
964
2/4
✓ Branch 0 taken 574 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 574 times.
✗ Branch 3 not taken.
574 if (sps->bit_depth_luma < 8 || sps->bit_depth_luma > 14 ||
965
2/4
✓ Branch 0 taken 574 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 574 times.
574 sps->bit_depth_luma == 11 || sps->bit_depth_luma == 13
966 ) {
967 av_log(h->avctx, AV_LOG_ERROR, "Unsupported bit depth %d\n",
968 sps->bit_depth_luma);
969 ret = AVERROR_INVALIDDATA;
970 goto fail;
971 }
972
973 574 h->cur_bit_depth_luma =
974 574 h->avctx->bits_per_raw_sample = sps->bit_depth_luma;
975 574 h->cur_chroma_format_idc = sps->chroma_format_idc;
976 574 h->pixel_shift = sps->bit_depth_luma > 8;
977 574 h->chroma_format_idc = sps->chroma_format_idc;
978 574 h->bit_depth_luma = sps->bit_depth_luma;
979
980 574 ff_h264dsp_init(&h->h264dsp, sps->bit_depth_luma,
981 574 sps->chroma_format_idc);
982 574 ff_h264chroma_init(&h->h264chroma, sps->bit_depth_chroma);
983 574 ff_h264qpel_init(&h->h264qpel, sps->bit_depth_luma);
984 574 ff_h264_pred_init(&h->hpc, AV_CODEC_ID_H264, sps->bit_depth_luma,
985 574 sps->chroma_format_idc);
986 574 ff_videodsp_init(&h->vdsp, sps->bit_depth_luma);
987
988
2/2
✓ Branch 0 taken 573 times.
✓ Branch 1 taken 1 times.
574 if (!HAVE_THREADS || !(h->avctx->active_thread_type & FF_THREAD_SLICE)) {
989 573 ff_h264_slice_context_init(h, &h->slice_ctx[0]);
990 } else {
991
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 times.
3 for (i = 0; i < h->nb_slice_ctx; i++) {
992 2 H264SliceContext *sl = &h->slice_ctx[i];
993
994 2 sl->h264 = h;
995 2 sl->intra4x4_pred_mode = h->intra4x4_pred_mode + i * 8 * 2 * h->mb_stride;
996 2 sl->mvd_table[0] = h->mvd_table[0] + i * 8 * 2 * h->mb_stride;
997 2 sl->mvd_table[1] = h->mvd_table[1] + i * 8 * 2 * h->mb_stride;
998
999 2 ff_h264_slice_context_init(h, sl);
1000 }
1001 }
1002
1003 574 h->context_initialized = 1;
1004
1005 574 return 0;
1006 fail:
1007 ff_h264_free_tables(h);
1008 h->context_initialized = 0;
1009 return ret;
1010 }
1011
1012 53096 static enum AVPixelFormat non_j_pixfmt(enum AVPixelFormat a)
1013 {
1014
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 53094 times.
53096 switch (a) {
1015 2 case AV_PIX_FMT_YUVJ420P: return AV_PIX_FMT_YUV420P;
1016 case AV_PIX_FMT_YUVJ422P: return AV_PIX_FMT_YUV422P;
1017 case AV_PIX_FMT_YUVJ444P: return AV_PIX_FMT_YUV444P;
1018 53094 default:
1019 53094 return a;
1020 }
1021 }
1022
1023 26856 static int h264_init_ps(H264Context *h, const H264SliceContext *sl, int first_slice)
1024 {
1025 const SPS *sps;
1026 26856 int needs_reinit = 0, must_reinit, ret;
1027
1028
2/2
✓ Branch 0 taken 26663 times.
✓ Branch 1 taken 193 times.
26856 if (first_slice) {
1029 26663 av_buffer_unref(&h->ps.pps_ref);
1030 26663 h->ps.pps = NULL;
1031 26663 h->ps.pps_ref = av_buffer_ref(h->ps.pps_list[sl->pps_id]);
1032
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 26663 times.
26663 if (!h->ps.pps_ref)
1033 return AVERROR(ENOMEM);
1034 26663 h->ps.pps = (const PPS*)h->ps.pps_ref->data;
1035 }
1036
1037
2/2
✓ Branch 0 taken 563 times.
✓ Branch 1 taken 26293 times.
26856 if (h->ps.sps != h->ps.pps->sps) {
1038 563 h->ps.sps = (const SPS*)h->ps.pps->sps;
1039
1040
2/2
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 556 times.
563 if (h->mb_width != h->ps.sps->mb_width ||
1041
1/2
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
7 h->mb_height != h->ps.sps->mb_height ||
1042
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 1 times.
7 h->cur_bit_depth_luma != h->ps.sps->bit_depth_luma ||
1043
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 5 times.
6 h->cur_chroma_format_idc != h->ps.sps->chroma_format_idc
1044 )
1045 558 needs_reinit = 1;
1046
1047
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 553 times.
563 if (h->bit_depth_luma != h->ps.sps->bit_depth_luma ||
1048
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 9 times.
10 h->chroma_format_idc != h->ps.sps->chroma_format_idc)
1049 554 needs_reinit = 1;
1050 }
1051 26856 sps = h->ps.sps;
1052
1053
2/2
✓ Branch 0 taken 26305 times.
✓ Branch 1 taken 551 times.
53161 must_reinit = (h->context_initialized &&
1054
2/2
✓ Branch 0 taken 26300 times.
✓ Branch 1 taken 5 times.
26305 ( 16*sps->mb_width != h->avctx->coded_width
1055
1/2
✓ Branch 0 taken 26300 times.
✗ Branch 1 not taken.
26300 || 16*sps->mb_height != h->avctx->coded_height
1056
2/2
✓ Branch 0 taken 26299 times.
✓ Branch 1 taken 1 times.
26300 || h->cur_bit_depth_luma != sps->bit_depth_luma
1057
2/2
✓ Branch 0 taken 26298 times.
✓ Branch 1 taken 1 times.
26299 || h->cur_chroma_format_idc != sps->chroma_format_idc
1058
1/2
✓ Branch 0 taken 26298 times.
✗ Branch 1 not taken.
26298 || h->mb_width != sps->mb_width
1059
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 26298 times.
26298 || h->mb_height != sps->mb_height
1060 ));
1061
2/2
✓ Branch 0 taken 26548 times.
✓ Branch 1 taken 308 times.
26856 if (h->avctx->pix_fmt == AV_PIX_FMT_NONE
1062
2/2
✓ Branch 3 taken 3 times.
✓ Branch 4 taken 26545 times.
26548 || (non_j_pixfmt(h->avctx->pix_fmt) != non_j_pixfmt(get_pixel_format(h, 0))))
1063 311 must_reinit = 1;
1064
1065
4/4
✓ Branch 0 taken 26663 times.
✓ Branch 1 taken 193 times.
✓ Branch 3 taken 60 times.
✓ Branch 4 taken 26603 times.
26856 if (first_slice && av_cmp_q(sps->vui.sar, h->avctx->sample_aspect_ratio))
1066 60 must_reinit = 1;
1067
1068
1/2
✓ Branch 0 taken 26856 times.
✗ Branch 1 not taken.
26856 if (!h->setup_finished) {
1069 26856 h->avctx->profile = ff_h264_get_profile(sps);
1070 26856 h->avctx->level = sps->level_idc;
1071 26856 h->avctx->refs = sps->ref_frame_count;
1072
1073 26856 h->mb_width = sps->mb_width;
1074 26856 h->mb_height = sps->mb_height;
1075 26856 h->mb_num = h->mb_width * h->mb_height;
1076 26856 h->mb_stride = h->mb_width + 1;
1077
1078 26856 h->b_stride = h->mb_width * 4;
1079
1080 26856 h->chroma_y_shift = sps->chroma_format_idc <= 1; // 400 uses yuv420p
1081
1082 26856 h->width = 16 * h->mb_width;
1083 26856 h->height = 16 * h->mb_height;
1084
1085 26856 init_dimensions(h);
1086
1087
2/2
✓ Branch 0 taken 1215 times.
✓ Branch 1 taken 25641 times.
26856 if (sps->vui.video_signal_type_present_flag) {
1088 2430 h->avctx->color_range = sps->vui.video_full_range_flag > 0 ? AVCOL_RANGE_JPEG
1089
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1213 times.
1215 : AVCOL_RANGE_MPEG;
1090
2/2
✓ Branch 0 taken 1117 times.
✓ Branch 1 taken 98 times.
1215 if (sps->vui.colour_description_present_flag) {
1091
2/2
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 1099 times.
1117 if (h->avctx->colorspace != sps->vui.matrix_coeffs)
1092 18 needs_reinit = 1;
1093 1117 h->avctx->color_primaries = sps->vui.colour_primaries;
1094 1117 h->avctx->color_trc = sps->vui.transfer_characteristics;
1095 1117 h->avctx->colorspace = sps->vui.matrix_coeffs;
1096 }
1097 }
1098
1099
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 26856 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
26856 if (h->sei.common.alternative_transfer.present &&
1100 av_color_transfer_name(h->sei.common.alternative_transfer.preferred_transfer_characteristics) &&
1101 h->sei.common.alternative_transfer.preferred_transfer_characteristics != AVCOL_TRC_UNSPECIFIED) {
1102 h->avctx->color_trc = h->sei.common.alternative_transfer.preferred_transfer_characteristics;
1103 }
1104 }
1105 26856 h->avctx->chroma_sample_location = sps->vui.chroma_location;
1106
1107
5/6
✓ Branch 0 taken 26305 times.
✓ Branch 1 taken 551 times.
✓ Branch 2 taken 26298 times.
✓ Branch 3 taken 7 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 26298 times.
26856 if (!h->context_initialized || must_reinit || needs_reinit) {
1108 558 int flush_changes = h->context_initialized;
1109 558 h->context_initialized = 0;
1110
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 558 times.
558 if (sl != h->slice_ctx) {
1111 av_log(h->avctx, AV_LOG_ERROR,
1112 "changing width %d -> %d / height %d -> %d on "
1113 "slice %d\n",
1114 h->width, h->avctx->coded_width,
1115 h->height, h->avctx->coded_height,
1116 h->current_slice + 1);
1117 return AVERROR_INVALIDDATA;
1118 }
1119
1120 av_assert1(first_slice);
1121
1122
2/2
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 551 times.
558 if (flush_changes)
1123 7 ff_h264_flush_change(h);
1124
1125
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 558 times.
558 if ((ret = get_pixel_format(h, 1)) < 0)
1126 return ret;
1127 558 h->avctx->pix_fmt = ret;
1128
1129 558 av_log(h->avctx, AV_LOG_VERBOSE, "Reinit context to %dx%d, "
1130 558 "pix_fmt: %s\n", h->width, h->height, av_get_pix_fmt_name(h->avctx->pix_fmt));
1131
1132
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 558 times.
558 if ((ret = h264_slice_header_init(h)) < 0) {
1133 av_log(h->avctx, AV_LOG_ERROR,
1134 "h264_slice_header_init() failed\n");
1135 return ret;
1136 }
1137 }
1138
1139 26856 return 0;
1140 }
1141
1142 23490 static int h264_export_frame_props(H264Context *h)
1143 {
1144 23490 const SPS *sps = h->ps.sps;
1145 23490 H264Picture *cur = h->cur_pic_ptr;
1146 23490 AVFrame *out = cur->f;
1147 int ret;
1148
1149 23490 out->interlaced_frame = 0;
1150 23490 out->repeat_pict = 0;
1151
1152 /* Signal interlacing information externally. */
1153 /* Prioritize picture timing SEI information over used
1154 * decoding process if it exists. */
1155
2/2
✓ Branch 0 taken 1420 times.
✓ Branch 1 taken 22070 times.
23490 if (h->sei.picture_timing.present) {
1156 1420 int ret = ff_h264_sei_process_picture_timing(&h->sei.picture_timing, sps,
1157 1420 h->avctx);
1158
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1420 times.
1420 if (ret < 0) {
1159 av_log(h->avctx, AV_LOG_ERROR, "Error processing a picture timing SEI\n");
1160 if (h->avctx->err_recognition & AV_EF_EXPLODE)
1161 return ret;
1162 h->sei.picture_timing.present = 0;
1163 }
1164 }
1165
1166
4/4
✓ Branch 0 taken 1498 times.
✓ Branch 1 taken 21992 times.
✓ Branch 2 taken 1412 times.
✓ Branch 3 taken 86 times.
24902 if (sps->pic_struct_present_flag && h->sei.picture_timing.present) {
1167 1412 H264SEIPictureTiming *pt = &h->sei.picture_timing;
1168
4/7
✓ Branch 0 taken 132 times.
✓ Branch 1 taken 376 times.
✓ Branch 2 taken 737 times.
✓ Branch 3 taken 167 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
1412 switch (pt->pic_struct) {
1169 132 case H264_SEI_PIC_STRUCT_FRAME:
1170 132 break;
1171 376 case H264_SEI_PIC_STRUCT_TOP_FIELD:
1172 case H264_SEI_PIC_STRUCT_BOTTOM_FIELD:
1173 376 out->interlaced_frame = 1;
1174 376 break;
1175 737 case H264_SEI_PIC_STRUCT_TOP_BOTTOM:
1176 case H264_SEI_PIC_STRUCT_BOTTOM_TOP:
1177
3/4
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 733 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 4 times.
737 if (FIELD_OR_MBAFF_PICTURE(h))
1178 733 out->interlaced_frame = 1;
1179 else
1180 // try to flag soft telecine progressive
1181 4 out->interlaced_frame = h->prev_interlaced_frame;
1182 737 break;
1183 167 case H264_SEI_PIC_STRUCT_TOP_BOTTOM_TOP:
1184 case H264_SEI_PIC_STRUCT_BOTTOM_TOP_BOTTOM:
1185 /* Signal the possibility of telecined film externally
1186 * (pic_struct 5,6). From these hints, let the applications
1187 * decide if they apply deinterlacing. */
1188 167 out->repeat_pict = 1;
1189 167 break;
1190 case H264_SEI_PIC_STRUCT_FRAME_DOUBLING:
1191 out->repeat_pict = 2;
1192 break;
1193 case H264_SEI_PIC_STRUCT_FRAME_TRIPLING:
1194 out->repeat_pict = 4;
1195 break;
1196 }
1197
1198
2/2
✓ Branch 0 taken 651 times.
✓ Branch 1 taken 761 times.
1412 if ((pt->ct_type & 3) &&
1199
2/2
✓ Branch 0 taken 487 times.
✓ Branch 1 taken 164 times.
651 pt->pic_struct <= H264_SEI_PIC_STRUCT_BOTTOM_TOP)
1200 487 out->interlaced_frame = (pt->ct_type & (1 << 1)) != 0;
1201 } else {
1202 /* Derive interlacing flag from used decoding process. */
1203
4/4
✓ Branch 0 taken 20491 times.
✓ Branch 1 taken 1587 times.
✓ Branch 2 taken 2821 times.
✓ Branch 3 taken 17670 times.
22078 out->interlaced_frame = FIELD_OR_MBAFF_PICTURE(h);
1204 }
1205 23490 h->prev_interlaced_frame = out->interlaced_frame;
1206
1207
2/2
✓ Branch 0 taken 3330 times.
✓ Branch 1 taken 20160 times.
23490 if (cur->field_poc[0] != cur->field_poc[1]) {
1208 /* Derive top_field_first from field pocs. */
1209 3330 out->top_field_first = cur->field_poc[0] < cur->field_poc[1];
1210 } else {
1211
4/4
✓ Branch 0 taken 225 times.
✓ Branch 1 taken 19935 times.
✓ Branch 2 taken 139 times.
✓ Branch 3 taken 86 times.
20160 if (sps->pic_struct_present_flag && h->sei.picture_timing.present) {
1212 /* Use picture timing SEI information. Even if it is a
1213 * information of a past frame, better than nothing. */
1214
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 ||
1215
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)
1216 3 out->top_field_first = 1;
1217 else
1218 136 out->top_field_first = 0;
1219
2/2
✓ Branch 0 taken 2554 times.
✓ Branch 1 taken 17467 times.
20021 } else if (out->interlaced_frame) {
1220 /* Default to top field first when pic_struct_present_flag
1221 * is not set but interlaced frame detected */
1222 2554 out->top_field_first = 1;
1223 } else {
1224 /* Most likely progressive */
1225 17467 out->top_field_first = 0;
1226 }
1227 }
1228
1229 23490 ret = ff_h2645_sei_to_frame(out, &h->sei.common, AV_CODEC_ID_H264, h->avctx,
1230 23490 &sps->vui, sps->bit_depth_luma, sps->bit_depth_chroma,
1231 23490 cur->poc + (h->poc_offset << 5));
1232
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 23490 times.
23490 if (ret < 0)
1233 return ret;
1234
1235
2/2
✓ Branch 0 taken 651 times.
✓ Branch 1 taken 22839 times.
23490 if (h->sei.picture_timing.timecode_cnt > 0) {
1236 uint32_t *tc_sd;
1237 char tcbuf[AV_TIMECODE_STR_SIZE];
1238
1239 651 AVFrameSideData *tcside = av_frame_new_side_data(out,
1240 AV_FRAME_DATA_S12M_TIMECODE,
1241 sizeof(uint32_t)*4);
1242
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 651 times.
651 if (!tcside)
1243 return AVERROR(ENOMEM);
1244
1245 651 tc_sd = (uint32_t*)tcside->data;
1246 651 tc_sd[0] = h->sei.picture_timing.timecode_cnt;
1247
1248
2/2
✓ Branch 0 taken 952 times.
✓ Branch 1 taken 651 times.
1603 for (int i = 0; i < tc_sd[0]; i++) {
1249 952 int drop = h->sei.picture_timing.timecode[i].dropframe;
1250 952 int hh = h->sei.picture_timing.timecode[i].hours;
1251 952 int mm = h->sei.picture_timing.timecode[i].minutes;
1252 952 int ss = h->sei.picture_timing.timecode[i].seconds;
1253 952 int ff = h->sei.picture_timing.timecode[i].frame;
1254
1255 952 tc_sd[i + 1] = av_timecode_get_smpte(h->avctx->framerate, drop, hh, mm, ss, ff);
1256 952 av_timecode_make_smpte_tc_string2(tcbuf, h->avctx->framerate, tc_sd[i + 1], 0, 0);
1257 952 av_dict_set(&out->metadata, "timecode", tcbuf, 0);
1258 }
1259 651 h->sei.picture_timing.timecode_cnt = 0;
1260 }
1261
1262 23490 return 0;
1263 }
1264
1265 23490 static int h264_select_output_frame(H264Context *h)
1266 {
1267 23490 const SPS *sps = h->ps.sps;
1268 23490 H264Picture *out = h->cur_pic_ptr;
1269 23490 H264Picture *cur = h->cur_pic_ptr;
1270 int i, pics, out_of_order, out_idx;
1271
1272 23490 cur->mmco_reset = h->mmco_reset;
1273 23490 h->mmco_reset = 0;
1274
1275
2/2
✓ Branch 0 taken 19958 times.
✓ Branch 1 taken 3532 times.
23490 if (sps->bitstream_restriction_flag ||
1276
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 19958 times.
19958 h->avctx->strict_std_compliance >= FF_COMPLIANCE_STRICT) {
1277 3532 h->avctx->has_b_frames = FFMAX(h->avctx->has_b_frames, sps->num_reorder_frames);
1278 }
1279
1280 384880 for (i = 0; 1; i++) {
1281
4/4
✓ Branch 0 taken 369849 times.
✓ Branch 1 taken 15031 times.
✓ Branch 2 taken 8459 times.
✓ Branch 3 taken 361390 times.
384880 if(i == H264_MAX_DPB_FRAMES || cur->poc < h->last_pocs[i]){
1282
1/2
✓ Branch 0 taken 23490 times.
✗ Branch 1 not taken.
23490 if(i)
1283 23490 h->last_pocs[i-1] = cur->poc;
1284 23490 break;
1285
2/2
✓ Branch 0 taken 337900 times.
✓ Branch 1 taken 23490 times.
361390 } else if(i) {
1286 337900 h->last_pocs[i-1]= h->last_pocs[i];
1287 }
1288 }
1289 23490 out_of_order = H264_MAX_DPB_FRAMES - i;
1290
2/2
✓ Branch 0 taken 15922 times.
✓ Branch 1 taken 7568 times.
23490 if( cur->f->pict_type == AV_PICTURE_TYPE_B
1291
4/4
✓ Branch 0 taken 14834 times.
✓ Branch 1 taken 1088 times.
✓ Branch 2 taken 4619 times.
✓ Branch 3 taken 10215 times.
15922 || (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))
1292 12187 out_of_order = FFMAX(out_of_order, 1);
1293
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 23490 times.
23490 if (out_of_order == H264_MAX_DPB_FRAMES) {
1294 av_log(h->avctx, AV_LOG_VERBOSE, "Invalid POC %d<%d\n", cur->poc, h->last_pocs[0]);
1295 for (i = 1; i < H264_MAX_DPB_FRAMES; i++)
1296 h->last_pocs[i] = INT_MIN;
1297 h->last_pocs[0] = cur->poc;
1298 cur->mmco_reset = 1;
1299
3/4
✓ Branch 0 taken 137 times.
✓ Branch 1 taken 23353 times.
✓ Branch 2 taken 137 times.
✗ Branch 3 not taken.
23490 } else if(h->avctx->has_b_frames < out_of_order && !sps->bitstream_restriction_flag){
1300
2/2
✓ Branch 0 taken 32 times.
✓ Branch 1 taken 105 times.
137 int loglevel = h->avctx->frame_number > 1 ? AV_LOG_WARNING : AV_LOG_VERBOSE;
1301 137 av_log(h->avctx, loglevel, "Increasing reorder buffer to %d\n", out_of_order);
1302 137 h->avctx->has_b_frames = out_of_order;
1303 }
1304
1305 23490 pics = 0;
1306
2/2
✓ Branch 0 taken 29129 times.
✓ Branch 1 taken 23490 times.
52619 while (h->delayed_pic[pics])
1307 29129 pics++;
1308
1309
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 23490 times.
23490 av_assert0(pics <= H264_MAX_DPB_FRAMES);
1310
1311 23490 h->delayed_pic[pics++] = cur;
1312
2/2
✓ Branch 0 taken 22015 times.
✓ Branch 1 taken 1475 times.
23490 if (cur->reference == 0)
1313 22015 cur->reference = DELAYED_PIC_REF;
1314
1315 23490 out = h->delayed_pic[0];
1316 23490 out_idx = 0;
1317 23490 for (i = 1; h->delayed_pic[i] &&
1318
4/4
✓ Branch 0 taken 26679 times.
✓ Branch 1 taken 23042 times.
✓ Branch 2 taken 26318 times.
✓ Branch 3 taken 361 times.
49721 !h->delayed_pic[i]->f->key_frame &&
1319
2/2
✓ Branch 0 taken 26231 times.
✓ Branch 1 taken 87 times.
26318 !h->delayed_pic[i]->mmco_reset;
1320 26231 i++)
1321
2/2
✓ Branch 0 taken 9757 times.
✓ Branch 1 taken 16474 times.
26231 if (h->delayed_pic[i]->poc < out->poc) {
1322 9757 out = h->delayed_pic[i];
1323 9757 out_idx = i;
1324 }
1325
2/2
✓ Branch 0 taken 10257 times.
✓ Branch 1 taken 13233 times.
23490 if (h->avctx->has_b_frames == 0 &&
1326
4/4
✓ Branch 0 taken 9470 times.
✓ Branch 1 taken 787 times.
✓ Branch 2 taken 12 times.
✓ Branch 3 taken 9458 times.
10257 (h->delayed_pic[0]->f->key_frame || h->delayed_pic[0]->mmco_reset))
1327 799 h->next_outputed_poc = INT_MIN;
1328 23490 out_of_order = out->poc < h->next_outputed_poc;
1329
1330
4/4
✓ Branch 0 taken 23373 times.
✓ Branch 1 taken 117 times.
✓ Branch 2 taken 22875 times.
✓ Branch 3 taken 498 times.
23490 if (out_of_order || pics > h->avctx->has_b_frames) {
1331 22992 out->reference &= ~DELAYED_PIC_REF;
1332
2/2
✓ Branch 0 taken 37278 times.
✓ Branch 1 taken 22992 times.
60270 for (i = out_idx; h->delayed_pic[i]; i++)
1333 37278 h->delayed_pic[i] = h->delayed_pic[i + 1];
1334 }
1335
4/4
✓ Branch 0 taken 23373 times.
✓ Branch 1 taken 117 times.
✓ Branch 2 taken 22875 times.
✓ Branch 3 taken 498 times.
23490 if (!out_of_order && pics > h->avctx->has_b_frames) {
1336 22875 h->next_output_pic = out;
1337
8/8
✓ Branch 0 taken 14638 times.
✓ Branch 1 taken 8237 times.
✓ Branch 2 taken 4386 times.
✓ Branch 3 taken 10252 times.
✓ Branch 4 taken 4320 times.
✓ Branch 5 taken 66 times.
✓ Branch 6 taken 21 times.
✓ Branch 7 taken 4299 times.
22875 if (out_idx == 0 && h->delayed_pic[0] && (h->delayed_pic[0]->f->key_frame || h->delayed_pic[0]->mmco_reset)) {
1338 87 h->next_outputed_poc = INT_MIN;
1339 } else
1340 22788 h->next_outputed_poc = out->poc;
1341
1342
2/2
✓ Branch 0 taken 22781 times.
✓ Branch 1 taken 94 times.
22875 if (out->recovered) {
1343 // We have reached an recovery point and all frames after it in
1344 // display order are "recovered".
1345 22781 h->frame_recovered |= FRAME_RECOVERED_SEI;
1346 }
1347 22875 out->recovered |= !!(h->frame_recovered & FRAME_RECOVERED_SEI);
1348
1349
2/2
✓ Branch 0 taken 81 times.
✓ Branch 1 taken 22794 times.
22875 if (!out->recovered) {
1350
1/2
✓ Branch 0 taken 81 times.
✗ Branch 1 not taken.
81 if (!(h->avctx->flags & AV_CODEC_FLAG_OUTPUT_CORRUPT) &&
1351
1/2
✓ Branch 0 taken 81 times.
✗ Branch 1 not taken.
81 !(h->avctx->flags2 & AV_CODEC_FLAG2_SHOW_ALL)) {
1352 81 h->next_output_pic = NULL;
1353 } else {
1354 out->f->flags |= AV_FRAME_FLAG_CORRUPT;
1355 }
1356 }
1357 } else {
1358
2/2
✓ Branch 0 taken 117 times.
✓ Branch 1 taken 498 times.
615 av_log(h->avctx, AV_LOG_DEBUG, "no picture %s\n", out_of_order ? "ooo" : "");
1359 }
1360
1361 23490 return 0;
1362 }
1363
1364 /* This function is called right after decoding the slice header for a first
1365 * slice in a field (or a frame). It decides whether we are decoding a new frame
1366 * or a second field in a pair and does the necessary setup.
1367 */
1368 26856 static int h264_field_start(H264Context *h, const H264SliceContext *sl,
1369 const H2645NAL *nal, int first_slice)
1370 {
1371 int i;
1372 const SPS *sps;
1373
1374 int last_pic_structure, last_pic_droppable, ret;
1375
1376 26856 ret = h264_init_ps(h, sl, first_slice);
1377
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 26856 times.
26856 if (ret < 0)
1378 return ret;
1379
1380 26856 sps = h->ps.sps;
1381
1382
3/4
✓ Branch 0 taken 26856 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3864 times.
✓ Branch 3 taken 22992 times.
26856 if (sps && sps->bitstream_restriction_flag &&
1383
2/2
✓ Branch 0 taken 27 times.
✓ Branch 1 taken 3837 times.
3864 h->avctx->has_b_frames < sps->num_reorder_frames) {
1384 27 h->avctx->has_b_frames = sps->num_reorder_frames;
1385 }
1386
1387 26856 last_pic_droppable = h->droppable;
1388 26856 last_pic_structure = h->picture_structure;
1389 26856 h->droppable = (nal->ref_idc == 0);
1390 26856 h->picture_structure = sl->picture_structure;
1391
1392 26856 h->poc.frame_num = sl->frame_num;
1393 26856 h->poc.poc_lsb = sl->poc_lsb;
1394 26856 h->poc.delta_poc_bottom = sl->delta_poc_bottom;
1395 26856 h->poc.delta_poc[0] = sl->delta_poc[0];
1396 26856 h->poc.delta_poc[1] = sl->delta_poc[1];
1397
1398
2/2
✓ Branch 0 taken 1037 times.
✓ Branch 1 taken 25819 times.
26856 if (nal->type == H264_NAL_IDR_SLICE)
1399 1037 h->poc_offset = sl->idr_pic_id;
1400
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 25819 times.
25819 else if (h->picture_intra_only)
1401 h->poc_offset = 0;
1402
1403 /* Shorten frame num gaps so we don't have to allocate reference
1404 * frames just to throw them away */
1405
2/2
✓ Branch 0 taken 15303 times.
✓ Branch 1 taken 11553 times.
26856 if (h->poc.frame_num != h->poc.prev_frame_num) {
1406 15303 int unwrap_prev_frame_num = h->poc.prev_frame_num;
1407 15303 int max_frame_num = 1 << sps->log2_max_frame_num;
1408
1409
2/2
✓ Branch 0 taken 173 times.
✓ Branch 1 taken 15130 times.
15303 if (unwrap_prev_frame_num > h->poc.frame_num)
1410 173 unwrap_prev_frame_num -= max_frame_num;
1411
1412
2/2
✓ Branch 0 taken 75 times.
✓ Branch 1 taken 15228 times.
15303 if ((h->poc.frame_num - unwrap_prev_frame_num) > sps->ref_frame_count) {
1413 75 unwrap_prev_frame_num = (h->poc.frame_num - sps->ref_frame_count) - 1;
1414
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 73 times.
75 if (unwrap_prev_frame_num < 0)
1415 2 unwrap_prev_frame_num += max_frame_num;
1416
1417 75 h->poc.prev_frame_num = unwrap_prev_frame_num;
1418 }
1419 }
1420
1421 /* See if we have a decoded first field looking for a pair...
1422 * Here, we're using that to see if we should mark previously
1423 * decode frames as "finished".
1424 * We have to do that before the "dummy" in-between frame allocation,
1425 * since that can modify h->cur_pic_ptr. */
1426
2/2
✓ Branch 0 taken 3361 times.
✓ Branch 1 taken 23495 times.
26856 if (h->first_field) {
1427 3361 int last_field = last_pic_structure == PICT_BOTTOM_FIELD;
1428
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3361 times.
3361 av_assert0(h->cur_pic_ptr);
1429
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3361 times.
3361 av_assert0(h->cur_pic_ptr->f->buf[0]);
1430 assert(h->cur_pic_ptr->reference != DELAYED_PIC_REF);
1431
1432 /* Mark old field/frame as completed */
1433
1/2
✓ Branch 0 taken 3361 times.
✗ Branch 1 not taken.
3361 if (h->cur_pic_ptr->tf.owner[last_field] == h->avctx) {
1434 3361 ff_thread_report_progress(&h->cur_pic_ptr->tf, INT_MAX, last_field);
1435 }
1436
1437 /* figure out if we have a complementary field pair */
1438
2/4
✓ Branch 0 taken 3361 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 3361 times.
3361 if (!FIELD_PICTURE(h) || h->picture_structure == last_pic_structure) {
1439 /* Previous field is unmatched. Don't display it, but let it
1440 * remain for reference if marked as such. */
1441 if (last_pic_structure != PICT_FRAME) {
1442 ff_thread_report_progress(&h->cur_pic_ptr->tf, INT_MAX,
1443 last_pic_structure == PICT_TOP_FIELD);
1444 }
1445 } else {
1446
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3361 times.
3361 if (h->cur_pic_ptr->frame_num != h->poc.frame_num) {
1447 /* This and previous field were reference, but had
1448 * different frame_nums. Consider this field first in
1449 * pair. Throw away previous field except for reference
1450 * purposes. */
1451 if (last_pic_structure != PICT_FRAME) {
1452 ff_thread_report_progress(&h->cur_pic_ptr->tf, INT_MAX,
1453 last_pic_structure == PICT_TOP_FIELD);
1454 }
1455 } else {
1456 /* Second field in complementary pair */
1457
3/4
✓ Branch 0 taken 3353 times.
✓ Branch 1 taken 8 times.
✓ Branch 2 taken 8 times.
✗ Branch 3 not taken.
3361 if (!((last_pic_structure == PICT_TOP_FIELD &&
1458
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3353 times.
3353 h->picture_structure == PICT_BOTTOM_FIELD) ||
1459 8 (last_pic_structure == PICT_BOTTOM_FIELD &&
1460
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
8 h->picture_structure == PICT_TOP_FIELD))) {
1461 av_log(h->avctx, AV_LOG_ERROR,
1462 "Invalid field mode combination %d/%d\n",
1463 last_pic_structure, h->picture_structure);
1464 h->picture_structure = last_pic_structure;
1465 h->droppable = last_pic_droppable;
1466 return AVERROR_INVALIDDATA;
1467
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3361 times.
3361 } else if (last_pic_droppable != h->droppable) {
1468 avpriv_request_sample(h->avctx,
1469 "Found reference and non-reference fields in the same frame, which");
1470 h->picture_structure = last_pic_structure;
1471 h->droppable = last_pic_droppable;
1472 return AVERROR_PATCHWELCOME;
1473 }
1474 }
1475 }
1476 }
1477
1478
3/4
✓ Branch 0 taken 16004 times.
✓ Branch 1 taken 11553 times.
✓ Branch 2 taken 16004 times.
✗ Branch 3 not taken.
54413 while (h->poc.frame_num != h->poc.prev_frame_num && !h->first_field &&
1479
2/2
✓ Branch 0 taken 701 times.
✓ Branch 1 taken 15303 times.
16004 h->poc.frame_num != (h->poc.prev_frame_num + 1) % (1 << sps->log2_max_frame_num)) {
1480
2/2
✓ Branch 0 taken 675 times.
✓ Branch 1 taken 26 times.
701 const H264Picture *prev = h->short_ref_count ? h->short_ref[0] : NULL;
1481 701 av_log(h->avctx, AV_LOG_DEBUG, "Frame num gap %d %d\n",
1482 h->poc.frame_num, h->poc.prev_frame_num);
1483
2/2
✓ Branch 0 taken 124 times.
✓ Branch 1 taken 577 times.
701 if (!sps->gaps_in_frame_num_allowed_flag)
1484
2/2
✓ Branch 0 taken 1984 times.
✓ Branch 1 taken 124 times.
2108 for(i=0; i<FF_ARRAY_ELEMS(h->last_pocs); i++)
1485 1984 h->last_pocs[i] = INT_MIN;
1486 701 ret = h264_frame_start(h);
1487
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 701 times.
701 if (ret < 0) {
1488 h->first_field = 0;
1489 return ret;
1490 }
1491
1492 701 h->poc.prev_frame_num++;
1493 701 h->poc.prev_frame_num %= 1 << sps->log2_max_frame_num;
1494 701 h->cur_pic_ptr->frame_num = h->poc.prev_frame_num;
1495 701 h->cur_pic_ptr->invalid_gap = !sps->gaps_in_frame_num_allowed_flag;
1496 701 ff_thread_report_progress(&h->cur_pic_ptr->tf, INT_MAX, 0);
1497 701 ff_thread_report_progress(&h->cur_pic_ptr->tf, INT_MAX, 1);
1498
1499 701 h->explicit_ref_marking = 0;
1500 701 ret = ff_h264_execute_ref_pic_marking(h);
1501
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 701 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
701 if (ret < 0 && (h->avctx->err_recognition & AV_EF_EXPLODE))
1502 return ret;
1503 /* Error concealment: If a ref is missing, copy the previous ref
1504 * in its place.
1505 * FIXME: Avoiding a memcpy would be nice, but ref handling makes
1506 * many assumptions about there being no actual duplicates.
1507 * FIXME: This does not copy padding for out-of-frame motion
1508 * vectors. Given we are concealing a lost frame, this probably
1509 * is not noticeable by comparison, but it should be fixed. */
1510
1/2
✓ Branch 0 taken 701 times.
✗ Branch 1 not taken.
701 if (h->short_ref_count) {
1511 701 int c[4] = {
1512 701 1<<(h->ps.sps->bit_depth_luma-1),
1513 701 1<<(h->ps.sps->bit_depth_chroma-1),
1514 701 1<<(h->ps.sps->bit_depth_chroma-1),
1515 -1
1516 };
1517
1518
2/2
✓ Branch 0 taken 675 times.
✓ Branch 1 taken 26 times.
701 if (prev &&
1519
1/2
✓ Branch 0 taken 675 times.
✗ Branch 1 not taken.
675 h->short_ref[0]->f->width == prev->f->width &&
1520
1/2
✓ Branch 0 taken 675 times.
✗ Branch 1 not taken.
675 h->short_ref[0]->f->height == prev->f->height &&
1521
1/2
✓ Branch 0 taken 675 times.
✗ Branch 1 not taken.
675 h->short_ref[0]->f->format == prev->f->format) {
1522 675 ff_thread_await_progress(&prev->tf, INT_MAX, 0);
1523
2/2
✓ Branch 0 taken 28 times.
✓ Branch 1 taken 647 times.
675 if (prev->field_picture)
1524 28 ff_thread_await_progress(&prev->tf, INT_MAX, 1);
1525 675 ff_thread_release_ext_buffer(h->avctx, &h->short_ref[0]->tf);
1526 675 h->short_ref[0]->tf.f = h->short_ref[0]->f;
1527 675 ret = ff_thread_ref_frame(&h->short_ref[0]->tf, &prev->tf);
1528
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 675 times.
675 if (ret < 0)
1529 return ret;
1530 675 h->short_ref[0]->poc = prev->poc + 2U;
1531 675 ff_thread_report_progress(&h->short_ref[0]->tf, INT_MAX, 0);
1532
2/2
✓ Branch 0 taken 28 times.
✓ Branch 1 taken 647 times.
675 if (h->short_ref[0]->field_picture)
1533 28 ff_thread_report_progress(&h->short_ref[0]->tf, INT_MAX, 1);
1534
3/4
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 8 times.
✓ Branch 2 taken 18 times.
✗ Branch 3 not taken.
26 } else if (!h->frame_recovered && !h->avctx->hwaccel)
1535 18 ff_color_frame(h->short_ref[0]->f, c);
1536 701 h->short_ref[0]->frame_num = h->poc.prev_frame_num;
1537 }
1538 }
1539
1540 /* See if we have a decoded first field looking for a pair...
1541 * We're using that to see whether to continue decoding in that
1542 * frame, or to allocate a new one. */
1543
2/2
✓ Branch 0 taken 3361 times.
✓ Branch 1 taken 23495 times.
26856 if (h->first_field) {
1544
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3361 times.
3361 av_assert0(h->cur_pic_ptr);
1545
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3361 times.
3361 av_assert0(h->cur_pic_ptr->f->buf[0]);
1546 assert(h->cur_pic_ptr->reference != DELAYED_PIC_REF);
1547
1548 /* figure out if we have a complementary field pair */
1549
2/4
✓ Branch 0 taken 3361 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 3361 times.
3361 if (!FIELD_PICTURE(h) || h->picture_structure == last_pic_structure) {
1550 /* Previous field is unmatched. Don't display it, but let it
1551 * remain for reference if marked as such. */
1552 h->missing_fields ++;
1553 h->cur_pic_ptr = NULL;
1554 h->first_field = FIELD_PICTURE(h);
1555 } else {
1556 3361 h->missing_fields = 0;
1557
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3361 times.
3361 if (h->cur_pic_ptr->frame_num != h->poc.frame_num) {
1558 ff_thread_report_progress(&h->cur_pic_ptr->tf, INT_MAX,
1559 h->picture_structure==PICT_BOTTOM_FIELD);
1560 /* This and the previous field had different frame_nums.
1561 * Consider this field first in pair. Throw away previous
1562 * one except for reference purposes. */
1563 h->first_field = 1;
1564 h->cur_pic_ptr = NULL;
1565
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3361 times.
3361 } else if (h->cur_pic_ptr->reference & DELAYED_PIC_REF) {
1566 /* This frame was already output, we cannot draw into it
1567 * anymore.
1568 */
1569 h->first_field = 1;
1570 h->cur_pic_ptr = NULL;
1571 } else {
1572 /* Second field in complementary pair */
1573 3361 h->first_field = 0;
1574 }
1575 }
1576 } else {
1577 /* Frame or first field in a potentially complementary pair */
1578 23495 h->first_field = FIELD_PICTURE(h);
1579 }
1580
1581
4/4
✓ Branch 0 taken 6727 times.
✓ Branch 1 taken 20129 times.
✓ Branch 2 taken 3366 times.
✓ Branch 3 taken 3361 times.
26856 if (!FIELD_PICTURE(h) || h->first_field) {
1582
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 23495 times.
23495 if (h264_frame_start(h) < 0) {
1583 h->first_field = 0;
1584 return AVERROR_INVALIDDATA;
1585 }
1586 } else {
1587 3361 int field = h->picture_structure == PICT_BOTTOM_FIELD;
1588 3361 release_unused_pictures(h, 0);
1589 3361 h->cur_pic_ptr->tf.owner[field] = h->avctx;
1590 }
1591 /* Some macroblocks can be accessed before they're available in case
1592 * of lost slices, MBAFF or threading. */
1593
2/2
✓ Branch 0 taken 6727 times.
✓ Branch 1 taken 20129 times.
26856 if (FIELD_PICTURE(h)) {
1594
2/2
✓ Branch 0 taken 168207 times.
✓ Branch 1 taken 6727 times.
174934 for(i = (h->picture_structure == PICT_BOTTOM_FIELD); i<h->mb_height; i++)
1595 168207 memset(h->slice_table + i*h->mb_stride, -1, (h->mb_stride - (i+1==h->mb_height)) * sizeof(*h->slice_table));
1596 } else {
1597 20129 memset(h->slice_table, -1,
1598 20129 (h->mb_height * h->mb_stride - 1) * sizeof(*h->slice_table));
1599 }
1600
1601 26856 ret = ff_h264_init_poc(h->cur_pic_ptr->field_poc, &h->cur_pic_ptr->poc,
1602 26856 h->ps.sps, &h->poc, h->picture_structure, nal->ref_idc);
1603
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 26856 times.
26856 if (ret < 0)
1604 return ret;
1605
1606 26856 memcpy(h->mmco, sl->mmco, sl->nb_mmco * sizeof(*h->mmco));
1607 26856 h->nb_mmco = sl->nb_mmco;
1608 26856 h->explicit_ref_marking = sl->explicit_ref_marking;
1609
1610 26856 h->picture_idr = nal->type == H264_NAL_IDR_SLICE;
1611
1612
2/2
✓ Branch 0 taken 66 times.
✓ Branch 1 taken 26790 times.
26856 if (h->sei.recovery_point.recovery_frame_cnt >= 0) {
1613 66 const int sei_recovery_frame_cnt = h->sei.recovery_point.recovery_frame_cnt;
1614
1615
4/4
✓ Branch 0 taken 25 times.
✓ Branch 1 taken 41 times.
✓ Branch 2 taken 11 times.
✓ Branch 3 taken 14 times.
66 if (h->poc.frame_num != sei_recovery_frame_cnt || sl->slice_type_nos != AV_PICTURE_TYPE_I)
1616 52 h->valid_recovery_point = 1;
1617
1618
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 65 times.
66 if ( h->recovery_frame < 0
1619
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 || av_mod_uintp2(h->recovery_frame - h->poc.frame_num, h->ps.sps->log2_max_frame_num) > sei_recovery_frame_cnt) {
1620 65 h->recovery_frame = av_mod_uintp2(h->poc.frame_num + sei_recovery_frame_cnt, h->ps.sps->log2_max_frame_num);
1621
1622
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 59 times.
65 if (!h->valid_recovery_point)
1623 6 h->recovery_frame = h->poc.frame_num;
1624 }
1625 }
1626
1627 26856 h->cur_pic_ptr->f->key_frame |= (nal->type == H264_NAL_IDR_SLICE);
1628
1629
2/2
✓ Branch 0 taken 25819 times.
✓ Branch 1 taken 1037 times.
26856 if (nal->type == H264_NAL_IDR_SLICE ||
1630
4/4
✓ Branch 0 taken 55 times.
✓ Branch 1 taken 25764 times.
✓ Branch 2 taken 53 times.
✓ Branch 3 taken 2 times.
25819 (h->recovery_frame == h->poc.frame_num && nal->ref_idc)) {
1631 1090 h->recovery_frame = -1;
1632 1090 h->cur_pic_ptr->recovered = 1;
1633 }
1634 // If we have an IDR, all frames after it in decoded order are
1635 // "recovered".
1636
2/2
✓ Branch 0 taken 1037 times.
✓ Branch 1 taken 25819 times.
26856 if (nal->type == H264_NAL_IDR_SLICE)
1637 1037 h->frame_recovered |= FRAME_RECOVERED_IDR;
1638 #if 1
1639 26856 h->cur_pic_ptr->recovered |= h->frame_recovered;
1640 #else
1641 h->cur_pic_ptr->recovered |= !!(h->frame_recovered & FRAME_RECOVERED_IDR);
1642 #endif
1643
1644 /* Set the frame properties/side data. Only done for the second field in
1645 * field coded frames, since some SEI information is present for each field
1646 * and is merged by the SEI parsing code. */
1647
5/6
✓ Branch 0 taken 6727 times.
✓ Branch 1 taken 20129 times.
✓ Branch 2 taken 3366 times.
✓ Branch 3 taken 3361 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 3366 times.
26856 if (!FIELD_PICTURE(h) || !h->first_field || h->missing_fields > 1) {
1648 23490 ret = h264_export_frame_props(h);
1649
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 23490 times.
23490 if (ret < 0)
1650 return ret;
1651
1652 23490 ret = h264_select_output_frame(h);
1653
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 23490 times.
23490 if (ret < 0)
1654 return ret;
1655 }
1656
1657 26856 return 0;
1658 }
1659
1660 35997 static int h264_slice_header_parse(const H264Context *h, H264SliceContext *sl,
1661 const H2645NAL *nal)
1662 {
1663 const SPS *sps;
1664 const PPS *pps;
1665 int ret;
1666 unsigned int slice_type, tmp, i;
1667 int field_pic_flag, bottom_field_flag;
1668
4/4
✓ Branch 0 taken 35547 times.
✓ Branch 1 taken 450 times.
✓ Branch 2 taken 27123 times.
✓ Branch 3 taken 8424 times.
35997 int first_slice = sl == h->slice_ctx && !h->current_slice;
1669 int picture_structure;
1670
1671
2/2
✓ Branch 0 taken 27123 times.
✓ Branch 1 taken 8874 times.
35997 if (first_slice)
1672
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 27123 times.
27123 av_assert0(!h->setup_finished);
1673
1674 35997 sl->first_mb_addr = get_ue_golomb_long(&sl->gb);
1675
1676 35997 slice_type = get_ue_golomb_31(&sl->gb);
1677
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 35997 times.
35997 if (slice_type > 9) {
1678 av_log(h->avctx, AV_LOG_ERROR,
1679 "slice type %d too large at %d\n",
1680 slice_type, sl->first_mb_addr);
1681 return AVERROR_INVALIDDATA;
1682 }
1683
2/2
✓ Branch 0 taken 22512 times.
✓ Branch 1 taken 13485 times.
35997 if (slice_type > 4) {
1684 22512 slice_type -= 5;
1685 22512 sl->slice_type_fixed = 1;
1686 } else
1687 13485 sl->slice_type_fixed = 0;
1688
1689 35997 slice_type = ff_h264_golomb_to_pict_type[slice_type];
1690 35997 sl->slice_type = slice_type;
1691 35997 sl->slice_type_nos = slice_type & 3;
1692
1693
2/2
✓ Branch 0 taken 2115 times.
✓ Branch 1 taken 33882 times.
35997 if (nal->type == H264_NAL_IDR_SLICE &&
1694
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2115 times.
2115 sl->slice_type_nos != AV_PICTURE_TYPE_I) {
1695 av_log(h->avctx, AV_LOG_ERROR, "A non-intra slice in an IDR NAL unit.\n");
1696 return AVERROR_INVALIDDATA;
1697 }
1698
1699 35997 sl->pps_id = get_ue_golomb(&sl->gb);
1700
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 35997 times.
35997 if (sl->pps_id >= MAX_PPS_COUNT) {
1701 av_log(h->avctx, AV_LOG_ERROR, "pps_id %u out of range\n", sl->pps_id);
1702 return AVERROR_INVALIDDATA;
1703 }
1704
2/2
✓ Branch 0 taken 389 times.
✓ Branch 1 taken 35608 times.
35997 if (!h->ps.pps_list[sl->pps_id]) {
1705 389 av_log(h->avctx, AV_LOG_ERROR,
1706 "non-existing PPS %u referenced\n",
1707 sl->pps_id);
1708 389 return AVERROR_INVALIDDATA;
1709 }
1710 35608 pps = (const PPS*)h->ps.pps_list[sl->pps_id]->data;
1711 35608 sps = pps->sps;
1712
1713 35608 sl->frame_num = get_bits(&sl->gb, sps->log2_max_frame_num);
1714
2/2
✓ Branch 0 taken 8865 times.
✓ Branch 1 taken 26743 times.
35608 if (!first_slice) {
1715
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8865 times.
8865 if (h->poc.frame_num != sl->frame_num) {
1716 av_log(h->avctx, AV_LOG_ERROR, "Frame num change from %d to %d\n",
1717 h->poc.frame_num, sl->frame_num);
1718 return AVERROR_INVALIDDATA;
1719 }
1720 }
1721
1722 35608 sl->mb_mbaff = 0;
1723
1724
2/2
✓ Branch 0 taken 21121 times.
✓ Branch 1 taken 14487 times.
35608 if (sps->frame_mbs_only_flag) {
1725 21121 picture_structure = PICT_FRAME;
1726 } else {
1727
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 14487 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
14487 if (!sps->direct_8x8_inference_flag && slice_type == AV_PICTURE_TYPE_B) {
1728 av_log(h->avctx, AV_LOG_ERROR, "This stream was generated by a broken encoder, invalid 8x8 inference\n");
1729 return -1;
1730 }
1731 14487 field_pic_flag = get_bits1(&sl->gb);
1732
2/2
✓ Branch 0 taken 9024 times.
✓ Branch 1 taken 5463 times.
14487 if (field_pic_flag) {
1733 9024 bottom_field_flag = get_bits1(&sl->gb);
1734 9024 picture_structure = PICT_TOP_FIELD + bottom_field_flag;
1735 } else {
1736 5463 picture_structure = PICT_FRAME;
1737 }
1738 }
1739 35608 sl->picture_structure = picture_structure;
1740 35608 sl->mb_field_decoding_flag = picture_structure != PICT_FRAME;
1741
1742
2/2
✓ Branch 0 taken 26584 times.
✓ Branch 1 taken 9024 times.
35608 if (picture_structure == PICT_FRAME) {
1743 26584 sl->curr_pic_num = sl->frame_num;
1744 26584 sl->max_pic_num = 1 << sps->log2_max_frame_num;
1745 } else {
1746 9024 sl->curr_pic_num = 2 * sl->frame_num + 1;
1747 9024 sl->max_pic_num = 1 << (sps->log2_max_frame_num + 1);
1748 }
1749
1750
2/2
✓ Branch 0 taken 2110 times.
✓ Branch 1 taken 33498 times.
35608 if (nal->type == H264_NAL_IDR_SLICE) {
1751 2110 unsigned idr_pic_id = get_ue_golomb_long(&sl->gb);
1752
1/2
✓ Branch 0 taken 2110 times.
✗ Branch 1 not taken.
2110 if (idr_pic_id < 65536) {
1753 2110 sl->idr_pic_id = idr_pic_id;
1754 } else
1755 av_log(h->avctx, AV_LOG_WARNING, "idr_pic_id is invalid\n");
1756 }
1757
1758 35608 sl->poc_lsb = 0;
1759 35608 sl->delta_poc_bottom = 0;
1760
2/2
✓ Branch 0 taken 27819 times.
✓ Branch 1 taken 7789 times.
35608 if (sps->poc_type == 0) {
1761 27819 sl->poc_lsb = get_bits(&sl->gb, sps->log2_max_poc_lsb);
1762
1763
4/4
✓ Branch 0 taken 8048 times.
✓ Branch 1 taken 19771 times.
✓ Branch 2 taken 6156 times.
✓ Branch 3 taken 1892 times.
27819 if (pps->pic_order_present == 1 && picture_structure == PICT_FRAME)
1764 6156 sl->delta_poc_bottom = get_se_golomb(&sl->gb);
1765 }
1766
1767 35608 sl->delta_poc[0] = sl->delta_poc[1] = 0;
1768
4/4
✓ Branch 0 taken 3064 times.
✓ Branch 1 taken 32544 times.
✓ Branch 2 taken 2848 times.
✓ Branch 3 taken 216 times.
35608 if (sps->poc_type == 1 && !sps->delta_pic_order_always_zero_flag) {
1769 2848 sl->delta_poc[0] = get_se_golomb(&sl->gb);
1770
1771
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)
1772 453 sl->delta_poc[1] = get_se_golomb(&sl->gb);
1773 }
1774
1775 35608 sl->redundant_pic_count = 0;
1776
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 35608 times.
35608 if (pps->redundant_pic_cnt_present)
1777 sl->redundant_pic_count = get_ue_golomb(&sl->gb);
1778
1779
2/2
✓ Branch 0 taken 12498 times.
✓ Branch 1 taken 23110 times.
35608 if (sl->slice_type_nos == AV_PICTURE_TYPE_B)
1780 12498 sl->direct_spatial_mv_pred = get_bits1(&sl->gb);
1781
1782 35608 ret = ff_h264_parse_ref_count(&sl->list_count, sl->ref_count,
1783 &sl->gb, pps, sl->slice_type_nos,
1784 35608 picture_structure, h->avctx);
1785
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 35608 times.
35608 if (ret < 0)
1786 return ret;
1787
1788
2/2
✓ Branch 0 taken 31290 times.
✓ Branch 1 taken 4318 times.
35608 if (sl->slice_type_nos != AV_PICTURE_TYPE_I) {
1789 31290 ret = ff_h264_decode_ref_pic_list_reordering(sl, h->avctx);
1790
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 31290 times.
31290 if (ret < 0) {
1791 sl->ref_count[1] = sl->ref_count[0] = 0;
1792 return ret;
1793 }
1794 }
1795
1796 35608 sl->pwt.use_weight = 0;
1797
2/2
✓ Branch 0 taken 71216 times.
✓ Branch 1 taken 35608 times.
106824 for (i = 0; i < 2; i++) {
1798 71216 sl->pwt.luma_weight_flag[i] = 0;
1799 71216 sl->pwt.chroma_weight_flag[i] = 0;
1800 }
1801
4/4
✓ Branch 0 taken 2946 times.
✓ Branch 1 taken 32662 times.
✓ Branch 2 taken 1076 times.
✓ Branch 3 taken 1870 times.
35608 if ((pps->weighted_pred && sl->slice_type_nos == AV_PICTURE_TYPE_P) ||
1802
2/2
✓ Branch 0 taken 386 times.
✓ Branch 1 taken 33352 times.
33738 (pps->weighted_bipred_idc == 1 &&
1803
1/2
✓ Branch 0 taken 386 times.
✗ Branch 1 not taken.
386 sl->slice_type_nos == AV_PICTURE_TYPE_B)) {
1804 2256 ret = ff_h264_pred_weight_table(&sl->gb, sps, sl->ref_count,
1805 sl->slice_type_nos, &sl->pwt,
1806 2256