00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "avcodec.h"
00023 #include "internal.h"
00024 #include "mjpeg.h"
00025 #include "mjpegdec.h"
00026 #include "libavutil/imgutils.h"
00027
00028 typedef struct {
00029 MJpegDecodeContext mjpeg_ctx;
00030 AVFrame frame;
00031 int is_mjpeg;
00032 int interlace;
00033 int tff;
00034 } AVRnContext;
00035
00036 static av_cold int init(AVCodecContext *avctx)
00037 {
00038 AVRnContext *a = avctx->priv_data;
00039 int ret;
00040
00041
00042 a->is_mjpeg = avctx->extradata_size < 31 || memcmp(&avctx->extradata[28], "1:1", 3);
00043
00044 if(!a->is_mjpeg && avctx->lowres) {
00045 av_log(avctx, AV_LOG_ERROR, "lowres is not possible with rawvideo\n");
00046 return AVERROR(EINVAL);
00047 }
00048
00049 if(a->is_mjpeg)
00050 return ff_mjpeg_decode_init(avctx);
00051
00052 if ((ret = av_image_check_size(avctx->width, avctx->height, 0, avctx)) < 0)
00053 return ret;
00054
00055 avcodec_get_frame_defaults(&a->frame);
00056 avctx->pix_fmt = AV_PIX_FMT_UYVY422;
00057
00058 if(avctx->extradata_size >= 9 && avctx->extradata[4]+28 < avctx->extradata_size) {
00059 int ndx = avctx->extradata[4] + 4;
00060 a->interlace = !memcmp(avctx->extradata + ndx, "1:1(", 4);
00061 if(a->interlace) {
00062 a->tff = avctx->extradata[ndx + 24] == 1;
00063 }
00064 }
00065
00066 return 0;
00067 }
00068
00069 static av_cold int end(AVCodecContext *avctx)
00070 {
00071 AVRnContext *a = avctx->priv_data;
00072 AVFrame *p = &a->frame;
00073
00074 if(p->data[0])
00075 avctx->release_buffer(avctx, p);
00076
00077 if(a->is_mjpeg)
00078 ff_mjpeg_decode_end(avctx);
00079
00080 return 0;
00081 }
00082
00083 static int decode_frame(AVCodecContext *avctx, void *data,
00084 int *got_frame, AVPacket *avpkt)
00085 {
00086 AVRnContext *a = avctx->priv_data;
00087 AVFrame *p = &a->frame;
00088 const uint8_t *buf = avpkt->data;
00089 int buf_size = avpkt->size;
00090 int y, ret, true_height;
00091
00092 if(a->is_mjpeg)
00093 return ff_mjpeg_decode_frame(avctx, data, got_frame, avpkt);
00094
00095 true_height = buf_size / (2*avctx->width);
00096 if(p->data[0])
00097 avctx->release_buffer(avctx, p);
00098
00099 if(buf_size < 2*avctx->width * avctx->height) {
00100 av_log(avctx, AV_LOG_ERROR, "packet too small\n");
00101 return AVERROR_INVALIDDATA;
00102 }
00103
00104 if((ret = ff_get_buffer(avctx, p)) < 0){
00105 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
00106 return ret;
00107 }
00108 p->pict_type= AV_PICTURE_TYPE_I;
00109 p->key_frame= 1;
00110
00111 if(a->interlace) {
00112 buf += (true_height - avctx->height)*avctx->width;
00113 for(y = 0; y < avctx->height-1; y+=2) {
00114 memcpy(p->data[0] + (y+ a->tff)*p->linesize[0], buf , 2*avctx->width);
00115 memcpy(p->data[0] + (y+!a->tff)*p->linesize[0], buf + avctx->width*true_height+4, 2*avctx->width);
00116 buf += 2*avctx->width;
00117 }
00118 } else {
00119 buf += (true_height - avctx->height)*avctx->width*2;
00120 for(y = 0; y < avctx->height; y++) {
00121 memcpy(p->data[0] + y*p->linesize[0], buf, 2*avctx->width);
00122 buf += 2*avctx->width;
00123 }
00124 }
00125
00126 *(AVFrame*)data = a->frame;
00127 *got_frame = 1;
00128 return buf_size;
00129 }
00130
00131 AVCodec ff_avrn_decoder = {
00132 .name = "avrn",
00133 .type = AVMEDIA_TYPE_VIDEO,
00134 .id = AV_CODEC_ID_AVRN,
00135 .priv_data_size = sizeof(AVRnContext),
00136 .init = init,
00137 .close = end,
00138 .decode = decode_frame,
00139 .long_name = NULL_IF_CONFIG_SMALL("Avid AVI Codec"),
00140 .capabilities = CODEC_CAP_DR1,
00141 .max_lowres = 3,
00142 };
00143