2 * H.26L/H.264/AVC/JVT/14496-10/... decoder
3 * Copyright (c) 2003 Michael Niedermayer <michaelni@gmx.at>
5 * This file is part of FFmpeg.
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 * H.264 / AVC / MPEG4 part10 codec.
25 * @author Michael Niedermayer <michaelni@gmx.at>
28 #include "libavutil/avassert.h"
29 #include "libavutil/imgutils.h"
30 #include "libavutil/timer.h"
33 #include "cabac_functions.h"
35 #include "error_resilience.h"
39 #include "h264chroma.h"
40 #include "h264_mvpred.h"
43 #include "mpegutils.h"
44 #include "rectangle.h"
48 static const uint8_t rem6[QP_MAX_NUM + 1] = {
49 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2,
50 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5,
51 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2,
52 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5,
56 static const uint8_t div6[QP_MAX_NUM + 1] = {
57 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 3, 3, 3,
58 3, 3, 3, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6,
59 7, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 10, 10, 10,
60 10,10,10,11,11,11,11,11,11,12,12,12,12,12,12,13,13,13, 13, 13, 13,
64 static const uint8_t field_scan[16+1] = {
65 0 + 0 * 4, 0 + 1 * 4, 1 + 0 * 4, 0 + 2 * 4,
66 0 + 3 * 4, 1 + 1 * 4, 1 + 2 * 4, 1 + 3 * 4,
67 2 + 0 * 4, 2 + 1 * 4, 2 + 2 * 4, 2 + 3 * 4,
68 3 + 0 * 4, 3 + 1 * 4, 3 + 2 * 4, 3 + 3 * 4,
71 static const uint8_t field_scan8x8[64+1] = {
72 0 + 0 * 8, 0 + 1 * 8, 0 + 2 * 8, 1 + 0 * 8,
73 1 + 1 * 8, 0 + 3 * 8, 0 + 4 * 8, 1 + 2 * 8,
74 2 + 0 * 8, 1 + 3 * 8, 0 + 5 * 8, 0 + 6 * 8,
75 0 + 7 * 8, 1 + 4 * 8, 2 + 1 * 8, 3 + 0 * 8,
76 2 + 2 * 8, 1 + 5 * 8, 1 + 6 * 8, 1 + 7 * 8,
77 2 + 3 * 8, 3 + 1 * 8, 4 + 0 * 8, 3 + 2 * 8,
78 2 + 4 * 8, 2 + 5 * 8, 2 + 6 * 8, 2 + 7 * 8,
79 3 + 3 * 8, 4 + 1 * 8, 5 + 0 * 8, 4 + 2 * 8,
80 3 + 4 * 8, 3 + 5 * 8, 3 + 6 * 8, 3 + 7 * 8,
81 4 + 3 * 8, 5 + 1 * 8, 6 + 0 * 8, 5 + 2 * 8,
82 4 + 4 * 8, 4 + 5 * 8, 4 + 6 * 8, 4 + 7 * 8,
83 5 + 3 * 8, 6 + 1 * 8, 6 + 2 * 8, 5 + 4 * 8,
84 5 + 5 * 8, 5 + 6 * 8, 5 + 7 * 8, 6 + 3 * 8,
85 7 + 0 * 8, 7 + 1 * 8, 6 + 4 * 8, 6 + 5 * 8,
86 6 + 6 * 8, 6 + 7 * 8, 7 + 2 * 8, 7 + 3 * 8,
87 7 + 4 * 8, 7 + 5 * 8, 7 + 6 * 8, 7 + 7 * 8,
90 static const uint8_t field_scan8x8_cavlc[64+1] = {
91 0 + 0 * 8, 1 + 1 * 8, 2 + 0 * 8, 0 + 7 * 8,
92 2 + 2 * 8, 2 + 3 * 8, 2 + 4 * 8, 3 + 3 * 8,
93 3 + 4 * 8, 4 + 3 * 8, 4 + 4 * 8, 5 + 3 * 8,
94 5 + 5 * 8, 7 + 0 * 8, 6 + 6 * 8, 7 + 4 * 8,
95 0 + 1 * 8, 0 + 3 * 8, 1 + 3 * 8, 1 + 4 * 8,
96 1 + 5 * 8, 3 + 1 * 8, 2 + 5 * 8, 4 + 1 * 8,
97 3 + 5 * 8, 5 + 1 * 8, 4 + 5 * 8, 6 + 1 * 8,
98 5 + 6 * 8, 7 + 1 * 8, 6 + 7 * 8, 7 + 5 * 8,
99 0 + 2 * 8, 0 + 4 * 8, 0 + 5 * 8, 2 + 1 * 8,
100 1 + 6 * 8, 4 + 0 * 8, 2 + 6 * 8, 5 + 0 * 8,
101 3 + 6 * 8, 6 + 0 * 8, 4 + 6 * 8, 6 + 2 * 8,
102 5 + 7 * 8, 6 + 4 * 8, 7 + 2 * 8, 7 + 6 * 8,
103 1 + 0 * 8, 1 + 2 * 8, 0 + 6 * 8, 3 + 0 * 8,
104 1 + 7 * 8, 3 + 2 * 8, 2 + 7 * 8, 4 + 2 * 8,
105 3 + 7 * 8, 5 + 2 * 8, 4 + 7 * 8, 5 + 4 * 8,
106 6 + 3 * 8, 6 + 5 * 8, 7 + 3 * 8, 7 + 7 * 8,
109 // zigzag_scan8x8_cavlc[i] = zigzag_scan8x8[(i/4) + 16*(i%4)]
110 static const uint8_t zigzag_scan8x8_cavlc[64+1] = {
111 0 + 0 * 8, 1 + 1 * 8, 1 + 2 * 8, 2 + 2 * 8,
112 4 + 1 * 8, 0 + 5 * 8, 3 + 3 * 8, 7 + 0 * 8,
113 3 + 4 * 8, 1 + 7 * 8, 5 + 3 * 8, 6 + 3 * 8,
114 2 + 7 * 8, 6 + 4 * 8, 5 + 6 * 8, 7 + 5 * 8,
115 1 + 0 * 8, 2 + 0 * 8, 0 + 3 * 8, 3 + 1 * 8,
116 3 + 2 * 8, 0 + 6 * 8, 4 + 2 * 8, 6 + 1 * 8,
117 2 + 5 * 8, 2 + 6 * 8, 6 + 2 * 8, 5 + 4 * 8,
118 3 + 7 * 8, 7 + 3 * 8, 4 + 7 * 8, 7 + 6 * 8,
119 0 + 1 * 8, 3 + 0 * 8, 0 + 4 * 8, 4 + 0 * 8,
120 2 + 3 * 8, 1 + 5 * 8, 5 + 1 * 8, 5 + 2 * 8,
121 1 + 6 * 8, 3 + 5 * 8, 7 + 1 * 8, 4 + 5 * 8,
122 4 + 6 * 8, 7 + 4 * 8, 5 + 7 * 8, 6 + 7 * 8,
123 0 + 2 * 8, 2 + 1 * 8, 1 + 3 * 8, 5 + 0 * 8,
124 1 + 4 * 8, 2 + 4 * 8, 6 + 0 * 8, 4 + 3 * 8,
125 0 + 7 * 8, 4 + 4 * 8, 7 + 2 * 8, 3 + 6 * 8,
126 5 + 5 * 8, 6 + 5 * 8, 6 + 6 * 8, 7 + 7 * 8,
129 static const uint8_t dequant4_coeff_init[6][3] = {
138 static const uint8_t dequant8_coeff_init_scan[16] = {
139 0, 3, 4, 3, 3, 1, 5, 1, 4, 5, 2, 5, 3, 1, 5, 1
142 static const uint8_t dequant8_coeff_init[6][6] = {
143 { 20, 18, 32, 19, 25, 24 },
144 { 22, 19, 35, 21, 28, 26 },
145 { 26, 23, 42, 24, 33, 31 },
146 { 28, 25, 45, 26, 35, 33 },
147 { 32, 28, 51, 30, 40, 38 },
148 { 36, 32, 58, 34, 46, 43 },
151 static const enum AVPixelFormat h264_hwaccel_pixfmt_list_420[] = {
152 #if CONFIG_H264_DXVA2_HWACCEL
153 AV_PIX_FMT_DXVA2_VLD,
155 #if CONFIG_H264_VAAPI_HWACCEL
156 AV_PIX_FMT_VAAPI_VLD,
158 #if CONFIG_H264_VDA_HWACCEL
161 #if CONFIG_H264_VDPAU_HWACCEL
168 static const enum AVPixelFormat h264_hwaccel_pixfmt_list_jpeg_420[] = {
169 #if CONFIG_H264_DXVA2_HWACCEL
170 AV_PIX_FMT_DXVA2_VLD,
172 #if CONFIG_H264_VAAPI_HWACCEL
173 AV_PIX_FMT_VAAPI_VLD,
175 #if CONFIG_H264_VDA_HWACCEL
178 #if CONFIG_H264_VDPAU_HWACCEL
186 static void release_unused_pictures(H264Context *h, int remove_current)
190 /* release non reference frames */
191 for (i = 0; i < H264_MAX_PICTURE_COUNT; i++) {
192 if (h->DPB[i].f.buf[0] && !h->DPB[i].reference &&
193 (remove_current || &h->DPB[i] != h->cur_pic_ptr)) {
194 ff_h264_unref_picture(h, &h->DPB[i]);
199 static int alloc_scratch_buffers(H264Context *h, int linesize)
201 int alloc_size = FFALIGN(FFABS(linesize) + 32, 32);
203 if (h->bipred_scratchpad)
206 h->bipred_scratchpad = av_malloc(16 * 6 * alloc_size);
207 // edge emu needs blocksize + filter length - 1
208 // (= 21x21 for h264)
209 h->edge_emu_buffer = av_mallocz(alloc_size * 2 * 21);
211 if (!h->bipred_scratchpad || !h->edge_emu_buffer) {
212 av_freep(&h->bipred_scratchpad);
213 av_freep(&h->edge_emu_buffer);
214 return AVERROR(ENOMEM);
220 static int init_table_pools(H264Context *h)
222 const int big_mb_num = h->mb_stride * (h->mb_height + 1) + 1;
223 const int mb_array_size = h->mb_stride * h->mb_height;
224 const int b4_stride = h->mb_width * 4 + 1;
225 const int b4_array_size = b4_stride * h->mb_height * 4;
227 h->qscale_table_pool = av_buffer_pool_init(big_mb_num + h->mb_stride,
229 h->mb_type_pool = av_buffer_pool_init((big_mb_num + h->mb_stride) *
230 sizeof(uint32_t), av_buffer_allocz);
231 h->motion_val_pool = av_buffer_pool_init(2 * (b4_array_size + 4) *
232 sizeof(int16_t), av_buffer_allocz);
233 h->ref_index_pool = av_buffer_pool_init(4 * mb_array_size, av_buffer_allocz);
235 if (!h->qscale_table_pool || !h->mb_type_pool || !h->motion_val_pool ||
236 !h->ref_index_pool) {
237 av_buffer_pool_uninit(&h->qscale_table_pool);
238 av_buffer_pool_uninit(&h->mb_type_pool);
239 av_buffer_pool_uninit(&h->motion_val_pool);
240 av_buffer_pool_uninit(&h->ref_index_pool);
241 return AVERROR(ENOMEM);
247 static int alloc_picture(H264Context *h, H264Picture *pic)
251 av_assert0(!pic->f.data[0]);
254 ret = ff_thread_get_buffer(h->avctx, &pic->tf, pic->reference ?
255 AV_GET_BUFFER_FLAG_REF : 0);
259 h->linesize = pic->f.linesize[0];
260 h->uvlinesize = pic->f.linesize[1];
261 pic->crop = h->sps.crop;
262 pic->crop_top = h->sps.crop_top;
263 pic->crop_left= h->sps.crop_left;
265 if (h->avctx->hwaccel) {
266 const AVHWAccel *hwaccel = h->avctx->hwaccel;
267 av_assert0(!pic->hwaccel_picture_private);
268 if (hwaccel->priv_data_size) {
269 pic->hwaccel_priv_buf = av_buffer_allocz(hwaccel->priv_data_size);
270 if (!pic->hwaccel_priv_buf)
271 return AVERROR(ENOMEM);
272 pic->hwaccel_picture_private = pic->hwaccel_priv_buf->data;
275 if (!h->avctx->hwaccel && CONFIG_GRAY && h->flags & CODEC_FLAG_GRAY && pic->f.data[2]) {
276 int h_chroma_shift, v_chroma_shift;
277 av_pix_fmt_get_chroma_sub_sample(pic->f.format,
278 &h_chroma_shift, &v_chroma_shift);
280 for(i=0; i<FF_CEIL_RSHIFT(h->avctx->height, v_chroma_shift); i++) {
281 memset(pic->f.data[1] + pic->f.linesize[1]*i,
282 0x80, FF_CEIL_RSHIFT(h->avctx->width, h_chroma_shift));
283 memset(pic->f.data[2] + pic->f.linesize[2]*i,
284 0x80, FF_CEIL_RSHIFT(h->avctx->width, h_chroma_shift));
288 if (!h->qscale_table_pool) {
289 ret = init_table_pools(h);
294 pic->qscale_table_buf = av_buffer_pool_get(h->qscale_table_pool);
295 pic->mb_type_buf = av_buffer_pool_get(h->mb_type_pool);
296 if (!pic->qscale_table_buf || !pic->mb_type_buf)
299 pic->mb_type = (uint32_t*)pic->mb_type_buf->data + 2 * h->mb_stride + 1;
300 pic->qscale_table = pic->qscale_table_buf->data + 2 * h->mb_stride + 1;
302 for (i = 0; i < 2; i++) {
303 pic->motion_val_buf[i] = av_buffer_pool_get(h->motion_val_pool);
304 pic->ref_index_buf[i] = av_buffer_pool_get(h->ref_index_pool);
305 if (!pic->motion_val_buf[i] || !pic->ref_index_buf[i])
308 pic->motion_val[i] = (int16_t (*)[2])pic->motion_val_buf[i]->data + 4;
309 pic->ref_index[i] = pic->ref_index_buf[i]->data;
314 ff_h264_unref_picture(h, pic);
315 return (ret < 0) ? ret : AVERROR(ENOMEM);
318 static inline int pic_is_unused(H264Context *h, H264Picture *pic)
322 if (pic->needs_realloc && !(pic->reference & DELAYED_PIC_REF))
327 static int find_unused_picture(H264Context *h)
331 for (i = 0; i < H264_MAX_PICTURE_COUNT; i++) {
332 if (pic_is_unused(h, &h->DPB[i]))
335 if (i == H264_MAX_PICTURE_COUNT)
336 return AVERROR_INVALIDDATA;
338 if (h->DPB[i].needs_realloc) {
339 h->DPB[i].needs_realloc = 0;
340 ff_h264_unref_picture(h, &h->DPB[i]);
347 static void init_dequant8_coeff_table(H264Context *h)
350 const int max_qp = 51 + 6 * (h->sps.bit_depth_luma - 8);
352 for (i = 0; i < 6; i++) {
353 h->dequant8_coeff[i] = h->dequant8_buffer[i];
354 for (j = 0; j < i; j++)
355 if (!memcmp(h->pps.scaling_matrix8[j], h->pps.scaling_matrix8[i],
356 64 * sizeof(uint8_t))) {
357 h->dequant8_coeff[i] = h->dequant8_buffer[j];
363 for (q = 0; q < max_qp + 1; q++) {
366 for (x = 0; x < 64; x++)
367 h->dequant8_coeff[i][q][(x >> 3) | ((x & 7) << 3)] =
368 ((uint32_t)dequant8_coeff_init[idx][dequant8_coeff_init_scan[((x >> 1) & 12) | (x & 3)]] *
369 h->pps.scaling_matrix8[i][x]) << shift;
374 static void init_dequant4_coeff_table(H264Context *h)
377 const int max_qp = 51 + 6 * (h->sps.bit_depth_luma - 8);
378 for (i = 0; i < 6; i++) {
379 h->dequant4_coeff[i] = h->dequant4_buffer[i];
380 for (j = 0; j < i; j++)
381 if (!memcmp(h->pps.scaling_matrix4[j], h->pps.scaling_matrix4[i],
382 16 * sizeof(uint8_t))) {
383 h->dequant4_coeff[i] = h->dequant4_buffer[j];
389 for (q = 0; q < max_qp + 1; q++) {
390 int shift = div6[q] + 2;
392 for (x = 0; x < 16; x++)
393 h->dequant4_coeff[i][q][(x >> 2) | ((x << 2) & 0xF)] =
394 ((uint32_t)dequant4_coeff_init[idx][(x & 1) + ((x >> 2) & 1)] *
395 h->pps.scaling_matrix4[i][x]) << shift;
400 void h264_init_dequant_tables(H264Context *h)
403 init_dequant4_coeff_table(h);
404 memset(h->dequant8_coeff, 0, sizeof(h->dequant8_coeff));
406 if (h->pps.transform_8x8_mode)
407 init_dequant8_coeff_table(h);
408 if (h->sps.transform_bypass) {
409 for (i = 0; i < 6; i++)
410 for (x = 0; x < 16; x++)
411 h->dequant4_coeff[i][0][x] = 1 << 6;
412 if (h->pps.transform_8x8_mode)
413 for (i = 0; i < 6; i++)
414 for (x = 0; x < 64; x++)
415 h->dequant8_coeff[i][0][x] = 1 << 6;
420 * Mimic alloc_tables(), but for every context thread.
422 static void clone_tables(H264Context *dst, H264Context *src, int i)
424 dst->intra4x4_pred_mode = src->intra4x4_pred_mode + i * 8 * 2 * src->mb_stride;
425 dst->non_zero_count = src->non_zero_count;
426 dst->slice_table = src->slice_table;
427 dst->cbp_table = src->cbp_table;
428 dst->mb2b_xy = src->mb2b_xy;
429 dst->mb2br_xy = src->mb2br_xy;
430 dst->chroma_pred_mode_table = src->chroma_pred_mode_table;
431 dst->mvd_table[0] = src->mvd_table[0] + i * 8 * 2 * src->mb_stride;
432 dst->mvd_table[1] = src->mvd_table[1] + i * 8 * 2 * src->mb_stride;
433 dst->direct_table = src->direct_table;
434 dst->list_counts = src->list_counts;
436 dst->cur_pic_ptr = src->cur_pic_ptr;
437 dst->cur_pic = src->cur_pic;
438 dst->bipred_scratchpad = NULL;
439 dst->edge_emu_buffer = NULL;
440 ff_h264_pred_init(&dst->hpc, src->avctx->codec_id, src->sps.bit_depth_luma,
441 src->sps.chroma_format_idc);
444 #define IN_RANGE(a, b, size) (((a) >= (b)) && ((a) < ((b) + (size))))
445 #undef REBASE_PICTURE
446 #define REBASE_PICTURE(pic, new_ctx, old_ctx) \
447 ((pic && pic >= old_ctx->DPB && \
448 pic < old_ctx->DPB + H264_MAX_PICTURE_COUNT) ? \
449 &new_ctx->DPB[pic - old_ctx->DPB] : NULL)
451 static void copy_picture_range(H264Picture **to, H264Picture **from, int count,
452 H264Context *new_base,
453 H264Context *old_base)
457 for (i = 0; i < count; i++) {
458 assert((IN_RANGE(from[i], old_base, sizeof(*old_base)) ||
459 IN_RANGE(from[i], old_base->DPB,
460 sizeof(H264Picture) * H264_MAX_PICTURE_COUNT) ||
462 to[i] = REBASE_PICTURE(from[i], new_base, old_base);
466 static int copy_parameter_set(void **to, void **from, int count, int size)
470 for (i = 0; i < count; i++) {
471 if (to[i] && !from[i]) {
473 } else if (from[i] && !to[i]) {
474 to[i] = av_malloc(size);
476 return AVERROR(ENOMEM);
480 memcpy(to[i], from[i], size);
486 #define copy_fields(to, from, start_field, end_field) \
487 memcpy(&to->start_field, &from->start_field, \
488 (char *)&to->end_field - (char *)&to->start_field)
490 static int h264_slice_header_init(H264Context *h, int reinit);
492 int ff_h264_update_thread_context(AVCodecContext *dst,
493 const AVCodecContext *src)
495 H264Context *h = dst->priv_data, *h1 = src->priv_data;
496 int inited = h->context_initialized, err = 0;
497 int context_reinitialized = 0;
504 (h->width != h1->width ||
505 h->height != h1->height ||
506 h->mb_width != h1->mb_width ||
507 h->mb_height != h1->mb_height ||
508 h->sps.bit_depth_luma != h1->sps.bit_depth_luma ||
509 h->sps.chroma_format_idc != h1->sps.chroma_format_idc ||
510 h->sps.colorspace != h1->sps.colorspace)) {
512 /* set bits_per_raw_sample to the previous value. the check for changed
513 * bit depth in h264_set_parameter_from_sps() uses it and sets it to
514 * the current value */
515 h->avctx->bits_per_raw_sample = h->sps.bit_depth_luma;
517 av_freep(&h->bipred_scratchpad);
519 h->width = h1->width;
520 h->height = h1->height;
521 h->mb_height = h1->mb_height;
522 h->mb_width = h1->mb_width;
523 h->mb_num = h1->mb_num;
524 h->mb_stride = h1->mb_stride;
525 h->b_stride = h1->b_stride;
527 if ((ret = copy_parameter_set((void **)h->sps_buffers,
528 (void **)h1->sps_buffers,
529 MAX_SPS_COUNT, sizeof(SPS))) < 0)
532 if ((ret = copy_parameter_set((void **)h->pps_buffers,
533 (void **)h1->pps_buffers,
534 MAX_PPS_COUNT, sizeof(PPS))) < 0)
538 if ((err = h264_slice_header_init(h, 1)) < 0) {
539 av_log(h->avctx, AV_LOG_ERROR, "h264_slice_header_init() failed");
542 context_reinitialized = 1;
545 h264_set_parameter_from_sps(h);
546 //Note we set context_reinitialized which will cause h264_set_parameter_from_sps to be reexecuted
547 h->cur_chroma_format_idc = h1->cur_chroma_format_idc;
550 /* update linesize on resize for h264. The h264 decoder doesn't
551 * necessarily call ff_MPV_frame_start in the new thread */
552 h->linesize = h1->linesize;
553 h->uvlinesize = h1->uvlinesize;
555 /* copy block_offset since frame_start may not be called */
556 memcpy(h->block_offset, h1->block_offset, sizeof(h->block_offset));
559 for (i = 0; i < MAX_SPS_COUNT; i++)
560 av_freep(h->sps_buffers + i);
562 for (i = 0; i < MAX_PPS_COUNT; i++)
563 av_freep(h->pps_buffers + i);
565 av_freep(&h->rbsp_buffer[0]);
566 av_freep(&h->rbsp_buffer[1]);
567 memcpy(h, h1, offsetof(H264Context, intra_pcm_ptr));
568 memcpy(&h->cabac, &h1->cabac,
569 sizeof(H264Context) - offsetof(H264Context, cabac));
570 av_assert0((void*)&h->cabac == &h->mb_padding + 1);
572 memset(h->sps_buffers, 0, sizeof(h->sps_buffers));
573 memset(h->pps_buffers, 0, sizeof(h->pps_buffers));
575 memset(&h->er, 0, sizeof(h->er));
576 memset(&h->mb, 0, sizeof(h->mb));
577 memset(&h->mb_luma_dc, 0, sizeof(h->mb_luma_dc));
578 memset(&h->mb_padding, 0, sizeof(h->mb_padding));
582 h->qscale_table_pool = NULL;
583 h->mb_type_pool = NULL;
584 h->ref_index_pool = NULL;
585 h->motion_val_pool = NULL;
586 for (i = 0; i < 2; i++) {
587 h->rbsp_buffer[i] = NULL;
588 h->rbsp_buffer_size[i] = 0;
591 if (h1->context_initialized) {
592 h->context_initialized = 0;
594 memset(&h->cur_pic, 0, sizeof(h->cur_pic));
595 av_frame_unref(&h->cur_pic.f);
596 h->cur_pic.tf.f = &h->cur_pic.f;
598 ret = ff_h264_alloc_tables(h);
600 av_log(dst, AV_LOG_ERROR, "Could not allocate memory\n");
603 ret = ff_h264_context_init(h);
605 av_log(dst, AV_LOG_ERROR, "context_init() failed.\n");
610 h->bipred_scratchpad = NULL;
611 h->edge_emu_buffer = NULL;
613 h->thread_context[0] = h;
614 h->context_initialized = h1->context_initialized;
617 h->avctx->coded_height = h1->avctx->coded_height;
618 h->avctx->coded_width = h1->avctx->coded_width;
619 h->avctx->width = h1->avctx->width;
620 h->avctx->height = h1->avctx->height;
621 h->coded_picture_number = h1->coded_picture_number;
622 h->first_field = h1->first_field;
623 h->picture_structure = h1->picture_structure;
624 h->qscale = h1->qscale;
625 h->droppable = h1->droppable;
626 h->low_delay = h1->low_delay;
628 for (i = 0; h->DPB && i < H264_MAX_PICTURE_COUNT; i++) {
629 ff_h264_unref_picture(h, &h->DPB[i]);
630 if (h1->DPB && h1->DPB[i].f.buf[0] &&
631 (ret = ff_h264_ref_picture(h, &h->DPB[i], &h1->DPB[i])) < 0)
635 h->cur_pic_ptr = REBASE_PICTURE(h1->cur_pic_ptr, h, h1);
636 ff_h264_unref_picture(h, &h->cur_pic);
637 if (h1->cur_pic.f.buf[0] && (ret = ff_h264_ref_picture(h, &h->cur_pic, &h1->cur_pic)) < 0)
640 h->workaround_bugs = h1->workaround_bugs;
641 h->low_delay = h1->low_delay;
642 h->droppable = h1->droppable;
644 // extradata/NAL handling
645 h->is_avc = h1->is_avc;
648 if ((ret = copy_parameter_set((void **)h->sps_buffers,
649 (void **)h1->sps_buffers,
650 MAX_SPS_COUNT, sizeof(SPS))) < 0)
653 if ((ret = copy_parameter_set((void **)h->pps_buffers,
654 (void **)h1->pps_buffers,
655 MAX_PPS_COUNT, sizeof(PPS))) < 0)
659 // Dequantization matrices
660 // FIXME these are big - can they be only copied when PPS changes?
661 copy_fields(h, h1, dequant4_buffer, dequant4_coeff);
663 for (i = 0; i < 6; i++)
664 h->dequant4_coeff[i] = h->dequant4_buffer[0] +
665 (h1->dequant4_coeff[i] - h1->dequant4_buffer[0]);
667 for (i = 0; i < 6; i++)
668 h->dequant8_coeff[i] = h->dequant8_buffer[0] +
669 (h1->dequant8_coeff[i] - h1->dequant8_buffer[0]);
671 h->dequant_coeff_pps = h1->dequant_coeff_pps;
674 copy_fields(h, h1, poc_lsb, redundant_pic_count);
677 copy_fields(h, h1, short_ref, cabac_init_idc);
679 copy_picture_range(h->short_ref, h1->short_ref, 32, h, h1);
680 copy_picture_range(h->long_ref, h1->long_ref, 32, h, h1);
681 copy_picture_range(h->delayed_pic, h1->delayed_pic,
682 MAX_DELAYED_PIC_COUNT + 2, h, h1);
684 h->frame_recovered = h1->frame_recovered;
686 if (context_reinitialized)
687 ff_h264_set_parameter_from_sps(h);
693 err = ff_h264_execute_ref_pic_marking(h, h->mmco, h->mmco_index);
694 h->prev_poc_msb = h->poc_msb;
695 h->prev_poc_lsb = h->poc_lsb;
697 h->prev_frame_num_offset = h->frame_num_offset;
698 h->prev_frame_num = h->frame_num;
699 h->outputed_poc = h->next_outputed_poc;
701 h->recovery_frame = h1->recovery_frame;
706 static int h264_frame_start(H264Context *h)
710 const int pixel_shift = h->pixel_shift;
712 1<<(h->sps.bit_depth_luma-1),
713 1<<(h->sps.bit_depth_chroma-1),
714 1<<(h->sps.bit_depth_chroma-1),
718 if (!ff_thread_can_start_frame(h->avctx)) {
719 av_log(h->avctx, AV_LOG_ERROR, "Attempt to start a frame outside SETUP state\n");
723 release_unused_pictures(h, 1);
724 h->cur_pic_ptr = NULL;
726 i = find_unused_picture(h);
728 av_log(h->avctx, AV_LOG_ERROR, "no frame buffer available\n");
733 pic->reference = h->droppable ? 0 : h->picture_structure;
734 pic->f.coded_picture_number = h->coded_picture_number++;
735 pic->field_picture = h->picture_structure != PICT_FRAME;
738 * Zero key_frame here; IDR markings per slice in frame or fields are ORed
740 * See decode_nal_units().
742 pic->f.key_frame = 0;
745 pic->invalid_gap = 0;
747 if ((ret = alloc_picture(h, pic)) < 0)
749 if(!h->frame_recovered && !h->avctx->hwaccel &&
750 !(h->avctx->codec->capabilities & CODEC_CAP_HWACCEL_VDPAU))
751 avpriv_color_frame(&pic->f, c);
753 h->cur_pic_ptr = pic;
754 ff_h264_unref_picture(h, &h->cur_pic);
755 if (CONFIG_ERROR_RESILIENCE) {
756 memset(&h->er.cur_pic, 0, sizeof(h->er.cur_pic));
759 if ((ret = ff_h264_ref_picture(h, &h->cur_pic, h->cur_pic_ptr)) < 0)
762 if (CONFIG_ERROR_RESILIENCE) {
763 ff_er_frame_start(&h->er);
764 memset(&h->er.last_pic, 0, sizeof(h->er.last_pic));
765 memset(&h->er.next_pic, 0, sizeof(h->er.next_pic));
768 assert(h->linesize && h->uvlinesize);
770 for (i = 0; i < 16; i++) {
771 h->block_offset[i] = (4 * ((scan8[i] - scan8[0]) & 7) << pixel_shift) + 4 * h->linesize * ((scan8[i] - scan8[0]) >> 3);
772 h->block_offset[48 + i] = (4 * ((scan8[i] - scan8[0]) & 7) << pixel_shift) + 8 * h->linesize * ((scan8[i] - scan8[0]) >> 3);
774 for (i = 0; i < 16; i++) {
775 h->block_offset[16 + i] =
776 h->block_offset[32 + i] = (4 * ((scan8[i] - scan8[0]) & 7) << pixel_shift) + 4 * h->uvlinesize * ((scan8[i] - scan8[0]) >> 3);
777 h->block_offset[48 + 16 + i] =
778 h->block_offset[48 + 32 + i] = (4 * ((scan8[i] - scan8[0]) & 7) << pixel_shift) + 8 * h->uvlinesize * ((scan8[i] - scan8[0]) >> 3);
781 // s->decode = (h->flags & CODEC_FLAG_PSNR) || !s->encoding ||
782 // h->cur_pic.reference /* || h->contains_intra */ || 1;
784 /* We mark the current picture as non-reference after allocating it, so
785 * that if we break out due to an error it can be released automatically
786 * in the next ff_MPV_frame_start().
788 h->cur_pic_ptr->reference = 0;
790 h->cur_pic_ptr->field_poc[0] = h->cur_pic_ptr->field_poc[1] = INT_MAX;
792 h->next_output_pic = NULL;
794 assert(h->cur_pic_ptr->long_ref == 0);
799 static av_always_inline void backup_mb_border(H264Context *h, uint8_t *src_y,
800 uint8_t *src_cb, uint8_t *src_cr,
801 int linesize, int uvlinesize,
806 const int pixel_shift = h->pixel_shift;
807 int chroma444 = CHROMA444(h);
808 int chroma422 = CHROMA422(h);
811 src_cb -= uvlinesize;
812 src_cr -= uvlinesize;
814 if (!simple && FRAME_MBAFF(h)) {
817 top_border = h->top_borders[0][h->mb_x];
818 AV_COPY128(top_border, src_y + 15 * linesize);
820 AV_COPY128(top_border + 16, src_y + 15 * linesize + 16);
821 if (simple || !CONFIG_GRAY || !(h->flags & CODEC_FLAG_GRAY)) {
824 AV_COPY128(top_border + 32, src_cb + 15 * uvlinesize);
825 AV_COPY128(top_border + 48, src_cb + 15 * uvlinesize + 16);
826 AV_COPY128(top_border + 64, src_cr + 15 * uvlinesize);
827 AV_COPY128(top_border + 80, src_cr + 15 * uvlinesize + 16);
829 AV_COPY128(top_border + 16, src_cb + 15 * uvlinesize);
830 AV_COPY128(top_border + 32, src_cr + 15 * uvlinesize);
832 } else if (chroma422) {
834 AV_COPY128(top_border + 32, src_cb + 15 * uvlinesize);
835 AV_COPY128(top_border + 48, src_cr + 15 * uvlinesize);
837 AV_COPY64(top_border + 16, src_cb + 15 * uvlinesize);
838 AV_COPY64(top_border + 24, src_cr + 15 * uvlinesize);
842 AV_COPY128(top_border + 32, src_cb + 7 * uvlinesize);
843 AV_COPY128(top_border + 48, src_cr + 7 * uvlinesize);
845 AV_COPY64(top_border + 16, src_cb + 7 * uvlinesize);
846 AV_COPY64(top_border + 24, src_cr + 7 * uvlinesize);
851 } else if (MB_MBAFF(h)) {
857 top_border = h->top_borders[top_idx][h->mb_x];
858 /* There are two lines saved, the line above the top macroblock
859 * of a pair, and the line above the bottom macroblock. */
860 AV_COPY128(top_border, src_y + 16 * linesize);
862 AV_COPY128(top_border + 16, src_y + 16 * linesize + 16);
864 if (simple || !CONFIG_GRAY || !(h->flags & CODEC_FLAG_GRAY)) {
867 AV_COPY128(top_border + 32, src_cb + 16 * linesize);
868 AV_COPY128(top_border + 48, src_cb + 16 * linesize + 16);
869 AV_COPY128(top_border + 64, src_cr + 16 * linesize);
870 AV_COPY128(top_border + 80, src_cr + 16 * linesize + 16);
872 AV_COPY128(top_border + 16, src_cb + 16 * linesize);
873 AV_COPY128(top_border + 32, src_cr + 16 * linesize);
875 } else if (chroma422) {
877 AV_COPY128(top_border + 32, src_cb + 16 * uvlinesize);
878 AV_COPY128(top_border + 48, src_cr + 16 * uvlinesize);
880 AV_COPY64(top_border + 16, src_cb + 16 * uvlinesize);
881 AV_COPY64(top_border + 24, src_cr + 16 * uvlinesize);
885 AV_COPY128(top_border + 32, src_cb + 8 * uvlinesize);
886 AV_COPY128(top_border + 48, src_cr + 8 * uvlinesize);
888 AV_COPY64(top_border + 16, src_cb + 8 * uvlinesize);
889 AV_COPY64(top_border + 24, src_cr + 8 * uvlinesize);
896 * Initialize implicit_weight table.
897 * @param field 0/1 initialize the weight for interlaced MBAFF
898 * -1 initializes the rest
900 static void implicit_weight_table(H264Context *h, int field)
902 int ref0, ref1, i, cur_poc, ref_start, ref_count0, ref_count1;
904 for (i = 0; i < 2; i++) {
905 h->luma_weight_flag[i] = 0;
906 h->chroma_weight_flag[i] = 0;
910 if (h->picture_structure == PICT_FRAME) {
911 cur_poc = h->cur_pic_ptr->poc;
913 cur_poc = h->cur_pic_ptr->field_poc[h->picture_structure - 1];
915 if (h->ref_count[0] == 1 && h->ref_count[1] == 1 && !FRAME_MBAFF(h) &&
916 h->ref_list[0][0].poc + h->ref_list[1][0].poc == 2 * cur_poc) {
918 h->use_weight_chroma = 0;
922 ref_count0 = h->ref_count[0];
923 ref_count1 = h->ref_count[1];
925 cur_poc = h->cur_pic_ptr->field_poc[field];
927 ref_count0 = 16 + 2 * h->ref_count[0];
928 ref_count1 = 16 + 2 * h->ref_count[1];
932 h->use_weight_chroma = 2;
933 h->luma_log2_weight_denom = 5;
934 h->chroma_log2_weight_denom = 5;
936 for (ref0 = ref_start; ref0 < ref_count0; ref0++) {
937 int poc0 = h->ref_list[0][ref0].poc;
938 for (ref1 = ref_start; ref1 < ref_count1; ref1++) {
940 if (!h->ref_list[0][ref0].long_ref && !h->ref_list[1][ref1].long_ref) {
941 int poc1 = h->ref_list[1][ref1].poc;
942 int td = av_clip(poc1 - poc0, -128, 127);
944 int tb = av_clip(cur_poc - poc0, -128, 127);
945 int tx = (16384 + (FFABS(td) >> 1)) / td;
946 int dist_scale_factor = (tb * tx + 32) >> 8;
947 if (dist_scale_factor >= -64 && dist_scale_factor <= 128)
948 w = 64 - dist_scale_factor;
952 h->implicit_weight[ref0][ref1][0] =
953 h->implicit_weight[ref0][ref1][1] = w;
955 h->implicit_weight[ref0][ref1][field] = w;
962 * initialize scan tables
964 static void init_scan_tables(H264Context *h)
967 for (i = 0; i < 16; i++) {
968 #define TRANSPOSE(x) (x >> 2) | ((x << 2) & 0xF)
969 h->zigzag_scan[i] = TRANSPOSE(zigzag_scan[i]);
970 h->field_scan[i] = TRANSPOSE(field_scan[i]);
973 for (i = 0; i < 64; i++) {
974 #define TRANSPOSE(x) (x >> 3) | ((x & 7) << 3)
975 h->zigzag_scan8x8[i] = TRANSPOSE(ff_zigzag_direct[i]);
976 h->zigzag_scan8x8_cavlc[i] = TRANSPOSE(zigzag_scan8x8_cavlc[i]);
977 h->field_scan8x8[i] = TRANSPOSE(field_scan8x8[i]);
978 h->field_scan8x8_cavlc[i] = TRANSPOSE(field_scan8x8_cavlc[i]);
981 if (h->sps.transform_bypass) { // FIXME same ugly
982 memcpy(h->zigzag_scan_q0 , zigzag_scan , sizeof(h->zigzag_scan_q0 ));
983 memcpy(h->zigzag_scan8x8_q0 , ff_zigzag_direct , sizeof(h->zigzag_scan8x8_q0 ));
984 memcpy(h->zigzag_scan8x8_cavlc_q0 , zigzag_scan8x8_cavlc , sizeof(h->zigzag_scan8x8_cavlc_q0));
985 memcpy(h->field_scan_q0 , field_scan , sizeof(h->field_scan_q0 ));
986 memcpy(h->field_scan8x8_q0 , field_scan8x8 , sizeof(h->field_scan8x8_q0 ));
987 memcpy(h->field_scan8x8_cavlc_q0 , field_scan8x8_cavlc , sizeof(h->field_scan8x8_cavlc_q0 ));
989 memcpy(h->zigzag_scan_q0 , h->zigzag_scan , sizeof(h->zigzag_scan_q0 ));
990 memcpy(h->zigzag_scan8x8_q0 , h->zigzag_scan8x8 , sizeof(h->zigzag_scan8x8_q0 ));
991 memcpy(h->zigzag_scan8x8_cavlc_q0 , h->zigzag_scan8x8_cavlc , sizeof(h->zigzag_scan8x8_cavlc_q0));
992 memcpy(h->field_scan_q0 , h->field_scan , sizeof(h->field_scan_q0 ));
993 memcpy(h->field_scan8x8_q0 , h->field_scan8x8 , sizeof(h->field_scan8x8_q0 ));
994 memcpy(h->field_scan8x8_cavlc_q0 , h->field_scan8x8_cavlc , sizeof(h->field_scan8x8_cavlc_q0 ));
999 * Replicate H264 "master" context to thread contexts.
1001 static int clone_slice(H264Context *dst, H264Context *src)
1003 memcpy(dst->block_offset, src->block_offset, sizeof(dst->block_offset));
1004 dst->cur_pic_ptr = src->cur_pic_ptr;
1005 dst->cur_pic = src->cur_pic;
1006 dst->linesize = src->linesize;
1007 dst->uvlinesize = src->uvlinesize;
1008 dst->first_field = src->first_field;
1010 dst->prev_poc_msb = src->prev_poc_msb;
1011 dst->prev_poc_lsb = src->prev_poc_lsb;
1012 dst->prev_frame_num_offset = src->prev_frame_num_offset;
1013 dst->prev_frame_num = src->prev_frame_num;
1014 dst->short_ref_count = src->short_ref_count;
1016 memcpy(dst->short_ref, src->short_ref, sizeof(dst->short_ref));
1017 memcpy(dst->long_ref, src->long_ref, sizeof(dst->long_ref));
1018 memcpy(dst->default_ref_list, src->default_ref_list, sizeof(dst->default_ref_list));
1020 memcpy(dst->dequant4_coeff, src->dequant4_coeff, sizeof(src->dequant4_coeff));
1021 memcpy(dst->dequant8_coeff, src->dequant8_coeff, sizeof(src->dequant8_coeff));
1026 static enum AVPixelFormat get_pixel_format(H264Context *h, int force_callback)
1028 switch (h->sps.bit_depth_luma) {
1031 if (h->avctx->colorspace == AVCOL_SPC_RGB) {
1032 return AV_PIX_FMT_GBRP9;
1034 return AV_PIX_FMT_YUV444P9;
1035 } else if (CHROMA422(h))
1036 return AV_PIX_FMT_YUV422P9;
1038 return AV_PIX_FMT_YUV420P9;
1042 if (h->avctx->colorspace == AVCOL_SPC_RGB) {
1043 return AV_PIX_FMT_GBRP10;
1045 return AV_PIX_FMT_YUV444P10;
1046 } else if (CHROMA422(h))
1047 return AV_PIX_FMT_YUV422P10;
1049 return AV_PIX_FMT_YUV420P10;
1053 if (h->avctx->colorspace == AVCOL_SPC_RGB) {
1054 return AV_PIX_FMT_GBRP12;
1056 return AV_PIX_FMT_YUV444P12;
1057 } else if (CHROMA422(h))
1058 return AV_PIX_FMT_YUV422P12;
1060 return AV_PIX_FMT_YUV420P12;
1064 if (h->avctx->colorspace == AVCOL_SPC_RGB) {
1065 return AV_PIX_FMT_GBRP14;
1067 return AV_PIX_FMT_YUV444P14;
1068 } else if (CHROMA422(h))
1069 return AV_PIX_FMT_YUV422P14;
1071 return AV_PIX_FMT_YUV420P14;
1075 if (h->avctx->colorspace == AVCOL_SPC_RGB) {
1076 av_log(h->avctx, AV_LOG_DEBUG, "Detected GBR colorspace.\n");
1077 return AV_PIX_FMT_GBR24P;
1078 } else if (h->avctx->colorspace == AVCOL_SPC_YCGCO) {
1079 av_log(h->avctx, AV_LOG_WARNING, "Detected unsupported YCgCo colorspace.\n");
1081 return h->avctx->color_range == AVCOL_RANGE_JPEG ? AV_PIX_FMT_YUVJ444P
1082 : AV_PIX_FMT_YUV444P;
1083 } else if (CHROMA422(h)) {
1084 return h->avctx->color_range == AVCOL_RANGE_JPEG ? AV_PIX_FMT_YUVJ422P
1085 : AV_PIX_FMT_YUV422P;
1088 const enum AVPixelFormat * fmt = h->avctx->codec->pix_fmts ?
1089 h->avctx->codec->pix_fmts :
1090 h->avctx->color_range == AVCOL_RANGE_JPEG ?
1091 h264_hwaccel_pixfmt_list_jpeg_420 :
1092 h264_hwaccel_pixfmt_list_420;
1094 for (i=0; fmt[i] != AV_PIX_FMT_NONE; i++)
1095 if (fmt[i] == h->avctx->pix_fmt && !force_callback)
1097 return ff_thread_get_format(h->avctx, fmt);
1101 av_log(h->avctx, AV_LOG_ERROR,
1102 "Unsupported bit depth %d\n", h->sps.bit_depth_luma);
1103 return AVERROR_INVALIDDATA;
1107 /* export coded and cropped frame dimensions to AVCodecContext */
1108 static int init_dimensions(H264Context *h)
1110 int width = h->width - (h->sps.crop_right + h->sps.crop_left);
1111 int height = h->height - (h->sps.crop_top + h->sps.crop_bottom);
1112 av_assert0(h->sps.crop_right + h->sps.crop_left < (unsigned)h->width);
1113 av_assert0(h->sps.crop_top + h->sps.crop_bottom < (unsigned)h->height);
1115 /* handle container cropping */
1117 FFALIGN(h->avctx->width, 16) == h->width &&
1118 FFALIGN(h->avctx->height, 16) == h->height) {
1119 width = h->avctx->width;
1120 height = h->avctx->height;
1123 if (width <= 0 || height <= 0) {
1124 av_log(h->avctx, AV_LOG_ERROR, "Invalid cropped dimensions: %dx%d.\n",
1126 if (h->avctx->err_recognition & AV_EF_EXPLODE)
1127 return AVERROR_INVALIDDATA;
1129 av_log(h->avctx, AV_LOG_WARNING, "Ignoring cropping information.\n");
1130 h->sps.crop_bottom = h->sps.crop_top = h->sps.crop_right = h->sps.crop_left = 0;
1137 h->avctx->coded_width = h->width;
1138 h->avctx->coded_height = h->height;
1139 h->avctx->width = width;
1140 h->avctx->height = height;
1145 static int h264_slice_header_init(H264Context *h, int reinit)
1147 int nb_slices = (HAVE_THREADS &&
1148 h->avctx->active_thread_type & FF_THREAD_SLICE) ?
1149 h->avctx->thread_count : 1;
1152 h->avctx->sample_aspect_ratio = h->sps.sar;
1153 av_assert0(h->avctx->sample_aspect_ratio.den);
1154 av_pix_fmt_get_chroma_sub_sample(h->avctx->pix_fmt,
1155 &h->chroma_x_shift, &h->chroma_y_shift);
1157 if (h->sps.timing_info_present_flag) {
1158 int64_t den = h->sps.time_scale;
1159 if (h->x264_build < 44U)
1161 av_reduce(&h->avctx->time_base.num, &h->avctx->time_base.den,
1162 h->sps.num_units_in_tick, den, 1 << 30);
1165 h->avctx->hwaccel = ff_find_hwaccel(h->avctx);
1168 ff_h264_free_tables(h, 0);
1170 h->prev_interlaced_frame = 1;
1172 init_scan_tables(h);
1173 ret = ff_h264_alloc_tables(h);
1175 av_log(h->avctx, AV_LOG_ERROR, "Could not allocate memory\n");
1179 if (nb_slices > H264_MAX_THREADS || (nb_slices > h->mb_height && h->mb_height)) {
1182 max_slices = FFMIN(H264_MAX_THREADS, h->mb_height);
1184 max_slices = H264_MAX_THREADS;
1185 av_log(h->avctx, AV_LOG_WARNING, "too many threads/slices %d,"
1186 " reducing to %d\n", nb_slices, max_slices);
1187 nb_slices = max_slices;
1189 h->slice_context_count = nb_slices;
1191 if (!HAVE_THREADS || !(h->avctx->active_thread_type & FF_THREAD_SLICE)) {
1192 ret = ff_h264_context_init(h);
1194 av_log(h->avctx, AV_LOG_ERROR, "context_init() failed.\n");
1198 for (i = 1; i < h->slice_context_count; i++) {
1200 c = h->thread_context[i] = av_mallocz(sizeof(H264Context));
1202 return AVERROR(ENOMEM);
1203 c->avctx = h->avctx;
1204 if (CONFIG_ERROR_RESILIENCE) {
1208 c->h264dsp = h->h264dsp;
1209 c->h264qpel = h->h264qpel;
1210 c->h264chroma = h->h264chroma;
1213 c->pixel_shift = h->pixel_shift;
1214 c->cur_chroma_format_idc = h->cur_chroma_format_idc;
1215 c->width = h->width;
1216 c->height = h->height;
1217 c->linesize = h->linesize;
1218 c->uvlinesize = h->uvlinesize;
1219 c->chroma_x_shift = h->chroma_x_shift;
1220 c->chroma_y_shift = h->chroma_y_shift;
1221 c->qscale = h->qscale;
1222 c->droppable = h->droppable;
1223 c->data_partitioning = h->data_partitioning;
1224 c->low_delay = h->low_delay;
1225 c->mb_width = h->mb_width;
1226 c->mb_height = h->mb_height;
1227 c->mb_stride = h->mb_stride;
1228 c->mb_num = h->mb_num;
1229 c->flags = h->flags;
1230 c->workaround_bugs = h->workaround_bugs;
1231 c->pict_type = h->pict_type;
1233 init_scan_tables(c);
1234 clone_tables(c, h, i);
1235 c->context_initialized = 1;
1238 for (i = 0; i < h->slice_context_count; i++)
1239 if ((ret = ff_h264_context_init(h->thread_context[i])) < 0) {
1240 av_log(h->avctx, AV_LOG_ERROR, "context_init() failed.\n");
1245 h->context_initialized = 1;
1250 static enum AVPixelFormat non_j_pixfmt(enum AVPixelFormat a)
1253 case AV_PIX_FMT_YUVJ420P: return AV_PIX_FMT_YUV420P;
1254 case AV_PIX_FMT_YUVJ422P: return AV_PIX_FMT_YUV422P;
1255 case AV_PIX_FMT_YUVJ444P: return AV_PIX_FMT_YUV444P;
1262 * Decode a slice header.
1263 * This will (re)intialize the decoder and call h264_frame_start() as needed.
1265 * @param h h264context
1266 * @param h0 h264 master context (differs from 'h' when doing sliced based
1267 * parallel decoding)
1269 * @return 0 if okay, <0 if an error occurred, 1 if decoding must not be multithreaded
1271 int ff_h264_decode_slice_header(H264Context *h, H264Context *h0)
1273 unsigned int first_mb_in_slice;
1274 unsigned int pps_id;
1276 unsigned int slice_type, tmp, i, j;
1277 int last_pic_structure, last_pic_droppable;
1279 int needs_reinit = 0;
1280 int field_pic_flag, bottom_field_flag;
1282 h->qpel_put = h->h264qpel.put_h264_qpel_pixels_tab;
1283 h->qpel_avg = h->h264qpel.avg_h264_qpel_pixels_tab;
1285 first_mb_in_slice = get_ue_golomb_long(&h->gb);
1287 if (first_mb_in_slice == 0) { // FIXME better field boundary detection
1288 if (h0->current_slice && h->cur_pic_ptr && FIELD_PICTURE(h)) {
1289 ff_h264_field_end(h, 1);
1292 h0->current_slice = 0;
1293 if (!h0->first_field) {
1294 if (h->cur_pic_ptr && !h->droppable) {
1295 ff_thread_report_progress(&h->cur_pic_ptr->tf, INT_MAX,
1296 h->picture_structure == PICT_BOTTOM_FIELD);
1298 h->cur_pic_ptr = NULL;
1302 slice_type = get_ue_golomb_31(&h->gb);
1303 if (slice_type > 9) {
1304 av_log(h->avctx, AV_LOG_ERROR,
1305 "slice type %d too large at %d %d\n",
1306 slice_type, h->mb_x, h->mb_y);
1307 return AVERROR_INVALIDDATA;
1309 if (slice_type > 4) {
1311 h->slice_type_fixed = 1;
1313 h->slice_type_fixed = 0;
1315 slice_type = golomb_to_pict_type[slice_type];
1316 h->slice_type = slice_type;
1317 h->slice_type_nos = slice_type & 3;
1319 if (h->nal_unit_type == NAL_IDR_SLICE &&
1320 h->slice_type_nos != AV_PICTURE_TYPE_I) {
1321 av_log(h->avctx, AV_LOG_ERROR, "A non-intra slice in an IDR NAL unit.\n");
1322 return AVERROR_INVALIDDATA;
1325 // to make a few old functions happy, it's wrong though
1326 h->pict_type = h->slice_type;
1328 pps_id = get_ue_golomb(&h->gb);
1329 if (pps_id >= MAX_PPS_COUNT) {
1330 av_log(h->avctx, AV_LOG_ERROR, "pps_id %u out of range\n", pps_id);
1331 return AVERROR_INVALIDDATA;
1333 if (!h0->pps_buffers[pps_id]) {
1334 av_log(h->avctx, AV_LOG_ERROR,
1335 "non-existing PPS %u referenced\n",
1337 return AVERROR_INVALIDDATA;
1339 if (h0->au_pps_id >= 0 && pps_id != h0->au_pps_id) {
1340 av_log(h->avctx, AV_LOG_ERROR,
1341 "PPS change from %d to %d forbidden\n",
1342 h0->au_pps_id, pps_id);
1343 return AVERROR_INVALIDDATA;
1345 h->pps = *h0->pps_buffers[pps_id];
1347 if (!h0->sps_buffers[h->pps.sps_id]) {
1348 av_log(h->avctx, AV_LOG_ERROR,
1349 "non-existing SPS %u referenced\n",
1351 return AVERROR_INVALIDDATA;
1354 if (h->pps.sps_id != h->sps.sps_id ||
1355 h->pps.sps_id != h->current_sps_id ||
1356 h0->sps_buffers[h->pps.sps_id]->new) {
1358 h->sps = *h0->sps_buffers[h->pps.sps_id];
1360 if (h->mb_width != h->sps.mb_width ||
1361 h->mb_height != h->sps.mb_height * (2 - h->sps.frame_mbs_only_flag) ||
1362 h->avctx->bits_per_raw_sample != h->sps.bit_depth_luma ||
1363 h->cur_chroma_format_idc != h->sps.chroma_format_idc
1367 if (h->bit_depth_luma != h->sps.bit_depth_luma ||
1368 h->chroma_format_idc != h->sps.chroma_format_idc) {
1369 h->bit_depth_luma = h->sps.bit_depth_luma;
1370 h->chroma_format_idc = h->sps.chroma_format_idc;
1373 if ((ret = ff_h264_set_parameter_from_sps(h)) < 0)
1377 h->avctx->profile = ff_h264_get_profile(&h->sps);
1378 h->avctx->level = h->sps.level_idc;
1379 h->avctx->refs = h->sps.ref_frame_count;
1381 must_reinit = (h->context_initialized &&
1382 ( 16*h->sps.mb_width != h->avctx->coded_width
1383 || 16*h->sps.mb_height * (2 - h->sps.frame_mbs_only_flag) != h->avctx->coded_height
1384 || h->avctx->bits_per_raw_sample != h->sps.bit_depth_luma
1385 || h->cur_chroma_format_idc != h->sps.chroma_format_idc
1386 || av_cmp_q(h->sps.sar, h->avctx->sample_aspect_ratio)
1387 || h->mb_width != h->sps.mb_width
1388 || h->mb_height != h->sps.mb_height * (2 - h->sps.frame_mbs_only_flag)
1390 if (non_j_pixfmt(h0->avctx->pix_fmt) != non_j_pixfmt(get_pixel_format(h0, 0)))
1393 h->mb_width = h->sps.mb_width;
1394 h->mb_height = h->sps.mb_height * (2 - h->sps.frame_mbs_only_flag);
1395 h->mb_num = h->mb_width * h->mb_height;
1396 h->mb_stride = h->mb_width + 1;
1398 h->b_stride = h->mb_width * 4;
1400 h->chroma_y_shift = h->sps.chroma_format_idc <= 1; // 400 uses yuv420p
1402 h->width = 16 * h->mb_width;
1403 h->height = 16 * h->mb_height;
1405 ret = init_dimensions(h);
1409 if (h->sps.video_signal_type_present_flag) {
1410 h->avctx->color_range = h->sps.full_range>0 ? AVCOL_RANGE_JPEG
1412 if (h->sps.colour_description_present_flag) {
1413 if (h->avctx->colorspace != h->sps.colorspace)
1415 h->avctx->color_primaries = h->sps.color_primaries;
1416 h->avctx->color_trc = h->sps.color_trc;
1417 h->avctx->colorspace = h->sps.colorspace;
1421 if (h->context_initialized &&
1422 (h->width != h->avctx->coded_width ||
1423 h->height != h->avctx->coded_height ||
1427 av_log(h->avctx, AV_LOG_ERROR,
1428 "changing width %d -> %d / height %d -> %d on "
1430 h->width, h->avctx->coded_width,
1431 h->height, h->avctx->coded_height,
1432 h0->current_slice + 1);
1433 return AVERROR_INVALIDDATA;
1436 ff_h264_flush_change(h);
1438 if ((ret = get_pixel_format(h, 1)) < 0)
1440 h->avctx->pix_fmt = ret;
1442 av_log(h->avctx, AV_LOG_INFO, "Reinit context to %dx%d, "
1443 "pix_fmt: %s\n", h->width, h->height, av_get_pix_fmt_name(h->avctx->pix_fmt));
1445 if ((ret = h264_slice_header_init(h, 1)) < 0) {
1446 av_log(h->avctx, AV_LOG_ERROR,
1447 "h264_slice_header_init() failed\n");
1451 if (!h->context_initialized) {
1453 av_log(h->avctx, AV_LOG_ERROR,
1454 "Cannot (re-)initialize context during parallel decoding.\n");
1455 return AVERROR_PATCHWELCOME;
1458 if ((ret = get_pixel_format(h, 1)) < 0)
1460 h->avctx->pix_fmt = ret;
1462 if ((ret = h264_slice_header_init(h, 0)) < 0) {
1463 av_log(h->avctx, AV_LOG_ERROR,
1464 "h264_slice_header_init() failed\n");
1469 if (h == h0 && h->dequant_coeff_pps != pps_id) {
1470 h->dequant_coeff_pps = pps_id;
1471 h264_init_dequant_tables(h);
1474 h->frame_num = get_bits(&h->gb, h->sps.log2_max_frame_num);
1477 h->mb_aff_frame = 0;
1478 last_pic_structure = h0->picture_structure;
1479 last_pic_droppable = h0->droppable;
1480 h->droppable = h->nal_ref_idc == 0;
1481 if (h->sps.frame_mbs_only_flag) {
1482 h->picture_structure = PICT_FRAME;
1484 if (!h->sps.direct_8x8_inference_flag && slice_type == AV_PICTURE_TYPE_B) {
1485 av_log(h->avctx, AV_LOG_ERROR, "This stream was generated by a broken encoder, invalid 8x8 inference\n");
1488 field_pic_flag = get_bits1(&h->gb);
1489 if (field_pic_flag) {
1490 bottom_field_flag = get_bits1(&h->gb);
1491 h->picture_structure = PICT_TOP_FIELD + bottom_field_flag;
1493 h->picture_structure = PICT_FRAME;
1494 h->mb_aff_frame = h->sps.mb_aff;
1497 h->mb_field_decoding_flag = h->picture_structure != PICT_FRAME;
1499 if (h0->current_slice != 0) {
1500 if (last_pic_structure != h->picture_structure ||
1501 last_pic_droppable != h->droppable) {
1502 av_log(h->avctx, AV_LOG_ERROR,
1503 "Changing field mode (%d -> %d) between slices is not allowed\n",
1504 last_pic_structure, h->picture_structure);
1505 h->picture_structure = last_pic_structure;
1506 h->droppable = last_pic_droppable;
1507 return AVERROR_INVALIDDATA;
1508 } else if (!h0->cur_pic_ptr) {
1509 av_log(h->avctx, AV_LOG_ERROR,
1510 "unset cur_pic_ptr on slice %d\n",
1511 h0->current_slice + 1);
1512 return AVERROR_INVALIDDATA;
1515 /* Shorten frame num gaps so we don't have to allocate reference
1516 * frames just to throw them away */
1517 if (h->frame_num != h->prev_frame_num) {
1518 int unwrap_prev_frame_num = h->prev_frame_num;
1519 int max_frame_num = 1 << h->sps.log2_max_frame_num;
1521 if (unwrap_prev_frame_num > h->frame_num)
1522 unwrap_prev_frame_num -= max_frame_num;
1524 if ((h->frame_num - unwrap_prev_frame_num) > h->sps.ref_frame_count) {
1525 unwrap_prev_frame_num = (h->frame_num - h->sps.ref_frame_count) - 1;
1526 if (unwrap_prev_frame_num < 0)
1527 unwrap_prev_frame_num += max_frame_num;
1529 h->prev_frame_num = unwrap_prev_frame_num;
1533 /* See if we have a decoded first field looking for a pair...
1534 * Here, we're using that to see if we should mark previously
1535 * decode frames as "finished".
1536 * We have to do that before the "dummy" in-between frame allocation,
1537 * since that can modify h->cur_pic_ptr. */
1538 if (h0->first_field) {
1539 assert(h0->cur_pic_ptr);
1540 assert(h0->cur_pic_ptr->f.buf[0]);
1541 assert(h0->cur_pic_ptr->reference != DELAYED_PIC_REF);
1543 /* Mark old field/frame as completed */
1544 if (h0->cur_pic_ptr->tf.owner == h0->avctx) {
1545 ff_thread_report_progress(&h0->cur_pic_ptr->tf, INT_MAX,
1546 last_pic_structure == PICT_BOTTOM_FIELD);
1549 /* figure out if we have a complementary field pair */
1550 if (!FIELD_PICTURE(h) || h->picture_structure == last_pic_structure) {
1551 /* Previous field is unmatched. Don't display it, but let it
1552 * remain for reference if marked as such. */
1553 if (last_pic_structure != PICT_FRAME) {
1554 ff_thread_report_progress(&h0->cur_pic_ptr->tf, INT_MAX,
1555 last_pic_structure == PICT_TOP_FIELD);
1558 if (h0->cur_pic_ptr->frame_num != h->frame_num) {
1559 /* This and previous field were reference, but had
1560 * different frame_nums. Consider this field first in
1561 * pair. Throw away previous field except for reference
1563 if (last_pic_structure != PICT_FRAME) {
1564 ff_thread_report_progress(&h0->cur_pic_ptr->tf, INT_MAX,
1565 last_pic_structure == PICT_TOP_FIELD);
1568 /* Second field in complementary pair */
1569 if (!((last_pic_structure == PICT_TOP_FIELD &&
1570 h->picture_structure == PICT_BOTTOM_FIELD) ||
1571 (last_pic_structure == PICT_BOTTOM_FIELD &&
1572 h->picture_structure == PICT_TOP_FIELD))) {
1573 av_log(h->avctx, AV_LOG_ERROR,
1574 "Invalid field mode combination %d/%d\n",
1575 last_pic_structure, h->picture_structure);
1576 h->picture_structure = last_pic_structure;
1577 h->droppable = last_pic_droppable;
1578 return AVERROR_INVALIDDATA;
1579 } else if (last_pic_droppable != h->droppable) {
1580 avpriv_request_sample(h->avctx,
1581 "Found reference and non-reference fields in the same frame, which");
1582 h->picture_structure = last_pic_structure;
1583 h->droppable = last_pic_droppable;
1584 return AVERROR_PATCHWELCOME;
1590 while (h->frame_num != h->prev_frame_num && !h0->first_field &&
1591 h->frame_num != (h->prev_frame_num + 1) % (1 << h->sps.log2_max_frame_num)) {
1592 H264Picture *prev = h->short_ref_count ? h->short_ref[0] : NULL;
1593 av_log(h->avctx, AV_LOG_DEBUG, "Frame num gap %d %d\n",
1594 h->frame_num, h->prev_frame_num);
1595 if (!h->sps.gaps_in_frame_num_allowed_flag)
1596 for(i=0; i<FF_ARRAY_ELEMS(h->last_pocs); i++)
1597 h->last_pocs[i] = INT_MIN;
1598 ret = h264_frame_start(h);
1600 h0->first_field = 0;
1604 h->prev_frame_num++;
1605 h->prev_frame_num %= 1 << h->sps.log2_max_frame_num;
1606 h->cur_pic_ptr->frame_num = h->prev_frame_num;
1607 h->cur_pic_ptr->invalid_gap = !h->sps.gaps_in_frame_num_allowed_flag;
1608 ff_thread_report_progress(&h->cur_pic_ptr->tf, INT_MAX, 0);
1609 ff_thread_report_progress(&h->cur_pic_ptr->tf, INT_MAX, 1);
1610 ret = ff_generate_sliding_window_mmcos(h, 1);
1611 if (ret < 0 && (h->avctx->err_recognition & AV_EF_EXPLODE))
1613 ret = ff_h264_execute_ref_pic_marking(h, h->mmco, h->mmco_index);
1614 if (ret < 0 && (h->avctx->err_recognition & AV_EF_EXPLODE))
1616 /* Error concealment: If a ref is missing, copy the previous ref
1618 * FIXME: Avoiding a memcpy would be nice, but ref handling makes
1619 * many assumptions about there being no actual duplicates.
1620 * FIXME: This does not copy padding for out-of-frame motion
1621 * vectors. Given we are concealing a lost frame, this probably
1622 * is not noticeable by comparison, but it should be fixed. */
1623 if (h->short_ref_count) {
1625 av_image_copy(h->short_ref[0]->f.data,
1626 h->short_ref[0]->f.linesize,
1627 (const uint8_t **)prev->f.data,
1632 h->short_ref[0]->poc = prev->poc + 2;
1634 h->short_ref[0]->frame_num = h->prev_frame_num;
1638 /* See if we have a decoded first field looking for a pair...
1639 * We're using that to see whether to continue decoding in that
1640 * frame, or to allocate a new one. */
1641 if (h0->first_field) {
1642 assert(h0->cur_pic_ptr);
1643 assert(h0->cur_pic_ptr->f.buf[0]);
1644 assert(h0->cur_pic_ptr->reference != DELAYED_PIC_REF);
1646 /* figure out if we have a complementary field pair */
1647 if (!FIELD_PICTURE(h) || h->picture_structure == last_pic_structure) {
1648 /* Previous field is unmatched. Don't display it, but let it
1649 * remain for reference if marked as such. */
1650 h0->cur_pic_ptr = NULL;
1651 h0->first_field = FIELD_PICTURE(h);
1653 if (h0->cur_pic_ptr->frame_num != h->frame_num) {
1654 ff_thread_report_progress(&h0->cur_pic_ptr->tf, INT_MAX,
1655 h0->picture_structure==PICT_BOTTOM_FIELD);
1656 /* This and the previous field had different frame_nums.
1657 * Consider this field first in pair. Throw away previous
1658 * one except for reference purposes. */
1659 h0->first_field = 1;
1660 h0->cur_pic_ptr = NULL;
1662 /* Second field in complementary pair */
1663 h0->first_field = 0;
1667 /* Frame or first field in a potentially complementary pair */
1668 h0->first_field = FIELD_PICTURE(h);
1671 if (!FIELD_PICTURE(h) || h0->first_field) {
1672 if (h264_frame_start(h) < 0) {
1673 h0->first_field = 0;
1674 return AVERROR_INVALIDDATA;
1677 release_unused_pictures(h, 0);
1679 /* Some macroblocks can be accessed before they're available in case
1680 * of lost slices, MBAFF or threading. */
1681 if (FIELD_PICTURE(h)) {
1682 for(i = (h->picture_structure == PICT_BOTTOM_FIELD); i<h->mb_height; i++)
1683 memset(h->slice_table + i*h->mb_stride, -1, (h->mb_stride - (i+1==h->mb_height)) * sizeof(*h->slice_table));
1685 memset(h->slice_table, -1,
1686 (h->mb_height * h->mb_stride - 1) * sizeof(*h->slice_table));
1688 h0->last_slice_type = -1;
1690 if (h != h0 && (ret = clone_slice(h, h0)) < 0)
1693 /* can't be in alloc_tables because linesize isn't known there.
1694 * FIXME: redo bipred weight to not require extra buffer? */
1695 for (i = 0; i < h->slice_context_count; i++)
1696 if (h->thread_context[i]) {
1697 ret = alloc_scratch_buffers(h->thread_context[i], h->linesize);
1702 h->cur_pic_ptr->frame_num = h->frame_num; // FIXME frame_num cleanup
1704 av_assert1(h->mb_num == h->mb_width * h->mb_height);
1705 if (first_mb_in_slice << FIELD_OR_MBAFF_PICTURE(h) >= h->mb_num ||
1706 first_mb_in_slice >= h->mb_num) {
1707 av_log(h->avctx, AV_LOG_ERROR, "first_mb_in_slice overflow\n");
1708 return AVERROR_INVALIDDATA;
1710 h->resync_mb_x = h->mb_x = first_mb_in_slice % h->mb_width;
1711 h->resync_mb_y = h->mb_y = (first_mb_in_slice / h->mb_width) <<
1712 FIELD_OR_MBAFF_PICTURE(h);
1713 if (h->picture_structure == PICT_BOTTOM_FIELD)
1714 h->resync_mb_y = h->mb_y = h->mb_y + 1;
1715 av_assert1(h->mb_y < h->mb_height);
1717 if (h->picture_structure == PICT_FRAME) {
1718 h->curr_pic_num = h->frame_num;
1719 h->max_pic_num = 1 << h->sps.log2_max_frame_num;
1721 h->curr_pic_num = 2 * h->frame_num + 1;
1722 h->max_pic_num = 1 << (h->sps.log2_max_frame_num + 1);
1725 if (h->nal_unit_type == NAL_IDR_SLICE)
1726 get_ue_golomb(&h->gb); /* idr_pic_id */
1728 if (h->sps.poc_type == 0) {
1729 h->poc_lsb = get_bits(&h->gb, h->sps.log2_max_poc_lsb);
1731 if (h->pps.pic_order_present == 1 && h->picture_structure == PICT_FRAME)
1732 h->delta_poc_bottom = get_se_golomb(&h->gb);
1735 if (h->sps.poc_type == 1 && !h->sps.delta_pic_order_always_zero_flag) {
1736 h->delta_poc[0] = get_se_golomb(&h->gb);
1738 if (h->pps.pic_order_present == 1 && h->picture_structure == PICT_FRAME)
1739 h->delta_poc[1] = get_se_golomb(&h->gb);
1742 ff_init_poc(h, h->cur_pic_ptr->field_poc, &h->cur_pic_ptr->poc);
1744 if (h->pps.redundant_pic_cnt_present)
1745 h->redundant_pic_count = get_ue_golomb(&h->gb);
1747 ret = ff_set_ref_count(h);
1751 if (slice_type != AV_PICTURE_TYPE_I &&
1752 (h0->current_slice == 0 ||
1753 slice_type != h0->last_slice_type ||
1754 memcmp(h0->last_ref_count, h0->ref_count, sizeof(h0->ref_count)))) {
1756 ff_h264_fill_default_ref_list(h);
1759 if (h->slice_type_nos != AV_PICTURE_TYPE_I) {
1760 ret = ff_h264_decode_ref_pic_list_reordering(h);
1762 h->ref_count[1] = h->ref_count[0] = 0;
1767 if ((h->pps.weighted_pred && h->slice_type_nos == AV_PICTURE_TYPE_P) ||
1768 (h->pps.weighted_bipred_idc == 1 &&
1769 h->slice_type_nos == AV_PICTURE_TYPE_B))
1770 ff_pred_weight_table(h);
1771 else if (h->pps.weighted_bipred_idc == 2 &&
1772 h->slice_type_nos == AV_PICTURE_TYPE_B) {
1773 implicit_weight_table(h, -1);
1776 for (i = 0; i < 2; i++) {
1777 h->luma_weight_flag[i] = 0;
1778 h->chroma_weight_flag[i] = 0;
1782 // If frame-mt is enabled, only update mmco tables for the first slice
1783 // in a field. Subsequent slices can temporarily clobber h->mmco_index
1784 // or h->mmco, which will cause ref list mix-ups and decoding errors
1785 // further down the line. This may break decoding if the first slice is
1786 // corrupt, thus we only do this if frame-mt is enabled.
1787 if (h->nal_ref_idc) {
1788 ret = ff_h264_decode_ref_pic_marking(h0, &h->gb,
1789 !(h->avctx->active_thread_type & FF_THREAD_FRAME) ||
1790 h0->current_slice == 0);
1791 if (ret < 0 && (h->avctx->err_recognition & AV_EF_EXPLODE))
1792 return AVERROR_INVALIDDATA;
1795 if (FRAME_MBAFF(h)) {
1796 ff_h264_fill_mbaff_ref_list(h);
1798 if (h->pps.weighted_bipred_idc == 2 && h->slice_type_nos == AV_PICTURE_TYPE_B) {
1799 implicit_weight_table(h, 0);
1800 implicit_weight_table(h, 1);
1804 if (h->slice_type_nos == AV_PICTURE_TYPE_B && !h->direct_spatial_mv_pred)
1805 ff_h264_direct_dist_scale_factor(h);
1806 ff_h264_direct_ref_list_init(h);
1808 if (h->slice_type_nos != AV_PICTURE_TYPE_I && h->pps.cabac) {
1809 tmp = get_ue_golomb_31(&h->gb);
1811 av_log(h->avctx, AV_LOG_ERROR, "cabac_init_idc %u overflow\n", tmp);
1812 return AVERROR_INVALIDDATA;
1814 h->cabac_init_idc = tmp;
1817 h->last_qscale_diff = 0;
1818 tmp = h->pps.init_qp + get_se_golomb(&h->gb);
1819 if (tmp > 51 + 6 * (h->sps.bit_depth_luma - 8)) {
1820 av_log(h->avctx, AV_LOG_ERROR, "QP %u out of range\n", tmp);
1821 return AVERROR_INVALIDDATA;
1824 h->chroma_qp[0] = get_chroma_qp(h, 0, h->qscale);
1825 h->chroma_qp[1] = get_chroma_qp(h, 1, h->qscale);
1826 // FIXME qscale / qp ... stuff
1827 if (h->slice_type == AV_PICTURE_TYPE_SP)
1828 get_bits1(&h->gb); /* sp_for_switch_flag */
1829 if (h->slice_type == AV_PICTURE_TYPE_SP ||
1830 h->slice_type == AV_PICTURE_TYPE_SI)
1831 get_se_golomb(&h->gb); /* slice_qs_delta */
1833 h->deblocking_filter = 1;
1834 h->slice_alpha_c0_offset = 0;
1835 h->slice_beta_offset = 0;
1836 if (h->pps.deblocking_filter_parameters_present) {
1837 tmp = get_ue_golomb_31(&h->gb);
1839 av_log(h->avctx, AV_LOG_ERROR,
1840 "deblocking_filter_idc %u out of range\n", tmp);
1841 return AVERROR_INVALIDDATA;
1843 h->deblocking_filter = tmp;
1844 if (h->deblocking_filter < 2)
1845 h->deblocking_filter ^= 1; // 1<->0
1847 if (h->deblocking_filter) {
1848 h->slice_alpha_c0_offset = get_se_golomb(&h->gb) * 2;
1849 h->slice_beta_offset = get_se_golomb(&h->gb) * 2;
1850 if (h->slice_alpha_c0_offset > 12 ||
1851 h->slice_alpha_c0_offset < -12 ||
1852 h->slice_beta_offset > 12 ||
1853 h->slice_beta_offset < -12) {
1854 av_log(h->avctx, AV_LOG_ERROR,
1855 "deblocking filter parameters %d %d out of range\n",
1856 h->slice_alpha_c0_offset, h->slice_beta_offset);
1857 return AVERROR_INVALIDDATA;
1862 if (h->avctx->skip_loop_filter >= AVDISCARD_ALL ||
1863 (h->avctx->skip_loop_filter >= AVDISCARD_NONKEY &&
1864 h->slice_type_nos != AV_PICTURE_TYPE_I) ||
1865 (h->avctx->skip_loop_filter >= AVDISCARD_BIDIR &&
1866 h->slice_type_nos == AV_PICTURE_TYPE_B) ||
1867 (h->avctx->skip_loop_filter >= AVDISCARD_NONREF &&
1868 h->nal_ref_idc == 0))
1869 h->deblocking_filter = 0;
1871 if (h->deblocking_filter == 1 && h0->max_contexts > 1) {
1872 if (h->avctx->flags2 & CODEC_FLAG2_FAST) {
1873 /* Cheat slightly for speed:
1874 * Do not bother to deblock across slices. */
1875 h->deblocking_filter = 2;
1877 h0->max_contexts = 1;
1878 if (!h0->single_decode_warning) {
1879 av_log(h->avctx, AV_LOG_INFO,
1880 "Cannot parallelize deblocking type 1, decoding such frames in sequential order\n");
1881 h0->single_decode_warning = 1;
1884 av_log(h->avctx, AV_LOG_ERROR,
1885 "Deblocking switched inside frame.\n");
1891 FFMIN(h->slice_alpha_c0_offset, h->slice_beta_offset) -
1893 h->pps.chroma_qp_index_offset[0],
1894 h->pps.chroma_qp_index_offset[1]) +
1895 6 * (h->sps.bit_depth_luma - 8);
1897 h0->last_slice_type = slice_type;
1898 memcpy(h0->last_ref_count, h0->ref_count, sizeof(h0->last_ref_count));
1899 h->slice_num = ++h0->current_slice;
1902 h0->slice_row[(h->slice_num-1)&(MAX_SLICES-1)]= h->resync_mb_y;
1903 if ( h0->slice_row[h->slice_num&(MAX_SLICES-1)] + 3 >= h->resync_mb_y
1904 && h0->slice_row[h->slice_num&(MAX_SLICES-1)] <= h->resync_mb_y
1905 && h->slice_num >= MAX_SLICES) {
1906 //in case of ASO this check needs to be updated depending on how we decide to assign slice numbers in this case
1907 av_log(h->avctx, AV_LOG_WARNING, "Possibly too many slices (%d >= %d), increase MAX_SLICES and recompile if there are artifacts\n", h->slice_num, MAX_SLICES);
1910 for (j = 0; j < 2; j++) {
1912 int *ref2frm = h->ref2frm[h->slice_num & (MAX_SLICES - 1)][j];
1913 for (i = 0; i < 16; i++) {
1915 if (j < h->list_count && i < h->ref_count[j] &&
1916 h->ref_list[j][i].f.buf[0]) {
1918 AVBuffer *buf = h->ref_list[j][i].f.buf[0]->buffer;
1919 for (k = 0; k < h->short_ref_count; k++)
1920 if (h->short_ref[k]->f.buf[0]->buffer == buf) {
1924 for (k = 0; k < h->long_ref_count; k++)
1925 if (h->long_ref[k] && h->long_ref[k]->f.buf[0]->buffer == buf) {
1926 id_list[i] = h->short_ref_count + k;
1934 for (i = 0; i < 16; i++)
1935 ref2frm[i + 2] = 4 * id_list[i] + (h->ref_list[j][i].reference & 3);
1937 ref2frm[18 + 1] = -1;
1938 for (i = 16; i < 48; i++)
1939 ref2frm[i + 4] = 4 * id_list[(i - 16) >> 1] +
1940 (h->ref_list[j][i].reference & 3);
1943 if (h->ref_count[0]) ff_h264_set_erpic(&h->er.last_pic, &h->ref_list[0][0]);
1944 if (h->ref_count[1]) ff_h264_set_erpic(&h->er.next_pic, &h->ref_list[1][0]);
1946 h->er.ref_count = h->ref_count[0];
1947 h0->au_pps_id = pps_id;
1949 h0->sps_buffers[h->pps.sps_id]->new = 0;
1950 h->current_sps_id = h->pps.sps_id;
1952 if (h->avctx->debug & FF_DEBUG_PICT_INFO) {
1953 av_log(h->avctx, AV_LOG_DEBUG,
1954 "slice:%d %s mb:%d %c%s%s pps:%u frame:%d poc:%d/%d ref:%d/%d qp:%d loop:%d:%d:%d weight:%d%s %s\n",
1956 (h->picture_structure == PICT_FRAME ? "F" : h->picture_structure == PICT_TOP_FIELD ? "T" : "B"),
1958 av_get_picture_type_char(h->slice_type),
1959 h->slice_type_fixed ? " fix" : "",
1960 h->nal_unit_type == NAL_IDR_SLICE ? " IDR" : "",
1961 pps_id, h->frame_num,
1962 h->cur_pic_ptr->field_poc[0],
1963 h->cur_pic_ptr->field_poc[1],
1964 h->ref_count[0], h->ref_count[1],
1966 h->deblocking_filter,
1967 h->slice_alpha_c0_offset, h->slice_beta_offset,
1969 h->use_weight == 1 && h->use_weight_chroma ? "c" : "",
1970 h->slice_type == AV_PICTURE_TYPE_B ? (h->direct_spatial_mv_pred ? "SPAT" : "TEMP") : "");
1976 int ff_h264_get_slice_type(const H264Context *h)
1978 switch (h->slice_type) {
1979 case AV_PICTURE_TYPE_P:
1981 case AV_PICTURE_TYPE_B:
1983 case AV_PICTURE_TYPE_I:
1985 case AV_PICTURE_TYPE_SP:
1987 case AV_PICTURE_TYPE_SI:
1990 return AVERROR_INVALIDDATA;
1994 static av_always_inline void fill_filter_caches_inter(H264Context *h,
1995 int mb_type, int top_xy,
1996 int left_xy[LEFT_MBS],
1998 int left_type[LEFT_MBS],
1999 int mb_xy, int list)
2001 int b_stride = h->b_stride;
2002 int16_t(*mv_dst)[2] = &h->mv_cache[list][scan8[0]];
2003 int8_t *ref_cache = &h->ref_cache[list][scan8[0]];
2004 if (IS_INTER(mb_type) || IS_DIRECT(mb_type)) {
2005 if (USES_LIST(top_type, list)) {
2006 const int b_xy = h->mb2b_xy[top_xy] + 3 * b_stride;
2007 const int b8_xy = 4 * top_xy + 2;
2008 int (*ref2frm)[64] = (void*)(h->ref2frm[h->slice_table[top_xy] & (MAX_SLICES - 1)][0] + (MB_MBAFF(h) ? 20 : 2));
2009 AV_COPY128(mv_dst - 1 * 8, h->cur_pic.motion_val[list][b_xy + 0]);
2010 ref_cache[0 - 1 * 8] =
2011 ref_cache[1 - 1 * 8] = ref2frm[list][h->cur_pic.ref_index[list][b8_xy + 0]];
2012 ref_cache[2 - 1 * 8] =
2013 ref_cache[3 - 1 * 8] = ref2frm[list][h->cur_pic.ref_index[list][b8_xy + 1]];
2015 AV_ZERO128(mv_dst - 1 * 8);
2016 AV_WN32A(&ref_cache[0 - 1 * 8], ((LIST_NOT_USED) & 0xFF) * 0x01010101u);
2019 if (!IS_INTERLACED(mb_type ^ left_type[LTOP])) {
2020 if (USES_LIST(left_type[LTOP], list)) {
2021 const int b_xy = h->mb2b_xy[left_xy[LTOP]] + 3;
2022 const int b8_xy = 4 * left_xy[LTOP] + 1;
2023 int (*ref2frm)[64] =(void*)( h->ref2frm[h->slice_table[left_xy[LTOP]] & (MAX_SLICES - 1)][0] + (MB_MBAFF(h) ? 20 : 2));
2024 AV_COPY32(mv_dst - 1 + 0, h->cur_pic.motion_val[list][b_xy + b_stride * 0]);
2025 AV_COPY32(mv_dst - 1 + 8, h->cur_pic.motion_val[list][b_xy + b_stride * 1]);
2026 AV_COPY32(mv_dst - 1 + 16, h->cur_pic.motion_val[list][b_xy + b_stride * 2]);
2027 AV_COPY32(mv_dst - 1 + 24, h->cur_pic.motion_val[list][b_xy + b_stride * 3]);
2029 ref_cache[-1 + 8] = ref2frm[list][h->cur_pic.ref_index[list][b8_xy + 2 * 0]];
2030 ref_cache[-1 + 16] =
2031 ref_cache[-1 + 24] = ref2frm[list][h->cur_pic.ref_index[list][b8_xy + 2 * 1]];
2033 AV_ZERO32(mv_dst - 1 + 0);
2034 AV_ZERO32(mv_dst - 1 + 8);
2035 AV_ZERO32(mv_dst - 1 + 16);
2036 AV_ZERO32(mv_dst - 1 + 24);
2039 ref_cache[-1 + 16] =
2040 ref_cache[-1 + 24] = LIST_NOT_USED;
2045 if (!USES_LIST(mb_type, list)) {
2046 fill_rectangle(mv_dst, 4, 4, 8, pack16to32(0, 0), 4);
2047 AV_WN32A(&ref_cache[0 * 8], ((LIST_NOT_USED) & 0xFF) * 0x01010101u);
2048 AV_WN32A(&ref_cache[1 * 8], ((LIST_NOT_USED) & 0xFF) * 0x01010101u);
2049 AV_WN32A(&ref_cache[2 * 8], ((LIST_NOT_USED) & 0xFF) * 0x01010101u);
2050 AV_WN32A(&ref_cache[3 * 8], ((LIST_NOT_USED) & 0xFF) * 0x01010101u);
2055 int8_t *ref = &h->cur_pic.ref_index[list][4 * mb_xy];
2056 int (*ref2frm)[64] = (void*)(h->ref2frm[h->slice_num & (MAX_SLICES - 1)][0] + (MB_MBAFF(h) ? 20 : 2));
2057 uint32_t ref01 = (pack16to32(ref2frm[list][ref[0]], ref2frm[list][ref[1]]) & 0x00FF00FF) * 0x0101;
2058 uint32_t ref23 = (pack16to32(ref2frm[list][ref[2]], ref2frm[list][ref[3]]) & 0x00FF00FF) * 0x0101;
2059 AV_WN32A(&ref_cache[0 * 8], ref01);
2060 AV_WN32A(&ref_cache[1 * 8], ref01);
2061 AV_WN32A(&ref_cache[2 * 8], ref23);
2062 AV_WN32A(&ref_cache[3 * 8], ref23);
2066 int16_t(*mv_src)[2] = &h->cur_pic.motion_val[list][4 * h->mb_x + 4 * h->mb_y * b_stride];
2067 AV_COPY128(mv_dst + 8 * 0, mv_src + 0 * b_stride);
2068 AV_COPY128(mv_dst + 8 * 1, mv_src + 1 * b_stride);
2069 AV_COPY128(mv_dst + 8 * 2, mv_src + 2 * b_stride);
2070 AV_COPY128(mv_dst + 8 * 3, mv_src + 3 * b_stride);
2076 * @return non zero if the loop filter can be skipped
2078 static int fill_filter_caches(H264Context *h, int mb_type)
2080 const int mb_xy = h->mb_xy;
2081 int top_xy, left_xy[LEFT_MBS];
2082 int top_type, left_type[LEFT_MBS];
2086 top_xy = mb_xy - (h->mb_stride << MB_FIELD(h));
2088 /* Wow, what a mess, why didn't they simplify the interlacing & intra
2089 * stuff, I can't imagine that these complex rules are worth it. */
2091 left_xy[LBOT] = left_xy[LTOP] = mb_xy - 1;
2092 if (FRAME_MBAFF(h)) {
2093 const int left_mb_field_flag = IS_INTERLACED(h->cur_pic.mb_type[mb_xy - 1]);
2094 const int curr_mb_field_flag = IS_INTERLACED(mb_type);
2096 if (left_mb_field_flag != curr_mb_field_flag)
2097 left_xy[LTOP] -= h->mb_stride;
2099 if (curr_mb_field_flag)
2100 top_xy += h->mb_stride &
2101 (((h->cur_pic.mb_type[top_xy] >> 7) & 1) - 1);
2102 if (left_mb_field_flag != curr_mb_field_flag)
2103 left_xy[LBOT] += h->mb_stride;
2107 h->top_mb_xy = top_xy;
2108 h->left_mb_xy[LTOP] = left_xy[LTOP];
2109 h->left_mb_xy[LBOT] = left_xy[LBOT];
2111 /* For sufficiently low qp, filtering wouldn't do anything.
2112 * This is a conservative estimate: could also check beta_offset
2113 * and more accurate chroma_qp. */
2114 int qp_thresh = h->qp_thresh; // FIXME strictly we should store qp_thresh for each mb of a slice
2115 int qp = h->cur_pic.qscale_table[mb_xy];
2116 if (qp <= qp_thresh &&
2117 (left_xy[LTOP] < 0 ||
2118 ((qp + h->cur_pic.qscale_table[left_xy[LTOP]] + 1) >> 1) <= qp_thresh) &&
2120 ((qp + h->cur_pic.qscale_table[top_xy] + 1) >> 1) <= qp_thresh)) {
2121 if (!FRAME_MBAFF(h))
2123 if ((left_xy[LTOP] < 0 ||
2124 ((qp + h->cur_pic.qscale_table[left_xy[LBOT]] + 1) >> 1) <= qp_thresh) &&
2125 (top_xy < h->mb_stride ||
2126 ((qp + h->cur_pic.qscale_table[top_xy - h->mb_stride] + 1) >> 1) <= qp_thresh))
2131 top_type = h->cur_pic.mb_type[top_xy];
2132 left_type[LTOP] = h->cur_pic.mb_type[left_xy[LTOP]];
2133 left_type[LBOT] = h->cur_pic.mb_type[left_xy[LBOT]];
2134 if (h->deblocking_filter == 2) {
2135 if (h->slice_table[top_xy] != h->slice_num)
2137 if (h->slice_table[left_xy[LBOT]] != h->slice_num)
2138 left_type[LTOP] = left_type[LBOT] = 0;
2140 if (h->slice_table[top_xy] == 0xFFFF)
2142 if (h->slice_table[left_xy[LBOT]] == 0xFFFF)
2143 left_type[LTOP] = left_type[LBOT] = 0;
2145 h->top_type = top_type;
2146 h->left_type[LTOP] = left_type[LTOP];
2147 h->left_type[LBOT] = left_type[LBOT];
2149 if (IS_INTRA(mb_type))
2152 fill_filter_caches_inter(h, mb_type, top_xy, left_xy,
2153 top_type, left_type, mb_xy, 0);
2154 if (h->list_count == 2)
2155 fill_filter_caches_inter(h, mb_type, top_xy, left_xy,
2156 top_type, left_type, mb_xy, 1);
2158 nnz = h->non_zero_count[mb_xy];
2159 nnz_cache = h->non_zero_count_cache;
2160 AV_COPY32(&nnz_cache[4 + 8 * 1], &nnz[0]);
2161 AV_COPY32(&nnz_cache[4 + 8 * 2], &nnz[4]);
2162 AV_COPY32(&nnz_cache[4 + 8 * 3], &nnz[8]);
2163 AV_COPY32(&nnz_cache[4 + 8 * 4], &nnz[12]);
2164 h->cbp = h->cbp_table[mb_xy];
2167 nnz = h->non_zero_count[top_xy];
2168 AV_COPY32(&nnz_cache[4 + 8 * 0], &nnz[3 * 4]);
2171 if (left_type[LTOP]) {
2172 nnz = h->non_zero_count[left_xy[LTOP]];
2173 nnz_cache[3 + 8 * 1] = nnz[3 + 0 * 4];
2174 nnz_cache[3 + 8 * 2] = nnz[3 + 1 * 4];
2175 nnz_cache[3 + 8 * 3] = nnz[3 + 2 * 4];
2176 nnz_cache[3 + 8 * 4] = nnz[3 + 3 * 4];
2179 /* CAVLC 8x8dct requires NNZ values for residual decoding that differ
2180 * from what the loop filter needs */
2181 if (!CABAC(h) && h->pps.transform_8x8_mode) {
2182 if (IS_8x8DCT(top_type)) {
2183 nnz_cache[4 + 8 * 0] =
2184 nnz_cache[5 + 8 * 0] = (h->cbp_table[top_xy] & 0x4000) >> 12;
2185 nnz_cache[6 + 8 * 0] =
2186 nnz_cache[7 + 8 * 0] = (h->cbp_table[top_xy] & 0x8000) >> 12;
2188 if (IS_8x8DCT(left_type[LTOP])) {
2189 nnz_cache[3 + 8 * 1] =
2190 nnz_cache[3 + 8 * 2] = (h->cbp_table[left_xy[LTOP]] & 0x2000) >> 12; // FIXME check MBAFF
2192 if (IS_8x8DCT(left_type[LBOT])) {
2193 nnz_cache[3 + 8 * 3] =
2194 nnz_cache[3 + 8 * 4] = (h->cbp_table[left_xy[LBOT]] & 0x8000) >> 12; // FIXME check MBAFF
2197 if (IS_8x8DCT(mb_type)) {
2198 nnz_cache[scan8[0]] =
2199 nnz_cache[scan8[1]] =
2200 nnz_cache[scan8[2]] =
2201 nnz_cache[scan8[3]] = (h->cbp & 0x1000) >> 12;
2203 nnz_cache[scan8[0 + 4]] =
2204 nnz_cache[scan8[1 + 4]] =
2205 nnz_cache[scan8[2 + 4]] =
2206 nnz_cache[scan8[3 + 4]] = (h->cbp & 0x2000) >> 12;
2208 nnz_cache[scan8[0 + 8]] =
2209 nnz_cache[scan8[1 + 8]] =
2210 nnz_cache[scan8[2 + 8]] =
2211 nnz_cache[scan8[3 + 8]] = (h->cbp & 0x4000) >> 12;
2213 nnz_cache[scan8[0 + 12]] =
2214 nnz_cache[scan8[1 + 12]] =
2215 nnz_cache[scan8[2 + 12]] =
2216 nnz_cache[scan8[3 + 12]] = (h->cbp & 0x8000) >> 12;
2223 static void loop_filter(H264Context *h, int start_x, int end_x)
2225 uint8_t *dest_y, *dest_cb, *dest_cr;
2226 int linesize, uvlinesize, mb_x, mb_y;
2227 const int end_mb_y = h->mb_y + FRAME_MBAFF(h);
2228 const int old_slice_type = h->slice_type;
2229 const int pixel_shift = h->pixel_shift;
2230 const int block_h = 16 >> h->chroma_y_shift;
2232 if (h->deblocking_filter) {
2233 for (mb_x = start_x; mb_x < end_x; mb_x++)
2234 for (mb_y = end_mb_y - FRAME_MBAFF(h); mb_y <= end_mb_y; mb_y++) {
2236 mb_xy = h->mb_xy = mb_x + mb_y * h->mb_stride;
2237 h->slice_num = h->slice_table[mb_xy];
2238 mb_type = h->cur_pic.mb_type[mb_xy];
2239 h->list_count = h->list_counts[mb_xy];
2243 h->mb_field_decoding_flag = !!IS_INTERLACED(mb_type);
2247 dest_y = h->cur_pic.f.data[0] +
2248 ((mb_x << pixel_shift) + mb_y * h->linesize) * 16;
2249 dest_cb = h->cur_pic.f.data[1] +
2250 (mb_x << pixel_shift) * (8 << CHROMA444(h)) +
2251 mb_y * h->uvlinesize * block_h;
2252 dest_cr = h->cur_pic.f.data[2] +
2253 (mb_x << pixel_shift) * (8 << CHROMA444(h)) +
2254 mb_y * h->uvlinesize * block_h;
2255 // FIXME simplify above
2258 linesize = h->mb_linesize = h->linesize * 2;
2259 uvlinesize = h->mb_uvlinesize = h->uvlinesize * 2;
2260 if (mb_y & 1) { // FIXME move out of this function?
2261 dest_y -= h->linesize * 15;
2262 dest_cb -= h->uvlinesize * (block_h - 1);
2263 dest_cr -= h->uvlinesize * (block_h - 1);
2266 linesize = h->mb_linesize = h->linesize;
2267 uvlinesize = h->mb_uvlinesize = h->uvlinesize;
2269 backup_mb_border(h, dest_y, dest_cb, dest_cr, linesize,
2271 if (fill_filter_caches(h, mb_type))
2273 h->chroma_qp[0] = get_chroma_qp(h, 0, h->cur_pic.qscale_table[mb_xy]);
2274 h->chroma_qp[1] = get_chroma_qp(h, 1, h->cur_pic.qscale_table[mb_xy]);
2276 if (FRAME_MBAFF(h)) {
2277 ff_h264_filter_mb(h, mb_x, mb_y, dest_y, dest_cb, dest_cr,
2278 linesize, uvlinesize);
2280 ff_h264_filter_mb_fast(h, mb_x, mb_y, dest_y, dest_cb,
2281 dest_cr, linesize, uvlinesize);
2285 h->slice_type = old_slice_type;
2287 h->mb_y = end_mb_y - FRAME_MBAFF(h);
2288 h->chroma_qp[0] = get_chroma_qp(h, 0, h->qscale);
2289 h->chroma_qp[1] = get_chroma_qp(h, 1, h->qscale);
2292 static void predict_field_decoding_flag(H264Context *h)
2294 const int mb_xy = h->mb_x + h->mb_y * h->mb_stride;
2295 int mb_type = (h->slice_table[mb_xy - 1] == h->slice_num) ?
2296 h->cur_pic.mb_type[mb_xy - 1] :
2297 (h->slice_table[mb_xy - h->mb_stride] == h->slice_num) ?
2298 h->cur_pic.mb_type[mb_xy - h->mb_stride] : 0;
2299 h->mb_mbaff = h->mb_field_decoding_flag = IS_INTERLACED(mb_type) ? 1 : 0;
2303 * Draw edges and report progress for the last MB row.
2305 static void decode_finish_row(H264Context *h)
2307 int top = 16 * (h->mb_y >> FIELD_PICTURE(h));
2308 int pic_height = 16 * h->mb_height >> FIELD_PICTURE(h);
2309 int height = 16 << FRAME_MBAFF(h);
2310 int deblock_border = (16 + 4) << FRAME_MBAFF(h);
2312 if (h->deblocking_filter) {
2313 if ((top + height) >= pic_height)
2314 height += deblock_border;
2315 top -= deblock_border;
2318 if (top >= pic_height || (top + height) < 0)
2321 height = FFMIN(height, pic_height - top);
2323 height = top + height;
2327 ff_h264_draw_horiz_band(h, top, height);
2329 if (h->droppable || h->er.error_occurred)
2332 ff_thread_report_progress(&h->cur_pic_ptr->tf, top + height - 1,
2333 h->picture_structure == PICT_BOTTOM_FIELD);
2336 static void er_add_slice(H264Context *h, int startx, int starty,
2337 int endx, int endy, int status)
2339 if (CONFIG_ERROR_RESILIENCE) {
2340 ERContext *er = &h->er;
2342 ff_er_add_slice(er, startx, starty, endx, endy, status);
2346 static int decode_slice(struct AVCodecContext *avctx, void *arg)
2348 H264Context *h = *(void **)arg;
2349 int lf_x_start = h->mb_x;
2351 h->mb_skip_run = -1;
2353 av_assert0(h->block_offset[15] == (4 * ((scan8[15] - scan8[0]) & 7) << h->pixel_shift) + 4 * h->linesize * ((scan8[15] - scan8[0]) >> 3));
2355 h->is_complex = FRAME_MBAFF(h) || h->picture_structure != PICT_FRAME ||
2356 avctx->codec_id != AV_CODEC_ID_H264 ||
2357 (CONFIG_GRAY && (h->flags & CODEC_FLAG_GRAY));
2359 if (!(h->avctx->active_thread_type & FF_THREAD_SLICE) && h->picture_structure == PICT_FRAME && h->er.error_status_table) {
2360 const int start_i = av_clip(h->resync_mb_x + h->resync_mb_y * h->mb_width, 0, h->mb_num - 1);
2362 int prev_status = h->er.error_status_table[h->er.mb_index2xy[start_i - 1]];
2363 prev_status &= ~ VP_START;
2364 if (prev_status != (ER_MV_END | ER_DC_END | ER_AC_END))
2365 h->er.error_occurred = 1;
2371 align_get_bits(&h->gb);
2374 ff_init_cabac_decoder(&h->cabac,
2375 h->gb.buffer + get_bits_count(&h->gb) / 8,
2376 (get_bits_left(&h->gb) + 7) / 8);
2378 ff_h264_init_cabac_states(h);
2382 int ret = ff_h264_decode_mb_cabac(h);
2384 // STOP_TIMER("decode_mb_cabac")
2387 ff_h264_hl_decode_mb(h);
2389 // FIXME optimal? or let mb_decode decode 16x32 ?
2390 if (ret >= 0 && FRAME_MBAFF(h)) {
2393 ret = ff_h264_decode_mb_cabac(h);
2396 ff_h264_hl_decode_mb(h);
2399 eos = get_cabac_terminate(&h->cabac);
2401 if ((h->workaround_bugs & FF_BUG_TRUNCATED) &&
2402 h->cabac.bytestream > h->cabac.bytestream_end + 2) {
2403 er_add_slice(h, h->resync_mb_x, h->resync_mb_y, h->mb_x - 1,
2404 h->mb_y, ER_MB_END);
2405 if (h->mb_x >= lf_x_start)
2406 loop_filter(h, lf_x_start, h->mb_x + 1);
2409 if (h->cabac.bytestream > h->cabac.bytestream_end + 2 )
2410 av_log(h->avctx, AV_LOG_DEBUG, "bytestream overread %td\n", h->cabac.bytestream_end - h->cabac.bytestream);
2411 if (ret < 0 || h->cabac.bytestream > h->cabac.bytestream_end + 4) {
2412 av_log(h->avctx, AV_LOG_ERROR,
2413 "error while decoding MB %d %d, bytestream %td\n",
2415 h->cabac.bytestream_end - h->cabac.bytestream);
2416 er_add_slice(h, h->resync_mb_x, h->resync_mb_y, h->mb_x,
2417 h->mb_y, ER_MB_ERROR);
2418 return AVERROR_INVALIDDATA;
2421 if (++h->mb_x >= h->mb_width) {
2422 loop_filter(h, lf_x_start, h->mb_x);
2423 h->mb_x = lf_x_start = 0;
2424 decode_finish_row(h);
2426 if (FIELD_OR_MBAFF_PICTURE(h)) {
2428 if (FRAME_MBAFF(h) && h->mb_y < h->mb_height)
2429 predict_field_decoding_flag(h);
2433 if (eos || h->mb_y >= h->mb_height) {
2434 tprintf(h->avctx, "slice end %d %d\n",
2435 get_bits_count(&h->gb), h->gb.size_in_bits);
2436 er_add_slice(h, h->resync_mb_x, h->resync_mb_y, h->mb_x - 1,
2437 h->mb_y, ER_MB_END);
2438 if (h->mb_x > lf_x_start)
2439 loop_filter(h, lf_x_start, h->mb_x);
2445 int ret = ff_h264_decode_mb_cavlc(h);
2448 ff_h264_hl_decode_mb(h);
2450 // FIXME optimal? or let mb_decode decode 16x32 ?
2451 if (ret >= 0 && FRAME_MBAFF(h)) {
2453 ret = ff_h264_decode_mb_cavlc(h);
2456 ff_h264_hl_decode_mb(h);
2461 av_log(h->avctx, AV_LOG_ERROR,
2462 "error while decoding MB %d %d\n", h->mb_x, h->mb_y);
2463 er_add_slice(h, h->resync_mb_x, h->resync_mb_y, h->mb_x,
2464 h->mb_y, ER_MB_ERROR);
2468 if (++h->mb_x >= h->mb_width) {
2469 loop_filter(h, lf_x_start, h->mb_x);
2470 h->mb_x = lf_x_start = 0;
2471 decode_finish_row(h);
2473 if (FIELD_OR_MBAFF_PICTURE(h)) {
2475 if (FRAME_MBAFF(h) && h->mb_y < h->mb_height)
2476 predict_field_decoding_flag(h);
2478 if (h->mb_y >= h->mb_height) {
2479 tprintf(h->avctx, "slice end %d %d\n",
2480 get_bits_count(&h->gb), h->gb.size_in_bits);
2482 if ( get_bits_left(&h->gb) == 0
2483 || get_bits_left(&h->gb) > 0 && !(h->avctx->err_recognition & AV_EF_AGGRESSIVE)) {
2484 er_add_slice(h, h->resync_mb_x, h->resync_mb_y,
2485 h->mb_x - 1, h->mb_y,
2490 er_add_slice(h, h->resync_mb_x, h->resync_mb_y,
2494 return AVERROR_INVALIDDATA;
2499 if (get_bits_left(&h->gb) <= 0 && h->mb_skip_run <= 0) {
2500 tprintf(h->avctx, "slice end %d %d\n",
2501 get_bits_count(&h->gb), h->gb.size_in_bits);
2503 if (get_bits_left(&h->gb) == 0) {
2504 er_add_slice(h, h->resync_mb_x, h->resync_mb_y,
2505 h->mb_x - 1, h->mb_y,
2507 if (h->mb_x > lf_x_start)
2508 loop_filter(h, lf_x_start, h->mb_x);
2512 er_add_slice(h, h->resync_mb_x, h->resync_mb_y, h->mb_x,
2513 h->mb_y, ER_MB_ERROR);
2515 return AVERROR_INVALIDDATA;
2523 * Call decode_slice() for each context.
2525 * @param h h264 master context
2526 * @param context_count number of contexts to execute
2528 int ff_h264_execute_decode_slices(H264Context *h, unsigned context_count)
2530 AVCodecContext *const avctx = h->avctx;
2534 av_assert0(h->mb_y < h->mb_height);
2536 if (h->avctx->hwaccel ||
2537 h->avctx->codec->capabilities & CODEC_CAP_HWACCEL_VDPAU)
2539 if (context_count == 1) {
2540 return decode_slice(avctx, &h);
2542 av_assert0(context_count > 0);
2543 for (i = 1; i < context_count; i++) {
2544 hx = h->thread_context[i];
2545 if (CONFIG_ERROR_RESILIENCE) {
2546 hx->er.error_count = 0;
2548 hx->x264_build = h->x264_build;
2551 avctx->execute(avctx, decode_slice, h->thread_context,
2552 NULL, context_count, sizeof(void *));
2554 /* pull back stuff from slices to master context */
2555 hx = h->thread_context[context_count - 1];
2558 h->droppable = hx->droppable;
2559 h->picture_structure = hx->picture_structure;
2560 if (CONFIG_ERROR_RESILIENCE) {
2561 for (i = 1; i < context_count; i++)
2562 h->er.error_count += h->thread_context[i]->er.error_count;