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