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 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 
00022 /**
00023  * @file aasc.c
00024  * Autodesk RLE Video Decoder by Konstantin Shishkov
00025  */
00026 
00027 #include <stdio.h>
00028 #include <stdlib.h>
00029 #include <string.h>
00030 
00031 #include "avcodec.h"
00032 #include "dsputil.h"
00033 
00034 typedef struct AascContext {
00035     AVCodecContext *avctx;
00036     AVFrame frame;
00037 } AascContext;
00038 
00039 #define FETCH_NEXT_STREAM_BYTE() \
00040     if (stream_ptr >= buf_size) \
00041     { \
00042       av_log(s->avctx, AV_LOG_ERROR, " AASC: stream ptr just went out of bounds (fetch)\n"); \
00043       break; \
00044     } \
00045     stream_byte = buf[stream_ptr++];
00046 
00047 static av_cold int aasc_decode_init(AVCodecContext *avctx)
00048 {
00049     AascContext *s = avctx->priv_data;
00050 
00051     s->avctx = avctx;
00052 
00053     avctx->pix_fmt = PIX_FMT_BGR24;
00054     s->frame.data[0] = NULL;
00055 
00056     return 0;
00057 }
00058 
00059 static int aasc_decode_frame(AVCodecContext *avctx,
00060                               void *data, int *data_size,
00061                               const uint8_t *buf, int buf_size)
00062 {
00063     AascContext *s = avctx->priv_data;
00064     int stream_ptr = 4;
00065     unsigned char rle_code;
00066     unsigned char stream_byte;
00067     int pixel_ptr = 0;
00068     int row_dec, row_ptr;
00069     int frame_size;
00070     int i;
00071 
00072     s->frame.reference = 1;
00073     s->frame.buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_PRESERVE | FF_BUFFER_HINTS_REUSABLE;
00074     if (avctx->reget_buffer(avctx, &s->frame)) {
00075         av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
00076         return -1;
00077     }
00078 
00079     row_dec = s->frame.linesize[0];
00080     row_ptr = (s->avctx->height - 1) * row_dec;
00081     frame_size = row_dec * s->avctx->height;
00082 
00083     while (row_ptr >= 0) {
00084         FETCH_NEXT_STREAM_BYTE();
00085         rle_code = stream_byte;
00086         if (rle_code == 0) {
00087             /* fetch the next byte to see how to handle escape code */
00088             FETCH_NEXT_STREAM_BYTE();
00089             if (stream_byte == 0) {
00090                 /* line is done, goto the next one */
00091                 row_ptr -= row_dec;
00092                 pixel_ptr = 0;
00093             } else if (stream_byte == 1) {
00094                 /* decode is done */
00095                 break;
00096             } else if (stream_byte == 2) {
00097                 /* reposition frame decode coordinates */
00098                 FETCH_NEXT_STREAM_BYTE();
00099                 pixel_ptr += stream_byte;
00100                 FETCH_NEXT_STREAM_BYTE();
00101                 row_ptr -= stream_byte * row_dec;
00102             } else {
00103                 /* copy pixels from encoded stream */
00104                 if ((pixel_ptr + stream_byte > avctx->width * 3) ||
00105                     (row_ptr < 0)) {
00106                     av_log(s->avctx, AV_LOG_ERROR, " AASC: frame ptr just went out of bounds (copy1)\n");
00107                     break;
00108                 }
00109 
00110                 rle_code = stream_byte;
00111                 if (stream_ptr + rle_code > buf_size) {
00112                     av_log(s->avctx, AV_LOG_ERROR, " AASC: stream ptr just went out of bounds (copy2)\n");
00113                     break;
00114                 }
00115 
00116                 for (i = 0; i < rle_code; i++) {
00117                     FETCH_NEXT_STREAM_BYTE();
00118                     s->frame.data[0][row_ptr + pixel_ptr] = stream_byte;
00119                     pixel_ptr++;
00120                 }
00121                 if (rle_code & 1)
00122                     stream_ptr++;
00123             }
00124         } else {
00125             /* decode a run of data */
00126             if ((pixel_ptr + rle_code > avctx->width * 3) ||
00127                 (row_ptr < 0)) {
00128                 av_log(s->avctx, AV_LOG_ERROR, " AASC: frame ptr just went out of bounds (run1)\n");
00129                 break;
00130             }
00131 
00132             FETCH_NEXT_STREAM_BYTE();
00133 
00134             while(rle_code--) {
00135                 s->frame.data[0][row_ptr + pixel_ptr] = stream_byte;
00136                 pixel_ptr++;
00137             }
00138         }
00139     }
00140 
00141     /* one last sanity check on the way out */
00142     if (stream_ptr < buf_size)
00143         av_log(s->avctx, AV_LOG_ERROR, " AASC: ended frame decode with bytes left over (%d < %d)\n",
00144             stream_ptr, buf_size);
00145 
00146     *data_size = sizeof(AVFrame);
00147     *(AVFrame*)data = s->frame;
00148 
00149     /* report that the buffer was completely consumed */
00150     return buf_size;
00151 }
00152 
00153 static av_cold int aasc_decode_end(AVCodecContext *avctx)
00154 {
00155     AascContext *s = avctx->priv_data;
00156 
00157     /* release the last frame */
00158     if (s->frame.data[0])
00159         avctx->release_buffer(avctx, &s->frame);
00160 
00161     return 0;
00162 }
00163 
00164 AVCodec aasc_decoder = {
00165     "aasc",
00166     CODEC_TYPE_VIDEO,
00167     CODEC_ID_AASC,
00168     sizeof(AascContext),
00169     aasc_decode_init,
00170     NULL,
00171     aasc_decode_end,
00172     aasc_decode_frame,
00173     CODEC_CAP_DR1,
00174     .long_name = NULL_IF_CONFIG_SMALL("Autodesk RLE"),
00175 };

Generated on Sat Oct 11 19:44:26 2008 for libextractor by  doxygen 1.5.1