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