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

Generated on Wed Apr 11 2012 07:31:32 for FFmpeg by  doxygen 1.7.1