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 | 28988 | 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 1043568 times.
✓ Branch 1 taken 28988 times.
|
1072556 | for (i = 0; i < H264_MAX_PICTURE_COUNT; i++) { |
123 |
6/6✓ Branch 0 taken 145949 times.
✓ Branch 1 taken 897619 times.
✓ Branch 2 taken 25355 times.
✓ Branch 3 taken 120594 times.
✓ Branch 4 taken 2612 times.
✓ Branch 5 taken 22743 times.
|
1043568 | 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 | 23466 | ff_h264_unref_picture(&h->DPB[i]); | |
126 | } | ||
127 | } | ||
128 | 28988 | } | |
129 | |||
130 | 37376 | static int alloc_scratch_buffers(H264SliceContext *sl, int linesize) | |
131 | { | ||
132 | 37376 | const H264Context *h = sl->h264; | |
133 | 37376 | int alloc_size = FFALIGN(FFABS(linesize) + 32, 32); | |
134 | |||
135 | 37376 | 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 | 37376 | av_fast_malloc(&sl->edge_emu_buffer, &sl->edge_emu_buffer_allocated, alloc_size * 2 * 21); | |
139 | |||
140 | 37376 | av_fast_mallocz(&sl->top_borders[0], &sl->top_borders_allocated[0], | |
141 | 37376 | h->mb_width * 16 * 3 * sizeof(uint8_t) * 2); | |
142 | 37376 | av_fast_mallocz(&sl->top_borders[1], &sl->top_borders_allocated[1], | |
143 | 37376 | h->mb_width * 16 * 3 * sizeof(uint8_t) * 2); | |
144 | |||
145 |
2/4✓ Branch 0 taken 37376 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 37376 times.
✗ Branch 3 not taken.
|
37376 | if (!sl->bipred_scratchpad || !sl->edge_emu_buffer || |
146 |
2/4✓ Branch 0 taken 37376 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 37376 times.
|
37376 | !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 | 37376 | 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 | 25621 | static int alloc_picture(H264Context *h, H264Picture *pic) | |
189 | { | ||
190 | 25621 | int i, ret = 0; | |
191 | |||
192 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 25621 times.
|
25621 | av_assert0(!pic->f->data[0]); |
193 | |||
194 | 25621 | pic->tf.f = pic->f; | |
195 | 25621 | ret = ff_thread_get_ext_buffer(h->avctx, &pic->tf, | |
196 | 25621 | pic->reference ? AV_GET_BUFFER_FLAG_REF : 0); | |
197 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 25621 times.
|
25621 | if (ret < 0) |
198 | ✗ | goto fail; | |
199 | |||
200 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 25621 times.
|
25621 | 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 | 25621 | ret = ff_hwaccel_frame_priv_alloc(h->avctx, &pic->hwaccel_picture_private); | |
210 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 25621 times.
|
25621 | if (ret < 0) |
211 | ✗ | goto fail; | |
212 | |||
213 |
2/2✓ Branch 0 taken 42 times.
✓ Branch 1 taken 25579 times.
|
25621 | 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 25018 times.
|
25621 | 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 | 25621 | pic->qscale_table_base = ff_refstruct_pool_get(h->qscale_table_pool); | |
240 | 25621 | pic->mb_type_base = ff_refstruct_pool_get(h->mb_type_pool); | |
241 |
2/4✓ Branch 0 taken 25621 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 25621 times.
|
25621 | if (!pic->qscale_table_base || !pic->mb_type_base) |
242 | ✗ | goto fail; | |
243 | |||
244 | 25621 | pic->mb_type = pic->mb_type_base + 2 * h->mb_stride + 1; | |
245 | 25621 | pic->qscale_table = pic->qscale_table_base + 2 * h->mb_stride + 1; | |
246 | |||
247 |
2/2✓ Branch 0 taken 51242 times.
✓ Branch 1 taken 25621 times.
|
76863 | for (i = 0; i < 2; i++) { |
248 | 51242 | pic->motion_val_base[i] = ff_refstruct_pool_get(h->motion_val_pool); | |
249 | 51242 | pic->ref_index[i] = ff_refstruct_pool_get(h->ref_index_pool); | |
250 |
2/4✓ Branch 0 taken 51242 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 51242 times.
|
51242 | if (!pic->motion_val_base[i] || !pic->ref_index[i]) |
251 | ✗ | goto fail; | |
252 | |||
253 | 51242 | pic->motion_val[i] = pic->motion_val_base[i] + 4; | |
254 | } | ||
255 | |||
256 | 25621 | pic->pps = ff_refstruct_ref_c(h->ps.pps); | |
257 | |||
258 | 25621 | pic->mb_width = h->mb_width; | |
259 | 25621 | pic->mb_height = h->mb_height; | |
260 | 25621 | pic->mb_stride = h->mb_stride; | |
261 | |||
262 | 25621 | return 0; | |
263 | ✗ | fail: | |
264 | ✗ | ff_h264_unref_picture(pic); | |
265 | ✗ | return (ret < 0) ? ret : AVERROR(ENOMEM); | |
266 | } | ||
267 | |||
268 | 25621 | static int find_unused_picture(const H264Context *h) | |
269 | { | ||
270 | int i; | ||
271 | |||
272 |
1/2✓ Branch 0 taken 87009 times.
✗ Branch 1 not taken.
|
87009 | for (i = 0; i < H264_MAX_PICTURE_COUNT; i++) { |
273 |
2/2✓ Branch 0 taken 25621 times.
✓ Branch 1 taken 61388 times.
|
87009 | if (!h->DPB[i].f->buf[0]) |
274 | 25621 | 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 | 25621 | static int h264_frame_start(H264Context *h) | |
476 | { | ||
477 | H264Picture *pic; | ||
478 | int i, ret; | ||
479 | 25621 | const int pixel_shift = h->pixel_shift; | |
480 | |||
481 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 25621 times.
|
25621 | 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 | 25621 | release_unused_pictures(h, 1); | |
487 | 25621 | h->cur_pic_ptr = NULL; | |
488 | |||
489 | 25621 | i = find_unused_picture(h); | |
490 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 25621 times.
|
25621 | if (i < 0) { |
491 | ✗ | av_log(h->avctx, AV_LOG_ERROR, "no frame buffer available\n"); | |
492 | ✗ | return i; | |
493 | } | ||
494 | 25621 | pic = &h->DPB[i]; | |
495 | |||
496 |
2/2✓ Branch 0 taken 17672 times.
✓ Branch 1 taken 7949 times.
|
25621 | pic->reference = h->droppable ? 0 : h->picture_structure; |
497 | 25621 | pic->field_picture = h->picture_structure != PICT_FRAME; | |
498 | 25621 | 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 | 25621 | pic->f->flags &= ~AV_FRAME_FLAG_KEY; | |
505 | 25621 | pic->mmco_reset = 0; | |
506 | 25621 | pic->recovered = 0; | |
507 | 25621 | pic->invalid_gap = 0; | |
508 | 25621 | pic->sei_recovery_frame_cnt = h->sei.recovery_point.recovery_frame_cnt; | |
509 | |||
510 | 25621 | pic->f->pict_type = h->slice_ctx[0].slice_type; | |
511 | |||
512 | 25621 | pic->f->crop_left = h->crop_left; | |
513 | 25621 | pic->f->crop_right = h->crop_right; | |
514 | 25621 | pic->f->crop_top = h->crop_top; | |
515 | 25621 | pic->f->crop_bottom = h->crop_bottom; | |
516 | |||
517 | 25621 | pic->needs_fg = | |
518 | 25621 | h->sei.common.film_grain_characteristics && | |
519 | ✗ | h->sei.common.film_grain_characteristics->present && | |
520 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 25621 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
25621 | !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 25621 times.
|
25621 | if ((ret = alloc_picture(h, pic)) < 0) |
524 | ✗ | return ret; | |
525 | |||
526 | 25621 | h->cur_pic_ptr = pic; | |
527 | 25621 | ff_h264_unref_picture(&h->cur_pic); | |
528 | if (CONFIG_ERROR_RESILIENCE) { | ||
529 | 25621 | ff_h264_set_erpic(&h->er.cur_pic, NULL); | |
530 | } | ||
531 | |||
532 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 25621 times.
|
25621 | if ((ret = ff_h264_ref_picture(&h->cur_pic, h->cur_pic_ptr)) < 0) |
533 | ✗ | return ret; | |
534 | |||
535 |
2/2✓ Branch 0 taken 25921 times.
✓ Branch 1 taken 25621 times.
|
51542 | for (i = 0; i < h->nb_slice_ctx; i++) { |
536 | 25921 | h->slice_ctx[i].linesize = h->cur_pic_ptr->f->linesize[0]; | |
537 | 25921 | h->slice_ctx[i].uvlinesize = h->cur_pic_ptr->f->linesize[1]; | |
538 | } | ||
539 | |||
540 |
2/2✓ Branch 0 taken 25321 times.
✓ Branch 1 taken 300 times.
|
25621 | if (CONFIG_ERROR_RESILIENCE && h->enable_er) { |
541 | 25321 | ff_er_frame_start(&h->er); | |
542 | 25321 | ff_h264_set_erpic(&h->er.last_pic, NULL); | |
543 | 25321 | ff_h264_set_erpic(&h->er.next_pic, NULL); | |
544 | } | ||
545 | |||
546 |
2/2✓ Branch 0 taken 409936 times.
✓ Branch 1 taken 25621 times.
|
435557 | for (i = 0; i < 16; i++) { |
547 | 409936 | h->block_offset[i] = (4 * ((scan8[i] - scan8[0]) & 7) << pixel_shift) + 4 * pic->f->linesize[0] * ((scan8[i] - scan8[0]) >> 3); | |
548 | 409936 | 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 409936 times.
✓ Branch 1 taken 25621 times.
|
435557 | for (i = 0; i < 16; i++) { |
551 | 409936 | h->block_offset[16 + i] = | |
552 | 409936 | h->block_offset[32 + i] = (4 * ((scan8[i] - scan8[0]) & 7) << pixel_shift) + 4 * pic->f->linesize[1] * ((scan8[i] - scan8[0]) >> 3); | |
553 | 409936 | h->block_offset[48 + 16 + i] = | |
554 | 409936 | 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 | 25621 | h->cur_pic_ptr->reference = 0; | |
562 | |||
563 | 25621 | h->cur_pic_ptr->field_poc[0] = h->cur_pic_ptr->field_poc[1] = INT_MAX; | |
564 | |||
565 | 25621 | h->next_output_pic = NULL; | |
566 | |||
567 | 25621 | h->postpone_filter = 0; | |
568 | |||
569 |
4/4✓ Branch 0 taken 2870 times.
✓ Branch 1 taken 22751 times.
✓ Branch 2 taken 2357 times.
✓ Branch 3 taken 513 times.
|
25621 | 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 23559 times.
|
25621 | 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 | 25621 | return 0; | |
577 | } | ||
578 | |||
579 | 12580480 | 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 | 12580480 | int top_idx = 1; | |
587 | 12580480 | const int pixel_shift = h->pixel_shift; | |
588 | 12580480 | int chroma444 = CHROMA444(h); | |
589 | 12580480 | int chroma422 = CHROMA422(h); | |
590 | |||
591 | 12580480 | src_y -= linesize; | |
592 | 12580480 | src_cb -= uvlinesize; | |
593 | 12580480 | src_cr -= uvlinesize; | |
594 | |||
595 |
3/4✓ Branch 0 taken 12580480 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2022480 times.
✓ Branch 3 taken 10558000 times.
|
12580480 | 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 | 11832430 | 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 | 11832430 | AV_COPY128(top_border, src_y + 16 * linesize); | |
642 |
2/2✓ Branch 0 taken 1381381 times.
✓ Branch 1 taken 10451049 times.
|
11832430 | 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 11752102 times.
|
11832430 | 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 11163467 times.
|
11752102 | } 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 10386561 times.
|
11163467 | 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 | 10386561 | AV_COPY64(top_border + 16, src_cb + 8 * uvlinesize); | |
670 | 10386561 | 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 | 3838 | 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 7676 times.
✓ Branch 1 taken 3838 times.
|
11514 | for (i = 0; i < 2; i++) { |
686 | 7676 | sl->pwt.luma_weight_flag[i] = 0; | |
687 | 7676 | sl->pwt.chroma_weight_flag[i] = 0; | |
688 | } | ||
689 | |||
690 |
2/2✓ Branch 0 taken 2128 times.
✓ Branch 1 taken 1710 times.
|
3838 | if (field < 0) { |
691 |
1/2✓ Branch 0 taken 2128 times.
✗ Branch 1 not taken.
|
2128 | if (h->picture_structure == PICT_FRAME) { |
692 | 2128 | 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 953 times.
✓ Branch 1 taken 1175 times.
✓ Branch 2 taken 643 times.
✓ Branch 3 taken 310 times.
✓ Branch 4 taken 633 times.
✓ Branch 5 taken 10 times.
|
2128 | 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 217 times.
|
633 | 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 | 1712 | ref_start = 0; | |
703 | 1712 | ref_count0 = sl->ref_count[0]; | |
704 | 1712 | 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 | 3422 | sl->pwt.use_weight = 2; | |
713 | 3422 | sl->pwt.use_weight_chroma = 2; | |
714 | 3422 | sl->pwt.luma_log2_weight_denom = 5; | |
715 | 3422 | sl->pwt.chroma_log2_weight_denom = 5; | |
716 | |||
717 |
2/2✓ Branch 0 taken 11780 times.
✓ Branch 1 taken 3422 times.
|
15202 | for (ref0 = ref_start; ref0 < ref_count0; ref0++) { |
718 | 11780 | int64_t poc0 = sl->ref_list[0][ref0].poc; | |
719 |
2/2✓ Branch 0 taken 21869 times.
✓ Branch 1 taken 11780 times.
|
33649 | for (ref1 = ref_start; ref1 < ref_count1; ref1++) { |
720 | 21869 | int w = 32; | |
721 |
2/4✓ Branch 0 taken 21869 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 21869 times.
✗ Branch 3 not taken.
|
21869 | if (!sl->ref_list[0][ref0].parent->long_ref && !sl->ref_list[1][ref1].parent->long_ref) { |
722 | 21869 | int poc1 = sl->ref_list[1][ref1].poc; | |
723 | 21869 | int td = av_clip_int8(poc1 - poc0); | |
724 |
2/2✓ Branch 0 taken 21802 times.
✓ Branch 1 taken 67 times.
|
21869 | if (td) { |
725 | 21802 | int tb = av_clip_int8(cur_poc - poc0); | |
726 | 21802 | int tx = (16384 + (FFABS(td) >> 1)) / td; | |
727 | 21802 | int dist_scale_factor = (tb * tx + 32) >> 8; | |
728 |
3/4✓ Branch 0 taken 21792 times.
✓ Branch 1 taken 10 times.
✓ Branch 2 taken 21792 times.
✗ Branch 3 not taken.
|
21802 | if (dist_scale_factor >= -64 && dist_scale_factor <= 128) |
729 | 21792 | w = 64 - dist_scale_factor; | |
730 | } | ||
731 | } | ||
732 |
2/2✓ Branch 0 taken 5989 times.
✓ Branch 1 taken 15880 times.
|
21869 | if (field < 0) { |
733 | 5989 | sl->pwt.implicit_weight[ref0][ref1][0] = | |
734 | 5989 | 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 | 28562 | 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 | 28562 | 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 28035 times.
✗ Branch 5 not taken.
|
28562 | 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 | 28035 | case 8: | |
854 | #if CONFIG_H264_VDPAU_HWACCEL | ||
855 | 28035 | *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 27551 times.
|
28035 | 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 27529 times.
|
27551 | } 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 | 27529 | *fmt++ = AV_PIX_FMT_VAAPI; | |
892 | #endif | ||
893 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 27526 times.
|
27529 | if (h->avctx->color_range == AVCOL_RANGE_JPEG) |
894 | 3 | *fmt++ = AV_PIX_FMT_YUVJ420P; | |
895 | else | ||
896 | 27526 | *fmt++ = AV_PIX_FMT_YUV420P; | |
897 | } | ||
898 | 28035 | 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 | 28562 | *fmt = AV_PIX_FMT_NONE; | |
906 | |||
907 |
2/2✓ Branch 0 taken 84283 times.
✓ Branch 1 taken 590 times.
|
84873 | for (int i = 0; pix_fmts[i] != AV_PIX_FMT_NONE; i++) |
908 |
4/4✓ Branch 0 taken 28228 times.
✓ Branch 1 taken 56055 times.
✓ Branch 2 taken 27972 times.
✓ Branch 3 taken 256 times.
|
84283 | if (pix_fmts[i] == h->avctx->pix_fmt && !force_callback) |
909 | 27972 | 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 | 28303 | static void init_dimensions(H264Context *h) | |
915 | { | ||
916 | 28303 | const SPS *sps = h->ps.sps; | |
917 | 28303 | int cr = sps->crop_right; | |
918 | 28303 | int cl = sps->crop_left; | |
919 | 28303 | int ct = sps->crop_top; | |
920 | 28303 | int cb = sps->crop_bottom; | |
921 | 28303 | int width = h->width - (cr + cl); | |
922 | 28303 | int height = h->height - (ct + cb); | |
923 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 28303 times.
|
28303 | av_assert0(sps->crop_right + sps->crop_left < (unsigned)h->width); |
924 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 28303 times.
|
28303 | av_assert0(sps->crop_top + sps->crop_bottom < (unsigned)h->height); |
925 | |||
926 | /* handle container cropping */ | ||
927 |
3/4✓ Branch 0 taken 26049 times.
✓ Branch 1 taken 2254 times.
✓ Branch 2 taken 26049 times.
✗ Branch 3 not taken.
|
28303 | if (h->width_from_caller > 0 && h->height_from_caller > 0 && |
928 |
3/4✓ Branch 0 taken 26048 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 26048 times.
✗ Branch 3 not taken.
|
26049 | !sps->crop_top && !sps->crop_left && |
929 |
2/2✓ Branch 0 taken 26039 times.
✓ Branch 1 taken 9 times.
|
26048 | FFALIGN(h->width_from_caller, 16) == FFALIGN(width, 16) && |
930 |
1/2✓ Branch 0 taken 26039 times.
✗ Branch 1 not taken.
|
26039 | FFALIGN(h->height_from_caller, 16) == FFALIGN(height, 16) && |
931 |
2/2✓ Branch 0 taken 26038 times.
✓ Branch 1 taken 1 times.
|
26039 | h->width_from_caller <= width && |
932 |
2/2✓ Branch 0 taken 26036 times.
✓ Branch 1 taken 2 times.
|
26038 | h->height_from_caller <= height) { |
933 | 26036 | width = h->width_from_caller; | |
934 | 26036 | height = h->height_from_caller; | |
935 | 26036 | cl = 0; | |
936 | 26036 | ct = 0; | |
937 | 26036 | cr = h->width - width; | |
938 | 26036 | cb = h->height - height; | |
939 | } else { | ||
940 | 2267 | h->width_from_caller = 0; | |
941 | 2267 | h->height_from_caller = 0; | |
942 | } | ||
943 | |||
944 | 28303 | h->avctx->coded_width = h->width; | |
945 | 28303 | h->avctx->coded_height = h->height; | |
946 | 28303 | h->avctx->width = width; | |
947 | 28303 | h->avctx->height = height; | |
948 | 28303 | h->crop_right = cr; | |
949 | 28303 | h->crop_left = cl; | |
950 | 28303 | h->crop_top = ct; | |
951 | 28303 | h->crop_bottom = cb; | |
952 | 28303 | } | |
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 | 55950 | 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 55948 times.
|
55950 | 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 | 55948 | default: | |
1043 | 55948 | return a; | |
1044 | } | ||
1045 | } | ||
1046 | |||
1047 | 28303 | static int h264_init_ps(H264Context *h, const H264SliceContext *sl, int first_slice) | |
1048 | { | ||
1049 | const SPS *sps; | ||
1050 | 28303 | int needs_reinit = 0, must_reinit, ret; | |
1051 | |||
1052 |
2/2✓ Branch 0 taken 28109 times.
✓ Branch 1 taken 194 times.
|
28303 | if (first_slice) |
1053 | 28109 | 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 27714 times.
|
28303 | 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 | 28303 | sps = h->ps.sps; | |
1070 | |||
1071 |
2/2✓ Branch 0 taken 27724 times.
✓ Branch 1 taken 579 times.
|
56027 | must_reinit = (h->context_initialized && |
1072 |
2/2✓ Branch 0 taken 27718 times.
✓ Branch 1 taken 6 times.
|
27724 | ( 16*sps->mb_width != h->avctx->coded_width |
1073 |
1/2✓ Branch 0 taken 27718 times.
✗ Branch 1 not taken.
|
27718 | || 16*sps->mb_height != h->avctx->coded_height |
1074 |
2/2✓ Branch 0 taken 27717 times.
✓ Branch 1 taken 1 times.
|
27718 | || h->cur_bit_depth_luma != sps->bit_depth_luma |
1075 |
2/2✓ Branch 0 taken 27716 times.
✓ Branch 1 taken 1 times.
|
27717 | || h->cur_chroma_format_idc != sps->chroma_format_idc |
1076 |
1/2✓ Branch 0 taken 27716 times.
✗ Branch 1 not taken.
|
27716 | || h->mb_width != sps->mb_width |
1077 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 27716 times.
|
27716 | || h->mb_height != sps->mb_height |
1078 | )); | ||
1079 |
2/2✓ Branch 0 taken 27975 times.
✓ Branch 1 taken 328 times.
|
28303 | if (h->avctx->pix_fmt == AV_PIX_FMT_NONE |
1080 |
2/2✓ Branch 3 taken 3 times.
✓ Branch 4 taken 27972 times.
|
27975 | || (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 28109 times.
✓ Branch 1 taken 194 times.
✓ Branch 3 taken 71 times.
✓ Branch 4 taken 28038 times.
|
28303 | 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 28303 times.
✗ Branch 1 not taken.
|
28303 | if (!h->setup_finished) { |
1087 | 28303 | h->avctx->profile = ff_h264_get_profile(sps); | |
1088 | 28303 | h->avctx->level = sps->level_idc; | |
1089 | 28303 | h->avctx->refs = sps->ref_frame_count; | |
1090 | |||
1091 | 28303 | h->mb_width = sps->mb_width; | |
1092 | 28303 | h->mb_height = sps->mb_height; | |
1093 | 28303 | h->mb_num = h->mb_width * h->mb_height; | |
1094 | 28303 | h->mb_stride = h->mb_width + 1; | |
1095 | |||
1096 | 28303 | h->b_stride = h->mb_width * 4; | |
1097 | |||
1098 | 28303 | h->chroma_y_shift = sps->chroma_format_idc <= 1; // 400 uses yuv420p | |
1099 | |||
1100 | 28303 | h->width = 16 * h->mb_width; | |
1101 | 28303 | h->height = 16 * h->mb_height; | |
1102 | |||
1103 | 28303 | init_dimensions(h); | |
1104 | |||
1105 |
2/2✓ Branch 0 taken 1287 times.
✓ Branch 1 taken 27016 times.
|
28303 | 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 28303 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
28303 | 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 | 28303 | h->avctx->chroma_sample_location = sps->vui.chroma_location; | |
1124 | |||
1125 |
5/6✓ Branch 0 taken 27724 times.
✓ Branch 1 taken 579 times.
✓ Branch 2 taken 27716 times.
✓ Branch 3 taken 8 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 27716 times.
|
28303 | 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 | 28303 | return 0; | |
1158 | } | ||
1159 | |||
1160 | 24931 | static int h264_export_frame_props(H264Context *h) | |
1161 | { | ||
1162 | 24931 | const SPS *sps = h->ps.sps; | |
1163 | 24931 | H264Picture *cur = h->cur_pic_ptr; | |
1164 | 24931 | AVFrame *out = cur->f; | |
1165 | 24931 | int interlaced_frame = 0, top_field_first = 0; | |
1166 | int ret; | ||
1167 | |||
1168 | 24931 | out->flags &= ~AV_FRAME_FLAG_INTERLACED; | |
1169 | 24931 | 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 23505 times.
|
24931 | 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 23427 times.
✓ Branch 2 taken 1418 times.
✓ Branch 3 taken 86 times.
|
26349 | 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 21922 times.
✓ Branch 1 taken 1591 times.
✓ Branch 2 taken 2826 times.
✓ Branch 3 taken 19096 times.
|
23513 | interlaced_frame = !!FIELD_OR_MBAFF_PICTURE(h); |
1223 | } | ||
1224 | 24931 | h->prev_interlaced_frame = interlaced_frame; | |
1225 | |||
1226 |
2/2✓ Branch 0 taken 3341 times.
✓ Branch 1 taken 21590 times.
|
24931 | 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 21365 times.
✓ Branch 2 taken 139 times.
✓ Branch 3 taken 86 times.
|
21590 | 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 18893 times.
|
21451 | } 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 | 24931 | out->flags |= (AV_FRAME_FLAG_INTERLACED * interlaced_frame) | | |
1245 | 24931 | (AV_FRAME_FLAG_TOP_FIELD_FIRST * top_field_first); | |
1246 | |||
1247 | 24931 | ret = ff_h2645_sei_to_frame(out, &h->sei.common, AV_CODEC_ID_H264, h->avctx, | |
1248 | 24931 | &sps->vui, sps->bit_depth_luma, sps->bit_depth_chroma, | |
1249 | 24931 | cur->poc + (unsigned)(h->poc_offset << 5)); | |
1250 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 24931 times.
|
24931 | if (ret < 0) |
1251 | ✗ | return ret; | |
1252 | |||
1253 |
2/2✓ Branch 0 taken 652 times.
✓ Branch 1 taken 24279 times.
|
24931 | 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 | 24931 | return 0; | |
1282 | } | ||
1283 | |||
1284 | 24931 | static int h264_select_output_frame(H264Context *h) | |
1285 | { | ||
1286 | 24931 | const SPS *sps = h->ps.sps; | |
1287 | 24931 | H264Picture *out = h->cur_pic_ptr; | |
1288 | 24931 | H264Picture *cur = h->cur_pic_ptr; | |
1289 | int i, pics, out_of_order, out_idx; | ||
1290 | |||
1291 | 24931 | cur->mmco_reset = h->mmco_reset; | |
1292 | 24931 | h->mmco_reset = 0; | |
1293 | |||
1294 |
2/2✓ Branch 0 taken 20028 times.
✓ Branch 1 taken 4903 times.
|
24931 | 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 | 4903 | h->avctx->has_b_frames = FFMAX(h->avctx->has_b_frames, sps->num_reorder_frames); | |
1297 | } | ||
1298 | |||
1299 | 408825 | for (i = 0; 1; i++) { | |
1300 |
4/4✓ Branch 0 taken 392905 times.
✓ Branch 1 taken 15920 times.
✓ Branch 2 taken 9011 times.
✓ Branch 3 taken 383894 times.
|
408825 | if(i == H264_MAX_DPB_FRAMES || cur->poc < h->last_pocs[i]){ |
1301 |
1/2✓ Branch 0 taken 24931 times.
✗ Branch 1 not taken.
|
24931 | if(i) |
1302 | 24931 | h->last_pocs[i-1] = cur->poc; | |
1303 | 24931 | break; | |
1304 |
2/2✓ Branch 0 taken 358963 times.
✓ Branch 1 taken 24931 times.
|
383894 | } else if(i) { |
1305 | 358963 | h->last_pocs[i-1]= h->last_pocs[i]; | |
1306 | } | ||
1307 | } | ||
1308 | 24931 | out_of_order = H264_MAX_DPB_FRAMES - i; | |
1309 |
2/2✓ Branch 0 taken 16804 times.
✓ Branch 1 taken 8127 times.
|
24931 | if( cur->f->pict_type == AV_PICTURE_TYPE_B |
1310 |
4/4✓ Branch 0 taken 15681 times.
✓ Branch 1 taken 1123 times.
✓ Branch 2 taken 5066 times.
✓ Branch 3 taken 10615 times.
|
16804 | || (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 | 13193 | out_of_order = FFMAX(out_of_order, 1); | |
1312 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 24931 times.
|
24931 | 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 24790 times.
✓ Branch 2 taken 141 times.
✗ Branch 3 not taken.
|
24931 | } 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 | 24931 | pics = 0; | |
1325 |
2/2✓ Branch 0 taken 30544 times.
✓ Branch 1 taken 24931 times.
|
55475 | while (h->delayed_pic[pics]) |
1326 | 30544 | pics++; | |
1327 | |||
1328 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 24931 times.
|
24931 | av_assert0(pics <= H264_MAX_DPB_FRAMES); |
1329 | |||
1330 | 24931 | h->delayed_pic[pics++] = cur; | |
1331 |
2/2✓ Branch 0 taken 23453 times.
✓ Branch 1 taken 1478 times.
|
24931 | if (cur->reference == 0) |
1332 | 23453 | cur->reference = DELAYED_PIC_REF; | |
1333 | |||
1334 | 24931 | out = h->delayed_pic[0]; | |
1335 | 24931 | out_idx = 0; | |
1336 | 24931 | for (i = 1; h->delayed_pic[i] && | |
1337 |
4/4✓ Branch 0 taken 28093 times.
✓ Branch 1 taken 24464 times.
✓ Branch 2 taken 27713 times.
✓ Branch 3 taken 380 times.
|
52557 | !(h->delayed_pic[i]->f->flags & AV_FRAME_FLAG_KEY) && |
1338 |
2/2✓ Branch 0 taken 27626 times.
✓ Branch 1 taken 87 times.
|
27713 | !h->delayed_pic[i]->mmco_reset; |
1339 | 27626 | i++) | |
1340 |
2/2✓ Branch 0 taken 10311 times.
✓ Branch 1 taken 17315 times.
|
27626 | if (h->delayed_pic[i]->poc < out->poc) { |
1341 | 10311 | out = h->delayed_pic[i]; | |
1342 | 10311 | out_idx = i; | |
1343 | } | ||
1344 |
2/2✓ Branch 0 taken 10259 times.
✓ Branch 1 taken 14672 times.
|
24931 | 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 | 24931 | out_of_order = out->poc < h->next_outputed_poc; | |
1348 | |||
1349 |
4/4✓ Branch 0 taken 24818 times.
✓ Branch 1 taken 113 times.
✓ Branch 2 taken 24291 times.
✓ Branch 3 taken 527 times.
|
24931 | if (out_of_order || pics > h->avctx->has_b_frames) { |
1350 | 24404 | out->reference &= ~DELAYED_PIC_REF; | |
1351 |
2/2✓ Branch 0 taken 39550 times.
✓ Branch 1 taken 24404 times.
|
63954 | for (i = out_idx; h->delayed_pic[i]; i++) |
1352 | 39550 | h->delayed_pic[i] = h->delayed_pic[i + 1]; | |
1353 | } | ||
1354 |
4/4✓ Branch 0 taken 24818 times.
✓ Branch 1 taken 113 times.
✓ Branch 2 taken 24291 times.
✓ Branch 3 taken 527 times.
|
24931 | if (!out_of_order && pics > h->avctx->has_b_frames) { |
1355 | 24291 | h->next_output_pic = out; | |
1356 |
8/8✓ Branch 0 taken 15500 times.
✓ Branch 1 taken 8791 times.
✓ Branch 2 taken 5242 times.
✓ Branch 3 taken 10258 times.
✓ Branch 4 taken 5158 times.
✓ Branch 5 taken 84 times.
✓ Branch 6 taken 21 times.
✓ Branch 7 taken 5137 times.
|
24291 | 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 | 24186 | 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 | 24291 | h->frame_recovered |= out->recovered; | |
1364 | |||
1365 | 24291 | out->recovered |= h->frame_recovered & FRAME_RECOVERED_SEI; | |
1366 | |||
1367 |
2/2✓ Branch 0 taken 249 times.
✓ Branch 1 taken 24042 times.
|
24291 | 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 | 24931 | 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 | 28303 | 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 | 28303 | ret = h264_init_ps(h, sl, first_slice); | |
1395 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 28303 times.
|
28303 | if (ret < 0) |
1396 | ✗ | return ret; | |
1397 | |||
1398 | 28303 | sps = h->ps.sps; | |
1399 | |||
1400 |
2/2✓ Branch 0 taken 5236 times.
✓ Branch 1 taken 23067 times.
|
28303 | if (sps->bitstream_restriction_flag && |
1401 |
2/2✓ Branch 0 taken 29 times.
✓ Branch 1 taken 5207 times.
|
5236 | h->avctx->has_b_frames < sps->num_reorder_frames) { |
1402 | 29 | h->avctx->has_b_frames = sps->num_reorder_frames; | |
1403 | } | ||
1404 | |||
1405 | 28303 | last_pic_droppable = h->droppable; | |
1406 | 28303 | last_pic_structure = h->picture_structure; | |
1407 | 28303 | h->droppable = (nal->ref_idc == 0); | |
1408 | 28303 | h->picture_structure = sl->picture_structure; | |
1409 | |||
1410 | 28303 | h->poc.frame_num = sl->frame_num; | |
1411 | 28303 | h->poc.poc_lsb = sl->poc_lsb; | |
1412 | 28303 | h->poc.delta_poc_bottom = sl->delta_poc_bottom; | |
1413 | 28303 | h->poc.delta_poc[0] = sl->delta_poc[0]; | |
1414 | 28303 | h->poc.delta_poc[1] = sl->delta_poc[1]; | |
1415 | |||
1416 |
2/2✓ Branch 0 taken 1079 times.
✓ Branch 1 taken 27224 times.
|
28303 | 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 27224 times.
|
27224 | 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 12155 times.
|
28303 | 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 24936 times.
|
28303 | 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 12155 times.
✓ Branch 2 taken 16833 times.
✗ Branch 3 not taken.
|
57291 | 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 24936 times.
|
28303 | 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 | 24936 | h->first_field = FIELD_PICTURE(h); | |
1601 | } | ||
1602 | |||
1603 |
4/4✓ Branch 0 taken 6739 times.
✓ Branch 1 taken 21564 times.
✓ Branch 2 taken 3372 times.
✓ Branch 3 taken 3367 times.
|
28303 | if (!FIELD_PICTURE(h) || h->first_field) { |
1604 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 24936 times.
|
24936 | 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 21564 times.
|
28303 | 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 | 21564 | memset(h->slice_table, -1, | |
1620 | 21564 | (h->mb_height * h->mb_stride - 1) * sizeof(*h->slice_table)); | |
1621 | } | ||
1622 | |||
1623 | 28303 | ret = ff_h264_init_poc(h->cur_pic_ptr->field_poc, &h->cur_pic_ptr->poc, | |
1624 | 28303 | h->ps.sps, &h->poc, h->picture_structure, nal->ref_idc); | |
1625 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 28303 times.
|
28303 | if (ret < 0) |
1626 | ✗ | return ret; | |
1627 | |||
1628 | 28303 | memcpy(h->mmco, sl->mmco, sl->nb_mmco * sizeof(*h->mmco)); | |
1629 | 28303 | h->nb_mmco = sl->nb_mmco; | |
1630 | 28303 | h->explicit_ref_marking = sl->explicit_ref_marking; | |
1631 | |||
1632 | 28303 | h->picture_idr = nal->type == H264_NAL_IDR_SLICE; | |
1633 | |||
1634 |
2/2✓ Branch 0 taken 90 times.
✓ Branch 1 taken 28213 times.
|
28303 | 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 27224 times.
|
28303 | 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 27224 times.
|
28303 | 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 28204 times.
✓ Branch 2 taken 85 times.
✓ Branch 3 taken 14 times.
|
28303 | 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 | 28303 | 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 21564 times.
✓ Branch 2 taken 3372 times.
✓ Branch 3 taken 3367 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 3372 times.
|
28303 | if (!FIELD_PICTURE(h) || !h->first_field || h->missing_fields > 1) { |
1673 | 24931 | ret = h264_export_frame_props(h); | |
1674 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 24931 times.
|
24931 | if (ret < 0) |
1675 | ✗ | return ret; | |
1676 | |||
1677 | 24931 | ret = h264_select_output_frame(h); | |
1678 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 24931 times.
|
24931 | if (ret < 0) |
1679 | ✗ | return ret; | |
1680 | } | ||
1681 | |||
1682 | 28303 | return 0; | |
1683 | } | ||
1684 | |||
1685 | 37623 | 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 37173 times.
✓ Branch 1 taken 450 times.
✓ Branch 2 taken 28352 times.
✓ Branch 3 taken 8821 times.
|
37623 | int first_slice = sl == h->slice_ctx && !h->current_slice; |
1694 | int picture_structure; | ||
1695 | |||
1696 |
2/2✓ Branch 0 taken 28352 times.
✓ Branch 1 taken 9271 times.
|
37623 | if (first_slice) |
1697 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 28352 times.
|
28352 | av_assert0(!h->setup_finished); |
1698 | |||
1699 | 37623 | sl->first_mb_addr = get_ue_golomb_long(&sl->gb); | |
1700 | |||
1701 | 37623 | slice_type = get_ue_golomb_31(&sl->gb); | |
1702 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 37623 times.
|
37623 | 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 23899 times.
✓ Branch 1 taken 13724 times.
|
37623 | if (slice_type > 4) { |
1709 | 23899 | slice_type -= 5; | |
1710 | 23899 | sl->slice_type_fixed = 1; | |
1711 | } else | ||
1712 | 13724 | sl->slice_type_fixed = 0; | |
1713 | |||
1714 | 37623 | slice_type = ff_h264_golomb_to_pict_type[slice_type]; | |
1715 | 37623 | sl->slice_type = slice_type; | |
1716 | 37623 | sl->slice_type_nos = slice_type & 3; | |
1717 | |||
1718 |
2/2✓ Branch 0 taken 2265 times.
✓ Branch 1 taken 35358 times.
|
37623 | 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 | 37623 | sl->pps_id = get_ue_golomb(&sl->gb); | |
1725 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 37623 times.
|
37623 | 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 37456 times.
|
37623 | 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 | 37456 | pps = h->ps.pps_list[sl->pps_id]; | |
1736 | 37456 | sps = pps->sps; | |
1737 | |||
1738 | 37456 | sl->frame_num = get_bits(&sl->gb, sps->log2_max_frame_num); | |
1739 |
2/2✓ Branch 0 taken 9267 times.
✓ Branch 1 taken 28189 times.
|
37456 | 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 | 37456 | sl->mb_mbaff = 0; | |
1748 | |||
1749 |
2/2✓ Branch 0 taken 22948 times.
✓ Branch 1 taken 14508 times.
|
37456 | if (sps->frame_mbs_only_flag) { |
1750 | 22948 | 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 | 37456 | sl->picture_structure = picture_structure; | |
1765 | 37456 | sl->mb_field_decoding_flag = picture_structure != PICT_FRAME; | |
1766 | |||
1767 |
2/2✓ Branch 0 taken 28420 times.
✓ Branch 1 taken 9036 times.
|
37456 | if (picture_structure == PICT_FRAME) { |
1768 | 28420 | sl->curr_pic_num = sl->frame_num; | |
1769 | 28420 | 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 35196 times.
|
37456 | 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 | 37456 | sl->poc_lsb = 0; | |
1784 | 37456 | sl->delta_poc_bottom = 0; | |
1785 |
2/2✓ Branch 0 taken 29676 times.
✓ Branch 1 taken 7780 times.
|
37456 | if (sps->poc_type == 0) { |
1786 | 29676 | sl->poc_lsb = get_bits(&sl->gb, sps->log2_max_poc_lsb); | |
1787 | |||
1788 |
4/4✓ Branch 0 taken 8523 times.
✓ Branch 1 taken 21153 times.
✓ Branch 2 taken 6631 times.
✓ Branch 3 taken 1892 times.
|
29676 | if (pps->pic_order_present == 1 && picture_structure == PICT_FRAME) |
1789 | 6631 | sl->delta_poc_bottom = get_se_golomb(&sl->gb); | |
1790 | } | ||
1791 | |||
1792 | 37456 | sl->delta_poc[0] = sl->delta_poc[1] = 0; | |
1793 |
4/4✓ Branch 0 taken 3055 times.
✓ Branch 1 taken 34401 times.
✓ Branch 2 taken 2848 times.
✓ Branch 3 taken 207 times.
|
37456 | 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 | 37456 | sl->redundant_pic_count = 0; | |
1801 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 37456 times.
|
37456 | if (pps->redundant_pic_cnt_present) |
1802 | ✗ | sl->redundant_pic_count = get_ue_golomb(&sl->gb); | |
1803 | |||
1804 |
2/2✓ Branch 0 taken 13155 times.
✓ Branch 1 taken 24301 times.
|
37456 | if (sl->slice_type_nos == AV_PICTURE_TYPE_B) |
1805 | 13155 | sl->direct_spatial_mv_pred = get_bits1(&sl->gb); | |
1806 | |||
1807 | 37456 | ret = ff_h264_parse_ref_count(&sl->list_count, sl->ref_count, | |
1808 | &sl->gb, pps, sl->slice_type_nos, | ||
1809 | 37456 | picture_structure, h->avctx); | |
1810 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 37456 times.
|
37456 | if (ret < 0) |
1811 | ✗ | return ret; | |
1812 | |||
1813 |
2/2✓ Branch 0 taken 32988 times.
✓ Branch 1 taken 4468 times.
|
37456 | if (sl->slice_type_nos != AV_PICTURE_TYPE_I) { |
1814 | 32988 | ret = ff_h264_decode_ref_pic_list_reordering(sl, h->avctx); | |
1815 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 32988 times.
|
32988 | if (ret < 0) { |
1816 | ✗ | sl->ref_count[1] = sl->ref_count[0] = 0; | |
1817 | ✗ | return ret; | |
1818 | } | ||
1819 | } | ||
1820 | |||
1821 | 37456 | sl->pwt.use_weight = 0; | |
1822 |
2/2✓ Branch 0 taken 74912 times.
✓ Branch 1 taken 37456 times.
|
112368 | for (i = 0; i < 2; i++) { |
1823 | 74912 | sl->pwt.luma_weight_flag[i] = 0; | |
1824 | 74912 | sl->pwt.chroma_weight_flag[i] = 0; | |
1825 | } | ||
1826 |
4/4✓ Branch 0 taken 4311 times.
✓ Branch 1 taken 33145 times.
✓ Branch 2 taken 1631 times.
✓ Branch 3 taken 2680 times.
|
37456 | if ((pps->weighted_pred && sl->slice_type_nos == AV_PICTURE_TYPE_P) || |
1827 |
2/2✓ Branch 0 taken 386 times.
✓ Branch 1 taken 34390 times.
|
34776 | (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 | 3066 | ret = ff_h264_pred_weight_table(&sl->gb, sps, sl->ref_count, | |
1830 | sl->slice_type_nos, &sl->pwt, | ||
1831 | 3066 | picture_structure, h->avctx); | |
1832 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3066 times.
|
3066 | if (ret < 0) |
1833 | ✗ | return ret; | |
1834 | } | ||
1835 | |||
1836 | 37456 | sl->explicit_ref_marking = 0; | |
1837 |
2/2✓ Branch 0 taken 24961 times.
✓ Branch 1 taken 12495 times.
|
37456 | if (nal->ref_idc) { |
1838 | 24961 | ret = ff_h264_decode_ref_pic_marking(sl, &sl->gb, nal, h->avctx); | |
1839 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 24961 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
24961 | if (ret < 0 && (h->avctx->err_recognition & AV_EF_EXPLODE)) |
1840 | ✗ | return AVERROR_INVALIDDATA; | |
1841 | } | ||
1842 | |||
1843 |
4/4✓ Branch 0 taken 32988 times.
✓ Branch 1 taken 4468 times.
✓ Branch 2 taken 18176 times.
✓ Branch 3 taken 14812 times.
|
37456 | if (sl->slice_type_nos != AV_PICTURE_TYPE_I && pps->cabac) { |
1844 | 18176 | tmp = get_ue_golomb_31(&sl->gb); | |
1845 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 18176 times.
|
18176 | if (tmp > 2) { |
1846 | ✗ | av_log(h->avctx, AV_LOG_ERROR, "cabac_init_idc %u overflow\n", tmp); | |
1847 | ✗ | return AVERROR_INVALIDDATA; | |
1848 | } | ||
1849 | 18176 | sl->cabac_init_idc = tmp; | |
1850 | } | ||
1851 | |||
1852 | 37456 | sl->last_qscale_diff = 0; | |
1853 | 37456 | tmp = pps->init_qp + (unsigned)get_se_golomb(&sl->gb); | |
1854 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 37456 times.
|
37456 | 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 | 37456 | sl->qscale = tmp; | |
1859 | 37456 | sl->chroma_qp[0] = get_chroma_qp(pps, 0, sl->qscale); | |
1860 | 37456 | 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 37456 times.
|
37456 | if (sl->slice_type == AV_PICTURE_TYPE_SP) |
1863 | ✗ | get_bits1(&sl->gb); /* sp_for_switch_flag */ | |
1864 |
1/2✓ Branch 0 taken 37456 times.
✗ Branch 1 not taken.
|
37456 | if (sl->slice_type == AV_PICTURE_TYPE_SP || |
1865 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 37456 times.
|
37456 | sl->slice_type == AV_PICTURE_TYPE_SI) |
1866 | ✗ | get_se_golomb(&sl->gb); /* slice_qs_delta */ | |
1867 | |||
1868 | 37456 | sl->deblocking_filter = 1; | |
1869 | 37456 | sl->slice_alpha_c0_offset = 0; | |
1870 | 37456 | sl->slice_beta_offset = 0; | |
1871 |
2/2✓ Branch 0 taken 22659 times.
✓ Branch 1 taken 14797 times.
|
37456 | if (pps->deblocking_filter_parameters_present) { |
1872 | 22659 | tmp = get_ue_golomb_31(&sl->gb); | |
1873 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 22659 times.
|
22659 | 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 | 22659 | sl->deblocking_filter = tmp; | |
1879 |
2/2✓ Branch 0 taken 20158 times.
✓ Branch 1 taken 2501 times.
|
22659 | if (sl->deblocking_filter < 2) |
1880 | 20158 | sl->deblocking_filter ^= 1; // 1<->0 | |
1881 | |||
1882 |
2/2✓ Branch 0 taken 14948 times.
✓ Branch 1 taken 7711 times.
|
22659 | if (sl->deblocking_filter) { |
1883 | 14948 | int slice_alpha_c0_offset_div2 = get_se_golomb(&sl->gb); | |
1884 | 14948 | int slice_beta_offset_div2 = get_se_golomb(&sl->gb); | |
1885 |
2/4✓ Branch 0 taken 14948 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 14948 times.
✗ Branch 3 not taken.
|
14948 | if (slice_alpha_c0_offset_div2 > 6 || |
1886 |
1/2✓ Branch 0 taken 14948 times.
✗ Branch 1 not taken.
|
14948 | slice_alpha_c0_offset_div2 < -6 || |
1887 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 14948 times.
|
14948 | slice_beta_offset_div2 > 6 || |
1888 | slice_beta_offset_div2 < -6) { | ||
1889 | ✗ | av_log(h->avctx, AV_LOG_ERROR, | |
1890 | "deblocking filter parameters %d %d out of range\n", | ||
1891 | slice_alpha_c0_offset_div2, slice_beta_offset_div2); | ||
1892 | ✗ | return AVERROR_INVALIDDATA; | |
1893 | } | ||
1894 | 14948 | sl->slice_alpha_c0_offset = slice_alpha_c0_offset_div2 * 2; | |
1895 | 14948 | sl->slice_beta_offset = slice_beta_offset_div2 * 2; | |
1896 | } | ||
1897 | } | ||
1898 | |||
1899 | 37456 | return 0; | |
1900 | } | ||
1901 | |||
1902 | /* do all the per-slice initialization needed before we can start decoding the | ||
1903 | * actual MBs */ | ||
1904 | 37376 | static int h264_slice_init(H264Context *h, H264SliceContext *sl, | |
1905 | const H2645NAL *nal) | ||
1906 | { | ||
1907 | 37376 | int i, j, ret = 0; | |
1908 | |||
1909 |
3/4✓ Branch 0 taken 2260 times.
✓ Branch 1 taken 35116 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 2260 times.
|
37376 | if (h->picture_idr && nal->type != H264_NAL_IDR_SLICE) { |
1910 | ✗ | av_log(h->avctx, AV_LOG_ERROR, "Invalid mix of IDR and non-IDR slices\n"); | |
1911 | ✗ | return AVERROR_INVALIDDATA; | |
1912 | } | ||
1913 | |||
1914 | av_assert1(h->mb_num == h->mb_width * h->mb_height); | ||
1915 |
5/6✓ Branch 0 taken 32401 times.
✓ Branch 1 taken 4975 times.
✓ Branch 2 taken 8956 times.
✓ Branch 3 taken 23445 times.
✓ Branch 4 taken 37376 times.
✗ Branch 5 not taken.
|
37376 | if (sl->first_mb_addr << FIELD_OR_MBAFF_PICTURE(h) >= h->mb_num || |
1916 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 37376 times.
|
37376 | sl->first_mb_addr >= h->mb_num) { |
1917 | ✗ | av_log(h->avctx, AV_LOG_ERROR, "first_mb_in_slice overflow\n"); | |
1918 | ✗ | return AVERROR_INVALIDDATA; | |
1919 | } | ||
1920 | 37376 | sl->resync_mb_x = sl->mb_x = sl->first_mb_addr % h->mb_width; | |
1921 | 74752 | sl->resync_mb_y = sl->mb_y = (sl->first_mb_addr / h->mb_width) << | |
1922 |
4/4✓ Branch 0 taken 32401 times.
✓ Branch 1 taken 4975 times.
✓ Branch 2 taken 8956 times.
✓ Branch 3 taken 23445 times.
|
37376 | FIELD_OR_MBAFF_PICTURE(h); |
1923 |
2/2✓ Branch 0 taken 4475 times.
✓ Branch 1 taken 32901 times.
|
37376 | if (h->picture_structure == PICT_BOTTOM_FIELD) |
1924 | 4475 | sl->resync_mb_y = sl->mb_y = sl->mb_y + 1; | |
1925 | av_assert1(sl->mb_y < h->mb_height); | ||
1926 | |||
1927 | 37376 | ret = ff_h264_build_ref_list(h, sl); | |
1928 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 37376 times.
|
37376 | if (ret < 0) |
1929 | ✗ | return ret; | |
1930 | |||
1931 |
2/2✓ Branch 0 taken 3813 times.
✓ Branch 1 taken 33563 times.
|
37376 | if (h->ps.pps->weighted_bipred_idc == 2 && |
1932 |
2/2✓ Branch 0 taken 2128 times.
✓ Branch 1 taken 1685 times.
|
3813 | sl->slice_type_nos == AV_PICTURE_TYPE_B) { |
1933 | 2128 | implicit_weight_table(h, sl, -1); | |
1934 |
2/2✓ Branch 0 taken 855 times.
✓ Branch 1 taken 1273 times.
|
2128 | if (FRAME_MBAFF(h)) { |
1935 | 855 | implicit_weight_table(h, sl, 0); | |
1936 | 855 | implicit_weight_table(h, sl, 1); | |
1937 | } | ||
1938 | } | ||
1939 | |||
1940 |
4/4✓ Branch 0 taken 13111 times.
✓ Branch 1 taken 24265 times.
✓ Branch 2 taken 7709 times.
✓ Branch 3 taken 5402 times.
|
37376 | if (sl->slice_type_nos == AV_PICTURE_TYPE_B && !sl->direct_spatial_mv_pred) |
1941 | 7709 | ff_h264_direct_dist_scale_factor(h, sl); | |
1942 |
1/2✓ Branch 0 taken 37376 times.
✗ Branch 1 not taken.
|
37376 | if (!h->setup_finished) |
1943 | 37376 | ff_h264_direct_ref_list_init(h, sl); | |
1944 | |||
1945 |
1/2✓ Branch 0 taken 37376 times.
✗ Branch 1 not taken.
|
37376 | if (h->avctx->skip_loop_filter >= AVDISCARD_ALL || |
1946 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 37376 times.
|
37376 | (h->avctx->skip_loop_filter >= AVDISCARD_NONKEY && |
1947 | ✗ | h->nal_unit_type != H264_NAL_IDR_SLICE) || | |
1948 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 37376 times.
|
37376 | (h->avctx->skip_loop_filter >= AVDISCARD_NONINTRA && |
1949 | ✗ | sl->slice_type_nos != AV_PICTURE_TYPE_I) || | |
1950 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 37376 times.
|
37376 | (h->avctx->skip_loop_filter >= AVDISCARD_BIDIR && |
1951 | ✗ | sl->slice_type_nos == AV_PICTURE_TYPE_B) || | |
1952 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 37376 times.
|
37376 | (h->avctx->skip_loop_filter >= AVDISCARD_NONREF && |
1953 | ✗ | nal->ref_idc == 0)) | |
1954 | ✗ | sl->deblocking_filter = 0; | |
1955 | |||
1956 |
3/4✓ Branch 0 taken 27164 times.
✓ Branch 1 taken 10212 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 27164 times.
|
37376 | if (sl->deblocking_filter == 1 && h->nb_slice_ctx > 1) { |
1957 | ✗ | if (h->avctx->flags2 & AV_CODEC_FLAG2_FAST) { | |
1958 | /* Cheat slightly for speed: | ||
1959 | * Do not bother to deblock across slices. */ | ||
1960 | ✗ | sl->deblocking_filter = 2; | |
1961 | } else { | ||
1962 | ✗ | h->postpone_filter = 1; | |
1963 | } | ||
1964 | } | ||
1965 | 37376 | sl->qp_thresh = 15 - | |
1966 | 37376 | FFMIN(sl->slice_alpha_c0_offset, sl->slice_beta_offset) - | |
1967 | 37376 | FFMAX3(0, | |
1968 | h->ps.pps->chroma_qp_index_offset[0], | ||
1969 | 37376 | h->ps.pps->chroma_qp_index_offset[1]) + | |
1970 | 37376 | 6 * (h->ps.sps->bit_depth_luma - 8); | |
1971 | |||
1972 | 37376 | sl->slice_num = ++h->current_slice; | |
1973 | |||
1974 |
1/2✓ Branch 0 taken 37376 times.
✗ Branch 1 not taken.
|
37376 | if (sl->slice_num) |
1975 | 37376 | h->slice_row[(sl->slice_num-1)&(MAX_SLICES-1)]= sl->resync_mb_y; | |
1976 |
2/2✓ Branch 0 taken 34504 times.
✓ Branch 1 taken 2872 times.
|
37376 | if ( h->slice_row[sl->slice_num&(MAX_SLICES-1)] + 3 >= sl->resync_mb_y |
1977 |
2/2✓ Branch 0 taken 25765 times.
✓ Branch 1 taken 8739 times.
|
34504 | && h->slice_row[sl->slice_num&(MAX_SLICES-1)] <= sl->resync_mb_y |
1978 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 25765 times.
|
25765 | && sl->slice_num >= MAX_SLICES) { |
1979 | //in case of ASO this check needs to be updated depending on how we decide to assign slice numbers in this case | ||
1980 | ✗ | av_log(h->avctx, AV_LOG_WARNING, "Possibly too many slices (%d >= %d), increase MAX_SLICES and recompile if there are artifacts\n", sl->slice_num, MAX_SLICES); | |
1981 | } | ||
1982 | |||
1983 |
2/2✓ Branch 0 taken 74752 times.
✓ Branch 1 taken 37376 times.
|
112128 | for (j = 0; j < 2; j++) { |
1984 | int id_list[16]; | ||
1985 | 74752 | int *ref2frm = h->ref2frm[sl->slice_num & (MAX_SLICES - 1)][j]; | |
1986 |
2/2✓ Branch 0 taken 1196032 times.
✓ Branch 1 taken 74752 times.
|
1270784 | for (i = 0; i < 16; i++) { |
1987 | 1196032 | id_list[i] = 60; | |
1988 |
4/4✓ Branch 0 taken 736304 times.
✓ Branch 1 taken 459728 times.
✓ Branch 2 taken 140561 times.
✓ Branch 3 taken 595743 times.
|
1196032 | if (j < sl->list_count && i < sl->ref_count[j] && |
1989 |
1/2✓ Branch 0 taken 140561 times.
✗ Branch 1 not taken.
|
140561 | sl->ref_list[j][i].parent->f->buf[0]) { |
1990 | int k; | ||
1991 | 140561 | const AVBuffer *buf = sl->ref_list[j][i].parent->f->buf[0]->buffer; | |
1992 |
2/2✓ Branch 0 taken 469158 times.
✓ Branch 1 taken 1933 times.
|
471091 | for (k = 0; k < h->short_ref_count; k++) |
1993 |
2/2✓ Branch 0 taken 138628 times.
✓ Branch 1 taken 330530 times.
|
469158 | if (h->short_ref[k]->f->buf[0]->buffer == buf) { |
1994 | 138628 | id_list[i] = k; | |
1995 | 138628 | break; | |
1996 | } | ||
1997 |
2/2✓ Branch 0 taken 11870 times.
✓ Branch 1 taken 138769 times.
|
150639 | for (k = 0; k < h->long_ref_count; k++) |
1998 |
4/4✓ Branch 0 taken 11269 times.
✓ Branch 1 taken 601 times.
✓ Branch 2 taken 1792 times.
✓ Branch 3 taken 9477 times.
|
11870 | if (h->long_ref[k] && h->long_ref[k]->f->buf[0]->buffer == buf) { |
1999 | 1792 | id_list[i] = h->short_ref_count + k; | |
2000 | 1792 | break; | |
2001 | } | ||
2002 | } | ||
2003 | } | ||
2004 | |||
2005 | 74752 | ref2frm[0] = | |
2006 | 74752 | ref2frm[1] = -1; | |
2007 |
2/2✓ Branch 0 taken 1196032 times.
✓ Branch 1 taken 74752 times.
|
1270784 | for (i = 0; i < 16; i++) |
2008 | 1196032 | ref2frm[i + 2] = 4 * id_list[i] + (sl->ref_list[j][i].reference & 3); | |
2009 | 74752 | ref2frm[18 + 0] = | |
2010 | 74752 | ref2frm[18 + 1] = -1; | |
2011 |
2/2✓ Branch 0 taken 2392064 times.
✓ Branch 1 taken 74752 times.
|
2466816 | for (i = 16; i < 48; i++) |
2012 | 2392064 | ref2frm[i + 4] = 4 * id_list[(i - 16) >> 1] + | |
2013 | 2392064 | (sl->ref_list[j][i].reference & 3); | |
2014 | } | ||
2015 | |||
2016 |
2/2✓ Branch 0 taken 4468 times.
✓ Branch 1 taken 32908 times.
|
37376 | if (sl->slice_type_nos == AV_PICTURE_TYPE_I) { |
2017 | 4468 | h->cur_pic_ptr->gray = 0; | |
2018 | 4468 | h->non_gray = 1; | |
2019 | } else { | ||
2020 | 32908 | int gray = 0; | |
2021 |
2/2✓ Branch 0 taken 46019 times.
✓ Branch 1 taken 32908 times.
|
78927 | for (j = 0; j < sl->list_count; j++) { |
2022 |
2/2✓ Branch 0 taken 140561 times.
✓ Branch 1 taken 46019 times.
|
186580 | for (i = 0; i < sl->ref_count[j]; i++) { |
2023 | 140561 | gray |= sl->ref_list[j][i].parent->gray; | |
2024 | } | ||
2025 | } | ||
2026 | 32908 | h->cur_pic_ptr->gray = gray; | |
2027 | } | ||
2028 | |||
2029 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 37376 times.
|
37376 | if (h->avctx->debug & FF_DEBUG_PICT_INFO) { |
2030 | ✗ | av_log(h->avctx, AV_LOG_DEBUG, | |
2031 | "slice:%d %c mb:%d %c%s%s frame:%d poc:%d/%d ref:%d/%d qp:%d loop:%d:%d:%d weight:%d%s %s\n", | ||
2032 | sl->slice_num, | ||
2033 | ✗ | (h->picture_structure == PICT_FRAME ? 'F' : h->picture_structure == PICT_TOP_FIELD ? 'T' : 'B'), | |
2034 | ✗ | sl->mb_y * h->mb_width + sl->mb_x, | |
2035 | ✗ | av_get_picture_type_char(sl->slice_type), | |
2036 | ✗ | sl->slice_type_fixed ? " fix" : "", | |
2037 | ✗ | nal->type == H264_NAL_IDR_SLICE ? " IDR" : "", | |
2038 | h->poc.frame_num, | ||
2039 | ✗ | h->cur_pic_ptr->field_poc[0], | |
2040 | ✗ | h->cur_pic_ptr->field_poc[1], | |
2041 | sl->ref_count[0], sl->ref_count[1], | ||
2042 | sl->qscale, | ||
2043 | sl->deblocking_filter, | ||
2044 | sl->slice_alpha_c0_offset, sl->slice_beta_offset, | ||
2045 | sl->pwt.use_weight, | ||
2046 | ✗ | sl->pwt.use_weight == 1 && sl->pwt.use_weight_chroma ? "c" : "", | |
2047 | ✗ | sl->slice_type == AV_PICTURE_TYPE_B ? (sl->direct_spatial_mv_pred ? "SPAT" : "TEMP") : ""); | |
2048 | } | ||
2049 | |||
2050 | 37376 | return 0; | |
2051 | } | ||
2052 | |||
2053 | 37623 | int ff_h264_queue_decode_slice(H264Context *h, const H2645NAL *nal) | |
2054 | { | ||
2055 | 37623 | H264SliceContext *sl = h->slice_ctx + h->nb_slice_ctx_queued; | |
2056 |
4/4✓ Branch 0 taken 37173 times.
✓ Branch 1 taken 450 times.
✓ Branch 2 taken 28352 times.
✓ Branch 3 taken 8821 times.
|
37623 | int first_slice = sl == h->slice_ctx && !h->current_slice; |
2057 | int ret; | ||
2058 | |||
2059 | 37623 | sl->gb = nal->gb; | |
2060 | |||
2061 | 37623 | ret = h264_slice_header_parse(h, sl, nal); | |
2062 |
2/2✓ Branch 0 taken 167 times.
✓ Branch 1 taken 37456 times.
|
37623 | if (ret < 0) |
2063 | 167 | return ret; | |
2064 | |||
2065 | // discard redundant pictures | ||
2066 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 37456 times.
|
37456 | if (sl->redundant_pic_count > 0) { |
2067 | ✗ | sl->ref_count[0] = sl->ref_count[1] = 0; | |
2068 | ✗ | return 0; | |
2069 | } | ||
2070 | |||
2071 |
3/4✓ Branch 0 taken 9073 times.
✓ Branch 1 taken 28383 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 9073 times.
|
37456 | if (sl->first_mb_addr == 0 || !h->current_slice) { |
2072 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 28383 times.
|
28383 | if (h->setup_finished) { |
2073 | ✗ | av_log(h->avctx, AV_LOG_ERROR, "Too many fields\n"); | |
2074 | ✗ | return AVERROR_INVALIDDATA; | |
2075 | } | ||
2076 | } | ||
2077 | |||
2078 |
2/2✓ Branch 0 taken 28383 times.
✓ Branch 1 taken 9073 times.
|
37456 | if (sl->first_mb_addr == 0) { // FIXME better field boundary detection |
2079 |
2/2✓ Branch 0 taken 194 times.
✓ Branch 1 taken 28189 times.
|
28383 | if (h->current_slice) { |
2080 | // this slice starts a new field | ||
2081 | // first decode any pending queued slices | ||
2082 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 194 times.
|
194 | if (h->nb_slice_ctx_queued) { |
2083 | H264SliceContext tmp_ctx; | ||
2084 | |||
2085 | ✗ | ret = ff_h264_execute_decode_slices(h); | |
2086 | ✗ | if (ret < 0 && (h->avctx->err_recognition & AV_EF_EXPLODE)) | |
2087 | ✗ | return ret; | |
2088 | |||
2089 | ✗ | memcpy(&tmp_ctx, h->slice_ctx, sizeof(tmp_ctx)); | |
2090 | ✗ | memcpy(h->slice_ctx, sl, sizeof(tmp_ctx)); | |
2091 | ✗ | memcpy(sl, &tmp_ctx, sizeof(tmp_ctx)); | |
2092 | ✗ | sl = h->slice_ctx; | |
2093 | } | ||
2094 | |||
2095 |
3/6✓ Branch 0 taken 194 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 194 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 194 times.
✗ Branch 5 not taken.
|
194 | if (h->cur_pic_ptr && FIELD_PICTURE(h) && h->first_field) { |
2096 | 194 | ret = ff_h264_field_end(h, h->slice_ctx, 1); | |
2097 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 194 times.
|
194 | if (ret < 0) |
2098 | ✗ | return ret; | |
2099 | ✗ | } else if (h->cur_pic_ptr && !FIELD_PICTURE(h) && !h->first_field && h->nal_unit_type == H264_NAL_IDR_SLICE) { | |
2100 | ✗ | av_log(h, AV_LOG_WARNING, "Broken frame packetizing\n"); | |
2101 | ✗ | ret = ff_h264_field_end(h, h->slice_ctx, 1); | |
2102 | ✗ | ff_thread_report_progress(&h->cur_pic_ptr->tf, INT_MAX, 0); | |
2103 | ✗ | ff_thread_report_progress(&h->cur_pic_ptr->tf, INT_MAX, 1); | |
2104 | ✗ | h->cur_pic_ptr = NULL; | |
2105 | ✗ | if (ret < 0) | |
2106 | ✗ | return ret; | |
2107 | } else | ||
2108 | ✗ | return AVERROR_INVALIDDATA; | |
2109 | } | ||
2110 | |||
2111 |
2/2✓ Branch 0 taken 25016 times.
✓ Branch 1 taken 3367 times.
|
28383 | if (!h->first_field) { |
2112 |
3/4✓ Branch 0 taken 299 times.
✓ Branch 1 taken 24717 times.
✓ Branch 2 taken 299 times.
✗ Branch 3 not taken.
|
25016 | if (h->cur_pic_ptr && !h->droppable) { |
2113 | 299 | ff_thread_report_progress(&h->cur_pic_ptr->tf, INT_MAX, | |
2114 | 299 | h->picture_structure == PICT_BOTTOM_FIELD); | |
2115 | } | ||
2116 | 25016 | h->cur_pic_ptr = NULL; | |
2117 | } | ||
2118 | } | ||
2119 | |||
2120 |
2/2✓ Branch 0 taken 28383 times.
✓ Branch 1 taken 9073 times.
|
37456 | if (!h->current_slice) |
2121 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 28383 times.
|
28383 | av_assert0(sl == h->slice_ctx); |
2122 | |||
2123 |
4/4✓ Branch 0 taken 28383 times.
✓ Branch 1 taken 9073 times.
✓ Branch 2 taken 25016 times.
✓ Branch 3 taken 3367 times.
|
37456 | if (h->current_slice == 0 && !h->first_field) { |
2124 | 25016 | if ( | |
2125 |
3/4✓ Branch 0 taken 84 times.
✓ Branch 1 taken 24932 times.
✓ Branch 2 taken 84 times.
✗ Branch 3 not taken.
|
25016 | (h->avctx->skip_frame >= AVDISCARD_NONREF && !h->nal_ref_idc) || |
2126 |
4/4✓ Branch 0 taken 84 times.
✓ Branch 1 taken 24932 times.
✓ Branch 2 taken 40 times.
✓ Branch 3 taken 44 times.
|
25016 | (h->avctx->skip_frame >= AVDISCARD_BIDIR && sl->slice_type_nos == AV_PICTURE_TYPE_B) || |
2127 |
4/4✓ Branch 0 taken 40 times.
✓ Branch 1 taken 24932 times.
✓ Branch 2 taken 4 times.
✓ Branch 3 taken 36 times.
|
24972 | (h->avctx->skip_frame >= AVDISCARD_NONINTRA && sl->slice_type_nos != AV_PICTURE_TYPE_I) || |
2128 |
4/6✓ Branch 0 taken 2 times.
✓ Branch 1 taken 24934 times.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
|
24936 | (h->avctx->skip_frame >= AVDISCARD_NONKEY && h->nal_unit_type != H264_NAL_IDR_SLICE && h->sei.recovery_point.recovery_frame_cnt < 0) || |
2129 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 24936 times.
|
24936 | h->avctx->skip_frame >= AVDISCARD_ALL) { |
2130 | 80 | return 0; | |
2131 | } | ||
2132 | } | ||
2133 | |||
2134 |
2/2✓ Branch 0 taken 9267 times.
✓ Branch 1 taken 28109 times.
|
37376 | if (!first_slice) { |
2135 | 9267 | const PPS *pps = h->ps.pps_list[sl->pps_id]; | |
2136 | |||
2137 |
1/2✓ Branch 0 taken 9267 times.
✗ Branch 1 not taken.
|
9267 | if (h->ps.pps->sps_id != pps->sps_id || |
2138 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 9267 times.
|
9267 | h->ps.pps->transform_8x8_mode != pps->transform_8x8_mode /*|| |
2139 | (h->setup_finished && h->ps.pps != pps)*/) { | ||
2140 | ✗ | av_log(h->avctx, AV_LOG_ERROR, "PPS changed between slices\n"); | |
2141 | ✗ | return AVERROR_INVALIDDATA; | |
2142 | } | ||
2143 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 9267 times.
|
9267 | if (h->ps.sps != pps->sps) { |
2144 | ✗ | av_log(h->avctx, AV_LOG_ERROR, | |
2145 | "SPS changed in the middle of the frame\n"); | ||
2146 | ✗ | return AVERROR_INVALIDDATA; | |
2147 | } | ||
2148 | } | ||
2149 | |||
2150 |
2/2✓ Branch 0 taken 28303 times.
✓ Branch 1 taken 9073 times.
|
37376 | if (h->current_slice == 0) { |
2151 | 28303 | ret = h264_field_start(h, sl, nal, first_slice); | |
2152 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 28303 times.
|
28303 | if (ret < 0) |
2153 | ✗ | return ret; | |
2154 | } else { | ||
2155 |
1/2✓ Branch 0 taken 9073 times.
✗ Branch 1 not taken.
|
9073 | if (h->picture_structure != sl->picture_structure || |
2156 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 9073 times.
|
9073 | h->droppable != (nal->ref_idc == 0)) { |
2157 | ✗ | av_log(h->avctx, AV_LOG_ERROR, | |
2158 | "Changing field mode (%d -> %d) between slices is not allowed\n", | ||
2159 | h->picture_structure, sl->picture_structure); | ||
2160 | ✗ | return AVERROR_INVALIDDATA; | |
2161 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 9073 times.
|
9073 | } else if (!h->cur_pic_ptr) { |
2162 | ✗ | av_log(h->avctx, AV_LOG_ERROR, | |
2163 | "unset cur_pic_ptr on slice %d\n", | ||
2164 | ✗ | h->current_slice + 1); | |
2165 | ✗ | return AVERROR_INVALIDDATA; | |
2166 | } | ||
2167 | } | ||
2168 | |||
2169 | 37376 | ret = h264_slice_init(h, sl, nal); | |
2170 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 37376 times.
|
37376 | if (ret < 0) |
2171 | ✗ | return ret; | |
2172 | |||
2173 | 37376 | h->nb_slice_ctx_queued++; | |
2174 | |||
2175 | 37376 | return 0; | |
2176 | } | ||
2177 | |||
2178 | ✗ | int ff_h264_get_slice_type(const H264SliceContext *sl) | |
2179 | { | ||
2180 | ✗ | switch (sl->slice_type) { | |
2181 | ✗ | case AV_PICTURE_TYPE_P: | |
2182 | ✗ | return 0; | |
2183 | ✗ | case AV_PICTURE_TYPE_B: | |
2184 | ✗ | return 1; | |
2185 | ✗ | case AV_PICTURE_TYPE_I: | |
2186 | ✗ | return 2; | |
2187 | ✗ | case AV_PICTURE_TYPE_SP: | |
2188 | ✗ | return 3; | |
2189 | ✗ | case AV_PICTURE_TYPE_SI: | |
2190 | ✗ | return 4; | |
2191 | ✗ | default: | |
2192 | ✗ | return AVERROR_INVALIDDATA; | |
2193 | } | ||
2194 | } | ||
2195 | |||
2196 | 12360735 | static av_always_inline void fill_filter_caches_inter(const H264Context *h, | |
2197 | H264SliceContext *sl, | ||
2198 | int mb_type, int top_xy, | ||
2199 | const int left_xy[LEFT_MBS], | ||
2200 | int top_type, | ||
2201 | const int left_type[LEFT_MBS], | ||
2202 | int mb_xy, int list) | ||
2203 | { | ||
2204 | 12360735 | int b_stride = h->b_stride; | |
2205 | 12360735 | int16_t(*mv_dst)[2] = &sl->mv_cache[list][scan8[0]]; | |
2206 | 12360735 | int8_t *ref_cache = &sl->ref_cache[list][scan8[0]]; | |
2207 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 12360735 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
12360735 | if (IS_INTER(mb_type) || IS_DIRECT(mb_type)) { |
2208 |
2/2✓ Branch 0 taken 9938666 times.
✓ Branch 1 taken 2422069 times.
|
12360735 | if (USES_LIST(top_type, list)) { |
2209 | 9938666 | const int b_xy = h->mb2b_xy[top_xy] + 3 * b_stride; | |
2210 | 9938666 | const int b8_xy = 4 * top_xy + 2; | |
2211 |
2/2✓ Branch 0 taken 342067 times.
✓ Branch 1 taken 9596599 times.
|
9938666 | const int *ref2frm = &h->ref2frm[h->slice_table[top_xy] & (MAX_SLICES - 1)][list][(MB_MBAFF(sl) ? 20 : 2)]; |
2212 | 9938666 | AV_COPY128(mv_dst - 1 * 8, h->cur_pic.motion_val[list][b_xy + 0]); | |
2213 | 9938666 | ref_cache[0 - 1 * 8] = | |
2214 | 9938666 | ref_cache[1 - 1 * 8] = ref2frm[h->cur_pic.ref_index[list][b8_xy + 0]]; | |
2215 | 9938666 | ref_cache[2 - 1 * 8] = | |
2216 | 9938666 | ref_cache[3 - 1 * 8] = ref2frm[h->cur_pic.ref_index[list][b8_xy + 1]]; | |
2217 | } else { | ||
2218 | 2422069 | AV_ZERO128(mv_dst - 1 * 8); | |
2219 | 2422069 | AV_WN32A(&ref_cache[0 - 1 * 8], ((LIST_NOT_USED) & 0xFF) * 0x01010101u); | |
2220 | } | ||
2221 | |||
2222 |
2/2✓ Branch 0 taken 11927905 times.
✓ Branch 1 taken 432830 times.
|
12360735 | if (!IS_INTERLACED(mb_type ^ left_type[LTOP])) { |
2223 |
2/2✓ Branch 0 taken 9963469 times.
✓ Branch 1 taken 1964436 times.
|
11927905 | if (USES_LIST(left_type[LTOP], list)) { |
2224 | 9963469 | const int b_xy = h->mb2b_xy[left_xy[LTOP]] + 3; | |
2225 | 9963469 | const int b8_xy = 4 * left_xy[LTOP] + 1; | |
2226 |
2/2✓ Branch 0 taken 239010 times.
✓ Branch 1 taken 9724459 times.
|
9963469 | const int *ref2frm = &h->ref2frm[h->slice_table[left_xy[LTOP]] & (MAX_SLICES - 1)][list][(MB_MBAFF(sl) ? 20 : 2)]; |
2227 | 9963469 | AV_COPY32(mv_dst - 1 + 0, h->cur_pic.motion_val[list][b_xy + b_stride * 0]); | |
2228 | 9963469 | AV_COPY32(mv_dst - 1 + 8, h->cur_pic.motion_val[list][b_xy + b_stride * 1]); | |
2229 | 9963469 | AV_COPY32(mv_dst - 1 + 16, h->cur_pic.motion_val[list][b_xy + b_stride * 2]); | |
2230 | 9963469 | AV_COPY32(mv_dst - 1 + 24, h->cur_pic.motion_val[list][b_xy + b_stride * 3]); | |
2231 | 9963469 | ref_cache[-1 + 0] = | |
2232 | 9963469 | ref_cache[-1 + 8] = ref2frm[h->cur_pic.ref_index[list][b8_xy + 2 * 0]]; | |
2233 | 9963469 | ref_cache[-1 + 16] = | |
2234 | 9963469 | ref_cache[-1 + 24] = ref2frm[h->cur_pic.ref_index[list][b8_xy + 2 * 1]]; | |
2235 | } else { | ||
2236 | 1964436 | AV_ZERO32(mv_dst - 1 + 0); | |
2237 | 1964436 | AV_ZERO32(mv_dst - 1 + 8); | |
2238 | 1964436 | AV_ZERO32(mv_dst - 1 + 16); | |
2239 | 1964436 | AV_ZERO32(mv_dst - 1 + 24); | |
2240 | 1964436 | ref_cache[-1 + 0] = | |
2241 | 1964436 | ref_cache[-1 + 8] = | |
2242 | 1964436 | ref_cache[-1 + 16] = | |
2243 | 1964436 | ref_cache[-1 + 24] = LIST_NOT_USED; | |
2244 | } | ||
2245 | } | ||
2246 | } | ||
2247 | |||
2248 |
2/2✓ Branch 0 taken 1379408 times.
✓ Branch 1 taken 10981327 times.
|
12360735 | if (!USES_LIST(mb_type, list)) { |
2249 | 1379408 | fill_rectangle(mv_dst, 4, 4, 8, pack16to32(0, 0), 4); | |
2250 | 1379408 | AV_WN32A(&ref_cache[0 * 8], ((LIST_NOT_USED) & 0xFF) * 0x01010101u); | |
2251 | 1379408 | AV_WN32A(&ref_cache[1 * 8], ((LIST_NOT_USED) & 0xFF) * 0x01010101u); | |
2252 | 1379408 | AV_WN32A(&ref_cache[2 * 8], ((LIST_NOT_USED) & 0xFF) * 0x01010101u); | |
2253 | 1379408 | AV_WN32A(&ref_cache[3 * 8], ((LIST_NOT_USED) & 0xFF) * 0x01010101u); | |
2254 | 1379408 | return; | |
2255 | } | ||
2256 | |||
2257 | { | ||
2258 | 10981327 | const int8_t *ref = &h->cur_pic.ref_index[list][4 * mb_xy]; | |
2259 |
2/2✓ Branch 0 taken 412907 times.
✓ Branch 1 taken 10568420 times.
|
10981327 | const int *ref2frm = &h->ref2frm[sl->slice_num & (MAX_SLICES - 1)][list][(MB_MBAFF(sl) ? 20 : 2)]; |
2260 | 10981327 | uint32_t ref01 = (pack16to32(ref2frm[ref[0]], ref2frm[ref[1]]) & 0x00FF00FF) * 0x0101; | |
2261 | 10981327 | uint32_t ref23 = (pack16to32(ref2frm[ref[2]], ref2frm[ref[3]]) & 0x00FF00FF) * 0x0101; | |
2262 | 10981327 | AV_WN32A(&ref_cache[0 * 8], ref01); | |
2263 | 10981327 | AV_WN32A(&ref_cache[1 * 8], ref01); | |
2264 | 10981327 | AV_WN32A(&ref_cache[2 * 8], ref23); | |
2265 | 10981327 | AV_WN32A(&ref_cache[3 * 8], ref23); | |
2266 | } | ||
2267 | |||
2268 | { | ||
2269 | 10981327 | int16_t(*mv_src)[2] = &h->cur_pic.motion_val[list][4 * sl->mb_x + 4 * sl->mb_y * b_stride]; | |
2270 | 10981327 | AV_COPY128(mv_dst + 8 * 0, mv_src + 0 * b_stride); | |
2271 | 10981327 | AV_COPY128(mv_dst + 8 * 1, mv_src + 1 * b_stride); | |
2272 | 10981327 | AV_COPY128(mv_dst + 8 * 2, mv_src + 2 * b_stride); | |
2273 | 10981327 | AV_COPY128(mv_dst + 8 * 3, mv_src + 3 * b_stride); | |
2274 | } | ||
2275 | } | ||
2276 | |||
2277 | /** | ||
2278 | * @return non zero if the loop filter can be skipped | ||
2279 | */ | ||
2280 | 12580480 | static int fill_filter_caches(const H264Context *h, H264SliceContext *sl, int mb_type) | |
2281 | { | ||
2282 | 12580480 | const int mb_xy = sl->mb_xy; | |
2283 | int top_xy, left_xy[LEFT_MBS]; | ||
2284 | int top_type, left_type[LEFT_MBS]; | ||
2285 | const uint8_t *nnz; | ||
2286 | uint8_t *nnz_cache; | ||
2287 | |||
2288 | 12580480 | top_xy = mb_xy - (h->mb_stride << MB_FIELD(sl)); | |
2289 | |||
2290 | 12580480 | left_xy[LBOT] = left_xy[LTOP] = mb_xy - 1; | |
2291 |
2/2✓ Branch 0 taken 2022480 times.
✓ Branch 1 taken 10558000 times.
|
12580480 | if (FRAME_MBAFF(h)) { |
2292 | 2022480 | const int left_mb_field_flag = IS_INTERLACED(h->cur_pic.mb_type[mb_xy - 1]); | |
2293 | 2022480 | const int curr_mb_field_flag = IS_INTERLACED(mb_type); | |
2294 |
2/2✓ Branch 0 taken 1011240 times.
✓ Branch 1 taken 1011240 times.
|
2022480 | if (sl->mb_y & 1) { |
2295 |
2/2✓ Branch 0 taken 205020 times.
✓ Branch 1 taken 806220 times.
|
1011240 | if (left_mb_field_flag != curr_mb_field_flag) |
2296 | 205020 | left_xy[LTOP] -= h->mb_stride; | |
2297 | } else { | ||
2298 |
2/2✓ Branch 0 taken 263190 times.
✓ Branch 1 taken 748050 times.
|
1011240 | if (curr_mb_field_flag) |
2299 | 263190 | top_xy += h->mb_stride & | |
2300 | 263190 | (((h->cur_pic.mb_type[top_xy] >> 7) & 1) - 1); | |
2301 |
2/2✓ Branch 0 taken 205020 times.
✓ Branch 1 taken 806220 times.
|
1011240 | if (left_mb_field_flag != curr_mb_field_flag) |
2302 | 205020 | left_xy[LBOT] += h->mb_stride; | |
2303 | } | ||
2304 | } | ||
2305 | |||
2306 | 12580480 | sl->top_mb_xy = top_xy; | |
2307 | 12580480 | sl->left_mb_xy[LTOP] = left_xy[LTOP]; | |
2308 | 12580480 | sl->left_mb_xy[LBOT] = left_xy[LBOT]; | |
2309 | { | ||
2310 | /* For sufficiently low qp, filtering wouldn't do anything. | ||
2311 | * This is a conservative estimate: could also check beta_offset | ||
2312 | * and more accurate chroma_qp. */ | ||
2313 | 12580480 | int qp_thresh = sl->qp_thresh; // FIXME strictly we should store qp_thresh for each mb of a slice | |
2314 | 12580480 | int qp = h->cur_pic.qscale_table[mb_xy]; | |
2315 |
2/2✓ Branch 0 taken 1768982 times.
✓ Branch 1 taken 10811498 times.
|
12580480 | if (qp <= qp_thresh && |
2316 |
2/2✓ Branch 0 taken 1767272 times.
✓ Branch 1 taken 1710 times.
|
1768982 | (left_xy[LTOP] < 0 || |
2317 |
4/4✓ Branch 0 taken 1707216 times.
✓ Branch 1 taken 60056 times.
✓ Branch 2 taken 1624300 times.
✓ Branch 3 taken 84626 times.
|
1768982 | ((qp + h->cur_pic.qscale_table[left_xy[LTOP]] + 1) >> 1) <= qp_thresh) && |
2318 | 1624300 | (top_xy < 0 || | |
2319 |
2/2✓ Branch 0 taken 1429319 times.
✓ Branch 1 taken 194981 times.
|
1624300 | ((qp + h->cur_pic.qscale_table[top_xy] + 1) >> 1) <= qp_thresh)) { |
2320 |
2/2✓ Branch 0 taken 1198134 times.
✓ Branch 1 taken 315811 times.
|
1513945 | if (!FRAME_MBAFF(h)) |
2321 | 1198134 | return 1; | |
2322 |
2/2✓ Branch 0 taken 315680 times.
✓ Branch 1 taken 131 times.
|
315811 | if ((left_xy[LTOP] < 0 || |
2323 |
2/2✓ Branch 0 taken 315151 times.
✓ Branch 1 taken 529 times.
|
315680 | ((qp + h->cur_pic.qscale_table[left_xy[LBOT]] + 1) >> 1) <= qp_thresh) && |
2324 |
2/2✓ Branch 0 taken 294673 times.
✓ Branch 1 taken 20609 times.
|
315282 | (top_xy < h->mb_stride || |
2325 |
2/2✓ Branch 0 taken 271562 times.
✓ Branch 1 taken 23111 times.
|
294673 | ((qp + h->cur_pic.qscale_table[top_xy - h->mb_stride] + 1) >> 1) <= qp_thresh)) |
2326 | 292171 | return 1; | |
2327 | } | ||
2328 | } | ||
2329 | |||
2330 | 11090175 | top_type = h->cur_pic.mb_type[top_xy]; | |
2331 | 11090175 | left_type[LTOP] = h->cur_pic.mb_type[left_xy[LTOP]]; | |
2332 | 11090175 | left_type[LBOT] = h->cur_pic.mb_type[left_xy[LBOT]]; | |
2333 |
2/2✓ Branch 0 taken 470376 times.
✓ Branch 1 taken 10619799 times.
|
11090175 | if (sl->deblocking_filter == 2) { |
2334 |
2/2✓ Branch 0 taken 56878 times.
✓ Branch 1 taken 413498 times.
|
470376 | if (h->slice_table[top_xy] != sl->slice_num) |
2335 | 56878 | top_type = 0; | |
2336 |
2/2✓ Branch 0 taken 13874 times.
✓ Branch 1 taken 456502 times.
|
470376 | if (h->slice_table[left_xy[LBOT]] != sl->slice_num) |
2337 | 13874 | left_type[LTOP] = left_type[LBOT] = 0; | |
2338 | } else { | ||
2339 |
2/2✓ Branch 0 taken 473948 times.
✓ Branch 1 taken 10145851 times.
|
10619799 | if (h->slice_table[top_xy] == 0xFFFF) |
2340 | 473948 | top_type = 0; | |
2341 |
2/2✓ Branch 0 taken 304266 times.
✓ Branch 1 taken 10315533 times.
|
10619799 | if (h->slice_table[left_xy[LBOT]] == 0xFFFF) |
2342 | 304266 | left_type[LTOP] = left_type[LBOT] = 0; | |
2343 | } | ||
2344 | 11090175 | sl->top_type = top_type; | |
2345 | 11090175 | sl->left_type[LTOP] = left_type[LTOP]; | |
2346 | 11090175 | sl->left_type[LBOT] = left_type[LBOT]; | |
2347 | |||
2348 |
2/2✓ Branch 0 taken 2985063 times.
✓ Branch 1 taken 8105112 times.
|
11090175 | if (IS_INTRA(mb_type)) |
2349 | 2985063 | return 0; | |
2350 | |||
2351 | 8105112 | fill_filter_caches_inter(h, sl, mb_type, top_xy, left_xy, | |
2352 | top_type, left_type, mb_xy, 0); | ||
2353 |
2/2✓ Branch 0 taken 4255623 times.
✓ Branch 1 taken 3849489 times.
|
8105112 | if (sl->list_count == 2) |
2354 | 4255623 | fill_filter_caches_inter(h, sl, mb_type, top_xy, left_xy, | |
2355 | top_type, left_type, mb_xy, 1); | ||
2356 | |||
2357 | 8105112 | nnz = h->non_zero_count[mb_xy]; | |
2358 | 8105112 | nnz_cache = sl->non_zero_count_cache; | |
2359 | 8105112 | AV_COPY32(&nnz_cache[4 + 8 * 1], &nnz[0]); | |
2360 | 8105112 | AV_COPY32(&nnz_cache[4 + 8 * 2], &nnz[4]); | |
2361 | 8105112 | AV_COPY32(&nnz_cache[4 + 8 * 3], &nnz[8]); | |
2362 | 8105112 | AV_COPY32(&nnz_cache[4 + 8 * 4], &nnz[12]); | |
2363 | 8105112 | sl->cbp = h->cbp_table[mb_xy]; | |
2364 | |||
2365 |
2/2✓ Branch 0 taken 7678188 times.
✓ Branch 1 taken 426924 times.
|
8105112 | if (top_type) { |
2366 | 7678188 | nnz = h->non_zero_count[top_xy]; | |
2367 | 7678188 | AV_COPY32(&nnz_cache[4 + 8 * 0], &nnz[3 * 4]); | |
2368 | } | ||
2369 | |||
2370 |
2/2✓ Branch 0 taken 7841490 times.
✓ Branch 1 taken 263622 times.
|
8105112 | if (left_type[LTOP]) { |
2371 | 7841490 | nnz = h->non_zero_count[left_xy[LTOP]]; | |
2372 | 7841490 | nnz_cache[3 + 8 * 1] = nnz[3 + 0 * 4]; | |
2373 | 7841490 | nnz_cache[3 + 8 * 2] = nnz[3 + 1 * 4]; | |
2374 | 7841490 | nnz_cache[3 + 8 * 3] = nnz[3 + 2 * 4]; | |
2375 | 7841490 | nnz_cache[3 + 8 * 4] = nnz[3 + 3 * 4]; | |
2376 | } | ||
2377 | |||
2378 | /* CAVLC 8x8dct requires NNZ values for residual decoding that differ | ||
2379 | * from what the loop filter needs */ | ||
2380 |
4/4✓ Branch 0 taken 2070968 times.
✓ Branch 1 taken 6034144 times.
✓ Branch 2 taken 246166 times.
✓ Branch 3 taken 1824802 times.
|
8105112 | if (!CABAC(h) && h->ps.pps->transform_8x8_mode) { |
2381 |
2/2✓ Branch 0 taken 107789 times.
✓ Branch 1 taken 138377 times.
|
246166 | if (IS_8x8DCT(top_type)) { |
2382 | 107789 | nnz_cache[4 + 8 * 0] = | |
2383 | 107789 | nnz_cache[5 + 8 * 0] = (h->cbp_table[top_xy] & 0x4000) >> 12; | |
2384 | 107789 | nnz_cache[6 + 8 * 0] = | |
2385 | 107789 | nnz_cache[7 + 8 * 0] = (h->cbp_table[top_xy] & 0x8000) >> 12; | |
2386 | } | ||
2387 |
2/2✓ Branch 0 taken 110456 times.
✓ Branch 1 taken 135710 times.
|
246166 | if (IS_8x8DCT(left_type[LTOP])) { |
2388 | 110456 | nnz_cache[3 + 8 * 1] = | |
2389 | 110456 | nnz_cache[3 + 8 * 2] = (h->cbp_table[left_xy[LTOP]] & 0x2000) >> 12; // FIXME check MBAFF | |
2390 | } | ||
2391 |
2/2✓ Branch 0 taken 110556 times.
✓ Branch 1 taken 135610 times.
|
246166 | if (IS_8x8DCT(left_type[LBOT])) { |
2392 | 110556 | nnz_cache[3 + 8 * 3] = | |
2393 | 110556 | nnz_cache[3 + 8 * 4] = (h->cbp_table[left_xy[LBOT]] & 0x8000) >> 12; // FIXME check MBAFF | |
2394 | } | ||
2395 | |||
2396 |
2/2✓ Branch 0 taken 104872 times.
✓ Branch 1 taken 141294 times.
|
246166 | if (IS_8x8DCT(mb_type)) { |
2397 | 104872 | nnz_cache[scan8[0]] = | |
2398 | 104872 | nnz_cache[scan8[1]] = | |
2399 | 104872 | nnz_cache[scan8[2]] = | |
2400 | 104872 | nnz_cache[scan8[3]] = (sl->cbp & 0x1000) >> 12; | |
2401 | |||
2402 | 104872 | nnz_cache[scan8[0 + 4]] = | |
2403 | 104872 | nnz_cache[scan8[1 + 4]] = | |
2404 | 104872 | nnz_cache[scan8[2 + 4]] = | |
2405 | 104872 | nnz_cache[scan8[3 + 4]] = (sl->cbp & 0x2000) >> 12; | |
2406 | |||
2407 | 104872 | nnz_cache[scan8[0 + 8]] = | |
2408 | 104872 | nnz_cache[scan8[1 + 8]] = | |
2409 | 104872 | nnz_cache[scan8[2 + 8]] = | |
2410 | 104872 | nnz_cache[scan8[3 + 8]] = (sl->cbp & 0x4000) >> 12; | |
2411 | |||
2412 | 104872 | nnz_cache[scan8[0 + 12]] = | |
2413 | 104872 | nnz_cache[scan8[1 + 12]] = | |
2414 | 104872 | nnz_cache[scan8[2 + 12]] = | |
2415 | 104872 | nnz_cache[scan8[3 + 12]] = (sl->cbp & 0x8000) >> 12; | |
2416 | } | ||
2417 | } | ||
2418 | |||
2419 | 8105112 | return 0; | |
2420 | } | ||
2421 | |||
2422 | 403185 | static void loop_filter(const H264Context *h, H264SliceContext *sl, int start_x, int end_x) | |
2423 | { | ||
2424 | uint8_t *dest_y, *dest_cb, *dest_cr; | ||
2425 | int linesize, uvlinesize, mb_x, mb_y; | ||
2426 | 403185 | const int end_mb_y = sl->mb_y + FRAME_MBAFF(h); | |
2427 | 403185 | const int old_slice_type = sl->slice_type; | |
2428 | 403185 | const int pixel_shift = h->pixel_shift; | |
2429 | 403185 | const int block_h = 16 >> h->chroma_y_shift; | |
2430 | |||
2431 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 403185 times.
|
403185 | if (h->postpone_filter) |
2432 | ✗ | return; | |
2433 | |||
2434 |
2/2✓ Branch 0 taken 326217 times.
✓ Branch 1 taken 76968 times.
|
403185 | if (sl->deblocking_filter) { |
2435 |
2/2✓ Branch 0 taken 11569240 times.
✓ Branch 1 taken 326217 times.
|
11895457 | for (mb_x = start_x; mb_x < end_x; mb_x++) |
2436 |
2/2✓ Branch 0 taken 12580480 times.
✓ Branch 1 taken 11569240 times.
|
24149720 | for (mb_y = end_mb_y - FRAME_MBAFF(h); mb_y <= end_mb_y; mb_y++) { |
2437 | int mb_xy, mb_type; | ||
2438 | 12580480 | mb_xy = sl->mb_xy = mb_x + mb_y * h->mb_stride; | |
2439 | 12580480 | mb_type = h->cur_pic.mb_type[mb_xy]; | |
2440 | |||
2441 |
2/2✓ Branch 0 taken 2022480 times.
✓ Branch 1 taken 10558000 times.
|
12580480 | if (FRAME_MBAFF(h)) |
2442 | 2022480 | sl->mb_mbaff = | |
2443 | 2022480 | sl->mb_field_decoding_flag = !!IS_INTERLACED(mb_type); | |
2444 | |||
2445 | 12580480 | sl->mb_x = mb_x; | |
2446 | 12580480 | sl->mb_y = mb_y; | |
2447 | 12580480 | dest_y = h->cur_pic.f->data[0] + | |
2448 | 12580480 | ((mb_x << pixel_shift) + mb_y * sl->linesize) * 16; | |
2449 | 25160960 | dest_cb = h->cur_pic.f->data[1] + | |
2450 |
2/2✓ Branch 0 taken 80328 times.
✓ Branch 1 taken 12500152 times.
|
12580480 | (mb_x << pixel_shift) * (8 << CHROMA444(h)) + |
2451 | 12580480 | mb_y * sl->uvlinesize * block_h; | |
2452 | 25160960 | dest_cr = h->cur_pic.f->data[2] + | |
2453 |
2/2✓ Branch 0 taken 80328 times.
✓ Branch 1 taken 12500152 times.
|
12580480 | (mb_x << pixel_shift) * (8 << CHROMA444(h)) + |
2454 | 12580480 | mb_y * sl->uvlinesize * block_h; | |
2455 | // FIXME simplify above | ||
2456 | |||
2457 |
2/2✓ Branch 0 taken 4649958 times.
✓ Branch 1 taken 7930522 times.
|
12580480 | if (MB_FIELD(sl)) { |
2458 | 4649958 | linesize = sl->mb_linesize = sl->linesize * 2; | |
2459 | 4649958 | uvlinesize = sl->mb_uvlinesize = sl->uvlinesize * 2; | |
2460 |
2/2✓ Branch 0 taken 2322240 times.
✓ Branch 1 taken 2327718 times.
|
4649958 | if (mb_y & 1) { // FIXME move out of this function? |
2461 | 2322240 | dest_y -= sl->linesize * 15; | |
2462 | 2322240 | dest_cb -= sl->uvlinesize * (block_h - 1); | |
2463 | 2322240 | dest_cr -= sl->uvlinesize * (block_h - 1); | |
2464 | } | ||
2465 | } else { | ||
2466 | 7930522 | linesize = sl->mb_linesize = sl->linesize; | |
2467 | 7930522 | uvlinesize = sl->mb_uvlinesize = sl->uvlinesize; | |
2468 | } | ||
2469 | 12580480 | backup_mb_border(h, sl, dest_y, dest_cb, dest_cr, linesize, | |
2470 | uvlinesize, 0); | ||
2471 |
2/2✓ Branch 1 taken 1490305 times.
✓ Branch 2 taken 11090175 times.
|
12580480 | if (fill_filter_caches(h, sl, mb_type)) |
2472 | 1490305 | continue; | |
2473 | 11090175 | sl->chroma_qp[0] = get_chroma_qp(h->ps.pps, 0, h->cur_pic.qscale_table[mb_xy]); | |
2474 | 11090175 | sl->chroma_qp[1] = get_chroma_qp(h->ps.pps, 1, h->cur_pic.qscale_table[mb_xy]); | |
2475 | |||
2476 |
2/2✓ Branch 0 taken 1730309 times.
✓ Branch 1 taken 9359866 times.
|
11090175 | if (FRAME_MBAFF(h)) { |
2477 | 1730309 | ff_h264_filter_mb(h, sl, mb_x, mb_y, dest_y, dest_cb, dest_cr, | |
2478 | linesize, uvlinesize); | ||
2479 | } else { | ||
2480 | 9359866 | ff_h264_filter_mb_fast(h, sl, mb_x, mb_y, dest_y, dest_cb, | |
2481 | dest_cr, linesize, uvlinesize); | ||
2482 | } | ||
2483 | } | ||
2484 | } | ||
2485 | 403185 | sl->slice_type = old_slice_type; | |
2486 | 403185 | sl->mb_x = end_x; | |
2487 | 403185 | sl->mb_y = end_mb_y - FRAME_MBAFF(h); | |
2488 | 403185 | sl->chroma_qp[0] = get_chroma_qp(h->ps.pps, 0, sl->qscale); | |
2489 | 403185 | sl->chroma_qp[1] = get_chroma_qp(h->ps.pps, 1, sl->qscale); | |
2490 | } | ||
2491 | |||
2492 | 24060 | static void predict_field_decoding_flag(const H264Context *h, H264SliceContext *sl) | |
2493 | { | ||
2494 | 24060 | const int mb_xy = sl->mb_x + sl->mb_y * h->mb_stride; | |
2495 | 48120 | int mb_type = (h->slice_table[mb_xy - 1] == sl->slice_num) ? | |
2496 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 24060 times.
|
48120 | h->cur_pic.mb_type[mb_xy - 1] : |
2497 |
2/2✓ Branch 0 taken 23924 times.
✓ Branch 1 taken 136 times.
|
24060 | (h->slice_table[mb_xy - h->mb_stride] == sl->slice_num) ? |
2498 | 23924 | h->cur_pic.mb_type[mb_xy - h->mb_stride] : 0; | |
2499 | 24060 | sl->mb_mbaff = sl->mb_field_decoding_flag = IS_INTERLACED(mb_type) ? 1 : 0; | |
2500 | 24060 | } | |
2501 | |||
2502 | /** | ||
2503 | * Draw edges and report progress for the last MB row. | ||
2504 | */ | ||
2505 | 398625 | static void decode_finish_row(const H264Context *h, H264SliceContext *sl) | |
2506 | { | ||
2507 | 398625 | int top = 16 * (sl->mb_y >> FIELD_PICTURE(h)); | |
2508 | 398625 | int pic_height = 16 * h->mb_height >> FIELD_PICTURE(h); | |
2509 | 398625 | int height = 16 << FRAME_MBAFF(h); | |
2510 | 398625 | int deblock_border = (16 + 4) << FRAME_MBAFF(h); | |
2511 | |||
2512 |
2/2✓ Branch 0 taken 323125 times.
✓ Branch 1 taken 75500 times.
|
398625 | if (sl->deblocking_filter) { |
2513 |
2/2✓ Branch 0 taken 22450 times.
✓ Branch 1 taken 300675 times.
|
323125 | if ((top + height) >= pic_height) |
2514 | 22450 | height += deblock_border; | |
2515 | 323125 | top -= deblock_border; | |
2516 | } | ||
2517 | |||
2518 |
3/4✓ Branch 0 taken 398625 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 22451 times.
✓ Branch 3 taken 376174 times.
|
398625 | if (top >= pic_height || (top + height) < 0) |
2519 | 22451 | return; | |
2520 | |||
2521 | 376174 | height = FFMIN(height, pic_height - top); | |
2522 |
2/2✓ Branch 0 taken 22451 times.
✓ Branch 1 taken 353723 times.
|
376174 | if (top < 0) { |
2523 | 22451 | height = top + height; | |
2524 | 22451 | top = 0; | |
2525 | } | ||
2526 | |||
2527 | 376174 | ff_h264_draw_horiz_band(h, sl, top, height); | |
2528 | |||
2529 |
3/4✓ Branch 0 taken 242613 times.
✓ Branch 1 taken 133561 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 242613 times.
|
376174 | if (h->droppable || h->er.error_occurred) |
2530 | 133561 | return; | |
2531 | |||
2532 | 242613 | ff_thread_report_progress(&h->cur_pic_ptr->tf, top + height - 1, | |
2533 | 242613 | h->picture_structure == PICT_BOTTOM_FIELD); | |
2534 | } | ||
2535 | |||
2536 | 37376 | static void er_add_slice(H264SliceContext *sl, | |
2537 | int startx, int starty, | ||
2538 | int endx, int endy, int status) | ||
2539 | { | ||
2540 |
2/2✓ Branch 0 taken 1200 times.
✓ Branch 1 taken 36176 times.
|
37376 | if (!sl->h264->enable_er) |
2541 | 1200 | return; | |
2542 | |||
2543 | if (CONFIG_ERROR_RESILIENCE) { | ||
2544 | 36176 | ff_er_add_slice(sl->er, startx, starty, endx, endy, status); | |
2545 | } | ||
2546 | } | ||
2547 | |||
2548 | 37376 | static int decode_slice(struct AVCodecContext *avctx, void *arg) | |
2549 | { | ||
2550 | 37376 | H264SliceContext *sl = arg; | |
2551 | 37376 | const H264Context *h = sl->h264; | |
2552 | 37376 | int lf_x_start = sl->mb_x; | |
2553 | 37376 | int orig_deblock = sl->deblocking_filter; | |
2554 | int ret; | ||
2555 | |||
2556 | 37376 | sl->linesize = h->cur_pic_ptr->f->linesize[0]; | |
2557 | 37376 | sl->uvlinesize = h->cur_pic_ptr->f->linesize[1]; | |
2558 | |||
2559 | 37376 | ret = alloc_scratch_buffers(sl, sl->linesize); | |
2560 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 37376 times.
|
37376 | if (ret < 0) |
2561 | ✗ | return ret; | |
2562 | |||
2563 | 37376 | sl->mb_skip_run = -1; | |
2564 | |||
2565 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 37376 times.
|
37376 | av_assert0(h->block_offset[15] == (4 * ((scan8[15] - scan8[0]) & 7) << h->pixel_shift) + 4 * sl->linesize * ((scan8[15] - scan8[0]) >> 3)); |
2566 | |||
2567 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 37376 times.
|
37376 | if (h->postpone_filter) |
2568 | ✗ | sl->deblocking_filter = 0; | |
2569 | |||
2570 |
4/4✓ Branch 0 taken 32401 times.
✓ Branch 1 taken 4975 times.
✓ Branch 2 taken 8956 times.
✓ Branch 3 taken 23445 times.
|
37376 | sl->is_complex = FRAME_MBAFF(h) || h->picture_structure != PICT_FRAME || |
2571 | (CONFIG_GRAY && (h->flags & AV_CODEC_FLAG_GRAY)); | ||
2572 | |||
2573 |
5/6✓ Branch 0 taken 36176 times.
✓ Branch 1 taken 1200 times.
✓ Branch 2 taken 27220 times.
✓ Branch 3 taken 8956 times.
✓ Branch 4 taken 27220 times.
✗ Branch 5 not taken.
|
37376 | if (!(h->avctx->active_thread_type & FF_THREAD_SLICE) && h->picture_structure == PICT_FRAME && sl->er->error_status_table) { |
2574 | 27220 | const int start_i = av_clip(sl->resync_mb_x + sl->resync_mb_y * h->mb_width, 0, h->mb_num - 1); | |
2575 |
2/2✓ Branch 0 taken 5956 times.
✓ Branch 1 taken 21264 times.
|
27220 | if (start_i) { |
2576 | 5956 | int prev_status = sl->er->error_status_table[sl->er->mb_index2xy[start_i - 1]]; | |
2577 | 5956 | prev_status &= ~ VP_START; | |
2578 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 5956 times.
|
5956 | if (prev_status != (ER_MV_END | ER_DC_END | ER_AC_END)) |
2579 | ✗ | sl->er->error_occurred = 1; | |
2580 | } | ||
2581 | } | ||
2582 | |||
2583 |
2/2✓ Branch 0 taken 20635 times.
✓ Branch 1 taken 16741 times.
|
37376 | if (h->ps.pps->cabac) { |
2584 | /* realign */ | ||
2585 | 20635 | align_get_bits(&sl->gb); | |
2586 | |||
2587 | /* init cabac */ | ||
2588 | 41270 | ret = ff_init_cabac_decoder(&sl->cabac, | |
2589 | 20635 | sl->gb.buffer + get_bits_count(&sl->gb) / 8, | |
2590 | 20635 | (get_bits_left(&sl->gb) + 7) / 8); | |
2591 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 20635 times.
|
20635 | if (ret < 0) |
2592 | ✗ | return ret; | |
2593 | |||
2594 | 20635 | ff_h264_init_cabac_states(h, sl); | |
2595 | |||
2596 | 9360242 | for (;;) { | |
2597 | int ret, eos; | ||
2598 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 9380877 times.
|
9380877 | if (sl->mb_x + sl->mb_y * h->mb_width >= sl->next_slice_idx) { |
2599 | ✗ | av_log(h->avctx, AV_LOG_ERROR, "Slice overlaps with next at %d\n", | |
2600 | sl->next_slice_idx); | ||
2601 | ✗ | er_add_slice(sl, sl->resync_mb_x, sl->resync_mb_y, sl->mb_x, | |
2602 | sl->mb_y, ER_MB_ERROR); | ||
2603 | ✗ | return AVERROR_INVALIDDATA; | |
2604 | } | ||
2605 | |||
2606 | 9380877 | ret = ff_h264_decode_mb_cabac(h, sl); | |
2607 | |||
2608 |
2/2✓ Branch 0 taken 9380876 times.
✓ Branch 1 taken 1 times.
|
9380877 | if (ret >= 0) |
2609 | 9380876 | ff_h264_hl_decode_mb(h, sl); | |
2610 | |||
2611 | // FIXME optimal? or let mb_decode decode 16x32 ? | ||
2612 |
4/4✓ Branch 0 taken 9380876 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 902892 times.
✓ Branch 3 taken 8477984 times.
|
9380877 | if (ret >= 0 && FRAME_MBAFF(h)) { |
2613 | 902892 | sl->mb_y++; | |
2614 | |||
2615 | 902892 | ret = ff_h264_decode_mb_cabac(h, sl); | |
2616 | |||
2617 |
1/2✓ Branch 0 taken 902892 times.
✗ Branch 1 not taken.
|
902892 | if (ret >= 0) |
2618 | 902892 | ff_h264_hl_decode_mb(h, sl); | |
2619 | 902892 | sl->mb_y--; | |
2620 | } | ||
2621 | 9380877 | eos = get_cabac_terminate(&sl->cabac); | |
2622 | |||
2623 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 9380877 times.
|
9380877 | if ((h->workaround_bugs & FF_BUG_TRUNCATED) && |
2624 | ✗ | sl->cabac.bytestream > sl->cabac.bytestream_end + 2) { | |
2625 | ✗ | er_add_slice(sl, sl->resync_mb_x, sl->resync_mb_y, sl->mb_x - 1, | |
2626 | sl->mb_y, ER_MB_END); | ||
2627 | ✗ | if (sl->mb_x >= lf_x_start) | |
2628 | ✗ | loop_filter(h, sl, lf_x_start, sl->mb_x + 1); | |
2629 | ✗ | goto finish; | |
2630 | } | ||
2631 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 9380876 times.
|
9380877 | if (sl->cabac.bytestream > sl->cabac.bytestream_end + 2 ) |
2632 | 1 | av_log(h->avctx, AV_LOG_DEBUG, "bytestream overread %"PTRDIFF_SPECIFIER"\n", sl->cabac.bytestream_end - sl->cabac.bytestream); | |
2633 |
4/4✓ Branch 0 taken 9380876 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 9380875 times.
|
9380877 | if (ret < 0 || sl->cabac.bytestream > sl->cabac.bytestream_end + 4) { |
2634 | 2 | av_log(h->avctx, AV_LOG_ERROR, | |
2635 | "error while decoding MB %d %d, bytestream %"PTRDIFF_SPECIFIER"\n", | ||
2636 | sl->mb_x, sl->mb_y, | ||
2637 | 2 | sl->cabac.bytestream_end - sl->cabac.bytestream); | |
2638 | 2 | er_add_slice(sl, sl->resync_mb_x, sl->resync_mb_y, sl->mb_x, | |
2639 | sl->mb_y, ER_MB_ERROR); | ||
2640 | 2 | return AVERROR_INVALIDDATA; | |
2641 | } | ||
2642 | |||
2643 |
2/2✓ Branch 0 taken 237661 times.
✓ Branch 1 taken 9143214 times.
|
9380875 | if (++sl->mb_x >= h->mb_width) { |
2644 | 237661 | loop_filter(h, sl, lf_x_start, sl->mb_x); | |
2645 | 237661 | sl->mb_x = lf_x_start = 0; | |
2646 | 237661 | decode_finish_row(h, sl); | |
2647 | 237661 | ++sl->mb_y; | |
2648 |
4/4✓ Branch 0 taken 216749 times.
✓ Branch 1 taken 20912 times.
✓ Branch 2 taken 59264 times.
✓ Branch 3 taken 157485 times.
|
237661 | if (FIELD_OR_MBAFF_PICTURE(h)) { |
2649 | 80176 | ++sl->mb_y; | |
2650 |
4/4✓ Branch 0 taken 20912 times.
✓ Branch 1 taken 59264 times.
✓ Branch 2 taken 19082 times.
✓ Branch 3 taken 1830 times.
|
80176 | if (FRAME_MBAFF(h) && sl->mb_y < h->mb_height) |
2651 | 19082 | predict_field_decoding_flag(h, sl); | |
2652 | } | ||
2653 | } | ||
2654 | |||
2655 |
3/4✓ Branch 0 taken 9360242 times.
✓ Branch 1 taken 20633 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 9360242 times.
|
9380875 | if (eos || sl->mb_y >= h->mb_height) { |
2656 | ff_tlog(h->avctx, "slice end %d %d\n", | ||
2657 | get_bits_count(&sl->gb), sl->gb.size_in_bits); | ||
2658 | 20633 | er_add_slice(sl, sl->resync_mb_x, sl->resync_mb_y, sl->mb_x - 1, | |
2659 | sl->mb_y, ER_MB_END); | ||
2660 |
2/2✓ Branch 0 taken 1382 times.
✓ Branch 1 taken 19251 times.
|
20633 | if (sl->mb_x > lf_x_start) |
2661 | 1382 | loop_filter(h, sl, lf_x_start, sl->mb_x); | |
2662 | 20633 | goto finish; | |
2663 | } | ||
2664 | } | ||
2665 | } else { | ||
2666 | 4575657 | for (;;) { | |
2667 | int ret; | ||
2668 | |||
2669 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 4592398 times.
|
4592398 | if (sl->mb_x + sl->mb_y * h->mb_width >= sl->next_slice_idx) { |
2670 | ✗ | av_log(h->avctx, AV_LOG_ERROR, "Slice overlaps with next at %d\n", | |
2671 | sl->next_slice_idx); | ||
2672 | ✗ | er_add_slice(sl, sl->resync_mb_x, sl->resync_mb_y, sl->mb_x, | |
2673 | sl->mb_y, ER_MB_ERROR); | ||
2674 | ✗ | return AVERROR_INVALIDDATA; | |
2675 | } | ||
2676 | |||
2677 | 4592398 | ret = ff_h264_decode_mb_cavlc(h, sl); | |
2678 | |||
2679 |
1/2✓ Branch 0 taken 4592398 times.
✗ Branch 1 not taken.
|
4592398 | if (ret >= 0) |
2680 | 4592398 | ff_h264_hl_decode_mb(h, sl); | |
2681 | |||
2682 | // FIXME optimal? or let mb_decode decode 16x32 ? | ||
2683 |
3/4✓ Branch 0 taken 4592398 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 254472 times.
✓ Branch 3 taken 4337926 times.
|
4592398 | if (ret >= 0 && FRAME_MBAFF(h)) { |
2684 | 254472 | sl->mb_y++; | |
2685 | 254472 | ret = ff_h264_decode_mb_cavlc(h, sl); | |
2686 | |||
2687 |
1/2✓ Branch 0 taken 254472 times.
✗ Branch 1 not taken.
|
254472 | if (ret >= 0) |
2688 | 254472 | ff_h264_hl_decode_mb(h, sl); | |
2689 | 254472 | sl->mb_y--; | |
2690 | } | ||
2691 | |||
2692 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 4592398 times.
|
4592398 | if (ret < 0) { |
2693 | ✗ | av_log(h->avctx, AV_LOG_ERROR, | |
2694 | "error while decoding MB %d %d\n", sl->mb_x, sl->mb_y); | ||
2695 | ✗ | er_add_slice(sl, sl->resync_mb_x, sl->resync_mb_y, sl->mb_x, | |
2696 | sl->mb_y, ER_MB_ERROR); | ||
2697 | ✗ | return ret; | |
2698 | } | ||
2699 | |||
2700 |
2/2✓ Branch 0 taken 160964 times.
✓ Branch 1 taken 4431434 times.
|
4592398 | if (++sl->mb_x >= h->mb_width) { |
2701 | 160964 | loop_filter(h, sl, lf_x_start, sl->mb_x); | |
2702 | 160964 | sl->mb_x = lf_x_start = 0; | |
2703 | 160964 | decode_finish_row(h, sl); | |
2704 | 160964 | ++sl->mb_y; | |
2705 |
4/4✓ Branch 0 taken 155487 times.
✓ Branch 1 taken 5477 times.
✓ Branch 2 taken 26666 times.
✓ Branch 3 taken 128821 times.
|
160964 | if (FIELD_OR_MBAFF_PICTURE(h)) { |
2706 | 32143 | ++sl->mb_y; | |
2707 |
4/4✓ Branch 0 taken 5477 times.
✓ Branch 1 taken 26666 times.
✓ Branch 2 taken 4978 times.
✓ Branch 3 taken 499 times.
|
32143 | if (FRAME_MBAFF(h) && sl->mb_y < h->mb_height) |
2708 | 4978 | predict_field_decoding_flag(h, sl); | |
2709 | } | ||
2710 |
2/2✓ Branch 0 taken 12651 times.
✓ Branch 1 taken 148313 times.
|
160964 | if (sl->mb_y >= h->mb_height) { |
2711 | ff_tlog(h->avctx, "slice end %d %d\n", | ||
2712 | get_bits_count(&sl->gb), sl->gb.size_in_bits); | ||
2713 | |||
2714 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 12651 times.
|
12651 | if ( get_bits_left(&sl->gb) == 0 |
2715 | ✗ | || get_bits_left(&sl->gb) > 0 && !(h->avctx->err_recognition & AV_EF_AGGRESSIVE)) { | |
2716 | 12651 | er_add_slice(sl, sl->resync_mb_x, sl->resync_mb_y, | |
2717 | 12651 | sl->mb_x - 1, sl->mb_y, ER_MB_END); | |
2718 | |||
2719 | 12651 | goto finish; | |
2720 | } else { | ||
2721 | ✗ | er_add_slice(sl, sl->resync_mb_x, sl->resync_mb_y, | |
2722 | sl->mb_x, sl->mb_y, ER_MB_END); | ||
2723 | |||
2724 | ✗ | return AVERROR_INVALIDDATA; | |
2725 | } | ||
2726 | } | ||
2727 | } | ||
2728 | |||
2729 |
4/4✓ Branch 1 taken 238513 times.
✓ Branch 2 taken 4341234 times.
✓ Branch 3 taken 4090 times.
✓ Branch 4 taken 234423 times.
|
4579747 | if (get_bits_left(&sl->gb) <= 0 && sl->mb_skip_run <= 0) { |
2730 | ff_tlog(h->avctx, "slice end %d %d\n", | ||
2731 | get_bits_count(&sl->gb), sl->gb.size_in_bits); | ||
2732 | |||
2733 |
1/2✓ Branch 1 taken 4090 times.
✗ Branch 2 not taken.
|
4090 | if (get_bits_left(&sl->gb) == 0) { |
2734 | 4090 | er_add_slice(sl, sl->resync_mb_x, sl->resync_mb_y, | |
2735 | 4090 | sl->mb_x - 1, sl->mb_y, ER_MB_END); | |
2736 |
2/2✓ Branch 0 taken 3178 times.
✓ Branch 1 taken 912 times.
|
4090 | if (sl->mb_x > lf_x_start) |
2737 | 3178 | loop_filter(h, sl, lf_x_start, sl->mb_x); | |
2738 | |||
2739 | 4090 | goto finish; | |
2740 | } else { | ||
2741 | ✗ | er_add_slice(sl, sl->resync_mb_x, sl->resync_mb_y, sl->mb_x, | |
2742 | sl->mb_y, ER_MB_ERROR); | ||
2743 | |||
2744 | ✗ | return AVERROR_INVALIDDATA; | |
2745 | } | ||
2746 | } | ||
2747 | } | ||
2748 | } | ||
2749 | |||
2750 | 37374 | finish: | |
2751 | 37374 | sl->deblocking_filter = orig_deblock; | |
2752 | 37374 | return 0; | |
2753 | } | ||
2754 | |||
2755 | /** | ||
2756 | * Call decode_slice() for each context. | ||
2757 | * | ||
2758 | * @param h h264 master context | ||
2759 | */ | ||
2760 | 65781 | int ff_h264_execute_decode_slices(H264Context *h) | |
2761 | { | ||
2762 | 65781 | AVCodecContext *const avctx = h->avctx; | |
2763 | H264SliceContext *sl; | ||
2764 | 65781 | int context_count = h->nb_slice_ctx_queued; | |
2765 | 65781 | int ret = 0; | |
2766 | int i, j; | ||
2767 | |||
2768 | 65781 | h->slice_ctx[0].next_slice_idx = INT_MAX; | |
2769 | |||
2770 |
3/4✓ Branch 0 taken 65781 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 28855 times.
✓ Branch 3 taken 36926 times.
|
65781 | if (h->avctx->hwaccel || context_count < 1) |
2771 | 28855 | return 0; | |
2772 | |||
2773 |
2/4✓ Branch 0 taken 36926 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 36926 times.
|
36926 | av_assert0(context_count && h->slice_ctx[context_count - 1].mb_y < h->mb_height); |
2774 | |||
2775 |
2/2✓ Branch 0 taken 36476 times.
✓ Branch 1 taken 450 times.
|
36926 | if (context_count == 1) { |
2776 | |||
2777 | 36476 | h->slice_ctx[0].next_slice_idx = h->mb_width * h->mb_height; | |
2778 | 36476 | h->postpone_filter = 0; | |
2779 | |||
2780 | 36476 | ret = decode_slice(avctx, &h->slice_ctx[0]); | |
2781 | 36476 | h->mb_y = h->slice_ctx[0].mb_y; | |
2782 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 36474 times.
|
36476 | if (ret < 0) |
2783 | 2 | goto finish; | |
2784 | } else { | ||
2785 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 450 times.
|
450 | av_assert0(context_count > 0); |
2786 |
2/2✓ Branch 0 taken 900 times.
✓ Branch 1 taken 450 times.
|
1350 | for (i = 0; i < context_count; i++) { |
2787 | 900 | int next_slice_idx = h->mb_width * h->mb_height; | |
2788 | int slice_idx; | ||
2789 | |||
2790 | 900 | sl = &h->slice_ctx[i]; | |
2791 | |||
2792 | /* make sure none of those slices overlap */ | ||
2793 | 900 | slice_idx = sl->mb_y * h->mb_width + sl->mb_x; | |
2794 |
2/2✓ Branch 0 taken 1800 times.
✓ Branch 1 taken 900 times.
|
2700 | for (j = 0; j < context_count; j++) { |
2795 | 1800 | H264SliceContext *sl2 = &h->slice_ctx[j]; | |
2796 | 1800 | int slice_idx2 = sl2->mb_y * h->mb_width + sl2->mb_x; | |
2797 | |||
2798 |
4/4✓ Branch 0 taken 900 times.
✓ Branch 1 taken 900 times.
✓ Branch 2 taken 450 times.
✓ Branch 3 taken 450 times.
|
1800 | if (i == j || slice_idx2 < slice_idx) |
2799 | 1350 | continue; | |
2800 | 450 | next_slice_idx = FFMIN(next_slice_idx, slice_idx2); | |
2801 | } | ||
2802 | 900 | sl->next_slice_idx = next_slice_idx; | |
2803 | } | ||
2804 | |||
2805 | 450 | avctx->execute(avctx, decode_slice, h->slice_ctx, | |
2806 | NULL, context_count, sizeof(h->slice_ctx[0])); | ||
2807 | |||
2808 | /* pull back stuff from slices to master context */ | ||
2809 | 450 | sl = &h->slice_ctx[context_count - 1]; | |
2810 | 450 | h->mb_y = sl->mb_y; | |
2811 | |||
2812 |
1/2✓ Branch 0 taken 450 times.
✗ Branch 1 not taken.
|
450 | if (h->postpone_filter) { |
2813 | ✗ | h->postpone_filter = 0; | |
2814 | |||
2815 | ✗ | for (i = 0; i < context_count; i++) { | |
2816 | int y_end, x_end; | ||
2817 | |||
2818 | ✗ | sl = &h->slice_ctx[i]; | |
2819 | ✗ | y_end = FFMIN(sl->mb_y + 1, h->mb_height); | |
2820 | ✗ | x_end = (sl->mb_y >= h->mb_height) ? h->mb_width : sl->mb_x; | |
2821 | |||
2822 | ✗ | for (j = sl->resync_mb_y; j < y_end; j += 1 + FIELD_OR_MBAFF_PICTURE(h)) { | |
2823 | ✗ | sl->mb_y = j; | |
2824 | ✗ | loop_filter(h, sl, j > sl->resync_mb_y ? 0 : sl->resync_mb_x, | |
2825 | ✗ | j == y_end - 1 ? x_end : h->mb_width); | |
2826 | } | ||
2827 | } | ||
2828 | } | ||
2829 | } | ||
2830 | |||
2831 | 450 | finish: | |
2832 | 36926 | h->nb_slice_ctx_queued = 0; | |
2833 | 36926 | return ret; | |
2834 | } | ||
2835 |