Coverage Report

Created: 2026-02-14 06:59

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/ffmpeg/libavcodec/iff.c
Line
Count
Source
1
/*
2
 * IFF ACBM/ANIM/DEEP/ILBM/PBM/RGB8/RGBN bitmap decoder
3
 * Copyright (c) 2010 Peter Ross <pross@xvid.org>
4
 * Copyright (c) 2010 Sebastian Vater <cdgs.basty@googlemail.com>
5
 * Copyright (c) 2016 Paul B Mahol
6
 *
7
 * This file is part of FFmpeg.
8
 *
9
 * FFmpeg is free software; you can redistribute it and/or
10
 * modify it under the terms of the GNU Lesser General Public
11
 * License as published by the Free Software Foundation; either
12
 * version 2.1 of the License, or (at your option) any later version.
13
 *
14
 * FFmpeg is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17
 * Lesser General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU Lesser General Public
20
 * License along with FFmpeg; if not, write to the Free Software
21
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22
 */
23
24
/**
25
 * @file
26
 * IFF ACBM/ANIM/DEEP/ILBM/PBM/RGB8/RGBN bitmap decoder
27
 */
28
29
#include <stdint.h>
30
31
#include "libavutil/imgutils.h"
32
#include "libavutil/mem.h"
33
34
#include "bytestream.h"
35
#include "avcodec.h"
36
#include "codec_internal.h"
37
#include "decode.h"
38
39
// TODO: masking bits
40
typedef enum {
41
    MASK_NONE,
42
    MASK_HAS_MASK,
43
    MASK_HAS_TRANSPARENT_COLOR,
44
    MASK_LASSO
45
} mask_type;
46
47
typedef struct IffContext {
48
    int planesize;
49
    uint8_t * planebuf;
50
    uint8_t * ham_buf;      ///< temporary buffer for planar to chunky conversation
51
    uint32_t *ham_palbuf;   ///< HAM decode table
52
    uint32_t *mask_buf;     ///< temporary buffer for palette indices
53
    uint32_t *mask_palbuf;  ///< masking palette table
54
    unsigned  compression;  ///< delta compression method used
55
    unsigned  is_short;     ///< short compression method used
56
    unsigned  is_interlaced;///< video is interlaced
57
    unsigned  is_brush;     ///< video is in ANBR format
58
    unsigned  bpp;          ///< bits per plane to decode (differs from bits_per_coded_sample if HAM)
59
    unsigned  ham;          ///< 0 if non-HAM or number of hold bits (6 for bpp > 6, 4 otherwise)
60
    unsigned  flags;        ///< 1 for EHB, 0 is no extra half darkening
61
    unsigned  transparency; ///< TODO: transparency color index in palette
62
    unsigned  masking;      ///< TODO: masking method used
63
    int init; // 1 if buffer and palette data already initialized, 0 otherwise
64
    int16_t   tvdc[16];     ///< TVDC lookup table
65
    uint8_t *video[2];
66
    unsigned video_size;
67
    uint32_t *pal;
68
} IffContext;
69
70
#define LUT8_PART(plane, v)                             \
71
    AV_LE2NE64C(UINT64_C(0x0000000)<<32 | v) << plane,  \
72
    AV_LE2NE64C(UINT64_C(0x1000000)<<32 | v) << plane,  \
73
    AV_LE2NE64C(UINT64_C(0x0010000)<<32 | v) << plane,  \
74
    AV_LE2NE64C(UINT64_C(0x1010000)<<32 | v) << plane,  \
75
    AV_LE2NE64C(UINT64_C(0x0000100)<<32 | v) << plane,  \
76
    AV_LE2NE64C(UINT64_C(0x1000100)<<32 | v) << plane,  \
77
    AV_LE2NE64C(UINT64_C(0x0010100)<<32 | v) << plane,  \
78
    AV_LE2NE64C(UINT64_C(0x1010100)<<32 | v) << plane,  \
79
    AV_LE2NE64C(UINT64_C(0x0000001)<<32 | v) << plane,  \
80
    AV_LE2NE64C(UINT64_C(0x1000001)<<32 | v) << plane,  \
81
    AV_LE2NE64C(UINT64_C(0x0010001)<<32 | v) << plane,  \
82
    AV_LE2NE64C(UINT64_C(0x1010001)<<32 | v) << plane,  \
83
    AV_LE2NE64C(UINT64_C(0x0000101)<<32 | v) << plane,  \
84
    AV_LE2NE64C(UINT64_C(0x1000101)<<32 | v) << plane,  \
85
    AV_LE2NE64C(UINT64_C(0x0010101)<<32 | v) << plane,  \
86
    AV_LE2NE64C(UINT64_C(0x1010101)<<32 | v) << plane
87
88
#define LUT8(plane) {                           \
89
    LUT8_PART(plane, 0x0000000),                \
90
    LUT8_PART(plane, 0x1000000),                \
91
    LUT8_PART(plane, 0x0010000),                \
92
    LUT8_PART(plane, 0x1010000),                \
93
    LUT8_PART(plane, 0x0000100),                \
94
    LUT8_PART(plane, 0x1000100),                \
95
    LUT8_PART(plane, 0x0010100),                \
96
    LUT8_PART(plane, 0x1010100),                \
97
    LUT8_PART(plane, 0x0000001),                \
98
    LUT8_PART(plane, 0x1000001),                \
99
    LUT8_PART(plane, 0x0010001),                \
100
    LUT8_PART(plane, 0x1010001),                \
101
    LUT8_PART(plane, 0x0000101),                \
102
    LUT8_PART(plane, 0x1000101),                \
103
    LUT8_PART(plane, 0x0010101),                \
104
    LUT8_PART(plane, 0x1010101),                \
105
}
106
107
// 8 planes * 8-bit mask
108
static const uint64_t plane8_lut[8][256] = {
109
    LUT8(0), LUT8(1), LUT8(2), LUT8(3),
110
    LUT8(4), LUT8(5), LUT8(6), LUT8(7),
111
};
112
113
#define LUT32(plane) {                                    \
114
              0,           0,           0,           0,   \
115
              0,           0,           0, 1U << plane,   \
116
              0,           0, 1U << plane,           0,   \
117
              0,           0, 1U << plane, 1U << plane,   \
118
              0, 1U << plane,           0,           0,   \
119
              0, 1U << plane,           0, 1U << plane,   \
120
              0, 1U << plane, 1U << plane,           0,   \
121
              0, 1U << plane, 1U << plane, 1U << plane,   \
122
    1U << plane,           0,           0,           0,   \
123
    1U << plane,           0,           0, 1U << plane,   \
124
    1U << plane,           0, 1U << plane,           0,   \
125
    1U << plane,           0, 1U << plane, 1U << plane,   \
126
    1U << plane, 1U << plane,           0,           0,   \
127
    1U << plane, 1U << plane,           0, 1U << plane,   \
128
    1U << plane, 1U << plane, 1U << plane,           0,   \
129
    1U << plane, 1U << plane, 1U << plane, 1U << plane,   \
130
}
131
132
// 32 planes * 4-bit mask * 4 lookup tables each
133
static const uint32_t plane32_lut[32][16*4] = {
134
    LUT32( 0), LUT32( 1), LUT32( 2), LUT32( 3),
135
    LUT32( 4), LUT32( 5), LUT32( 6), LUT32( 7),
136
    LUT32( 8), LUT32( 9), LUT32(10), LUT32(11),
137
    LUT32(12), LUT32(13), LUT32(14), LUT32(15),
138
    LUT32(16), LUT32(17), LUT32(18), LUT32(19),
139
    LUT32(20), LUT32(21), LUT32(22), LUT32(23),
140
    LUT32(24), LUT32(25), LUT32(26), LUT32(27),
141
    LUT32(28), LUT32(29), LUT32(30), LUT32(31),
142
};
143
144
// Gray to RGB, required for palette table of grayscale images with bpp < 8
145
76.0k
static av_always_inline uint32_t gray2rgb(const uint32_t x) {
146
76.0k
    return x << 16 | x << 8 | x;
147
76.0k
}
148
149
/**
150
 * Convert CMAP buffer (stored in extradata) to lavc palette format
151
 */
152
static int cmap_read_palette(AVCodecContext *avctx, uint32_t *pal)
153
3.00k
{
154
3.00k
    IffContext *s = avctx->priv_data;
155
3.00k
    unsigned count, i;
156
3.00k
    const uint8_t *const palette = avctx->extradata + AV_RB16(avctx->extradata);
157
    /* extract_header() already checked that the RHS is >= 0. */
158
3.00k
    unsigned palette_size = avctx->extradata_size - AV_RB16(avctx->extradata);
159
160
3.00k
    if (avctx->bits_per_coded_sample > 8) {
161
0
        av_log(avctx, AV_LOG_ERROR, "bits_per_coded_sample > 8 not supported\n");
162
0
        return AVERROR_INVALIDDATA;
163
0
    }
164
165
3.00k
    count = 1 << avctx->bits_per_coded_sample;
166
    // If extradata is smaller than actually needed, fill the remaining with black.
167
3.00k
    count = FFMIN(palette_size / 3, count);
168
3.00k
    if (count) {
169
52.6k
        for (i = 0; i < count; i++)
170
50.3k
            pal[i] = 0xFF000000 | AV_RB24(palette + i*3);
171
2.31k
        if (s->flags && count >= 32) { // EHB
172
40.8k
            for (i = 0; i < 32; i++)
173
39.6k
                pal[i + 32] = 0xFF000000 | (AV_RB24(palette + i*3) & 0xFEFEFE) >> 1;
174
1.23k
            count = FFMAX(count, 64);
175
1.23k
        }
176
2.31k
    } else { // Create gray-scale color palette for bps < 8
177
681
        count = 1 << avctx->bits_per_coded_sample;
178
179
76.4k
        for (i = 0; i < count; i++)
180
75.7k
            pal[i] = 0xFF000000 | gray2rgb((i * 255) >> avctx->bits_per_coded_sample);
181
681
    }
182
3.00k
    if (s->masking == MASK_HAS_MASK) {
183
1.78k
        if ((1 << avctx->bits_per_coded_sample) < count) {
184
1.23k
            avpriv_request_sample(avctx, "overlapping mask");
185
1.23k
            return AVERROR_PATCHWELCOME;
186
1.23k
        }
187
554
        memcpy(pal + (1 << avctx->bits_per_coded_sample), pal, count * 4);
188
4.34k
        for (i = 0; i < count; i++)
189
3.79k
            pal[i] &= 0xFFFFFF;
190
1.21k
    } else if (s->masking == MASK_HAS_TRANSPARENT_COLOR &&
191
11
        s->transparency < 1 << avctx->bits_per_coded_sample)
192
6
        pal[s->transparency] &= 0xFFFFFF;
193
1.77k
    return 0;
194
3.00k
}
195
196
/**
197
 * Extracts the IFF extra context and updates internal
198
 * decoder structures.
199
 *
200
 * @param avctx the AVCodecContext where to extract extra context to
201
 * @return >= 0 in case of success, a negative error code otherwise
202
 */
203
static int extract_header(AVCodecContext *const avctx,
204
                          const uint8_t *const extradata, int extradata_size)
