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 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 #include "avcodec.h"
24 #include "libavutil/intreadwrite.h"
25 #include "bytestream.h"
26 #define BITSTREAM_READER_LE
27 #include "get_bits.h"
28 // for av_memcpy_backptr
29 #include "libavutil/lzo.h"
30 
31 typedef struct XanContext {
34 
35  uint8_t *y_buffer;
36  uint8_t *scratch_buffer;
39 } XanContext;
40 
42 {
43  XanContext *s = avctx->priv_data;
44 
45  s->avctx = avctx;
46 
47  avctx->pix_fmt = PIX_FMT_YUV420P;
48 
49  if (avctx->width & 1) {
50  av_log(avctx, AV_LOG_ERROR, "Invalid frame width: %d.\n", avctx->width);
51  return AVERROR(EINVAL);
52  }
53 
54  s->buffer_size = avctx->width * avctx->height;
56  if (!s->y_buffer)
57  return AVERROR(ENOMEM);
58  s->scratch_buffer = av_malloc(s->buffer_size + 130);
59  if (!s->scratch_buffer) {
60  av_freep(&s->y_buffer);
61  return AVERROR(ENOMEM);
62  }
63 
64  return 0;
65 }
66 
68  uint8_t *dst, const int dst_size)
69 {
70  int tree_size, eof;
71  int bits, mask;
72  int tree_root, node;
73  const uint8_t *dst_end = dst + dst_size;
74  GetByteContext tree = s->gb;
75  int start_off = bytestream2_tell(&tree);
76 
77  tree_size = bytestream2_get_byte(&s->gb);
78  eof = bytestream2_get_byte(&s->gb);
79  tree_root = eof + tree_size;
80  bytestream2_skip(&s->gb, tree_size * 2);
81 
82  node = tree_root;
83  bits = bytestream2_get_byte(&s->gb);
84  mask = 0x80;
85  for (;;) {
86  int bit = !!(bits & mask);
87  mask >>= 1;
88  bytestream2_seek(&tree, start_off + node*2 + bit - eof * 2, SEEK_SET);
89  node = bytestream2_get_byte(&tree);
90  if (node == eof)
91  break;
92  if (node < eof) {
93  *dst++ = node;
94  if (dst > dst_end)
95  break;
96  node = tree_root;
97  }
98  if (!mask) {
99  if (bytestream2_get_bytes_left(&s->gb) <= 0)
100  break;
101  bits = bytestream2_get_byteu(&s->gb);
102  mask = 0x80;
103  }
104  }
105  return dst != dst_end ? AVERROR_INVALIDDATA : 0;
106 }
107 
108 /* almost the same as in xan_wc3 decoder */
109 static int xan_unpack(XanContext *s,
110  uint8_t *dest, const int dest_len)
111 {
112  uint8_t opcode;
113  int size;
114  uint8_t *orig_dest = dest;
115  const uint8_t *dest_end = dest + dest_len;
116 
117  while (dest < dest_end) {
118  if (bytestream2_get_bytes_left(&s->gb) <= 0)
119  return AVERROR_INVALIDDATA;
120 
121  opcode = bytestream2_get_byteu(&s->gb);
122 
123  if (opcode < 0xe0) {
124  int size2, back;
125  if ((opcode & 0x80) == 0) {
126  size = opcode & 3;
127  back = ((opcode & 0x60) << 3) + bytestream2_get_byte(&s->gb) + 1;
128  size2 = ((opcode & 0x1c) >> 2) + 3;
129  } else if ((opcode & 0x40) == 0) {
130  size = bytestream2_peek_byte(&s->gb) >> 6;
131  back = (bytestream2_get_be16(&s->gb) & 0x3fff) + 1;
132  size2 = (opcode & 0x3f) + 4;
133  } else {
134  size = opcode & 3;
135  back = ((opcode & 0x10) << 12) + bytestream2_get_be16(&s->gb) + 1;
136  size2 = ((opcode & 0x0c) << 6) + bytestream2_get_byte(&s->gb) + 5;
137  if (size + size2 > dest_end - dest)
138  break;
139  }
140  if (dest + size + size2 > dest_end ||
141  dest - orig_dest + size < back)
142  return -1;
143  bytestream2_get_buffer(&s->gb, dest, size);
144  dest += size;
145  av_memcpy_backptr(dest, back, size2);
146  dest += size2;
147  } else {
148  int finish = opcode >= 0xfc;
149 
150  size = finish ? opcode & 3 : ((opcode & 0x1f) << 2) + 4;
151  if (dest_end - dest < size)
152  return -1;
153  bytestream2_get_buffer(&s->gb, dest, size);
154  dest += size;
155  if (finish)
156  break;
157  }
158  }
159  return dest - orig_dest;
160 }
161 
162 static int xan_decode_chroma(AVCodecContext *avctx, unsigned chroma_off)
163 {
164  XanContext *s = avctx->priv_data;
165  uint8_t *U, *V;
166  int val, uval, vval;
167  int i, j;
168  const uint8_t *src, *src_end;
169  const uint8_t *table;
170  int mode, offset, dec_size, table_size;
171 
172  if (!chroma_off)
173  return 0;
174  if (chroma_off + 4 >= bytestream2_get_bytes_left(&s->gb)) {
175  av_log(avctx, AV_LOG_ERROR, "Invalid chroma block position\n");
176  return -1;
177  }
178  bytestream2_seek(&s->gb, chroma_off + 4, SEEK_SET);
179  mode = bytestream2_get_le16(&s->gb);
180  table = s->gb.buffer;
181  table_size = bytestream2_get_le16(&s->gb);
182  offset = table_size * 2;
183  table_size += 1;
184 
185  if (offset >= bytestream2_get_bytes_left(&s->gb)) {
186  av_log(avctx, AV_LOG_ERROR, "Invalid chroma block offset\n");
187  return -1;
188  }
189 
190  bytestream2_skip(&s->gb, offset);
191  memset(s->scratch_buffer, 0, s->buffer_size);
192  dec_size = xan_unpack(s, s->scratch_buffer, s->buffer_size);
193  if (dec_size < 0) {
194  av_log(avctx, AV_LOG_ERROR, "Chroma unpacking failed\n");
195  return -1;
196  }
197 
198  U = s->pic.data[1];
199  V = s->pic.data[2];
200  src = s->scratch_buffer;
201  src_end = src + dec_size;
202  if (mode) {
203  for (j = 0; j < avctx->height >> 1; j++) {
204  for (i = 0; i < avctx->width >> 1; i++) {
205  val = *src++;
206  if (val && val < table_size) {
207  val = AV_RL16(table + (val << 1));
208  uval = (val >> 3) & 0xF8;
209  vval = (val >> 8) & 0xF8;
210  U[i] = uval | (uval >> 5);
211  V[i] = vval | (vval >> 5);
212  }
213  if (src == src_end)
214  return 0;
215  }
216  U += s->pic.linesize[1];
217  V += s->pic.linesize[2];
218  }
219  } else {
220  uint8_t *U2 = U + s->pic.linesize[1];
221  uint8_t *V2 = V + s->pic.linesize[2];
222 
223  for (j = 0; j < avctx->height >> 2; j++) {
224  for (i = 0; i < avctx->width >> 1; i += 2) {
225  val = *src++;
226  if (val && val < table_size) {
227  val = AV_RL16(table + (val << 1));
228  uval = (val >> 3) & 0xF8;
229  vval = (val >> 8) & 0xF8;
230  U[i] = U[i+1] = U2[i] = U2[i+1] = uval | (uval >> 5);
231  V[i] = V[i+1] = V2[i] = V2[i+1] = vval | (vval >> 5);
232  }
233  }
234  U += s->pic.linesize[1] * 2;
235  V += s->pic.linesize[2] * 2;
236  U2 += s->pic.linesize[1] * 2;
237  V2 += s->pic.linesize[2] * 2;
238  }
239  }
240 
241  return 0;
242 }
243 
245 {
246  XanContext *s = avctx->priv_data;
247  uint8_t *ybuf, *prev_buf, *src = s->scratch_buffer;
248  unsigned chroma_off, corr_off;
249  int cur, last;
250  int i, j;
251  int ret;
252 
253  chroma_off = bytestream2_get_le32(&s->gb);
254  corr_off = bytestream2_get_le32(&s->gb);
255 
256  if ((ret = xan_decode_chroma(avctx, chroma_off)) != 0)
257  return ret;
258 
259  if (corr_off >= (s->gb.buffer_end - s->gb.buffer_start)) {
260  av_log(avctx, AV_LOG_WARNING, "Ignoring invalid correction block position\n");
261  corr_off = 0;
262  }
263  bytestream2_seek(&s->gb, 12, SEEK_SET);
264  ret = xan_unpack_luma(s, src, s->buffer_size >> 1);
265  if (ret) {
266  av_log(avctx, AV_LOG_ERROR, "Luma decoding failed\n");
267  return ret;
268  }
269 
270  ybuf = s->y_buffer;
271  last = *src++;
272  ybuf[0] = last << 1;
273  for (j = 1; j < avctx->width - 1; j += 2) {
274  cur = (last + *src++) & 0x1F;
275  ybuf[j] = last + cur;
276  ybuf[j+1] = cur << 1;
277  last = cur;
278  }
279  ybuf[j] = last << 1;
280  prev_buf = ybuf;
281  ybuf += avctx->width;
282 
283  for (i = 1; i < avctx->height; i++) {
284  last = ((prev_buf[0] >> 1) + *src++) & 0x1F;
285  ybuf[0] = last << 1;
286  for (j = 1; j < avctx->width - 1; j += 2) {
287  cur = ((prev_buf[j + 1] >> 1) + *src++) & 0x1F;
288  ybuf[j] = last + cur;
289  ybuf[j+1] = cur << 1;
290  last = cur;
291  }
292  ybuf[j] = last << 1;
293  prev_buf = ybuf;
294  ybuf += avctx->width;
295  }
296 
297  if (corr_off) {
298  int corr_end, dec_size;
299 
300  corr_end = (s->gb.buffer_end - s->gb.buffer_start);
301  if (chroma_off > corr_off)
302  corr_end = chroma_off;
303  bytestream2_seek(&s->gb, 8 + corr_off, SEEK_SET);
304  dec_size = xan_unpack(s, s->scratch_buffer, s->buffer_size / 2);
305  if (dec_size < 0)
306  dec_size = 0;
307  for (i = 0; i < dec_size; i++)
308  s->y_buffer[i*2+1] = (s->y_buffer[i*2+1] + (s->scratch_buffer[i] << 1)) & 0x3F;
309  }
310 
311  src = s->y_buffer;
312  ybuf = s->pic.data[0];
313  for (j = 0; j < avctx->height; j++) {
314  for (i = 0; i < avctx->width; i++)
315  ybuf[i] = (src[i] << 2) | (src[i] >> 3);
316  src += avctx->width;
317  ybuf += s->pic.linesize[0];
318  }
319 
320  return 0;
321 }
322 
324 {
325  XanContext *s = avctx->priv_data;
326  uint8_t *ybuf, *src = s->scratch_buffer;
327  int cur, last;
328  int i, j;
329  int ret;
330 
331  if ((ret = xan_decode_chroma(avctx, bytestream2_get_le32(&s->gb))) != 0)
332  return ret;
333 
334  bytestream2_seek(&s->gb, 16, SEEK_SET);
335  ret = xan_unpack_luma(s, src,
336  s->buffer_size >> 1);
337  if (ret) {
338  av_log(avctx, AV_LOG_ERROR, "Luma decoding failed\n");
339  return ret;
340  }
341 
342  ybuf = s->y_buffer;
343  for (i = 0; i < avctx->height; i++) {
344  last = (ybuf[0] + (*src++ << 1)) & 0x3F;
345  ybuf[0] = last;
346  for (j = 1; j < avctx->width - 1; j += 2) {
347  cur = (ybuf[j + 1] + (*src++ << 1)) & 0x3F;
348  ybuf[j] = (last + cur) >> 1;
349  ybuf[j+1] = cur;
350  last = cur;
351  }
352  ybuf[j] = last;
353  ybuf += avctx->width;
354  }
355 
356  src = s->y_buffer;
357  ybuf = s->pic.data[0];
358  for (j = 0; j < avctx->height; j++) {
359  for (i = 0; i < avctx->width; i++)
360  ybuf[i] = (src[i] << 2) | (src[i] >> 3);
361  src += avctx->width;
362  ybuf += s->pic.linesize[0];
363  }
364 
365  return 0;
366 }
367 
369  void *data, int *data_size,
370  AVPacket *avpkt)
371 {
372  XanContext *s = avctx->priv_data;
373  int ftype;
374  int ret;
375 
376  s->pic.reference = 1;
380  if ((ret = avctx->reget_buffer(avctx, &s->pic))) {
381  av_log(s->avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
382  return ret;
383  }
384 
385  bytestream2_init(&s->gb, avpkt->data, avpkt->size);
386  ftype = bytestream2_get_le32(&s->gb);
387  switch (ftype) {
388  case 0:
389  ret = xan_decode_frame_type0(avctx);
390  break;
391  case 1:
392  ret = xan_decode_frame_type1(avctx);
393  break;
394  default:
395  av_log(avctx, AV_LOG_ERROR, "Unknown frame type %d\n", ftype);
396  return -1;
397  }
398  if (ret)
399  return ret;
400 
401  *data_size = sizeof(AVFrame);
402  *(AVFrame*)data = s->pic;
403 
404  return avpkt->size;
405 }
406 
408 {
409  XanContext *s = avctx->priv_data;
410 
411  if (s->pic.data[0])
412  avctx->release_buffer(avctx, &s->pic);
413 
414  av_freep(&s->y_buffer);
416 
417  return 0;
418 }
419 
421  .name = "xan_wc4",
422  .type = AVMEDIA_TYPE_VIDEO,
423  .id = CODEC_ID_XAN_WC4,
424  .priv_data_size = sizeof(XanContext),
428  .capabilities = CODEC_CAP_DR1,
429  .long_name = NULL_IF_CONFIG_SMALL("Wing Commander IV / Xxan"),
430 };
431 
enum PixelFormat pix_fmt
Pixel format, see PIX_FMT_xxx.
Definition: avcodec.h:1426
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:54
int size
#define FF_BUFFER_HINTS_VALID
Definition: avcodec.h:880
Audio Video Frame.
Definition: avcodec.h:985
int buffer_hints
codec suggestion on buffer type if != 0
Definition: avcodec.h:1195
void(* release_buffer)(struct AVCodecContext *c, AVFrame *pic)
Called to release buffers which were allocated with get_buffer.
Definition: avcodec.h:1737
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:117
int size
Definition: avcodec.h:909
static av_always_inline void bytestream2_init(GetByteContext *g, const uint8_t *buf, int buf_size)
Definition: bytestream.h:138
AVCodec ff_xan_wc4_decoder
Definition: xxan.c:420
AVCodec.
Definition: avcodec.h:3189
AVFrame pic
Definition: xxan.c:33
int(* reget_buffer)(struct AVCodecContext *c, AVFrame *pic)
Called at the beginning of a frame to get cr buffer for it.
Definition: avcodec.h:2336
void av_freep(void *arg)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
Definition: mem.c:147
static int decode(MimicContext *ctx, int quality, int num_coeffs, int is_iframe)
Definition: mimic.c:228
uint8_t bits
Definition: crc.c:31
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:64
#define av_cold
Definition: attributes.h:71
static int xan_decode_frame_type0(AVCodecContext *avctx)
Definition: xxan.c:244
void av_memcpy_backptr(uint8_t *dst, int back, int cnt)
deliberately overlapping memcpy implementation
Definition: lzo.c:177
const char data[16]
Definition: mxf.c:60
uint8_t * data
Definition: avcodec.h:908
const uint8_t * buffer
Definition: bytestream.h:32
bitstream reader API header.
#define V
Definition: options.c:69
static int init(AVCodecParserContext *s)
Definition: h264_parser.c:336
#define CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
Definition: avcodec.h:719
static int xan_decode_frame(AVCodecContext *avctx, void *data, int *data_size, AVPacket *avpkt)
Definition: xxan.c:368
static const uint16_t mask[17]
Definition: lzw.c:36
int reference
is this picture used as reference The values for this are the same as the MpegEncContext.picture_structure variable, that is 1->top field, 2->bottom field, 3->frame/both fields.
Definition: avcodec.h:1074
#define AVERROR(e)
Definition: error.h:43
static av_always_inline void bytestream2_skip(GetByteContext *g, unsigned int size)
Definition: bytestream.h:167
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:191
static av_always_inline unsigned int bytestream2_get_buffer(GetByteContext *g, uint8_t *dst, unsigned int size)
Definition: bytestream.h:266
static av_always_inline unsigned int bytestream2_get_bytes_left(GetByteContext *g)
Definition: bytestream.h:157
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:140
const char * name
Name of the codec implementation.
Definition: avcodec.h:3196
uint8_t * y_buffer
Definition: xxan.c:35
const uint8_t * buffer_end
Definition: bytestream.h:32
uint8_t * scratch_buffer
Definition: xxan.c:36
int width
picture width / height.
Definition: avcodec.h:1408
struct AVFrame AVFrame
Audio Video Frame.
#define FF_BUFFER_HINTS_REUSABLE
Definition: avcodec.h:883
static av_always_inline int bytestream2_tell(GetByteContext *g)
Definition: bytestream.h:191
static av_cold int xan_decode_end(AVCodecContext *avctx)
Definition: xxan.c:407
external API header
int linesize[AV_NUM_DATA_POINTERS]
Size, in bytes, of the data for each picture/channel plane.
Definition: avcodec.h:1008
main external API structure.
Definition: avcodec.h:1329
static void close(AVCodecParserContext *s)
Definition: h264_parser.c:327
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:111
const uint8_t * buffer_start
Definition: bytestream.h:32
Definition: xan.c:53
void * av_malloc(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:64
#define FF_BUFFER_HINTS_PRESERVE
Definition: avcodec.h:882
static int xan_unpack_luma(XanContext *s, uint8_t *dst, const int dst_size)
Definition: xxan.c:67
static av_cold int xan_decode_init(AVCodecContext *avctx)
Definition: xxan.c:41
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: avcodec.h:997
int buffer_size
Definition: xxan.c:37
AV_WL32 AV_WL24 AV_RL16
Definition: bytestream.h:89
struct XanContext XanContext
static int xan_decode_frame_type1(AVCodecContext *avctx)
Definition: xxan.c:323
void * priv_data
Definition: avcodec.h:1531
static int xan_unpack(XanContext *s, uint8_t *dest, const int dest_len)
Definition: xxan.c:109
static av_always_inline int bytestream2_seek(GetByteContext *g, int offset, int whence)
Definition: bytestream.h:211
Definition: vf_drawbox.c:32
GetByteContext gb
Definition: xxan.c:38
AVCodecContext * avctx
Definition: xan.c:55
static int xan_decode_chroma(AVCodecContext *avctx, unsigned chroma_off)
Definition: xxan.c:162