FFmpeg
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 "libavutil/intreadwrite.h"
24 #include "libavutil/mem.h"
25 
26 #include "avcodec.h"
27 #include "bytestream.h"
28 #include "codec_internal.h"
29 #include "decode.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  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 
59  avctx->pix_fmt = AV_PIX_FMT_YUV420P;
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_malloc(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 */
127 static int xan_unpack(XanContext *s,
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 
180 static 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 
208  bytestream2_skip(&s->gb, offset);
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);
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 
402 static 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),
443  .close = xan_decode_end,
445  .p.capabilities = AV_CODEC_CAP_DR1,
446  .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
447 };
AV_LOG_WARNING
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:215
FF_CODEC_CAP_INIT_CLEANUP
#define FF_CODEC_CAP_INIT_CLEANUP
The codec allows calling the close function for deallocation even if the init function returned a fai...
Definition: codec_internal.h:43
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
GetByteContext
Definition: bytestream.h:33
xan_decode_frame_type0
static int xan_decode_frame_type0(AVCodecContext *avctx)
Definition: xxan.c:278
XanContext::scratch_buffer
uint8_t * scratch_buffer
Definition: xxan.c:36
mask
int mask
Definition: mediacodecdec_common.c:154
av_frame_free
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:162
bytestream2_seek
static av_always_inline int bytestream2_seek(GetByteContext *g, int offset, int whence)
Definition: bytestream.h:212
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:389
AVPacket::data
uint8_t * data
Definition: packet.h:539
xan_unpack
static int xan_unpack(XanContext *s, uint8_t *dest, const int dest_len)
Definition: xxan.c:127
table
static const uint16_t table[]
Definition: prosumer.c:203
FFCodec
Definition: codec_internal.h:127
xan_decode_frame_type1
static int xan_decode_frame_type1(AVCodecContext *avctx)
Definition: xxan.c:357
XanContext::gb
GetByteContext gb
Definition: xxan.c:38
av_malloc
#define av_malloc(s)
Definition: tableprint_vlc.h:30
bit
#define bit(string, value)
Definition: cbs_mpeg2.c:56
bytestream2_skip
static av_always_inline void bytestream2_skip(GetByteContext *g, unsigned int size)
Definition: bytestream.h:168
xan_decode_end
static av_cold int xan_decode_end(AVCodecContext *avctx)
Definition: xxan.c:41
FFCodec::p
AVCodec p
The public AVCodec.
Definition: codec_internal.h:131
finish
static void finish(void)
Definition: movenc.c:373
val
static double val(void *priv, double ch)
Definition: aeval.c:77
ftype
#define ftype
Definition: aap_template.c:30
av_frame_alloc
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition: frame.c:150
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:209
av_cold
#define av_cold
Definition: attributes.h:90
av_memcpy_backptr
void av_memcpy_backptr(uint8_t *dst, int back, int cnt)
Overlapping memcpy() implementation.
Definition: mem.c:447
FF_CODEC_DECODE_CB
#define FF_CODEC_DECODE_CB(func)
Definition: codec_internal.h:311
intreadwrite.h
s
#define s(width, name)
Definition: cbs_vp9.c:198
xan_decode_init
static av_cold int xan_decode_init(AVCodecContext *avctx)
Definition: xxan.c:53
bits
uint8_t bits
Definition: vp3data.h:128
decode.h
AV_RL16
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_RL16
Definition: bytestream.h:94
AV_PIX_FMT_YUV420P
@ AV_PIX_FMT_YUV420P
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:73
AV_CODEC_ID_XAN_WC4
@ AV_CODEC_ID_XAN_WC4
Definition: codec_id.h:93
CODEC_LONG_NAME
#define CODEC_LONG_NAME(str)
Definition: codec_internal.h:296
XanContext::y_buffer
uint8_t * y_buffer
Definition: xxan.c:35
V
#define V
Definition: avdct.c:31
bytestream2_get_buffer
static av_always_inline unsigned int bytestream2_get_buffer(GetByteContext *g, uint8_t *dst, unsigned int size)
Definition: bytestream.h:267
bytestream2_get_bytes_left
static av_always_inline int bytestream2_get_bytes_left(GetByteContext *g)
Definition: bytestream.h:158
bytestream2_tell
static av_always_inline int bytestream2_tell(GetByteContext *g)
Definition: bytestream.h:192
init
int(* init)(AVBSFContext *ctx)
Definition: dts2pts.c:368
AV_CODEC_CAP_DR1
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
Definition: codec.h:52
AVPacket::size
int size
Definition: packet.h:540
bytestream2_size
static av_always_inline int bytestream2_size(GetByteContext *g)
Definition: bytestream.h:202
av_frame_ref
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:400
codec_internal.h
dst
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
Definition: dsp.h:83
size
int size
Definition: twinvq_data.h:10344
XanContext::avctx
AVCodecContext * avctx
Definition: xan.c:54
XanContext::pic
AVFrame * pic
Definition: xxan.c:33
offset
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf offset
Definition: writing_filters.txt:86
ff_xan_wc4_decoder
const FFCodec ff_xan_wc4_decoder
Definition: xxan.c:436
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:256
XanContext
Definition: xan.c:52
xan_decode_chroma
static int xan_decode_chroma(AVCodecContext *avctx, unsigned chroma_off)
Definition: xxan.c:180
FFMIN
#define FFMIN(a, b)
Definition: macros.h:49
AVCodec::name
const char * name
Name of the codec implementation.
Definition: codec.h:194
AVCodecContext::height
int height
Definition: avcodec.h:624
AVCodecContext::pix_fmt
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:663
avcodec.h
ff_reget_buffer
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:1815
ret
ret
Definition: filter_design.txt:187
U
#define U(x)
Definition: vpx_arith.h:37
AVCodecContext
main external API structure.
Definition: avcodec.h:451
mode
mode
Definition: ebur128.h:83
xan_decode_frame
static int xan_decode_frame(AVCodecContext *avctx, AVFrame *rframe, int *got_frame, AVPacket *avpkt)
Definition: xxan.c:402
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
XanContext::buffer_size
int buffer_size
Definition: xxan.c:37
mem.h
AVPacket
This structure stores compressed data.
Definition: packet.h:516
AVCodecContext::priv_data
void * priv_data
Definition: avcodec.h:478
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:34
AVCodecContext::width
int width
picture width / height.
Definition: avcodec.h:624
bytestream.h
bytestream2_init
static av_always_inline void bytestream2_init(GetByteContext *g, const uint8_t *buf, int buf_size)
Definition: bytestream.h:137
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:61
src
#define src
Definition: vp8dsp.c:248
xan_unpack_luma
static int xan_unpack_luma(XanContext *s, uint8_t *dst, const int dst_size)
Definition: xxan.c:85