Commit | Line | Data |
---|---|---|
0da71265 | 1 | /* |
ff3d4310 | 2 | * H.26L/H.264/AVC/JVT/14496-10/... decoder |
0da71265 MN |
3 | * Copyright (c) 2003 Michael Niedermayer <michaelni@gmx.at> |
4 | * | |
2912e87a | 5 | * This file is part of Libav. |
b78e7197 | 6 | * |
2912e87a | 7 | * Libav is free software; you can redistribute it and/or |
0da71265 MN |
8 | * modify it under the terms of the GNU Lesser General Public |
9 | * License as published by the Free Software Foundation; either | |
b78e7197 | 10 | * version 2.1 of the License, or (at your option) any later version. |
0da71265 | 11 | * |
2912e87a | 12 | * Libav is distributed in the hope that it will be useful, |
0da71265 MN |
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 | |
2912e87a | 18 | * License along with Libav; if not, write to the Free Software |
5509bffa | 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
0da71265 | 20 | */ |
115329f1 | 21 | |
0da71265 | 22 | /** |
ba87f080 | 23 | * @file |
0da71265 MN |
24 | * H.264 / AVC / MPEG4 part10 codec. |
25 | * @author Michael Niedermayer <michaelni@gmx.at> | |
26 | */ | |
27 | ||
737eb597 | 28 | #include "libavutil/imgutils.h" |
40e5d31b | 29 | #include "internal.h" |
55b9ef18 DB |
30 | #include "cabac.h" |
31 | #include "cabac_functions.h" | |
0da71265 | 32 | #include "dsputil.h" |
5f401b7b | 33 | #include "error_resilience.h" |
0da71265 MN |
34 | #include "avcodec.h" |
35 | #include "mpegvideo.h" | |
26b4fe82 | 36 | #include "h264.h" |
0da71265 | 37 | #include "h264data.h" |
79dad2a9 | 38 | #include "h264chroma.h" |
188d3c51 | 39 | #include "h264_mvpred.h" |
0da71265 | 40 | #include "golomb.h" |
199436b9 | 41 | #include "mathops.h" |
626464fb | 42 | #include "rectangle.h" |
75d5156a | 43 | #include "svq3.h" |
6a9c8594 | 44 | #include "thread.h" |
369122dd | 45 | #include "vdpau_internal.h" |
cfa5a81e | 46 | #include "libavutil/avassert.h" |
0da71265 | 47 | |
e5d40372 | 48 | // #undef NDEBUG |
0da71265 MN |
49 | #include <assert.h> |
50 | ||
0becb078 DB |
51 | const uint16_t ff_h264_mb_sizes[4] = { 256, 384, 512, 768 }; |
52 | ||
e5d40372 DB |
53 | static const uint8_t rem6[QP_MAX_NUM + 1] = { |
54 | 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, | |
55 | 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, | |
56 | 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, | |
acd8d10f PI |
57 | }; |
58 | ||
e5d40372 DB |
59 | static const uint8_t div6[QP_MAX_NUM + 1] = { |
60 | 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 3, 3, 3, | |
61 | 3, 3, 3, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6, | |
62 | 7, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 10, 10, 10, 10, | |
acd8d10f PI |
63 | }; |
64 | ||
8d061989 RB |
65 | static const enum AVPixelFormat h264_hwaccel_pixfmt_list_420[] = { |
66 | #if CONFIG_H264_DXVA2_HWACCEL | |
67 | AV_PIX_FMT_DXVA2_VLD, | |
68 | #endif | |
69 | #if CONFIG_H264_VAAPI_HWACCEL | |
70 | AV_PIX_FMT_VAAPI_VLD, | |
71 | #endif | |
72 | #if CONFIG_H264_VDA_HWACCEL | |
73 | AV_PIX_FMT_VDA_VLD, | |
74 | #endif | |
75 | #if CONFIG_H264_VDPAU_HWACCEL | |
76 | AV_PIX_FMT_VDPAU, | |
77 | #endif | |
78 | AV_PIX_FMT_YUV420P, | |
79 | AV_PIX_FMT_NONE | |
80 | }; | |
81 | ||
d65522e8 | 82 | static const enum AVPixelFormat h264_hwaccel_pixfmt_list_jpeg_420[] = { |
78bc4d69 | 83 | #if CONFIG_H264_DXVA2_HWACCEL |
716d413c | 84 | AV_PIX_FMT_DXVA2_VLD, |
78bc4d69 RDC |
85 | #endif |
86 | #if CONFIG_H264_VAAPI_HWACCEL | |
716d413c | 87 | AV_PIX_FMT_VAAPI_VLD, |
78bc4d69 RDC |
88 | #endif |
89 | #if CONFIG_H264_VDA_HWACCEL | |
716d413c | 90 | AV_PIX_FMT_VDA_VLD, |
78bc4d69 RDC |
91 | #endif |
92 | #if CONFIG_H264_VDPAU_HWACCEL | |
ec0e9200 | 93 | AV_PIX_FMT_VDPAU, |
78bc4d69 | 94 | #endif |
716d413c AK |
95 | AV_PIX_FMT_YUVJ420P, |
96 | AV_PIX_FMT_NONE | |
0435fb16 BC |
97 | }; |
98 | ||
54974c62 AK |
99 | static void h264_er_decode_mb(void *opaque, int ref, int mv_dir, int mv_type, |
100 | int (*mv)[2][4][2], | |
101 | int mb_x, int mb_y, int mb_intra, int mb_skipped) | |
102 | { | |
103 | H264Context *h = opaque; | |
54974c62 | 104 | |
2c541554 AK |
105 | h->mb_x = mb_x; |
106 | h->mb_y = mb_y; | |
107 | h->mb_xy = mb_x + mb_y * h->mb_stride; | |
54974c62 AK |
108 | memset(h->non_zero_count_cache, 0, sizeof(h->non_zero_count_cache)); |
109 | assert(ref >= 0); | |
110 | /* FIXME: It is possible albeit uncommon that slice references | |
111 | * differ between slices. We take the easy approach and ignore | |
112 | * it for now. If this turns out to have any relevance in | |
113 | * practice then correct remapping should be added. */ | |
114 | if (ref >= h->ref_count[0]) | |
115 | ref = 0; | |
759001c5 | 116 | fill_rectangle(&h->cur_pic.ref_index[0][4 * h->mb_xy], |
54974c62 AK |
117 | 2, 2, 2, ref, 1); |
118 | fill_rectangle(&h->ref_cache[0][scan8[0]], 4, 4, 8, ref, 1); | |
119 | fill_rectangle(h->mv_cache[0][scan8[0]], 4, 4, 8, | |
2c541554 | 120 | pack16to32((*mv)[0][0][0], (*mv)[0][0][1]), 4); |
7bece9b2 | 121 | assert(!FRAME_MBAFF(h)); |
54974c62 AK |
122 | ff_h264_hl_decode_mb(h); |
123 | } | |
124 | ||
2c541554 AK |
125 | void ff_h264_draw_horiz_band(H264Context *h, int y, int height) |
126 | { | |
64e43869 RB |
127 | AVCodecContext *avctx = h->avctx; |
128 | Picture *cur = &h->cur_pic; | |
129 | Picture *last = h->ref_list[0][0].f.data[0] ? &h->ref_list[0][0] : NULL; | |
130 | const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(avctx->pix_fmt); | |
131 | int vshift = desc->log2_chroma_h; | |
132 | const int field_pic = h->picture_structure != PICT_FRAME; | |
133 | if (field_pic) { | |
134 | height <<= 1; | |
135 | y <<= 1; | |
136 | } | |
137 | ||
138 | height = FFMIN(height, avctx->height - y); | |
139 | ||
140 | if (field_pic && h->first_field && !(avctx->slice_flags & SLICE_FLAG_ALLOW_FIELD)) | |
141 | return; | |
142 | ||
143 | if (avctx->draw_horiz_band) { | |
144 | AVFrame *src; | |
145 | int offset[AV_NUM_DATA_POINTERS]; | |
146 | int i; | |
147 | ||
148 | if (cur->f.pict_type == AV_PICTURE_TYPE_B || h->low_delay || | |
149 | (avctx->slice_flags & SLICE_FLAG_CODED_ORDER)) | |
150 | src = &cur->f; | |
151 | else if (last) | |
152 | src = &last->f; | |
153 | else | |
154 | return; | |
155 | ||
156 | offset[0] = y * src->linesize[0]; | |
157 | offset[1] = | |
158 | offset[2] = (y >> vshift) * src->linesize[1]; | |
159 | for (i = 3; i < AV_NUM_DATA_POINTERS; i++) | |
160 | offset[i] = 0; | |
161 | ||
162 | emms_c(); | |
163 | ||
164 | avctx->draw_horiz_band(avctx, src, offset, | |
165 | y, h->picture_structure, height); | |
166 | } | |
2c541554 AK |
167 | } |
168 | ||
759001c5 | 169 | static void unref_picture(H264Context *h, Picture *pic) |
2c541554 | 170 | { |
759001c5 | 171 | int off = offsetof(Picture, tf) + sizeof(pic->tf); |
2c541554 AK |
172 | int i; |
173 | ||
759001c5 AK |
174 | if (!pic->f.data[0]) |
175 | return; | |
176 | ||
177 | ff_thread_release_buffer(h->avctx, &pic->tf); | |
178 | av_buffer_unref(&pic->hwaccel_priv_buf); | |
2c541554 | 179 | |
759001c5 AK |
180 | av_buffer_unref(&pic->qscale_table_buf); |
181 | av_buffer_unref(&pic->mb_type_buf); | |
2c541554 | 182 | for (i = 0; i < 2; i++) { |
759001c5 AK |
183 | av_buffer_unref(&pic->motion_val_buf[i]); |
184 | av_buffer_unref(&pic->ref_index_buf[i]); | |
2c541554 | 185 | } |
759001c5 AK |
186 | |
187 | memset((uint8_t*)pic + off, 0, sizeof(*pic) - off); | |
2c541554 AK |
188 | } |
189 | ||
190 | static void release_unused_pictures(H264Context *h, int remove_current) | |
191 | { | |
192 | int i; | |
193 | ||
194 | /* release non reference frames */ | |
759001c5 AK |
195 | for (i = 0; i < MAX_PICTURE_COUNT; i++) { |
196 | if (h->DPB[i].f.data[0] && !h->DPB[i].reference && | |
2c541554 | 197 | (remove_current || &h->DPB[i] != h->cur_pic_ptr)) { |
759001c5 | 198 | unref_picture(h, &h->DPB[i]); |
2c541554 AK |
199 | } |
200 | } | |
201 | } | |
202 | ||
759001c5 AK |
203 | static int ref_picture(H264Context *h, Picture *dst, Picture *src) |
204 | { | |
205 | int ret, i; | |
206 | ||
207 | av_assert0(!dst->f.buf[0]); | |
208 | av_assert0(src->f.buf[0]); | |
209 | ||
210 | src->tf.f = &src->f; | |
211 | dst->tf.f = &dst->f; | |
212 | ret = ff_thread_ref_frame(&dst->tf, &src->tf); | |
213 | if (ret < 0) | |
214 | goto fail; | |
215 | ||
216 | ||
217 | dst->qscale_table_buf = av_buffer_ref(src->qscale_table_buf); | |
218 | dst->mb_type_buf = av_buffer_ref(src->mb_type_buf); | |
219 | if (!dst->qscale_table_buf || !dst->mb_type_buf) | |
220 | goto fail; | |
221 | dst->qscale_table = src->qscale_table; | |
222 | dst->mb_type = src->mb_type; | |
223 | ||
224 | for (i = 0; i < 2; i ++) { | |
225 | dst->motion_val_buf[i] = av_buffer_ref(src->motion_val_buf[i]); | |
226 | dst->ref_index_buf[i] = av_buffer_ref(src->ref_index_buf[i]); | |
227 | if (!dst->motion_val_buf[i] || !dst->ref_index_buf[i]) | |
228 | goto fail; | |
229 | dst->motion_val[i] = src->motion_val[i]; | |
230 | dst->ref_index[i] = src->ref_index[i]; | |
231 | } | |
232 | ||
233 | if (src->hwaccel_picture_private) { | |
234 | dst->hwaccel_priv_buf = av_buffer_ref(src->hwaccel_priv_buf); | |
235 | if (!dst->hwaccel_priv_buf) | |
236 | goto fail; | |
237 | dst->hwaccel_picture_private = dst->hwaccel_priv_buf->data; | |
238 | } | |
239 | ||
240 | for (i = 0; i < 2; i++) | |
241 | dst->field_poc[i] = src->field_poc[i]; | |
242 | ||
243 | memcpy(dst->ref_poc, src->ref_poc, sizeof(src->ref_poc)); | |
244 | memcpy(dst->ref_count, src->ref_count, sizeof(src->ref_count)); | |
245 | ||
246 | dst->poc = src->poc; | |
247 | dst->frame_num = src->frame_num; | |
248 | dst->mmco_reset = src->mmco_reset; | |
249 | dst->pic_id = src->pic_id; | |
250 | dst->long_ref = src->long_ref; | |
251 | dst->mbaff = src->mbaff; | |
252 | dst->field_picture = src->field_picture; | |
253 | dst->needs_realloc = src->needs_realloc; | |
254 | dst->reference = src->reference; | |
255 | ||
256 | return 0; | |
257 | fail: | |
258 | unref_picture(h, dst); | |
259 | return ret; | |
260 | } | |
261 | ||
262 | ||
2c541554 AK |
263 | static int alloc_scratch_buffers(H264Context *h, int linesize) |
264 | { | |
265 | int alloc_size = FFALIGN(FFABS(linesize) + 32, 32); | |
266 | ||
267 | if (h->bipred_scratchpad) | |
268 | return 0; | |
269 | ||
270 | h->bipred_scratchpad = av_malloc(16 * 6 * alloc_size); | |
271 | // edge emu needs blocksize + filter length - 1 | |
272 | // (= 21x21 for h264) | |
273 | h->edge_emu_buffer = av_mallocz(alloc_size * 2 * 21); | |
274 | h->me.scratchpad = av_mallocz(alloc_size * 2 * 16 * 2); | |
275 | ||
276 | if (!h->bipred_scratchpad || !h->edge_emu_buffer || !h->me.scratchpad) { | |
277 | av_freep(&h->bipred_scratchpad); | |
278 | av_freep(&h->edge_emu_buffer); | |
279 | av_freep(&h->me.scratchpad); | |
280 | return AVERROR(ENOMEM); | |
281 | } | |
282 | ||
283 | h->me.temp = h->me.scratchpad; | |
284 | ||
285 | return 0; | |
286 | } | |
287 | ||
759001c5 | 288 | static int init_table_pools(H264Context *h) |
2c541554 AK |
289 | { |
290 | const int big_mb_num = h->mb_stride * (h->mb_height + 1) + 1; | |
291 | const int mb_array_size = h->mb_stride * h->mb_height; | |
292 | const int b4_stride = h->mb_width * 4 + 1; | |
293 | const int b4_array_size = b4_stride * h->mb_height * 4; | |
759001c5 AK |
294 | |
295 | h->qscale_table_pool = av_buffer_pool_init(big_mb_num + h->mb_stride, | |
296 | av_buffer_allocz); | |
297 | h->mb_type_pool = av_buffer_pool_init((big_mb_num + h->mb_stride) * | |
298 | sizeof(uint32_t), av_buffer_allocz); | |
299 | h->motion_val_pool = av_buffer_pool_init(2 * (b4_array_size + 4) * | |
300 | sizeof(int16_t), av_buffer_allocz); | |
301 | h->ref_index_pool = av_buffer_pool_init(4 * mb_array_size, av_buffer_allocz); | |
302 | ||
303 | if (!h->qscale_table_pool || !h->mb_type_pool || !h->motion_val_pool || | |
304 | !h->ref_index_pool) { | |
305 | av_buffer_pool_uninit(&h->qscale_table_pool); | |
306 | av_buffer_pool_uninit(&h->mb_type_pool); | |
307 | av_buffer_pool_uninit(&h->motion_val_pool); | |
308 | av_buffer_pool_uninit(&h->ref_index_pool); | |
309 | return AVERROR(ENOMEM); | |
310 | } | |
311 | ||
312 | return 0; | |
313 | } | |
314 | ||
315 | static int alloc_picture(H264Context *h, Picture *pic) | |
316 | { | |
2c541554 AK |
317 | int i, ret = 0; |
318 | ||
319 | av_assert0(!pic->f.data[0]); | |
320 | ||
9c9ede44 HL |
321 | pic->tf.f = &pic->f; |
322 | ret = ff_thread_get_buffer(h->avctx, &pic->tf, pic->reference ? | |
323 | AV_GET_BUFFER_FLAG_REF : 0); | |
324 | if (ret < 0) | |
325 | goto fail; | |
326 | ||
327 | h->linesize = pic->f.linesize[0]; | |
328 | h->uvlinesize = pic->f.linesize[1]; | |
329 | ||
2c541554 AK |
330 | if (h->avctx->hwaccel) { |
331 | const AVHWAccel *hwaccel = h->avctx->hwaccel; | |
759001c5 | 332 | av_assert0(!pic->hwaccel_picture_private); |
2c541554 | 333 | if (hwaccel->priv_data_size) { |
759001c5 AK |
334 | pic->hwaccel_priv_buf = av_buffer_allocz(hwaccel->priv_data_size); |
335 | if (!pic->hwaccel_priv_buf) | |
2c541554 | 336 | return AVERROR(ENOMEM); |
759001c5 | 337 | pic->hwaccel_picture_private = pic->hwaccel_priv_buf->data; |
2c541554 AK |
338 | } |
339 | } | |
2c541554 | 340 | |
759001c5 AK |
341 | if (!h->qscale_table_pool) { |
342 | ret = init_table_pools(h); | |
343 | if (ret < 0) | |
344 | goto fail; | |
345 | } | |
2c541554 | 346 | |
759001c5 AK |
347 | pic->qscale_table_buf = av_buffer_pool_get(h->qscale_table_pool); |
348 | pic->mb_type_buf = av_buffer_pool_get(h->mb_type_pool); | |
349 | if (!pic->qscale_table_buf || !pic->mb_type_buf) | |
350 | goto fail; | |
2c541554 | 351 | |
759001c5 AK |
352 | pic->mb_type = (uint32_t*)pic->mb_type_buf->data + 2 * h->mb_stride + 1; |
353 | pic->qscale_table = pic->qscale_table_buf->data + 2 * h->mb_stride + 1; | |
354 | ||
355 | for (i = 0; i < 2; i++) { | |
356 | pic->motion_val_buf[i] = av_buffer_pool_get(h->motion_val_pool); | |
357 | pic->ref_index_buf[i] = av_buffer_pool_get(h->ref_index_pool); | |
358 | if (!pic->motion_val_buf[i] || !pic->ref_index_buf[i]) | |
359 | goto fail; | |
2c541554 | 360 | |
759001c5 AK |
361 | pic->motion_val[i] = (int16_t (*)[2])pic->motion_val_buf[i]->data + 4; |
362 | pic->ref_index[i] = pic->ref_index_buf[i]->data; | |
363 | } | |
2c541554 AK |
364 | |
365 | return 0; | |
366 | fail: | |
759001c5 | 367 | unref_picture(h, pic); |
2c541554 AK |
368 | return (ret < 0) ? ret : AVERROR(ENOMEM); |
369 | } | |
370 | ||
371 | static inline int pic_is_unused(H264Context *h, Picture *pic) | |
372 | { | |
373 | if (pic->f.data[0] == NULL) | |
374 | return 1; | |
759001c5 AK |
375 | if (pic->needs_realloc && !(pic->reference & DELAYED_PIC_REF)) |
376 | return 1; | |
2c541554 AK |
377 | return 0; |
378 | } | |
379 | ||
380 | static int find_unused_picture(H264Context *h) | |
381 | { | |
382 | int i; | |
383 | ||
759001c5 | 384 | for (i = 0; i < MAX_PICTURE_COUNT; i++) { |
2c541554 AK |
385 | if (pic_is_unused(h, &h->DPB[i])) |
386 | break; | |
387 | } | |
759001c5 | 388 | if (i == MAX_PICTURE_COUNT) |
2c541554 AK |
389 | return AVERROR_INVALIDDATA; |
390 | ||
391 | if (h->DPB[i].needs_realloc) { | |
392 | h->DPB[i].needs_realloc = 0; | |
759001c5 | 393 | unref_picture(h, &h->DPB[i]); |
2c541554 AK |
394 | } |
395 | ||
396 | return i; | |
397 | } | |
398 | ||
0da71265 | 399 | /** |
58c42af7 DB |
400 | * Check if the top & left blocks are available if needed and |
401 | * change the dc mode so it only uses the available blocks. | |
0da71265 | 402 | */ |
e5d40372 DB |
403 | int ff_h264_check_intra4x4_pred_mode(H264Context *h) |
404 | { | |
e5d40372 DB |
405 | static const int8_t top[12] = { |
406 | -1, 0, LEFT_DC_PRED, -1, -1, -1, -1, -1, 0 | |
407 | }; | |
408 | static const int8_t left[12] = { | |
409 | 0, -1, TOP_DC_PRED, 0, -1, -1, -1, 0, -1, DC_128_PRED | |
410 | }; | |
2bedc0e8 MN |
411 | int i; |
412 | ||
e5d40372 DB |
413 | if (!(h->top_samples_available & 0x8000)) { |
414 | for (i = 0; i < 4; i++) { | |
415 | int status = top[h->intra4x4_pred_mode_cache[scan8[0] + i]]; | |
416 | if (status < 0) { | |
2c541554 | 417 | av_log(h->avctx, AV_LOG_ERROR, |
e5d40372 | 418 | "top block unavailable for requested intra4x4 mode %d at %d %d\n", |
2c541554 | 419 | status, h->mb_x, h->mb_y); |
2bedc0e8 | 420 | return -1; |
e5d40372 DB |
421 | } else if (status) { |
422 | h->intra4x4_pred_mode_cache[scan8[0] + i] = status; | |
2bedc0e8 MN |
423 | } |
424 | } | |
425 | } | |
426 | ||
e5d40372 DB |
427 | if ((h->left_samples_available & 0x8888) != 0x8888) { |
428 | static const int mask[4] = { 0x8000, 0x2000, 0x80, 0x20 }; | |
429 | for (i = 0; i < 4; i++) | |
430 | if (!(h->left_samples_available & mask[i])) { | |
431 | int status = left[h->intra4x4_pred_mode_cache[scan8[0] + 8 * i]]; | |
432 | if (status < 0) { | |
2c541554 | 433 | av_log(h->avctx, AV_LOG_ERROR, |
e5d40372 | 434 | "left block unavailable for requested intra4x4 mode %d at %d %d\n", |
2c541554 | 435 | status, h->mb_x, h->mb_y); |
2bedc0e8 | 436 | return -1; |
e5d40372 DB |
437 | } else if (status) { |
438 | h->intra4x4_pred_mode_cache[scan8[0] + 8 * i] = status; | |
2bedc0e8 MN |
439 | } |
440 | } | |
2bedc0e8 MN |
441 | } |
442 | ||
443 | return 0; | |
e5d40372 | 444 | } // FIXME cleanup like ff_h264_check_intra_pred_mode |
2bedc0e8 MN |
445 | |
446 | /** | |
58c42af7 DB |
447 | * Check if the top & left blocks are available if needed and |
448 | * change the dc mode so it only uses the available blocks. | |
2bedc0e8 | 449 | */ |
e5d40372 DB |
450 | int ff_h264_check_intra_pred_mode(H264Context *h, int mode, int is_chroma) |
451 | { | |
e5d40372 DB |
452 | static const int8_t top[7] = { LEFT_DC_PRED8x8, 1, -1, -1 }; |
453 | static const int8_t left[7] = { TOP_DC_PRED8x8, -1, 2, -1, DC_128_PRED8x8 }; | |
454 | ||
455 | if (mode > 6U) { | |
2c541554 | 456 | av_log(h->avctx, AV_LOG_ERROR, |
e5d40372 | 457 | "out of range intra chroma pred mode at %d %d\n", |
2c541554 | 458 | h->mb_x, h->mb_y); |
7440fe83 | 459 | return -1; |
5175b937 | 460 | } |
115329f1 | 461 | |
e5d40372 DB |
462 | if (!(h->top_samples_available & 0x8000)) { |
463 | mode = top[mode]; | |
464 | if (mode < 0) { | |
2c541554 | 465 | av_log(h->avctx, AV_LOG_ERROR, |
e5d40372 | 466 | "top block unavailable for requested intra mode at %d %d\n", |
2c541554 | 467 | h->mb_x, h->mb_y); |
0da71265 MN |
468 | return -1; |
469 | } | |
470 | } | |
115329f1 | 471 | |
e5d40372 DB |
472 | if ((h->left_samples_available & 0x8080) != 0x8080) { |
473 | mode = left[mode]; | |
474 | if (is_chroma && (h->left_samples_available & 0x8080)) { | |
475 | // mad cow disease mode, aka MBAFF + constrained_intra_pred | |
476 | mode = ALZHEIMER_DC_L0T_PRED8x8 + | |
477 | (!(h->left_samples_available & 0x8000)) + | |
478 | 2 * (mode == DC_128_PRED8x8); | |
d1d10e91 | 479 | } |
e5d40372 | 480 | if (mode < 0) { |
2c541554 | 481 | av_log(h->avctx, AV_LOG_ERROR, |
e5d40372 | 482 | "left block unavailable for requested intra mode at %d %d\n", |
2c541554 | 483 | h->mb_x, h->mb_y); |
0da71265 | 484 | return -1; |
115329f1 | 485 | } |
0da71265 MN |
486 | } |
487 | ||
488 | return mode; | |
489 | } | |
490 | ||
e5d40372 DB |
491 | const uint8_t *ff_h264_decode_nal(H264Context *h, const uint8_t *src, |
492 | int *dst_length, int *consumed, int length) | |
493 | { | |
0da71265 MN |
494 | int i, si, di; |
495 | uint8_t *dst; | |
24456882 | 496 | int bufidx; |
0da71265 | 497 | |
e5d40372 DB |
498 | // src[0]&0x80; // forbidden bit |
499 | h->nal_ref_idc = src[0] >> 5; | |
500 | h->nal_unit_type = src[0] & 0x1F; | |
0da71265 | 501 | |
e5d40372 DB |
502 | src++; |
503 | length--; | |
e08715d3 | 504 | |
58db34aa RB |
505 | #define STARTCODE_TEST \ |
506 | if (i + 2 < length && src[i + 1] == 0 && src[i + 2] <= 3) { \ | |
507 | if (src[i + 2] != 3) { \ | |
508 | /* startcode, so we must be past the end */ \ | |
509 | length = i; \ | |
510 | } \ | |
511 | break; \ | |
512 | } | |
b250f9c6 | 513 | #if HAVE_FAST_UNALIGNED |
58db34aa RB |
514 | #define FIND_FIRST_ZERO \ |
515 | if (i > 0 && !src[i]) \ | |
516 | i--; \ | |
517 | while (src[i]) \ | |
518 | i++ | |
e5d40372 | 519 | #if HAVE_FAST_64BIT |
e5d40372 DB |
520 | for (i = 0; i + 1 < length; i += 9) { |
521 | if (!((~AV_RN64A(src + i) & | |
522 | (AV_RN64A(src + i) - 0x0100010001000101ULL)) & | |
523 | 0x8000800080008080ULL)) | |
58db34aa RB |
524 | continue; |
525 | FIND_FIRST_ZERO; | |
526 | STARTCODE_TEST; | |
527 | i -= 7; | |
528 | } | |
e5d40372 | 529 | #else |
e5d40372 DB |
530 | for (i = 0; i + 1 < length; i += 5) { |
531 | if (!((~AV_RN32A(src + i) & | |
532 | (AV_RN32A(src + i) - 0x01000101U)) & | |
533 | 0x80008080U)) | |
e08715d3 | 534 | continue; |
58db34aa RB |
535 | FIND_FIRST_ZERO; |
536 | STARTCODE_TEST; | |
537 | i -= 3; | |
538 | } | |
539 | #endif | |
e08715d3 | 540 | #else |
e5d40372 DB |
541 | for (i = 0; i + 1 < length; i += 2) { |
542 | if (src[i]) | |
543 | continue; | |
544 | if (i > 0 && src[i - 1] == 0) | |
545 | i--; | |
58db34aa | 546 | STARTCODE_TEST; |
0da71265 | 547 | } |
58db34aa | 548 | #endif |
0da71265 | 549 | |
e5d40372 DB |
550 | if (i >= length - 1) { // no escaped 0 |
551 | *dst_length = length; | |
552 | *consumed = length + 1; // +1 for the header | |
115329f1 | 553 | return src; |
0da71265 MN |
554 | } |
555 | ||
e5d40372 DB |
556 | // use second escape buffer for inter data |
557 | bufidx = h->nal_unit_type == NAL_DPC ? 1 : 0; | |
558 | av_fast_malloc(&h->rbsp_buffer[bufidx], &h->rbsp_buffer_size[bufidx], | |
559 | length + FF_INPUT_BUFFER_PADDING_SIZE); | |
560 | dst = h->rbsp_buffer[bufidx]; | |
0da71265 | 561 | |
e5d40372 | 562 | if (dst == NULL) |
ac658be5 | 563 | return NULL; |
ac658be5 | 564 | |
593af7cd | 565 | memcpy(dst, src, i); |
e5d40372 DB |
566 | si = di = i; |
567 | while (si + 2 < length) { | |
568 | // remove escapes (very rare 1:2^22) | |
569 | if (src[si + 2] > 3) { | |
570 | dst[di++] = src[si++]; | |
571 | dst[di++] = src[si++]; | |
572 | } else if (src[si] == 0 && src[si + 1] == 0) { | |
573 | if (src[si + 2] == 3) { // escape | |
574 | dst[di++] = 0; | |
575 | dst[di++] = 0; | |
576 | si += 3; | |
c8470cc1 | 577 | continue; |
e5d40372 | 578 | } else // next start code |
593af7cd | 579 | goto nsc; |
0da71265 MN |
580 | } |
581 | ||
e5d40372 | 582 | dst[di++] = src[si++]; |
0da71265 | 583 | } |
e5d40372 DB |
584 | while (si < length) |
585 | dst[di++] = src[si++]; | |
593af7cd | 586 | nsc: |
0da71265 | 587 | |
e5d40372 | 588 | memset(dst + di, 0, FF_INPUT_BUFFER_PADDING_SIZE); |
d4369630 | 589 | |
e5d40372 DB |
590 | *dst_length = di; |
591 | *consumed = si + 1; // +1 for the header | |
592 | /* FIXME store exact number of bits in the getbitcontext | |
593 | * (it is needed for decoding) */ | |
0da71265 MN |
594 | return dst; |
595 | } | |
596 | ||
85297319 DEP |
597 | /** |
598 | * Identify the exact end of the bitstream | |
599 | * @return the length of the trailing, or 0 if damaged | |
600 | */ | |
b691fd7a | 601 | static int decode_rbsp_trailing(H264Context *h, const uint8_t *src) |
e5d40372 DB |
602 | { |
603 | int v = *src; | |
0da71265 MN |
604 | int r; |
605 | ||
2c541554 | 606 | tprintf(h->avctx, "rbsp trailing %X\n", v); |
0da71265 | 607 | |
e5d40372 DB |
608 | for (r = 1; r < 9; r++) { |
609 | if (v & 1) | |
610 | return r; | |
611 | v >>= 1; | |
0da71265 MN |
612 | } |
613 | return 0; | |
614 | } | |
615 | ||
e5d40372 DB |
616 | static inline int get_lowest_part_list_y(H264Context *h, Picture *pic, int n, |
617 | int height, int y_offset, int list) | |
618 | { | |
619 | int raw_my = h->mv_cache[list][scan8[n]][1]; | |
fb845ffd RB |
620 | int filter_height_up = (raw_my & 3) ? 2 : 0; |
621 | int filter_height_down = (raw_my & 3) ? 3 : 0; | |
e5d40372 | 622 | int full_my = (raw_my >> 2) + y_offset; |
fb845ffd RB |
623 | int top = full_my - filter_height_up; |
624 | int bottom = full_my + filter_height_down + height; | |
6a9c8594 AS |
625 | |
626 | return FFMAX(abs(top), bottom); | |
627 | } | |
628 | ||
e5d40372 DB |
629 | static inline void get_lowest_part_y(H264Context *h, int refs[2][48], int n, |
630 | int height, int y_offset, int list0, | |
631 | int list1, int *nrefs) | |
632 | { | |
6a9c8594 AS |
633 | int my; |
634 | ||
da6be8fc | 635 | y_offset += 16 * (h->mb_y >> MB_FIELD(h)); |
6a9c8594 | 636 | |
e5d40372 DB |
637 | if (list0) { |
638 | int ref_n = h->ref_cache[0][scan8[n]]; | |
639 | Picture *ref = &h->ref_list[0][ref_n]; | |
6a9c8594 AS |
640 | |
641 | // Error resilience puts the current picture in the ref list. | |
642 | // Don't try to wait on these as it will cause a deadlock. | |
643 | // Fields can wait on each other, though. | |
759001c5 AK |
644 | if (ref->tf.progress->data != h->cur_pic.tf.progress->data || |
645 | (ref->reference & 3) != h->picture_structure) { | |
6a9c8594 | 646 | my = get_lowest_part_list_y(h, ref, n, height, y_offset, 0); |
e5d40372 DB |
647 | if (refs[0][ref_n] < 0) |
648 | nrefs[0] += 1; | |
6a9c8594 AS |
649 | refs[0][ref_n] = FFMAX(refs[0][ref_n], my); |
650 | } | |
651 | } | |
652 | ||
e5d40372 DB |
653 | if (list1) { |
654 | int ref_n = h->ref_cache[1][scan8[n]]; | |
655 | Picture *ref = &h->ref_list[1][ref_n]; | |
6a9c8594 | 656 | |
759001c5 AK |
657 | if (ref->tf.progress->data != h->cur_pic.tf.progress->data || |
658 | (ref->reference & 3) != h->picture_structure) { | |
6a9c8594 | 659 | my = get_lowest_part_list_y(h, ref, n, height, y_offset, 1); |
e5d40372 DB |
660 | if (refs[1][ref_n] < 0) |
661 | nrefs[1] += 1; | |
6a9c8594 AS |
662 | refs[1][ref_n] = FFMAX(refs[1][ref_n], my); |
663 | } | |
664 | } | |
665 | } | |
666 | ||
667 | /** | |
668 | * Wait until all reference frames are available for MC operations. | |
669 | * | |
670 | * @param h the H264 context | |
671 | */ | |
e5d40372 DB |
672 | static void await_references(H264Context *h) |
673 | { | |
e5d40372 | 674 | const int mb_xy = h->mb_xy; |
759001c5 | 675 | const int mb_type = h->cur_pic.mb_type[mb_xy]; |
6a9c8594 | 676 | int refs[2][48]; |
e5d40372 | 677 | int nrefs[2] = { 0 }; |
6a9c8594 AS |
678 | int ref, list; |
679 | ||
680 | memset(refs, -1, sizeof(refs)); | |
681 | ||
e5d40372 | 682 | if (IS_16X16(mb_type)) { |
6a9c8594 | 683 | get_lowest_part_y(h, refs, 0, 16, 0, |
e5d40372 DB |
684 | IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1), nrefs); |
685 | } else if (IS_16X8(mb_type)) { | |
6a9c8594 | 686 | get_lowest_part_y(h, refs, 0, 8, 0, |
e5d40372 | 687 | IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1), nrefs); |
6a9c8594 | 688 | get_lowest_part_y(h, refs, 8, 8, 8, |
e5d40372 DB |
689 | IS_DIR(mb_type, 1, 0), IS_DIR(mb_type, 1, 1), nrefs); |
690 | } else if (IS_8X16(mb_type)) { | |
6a9c8594 | 691 | get_lowest_part_y(h, refs, 0, 16, 0, |
e5d40372 | 692 | IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1), nrefs); |
6a9c8594 | 693 | get_lowest_part_y(h, refs, 4, 16, 0, |
e5d40372 DB |
694 | IS_DIR(mb_type, 1, 0), IS_DIR(mb_type, 1, 1), nrefs); |
695 | } else { | |
6a9c8594 AS |
696 | int i; |
697 | ||
698 | assert(IS_8X8(mb_type)); | |
699 | ||
e5d40372 DB |
700 | for (i = 0; i < 4; i++) { |
701 | const int sub_mb_type = h->sub_mb_type[i]; | |
702 | const int n = 4 * i; | |
703 | int y_offset = (i & 2) << 2; | |
704 | ||
705 | if (IS_SUB_8X8(sub_mb_type)) { | |
706 | get_lowest_part_y(h, refs, n, 8, y_offset, | |
707 | IS_DIR(sub_mb_type, 0, 0), | |
708 | IS_DIR(sub_mb_type, 0, 1), | |
709 | nrefs); | |
710 | } else if (IS_SUB_8X4(sub_mb_type)) { | |
711 | get_lowest_part_y(h, refs, n, 4, y_offset, | |
712 | IS_DIR(sub_mb_type, 0, 0), | |
713 | IS_DIR(sub_mb_type, 0, 1), | |
714 | nrefs); | |
715 | get_lowest_part_y(h, refs, n + 2, 4, y_offset + 4, | |
716 | IS_DIR(sub_mb_type, 0, 0), | |
717 | IS_DIR(sub_mb_type, 0, 1), | |
718 | nrefs); | |
719 | } else if (IS_SUB_4X8(sub_mb_type)) { | |
720 | get_lowest_part_y(h, refs, n, 8, y_offset, | |
721 | IS_DIR(sub_mb_type, 0, 0), | |
722 | IS_DIR(sub_mb_type, 0, 1), | |
723 | nrefs); | |
724 | get_lowest_part_y(h, refs, n + 1, 8, y_offset, | |
725 | IS_DIR(sub_mb_type, 0, 0), | |
726 | IS_DIR(sub_mb_type, 0, 1), | |
727 | nrefs); | |
728 | } else { | |
6a9c8594 AS |
729 | int j; |
730 | assert(IS_SUB_4X4(sub_mb_type)); | |
e5d40372 DB |
731 | for (j = 0; j < 4; j++) { |
732 | int sub_y_offset = y_offset + 2 * (j & 2); | |
733 | get_lowest_part_y(h, refs, n + j, 4, sub_y_offset, | |
734 | IS_DIR(sub_mb_type, 0, 0), | |
735 | IS_DIR(sub_mb_type, 0, 1), | |
736 | nrefs); | |
6a9c8594 AS |
737 | } |
738 | } | |
739 | } | |
740 | } | |
741 | ||
e5d40372 DB |
742 | for (list = h->list_count - 1; list >= 0; list--) |
743 | for (ref = 0; ref < 48 && nrefs[list]; ref++) { | |
6a9c8594 | 744 | int row = refs[list][ref]; |
e5d40372 DB |
745 | if (row >= 0) { |
746 | Picture *ref_pic = &h->ref_list[list][ref]; | |
759001c5 | 747 | int ref_field = ref_pic->reference - 1; |
6a9c8594 | 748 | int ref_field_picture = ref_pic->field_picture; |
2c541554 | 749 | int pic_height = 16 * h->mb_height >> ref_field_picture; |
6a9c8594 | 750 | |
82313eaa | 751 | row <<= MB_MBAFF(h); |
6a9c8594 AS |
752 | nrefs[list]--; |
753 | ||
7fa00653 | 754 | if (!FIELD_PICTURE(h) && ref_field_picture) { // frame referencing two fields |
759001c5 | 755 | ff_thread_await_progress(&ref_pic->tf, |
e5d40372 DB |
756 | FFMIN((row >> 1) - !(row & 1), |
757 | pic_height - 1), | |
758 | 1); | |
759001c5 | 759 | ff_thread_await_progress(&ref_pic->tf, |
e5d40372 DB |
760 | FFMIN((row >> 1), pic_height - 1), |
761 | 0); | |
7fa00653 | 762 | } else if (FIELD_PICTURE(h) && !ref_field_picture) { // field referencing one field of a frame |
759001c5 | 763 | ff_thread_await_progress(&ref_pic->tf, |
e5d40372 DB |
764 | FFMIN(row * 2 + ref_field, |
765 | pic_height - 1), | |
766 | 0); | |
7fa00653 | 767 | } else if (FIELD_PICTURE(h)) { |
759001c5 | 768 | ff_thread_await_progress(&ref_pic->tf, |
e5d40372 DB |
769 | FFMIN(row, pic_height - 1), |
770 | ref_field); | |
771 | } else { | |
759001c5 | 772 | ff_thread_await_progress(&ref_pic->tf, |
e5d40372 DB |
773 | FFMIN(row, pic_height - 1), |
774 | 0); | |
6a9c8594 AS |
775 | } |
776 | } | |
777 | } | |
66c6b5e2 | 778 | } |
66c6b5e2 | 779 | |
e5d40372 DB |
780 | static av_always_inline void mc_dir_part(H264Context *h, Picture *pic, |
781 | int n, int square, int height, | |
782 | int delta, int list, | |
783 | uint8_t *dest_y, uint8_t *dest_cb, | |
784 | uint8_t *dest_cr, | |
785 | int src_x_offset, int src_y_offset, | |
786 | qpel_mc_func *qpix_op, | |
787 | h264_chroma_mc_func chroma_op, | |
788 | int pixel_shift, int chroma_idc) | |
05fb63f5 | 789 | { |
e5d40372 DB |
790 | const int mx = h->mv_cache[list][scan8[n]][0] + src_x_offset * 8; |
791 | int my = h->mv_cache[list][scan8[n]][1] + src_y_offset * 8; | |
792 | const int luma_xy = (mx & 3) + ((my & 3) << 2); | |
793 | int offset = ((mx >> 2) << pixel_shift) + (my >> 2) * h->mb_linesize; | |
794 | uint8_t *src_y = pic->f.data[0] + offset; | |
795 | uint8_t *src_cb, *src_cr; | |
fae6fd5b RB |
796 | int extra_width = 0; |
797 | int extra_height = 0; | |
e5d40372 DB |
798 | int emu = 0; |
799 | const int full_mx = mx >> 2; | |
800 | const int full_my = my >> 2; | |
2c541554 | 801 | const int pic_width = 16 * h->mb_width; |
da6be8fc | 802 | const int pic_height = 16 * h->mb_height >> MB_FIELD(h); |
229d263c | 803 | int ysh; |
115329f1 | 804 | |
e5d40372 DB |
805 | if (mx & 7) |
806 | extra_width -= 3; | |
807 | if (my & 7) | |
808 | extra_height -= 3; | |
809 | ||
810 | if (full_mx < 0 - extra_width || | |
811 | full_my < 0 - extra_height || | |
812 | full_mx + 16 /*FIXME*/ > pic_width + extra_width || | |
813 | full_my + 16 /*FIXME*/ > pic_height + extra_height) { | |
2c541554 | 814 | h->vdsp.emulated_edge_mc(h->edge_emu_buffer, |
8c53d39e RB |
815 | src_y - (2 << pixel_shift) - 2 * h->mb_linesize, |
816 | h->mb_linesize, | |
817 | 16 + 5, 16 + 5 /*FIXME*/, full_mx - 2, | |
818 | full_my - 2, pic_width, pic_height); | |
2c541554 | 819 | src_y = h->edge_emu_buffer + (2 << pixel_shift) + 2 * h->mb_linesize; |
e5d40372 DB |
820 | emu = 1; |
821 | } | |
822 | ||
823 | qpix_op[luma_xy](dest_y, src_y, h->mb_linesize); // FIXME try variable height perhaps? | |
824 | if (!square) | |
5d18eaad | 825 | qpix_op[luma_xy](dest_y + delta, src_y + delta, h->mb_linesize); |
115329f1 | 826 | |
2c541554 | 827 | if (CONFIG_GRAY && h->flags & CODEC_FLAG_GRAY) |
e5d40372 | 828 | return; |
7b442ad9 | 829 | |
e5d40372 | 830 | if (chroma_idc == 3 /* yuv444 */) { |
657ccb5a | 831 | src_cb = pic->f.data[1] + offset; |
e5d40372 | 832 | if (emu) { |
2c541554 | 833 | h->vdsp.emulated_edge_mc(h->edge_emu_buffer, |
8c53d39e RB |
834 | src_cb - (2 << pixel_shift) - 2 * h->mb_linesize, |
835 | h->mb_linesize, | |
836 | 16 + 5, 16 + 5 /*FIXME*/, | |
837 | full_mx - 2, full_my - 2, | |
838 | pic_width, pic_height); | |
2c541554 | 839 | src_cb = h->edge_emu_buffer + (2 << pixel_shift) + 2 * h->mb_linesize; |
c90b9442 | 840 | } |
e5d40372 DB |
841 | qpix_op[luma_xy](dest_cb, src_cb, h->mb_linesize); // FIXME try variable height perhaps? |
842 | if (!square) | |
c90b9442 | 843 | qpix_op[luma_xy](dest_cb + delta, src_cb + delta, h->mb_linesize); |
c90b9442 | 844 | |
657ccb5a | 845 | src_cr = pic->f.data[2] + offset; |
e5d40372 | 846 | if (emu) { |
2c541554 | 847 | h->vdsp.emulated_edge_mc(h->edge_emu_buffer, |
8c53d39e RB |
848 | src_cr - (2 << pixel_shift) - 2 * h->mb_linesize, |
849 | h->mb_linesize, | |
850 | 16 + 5, 16 + 5 /*FIXME*/, | |
851 | full_mx - 2, full_my - 2, | |
852 | pic_width, pic_height); | |
2c541554 | 853 | src_cr = h->edge_emu_buffer + (2 << pixel_shift) + 2 * h->mb_linesize; |
c90b9442 | 854 | } |
e5d40372 DB |
855 | qpix_op[luma_xy](dest_cr, src_cr, h->mb_linesize); // FIXME try variable height perhaps? |
856 | if (!square) | |
c90b9442 | 857 | qpix_op[luma_xy](dest_cr + delta, src_cr + delta, h->mb_linesize); |
c90b9442 JGG |
858 | return; |
859 | } | |
860 | ||
05fb63f5 | 861 | ysh = 3 - (chroma_idc == 2 /* yuv422 */); |
da6be8fc | 862 | if (chroma_idc == 1 /* yuv420 */ && MB_FIELD(h)) { |
5d18eaad | 863 | // chroma offset when predicting from a field of opposite parity |
759001c5 | 864 | my += 2 * ((h->mb_y & 1) - (pic->reference - 1)); |
e5d40372 | 865 | emu |= (my >> 3) < 0 || (my >> 3) + 8 >= (pic_height >> 1); |
5d18eaad | 866 | } |
229d263c | 867 | |
e5d40372 DB |
868 | src_cb = pic->f.data[1] + ((mx >> 3) << pixel_shift) + |
869 | (my >> ysh) * h->mb_uvlinesize; | |
870 | src_cr = pic->f.data[2] + ((mx >> 3) << pixel_shift) + | |
871 | (my >> ysh) * h->mb_uvlinesize; | |
5d18eaad | 872 | |
e5d40372 | 873 | if (emu) { |
2c541554 | 874 | h->vdsp.emulated_edge_mc(h->edge_emu_buffer, src_cb, h->mb_uvlinesize, |
8c53d39e RB |
875 | 9, 8 * chroma_idc + 1, (mx >> 3), (my >> ysh), |
876 | pic_width >> 1, pic_height >> (chroma_idc == 1 /* yuv420 */)); | |
2c541554 | 877 | src_cb = h->edge_emu_buffer; |
0da71265 | 878 | } |
e5d40372 DB |
879 | chroma_op(dest_cb, src_cb, h->mb_uvlinesize, |
880 | height >> (chroma_idc == 1 /* yuv420 */), | |
881 | mx & 7, (my << (chroma_idc == 2 /* yuv422 */)) & 7); | |
0da71265 | 882 | |
e5d40372 | 883 | if (emu) { |
2c541554 | 884 | h->vdsp.emulated_edge_mc(h->edge_emu_buffer, src_cr, h->mb_uvlinesize, |
8c53d39e RB |
885 | 9, 8 * chroma_idc + 1, (mx >> 3), (my >> ysh), |
886 | pic_width >> 1, pic_height >> (chroma_idc == 1 /* yuv420 */)); | |
2c541554 | 887 | src_cr = h->edge_emu_buffer; |
0da71265 | 888 | } |
05fb63f5 | 889 | chroma_op(dest_cr, src_cr, h->mb_uvlinesize, height >> (chroma_idc == 1 /* yuv420 */), |
e5d40372 | 890 | mx & 7, (my << (chroma_idc == 2 /* yuv422 */)) & 7); |
0da71265 MN |
891 | } |
892 | ||
e5d40372 DB |
893 | static av_always_inline void mc_part_std(H264Context *h, int n, int square, |
894 | int height, int delta, | |
895 | uint8_t *dest_y, uint8_t *dest_cb, | |
896 | uint8_t *dest_cr, | |
897 | int x_offset, int y_offset, | |
898 | qpel_mc_func *qpix_put, | |
899 | h264_chroma_mc_func chroma_put, | |
900 | qpel_mc_func *qpix_avg, | |
901 | h264_chroma_mc_func chroma_avg, | |
902 | int list0, int list1, | |
903 | int pixel_shift, int chroma_idc) | |
05fb63f5 | 904 | { |
e5d40372 DB |
905 | qpel_mc_func *qpix_op = qpix_put; |
906 | h264_chroma_mc_func chroma_op = chroma_put; | |
115329f1 | 907 | |
e5d40372 | 908 | dest_y += (2 * x_offset << pixel_shift) + 2 * y_offset * h->mb_linesize; |
05fb63f5 | 909 | if (chroma_idc == 3 /* yuv444 */) { |
e5d40372 DB |
910 | dest_cb += (2 * x_offset << pixel_shift) + 2 * y_offset * h->mb_linesize; |
911 | dest_cr += (2 * x_offset << pixel_shift) + 2 * y_offset * h->mb_linesize; | |
05fb63f5 | 912 | } else if (chroma_idc == 2 /* yuv422 */) { |
e5d40372 DB |
913 | dest_cb += (x_offset << pixel_shift) + 2 * y_offset * h->mb_uvlinesize; |
914 | dest_cr += (x_offset << pixel_shift) + 2 * y_offset * h->mb_uvlinesize; | |
915 | } else { /* yuv420 */ | |
916 | dest_cb += (x_offset << pixel_shift) + y_offset * h->mb_uvlinesize; | |
917 | dest_cr += (x_offset << pixel_shift) + y_offset * h->mb_uvlinesize; | |
c90b9442 | 918 | } |
2c541554 | 919 | x_offset += 8 * h->mb_x; |
da6be8fc | 920 | y_offset += 8 * (h->mb_y >> MB_FIELD(h)); |
115329f1 | 921 | |
e5d40372 DB |
922 | if (list0) { |
923 | Picture *ref = &h->ref_list[0][h->ref_cache[0][scan8[n]]]; | |
c2d33742 | 924 | mc_dir_part(h, ref, n, square, height, delta, 0, |
e5d40372 DB |
925 | dest_y, dest_cb, dest_cr, x_offset, y_offset, |
926 | qpix_op, chroma_op, pixel_shift, chroma_idc); | |
0da71265 | 927 | |
e5d40372 DB |
928 | qpix_op = qpix_avg; |
929 | chroma_op = chroma_avg; | |
0da71265 MN |
930 | } |
931 | ||
e5d40372 DB |
932 | if (list1) { |
933 | Picture *ref = &h->ref_list[1][h->ref_cache[1][scan8[n]]]; | |
c2d33742 | 934 | mc_dir_part(h, ref, n, square, height, delta, 1, |
e5d40372 DB |
935 | dest_y, dest_cb, dest_cr, x_offset, y_offset, |
936 | qpix_op, chroma_op, pixel_shift, chroma_idc); | |
0da71265 MN |
937 | } |
938 | } | |
939 | ||
e5d40372 DB |
940 | static av_always_inline void mc_part_weighted(H264Context *h, int n, int square, |
941 | int height, int delta, | |
942 | uint8_t *dest_y, uint8_t *dest_cb, | |
943 | uint8_t *dest_cr, | |
944 | int x_offset, int y_offset, | |
945 | qpel_mc_func *qpix_put, | |
946 | h264_chroma_mc_func chroma_put, | |
947 | h264_weight_func luma_weight_op, | |
948 | h264_weight_func chroma_weight_op, | |
949 | h264_biweight_func luma_weight_avg, | |
950 | h264_biweight_func chroma_weight_avg, | |
951 | int list0, int list1, | |
952 | int pixel_shift, int chroma_idc) | |
953 | { | |
c2d33742 | 954 | int chroma_height; |
9f2d1b4f | 955 | |
e5d40372 | 956 | dest_y += (2 * x_offset << pixel_shift) + 2 * y_offset * h->mb_linesize; |
05fb63f5 | 957 | if (chroma_idc == 3 /* yuv444 */) { |
e5d40372 | 958 | chroma_height = height; |
c90b9442 | 959 | chroma_weight_avg = luma_weight_avg; |
e5d40372 DB |
960 | chroma_weight_op = luma_weight_op; |
961 | dest_cb += (2 * x_offset << pixel_shift) + 2 * y_offset * h->mb_linesize; | |
962 | dest_cr += (2 * x_offset << pixel_shift) + 2 * y_offset * h->mb_linesize; | |
05fb63f5 | 963 | } else if (chroma_idc == 2 /* yuv422 */) { |
c2d33742 | 964 | chroma_height = height; |
e5d40372 DB |
965 | dest_cb += (x_offset << pixel_shift) + 2 * y_offset * h->mb_uvlinesize; |
966 | dest_cr += (x_offset << pixel_shift) + 2 * y_offset * h->mb_uvlinesize; | |
967 | } else { /* yuv420 */ | |
c2d33742 | 968 | chroma_height = height >> 1; |
e5d40372 DB |
969 | dest_cb += (x_offset << pixel_shift) + y_offset * h->mb_uvlinesize; |
970 | dest_cr += (x_offset << pixel_shift) + y_offset * h->mb_uvlinesize; | |
c90b9442 | 971 | } |
2c541554 | 972 | x_offset += 8 * h->mb_x; |
da6be8fc | 973 | y_offset += 8 * (h->mb_y >> MB_FIELD(h)); |
115329f1 | 974 | |
e5d40372 | 975 | if (list0 && list1) { |
9f2d1b4f LM |
976 | /* don't optimize for luma-only case, since B-frames usually |
977 | * use implicit weights => chroma too. */ | |
a394959b JG |
978 | uint8_t *tmp_cb = h->bipred_scratchpad; |
979 | uint8_t *tmp_cr = h->bipred_scratchpad + (16 << pixel_shift); | |
980 | uint8_t *tmp_y = h->bipred_scratchpad + 16 * h->mb_uvlinesize; | |
e5d40372 DB |
981 | int refn0 = h->ref_cache[0][scan8[n]]; |
982 | int refn1 = h->ref_cache[1][scan8[n]]; | |
9f2d1b4f | 983 | |
c2d33742 | 984 | mc_dir_part(h, &h->ref_list[0][refn0], n, square, height, delta, 0, |
9f2d1b4f | 985 | dest_y, dest_cb, dest_cr, |
05fb63f5 RB |
986 | x_offset, y_offset, qpix_put, chroma_put, |
987 | pixel_shift, chroma_idc); | |
c2d33742 | 988 | mc_dir_part(h, &h->ref_list[1][refn1], n, square, height, delta, 1, |
9f2d1b4f | 989 | tmp_y, tmp_cb, tmp_cr, |
05fb63f5 RB |
990 | x_offset, y_offset, qpix_put, chroma_put, |
991 | pixel_shift, chroma_idc); | |
9f2d1b4f | 992 | |
e5d40372 | 993 | if (h->use_weight == 2) { |
2c541554 | 994 | int weight0 = h->implicit_weight[refn0][refn1][h->mb_y & 1]; |
9f2d1b4f | 995 | int weight1 = 64 - weight0; |
e5d40372 DB |
996 | luma_weight_avg(dest_y, tmp_y, h->mb_linesize, |
997 | height, 5, weight0, weight1, 0); | |
c2d33742 RB |
998 | chroma_weight_avg(dest_cb, tmp_cb, h->mb_uvlinesize, |
999 | chroma_height, 5, weight0, weight1, 0); | |
1000 | chroma_weight_avg(dest_cr, tmp_cr, h->mb_uvlinesize, | |
1001 | chroma_height, 5, weight0, weight1, 0); | |
e5d40372 DB |
1002 | } else { |
1003 | luma_weight_avg(dest_y, tmp_y, h->mb_linesize, height, | |
1004 | h->luma_log2_weight_denom, | |
1005 | h->luma_weight[refn0][0][0], | |
1006 | h->luma_weight[refn1][1][0], | |
1007 | h->luma_weight[refn0][0][1] + | |
1008 | h->luma_weight[refn1][1][1]); | |
1009 | chroma_weight_avg(dest_cb, tmp_cb, h->mb_uvlinesize, chroma_height, | |
1010 | h->chroma_log2_weight_denom, | |
1011 | h->chroma_weight[refn0][0][0][0], | |
1012 | h->chroma_weight[refn1][1][0][0], | |
1013 | h->chroma_weight[refn0][0][0][1] + | |
1014 | h->chroma_weight[refn1][1][0][1]); | |
1015 | chroma_weight_avg(dest_cr, tmp_cr, h->mb_uvlinesize, chroma_height, | |
1016 | h->chroma_log2_weight_denom, | |
1017 | h->chroma_weight[refn0][0][1][0], | |
1018 | h->chroma_weight[refn1][1][1][0], | |
1019 | h->chroma_weight[refn0][0][1][1] + | |
1020 | h->chroma_weight[refn1][1][1][1]); | |
9f2d1b4f | 1021 | } |
e5d40372 DB |
1022 | } else { |
1023 | int list = list1 ? 1 : 0; | |
1024 | int refn = h->ref_cache[list][scan8[n]]; | |
1025 | Picture *ref = &h->ref_list[list][refn]; | |
c2d33742 | 1026 | mc_dir_part(h, ref, n, square, height, delta, list, |
9f2d1b4f | 1027 | dest_y, dest_cb, dest_cr, x_offset, y_offset, |
05fb63f5 | 1028 | qpix_put, chroma_put, pixel_shift, chroma_idc); |
9f2d1b4f | 1029 | |
e5d40372 DB |
1030 | luma_weight_op(dest_y, h->mb_linesize, height, |
1031 | h->luma_log2_weight_denom, | |
1032 | h->luma_weight[refn][list][0], | |
1033 | h->luma_weight[refn][list][1]); | |
1034 | if (h->use_weight_chroma) { | |
1035 | chroma_weight_op(dest_cb, h->mb_uvlinesize, chroma_height, | |
1036 | h->chroma_log2_weight_denom, | |
1037 | h->chroma_weight[refn][list][0][0], | |
1038 | h->chroma_weight[refn][list][0][1]); | |
1039 | chroma_weight_op(dest_cr, h->mb_uvlinesize, chroma_height, | |
1040 | h->chroma_log2_weight_denom, | |
1041 | h->chroma_weight[refn][list][1][0], | |
1042 | h->chroma_weight[refn][list][1][1]); | |
9f2d1b4f LM |
1043 | } |
1044 | } | |
1045 | } | |
1046 | ||
e5d40372 DB |
1047 | static av_always_inline void prefetch_motion(H264Context *h, int list, |
1048 | int pixel_shift, int chroma_idc) | |
27209bb1 | 1049 | { |
513fbd8e LM |
1050 | /* fetch pixels for estimated mv 4 macroblocks ahead |
1051 | * optimized for 64byte cache lines */ | |
513fbd8e | 1052 | const int refn = h->ref_cache[list][scan8[0]]; |
e5d40372 | 1053 | if (refn >= 0) { |
2c541554 AK |
1054 | const int mx = (h->mv_cache[list][scan8[0]][0] >> 2) + 16 * h->mb_x + 8; |
1055 | const int my = (h->mv_cache[list][scan8[0]][1] >> 2) + 16 * h->mb_y; | |
657ccb5a | 1056 | uint8_t **src = h->ref_list[list][refn].f.data; |
e5d40372 | 1057 | int off = (mx << pixel_shift) + |
2c541554 | 1058 | (my + (h->mb_x & 3) * 4) * h->mb_linesize + |
e5d40372 | 1059 | (64 << pixel_shift); |
2c541554 | 1060 | h->vdsp.prefetch(src[0] + off, h->linesize, 4); |
05fb63f5 | 1061 | if (chroma_idc == 3 /* yuv444 */) { |
2c541554 AK |
1062 | h->vdsp.prefetch(src[1] + off, h->linesize, 4); |
1063 | h->vdsp.prefetch(src[2] + off, h->linesize, 4); | |
e5d40372 DB |
1064 | } else { |
1065 | off = ((mx >> 1) << pixel_shift) + | |
2c541554 | 1066 | ((my >> 1) + (h->mb_x & 7)) * h->uvlinesize + |
e5d40372 | 1067 | (64 << pixel_shift); |
2c541554 | 1068 | h->vdsp.prefetch(src[1] + off, src[2] - src[1], 2); |
c90b9442 | 1069 | } |
513fbd8e LM |
1070 | } |
1071 | } | |
1072 | ||
e5d40372 DB |
1073 | static void free_tables(H264Context *h, int free_rbsp) |
1074 | { | |
7978debd | 1075 | int i; |
afebe2f7 | 1076 | H264Context *hx; |
6a9c8594 | 1077 | |
0da71265 | 1078 | av_freep(&h->intra4x4_pred_mode); |
e5017ab8 LA |
1079 | av_freep(&h->chroma_pred_mode_table); |
1080 | av_freep(&h->cbp_table); | |
9e528114 LA |
1081 | av_freep(&h->mvd_table[0]); |
1082 | av_freep(&h->mvd_table[1]); | |
5ad984c9 | 1083 | av_freep(&h->direct_table); |
0da71265 MN |
1084 | av_freep(&h->non_zero_count); |
1085 | av_freep(&h->slice_table_base); | |
e5d40372 | 1086 | h->slice_table = NULL; |
c988f975 | 1087 | av_freep(&h->list_counts); |
e5017ab8 | 1088 | |
0da71265 | 1089 | av_freep(&h->mb2b_xy); |
d43c1922 | 1090 | av_freep(&h->mb2br_xy); |
9f2d1b4f | 1091 | |
759001c5 AK |
1092 | av_buffer_pool_uninit(&h->qscale_table_pool); |
1093 | av_buffer_pool_uninit(&h->mb_type_pool); | |
1094 | av_buffer_pool_uninit(&h->motion_val_pool); | |
1095 | av_buffer_pool_uninit(&h->ref_index_pool); | |
1096 | ||
1097 | if (free_rbsp && h->DPB) { | |
1098 | for (i = 0; i < MAX_PICTURE_COUNT; i++) | |
1099 | unref_picture(h, &h->DPB[i]); | |
2c541554 | 1100 | av_freep(&h->DPB); |
2c541554 | 1101 | } else if (h->DPB) { |
759001c5 | 1102 | for (i = 0; i < MAX_PICTURE_COUNT; i++) |
2c541554 AK |
1103 | h->DPB[i].needs_realloc = 1; |
1104 | } | |
1105 | ||
1106 | h->cur_pic_ptr = NULL; | |
1107 | ||
e5d40372 | 1108 | for (i = 0; i < MAX_THREADS; i++) { |
afebe2f7 | 1109 | hx = h->thread_context[i]; |
e5d40372 DB |
1110 | if (!hx) |
1111 | continue; | |
afebe2f7 AÖ |
1112 | av_freep(&hx->top_borders[1]); |
1113 | av_freep(&hx->top_borders[0]); | |
a394959b | 1114 | av_freep(&hx->bipred_scratchpad); |
2c541554 AK |
1115 | av_freep(&hx->edge_emu_buffer); |
1116 | av_freep(&hx->dc_val_base); | |
1117 | av_freep(&hx->me.scratchpad); | |
1118 | av_freep(&hx->er.mb_index2xy); | |
1119 | av_freep(&hx->er.error_status_table); | |
1120 | av_freep(&hx->er.er_temp_buffer); | |
1121 | av_freep(&hx->er.mbintra_table); | |
1122 | av_freep(&hx->er.mbskip_table); | |
1123 | ||
e5d40372 | 1124 | if (free_rbsp) { |
fcb7e535 RB |
1125 | av_freep(&hx->rbsp_buffer[1]); |
1126 | av_freep(&hx->rbsp_buffer[0]); | |
1127 | hx->rbsp_buffer_size[0] = 0; | |
1128 | hx->rbsp_buffer_size[1] = 0; | |
91078926 | 1129 | } |
e5d40372 DB |
1130 | if (i) |
1131 | av_freep(&h->thread_context[i]); | |
afebe2f7 | 1132 | } |
0da71265 MN |
1133 | } |
1134 | ||
e5d40372 DB |
1135 | static void init_dequant8_coeff_table(H264Context *h) |
1136 | { | |
1137 | int i, j, q, x; | |
1138 | const int max_qp = 51 + 6 * (h->sps.bit_depth_luma - 8); | |
239ea04c | 1139 | |
e5d40372 | 1140 | for (i = 0; i < 6; i++) { |
c90b9442 | 1141 | h->dequant8_coeff[i] = h->dequant8_buffer[i]; |
e5d40372 DB |
1142 | for (j = 0; j < i; j++) |
1143 | if (!memcmp(h->pps.scaling_matrix8[j], h->pps.scaling_matrix8[i], | |
1144 | 64 * sizeof(uint8_t))) { | |
c90b9442 JGG |
1145 | h->dequant8_coeff[i] = h->dequant8_buffer[j]; |
1146 | break; | |
1147 | } | |
e5d40372 | 1148 | if (j < i) |
c90b9442 | 1149 | continue; |
239ea04c | 1150 | |
e5d40372 | 1151 | for (q = 0; q < max_qp + 1; q++) { |
d9ec210b | 1152 | int shift = div6[q]; |
e5d40372 DB |
1153 | int idx = rem6[q]; |
1154 | for (x = 0; x < 64; x++) | |
1155 | h->dequant8_coeff[i][q][(x >> 3) | ((x & 7) << 3)] = | |
1156 | ((uint32_t)dequant8_coeff_init[idx][dequant8_coeff_init_scan[((x >> 1) & 12) | (x & 3)]] * | |
1157 | h->pps.scaling_matrix8[i][x]) << shift; | |
239ea04c LM |
1158 | } |
1159 | } | |
1160 | } | |
1161 | ||
e5d40372 DB |
1162 | static void init_dequant4_coeff_table(H264Context *h) |
1163 | { | |
1164 | int i, j, q, x; | |
1165 | const int max_qp = 51 + 6 * (h->sps.bit_depth_luma - 8); | |
1166 | for (i = 0; i < 6; i++) { | |
239ea04c | 1167 | h->dequant4_coeff[i] = h->dequant4_buffer[i]; |
e5d40372 DB |
1168 | for (j = 0; j < i; j++) |
1169 | if (!memcmp(h->pps.scaling_matrix4[j], h->pps.scaling_matrix4[i], | |
1170 | 16 * sizeof(uint8_t))) { | |
239ea04c LM |
1171 | h->dequant4_coeff[i] = h->dequant4_buffer[j]; |
1172 | break; | |
1173 | } | |
e5d40372 | 1174 | if (j < i) |
239ea04c LM |
1175 | continue; |
1176 | ||
e5d40372 | 1177 | for (q = 0; q < max_qp + 1; q++) { |
d9ec210b | 1178 | int shift = div6[q] + 2; |
e5d40372 DB |
1179 | int idx = rem6[q]; |
1180 | for (x = 0; x < 16; x++) | |
1181 | h->dequant4_coeff[i][q][(x >> 2) | ((x << 2) & 0xF)] = | |
1182 | ((uint32_t)dequant4_coeff_init[idx][(x & 1) + ((x >> 2) & 1)] * | |
1183 | h->pps.scaling_matrix4[i][x]) << shift; | |
239ea04c LM |
1184 | } |
1185 | } | |
1186 | } | |
1187 | ||
e5d40372 DB |
1188 | static void init_dequant_tables(H264Context *h) |
1189 | { | |
1190 | int i, x; | |
239ea04c | 1191 | init_dequant4_coeff_table(h); |
e5d40372 | 1192 | if (h->pps.transform_8x8_mode) |
239ea04c | 1193 | init_dequant8_coeff_table(h); |
e5d40372 DB |
1194 | if (h->sps.transform_bypass) { |
1195 | for (i = 0; i < 6; i++) | |
1196 | for (x = 0; x < 16; x++) | |
1197 | h->dequant4_coeff[i][0][x] = 1 << 6; | |
1198 | if (h->pps.transform_8x8_mode) | |
1199 | for (i = 0; i < 6; i++) | |
1200 | for (x = 0; x < 64; x++) | |
1201 | h->dequant8_coeff[i][0][x] = 1 << 6; | |
239ea04c LM |
1202 | } |
1203 | } | |
1204 | ||
e5d40372 DB |
1205 | int ff_h264_alloc_tables(H264Context *h) |
1206 | { | |
2c541554 AK |
1207 | const int big_mb_num = h->mb_stride * (h->mb_height + 1); |
1208 | const int row_mb_num = h->mb_stride * 2 * h->avctx->thread_count; | |
1209 | int x, y, i; | |
e5d40372 | 1210 | |
2c541554 | 1211 | FF_ALLOCZ_OR_GOTO(h->avctx, h->intra4x4_pred_mode, |
e5d40372 | 1212 | row_mb_num * 8 * sizeof(uint8_t), fail) |
2c541554 | 1213 | FF_ALLOCZ_OR_GOTO(h->avctx, h->non_zero_count, |
e5d40372 | 1214 | big_mb_num * 48 * sizeof(uint8_t), fail) |
2c541554 AK |
1215 | FF_ALLOCZ_OR_GOTO(h->avctx, h->slice_table_base, |
1216 | (big_mb_num + h->mb_stride) * sizeof(*h->slice_table_base), fail) | |
1217 | FF_ALLOCZ_OR_GOTO(h->avctx, h->cbp_table, | |
e5d40372 | 1218 | big_mb_num * sizeof(uint16_t), fail) |
2c541554 | 1219 | FF_ALLOCZ_OR_GOTO(h->avctx, h->chroma_pred_mode_table, |
e5d40372 | 1220 | big_mb_num * sizeof(uint8_t), fail) |
2c541554 | 1221 | FF_ALLOCZ_OR_GOTO(h->avctx, h->mvd_table[0], |
e5d40372 | 1222 | 16 * row_mb_num * sizeof(uint8_t), fail); |
2c541554 | 1223 | FF_ALLOCZ_OR_GOTO(h->avctx, h->mvd_table[1], |
e5d40372 | 1224 | 16 * row_mb_num * sizeof(uint8_t), fail); |
2c541554 | 1225 | FF_ALLOCZ_OR_GOTO(h->avctx, h->direct_table, |
e5d40372 | 1226 | 4 * big_mb_num * sizeof(uint8_t), fail); |
2c541554 | 1227 | FF_ALLOCZ_OR_GOTO(h->avctx, h->list_counts, |
e5d40372 DB |
1228 | big_mb_num * sizeof(uint8_t), fail) |
1229 | ||
1230 | memset(h->slice_table_base, -1, | |
2c541554 AK |
1231 | (big_mb_num + h->mb_stride) * sizeof(*h->slice_table_base)); |
1232 | h->slice_table = h->slice_table_base + h->mb_stride * 2 + 1; | |
e5d40372 | 1233 | |
2c541554 | 1234 | FF_ALLOCZ_OR_GOTO(h->avctx, h->mb2b_xy, |
e5d40372 | 1235 | big_mb_num * sizeof(uint32_t), fail); |
2c541554 | 1236 | FF_ALLOCZ_OR_GOTO(h->avctx, h->mb2br_xy, |
e5d40372 | 1237 | big_mb_num * sizeof(uint32_t), fail); |
2c541554 AK |
1238 | for (y = 0; y < h->mb_height; y++) |
1239 | for (x = 0; x < h->mb_width; x++) { | |
1240 | const int mb_xy = x + y * h->mb_stride; | |
e5d40372 DB |
1241 | const int b_xy = 4 * x + 4 * y * h->b_stride; |
1242 | ||
1243 | h->mb2b_xy[mb_xy] = b_xy; | |
2c541554 | 1244 | h->mb2br_xy[mb_xy] = 8 * (FMO ? mb_xy : (mb_xy % (2 * h->mb_stride))); |
0da71265 | 1245 | } |
9f2d1b4f | 1246 | |
e5d40372 | 1247 | if (!h->dequant4_coeff[0]) |
56edbd81 LM |
1248 | init_dequant_tables(h); |
1249 | ||
2c541554 | 1250 | if (!h->DPB) { |
759001c5 | 1251 | h->DPB = av_mallocz_array(MAX_PICTURE_COUNT, sizeof(*h->DPB)); |
2c541554 AK |
1252 | if (!h->DPB) |
1253 | return AVERROR(ENOMEM); | |
759001c5 | 1254 | for (i = 0; i < MAX_PICTURE_COUNT; i++) |
2c541554 AK |
1255 | avcodec_get_frame_defaults(&h->DPB[i].f); |
1256 | avcodec_get_frame_defaults(&h->cur_pic.f); | |
1257 | } | |
1258 | ||
0da71265 | 1259 | return 0; |
e5d40372 | 1260 | |
0da71265 | 1261 | fail: |
91078926 | 1262 | free_tables(h, 1); |
0da71265 MN |
1263 | return -1; |
1264 | } | |
1265 | ||
afebe2f7 AÖ |
1266 | /** |
1267 | * Mimic alloc_tables(), but for every context thread. | |
1268 | */ | |
e5d40372 DB |
1269 | static void clone_tables(H264Context *dst, H264Context *src, int i) |
1270 | { | |
2c541554 | 1271 | dst->intra4x4_pred_mode = src->intra4x4_pred_mode + i * 8 * 2 * src->mb_stride; |
e5d40372 DB |
1272 | dst->non_zero_count = src->non_zero_count; |
1273 | dst->slice_table = src->slice_table; | |
1274 | dst->cbp_table = src->cbp_table; | |
1275 | dst->mb2b_xy = src->mb2b_xy; | |
1276 | dst->mb2br_xy = src->mb2br_xy; | |
1277 | dst->chroma_pred_mode_table = src->chroma_pred_mode_table; | |
2c541554 AK |
1278 | dst->mvd_table[0] = src->mvd_table[0] + i * 8 * 2 * src->mb_stride; |
1279 | dst->mvd_table[1] = src->mvd_table[1] + i * 8 * 2 * src->mb_stride; | |
e5d40372 DB |
1280 | dst->direct_table = src->direct_table; |
1281 | dst->list_counts = src->list_counts; | |
2c541554 AK |
1282 | dst->DPB = src->DPB; |
1283 | dst->cur_pic_ptr = src->cur_pic_ptr; | |
1284 | dst->cur_pic = src->cur_pic; | |
a394959b | 1285 | dst->bipred_scratchpad = NULL; |
2c541554 AK |
1286 | dst->edge_emu_buffer = NULL; |
1287 | dst->me.scratchpad = NULL; | |
1288 | ff_h264_pred_init(&dst->hpc, src->avctx->codec_id, src->sps.bit_depth_luma, | |
e5d40372 | 1289 | src->sps.chroma_format_idc); |
afebe2f7 AÖ |
1290 | } |
1291 | ||
1292 | /** | |
1293 | * Init context | |
1294 | * Allocate buffers which are not shared amongst multiple threads. | |
1295 | */ | |
e5d40372 DB |
1296 | static int context_init(H264Context *h) |
1297 | { | |
2c541554 AK |
1298 | ERContext *er = &h->er; |
1299 | int mb_array_size = h->mb_height * h->mb_stride; | |
1300 | int y_size = (2 * h->mb_width + 1) * (2 * h->mb_height + 1); | |
1301 | int c_size = h->mb_stride * (h->mb_height + 1); | |
1302 | int yc_size = y_size + 2 * c_size; | |
1303 | int x, y, i; | |
1304 | ||
1305 | FF_ALLOCZ_OR_GOTO(h->avctx, h->top_borders[0], | |
1306 | h->mb_width * 16 * 3 * sizeof(uint8_t) * 2, fail) | |
1307 | FF_ALLOCZ_OR_GOTO(h->avctx, h->top_borders[1], | |
1308 | h->mb_width * 16 * 3 * sizeof(uint8_t) * 2, fail) | |
e5d40372 DB |
1309 | |
1310 | h->ref_cache[0][scan8[5] + 1] = | |
1311 | h->ref_cache[0][scan8[7] + 1] = | |
1312 | h->ref_cache[0][scan8[13] + 1] = | |
1313 | h->ref_cache[1][scan8[5] + 1] = | |
1314 | h->ref_cache[1][scan8[7] + 1] = | |
1315 | h->ref_cache[1][scan8[13] + 1] = PART_NOT_AVAILABLE; | |
145061a1 | 1316 | |
2c541554 AK |
1317 | /* init ER */ |
1318 | er->avctx = h->avctx; | |
1319 | er->dsp = &h->dsp; | |
1320 | er->decode_mb = h264_er_decode_mb; | |
1321 | er->opaque = h; | |
1322 | er->quarter_sample = 1; | |
1323 | ||
1324 | er->mb_num = h->mb_num; | |
1325 | er->mb_width = h->mb_width; | |
1326 | er->mb_height = h->mb_height; | |
1327 | er->mb_stride = h->mb_stride; | |
1328 | er->b8_stride = h->mb_width * 2 + 1; | |
1329 | ||
1330 | FF_ALLOCZ_OR_GOTO(h->avctx, er->mb_index2xy, (h->mb_num + 1) * sizeof(int), | |
1331 | fail); // error ressilience code looks cleaner with this | |
1332 | for (y = 0; y < h->mb_height; y++) | |
1333 | for (x = 0; x < h->mb_width; x++) | |
1334 | er->mb_index2xy[x + y * h->mb_width] = x + y * h->mb_stride; | |
1335 | ||
1336 | er->mb_index2xy[h->mb_height * h->mb_width] = (h->mb_height - 1) * | |
1337 | h->mb_stride + h->mb_width; | |
1338 | ||
1339 | FF_ALLOCZ_OR_GOTO(h->avctx, er->error_status_table, | |
1340 | mb_array_size * sizeof(uint8_t), fail); | |
1341 | ||
1342 | FF_ALLOC_OR_GOTO(h->avctx, er->mbintra_table, mb_array_size, fail); | |
1343 | memset(er->mbintra_table, 1, mb_array_size); | |
1344 | ||
1345 | FF_ALLOCZ_OR_GOTO(h->avctx, er->mbskip_table, mb_array_size + 2, fail); | |
1346 | ||
1347 | FF_ALLOC_OR_GOTO(h->avctx, er->er_temp_buffer, h->mb_height * h->mb_stride, | |
1348 | fail); | |
1349 | ||
1350 | FF_ALLOCZ_OR_GOTO(h->avctx, h->dc_val_base, yc_size * sizeof(int16_t), fail); | |
1351 | er->dc_val[0] = h->dc_val_base + h->mb_width * 2 + 2; | |
1352 | er->dc_val[1] = h->dc_val_base + y_size + h->mb_stride + 1; | |
1353 | er->dc_val[2] = er->dc_val[1] + c_size; | |
1354 | for (i = 0; i < yc_size; i++) | |
1355 | h->dc_val_base[i] = 1024; | |
54974c62 | 1356 | |
afebe2f7 | 1357 | return 0; |
e5d40372 | 1358 | |
afebe2f7 AÖ |
1359 | fail: |
1360 | return -1; // free_tables will clean up for us | |
1361 | } | |
1362 | ||
61c6eef5 JG |
1363 | static int decode_nal_units(H264Context *h, const uint8_t *buf, int buf_size, |
1364 | int parse_extradata); | |
9855b2e3 | 1365 | |
05e95319 HC |
1366 | int ff_h264_decode_extradata(H264Context *h) |
1367 | { | |
2c541554 | 1368 | AVCodecContext *avctx = h->avctx; |
9855b2e3 | 1369 | |
e5d40372 | 1370 | if (avctx->extradata[0] == 1) { |
9855b2e3 MN |
1371 | int i, cnt, nalsize; |
1372 | unsigned char *p = avctx->extradata; | |
1373 | ||
1374 | h->is_avc = 1; | |
1375 | ||
e5d40372 | 1376 | if (avctx->extradata_size < 7) { |
9855b2e3 MN |
1377 | av_log(avctx, AV_LOG_ERROR, "avcC too short\n"); |
1378 | return -1; | |
1379 | } | |
1380 | /* sps and pps in the avcC always have length coded with 2 bytes, | |
e5d40372 | 1381 | * so put a fake nal_length_size = 2 while parsing them */ |
9855b2e3 MN |
1382 | h->nal_length_size = 2; |
1383 | // Decode sps from avcC | |
e5d40372 DB |
1384 | cnt = *(p + 5) & 0x1f; // Number of sps |
1385 | p += 6; | |
9855b2e3 MN |
1386 | for (i = 0; i < cnt; i++) { |
1387 | nalsize = AV_RB16(p) + 2; | |
d1186ff7 LA |
1388 | if (p - avctx->extradata + nalsize > avctx->extradata_size) |
1389 | return -1; | |
61c6eef5 | 1390 | if (decode_nal_units(h, p, nalsize, 1) < 0) { |
e5d40372 DB |
1391 | av_log(avctx, AV_LOG_ERROR, |
1392 | "Decoding sps %d from avcC failed\n", i); | |
9855b2e3 MN |
1393 | return -1; |
1394 | } | |
1395 | p += nalsize; | |
1396 | } | |
1397 | // Decode pps from avcC | |
1398 | cnt = *(p++); // Number of pps | |
1399 | for (i = 0; i < cnt; i++) { | |
1400 | nalsize = AV_RB16(p) + 2; | |
d1186ff7 LA |
1401 | if (p - avctx->extradata + nalsize > avctx->extradata_size) |
1402 | return -1; | |
61c6eef5 | 1403 | if (decode_nal_units(h, p, nalsize, 1) < 0) { |
e5d40372 DB |
1404 | av_log(avctx, AV_LOG_ERROR, |
1405 | "Decoding pps %d from avcC failed\n", i); | |
9855b2e3 MN |
1406 | return -1; |
1407 | } | |
1408 | p += nalsize; | |
1409 | } | |
e5d40372 | 1410 | // Now store right nal length size, that will be used to parse all other nals |
0c17beba | 1411 | h->nal_length_size = (avctx->extradata[4] & 0x03) + 1; |
9855b2e3 MN |
1412 | } else { |
1413 | h->is_avc = 0; | |
61c6eef5 | 1414 | if (decode_nal_units(h, avctx->extradata, avctx->extradata_size, 1) < 0) |
9855b2e3 MN |
1415 | return -1; |
1416 | } | |
05e95319 HC |
1417 | return 0; |
1418 | } | |
1419 | ||
e5d40372 DB |
1420 | av_cold int ff_h264_decode_init(AVCodecContext *avctx) |
1421 | { | |
1422 | H264Context *h = avctx->priv_data; | |
ea2bb12e | 1423 | int i; |
05e95319 | 1424 | |
2c541554 | 1425 | h->avctx = avctx; |
48d0fd2d AK |
1426 | |
1427 | h->width = h->avctx->width; | |
1428 | h->height = h->avctx->height; | |
1429 | ||
1430 | h->bit_depth_luma = 8; | |
1431 | h->chroma_format_idc = 1; | |
1432 | ||
1433 | ff_h264dsp_init(&h->h264dsp, 8, 1); | |
1434 | ff_h264chroma_init(&h->h264chroma, h->sps.bit_depth_chroma); | |
1435 | ff_h264qpel_init(&h->h264qpel, 8); | |
1436 | ff_h264_pred_init(&h->hpc, h->avctx->codec_id, 8, 1); | |
1437 | ||
1438 | h->dequant_coeff_pps = -1; | |
1439 | ||
1440 | /* needed so that IDCT permutation is known early */ | |
1441 | ff_dsputil_init(&h->dsp, h->avctx); | |
1442 | ff_videodsp_init(&h->vdsp, 8); | |
1443 | ||
1444 | memset(h->pps.scaling_matrix4, 16, 6 * 16 * sizeof(uint8_t)); | |
1445 | memset(h->pps.scaling_matrix8, 16, 2 * 64 * sizeof(uint8_t)); | |
05e95319 | 1446 | |
2c541554 | 1447 | h->picture_structure = PICT_FRAME; |
2c541554 AK |
1448 | h->slice_context_count = 1; |
1449 | h->workaround_bugs = avctx->workaround_bugs; | |
1450 | h->flags = avctx->flags; | |
05e95319 | 1451 | |
e5d40372 DB |
1452 | /* set defaults */ |
1453 | // s->decode_mb = ff_h263_decode_mb; | |
e5d40372 | 1454 | if (!avctx->has_b_frames) |
2c541554 | 1455 | h->low_delay = 1; |
05e95319 HC |
1456 | |
1457 | avctx->chroma_sample_location = AVCHROMA_LOC_LEFT; | |
1458 | ||
1459 | ff_h264_decode_init_vlc(); | |
1460 | ||
6e3ef511 | 1461 | h->pixel_shift = 0; |
19a0729b | 1462 | h->sps.bit_depth_luma = avctx->bits_per_raw_sample = 8; |
6e3ef511 | 1463 | |
05e95319 | 1464 | h->thread_context[0] = h; |
e5d40372 | 1465 | h->outputed_poc = h->next_outputed_poc = INT_MIN; |
ea2bb12e RB |
1466 | for (i = 0; i < MAX_DELAYED_PIC_COUNT; i++) |
1467 | h->last_pocs[i] = INT_MIN; | |
e5d40372 DB |
1468 | h->prev_poc_msb = 1 << 16; |
1469 | h->x264_build = -1; | |
05e95319 | 1470 | ff_h264_reset_sei(h); |
36ef5369 | 1471 | if (avctx->codec_id == AV_CODEC_ID_H264) { |
e5d40372 | 1472 | if (avctx->ticks_per_frame == 1) |
2c541554 | 1473 | h->avctx->time_base.den *= 2; |
05e95319 HC |
1474 | avctx->ticks_per_frame = 2; |
1475 | } | |
1476 | ||
e5d40372 | 1477 | if (avctx->extradata_size > 0 && avctx->extradata && |
05e95319 HC |
1478 | ff_h264_decode_extradata(h)) |
1479 | return -1; | |
1480 | ||
e5d40372 | 1481 | if (h->sps.bitstream_restriction_flag && |
2c541554 AK |
1482 | h->avctx->has_b_frames < h->sps.num_reorder_frames) { |
1483 | h->avctx->has_b_frames = h->sps.num_reorder_frames; | |
1484 | h->low_delay = 0; | |
db8cb47d | 1485 | } |
9855b2e3 | 1486 | |
759001c5 AK |
1487 | avctx->internal->allocate_progress = 1; |
1488 | ||
0da71265 MN |
1489 | return 0; |
1490 | } | |
1491 | ||
e5d40372 | 1492 | #define IN_RANGE(a, b, size) (((a) >= (b)) && ((a) < ((b) + (size)))) |
2c541554 AK |
1493 | #undef REBASE_PICTURE |
1494 | #define REBASE_PICTURE(pic, new_ctx, old_ctx) \ | |
1495 | ((pic && pic >= old_ctx->DPB && \ | |
759001c5 | 1496 | pic < old_ctx->DPB + MAX_PICTURE_COUNT) ? \ |
2c541554 | 1497 | &new_ctx->DPB[pic - old_ctx->DPB] : NULL) |
e5d40372 DB |
1498 | |
1499 | static void copy_picture_range(Picture **to, Picture **from, int count, | |
2c541554 AK |
1500 | H264Context *new_base, |
1501 | H264Context *old_base) | |
6a9c8594 AS |
1502 | { |
1503 | int i; | |
1504 | ||
e5d40372 | 1505 | for (i = 0; i < count; i++) { |
6a9c8594 | 1506 | assert((IN_RANGE(from[i], old_base, sizeof(*old_base)) || |
2c541554 | 1507 | IN_RANGE(from[i], old_base->DPB, |
759001c5 | 1508 | sizeof(Picture) * MAX_PICTURE_COUNT) || |
6a9c8594 AS |
1509 | !from[i])); |
1510 | to[i] = REBASE_PICTURE(from[i], new_base, old_base); | |
1511 | } | |
1512 | } | |
1513 | ||
1514 | static void copy_parameter_set(void **to, void **from, int count, int size) | |
1515 | { | |
1516 | int i; | |
1517 | ||
e5d40372 DB |
1518 | for (i = 0; i < count; i++) { |
1519 | if (to[i] && !from[i]) | |
1520 | av_freep(&to[i]); | |
1521 | else if (from[i] && !to[i]) | |
1522 | to[i] = av_malloc(size); | |
6a9c8594 | 1523 | |
e5d40372 DB |
1524 | if (from[i]) |
1525 | memcpy(to[i], from[i], size); | |
6a9c8594 AS |
1526 | } |
1527 | } | |
1528 | ||
e5d40372 DB |
1529 | static int decode_init_thread_copy(AVCodecContext *avctx) |
1530 | { | |
1531 | H264Context *h = avctx->priv_data; | |
6a9c8594 | 1532 | |
f3a29b75 JR |
1533 | if (!avctx->internal->is_copy) |
1534 | return 0; | |
6a9c8594 AS |
1535 | memset(h->sps_buffers, 0, sizeof(h->sps_buffers)); |
1536 | memset(h->pps_buffers, 0, sizeof(h->pps_buffers)); | |
1537 | ||
2c541554 | 1538 | h->context_initialized = 0; |
0eae920c | 1539 | |
6a9c8594 AS |
1540 | return 0; |
1541 | } | |
1542 | ||
e5d40372 DB |
1543 | #define copy_fields(to, from, start_field, end_field) \ |
1544 | memcpy(&to->start_field, &from->start_field, \ | |
1545 | (char *)&to->end_field - (char *)&to->start_field) | |
1546 | ||
9e696d2e JG |
1547 | static int h264_slice_header_init(H264Context *, int); |
1548 | ||
1549 | static int h264_set_parameter_from_sps(H264Context *h); | |
1550 | ||
e5d40372 DB |
1551 | static int decode_update_thread_context(AVCodecContext *dst, |
1552 | const AVCodecContext *src) | |
1553 | { | |
1554 | H264Context *h = dst->priv_data, *h1 = src->priv_data; | |
2c541554 AK |
1555 | int inited = h->context_initialized, err = 0; |
1556 | int context_reinitialized = 0; | |
759001c5 | 1557 | int i, ret; |
6a9c8594 | 1558 | |
2c541554 | 1559 | if (dst == src || !h1->context_initialized) |
e5d40372 | 1560 | return 0; |
6a9c8594 | 1561 | |
9e696d2e | 1562 | if (inited && |
2c541554 AK |
1563 | (h->width != h1->width || |
1564 | h->height != h1->height || | |
1565 | h->mb_width != h1->mb_width || | |
1566 | h->mb_height != h1->mb_height || | |
9e696d2e JG |
1567 | h->sps.bit_depth_luma != h1->sps.bit_depth_luma || |
1568 | h->sps.chroma_format_idc != h1->sps.chroma_format_idc || | |
1569 | h->sps.colorspace != h1->sps.colorspace)) { | |
1570 | ||
a157c7f2 JG |
1571 | /* set bits_per_raw_sample to the previous value. the check for changed |
1572 | * bit depth in h264_set_parameter_from_sps() uses it and sets it to | |
1573 | * the current value */ | |
1574 | h->avctx->bits_per_raw_sample = h->sps.bit_depth_luma; | |
1575 | ||
9e696d2e JG |
1576 | av_freep(&h->bipred_scratchpad); |
1577 | ||
2c541554 AK |
1578 | h->width = h1->width; |
1579 | h->height = h1->height; | |
1580 | h->mb_height = h1->mb_height; | |
1581 | h->mb_width = h1->mb_width; | |
1582 | h->mb_num = h1->mb_num; | |
1583 | h->mb_stride = h1->mb_stride; | |
9e696d2e JG |
1584 | h->b_stride = h1->b_stride; |
1585 | ||
1586 | if ((err = h264_slice_header_init(h, 1)) < 0) { | |
2c541554 | 1587 | av_log(h->avctx, AV_LOG_ERROR, "h264_slice_header_init() failed"); |
9e696d2e JG |
1588 | return err; |
1589 | } | |
2c541554 | 1590 | context_reinitialized = 1; |
9e696d2e | 1591 | |
2c541554 | 1592 | /* update linesize on resize. The decoder doesn't |
25408b2a | 1593 | * necessarily call h264_frame_start in the new thread */ |
2c541554 AK |
1594 | h->linesize = h1->linesize; |
1595 | h->uvlinesize = h1->uvlinesize; | |
9e696d2e JG |
1596 | |
1597 | /* copy block_offset since frame_start may not be called */ | |
1598 | memcpy(h->block_offset, h1->block_offset, sizeof(h->block_offset)); | |
9e696d2e JG |
1599 | } |
1600 | ||
e5d40372 DB |
1601 | if (!inited) { |
1602 | for (i = 0; i < MAX_SPS_COUNT; i++) | |
6a9c8594 AS |
1603 | av_freep(h->sps_buffers + i); |
1604 | ||
e5d40372 | 1605 | for (i = 0; i < MAX_PPS_COUNT; i++) |
6a9c8594 AS |
1606 | av_freep(h->pps_buffers + i); |
1607 | ||
2c541554 | 1608 | memcpy(h, h1, sizeof(*h1)); |
6a9c8594 AS |
1609 | memset(h->sps_buffers, 0, sizeof(h->sps_buffers)); |
1610 | memset(h->pps_buffers, 0, sizeof(h->pps_buffers)); | |
2c541554 AK |
1611 | memset(&h->er, 0, sizeof(h->er)); |
1612 | memset(&h->me, 0, sizeof(h->me)); | |
1613 | h->context_initialized = 0; | |
1614 | ||
759001c5 AK |
1615 | memset(&h->cur_pic, 0, sizeof(h->cur_pic)); |
1616 | avcodec_get_frame_defaults(&h->cur_pic.f); | |
1617 | h->cur_pic.tf.f = &h->cur_pic.f; | |
2c541554 AK |
1618 | |
1619 | h->avctx = dst; | |
1620 | h->DPB = NULL; | |
759001c5 AK |
1621 | h->qscale_table_pool = NULL; |
1622 | h->mb_type_pool = NULL; | |
1623 | h->ref_index_pool = NULL; | |
1624 | h->motion_val_pool = NULL; | |
2c541554 | 1625 | |
bac3ab13 DB |
1626 | if (ff_h264_alloc_tables(h) < 0) { |
1627 | av_log(dst, AV_LOG_ERROR, "Could not allocate memory for h264\n"); | |
1628 | return AVERROR(ENOMEM); | |
1629 | } | |
6a9c8594 AS |
1630 | context_init(h); |
1631 | ||
e5d40372 DB |
1632 | for (i = 0; i < 2; i++) { |
1633 | h->rbsp_buffer[i] = NULL; | |
6a9c8594 AS |
1634 | h->rbsp_buffer_size[i] = 0; |
1635 | } | |
a394959b | 1636 | h->bipred_scratchpad = NULL; |
2c541554 | 1637 | h->edge_emu_buffer = NULL; |
6a9c8594 AS |
1638 | |
1639 | h->thread_context[0] = h; | |
1640 | ||
2c541554 AK |
1641 | h->dsp.clear_blocks(h->mb); |
1642 | h->dsp.clear_blocks(h->mb + (24 * 16 << h->pixel_shift)); | |
1643 | h->context_initialized = 1; | |
6a9c8594 AS |
1644 | } |
1645 | ||
2c541554 AK |
1646 | h->avctx->coded_height = h1->avctx->coded_height; |
1647 | h->avctx->coded_width = h1->avctx->coded_width; | |
1648 | h->avctx->width = h1->avctx->width; | |
1649 | h->avctx->height = h1->avctx->height; | |
1650 | h->coded_picture_number = h1->coded_picture_number; | |
1651 | h->first_field = h1->first_field; | |
1652 | h->picture_structure = h1->picture_structure; | |
1653 | h->qscale = h1->qscale; | |
1654 | h->droppable = h1->droppable; | |
1655 | h->data_partitioning = h1->data_partitioning; | |
1656 | h->low_delay = h1->low_delay; | |
1657 | ||
759001c5 AK |
1658 | for (i = 0; i < MAX_PICTURE_COUNT; i++) { |
1659 | unref_picture(h, &h->DPB[i]); | |
1660 | if (h1->DPB[i].f.data[0] && | |
1661 | (ret = ref_picture(h, &h->DPB[i], &h1->DPB[i])) < 0) | |
1662 | return ret; | |
1663 | } | |
2c541554 AK |
1664 | |
1665 | h->cur_pic_ptr = REBASE_PICTURE(h1->cur_pic_ptr, h, h1); | |
759001c5 AK |
1666 | unref_picture(h, &h->cur_pic); |
1667 | if ((ret = ref_picture(h, &h->cur_pic, &h1->cur_pic)) < 0) | |
1668 | return ret; | |
2c541554 AK |
1669 | |
1670 | h->workaround_bugs = h1->workaround_bugs; | |
1671 | h->low_delay = h1->low_delay; | |
1672 | h->droppable = h1->droppable; | |
1673 | ||
a394959b JG |
1674 | /* frame_start may not be called for the next thread (if it's decoding |
1675 | * a bottom field) so this has to be allocated here */ | |
2c541554 AK |
1676 | err = alloc_scratch_buffers(h, h1->linesize); |
1677 | if (err < 0) | |
1678 | return err; | |
a394959b | 1679 | |
e5d40372 DB |
1680 | // extradata/NAL handling |
1681 | h->is_avc = h1->is_avc; | |
6a9c8594 | 1682 | |
e5d40372 DB |
1683 | // SPS/PPS |
1684 | copy_parameter_set((void **)h->sps_buffers, (void **)h1->sps_buffers, | |
1685 | MAX_SPS_COUNT, sizeof(SPS)); | |
1686 | h->sps = h1->sps; | |
1687 | copy_parameter_set((void **)h->pps_buffers, (void **)h1->pps_buffers, | |
1688 | MAX_PPS_COUNT, sizeof(PPS)); | |
1689 | h->pps = h1->pps; | |
6a9c8594 | 1690 | |
e5d40372 DB |
1691 | // Dequantization matrices |
1692 | // FIXME these are big - can they be only copied when PPS changes? | |
6a9c8594 AS |
1693 | copy_fields(h, h1, dequant4_buffer, dequant4_coeff); |
1694 | ||
e5d40372 DB |
1695 | for (i = 0; i < 6; i++) |
1696 | h->dequant4_coeff[i] = h->dequant4_buffer[0] + | |
1697 | (h1->dequant4_coeff[i] - h1->dequant4_buffer[0]); | |
6a9c8594 | 1698 | |
e5d40372 DB |
1699 | for (i = 0; i < 6; i++) |
1700 | h->dequant8_coeff[i] = h->dequant8_buffer[0] + | |
1701 | (h1->dequant8_coeff[i] - h1->dequant8_buffer[0]); | |
6a9c8594 AS |
1702 | |
1703 | h->dequant_coeff_pps = h1->dequant_coeff_pps; | |
1704 | ||
e5d40372 | 1705 | // POC timing |
6a9c8594 AS |
1706 | copy_fields(h, h1, poc_lsb, redundant_pic_count); |
1707 | ||
e5d40372 | 1708 | // reference lists |
6a9c8594 AS |
1709 | copy_fields(h, h1, short_ref, cabac_init_idc); |
1710 | ||
2c541554 AK |
1711 | copy_picture_range(h->short_ref, h1->short_ref, 32, h, h1); |
1712 | copy_picture_range(h->long_ref, h1->long_ref, 32, h, h1); | |
e5d40372 | 1713 | copy_picture_range(h->delayed_pic, h1->delayed_pic, |
2c541554 | 1714 | MAX_DELAYED_PIC_COUNT + 2, h, h1); |
6a9c8594 AS |
1715 | |
1716 | h->last_slice_type = h1->last_slice_type; | |
1717 | ||
2c541554 AK |
1718 | if (context_reinitialized) |
1719 | h264_set_parameter_from_sps(h); | |
1720 | ||
1721 | if (!h->cur_pic_ptr) | |
e5d40372 | 1722 | return 0; |
6a9c8594 | 1723 | |
2c541554 | 1724 | if (!h->droppable) { |
12fe7594 | 1725 | err = ff_h264_execute_ref_pic_marking(h, h->mmco, h->mmco_index); |
e5d40372 DB |
1726 | h->prev_poc_msb = h->poc_msb; |
1727 | h->prev_poc_lsb = h->poc_lsb; | |
6a9c8594 | 1728 | } |
e5d40372 DB |
1729 | h->prev_frame_num_offset = h->frame_num_offset; |
1730 | h->prev_frame_num = h->frame_num; | |
1731 | h->outputed_poc = h->next_outputed_poc; | |
6a9c8594 | 1732 | |
12fe7594 | 1733 | return err; |
6a9c8594 AS |
1734 | } |
1735 | ||
25408b2a | 1736 | static int h264_frame_start(H264Context *h) |
e5d40372 | 1737 | { |
2c541554 AK |
1738 | Picture *pic; |
1739 | int i, ret; | |
6e3ef511 | 1740 | const int pixel_shift = h->pixel_shift; |
0da71265 | 1741 | |
2c541554 AK |
1742 | release_unused_pictures(h, 1); |
1743 | h->cur_pic_ptr = NULL; | |
1744 | ||
1745 | i = find_unused_picture(h); | |
1746 | if (i < 0) { | |
1747 | av_log(h->avctx, AV_LOG_ERROR, "no frame buffer available\n"); | |
1748 | return i; | |
1749 | } | |
1750 | pic = &h->DPB[i]; | |
1751 | ||
759001c5 | 1752 | pic->reference = h->droppable ? 0 : h->picture_structure; |
2c541554 AK |
1753 | pic->f.coded_picture_number = h->coded_picture_number++; |
1754 | pic->field_picture = h->picture_structure != PICT_FRAME; | |
3a22d7fa | 1755 | /* |
2c541554 AK |
1756 | * Zero key_frame here; IDR markings per slice in frame or fields are ORed |
1757 | * in later. | |
3a22d7fa JD |
1758 | * See decode_nal_units(). |
1759 | */ | |
2c541554 AK |
1760 | pic->f.key_frame = 0; |
1761 | pic->mmco_reset = 0; | |
1762 | ||
1763 | if ((ret = alloc_picture(h, pic)) < 0) | |
1764 | return ret; | |
1765 | ||
1766 | h->cur_pic_ptr = pic; | |
759001c5 AK |
1767 | unref_picture(h, &h->cur_pic); |
1768 | if ((ret = ref_picture(h, &h->cur_pic, h->cur_pic_ptr)) < 0) | |
1769 | return ret; | |
2c541554 AK |
1770 | |
1771 | ff_er_frame_start(&h->er); | |
0da71265 | 1772 | |
2c541554 | 1773 | assert(h->linesize && h->uvlinesize); |
0da71265 | 1774 | |
e5d40372 | 1775 | for (i = 0; i < 16; i++) { |
2c541554 AK |
1776 | h->block_offset[i] = (4 * ((scan8[i] - scan8[0]) & 7) << pixel_shift) + 4 * h->linesize * ((scan8[i] - scan8[0]) >> 3); |
1777 | h->block_offset[48 + i] = (4 * ((scan8[i] - scan8[0]) & 7) << pixel_shift) + 8 * h->linesize * ((scan8[i] - scan8[0]) >> 3); | |
0da71265 | 1778 | } |
e5d40372 DB |
1779 | for (i = 0; i < 16; i++) { |
1780 | h->block_offset[16 + i] = | |
2c541554 | 1781 | h->block_offset[32 + i] = (4 * ((scan8[i] - scan8[0]) & 7) << pixel_shift) + 4 * h->uvlinesize * ((scan8[i] - scan8[0]) >> 3); |
e5d40372 | 1782 | h->block_offset[48 + 16 + i] = |
2c541554 | 1783 | h->block_offset[48 + 32 + i] = (4 * ((scan8[i] - scan8[0]) & 7) << pixel_shift) + 8 * h->uvlinesize * ((scan8[i] - scan8[0]) >> 3); |
0da71265 MN |
1784 | } |
1785 | ||
934b0821 LM |
1786 | /* can't be in alloc_tables because linesize isn't known there. |
1787 | * FIXME: redo bipred weight to not require extra buffer? */ | |
2c541554 AK |
1788 | for (i = 0; i < h->slice_context_count; i++) |
1789 | if (h->thread_context[i]) { | |
1790 | ret = alloc_scratch_buffers(h->thread_context[i], h->linesize); | |
1791 | if (ret < 0) | |
1792 | return ret; | |
1793 | } | |
e5d40372 DB |
1794 | |
1795 | /* Some macroblocks can be accessed before they're available in case | |
1796 | * of lost slices, MBAFF or threading. */ | |
1797 | memset(h->slice_table, -1, | |
2c541554 | 1798 | (h->mb_height * h->mb_stride - 1) * sizeof(*h->slice_table)); |
e5d40372 DB |
1799 | |
1800 | // s->decode = (s->flags & CODEC_FLAG_PSNR) || !s->encoding || | |
1801 | // s->current_picture.f.reference /* || h->contains_intra */ || 1; | |
1802 | ||
1803 | /* We mark the current picture as non-reference after allocating it, so | |
1804 | * that if we break out due to an error it can be released automatically | |
1805 | * in the next ff_MPV_frame_start(). | |
f08fefc4 AK |
1806 | */ |
1807 | h->cur_pic_ptr->reference = 0; | |
357282c6 | 1808 | |
2c541554 | 1809 | h->cur_pic_ptr->field_poc[0] = h->cur_pic_ptr->field_poc[1] = INT_MAX; |
6a9c8594 AS |
1810 | |
1811 | h->next_output_pic = NULL; | |
1812 | ||
2c541554 | 1813 | assert(h->cur_pic_ptr->long_ref == 0); |
357282c6 | 1814 | |
af8aa846 | 1815 | return 0; |
0da71265 MN |
1816 | } |
1817 | ||
6a9c8594 | 1818 | /** |
e5d40372 DB |
1819 | * Run setup operations that must be run after slice header decoding. |
1820 | * This includes finding the next displayed frame. | |
1821 | * | |
1822 | * @param h h264 master context | |
1823 | * @param setup_finished enough NALs have been read that we can call | |
1824 | * ff_thread_finish_setup() | |
1825 | */ | |
1826 | static void decode_postinit(H264Context *h, int setup_finished) | |
1827 | { | |
2c541554 AK |
1828 | Picture *out = h->cur_pic_ptr; |
1829 | Picture *cur = h->cur_pic_ptr; | |
6a9c8594 | 1830 | int i, pics, out_of_order, out_idx; |
adedd840 | 1831 | int invalid = 0, cnt = 0; |
6a9c8594 | 1832 | |
2c541554 | 1833 | h->cur_pic_ptr->f.pict_type = h->pict_type; |
6a9c8594 | 1834 | |
e5d40372 DB |
1835 | if (h->next_output_pic) |
1836 | return; | |
6a9c8594 | 1837 | |
e5d40372 DB |
1838 | if (cur->field_poc[0] == INT_MAX || cur->field_poc[1] == INT_MAX) { |
1839 | /* FIXME: if we have two PAFF fields in one packet, we can't start | |
1840 | * the next thread here. If we have one field per packet, we can. | |
1841 | * The check in decode_nal_units() is not good enough to find this | |
1842 | * yet, so we assume the worst for now. */ | |
1843 | // if (setup_finished) | |
2c541554 | 1844 | // ff_thread_finish_setup(h->avctx); |
6a9c8594 AS |
1845 | return; |
1846 | } | |
1847 | ||
657ccb5a DB |
1848 | cur->f.interlaced_frame = 0; |
1849 | cur->f.repeat_pict = 0; | |
6a9c8594 AS |
1850 | |
1851 | /* Signal interlacing information externally. */ | |
e5d40372 DB |
1852 | /* Prioritize picture timing SEI information over used |
1853 | * decoding process if it exists. */ | |
6a9c8594 | 1854 | |
e5d40372 DB |
1855 | if (h->sps.pic_struct_present_flag) { |
1856 | switch (h->sei_pic_struct) { | |
6a9c8594 AS |
1857 | case SEI_PIC_STRUCT_FRAME: |
1858 | break; | |
1859 | case SEI_PIC_STRUCT_TOP_FIELD: | |
1860 | case SEI_PIC_STRUCT_BOTTOM_FIELD: | |
657ccb5a | 1861 | cur->f.interlaced_frame = 1; |
6a9c8594 AS |
1862 | break; |
1863 | case SEI_PIC_STRUCT_TOP_BOTTOM: | |
1864 | case SEI_PIC_STRUCT_BOTTOM_TOP: | |
a6931d8e | 1865 | if (FIELD_OR_MBAFF_PICTURE(h)) |
657ccb5a | 1866 | cur->f.interlaced_frame = 1; |
6a9c8594 AS |
1867 | else |
1868 | // try to flag soft telecine progressive | |
657ccb5a | 1869 | cur->f.interlaced_frame = h->prev_interlaced_frame; |
6a9c8594 AS |
1870 | break; |
1871 | case SEI_PIC_STRUCT_TOP_BOTTOM_TOP: | |
1872 | case SEI_PIC_STRUCT_BOTTOM_TOP_BOTTOM: | |
e5d40372 DB |
1873 | /* Signal the possibility of telecined film externally |
1874 | * (pic_struct 5,6). From these hints, let the applications | |
1875 | * decide if they apply deinterlacing. */ | |
657ccb5a | 1876 | cur->f.repeat_pict = 1; |
6a9c8594 AS |
1877 | break; |
1878 | case SEI_PIC_STRUCT_FRAME_DOUBLING: | |
657ccb5a | 1879 | cur->f.repeat_pict = 2; |
6a9c8594 AS |
1880 | break; |
1881 | case SEI_PIC_STRUCT_FRAME_TRIPLING: | |
657ccb5a | 1882 | cur->f.repeat_pict = 4; |
6a9c8594 AS |
1883 | break; |
1884 | } | |
1885 | ||
e5d40372 DB |
1886 | if ((h->sei_ct_type & 3) && |
1887 | h->sei_pic_struct <= SEI_PIC_STRUCT_BOTTOM_TOP) | |
657ccb5a | 1888 | cur->f.interlaced_frame = (h->sei_ct_type & (1 << 1)) != 0; |
e5d40372 | 1889 | } else { |
6a9c8594 | 1890 | /* Derive interlacing flag from used decoding process. */ |
a6931d8e | 1891 | cur->f.interlaced_frame = FIELD_OR_MBAFF_PICTURE(h); |
6a9c8594 | 1892 | } |
657ccb5a | 1893 | h->prev_interlaced_frame = cur->f.interlaced_frame; |
6a9c8594 | 1894 | |
e5d40372 | 1895 | if (cur->field_poc[0] != cur->field_poc[1]) { |
6a9c8594 | 1896 | /* Derive top_field_first from field pocs. */ |
657ccb5a | 1897 | cur->f.top_field_first = cur->field_poc[0] < cur->field_poc[1]; |
e5d40372 | 1898 | } else { |
657ccb5a | 1899 | if (cur->f.interlaced_frame || h->sps.pic_struct_present_flag) { |
e5d40372 DB |
1900 | /* Use picture timing SEI information. Even if it is a |
1901 | * information of a past frame, better than nothing. */ | |
1902 | if (h->sei_pic_struct == SEI_PIC_STRUCT_TOP_BOTTOM || | |
1903 | h->sei_pic_struct == SEI_PIC_STRUCT_TOP_BOTTOM_TOP) | |
657ccb5a | 1904 | cur->f.top_field_first = 1; |
6a9c8594 | 1905 | else |
657ccb5a | 1906 | cur->f.top_field_first = 0; |
e5d40372 | 1907 | } else { |
6a9c8594 | 1908 | /* Most likely progressive */ |
657ccb5a | 1909 | cur->f.top_field_first = 0; |
6a9c8594 AS |
1910 | } |
1911 | } | |
1912 | ||
e5d40372 | 1913 | // FIXME do something with unavailable reference frames |
6a9c8594 AS |
1914 | |
1915 | /* Sort B-frames into display order */ | |
1916 | ||
e5d40372 | 1917 | if (h->sps.bitstream_restriction_flag && |
2c541554 AK |
1918 | h->avctx->has_b_frames < h->sps.num_reorder_frames) { |
1919 | h->avctx->has_b_frames = h->sps.num_reorder_frames; | |
1920 | h->low_delay = 0; | |
6a9c8594 AS |
1921 | } |
1922 | ||
2c541554 | 1923 | if (h->avctx->strict_std_compliance >= FF_COMPLIANCE_STRICT && |
e5d40372 | 1924 | !h->sps.bitstream_restriction_flag) { |
2c541554 AK |
1925 | h->avctx->has_b_frames = MAX_DELAYED_PIC_COUNT - 1; |
1926 | h->low_delay = 0; | |
6a9c8594 AS |
1927 | } |
1928 | ||
1929 | pics = 0; | |
e5d40372 DB |
1930 | while (h->delayed_pic[pics]) |
1931 | pics++; | |
6a9c8594 AS |
1932 | |
1933 | assert(pics <= MAX_DELAYED_PIC_COUNT); | |
1934 | ||
1935 | h->delayed_pic[pics++] = cur; | |
759001c5 AK |
1936 | if (cur->reference == 0) |
1937 | cur->reference = DELAYED_PIC_REF; | |
6a9c8594 | 1938 | |
adedd840 RB |
1939 | /* Frame reordering. This code takes pictures from coding order and sorts |
1940 | * them by their incremental POC value into display order. It supports POC | |
1941 | * gaps, MMCO reset codes and random resets. | |
1942 | * A "display group" can start either with a IDR frame (f.key_frame = 1), | |
1943 | * and/or can be closed down with a MMCO reset code. In sequences where | |
1944 | * there is no delay, we can't detect that (since the frame was already | |
1945 | * output to the user), so we also set h->mmco_reset to detect the MMCO | |
1946 | * reset code. | |
2c541554 | 1947 | * FIXME: if we detect insufficient delays (as per h->avctx->has_b_frames), |
adedd840 RB |
1948 | * we increase the delay between input and output. All frames affected by |
1949 | * the lag (e.g. those that should have been output before another frame | |
1950 | * that we already returned to the user) will be dropped. This is a bug | |
1951 | * that we will fix later. */ | |
1952 | for (i = 0; i < MAX_DELAYED_PIC_COUNT; i++) { | |
1953 | cnt += out->poc < h->last_pocs[i]; | |
1954 | invalid += out->poc == INT_MIN; | |
1955 | } | |
e5d40372 DB |
1956 | if (!h->mmco_reset && !cur->f.key_frame && |
1957 | cnt + invalid == MAX_DELAYED_PIC_COUNT && cnt > 0) { | |
adedd840 RB |
1958 | h->mmco_reset = 2; |
1959 | if (pics > 1) | |
1960 | h->delayed_pic[pics - 2]->mmco_reset = 2; | |
1961 | } | |
1962 | if (h->mmco_reset || cur->f.key_frame) { | |
1963 | for (i = 0; i < MAX_DELAYED_PIC_COUNT; i++) | |
1964 | h->last_pocs[i] = INT_MIN; | |
1965 | cnt = 0; | |
1966 | invalid = MAX_DELAYED_PIC_COUNT; | |
1967 | } | |
e5d40372 | 1968 | out = h->delayed_pic[0]; |
6a9c8594 | 1969 | out_idx = 0; |
e5d40372 DB |
1970 | for (i = 1; i < MAX_DELAYED_PIC_COUNT && |
1971 | h->delayed_pic[i] && | |
1972 | !h->delayed_pic[i - 1]->mmco_reset && | |
1973 | !h->delayed_pic[i]->f.key_frame; | |
1974 | i++) | |
1975 | if (h->delayed_pic[i]->poc < out->poc) { | |
1976 | out = h->delayed_pic[i]; | |
6a9c8594 AS |
1977 | out_idx = i; |
1978 | } | |
2c541554 | 1979 | if (h->avctx->has_b_frames == 0 && |
e5d40372 | 1980 | (h->delayed_pic[0]->f.key_frame || h->mmco_reset)) |
adedd840 | 1981 | h->next_outputed_poc = INT_MIN; |
e5d40372 DB |
1982 | out_of_order = !out->f.key_frame && !h->mmco_reset && |
1983 | (out->poc < h->next_outputed_poc); | |
6a9c8594 | 1984 | |
e5d40372 | 1985 | if (h->sps.bitstream_restriction_flag && |
2c541554 AK |
1986 | h->avctx->has_b_frames >= h->sps.num_reorder_frames) { |
1987 | } else if (out_of_order && pics - 1 == h->avctx->has_b_frames && | |
1988 | h->avctx->has_b_frames < MAX_DELAYED_PIC_COUNT) { | |
ea2bb12e | 1989 | if (invalid + cnt < MAX_DELAYED_PIC_COUNT) { |
2c541554 | 1990 | h->avctx->has_b_frames = FFMAX(h->avctx->has_b_frames, cnt); |
ea2bb12e | 1991 | } |
2c541554 AK |
1992 | h->low_delay = 0; |
1993 | } else if (h->low_delay && | |
e5d40372 DB |
1994 | ((h->next_outputed_poc != INT_MIN && |
1995 | out->poc > h->next_outputed_poc + 2) || | |
ea2bb12e | 1996 | cur->f.pict_type == AV_PICTURE_TYPE_B)) { |
2c541554 AK |
1997 | h->low_delay = 0; |
1998 | h->avctx->has_b_frames++; | |
6a9c8594 AS |
1999 | } |
2000 | ||
2c541554 | 2001 | if (pics > h->avctx->has_b_frames) { |
759001c5 | 2002 | out->reference &= ~DELAYED_PIC_REF; |
e5d40372 DB |
2003 | // for frame threading, the owner must be the second field's thread or |
2004 | // else the first thread can release the picture and reuse it unsafely | |
e5d40372 DB |
2005 | for (i = out_idx; h->delayed_pic[i]; i++) |
2006 | h->delayed_pic[i] = h->delayed_pic[i + 1]; | |
2007 | } | |
2008 | memmove(h->last_pocs, &h->last_pocs[1], | |
2009 | sizeof(*h->last_pocs) * (MAX_DELAYED_PIC_COUNT - 1)); | |
adedd840 | 2010 | h->last_pocs[MAX_DELAYED_PIC_COUNT - 1] = cur->poc; |
2c541554 | 2011 | if (!out_of_order && pics > h->avctx->has_b_frames) { |
6a9c8594 | 2012 | h->next_output_pic = out; |
adedd840 RB |
2013 | if (out->mmco_reset) { |
2014 | if (out_idx > 0) { | |
e5d40372 | 2015 | h->next_outputed_poc = out->poc; |
adedd840 RB |
2016 | h->delayed_pic[out_idx - 1]->mmco_reset = out->mmco_reset; |
2017 | } else { | |
2018 | h->next_outputed_poc = INT_MIN; | |
2019 | } | |
2020 | } else { | |
0b4c3232 RB |
2021 | if (out_idx == 0 && pics > 1 && h->delayed_pic[0]->f.key_frame) { |
2022 | h->next_outputed_poc = INT_MIN; | |
2023 | } else { | |
2024 | h->next_outputed_poc = out->poc; | |
2025 | } | |
adedd840 RB |
2026 | } |
2027 | h->mmco_reset = 0; | |
e5d40372 | 2028 | } else { |
2c541554 | 2029 | av_log(h->avctx, AV_LOG_DEBUG, "no picture\n"); |
6a9c8594 AS |
2030 | } |
2031 | ||
2032 | if (setup_finished) | |
2c541554 | 2033 | ff_thread_finish_setup(h->avctx); |
6a9c8594 AS |
2034 | } |
2035 | ||
76741b0e BC |
2036 | static av_always_inline void backup_mb_border(H264Context *h, uint8_t *src_y, |
2037 | uint8_t *src_cb, uint8_t *src_cr, | |
e5d40372 DB |
2038 | int linesize, int uvlinesize, |
2039 | int simple) | |
76741b0e | 2040 | { |
0b69d625 | 2041 | uint8_t *top_border; |
5f7f9719 | 2042 | int top_idx = 1; |
6e3ef511 | 2043 | const int pixel_shift = h->pixel_shift; |
23e85be5 | 2044 | int chroma444 = CHROMA444(h); |
e962bd08 | 2045 | int chroma422 = CHROMA422(h); |
115329f1 | 2046 | |
e5d40372 | 2047 | src_y -= linesize; |
53c05b1e MN |
2048 | src_cb -= uvlinesize; |
2049 | src_cr -= uvlinesize; | |
2050 | ||
7bece9b2 | 2051 | if (!simple && FRAME_MBAFF(h)) { |
2c541554 | 2052 | if (h->mb_y & 1) { |
82313eaa | 2053 | if (!MB_MBAFF(h)) { |
2c541554 | 2054 | top_border = h->top_borders[0][h->mb_x]; |
e5d40372 | 2055 | AV_COPY128(top_border, src_y + 15 * linesize); |
6e3ef511 | 2056 | if (pixel_shift) |
e5d40372 | 2057 | AV_COPY128(top_border + 16, src_y + 15 * linesize + 16); |
2c541554 | 2058 | if (simple || !CONFIG_GRAY || !(h->flags & CODEC_FLAG_GRAY)) { |
e5d40372 DB |
2059 | if (chroma444) { |
2060 | if (pixel_shift) { | |
2061 | AV_COPY128(top_border + 32, src_cb + 15 * uvlinesize); | |
2062 | AV_COPY128(top_border + 48, src_cb + 15 * uvlinesize + 16); | |
2063 | AV_COPY128(top_border + 64, src_cr + 15 * uvlinesize); | |
2064 | AV_COPY128(top_border + 80, src_cr + 15 * uvlinesize + 16); | |
c90b9442 | 2065 | } else { |
e5d40372 DB |
2066 | AV_COPY128(top_border + 16, src_cb + 15 * uvlinesize); |
2067 | AV_COPY128(top_border + 32, src_cr + 15 * uvlinesize); | |
c90b9442 | 2068 | } |
e5d40372 | 2069 | } else if (chroma422) { |
76741b0e | 2070 | if (pixel_shift) { |
e5d40372 DB |
2071 | AV_COPY128(top_border + 32, src_cb + 15 * uvlinesize); |
2072 | AV_COPY128(top_border + 48, src_cr + 15 * uvlinesize); | |
76741b0e | 2073 | } else { |
e5d40372 DB |
2074 | AV_COPY64(top_border + 16, src_cb + 15 * uvlinesize); |
2075 | AV_COPY64(top_border + 24, src_cr + 15 * uvlinesize); | |
76741b0e | 2076 | } |
6e3ef511 | 2077 | } else { |
c90b9442 | 2078 | if (pixel_shift) { |
e5d40372 DB |
2079 | AV_COPY128(top_border + 32, src_cb + 7 * uvlinesize); |
2080 | AV_COPY128(top_border + 48, src_cr + 7 * uvlinesize); | |
c90b9442 | 2081 | } else { |
e5d40372 DB |
2082 | AV_COPY64(top_border + 16, src_cb + 7 * uvlinesize); |
2083 | AV_COPY64(top_border + 24, src_cr + 7 * uvlinesize); | |
c90b9442 | 2084 | } |
6e3ef511 | 2085 | } |
5f7f9719 MN |
2086 | } |
2087 | } | |
82313eaa | 2088 | } else if (MB_MBAFF(h)) { |
c988f975 | 2089 | top_idx = 0; |
e5d40372 | 2090 | } else |
c988f975 | 2091 | return; |
5f7f9719 MN |
2092 | } |
2093 | ||
2c541554 | 2094 | top_border = h->top_borders[top_idx][h->mb_x]; |
09f21198 | 2095 | /* There are two lines saved, the line above the top macroblock |
e5d40372 DB |
2096 | * of a pair, and the line above the bottom macroblock. */ |
2097 | AV_COPY128(top_border, src_y + 16 * linesize); | |
6e3ef511 | 2098 | if (pixel_shift) |
e5d40372 DB |
2099 | AV_COPY128(top_border + 16, src_y + 16 * linesize + 16); |
2100 | ||
2c541554 | 2101 | if (simple || !CONFIG_GRAY || !(h->flags & CODEC_FLAG_GRAY)) { |
e5d40372 DB |
2102 | if (chroma444) { |
2103 | if (pixel_shift) { | |
2104 | AV_COPY128(top_border + 32, src_cb + 16 * linesize); | |
2105 | AV_COPY128(top_border + 48, src_cb + 16 * linesize + 16); | |
2106 | AV_COPY128(top_border + 64, src_cr + 16 * linesize); | |
2107 | AV_COPY128(top_border + 80, src_cr + 16 * linesize + 16); | |
c90b9442 | 2108 | } else { |
e5d40372 DB |
2109 | AV_COPY128(top_border + 16, src_cb + 16 * linesize); |
2110 | AV_COPY128(top_border + 32, src_cr + 16 * linesize); | |
c90b9442 | 2111 | } |
e5d40372 | 2112 | } else if (chroma422) { |
76741b0e | 2113 | if (pixel_shift) { |
e5d40372 DB |
2114 | AV_COPY128(top_border + 32, src_cb + 16 * uvlinesize); |
2115 | AV_COPY128(top_border + 48, src_cr + 16 * uvlinesize); | |
76741b0e | 2116 | } else { |
e5d40372 DB |
2117 | AV_COPY64(top_border + 16, src_cb + 16 * uvlinesize); |
2118 | AV_COPY64(top_border + 24, src_cr + 16 * uvlinesize); | |
76741b0e | 2119 | } |
6e3ef511 | 2120 | } else { |
c90b9442 | 2121 | if (pixel_shift) { |
e5d40372 DB |
2122 | AV_COPY128(top_border + 32, src_cb + 8 * uvlinesize); |
2123 | AV_COPY128(top_border + 48, src_cr + 8 * uvlinesize); | |
c90b9442 | 2124 | } else { |
e5d40372 DB |
2125 | AV_COPY64(top_border + 16, src_cb + 8 * uvlinesize); |
2126 | AV_COPY64(top_border + 24, src_cr + 8 * uvlinesize); | |
c90b9442 | 2127 | } |
6e3ef511 | 2128 | } |
53c05b1e MN |
2129 | } |
2130 | } | |
2131 | ||
bbdd52ed | 2132 | static av_always_inline void xchg_mb_border(H264Context *h, uint8_t *src_y, |
e5d40372 DB |
2133 | uint8_t *src_cb, uint8_t *src_cr, |
2134 | int linesize, int uvlinesize, | |
2135 | int xchg, int chroma444, | |
2136 | int simple, int pixel_shift) | |
2137 | { | |
4e987f82 | 2138 | int deblock_topleft; |
b69378e2 | 2139 | int deblock_top; |
5f7f9719 | 2140 | int top_idx = 1; |
1e4f1c56 AS |
2141 | uint8_t *top_border_m1; |
2142 | uint8_t *top_border; | |
5f7f9719 | 2143 | |
7bece9b2 | 2144 | if (!simple && FRAME_MBAFF(h)) { |
2c541554 | 2145 | if (h->mb_y & 1) { |
82313eaa | 2146 | if (!MB_MBAFF(h)) |
c988f975 | 2147 | return; |
e5d40372 | 2148 | } else { |
82313eaa | 2149 | top_idx = MB_MBAFF(h) ? 0 : 1; |
5f7f9719 | 2150 | } |
5f7f9719 | 2151 | } |
b69378e2 | 2152 | |
e5d40372 | 2153 | if (h->deblocking_filter == 2) { |
2c541554 | 2154 | deblock_topleft = h->slice_table[h->mb_xy - 1 - h->mb_stride] == h->slice_num; |
4e987f82 | 2155 | deblock_top = h->top_type; |
b69378e2 | 2156 | } else { |
2c541554 | 2157 | deblock_topleft = (h->mb_x > 0); |
da6be8fc | 2158 | deblock_top = (h->mb_y > !!MB_FIELD(h)); |
b69378e2 | 2159 | } |
53c05b1e | 2160 | |
e5d40372 | 2161 | src_y -= linesize + 1 + pixel_shift; |
6e3ef511 OA |
2162 | src_cb -= uvlinesize + 1 + pixel_shift; |
2163 | src_cr -= uvlinesize + 1 + pixel_shift; | |
53c05b1e | 2164 | |
2c541554 AK |
2165 | top_border_m1 = h->top_borders[top_idx][h->mb_x - 1]; |
2166 | top_border = h->top_borders[top_idx][h->mb_x]; | |
1e4f1c56 | 2167 | |
e5d40372 DB |
2168 | #define XCHG(a, b, xchg) \ |
2169 | if (pixel_shift) { \ | |
2170 | if (xchg) { \ | |
2171 | AV_SWAP64(b + 0, a + 0); \ | |
2172 | AV_SWAP64(b + 8, a + 8); \ | |
2173 | } else { \ | |
2174 | AV_COPY128(b, a); \ | |
2175 | } \ | |
2176 | } else if (xchg) \ | |
2177 | AV_SWAP64(b, a); \ | |
2178 | else \ | |
2179 | AV_COPY64(b, a); | |
2180 | ||
2181 | if (deblock_top) { | |
2182 | if (deblock_topleft) { | |
2183 | XCHG(top_border_m1 + (8 << pixel_shift), | |
2184 | src_y - (7 << pixel_shift), 1); | |
c988f975 | 2185 | } |
6e3ef511 OA |
2186 | XCHG(top_border + (0 << pixel_shift), src_y + (1 << pixel_shift), xchg); |
2187 | XCHG(top_border + (8 << pixel_shift), src_y + (9 << pixel_shift), 1); | |
2c541554 AK |
2188 | if (h->mb_x + 1 < h->mb_width) { |
2189 | XCHG(h->top_borders[top_idx][h->mb_x + 1], | |
e5d40372 | 2190 | src_y + (17 << pixel_shift), 1); |
43efd19a | 2191 | } |
53c05b1e | 2192 | } |
2c541554 | 2193 | if (simple || !CONFIG_GRAY || !(h->flags & CODEC_FLAG_GRAY)) { |
e5d40372 DB |
2194 | if (chroma444) { |
2195 | if (deblock_topleft) { | |
c90b9442 JGG |
2196 | XCHG(top_border_m1 + (24 << pixel_shift), src_cb - (7 << pixel_shift), 1); |
2197 | XCHG(top_border_m1 + (40 << pixel_shift), src_cr - (7 << pixel_shift), 1); | |
2198 | } | |
2199 | XCHG(top_border + (16 << pixel_shift), src_cb + (1 << pixel_shift), xchg); | |
2200 | XCHG(top_border + (24 << pixel_shift), src_cb + (9 << pixel_shift), 1); | |
2201 | XCHG(top_border + (32 << pixel_shift), src_cr + (1 << pixel_shift), xchg); | |
2202 | XCHG(top_border + (40 << pixel_shift), src_cr + (9 << pixel_shift), 1); | |
2c541554 AK |
2203 | if (h->mb_x + 1 < h->mb_width) { |
2204 | XCHG(h->top_borders[top_idx][h->mb_x + 1] + (16 << pixel_shift), src_cb + (17 << pixel_shift), 1); | |
2205 | XCHG(h->top_borders[top_idx][h->mb_x + 1] + (32 << pixel_shift), src_cr + (17 << pixel_shift), 1); | |
c90b9442 JGG |
2206 | } |
2207 | } else { | |
e5d40372 DB |
2208 | if (deblock_top) { |
2209 | if (deblock_topleft) { | |
c90b9442 JGG |
2210 | XCHG(top_border_m1 + (16 << pixel_shift), src_cb - (7 << pixel_shift), 1); |
2211 | XCHG(top_border_m1 + (24 << pixel_shift), src_cr - (7 << pixel_shift), 1); | |
2212 | } | |
e5d40372 DB |
2213 | XCHG(top_border + (16 << pixel_shift), src_cb + 1 + pixel_shift, 1); |
2214 | XCHG(top_border + (24 << pixel_shift), src_cr + 1 + pixel_shift, 1); | |
c988f975 | 2215 | } |
53c05b1e | 2216 | } |
53c05b1e MN |
2217 | } |
2218 | } | |
2219 | ||
88bd7fdc | 2220 | static av_always_inline int dctcoef_get(int16_t *mb, int high_bit_depth, |
e5d40372 DB |
2221 | int index) |
2222 | { | |
6e3ef511 | 2223 | if (high_bit_depth) { |
e5d40372 | 2224 | return AV_RN32A(((int32_t *)mb) + index); |
6e3ef511 OA |
2225 | } else |
2226 | return AV_RN16A(mb + index); | |
2227 | } | |
2228 | ||
88bd7fdc | 2229 | static av_always_inline void dctcoef_set(int16_t *mb, int high_bit_depth, |
e5d40372 DB |
2230 | int index, int value) |
2231 | { | |
6e3ef511 | 2232 | if (high_bit_depth) { |
e5d40372 | 2233 | AV_WN32A(((int32_t *)mb) + index, value); |
6e3ef511 OA |
2234 | } else |
2235 | AV_WN16A(mb + index, value); | |
2236 | } | |
2237 | ||
e5d40372 DB |
2238 | static av_always_inline void hl_decode_mb_predict_luma(H264Context *h, |
2239 | int mb_type, int is_h264, | |
2240 | int simple, | |
2241 | int transform_bypass, | |
2242 | int pixel_shift, | |
2243 | int *block_offset, | |
2244 | int linesize, | |
2245 | uint8_t *dest_y, int p) | |
c90b9442 | 2246 | { |
88bd7fdc DB |
2247 | void (*idct_add)(uint8_t *dst, int16_t *block, int stride); |
2248 | void (*idct_dc_add)(uint8_t *dst, int16_t *block, int stride); | |
c90b9442 | 2249 | int i; |
2c541554 | 2250 | int qscale = p == 0 ? h->qscale : h->chroma_qp[p - 1]; |
e5d40372 DB |
2251 | block_offset += 16 * p; |
2252 | if (IS_INTRA4x4(mb_type)) { | |
2491f9ee AK |
2253 | if (IS_8x8DCT(mb_type)) { |
2254 | if (transform_bypass) { | |
2255 | idct_dc_add = | |
2ed00820 | 2256 | idct_add = h->h264dsp.h264_add_pixels8; |
2491f9ee AK |
2257 | } else { |
2258 | idct_dc_add = h->h264dsp.h264_idct8_dc_add; | |
2259 | idct_add = h->h264dsp.h264_idct8_add; | |
2260 | } | |
2261 | for (i = 0; i < 16; i += 4) { | |
2262 | uint8_t *const ptr = dest_y + block_offset[i]; | |
2263 | const int dir = h->intra4x4_pred_mode_cache[scan8[i]]; | |
2264 | if (transform_bypass && h->sps.profile_idc == 244 && dir <= 1) { | |
2265 | h->hpc.pred8x8l_add[dir](ptr, h->mb + (i * 16 + p * 256 << pixel_shift), linesize); | |
e5d40372 | 2266 | } else { |
2491f9ee AK |
2267 | const int nnz = h->non_zero_count_cache[scan8[i + p * 16]]; |
2268 | h->hpc.pred8x8l[dir](ptr, (h->topleft_samples_available << i) & 0x8000, | |
2269 | (h->topright_samples_available << i) & 0x4000, linesize); | |
2270 | if (nnz) { | |
2271 | if (nnz == 1 && dctcoef_get(h->mb, pixel_shift, i * 16 + p * 256)) | |
2272 | idct_dc_add(ptr, h->mb + (i * 16 + p * 256 << pixel_shift), linesize); | |
2273 | else | |
2274 | idct_add(ptr, h->mb + (i * 16 + p * 256 << pixel_shift), linesize); | |
c90b9442 JGG |
2275 | } |
2276 | } | |
2491f9ee AK |
2277 | } |
2278 | } else { | |
2279 | if (transform_bypass) { | |
2280 | idct_dc_add = | |
2ed00820 | 2281 | idct_add = h->h264dsp.h264_add_pixels4; |
e5d40372 | 2282 | } else { |
2491f9ee AK |
2283 | idct_dc_add = h->h264dsp.h264_idct_dc_add; |
2284 | idct_add = h->h264dsp.h264_idct_add; | |
2285 | } | |
2286 | for (i = 0; i < 16; i++) { | |
2287 | uint8_t *const ptr = dest_y + block_offset[i]; | |
2288 | const int dir = h->intra4x4_pred_mode_cache[scan8[i]]; | |
c90b9442 | 2289 | |
2491f9ee AK |
2290 | if (transform_bypass && h->sps.profile_idc == 244 && dir <= 1) { |
2291 | h->hpc.pred4x4_add[dir](ptr, h->mb + (i * 16 + p * 256 << pixel_shift), linesize); | |
2292 | } else { | |
2293 | uint8_t *topright; | |
2294 | int nnz, tr; | |
2295 | uint64_t tr_high; | |
2296 | if (dir == DIAG_DOWN_LEFT_PRED || dir == VERT_LEFT_PRED) { | |
2297 | const int topright_avail = (h->topright_samples_available << i) & 0x8000; | |
2c541554 | 2298 | assert(h->mb_y || linesize <= block_offset[i]); |
2491f9ee AK |
2299 | if (!topright_avail) { |
2300 | if (pixel_shift) { | |
2301 | tr_high = ((uint16_t *)ptr)[3 - linesize / 2] * 0x0001000100010001ULL; | |
2302 | topright = (uint8_t *)&tr_high; | |
2303 | } else { | |
2304 | tr = ptr[3 - linesize] * 0x01010101u; | |
2305 | topright = (uint8_t *)&tr; | |
2306 | } | |
e5d40372 | 2307 | } else |
2491f9ee AK |
2308 | topright = ptr + (4 << pixel_shift) - linesize; |
2309 | } else | |
2310 | topright = NULL; | |
2311 | ||
2312 | h->hpc.pred4x4[dir](ptr, topright, linesize); | |
2313 | nnz = h->non_zero_count_cache[scan8[i + p * 16]]; | |
2314 | if (nnz) { | |
2315 | if (is_h264) { | |
2316 | if (nnz == 1 && dctcoef_get(h->mb, pixel_shift, i * 16 + p * 256)) | |
2317 | idct_dc_add(ptr, h->mb + (i * 16 + p * 256 << pixel_shift), linesize); | |
2318 | else | |
2319 | idct_add(ptr, h->mb + (i * 16 + p * 256 << pixel_shift), linesize); | |
2320 | } else if (CONFIG_SVQ3_DECODER) | |
2321 | ff_svq3_add_idct_c(ptr, h->mb + i * 16 + p * 256, linesize, qscale, 0); | |
c90b9442 JGG |
2322 | } |
2323 | } | |
2324 | } | |
2325 | } | |
e5d40372 DB |
2326 | } else { |
2327 | h->hpc.pred16x16[h->intra16x16_pred_mode](dest_y, linesize); | |
2328 | if (is_h264) { | |
2329 | if (h->non_zero_count_cache[scan8[LUMA_DC_BLOCK_INDEX + p]]) { | |
2330 | if (!transform_bypass) | |
2331 | h->h264dsp.h264_luma_dc_dequant_idct(h->mb + (p * 256 << pixel_shift), | |
2332 | h->mb_luma_dc[p], | |
2333 | h->dequant4_coeff[p][qscale][0]); | |
2334 | else { | |
2335 | static const uint8_t dc_mapping[16] = { | |
2336 | 0 * 16, 1 * 16, 4 * 16, 5 * 16, | |
2337 | 2 * 16, 3 * 16, 6 * 16, 7 * 16, | |
2338 | 8 * 16, 9 * 16, 12 * 16, 13 * 16, | |
2339 | 10 * 16, 11 * 16, 14 * 16, 15 * 16 }; | |
2340 | for (i = 0; i < 16; i++) | |
2341 | dctcoef_set(h->mb + (p * 256 << pixel_shift), | |
2342 | pixel_shift, dc_mapping[i], | |
2343 | dctcoef_get(h->mb_luma_dc[p], | |
2344 | pixel_shift, i)); | |
c90b9442 JGG |
2345 | } |
2346 | } | |
301fb921 | 2347 | } else if (CONFIG_SVQ3_DECODER) |
e5d40372 DB |
2348 | ff_svq3_luma_dc_dequant_idct_c(h->mb + p * 256, |
2349 | h->mb_luma_dc[p], qscale); | |
c90b9442 JGG |
2350 | } |
2351 | } | |
2352 | ||
e5d40372 DB |
2353 | static av_always_inline void hl_decode_mb_idct_luma(H264Context *h, int mb_type, |
2354 | int is_h264, int simple, | |
2355 | int transform_bypass, | |
2356 | int pixel_shift, | |
2357 | int *block_offset, | |
2358 | int linesize, | |
2359 | uint8_t *dest_y, int p) | |
c90b9442 | 2360 | { |
88bd7fdc | 2361 | void (*idct_add)(uint8_t *dst, int16_t *block, int stride); |
c90b9442 | 2362 | int i; |
e5d40372 DB |
2363 | block_offset += 16 * p; |
2364 | if (!IS_INTRA4x4(mb_type)) { | |
2365 | if (is_h264) { | |
2366 | if (IS_INTRA16x16(mb_type)) { | |
2367 | if (transform_bypass) { | |
2368 | if (h->sps.profile_idc == 244 && | |
2369 | (h->intra16x16_pred_mode == VERT_PRED8x8 || | |
2370 | h->intra16x16_pred_mode == HOR_PRED8x8)) { | |
2371 | h->hpc.pred16x16_add[h->intra16x16_pred_mode](dest_y, block_offset, | |
2372 | h->mb + (p * 256 << pixel_shift), | |
2373 | linesize); | |
2374 | } else { | |
2375 | for (i = 0; i < 16; i++) | |
2376 | if (h->non_zero_count_cache[scan8[i + p * 16]] || | |
2377 | dctcoef_get(h->mb, pixel_shift, i * 16 + p * 256)) | |
2ed00820 RB |
2378 | h->h264dsp.h264_add_pixels4(dest_y + block_offset[i], |
2379 | h->mb + (i * 16 + p * 256 << pixel_shift), | |
2380 | linesize); | |
c90b9442 | 2381 | } |
e5d40372 DB |
2382 | } else { |
2383 | h->h264dsp.h264_idct_add16intra(dest_y, block_offset, | |
2384 | h->mb + (p * 256 << pixel_shift), | |
2385 | linesize, | |
2386 | h->non_zero_count_cache + p * 5 * 8); | |
c90b9442 | 2387 | } |
e5d40372 DB |
2388 | } else if (h->cbp & 15) { |
2389 | if (transform_bypass) { | |
c90b9442 | 2390 | const int di = IS_8x8DCT(mb_type) ? 4 : 1; |
2ed00820 RB |
2391 | idct_add = IS_8x8DCT(mb_type) ? h->h264dsp.h264_add_pixels8 |
2392 | : h->h264dsp.h264_add_pixels4; | |
e5d40372 DB |
2393 | for (i = 0; i < 16; i += di) |
2394 | if (h->non_zero_count_cache[scan8[i + p * 16]]) | |
2395 | idct_add(dest_y + block_offset[i], | |
2396 | h->mb + (i * 16 + p * 256 << pixel_shift), | |
2397 | linesize); | |
2398 | } else { | |
2399 | if (IS_8x8DCT(mb_type)) | |
2400 | h->h264dsp.h264_idct8_add4(dest_y, block_offset, | |
2401 | h->mb + (p * 256 << pixel_shift), | |
2402 | linesize, | |
2403 | h->non_zero_count_cache + p * 5 * 8); | |
2404 | else | |
2405 | h->h264dsp.h264_idct_add16(dest_y, block_offset, | |
2406 | h->mb + (p * 256 << pixel_shift), | |
2407 | linesize, | |
2408 | h->non_zero_count_cache + p * 5 * 8); | |
c90b9442 JGG |
2409 | } |
2410 | } | |
301fb921 | 2411 | } else if (CONFIG_SVQ3_DECODER) { |
e5d40372 DB |
2412 | for (i = 0; i < 16; i++) |
2413 | if (h->non_zero_count_cache[scan8[i + p * 16]] || h->mb[i * 16 + p * 256]) { | |
2414 | // FIXME benchmark weird rule, & below | |
2415 | uint8_t *const ptr = dest_y + block_offset[i]; | |
2416 | ff_svq3_add_idct_c(ptr, h->mb + i * 16 + p * 256, linesize, | |
2c541554 | 2417 | h->qscale, IS_INTRA(mb_type) ? 1 : 0); |
c90b9442 | 2418 | } |
c90b9442 JGG |
2419 | } |
2420 | } | |
2421 | } | |
2422 | ||
28fff0d9 MR |
2423 | #define BITS 8 |
2424 | #define SIMPLE 1 | |
2425 | #include "h264_mb_template.c" | |
e5d40372 | 2426 | |
28fff0d9 MR |
2427 | #undef BITS |
2428 | #define BITS 16 | |
2429 | #include "h264_mb_template.c" | |
bd91fee3 | 2430 | |
28fff0d9 MR |
2431 | #undef SIMPLE |
2432 | #define SIMPLE 0 | |
2433 | #include "h264_mb_template.c" | |
c90b9442 | 2434 | |
e5d40372 DB |
2435 | void ff_h264_hl_decode_mb(H264Context *h) |
2436 | { | |
e5d40372 | 2437 | const int mb_xy = h->mb_xy; |
759001c5 | 2438 | const int mb_type = h->cur_pic.mb_type[mb_xy]; |
2c541554 | 2439 | int is_complex = CONFIG_SMALL || h->is_complex || IS_INTRA_PCM(mb_type) || h->qscale == 0; |
bd91fee3 | 2440 | |
23e85be5 | 2441 | if (CHROMA444(h)) { |
e5d40372 | 2442 | if (is_complex || h->pixel_shift) |
c90b9442 JGG |
2443 | hl_decode_mb_444_complex(h); |
2444 | else | |
28fff0d9 | 2445 | hl_decode_mb_444_simple_8(h); |
c90b9442 | 2446 | } else if (is_complex) { |
bd91fee3 | 2447 | hl_decode_mb_complex(h); |
6e3ef511 OA |
2448 | } else if (h->pixel_shift) { |
2449 | hl_decode_mb_simple_16(h); | |
2450 | } else | |
2451 | hl_decode_mb_simple_8(h); | |
bd91fee3 AS |
2452 | } |
2453 | ||
e5d40372 DB |
2454 | static int pred_weight_table(H264Context *h) |
2455 | { | |
0da71265 | 2456 | int list, i; |
9f2d1b4f | 2457 | int luma_def, chroma_def; |
115329f1 | 2458 | |
e5d40372 DB |
2459 | h->use_weight = 0; |
2460 | h->use_weight_chroma = 0; | |
2c541554 | 2461 | h->luma_log2_weight_denom = get_ue_golomb(&h->gb); |
e5d40372 | 2462 | if (h->sps.chroma_format_idc) |
2c541554 | 2463 | h->chroma_log2_weight_denom = get_ue_golomb(&h->gb); |
e5d40372 DB |
2464 | luma_def = 1 << h->luma_log2_weight_denom; |
2465 | chroma_def = 1 << h->chroma_log2_weight_denom; | |
0da71265 | 2466 | |
e5d40372 | 2467 | for (list = 0; list < 2; list++) { |
cb99c652 GB |
2468 | h->luma_weight_flag[list] = 0; |
2469 | h->chroma_weight_flag[list] = 0; | |
e5d40372 | 2470 | for (i = 0; i < h->ref_count[list]; i++) { |
0da71265 | 2471 | int luma_weight_flag, chroma_weight_flag; |
115329f1 | 2472 | |
2c541554 | 2473 | luma_weight_flag = get_bits1(&h->gb); |
e5d40372 | 2474 | if (luma_weight_flag) { |
2c541554 AK |
2475 | h->luma_weight[i][list][0] = get_se_golomb(&h->gb); |
2476 | h->luma_weight[i][list][1] = get_se_golomb(&h->gb); | |
e5d40372 DB |
2477 | if (h->luma_weight[i][list][0] != luma_def || |
2478 | h->luma_weight[i][list][1] != 0) { | |
2479 | h->use_weight = 1; | |
2480 | h->luma_weight_flag[list] = 1; | |
cb99c652 | 2481 | } |
e5d40372 DB |
2482 | } else { |
2483 | h->luma_weight[i][list][0] = luma_def; | |
2484 | h->luma_weight[i][list][1] = 0; | |
0da71265 MN |
2485 | } |
2486 | ||
e5d40372 | 2487 | if (h->sps.chroma_format_idc) { |
2c541554 | 2488 | chroma_weight_flag = get_bits1(&h->gb); |
e5d40372 | 2489 | if (chroma_weight_flag) { |
fef744d4 | 2490 | int j; |
e5d40372 | 2491 | for (j = 0; j < 2; j++) { |
2c541554 AK |
2492 | h->chroma_weight[i][list][j][0] = get_se_golomb(&h->gb); |
2493 | h->chroma_weight[i][list][j][1] = get_se_golomb(&h->gb); | |
e5d40372 DB |
2494 | if (h->chroma_weight[i][list][j][0] != chroma_def || |
2495 | h->chroma_weight[i][list][j][1] != 0) { | |
2496 | h->use_weight_chroma = 1; | |
2497 | h->chroma_weight_flag[list] = 1; | |
cb99c652 | 2498 | } |
fef744d4 | 2499 | } |
e5d40372 | 2500 | } else { |
fef744d4 | 2501 | int j; |
e5d40372 DB |
2502 | for (j = 0; j < 2; j++) { |
2503 | h->chroma_weight[i][list][j][0] = chroma_def; | |
2504 | h->chroma_weight[i][list][j][1] = 0; | |
fef744d4 | 2505 | } |
0da71265 MN |
2506 | } |
2507 | } | |
2508 | } | |
e5d40372 DB |
2509 | if (h->slice_type_nos != AV_PICTURE_TYPE_B) |
2510 | break; | |
0da71265 | 2511 | } |
e5d40372 | 2512 | h->use_weight = h->use_weight || h->use_weight_chroma; |
0da71265 MN |
2513 | return 0; |
2514 | } | |
2515 | ||
1052b76f MN |
2516 | /** |
2517 | * Initialize implicit_weight table. | |
6da88bd3 | 2518 | * @param field 0/1 initialize the weight for interlaced MBAFF |
1052b76f MN |
2519 | * -1 initializes the rest |
2520 | */ | |
e5d40372 DB |
2521 | static void implicit_weight_table(H264Context *h, int field) |
2522 | { | |
1052b76f | 2523 | int ref0, ref1, i, cur_poc, ref_start, ref_count0, ref_count1; |
9f2d1b4f | 2524 | |
ce09f927 GB |
2525 | for (i = 0; i < 2; i++) { |
2526 | h->luma_weight_flag[i] = 0; | |
2527 | h->chroma_weight_flag[i] = 0; | |
2528 | } | |
2529 | ||
e5d40372 | 2530 | if (field < 0) { |
2c541554 AK |
2531 | if (h->picture_structure == PICT_FRAME) { |
2532 | cur_poc = h->cur_pic_ptr->poc; | |
4418aa9c | 2533 | } else { |
2c541554 | 2534 | cur_poc = h->cur_pic_ptr->field_poc[h->picture_structure - 1]; |
4418aa9c | 2535 | } |
7bece9b2 | 2536 | if (h->ref_count[0] == 1 && h->ref_count[1] == 1 && !FRAME_MBAFF(h) && |
e5d40372 DB |
2537 | h->ref_list[0][0].poc + h->ref_list[1][0].poc == 2 * cur_poc) { |
2538 | h->use_weight = 0; | |
2539 | h->use_weight_chroma = 0; | |
2540 | return; | |
2541 | } | |
2542 | ref_start = 0; | |
2543 | ref_count0 = h->ref_count[0]; | |
2544 | ref_count1 = h->ref_count[1]; | |
2545 | } else { | |
2c541554 | 2546 | cur_poc = h->cur_pic_ptr->field_poc[field]; |
e5d40372 DB |
2547 | ref_start = 16; |
2548 | ref_count0 = 16 + 2 * h->ref_count[0]; | |
2549 | ref_count1 = 16 + 2 * h->ref_count[1]; | |
1052b76f | 2550 | } |
9f2d1b4f | 2551 | |
e5d40372 DB |
2552 | h->use_weight = 2; |
2553 | h->use_weight_chroma = 2; | |
2554 | h->luma_log2_weight_denom = 5; | |
2555 | h->chroma_log2_weight_denom = 5; | |
9f2d1b4f | 2556 | |
e5d40372 | 2557 | for (ref0 = ref_start; ref0 < ref_count0; ref0++) { |
9f2d1b4f | 2558 | int poc0 = h->ref_list[0][ref0].poc; |
e5d40372 | 2559 | for (ref1 = ref_start; ref1 < ref_count1; ref1++) { |
87cf70eb JD |
2560 | int w = 32; |
2561 | if (!h->ref_list[0][ref0].long_ref && !h->ref_list[1][ref1].long_ref) { | |
2562 | int poc1 = h->ref_list[1][ref1].poc; | |
e5d40372 DB |
2563 | int td = av_clip(poc1 - poc0, -128, 127); |
2564 | if (td) { | |
87cf70eb JD |
2565 | int tb = av_clip(cur_poc - poc0, -128, 127); |
2566 | int tx = (16384 + (FFABS(td) >> 1)) / td; | |
e5d40372 DB |
2567 | int dist_scale_factor = (tb * tx + 32) >> 8; |
2568 | if (dist_scale_factor >= -64 && dist_scale_factor <= 128) | |
87cf70eb JD |
2569 | w = 64 - dist_scale_factor; |
2570 | } | |
1052b76f | 2571 | } |
e5d40372 DB |
2572 | if (field < 0) { |
2573 | h->implicit_weight[ref0][ref1][0] = | |
2574 | h->implicit_weight[ref0][ref1][1] = w; | |
2575 | } else { | |
2576 | h->implicit_weight[ref0][ref1][field] = w; | |
72f86ec0 | 2577 | } |
9f2d1b4f LM |
2578 | } |
2579 | } | |
2580 | } | |
2581 | ||
8fd57a66 | 2582 | /** |
5175b937 | 2583 | * instantaneous decoder refresh. |
0da71265 | 2584 | */ |
e5d40372 DB |
2585 | static void idr(H264Context *h) |
2586 | { | |
ea6f00c4 | 2587 | ff_h264_remove_all_refs(h); |
e5d40372 DB |
2588 | h->prev_frame_num = 0; |
2589 | h->prev_frame_num_offset = 0; | |
2590 | h->prev_poc_msb = | |
2591 | h->prev_poc_lsb = 0; | |
0da71265 MN |
2592 | } |
2593 | ||
7c33ad19 | 2594 | /* forget old pics after a seek */ |
9e696d2e | 2595 | static void flush_change(H264Context *h) |
e5d40372 | 2596 | { |
7c33ad19 | 2597 | int i; |
adedd840 RB |
2598 | for (i = 0; i < MAX_DELAYED_PIC_COUNT; i++) |
2599 | h->last_pocs[i] = INT_MIN; | |
e5d40372 | 2600 | h->outputed_poc = h->next_outputed_poc = INT_MIN; |
b19d493f | 2601 | h->prev_interlaced_frame = 1; |
7c33ad19 | 2602 | idr(h); |
2c541554 | 2603 | if (h->cur_pic_ptr) |
759001c5 | 2604 | h->cur_pic_ptr->reference = 0; |
2c541554 | 2605 | h->first_field = 0; |
9e696d2e JG |
2606 | memset(h->ref_list[0], 0, sizeof(h->ref_list[0])); |
2607 | memset(h->ref_list[1], 0, sizeof(h->ref_list[1])); | |
2608 | memset(h->default_ref_list[0], 0, sizeof(h->default_ref_list[0])); | |
2609 | memset(h->default_ref_list[1], 0, sizeof(h->default_ref_list[1])); | |
9c095463 | 2610 | ff_h264_reset_sei(h); |
9e696d2e JG |
2611 | } |
2612 | ||
2613 | /* forget old pics after a seek */ | |
2614 | static void flush_dpb(AVCodecContext *avctx) | |
2615 | { | |
2616 | H264Context *h = avctx->priv_data; | |
2617 | int i; | |
2618 | ||
2619 | for (i = 0; i < MAX_DELAYED_PIC_COUNT; i++) { | |
2620 | if (h->delayed_pic[i]) | |
759001c5 | 2621 | h->delayed_pic[i]->reference = 0; |
9e696d2e JG |
2622 | h->delayed_pic[i] = NULL; |
2623 | } | |
2624 | ||
2625 | flush_change(h); | |
2c541554 | 2626 | |
555000c7 AK |
2627 | if (h->DPB) |
2628 | for (i = 0; i < MAX_PICTURE_COUNT; i++) | |
2629 | unref_picture(h, &h->DPB[i]); | |
2c541554 | 2630 | h->cur_pic_ptr = NULL; |
759001c5 | 2631 | unref_picture(h, &h->cur_pic); |
2c541554 AK |
2632 | |
2633 | h->mb_x = h->mb_y = 0; | |
2634 | ||
2635 | h->parse_context.state = -1; | |
2636 | h->parse_context.frame_start_found = 0; | |
2637 | h->parse_context.overread = 0; | |
2638 | h->parse_context.overread_index = 0; | |
2639 | h->parse_context.index = 0; | |
2640 | h->parse_context.last_index = 0; | |
7c33ad19 LM |
2641 | } |
2642 | ||
e5d40372 DB |
2643 | static int init_poc(H264Context *h) |
2644 | { | |
e5d40372 | 2645 | const int max_frame_num = 1 << h->sps.log2_max_frame_num; |
0da71265 | 2646 | int field_poc[2]; |
2c541554 | 2647 | Picture *cur = h->cur_pic_ptr; |
0da71265 | 2648 | |
e5d40372 DB |
2649 | h->frame_num_offset = h->prev_frame_num_offset; |
2650 | if (h->frame_num < h->prev_frame_num) | |
b78a6baa | 2651 | h->frame_num_offset += max_frame_num; |
0da71265 | 2652 | |
e5d40372 DB |
2653 | if (h->sps.poc_type == 0) { |
2654 | const int max_poc_lsb = 1 << h->sps.log2_max_poc_lsb; | |
0da71265 | 2655 | |
e5d40372 | 2656 | if (h->poc_lsb < h->prev_poc_lsb && h->prev_poc_lsb - h->poc_lsb >= max_poc_lsb / 2) |
0da71265 | 2657 | h->poc_msb = h->prev_poc_msb + max_poc_lsb; |
e5d40372 | 2658 | else if (h->poc_lsb > h->prev_poc_lsb && h->prev_poc_lsb - h->poc_lsb < -max_poc_lsb / 2) |
0da71265 MN |
2659 | h->poc_msb = h->prev_poc_msb - max_poc_lsb; |
2660 | else | |
2661 | h->poc_msb = h->prev_poc_msb; | |
115329f1 | 2662 | field_poc[0] = |
0da71265 | 2663 | field_poc[1] = h->poc_msb + h->poc_lsb; |
2c541554 | 2664 | if (h->picture_structure == PICT_FRAME) |
0da71265 | 2665 | field_poc[1] += h->delta_poc_bottom; |
e5d40372 | 2666 | } else if (h->sps.poc_type == 1) { |
0da71265 MN |
2667 | int abs_frame_num, expected_delta_per_poc_cycle, expectedpoc; |
2668 | int i; | |
2669 | ||
e5d40372 | 2670 | if (h->sps.poc_cycle_length != 0) |
0da71265 MN |
2671 | abs_frame_num = h->frame_num_offset + h->frame_num; |
2672 | else | |
2673 | abs_frame_num = 0; | |
2674 | ||
e5d40372 | 2675 | if (h->nal_ref_idc == 0 && abs_frame_num > 0) |
0da71265 | 2676 | abs_frame_num--; |
115329f1 | 2677 | |
0da71265 | 2678 | expected_delta_per_poc_cycle = 0; |
e5d40372 DB |
2679 | for (i = 0; i < h->sps.poc_cycle_length; i++) |
2680 | // FIXME integrate during sps parse | |
2681 | expected_delta_per_poc_cycle += h->sps.offset_for_ref_frame[i]; | |
0da71265 | 2682 | |
e5d40372 | 2683 | if (abs_frame_num > 0) { |
0da71265 MN |
2684 | int poc_cycle_cnt = (abs_frame_num - 1) / h->sps.poc_cycle_length; |
2685 | int frame_num_in_poc_cycle = (abs_frame_num - 1) % h->sps.poc_cycle_length; | |
2686 | ||
2687 | expectedpoc = poc_cycle_cnt * expected_delta_per_poc_cycle; | |
e5d40372 DB |
2688 | for (i = 0; i <= frame_num_in_poc_cycle; i++) |
2689 | expectedpoc = expectedpoc + h->sps.offset_for_ref_frame[i]; | |
0da71265 MN |
2690 | } else |
2691 | expectedpoc = 0; | |
2692 | ||
e5d40372 | 2693 | if (h->nal_ref_idc == 0) |
0da71265 | 2694 | expectedpoc = expectedpoc + h->sps.offset_for_non_ref_pic; |
115329f1 | 2695 | |
0da71265 MN |
2696 | field_poc[0] = expectedpoc + h->delta_poc[0]; |
2697 | field_poc[1] = field_poc[0] + h->sps.offset_for_top_to_bottom_field; | |
2698 | ||
2c541554 | 2699 | if (h->picture_structure == PICT_FRAME) |
0da71265 | 2700 | field_poc[1] += h->delta_poc[1]; |
e5d40372 DB |
2701 | } else { |
2702 | int poc = 2 * (h->frame_num_offset + h->frame_num); | |
5710b371 | 2703 | |
e5d40372 | 2704 | if (!h->nal_ref_idc) |
b78a6baa | 2705 | poc--; |
5710b371 | 2706 | |
e5d40372 DB |
2707 | field_poc[0] = poc; |
2708 | field_poc[1] = poc; | |
0da71265 | 2709 | } |
115329f1 | 2710 | |
2c541554 AK |
2711 | if (h->picture_structure != PICT_BOTTOM_FIELD) |
2712 | h->cur_pic_ptr->field_poc[0] = field_poc[0]; | |
2713 | if (h->picture_structure != PICT_TOP_FIELD) | |
2714 | h->cur_pic_ptr->field_poc[1] = field_poc[1]; | |
e5d40372 | 2715 | cur->poc = FFMIN(cur->field_poc[0], cur->field_poc[1]); |
0da71265 MN |
2716 | |
2717 | return 0; | |
2718 | } | |
2719 | ||
b41c1db3 AÖ |
2720 | /** |
2721 | * initialize scan tables | |
2722 | */ | |
e5d40372 DB |
2723 | static void init_scan_tables(H264Context *h) |
2724 | { | |
b41c1db3 | 2725 | int i; |
e5d40372 DB |
2726 | for (i = 0; i < 16; i++) { |
2727 | #define T(x) (x >> 2) | ((x << 2) & 0xF) | |
ca32f7f2 | 2728 | h->zigzag_scan[i] = T(zigzag_scan[i]); |
e5d40372 | 2729 | h->field_scan[i] = T(field_scan[i]); |
b41c1db3 | 2730 | #undef T |
b41c1db3 | 2731 | } |
e5d40372 DB |
2732 | for (i = 0; i < 64; i++) { |
2733 | #define T(x) (x >> 3) | ((x & 7) << 3) | |
ca32f7f2 JGG |
2734 | h->zigzag_scan8x8[i] = T(ff_zigzag_direct[i]); |
2735 | h->zigzag_scan8x8_cavlc[i] = T(zigzag_scan8x8_cavlc[i]); | |
2736 | h->field_scan8x8[i] = T(field_scan8x8[i]); | |
2737 | h->field_scan8x8_cavlc[i] = T(field_scan8x8_cavlc[i]); | |
b41c1db3 | 2738 | #undef T |
b41c1db3 | 2739 | } |
e5d40372 | 2740 | if (h->sps.transform_bypass) { // FIXME same ugly |
b41c1db3 | 2741 | h->zigzag_scan_q0 = zigzag_scan; |
45beb850 | 2742 | h->zigzag_scan8x8_q0 = ff_zigzag_direct; |
b41c1db3 AÖ |
2743 | h->zigzag_scan8x8_cavlc_q0 = zigzag_scan8x8_cavlc; |
2744 | h->field_scan_q0 = field_scan; | |
2745 | h->field_scan8x8_q0 = field_scan8x8; | |
2746 | h->field_scan8x8_cavlc_q0 = field_scan8x8_cavlc; | |
e5d40372 | 2747 | } else { |
b41c1db3 AÖ |
2748 | h->zigzag_scan_q0 = h->zigzag_scan; |
2749 | h->zigzag_scan8x8_q0 = h->zigzag_scan8x8; | |
2750 | h->zigzag_scan8x8_cavlc_q0 = h->zigzag_scan8x8_cavlc; | |
2751 | h->field_scan_q0 = h->field_scan; | |
2752 | h->field_scan8x8_q0 = h->field_scan8x8; | |
2753 | h->field_scan8x8_cavlc_q0 = h->field_scan8x8_cavlc; | |
2754 | } | |
2755 | } | |
afebe2f7 | 2756 | |
e5d40372 DB |
2757 | static int field_end(H264Context *h, int in_setup) |
2758 | { | |
2c541554 | 2759 | AVCodecContext *const avctx = h->avctx; |
12fe7594 | 2760 | int err = 0; |
2c541554 | 2761 | h->mb_y = 0; |
256299d3 | 2762 | |
2c541554 | 2763 | if (!in_setup && !h->droppable) |
759001c5 | 2764 | ff_thread_report_progress(&h->cur_pic_ptr->tf, INT_MAX, |
2c541554 | 2765 | h->picture_structure == PICT_BOTTOM_FIELD); |
256299d3 | 2766 | |
e5d40372 | 2767 | if (CONFIG_H264_VDPAU_DECODER && |
2c541554 AK |
2768 | h->avctx->codec->capabilities & CODEC_CAP_HWACCEL_VDPAU) |
2769 | ff_vdpau_h264_set_reference_frames(h); | |
256299d3 | 2770 | |
e5d40372 | 2771 | if (in_setup || !(avctx->active_thread_type & FF_THREAD_FRAME)) { |
2c541554 | 2772 | if (!h->droppable) { |
12fe7594 | 2773 | err = ff_h264_execute_ref_pic_marking(h, h->mmco, h->mmco_index); |
e5d40372 DB |
2774 | h->prev_poc_msb = h->poc_msb; |
2775 | h->prev_poc_lsb = h->poc_lsb; | |
6a9c8594 | 2776 | } |
e5d40372 DB |
2777 | h->prev_frame_num_offset = h->frame_num_offset; |
2778 | h->prev_frame_num = h->frame_num; | |
2779 | h->outputed_poc = h->next_outputed_poc; | |
256299d3 | 2780 | } |
256299d3 MN |
2781 | |
2782 | if (avctx->hwaccel) { | |
2783 | if (avctx->hwaccel->end_frame(avctx) < 0) | |
e5d40372 DB |
2784 | av_log(avctx, AV_LOG_ERROR, |
2785 | "hardware accelerator failed to decode picture\n"); | |
256299d3 MN |
2786 | } |
2787 | ||
e5d40372 | 2788 | if (CONFIG_H264_VDPAU_DECODER && |
2c541554 AK |
2789 | h->avctx->codec->capabilities & CODEC_CAP_HWACCEL_VDPAU) |
2790 | ff_vdpau_h264_picture_complete(h); | |
256299d3 MN |
2791 | |
2792 | /* | |
2793 | * FIXME: Error handling code does not seem to support interlaced | |
2794 | * when slices span multiple rows | |
2795 | * The ff_er_add_slice calls don't work right for bottom | |
2796 | * fields; they cause massive erroneous error concealing | |
2797 | * Error marking covers both fields (top and bottom). | |
2798 | * This causes a mismatched s->error_count | |
2799 | * and a bad error table. Further, the error count goes to | |
2800 | * INT_MAX when called for bottom field, because mb_y is | |
2801 | * past end by one (callers fault) and resync_mb_y != 0 | |
2802 | * causes problems for the first MB line, too. | |
2803 | */ | |
7fa00653 | 2804 | if (!FIELD_PICTURE(h)) { |
2c541554 AK |
2805 | h->er.cur_pic = h->cur_pic_ptr; |
2806 | h->er.last_pic = h->ref_count[0] ? &h->ref_list[0][0] : NULL; | |
2807 | h->er.next_pic = h->ref_count[1] ? &h->ref_list[1][0] : NULL; | |
2808 | ff_er_frame_end(&h->er); | |
2809 | } | |
2c541554 | 2810 | emms_c(); |
d225a1e2 | 2811 | |
e5d40372 | 2812 | h->current_slice = 0; |
12fe7594 DB |
2813 | |
2814 | return err; | |
256299d3 MN |
2815 | } |
2816 | ||
afebe2f7 | 2817 | /** |
49bd8e4b | 2818 | * Replicate H264 "master" context to thread contexts. |
afebe2f7 | 2819 | */ |
f1d8763a | 2820 | static int clone_slice(H264Context *dst, H264Context *src) |
afebe2f7 | 2821 | { |
e5d40372 | 2822 | memcpy(dst->block_offset, src->block_offset, sizeof(dst->block_offset)); |
2c541554 AK |
2823 | dst->cur_pic_ptr = src->cur_pic_ptr; |
2824 | dst->cur_pic = src->cur_pic; | |
2825 | dst->linesize = src->linesize; | |
2826 | dst->uvlinesize = src->uvlinesize; | |
2827 | dst->first_field = src->first_field; | |
f1d8763a | 2828 | |
e5d40372 DB |
2829 | dst->prev_poc_msb = src->prev_poc_msb; |
2830 | dst->prev_poc_lsb = src->prev_poc_lsb; | |
2831 | dst->prev_frame_num_offset = src->prev_frame_num_offset; | |
2832 | dst->prev_frame_num = src->prev_frame_num; | |
2833 | dst->short_ref_count = src->short_ref_count; | |
afebe2f7 AÖ |
2834 | |
2835 | memcpy(dst->short_ref, src->short_ref, sizeof(dst->short_ref)); | |
2836 | memcpy(dst->long_ref, src->long_ref, sizeof(dst->long_ref)); | |
2837 | memcpy(dst->default_ref_list, src->default_ref_list, sizeof(dst->default_ref_list)); | |
50c21814 AÖ |
2838 | |
2839 | memcpy(dst->dequant4_coeff, src->dequant4_coeff, sizeof(src->dequant4_coeff)); | |
2840 | memcpy(dst->dequant8_coeff, src->dequant8_coeff, sizeof(src->dequant8_coeff)); | |
f1d8763a JG |
2841 | |
2842 | return 0; | |
afebe2f7 AÖ |
2843 | } |
2844 | ||
0da71265 | 2845 | /** |
58c42af7 | 2846 | * Compute profile from profile_idc and constraint_set?_flags. |
fe9a3fbe JG |
2847 | * |
2848 | * @param sps SPS | |
2849 | * | |
2850 | * @return profile as defined by FF_PROFILE_H264_* | |
2851 | */ | |
2852 | int ff_h264_get_profile(SPS *sps) | |
2853 | { | |
2854 | int profile = sps->profile_idc; | |
2855 | ||
e5d40372 | 2856 | switch (sps->profile_idc) { |
fe9a3fbe JG |
2857 | case FF_PROFILE_H264_BASELINE: |
2858 | // constraint_set1_flag set to 1 | |
e5d40372 | 2859 | profile |= (sps->constraint_set_flags & 1 << 1) ? FF_PROFILE_H264_CONSTRAINED : 0; |
fe9a3fbe JG |
2860 | break; |
2861 | case FF_PROFILE_H264_HIGH_10: | |
2862 | case FF_PROFILE_H264_HIGH_422: | |
2863 | case FF_PROFILE_H264_HIGH_444_PREDICTIVE: | |
2864 | // constraint_set3_flag set to 1 | |
e5d40372 | 2865 | profile |= (sps->constraint_set_flags & 1 << 3) ? FF_PROFILE_H264_INTRA : 0; |
fe9a3fbe JG |
2866 | break; |
2867 | } | |
2868 | ||
2869 | return profile; | |
2870 | } | |
2871 | ||
072be3e8 JG |
2872 | static int h264_set_parameter_from_sps(H264Context *h) |
2873 | { | |
2c541554 | 2874 | if (h->flags & CODEC_FLAG_LOW_DELAY || |
072be3e8 JG |
2875 | (h->sps.bitstream_restriction_flag && |
2876 | !h->sps.num_reorder_frames)) { | |
2c541554 AK |
2877 | if (h->avctx->has_b_frames > 1 || h->delayed_pic[0]) |
2878 | av_log(h->avctx, AV_LOG_WARNING, "Delayed frames seen. " | |
072be3e8 JG |
2879 | "Reenabling low delay requires a codec flush.\n"); |
2880 | else | |
2c541554 | 2881 | h->low_delay = 1; |
072be3e8 JG |
2882 | } |
2883 | ||
2c541554 AK |
2884 | if (h->avctx->has_b_frames < 2) |
2885 | h->avctx->has_b_frames = !h->low_delay; | |
072be3e8 | 2886 | |
4987faee | 2887 | if (h->sps.bit_depth_luma != h->sps.bit_depth_chroma) { |
12e25ed2 DB |
2888 | avpriv_request_sample(h->avctx, |
2889 | "Different chroma and luma bit depth"); | |
4987faee LB |
2890 | return AVERROR_PATCHWELCOME; |
2891 | } | |
2892 | ||
2c541554 | 2893 | if (h->avctx->bits_per_raw_sample != h->sps.bit_depth_luma || |
072be3e8 | 2894 | h->cur_chroma_format_idc != h->sps.chroma_format_idc) { |
2c541554 AK |
2895 | if (h->avctx->codec && |
2896 | h->avctx->codec->capabilities & CODEC_CAP_HWACCEL_VDPAU && | |
072be3e8 | 2897 | (h->sps.bit_depth_luma != 8 || h->sps.chroma_format_idc > 1)) { |
2c541554 | 2898 | av_log(h->avctx, AV_LOG_ERROR, |
072be3e8 JG |
2899 | "VDPAU decoding does not support video colorspace.\n"); |
2900 | return AVERROR_INVALIDDATA; | |
2901 | } | |
2902 | if (h->sps.bit_depth_luma >= 8 && h->sps.bit_depth_luma <= 10) { | |
2c541554 | 2903 | h->avctx->bits_per_raw_sample = h->sps.bit_depth_luma; |
072be3e8 JG |
2904 | h->cur_chroma_format_idc = h->sps.chroma_format_idc; |
2905 | h->pixel_shift = h->sps.bit_depth_luma > 8; | |
2906 | ||
2907 | ff_h264dsp_init(&h->h264dsp, h->sps.bit_depth_luma, | |
2908 | h->sps.chroma_format_idc); | |
79dad2a9 | 2909 | ff_h264chroma_init(&h->h264chroma, h->sps.bit_depth_chroma); |
e9d81735 | 2910 | ff_h264qpel_init(&h->h264qpel, h->sps.bit_depth_luma); |
2c541554 | 2911 | ff_h264_pred_init(&h->hpc, h->avctx->codec_id, h->sps.bit_depth_luma, |
072be3e8 | 2912 | h->sps.chroma_format_idc); |
2c541554 AK |
2913 | h->dsp.dct_bits = h->sps.bit_depth_luma > 8 ? 32 : 16; |
2914 | ff_dsputil_init(&h->dsp, h->avctx); | |
2915 | ff_videodsp_init(&h->vdsp, h->sps.bit_depth_luma); | |
072be3e8 | 2916 | } else { |
2c541554 | 2917 | av_log(h->avctx, AV_LOG_ERROR, "Unsupported bit depth: %d\n", |
072be3e8 JG |
2918 | h->sps.bit_depth_luma); |
2919 | return AVERROR_INVALIDDATA; | |
2920 | } | |
2921 | } | |
2922 | return 0; | |
2923 | } | |
2924 | ||
542b83fc | 2925 | static enum AVPixelFormat get_pixel_format(H264Context *h) |
9e696d2e | 2926 | { |
9e696d2e JG |
2927 | switch (h->sps.bit_depth_luma) { |
2928 | case 9: | |
23e85be5 | 2929 | if (CHROMA444(h)) { |
2c541554 | 2930 | if (h->avctx->colorspace == AVCOL_SPC_RGB) { |
9e696d2e JG |
2931 | return AV_PIX_FMT_GBRP9; |
2932 | } else | |
2933 | return AV_PIX_FMT_YUV444P9; | |
e962bd08 | 2934 | } else if (CHROMA422(h)) |
9e696d2e JG |
2935 | return AV_PIX_FMT_YUV422P9; |
2936 | else | |
2937 | return AV_PIX_FMT_YUV420P9; | |
2938 | break; | |
2939 | case 10: | |
23e85be5 | 2940 | if (CHROMA444(h)) { |
2c541554 | 2941 | if (h->avctx->colorspace == AVCOL_SPC_RGB) { |
9e696d2e JG |
2942 | return AV_PIX_FMT_GBRP10; |
2943 | } else | |
2944 | return AV_PIX_FMT_YUV444P10; | |
e962bd08 | 2945 | } else if (CHROMA422(h)) |
9e696d2e JG |
2946 | return AV_PIX_FMT_YUV422P10; |
2947 | else | |
2948 | return AV_PIX_FMT_YUV420P10; | |
2949 | break; | |
2950 | case 8: | |
23e85be5 | 2951 | if (CHROMA444(h)) { |
2c541554 | 2952 | if (h->avctx->colorspace == AVCOL_SPC_RGB) { |
9e696d2e JG |
2953 | return AV_PIX_FMT_GBRP; |
2954 | } else | |
2c541554 | 2955 | return h->avctx->color_range == AVCOL_RANGE_JPEG ? AV_PIX_FMT_YUVJ444P |
9e696d2e | 2956 | : AV_PIX_FMT_YUV444P; |
e962bd08 | 2957 | } else if (CHROMA422(h)) { |
2c541554 | 2958 | return h->avctx->color_range == AVCOL_RANGE_JPEG ? AV_PIX_FMT_YUVJ422P |
9e696d2e JG |
2959 | : AV_PIX_FMT_YUV422P; |
2960 | } else { | |
2c541554 AK |
2961 | return h->avctx->get_format(h->avctx, h->avctx->codec->pix_fmts ? |
2962 | h->avctx->codec->pix_fmts : | |
2963 | h->avctx->color_range == AVCOL_RANGE_JPEG ? | |
d65522e8 | 2964 | h264_hwaccel_pixfmt_list_jpeg_420 : |
8d061989 | 2965 | h264_hwaccel_pixfmt_list_420); |
9e696d2e JG |
2966 | } |
2967 | break; | |
2968 | default: | |
2c541554 | 2969 | av_log(h->avctx, AV_LOG_ERROR, |
9e696d2e JG |
2970 | "Unsupported bit depth: %d\n", h->sps.bit_depth_luma); |
2971 | return AVERROR_INVALIDDATA; | |
2972 | } | |
2973 | } | |
2974 | ||
2975 | static int h264_slice_header_init(H264Context *h, int reinit) | |
2976 | { | |
2c541554 AK |
2977 | int nb_slices = (HAVE_THREADS && |
2978 | h->avctx->active_thread_type & FF_THREAD_SLICE) ? | |
2979 | h->avctx->thread_count : 1; | |
2980 | int i; | |
9e696d2e | 2981 | |
2c541554 AK |
2982 | avcodec_set_dimensions(h->avctx, h->width, h->height); |
2983 | h->avctx->sample_aspect_ratio = h->sps.sar; | |
2984 | av_assert0(h->avctx->sample_aspect_ratio.den); | |
2985 | av_pix_fmt_get_chroma_sub_sample(h->avctx->pix_fmt, | |
2986 | &h->chroma_x_shift, &h->chroma_y_shift); | |
9e696d2e JG |
2987 | |
2988 | if (h->sps.timing_info_present_flag) { | |
2989 | int64_t den = h->sps.time_scale; | |
2990 | if (h->x264_build < 44U) | |
2991 | den *= 2; | |
2c541554 | 2992 | av_reduce(&h->avctx->time_base.num, &h->avctx->time_base.den, |
9e696d2e JG |
2993 | h->sps.num_units_in_tick, den, 1 << 30); |
2994 | } | |
2995 | ||
2c541554 | 2996 | h->avctx->hwaccel = ff_find_hwaccel(h->avctx->codec->id, h->avctx->pix_fmt); |
9e696d2e | 2997 | |
2c541554 | 2998 | if (reinit) |
9e696d2e | 2999 | free_tables(h, 0); |
2c541554 | 3000 | h->first_field = 0; |
9e696d2e JG |
3001 | h->prev_interlaced_frame = 1; |
3002 | ||
3003 | init_scan_tables(h); | |
3004 | if (ff_h264_alloc_tables(h) < 0) { | |
2c541554 | 3005 | av_log(h->avctx, AV_LOG_ERROR, |
9e696d2e JG |
3006 | "Could not allocate memory for h264\n"); |
3007 | return AVERROR(ENOMEM); | |
3008 | } | |
3009 | ||
2c541554 AK |
3010 | if (nb_slices > MAX_THREADS || (nb_slices > h->mb_height && h->mb_height)) { |
3011 | int max_slices; | |
3012 | if (h->mb_height) | |
3013 | max_slices = FFMIN(MAX_THREADS, h->mb_height); | |
3014 | else | |
3015 | max_slices = MAX_THREADS; | |
3016 | av_log(h->avctx, AV_LOG_WARNING, "too many threads/slices (%d)," | |
3017 | " reducing to %d\n", nb_slices, max_slices); | |
3018 | nb_slices = max_slices; | |
3019 | } | |
3020 | h->slice_context_count = nb_slices; | |
3021 | ||
3022 | if (!HAVE_THREADS || !(h->avctx->active_thread_type & FF_THREAD_SLICE)) { | |
9e696d2e | 3023 | if (context_init(h) < 0) { |
2c541554 | 3024 | av_log(h->avctx, AV_LOG_ERROR, "context_init() failed.\n"); |
9e696d2e JG |
3025 | return -1; |
3026 | } | |
3027 | } else { | |
2c541554 | 3028 | for (i = 1; i < h->slice_context_count; i++) { |
9e696d2e | 3029 | H264Context *c; |
2c541554 AK |
3030 | c = h->thread_context[i] = av_mallocz(sizeof(H264Context)); |
3031 | c->avctx = h->avctx; | |
3032 | c->dsp = h->dsp; | |
3033 | c->vdsp = h->vdsp; | |
9e696d2e | 3034 | c->h264dsp = h->h264dsp; |
6bdb841b | 3035 | c->h264qpel = h->h264qpel; |
4c51fe48 | 3036 | c->h264chroma = h->h264chroma; |
9e696d2e JG |
3037 | c->sps = h->sps; |
3038 | c->pps = h->pps; | |
3039 | c->pixel_shift = h->pixel_shift; | |
2c541554 AK |
3040 | c->width = h->width; |
3041 | c->height = h->height; | |
3042 | c->linesize = h->linesize; | |
3043 | c->uvlinesize = h->uvlinesize; | |
3044 | c->chroma_x_shift = h->chroma_x_shift; | |
3045 | c->chroma_y_shift = h->chroma_y_shift; | |
3046 | c->qscale = h->qscale; | |
3047 | c->droppable = h->droppable; | |
3048 | c->data_partitioning = h->data_partitioning; | |
3049 | c->low_delay = h->low_delay; | |
3050 | c->mb_width = h->mb_width; | |
3051 | c->mb_height = h->mb_height; | |
3052 | c->mb_stride = h->mb_stride; | |
3053 | c->mb_num = h->mb_num; | |
3054 | c->flags = h->flags; | |
3055 | c->workaround_bugs = h->workaround_bugs; | |
3056 | c->pict_type = h->pict_type; | |
3057 | ||
9e696d2e JG |
3058 | init_scan_tables(c); |
3059 | clone_tables(c, h, i); | |
2c541554 | 3060 | c->context_initialized = 1; |
9e696d2e JG |
3061 | } |
3062 | ||
2c541554 | 3063 | for (i = 0; i < h->slice_context_count; i++) |
9e696d2e | 3064 | if (context_init(h->thread_context[i]) < 0) { |
2c541554 | 3065 | av_log(h->avctx, AV_LOG_ERROR, "context_init() failed.\n"); |
9e696d2e JG |
3066 | return -1; |
3067 | } | |
3068 | } | |
3069 | ||
2c541554 AK |
3070 | h->context_initialized = 1; |
3071 | ||
9e696d2e JG |
3072 | return 0; |
3073 | } | |
3074 | ||
fe9a3fbe | 3075 | /** |
58c42af7 | 3076 | * Decode a slice header. |
efd29844 | 3077 | * This will also call ff_MPV_common_init() and frame_start() as needed. |
afebe2f7 AÖ |
3078 | * |
3079 | * @param h h264context | |
e5d40372 DB |
3080 | * @param h0 h264 master context (differs from 'h' when doing sliced based |
3081 | * parallel decoding) | |
afebe2f7 | 3082 | * |
d9526386 | 3083 | * @return 0 if okay, <0 if an error occurred, 1 if decoding must not be multithreaded |
0da71265 | 3084 | */ |
e5d40372 DB |
3085 | static int decode_slice_header(H264Context *h, H264Context *h0) |
3086 | { | |
88e7a4d1 | 3087 | unsigned int first_mb_in_slice; |
ac658be5 | 3088 | unsigned int pps_id; |
072be3e8 | 3089 | int num_ref_idx_active_override_flag, max_refs, ret; |
41f5c62f | 3090 | unsigned int slice_type, tmp, i, j; |
0bf79634 | 3091 | int default_ref_list_done = 0; |
ba0c8981 | 3092 | int last_pic_structure, last_pic_droppable; |
9e696d2e | 3093 | int needs_reinit = 0; |
0da71265 | 3094 | |
2c541554 AK |
3095 | h->me.qpel_put = h->h264qpel.put_h264_qpel_pixels_tab; |
3096 | h->me.qpel_avg = h->h264qpel.avg_h264_qpel_pixels_tab; | |
cf653d08 | 3097 | |
2c541554 | 3098 | first_mb_in_slice = get_ue_golomb(&h->gb); |
0da71265 | 3099 | |
e5d40372 | 3100 | if (first_mb_in_slice == 0) { // FIXME better field boundary detection |
7fa00653 | 3101 | if (h0->current_slice && FIELD_PICTURE(h)) { |
6a9c8594 | 3102 | field_end(h, 1); |
d225a1e2 MN |
3103 | } |
3104 | ||
afebe2f7 | 3105 | h0->current_slice = 0; |
2c541554 | 3106 | if (!h0->first_field) { |
759001c5 AK |
3107 | if (h->cur_pic_ptr && !h->droppable) { |
3108 | ff_thread_report_progress(&h->cur_pic_ptr->tf, INT_MAX, | |
2c541554 | 3109 | h->picture_structure == PICT_BOTTOM_FIELD); |
1e26a48f | 3110 | } |
2c541554 | 3111 | h->cur_pic_ptr = NULL; |
1e26a48f | 3112 | } |
66a4b2c1 MN |
3113 | } |
3114 | ||
2c541554 | 3115 | slice_type = get_ue_golomb_31(&h->gb); |
e5d40372 | 3116 | if (slice_type > 9) { |
2c541554 | 3117 | av_log(h->avctx, AV_LOG_ERROR, |
e5d40372 | 3118 | "slice type too large (%d) at %d %d\n", |
2c541554 | 3119 | h->slice_type, h->mb_x, h->mb_y); |
5175b937 | 3120 | return -1; |
0da71265 | 3121 | } |
e5d40372 | 3122 | if (slice_type > 4) { |
0bf79634 | 3123 | slice_type -= 5; |
e5d40372 DB |
3124 | h->slice_type_fixed = 1; |
3125 | } else | |
3126 | h->slice_type_fixed = 0; | |
115329f1 | 3127 | |
e5d40372 DB |
3128 | slice_type = golomb_to_pict_type[slice_type]; |
3129 | if (slice_type == AV_PICTURE_TYPE_I || | |
3130 | (h0->current_slice != 0 && slice_type == h0->last_slice_type)) { | |
0bf79634 LLL |
3131 | default_ref_list_done = 1; |
3132 | } | |
e5d40372 DB |
3133 | h->slice_type = slice_type; |
3134 | h->slice_type_nos = slice_type & 3; | |
0bf79634 | 3135 | |
e5d40372 | 3136 | // to make a few old functions happy, it's wrong though |
2c541554 | 3137 | h->pict_type = h->slice_type; |
115329f1 | 3138 | |
2c541554 | 3139 | pps_id = get_ue_golomb(&h->gb); |
e5d40372 | 3140 | if (pps_id >= MAX_PPS_COUNT) { |
2c541554 | 3141 | av_log(h->avctx, AV_LOG_ERROR, "pps_id out of range\n"); |
0da71265 MN |
3142 | return -1; |
3143 | } | |
e5d40372 | 3144 | if (!h0->pps_buffers[pps_id]) { |
2c541554 | 3145 | av_log(h->avctx, AV_LOG_ERROR, |
e5d40372 DB |
3146 | "non-existing PPS %u referenced\n", |
3147 | pps_id); | |
8b92b792 MN |
3148 | return -1; |
3149 | } | |
e5d40372 | 3150 | h->pps = *h0->pps_buffers[pps_id]; |
8b92b792 | 3151 | |
e5d40372 | 3152 | if (!h0->sps_buffers[h-& |