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