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_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  if ((ret = s->decode(gb, rc, cumfr, cnt_c, totfr)) < 0)
215  return ret;
216 
217  cnt[c] = cnt_c + step;
218  totfr += step;
219  if (totfr > BOT) {
220  totfr = 0;
221  for (i = 0; i < maxc; i++) {
222  unsigned nc = (cnt[i] >> 1) + 1;
223  cnt[i] = nc;
224  totfr += nc;
225  }
226  }
227 
228  cnt[maxc] = totfr;
229  *rval = c;
230 
231  return 0;
232 }
233 
234 static int decode_unit(SCPRContext *s, PixelModel *pixel, unsigned step, unsigned *rval)
235 {
236  GetByteContext *gb = &s->gb;
237  RangeCoder *rc = &s->rc;
238  unsigned totfr = pixel->total_freq;
239  unsigned value, x = 0, cumfr = 0, cnt_x = 0;
240  int i, j, ret, c, cnt_c;
241 
242  if ((ret = s->get_freq(rc, totfr, &value)) < 0)
243  return ret;
244 
245  while (x < 16) {
246  cnt_x = pixel->lookup[x];
247  if (value >= cumfr + cnt_x)
248  cumfr += cnt_x;
249  else
250  break;
251  x++;
252  }
253 
254  c = x * 16;
255  cnt_c = 0;
256  while (c < 256) {
257  cnt_c = pixel->freq[c];
258  if (value >= cumfr + cnt_c)
259  cumfr += cnt_c;
260  else
261  break;
262  c++;
263  }
264  if (x >= 16 || c >= 256) {
265  return AVERROR_INVALIDDATA;
266  }
267 
268  if ((ret = s->decode(gb, rc, cumfr, cnt_c, totfr)) < 0)
269  return ret;
270 
271  pixel->freq[c] = cnt_c + step;
272  pixel->lookup[x] = cnt_x + step;
273  totfr += step;
274  if (totfr > BOT) {
275  totfr = 0;
276  for (i = 0; i < 256; i++) {
277  unsigned nc = (pixel->freq[i] >> 1) + 1;
278  pixel->freq[i] = nc;
279  totfr += nc;
280  }
281  for (i = 0; i < 16; i++) {
282  unsigned sum = 0;
283  unsigned i16_17 = i << 4;
284  for (j = 0; j < 16; j++)
285  sum += pixel->freq[i16_17 + j];
286  pixel->lookup[i] = sum;
287  }
288  }
289  pixel->total_freq = totfr;
290 
291  *rval = c & s->cbits;
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, clr = 0;
301  int run, r, g, b, off, y = 0, x = 0, z, ret;
302  unsigned backstep = linesize - avctx->width;
303  const int cxshift = s->cxshift;
304  unsigned lx, ly, ptype;
305 
306  reinit_tables(s);
307  bytestream2_skip(gb, 2);
308  init_rangecoder(&s->rc, gb);
309 
310  while (k < avctx->width + 1) {
311  ret = decode_unit(s, &s->pixel_model[0][cx + cx1], 400, &r);
312  if (ret < 0)
313  return ret;
314 
315  cx1 = (cx << 6) & 0xFC0;
316  cx = r >> cxshift;
317  ret = decode_unit(s, &s->pixel_model[1][cx + cx1], 400, &g);
318  if (ret < 0)
319  return ret;
320 
321  cx1 = (cx << 6) & 0xFC0;
322  cx = g >> cxshift;
323  ret = decode_unit(s, &s->pixel_model[2][cx + cx1], 400, &b);
324  if (ret < 0)
325  return ret;
326 
327  cx1 = (cx << 6) & 0xFC0;
328  cx = b >> cxshift;
329 
330  ret = decode_value(s, s->run_model[0], 256, 400, &run);
331  if (ret < 0)
332  return ret;
333 
334  clr = (b << 16) + (g << 8) + r;
335  k += run;
336  while (run-- > 0) {
337  if (y >= avctx->height)
338  return AVERROR_INVALIDDATA;
339 
340  dst[y * linesize + x] = clr;
341  lx = x;
342  ly = y;
343  x++;
344  if (x >= avctx->width) {
345  x = 0;
346  y++;
347  }
348  }
349  }
350  off = -linesize - 1;
351  ptype = 0;
352 
353  while (x < avctx->width && y < avctx->height) {
354  ret = decode_value(s, s->op_model[ptype], 6, 1000, &ptype);
355  if (ret < 0)
356  return ret;
357  if (ptype == 0) {
358  ret = decode_unit(s, &s->pixel_model[0][cx + cx1], 400, &r);
359  if (ret < 0)
360  return ret;
361 
362  cx1 = (cx << 6) & 0xFC0;
363  cx = r >> cxshift;
364  ret = decode_unit(s, &s->pixel_model[1][cx + cx1], 400, &g);
365  if (ret < 0)
366  return ret;
367 
368  cx1 = (cx << 6) & 0xFC0;
369  cx = g >> cxshift;
370  ret = decode_unit(s, &s->pixel_model[2][cx + cx1], 400, &b);
371  if (ret < 0)
372  return ret;
373 
374  clr = (b << 16) + (g << 8) + r;
375  }
376  if (ptype > 5)
377  return AVERROR_INVALIDDATA;
378  ret = decode_value(s, s->run_model[ptype], 256, 400, &run);
379  if (ret < 0)
380  return ret;
381 
382  switch (ptype) {
383  case 0:
384  while (run-- > 0) {
385  if (y >= avctx->height)
386  return AVERROR_INVALIDDATA;
387 
388  dst[y * linesize + x] = clr;
389  lx = x;
390  ly = y;
391  x++;
392  if (x >= avctx->width) {
393  x = 0;
394  y++;
395  }
396  }
397  break;
398  case 1:
399  while (run-- > 0) {
400  if (y >= avctx->height)
401  return AVERROR_INVALIDDATA;
402 
403  dst[y * linesize + x] = dst[ly * linesize + lx];
404  lx = x;
405  ly = y;
406  x++;
407  if (x >= avctx->width) {
408  x = 0;
409  y++;
410  }
411  }
412  clr = dst[ly * linesize + lx];
413  break;
414  case 2:
415  while (run-- > 0) {
416  if (y < 1 || y >= avctx->height)
417  return AVERROR_INVALIDDATA;
418 
419  clr = dst[y * linesize + x + off + 1];
420  dst[y * linesize + x] = clr;
421  lx = x;
422  ly = y;
423  x++;
424  if (x >= avctx->width) {
425  x = 0;
426  y++;
427  }
428  }
429  break;
430  case 4:
431  while (run-- > 0) {
432  uint8_t *odst = (uint8_t *)dst;
433 
434  if (y < 1 || y >= avctx->height ||
435  (y == 1 && x == 0))
436  return AVERROR_INVALIDDATA;
437 
438  if (x == 0) {
439  z = backstep;
440  } else {
441  z = 0;
442  }
443 
444  r = odst[(ly * linesize + lx) * 4] +
445  odst[((y * linesize + x) + off - z) * 4 + 4] -
446  odst[((y * linesize + x) + off - z) * 4];
447  g = odst[(ly * linesize + lx) * 4 + 1] +
448  odst[((y * linesize + x) + off - z) * 4 + 5] -
449  odst[((y * linesize + x) + off - z) * 4 + 1];
450  b = odst[(ly * linesize + lx) * 4 + 2] +
451  odst[((y * linesize + x) + off - z) * 4 + 6] -
452  odst[((y * linesize + x) + off - z) * 4 + 2];
453  clr = ((b & 0xFF) << 16) + ((g & 0xFF) << 8) + (r & 0xFF);
454  dst[y * linesize + x] = clr;
455  lx = x;
456  ly = y;
457  x++;
458  if (x >= avctx->width) {
459  x = 0;
460  y++;
461  }
462  }
463  break;
464  case 5:
465  while (run-- > 0) {
466  if (y < 1 || y >= avctx->height ||
467  (y == 1 && x == 0))
468  return AVERROR_INVALIDDATA;
469 
470  if (x == 0) {
471  z = backstep;
472  } else {
473  z = 0;
474  }
475 
476  clr = dst[y * linesize + x + off - z];
477  dst[y * linesize + x] = clr;
478  lx = x;
479  ly = y;
480  x++;
481  if (x >= avctx->width) {
482  x = 0;
483  y++;
484  }
485  }
486  break;
487  }
488 
489  if (avctx->bits_per_coded_sample == 16) {
490  cx1 = (clr & 0x3F00) >> 2;
491  cx = (clr & 0x3FFFFF) >> 16;
492  } else {
493  cx1 = (clr & 0xFC00) >> 4;
494  cx = (clr & 0xFFFFFF) >> 18;
495  }
496  }
497 
498  return 0;
499 }
500 
501 static int decompress_p(AVCodecContext *avctx,
502  uint32_t *dst, int linesize,
503  uint32_t *prev, int plinesize)
504 {
505  SCPRContext *s = avctx->priv_data;
506  GetByteContext *gb = &s->gb;
507  int ret, temp, min, max, x, y, cx = 0, cx1 = 0;
508  int backstep = linesize - avctx->width;
509  const int cxshift = s->cxshift;
510 
511  if (bytestream2_get_byte(gb) == 0)
512  return 0;
513  bytestream2_skip(gb, 1);
514  init_rangecoder(&s->rc, gb);
515 
516  ret = decode_value(s, s->range_model, 256, 1, &min);
517  ret |= decode_value(s, s->range_model, 256, 1, &temp);
518  min += temp << 8;
519  ret |= decode_value(s, s->range_model, 256, 1, &max);
520  ret |= decode_value(s, s->range_model, 256, 1, &temp);
521  if (ret < 0)
522  return ret;
523 
524  max += temp << 8;
525  memset(s->blocks, 0, sizeof(*s->blocks) * s->nbcount);
526 
527  while (min <= max) {
528  int fill, count;
529 
530  ret = decode_value(s, s->fill_model, 5, 10, &fill);
531  ret |= decode_value(s, s->count_model, 256, 20, &count);
532  if (ret < 0)
533  return ret;
534 
535  while (min < s->nbcount && count-- > 0) {
536  s->blocks[min++] = fill;
537  }
538  }
539 
540  for (y = 0; y < s->nby; y++) {
541  for (x = 0; x < s->nbx; x++) {
542  int sy1 = 0, sy2 = 16, sx1 = 0, sx2 = 16;
543 
544  if (s->blocks[y * s->nbx + x] == 0)
545  continue;
546 
547  if (((s->blocks[y * s->nbx + x] - 1) & 1) > 0) {
548  ret = decode_value(s, s->sxy_model[0], 16, 100, &sx1);
549  ret |= decode_value(s, s->sxy_model[1], 16, 100, &sy1);
550  ret |= decode_value(s, s->sxy_model[2], 16, 100, &sx2);
551  ret |= decode_value(s, s->sxy_model[3], 16, 100, &sy2);
552  if (ret < 0)
553  return ret;
554 
555  sx2++;
556  sy2++;
557  }
558  if (((s->blocks[y * s->nbx + x] - 1) & 2) > 0) {
559  int i, j, by = y * 16, bx = x * 16;
560  int mvx, mvy;
561 
562  ret = decode_value(s, s->mv_model[0], 512, 100, &mvx);
563  ret |= decode_value(s, s->mv_model[1], 512, 100, &mvy);
564  if (ret < 0)
565  return ret;
566 
567  mvx -= 256;
568  mvy -= 256;
569 
570  if (by + mvy + sy1 < 0 || bx + mvx + sx1 < 0 ||
571  by + mvy + sy1 >= avctx->height || bx + mvx + sx1 >= avctx->width)
572  return AVERROR_INVALIDDATA;
573 
574  for (i = 0; i < sy2 - sy1 && (by + sy1 + i) < avctx->height && (by + mvy + sy1 + i) < avctx->height; i++) {
575  for (j = 0; j < sx2 - sx1 && (bx + sx1 + j) < avctx->width && (bx + mvx + sx1 + j) < avctx->width; j++) {
576  dst[(by + i + sy1) * linesize + bx + sx1 + j] = prev[(by + mvy + sy1 + i) * plinesize + bx + sx1 + mvx + j];
577  }
578  }
579  } else {
580  int run, r, g, b, z, bx = x * 16 + sx1, by = y * 16 + sy1;
581  unsigned clr, ptype = 0;
582 
583  for (; by < y * 16 + sy2 && by < avctx->height;) {
584  ret = decode_value(s, s->op_model[ptype], 6, 1000, &ptype);
585  if (ret < 0)
586  return ret;
587  if (ptype == 0) {
588  ret = decode_unit(s, &s->pixel_model[0][cx + cx1], 400, &r);
589  if (ret < 0)
590  return ret;
591 
592  cx1 = (cx << 6) & 0xFC0;
593  cx = r >> cxshift;
594  ret = decode_unit(s, &s->pixel_model[1][cx + cx1], 400, &g);
595  if (ret < 0)
596  return ret;
597 
598  cx1 = (cx << 6) & 0xFC0;
599  cx = g >> cxshift;
600  ret = decode_unit(s, &s->pixel_model[2][cx + cx1], 400, &b);
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 
612  switch (ptype) {
613  case 0:
614  while (run-- > 0) {
615  if (by >= avctx->height)
616  return AVERROR_INVALIDDATA;
617 
618  dst[by * linesize + bx] = clr;
619  bx++;
620  if (bx >= x * 16 + sx2 || bx >= avctx->width) {
621  bx = x * 16 + sx1;
622  by++;
623  }
624  }
625  break;
626  case 1:
627  while (run-- > 0) {
628  if (bx == 0) {
629  if (by < 1)
630  return AVERROR_INVALIDDATA;
631  z = backstep;
632  } else {
633  z = 0;
634  }
635 
636  if (by >= avctx->height)
637  return AVERROR_INVALIDDATA;
638 
639  clr = dst[by * linesize + bx - 1 - z];
640  dst[by * linesize + bx] = clr;
641  bx++;
642  if (bx >= x * 16 + sx2 || bx >= avctx->width) {
643  bx = x * 16 + sx1;
644  by++;
645  }
646  }
647  break;
648  case 2:
649  while (run-- > 0) {
650  if (by < 1 || by >= avctx->height)
651  return AVERROR_INVALIDDATA;
652 
653  clr = dst[(by - 1) * linesize + bx];
654  dst[by * linesize + bx] = clr;
655  bx++;
656  if (bx >= x * 16 + sx2 || bx >= avctx->width) {
657  bx = x * 16 + sx1;
658  by++;
659  }
660  }
661  break;
662  case 3:
663  while (run-- > 0) {
664  if (by >= avctx->height)
665  return AVERROR_INVALIDDATA;
666 
667  clr = prev[by * plinesize + bx];
668  dst[by * linesize + bx] = clr;
669  bx++;
670  if (bx >= x * 16 + sx2 || bx >= avctx->width) {
671  bx = x * 16 + sx1;
672  by++;
673  }
674  }
675  break;
676  case 4:
677  while (run-- > 0) {
678  uint8_t *odst = (uint8_t *)dst;
679 
680  if (by < 1 || by >= avctx->height)
681  return AVERROR_INVALIDDATA;
682 
683  if (bx == 0) {
684  z = backstep;
685  } else {
686  z = 0;
687  }
688 
689  r = odst[((by - 1) * linesize + bx) * 4] +
690  odst[(by * linesize + bx - 1 - z) * 4] -
691  odst[((by - 1) * linesize + bx - 1 - z) * 4];
692  g = odst[((by - 1) * linesize + bx) * 4 + 1] +
693  odst[(by * linesize + bx - 1 - z) * 4 + 1] -
694  odst[((by - 1) * linesize + bx - 1 - z) * 4 + 1];
695  b = odst[((by - 1) * linesize + bx) * 4 + 2] +
696  odst[(by * linesize + bx - 1 - z) * 4 + 2] -
697  odst[((by - 1) * linesize + bx - 1 - z) * 4 + 2];
698  clr = ((b & 0xFF) << 16) + ((g & 0xFF) << 8) + (r & 0xFF);
699  dst[by * linesize + bx] = clr;
700  bx++;
701  if (bx >= x * 16 + sx2 || bx >= avctx->width) {
702  bx = x * 16 + sx1;
703  by++;
704  }
705  }
706  break;
707  case 5:
708  while (run-- > 0) {
709  if (by < 1 || by >= avctx->height)
710  return AVERROR_INVALIDDATA;
711 
712  if (bx == 0) {
713  z = backstep;
714  } else {
715  z = 0;
716  }
717 
718  clr = dst[(by - 1) * linesize + bx - 1 - z];
719  dst[by * linesize + bx] = clr;
720  bx++;
721  if (bx >= x * 16 + sx2 || bx >= avctx->width) {
722  bx = x * 16 + sx1;
723  by++;
724  }
725  }
726  break;
727  }
728 
729  if (avctx->bits_per_coded_sample == 16) {
730  cx1 = (clr & 0x3F00) >> 2;
731  cx = (clr & 0x3FFFFF) >> 16;
732  } else {
733  cx1 = (clr & 0xFC00) >> 4;
734  cx = (clr & 0xFFFFFF) >> 18;
735  }
736  }
737  }
738  }
739  }
740 
741  return 0;
742 }
743 
744 static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
745  AVPacket *avpkt)
746 {
747  SCPRContext *s = avctx->priv_data;
748  GetByteContext *gb = &s->gb;
749  AVFrame *frame = data;
750  int ret, type;
751 
752  if (avctx->bits_per_coded_sample == 16) {
753  if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
754  return ret;
755  }
756 
757  if ((ret = ff_reget_buffer(avctx, s->current_frame)) < 0)
758  return ret;
759 
760  bytestream2_init(gb, avpkt->data, avpkt->size);
761 
762  type = bytestream2_peek_byte(gb);
763 
764  if (type == 2) {
765  s->get_freq = get_freq0;
766  s->decode = decode0;
767  frame->key_frame = 1;
768  ret = decompress_i(avctx, (uint32_t *)s->current_frame->data[0],
769  s->current_frame->linesize[0] / 4);
770  } else if (type == 18) {
771  s->get_freq = get_freq;
772  s->decode = decode;
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 == 17) {
777  uint32_t clr, *dst = (uint32_t *)s->current_frame->data[0];
778  int x, y;
779 
780  frame->key_frame = 1;
781  bytestream2_skip(gb, 1);
782  if (avctx->bits_per_coded_sample == 16) {
783  uint16_t value = bytestream2_get_le16(gb);
784  int r, g, b;
785 
786  r = (value ) & 31;
787  g = (value >> 5) & 31;
788  b = (value >> 10) & 31;
789  clr = (r << 16) + (g << 8) + b;
790  } else {
791  clr = bytestream2_get_le24(gb);
792  }
793  for (y = 0; y < avctx->height; y++) {
794  for (x = 0; x < avctx->width; x++) {
795  dst[x] = clr;
796  }
797  dst += s->current_frame->linesize[0] / 4;
798  }
799  } else if (type == 0 || type == 1) {
800  frame->key_frame = 0;
801 
802  ret = av_frame_copy(s->current_frame, s->last_frame);
803  if (ret < 0)
804  return ret;
805 
806  ret = decompress_p(avctx, (uint32_t *)s->current_frame->data[0],
807  s->current_frame->linesize[0] / 4,
808  (uint32_t *)s->last_frame->data[0],
809  s->last_frame->linesize[0] / 4);
810  } else {
811  return AVERROR_PATCHWELCOME;
812  }
813 
814  if (ret < 0)
815  return ret;
816 
817  if (avctx->bits_per_coded_sample != 16) {
818  ret = av_frame_ref(data, s->current_frame);
819  if (ret < 0)
820  return ret;
821  } else {
822  uint8_t *dst = frame->data[0];
823  int x, y;
824 
825  ret = av_frame_copy(frame, s->current_frame);
826  if (ret < 0)
827  return ret;
828 
829  // scale up each sample by 8
830  for (y = 0; y < avctx->height; y++) {
831  // If the image is sufficiently aligned, compute 8 samples at once
832  if (!(((uintptr_t)dst) & 7)) {
833  uint64_t *dst64 = (uint64_t *)dst;
834  int w = avctx->width>>1;
835  for (x = 0; x < w; x++) {
836  dst64[x] = (dst64[x] << 3) & 0xFCFCFCFCFCFCFCFCULL;
837  }
838  x *= 8;
839  } else
840  x = 0;
841  for (; x < avctx->width * 4; x++) {
842  dst[x] = dst[x] << 3;
843  }
844  dst += frame->linesize[0];
845  }
846  }
847 
849 
851 
852  frame->data[0] += frame->linesize[0] * (avctx->height - 1);
853  frame->linesize[0] *= -1;
854 
855  *got_frame = 1;
856 
857  return avpkt->size;
858 }
859 
861 {
862  SCPRContext *s = avctx->priv_data;
863 
864  switch (avctx->bits_per_coded_sample) {
865  case 16: avctx->pix_fmt = AV_PIX_FMT_RGB0; break;
866  case 24:
867  case 32: avctx->pix_fmt = AV_PIX_FMT_BGR0; break;
868  default:
869  av_log(avctx, AV_LOG_ERROR, "Unsupported bitdepth %i\n", avctx->bits_per_coded_sample);
870  return AVERROR_INVALIDDATA;
871  }
872 
873  s->get_freq = get_freq0;
874  s->decode = decode0;
875 
876  s->cxshift = avctx->bits_per_coded_sample == 16 ? 0 : 2;
877  s->cbits = avctx->bits_per_coded_sample == 16 ? 0x1F : 0xFF;
878  s->nbx = (avctx->width + 15) / 16;
879  s->nby = (avctx->height + 15) / 16;
880  s->nbcount = s->nbx * s->nby;
881  s->blocks = av_malloc_array(s->nbcount, sizeof(*s->blocks));
882  if (!s->blocks)
883  return AVERROR(ENOMEM);
884 
885  s->last_frame = av_frame_alloc();
887  if (!s->last_frame || !s->current_frame)
888  return AVERROR(ENOMEM);
889 
890  return 0;
891 }
892 
894 {
895  SCPRContext *s = avctx->priv_data;
896 
897  av_freep(&s->blocks);
900 
901  return 0;
902 }
903 
905  .name = "scpr",
906  .long_name = NULL_IF_CONFIG_SMALL("ScreenPressor"),
907  .type = AVMEDIA_TYPE_VIDEO,
908  .id = AV_CODEC_ID_SCPR,
909  .priv_data_size = sizeof(SCPRContext),
910  .init = decode_init,
911  .close = decode_close,
912  .decode = decode_frame,
913  .capabilities = AV_CODEC_CAP_DR1,
914  .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE |
916 };
#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:860
int(* decode)(GetByteContext *gb, RangeCoder *rc, unsigned cumFreq, unsigned freq, unsigned total_freq)
Definition: scpr.c:66
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:201
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: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:1415
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:1711
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:296
static int decompress_p(AVCodecContext *avctx, uint32_t *dst, int linesize, uint32_t *prev, int plinesize)
Definition: scpr.c:501
AVCodec.
Definition: avcodec.h:3365
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:1935
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:150
packed RGB 8:8:8, 32bpp, RGBXRGBX... X=unused/undefined
Definition: pixfmt.h:234
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:400
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:744
#define height
uint8_t * data
Definition: avcodec.h:1414
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:2718
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:893
#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:163
#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:3372
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:746
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:284
int width
picture width / height.
Definition: avcodec.h:1674
GLsizei GLboolean const GLfloat * value
Definition: opengl_enc.c:109
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
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:232
main external API structure.
Definition: avcodec.h:1502
#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:1886
GLint GLenum type
Definition: opengl_enc.c:105
packed BGR 8:8:8, 32bpp, BGRXBGRX... X=unused/undefined
Definition: pixfmt.h:236
AVCodec ff_scpr_decoder
Definition: scpr.c:904
#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:215
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:234
static double c[64]
void * priv_data
Definition: avcodec.h:1529
int key_frame
1 -> keyframe, 0-> not
Definition: frame.h:279
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
float min
This structure stores compressed data.
Definition: avcodec.h:1391
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
Definition: avcodec.h:956
unsigned nbx
Definition: scpr.c:59
for(j=16;j >0;--j)
Predicted.
Definition: avutil.h:275