• 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 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     GetByteContext g;
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     bytestream2_init(&g, avpkt->data, buf_size);
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         bfi->frame.palette_has_changed = 1;
00091     } else {
00092         bfi->frame.pict_type = AV_PICTURE_TYPE_P;
00093         bfi->frame.key_frame = 0;
00094     }
00095 
00096     bytestream2_skip(&g, 4); // Unpacked size, not required.
00097 
00098     while (dst != frame_end) {
00099         static const uint8_t lentab[4] = { 0, 2, 0, 1 };
00100         unsigned int byte   = bytestream2_get_byte(&g), av_uninit(offset);
00101         unsigned int code   = byte >> 6;
00102         unsigned int length = byte & ~0xC0;
00103 
00104         if (!bytestream2_get_bytes_left(&g)) {
00105             av_log(avctx, AV_LOG_ERROR,
00106                    "Input resolution larger than actual frame.\n");
00107             return -1;
00108         }
00109 
00110         /* Get length and offset(if required) */
00111         if (length == 0) {
00112             if (code == 1) {
00113                 length = bytestream2_get_byte(&g);
00114                 offset = bytestream2_get_le16(&g);
00115             } else {
00116                 length = bytestream2_get_le16(&g);
00117                 if (code == 2 && length == 0)
00118                     break;
00119             }
00120         } else {
00121             if (code == 1)
00122                 offset = bytestream2_get_byte(&g);
00123         }
00124 
00125         /* Do boundary check */
00126         if (dst + (length << lentab[code]) > frame_end)
00127             break;
00128 
00129         switch (code) {
00130 
00131         case 0:                //Normal Chain
00132             if (length >= bytestream2_get_bytes_left(&g)) {
00133                 av_log(avctx, AV_LOG_ERROR, "Frame larger than buffer.\n");
00134                 return -1;
00135             }
00136             bytestream2_get_buffer(&g, dst, length);
00137             dst += length;
00138             break;
00139 
00140         case 1:                //Back Chain
00141             dst_offset = dst - offset;
00142             length *= 4;        //Convert dwords to bytes.
00143             if (dst_offset < bfi->dst)
00144                 break;
00145             while (length--)
00146                 *dst++ = *dst_offset++;
00147             break;
00148 
00149         case 2:                //Skip Chain
00150             dst += length;
00151             break;
00152 
00153         case 3:                //Fill Chain
00154             colour1 = bytestream2_get_byte(&g);
00155             colour2 = bytestream2_get_byte(&g);
00156             while (length--) {
00157                 *dst++ = colour1;
00158                 *dst++ = colour2;
00159             }
00160             break;
00161 
00162         }
00163     }
00164 
00165     src = bfi->dst;
00166     dst = bfi->frame.data[0];
00167     while (height--) {
00168         memcpy(dst, src, avctx->width);
00169         src += avctx->width;
00170         dst += bfi->frame.linesize[0];
00171     }
00172     *data_size = sizeof(AVFrame);
00173     *(AVFrame *)data = bfi->frame;
00174     return buf_size;
00175 }
00176 
00177 static av_cold int bfi_decode_close(AVCodecContext * avctx)
00178 {
00179     BFIContext *bfi = avctx->priv_data;
00180     if (bfi->frame.data[0])
00181         avctx->release_buffer(avctx, &bfi->frame);
00182     av_free(bfi->dst);
00183     return 0;
00184 }
00185 
00186 AVCodec ff_bfi_decoder = {
00187     .name = "bfi",
00188     .type = AVMEDIA_TYPE_VIDEO,
00189     .id = CODEC_ID_BFI,
00190     .priv_data_size = sizeof(BFIContext),
00191     .init = bfi_decode_init,
00192     .close = bfi_decode_close,
00193     .decode = bfi_decode_frame,
00194     .capabilities = CODEC_CAP_DR1,
00195     .long_name = NULL_IF_CONFIG_SMALL("Brute Force & Ignorance"),
00196 };
Generated on Sun Apr 22 2012 21:53:59 for Libav by doxygen 1.7.1