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/intreadwrite.h"
00030
00031 typedef struct RawVideoContext {
00032 unsigned char * buffer;
00033 int length;
00034 int flip;
00035 AVFrame pic;
00036 } RawVideoContext;
00037
00038 static const PixelFormatTag pixelFormatBpsAVI[] = {
00039 { PIX_FMT_PAL8, 4 },
00040 { PIX_FMT_PAL8, 8 },
00041 { PIX_FMT_RGB555, 15 },
00042 { PIX_FMT_RGB555, 16 },
00043 { PIX_FMT_BGR24, 24 },
00044 { PIX_FMT_RGB32, 32 },
00045 { PIX_FMT_NONE, 0 },
00046 };
00047
00048 static const PixelFormatTag pixelFormatBpsMOV[] = {
00049 { PIX_FMT_MONOWHITE, 1 },
00050 { PIX_FMT_PAL8, 2 },
00051 { PIX_FMT_PAL8, 4 },
00052 { PIX_FMT_PAL8, 8 },
00053
00054
00055 { PIX_FMT_RGB555BE, 16 },
00056 { PIX_FMT_RGB24, 24 },
00057 { PIX_FMT_ARGB, 32 },
00058 { PIX_FMT_NONE, 0 },
00059 };
00060
00061 static enum PixelFormat findPixelFormat(const PixelFormatTag *tags, unsigned int fourcc)
00062 {
00063 while (tags->pix_fmt >= 0) {
00064 if (tags->fourcc == fourcc)
00065 return tags->pix_fmt;
00066 tags++;
00067 }
00068 return PIX_FMT_YUV420P;
00069 }
00070
00071 static av_cold int raw_init_decoder(AVCodecContext *avctx)
00072 {
00073 RawVideoContext *context = avctx->priv_data;
00074
00075 if (avctx->codec_tag == MKTAG('r','a','w',' '))
00076 avctx->pix_fmt = findPixelFormat(pixelFormatBpsMOV, avctx->bits_per_coded_sample);
00077 else if (avctx->codec_tag)
00078 avctx->pix_fmt = findPixelFormat(ff_raw_pixelFormatTags, avctx->codec_tag);
00079 else if (avctx->bits_per_coded_sample)
00080 avctx->pix_fmt = findPixelFormat(pixelFormatBpsAVI, avctx->bits_per_coded_sample);
00081
00082 context->length = avpicture_get_size(avctx->pix_fmt, avctx->width, avctx->height);
00083 context->buffer = av_malloc(context->length);
00084 context->pic.pict_type = FF_I_TYPE;
00085 context->pic.key_frame = 1;
00086
00087 avctx->coded_frame= &context->pic;
00088
00089 if (!context->buffer)
00090 return -1;
00091
00092 if((avctx->extradata_size >= 9 && !memcmp(avctx->extradata + avctx->extradata_size - 9, "BottomUp", 9)) ||
00093 avctx->codec_tag == MKTAG( 3 , 0 , 0 , 0 ))
00094 context->flip=1;
00095
00096 return 0;
00097 }
00098
00099 static void flip(AVCodecContext *avctx, AVPicture * picture){
00100 picture->data[0] += picture->linesize[0] * (avctx->height-1);
00101 picture->linesize[0] *= -1;
00102 }
00103
00104 static int raw_decode(AVCodecContext *avctx,
00105 void *data, int *data_size,
00106 AVPacket *avpkt)
00107 {
00108 const uint8_t *buf = avpkt->data;
00109 int buf_size = avpkt->size;
00110 RawVideoContext *context = avctx->priv_data;
00111
00112 AVFrame * frame = (AVFrame *) data;
00113 AVPicture * picture = (AVPicture *) data;
00114
00115 frame->interlaced_frame = avctx->coded_frame->interlaced_frame;
00116 frame->top_field_first = avctx->coded_frame->top_field_first;
00117
00118
00119 if((avctx->bits_per_coded_sample == 4 || avctx->bits_per_coded_sample == 2) &&
00120 avctx->pix_fmt==PIX_FMT_PAL8 &&
00121 (!avctx->codec_tag || avctx->codec_tag == MKTAG('r','a','w',' '))){
00122 int i;
00123 uint8_t *dst = context->buffer + 256*4;
00124 buf_size = context->length - 256*4;
00125 if (avctx->bits_per_coded_sample == 4){
00126 for(i=0; 2*i+1 < buf_size; i++){
00127 dst[2*i+0]= buf[i]>>4;
00128 dst[2*i+1]= buf[i]&15;
00129 }
00130 } else
00131 for(i=0; 4*i+3 < buf_size; i++){
00132 dst[4*i+0]= buf[i]>>6;
00133 dst[4*i+1]= buf[i]>>4&3;
00134 dst[4*i+2]= buf[i]>>2&3;
00135 dst[4*i+3]= buf[i] &3;
00136 }
00137 buf= dst;
00138 }
00139
00140 if(avctx->codec_tag == MKTAG('A', 'V', '1', 'x') ||
00141 avctx->codec_tag == MKTAG('A', 'V', 'u', 'p'))
00142 buf += buf_size - context->length;
00143
00144 if(buf_size < context->length - (avctx->pix_fmt==PIX_FMT_PAL8 ? 256*4 : 0))
00145 return -1;
00146
00147 avpicture_fill(picture, buf, avctx->pix_fmt, avctx->width, avctx->height);
00148 if(avctx->pix_fmt==PIX_FMT_PAL8 && buf_size < context->length){
00149 frame->data[1]= context->buffer;
00150 }
00151 if (avctx->palctrl && avctx->palctrl->palette_changed) {
00152 memcpy(frame->data[1], avctx->palctrl->palette, AVPALETTE_SIZE);
00153 avctx->palctrl->palette_changed = 0;
00154 }
00155
00156 if(context->flip)
00157 flip(avctx, picture);
00158
00159 if ( avctx->codec_tag == MKTAG('Y', 'V', '1', '2')
00160 || avctx->codec_tag == MKTAG('Y', 'V', 'U', '9'))
00161 FFSWAP(uint8_t *, picture->data[1], picture->data[2]);
00162
00163 if(avctx->codec_tag == AV_RL32("yuv2") &&
00164 avctx->pix_fmt == PIX_FMT_YUYV422) {
00165 int x, y;
00166 uint8_t *line = picture->data[0];
00167 for(y = 0; y < avctx->height; y++) {
00168 for(x = 0; x < avctx->width; x++)
00169 line[2*x + 1] ^= 0x80;
00170 line += picture->linesize[0];
00171 }
00172 }
00173
00174 *data_size = sizeof(AVPicture);
00175 return buf_size;
00176 }
00177
00178 static av_cold int raw_close_decoder(AVCodecContext *avctx)
00179 {
00180 RawVideoContext *context = avctx->priv_data;
00181
00182 av_freep(&context->buffer);
00183 return 0;
00184 }
00185
00186 AVCodec rawvideo_decoder = {
00187 "rawvideo",
00188 AVMEDIA_TYPE_VIDEO,
00189 CODEC_ID_RAWVIDEO,
00190 sizeof(RawVideoContext),
00191 raw_init_decoder,
00192 NULL,
00193 raw_close_decoder,
00194 raw_decode,
00195 .long_name = NULL_IF_CONFIG_SMALL("raw video"),
00196 };