00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00027 #include "avcodec.h"
00028 #include "raw.h"
00029 #include "libavutil/avassert.h"
00030 #include "libavutil/common.h"
00031 #include "libavutil/intreadwrite.h"
00032 #include "libavutil/imgutils.h"
00033 #include "libavutil/opt.h"
00034
00035 typedef struct RawVideoContext {
00036 AVClass *av_class;
00037 uint32_t palette[AVPALETTE_COUNT];
00038 unsigned char * buffer;
00039 int length;
00040 int flip;
00041 AVFrame pic;
00042 int tff;
00043 } RawVideoContext;
00044
00045 static const AVOption options[]={
00046 {"top", "top field first", offsetof(RawVideoContext, tff), AV_OPT_TYPE_INT, {.i64 = -1}, -1, 1, AV_OPT_FLAG_DECODING_PARAM|AV_OPT_FLAG_VIDEO_PARAM},
00047 {NULL}
00048 };
00049
00050 static const AVClass class = {
00051 .class_name = "rawdec",
00052 .option = options,
00053 .version = LIBAVUTIL_VERSION_INT,
00054 };
00055
00056 static const PixelFormatTag pix_fmt_bps_avi[] = {
00057 { AV_PIX_FMT_MONOWHITE, 1 },
00058 { AV_PIX_FMT_PAL8, 2 },
00059 { AV_PIX_FMT_PAL8, 4 },
00060 { AV_PIX_FMT_PAL8, 8 },
00061 { AV_PIX_FMT_RGB444, 12 },
00062 { AV_PIX_FMT_RGB555, 15 },
00063 { AV_PIX_FMT_RGB555, 16 },
00064 { AV_PIX_FMT_BGR24, 24 },
00065 { AV_PIX_FMT_BGRA, 32 },
00066 { AV_PIX_FMT_NONE, 0 },
00067 };
00068
00069 static const PixelFormatTag pix_fmt_bps_mov[] = {
00070 { AV_PIX_FMT_MONOWHITE, 1 },
00071 { AV_PIX_FMT_PAL8, 2 },
00072 { AV_PIX_FMT_PAL8, 4 },
00073 { AV_PIX_FMT_PAL8, 8 },
00074
00075
00076 { AV_PIX_FMT_RGB555BE, 16 },
00077 { AV_PIX_FMT_RGB24, 24 },
00078 { AV_PIX_FMT_ARGB, 32 },
00079 { AV_PIX_FMT_MONOWHITE,33 },
00080 { AV_PIX_FMT_NONE, 0 },
00081 };
00082
00083 enum AVPixelFormat ff_find_pix_fmt(const PixelFormatTag *tags, unsigned int fourcc)
00084 {
00085 while (tags->pix_fmt >= 0) {
00086 if (tags->fourcc == fourcc)
00087 return tags->pix_fmt;
00088 tags++;
00089 }
00090 return AV_PIX_FMT_YUV420P;
00091 }
00092
00093 static av_cold int raw_init_decoder(AVCodecContext *avctx)
00094 {
00095 RawVideoContext *context = avctx->priv_data;
00096
00097 if (avctx->codec_tag == MKTAG('r','a','w',' ') || avctx->codec_tag == MKTAG('N','O','1','6'))
00098 avctx->pix_fmt = ff_find_pix_fmt(pix_fmt_bps_mov, avctx->bits_per_coded_sample);
00099 else if (avctx->codec_tag == MKTAG('W','R','A','W'))
00100 avctx->pix_fmt = ff_find_pix_fmt(pix_fmt_bps_avi, avctx->bits_per_coded_sample);
00101 else if (avctx->codec_tag)
00102 avctx->pix_fmt = ff_find_pix_fmt(ff_raw_pix_fmt_tags, avctx->codec_tag);
00103 else if (avctx->pix_fmt == AV_PIX_FMT_NONE && avctx->bits_per_coded_sample)
00104 avctx->pix_fmt = ff_find_pix_fmt(pix_fmt_bps_avi, avctx->bits_per_coded_sample);
00105
00106 if (avctx->pix_fmt == AV_PIX_FMT_NONE) {
00107 av_log(avctx, AV_LOG_ERROR, "Pixel format was not specified and cannot be detected\n");
00108 return AVERROR(EINVAL);
00109 }
00110
00111 avpriv_set_systematic_pal2(context->palette, avctx->pix_fmt);
00112 if((avctx->bits_per_coded_sample == 4 || avctx->bits_per_coded_sample == 2) &&
00113 avctx->pix_fmt==AV_PIX_FMT_PAL8 &&
00114 (!avctx->codec_tag || avctx->codec_tag == MKTAG('r','a','w',' '))){
00115 context->length = avpicture_get_size(avctx->pix_fmt, FFALIGN(avctx->width, 16), avctx->height);
00116 if (context->length < 0)
00117 return context->length;
00118 context->buffer = av_malloc(context->length);
00119 if (!context->buffer)
00120 return AVERROR(ENOMEM);
00121 } else {
00122 context->length = avpicture_get_size(avctx->pix_fmt, avctx->width, avctx->height);
00123 if (context->length < 0)
00124 return context->length;
00125 }
00126 context->pic.pict_type = AV_PICTURE_TYPE_I;
00127 context->pic.key_frame = 1;
00128
00129 avctx->coded_frame= &context->pic;
00130
00131 if((avctx->extradata_size >= 9 && !memcmp(avctx->extradata + avctx->extradata_size - 9, "BottomUp", 9)) ||
00132 avctx->codec_tag == MKTAG('c','y','u','v') ||
00133 avctx->codec_tag == MKTAG(3, 0, 0, 0) || avctx->codec_tag == MKTAG('W','R','A','W'))
00134 context->flip=1;
00135
00136 if (avctx->field_order > AV_FIELD_PROGRESSIVE) {
00137 avctx->coded_frame->interlaced_frame = 1;
00138 if (avctx->field_order == AV_FIELD_TT || avctx->field_order == AV_FIELD_TB)
00139 avctx->coded_frame->top_field_first = 1;
00140 }
00141
00142
00143 return 0;
00144 }
00145
00146 static void flip(AVCodecContext *avctx, AVPicture * picture){
00147 picture->data[0] += picture->linesize[0] * (avctx->height-1);
00148 picture->linesize[0] *= -1;
00149 }
00150
00151 static int raw_decode(AVCodecContext *avctx,
00152 void *data, int *got_frame,
00153 AVPacket *avpkt)
00154 {
00155 const uint8_t *buf = avpkt->data;
00156 int buf_size = avpkt->size;
00157 int linesize_align = 4;
00158 RawVideoContext *context = avctx->priv_data;
00159 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(avctx->pix_fmt);
00160 int res, len;
00161
00162 AVFrame *frame = data;
00163 AVPicture *picture = data;
00164
00165 frame->pict_type = avctx->coded_frame->pict_type;
00166 frame->interlaced_frame = avctx->coded_frame->interlaced_frame;
00167 frame->top_field_first = avctx->coded_frame->top_field_first;
00168 frame->reordered_opaque = avctx->reordered_opaque;
00169 frame->pkt_pts = avctx->pkt->pts;
00170 frame->pkt_pos = avctx->pkt->pos;
00171 frame->pkt_duration = avctx->pkt->duration;
00172
00173 if(context->tff>=0){
00174 frame->interlaced_frame = 1;
00175 frame->top_field_first = context->tff;
00176 }
00177
00178 if ((res = av_image_check_size(avctx->width, avctx->height, 0, avctx)) < 0)
00179 return res;
00180
00181
00182 if (context->buffer) {
00183 int i;
00184 uint8_t *dst = context->buffer;
00185 buf_size = context->length - AVPALETTE_SIZE;
00186 if (avctx->bits_per_coded_sample == 4){
00187 for(i=0; 2*i+1 < buf_size && i<avpkt->size; i++){
00188 dst[2*i+0]= buf[i]>>4;
00189 dst[2*i+1]= buf[i]&15;
00190 }
00191 linesize_align = 8;
00192 } else {
00193 av_assert0(avctx->bits_per_coded_sample == 2);
00194 for(i=0; 4*i+3 < buf_size && i<avpkt->size; i++){
00195 dst[4*i+0]= buf[i]>>6;
00196 dst[4*i+1]= buf[i]>>4&3;
00197 dst[4*i+2]= buf[i]>>2&3;
00198 dst[4*i+3]= buf[i] &3;
00199 }
00200 linesize_align = 16;
00201 }
00202 buf= dst;
00203 }
00204
00205 if(avctx->codec_tag == MKTAG('A', 'V', '1', 'x') ||
00206 avctx->codec_tag == MKTAG('A', 'V', 'u', 'p'))
00207 buf += buf_size - context->length;
00208
00209 len = context->length - (avctx->pix_fmt==AV_PIX_FMT_PAL8 ? AVPALETTE_SIZE : 0);
00210 if (buf_size < len) {
00211 av_log(avctx, AV_LOG_ERROR, "Invalid buffer size, packet size %d < expected length %d\n", buf_size, len);
00212 return AVERROR(EINVAL);
00213 }
00214
00215 if ((res = avpicture_fill(picture, buf, avctx->pix_fmt,
00216 avctx->width, avctx->height)) < 0)
00217 return res;
00218 if((avctx->pix_fmt==AV_PIX_FMT_PAL8 && buf_size < context->length) ||
00219 (desc->flags & PIX_FMT_PSEUDOPAL)) {
00220 frame->data[1]= (uint8_t*)context->palette;
00221 }
00222 if (avctx->pix_fmt == AV_PIX_FMT_PAL8) {
00223 const uint8_t *pal = av_packet_get_side_data(avpkt, AV_PKT_DATA_PALETTE, NULL);
00224
00225 if (pal) {
00226 memcpy(frame->data[1], pal, AVPALETTE_SIZE);
00227 frame->palette_has_changed = 1;
00228 }
00229 }
00230 if((avctx->pix_fmt==AV_PIX_FMT_BGR24 ||
00231 avctx->pix_fmt==AV_PIX_FMT_GRAY8 ||
00232 avctx->pix_fmt==AV_PIX_FMT_RGB555LE ||
00233 avctx->pix_fmt==AV_PIX_FMT_RGB555BE ||
00234 avctx->pix_fmt==AV_PIX_FMT_RGB565LE ||
00235 avctx->pix_fmt==AV_PIX_FMT_MONOWHITE ||
00236 avctx->pix_fmt==AV_PIX_FMT_PAL8) &&
00237 FFALIGN(frame->linesize[0], linesize_align)*avctx->height <= buf_size)
00238 frame->linesize[0] = FFALIGN(frame->linesize[0], linesize_align);
00239
00240 if(context->flip)
00241 flip(avctx, picture);
00242
00243 if ( avctx->codec_tag == MKTAG('Y', 'V', '1', '2')
00244 || avctx->codec_tag == MKTAG('Y', 'V', '1', '6')
00245 || avctx->codec_tag == MKTAG('Y', 'V', '2', '4')
00246 || avctx->codec_tag == MKTAG('Y', 'V', 'U', '9'))
00247 FFSWAP(uint8_t *, picture->data[1], picture->data[2]);
00248
00249 if(avctx->codec_tag == AV_RL32("yuv2") &&
00250 avctx->pix_fmt == AV_PIX_FMT_YUYV422) {
00251 int x, y;
00252 uint8_t *line = picture->data[0];
00253 for(y = 0; y < avctx->height; y++) {
00254 for(x = 0; x < avctx->width; x++)
00255 line[2*x + 1] ^= 0x80;
00256 line += picture->linesize[0];
00257 }
00258 }
00259 if(avctx->codec_tag == AV_RL32("YVYU") &&
00260 avctx->pix_fmt == AV_PIX_FMT_YUYV422) {
00261 int x, y;
00262 uint8_t *line = picture->data[0];
00263 for(y = 0; y < avctx->height; y++) {
00264 for(x = 0; x < avctx->width - 1; x += 2)
00265 FFSWAP(uint8_t, line[2*x + 1], line[2*x + 3]);
00266 line += picture->linesize[0];
00267 }
00268 }
00269
00270 *got_frame = 1;
00271 return buf_size;
00272 }
00273
00274 static av_cold int raw_close_decoder(AVCodecContext *avctx)
00275 {
00276 RawVideoContext *context = avctx->priv_data;
00277
00278 av_freep(&context->buffer);
00279 return 0;
00280 }
00281
00282 AVCodec ff_rawvideo_decoder = {
00283 .name = "rawvideo",
00284 .type = AVMEDIA_TYPE_VIDEO,
00285 .id = AV_CODEC_ID_RAWVIDEO,
00286 .priv_data_size = sizeof(RawVideoContext),
00287 .init = raw_init_decoder,
00288 .close = raw_close_decoder,
00289 .decode = raw_decode,
00290 .long_name = NULL_IF_CONFIG_SMALL("raw video"),
00291 .priv_class = &class,
00292 };