205
3.97k
{
206
3.97k
    IffContext *s = avctx->priv_data;
207
3.97k
    const uint8_t *buf = extradata;
208
3.97k
    unsigned buf_size = 0;
209
3.97k
    int palette_size;
210
211
3.97k
    if (extradata_size < 2) {
212
58
        av_log(avctx, AV_LOG_ERROR, "not enough extradata\n");
213
58
        return AVERROR_INVALIDDATA;
214
58
    }
215
3.92k
    palette_size = extradata_size - AV_RB16(extradata);
216
217
3.92k
    buf_size = bytestream_get_be16(&buf);
218
3.92k
    if (buf_size <= 1 || palette_size < 0) {
219
27
        av_log(avctx, AV_LOG_ERROR,
220
27
                "Invalid palette size received: %u -> palette data offset: %d\n",
221
27
                buf_size, palette_size);
222
27
        return AVERROR_INVALIDDATA;
223
27
    }
224
225
3.89k
    if (buf_size < 41)
226
2.39k
        return 0;
227
228
1.49k
    s->compression  = bytestream_get_byte(&buf);
229
1.49k
    s->bpp          = bytestream_get_byte(&buf);
230
1.49k
    s->ham          = bytestream_get_byte(&buf);
231
1.49k
    s->flags        = bytestream_get_byte(&buf);
232
1.49k
    s->transparency = bytestream_get_be16(&buf);
233
1.49k
    s->masking      = bytestream_get_byte(&buf);
234
25.4k
    for (int i = 0; i < 16; i++)
235
23.9k
        s->tvdc[i] = bytestream_get_be16(&buf);
236
237
1.49k
    if (s->ham) {
238
819
        if (s->bpp > 8) {
239
5
            av_log(avctx, AV_LOG_ERROR, "Invalid number of hold bits for HAM: %u\n", s->ham);
240
5
            return AVERROR_INVALIDDATA;
241
814
        } else if (s->ham != (s->bpp > 6 ? 6 : 4)) {
242
2
            av_log(avctx, AV_LOG_ERROR, "Invalid number of hold bits for HAM: %u, BPP: %u\n", s->ham, s->bpp);
243
2
            return AVERROR_INVALIDDATA;
244
2
        }
245
819
    }
246
247
1.49k
    if (s->masking == MASK_HAS_MASK) {
248
978
        if (s->bpp >= 8 && !s->ham) {
249
260
            avctx->pix_fmt = AV_PIX_FMT_RGB32;
250
260
            if (s->bpp > 16) {
251
1
                av_log(avctx, AV_LOG_ERROR, "bpp %d too large for palette\n", s->bpp);
252
1
                return AVERROR(ENOMEM);
253
1
            }
254
259
            s->mask_buf = av_malloc((s->planesize * 32) + AV_INPUT_BUFFER_PADDING_SIZE);
255
259
            if (!s->mask_buf)
256
0
                return AVERROR(ENOMEM);
257
259
            s->mask_palbuf = av_malloc((2 << s->bpp) * sizeof(uint32_t) + AV_INPUT_BUFFER_PADDING_SIZE);
258
259
            if (!s->mask_palbuf)
259
0
                return AVERROR(ENOMEM);
260
259
        }
261
977
        s->bpp++;
262
977
    } else if (s->masking != MASK_NONE && s->masking != MASK_HAS_TRANSPARENT_COLOR) {
263
9
        av_log(avctx, AV_LOG_ERROR, "Masking not supported\n");
264
9
        return AVERROR_PATCHWELCOME;
265
9
    }
266
1.48k
    if (!s->bpp || s->bpp > 32) {
267
4
        av_log(avctx, AV_LOG_ERROR, "Invalid number of bitplanes: %u\n", s->bpp);
268
4
        return AVERROR_INVALIDDATA;
269
4
    }
270
1.47k
    if (s->video_size && s->planesize * s->bpp * avctx->height > s->video_size)
271
1
        return AVERROR_INVALIDDATA;
272
273
1.47k
    if (s->ham) {
274
809
        int count = FFMIN(palette_size / 3, 1 << s->ham);
275
809
        int ham_count;
276
809
        const uint8_t *const palette = extradata + AV_RB16(extradata);
277
809
        int extra_space = 1;
278
279
809
        if (avctx->codec_tag == MKTAG('P', 'B', 'M', ' ') && s->ham == 4)
280
144
            extra_space = 4;
281
282
809
        s->ham_buf = av_mallocz((s->planesize * 8) + AV_INPUT_BUFFER_PADDING_SIZE);
283
809
        if (!s->ham_buf)
284
0
            return AVERROR(ENOMEM);
285
286
809
        ham_count = 8 * (1 << s->ham);
287
809
        s->ham_palbuf = av_malloc(extra_space * (ham_count << !!(s->masking == MASK_HAS_MASK)) * sizeof (uint32_t) + AV_INPUT_BUFFER_PADDING_SIZE);
288
809
        if (!s->ham_palbuf)
289
0
            return AVERROR(ENOMEM);
290
291
809
        if (count) { // HAM with color palette attached
292
            // prefill with black and palette and set HAM take direct value mask to zero
293
794
            memset(s->ham_palbuf, 0, (1 << s->ham) * 2 * sizeof (uint32_t));
294
8.83k
            for (int i = 0; i < count; i++) {
295
8.04k
                s->ham_palbuf[i*2+1] = 0xFF000000 | AV_RL24(palette + i*3);
296
8.04k
            }
297
794
            count = 1 << s->ham;
298
794
        } else { // HAM with grayscale color palette
299
15
            count = 1 << s->ham;
300
303
            for (int i = 0; i < count; i++) {
301
288
                s->ham_palbuf[i*2]   = 0xFF000000; // take direct color value from palette
302
288
                s->ham_palbuf[i*2+1] = 0xFF000000 | av_le2ne32(gray2rgb((i * 255) >> s->ham));
303
288
            }
304
15
        }
305
24.0k
        for (int i = 0; i < count; i++) {
306
23.2k
            uint32_t tmp = i << (8 - s->ham);
307
23.2k
            tmp |= tmp >> s->ham;
308
23.2k
            s->ham_palbuf[(i+count)*2]     = 0xFF00FFFF; // just modify blue color component
309
23.2k
            s->ham_palbuf[(i+count*2)*2]   = 0xFFFFFF00; // just modify red color component
310
23.2k
            s->ham_palbuf[(i+count*3)*2]   = 0xFFFF00FF; // just modify green color component
311
23.2k
            s->ham_palbuf[(i+count)*2+1]   = 0xFF000000 | tmp << 16;
312
23.2k
            s->ham_palbuf[(i+count*2)*2+1] = 0xFF000000 | tmp;
313
23.2k
            s->ham_palbuf[(i+count*3)*2+1] = 0xFF000000 | tmp << 8;
314
23.2k
        }
315
809
        if (s->masking == MASK_HAS_MASK) {
316
144k
            for (int i = 0; i < ham_count; i++)
317
144k
                s->ham_palbuf[(1 << s->bpp) + i] = s->ham_palbuf[i] | 0xFF000000;
318
588
        }
319
809
    }
320
321
1.47k
    return 0;
322
1.47k
}
323
324
static av_cold int decode_end(AVCodecContext *avctx)
325
4.22k
{
326
4.22k
    IffContext *s = avctx->priv_data;
327
4.22k
    av_freep(&s->planebuf);
328
4.22k
    av_freep(&s->ham_buf);
329
4.22k
    av_freep(&s->ham_palbuf);
330
4.22k
    av_freep(&s->mask_buf);
331
4.22k
    av_freep(&s->mask_palbuf);
332
4.22k
    av_freep(&s->video[0]);
333
4.22k
    av_freep(&s->video[1]);
334
4.22k
    av_freep(&s->pal);
335
4.22k
    return 0;
336
4.22k
}
337
338
static av_cold int decode_init(AVCodecContext *avctx)
339
4.22k
{
340
4.22k
    IffContext *s = avctx->priv_data;
341
4.22k
    int err;
342
343
4.22k
    if (avctx->bits_per_coded_sample <= 8) {
344
2.19k
        int palette_size;
345
346
2.19k
        if (avctx->extradata_size >= 2)
347
2.04k
            palette_size = avctx->extradata_size - AV_RB16(avctx->extradata);
348
149
        else
349
149
            palette_size = 0;
350
2.19k
        avctx->pix_fmt = (avctx->bits_per_coded_sample < 8) ||
351
2.05k
                         (avctx->extradata_size >= 2 && palette_size) ? AV_PIX_FMT_PAL8 : AV_PIX_FMT_GRAY8;
352
2.19k
    } else if (avctx->bits_per_coded_sample <= 32) {
353
1.96k
        if (avctx->codec_tag == MKTAG('R', 'G', 'B', '8')) {
354
49
            avctx->pix_fmt = AV_PIX_FMT_RGB32;
355
1.91k
        } else if (avctx->codec_tag == MKTAG('R', 'G', 'B', 'N')) {
356
32
            avctx->pix_fmt = AV_PIX_FMT_RGB444;
357
1.88k
        } else if (avctx->codec_tag != MKTAG('D', 'E', 'E', 'P')) {
358
1.87k
            if (avctx->bits_per_coded_sample == 24) {
359
1.28k
                avctx->pix_fmt = AV_PIX_FMT_0BGR32;
360
1.28k
            } else if (avctx->bits_per_coded_sample == 32) {
361
565
                avctx->pix_fmt = AV_PIX_FMT_BGR32;
362
565
            } else {
363
22
                avpriv_request_sample(avctx, "unknown bits_per_coded_sample");
364
22
                return AVERROR_PATCHWELCOME;
365
22
            }
366
1.87k
        }
367
1.96k
    } else {
368
64
        return AVERROR_INVALIDDATA;
369
64
    }
370
371
4.14k
    if ((err = av_image_check_size(avctx->width, avctx->height, 0, avctx)))
372
109
        return err;
373
4.03k
    s->planesize = FFALIGN(avctx->width, 16) >> 3; // Align plane size in bits to word-boundary
374
4.03k
    s->planebuf  = av_malloc(s->planesize * avctx->height + AV_INPUT_BUFFER_PADDING_SIZE);
375
4.03k
    if (!s->planebuf)
376
0
        return AVERROR(ENOMEM);
377
378
4.03k
    s->bpp = avctx->bits_per_coded_sample;
379
380
4.03k
    if (avctx->codec_tag == MKTAG('A', 'N', 'I', 'M')) {
381
2.36k
        s->video_size = FFALIGN(avctx->width, 2) * avctx->height * s->bpp;
382
2.36k
        if (!s->video_size)
383
1
            return AVERROR_INVALIDDATA;
384
2.36k
        s->video[0] = av_calloc(FFALIGN(avctx->width, 2) * avctx->height, s->bpp);
385
2.36k
        s->video[1] = av_calloc(FFALIGN(avctx->width, 2) * avctx->height, s->bpp);
386
2.36k
        s->pal = av_calloc(256, sizeof(*s->pal));
387
2.36k
        if (!s->video[0] || !s->video[1] || !s->pal)
388
52
            return AVERROR(ENOMEM);
389
2.36k
    }
390
391
3.97k
    err = extract_header(avctx, avctx->extradata, avctx->extradata_size);
392
3.97k
    if (err < 0)
393
107
        return err;
394
395
3.87k
    return 0;
396
3.97k
}
397
398
/**
399
 * Decode interleaved plane buffer up to 8bpp
400
 * @param dst Destination buffer
401
 * @param buf Source buffer
402
 * @param buf_size
403
 * @param plane plane number to decode as
404
 */
405
static void decodeplane8(uint8_t *dst, const uint8_t *buf, int buf_size, int plane)
406
86.9M
{
407
86.9M
    const uint64_t *lut;
408
86.9M
    if (plane >= 8) {
409
4.17M
        av_log(NULL, AV_LOG_WARNING, "Ignoring extra planes beyond 8\n");
410
4.17M
        return;
411
4.17M
    }
412
82.8M
    lut = plane8_lut[plane];
413
1.83G
    do {
414
1.83G
        uint64_t v = AV_RN64A(dst) | lut[*buf++];
415
1.83G
        AV_WN64A(dst, v);
416
1.83G
        dst += 8;
417
1.83G
    } while (--buf_size);
418
82.8M
}
419
420
/**
421
 * Decode interleaved plane buffer up to 24bpp
422
 * @param dst Destination buffer
423
 * @param buf Source buffer
424
 * @param buf_size
425
 * @param plane plane number to decode as
426
 */
427
static void decodeplane32(uint32_t *dst, const uint8_t *buf, int buf_size, int plane)
428
126M
{
429
126M
    const uint32_t *lut = plane32_lut[plane];
430
1.13G
    do {
431
1.13G
        unsigned mask = (*buf >> 2) & ~3;
432
1.13G
        dst[0] |= lut[mask++];
433
1.13G
        dst[1] |= lut[mask++];
434
1.13G
        dst[2] |= lut[mask++];
435
1.13G
        dst[3] |= lut[mask];
436
1.13G
        mask    = (*buf++ << 2) & 0x3F;
437
1.13G
        dst[4] |= lut[mask++];
438
1.13G
        dst[5] |= lut[mask++];
439
1.13G
        dst[6] |= lut[mask++];
440
1.13G
        dst[7] |= lut[mask];
441
1.13G
        dst    += 8;
442
1.13G
    } while (--buf_size);
443
126M
}
444
445
#define DECODE_HAM_PLANE32(x)       \
446
309M
    first       = buf[x] << 1;      \
447
309M
    second      = buf[(x)+1] << 1;  \
448
309M
    delta      &= pal[first++];     \
449
309M
    delta      |= pal[first];       \
450
309M
    dst[x]      = delta;            \
451
309M
    delta      &= pal[second++];    \
452
309M
    delta      |= pal[second];      \
453
309M
    dst[(x)+1]  = delta
454
455
/**
456
 * Converts one line of HAM6/8-encoded chunky buffer to 24bpp.
457
 *
458
 * @param dst the destination 24bpp buffer
459
 * @param buf the source 8bpp chunky buffer
460
 * @param pal the HAM decode table
461
 * @param buf_size the plane size in bytes
462
 */
463
static void decode_ham_plane32(uint32_t *dst, const uint8_t  *buf,
464
                               const uint32_t *const pal, unsigned buf_size)
465
13.6M
{
466
13.6M
    uint32_t delta = pal[1]; /* first palette entry */
467
77.3M
    do {
468
77.3M
        uint32_t first, second;
469
77.3M
        DECODE_HAM_PLANE32(0);
470
77.3M
        DECODE_HAM_PLANE32(2);
471
77.3M
        DECODE_HAM_PLANE32(4);
472
77.3M
        DECODE_HAM_PLANE32(6);
473
77.3M
        buf += 8;
474
77.3M
        dst += 8;
475
77.3M
    } while (--buf_size);
476
13.6M
}
477
478
static void lookup_pal_indicies(uint32_t *dst, const uint32_t *buf,
479
                         const uint32_t *const pal, unsigned width)
