Commit e0031ca2 authored by Michael Niedermayer's avatar Michael Niedermayer

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: 's avatarMichael Niedermayer <michael@niedermayer.cc>
parent 4b90dcb8
...@@ -26,7 +26,7 @@ ...@@ -26,7 +26,7 @@
#include "libavutil/imgutils.h" #include "libavutil/imgutils.h"
typedef struct { typedef struct {
MJpegDecodeContext mjpeg_ctx; AVCodecContext *mjpeg_avctx;
int is_mjpeg; int is_mjpeg;
int interlace; //FIXME use frame.interlaced_frame int interlace; //FIXME use frame.interlaced_frame
int tff; int tff;
...@@ -45,8 +45,31 @@ static av_cold int init(AVCodecContext *avctx) ...@@ -45,8 +45,31 @@ static av_cold int init(AVCodecContext *avctx)
return AVERROR(EINVAL); return AVERROR(EINVAL);
} }
if(a->is_mjpeg) if(a->is_mjpeg) {
return ff_mjpeg_decode_init(avctx); 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) if ((ret = av_image_check_size(avctx->width, avctx->height, 0, avctx)) < 0)
return ret; return ret;
...@@ -68,8 +91,8 @@ static av_cold int end(AVCodecContext *avctx) ...@@ -68,8 +91,8 @@ static av_cold int end(AVCodecContext *avctx)
{ {
AVRnContext *a = avctx->priv_data; AVRnContext *a = avctx->priv_data;
if(a->is_mjpeg) avcodec_close(a->mjpeg_avctx);
ff_mjpeg_decode_end(avctx); av_freep(&a->mjpeg_avctx);
return 0; return 0;
} }
...@@ -83,8 +106,27 @@ static int decode_frame(AVCodecContext *avctx, void *data, ...@@ -83,8 +106,27 @@ static int decode_frame(AVCodecContext *avctx, void *data,
int buf_size = avpkt->size; int buf_size = avpkt->size;
int y, ret, true_height; int y, ret, true_height;
if(a->is_mjpeg) if(a->is_mjpeg) {
return ff_mjpeg_decode_frame(avctx, data, got_frame, avpkt); 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); true_height = buf_size / (2*avctx->width);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment