Statistics
| Branch: | Revision:

ffmpeg / libavcodec / flashsv.c @ a14c0824

History | View | Annotate | Download (8.66 KB)

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

    
23
/**
24
 * @file
25
 * Flash Screen Video decoder
26
 * @author Alex Beregszaszi
27
 * @author Benjamin Larsson
28
 */
29

    
30
/* Bitstream description
31
 * The picture is divided into blocks that are zlib compressed.
32
 *
33
 * The decoder is fed complete frames, the frameheader contains:
34
 * 4bits of block width
35
 * 12bits of frame width
36
 * 4bits of block height
37
 * 12bits of frame height
38
 *
39
 * Directly after the header are the compressed blocks. The blocks
40
 * have their compressed size represented with 16bits in the beginnig.
41
 * If the size = 0 then the block is unchanged from the previous frame.
42
 * All blocks are decompressed until the buffer is consumed.
43
 *
44
 * Encoding ideas, a basic encoder would just use a fixed block size.
45
 * Block sizes can be multipels of 16, from 16 to 256. The blocks don't
46
 * have to be quadratic. A brute force search with a set of diffrent
47
 * block sizes should give a better result then to just use a fixed size.
48
 */
49

    
50
#include <stdio.h>
51
#include <stdlib.h>
52
#include <zlib.h>
53

    
54
#include "avcodec.h"
55
#include "get_bits.h"
56

    
57
typedef struct FlashSVContext {
58
    AVCodecContext *avctx;
59
    AVFrame         frame;
60
    int             image_width, image_height;
61
    int             block_width, block_height;
62
    uint8_t        *tmpblock;
63
    int             block_size;
64
    z_stream        zstream;
65
} FlashSVContext;
66

    
67

    
68
static void copy_region(uint8_t *sptr, uint8_t *dptr,
69
                        int dx, int dy, int h, int w, int stride)
70
{
71
    int i;
72

    
73
    for (i = dx + h; i > dx; i--) {
74
        memcpy(dptr + (i * stride) + dy * 3, sptr, w * 3);
75
        sptr += w * 3;
76
    }
77
}
78

    
79

    
80
static av_cold int flashsv_decode_init(AVCodecContext *avctx)
81
{
82
    FlashSVContext *s = avctx->priv_data;
83
    int zret; // Zlib return code
84

    
85
    s->avctx          = avctx;
86
    s->zstream.zalloc = Z_NULL;
87
    s->zstream.zfree  = Z_NULL;
88
    s->zstream.opaque = Z_NULL;
89
    zret = inflateInit(&(s->zstream));
90
    if (zret != Z_OK) {
91
        av_log(avctx, AV_LOG_ERROR, "Inflate init error: %d\n", zret);
92
        return 1;
93
    }
94
    avctx->pix_fmt = PIX_FMT_BGR24;
95
    s->frame.data[0] = NULL;
96

    
97
    return 0;
98
}
99

    
100

    
101
static int flashsv_decode_frame(AVCodecContext *avctx, void *data,
102
                                int *data_size, AVPacket *avpkt)
