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 "libavutil/imgutils.h"
00032
00033 typedef struct RawVideoContext {
00034 uint32_t palette[AVPALETTE_COUNT];
00035 unsigned char * buffer;
00036 int length;
00037 int flip;
00038 AVFrame pic;
00039 } RawVideoContext;
00040
00041 static const PixelFormatTag pix_fmt_bps_avi[] = {
00042 { PIX_FMT_PAL8, 4 },
00043 { PIX_FMT_PAL8, 8 },
00044 { PIX_FMT_RGB444, 12 },
00045 { PIX_FMT_RGB555, 15 },
00046 { PIX_FMT_RGB555, 16 },
00047 { PIX_FMT_BGR24, 24 },
00048 { PIX_FMT_RGB32, 32 },
00049 { PIX_FMT_NONE, 0 },
00050 };
00051
00052 static const PixelFormatTag pix_fmt_bps_mov[] = {
00053 { PIX_FMT_MONOWHITE, 1 },
00054 { PIX_FMT_PAL8, 2 },
00055 { PIX_FMT_PAL8, 4 },
00056 { PIX_FMT_PAL8, 8 },
00057
00058
00059 { PIX_FMT_RGB555BE, 16 },
00060 { PIX_FMT_RGB24, 24 },
00061 { PIX_FMT_ARGB, 32 },
00062 { PIX_FMT_NONE, 0 },
00063 };
00064
00065 static enum PixelFormat find_pix_fmt(const PixelFormatTag *tags, unsigned int fourcc)
00066 {
00067 while (tags->pix_fmt >= 0) {
00068 if (tags->fourcc == fourcc)
00069 return tags->pix_fmt;
00070 tags++;
00071 }
00072 return PIX_FMT_YUV420P;
00073 }
00074
00075 static av_cold int raw_init_decoder(AVCodecContext *avctx)
00076 {
00077 RawVideoContext *context = avctx->priv_data;
00078
00079 if (avctx->codec_tag == MKTAG('r','a','w',' '))
00080 avctx->pix_fmt = find_pix_fmt(pix_fmt_bps_mov, avctx->bits_per_coded_sample);
00081 else if (avctx->codec_tag)
00082 avctx->pix_fmt = find_pix_fmt(ff_raw_pix_fmt_tags, avctx->codec_tag);
00083 else if (avctx->pix_fmt == PIX_FMT_NONE && avctx->bits_per_coded_sample)
00084 avctx->pix_fmt = find_pix_fmt(pix_fmt_bps_avi, avctx->bits_per_coded_sample);
00085
00086 ff_set_systematic_pal2(context->palette, avctx->pix_fmt);
00087 context->length = avpicture_get_size(avctx->pix_fmt, avctx->width, avctx->height);
00088 if((avctx->bits_per_coded_sample == 4 || avctx->bits_per_coded_sample == 2) &&
00089 avctx->pix_fmt==PIX_FMT_PAL8 &&
00090 (!avctx->codec_tag || avctx->codec_tag == MKTAG('r','a','w',' '))){
00091 context->buffer = av_malloc(context->length);
00092 if (!context->buffer)
00093 return -1;
00094 }
00095 context->pic.pict_type = FF_I_TYPE;
00096 context->pic.key_frame = 1;
00097
00098 avctx->coded_frame= &context->pic;
00099
00100 if((avctx->extradata_size >= 9 && !memcmp(avctx->extradata + avctx->extradata_size - 9, "BottomUp", 9)) ||
00101 avctx->codec_tag == MKTAG( 3 , 0 , 0 , 0 ))
00102 context->flip=1;
00103
00104 return 0;
00105 }
00106
00107 static void flip(AVCodecContext *avctx, AVPicture * picture){
00108 picture->data[0] += picture->linesize[0] * (avctx->height-1);
00109 picture->linesize[0] *= -1;
00110 }
00111
00112 static int raw_decode(AVCodecContext *avctx,
00113 void *data, int *data_size,
00114 AVPacket *avpkt)
00115 {
00116 const uint8_t *buf = avpkt->data;
00117 int buf_size = avpkt->size;
00118 RawVideoContext *context = avctx->priv_data;
00119
00120 AVFrame * frame = (AVFrame *) data;
00121 AVPicture * picture = (AVPicture *) data;
00122
00123 frame->interlaced_frame = avctx->coded_frame->interlaced_frame;
00124 frame->top_field_first = avctx->coded_frame->top_field_first;
00125 frame->reordered_opaque = avctx->reordered_opaque;
00126 frame->pkt_pts = avctx->pkt->pts;
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 ff_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 };