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 *px, int *py, uint32_t clr, uint32_t *dst,
82 int linesize, uint32_t *plx, uint32_t *ply,
83 uint32_t backstep, int off, int *cx, int *cx1)
92 if (y >= avctx->height)
93 return AVERROR_INVALIDDATA;
98 dst[y * linesize + x] = clr;
102 if (x >= avctx->width) {
105 if (y >= avctx->height && run)
106 return AVERROR_INVALIDDATA;
112 dst[y * linesize + x] = dst[ly * linesize + lx];
116 if (x >= avctx->width) {
119 if (y >= avctx->height && run)
120 return AVERROR_INVALIDDATA;
123 clr = dst[ly * linesize + lx];
127 return AVERROR_INVALIDDATA;
130 clr = dst[y * linesize + x + off + 1];
131 dst[y * linesize + x] = clr;
135 if (x >= avctx->width) {
138 if (y >= avctx->height && run)
139 return AVERROR_INVALIDDATA;
144 if (y < 1 || (y == 1 && x == 0))
145 return AVERROR_INVALIDDATA;
148 uint8_t *odst = (uint8_t *)dst;
149 int off1 = (ly * linesize + lx) * 4;
150 int off2 = ((y * linesize + x) + off) * 4;
167 clr = ((b & 0xFF) << 16) + ((g & 0xFF) << 8) + (r & 0xFF);
168 dst[y * linesize + x] = clr;
172 if (x >= avctx->width) {
175 if (y >= avctx->height && run)
176 return AVERROR_INVALIDDATA;
181 if (y < 1 || (y == 1 && x == 0))
182 return AVERROR_INVALIDDATA;
191 clr = dst[y * linesize + x + off - z];
192 dst[y * linesize + x] = clr;
196 if (x >= avctx->width) {
199 if (y >= avctx->height && run)
200 return AVERROR_INVALIDDATA;
211 if (avctx->bits_per_coded_sample == 16) {
212 *cx1 = (clr & 0x3F00) >> 2;
213 *cx = (clr & 0x3FFFFF) >> 16;
215 *cx1 = (clr & 0xFC00) >> 4;
216 *cx = (clr & 0xFFFFFF) >> 18;
222 static int decode_run_p(AVCodecContext *avctx, uint32_t ptype, int run,
223 int x, int y, uint32_t clr,
224 uint32_t *dst, uint32_t *prev,
225 int linesize, int plinesize,
226 uint32_t *bx, uint32_t *by,
227 uint32_t backstep, int sx1, int sx2,
236 if (*by >= avctx->height)
237 return AVERROR_INVALIDDATA;
239 dst[*by * linesize + *bx] = clr;
241 if (*bx >= x * 16 + sx2 || *bx >= avctx->width) {
251 return AVERROR_INVALIDDATA;
257 if (*by >= avctx->height)
258 return AVERROR_INVALIDDATA;
260 clr = dst[*by * linesize + *bx - 1 - z];
261 dst[*by * linesize + *bx] = clr;
263 if (*bx >= x * 16 + sx2 || *bx >= avctx->width) {
271 if (*by < 1 || *by >= avctx->height)
272 return AVERROR_INVALIDDATA;
274 clr = dst[(*by - 1) * linesize + *bx];
275 dst[*by * linesize + *bx] = clr;
277 if (*bx >= x * 16 + sx2 || *bx >= avctx->width) {
285 if (*by >= avctx->height)
286 return AVERROR_INVALIDDATA;
288 clr = prev[*by * plinesize + *bx];
289 dst[*by * linesize + *bx] = clr;
291 if (*bx >= x * 16 + sx2 || *bx >= avctx->width) {
299 uint8_t *odst = (uint8_t *)dst;
301 if (*by < 1 || *by >= avctx->height)
302 return AVERROR_INVALIDDATA;
306 return AVERROR_INVALIDDATA;
312 r = odst[((*by - 1) * linesize + *bx) * 4] +
313 odst[(*by * linesize + *bx - 1 - z) * 4] -
314 odst[((*by - 1) * linesize + *bx - 1 - z) * 4];
315 g = odst[((*by - 1) * linesize + *bx) * 4 + 1] +
316 odst[(*by * linesize + *bx - 1 - z) * 4 + 1] -
317 odst[((*by - 1) * linesize + *bx - 1 - z) * 4 + 1];
318 b = odst[((*by - 1) * linesize + *bx) * 4 + 2] +
319 odst[(*by * linesize + *bx - 1 - z) * 4 + 2] -
320 odst[((*by - 1) * linesize + *bx - 1 - z) * 4 + 2];
321 clr = ((b & 0xFF) << 16) + ((g & 0xFF) << 8) + (r & 0xFF);
322 dst[*by * linesize + *bx] = clr;
324 if (*bx >= x * 16 + sx2 || *bx >= avctx->width) {
332 if (*by < 1 || *by >= avctx->height)
333 return AVERROR_INVALIDDATA;
337 return AVERROR_INVALIDDATA;
343 clr = dst[(*by - 1) * linesize + *bx - 1 - z];
344 dst[*by * linesize + *bx] = clr;
346 if (*bx >= x * 16 + sx2 || *bx >= avctx->width) {
354 if (avctx->bits_per_coded_sample == 16) {
355 *cx1 = (clr & 0x3F00) >> 2;
356 *cx = (clr & 0x3FFFFF) >> 16;
358 *cx1 = (clr & 0xFC00) >> 4;
359 *cx = (clr & 0xFFFFFF) >> 18;
365 #endif /* AVCODEC_SCPR_H */