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