-static enum AVPixelFormat non_j_pixfmt(enum AVPixelFormat a)
-{
- switch (a) {
- case AV_PIX_FMT_YUVJ420P: return AV_PIX_FMT_YUV420P;
- case AV_PIX_FMT_YUVJ422P: return AV_PIX_FMT_YUV422P;
- case AV_PIX_FMT_YUVJ444P: return AV_PIX_FMT_YUV444P;
- default:
- return a;
- }
-}
-
-/**
- * Decode a slice header.
- * This will (re)intialize the decoder and call h264_frame_start() as needed.
- *
- * @param h h264context
- * @param h0 h264 master context (differs from 'h' when doing sliced based
- * parallel decoding)
- *
- * @return 0 if okay, <0 if an error occurred, 1 if decoding must not be multithreaded
- */
-static int decode_slice_header(H264Context *h, H264Context *h0)
-{
- unsigned int first_mb_in_slice;
- unsigned int pps_id;
- int ret;
- unsigned int slice_type, tmp, i, j;
- int last_pic_structure, last_pic_droppable;
- int must_reinit;
- int needs_reinit = 0;
- int field_pic_flag, bottom_field_flag;
-
- h->qpel_put = h->h264qpel.put_h264_qpel_pixels_tab;
- h->qpel_avg = h->h264qpel.avg_h264_qpel_pixels_tab;
-
- first_mb_in_slice = get_ue_golomb_long(&h->gb);
-
- if (first_mb_in_slice == 0) { // FIXME better field boundary detection
- if (h0->current_slice && h->cur_pic_ptr && FIELD_PICTURE(h)) {
- field_end(h, 1);
- }
-
- h0->current_slice = 0;
- if (!h0->first_field) {
- if (h->cur_pic_ptr && !h->droppable) {
- ff_thread_report_progress(&h->cur_pic_ptr->tf, INT_MAX,
- h->picture_structure == PICT_BOTTOM_FIELD);
- }
- h->cur_pic_ptr = NULL;
- }
- }
-
- slice_type = get_ue_golomb_31(&h->gb);
- if (slice_type > 9) {
- av_log(h->avctx, AV_LOG_ERROR,
- "slice type %d too large at %d %d\n",
- slice_type, h->mb_x, h->mb_y);
- return AVERROR_INVALIDDATA;
- }
- if (slice_type > 4) {
- slice_type -= 5;
- h->slice_type_fixed = 1;
- } else
- h->slice_type_fixed = 0;
-
- slice_type = golomb_to_pict_type[slice_type];
- h->slice_type = slice_type;
- h->slice_type_nos = slice_type & 3;
-
- if (h->nal_unit_type == NAL_IDR_SLICE &&
- h->slice_type_nos != AV_PICTURE_TYPE_I) {
- av_log(h->avctx, AV_LOG_ERROR, "A non-intra slice in an IDR NAL unit.\n");
- return AVERROR_INVALIDDATA;
- }
-
- // to make a few old functions happy, it's wrong though
- h->pict_type = h->slice_type;
-
- pps_id = get_ue_golomb(&h->gb);
- if (pps_id >= MAX_PPS_COUNT) {
- av_log(h->avctx, AV_LOG_ERROR, "pps_id %u out of range\n", pps_id);
- return AVERROR_INVALIDDATA;
- }
- if (!h0->pps_buffers[pps_id]) {
- av_log(h->avctx, AV_LOG_ERROR,
- "non-existing PPS %u referenced\n",
- pps_id);
- return AVERROR_INVALIDDATA;
- }
- if (h0->au_pps_id >= 0 && pps_id != h0->au_pps_id) {
- av_log(h->avctx, AV_LOG_ERROR,
- "PPS change from %d to %d forbidden\n",
- h0->au_pps_id, pps_id);
- return AVERROR_INVALIDDATA;
- }
- h->pps = *h0->pps_buffers[pps_id];
-
- if (!h0->sps_buffers[h->pps.sps_id]) {
- av_log(h->avctx, AV_LOG_ERROR,
- "non-existing SPS %u referenced\n",
- h->pps.sps_id);
- return AVERROR_INVALIDDATA;
- }
-
- if (h->pps.sps_id != h->sps.sps_id ||
- h->pps.sps_id != h->current_sps_id ||
- h0->sps_buffers[h->pps.sps_id]->new) {
-
- h->sps = *h0->sps_buffers[h->pps.sps_id];
-
- if (h->mb_width != h->sps.mb_width ||
- h->mb_height != h->sps.mb_height * (2 - h->sps.frame_mbs_only_flag) ||
- h->avctx->bits_per_raw_sample != h->sps.bit_depth_luma ||
- h->cur_chroma_format_idc != h->sps.chroma_format_idc
- )
- needs_reinit = 1;
-
- if (h->bit_depth_luma != h->sps.bit_depth_luma ||
- h->chroma_format_idc != h->sps.chroma_format_idc) {
- h->bit_depth_luma = h->sps.bit_depth_luma;
- h->chroma_format_idc = h->sps.chroma_format_idc;
- needs_reinit = 1;
- }
- if ((ret = h264_set_parameter_from_sps(h)) < 0)
- return ret;
- }
-
- h->avctx->profile = ff_h264_get_profile(&h->sps);
- h->avctx->level = h->sps.level_idc;
- h->avctx->refs = h->sps.ref_frame_count;
-
- must_reinit = (h->context_initialized &&
- ( 16*h->sps.mb_width != h->avctx->coded_width
- || 16*h->sps.mb_height * (2 - h->sps.frame_mbs_only_flag) != h->avctx->coded_height
- || h->avctx->bits_per_raw_sample != h->sps.bit_depth_luma
- || h->cur_chroma_format_idc != h->sps.chroma_format_idc
- || av_cmp_q(h->sps.sar, h->avctx->sample_aspect_ratio)
- || h->mb_width != h->sps.mb_width
- || h->mb_height != h->sps.mb_height * (2 - h->sps.frame_mbs_only_flag)
- ));
- if (non_j_pixfmt(h0->avctx->pix_fmt) != non_j_pixfmt(get_pixel_format(h0, 0)))
- must_reinit = 1;
-
- h->mb_width = h->sps.mb_width;
- h->mb_height = h->sps.mb_height * (2 - h->sps.frame_mbs_only_flag);
- h->mb_num = h->mb_width * h->mb_height;
- h->mb_stride = h->mb_width + 1;
-
- h->b_stride = h->mb_width * 4;
-
- h->chroma_y_shift = h->sps.chroma_format_idc <= 1; // 400 uses yuv420p
-
- h->width = 16 * h->mb_width;
- h->height = 16 * h->mb_height;
-
- ret = init_dimensions(h);
- if (ret < 0)
- return ret;
-
- if (h->sps.video_signal_type_present_flag) {
- h->avctx->color_range = h->sps.full_range>0 ? AVCOL_RANGE_JPEG
- : AVCOL_RANGE_MPEG;
- if (h->sps.colour_description_present_flag) {
- if (h->avctx->colorspace != h->sps.colorspace)
- needs_reinit = 1;
- h->avctx->color_primaries = h->sps.color_primaries;
- h->avctx->color_trc = h->sps.color_trc;
- h->avctx->colorspace = h->sps.colorspace;
- }
- }
-
- if (h->context_initialized &&
- (h->width != h->avctx->coded_width ||
- h->height != h->avctx->coded_height ||
- must_reinit ||
- needs_reinit)) {
- if (h != h0) {
- av_log(h->avctx, AV_LOG_ERROR,
- "changing width %d -> %d / height %d -> %d on "
- "slice %d\n",
- h->width, h->avctx->coded_width,
- h->height, h->avctx->coded_height,
- h0->current_slice + 1);
- return AVERROR_INVALIDDATA;
- }
-
- flush_change(h);
-
- if ((ret = get_pixel_format(h, 1)) < 0)
- return ret;
- h->avctx->pix_fmt = ret;
-
- av_log(h->avctx, AV_LOG_INFO, "Reinit context to %dx%d, "
- "pix_fmt: %s\n", h->width, h->height, av_get_pix_fmt_name(h->avctx->pix_fmt));
-
- if ((ret = h264_slice_header_init(h, 1)) < 0) {
- av_log(h->avctx, AV_LOG_ERROR,
- "h264_slice_header_init() failed\n");
- return ret;
- }
- }
- if (!h->context_initialized) {
- if (h != h0) {
- av_log(h->avctx, AV_LOG_ERROR,
- "Cannot (re-)initialize context during parallel decoding.\n");
- return AVERROR_PATCHWELCOME;
- }
-
- if ((ret = get_pixel_format(h, 1)) < 0)
- return ret;
- h->avctx->pix_fmt = ret;
-
- if ((ret = h264_slice_header_init(h, 0)) < 0) {
- av_log(h->avctx, AV_LOG_ERROR,
- "h264_slice_header_init() failed\n");
- return ret;
- }
- }
-
- if (h == h0 && h->dequant_coeff_pps != pps_id) {
- h->dequant_coeff_pps = pps_id;
- init_dequant_tables(h);
- }
-
- h->frame_num = get_bits(&h->gb, h->sps.log2_max_frame_num);
-
- h->mb_mbaff = 0;
- h->mb_aff_frame = 0;
- last_pic_structure = h0->picture_structure;
- last_pic_droppable = h0->droppable;
- h->droppable = h->nal_ref_idc == 0;
- if (h->sps.frame_mbs_only_flag) {
- h->picture_structure = PICT_FRAME;
- } else {
- if (!h->sps.direct_8x8_inference_flag && slice_type == AV_PICTURE_TYPE_B) {
- av_log(h->avctx, AV_LOG_ERROR, "This stream was generated by a broken encoder, invalid 8x8 inference\n");
- return -1;
- }
- field_pic_flag = get_bits1(&h->gb);
- if (field_pic_flag) {
- bottom_field_flag = get_bits1(&h->gb);
- h->picture_structure = PICT_TOP_FIELD + bottom_field_flag;
- } else {
- h->picture_structure = PICT_FRAME;
- h->mb_aff_frame = h->sps.mb_aff;
- }
- }
- h->mb_field_decoding_flag = h->picture_structure != PICT_FRAME;
-
- if (h0->current_slice != 0) {
- if (last_pic_structure != h->picture_structure ||
- last_pic_droppable != h->droppable) {
- av_log(h->avctx, AV_LOG_ERROR,
- "Changing field mode (%d -> %d) between slices is not allowed\n",
- last_pic_structure, h->picture_structure);
- h->picture_structure = last_pic_structure;
- h->droppable = last_pic_droppable;
- return AVERROR_INVALIDDATA;
- } else if (!h0->cur_pic_ptr) {
- av_log(h->avctx, AV_LOG_ERROR,
- "unset cur_pic_ptr on slice %d\n",
- h0->current_slice + 1);
- return AVERROR_INVALIDDATA;
- }
- } else {
- /* Shorten frame num gaps so we don't have to allocate reference
- * frames just to throw them away */
- if (h->frame_num != h->prev_frame_num) {
- int unwrap_prev_frame_num = h->prev_frame_num;
- int max_frame_num = 1 << h->sps.log2_max_frame_num;
-
- if (unwrap_prev_frame_num > h->frame_num)
- unwrap_prev_frame_num -= max_frame_num;
-
- if ((h->frame_num - unwrap_prev_frame_num) > h->sps.ref_frame_count) {
- unwrap_prev_frame_num = (h->frame_num - h->sps.ref_frame_count) - 1;
- if (unwrap_prev_frame_num < 0)
- unwrap_prev_frame_num += max_frame_num;
-
- h->prev_frame_num = unwrap_prev_frame_num;
- }
- }
-
- /* See if we have a decoded first field looking for a pair...
- * Here, we're using that to see if we should mark previously
- * decode frames as "finished".
- * We have to do that before the "dummy" in-between frame allocation,
- * since that can modify h->cur_pic_ptr. */
- if (h0->first_field) {
- assert(h0->cur_pic_ptr);
- assert(h0->cur_pic_ptr->f.buf[0]);
- assert(h0->cur_pic_ptr->reference != DELAYED_PIC_REF);
-
- /* Mark old field/frame as completed */
- if (h0->cur_pic_ptr->tf.owner == h0->avctx) {
- ff_thread_report_progress(&h0->cur_pic_ptr->tf, INT_MAX,
- last_pic_structure == PICT_BOTTOM_FIELD);
- }
-
- /* figure out if we have a complementary field pair */
- if (!FIELD_PICTURE(h) || h->picture_structure == last_pic_structure) {
- /* Previous field is unmatched. Don't display it, but let it
- * remain for reference if marked as such. */
- if (last_pic_structure != PICT_FRAME) {
- ff_thread_report_progress(&h0->cur_pic_ptr->tf, INT_MAX,
- last_pic_structure == PICT_TOP_FIELD);
- }
- } else {
- if (h0->cur_pic_ptr->frame_num != h->frame_num) {
- /* This and previous field were reference, but had
- * different frame_nums. Consider this field first in
- * pair. Throw away previous field except for reference
- * purposes. */
- if (last_pic_structure != PICT_FRAME) {
- ff_thread_report_progress(&h0->cur_pic_ptr->tf, INT_MAX,
- last_pic_structure == PICT_TOP_FIELD);
- }
- } else {
- /* Second field in complementary pair */
- if (!((last_pic_structure == PICT_TOP_FIELD &&
- h->picture_structure == PICT_BOTTOM_FIELD) ||
- (last_pic_structure == PICT_BOTTOM_FIELD &&
- h->picture_structure == PICT_TOP_FIELD))) {
- av_log(h->avctx, AV_LOG_ERROR,
- "Invalid field mode combination %d/%d\n",
- last_pic_structure, h->picture_structure);
- h->picture_structure = last_pic_structure;
- h->droppable = last_pic_droppable;
- return AVERROR_INVALIDDATA;
- } else if (last_pic_droppable != h->droppable) {
- avpriv_request_sample(h->avctx,
- "Found reference and non-reference fields in the same frame, which");
- h->picture_structure = last_pic_structure;
- h->droppable = last_pic_droppable;
- return AVERROR_PATCHWELCOME;
- }
- }
- }
- }
-
- while (h->frame_num != h->prev_frame_num && !h0->first_field &&
- h->frame_num != (h->prev_frame_num + 1) % (1 << h->sps.log2_max_frame_num)) {
- H264Picture *prev = h->short_ref_count ? h->short_ref[0] : NULL;
- av_log(h->avctx, AV_LOG_DEBUG, "Frame num gap %d %d\n",
- h->frame_num, h->prev_frame_num);
- if (!h->sps.gaps_in_frame_num_allowed_flag)
- for(i=0; i<FF_ARRAY_ELEMS(h->last_pocs); i++)
- h->last_pocs[i] = INT_MIN;
- ret = h264_frame_start(h);
- if (ret < 0) {
- h0->first_field = 0;
- return ret;
- }
-
- h->prev_frame_num++;
- h->prev_frame_num %= 1 << h->sps.log2_max_frame_num;
- h->cur_pic_ptr->frame_num = h->prev_frame_num;
- h->cur_pic_ptr->invalid_gap = !h->sps.gaps_in_frame_num_allowed_flag;
- ff_thread_report_progress(&h->cur_pic_ptr->tf, INT_MAX, 0);
- ff_thread_report_progress(&h->cur_pic_ptr->tf, INT_MAX, 1);
- ret = ff_generate_sliding_window_mmcos(h, 1);
- if (ret < 0 && (h->avctx->err_recognition & AV_EF_EXPLODE))
- return ret;
- ret = ff_h264_execute_ref_pic_marking(h, h->mmco, h->mmco_index);
- if (ret < 0 && (h->avctx->err_recognition & AV_EF_EXPLODE))
- return ret;
- /* Error concealment: If a ref is missing, copy the previous ref
- * in its place.
- * FIXME: Avoiding a memcpy would be nice, but ref handling makes
- * many assumptions about there being no actual duplicates.
- * FIXME: This does not copy padding for out-of-frame motion
- * vectors. Given we are concealing a lost frame, this probably
- * is not noticeable by comparison, but it should be fixed. */
- if (h->short_ref_count) {
- if (prev) {
- av_image_copy(h->short_ref[0]->f.data,
- h->short_ref[0]->f.linesize,
- (const uint8_t **)prev->f.data,
- prev->f.linesize,
- h->avctx->pix_fmt,
- h->mb_width * 16,
- h->mb_height * 16);
- h->short_ref[0]->poc = prev->poc + 2;
- }
- h->short_ref[0]->frame_num = h->prev_frame_num;
- }
- }
-
- /* See if we have a decoded first field looking for a pair...
- * We're using that to see whether to continue decoding in that
- * frame, or to allocate a new one. */
- if (h0->first_field) {
- assert(h0->cur_pic_ptr);
- assert(h0->cur_pic_ptr->f.buf[0]);
- assert(h0->cur_pic_ptr->reference != DELAYED_PIC_REF);
-
- /* figure out if we have a complementary field pair */
- if (!FIELD_PICTURE(h) || h->picture_structure == last_pic_structure) {
- /* Previous field is unmatched. Don't display it, but let it
- * remain for reference if marked as such. */
- h0->cur_pic_ptr = NULL;
- h0->first_field = FIELD_PICTURE(h);
- } else {
- if (h0->cur_pic_ptr->frame_num != h->frame_num) {
- ff_thread_report_progress(&h0->cur_pic_ptr->tf, INT_MAX,
- h0->picture_structure==PICT_BOTTOM_FIELD);
- /* This and the previous field had different frame_nums.
- * Consider this field first in pair. Throw away previous
- * one except for reference purposes. */
- h0->first_field = 1;
- h0->cur_pic_ptr = NULL;
- } else {
- /* Second field in complementary pair */
- h0->first_field = 0;
- }
- }
- } else {
- /* Frame or first field in a potentially complementary pair */
- h0->first_field = FIELD_PICTURE(h);
- }
-
- if (!FIELD_PICTURE(h) || h0->first_field) {
- if (h264_frame_start(h) < 0) {
- h0->first_field = 0;
- return AVERROR_INVALIDDATA;
- }
- } else {
- release_unused_pictures(h, 0);
- }
- /* Some macroblocks can be accessed before they're available in case
- * of lost slices, MBAFF or threading. */
- if (FIELD_PICTURE(h)) {
- for(i = (h->picture_structure == PICT_BOTTOM_FIELD); i<h->mb_height; i++)
- memset(h->slice_table + i*h->mb_stride, -1, (h->mb_stride - (i+1==h->mb_height)) * sizeof(*h->slice_table));
- } else {
- memset(h->slice_table, -1,
- (h->mb_height * h->mb_stride - 1) * sizeof(*h->slice_table));
- }
- h0->last_slice_type = -1;
- }
- if (h != h0 && (ret = clone_slice(h, h0)) < 0)
- return ret;
-
- /* can't be in alloc_tables because linesize isn't known there.
- * FIXME: redo bipred weight to not require extra buffer? */
- for (i = 0; i < h->slice_context_count; i++)
- if (h->thread_context[i]) {
- ret = alloc_scratch_buffers(h->thread_context[i], h->linesize);
- if (ret < 0)
- return ret;
- }
-
- h->cur_pic_ptr->frame_num = h->frame_num; // FIXME frame_num cleanup
-
- av_assert1(h->mb_num == h->mb_width * h->mb_height);
- if (first_mb_in_slice << FIELD_OR_MBAFF_PICTURE(h) >= h->mb_num ||
- first_mb_in_slice >= h->mb_num) {
- av_log(h->avctx, AV_LOG_ERROR, "first_mb_in_slice overflow\n");
- return AVERROR_INVALIDDATA;
- }
- h->resync_mb_x = h->mb_x = first_mb_in_slice % h->mb_width;
- h->resync_mb_y = h->mb_y = (first_mb_in_slice / h->mb_width) <<
- FIELD_OR_MBAFF_PICTURE(h);
- if (h->picture_structure == PICT_BOTTOM_FIELD)
- h->resync_mb_y = h->mb_y = h->mb_y + 1;
- av_assert1(h->mb_y < h->mb_height);
-
- if (h->picture_structure == PICT_FRAME) {
- h->curr_pic_num = h->frame_num;
- h->max_pic_num = 1 << h->sps.log2_max_frame_num;
- } else {
- h->curr_pic_num = 2 * h->frame_num + 1;
- h->max_pic_num = 1 << (h->sps.log2_max_frame_num + 1);
- }
-
- if (h->nal_unit_type == NAL_IDR_SLICE)
- get_ue_golomb(&h->gb); /* idr_pic_id */
-
- if (h->sps.poc_type == 0) {
- h->poc_lsb = get_bits(&h->gb, h->sps.log2_max_poc_lsb);
-
- if (h->pps.pic_order_present == 1 && h->picture_structure == PICT_FRAME)
- h->delta_poc_bottom = get_se_golomb(&h->gb);
- }
-
- if (h->sps.poc_type == 1 && !h->sps.delta_pic_order_always_zero_flag) {
- h->delta_poc[0] = get_se_golomb(&h->gb);
-
- if (h->pps.pic_order_present == 1 && h->picture_structure == PICT_FRAME)
- h->delta_poc[1] = get_se_golomb(&h->gb);
- }
-
- ff_init_poc(h, h->cur_pic_ptr->field_poc, &h->cur_pic_ptr->poc);
-
- if (h->pps.redundant_pic_cnt_present)
- h->redundant_pic_count = get_ue_golomb(&h->gb);
-
- ret = ff_set_ref_count(h);
- if (ret < 0)
- return ret;
-
- if (slice_type != AV_PICTURE_TYPE_I &&
- (h0->current_slice == 0 ||
- slice_type != h0->last_slice_type ||
- memcmp(h0->last_ref_count, h0->ref_count, sizeof(h0->ref_count)))) {
-
- ff_h264_fill_default_ref_list(h);
- }
-
- if (h->slice_type_nos != AV_PICTURE_TYPE_I) {
- ret = ff_h264_decode_ref_pic_list_reordering(h);
- if (ret < 0) {
- h->ref_count[1] = h->ref_count[0] = 0;
- return ret;
- }
- }
-
- if ((h->pps.weighted_pred && h->slice_type_nos == AV_PICTURE_TYPE_P) ||
- (h->pps.weighted_bipred_idc == 1 &&
- h->slice_type_nos == AV_PICTURE_TYPE_B))
- ff_pred_weight_table(h);
- else if (h->pps.weighted_bipred_idc == 2 &&
- h->slice_type_nos == AV_PICTURE_TYPE_B) {
- implicit_weight_table(h, -1);
- } else {
- h->use_weight = 0;
- for (i = 0; i < 2; i++) {
- h->luma_weight_flag[i] = 0;
- h->chroma_weight_flag[i] = 0;
- }
- }
-
- // If frame-mt is enabled, only update mmco tables for the first slice
- // in a field. Subsequent slices can temporarily clobber h->mmco_index
- // or h->mmco, which will cause ref list mix-ups and decoding errors
- // further down the line. This may break decoding if the first slice is
- // corrupt, thus we only do this if frame-mt is enabled.
- if (h->nal_ref_idc) {
- ret = ff_h264_decode_ref_pic_marking(h0, &h->gb,
- !(h->avctx->active_thread_type & FF_THREAD_FRAME) ||
- h0->current_slice == 0);
- if (ret < 0 && (h->avctx->err_recognition & AV_EF_EXPLODE))
- return AVERROR_INVALIDDATA;
- }
-
- if (FRAME_MBAFF(h)) {
- ff_h264_fill_mbaff_ref_list(h);
-
- if (h->pps.weighted_bipred_idc == 2 && h->slice_type_nos == AV_PICTURE_TYPE_B) {
- implicit_weight_table(h, 0);
- implicit_weight_table(h, 1);
- }
- }
-
- if (h->slice_type_nos == AV_PICTURE_TYPE_B && !h->direct_spatial_mv_pred)
- ff_h264_direct_dist_scale_factor(h);
- ff_h264_direct_ref_list_init(h);
-
- if (h->slice_type_nos != AV_PICTURE_TYPE_I && h->pps.cabac) {
- tmp = get_ue_golomb_31(&h->gb);
- if (tmp > 2) {
- av_log(h->avctx, AV_LOG_ERROR, "cabac_init_idc %u overflow\n", tmp);
- return AVERROR_INVALIDDATA;
- }
- h->cabac_init_idc = tmp;
- }
-
- h->last_qscale_diff = 0;
- tmp = h->pps.init_qp + get_se_golomb(&h->gb);
- if (tmp > 51 + 6 * (h->sps.bit_depth_luma - 8)) {
- av_log(h->avctx, AV_LOG_ERROR, "QP %u out of range\n", tmp);
- return AVERROR_INVALIDDATA;
- }
- h->qscale = tmp;
- h->chroma_qp[0] = get_chroma_qp(h, 0, h->qscale);
- h->chroma_qp[1] = get_chroma_qp(h, 1, h->qscale);
- // FIXME qscale / qp ... stuff
- if (h->slice_type == AV_PICTURE_TYPE_SP)
- get_bits1(&h->gb); /* sp_for_switch_flag */
- if (h->slice_type == AV_PICTURE_TYPE_SP ||
- h->slice_type == AV_PICTURE_TYPE_SI)
- get_se_golomb(&h->gb); /* slice_qs_delta */
-
- h->deblocking_filter = 1;
- h->slice_alpha_c0_offset = 0;
- h->slice_beta_offset = 0;
- if (h->pps.deblocking_filter_parameters_present) {
- tmp = get_ue_golomb_31(&h->gb);
- if (tmp > 2) {
- av_log(h->avctx, AV_LOG_ERROR,
- "deblocking_filter_idc %u out of range\n", tmp);
- return AVERROR_INVALIDDATA;
- }
- h->deblocking_filter = tmp;
- if (h->deblocking_filter < 2)
- h->deblocking_filter ^= 1; // 1<->0
-
- if (h->deblocking_filter) {
- h->slice_alpha_c0_offset = get_se_golomb(&h->gb) * 2;
- h->slice_beta_offset = get_se_golomb(&h->gb) * 2;
- if (h->slice_alpha_c0_offset > 12 ||
- h->slice_alpha_c0_offset < -12 ||
- h->slice_beta_offset > 12 ||
- h->slice_beta_offset < -12) {
- av_log(h->avctx, AV_LOG_ERROR,
- "deblocking filter parameters %d %d out of range\n",
- h->slice_alpha_c0_offset, h->slice_beta_offset);
- return AVERROR_INVALIDDATA;
- }
- }
- }
-
- if (h->avctx->skip_loop_filter >= AVDISCARD_ALL ||
- (h->avctx->skip_loop_filter >= AVDISCARD_NONKEY &&
- h->slice_type_nos != AV_PICTURE_TYPE_I) ||
- (h->avctx->skip_loop_filter >= AVDISCARD_BIDIR &&
- h->slice_type_nos == AV_PICTURE_TYPE_B) ||
- (h->avctx->skip_loop_filter >= AVDISCARD_NONREF &&
- h->nal_ref_idc == 0))
- h->deblocking_filter = 0;
-
- if (h->deblocking_filter == 1 && h0->max_contexts > 1) {
- if (h->avctx->flags2 & CODEC_FLAG2_FAST) {
- /* Cheat slightly for speed:
- * Do not bother to deblock across slices. */
- h->deblocking_filter = 2;
- } else {
- h0->max_contexts = 1;
- if (!h0->single_decode_warning) {
- av_log(h->avctx, AV_LOG_INFO,
- "Cannot parallelize deblocking type 1, decoding such frames in sequential order\n");
- h0->single_decode_warning = 1;
- }
- if (h != h0) {
- av_log(h->avctx, AV_LOG_ERROR,
- "Deblocking switched inside frame.\n");
- return 1;
- }
- }
- }
- h->qp_thresh = 15 -
- FFMIN(h->slice_alpha_c0_offset, h->slice_beta_offset) -
- FFMAX3(0,
- h->pps.chroma_qp_index_offset[0],
- h->pps.chroma_qp_index_offset[1]) +
- 6 * (h->sps.bit_depth_luma - 8);
-
- h0->last_slice_type = slice_type;
- memcpy(h0->last_ref_count, h0->ref_count, sizeof(h0->last_ref_count));
- h->slice_num = ++h0->current_slice;
-
- if (h->slice_num)
- h0->slice_row[(h->slice_num-1)&(MAX_SLICES-1)]= h->resync_mb_y;
- if ( h0->slice_row[h->slice_num&(MAX_SLICES-1)] + 3 >= h->resync_mb_y
- && h0->slice_row[h->slice_num&(MAX_SLICES-1)] <= h->resync_mb_y
- && h->slice_num >= MAX_SLICES) {
- //in case of ASO this check needs to be updated depending on how we decide to assign slice numbers in this case
- 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);
- }
-
- for (j = 0; j < 2; j++) {
- int id_list[16];
- int *ref2frm = h->ref2frm[h->slice_num & (MAX_SLICES - 1)][j];
- for (i = 0; i < 16; i++) {
- id_list[i] = 60;
- if (j < h->list_count && i < h->ref_count[j] &&
- h->ref_list[j][i].f.buf[0]) {
- int k;
- AVBuffer *buf = h->ref_list[j][i].f.buf[0]->buffer;
- for (k = 0; k < h->short_ref_count; k++)
- if (h->short_ref[k]->f.buf[0]->buffer == buf) {
- id_list[i] = k;
- break;
- }
- for (k = 0; k < h->long_ref_count; k++)
- if (h->long_ref[k] && h->long_ref[k]->f.buf[0]->buffer == buf) {
- id_list[i] = h->short_ref_count + k;
- break;
- }
- }
- }
-
- ref2frm[0] =
- ref2frm[1] = -1;
- for (i = 0; i < 16; i++)
- ref2frm[i + 2] = 4 * id_list[i] + (h->ref_list[j][i].reference & 3);
- ref2frm[18 + 0] =
- ref2frm[18 + 1] = -1;
- for (i = 16; i < 48; i++)
- ref2frm[i + 4] = 4 * id_list[(i - 16) >> 1] +
- (h->ref_list[j][i].reference & 3);
- }
-
- if (h->ref_count[0]) h264_set_erpic(&h->er.last_pic, &h->ref_list[0][0]);
- if (h->ref_count[1]) h264_set_erpic(&h->er.next_pic, &h->ref_list[1][0]);
-
- h->er.ref_count = h->ref_count[0];
- h0->au_pps_id = pps_id;
- h->sps.new =
- h0->sps_buffers[h->pps.sps_id]->new = 0;
- h->current_sps_id = h->pps.sps_id;
-
- if (h->avctx->debug & FF_DEBUG_PICT_INFO) {
- av_log(h->avctx, AV_LOG_DEBUG,
- "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",
- h->slice_num,
- (h->picture_structure == PICT_FRAME ? "F" : h->picture_structure == PICT_TOP_FIELD ? "T" : "B"),
- first_mb_in_slice,
- av_get_picture_type_char(h->slice_type),
- h->slice_type_fixed ? " fix" : "",
- h->nal_unit_type == NAL_IDR_SLICE ? " IDR" : "",
- pps_id, h->frame_num,
- h->cur_pic_ptr->field_poc[0],
- h->cur_pic_ptr->field_poc[1],
- h->ref_count[0], h->ref_count[1],
- h->qscale,
- h->deblocking_filter,
- h->slice_alpha_c0_offset, h->slice_beta_offset,
- h->use_weight,
- h->use_weight == 1 && h->use_weight_chroma ? "c" : "",
- h->slice_type == AV_PICTURE_TYPE_B ? (h->direct_spatial_mv_pred ? "SPAT" : "TEMP") : "");
- }
-
- return 0;
-}
-
-int ff_h264_get_slice_type(const H264Context *h)
-{
- switch (h->slice_type) {
- case AV_PICTURE_TYPE_P:
- return 0;
- case AV_PICTURE_TYPE_B:
- return 1;
- case AV_PICTURE_TYPE_I:
- return 2;
- case AV_PICTURE_TYPE_SP:
- return 3;
- case AV_PICTURE_TYPE_SI:
- return 4;
- default:
- return AVERROR_INVALIDDATA;
- }
-}
-
-static av_always_inline void fill_filter_caches_inter(H264Context *h,
- int mb_type, int top_xy,
- int left_xy[LEFT_MBS],
- int top_type,
- int left_type[LEFT_MBS],
- int mb_xy, int list)
-{
- int b_stride = h->b_stride;
- int16_t(*mv_dst)[2] = &h->mv_cache[list][scan8[0]];
- int8_t *ref_cache = &h->ref_cache[list][scan8[0]];
- if (IS_INTER(mb_type) || IS_DIRECT(mb_type)) {
- 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)[64] = (void*)(h->ref2frm[h->slice_table[top_xy] & (MAX_SLICES - 1)][0] + (MB_MBAFF(h) ? 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[list][h->cur_pic.ref_index[list][b8_xy + 0]];
- ref_cache[2 - 1 * 8] =
- ref_cache[3 - 1 * 8] = ref2frm[list][h->cur_pic.ref_index[list][b8_xy + 1]];
- } else {
- AV_ZERO128(mv_dst - 1 * 8);
- AV_WN32A(&ref_cache[0 - 1 * 8], ((LIST_NOT_USED) & 0xFF) * 0x01010101u);
- }
-
- if (!IS_INTERLACED(mb_type ^ left_type[LTOP])) {
- 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)[64] =(void*)( h->ref2frm[h->slice_table[left_xy[LTOP]] & (MAX_SLICES - 1)][0] + (MB_MBAFF(h) ? 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]);
- AV_COPY32(mv_dst - 1 + 24, h->cur_pic.motion_val[list][b_xy + b_stride * 3]);
- ref_cache[-1 + 0] =
- ref_cache[-1 + 8] = ref2frm[list][h->cur_pic.ref_index[list][b8_xy + 2 * 0]];
- ref_cache[-1 + 16] =
- ref_cache[-1 + 24] = ref2frm[list][h->cur_pic.ref_index[list][b8_xy + 2 * 1]];
- } else {
- AV_ZERO32(mv_dst - 1 + 0);
- AV_ZERO32(mv_dst - 1 + 8);
- AV_ZERO32(mv_dst - 1 + 16);
- AV_ZERO32(mv_dst - 1 + 24);
- ref_cache[-1 + 0] =
- ref_cache[-1 + 8] =
- ref_cache[-1 + 16] =
- ref_cache[-1 + 24] = LIST_NOT_USED;
- }
- }
- }
-
- if (!USES_LIST(mb_type, list)) {
- fill_rectangle(mv_dst, 4, 4, 8, pack16to32(0, 0), 4);
- AV_WN32A(&ref_cache[0 * 8], ((LIST_NOT_USED) & 0xFF) * 0x01010101u);
- AV_WN32A(&ref_cache[1 * 8], ((LIST_NOT_USED) & 0xFF) * 0x01010101u);
- AV_WN32A(&ref_cache[2 * 8], ((LIST_NOT_USED) & 0xFF) * 0x01010101u);
- AV_WN32A(&ref_cache[3 * 8], ((LIST_NOT_USED) & 0xFF) * 0x01010101u);
- return;
- }
-
- {
- int8_t *ref = &h->cur_pic.ref_index[list][4 * mb_xy];
- int (*ref2frm)[64] = (void*)(h->ref2frm[h->slice_num & (MAX_SLICES - 1)][0] + (MB_MBAFF(h) ? 20 : 2));
- uint32_t ref01 = (pack16to32(ref2frm[list][ref[0]], ref2frm[list][ref[1]]) & 0x00FF00FF) * 0x0101;
- uint32_t ref23 = (pack16to32(ref2frm[list][ref[2]], ref2frm[list][ref[3]]) & 0x00FF00FF) * 0x0101;
- AV_WN32A(&ref_cache[0 * 8], ref01);
- AV_WN32A(&ref_cache[1 * 8], ref01);
- AV_WN32A(&ref_cache[2 * 8], ref23);
- AV_WN32A(&ref_cache[3 * 8], ref23);
- }
-
- {
- int16_t(*mv_src)[2] = &h->cur_pic.motion_val[list][4 * h->mb_x + 4 * h->mb_y * b_stride];
- AV_COPY128(mv_dst + 8 * 0, mv_src + 0 * b_stride);
- AV_COPY128(mv_dst + 8 * 1, mv_src + 1 * b_stride);
- AV_COPY128(mv_dst + 8 * 2, mv_src + 2 * b_stride);
- AV_COPY128(mv_dst + 8 * 3, mv_src + 3 * b_stride);
- }
-}
-
-/**
- *
- * @return non zero if the loop filter can be skipped
- */
-static int fill_filter_caches(H264Context *h, int mb_type)
-{
- const int mb_xy = h->mb_xy;
- int top_xy, left_xy[LEFT_MBS];
- int top_type, left_type[LEFT_MBS];
- uint8_t *nnz;
- uint8_t *nnz_cache;
-
- top_xy = mb_xy - (h->mb_stride << MB_FIELD(h));
-
- /* Wow, what a mess, why didn't they simplify the interlacing & intra
- * stuff, I can't imagine that these complex rules are worth it. */
-
- left_xy[LBOT] = left_xy[LTOP] = mb_xy - 1;
- if (FRAME_MBAFF(h)) {
- const int left_mb_field_flag = IS_INTERLACED(h->cur_pic.mb_type[mb_xy - 1]);
- const int curr_mb_field_flag = IS_INTERLACED(mb_type);
- if (h->mb_y & 1) {
- if (left_mb_field_flag != curr_mb_field_flag)
- left_xy[LTOP] -= h->mb_stride;
- } else {
- if (curr_mb_field_flag)
- top_xy += h->mb_stride &
- (((h->cur_pic.mb_type[top_xy] >> 7) & 1) - 1);
- if (left_mb_field_flag != curr_mb_field_flag)
- left_xy[LBOT] += h->mb_stride;
- }
- }
-
- h->top_mb_xy = top_xy;
- h->left_mb_xy[LTOP] = left_xy[LTOP];
- h->left_mb_xy[LBOT] = left_xy[LBOT];
- {
- /* For sufficiently low qp, filtering wouldn't do anything.
- * This is a conservative estimate: could also check beta_offset
- * and more accurate chroma_qp. */
- int qp_thresh = h->qp_thresh; // FIXME strictly we should store qp_thresh for each mb of a slice
- int qp = h->cur_pic.qscale_table[mb_xy];
- if (qp <= qp_thresh &&
- (left_xy[LTOP] < 0 ||
- ((qp + h->cur_pic.qscale_table[left_xy[LTOP]] + 1) >> 1) <= qp_thresh) &&
- (top_xy < 0 ||
- ((qp + h->cur_pic.qscale_table[top_xy] + 1) >> 1) <= qp_thresh)) {
- if (!FRAME_MBAFF(h))
- return 1;
- if ((left_xy[LTOP] < 0 ||
- ((qp + h->cur_pic.qscale_table[left_xy[LBOT]] + 1) >> 1) <= qp_thresh) &&
- (top_xy < h->mb_stride ||
- ((qp + h->cur_pic.qscale_table[top_xy - h->mb_stride] + 1) >> 1) <= qp_thresh))
- return 1;
- }
- }
-
- top_type = h->cur_pic.mb_type[top_xy];
- left_type[LTOP] = h->cur_pic.mb_type[left_xy[LTOP]];
- left_type[LBOT] = h->cur_pic.mb_type[left_xy[LBOT]];
- if (h->deblocking_filter == 2) {
- if (h->slice_table[top_xy] != h->slice_num)
- top_type = 0;
- if (h->slice_table[left_xy[LBOT]] != h->slice_num)
- left_type[LTOP] = left_type[LBOT] = 0;
- } else {
- if (h->slice_table[top_xy] == 0xFFFF)
- top_type = 0;
- if (h->slice_table[left_xy[LBOT]] == 0xFFFF)
- left_type[LTOP] = left_type[LBOT] = 0;
- }
- h->top_type = top_type;
- h->left_type[LTOP] = left_type[LTOP];
- h->left_type[LBOT] = left_type[LBOT];
-
- if (IS_INTRA(mb_type))
- return 0;
-
- fill_filter_caches_inter(h, mb_type, top_xy, left_xy,
- top_type, left_type, mb_xy, 0);
- if (h->list_count == 2)
- fill_filter_caches_inter(h, mb_type, top_xy, left_xy,
- top_type, left_type, mb_xy, 1);
-
- nnz = h->non_zero_count[mb_xy];
- nnz_cache = h->non_zero_count_cache;
- AV_COPY32(&nnz_cache[4 + 8 * 1], &nnz[0]);
- AV_COPY32(&nnz_cache[4 + 8 * 2], &nnz[4]);
- AV_COPY32(&nnz_cache[4 + 8 * 3], &nnz[8]);
- AV_COPY32(&nnz_cache[4 + 8 * 4], &nnz[12]);
- h->cbp = h->cbp_table[mb_xy];
-
- if (top_type) {
- nnz = h->non_zero_count[top_xy];
- AV_COPY32(&nnz_cache[4 + 8 * 0], &nnz[3 * 4]);
- }
-
- if (left_type[LTOP]) {
- nnz = h->non_zero_count[left_xy[LTOP]];
- nnz_cache[3 + 8 * 1] = nnz[3 + 0 * 4];
- nnz_cache[3 + 8 * 2] = nnz[3 + 1 * 4];
- nnz_cache[3 + 8 * 3] = nnz[3 + 2 * 4];
- nnz_cache[3 + 8 * 4] = nnz[3 + 3 * 4];
- }
-
- /* CAVLC 8x8dct requires NNZ values for residual decoding that differ
- * from what the loop filter needs */
- if (!CABAC(h) && h->pps.transform_8x8_mode) {
- if (IS_8x8DCT(top_type)) {
- nnz_cache[4 + 8 * 0] =
- nnz_cache[5 + 8 * 0] = (h->cbp_table[top_xy] & 0x4000) >> 12;
- nnz_cache[6 + 8 * 0] =
- nnz_cache[7 + 8 * 0] = (h->cbp_table[top_xy] & 0x8000) >> 12;
- }
- if (IS_8x8DCT(left_type[LTOP])) {
- nnz_cache[3 + 8 * 1] =
- nnz_cache[3 + 8 * 2] = (h->cbp_table[left_xy[LTOP]] & 0x2000) >> 12; // FIXME check MBAFF
- }
- if (IS_8x8DCT(left_type[LBOT])) {
- nnz_cache[3 + 8 * 3] =
- nnz_cache[3 + 8 * 4] = (h->cbp_table[left_xy[LBOT]] & 0x8000) >> 12; // FIXME check MBAFF
- }
-
- if (IS_8x8DCT(mb_type)) {
- nnz_cache[scan8[0]] =
- nnz_cache[scan8[1]] =
- nnz_cache[scan8[2]] =
- nnz_cache[scan8[3]] = (h->cbp & 0x1000) >> 12;
-
- nnz_cache[scan8[0 + 4]] =
- nnz_cache[scan8[1 + 4]] =
- nnz_cache[scan8[2 + 4]] =
- nnz_cache[scan8[3 + 4]] = (h->cbp & 0x2000) >> 12;
-
- nnz_cache[scan8[0 + 8]] =
- nnz_cache[scan8[1 + 8]] =
- nnz_cache[scan8[2 + 8]] =
- nnz_cache[scan8[3 + 8]] = (h->cbp & 0x4000) >> 12;
-
- nnz_cache[scan8[0 + 12]] =
- nnz_cache[scan8[1 + 12]] =
- nnz_cache[scan8[2 + 12]] =
- nnz_cache[scan8[3 + 12]] = (h->cbp & 0x8000) >> 12;
- }
- }
-
- return 0;
-}
-
-static void loop_filter(H264Context *h, int start_x, int end_x)
-{
- uint8_t *dest_y, *dest_cb, *dest_cr;
- int linesize, uvlinesize, mb_x, mb_y;
- const int end_mb_y = h->mb_y + FRAME_MBAFF(h);
- const int old_slice_type = h->slice_type;
- const int pixel_shift = h->pixel_shift;
- const int block_h = 16 >> h->chroma_y_shift;
-
- if (h->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++) {
- int mb_xy, mb_type;
- mb_xy = h->mb_xy = mb_x + mb_y * h->mb_stride;
- h->slice_num = h->slice_table[mb_xy];
- mb_type = h->cur_pic.mb_type[mb_xy];
- h->list_count = h->list_counts[mb_xy];
-
- if (FRAME_MBAFF(h))
- h->mb_mbaff =
- h->mb_field_decoding_flag = !!IS_INTERLACED(mb_type);
-
- h->mb_x = mb_x;
- h->mb_y = mb_y;
- dest_y = h->cur_pic.f.data[0] +
- ((mb_x << pixel_shift) + mb_y * h->linesize) * 16;
- dest_cb = h->cur_pic.f.data[1] +
- (mb_x << pixel_shift) * (8 << CHROMA444(h)) +
- mb_y * h->uvlinesize * block_h;
- dest_cr = h->cur_pic.f.data[2] +
- (mb_x << pixel_shift) * (8 << CHROMA444(h)) +
- mb_y * h->uvlinesize * block_h;
- // FIXME simplify above
-
- if (MB_FIELD(h)) {
- linesize = h->mb_linesize = h->linesize * 2;
- uvlinesize = h->mb_uvlinesize = h->uvlinesize * 2;
- if (mb_y & 1) { // FIXME move out of this function?
- dest_y -= h->linesize * 15;
- dest_cb -= h->uvlinesize * (block_h - 1);
- dest_cr -= h->uvlinesize * (block_h - 1);
- }
- } else {
- linesize = h->mb_linesize = h->linesize;
- uvlinesize = h->mb_uvlinesize = h->uvlinesize;
- }
- backup_mb_border(h, dest_y, dest_cb, dest_cr, linesize,
- uvlinesize, 0);
- if (fill_filter_caches(h, mb_type))
- continue;
- h->chroma_qp[0] = get_chroma_qp(h, 0, h->cur_pic.qscale_table[mb_xy]);
- h->chroma_qp[1] = get_chroma_qp(h, 1, h->cur_pic.qscale_table[mb_xy]);
-
- if (FRAME_MBAFF(h)) {
- ff_h264_filter_mb(h, mb_x, mb_y, dest_y, dest_cb, dest_cr,
- linesize, uvlinesize);
- } else {
- ff_h264_filter_mb_fast(h, mb_x, mb_y, dest_y, dest_cb,
- dest_cr, linesize, uvlinesize);
- }
- }
- }
- h->slice_type = old_slice_type;
- h->mb_x = end_x;
- h->mb_y = end_mb_y - FRAME_MBAFF(h);
- h->chroma_qp[0] = get_chroma_qp(h, 0, h->qscale);
- h->chroma_qp[1] = get_chroma_qp(h, 1, h->qscale);
-}
-
-static void predict_field_decoding_flag(H264Context *h)
-{
- const int mb_xy = h->mb_x + h->mb_y * h->mb_stride;
- int mb_type = (h->slice_table[mb_xy - 1] == h->slice_num) ?
- h->cur_pic.mb_type[mb_xy - 1] :
- (h->slice_table[mb_xy - h->mb_stride] == h->slice_num) ?
- h->cur_pic.mb_type[mb_xy - h->mb_stride] : 0;
- h->mb_mbaff = h->mb_field_decoding_flag = IS_INTERLACED(mb_type) ? 1 : 0;
-}
-
-/**
- * Draw edges and report progress for the last MB row.
- */
-static void decode_finish_row(H264Context *h)
-{
- int top = 16 * (h->mb_y >> FIELD_PICTURE(h));
- int pic_height = 16 * h->mb_height >> FIELD_PICTURE(h);
- int height = 16 << FRAME_MBAFF(h);
- int deblock_border = (16 + 4) << FRAME_MBAFF(h);
-
- if (h->deblocking_filter) {
- if ((top + height) >= pic_height)
- height += deblock_border;
- top -= deblock_border;
- }
-
- if (top >= pic_height || (top + height) < 0)
- return;
-
- height = FFMIN(height, pic_height - top);
- if (top < 0) {
- height = top + height;
- top = 0;
- }
-
- ff_h264_draw_horiz_band(h, top, height);
-
- if (h->droppable || h->er.error_occurred)
- return;
-
- ff_thread_report_progress(&h->cur_pic_ptr->tf, top + height - 1,
- h->picture_structure == PICT_BOTTOM_FIELD);
-}
-
-static void er_add_slice(H264Context *h, int startx, int starty,
- int endx, int endy, int status)
-{
- if (CONFIG_ERROR_RESILIENCE) {
- ERContext *er = &h->er;
-
- ff_er_add_slice(er, startx, starty, endx, endy, status);
- }
-}
-
-static int decode_slice(struct AVCodecContext *avctx, void *arg)
-{
- H264Context *h = *(void **)arg;
- int lf_x_start = h->mb_x;
-
- h->mb_skip_run = -1;
-
- av_assert0(h->block_offset[15] == (4 * ((scan8[15] - scan8[0]) & 7) << h->pixel_shift) + 4 * h->linesize * ((scan8[15] - scan8[0]) >> 3));
-
- h->is_complex = FRAME_MBAFF(h) || h->picture_structure != PICT_FRAME ||
- avctx->codec_id != AV_CODEC_ID_H264 ||
- (CONFIG_GRAY && (h->flags & CODEC_FLAG_GRAY));
-
- if (!(h->avctx->active_thread_type & FF_THREAD_SLICE) && h->picture_structure == PICT_FRAME && h->er.error_status_table) {
- const int start_i = av_clip(h->resync_mb_x + h->resync_mb_y * h->mb_width, 0, h->mb_num - 1);
- if (start_i) {
- int prev_status = h->er.error_status_table[h->er.mb_index2xy[start_i - 1]];
- prev_status &= ~ VP_START;
- if (prev_status != (ER_MV_END | ER_DC_END | ER_AC_END))
- h->er.error_occurred = 1;
- }
- }
-
- if (h->pps.cabac) {
- /* realign */
- align_get_bits(&h->gb);
-
- /* init cabac */
- ff_init_cabac_decoder(&h->cabac,
- h->gb.buffer + get_bits_count(&h->gb) / 8,
- (get_bits_left(&h->gb) + 7) / 8);
-
- ff_h264_init_cabac_states(h);
-
- for (;;) {
- // START_TIMER
- int ret = ff_h264_decode_mb_cabac(h);
- int eos;
- // STOP_TIMER("decode_mb_cabac")
-
- if (ret >= 0)
- ff_h264_hl_decode_mb(h);
-
- // FIXME optimal? or let mb_decode decode 16x32 ?
- if (ret >= 0 && FRAME_MBAFF(h)) {
- h->mb_y++;
-
- ret = ff_h264_decode_mb_cabac(h);
-
- if (ret >= 0)
- ff_h264_hl_decode_mb(h);
- h->mb_y--;
- }
- eos = get_cabac_terminate(&h->cabac);
-
- if ((h->workaround_bugs & FF_BUG_TRUNCATED) &&
- h->cabac.bytestream > h->cabac.bytestream_end + 2) {
- er_add_slice(h, h->resync_mb_x, h->resync_mb_y, h->mb_x - 1,
- h->mb_y, ER_MB_END);
- if (h->mb_x >= lf_x_start)
- loop_filter(h, lf_x_start, h->mb_x + 1);
- return 0;
- }
- if (h->cabac.bytestream > h->cabac.bytestream_end + 2 )
- av_log(h->avctx, AV_LOG_DEBUG, "bytestream overread %td\n", h->cabac.bytestream_end - h->cabac.bytestream);
- if (ret < 0 || h->cabac.bytestream > h->cabac.bytestream_end + 4) {
- av_log(h->avctx, AV_LOG_ERROR,
- "error while decoding MB %d %d, bytestream %td\n",
- h->mb_x, h->mb_y,
- h->cabac.bytestream_end - h->cabac.bytestream);
- er_add_slice(h, h->resync_mb_x, h->resync_mb_y, h->mb_x,
- h->mb_y, ER_MB_ERROR);
- return AVERROR_INVALIDDATA;
- }
-
- if (++h->mb_x >= h->mb_width) {
- loop_filter(h, lf_x_start, h->mb_x);
- h->mb_x = lf_x_start = 0;
- decode_finish_row(h);
- ++h->mb_y;
- if (FIELD_OR_MBAFF_PICTURE(h)) {
- ++h->mb_y;
- if (FRAME_MBAFF(h) && h->mb_y < h->mb_height)
- predict_field_decoding_flag(h);
- }
- }
-
- if (eos || h->mb_y >= h->mb_height) {
- tprintf(h->avctx, "slice end %d %d\n",
- get_bits_count(&h->gb), h->gb.size_in_bits);
- er_add_slice(h, h->resync_mb_x, h->resync_mb_y, h->mb_x - 1,
- h->mb_y, ER_MB_END);
- if (h->mb_x > lf_x_start)
- loop_filter(h, lf_x_start, h->mb_x);
- return 0;
- }
- }
- } else {
- for (;;) {
- int ret = ff_h264_decode_mb_cavlc(h);
-
- if (ret >= 0)
- ff_h264_hl_decode_mb(h);
-
- // FIXME optimal? or let mb_decode decode 16x32 ?
- if (ret >= 0 && FRAME_MBAFF(h)) {
- h->mb_y++;
- ret = ff_h264_decode_mb_cavlc(h);
-
- if (ret >= 0)
- ff_h264_hl_decode_mb(h);
- h->mb_y--;
- }
-
- if (ret < 0) {
- av_log(h->avctx, AV_LOG_ERROR,
- "error while decoding MB %d %d\n", h->mb_x, h->mb_y);
- er_add_slice(h, h->resync_mb_x, h->resync_mb_y, h->mb_x,
- h->mb_y, ER_MB_ERROR);
- return ret;
- }
-
- if (++h->mb_x >= h->mb_width) {
- loop_filter(h, lf_x_start, h->mb_x);
- h->mb_x = lf_x_start = 0;
- decode_finish_row(h);
- ++h->mb_y;
- if (FIELD_OR_MBAFF_PICTURE(h)) {
- ++h->mb_y;
- if (FRAME_MBAFF(h) && h->mb_y < h->mb_height)
- predict_field_decoding_flag(h);
- }
- if (h->mb_y >= h->mb_height) {
- tprintf(h->avctx, "slice end %d %d\n",
- get_bits_count(&h->gb), h->gb.size_in_bits);
-
- if ( get_bits_left(&h->gb) == 0
- || get_bits_left(&h->gb) > 0 && !(h->avctx->err_recognition & AV_EF_AGGRESSIVE)) {
- er_add_slice(h, h->resync_mb_x, h->resync_mb_y,
- h->mb_x - 1, h->mb_y,
- ER_MB_END);
-
- return 0;
- } else {
- er_add_slice(h, h->resync_mb_x, h->resync_mb_y,
- h->mb_x, h->mb_y,
- ER_MB_END);
-
- return AVERROR_INVALIDDATA;
- }
- }
- }
-
- if (get_bits_left(&h->gb) <= 0 && h->mb_skip_run <= 0) {
- tprintf(h->avctx, "slice end %d %d\n",
- get_bits_count(&h->gb), h->gb.size_in_bits);
-
- if (get_bits_left(&h->gb) == 0) {
- er_add_slice(h, h->resync_mb_x, h->resync_mb_y,
- h->mb_x - 1, h->mb_y,
- ER_MB_END);
- if (h->mb_x > lf_x_start)
- loop_filter(h, lf_x_start, h->mb_x);
-
- return 0;
- } else {
- er_add_slice(h, h->resync_mb_x, h->resync_mb_y, h->mb_x,
- h->mb_y, ER_MB_ERROR);
-
- return AVERROR_INVALIDDATA;
- }
- }
- }
- }
-}
-
-/**
- * Call decode_slice() for each context.
- *
- * @param h h264 master context
- * @param context_count number of contexts to execute
- */
-static int execute_decode_slices(H264Context *h, unsigned context_count)
-{
- AVCodecContext *const avctx = h->avctx;
- H264Context *hx;
- int i;
-
- av_assert0(h->mb_y < h->mb_height);
-
- if (h->avctx->hwaccel ||
- h->avctx->codec->capabilities & CODEC_CAP_HWACCEL_VDPAU)
- return 0;
- if (context_count == 1) {
- return decode_slice(avctx, &h);
- } else {
- av_assert0(context_count > 0);
- for (i = 1; i < context_count; i++) {
- hx = h->thread_context[i];
- if (CONFIG_ERROR_RESILIENCE) {
- hx->er.error_count = 0;
- }
- hx->x264_build = h->x264_build;
- }
-
- avctx->execute(avctx, decode_slice, h->thread_context,
- NULL, context_count, sizeof(void *));
-
- /* pull back stuff from slices to master context */
- hx = h->thread_context[context_count - 1];
- h->mb_x = hx->mb_x;
- h->mb_y = hx->mb_y;
- h->droppable = hx->droppable;
- h->picture_structure = hx->picture_structure;
- if (CONFIG_ERROR_RESILIENCE) {
- for (i = 1; i < context_count; i++)
- h->er.error_count += h->thread_context[i]->er.error_count;
- }
- }
-
- return 0;
-}
-