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