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 <michael@niedermayer.cc>
This commit is contained in:
Michael Niedermayer 2015-07-26 17:05:03 +02:00
parent 4b90dcb849
commit e0031ca29a
1 changed files with 49 additions and 7 deletions

View File

@ -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);