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)
214 if ((ret = s->decode(gb, rc, cumfr, cnt_c, totfr)) < 0)
217 cnt[c] = cnt_c + step;
221 for (i = 0; i < maxc; i++) {
222 unsigned nc = (cnt[i] >> 1) + 1;
234 static int decode_unit(SCPRContext *s, PixelModel *pixel, unsigned step, unsigned *rval)
236 GetByteContext *gb = &s->gb;
237 RangeCoder *rc = &s->rc;
238 unsigned totfr = pixel->total_freq;
239 unsigned value, x = 0, cumfr = 0, cnt_x = 0;
240 int i, j, ret, c, cnt_c;
242 if ((ret = s->get_freq(rc, totfr, &value)) < 0)
246 cnt_x = pixel->lookup[x];
247 if (value >= cumfr + cnt_x)
257 cnt_c = pixel->freq[c];
258 if (value >= cumfr + cnt_c)
264 if (x >= 16 || c >= 256) {
265 return AVERROR_INVALIDDATA;
268 if ((ret = s->decode(gb, rc, cumfr, cnt_c, totfr)) < 0)
271 pixel->freq[c] = cnt_c + step;
272 pixel->lookup[x] = cnt_x + step;
276 for (i = 0; i < 256; i++) {
277 unsigned nc = (pixel->freq[i] >> 1) + 1;
281 for (i = 0; i < 16; i++) {
283 unsigned i16_17 = i << 4;
284 for (j = 0; j < 16; j++)
285 sum += pixel->freq[i16_17 + j];
286 pixel->lookup[i] = sum;
289 pixel->total_freq = totfr;
291 *rval = c & s->cbits;
296 static int decompress_i(AVCodecContext *avctx, uint32_t *dst, int linesize)
298 SCPRContext *s = avctx->priv_data;
299 GetByteContext *gb = &s->gb;
300 int cx = 0, cx1 = 0, k = 0, clr = 0;
301 int run, r, g, b, off, y = 0, x = 0, z, ret;
302 unsigned backstep = linesize - avctx->width;
303 const int cxshift = s->cxshift;
304 unsigned lx, ly, ptype;
307 bytestream2_skip(gb, 2);
308 init_rangecoder(&s->rc, gb);
310 while (k < avctx->width + 1) {
311 ret = decode_unit(s, &s->pixel_model[0][cx + cx1], 400, &r);
315 cx1 = (cx << 6) & 0xFC0;
317 ret = decode_unit(s, &s->pixel_model[1][cx + cx1], 400, &g);
321 cx1 = (cx << 6) & 0xFC0;
323 ret = decode_unit(s, &s->pixel_model[2][cx + cx1], 400, &b);
327 cx1 = (cx << 6) & 0xFC0;
330 ret = decode_value(s, s->run_model[0], 256, 400, &run);
334 clr = (b << 16) + (g << 8) + r;
337 if (y >= avctx->height)
338 return AVERROR_INVALIDDATA;
340 dst[y * linesize + x] = clr;
344 if (x >= avctx->width) {
353 while (x < avctx->width && y < avctx->height) {
354 ret = decode_value(s, s->op_model[ptype], 6, 1000, &ptype);
358 ret = decode_unit(s, &s->pixel_model[0][cx + cx1], 400, &r);
362 cx1 = (cx << 6) & 0xFC0;
364 ret = decode_unit(s, &s->pixel_model[1][cx + cx1], 400, &g);
368 cx1 = (cx << 6) & 0xFC0;
370 ret = decode_unit(s, &s->pixel_model[2][cx + cx1], 400, &b);
374 clr = (b << 16) + (g << 8) + r;
377 return AVERROR_INVALIDDATA;
378 ret = decode_value(s, s->run_model[ptype], 256, 400, &run);
385 if (y >= avctx->height)
386 return AVERROR_INVALIDDATA;
388 dst[y * linesize + x] = clr;
392 if (x >= avctx->width) {
400 if (y >= avctx->height)
401 return AVERROR_INVALIDDATA;
403 dst[y * linesize + x] = dst[ly * linesize + lx];
407 if (x >= avctx->width) {
412 clr = dst[ly * linesize + lx];
416 if (y < 1 || y >= avctx->height)
417 return AVERROR_INVALIDDATA;
419 clr = dst[y * linesize + x + off + 1];
420 dst[y * linesize + x] = clr;
424 if (x >= avctx->width) {
432 uint8_t *odst = (uint8_t *)dst;
434 if (y < 1 || y >= avctx->height ||
436 return AVERROR_INVALIDDATA;
444 r = odst[(ly * linesize + lx) * 4] +
445 odst[((y * linesize + x) + off - z) * 4 + 4] -
446 odst[((y * linesize + x) + off - z) * 4];
447 g = odst[(ly * linesize + lx) * 4 + 1] +
448 odst[((y * linesize + x) + off - z) * 4 + 5] -
449 odst[((y * linesize + x) + off - z) * 4 + 1];
450 b = odst[(ly * linesize + lx) * 4 + 2] +
451 odst[((y * linesize + x) + off - z) * 4 + 6] -
452 odst[((y * linesize + x) + off - z) * 4 + 2];
453 clr = ((b & 0xFF) << 16) + ((g & 0xFF) << 8) + (r & 0xFF);
454 dst[y * linesize + x] = clr;
458 if (x >= avctx->width) {
466 if (y < 1 || y >= avctx->height ||
468 return AVERROR_INVALIDDATA;
476 clr = dst[y * linesize + x + off - z];
477 dst[y * linesize + x] = clr;
481 if (x >= avctx->width) {
489 if (avctx->bits_per_coded_sample == 16) {
490 cx1 = (clr & 0x3F00) >> 2;
491 cx = (clr & 0x3FFFFF) >> 16;
493 cx1 = (clr & 0xFC00) >> 4;
494 cx = (clr & 0xFFFFFF) >> 18;
501 static int decompress_p(AVCodecContext *avctx,
502 uint32_t *dst, int linesize,
503 uint32_t *prev, int plinesize)
505 SCPRContext *s = avctx->priv_data;
506 GetByteContext *gb = &s->gb;
507 int ret, temp, min, max, x, y, cx = 0, cx1 = 0;
508 int backstep = linesize - avctx->width;
509 const int cxshift = s->cxshift;
511 if (bytestream2_get_byte(gb) == 0)
513 bytestream2_skip(gb, 1);
514 init_rangecoder(&s->rc, gb);
516 ret = decode_value(s, s->range_model, 256, 1, &min);
517 ret |= decode_value(s, s->range_model, 256, 1, &temp);
519 ret |= decode_value(s, s->range_model, 256, 1, &max);
520 ret |= decode_value(s, s->range_model, 256, 1, &temp);
525 memset(s->blocks, 0, sizeof(*s->blocks) * s->nbcount);
530 ret = decode_value(s, s->fill_model, 5, 10, &fill);
531 ret |= decode_value(s, s->count_model, 256, 20, &count);
535 while (min < s->nbcount && count-- > 0) {
536 s->blocks[min++] = fill;
540 for (y = 0; y < s->nby; y++) {
541 for (x = 0; x < s->nbx; x++) {
542 int sy1 = 0, sy2 = 16, sx1 = 0, sx2 = 16;
544 if (s->blocks[y * s->nbx + x] == 0)
547 if (((s->blocks[y * s->nbx + x] - 1) & 1) > 0) {
548 ret = decode_value(s, s->sxy_model[0], 16, 100, &sx1);
549 ret |= decode_value(s, s->sxy_model[1], 16, 100, &sy1);
550 ret |= decode_value(s, s->sxy_model[2], 16, 100, &sx2);
551 ret |= decode_value(s, s->sxy_model[3], 16, 100, &sy2);
558 if (((s->blocks[y * s->nbx + x] - 1) & 2) > 0) {
559 int i, j, by = y * 16, bx = x * 16;
562 ret = decode_value(s, s->mv_model[0], 512, 100, &mvx);
563 ret |= decode_value(s, s->mv_model[1], 512, 100, &mvy);
570 if (by + mvy + sy1 < 0 || bx + mvx + sx1 < 0 ||
571 by + mvy + sy1 >= avctx->height || bx + mvx + sx1 >= avctx->width)
572 return AVERROR_INVALIDDATA;
574 for (i = 0; i < sy2 - sy1 && (by + sy1 + i) < avctx->height && (by + mvy + sy1 + i) < avctx->height; i++) {
575 for (j = 0; j < sx2 - sx1 && (bx + sx1 + j) < avctx->width && (bx + mvx + sx1 + j) < avctx->width; j++) {
576 dst[(by + i + sy1) * linesize + bx + sx1 + j] = prev[(by + mvy + sy1 + i) * plinesize + bx + sx1 + mvx + j];
580 int run, r, g, b, z, bx = x * 16 + sx1, by = y * 16 + sy1;
581 unsigned clr, ptype = 0;
583 for (; by < y * 16 + sy2 && by < avctx->height;) {
584 ret = decode_value(s, s->op_model[ptype], 6, 1000, &ptype);
586 ret = decode_unit(s, &s->pixel_model[0][cx + cx1], 400, &r);
590 cx1 = (cx << 6) & 0xFC0;
592 ret = decode_unit(s, &s->pixel_model[1][cx + cx1], 400, &g);
596 cx1 = (cx << 6) & 0xFC0;
598 ret = decode_unit(s, &s->pixel_model[2][cx + cx1], 400, &b);
602 clr = (b << 16) + (g << 8) + r;
605 return AVERROR_INVALIDDATA;
606 ret = decode_value(s, s->run_model[ptype], 256, 400, &run);
613 if (by >= avctx->height)
614 return AVERROR_INVALIDDATA;
616 dst[by * linesize + bx] = clr;
618 if (bx >= x * 16 + sx2 || bx >= avctx->width) {
628 return AVERROR_INVALIDDATA;
634 if (by >= avctx->height)
635 return AVERROR_INVALIDDATA;
637 clr = dst[by * linesize + bx - 1 - z];
638 dst[by * linesize + bx] = clr;
640 if (bx >= x * 16 + sx2 || bx >= avctx->width) {
648 if (by < 1 || by >= avctx->height)
649 return AVERROR_INVALIDDATA;
651 clr = dst[(by - 1) * linesize + bx];
652 dst[by * linesize + bx] = clr;
654 if (bx >= x * 16 + sx2 || bx >= avctx->width) {
662 if (by >= avctx->height)
663 return AVERROR_INVALIDDATA;
665 clr = prev[by * plinesize + bx];
666 dst[by * linesize + bx] = clr;
668 if (bx >= x * 16 + sx2 || bx >= avctx->width) {
676 uint8_t *odst = (uint8_t *)dst;
678 if (by < 1 || by >= avctx->height)
679 return AVERROR_INVALIDDATA;
687 r = odst[((by - 1) * linesize + bx) * 4] +
688 odst[(by * linesize + bx - 1 - z) * 4] -
689 odst[((by - 1) * linesize + bx - 1 - z) * 4];
690 g = odst[((by - 1) * linesize + bx) * 4 + 1] +
691 odst[(by * linesize + bx - 1 - z) * 4 + 1] -
692 odst[((by - 1) * linesize + bx - 1 - z) * 4 + 1];
693 b = odst[((by - 1) * linesize + bx) * 4 + 2] +
694 odst[(by * linesize + bx - 1 - z) * 4 + 2] -
695 odst[((by - 1) * linesize + bx - 1 - z) * 4 + 2];
696 clr = ((b & 0xFF) << 16) + ((g & 0xFF) << 8) + (r & 0xFF);
697 dst[by * linesize + bx] = clr;
699 if (bx >= x * 16 + sx2 || bx >= avctx->width) {
707 if (by < 1 || by >= avctx->height)
708 return AVERROR_INVALIDDATA;
716 clr = dst[(by - 1) * linesize + bx - 1 - z];
717 dst[by * linesize + bx] = clr;
719 if (bx >= x * 16 + sx2 || bx >= avctx->width) {
727 if (avctx->bits_per_coded_sample == 16) {
728 cx1 = (clr & 0x3F00) >> 2;
729 cx = (clr & 0x3FFFFF) >> 16;
731 cx1 = (clr & 0xFC00) >> 4;
732 cx = (clr & 0xFFFFFF) >> 18;
742 static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
745 SCPRContext *s = avctx->priv_data;
746 GetByteContext *gb = &s->gb;
747 AVFrame *frame = data;
750 if (avctx->bits_per_coded_sample == 16) {
751 if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
755 if ((ret = ff_reget_buffer(avctx, s->current_frame)) < 0)
758 bytestream2_init(gb, avpkt->data, avpkt->size);
760 type = bytestream2_peek_byte(gb);
763 s->get_freq = get_freq0;
765 frame->key_frame = 1;
766 ret = decompress_i(avctx, (uint32_t *)s->current_frame->data[0],
767 s->current_frame->linesize[0] / 4);
768 } else if (type == 18) {
769 s->get_freq = get_freq;
771 frame->key_frame = 1;
772 ret = decompress_i(avctx, (uint32_t *)s->current_frame->data[0],
773 s->current_frame->linesize[0] / 4);
774 } else if (type == 17) {
775 uint32_t clr, *dst = (uint32_t *)s->current_frame->data[0];
778 frame->key_frame = 1;
779 bytestream2_skip(gb, 1);
780 if (avctx->bits_per_coded_sample == 16) {
781 uint16_t value = bytestream2_get_le16(gb);
785 g = (value >> 5) & 31;
786 b = (value >> 10) & 31;
787 clr = (r << 16) + (g << 8) + b;
789 clr = bytestream2_get_le24(gb);
791 for (y = 0; y < avctx->height; y++) {
792 for (x = 0; x < avctx->width; x++) {
795 dst += s->current_frame->linesize[0] / 4;
797 } else if (type == 0 || type == 1) {
798 frame->key_frame = 0;
800 ret = av_frame_copy(s->current_frame, s->last_frame);
804 ret = decompress_p(avctx, (uint32_t *)s->current_frame->data[0],
805 s->current_frame->linesize[0] / 4,
806 (uint32_t *)s->last_frame->data[0],
807 s->last_frame->linesize[0] / 4);
809 return AVERROR_PATCHWELCOME;
815 if (avctx->bits_per_coded_sample != 16) {
816 ret = av_frame_ref(data, s->current_frame);
820 uint8_t *dst = frame->data[0];
823 ret = av_frame_copy(frame, s->current_frame);
827 for (y = 0; y < avctx->height; y++) {
828 for (x = 0; x < avctx->width * 4; x++) {
829 dst[x] = dst[x] << 3;
831 dst += frame->linesize[0];
835 frame->pict_type = frame->key_frame ? AV_PICTURE_TYPE_I : AV_PICTURE_TYPE_P;
837 FFSWAP(AVFrame *, s->current_frame, s->last_frame);
839 frame->data[0] += frame->linesize[0] * (avctx->height - 1);
840 frame->linesize[0] *= -1;
847 static av_cold int decode_init(AVCodecContext *avctx)
849 SCPRContext *s = avctx->priv_data;
851 switch (avctx->bits_per_coded_sample) {
852 case 16: avctx->pix_fmt = AV_PIX_FMT_RGB0; break;
854 case 32: avctx->pix_fmt = AV_PIX_FMT_BGR0; break;
856 av_log(avctx, AV_LOG_ERROR, "Unsupported bitdepth %i\n", avctx->bits_per_coded_sample);
857 return AVERROR_INVALIDDATA;
860 s->get_freq = get_freq0;
863 s->cxshift = avctx->bits_per_coded_sample == 16 ? 0 : 2;
864 s->cbits = avctx->bits_per_coded_sample == 16 ? 0x1F : 0xFF;
865 s->nbx = (avctx->width + 15) / 16;
866 s->nby = (avctx->height + 15) / 16;
867 s->nbcount = s->nbx * s->nby;
868 s->blocks = av_malloc_array(s->nbcount, sizeof(*s->blocks));
870 return AVERROR(ENOMEM);
872 s->last_frame = av_frame_alloc();
873 s->current_frame = av_frame_alloc();
874 if (!s->last_frame || !s->current_frame)
875 return AVERROR(ENOMEM);
880 static av_cold int decode_close(AVCodecContext *avctx)
882 SCPRContext *s = avctx->priv_data;
884 av_freep(&s->blocks);
885 av_frame_free(&s->last_frame);
886 av_frame_free(&s->current_frame);
891 AVCodec ff_scpr_decoder = {
893 .long_name = NULL_IF_CONFIG_SMALL("ScreenPressor"),
894 .type = AVMEDIA_TYPE_VIDEO,
895 .id = AV_CODEC_ID_SCPR,
896 .priv_data_size = sizeof(SCPRContext),
898 .close = decode_close,
899 .decode = decode_frame,
900 .capabilities = AV_CODEC_CAP_DR1,
901 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE |
902 FF_CODEC_CAP_INIT_CLEANUP,