FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/xxan.c
Date: 2022-12-09 07:38:14
Exec Total Coverage
Lines: 228 267 85.4%
Functions: 8 8 100.0%
Branches: 87 123 70.7%

Line Branch Exec Source
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 {
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 3 static av_cold int xan_decode_end(AVCodecContext *avctx)
42 {
43 3 XanContext *s = avctx->priv_data;
44
45 3 av_frame_free(&s->pic);
46
47 3 av_freep(&s->y_buffer);
48 3 av_freep(&s->scratch_buffer);
49
50 3 return 0;
51 }
52
53 3 static av_cold int xan_decode_init(AVCodecContext *avctx)
54 {
55 3 XanContext *s = avctx->priv_data;
56
57 3 s->avctx = avctx;
58
59 3 avctx->pix_fmt = AV_PIX_FMT_YUV420P;
60
61
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (avctx->height < 8) {
62 av_log(avctx, AV_LOG_ERROR, "Invalid frame height: %d.\n", avctx->height);
63 return AVERROR(EINVAL);
64 }
65
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 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 3 s->buffer_size = avctx->width * avctx->height;
71 3 s->y_buffer = av_malloc(s->buffer_size);
72
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (!s->y_buffer)
73 return AVERROR(ENOMEM);
74 3 s->scratch_buffer = av_malloc(s->buffer_size + 130);
75
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (!s->scratch_buffer)
76 return AVERROR(ENOMEM);
77
78 3 s->pic = av_frame_alloc();
79
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (!s->pic)
80 return AVERROR(ENOMEM);
81
82 3 return 0;
83 }
84
85 21 static int xan_unpack_luma(XanContext *s,
86 uint8_t *dst, const int dst_size)
87 {
88 int tree_size, eof;
89 int bits, mask;
90 int tree_root, node;
91 21 const uint8_t *dst_end = dst + dst_size;
92 21 GetByteContext tree = s->gb;
93 21 int start_off = bytestream2_tell(&tree);
94
95 21 tree_size = bytestream2_get_byte(&s->gb);
96 21 eof = bytestream2_get_byte(&s->gb);
97 21 tree_root = eof + tree_size;
98 21 bytestream2_skip(&s->gb, tree_size * 2);
99
100 21 node = tree_root;
101 21 bits = bytestream2_get_byte(&s->gb);
102 21 mask = 0x80;
103 691350 for (;;) {
104 691371 int bit = !!(bits & mask);
105 691371 mask >>= 1;
106 691371 bytestream2_seek(&tree, start_off + node*2 + bit - eof * 2, SEEK_SET);
107 691371 node = bytestream2_get_byte(&tree);
108
2/2
✓ Branch 0 taken 21 times.
✓ Branch 1 taken 691350 times.
691371 if (node == eof)
109 21 break;
110
2/2
✓ Branch 0 taken 554400 times.
✓ Branch 1 taken 136950 times.
691350 if (node < eof) {
111 554400 *dst++ = node;
112
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 554400 times.
554400 if (dst > dst_end)
113 break;
114 554400 node = tree_root;
115 }
116
2/2
✓ Branch 0 taken 86409 times.
✓ Branch 1 taken 604941 times.
691350 if (!mask) {
117
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 86409 times.
86409 if (bytestream2_get_bytes_left(&s->gb) <= 0)
118 break;
119 86409 bits = bytestream2_get_byteu(&s->gb);
120 86409 mask = 0x80;
121 }
122 }
123
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 21 times.
21 return dst != dst_end ? AVERROR_INVALIDDATA : 0;
124 }
125
126 /* almost the same as in xan_wc3 decoder */
127 26 static int xan_unpack(XanContext *s,
128 uint8_t *dest, const int dest_len)
129 {
130 uint8_t opcode;
131 int size;
132 26 uint8_t *orig_dest = dest;
133 26 const uint8_t *dest_end = dest + dest_len;
134
135
2/2
✓ Branch 0 taken 11856 times.
✓ Branch 1 taken 5 times.
11861 while (dest < dest_end) {
136
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 11856 times.
11856 if (bytestream2_get_bytes_left(&s->gb) <= 0)
137 return AVERROR_INVALIDDATA;
138
139 11856 opcode = bytestream2_get_byteu(&s->gb);
140
141
2/2
✓ Branch 0 taken 11348 times.
✓ Branch 1 taken 508 times.
11856 if (opcode < 0xe0) {
142 int size2, back;
143
2/2
✓ Branch 0 taken 7882 times.
✓ Branch 1 taken 3466 times.
11348 if ((opcode & 0x80) == 0) {
144 7882 size = opcode & 3;
145 7882 back = ((opcode & 0x60) << 3) + bytestream2_get_byte(&s->gb) + 1;
146 7882 size2 = ((opcode & 0x1c) >> 2) + 3;
147
2/2
✓ Branch 0 taken 3066 times.
✓ Branch 1 taken 400 times.
3466 } else if ((opcode & 0x40) == 0) {
148 3066 size = bytestream2_peek_byte(&s->gb) >> 6;
149 3066 back = (bytestream2_get_be16(&s->gb) & 0x3fff) + 1;
150 3066 size2 = (opcode & 0x3f) + 4;
151 } else {
152 400 size = opcode & 3;
153 400 back = ((opcode & 0x10) << 12) + bytestream2_get_be16(&s->gb) + 1;
154 400 size2 = ((opcode & 0x0c) << 6) + bytestream2_get_byte(&s->gb) + 5;
155
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 400 times.
400 if (size + size2 > dest_end - dest)
156 break;
157 }
158
1/2
✓ Branch 0 taken 11348 times.
✗ Branch 1 not taken.
11348 if (dest + size + size2 > dest_end ||
159
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11348 times.
11348 dest - orig_dest + size < back)
160 return AVERROR_INVALIDDATA;
161 11348 bytestream2_get_buffer(&s->gb, dest, size);
162 11348 dest += size;
163 11348 av_memcpy_backptr(dest, back, size2);
164 11348 dest += size2;
165 } else {
166 508 int finish = opcode >= 0xfc;
167
168
2/2
✓ Branch 0 taken 21 times.
✓ Branch 1 taken 487 times.
508 size = finish ? opcode & 3 : ((opcode & 0x1f) << 2) + 4;
169
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 508 times.
508 if (dest_end - dest < size)
170 return AVERROR_INVALIDDATA;
171 508 bytestream2_get_buffer(&s->gb, dest, size);
172 508 dest += size;
173
2/2
✓ Branch 0 taken 21 times.
✓ Branch 1 taken 487 times.
508 if (finish)
174 21 break;
175 }
176 }
177 26 return dest - orig_dest;
178 }
179
180 21 static int xan_decode_chroma(AVCodecContext *avctx, unsigned chroma_off)
181 {
182 21 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
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 21 times.
21 if (!chroma_off)
191 return 0;
192
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 21 times.
21 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 21 bytestream2_seek(&s->gb, chroma_off + 4, SEEK_SET);
197 21 mode = bytestream2_get_le16(&s->gb);
198 21 table = s->gb.buffer;
199 21 table_size = bytestream2_get_le16(&s->gb);
200 21 offset = table_size * 2;
201 21 table_size += 1;
202
203
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 21 times.
21 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 21 bytestream2_skip(&s->gb, offset);
209 21 memset(s->scratch_buffer, 0, s->buffer_size);
210 21 dec_size = xan_unpack(s, s->scratch_buffer, s->buffer_size);
211
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 21 times.
21 if (dec_size < 0) {
212 av_log(avctx, AV_LOG_ERROR, "Chroma unpacking failed\n");
213 return dec_size;
214 }
215
216 21 U = s->pic->data[1];
217 21 V = s->pic->data[2];
218 21 src = s->scratch_buffer;
219 21 src_end = src + dec_size;
220
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 17 times.
21 if (mode) {
221
2/2
✓ Branch 0 taken 328 times.
✓ Branch 1 taken 4 times.
332 for (j = 0; j < avctx->height >> 1; j++) {
222
2/2
✓ Branch 0 taken 52480 times.
✓ Branch 1 taken 328 times.
52808 for (i = 0; i < avctx->width >> 1; i++) {
223
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 52480 times.
52480 if (src_end - src < 1)
224 return 0;
225 52480 val = *src++;
226
1/2
✓ Branch 0 taken 52480 times.
✗ Branch 1 not taken.
52480 if (val) {
227
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 52480 times.
52480 if (val >= table_size)
228 return AVERROR_INVALIDDATA;
229 52480 val = AV_RL16(table + (val << 1));
230 52480 uval = (val >> 3) & 0xF8;
231 52480 vval = (val >> 8) & 0xF8;
232 52480 U[i] = uval | (uval >> 5);
233 52480 V[i] = vval | (vval >> 5);
234 }
235 }
236 328 U += s->pic->linesize[1];
237 328 V += s->pic->linesize[2];
238 }
239
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 if (avctx->height & 1) {
240 4 memcpy(U, U - s->pic->linesize[1], avctx->width >> 1);
241 4 memcpy(V, V - s->pic->linesize[2], avctx->width >> 1);
242 }
243 } else {
244 17 uint8_t *U2 = U + s->pic->linesize[1];
245 17 uint8_t *V2 = V + s->pic->linesize[2];
246
247
2/2
✓ Branch 0 taken 697 times.
✓ Branch 1 taken 17 times.
714 for (j = 0; j < avctx->height >> 2; j++) {
248
2/2
✓ Branch 0 taken 55760 times.
✓ Branch 1 taken 697 times.
56457 for (i = 0; i < avctx->width >> 1; i += 2) {
249
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 55760 times.
55760 if (src_end - src < 1)
250 return 0;
251 55760 val = *src++;
252
2/2
✓ Branch 0 taken 17613 times.
✓ Branch 1 taken 38147 times.
55760 if (val) {
253
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 17613 times.
17613 if (val >= table_size)
254 return AVERROR_INVALIDDATA;
255 17613 val = AV_RL16(table + (val << 1));
256 17613 uval = (val >> 3) & 0xF8;
257 17613 vval = (val >> 8) & 0xF8;
258 17613 U[i] = U[i+1] = U2[i] = U2[i+1] = uval | (uval >> 5);
259 17613 V[i] = V[i+1] = V2[i] = V2[i+1] = vval | (vval >> 5);
260 }
261 }
262 697 U += s->pic->linesize[1] * 2;
263 697 V += s->pic->linesize[2] * 2;
264 697 U2 += s->pic->linesize[1] * 2;
265 697 V2 += s->pic->linesize[2] * 2;
266 }
267
1/2
✓ Branch 0 taken 17 times.
✗ Branch 1 not taken.
17 if (avctx->height & 3) {
268 17 int lines = ((avctx->height + 1) >> 1) - (avctx->height >> 2) * 2;
269
270 17 memcpy(U, U - lines * s->pic->linesize[1], lines * s->pic->linesize[1]);
271 17 memcpy(V, V - lines * s->pic->linesize[2], lines * s->pic->linesize[2]);
272 }
273 }
274
275 21 return 0;
276 }
277
278 5 static int xan_decode_frame_type0(AVCodecContext *avctx)
279 {
280 5 XanContext *s = avctx->priv_data;
281 5 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 5 chroma_off = bytestream2_get_le32(&s->gb);
288 5 corr_off = bytestream2_get_le32(&s->gb);
289
290
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 5 times.
5 if ((ret = xan_decode_chroma(avctx, chroma_off)) != 0)
291 return ret;
292
293
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 5 times.
5 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 5 bytestream2_seek(&s->gb, 12, SEEK_SET);
298 5 ret = xan_unpack_luma(s, src, s->buffer_size >> 1);
299
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
5 if (ret) {
300 av_log(avctx, AV_LOG_ERROR, "Luma decoding failed\n");
301 return ret;
302 }
303
304 5 ybuf = s->y_buffer;
305 5 last = *src++;
306 5 ybuf[0] = last << 1;
307
2/2
✓ Branch 0 taken 795 times.
✓ Branch 1 taken 5 times.
800 for (j = 1; j < avctx->width - 1; j += 2) {
308 795 cur = (last + *src++) & 0x1F;
309 795 ybuf[j] = last + cur;
310 795 ybuf[j+1] = cur << 1;
311 795 last = cur;
312 }
313 5 ybuf[j] = last << 1;
314 5 prev_buf = ybuf;
315 5 ybuf += avctx->width;
316
317
2/2
✓ Branch 0 taken 820 times.
✓ Branch 1 taken 5 times.
825 for (i = 1; i < avctx->height; i++) {
318 820 last = ((prev_buf[0] >> 1) + *src++) & 0x1F;
319 820 ybuf[0] = last << 1;
320
2/2
✓ Branch 0 taken 130380 times.
✓ Branch 1 taken 820 times.
131200 for (j = 1; j < avctx->width - 1; j += 2) {
321 130380 cur = ((prev_buf[j + 1] >> 1) + *src++) & 0x1F;
322 130380 ybuf[j] = last + cur;
323 130380 ybuf[j+1] = cur << 1;
324 130380 last = cur;
325 }
326 820 ybuf[j] = last << 1;
327 820 prev_buf = ybuf;
328 820 ybuf += avctx->width;
329 }
330
331
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
5 if (corr_off) {
332 int dec_size;
333
334 5 bytestream2_seek(&s->gb, 8 + corr_off, SEEK_SET);
335 5 dec_size = xan_unpack(s, s->scratch_buffer, s->buffer_size / 2);
336
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
5 if (dec_size < 0)
337 dec_size = 0;
338 else
339
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
5 dec_size = FFMIN(dec_size, s->buffer_size/2 - 1);
340
341
2/2
✓ Branch 0 taken 131995 times.
✓ Branch 1 taken 5 times.
132000 for (i = 0; i < dec_size; i++)
342 131995 s->y_buffer[i*2+1] = (s->y_buffer[i*2+1] + (s->scratch_buffer[i] << 1)) & 0x3F;
343 }
344
345 5 src = s->y_buffer;
346 5 ybuf = s->pic->data[0];
347
2/2
✓ Branch 0 taken 825 times.
✓ Branch 1 taken 5 times.
830 for (j = 0; j < avctx->height; j++) {
348
2/2
✓ Branch 0 taken 264000 times.
✓ Branch 1 taken 825 times.
264825 for (i = 0; i < avctx->width; i++)
349 264000 ybuf[i] = (src[i] << 2) | (src[i] >> 3);
350 825 src += avctx->width;
351 825 ybuf += s->pic->linesize[0];
352 }
353
354 5 return 0;
355 }
356
357 16 static int xan_decode_frame_type1(AVCodecContext *avctx)
358 {
359 16 XanContext *s = avctx->priv_data;
360 16 uint8_t *ybuf, *src = s->scratch_buffer;
361 int cur, last;
362 int i, j;
363 int ret;
364
365
1/2
✗ Branch 2 not taken.
✓ Branch 3 taken 16 times.
16 if ((ret = xan_decode_chroma(avctx, bytestream2_get_le32(&s->gb))) != 0)
366 return ret;
367
368 16 bytestream2_seek(&s->gb, 16, SEEK_SET);
369 16 ret = xan_unpack_luma(s, src,
370 16 s->buffer_size >> 1);
371
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 16 times.
16 if (ret) {
372 av_log(avctx, AV_LOG_ERROR, "Luma decoding failed\n");
373 return ret;
374 }
375
376 16 ybuf = s->y_buffer;
377
2/2
✓ Branch 0 taken 2640 times.
✓ Branch 1 taken 16 times.
2656 for (i = 0; i < avctx->height; i++) {
378 2640 last = (ybuf[0] + (*src++ << 1)) & 0x3F;
379 2640 ybuf[0] = last;
380
2/2
✓ Branch 0 taken 419760 times.
✓ Branch 1 taken 2640 times.
422400 for (j = 1; j < avctx->width - 1; j += 2) {
381 419760 cur = (ybuf[j + 1] + (*src++ << 1)) & 0x3F;
382 419760 ybuf[j] = (last + cur) >> 1;
383 419760 ybuf[j+1] = cur;
384 419760 last = cur;
385 }
386 2640 ybuf[j] = last;
387 2640 ybuf += avctx->width;
388 }
389
390 16 src = s->y_buffer;
391 16 ybuf = s->pic->data[0];
392
2/2
✓ Branch 0 taken 2640 times.
✓ Branch 1 taken 16 times.
2656 for (j = 0; j < avctx->height; j++) {
393
2/2
✓ Branch 0 taken 844800 times.
✓ Branch 1 taken 2640 times.
847440 for (i = 0; i < avctx->width; i++)
394 844800 ybuf[i] = (src[i] << 2) | (src[i] >> 3);
395 2640 src += avctx->width;
396 2640 ybuf += s->pic->linesize[0];
397 }
398
399 16 return 0;
400 }
401
402 21 static int xan_decode_frame(AVCodecContext *avctx, AVFrame *rframe,
403 int *got_frame, AVPacket *avpkt)
404 {
405 21 XanContext *s = avctx->priv_data;
406 int ftype;
407 int ret;
408
409
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 21 times.
21 if ((ret = ff_reget_buffer(avctx, s->pic, 0)) < 0)
410 return ret;
411
412 21 bytestream2_init(&s->gb, avpkt->data, avpkt->size);
413 21 ftype = bytestream2_get_le32(&s->gb);
414
2/3
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 16 times.
✗ Branch 2 not taken.
21 switch (ftype) {
415 5 case 0:
416 5 ret = xan_decode_frame_type0(avctx);
417 5 break;
418 16 case 1:
419 16 ret = xan_decode_frame_type1(avctx);
420 16 break;
421 default:
422 av_log(avctx, AV_LOG_ERROR, "Unknown frame type %d\n", ftype);
423 return AVERROR_INVALIDDATA;
424 }
425
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 21 times.
21 if (ret)
426 return ret;
427
428
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 21 times.
21 if ((ret = av_frame_ref(rframe, s->pic)) < 0)
429 return ret;
430
431 21 *got_frame = 1;
432
433 21 return avpkt->size;
434 }
435
436 const FFCodec ff_xan_wc4_decoder = {
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),
442 .init = xan_decode_init,
443 .close = xan_decode_end,
444 FF_CODEC_DECODE_CB(xan_decode_frame),
445 .p.capabilities = AV_CODEC_CAP_DR1,
446 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
447 };
448