FFmpeg  4.0.3
scpr.c
Go to the documentation of this file.
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 
27 #include "avcodec.h"
28 #include "bytestream.h"
29 #include "internal.h"
30 
31 #define TOP 0x01000000
32 #define BOT 0x010000
33 
34 typedef struct RangeCoder {
35  unsigned code;
36  unsigned range;
37  unsigned code1;
38 } RangeCoder;
39 
40 typedef struct PixelModel {
41  unsigned freq[256];
42  unsigned lookup[16];
43  unsigned total_freq;
44 } PixelModel;
45 
46 typedef struct SCPRContext {
51  PixelModel pixel_model[3][4096];
52  unsigned op_model[6][7];
53  unsigned run_model[6][257];
54  unsigned range_model[257];
55  unsigned count_model[257];
56  unsigned fill_model[6];
57  unsigned sxy_model[4][17];
58  unsigned mv_model[2][513];
59  unsigned nbx, nby;
60  unsigned nbcount;
61  unsigned *blocks;
62  unsigned cbits;
63  int cxshift;
64 
65  int (*get_freq)(RangeCoder *rc, unsigned total_freq, unsigned *freq);
66  int (*decode)(GetByteContext *gb, RangeCoder *rc, unsigned cumFreq, unsigned freq, unsigned total_freq);
67 } SCPRContext;
68 
70 {
71  rc->code1 = 0;
72  rc->range = 0xFFFFFFFFU;
73  rc->code = bytestream2_get_be32(gb);
74 }
75 
77 {
78  int comp, i, j;
79 
80  for (comp = 0; comp < 3; comp++) {
81  for (j = 0; j < 4096; j++) {
82  if (s->pixel_model[comp][j].total_freq != 256) {
83  for (i = 0; i < 256; i++)
84  s->pixel_model[comp][j].freq[i] = 1;
85  for (i = 0; i < 16; i++)
86  s->pixel_model[comp][j].lookup[i] = 16;
87  s->pixel_model[comp][j].total_freq = 256;
88  }
89  }
90  }
91 
92  for (j = 0; j < 6; j++) {
93  unsigned *p = s->run_model[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++) {
100  unsigned *op = s->op_model[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++) {
107  s->range_model[i] = 1;
108  s->count_model[i] = 1;
109  }
110  s->range_model[256] = 256;
111  s->count_model[256] = 256;
112 
113  for (i = 0; i < 5; i++) {
114  s->fill_model[i] = 1;
115  }
116  s->fill_model[5] = 5;
117 
118  for (j = 0; j < 4; j++) {
119  for (i = 0; i < 16; i++) {
120  s->sxy_model[j][i] = 1;
121  }
122  s->sxy_model[j][16] = 16;
123  }
124 
125  for (i = 0; i < 512; i++) {
126  s->mv_model[0][i] = 1;
127  s->mv_model[1][i] = 1;
128  }
129  s->mv_model[0][512] = 512;
130  s->mv_model[1][512] = 512;
131 }
132 
133 static int decode(GetByteContext *gb, RangeCoder *rc, unsigned cumFreq, unsigned freq, unsigned total_freq)
134 {
135  rc->code -= cumFreq * rc->range;
136  rc->range *= freq;
137 
138  while (rc->range < TOP && bytestream2_get_bytes_left(gb) > 0) {
139  unsigned byte = bytestream2_get_byte(gb);
140  rc->code = (rc->code << 8) | byte;
141  rc->range <<= 8;
142  }
143 
144  return 0;
145 }
146 
147 static int get_freq(RangeCoder *rc, unsigned total_freq, unsigned *freq)
148 {
149  if (total_freq == 0)
150  return AVERROR_INVALIDDATA;
151 
152  rc->range = rc->range / total_freq;
153 
154  if (rc->range == 0)
155  return AVERROR_INVALIDDATA;
156 
157  *freq = rc->code / rc->range;
158 
159  return 0;
160 }
161 
162 static int decode0(GetByteContext *gb, RangeCoder *rc, unsigned cumFreq, unsigned freq, unsigned total_freq)
163 {
164  unsigned t;
165 
166  if (total_freq == 0)
167  return AVERROR_INVALIDDATA;
168 
169  t = rc->range * (uint64_t)cumFreq / total_freq;
170 
171  rc->code1 += t + 1;
172  rc->range = rc->range * (uint64_t)(freq + cumFreq) / total_freq - (t + 1);
173 
174  while (rc->range < TOP && bytestream2_get_bytes_left(gb) > 0) {
175  unsigned byte = bytestream2_get_byte(gb);
176  rc->code = (rc->code << 8) | byte;
177  rc->code1 <<= 8;
178  rc->range <<= 8;
179  }
180 
181  return 0;
182 }
183 
184 static int get_freq0(RangeCoder *rc, unsigned total_freq, unsigned *freq)
185 {
186  if (rc->range == 0)
187  return AVERROR_INVALIDDATA;
188 
189  *freq = total_freq * (uint64_t)(rc->code - rc->code1) / rc->range;
190 
191  return 0;
192 }
193 
194 static int decode_value(SCPRContext *s, unsigned *cnt, unsigned maxc, unsigned step, unsigned *rval)
195 {
196  GetByteContext *gb = &s->gb;
197  RangeCoder *rc = &s->rc;
198  unsigned totfr = cnt[maxc];
199  unsigned value;
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) {
207  cnt_c = cnt[c];
208  if (value >= cumfr + cnt_c)
209  cumfr += cnt_c;
210  else
211  break;
212  c++;
213  }
214 
215  if (c >= maxc)
216  return AVERROR_INVALIDDATA;
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;
223  if (totfr > BOT) {
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;
233  *rval = c;
234 
235  return 0;
236 }
237 
238 static int decode_unit(SCPRContext *s, PixelModel *pixel, unsigned step, unsigned *rval)
239 {
240  GetByteContext *gb = &s->gb;
241  RangeCoder *rc = &s->rc;
242  unsigned totfr = pixel->total_freq;
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) {
250  cnt_x = pixel->lookup[x];
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) {
269  return AVERROR_INVALIDDATA;
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;
278  if (totfr > BOT) {
279  totfr = 0;
280  for (i = 0; i < 256; i++) {
281  unsigned nc = (pixel->freq[i] >> 1) + 1;
282  pixel->freq[i] = nc;
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];
290  pixel->lookup[i] = sum;
291  }
292  }
293  pixel->total_freq = totfr;
294 
295  *rval = c & s->cbits;
296 
297  return 0;
298 }
299 
300 static int decompress_i(AVCodecContext *avctx, uint32_t *dst, int linesize)
301 {
302  SCPRContext *s = avctx->priv_data;
303  GetByteContext *gb = &s->gb;
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 
310  reinit_tables(s);
311  bytestream2_skip(gb, 2);
312  init_rangecoder(&s->rc, gb);
313 
314  while (k < avctx->width + 1) {
315  ret = decode_unit(s, &s->pixel_model[0][cx + cx1], 400, &r);
316  if (ret < 0)
317  return ret;
318 
319  cx1 = (cx << 6) & 0xFC0;
320  cx = r >> cxshift;
321  ret = decode_unit(s, &s->pixel_model[1][cx + cx1], 400, &g);
322  if (ret < 0)
323  return ret;
324 
325  cx1 = (cx << 6) & 0xFC0;
326  cx = g >> cxshift;
327  ret = decode_unit(s, &s->pixel_model[2][cx + cx1], 400, &b);
328  if (ret < 0)
329  return ret;
330 
331  cx1 = (cx << 6) & 0xFC0;
332  cx = b >> cxshift;
333 
334  ret = decode_value(s, s->run_model[0], 256, 400, &run);
335  if (ret < 0)
336  return ret;
337 
338  clr = (b << 16) + (g << 8) + r;
339  k += run;
340  while (run-- > 0) {
341  if (y >= avctx->height)
342  return AVERROR_INVALIDDATA;
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 
357  while (x < avctx->width && y < avctx->height) {
358  ret = decode_value(s, s->op_model[ptype], 6, 1000, &ptype);
359  if (ret < 0)
360  return ret;
361  if (ptype == 0) {
362  ret = decode_unit(s, &s->pixel_model[0][cx + cx1], 400, &r);
363  if (ret < 0)
364  return ret;
365 
366  cx1 = (cx << 6) & 0xFC0;
367  cx = r >> cxshift;
368  ret = decode_unit(s, &s->pixel_model[1][cx + cx1], 400, &g);
369  if (ret < 0)
370  return ret;
371 
372  cx1 = (cx << 6) & 0xFC0;
373  cx = g >> cxshift;
374  ret = decode_unit(s, &s->pixel_model[2][cx + cx1], 400, &b);
375  if (ret < 0)
376  return ret;
377 
378  clr = (b << 16) + (g << 8) + r;
379  }
380  if (ptype > 5)
381  return AVERROR_INVALIDDATA;
382  ret = decode_value(s, s->run_model[ptype], 256, 400, &run);
383  if (ret < 0)
384  return ret;
385 
386  switch (ptype) {
387  case 0:
388  while (run-- > 0) {
389  if (y >= avctx->height)
390  return AVERROR_INVALIDDATA;
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) {
404  if (y >= avctx->height)
405  return AVERROR_INVALIDDATA;
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)
421  return AVERROR_INVALIDDATA;
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) {
436  uint8_t *odst = (uint8_t *)dst;
437 
438  if (y < 1 || y >= avctx->height ||
439  (y == 1 && x == 0))
440  return AVERROR_INVALIDDATA;
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))
472  return AVERROR_INVALIDDATA;
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 
493  if (avctx->bits_per_coded_sample == 16) {
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 
505 static int decompress_p(AVCodecContext *avctx,
506  uint32_t *dst, int linesize,
507  uint32_t *prev, int plinesize)
508 {
509  SCPRContext *s = avctx->priv_data;
510  GetByteContext *gb = &s->gb;
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;
517  bytestream2_skip(gb, 1);
518  init_rangecoder(&s->rc, gb);
519 
520  ret = decode_value(s, s->range_model, 256, 1, &min);
521  ret |= decode_value(s, s->range_model, 256, 1, &temp);
522  min += temp << 8;
523  ret |= decode_value(s, s->range_model, 256, 1, &max);
524  ret |= decode_value(s, s->range_model, 256, 1, &temp);
525  if (ret < 0)
526  return ret;
527 
528  max += temp << 8;
529  if (min > max)
530  return AVERROR_INVALIDDATA;
531 
532  memset(s->blocks, 0, sizeof(*s->blocks) * s->nbcount);
533 
534  while (min <= max) {
535  int fill, count;
536 
537  ret = decode_value(s, s->fill_model, 5, 10, &fill);
538  ret |= decode_value(s, s->count_model, 256, 20, &count);
539  if (ret < 0)
540  return ret;
541 
542  while (min < s->nbcount && count-- > 0) {
543  s->blocks[min++] = fill;
544  }
545  }
546 
547  for (y = 0; y < s->nby; y++) {
548  for (x = 0; x < s->nbx; x++) {
549  int sy1 = 0, sy2 = 16, sx1 = 0, sx2 = 16;
550 
551  if (s->blocks[y * s->nbx + x] == 0)
552  continue;
553 
554  if (((s->blocks[y * s->nbx + x] - 1) & 1) > 0) {
555  ret = decode_value(s, s->sxy_model[0], 16, 100, &sx1);
556  ret |= decode_value(s, s->sxy_model[1], 16, 100, &sy1);
557  ret |= decode_value(s, s->sxy_model[2], 16, 100, &sx2);
558  ret |= decode_value(s, s->sxy_model[3], 16, 100, &sy2);
559  if (ret < 0)
560  return ret;
561 
562  sx2++;
563  sy2++;
564  }
565  if (((s->blocks[y * s->nbx + x] - 1) & 2) > 0) {
566  int i, j, by = y * 16, bx = x * 16;
567  int mvx, mvy;
568 
569  ret = decode_value(s, s->mv_model[0], 512, 100, &mvx);
570  ret |= decode_value(s, s->mv_model[1], 512, 100, &mvy);
571  if (ret < 0)
572  return ret;
573 
574  mvx -= 256;
575  mvy -= 256;
576 
577  if (by + mvy + sy1 < 0 || bx + mvx + sx1 < 0 ||
578  by + mvy + sy1 >= avctx->height || bx + mvx + sx1 >= avctx->width)
579  return AVERROR_INVALIDDATA;
580 
581  for (i = 0; i < sy2 - sy1 && (by + sy1 + i) < avctx->height && (by + mvy + sy1 + i) < avctx->height; i++) {
582  for (j = 0; j < sx2 - sx1 && (bx + sx1 + j) < avctx->width && (bx + mvx + sx1 + j) < avctx->width; j++) {
583  dst[(by + i + sy1) * linesize + bx + sx1 + j] = prev[(by + mvy + sy1 + i) * plinesize + bx + sx1 + mvx + j];
584  }
585  }
586  } else {
587  int run, r, g, b, z, bx = x * 16 + sx1, by = y * 16 + sy1;
588  unsigned clr, ptype = 0;
589 
590  for (; by < y * 16 + sy2 && by < avctx->height;) {
591  ret = decode_value(s, s->op_model[ptype], 6, 1000, &ptype);
592  if (ret < 0)
593  return ret;
594  if (ptype == 0) {
595  ret = decode_unit(s, &s->pixel_model[0][cx + cx1], 400, &r);
596  if (ret < 0)
597  return ret;
598 
599  cx1 = (cx << 6) & 0xFC0;
600  cx = r >> cxshift;
601  ret = decode_unit(s, &s->pixel_model[1][cx + cx1], 400, &g);
602  if (ret < 0)
603  return ret;
604 
605  cx1 = (cx << 6) & 0xFC0;
606  cx = g >> cxshift;
607  ret = decode_unit(s, &s->pixel_model[2][cx + cx1], 400, &b);
608  if (ret < 0)
609  return ret;
610 
611  clr = (b << 16) + (g << 8) + r;
612  }
613  if (ptype > 5)
614  return AVERROR_INVALIDDATA;
615  ret = decode_value(s, s->run_model[ptype], 256, 400, &run);
616  if (ret < 0)
617  return ret;
618 
619  switch (ptype) {
620  case 0:
621  while (run-- > 0) {
622  if (by >= avctx->height)
623  return AVERROR_INVALIDDATA;
624 
625  dst[by * linesize + bx] = clr;
626  bx++;
627  if (bx >= x * 16 + sx2 || bx >= avctx->width) {
628  bx = x * 16 + sx1;
629  by++;
630  }
631  }
632  break;
633  case 1:
634  while (run-- > 0) {
635  if (bx == 0) {
636  if (by < 1)
637  return AVERROR_INVALIDDATA;
638  z = backstep;
639  } else {
640  z = 0;
641  }
642 
643  if (by >= avctx->height)
644  return AVERROR_INVALIDDATA;
645 
646  clr = dst[by * linesize + bx - 1 - z];
647  dst[by * linesize + bx] = clr;
648  bx++;
649  if (bx >= x * 16 + sx2 || bx >= avctx->width) {
650  bx = x * 16 + sx1;
651  by++;
652  }
653  }
654  break;
655  case 2:
656  while (run-- > 0) {
657  if (by < 1 || by >= avctx->height)
658  return AVERROR_INVALIDDATA;
659 
660  clr = dst[(by - 1) * linesize + bx];
661  dst[by * linesize + bx] = clr;
662  bx++;
663  if (bx >= x * 16 + sx2 || bx >= avctx->width) {
664  bx = x * 16 + sx1;
665  by++;
666  }
667  }
668  break;
669  case 3:
670  while (run-- > 0) {
671  if (by >= avctx->height)
672  return AVERROR_INVALIDDATA;
673 
674  clr = prev[by * plinesize + bx];
675  dst[by * linesize + bx] = clr;
676  bx++;
677  if (bx >= x * 16 + sx2 || bx >= avctx->width) {
678  bx = x * 16 + sx1;
679  by++;
680  }
681  }
682  break;
683  case 4:
684  while (run-- > 0) {
685  uint8_t *odst = (uint8_t *)dst;
686 
687  if (by < 1 || by >= avctx->height)
688  return AVERROR_INVALIDDATA;
689 
690  if (bx == 0) {
691  if (by < 2)
692  return AVERROR_INVALIDDATA;
693  z = backstep;
694  } else {
695  z = 0;
696  }
697 
698  r = odst[((by - 1) * linesize + bx) * 4] +
699  odst[(by * linesize + bx - 1 - z) * 4] -
700  odst[((by - 1) * linesize + bx - 1 - z) * 4];
701  g = odst[((by - 1) * linesize + bx) * 4 + 1] +
702  odst[(by * linesize + bx - 1 - z) * 4 + 1] -
703  odst[((by - 1) * linesize + bx - 1 - z) * 4 + 1];
704  b = odst[((by - 1) * linesize + bx) * 4 + 2] +
705  odst[(by * linesize + bx - 1 - z) * 4 + 2] -
706  odst[((by - 1) * linesize + bx - 1 - z) * 4 + 2];
707  clr = ((b & 0xFF) << 16) + ((g & 0xFF) << 8) + (r & 0xFF);
708  dst[by * linesize + bx] = clr;
709  bx++;
710  if (bx >= x * 16 + sx2 || bx >= avctx->width) {
711  bx = x * 16 + sx1;
712  by++;
713  }
714  }
715  break;
716  case 5:
717  while (run-- > 0) {
718  if (by < 1 || by >= avctx->height)
719  return AVERROR_INVALIDDATA;
720 
721  if (bx == 0) {
722  if (by < 2)
723  return AVERROR_INVALIDDATA;
724  z = backstep;
725  } else {
726  z = 0;
727  }
728 
729  clr = dst[(by - 1) * linesize + bx - 1 - z];
730  dst[by * linesize + bx] = clr;
731  bx++;
732  if (bx >= x * 16 + sx2 || bx >= avctx->width) {
733  bx = x * 16 + sx1;
734  by++;
735  }
736  }
737  break;
738  }
739 
740  if (avctx->bits_per_coded_sample == 16) {
741  cx1 = (clr & 0x3F00) >> 2;
742  cx = (clr & 0x3FFFFF) >> 16;
743  } else {
744  cx1 = (clr & 0xFC00) >> 4;
745  cx = (clr & 0xFFFFFF) >> 18;
746  }
747  }
748  }
749  }
750  }
751 
752  return 0;
753 }
754 
755 static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
756  AVPacket *avpkt)
757 {
758  SCPRContext *s = avctx->priv_data;
759  GetByteContext *gb = &s->gb;
760  AVFrame *frame = data;
761  int ret, type;
762 
763  if (avctx->bits_per_coded_sample == 16) {
764  if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
765  return ret;
766  }
767 
768  if ((ret = ff_reget_buffer(avctx, s->current_frame)) < 0)
769  return ret;
770 
771  bytestream2_init(gb, avpkt->data, avpkt->size);
772 
773  type = bytestream2_peek_byte(gb);
774 
775  if (type == 2) {
776  s->get_freq = get_freq0;
777  s->decode = decode0;
778  frame->key_frame = 1;
779  ret = decompress_i(avctx, (uint32_t *)s->current_frame->data[0],
780  s->current_frame->linesize[0] / 4);
781  } else if (type == 18) {
782  s->get_freq = get_freq;
783  s->decode = decode;
784  frame->key_frame = 1;
785  ret = decompress_i(avctx, (uint32_t *)s->current_frame->data[0],
786  s->current_frame->linesize[0] / 4);
787  } else if (type == 17) {
788  uint32_t clr, *dst = (uint32_t *)s->current_frame->data[0];
789  int x, y;
790 
791  frame->key_frame = 1;
792  bytestream2_skip(gb, 1);
793  if (avctx->bits_per_coded_sample == 16) {
794  uint16_t value = bytestream2_get_le16(gb);
795  int r, g, b;
796 
797  r = (value ) & 31;
798  g = (value >> 5) & 31;
799  b = (value >> 10) & 31;
800  clr = (r << 16) + (g << 8) + b;
801  } else {
802  clr = bytestream2_get_le24(gb);
803  }
804  for (y = 0; y < avctx->height; y++) {
805  for (x = 0; x < avctx->width; x++) {
806  dst[x] = clr;
807  }
808  dst += s->current_frame->linesize[0] / 4;
809  }
810  } else if (type == 0 || type == 1) {
811  frame->key_frame = 0;
812 
813  ret = av_frame_copy(s->current_frame, s->last_frame);
814  if (ret < 0)
815  return ret;
816 
817  ret = decompress_p(avctx, (uint32_t *)s->current_frame->data[0],
818  s->current_frame->linesize[0] / 4,
819  (uint32_t *)s->last_frame->data[0],
820  s->last_frame->linesize[0] / 4);
821  } else {
822  return AVERROR_PATCHWELCOME;
823  }
824 
825  if (ret < 0)
826  return ret;
827 
828  if (avctx->bits_per_coded_sample != 16) {
829  ret = av_frame_ref(data, s->current_frame);
830  if (ret < 0)
831  return ret;
832  } else {
833  uint8_t *dst = frame->data[0];
834  int x, y;
835 
836  ret = av_frame_copy(frame, s->current_frame);
837  if (ret < 0)
838  return ret;
839 
840  // scale up each sample by 8
841  for (y = 0; y < avctx->height; y++) {
842  // If the image is sufficiently aligned, compute 8 samples at once
843  if (!(((uintptr_t)dst) & 7)) {
844  uint64_t *dst64 = (uint64_t *)dst;
845  int w = avctx->width>>1;
846  for (x = 0; x < w; x++) {
847  dst64[x] = (dst64[x] << 3) & 0xFCFCFCFCFCFCFCFCULL;
848  }
849  x *= 8;
850  } else
851  x = 0;
852  for (; x < avctx->width * 4; x++) {
853  dst[x] = dst[x] << 3;
854  }
855  dst += frame->linesize[0];
856  }
857  }
858 
860 
862 
863  frame->data[0] += frame->linesize[0] * (avctx->height - 1);
864  frame->linesize[0] *= -1;
865 
866  *got_frame = 1;
867 
868  return avpkt->size;
869 }
870 
872 {
873  SCPRContext *s = avctx->priv_data;
874 
875  switch (avctx->bits_per_coded_sample) {
876  case 16: avctx->pix_fmt = AV_PIX_FMT_RGB0; break;
877  case 24:
878  case 32: avctx->pix_fmt = AV_PIX_FMT_BGR0; break;
879  default:
880  av_log(avctx, AV_LOG_ERROR, "Unsupported bitdepth %i\n", avctx->bits_per_coded_sample);
881  return AVERROR_INVALIDDATA;
882  }
883 
884  s->get_freq = get_freq0;
885  s->decode = decode0;
886 
887  s->cxshift = avctx->bits_per_coded_sample == 16 ? 0 : 2;
888  s->cbits = avctx->bits_per_coded_sample == 16 ? 0x1F : 0xFF;
889  s->nbx = (avctx->width + 15) / 16;
890  s->nby = (avctx->height + 15) / 16;
891  s->nbcount = s->nbx * s->nby;
892  s->blocks = av_malloc_array(s->nbcount, sizeof(*s->blocks));
893  if (!s->blocks)
894  return AVERROR(ENOMEM);
895 
896  s->last_frame = av_frame_alloc();
898  if (!s->last_frame || !s->current_frame)
899  return AVERROR(ENOMEM);
900 
901  return 0;
902 }
903 
905 {
906  SCPRContext *s = avctx->priv_data;
907 
908  av_freep(&s->blocks);
911 
912  return 0;
913 }
914 
916  .name = "scpr",
917  .long_name = NULL_IF_CONFIG_SMALL("ScreenPressor"),
918  .type = AVMEDIA_TYPE_VIDEO,
919  .id = AV_CODEC_ID_SCPR,
920  .priv_data_size = sizeof(SCPRContext),
921  .init = decode_init,
922  .close = decode_close,
923  .decode = decode_frame,
924  .capabilities = AV_CODEC_CAP_DR1,
925  .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE |
927 };
#define FF_CODEC_CAP_INIT_CLEANUP
The codec allows calling the close function for deallocation even if the init function returned a fai...
Definition: internal.h:48
unsigned mv_model[2][513]
Definition: scpr.c:58
AVFrame * last_frame
Definition: scpr.c:47
static av_cold int decode_init(AVCodecContext *avctx)
Definition: scpr.c:871
const char * s
Definition: avisynth_c.h:768
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
This structure describes decoded (raw) audio or video data.
Definition: frame.h:218
unsigned * blocks
Definition: scpr.c:61
PixelModel pixel_model[3][4096]
Definition: scpr.c:51
else temp
Definition: vf_mcdeint.c:256
const char * g
Definition: vf_curves.c:112
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
static int decode0(GetByteContext *gb, RangeCoder *rc, unsigned cumFreq, unsigned freq, unsigned total_freq)
Definition: scpr.c:162
static void reinit_tables(SCPRContext *s)
Definition: scpr.c:76
int size
Definition: avcodec.h:1431
unsigned total_freq
Definition: scpr.c:43
static int decode_value(SCPRContext *s, unsigned *cnt, unsigned maxc, unsigned step, unsigned *rval)
Definition: scpr.c:194
const char * b
Definition: vf_curves.c:113
unsigned run_model[6][257]
Definition: scpr.c:53
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1727
static av_always_inline void bytestream2_init(GetByteContext *g, const uint8_t *buf, int buf_size)
Definition: bytestream.h:133
unsigned count_model[257]
Definition: scpr.c:55
uint32_t range
Definition: mss3.c:64
uint8_t run
Definition: svq3.c:206
static int decompress_i(AVCodecContext *avctx, uint32_t *dst, int linesize)
Definition: scpr.c:300
static int decompress_p(AVCodecContext *avctx, uint32_t *dst, int linesize, uint32_t *prev, int plinesize)
Definition: scpr.c:505
AVCodec.
Definition: avcodec.h:3408
unsigned fill_model[6]
Definition: scpr.c:56
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...
Definition: decode.c:1938
unsigned sxy_model[4][17]
Definition: scpr.c:57
#define FF_CODEC_CAP_INIT_THREADSAFE
The codec does not modify any global variables in the init function, allowing to call the init functi...
Definition: internal.h:40
uint8_t
#define av_cold
Definition: attributes.h:82
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition: frame.c:189
packed RGB 8:8:8, 32bpp, RGBXRGBX... X=unused/undefined
Definition: pixfmt.h:238
unsigned range
Definition: scpr.c:36
unsigned cbits
Definition: scpr.c:62
int av_frame_ref(AVFrame *dst, const AVFrame *src)
Set up a new reference to the data described by the source frame.
Definition: frame.c:441
unsigned lookup[16]
Definition: scpr.c:42
static AVFrame * frame
const char data[16]
Definition: mxf.c:90
static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: scpr.c:755
#define height
uint8_t * data
Definition: avcodec.h:1430
int bits_per_coded_sample
bits per sample/pixel from the demuxer (needed for huffyuv).
Definition: avcodec.h:2734
AVFrame * current_frame
Definition: scpr.c:48
#define av_log(a,...)
#define U(x)
Definition: vp56_arith.h:37
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
unsigned nbcount
Definition: scpr.c:60
RangeCoder rc
Definition: scpr.c:50
static av_cold int decode_close(AVCodecContext *avctx)
Definition: scpr.c:904
#define AVERROR(e)
Definition: error.h:43
static av_always_inline void bytestream2_skip(GetByteContext *g, unsigned int size)
Definition: bytestream.h:164
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:202
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:186
const char * r
Definition: vf_curves.c:111
static int decode(GetByteContext *gb, RangeCoder *rc, unsigned cumFreq, unsigned freq, unsigned total_freq)
Definition: scpr.c:133
static av_always_inline unsigned int bytestream2_get_bytes_left(GetByteContext *g)
Definition: bytestream.h:154
int cxshift
Definition: scpr.c:63
uint16_t width
Definition: gdv.c:47
const char * name
Name of the codec implementation.
Definition: avcodec.h:3415
int av_frame_copy(AVFrame *dst, const AVFrame *src)
Copy the frame data from src to dst.
Definition: frame.c:790
unsigned code
Definition: scpr.c:35
unsigned range_model[257]
Definition: scpr.c:54
enum AVPictureType pict_type
Picture type of the frame.
Definition: frame.h:301
int width
picture width / height.
Definition: avcodec.h:1690
uint8_t w
Definition: llviddspenc.c:38
static void init_rangecoder(RangeCoder *rc, GetByteContext *gb)
Definition: scpr.c:69
static int get_freq0(RangeCoder *rc, unsigned total_freq, unsigned *freq)
Definition: scpr.c:184
static void comp(unsigned char *dst, ptrdiff_t dst_stride, unsigned char *src, ptrdiff_t src_stride, int add)
Definition: eamad.c:83
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition: error.h:62
Libavcodec external API header.
unsigned freq[256]
Definition: scpr.c:41
int(* decode)(GetByteContext *gb, RangeCoder *rc, unsigned cumFreq, unsigned freq, unsigned total_freq)
Definition: scpr.c:66
int(* get_freq)(RangeCoder *rc, unsigned total_freq, unsigned *freq)
Definition: scpr.c:65
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:249
main external API structure.
Definition: avcodec.h:1518
#define BOT
Definition: scpr.c:32
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: decode.c:1891
double value
Definition: eval.c:98
cl_device_type type
packed BGR 8:8:8, 32bpp, BGRXBGRX... X=unused/undefined
Definition: pixfmt.h:240
AVCodec ff_scpr_decoder
Definition: scpr.c:915
#define TOP
Definition: scpr.c:31
uint8_t pixel
Definition: tiny_ssim.c:42
unsigned op_model[6][7]
Definition: scpr.c:52
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:232
unsigned nby
Definition: scpr.c:59
GetByteContext gb
Definition: scpr.c:49
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.
Definition: anm.c:78
int
static int get_freq(RangeCoder *rc, unsigned total_freq, unsigned *freq)
Definition: scpr.c:147
common internal api header.
static int decode_unit(SCPRContext *s, PixelModel *pixel, unsigned step, unsigned *rval)
Definition: scpr.c:238
static double c[64]
void * priv_data
Definition: avcodec.h:1545
int key_frame
1 -> keyframe, 0-> not
Definition: frame.h:296
unsigned code1
Definition: scpr.c:37
#define av_freep(p)
void INT64 INT64 count
Definition: avisynth_c.h:690
#define av_malloc_array(a, b)
int lookup
#define FFSWAP(type, a, b)
Definition: common.h:99
float min
This structure stores compressed data.
Definition: avcodec.h:1407
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
Definition: avcodec.h:959
unsigned nbx
Definition: scpr.c:59
for(j=16;j >0;--j)
Predicted.
Definition: avutil.h:275