FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/sanm.c
Date: 2025-06-01 09:29:47
Exec Total Coverage
Lines: 329 1349 24.4%
Functions: 25 51 49.0%
Branches: 165 882 18.7%

Line Branch Exec Source
1 /*
2 * LucasArts Smush video decoder
3 * Copyright (c) 2006 Cyril Zorin
4 * Copyright (c) 2011 Konstantin Shishkov
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 "libavutil/avassert.h"
24 #include "libavutil/mem.h"
25
26 #include "avcodec.h"
27 #include "bytestream.h"
28 #include "copy_block.h"
29 #include "codec_internal.h"
30 #include "decode.h"
31
32 #define NGLYPHS 256
33 #define GLYPH_COORD_VECT_SIZE 16
34 #define PALETTE_SIZE 256
35 #define PALETTE_DELTA 768
36
37 static const int8_t glyph4_x[GLYPH_COORD_VECT_SIZE] = {
38 0, 1, 2, 3, 3, 3, 3, 2, 1, 0, 0, 0, 1, 2, 2, 1
39 };
40
41 static const int8_t glyph4_y[GLYPH_COORD_VECT_SIZE] = {
42 0, 0, 0, 0, 1, 2, 3, 3, 3, 3, 2, 1, 1, 1, 2, 2
43 };
44
45 static const int8_t glyph8_x[GLYPH_COORD_VECT_SIZE] = {
46 0, 2, 5, 7, 7, 7, 7, 7, 7, 5, 2, 0, 0, 0, 0, 0
47 };
48
49 static const int8_t glyph8_y[GLYPH_COORD_VECT_SIZE] = {
50 0, 0, 0, 0, 1, 3, 4, 6, 7, 7, 7, 7, 6, 4, 3, 1
51 };
52
53 static const int8_t motion_vectors[256][2] = {
54 { 0, 0 }, { -1, -43 }, { 6, -43 }, { -9, -42 }, { 13, -41 },
55 { -16, -40 }, { 19, -39 }, { -23, -36 }, { 26, -34 }, { -2, -33 },
56 { 4, -33 }, { -29, -32 }, { -9, -32 }, { 11, -31 }, { -16, -29 },
57 { 32, -29 }, { 18, -28 }, { -34, -26 }, { -22, -25 }, { -1, -25 },
58 { 3, -25 }, { -7, -24 }, { 8, -24 }, { 24, -23 }, { 36, -23 },
59 { -12, -22 }, { 13, -21 }, { -38, -20 }, { 0, -20 }, { -27, -19 },
60 { -4, -19 }, { 4, -19 }, { -17, -18 }, { -8, -17 }, { 8, -17 },
61 { 18, -17 }, { 28, -17 }, { 39, -17 }, { -12, -15 }, { 12, -15 },
62 { -21, -14 }, { -1, -14 }, { 1, -14 }, { -41, -13 }, { -5, -13 },
63 { 5, -13 }, { 21, -13 }, { -31, -12 }, { -15, -11 }, { -8, -11 },
64 { 8, -11 }, { 15, -11 }, { -2, -10 }, { 1, -10 }, { 31, -10 },
65 { -23, -9 }, { -11, -9 }, { -5, -9 }, { 4, -9 }, { 11, -9 },
66 { 42, -9 }, { 6, -8 }, { 24, -8 }, { -18, -7 }, { -7, -7 },
67 { -3, -7 }, { -1, -7 }, { 2, -7 }, { 18, -7 }, { -43, -6 },
68 { -13, -6 }, { -4, -6 }, { 4, -6 }, { 8, -6 }, { -33, -5 },
69 { -9, -5 }, { -2, -5 }, { 0, -5 }, { 2, -5 }, { 5, -5 },
70 { 13, -5 }, { -25, -4 }, { -6, -4 }, { -3, -4 }, { 3, -4 },
71 { 9, -4 }, { -19, -3 }, { -7, -3 }, { -4, -3 }, { -2, -3 },
72 { -1, -3 }, { 0, -3 }, { 1, -3 }, { 2, -3 }, { 4, -3 },
73 { 6, -3 }, { 33, -3 }, { -14, -2 }, { -10, -2 }, { -5, -2 },
74 { -3, -2 }, { -2, -2 }, { -1, -2 }, { 0, -2 }, { 1, -2 },
75 { 2, -2 }, { 3, -2 }, { 5, -2 }, { 7, -2 }, { 14, -2 },
76 { 19, -2 }, { 25, -2 }, { 43, -2 }, { -7, -1 }, { -3, -1 },
77 { -2, -1 }, { -1, -1 }, { 0, -1 }, { 1, -1 }, { 2, -1 },
78 { 3, -1 }, { 10, -1 }, { -5, 0 }, { -3, 0 }, { -2, 0 },
79 { -1, 0 }, { 1, 0 }, { 2, 0 }, { 3, 0 }, { 5, 0 },
80 { 7, 0 }, { -10, 1 }, { -7, 1 }, { -3, 1 }, { -2, 1 },
81 { -1, 1 }, { 0, 1 }, { 1, 1 }, { 2, 1 }, { 3, 1 },
82 { -43, 2 }, { -25, 2 }, { -19, 2 }, { -14, 2 }, { -5, 2 },
83 { -3, 2 }, { -2, 2 }, { -1, 2 }, { 0, 2 }, { 1, 2 },
84 { 2, 2 }, { 3, 2 }, { 5, 2 }, { 7, 2 }, { 10, 2 },
85 { 14, 2 }, { -33, 3 }, { -6, 3 }, { -4, 3 }, { -2, 3 },
86 { -1, 3 }, { 0, 3 }, { 1, 3 }, { 2, 3 }, { 4, 3 },
87 { 19, 3 }, { -9, 4 }, { -3, 4 }, { 3, 4 }, { 7, 4 },
88 { 25, 4 }, { -13, 5 }, { -5, 5 }, { -2, 5 }, { 0, 5 },
89 { 2, 5 }, { 5, 5 }, { 9, 5 }, { 33, 5 }, { -8, 6 },
90 { -4, 6 }, { 4, 6 }, { 13, 6 }, { 43, 6 }, { -18, 7 },
91 { -2, 7 }, { 0, 7 }, { 2, 7 }, { 7, 7 }, { 18, 7 },
92 { -24, 8 }, { -6, 8 }, { -42, 9 }, { -11, 9 }, { -4, 9 },
93 { 5, 9 }, { 11, 9 }, { 23, 9 }, { -31, 10 }, { -1, 10 },
94 { 2, 10 }, { -15, 11 }, { -8, 11 }, { 8, 11 }, { 15, 11 },
95 { 31, 12 }, { -21, 13 }, { -5, 13 }, { 5, 13 }, { 41, 13 },
96 { -1, 14 }, { 1, 14 }, { 21, 14 }, { -12, 15 }, { 12, 15 },
97 { -39, 17 }, { -28, 17 }, { -18, 17 }, { -8, 17 }, { 8, 17 },
98 { 17, 18 }, { -4, 19 }, { 0, 19 }, { 4, 19 }, { 27, 19 },
99 { 38, 20 }, { -13, 21 }, { 12, 22 }, { -36, 23 }, { -24, 23 },
100 { -8, 24 }, { 7, 24 }, { -3, 25 }, { 1, 25 }, { 22, 25 },
101 { 34, 26 }, { -18, 28 }, { -32, 29 }, { 16, 29 }, { -11, 31 },
102 { 9, 32 }, { 29, 32 }, { -4, 33 }, { 2, 33 }, { -26, 34 },
103 { 23, 36 }, { -19, 39 }, { 16, 40 }, { -13, 41 }, { 9, 42 },
104 { -6, 43 }, { 1, 43 }, { 0, 0 }, { 0, 0 }, { 0, 0 },
105 };
106
107 static const int8_t c37_mv[] = {
108 0, 0, 1, 0, 2, 0, 3, 0, 5, 0,
109 8, 0, 13, 0, 21, 0, -1, 0, -2, 0,
110 -3, 0, -5, 0, -8, 0, -13, 0, -17, 0,
111 -21, 0, 0, 1, 1, 1, 2, 1, 3, 1,
112 5, 1, 8, 1, 13, 1, 21, 1, -1, 1,
113 -2, 1, -3, 1, -5, 1, -8, 1, -13, 1,
114 -17, 1, -21, 1, 0, 2, 1, 2, 2, 2,
115 3, 2, 5, 2, 8, 2, 13, 2, 21, 2,
116 -1, 2, -2, 2, -3, 2, -5, 2, -8, 2,
117 -13, 2, -17, 2, -21, 2, 0, 3, 1, 3,
118 2, 3, 3, 3, 5, 3, 8, 3, 13, 3,
119 21, 3, -1, 3, -2, 3, -3, 3, -5, 3,
120 -8, 3, -13, 3, -17, 3, -21, 3, 0, 5,
121 1, 5, 2, 5, 3, 5, 5, 5, 8, 5,
122 13, 5, 21, 5, -1, 5, -2, 5, -3, 5,
123 -5, 5, -8, 5, -13, 5, -17, 5, -21, 5,
124 0, 8, 1, 8, 2, 8, 3, 8, 5, 8,
125 8, 8, 13, 8, 21, 8, -1, 8, -2, 8,
126 -3, 8, -5, 8, -8, 8, -13, 8, -17, 8,
127 -21, 8, 0, 13, 1, 13, 2, 13, 3, 13,
128 5, 13, 8, 13, 13, 13, 21, 13, -1, 13,
129 -2, 13, -3, 13, -5, 13, -8, 13, -13, 13,
130 -17, 13, -21, 13, 0, 21, 1, 21, 2, 21,
131 3, 21, 5, 21, 8, 21, 13, 21, 21, 21,
132 -1, 21, -2, 21, -3, 21, -5, 21, -8, 21,
133 -13, 21, -17, 21, -21, 21, 0, -1, 1, -1,
134 2, -1, 3, -1, 5, -1, 8, -1, 13, -1,
135 21, -1, -1, -1, -2, -1, -3, -1, -5, -1,
136 -8, -1, -13, -1, -17, -1, -21, -1, 0, -2,
137 1, -2, 2, -2, 3, -2, 5, -2, 8, -2,
138 13, -2, 21, -2, -1, -2, -2, -2, -3, -2,
139 -5, -2, -8, -2, -13, -2, -17, -2, -21, -2,
140 0, -3, 1, -3, 2, -3, 3, -3, 5, -3,
141 8, -3, 13, -3, 21, -3, -1, -3, -2, -3,
142 -3, -3, -5, -3, -8, -3, -13, -3, -17, -3,
143 -21, -3, 0, -5, 1, -5, 2, -5, 3, -5,
144 5, -5, 8, -5, 13, -5, 21, -5, -1, -5,
145 -2, -5, -3, -5, -5, -5, -8, -5, -13, -5,
146 -17, -5, -21, -5, 0, -8, 1, -8, 2, -8,
147 3, -8, 5, -8, 8, -8, 13, -8, 21, -8,
148 -1, -8, -2, -8, -3, -8, -5, -8, -8, -8,
149 -13, -8, -17, -8, -21, -8, 0, -13, 1, -13,
150 2, -13, 3, -13, 5, -13, 8, -13, 13, -13,
151 21, -13, -1, -13, -2, -13, -3, -13, -5, -13,
152 -8, -13, -13, -13, -17, -13, -21, -13, 0, -17,
153 1, -17, 2, -17, 3, -17, 5, -17, 8, -17,
154 13, -17, 21, -17, -1, -17, -2, -17, -3, -17,
155 -5, -17, -8, -17, -13, -17, -17, -17, -21, -17,
156 0, -21, 1, -21, 2, -21, 3, -21, 5, -21,
157 8, -21, 13, -21, 21, -21, -1, -21, -2, -21,
158 -3, -21, -5, -21, -8, -21, -13, -21, -17, -21,
159 0, 0, -8, -29, 8, -29, -18, -25, 17, -25,
160 0, -23, -6, -22, 6, -22, -13, -19, 12, -19,
161 0, -18, 25, -18, -25, -17, -5, -17, 5, -17,
162 -10, -15, 10, -15, 0, -14, -4, -13, 4, -13,
163 19, -13, -19, -12, -8, -11, -2, -11, 0, -11,
164 2, -11, 8, -11, -15, -10, -4, -10, 4, -10,
165 15, -10, -6, -9, -1, -9, 1, -9, 6, -9,
166 -29, -8, -11, -8, -8, -8, -3, -8, 3, -8,
167 8, -8, 11, -8, 29, -8, -5, -7, -2, -7,
168 0, -7, 2, -7, 5, -7, -22, -6, -9, -6,
169 -6, -6, -3, -6, -1, -6, 1, -6, 3, -6,
170 6, -6, 9, -6, 22, -6, -17, -5, -7, -5,
171 -4, -5, -2, -5, 0, -5, 2, -5, 4, -5,
172 7, -5, 17, -5, -13, -4, -10, -4, -5, -4,
173 -3, -4, -1, -4, 0, -4, 1, -4, 3, -4,
174 5, -4, 10, -4, 13, -4, -8, -3, -6, -3,
175 -4, -3, -3, -3, -2, -3, -1, -3, 0, -3,
176 1, -3, 2, -3, 4, -3, 6, -3, 8, -3,
177 -11, -2, -7, -2, -5, -2, -3, -2, -2, -2,
178 -1, -2, 0, -2, 1, -2, 2, -2, 3, -2,
179 5, -2, 7, -2, 11, -2, -9, -1, -6, -1,
180 -4, -1, -3, -1, -2, -1, -1, -1, 0, -1,
181 1, -1, 2, -1, 3, -1, 4, -1, 6, -1,
182 9, -1, -31, 0, -23, 0, -18, 0, -14, 0,
183 -11, 0, -7, 0, -5, 0, -4, 0, -3, 0,
184 -2, 0, -1, 0, 0, -31, 1, 0, 2, 0,
185 3, 0, 4, 0, 5, 0, 7, 0, 11, 0,
186 14, 0, 18, 0, 23, 0, 31, 0, -9, 1,
187 -6, 1, -4, 1, -3, 1, -2, 1, -1, 1,
188 0, 1, 1, 1, 2, 1, 3, 1, 4, 1,
189 6, 1, 9, 1, -11, 2, -7, 2, -5, 2,
190 -3, 2, -2, 2, -1, 2, 0, 2, 1, 2,
191 2, 2, 3, 2, 5, 2, 7, 2, 11, 2,
192 -8, 3, -6, 3, -4, 3, -2, 3, -1, 3,
193 0, 3, 1, 3, 2, 3, 3, 3, 4, 3,
194 6, 3, 8, 3, -13, 4, -10, 4, -5, 4,
195 -3, 4, -1, 4, 0, 4, 1, 4, 3, 4,
196 5, 4, 10, 4, 13, 4, -17, 5, -7, 5,
197 -4, 5, -2, 5, 0, 5, 2, 5, 4, 5,
198 7, 5, 17, 5, -22, 6, -9, 6, -6, 6,
199 -3, 6, -1, 6, 1, 6, 3, 6, 6, 6,
200 9, 6, 22, 6, -5, 7, -2, 7, 0, 7,
201 2, 7, 5, 7, -29, 8, -11, 8, -8, 8,
202 -3, 8, 3, 8, 8, 8, 11, 8, 29, 8,
203 -6, 9, -1, 9, 1, 9, 6, 9, -15, 10,
204 -4, 10, 4, 10, 15, 10, -8, 11, -2, 11,
205 0, 11, 2, 11, 8, 11, 19, 12, -19, 13,
206 -4, 13, 4, 13, 0, 14, -10, 15, 10, 15,
207 -5, 17, 5, 17, 25, 17, -25, 18, 0, 18,
208 -12, 19, 13, 19, -6, 22, 6, 22, 0, 23,
209 -17, 25, 18, 25, -8, 29, 8, 29, 0, 31,
210 0, 0, -6, -22, 6, -22, -13, -19, 12, -19,
211 0, -18, -5, -17, 5, -17, -10, -15, 10, -15,
212 0, -14, -4, -13, 4, -13, 19, -13, -19, -12,
213 -8, -11, -2, -11, 0, -11, 2, -11, 8, -11,
214 -15, -10, -4, -10, 4, -10, 15, -10, -6, -9,
215 -1, -9, 1, -9, 6, -9, -11, -8, -8, -8,
216 -3, -8, 0, -8, 3, -8, 8, -8, 11, -8,
217 -5, -7, -2, -7, 0, -7, 2, -7, 5, -7,
218 -22, -6, -9, -6, -6, -6, -3, -6, -1, -6,
219 1, -6, 3, -6, 6, -6, 9, -6, 22, -6,
220 -17, -5, -7, -5, -4, -5, -2, -5, -1, -5,
221 0, -5, 1, -5, 2, -5, 4, -5, 7, -5,
222 17, -5, -13, -4, -10, -4, -5, -4, -3, -4,
223 -2, -4, -1, -4, 0, -4, 1, -4, 2, -4,
224 3, -4, 5, -4, 10, -4, 13, -4, -8, -3,
225 -6, -3, -4, -3, -3, -3, -2, -3, -1, -3,
226 0, -3, 1, -3, 2, -3, 3, -3, 4, -3,
227 6, -3, 8, -3, -11, -2, -7, -2, -5, -2,
228 -4, -2, -3, -2, -2, -2, -1, -2, 0, -2,
229 1, -2, 2, -2, 3, -2, 4, -2, 5, -2,
230 7, -2, 11, -2, -9, -1, -6, -1, -5, -1,
231 -4, -1, -3, -1, -2, -1, -1, -1, 0, -1,
232 1, -1, 2, -1, 3, -1, 4, -1, 5, -1,
233 6, -1, 9, -1, -23, 0, -18, 0, -14, 0,
234 -11, 0, -7, 0, -5, 0, -4, 0, -3, 0,
235 -2, 0, -1, 0, 0, -23, 1, 0, 2, 0,
236 3, 0, 4, 0, 5, 0, 7, 0, 11, 0,
237 14, 0, 18, 0, 23, 0, -9, 1, -6, 1,
238 -5, 1, -4, 1, -3, 1, -2, 1, -1, 1,
239 0, 1, 1, 1, 2, 1, 3, 1, 4, 1,
240 5, 1, 6, 1, 9, 1, -11, 2, -7, 2,
241 -5, 2, -4, 2, -3, 2, -2, 2, -1, 2,
242 0, 2, 1, 2, 2, 2, 3, 2, 4, 2,
243 5, 2, 7, 2, 11, 2, -8, 3, -6, 3,
244 -4, 3, -3, 3, -2, 3, -1, 3, 0, 3,
245 1, 3, 2, 3, 3, 3, 4, 3, 6, 3,
246 8, 3, -13, 4, -10, 4, -5, 4, -3, 4,
247 -2, 4, -1, 4, 0, 4, 1, 4, 2, 4,
248 3, 4, 5, 4, 10, 4, 13, 4, -17, 5,
249 -7, 5, -4, 5, -2, 5, -1, 5, 0, 5,
250 1, 5, 2, 5, 4, 5, 7, 5, 17, 5,
251 -22, 6, -9, 6, -6, 6, -3, 6, -1, 6,
252 1, 6, 3, 6, 6, 6, 9, 6, 22, 6,
253 -5, 7, -2, 7, 0, 7, 2, 7, 5, 7,
254 -11, 8, -8, 8, -3, 8, 0, 8, 3, 8,
255 8, 8, 11, 8, -6, 9, -1, 9, 1, 9,
256 6, 9, -15, 10, -4, 10, 4, 10, 15, 10,
257 -8, 11, -2, 11, 0, 11, 2, 11, 8, 11,
258 19, 12, -19, 13, -4, 13, 4, 13, 0, 14,
259 -10, 15, 10, 15, -5, 17, 5, 17, 0, 18,
260 -12, 19, 13, 19, -6, 22, 6, 22, 0, 23,
261 };
262
263 typedef struct SANMVideoContext {
264 AVCodecContext *avctx;
265 GetByteContext gb;
266
267 int version, subversion, have_dimensions, first_fob;
268 uint32_t pal[PALETTE_SIZE];
269 int16_t delta_pal[PALETTE_DELTA];
270
271 ptrdiff_t pitch;
272 int width, height;
273 int aligned_width, aligned_height;
274 int prev_seq;
275
276 AVFrame *frame;
277 uint16_t *fbuf, *frm0, *frm1, *frm2;
278 uint8_t *stored_frame;
279 uint32_t fbuf_size, frm0_size, frm1_size, frm2_size;
280 uint32_t stored_frame_size;
281
282 uint8_t *rle_buf;
283 unsigned int rle_buf_size;
284
285 int rotate_code;
286
287 long npixels, buf_size;
288
289 uint16_t codebook[256];
290 uint16_t small_codebook[4];
291
292 int8_t p4x4glyphs[NGLYPHS][16];
293 int8_t p8x8glyphs[NGLYPHS][64];
294 uint8_t c47itbl[0x10000];
295 uint8_t c23lut[256];
296 uint8_t c4tbl[2][256][16];
297 uint16_t c4param;
298 } SANMVideoContext;
299
300 typedef struct SANMFrameHeader {
301 int seq_num, codec, rotate_code, rle_output_size;
302
303 uint16_t bg_color;
304 uint32_t width, height;
305 } SANMFrameHeader;
306
307 enum GlyphEdge {
308 LEFT_EDGE,
309 TOP_EDGE,
310 RIGHT_EDGE,
311 BOTTOM_EDGE,
312 NO_EDGE
313 };
314
315 enum GlyphDir {
316 DIR_LEFT,
317 DIR_UP,
318 DIR_RIGHT,
319 DIR_DOWN,
320 NO_DIR
321 };
322
323 /**
324 * Return enum GlyphEdge of box where point (x, y) lies.
325 *
326 * @param x x point coordinate
327 * @param y y point coordinate
328 * @param edge_size box width/height.
329 */
330 1632 static enum GlyphEdge which_edge(int x, int y, int edge_size)
331 {
332 1632 const int edge_max = edge_size - 1;
333
334
2/2
✓ Branch 0 taken 408 times.
✓ Branch 1 taken 1224 times.
1632 if (!y)
335 408 return BOTTOM_EDGE;
336
2/2
✓ Branch 0 taken 408 times.
✓ Branch 1 taken 816 times.
1224 else if (y == edge_max)
337 408 return TOP_EDGE;
338
2/2
✓ Branch 0 taken 306 times.
✓ Branch 1 taken 510 times.
816 else if (!x)
339 306 return LEFT_EDGE;
340
2/2
✓ Branch 0 taken 306 times.
✓ Branch 1 taken 204 times.
510 else if (x == edge_max)
341 306 return RIGHT_EDGE;
342 else
343 204 return NO_EDGE;
344 }
345
346 1536 static enum GlyphDir which_direction(enum GlyphEdge edge0, enum GlyphEdge edge1)
347 {
348
6/6
✓ Branch 0 taken 288 times.
✓ Branch 1 taken 1248 times.
✓ Branch 2 taken 228 times.
✓ Branch 3 taken 60 times.
✓ Branch 4 taken 288 times.
✓ Branch 5 taken 1188 times.
1536 if ((edge0 == LEFT_EDGE && edge1 == RIGHT_EDGE) ||
349
4/4
✓ Branch 0 taken 228 times.
✓ Branch 1 taken 60 times.
✓ Branch 2 taken 384 times.
✓ Branch 3 taken 1032 times.
1476 (edge1 == LEFT_EDGE && edge0 == RIGHT_EDGE) ||
350
4/4
✓ Branch 0 taken 96 times.
✓ Branch 1 taken 288 times.
✓ Branch 2 taken 288 times.
✓ Branch 3 taken 840 times.
1416 (edge0 == BOTTOM_EDGE && edge1 != TOP_EDGE) ||
351
2/2
✓ Branch 0 taken 192 times.
✓ Branch 1 taken 96 times.
288 (edge1 == BOTTOM_EDGE && edge0 != TOP_EDGE))
352 600 return DIR_UP;
353
6/6
✓ Branch 0 taken 384 times.
✓ Branch 1 taken 552 times.
✓ Branch 2 taken 96 times.
✓ Branch 3 taken 288 times.
✓ Branch 4 taken 288 times.
✓ Branch 5 taken 360 times.
936 else if ((edge0 == TOP_EDGE && edge1 != BOTTOM_EDGE) ||
354
2/2
✓ Branch 0 taken 192 times.
✓ Branch 1 taken 96 times.
288 (edge1 == TOP_EDGE && edge0 != BOTTOM_EDGE))
355 480 return DIR_DOWN;
356
5/6
✓ Branch 0 taken 84 times.
✓ Branch 1 taken 372 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 84 times.
✓ Branch 4 taken 24 times.
✓ Branch 5 taken 348 times.
456 else if ((edge0 == LEFT_EDGE && edge1 != RIGHT_EDGE) ||
357
1/2
✓ Branch 0 taken 24 times.
✗ Branch 1 not taken.
24 (edge1 == LEFT_EDGE && edge0 != RIGHT_EDGE))
358 108 return DIR_LEFT;
359
5/6
✓ Branch 0 taken 96 times.
✓ Branch 1 taken 252 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 96 times.
✓ Branch 4 taken 96 times.
✓ Branch 5 taken 156 times.
348 else if ((edge0 == TOP_EDGE && edge1 == BOTTOM_EDGE) ||
360
3/4
✗ Branch 0 not taken.
✓ Branch 1 taken 96 times.
✓ Branch 2 taken 84 times.
✓ Branch 3 taken 72 times.
252 (edge1 == TOP_EDGE && edge0 == BOTTOM_EDGE) ||
361
3/4
✗ Branch 0 not taken.
✓ Branch 1 taken 84 times.
✓ Branch 2 taken 24 times.
✓ Branch 3 taken 48 times.
156 (edge0 == RIGHT_EDGE && edge1 != LEFT_EDGE) ||
362
1/2
✓ Branch 0 taken 24 times.
✗ Branch 1 not taken.
24 (edge1 == RIGHT_EDGE && edge0 != LEFT_EDGE))
363 300 return DIR_RIGHT;
364
365 48 return NO_DIR;
366 }
367
368 /* Interpolate two points. */
369 6600 static void interp_point(int8_t *points, int x0, int y0, int x1, int y1,
370 int pos, int npoints)
371 {
372
2/2
✓ Branch 0 taken 6504 times.
✓ Branch 1 taken 96 times.
6600 if (npoints) {
373 6504 points[0] = (x0 * pos + x1 * (npoints - pos) + (npoints >> 1)) / npoints;
374 6504 points[1] = (y0 * pos + y1 * (npoints - pos) + (npoints >> 1)) / npoints;
375 } else {
376 96 points[0] = x0;
377 96 points[1] = y0;
378 }
379 6600 }
380
381 /**
382 * Construct glyphs by iterating through vector coordinates.
383 *
384 * @param pglyphs pointer to table where glyphs are stored
385 * @param xvec pointer to x component of vector coordinates
386 * @param yvec pointer to y component of vector coordinates
387 * @param side_length glyph width/height.
388 */
389 6 static void make_glyphs(int8_t *pglyphs, const int8_t *xvec, const int8_t *yvec,
390 const int side_length)
391 {
392 6 const int glyph_size = side_length * side_length;
393 6 int8_t *pglyph = pglyphs;
394
395 int i, j;
396
2/2
✓ Branch 0 taken 96 times.
✓ Branch 1 taken 6 times.
102 for (i = 0; i < GLYPH_COORD_VECT_SIZE; i++) {
397 96 int x0 = xvec[i];
398 96 int y0 = yvec[i];
399 96 enum GlyphEdge edge0 = which_edge(x0, y0, side_length);
400
401
2/2
✓ Branch 0 taken 1536 times.
✓ Branch 1 taken 96 times.
1632 for (j = 0; j < GLYPH_COORD_VECT_SIZE; j++, pglyph += glyph_size) {
402 1536 int x1 = xvec[j];
403 1536 int y1 = yvec[j];
404 1536 enum GlyphEdge edge1 = which_edge(x1, y1, side_length);
405 1536 enum GlyphDir dir = which_direction(edge0, edge1);
406 1536 int npoints = FFMAX(FFABS(x1 - x0), FFABS(y1 - y0));
407 int ipoint;
408
409
2/2
✓ Branch 0 taken 6600 times.
✓ Branch 1 taken 1536 times.
8136 for (ipoint = 0; ipoint <= npoints; ipoint++) {
410 int8_t point[2];
411 int irow, icol;
412
413 6600 interp_point(point, x0, y0, x1, y1, ipoint, npoints);
414
415
5/5
✓ Branch 0 taken 2832 times.
✓ Branch 1 taken 1968 times.
✓ Branch 2 taken 282 times.
✓ Branch 3 taken 1434 times.
✓ Branch 4 taken 84 times.
6600 switch (dir) {
416 2832 case DIR_UP:
417
2/2
✓ Branch 0 taken 8232 times.
✓ Branch 1 taken 2832 times.
11064 for (irow = point[1]; irow >= 0; irow--)
418 8232 pglyph[point[0] + irow * side_length] = 1;
419 2832 break;
420
421 1968 case DIR_DOWN:
422
2/2
✓ Branch 0 taken 4500 times.
✓ Branch 1 taken 1968 times.
6468 for (irow = point[1]; irow < side_length; irow++)
423 4500 pglyph[point[0] + irow * side_length] = 1;
424 1968 break;
425
426 282 case DIR_LEFT:
427
2/2
✓ Branch 0 taken 378 times.
✓ Branch 1 taken 282 times.
660 for (icol = point[0]; icol >= 0; icol--)
428 378 pglyph[icol + point[1] * side_length] = 1;
429 282 break;
430
431 1434 case DIR_RIGHT:
432
2/2
✓ Branch 0 taken 4794 times.
✓ Branch 1 taken 1434 times.
6228 for (icol = point[0]; icol < side_length; icol++)
433 4794 pglyph[icol + point[1] * side_length] = 1;
434 1434 break;
435 }
436 }
437 }
438 }
439 6 }
440
441 6 static void init_sizes(SANMVideoContext *ctx, int width, int height)
442 {
443 6 ctx->width = width;
444 6 ctx->height = height;
445 6 ctx->npixels = width * height;
446
447 6 ctx->aligned_width = FFALIGN(width, 8);
448 6 ctx->aligned_height = FFALIGN(height, 8);
449
450 6 ctx->buf_size = ctx->aligned_width * ctx->aligned_height * sizeof(ctx->frm0[0]);
451 6 ctx->pitch = width;
452 6 }
453
454 3 static void destroy_buffers(SANMVideoContext *ctx)
455 {
456 3 av_freep(&ctx->fbuf);
457 3 av_freep(&ctx->frm0);
458 3 av_freep(&ctx->frm1);
459 3 av_freep(&ctx->frm2);
460 3 av_freep(&ctx->stored_frame);
461 3 av_freep(&ctx->rle_buf);
462 3 ctx->frm0_size =
463 3 ctx->frm1_size =
464 3 ctx->frm2_size = 0;
465 3 init_sizes(ctx, 0, 0);
466 3 }
467
468 3 static av_cold int init_buffers(SANMVideoContext *ctx)
469 {
470 3 av_fast_padded_mallocz(&ctx->fbuf, &ctx->fbuf_size, ctx->buf_size);
471 3 av_fast_padded_mallocz(&ctx->frm0, &ctx->frm0_size, ctx->buf_size);
472 3 av_fast_padded_mallocz(&ctx->frm1, &ctx->frm1_size, ctx->buf_size);
473 3 av_fast_padded_mallocz(&ctx->frm2, &ctx->frm2_size, ctx->buf_size);
474
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (!ctx->version)
475 av_fast_padded_mallocz(&ctx->stored_frame,
476 &ctx->stored_frame_size, ctx->buf_size);
477
478
3/6
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 3 times.
✗ Branch 5 not taken.
3 if (!ctx->frm0 || !ctx->frm1 || !ctx->frm2 ||
479
2/4
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 3 times.
3 (!ctx->stored_frame && !ctx->version)) {
480 destroy_buffers(ctx);
481 return AVERROR(ENOMEM);
482 }
483
484 3 return 0;
485 }
486
487 static void codec33_gen_tiles(SANMVideoContext *ctx, int8_t param1)
488 {
489 uint8_t *dst = &(ctx->c4tbl[0][0][0]);
490 int i, j, k, l, m, n, o, p;
491
492 for (i = 0; i < 8; i++) {
493 for (k = 0; k < 8; k++) {
494 j = i + param1;
495 l = k + param1;
496 p = (j + k) / 2;
497 j = (j + p) / 2;
498 m = l / 2;
499 n = (i + param1);
500 o = (k + param1);
501
502 *dst++ = p; *dst++ = p; *dst++ = j; *dst++ = n;
503 *dst++ = p; *dst++ = p; *dst++ = j; *dst++ = i;
504 *dst++ = m; *dst++ = m; *dst++ = p; *dst++ = j;
505 *dst++ = l; *dst++ = l; *dst++ = m; *dst++ = p;
506 }
507 }
508
509 for (i = 0; i < 8; i++) {
510 for (k = 0; k < 8; k++) {
511 j = i + param1;
512 l = k + param1;
513 n = ((j + l) / 2);
514 m = ((l + n) / 2);
515
516 *dst++ = j; *dst++ = j; *dst++ = j; *dst++ = j;
517 *dst++ = n; *dst++ = n; *dst++ = n; *dst++ = n;
518 *dst++ = m; *dst++ = m; *dst++ = m; *dst++ = m;
519 *dst++ = l; *dst++ = l; *dst++ = l; *dst++ = l;
520 }
521 }
522
523 for (i = 0; i < 8; i++) {
524 for (k = 0; k < 8; k++) {
525 j = i + param1;
526 l = k + param1;
527 m = (j + l) / 2;
528 n = (j + m) / 2;
529 o = m / 2;
530 p = j & 0xff;
531
532 *dst++ = p; *dst++ = p; *dst++ = n; *dst++ = m;
533 *dst++ = p; *dst++ = p; *dst++ = n; *dst++ = m;
534 *dst++ = n; *dst++ = n; *dst++ = m; *dst++ = o;
535 *dst++ = m; *dst++ = m; *dst++ = o; *dst++ = l;
536 }
537 }
538
539 for (i = 0; i < 8; i++) {
540 for (k = 0; k < 8; k++) {
541 j = i + param1;
542 l = k + param1;
543 m = (j + l) / 2;
544 n = m / 2;
545
546 *dst++ = j; *dst++ = m; *dst++ = n; *dst++ = l;
547 *dst++ = j; *dst++ = m; *dst++ = n; *dst++ = l;
548 *dst++ = j; *dst++ = m; *dst++ = n; *dst++ = l;
549 *dst++ = j; *dst++ = m; *dst++ = n; *dst++ = l;
550 }
551 }
552 }
553
554 static void codec4_gen_tiles(SANMVideoContext *ctx, uint16_t param1)
555 {
556 uint8_t *dst = &(ctx->c4tbl[0][0][0]);
557 int i, j, k, l, m, n, o;
558
559 for (i = 1; i < 16; i += 2) {
560 for (k = 0; k < 16; k++) {
561 j = i + param1;
562 l = k + param1;
563 m = (j + l) / 2;
564 n = (j + m) / 2;
565 o = (l + m) / 2;
566 if (j == m || l == m) {
567 *dst++ = l; *dst++ = j; *dst++ = l; *dst++ = j;
568 *dst++ = j; *dst++ = l; *dst++ = j; *dst++ = j;
569 *dst++ = l; *dst++ = j; *dst++ = l; *dst++ = j;
570 *dst++ = l; *dst++ = l; *dst++ = j; *dst++ = l;
571 } else {
572 *dst++ = m; *dst++ = m; *dst++ = n; *dst++ = j;
573 *dst++ = m; *dst++ = m; *dst++ = n; *dst++ = j;
574 *dst++ = o; *dst++ = o; *dst++ = m; *dst++ = n;
575 *dst++ = l; *dst++ = l; *dst++ = o; *dst++ = m;
576 }
577 }
578 }
579
580 for (i = 0; i < 16; i += 2) {
581 for (k = 0; k < 16; k++) {
582 j = i + param1;
583 l = k + param1;
584 m = (j + l) / 2;
585 n = (j + m) / 2;
586 o = (l + m) / 2;
587 if (m == j || m == l) {
588 *dst++ = j; *dst++ = j; *dst++ = l; *dst++ = j;
589 *dst++ = j; *dst++ = j; *dst++ = j; *dst++ = l;
590 *dst++ = l; *dst++ = j; *dst++ = l; *dst++ = l;
591 *dst++ = j; *dst++ = l; *dst++ = j; *dst++ = l;
592 } else {
593 *dst++ = j; *dst++ = j; *dst++ = n; *dst++ = m;
594 *dst++ = j; *dst++ = j; *dst++ = n; *dst++ = m;
595 *dst++ = n; *dst++ = n; *dst++ = m; *dst++ = o;
596 *dst++ = m; *dst++ = m; *dst++ = o; *dst++ = l;
597 }
598 }
599 }
600 }
601
602
603 static int codec4_load_tiles(SANMVideoContext *ctx, GetByteContext *gb,
604 uint16_t param2, uint8_t clr)
605 {
606 uint8_t c, *dst = (uint8_t *)&(ctx->c4tbl[1][0][0]);
607 uint32_t loop = param2 * 8;
608
609 if ((param2 > 256) || (bytestream2_get_bytes_left(gb) < loop))
610 return AVERROR_INVALIDDATA;
611
612 while (loop--) {
613 c = bytestream2_get_byteu(gb);
614 *dst++ = (c >> 4) + clr;
615 *dst++ = (c & 0xf) + clr;
616 }
617
618 return 0;
619 }
620
621 9 static void rotate_bufs(SANMVideoContext *ctx, int rotate_code)
622 {
623
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 8 times.
9 if (rotate_code == 2)
624 1 FFSWAP(uint16_t*, ctx->frm1, ctx->frm2);
625 9 FFSWAP(uint16_t*, ctx->frm2, ctx->frm0);
626 9 }
627
628 3 static av_cold int decode_init(AVCodecContext *avctx)
629 {
630 3 SANMVideoContext *ctx = avctx->priv_data;
631
632 3 ctx->avctx = avctx;
633 3 ctx->version = !avctx->extradata_size;
634 // early sanity check before allocations to avoid need for deallocation code.
635
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
3 if (!ctx->version && avctx->extradata_size < 1026) {
636 av_log(avctx, AV_LOG_ERROR, "Not enough extradata.\n");
637 return AVERROR_INVALIDDATA;
638 }
639
640
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 avctx->pix_fmt = ctx->version ? AV_PIX_FMT_RGB565 : AV_PIX_FMT_PAL8;
641
642 3 init_sizes(ctx, avctx->width, avctx->height);
643
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
3 if (init_buffers(ctx)) {
644 av_log(avctx, AV_LOG_ERROR, "Error allocating buffers.\n");
645 return AVERROR(ENOMEM);
646 }
647
648 3 make_glyphs(ctx->p4x4glyphs[0], glyph4_x, glyph4_y, 4);
649 3 make_glyphs(ctx->p8x8glyphs[0], glyph8_x, glyph8_y, 8);
650
651
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (!ctx->version) {
652 int i;
653
654 ctx->subversion = AV_RL16(avctx->extradata);
655 for (i = 0; i < PALETTE_SIZE; i++)
656 ctx->pal[i] = 0xFFU << 24 | AV_RL32(avctx->extradata + 2 + i * 4);
657 if (ctx->subversion < 2)
658 ctx->pal[0] = 0xFFU << 24;
659 }
660 3 ctx->c4param = 0xffff;
661
662 3 return 0;
663 }
664
665 3 static av_cold int decode_end(AVCodecContext *avctx)
666 {
667 3 SANMVideoContext *ctx = avctx->priv_data;
668
669 3 destroy_buffers(ctx);
670
671 3 return 0;
672 }
673
674 static int old_codec4(SANMVideoContext *ctx, GetByteContext *gb, int top, int left,
675 int w, int h, uint8_t param, uint16_t param2, int codec)
676 {
677 const uint16_t p = ctx->pitch;
678 const uint32_t maxpxo = ctx->height * p;
679 uint8_t mask, bits, idx, *gs, *dst = (uint8_t *)ctx->fbuf;
680 int i, j, k, l, bit, ret;
681 int32_t pxoff, pxo2;
682
683 if (ctx->c4param != param) {
684 if (codec > 32)
685 codec33_gen_tiles(ctx, param);
686 else
687 codec4_gen_tiles(ctx, param);
688 ctx->c4param = param;
689 }
690 if (param2 > 0) {
691 ret = codec4_load_tiles(ctx, gb, param2, param);
692 if (ret)
693 return ret;
694 }
695
696 if (codec > 32)
697 codec -= 29;
698
699 for (j = 0; j < w; j += 4) {
700 mask = bits = 0;
701 for (i = 0; i < h; i += 4) {
702 pxoff = j + left + ((top + i) * p);
703 if (param2 > 0) {
704 if (bits == 0) {
705 if (bytestream2_get_bytes_left(gb) < 1)
706 return AVERROR_INVALIDDATA;
707 mask = bytestream2_get_byteu(gb);
708 bits = 8;
709 }
710 bit = !!(mask & 0x80);
711 mask <<= 1;
712 bits--;
713 } else {
714 bit = 0;
715 }
716
717 if (bytestream2_get_bytes_left(gb) < 1)
718 return AVERROR_INVALIDDATA;
719 idx = bytestream2_get_byteu(gb);
720 if ((bit == 0) && (idx == 0x80) && (codec != 5))
721 continue;
722
723 gs = &(ctx->c4tbl[bit][idx][0]);
724 pxo2 = pxoff;
725 for (k = 0; k < 4; k++) {
726 for (l = 0; l < 4; l++) {
727 if (pxo2 >= 0 && pxo2 < maxpxo) {
728 *(dst + pxo2) = *gs;
729 }
730 gs++;
731 pxo2++;
732 }
733 pxo2 = pxo2 - 4 + p;
734 }
735
736 /* smooth top and left block borders with neighbours */
737 if (((pxoff - p + k) < 0) || ((pxoff - p + k) >= maxpxo)
738 || ((pxoff + 3 * p) < 0) || ((pxoff + 3 * p) >= maxpxo)
739 || (i == 0) || (j == 0))
740 continue;
741 if (param & 0x80) {
742 for (k = 0; k < 4; k++)
743 *(dst + pxoff + k) = ((*(dst + pxoff + k) + *(dst + pxoff - p + k)) >> 1) | 0x80;
744 *(dst + pxoff + 1 * p) = (*(dst + pxoff + 1 * p) + *(dst + pxoff + 1 * p - 1)) >> 1 | 0x80;
745 *(dst + pxoff + 2 * p) = (*(dst + pxoff + 2 * p) + *(dst + pxoff + 2 * p - 1)) >> 1 | 0x80;
746 *(dst + pxoff + 3 * p) = (*(dst + pxoff + 3 * p) + *(dst + pxoff + 3 * p - 1)) >> 1 | 0x80;
747 } else {
748 for (k = 0; k < 4; k++)
749 *(dst + pxoff + k) = ((*(dst + pxoff + k) + *(dst + pxoff - p + k)) >> 1) & 0x7f;
750 *(dst + pxoff + 1 * p) = (*(dst + pxoff + 1 * p) + *(dst + pxoff + 1 * p - 1)) >> 1;
751 *(dst + pxoff + 2 * p) = (*(dst + pxoff + 2 * p) + *(dst + pxoff + 2 * p - 1)) >> 1;
752 *(dst + pxoff + 3 * p) = (*(dst + pxoff + 3 * p) + *(dst + pxoff + 3 * p - 1)) >> 1;
753 }
754 }
755 }
756 return 0;
757 }
758
759 1 static int rle_decode(SANMVideoContext *ctx, GetByteContext *gb, uint8_t *dst, const int out_size)
760 {
761 1 int opcode, color, run_len, left = out_size;
762
763
2/2
✓ Branch 0 taken 4800 times.
✓ Branch 1 taken 1 times.
4801 while (left > 0) {
764 4800 opcode = bytestream2_get_byte(gb);
765 4800 run_len = (opcode >> 1) + 1;
766
2/4
✓ Branch 0 taken 4800 times.
✗ Branch 1 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 4800 times.
4800 if (run_len > left || bytestream2_get_bytes_left(gb) <= 0)
767 return AVERROR_INVALIDDATA;
768
769
1/2
✓ Branch 0 taken 4800 times.
✗ Branch 1 not taken.
4800 if (opcode & 1) {
770 4800 color = bytestream2_get_byte(gb);
771 4800 memset(dst, color, run_len);
772 } else {
773 if (bytestream2_get_bytes_left(gb) < run_len)
774 return AVERROR_INVALIDDATA;
775 bytestream2_get_bufferu(gb, dst, run_len);
776 }
777
778 4800 dst += run_len;
779 4800 left -= run_len;
780 }
781
782 1 return 0;
783 }
784
785 static int old_codec23(SANMVideoContext *ctx, GetByteContext *gb, int top, int left,
786 int width, int height, uint8_t param, uint16_t param2)
787 {
788 const uint32_t maxpxo = ctx->height * ctx->pitch;
789 uint8_t *dst, lut[256], c;
790 int i, j, k, pc, sk;
791 int32_t pxoff;
792
793 if (ctx->subversion < 2) {
794 /* Rebel Assault 1: constant offset + 0xd0 */
795 for (i = 0; i < 256; i++)
796 lut[i] = (i + param + 0xd0) & 0xff;
797 } else if (param2 == 256) {
798 if (bytestream2_get_bytes_left(gb) < 256)
799 return AVERROR_INVALIDDATA;
800 bytestream2_get_bufferu(gb, ctx->c23lut, 256);
801 } else if (param2 < 256) {
802 for (i = 0; i < 256; i++)
803 lut[i] = (i + param2) & 0xff;
804 } else {
805 memcpy(lut, ctx->c23lut, 256);
806 }
807 if (bytestream2_get_bytes_left(gb) < 1)
808 return 0; /* some c23 frames just set up the LUT */
809
810 dst = (uint8_t *)ctx->fbuf;
811 for (i = 0; i < height; i++) {
812 if (bytestream2_get_bytes_left(gb) < 2)
813 return 0;
814 pxoff = left + ((top + i) * ctx->pitch);
815 k = bytestream2_get_le16u(gb);
816 sk = 1;
817 pc = 0;
818 while (k > 0 && pc <= width) {
819 if (bytestream2_get_bytes_left(gb) < 1)
820 return AVERROR_INVALIDDATA;
821 j = bytestream2_get_byteu(gb);
822 if (sk) {
823 pxoff += j;
824 pc += j;
825 } else {
826 while (j--) {
827 if (pxoff >=0 && pxoff < maxpxo) {
828 c = *(dst + pxoff);
829 *(dst + pxoff) = lut[c];
830 }
831 pxoff++;
832 pc++;
833 }
834 }
835 sk ^= 1;
836 }
837 }
838 return 0;
839 }
840
841 static int old_codec21(SANMVideoContext *ctx, GetByteContext *gb, int top, int left,
842 int width, int height)
843 {
844 const uint32_t maxpxo = ctx->height * ctx->pitch;
845 uint8_t *dst = (uint8_t *)ctx->fbuf, c;
846 int i, j, k, pc, sk, pxoff;
847
848 dst = (uint8_t *)ctx->fbuf;
849 for (i = 0; i < height; i++) {
850 if (bytestream2_get_bytes_left(gb) < 2)
851 return 0;
852 pxoff = left + ((top + i) * ctx->pitch);
853 k = bytestream2_get_le16u(gb);
854 sk = 1;
855 pc = 0;
856 while (k > 0 && pc <= width) {
857 if (bytestream2_get_bytes_left(gb) < 2)
858 return AVERROR_INVALIDDATA;
859 j = bytestream2_get_le16u(gb);
860 k -= 2;
861 if (sk) {
862 pxoff += j;
863 pc += j;
864 } else {
865 if (bytestream2_get_bytes_left(gb) < (j + 1))
866 return AVERROR_INVALIDDATA;
867 do {
868 c = bytestream2_get_byteu(gb);
869 if (pxoff >=0 && pxoff < maxpxo) {
870 *(dst + pxoff) = c;
871 }
872 pxoff++;
873 pc++;
874 j--;
875 k--;
876 } while (j > -1);
877 }
878 sk ^= 1;
879 }
880 }
881 return 0;
882 }
883
884 static int old_codec1(SANMVideoContext *ctx, GetByteContext *gb, int top,
885 int left, int width, int height, int opaque)
886 {
887 int i, j, len, flag, code, val, end, pxoff;
888 const int maxpxo = ctx->height * ctx->pitch;
889 uint8_t *dst = (uint8_t *)ctx->fbuf;
890
891 for (i = 0; i < height; i++) {
892 if (bytestream2_get_bytes_left(gb) < 2)
893 return AVERROR_INVALIDDATA;
894
895 len = bytestream2_get_le16u(gb);
896 end = bytestream2_tell(gb) + len;
897
898 pxoff = left + ((top + i) * ctx->pitch);
899 while (bytestream2_tell(gb) < end) {
900 if (bytestream2_get_bytes_left(gb) < 2)
901 return AVERROR_INVALIDDATA;
902
903 code = bytestream2_get_byteu(gb);
904 flag = code & 1;
905 code = (code >> 1) + 1;
906 if (flag) {
907 val = bytestream2_get_byteu(gb);
908 if (val || opaque) {
909 for (j = 0; j < code; j++) {
910 if (pxoff >= 0 && pxoff < maxpxo)
911 *(dst + pxoff) = val;
912 pxoff++;
913 }
914 } else {
915 pxoff += code;
916 }
917 } else {
918 if (bytestream2_get_bytes_left(gb) < code)
919 return AVERROR_INVALIDDATA;
920 for (j = 0; j < code; j++) {
921 val = bytestream2_get_byteu(gb);
922 if ((pxoff >= 0) && (pxoff < maxpxo) && (val || opaque))
923 *(dst + pxoff) = val;
924 pxoff++;
925 }
926 }
927 }
928 }
929 ctx->rotate_code = 0;
930
931 return 0;
932 }
933
934 static int old_codec2(SANMVideoContext *ctx, GetByteContext *gb, int top,
935 int left, int width, int height)
936 {
937 uint8_t *dst = (uint8_t *)ctx->fbuf, col;
938 int16_t xpos = left, ypos = top;
939
940 while (bytestream2_get_bytes_left(gb) > 3) {
941 xpos += bytestream2_get_le16u(gb);
942 ypos += bytestream2_get_byteu(gb);
943 col = bytestream2_get_byteu(gb);
944 if (xpos >= 0 && ypos >= 0 &&
945 xpos < ctx->width && ypos < ctx->height) {
946 *(dst + xpos + ypos * ctx->pitch) = col;
947 }
948 }
949 return 0;
950 }
951
952 static int old_codec20(SANMVideoContext *ctx, int w, int h)
953 {
954 uint8_t *dst = (uint8_t *)ctx->fbuf;
955
956 if (bytestream2_get_bytes_left(&ctx->gb) < w * h)
957 return AVERROR_INVALIDDATA;
958
959 if (w == ctx->pitch) {
960 bytestream2_get_bufferu(&ctx->gb, dst, w * h);
961 } else {
962 for (int i = 0; i < h; i++) {
963 bytestream2_get_bufferu(&ctx->gb, dst, w);
964 dst += ctx->pitch;
965 }
966 }
967 return 0;
968 }
969
970 static inline void codec37_mv(uint8_t *dst, const uint8_t *src,
971 int height, int stride, int x, int y)
972 {
973 int pos, i, j;
974
975 pos = x + y * stride;
976 for (j = 0; j < 4; j++) {
977 for (i = 0; i < 4; i++) {
978 if ((pos + i) < 0 || (pos + i) >= height * stride)
979 dst[i] = 0;
980 else
981 dst[i] = src[i];
982 }
983 dst += stride;
984 src += stride;
985 pos += stride;
986 }
987 }
988
989 static int old_codec37(SANMVideoContext *ctx, int width, int height)
990 {
991 int i, j, k, l, t, run, len, code, skip, mx, my;
992 ptrdiff_t stride = ctx->pitch;
993 uint8_t *dst, *prev;
994 int skip_run = 0;
995 int compr = bytestream2_get_byte(&ctx->gb);
996 int mvoff = bytestream2_get_byte(&ctx->gb);
997 int seq = bytestream2_get_le16(&ctx->gb);
998 uint32_t decoded_size = bytestream2_get_le32(&ctx->gb);
999 int flags;
1000
1001 bytestream2_skip(&ctx->gb, 4);
1002 flags = bytestream2_get_byte(&ctx->gb);
1003 bytestream2_skip(&ctx->gb, 3);
1004
1005 if (decoded_size > ctx->height * stride) {
1006 decoded_size = ctx->height * stride;
1007 av_log(ctx->avctx, AV_LOG_WARNING, "Decoded size is too large.\n");
1008 }
1009
1010 ctx->rotate_code = 0;
1011
1012 if (((seq & 1) || !(flags & 1)) && (compr && compr != 2)) {
1013 FFSWAP(uint16_t*, ctx->frm0, ctx->frm2);
1014 }
1015
1016 dst = ((uint8_t*)ctx->frm0);
1017 prev = ((uint8_t*)ctx->frm2);
1018
1019 if (mvoff > 2) {
1020 av_log(ctx->avctx, AV_LOG_ERROR, "Invalid motion base value %d.\n", mvoff);
1021 return AVERROR_INVALIDDATA;
1022 }
1023
1024 switch (compr) {
1025 case 0:
1026 for (i = 0; i < height; i++) {
1027 bytestream2_get_buffer(&ctx->gb, dst, width);
1028 dst += stride;
1029 }
1030 memset(ctx->frm2, 0, ctx->height * stride);
1031 break;
1032 case 1:
1033 run = 0;
1034 len = -1;
1035 code = 0;
1036
1037 for (j = 0; j < height; j += 4) {
1038 for (i = 0; i < width; i += 4) {
1039 if (len < 0) {
1040 if (bytestream2_get_bytes_left(&ctx->gb) < 1)
1041 return AVERROR_INVALIDDATA;
1042 code = bytestream2_get_byte(&ctx->gb);
1043 len = code >> 1;
1044 run = code & 1;
1045 skip = 0;
1046 } else {
1047 skip = run;
1048 }
1049
1050 if (!skip) {
1051 if (bytestream2_get_bytes_left(&ctx->gb) < 1)
1052 return AVERROR_INVALIDDATA;
1053 code = bytestream2_get_byte(&ctx->gb);
1054 if (code == 0xff) {
1055 len--;
1056 for (k = 0; k < 4; k++) {
1057 for (l = 0; l < 4; l++) {
1058 if (len < 0) {
1059 if (bytestream2_get_bytes_left(&ctx->gb) < 1)
1060 return AVERROR_INVALIDDATA;
1061 code = bytestream2_get_byte(&ctx->gb);
1062 len = code >> 1;
1063 run = code & 1;
1064 if (run) {
1065 if (bytestream2_get_bytes_left(&ctx->gb) < 1)
1066 return AVERROR_INVALIDDATA;
1067 code = bytestream2_get_byte(&ctx->gb);
1068 }
1069 }
1070 if (!run) {
1071 if (bytestream2_get_bytes_left(&ctx->gb) < 1)
1072 return AVERROR_INVALIDDATA;
1073 code = bytestream2_get_byte(&ctx->gb);
1074 }
1075 *(dst + i + (k * stride) + l) = code;
1076 len--;
1077 }
1078 }
1079 continue;
1080 }
1081 }
1082 /* 4x4 block copy from prev with MV */
1083 mx = c37_mv[(mvoff * 255 + code) * 2];
1084 my = c37_mv[(mvoff * 255 + code) * 2 + 1];
1085 codec37_mv(dst + i, prev + i + mx + my * stride,
1086 ctx->height, stride, i + mx, j + my);
1087 len--;
1088 }
1089 dst += stride * 4;
1090 prev += stride * 4;
1091 }
1092 break;
1093 case 2:
1094 if (rle_decode(ctx, &ctx->gb, dst, decoded_size))
1095 return AVERROR_INVALIDDATA;
1096 memset(ctx->frm2, 0, ctx->frm2_size);
1097 break;
1098 case 3:
1099 case 4:
1100 for (j = 0; j < height; j += 4) {
1101 for (i = 0; i < width; i += 4) {
1102 int code;
1103 if (skip_run) {
1104 skip_run--;
1105 copy_block4(dst + i, prev + i, stride, stride, 4);
1106 continue;
1107 }
1108 if (bytestream2_get_bytes_left(&ctx->gb) < 1)
1109 return AVERROR_INVALIDDATA;
1110 code = bytestream2_get_byteu(&ctx->gb);
1111 if (code == 0xFF) {
1112 if (bytestream2_get_bytes_left(&ctx->gb) < 16)
1113 return AVERROR_INVALIDDATA;
1114 for (k = 0; k < 4; k++)
1115 bytestream2_get_bufferu(&ctx->gb, dst + i + k * stride, 4);
1116 } else if ((flags & 4) && (code == 0xFE)) {
1117 if (bytestream2_get_bytes_left(&ctx->gb) < 4)
1118 return AVERROR_INVALIDDATA;
1119 for (k = 0; k < 4; k++)
1120 memset(dst + i + k * stride, bytestream2_get_byteu(&ctx->gb), 4);
1121 } else if ((flags & 4) && (code == 0xFD)) {
1122 if (bytestream2_get_bytes_left(&ctx->gb) < 1)
1123 return AVERROR_INVALIDDATA;
1124 t = bytestream2_get_byteu(&ctx->gb);
1125 for (k = 0; k < 4; k++)
1126 memset(dst + i + k * stride, t, 4);
1127 } else {
1128 mx = c37_mv[(mvoff * 255 + code) * 2];
1129 my = c37_mv[(mvoff * 255 + code) * 2 + 1];
1130 codec37_mv(dst + i, prev + i + mx + my * stride,
1131 ctx->height, stride, i + mx, j + my);
1132
1133 if ((compr == 4) && (code == 0)) {
1134 if (bytestream2_get_bytes_left(&ctx->gb) < 1)
1135 return AVERROR_INVALIDDATA;
1136 skip_run = bytestream2_get_byteu(&ctx->gb);
1137 }
1138 }
1139 }
1140 dst += stride * 4;
1141 prev += stride * 4;
1142 }
1143 break;
1144 default:
1145 avpriv_report_missing_feature(ctx->avctx,
1146 "Subcodec 37 compression %d", compr);
1147 return AVERROR_PATCHWELCOME;
1148 }
1149
1150 return 0;
1151 }
1152
1153 static int process_block(SANMVideoContext *ctx, uint8_t *dst, uint8_t *prev1,
1154 uint8_t *prev2, int stride, int tbl, int size)
1155 {
1156 int code, k, t;
1157 uint8_t colors[2];
1158 int8_t *pglyph;
1159
1160 if (bytestream2_get_bytes_left(&ctx->gb) < 1)
1161 return AVERROR_INVALIDDATA;
1162
1163 code = bytestream2_get_byteu(&ctx->gb);
1164 if (code >= 0xF8) {
1165 switch (code) {
1166 case 0xFF:
1167 if (size == 2) {
1168 if (bytestream2_get_bytes_left(&ctx->gb) < 4)
1169 return AVERROR_INVALIDDATA;
1170 dst[0] = bytestream2_get_byteu(&ctx->gb);
1171 dst[1] = bytestream2_get_byteu(&ctx->gb);
1172 dst[0 + stride] = bytestream2_get_byteu(&ctx->gb);
1173 dst[1 + stride] = bytestream2_get_byteu(&ctx->gb);
1174 } else {
1175 size >>= 1;
1176 if (process_block(ctx, dst, prev1, prev2, stride, tbl, size))
1177 return AVERROR_INVALIDDATA;
1178 if (process_block(ctx, dst + size, prev1 + size, prev2 + size,
1179 stride, tbl, size))
1180 return AVERROR_INVALIDDATA;
1181 dst += size * stride;
1182 prev1 += size * stride;
1183 prev2 += size * stride;
1184 if (process_block(ctx, dst, prev1, prev2, stride, tbl, size))
1185 return AVERROR_INVALIDDATA;
1186 if (process_block(ctx, dst + size, prev1 + size, prev2 + size,
1187 stride, tbl, size))
1188 return AVERROR_INVALIDDATA;
1189 }
1190 break;
1191 case 0xFE:
1192 if (bytestream2_get_bytes_left(&ctx->gb) < 1)
1193 return AVERROR_INVALIDDATA;
1194
1195 t = bytestream2_get_byteu(&ctx->gb);
1196 for (k = 0; k < size; k++)
1197 memset(dst + k * stride, t, size);
1198 break;
1199 case 0xFD:
1200 if (bytestream2_get_bytes_left(&ctx->gb) < 3)
1201 return AVERROR_INVALIDDATA;
1202
1203 code = bytestream2_get_byteu(&ctx->gb);
1204 pglyph = (size == 8) ? ctx->p8x8glyphs[code] : ctx->p4x4glyphs[code];
1205 bytestream2_get_bufferu(&ctx->gb, colors, 2);
1206
1207 for (k = 0; k < size; k++)
1208 for (t = 0; t < size; t++)
1209 dst[t + k * stride] = colors[!*pglyph++];
1210 break;
1211 case 0xFC:
1212 for (k = 0; k < size; k++)
1213 memcpy(dst + k * stride, prev1 + k * stride, size);
1214 break;
1215 default:
1216 k = bytestream2_tell(&ctx->gb);
1217 bytestream2_seek(&ctx->gb, tbl + (code & 7), SEEK_SET);
1218 t = bytestream2_get_byte(&ctx->gb);
1219 bytestream2_seek(&ctx->gb, k, SEEK_SET);
1220 for (k = 0; k < size; k++)
1221 memset(dst + k * stride, t, size);
1222 }
1223 } else {
1224 int mx = motion_vectors[code][0];
1225 int my = motion_vectors[code][1];
1226 int index = prev2 - (const uint8_t *)ctx->frm2;
1227
1228 av_assert2(index >= 0 && index < (ctx->buf_size >> 1));
1229
1230 if (index < -mx - my * stride ||
1231 (ctx->buf_size >> 1) - index < mx + size + (my + size - 1) * stride) {
1232 av_log(ctx->avctx, AV_LOG_ERROR, "MV is invalid.\n");
1233 return AVERROR_INVALIDDATA;
1234 }
1235
1236 for (k = 0; k < size; k++)
1237 memcpy(dst + k * stride, prev2 + mx + (my + k) * stride, size);
1238 }
1239
1240 return 0;
1241 }
1242
1243 static void codec47_read_interptable(SANMVideoContext *ctx)
1244 {
1245 uint8_t *p1, *p2, *itbl = ctx->c47itbl;
1246 int i, j;
1247
1248 for (i = 0; i < 256; i++) {
1249 p1 = p2 = itbl + i;
1250 for (j = 256 - i; j; j--) {
1251 *p1 = *p2 = bytestream2_get_byte(&ctx->gb);
1252 p1 += 1;
1253 p2 += 256;
1254 }
1255 itbl += 256;
1256 }
1257 }
1258
1259 static void codec47_comp1(SANMVideoContext *ctx, uint8_t *dst_in, int width,
1260 int height, ptrdiff_t stride)
1261 {
1262 uint8_t p1, *dst, *itbl = ctx->c47itbl;
1263 uint16_t px;
1264 int i, j;
1265
1266 dst = dst_in + stride;
1267 for (i = 0; i < height; i += 2) {
1268 p1 = bytestream2_get_byte(&ctx->gb);
1269 *dst++ = p1;
1270 *dst++ = p1;
1271 px = p1;
1272 for (j = 2; j < width; j += 2) {
1273 p1 = bytestream2_get_byte(&ctx->gb);
1274 px = (px << 8) | p1;
1275 *dst++ = itbl[px];
1276 *dst++ = p1;
1277 }
1278 dst += stride;
1279 }
1280
1281 memcpy(dst_in, dst_in + stride, width);
1282 dst = dst_in + stride + stride;
1283 for (i = 2; i < height - 1; i += 2) {
1284 for (j = 0; j < width; j++) {
1285 px = (*(dst - stride) << 8) | *(dst + stride);
1286 *dst++ = itbl[px];
1287 }
1288 dst += stride;
1289 }
1290 }
1291
1292 static int old_codec47(SANMVideoContext *ctx, int width, int height)
1293 {
1294 uint32_t decoded_size;
1295 int i, j;
1296 ptrdiff_t stride = ctx->pitch;
1297 uint8_t *dst = (uint8_t *)ctx->frm0;
1298 uint8_t *prev1 = (uint8_t *)ctx->frm1;
1299 uint8_t *prev2 = (uint8_t *)ctx->frm2;
1300 uint8_t auxcol[2];
1301 int tbl_pos = bytestream2_tell(&ctx->gb);
1302 int seq = bytestream2_get_le16(&ctx->gb);
1303 int compr = bytestream2_get_byte(&ctx->gb);
1304 int new_rot = bytestream2_get_byte(&ctx->gb);
1305 int skip = bytestream2_get_byte(&ctx->gb);
1306
1307 bytestream2_skip(&ctx->gb, 7);
1308 auxcol[0] = bytestream2_get_byteu(&ctx->gb);
1309 auxcol[1] = bytestream2_get_byteu(&ctx->gb);
1310 decoded_size = bytestream2_get_le32(&ctx->gb);
1311 bytestream2_skip(&ctx->gb, 8);
1312
1313 if (decoded_size > ctx->height * stride) {
1314 decoded_size = ctx->height * stride;
1315 av_log(ctx->avctx, AV_LOG_WARNING, "Decoded size is too large.\n");
1316 }
1317
1318 if (skip & 1) {
1319 if (bytestream2_get_bytes_left(&ctx->gb) < 0x8080)
1320 return AVERROR_INVALIDDATA;
1321 codec47_read_interptable(ctx);
1322 }
1323 if (!seq) {
1324 ctx->prev_seq = -1;
1325 memset(prev1, auxcol[0], ctx->height * stride);
1326 memset(prev2, auxcol[1], ctx->height * stride);
1327 }
1328
1329 switch (compr) {
1330 case 0:
1331 if (bytestream2_get_bytes_left(&ctx->gb) < width * height)
1332 return AVERROR_INVALIDDATA;
1333 for (j = 0; j < height; j++) {
1334 bytestream2_get_bufferu(&ctx->gb, dst, width);
1335 dst += stride;
1336 }
1337 break;
1338 case 1:
1339 if (bytestream2_get_bytes_left(&ctx->gb) < ((width + 1) >> 1) * ((height + 1) >> 1))
1340 return AVERROR_INVALIDDATA;
1341 codec47_comp1(ctx, dst, width, height, stride);
1342 break;
1343 case 2:
1344 if (seq == ctx->prev_seq + 1) {
1345 for (j = 0; j < height; j += 8) {
1346 for (i = 0; i < width; i += 8)
1347 if (process_block(ctx, dst + i, prev1 + i, prev2 + i, stride,
1348 tbl_pos + 8, 8))
1349 return AVERROR_INVALIDDATA;
1350 dst += stride * 8;
1351 prev1 += stride * 8;
1352 prev2 += stride * 8;
1353 }
1354 }
1355 break;
1356 case 3:
1357 memcpy(ctx->frm0, ctx->frm2, ctx->pitch * ctx->height);
1358 break;
1359 case 4:
1360 memcpy(ctx->frm0, ctx->frm1, ctx->pitch * ctx->height);
1361 break;
1362 case 5:
1363 if (rle_decode(ctx, &ctx->gb, dst, decoded_size))
1364 return AVERROR_INVALIDDATA;
1365 break;
1366 default:
1367 avpriv_report_missing_feature(ctx->avctx,
1368 "Subcodec 47 compression %d", compr);
1369 return AVERROR_PATCHWELCOME;
1370 }
1371 if (seq == ctx->prev_seq + 1)
1372 ctx->rotate_code = new_rot;
1373 else
1374 ctx->rotate_code = 0;
1375 ctx->prev_seq = seq;
1376
1377 return 0;
1378 }
1379
1380 // scale 4x4 input block to an 8x8 output block
1381 static void c48_4to8(uint8_t *dst, const uint8_t *src, const uint16_t w)
1382 {
1383 uint16_t p;
1384 // dst is always at least 16bit aligned
1385 for (int i = 0; i < 4; i++) {
1386 for (int j = 0; j < 8; j += 2) {
1387 p = *src++;
1388 p = (p << 8) | p;
1389 *((uint16_t *)(dst + w * 0 + j)) = p;
1390 *((uint16_t *)(dst + w * 1 + j)) = p;
1391 }
1392 dst += w * 2;
1393 }
1394 }
1395
1396 static int codec48_block(SANMVideoContext *ctx, uint8_t *dst, uint8_t *db,
1397 const uint16_t w)
1398 {
1399 uint8_t opc, sb[16];
1400 int i, j, k, l;
1401 int16_t mvofs;
1402 uint32_t ofs;
1403
1404 if (bytestream2_get_bytes_left(&ctx->gb) < 1)
1405 return 1;
1406
1407 opc = bytestream2_get_byteu(&ctx->gb);
1408 switch (opc) {
1409 case 0xFF: // 1x1 -> 8x8 block scale
1410 if (bytestream2_get_bytes_left(&ctx->gb) < 1)
1411 return 1;
1412
1413 opc = bytestream2_get_byteu(&ctx->gb);
1414 for (i = 0; i < 16; i++)
1415 sb[i] = opc;
1416 c48_4to8(dst, sb, w);
1417 break;
1418 case 0xFE: // 1x 8x8 copy from deltabuf, 16bit mv from source
1419 if (bytestream2_get_bytes_left(&ctx->gb) < 2)
1420 return 1;
1421 mvofs = bytestream2_get_le16(&ctx->gb);
1422 for (i = 0; i < 8; i++) {
1423 ofs = w * i;
1424 for (k = 0; k < 8; k++)
1425 *(dst + ofs + k) = *(db + ofs + k + mvofs);
1426 }
1427 break;
1428 case 0xFD: // 2x2 -> 8x8 block scale
1429 if (bytestream2_get_bytes_left(&ctx->gb) < 4)
1430 return 1;
1431 sb[ 5] = bytestream2_get_byteu(&ctx->gb);
1432 sb[ 7] = bytestream2_get_byteu(&ctx->gb);
1433 sb[13] = bytestream2_get_byteu(&ctx->gb);
1434 sb[15] = bytestream2_get_byteu(&ctx->gb);
1435
1436 sb[0] = sb[1] = sb[4] = sb[5];
1437 sb[2] = sb[3] = sb[6] = sb[7];
1438 sb[8] = sb[9] = sb[12] = sb[13];
1439 sb[10] = sb[11] = sb[14] = sb[15];
1440 c48_4to8(dst, sb, w);
1441 break;
1442 case 0xFC: // 4x copy 4x4 block, per-block c37_mv from source
1443 if (bytestream2_get_bytes_left(&ctx->gb) < 4)
1444 return 1;
1445 for (i = 0; i < 8; i += 4) {
1446 for (k = 0; k < 8; k += 4) {
1447 opc = bytestream2_get_byteu(&ctx->gb);
1448 mvofs = c37_mv[opc * 2] + (c37_mv[opc * 2 + 1] * w);
1449 for (j = 0; j < 4; j++) {
1450 ofs = (w * (j + i)) + k;
1451 for (l = 0; l < 4; l++)
1452 *(dst + ofs + l) = *(db + ofs + l + mvofs);
1453 }
1454 }
1455 }
1456 break;
1457 case 0xFB: // Copy 4x 4x4 blocks, per-block mv from source
1458 if (bytestream2_get_bytes_left(&ctx->gb) < 8)
1459 return 1;
1460 for (i = 0; i < 8; i += 4) {
1461 for (k = 0; k < 8; k += 4) {
1462 mvofs = bytestream2_get_le16(&ctx->gb);
1463 for (j = 0; j < 4; j++) {
1464 ofs = (w * (j + i)) + k;
1465 for (l = 0; l < 4; l++)
1466 *(dst + ofs + l) = *(db + ofs + l + mvofs);
1467 }
1468 }
1469 }
1470 break;
1471 case 0xFA: // scale 4x4 input block to 8x8 dest block
1472 if (bytestream2_get_bytes_left(&ctx->gb) < 16)
1473 return 1;
1474 bytestream2_get_bufferu(&ctx->gb, sb, 16);
1475 c48_4to8(dst, sb, w);
1476 break;
1477 case 0xF9: // 16x 2x2 copy from delta, per-block c37_mv from source
1478 if (bytestream2_get_bytes_left(&ctx->gb) < 16)
1479 return 1;
1480 for (i = 0; i < 8; i += 2) {
1481 for (j = 0; j < 8; j += 2) {
1482 ofs = (w * i) + j;
1483 opc = bytestream2_get_byteu(&ctx->gb);
1484 mvofs = c37_mv[opc * 2] + (c37_mv[opc * 2 + 1] * w);
1485 for (l = 0; l < 2; l++) {
1486 *(dst + ofs + l + 0) = *(db + ofs + l + 0 + mvofs);
1487 *(dst + ofs + l + w) = *(db + ofs + l + w + mvofs);
1488 }
1489 }
1490 }
1491 break;
1492 case 0xF8: // 16x 2x2 blocks copy, 16bit mv from source
1493 if (bytestream2_get_bytes_left(&ctx->gb) < 32)
1494 return 1;
1495 for (i = 0; i < 8; i += 2) {
1496 for (j = 0; j < 8; j += 2) {
1497 ofs = w * i + j;
1498 mvofs = bytestream2_get_le16(&ctx->gb);
1499 for (l = 0; l < 2; l++) {
1500 *(dst + ofs + l + 0) = *(db + ofs + l + 0 + mvofs);
1501 *(dst + ofs + l + w) = *(db + ofs + l + w + mvofs);
1502 }
1503 }
1504 }
1505 break;
1506 case 0xF7: // copy 8x8 block from src to dest
1507 if (bytestream2_get_bytes_left(&ctx->gb) < 64)
1508 return 1;
1509 for (i = 0; i < 8; i++) {
1510 ofs = i * w;
1511 for (l = 0; l < 8; l++)
1512 *(dst + ofs + l) = bytestream2_get_byteu(&ctx->gb);
1513 }
1514 break;
1515 default: // copy 8x8 block from prev, c37_mv from source
1516 mvofs = c37_mv[opc * 2] + (c37_mv[opc * 2 + 1] * w);
1517 for (i = 0; i < 8; i++) {
1518 ofs = i * w;
1519 for (l = 0; l < 8; l++)
1520 *(dst + ofs + l) = *(db + ofs + l + mvofs);
1521 }
1522 break;
1523 }
1524 return 0;
1525 }
1526
1527 static int old_codec48(SANMVideoContext *ctx, int width, int height)
1528 {
1529 uint8_t *dst, *prev;
1530 int compr = bytestream2_get_byte(&ctx->gb);
1531 int mvidx = bytestream2_get_byte(&ctx->gb);
1532 int seq = bytestream2_get_le16(&ctx->gb);
1533 uint32_t decoded_size = bytestream2_get_le32(&ctx->gb);
1534 int i, j, flags;
1535
1536 // all codec48 videos use 1, but just to be safe...
1537 if (mvidx != 1) {
1538 av_log(ctx->avctx, AV_LOG_ERROR, "Invalid motion base value %d.\n", mvidx);
1539 return AVERROR_INVALIDDATA;
1540 }
1541
1542 bytestream2_skip(&ctx->gb, 4);
1543 flags = bytestream2_get_byte(&ctx->gb);
1544 bytestream2_skip(&ctx->gb, 3);
1545
1546 if (flags & 8) {
1547 if (bytestream2_get_bytes_left(&ctx->gb) < 0x8080)
1548 return AVERROR_INVALIDDATA;
1549 codec47_read_interptable(ctx);
1550 }
1551
1552 dst = (uint8_t*)ctx->frm0;
1553 prev = (uint8_t*)ctx->frm2;
1554
1555 if (!seq) {
1556 ctx->prev_seq = -1;
1557 memset(prev, 0, ctx->aligned_height * width);
1558 }
1559
1560 switch (compr) {
1561 case 0:
1562 if (bytestream2_get_bytes_left(&ctx->gb) < width * height)
1563 return AVERROR_INVALIDDATA;
1564 for (j = 0; j < height; j++) {
1565 bytestream2_get_bufferu(&ctx->gb, dst, width);
1566 dst += width;
1567 }
1568 break;
1569 case 2:
1570 if (rle_decode(ctx, &ctx->gb, dst, decoded_size))
1571 return AVERROR_INVALIDDATA;
1572 break;
1573 case 3:
1574 if (seq == ctx->prev_seq + 1) {
1575 for (j = 0; j < height; j += 8) {
1576 for (i = 0; i < width; i += 8) {
1577 if (codec48_block(ctx, dst + i, prev + i, width))
1578 return AVERROR_INVALIDDATA;
1579 }
1580 dst += width * 8;
1581 prev += width * 8;
1582 }
1583 }
1584 break;
1585 case 5:
1586 if (bytestream2_get_bytes_left(&ctx->gb) < ((width + 1) >> 1) * ((height + 1) >> 1))
1587 return AVERROR_INVALIDDATA;
1588 codec47_comp1(ctx, dst, width, height, width);
1589 break;
1590 case 6: // in some videos of "Star Wars - Making Magic", ignored.
1591 break;
1592 default:
1593 avpriv_report_missing_feature(ctx->avctx,
1594 "Subcodec 48 compression %d", compr);
1595 return AVERROR_PATCHWELCOME;
1596 }
1597 ctx->rotate_code = 1; // swap frm[0] and frm[2]
1598 ctx->prev_seq = seq;
1599 return 0;
1600 }
1601
1602 static int process_frame_obj(SANMVideoContext *ctx, GetByteContext *gb)
1603 {
1604 uint16_t w, h, parm2;
1605 uint8_t codec, param;
1606 int16_t left, top;
1607 int fsc, sote, ret;
1608
1609 codec = bytestream2_get_byteu(gb);
1610 param = bytestream2_get_byteu(gb);
1611 left = bytestream2_get_le16u(gb);
1612 top = bytestream2_get_le16u(gb);
1613 w = bytestream2_get_le16u(gb);
1614 h = bytestream2_get_le16u(gb);
1615 bytestream2_skip(gb, 2);
1616 parm2 = bytestream2_get_le16u(gb);
1617
1618 if (w < 1 || h < 1 || w > 800 || h > 600 || left > 800 || top > 600) {
1619 av_log(ctx->avctx, AV_LOG_WARNING,
1620 "ignoring invalid fobj dimensions: c%d %d %d @ %d %d\n",
1621 codec, w, h, left, top);
1622 return 0;
1623 }
1624
1625 /* codecs with their own buffers */
1626 fsc = (codec == 37 || codec == 47 || codec == 48);
1627
1628 /* special case for "Shadows of the Empire" videos */
1629 sote = ((w == 640) && (h == 272) && (codec == 47));
1630 if (sote)
1631 left = top = 0;
1632
1633 if (!ctx->have_dimensions) {
1634 int xres, yres;
1635 if (ctx->subversion < 2) {
1636 /* Rebel Assault 1: 384x242 internal size */
1637 xres = 384;
1638 yres = 242;
1639 ctx->have_dimensions = 1;
1640 } else if (codec == 37 || codec == 47 || codec == 48) {
1641 /* these codecs work on full frames, trust their dimensions */
1642 xres = w;
1643 yres = h;
1644 ctx->have_dimensions = 1;
1645 } else {
1646 /* detect common sizes */
1647 xres = w + left;
1648 yres = h + top;
1649 if (sote) {
1650 /* SotE: has top=60 at all times to center video
1651 * inside the 640x480 game window
1652 */
1653 xres = w;
1654 yres = h;
1655 ctx->have_dimensions = 1;
1656 } else if (((xres == 424) && (yres == 260)) || /* RA1 */
1657 ((xres == 320) && (yres == 200)) || /* ft/dig/... */
1658 ((xres == 640) && (yres == 480))) { /* ol/comi/mots... */
1659 ctx->have_dimensions = 1;
1660 }
1661
1662 xres = FFMAX(xres, ctx->width);
1663 yres = FFMAX(yres, ctx->height);
1664 }
1665
1666 if (ctx->width < xres || ctx->height < yres) {
1667 int ret = ff_set_dimensions(ctx->avctx, xres, yres);
1668 if (ret < 0)
1669 return ret;
1670 init_sizes(ctx, xres, yres);
1671 if (init_buffers(ctx)) {
1672 av_log(ctx->avctx, AV_LOG_ERROR, "Error resizing buffers.\n");
1673 return AVERROR(ENOMEM);
1674 }
1675 }
1676 } else {
1677 if (((left + w > ctx->width) || (top + h > ctx->height)) && fsc) {
1678 /* correct unexpected overly large frames: this happens
1679 * for instance with The Dig's sq1.san video: it has a few
1680 * (all black) 640x480 frames halfway in, while the rest is
1681 * 320x200.
1682 */
1683 av_log(ctx->avctx, AV_LOG_WARNING,
1684 "resizing too large fobj: c%d %d %d @ %d %d\n", codec, w, h, left, top);
1685 w = ctx->width;
1686 h = ctx->height;
1687 }
1688 }
1689
1690 /* clear the main buffer on the first fob */
1691 if (ctx->first_fob) {
1692 ctx->first_fob = 0;
1693 if (!fsc)
1694 memset(ctx->fbuf, 0, ctx->frm0_size);
1695 }
1696
1697 switch (codec) {
1698 case 1:
1699 case 3:
1700 return old_codec1(ctx, gb, top, left, w, h, codec == 3);
1701 case 2:
1702 return old_codec2(ctx, gb, top, left, w, h);
1703 case 4:
1704 case 5:
1705 case 33:
1706 case 34:
1707 return old_codec4(ctx, gb, top, left, w, h, param, parm2, codec);
1708 case 20:
1709 return old_codec20(ctx, w, h);
1710 case 21:
1711 return old_codec21(ctx, gb, top, left, w, h);
1712 case 23:
1713 return old_codec23(ctx, gb, top, left, w, h, param, parm2);
1714 case 37:
1715 ret = old_codec37(ctx, w, h); break;
1716 case 45:
1717 return 0;
1718 case 47:
1719 ret = old_codec47(ctx, w, h); break;
1720 case 48:
1721 ret = old_codec48(ctx, w, h); break;
1722 default:
1723 avpriv_request_sample(ctx->avctx, "Subcodec %d", codec);
1724 ctx->frame->flags |= AV_FRAME_FLAG_CORRUPT;
1725 return 0;
1726 }
1727 if (ret)
1728 return ret;
1729
1730 /* copy the codec37/47/48 result to main buffer */
1731 if ((w == ctx->width) && (h == ctx->height)) {
1732 memcpy(ctx->fbuf, ctx->frm0, ctx->fbuf_size);
1733 } else {
1734 uint8_t *dst = (uint8_t *)ctx->fbuf + left + top * ctx->pitch;
1735 const uint8_t *src = (uint8_t *)ctx->frm0;
1736 const int cw = FFMIN(w, ctx->width - left);
1737 const int ch = FFMIN(h, ctx->height - top);
1738 if ((cw > 0) && (ch > 0) && (left >= 0) && (top >= 0)) {
1739 for (int i = 0; i < ch; i++) {
1740 memcpy(dst, src, cw);
1741 dst += ctx->pitch;
1742 src += w;
1743 }
1744 }
1745 }
1746 return 0;
1747 }
1748
1749 static int process_ftch(SANMVideoContext *ctx, int size)
1750 {
1751 uint8_t *sf = ctx->stored_frame;
1752 int xoff, yoff, left, top, ret;
1753 GetByteContext gb;
1754 uint32_t sz;
1755
1756 /* FTCH defines additional x/y offsets */
1757 if (size != 12) {
1758 if (bytestream2_get_bytes_left(&ctx->gb) < 6)
1759 return AVERROR_INVALIDDATA;
1760 bytestream2_skip(&ctx->gb, 2);
1761 xoff = bytestream2_get_le16u(&ctx->gb);
1762 yoff = bytestream2_get_le16u(&ctx->gb);
1763 } else {
1764 if (bytestream2_get_bytes_left(&ctx->gb) < 12)
1765 return AVERROR_INVALIDDATA;
1766 bytestream2_skip(&ctx->gb, 4);
1767 xoff = bytestream2_get_be32u(&ctx->gb);
1768 yoff = bytestream2_get_be32u(&ctx->gb);
1769 }
1770
1771 sz = *(uint32_t *)(sf + 0);
1772 if ((sz > 0) && (sz <= ctx->stored_frame_size - 4)) {
1773 /* add the FTCH offsets to the left/top values of the stored FOBJ */
1774 left = av_le2ne16(*(int16_t *)(sf + 4 + 2));
1775 top = av_le2ne16(*(int16_t *)(sf + 4 + 4));
1776 *(int16_t *)(sf + 4 + 2) = av_le2ne16(left + xoff);
1777 *(int16_t *)(sf + 4 + 4) = av_le2ne16(top + yoff);
1778
1779 /* decode the stored FOBJ */
1780 bytestream2_init(&gb, sf + 4, sz);
1781 ret = process_frame_obj(ctx, &gb);
1782
1783 /* now restore the original left/top values again */
1784 *(int16_t *)(sf + 4 + 2) = av_le2ne16(left);
1785 *(int16_t *)(sf + 4 + 4) = av_le2ne16(top);
1786 } else {
1787 /* this happens a lot in RA1: The individual files are meant to
1788 * be played in sequence, with some referencing objects STORed
1789 * by previous files, e.g. the cockpit codec21 object in RA1 LVL8.
1790 * But spamming the log with errors is also not helpful, so
1791 * here we simply ignore this case.
1792 */
1793 ret = 0;
1794 }
1795 return ret;
1796 }
1797
1798 static int process_xpal(SANMVideoContext *ctx, int size)
1799 {
1800 int16_t *dp = ctx->delta_pal;
1801 uint32_t *pal = ctx->pal;
1802 uint16_t cmd;
1803 uint8_t c[3];
1804 int i, j;
1805
1806 bytestream2_skip(&ctx->gb, 2);
1807 cmd = bytestream2_get_be16(&ctx->gb);
1808
1809 if (cmd == 1) {
1810 for (i = 0; i < PALETTE_DELTA; i += 3) {
1811 c[0] = (*pal >> 16) & 0xFF;
1812 c[1] = (*pal >> 8) & 0xFF;
1813 c[2] = (*pal >> 0) & 0xFF;
1814 for (j = 0; j < 3; j++) {
1815 int cl = (c[j] * 129) + *dp++;
1816 c[j] = av_clip_uint8(cl / 128) & 0xFF;
1817 }
1818 *pal++ = 0xFFU << 24 | c[0] << 16 | c[1] << 8 | c[2];
1819 }
1820 } else if (cmd == 2) {
1821 if (size < PALETTE_DELTA * 2 + 4) {
1822 av_log(ctx->avctx, AV_LOG_ERROR,
1823 "Incorrect palette change block size %"PRIu32".\n", size);
1824 return AVERROR_INVALIDDATA;
1825 }
1826 for (i = 0; i < PALETTE_DELTA; i++)
1827 dp[i] = bytestream2_get_le16u(&ctx->gb);
1828
1829 if (size >= PALETTE_DELTA * 2 + 4 + PALETTE_SIZE * 3) {
1830 for (i = 0; i < PALETTE_SIZE; i++)
1831 ctx->pal[i] = 0xFFU << 24 | bytestream2_get_be24u(&ctx->gb);
1832 if (ctx->subversion < 2)
1833 ctx->pal[0] = 0xFFU << 24;
1834 }
1835 }
1836 return 0;
1837 }
1838
1839 static int decode_0(SANMVideoContext *ctx)
1840 {
1841 uint16_t *frm = ctx->frm0;
1842 int x, y;
1843
1844 if (bytestream2_get_bytes_left(&ctx->gb) < ctx->width * ctx->height * 2) {
1845 av_log(ctx->avctx, AV_LOG_ERROR, "Insufficient data for raw frame.\n");
1846 return AVERROR_INVALIDDATA;
1847 }
1848 for (y = 0; y < ctx->height; y++) {
1849 for (x = 0; x < ctx->width; x++)
1850 frm[x] = bytestream2_get_le16u(&ctx->gb);
1851 frm += ctx->pitch;
1852 }
1853 return 0;
1854 }
1855
1856 static int decode_nop(SANMVideoContext *ctx)
1857 {
1858 avpriv_request_sample(ctx->avctx, "Unknown/unsupported compression type");
1859 return AVERROR_PATCHWELCOME;
1860 }
1861
1862 42964 static void copy_block(uint16_t *pdest, uint16_t *psrc, int block_size, ptrdiff_t pitch)
1863 {
1864 42964 uint8_t *dst = (uint8_t *)pdest;
1865 42964 uint8_t *src = (uint8_t *)psrc;
1866 42964 ptrdiff_t stride = pitch * 2;
1867
1868
3/4
✓ Branch 0 taken 14526 times.
✓ Branch 1 taken 12400 times.
✓ Branch 2 taken 16038 times.
✗ Branch 3 not taken.
42964 switch (block_size) {
1869 14526 case 2:
1870 14526 copy_block4(dst, src, stride, stride, 2);
1871 14526 break;
1872 12400 case 4:
1873 12400 copy_block8(dst, src, stride, stride, 4);
1874 12400 break;
1875 16038 case 8:
1876 16038 copy_block16(dst, src, stride, stride, 8);
1877 16038 break;
1878 }
1879 42964 }
1880
1881 7913 static void fill_block(uint16_t *pdest, uint16_t color, int block_size, ptrdiff_t pitch)
1882 {
1883 int x, y;
1884
1885 7913 pitch -= block_size;
1886
2/2
✓ Branch 0 taken 30438 times.
✓ Branch 1 taken 7913 times.
38351 for (y = 0; y < block_size; y++, pdest += pitch)
1887
2/2
✓ Branch 0 taken 149540 times.
✓ Branch 1 taken 30438 times.
179978 for (x = 0; x < block_size; x++)
1888 149540 *pdest++ = color;
1889 7913 }
1890
1891 8368 static int draw_glyph(SANMVideoContext *ctx, uint16_t *dst, int index,
1892 uint16_t fg_color, uint16_t bg_color, int block_size,
1893 ptrdiff_t pitch)
1894 {
1895 int8_t *pglyph;
1896 8368 uint16_t colors[2] = { fg_color, bg_color };
1897 int x, y;
1898
1899
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8368 times.
8368 if (index >= NGLYPHS) {
1900 av_log(ctx->avctx, AV_LOG_ERROR, "Ignoring nonexistent glyph #%u.\n", index);
1901 return AVERROR_INVALIDDATA;
1902 }
1903
1904
2/2
✓ Branch 0 taken 1158 times.
✓ Branch 1 taken 7210 times.
8368 pglyph = block_size == 8 ? ctx->p8x8glyphs[index] : ctx->p4x4glyphs[index];
1905 8368 pitch -= block_size;
1906
1907
2/2
✓ Branch 0 taken 38104 times.
✓ Branch 1 taken 8368 times.
46472 for (y = 0; y < block_size; y++, dst += pitch)
1908
2/2
✓ Branch 0 taken 189472 times.
✓ Branch 1 taken 38104 times.
227576 for (x = 0; x < block_size; x++)
1909 189472 *dst++ = colors[*pglyph++];
1910 8368 return 0;
1911 }
1912
1913 8029 static int opcode_0xf7(SANMVideoContext *ctx, int cx, int cy, int block_size, ptrdiff_t pitch)
1914 {
1915 8029 uint16_t *dst = ctx->frm0 + cx + cy * ctx->pitch;
1916
1917
2/2
✓ Branch 0 taken 825 times.
✓ Branch 1 taken 7204 times.
8029 if (block_size == 2) {
1918 uint32_t indices;
1919
1920
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 825 times.
825 if (bytestream2_get_bytes_left(&ctx->gb) < 4)
1921 return AVERROR_INVALIDDATA;
1922
1923 825 indices = bytestream2_get_le32u(&ctx->gb);
1924 825 dst[0] = ctx->codebook[indices & 0xFF];
1925 825 indices >>= 8;
1926 825 dst[1] = ctx->codebook[indices & 0xFF];
1927 825 indices >>= 8;
1928 825 dst[pitch] = ctx->codebook[indices & 0xFF];
1929 825 indices >>= 8;
1930 825 dst[pitch + 1] = ctx->codebook[indices & 0xFF];
1931 } else {
1932 uint16_t fgcolor, bgcolor;
1933 int glyph;
1934
1935
2/2
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 7203 times.
7204 if (bytestream2_get_bytes_left(&ctx->gb) < 3)
1936 1 return AVERROR_INVALIDDATA;
1937
1938 7203 glyph = bytestream2_get_byteu(&ctx->gb);
1939 7203 bgcolor = ctx->codebook[bytestream2_get_byteu(&ctx->gb)];
1940 7203 fgcolor = ctx->codebook[bytestream2_get_byteu(&ctx->gb)];
1941
1942 7203 draw_glyph(ctx, dst, glyph, fgcolor, bgcolor, block_size, pitch);
1943 }
1944 8028 return 0;
1945 }
1946
1947 1169 static int opcode_0xf8(SANMVideoContext *ctx, int cx, int cy, int block_size, ptrdiff_t pitch)
1948 {
1949 1169 uint16_t *dst = ctx->frm0 + cx + cy * ctx->pitch;
1950
1951
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 1165 times.
1169 if (block_size == 2) {
1952
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
4 if (bytestream2_get_bytes_left(&ctx->gb) < 8)
1953 return AVERROR_INVALIDDATA;
1954
1955 4 dst[0] = bytestream2_get_le16u(&ctx->gb);
1956 4 dst[1] = bytestream2_get_le16u(&ctx->gb);
1957 4 dst[pitch] = bytestream2_get_le16u(&ctx->gb);
1958 4 dst[pitch + 1] = bytestream2_get_le16u(&ctx->gb);
1959 } else {
1960 uint16_t fgcolor, bgcolor;
1961 int glyph;
1962
1963
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1165 times.
1165 if (bytestream2_get_bytes_left(&ctx->gb) < 5)
1964 return AVERROR_INVALIDDATA;
1965
1966 1165 glyph = bytestream2_get_byteu(&ctx->gb);
1967 1165 bgcolor = bytestream2_get_le16u(&ctx->gb);
1968 1165 fgcolor = bytestream2_get_le16u(&ctx->gb);
1969
1970 1165 draw_glyph(ctx, dst, glyph, fgcolor, bgcolor, block_size, pitch);
1971 }
1972 1169 return 0;
1973 }
1974
1975 30376 static int good_mvec(SANMVideoContext *ctx, int cx, int cy, int mx, int my,
1976 int block_size)
1977 {
1978 30376 int start_pos = cx + mx + (cy + my) * ctx->pitch;
1979 30376 int end_pos = start_pos + (block_size - 1) * (ctx->pitch + 1);
1980
1981
2/4
✓ Branch 0 taken 30376 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 30376 times.
✗ Branch 3 not taken.
30376 int good = start_pos >= 0 && end_pos < (ctx->buf_size >> 1);
1982
1983
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 30376 times.
30376 if (!good)
1984 av_log(ctx->avctx, AV_LOG_ERROR,
1985 "Ignoring invalid motion vector (%i, %i)->(%u, %u), block size = %u\n",
1986 cx + mx, cy + my, cx, cy, block_size);
1987
1988 30376 return good;
1989 }
1990
1991 71636 static int codec2subblock(SANMVideoContext *ctx, int cx, int cy, int blk_size)
1992 {
1993 int16_t mx, my, index;
1994 int opcode;
1995
1996
2/2
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 71635 times.
71636 if (bytestream2_get_bytes_left(&ctx->gb) < 1)
1997 1 return AVERROR_INVALIDDATA;
1998
1999 71635 opcode = bytestream2_get_byteu(&ctx->gb);
2000
2001
9/9
✓ Branch 0 taken 28212 times.
✓ Branch 1 taken 2164 times.
✓ Branch 2 taken 12588 times.
✓ Branch 3 taken 8029 times.
✓ Branch 4 taken 1169 times.
✓ Branch 5 taken 315 times.
✓ Branch 6 taken 5496 times.
✓ Branch 7 taken 2102 times.
✓ Branch 8 taken 11560 times.
71635 switch (opcode) {
2002 28212 default:
2003 28212 mx = motion_vectors[opcode][0];
2004 28212 my = motion_vectors[opcode][1];
2005
2006
1/2
✓ Branch 1 taken 28212 times.
✗ Branch 2 not taken.
28212 if (good_mvec(ctx, cx, cy, mx, my, blk_size)) {
2007 28212 copy_block(ctx->frm0 + cx + ctx->pitch * cy,
2008 28212 ctx->frm2 + cx + mx + ctx->pitch * (cy + my),
2009 blk_size, ctx->pitch);
2010 }
2011 28212 break;
2012 2164 case 0xF5:
2013
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2164 times.
2164 if (bytestream2_get_bytes_left(&ctx->gb) < 2)
2014 return AVERROR_INVALIDDATA;
2015 2164 index = bytestream2_get_le16u(&ctx->gb);
2016
2017 2164 mx = index % ctx->width;
2018 2164 my = index / ctx->width;
2019
2020
1/2
✓ Branch 1 taken 2164 times.
✗ Branch 2 not taken.
2164 if (good_mvec(ctx, cx, cy, mx, my, blk_size)) {
2021 2164 copy_block(ctx->frm0 + cx + ctx->pitch * cy,
2022 2164 ctx->frm2 + cx + mx + ctx->pitch * (cy + my),
2023 blk_size, ctx->pitch);
2024 }
2025 2164 break;
2026 12588 case 0xF6:
2027 12588 copy_block(ctx->frm0 + cx + ctx->pitch * cy,
2028 12588 ctx->frm1 + cx + ctx->pitch * cy,
2029 blk_size, ctx->pitch);
2030 12588 break;
2031 8029 case 0xF7:
2032 8029 opcode_0xf7(ctx, cx, cy, blk_size, ctx->pitch);
2033 8029 break;
2034
2035 1169 case 0xF8:
2036 1169 opcode_0xf8(ctx, cx, cy, blk_size, ctx->pitch);
2037 1169 break;
2038 315 case 0xF9:
2039 case 0xFA:
2040 case 0xFB:
2041 case 0xFC:
2042 315 fill_block(ctx->frm0 + cx + cy * ctx->pitch,
2043 315 ctx->small_codebook[opcode - 0xf9], blk_size, ctx->pitch);
2044 315 break;
2045 5496 case 0xFD:
2046
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 5496 times.
5496 if (bytestream2_get_bytes_left(&ctx->gb) < 1)
2047 return AVERROR_INVALIDDATA;
2048 10992 fill_block(ctx->frm0 + cx + cy * ctx->pitch,
2049 5496 ctx->codebook[bytestream2_get_byteu(&ctx->gb)], blk_size, ctx->pitch);
2050 5496 break;
2051 2102 case 0xFE:
2052
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2102 times.
2102 if (bytestream2_get_bytes_left(&ctx->gb) < 2)
2053 return AVERROR_INVALIDDATA;
2054 2102 fill_block(ctx->frm0 + cx + cy * ctx->pitch,
2055 2102 bytestream2_get_le16u(&ctx->gb), blk_size, ctx->pitch);
2056 2102 break;
2057 11560 case 0xFF:
2058
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11560 times.
11560 if (blk_size == 2) {
2059 opcode_0xf8(ctx, cx, cy, blk_size, ctx->pitch);
2060 } else {
2061 11560 blk_size >>= 1;
2062
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 11560 times.
11560 if (codec2subblock(ctx, cx, cy, blk_size))
2063 return AVERROR_INVALIDDATA;
2064
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 11560 times.
11560 if (codec2subblock(ctx, cx + blk_size, cy, blk_size))
2065 return AVERROR_INVALIDDATA;
2066
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 11560 times.
11560 if (codec2subblock(ctx, cx, cy + blk_size, blk_size))
2067 return AVERROR_INVALIDDATA;
2068
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 11560 times.
11560 if (codec2subblock(ctx, cx + blk_size, cy + blk_size, blk_size))
2069 return AVERROR_INVALIDDATA;
2070 }
2071 11560 break;
2072 }
2073 71635 return 0;
2074 }
2075
2076 6 static int decode_2(SANMVideoContext *ctx)
2077 {
2078 int cx, cy, ret;
2079
2080
2/2
✓ Branch 0 taken 318 times.
✓ Branch 1 taken 5 times.
323 for (cy = 0; cy < ctx->aligned_height; cy += 8)
2081
2/2
✓ Branch 0 taken 25396 times.
✓ Branch 1 taken 317 times.
25713 for (cx = 0; cx < ctx->aligned_width; cx += 8)
2082
2/2
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 25395 times.
25396 if (ret = codec2subblock(ctx, cx, cy, 8))
2083 1 return ret;
2084
2085 5 return 0;
2086 }
2087
2088 static int decode_3(SANMVideoContext *ctx)
2089 {
2090 memcpy(ctx->frm0, ctx->frm2, ctx->frm2_size);
2091 return 0;
2092 }
2093
2094 9 static int decode_4(SANMVideoContext *ctx)
2095 {
2096 9 memcpy(ctx->frm0, ctx->frm1, ctx->frm1_size);
2097 9 return 0;
2098 }
2099
2100 1 static int decode_5(SANMVideoContext *ctx)
2101 {
2102 #if HAVE_BIGENDIAN
2103 uint16_t *frm;
2104 int npixels;
2105 #endif
2106 1 uint8_t *dst = (uint8_t*)ctx->frm0;
2107
2108
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 if (rle_decode(ctx, &ctx->gb, dst, ctx->buf_size))
2109 return AVERROR_INVALIDDATA;
2110
2111 #if HAVE_BIGENDIAN
2112 npixels = ctx->npixels;
2113 frm = ctx->frm0;
2114 while (npixels--) {
2115 *frm = av_bswap16(*frm);
2116 frm++;
2117 }
2118 #endif
2119
2120 1 return 0;
2121 }
2122
2123 static int decode_6(SANMVideoContext *ctx)
2124 {
2125 int npixels = ctx->npixels;
2126 uint16_t *frm = ctx->frm0;
2127
2128 if (bytestream2_get_bytes_left(&ctx->gb) < npixels) {
2129 av_log(ctx->avctx, AV_LOG_ERROR, "Insufficient data for frame.\n");
2130 return AVERROR_INVALIDDATA;
2131 }
2132 while (npixels--)
2133 *frm++ = ctx->codebook[bytestream2_get_byteu(&ctx->gb)];
2134
2135 return 0;
2136 }
2137
2138 static int decode_8(SANMVideoContext *ctx)
2139 {
2140 uint16_t *pdest = ctx->frm0;
2141 uint8_t *rsrc;
2142 long npixels = ctx->npixels;
2143
2144 av_fast_malloc(&ctx->rle_buf, &ctx->rle_buf_size, npixels);
2145 if (!ctx->rle_buf) {
2146 av_log(ctx->avctx, AV_LOG_ERROR, "RLE buffer allocation failed.\n");
2147 return AVERROR(ENOMEM);
2148 }
2149 rsrc = ctx->rle_buf;
2150
2151 if (rle_decode(ctx, &ctx->gb, rsrc, npixels))
2152 return AVERROR_INVALIDDATA;
2153
2154 while (npixels--)
2155 *pdest++ = ctx->codebook[*rsrc++];
2156
2157 return 0;
2158 }
2159
2160 typedef int (*frm_decoder)(SANMVideoContext *ctx);
2161
2162 static const frm_decoder v1_decoders[] = {
2163 decode_0, decode_nop, decode_2, decode_3, decode_4, decode_5,
2164 decode_6, decode_nop, decode_8
2165 };
2166
2167 16 static int read_frame_header(SANMVideoContext *ctx, SANMFrameHeader *hdr)
2168 {
2169 int i, ret;
2170
2171
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 16 times.
16 if ((ret = bytestream2_get_bytes_left(&ctx->gb)) < 560) {
2172 av_log(ctx->avctx, AV_LOG_ERROR, "Input frame too short (%d bytes).\n",
2173 ret);
2174 return AVERROR_INVALIDDATA;
2175 }
2176 16 bytestream2_skip(&ctx->gb, 8); // skip pad
2177
2178 16 hdr->width = bytestream2_get_le32u(&ctx->gb);
2179 16 hdr->height = bytestream2_get_le32u(&ctx->gb);
2180
2181
2/4
✓ Branch 0 taken 16 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 16 times.
16 if (hdr->width != ctx->width || hdr->height != ctx->height) {
2182 avpriv_report_missing_feature(ctx->avctx, "Variable size frames");
2183 return AVERROR_PATCHWELCOME;
2184 }
2185
2186 16 hdr->seq_num = bytestream2_get_le16u(&ctx->gb);
2187 16 hdr->codec = bytestream2_get_byteu(&ctx->gb);
2188 16 hdr->rotate_code = bytestream2_get_byteu(&ctx->gb);
2189
2190 16 bytestream2_skip(&ctx->gb, 4); // skip pad
2191
2192
2/2
✓ Branch 0 taken 64 times.
✓ Branch 1 taken 16 times.
80 for (i = 0; i < 4; i++)
2193 64 ctx->small_codebook[i] = bytestream2_get_le16u(&ctx->gb);
2194 16 hdr->bg_color = bytestream2_get_le16u(&ctx->gb);
2195
2196 16 bytestream2_skip(&ctx->gb, 2); // skip pad
2197
2198 16 hdr->rle_output_size = bytestream2_get_le32u(&ctx->gb);
2199
2/2
✓ Branch 0 taken 4096 times.
✓ Branch 1 taken 16 times.
4112 for (i = 0; i < 256; i++)
2200 4096 ctx->codebook[i] = bytestream2_get_le16u(&ctx->gb);
2201
2202 16 bytestream2_skip(&ctx->gb, 8); // skip pad
2203
2204 16 return 0;
2205 }
2206
2207 2 static void fill_frame(uint16_t *pbuf, int buf_size, uint16_t color)
2208 {
2209
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if (buf_size--) {
2210 2 *pbuf++ = color;
2211 2 av_memcpy_backptr((uint8_t*)pbuf, 2, 2*buf_size);
2212 }
2213 2 }
2214
2215 15 static int copy_output(SANMVideoContext *ctx, SANMFrameHeader *hdr)
2216 {
2217 uint8_t *dst;
2218
1/2
✓ Branch 0 taken 15 times.
✗ Branch 1 not taken.
15 const uint8_t *src = hdr ? (uint8_t *)ctx->frm0 : (uint8_t *)ctx->fbuf;
2219 15 int ret, height = ctx->height;
2220
1/2
✓ Branch 0 taken 15 times.
✗ Branch 1 not taken.
15 ptrdiff_t dstpitch, srcpitch = ctx->pitch * (hdr ? sizeof(ctx->frm0[0]) : 1);
2221
2222
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 15 times.
15 if ((ret = ff_get_buffer(ctx->avctx, ctx->frame, 0)) < 0)
2223 return ret;
2224
2225 15 dst = ctx->frame->data[0];
2226 15 dstpitch = ctx->frame->linesize[0];
2227
2228
2/2
✓ Branch 0 taken 7200 times.
✓ Branch 1 taken 15 times.
7215 while (height--) {
2229 7200 memcpy(dst, src, srcpitch);
2230 7200 src += srcpitch;
2231 7200 dst += dstpitch;
2232 }
2233
2234 15 return 0;
2235 }
2236
2237 16 static int decode_frame(AVCodecContext *avctx, AVFrame *frame,
2238 int *got_frame_ptr, AVPacket *pkt)
2239 {
2240 16 SANMVideoContext *ctx = avctx->priv_data;
2241 int i, ret;
2242
2243 16 ctx->frame = frame;
2244 16 bytestream2_init(&ctx->gb, pkt->data, pkt->size);
2245
2246
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 16 times.
16 if (!ctx->version) {
2247 int to_store = 0, have_img = 0;
2248
2249 ctx->first_fob = 1;
2250
2251 while (bytestream2_get_bytes_left(&ctx->gb) >= 8) {
2252 uint32_t sig, size;
2253 int pos;
2254
2255 sig = bytestream2_get_be32u(&ctx->gb);
2256 size = bytestream2_get_be32u(&ctx->gb);
2257 pos = bytestream2_tell(&ctx->gb);
2258
2259 if (bytestream2_get_bytes_left(&ctx->gb) < size) {
2260 av_log(avctx, AV_LOG_ERROR, "Incorrect chunk size %"PRIu32".\n", size);
2261 break;
2262 }
2263 switch (sig) {
2264 case MKBETAG('N', 'P', 'A', 'L'):
2265 if (size != PALETTE_SIZE * 3) {
2266 av_log(avctx, AV_LOG_ERROR,
2267 "Incorrect palette block size %"PRIu32".\n", size);
2268 return AVERROR_INVALIDDATA;
2269 }
2270 for (i = 0; i < PALETTE_SIZE; i++)
2271 ctx->pal[i] = 0xFFU << 24 | bytestream2_get_be24u(&ctx->gb);
2272 if (ctx->subversion < 2)
2273 ctx->pal[0] = 0xFFU << 24;
2274 break;
2275 case MKBETAG('F', 'O', 'B', 'J'):
2276 if (size < 16)
2277 return AVERROR_INVALIDDATA;
2278 if (ret = process_frame_obj(ctx, &ctx->gb))
2279 return ret;
2280 have_img = 1;
2281
2282 /* STOR: for ANIMv0/1 store the whole FOBJ datablock, as it
2283 * needs to be replayed on FTCH, since none of the codecs
2284 * it uses work on the full buffer.
2285 * For ANIMv2, it's enough to store the current framebuffer.
2286 */
2287 if (to_store) {
2288 to_store = 0;
2289 if (ctx->subversion < 2) {
2290 if (size + 4 <= ctx->stored_frame_size) {
2291 int pos2 = bytestream2_tell(&ctx->gb);
2292 bytestream2_seek(&ctx->gb, pos, SEEK_SET);
2293 *(uint32_t *)(ctx->stored_frame) = size;
2294 bytestream2_get_bufferu(&ctx->gb, ctx->stored_frame + 4, size);
2295 bytestream2_seek(&ctx->gb, pos2, SEEK_SET);
2296 } else {
2297 av_log(avctx, AV_LOG_ERROR, "FOBJ too large for STOR\n");
2298 ret = AVERROR(ENOMEM);
2299 }
2300 } else {
2301 memcpy(ctx->stored_frame, ctx->fbuf, ctx->buf_size);
2302 }
2303 }
2304 break;
2305 case MKBETAG('X', 'P', 'A', 'L'):
2306 if (ret = process_xpal(ctx, size))
2307 return ret;
2308 break;
2309 case MKBETAG('S', 'T', 'O', 'R'):
2310 to_store = 1;
2311 break;
2312 case MKBETAG('F', 'T', 'C', 'H'):
2313 if (ctx->subversion < 2) {
2314 if (ret = process_ftch(ctx, size))
2315 return ret;
2316 } else {
2317 memcpy(ctx->fbuf, ctx->stored_frame, ctx->buf_size);
2318 }
2319 have_img = 1;
2320 break;
2321 default:
2322 bytestream2_skip(&ctx->gb, size);
2323 av_log(avctx, AV_LOG_DEBUG,
2324 "Unknown/unsupported chunk %"PRIx32".\n", sig);
2325 break;
2326 }
2327
2328 /* the sizes of chunks are usually a multiple of 2. However
2329 * there are a few unaligned FOBJs in RA1 L2PLAY.ANM only (looks
2330 * like a game bug) and IACT audio chunks which have odd sizes
2331 * but are padded with a zero byte.
2332 */
2333 bytestream2_seek(&ctx->gb, pos + size, SEEK_SET);
2334 if ((pos + size) & 1) {
2335 if (0 != bytestream2_get_byteu(&ctx->gb))
2336 bytestream2_seek(&ctx->gb, pos + size, SEEK_SET);
2337 }
2338 }
2339
2340 if (have_img) {
2341 if ((ret = copy_output(ctx, NULL)))
2342 return ret;
2343 memcpy(ctx->frame->data[1], ctx->pal, 1024);
2344 *got_frame_ptr = 1;
2345 }
2346 } else {
2347 SANMFrameHeader header;
2348
2349
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 16 times.
16 if ((ret = read_frame_header(ctx, &header)))
2350 1 return ret;
2351
2352 16 ctx->rotate_code = header.rotate_code;
2353
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 15 times.
16 if (!header.seq_num) {
2354 1 ctx->frame->flags |= AV_FRAME_FLAG_KEY;
2355 1 ctx->frame->pict_type = AV_PICTURE_TYPE_I;
2356 1 fill_frame(ctx->frm1, ctx->npixels, header.bg_color);
2357 1 fill_frame(ctx->frm2, ctx->npixels, header.bg_color);
2358 } else {
2359 15 ctx->frame->flags &= ~AV_FRAME_FLAG_KEY;
2360 15 ctx->frame->pict_type = AV_PICTURE_TYPE_P;
2361 }
2362
2363
1/2
✓ Branch 0 taken 16 times.
✗ Branch 1 not taken.
16 if (header.codec < FF_ARRAY_ELEMS(v1_decoders)) {
2364
2/2
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 15 times.
16 if ((ret = v1_decoders[header.codec](ctx))) {
2365 1 av_log(avctx, AV_LOG_ERROR,
2366 "Subcodec %d: error decoding frame.\n", header.codec);
2367 1 return ret;
2368 }
2369 } else {
2370 avpriv_request_sample(avctx, "Subcodec %d", header.codec);
2371 return AVERROR_PATCHWELCOME;
2372 }
2373
2374
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 15 times.
15 if ((ret = copy_output(ctx, &header)))
2375 return ret;
2376
2377 15 *got_frame_ptr = 1;
2378
2379 }
2380
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 6 times.
15 if (ctx->rotate_code)
2381 9 rotate_bufs(ctx, ctx->rotate_code);
2382
2383 15 return pkt->size;
2384 }
2385
2386 const FFCodec ff_sanm_decoder = {
2387 .p.name = "sanm",
2388 CODEC_LONG_NAME("LucasArts SANM/Smush video"),
2389 .p.type = AVMEDIA_TYPE_VIDEO,
2390 .p.id = AV_CODEC_ID_SANM,
2391 .priv_data_size = sizeof(SANMVideoContext),
2392 .init = decode_init,
2393 .close = decode_end,
2394 FF_CODEC_DECODE_CB(decode_frame),
2395 .p.capabilities = AV_CODEC_CAP_DR1,
2396 };
2397