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 FFmpeg.
00006  *
00007  * FFmpeg 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  * FFmpeg 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 FFmpeg; 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 #include "libavutil/opt.h"
00033 
00034 typedef struct RawVideoContext {
00035     AVClass *av_class;
00036     uint32_t palette[AVPALETTE_COUNT];
00037     unsigned char * buffer;  /* block of memory for holding one frame */
00038     int             length;  /* number of bytes in buffer */
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     // FIXME swscale does not support 16 bit in .mov, sample 16bit.mov
00069     // http://developer.apple.com/documentation/QuickTime/QTFF/QTFFChap3/qtff3.html
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 
00143     AVFrame * frame = (AVFrame *) data;
00144     AVPicture * picture = (AVPicture *) data;
00145 
00146     frame->pict_type        = avctx->coded_frame->pict_type;
00147     frame->interlaced_frame = avctx->coded_frame->interlaced_frame;
00148     frame->top_field_first = avctx->coded_frame->top_field_first;
00149     frame->reordered_opaque = avctx->reordered_opaque;
00150     frame->pkt_pts          = avctx->pkt->pts;
00151     frame->pkt_pos          = avctx->pkt->pos;
00152 
00153     if(context->tff>=0){
00154         frame->interlaced_frame = 1;
00155         frame->top_field_first  = context->tff;
00156     }
00157 
00158     if(buf_size < context->length - (avctx->pix_fmt==PIX_FMT_PAL8 ? 256*4 : 0))
00159         return -1;
00160 
00161     //2bpp and 4bpp raw in avi and mov (yes this is ugly ...)
00162     if (context->buffer) {
00163         int i;
00164         uint8_t *dst = context->buffer;
00165         buf_size = context->length - 256*4;
00166         if (avctx->bits_per_coded_sample == 4){
00167             for(i=0; 2*i+1 < buf_size; i++){
00168                 dst[2*i+0]= buf[i]>>4;
00169                 dst[2*i+1]= buf[i]&15;
00170             }
00171             linesize_align = 8;
00172         } else {
00173             for(i=0; 4*i+3 < buf_size; i++){
00174                 dst[4*i+0]= buf[i]>>6;
00175                 dst[4*i+1]= buf[i]>>4&3;
00176                 dst[4*i+2]= buf[i]>>2&3;
00177                 dst[4*i+3]= buf[i]   &3;
00178             }
00179             linesize_align = 16;
00180         }
00181         buf= dst;
00182     }
00183 
00184     if(avctx->codec_tag == MKTAG('A', 'V', '1', 'x') ||
00185        avctx->codec_tag == MKTAG('A', 'V', 'u', 'p'))
00186         buf += buf_size - context->length;
00187 
00188     avpicture_fill(picture, buf, avctx->pix_fmt, avctx->width, avctx->height);
00189     if((avctx->pix_fmt==PIX_FMT_PAL8 && buf_size < context->length) ||
00190        (avctx->pix_fmt!=PIX_FMT_PAL8 &&
00191         (av_pix_fmt_descriptors[avctx->pix_fmt].flags & PIX_FMT_PAL))){
00192         frame->data[1]= context->palette;
00193     }
00194     if (avctx->pix_fmt == PIX_FMT_PAL8) {
00195         const uint8_t *pal = av_packet_get_side_data(avpkt, AV_PKT_DATA_PALETTE, NULL);
00196 
00197         if (pal) {
00198             memcpy(frame->data[1], pal, AVPALETTE_SIZE);
00199             frame->palette_has_changed = 1;
00200         }
00201     }
00202     if((avctx->pix_fmt==PIX_FMT_BGR24    ||
00203         avctx->pix_fmt==PIX_FMT_GRAY8    ||
00204         avctx->pix_fmt==PIX_FMT_RGB555LE ||
00205         avctx->pix_fmt==PIX_FMT_RGB555BE ||
00206         avctx->pix_fmt==PIX_FMT_RGB565LE ||
00207         avctx->pix_fmt==PIX_FMT_MONOWHITE ||
00208         avctx->pix_fmt==PIX_FMT_PAL8) &&
00209         FFALIGN(frame->linesize[0], linesize_align)*avctx->height <= buf_size)
00210         frame->linesize[0] = FFALIGN(frame->linesize[0], linesize_align);
00211 
00212     if(context->flip)
00213         flip(avctx, picture);
00214 
00215     if (   avctx->codec_tag == MKTAG('Y', 'V', '1', '2')
00216         || avctx->codec_tag == MKTAG('Y', 'V', '1', '6')
00217         || avctx->codec_tag == MKTAG('Y', 'V', '2', '4')
00218         || avctx->codec_tag == MKTAG('Y', 'V', 'U', '9'))
00219         FFSWAP(uint8_t *, picture->data[1], picture->data[2]);
00220 
00221     if(avctx->codec_tag == AV_RL32("yuv2") &&
00222        avctx->pix_fmt   == PIX_FMT_YUYV422) {
00223         int x, y;
00224         uint8_t *line = picture->data[0];
00225         for(y = 0; y < avctx->height; y++) {
00226             for(x = 0; x < avctx->width; x++)
00227                 line[2*x + 1] ^= 0x80;
00228             line += picture->linesize[0];
00229         }
00230     }
00231 
00232     *data_size = sizeof(AVPicture);
00233     return buf_size;
00234 }
00235 
00236 static av_cold int raw_close_decoder(AVCodecContext *avctx)
00237 {
00238     RawVideoContext *context = avctx->priv_data;
00239 
00240     av_freep(&context->buffer);
00241     return 0;
00242 }
00243 
00244 AVCodec ff_rawvideo_decoder = {
00245     .name           = "rawvideo",
00246     .type           = AVMEDIA_TYPE_VIDEO,
00247     .id             = CODEC_ID_RAWVIDEO,
00248     .priv_data_size = sizeof(RawVideoContext),
00249     .init           = raw_init_decoder,
00250     .close          = raw_close_decoder,
00251     .decode         = raw_decode,
00252     .long_name = NULL_IF_CONFIG_SMALL("raw video"),
00253     .priv_class= &class,
00254 };