480
8.60M
{
481
406M
    do {
482
406M
        *dst++ = pal[*buf++];
483
406M
    } while (--width);
484
8.60M
}
485
486
/**
487
 * Decode one complete byterun1 encoded line.
488
 *
489
 * @param dst the destination buffer where to store decompressed bitstream
490
 * @param dst_size the destination plane size in bytes
491
 * @param buf the source byterun1 compressed bitstream
492
 * @param buf_end the EOF of source byterun1 compressed bitstream
493
 * @return number of consumed bytes in byterun1 compressed bitstream
494
 */
495
static int decode_byterun(uint8_t *dst, int dst_size,
496
                          GetByteContext *gb)
497
146M
{
498
146M
    unsigned x;
499
148M
    for (x = 0; x < dst_size && bytestream2_get_bytes_left(gb) > 0;) {
500
1.81M
        unsigned length;
501
1.81M
        const int8_t value = bytestream2_get_byte(gb);
502
1.81M
        if (value >= 0) {
503
841k
            length = FFMIN3(value + 1, dst_size - x, bytestream2_get_bytes_left(gb));
504
841k
            bytestream2_get_buffer(gb, dst + x, length);
505
841k
            if (length < value + 1)
506
111k
                bytestream2_skip(gb, value + 1 - length);
507
969k
        } else if (value > -128) {
508
953k
            length = FFMIN(-value + 1, dst_size - x);
509
953k
            memset(dst + x, bytestream2_get_byte(gb), length);
510
953k
        } else { // noop
511
16.3k
            continue;
512
16.3k
        }
513
1.79M
        x += length;
514
1.79M
    }
515
146M
    if (x < dst_size) {
516
146M
        av_log(NULL, AV_LOG_WARNING, "decode_byterun ended before plane size\n");
517
146M
        memset(dst+x, 0, dst_size - x);
518
146M
    }
519
146M
    return bytestream2_tell(gb);
520
146M
}
521
522
static int decode_byterun2(uint8_t *dst, int height, int line_size,
523
                           GetByteContext *gb)
524
15.4k
{
525
15.4k
    GetByteContext cmds;
526
15.4k
    int count;
527
15.4k
    int i, y_pos = 0, x_pos = 0;
528
529
15.4k
    if (bytestream2_get_be32(gb) != MKBETAG('V', 'D', 'A', 'T'))
530
13.8k
        return 0;
531
532
1.55k
    bytestream2_skip(gb, 4);
533
1.55k
    count = bytestream2_get_be16(gb) - 2;
534
1.55k
    if (count < 0 || bytestream2_get_bytes_left(gb) < count)
535
924
        return 0;
536
537
627
    bytestream2_init(&cmds, gb->buffer, count);
538
627
    bytestream2_skip(gb, count);
539
540
77.1k
    for (i = 0; i < count && x_pos < line_size; i++) {
541
76.5k
        int8_t cmd = bytestream2_get_byte(&cmds);
542
76.5k
        int l, r;
543
544
76.5k
        if (cmd == 0) {
545
21.3k
            l = bytestream2_get_be16(gb);
546
5.10M
            while (l-- > 0 && x_pos < line_size) {
547
5.08M
                dst[x_pos + y_pos   * line_size    ] = bytestream2_get_byte(gb);
548
5.08M
                dst[x_pos + y_pos++ * line_size + 1] = bytestream2_get_byte(gb);
549
5.08M
                if (y_pos >= height) {
550
11.7k
                    y_pos  = 0;
551
11.7k
                    x_pos += 2;
552
11.7k
                }
553
5.08M
            }
554
55.1k
        } else if (cmd < 0) {
555
15.1k
            l = -cmd;
556
543k
            while (l-- > 0 && x_pos < line_size) {
557
528k
                dst[x_pos + y_pos   * line_size    ] = bytestream2_get_byte(gb);
558
528k
                dst[x_pos + y_pos++ * line_size + 1] = bytestream2_get_byte(gb);
559
528k
                if (y_pos >= height) {
560
890
                    y_pos  = 0;
561
890
                    x_pos += 2;
562
890
                }
563
528k
            }
564
40.0k
        } else if (cmd == 1) {
565
1.57k
            l = bytestream2_get_be16(gb);
566
1.57k
            r = bytestream2_get_be16(gb);
567
1.44M
            while (l-- > 0 && x_pos < line_size) {
568
1.44M
                dst[x_pos + y_pos   * line_size    ] = r >> 8;
569
1.44M
                dst[x_pos + y_pos++ * line_size + 1] = r & 0xFF;
570
1.44M
                if (y_pos >= height) {
571
920
                    y_pos  = 0;
572
920
                    x_pos += 2;
573
920
                }
574
1.44M
            }
575
38.4k
        } else {
576
38.4k
            l = cmd;
577
38.4k
            r = bytestream2_get_be16(gb);
578
1.28M
            while (l-- > 0 && x_pos < line_size) {
579
1.25M
                dst[x_pos + y_pos   * line_size    ] = r >> 8;
580
1.25M
                dst[x_pos + y_pos++ * line_size + 1] = r & 0xFF;
581
1.25M
                if (y_pos >= height) {
582
3.98k
                    y_pos  = 0;
583
3.98k
                    x_pos += 2;
584
3.98k
                }
585
1.25M
            }
586
38.4k
        }
587
76.5k
    }
588
589
627
    return bytestream2_tell(gb);
590
1.55k
}
591
592
#define DECODE_RGBX_COMMON(type) \
593
38.6k
    if (!length) { \
594
5.90k
        length = bytestream2_get_byte(gb); \
595
5.90k
        if (!length) { \
596
3.70k
            length = bytestream2_get_be16(gb); \
597
3.70k
            if (!length) \
598
3.70k
                return; \
599
3.70k
        } \
600
5.90k
    } \
601
26.5M
    for (i = 0; i < length; i++) { \
602
26.5M
        *(type *)(dst + y*linesize + x * sizeof(type)) = pixel; \
603
26.5M
        x += 1; \
604
26.5M
        if (x >= width) { \
605
657k
            y += 1; \
606
657k
            if (y >= height) \
607
657k
                return; \
608
657k
            x = 0; \
609
657k
        } \
610
26.5M
    }
611
612
/**
613
 * Decode RGB8 buffer
614
 * @param[out] dst Destination buffer
615
 * @param width Width of destination buffer (pixels)
616
 * @param height Height of destination buffer (pixels)
617
 * @param linesize Line size of destination buffer (bytes)
618
 */
619
static void decode_rgb8(GetByteContext *gb, uint8_t *dst, int width, int height, int linesize)
620
2.00k
{
621
2.00k
    int x = 0, y = 0, i, length;
622
25.7k
    while (bytestream2_get_bytes_left(gb) >= 4) {
623
24.4k
        uint32_t pixel = 0xFF000000 | bytestream2_get_be24(gb);
624
24.4k
        length = bytestream2_get_byte(gb) & 0x7F;
625
24.4k
        DECODE_RGBX_COMMON(uint32_t)
626
23.9k
    }
627
2.00k
}
628
629
/**
630
 * Decode RGBN buffer
631
 * @param[out] dst Destination buffer
632
 * @param width Width of destination buffer (pixels)
633
 * @param height Height of destination buffer (pixels)
634
 * @param linesize Line size of destination buffer (bytes)
635
 */
636
static void decode_rgbn(GetByteContext *gb, uint8_t *dst, int width, int height, int linesize)
637
1.16k
{
638
1.16k
    int x = 0, y = 0, i, length;
639
14.6k
    while (bytestream2_get_bytes_left(gb) >= 2) {
640
14.1k
        uint32_t pixel = bytestream2_get_be16u(gb);
641
14.1k
        length = pixel & 0x7;
642
14.1k
        pixel >>= 4;
643
14.1k
        DECODE_RGBX_COMMON(uint16_t)
644
13.7k
    }
645
1.16k
}
646
647
/**
648
 * Decode DEEP RLE 32-bit buffer
649
 * @param[out] dst Destination buffer
650
 * @param[in] src Source buffer
651
 * @param src_size Source buffer size (bytes)
652
 * @param width Width of destination buffer (pixels)
653
 * @param height Height of destination buffer (pixels)
654
 * @param linesize Line size of destination buffer (bytes)
655
 */
656
static void decode_deep_rle32(uint8_t *dst, const uint8_t *src, int src_size, int width, int height, int linesize)
657
1.48k
{
658
1.48k
    const uint8_t *src_end = src + src_size;
659
1.48k
    int x = 0, y = 0, i;
660
45.4k
    while (src_end - src >= 5) {
661
44.7k
        int opcode;
662
44.7k
        opcode = *(int8_t *)src++;
663
44.7k
        if (opcode >= 0) {
664
30.3k
            int size = opcode + 1;
665
72.4k
            for (i = 0; i < size; i++) {
666
42.6k
                int length = FFMIN(size - i, width - x);
667
42.6k
                if (src_end - src < length * 4)
668
356
                    return;
669
42.2k
                memcpy(dst + y*linesize + x * 4, src, length * 4);
670
42.2k
                src += length * 4;
671
42.2k
                x += length;
672
42.2k
                i += length;
673
42.2k
                if (x >= width) {
674
14.6k
                    x = 0;
675
14.6k
                    y += 1;
676
14.6k
                    if (y >= height)
677
205
                        return;
678
14.6k
                }
679
42.2k
            }
680
30.3k
        } else {
681
14.4k
            int size = -opcode + 1;
682
14.4k
            uint32_t pixel = AV_RN32(src);
683
618k
            for (i = 0; i < size; i++) {
684
604k
                *(uint32_t *)(dst + y*linesize + x * 4) = pixel;
685
604k
                x += 1;
686
604k
                if (x >= width) {
687
20.4k
                    x = 0;
688
20.4k
                    y += 1;
689
20.4k
                    if (y >= height)
690
243
                        return;
691
20.4k
                }
692
604k
            }
693
14.1k
            src += 4;
694
14.1k
        }
695
44.7k
    }
696
1.48k
}
697
698
/**
699
 * Decode DEEP TVDC 32-bit buffer
700
 * @param[out] dst Destination buffer
701
 * @param[in] src Source buffer
702
 * @param src_size Source buffer size (bytes)
703
 * @param width Width of destination buffer (pixels)
704
 * @param height Height of destination buffer (pixels)
705
 * @param linesize Line size of destination buffer (bytes)
706
 * @param[int] tvdc TVDC lookup table
707
 */
708
static void decode_deep_tvdc32(uint8_t *dst, const uint8_t *src, int src_size, int width, int height, int linesize, const int16_t *tvdc)
709
2.19k
{
710
2.19k
    int x = 0, y = 0, plane = 0;
711
2.19k
    int8_t pixel = 0;
712
2.19k
    int i, j;
713
714
1.18M
    for (i = 0; i < src_size * 2;) {
715
1.29M
#define GETNIBBLE ((i & 1) ?  (src[i>>1] & 0xF) : (src[i>>1] >> 4))
716
1.18M
        int d = tvdc[GETNIBBLE];
717
1.18M
        i++;
718
1.18M
        if (d) {
719
1.07M
            pixel += d;
720
1.07M
            dst[y * linesize + x*4 + plane] = pixel;
721
1.07M
            x++;
722
1.07M
        } else {
723
110k
            if (i >= src_size * 2)
724
1.24k
                return;
725
109k
            d = GETNIBBLE + 1;
726
109k
            i++;
727
109k
            d = FFMIN(d, width - x);
728
875k
            for (j = 0; j < d; j++) {
729
766k
                dst[y * linesize + x*4 + plane] = pixel;
730
766k
                x++;
731
766k
            }
732
109k
        }
733
1.18M
        if (x >= width) {
734
25.0k
            plane++;
735
25.0k
            if (plane >= 4) {
736
6.18k
                y++;
737
6.18k
                if (y >= height)
738
229
                    return;
739
5.95k
                plane = 0;
740
5.95k
            }
741
24.8k
            x = 0;
742
24.8k
            pixel = 0;
743
24.8k
            i = (i + 1) & ~1;
744
24.8k
        }
745
1.18M
    }
746
2.19k
}
747
748
static void decode_short_horizontal_delta(uint8_t *dst,
749
                                          const uint8_t *buf, const uint8_t *buf_end,
750
                                          int w, int bpp, int dst_size)
