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 av_cold int flashsv_decode_init(AVCodecContext *avctx)
54 FlashSVContext *s = avctx->priv_data;
55 int zret; // Zlib return code
58 s->zstream.zalloc = Z_NULL;
59 s->zstream.zfree = Z_NULL;
60 s->zstream.opaque = Z_NULL;
61 zret = inflateInit(&s->zstream);
63 av_log(avctx, AV_LOG_ERROR, "Inflate init error: %d\n", zret);
66 avctx->pix_fmt = PIX_FMT_BGR24;
67 s->frame.data[0] = NULL;
73 static int flashsv_decode_block(AVCodecContext *avctx, AVPacket *avpkt,
74 GetBitContext *gb, int block_size,
75 int width, int height, int x_pos, int y_pos)
77 struct FlashSVContext *s = avctx->priv_data;
78 uint8_t *line = s->tmpblock;
80 int ret = inflateReset(&s->zstream);
84 s->zstream.next_in = avpkt->data + get_bits_count(gb) / 8;
85 s->zstream.avail_in = block_size;
86 s->zstream.next_out = s->tmpblock;
87 s->zstream.avail_out = s->block_size * 3;
88 ret = inflate(&s->zstream, Z_FINISH);
89 if (ret == Z_DATA_ERROR) {
90 av_log(avctx, AV_LOG_ERROR, "Zlib resync occurred\n");
91 inflateSync(&s->zstream);
92 ret = inflate(&s->zstream, Z_FINISH);
95 if (ret != Z_OK && ret != Z_STREAM_END) {
98 /* Flash Screen Video stores the image upside down, so copy
99 * lines to destination in reverse order. */
100 for (k = 1; k <= height; k++) {
101 memcpy(s->frame.data[0] + x_pos * 3 +
102 (s->image_height - y_pos - k) * s->frame.linesize[0],
104 /* advance source pointer to next line */
107 skip_bits_long(gb, 8 * block_size); /* skip the consumed bits */
112 static int flashsv_decode_frame(AVCodecContext *avctx, void *data,
113 int *data_size, AVPacket *avpkt)
115 int buf_size = avpkt->size;
116 FlashSVContext *s = avctx->priv_data;
117 int h_blocks, v_blocks, h_part, v_part, i, j;
120 /* no supplementary picture */
126 init_get_bits(&gb, avpkt->data, buf_size * 8);
128 /* start to parse the bitstream */
129 s->block_width = 16 * (get_bits(&gb, 4) + 1);
130 s->image_width = get_bits(&gb, 12);
131 s->block_height = 16 * (get_bits(&gb, 4) + 1);
132 s->image_height = get_bits(&gb, 12);
134 /* calculate number of blocks and size of border (partial) blocks */
135 h_blocks = s->image_width / s->block_width;
136 h_part = s->image_width % s->block_width;
137 v_blocks = s->image_height / s->block_height;
138 v_part = s->image_height % s->block_height;
140 /* the block size could change between frames, make sure the buffer
141 * is large enough, if not, get a larger one */
142 if (s->block_size < s->block_width * s->block_height) {
143 av_free(s->tmpblock);
144 if ((s->tmpblock = av_malloc(3 * s->block_width * s->block_height)) == NULL) {
145 av_log(avctx, AV_LOG_ERROR, "Can't allocate decompression buffer.\n");
146 return AVERROR(ENOMEM);
149 s->block_size = s->block_width * s->block_height;
151 /* initialize the image size once */
152 if (avctx->width == 0 && avctx->height == 0) {
153 avctx->width = s->image_width;
154 avctx->height = s->image_height;
157 /* check for changes of image width and image height */
158 if (avctx->width != s->image_width || avctx->height != s->image_height) {
159 av_log(avctx, AV_LOG_ERROR,
160 "Frame width or height differs from first frames!\n");
161 av_log(avctx, AV_LOG_ERROR, "fh = %d, fv %d vs ch = %d, cv = %d\n",
162 avctx->height, avctx->width, s->image_height, s->image_width);
163 return AVERROR_INVALIDDATA;
166 av_dlog(avctx, "image: %dx%d block: %dx%d num: %dx%d part: %dx%d\n",
167 s->image_width, s->image_height, s->block_width, s->block_height,
168 h_blocks, v_blocks, h_part, v_part);
170 s->frame.reference = 3;
171 s->frame.buffer_hints = FF_BUFFER_HINTS_VALID |
172 FF_BUFFER_HINTS_PRESERVE |
173 FF_BUFFER_HINTS_REUSABLE;
174 if (avctx->reget_buffer(avctx, &s->frame) < 0) {
175 av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
179 /* loop over all block columns */
180 for (j = 0; j < v_blocks + (v_part ? 1 : 0); j++) {
182 int y_pos = j * s->block_height; // vertical position in frame
183 int cur_blk_height = (j < v_blocks) ? s->block_height : v_part;
185 /* loop over all block rows */
186 for (i = 0; i < h_blocks + (h_part ? 1 : 0); i++) {
187 int x_pos = i * s->block_width; // horizontal position in frame
188 int cur_blk_width = (i < h_blocks) ? s->block_width : h_part;
190 /* get the size of the compressed zlib chunk */
191 int size = get_bits(&gb, 16);
192 if (8 * size > get_bits_left(&gb)) {
193 avctx->release_buffer(avctx, &s->frame);
194 s->frame.data[0] = NULL;
195 return AVERROR_INVALIDDATA;
198 /* skip unchanged blocks, which have size 0 */
200 if (flashsv_decode_block(avctx, avpkt, &gb, size,
201 cur_blk_width, cur_blk_height,
203 av_log(avctx, AV_LOG_ERROR,
204 "error in decompression of block %dx%d\n", i, j);
209 *data_size = sizeof(AVFrame);
210 *(AVFrame*)data = s->frame;
212 if ((get_bits_count(&gb) / 8) != buf_size)
213 av_log(avctx, AV_LOG_ERROR, "buffer not fully consumed (%d != %d)\n",
214 buf_size, (get_bits_count(&gb) / 8));
216 /* report that the buffer was completely consumed */
221 static av_cold int flashsv_decode_end(AVCodecContext *avctx)
223 FlashSVContext *s = avctx->priv_data;
224 inflateEnd(&s->zstream);
225 /* release the frame if needed */
226 if (s->frame.data[0])
227 avctx->release_buffer(avctx, &s->frame);
229 /* free the tmpblock */
230 av_free(s->tmpblock);
236 AVCodec ff_flashsv_decoder = {
238 .type = AVMEDIA_TYPE_VIDEO,
239 .id = CODEC_ID_FLASHSV,
240 .priv_data_size = sizeof(FlashSVContext),
241 .init = flashsv_decode_init,
242 .close = flashsv_decode_end,
243 .decode = flashsv_decode_frame,
244 .capabilities = CODEC_CAP_DR1,
245 .pix_fmts = (const enum PixelFormat[]){PIX_FMT_BGR24, PIX_FMT_NONE},
246 .long_name = NULL_IF_CONFIG_SMALL("Flash Screen Video v1"),