Browse code

avcodec: add ScreenPressor decoder

Signed-off-by: Paul B Mahol <onemda@gmail.com>

Paul B Mahol authored on 2017/02/16 23:21:29
Showing 9 changed files
... ...
@@ -24,6 +24,7 @@ version <next>:
24 24
 - Optimal Huffman tables for (M)JPEG encoding
25 25
 - FM Screen Capture Codec decoder
26 26
 - native Opus encoder
27
+- ScreenPressor decoder
27 28
 
28 29
 version 3.2:
29 30
 - libopenmpt demuxer
... ...
@@ -830,6 +830,7 @@ following image formats are supported:
830 830
     @tab Texture dictionaries used by the Renderware Engine.
831 831
 @item RL2 video              @tab     @tab  X
832 832
     @tab used in some games by Entertainment Software Partners
833
+@item ScreenPressor          @tab     @tab  X
833 834
 @item Screenpresso           @tab     @tab  X
834 835
 @item Sierra VMD video       @tab     @tab  X
835 836
     @tab Used in Sierra VMD files.
... ...
@@ -509,6 +509,7 @@ OBJS-$(CONFIG_SAMI_DECODER)            += samidec.o ass.o htmlsubtitles.o
509 509
 OBJS-$(CONFIG_S302M_DECODER)           += s302m.o
510 510
 OBJS-$(CONFIG_S302M_ENCODER)           += s302menc.o
511 511
 OBJS-$(CONFIG_SANM_DECODER)            += sanm.o
512
+OBJS-$(CONFIG_SCPR_DECODER)            += scpr.o
512 513
 OBJS-$(CONFIG_SCREENPRESSO_DECODER)    += screenpresso.o
513 514
 OBJS-$(CONFIG_SDX2_DPCM_DECODER)       += dpcm.o
514 515
 OBJS-$(CONFIG_SGI_DECODER)             += sgidec.o
... ...
@@ -307,6 +307,7 @@ void avcodec_register_all(void)
307 307
     REGISTER_DECODER(RV40,              rv40);
308 308
     REGISTER_ENCDEC (S302M,             s302m);
309 309
     REGISTER_DECODER(SANM,              sanm);
310
+    REGISTER_DECODER(SCPR,              scpr);
310 311
     REGISTER_DECODER(SCREENPRESSO,      screenpresso);
311 312
     REGISTER_DECODER(SDX2_DPCM,         sdx2_dpcm);
312 313
     REGISTER_ENCDEC (SGI,               sgi);
