FFmpeg
Loading...
Searching...
No Matches
xxan.c
Go to the documentation of this file.
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
24#include "libavutil/mem.h"
25
26#include "avcodec.h"
27#include "bytestream.h"
28#include "codec_internal.h"
29#include "decode.h"
30
31typedef struct XanContext {
32 AVCodecContext *avctx;
34
35 uint8_t *y_buffer;
40
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
54{
55 XanContext *s = avctx->priv_data;
56
57 s->avctx = avctx;
58
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_mallocz(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
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 */
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
180static 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
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
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
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
402static 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
437 .p.name = "xan_wc4",
438 CODEC_LONG_NAME("Wing Commander IV / Xxan"),
439 .p.type = AVMEDIA_TYPE_VIDEO,
440 .p.id = AV_CODEC_ID_XAN_WC4,
441 .priv_data_size = sizeof(XanContext),
445 .p.capabilities = AV_CODEC_CAP_DR1,
446 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
447};
#define ftype
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
Definition dsp.h:87
static double val(void *priv, double ch)
Definition aeval.c:77
const FFCodec ff_xan_wc4_decoder
Definition xxan.c:436
static av_cold void close(AVCodecParserContext *s)
Definition apv_parser.c:197
#define U(x)
Definition vpx_arith.h:37
Libavcodec external API header.
#define V
Definition avdct.c:32
static av_always_inline unsigned int bytestream2_get_buffer(GetByteContext *g, uint8_t *dst, unsigned int size)
Definition bytestream.h:267
static av_always_inline int bytestream2_get_bytes_left(const GetByteContext *g)
Definition bytestream.h:158
static av_always_inline void bytestream2_init(GetByteContext *g, const uint8_t *buf, int buf_size)
Definition bytestream.h:137
static av_always_inline void bytestream2_skip(GetByteContext *g, unsigned int size)
Definition bytestream.h:168
static av_always_inline int bytestream2_seek(GetByteContext *g, int offset, int whence)
Definition bytestream.h:212
static av_always_inline int bytestream2_tell(const GetByteContext *g)
Definition bytestream.h:192
static av_always_inline int bytestream2_size(const GetByteContext *g)
Definition bytestream.h:202
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define bit(string, value)
Definition cbs_mpeg2.c:56
#define s(width, name)
Definition cbs_vp9.c:198
#define FF_CODEC_DECODE_CB(func)
#define CODEC_LONG_NAME(str)
#define FF_CODEC_CAP_INIT_CLEANUP
The codec allows calling the close function for deallocation even if the init function returned a fai...
int ff_reget_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Identical in function to ff_get_buffer(), except it reuses the existing buffer if available.
Definition decode.c:1906
int(* init)(AVBSFContext *ctx)
Definition dts2pts.c:608
CheckasmFuncTree tree
Definition checkasm.c:79
static const uint8_t bits[8]
Definition fastaudio.c:100
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
Definition codec.h:49
@ AV_CODEC_ID_XAN_WC4
Definition codec_id.h:91
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition error.h:61
#define AVERROR(e)
Definition error.h:45
int av_frame_ref(AVFrame *dst, const AVFrame *src)
Set up a new reference to the data described by the source frame.
Definition frame.c:278
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition frame.c:64
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition frame.c:52
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition log.h:216
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
void av_memcpy_backptr(uint8_t *dst, int back, int cnt)
Overlapping memcpy() implementation.
Definition mem.c:445
@ AVMEDIA_TYPE_VIDEO
Definition avutil.h:200
#define AV_RL16(p)
unsigned offset
Definition libaomenc.c:763
#define av_cold
Definition attributes.h:117
static const uint16_t mask[17]
Definition lzw.c:38
#define FFMIN(a, b)
Definition macros.h:49
Memory handling functions.
#define av_malloc(s)
Definition ops_static.c:52
@ AV_PIX_FMT_YUV420P
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition pixfmt.h:73
static const uint16_t table[]
Definition prosumer.c:203
main external API structure.
Definition avcodec.h:443
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition avcodec.h:643
int width
picture width / height.
Definition avcodec.h:604
void * priv_data
Definition avcodec.h:470
This structure describes decoded (raw) audio or video data.
Definition frame.h:472
This structure stores compressed data.
Definition packet.h:580
int size
Definition packet.h:604
uint8_t * data
Definition packet.h:603
uint8_t * scratch_buffer
Definition xxan.c:36
int buffer_size
Definition xxan.c:37
GetByteContext gb
Definition xxan.c:38
AVCodecContext * avctx
Definition xan.c:54
AVFrame * pic
Definition xxan.c:33
uint8_t * y_buffer
Definition xxan.c:35
Definition swscale.c:71
#define av_mallocz(s)
#define av_freep(p)
#define av_log(a,...)
#define src
Definition vp8dsp.c:248
static void finish(void)
Definition movenc.c:374
int size
static av_cold int xan_decode_init(AVCodecContext *avctx)
Definition xan.c:87
static int xan_decode_frame(AVCodecContext *avctx, AVFrame *frame, int *got_frame, AVPacket *avpkt)
Definition xan.c:539
static av_cold int xan_decode_end(AVCodecContext *avctx)
Definition xan.c:74
static int xan_decode_frame_type1(AVCodecContext *avctx)
Definition xxan.c:357
static av_cold int xan_decode_init(AVCodecContext *avctx)
Definition xxan.c:53
static int xan_unpack_luma(XanContext *s, uint8_t *dst, const int dst_size)
Definition xxan.c:85
static int xan_unpack(XanContext *s, uint8_t *dest, const int dest_len)
Definition xxan.c:127
static int xan_decode_chroma(AVCodecContext *avctx, unsigned chroma_off)
Definition xxan.c:180
static int xan_decode_frame(AVCodecContext *avctx, AVFrame *rframe, int *got_frame, AVPacket *avpkt)
Definition xxan.c:402
static av_cold int xan_decode_end(AVCodecContext *avctx)
Definition xxan.c:41
static int xan_decode_frame_type0(AVCodecContext *avctx)
Definition xxan.c:278