2 * Flash Screen Video decoder
3 * Copyright (C) 2004 Alex Beregszaszi
4 * Copyright (C) 2006 Benjamin Larsson
6 * This file is part of Libav.
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.
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.
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
25 * Flash Screen Video decoder
26 * @author Alex Beregszaszi
27 * @author Benjamin Larsson
29 * A description of the bitstream format for Flash Screen Video version 1/2
30 * is part of the SWF File Format Specification (version 10), which can be
31 * downloaded from http://www.adobe.com/devnet/swf.html.
41 typedef struct FlashSVContext {
42 AVCodecContext *avctx;
44 int image_width, image_height;
45 int block_width, block_height;
52 static void copy_region(uint8_t *sptr, uint8_t *dptr,
53 int dx, int dy, int h, int w, int stride)
57 for (i = dx + h; i > dx; i--) {
58 memcpy(dptr + i * stride + dy * 3, sptr, w * 3);
64 static av_cold int flashsv_decode_init(AVCodecContext *avctx)
66 FlashSVContext *s = avctx->priv_data;
67 int zret; // Zlib return code
70 s->zstream.zalloc = Z_NULL;
71 s->zstream.zfree = Z_NULL;
72 s->zstream.opaque = Z_NULL;
73 zret = inflateInit(&s->zstream);
75 av_log(avctx, AV_LOG_ERROR, "Inflate init error: %d\n", zret);
78 avctx->pix_fmt = PIX_FMT_BGR24;
79 s->frame.data[0] = NULL;
85 static int flashsv_decode_frame(AVCodecContext *avctx, void *data,
86 int *data_size, AVPacket *avpkt)
88 const uint8_t *buf = avpkt->data;
89 int buf_size = avpkt->size;
90 FlashSVContext *s = avctx->priv_data;
91 int h_blocks, v_blocks, h_part, v_part, i, j;
94 /* no supplementary picture */
100 init_get_bits(&gb, buf, buf_size * 8);
102 /* start to parse the bitstream */
103 s->block_width = 16 * (get_bits(&gb, 4) + 1);
104 s->image_width = get_bits(&gb, 12);
105 s->block_height = 16 * (get_bits(&gb, 4) + 1);
106 s->image_height = get_bits(&gb, 12);
108 /* calculate amount of blocks and the size of the border blocks */
109 h_blocks = s->image_width / s->block_width;
110 h_part = s->image_width % s->block_width;
111 v_blocks = s->image_height / s->block_height;
112 v_part = s->image_height % s->block_height;
114 /* the block size could change between frames, make sure the buffer
115 * is large enough, if not, get a larger one */
116 if (s->block_size < s->block_width * s->block_height) {
117 av_free(s->tmpblock);
118 if ((s->tmpblock = av_malloc(3 * s->block_width * s->block_height)) == NULL) {
119 av_log(avctx, AV_LOG_ERROR, "Can't allocate decompression buffer.\n");
120 return AVERROR(ENOMEM);
123 s->block_size = s->block_width * s->block_height;
125 /* init the image size once */
126 if (avctx->width == 0 && avctx->height == 0) {
127 avctx->width = s->image_width;
128 avctx->height = s->image_height;
131 /* check for changes of image width and image height */
132 if (avctx->width != s->image_width || avctx->height != s->image_height) {
133 av_log(avctx, AV_LOG_ERROR,
134 "Frame width or height differs from first frames!\n");
135 av_log(avctx, AV_LOG_ERROR, "fh = %d, fv %d vs ch = %d, cv = %d\n",
136 avctx->height, avctx->width, s->image_height, s->image_width);
137 return AVERROR_INVALIDDATA;
140 av_dlog(avctx, "image: %dx%d block: %dx%d num: %dx%d part: %dx%d\n",
141 s->image_width, s->image_height, s->block_width, s->block_height,
142 h_blocks, v_blocks, h_part, v_part);
144 s->frame.reference = 3;
145 s->frame.buffer_hints = FF_BUFFER_HINTS_VALID |
146 FF_BUFFER_HINTS_PRESERVE |
147 FF_BUFFER_HINTS_REUSABLE;
148 if (avctx->reget_buffer(avctx, &s->frame) < 0) {
149 av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
153 /* loop over all block columns */
154 for (j = 0; j < v_blocks + (v_part ? 1 : 0); j++) {
156 int hp = j * s->block_height; // horiz position in frame
157 int hs = (j < v_blocks) ? s->block_height : v_part; // size of block
160 /* loop over all block rows */
161 for (i = 0; i < h_blocks + (h_part ? 1 : 0); i++) {
162 int wp = i * s->block_width; // vert position in frame
163 int ws = (i < h_blocks) ? s->block_width : h_part; // size of block
165 /* get the size of the compressed zlib chunk */
166 int size = get_bits(&gb, 16);
167 if (8 * size > get_bits_left(&gb)) {
168 avctx->release_buffer(avctx, &s->frame);
169 s->frame.data[0] = NULL;
170 return AVERROR_INVALIDDATA;
173 /* skip unchanged blocks, which have size 0 */
175 /* decompress block */
176 int ret = inflateReset(&s->zstream);
178 av_log(avctx, AV_LOG_ERROR,
179 "error in decompression (reset) of block %dx%d\n", i, j);
182 s->zstream.next_in = buf + get_bits_count(&gb) / 8;
183 s->zstream.avail_in = size;
184 s->zstream.next_out = s->tmpblock;
185 s->zstream.avail_out = s->block_size * 3;
186 ret = inflate(&s->zstream, Z_FINISH);
187 if (ret == Z_DATA_ERROR) {
188 av_log(avctx, AV_LOG_ERROR, "Zlib resync occurred\n");
189 inflateSync(&s->zstream);
190 ret = inflate(&s->zstream, Z_FINISH);
193 if (ret != Z_OK && ret != Z_STREAM_END) {
194 av_log(avctx, AV_LOG_ERROR,
195 "error in decompression of block %dx%d: %d\n", i, j, ret);
198 copy_region(s->tmpblock, s->frame.data[0],
199 s->image_height - (hp + hs + 1),
200 wp, hs, ws, s->frame.linesize[0]);
201 skip_bits_long(&gb, 8 * size); /* skip the consumed bits */
206 *data_size = sizeof(AVFrame);
207 *(AVFrame*)data = s->frame;
209 if ((get_bits_count(&gb) / 8) != buf_size)
210 av_log(avctx, AV_LOG_ERROR, "buffer not fully consumed (%d != %d)\n",
211 buf_size, (get_bits_count(&gb) / 8));
213 /* report that the buffer was completely consumed */
218 static av_cold int flashsv_decode_end(AVCodecContext *avctx)
220 FlashSVContext *s = avctx->priv_data;
221 inflateEnd(&s->zstream);
222 /* release the frame if needed */
223 if (s->frame.data[0])
224 avctx->release_buffer(avctx, &s->frame);
226 /* free the tmpblock */
227 av_free(s->tmpblock);
233 AVCodec ff_flashsv_decoder = {
235 .type = AVMEDIA_TYPE_VIDEO,
236 .id = CODEC_ID_FLASHSV,
237 .priv_data_size = sizeof(FlashSVContext),
238 .init = flashsv_decode_init,
239 .close = flashsv_decode_end,
240 .decode = flashsv_decode_frame,
241 .capabilities = CODEC_CAP_DR1,
242 .pix_fmts = (const enum PixelFormat[]){PIX_FMT_BGR24, PIX_FMT_NONE},
243 .long_name = NULL_IF_CONFIG_SMALL("Flash Screen Video v1"),