libavcodec/aasc.c
Go to the documentation of this file.
00001 /*
00002  * Autodesk RLE Decoder
00003  * Copyright (C) 2005 the ffmpeg project
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 <stdio.h>
00028 #include <stdlib.h>
00029 #include <string.h>
00030 
00031 #include "avcodec.h"
00032 #include "dsputil.h"
00033 #include "msrledec.h"
00034 
00035 typedef struct AascContext {
00036     AVCodecContext *avctx;
00037     GetByteContext gb;
00038     AVFrame frame;
00039 } AascContext;
00040 
00041 static av_cold int aasc_decode_init(AVCodecContext *avctx)
00042 {
00043     AascContext *s = avctx->priv_data;
00044 
00045     s->avctx = avctx;
00046 
00047     avctx->pix_fmt = PIX_FMT_BGR24;
00048 
00049     return 0;
00050 }
00051 
00052 static int aasc_decode_frame(AVCodecContext *avctx,
00053                               void *data, int *data_size,
00054                               AVPacket *avpkt)
00055 {
00056     const uint8_t *buf = avpkt->data;
00057     int buf_size = avpkt->size;
00058     AascContext *s = avctx->priv_data;
00059     int compr, i, stride;
00060 
00061     s->frame.reference = 1;
00062     s->frame.buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_PRESERVE | FF_BUFFER_HINTS_REUSABLE;
00063     if (avctx->reget_buffer(avctx, &s->frame)) {
00064         av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
00065         return -1;
00066     }
00067 
00068     compr = AV_RL32(buf);
00069     buf += 4;
00070     buf_size -= 4;
00071     switch(compr){
00072     case 0:
00073         stride = (avctx->width * 3 + 3) & ~3;
00074         for(i = avctx->height - 1; i >= 0; i--){
00075             memcpy(s->frame.data[0] + i*s->frame.linesize[0], buf, avctx->width*3);
00076             buf += stride;
00077         }
00078         break;
00079     case 1:
00080         bytestream2_init(&s->gb, buf - 4, buf_size + 4);
00081         ff_msrle_decode(avctx, (AVPicture*)&s->frame, 8, &s->gb);
00082         break;
00083     default:
00084         av_log(avctx, AV_LOG_ERROR, "Unknown compression type %d\n", compr);
00085         return -1;
00086     }
00087 
00088     *data_size = sizeof(AVFrame);
00089     *(AVFrame*)data = s->frame;
00090 
00091     /* report that the buffer was completely consumed */
00092     return buf_size;
00093 }
00094 
00095 static av_cold int aasc_decode_end(AVCodecContext *avctx)
00096 {
00097     AascContext *s = avctx->priv_data;
00098 
00099     /* release the last frame */
00100     if (s->frame.data[0])
00101         avctx->release_buffer(avctx, &s->frame);
00102 
00103     return 0;
00104 }
00105 
00106 AVCodec ff_aasc_decoder = {
00107     .name           = "aasc",
00108     .type           = AVMEDIA_TYPE_VIDEO,
00109     .id             = CODEC_ID_AASC,
00110     .priv_data_size = sizeof(AascContext),
00111     .init           = aasc_decode_init,
00112     .close          = aasc_decode_end,
00113     .decode         = aasc_decode_frame,
00114     .capabilities   = CODEC_CAP_DR1,
00115     .long_name = NULL_IF_CONFIG_SMALL("Autodesk RLE"),
00116 };