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 void (*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 void 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;
145 static int get_freq(RangeCoder *rc, unsigned total_freq, unsigned *freq)
148 return AVERROR_INVALIDDATA;
150 rc->range = rc->range / total_freq;
153 return AVERROR_INVALIDDATA;
155 *freq = rc->code / rc->range;
160 static void decode0(GetByteContext *gb, RangeCoder *rc, unsigned cumFreq, unsigned freq, unsigned total_freq)
162 int t = rc->range * (uint64_t)cumFreq / total_freq;
165 rc->range = rc->range * (uint64_t)(freq + cumFreq) / total_freq - (t + 1);
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;
175 static int get_freq0(RangeCoder *rc, unsigned total_freq, unsigned *freq)
178 return AVERROR_INVALIDDATA;
180 *freq = total_freq * (uint64_t)(rc->code - rc->code1) / rc->range;
185 static int decode_value(SCPRContext *s, unsigned *cnt, unsigned maxc, unsigned step, unsigned *rval)
187 GetByteContext *gb = &s->gb;
188 RangeCoder *rc = &s->rc;
189 unsigned totfr = cnt[maxc];
191 unsigned c = 0, cumfr = 0, cnt_c = 0;
194 if ((ret = s->get_freq(rc, totfr, &value)) < 0)
199 if (value >= cumfr + cnt_c)
205 s->decode(gb, rc, cumfr, cnt_c, totfr);
207 cnt[c] = cnt_c + step;
211 for (i = 0; i < maxc; i++) {
212 unsigned nc = (cnt[i] >> 1) + 1;
224 static int decode_unit(SCPRContext *s, PixelModel *pixel, unsigned step, unsigned *rval)
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;
232 if ((ret = s->get_freq(rc, totfr, &value)) < 0)
236 cnt_x = pixel->lookup[x];
237 if (value >= cumfr + cnt_x)
247 cnt_c = pixel->freq[c];
248 if (value >= cumfr + cnt_c)
254 s->decode(gb, rc, cumfr, cnt_c, totfr);
255 pixel->freq[c] = cnt_c + step;
256 pixel->lookup[x] = cnt_x + step;
260 for (i = 0; i < 256; i++) {
261 unsigned nc = (pixel->freq[i] >> 1) + 1;
265 for (i = 0; i < 16; i++) {
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;
273 pixel->total_freq = totfr;
275 *rval = c & s->cbits;
280 static int decompress_i(AVCodecContext *avctx, uint32_t *dst, int linesize)
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;
290 bytestream2_skip(gb, 2);
291 init_rangecoder(&s->rc, gb);
293 while (k < avctx->width + 1) {
294 ret = decode_unit(s, &s->pixel_model[0][cx + cx1], 400, &r);
298 cx1 = (cx << 6) & 0xFC0;
300 ret = decode_unit(s, &s->pixel_model[1][cx + cx1], 400, &g);
304 cx1 = (cx << 6) & 0xFC0;
306 ret = decode_unit(s, &s->pixel_model[2][cx + cx1], 400, &b);
310 cx1 = (cx << 6) & 0xFC0;
313 ret = decode_value(s, s->run_model[0], 256, 400, &run);
317 clr = (b << 16) + (g << 8) + r;
320 dst[y * linesize + x] = clr;
324 if (x >= avctx->width) {
333 while (x < avctx->width && y < avctx->height) {
334 ret = decode_value(s, s->op_model[ptype], 6, 1000, &ptype);
338 ret = decode_unit(s, &s->pixel_model[0][cx + cx1], 400, &r);
342 cx1 = (cx << 6) & 0xFC0;
344 ret = decode_unit(s, &s->pixel_model[1][cx + cx1], 400, &g);
348 cx1 = (cx << 6) & 0xFC0;
350 ret = decode_unit(s, &s->pixel_model[2][cx + cx1], 400, &b);
354 clr = (b << 16) + (g << 8) + r;
357 return AVERROR_INVALIDDATA;
358 ret = decode_value(s, s->run_model[ptype], 256, 400, &run);
365 if (y >= avctx->height)
366 return AVERROR_INVALIDDATA;
368 dst[y * linesize + x] = clr;
372 if (x >= avctx->width) {
380 if (y >= avctx->height)
381 return AVERROR_INVALIDDATA;
383 dst[y * linesize + x] = dst[ly * linesize + lx];
387 if (x >= avctx->width) {
392 clr = dst[ly * linesize + lx];
396 if (y < 1 || y >= avctx->height)
397 return AVERROR_INVALIDDATA;
399 clr = dst[y * linesize + x + off + 1];
400 dst[y * linesize + x] = clr;
404 if (x >= avctx->width) {
412 uint8_t *odst = (uint8_t *)dst;
414 if (y < 1 || y >= avctx->height)
415 return AVERROR_INVALIDDATA;
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;
431 if (x >= avctx->width) {
439 if (y < 1 || y >= avctx->height)
440 return AVERROR_INVALIDDATA;
442 clr = dst[y * linesize + x + off];
443 dst[y * linesize + x] = clr;
447 if (x >= avctx->width) {
455 if (avctx->bits_per_coded_sample == 16) {
456 cx1 = (clr & 0x3F00) >> 2;
457 cx = (clr & 0xFFFFFF) >> 16;
459 cx1 = (clr & 0xFC00) >> 4;
460 cx = (clr & 0xFFFFFF) >> 18;
467 static int decompress_p(AVCodecContext *avctx,
468 uint32_t *dst, int linesize,
469 uint32_t *prev, int plinesize)
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;
477 if (bytestream2_get_byte(gb) == 0)
479 bytestream2_skip(gb, 1);
480 init_rangecoder(&s->rc, gb);
482 ret = decode_value(s, s->range_model, 256, 1, &min);
483 ret |= decode_value(s, s->range_model, 256, 1, &temp);
485 ret |= decode_value(s, s->range_model, 256, 1, &max);
486 ret |= decode_value(s, s->range_model, 256, 1, &temp);
491 memset(s->blocks, 0, sizeof(*s->blocks) * s->nbcount);
496 ret = decode_value(s, s->fill_model, 5, 10, &fill);
497 ret |= decode_value(s, s->count_model, 256, 20, &count);
501 while (min < s->nbcount && count-- > 0) {
502 s->blocks[min++] = fill;
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;
510 if (s->blocks[y * s->nbx + x] == 0)
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);
524 if (((s->blocks[y * s->nbx + x] - 1) & 2) > 0) {
525 int i, j, by = y * 16, bx = x * 16;
528 ret = decode_value(s, s->mv_model[0], 512, 100, &mvx);
529 ret |= decode_value(s, s->mv_model[1], 512, 100, &mvy);
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;
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];
546 int run, r, g, b, z, bx = x * 16 + sx1, by = y * 16 + sy1;
547 unsigned clr, ptype = 0;
549 for (; by < y * 16 + sy2 && by < avctx->height;) {
550 ret = decode_value(s, s->op_model[ptype], 6, 1000, &ptype);
552 ret = decode_unit(s, &s->pixel_model[0][cx + cx1], 400, &r);
556 cx1 = (cx << 6) & 0xFC0;
558 ret = decode_unit(s, &s->pixel_model[1][cx + cx1], 400, &g);
562 cx1 = (cx << 6) & 0xFC0;
564 ret = decode_unit(s, &s->pixel_model[2][cx + cx1], 400, &b);
568 clr = (b << 16) + (g << 8) + r;
571 return AVERROR_INVALIDDATA;
572 ret = decode_value(s, s->run_model[ptype], 256, 400, &run);
579 if (by >= avctx->height)
580 return AVERROR_INVALIDDATA;
582 dst[by * linesize + bx] = clr;
584 if (bx >= x * 16 + sx2 || bx >= avctx->width) {
594 return AVERROR_INVALIDDATA;
600 if (by >= avctx->height)
601 return AVERROR_INVALIDDATA;
603 clr = dst[by * linesize + bx - 1 - z];
604 dst[by * linesize + bx] = clr;
606 if (bx >= x * 16 + sx2 || bx >= avctx->width) {
614 if (by < 1 || by >= avctx->height)
615 return AVERROR_INVALIDDATA;
617 clr = dst[(by - 1) * linesize + bx];
618 dst[by * linesize + bx] = clr;
620 if (bx >= x * 16 + sx2 || bx >= avctx->width) {
628 if (by >= avctx->height)
629 return AVERROR_INVALIDDATA;
631 clr = prev[by * linesize + bx];
632 dst[by * linesize + bx] = clr;
634 if (bx >= x * 16 + sx2 || bx >= avctx->width) {
642 uint8_t *odst = (uint8_t *)dst;
644 if (by < 1 || by >= avctx->height)
645 return AVERROR_INVALIDDATA;
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;
665 if (bx >= x * 16 + sx2 || bx >= avctx->width) {
673 if (by < 1 || by >= avctx->height)
674 return AVERROR_INVALIDDATA;
682 clr = dst[(by - 1) * linesize + bx - 1 - z];
683 dst[by * linesize + bx] = clr;
685 if (bx >= x * 16 + sx2 || bx >= avctx->width) {
693 if (avctx->bits_per_coded_sample == 16) {
694 cx1 = (clr & 0x3F00) >> 2;
695 cx = (clr & 0xFFFFFF) >> 16;
697 cx1 = (clr & 0xFC00) >> 4;
698 cx = (clr & 0xFFFFFF) >> 18;
708 static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
711 SCPRContext *s = avctx->priv_data;
712 GetByteContext *gb = &s->gb;
713 AVFrame *frame = data;
716 if (avctx->bits_per_coded_sample == 16) {
717 if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
721 if ((ret = ff_reget_buffer(avctx, s->current_frame)) < 0)
724 bytestream2_init(gb, avpkt->data, avpkt->size);
726 type = bytestream2_peek_byte(gb);
729 s->get_freq = get_freq0;
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;
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];
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);
751 g = (value >> 5) & 31;
752 b = (value >> 10) & 31;
753 clr = (r << 16) + (g << 8) + b;
755 clr = bytestream2_get_le24(gb);
757 for (y = 0; y < avctx->height; y++) {
758 for (x = 0; x < avctx->width; x++) {
761 dst += s->current_frame->linesize[0] / 4;
763 } else if (type == 0 || type == 1) {
764 frame->key_frame = 0;
766 ret = av_frame_copy(s->current_frame, s->last_frame);
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);
775 return AVERROR_PATCHWELCOME;
781 if (avctx->bits_per_coded_sample != 16) {
782 ret = av_frame_ref(data, s->current_frame);
786 uint8_t *dst = frame->data[0];
789 ret = av_frame_copy(frame, s->current_frame);
793 for (y = 0; y < avctx->height; y++) {
794 for (x = 0; x < avctx->width * 4; x++) {
795 dst[x] = dst[x] << 3;
797 dst += frame->linesize[0];
801 frame->pict_type = frame->key_frame ? AV_PICTURE_TYPE_I : AV_PICTURE_TYPE_P;
803 FFSWAP(AVFrame *, s->current_frame, s->last_frame);
805 frame->data[0] += frame->linesize[0] * (avctx->height - 1);
806 frame->linesize[0] *= -1;
813 static av_cold int decode_init(AVCodecContext *avctx)
815 SCPRContext *s = avctx->priv_data;
817 switch (avctx->bits_per_coded_sample) {
818 case 16: avctx->pix_fmt = AV_PIX_FMT_RGB0; break;
820 case 32: avctx->pix_fmt = AV_PIX_FMT_BGR0; break;
822 av_log(avctx, AV_LOG_ERROR, "Unsupported bitdepth %i\n", avctx->bits_per_coded_sample);
823 return AVERROR_INVALIDDATA;
826 s->get_freq = get_freq0;
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));
836 return AVERROR(ENOMEM);
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);
846 static av_cold int decode_close(AVCodecContext *avctx)
848 SCPRContext *s = avctx->priv_data;
850 av_freep(&s->blocks);
851 av_frame_free(&s->last_frame);
852 av_frame_free(&s->current_frame);
857 AVCodec ff_scpr_decoder = {
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),
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,