2 * ScreenPressor decoder
4 * Copyright (c) 2017 Paul B Mahol
6 * This file is part of FFmpeg.
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.
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.
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
23 #ifndef AVCODEC_SCPR_H
24 #define AVCODEC_SCPR_H
31 #include "bytestream.h"
35 typedef struct RangeCoder {
41 typedef struct PixelModel {
47 typedef struct SCPRContext {
50 AVFrame *current_frame;
53 PixelModel pixel_model[3][4096];
54 uint32_t op_model[6][7];
55 uint32_t run_model[6][257];
56 uint32_t range_model[257];
57 uint32_t count_model[257];
58 uint32_t fill_model[6];
59 uint32_t sxy_model[4][17];
60 uint32_t mv_model[2][513];
67 PixelModel3 pixel_model3[3][4096];
68 RunModel3 run_model3[6];
69 RunModel3 range_model3;
70 RunModel3 count_model3;
71 FillModel3 fill_model3;
72 SxyModel3 sxy_model3[4];
73 MVModel3 mv_model3[2];
74 OpModel3 op_model3[6];
76 int (*get_freq)(RangeCoder *rc, uint32_t total_freq, uint32_t *freq);
77 int (*decode)(GetByteContext *gb, RangeCoder *rc, uint32_t cumFreq, uint32_t freq, uint32_t total_freq);
80 static int decode_run_i(AVCodecContext *avctx, uint32_t ptype, int run,
81 int *x, int *y, uint32_t clr, uint32_t *dst,
82 int linesize, uint32_t *lx, uint32_t *ly,
83 uint32_t backstep, int off, int *cx, int *cx1)
91 if (*y >= avctx->height)
92 return AVERROR_INVALIDDATA;
94 dst[*y * linesize + *x] = clr;
98 if (*x >= avctx->width) {
106 if (*y >= avctx->height)
107 return AVERROR_INVALIDDATA;
109 dst[*y * linesize + *x] = dst[*ly * linesize + *lx];
113 if (*x >= avctx->width) {
118 clr = dst[*ly * linesize + *lx];
122 if (*y < 1 || *y >= avctx->height)
123 return AVERROR_INVALIDDATA;
125 clr = dst[*y * linesize + *x + off + 1];
126 dst[*y * linesize + *x] = clr;
130 if (*x >= avctx->width) {
138 uint8_t *odst = (uint8_t *)dst;
140 if (*y < 1 || *y >= avctx->height ||
141 (*y == 1 && *x == 0))
142 return AVERROR_INVALIDDATA;
150 r = odst[(*ly * linesize + *lx) * 4] +
151 odst[((*y * linesize + *x) + off) * 4 + 4] -
152 odst[((*y * linesize + *x) + off - z) * 4];
153 g = odst[(*ly * linesize + *lx) * 4 + 1] +
154 odst[((*y * linesize + *x) + off) * 4 + 5] -
155 odst[((*y * linesize + *x) + off - z) * 4 + 1];
156 b = odst[(*ly * linesize + *lx) * 4 + 2] +
157 odst[((*y * linesize + *x) + off) * 4 + 6] -
158 odst[((*y * linesize + *x) + off - z) * 4 + 2];
159 clr = ((b & 0xFF) << 16) + ((g & 0xFF) << 8) + (r & 0xFF);
160 dst[*y * linesize + *x] = clr;
164 if (*x >= avctx->width) {
172 if (*y < 1 || *y >= avctx->height ||
173 (*y == 1 && *x == 0))
174 return AVERROR_INVALIDDATA;
182 clr = dst[*y * linesize + *x + off - z];
183 dst[*y * linesize + *x] = clr;
187 if (*x >= avctx->width) {
195 if (avctx->bits_per_coded_sample == 16) {
196 *cx1 = (clr & 0x3F00) >> 2;
197 *cx = (clr & 0x3FFFFF) >> 16;
199 *cx1 = (clr & 0xFC00) >> 4;
200 *cx = (clr & 0xFFFFFF) >> 18;
206 static int decode_run_p(AVCodecContext *avctx, uint32_t ptype, int run,
207 int x, int y, uint32_t clr,
208 uint32_t *dst, uint32_t *prev,
209 int linesize, int plinesize,
210 uint32_t *bx, uint32_t *by,
211 uint32_t backstep, int sx1, int sx2,
220 if (*by >= avctx->height)
221 return AVERROR_INVALIDDATA;
223 dst[*by * linesize + *bx] = clr;
225 if (*bx >= x * 16 + sx2 || *bx >= avctx->width) {
235 return AVERROR_INVALIDDATA;
241 if (*by >= avctx->height)
242 return AVERROR_INVALIDDATA;
244 clr = dst[*by * linesize + *bx - 1 - z];
245 dst[*by * linesize + *bx] = clr;
247 if (*bx >= x * 16 + sx2 || *bx >= avctx->width) {
255 if (*by < 1 || *by >= avctx->height)
256 return AVERROR_INVALIDDATA;
258 clr = dst[(*by - 1) * linesize + *bx];
259 dst[*by * linesize + *bx] = clr;
261 if (*bx >= x * 16 + sx2 || *bx >= avctx->width) {
269 if (*by >= avctx->height)
270 return AVERROR_INVALIDDATA;
272 clr = prev[*by * plinesize + *bx];
273 dst[*by * linesize + *bx] = clr;
275 if (*bx >= x * 16 + sx2 || *bx >= avctx->width) {
283 uint8_t *odst = (uint8_t *)dst;
285 if (*by < 1 || *by >= avctx->height)
286 return AVERROR_INVALIDDATA;
290 return AVERROR_INVALIDDATA;
296 r = odst[((*by - 1) * linesize + *bx) * 4] +
297 odst[(*by * linesize + *bx - 1 - z) * 4] -
298 odst[((*by - 1) * linesize + *bx - 1 - z) * 4];
299 g = odst[((*by - 1) * linesize + *bx) * 4 + 1] +
300 odst[(*by * linesize + *bx - 1 - z) * 4 + 1] -
301 odst[((*by - 1) * linesize + *bx - 1 - z) * 4 + 1];
302 b = odst[((*by - 1) * linesize + *bx) * 4 + 2] +
303 odst[(*by * linesize + *bx - 1 - z) * 4 + 2] -
304 odst[((*by - 1) * linesize + *bx - 1 - z) * 4 + 2];
305 clr = ((b & 0xFF) << 16) + ((g & 0xFF) << 8) + (r & 0xFF);
306 dst[*by * linesize + *bx] = clr;
308 if (*bx >= x * 16 + sx2 || *bx >= avctx->width) {
316 if (*by < 1 || *by >= avctx->height)
317 return AVERROR_INVALIDDATA;
321 return AVERROR_INVALIDDATA;
327 clr = dst[(*by - 1) * linesize + *bx - 1 - z];
328 dst[*by * linesize + *bx] = clr;
330 if (*bx >= x * 16 + sx2 || *bx >= avctx->width) {
338 if (avctx->bits_per_coded_sample == 16) {
339 *cx1 = (clr & 0x3F00) >> 2;
340 *cx = (clr & 0x3FFFFF) >> 16;
342 *cx1 = (clr & 0xFC00) >> 4;
343 *cx = (clr & 0xFFFFFF) >> 18;
349 #endif /* AVCODEC_SCPR_H */