Libav 0.7.1
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_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 == MKTAG('W','R','A','W'))
00082         avctx->pix_fmt = find_pix_fmt(pix_fmt_bps_avi, avctx->bits_per_coded_sample);
00083     else if (avctx->codec_tag)
00084         avctx->pix_fmt = find_pix_fmt(ff_raw_pix_fmt_tags, avctx->codec_tag);
00085     else if (avctx->pix_fmt == PIX_FMT_NONE && avctx->bits_per_coded_sample)
00086         avctx->pix_fmt = find_pix_fmt(pix_fmt_bps_avi, avctx->bits_per_coded_sample);
00087 
00088     ff_set_systematic_pal2(context->palette, avctx->pix_fmt);
00089     context->length = avpicture_get_size(avctx->pix_fmt, avctx->width, avctx->height);
00090     if((avctx->bits_per_coded_sample == 4 || avctx->bits_per_coded_sample == 2) &&
00091        avctx->pix_fmt==PIX_FMT_PAL8 &&
00092        (!avctx->codec_tag || avctx->codec_tag == MKTAG('r','a','w',' '))){
00093         context->buffer = av_malloc(context->length);
00094         if (!context->buffer)
00095             return -1;
00096     }
00097     context->pic.pict_type = AV_PICTURE_TYPE_I;
00098     context->pic.key_frame = 1;
00099 
00100     avctx->coded_frame= &context->pic;
00101 
00102     if((avctx->extradata_size >= 9 && !memcmp(avctx->extradata + avctx->extradata_size - 9, "BottomUp", 9)) ||
00103         avctx->codec_tag == MKTAG(3, 0, 0, 0) || avctx->codec_tag == MKTAG('W','R','A','W'))
00104         context->flip=1;
00105 
00106     return 0;
00107 }
00108 
00109 static void flip(AVCodecContext *avctx, AVPicture * picture){
00110     picture->data[0] += picture->linesize[0] * (avctx->height-1);
00111     picture->linesize[0] *= -1;
00112 }
00113 
00114 static int raw_decode(AVCodecContext *avctx,
00115                             void *data, int *data_size,
00116                             AVPacket *avpkt)
00117 {
00118     const uint8_t *buf = avpkt->data;
00119     int buf_size = avpkt->size;
00120     RawVideoContext *context = avctx->priv_data;
00121 
00122     AVFrame * frame = (AVFrame *) data;
00123     AVPicture * picture = (AVPicture *) data;
00124 
00125     frame->interlaced_frame = avctx->coded_frame->interlaced_frame;
00126     frame->top_field_first = avctx->coded_frame->top_field_first;
00127     frame->reordered_opaque = avctx->reordered_opaque;
00128     frame->pkt_pts          = avctx->pkt->pts;
00129 
00130     if(buf_size < context->length - (avctx->pix_fmt==PIX_FMT_PAL8 ? 256*4 : 0))
00131         return -1;
00132 
00133     //2bpp and 4bpp raw in avi and mov (yes this is ugly ...)
00134     if (context->buffer) {
00135         int i;
00136         uint8_t *dst = context->buffer;
00137         buf_size = context->length - 256*4;
00138         if (avctx->bits_per_coded_sample == 4){
00139             for(i=0; 2*i+1 < buf_size; i++){
00140                 dst[2*i+0]= buf[i]>>4;
00141                 dst[2*i+1]= buf[i]&15;
00142             }
00143         } else
00144             for(i=0; 4*i+3 < buf_size; i++){
00145                 dst[4*i+0]= buf[i]>>6;
00146                 dst[4*i+1]= buf[i]>>4&3;
00147                 dst[4*i+2]= buf[i]>>2&3;
00148                 dst[4*i+3]= buf[i]   &3;
00149             }
00150         buf= dst;
00151     }
00152 
00153     if(avctx->codec_tag == MKTAG('A', 'V', '1', 'x') ||
00154        avctx->codec_tag == MKTAG('A', 'V', 'u', 'p'))
00155         buf += buf_size - context->length;
00156 
00157     avpicture_fill(picture, buf, avctx->pix_fmt, avctx->width, avctx->height);
00158     if((avctx->pix_fmt==PIX_FMT_PAL8 && buf_size < context->length) ||
00159        (avctx->pix_fmt!=PIX_FMT_PAL8 &&
00160         (av_pix_fmt_descriptors[avctx->pix_fmt].flags & PIX_FMT_PAL))){
00161         frame->data[1]= context->palette;
00162     }
00163     if (avctx->pix_fmt == PIX_FMT_PAL8) {
00164         const uint8_t *pal = av_packet_get_side_data(avpkt, AV_PKT_DATA_PALETTE, NULL);
00165 
00166         if (pal) {
00167             memcpy(frame->data[1], pal, AVPALETTE_SIZE);
00168             frame->palette_has_changed = 1;
00169         }
00170     }
00171     if(avctx->pix_fmt==PIX_FMT_BGR24 && ((frame->linesize[0]+3)&~3)*avctx->height <= buf_size)
00172         frame->linesize[0] = (frame->linesize[0]+3)&~3;
00173 
00174     if(context->flip)
00175         flip(avctx, picture);
00176 
00177     if (   avctx->codec_tag == MKTAG('Y', 'V', '1', '2')
00178         || avctx->codec_tag == MKTAG('Y', 'V', 'U', '9'))
00179         FFSWAP(uint8_t *, picture->data[1], picture->data[2]);
00180 
00181     if(avctx->codec_tag == AV_RL32("yuv2") &&
00182        avctx->pix_fmt   == PIX_FMT_YUYV422) {
00183         int x, y;
00184         uint8_t *line = picture->data[0];
00185         for(y = 0; y < avctx->height; y++) {
00186             for(x = 0; x < avctx->width; x++)
00187                 line[2*x + 1] ^= 0x80;
00188             line += picture->linesize[0];
00189         }
00190     }
00191 
00192     *data_size = sizeof(AVPicture);
00193     return buf_size;
00194 }
00195 
00196 static av_cold int raw_close_decoder(AVCodecContext *avctx)
00197 {
00198     RawVideoContext *context = avctx->priv_data;
00199 
00200     av_freep(&context->buffer);
00201     return 0;
00202 }
00203 
00204 AVCodec ff_rawvideo_decoder = {
00205     "rawvideo",
00206     AVMEDIA_TYPE_VIDEO,
00207     CODEC_ID_RAWVIDEO,
00208     sizeof(RawVideoContext),
00209     raw_init_decoder,
00210     NULL,
00211     raw_close_decoder,
00212     raw_decode,
00213     .long_name = NULL_IF_CONFIG_SMALL("raw video"),
00214 };