c2bbb3de236873df2bac862c60eb3611e79ead82
[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 FFmpeg.
7 *
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.
12 *
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.
17 *
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
21 */
22
23 /**
24 * @file flashsv.c
25 * Flash Screen Video decoder
26 * @author Alex Beregszaszi
27 * @author Benjamin Larsson
28 */
29
30 /* Bitstream description
31 * The picture is divided into blocks that are zlib compressed.
32 *
33 * The decoder is fed complete frames, the frameheader contains:
34 * 4bits of block width
35 * 12bits of frame width
36 * 4bits of block height
37 * 12bits of frame height
38 *
39 * Directly after the header are the compressed blocks. The blocks
40 * have their compressed size represented with 16bits in the beginnig.
41 * If the size = 0 then the block is unchanged from the previous frame.
42 * All blocks are decompressed until the buffer is consumed.
43 *
44 * Encoding ideas, a basic encoder would just use a fixed block size.
45 * Block sizes can be multipels of 16, from 16 to 256. The blocks don't
46 * have to be quadratic. A brute force search with a set of diffrent
47 * block sizes should give a better result then to just use a fixed size.
48 */
49
50 #include <stdio.h>
51 #include <stdlib.h>
52
53 #include "common.h"
54 #include "avcodec.h"
55 #include "bitstream.h"
56
57 #ifdef CONFIG_ZLIB
58 #include <zlib.h>
59
60 typedef struct FlashSVContext {
61 AVCodecContext *avctx;
62 AVFrame frame;
63 int image_width, image_height;
64 int block_width, block_height;
65 uint8_t* tmpblock;
66 int block_size;
67 z_stream zstream;
68 } FlashSVContext;
69
70
71 static void copy_region(uint8_t *sptr, uint8_t *dptr,
72 int dx, int dy, int h, int w, int stride)
73 {
74 int i;
75
76 for (i = dx+h; i > dx; i--)
77 {
78 memcpy(dptr+(i*stride)+dy*3, sptr, w*3);
79 sptr += w*3;
80 }
81 }
82
83
84 static int flashsv_decode_init(AVCodecContext *avctx)
85 {
86 FlashSVContext *s = (FlashSVContext *)avctx->priv_data;
87 int zret; // Zlib return code
88
89 s->avctx = avctx;
90 s->zstream.zalloc = Z_NULL;
91 s->zstream.zfree = Z_NULL;
92 s->zstream.opaque = Z_NULL;
93 zret = inflateInit(&(s->zstream));
94 if (zret != Z_OK) {
95 av_log(avctx, AV_LOG_ERROR, "Inflate init error: %d\n", zret);
96 return 1;
97 }
98 avctx->pix_fmt = PIX_FMT_BGR24;
99 avctx->has_b_frames = 0;
100 s->frame.data[0] = NULL;
101
102 return 0;
103 }
104
105
106 static int flashsv_decode_frame(AVCodecContext *avctx,
107 void *data, int *data_size,
108 uint8_t *buf, int buf_size)
109 {
110 FlashSVContext *s = (FlashSVContext *)avctx->priv_data;
111 int h_blocks, v_blocks, h_part, v_part, i, j;
112 GetBitContext gb;
113
114 /* no supplementary picture */
115 if (buf_size == 0)
116 return 0;
117
118 if(s->frame.data[0])
119 avctx->release_buffer(avctx, &s->frame);
120
121 init_get_bits(&gb, buf, buf_size * 8);
122
123 /* start to parse the bitstream */
124 s->block_width = 16* (get_bits(&gb, 4)+1);
125 s->image_width = get_bits(&gb,12);
126 s->block_height= 16* (get_bits(&gb, 4)+1);
127 s->image_height= get_bits(&gb,12);
128
129 /* calculate amount of blocks and the size of the border blocks */
130 h_blocks = s->image_width / s->block_width;
131 h_part = s->image_width % s->block_width;
132 v_blocks = s->image_height / s->block_height;
133 v_part = s->image_height % s->block_height;
134
135 /* the block size could change between frames, make sure the buffer
136 * is large enough, if not, get a larger one */
137 if(s->block_size < s->block_width*s->block_height) {
138 if (s->tmpblock != NULL)
139 av_free(s->tmpblock);
140 if ((s->tmpblock = av_malloc(3*s->block_width*s->block_height)) == NULL) {
141 av_log(avctx, AV_LOG_ERROR, "Can't allocate decompression buffer.\n");
142 return -1;
143 }
144 }
145 s->block_size = s->block_width*s->block_height;
146
147 /* init the image size once */
148 if((avctx->width==0) && (avctx->height==0)){
149 avctx->width = s->image_width;
150 avctx->height = s->image_height;
151 }
152
153 /* check for changes of image width and image height */
154 if ((avctx->width != s->image_width) || (avctx->height != s->image_height)) {
155 av_log(avctx, AV_LOG_ERROR, "Frame width or height differs from first frames!\n");
156 av_log(avctx, AV_LOG_ERROR, "fh = %d, fv %d vs ch = %d, cv = %d\n",avctx->height,
157 avctx->width,s->image_height,s->image_width);
158 return -1;
159 }
160
161 av_log(avctx, AV_LOG_DEBUG, "image: %dx%d block: %dx%d num: %dx%d part: %dx%d\n",
162 s->image_width, s->image_height, s->block_width, s->block_height,
163 h_blocks, v_blocks, h_part, v_part);
164
165 s->frame.reference = 1;
166 s->frame.buffer_hints = FF_BUFFER_HINTS_VALID;
167 if (avctx->get_buffer(avctx, &s->frame) < 0) {
168 av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed\n");
169 return -1;
170 }
171
172 /* loop over all block columns */
173 for (j = 0; j < v_blocks + (v_part?1:0); j++)
174 {
175
176 int hp = j*s->block_height; // horiz position in frame
177 int hs = (j<v_blocks)?s->block_height:v_part; // size of block
178
179
180 /* loop over all block rows */
181 for (i = 0; i < h_blocks + (h_part?1:0); i++)
182 {
183 int wp = i*s->block_width; // vert position in frame
184 int ws = (i<h_blocks)?s->block_width:h_part; // size of block
185
186 /* get the size of the compressed zlib chunk */
187 int size = get_bits(&gb, 16);
188
189 if (size == 0) {
190 /* no change, don't do anything */
191 } else {
192 /* decompress block */
193 int ret = inflateReset(&(s->zstream));
194 if (ret != Z_OK)
195 {
196 av_log(avctx, AV_LOG_ERROR, "error in decompression (reset) of block %dx%d\n", i, j);
197 /* return -1; */
198 }
199 s->zstream.next_in = buf+(get_bits_count(&gb)/8);
200 s->zstream.avail_in = size;
201 s->zstream.next_out = s->tmpblock;
202 s->zstream.avail_out = s->block_size*3;
203 ret = inflate(&(s->zstream), Z_FINISH);
204 if (ret == Z_DATA_ERROR)
205 {
206 av_log(avctx, AV_LOG_ERROR, "Zlib resync occured\n");
207 inflateSync(&(s->zstream));
208 ret = inflate(&(s->zstream), Z_FINISH);
209 }
210
211 if ((ret != Z_OK) && (ret != Z_STREAM_END))
212 {
213 av_log(avctx, AV_LOG_ERROR, "error in decompression of block %dx%d: %d\n", i, j, ret);
214 /* return -1; */
215 }
216 copy_region(s->tmpblock, s->frame.data[0], s->image_height-(hp+hs+1), wp, hs, ws, s->frame.linesize[0]);
217 skip_bits(&gb, 8*size); /* skip the consumed bits */
218 }
219 }
220 }
221
222 *data_size = sizeof(AVFrame);
223 *(AVFrame*)data = s->frame;
224
225 if ((get_bits_count(&gb)/8) != buf_size)
226 av_log(avctx, AV_LOG_ERROR, "buffer not fully consumed (%d != %d)\n",
227 buf_size, (get_bits_count(&gb)/8));
228
229 /* report that the buffer was completely consumed */
230 return buf_size;
231 }
232
233
234 static int flashsv_decode_end(AVCodecContext *avctx)
235 {
236 FlashSVContext *s = (FlashSVContext *)avctx->priv_data;
237 inflateEnd(&(s->zstream));
238 /* release the frame if needed */
239 if (s->frame.data[0])
240 avctx->release_buffer(avctx, &s->frame);
241
242 /* free the tmpblock */
243 if (s->tmpblock != NULL)
244 av_free(s->tmpblock);
245
246 return 0;
247 }
248
249
250 AVCodec flashsv_decoder = {
251 "flashsv",
252 CODEC_TYPE_VIDEO,
253 CODEC_ID_FLASHSV,
254 sizeof(FlashSVContext),
255 flashsv_decode_init,
256 NULL,
257 flashsv_decode_end,
258 flashsv_decode_frame,
259 CODEC_CAP_DR1,
260 .pix_fmts = (enum PixelFormat[]){PIX_FMT_BGR24, -1},
261 };
262 #endif //CONFIG_ZLIB