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"
29 #include "codec_internal.h"
34 #define TOP 0x01000000
39 static void init_rangecoder(RangeCoder
*rc
, GetByteContext
*gb
)
42 rc
->range
= 0xFFFFFFFFU
;
43 rc
->code
= bytestream2_get_be32(gb
);
46 static void reinit_tables(SCPRContext
*s
)
50 for (comp
= 0; comp
< 3; comp
++) {
51 for (j
= 0; j
< 4096; j
++) {
52 if (s
->pixel_model
[comp
][j
].total_freq
!= 256) {
53 for (i
= 0; i
< 256; i
++)
54 s
->pixel_model
[comp
][j
].freq
[i
] = 1;
55 for (i
= 0; i
< 16; i
++)
56 s
->pixel_model
[comp
][j
].lookup
[i
] = 16;
57 s
->pixel_model
[comp
][j
].total_freq
= 256;
62 for (j
= 0; j
< 6; j
++) {
63 uint32_t *p
= s
->run_model
[j
];
64 for (i
= 0; i
< 256; i
++)
69 for (j
= 0; j
< 6; j
++) {
70 uint32_t *op
= s
->op_model
[j
];
71 for (i
= 0; i
< 6; i
++)
76 for (i
= 0; i
< 256; i
++) {
77 s
->range_model
[i
] = 1;
78 s
->count_model
[i
] = 1;
80 s
->range_model
[256] = 256;
81 s
->count_model
[256] = 256;
83 for (i
= 0; i
< 5; i
++) {
88 for (j
= 0; j
< 4; j
++) {
89 for (i
= 0; i
< 16; i
++) {
90 s
->sxy_model
[j
][i
] = 1;
92 s
->sxy_model
[j
][16] = 16;
95 for (i
= 0; i
< 512; i
++) {
96 s
->mv_model
[0][i
] = 1;
97 s
->mv_model
[1][i
] = 1;
99 s
->mv_model
[0][512] = 512;
100 s
->mv_model
[1][512] = 512;
103 static int decode(GetByteContext
*gb
, RangeCoder
*rc
, uint32_t cumFreq
, uint32_t freq
, uint32_t total_freq
)
105 rc
->code
-= cumFreq
* rc
->range
;
108 while (rc
->range
< TOP
&& bytestream2_get_bytes_left(gb
) > 0) {
109 uint32_t byte
= bytestream2_get_byteu(gb
);
110 rc
->code
= (rc
->code
<< 8) | byte
;
117 static int get_freq(RangeCoder
*rc
, uint32_t total_freq
, uint32_t *freq
)
120 return AVERROR_INVALIDDATA
;
122 rc
->range
= rc
->range
/ total_freq
;
125 return AVERROR_INVALIDDATA
;
127 *freq
= rc
->code
/ rc
->range
;
132 static int decode0(GetByteContext
*gb
, RangeCoder
*rc
, uint32_t cumFreq
, uint32_t freq
, uint32_t total_freq
)
137 return AVERROR_INVALIDDATA
;
139 t
= rc
->range
* (uint64_t)cumFreq
/ total_freq
;
142 rc
->range
= rc
->range
* (uint64_t)(freq
+ cumFreq
) / total_freq
- (t
+ 1);
144 while (rc
->range
< TOP
&& bytestream2_get_bytes_left(gb
) > 0) {
145 uint32_t byte
= bytestream2_get_byteu(gb
);
146 rc
->code
= (rc
->code
<< 8) | byte
;
154 static int get_freq0(RangeCoder
*rc
, uint32_t total_freq
, uint32_t *freq
)
157 return AVERROR_INVALIDDATA
;
159 *freq
= total_freq
* (uint64_t)(rc
->code
- rc
->code1
) / rc
->range
;
164 static int decode_value(SCPRContext
*s
, uint32_t *cnt
, uint32_t maxc
, uint32_t step
, uint32_t *rval
)
166 GetByteContext
*gb
= &s
->gb
;
167 RangeCoder
*rc
= &s
->rc
;
168 uint32_t totfr
= cnt
[maxc
];
170 uint32_t c
= 0, cumfr
= 0, cnt_c
= 0;
173 if ((ret
= s
->get_freq(rc
, totfr
, &value
)) < 0)
178 if (value
>= cumfr
+ cnt_c
)
186 return AVERROR_INVALIDDATA
;
188 if ((ret
= s
->decode(gb
, rc
, cumfr
, cnt_c
, totfr
)) < 0)
191 cnt
[c
] = cnt_c
+ step
;
195 for (i
= 0; i
< maxc
; i
++) {
196 uint32_t nc
= (cnt
[i
] >> 1) + 1;
208 static int decode_unit(SCPRContext
*s
, PixelModel
*pixel
, uint32_t step
, uint32_t *rval
)
210 GetByteContext
*gb
= &s
->gb
;
211 RangeCoder
*rc
= &s
->rc
;
212 uint32_t totfr
= pixel
->total_freq
;
213 uint32_t value
, x
= 0, cumfr
= 0, cnt_x
= 0;
214 int i
, j
, ret
, c
, cnt_c
;
216 if ((ret
= s
->get_freq(rc
, totfr
, &value
)) < 0)
220 cnt_x
= pixel
->lookup
[x
];
221 if (value
>= cumfr
+ cnt_x
)
231 cnt_c
= pixel
->freq
[c
];
232 if (value
>= cumfr
+ cnt_c
)
238 if (x
>= 16 || c
>= 256) {
239 return AVERROR_INVALIDDATA
;
242 if ((ret
= s
->decode(gb
, rc
, cumfr
, cnt_c
, totfr
)) < 0)
245 pixel
->freq
[c
] = cnt_c
+ step
;
246 pixel
->lookup
[x
] = cnt_x
+ step
;
250 for (i
= 0; i
< 256; i
++) {
251 uint32_t nc
= (pixel
->freq
[i
] >> 1) + 1;
255 for (i
= 0; i
< 16; i
++) {
257 uint32_t i16_17
= i
<< 4;
258 for (j
= 0; j
< 16; j
++)
259 sum
+= pixel
->freq
[i16_17
+ j
];
260 pixel
->lookup
[i
] = sum
;
263 pixel
->total_freq
= totfr
;
265 *rval
= c
& s
->cbits
;
270 static int decode_units(SCPRContext
*s
, uint32_t *r
, uint32_t *g
, uint32_t *b
,
273 const int cxshift
= s
->cxshift
;
276 ret
= decode_unit(s
, &s
->pixel_model
[0][*cx
+ *cx1
], 400, r
);
280 *cx1
= (*cx
<< 6) & 0xFC0;
282 ret
= decode_unit(s
, &s
->pixel_model
[1][*cx
+ *cx1
], 400, g
);
286 *cx1
= (*cx
<< 6) & 0xFC0;
288 ret
= decode_unit(s
, &s
->pixel_model
[2][*cx
+ *cx1
], 400, b
);
292 *cx1
= (*cx
<< 6) & 0xFC0;
298 static int decompress_i(AVCodecContext
*avctx
, uint32_t *dst
, int linesize
)
300 SCPRContext
*s
= avctx
->priv_data
;
301 GetByteContext
*gb
= &s
->gb
;
302 int cx
= 0, cx1
= 0, k
= 0;
303 int run
, off
, y
= 0, x
= 0, ret
;
304 uint32_t clr
= 0, r
, g
, b
, backstep
= linesize
- avctx
->width
;
305 uint32_t lx
, ly
, ptype
;
308 bytestream2_skip(gb
, 2);
309 init_rangecoder(&s
->rc
, gb
);
311 while (k
< avctx
->width
+ 1) {
312 ret
= decode_units(s
, &r
, &g
, &b
, &cx
, &cx1
);
316 ret
= decode_value(s
, s
->run_model
[0], 256, 400, &run
);
320 return AVERROR_INVALIDDATA
;
322 clr
= (b
<< 16) + (g
<< 8) + r
;
325 if (y
>= avctx
->height
)
326 return AVERROR_INVALIDDATA
;
328 dst
[y
* linesize
+ x
] = clr
;
332 if (x
>= avctx
->width
) {
341 while (x
< avctx
->width
&& y
< avctx
->height
) {
342 ret
= decode_value(s
, s
->op_model
[ptype
], 6, 1000, &ptype
);
346 ret
= decode_units(s
, &r
, &g
, &b
, &cx
, &cx1
);
350 clr
= (b
<< 16) + (g
<< 8) + r
;
353 return AVERROR_INVALIDDATA
;
354 ret
= decode_value(s
, s
->run_model
[ptype
], 256, 400, &run
);
358 return AVERROR_INVALIDDATA
;
360 ret
= decode_run_i(avctx
, ptype
, run
, &x
, &y
, clr
,
361 dst
, linesize
, &lx
, &ly
,
362 backstep
, off
, &cx
, &cx1
);
370 static int decompress_p(AVCodecContext
*avctx
,
371 uint32_t *dst
, int linesize
,
372 uint32_t *prev
, int plinesize
)
374 SCPRContext
*s
= avctx
->priv_data
;
375 GetByteContext
*gb
= &s
->gb
;
376 int ret
, temp
= 0, min
, max
, x
, y
, cx
= 0, cx1
= 0;
377 int backstep
= linesize
- avctx
->width
;
379 if (bytestream2_get_byte(gb
) == 0)
381 bytestream2_skip(gb
, 1);
382 init_rangecoder(&s
->rc
, gb
);
384 ret
= decode_value(s
, s
->range_model
, 256, 1, &min
);
385 ret
|= decode_value(s
, s
->range_model
, 256, 1, &temp
);
390 ret
= decode_value(s
, s
->range_model
, 256, 1, &max
);
391 ret
|= decode_value(s
, s
->range_model
, 256, 1, &temp
);
396 if (min
> max
|| min
>= s
->nbcount
)
397 return AVERROR_INVALIDDATA
;
399 memset(s
->blocks
, 0, sizeof(*s
->blocks
) * s
->nbcount
);
404 ret
= decode_value(s
, s
->fill_model
, 5, 10, &fill
);
405 ret
|= decode_value(s
, s
->count_model
, 256, 20, &count
);
409 return AVERROR_INVALIDDATA
;
411 while (min
< s
->nbcount
&& count
-- > 0) {
412 s
->blocks
[min
++] = fill
;
416 ret
= av_frame_copy(s
->current_frame
, s
->last_frame
);
420 for (y
= 0; y
< s
->nby
; y
++) {
421 for (x
= 0; x
< s
->nbx
; x
++) {
422 int sy1
= 0, sy2
= 16, sx1
= 0, sx2
= 16;
424 if (s
->blocks
[y
* s
->nbx
+ x
] == 0)
427 if (((s
->blocks
[y
* s
->nbx
+ x
] - 1) & 1) > 0) {
428 ret
= decode_value(s
, s
->sxy_model
[0], 16, 100, &sx1
);
429 ret
|= decode_value(s
, s
->sxy_model
[1], 16, 100, &sy1
);
430 ret
|= decode_value(s
, s
->sxy_model
[2], 16, 100, &sx2
);
431 ret
|= decode_value(s
, s
->sxy_model
[3], 16, 100, &sy2
);
438 if (((s
->blocks
[y
* s
->nbx
+ x
] - 1) & 2) > 0) {
439 int i
, j
, by
= y
* 16, bx
= x
* 16;
442 ret
= decode_value(s
, s
->mv_model
[0], 512, 100, &mvx
);
443 ret
|= decode_value(s
, s
->mv_model
[1], 512, 100, &mvy
);
450 if (by
+ mvy
+ sy1
< 0 || bx
+ mvx
+ sx1
< 0 ||
451 by
+ mvy
+ sy1
>= avctx
->height
|| bx
+ mvx
+ sx1
>= avctx
->width
)
452 return AVERROR_INVALIDDATA
;
454 for (i
= 0; i
< sy2
- sy1
&& (by
+ sy1
+ i
) < avctx
->height
&& (by
+ mvy
+ sy1
+ i
) < avctx
->height
; i
++) {
455 for (j
= 0; j
< sx2
- sx1
&& (bx
+ sx1
+ j
) < avctx
->width
&& (bx
+ mvx
+ sx1
+ j
) < avctx
->width
; j
++) {
456 dst
[(by
+ i
+ sy1
) * linesize
+ bx
+ sx1
+ j
] = prev
[(by
+ mvy
+ sy1
+ i
) * plinesize
+ bx
+ sx1
+ mvx
+ j
];
460 int run
, bx
= x
* 16 + sx1
, by
= y
* 16 + sy1
;
461 uint32_t r
, g
, b
, clr
, ptype
= 0;
463 if (bx
>= avctx
->width
)
464 return AVERROR_INVALIDDATA
;
466 for (; by
< y
* 16 + sy2
&& by
< avctx
->height
;) {
467 ret
= decode_value(s
, s
->op_model
[ptype
], 6, 1000, &ptype
);
471 ret
= decode_units(s
, &r
, &g
, &b
, &cx
, &cx1
);
475 clr
= (b
<< 16) + (g
<< 8) + r
;
478 return AVERROR_INVALIDDATA
;
479 ret
= decode_value(s
, s
->run_model
[ptype
], 256, 400, &run
);
483 return AVERROR_INVALIDDATA
;
485 ret
= decode_run_p(avctx
, ptype
, run
, x
, y
, clr
,
486 dst
, prev
, linesize
, plinesize
, &bx
, &by
,
487 backstep
, sx1
, sx2
, &cx
, &cx1
);
498 static int decode_frame(AVCodecContext
*avctx
, AVFrame
*frame
,
499 int *got_frame
, AVPacket
*avpkt
)
501 SCPRContext
*s
= avctx
->priv_data
;
502 GetByteContext
*gb
= &s
->gb
;
505 if (avctx
->bits_per_coded_sample
== 16) {
506 if ((ret
= ff_get_buffer(avctx
, frame
, 0)) < 0)
510 if ((ret
= ff_reget_buffer(avctx
, s
->current_frame
, 0)) < 0)
513 bytestream2_init(gb
, avpkt
->data
, avpkt
->size
);
515 type
= bytestream2_peek_byte(gb
);
519 s
->get_freq
= get_freq0
;
521 frame
->key_frame
= 1;
522 ret
= decompress_i(avctx
, (uint32_t *)s
->current_frame
->data
[0],
523 s
->current_frame
->linesize
[0] / 4);
524 } else if (type
== 18) {
526 s
->get_freq
= get_freq
;
528 frame
->key_frame
= 1;
529 ret
= decompress_i(avctx
, (uint32_t *)s
->current_frame
->data
[0],
530 s
->current_frame
->linesize
[0] / 4);
531 } else if (type
== 34) {
532 frame
->key_frame
= 1;
534 ret
= decompress_i3(avctx
, (uint32_t *)s
->current_frame
->data
[0],
535 s
->current_frame
->linesize
[0] / 4);
536 } else if (type
== 17 || type
== 33) {
537 uint32_t clr
, *dst
= (uint32_t *)s
->current_frame
->data
[0];
540 if (bytestream2_get_bytes_left(gb
) < 3)
541 return AVERROR_INVALIDDATA
;
543 frame
->key_frame
= 1;
544 bytestream2_skip(gb
, 1);
545 if (avctx
->bits_per_coded_sample
== 16) {
546 uint16_t value
= bytestream2_get_le16(gb
);
550 g
= (value
>> 5) & 31;
551 b
= (value
>> 10) & 31;
552 clr
= (r
<< 16) + (g
<< 8) + b
;
554 clr
= bytestream2_get_le24(gb
);
556 for (y
= 0; y
< avctx
->height
; y
++) {
558 av_memcpy_backptr((uint8_t*)(dst
+1), 4, 4*avctx
->width
- 4);
559 dst
+= s
->current_frame
->linesize
[0] / 4;
561 } else if (type
== 0 || type
== 1) {
562 frame
->key_frame
= 0;
564 if (s
->version
== 1 || s
->version
== 2)
565 ret
= decompress_p(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);
570 ret
= decompress_p3(avctx
, (uint32_t *)s
->current_frame
->data
[0],
571 s
->current_frame
->linesize
[0] / 4,
572 (uint32_t *)s
->last_frame
->data
[0],
573 s
->last_frame
->linesize
[0] / 4);
577 return AVERROR_PATCHWELCOME
;
583 if (bytestream2_get_bytes_left(gb
) > 5)
584 return AVERROR_INVALIDDATA
;
586 if (avctx
->bits_per_coded_sample
!= 16) {
587 ret
= av_frame_ref(frame
, s
->current_frame
);
591 uint8_t *dst
= frame
->data
[0];
594 ret
= av_frame_copy(frame
, s
->current_frame
);
598 // scale up each sample by 8
599 for (y
= 0; y
< avctx
->height
; y
++) {
600 // If the image is sufficiently aligned, compute 8 samples at once
601 if (!(((uintptr_t)dst
) & 7)) {
602 uint64_t *dst64
= (uint64_t *)dst
;
603 int w
= avctx
->width
>>1;
604 for (x
= 0; x
< w
; x
++) {
605 dst64
[x
] = (dst64
[x
] << 3) & 0xFCFCFCFCFCFCFCFCULL
;
610 for (; x
< avctx
->width
* 4; x
++) {
611 dst
[x
] = dst
[x
] << 3;
613 dst
+= frame
->linesize
[0];
617 frame
->pict_type
= frame
->key_frame
? AV_PICTURE_TYPE_I
: AV_PICTURE_TYPE_P
;
619 FFSWAP(AVFrame
*, s
->current_frame
, s
->last_frame
);
621 frame
->data
[0] += frame
->linesize
[0] * (avctx
->height
- 1);
622 frame
->linesize
[0] *= -1;
629 static av_cold
int decode_init(AVCodecContext
*avctx
)
631 SCPRContext
*s
= avctx
->priv_data
;
633 switch (avctx
->bits_per_coded_sample
) {
634 case 16: avctx
->pix_fmt
= AV_PIX_FMT_RGB0
; break;
636 case 32: avctx
->pix_fmt
= AV_PIX_FMT_BGR0
; break;
638 av_log(avctx
, AV_LOG_ERROR
, "Unsupported bitdepth %i\n", avctx
->bits_per_coded_sample
);
639 return AVERROR_INVALIDDATA
;
642 s
->get_freq
= get_freq0
;
645 s
->cxshift
= avctx
->bits_per_coded_sample
== 16 ? 0 : 2;
646 s
->cbits
= avctx
->bits_per_coded_sample
== 16 ? 0x1F : 0xFF;
647 s
->nbx
= (avctx
->width
+ 15) / 16;
648 s
->nby
= (avctx
->height
+ 15) / 16;
649 s
->nbcount
= s
->nbx
* s
->nby
;
650 s
->blocks
= av_malloc_array(s
->nbcount
, sizeof(*s
->blocks
));
652 return AVERROR(ENOMEM
);
654 s
->last_frame
= av_frame_alloc();
655 s
->current_frame
= av_frame_alloc();
656 if (!s
->last_frame
|| !s
->current_frame
)
657 return AVERROR(ENOMEM
);
662 static av_cold
int decode_close(AVCodecContext
*avctx
)
664 SCPRContext
*s
= avctx
->priv_data
;
666 av_freep(&s
->blocks
);
667 av_frame_free(&s
->last_frame
);
668 av_frame_free(&s
->current_frame
);
673 const FFCodec ff_scpr_decoder
= {
675 .p
.long_name
= NULL_IF_CONFIG_SMALL("ScreenPressor"),
676 .p
.type
= AVMEDIA_TYPE_VIDEO
,
677 .p
.id
= AV_CODEC_ID_SCPR
,
678 .priv_data_size
= sizeof(SCPRContext
),
680 .close
= decode_close
,
681 FF_CODEC_DECODE_CB(decode_frame
),
682 .p
.capabilities
= AV_CODEC_CAP_DR1
,
683 .caps_internal
= FF_CODEC_CAP_INIT_THREADSAFE
|
684 FF_CODEC_CAP_INIT_CLEANUP
,