2 * ScreenPressor decoder
4 * Copyright (c) 2017 Paul B Mahol
6 * This file is part of FFmpeg.
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.
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.
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
28 #include "bytestream.h"
31 #define TOP 0x01000000
34 typedef struct RangeCoder {
40 typedef struct PixelModel {
46 typedef struct SCPRContext {
48 AVFrame *current_frame;
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];
65 int (*get_freq)(RangeCoder *rc, unsigned total_freq, unsigned *freq);
66 int (*decode)(GetByteContext *gb, RangeCoder *rc, unsigned cumFreq, unsigned freq, unsigned total_freq);
69 static void init_rangecoder(RangeCoder *rc, GetByteContext *gb)
72 rc->range = 0xFFFFFFFFU;
73 rc->code = bytestream2_get_be32(gb);
76 static void reinit_tables(SCPRContext *s)
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;
92 for (j = 0; j < 6; j++) {
93 unsigned *p = s->run_model[j];
94 for (i = 0; i < 256; i++)
99 for (j = 0; j < 6; j++) {
100 unsigned *op = s->op_model[j];
101 for (i = 0; i < 6; i++)
106 for (i = 0; i < 256; i++) {
107 s->range_model[i] = 1;
108 s->count_model[i] = 1;
110 s->range_model[256] = 256;
111 s->count_model[256] = 256;
113 for (i = 0; i < 5; i++) {
114 s->fill_model[i] = 1;
116 s->fill_model[5] = 5;
118 for (j = 0; j < 4; j++) {
119 for (i = 0; i < 16; i++) {
120 s->sxy_model[j][i] = 1;
122 s->sxy_model[j][16] = 16;
125 for (i = 0; i < 512; i++) {
126 s->mv_model[0][i] = 1;
127 s->mv_model[1][i] = 1;
129 s->mv_model[0][512] = 512;
130 s->mv_model[1][512] = 512;
133 static int decode(GetByteContext *gb, RangeCoder *rc, unsigned cumFreq, unsigned freq, unsigned total_freq)
135 rc->code -= cumFreq * rc->range;
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;
147 static int get_freq(RangeCoder *rc, unsigned total_freq, unsigned *freq)
150 return AVERROR_INVALIDDATA;
152 rc->range = rc->range / total_freq;
155 return AVERROR_INVALIDDATA;
157 *freq = rc->code / rc->range;
162 static int decode0(GetByteContext *gb, RangeCoder *rc, unsigned cumFreq, unsigned freq, unsigned total_freq)
167 return AVERROR_INVALIDDATA;
169 t = rc->range * (uint64_t)cumFreq / total_freq;
172 rc->range = rc->range * (uint64_t)(freq + cumFreq) / total_freq - (t + 1);
174 while (rc->range < TOP && bytestream2_get_bytes_left(gb) > 0) {
175 unsigned byte = bytestream2_get_byte(gb);
176 rc->code = (rc->code << 8) | byte;
184 static int get_freq0(RangeCoder *rc, unsigned total_freq, unsigned *freq)
187 return AVERROR_INVALIDDATA;
189 *freq = total_freq * (uint64_t)(rc->code - rc->code1) / rc->range;
194 static int decode_value(SCPRContext *s, unsigned *cnt, unsigned maxc, unsigned step, unsigned *rval)
196 GetByteContext *gb = &s->gb;
197 RangeCoder *rc = &s->rc;
198 unsigned totfr = cnt[maxc];
200 unsigned c = 0, cumfr = 0, cnt_c = 0;
203 if ((ret = s->get_freq(rc, totfr, &value)) < 0)
208 if (value >= cumfr + cnt_c)
216 return AVERROR_INVALIDDATA;
218 if ((ret = s->decode(gb, rc, cumfr, cnt_c, totfr)) < 0)
221 cnt[c] = cnt_c + step;
225 for (i = 0; i < maxc; i++) {
226 unsigned nc = (cnt[i] >> 1) + 1;
238 static int decode_unit(SCPRContext *s, PixelModel *pixel, unsigned step, unsigned *rval)
240 GetByteContext *gb = &s->gb;
241 RangeCoder *rc = &s->rc;
242 unsigned totfr = pixel->total_freq;
243 unsigned value, x = 0, cumfr = 0, cnt_x = 0;
244 int i, j, ret, c, cnt_c;
246 if ((ret = s->get_freq(rc, totfr, &value)) < 0)
250 cnt_x = pixel->lookup[x];
251 if (value >= cumfr + cnt_x)
261 cnt_c = pixel->freq[c];
262 if (value >= cumfr + cnt_c)
268 if (x >= 16 || c >= 256) {
269 return AVERROR_INVALIDDATA;
272 if ((ret = s->decode(gb, rc, cumfr, cnt_c, totfr)) < 0)
275 pixel->freq[c] = cnt_c + step;
276 pixel->lookup[x] = cnt_x + step;
280 for (i = 0; i < 256; i++) {
281 unsigned nc = (pixel->freq[i] >> 1) + 1;
285 for (i = 0; i < 16; i++) {
287 unsigned i16_17 = i << 4;
288 for (j = 0; j < 16; j++)
289 sum += pixel->freq[i16_17 + j];
290 pixel->lookup[i] = sum;
293 pixel->total_freq = totfr;
295 *rval = c & s->cbits;
300 static int decompress_i(AVCodecContext *avctx, uint32_t *dst, int linesize)
302 SCPRContext *s = avctx->priv_data;
303 GetByteContext *gb = &s->gb;
304 int cx = 0, cx1 = 0, k = 0, clr = 0;
305 int run, r, g, b, off, y = 0, x = 0, z, ret;
306 unsigned backstep = linesize - avctx->width;
307 const int cxshift = s->cxshift;
308 unsigned lx, ly, ptype;
311 bytestream2_skip(gb, 2);
312 init_rangecoder(&s->rc, gb);
314 while (k < avctx->width + 1) {
315 ret = decode_unit(s, &s->pixel_model[0][cx + cx1], 400, &r);
319 cx1 = (cx << 6) & 0xFC0;
321 ret = decode_unit(s, &s->pixel_model[1][cx + cx1], 400, &g);
325 cx1 = (cx << 6) & 0xFC0;
327 ret = decode_unit(s, &s->pixel_model[2][cx + cx1], 400, &b);
331 cx1 = (cx << 6) & 0xFC0;
334 ret = decode_value(s, s->run_model[0], 256, 400, &run);
338 clr = (b << 16) + (g << 8) + r;
341 if (y >= avctx->height)
342 return AVERROR_INVALIDDATA;
344 dst[y * linesize + x] = clr;
348 if (x >= avctx->width) {
357 while (x < avctx->width && y < avctx->height) {
358 ret = decode_value(s, s->op_model[ptype], 6, 1000, &ptype);
362 ret = decode_unit(s, &s->pixel_model[0][cx + cx1], 400, &r);
366 cx1 = (cx << 6) & 0xFC0;
368 ret = decode_unit(s, &s->pixel_model[1][cx + cx1], 400, &g);
372 cx1 = (cx << 6) & 0xFC0;
374 ret = decode_unit(s, &s->pixel_model[2][cx + cx1], 400, &b);
378 clr = (b << 16) + (g << 8) + r;
381 return AVERROR_INVALIDDATA;
382 ret = decode_value(s, s->run_model[ptype], 256, 400, &run);
389 if (y >= avctx->height)
390 return AVERROR_INVALIDDATA;
392 dst[y * linesize + x] = clr;
396 if (x >= avctx->width) {
404 if (y >= avctx->height)
405 return AVERROR_INVALIDDATA;
407 dst[y * linesize + x] = dst[ly * linesize + lx];
411 if (x >= avctx->width) {
416 clr = dst[ly * linesize + lx];
420 if (y < 1 || y >= avctx->height)
421 return AVERROR_INVALIDDATA;
423 clr = dst[y * linesize + x + off + 1];
424 dst[y * linesize + x] = clr;
428 if (x >= avctx->width) {
436 uint8_t *odst = (uint8_t *)dst;
438 if (y < 1 || y >= avctx->height ||
440 return AVERROR_INVALIDDATA;
448 r = odst[(ly * linesize + lx) * 4] +
449 odst[((y * linesize + x) + off) * 4 + 4] -
450 odst[((y * linesize + x) + off - z) * 4];
451 g = odst[(ly * linesize + lx) * 4 + 1] +
452 odst[((y * linesize + x) + off) * 4 + 5] -
453 odst[((y * linesize + x) + off - z) * 4 + 1];
454 b = odst[(ly * linesize + lx) * 4 + 2] +
455 odst[((y * linesize + x) + off) * 4 + 6] -
456 odst[((y * linesize + x) + off - z) * 4 + 2];
457 clr = ((b & 0xFF) << 16) + ((g & 0xFF) << 8) + (r & 0xFF);
458 dst[y * linesize + x] = clr;
462 if (x >= avctx->width) {
470 if (y < 1 || y >= avctx->height ||
472 return AVERROR_INVALIDDATA;
480 clr = dst[y * linesize + x + off - z];
481 dst[y * linesize + x] = clr;
485 if (x >= avctx->width) {
493 if (avctx->bits_per_coded_sample == 16) {
494 cx1 = (clr & 0x3F00) >> 2;
495 cx = (clr & 0x3FFFFF) >> 16;
497 cx1 = (clr & 0xFC00) >> 4;
498 cx = (clr & 0xFFFFFF) >> 18;
505 static int decompress_p(AVCodecContext *avctx,
506 uint32_t *dst, int linesize,
507 uint32_t *prev, int plinesize)
509 SCPRContext *s = avctx->priv_data;
510 GetByteContext *gb = &s->gb;
511 int ret, temp, min, max, x, y, cx = 0, cx1 = 0;
512 int backstep = linesize - avctx->width;
513 const int cxshift = s->cxshift;
515 if (bytestream2_get_byte(gb) == 0)
517 bytestream2_skip(gb, 1);
518 init_rangecoder(&s->rc, gb);
520 ret = decode_value(s, s->range_model, 256, 1, &min);
521 ret |= decode_value(s, s->range_model, 256, 1, &temp);
523 ret |= decode_value(s, s->range_model, 256, 1, &max);
524 ret |= decode_value(s, s->range_model, 256, 1, &temp);
529 memset(s->blocks, 0, sizeof(*s->blocks) * s->nbcount);
534 ret = decode_value(s, s->fill_model, 5, 10, &fill);
535 ret |= decode_value(s, s->count_model, 256, 20, &count);
539 while (min < s->nbcount && count-- > 0) {
540 s->blocks[min++] = fill;
544 for (y = 0; y < s->nby; y++) {
545 for (x = 0; x < s->nbx; x++) {
546 int sy1 = 0, sy2 = 16, sx1 = 0, sx2 = 16;
548 if (s->blocks[y * s->nbx + x] == 0)
551 if (((s->blocks[y * s->nbx + x] - 1) & 1) > 0) {
552 ret = decode_value(s, s->sxy_model[0], 16, 100, &sx1);
553 ret |= decode_value(s, s->sxy_model[1], 16, 100, &sy1);
554 ret |= decode_value(s, s->sxy_model[2], 16, 100, &sx2);
555 ret |= decode_value(s, s->sxy_model[3], 16, 100, &sy2);
562 if (((s->blocks[y * s->nbx + x] - 1) & 2) > 0) {
563 int i, j, by = y * 16, bx = x * 16;
566 ret = decode_value(s, s->mv_model[0], 512, 100, &mvx);
567 ret |= decode_value(s, s->mv_model[1], 512, 100, &mvy);
574 if (by + mvy + sy1 < 0 || bx + mvx + sx1 < 0 ||
575 by + mvy + sy1 >= avctx->height || bx + mvx + sx1 >= avctx->width)
576 return AVERROR_INVALIDDATA;
578 for (i = 0; i < sy2 - sy1 && (by + sy1 + i) < avctx->height && (by + mvy + sy1 + i) < avctx->height; i++) {
579 for (j = 0; j < sx2 - sx1 && (bx + sx1 + j) < avctx->width && (bx + mvx + sx1 + j) < avctx->width; j++) {
580 dst[(by + i + sy1) * linesize + bx + sx1 + j] = prev[(by + mvy + sy1 + i) * plinesize + bx + sx1 + mvx + j];
584 int run, r, g, b, z, bx = x * 16 + sx1, by = y * 16 + sy1;
585 unsigned clr, ptype = 0;
587 for (; by < y * 16 + sy2 && by < avctx->height;) {
588 ret = decode_value(s, s->op_model[ptype], 6, 1000, &ptype);
592 ret = decode_unit(s, &s->pixel_model[0][cx + cx1], 400, &r);
596 cx1 = (cx << 6) & 0xFC0;
598 ret = decode_unit(s, &s->pixel_model[1][cx + cx1], 400, &g);
602 cx1 = (cx << 6) & 0xFC0;
604 ret = decode_unit(s, &s->pixel_model[2][cx + cx1], 400, &b);
608 clr = (b << 16) + (g << 8) + r;
611 return AVERROR_INVALIDDATA;
612 ret = decode_value(s, s->run_model[ptype], 256, 400, &run);
619 if (by >= avctx->height)
620 return AVERROR_INVALIDDATA;
622 dst[by * linesize + bx] = clr;
624 if (bx >= x * 16 + sx2 || bx >= avctx->width) {
634 return AVERROR_INVALIDDATA;
640 if (by >= avctx->height)
641 return AVERROR_INVALIDDATA;
643 clr = dst[by * linesize + bx - 1 - z];
644 dst[by * linesize + bx] = clr;
646 if (bx >= x * 16 + sx2 || bx >= avctx->width) {
654 if (by < 1 || by >= avctx->height)
655 return AVERROR_INVALIDDATA;
657 clr = dst[(by - 1) * linesize + bx];
658 dst[by * linesize + bx] = clr;
660 if (bx >= x * 16 + sx2 || bx >= avctx->width) {
668 if (by >= avctx->height)
669 return AVERROR_INVALIDDATA;
671 clr = prev[by * plinesize + bx];
672 dst[by * linesize + bx] = clr;
674 if (bx >= x * 16 + sx2 || bx >= avctx->width) {
682 uint8_t *odst = (uint8_t *)dst;
684 if (by < 1 || by >= avctx->height)
685 return AVERROR_INVALIDDATA;
689 return AVERROR_INVALIDDATA;
695 r = odst[((by - 1) * linesize + bx) * 4] +
696 odst[(by * linesize + bx - 1 - z) * 4] -
697 odst[((by - 1) * linesize + bx - 1 - z) * 4];
698 g = odst[((by - 1) * linesize + bx) * 4 + 1] +
699 odst[(by * linesize + bx - 1 - z) * 4 + 1] -
700 odst[((by - 1) * linesize + bx - 1 - z) * 4 + 1];
701 b = odst[((by - 1) * linesize + bx) * 4 + 2] +
702 odst[(by * linesize + bx - 1 - z) * 4 + 2] -
703 odst[((by - 1) * linesize + bx - 1 - z) * 4 + 2];
704 clr = ((b & 0xFF) << 16) + ((g & 0xFF) << 8) + (r & 0xFF);
705 dst[by * linesize + bx] = clr;
707 if (bx >= x * 16 + sx2 || bx >= avctx->width) {
715 if (by < 1 || by >= avctx->height)
716 return AVERROR_INVALIDDATA;
720 return AVERROR_INVALIDDATA;
726 clr = dst[(by - 1) * linesize + bx - 1 - z];
727 dst[by * linesize + bx] = clr;
729 if (bx >= x * 16 + sx2 || bx >= avctx->width) {
737 if (avctx->bits_per_coded_sample == 16) {
738 cx1 = (clr & 0x3F00) >> 2;
739 cx = (clr & 0x3FFFFF) >> 16;
741 cx1 = (clr & 0xFC00) >> 4;
742 cx = (clr & 0xFFFFFF) >> 18;
752 static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
755 SCPRContext *s = avctx->priv_data;
756 GetByteContext *gb = &s->gb;
757 AVFrame *frame = data;
760 if (avctx->bits_per_coded_sample == 16) {
761 if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
765 if ((ret = ff_reget_buffer(avctx, s->current_frame)) < 0)
768 bytestream2_init(gb, avpkt->data, avpkt->size);
770 type = bytestream2_peek_byte(gb);
773 s->get_freq = get_freq0;
775 frame->key_frame = 1;
776 ret = decompress_i(avctx, (uint32_t *)s->current_frame->data[0],
777 s->current_frame->linesize[0] / 4);
778 } else if (type == 18) {
779 s->get_freq = get_freq;
781 frame->key_frame = 1;
782 ret = decompress_i(avctx, (uint32_t *)s->current_frame->data[0],
783 s->current_frame->linesize[0] / 4);
784 } else if (type == 17) {
785 uint32_t clr, *dst = (uint32_t *)s->current_frame->data[0];
788 frame->key_frame = 1;
789 bytestream2_skip(gb, 1);
790 if (avctx->bits_per_coded_sample == 16) {
791 uint16_t value = bytestream2_get_le16(gb);
795 g = (value >> 5) & 31;
796 b = (value >> 10) & 31;
797 clr = (r << 16) + (g << 8) + b;
799 clr = bytestream2_get_le24(gb);
801 for (y = 0; y < avctx->height; y++) {
802 for (x = 0; x < avctx->width; x++) {
805 dst += s->current_frame->linesize[0] / 4;
807 } else if (type == 0 || type == 1) {
808 frame->key_frame = 0;
810 ret = av_frame_copy(s->current_frame, s->last_frame);
814 ret = decompress_p(avctx, (uint32_t *)s->current_frame->data[0],
815 s->current_frame->linesize[0] / 4,
816 (uint32_t *)s->last_frame->data[0],
817 s->last_frame->linesize[0] / 4);
819 return AVERROR_PATCHWELCOME;
825 if (avctx->bits_per_coded_sample != 16) {
826 ret = av_frame_ref(data, s->current_frame);
830 uint8_t *dst = frame->data[0];
833 ret = av_frame_copy(frame, s->current_frame);
837 // scale up each sample by 8
838 for (y = 0; y < avctx->height; y++) {
839 // If the image is sufficiently aligned, compute 8 samples at once
840 if (!(((uintptr_t)dst) & 7)) {
841 uint64_t *dst64 = (uint64_t *)dst;
842 int w = avctx->width>>1;
843 for (x = 0; x < w; x++) {
844 dst64[x] = (dst64[x] << 3) & 0xFCFCFCFCFCFCFCFCULL;
849 for (; x < avctx->width * 4; x++) {
850 dst[x] = dst[x] << 3;
852 dst += frame->linesize[0];
856 frame->pict_type = frame->key_frame ? AV_PICTURE_TYPE_I : AV_PICTURE_TYPE_P;
858 FFSWAP(AVFrame *, s->current_frame, s->last_frame);
860 frame->data[0] += frame->linesize[0] * (avctx->height - 1);
861 frame->linesize[0] *= -1;
868 static av_cold int decode_init(AVCodecContext *avctx)
870 SCPRContext *s = avctx->priv_data;
872 switch (avctx->bits_per_coded_sample) {
873 case 16: avctx->pix_fmt = AV_PIX_FMT_RGB0; break;
875 case 32: avctx->pix_fmt = AV_PIX_FMT_BGR0; break;
877 av_log(avctx, AV_LOG_ERROR, "Unsupported bitdepth %i\n", avctx->bits_per_coded_sample);
878 return AVERROR_INVALIDDATA;
881 s->get_freq = get_freq0;
884 s->cxshift = avctx->bits_per_coded_sample == 16 ? 0 : 2;
885 s->cbits = avctx->bits_per_coded_sample == 16 ? 0x1F : 0xFF;
886 s->nbx = (avctx->width + 15) / 16;
887 s->nby = (avctx->height + 15) / 16;
888 s->nbcount = s->nbx * s->nby;
889 s->blocks = av_malloc_array(s->nbcount, sizeof(*s->blocks));
891 return AVERROR(ENOMEM);
893 s->last_frame = av_frame_alloc();
894 s->current_frame = av_frame_alloc();
895 if (!s->last_frame || !s->current_frame)
896 return AVERROR(ENOMEM);
901 static av_cold int decode_close(AVCodecContext *avctx)
903 SCPRContext *s = avctx->priv_data;
905 av_freep(&s->blocks);
906 av_frame_free(&s->last_frame);
907 av_frame_free(&s->current_frame);
912 AVCodec ff_scpr_decoder = {
914 .long_name = NULL_IF_CONFIG_SMALL("ScreenPressor"),
915 .type = AVMEDIA_TYPE_VIDEO,
916 .id = AV_CODEC_ID_SCPR,
917 .priv_data_size = sizeof(SCPRContext),
919 .close = decode_close,
920 .decode = decode_frame,
921 .capabilities = AV_CODEC_CAP_DR1,
922 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE |
923 FF_CODEC_CAP_INIT_CLEANUP,