2 * Flash Screen Video decoder
3 * Copyright (C) 2004 Alex Beregszaszi
4 * Copyright (C) 2006 Benjamin Larsson
6 * This file is part of FFmpeg.
8 * FFmpeg 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 * FFmpeg 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 FFmpeg; 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 avcodec_get_frame_defaults(&s->frame);
68 s->frame.data[0] = NULL;
74 static int flashsv_decode_block(AVCodecContext *avctx, AVPacket *avpkt,
75 GetBitContext *gb, int block_size,
76 int width, int height, int x_pos, int y_pos)
78 struct FlashSVContext *s = avctx->priv_data;
79 uint8_t *line = s->tmpblock;
81 int ret = inflateReset(&s->zstream);
85 s->zstream.next_in = avpkt->data + get_bits_count(gb) / 8;
86 s->zstream.avail_in = block_size;
87 s->zstream.next_out = s->tmpblock;
88 s->zstream.avail_out = s->block_size * 3;
89 ret = inflate(&s->zstream, Z_FINISH);
90 if (ret == Z_DATA_ERROR) {
91 av_log(avctx, AV_LOG_ERROR, "Zlib resync occurred\n");
92 inflateSync(&s->zstream);
93 ret = inflate(&s->zstream, Z_FINISH);
96 if (ret != Z_OK && ret != Z_STREAM_END) {
99 /* Flash Screen Video stores the image upside down, so copy
100 * lines to destination in reverse order. */
101 for (k = 1; k <= height; k++) {
102 memcpy(s->frame.data[0] + x_pos * 3 +
103 (s->image_height - y_pos - k) * s->frame.linesize[0],
105 /* advance source pointer to next line */
108 skip_bits_long(gb, 8 * block_size); /* skip the consumed bits */
113 static int flashsv_decode_frame(AVCodecContext *avctx, void *data,
114 int *data_size, AVPacket *avpkt)
116 int buf_size = avpkt->size;
117 FlashSVContext *s = avctx->priv_data;
118 int h_blocks, v_blocks, h_part, v_part, i, j;
121 /* no supplementary picture */
127 init_get_bits(&gb, avpkt->data, buf_size * 8);
129 /* start to parse the bitstream */
130 s->block_width = 16 * (get_bits(&gb, 4) + 1);
131 s->image_width = get_bits(&gb, 12);
132 s->block_height = 16 * (get_bits(&gb, 4) + 1);
133 s->image_height = get_bits(&gb, 12);
135 /* calculate number of blocks and size of border (partial) blocks */
136 h_blocks = s->image_width / s->block_width;
137 h_part = s->image_width % s->block_width;
138 v_blocks = s->image_height / s->block_height;
139 v_part = s->image_height % s->block_height;
141 /* the block size could change between frames, make sure the buffer
142 * is large enough, if not, get a larger one */
143 if (s->block_size < s->block_width * s->block_height) {
144 av_free(s->tmpblock);
145 if ((s->tmpblock = av_malloc(3 * s->block_width * s->block_height)) == NULL) {
146 av_log(avctx, AV_LOG_ERROR, "Can't allocate decompression buffer.\n");
147 return AVERROR(ENOMEM);
150 s->block_size = s->block_width * s->block_height;
152 /* initialize the image size once */
153 if (avctx->width == 0 && avctx->height == 0) {
154 avctx->width = s->image_width;
155 avctx->height = s->image_height;
158 /* check for changes of image width and image height */
159 if (avctx->width != s->image_width || avctx->height != s->image_height) {
160 av_log(avctx, AV_LOG_ERROR,
161 "Frame width or height differs from first frames!\n");
162 av_log(avctx, AV_LOG_ERROR, "fh = %d, fv %d vs ch = %d, cv = %d\n",
163 avctx->height, avctx->width, s->image_height, s->image_width);
164 return AVERROR_INVALIDDATA;
167 av_dlog(avctx, "image: %dx%d block: %dx%d num: %dx%d part: %dx%d\n",
168 s->image_width, s->image_height, s->block_width, s->block_height,
169 h_blocks, v_blocks, h_part, v_part);
171 s->frame.reference = 3;
172 s->frame.buffer_hints = FF_BUFFER_HINTS_VALID |
173 FF_BUFFER_HINTS_PRESERVE |
174 FF_BUFFER_HINTS_REUSABLE;
175 if (avctx->reget_buffer(avctx, &s->frame) < 0) {
176 av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
180 /* loop over all block columns */
181 for (j = 0; j < v_blocks + (v_part ? 1 : 0); j++) {
183 int y_pos = j * s->block_height; // vertical position in frame
184 int cur_blk_height = (j < v_blocks) ? s->block_height : v_part;
186 /* loop over all block rows */
187 for (i = 0; i < h_blocks + (h_part ? 1 : 0); i++) {
188 int x_pos = i * s->block_width; // horizontal position in frame
189 int cur_blk_width = (i < h_blocks) ? s->block_width : h_part;
191 /* get the size of the compressed zlib chunk */
192 int size = get_bits(&gb, 16);
193 if (8 * size > get_bits_left(&gb)) {
194 avctx->release_buffer(avctx, &s->frame);
195 s->frame.data[0] = NULL;
196 return AVERROR_INVALIDDATA;
199 /* skip unchanged blocks, which have size 0 */
201 if (flashsv_decode_block(avctx, avpkt, &gb, size,
202 cur_blk_width, cur_blk_height,
204 av_log(avctx, AV_LOG_ERROR,
205 "error in decompression of block %dx%d\n", i, j);
210 *data_size = sizeof(AVFrame);
211 *(AVFrame*)data = s->frame;
213 if ((get_bits_count(&gb) / 8) != buf_size)
214 av_log(avctx, AV_LOG_ERROR, "buffer not fully consumed (%d != %d)\n",
215 buf_size, (get_bits_count(&gb) / 8));
217 /* report that the buffer was completely consumed */
222 static av_cold int flashsv_decode_end(AVCodecContext *avctx)
224 FlashSVContext *s = avctx->priv_data;
225 inflateEnd(&s->zstream);
226 /* release the frame if needed */
227 if (s->frame.data[0])
228 avctx->release_buffer(avctx, &s->frame);
230 /* free the tmpblock */
231 av_free(s->tmpblock);
237 AVCodec ff_flashsv_decoder = {
239 .type = AVMEDIA_TYPE_VIDEO,
240 .id = CODEC_ID_FLASHSV,
241 .priv_data_size = sizeof(FlashSVContext),
242 .init = flashsv_decode_init,
243 .close = flashsv_decode_end,
244 .decode = flashsv_decode_frame,
245 .capabilities = CODEC_CAP_DR1,
246 .pix_fmts = (const enum PixelFormat[]){PIX_FMT_BGR24, PIX_FMT_NONE},
247 .long_name = NULL_IF_CONFIG_SMALL("Flash Screen Video v1"),