751
8.25k
{
752
8.25k
    int planepitch = FFALIGN(w, 16) >> 3;
753
8.25k
    int pitch = planepitch * bpp;
754
8.25k
    GetByteContext ptrs, gb;
755
8.25k
    PutByteContext pb;
756
8.25k
    unsigned ofssrc, pos;
757
8.25k
    int i, k;
758
759
8.25k
    bytestream2_init(&ptrs, buf, buf_end - buf);
760
8.25k
    bytestream2_init_writer(&pb, dst, dst_size);
761
762
194k
    for (k = 0; k < bpp; k++) {
763
185k
        ofssrc = bytestream2_get_be32(&ptrs);
764
185k
        pos = 0;
765
766
185k
        if (!ofssrc)
767
177k
            continue;
768
769
7.86k
        if (ofssrc >= buf_end - buf)
770
6.29k
            continue;
771
772
1.56k
        bytestream2_init(&gb, buf + ofssrc, buf_end - (buf + ofssrc));
773
995k
        while (bytestream2_peek_be16(&gb) != 0xFFFF && bytestream2_get_bytes_left(&gb) > 3) {
774
994k
            int16_t offset = bytestream2_get_be16(&gb);
775
994k
            unsigned noffset;
776
777
994k
            if (offset >= 0) {
778
991k
                unsigned data = bytestream2_get_be16(&gb);
779
780
991k
                pos += offset * 2;
781
991k
                noffset = (pos / planepitch) * pitch + (pos % planepitch) + k * planepitch;
782
991k
                bytestream2_seek_p(&pb, noffset, SEEK_SET);
783
991k
                bytestream2_put_be16(&pb, data);
784
991k
            } else {
785
2.59k
                uint16_t count = bytestream2_get_be16(&gb);
786
787
2.59k
                pos += 2 * -(offset + 2);
788
29.8M
                for (i = 0; i < count; i++) {
789
29.8M
                    uint16_t data = bytestream2_get_be16(&gb);
790
791
29.8M
                    pos += 2;
792
29.8M
                    noffset = (pos / planepitch) * pitch + (pos % planepitch) + k * planepitch;
793
29.8M
                    bytestream2_seek_p(&pb, noffset, SEEK_SET);
794
29.8M
                    bytestream2_put_be16(&pb, data);
795
29.8M
                }
796
2.59k
            }
797
994k
        }
798
1.56k
    }
799
8.25k
}
800
801
static void decode_byte_vertical_delta(uint8_t *dst,
802
                                       const uint8_t *buf, const uint8_t *buf_end,
803
                                       int w, int xor, int bpp, int dst_size)
804
1.64k
{
805
1.64k
    int ncolumns = ((w + 15) / 16) * 2;
806
1.64k
    int dstpitch = ncolumns * bpp;
807
1.64k
    unsigned ofsdst, ofssrc, opcode, x;
808
1.64k
    GetByteContext ptrs, gb;
809
1.64k
    PutByteContext pb;
810
1.64k
    int i, j, k;
811
812
1.64k
    bytestream2_init(&ptrs, buf, buf_end - buf);
813
1.64k
    bytestream2_init_writer(&pb, dst, dst_size);
814
815
16.8k
    for (k = 0; k < bpp; k++) {
816
15.2k
        ofssrc = bytestream2_get_be32(&ptrs);
817
818
15.2k
        if (!ofssrc)
819
9.01k
            continue;
820
821
6.22k
        if (ofssrc >= buf_end - buf)
822
4.72k
            continue;
823
824
1.49k
        bytestream2_init(&gb, buf + ofssrc, buf_end - (buf + ofssrc));
825
8.48M
        for (j = 0; j < ncolumns; j++) {
826
8.48M
            ofsdst = j + k * ncolumns;
827
828
8.48M
            i = bytestream2_get_byte(&gb);
829
13.4M
            while (i > 0) {
830
4.97M
                opcode = bytestream2_get_byte(&gb);
831
832
4.97M
                if (opcode == 0) {
833
617k
                    opcode  = bytestream2_get_byte(&gb);
834
617k
                    x = bytestream2_get_byte(&gb);
835
836
6.26M
                    while (opcode) {
837
5.64M
                        bytestream2_seek_p(&pb, ofsdst, SEEK_SET);
838
5.64M
                        if (xor && ofsdst < dst_size) {
839
18.8k
                            bytestream2_put_byte(&pb, dst[ofsdst] ^ x);
840
5.62M
                        } else {
841
5.62M
                            bytestream2_put_byte(&pb, x);
842
5.62M
                        }
843
5.64M
                        ofsdst += dstpitch;
844
5.64M
                        opcode--;
845
5.64M
                    }
846
4.35M
                } else if (opcode < 0x80) {
847
4.30M
                    ofsdst += opcode * dstpitch;
848
4.30M
                } else {
849
56.3k
                    opcode &= 0x7f;
850
851
4.63M
                    while (opcode) {
852
4.58M
                        bytestream2_seek_p(&pb, ofsdst, SEEK_SET);
853
4.58M
                        if (xor && ofsdst < dst_size) {
854
16.2k
                            bytestream2_put_byte(&pb, dst[ofsdst] ^ bytestream2_get_byte(&gb));
855
4.56M
                        } else {
856
4.56M
                            bytestream2_put_byte(&pb, bytestream2_get_byte(&gb));
857
4.56M
                        }
858
4.58M
                        ofsdst += dstpitch;
859
4.58M
                        opcode--;
860
4.58M
                    }
861
56.3k
                }
862
4.97M
                i--;
863
4.97M
            }
864
8.48M
        }
865
1.49k
    }
866
1.64k
}
867
868
static void decode_delta_j(uint8_t *dst,
869
                           const uint8_t *buf, const uint8_t *buf_end,
870
                           int w, int h, int bpp, int dst_size)
871
6.65k
{
872
6.65k
    int32_t pitch;
873
6.65k
    uint8_t *ptr;
874
6.65k
    uint32_t type, flag, cols, groups, rows, bytes;
875
6.65k
    uint32_t offset;
876
6.65k
    int planepitch_byte = (w + 7) / 8;
877
6.65k
    int planepitch = ((w + 15) / 16) * 2;
878
6.65k
    int kludge_j, b, g, r, d;
879
6.65k
    GetByteContext gb;
880
881
6.65k
    pitch = planepitch * bpp;
882
6.65k
    kludge_j = w < 320 ? (320 - w) / 8 / 2 : 0;
883
884
6.65k
    bytestream2_init(&gb, buf, buf_end - buf);
885
886
8.02k
    while (bytestream2_get_bytes_left(&gb) >= 2) {
887
5.16k
        type = bytestream2_get_be16(&gb);
888
889
5.16k
        switch (type) {
890
158
        case 0:
891
158
            return;
892
2.11k
        case 1:
893
2.11k
            flag   = bytestream2_get_be16(&gb);
894
2.11k
            cols   = bytestream2_get_be16(&gb);
895
2.11k
            groups = bytestream2_get_be16(&gb);
896
897
548k
            for (g = 0; g < groups; g++) {
898
548k
                offset = bytestream2_get_be16(&gb);
899
900
548k
                if (cols * bpp == 0 || bytestream2_get_bytes_left(&gb) < cols * bpp) {
901
1.03k
                    av_log(NULL, AV_LOG_ERROR, "cols*bpp is invalid (%"PRId32"*%d)", cols, bpp);
902
1.03k
                    return;
903
1.03k
                }
904
905
547k
                if (kludge_j)
906
1.73k
                    offset = ((offset / (320 / 8)) * pitch) + (offset % (320 / 8)) - kludge_j;
907
545k
                else
908
545k
                    offset = ((offset / planepitch_byte) * pitch) + (offset % planepitch_byte);
909
910
1.09M
                for (b = 0; b < cols; b++) {
911
1.15M
                    for (d = 0; d < bpp; d++) {
912
610k
                        uint8_t value = bytestream2_get_byte(&gb);
913
914
610k
                        if (offset >= dst_size)
915
569
                            return;
916
609k
                        ptr = dst + offset;
917
918
609k
                        if (flag)
919
62.4k
                            ptr[0] ^= value;
920
547k
                        else
921
547k
                            ptr[0]  = value;
922
923
609k
                        offset += planepitch;
924
609k
                    }
925
548k
                }
926
546k
                if ((cols * bpp) & 1)
927
543k
                    bytestream2_skip(&gb, 1);
928
546k
            }
929
506
            break;
930
2.46k
        case 2:
931
2.46k
            flag   = bytestream2_get_be16(&gb);
932
2.46k
            rows   = bytestream2_get_be16(&gb);
933
2.46k
            bytes  = bytestream2_get_be16(&gb);
934
2.46k
            groups = bytestream2_get_be16(&gb);
935
936
13.0M
            for (g = 0; g < groups; g++) {
937
13.0M
                offset = bytestream2_get_be16(&gb);
938
939
13.0M
                if (kludge_j)
940
98.9k
                    offset = ((offset / (320 / 8)) * pitch) + (offset % (320/ 8)) - kludge_j;
941
12.9M
                else
942
12.9M
                    offset = ((offset / planepitch_byte) * pitch) + (offset % planepitch_byte);
943
944
13.0M
                for (r = 0; r < rows; r++) {
945
242k
                    for (d = 0; d < bpp; d++) {
946
231k
                        unsigned noffset = offset + (r * pitch) + d * planepitch;
947
948
231k
                        if (!bytes || bytestream2_get_bytes_left(&gb) < bytes) {
949
955
                            av_log(NULL, AV_LOG_ERROR, "bytes %"PRId32" is invalid", bytes);
950
955
                            return;
951
955
                        }
952
953
475k
                        for (b = 0; b < bytes; b++) {
954
246k
                            uint8_t value = bytestream2_get_byte(&gb);
955
956
246k
                            if (noffset >= dst_size)
957
650
                                return;
958
245k
                            ptr = dst + noffset;
959
960
245k
                            if (flag)
961
16.0k
                                ptr[0] ^= value;
962
229k
                            else
963
229k
                                ptr[0]  = value;
964
965
245k
                            noffset++;
966
245k
                        }
967
230k
                    }
968
13.0k
                }
969
13.0M
                if ((rows * bytes * bpp) & 1)
970
3.38k
                    bytestream2_skip(&gb, 1);
971
13.0M
            }
972
857
            break;
973
857
        default:
974
429
            return;
975
5.16k
        }
976
5.16k
    }
977
6.65k
}
978
979
static void decode_short_vertical_delta(uint8_t *dst,
980
                                        const uint8_t *buf, const uint8_t *buf_end,
981
                                        int w, int bpp, int dst_size)
982
2.57k
{
983
2.57k
    int ncolumns = (w + 15) >> 4;
984
2.57k
    int dstpitch = ncolumns * bpp * 2;
985
2.57k
    unsigned ofsdst, ofssrc, ofsdata, opcode, x;
986
2.57k
    GetByteContext ptrs, gb, dptrs, dgb;
987
2.57k
    PutByteContext pb;
988
2.57k
    int i, j, k;
989
990
2.57k
    if (buf_end - buf <= 64)
991
1.16k
        return;
992
993
1.40k
    bytestream2_init(&ptrs, buf, buf_end - buf);
994
1.40k
    bytestream2_init(&dptrs, buf + 32, (buf_end - buf) - 32);
995
1.40k
    bytestream2_init_writer(&pb, dst, dst_size);
996
997
5.17k
    for (k = 0; k < bpp; k++) {
998
4.91k
        ofssrc = bytestream2_get_be32(&ptrs);
999
4.91k
        ofsdata = bytestream2_get_be32(&dptrs);
1000
1001
4.91k
        if (!ofssrc)
1002
2.56k
            continue;
1003
1004
2.34k
        if (ofssrc >= buf_end - buf)
1005
858
            return;
1006
1007
1.48k
        if (ofsdata >= buf_end - buf)
1008
287
            return;
1009
1010
1.20k
        bytestream2_init(&gb, buf + ofssrc, buf_end - (buf + ofssrc));
1011
1.20k
        bytestream2_init(&dgb, buf + ofsdata, buf_end - (buf + ofsdata));
1012
1.48M
        for (j = 0; j < ncolumns; j++) {
1013
1.48M
            ofsdst = (j + k * ncolumns) * 2;
1014
1015
1.48M
            i = bytestream2_get_byte(&gb);
1016
8.45M
            while (i > 0) {
1017
6.97M
                opcode = bytestream2_get_byte(&gb);
1018
1019
6.97M
                if (opcode == 0) {
1020
1.32M
                    opcode = bytestream2_get_byte(&gb);
1021
1.32M
                    x = bytestream2_get_be16(&dgb);
1022
1023
41.1M
                    while (opcode) {
1024
39.8M
                        bytestream2_seek_p(&pb, ofsdst, SEEK_SET);
1025
39.8M
                        bytestream2_put_be16(&pb, x);
1026
39.8M
                        ofsdst += dstpitch;
1027
39.8M
                        opcode--;
1028
39.8M
                    }
1029
5.65M
                } else if (opcode < 0x80) {
1030
1.97M
                    ofsdst += opcode * dstpitch;
1031
3.67M
                } else {
1032
3.67M
                    opcode &= 0x7f;
1033
1034
376M
                    while (opcode) {
1035
372M
                        bytestream2_seek_p(&pb, ofsdst, SEEK_SET);
1036
372M
                        bytestream2_put_be16(&pb, bytestream2_get_be16(&dgb));
1037
372M
                        ofsdst += dstpitch;
1038
372M
                        opcode--;
1039
372M
                    }
1040
3.67M
                }
1041
6.97M
                i--;
1042
6.97M
            }
1043
1.48M
        }
1044
1.20k
    }
1045
1.40k
}
1046
1047
static void decode_long_vertical_delta(uint8_t *dst,
1048
                                       const uint8_t *buf, const uint8_t *buf_end,
1049
                                       int w, int bpp, int dst_size)
