]> git.sesse.net Git - ffmpeg/blob - libavcodec/scpr.c
d5ce8c785db2fbcd1938e0f97e493576187b0607
[ffmpeg] / libavcodec / scpr.c
1 /*
2  * ScreenPressor decoder
3  *
4  * Copyright (c) 2017 Paul B Mahol
5  *
6  * This file is part of FFmpeg.
7  *
8  * FFmpeg is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * FFmpeg is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with FFmpeg; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26
27 #include "avcodec.h"
28 #include "bytestream.h"
29 #include "internal.h"
30
31 #define TOP  0x01000000
32 #define BOT    0x010000
33
34 typedef struct RangeCoder {
35     unsigned   code;
36     unsigned   range;
37     unsigned   code1;
38 } RangeCoder;
39
40 typedef struct PixelModel {
41     unsigned    freq[256];
42     unsigned    lookup[16];
43     unsigned    total_freq;
44 } PixelModel;
45
46 typedef struct SCPRContext {
47     AVFrame        *last_frame;
48     AVFrame        *current_frame;
49     GetByteContext  gb;
50     RangeCoder      rc;
51     PixelModel      pixel_model[3][4096];
52     unsigned        op_model[6][7];
53     unsigned        run_model[6][257];
54     unsigned        range_model[257];
55     unsigned        count_model[257];
56     unsigned        fill_model[6];
57     unsigned        sxy_model[4][17];
58     unsigned        mv_model[2][513];
59     unsigned        nbx, nby;
60     unsigned        nbcount;
61     unsigned       *blocks;
62     unsigned        cbits;
63     int             cxshift;
64
65     int           (*get_freq)(RangeCoder *rc, unsigned total_freq, unsigned *freq);
66     void          (*decode)(GetByteContext *gb, RangeCoder *rc, unsigned cumFreq, unsigned freq, unsigned total_freq);
67 } SCPRContext;
68
69 static void init_rangecoder(RangeCoder *rc, GetByteContext *gb)
70 {
71     rc->code1 = 0;
72     rc->range = 0xFFFFFFFFU;
73     rc->code  = bytestream2_get_be32(gb);
74 }
75
76 static void reinit_tables(SCPRContext *s)
77 {
78     int comp, i, j;
79
80     for (comp = 0; comp < 3; comp++) {
81         for (j = 0; j < 4096; j++) {
82             if (s->pixel_model[comp][j].total_freq != 256) {
83                 for (i = 0; i < 256; i++)
84                     s->pixel_model[comp][j].freq[i] = 1;
85                 for (i = 0; i < 16; i++)
86                     s->pixel_model[comp][j].lookup[i] = 16;
87                 s->pixel_model[comp][j].total_freq = 256;
88             }
89         }
90     }
91
92     for (j = 0; j < 6; j++) {
93         unsigned *p = s->run_model[j];
94         for (i = 0; i < 256; i++)
95             p[i] = 1;
96         p[256] = 256;
97     }
98
99     for (j = 0; j < 6; j++) {
100         unsigned *op = s->op_model[j];
101         for (i = 0; i < 6; i++)
102             op[i] = 1;
103         op[6] = 6;
104     }
105
106     for (i = 0; i < 256; i++) {
107         s->range_model[i] = 1;
108         s->count_model[i] = 1;
109     }
110     s->range_model[256] = 256;
111     s->count_model[256] = 256;
112
113     for (i = 0; i < 5; i++) {
114         s->fill_model[i] = 1;
115     }
116     s->fill_model[5] = 5;
117
118     for (j = 0; j < 4; j++) {
119         for (i = 0; i < 16; i++) {
120             s->sxy_model[j][i] = 1;
121         }
122         s->sxy_model[j][16] = 16;
123     }
124
125     for (i = 0; i < 512; i++) {
126         s->mv_model[0][i] = 1;
127         s->mv_model[1][i] = 1;
128     }
129     s->mv_model[0][512] = 512;
130     s->mv_model[1][512] = 512;
131 }
132
133 static void decode(GetByteContext *gb, RangeCoder *rc, unsigned cumFreq, unsigned freq, unsigned total_freq)
134 {
135     rc->code -= cumFreq * rc->range;
136     rc->range *= freq;
137
138     while (rc->range < TOP && bytestream2_get_bytes_left(gb) > 0) {
139         unsigned byte = bytestream2_get_byte(gb);
140         rc->code = (rc->code << 8) | byte;
141         rc->range <<= 8;
142     }
143 }
144
145 static int get_freq(RangeCoder *rc, unsigned total_freq, unsigned *freq)
146 {
147     if (total_freq == 0)
148         return AVERROR_INVALIDDATA;
149
150     rc->range = rc->range / total_freq;
151
152     if (rc->range == 0)
153         return AVERROR_INVALIDDATA;
154
155     *freq = rc->code / rc->range;
156
157     return 0;
158 }
159
160 static void decode0(GetByteContext *gb, RangeCoder *rc, unsigned cumFreq, unsigned freq, unsigned total_freq)
161 {
162     int t = rc->range * (uint64_t)cumFreq / total_freq;
163
164     rc->code1 += t + 1;
165     rc->range = rc->range * (uint64_t)(freq + cumFreq) / total_freq - (t + 1);
166
167     while (rc->range < TOP && bytestream2_get_bytes_left(gb) > 0) {
168         unsigned byte = bytestream2_get_byte(gb);
169         rc->code = (rc->code << 8) | byte;
170         rc->code1 <<= 8;
171         rc->range <<= 8;
172     }
173 }
174
175 static int get_freq0(RangeCoder *rc, unsigned total_freq, unsigned *freq)
176 {
177     if (rc->range == 0)
178         return AVERROR_INVALIDDATA;
179
180     *freq = total_freq * (uint64_t)(rc->code - rc->code1) / rc->range;
181
182     return 0;
183 }
184
185 static int decode_value(SCPRContext *s, unsigned *cnt, unsigned maxc, unsigned step, unsigned *rval)
186 {
187     GetByteContext *gb = &s->gb;
188     RangeCoder *rc = &s->rc;
189     unsigned totfr = cnt[maxc];
190     unsigned value;
191     unsigned c = 0, cumfr = 0, cnt_c = 0;
192     int i, ret;
193
194     if ((ret = s->get_freq(rc, totfr, &value)) < 0)
195         return ret;
196
197     while (c < maxc) {
198         cnt_c = cnt[c];
199         if (value >= cumfr + cnt_c)
200             cumfr += cnt_c;
201         else
202             break;
203         c++;
204     }
205     s->decode(gb, rc, cumfr, cnt_c, totfr);
206
207     cnt[c] = cnt_c + step;
208     totfr += step;
209     if (totfr > BOT) {
210         totfr = 0;
211         for (i = 0; i < maxc; i++) {
212             unsigned nc = (cnt[i] >> 1) + 1;
213             cnt[i] = nc;
214             totfr += nc;
215         }
216     }
217
218     cnt[maxc] = totfr;
219     *rval = c;
220
221     return 0;
222 }
223
224 static int decode_unit(SCPRContext *s, PixelModel *pixel, unsigned step, unsigned *rval)
225 {
226     GetByteContext *gb = &s->gb;
227     RangeCoder *rc = &s->rc;
228     unsigned totfr = pixel->total_freq;
229     unsigned value, x = 0, cumfr = 0, cnt_x = 0;
230     int i, j, ret, c, cnt_c;
231
232     if ((ret = s->get_freq(rc, totfr, &value)) < 0)
233         return ret;
234
235     while (x < 16) {
236         cnt_x = pixel->lookup[x];
237         if (value >= cumfr + cnt_x)
238             cumfr += cnt_x;
239         else
240             break;
241         x++;
242     }
243
244     c = x * 16;
245     cnt_c = 0;
246     while (c < 256) {
247         cnt_c = pixel->freq[c];
248         if (value >= cumfr + cnt_c)
249             cumfr += cnt_c;
250         else
251             break;
252         c++;
253     }
254     s->decode(gb, rc, cumfr, cnt_c, totfr);
255     pixel->freq[c] = cnt_c + step;
256     pixel->lookup[x] = cnt_x + step;
257     totfr += step;
258     if (totfr > BOT) {
259         totfr = 0;
260         for (i = 0; i < 256; i++) {
261             unsigned nc = (pixel->freq[i] >> 1) + 1;
262             pixel->freq[i] = nc;
263             totfr += nc;
264         }
265         for (i = 0; i < 16; i++) {
266             unsigned sum = 0;
267             unsigned i16_17 = i << 4;
268             for (j = 0; j < 16; j++)
269                 sum += pixel->freq[i16_17 + j];
270             pixel->lookup[i] = sum;
271         }
272     }
273     pixel->total_freq = totfr;
274
275     *rval = c & s->cbits;
276
277     return 0;
278 }
279
280 static int decompress_i(AVCodecContext *avctx, uint32_t *dst, int linesize)
281 {
282     SCPRContext *s = avctx->priv_data;
283     GetByteContext *gb = &s->gb;
284     int cx = 0, cx1 = 0, k = 0, clr = 0;
285     int run, r, g, b, off, y = 0, x = 0, ret;
286     const int cxshift = s->cxshift;
287     unsigned lx, ly, ptype;
288
289     reinit_tables(s);
290     bytestream2_skip(gb, 2);
291     init_rangecoder(&s->rc, gb);
292
293     while (k < avctx->width + 1) {
294         ret = decode_unit(s, &s->pixel_model[0][cx + cx1], 400, &r);
295         if (ret < 0)
296             return ret;
297
298         cx1 = (cx << 6) & 0xFC0;
299         cx = r >> cxshift;
300         ret = decode_unit(s, &s->pixel_model[1][cx + cx1], 400, &g);
301         if (ret < 0)
302             return ret;
303
304         cx1 = (cx << 6) & 0xFC0;
305         cx = g >> cxshift;
306         ret = decode_unit(s, &s->pixel_model[2][cx + cx1], 400, &b);
307         if (ret < 0)
308             return ret;
309
310         cx1 = (cx << 6) & 0xFC0;
311         cx = b >> cxshift;
312
313         ret = decode_value(s, s->run_model[0], 256, 400, &run);
314         if (ret < 0)
315             return ret;
316
317         clr = (b << 16) + (g << 8) + r;
318         k += run;
319         while (run-- > 0) {
320             dst[y * linesize + x] = clr;
321             lx = x;
322             ly = y;
323             x++;
324             if (x >= avctx->width) {
325                 x = 0;
326                 y++;
327             }
328         }
329     }
330     off = -linesize - 1;
331     ptype = 0;
332
333     while (x < avctx->width && y < avctx->height) {
334         ret = decode_value(s, s->op_model[ptype], 6, 1000, &ptype);
335         if (ret < 0)
336             return ret;
337         if (ptype == 0) {
338             ret = decode_unit(s, &s->pixel_model[0][cx + cx1], 400, &r);
339             if (ret < 0)
340                 return ret;
341
342             cx1 = (cx << 6) & 0xFC0;
343             cx = r >> cxshift;
344             ret = decode_unit(s, &s->pixel_model[1][cx + cx1], 400, &g);
345             if (ret < 0)
346                 return ret;
347
348             cx1 = (cx << 6) & 0xFC0;
349             cx = g >> cxshift;
350             ret = decode_unit(s, &s->pixel_model[2][cx + cx1], 400, &b);
351             if (ret < 0)
352                 return ret;
353
354             clr = (b << 16) + (g << 8) + r;
355         }
356         if (ptype > 5)
357             return AVERROR_INVALIDDATA;
358         ret = decode_value(s, s->run_model[ptype], 256, 400, &run);
359         if (ret < 0)
360             return ret;
361
362         switch (ptype) {
363         case 0:
364             while (run-- > 0) {
365                 if (y >= avctx->height)
366                     return AVERROR_INVALIDDATA;
367
368                 dst[y * linesize + x] = clr;
369                 lx = x;
370                 ly = y;
371                 x++;
372                 if (x >= avctx->width) {
373                     x = 0;
374                     y++;
375                 }
376             }
377             break;
378         case 1:
379             while (run-- > 0) {
380                 if (y >= avctx->height)
381                     return AVERROR_INVALIDDATA;
382
383                 dst[y * linesize + x] = dst[ly * linesize + lx];
384                 lx = x;
385                 ly = y;
386                 x++;
387                 if (x >= avctx->width) {
388                     x = 0;
389                     y++;
390                 }
391             }
392             clr = dst[ly * linesize + lx];
393             break;
394         case 2:
395             while (run-- > 0) {
396                 if (y < 1 || y >= avctx->height)
397                     return AVERROR_INVALIDDATA;
398
399                 clr = dst[y * linesize + x + off + 1];
400                 dst[y * linesize + x] = clr;
401                 lx = x;
402                 ly = y;
403                 x++;
404                 if (x >= avctx->width) {
405                     x = 0;
406                     y++;
407                 }
408             }
409             break;
410         case 4:
411             while (run-- > 0) {
412                 uint8_t *odst = (uint8_t *)dst;
413
414                 if (y < 1 || y >= avctx->height)
415                     return AVERROR_INVALIDDATA;
416
417                 r = odst[(ly * linesize + lx) * 4] +
418                     odst[((y * linesize + x) + off) * 4 + 4] -
419                     odst[((y * linesize + x) + off) * 4];
420                 g = odst[(ly * linesize + lx) * 4 + 1] +
421                     odst[((y * linesize + x) + off) * 4 + 5] -
422                     odst[((y * linesize + x) + off) * 4 + 1];
423                 b = odst[(ly * linesize + lx) * 4 + 2] +
424                     odst[((y * linesize + x) + off) * 4 + 6] -
425                     odst[((y * linesize + x) + off) * 4 + 2];
426                 clr = ((b & 0xFF) << 16) + ((g & 0xFF) << 8) + (r & 0xFF);
427                 dst[y * linesize + x] = clr;
428                 lx = x;
429                 ly = y;
430                 x++;
431                 if (x >= avctx->width) {
432                     x = 0;
433                     y++;
434                 }
435             }
436             break;
437         case 5:
438             while (run-- > 0) {
439                 if (y < 1 || y >= avctx->height)
440                     return AVERROR_INVALIDDATA;
441
442                 clr = dst[y * linesize + x + off];
443                 dst[y * linesize + x] = clr;
444                 lx = x;
445                 ly = y;
446                 x++;
447                 if (x >= avctx->width) {
448                     x = 0;
449                     y++;
450                 }
451             }
452             break;
453         }
454
455         if (avctx->bits_per_coded_sample == 16) {
456             cx1 = (clr & 0x3F00) >> 2;
457             cx = (clr & 0xFFFFFF) >> 16;
458         } else {
459             cx1 = (clr & 0xFC00) >> 4;
460             cx = (clr & 0xFFFFFF) >> 18;
461         }
462     }
463
464     return 0;
465 }
466
467 static int decompress_p(AVCodecContext *avctx,
468                         uint32_t *dst, int linesize,
469                         uint32_t *prev, int plinesize)
470 {
471     SCPRContext *s = avctx->priv_data;
472     GetByteContext *gb = &s->gb;
473     int ret, temp, min, max, x, y, cx = 0, cx1 = 0;
474     int backstep = linesize - avctx->width;
475     const int cxshift = s->cxshift;
476
477     if (bytestream2_get_byte(gb) == 0)
478         return 0;
479     bytestream2_skip(gb, 1);
480     init_rangecoder(&s->rc, gb);
481
482     ret  = decode_value(s, s->range_model, 256, 1, &min);
483     ret |= decode_value(s, s->range_model, 256, 1, &temp);
484     min += temp << 8;
485     ret |= decode_value(s, s->range_model, 256, 1, &max);
486     ret |= decode_value(s, s->range_model, 256, 1, &temp);
487     if (ret < 0)
488         return ret;
489
490     max += temp << 8;
491     memset(s->blocks, 0, sizeof(*s->blocks) * s->nbcount);
492
493     while (min <= max) {
494         int fill, count;
495
496         ret  = decode_value(s, s->fill_model,  5,   10, &fill);
497         ret |= decode_value(s, s->count_model, 256, 20, &count);
498         if (ret < 0)
499             return ret;
500
501         while (min < s->nbcount && count-- > 0) {
502             s->blocks[min++] = fill;
503         }
504     }
505
506     for (y = 0; y < s->nby; y++) {
507         for (x = 0; x < s->nbx; x++) {
508             int sy1 = 0, sy2 = 16, sx1 = 0, sx2 = 16;
509
510             if (s->blocks[y * s->nbx + x] == 0)
511                 continue;
512
513             if (((s->blocks[y * s->nbx + x] - 1) & 1) > 0) {
514                 ret  = decode_value(s, s->sxy_model[0], 16, 100, &sx1);
515                 ret |= decode_value(s, s->sxy_model[1], 16, 100, &sy1);
516                 ret |= decode_value(s, s->sxy_model[2], 16, 100, &sx2);
517                 ret |= decode_value(s, s->sxy_model[3], 16, 100, &sy2);
518                 if (ret < 0)
519                     return ret;
520
521                 sx2++;
522                 sy2++;
523             }
524             if (((s->blocks[y * s->nbx + x] - 1) & 2) > 0) {
525                 int i, j, by = y * 16, bx = x * 16;
526                 int mvx, mvy;
527
528                 ret  = decode_value(s, s->mv_model[0], 512, 100, &mvx);
529                 ret |= decode_value(s, s->mv_model[1], 512, 100, &mvy);
530                 if (ret < 0)
531                     return ret;
532
533                 mvx -= 256;
534                 mvy -= 256;
535
536                 if (by + mvy + sy1 < 0 || bx + mvx + sx1 < 0 ||
537                     by + mvy + sy1 >= avctx->height || bx + mvx + sx1 >= avctx->width)
538                     return AVERROR_INVALIDDATA;
539
540                 for (i = 0; i < sy2 - sy1 && (by + sy1 + i) < avctx->height && (by + mvy + sy1 + i) < avctx->height; i++) {
541                     for (j = 0; j < sx2 - sx1 && (bx + sx1 + j) < avctx->width && (bx + mvx + sx1 + j) < avctx->width; j++) {
542                         dst[(by + i + sy1) * linesize + bx + sx1 + j] = prev[(by + mvy + sy1 + i) * plinesize + bx + sx1 + mvx + j];
543                     }
544                 }
545             } else {
546                 int run, r, g, b, z, bx = x * 16 + sx1, by = y * 16 + sy1;
547                 unsigned clr, ptype = 0;
548
549                 for (; by < y * 16 + sy2 && by < avctx->height;) {
550                     ret = decode_value(s, s->op_model[ptype], 6, 1000, &ptype);
551                     if (ptype == 0) {
552                         ret = decode_unit(s, &s->pixel_model[0][cx + cx1], 400, &r);
553                         if (ret < 0)
554                             return ret;
555
556                         cx1 = (cx << 6) & 0xFC0;
557                         cx = r >> cxshift;
558                         ret = decode_unit(s, &s->pixel_model[1][cx + cx1], 400, &g);
559                         if (ret < 0)
560                             return ret;
561
562                         cx1 = (cx << 6) & 0xFC0;
563                         cx = g >> cxshift;
564                         ret = decode_unit(s, &s->pixel_model[2][cx + cx1], 400, &b);
565                         if (ret < 0)
566                             return ret;
567
568                         clr = (b << 16) + (g << 8) + r;
569                     }
570                     if (ptype > 5)
571                         return AVERROR_INVALIDDATA;
572                     ret = decode_value(s, s->run_model[ptype], 256, 400, &run);
573                     if (ret < 0)
574                         return ret;
575
576                     switch (ptype) {
577                     case 0:
578                         while (run-- > 0) {
579                             if (by >= avctx->height)
580                                 return AVERROR_INVALIDDATA;
581
582                             dst[by * linesize + bx] = clr;
583                             bx++;
584                             if (bx >= x * 16 + sx2 || bx >= avctx->width) {
585                                 bx = x * 16 + sx1;
586                                 by++;
587                             }
588                         }
589                         break;
590                     case 1:
591                         while (run-- > 0) {
592                             if (bx == 0) {
593                                 if (by < 1)
594                                     return AVERROR_INVALIDDATA;
595                                 z = backstep;
596                             } else {
597                                 z = 0;
598                             }
599
600                             if (by >= avctx->height)
601                                 return AVERROR_INVALIDDATA;
602
603                             clr = dst[by * linesize + bx - 1 - z];
604                             dst[by * linesize + bx] = clr;
605                             bx++;
606                             if (bx >= x * 16 + sx2 || bx >= avctx->width) {
607                                 bx = x * 16 + sx1;
608                                 by++;
609                             }
610                         }
611                         break;
612                     case 2:
613                         while (run-- > 0) {
614                             if (by < 1 || by >= avctx->height)
615                                 return AVERROR_INVALIDDATA;
616
617                             clr = dst[(by - 1) * linesize + bx];
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 3:
627                         while (run-- > 0) {
628                             if (by >= avctx->height)
629                                 return AVERROR_INVALIDDATA;
630
631                             clr = prev[by * linesize + bx];
632                             dst[by * linesize + bx] = clr;
633                             bx++;
634                             if (bx >= x * 16 + sx2 || bx >= avctx->width) {
635                                 bx = x * 16 + sx1;
636                                 by++;
637                             }
638                         }
639                         break;
640                     case 4:
641                         while (run-- > 0) {
642                             uint8_t *odst = (uint8_t *)dst;
643
644                             if (by < 1 || by >= avctx->height)
645                                 return AVERROR_INVALIDDATA;
646
647                             if (bx == 0) {
648                                 z = backstep;
649                             } else {
650                                 z = 0;
651                             }
652
653                             r = odst[((by - 1) * linesize + bx) * 4] +
654                                 odst[(by * linesize + bx - 1 - z) * 4] -
655                                 odst[((by - 1) * linesize + bx - 1 - z) * 4];
656                             g = odst[((by - 1) * linesize + bx) * 4 + 1] +
657                                 odst[(by * linesize + bx - 1 - z) * 4 + 1] -
658                                 odst[((by - 1) * linesize + bx - 1 - z) * 4 + 1];
659                             b = odst[((by - 1) * linesize + bx) * 4 + 2] +
660                                 odst[(by * linesize + bx - 1 - z) * 4 + 2] -
661                                 odst[((by - 1) * linesize + bx - 1 - z) * 4 + 2];
662                             clr = ((b & 0xFF) << 16) + ((g & 0xFF) << 8) + (r & 0xFF);
663                             dst[by * linesize + bx] = clr;
664                             bx++;
665                             if (bx >= x * 16 + sx2 || bx >= avctx->width) {
666                                 bx = x * 16 + sx1;
667                                 by++;
668                             }
669                         }
670                         break;
671                     case 5:
672                         while (run-- > 0) {
673                             if (by < 1 || by >= avctx->height)
674                                 return AVERROR_INVALIDDATA;
675
676                             if (bx == 0) {
677                                 z = backstep;
678                             } else {
679                                 z = 0;
680                             }
681
682                             clr = dst[(by - 1) * linesize + bx - 1 - z];
683                             dst[by * linesize + bx] = clr;
684                             bx++;
685                             if (bx >= x * 16 + sx2 || bx >= avctx->width) {
686                                 bx = x * 16 + sx1;
687                                 by++;
688                             }
689                         }
690                         break;
691                     }
692
693                     if (avctx->bits_per_coded_sample == 16) {
694                         cx1 = (clr & 0x3F00) >> 2;
695                         cx = (clr & 0xFFFFFF) >> 16;
696                     } else {
697                         cx1 = (clr & 0xFC00) >> 4;
698                         cx = (clr & 0xFFFFFF) >> 18;
699                     }
700                 }
701             }
702         }
703     }
704
705     return 0;
706 }
707
708 static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
709                         AVPacket *avpkt)
710 {
711     SCPRContext *s = avctx->priv_data;
712     GetByteContext *gb = &s->gb;
713     AVFrame *frame = data;
714     int ret, type;
715
716     if (avctx->bits_per_coded_sample == 16) {
717         if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
718             return ret;
719     }
720
721     if ((ret = ff_reget_buffer(avctx, s->current_frame)) < 0)
722         return ret;
723
724     bytestream2_init(gb, avpkt->data, avpkt->size);
725
726     type = bytestream2_peek_byte(gb);
727
728     if (type == 2) {
729         s->get_freq = get_freq0;
730         s->decode = decode0;
731         frame->key_frame = 1;
732         ret = decompress_i(avctx, (uint32_t *)s->current_frame->data[0],
733                            s->current_frame->linesize[0] / 4);
734     } else if (type == 18) {
735         s->get_freq = get_freq;
736         s->decode = decode;
737         frame->key_frame = 1;
738         ret = decompress_i(avctx, (uint32_t *)s->current_frame->data[0],
739                            s->current_frame->linesize[0] / 4);
740     } else if (type == 17) {
741         uint32_t clr, *dst = (uint32_t *)s->current_frame->data[0];
742         int x, y;
743
744         frame->key_frame = 1;
745         bytestream2_skip(gb, 1);
746         if (avctx->bits_per_coded_sample == 16) {
747             uint16_t value = bytestream2_get_le16(gb);
748             int r, g, b;
749
750             r = (value      ) & 31;
751             g = (value >>  5) & 31;
752             b = (value >> 10) & 31;
753             clr = (r << 16) + (g << 8) + b;
754         } else {
755             clr = bytestream2_get_le24(gb);
756         }
757         for (y = 0; y < avctx->height; y++) {
758             for (x = 0; x < avctx->width; x++) {
759                 dst[x] = clr;
760             }
761             dst += s->current_frame->linesize[0] / 4;
762         }
763     } else if (type == 0 || type == 1) {
764         frame->key_frame = 0;
765
766         ret = av_frame_copy(s->current_frame, s->last_frame);
767         if (ret < 0)
768             return ret;
769
770         ret = decompress_p(avctx, (uint32_t *)s->current_frame->data[0],
771                            s->current_frame->linesize[0] / 4,
772                            (uint32_t *)s->last_frame->data[0],
773                            s->last_frame->linesize[0] / 4);
774     } else {
775         return AVERROR_PATCHWELCOME;
776     }
777
778     if (ret < 0)
779         return ret;
780
781     if (avctx->bits_per_coded_sample != 16) {
782         ret = av_frame_ref(data, s->current_frame);
783         if (ret < 0)
784             return ret;
785     } else {
786         uint8_t *dst = frame->data[0];
787         int x, y;
788
789         ret = av_frame_copy(frame, s->current_frame);
790         if (ret < 0)
791             return ret;
792
793         for (y = 0; y < avctx->height; y++) {
794             for (x = 0; x < avctx->width * 4; x++) {
795                 dst[x] = dst[x] << 3;
796             }
797             dst += frame->linesize[0];
798         }
799     }
800
801     frame->pict_type = frame->key_frame ? AV_PICTURE_TYPE_I : AV_PICTURE_TYPE_P;
802
803     FFSWAP(AVFrame *, s->current_frame, s->last_frame);
804
805     frame->data[0]     += frame->linesize[0] * (avctx->height - 1);
806     frame->linesize[0] *= -1;
807
808     *got_frame = 1;
809
810     return avpkt->size;
811 }
812
813 static av_cold int decode_init(AVCodecContext *avctx)
814 {
815     SCPRContext *s = avctx->priv_data;
816
817     switch (avctx->bits_per_coded_sample) {
818     case 16: avctx->pix_fmt = AV_PIX_FMT_RGB0; break;
819     case 24:
820     case 32: avctx->pix_fmt = AV_PIX_FMT_BGR0; break;
821     default:
822         av_log(avctx, AV_LOG_ERROR, "Unsupported bitdepth %i\n", avctx->bits_per_coded_sample);
823         return AVERROR_INVALIDDATA;
824     }
825
826     s->get_freq = get_freq0;
827     s->decode = decode0;
828
829     s->cxshift = avctx->bits_per_coded_sample == 16 ? 0 : 2;
830     s->cbits = avctx->bits_per_coded_sample == 16 ? 0x1F : 0xFF;
831     s->nbx = (avctx->width + 15) / 16;
832     s->nby = (avctx->height + 15) / 16;
833     s->nbcount = s->nbx * s->nby;
834     s->blocks = av_malloc_array(s->nbcount, sizeof(*s->blocks));
835     if (!s->blocks)
836         return AVERROR(ENOMEM);
837
838     s->last_frame = av_frame_alloc();
839     s->current_frame = av_frame_alloc();
840     if (!s->last_frame || !s->current_frame)
841         return AVERROR(ENOMEM);
842
843     return 0;
844 }
845
846 static av_cold int decode_close(AVCodecContext *avctx)
847 {
848     SCPRContext *s = avctx->priv_data;
849
850     av_freep(&s->blocks);
851     av_frame_free(&s->last_frame);
852     av_frame_free(&s->current_frame);
853
854     return 0;
855 }
856
857 AVCodec ff_scpr_decoder = {
858     .name             = "scpr",
859     .long_name        = NULL_IF_CONFIG_SMALL("ScreenPressor"),
860     .type             = AVMEDIA_TYPE_VIDEO,
861     .id               = AV_CODEC_ID_SCPR,
862     .priv_data_size   = sizeof(SCPRContext),
863     .init             = decode_init,
864     .close            = decode_close,
865     .decode           = decode_frame,
866     .capabilities     = AV_CODEC_CAP_DR1,
867     .caps_internal    = FF_CODEC_CAP_INIT_THREADSAFE |
868                         FF_CODEC_CAP_INIT_CLEANUP,
869 };