h->ref_cache[1][scan8[7] + 1] =
h->ref_cache[1][scan8[13] + 1] = PART_NOT_AVAILABLE;
- /* init ER */
- er->avctx = h->avctx;
- er->dsp = &h->dsp;
- er->decode_mb = h264_er_decode_mb;
- er->opaque = h;
- er->quarter_sample = 1;
-
- er->mb_num = h->mb_num;
- er->mb_width = h->mb_width;
- er->mb_height = h->mb_height;
- er->mb_stride = h->mb_stride;
- er->b8_stride = h->mb_width * 2 + 1;
-
- FF_ALLOCZ_OR_GOTO(h->avctx, er->mb_index2xy, (h->mb_num + 1) * sizeof(int),
- fail); // error ressilience code looks cleaner with this
- for (y = 0; y < h->mb_height; y++)
- for (x = 0; x < h->mb_width; x++)
- er->mb_index2xy[x + y * h->mb_width] = x + y * h->mb_stride;
+ if (CONFIG_ERROR_RESILIENCE) {
+ /* init ER */
+ er->avctx = h->avctx;
+ er->dsp = &h->dsp;
+ er->decode_mb = h264_er_decode_mb;
+ er->opaque = h;
+ er->quarter_sample = 1;
+
+ er->mb_num = h->mb_num;
+ er->mb_width = h->mb_width;
+ er->mb_height = h->mb_height;
+ er->mb_stride = h->mb_stride;
+ er->b8_stride = h->mb_width * 2 + 1;
+
+ FF_ALLOCZ_OR_GOTO(h->avctx, er->mb_index2xy, (h->mb_num + 1) * sizeof(int),
+ fail); // error ressilience code looks cleaner with this
+ for (y = 0; y < h->mb_height; y++)
+ for (x = 0; x < h->mb_width; x++)
+ er->mb_index2xy[x + y * h->mb_width] = x + y * h->mb_stride;
- er->mb_index2xy[h->mb_height * h->mb_width] = (h->mb_height - 1) *
- h->mb_stride + h->mb_width;
+ er->mb_index2xy[h->mb_height * h->mb_width] = (h->mb_height - 1) *
+ h->mb_stride + h->mb_width;
- FF_ALLOCZ_OR_GOTO(h->avctx, er->error_status_table,
- mb_array_size * sizeof(uint8_t), fail);
+ FF_ALLOCZ_OR_GOTO(h->avctx, er->error_status_table,
+ mb_array_size * sizeof(uint8_t), fail);
- FF_ALLOC_OR_GOTO(h->avctx, er->mbintra_table, mb_array_size, fail);
- memset(er->mbintra_table, 1, mb_array_size);
+ FF_ALLOC_OR_GOTO(h->avctx, er->mbintra_table, mb_array_size, fail);
+ memset(er->mbintra_table, 1, mb_array_size);
- FF_ALLOCZ_OR_GOTO(h->avctx, er->mbskip_table, mb_array_size + 2, fail);
+ FF_ALLOCZ_OR_GOTO(h->avctx, er->mbskip_table, mb_array_size + 2, fail);
- FF_ALLOC_OR_GOTO(h->avctx, er->er_temp_buffer, h->mb_height * h->mb_stride,
- fail);
+ FF_ALLOC_OR_GOTO(h->avctx, er->er_temp_buffer, h->mb_height * h->mb_stride,
+ fail);
- FF_ALLOCZ_OR_GOTO(h->avctx, h->dc_val_base, yc_size * sizeof(int16_t), fail);
- er->dc_val[0] = h->dc_val_base + h->mb_width * 2 + 2;
- er->dc_val[1] = h->dc_val_base + y_size + h->mb_stride + 1;
- er->dc_val[2] = er->dc_val[1] + c_size;
- for (i = 0; i < yc_size; i++)
- h->dc_val_base[i] = 1024;
+ FF_ALLOCZ_OR_GOTO(h->avctx, h->dc_val_base, yc_size * sizeof(int16_t), fail);
+ er->dc_val[0] = h->dc_val_base + h->mb_width * 2 + 2;
+ er->dc_val[1] = h->dc_val_base + y_size + h->mb_stride + 1;
+ er->dc_val[2] = er->dc_val[1] + c_size;
+ for (i = 0; i < yc_size; i++)
+ h->dc_val_base[i] = 1024;
+ }
return 0;
h->avctx = avctx;
- h->width = h->avctx->width;
- h->height = h->avctx->height;
-
h->bit_depth_luma = 8;
h->chroma_format_idc = 1;
h->dequant_coeff_pps = -1;
/* needed so that IDCT permutation is known early */
- ff_dsputil_init(&h->dsp, h->avctx);
+ if (CONFIG_ERROR_RESILIENCE)
+ ff_dsputil_init(&h->dsp, h->avctx);
ff_videodsp_init(&h->vdsp, 8);
memset(h->pps.scaling_matrix4, 16, 6 * 16 * sizeof(uint8_t));
memset(h->pps_buffers, 0, sizeof(h->pps_buffers));
memset(&h->er, 0, sizeof(h->er));
memset(&h->me, 0, sizeof(h->me));
+ memset(&h->mb, 0, sizeof(h->mb));
+ memset(&h->mb_luma_dc, 0, sizeof(h->mb_luma_dc));
+ memset(&h->mb_padding, 0, sizeof(h->mb_padding));
h->context_initialized = 0;
memset(&h->cur_pic, 0, sizeof(h->cur_pic));
h->thread_context[0] = h;
- h->dsp.clear_blocks(h->mb);
- h->dsp.clear_blocks(h->mb + (24 * 16 << h->pixel_shift));
h->context_initialized = 1;
}
if ((ret = ref_picture(h, &h->cur_pic, h->cur_pic_ptr)) < 0)
return ret;
- ff_er_frame_start(&h->er);
+ if (CONFIG_ERROR_RESILIENCE)
+ ff_er_frame_start(&h->er);
assert(h->linesize && h->uvlinesize);
av_log(h->avctx, AV_LOG_DEBUG, "no picture\n");
}
- if (setup_finished)
+ if (setup_finished && !h->avctx->hwaccel)
ff_thread_finish_setup(h->avctx);
}
uint8_t *top_border;
int top_idx = 1;
const int pixel_shift = h->pixel_shift;
- int chroma444 = CHROMA444;
- int chroma422 = CHROMA422;
+ int chroma444 = CHROMA444(h);
+ int chroma422 = CHROMA422(h);
src_y -= linesize;
src_cb -= uvlinesize;
if (IS_8x8DCT(mb_type)) {
if (transform_bypass) {
idct_dc_add =
- idct_add = h->h264dsp.h264_add_pixels8;
+ idct_add = h->h264dsp.h264_add_pixels8_clear;
} else {
idct_dc_add = h->h264dsp.h264_idct8_dc_add;
idct_add = h->h264dsp.h264_idct8_add;
} else {
if (transform_bypass) {
idct_dc_add =
- idct_add = h->h264dsp.h264_add_pixels4;
+ idct_add = h->h264dsp.h264_add_pixels4_clear;
} else {
idct_dc_add = h->h264dsp.h264_idct_dc_add;
idct_add = h->h264dsp.h264_idct_add;
for (i = 0; i < 16; i++)
if (h->non_zero_count_cache[scan8[i + p * 16]] ||
dctcoef_get(h->mb, pixel_shift, i * 16 + p * 256))
- h->h264dsp.h264_add_pixels4(dest_y + block_offset[i],
- h->mb + (i * 16 + p * 256 << pixel_shift),
- linesize);
+ h->h264dsp.h264_add_pixels4_clear(dest_y + block_offset[i],
+ h->mb + (i * 16 + p * 256 << pixel_shift),
+ linesize);
}
} else {
h->h264dsp.h264_idct_add16intra(dest_y, block_offset,
} else if (h->cbp & 15) {
if (transform_bypass) {
const int di = IS_8x8DCT(mb_type) ? 4 : 1;
- idct_add = IS_8x8DCT(mb_type) ? h->h264dsp.h264_add_pixels8
- : h->h264dsp.h264_add_pixels4;
+ idct_add = IS_8x8DCT(mb_type) ? h->h264dsp.h264_add_pixels8_clear
+ : h->h264dsp.h264_add_pixels4_clear;
for (i = 0; i < 16; i += di)
if (h->non_zero_count_cache[scan8[i + p * 16]])
idct_add(dest_y + block_offset[i],
const int mb_type = h->cur_pic.mb_type[mb_xy];
int is_complex = CONFIG_SMALL || h->is_complex || IS_INTRA_PCM(mb_type) || h->qscale == 0;
- if (CHROMA444) {
+ if (CHROMA444(h)) {
if (is_complex || h->pixel_shift)
hl_decode_mb_444_complex(h);
else
h->parse_context.last_index = 0;
}
-static int init_poc(H264Context *h)
+int ff_init_poc(H264Context *h, int pic_field_poc[2], int *pic_poc)
{
const int max_frame_num = 1 << h->sps.log2_max_frame_num;
int field_poc[2];
- Picture *cur = h->cur_pic_ptr;
h->frame_num_offset = h->prev_frame_num_offset;
if (h->frame_num < h->prev_frame_num)
}
if (h->picture_structure != PICT_BOTTOM_FIELD)
- h->cur_pic_ptr->field_poc[0] = field_poc[0];
+ pic_field_poc[0] = field_poc[0];
if (h->picture_structure != PICT_TOP_FIELD)
- h->cur_pic_ptr->field_poc[1] = field_poc[1];
- cur->poc = FFMIN(cur->field_poc[0], cur->field_poc[1]);
+ pic_field_poc[1] = field_poc[1];
+ if (pic_poc)
+ *pic_poc = FFMIN(pic_field_poc[0], pic_field_poc[1]);
return 0;
}
* past end by one (callers fault) and resync_mb_y != 0
* causes problems for the first MB line, too.
*/
- if (!FIELD_PICTURE(h)) {
+ if (CONFIG_ERROR_RESILIENCE && !FIELD_PICTURE(h)) {
h->er.cur_pic = h->cur_pic_ptr;
h->er.last_pic = h->ref_count[0] ? &h->ref_list[0][0] : NULL;
h->er.next_pic = h->ref_count[1] ? &h->ref_list[1][0] : NULL;
ff_h264qpel_init(&h->h264qpel, h->sps.bit_depth_luma);
ff_h264_pred_init(&h->hpc, h->avctx->codec_id, h->sps.bit_depth_luma,
h->sps.chroma_format_idc);
- h->dsp.dct_bits = h->sps.bit_depth_luma > 8 ? 32 : 16;
- ff_dsputil_init(&h->dsp, h->avctx);
+ if (CONFIG_ERROR_RESILIENCE)
+ ff_dsputil_init(&h->dsp, h->avctx);
ff_videodsp_init(&h->vdsp, h->sps.bit_depth_luma);
} else {
av_log(h->avctx, AV_LOG_ERROR, "Unsupported bit depth: %d\n",
{
switch (h->sps.bit_depth_luma) {
case 9:
- if (CHROMA444) {
+ if (CHROMA444(h)) {
if (h->avctx->colorspace == AVCOL_SPC_RGB) {
return AV_PIX_FMT_GBRP9;
} else
return AV_PIX_FMT_YUV444P9;
- } else if (CHROMA422)
+ } else if (CHROMA422(h))
return AV_PIX_FMT_YUV422P9;
else
return AV_PIX_FMT_YUV420P9;
break;
case 10:
- if (CHROMA444) {
+ if (CHROMA444(h)) {
if (h->avctx->colorspace == AVCOL_SPC_RGB) {
return AV_PIX_FMT_GBRP10;
} else
return AV_PIX_FMT_YUV444P10;
- } else if (CHROMA422)
+ } else if (CHROMA422(h))
return AV_PIX_FMT_YUV422P10;
else
return AV_PIX_FMT_YUV420P10;
break;
case 8:
- if (CHROMA444) {
+ if (CHROMA444(h)) {
if (h->avctx->colorspace == AVCOL_SPC_RGB) {
return AV_PIX_FMT_GBRP;
} else
return h->avctx->color_range == AVCOL_RANGE_JPEG ? AV_PIX_FMT_YUVJ444P
: AV_PIX_FMT_YUV444P;
- } else if (CHROMA422) {
+ } else if (CHROMA422(h)) {
return h->avctx->color_range == AVCOL_RANGE_JPEG ? AV_PIX_FMT_YUVJ422P
: AV_PIX_FMT_YUV422P;
} else {
}
}
+/* export coded and cropped frame dimensions to AVCodecContext */
+static int init_dimensions(H264Context *h)
+{
+ int width = h->width - (h->sps.crop_right + h->sps.crop_left);
+ int height = h->height - (h->sps.crop_top + h->sps.crop_bottom);
+
+ /* handle container cropping */
+ if (!h->sps.crop &&
+ FFALIGN(h->avctx->width, 16) == h->width &&
+ FFALIGN(h->avctx->height, 16) == h->height) {
+ width = h->avctx->width;
+ height = h->avctx->height;
+ }
+
+ if (width <= 0 || height <= 0) {
+ av_log(h->avctx, AV_LOG_ERROR, "Invalid cropped dimensions: %dx%d.\n",
+ width, height);
+ if (h->avctx->err_recognition & AV_EF_EXPLODE)
+ return AVERROR_INVALIDDATA;
+
+ av_log(h->avctx, AV_LOG_WARNING, "Ignoring cropping information.\n");
+ h->sps.crop_bottom = h->sps.crop_top = h->sps.crop_right = h->sps.crop_left = 0;
+ h->sps.crop = 0;
+
+ width = h->width;
+ height = h->height;
+ }
+
+ h->avctx->coded_width = h->width;
+ h->avctx->coded_height = h->height;
+ h->avctx->width = width;
+ h->avctx->height = height;
+
+ return 0;
+}
+
static int h264_slice_header_init(H264Context *h, int reinit)
{
int nb_slices = (HAVE_THREADS &&
h->avctx->thread_count : 1;
int i;
- avcodec_set_dimensions(h->avctx, h->width, h->height);
h->avctx->sample_aspect_ratio = h->sps.sar;
av_assert0(h->avctx->sample_aspect_ratio.den);
av_pix_fmt_get_chroma_sub_sample(h->avctx->pix_fmt,
h->chroma_y_shift = h->sps.chroma_format_idc <= 1; // 400 uses yuv420p
- h->width = 16 * h->mb_width - (2 >> CHROMA444) * FFMIN(h->sps.crop_right, (8 << CHROMA444) - 1);
- if (h->sps.frame_mbs_only_flag)
- h->height = 16 * h->mb_height - (1 << h->chroma_y_shift) * FFMIN(h->sps.crop_bottom, (16 >> h->chroma_y_shift) - 1);
- else
- h->height = 16 * h->mb_height - (2 << h->chroma_y_shift) * FFMIN(h->sps.crop_bottom, (16 >> h->chroma_y_shift) - 1);
+ h->width = 16 * h->mb_width;
+ h->height = 16 * h->mb_height;
- if (FFALIGN(h->avctx->width, 16) == h->width &&
- FFALIGN(h->avctx->height, 16) == h->height) {
- h->width = h->avctx->width;
- h->height = h->avctx->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 ? AVCOL_RANGE_JPEG
}
if (h->context_initialized &&
- (h->width != h->avctx->width ||
- h->height != h->avctx->height ||
+ (h->width != h->avctx->coded_width ||
+ h->height != h->avctx->coded_height ||
needs_reinit)) {
if (h != h0) {
h->delta_poc[1] = get_se_golomb(&h->gb);
}
- init_poc(h);
+ 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);
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) +
+ (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) +
+ (mb_x << pixel_shift) * (8 << CHROMA444(h)) +
mb_y * h->uvlinesize * block_h;
// FIXME simplify above
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;
er->ref_count = h->ref_count[0];
ff_er_add_slice(er, startx, starty, endx, endy, status);
+#endif
}
static int decode_slice(struct AVCodecContext *avctx, void *arg)
return pos;
}
+static int output_frame(H264Context *h, AVFrame *dst, AVFrame *src)
+{
+ int i;
+ int ret = av_frame_ref(dst, src);
+ if (ret < 0)
+ return ret;
+
+ if (!h->sps.crop)
+ return 0;
+
+ for (i = 0; i < 3; i++) {
+ int hshift = (i > 0) ? h->chroma_x_shift : 0;
+ int vshift = (i > 0) ? h->chroma_y_shift : 0;
+ int off = ((h->sps.crop_left >> hshift) << h->pixel_shift) +
+ (h->sps.crop_top >> vshift) * dst->linesize[i];
+ dst->data[i] += off;
+ }
+ return 0;
+}
+
static int decode_frame(AVCodecContext *avctx, void *data,
int *got_frame, AVPacket *avpkt)
{
h->delayed_pic[i] = h->delayed_pic[i + 1];
if (out) {
- if ((ret = av_frame_ref(pict, &out->f)) < 0)
+ ret = output_frame(h, pict, &out->f);
+ if (ret < 0)
return ret;
*got_frame = 1;
}
/* Wait for second field. */
*got_frame = 0;
} else {
- if ((ret = av_frame_ref(pict, &h->next_output_pic->f)) < 0)
+ ret = output_frame(h, pict, &h->next_output_pic->f);
+ if (ret < 0)
return ret;
*got_frame = 1;
}
static av_cold int h264_decode_end(AVCodecContext *avctx)
{
H264Context *h = avctx->priv_data;
- int i;
ff_h264_free_context(h);
- if (h->DPB) {
- for (i = 0; i < MAX_PICTURE_COUNT; i++) {
- unref_picture(h, &h->DPB[i]);
- }
- }
- av_freep(&h->DPB);
-
unref_picture(h, &h->cur_pic);
return 0;