FFmpeg  2.1.1
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 
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 #include "internal.h"
30 
31 typedef struct XanContext {
34 
39 } XanContext;
40 
41 static av_cold int xan_decode_end(AVCodecContext *avctx);
42 
44 {
45  XanContext *s = avctx->priv_data;
46 
47  s->avctx = avctx;
48 
49  avctx->pix_fmt = AV_PIX_FMT_YUV420P;
50 
51  if (avctx->height < 8) {
52  av_log(avctx, AV_LOG_ERROR, "Invalid frame height: %d.\n", avctx->height);
53  return AVERROR(EINVAL);
54  }
55  if (avctx->width & 1) {
56  av_log(avctx, AV_LOG_ERROR, "Invalid frame width: %d.\n", avctx->width);
57  return AVERROR(EINVAL);
58  }
59 
60  s->buffer_size = avctx->width * avctx->height;
62  if (!s->y_buffer)
63  return AVERROR(ENOMEM);
64  s->scratch_buffer = av_malloc(s->buffer_size + 130);
65  if (!s->scratch_buffer) {
66  xan_decode_end(avctx);
67  return AVERROR(ENOMEM);
68  }
69 
70  s->pic = av_frame_alloc();
71  if (!s->pic) {
72  xan_decode_end(avctx);
73  return AVERROR(ENOMEM);
74  }
75 
76  return 0;
77 }
78 
80  uint8_t *dst, const int dst_size)
81 {
82  int tree_size, eof;
83  int bits, mask;
84  int tree_root, node;
85  const uint8_t *dst_end = dst + dst_size;
86  GetByteContext tree = s->gb;
87  int start_off = bytestream2_tell(&tree);
88 
89  tree_size = bytestream2_get_byte(&s->gb);
90  eof = bytestream2_get_byte(&s->gb);
91  tree_root = eof + tree_size;
92  bytestream2_skip(&s->gb, tree_size * 2);
93 
94  node = tree_root;
95  bits = bytestream2_get_byte(&s->gb);
96  mask = 0x80;
97  for (;;) {
98  int bit = !!(bits & mask);
99  mask >>= 1;
100  bytestream2_seek(&tree, start_off + node*2 + bit - eof * 2, SEEK_SET);
101  node = bytestream2_get_byte(&tree);
102  if (node == eof)
103  break;
104  if (node < eof) {
105  *dst++ = node;
106  if (dst > dst_end)
107  break;
108  node = tree_root;
109  }
110  if (!mask) {
111  if (bytestream2_get_bytes_left(&s->gb) <= 0)
112  break;
113  bits = bytestream2_get_byteu(&s->gb);
114  mask = 0x80;
115  }
116  }
117  return dst != dst_end ? AVERROR_INVALIDDATA : 0;
118 }
119 
120 /* almost the same as in xan_wc3 decoder */
121 static int xan_unpack(XanContext *s,
122  uint8_t *dest, const int dest_len)
123 {
124  uint8_t opcode;
125  int size;
126  uint8_t *orig_dest = dest;
127  const uint8_t *dest_end = dest + dest_len;
128 
129  while (dest < dest_end) {
130  if (bytestream2_get_bytes_left(&s->gb) <= 0)
131  return AVERROR_INVALIDDATA;
132 
133  opcode = bytestream2_get_byteu(&s->gb);
134 
135  if (opcode < 0xe0) {
136  int size2, back;
137  if ((opcode & 0x80) == 0) {
138  size = opcode & 3;
139  back = ((opcode & 0x60) << 3) + bytestream2_get_byte(&s->gb) + 1;
140  size2 = ((opcode & 0x1c) >> 2) + 3;
141  } else if ((opcode & 0x40) == 0) {
142  size = bytestream2_peek_byte(&s->gb) >> 6;
143  back = (bytestream2_get_be16(&s->gb) & 0x3fff) + 1;
144  size2 = (opcode & 0x3f) + 4;
145  } else {
146  size = opcode & 3;
147  back = ((opcode & 0x10) << 12) + bytestream2_get_be16(&s->gb) + 1;
148  size2 = ((opcode & 0x0c) << 6) + bytestream2_get_byte(&s->gb) + 5;
149  if (size + size2 > dest_end - dest)
150  break;
151  }
152  if (dest + size + size2 > dest_end ||
153  dest - orig_dest + size < back)
154  return AVERROR_INVALIDDATA;
155  bytestream2_get_buffer(&s->gb, dest, size);
156  dest += size;
157  av_memcpy_backptr(dest, back, size2);
158  dest += size2;
159  } else {
160  int finish = opcode >= 0xfc;
161 
162  size = finish ? opcode & 3 : ((opcode & 0x1f) << 2) + 4;
163  if (dest_end - dest < size)
164  return AVERROR_INVALIDDATA;
165  bytestream2_get_buffer(&s->gb, dest, size);
166  dest += size;
167  if (finish)
168  break;
169  }
170  }
171  return dest - orig_dest;
172 }
173 
174 static int xan_decode_chroma(AVCodecContext *avctx, unsigned chroma_off)
175 {
176  XanContext *s = avctx->priv_data;
177  uint8_t *U, *V;
178  int val, uval, vval;
179  int i, j;
180  const uint8_t *src, *src_end;
181  const uint8_t *table;
182  int mode, offset, dec_size, table_size;
183 
184  if (!chroma_off)
185  return 0;
186  if (chroma_off + 4 >= bytestream2_get_bytes_left(&s->gb)) {
187  av_log(avctx, AV_LOG_ERROR, "Invalid chroma block position\n");
188  return AVERROR_INVALIDDATA;
189  }
190  bytestream2_seek(&s->gb, chroma_off + 4, SEEK_SET);
191  mode = bytestream2_get_le16(&s->gb);
192  table = s->gb.buffer;
193  table_size = bytestream2_get_le16(&s->gb);
194  offset = table_size * 2;
195  table_size += 1;
196 
197  if (offset >= bytestream2_get_bytes_left(&s->gb)) {
198  av_log(avctx, AV_LOG_ERROR, "Invalid chroma block offset\n");
199  return AVERROR_INVALIDDATA;
200  }
201 
202  bytestream2_skip(&s->gb, offset);
203  memset(s->scratch_buffer, 0, s->buffer_size);
204  dec_size = xan_unpack(s, s->scratch_buffer, s->buffer_size);
205  if (dec_size < 0) {
206  av_log(avctx, AV_LOG_ERROR, "Chroma unpacking failed\n");
207  return dec_size;
208  }
209 
210  U = s->pic->data[1];
211  V = s->pic->data[2];
212  src = s->scratch_buffer;
213  src_end = src + dec_size;
214  if (mode) {
215  for (j = 0; j < avctx->height >> 1; j++) {
216  for (i = 0; i < avctx->width >> 1; i++) {
217  if (src_end - src < 1)
218  return 0;
219  val = *src++;
220  if (val) {
221  if (val >= table_size)
222  return AVERROR_INVALIDDATA;
223  val = AV_RL16(table + (val << 1));
224  uval = (val >> 3) & 0xF8;
225  vval = (val >> 8) & 0xF8;
226  U[i] = uval | (uval >> 5);
227  V[i] = vval | (vval >> 5);
228  }
229  }
230  U += s->pic->linesize[1];
231  V += s->pic->linesize[2];
232  }
233  if (avctx->height & 1) {
234  memcpy(U, U - s->pic->linesize[1], avctx->width >> 1);
235  memcpy(V, V - s->pic->linesize[2], avctx->width >> 1);
236  }
237  } else {
238  uint8_t *U2 = U + s->pic->linesize[1];
239  uint8_t *V2 = V + s->pic->linesize[2];
240 
241  for (j = 0; j < avctx->height >> 2; j++) {
242  for (i = 0; i < avctx->width >> 1; i += 2) {
243  if (src_end - src < 1)
244  return 0;
245  val = *src++;
246  if (val) {
247  if (val >= table_size)
248  return AVERROR_INVALIDDATA;
249  val = AV_RL16(table + (val << 1));
250  uval = (val >> 3) & 0xF8;
251  vval = (val >> 8) & 0xF8;
252  U[i] = U[i+1] = U2[i] = U2[i+1] = uval | (uval >> 5);
253  V[i] = V[i+1] = V2[i] = V2[i+1] = vval | (vval >> 5);
254  }
255  }
256  U += s->pic->linesize[1] * 2;
257  V += s->pic->linesize[2] * 2;
258  U2 += s->pic->linesize[1] * 2;
259  V2 += s->pic->linesize[2] * 2;
260  }
261  if (avctx->height & 3) {
262  int lines = ((avctx->height + 1) >> 1) - (avctx->height >> 2) * 2;
263 
264  memcpy(U, U - lines * s->pic->linesize[1], lines * s->pic->linesize[1]);
265  memcpy(V, V - lines * s->pic->linesize[2], lines * s->pic->linesize[2]);
266  }
267  }
268 
269  return 0;
270 }
271 
273 {
274  XanContext *s = avctx->priv_data;
275  uint8_t *ybuf, *prev_buf, *src = s->scratch_buffer;
276  unsigned chroma_off, corr_off;
277  int cur, last;
278  int i, j;
279  int ret;
280 
281  chroma_off = bytestream2_get_le32(&s->gb);
282  corr_off = bytestream2_get_le32(&s->gb);
283 
284  if ((ret = xan_decode_chroma(avctx, chroma_off)) != 0)
285  return ret;
286 
287  if (corr_off >= bytestream2_size(&s->gb)) {
288  av_log(avctx, AV_LOG_WARNING, "Ignoring invalid correction block position\n");
289  corr_off = 0;
290  }
291  bytestream2_seek(&s->gb, 12, SEEK_SET);
292  ret = xan_unpack_luma(s, src, s->buffer_size >> 1);
293  if (ret) {
294  av_log(avctx, AV_LOG_ERROR, "Luma decoding failed\n");
295  return ret;
296  }
297 
298  ybuf = s->y_buffer;
299  last = *src++;
300  ybuf[0] = last << 1;
301  for (j = 1; j < avctx->width - 1; j += 2) {
302  cur = (last + *src++) & 0x1F;
303  ybuf[j] = last + cur;
304  ybuf[j+1] = cur << 1;
305  last = cur;
306  }
307  ybuf[j] = last << 1;
308  prev_buf = ybuf;
309  ybuf += avctx->width;
310 
311  for (i = 1; i < avctx->height; i++) {
312  last = ((prev_buf[0] >> 1) + *src++) & 0x1F;
313  ybuf[0] = last << 1;
314  for (j = 1; j < avctx->width - 1; j += 2) {
315  cur = ((prev_buf[j + 1] >> 1) + *src++) & 0x1F;
316  ybuf[j] = last + cur;
317  ybuf[j+1] = cur << 1;
318  last = cur;
319  }
320  ybuf[j] = last << 1;
321  prev_buf = ybuf;
322  ybuf += avctx->width;
323  }
324 
325  if (corr_off) {
326  int dec_size;
327 
328  bytestream2_seek(&s->gb, 8 + corr_off, SEEK_SET);
329  dec_size = xan_unpack(s, s->scratch_buffer, s->buffer_size / 2);
330  if (dec_size < 0)
331  dec_size = 0;
332  else
333  dec_size = FFMIN(dec_size, s->buffer_size/2 - 1);
334 
335  for (i = 0; i < dec_size; i++)
336  s->y_buffer[i*2+1] = (s->y_buffer[i*2+1] + (s->scratch_buffer[i] << 1)) & 0x3F;
337  }
338 
339  src = s->y_buffer;
340  ybuf = s->pic->data[0];
341  for (j = 0; j < avctx->height; j++) {
342  for (i = 0; i < avctx->width; i++)
343  ybuf[i] = (src[i] << 2) | (src[i] >> 3);
344  src += avctx->width;
345  ybuf += s->pic->linesize[0];
346  }
347 
348  return 0;
349 }
350 
352 {
353  XanContext *s = avctx->priv_data;
354  uint8_t *ybuf, *src = s->scratch_buffer;
355  int cur, last;
356  int i, j;
357  int ret;
358 
359  if ((ret = xan_decode_chroma(avctx, bytestream2_get_le32(&s->gb))) != 0)
360  return ret;
361 
362  bytestream2_seek(&s->gb, 16, SEEK_SET);
363  ret = xan_unpack_luma(s, src,
364  s->buffer_size >> 1);
365  if (ret) {
366  av_log(avctx, AV_LOG_ERROR, "Luma decoding failed\n");
367  return ret;
368  }
369 
370  ybuf = s->y_buffer;
371  for (i = 0; i < avctx->height; i++) {
372  last = (ybuf[0] + (*src++ << 1)) & 0x3F;
373  ybuf[0] = last;
374  for (j = 1; j < avctx->width - 1; j += 2) {
375  cur = (ybuf[j + 1] + (*src++ << 1)) & 0x3F;
376  ybuf[j] = (last + cur) >> 1;
377  ybuf[j+1] = cur;
378  last = cur;
379  }
380  ybuf[j] = last;
381  ybuf += avctx->width;
382  }
383 
384  src = s->y_buffer;
385  ybuf = s->pic->data[0];
386  for (j = 0; j < avctx->height; j++) {
387  for (i = 0; i < avctx->width; i++)
388  ybuf[i] = (src[i] << 2) | (src[i] >> 3);
389  src += avctx->width;
390  ybuf += s->pic->linesize[0];
391  }
392 
393  return 0;
394 }
395 
397  void *data, int *got_frame,
398  AVPacket *avpkt)
399 {
400  XanContext *s = avctx->priv_data;
401  int ftype;
402  int ret;
403 
404  if ((ret = ff_reget_buffer(avctx, s->pic)) < 0)
405  return ret;
406 
407  bytestream2_init(&s->gb, avpkt->data, avpkt->size);
408  ftype = bytestream2_get_le32(&s->gb);
409  switch (ftype) {
410  case 0:
411  ret = xan_decode_frame_type0(avctx);
412  break;
413  case 1:
414  ret = xan_decode_frame_type1(avctx);
415  break;
416  default:
417  av_log(avctx, AV_LOG_ERROR, "Unknown frame type %d\n", ftype);
418  return AVERROR_INVALIDDATA;
419  }
420  if (ret)
421  return ret;
422 
423  if ((ret = av_frame_ref(data, s->pic)) < 0)
424  return ret;
425 
426  *got_frame = 1;
427 
428  return avpkt->size;
429 }
430 
432 {
433  XanContext *s = avctx->priv_data;
434 
435  av_frame_free(&s->pic);
436 
437  av_freep(&s->y_buffer);
439 
440  return 0;
441 }
442 
444  .name = "xan_wc4",
445  .long_name = NULL_IF_CONFIG_SMALL("Wing Commander IV / Xxan"),
446  .type = AVMEDIA_TYPE_VIDEO,
447  .id = AV_CODEC_ID_XAN_WC4,
448  .priv_data_size = sizeof(XanContext),
452  .capabilities = CODEC_CAP_DR1,
453 };
const char const char void * val
Definition: avisynth_c.h:671
const char * s
Definition: avisynth_c.h:668
int size
This structure describes decoded (raw) audio or video data.
Definition: frame.h:96
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: avcodec.h:4153
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
static struct endianess table[]
#define AV_RL16(x)
Definition: intreadwrite.h:245
int size
Definition: avcodec.h:1064
void av_log(void *avcl, int level, const char *fmt,...) av_printf_format(3
Send the specified message to the log if the level is less than or equal to the current av_log_level...
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1342
static av_always_inline void bytestream2_init(GetByteContext *g, const uint8_t *buf, int buf_size)
Definition: bytestream.h:131
AVCodec ff_xan_wc4_decoder
Definition: xxan.c:443
AVCodec.
Definition: avcodec.h:2922
#define av_cold
Definition: avcodec.h:653
void av_freep(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
Definition: mem.c:234
uint8_t bits
Definition: crc.c:260
uint8_t
static int xan_decode_frame_type0(AVCodecContext *avctx)
Definition: xxan.c:272
mode
Definition: f_perms.c:27
static const uint8_t offset[511][2]
Definition: vf_uspp.c:58
const char * name
Name of the codec implementation.
Definition: avcodec.h:2929
int av_frame_ref(AVFrame *dst, AVFrame *src)
Setup a new reference to the data described by a given frame.
Definition: frame.c:247
#define CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
Definition: avcodec.h:742
const char data[16]
Definition: mxf.c:68
const uint8_t * buffer
Definition: bytestream.h:34
bitstream reader API header.
static av_always_inline int bytestream2_size(GetByteContext *g)
Definition: bytestream.h:196
#define U(x)
Definition: vp56_arith.h:37
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: avcodec.h:4147
static const uint16_t mask[17]
Definition: lzw.c:37
static av_always_inline void bytestream2_skip(GetByteContext *g, unsigned int size)
Definition: bytestream.h:162
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:151
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition: frame.c:110
static av_always_inline unsigned int bytestream2_get_buffer(GetByteContext *g, uint8_t *dst, unsigned int size)
Definition: bytestream.h:261
static av_always_inline unsigned int bytestream2_get_bytes_left(GetByteContext *g)
Definition: bytestream.h:152
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:123
Libavcodec external API header.
uint8_t * y_buffer
Definition: xxan.c:35
#define V
Definition: options_table.h:35
int ff_reget_buffer(AVCodecContext *avctx, AVFrame *frame)
Identical in function to av_frame_make_writable(), except it uses ff_get_buffer() to allocate the buf...
Definition: utils.c:986
uint8_t * scratch_buffer
Definition: xxan.c:36
ret
Definition: avfilter.c:961
int width
picture width / height.
Definition: avcodec.h:1314
void * av_malloc(size_t size) av_malloc_attrib 1(1)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:73
#define FFMIN(a, b)
Definition: avcodec.h:925
static int xan_decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: xxan.c:396
static av_always_inline int bytestream2_tell(GetByteContext *g)
Definition: bytestream.h:186
planar YUV 4:2:0, 12bpp, (1 Cr &amp; Cb sample per 2x2 Y samples)
Definition: avcodec.h:4534
static av_cold int xan_decode_end(AVCodecContext *avctx)
Definition: xxan.c:431
AVS_Value src
Definition: avisynth_c.h:523
main external API structure.
Definition: avcodec.h:1146
static void close(AVCodecParserContext *s)
Definition: h264_parser.c:538
Definition: xan.c:52
static int xan_unpack_luma(XanContext *s, uint8_t *dst, const int dst_size)
Definition: xxan.c:79
uint8_t * data
Definition: avcodec.h:1063
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:124
static av_cold int xan_decode_init(AVCodecContext *avctx)
Definition: xxan.c:43
void * priv_data
Definition: avcodec.h:1182
int buffer_size
Definition: xxan.c:37
static void finish(void)
Definition: ffhash.c:61
common internal api header.
static int xan_decode_frame_type1(AVCodecContext *avctx)
Definition: xxan.c:351
#define AVERROR_INVALIDDATA
static int xan_unpack(XanContext *s, uint8_t *dest, const int dest_len)
Definition: xxan.c:121
static av_always_inline int bytestream2_seek(GetByteContext *g, int offset, int whence)
Definition: bytestream.h:206
#define AVERROR(e)
AVFrame * pic
Definition: xxan.c:33
void av_memcpy_backptr(uint8_t *dst, int back, int cnt)
deliberately overlapping memcpy implementation
Definition: mem.c:416
static int decode(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: crystalhd.c:868
GetByteContext gb
Definition: xxan.c:38
This structure stores compressed data.
Definition: avcodec.h:1040
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:107
AVCodecContext * avctx
Definition: xan.c:54
static int xan_decode_chroma(AVCodecContext *avctx, unsigned chroma_off)
Definition: xxan.c:174