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