1 /*
2 * Wing Commander/Xan Video Decoder
3 * Copyright (C) 2011 Konstantin Shishkov
4 * based on work by Mike Melanson
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 #include "libavutil/intreadwrite.h"
24 #include "libavutil/mem.h"
25
26 #include "avcodec.h"
27 #include "bytestream.h"
28 #include "codec_internal.h"
29 #include "internal.h"
30
31 typedef struct XanContext {
32 AVCodecContext *avctx;
33 AVFrame *pic;
34
35 uint8_t *y_buffer;
36 uint8_t *scratch_buffer;
37 int buffer_size;
38 GetByteContext gb;
39 } XanContext;
40
xan_decode_end(AVCodecContext * avctx)41 static av_cold int xan_decode_end(AVCodecContext *avctx)
42 {
43 XanContext *s = avctx->priv_data;
44
45 av_frame_free(&s->pic);
46
47 av_freep(&s->y_buffer);
48 av_freep(&s->scratch_buffer);
49
50 return 0;
51 }
52
xan_decode_init(AVCodecContext * avctx)53 static av_cold int xan_decode_init(AVCodecContext *avctx)
54 {
55 XanContext *s = avctx->priv_data;
56
57 s->avctx = avctx;
58
59 avctx->pix_fmt = AV_PIX_FMT_YUV420P;
60
61 if (avctx->height < 8) {
62 av_log(avctx, AV_LOG_ERROR, "Invalid frame height: %d.\n", avctx->height);
63 return AVERROR(EINVAL);
64 }
65 if (avctx->width & 1) {
66 av_log(avctx, AV_LOG_ERROR, "Invalid frame width: %d.\n", avctx->width);
67 return AVERROR(EINVAL);
68 }
69
70 s->buffer_size = avctx->width * avctx->height;
71 s->y_buffer = av_malloc(s->buffer_size);
72 if (!s->y_buffer)
73 return AVERROR(ENOMEM);
74 s->scratch_buffer = av_malloc(s->buffer_size + 130);
75 if (!s->scratch_buffer)
76 return AVERROR(ENOMEM);
77
78 s->pic = av_frame_alloc();
79 if (!s->pic)
80 return AVERROR(ENOMEM);
81
82 return 0;
83 }
84
xan_unpack_luma(XanContext * s,uint8_t * dst,const int dst_size)85 static int xan_unpack_luma(XanContext *s,
86 uint8_t *dst, const int dst_size)
87 {
88 int tree_size, eof;
89 int bits, mask;
90 int tree_root, node;
91 const uint8_t *dst_end = dst + dst_size;
92 GetByteContext tree = s->gb;
93 int start_off = bytestream2_tell(&tree);
94
95 tree_size = bytestream2_get_byte(&s->gb);
96 eof = bytestream2_get_byte(&s->gb);
97 tree_root = eof + tree_size;
98 bytestream2_skip(&s->gb, tree_size * 2);
99
100 node = tree_root;
101 bits = bytestream2_get_byte(&s->gb);
102 mask = 0x80;
103 for (;;) {
104 int bit = !!(bits & mask);
105 mask >>= 1;
106 bytestream2_seek(&tree, start_off + node*2 + bit - eof * 2, SEEK_SET);
107 node = bytestream2_get_byte(&tree);
108 if (node == eof)
109 break;
110 if (node < eof) {
111 *dst++ = node;
112 if (dst > dst_end)
113 break;
114 node = tree_root;
115 }
116 if (!mask) {
117 if (bytestream2_get_bytes_left(&s->gb) <= 0)
118 break;
119 bits = bytestream2_get_byteu(&s->gb);
120 mask = 0x80;
121 }
122 }
123 return dst != dst_end ? AVERROR_INVALIDDATA : 0;
124 }
125
126 /* almost the same as in xan_wc3 decoder */
xan_unpack(XanContext * s,uint8_t * dest,const int dest_len)127 static int xan_unpack(XanContext *s,
128 uint8_t *dest, const int dest_len)
129 {
130 uint8_t opcode;
131 int size;
132 uint8_t *orig_dest = dest;
133 const uint8_t *dest_end = dest + dest_len;
134
135 while (dest < dest_end) {
136 if (bytestream2_get_bytes_left(&s->gb) <= 0)
137 return AVERROR_INVALIDDATA;
138
139 opcode = bytestream2_get_byteu(&s->gb);
140
141 if (opcode < 0xe0) {
142 int size2, back;
143 if ((opcode & 0x80) == 0) {
144 size = opcode & 3;
145 back = ((opcode & 0x60) << 3) + bytestream2_get_byte(&s->gb) + 1;
146 size2 = ((opcode & 0x1c) >> 2) + 3;
147 } else if ((opcode & 0x40) == 0) {
148 size = bytestream2_peek_byte(&s->gb) >> 6;
149 back = (bytestream2_get_be16(&s->gb) & 0x3fff) + 1;
150 size2 = (opcode & 0x3f) + 4;
151 } else {
152 size = opcode & 3;
153 back = ((opcode & 0x10) << 12) + bytestream2_get_be16(&s->gb) + 1;
154 size2 = ((opcode & 0x0c) << 6) + bytestream2_get_byte(&s->gb) + 5;
155 if (size + size2 > dest_end - dest)
156 break;
157 }
158 if (dest + size + size2 > dest_end ||
159 dest - orig_dest + size < back)
160 return AVERROR_INVALIDDATA;
161 bytestream2_get_buffer(&s->gb, dest, size);
162 dest += size;
163 av_memcpy_backptr(dest, back, size2);
164 dest += size2;
165 } else {
166 int finish = opcode >= 0xfc;
167
168 size = finish ? opcode & 3 : ((opcode & 0x1f) << 2) + 4;
169 if (dest_end - dest < size)
170 return AVERROR_INVALIDDATA;
171 bytestream2_get_buffer(&s->gb, dest, size);
172 dest += size;
173 if (finish)
174 break;
175 }
176 }
177 return dest - orig_dest;
178 }
179
xan_decode_chroma(AVCodecContext * avctx,unsigned chroma_off)180 static int xan_decode_chroma(AVCodecContext *avctx, unsigned chroma_off)
181 {
182 XanContext *s = avctx->priv_data;
183 uint8_t *U, *V;
184 int val, uval, vval;
185 int i, j;
186 const uint8_t *src, *src_end;
187 const uint8_t *table;
188 int mode, offset, dec_size, table_size;
189
190 if (!chroma_off)
191 return 0;
192 if (chroma_off + 4 >= bytestream2_get_bytes_left(&s->gb)) {
193 av_log(avctx, AV_LOG_ERROR, "Invalid chroma block position\n");
194 return AVERROR_INVALIDDATA;
195 }
196 bytestream2_seek(&s->gb, chroma_off + 4, SEEK_SET);
197 mode = bytestream2_get_le16(&s->gb);
198 table = s->gb.buffer;
199 table_size = bytestream2_get_le16(&s->gb);
200 offset = table_size * 2;
201 table_size += 1;
202
203 if (offset >= bytestream2_get_bytes_left(&s->gb)) {
204 av_log(avctx, AV_LOG_ERROR, "Invalid chroma block offset\n");
205 return AVERROR_INVALIDDATA;
206 }
207
208 bytestream2_skip(&s->gb, offset);
209 memset(s->scratch_buffer, 0, s->buffer_size);
210 dec_size = xan_unpack(s, s->scratch_buffer, s->buffer_size);
211 if (dec_size < 0) {
212 av_log(avctx, AV_LOG_ERROR, "Chroma unpacking failed\n");
213 return dec_size;
214 }
215
216 U = s->pic->data[1];
217 V = s->pic->data[2];
218 src = s->scratch_buffer;
219 src_end = src + dec_size;
220 if (mode) {
221 for (j = 0; j < avctx->height >> 1; j++) {
222 for (i = 0; i < avctx->width >> 1; i++) {
223 if (src_end - src < 1)
224 return 0;
225 val = *src++;
226 if (val) {
227 if (val >= table_size)
228 return AVERROR_INVALIDDATA;
229 val = AV_RL16(table + (val << 1));
230 uval = (val >> 3) & 0xF8;
231 vval = (val >> 8) & 0xF8;
232 U[i] = uval | (uval >> 5);
233 V[i] = vval | (vval >> 5);
234 }
235 }
236 U += s->pic->linesize[1];
237 V += s->pic->linesize[2];
238 }
239 if (avctx->height & 1) {
240 memcpy(U, U - s->pic->linesize[1], avctx->width >> 1);
241 memcpy(V, V - s->pic->linesize[2], avctx->width >> 1);
242 }
243 } else {
244 uint8_t *U2 = U + s->pic->linesize[1];
245 uint8_t *V2 = V + s->pic->linesize[2];
246
247 for (j = 0; j < avctx->height >> 2; j++) {
248 for (i = 0; i < avctx->width >> 1; i += 2) {
249 if (src_end - src < 1)
250 return 0;
251 val = *src++;
252 if (val) {
253 if (val >= table_size)
254 return AVERROR_INVALIDDATA;
255 val = AV_RL16(table + (val << 1));
256 uval = (val >> 3) & 0xF8;
257 vval = (val >> 8) & 0xF8;
258 U[i] = U[i+1] = U2[i] = U2[i+1] = uval | (uval >> 5);
259 V[i] = V[i+1] = V2[i] = V2[i+1] = vval | (vval >> 5);
260 }
261 }
262 U += s->pic->linesize[1] * 2;
263 V += s->pic->linesize[2] * 2;
264 U2 += s->pic->linesize[1] * 2;
265 V2 += s->pic->linesize[2] * 2;
266 }
267 if (avctx->height & 3) {
268 int lines = ((avctx->height + 1) >> 1) - (avctx->height >> 2) * 2;
269
270 memcpy(U, U - lines * s->pic->linesize[1], lines * s->pic->linesize[1]);
271 memcpy(V, V - lines * s->pic->linesize[2], lines * s->pic->linesize[2]);
272 }
273 }
274
275 return 0;
276 }
277
xan_decode_frame_type0(AVCodecContext * avctx)278 static int xan_decode_frame_type0(AVCodecContext *avctx)
279 {
280 XanContext *s = avctx->priv_data;
281 uint8_t *ybuf, *prev_buf, *src = s->scratch_buffer;
282 unsigned chroma_off, corr_off;
283 int cur, last;
284 int i, j;
285 int ret;
286
287 chroma_off = bytestream2_get_le32(&s->gb);
288 corr_off = bytestream2_get_le32(&s->gb);
289
290 if ((ret = xan_decode_chroma(avctx, chroma_off)) != 0)
291 return ret;
292
293 if (corr_off >= bytestream2_size(&s->gb)) {
294 av_log(avctx, AV_LOG_WARNING, "Ignoring invalid correction block position\n");
295 corr_off = 0;
296 }
297 bytestream2_seek(&s->gb, 12, SEEK_SET);
298 ret = xan_unpack_luma(s, src, s->buffer_size >> 1);
299 if (ret) {
300 av_log(avctx, AV_LOG_ERROR, "Luma decoding failed\n");
301 return ret;
302 }
303
304 ybuf = s->y_buffer;
305 last = *src++;
306 ybuf[0] = last << 1;
307 for (j = 1; j < avctx->width - 1; j += 2) {
308 cur = (last + *src++) & 0x1F;
309 ybuf[j] = last + cur;
310 ybuf[j+1] = cur << 1;
311 last = cur;
312 }
313 ybuf[j] = last << 1;
314 prev_buf = ybuf;
315 ybuf += avctx->width;
316
317 for (i = 1; i < avctx->height; i++) {
318 last = ((prev_buf[0] >> 1) + *src++) & 0x1F;
319 ybuf[0] = last << 1;
320 for (j = 1; j < avctx->width - 1; j += 2) {
321 cur = ((prev_buf[j + 1] >> 1) + *src++) & 0x1F;
322 ybuf[j] = last + cur;
323 ybuf[j+1] = cur << 1;
324 last = cur;
325 }
326 ybuf[j] = last << 1;
327 prev_buf = ybuf;
328 ybuf += avctx->width;
329 }
330
331 if (corr_off) {
332 int dec_size;
333
334 bytestream2_seek(&s->gb, 8 + corr_off, SEEK_SET);
335 dec_size = xan_unpack(s, s->scratch_buffer, s->buffer_size / 2);
336 if (dec_size < 0)
337 dec_size = 0;
338 else
339 dec_size = FFMIN(dec_size, s->buffer_size/2 - 1);
340
341 for (i = 0; i < dec_size; i++)
342 s->y_buffer[i*2+1] = (s->y_buffer[i*2+1] + (s->scratch_buffer[i] << 1)) & 0x3F;
343 }
344
345 src = s->y_buffer;
346 ybuf = s->pic->data[0];
347 for (j = 0; j < avctx->height; j++) {
348 for (i = 0; i < avctx->width; i++)
349 ybuf[i] = (src[i] << 2) | (src[i] >> 3);
350 src += avctx->width;
351 ybuf += s->pic->linesize[0];
352 }
353
354 return 0;
355 }
356
xan_decode_frame_type1(AVCodecContext * avctx)357 static int xan_decode_frame_type1(AVCodecContext *avctx)
358 {
359 XanContext *s = avctx->priv_data;
360 uint8_t *ybuf, *src = s->scratch_buffer;
361 int cur, last;
362 int i, j;
363 int ret;
364
365 if ((ret = xan_decode_chroma(avctx, bytestream2_get_le32(&s->gb))) != 0)
366 return ret;
367
368 bytestream2_seek(&s->gb, 16, SEEK_SET);
369 ret = xan_unpack_luma(s, src,
370 s->buffer_size >> 1);
371 if (ret) {
372 av_log(avctx, AV_LOG_ERROR, "Luma decoding failed\n");
373 return ret;
374 }
375
376 ybuf = s->y_buffer;
377 for (i = 0; i < avctx->height; i++) {
378 last = (ybuf[0] + (*src++ << 1)) & 0x3F;
379 ybuf[0] = last;
380 for (j = 1; j < avctx->width - 1; j += 2) {
381 cur = (ybuf[j + 1] + (*src++ << 1)) & 0x3F;
382 ybuf[j] = (last + cur) >> 1;
383 ybuf[j+1] = cur;
384 last = cur;
385 }
386 ybuf[j] = last;
387 ybuf += avctx->width;
388 }
389
390 src = s->y_buffer;
391 ybuf = s->pic->data[0];
392 for (j = 0; j < avctx->height; j++) {
393 for (i = 0; i < avctx->width; i++)
394 ybuf[i] = (src[i] << 2) | (src[i] >> 3);
395 src += avctx->width;
396 ybuf += s->pic->linesize[0];
397 }
398
399 return 0;
400 }
401
xan_decode_frame(AVCodecContext * avctx,AVFrame * rframe,int * got_frame,AVPacket * avpkt)402 static int xan_decode_frame(AVCodecContext *avctx, AVFrame *rframe,
403 int *got_frame, AVPacket *avpkt)
404 {
405 XanContext *s = avctx->priv_data;
406 int ftype;
407 int ret;
408
409 if ((ret = ff_reget_buffer(avctx, s->pic, 0)) < 0)
410 return ret;
411
412 bytestream2_init(&s->gb, avpkt->data, avpkt->size);
413 ftype = bytestream2_get_le32(&s->gb);
414 switch (ftype) {
415 case 0:
416 ret = xan_decode_frame_type0(avctx);
417 break;
418 case 1:
419 ret = xan_decode_frame_type1(avctx);
420 break;
421 default:
422 av_log(avctx, AV_LOG_ERROR, "Unknown frame type %d\n", ftype);
423 return AVERROR_INVALIDDATA;
424 }
425 if (ret)
426 return ret;
427
428 if ((ret = av_frame_ref(rframe, s->pic)) < 0)
429 return ret;
430
431 *got_frame = 1;
432
433 return avpkt->size;
434 }
435
436 const FFCodec ff_xan_wc4_decoder = {
437 .p.name = "xan_wc4",
438 .p.long_name = NULL_IF_CONFIG_SMALL("Wing Commander IV / Xxan"),
439 .p.type = AVMEDIA_TYPE_VIDEO,
440 .p.id = AV_CODEC_ID_XAN_WC4,
441 .priv_data_size = sizeof(XanContext),
442 .init = xan_decode_init,
443 .close = xan_decode_end,
444 FF_CODEC_DECODE_CB(xan_decode_frame),
445 .p.capabilities = AV_CODEC_CAP_DR1,
446 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP | FF_CODEC_CAP_INIT_THREADSAFE,
447 };
448