From e0031ca29a471c4a540ba2e01b3f81af03ef757b Mon Sep 17 00:00:00 2001 From: Michael Niedermayer Date: Sun, 26 Jul 2015 17:05:03 +0200 Subject: [PATCH] avcodec/avrndec: Fix cropping This also switches to using a separate AVCodecContext for the inner mjpeg decoder which is cleaner and avoids fields from being overwritten with wrong values Fixes Ticket162 Signed-off-by: Michael Niedermayer --- libavcodec/avrndec.c | 56 ++++++++++++++++++++++++++++++++++++++------ 1 file changed, 49 insertions(+), 7 deletions(-) diff --git a/libavcodec/avrndec.c b/libavcodec/avrndec.c index 7a50a5c3b1..7c156cc096 100644 --- a/libavcodec/avrndec.c +++ b/libavcodec/avrndec.c @@ -26,7 +26,7 @@ #include "libavutil/imgutils.h" typedef struct { - MJpegDecodeContext mjpeg_ctx; + AVCodecContext *mjpeg_avctx; int is_mjpeg; int interlace; //FIXME use frame.interlaced_frame int tff; @@ -45,8 +45,31 @@ static av_cold int init(AVCodecContext *avctx) return AVERROR(EINVAL); } - if(a->is_mjpeg) - return ff_mjpeg_decode_init(avctx); + if(a->is_mjpeg) { + AVCodec *codec = avcodec_find_decoder(AV_CODEC_ID_MJPEG); + AVDictionary *thread_opt = NULL; + if (!codec) { + av_log(avctx, AV_LOG_ERROR, "MJPEG codec not found\n"); + return AVERROR_DECODER_NOT_FOUND; + } + + a->mjpeg_avctx = avcodec_alloc_context3(codec); + + av_dict_set(&thread_opt, "threads", "1", 0); // Is this needed ? + a->mjpeg_avctx->refcounted_frames = 1; + a->mjpeg_avctx->flags = avctx->flags; + a->mjpeg_avctx->idct_algo = avctx->idct_algo; + a->mjpeg_avctx->lowres = avctx->lowres; + a->mjpeg_avctx->width = avctx->width; + a->mjpeg_avctx->height = avctx->height; + + if ((ret = ff_codec_open2_recursive(a->mjpeg_avctx, codec, &thread_opt)) < 0) { + av_log(avctx, AV_LOG_ERROR, "MJPEG codec failed to open\n"); + } + av_dict_free(&thread_opt); + + return ret; + } if ((ret = av_image_check_size(avctx->width, avctx->height, 0, avctx)) < 0) return ret; @@ -68,8 +91,8 @@ static av_cold int end(AVCodecContext *avctx) { AVRnContext *a = avctx->priv_data; - if(a->is_mjpeg) - ff_mjpeg_decode_end(avctx); + avcodec_close(a->mjpeg_avctx); + av_freep(&a->mjpeg_avctx); return 0; } @@ -83,8 +106,27 @@ static int decode_frame(AVCodecContext *avctx, void *data, int buf_size = avpkt->size; int y, ret, true_height; - if(a->is_mjpeg) - return ff_mjpeg_decode_frame(avctx, data, got_frame, avpkt); + if(a->is_mjpeg) { + ret = avcodec_decode_video2(a->mjpeg_avctx, data, got_frame, avpkt); + + if (ret >= 0 && *got_frame && avctx->width <= p->width && avctx->height <= p->height) { + int shift = p->height - avctx->height; + int subsample_h, subsample_v; + + av_pix_fmt_get_chroma_sub_sample(avctx->pix_fmt, &subsample_h, &subsample_v); + + p->data[0] += p->linesize[0] * shift; + if (p->data[2]) { + p->data[1] += p->linesize[1] * (shift>>subsample_v); + p->data[2] += p->linesize[2] * (shift>>subsample_v); + } + + p->width = avctx->width; + p->height = avctx->height; + } + avctx->pix_fmt = a->mjpeg_avctx->pix_fmt; + return ret; + } true_height = buf_size / (2*avctx->width);