Coverage Report

Created: 2026-02-14 06:59

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/ffmpeg/libavcodec/argo.c
Line
Count
Source
1
/*
2
 * Argonaut Games Video decoder
3
 * Copyright (c) 2020 Paul B Mahol
4
 *
5
 * This file is part of FFmpeg.
6
 *
7
 * FFmpeg is free software; you can redistribute it and/or
8
 * modify it under the terms of the GNU Lesser General Public
9
 * License as published by the Free Software Foundation; either
10
 * version 2.1 of the License, or (at your option) any later version.
11
 *
12
 * FFmpeg is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15
 * Lesser General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Lesser General Public
18
 * License along with FFmpeg; if not, write to the Free Software
19
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20
 */
21
22
#include <string.h>
23
24
#include "libavutil/attributes.h"
25
#include "libavutil/internal.h"
26
#include "libavutil/intreadwrite.h"
27
28
#include "avcodec.h"
29
#include "bytestream.h"
30
#include "codec_internal.h"
31
#include "decode.h"
32
33
typedef struct ArgoContext {
34
    GetByteContext gb;
35
36
    int bpp;
37
    int key;
38
    int mv0[128][2];
39
    int mv1[16][2];
40
    uint32_t pal[256];
41
    AVFrame *frame;
42
} ArgoContext;
43
44
static int decode_pal8(AVCodecContext *avctx, uint32_t *pal)
45
1.95k
{
46
1.95k
    ArgoContext *s = avctx->priv_data;
47
1.95k
    GetByteContext *gb = &s->gb;
48
1.95k
    int start, count;
49
50
1.95k
    start = bytestream2_get_le16(gb);
51
1.95k
    count = bytestream2_get_le16(gb);
52
53
1.95k
    if (start + count > 256)
54
642
        return AVERROR_INVALIDDATA;
55
56
1.31k
    if (bytestream2_get_bytes_left(gb) < 3 * count)
57
515
        return AVERROR_INVALIDDATA;
58
59
3.78k
    for (int i = 0; i < count; i++)
60
2.98k
        pal[start + i] = (0xFFU << 24) | bytestream2_get_be24u(gb);
61
62
801
    return 0;
63
1.31k
}
64
65
static int decode_avcf(AVCodecContext *avctx, AVFrame *frame)
66
502
{
67
502
    ArgoContext *s = avctx->priv_data;
68
502
    GetByteContext *gb = &s->gb;
69
502
    const int l = frame->linesize[0];
70
502
    const uint8_t *map = gb->buffer;
71
502
    uint8_t *dst = frame->data[0];
72
73
502
    if (bytestream2_get_bytes_left(gb) < 1024 + (frame->width / 2) * (frame->height / 2))
74
294
        return AVERROR_INVALIDDATA;
75
76
208
    bytestream2_skipu(gb, 1024);
77
3.06k
    for (int y = 0; y < frame->height; y += 2) {
78
9.27k
        for (int x = 0; x < frame->width; x += 2) {
79
6.41k
            int index = bytestream2_get_byteu(gb);
80
6.41k
            const uint8_t *block = map + index * 4;
81
82
6.41k
            dst[x+0]   = block[0];
83
6.41k
            dst[x+1]   = block[1];
84
6.41k
            dst[x+l]   = block[2];
85
6.41k
            dst[x+l+1] = block[3];
86
6.41k
        }
87
88
2.86k
        dst += frame->linesize[0] * 2;
89
2.86k
    }
90
91
208
    return 0;
92
502
}
93
94
static int decode_alcd(AVCodecContext *avctx, AVFrame *frame)
95
707
{
96
707
    ArgoContext *s = avctx->priv_data;
97
707
    GetByteContext *gb = &s->gb;
98
707
    GetByteContext sb;
99
707
    const int l = frame->linesize[0];
100
707
    const uint8_t *map = gb->buffer;
101
707
    uint8_t *dst = frame->data[0];
102
707
    uint8_t codes = 0;
103
707
    int count = 0;
104
105
707
    if (bytestream2_get_bytes_left(gb) < 1024 + (((frame->width / 2) * (frame->height / 2) + 7) >> 3))
106
453
        return AVERROR_INVALIDDATA;
107
108
254
    bytestream2_skipu(gb, 1024);
109
254
    sb = *gb;
110
254
    bytestream2_skipu(gb, ((frame->width / 2) * (frame->height / 2) + 7) >> 3);
111
112
216k
    for (int y = 0; y < frame->height; y += 2) {
113
664k
        for (int x = 0; x < frame->width; x += 2) {
114
447k
            const uint8_t *block;
115
447k
            int index;
116
117
447k
            if (count == 0) {
118
56.0k
                codes = bytestream2_get_byteu(&sb);
119
56.0k
                count = 8;
120
56.0k
            }
121
122
447k
            if (codes & 0x80) {
123
154k
                index = bytestream2_get_byte(gb);
124
154k
                block = map + index * 4;
125
126
154k
                dst[x+0]   = block[0];
127
154k
                dst[x+1]   = block[1];
128
154k
                dst[x+l]   = block[2];
129
154k
                dst[x+l+1] = block[3];
130
154k
            }
131
132
447k
            codes <<= 1;
133
447k
            count--;
134
447k
        }
135
136
216k
        dst += frame->linesize[0] * 2;
137
216k
    }
138
139
254
    return 0;
140
707
}
141
142
static int decode_mad1(AVCodecContext *avctx, AVFrame *frame)
143
7.59k
{
144
7.59k
    ArgoContext *s = avctx->priv_data;
145
7.59k
    GetByteContext *gb = &s->gb;
146
7.59k
    const int w = frame->width;
147
7.59k
    const int h = frame->height;
148
7.59k
    const int l = frame->linesize[0];
149
150
11.1k
    while (bytestream2_get_bytes_left(gb) > 0) {
151
9.91k
        int size, type, pos, dy;
152
9.91k
        uint8_t *dst;
153
154
9.91k
        type = bytestream2_get_byte(gb);
155
9.91k
        if (type == 0xFF)
156
210
            break;
157
158
9.70k
        switch (type) {
159
727
        case 8:
160
727
            dst = frame->data[0];
161
112k
            for (int y = 0; y < h; y += 8) {
162
1.47M
                for (int x = 0; x < w; x += 8) {
163
1.36M
                    int fill = bytestream2_get_byte(gb);
164
1.36M
                    uint8_t *ddst = dst + x;
165
166
12.3M
                    for (int by = 0; by < 8; by++) {
167
10.9M
                        memset(ddst, fill, 8);
168
10.9M
                        ddst += l;
169
10.9M
                    }
170
1.36M
                }
171
172
111k
                dst += 8 * l;
173
111k
            }
174
727
            break;
175
2.87k
        case 7:
176
3.55k
            while (bytestream2_get_bytes_left(gb) > 0) {
177
3.30k
                int bsize = bytestream2_get_byte(gb);
178
3.30k
                uint8_t *src;
179
3.30k
                int count;
180
181
3.30k
                if (!bsize)
182
278
                    break;
183
184
3.02k
                count = bytestream2_get_be16(gb);
185
6.65k
                while (count > 0) {
186
5.97k
                    int mvx, mvy, a, b, c, mx, my;
187
5.97k
                    int bsize_w, bsize_h;
188
189
5.97k
                    bsize_w = bsize_h = bsize;
190
5.97k
                    if (bytestream2_get_bytes_left(gb) < 4)
191
396
                        return AVERROR_INVALIDDATA;
192
5.58k
                    mvx = bytestream2_get_byte(gb) * bsize;
193
5.58k
                    mvy = bytestream2_get_byte(gb) * bsize;
194
5.58k
                    a = bytestream2_get_byte(gb);
195
5.58k
                    b = bytestream2_get_byte(gb);
196
5.58k
                    c = ((a & 0x3F) << 8) + b;
197
5.58k
                    mx = mvx + (c  & 0x7F) - 64;
198
5.58k
                    my = mvy + (c >>    7) - 64;
199
200
5.58k
                    if (mvy < 0 || mvy >= h)
201
516
                        return AVERROR_INVALIDDATA;
202
203
5.06k
                    if (mvx < 0 || mvx >= w)
204
333
                        return AVERROR_INVALIDDATA;
205
206
4.73k
                    if (my < 0 || my >= h)
207
520
                        return AVERROR_INVALIDDATA;
208
209
4.21k
                    if (mx < 0 || mx >= w)
210
582
                        return AVERROR_INVALIDDATA;
211
212
3.63k
                    dst = frame->data[0] + mvx + l * mvy;
213
3.63k
                    src = frame->data[0] + mx  + l * my;
214
215
3.63k
                    bsize_w = FFMIN3(bsize_w, w - mvx, w - mx);
216
3.63k
                    bsize_h = FFMIN3(bsize_h, h - mvy, h - my);
217
218
3.63k
                    if (mvy >= my && (mvy != my || mvx >= mx)) {
219
1.91k
                        src += (bsize_h - 1) * l;
220
1.91k
                        dst += (bsize_h - 1) * l;
221
39.1k
                        for (int by = 0; by < bsize_h; by++) {
222
37.2k
                            memmove(dst, src, bsize_w);
223
37.2k
                            src -= l;
224
37.2k
                            dst -= l;
225
37.2k
                        }
226
1.91k
                    } else {
227
58.2k
                        for (int by = 0; by < bsize_h; by++) {
228
56.4k
                            memmove(dst, src, bsize_w);
229
56.4k
                            src += l;
230
56.4k
                            dst += l;
231
56.4k
                        }
232
1.71k
                    }
233
234
3.63k
                    count--;
235
3.63k
                }
236
3.02k
            }
237
529
            break;
238
1.11k
        case 6:
239
1.11k
            dst = frame->data[0];
240
1.11k
            if (bytestream2_get_bytes_left(gb) < w * h)
241
297
                return AVERROR_INVALIDDATA;
242
2.62k
            for (int y = 0; y < h; y++) {
243
1.80k
                bytestream2_get_bufferu(gb, dst, w);
244
1.80k
                dst += l;
245
1.80k
            }
246
821
            break;
247
292
        case 5:
248
292
            dst = frame->data[0];
249
382k
            for (int y = 0; y < h; y += 2) {
250
7.47M
                for (int x = 0; x < w; x += 2) {
251
7.09M
                    int fill = bytestream2_get_byte(gb);
252
7.09M
                    uint8_t *ddst = dst + x;
253
254
7.09M
                    fill = (fill << 8) | fill;
255
21.2M
                    for (int by = 0; by < 2; by++) {
256
14.1M
                            AV_WN16(ddst, fill);
257
258
14.1M
                        ddst += l;
259
14.1M
                    }
260
7.09M
                }
261
262
382k
                dst += 2 * l;
263
382k
            }
264
292
            break;
265
1.42k
        case 3:
266
1.42k
            size = bytestream2_get_le16(gb);
267
1.42k
            if (size > 0) {
268
882
                int x = bytestream2_get_byte(gb) * 4;
269
882
                int y = bytestream2_get_byte(gb) * 4;
270
882
                int count = bytestream2_get_byte(gb);
271
882
                int fill = bytestream2_get_byte(gb);
272
273
882
                av_log(avctx, AV_LOG_DEBUG, "%d %d %d %d\n", x, y, count, fill);
274
49.0k
                for (int i = 0; i < count; i++)
275
48.1k
                    ;
276
882
                return AVERROR_PATCHWELCOME;
277
882
            }
278
547
            break;
279
2.26k
        case 2:
280
2.26k
            dst = frame->data[0];
281
2.26k
            pos = 0;
282
2.26k
            dy  = 0;
283
181k
            while (bytestream2_get_bytes_left(gb) > 0) {
284
181k
                int count = bytestream2_get_byteu(gb);
285
181k
                int skip = count & 0x3F;
286
287
181k
                count = count >> 6;
288
181k
                if (skip == 0x3F) {
289
33.5k
                    pos += 0x3E;
290
752k
                    while (pos >= w) {
291
719k
                        pos -= w;
292
719k
                        dst += l;
293
719k
                        dy++;
294
719k
                        if (dy >= h)
295
232
                            return 0;
296
719k
                    }
297
147k
                } else {
298
147k
                    pos += skip;
299
660k
                    while (pos >= w) {
300
512k
                        pos -= w;
301
512k
                        dst += l;
302
512k
                        dy++;
303
512k
                        if (dy >= h)
304
283
                            return 0;
305
512k
                    }
306
339k
                    while (count >= 0) {
307
193k
                        int bits = bytestream2_get_byte(gb);
308
309
962k
                        for (int i = 0; i < 4; i++) {
310
770k
                            switch (bits & 3) {
311
484k
                            case 0:
312
484k
                                break;
313
168k
                            case 1:
314
168k
                                if (dy < 1 && !pos)
315
220
                                    return AVERROR_INVALIDDATA;
316
168k
                                else
317
168k
                                    dst[pos] = pos ? dst[pos - 1] : dst[-l + w - 1];
318
168k
                                break;
319
168k
                            case 2:
320
58.4k
                                if (dy < 1)
321
601
                                    return AVERROR_INVALIDDATA;
322
57.8k
                                dst[pos] = dst[pos - l];
323
57.8k
                                break;
324
59.6k
                            case 3:
325
59.6k
                                dst[pos] = bytestream2_get_byte(gb);
326
59.6k
                                break;
327
770k
                            }
328
329
769k
                            pos++;
330
769k
                            if (pos >= w) {
331
263k
                                pos -= w;
332
263k
                                dst += l;
333
263k
                                dy++;
334
263k
                                if (dy >= h)
335
245
                                    return 0;
336
263k
                            }
337
769k
                            bits >>= 2;
338
769k
                        }
339
192k
                        count--;
340
192k
                    }
341
147k
                }
342
181k
            }
343
682
            break;
344
997
        default:
345
997
            return AVERROR_INVALIDDATA;
346
9.70k
        }
347
9.70k
    }
348
349
1.49k
    return 0;
350
7.59k
}
351
352
static int decode_mad1_24(AVCodecContext *avctx, AVFrame *frame)
353
6.48k
{
354
6.48k
    ArgoContext *s = avctx->priv_data;
355
6.48k
    GetByteContext *gb = &s->gb;
356
6.48k
    const int w = frame->width;
357
6.48k
    const int h = frame->height;
358
6.48k
    const int l = frame->linesize[0] / 4;
359
360
10.2k
    while (bytestream2_get_bytes_left(gb) > 0) {
361
8.71k
        int osize, type, pos, dy, di, bcode, value, v14;
362
8.71k
        const uint8_t *bits;
363
8.71k
        uint32_t *dst;
364
365
8.71k
        type = bytestream2_get_byte(gb);
366
8.71k
        if (type == 0xFF)
367
264
            return 0;
368
369
8.44k
        switch (type) {
370
1.82k
        case 8:
371
1.82k
            dst = (uint32_t *)frame->data[0];
372
2.57M
            for (int y = 0; y + 12 <= h; y += 12) {
373
2.86M
                for (int x = 0; x + 12 <= w; x += 12) {
374
294k
                    int fill = bytestream2_get_be24(gb);
375
294k
                    uint32_t *dstp = dst + x;
376
377
3.83M
                    for (int by = 0; by < 12; by++) {
378
45.9M
                        for (int bx = 0; bx < 12; bx++)
379
42.4M
                            dstp[bx] = fill;
380
381
3.53M
                        dstp += l;
382
3.53M
                    }
383
294k
                }
384
385
2.57M
                dst += 12 * l;
386
2.57M
            }
387
1.82k
            break;
388
2.82k
        case 7:
389
3.64k
            while (bytestream2_get_bytes_left(gb) > 0) {
390
3.41k
                int bsize = bytestream2_get_byte(gb);
391
3.41k
                uint32_t *src;
392
3.41k
                int count;
393
394
3.41k
                if (!bsize)
395
331
                    break;
396
397
3.08k
                count = bytestream2_get_be16(gb);
398
16.0k
                while (count > 0) {
399
15.2k
                    int mvx, mvy, a, b, c, mx, my;
400
15.2k
                    int bsize_w, bsize_h;
401
402
15.2k
                    bsize_w = bsize_h = bsize;
403
15.2k
                    if (bytestream2_get_bytes_left(gb) < 4)
404
356
                        return AVERROR_INVALIDDATA;
405
14.8k
                    mvx = bytestream2_get_byte(gb) * bsize;
406
14.8k
                    mvy = bytestream2_get_byte(gb) * bsize;
407
14.8k
                    a = bytestream2_get_byte(gb);
408
14.8k
                    b = bytestream2_get_byte(gb);
409
14.8k
                    c = ((a & 0x3F) << 8) + b;
410
14.8k
                    mx = mvx + (c  & 0x7F) - 64;
411
14.8k
                    my = mvy + (c >>    7) - 64;
412
413
14.8k
                    if (mvy < 0 || mvy >= h)
414
498
                        return AVERROR_INVALIDDATA;
415
416
14.3k
                    if (mvx < 0 || mvx >= w)
417
337
                        return AVERROR_INVALIDDATA;
418
419
14.0k
                    if (my < 0 || my >= h)
420
545
                        return AVERROR_INVALIDDATA;
421
422
13.4k
                    if (mx < 0 || mx >= w)
423
522
                        return AVERROR_INVALIDDATA;
424
425
12.9k
                    dst = (uint32_t *)frame->data[0] + mvx + l * mvy;
426
12.9k
                    src = (uint32_t *)frame->data[0] + mx  + l * my;
427
428
12.9k
                    bsize_w = FFMIN3(bsize_w, w - mvx, w - mx);
429
12.9k
                    bsize_h = FFMIN3(bsize_h, h - mvy, h - my);
430
431
12.9k
                    if (mvy >= my && (mvy != my || mvx >= mx)) {
432
2.15k
                        src += (bsize_h - 1) * l;
433
2.15k
                        dst += (bsize_h - 1) * l;
434
43.8k
                        for (int by = 0; by < bsize_h; by++) {
435
41.6k
                            memmove(dst, src, bsize_w * 4);
436
41.6k
                            src -= l;
437
41.6k
                            dst -= l;
438
41.6k
                        }
439
10.8k
                    } else {
440
138k
                        for (int by = 0; by < bsize_h; by++) {
441
127k
                            memmove(dst, src, bsize_w * 4);
442
127k
                            src += l;
443
127k
                            dst += l;
444
127k
                        }
445
10.8k
                    }
446
447
12.9k
                    count--;
448
12.9k
                }
449
3.08k
            }
450
567
            break;
451
3.09k
        case 12:
452
3.09k
            osize = ((h + 3) / 4) * ((w + 3) / 4) + 7;
453
3.09k
            bits = gb->buffer;
454
3.09k
            di   = 0;
455
3.09k
            bcode = v14 = 0;
456
3.09k
            if (bytestream2_get_bytes_left(gb) < osize >> 3)
457
645
                return AVERROR_INVALIDDATA;
458
2.45k
            bytestream2_skip(gb, osize >> 3);
459
1.54M
            for (int x = 0; x < w; x += 4) {
460
6.30M
                for (int y = 0; y < h; y += 4) {
461
4.76M
                    int astate = 0;
462
463
4.76M
                    if (bits[di >> 3] & (1 << (di & 7))) {
464
1.24M
                        int codes = bytestream2_get_byte(gb);
465
466
6.21M
                        for (int count = 0; count < 4; count++) {
467
4.97M
                            uint32_t *src = (uint32_t *)frame->data[0];
468
4.97M
                            size_t src_size = l * (h - 1) + (w - 1);
469
4.97M
                            int nv, v, code = codes & 3;
470
471
4.97M
                            pos = x;
472
4.97M
                            dy  = y + count;
473
4.97M
                            dst = (uint32_t *)frame->data[0] + pos + dy * l;
474
4.97M
                            if (code & 1)
475
73.2k
                                bcode = bytestream2_get_byte(gb);
476
4.97M
                            if (code == 3) {
477
237k
                                for (int j = 0; j < 4; j++) {
478
190k
                                    switch (bcode & 3) {
479
66.6k
                                    case 0:
480
66.6k
                                        break;
481
16.1k
                                    case 1:
482
16.1k
                                        if (dy < 1 && !pos)
483
202
                                            return AVERROR_INVALIDDATA;
484
15.9k
                                        dst[0] = dst[-1];
485
15.9k
                                        break;
486
12.2k
                                    case 2:
487
12.2k
                                        if (dy < 1)
488
246
                                            return AVERROR_INVALIDDATA;
489
12.0k
                                        dst[0] = dst[-l];
490
12.0k
                                        break;
491
95.0k
                                    case 3:
492
95.0k
                                        if (astate) {
493
44.3k
                                            nv = value >> 4;
494
50.6k
                                        } else {
495
50.6k
                                            value = bytestream2_get_byte(gb);
496
50.6k
                                            nv = value & 0xF;
497
50.6k
                                        }
498
95.0k
                                        astate ^= 1;
499
95.0k
                                        dst[0] = src[av_clip(l * (dy + s->mv1[nv][1]) + pos +
500
95.0k
                                                             s->mv1[nv][0], 0, src_size)];
501
95.0k
                                        break;
502
190k
                                    }
503
504
189k
                                    bcode >>= 2;
505
189k
                                    dst++;
506
189k
                                    pos++;
507
189k
                                }
508
4.92M
                            } else if (code) {
509
127k
                                if (code == 1)
510
25.3k
                                    v14 = bcode;
511
101k
                                else
512
101k
                                    bcode = v14;
513
633k
                                for (int j = 0; j < 4; j++) {
514
506k
                                    switch (bcode & 3) {
515
349k
                                    case 0:
516
349k
                                        break;
517
38.8k
                                    case 1:
518
38.8k
                                        if (dy < 1 && !pos)
519
337
                                            return AVERROR_INVALIDDATA;
520
38.4k
                                        dst[0] = dst[-1];
521
38.4k
                                        break;
522
92.8k
                                    case 2:
523
92.8k
                                        if (dy < 1)
524
272
                                            return AVERROR_INVALIDDATA;
525
92.5k
                                        dst[0] = dst[-l];
526
92.5k
                                        break;
527
25.2k
                                    case 3:
528
25.2k
                                        v = bytestream2_get_byte(gb);
529
25.2k
                                        if (v < 128) {
530
17.8k
                                            dst[0] = src[av_clip(l * (dy + s->mv0[v][1]) + pos +
531
17.8k
                                                                 s->mv0[v][0], 0, src_size)];
532
17.8k
                                        } else {
533
7.39k
                                            dst[0] = ((v & 0x7F) << 17) | bytestream2_get_be16(gb);
534
7.39k
                                        }
535
25.2k
                                        break;
536
506k
                                    }
537
538
505k
                                    bcode >>= 2;
539
505k
                                    dst++;
540
505k
                                    pos++;
541
505k
                                }
542
127k
                            }
543
544
4.97M
                            codes >>= 2;
545
4.97M
                        }
546
1.24M
                    }
547
548
4.76M
                    di++;
549
4.76M
                }
550
1.54M
            }
551
1.39k
            break;
552
1.39k
        default:
553
700
            return AVERROR_INVALIDDATA;
554
8.44k
        }
555
8.44k
    }
556
557
1.56k
    return AVERROR_INVALIDDATA;
558
6.48k
}
559
560
static int decode_rle(AVCodecContext *avctx, AVFrame *frame)
561
1.96k
{
562
1.96k
    ArgoContext *s = avctx->priv_data;
563
1.96k
    GetByteContext *gb = &s->gb;
564
1.96k
    const int w = frame->width;
565
1.96k
    const int h = frame->height;
566
1.96k
    const int l = frame->linesize[0];
567
1.96k
    uint8_t *dst = frame->data[0];
568
1.96k
    int pos = 0, y = 0;
569
570
210k
    while (bytestream2_get_bytes_left(gb) > 0) {
571
209k
        int count = bytestream2_get_byte(gb);
572
209k
        int pixel = bytestream2_get_byte(gb);
573
574
209k
        if (!count) {
575
96.1k
            pos += pixel;
576
107k
            while (pos >= w) {
577
11.3k
                pos -= w;
578
11.3k
                y++;
579
11.3k
                if (y >= h)
580
440
                    return 0;
581
11.3k
            }
582
113k
        } else {
583
19.3M
            while (count > 0) {
584
19.2M
                dst[pos + y * l] = pixel;
585
19.2M
                count--;
586
19.2M
                pos++;
587
19.2M
                if (pos >= w) {
588
169k
                    pos = 0;
589
169k
                    y++;
590
169k
                    if (y >= h)
591
728
                        return 0;
592
169k
                }
593
19.2M
            }
594
113k
        }
595
209k
    }
596
597
792
    return 0;
598
1.96k
}
599
600
static int decode_frame(AVCodecContext *avctx, AVFrame *rframe,
601
                        int *got_frame, AVPacket *avpkt)
