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), AV_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     if((avctx->bits_per_coded_sample == 4 || avctx->bits_per_coded_sample == 2) &&
00107        avctx->pix_fmt==PIX_FMT_PAL8 &&
00108        (!avctx->codec_tag || avctx->codec_tag == MKTAG('r','a','w',' '))){
00109         context->length = avpicture_get_size(avctx->pix_fmt, FFALIGN(avctx->width, 16), avctx->height);
00110         context->buffer = av_malloc(context->length);
00111         if (!context->buffer)
00112             return -1;
00113     } else {
00114         context->length = avpicture_get_size(avctx->pix_fmt, avctx->width, avctx->height);
00115     }
00116     context->pic.pict_type = AV_PICTURE_TYPE_I;
00117     context->pic.key_frame = 1;
00118 
00119     avctx->coded_frame= &context->pic;
00120 
00121     if((avctx->extradata_size >= 9 && !memcmp(avctx->extradata + avctx->extradata_size - 9, "BottomUp", 9)) ||
00122         avctx->codec_tag == MKTAG('c','y','u','v') ||
00123         avctx->codec_tag == MKTAG(3, 0, 0, 0) || avctx->codec_tag == MKTAG('W','R','A','W'))
00124         context->flip=1;
00125 
00126     return 0;
00127 }
00128 
00129 static void flip(AVCodecContext *avctx, AVPicture * picture){
00130     picture->data[0] += picture->linesize[0] * (avctx->height-1);
00131     picture->linesize[0] *= -1;
00132 }
00133 
00134 static int raw_decode(AVCodecContext *avctx,
00135                             void *data, int *data_size,
00136                             AVPacket *avpkt)
00137 {
00138     const uint8_t *buf = avpkt->data;
00139     int buf_size = avpkt->size;
00140     int linesize_align = 4;
00141     RawVideoContext *context = avctx->priv_data;
00142     int res;
00143 
00144     AVFrame   *frame   = data;
00145     AVPicture *picture = data;
00146 
00147     frame->pict_type        = avctx->coded_frame->pict_type;
00148     frame->interlaced_frame = avctx->coded_frame->interlaced_frame;
00149     frame->top_field_first = avctx->coded_frame->top_field_first;
00150     frame->reordered_opaque = avctx->reordered_opaque;
00151     frame->pkt_pts          = avctx->pkt->pts;
00152     frame->pkt_pos          = avctx->pkt->pos;
00153 
00154     if(context->tff>=0){
00155         frame->interlaced_frame = 1;
00156         frame->top_field_first  = context->tff;
00157     }
00158 
00159     if (avctx->width <= 0 || avctx->height <= 0) {
00160         av_log(avctx, AV_LOG_ERROR, "w/h is invalid\n");
00161         return AVERROR(EINVAL);
00162     }
00163 
00164     
00165     if (context->buffer) {
00166         int i;
00167         uint8_t *dst = context->buffer;
00168         buf_size = context->length - 256*4;
00169         if (avctx->bits_per_coded_sample == 4){
00170             for(i=0; 2*i+1 < buf_size && i<avpkt->size; i++){
00171                 dst[2*i+0]= buf[i]>>4;
00172                 dst[2*i+1]= buf[i]&15;
00173             }
00174             linesize_align = 8;
00175         } else {
00176             for(i=0; 4*i+3 < buf_size && i<avpkt->size; i++){
00177                 dst[4*i+0]= buf[i]>>6;
00178                 dst[4*i+1]= buf[i]>>4&3;
00179                 dst[4*i+2]= buf[i]>>2&3;
00180                 dst[4*i+3]= buf[i]   &3;
00181             }
00182             linesize_align = 16;
00183         }
00184         buf= dst;
00185     }
00186 
00187     if(avctx->codec_tag == MKTAG('A', 'V', '1', 'x') ||
00188        avctx->codec_tag == MKTAG('A', 'V', 'u', 'p'))
00189         buf += buf_size - context->length;
00190 
00191     if(buf_size < context->length - (avctx->pix_fmt==PIX_FMT_PAL8 ? 256*4 : 0))
00192         return -1;
00193 
00194     if ((res = avpicture_fill(picture, buf, avctx->pix_fmt,
00195                               avctx->width, avctx->height)) < 0)
00196         return res;
00197     if((avctx->pix_fmt==PIX_FMT_PAL8 && buf_size < context->length) ||
00198        (av_pix_fmt_descriptors[avctx->pix_fmt].flags & PIX_FMT_PSEUDOPAL)) {
00199         frame->data[1]= context->palette;
00200     }
00201     if (avctx->pix_fmt == PIX_FMT_PAL8) {
00202         const uint8_t *pal = av_packet_get_side_data(avpkt, AV_PKT_DATA_PALETTE, NULL);
00203 
00204         if (pal) {
00205             memcpy(frame->data[1], pal, AVPALETTE_SIZE);
00206             frame->palette_has_changed = 1;
00207         }
00208     }
00209     if((avctx->pix_fmt==PIX_FMT_BGR24    ||
00210         avctx->pix_fmt==PIX_FMT_GRAY8    ||
00211         avctx->pix_fmt==PIX_FMT_RGB555LE ||
00212         avctx->pix_fmt==PIX_FMT_RGB555BE ||
00213         avctx->pix_fmt==PIX_FMT_RGB565LE ||
00214         avctx->pix_fmt==PIX_FMT_MONOWHITE ||
00215         avctx->pix_fmt==PIX_FMT_PAL8) &&
00216         FFALIGN(frame->linesize[0], linesize_align)*avctx->height <= buf_size)
00217         frame->linesize[0] = FFALIGN(frame->linesize[0], linesize_align);
00218 
00219     if(context->flip)
00220         flip(avctx, picture);
00221 
00222     if (   avctx->codec_tag == MKTAG('Y', 'V', '1', '2')
00223         || avctx->codec_tag == MKTAG('Y', 'V', '1', '6')
00224         || avctx->codec_tag == MKTAG('Y', 'V', '2', '4')
00225         || avctx->codec_tag == MKTAG('Y', 'V', 'U', '9'))
00226         FFSWAP(uint8_t *, picture->data[1], picture->data[2]);
00227 
00228     if(avctx->codec_tag == AV_RL32("yuv2") &&
00229        avctx->pix_fmt   == PIX_FMT_YUYV422) {
00230         int x, y;
00231         uint8_t *line = picture->data[0];
00232         for(y = 0; y < avctx->height; y++) {
00233             for(x = 0; x < avctx->width; x++)
00234                 line[2*x + 1] ^= 0x80;
00235             line += picture->linesize[0];
00236         }
00237     }
00238 
00239     *data_size = sizeof(AVPicture);
00240     return buf_size;
00241 }
00242 
00243 static av_cold int raw_close_decoder(AVCodecContext *avctx)
00244 {
00245     RawVideoContext *context = avctx->priv_data;
00246 
00247     av_freep(&context->buffer);
00248     return 0;
00249 }
00250 
00251 AVCodec ff_rawvideo_decoder = {
00252     .name           = "rawvideo",
00253     .type           = AVMEDIA_TYPE_VIDEO,
00254     .id             = CODEC_ID_RAWVIDEO,
00255     .priv_data_size = sizeof(RawVideoContext),
00256     .init           = raw_init_decoder,
00257     .close          = raw_close_decoder,
00258     .decode         = raw_decode,
00259     .long_name      = NULL_IF_CONFIG_SMALL("raw video"),
00260     .priv_class     = &class,
00261 };