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