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