Mercurial > libavcodec.hg
comparison dxa.c @ 4671:97e3364d267a libavcodec
DXA demuxer and decoder
author | kostya |
---|---|
date | Wed, 14 Mar 2007 14:49:49 +0000 |
parents | |
children | b3ee9a1526b0 |
comparison
equal
deleted
inserted
replaced
4670:aab60ff3d468 | 4671:97e3364d267a |
---|---|
1 /* | |
2 * Feeble Files/ScummVM DXA decoder | |
3 * Copyright (c) 2007 Konstantin Shishkov | |
4 * | |
5 * This file is part of FFmpeg. | |
6 * | |
7 * FFmpeg is free software; you can redistribute it and/or | |
8 * modify it under the terms of the GNU Lesser General Public | |
9 * License as published by the Free Software Foundation; either | |
10 * version 2.1 of the License, or (at your option) any later version. | |
11 * | |
12 * FFmpeg is distributed in the hope that it will be useful, | |
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
15 * Lesser General Public License for more details. | |
16 * | |
17 * You should have received a copy of the GNU Lesser General Public | |
18 * License along with FFmpeg; if not, write to the Free Software | |
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | |
20 * | |
21 */ | |
22 | |
23 /** | |
24 * @file dxa.c | |
25 * DXA Video decoder | |
26 */ | |
27 | |
28 #include <stdio.h> | |
29 #include <stdlib.h> | |
30 | |
31 #include "common.h" | |
32 #include "avcodec.h" | |
33 | |
34 #include <zlib.h> | |
35 | |
36 /* | |
37 * Decoder context | |
38 */ | |
39 typedef struct DxaDecContext { | |
40 AVCodecContext *avctx; | |
41 AVFrame pic, prev; | |
42 | |
43 int dsize; | |
44 uint8_t *decomp_buf; | |
45 uint32_t pal[256]; | |
46 } DxaDecContext; | |
47 | |
48 static const int shift1[6] = { 0, 8, 8, 8, 4, 4 }; | |
49 static const int shift2[6] = { 0, 0, 8, 4, 0, 4 }; | |
50 | |
51 static int decode_13(AVCodecContext *avctx, DxaDecContext *c, uint8_t* dst, uint8_t *src, uint8_t *ref) | |
52 { | |
53 uint8_t *code, *data, *mv, *msk, *tmp, *tmp2; | |
54 int i, j, k; | |
55 int type, x, y, d, d2; | |
56 int stride = c->pic.linesize[0]; | |
57 uint32_t mask; | |
58 | |
59 code = src + 12; | |
60 data = code + ((avctx->width * avctx->height) >> 4); | |
61 mv = data + AV_RB32(src + 0); | |
62 msk = mv + AV_RB32(src + 4); | |
63 | |
64 for(j = 0; j < avctx->height; j += 4){ | |
65 for(i = 0; i < avctx->width; i += 4){ | |
66 tmp = dst + i; | |
67 tmp2 = ref + i; | |
68 type = *code++; | |
69 switch(type){ | |
70 case 4: // motion compensation | |
71 x = (*mv) >> 4; if(x & 8) x = 8 - x; | |
72 y = (*mv++) & 0xF; if(y & 8) y = 8 - y; | |
73 tmp2 += x + y*stride; | |
74 case 0: // skip | |
75 case 5: // skip in method 12 | |
76 for(y = 0; y < 4; y++){ | |
77 memcpy(tmp, tmp2, 4); | |
78 tmp += stride; | |
79 tmp2 += stride; | |
80 } | |
81 break; | |
82 case 1: // masked change | |
83 case 10: // masked change with only half of pixels changed | |
84 case 11: // cases 10-15 are for method 12 only | |
85 case 12: | |
86 case 13: | |
87 case 14: | |
88 case 15: | |
89 if(type == 1){ | |
90 mask = AV_RB16(msk); | |
91 msk += 2; | |
92 }else{ | |
93 type -= 10; | |
94 mask = ((msk[0] & 0xF0) << shift1[type]) | ((msk[0] & 0xF) << shift2[type]); | |
95 msk++; | |
96 } | |
97 for(y = 0; y < 4; y++){ | |
98 for(x = 0; x < 4; x++){ | |
99 tmp[x] = (mask & 0x8000) ? *data++ : tmp2[x]; | |
100 mask <<= 1; | |
101 } | |
102 tmp += stride; | |
103 tmp2 += stride; | |
104 } | |
105 break; | |
106 case 2: // fill block | |
107 for(y = 0; y < 4; y++){ | |
108 memset(tmp, data[0], 4); | |
109 tmp += stride; | |
110 } | |
111 data++; | |
112 break; | |
113 case 3: // raw block | |
114 for(y = 0; y < 4; y++){ | |
115 memcpy(tmp, data, 4); | |
116 data += 4; | |
117 tmp += stride; | |
118 } | |
119 break; | |
120 case 8: // subblocks - method 13 only | |
121 mask = *msk++; | |
122 for(k = 0; k < 4; k++){ | |
123 d = ((k & 1) << 1) + ((k & 2) * stride); | |
124 d2 = ((k & 1) << 1) + ((k & 2) * stride); | |
125 tmp2 = ref + i + d2; | |
126 switch(mask & 0xC0){ | |
127 case 0x80: // motion compensation | |
128 x = (*mv) >> 4; if(x & 8) x = 8 - x; | |
129 y = (*mv++) & 0xF; if(y & 8) y = 8 - y; | |
130 tmp2 += x + y*stride; | |
131 case 0x00: // skip | |
132 tmp[d + 0 ] = tmp2[0]; | |
133 tmp[d + 1 ] = tmp2[1]; | |
134 tmp[d + 0 + stride] = tmp2[0 + stride]; | |
135 tmp[d + 1 + stride] = tmp2[1 + stride]; | |
136 break; | |
137 case 0x40: // fill | |
138 tmp[d + 0 ] = data[0]; | |
139 tmp[d + 1 ] = data[0]; | |
140 tmp[d + 0 + stride] = data[0]; | |
141 tmp[d + 1 + stride] = data[0]; | |
142 data++; | |
143 break; | |
144 case 0xC0: // raw | |
145 tmp[d + 0 ] = *data++; | |
146 tmp[d + 1 ] = *data++; | |
147 tmp[d + 0 + stride] = *data++; | |
148 tmp[d + 1 + stride] = *data++; | |
149 break; | |
150 } | |
151 mask <<= 2; | |
152 } | |
153 break; | |
154 case 32: // vector quantization - 2 colors | |
155 mask = AV_RB16(msk); | |
156 msk += 2; | |
157 for(y = 0; y < 4; y++){ | |
158 for(x = 0; x < 4; x++){ | |
159 tmp[x] = data[mask & 1]; | |
160 mask >>= 1; | |
161 } | |
162 tmp += stride; | |
163 tmp2 += stride; | |
164 } | |
165 data += 2; | |
166 break; | |
167 case 33: // vector quantization - 3 or 4 colors | |
168 case 34: | |
169 mask = AV_RB32(msk); | |
170 msk += 4; | |
171 for(y = 0; y < 4; y++){ | |
172 for(x = 0; x < 4; x++){ | |
173 tmp[x] = data[mask & 3]; | |
174 mask >>= 2; | |
175 } | |
176 tmp += stride; | |
177 tmp2 += stride; | |
178 } | |
179 data += type - 30; | |
180 break; | |
181 default: | |
182 av_log(avctx, AV_LOG_ERROR, "Unknown opcode %d\n", type); | |
183 return -1; | |
184 } | |
185 } | |
186 dst += stride * 4; | |
187 ref += stride * 4; | |
188 } | |
189 return 0; | |
190 } | |
191 | |
192 static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, uint8_t *buf, int buf_size) | |
193 { | |
194 DxaDecContext * const c = (DxaDecContext *)avctx->priv_data; | |
195 uint8_t *outptr, *srcptr, *tmpptr; | |
196 unsigned long dsize; | |
197 int i, j, compr; | |
198 int stride; | |
199 int orig_buf_size = buf_size; | |
200 int pc = 0; | |
201 | |
202 /* make the palette available on the way out */ | |
203 if(buf[0]=='C' && buf[1]=='M' && buf[2]=='A' && buf[3]=='P'){ | |
204 int r, g, b; | |
205 | |
206 buf += 4; | |
207 for(i = 0; i < 256; i++){ | |
208 r = *buf++; | |
209 g = *buf++; | |
210 b = *buf++; | |
211 c->pal[i] = (r << 16) | (g << 8) | b; | |
212 } | |
213 pc = 1; | |
214 buf_size -= 768+4; | |
215 } | |
216 | |
217 if(avctx->get_buffer(avctx, &c->pic) < 0){ | |
218 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n"); | |
219 return -1; | |
220 } | |
221 memcpy(c->pic.data[1], c->pal, AVPALETTE_SIZE); | |
222 c->pic.palette_has_changed = pc; | |
223 | |
224 outptr = c->pic.data[0]; | |
225 srcptr = c->decomp_buf; | |
226 tmpptr = c->prev.data[0]; | |
227 stride = c->pic.linesize[0]; | |
228 | |
229 if(buf[0]=='N' && buf[1]=='U' && buf[2]=='L' && buf[3]=='L') | |
230 compr = -1; | |
231 else | |
232 compr = buf[4]; | |
233 | |
234 dsize = c->dsize; | |
235 if((compr != 4 && compr != -1) && uncompress(c->decomp_buf, &dsize, buf + 9, buf_size - 9) != Z_OK){ | |
236 av_log(avctx, AV_LOG_ERROR, "Uncompress failed!\n"); | |
237 return -1; | |
238 } | |
239 switch(compr){ | |
240 case -1: | |
241 c->pic.key_frame = 0; | |
242 c->pic.pict_type = FF_P_TYPE; | |
243 if(c->prev.data[0]) | |
244 memcpy(c->pic.data[0], c->prev.data[0], c->pic.linesize[0] * avctx->height); | |
245 else{ // Should happen only when first frame is 'NULL' | |
246 memset(c->pic.data[0], 0, c->pic.linesize[0] * avctx->height); | |
247 c->pic.key_frame = 1; | |
248 c->pic.pict_type = FF_I_TYPE; | |
249 } | |
250 break; | |
251 case 2: | |
252 case 3: | |
253 case 4: | |
254 case 5: | |
255 c->pic.key_frame = !(compr & 1); | |
256 c->pic.pict_type = (compr & 1) ? FF_P_TYPE : FF_I_TYPE; | |
257 for(j = 0; j < avctx->height; j++){ | |
258 if(compr & 1){ | |
259 for(i = 0; i < avctx->width; i++) | |
260 outptr[i] = srcptr[i] ^ tmpptr[i]; | |
261 tmpptr += stride; | |
262 }else | |
263 memcpy(outptr, srcptr, avctx->width); | |
264 outptr += stride; | |
265 srcptr += avctx->width; | |
266 } | |
267 break; | |
268 case 12: // ScummVM coding | |
269 case 13: | |
270 c->pic.key_frame = 0; | |
271 c->pic.pict_type = FF_P_TYPE; | |
272 decode_13(avctx, c, c->pic.data[0], srcptr, c->prev.data[0]); | |
273 break; | |
274 default: | |
275 av_log(avctx, AV_LOG_ERROR, "Unknown/unsupported compression type %d\n", buf[4]); | |
276 return -1; | |
277 } | |
278 | |
279 FFSWAP(AVFrame, c->pic, c->prev); | |
280 if(c->pic.data[0]) | |
281 avctx->release_buffer(avctx, &c->pic); | |
282 | |
283 *data_size = sizeof(AVFrame); | |
284 *(AVFrame*)data = c->prev; | |
285 | |
286 /* always report that the buffer was completely consumed */ | |
287 return orig_buf_size; | |
288 } | |
289 | |
290 static int decode_init(AVCodecContext *avctx) | |
291 { | |
292 DxaDecContext * const c = (DxaDecContext *)avctx->priv_data; | |
293 | |
294 c->avctx = avctx; | |
295 avctx->pix_fmt = PIX_FMT_PAL8; | |
296 | |
297 if (avcodec_check_dimensions(avctx, avctx->width, avctx->height) < 0) { | |
298 return -1; | |
299 } | |
300 | |
301 c->dsize = avctx->width * avctx->height * 2; | |
302 if((c->decomp_buf = av_malloc(c->dsize)) == NULL) { | |
303 av_log(avctx, AV_LOG_ERROR, "Can't allocate decompression buffer.\n"); | |
304 return -1; | |
305 } | |
306 | |
307 return 0; | |
308 } | |
309 | |
310 static int decode_end(AVCodecContext *avctx) | |
311 { | |
312 DxaDecContext * const c = (DxaDecContext *)avctx->priv_data; | |
313 | |
314 av_freep(&c->decomp_buf); | |
315 if(c->prev.data[0]) | |
316 avctx->release_buffer(avctx, &c->prev); | |
317 if(c->pic.data[0]) | |
318 avctx->release_buffer(avctx, &c->pic); | |
319 | |
320 return 0; | |
321 } | |
322 | |
323 AVCodec dxa_decoder = { | |
324 "dxa", | |
325 CODEC_TYPE_VIDEO, | |
326 CODEC_ID_DXA, | |
327 sizeof(DxaDecContext), | |
328 decode_init, | |
329 NULL, | |
330 decode_end, | |
331 decode_frame | |
332 }; | |
333 |