FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/sanm.c
Date: 2025-01-20 09:27:23
Exec Total Coverage
Lines: 326 994 32.8%
Functions: 25 42 59.5%
Branches: 164 601 27.3%

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;
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 *frm0, *frm1, *frm2;
278 uint8_t *stored_frame;
279 uint32_t 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 } SANMVideoContext;
296
297 typedef struct SANMFrameHeader {
298 int seq_num, codec, rotate_code, rle_output_size;
299
300 uint16_t bg_color;
301 uint32_t width, height;
302 } SANMFrameHeader;
303
304 enum GlyphEdge {
305 LEFT_EDGE,
306 TOP_EDGE,
307 RIGHT_EDGE,
308 BOTTOM_EDGE,
309 NO_EDGE
310 };
311
312 enum GlyphDir {
313 DIR_LEFT,
314 DIR_UP,
315 DIR_RIGHT,
316 DIR_DOWN,
317 NO_DIR
318 };
319
320 /**
321 * Return enum GlyphEdge of box where point (x, y) lies.
322 *
323 * @param x x point coordinate
324 * @param y y point coordinate
325 * @param edge_size box width/height.
326 */
327 1632 static enum GlyphEdge which_edge(int x, int y, int edge_size)
328 {
329 1632 const int edge_max = edge_size - 1;
330
331
2/2
✓ Branch 0 taken 408 times.
✓ Branch 1 taken 1224 times.
1632 if (!y)
332 408 return BOTTOM_EDGE;
333
2/2
✓ Branch 0 taken 408 times.
✓ Branch 1 taken 816 times.
1224 else if (y == edge_max)
334 408 return TOP_EDGE;
335
2/2
✓ Branch 0 taken 306 times.
✓ Branch 1 taken 510 times.
816 else if (!x)
336 306 return LEFT_EDGE;
337
2/2
✓ Branch 0 taken 306 times.
✓ Branch 1 taken 204 times.
510 else if (x == edge_max)
338 306 return RIGHT_EDGE;
339 else
340 204 return NO_EDGE;
341 }
342
343 1536 static enum GlyphDir which_direction(enum GlyphEdge edge0, enum GlyphEdge edge1)
344 {
345
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) ||
346
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) ||
347
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) ||
348
2/2
✓ Branch 0 taken 192 times.
✓ Branch 1 taken 96 times.
288 (edge1 == BOTTOM_EDGE && edge0 != TOP_EDGE))
349 600 return DIR_UP;
350
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) ||
351
2/2
✓ Branch 0 taken 192 times.
✓ Branch 1 taken 96 times.
288 (edge1 == TOP_EDGE && edge0 != BOTTOM_EDGE))
352 480 return DIR_DOWN;
353
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) ||
354
1/2
✓ Branch 0 taken 24 times.
✗ Branch 1 not taken.
24 (edge1 == LEFT_EDGE && edge0 != RIGHT_EDGE))
355 108 return DIR_LEFT;
356
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) ||
357
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) ||
358
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) ||
359
1/2
✓ Branch 0 taken 24 times.
✗ Branch 1 not taken.
24 (edge1 == RIGHT_EDGE && edge0 != LEFT_EDGE))
360 300 return DIR_RIGHT;
361
362 48 return NO_DIR;
363 }
364
365 /* Interpolate two points. */
366 6600 static void interp_point(int8_t *points, int x0, int y0, int x1, int y1,
367 int pos, int npoints)
368 {
369
2/2
✓ Branch 0 taken 6504 times.
✓ Branch 1 taken 96 times.
6600 if (npoints) {
370 6504 points[0] = (x0 * pos + x1 * (npoints - pos) + (npoints >> 1)) / npoints;
371 6504 points[1] = (y0 * pos + y1 * (npoints - pos) + (npoints >> 1)) / npoints;
372 } else {
373 96 points[0] = x0;
374 96 points[1] = y0;
375 }
376 6600 }
377
378 /**
379 * Construct glyphs by iterating through vector coordinates.
380 *
381 * @param pglyphs pointer to table where glyphs are stored
382 * @param xvec pointer to x component of vector coordinates
383 * @param yvec pointer to y component of vector coordinates
384 * @param side_length glyph width/height.
385 */
386 6 static void make_glyphs(int8_t *pglyphs, const int8_t *xvec, const int8_t *yvec,
387 const int side_length)
388 {
389 6 const int glyph_size = side_length * side_length;
390 6 int8_t *pglyph = pglyphs;
391
392 int i, j;
393
2/2
✓ Branch 0 taken 96 times.
✓ Branch 1 taken 6 times.
102 for (i = 0; i < GLYPH_COORD_VECT_SIZE; i++) {
394 96 int x0 = xvec[i];
395 96 int y0 = yvec[i];
396 96 enum GlyphEdge edge0 = which_edge(x0, y0, side_length);
397
398
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) {
399 1536 int x1 = xvec[j];
400 1536 int y1 = yvec[j];
401 1536 enum GlyphEdge edge1 = which_edge(x1, y1, side_length);
402 1536 enum GlyphDir dir = which_direction(edge0, edge1);
403 1536 int npoints = FFMAX(FFABS(x1 - x0), FFABS(y1 - y0));
404 int ipoint;
405
406
2/2
✓ Branch 0 taken 6600 times.
✓ Branch 1 taken 1536 times.
8136 for (ipoint = 0; ipoint <= npoints; ipoint++) {
407 int8_t point[2];
408 int irow, icol;
409
410 6600 interp_point(point, x0, y0, x1, y1, ipoint, npoints);
411
412
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) {
413 2832 case DIR_UP:
414
2/2
✓ Branch 0 taken 8232 times.
✓ Branch 1 taken 2832 times.
11064 for (irow = point[1]; irow >= 0; irow--)
415 8232 pglyph[point[0] + irow * side_length] = 1;
416 2832 break;
417
418 1968 case DIR_DOWN:
419
2/2
✓ Branch 0 taken 4500 times.
✓ Branch 1 taken 1968 times.
6468 for (irow = point[1]; irow < side_length; irow++)
420 4500 pglyph[point[0] + irow * side_length] = 1;
421 1968 break;
422
423 282 case DIR_LEFT:
424
2/2
✓ Branch 0 taken 378 times.
✓ Branch 1 taken 282 times.
660 for (icol = point[0]; icol >= 0; icol--)
425 378 pglyph[icol + point[1] * side_length] = 1;
426 282 break;
427
428 1434 case DIR_RIGHT:
429
2/2
✓ Branch 0 taken 4794 times.
✓ Branch 1 taken 1434 times.
6228 for (icol = point[0]; icol < side_length; icol++)
430 4794 pglyph[icol + point[1] * side_length] = 1;
431 1434 break;
432 }
433 }
434 }
435 }
436 6 }
437
438 6 static void init_sizes(SANMVideoContext *ctx, int width, int height)
439 {
440 6 ctx->width = width;
441 6 ctx->height = height;
442 6 ctx->npixels = width * height;
443
444 6 ctx->aligned_width = FFALIGN(width, 8);
445 6 ctx->aligned_height = FFALIGN(height, 8);
446
447 6 ctx->buf_size = ctx->aligned_width * ctx->aligned_height * sizeof(ctx->frm0[0]);
448 6 ctx->pitch = width;
449 6 }
450
451 3 static void destroy_buffers(SANMVideoContext *ctx)
452 {
453 3 av_freep(&ctx->frm0);
454 3 av_freep(&ctx->frm1);
455 3 av_freep(&ctx->frm2);
456 3 av_freep(&ctx->stored_frame);
457 3 av_freep(&ctx->rle_buf);
458 3 ctx->frm0_size =
459 3 ctx->frm1_size =
460 3 ctx->frm2_size = 0;
461 3 init_sizes(ctx, 0, 0);
462 3 }
463
464 3 static av_cold int init_buffers(SANMVideoContext *ctx)
465 {
466 3 av_fast_padded_mallocz(&ctx->frm0, &ctx->frm0_size, ctx->buf_size);
467 3 av_fast_padded_mallocz(&ctx->frm1, &ctx->frm1_size, ctx->buf_size);
468 3 av_fast_padded_mallocz(&ctx->frm2, &ctx->frm2_size, ctx->buf_size);
469
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (!ctx->version)
470 av_fast_padded_mallocz(&ctx->stored_frame,
471 &ctx->stored_frame_size, ctx->buf_size);
472
473
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 ||
474
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)) {
475 destroy_buffers(ctx);
476 return AVERROR(ENOMEM);
477 }
478
479 3 return 0;
480 }
481
482 9 static void rotate_bufs(SANMVideoContext *ctx, int rotate_code)
483 {
484
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 8 times.
9 if (rotate_code == 2)
485 1 FFSWAP(uint16_t*, ctx->frm1, ctx->frm2);
486 9 FFSWAP(uint16_t*, ctx->frm2, ctx->frm0);
487 9 }
488
489 3 static av_cold int decode_init(AVCodecContext *avctx)
490 {
491 3 SANMVideoContext *ctx = avctx->priv_data;
492
493 3 ctx->avctx = avctx;
494 3 ctx->version = !avctx->extradata_size;
495 // early sanity check before allocations to avoid need for deallocation code.
496
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) {
497 av_log(avctx, AV_LOG_ERROR, "Not enough extradata.\n");
498 return AVERROR_INVALIDDATA;
499 }
500
501
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;
502
503 3 init_sizes(ctx, avctx->width, avctx->height);
504
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
3 if (init_buffers(ctx)) {
505 av_log(avctx, AV_LOG_ERROR, "Error allocating buffers.\n");
506 return AVERROR(ENOMEM);
507 }
508
509 3 make_glyphs(ctx->p4x4glyphs[0], glyph4_x, glyph4_y, 4);
510 3 make_glyphs(ctx->p8x8glyphs[0], glyph8_x, glyph8_y, 8);
511
512
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (!ctx->version) {
513 int i;
514
515 ctx->subversion = AV_RL16(avctx->extradata);
516 for (i = 0; i < PALETTE_SIZE; i++)
517 ctx->pal[i] = 0xFFU << 24 | AV_RL32(avctx->extradata + 2 + i * 4);
518 }
519
520 3 return 0;
521 }
522
523 3 static av_cold int decode_end(AVCodecContext *avctx)
524 {
525 3 SANMVideoContext *ctx = avctx->priv_data;
526
527 3 destroy_buffers(ctx);
528
529 3 return 0;
530 }
531
532 1 static int rle_decode(SANMVideoContext *ctx, uint8_t *dst, const int out_size)
533 {
534 1 int opcode, color, run_len, left = out_size;
535
536
2/2
✓ Branch 0 taken 4800 times.
✓ Branch 1 taken 1 times.
4801 while (left > 0) {
537 4800 opcode = bytestream2_get_byte(&ctx->gb);
538 4800 run_len = (opcode >> 1) + 1;
539
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(&ctx->gb) <= 0)
540 return AVERROR_INVALIDDATA;
541
542
1/2
✓ Branch 0 taken 4800 times.
✗ Branch 1 not taken.
4800 if (opcode & 1) {
543 4800 color = bytestream2_get_byte(&ctx->gb);
544 4800 memset(dst, color, run_len);
545 } else {
546 if (bytestream2_get_bytes_left(&ctx->gb) < run_len)
547 return AVERROR_INVALIDDATA;
548 bytestream2_get_bufferu(&ctx->gb, dst, run_len);
549 }
550
551 4800 dst += run_len;
552 4800 left -= run_len;
553 }
554
555 1 return 0;
556 }
557
558 static int old_codec1(SANMVideoContext *ctx, int top,
559 int left, int width, int height)
560 {
561 uint8_t *dst = ((uint8_t *)ctx->frm0) + left + top * ctx->pitch;
562 int i, j, len, flag, code, val, pos, end;
563
564 for (i = 0; i < height; i++) {
565 pos = 0;
566
567 if (bytestream2_get_bytes_left(&ctx->gb) < 2)
568 return AVERROR_INVALIDDATA;
569
570 len = bytestream2_get_le16u(&ctx->gb);
571 end = bytestream2_tell(&ctx->gb) + len;
572
573 while (bytestream2_tell(&ctx->gb) < end) {
574 if (bytestream2_get_bytes_left(&ctx->gb) < 2)
575 return AVERROR_INVALIDDATA;
576
577 code = bytestream2_get_byteu(&ctx->gb);
578 flag = code & 1;
579 code = (code >> 1) + 1;
580 if (pos + code > width)
581 return AVERROR_INVALIDDATA;
582 if (flag) {
583 val = bytestream2_get_byteu(&ctx->gb);
584 if (val)
585 memset(dst + pos, val, code);
586 pos += code;
587 } else {
588 if (bytestream2_get_bytes_left(&ctx->gb) < code)
589 return AVERROR_INVALIDDATA;
590 for (j = 0; j < code; j++) {
591 val = bytestream2_get_byteu(&ctx->gb);
592 if (val)
593 dst[pos] = val;
594 pos++;
595 }
596 }
597 }
598 dst += ctx->pitch;
599 }
600 ctx->rotate_code = 0;
601
602 return 0;
603 }
604
605 static inline void codec37_mv(uint8_t *dst, const uint8_t *src,
606 int height, int stride, int x, int y)
607 {
608 int pos, i, j;
609
610 pos = x + y * stride;
611 for (j = 0; j < 4; j++) {
612 for (i = 0; i < 4; i++) {
613 if ((pos + i) < 0 || (pos + i) >= height * stride)
614 dst[i] = 0;
615 else
616 dst[i] = src[i];
617 }
618 dst += stride;
619 src += stride;
620 pos += stride;
621 }
622 }
623
624 static int old_codec37(SANMVideoContext *ctx, int top,
625 int left, int width, int height)
626 {
627 int i, j, k, l, t, run, len, code, skip, mx, my;
628 ptrdiff_t stride = ctx->pitch;
629 uint8_t *dst, *prev;
630 int skip_run = 0;
631 int compr = bytestream2_get_byte(&ctx->gb);
632 int mvoff = bytestream2_get_byte(&ctx->gb);
633 int seq = bytestream2_get_le16(&ctx->gb);
634 uint32_t decoded_size = bytestream2_get_le32(&ctx->gb);
635 int flags;
636
637 bytestream2_skip(&ctx->gb, 4);
638 flags = bytestream2_get_byte(&ctx->gb);
639 bytestream2_skip(&ctx->gb, 3);
640
641 if (decoded_size > ctx->height * stride - left - top * stride) {
642 decoded_size = ctx->height * stride - left - top * stride;
643 av_log(ctx->avctx, AV_LOG_WARNING, "Decoded size is too large.\n");
644 }
645
646 ctx->rotate_code = 0;
647
648 if (((seq & 1) || !(flags & 1)) && (compr && compr != 2))
649 rotate_bufs(ctx, 1);
650
651 dst = ((uint8_t*)ctx->frm0) + left + top * stride;
652 prev = ((uint8_t*)ctx->frm2) + left + top * stride;
653
654 if (mvoff > 2) {
655 av_log(ctx->avctx, AV_LOG_ERROR, "Invalid motion base value %d.\n", mvoff);
656 return AVERROR_INVALIDDATA;
657 }
658
659 switch (compr) {
660 case 0:
661 for (i = 0; i < height; i++) {
662 bytestream2_get_buffer(&ctx->gb, dst, width);
663 dst += stride;
664 }
665 memset(ctx->frm1, 0, ctx->height * stride);
666 memset(ctx->frm2, 0, ctx->height * stride);
667 break;
668 case 1:
669 run = 0;
670 len = -1;
671 code = 0;
672
673 for (j = 0; j < height; j += 4) {
674 for (i = 0; i < width; i += 4) {
675 if (len < 0) {
676 if (bytestream2_get_bytes_left(&ctx->gb) < 1)
677 return AVERROR_INVALIDDATA;
678 code = bytestream2_get_byte(&ctx->gb);
679 len = code >> 1;
680 run = code & 1;
681 skip = 0;
682 } else {
683 skip = run;
684 }
685
686 if (!skip) {
687 if (bytestream2_get_bytes_left(&ctx->gb) < 1)
688 return AVERROR_INVALIDDATA;
689 code = bytestream2_get_byte(&ctx->gb);
690 if (code == 0xff) {
691 len--;
692 for (k = 0; k < 4; k++) {
693 for (l = 0; l < 4; l++) {
694 if (len < 0) {
695 if (bytestream2_get_bytes_left(&ctx->gb) < 1)
696 return AVERROR_INVALIDDATA;
697 code = bytestream2_get_byte(&ctx->gb);
698 len = code >> 1;
699 run = code & 1;
700 if (run) {
701 if (bytestream2_get_bytes_left(&ctx->gb) < 1)
702 return AVERROR_INVALIDDATA;
703 code = bytestream2_get_byte(&ctx->gb);
704 }
705 }
706 if (!run) {
707 if (bytestream2_get_bytes_left(&ctx->gb) < 1)
708 return AVERROR_INVALIDDATA;
709 code = bytestream2_get_byte(&ctx->gb);
710 }
711 *(dst + i + (k * stride) + l) = code;
712 len--;
713 }
714 }
715 continue;
716 }
717 }
718 /* 4x4 block copy from prev with MV */
719 mx = c37_mv[(mvoff * 255 + code) * 2];
720 my = c37_mv[(mvoff * 255 + code) * 2 + 1];
721 codec37_mv(dst + i, prev + i + mx + my * stride,
722 ctx->height, stride, i + mx, j + my);
723 len--;
724 }
725 dst += stride * 4;
726 prev += stride * 4;
727 }
728 break;
729 case 2:
730 if (rle_decode(ctx, dst, decoded_size))
731 return AVERROR_INVALIDDATA;
732 memset(ctx->frm1, 0, ctx->frm1_size);
733 memset(ctx->frm2, 0, ctx->frm2_size);
734 break;
735 case 3:
736 case 4:
737 for (j = 0; j < height; j += 4) {
738 for (i = 0; i < width; i += 4) {
739 int code;
740 if (skip_run) {
741 skip_run--;
742 copy_block4(dst + i, prev + i, stride, stride, 4);
743 continue;
744 }
745 if (bytestream2_get_bytes_left(&ctx->gb) < 1)
746 return AVERROR_INVALIDDATA;
747 code = bytestream2_get_byteu(&ctx->gb);
748 if (code == 0xFF) {
749 if (bytestream2_get_bytes_left(&ctx->gb) < 16)
750 return AVERROR_INVALIDDATA;
751 for (k = 0; k < 4; k++)
752 bytestream2_get_bufferu(&ctx->gb, dst + i + k * stride, 4);
753 } else if ((flags & 4) && (code == 0xFE)) {
754 if (bytestream2_get_bytes_left(&ctx->gb) < 4)
755 return AVERROR_INVALIDDATA;
756 for (k = 0; k < 4; k++)
757 memset(dst + i + k * stride, bytestream2_get_byteu(&ctx->gb), 4);
758 } else if ((flags & 4) && (code == 0xFD)) {
759 if (bytestream2_get_bytes_left(&ctx->gb) < 1)
760 return AVERROR_INVALIDDATA;
761 t = bytestream2_get_byteu(&ctx->gb);
762 for (k = 0; k < 4; k++)
763 memset(dst + i + k * stride, t, 4);
764 } else if ((compr == 4) && (code == 0)) {
765 if (bytestream2_get_bytes_left(&ctx->gb) < 1)
766 return AVERROR_INVALIDDATA;
767 skip_run = bytestream2_get_byteu(&ctx->gb) + 1;
768 i -= 4;
769 } else {
770 mx = c37_mv[(mvoff * 255 + code) * 2];
771 my = c37_mv[(mvoff * 255 + code) * 2 + 1];
772 codec37_mv(dst + i, prev + i + mx + my * stride,
773 ctx->height, stride, i + mx, j + my);
774 }
775 }
776 dst += stride * 4;
777 prev += stride * 4;
778 }
779 break;
780 default:
781 avpriv_report_missing_feature(ctx->avctx,
782 "Subcodec 37 compression %d", compr);
783 return AVERROR_PATCHWELCOME;
784 }
785
786 return 0;
787 }
788
789 static int process_block(SANMVideoContext *ctx, uint8_t *dst, uint8_t *prev1,
790 uint8_t *prev2, int stride, int tbl, int size)
791 {
792 int code, k, t;
793 uint8_t colors[2];
794 int8_t *pglyph;
795
796 if (bytestream2_get_bytes_left(&ctx->gb) < 1)
797 return AVERROR_INVALIDDATA;
798
799 code = bytestream2_get_byteu(&ctx->gb);
800 if (code >= 0xF8) {
801 switch (code) {
802 case 0xFF:
803 if (size == 2) {
804 if (bytestream2_get_bytes_left(&ctx->gb) < 4)
805 return AVERROR_INVALIDDATA;
806 dst[0] = bytestream2_get_byteu(&ctx->gb);
807 dst[1] = bytestream2_get_byteu(&ctx->gb);
808 dst[0 + stride] = bytestream2_get_byteu(&ctx->gb);
809 dst[1 + stride] = bytestream2_get_byteu(&ctx->gb);
810 } else {
811 size >>= 1;
812 if (process_block(ctx, dst, prev1, prev2, stride, tbl, size))
813 return AVERROR_INVALIDDATA;
814 if (process_block(ctx, dst + size, prev1 + size, prev2 + size,
815 stride, tbl, size))
816 return AVERROR_INVALIDDATA;
817 dst += size * stride;
818 prev1 += size * stride;
819 prev2 += size * stride;
820 if (process_block(ctx, dst, prev1, prev2, stride, tbl, size))
821 return AVERROR_INVALIDDATA;
822 if (process_block(ctx, dst + size, prev1 + size, prev2 + size,
823 stride, tbl, size))
824 return AVERROR_INVALIDDATA;
825 }
826 break;
827 case 0xFE:
828 if (bytestream2_get_bytes_left(&ctx->gb) < 1)
829 return AVERROR_INVALIDDATA;
830
831 t = bytestream2_get_byteu(&ctx->gb);
832 for (k = 0; k < size; k++)
833 memset(dst + k * stride, t, size);
834 break;
835 case 0xFD:
836 if (bytestream2_get_bytes_left(&ctx->gb) < 3)
837 return AVERROR_INVALIDDATA;
838
839 code = bytestream2_get_byteu(&ctx->gb);
840 pglyph = (size == 8) ? ctx->p8x8glyphs[code] : ctx->p4x4glyphs[code];
841 bytestream2_get_bufferu(&ctx->gb, colors, 2);
842
843 for (k = 0; k < size; k++)
844 for (t = 0; t < size; t++)
845 dst[t + k * stride] = colors[!*pglyph++];
846 break;
847 case 0xFC:
848 for (k = 0; k < size; k++)
849 memcpy(dst + k * stride, prev1 + k * stride, size);
850 break;
851 default:
852 k = bytestream2_tell(&ctx->gb);
853 bytestream2_seek(&ctx->gb, tbl + (code & 7), SEEK_SET);
854 t = bytestream2_get_byte(&ctx->gb);
855 bytestream2_seek(&ctx->gb, k, SEEK_SET);
856 for (k = 0; k < size; k++)
857 memset(dst + k * stride, t, size);
858 }
859 } else {
860 int mx = motion_vectors[code][0];
861 int my = motion_vectors[code][1];
862 int index = prev2 - (const uint8_t *)ctx->frm2;
863
864 av_assert2(index >= 0 && index < (ctx->buf_size >> 1));
865
866 if (index < -mx - my * stride ||
867 (ctx->buf_size >> 1) - index < mx + size + (my + size - 1) * stride) {
868 av_log(ctx->avctx, AV_LOG_ERROR, "MV is invalid.\n");
869 return AVERROR_INVALIDDATA;
870 }
871
872 for (k = 0; k < size; k++)
873 memcpy(dst + k * stride, prev2 + mx + (my + k) * stride, size);
874 }
875
876 return 0;
877 }
878
879 static void codec47_read_interptable(SANMVideoContext *ctx)
880 {
881 uint8_t *p1, *p2, *itbl = ctx->c47itbl;
882 int i, j;
883
884 for (i = 0; i < 256; i++) {
885 p1 = p2 = itbl + i;
886 for (j = 256 - i; j; j--) {
887 *p1 = *p2 = bytestream2_get_byte(&ctx->gb);
888 p1 += 1;
889 p2 += 256;
890 }
891 itbl += 256;
892 }
893 }
894
895 static void codec47_comp1(SANMVideoContext *ctx, uint8_t *dst_in, int width,
896 int height, ptrdiff_t stride)
897 {
898 uint8_t p1, *dst, *itbl = ctx->c47itbl;
899 uint16_t px;
900 int i, j;
901
902 dst = dst_in + stride;
903 for (i = 0; i < height; i += 2) {
904 p1 = bytestream2_get_byte(&ctx->gb);
905 *dst++ = p1;
906 *dst++ = p1;
907 px = p1;
908 for (j = 2; j < width; j += 2) {
909 p1 = bytestream2_get_byte(&ctx->gb);
910 px = (px << 8) | p1;
911 *dst++ = itbl[px];
912 *dst++ = p1;
913 }
914 dst += stride;
915 }
916
917 memcpy(dst_in, dst_in + stride, width);
918 dst = dst_in + stride + stride;
919 for (i = 2; i < height - 1; i += 2) {
920 for (j = 0; j < width; j++) {
921 px = (*(dst - stride) << 8) | *(dst + stride);
922 *dst++ = itbl[px];
923 }
924 dst += stride;
925 }
926 }
927
928 static int old_codec47(SANMVideoContext *ctx, int top,
929 int left, int width, int height)
930 {
931 uint32_t decoded_size;
932 int i, j;
933 ptrdiff_t stride = ctx->pitch;
934 uint8_t *dst = (uint8_t *)ctx->frm0 + left + top * stride;
935 uint8_t *prev1 = (uint8_t *)ctx->frm1 + left + top * stride;
936 uint8_t *prev2 = (uint8_t *)ctx->frm2 + left + top * stride;
937 uint8_t auxcol[2];
938 int tbl_pos = bytestream2_tell(&ctx->gb);
939 int seq = bytestream2_get_le16(&ctx->gb);
940 int compr = bytestream2_get_byte(&ctx->gb);
941 int new_rot = bytestream2_get_byte(&ctx->gb);
942 int skip = bytestream2_get_byte(&ctx->gb);
943
944 bytestream2_skip(&ctx->gb, 7);
945 auxcol[0] = bytestream2_get_byteu(&ctx->gb);
946 auxcol[1] = bytestream2_get_byteu(&ctx->gb);
947 decoded_size = bytestream2_get_le32(&ctx->gb);
948 bytestream2_skip(&ctx->gb, 8);
949
950 if (decoded_size > ctx->height * stride - left - top * stride) {
951 decoded_size = ctx->height * stride - left - top * stride;
952 av_log(ctx->avctx, AV_LOG_WARNING, "Decoded size is too large.\n");
953 }
954
955 if (skip & 1) {
956 if (bytestream2_get_bytes_left(&ctx->gb) < 0x8080)
957 return AVERROR_INVALIDDATA;
958 codec47_read_interptable(ctx);
959 }
960 if (!seq) {
961 ctx->prev_seq = -1;
962 memset(prev1, auxcol[0], (ctx->height - top) * stride);
963 memset(prev2, auxcol[1], (ctx->height - top) * stride);
964 }
965
966 switch (compr) {
967 case 0:
968 if (bytestream2_get_bytes_left(&ctx->gb) < width * height)
969 return AVERROR_INVALIDDATA;
970 for (j = 0; j < height; j++) {
971 bytestream2_get_bufferu(&ctx->gb, dst, width);
972 dst += stride;
973 }
974 break;
975 case 1:
976 if (bytestream2_get_bytes_left(&ctx->gb) < ((width + 1) >> 1) * ((height + 1) >> 1))
977 return AVERROR_INVALIDDATA;
978 codec47_comp1(ctx, dst, width, height, stride);
979 break;
980 case 2:
981 if (seq == ctx->prev_seq + 1) {
982 for (j = 0; j < height; j += 8) {
983 for (i = 0; i < width; i += 8)
984 if (process_block(ctx, dst + i, prev1 + i, prev2 + i, stride,
985 tbl_pos + 8, 8))
986 return AVERROR_INVALIDDATA;
987 dst += stride * 8;
988 prev1 += stride * 8;
989 prev2 += stride * 8;
990 }
991 }
992 break;
993 case 3:
994 memcpy(ctx->frm0, ctx->frm2, ctx->pitch * ctx->height);
995 break;
996 case 4:
997 memcpy(ctx->frm0, ctx->frm1, ctx->pitch * ctx->height);
998 break;
999 case 5:
1000 if (rle_decode(ctx, dst, decoded_size))
1001 return AVERROR_INVALIDDATA;
1002 break;
1003 default:
1004 avpriv_report_missing_feature(ctx->avctx,
1005 "Subcodec 47 compression %d", compr);
1006 return AVERROR_PATCHWELCOME;
1007 }
1008 if (seq == ctx->prev_seq + 1)
1009 ctx->rotate_code = new_rot;
1010 else
1011 ctx->rotate_code = 0;
1012 ctx->prev_seq = seq;
1013
1014 return 0;
1015 }
1016
1017 // scale 4x4 input block to an 8x8 output block
1018 static void c48_4to8(uint8_t *dst, const uint8_t *src, const uint16_t w)
1019 {
1020 uint16_t p;
1021 // dst is always at least 16bit aligned
1022 for (int i = 0; i < 4; i++) {
1023 for (int j = 0; j < 8; j += 2) {
1024 p = *src++;
1025 p = (p << 8) | p;
1026 *((uint16_t *)(dst + w * 0 + j)) = p;
1027 *((uint16_t *)(dst + w * 1 + j)) = p;
1028 }
1029 dst += w * 2;
1030 }
1031 }
1032
1033 static int codec48_block(SANMVideoContext *ctx, uint8_t *dst, uint8_t *db,
1034 const uint16_t w)
1035 {
1036 uint8_t opc, sb[16];
1037 int i, j, k, l;
1038 int16_t mvofs;
1039 uint32_t ofs;
1040
1041 if (bytestream2_get_bytes_left(&ctx->gb) < 1)
1042 return 1;
1043
1044 opc = bytestream2_get_byteu(&ctx->gb);
1045 switch (opc) {
1046 case 0xFF: // 1x1 -> 8x8 block scale
1047 if (bytestream2_get_bytes_left(&ctx->gb) < 1)
1048 return 1;
1049
1050 opc = bytestream2_get_byteu(&ctx->gb);
1051 for (i = 0; i < 16; i++)
1052 sb[i] = opc;
1053 c48_4to8(dst, sb, w);
1054 break;
1055 case 0xFE: // 1x 8x8 copy from deltabuf, 16bit mv from source
1056 if (bytestream2_get_bytes_left(&ctx->gb) < 2)
1057 return 1;
1058 mvofs = bytestream2_get_le16(&ctx->gb);
1059 for (i = 0; i < 8; i++) {
1060 ofs = w * i;
1061 for (k = 0; k < 8; k++)
1062 *(dst + ofs + k) = *(db + ofs + k + mvofs);
1063 }
1064 break;
1065 case 0xFD: // 2x2 -> 8x8 block scale
1066 if (bytestream2_get_bytes_left(&ctx->gb) < 4)
1067 return 1;
1068 sb[ 5] = bytestream2_get_byteu(&ctx->gb);
1069 sb[ 7] = bytestream2_get_byteu(&ctx->gb);
1070 sb[13] = bytestream2_get_byteu(&ctx->gb);
1071 sb[15] = bytestream2_get_byteu(&ctx->gb);
1072
1073 sb[0] = sb[1] = sb[4] = sb[5];
1074 sb[2] = sb[3] = sb[6] = sb[7];
1075 sb[8] = sb[9] = sb[12] = sb[13];
1076 sb[10] = sb[11] = sb[14] = sb[15];
1077 c48_4to8(dst, sb, w);
1078 break;
1079 case 0xFC: // 4x copy 4x4 block, per-block c37_mv from source
1080 if (bytestream2_get_bytes_left(&ctx->gb) < 4)
1081 return 1;
1082 for (i = 0; i < 8; i += 4) {
1083 for (k = 0; k < 8; k += 4) {
1084 opc = bytestream2_get_byteu(&ctx->gb);
1085 mvofs = c37_mv[opc * 2] + (c37_mv[opc * 2 + 1] * w);
1086 for (j = 0; j < 4; j++) {
1087 ofs = (w * (j + i)) + k;
1088 for (l = 0; l < 4; l++)
1089 *(dst + ofs + l) = *(db + ofs + l + mvofs);
1090 }
1091 }
1092 }
1093 break;
1094 case 0xFB: // Copy 4x 4x4 blocks, per-block mv from source
1095 if (bytestream2_get_bytes_left(&ctx->gb) < 8)
1096 return 1;
1097 for (i = 0; i < 8; i += 4) {
1098 for (k = 0; k < 8; k += 4) {
1099 mvofs = bytestream2_get_le16(&ctx->gb);
1100 for (j = 0; j < 4; j++) {
1101 ofs = (w * (j + i)) + k;
1102 for (l = 0; l < 4; l++)
1103 *(dst + ofs + l) = *(db + ofs + l + mvofs);
1104 }
1105 }
1106 }
1107 break;
1108 case 0xFA: // scale 4x4 input block to 8x8 dest block
1109 if (bytestream2_get_bytes_left(&ctx->gb) < 16)
1110 return 1;
1111 bytestream2_get_bufferu(&ctx->gb, sb, 16);
1112 c48_4to8(dst, sb, w);
1113 break;
1114 case 0xF9: // 16x 2x2 copy from delta, per-block c37_mv from source
1115 if (bytestream2_get_bytes_left(&ctx->gb) < 16)
1116 return 1;
1117 for (i = 0; i < 8; i += 2) {
1118 for (j = 0; j < 8; j += 2) {
1119 ofs = (w * i) + j;
1120 opc = bytestream2_get_byteu(&ctx->gb);
1121 mvofs = c37_mv[opc * 2] + (c37_mv[opc * 2 + 1] * w);
1122 for (l = 0; l < 2; l++) {
1123 *(dst + ofs + l + 0) = *(db + ofs + l + 0 + mvofs);
1124 *(dst + ofs + l + w) = *(db + ofs + l + w + mvofs);
1125 }
1126 }
1127 }
1128 break;
1129 case 0xF8: // 16x 2x2 blocks copy, 16bit mv from source
1130 if (bytestream2_get_bytes_left(&ctx->gb) < 32)
1131 return 1;
1132 for (i = 0; i < 8; i += 2) {
1133 for (j = 0; j < 8; j += 2) {
1134 ofs = w * i + j;
1135 mvofs = bytestream2_get_le16(&ctx->gb);
1136 for (l = 0; l < 2; l++) {
1137 *(dst + ofs + l + 0) = *(db + ofs + l + 0 + mvofs);
1138 *(dst + ofs + l + w) = *(db + ofs + l + w + mvofs);
1139 }
1140 }
1141 }
1142 break;
1143 case 0xF7: // copy 8x8 block from src to dest
1144 if (bytestream2_get_bytes_left(&ctx->gb) < 64)
1145 return 1;
1146 for (i = 0; i < 8; i++) {
1147 ofs = i * w;
1148 for (l = 0; l < 8; l++)
1149 *(dst + ofs + l) = bytestream2_get_byteu(&ctx->gb);
1150 }
1151 break;
1152 default: // copy 8x8 block from prev, c37_mv from source
1153 mvofs = c37_mv[opc * 2] + (c37_mv[opc * 2 + 1] * w);
1154 for (i = 0; i < 8; i++) {
1155 ofs = i * w;
1156 for (l = 0; l < 8; l++)
1157 *(dst + ofs + l) = *(db + ofs + l + mvofs);
1158 }
1159 break;
1160 }
1161 return 0;
1162 }
1163
1164 static int old_codec48(SANMVideoContext *ctx, int width, int height)
1165 {
1166 uint8_t *dst, *prev;
1167 int compr = bytestream2_get_byte(&ctx->gb);
1168 int mvidx = bytestream2_get_byte(&ctx->gb);
1169 int seq = bytestream2_get_le16(&ctx->gb);
1170 uint32_t decoded_size = bytestream2_get_le32(&ctx->gb);
1171 int i, j, flags;
1172
1173 // all codec48 videos use 1, but just to be safe...
1174 if (mvidx != 1) {
1175 av_log(ctx->avctx, AV_LOG_ERROR, "Invalid motion base value %d.\n", mvidx);
1176 return AVERROR_INVALIDDATA;
1177 }
1178
1179 bytestream2_skip(&ctx->gb, 4);
1180 flags = bytestream2_get_byte(&ctx->gb);
1181 bytestream2_skip(&ctx->gb, 3);
1182
1183 if (flags & 8) {
1184 if (bytestream2_get_bytes_left(&ctx->gb) < 0x8080)
1185 return AVERROR_INVALIDDATA;
1186 codec47_read_interptable(ctx);
1187 }
1188
1189 dst = (uint8_t*)ctx->frm0;
1190 prev = (uint8_t*)ctx->frm2;
1191
1192 if (!seq) {
1193 ctx->prev_seq = -1;
1194 memset(prev, 0, ctx->aligned_height * width);
1195 }
1196
1197 switch (compr) {
1198 case 0:
1199 if (bytestream2_get_bytes_left(&ctx->gb) < width * height)
1200 return AVERROR_INVALIDDATA;
1201 for (j = 0; j < height; j++) {
1202 bytestream2_get_bufferu(&ctx->gb, dst, width);
1203 dst += width;
1204 }
1205 break;
1206 case 2:
1207 if (rle_decode(ctx, dst, decoded_size))
1208 return AVERROR_INVALIDDATA;
1209 break;
1210 case 3:
1211 if (seq == ctx->prev_seq + 1) {
1212 for (j = 0; j < height; j += 8) {
1213 for (i = 0; i < width; i += 8) {
1214 if (codec48_block(ctx, dst + i, prev + i, width))
1215 return AVERROR_INVALIDDATA;
1216 }
1217 dst += width * 8;
1218 prev += width * 8;
1219 }
1220 }
1221 break;
1222 case 5:
1223 if (bytestream2_get_bytes_left(&ctx->gb) < ((width + 1) >> 1) * ((height + 1) >> 1))
1224 return AVERROR_INVALIDDATA;
1225 codec47_comp1(ctx, dst, width, height, width);
1226 break;
1227
1228 default:
1229 avpriv_report_missing_feature(ctx->avctx,
1230 "Subcodec 48 compression %d", compr);
1231 return AVERROR_PATCHWELCOME;
1232 }
1233 ctx->rotate_code = 1; // swap frm[0] and frm[2]
1234 ctx->prev_seq = seq;
1235 return 0;
1236 }
1237
1238 static int process_frame_obj(SANMVideoContext *ctx)
1239 {
1240 uint16_t codec = bytestream2_get_le16u(&ctx->gb);
1241 uint16_t left = bytestream2_get_le16u(&ctx->gb);
1242 uint16_t top = bytestream2_get_le16u(&ctx->gb);
1243 uint16_t w = bytestream2_get_le16u(&ctx->gb);
1244 uint16_t h = bytestream2_get_le16u(&ctx->gb);
1245
1246 if (!w || !h) {
1247 av_log(ctx->avctx, AV_LOG_ERROR, "Dimensions are invalid.\n");
1248 return AVERROR_INVALIDDATA;
1249 }
1250
1251 if (ctx->width < left + w || ctx->height < top + h) {
1252 int ret = ff_set_dimensions(ctx->avctx, FFMAX(left + w, ctx->width),
1253 FFMAX(top + h, ctx->height));
1254 if (ret < 0)
1255 return ret;
1256 init_sizes(ctx, FFMAX(left + w, ctx->width),
1257 FFMAX(top + h, ctx->height));
1258 if (init_buffers(ctx)) {
1259 av_log(ctx->avctx, AV_LOG_ERROR, "Error resizing buffers.\n");
1260 return AVERROR(ENOMEM);
1261 }
1262 }
1263 bytestream2_skip(&ctx->gb, 4);
1264
1265 switch (codec) {
1266 case 1:
1267 case 3:
1268 return old_codec1(ctx, top, left, w, h);
1269 case 37:
1270 return old_codec37(ctx, top, left, w, h);
1271 case 47:
1272 return old_codec47(ctx, top, left, w, h);
1273 case 48:
1274 return old_codec48(ctx, w, h);
1275 default:
1276 avpriv_request_sample(ctx->avctx, "Subcodec %d", codec);
1277 return AVERROR_PATCHWELCOME;
1278 }
1279 }
1280
1281 static int process_xpal(SANMVideoContext *ctx, int size)
1282 {
1283 int16_t *dp = ctx->delta_pal;
1284 uint32_t *pal = ctx->pal;
1285 uint16_t cmd;
1286 uint8_t c[3];
1287 int i, j;
1288
1289 bytestream2_skip(&ctx->gb, 2);
1290 cmd = bytestream2_get_be16(&ctx->gb);
1291
1292 if (cmd == 1) {
1293 for (i = 0; i < PALETTE_DELTA; i += 3) {
1294 c[0] = (*pal >> 16) & 0xFF;
1295 c[1] = (*pal >> 8) & 0xFF;
1296 c[2] = (*pal >> 0) & 0xFF;
1297 for (j = 0; j < 3; j++) {
1298 int cl = (c[j] * 129) + *dp++;
1299 c[j] = av_clip_uint8(cl / 128) & 0xFF;
1300 }
1301 *pal++ = 0xFFU << 24 | c[0] << 16 | c[1] << 8 | c[2];
1302 }
1303 } else if (cmd == 2) {
1304 if (size < PALETTE_DELTA * 2 + 4) {
1305 av_log(ctx->avctx, AV_LOG_ERROR,
1306 "Incorrect palette change block size %"PRIu32".\n", size);
1307 return AVERROR_INVALIDDATA;
1308 }
1309 for (i = 0; i < PALETTE_DELTA; i++)
1310 dp[i] = bytestream2_get_le16u(&ctx->gb);
1311
1312 if (size >= PALETTE_DELTA * 2 + 4 + PALETTE_SIZE * 3) {
1313 for (i = 0; i < PALETTE_SIZE; i++)
1314 ctx->pal[i] = 0xFFU << 24 | bytestream2_get_be24u(&ctx->gb);
1315 }
1316 }
1317 return 0;
1318 }
1319
1320 static int decode_0(SANMVideoContext *ctx)
1321 {
1322 uint16_t *frm = ctx->frm0;
1323 int x, y;
1324
1325 if (bytestream2_get_bytes_left(&ctx->gb) < ctx->width * ctx->height * 2) {
1326 av_log(ctx->avctx, AV_LOG_ERROR, "Insufficient data for raw frame.\n");
1327 return AVERROR_INVALIDDATA;
1328 }
1329 for (y = 0; y < ctx->height; y++) {
1330 for (x = 0; x < ctx->width; x++)
1331 frm[x] = bytestream2_get_le16u(&ctx->gb);
1332 frm += ctx->pitch;
1333 }
1334 return 0;
1335 }
1336
1337 static int decode_nop(SANMVideoContext *ctx)
1338 {
1339 avpriv_request_sample(ctx->avctx, "Unknown/unsupported compression type");
1340 return AVERROR_PATCHWELCOME;
1341 }
1342
1343 42964 static void copy_block(uint16_t *pdest, uint16_t *psrc, int block_size, ptrdiff_t pitch)
1344 {
1345 42964 uint8_t *dst = (uint8_t *)pdest;
1346 42964 uint8_t *src = (uint8_t *)psrc;
1347 42964 ptrdiff_t stride = pitch * 2;
1348
1349
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) {
1350 14526 case 2:
1351 14526 copy_block4(dst, src, stride, stride, 2);
1352 14526 break;
1353 12400 case 4:
1354 12400 copy_block8(dst, src, stride, stride, 4);
1355 12400 break;
1356 16038 case 8:
1357 16038 copy_block16(dst, src, stride, stride, 8);
1358 16038 break;
1359 }
1360 42964 }
1361
1362 7913 static void fill_block(uint16_t *pdest, uint16_t color, int block_size, ptrdiff_t pitch)
1363 {
1364 int x, y;
1365
1366 7913 pitch -= block_size;
1367
2/2
✓ Branch 0 taken 30438 times.
✓ Branch 1 taken 7913 times.
38351 for (y = 0; y < block_size; y++, pdest += pitch)
1368
2/2
✓ Branch 0 taken 149540 times.
✓ Branch 1 taken 30438 times.
179978 for (x = 0; x < block_size; x++)
1369 149540 *pdest++ = color;
1370 7913 }
1371
1372 8368 static int draw_glyph(SANMVideoContext *ctx, uint16_t *dst, int index,
1373 uint16_t fg_color, uint16_t bg_color, int block_size,
1374 ptrdiff_t pitch)
1375 {
1376 int8_t *pglyph;
1377 8368 uint16_t colors[2] = { fg_color, bg_color };
1378 int x, y;
1379
1380
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8368 times.
8368 if (index >= NGLYPHS) {
1381 av_log(ctx->avctx, AV_LOG_ERROR, "Ignoring nonexistent glyph #%u.\n", index);
1382 return AVERROR_INVALIDDATA;
1383 }
1384
1385
2/2
✓ Branch 0 taken 1158 times.
✓ Branch 1 taken 7210 times.
8368 pglyph = block_size == 8 ? ctx->p8x8glyphs[index] : ctx->p4x4glyphs[index];
1386 8368 pitch -= block_size;
1387
1388
2/2
✓ Branch 0 taken 38104 times.
✓ Branch 1 taken 8368 times.
46472 for (y = 0; y < block_size; y++, dst += pitch)
1389
2/2
✓ Branch 0 taken 189472 times.
✓ Branch 1 taken 38104 times.
227576 for (x = 0; x < block_size; x++)
1390 189472 *dst++ = colors[*pglyph++];
1391 8368 return 0;
1392 }
1393
1394 8029 static int opcode_0xf7(SANMVideoContext *ctx, int cx, int cy, int block_size, ptrdiff_t pitch)
1395 {
1396 8029 uint16_t *dst = ctx->frm0 + cx + cy * ctx->pitch;
1397
1398
2/2
✓ Branch 0 taken 825 times.
✓ Branch 1 taken 7204 times.
8029 if (block_size == 2) {
1399 uint32_t indices;
1400
1401
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 825 times.
825 if (bytestream2_get_bytes_left(&ctx->gb) < 4)
1402 return AVERROR_INVALIDDATA;
1403
1404 825 indices = bytestream2_get_le32u(&ctx->gb);
1405 825 dst[0] = ctx->codebook[indices & 0xFF];
1406 825 indices >>= 8;
1407 825 dst[1] = ctx->codebook[indices & 0xFF];
1408 825 indices >>= 8;
1409 825 dst[pitch] = ctx->codebook[indices & 0xFF];
1410 825 indices >>= 8;
1411 825 dst[pitch + 1] = ctx->codebook[indices & 0xFF];
1412 } else {
1413 uint16_t fgcolor, bgcolor;
1414 int glyph;
1415
1416
2/2
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 7203 times.
7204 if (bytestream2_get_bytes_left(&ctx->gb) < 3)
1417 1 return AVERROR_INVALIDDATA;
1418
1419 7203 glyph = bytestream2_get_byteu(&ctx->gb);
1420 7203 bgcolor = ctx->codebook[bytestream2_get_byteu(&ctx->gb)];
1421 7203 fgcolor = ctx->codebook[bytestream2_get_byteu(&ctx->gb)];
1422
1423 7203 draw_glyph(ctx, dst, glyph, fgcolor, bgcolor, block_size, pitch);
1424 }
1425 8028 return 0;
1426 }
1427
1428 1169 static int opcode_0xf8(SANMVideoContext *ctx, int cx, int cy, int block_size, ptrdiff_t pitch)
1429 {
1430 1169 uint16_t *dst = ctx->frm0 + cx + cy * ctx->pitch;
1431
1432
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 1165 times.
1169 if (block_size == 2) {
1433
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
4 if (bytestream2_get_bytes_left(&ctx->gb) < 8)
1434 return AVERROR_INVALIDDATA;
1435
1436 4 dst[0] = bytestream2_get_le16u(&ctx->gb);
1437 4 dst[1] = bytestream2_get_le16u(&ctx->gb);
1438 4 dst[pitch] = bytestream2_get_le16u(&ctx->gb);
1439 4 dst[pitch + 1] = bytestream2_get_le16u(&ctx->gb);
1440 } else {
1441 uint16_t fgcolor, bgcolor;
1442 int glyph;
1443
1444
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1165 times.
1165 if (bytestream2_get_bytes_left(&ctx->gb) < 5)
1445 return AVERROR_INVALIDDATA;
1446
1447 1165 glyph = bytestream2_get_byteu(&ctx->gb);
1448 1165 bgcolor = bytestream2_get_le16u(&ctx->gb);
1449 1165 fgcolor = bytestream2_get_le16u(&ctx->gb);
1450
1451 1165 draw_glyph(ctx, dst, glyph, fgcolor, bgcolor, block_size, pitch);
1452 }
1453 1169 return 0;
1454 }
1455
1456 30376 static int good_mvec(SANMVideoContext *ctx, int cx, int cy, int mx, int my,
1457 int block_size)
1458 {
1459 30376 int start_pos = cx + mx + (cy + my) * ctx->pitch;
1460 30376 int end_pos = start_pos + (block_size - 1) * (ctx->pitch + 1);
1461
1462
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);
1463
1464
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 30376 times.
30376 if (!good)
1465 av_log(ctx->avctx, AV_LOG_ERROR,
1466 "Ignoring invalid motion vector (%i, %i)->(%u, %u), block size = %u\n",
1467 cx + mx, cy + my, cx, cy, block_size);
1468
1469 30376 return good;
1470 }
1471
1472 71636 static int codec2subblock(SANMVideoContext *ctx, int cx, int cy, int blk_size)
1473 {
1474 int16_t mx, my, index;
1475 int opcode;
1476
1477
2/2
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 71635 times.
71636 if (bytestream2_get_bytes_left(&ctx->gb) < 1)
1478 1 return AVERROR_INVALIDDATA;
1479
1480 71635 opcode = bytestream2_get_byteu(&ctx->gb);
1481
1482
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) {
1483 28212 default:
1484 28212 mx = motion_vectors[opcode][0];
1485 28212 my = motion_vectors[opcode][1];
1486
1487
1/2
✓ Branch 1 taken 28212 times.
✗ Branch 2 not taken.
28212 if (good_mvec(ctx, cx, cy, mx, my, blk_size)) {
1488 28212 copy_block(ctx->frm0 + cx + ctx->pitch * cy,
1489 28212 ctx->frm2 + cx + mx + ctx->pitch * (cy + my),
1490 blk_size, ctx->pitch);
1491 }
1492 28212 break;
1493 2164 case 0xF5:
1494
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2164 times.
2164 if (bytestream2_get_bytes_left(&ctx->gb) < 2)
1495 return AVERROR_INVALIDDATA;
1496 2164 index = bytestream2_get_le16u(&ctx->gb);
1497
1498 2164 mx = index % ctx->width;
1499 2164 my = index / ctx->width;
1500
1501
1/2
✓ Branch 1 taken 2164 times.
✗ Branch 2 not taken.
2164 if (good_mvec(ctx, cx, cy, mx, my, blk_size)) {
1502 2164 copy_block(ctx->frm0 + cx + ctx->pitch * cy,
1503 2164 ctx->frm2 + cx + mx + ctx->pitch * (cy + my),
1504 blk_size, ctx->pitch);
1505 }
1506 2164 break;
1507 12588 case 0xF6:
1508 12588 copy_block(ctx->frm0 + cx + ctx->pitch * cy,
1509 12588 ctx->frm1 + cx + ctx->pitch * cy,
1510 blk_size, ctx->pitch);
1511 12588 break;
1512 8029 case 0xF7:
1513 8029 opcode_0xf7(ctx, cx, cy, blk_size, ctx->pitch);
1514 8029 break;
1515
1516 1169 case 0xF8:
1517 1169 opcode_0xf8(ctx, cx, cy, blk_size, ctx->pitch);
1518 1169 break;
1519 315 case 0xF9:
1520 case 0xFA:
1521 case 0xFB:
1522 case 0xFC:
1523 315 fill_block(ctx->frm0 + cx + cy * ctx->pitch,
1524 315 ctx->small_codebook[opcode - 0xf9], blk_size, ctx->pitch);
1525 315 break;
1526 5496 case 0xFD:
1527
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 5496 times.
5496 if (bytestream2_get_bytes_left(&ctx->gb) < 1)
1528 return AVERROR_INVALIDDATA;
1529 10992 fill_block(ctx->frm0 + cx + cy * ctx->pitch,
1530 5496 ctx->codebook[bytestream2_get_byteu(&ctx->gb)], blk_size, ctx->pitch);
1531 5496 break;
1532 2102 case 0xFE:
1533
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2102 times.
2102 if (bytestream2_get_bytes_left(&ctx->gb) < 2)
1534 return AVERROR_INVALIDDATA;
1535 2102 fill_block(ctx->frm0 + cx + cy * ctx->pitch,
1536 2102 bytestream2_get_le16u(&ctx->gb), blk_size, ctx->pitch);
1537 2102 break;
1538 11560 case 0xFF:
1539
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11560 times.
11560 if (blk_size == 2) {
1540 opcode_0xf8(ctx, cx, cy, blk_size, ctx->pitch);
1541 } else {
1542 11560 blk_size >>= 1;
1543
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 11560 times.
11560 if (codec2subblock(ctx, cx, cy, blk_size))
1544 return AVERROR_INVALIDDATA;
1545
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 11560 times.
11560 if (codec2subblock(ctx, cx + blk_size, cy, blk_size))
1546 return AVERROR_INVALIDDATA;
1547
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 11560 times.
11560 if (codec2subblock(ctx, cx, cy + blk_size, blk_size))
1548 return AVERROR_INVALIDDATA;
1549
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 11560 times.
11560 if (codec2subblock(ctx, cx + blk_size, cy + blk_size, blk_size))
1550 return AVERROR_INVALIDDATA;
1551 }
1552 11560 break;
1553 }
1554 71635 return 0;
1555 }
1556
1557 6 static int decode_2(SANMVideoContext *ctx)
1558 {
1559 int cx, cy, ret;
1560
1561
2/2
✓ Branch 0 taken 318 times.
✓ Branch 1 taken 5 times.
323 for (cy = 0; cy < ctx->aligned_height; cy += 8)
1562
2/2
✓ Branch 0 taken 25396 times.
✓ Branch 1 taken 317 times.
25713 for (cx = 0; cx < ctx->aligned_width; cx += 8)
1563
2/2
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 25395 times.
25396 if (ret = codec2subblock(ctx, cx, cy, 8))
1564 1 return ret;
1565
1566 5 return 0;
1567 }
1568
1569 static int decode_3(SANMVideoContext *ctx)
1570 {
1571 memcpy(ctx->frm0, ctx->frm2, ctx->frm2_size);
1572 return 0;
1573 }
1574
1575 9 static int decode_4(SANMVideoContext *ctx)
1576 {
1577 9 memcpy(ctx->frm0, ctx->frm1, ctx->frm1_size);
1578 9 return 0;
1579 }
1580
1581 1 static int decode_5(SANMVideoContext *ctx)
1582 {
1583 #if HAVE_BIGENDIAN
1584 uint16_t *frm;
1585 int npixels;
1586 #endif
1587 1 uint8_t *dst = (uint8_t*)ctx->frm0;
1588
1589
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 if (rle_decode(ctx, dst, ctx->buf_size))
1590 return AVERROR_INVALIDDATA;
1591
1592 #if HAVE_BIGENDIAN
1593 npixels = ctx->npixels;
1594 frm = ctx->frm0;
1595 while (npixels--) {
1596 *frm = av_bswap16(*frm);
1597 frm++;
1598 }
1599 #endif
1600
1601 1 return 0;
1602 }
1603
1604 static int decode_6(SANMVideoContext *ctx)
1605 {
1606 int npixels = ctx->npixels;
1607 uint16_t *frm = ctx->frm0;
1608
1609 if (bytestream2_get_bytes_left(&ctx->gb) < npixels) {
1610 av_log(ctx->avctx, AV_LOG_ERROR, "Insufficient data for frame.\n");
1611 return AVERROR_INVALIDDATA;
1612 }
1613 while (npixels--)
1614 *frm++ = ctx->codebook[bytestream2_get_byteu(&ctx->gb)];
1615
1616 return 0;
1617 }
1618
1619 static int decode_8(SANMVideoContext *ctx)
1620 {
1621 uint16_t *pdest = ctx->frm0;
1622 uint8_t *rsrc;
1623 long npixels = ctx->npixels;
1624
1625 av_fast_malloc(&ctx->rle_buf, &ctx->rle_buf_size, npixels);
1626 if (!ctx->rle_buf) {
1627 av_log(ctx->avctx, AV_LOG_ERROR, "RLE buffer allocation failed.\n");
1628 return AVERROR(ENOMEM);
1629 }
1630 rsrc = ctx->rle_buf;
1631
1632 if (rle_decode(ctx, rsrc, npixels))
1633 return AVERROR_INVALIDDATA;
1634
1635 while (npixels--)
1636 *pdest++ = ctx->codebook[*rsrc++];
1637
1638 return 0;
1639 }
1640
1641 typedef int (*frm_decoder)(SANMVideoContext *ctx);
1642
1643 static const frm_decoder v1_decoders[] = {
1644 decode_0, decode_nop, decode_2, decode_3, decode_4, decode_5,
1645 decode_6, decode_nop, decode_8
1646 };
1647
1648 16 static int read_frame_header(SANMVideoContext *ctx, SANMFrameHeader *hdr)
1649 {
1650 int i, ret;
1651
1652
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 16 times.
16 if ((ret = bytestream2_get_bytes_left(&ctx->gb)) < 560) {
1653 av_log(ctx->avctx, AV_LOG_ERROR, "Input frame too short (%d bytes).\n",
1654 ret);
1655 return AVERROR_INVALIDDATA;
1656 }
1657 16 bytestream2_skip(&ctx->gb, 8); // skip pad
1658
1659 16 hdr->width = bytestream2_get_le32u(&ctx->gb);
1660 16 hdr->height = bytestream2_get_le32u(&ctx->gb);
1661
1662
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) {
1663 avpriv_report_missing_feature(ctx->avctx, "Variable size frames");
1664 return AVERROR_PATCHWELCOME;
1665 }
1666
1667 16 hdr->seq_num = bytestream2_get_le16u(&ctx->gb);
1668 16 hdr->codec = bytestream2_get_byteu(&ctx->gb);
1669 16 hdr->rotate_code = bytestream2_get_byteu(&ctx->gb);
1670
1671 16 bytestream2_skip(&ctx->gb, 4); // skip pad
1672
1673
2/2
✓ Branch 0 taken 64 times.
✓ Branch 1 taken 16 times.
80 for (i = 0; i < 4; i++)
1674 64 ctx->small_codebook[i] = bytestream2_get_le16u(&ctx->gb);
1675 16 hdr->bg_color = bytestream2_get_le16u(&ctx->gb);
1676
1677 16 bytestream2_skip(&ctx->gb, 2); // skip pad
1678
1679 16 hdr->rle_output_size = bytestream2_get_le32u(&ctx->gb);
1680
2/2
✓ Branch 0 taken 4096 times.
✓ Branch 1 taken 16 times.
4112 for (i = 0; i < 256; i++)
1681 4096 ctx->codebook[i] = bytestream2_get_le16u(&ctx->gb);
1682
1683 16 bytestream2_skip(&ctx->gb, 8); // skip pad
1684
1685 16 return 0;
1686 }
1687
1688 2 static void fill_frame(uint16_t *pbuf, int buf_size, uint16_t color)
1689 {
1690
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if (buf_size--) {
1691 2 *pbuf++ = color;
1692 2 av_memcpy_backptr((uint8_t*)pbuf, 2, 2*buf_size);
1693 }
1694 2 }
1695
1696 15 static int copy_output(SANMVideoContext *ctx, SANMFrameHeader *hdr)
1697 {
1698 uint8_t *dst;
1699 15 const uint8_t *src = (uint8_t*) ctx->frm0;
1700 15 int ret, height = ctx->height;
1701
1/2
✓ Branch 0 taken 15 times.
✗ Branch 1 not taken.
15 ptrdiff_t dstpitch, srcpitch = ctx->pitch * (hdr ? sizeof(ctx->frm0[0]) : 1);
1702
1703
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 15 times.
15 if ((ret = ff_get_buffer(ctx->avctx, ctx->frame, 0)) < 0)
1704 return ret;
1705
1706 15 dst = ctx->frame->data[0];
1707 15 dstpitch = ctx->frame->linesize[0];
1708
1709
2/2
✓ Branch 0 taken 7200 times.
✓ Branch 1 taken 15 times.
7215 while (height--) {
1710 7200 memcpy(dst, src, srcpitch);
1711 7200 src += srcpitch;
1712 7200 dst += dstpitch;
1713 }
1714
1715 15 return 0;
1716 }
1717
1718 16 static int decode_frame(AVCodecContext *avctx, AVFrame *frame,
1719 int *got_frame_ptr, AVPacket *pkt)
1720 {
1721 16 SANMVideoContext *ctx = avctx->priv_data;
1722 int i, ret;
1723
1724 16 ctx->frame = frame;
1725 16 bytestream2_init(&ctx->gb, pkt->data, pkt->size);
1726
1727
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 16 times.
16 if (!ctx->version) {
1728 int to_store = 0;
1729
1730 while (bytestream2_get_bytes_left(&ctx->gb) >= 8) {
1731 uint32_t sig, size;
1732 int pos;
1733
1734 sig = bytestream2_get_be32u(&ctx->gb);
1735 size = bytestream2_get_be32u(&ctx->gb);
1736 pos = bytestream2_tell(&ctx->gb);
1737
1738 if (bytestream2_get_bytes_left(&ctx->gb) < size) {
1739 av_log(avctx, AV_LOG_ERROR, "Incorrect chunk size %"PRIu32".\n", size);
1740 break;
1741 }
1742 switch (sig) {
1743 case MKBETAG('N', 'P', 'A', 'L'):
1744 if (size != PALETTE_SIZE * 3) {
1745 av_log(avctx, AV_LOG_ERROR,
1746 "Incorrect palette block size %"PRIu32".\n", size);
1747 return AVERROR_INVALIDDATA;
1748 }
1749 for (i = 0; i < PALETTE_SIZE; i++)
1750 ctx->pal[i] = 0xFFU << 24 | bytestream2_get_be24u(&ctx->gb);
1751 break;
1752 case MKBETAG('F', 'O', 'B', 'J'):
1753 if (size < 16)
1754 return AVERROR_INVALIDDATA;
1755 if (ret = process_frame_obj(ctx))
1756 return ret;
1757 break;
1758 case MKBETAG('X', 'P', 'A', 'L'):
1759 if (ret = process_xpal(ctx, size))
1760 return ret;
1761 break;
1762 case MKBETAG('S', 'T', 'O', 'R'):
1763 to_store = 1;
1764 break;
1765 case MKBETAG('F', 'T', 'C', 'H'):
1766 memcpy(ctx->frm0, ctx->stored_frame, ctx->buf_size);
1767 break;
1768 default:
1769 bytestream2_skip(&ctx->gb, size);
1770 av_log(avctx, AV_LOG_DEBUG,
1771 "Unknown/unsupported chunk %"PRIx32".\n", sig);
1772 break;
1773 }
1774
1775 bytestream2_seek(&ctx->gb, pos + size, SEEK_SET);
1776 if (size & 1)
1777 bytestream2_skip(&ctx->gb, 1);
1778 }
1779 if (to_store)
1780 memcpy(ctx->stored_frame, ctx->frm0, ctx->buf_size);
1781 if ((ret = copy_output(ctx, NULL)))
1782 return ret;
1783 memcpy(ctx->frame->data[1], ctx->pal, 1024);
1784 } else {
1785 SANMFrameHeader header;
1786
1787
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 16 times.
16 if ((ret = read_frame_header(ctx, &header)))
1788 1 return ret;
1789
1790 16 ctx->rotate_code = header.rotate_code;
1791
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 15 times.
16 if (!header.seq_num) {
1792 1 ctx->frame->flags |= AV_FRAME_FLAG_KEY;
1793 1 ctx->frame->pict_type = AV_PICTURE_TYPE_I;
1794 1 fill_frame(ctx->frm1, ctx->npixels, header.bg_color);
1795 1 fill_frame(ctx->frm2, ctx->npixels, header.bg_color);
1796 } else {
1797 15 ctx->frame->flags &= ~AV_FRAME_FLAG_KEY;
1798 15 ctx->frame->pict_type = AV_PICTURE_TYPE_P;
1799 }
1800
1801
1/2
✓ Branch 0 taken 16 times.
✗ Branch 1 not taken.
16 if (header.codec < FF_ARRAY_ELEMS(v1_decoders)) {
1802
2/2
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 15 times.
16 if ((ret = v1_decoders[header.codec](ctx))) {
1803 1 av_log(avctx, AV_LOG_ERROR,
1804 "Subcodec %d: error decoding frame.\n", header.codec);
1805 1 return ret;
1806 }
1807 } else {
1808 avpriv_request_sample(avctx, "Subcodec %d", header.codec);
1809 return AVERROR_PATCHWELCOME;
1810 }
1811
1812
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 15 times.
15 if ((ret = copy_output(ctx, &header)))
1813 return ret;
1814 }
1815
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 6 times.
15 if (ctx->rotate_code)
1816 9 rotate_bufs(ctx, ctx->rotate_code);
1817
1818 15 *got_frame_ptr = 1;
1819
1820 15 return pkt->size;
1821 }
1822
1823 const FFCodec ff_sanm_decoder = {
1824 .p.name = "sanm",
1825 CODEC_LONG_NAME("LucasArts SANM/Smush video"),
1826 .p.type = AVMEDIA_TYPE_VIDEO,
1827 .p.id = AV_CODEC_ID_SANM,
1828 .priv_data_size = sizeof(SANMVideoContext),
1829 .init = decode_init,
1830 .close = decode_end,
1831 FF_CODEC_DECODE_CB(decode_frame),
1832 .p.capabilities = AV_CODEC_CAP_DR1,
1833 };
1834