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 int buf_size = avpkt->size;
89 FlashSVContext *s = avctx->priv_data;
90 int h_blocks, v_blocks, h_part, v_part, i, j;
93 /* no supplementary picture */
99 init_get_bits(&gb, avpkt->data, buf_size * 8);
101 /* start to parse the bitstream */
102 s->block_width = 16 * (get_bits(&gb, 4) + 1);
103 s->image_width = get_bits(&gb, 12);
104 s->block_height = 16 * (get_bits(&gb, 4) + 1);
105 s->image_height = get_bits(&gb, 12);
107 /* calculate amount of blocks and the size of the border blocks */
108 h_blocks = s->image_width / s->block_width;
109 h_part = s->image_width % s->block_width;
110 v_blocks = s->image_height / s->block_height;
111 v_part = s->image_height % s->block_height;
113 /* the block size could change between frames, make sure the buffer
114 * is large enough, if not, get a larger one */
115 if (s->block_size < s->block_width * s->block_height) {
116 av_free(s->tmpblock);
117 if ((s->tmpblock = av_malloc(3 * s->block_width * s->block_height)) == NULL) {
118 av_log(avctx, AV_LOG_ERROR, "Can't allocate decompression buffer.\n");
119 return AVERROR(ENOMEM);
122 s->block_size = s->block_width * s->block_height;
124 /* init the image size once */
125 if (avctx->width == 0 && avctx->height == 0) {
126 avctx->width = s->image_width;
127 avctx->height = s->image_height;
130 /* check for changes of image width and image height */
131 if (avctx->width != s->image_width || avctx->height != s->image_height) {
132 av_log(avctx, AV_LOG_ERROR,
133 "Frame width or height differs from first frames!\n");
134 av_log(avctx, AV_LOG_ERROR, "fh = %d, fv %d vs ch = %d, cv = %d\n",
135 avctx->height, avctx->width, s->image_height, s->image_width);
136 return AVERROR_INVALIDDATA;
139 av_dlog(avctx, "image: %dx%d block: %dx%d num: %dx%d part: %dx%d\n",
140 s->image_width, s->image_height, s->block_width, s->block_height,
141 h_blocks, v_blocks, h_part, v_part);
143 s->frame.reference = 3;
144 s->frame.buffer_hints = FF_BUFFER_HINTS_VALID |
145 FF_BUFFER_HINTS_PRESERVE |
146 FF_BUFFER_HINTS_REUSABLE;
147 if (avctx->reget_buffer(avctx, &s->frame) < 0) {
148 av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
152 /* loop over all block columns */
153 for (j = 0; j < v_blocks + (v_part ? 1 : 0); j++) {
155 int hp = j * s->block_height; // horiz position in frame
156 int hs = (j < v_blocks) ? s->block_height : v_part; // size of block
159 /* loop over all block rows */
160 for (i = 0; i < h_blocks + (h_part ? 1 : 0); i++) {
161 int wp = i * s->block_width; // vert position in frame
162 int ws = (i < h_blocks) ? s->block_width : h_part; // size of block
164 /* get the size of the compressed zlib chunk */
165 int size = get_bits(&gb, 16);
166 if (8 * size > get_bits_left(&gb)) {
167 avctx->release_buffer(avctx, &s->frame);
168 s->frame.data[0] = NULL;
169 return AVERROR_INVALIDDATA;
172 /* skip unchanged blocks, which have size 0 */
174 /* decompress block */
175 int ret = inflateReset(&s->zstream);
177 av_log(avctx, AV_LOG_ERROR,
178 "error in decompression (reset) of block %dx%d\n", i, j);
181 s->zstream.next_in = avpkt->data + get_bits_count(&gb) / 8;
182 s->zstream.avail_in = size;
183 s->zstream.next_out = s->tmpblock;
184 s->zstream.avail_out = s->block_size * 3;
185 ret = inflate(&s->zstream, Z_FINISH);
186 if (ret == Z_DATA_ERROR) {
187 av_log(avctx, AV_LOG_ERROR, "Zlib resync occurred\n");
188 inflateSync(&s->zstream);
189 ret = inflate(&s->zstream, Z_FINISH);
192 if (ret != Z_OK && ret != Z_STREAM_END) {
193 av_log(avctx, AV_LOG_ERROR,
194 "error in decompression of block %dx%d: %d\n", i, j, ret);
197 copy_region(s->tmpblock, s->frame.data[0],
198 s->image_height - (hp + hs + 1),
199 wp, hs, ws, s->frame.linesize[0]);
200 skip_bits_long(&gb, 8 * size); /* skip the consumed bits */
205 *data_size = sizeof(AVFrame);
206 *(AVFrame*)data = s->frame;
208 if ((get_bits_count(&gb) / 8) != buf_size)
209 av_log(avctx, AV_LOG_ERROR, "buffer not fully consumed (%d != %d)\n",
210 buf_size, (get_bits_count(&gb) / 8));
212 /* report that the buffer was completely consumed */
217 static av_cold int flashsv_decode_end(AVCodecContext *avctx)
219 FlashSVContext *s = avctx->priv_data;
220 inflateEnd(&s->zstream);
221 /* release the frame if needed */
222 if (s->frame.data[0])
223 avctx->release_buffer(avctx, &s->frame);
225 /* free the tmpblock */
226 av_free(s->tmpblock);
232 AVCodec ff_flashsv_decoder = {
234 .type = AVMEDIA_TYPE_VIDEO,
235 .id = CODEC_ID_FLASHSV,
236 .priv_data_size = sizeof(FlashSVContext),
237 .init = flashsv_decode_init,
238 .close = flashsv_decode_end,
239 .decode = flashsv_decode_frame,
240 .capabilities = CODEC_CAP_DR1,
241 .pix_fmts = (const enum PixelFormat[]){PIX_FMT_BGR24, PIX_FMT_NONE},
242 .long_name = NULL_IF_CONFIG_SMALL("Flash Screen Video v1"),