diff --git a/libavcodec/h264.h b/libavcodec/h264.h index 5e9827d6c8..61d94cd6d4 100644 --- a/libavcodec/h264.h +++ b/libavcodec/h264.h @@ -407,7 +407,6 @@ typedef struct H264SliceContext { H264Ref ref_list[2][48]; /**< 0..15: frame refs, 16..47: mbaff field refs. * Reordered version of default_ref_list * according to picture reordering in slice header */ - int ref2frm[MAX_SLICES][2][64]; ///< reference to frame number lists, used in the loop filter, the first 2 are for -2,-1 const uint8_t *intra_pcm_ptr; int16_t *dc_val_base; @@ -494,6 +493,11 @@ typedef struct H264Context { int context_initialized; int flags; int workaround_bugs; + /* Set when slice threading is used and at least one slice uses deblocking + * mode 1 (i.e. across slice boundaries). Then we disable the loop filter + * during normal MB decoding and execute it serially at the end. + */ + int postpone_filter; int8_t(*intra4x4_pred_mode); H264PredContext hpc; @@ -685,6 +689,7 @@ typedef struct H264Context { AVBufferPool *mb_type_pool; AVBufferPool *motion_val_pool; AVBufferPool *ref_index_pool; + int ref2frm[MAX_SLICES][2][64]; ///< reference to frame number lists, used in the loop filter, the first 2 are for -2,-1 } H264Context; extern const uint16_t ff_h264_mb_sizes[4]; diff --git a/libavcodec/h264_slice.c b/libavcodec/h264_slice.c index 0e13fd9956..8d8e93941f 100644 --- a/libavcodec/h264_slice.c +++ b/libavcodec/h264_slice.c @@ -551,6 +551,8 @@ static int h264_frame_start(H264Context *h) h->next_output_pic = NULL; + h->postpone_filter = 0; + assert(h->cur_pic_ptr->long_ref == 0); return 0; @@ -1040,7 +1042,7 @@ static enum AVPixelFormat non_j_pixfmt(enum AVPixelFormat a) * * @param h h264context * - * @return 0 if okay, <0 if an error occurred, 1 if decoding must not be multithreaded + * @return 0 if okay, <0 if an error occurred */ int ff_h264_decode_slice_header(H264Context *h, H264SliceContext *sl) { @@ -1742,20 +1744,7 @@ int ff_h264_decode_slice_header(H264Context *h, H264SliceContext *sl) * Do not bother to deblock across slices. */ sl->deblocking_filter = 2; } else { - h->max_contexts = 1; - if (!h->single_decode_warning) { - av_log(h->avctx, AV_LOG_INFO, - "Cannot parallelize slice decoding with deblocking filter type 1, decoding such frames in sequential order\n" - "To parallelize slice decoding you need video encoded with disable_deblocking_filter_idc set to 2 (deblock only edges that do not cross slices).\n" - "Setting the flags2 libavcodec option to +fast (-flags2 +fast) will disable deblocking across slices and enable parallel slice decoding " - "but will generate non-standard-compliant output.\n"); - h->single_decode_warning = 1; - } - if (sl != h->slice_ctx) { - av_log(h->avctx, AV_LOG_ERROR, - "Deblocking switched inside frame.\n"); - return SLICE_SINGLETHREAD; - } + h->postpone_filter = 1; } } sl->qp_thresh = 15 - @@ -1778,7 +1767,7 @@ int ff_h264_decode_slice_header(H264Context *h, H264SliceContext *sl) for (j = 0; j < 2; j++) { int id_list[16]; - int *ref2frm = sl->ref2frm[sl->slice_num & (MAX_SLICES - 1)][j]; + int *ref2frm = h->ref2frm[sl->slice_num & (MAX_SLICES - 1)][j]; for (i = 0; i < 16; i++) { id_list[i] = 60; if (j < sl->list_count && i < sl->ref_count[j] && @@ -1869,7 +1858,7 @@ static av_always_inline void fill_filter_caches_inter(const H264Context *h, if (USES_LIST(top_type, list)) { const int b_xy = h->mb2b_xy[top_xy] + 3 * b_stride; const int b8_xy = 4 * top_xy + 2; - int *ref2frm = sl->ref2frm[h->slice_table[top_xy] & (MAX_SLICES - 1)][list] + (MB_MBAFF(sl) ? 20 : 2); + int *ref2frm = h->ref2frm[h->slice_table[top_xy] & (MAX_SLICES - 1)][list] + (MB_MBAFF(sl) ? 20 : 2); AV_COPY128(mv_dst - 1 * 8, h->cur_pic.motion_val[list][b_xy + 0]); ref_cache[0 - 1 * 8] = ref_cache[1 - 1 * 8] = ref2frm[h->cur_pic.ref_index[list][b8_xy + 0]]; @@ -1884,7 +1873,7 @@ static av_always_inline void fill_filter_caches_inter(const H264Context *h, if (USES_LIST(left_type[LTOP], list)) { const int b_xy = h->mb2b_xy[left_xy[LTOP]] + 3; const int b8_xy = 4 * left_xy[LTOP] + 1; - int *ref2frm = sl->ref2frm[h->slice_table[left_xy[LTOP]] & (MAX_SLICES - 1)][list] + (MB_MBAFF(sl) ? 20 : 2); + int *ref2frm = h->ref2frm[h->slice_table[left_xy[LTOP]] & (MAX_SLICES - 1)][list] + (MB_MBAFF(sl) ? 20 : 2); AV_COPY32(mv_dst - 1 + 0, h->cur_pic.motion_val[list][b_xy + b_stride * 0]); AV_COPY32(mv_dst - 1 + 8, h->cur_pic.motion_val[list][b_xy + b_stride * 1]); AV_COPY32(mv_dst - 1 + 16, h->cur_pic.motion_val[list][b_xy + b_stride * 2]); @@ -1917,7 +1906,7 @@ static av_always_inline void fill_filter_caches_inter(const H264Context *h, { int8_t *ref = &h->cur_pic.ref_index[list][4 * mb_xy]; - int *ref2frm = sl->ref2frm[sl->slice_num & (MAX_SLICES - 1)][list] + (MB_MBAFF(sl) ? 20 : 2); + int *ref2frm = h->ref2frm[sl->slice_num & (MAX_SLICES - 1)][list] + (MB_MBAFF(sl) ? 20 : 2); uint32_t ref01 = (pack16to32(ref2frm[ref[0]], ref2frm[ref[1]]) & 0x00FF00FF) * 0x0101; uint32_t ref23 = (pack16to32(ref2frm[ref[2]], ref2frm[ref[3]]) & 0x00FF00FF) * 0x0101; AV_WN32A(&ref_cache[0 * 8], ref01); @@ -2092,6 +2081,9 @@ static void loop_filter(const H264Context *h, H264SliceContext *sl, int start_x, const int pixel_shift = h->pixel_shift; const int block_h = 16 >> h->chroma_y_shift; + if (h->postpone_filter) + return; + if (sl->deblocking_filter) { for (mb_x = start_x; mb_x < end_x; mb_x++) for (mb_y = end_mb_y - FRAME_MBAFF(h); mb_y <= end_mb_y; mb_y++) { @@ -2215,6 +2207,7 @@ static int decode_slice(struct AVCodecContext *avctx, void *arg) H264SliceContext *sl = arg; const H264Context *h = sl->h264; int lf_x_start = sl->mb_x; + int orig_deblock = sl->deblocking_filter; int ret; sl->linesize = h->cur_pic_ptr->f->linesize[0]; @@ -2228,6 +2221,9 @@ static int decode_slice(struct AVCodecContext *avctx, void *arg) av_assert0(h->block_offset[15] == (4 * ((scan8[15] - scan8[0]) & 7) << h->pixel_shift) + 4 * sl->linesize * ((scan8[15] - scan8[0]) >> 3)); + if (h->postpone_filter) + sl->deblocking_filter = 0; + sl->is_complex = FRAME_MBAFF(h) || h->picture_structure != PICT_FRAME || avctx->codec_id != AV_CODEC_ID_H264 || (CONFIG_GRAY && (h->flags & AV_CODEC_FLAG_GRAY)); @@ -2290,7 +2286,7 @@ static int decode_slice(struct AVCodecContext *avctx, void *arg) sl->mb_y, ER_MB_END); if (sl->mb_x >= lf_x_start) loop_filter(h, sl, lf_x_start, sl->mb_x + 1); - return 0; + goto finish; } if (sl->cabac.bytestream > sl->cabac.bytestream_end + 2 ) av_log(h->avctx, AV_LOG_DEBUG, "bytestream overread %"PTRDIFF_SPECIFIER"\n", sl->cabac.bytestream_end - sl->cabac.bytestream); @@ -2323,7 +2319,7 @@ static int decode_slice(struct AVCodecContext *avctx, void *arg) sl->mb_y, ER_MB_END); if (sl->mb_x > lf_x_start) loop_filter(h, sl, lf_x_start, sl->mb_x); - return 0; + goto finish; } } } else { @@ -2380,7 +2376,7 @@ static int decode_slice(struct AVCodecContext *avctx, void *arg) er_add_slice(sl, sl->resync_mb_x, sl->resync_mb_y, sl->mb_x - 1, sl->mb_y, ER_MB_END); - return 0; + goto finish; } else { er_add_slice(sl, sl->resync_mb_x, sl->resync_mb_y, sl->mb_x, sl->mb_y, ER_MB_END); @@ -2400,7 +2396,7 @@ static int decode_slice(struct AVCodecContext *avctx, void *arg) if (sl->mb_x > lf_x_start) loop_filter(h, sl, lf_x_start, sl->mb_x); - return 0; + goto finish; } else { er_add_slice(sl, sl->resync_mb_x, sl->resync_mb_y, sl->mb_x, sl->mb_y, ER_MB_ERROR); @@ -2410,6 +2406,10 @@ static int decode_slice(struct AVCodecContext *avctx, void *arg) } } } + +finish: + sl->deblocking_filter = orig_deblock; + return 0; } /** @@ -2438,6 +2438,7 @@ int ff_h264_execute_decode_slices(H264Context *h, unsigned context_count) int ret; h->slice_ctx[0].next_slice_idx = h->mb_width * h->mb_height; + h->postpone_filter = 0; ret = decode_slice(avctx, &h->slice_ctx[0]); h->mb_y = h->slice_ctx[0].mb_y; @@ -2476,6 +2477,24 @@ int ff_h264_execute_decode_slices(H264Context *h, unsigned context_count) for (i = 1; i < context_count; i++) h->slice_ctx[0].er.error_count += h->slice_ctx[i].er.error_count; } + + if (h->postpone_filter) { + h->postpone_filter = 0; + + for (i = 0; i < context_count; i++) { + int y_end, x_end; + + sl = &h->slice_ctx[i]; + y_end = FFMIN(sl->mb_y + 1, h->mb_height); + x_end = (sl->mb_y >= h->mb_height) ? h->mb_width : sl->mb_x; + + for (j = sl->resync_mb_y; j < y_end; j += 1 + FIELD_OR_MBAFF_PICTURE(h)) { + sl->mb_y = j; + loop_filter(h, sl, j > sl->resync_mb_y ? 0 : sl->resync_mb_x, + j == y_end - 1 ? x_end : h->mb_width); + } + } + } } return 0;