Libav 0.7.1
libavcodec/bfi.c
Go to the documentation of this file.
00001 /*
00002  * Brute Force & Ignorance (BFI) video decoder
00003  * Copyright (c) 2008 Sisir Koppaka
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 
00029 #include "libavutil/common.h"
00030 #include "avcodec.h"
00031 #include "bytestream.h"
00032 
00033 typedef struct BFIContext {
00034     AVCodecContext *avctx;
00035     AVFrame frame;
00036     uint8_t *dst;
00037 } BFIContext;
00038 
00039 static av_cold int bfi_decode_init(AVCodecContext * avctx)
00040 {
00041     BFIContext *bfi = avctx->priv_data;
00042     avctx->pix_fmt = PIX_FMT_PAL8;
00043     bfi->dst = av_mallocz(avctx->width * avctx->height);
00044     return 0;
00045 }
00046 
00047 static int bfi_decode_frame(AVCodecContext * avctx, void *data,
00048                             int *data_size, AVPacket *avpkt)
00049 {
00050     const uint8_t *buf = avpkt->data, *buf_end = avpkt->data + avpkt->size;
00051     int buf_size = avpkt->size;
00052     BFIContext *bfi = avctx->priv_data;
00053     uint8_t *dst = bfi->dst;
00054     uint8_t *src, *dst_offset, colour1, colour2;
00055     uint8_t *frame_end = bfi->dst + avctx->width * avctx->height;
00056     uint32_t *pal;
00057     int i, j, height = avctx->height;
00058 
00059     if (bfi->frame.data[0])
00060         avctx->release_buffer(avctx, &bfi->frame);
00061 
00062     bfi->frame.reference = 1;
00063 
00064     if (avctx->get_buffer(avctx, &bfi->frame) < 0) {
00065         av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
00066         return -1;
00067     }
00068 
00069     /* Set frame parameters and palette, if necessary */
00070     if (!avctx->frame_number) {
00071         bfi->frame.pict_type = AV_PICTURE_TYPE_I;
00072         bfi->frame.key_frame = 1;
00073         /* Setting the palette */
00074         if(avctx->extradata_size>768) {
00075             av_log(NULL, AV_LOG_ERROR, "Palette is too large.\n");
00076             return -1;
00077         }
00078         pal = (uint32_t *) bfi->frame.data[1];
00079         for (i = 0; i < avctx->extradata_size / 3; i++) {
00080             int shift = 16;
00081             *pal = 0;
00082             for (j = 0; j < 3; j++, shift -= 8)
00083                 *pal +=
00084                     ((avctx->extradata[i * 3 + j] << 2) |
00085                     (avctx->extradata[i * 3 + j] >> 4)) << shift;
00086             pal++;
00087         }
00088         bfi->frame.palette_has_changed = 1;
00089     } else {
00090         bfi->frame.pict_type = AV_PICTURE_TYPE_P;
00091         bfi->frame.key_frame = 0;
00092     }
00093 
00094     buf += 4; //Unpacked size, not required.
00095 
00096     while (dst != frame_end) {
00097         static const uint8_t lentab[4]={0,2,0,1};
00098         unsigned int byte = *buf++, av_uninit(offset);
00099         unsigned int code = byte >> 6;
00100         unsigned int length = byte & ~0xC0;
00101 
00102         if (buf >= buf_end) {
00103             av_log(avctx, AV_LOG_ERROR, "Input resolution larger than actual frame.\n");
00104             return -1;
00105         }
00106 
00107         /* Get length and offset(if required) */
00108         if (length == 0) {
00109             if (code == 1) {
00110                 length = bytestream_get_byte(&buf);
00111                 offset = bytestream_get_le16(&buf);
00112             } else {
00113                 length = bytestream_get_le16(&buf);
00114                 if (code == 2 && length == 0)
00115                     break;
00116             }
00117         } else {
00118             if (code == 1)
00119                 offset = bytestream_get_byte(&buf);
00120         }
00121 
00122         /* Do boundary check */
00123         if (dst + (length<<lentab[code]) > frame_end)
00124             break;
00125 
00126         switch (code) {
00127 
00128         case 0:                //Normal Chain
00129             if (length >= buf_end - buf) {
00130                 av_log(avctx, AV_LOG_ERROR, "Frame larger than buffer.\n");
00131                 return -1;
00132             }
00133             bytestream_get_buffer(&buf, dst, length);
00134             dst += length;
00135             break;
00136 
00137         case 1:                //Back Chain
00138             dst_offset = dst - offset;
00139             length *= 4;        //Convert dwords to bytes.
00140             if (dst_offset < bfi->dst)
00141                 break;
00142             while (length--)
00143                 *dst++ = *dst_offset++;
00144             break;
00145 
00146         case 2:                //Skip Chain
00147             dst += length;
00148             break;
00149 
00150         case 3:                //Fill Chain
00151             colour1 = bytestream_get_byte(&buf);
00152             colour2 = bytestream_get_byte(&buf);
00153             while (length--) {
00154                 *dst++ = colour1;
00155                 *dst++ = colour2;
00156             }
00157             break;
00158 
00159         }
00160     }
00161 
00162     src = bfi->dst;
00163     dst = bfi->frame.data[0];
00164     while (height--) {
00165         memcpy(dst, src, avctx->width);
00166         src += avctx->width;
00167         dst += bfi->frame.linesize[0];
00168     }
00169     *data_size = sizeof(AVFrame);
00170     *(AVFrame *) data = bfi->frame;
00171     return buf_size;
00172 }
00173 
00174 static av_cold int bfi_decode_close(AVCodecContext * avctx)
00175 {
00176     BFIContext *bfi = avctx->priv_data;
00177     if (bfi->frame.data[0])
00178         avctx->release_buffer(avctx, &bfi->frame);
00179     av_free(bfi->dst);
00180     return 0;
00181 }
00182 
00183 AVCodec ff_bfi_decoder = {
00184     .name = "bfi",
00185     .type = AVMEDIA_TYPE_VIDEO,
00186     .id = CODEC_ID_BFI,
00187     .priv_data_size = sizeof(BFIContext),
00188     .init = bfi_decode_init,
00189     .close = bfi_decode_close,
00190     .decode = bfi_decode_frame,
00191     .capabilities = CODEC_CAP_DR1,
00192     .long_name = NULL_IF_CONFIG_SMALL("Brute Force & Ignorance"),
00193 };