FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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 {
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_byteu(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_byteu(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 decode_units(SCPRContext *s, unsigned *r, unsigned *g, unsigned *b,
301  int *cx, int *cx1)
302 {
303  const int cxshift = s->cxshift;
304  int ret;
305 
306  ret = decode_unit(s, &s->pixel_model[0][*cx + *cx1], 400, r);
307  if (ret < 0)
308  return ret;
309 
310  *cx1 = (*cx << 6) & 0xFC0;
311  *cx = *r >> cxshift;
312  ret = decode_unit(s, &s->pixel_model[1][*cx + *cx1], 400, g);
313  if (ret < 0)
314  return ret;
315 
316  *cx1 = (*cx << 6) & 0xFC0;
317  *cx = *g >> cxshift;
318  ret = decode_unit(s, &s->pixel_model[2][*cx + *cx1], 400, b);
319  if (ret < 0)
320  return ret;
321 
322  *cx1 = (*cx << 6) & 0xFC0;
323  *cx = *b >> cxshift;
324 
325  return 0;
326 }
327 
328 static int decompress_i(AVCodecContext *avctx, uint32_t *dst, int linesize)
329 {
330  SCPRContext *s = avctx->priv_data;
331  GetByteContext *gb = &s->gb;
332  int cx = 0, cx1 = 0, k = 0, clr = 0;
333  int run, off, y = 0, x = 0, z, ret;
334  unsigned r, g, b, backstep = linesize - avctx->width;
335  unsigned lx, ly, ptype;
336 
337  reinit_tables(s);
338  bytestream2_skip(gb, 2);
339  init_rangecoder(&s->rc, gb);
340 
341  while (k < avctx->width + 1) {
342  ret = decode_units(s, &r, &g, &b, &cx, &cx1);
343  if (ret < 0)
344  return ret;
345 
346  ret = decode_value(s, s->run_model[0], 256, 400, &run);
347  if (ret < 0)
348  return ret;
349  if (run <= 0)
350  return AVERROR_INVALIDDATA;
351 
352  clr = (b << 16) + (g << 8) + r;
353  k += run;
354  while (run-- > 0) {
355  if (y >= avctx->height)
356  return AVERROR_INVALIDDATA;
357 
358  dst[y * linesize + x] = clr;
359  lx = x;
360  ly = y;
361  x++;
362  if (x >= avctx->width) {
363  x = 0;
364  y++;
365  }
366  }
367  }
368  off = -linesize - 1;
369  ptype = 0;
370 
371  while (x < avctx->width && y < avctx->height) {
372  ret = decode_value(s, s->op_model[ptype], 6, 1000, &ptype);
373  if (ret < 0)
374  return ret;
375  if (ptype == 0) {
376  ret = decode_units(s, &r, &g, &b, &cx, &cx1);
377  if (ret < 0)
378  return ret;
379 
380  clr = (b << 16) + (g << 8) + r;
381  }
382  if (ptype > 5)
383  return AVERROR_INVALIDDATA;
384  ret = decode_value(s, s->run_model[ptype], 256, 400, &run);
385  if (ret < 0)
386  return ret;
387  if (run <= 0)
388  return AVERROR_INVALIDDATA;
389 
390  switch (ptype) {
391  case 0:
392  while (run-- > 0) {
393  if (y >= avctx->height)
394  return AVERROR_INVALIDDATA;
395 
396  dst[y * linesize + x] = clr;
397  lx = x;
398  ly = y;
399  x++;
400  if (x >= avctx->width) {
401  x = 0;
402  y++;
403  }
404  }
405  break;
406  case 1:
407  while (run-- > 0) {
408  if (y >= avctx->height)
409  return AVERROR_INVALIDDATA;
410 
411  dst[y * linesize + x] = dst[ly * linesize + lx];
412  lx = x;
413  ly = y;
414  x++;
415  if (x >= avctx->width) {
416  x = 0;
417  y++;
418  }
419  }
420  clr = dst[ly * linesize + lx];
421  break;
422  case 2:
423  while (run-- > 0) {
424  if (y < 1 || y >= avctx->height)
425  return AVERROR_INVALIDDATA;
426 
427  clr = dst[y * linesize + x + off + 1];
428  dst[y * linesize + x] = clr;
429  lx = x;
430  ly = y;
431  x++;
432  if (x >= avctx->width) {
433  x = 0;
434  y++;
435  }
436  }
437  break;
438  case 4:
439  while (run-- > 0) {
440  uint8_t *odst = (uint8_t *)dst;
441 
442  if (y < 1 || y >= avctx->height ||
443  (y == 1 && x == 0))
444  return AVERROR_INVALIDDATA;
445 
446  if (x == 0) {
447  z = backstep;
448  } else {
449  z = 0;
450  }
451 
452  r = odst[(ly * linesize + lx) * 4] +
453  odst[((y * linesize + x) + off) * 4 + 4] -
454  odst[((y * linesize + x) + off - z) * 4];
455  g = odst[(ly * linesize + lx) * 4 + 1] +
456  odst[((y * linesize + x) + off) * 4 + 5] -
457  odst[((y * linesize + x) + off - z) * 4 + 1];
458  b = odst[(ly * linesize + lx) * 4 + 2] +
459  odst[((y * linesize + x) + off) * 4 + 6] -
460  odst[((y * linesize + x) + off - z) * 4 + 2];
461  clr = ((b & 0xFF) << 16) + ((g & 0xFF) << 8) + (r & 0xFF);
462  dst[y * linesize + x] = clr;
463  lx = x;
464  ly = y;
465  x++;
466  if (x >= avctx->width) {
467  x = 0;
468  y++;
469  }
470  }
471  break;
472  case 5:
473  while (run-- > 0) {
474  if (y < 1 || y >= avctx->height ||
475  (y == 1 && x == 0))
476  return AVERROR_INVALIDDATA;
477 
478  if (x == 0) {
479  z = backstep;
480  } else {
481  z = 0;
482  }
483 
484  clr = dst[y * linesize + x + off - z];
485  dst[y * linesize + x] = clr;
486  lx = x;
487  ly = y;
488  x++;
489  if (x >= avctx->width) {
490  x = 0;
491  y++;
492  }
493  }
494  break;
495  }
496 
497  if (avctx->bits_per_coded_sample == 16) {
498  cx1 = (clr & 0x3F00) >> 2;
499  cx = (clr & 0x3FFFFF) >> 16;
500  } else {
501  cx1 = (clr & 0xFC00) >> 4;
502  cx = (clr & 0xFFFFFF) >> 18;
503  }
504  }
505 
506  return 0;
507 }
508 
509 static int decompress_p(AVCodecContext *avctx,
510  uint32_t *dst, int linesize,
511  uint32_t *prev, int plinesize)
512 {
513  SCPRContext *s = avctx->priv_data;
514  GetByteContext *gb = &s->gb;
515  int ret, temp, min, max, x, y, cx = 0, cx1 = 0;
516  int backstep = linesize - avctx->width;
517 
518  if (bytestream2_get_byte(gb) == 0)
519  return 1;
520  bytestream2_skip(gb, 1);
521  init_rangecoder(&s->rc, gb);
522 
523  ret = decode_value(s, s->range_model, 256, 1, &min);
524  ret |= decode_value(s, s->range_model, 256, 1, &temp);
525  min += temp << 8;
526  ret |= decode_value(s, s->range_model, 256, 1, &max);
527  ret |= decode_value(s, s->range_model, 256, 1, &temp);
528  if (ret < 0)
529  return ret;
530 
531  max += temp << 8;
532  if (min > max || min >= s->nbcount)
533  return AVERROR_INVALIDDATA;
534 
535  memset(s->blocks, 0, sizeof(*s->blocks) * s->nbcount);
536 
537  while (min <= max) {
538  int fill, count;
539 
540  ret = decode_value(s, s->fill_model, 5, 10, &fill);
541  ret |= decode_value(s, s->count_model, 256, 20, &count);
542  if (ret < 0)
543  return ret;
544  if (count <= 0)
545  return AVERROR_INVALIDDATA;
546 
547  while (min < s->nbcount && count-- > 0) {
548  s->blocks[min++] = fill;
549  }
550  }
551 
552  for (y = 0; y < s->nby; y++) {
553  for (x = 0; x < s->nbx; x++) {
554  int sy1 = 0, sy2 = 16, sx1 = 0, sx2 = 16;
555 
556  if (s->blocks[y * s->nbx + x] == 0)
557  continue;
558 
559  if (((s->blocks[y * s->nbx + x] - 1) & 1) > 0) {
560  ret = decode_value(s, s->sxy_model[0], 16, 100, &sx1);
561  ret |= decode_value(s, s->sxy_model[1], 16, 100, &sy1);
562  ret |= decode_value(s, s->sxy_model[2], 16, 100, &sx2);
563  ret |= decode_value(s, s->sxy_model[3], 16, 100, &sy2);
564  if (ret < 0)
565  return ret;
566 
567  sx2++;
568  sy2++;
569  }
570  if (((s->blocks[y * s->nbx + x] - 1) & 2) > 0) {
571  int i, j, by = y * 16, bx = x * 16;
572  int mvx, mvy;
573 
574  ret = decode_value(s, s->mv_model[0], 512, 100, &mvx);
575  ret |= decode_value(s, s->mv_model[1], 512, 100, &mvy);
576  if (ret < 0)
577  return ret;
578 
579  mvx -= 256;
580  mvy -= 256;
581 
582  if (by + mvy + sy1 < 0 || bx + mvx + sx1 < 0 ||
583  by + mvy + sy1 >= avctx->height || bx + mvx + sx1 >= avctx->width)
584  return AVERROR_INVALIDDATA;
585 
586  for (i = 0; i < sy2 - sy1 && (by + sy1 + i) < avctx->height && (by + mvy + sy1 + i) < avctx->height; i++) {
587  for (j = 0; j < sx2 - sx1 && (bx + sx1 + j) < avctx->width && (bx + mvx + sx1 + j) < avctx->width; j++) {
588  dst[(by + i + sy1) * linesize + bx + sx1 + j] = prev[(by + mvy + sy1 + i) * plinesize + bx + sx1 + mvx + j];
589  }
590  }
591  } else {
592  int run, z, bx = x * 16 + sx1, by = y * 16 + sy1;
593  unsigned r, g, b, clr, ptype = 0;
594 
595  for (; by < y * 16 + sy2 && by < avctx->height;) {
596  ret = decode_value(s, s->op_model[ptype], 6, 1000, &ptype);
597  if (ret < 0)
598  return ret;
599  if (ptype == 0) {
600  ret = decode_units(s, &r, &g, &b, &cx, &cx1);
601  if (ret < 0)
602  return ret;
603 
604  clr = (b << 16) + (g << 8) + r;
605  }
606  if (ptype > 5)
607  return AVERROR_INVALIDDATA;
608  ret = decode_value(s, s->run_model[ptype], 256, 400, &run);
609  if (ret < 0)
610  return ret;
611  if (run <= 0)
612  return AVERROR_INVALIDDATA;
613 
614  switch (ptype) {
615  case 0:
616  while (run-- > 0) {
617  if (by >= avctx->height)
618  return AVERROR_INVALIDDATA;
619 
620  dst[by * linesize + bx] = clr;
621  bx++;
622  if (bx >= x * 16 + sx2 || bx >= avctx->width) {
623  bx = x * 16 + sx1;
624  by++;
625  }
626  }
627  break;
628  case 1:
629  while (run-- > 0) {
630  if (bx == 0) {
631  if (by < 1)
632  return AVERROR_INVALIDDATA;
633  z = backstep;
634  } else {
635  z = 0;
636  }
637 
638  if (by >= avctx->height)
639  return AVERROR_INVALIDDATA;
640 
641  clr = dst[by * linesize + bx - 1 - z];
642  dst[by * linesize + bx] = clr;
643  bx++;
644  if (bx >= x * 16 + sx2 || bx >= avctx->width) {
645  bx = x * 16 + sx1;
646  by++;
647  }
648  }
649  break;
650  case 2:
651  while (run-- > 0) {
652  if (by < 1 || by >= avctx->height)
653  return AVERROR_INVALIDDATA;
654 
655  clr = dst[(by - 1) * linesize + bx];
656  dst[by * linesize + bx] = clr;
657  bx++;
658  if (bx >= x * 16 + sx2 || bx >= avctx->width) {
659  bx = x * 16 + sx1;
660  by++;
661  }
662  }
663  break;
664  case 3:
665  while (run-- > 0) {
666  if (by >= avctx->height)
667  return AVERROR_INVALIDDATA;
668 
669  clr = prev[by * plinesize + bx];
670  dst[by * linesize + bx] = clr;
671  bx++;
672  if (bx >= x * 16 + sx2 || bx >= avctx->width) {
673  bx = x * 16 + sx1;
674  by++;
675  }
676  }
677  break;
678  case 4:
679  while (run-- > 0) {
680  uint8_t *odst = (uint8_t *)dst;
681 
682  if (by < 1 || by >= avctx->height)
683  return AVERROR_INVALIDDATA;
684 
685  if (bx == 0) {
686  if (by < 2)
687  return AVERROR_INVALIDDATA;
688  z = backstep;
689  } else {
690  z = 0;
691  }
692 
693  r = odst[((by - 1) * linesize + bx) * 4] +
694  odst[(by * linesize + bx - 1 - z) * 4] -
695  odst[((by - 1) * linesize + bx - 1 - z) * 4];
696  g = odst[((by - 1) * linesize + bx) * 4 + 1] +
697  odst[(by * linesize + bx - 1 - z) * 4 + 1] -
698  odst[((by - 1) * linesize + bx - 1 - z) * 4 + 1];
699  b = odst[((by - 1) * linesize + bx) * 4 + 2] +
700  odst[(by * linesize + bx - 1 - z) * 4 + 2] -
701  odst[((by - 1) * linesize + bx - 1 - z) * 4 + 2];
702  clr = ((b & 0xFF) << 16) + ((g & 0xFF) << 8) + (r & 0xFF);
703  dst[by * linesize + bx] = clr;
704  bx++;
705  if (bx >= x * 16 + sx2 || bx >= avctx->width) {
706  bx = x * 16 + sx1;
707  by++;
708  }
709  }
710  break;
711  case 5:
712  while (run-- > 0) {
713  if (by < 1 || by >= avctx->height)
714  return AVERROR_INVALIDDATA;
715 
716  if (bx == 0) {
717  if (by < 2)
718  return AVERROR_INVALIDDATA;
719  z = backstep;
720  } else {
721  z = 0;
722  }
723 
724  clr = dst[(by - 1) * linesize + bx - 1 - z];
725  dst[by * linesize + bx] = clr;
726  bx++;
727  if (bx >= x * 16 + sx2 || bx >= avctx->width) {
728  bx = x * 16 + sx1;
729  by++;
730  }
731  }
732  break;
733  }
734 
735  if (avctx->bits_per_coded_sample == 16) {
736  cx1 = (clr & 0x3F00) >> 2;
737  cx = (clr & 0x3FFFFF) >> 16;
738  } else {
739  cx1 = (clr & 0xFC00) >> 4;
740  cx = (clr & 0xFFFFFF) >> 18;
741  }
742  }
743  }
744  }
745  }
746 
747  return 0;
748 }
749 
750 static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
751  AVPacket *avpkt)
752 {
753  SCPRContext *s = avctx->priv_data;
754  GetByteContext *gb = &s->gb;
755  AVFrame *frame = data;
756  int ret, type;
757 
758  if (avctx->bits_per_coded_sample == 16) {
759  if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
760  return ret;
761  }
762 
763  if ((ret = ff_reget_buffer(avctx, s->current_frame)) < 0)
764  return ret;
765 
766  bytestream2_init(gb, avpkt->data, avpkt->size);
767 
768  type = bytestream2_peek_byte(gb);
769 
770  if (type == 2) {
771  s->get_freq = get_freq0;
772  s->decode = decode0;
773  frame->key_frame = 1;
774  ret = decompress_i(avctx, (uint32_t *)s->current_frame->data[0],
775  s->current_frame->linesize[0] / 4);
776  } else if (type == 18) {
777  s->get_freq = get_freq;
778  s->decode = decode;
779  frame->key_frame = 1;
780  ret = decompress_i(avctx, (uint32_t *)s->current_frame->data[0],
781  s->current_frame->linesize[0] / 4);
782  } else if (type == 17) {
783  uint32_t clr, *dst = (uint32_t *)s->current_frame->data[0];
784  int x, y;
785 
786  frame->key_frame = 1;
787  bytestream2_skip(gb, 1);
788  if (avctx->bits_per_coded_sample == 16) {
789  uint16_t value = bytestream2_get_le16(gb);
790  int r, g, b;
791 
792  r = (value ) & 31;
793  g = (value >> 5) & 31;
794  b = (value >> 10) & 31;
795  clr = (r << 16) + (g << 8) + b;
796  } else {
797  clr = bytestream2_get_le24(gb);
798  }
799  for (y = 0; y < avctx->height; y++) {
800  for (x = 0; x < avctx->width; x++) {
801  dst[x] = clr;
802  }
803  dst += s->current_frame->linesize[0] / 4;
804  }
805  } else if (type == 0 || type == 1) {
806  frame->key_frame = 0;
807 
808  ret = av_frame_copy(s->current_frame, s->last_frame);
809  if (ret < 0)
810  return ret;
811 
812  ret = decompress_p(avctx, (uint32_t *)s->current_frame->data[0],
813  s->current_frame->linesize[0] / 4,
814  (uint32_t *)s->last_frame->data[0],
815  s->last_frame->linesize[0] / 4);
816  if (ret == 1)
817  return avpkt->size;
818  } else {
819  return AVERROR_PATCHWELCOME;
820  }
821 
822  if (ret < 0)
823  return ret;
824 
825  if (avctx->bits_per_coded_sample != 16) {
826  ret = av_frame_ref(data, s->current_frame);
827  if (ret < 0)
828  return ret;
829  } else {
830  uint8_t *dst = frame->data[0];
831  int x, y;
832 
833  ret = av_frame_copy(frame, s->current_frame);
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;
842  int w = avctx->width>>1;
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  }
852  dst += frame->linesize[0];
853  }
854  }
855 
857 
859 
860  frame->data[0] += frame->linesize[0] * (avctx->height - 1);
861  frame->linesize[0] *= -1;
862 
863  *got_frame = 1;
864 
865  return avpkt->size;
866 }
867 
869 {
870  SCPRContext *s = avctx->priv_data;
871 
872  switch (avctx->bits_per_coded_sample) {
873  case 16: avctx->pix_fmt = AV_PIX_FMT_RGB0; break;
874  case 24:
875  case 32: avctx->pix_fmt = AV_PIX_FMT_BGR0; break;
876  default:
877  av_log(avctx, AV_LOG_ERROR, "Unsupported bitdepth %i\n", avctx->bits_per_coded_sample);
878  return AVERROR_INVALIDDATA;
879  }
880 
881  s->get_freq = get_freq0;
882  s->decode = decode0;
883 
884  s->cxshift = avctx->bits_per_coded_sample == 16 ? 0 : 2;
885  s->cbits = avctx->bits_per_coded_sample == 16 ? 0x1F : 0xFF;
886  s->nbx = (avctx->width + 15) / 16;
887  s->nby = (avctx->height + 15) / 16;
888  s->nbcount = s->nbx * s->nby;
889  s->blocks = av_malloc_array(s->nbcount, sizeof(*s->blocks));
890  if (!s->blocks)
891  return AVERROR(ENOMEM);
892 
893  s->last_frame = av_frame_alloc();
895  if (!s->last_frame || !s->current_frame)
896  return AVERROR(ENOMEM);
897 
898  return 0;
899 }
900 
902 {
903  SCPRContext *s = avctx->priv_data;
904 
905  av_freep(&s->blocks);
908 
909  return 0;
910 }
911 
913  .name = "scpr",
914  .long_name = NULL_IF_CONFIG_SMALL("ScreenPressor"),
915  .type = AVMEDIA_TYPE_VIDEO,
916  .id = AV_CODEC_ID_SCPR,
917  .priv_data_size = sizeof(SCPRContext),
918  .init = decode_init,
919  .close = decode_close,
920  .decode = decode_frame,
921  .capabilities = AV_CODEC_CAP_DR1,
922  .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE |
924 };
#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:868
int(* decode)(GetByteContext *gb, RangeCoder *rc, unsigned cumFreq, unsigned freq, unsigned total_freq)
Definition: scpr.c:66
#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:226
ptrdiff_t const GLvoid * data
Definition: opengl_enc.c:101
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:115
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:1446
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:116
unsigned run_model[6][257]
Definition: scpr.c:53
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1743
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:328
static int decompress_p(AVCodecContext *avctx, uint32_t *dst, int linesize, uint32_t *prev, int plinesize)
Definition: scpr.c:509
AVCodec.
Definition: avcodec.h:3424
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:1965
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:443
unsigned lookup[16]
Definition: scpr.c:42
static AVFrame * frame
static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: scpr.c:750
#define height
uint8_t * data
Definition: avcodec.h:1445
int(* get_freq)(RangeCoder *rc, unsigned total_freq, unsigned *freq)
Definition: scpr.c:65
int bits_per_coded_sample
bits per sample/pixel from the demuxer (needed for huffyuv).
Definition: avcodec.h:2750
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:901
#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:114
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
const char * name
Name of the codec implementation.
Definition: avcodec.h:3431
GLsizei count
Definition: opengl_enc.c:109
int av_frame_copy(AVFrame *dst, const AVFrame *src)
Copy the frame data from src to dst.
Definition: frame.c:792
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:309
#define width
int width
picture width / height.
Definition: avcodec.h:1706
uint8_t w
Definition: llviddspenc.c:38
GLsizei GLboolean const GLfloat * value
Definition: opengl_enc.c:109
static void init_rangecoder(RangeCoder *rc, GetByteContext *gb)
Definition: scpr.c:69
#define s(width, name)
Definition: cbs_vp9.c:257
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
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
Definition: bytestream.h:87
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:257
main external API structure.
Definition: avcodec.h:1533
#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:1918
GLint GLenum type
Definition: opengl_enc.c:105
packed BGR 8:8:8, 32bpp, BGRXBGRX... X=unused/undefined
Definition: pixfmt.h:240
AVCodec ff_scpr_decoder
Definition: scpr.c:912
#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:240
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:1560
int key_frame
1 -> keyframe, 0-> not
Definition: frame.h:304
unsigned code1
Definition: scpr.c:37
#define av_freep(p)
#define av_malloc_array(a, b)
#define FFSWAP(type, a, b)
Definition: common.h:99
static int decode_units(SCPRContext *s, unsigned *r, unsigned *g, unsigned *b, int *cx, int *cx1)
Definition: scpr.c:300
float min
This structure stores compressed data.
Definition: avcodec.h:1422
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
Definition: avcodec.h:968
unsigned nbx
Definition: scpr.c:59
for(j=16;j >0;--j)
Predicted.
Definition: avutil.h:275