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