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

Generated on Tue Nov 4 2014 12:59:21 for ffmpeg by  doxygen 1.7.1