Browse code

Flash Screen Video decoder (fourcc:FSV1)

Originally committed as revision 5436 to svn://svn.ffmpeg.org/ffmpeg/trunk

Benjamin Larsson authored on 2006/06/01 16:31:59
Showing 8 changed files
... ...
@@ -47,6 +47,7 @@ version <next>
47 47
 - KMVC decoder
48 48
 - MPEG-2 intra vlc support
49 49
 - MPEG-2 4:2:2 encoder
50
+- Flash Screen Video decoder
50 51
 
51 52
 version 0.4.9-pre1:
52 53
 
... ...
@@ -762,6 +762,7 @@ following image formats are supported:
762 762
 @item Theora                 @tab     @tab  X @tab still experimental
763 763
 @item Intel Indeo 3          @tab     @tab  X
764 764
 @item FLV                    @tab  X  @tab  X @tab Sorenson H.263 used in Flash
765
+@item Flash Screen Video     @tab     @tab  X @tab fourcc: FSV1
765 766
 @item ATI VCR1               @tab     @tab  X @tab fourcc: VCR1
766 767
 @item ATI VCR2               @tab     @tab  X @tab fourcc: VCR2
767 768
 @item Cirrus Logic AccuPak   @tab     @tab  X @tab fourcc: CLJR
... ...
@@ -233,6 +233,9 @@ endif
233 233
 ifeq ($(CONFIG_ZMBV_DECODER),yes)
234 234
 	OBJS+= zmbv.o
235 235
 endif
236
+ifeq ($(CONFIG_FLASHSV_DECODER),yes)
237
+	OBJS+= flashsv.o
238
+endif
236 239
 
237 240
 
238 241
 ifeq ($(AMR),yes)
... ...
@@ -529,6 +529,9 @@ void avcodec_register_all(void)
529 529
 #ifdef CONFIG_RAWVIDEO_DECODER
530 530
     register_avcodec(&rawvideo_decoder);
531 531
 #endif //CONFIG_RAWVIDEO_DECODER
532
+#ifdef CONFIG_FLASHSV_DECODER
533
+    register_avcodec(&flashsv_decoder);
534
+#endif //CONFIG_FLASHSV_DECODER
532 535
 #endif /* CONFIG_DECODERS */
533 536
 
534 537
 #if defined(AMR_NB) || defined(AMR_NB_FIXED)
