51831eb801deb9157cc3de3ab59be50454b4dd40
[libav.git] / libavcodec / flashsv.c
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 * 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.
32 */
33
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <zlib.h>
37
38 #include "avcodec.h"
39 #include "get_bits.h"
40
41 typedef struct FlashSVContext {
42 AVCodecContext *avctx;
43 AVFrame frame;
44 int image_width, image_height;
45 int block_width, block_height;
46 uint8_t *tmpblock;
47 int block_size;
48 z_stream zstream;
49 } FlashSVContext;
50
51
52 static av_cold int flashsv_decode_init(AVCodecContext *avctx)
53 {
54 FlashSVContext *s = avctx->priv_data;
55 int zret; // Zlib return code
56
57 s->avctx = avctx;
58 s->zstream.zalloc = Z_NULL;
59 s->zstream.zfree = Z_NULL;
60 s->zstream.opaque = Z_NULL;
61 zret = inflateInit(&s->zstream);
62 if (zret != Z_OK) {
63 av_log(avctx, AV_LOG_ERROR, "Inflate init error: %d\n", zret);
64 return 1;
65 }
66 avctx->pix_fmt = PIX_FMT_BGR24;
67 s->frame.data[0] = NULL;
68
69 return 0;
70 }
71
72
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)
76 {
77 struct FlashSVContext *s = avctx->priv_data;
78 uint8_t *line = s->tmpblock;
79 int k;
80 int ret = inflateReset(&s->zstream);
81 if (ret != Z_OK) {
82 //return -1;
83 }
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);
93 }
94
95 if (ret != Z_OK && ret != Z_STREAM_END) {
96 //return -1;
97 }
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],
103 line, width * 3);
104 /* advance source pointer to next line */
105 line += width * 3;
106 }
107 skip_bits_long(gb, 8 * block_size); /* skip the consumed bits */
108 return 0;
109 }
110
111
112 static int flashsv_decode_frame(AVCodecContext *avctx, void *data,
113 int *data_size, AVPacket *avpkt)
114 {
115 int buf_size = avpkt->size;
116 FlashSVContext *s = avctx->priv_data;
117 int h_blocks, v_blocks, h_part, v_part, i, j;
118 GetBitContext gb;
119
120 /* no supplementary picture */
121 if (buf_size == 0)
122 return 0;
123 if (buf_size < 4)
124 return -1;
125
126 init_get_bits(&gb, avpkt->data, buf_size * 8);
127
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);
133
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;
139
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);
147 }
148 }
149 s->block_size = s->block_width * s->block_height;
150
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;
155 }
156
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;
164 }
165
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);
169
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");
176 return -1;
177 }
178
179 /* loop over all block columns */
180 for (j = 0; j < v_blocks + (v_part ? 1 : 0); j++) {
181
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;
184
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;
189
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;
196 }
197
198 /* skip unchanged blocks, which have size 0 */
199 if (size) {
200 if (flashsv_decode_block(avctx, avpkt, &gb, size,
201 cur_blk_width, cur_blk_height,
202 x_pos, y_pos))
203 av_log(avctx, AV_LOG_ERROR,
204 "error in decompression of block %dx%d\n", i, j);
205 }
206 }
207 }
208
209 *data_size = sizeof(AVFrame);
210 *(AVFrame*)data = s->frame;
211
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));
215
216 /* report that the buffer was completely consumed */
217 return buf_size;
218 }
219
220
221 static av_cold int flashsv_decode_end(AVCodecContext *avctx)
222 {
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);
228
229 /* free the tmpblock */
230 av_free(s->tmpblock);
231
232 return 0;
233 }
234
235
236 AVCodec ff_flashsv_decoder = {
237 .name = "flashsv",
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"),
247 };