Statistics
| Branch: | Revision:

ffmpeg / libavcodec / bfi.c @ ce5e49b0

History | View | Annotate | Download (5.94 KB)

1
/*
2
 * Brute Force & Ignorance (BFI) video decoder
3
 * Copyright (c) 2008 Sisir Koppaka
4
 *
5
 * This file is part of FFmpeg.
6
 *
7
 * FFmpeg is free software; you can redistribute it and/or
8
 * modify it under the terms of the GNU Lesser General Public
9
 * License as published by the Free Software Foundation; either
10
 * version 2.1 of the License, or (at your option) any later version.
11
 *
12
 * FFmpeg is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15
 * Lesser General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Lesser General Public
18
 * License along with FFmpeg; if not, write to the Free Software
19
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20
 */
21

    
22
/**
23
 * @file
24
 * @brief Brute Force & Ignorance (.bfi) video decoder
25
 * @author Sisir Koppaka ( sisir.koppaka at gmail dot com )
26
 * @sa http://wiki.multimedia.cx/index.php?title=BFI
27
 */
28

    
29
#include "libavutil/common.h"
30
#include "avcodec.h"
31
#include "bytestream.h"
32

    
33
typedef struct BFIContext {
34
    AVCodecContext *avctx;
35
    AVFrame frame;
36
    uint8_t *dst;
37
    uint32_t pal[256];
38
} BFIContext;
39

    
40
static av_cold int bfi_decode_init(AVCodecContext * avctx)
41
{
42
    BFIContext *bfi = avctx->priv_data;
43
    avctx->pix_fmt = PIX_FMT_PAL8;
44
    bfi->dst = av_mallocz(avctx->width * avctx->height);
45
    return 0;
46
}
47

    
48
static int bfi_decode_frame(AVCodecContext * avctx, void *data,
49
                            int *data_size, AVPacket *avpkt)
50
{
51
    const uint8_t *buf = avpkt->data, *buf_end = avpkt->data + avpkt->size;
52
    int buf_size = avpkt->size;
53
    BFIContext *bfi = avctx->priv_data;
54
    uint8_t *dst = bfi->dst;
55
    uint8_t *src, *dst_offset, colour1, colour2;
56
    uint8_t *frame_end = bfi->dst + avctx->width * avctx->height;
57
    uint32_t *pal;
58
    int i, j, height = avctx->height;
59

    
60
    if (bfi->frame.data[0])
61
        avctx->release_buffer(avctx, &bfi->frame);
62

    
63
    bfi->frame.reference = 1;
64

    
65
    if (avctx->get_buffer(avctx, &bfi->frame) < 0) {
66
        av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
67
        return -1;
68
    }
69

    
70
    /* Set frame parameters and palette, if necessary */
71
    if (!avctx->frame_number) {
72
        bfi->frame.pict_type = AV_PICTURE_TYPE_I;
73
        bfi->frame.key_frame = 1;
74
        /* Setting the palette */
75
        if(avctx->extradata_size>768) {
76
            av_log(NULL, AV_LOG_ERROR, "Palette is too large.\n");
77
            return -1;
78
        }
79
        pal = (uint32_t *) bfi->frame.data[1];
80
        for (i = 0; i < avctx->extradata_size / 3; i++) {
81
            int shift = 16;
82
            *pal = 0;
83
            for (j = 0; j < 3; j++, shift -= 8)
84
                *pal +=
85
                    ((avctx->extradata[i * 3 + j] << 2) |
86
                    (avctx->extradata[i * 3 + j] >> 4)) << shift;
87
            pal++;
88
        }
89
        memcpy(bfi->pal, bfi->frame.data[1], sizeof(bfi->pal));
90
        bfi->frame.palette_has_changed = 1;
91
    } else {
92
        bfi->frame.pict_type = AV_PICTURE_TYPE_P;
93
        bfi->frame.key_frame = 0;
94
        bfi->frame.palette_has_changed = 0;
95
        memcpy(bfi->frame.data[1], bfi->pal, sizeof(bfi->pal));
96
    }
97

    
98
    buf += 4; //Unpacked size, not required.
99

    
100
    while (dst != frame_end) {
101
        static const uint8_t lentab[4]={0,2,0,1};
102
        unsigned int byte = *buf++, av_uninit(offset);
103
        unsigned int code = byte >> 6;
104
        unsigned int length = byte & ~0xC0;
105

    
106
        if (buf >= buf_end) {
107
            av_log(avctx, AV_LOG_ERROR, "Input resolution larger than actual frame.\n");
108
            return -1;
109
        }
110

    
111
        /* Get length and offset(if required) */
112
        if (length == 0) {
113
            if (code == 1) {
114
                length = bytestream_get_byte(&buf);
115
                offset = bytestream_get_le16(&buf);
116
            } else {
117
                length = bytestream_get_le16(&buf);
118
                if (code == 2 && length == 0)
119
                    break;
120
            }
121
        } else {
122
            if (code == 1)
123
                offset = bytestream_get_byte(&buf);
124
        }
125

    
126
        /* Do boundary check */
127
        if (dst + (length<<lentab[code]) > frame_end)
128
            break;
129

    
130
        switch (code) {
131

    
132
        case 0:                //Normal Chain
133
            if (length >= buf_end - buf) {
134
                av_log(avctx, AV_LOG_ERROR, "Frame larger than buffer.\n");
135
                return -1;
136
            }
137
            bytestream_get_buffer(&buf, dst, length);
138
            dst += length;
139
            break;
140

    
141
        case 1:                //Back Chain
142
            dst_offset = dst - offset;
143
            length *= 4;        //Convert dwords to bytes.
144
            if (dst_offset < bfi->dst)
145
                break;
146
            while (length--)
147
                *dst++ = *dst_offset++;
148
            break;
149

    
150
        case 2:                //Skip Chain
151
            dst += length;
152
            break;
153

    
154
        case 3:                //Fill Chain
155
            colour1 = bytestream_get_byte(&buf);
156
            colour2 = bytestream_get_byte(&buf);
157
            while (length--) {
158
                *dst++ = colour1;
159
                *dst++ = colour2;
160
            }
161
            break;
162

    
163
        }
164
    }
165

    
166
    src = bfi->dst;
167
    dst = bfi->frame.data[0];
168
    while (height--) {
169
        memcpy(dst, src, avctx->width);
170
        src += avctx->width;
171
        dst += bfi->frame.linesize[0];
172
    }
173
    *data_size = sizeof(AVFrame);
174
    *(AVFrame *) data = bfi->frame;
175
    return buf_size;
176
}
177

    
178
static av_cold int bfi_decode_close(AVCodecContext * avctx)
179
{
180
    BFIContext *bfi = avctx->priv_data;
181
    if (bfi->frame.data[0])
182
        avctx->release_buffer(avctx, &bfi->frame);
183
    av_free(bfi->dst);
184
    return 0;
185
}
186

    
187
AVCodec ff_bfi_decoder = {
188
    .name = "bfi",
189
    .type = AVMEDIA_TYPE_VIDEO,
190
    .id = CODEC_ID_BFI,
191
    .priv_data_size = sizeof(BFIContext),
192
    .init = bfi_decode_init,
193
    .close = bfi_decode_close,
194
    .decode = bfi_decode_frame,
195
    .capabilities = CODEC_CAP_DR1,
196
    .long_name = NULL_IF_CONFIG_SMALL("Brute Force & Ignorance"),
197
};