... ...
@@ -121,6 +121,7 @@ enum CodecID {
121 121
     CODEC_ID_SMACKVIDEO,
122 122
     CODEC_ID_NUV,
123 123
     CODEC_ID_KMVC,
124
+    CODEC_ID_FLASHSV,
124 125
 
125 126
     /* various pcm "codecs" */
126 127
     CODEC_ID_PCM_S16LE= 0x10000,
... ...
@@ -2210,6 +2211,7 @@ extern AVCodec avs_decoder;
2210 2210
 extern AVCodec smacker_decoder;
2211 2211
 extern AVCodec smackaud_decoder;
2212 2212
 extern AVCodec kmvc_decoder;
2213
+extern AVCodec flashsv_decoder;
2213 2214
 
2214 2215
 /* pcm codecs */
2215 2216
 #define PCM_CODEC(id, name) \
2216 2217
new file mode 100644
... ...
@@ -0,0 +1,274 @@
0
+/*
1
+ * Flash Screen Video decoder
2
+ * Copyright (C) 2004 Alex Beregszaszi
3
+ * Copyright (C) 2006 Benjamin Larsson
4
+ *
5
+ * This library is free software; you can redistribute it and/or
6
+ * modify it under the terms of the GNU Lesser General Public
7
+ * License as published by the Free Software Foundation; either
8
+ * version 2 of the License, or (at your option) any later version.
9
+ *
10
+ * This library is distributed in the hope that it will be useful,
11
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13
+ * Lesser General Public License for more details.
14
+ *
15
+ * You should have received a copy of the GNU Lesser General Public
16
+ * License along with this library; if not, write to the Free Software
17
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18
+ */
19
+
20
+/**
21
+ * @file flashsv.c
22
+ * Flash Screen Video decoder
23
+ * @author Alex Beregszaszi
24
+ * @author Benjamin Larsson
25
+ */
26
+
27
+/* Bitstream description
28
+ * The picture is divided into blocks that are zlib compressed.
29
+ *
30
+ * The decoder is fed complete frames, the frameheader contains:
31
+ * 4bits of block width
32
+ * 12bits of frame width
33
+ * 4bits of block height
34
+ * 12bits of frame height
35
+ *
36
+ * Directly after the header are the compressed blocks. The blocks
37
+ * have their compressed size represented with 16bits in the beginnig.
38
+ * If the size = 0 then the block is unchanged from the previous frame.
39
+ * All blocks are decompressed until the buffer is consumed.
40
+ *
41
+ * Encoding ideas, a basic encoder would just use a fixed block size.
42
+ * Block sizes can be multipels of 16, from 16 to 256. The blocks don't
43
+ * have to be quadratic. A brute force search with a set of diffrent
44
+ * block sizes should give a better result then to just use a fixed size.
45
+ */
46
+
47
+#include <stdio.h>
48
+#include <stdlib.h>
49
+
50
+#include "common.h"
51
+#include "avcodec.h"
52
+#include "bitstream.h"
53
+
54
+#ifdef CONFIG_ZLIB
55
+#include <zlib.h>
56
+#endif
57
+
58
+typedef struct FlashSVContext {
59
+    AVCodecContext *avctx;
60
+    AVFrame frame;
61
+    int image_width, image_height;
62
+    int block_width, block_height;
63
+    uint8_t* tmpblock;
64
+    int block_size;
65
+#ifdef CONFIG_ZLIB
66
+    z_stream zstream;
67
+#endif
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
+#ifdef CONFIG_ZLIB
91
+    s->zstream.zalloc = Z_NULL;
92
+    s->zstream.zfree = Z_NULL;
93
+    s->zstream.opaque = Z_NULL;
94
+    zret = inflateInit(&(s->zstream));
95
+    if (zret != Z_OK) {
96
+        av_log(avctx, AV_LOG_ERROR, "Inflate init error: %d\n", zret);
97
+        return 1;
98
+    }
99
+#else
100
+    av_log(avctx, AV_LOG_ERROR, "Zlib support not compiled. Needed for the decoder.\n");
101
+    return 1;
102
+#endif
103
+    avctx->pix_fmt = PIX_FMT_BGR24;
104
+    avctx->has_b_frames = 0;
105
+    s->frame.data[0] = NULL;
106
+
107
+    return 0;
108
+}
109
+
110
+
111
+static int flashsv_decode_frame(AVCodecContext *avctx,
112
+                                    void *data, int *data_size,
113
+                                    uint8_t *buf, int buf_size)
114
+{
115
+    FlashSVContext *s = (FlashSVContext *)avctx->priv_data;
116
+    int h_blocks, v_blocks, h_part, v_part, i, j;
117
+    GetBitContext gb;
118
+
119
+    /* no supplementary picture */
120
+    if (buf_size == 0)
121
+        return 0;
122
+
123
+    if(s->frame.data[0])
124
+            avctx->release_buffer(avctx, &s->frame);
125
+
126
+    init_get_bits(&gb, buf, 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 amount of blocks and the size of the border 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
+        if (s->tmpblock != NULL)
144
+            av_free(s->tmpblock);
145
+        s->block_size = s->block_width*s->block_height;
146
+        if ((s->tmpblock = av_malloc(3*s->block_size)) == NULL) {
147
+            av_log(avctx, AV_LOG_ERROR, "Can't allocate decompression buffer.\n");
148
+            return -1;
149
+        }
150
+    }
151
+
152
+    /* init the image size once */
153
+    if((avctx->width==0) && (avctx->height==0)){
154
+        avctx->width = s->image_width;
155
+        avctx->height = s->image_height;
156
+    }
157
+
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, "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",avctx->height,
162
+        avctx->width,s->image_height,s->image_width);
163
+        return -1;
164
+    }
165
+
166
+    av_log(avctx, AV_LOG_DEBUG, "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 = 1;
171
+    s->frame.buffer_hints = FF_BUFFER_HINTS_VALID;
172
+    if (avctx->get_buffer(avctx, &s->frame) < 0) {
173
+        av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed\n");
174
+        return -1;
175
+    }
176
+
177
+    /* loop over all block columns */
178
+    for (j = 0; j < v_blocks + (v_part?1:0); j++)
179
+    {
180
+
181
+        int hp = j*s->block_height; // horiz position in frame
182
+        int hs = (j<v_blocks)?s->block_height:v_part; // size of block
183
+
184
+
185
+        /* loop over all block rows */
186
+        for (i = 0; i < h_blocks + (h_part?1:0); i++)
187
+        {
188
+            int wp = i*s->block_width; // vert position in frame
189
+            int ws = (i<h_blocks)?s->block_width:h_part; // size of block
190
+
191
+            /* get the size of the compressed zlib chunk */
192
+            int size = get_bits(&gb, 16);
193
+
194
+            if (size == 0) {
195
+                /* no change, don't do anything */
196
+            } else {
197
+                /* decompress block */
198
+#ifdef CONFIG_ZLIB
199
+                int ret = inflateReset(&(s->zstream));
200
+                if (ret != Z_OK)
201
+                {
202
+                    av_log(avctx, AV_LOG_ERROR, "error in decompression (reset) of block %dx%d\n", i, j);
203
+                    /* return -1; */
204
+                }
205
+                s->zstream.next_in = buf+(get_bits_count(&gb)/8);
206
+                s->zstream.avail_in = size;
207
+                s->zstream.next_out = s->tmpblock;
208
+                s->zstream.avail_out = s->block_size*3;
209
+                ret = inflate(&(s->zstream), Z_FINISH);
210
+                if (ret == Z_DATA_ERROR)
211
+                {
212
+                    av_log(avctx, AV_LOG_ERROR, "Zlib resync occured\n");
213
+                    inflateSync(&(s->zstream));
214
+                    ret = inflate(&(s->zstream), Z_FINISH);
215
+                }
216
+
217
+                if ((ret != Z_OK) && (ret != Z_STREAM_END))
218
+                {
219
+                    av_log(avctx, AV_LOG_ERROR, "error in decompression of block %dx%d: %d\n", i, j, ret);
220
+                    /* return -1; */
221
+                }
222
+#else
223
+                av_log(avctx, AV_LOG_ERROR, "Zlib support not compiled in.\n");
224
+                return -1;
225
+#endif
226
+                copy_region(s->tmpblock, s->frame.data[0], s->image_height-(hp+hs+1), wp, hs, ws, s->frame.linesize[0]);
227
+                skip_bits(&gb, 8*size);   /* skip the consumed bits */
228
+            }
229
+        }
230
+    }
231
+
232
+    *data_size = sizeof(AVFrame);
233
+    *(AVFrame*)data = s->frame;
234
+
235
+    if ((get_bits_count(&gb)/8) != buf_size)
236
+        av_log(avctx, AV_LOG_ERROR, "buffer not fully consumed (%d != %d)\n",
237
+            buf_size, (get_bits_count(&gb)/8));
238
+
239
+    /* report that the buffer was completely consumed */
240
+    return buf_size;
241
+}
242
+
243
+
244
+static int flashsv_decode_end(AVCodecContext *avctx)
245
+{
246
+    FlashSVContext *s = (FlashSVContext *)avctx->priv_data;
247
+#ifdef CONFIG_ZLIB
248
+    inflateEnd(&(s->zstream));
249
+#endif
250
+    /* release the frame if needed */
251
+    if (s->frame.data[0])
252
+        avctx->release_buffer(avctx, &s->frame);
253
+
254
+    /* free the tmpblock */
255
+    if (s->tmpblock != NULL)
256
+        av_free(s->tmpblock);
257
+
258
+    return 0;
259
+}
260
+
261
+
262
+AVCodec flashsv_decoder = {
263
+    "flashsv",
264
+    CODEC_TYPE_VIDEO,
265
+    CODEC_ID_FLASHSV,
266
+    sizeof(FlashSVContext),
267
+    flashsv_decode_init,
268
+    NULL,
269
+    flashsv_decode_end,
270
+    flashsv_decode_frame,
271
+    CODEC_CAP_DR1,
272
+    .pix_fmts = (enum PixelFormat[]){PIX_FMT_BGR24, -1},
273
+};
... ...
@@ -184,6 +184,7 @@ const CodecTag codec_bmp_tags[] = {
184 184
     { CODEC_ID_SNOW, MKTAG('S', 'N', 'O', 'W') },
185 185
     { CODEC_ID_4XM, MKTAG('4', 'X', 'M', 'V') },
186 186
     { CODEC_ID_FLV1, MKTAG('F', 'L', 'V', '1') },
187
+    { CODEC_ID_FLASHSV, MKTAG('F', 'S', 'V', '1') },
187 188
     { CODEC_ID_SVQ1, MKTAG('s', 'v', 'q', '1') },
188 189
     { CODEC_ID_TSCC, MKTAG('t', 's', 'c', 'c') },
189 190
     { CODEC_ID_ULTI, MKTAG('U', 'L', 'T', 'I') },
... ...
@@ -183,6 +183,7 @@ static int flv_read_packet(AVFormatContext *s, AVPacket *pkt)
183 183
             st->codec->codec_type = CODEC_TYPE_VIDEO;
184 184
             switch(flags & 0xF){
185 185
             case 2: st->codec->codec_id = CODEC_ID_FLV1; break;
186
+            case 3: st->codec->codec_id = CODEC_ID_FLASHSV; break;
186 187
             default:
187 188
                     av_log(s, AV_LOG_INFO, "Unsupported video codec (%x)\n", flags & 0xf);
188 189
                 st->codec->codec_tag= flags & 0xF;