1050
3.28k
{
1051
3.28k
    int ncolumns = (w + 31) >> 5;
1052
3.28k
    int dstpitch = ((w + 15) / 16 * 2) * bpp;
1053
3.28k
    unsigned ofsdst, ofssrc, ofsdata, opcode, x;
1054
3.28k
    GetByteContext ptrs, gb, dptrs, dgb;
1055
3.28k
    PutByteContext pb;
1056
3.28k
    int i, j, k, h;
1057
1058
3.28k
    if (buf_end - buf <= 64)
1059
1.11k
        return;
1060
1061
2.17k
    h = (((w + 15) / 16 * 2) != ((w + 31) / 32 * 4)) ? 1 : 0;
1062
2.17k
    bytestream2_init(&ptrs, buf, buf_end - buf);
1063
2.17k
    bytestream2_init(&dptrs, buf + 32, (buf_end - buf) - 32);
1064
2.17k
    bytestream2_init_writer(&pb, dst, dst_size);
1065
1066
5.16k
    for (k = 0; k < bpp; k++) {
1067
4.84k
        ofssrc = bytestream2_get_be32(&ptrs);
1068
4.84k
        ofsdata = bytestream2_get_be32(&dptrs);
1069
1070
4.84k
        if (!ofssrc)
1071
2.32k
            continue;
1072
1073
2.52k
        if (ofssrc >= buf_end - buf)
1074
472
            return;
1075
1076
2.05k
        if (ofsdata >= buf_end - buf)
1077
313
            return;
1078
1079
1.74k
        bytestream2_init(&gb, buf + ofssrc, buf_end - (buf + ofssrc));
1080
1.74k
        bytestream2_init(&dgb, buf + ofsdata, buf_end - (buf + ofsdata));
1081
43.1k
        for (j = 0; j < ncolumns; j++) {
1082
42.4k
            ofsdst = (j + k * ncolumns) * 4 - h * (2 * k);
1083
1084
42.4k
            i = bytestream2_get_byte(&gb);
1085
205k
            while (i > 0) {
1086
164k
                opcode = bytestream2_get_byte(&gb);
1087
1088
164k
                if (opcode == 0) {
1089
52.0k
                    opcode = bytestream2_get_byte(&gb);
1090
52.0k
                    if (h && (j == (ncolumns - 1))) {
1091
4.96k
                        x = bytestream2_get_be16(&dgb);
1092
4.96k
                        bytestream2_skip(&dgb, 2);
1093
47.0k
                    } else {
1094
47.0k
                        x = bytestream2_get_be32(&dgb);
1095
47.0k
                    }
1096
1097
52.0k
                    if (ofsdst + (opcode - 1LL) * dstpitch > bytestream2_size_p(&pb))
1098
1.06k
                        return;
1099
1100
1.00M
                    while (opcode) {
1101
951k
                        bytestream2_seek_p(&pb, ofsdst, SEEK_SET);
1102
951k
                        if (h && (j == (ncolumns - 1))) {
1103
37.4k
                            bytestream2_put_be16(&pb, x);
1104
913k
                        } else {
1105
913k
                            bytestream2_put_be32(&pb, x);
1106
913k
                        }
1107
951k
                        ofsdst += dstpitch;
1108
951k
                        opcode--;
1109
951k
                    }
1110
112k
                } else if (opcode < 0x80) {
1111
78.7k
                    ofsdst += opcode * dstpitch;
1112
78.7k
                } else {
1113
33.2k
                    opcode &= 0x7f;
1114
1115
3.00M
                    while (opcode) {
1116
2.97M
                        bytestream2_seek_p(&pb, ofsdst, SEEK_SET);
1117
2.97M
                        if (h && (j == (ncolumns - 1))) {
1118
241k
                            bytestream2_put_be16(&pb, bytestream2_get_be16(&dgb));
1119
241k
                            bytestream2_skip(&dgb, 2);
1120
2.73M
                        } else {
1121
2.73M
                            bytestream2_put_be32(&pb, bytestream2_get_be32(&dgb));
1122
2.73M
                        }
1123
2.97M
                        ofsdst += dstpitch;
1124
2.97M
                        opcode--;
1125
2.97M
                    }
1126
33.2k
                }
1127
162k
                i--;
1128
162k
            }
1129
42.4k
        }
1130
1.74k
    }
1131
2.17k
}
1132
1133
static void decode_short_vertical_delta2(uint8_t *dst,
1134
                                         const uint8_t *buf, const uint8_t *buf_end,
1135
                                         int w, int bpp, int dst_size)
1136
3.76k
{
1137
3.76k
    int ncolumns = (w + 15) >> 4;
1138
3.76k
    int dstpitch = ncolumns * bpp * 2;
1139
3.76k
    unsigned ofsdst, ofssrc, opcode, x;
1140
3.76k
    GetByteContext ptrs, gb;
1141
3.76k
    PutByteContext pb;
1142
3.76k
    int i, j, k;
1143
1144
3.76k
    bytestream2_init(&ptrs, buf, buf_end - buf);
1145
3.76k
    bytestream2_init_writer(&pb, dst, dst_size);
1146
1147
44.3k
    for (k = 0; k < bpp; k++) {
1148
40.6k
        ofssrc = bytestream2_get_be32(&ptrs);
1149
1150
40.6k
        if (!ofssrc)
1151
27.1k
            continue;
1152
1153
13.4k
        if (ofssrc >= buf_end - buf)
1154
10.9k
            continue;
1155
1156
2.47k
        bytestream2_init(&gb, buf + ofssrc, buf_end - (buf + ofssrc));
1157
1.15M
        for (j = 0; j < ncolumns; j++) {
1158
1.14M
            ofsdst = (j + k * ncolumns) * 2;
1159
1160
1.14M
            i = bytestream2_get_be16(&gb);
1161
2.47M
            while (i > 0 && bytestream2_get_bytes_left(&gb) > 4) {
1162
1.32M
                opcode = bytestream2_get_be16(&gb);
1163
1164
1.32M
                if (opcode == 0) {
1165
134k
                    opcode = bytestream2_get_be16(&gb);
1166
134k
                    x = bytestream2_get_be16(&gb);
1167
1168
2.46M
                    while (opcode && bytestream2_get_bytes_left_p(&pb) > 1) {
1169
2.32M
                        bytestream2_seek_p(&pb, ofsdst, SEEK_SET);
1170
2.32M
                        bytestream2_put_be16(&pb, x);
1171
2.32M
                        ofsdst += dstpitch;
1172
2.32M
                        opcode--;
1173
2.32M
                    }
1174
1.19M
                } else if (opcode < 0x8000) {
1175
771k
                    ofsdst += opcode * dstpitch;
1176
771k
                } else {
1177
422k
                    opcode &= 0x7fff;
1178
1179
1.09M
                    while (opcode && bytestream2_get_bytes_left(&gb) > 1 &&
1180
1.09M
                           bytestream2_get_bytes_left_p(&pb) > 1) {
1181
673k
                        bytestream2_seek_p(&pb, ofsdst, SEEK_SET);
1182
673k
                        bytestream2_put_be16(&pb, bytestream2_get_be16(&gb));
1183
673k
                        ofsdst += dstpitch;
1184
673k
                        opcode--;
1185
673k
                    }
1186
422k
                }
1187
1.32M
                i--;
1188
1.32M
            }
1189
1.14M
        }
1190
2.47k
    }
1191
3.76k
}
1192
1193
static void decode_long_vertical_delta2(uint8_t *dst,
1194
                                        const uint8_t *buf, const uint8_t *buf_end,
1195
                                        int w, int bpp, int dst_size)
1196
9.41k
{
1197
9.41k
    int ncolumns = (w + 31) >> 5;
1198
9.41k
    int dstpitch = ((w + 15) / 16 * 2) * bpp;
1199
9.41k
    unsigned ofsdst, ofssrc, opcode, x;
1200
9.41k
    unsigned skip = 0x80000000, mask = skip - 1;
1201
9.41k
    GetByteContext ptrs, gb;
1202
9.41k
    PutByteContext pb;
1203
9.41k
    int i, j, k, h;
1204
1205
9.41k
    h = (((w + 15) / 16 * 2) != ((w + 31) / 32 * 4)) ? 1 : 0;
1206
9.41k
    bytestream2_init(&ptrs, buf, buf_end - buf);
1207
9.41k
    bytestream2_init_writer(&pb, dst, dst_size);
1208
1209
98.4k
    for (k = 0; k < bpp; k++) {
1210
90.2k
        ofssrc = bytestream2_get_be32(&ptrs);
1211
1212
90.2k
        if (!ofssrc)
1213
75.3k
            continue;
1214
1215
14.8k
        if (ofssrc >= buf_end - buf)
1216
10.2k
            continue;
1217
1218
4.54k
        bytestream2_init(&gb, buf + ofssrc, buf_end - (buf + ofssrc));
1219
256k
        for (j = 0; j < ncolumns; j++) {
1220
253k
            ofsdst = (j + k * ncolumns) * 4 - h * (2 * k);
1221
1222
253k
            if (h && (j == (ncolumns - 1))) {
1223
3.58k
                skip = 0x8000;
1224
3.58k
                mask = skip - 1;
1225
3.58k
            }
1226
1227
253k
            i = bytestream2_get_be32(&gb);
1228
340k
            while (i > 0 && bytestream2_get_bytes_left(&gb) > 4) {
1229
88.0k
                opcode = bytestream2_get_be32(&gb);
1230
1231
88.0k
                if (opcode == 0) {
1232
66.0k
                    if (h && (j == ncolumns - 1)) {
1233
56.1k
                        opcode = bytestream2_get_be16(&gb);
1234
56.1k
                        x = bytestream2_get_be16(&gb);
1235
56.1k
                    } else {
1236
9.94k
                        opcode = bytestream2_get_be32(&gb);
1237
9.94k
                        x = bytestream2_get_be32(&gb);
1238
9.94k
                    }
1239
1240
66.0k
                    if (ofsdst + (opcode - 1LL) * dstpitch > bytestream2_size_p(&pb))
1241
1.16k
                        return;
1242
1243
10.6M
                    while (opcode && bytestream2_get_bytes_left_p(&pb) > 1) {
1244
10.5M
                        bytestream2_seek_p(&pb, ofsdst, SEEK_SET);
1245
10.5M
                        if (h && (j == ncolumns - 1))
1246
3.01M
                            bytestream2_put_be16(&pb, x);
1247
7.57M
                        else
1248
7.57M
                            bytestream2_put_be32(&pb, x);
1249
10.5M
                        ofsdst += dstpitch;
1250
10.5M
                        opcode--;
1251
10.5M
                    }
1252
64.9k
                } else if (opcode < skip) {
1253
8.23k
                    ofsdst += opcode * dstpitch;
1254
13.7k
                } else {
1255
13.7k
                    opcode &= mask;
1256
1257
3.27M
                    while (opcode && bytestream2_get_bytes_left(&gb) > 1 &&
1258
3.27M
                           bytestream2_get_bytes_left_p(&pb) > 1) {
1259
3.26M
                        bytestream2_seek_p(&pb, ofsdst, SEEK_SET);
1260
3.26M
                        if (h && (j == ncolumns - 1)) {
1261
2.46M
                            bytestream2_put_be16(&pb, bytestream2_get_be16(&gb));
1262
2.46M
                        } else {
1263
797k
                            bytestream2_put_be32(&pb, bytestream2_get_be32(&gb));
1264
797k
                        }
1265
3.26M
                        ofsdst += dstpitch;
1266
3.26M
                        opcode--;
1267
3.26M
                    }
1268
13.7k
                }
1269
86.8k
                i--;
1270
86.8k
            }
1271
253k
        }
1272
4.54k
    }
1273
9.41k
}
1274
1275
static void decode_delta_d(uint8_t *dst,
1276
                           const uint8_t *buf, const uint8_t *buf_end,
1277
                           int w, int flag, int bpp, int dst_size)
