FFmpeg
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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 
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 
53  s->buffer_size = avctx->width * avctx->height;
55  if (!s->y_buffer)
56  return AVERROR(ENOMEM);
57  s->scratch_buffer = av_malloc(s->buffer_size + 130);
58  if (!s->scratch_buffer) {
59  av_freep(&s->y_buffer);
60  return AVERROR(ENOMEM);
61  }
62 
63  return 0;
64 }
65 
67  uint8_t *dst, const int dst_size)
68 {
69  int tree_size, eof;
70  int bits, mask;
71  int tree_root, node;
72  const uint8_t *dst_end = dst + dst_size;
73  GetByteContext tree = s->gb;
74  int start_off = bytestream2_tell(&tree);
75 
76  tree_size = bytestream2_get_byte(&s->gb);
77  eof = bytestream2_get_byte(&s->gb);
78  tree_root = eof + tree_size;
79  bytestream2_skip(&s->gb, tree_size * 2);
80 
81  node = tree_root;
82  bits = bytestream2_get_byte(&s->gb);
83  mask = 0x80;
84  for (;;) {
85  int bit = !!(bits & mask);
86  mask >>= 1;
87  bytestream2_seek(&tree, start_off + node*2 + bit - eof * 2, SEEK_SET);
88  node = bytestream2_get_byte(&tree);
89  if (node == eof)
90  break;
91  if (node < eof) {
92  *dst++ = node;
93  if (dst > dst_end)
94  break;
95  node = tree_root;
96  }
97  if (!mask) {
98  if (bytestream2_get_bytes_left(&s->gb) <= 0)
99  break;
100  bits = bytestream2_get_byteu(&s->gb);
101  mask = 0x80;
102  }
103  }
104  return dst != dst_end ? AVERROR_INVALIDDATA : 0;
105 }
106 
107 /* almost the same as in xan_wc3 decoder */
108 static int xan_unpack(XanContext *s,
109  uint8_t *dest, const int dest_len)
110 {
111  uint8_t opcode;
112  int size;
113  uint8_t *orig_dest = dest;
114  const uint8_t *dest_end = dest + dest_len;
115 
116  while (dest < dest_end) {
117  if (bytestream2_get_bytes_left(&s->gb) <= 0)
118  return AVERROR_INVALIDDATA;
119 
120  opcode = bytestream2_get_byteu(&s->gb);
121 
122  if (opcode < 0xe0) {
123  int size2, back;
124  if ((opcode & 0x80) == 0) {
125  size = opcode & 3;
126  back = ((opcode & 0x60) << 3) + bytestream2_get_byte(&s->gb) + 1;
127  size2 = ((opcode & 0x1c) >> 2) + 3;
128  } else if ((opcode & 0x40) == 0) {
129  size = bytestream2_peek_byte(&s->gb) >> 6;
130  back = (bytestream2_get_be16(&s->gb) & 0x3fff) + 1;
131  size2 = (opcode & 0x3f) + 4;
132  } else {
133  size = opcode & 3;
134  back = ((opcode & 0x10) << 12) + bytestream2_get_be16(&s->gb) + 1;
135  size2 = ((opcode & 0x0c) << 6) + bytestream2_get_byte(&s->gb) + 5;
136  if (size + size2 > dest_end - dest)
137  break;
138  }
139  if (dest + size + size2 > dest_end ||
140  dest - orig_dest + size < back)
141  return AVERROR_INVALIDDATA;
142  bytestream2_get_buffer(&s->gb, dest, size);
143  dest += size;
144  av_memcpy_backptr(dest, back, size2);
145  dest += size2;
146  } else {
147  int finish = opcode >= 0xfc;
148 
149  size = finish ? opcode & 3 : ((opcode & 0x1f) << 2) + 4;
150  if (dest_end - dest < size)
151  return AVERROR_INVALIDDATA;
152  bytestream2_get_buffer(&s->gb, dest, size);
153  dest += size;
154  if (finish)
155  break;
156  }
157  }
158  return dest - orig_dest;
159 }
160 
161 static int xan_decode_chroma(AVCodecContext *avctx, unsigned chroma_off)
162 {
163  XanContext *s = avctx->priv_data;
164  uint8_t *U, *V;
165  int val, uval, vval;
166  int i, j;
167  const uint8_t *src, *src_end;
168  const uint8_t *table;
169  int mode, offset, dec_size, table_size;
170 
171  if (!chroma_off)
172  return 0;
173  if (chroma_off + 4 >= bytestream2_get_bytes_left(&s->gb)) {
174  av_log(avctx, AV_LOG_ERROR, "Invalid chroma block position\n");
175  return AVERROR_INVALIDDATA;
176  }
177  bytestream2_seek(&s->gb, chroma_off + 4, SEEK_SET);
178  mode = bytestream2_get_le16(&s->gb);
179  table = s->gb.buffer;
180  table_size = bytestream2_get_le16(&s->gb);
181  offset = table_size * 2;
182  table_size += 1;
183 
184  if (offset >= bytestream2_get_bytes_left(&s->gb)) {
185  av_log(avctx, AV_LOG_ERROR, "Invalid chroma block offset\n");
186  return AVERROR_INVALIDDATA;
187  }
188 
189  bytestream2_skip(&s->gb, offset);
190  memset(s->scratch_buffer, 0, s->buffer_size);
191  dec_size = xan_unpack(s, s->scratch_buffer, s->buffer_size);
192  if (dec_size < 0) {
193  av_log(avctx, AV_LOG_ERROR, "Chroma unpacking failed\n");
194  return AVERROR_INVALIDDATA;
195  }
196 
197  U = s->pic.data[1];
198  V = s->pic.data[2];
199  src = s->scratch_buffer;
200  src_end = src + dec_size;
201  if (mode) {
202  for (j = 0; j < avctx->height >> 1; j++) {
203  for (i = 0; i < avctx->width >> 1; i++) {
204  if (src_end - src < 1)
205  return 0;
206  val = *src++;
207  if (val) {
208  if (val >= table_size)
209  return AVERROR_INVALIDDATA;
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  }
217  U += s->pic.linesize[1];
218  V += s->pic.linesize[2];
219  }
220  if (avctx->height & 1) {
221  memcpy(U, U - s->pic.linesize[1], avctx->width >> 1);
222  memcpy(V, V - s->pic.linesize[2], avctx->width >> 1);
223  }
224  } else {
225  uint8_t *U2 = U + s->pic.linesize[1];
226  uint8_t *V2 = V + s->pic.linesize[2];
227 
228  for (j = 0; j < avctx->height >> 2; j++) {
229  for (i = 0; i < avctx->width >> 1; i += 2) {
230  if (src_end - src < 1)
231  return 0;
232  val = *src++;
233  if (val) {
234  if (val >= table_size)
235  return AVERROR_INVALIDDATA;
236  val = AV_RL16(table + (val << 1));
237  uval = (val >> 3) & 0xF8;
238  vval = (val >> 8) & 0xF8;
239  U[i] = U[i+1] = U2[i] = U2[i+1] = uval | (uval >> 5);
240  V[i] = V[i+1] = V2[i] = V2[i+1] = vval | (vval >> 5);
241  }
242  }
243  U += s->pic.linesize[1] * 2;
244  V += s->pic.linesize[2] * 2;
245  U2 += s->pic.linesize[1] * 2;
246  V2 += s->pic.linesize[2] * 2;
247  }
248  if (avctx->height & 3) {
249  int lines = ((avctx->height + 1) >> 1) - (avctx->height >> 2) * 2;
250 
251  memcpy(U, U - lines * s->pic.linesize[1], lines * s->pic.linesize[1]);
252  memcpy(V, V - lines * s->pic.linesize[2], lines * s->pic.linesize[2]);
253  }
254  }
255 
256  return 0;
257 }
258 
260 {
261  XanContext *s = avctx->priv_data;
262  uint8_t *ybuf, *prev_buf, *src = s->scratch_buffer;
263  unsigned chroma_off, corr_off;
264  int cur, last;
265  int i, j;
266  int ret;
267 
268  chroma_off = bytestream2_get_le32(&s->gb);
269  corr_off = bytestream2_get_le32(&s->gb);
270 
271  if ((ret = xan_decode_chroma(avctx, chroma_off)) != 0)
272  return ret;
273 
274  if (corr_off >= bytestream2_size(&s->gb)) {
275  av_log(avctx, AV_LOG_WARNING, "Ignoring invalid correction block position\n");
276  corr_off = 0;
277  }
278  bytestream2_seek(&s->gb, 12, SEEK_SET);
279  ret = xan_unpack_luma(s, src, s->buffer_size >> 1);
280  if (ret) {
281  av_log(avctx, AV_LOG_ERROR, "Luma decoding failed\n");
282  return ret;
283  }
284 
285  ybuf = s->y_buffer;
286  last = *src++;
287  ybuf[0] = last << 1;
288  for (j = 1; j < avctx->width - 1; j += 2) {
289  cur = (last + *src++) & 0x1F;
290  ybuf[j] = last + cur;
291  ybuf[j+1] = cur << 1;
292  last = cur;
293  }
294  if(j < avctx->width)
295  ybuf[j] = last << 1;
296  prev_buf = ybuf;
297  ybuf += avctx->width;
298 
299  for (i = 1; i < avctx->height; i++) {
300  last = ((prev_buf[0] >> 1) + *src++) & 0x1F;
301  ybuf[0] = last << 1;
302  for (j = 1; j < avctx->width - 1; j += 2) {
303  cur = ((prev_buf[j + 1] >> 1) + *src++) & 0x1F;
304  ybuf[j] = last + cur;
305  ybuf[j+1] = cur << 1;
306  last = cur;
307  }
308  if(j < avctx->width)
309  ybuf[j] = last << 1;
310  prev_buf = ybuf;
311  ybuf += avctx->width;
312  }
313 
314  if (corr_off) {
315  int dec_size;
316 
317  bytestream2_seek(&s->gb, 8 + corr_off, SEEK_SET);
318  dec_size = xan_unpack(s, s->scratch_buffer, s->buffer_size / 2);
319  if (dec_size < 0)
320  dec_size = 0;
321  else
322  dec_size = FFMIN(dec_size, s->buffer_size/2 - 1);
323 
324  for (i = 0; i < dec_size; i++)
325  s->y_buffer[i*2+1] = (s->y_buffer[i*2+1] + (s->scratch_buffer[i] << 1)) & 0x3F;
326  }
327 
328  src = s->y_buffer;
329  ybuf = s->pic.data[0];
330  for (j = 0; j < avctx->height; j++) {
331  for (i = 0; i < avctx->width; i++)
332  ybuf[i] = (src[i] << 2) | (src[i] >> 3);
333  src += avctx->width;
334  ybuf += s->pic.linesize[0];
335  }
336 
337  return 0;
338 }
339 
341 {
342  XanContext *s = avctx->priv_data;
343  uint8_t *ybuf, *src = s->scratch_buffer;
344  int cur, last;
345  int i, j;
346  int ret;
347 
348  if ((ret = xan_decode_chroma(avctx, bytestream2_get_le32(&s->gb))) != 0)
349  return ret;
350 
351  bytestream2_seek(&s->gb, 16, SEEK_SET);
352  ret = xan_unpack_luma(s, src,
353  s->buffer_size >> 1);
354  if (ret) {
355  av_log(avctx, AV_LOG_ERROR, "Luma decoding failed\n");
356  return ret;
357  }
358 
359  ybuf = s->y_buffer;
360  for (i = 0; i < avctx->height; i++) {
361  last = (ybuf[0] + (*src++ << 1)) & 0x3F;
362  ybuf[0] = last;
363  for (j = 1; j < avctx->width - 1; j += 2) {
364  cur = (ybuf[j + 1] + (*src++ << 1)) & 0x3F;
365  ybuf[j] = (last + cur) >> 1;
366  ybuf[j+1] = cur;
367  last = cur;
368  }
369  if(j < avctx->width)
370  ybuf[j] = last;
371  ybuf += avctx->width;
372  }
373 
374  src = s->y_buffer;
375  ybuf = s->pic.data[0];
376  for (j = 0; j < avctx->height; j++) {
377  for (i = 0; i < avctx->width; i++)
378  ybuf[i] = (src[i] << 2) | (src[i] >> 3);
379  src += avctx->width;
380  ybuf += s->pic.linesize[0];
381  }
382 
383  return 0;
384 }
385 
387  void *data, int *got_frame,
388  AVPacket *avpkt)
389 {
390  XanContext *s = avctx->priv_data;
391  int ftype;
392  int ret;
393 
394  s->pic.reference = 3;
398  if ((ret = avctx->reget_buffer(avctx, &s->pic))) {
399  av_log(s->avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
400  return ret;
401  }
402 
403  bytestream2_init(&s->gb, avpkt->data, avpkt->size);
404  ftype = bytestream2_get_le32(&s->gb);
405  switch (ftype) {
406  case 0:
407  ret = xan_decode_frame_type0(avctx);
408  break;
409  case 1:
410  ret = xan_decode_frame_type1(avctx);
411  break;
412  default:
413  av_log(avctx, AV_LOG_ERROR, "Unknown frame type %d\n", ftype);
414  return AVERROR_INVALIDDATA;
415  }
416  if (ret)
417  return ret;
418 
419  *got_frame = 1;
420  *(AVFrame*)data = s->pic;
421 
422  return avpkt->size;
423 }
424 
426 {
427  XanContext *s = avctx->priv_data;
428 
429  if (s->pic.data[0])
430  avctx->release_buffer(avctx, &s->pic);
431 
432  av_freep(&s->y_buffer);
434 
435  return 0;
436 }
437 
439  .name = "xan_wc4",
440  .type = AVMEDIA_TYPE_VIDEO,
441  .id = AV_CODEC_ID_XAN_WC4,
442  .priv_data_size = sizeof(XanContext),
446  .capabilities = CODEC_CAP_DR1,
447  .long_name = NULL_IF_CONFIG_SMALL("Wing Commander IV / Xxan"),
448 };