annotate flashsv.c @ 3529:b8656dfe6b1a libavcodec

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