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