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 = 0, 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);
389 ret = decode_value(s, s->range_model, 256, 1, &max);
390 ret |= decode_value(s, s->range_model, 256, 1, &temp);
395 if (min > max || min >= s->nbcount)
396 return AVERROR_INVALIDDATA;
398 memset(s->blocks, 0, sizeof(*s->blocks) * s->nbcount);
403 ret = decode_value(s, s->fill_model, 5, 10, &fill);
404 ret |= decode_value(s, s->count_model, 256, 20, &count);
408 return AVERROR_INVALIDDATA;
410 while (min < s->nbcount && count-- > 0) {
411 s->blocks[min++] = fill;
415 ret = av_frame_copy(s->current_frame, s->last_frame);
419 for (y = 0; y < s->nby; y++) {
420 for (x = 0; x < s->nbx; x++) {
421 int sy1 = 0, sy2 = 16, sx1 = 0, sx2 = 16;
423 if (s->blocks[y * s->nbx + x] == 0)
426 if (((s->blocks[y * s->nbx + x] - 1) & 1) > 0) {
427 ret = decode_value(s, s->sxy_model[0], 16, 100, &sx1);
428 ret |= decode_value(s, s->sxy_model[1], 16, 100, &sy1);
429 ret |= decode_value(s, s->sxy_model[2], 16, 100, &sx2);
430 ret |= decode_value(s, s->sxy_model[3], 16, 100, &sy2);
437 if (((s->blocks[y * s->nbx + x] - 1) & 2) > 0) {
438 int i, j, by = y * 16, bx = x * 16;
441 ret = decode_value(s, s->mv_model[0], 512, 100, &mvx);
442 ret |= decode_value(s, s->mv_model[1], 512, 100, &mvy);
449 if (by + mvy + sy1 < 0 || bx + mvx + sx1 < 0 ||
450 by + mvy + sy1 >= avctx->height || bx + mvx + sx1 >= avctx->width)
451 return AVERROR_INVALIDDATA;
453 for (i = 0; i < sy2 - sy1 && (by + sy1 + i) < avctx->height && (by + mvy + sy1 + i) < avctx->height; i++) {
454 for (j = 0; j < sx2 - sx1 && (bx + sx1 + j) < avctx->width && (bx + mvx + sx1 + j) < avctx->width; j++) {
455 dst[(by + i + sy1) * linesize + bx + sx1 + j] = prev[(by + mvy + sy1 + i) * plinesize + bx + sx1 + mvx + j];
459 int run, bx = x * 16 + sx1, by = y * 16 + sy1;
460 uint32_t r, g, b, clr, ptype = 0;
462 for (; by < y * 16 + sy2 && by < avctx->height;) {
463 ret = decode_value(s, s->op_model[ptype], 6, 1000, &ptype);
467 ret = decode_units(s, &r, &g, &b, &cx, &cx1);
471 clr = (b << 16) + (g << 8) + r;
474 return AVERROR_INVALIDDATA;
475 ret = decode_value(s, s->run_model[ptype], 256, 400, &run);
479 return AVERROR_INVALIDDATA;
481 ret = decode_run_p(avctx, ptype, run, x, y, clr,
482 dst, prev, linesize, plinesize, &bx, &by,
483 backstep, sx1, sx2, &cx, &cx1);
494 static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
497 SCPRContext *s = avctx->priv_data;
498 GetByteContext *gb = &s->gb;
499 AVFrame *frame = data;
502 if (avctx->bits_per_coded_sample == 16) {
503 if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
507 if ((ret = ff_reget_buffer(avctx, s->current_frame)) < 0)
510 bytestream2_init(gb, avpkt->data, avpkt->size);
512 type = bytestream2_peek_byte(gb);
516 s->get_freq = get_freq0;
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 == 18) {
523 s->get_freq = get_freq;
525 frame->key_frame = 1;
526 ret = decompress_i(avctx, (uint32_t *)s->current_frame->data[0],
527 s->current_frame->linesize[0] / 4);
528 } else if (type == 34) {
529 frame->key_frame = 1;
531 ret = decompress_i3(avctx, (uint32_t *)s->current_frame->data[0],
532 s->current_frame->linesize[0] / 4);
533 } else if (type == 17 || type == 33) {
534 uint32_t clr, *dst = (uint32_t *)s->current_frame->data[0];
537 frame->key_frame = 1;
538 bytestream2_skip(gb, 1);
539 if (avctx->bits_per_coded_sample == 16) {
540 uint16_t value = bytestream2_get_le16(gb);
544 g = (value >> 5) & 31;
545 b = (value >> 10) & 31;
546 clr = (r << 16) + (g << 8) + b;
548 clr = bytestream2_get_le24(gb);
550 for (y = 0; y < avctx->height; y++) {
551 for (x = 0; x < avctx->width; x++) {
554 dst += s->current_frame->linesize[0] / 4;
556 } else if (type == 0 || type == 1) {
557 frame->key_frame = 0;
559 if (s->version == 1 || s->version == 2)
560 ret = decompress_p(avctx, (uint32_t *)s->current_frame->data[0],
561 s->current_frame->linesize[0] / 4,
562 (uint32_t *)s->last_frame->data[0],
563 s->last_frame->linesize[0] / 4);
565 ret = decompress_p3(avctx, (uint32_t *)s->current_frame->data[0],
566 s->current_frame->linesize[0] / 4,
567 (uint32_t *)s->last_frame->data[0],
568 s->last_frame->linesize[0] / 4);
572 return AVERROR_PATCHWELCOME;
578 if (avctx->bits_per_coded_sample != 16) {
579 ret = av_frame_ref(data, s->current_frame);
583 uint8_t *dst = frame->data[0];
586 ret = av_frame_copy(frame, s->current_frame);
590 // scale up each sample by 8
591 for (y = 0; y < avctx->height; y++) {
592 // If the image is sufficiently aligned, compute 8 samples at once
593 if (!(((uintptr_t)dst) & 7)) {
594 uint64_t *dst64 = (uint64_t *)dst;
595 int w = avctx->width>>1;
596 for (x = 0; x < w; x++) {
597 dst64[x] = (dst64[x] << 3) & 0xFCFCFCFCFCFCFCFCULL;
602 for (; x < avctx->width * 4; x++) {
603 dst[x] = dst[x] << 3;
605 dst += frame->linesize[0];
609 frame->pict_type = frame->key_frame ? AV_PICTURE_TYPE_I : AV_PICTURE_TYPE_P;
611 FFSWAP(AVFrame *, s->current_frame, s->last_frame);
613 frame->data[0] += frame->linesize[0] * (avctx->height - 1);
614 frame->linesize[0] *= -1;
621 static av_cold int decode_init(AVCodecContext *avctx)
623 SCPRContext *s = avctx->priv_data;
625 switch (avctx->bits_per_coded_sample) {
626 case 16: avctx->pix_fmt = AV_PIX_FMT_RGB0; break;
628 case 32: avctx->pix_fmt = AV_PIX_FMT_BGR0; break;
630 av_log(avctx, AV_LOG_ERROR, "Unsupported bitdepth %i\n", avctx->bits_per_coded_sample);
631 return AVERROR_INVALIDDATA;
634 s->get_freq = get_freq0;
637 s->cxshift = avctx->bits_per_coded_sample == 16 ? 0 : 2;
638 s->cbits = avctx->bits_per_coded_sample == 16 ? 0x1F : 0xFF;
639 s->nbx = (avctx->width + 15) / 16;
640 s->nby = (avctx->height + 15) / 16;
641 s->nbcount = s->nbx * s->nby;
642 s->blocks = av_malloc_array(s->nbcount, sizeof(*s->blocks));
644 return AVERROR(ENOMEM);
646 s->last_frame = av_frame_alloc();
647 s->current_frame = av_frame_alloc();
648 if (!s->last_frame || !s->current_frame)
649 return AVERROR(ENOMEM);
654 static av_cold int decode_close(AVCodecContext *avctx)
656 SCPRContext *s = avctx->priv_data;
658 av_freep(&s->blocks);
659 av_frame_free(&s->last_frame);
660 av_frame_free(&s->current_frame);
665 AVCodec ff_scpr_decoder = {
667 .long_name = NULL_IF_CONFIG_SMALL("ScreenPressor"),
668 .type = AVMEDIA_TYPE_VIDEO,
669 .id = AV_CODEC_ID_SCPR,
670 .priv_data_size = sizeof(SCPRContext),
672 .close = decode_close,
673 .decode = decode_frame,
674 .capabilities = AV_CODEC_CAP_DR1,
675 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE |
676 FF_CODEC_CAP_INIT_CLEANUP,