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
27 #define BITSTREAM_READER_LE
30
34
40
42 {
44
46
49
50 return 0;
51 }
52
54 {
56
58
60
64 }
65 if (avctx->
width & 1) {
68 }
69
78 }
79
84 }
85
86 return 0;
87 }
88
90 uint8_t *dst,
const int dst_size)
91 {
92 int tree_size, eof;
94 int tree_root, node;
95 const uint8_t *dst_end = dst + dst_size;
98
99 tree_size = bytestream2_get_byte(&s->
gb);
100 eof = bytestream2_get_byte(&s->
gb);
101 tree_root = eof + tree_size;
103
104 node = tree_root;
105 bits = bytestream2_get_byte(&s->
gb);
106 mask = 0x80;
107 for (;;) {
108 int bit = !!(bits &
mask);
109 mask >>= 1;
111 node = bytestream2_get_byte(&tree);
112 if (node == eof)
113 break;
114 if (node < eof) {
115 *dst++ = node;
116 if (dst > dst_end)
117 break;
118 node = tree_root;
119 }
120 if (!mask) {
122 break;
123 bits = bytestream2_get_byteu(&s->
gb);
124 mask = 0x80;
125 }
126 }
128 }
129
130 /* almost the same as in xan_wc3 decoder */
132 uint8_t *dest,
const int dest_len)
133 {
137 const uint8_t *dest_end = dest + dest_len;
138
139 while (dest < dest_end) {
142
143 opcode = bytestream2_get_byteu(&s->
gb);
144
145 if (opcode < 0xe0) {
146 int size2, back;
147 if ((opcode & 0x80) == 0) {
148 size = opcode & 3;
149 back = ((opcode & 0x60) << 3) + bytestream2_get_byte(&s->
gb) + 1;
150 size2 = ((opcode & 0x1c) >> 2) + 3;
151 } else if ((opcode & 0x40) == 0) {
152 size = bytestream2_peek_byte(&s->
gb) >> 6;
153 back = (bytestream2_get_be16(&s->
gb) & 0x3fff) + 1;
154 size2 = (opcode & 0x3f) + 4;
155 } else {
156 size = opcode & 3;
157 back = ((opcode & 0x10) << 12) + bytestream2_get_be16(&s->
gb) + 1;
158 size2 = ((opcode & 0x0c) << 6) + bytestream2_get_byte(&s->
gb) + 5;
159 if (size + size2 > dest_end - dest)
160 break;
161 }
162 if (dest + size + size2 > dest_end ||
163 dest - orig_dest + size < back)
168 dest += size2;
169 } else {
170 int finish = opcode >= 0xfc;
171
172 size = finish ? opcode & 3 : ((opcode & 0x1f) << 2) + 4;
173 if (dest_end - dest < size)
177 if (finish)
178 break;
179 }
180 }
181 return dest - orig_dest;
182 }
183
185 {
189 int i, j;
193
194 if (!chroma_off)
195 return 0;
199 }
201 mode = bytestream2_get_le16(&s->
gb);
203 table_size = bytestream2_get_le16(&s->
gb);
204 offset = table_size * 2;
205 table_size += 1;
206
210 }
211
215 if (dec_size < 0) {
217 return dec_size;
218 }
219
223 src_end = src + dec_size;
224 if (mode) {
225 for (j = 0; j < avctx->
height >> 1; j++) {
226 for (i = 0; i < avctx->
width >> 1; i++) {
227 if (src_end - src < 1)
228 return 0;
229 val = *src++;
230 if (val) {
231 if (val >= table_size)
233 val =
AV_RL16(table + (val << 1));
234 uval = (val >> 3) & 0xF8;
235 vval = (val >> 8) & 0xF8;
236 U[i] = uval | (uval >> 5);
237 V[i] = vval | (vval >> 5);
238 }
239 }
242 }
246 }
247 } else {
250
251 for (j = 0; j < avctx->
height >> 2; j++) {
252 for (i = 0; i < avctx->
width >> 1; i += 2) {
253 if (src_end - src < 1)
254 return 0;
255 val = *src++;
256 if (val) {
257 if (val >= table_size)
259 val =
AV_RL16(table + (val << 1));
260 uval = (val >> 3) & 0xF8;
261 vval = (val >> 8) & 0xF8;
262 U[i] = U[i+1] = U2[i] = U2[i+1] = uval | (uval >> 5);
263 V[i] = V[i+1] = V2[i] = V2[i+1] = vval | (vval >> 5);
264 }
265 }
270 }
272 int lines = ((avctx->
height + 1) >> 1) - (avctx->
height >> 2) * 2;
273
276 }
277 }
278
279 return 0;
280 }
281
283 {
286 unsigned chroma_off, corr_off;
287 int cur, last;
288 int i, j;
290
291 chroma_off = bytestream2_get_le32(&s->
gb);
292 corr_off = bytestream2_get_le32(&s->
gb);
293
296
299 corr_off = 0;
300 }
303 if (ret) {
306 }
307
309 last = *src++;
310 ybuf[0] = last << 1;
311 for (j = 1; j < avctx->
width - 1; j += 2) {
312 cur = (last + *src++) & 0x1F;
313 ybuf[j] = last + cur;
314 ybuf[j+1] = cur << 1;
315 last = cur;
316 }
317 ybuf[j] = last << 1;
318 prev_buf = ybuf;
319 ybuf += avctx->
width;
320
321 for (i = 1; i < avctx->
height; i++) {
322 last = ((prev_buf[0] >> 1) + *src++) & 0x1F;
323 ybuf[0] = last << 1;
324 for (j = 1; j < avctx->
width - 1; j += 2) {
325 cur = ((prev_buf[j + 1] >> 1) + *src++) & 0x1F;
326 ybuf[j] = last + cur;
327 ybuf[j+1] = cur << 1;
328 last = cur;
329 }
330 ybuf[j] = last << 1;
331 prev_buf = ybuf;
332 ybuf += avctx->
width;
333 }
334
335 if (corr_off) {
336 int dec_size;
337
340 if (dec_size < 0)
341 dec_size = 0;
342 else
344
345 for (i = 0; i < dec_size; i++)
347 }
348
351 for (j = 0; j < avctx->
height; j++) {
352 for (i = 0; i < avctx->
width; i++)
353 ybuf[i] = (src[i] << 2) | (src[i] >> 3);
356 }
357
358 return 0;
359 }
360
362 {
365 int cur, last;
366 int i, j;
368
371
375 if (ret) {
378 }
379
381 for (i = 0; i < avctx->
height; i++) {
382 last = (ybuf[0] + (*src++ << 1)) & 0x3F;
383 ybuf[0] = last;
384 for (j = 1; j < avctx->
width - 1; j += 2) {
385 cur = (ybuf[j + 1] + (*src++ << 1)) & 0x3F;
386 ybuf[j] = (last + cur) >> 1;
387 ybuf[j+1] = cur;
388 last = cur;
389 }
390 ybuf[j] = last;
391 ybuf += avctx->
width;
392 }
393
396 for (j = 0; j < avctx->
height; j++) {
397 for (i = 0; i < avctx->
width; i++)
398 ybuf[i] = (src[i] << 2) | (src[i] >> 3);
401 }
402
403 return 0;
404 }
405
407 void *
data,
int *got_frame,
409 {
411 int ftype;
413
416
418 ftype = bytestream2_get_le32(&s->
gb);
419 switch (ftype) {
420 case 0:
422 break;
423 case 1:
425 break;
426 default:
429 }
430 if (ret)
432
435
436 *got_frame = 1;
437
439 }
440
451 };