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