103
{
104
    const uint8_t *buf = avpkt->data;
105
    int buf_size       = avpkt->size;
106
    FlashSVContext *s  = avctx->priv_data;
107
    int h_blocks, v_blocks, h_part, v_part, i, j;
108
    GetBitContext gb;
109

    
110
    /* no supplementary picture */
111
    if (buf_size == 0)
112
        return 0;
113
    if (buf_size < 4)
114
        return -1;
115

    
116
    init_get_bits(&gb, buf, buf_size * 8);
117

    
118
    /* start to parse the bitstream */
119
    s->block_width  = 16 * (get_bits(&gb,  4) + 1);
120
    s->image_width  =       get_bits(&gb, 12);
121
    s->block_height = 16 * (get_bits(&gb,  4) + 1);
122
    s->image_height =       get_bits(&gb, 12);
123

    
124
    /* calculate amount of blocks and the size of the border blocks */
125
    h_blocks = s->image_width  / s->block_width;
126
    h_part   = s->image_width  % s->block_width;
127
    v_blocks = s->image_height / s->block_height;
128
    v_part   = s->image_height % s->block_height;
129

    
130
    /* the block size could change between frames, make sure the buffer
131
     * is large enough, if not, get a larger one */
132
    if (s->block_size < s->block_width * s->block_height) {
133
        av_free(s->tmpblock);
134
        if ((s->tmpblock = av_malloc(3 * s->block_width * s->block_height)) == NULL) {
135
            av_log(avctx, AV_LOG_ERROR, "Can't allocate decompression buffer.\n");
136
            return AVERROR(ENOMEM);
137
        }
138
    }
139
    s->block_size = s->block_width * s->block_height;
140

    
141
    /* init the image size once */
142
    if ((avctx->width == 0) && (avctx->height == 0)) {
143
        avctx->width  = s->image_width;
144
        avctx->height = s->image_height;
145
    }
146

    
147
    /* check for changes of image width and image height */
148
    if ((avctx->width != s->image_width) || (avctx->height != s->image_height)) {
149
        av_log(avctx, AV_LOG_ERROR, "Frame width or height differs from first frames!\n");
150
        av_log(avctx, AV_LOG_ERROR, "fh = %d, fv %d  vs  ch = %d, cv = %d\n", avctx->height,
151
        avctx->width,s->image_height, s->image_width);
152
        return -1;
153
    }
154

    
155
    av_log(avctx, AV_LOG_DEBUG, "image: %dx%d block: %dx%d num: %dx%d part: %dx%d\n",
156
           s->image_width, s->image_height, s->block_width, s->block_height,
157
           h_blocks, v_blocks, h_part, v_part);
158

    
159
    s->frame.reference    = 1;
160
    s->frame.buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_PRESERVE | FF_BUFFER_HINTS_REUSABLE;
161
    if (avctx->reget_buffer(avctx, &s->frame) < 0) {
162
        av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
163
        return -1;
164
    }
165

    
166
    /* loop over all block columns */
167
    for (j = 0; j < v_blocks + (v_part ? 1 : 0); j++) {
168

    
169
        int hp = j * s->block_height; // horiz position in frame
170
        int hs = (j < v_blocks) ? s->block_height : v_part; // size of block
171

    
172

    
173
        /* loop over all block rows */
174
        for (i = 0; i < h_blocks + (h_part ? 1 : 0); i++) {
175
            int wp = i * s->block_width; // vert position in frame
176
            int ws = (i < h_blocks) ? s->block_width : h_part; // size of block
177

    
178
            /* get the size of the compressed zlib chunk */
179
            int size = get_bits(&gb, 16);
180
            if (8 * size > get_bits_left(&gb)) {
181
                avctx->release_buffer(avctx, &s->frame);
182
                s->frame.data[0] = NULL;
183
                return -1;
184
            }
185

    
186
            if (size == 0) {
187
                /* no change, don't do anything */
188
            } else {
189
                /* decompress block */
190
                int ret = inflateReset(&(s->zstream));
191
                if (ret != Z_OK) {
192
                    av_log(avctx, AV_LOG_ERROR, "error in decompression (reset) of block %dx%d\n", i, j);
193
                    /* return -1; */
194
                }
195
                s->zstream.next_in   = buf + (get_bits_count(&gb) / 8);
196
                s->zstream.avail_in  = size;
197
                s->zstream.next_out  = s->tmpblock;
198
                s->zstream.avail_out = s->block_size*3;
199
                ret = inflate(&(s->zstream), Z_FINISH);
200
                if (ret == Z_DATA_ERROR) {
201
                    av_log(avctx, AV_LOG_ERROR, "Zlib resync occurred\n");
202
                    inflateSync(&(s->zstream));
203
                    ret = inflate(&(s->zstream), Z_FINISH);
204
                }
205

    
206
                if ((ret != Z_OK) && (ret != Z_STREAM_END)) {
207
                    av_log(avctx, AV_LOG_ERROR, "error in decompression of block %dx%d: %d\n", i, j, ret);
208
                    /* return -1; */
209
                }
210
                copy_region(s->tmpblock, s->frame.data[0], s->image_height - (hp + hs + 1),
211
                            wp, hs, ws, s->frame.linesize[0]);
212
                skip_bits_long(&gb, 8 * size);   /* skip the consumed bits */
213
            }
214
        }
215
    }
216

    
217
    *data_size = sizeof(AVFrame);
218
    *(AVFrame*)data = s->frame;
219

    
220
    if ((get_bits_count(&gb) / 8) != buf_size)
221
        av_log(avctx, AV_LOG_ERROR, "buffer not fully consumed (%d != %d)\n",
222
               buf_size, (get_bits_count(&gb) / 8));
223

    
224
    /* report that the buffer was completely consumed */
225
    return buf_size;
226
}
227

    
228

    
229
static av_cold int flashsv_decode_end(AVCodecContext *avctx)
230
{
231
    FlashSVContext *s = avctx->priv_data;
232
    inflateEnd(&(s->zstream));
233
    /* release the frame if needed */
234
    if (s->frame.data[0])
235
        avctx->release_buffer(avctx, &s->frame);
236

    
237
    /* free the tmpblock */
238
    av_free(s->tmpblock);
239

    
240
    return 0;
241
}
242

    
243

    
244
AVCodec ff_flashsv_decoder = {
245
    .name           = "flashsv",
246
    .type           = AVMEDIA_TYPE_VIDEO,
247
    .id             = CODEC_ID_FLASHSV,
248
    .priv_data_size = sizeof(FlashSVContext),
249
    .init           = flashsv_decode_init,
250
    .close          = flashsv_decode_end,
251
    .decode         = flashsv_decode_frame,
252
    .capabilities   = CODEC_CAP_DR1,
253
    .pix_fmts       = (const enum PixelFormat[]){PIX_FMT_BGR24, PIX_FMT_NONE},
254
    .long_name      = NULL_IF_CONFIG_SMALL("Flash Screen Video v1"),
255
};