libavcodec/rawdec.c
Go to the documentation of this file.
00001 /*
00002  * Raw Video Decoder
00003  * Copyright (c) 2001 Fabrice Bellard
00004  *
00005  * This file is part of Libav.
00006  *
00007  * Libav is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2.1 of the License, or (at your option) any later version.
00011  *
00012  * Libav is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with Libav; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
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;  /* block of memory for holding one frame */
00036     int             length;  /* number of bytes in buffer */
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     // FIXME swscale does not support 16 bit in .mov, sample 16bit.mov
00058     // http://developer.apple.com/documentation/QuickTime/QTFF/QTFFChap3/qtff3.html
00059     { PIX_FMT_RGB555BE, 16 },
00060     { PIX_FMT_RGB24,    24 },
00061     { PIX_FMT_ARGB,     32 },
00062     { PIX_FMT_MONOWHITE,33 },
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 == MKTAG('W','R','A','W'))
00083         avctx->pix_fmt = find_pix_fmt(pix_fmt_bps_avi, avctx->bits_per_coded_sample);
00084     else if (avctx->codec_tag)
00085         avctx->pix_fmt = find_pix_fmt(ff_raw_pix_fmt_tags, avctx->codec_tag);
00086     else if (avctx->pix_fmt == PIX_FMT_NONE && avctx->bits_per_coded_sample)
00087         avctx->pix_fmt = find_pix_fmt(pix_fmt_bps_avi, avctx->bits_per_coded_sample);
00088 
00089     ff_set_systematic_pal2(context->palette, avctx->pix_fmt);
00090     context->length = avpicture_get_size(avctx->pix_fmt, avctx->width, avctx->height);
00091     if((avctx->bits_per_coded_sample == 4 || avctx->bits_per_coded_sample == 2) &&
00092        avctx->pix_fmt==PIX_FMT_PAL8 &&
00093        (!avctx->codec_tag || avctx->codec_tag == MKTAG('r','a','w',' '))){
00094         context->buffer = av_malloc(context->length);
00095         if (!context->buffer)
00096             return -1;
00097     }
00098     context->pic.pict_type = AV_PICTURE_TYPE_I;
00099     context->pic.key_frame = 1;
00100 
00101     avctx->coded_frame= &context->pic;
00102 
00103     if((avctx->extradata_size >= 9 && !memcmp(avctx->extradata + avctx->extradata_size - 9, "BottomUp", 9)) ||
00104         avctx->codec_tag == MKTAG(3, 0, 0, 0) || avctx->codec_tag == MKTAG('W','R','A','W'))
00105         context->flip=1;
00106 
00107     return 0;
00108 }
00109 
00110 static void flip(AVCodecContext *avctx, AVPicture * picture){
00111     picture->data[0] += picture->linesize[0] * (avctx->height-1);
00112     picture->linesize[0] *= -1;
00113 }
00114 
00115 static int raw_decode(AVCodecContext *avctx,
00116                             void *data, int *data_size,
00117                             AVPacket *avpkt)
00118 {
00119     const uint8_t *buf = avpkt->data;
00120     int buf_size = avpkt->size;
00121     RawVideoContext *context = avctx->priv_data;
00122 
00123     AVFrame * frame = (AVFrame *) data;
00124     AVPicture * picture = (AVPicture *) data;
00125 
00126     frame->pict_type        = avctx->coded_frame->pict_type;
00127     frame->interlaced_frame = avctx->coded_frame->interlaced_frame;
00128     frame->top_field_first = avctx->coded_frame->top_field_first;
00129     frame->reordered_opaque = avctx->reordered_opaque;
00130     frame->pkt_pts          = avctx->pkt->pts;
00131 
00132     if(buf_size < context->length - (avctx->pix_fmt==PIX_FMT_PAL8 ? 256*4 : 0))
00133         return -1;
00134 
00135     //2bpp and 4bpp raw in avi and mov (yes this is ugly ...)
00136     if (context->buffer) {
00137         int i;
00138         uint8_t *dst = context->buffer;
00139         buf_size = context->length - 256*4;
00140         if (avctx->bits_per_coded_sample == 4){
00141             for(i=0; 2*i+1 < buf_size; i++){
00142                 dst[2*i+0]= buf[i]>>4;
00143                 dst[2*i+1]= buf[i]&15;
00144             }
00145         } else
00146             for(i=0; 4*i+3 < buf_size; i++){
00147                 dst[4*i+0]= buf[i]>>6;
00148                 dst[4*i+1]= buf[i]>>4&3;
00149                 dst[4*i+2]= buf[i]>>2&3;
00150                 dst[4*i+3]= buf[i]   &3;
00151             }
00152         buf= dst;
00153     }
00154 
00155     if(avctx->codec_tag == MKTAG('A', 'V', '1', 'x') ||
00156        avctx->codec_tag == MKTAG('A', 'V', 'u', 'p'))
00157         buf += buf_size - context->length;
00158 
00159     avpicture_fill(picture, buf, avctx->pix_fmt, avctx->width, avctx->height);
00160     if((avctx->pix_fmt==PIX_FMT_PAL8 && buf_size < context->length) ||
00161        (avctx->pix_fmt!=PIX_FMT_PAL8 &&
00162         (av_pix_fmt_descriptors[avctx->pix_fmt].flags & PIX_FMT_PAL))){
00163         frame->data[1]= context->palette;
00164     }
00165     if (avctx->pix_fmt == PIX_FMT_PAL8) {
00166         const uint8_t *pal = av_packet_get_side_data(avpkt, AV_PKT_DATA_PALETTE, NULL);
00167 
00168         if (pal) {
00169             memcpy(frame->data[1], pal, AVPALETTE_SIZE);
00170             frame->palette_has_changed = 1;
00171         }
00172     }
00173     if(avctx->pix_fmt==PIX_FMT_BGR24 && ((frame->linesize[0]+3)&~3)*avctx->height <= buf_size)
00174         frame->linesize[0] = (frame->linesize[0]+3)&~3;
00175 
00176     if(context->flip)
00177         flip(avctx, picture);
00178 
00179     if (   avctx->codec_tag == MKTAG('Y', 'V', '1', '2')
00180         || avctx->codec_tag == MKTAG('Y', 'V', 'U', '9'))
00181         FFSWAP(uint8_t *, picture->data[1], picture->data[2]);
00182 
00183     if(avctx->codec_tag == AV_RL32("yuv2") &&
00184        avctx->pix_fmt   == PIX_FMT_YUYV422) {
00185         int x, y;
00186         uint8_t *line = picture->data[0];
00187         for(y = 0; y < avctx->height; y++) {
00188             for(x = 0; x < avctx->width; x++)
00189                 line[2*x + 1] ^= 0x80;
00190             line += picture->linesize[0];
00191         }
00192     }
00193 
00194     *data_size = sizeof(AVPicture);
00195     return buf_size;
00196 }
00197 
00198 static av_cold int raw_close_decoder(AVCodecContext *avctx)
00199 {
00200     RawVideoContext *context = avctx->priv_data;
00201 
00202     av_freep(&context->buffer);
00203     return 0;
00204 }
00205 
00206 AVCodec ff_rawvideo_decoder = {
00207     .name           = "rawvideo",
00208     .type           = AVMEDIA_TYPE_VIDEO,
00209     .id             = CODEC_ID_RAWVIDEO,
00210     .priv_data_size = sizeof(RawVideoContext),
00211     .init           = raw_init_decoder,
00212     .close          = raw_close_decoder,
00213     .decode         = raw_decode,
00214     .long_name = NULL_IF_CONFIG_SMALL("raw video"),
00215 };