• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • File List
  • Globals

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 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 
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;
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 = FF_I_TYPE;
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 = FF_P_TYPE;
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         /* Get length and offset(if required) */
00103         if (length == 0) {
00104             if (code == 1) {
00105                 length = bytestream_get_byte(&buf);
00106                 offset = bytestream_get_le16(&buf);
00107             } else {
00108                 length = bytestream_get_le16(&buf);
00109                 if (code == 2 && length == 0)
00110                     break;
00111             }
00112         } else {
00113             if (code == 1)
00114                 offset = bytestream_get_byte(&buf);
00115         }
00116 
00117         /* Do boundary check */
00118         if (dst + (length<<lentab[code]) > frame_end)
00119             break;
00120 
00121         switch (code) {
00122 
00123         case 0:                //Normal Chain
00124             bytestream_get_buffer(&buf, dst, length);
00125             dst += length;
00126             break;
00127 
00128         case 1:                //Back Chain
00129             dst_offset = dst - offset;
00130             length *= 4;        //Convert dwords to bytes.
00131             if (dst_offset < bfi->dst)
00132                 break;
00133             while (length--)
00134                 *dst++ = *dst_offset++;
00135             break;
00136 
00137         case 2:                //Skip Chain
00138             dst += length;
00139             break;
00140 
00141         case 3:                //Fill Chain
00142             colour1 = bytestream_get_byte(&buf);
00143             colour2 = bytestream_get_byte(&buf);
00144             while (length--) {
00145                 *dst++ = colour1;
00146                 *dst++ = colour2;
00147             }
00148             break;
00149 
00150         }
00151     }
00152 
00153     src = bfi->dst;
00154     dst = bfi->frame.data[0];
00155     while (height--) {
00156         memcpy(dst, src, avctx->width);
00157         src += avctx->width;
00158         dst += bfi->frame.linesize[0];
00159     }
00160     *data_size = sizeof(AVFrame);
00161     *(AVFrame *) data = bfi->frame;
00162     return buf_size;
00163 }
00164 
00165 static av_cold int bfi_decode_close(AVCodecContext * avctx)
00166 {
00167     BFIContext *bfi = avctx->priv_data;
00168     if (bfi->frame.data[0])
00169         avctx->release_buffer(avctx, &bfi->frame);
00170     av_free(bfi->dst);
00171     return 0;
00172 }
00173 
00174 AVCodec bfi_decoder = {
00175     .name = "bfi",
00176     .type = AVMEDIA_TYPE_VIDEO,
00177     .id = CODEC_ID_BFI,
00178     .priv_data_size = sizeof(BFIContext),
00179     .init = bfi_decode_init,
00180     .close = bfi_decode_close,
00181     .decode = bfi_decode_frame,
00182     .capabilities = CODEC_CAP_DR1,
00183     .long_name = NULL_IF_CONFIG_SMALL("Brute Force & Ignorance"),
00184 };

Generated on Fri Sep 16 2011 17:17:34 for FFmpeg by  doxygen 1.7.1