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"
33 #define TOP 0x01000000
38 static void init_rangecoder(RangeCoder *rc, GetByteContext *gb)
41 rc->range = 0xFFFFFFFFU;
42 rc->code = bytestream2_get_be32(gb);
45 static void reinit_tables(SCPRContext *s)
49 for (comp = 0; comp < 3; comp++) {
50 for (j = 0; j < 4096; j++) {
51 if (s->pixel_model[comp][j].total_freq != 256) {
52 for (i = 0; i < 256; i++)
53 s->pixel_model[comp][j].freq[i] = 1;
54 for (i = 0; i < 16; i++)
55 s->pixel_model[comp][j].lookup[i] = 16;
56 s->pixel_model[comp][j].total_freq = 256;
61 for (j = 0; j < 6; j++) {
62 uint32_t *p = s->run_model[j];
63 for (i = 0; i < 256; i++)
68 for (j = 0; j < 6; j++) {
69 uint32_t *op = s->op_model[j];
70 for (i = 0; i < 6; i++)
75 for (i = 0; i < 256; i++) {
76 s->range_model[i] = 1;
77 s->count_model[i] = 1;
79 s->range_model[256] = 256;
80 s->count_model[256] = 256;
82 for (i = 0; i < 5; i++) {
87 for (j = 0; j < 4; j++) {
88 for (i = 0; i < 16; i++) {
89 s->sxy_model[j][i] = 1;
91 s->sxy_model[j][16] = 16;
94 for (i = 0; i < 512; i++) {
95 s->mv_model[0][i] = 1;
96 s->mv_model[1][i] = 1;
98 s->mv_model[0][512] = 512;
99 s->mv_model[1][512] = 512;
102 static int decode(GetByteContext *gb, RangeCoder *rc, uint32_t cumFreq, uint32_t freq, uint32_t total_freq)
104 rc->code -= cumFreq * rc->range;
107 while (rc->range < TOP && bytestream2_get_bytes_left(gb) > 0) {
108 uint32_t byte = bytestream2_get_byteu(gb);
109 rc->code = (rc->code << 8) | byte;
116 static int get_freq(RangeCoder *rc, uint32_t total_freq, uint32_t *freq)
119 return AVERROR_INVALIDDATA;
121 rc->range = rc->range / total_freq;
124 return AVERROR_INVALIDDATA;
126 *freq = rc->code / rc->range;
131 static int decode0(GetByteContext *gb, RangeCoder *rc, uint32_t cumFreq, uint32_t freq, uint32_t total_freq)
136 return AVERROR_INVALIDDATA;
138 t = rc->range * (uint64_t)cumFreq / total_freq;
141 rc->range = rc->range * (uint64_t)(freq + cumFreq) / total_freq - (t + 1);
143 while (rc->range < TOP && bytestream2_get_bytes_left(gb) > 0) {
144 uint32_t byte = bytestream2_get_byteu(gb);
145 rc->code = (rc->code << 8) | byte;
153 static int get_freq0(RangeCoder *rc, uint32_t total_freq, uint32_t *freq)
156 return AVERROR_INVALIDDATA;
158 *freq = total_freq * (uint64_t)(rc->code - rc->code1) / rc->range;
163 static int decode_value(SCPRContext *s, uint32_t *cnt, uint32_t maxc, uint32_t step, uint32_t *rval)
165 GetByteContext *gb = &s->gb;
166 RangeCoder *rc = &s->rc;
167 uint32_t totfr = cnt[maxc];
169 uint32_t c = 0, cumfr = 0, cnt_c = 0;
172 if ((ret = s->get_freq(rc, totfr, &value)) < 0)
177 if (value >= cumfr + cnt_c)
185 return AVERROR_INVALIDDATA;
187 if ((ret = s->decode(gb, rc, cumfr, cnt_c, totfr)) < 0)
190 cnt[c] = cnt_c + step;
194 for (i = 0; i < maxc; i++) {
195 uint32_t nc = (cnt[i] >> 1) + 1;
207 static int decode_unit(SCPRContext *s, PixelModel *pixel, uint32_t step, uint32_t *rval)
209 GetByteContext *gb = &s->gb;
210 RangeCoder *rc = &s->rc;
211 uint32_t totfr = pixel->total_freq;
212 uint32_t value, x = 0, cumfr = 0, cnt_x = 0;
213 int i, j, ret, c, cnt_c;
215 if ((ret = s->get_freq(rc, totfr, &value)) < 0)
219 cnt_x = pixel->lookup[x];
220 if (value >= cumfr + cnt_x)
230 cnt_c = pixel->freq[c];
231 if (value >= cumfr + cnt_c)
237 if (x >= 16 || c >= 256) {
238 return AVERROR_INVALIDDATA;
241 if ((ret = s->decode(gb, rc, cumfr, cnt_c, totfr)) < 0)
244 pixel->freq[c] = cnt_c + step;
245 pixel->lookup[x] = cnt_x + step;
249 for (i = 0; i < 256; i++) {
250 uint32_t nc = (pixel->freq[i] >> 1) + 1;
254 for (i = 0; i < 16; i++) {
256 uint32_t i16_17 = i << 4;
257 for (j = 0; j < 16; j++)
258 sum += pixel->freq[i16_17 + j];
259 pixel->lookup[i] = sum;
262 pixel->total_freq = totfr;
264 *rval = c & s->cbits;
269 static int decode_units(SCPRContext *s, uint32_t *r, uint32_t *g, uint32_t *b,
272 const int cxshift = s->cxshift;
275 ret = decode_unit(s, &s->pixel_model[0][*cx + *cx1], 400, r);
279 *cx1 = (*cx << 6) & 0xFC0;
281 ret = decode_unit(s, &s->pixel_model[1][*cx + *cx1], 400, g);
285 *cx1 = (*cx << 6) & 0xFC0;
287 ret = decode_unit(s, &s->pixel_model[2][*cx + *cx1], 400, b);
291 *cx1 = (*cx << 6) & 0xFC0;
297 static int decompress_i(AVCodecContext *avctx, uint32_t *dst, int linesize)
299 SCPRContext *s = avctx->priv_data;
300 GetByteContext *gb = &s->gb;
301 int cx = 0, cx1 = 0, k = 0;
302 int run, off, y = 0, x = 0, ret;
303 uint32_t clr = 0, r, g, b, backstep = linesize - avctx->width;
304 uint32_t lx, ly, ptype;
307 bytestream2_skip(gb, 2);
308 init_rangecoder(&s->rc, gb);
310 while (k < avctx->width + 1) {
311 ret = decode_units(s, &r, &g, &b, &cx, &cx1);
315 ret = decode_value(s, s->run_model[0], 256, 400, &run);
319 return AVERROR_INVALIDDATA;
321 clr = (b << 16) + (g << 8) + r;
324 if (y >= avctx->height)
325 return AVERROR_INVALIDDATA;
327 dst[y * linesize + x] = clr;
331 if (x >= avctx->width) {
340 while (x < avctx->width && y < avctx->height) {
341 ret = decode_value(s, s->op_model[ptype], 6, 1000, &ptype);
345 ret = decode_units(s, &r, &g, &b, &cx, &cx1);
349 clr = (b << 16) + (g << 8) + r;
352 return AVERROR_INVALIDDATA;
353 ret = decode_value(s, s->run_model[ptype], 256, 400, &run);
357 return AVERROR_INVALIDDATA;
359 ret = decode_run_i(avctx, ptype, run, &x, &y, clr,
360 dst, linesize, &lx, &ly,
361 backstep, off, &cx, &cx1);
369 static int decompress_p(AVCodecContext *avctx,
370 uint32_t *dst, int linesize,
371 uint32_t *prev, int plinesize)
373 SCPRContext *s = avctx->priv_data;
374 GetByteContext *gb = &s->gb;
375 int ret, temp, min, max, x, y, cx = 0, cx1 = 0;
376 int backstep = linesize - avctx->width;
378 if (bytestream2_get_byte(gb) == 0)
380 bytestream2_skip(gb, 1);
381 init_rangecoder(&s->rc, gb);
383 ret = decode_value(s, s->range_model, 256, 1, &min);
384 ret |= decode_value(s, s->range_model, 256, 1, &temp);
386 ret |= decode_value(s, s->range_model, 256, 1, &max);
387 ret |= decode_value(s, s->range_model, 256, 1, &temp);
392 if (min > max || min >= s->nbcount)
393 return AVERROR_INVALIDDATA;
395 memset(s->blocks, 0, sizeof(*s->blocks) * s->nbcount);
400 ret = decode_value(s, s->fill_model, 5, 10, &fill);
401 ret |= decode_value(s, s->count_model, 256, 20, &count);
405 return AVERROR_INVALIDDATA;
407 while (min < s->nbcount && count-- > 0) {
408 s->blocks[min++] = fill;
412 for (y = 0; y < s->nby; y++) {
413 for (x = 0; x < s->nbx; x++) {
414 int sy1 = 0, sy2 = 16, sx1 = 0, sx2 = 16;
416 if (s->blocks[y * s->nbx + x] == 0)
419 if (((s->blocks[y * s->nbx + x] - 1) & 1) > 0) {
420 ret = decode_value(s, s->sxy_model[0], 16, 100, &sx1);
421 ret |= decode_value(s, s->sxy_model[1], 16, 100, &sy1);
422 ret |= decode_value(s, s->sxy_model[2], 16, 100, &sx2);
423 ret |= decode_value(s, s->sxy_model[3], 16, 100, &sy2);
430 if (((s->blocks[y * s->nbx + x] - 1) & 2) > 0) {
431 int i, j, by = y * 16, bx = x * 16;
434 ret = decode_value(s, s->mv_model[0], 512, 100, &mvx);
435 ret |= decode_value(s, s->mv_model[1], 512, 100, &mvy);
442 if (by + mvy + sy1 < 0 || bx + mvx + sx1 < 0 ||
443 by + mvy + sy1 >= avctx->height || bx + mvx + sx1 >= avctx->width)
444 return AVERROR_INVALIDDATA;
446 for (i = 0; i < sy2 - sy1 && (by + sy1 + i) < avctx->height && (by + mvy + sy1 + i) < avctx->height; i++) {
447 for (j = 0; j < sx2 - sx1 && (bx + sx1 + j) < avctx->width && (bx + mvx + sx1 + j) < avctx->width; j++) {
448 dst[(by + i + sy1) * linesize + bx + sx1 + j] = prev[(by + mvy + sy1 + i) * plinesize + bx + sx1 + mvx + j];
452 int run, bx = x * 16 + sx1, by = y * 16 + sy1;
453 uint32_t r, g, b, clr, ptype = 0;
455 for (; by < y * 16 + sy2 && by < avctx->height;) {
456 ret = decode_value(s, s->op_model[ptype], 6, 1000, &ptype);
460 ret = decode_units(s, &r, &g, &b, &cx, &cx1);
464 clr = (b << 16) + (g << 8) + r;
467 return AVERROR_INVALIDDATA;
468 ret = decode_value(s, s->run_model[ptype], 256, 400, &run);
472 return AVERROR_INVALIDDATA;
474 ret = decode_run_p(avctx, ptype, run, x, y, clr,
475 dst, prev, linesize, plinesize, &bx, &by,
476 backstep, sx1, sx2, &cx, &cx1);
487 static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
490 SCPRContext *s = avctx->priv_data;
491 GetByteContext *gb = &s->gb;
492 AVFrame *frame = data;
495 if (avctx->bits_per_coded_sample == 16) {
496 if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
500 if ((ret = ff_reget_buffer(avctx, s->current_frame)) < 0)
503 bytestream2_init(gb, avpkt->data, avpkt->size);
505 type = bytestream2_peek_byte(gb);
509 s->get_freq = get_freq0;
511 frame->key_frame = 1;
512 ret = decompress_i(avctx, (uint32_t *)s->current_frame->data[0],
513 s->current_frame->linesize[0] / 4);
514 } else if (type == 18) {
516 s->get_freq = get_freq;
518 frame->key_frame = 1;
519 ret = decompress_i(avctx, (uint32_t *)s->current_frame->data[0],
520 s->current_frame->linesize[0] / 4);
521 } else if (type == 34) {
522 frame->key_frame = 1;
524 ret = decompress_i3(avctx, (uint32_t *)s->current_frame->data[0],
525 s->current_frame->linesize[0] / 4);
526 } else if (type == 17 || type == 33) {
527 uint32_t clr, *dst = (uint32_t *)s->current_frame->data[0];
530 frame->key_frame = 1;
531 bytestream2_skip(gb, 1);
532 if (avctx->bits_per_coded_sample == 16) {
533 uint16_t value = bytestream2_get_le16(gb);
537 g = (value >> 5) & 31;
538 b = (value >> 10) & 31;
539 clr = (r << 16) + (g << 8) + b;
541 clr = bytestream2_get_le24(gb);
543 for (y = 0; y < avctx->height; y++) {
544 for (x = 0; x < avctx->width; x++) {
547 dst += s->current_frame->linesize[0] / 4;
549 } else if (type == 0 || type == 1) {
550 frame->key_frame = 0;
552 ret = av_frame_copy(s->current_frame, s->last_frame);
556 if (s->version == 1 || s->version == 2)
557 ret = decompress_p(avctx, (uint32_t *)s->current_frame->data[0],
558 s->current_frame->linesize[0] / 4,
559 (uint32_t *)s->last_frame->data[0],
560 s->last_frame->linesize[0] / 4);
562 ret = decompress_p3(avctx, (uint32_t *)s->current_frame->data[0],
563 s->current_frame->linesize[0] / 4,
564 (uint32_t *)s->last_frame->data[0],
565 s->last_frame->linesize[0] / 4);
569 return AVERROR_PATCHWELCOME;
575 if (avctx->bits_per_coded_sample != 16) {
576 ret = av_frame_ref(data, s->current_frame);
580 uint8_t *dst = frame->data[0];
583 ret = av_frame_copy(frame, s->current_frame);
587 // scale up each sample by 8
588 for (y = 0; y < avctx->height; y++) {
589 // If the image is sufficiently aligned, compute 8 samples at once
590 if (!(((uintptr_t)dst) & 7)) {
591 uint64_t *dst64 = (uint64_t *)dst;
592 int w = avctx->width>>1;
593 for (x = 0; x < w; x++) {
594 dst64[x] = (dst64[x] << 3) & 0xFCFCFCFCFCFCFCFCULL;
599 for (; x < avctx->width * 4; x++) {
600 dst[x] = dst[x] << 3;
602 dst += frame->linesize[0];
606 frame->pict_type = frame->key_frame ? AV_PICTURE_TYPE_I : AV_PICTURE_TYPE_P;
608 FFSWAP(AVFrame *, s->current_frame, s->last_frame);
610 frame->data[0] += frame->linesize[0] * (avctx->height - 1);
611 frame->linesize[0] *= -1;
618 static av_cold int decode_init(AVCodecContext *avctx)
620 SCPRContext *s = avctx->priv_data;
622 switch (avctx->bits_per_coded_sample) {
623 case 16: avctx->pix_fmt = AV_PIX_FMT_RGB0; break;
625 case 32: avctx->pix_fmt = AV_PIX_FMT_BGR0; break;
627 av_log(avctx, AV_LOG_ERROR, "Unsupported bitdepth %i\n", avctx->bits_per_coded_sample);
628 return AVERROR_INVALIDDATA;
631 s->get_freq = get_freq0;
634 s->cxshift = avctx->bits_per_coded_sample == 16 ? 0 : 2;
635 s->cbits = avctx->bits_per_coded_sample == 16 ? 0x1F : 0xFF;
636 s->nbx = (avctx->width + 15) / 16;
637 s->nby = (avctx->height + 15) / 16;
638 s->nbcount = s->nbx * s->nby;
639 s->blocks = av_malloc_array(s->nbcount, sizeof(*s->blocks));
641 return AVERROR(ENOMEM);
643 s->last_frame = av_frame_alloc();
644 s->current_frame = av_frame_alloc();
645 if (!s->last_frame || !s->current_frame)
646 return AVERROR(ENOMEM);
651 static av_cold int decode_close(AVCodecContext *avctx)
653 SCPRContext *s = avctx->priv_data;
655 av_freep(&s->blocks);
656 av_frame_free(&s->last_frame);
657 av_frame_free(&s->current_frame);
662 AVCodec ff_scpr_decoder = {
664 .long_name = NULL_IF_CONFIG_SMALL("ScreenPressor"),
665 .type = AVMEDIA_TYPE_VIDEO,
666 .id = AV_CODEC_ID_SCPR,
667 .priv_data_size = sizeof(SCPRContext),
669 .close = decode_close,
670 .decode = decode_frame,
671 .capabilities = AV_CODEC_CAP_DR1,
672 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE |
673 FF_CODEC_CAP_INIT_CLEANUP,