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 "internal.h"
29 
30 typedef struct XanContext {
33 
34  uint8_t *y_buffer;
35  uint8_t *scratch_buffer;
38 } XanContext;
39 
41 {
42  XanContext *s = avctx->priv_data;
43 
44  av_frame_free(&s->pic);
45 
46  av_freep(&s->y_buffer);
47  av_freep(&s->scratch_buffer);
48 
49  return 0;
50 }
51 
53 {
54  XanContext *s = avctx->priv_data;
55 
56  s->avctx = avctx;
57 
58  avctx->pix_fmt = AV_PIX_FMT_YUV420P;
59 
60  if (avctx->height < 8) {
61  av_log(avctx, AV_LOG_ERROR, "Invalid frame height: %d.\n", avctx->height);
62  return AVERROR(EINVAL);
63  }
64  if (avctx->width & 1) {
65  av_log(avctx, AV_LOG_ERROR, "Invalid frame width: %d.\n", avctx->width);
66  return AVERROR(EINVAL);
67  }
68 
69  s->buffer_size = avctx->width * avctx->height;
70  s->y_buffer = av_malloc(s->buffer_size);
71  if (!s->y_buffer)
72  return AVERROR(ENOMEM);
73  s->scratch_buffer = av_malloc(s->buffer_size + 130);
74  if (!s->scratch_buffer)
75  return AVERROR(ENOMEM);
76 
77  s->pic = av_frame_alloc();
78  if (!s->pic)
79  return AVERROR(ENOMEM);
80 
81  return 0;
82 }
83 
85  uint8_t *dst, const int dst_size)
86 {
87  int tree_size, eof;
88  int bits, mask;
89  int tree_root, node;
90  const uint8_t *dst_end = dst + dst_size;
91  GetByteContext tree = s->gb;
92  int start_off = bytestream2_tell(&tree);
93 
94  tree_size = bytestream2_get_byte(&s->gb);
95  eof = bytestream2_get_byte(&s->gb);
96  tree_root = eof + tree_size;
97  bytestream2_skip(&s->gb, tree_size * 2);
98 
99  node = tree_root;
100  bits = bytestream2_get_byte(&s->gb);
101  mask = 0x80;
102  for (;;) {
103  int bit = !!(bits & mask);
104  mask >>= 1;
105  bytestream2_seek(&tree, start_off + node*2 + bit - eof * 2, SEEK_SET);
106  node = bytestream2_get_byte(&tree);
107  if (node == eof)
108  break;
109  if (node < eof) {
110  *dst++ = node;
111  if (dst > dst_end)
112  break;
113  node = tree_root;
114  }
115  if (!mask) {
116  if (bytestream2_get_bytes_left(&s->gb) <= 0)
117  break;
118  bits = bytestream2_get_byteu(&s->gb);
119  mask = 0x80;
120  }
121  }
122  return dst != dst_end ? AVERROR_INVALIDDATA : 0;
123 }
124 
125 /* almost the same as in xan_wc3 decoder */
126 static int xan_unpack(XanContext *s,
127  uint8_t *dest, const int dest_len)
128 {
129  uint8_t opcode;
130  int size;
131  uint8_t *orig_dest = dest;
132  const uint8_t *dest_end = dest + dest_len;
133 
134  while (dest < dest_end) {
135  if (bytestream2_get_bytes_left(&s->gb) <= 0)
136  return AVERROR_INVALIDDATA;
137 
138  opcode = bytestream2_get_byteu(&s->gb);
139 
140  if (opcode < 0xe0) {
141  int size2, back;
142  if ((opcode & 0x80) == 0) {
143  size = opcode & 3;
144  back = ((opcode & 0x60) << 3) + bytestream2_get_byte(&s->gb) + 1;
145  size2 = ((opcode & 0x1c) >> 2) + 3;
146  } else if ((opcode & 0x40) == 0) {
147  size = bytestream2_peek_byte(&s->gb) >> 6;
148  back = (bytestream2_get_be16(&s->gb) & 0x3fff) + 1;
149  size2 = (opcode & 0x3f) + 4;
150  } else {
151  size = opcode & 3;
152  back = ((opcode & 0x10) << 12) + bytestream2_get_be16(&s->gb) + 1;
153  size2 = ((opcode & 0x0c) << 6) + bytestream2_get_byte(&s->gb) + 5;
154  if (size + size2 > dest_end - dest)
155  break;
156  }
157  if (dest + size + size2 > dest_end ||
158  dest - orig_dest + size < back)
159  return AVERROR_INVALIDDATA;
160  bytestream2_get_buffer(&s->gb, dest, size);
161  dest += size;
162  av_memcpy_backptr(dest, back, size2);
163  dest += size2;
164  } else {
165  int finish = opcode >= 0xfc;
166 
167  size = finish ? opcode & 3 : ((opcode & 0x1f) << 2) + 4;
168  if (dest_end - dest < size)
169  return AVERROR_INVALIDDATA;
170  bytestream2_get_buffer(&s->gb, dest, size);
171  dest += size;
172  if (finish)
173  break;
174  }
175  }
176  return dest - orig_dest;
177 }
178 
179 static int xan_decode_chroma(AVCodecContext *avctx, unsigned chroma_off)
180 {
181  XanContext *s = avctx->priv_data;
182  uint8_t *U, *V;
183  int val, uval, vval;
184  int i, j;
185  const uint8_t *src, *src_end;
186  const uint8_t *table;
187  int mode, offset, dec_size, table_size;
188 
189  if (!chroma_off)
190  return 0;
191  if (chroma_off + 4 >= bytestream2_get_bytes_left(&s->gb)) {
192  av_log(avctx, AV_LOG_ERROR, "Invalid chroma block position\n");
193  return AVERROR_INVALIDDATA;
194  }
195  bytestream2_seek(&s->gb, chroma_off + 4, SEEK_SET);
196  mode = bytestream2_get_le16(&s->gb);
197  table = s->gb.buffer;
198  table_size = bytestream2_get_le16(&s->gb);
199  offset = table_size * 2;
200  table_size += 1;
201 
202  if (offset >= bytestream2_get_bytes_left(&s->gb)) {
203  av_log(avctx, AV_LOG_ERROR, "Invalid chroma block offset\n");
204  return AVERROR_INVALIDDATA;
205  }
206 
207  bytestream2_skip(&s->gb, offset);
208  memset(s->scratch_buffer, 0, s->buffer_size);
209  dec_size = xan_unpack(s, s->scratch_buffer, s->buffer_size);
210  if (dec_size < 0) {
211  av_log(avctx, AV_LOG_ERROR, "Chroma unpacking failed\n");
212  return dec_size;
213  }
214 
215  U = s->pic->data[1];
216  V = s->pic->data[2];
217  src = s->scratch_buffer;
218  src_end = src + dec_size;
219  if (mode) {
220  for (j = 0; j < avctx->height >> 1; j++) {
221  for (i = 0; i < avctx->width >> 1; i++) {
222  if (src_end - src < 1)
223  return 0;
224  val = *src++;
225  if (val) {
226  if (val >= table_size)
227  return AVERROR_INVALIDDATA;
228  val = AV_RL16(table + (val << 1));
229  uval = (val >> 3) & 0xF8;
230  vval = (val >> 8) & 0xF8;
231  U[i] = uval | (uval >> 5);
232  V[i] = vval | (vval >> 5);
233  }
234  }
235  U += s->pic->linesize[1];
236  V += s->pic->linesize[2];
237  }
238  if (avctx->height & 1) {
239  memcpy(U, U - s->pic->linesize[1], avctx->width >> 1);
240  memcpy(V, V - s->pic->linesize[2], avctx->width >> 1);
241  }
242  } else {
243  uint8_t *U2 = U + s->pic->linesize[1];
244  uint8_t *V2 = V + s->pic->linesize[2];
245 
246  for (j = 0; j < avctx->height >> 2; j++) {
247  for (i = 0; i < avctx->width >> 1; i += 2) {
248  if (src_end - src < 1)
249  return 0;
250  val = *src++;
251  if (val) {
252  if (val >= table_size)
253  return AVERROR_INVALIDDATA;
254  val = AV_RL16(table + (val << 1));
255  uval = (val >> 3) & 0xF8;
256  vval = (val >> 8) & 0xF8;
257  U[i] = U[i+1] = U2[i] = U2[i+1] = uval | (uval >> 5);
258  V[i] = V[i+1] = V2[i] = V2[i+1] = vval | (vval >> 5);
259  }
260  }
261  U += s->pic->linesize[1] * 2;
262  V += s->pic->linesize[2] * 2;
263  U2 += s->pic->linesize[1] * 2;
264  V2 += s->pic->linesize[2] * 2;
265  }
266  if (avctx->height & 3) {
267  int lines = ((avctx->height + 1) >> 1) - (avctx->height >> 2) * 2;
268 
269  memcpy(U, U - lines * s->pic->linesize[1], lines * s->pic->linesize[1]);
270  memcpy(V, V - lines * s->pic->linesize[2], lines * s->pic->linesize[2]);
271  }
272  }
273 
274  return 0;
275 }
276 
278 {
279  XanContext *s = avctx->priv_data;
280  uint8_t *ybuf, *prev_buf, *src = s->scratch_buffer;
281  unsigned chroma_off, corr_off;
282  int cur, last;
283  int i, j;
284  int ret;
285 
286  chroma_off = bytestream2_get_le32(&s->gb);
287  corr_off = bytestream2_get_le32(&s->gb);
288 
289  if ((ret = xan_decode_chroma(avctx, chroma_off)) != 0)
290  return ret;
291 
292  if (corr_off >= bytestream2_size(&s->gb)) {
293  av_log(avctx, AV_LOG_WARNING, "Ignoring invalid correction block position\n");
294  corr_off = 0;
295  }
296  bytestream2_seek(&s->gb, 12, SEEK_SET);
297  ret = xan_unpack_luma(s, src, s->buffer_size >> 1);
298  if (ret) {
299  av_log(avctx, AV_LOG_ERROR, "Luma decoding failed\n");
300  return ret;
301  }
302 
303  ybuf = s->y_buffer;
304  last = *src++;
305  ybuf[0] = last << 1;
306  for (j = 1; j < avctx->width - 1; j += 2) {
307  cur = (last + *src++) & 0x1F;
308  ybuf[j] = last + cur;
309  ybuf[j+1] = cur << 1;
310  last = cur;
311  }
312  ybuf[j] = last << 1;
313  prev_buf = ybuf;
314  ybuf += avctx->width;
315 
316  for (i = 1; i < avctx->height; i++) {
317  last = ((prev_buf[0] >> 1) + *src++) & 0x1F;
318  ybuf[0] = last << 1;
319  for (j = 1; j < avctx->width - 1; j += 2) {
320  cur = ((prev_buf[j + 1] >> 1) + *src++) & 0x1F;
321  ybuf[j] = last + cur;
322  ybuf[j+1] = cur << 1;
323  last = cur;
324  }
325  ybuf[j] = last << 1;
326  prev_buf = ybuf;
327  ybuf += avctx->width;
328  }
329 
330  if (corr_off) {
331  int dec_size;
332 
333  bytestream2_seek(&s->gb, 8 + corr_off, SEEK_SET);
334  dec_size = xan_unpack(s, s->scratch_buffer, s->buffer_size / 2);
335  if (dec_size < 0)
336  dec_size = 0;
337  else
338  dec_size = FFMIN(dec_size, s->buffer_size/2 - 1);
339 
340  for (i = 0; i < dec_size; i++)
341  s->y_buffer[i*2+1] = (s->y_buffer[i*2+1] + (s->scratch_buffer[i] << 1)) & 0x3F;
342  }
343 
344  src = s->y_buffer;
345  ybuf = s->pic->data[0];
346  for (j = 0; j < avctx->height; j++) {
347  for (i = 0; i < avctx->width; i++)
348  ybuf[i] = (src[i] << 2) | (src[i] >> 3);
349  src += avctx->width;
350  ybuf += s->pic->linesize[0];
351  }
352 
353  return 0;
354 }
355 
357 {
358  XanContext *s = avctx->priv_data;
359  uint8_t *ybuf, *src = s->scratch_buffer;
360  int cur, last;
361  int i, j;
362  int ret;
363 
364  if ((ret = xan_decode_chroma(avctx, bytestream2_get_le32(&s->gb))) != 0)
365  return ret;
366 
367  bytestream2_seek(&s->gb, 16, SEEK_SET);
369  s->buffer_size >> 1);
370  if (ret) {
371  av_log(avctx, AV_LOG_ERROR, "Luma decoding failed\n");
372  return ret;
373  }
374 
375  ybuf = s->y_buffer;
376  for (i = 0; i < avctx->height; i++) {
377  last = (ybuf[0] + (*src++ << 1)) & 0x3F;
378  ybuf[0] = last;
379  for (j = 1; j < avctx->width - 1; j += 2) {
380  cur = (ybuf[j + 1] + (*src++ << 1)) & 0x3F;
381  ybuf[j] = (last + cur) >> 1;
382  ybuf[j+1] = cur;
383  last = cur;
384  }
385  ybuf[j] = last;
386  ybuf += avctx->width;
387  }
388 
389  src = s->y_buffer;
390  ybuf = s->pic->data[0];
391  for (j = 0; j < avctx->height; j++) {
392  for (i = 0; i < avctx->width; i++)
393  ybuf[i] = (src[i] << 2) | (src[i] >> 3);
394  src += avctx->width;
395  ybuf += s->pic->linesize[0];
396  }
397 
398  return 0;
399 }
400 
402  void *data, int *got_frame,
403  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(data, s->pic)) < 0)
429  return ret;
430 
431  *got_frame = 1;
432 
433  return avpkt->size;
434 }
435 
437  .name = "xan_wc4",
438  .long_name = NULL_IF_CONFIG_SMALL("Wing Commander IV / Xxan"),
439  .type = AVMEDIA_TYPE_VIDEO,
440  .id = AV_CODEC_ID_XAN_WC4,
441  .priv_data_size = sizeof(XanContext),
443  .close = xan_decode_end,
445  .capabilities = AV_CODEC_CAP_DR1,
447 };
AVCodec
AVCodec.
Definition: codec.h:202
AV_LOG_WARNING
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:186
FF_CODEC_CAP_INIT_THREADSAFE
#define FF_CODEC_CAP_INIT_THREADSAFE
The codec does not modify any global variables in the init function, allowing to call the init functi...
Definition: internal.h:42
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:277
XanContext::scratch_buffer
uint8_t * scratch_buffer
Definition: xxan.c:35
av_frame_free
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:109
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:317
internal.h
AVPacket::data
uint8_t * data
Definition: packet.h:373
xan_unpack
static int xan_unpack(XanContext *s, uint8_t *dest, const int dest_len)
Definition: xxan.c:126
table
static const uint16_t table[]
Definition: prosumer.c:206
data
const char data[16]
Definition: mxf.c:143
xan_decode_frame_type1
static int xan_decode_frame_type1(AVCodecContext *avctx)
Definition: xxan.c:356
XanContext::gb
GetByteContext gb
Definition: xxan.c:37
av_malloc
#define av_malloc(s)
Definition: tableprint_vlc.h:31
init
static int init
Definition: av_tx.c:47
bit
#define bit(string, value)
Definition: cbs_mpeg2.c:58
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:40
finish
static void finish(void)
Definition: movenc.c:342
U
#define U(x)
Definition: vp56_arith.h:37
val
static double val(void *priv, double ch)
Definition: aeval.c:76
xan_decode_frame
static int xan_decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: xxan.c:401
av_frame_alloc
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition: frame.c:97
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
av_cold
#define av_cold
Definition: attributes.h:90
ff_xan_wc4_decoder
const AVCodec ff_xan_wc4_decoder
Definition: xxan.c:436
mask
static const uint16_t mask[17]
Definition: lzw.c:38
av_memcpy_backptr
void av_memcpy_backptr(uint8_t *dst, int back, int cnt)
Overlapping memcpy() implementation.
Definition: mem.c:454
decode
static void decode(AVCodecContext *dec_ctx, AVPacket *pkt, AVFrame *frame, FILE *outfile)
Definition: decode_audio.c:71
intreadwrite.h
s
#define s(width, name)
Definition: cbs_vp9.c:257
xan_decode_init
static av_cold int xan_decode_init(AVCodecContext *avctx)
Definition: xxan.c:52
bits
uint8_t bits
Definition: vp3data.h:141
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:66
AV_CODEC_ID_XAN_WC4
@ AV_CODEC_ID_XAN_WC4
Definition: codec_id.h:91
XanContext::y_buffer
uint8_t * y_buffer
Definition: xxan.c:34
V
#define V
Definition: avdct.c:30
src
#define src
Definition: vp8dsp.c:255
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
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:374
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:117
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:325
size
int size
Definition: twinvq_data.h:10344
XanContext::avctx
AVCodecContext * avctx
Definition: xan.c:55
XanContext::pic
AVFrame * pic
Definition: xxan.c:32
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
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:271
XanContext
Definition: xan.c:53
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: internal.h:50
xan_decode_chroma
static int xan_decode_chroma(AVCodecContext *avctx, unsigned chroma_off)
Definition: xxan.c:179
FFMIN
#define FFMIN(a, b)
Definition: macros.h:49
AVCodec::name
const char * name
Name of the codec implementation.
Definition: codec.h:209
AVCodecContext::height
int height
Definition: avcodec.h:556
AVCodecContext::pix_fmt
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:593
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:1759
ret
ret
Definition: filter_design.txt:187
AVCodecContext
main external API structure.
Definition: avcodec.h:383
mode
mode
Definition: ebur128.h:83
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
XanContext::buffer_size
int buffer_size
Definition: xxan.c:36
mem.h
AVPacket
This structure stores compressed data.
Definition: packet.h:350
AVCodecContext::priv_data
void * priv_data
Definition: avcodec.h:410
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:35
AVCodecContext::width
int width
picture width / height.
Definition: avcodec.h:556
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:28
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:61
xan_unpack_luma
static int xan_unpack_luma(XanContext *s, uint8_t *dst, const int dst_size)
Definition: xxan.c:84