602
137k
{
603
137k
    ArgoContext *s = avctx->priv_data;
604
137k
    GetByteContext *gb = &s->gb;
605
137k
    AVFrame *frame = s->frame;
606
137k
    uint32_t chunk;
607
137k
    int ret;
608
609
137k
    if (avpkt->size < 4)
610
6.81k
        return AVERROR_INVALIDDATA;
611
612
130k
    bytestream2_init(gb, avpkt->data, avpkt->size);
613
614
130k
    if ((ret = ff_reget_buffer(avctx, frame, 0)) < 0)
615
1.54k
        return ret;
616
617
129k
    chunk = bytestream2_get_be32(gb);
618
129k
    switch (chunk) {
619
1.95k
    case MKBETAG('P', 'A', 'L', '8'):
620
12.5M
        for (int y = 0; y < frame->height; y++)
621
12.5M
            memset(frame->data[0] + y * frame->linesize[0], 0, frame->width * s->bpp);
622
1.95k
        if (avctx->pix_fmt == AV_PIX_FMT_PAL8)
623
846
            memset(frame->data[1], 0, AVPALETTE_SIZE);
624
1.95k
        return decode_pal8(avctx, s->pal);
625
14.0k
    case MKBETAG('M', 'A', 'D', '1'):
626
14.0k
        if (avctx->pix_fmt == AV_PIX_FMT_PAL8)
627
7.59k
            ret = decode_mad1(avctx, frame);
628
6.48k
        else
629
6.48k
            ret = decode_mad1_24(avctx, frame);
630
14.0k
        break;
631
746
    case MKBETAG('A', 'V', 'C', 'F'):
632
746
        if (avctx->pix_fmt == AV_PIX_FMT_PAL8) {
633
502
            s->key = 1;
634
502
            ret = decode_avcf(avctx, frame);
635
502
            break;
636
502
        }
637
1.12k
    case MKBETAG('A', 'L', 'C', 'D'):
638
1.12k
        if (avctx->pix_fmt == AV_PIX_FMT_PAL8) {
639
707
            s->key = 0;
640
707
            ret = decode_alcd(avctx, frame);
641
707
            break;
642
707
        }
643
1.54k
    case MKBETAG('R', 'L', 'E', 'F'):
644
1.54k
        if (avctx->pix_fmt == AV_PIX_FMT_PAL8) {
645
1.08k
            s->key = 1;
646
1.08k
            ret = decode_rle(avctx, frame);
647
1.08k
            break;
648
1.08k
        }
649
1.36k
    case MKBETAG('R', 'L', 'E', 'D'):
650
1.36k
        if (avctx->pix_fmt == AV_PIX_FMT_PAL8) {
651
872
            s->key = 0;
652
872
            ret = decode_rle(avctx, frame);
653
872
            break;
654
872
        }
655
109k
    default:
656
109k
        av_log(avctx, AV_LOG_DEBUG, "unknown chunk 0x%X\n", chunk);
657
109k
        break;
658
129k
    }
659
660
127k
    if (ret < 0)
661
12.3k
        return ret;
662
663
114k
    if (avctx->pix_fmt == AV_PIX_FMT_PAL8)
664
101k
        memcpy(frame->data[1], s->pal, AVPALETTE_SIZE);
665
666
114k
    if ((ret = av_frame_ref(rframe, s->frame)) < 0)
667
0
        return ret;
668
669
114k
    frame->pict_type = s->key ? AV_PICTURE_TYPE_I : AV_PICTURE_TYPE_P;
670
114k
    if (s->key)
671
1.83k
        frame->flags |= AV_FRAME_FLAG_KEY;
672
112k
    else
673
112k
        frame->flags &= ~AV_FRAME_FLAG_KEY;
674
114k
    *got_frame = 1;
675
676
114k
    return avpkt->size;
677
114k
}
678
679
static av_cold int decode_init(AVCodecContext *avctx)
680
1.39k
{
681
1.39k
    ArgoContext *s = avctx->priv_data;
682
683
1.39k
    switch (avctx->bits_per_coded_sample) {
684
719
    case  8: s->bpp = 1;
685
719
             avctx->pix_fmt = AV_PIX_FMT_PAL8; break;
686
552
    case 24: s->bpp = 4;
687
552
             avctx->pix_fmt = AV_PIX_FMT_BGR0; break;
688
125
    default: avpriv_request_sample(s, "depth == %u", avctx->bits_per_coded_sample);
689
125
             return AVERROR_PATCHWELCOME;
690
1.39k
    }
691
692
1.27k
    if (avctx->width % 2 || avctx->height % 2) {
693
5
        avpriv_request_sample(s, "Odd dimensions\n");
694
5
        return AVERROR_PATCHWELCOME;
695
5
    }
696
697
1.26k
    s->frame = av_frame_alloc();
698
1.26k
    if (!s->frame)
699
0
        return AVERROR(ENOMEM);
700
701
11.3k
    for (int n = 0, i = -4; i < 4; i++) {
702
172k
        for (int j = -14; j < 2; j++) {
703
162k
            s->mv0[n][0] = j;
704
162k
            s->mv0[n++][1] = i;
705
162k
        }
706
10.1k
    }
707
708
6.33k
    for (int n = 0, i = -5; i <= 1; i += 2) {
709
5.06k
        int j = -5;
710
711
25.3k
        while (j <= 1) {
712
20.2k
            s->mv1[n][0] = j;
713
20.2k
            s->mv1[n++][1] = i;
714
20.2k
            j += 2;
715
20.2k
        }
716
5.06k
    }
717
718
1.26k
    return 0;
719
1.26k
}
720
721
static av_cold void decode_flush(AVCodecContext *avctx)
722
30.7k
{
723
30.7k
    ArgoContext *s = avctx->priv_data;
724
725
30.7k
    av_frame_unref(s->frame);
726
30.7k
}
727
728
static av_cold int decode_close(AVCodecContext *avctx)
729
1.39k
{
730
1.39k
    ArgoContext *s = avctx->priv_data;
731
732
1.39k
    av_frame_free(&s->frame);
733
734
1.39k
    return 0;
735
1.39k
}
736
737
const FFCodec ff_argo_decoder = {
738
    .p.name         = "argo",
739
    CODEC_LONG_NAME("Argonaut Games Video"),
740
    .p.type         = AVMEDIA_TYPE_VIDEO,
741
    .p.id           = AV_CODEC_ID_ARGO,
742
    .priv_data_size = sizeof(ArgoContext),
743
    .init           = decode_init,
744
    FF_CODEC_DECODE_CB(decode_frame),
745
    .flush          = decode_flush,
746
    .close          = decode_close,
747
    .p.capabilities = AV_CODEC_CAP_DR1,
748
    .caps_internal  = FF_CODEC_CAP_INIT_CLEANUP,
749
};