FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/scpr.c
Date: 2022-11-26 13:19:19
Exec Total Coverage
Lines: 0 417 0.0%
Branches: 0 259 0.0%

Line Branch Exec Source
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 <string.h>
24
25 #include "avcodec.h"
26 #include "bytestream.h"
27 #include "codec_internal.h"
28 #include "decode.h"
29 #include "scpr.h"
30 #include "scpr3.h"
31
32 #define TOP 0x01000000
33 #define BOT 0x010000
34
35 #include "scpr3.c"
36
37 static void init_rangecoder(RangeCoder *rc, GetByteContext *gb)
38 {
39 rc->code1 = 0;
40 rc->range = 0xFFFFFFFFU;
41 rc->code = bytestream2_get_be32(gb);
42 }
43
44 static void reinit_tables(SCPRContext *s)
45 {
46 int comp, i, j;
47
48 for (comp = 0; comp < 3; comp++) {
49 for (j = 0; j < 4096; j++) {
50 if (s->pixel_model[comp][j].total_freq != 256) {
51 for (i = 0; i < 256; i++)
52 s->pixel_model[comp][j].freq[i] = 1;
53 for (i = 0; i < 16; i++)
54 s->pixel_model[comp][j].lookup[i] = 16;
55 s->pixel_model[comp][j].total_freq = 256;
56 }
57 }
58 }
59
60 for (j = 0; j < 6; j++) {
61 uint32_t *p = s->run_model[j];
62 for (i = 0; i < 256; i++)
63 p[i] = 1;
64 p[256] = 256;
65 }
66
67 for (j = 0; j < 6; j++) {
68 uint32_t *op = s->op_model[j];
69 for (i = 0; i < 6; i++)
70 op[i] = 1;
71 op[6] = 6;
72 }
73
74 for (i = 0; i < 256; i++) {
75 s->range_model[i] = 1;
76 s->count_model[i] = 1;
77 }
78 s->range_model[256] = 256;
79 s->count_model[256] = 256;
80
81 for (i = 0; i < 5; i++) {
82 s->fill_model[i] = 1;
83 }
84 s->fill_model[5] = 5;
85
86 for (j = 0; j < 4; j++) {
87 for (i = 0; i < 16; i++) {
88 s->sxy_model[j][i] = 1;
89 }
90 s->sxy_model[j][16] = 16;
91 }
92
93 for (i = 0; i < 512; i++) {
94 s->mv_model[0][i] = 1;
95 s->mv_model[1][i] = 1;
96 }
97 s->mv_model[0][512] = 512;
98 s->mv_model[1][512] = 512;
99 }
100
101 static int decode(GetByteContext *gb, RangeCoder *rc, uint32_t cumFreq, uint32_t freq, uint32_t total_freq)
102 {
103 rc->code -= cumFreq * rc->range;
104 rc->range *= freq;
105
106 while (rc->range < TOP && bytestream2_get_bytes_left(gb) > 0) {
107 uint32_t byte = bytestream2_get_byteu(gb);
108 rc->code = (rc->code << 8) | byte;
109 rc->range <<= 8;
110 }
111
112 return 0;
113 }
114
115 static int get_freq(RangeCoder *rc, uint32_t total_freq, uint32_t *freq)
116 {
117 if (total_freq == 0)
118 return AVERROR_INVALIDDATA;
119
120 rc->range = rc->range / total_freq;
121
122 if (rc->range == 0)
123 return AVERROR_INVALIDDATA;
124
125 *freq = rc->code / rc->range;
126
127 return 0;
128 }
129
130 static int decode0(GetByteContext *gb, RangeCoder *rc, uint32_t cumFreq, uint32_t freq, uint32_t total_freq)
131 {
132 uint32_t t;
133
134 if (total_freq == 0)
135 return AVERROR_INVALIDDATA;
136
137 t = rc->range * (uint64_t)cumFreq / total_freq;
138
139 rc->code1 += t + 1;
140 rc->range = rc->range * (uint64_t)(freq + cumFreq) / total_freq - (t + 1);
141
142 while (rc->range < TOP && bytestream2_get_bytes_left(gb) > 0) {
143 uint32_t byte = bytestream2_get_byteu(gb);
144 rc->code = (rc->code << 8) | byte;
145 rc->code1 <<= 8;
146 rc->range <<= 8;
147 }
148
149 return 0;
150 }
151
152 static int get_freq0(RangeCoder *rc, uint32_t total_freq, uint32_t *freq)
153 {
154 if (rc->range == 0)
155 return AVERROR_INVALIDDATA;
156
157 *freq = total_freq * (uint64_t)(rc->code - rc->code1) / rc->range;
158
159 return 0;
160 }
161
162 static int decode_value(SCPRContext *s, uint32_t *cnt, uint32_t maxc, uint32_t step, uint32_t *rval)
163 {
164 GetByteContext *gb = &s->gb;
165 RangeCoder *rc = &s->rc;
166 uint32_t totfr = cnt[maxc];
167 uint32_t value;
168 uint32_t c = 0, cumfr = 0, cnt_c = 0;
169 int i, ret;
170
171 if ((ret = s->get_freq(rc, totfr, &value)) < 0)
172 return ret;
173
174 while (c < maxc) {
175 cnt_c = cnt[c];
176 if (value >= cumfr + cnt_c)
177 cumfr += cnt_c;
178 else
179 break;
180 c++;
181 }
182
183 if (c >= maxc)
184 return AVERROR_INVALIDDATA;
185
186 if ((ret = s->decode(gb, rc, cumfr, cnt_c, totfr)) < 0)
187 return ret;
188
189 cnt[c] = cnt_c + step;
190 totfr += step;
191 if (totfr > BOT) {
192 totfr = 0;
193 for (i = 0; i < maxc; i++) {
194 uint32_t nc = (cnt[i] >> 1) + 1;
195 cnt[i] = nc;
196 totfr += nc;
197 }
198 }
199
200 cnt[maxc] = totfr;
201 *rval = c;
202
203 return 0;
204 }
205
206 static int decode_unit(SCPRContext *s, PixelModel *pixel, uint32_t step, uint32_t *rval)
207 {
208 GetByteContext *gb = &s->gb;
209 RangeCoder *rc = &s->rc;
210 uint32_t totfr = pixel->total_freq;
211 uint32_t value, x = 0, cumfr = 0, cnt_x = 0;
212 int i, j, ret, c, cnt_c;
213
214 if ((ret = s->get_freq(rc, totfr, &value)) < 0)
215 return ret;
216
217 while (x < 16) {
218 cnt_x = pixel->lookup[x];
219 if (value >= cumfr + cnt_x)
220 cumfr += cnt_x;
221 else
222 break;
223 x++;
224 }
225
226 c = x * 16;
227 cnt_c = 0;
228 while (c < 256) {
229 cnt_c = pixel->freq[c];
230 if (value >= cumfr + cnt_c)
231 cumfr += cnt_c;
232 else
233 break;
234 c++;
235 }
236 if (x >= 16 || c >= 256) {
237 return AVERROR_INVALIDDATA;
238 }
239
240 if ((ret = s->decode(gb, rc, cumfr, cnt_c, totfr)) < 0)
241 return ret;
242
243 pixel->freq[c] = cnt_c + step;
244 pixel->lookup[x] = cnt_x + step;
245 totfr += step;
246 if (totfr > BOT) {
247 totfr = 0;
248 for (i = 0; i < 256; i++) {
249 uint32_t nc = (pixel->freq[i] >> 1) + 1;
250 pixel->freq[i] = nc;
251 totfr += nc;
252 }
253 for (i = 0; i < 16; i++) {
254 uint32_t sum = 0;
255 uint32_t i16_17 = i << 4;
256 for (j = 0; j < 16; j++)
257 sum += pixel->freq[i16_17 + j];
258 pixel->lookup[i] = sum;
259 }
260 }
261 pixel->total_freq = totfr;
262
263 *rval = c & s->cbits;
264
265 return 0;
266 }
267
268 static int decode_units(SCPRContext *s, uint32_t *r, uint32_t *g, uint32_t *b,
269 int *cx, int *cx1)
270 {
271 const int cxshift = s->cxshift;
272 int ret;
273
274 ret = decode_unit(s, &s->pixel_model[0][*cx + *cx1], 400, r);
275 if (ret < 0)
276 return ret;
277
278 *cx1 = (*cx << 6) & 0xFC0;
279 *cx = *r >> cxshift;
280 ret = decode_unit(s, &s->pixel_model[1][*cx + *cx1], 400, g);
281 if (ret < 0)
282 return ret;
283
284 *cx1 = (*cx << 6) & 0xFC0;
285 *cx = *g >> cxshift;
286 ret = decode_unit(s, &s->pixel_model[2][*cx + *cx1], 400, b);
287 if (ret < 0)
288 return ret;
289
290 *cx1 = (*cx << 6) & 0xFC0;
291 *cx = *b >> cxshift;
292
293 return 0;
294 }
295
296 static int decompress_i(AVCodecContext *avctx, uint32_t *dst, int linesize)
297 {
298 SCPRContext *s = avctx->priv_data;
299 GetByteContext *gb = &s->gb;
300 int cx = 0, cx1 = 0, k = 0;
301 int run, off, y = 0, x = 0, ret;
302 uint32_t clr = 0, r, g, b, backstep = linesize - avctx->width;
303 uint32_t lx, ly, ptype;
304
305 reinit_tables(s);
306 bytestream2_skip(gb, 2);
307 init_rangecoder(&s->rc, gb);
308
309 while (k < avctx->width + 1) {
310 ret = decode_units(s, &r, &g, &b, &cx, &cx1);
311 if (ret < 0)
312 return ret;
313
314 ret = decode_value(s, s->run_model[0], 256, 400, &run);
315 if (ret < 0)
316 return ret;
317 if (run <= 0)
318 return AVERROR_INVALIDDATA;
319
320 clr = (b << 16) + (g << 8) + r;
321 k += run;
322 while (run-- > 0) {
323 if (y >= avctx->height)
324 return AVERROR_INVALIDDATA;
325
326 dst[y * linesize + x] = clr;
327 lx = x;
328 ly = y;
329 x++;
330 if (x >= avctx->width) {
331 x = 0;
332 y++;
333 }
334 }
335 }
336 off = -linesize - 1;
337 ptype = 0;
338
339 while (x < avctx->width && y < avctx->height) {
340 ret = decode_value(s, s->op_model[ptype], 6, 1000, &ptype);
341 if (ret < 0)
342 return ret;
343 if (ptype == 0) {
344 ret = decode_units(s, &r, &g, &b, &cx, &cx1);
345 if (ret < 0)
346 return ret;
347
348 clr = (b << 16) + (g << 8) + r;
349 }
350 if (ptype > 5)
351 return AVERROR_INVALIDDATA;
352 ret = decode_value(s, s->run_model[ptype], 256, 400, &run);
353 if (ret < 0)
354 return ret;
355 if (run <= 0)
356 return AVERROR_INVALIDDATA;
357
358 ret = decode_run_i(avctx, ptype, run, &x, &y, clr,
359 dst, linesize, &lx, &ly,
360 backstep, off, &cx, &cx1);
361 if (ret < 0)
362 return ret;
363 }
364
365 return 0;
366 }
367
368 static int decompress_p(AVCodecContext *avctx,
369 uint32_t *dst, int linesize,
370 uint32_t *prev, int plinesize)
371 {
372 SCPRContext *s = avctx->priv_data;
373 GetByteContext *gb = &s->gb;
374 int ret, temp = 0, min, max, x, y, cx = 0, cx1 = 0;
375 int backstep = linesize - avctx->width;
376
377 if (bytestream2_get_byte(gb) == 0)
378 return 1;
379 bytestream2_skip(gb, 1);
380 init_rangecoder(&s->rc, gb);
381
382 ret = decode_value(s, s->range_model, 256, 1, &min);
383 ret |= decode_value(s, s->range_model, 256, 1, &temp);
384 if (ret < 0)
385 return ret;
386
387 min += temp << 8;
388 ret = decode_value(s, s->range_model, 256, 1, &max);
389 ret |= decode_value(s, s->range_model, 256, 1, &temp);
390 if (ret < 0)
391 return ret;
392
393 max += temp << 8;
394 if (min > max || min >= s->nbcount)
395 return AVERROR_INVALIDDATA;
396
397 memset(s->blocks, 0, sizeof(*s->blocks) * s->nbcount);
398
399 while (min <= max) {
400 int fill, count;
401
402 ret = decode_value(s, s->fill_model, 5, 10, &fill);
403 ret |= decode_value(s, s->count_model, 256, 20, &count);
404 if (ret < 0)
405 return ret;
406 if (count <= 0)
407 return AVERROR_INVALIDDATA;
408
409 while (min < s->nbcount && count-- > 0) {
410 s->blocks[min++] = fill;
411 }
412 }
413
414 ret = av_frame_copy(s->current_frame, s->last_frame);
415 if (ret < 0)
416 return ret;
417
418 for (y = 0; y < s->nby; y++) {
419 for (x = 0; x < s->nbx; x++) {
420 int sy1 = 0, sy2 = 16, sx1 = 0, sx2 = 16;
421
422 if (s->blocks[y * s->nbx + x] == 0)
423 continue;
424
425 if (((s->blocks[y * s->nbx + x] - 1) & 1) > 0) {
426 ret = decode_value(s, s->sxy_model[0], 16, 100, &sx1);
427 ret |= decode_value(s, s->sxy_model[1], 16, 100, &sy1);
428 ret |= decode_value(s, s->sxy_model[2], 16, 100, &sx2);
429 ret |= decode_value(s, s->sxy_model[3], 16, 100, &sy2);
430 if (ret < 0)
431 return ret;
432
433 sx2++;
434 sy2++;
435 }
436 if (((s->blocks[y * s->nbx + x] - 1) & 2) > 0) {
437 int i, j, by = y * 16, bx = x * 16;
438 int mvx, mvy;
439
440 ret = decode_value(s, s->mv_model[0], 512, 100, &mvx);
441 ret |= decode_value(s, s->mv_model[1], 512, 100, &mvy);
442 if (ret < 0)
443 return ret;
444
445 mvx -= 256;
446 mvy -= 256;
447
448 if (by + mvy + sy1 < 0 || bx + mvx + sx1 < 0 ||
449 by + mvy + sy1 >= avctx->height || bx + mvx + sx1 >= avctx->width)
450 return AVERROR_INVALIDDATA;
451
452 for (i = 0; i < sy2 - sy1 && (by + sy1 + i) < avctx->height && (by + mvy + sy1 + i) < avctx->height; i++) {
453 for (j = 0; j < sx2 - sx1 && (bx + sx1 + j) < avctx->width && (bx + mvx + sx1 + j) < avctx->width; j++) {
454 dst[(by + i + sy1) * linesize + bx + sx1 + j] = prev[(by + mvy + sy1 + i) * plinesize + bx + sx1 + mvx + j];
455 }
456 }
457 } else {
458 int run, bx = x * 16 + sx1, by = y * 16 + sy1;
459 uint32_t r, g, b, clr, ptype = 0;
460
461 for (; by < y * 16 + sy2 && by < avctx->height;) {
462 ret = decode_value(s, s->op_model[ptype], 6, 1000, &ptype);
463 if (ret < 0)
464 return ret;
465 if (ptype == 0) {
466 ret = decode_units(s, &r, &g, &b, &cx, &cx1);
467 if (ret < 0)
468 return ret;
469
470 clr = (b << 16) + (g << 8) + r;
471 }
472 if (ptype > 5)
473 return AVERROR_INVALIDDATA;
474 ret = decode_value(s, s->run_model[ptype], 256, 400, &run);
475 if (ret < 0)
476 return ret;
477 if (run <= 0)
478 return AVERROR_INVALIDDATA;
479
480 ret = decode_run_p(avctx, ptype, run, x, y, clr,
481 dst, prev, linesize, plinesize, &bx, &by,
482 backstep, sx1, sx2, &cx, &cx1);
483 if (ret < 0)
484 return ret;
485 }
486 }
487 }
488 }
489
490 return 0;
491 }
492
493 static int decode_frame(AVCodecContext *avctx, AVFrame *frame,
494 int *got_frame, AVPacket *avpkt)
495 {
496 SCPRContext *s = avctx->priv_data;
497 GetByteContext *gb = &s->gb;
498 int ret, type;
499
500 if (avctx->bits_per_coded_sample == 16) {
501 if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
502 return ret;
503 }
504
505 if ((ret = ff_reget_buffer(avctx, s->current_frame, 0)) < 0)
506 return ret;
507
508 bytestream2_init(gb, avpkt->data, avpkt->size);
509
510 type = bytestream2_peek_byte(gb);
511
512 if (type == 2) {
513 s->version = 1;
514 s->get_freq = get_freq0;
515 s->decode = decode0;
516 frame->key_frame = 1;
517 ret = decompress_i(avctx, (uint32_t *)s->current_frame->data[0],
518 s->current_frame->linesize[0] / 4);
519 } else if (type == 18) {
520 s->version = 2;
521 s->get_freq = get_freq;
522 s->decode = decode;
523 frame->key_frame = 1;
524 ret = decompress_i(avctx, (uint32_t *)s->current_frame->data[0],
525 s->current_frame->linesize[0] / 4);
526 } else if (type == 34) {
527 frame->key_frame = 1;
528 s->version = 3;
529 ret = decompress_i3(avctx, (uint32_t *)s->current_frame->data[0],
530 s->current_frame->linesize[0] / 4);
531 } else if (type == 17 || type == 33) {
532 uint32_t clr, *dst = (uint32_t *)s->current_frame->data[0];
533 int y;
534
535 if (bytestream2_get_bytes_left(gb) < 3)
536 return AVERROR_INVALIDDATA;
537
538 frame->key_frame = 1;
539 bytestream2_skip(gb, 1);
540 if (avctx->bits_per_coded_sample == 16) {
541 uint16_t value = bytestream2_get_le16(gb);
542 int r, g, b;
543
544 r = (value ) & 31;
545 g = (value >> 5) & 31;
546 b = (value >> 10) & 31;
547 clr = (r << 16) + (g << 8) + b;
548 } else {
549 clr = bytestream2_get_le24(gb);
550 }
551 for (y = 0; y < avctx->height; y++) {
552 dst[0] = clr;
553 av_memcpy_backptr((uint8_t*)(dst+1), 4, 4*avctx->width - 4);
554 dst += s->current_frame->linesize[0] / 4;
555 }
556 } else if (type == 0 || type == 1) {
557 frame->key_frame = 0;
558
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);
564 else
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);
569 if (ret == 1)
570 return avpkt->size;
571 } else {
572 return AVERROR_PATCHWELCOME;
573 }
574
575 if (ret < 0)
576 return ret;
577
578 if (bytestream2_get_bytes_left(gb) > 5)
579 return AVERROR_INVALIDDATA;
580
581 if (avctx->bits_per_coded_sample != 16) {
582 ret = av_frame_ref(frame, s->current_frame);
583 if (ret < 0)
584 return ret;
585 } else {
586 uint8_t *dst = frame->data[0];
587 int x, y;
588
589 ret = av_frame_copy(frame, s->current_frame);
590 if (ret < 0)
591 return ret;
592
593 // scale up each sample by 8
594 for (y = 0; y < avctx->height; y++) {
595 // If the image is sufficiently aligned, compute 8 samples at once
596 if (!(((uintptr_t)dst) & 7)) {
597 uint64_t *dst64 = (uint64_t *)dst;
598 int w = avctx->width>>1;
599 for (x = 0; x < w; x++) {
600 dst64[x] = (dst64[x] << 3) & 0xFCFCFCFCFCFCFCFCULL;
601 }
602 x *= 8;
603 } else
604 x = 0;
605 for (; x < avctx->width * 4; x++) {
606 dst[x] = dst[x] << 3;
607 }
608 dst += frame->linesize[0];
609 }
610 }
611
612 frame->pict_type = frame->key_frame ? AV_PICTURE_TYPE_I : AV_PICTURE_TYPE_P;
613
614 FFSWAP(AVFrame *, s->current_frame, s->last_frame);
615
616 frame->data[0] += frame->linesize[0] * (avctx->height - 1);
617 frame->linesize[0] *= -1;
618
619 *got_frame = 1;
620
621 return avpkt->size;
622 }
623
624 static av_cold int decode_init(AVCodecContext *avctx)
625 {
626 SCPRContext *s = avctx->priv_data;
627
628 switch (avctx->bits_per_coded_sample) {
629 case 16: avctx->pix_fmt = AV_PIX_FMT_RGB0; break;
630 case 24:
631 case 32: avctx->pix_fmt = AV_PIX_FMT_BGR0; break;
632 default:
633 av_log(avctx, AV_LOG_ERROR, "Unsupported bitdepth %i\n", avctx->bits_per_coded_sample);
634 return AVERROR_INVALIDDATA;
635 }
636
637 s->get_freq = get_freq0;
638 s->decode = decode0;
639
640 s->cxshift = avctx->bits_per_coded_sample == 16 ? 0 : 2;
641 s->cbits = avctx->bits_per_coded_sample == 16 ? 0x1F : 0xFF;
642 s->nbx = (avctx->width + 15) / 16;
643 s->nby = (avctx->height + 15) / 16;
644 s->nbcount = s->nbx * s->nby;
645 s->blocks = av_malloc_array(s->nbcount, sizeof(*s->blocks));
646 if (!s->blocks)
647 return AVERROR(ENOMEM);
648
649 s->last_frame = av_frame_alloc();
650 s->current_frame = av_frame_alloc();
651 if (!s->last_frame || !s->current_frame)
652 return AVERROR(ENOMEM);
653
654 return 0;
655 }
656
657 static av_cold int decode_close(AVCodecContext *avctx)
658 {
659 SCPRContext *s = avctx->priv_data;
660
661 av_freep(&s->blocks);
662 av_frame_free(&s->last_frame);
663 av_frame_free(&s->current_frame);
664
665 return 0;
666 }
667
668 const FFCodec ff_scpr_decoder = {
669 .p.name = "scpr",
670 CODEC_LONG_NAME("ScreenPressor"),
671 .p.type = AVMEDIA_TYPE_VIDEO,
672 .p.id = AV_CODEC_ID_SCPR,
673 .priv_data_size = sizeof(SCPRContext),
674 .init = decode_init,
675 .close = decode_close,
676 FF_CODEC_DECODE_CB(decode_frame),
677 .p.capabilities = AV_CODEC_CAP_DR1,
678 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
679 };
680