]> git.sesse.net Git - ffmpeg/blob - libavcodec/flashsv.c
8305378f949ca14e9e37a24eaad53c3a7a1e910b
[ffmpeg] / 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 void copy_region(uint8_t *sptr, uint8_t *dptr,
53                         int dx, int dy, int h, int w, int stride)
54 {
55     int i;
56
57     for (i = dx + h; i > dx; i--) {
58         memcpy(dptr + i * stride + dy * 3, sptr, w * 3);
59         sptr += w * 3;
60     }
61 }
62
63
64 static av_cold int flashsv_decode_init(AVCodecContext *avctx)
65 {
66     FlashSVContext *s = avctx->priv_data;
67     int zret; // Zlib return code
68
69     s->avctx          = avctx;
70     s->zstream.zalloc = Z_NULL;
71     s->zstream.zfree  = Z_NULL;
72     s->zstream.opaque = Z_NULL;
73     zret = inflateInit(&s->zstream);
74     if (zret != Z_OK) {
75         av_log(avctx, AV_LOG_ERROR, "Inflate init error: %d\n", zret);
76         return 1;
77     }
78     avctx->pix_fmt = PIX_FMT_BGR24;
79     s->frame.data[0] = NULL;
80
81     return 0;
82 }
83
84
85 static int flashsv_decode_frame(AVCodecContext *avctx, void *data,
86                                 int *data_size, AVPacket *avpkt)
87 {
88     int buf_size       = avpkt->size;
89     FlashSVContext *s  = avctx->priv_data;
90     int h_blocks, v_blocks, h_part, v_part, i, j;
91     GetBitContext gb;
92
93     /* no supplementary picture */
94     if (buf_size == 0)
95         return 0;
96     if (buf_size < 4)
97         return -1;
98
99     init_get_bits(&gb, avpkt->data, buf_size * 8);
100
101     /* start to parse the bitstream */
102     s->block_width  = 16 * (get_bits(&gb,  4) + 1);
103     s->image_width  =       get_bits(&gb, 12);
104     s->block_height = 16 * (get_bits(&gb,  4) + 1);
105     s->image_height =       get_bits(&gb, 12);
106
107     /* calculate amount of blocks and the size of the border blocks */
108     h_blocks = s->image_width  / s->block_width;
109     h_part   = s->image_width  % s->block_width;
110     v_blocks = s->image_height / s->block_height;
111     v_part   = s->image_height % s->block_height;
112
113     /* the block size could change between frames, make sure the buffer
114      * is large enough, if not, get a larger one */
115     if (s->block_size < s->block_width * s->block_height) {
116         av_free(s->tmpblock);
117         if ((s->tmpblock = av_malloc(3 * s->block_width * s->block_height)) == NULL) {
118             av_log(avctx, AV_LOG_ERROR, "Can't allocate decompression buffer.\n");
119             return AVERROR(ENOMEM);
120         }
121     }
122     s->block_size = s->block_width * s->block_height;
123
124     /* init the image size once */
125     if (avctx->width == 0 && avctx->height == 0) {
126         avctx->width  = s->image_width;
127         avctx->height = s->image_height;
128     }
129
130     /* check for changes of image width and image height */
131     if (avctx->width != s->image_width || avctx->height != s->image_height) {
132         av_log(avctx, AV_LOG_ERROR,
133                "Frame width or height differs from first frames!\n");
134         av_log(avctx, AV_LOG_ERROR, "fh = %d, fv %d  vs  ch = %d, cv = %d\n",
135                avctx->height, avctx->width, s->image_height, s->image_width);
136         return AVERROR_INVALIDDATA;
137     }
138
139     av_dlog(avctx, "image: %dx%d block: %dx%d num: %dx%d part: %dx%d\n",
140             s->image_width, s->image_height, s->block_width, s->block_height,
141             h_blocks, v_blocks, h_part, v_part);
142
143     s->frame.reference    = 3;
144     s->frame.buffer_hints = FF_BUFFER_HINTS_VALID    |
145                             FF_BUFFER_HINTS_PRESERVE |
146                             FF_BUFFER_HINTS_REUSABLE;
147     if (avctx->reget_buffer(avctx, &s->frame) < 0) {
148         av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
149         return -1;
150     }
151
152     /* loop over all block columns */
153     for (j = 0; j < v_blocks + (v_part ? 1 : 0); j++) {
154
155         int hp = j * s->block_height; // horiz position in frame
156         int hs = (j < v_blocks) ? s->block_height : v_part; // size of block
157
158
159         /* loop over all block rows */
160         for (i = 0; i < h_blocks + (h_part ? 1 : 0); i++) {
161             int wp = i * s->block_width; // vert position in frame
162             int ws = (i < h_blocks) ? s->block_width : h_part; // size of block
163
164             /* get the size of the compressed zlib chunk */
165             int size = get_bits(&gb, 16);
166             if (8 * size > get_bits_left(&gb)) {
167                 avctx->release_buffer(avctx, &s->frame);
168                 s->frame.data[0] = NULL;
169                 return AVERROR_INVALIDDATA;
170             }
171
172             /* skip unchanged blocks, which have size 0 */
173             if (size) {
174                 /* decompress block */
175                 int ret = inflateReset(&s->zstream);
176                 if (ret != Z_OK) {
177                     av_log(avctx, AV_LOG_ERROR,
178                            "error in decompression (reset) of block %dx%d\n", i, j);
179                     /* return -1; */
180                 }
181                 s->zstream.next_in   = avpkt->data + get_bits_count(&gb) / 8;
182                 s->zstream.avail_in  = size;
183                 s->zstream.next_out  = s->tmpblock;
184                 s->zstream.avail_out = s->block_size * 3;
185                 ret = inflate(&s->zstream, Z_FINISH);
186                 if (ret == Z_DATA_ERROR) {
187                     av_log(avctx, AV_LOG_ERROR, "Zlib resync occurred\n");
188                     inflateSync(&s->zstream);
189                     ret = inflate(&s->zstream, Z_FINISH);
190                 }
191
192                 if (ret != Z_OK && ret != Z_STREAM_END) {
193                     av_log(avctx, AV_LOG_ERROR,
194                            "error in decompression of block %dx%d: %d\n", i, j, ret);
195                     /* return -1; */
196                 }
197                 copy_region(s->tmpblock, s->frame.data[0],
198                             s->image_height - (hp + hs + 1),
199                             wp, hs, ws, s->frame.linesize[0]);
200                 skip_bits_long(&gb, 8 * size);   /* skip the consumed bits */
201             }
202         }
203     }
204
205     *data_size = sizeof(AVFrame);
206     *(AVFrame*)data = s->frame;
207
208     if ((get_bits_count(&gb) / 8) != buf_size)
209         av_log(avctx, AV_LOG_ERROR, "buffer not fully consumed (%d != %d)\n",
210                buf_size, (get_bits_count(&gb) / 8));
211
212     /* report that the buffer was completely consumed */
213     return buf_size;
214 }
215
216
217 static av_cold int flashsv_decode_end(AVCodecContext *avctx)
218 {
219     FlashSVContext *s = avctx->priv_data;
220     inflateEnd(&s->zstream);
221     /* release the frame if needed */
222     if (s->frame.data[0])
223         avctx->release_buffer(avctx, &s->frame);
224
225     /* free the tmpblock */
226     av_free(s->tmpblock);
227
228     return 0;
229 }
230
231
232 AVCodec ff_flashsv_decoder = {
233     .name           = "flashsv",
234     .type           = AVMEDIA_TYPE_VIDEO,
235     .id             = CODEC_ID_FLASHSV,
236     .priv_data_size = sizeof(FlashSVContext),
237     .init           = flashsv_decode_init,
238     .close          = flashsv_decode_end,
239     .decode         = flashsv_decode_frame,
240     .capabilities   = CODEC_CAP_DR1,
241     .pix_fmts       = (const enum PixelFormat[]){PIX_FMT_BGR24, PIX_FMT_NONE},
242     .long_name      = NULL_IF_CONFIG_SMALL("Flash Screen Video v1"),
243 };