1 /*
2 * ScreenPressor decoder
3 *
4 * Copyright (c) 2017 Paul B Mahol
5 *
6 * This file is part of FFmpeg.
7 *
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.
12 *
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.
17 *
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
21 */
22
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26
30
31 #define TOP 0x01000000
33
39
45
64
68
70 {
73 rc->
code = bytestream2_get_be32(gb);
74 }
75
77 {
79
80 for (comp = 0; comp < 3; comp++) {
81 for (j = 0; j < 4096; j++) {
83 for (i = 0; i < 256; i++)
85 for (i = 0; i < 16; i++)
88 }
89 }
90 }
91
92 for (j = 0; j < 6; j++) {
94 for (i = 0; i < 256; i++)
95 p[i] = 1;
96 p[256] = 256;
97 }
98
99 for (j = 0; j < 6; j++) {
101 for (i = 0; i < 6; i++)
102 op[i] = 1;
103 op[6] = 6;
104 }
105
106 for (i = 0; i < 256; i++) {
109 }
112
113 for (i = 0; i < 5; i++) {
115 }
117
118 for (j = 0; j < 4; j++) {
119 for (i = 0; i < 16; i++) {
121 }
123 }
124
125 for (i = 0; i < 512; i++) {
128 }
131 }
132
134 {
137
139 unsigned byte = bytestream2_get_byte(gb);
142 }
143
144 return 0;
145 }
146
148 {
149 if (total_freq == 0)
151
153
156
158
159 return 0;
160 }
161
163 {
164 unsigned t;
165
166 if (total_freq == 0)
168
169 t = rc->
range * (uint64_t)cumFreq / total_freq;
170
172 rc->
range = rc->
range * (uint64_t)(freq + cumFreq) / total_freq - (t + 1);
173
175 unsigned byte = bytestream2_get_byte(gb);
179 }
180
181 return 0;
182 }
183
185 {
188
189 *freq = total_freq * (uint64_t)(rc->
code - rc->
code1) / rc->
range;
190
191 return 0;
192 }
193
195 {
198 unsigned totfr = cnt[maxc];
200 unsigned c = 0, cumfr = 0, cnt_c = 0;
201 int i, ret;
202
203 if ((ret = s->
get_freq(rc, totfr, &value)) < 0)
204 return ret;
205
206 while (c < maxc) {
208 if (value >= cumfr + cnt_c)
209 cumfr += cnt_c;
210 else
211 break;
212 c++;
213 }
214
215 if (c >= maxc)
217
218 if ((ret = s->
decode(gb, rc, cumfr, cnt_c, totfr)) < 0)
219 return ret;
220
221 cnt[
c] = cnt_c + step;
222 totfr += step;
224 totfr = 0;
225 for (i = 0; i < maxc; i++) {
226 unsigned nc = (cnt[i] >> 1) + 1;
227 cnt[i] = nc;
228 totfr += nc;
229 }
230 }
231
232 cnt[maxc] = totfr;
234
235 return 0;
236 }
237
239 {
243 unsigned value, x = 0, cumfr = 0, cnt_x = 0;
244 int i, j, ret,
c, cnt_c;
245
246 if ((ret = s->
get_freq(rc, totfr, &value)) < 0)
247 return ret;
248
249 while (x < 16) {
251 if (value >= cumfr + cnt_x)
252 cumfr += cnt_x;
253 else
254 break;
255 x++;
256 }
257
258 c = x * 16;
259 cnt_c = 0;
260 while (c < 256) {
261 cnt_c = pixel->
freq[
c];
262 if (value >= cumfr + cnt_c)
263 cumfr += cnt_c;
264 else
265 break;
266 c++;
267 }
268 if (x >= 16 || c >= 256) {
270 }
271
272 if ((ret = s->
decode(gb, rc, cumfr, cnt_c, totfr)) < 0)
273 return ret;
274
275 pixel->
freq[
c] = cnt_c + step;
276 pixel->
lookup[x] = cnt_x + step;
277 totfr += step;
279 totfr = 0;
280 for (i = 0; i < 256; i++) {
281 unsigned nc = (pixel->
freq[i] >> 1) + 1;
283 totfr += nc;
284 }
285 for (i = 0; i < 16; i++) {
286 unsigned sum = 0;
287 unsigned i16_17 = i << 4;
288 for (j = 0; j < 16; j++)
289 sum += pixel->
freq[i16_17 + j];
291 }
292 }
294
295 *rval = c & s->
cbits;
296
297 return 0;
298 }
299
301 {
304 int cx = 0, cx1 = 0, k = 0, clr = 0;
305 int run,
r,
g,
b, off, y = 0, x = 0, z, ret;
306 unsigned backstep = linesize - avctx->
width;
307 const int cxshift = s->
cxshift;
308 unsigned lx, ly, ptype;
309
313
314 while (k < avctx->
width + 1) {
316 if (ret < 0)
317 return ret;
318
319 cx1 = (cx << 6) & 0xFC0;
320 cx = r >> cxshift;
322 if (ret < 0)
323 return ret;
324
325 cx1 = (cx << 6) & 0xFC0;
326 cx = g >> cxshift;
328 if (ret < 0)
329 return ret;
330
331 cx1 = (cx << 6) & 0xFC0;
332 cx = b >> cxshift;
333
335 if (ret < 0)
336 return ret;
337
338 clr = (b << 16) + (g << 8) +
r;
340 while (run-- > 0) {
343
344 dst[y * linesize + x] = clr;
345 lx = x;
346 ly = y;
347 x++;
348 if (x >= avctx->
width) {
349 x = 0;
350 y++;
351 }
352 }
353 }
354 off = -linesize - 1;
355 ptype = 0;
356
359 if (ret < 0)
360 return ret;
361 if (ptype == 0) {
363 if (ret < 0)
364 return ret;
365
366 cx1 = (cx << 6) & 0xFC0;
367 cx = r >> cxshift;
369 if (ret < 0)
370 return ret;
371
372 cx1 = (cx << 6) & 0xFC0;
373 cx = g >> cxshift;
375 if (ret < 0)
376 return ret;
377
378 clr = (b << 16) + (g << 8) +
r;
379 }
380 if (ptype > 5)
383 if (ret < 0)
384 return ret;
385
386 switch (ptype) {
387 case 0:
388 while (run-- > 0) {
391
392 dst[y * linesize + x] = clr;
393 lx = x;
394 ly = y;
395 x++;
396 if (x >= avctx->
width) {
397 x = 0;
398 y++;
399 }
400 }
401 break;
402 case 1:
403 while (run-- > 0) {
406
407 dst[y * linesize + x] = dst[ly * linesize + lx];
408 lx = x;
409 ly = y;
410 x++;
411 if (x >= avctx->
width) {
412 x = 0;
413 y++;
414 }
415 }
416 clr = dst[ly * linesize + lx];
417 break;
418 case 2:
419 while (run-- > 0) {
420 if (y < 1 || y >= avctx->
height)
422
423 clr = dst[y * linesize + x + off + 1];
424 dst[y * linesize + x] = clr;
425 lx = x;
426 ly = y;
427 x++;
428 if (x >= avctx->
width) {
429 x = 0;
430 y++;
431 }
432 }
433 break;
434 case 4:
435 while (run-- > 0) {
437
438 if (y < 1 || y >= avctx->
height ||
439 (y == 1 && x == 0))
441
442 if (x == 0) {
443 z = backstep;
444 } else {
445 z = 0;
446 }
447
448 r = odst[(ly * linesize + lx) * 4] +
449 odst[((y * linesize + x) + off) * 4 + 4] -
450 odst[((y * linesize + x) + off - z) * 4];
451 g = odst[(ly * linesize + lx) * 4 + 1] +
452 odst[((y * linesize + x) + off) * 4 + 5] -
453 odst[((y * linesize + x) + off - z) * 4 + 1];
454 b = odst[(ly * linesize + lx) * 4 + 2] +
455 odst[((y * linesize + x) + off) * 4 + 6] -
456 odst[((y * linesize + x) + off - z) * 4 + 2];
457 clr = ((b & 0xFF) << 16) + ((g & 0xFF) << 8) + (r & 0xFF);
458 dst[y * linesize + x] = clr;
459 lx = x;
460 ly = y;
461 x++;
462 if (x >= avctx->
width) {
463 x = 0;
464 y++;
465 }
466 }
467 break;
468 case 5:
469 while (run-- > 0) {
470 if (y < 1 || y >= avctx->
height ||
471 (y == 1 && x == 0))
473
474 if (x == 0) {
475 z = backstep;
476 } else {
477 z = 0;
478 }
479
480 clr = dst[y * linesize + x + off - z];
481 dst[y * linesize + x] = clr;
482 lx = x;
483 ly = y;
484 x++;
485 if (x >= avctx->
width) {
486 x = 0;
487 y++;
488 }
489 }
490 break;
491 }
492
494 cx1 = (clr & 0x3F00) >> 2;
495 cx = (clr & 0x3FFFFF) >> 16;
496 } else {
497 cx1 = (clr & 0xFC00) >> 4;
498 cx = (clr & 0xFFFFFF) >> 18;
499 }
500 }
501
502 return 0;
503 }
504
506 uint32_t *dst, int linesize,
507 uint32_t *prev, int plinesize)
508 {
511 int ret,
temp,
min, max, x, y, cx = 0, cx1 = 0;
512 int backstep = linesize - avctx->
width;
513 const int cxshift = s->
cxshift;
514
515 if (bytestream2_get_byte(gb) == 0)
516 return 0;
519
522 min += temp << 8;
525 if (ret < 0)
526 return ret;
527
528 max += temp << 8;
530
531 while (min <= max) {
533
536 if (ret < 0)
537 return ret;
538
539 while (min < s->nbcount && count-- > 0) {
541 }
542 }
543
544 for (y = 0; y < s->
nby; y++) {
545 for (x = 0; x < s->
nbx; x++) {
546 int sy1 = 0, sy2 = 16, sx1 = 0, sx2 = 16;
547
549 continue;
550
551 if (((s->
blocks[y * s->
nbx + x] - 1) & 1) > 0) {
556 if (ret < 0)
557 return ret;
558
559 sx2++;
560 sy2++;
561 }
562 if (((s->
blocks[y * s->
nbx + x] - 1) & 2) > 0) {
563 int i, j, by = y * 16, bx = x * 16;
564 int mvx, mvy;
565
568 if (ret < 0)
569 return ret;
570
571 mvx -= 256;
572 mvy -= 256;
573
574 if (by + mvy + sy1 < 0 || bx + mvx + sx1 < 0 ||
575 by + mvy + sy1 >= avctx->
height || bx + mvx + sx1 >= avctx->
width)
577
578 for (i = 0; i < sy2 - sy1 && (by + sy1 + i) < avctx->
height && (by + mvy + sy1 + i) < avctx->
height; i++) {
579 for (j = 0; j < sx2 - sx1 && (bx + sx1 + j) < avctx->
width && (bx + mvx + sx1 + j) < avctx->
width; j++) {
580 dst[(by + i + sy1) * linesize + bx + sx1 + j] = prev[(by + mvy + sy1 + i) * plinesize + bx + sx1 + mvx + j];
581 }
582 }
583 } else {
584 int run,
r,
g,
b, z, bx = x * 16 + sx1, by = y * 16 + sy1;
585 unsigned clr, ptype = 0;
586
587 for (; by < y * 16 + sy2 && by < avctx->
height;) {
589 if (ret < 0)
590 return ret;
591 if (ptype == 0) {
593 if (ret < 0)
594 return ret;
595
596 cx1 = (cx << 6) & 0xFC0;
597 cx = r >> cxshift;
599 if (ret < 0)
600 return ret;
601
602 cx1 = (cx << 6) & 0xFC0;
603 cx = g >> cxshift;
605 if (ret < 0)
606 return ret;
607
608 clr = (b << 16) + (g << 8) +
r;
609 }
610 if (ptype > 5)
613 if (ret < 0)
614 return ret;
615
616 switch (ptype) {
617 case 0:
618 while (run-- > 0) {
621
622 dst[by * linesize + bx] = clr;
623 bx++;
624 if (bx >= x * 16 + sx2 || bx >= avctx->
width) {
625 bx = x * 16 + sx1;
626 by++;
627 }
628 }
629 break;
630 case 1:
631 while (run-- > 0) {
632 if (bx == 0) {
633 if (by < 1)
635 z = backstep;
636 } else {
637 z = 0;
638 }
639
642
643 clr = dst[by * linesize + bx - 1 - z];
644 dst[by * linesize + bx] = clr;
645 bx++;
646 if (bx >= x * 16 + sx2 || bx >= avctx->
width) {
647 bx = x * 16 + sx1;
648 by++;
649 }
650 }
651 break;
652 case 2:
653 while (run-- > 0) {
654 if (by < 1 || by >= avctx->
height)
656
657 clr = dst[(by - 1) * linesize + bx];
658 dst[by * linesize + bx] = clr;
659 bx++;
660 if (bx >= x * 16 + sx2 || bx >= avctx->
width) {
661 bx = x * 16 + sx1;
662 by++;
663 }
664 }
665 break;
666 case 3:
667 while (run-- > 0) {
670
671 clr = prev[by * plinesize + bx];
672 dst[by * linesize + bx] = clr;
673 bx++;
674 if (bx >= x * 16 + sx2 || bx >= avctx->
width) {
675 bx = x * 16 + sx1;
676 by++;
677 }
678 }
679 break;
680 case 4:
681 while (run-- > 0) {
683
684 if (by < 1 || by >= avctx->
height)
686
687 if (bx == 0) {
688 if (by < 2)
690 z = backstep;
691 } else {
692 z = 0;
693 }
694
695 r = odst[((by - 1) * linesize + bx) * 4] +
696 odst[(by * linesize + bx - 1 - z) * 4] -
697 odst[((by - 1) * linesize + bx - 1 - z) * 4];
698 g = odst[((by - 1) * linesize + bx) * 4 + 1] +
699 odst[(by * linesize + bx - 1 - z) * 4 + 1] -
700 odst[((by - 1) * linesize + bx - 1 - z) * 4 + 1];
701 b = odst[((by - 1) * linesize + bx) * 4 + 2] +
702 odst[(by * linesize + bx - 1 - z) * 4 + 2] -
703 odst[((by - 1) * linesize + bx - 1 - z) * 4 + 2];
704 clr = ((b & 0xFF) << 16) + ((g & 0xFF) << 8) + (r & 0xFF);
705 dst[by * linesize + bx] = clr;
706 bx++;
707 if (bx >= x * 16 + sx2 || bx >= avctx->
width) {
708 bx = x * 16 + sx1;
709 by++;
710 }
711 }
712 break;
713 case 5:
714 while (run-- > 0) {
715 if (by < 1 || by >= avctx->
height)
717
718 if (bx == 0) {
719 if (by < 2)
721 z = backstep;
722 } else {
723 z = 0;
724 }
725
726 clr = dst[(by - 1) * linesize + bx - 1 - z];
727 dst[by * linesize + bx] = clr;
728 bx++;
729 if (bx >= x * 16 + sx2 || bx >= avctx->
width) {
730 bx = x * 16 + sx1;
731 by++;
732 }
733 }
734 break;
735 }
736
738 cx1 = (clr & 0x3F00) >> 2;
739 cx = (clr & 0x3FFFFF) >> 16;
740 } else {
741 cx1 = (clr & 0xFC00) >> 4;
742 cx = (clr & 0xFFFFFF) >> 18;
743 }
744 }
745 }
746 }
747 }
748
749 return 0;
750 }
751
754 {
759
762 return ret;
763 }
764
766 return ret;
767
769
770 type = bytestream2_peek_byte(gb);
771
772 if (type == 2) {
778 } else if (type == 18) {
784 } else if (type == 17) {
786 int x, y;
787
791 uint16_t
value = bytestream2_get_le16(gb);
793
795 g = (value >> 5) & 31;
796 b = (value >> 10) & 31;
797 clr = (r << 16) + (g << 8) +
b;
798 } else {
799 clr = bytestream2_get_le24(gb);
800 }
801 for (y = 0; y < avctx->
height; y++) {
802 for (x = 0; x < avctx->
width; x++) {
803 dst[x] = clr;
804 }
806 }
807 } else if (type == 0 || type == 1) {
809
811 if (ret < 0)
812 return ret;
813
818 } else {
820 }
821
822 if (ret < 0)
823 return ret;
824
827 if (ret < 0)
828 return ret;
829 } else {
831 int x, y;
832
834 if (ret < 0)
835 return ret;
836
837 // scale up each sample by 8
838 for (y = 0; y < avctx->
height; y++) {
839 // If the image is sufficiently aligned, compute 8 samples at once
840 if (!(((uintptr_t)dst) & 7)) {
841 uint64_t *dst64 = (uint64_t *)dst;
843 for (x = 0; x <
w; x++) {
844 dst64[x] = (dst64[x] << 3) & 0xFCFCFCFCFCFCFCFCULL;
845 }
846 x *= 8;
847 } else
848 x = 0;
849 for (; x < avctx->
width * 4; x++) {
850 dst[x] = dst[x] << 3;
851 }
853 }
854 }
855
857
859
862
863 *got_frame = 1;
864
866 }
867
869 {
871
874 case 24:
876 default:
879 }
880
883
892
897
898 return 0;
899 }
900
902 {
904
908
909 return 0;
910 }
911
924 };
#define FF_CODEC_CAP_INIT_CLEANUP
The codec allows calling the close function for deallocation even if the init function returned a fai...
unsigned mv_model[2][513]
static av_cold int decode_init(AVCodecContext *avctx)
int(* decode)(GetByteContext *gb, RangeCoder *rc, unsigned cumFreq, unsigned freq, unsigned total_freq)
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
This structure describes decoded (raw) audio or video data.
ptrdiff_t const GLvoid * data
PixelModel pixel_model[3][4096]
static av_cold int init(AVCodecContext *avctx)
static int decode0(GetByteContext *gb, RangeCoder *rc, unsigned cumFreq, unsigned freq, unsigned total_freq)
static void reinit_tables(SCPRContext *s)
static int decode_value(SCPRContext *s, unsigned *cnt, unsigned maxc, unsigned step, unsigned *rval)
unsigned run_model[6][257]
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
static av_always_inline void bytestream2_init(GetByteContext *g, const uint8_t *buf, int buf_size)
unsigned count_model[257]
static int decompress_i(AVCodecContext *avctx, uint32_t *dst, int linesize)
static int decompress_p(AVCodecContext *avctx, uint32_t *dst, int linesize, uint32_t *prev, int plinesize)
int ff_reget_buffer(AVCodecContext *avctx, AVFrame *frame)
Identical in function to av_frame_make_writable(), except it uses ff_get_buffer() to allocate the buf...
unsigned sxy_model[4][17]
#define FF_CODEC_CAP_INIT_THREADSAFE
The codec does not modify any global variables in the init function, allowing to call the init functi...
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
packed RGB 8:8:8, 32bpp, RGBXRGBX... X=unused/undefined
int av_frame_ref(AVFrame *dst, const AVFrame *src)
Set up a new reference to the data described by the source frame.
static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
int(* get_freq)(RangeCoder *rc, unsigned total_freq, unsigned *freq)
int bits_per_coded_sample
bits per sample/pixel from the demuxer (needed for huffyuv).
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
static av_cold int decode_close(AVCodecContext *avctx)
static av_always_inline void bytestream2_skip(GetByteContext *g, unsigned int size)
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
static int decode(GetByteContext *gb, RangeCoder *rc, unsigned cumFreq, unsigned freq, unsigned total_freq)
static av_always_inline unsigned int bytestream2_get_bytes_left(GetByteContext *g)
const char * name
Name of the codec implementation.
int av_frame_copy(AVFrame *dst, const AVFrame *src)
Copy the frame data from src to dst.
unsigned range_model[257]
enum AVPictureType pict_type
Picture type of the frame.
int width
picture width / height.
GLsizei GLboolean const GLfloat * value
static void init_rangecoder(RangeCoder *rc, GetByteContext *gb)
static int get_freq0(RangeCoder *rc, unsigned total_freq, unsigned *freq)
static void comp(unsigned char *dst, ptrdiff_t dst_stride, unsigned char *src, ptrdiff_t src_stride, int add)
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Libavcodec external API header.
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_WB64 unsigned int_TMPL AV_WB32 unsigned int_TMPL AV_WB24 unsigned int_TMPL AV_WB16 unsigned int_TMPL byte
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
main external API structure.
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
packed BGR 8:8:8, 32bpp, BGRXBGRX... X=unused/undefined
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
static int op(uint8_t **dst, const uint8_t *dst_end, GetByteContext *gb, int pixel, int count, int *x, int width, int linesize)
Perform decode operation.
static int get_freq(RangeCoder *rc, unsigned total_freq, unsigned *freq)
common internal api header.
static int decode_unit(SCPRContext *s, PixelModel *pixel, unsigned step, unsigned *rval)
int key_frame
1 -> keyframe, 0-> not
#define av_malloc_array(a, b)
#define FFSWAP(type, a, b)
This structure stores compressed data.
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.