1278
2.61k
{
1279
2.61k
    int planepitch = FFALIGN(w, 16) >> 3;
1280
2.61k
    int pitch = planepitch * bpp;
1281
2.61k
    int planepitch_byte = (w + 7) / 8;
1282
2.61k
    unsigned entries, ofssrc;
1283
2.61k
    GetByteContext gb, ptrs;
1284
2.61k
    PutByteContext pb;
1285
2.61k
    int k;
1286
1287
2.61k
    if (buf_end - buf <= 4 * bpp)
1288
1.49k
        return;
1289
1290
1.11k
    bytestream2_init_writer(&pb, dst, dst_size);
1291
1.11k
    bytestream2_init(&ptrs, buf, bpp * 4);
1292
1293
9.91k
    for (k = 0; k < bpp; k++) {
1294
9.18k
        ofssrc = bytestream2_get_be32(&ptrs);
1295
1296
9.18k
        if (!ofssrc)
1297
1.87k
            continue;
1298
1299
7.30k
        if (ofssrc >= buf_end - buf)
1300
4.68k
            continue;
1301
1302
2.62k
        bytestream2_init(&gb, buf + ofssrc, buf_end - (buf + ofssrc));
1303
1304
2.62k
        entries = bytestream2_get_be32(&gb);
1305
2.62k
        if (entries * 8LL > bytestream2_get_bytes_left(&gb))
1306
382
            return;
1307
1308
30.3k
        while (entries && bytestream2_get_bytes_left(&gb) >= 8) {
1309
28.0k
            int32_t opcode  = bytestream2_get_be32(&gb);
1310
28.0k
            unsigned offset = bytestream2_get_be32(&gb);
1311
1312
28.0k
            bytestream2_seek_p(&pb, (offset / planepitch_byte) * pitch + (offset % planepitch_byte) + k * planepitch, SEEK_SET);
1313
28.0k
            if (opcode >= 0) {
1314
25.6k
                uint32_t x = bytestream2_get_be32(&gb);
1315
25.6k
                if (opcode && 4 + (opcode - 1LL) * pitch > bytestream2_get_bytes_left_p(&pb))
1316
19.7k
                    continue;
1317
1.71M
                while (opcode && bytestream2_get_bytes_left_p(&pb) > 0) {
1318
1.70M
                    bytestream2_put_be32(&pb, x);
1319
1.70M
                    bytestream2_skip_p(&pb, pitch - 4);
1320
1.70M
                    opcode--;
1321
1.70M
                }
1322
5.95k
            } else {
1323
2.17M
                while (opcode && bytestream2_get_bytes_left(&gb) > 0) {
1324
2.17M
                    bytestream2_put_be32(&pb, bytestream2_get_be32(&gb));
1325
2.17M
                    bytestream2_skip_p(&pb, pitch - 4);
1326
2.17M
                    opcode++;
1327
2.17M
                }
1328
2.40k
            }
1329
8.36k
            entries--;
1330
8.36k
        }
1331
2.24k
    }
1332
1.11k
}
1333
1334
static void decode_delta_e(uint8_t *dst,
1335
                           const uint8_t *buf, const uint8_t *buf_end,
1336
                           int w, int flag, int bpp, int dst_size)
1337
2.22k
{
1338
2.22k
    int planepitch = FFALIGN(w, 16) >> 3;
1339
2.22k
    int pitch = planepitch * bpp;
1340
2.22k
    int planepitch_byte = (w + 7) / 8;
1341
2.22k
    unsigned entries, ofssrc;
1342
2.22k
    GetByteContext gb, ptrs;
1343
2.22k
    PutByteContext pb;
1344
2.22k
    int k;
1345
1346
2.22k
    if (buf_end - buf <= 4 * bpp)
1347
1.33k
        return;
1348
1349
887
    bytestream2_init_writer(&pb, dst, dst_size);
1350
887
    bytestream2_init(&ptrs, buf, bpp * 4);
1351
1352
14.3k
    for (k = 0; k < bpp; k++) {
1353
13.4k
        ofssrc = bytestream2_get_be32(&ptrs);
1354
1355
13.4k
        if (!ofssrc)
1356
3.41k
            continue;
1357
1358
10.0k
        if (ofssrc >= buf_end - buf)
1359
7.63k
            continue;
1360
1361
2.42k
        bytestream2_init(&gb, buf + ofssrc, buf_end - (buf + ofssrc));
1362
1363
2.42k
        entries = bytestream2_get_be16(&gb);
1364
127k
        while (entries && bytestream2_get_bytes_left(&gb) >= 6) {
1365
125k
            int16_t opcode  = bytestream2_get_be16(&gb);
1366
125k
            unsigned offset = bytestream2_get_be32(&gb);
1367
1368
125k
            bytestream2_seek_p(&pb, (offset / planepitch_byte) * pitch + (offset % planepitch_byte) + k * planepitch, SEEK_SET);
1369
125k
            if (opcode >= 0) {
1370
122k
                uint16_t x = bytestream2_get_be16(&gb);
1371
10.4M
                while (opcode && bytestream2_get_bytes_left_p(&pb) > 0) {
1372
10.3M
                    bytestream2_put_be16(&pb, x);
1373
10.3M
                    bytestream2_skip_p(&pb, pitch - 2);
1374
10.3M
                    opcode--;
1375
10.3M
                }
1376
122k
            } else {
1377
2.83k
                opcode = -opcode;
1378
4.36M
                while (opcode && bytestream2_get_bytes_left(&gb) > 0) {
1379
4.36M
                    bytestream2_put_be16(&pb, bytestream2_get_be16(&gb));
1380
4.36M
                    bytestream2_skip_p(&pb, pitch - 2);
1381
4.36M
                    opcode--;
1382
4.36M
                }
1383
2.83k
            }
1384
125k
            entries--;
1385
125k
        }
1386
2.42k
    }
1387
887
}
1388
1389
static void decode_delta_l(uint8_t *dst,
1390
                           const uint8_t *buf, const uint8_t *buf_end,
1391
                           int w, int flag, int bpp, int dst_size)
1392
3.91k
{
1393
3.91k
    GetByteContext off0, off1, dgb, ogb;
1394
3.91k
    PutByteContext pb;
1395
3.91k
    unsigned poff0, poff1;
1396
3.91k
    int i, k, dstpitch;
1397
3.91k
    int planepitch_byte = (w + 7) / 8;
1398
3.91k
    int planepitch = ((w + 15) / 16) * 2;
1399
3.91k
    int pitch = planepitch * bpp;
1400
3.91k
    int count = 0;
1401
1402
3.91k
    if (buf_end - buf <= 64)
1403
2.17k
        return;
1404
1405
1.74k
    bytestream2_init(&off0, buf, buf_end - buf);
1406
1.74k
    bytestream2_init(&off1, buf + 32, buf_end - (buf + 32));
1407
1.74k
    bytestream2_init_writer(&pb, dst, dst_size);
1408
1409
1.74k
    dstpitch = flag ? (((w + 7) / 8) * bpp): 2;
1410
1411
9.69k
    for (k = 0; k < bpp; k++) {
1412
9.36k
        poff0 = bytestream2_get_be32(&off0);
1413
9.36k
        poff1 = bytestream2_get_be32(&off1);
1414
1415
9.36k
        if (!poff0)
1416
5.54k
            continue;
1417
1418
3.81k
        if (2LL * poff0 >= buf_end - buf)
1419
846
            return;
1420
1421
2.97k
        if (2LL * poff1 >= buf_end - buf)
1422
563
            return;
1423
1424
2.40k
        bytestream2_init(&dgb, buf + 2 * poff0, buf_end - (buf + 2 * poff0));
1425
2.40k
        bytestream2_init(&ogb, buf + 2 * poff1, buf_end - (buf + 2 * poff1));
1426
1427
41.1k
        while (bytestream2_peek_be16(&ogb) != 0xFFFF && bytestream2_get_bytes_left(&ogb) >= 4) {
1428
40.5k
            uint32_t offset = bytestream2_get_be16(&ogb);
1429
40.5k
            int16_t cnt = bytestream2_get_be16(&ogb);
1430
40.5k
            uint16_t data;
1431
1432
40.5k
            if (count > dst_size)
1433
403
                break;
1434
40.1k
            offset = ((2 * offset) / planepitch_byte) * pitch + ((2 * offset) % planepitch_byte) + k * planepitch;
1435
40.1k
            if (cnt < 0) {
1436
5.84k
                if (bytestream2_get_bytes_left(&dgb) < 2)
1437
223
                    break;
1438
5.62k
                bytestream2_seek_p(&pb, offset, SEEK_SET);
1439
5.62k
                cnt = -cnt;
1440
5.62k
                data = bytestream2_get_be16(&dgb);
1441
5.62k
                count += cnt;
1442
39.8M
                for (i = 0; i < cnt; i++) {
1443
39.8M
                    bytestream2_put_be16(&pb, data);
1444
39.8M
                    bytestream2_skip_p(&pb, dstpitch - 2);
1445
39.8M
                }
1446
34.2k
            } else {
1447
34.2k
                if (bytestream2_get_bytes_left(&dgb) < 2*cnt)
1448
1.17k
                    break;
1449
33.1k
                bytestream2_seek_p(&pb, offset, SEEK_SET);
1450
33.1k
                count += cnt;
1451
4.16M
                for (i = 0; i < cnt; i++) {
1452
4.12M
                    data = bytestream2_get_be16(&dgb);
1453
4.12M
                    bytestream2_put_be16(&pb, data);
1454
4.12M
                    bytestream2_skip_p(&pb, dstpitch - 2);
1455
4.12M
                }
1456
33.1k
            }
1457
40.1k
        }
1458
2.40k
    }
1459
1.74k
}
1460
1461
static int unsupported(AVCodecContext *avctx)
1462
38.0k
{
1463
38.0k
    IffContext *s = avctx->priv_data;
1464
38.0k
    avpriv_request_sample(avctx, "bitmap (compression 0x%0x, bpp %i, ham %i, interlaced %i)", s->compression, s->bpp, s->ham, s->is_interlaced);
1465
38.0k
    return AVERROR_INVALIDDATA;
1466
38.0k
}
1467
1468
static int parse_packet_header(AVCodecContext *const avctx,
1469
                               GetByteContext *gb)
1470
165k
{
1471
165k
    IffContext *s = avctx->priv_data;
1472
1473
165k
    if (avctx->codec_tag != MKTAG('A', 'N', 'I', 'M'))
1474
50.6k
        return 0;
1475
1476
115k
    bytestream2_skip(gb, 4);
1477
139k
    while (bytestream2_get_bytes_left(gb) >= 1) {
1478
45.9k
        uint32_t chunk_id  = bytestream2_get_le32(gb);
1479
45.9k
        uint64_t data_size = bytestream2_get_be32(gb);
1480
1481
45.9k
        if (chunk_id == MKTAG('B', 'M', 'H', 'D')) {
1482
487
            bytestream2_skip(gb, data_size + (data_size & 1));
1483
45.4k
        } else if (chunk_id == MKTAG('A', 'N', 'H', 'D')) {
1484
4.72k
            unsigned extra;
1485
4.72k
            if (data_size < 40)
1486
255
                return AVERROR_INVALIDDATA;
1487
1488
4.47k
            s->compression = (bytestream2_get_byte(gb) << 8) | (s->compression & 0xFF);
1489
4.47k
            bytestream2_skip(gb, 19);
1490
4.47k
            extra = bytestream2_get_be32(gb);
1491
4.47k
            s->is_short = !(extra & 1);
1492
4.47k
            s->is_brush = extra == 2;
1493
4.47k
            s->is_interlaced = !!(extra & 0x40);
1494
4.47k
            data_size -= 24;
1495
4.47k
            bytestream2_skip(gb, data_size + (data_size & 1));
1496
40.7k
        } else if (chunk_id == MKTAG('D', 'L', 'T', 'A') ||
1497
21.1k
                    chunk_id == MKTAG('B', 'O', 'D', 'Y')) {
1498
21.1k
            if (chunk_id == MKTAG('B','O','D','Y'))
1499
664
                s->compression &= 0xFF;
1500
21.1k
            break;
1501
21.1k
        } else if (chunk_id == MKTAG('C', 'M', 'A', 'P')) {
1502
1.93k
            int count = data_size / 3;
1503
1.93k
            uint32_t *pal = s->pal;
1504
1505
1.93k
            if (count > 256)
1506
314
                return AVERROR_INVALIDDATA;
1507
1.62k
            if (s->ham) {
1508
11.4k
                for (int i = 0; i < count; i++)
1509
10.9k
                    pal[i] = 0xFF000000 | bytestream2_get_le24(gb);
1510
1.15k
            } else {
1511
34.5k
                for (int i = 0; i < count; i++)
1512
33.4k
                    pal[i] = 0xFF000000 | bytestream2_get_be24(gb);
1513
1.15k
            }
1514
1.62k
            bytestream2_skip(gb, data_size & 1);
1515
17.5k
        } else {
1516
17.5k
            bytestream2_skip(gb, data_size + (data_size&1));
1517
17.5k
        }
1518
45.9k
    }
1519
1520
114k
    return 0;
1521
115k
}
1522
1523
static int decode_frame(AVCodecContext *avctx, AVFrame *frame,
1524
                        int *got_frame, AVPacket *avpkt)