... ...
@@ -415,6 +415,7 @@ enum AVCodecID {
415 415
     AV_CODEC_ID_PIXLET,
416 416
     AV_CODEC_ID_SPEEDHQ,
417 417
     AV_CODEC_ID_FMVC,
418
+    AV_CODEC_ID_SCPR,
418 419
 
419 420
     /* various PCM "codecs" */
420 421
     AV_CODEC_ID_FIRST_AUDIO = 0x10000,     ///< A dummy id pointing at the start of audio codecs
... ...
@@ -1360,6 +1360,13 @@ static const AVCodecDescriptor codec_descriptors[] = {
1360 1360
         .long_name = NULL_IF_CONFIG_SMALL("FM Screen Capture Codec"),
1361 1361
         .props     = AV_CODEC_PROP_LOSSLESS,
1362 1362
     },
1363
+    {
1364
+        .id        = AV_CODEC_ID_SCPR,
1365
+        .type      = AVMEDIA_TYPE_VIDEO,
1366
+        .name      = "scpr",
1367
+        .long_name = NULL_IF_CONFIG_SMALL("ScreenPressor"),
1368
+        .props     = AV_CODEC_PROP_LOSSLESS | AV_CODEC_PROP_LOSSY,
1369
+    },
1363 1370
 
1364 1371
     /* image codecs */
1365 1372
     {
1366 1373
new file mode 100644
... ...
@@ -0,0 +1,813 @@
0
+/*
1
+ * ScreenPressor decoder
2
+ *
3
+ * Copyright (c) 2017 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 <stdio.h>
23
+#include <stdlib.h>
24
+#include <string.h>
25
+
26
+#include "avcodec.h"
27
+#include "bytestream.h"
28
+#include "internal.h"
29
+
30
+#define TOP  0x01000000
31
+#define BOT    0x010000
32
+
33
+typedef struct RangeCoder {
34
+    unsigned   code;
35
+    unsigned   range;
36
+} RangeCoder;
37
+
38
+typedef struct PixelModel {
39
+    unsigned    freq[256];
40
+    unsigned    lookup[16];
41
+    unsigned    total_freq;
42
+} PixelModel;
43
+
44
+typedef struct SCPRContext {
45
+    AVFrame        *last_frame;
46
+    AVFrame        *current_frame;
47
+    GetByteContext  gb;
48
+    RangeCoder      rc;
49
+    PixelModel      pixel_model[3][4096];
50
+    unsigned        op_model[6][7];
51
+    unsigned        run_model[6][257];
52
+    unsigned        range_model[257];
53
+    unsigned        count_model[257];
54
+    unsigned        fill_model[6];
55
+    unsigned        sxy_model[4][17];
56
+    unsigned        mv_model[2][513];
57
+    unsigned        nbx, nby;
58
+    unsigned        nbcount;
59
+    unsigned       *blocks;
60
+    int             cxshift;
61
+} SCPRContext;
62
+
63
+static void init_rangecoder(RangeCoder *rc, GetByteContext *gb)
64
+{
65
+    rc->range = 0xFFFFFFFFU;
66
+    rc->code  = bytestream2_get_be32(gb);
67
+}
68
+
69
+static void reinit_tables(SCPRContext *s)
70
+{
71
+    int comp, i, j;
72
+
73
+    for (comp = 0; comp < 3; comp++) {
74
+        for (j = 0; j < 4096; j++) {
75
+            if (s->pixel_model[comp][j].total_freq != 256) {
76
+                for (i = 0; i < 256; i++)
77
+                    s->pixel_model[comp][j].freq[i] = 1;
78
+                for (i = 0; i < 16; i++)
79
+                    s->pixel_model[comp][j].lookup[i] = 16;
80
+                s->pixel_model[comp][j].total_freq = 256;
81
+            }
82
+        }
83
+    }
84
+
85
+    for (j = 0; j < 6; j++) {
86
+        unsigned *p = s->run_model[j];
87
+        for (i = 0; i < 256; i++)
88
+            p[i] = 1;
89
+        p[256] = 256;
90
+    }
91
+
92
+    for (j = 0; j < 6; j++) {
93
+        unsigned *op = s->op_model[j];
94
+        for (i = 0; i < 6; i++)
95
+            op[i] = 1;
96
+        op[6] = 6;
97
+    }
98
+
99
+    for (i = 0; i < 256; i++) {
100
+        s->range_model[i] = 1;
101
+        s->count_model[i] = 1;
102
+    }
103
+    s->range_model[256] = 256;
104
+    s->count_model[256] = 256;
105
+
106
+    for (i = 0; i < 5; i++) {
107
+        s->fill_model[i] = 1;
108
+    }
109
+    s->fill_model[5] = 5;
110
+
111
+    for (j = 0; j < 4; j++) {
112
+        for (i = 0; i < 16; i++) {
113
+            s->sxy_model[j][i] = 1;
114
+        }
115
+        s->sxy_model[j][16] = 16;
116
+    }
117
+
118
+    for (i = 0; i < 512; i++) {
119
+        s->mv_model[0][i] = 1;
120
+        s->mv_model[1][i] = 1;
121
+    }
122
+    s->mv_model[0][512] = 512;
123
+    s->mv_model[1][512] = 512;
124
+}
125
+
126
+static void decode(GetByteContext *gb, RangeCoder *rc, unsigned cumFreq, unsigned freq, unsigned total_freq)
127
+{
128
+    rc->code -= cumFreq * rc->range;
129
+    rc->range *= freq;
130
+
131
+    while (rc->range < TOP && bytestream2_get_bytes_left(gb) > 0) {
132
+        unsigned byte = bytestream2_get_byte(gb);
133
+        rc->code = (rc->code << 8) | byte;
134
+        rc->range <<= 8;
135
+    }
136
+}
137
+
138
+static int get_freq(RangeCoder *rc, unsigned total_freq, unsigned *freq)
139
+{
140
+    if (total_freq == 0)
141
+        return AVERROR_INVALIDDATA;
142
+
143
+    rc->range = rc->range / total_freq;
144
+
145
+    if (rc->range == 0)
146
+        return AVERROR_INVALIDDATA;
147
+
148
+    *freq = rc->code / rc->range;
149
+
150
+    return 0;
151
+}
152
+
153
+static int decode_value(SCPRContext *s, unsigned *cnt, unsigned maxc, unsigned step, unsigned *rval)
154
+{
155
+    GetByteContext *gb = &s->gb;
156
+    RangeCoder *rc = &s->rc;
157
+    unsigned totfr = cnt[maxc];
158
+    unsigned value;
159
+    unsigned c = 0, cumfr = 0, cnt_c = 0;
160
+    int i, ret;
161
+
162
+    if ((ret = get_freq(rc, totfr, &value)) < 0)
163
+        return ret;
164
+
165
+    while (c < maxc) {
166
+        cnt_c = cnt[c];
167
+        if (value >= cumfr + cnt_c)
168
+            cumfr += cnt_c;
169
+        else
170
+            break;
171
+        c++;
172
+    }
173
+    decode(gb, rc, cumfr, cnt_c, totfr);
174
+
175
+    cnt[c] = cnt_c + step;
176
+    totfr += step;
177
+    if (totfr > BOT) {
178
+        totfr = 0;
179
+        for (i = 0; i < maxc; i++) {
180
+            unsigned nc = (cnt[i] >> 1) + 1;
181
+            cnt[i] = nc;
182
+            totfr += nc;
183
+        }
184
+    }
185
+
186
+    cnt[maxc] = totfr;
187
+    *rval = c;
188
+
189
+    return 0;
190
+}
191
+
192
+static int decode_unit(SCPRContext *s, PixelModel *pixel, unsigned step, unsigned *rval)
193
+{
194
+    GetByteContext *gb = &s->gb;
195
+    RangeCoder *rc = &s->rc;
196
+    unsigned totfr = pixel->total_freq;
197
+    unsigned value, x = 0, cumfr = 0, cnt_x = 0;
198
+    int i, j, ret, c, cnt_c;
199
+
200
+    if ((ret = get_freq(rc, totfr, &value)) < 0)
201
+        return ret;
202
+
203
+    while (x < 16) {
204
+        cnt_x = pixel->lookup[x];
205
+        if (value >= cumfr + cnt_x)
206
+            cumfr += cnt_x;
207
+        else
208
+            break;
209
+        x++;
210
+    }
211
+
212
+    c = x * 16;
213
+    cnt_c = 0;
214
+    while (c < 256) {
215
+        cnt_c = pixel->freq[c];
216
+        if (value >= cumfr + cnt_c)
217
+            cumfr += cnt_c;
218
+        else
219
+            break;
220
+        c++;
221
+    }
222
+    decode(gb, rc, cumfr, cnt_c, totfr);
223
+    pixel->freq[c] = cnt_c + step;
224
+    pixel->lookup[x] = cnt_x + step;
225
+    totfr += step;
226
+    if (totfr > BOT) {
227
+        totfr = 0;
228
+        for (i = 0; i < 256; i++) {
229
+            unsigned nc = (pixel->freq[i] >> 1) + 1;
230
+            pixel->freq[i] = nc;
231
+            totfr += nc;
232
+        }
233
+        for (i = 0; i < 16; i++) {
234
+            unsigned sum = 0;
235
+            unsigned i16_17 = i << 4;
236
+            for (j = 0; j < 16; j++)
237
+                sum += pixel->freq[i16_17 + j];
238
+            pixel->lookup[i] = sum;
239
+        }
240
+    }
241
+    pixel->total_freq = totfr;
242
+
243
+    *rval = c;
244
+
245
+    return 0;
246
+}
247
+
248
+static int decompress_i(AVCodecContext *avctx, uint32_t *dst, int linesize)
249
+{
250
+    SCPRContext *s = avctx->priv_data;
251
+    GetByteContext *gb = &s->gb;
252
+    int cx = 0, cx1 = 0, k = 0, clr = 0;
253
+    int run, r, g, b, off, y = 0, x = 0, ret;
254
+    const int cxshift = s->cxshift;
255
+    unsigned lx, ly, ptype;
256
+
257
+    reinit_tables(s);
258
+    bytestream2_skip(gb, 2);
259
+    init_rangecoder(&s->rc, gb);
260
+
261
+    while (k < avctx->width + 1) {
262
+        ret = decode_unit(s, &s->pixel_model[0][cx + cx1], 400, &r);
263
+        if (ret < 0)
264
+            return ret;
265
+
266
+        cx1 = (cx << 6) & 0xFC0;
267
+        cx = r >> cxshift;
268
+        ret = decode_unit(s, &s->pixel_model[1][cx + cx1], 400, &g);
269
+        if (ret < 0)
270
+            return ret;
271
+
272
+        cx1 = (cx << 6) & 0xFC0;
273
+        cx = g >> cxshift;
274
+        ret = decode_unit(s, &s->pixel_model[2][cx + cx1], 400, &b);
275
+        if (ret < 0)
276
+            return ret;
277
+
278
+        cx1 = (cx << 6) & 0xFC0;
279
+        cx = b >> cxshift;
280
+
281
+        ret = decode_value(s, s->run_model[0], 256, 400, &run);
282
+        if (ret < 0)
283
+            return ret;
284
+
285
+        clr = (b << 16) + (g << 8) + r;
286
+        k += run;
287
+        while (run-- > 0) {
288
+            dst[y * linesize + x] = clr;
289
+            lx = x;
290
+            ly = y;
291
+            x++;
292
+            if (x >= avctx->width) {
293
+                x = 0;
294
+                y++;
295
+            }
296
+        }
297
+    }
298
+    off = -linesize - 1;
299
+    ptype = 0;
300
+
301
+    while (x < avctx->width && y < avctx->height) {
302
+        ret = decode_value(s, s->op_model[ptype], 6, 1000, &ptype);
303
+        if (ret < 0)
304
+            return ret;
305
+        if (ptype == 0) {
306
+            ret = decode_unit(s, &s->pixel_model[0][cx + cx1], 400, &r);
307
+            if (ret < 0)
308
+                return ret;
309
+
310
+            cx1 = (cx << 6) & 0xFC0;
311
+            cx = r >> cxshift;
312
+            ret = decode_unit(s, &s->pixel_model[1][cx + cx1], 400, &g);
313
+            if (ret < 0)
314
+                return ret;
315
+
316
+            cx1 = (cx << 6) & 0xFC0;
317
+            cx = g >> cxshift;
318
+            ret = decode_unit(s, &s->pixel_model[2][cx + cx1], 400, &b);
319
+            if (ret < 0)
320
+                return ret;
321
+
322
+            cx1 = (cx << 6) & 0xFC0;
323
+            cx = b >> cxshift;
324
+            clr = (b << 16) + (g << 8) + r;
325
+        }
326
+        if (ptype > 5)
327
+            return AVERROR_INVALIDDATA;
328
+        ret = decode_value(s, s->run_model[ptype], 256, 400, &run);
329
+        if (ret < 0)
330
+            return ret;
331
+
332
+        switch (ptype) {
333
+        case 0:
334
+            while (run-- > 0) {
335
+                dst[y * linesize + x] = clr;
336
+                lx = x;
337
+                ly = y;
338
+                x++;
339
+                if (x >= avctx->width) {
340
+                    x = 0;
341
+                    y++;
342
+                }
343
+            }
344
+            break;
345
+        case 1:
346
+            while (run-- > 0) {
347
+                dst[y * linesize + x] = dst[ly * linesize + lx];
348
+                lx = x;
349
+                ly = y;
350
+                x++;
351
+                if (x >= avctx->width) {
352
+                    x = 0;
353
+                    y++;
354
+                }
355
+            }
356
+            clr = dst[ly * linesize + lx];
357
+            break;
358
+        case 2:
359
+            while (run-- > 0) {
360
+                clr = dst[y * linesize + x + off + 1];
361
+                dst[y * linesize + x] = clr;
362
+                lx = x;
363
+                ly = y;
364
+                x++;
365
+                if (x >= avctx->width) {
366
+                    x = 0;
367
+                    y++;
368
+                }
369
+            }
370
+            break;
371
+        case 4:
372
+            while (run-- > 0) {
373
+                uint8_t *odst = (uint8_t *)dst;
374
+                r = odst[(ly * linesize + lx) * 4] +
375
+                    odst[((y * linesize + x) + off) * 4 + 4] -
376
+                    odst[((y * linesize + x) + off) * 4];
377
+                g = odst[(ly * linesize + lx) * 4 + 1] +
378
+                    odst[((y * linesize + x) + off) * 4 + 5] -
379
+                    odst[((y * linesize + x) + off) * 4 + 1];
380
+                b = odst[(ly * linesize + lx) * 4 + 2] +
381
+                    odst[((y * linesize + x) + off) * 4 + 6] -
382
+                    odst[((y * linesize + x) + off) * 4 + 2];
383
+                clr = ((b & 0xFF) << 16) + ((g & 0xFF) << 8) + (r & 0xFF);
384
+                dst[y * linesize + x] = clr;
385
+                lx = x;
386
+                ly = y;
387
+                x++;
388
+                if (x >= avctx->width) {
389
+                    x = 0;
390
+                    y++;
391
+                }
392
+            }
393
+            break;
394
+        case 5:
395
+            while (run-- > 0) {
396
+                clr = dst[y * linesize + x + off];
397
+                dst[y * linesize + x] = clr;
398
+                lx = x;
399
+                ly = y;
400
+                x++;
401
+                if (x >= avctx->width) {
402
+                    x = 0;
403
+                    y++;
404
+                }
405
+            }
406
+            break;
407
+        }
408
+
409
+        if (avctx->bits_per_coded_sample == 16) {
410
+            cx1 = (clr & 0xFF00) >> 2;
411
+            cx = (clr & 0xFFFFFF) >> 16;
412
+        } else {
413
+            cx1 = (clr & 0xFC00) >> 4;
414
+            cx = (clr & 0xFFFFFF) >> 18;
415
+        }
416
+    }
417
+
418
+    return 0;
419
+}
420
+
421
+static int decompress_p(AVCodecContext *avctx,
422
+                        uint32_t *dst, int linesize,
423
+                        uint32_t *prev, int plinesize)
424
+{
425
+    SCPRContext *s = avctx->priv_data;
426
+    GetByteContext *gb = &s->gb;
427
+    int ret, temp, min, max, x, y, cx = 0, cx1 = 0;
428
+    int backstep = linesize - avctx->width;
429
+    const int cxshift = s->cxshift;
430
+
431
+    if (bytestream2_get_byte(gb) == 0)
432
+        return 0;
433
+    bytestream2_skip(gb, 1);
434
+    init_rangecoder(&s->rc, gb);
435
+
436
+    ret  = decode_value(s, s->range_model, 256, 1, &min);
437
+    ret |= decode_value(s, s->range_model, 256, 1, &temp);
438
+    min += temp << 8;
439
+    ret |= decode_value(s, s->range_model, 256, 1, &max);
440
+    ret |= decode_value(s, s->range_model, 256, 1, &temp);
441
+    if (ret < 0)
442
+        return ret;
443
+
444
+    max += temp << 8;
445
+    memset(s->blocks, 0, sizeof(*s->blocks) * s->nbcount);
446
+
447
+    while (min <= max) {
448
+        int fill, count;
449
+
450
+        ret  = decode_value(s, s->fill_model,  5,   10, &fill);
451
+        ret |= decode_value(s, s->count_model, 256, 20, &count);
452
+        if (ret < 0)
453
+            return ret;
454
+
455
+        while (min < s->nbcount && count-- > 0) {
456
+            s->blocks[min++] = fill;
457
+        }
458
+    }
459
+
460
+    for (y = 0; y < s->nby; y++) {
461
+        for (x = 0; x < s->nbx; x++) {
462
+            int sy1 = 0, sy2 = 16, sx1 = 0, sx2 = 16;
463
+
464
+            if (s->blocks[y * s->nbx + x] == 0)
465
+                continue;
466
+
467
+            if (((s->blocks[y * s->nbx + x] - 1) & 1) > 0) {
468
+                ret  = decode_value(s, s->sxy_model[0], 16, 100, &sx1);
469
+                ret |= decode_value(s, s->sxy_model[1], 16, 100, &sy1);
470
+                ret |= decode_value(s, s->sxy_model[2], 16, 100, &sx2);
471
+                ret |= decode_value(s, s->sxy_model[3], 16, 100, &sy2);
472
+                if (ret < 0)
473
+                    return ret;
474
+
475
+                sx2++;
476
+                sy2++;
477
+            }
478
+            if (((s->blocks[y * s->nbx + x] - 1) & 2) > 0) {
479
+                int i, j, by = y * 16, bx = x * 16;
480
+                int mvx, mvy;
481
+
482
+                ret  = decode_value(s, s->mv_model[0], 512, 100, &mvx);
483
+                ret |= decode_value(s, s->mv_model[1], 512, 100, &mvy);
484
+                if (ret < 0)
485
+                    return ret;
486
+
487
+                mvx -= 256;
488
+                mvy -= 256;
489
+
490
+                if (by + mvy + sy1 < 0 || bx + mvx + sx1 < 0)
491
+                    return AVERROR_INVALIDDATA;
492
+
493
+                for (i = 0; i < sy2 - sy1 && (by + sy1 + i) < avctx->height; i++) {
494
+                    for (j = 0; j < sx2 - sx1 && (bx + sx1 + j) < avctx->width; j++) {
495
+                        dst[(by + i + sy1) * linesize + bx + sx1 + j] = prev[(by + mvy + sy1 + i) * plinesize + bx + sx1 + mvx + j];
496
+                    }
497
+                }
498
+            } else {
499
+                int run, r, g, b, z, bx = x * 16 + sx1, by = y * 16 + sy1;
500
+                unsigned clr, ptype = 0;
501
+
502
+                for (; by < y * 16 + sy2 && by < avctx->height;) {
503
+                    ret = decode_value(s, s->op_model[ptype], 6, 1000, &ptype);
504
+                    if (ptype == 0) {
505
+                        ret = decode_unit(s, &s->pixel_model[0][cx + cx1], 400, &r);
506
+                        if (ret < 0)
507
+                            return ret;
508
+
509
+                        cx1 = (cx << 6) & 0xFC0;
510
+                        cx = r >> cxshift;
511
+                        ret = decode_unit(s, &s->pixel_model[1][cx + cx1], 400, &g);
512
+                        if (ret < 0)
513
+                            return ret;
514
+
515
+                        cx1 = (cx << 6) & 0xFC0;
516
+                        cx = g >> cxshift;
517
+                        ret = decode_unit(s, &s->pixel_model[2][cx + cx1], 400, &b);
518
+                        if (ret < 0)
519
+                            return ret;
520
+
521
+                        cx1 = (cx << 6) & 0xFC0;
522
+                        cx = b >> cxshift;
523
+                        clr = (b << 16) + (g << 8) + r;
524
+                    }
525
+                    if (ptype > 5)
526
+                        return AVERROR_INVALIDDATA;
527
+                    ret = decode_value(s, s->run_model[ptype], 256, 400, &run);
528
+                    if (ret < 0)
529
+                        return ret;
530
+
531
+                    switch (ptype) {
532
+                    case 0:
533
+                        while (run-- > 0) {
534
+                            if (by >= avctx->height)
535
+                                return AVERROR_INVALIDDATA;
536
+
537
+                            dst[by * linesize + bx] = clr;
538
+                            bx++;
539
+                            if (bx >= x * 16 + sx2 || bx >= avctx->width) {
540
+                                bx = x * 16 + sx1;
541
+                                by++;
542
+                            }
543
+                        }
544
+                        break;
545
+                    case 1:
546
+                        while (run-- > 0) {
547
+                            if (bx == 0) {
548
+                                if (by < 1)
549
+                                    return AVERROR_INVALIDDATA;
550
+                                z = backstep;
551
+                            } else {
552
+                                z = 0;
553
+                            }
554
+
555
+                            if (by >= avctx->height)
556
+                                return AVERROR_INVALIDDATA;
557
+
558
+                            clr = dst[by * linesize + bx - 1 - z];
559
+                            dst[by * linesize + bx] = clr;
560
+                            bx++;
561
+                            if (bx >= x * 16 + sx2 || bx >= avctx->width) {
562
+                                bx = x * 16 + sx1;
563
+                                by++;
564
+                            }
565
+                        }
566
+                        break;
567
+                    case 2:
568
+                        while (run-- > 0) {
569
+                            if (by < 1 || by >= avctx->height)
570
+                                return AVERROR_INVALIDDATA;
571
+
572
+                            clr = dst[(by - 1) * linesize + bx];
573
+                            dst[by * linesize + bx] = clr;
574
+                            bx++;
575
+                            if (bx >= x * 16 + sx2 || bx >= avctx->width) {
576
+                                bx = x * 16 + sx1;
577
+                                by++;
578
+                            }
579
+                        }
580
+                        break;
581
+                    case 3:
582
+                        while (run-- > 0) {
583
+                            if (by >= avctx->height)
584
+                                return AVERROR_INVALIDDATA;
585
+
586
+                            clr = prev[by * linesize + bx];
587
+                            dst[by * linesize + bx] = clr;
588
+                            bx++;
589
+                            if (bx >= x * 16 + sx2 || bx >= avctx->width) {
590
+                                bx = x * 16 + sx1;
591
+                                by++;
592
+                            }
593
+                        }
594
+                        break;
595
+                    case 4:
596
+                        while (run-- > 0) {
597
+                            uint8_t *odst = (uint8_t *)dst;
598
+
599
+                            if (by < 1 || by >= avctx->height)
600
+                                return AVERROR_INVALIDDATA;
601
+
602
+                            if (bx == 0) {
603
+                                z = backstep;
604
+                            } else {
605
+                                z = 0;
606
+                            }
607
+
608
+                            r = odst[((by - 1) * linesize + bx) * 4] +
609
+                                odst[(by * linesize + bx - 1 - z) * 4] -
610
+                                odst[((by - 1) * linesize + bx - 1 - z) * 4];
611
+                            g = odst[((by - 1) * linesize + bx) * 4 + 1] +
612
+                                odst[(by * linesize + bx - 1 - z) * 4 + 1] -
613
+                                odst[((by - 1) * linesize + bx - 1 - z) * 4 + 1];
614
+                            b = odst[((by - 1) * linesize + bx) * 4 + 2] +
615
+                                odst[(by * linesize + bx - 1 - z) * 4 + 2] -
616
+                                odst[((by - 1) * linesize + bx - 1 - z) * 4 + 2];
617
+                            clr = ((b & 0xFF) << 16) + ((g & 0xFF) << 8) + (r & 0xFF);
618
+                            dst[by * linesize + bx] = clr;
619
+                            bx++;
620
+                            if (bx >= x * 16 + sx2 || bx >= avctx->width) {
621
+                                bx = x * 16 + sx1;
622
+                                by++;
623
+                            }
624
+                        }
625
+                        break;
626
+                    case 5:
627
+                        while (run-- > 0) {
628
+                            if (by < 1 || by >= avctx->height)
629
+                                return AVERROR_INVALIDDATA;
630
+
631
+                            if (bx == 0) {
632
+                                z = backstep;
633
+                            } else {
634
+                                z = 0;
635
+                            }
636
+
637
+                            clr = dst[(by - 1) * linesize + bx - 1 - z];
638
+                            dst[by * linesize + bx] = clr;
639
+                            bx++;
640
+                            if (bx >= x * 16 + sx2 || bx >= avctx->width) {
641
+                                bx = x * 16 + sx1;
642
+                                by++;
643
+                            }
644
+                        }
645
+                        break;
646
+                    }
647
+
648
+                    if (avctx->bits_per_coded_sample == 16) {
649
+                        cx1 = (clr & 0xFF00) >> 2;
650
+                        cx = (clr & 0xFFFFFF) >> 16;
651
+                    } else {
652
+                        cx1 = (clr & 0xFC00) >> 4;
653
+                        cx = (clr & 0xFFFFFF) >> 18;
654
+                    }
655
+                }
656
+            }
657
+        }
658
+    }
659
+
660
+    return 0;
661
+}
662
+
663
+static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
664
+                        AVPacket *avpkt)
665
+{
666
+    SCPRContext *s = avctx->priv_data;
667
+    GetByteContext *gb = &s->gb;
668
+    AVFrame *frame = data;
669
+    int ret, type;
670
+
671
+    if (avctx->bits_per_coded_sample == 16) {
672
+        if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
673
+            return ret;
674
+    }
675
+
676
+    if ((ret = ff_reget_buffer(avctx, s->current_frame)) < 0)
677
+        return ret;
678
+
679
+    bytestream2_init(gb, avpkt->data, avpkt->size);
680
+
681
+    type = bytestream2_peek_byte(gb);
682
+
683
+    if (type == 18) {
684
+        frame->key_frame = 1;
685
+        ret = decompress_i(avctx, (uint32_t *)s->current_frame->data[0],
686
+                           s->current_frame->linesize[0] / 4);
687
+    } else if (type == 17) {
688
+        uint32_t clr, *dst = (uint32_t *)s->current_frame->data[0];
689
+        int x, y;
690
+
691
+        frame->key_frame = 1;
692
+        bytestream2_skip(gb, 1);
693
+        if (avctx->bits_per_coded_sample == 16) {
694
+            uint16_t value = bytestream2_get_le16(gb);
695
+            int r, g, b;
696
+
697
+            r = (value      ) & 31;
698
+            g = (value >>  5) & 31;
699
+            b = (value >> 10) & 31;
700
+            clr = (r << 16) + (g << 8) + b;
701
+        } else {
702
+            clr = bytestream2_get_le24(gb);
703
+        }
704
+        for (y = 0; y < avctx->height; y++) {
705
+            for (x = 0; x < avctx->width; x++) {
706
+                dst[x] = clr;
707
+            }
708
+            dst += s->current_frame->linesize[0] / 4;
709
+        }
710
+    } else if (type == 0 || type == 1) {
711
+        frame->key_frame = 0;
712
+
713
+        ret = av_frame_copy(s->current_frame, s->last_frame);
714
+        if (ret < 0)
715
+            return ret;
716
+
717
+        ret = decompress_p(avctx, (uint32_t *)s->current_frame->data[0],
718
+                           s->current_frame->linesize[0] / 4,
719
+                           (uint32_t *)s->last_frame->data[0],
720
+                           s->last_frame->linesize[0] / 4);
721
+    } else {
722
+        return AVERROR_PATCHWELCOME;
723
+    }
724
+
725
+    if (ret < 0)
726
+        return ret;
727
+
728
+    if (avctx->bits_per_coded_sample != 16) {
729
+        ret = av_frame_ref(data, s->current_frame);
730
+        if (ret < 0)
731
+            return ret;
732
+    } else {
733
+        uint8_t *dst = frame->data[0];
734
+        int x, y;
735
+
736
+        ret = av_frame_copy(frame, s->current_frame);
737
+        if (ret < 0)
738
+            return ret;
739
+
740
+        for (y = 0; y < avctx->height; y++) {
741
+            for (x = 0; x < avctx->width * 4; x++) {
742
+                dst[x] = dst[x] << 3;
743
+            }
744
+            dst += frame->linesize[0];
745
+        }
746
+    }
747
+
748
+    frame->pict_type = frame->key_frame ? AV_PICTURE_TYPE_I : AV_PICTURE_TYPE_P;
749
+
750
+    FFSWAP(AVFrame *, s->current_frame, s->last_frame);
751
+
752
+    frame->data[0]     += frame->linesize[0] * (avctx->height - 1);
753
+    frame->linesize[0] *= -1;
754
+
755
+    *got_frame = 1;
756
+
757
+    return avpkt->size;
758
+}
759
+
760
+static av_cold int decode_init(AVCodecContext *avctx)
761
+{
762
+    SCPRContext *s = avctx->priv_data;
763
+
764
+    switch (avctx->bits_per_coded_sample) {
765
+    case 16: avctx->pix_fmt = AV_PIX_FMT_RGB0; break;
766
+    case 24:
767
+    case 32: avctx->pix_fmt = AV_PIX_FMT_BGR0; break;
768
+    default:
769
+        av_log(avctx, AV_LOG_ERROR, "Unsupported bitdepth %i\n", avctx->bits_per_coded_sample);
770
+        return AVERROR_INVALIDDATA;
771
+    }
772
+
773
+    s->cxshift = avctx->bits_per_coded_sample == 16 ? 0 : 2;
774
+    s->nbx = (avctx->width + 15) / 16;
775
+    s->nby = (avctx->height + 15) / 16;
776
+    s->nbcount = s->nbx * s->nby;
777
+    s->blocks = av_malloc_array(s->nbcount, sizeof(*s->blocks));
778
+    if (!s->blocks)
779
+        return AVERROR(ENOMEM);
780
+
781
+    s->last_frame = av_frame_alloc();
782
+    s->current_frame = av_frame_alloc();
783
+    if (!s->last_frame || !s->current_frame)
784
+        return AVERROR(ENOMEM);
785
+
786
+    return 0;
787
+}
788
+
789
+static av_cold int decode_close(AVCodecContext *avctx)
790
+{
791
+    SCPRContext *s = avctx->priv_data;
792
+
793
+    av_freep(&s->blocks);
794
+    av_frame_free(&s->last_frame);
795
+    av_frame_free(&s->current_frame);
796
+
797
+    return 0;
798
+}
799
+
800
+AVCodec ff_scpr_decoder = {
801
+    .name             = "scpr",
802
+    .long_name        = NULL_IF_CONFIG_SMALL("ScreenPressor"),
803
+    .type             = AVMEDIA_TYPE_VIDEO,
804
+    .id               = AV_CODEC_ID_SCPR,
805
+    .priv_data_size   = sizeof(SCPRContext),
806
+    .init             = decode_init,
807
+    .close            = decode_close,
808
+    .decode           = decode_frame,
809
+    .capabilities     = AV_CODEC_CAP_DR1,
810
+    .caps_internal    = FF_CODEC_CAP_INIT_THREADSAFE |
811
+                        FF_CODEC_CAP_INIT_CLEANUP,
812
+};
... ...
@@ -28,8 +28,8 @@
28 28
 #include "libavutil/version.h"
29 29
 
30 30
 #define LIBAVCODEC_VERSION_MAJOR  57
31
-#define LIBAVCODEC_VERSION_MINOR  80
32
-#define LIBAVCODEC_VERSION_MICRO 101
31
+#define LIBAVCODEC_VERSION_MINOR  81
32
+#define LIBAVCODEC_VERSION_MICRO 100
33 33
 
34 34
 #define LIBAVCODEC_VERSION_INT  AV_VERSION_INT(LIBAVCODEC_VERSION_MAJOR, \
35 35
                                                LIBAVCODEC_VERSION_MINOR, \
... ...
@@ -449,6 +449,7 @@ const AVCodecTag ff_codec_bmp_tags[] = {
449 449
     { AV_CODEC_ID_SPEEDHQ,      MKTAG('S', 'H', 'Q', '7') },
450 450
     { AV_CODEC_ID_SPEEDHQ,      MKTAG('S', 'H', 'Q', '9') },
451 451
     { AV_CODEC_ID_FMVC,         MKTAG('F', 'M', 'V', 'C') },
452
+    { AV_CODEC_ID_SCPR,         MKTAG('S', 'C', 'P', 'R') },
452 453
 
453 454
     { AV_CODEC_ID_NONE,         0 }
454 455
 };