1525
165k
{
1526
165k
    IffContext *s          = avctx->priv_data;
1527
165k
    const uint8_t *buf     = avpkt->data;
1528
165k
    int buf_size           = avpkt->size;
1529
165k
    const uint8_t *buf_end = buf + buf_size;
1530
165k
    int y, plane, res;
1531
165k
    GetByteContext gb0, *const gb = &gb0;
1532
165k
    const AVPixFmtDescriptor *desc;
1533
1534
165k
    bytestream2_init(gb, avpkt->data, avpkt->size);
1535
1536
165k
    if ((res = parse_packet_header(avctx, gb)) < 0)
1537
569
        return res;
1538
1539
165k
    if ((res = ff_get_buffer(avctx, frame, 0)) < 0)
1540
203
        return res;
1541
1542
165k
    buf      += bytestream2_tell(gb);
1543
165k
    buf_size -= bytestream2_tell(gb);
1544
165k
    desc = av_pix_fmt_desc_get(avctx->pix_fmt);
1545
1546
165k
    if (!s->init && avctx->bits_per_coded_sample <= 8 - (s->masking == MASK_HAS_MASK) &&
1547
3.12k
        avctx->pix_fmt == AV_PIX_FMT_PAL8) {
1548
1.97k
        if ((res = cmap_read_palette(avctx, (uint32_t *)frame->data[1])) < 0)
1549
438
            return res;
1550
163k
    } else if (!s->init && avctx->bits_per_coded_sample <= 8 &&
1551
1.16k
               avctx->pix_fmt == AV_PIX_FMT_RGB32) {
1552
1.02k
        if ((res = cmap_read_palette(avctx, s->mask_palbuf)) < 0)
1553
792
            return res;
1554
1.02k
    }
1555
163k
    s->init = 1;
1556
1557
163k
    if (s->compression <= 0xff && (avctx->codec_tag == MKTAG('A', 'N', 'I', 'M'))) {
1558
65.4k
        if (avctx->pix_fmt == AV_PIX_FMT_PAL8)
1559
4.84k
            memcpy(s->pal, frame->data[1], 256 * 4);
1560
65.4k
    }
1561
1562
163k
    switch (s->compression) {
1563
79.3k
    case 0x0:
1564
79.3k
        if (avctx->codec_tag == MKTAG('A', 'C', 'B', 'M')) {
1565
3.56k
            if (avctx->pix_fmt == AV_PIX_FMT_PAL8 || avctx->pix_fmt == AV_PIX_FMT_GRAY8) {
1566
2.74k
                memset(frame->data[0], 0, avctx->height * frame->linesize[0]);
1567
20.4k
                for (plane = 0; plane < s->bpp; plane++) {
1568
201k
                    for (y = 0; y < avctx->height && buf < buf_end; y++) {
1569
183k
                        uint8_t *row = &frame->data[0][y * frame->linesize[0]];
1570
183k
                        decodeplane8(row, buf, FFMIN(s->planesize, buf_end - buf), plane);
1571
183k
                        buf += s->planesize;
1572
183k
                    }
1573
17.6k
                }
1574
2.74k
            } else if (s->ham) { // HAM to AV_PIX_FMT_BGR32
1575
514
                memset(frame->data[0], 0, avctx->height * frame->linesize[0]);
1576
29.2k
                for (y = 0; y < avctx->height; y++) {
1577
28.7k
                    uint8_t *row = &frame->data[0][y * frame->linesize[0]];
1578
28.7k
                    memset(s->ham_buf, 0, s->planesize * 8);
1579
33.2k
                    for (plane = 0; plane < s->bpp; plane++) {
1580
31.5k
                        const uint8_t * start = buf + (plane * avctx->height + y) * s->planesize;
1581
31.5k
                        if (start >= buf_end)
1582
26.9k
                            break;
1583
4.52k
                        decodeplane8(s->ham_buf, start, FFMIN(s->planesize, buf_end - start), plane);
1584
4.52k
                    }
1585
28.7k
                    decode_ham_plane32((uint32_t *)row, s->ham_buf, s->ham_palbuf, s->planesize);
1586
28.7k
                }
1587
514
            } else
1588
307
                return unsupported(avctx);
1589
75.7k
        } else if (avctx->codec_tag == MKTAG('D', 'E', 'E', 'P')) {
1590
947
            int raw_width = avctx->width * (av_get_bits_per_pixel(desc) >> 3);
1591
947
            int x;
1592
24.7k
            for (y = 0; y < avctx->height && buf < buf_end; y++) {
1593
23.8k
                uint8_t *row = &frame->data[0][y * frame->linesize[0]];
1594
23.8k
                memcpy(row, buf, FFMIN(raw_width, buf_end - buf));
1595
23.8k
                buf += raw_width;
1596
23.8k
                if (avctx->pix_fmt == AV_PIX_FMT_BGR32) {
1597
0
                    for (x = 0; x < avctx->width; x++)
1598
0
                        row[4 * x + 3] = row[4 * x + 3] & 0xF0 | (row[4 * x + 3] >> 4);
1599
0
                }
1600
23.8k
            }
1601
74.8k
        } else if (avctx->codec_tag == MKTAG('I', 'L', 'B', 'M') || // interleaved
1602
73.1k
                   avctx->codec_tag == MKTAG('A', 'N', 'I', 'M')) {
1603
64.2k
            if (avctx->codec_tag == MKTAG('A', 'N', 'I', 'M'))
1604
62.6k
                memcpy(s->video[0], buf, FFMIN(buf_end - buf, s->video_size));
1605
64.2k
            if (avctx->pix_fmt == AV_PIX_FMT_PAL8 || avctx->pix_fmt == AV_PIX_FMT_GRAY8) {
1606
5.02M
                for (y = 0; y < avctx->height; y++) {
1607
5.01M
                    uint8_t *row = &frame->data[0][y * frame->linesize[0]];
1608
5.01M
                    memset(row, 0, avctx->width);
1609
5.25M
                    for (plane = 0; plane < s->bpp && buf < buf_end; plane++) {
1610
234k
                        decodeplane8(row, buf, FFMIN(s->planesize, buf_end - buf), plane);
1611
234k
                        buf += s->planesize;
1612
234k
                    }
1613
5.01M
                }
1614
59.2k
            } else if (s->ham) { // HAM to AV_PIX_FMT_BGR32
1615
2.61M
                for (y = 0; y < avctx->height; y++) {
1616
2.61M
                    uint8_t *row = &frame->data[0][y * frame->linesize[0]];
1617
2.61M
                    memset(s->ham_buf, 0, s->planesize * 8);
1618
2.61M
                    for (plane = 0; plane < s->bpp && buf < buf_end; plane++) {
1619
3.92k
                        decodeplane8(s->ham_buf, buf, FFMIN(s->planesize, buf_end - buf), plane);
1620
3.92k
                        buf += s->planesize;
1621
3.92k
                    }
1622
2.61M
                    decode_ham_plane32((uint32_t *)row, s->ham_buf, s->ham_palbuf, s->planesize);
1623
2.61M
                }
1624
58.5k
            } else { // AV_PIX_FMT_BGR32
1625
12.2M
                for (y = 0; y < avctx->height; y++) {
1626
12.1M
                    uint8_t *row = &frame->data[0][y * frame->linesize[0]];
1627
12.1M
                    memset(row, 0, avctx->width << 2);
1628
12.2M
                    for (plane = 0; plane < s->bpp && buf < buf_end; plane++) {
1629
25.3k
                        decodeplane32((uint32_t *)row, buf,
1630
25.3k
                                      FFMIN(s->planesize, buf_end - buf), plane);
1631
25.3k
                        buf += s->planesize;
1632
25.3k
                    }
1633
12.1M
                }
1634
58.5k
            }
1635
64.2k
        } else if (avctx->codec_tag == MKTAG('P', 'B', 'M', ' ')) { // IFF-PBM
1636
3.62k
            if (avctx->pix_fmt == AV_PIX_FMT_PAL8 || avctx->pix_fmt == AV_PIX_FMT_GRAY8) {
1637
9.93k
                for (y = 0; y < avctx->height && buf_end > buf; y++) {
1638
8.94k
                    uint8_t *row = &frame->data[0][y * frame->linesize[0]];
1639
8.94k
                    memcpy(row, buf, FFMIN(avctx->width, buf_end - buf));
1640
8.94k
                    buf += avctx->width + (avctx->width % 2); // padding if odd
1641
8.94k
                }
1642
2.62k
            } else if (s->ham) { // IFF-PBM: HAM to AV_PIX_FMT_BGR32
1643
6.03k
                for (y = 0; y < avctx->height && buf_end > buf; y++) {
1644
3.61k
                    uint8_t *row = &frame->data[0][y * frame->linesize[0]];
1645
3.61k
                    memcpy(s->ham_buf, buf, FFMIN(avctx->width, buf_end - buf));
1646
3.61k
                    buf += avctx->width + (avctx->width & 1); // padding if odd
1647
3.61k
                    decode_ham_plane32((uint32_t *)row, s->ham_buf, s->ham_palbuf, s->planesize);
1648
3.61k
                }
1649
2.41k
            } else
1650
211
                return unsupported(avctx);
1651
6.91k
        } else {
1652
6.91k
            return unsupported(avctx);
1653
6.91k
        }
1654
71.9k
        break;
1655
71.9k
    case 0x1:
1656
25.1k
        if (avctx->codec_tag == MKTAG('I', 'L', 'B', 'M') || // interleaved
1657
15.7k
            avctx->codec_tag == MKTAG('A', 'N', 'I', 'M')) {
1658
15.7k
            if (avctx->pix_fmt == AV_PIX_FMT_PAL8 || avctx->pix_fmt == AV_PIX_FMT_GRAY8) {
1659
5.55k
                uint8_t *video = s->video[0];
1660
1661
2.21M
                for (y = 0; y < avctx->height; y++) {
1662
2.21M
                    uint8_t *row = &frame->data[0][y * frame->linesize[0]];
1663
2.21M
                    memset(row, 0, avctx->width);
1664
8.81M
                    for (plane = 0; plane < s->bpp; plane++) {
1665
6.60M
                        decode_byterun(s->planebuf, s->planesize, gb);
1666
6.60M
                        if (avctx->codec_tag == MKTAG('A', 'N', 'I', 'M')) {
1667
3.29M
                            memcpy(video, s->planebuf, s->planesize);
1668
3.29M
                            video += s->planesize;
1669
3.29M
                        }
1670
6.60M
                        decodeplane8(row, s->planebuf, s->planesize, plane);
1671
6.60M
                    }
1672
2.21M
                }
1673
10.1k
            } else if (avctx->bits_per_coded_sample <= 8) { //8-bit (+ mask) to AV_PIX_FMT_BGR32
1674
8.60M
                for (y = 0; y < avctx->height; y++) {
1675
8.60M
                    uint8_t *row = &frame->data[0][y * frame->linesize[0]];
1676
8.60M
                    memset(s->mask_buf, 0, avctx->width * sizeof(uint32_t));
1677
130M
                    for (plane = 0; plane < s->bpp; plane++) {
1678
122M
                        decode_byterun(s->planebuf, s->planesize, gb);
1679
122M
                        decodeplane32(s->mask_buf, s->planebuf, s->planesize, plane);
1680
122M
                    }
1681
8.60M
                    lookup_pal_indicies((uint32_t *)row, s->mask_buf, s->mask_palbuf, avctx->width);
1682
8.60M
                }
1683
5.93k
            } else if (s->ham) { // HAM to AV_PIX_FMT_BGR32
1684
4.30k
                uint8_t *video = s->video[0];
1685
3.98M
                for (y = 0; y < avctx->height; y++) {
1686
3.97M
                    uint8_t *row = &frame->data[0][y * frame->linesize[0]];
1687
3.97M
                    memset(s->ham_buf, 0, s->planesize * 8);
1688
12.8M
                    for (plane = 0; plane < s->bpp; plane++) {
1689
8.86M
                        decode_byterun(s->planebuf, s->planesize, gb);
1690
8.86M
                        if (avctx->codec_tag == MKTAG('A', 'N', 'I', 'M')) {
1691
8.13M
                            memcpy(video, s->planebuf, s->planesize);
1692
8.13M
                            video += s->planesize;
1693
8.13M
                        }
1694
8.86M
                        decodeplane8(s->ham_buf, s->planebuf, s->planesize, plane);
1695
8.86M
                    }
1696
3.97M
                    decode_ham_plane32((uint32_t *)row, s->ham_buf, s->ham_palbuf, s->planesize);
1697
3.97M
                }
1698
4.30k
            } else { // AV_PIX_FMT_BGR32
1699
952k
                for (y = 0; y < avctx->height; y++) {
1700
950k
                    uint8_t *row = &frame->data[0][y * frame->linesize[0]];
1701
950k
                    memset(row, 0, avctx->width << 2);
1702
5.28M
                    for (plane = 0; plane < s->bpp; plane++) {
1703
4.33M
                        decode_byterun(s->planebuf, s->planesize, gb);
1704
4.33M
                        decodeplane32((uint32_t *)row, s->planebuf, s->planesize, plane);
1705
4.33M
                    }
1706
950k
                }
1707
1.63k
            }
1708
15.7k
        } else if (avctx->codec_tag == MKTAG('P', 'B', 'M', ' ')) { // IFF-PBM
1709
3.73k
            if (avctx->pix_fmt == AV_PIX_FMT_PAL8 || avctx->pix_fmt == AV_PIX_FMT_GRAY8) {
1710
2.86M
                for (y = 0; y < avctx->height; y++) {
1711
2.85M
                    uint8_t *row = &frame->data[0][y * frame->linesize[0]];
1712
2.85M
                    decode_byterun(row, avctx->width, gb);
1713
2.85M
                }
1714
2.03k
            } else if (s->ham) { // IFF-PBM: HAM to AV_PIX_FMT_BGR32
1715
1.71M
                for (y = 0; y < avctx->height; y++) {
1716
1.71M
                    uint8_t *row = &frame->data[0][y * frame->linesize[0]];
1717
1.71M
                    decode_byterun(s->ham_buf, avctx->width, gb);
1718
1.71M
                    decode_ham_plane32((uint32_t *)row, s->ham_buf, s->ham_palbuf, s->planesize);
1719
1.71M
                }
1720
1.47k
            } else
1721
223
                return unsupported(avctx);
1722
5.68k
        } else if (avctx->codec_tag == MKTAG('D', 'E', 'E', 'P')) { // IFF-DEEP
1723
1.70k
            if (av_get_bits_per_pixel(desc) == 32)
1724
1.48k
                decode_deep_rle32(frame->data[0], buf, buf_size, avctx->width, avctx->height, frame->linesize[0]);
1725
219
            else
1726
219
                return unsupported(avctx);
1727
3.98k
        } else if (avctx->codec_tag == MKTAG('A', 'C', 'B', 'M')) {
1728
2.27k
            if (avctx->pix_fmt == AV_PIX_FMT_PAL8 || avctx->pix_fmt == AV_PIX_FMT_GRAY8) {
1729
634
                memset(frame->data[0], 0, avctx->height * frame->linesize[0]);
1730
6.61k
                for (plane = 0; plane < s->bpp; plane++) {
1731
9.06k
                    for (y = 0; y < avctx->height && buf < buf_end; y++) {
1732
3.08k
                        uint8_t *row = &frame->data[0][y * frame->linesize[0]];
1733
3.08k
                        decodeplane8(row, buf, FFMIN(s->planesize, buf_end - buf), plane);
1734
3.08k
                        buf += s->planesize;
1735
3.08k
                    }
1736
5.98k
                }
1737
1.64k
            } else if (s->ham) { // HAM to AV_PIX_FMT_BGR32
1738
1.43k
                memset(frame->data[0], 0, avctx->height * frame->linesize[0]);
1739
171k
                for (y = 0; y < avctx->height; y++) {
1740
170k
                    uint8_t *row = &frame->data[0][y * frame->linesize[0]];
1741
170k
                    memset(s->ham_buf, 0, s->planesize * 8);
1742
176k
                    for (plane = 0; plane < s->bpp; plane++) {
1743
174k
                        const uint8_t * start = buf + (plane * avctx->height + y) * s->planesize;
1744
174k
                        if (start >= buf_end)
1745
168k
                            break;
1746
6.00k
                        decodeplane8(s->ham_buf, start, FFMIN(s->planesize, buf_end - start), plane);
1747
6.00k
                    }
1748
170k
                    decode_ham_plane32((uint32_t *)row, s->ham_buf, s->ham_palbuf, s->planesize);
1749
170k
                }
1750
1.43k
            } else {
1751
208
                return unsupported(avctx);
1752
208
            }
1753
2.27k
        } else {
1754
1.70k
            return unsupported(avctx);
1755
1.70k
        }
1756
22.8k
        break;
1757
22.8k
    case 0x2:
1758
3.06k
        if (avctx->codec_tag == MKTAG('I', 'L', 'B', 'M') && avctx->pix_fmt == AV_PIX_FMT_PAL8) {
1759
17.5k
            for (plane = 0; plane < s->bpp; plane++) {
1760
15.4k
                decode_byterun2(s->planebuf, avctx->height, s->planesize, gb);
1761
35.2M
                for (y = 0; y < avctx->height; y++) {
1762
35.2M
                    uint8_t *row = &frame->data[0][y * frame->linesize[0]];
1763
35.2M
                    decodeplane8(row, s->planebuf + s->planesize * y, s->planesize, plane);
1764
35.2M
                }
1765
15.4k
            }
1766
2.15k
        } else {
1767
915
            return unsupported(avctx);
1768
915
        }
1769
2.15k
        break;
1770
4.30k
    case 0x4:
1771
4.30k
        if (avctx->codec_tag == MKTAG('R', 'G', 'B', '8') && avctx->pix_fmt == AV_PIX_FMT_RGB32)
1772
2.00k
            decode_rgb8(gb, frame->data[0], avctx->width, avctx->height, frame->linesize[0]);
1773
2.30k
        else if (avctx->codec_tag == MKTAG('R', 'G', 'B', 'N') && avctx->pix_fmt == AV_PIX_FMT_RGB444)
1774
1.16k
            decode_rgbn(gb, frame->data[0], avctx->width, avctx->height, frame->linesize[0]);
1775
1.14k
        else
1776
1.14k
            return unsupported(avctx);
1777
3.16k
        break;
1778
3.35k
    case 0x5:
1779
3.35k
        if (avctx->codec_tag == MKTAG('D', 'E', 'E', 'P')) {
1780
2.43k
            if (av_get_bits_per_pixel(desc) == 32)
1781
2.19k
                decode_deep_tvdc32(frame->data[0], buf, buf_size, avctx->width, avctx->height, frame->linesize[0], s->tvdc);
1782
233
            else
1783
233
                return unsupported(avctx);
1784
2.43k
        } else
1785
918
            return unsupported(avctx);
1786
2.19k
        break;
1787
8.03k
    case 0x300:
1788
8.25k
    case 0x301:
1789
8.25k
        decode_short_horizontal_delta(s->video[0], buf, buf_end, avctx->width, s->bpp, s->video_size);
1790
8.25k
        break;
1791
1.39k
    case 0x500:
1792
1.64k
    case 0x501:
1793
1.64k
        decode_byte_vertical_delta(s->video[0], buf, buf_end, avctx->width, s->is_brush, s->bpp, s->video_size);
1794
1.64k
        break;
1795
4.94k
    case 0x700:
1796
5.85k
    case 0x701:
1797
5.85k
        if (s->is_short)
1798
2.57k
            decode_short_vertical_delta(s->video[0], buf, buf_end, avctx->width, s->bpp, s->video_size);
1799
3.28k
        else
1800
3.28k
            decode_long_vertical_delta(s->video[0], buf, buf_end, avctx->width, s->bpp, s->video_size);
1801
5.85k
        break;
1802
12.3k
    case 0x800:
1803
13.1k
    case 0x801:
1804
13.1k
        if (s->is_short)
1805
3.76k
            decode_short_vertical_delta2(s->video[0], buf, buf_end, avctx->width, s->bpp, s->video_size);
1806
9.41k
        else
1807
9.41k
            decode_long_vertical_delta2(s->video[0], buf, buf_end, avctx->width, s->bpp, s->video_size);
1808
13.1k
        break;
1809
5.40k
    case 0x4a00:
1810
6.65k
    case 0x4a01:
1811
6.65k
        decode_delta_j(s->video[0], buf, buf_end, avctx->width, avctx->height, s->bpp, s->video_size);
1812
6.65k
        break;
1813
2.49k
    case 0x6400:
1814
2.86k
    case 0x6401:
1815
2.86k
        if (s->is_interlaced)
1816
253
            return unsupported(avctx);
1817
2.61k
        decode_delta_d(s->video[0], buf, buf_end, avctx->width, s->is_interlaced, s->bpp, s->video_size);
1818
2.61k
        break;
1819
2.06k
    case 0x6500:
1820
2.43k
    case 0x6501:
1821
2.43k
        if (s->is_interlaced)
1822
209
            return unsupported(avctx);
1823
2.22k
        decode_delta_e(s->video[0], buf, buf_end, avctx->width, s->is_interlaced, s->bpp, s->video_size);
1824
2.22k
        break;
1825
3.66k
    case 0x6c00:
1826
3.91k
    case 0x6c01:
1827
3.91k
        decode_delta_l(s->video[0], buf, buf_end, avctx->width, s->is_short, s->bpp, s->video_size);
1828
3.91k
        break;
1829
3.90k
    default:
1830
3.90k
        return unsupported(avctx);
1831
163k
    }
1832
1833
146k
    if (s->compression <= 0xff && (avctx->codec_tag == MKTAG('A', 'N', 'I', 'M'))) {
1834
64.7k
        memcpy(s->video[1], s->video[0], s->video_size);
1835
64.7k
    }
1836
1837
146k
    if (s->compression > 0xff) {
1838
44.3k
        if (avctx->pix_fmt == AV_PIX_FMT_PAL8 || avctx->pix_fmt == AV_PIX_FMT_GRAY8) {
1839
19.5k
            buf = s->video[0];
1840
16.5M
            for (y = 0; y < avctx->height; y++) {
1841
16.5M
                uint8_t *row = &frame->data[0][y * frame->linesize[0]];
1842
16.5M
                memset(row, 0, avctx->width);
1843
44.3M
                for (plane = 0; plane < s->bpp; plane++) {
1844
27.7M
                    decodeplane8(row, buf, s->planesize, plane);
1845
27.7M
                    buf += s->planesize;
1846
27.7M
                }
1847
16.5M
            }
1848
19.5k
            if (avctx->pix_fmt == AV_PIX_FMT_PAL8)
1849
18.6k
                memcpy(frame->data[1], s->pal, 256 * 4);
1850
24.8k
        } else if (s->ham) {
1851
4.17k
            int i, count = 1 << s->ham;
1852
1853
4.17k
            buf = s->video[0];
1854
4.17k
            memset(s->ham_palbuf, 0, (1 << s->ham) * 2 * sizeof(uint32_t));
1855
86.2k
            for (i = 0; i < count; i++) {
1856
82.0k
                s->ham_palbuf[i*2+1] = s->pal[i];
1857
82.0k
            }
1858
86.2k
            for (i = 0; i < count; i++) {
1859
82.0k
                uint32_t tmp = i << (8 - s->ham);
1860
82.0k
                tmp |= tmp >> s->ham;
1861
82.0k
                s->ham_palbuf[(i+count)*2]     = 0xFF00FFFF;
1862
82.0k
                s->ham_palbuf[(i+count*2)*2]   = 0xFFFFFF00;
1863
82.0k
                s->ham_palbuf[(i+count*3)*2]   = 0xFFFF00FF;
1864
82.0k
                s->ham_palbuf[(i+count)*2+1]   = 0xFF000000 | tmp << 16;
1865
82.0k
                s->ham_palbuf[(i+count*2)*2+1] = 0xFF000000 | tmp;
1866
82.0k
                s->ham_palbuf[(i+count*3)*2+1] = 0xFF000000 | tmp << 8;
1867
82.0k
            }
1868
4.17k
            if (s->masking == MASK_HAS_MASK) {
1869
631k
                for (i = 0; i < 8 * (1 << s->ham); i++)
1870
627k
                    s->ham_palbuf[(1 << s->bpp) + i] = s->ham_palbuf[i] | 0xFF000000;
1871
3.97k
            }
1872
5.18M
            for (y = 0; y < avctx->height; y++) {
1873
5.18M
                uint8_t *row = &frame->data[0][y * frame->linesize[0]];
1874
5.18M
                memset(s->ham_buf, 0, s->planesize * 8);
1875
13.1M
                for (plane = 0; plane < s->bpp; plane++) {
1876
8.00M
                    decodeplane8(s->ham_buf, buf, s->planesize, plane);
1877
8.00M
                    buf += s->planesize;
1878
8.00M
                }
1879
5.18M
                decode_ham_plane32((uint32_t *)row, s->ham_buf, s->ham_palbuf, s->planesize);
1880
5.18M
            }
1881
20.6k
        } else {
1882
20.6k
            return unsupported(avctx);
1883
20.6k
        }
1884
1885
23.7k
        if (!s->is_brush) {
1886
23.1k
            FFSWAP(uint8_t *, s->video[0], s->video[1]);
1887
23.1k
        }
1888
23.7k
    }
1889
1890
125k
    if (avpkt->flags & AV_PKT_FLAG_KEY) {
1891
73.8k
        frame->flags |= AV_FRAME_FLAG_KEY;
1892
73.8k
        frame->pict_type = AV_PICTURE_TYPE_I;
1893
73.8k
    } else {
1894
52.1k
        frame->flags &= ~AV_FRAME_FLAG_KEY;
1895
52.1k
        frame->pict_type = AV_PICTURE_TYPE_P;
1896
52.1k
    }
1897
1898
125k
    *got_frame = 1;
1899
1900
125k
    return buf_size;
1901
146k
}
1902
1903
const FFCodec ff_iff_ilbm_decoder = {
1904
    .p.name         = "iff",
1905
    CODEC_LONG_NAME("IFF ACBM/ANIM/DEEP/ILBM/PBM/RGB8/RGBN"),
1906
    .p.type         = AVMEDIA_TYPE_VIDEO,
1907
    .p.id           = AV_CODEC_ID_IFF_ILBM,
1908
    .priv_data_size = sizeof(IffContext),
1909
    .init           = decode_init,
1910
    .close          = decode_end,
1911
    FF_CODEC_DECODE_CB(decode_frame),
1912
    .p.capabilities = AV_CODEC_CAP_DR1,
1913
    .caps_internal  = FF_CODEC_CAP_INIT_CLEANUP,
1914
};