Libav
sanm.c
Go to the documentation of this file.
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 Libav.
7  *
8  * Libav 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  * Libav 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 Libav; 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/bswap.h"
25 #include "libavutil/imgutils.h"
26 
27 #include "avcodec.h"
28 #include "bytestream.h"
29 #include "copy_block.h"
30 #include "internal.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, 8, 0, 13, 0, 21,
109  0, -1, 0, -2, 0, -3, 0, -5, 0, -8, 0, -13, 0, -17, 0,
110  -21, 0, 0, 1, 1, 1, 2, 1, 3, 1, 5, 1, 8, 1, 13,
111  1, 21, 1, -1, 1, -2, 1, -3, 1, -5, 1, -8, 1, -13, 1,
112  -17, 1, -21, 1, 0, 2, 1, 2, 2, 2, 3, 2, 5, 2, 8,
113  2, 13, 2, 21, 2, -1, 2, -2, 2, -3, 2, -5, 2, -8, 2,
114  -13, 2, -17, 2, -21, 2, 0, 3, 1, 3, 2, 3, 3, 3, 5,
115  3, 8, 3, 13, 3, 21, 3, -1, 3, -2, 3, -3, 3, -5, 3,
116  -8, 3, -13, 3, -17, 3, -21, 3, 0, 5, 1, 5, 2, 5, 3,
117  5, 5, 5, 8, 5, 13, 5, 21, 5, -1, 5, -2, 5, -3, 5,
118  -5, 5, -8, 5, -13, 5, -17, 5, -21, 5, 0, 8, 1, 8, 2,
119  8, 3, 8, 5, 8, 8, 8, 13, 8, 21, 8, -1, 8, -2, 8,
120  -3, 8, -5, 8, -8, 8, -13, 8, -17, 8, -21, 8, 0, 13, 1,
121  13, 2, 13, 3, 13, 5, 13, 8, 13, 13, 13, 21, 13, -1, 13,
122  -2, 13, -3, 13, -5, 13, -8, 13, -13, 13, -17, 13, -21, 13, 0,
123  21, 1, 21, 2, 21, 3, 21, 5, 21, 8, 21, 13, 21, 21, 21,
124  -1, 21, -2, 21, -3, 21, -5, 21, -8, 21, -13, 21, -17, 21, -21,
125  21, 0, -1, 1, -1, 2, -1, 3, -1, 5, -1, 8, -1, 13, -1,
126  21, -1, -1, -1, -2, -1, -3, -1, -5, -1, -8, -1, -13, -1, -17,
127  -1, -21, -1, 0, -2, 1, -2, 2, -2, 3, -2, 5, -2, 8, -2,
128  13, -2, 21, -2, -1, -2, -2, -2, -3, -2, -5, -2, -8, -2, -13,
129  -2, -17, -2, -21, -2, 0, -3, 1, -3, 2, -3, 3, -3, 5, -3,
130  8, -3, 13, -3, 21, -3, -1, -3, -2, -3, -3, -3, -5, -3, -8,
131  -3, -13, -3, -17, -3, -21, -3, 0, -5, 1, -5, 2, -5, 3, -5,
132  5, -5, 8, -5, 13, -5, 21, -5, -1, -5, -2, -5, -3, -5, -5,
133  -5, -8, -5, -13, -5, -17, -5, -21, -5, 0, -8, 1, -8, 2, -8,
134  3, -8, 5, -8, 8, -8, 13, -8, 21, -8, -1, -8, -2, -8, -3,
135  -8, -5, -8, -8, -8, -13, -8, -17, -8, -21, -8, 0, -13, 1, -13,
136  2, -13, 3, -13, 5, -13, 8, -13, 13, -13, 21, -13, -1, -13, -2,
137  -13, -3, -13, -5, -13, -8, -13, -13, -13, -17, -13, -21, -13, 0, -17,
138  1, -17, 2, -17, 3, -17, 5, -17, 8, -17, 13, -17, 21, -17, -1,
139  -17, -2, -17, -3, -17, -5, -17, -8, -17, -13, -17, -17, -17, -21, -17,
140  0, -21, 1, -21, 2, -21, 3, -21, 5, -21, 8, -21, 13, -21, 21,
141  -21, -1, -21, -2, -21, -3, -21, -5, -21, -8, -21, -13, -21, -17, -21,
142  0, 0, -8, -29, 8, -29, -18, -25, 17, -25, 0, -23, -6, -22, 6,
143  -22, -13, -19, 12, -19, 0, -18, 25, -18, -25, -17, -5, -17, 5, -17,
144  -10, -15, 10, -15, 0, -14, -4, -13, 4, -13, 19, -13, -19, -12, -8,
145  -11, -2, -11, 0, -11, 2, -11, 8, -11, -15, -10, -4, -10, 4, -10,
146  15, -10, -6, -9, -1, -9, 1, -9, 6, -9, -29, -8, -11, -8, -8,
147  -8, -3, -8, 3, -8, 8, -8, 11, -8, 29, -8, -5, -7, -2, -7,
148  0, -7, 2, -7, 5, -7, -22, -6, -9, -6, -6, -6, -3, -6, -1,
149  -6, 1, -6, 3, -6, 6, -6, 9, -6, 22, -6, -17, -5, -7, -5,
150  -4, -5, -2, -5, 0, -5, 2, -5, 4, -5, 7, -5, 17, -5, -13,
151  -4, -10, -4, -5, -4, -3, -4, -1, -4, 0, -4, 1, -4, 3, -4,
152  5, -4, 10, -4, 13, -4, -8, -3, -6, -3, -4, -3, -3, -3, -2,
153  -3, -1, -3, 0, -3, 1, -3, 2, -3, 4, -3, 6, -3, 8, -3,
154  -11, -2, -7, -2, -5, -2, -3, -2, -2, -2, -1, -2, 0, -2, 1,
155  -2, 2, -2, 3, -2, 5, -2, 7, -2, 11, -2, -9, -1, -6, -1,
156  -4, -1, -3, -1, -2, -1, -1, -1, 0, -1, 1, -1, 2, -1, 3,
157  -1, 4, -1, 6, -1, 9, -1, -31, 0, -23, 0, -18, 0, -14, 0,
158  -11, 0, -7, 0, -5, 0, -4, 0, -3, 0, -2, 0, -1, 0, 0,
159  -31, 1, 0, 2, 0, 3, 0, 4, 0, 5, 0, 7, 0, 11, 0,
160  14, 0, 18, 0, 23, 0, 31, 0, -9, 1, -6, 1, -4, 1, -3,
161  1, -2, 1, -1, 1, 0, 1, 1, 1, 2, 1, 3, 1, 4, 1,
162  6, 1, 9, 1, -11, 2, -7, 2, -5, 2, -3, 2, -2, 2, -1,
163  2, 0, 2, 1, 2, 2, 2, 3, 2, 5, 2, 7, 2, 11, 2,
164  -8, 3, -6, 3, -4, 3, -2, 3, -1, 3, 0, 3, 1, 3, 2,
165  3, 3, 3, 4, 3, 6, 3, 8, 3, -13, 4, -10, 4, -5, 4,
166  -3, 4, -1, 4, 0, 4, 1, 4, 3, 4, 5, 4, 10, 4, 13,
167  4, -17, 5, -7, 5, -4, 5, -2, 5, 0, 5, 2, 5, 4, 5,
168  7, 5, 17, 5, -22, 6, -9, 6, -6, 6, -3, 6, -1, 6, 1,
169  6, 3, 6, 6, 6, 9, 6, 22, 6, -5, 7, -2, 7, 0, 7,
170  2, 7, 5, 7, -29, 8, -11, 8, -8, 8, -3, 8, 3, 8, 8,
171  8, 11, 8, 29, 8, -6, 9, -1, 9, 1, 9, 6, 9, -15, 10,
172  -4, 10, 4, 10, 15, 10, -8, 11, -2, 11, 0, 11, 2, 11, 8,
173  11, 19, 12, -19, 13, -4, 13, 4, 13, 0, 14, -10, 15, 10, 15,
174  -5, 17, 5, 17, 25, 17, -25, 18, 0, 18, -12, 19, 13, 19, -6,
175  22, 6, 22, 0, 23, -17, 25, 18, 25, -8, 29, 8, 29, 0, 31,
176  0, 0, -6, -22, 6, -22, -13, -19, 12, -19, 0, -18, -5, -17, 5,
177  -17, -10, -15, 10, -15, 0, -14, -4, -13, 4, -13, 19, -13, -19, -12,
178  -8, -11, -2, -11, 0, -11, 2, -11, 8, -11, -15, -10, -4, -10, 4,
179  -10, 15, -10, -6, -9, -1, -9, 1, -9, 6, -9, -11, -8, -8, -8,
180  -3, -8, 0, -8, 3, -8, 8, -8, 11, -8, -5, -7, -2, -7, 0,
181  -7, 2, -7, 5, -7, -22, -6, -9, -6, -6, -6, -3, -6, -1, -6,
182  1, -6, 3, -6, 6, -6, 9, -6, 22, -6, -17, -5, -7, -5, -4,
183  -5, -2, -5, -1, -5, 0, -5, 1, -5, 2, -5, 4, -5, 7, -5,
184  17, -5, -13, -4, -10, -4, -5, -4, -3, -4, -2, -4, -1, -4, 0,
185  -4, 1, -4, 2, -4, 3, -4, 5, -4, 10, -4, 13, -4, -8, -3,
186  -6, -3, -4, -3, -3, -3, -2, -3, -1, -3, 0, -3, 1, -3, 2,
187  -3, 3, -3, 4, -3, 6, -3, 8, -3, -11, -2, -7, -2, -5, -2,
188  -4, -2, -3, -2, -2, -2, -1, -2, 0, -2, 1, -2, 2, -2, 3,
189  -2, 4, -2, 5, -2, 7, -2, 11, -2, -9, -1, -6, -1, -5, -1,
190  -4, -1, -3, -1, -2, -1, -1, -1, 0, -1, 1, -1, 2, -1, 3,
191  -1, 4, -1, 5, -1, 6, -1, 9, -1, -23, 0, -18, 0, -14, 0,
192  -11, 0, -7, 0, -5, 0, -4, 0, -3, 0, -2, 0, -1, 0, 0,
193  -23, 1, 0, 2, 0, 3, 0, 4, 0, 5, 0, 7, 0, 11, 0,
194  14, 0, 18, 0, 23, 0, -9, 1, -6, 1, -5, 1, -4, 1, -3,
195  1, -2, 1, -1, 1, 0, 1, 1, 1, 2, 1, 3, 1, 4, 1,
196  5, 1, 6, 1, 9, 1, -11, 2, -7, 2, -5, 2, -4, 2, -3,
197  2, -2, 2, -1, 2, 0, 2, 1, 2, 2, 2, 3, 2, 4, 2,
198  5, 2, 7, 2, 11, 2, -8, 3, -6, 3, -4, 3, -3, 3, -2,
199  3, -1, 3, 0, 3, 1, 3, 2, 3, 3, 3, 4, 3, 6, 3,
200  8, 3, -13, 4, -10, 4, -5, 4, -3, 4, -2, 4, -1, 4, 0,
201  4, 1, 4, 2, 4, 3, 4, 5, 4, 10, 4, 13, 4, -17, 5,
202  -7, 5, -4, 5, -2, 5, -1, 5, 0, 5, 1, 5, 2, 5, 4,
203  5, 7, 5, 17, 5, -22, 6, -9, 6, -6, 6, -3, 6, -1, 6,
204  1, 6, 3, 6, 6, 6, 9, 6, 22, 6, -5, 7, -2, 7, 0,
205  7, 2, 7, 5, 7, -11, 8, -8, 8, -3, 8, 0, 8, 3, 8,
206  8, 8, 11, 8, -6, 9, -1, 9, 1, 9, 6, 9, -15, 10, -4,
207  10, 4, 10, 15, 10, -8, 11, -2, 11, 0, 11, 2, 11, 8, 11,
208  19, 12, -19, 13, -4, 13, 4, 13, 0, 14, -10, 15, 10, 15, -5,
209  17, 5, 17, 0, 18, -12, 19, 13, 19, -6, 22, 6, 22, 0, 23,
210 };
211 
212 typedef struct SANMVideoContext {
215 
217  uint32_t pal[PALETTE_SIZE];
219 
220  int pitch;
221  int width, height;
223  int prev_seq;
224 
226  uint16_t *frm0, *frm1, *frm2;
230 
232  unsigned int rle_buf_size;
233 
235 
237 
238  uint16_t codebook[256];
239  uint16_t small_codebook[4];
240 
241  int8_t p4x4glyphs[NGLYPHS][16];
242  int8_t p8x8glyphs[NGLYPHS][64];
244 
245 typedef struct SANMFrameHeader {
247 
248  uint16_t bg_color;
249  uint32_t width, height;
251 
252 enum GlyphEdge {
258 };
259 
260 enum GlyphDir {
266 };
267 
275 static enum GlyphEdge which_edge(int x, int y, int edge_size)
276 {
277  const int edge_max = edge_size - 1;
278 
279  if (!y)
280  return BOTTOM_EDGE;
281  else if (y == edge_max)
282  return TOP_EDGE;
283  else if (!x)
284  return LEFT_EDGE;
285  else if (x == edge_max)
286  return RIGHT_EDGE;
287  else
288  return NO_EDGE;
289 }
290 
291 static enum GlyphDir which_direction(enum GlyphEdge edge0, enum GlyphEdge edge1)
292 {
293  if ((edge0 == LEFT_EDGE && edge1 == RIGHT_EDGE) ||
294  (edge1 == LEFT_EDGE && edge0 == RIGHT_EDGE) ||
295  (edge0 == BOTTOM_EDGE && edge1 != TOP_EDGE) ||
296  (edge1 == BOTTOM_EDGE && edge0 != TOP_EDGE))
297  return DIR_UP;
298  else if ((edge0 == TOP_EDGE && edge1 != BOTTOM_EDGE) ||
299  (edge1 == TOP_EDGE && edge0 != BOTTOM_EDGE))
300  return DIR_DOWN;
301  else if ((edge0 == LEFT_EDGE && edge1 != RIGHT_EDGE) ||
302  (edge1 == LEFT_EDGE && edge0 != RIGHT_EDGE))
303  return DIR_LEFT;
304  else if ((edge0 == TOP_EDGE && edge1 == BOTTOM_EDGE) ||
305  (edge1 == TOP_EDGE && edge0 == BOTTOM_EDGE) ||
306  (edge0 == RIGHT_EDGE && edge1 != LEFT_EDGE) ||
307  (edge1 == RIGHT_EDGE && edge0 != LEFT_EDGE))
308  return DIR_RIGHT;
309 
310  return NO_DIR;
311 }
312 
313 /* Interpolate two points. */
314 static void interp_point(int8_t *points, int x0, int y0, int x1, int y1,
315  int pos, int npoints)
316 {
317  if (npoints) {
318  points[0] = (x0 * pos + x1 * (npoints - pos) + (npoints >> 1)) / npoints;
319  points[1] = (y0 * pos + y1 * (npoints - pos) + (npoints >> 1)) / npoints;
320  } else {
321  points[0] = x0;
322  points[1] = y0;
323  }
324 }
325 
334 static void make_glyphs(int8_t *pglyphs, const int8_t *xvec, const int8_t *yvec,
335  const int side_length)
336 {
337  const int glyph_size = side_length * side_length;
338  int8_t *pglyph = pglyphs;
339 
340  int i, j;
341  for (i = 0; i < GLYPH_COORD_VECT_SIZE; i++) {
342  int x0 = xvec[i];
343  int y0 = yvec[i];
344  enum GlyphEdge edge0 = which_edge(x0, y0, side_length);
345 
346  for (j = 0; j < GLYPH_COORD_VECT_SIZE; j++, pglyph += glyph_size) {
347  int x1 = xvec[j];
348  int y1 = yvec[j];
349  enum GlyphEdge edge1 = which_edge(x1, y1, side_length);
350  enum GlyphDir dir = which_direction(edge0, edge1);
351  int npoints = FFMAX(FFABS(x1 - x0), FFABS(y1 - y0));
352  int ipoint;
353 
354  for (ipoint = 0; ipoint <= npoints; ipoint++) {
355  int8_t point[2];
356  int irow, icol;
357 
358  interp_point(point, x0, y0, x1, y1, ipoint, npoints);
359 
360  switch (dir) {
361  case DIR_UP:
362  for (irow = point[1]; irow >= 0; irow--)
363  pglyph[point[0] + irow * side_length] = 1;
364  break;
365 
366  case DIR_DOWN:
367  for (irow = point[1]; irow < side_length; irow++)
368  pglyph[point[0] + irow * side_length] = 1;
369  break;
370 
371  case DIR_LEFT:
372  for (icol = point[0]; icol >= 0; icol--)
373  pglyph[icol + point[1] * side_length] = 1;
374  break;
375 
376  case DIR_RIGHT:
377  for (icol = point[0]; icol < side_length; icol++)
378  pglyph[icol + point[1] * side_length] = 1;
379  break;
380  }
381  }
382  }
383  }
384 }
385 
386 static void init_sizes(SANMVideoContext *ctx, int width, int height)
387 {
388  ctx->width = width;
389  ctx->height = height;
390  ctx->npixels = width * height;
391 
392  ctx->aligned_width = FFALIGN(width, 8);
393  ctx->aligned_height = FFALIGN(height, 8);
394 
395  ctx->buf_size = ctx->aligned_width * ctx->aligned_height * sizeof(ctx->frm0[0]);
396  ctx->pitch = width;
397 }
398 
400 {
401  av_freep(&ctx->frm0);
402  av_freep(&ctx->frm1);
403  av_freep(&ctx->frm2);
404  av_freep(&ctx->stored_frame);
405  av_freep(&ctx->rle_buf);
406  ctx->frm0_size =
407  ctx->frm1_size =
408  ctx->frm2_size = 0;
409  init_sizes(ctx, 0, 0);
410 }
411 
413 {
414  av_fast_padded_malloc(&ctx->frm0, &ctx->frm0_size, ctx->buf_size);
415  av_fast_padded_malloc(&ctx->frm1, &ctx->frm1_size, ctx->buf_size);
416  av_fast_padded_malloc(&ctx->frm2, &ctx->frm2_size, ctx->buf_size);
417  if (!ctx->version)
419  &ctx->stored_frame_size, ctx->buf_size);
420 
421  if (!ctx->frm0 || !ctx->frm1 || !ctx->frm2 ||
422  (!ctx->stored_frame && !ctx->version)) {
423  destroy_buffers(ctx);
424  return AVERROR(ENOMEM);
425  }
426 
427  return 0;
428 }
429 
430 static void rotate_bufs(SANMVideoContext *ctx, int rotate_code)
431 {
432  if (rotate_code == 2)
433  FFSWAP(uint16_t*, ctx->frm1, ctx->frm2);
434  FFSWAP(uint16_t*, ctx->frm2, ctx->frm0);
435 }
436 
438 {
439  SANMVideoContext *ctx = avctx->priv_data;
440 
441  ctx->avctx = avctx;
442  ctx->version = !avctx->extradata_size;
443 
445 
446  init_sizes(ctx, avctx->width, avctx->height);
447  if (init_buffers(ctx)) {
448  av_log(avctx, AV_LOG_ERROR, "Error allocating buffers.\n");
449  return AVERROR(ENOMEM);
450  }
451 
452  make_glyphs(ctx->p4x4glyphs[0], glyph4_x, glyph4_y, 4);
453  make_glyphs(ctx->p8x8glyphs[0], glyph8_x, glyph8_y, 8);
454 
455  if (!ctx->version) {
456  int i;
457 
458  if (avctx->extradata_size < 1026) {
459  av_log(avctx, AV_LOG_ERROR, "Not enough extradata.\n");
460  return AVERROR_INVALIDDATA;
461  }
462 
463  ctx->subversion = AV_RL16(avctx->extradata);
464  for (i = 0; i < 256; i++)
465  ctx->pal[i] = 0xFFU << 24 | AV_RL32(avctx->extradata + 2 + i * 4);
466  }
467 
468  return 0;
469 }
470 
472 {
473  SANMVideoContext *ctx = avctx->priv_data;
474 
475  destroy_buffers(ctx);
476 
477  return 0;
478 }
479 
480 static int rle_decode(SANMVideoContext *ctx, uint8_t *dst, const int out_size)
481 {
482  int opcode, color, run_len, left = out_size;
483 
484  while (left > 0) {
485  opcode = bytestream2_get_byte(&ctx->gb);
486  run_len = (opcode >> 1) + 1;
487  if (run_len > left || bytestream2_get_bytes_left(&ctx->gb) <= 0)
488  return AVERROR_INVALIDDATA;
489 
490  if (opcode & 1) {
491  color = bytestream2_get_byte(&ctx->gb);
492  memset(dst, color, run_len);
493  } else {
494  if (bytestream2_get_bytes_left(&ctx->gb) < run_len)
495  return AVERROR_INVALIDDATA;
496  bytestream2_get_bufferu(&ctx->gb, dst, run_len);
497  }
498 
499  dst += run_len;
500  left -= run_len;
501  }
502 
503  return 0;
504 }
505 
506 static int old_codec1(SANMVideoContext *ctx, int top,
507  int left, int width, int height)
508 {
509  uint8_t *dst = ((uint8_t *)ctx->frm0) + left + top * ctx->pitch;
510  int i, j, len, flag, code, val, pos, end;
511 
512  for (i = 0; i < height; i++) {
513  pos = 0;
514 
515  if (bytestream2_get_bytes_left(&ctx->gb) < 2)
516  return AVERROR_INVALIDDATA;
517 
518  len = bytestream2_get_le16u(&ctx->gb);
519  end = bytestream2_tell(&ctx->gb) + len;
520 
521  while (bytestream2_tell(&ctx->gb) < end) {
522  if (bytestream2_get_bytes_left(&ctx->gb) < 2)
523  return AVERROR_INVALIDDATA;
524 
525  code = bytestream2_get_byteu(&ctx->gb);
526  flag = code & 1;
527  code = (code >> 1) + 1;
528  if (pos + code > width)
529  return AVERROR_INVALIDDATA;
530  if (flag) {
531  val = bytestream2_get_byteu(&ctx->gb);
532  if (val)
533  memset(dst + pos, val, code);
534  pos += code;
535  } else {
536  if (bytestream2_get_bytes_left(&ctx->gb) < code)
537  return AVERROR_INVALIDDATA;
538  for (j = 0; j < code; j++) {
539  val = bytestream2_get_byteu(&ctx->gb);
540  if (val)
541  dst[pos] = val;
542  pos++;
543  }
544  }
545  }
546  dst += ctx->pitch;
547  }
548  ctx->rotate_code = 0;
549 
550  return 0;
551 }
552 
553 static inline void codec37_mv(uint8_t *dst, const uint8_t *src,
554  int height, int stride, int x, int y)
555 {
556  int pos, i, j;
557 
558  pos = x + y * stride;
559  for (j = 0; j < 4; j++) {
560  for (i = 0; i < 4; i++) {
561  if ((pos + i) < 0 || (pos + i) >= height * stride)
562  dst[i] = 0;
563  else
564  dst[i] = src[i];
565  }
566  dst += stride;
567  src += stride;
568  pos += stride;
569  }
570 }
571 
572 static int old_codec37(SANMVideoContext *ctx, int top,
573  int left, int width, int height)
574 {
575  int stride = ctx->pitch;
576  int i, j, k, t;
577  uint8_t *dst, *prev;
578  int skip_run = 0;
579  int compr = bytestream2_get_byte(&ctx->gb);
580  int mvoff = bytestream2_get_byte(&ctx->gb);
581  int seq = bytestream2_get_le16(&ctx->gb);
582  uint32_t decoded_size = bytestream2_get_le32(&ctx->gb);
583  int flags;
584 
585  bytestream2_skip(&ctx->gb, 4);
586  flags = bytestream2_get_byte(&ctx->gb);
587  bytestream2_skip(&ctx->gb, 3);
588 
589  if (decoded_size > ctx->height * stride - left - top * stride) {
590  decoded_size = ctx->height * stride - left - top * stride;
591  av_log(ctx->avctx, AV_LOG_WARNING, "Decoded size is too large.\n");
592  }
593 
594  ctx->rotate_code = 0;
595 
596  if (((seq & 1) || !(flags & 1)) && (compr && compr != 2))
597  rotate_bufs(ctx, 1);
598 
599  dst = ((uint8_t*)ctx->frm0) + left + top * stride;
600  prev = ((uint8_t*)ctx->frm2) + left + top * stride;
601 
602  if (mvoff > 2) {
603  av_log(ctx->avctx, AV_LOG_ERROR, "Invalid motion base value %d.\n", mvoff);
604  return AVERROR_INVALIDDATA;
605  }
606 
607  switch (compr) {
608  case 0:
609  for (i = 0; i < height; i++) {
610  bytestream2_get_buffer(&ctx->gb, dst, width);
611  dst += stride;
612  }
613  memset(ctx->frm1, 0, ctx->height * stride);
614  memset(ctx->frm2, 0, ctx->height * stride);
615  break;
616  case 2:
617  if (rle_decode(ctx, dst, decoded_size))
618  return AVERROR_INVALIDDATA;
619  memset(ctx->frm1, 0, ctx->frm1_size);
620  memset(ctx->frm2, 0, ctx->frm2_size);
621  break;
622  case 3:
623  case 4:
624  if (flags & 4) {
625  for (j = 0; j < height; j += 4) {
626  for (i = 0; i < width; i += 4) {
627  int code;
628  if (skip_run) {
629  skip_run--;
630  copy_block4(dst + i, prev + i, stride, stride, 4);
631  continue;
632  }
633  if (bytestream2_get_bytes_left(&ctx->gb) < 1)
634  return AVERROR_INVALIDDATA;
635  code = bytestream2_get_byteu(&ctx->gb);
636  switch (code) {
637  case 0xFF:
638  if (bytestream2_get_bytes_left(&ctx->gb) < 16)
639  return AVERROR_INVALIDDATA;
640  for (k = 0; k < 4; k++)
641  bytestream2_get_bufferu(&ctx->gb, dst + i + k * stride, 4);
642  break;
643  case 0xFE:
644  if (bytestream2_get_bytes_left(&ctx->gb) < 4)
645  return AVERROR_INVALIDDATA;
646  for (k = 0; k < 4; k++)
647  memset(dst + i + k * stride, bytestream2_get_byteu(&ctx->gb), 4);
648  break;
649  case 0xFD:
650  if (bytestream2_get_bytes_left(&ctx->gb) < 1)
651  return AVERROR_INVALIDDATA;
652  t = bytestream2_get_byteu(&ctx->gb);
653  for (k = 0; k < 4; k++)
654  memset(dst + i + k * stride, t, 4);
655  break;
656  default:
657  if (compr == 4 && !code) {
658  if (bytestream2_get_bytes_left(&ctx->gb) < 1)
659  return AVERROR_INVALIDDATA;
660  skip_run = bytestream2_get_byteu(&ctx->gb) + 1;
661  i -= 4;
662  } else {
663  int mx, my;
664 
665  mx = c37_mv[(mvoff * 255 + code) * 2];
666  my = c37_mv[(mvoff * 255 + code) * 2 + 1];
667  codec37_mv(dst + i, prev + i + mx + my * stride,
668  ctx->height, stride, i + mx, j + my);
669  }
670  }
671  }
672  dst += stride * 4;
673  prev += stride * 4;
674  }
675  } else {
676  for (j = 0; j < height; j += 4) {
677  for (i = 0; i < width; i += 4) {
678  int code;
679  if (skip_run) {
680  skip_run--;
681  copy_block4(dst + i, prev + i, stride, stride, 4);
682  continue;
683  }
684  code = bytestream2_get_byte(&ctx->gb);
685  if (code == 0xFF) {
686  if (bytestream2_get_bytes_left(&ctx->gb) < 16)
687  return AVERROR_INVALIDDATA;
688  for (k = 0; k < 4; k++)
689  bytestream2_get_bufferu(&ctx->gb, dst + i + k * stride, 4);
690  } else if (compr == 4 && !code) {
691  if (bytestream2_get_bytes_left(&ctx->gb) < 1)
692  return AVERROR_INVALIDDATA;
693  skip_run = bytestream2_get_byteu(&ctx->gb) + 1;
694  i -= 4;
695  } else {
696  int mx, my;
697 
698  mx = c37_mv[(mvoff * 255 + code) * 2];
699  my = c37_mv[(mvoff * 255 + code) * 2 + 1];
700  codec37_mv(dst + i, prev + i + mx + my * stride,
701  ctx->height, stride, i + mx, j + my);
702  }
703  }
704  dst += stride * 4;
705  prev += stride * 4;
706  }
707  }
708  break;
709  default:
711  "Subcodec 37 compression %d", compr);
712  return AVERROR_PATCHWELCOME;
713  }
714 
715  return 0;
716 }
717 
718 static int process_block(SANMVideoContext *ctx, uint8_t *dst, uint8_t *prev1,
719  uint8_t *prev2, int stride, int tbl, int size)
720 {
721  int code, k, t;
722  uint8_t colors[2];
723  int8_t *pglyph;
724 
725  if (bytestream2_get_bytes_left(&ctx->gb) < 1)
726  return AVERROR_INVALIDDATA;
727 
728  code = bytestream2_get_byteu(&ctx->gb);
729  if (code >= 0xF8) {
730  switch (code) {
731  case 0xFF:
732  if (size == 2) {
733  if (bytestream2_get_bytes_left(&ctx->gb) < 4)
734  return AVERROR_INVALIDDATA;
735  dst[0] = bytestream2_get_byteu(&ctx->gb);
736  dst[1] = bytestream2_get_byteu(&ctx->gb);
737  dst[0 + stride] = bytestream2_get_byteu(&ctx->gb);
738  dst[1 + stride] = bytestream2_get_byteu(&ctx->gb);
739  } else {
740  size >>= 1;
741  if (process_block(ctx, dst, prev1, prev2, stride, tbl, size))
742  return AVERROR_INVALIDDATA;
743  if (process_block(ctx, dst + size, prev1 + size, prev2 + size,
744  stride, tbl, size))
745  return AVERROR_INVALIDDATA;
746  dst += size * stride;
747  prev1 += size * stride;
748  prev2 += size * stride;
749  if (process_block(ctx, dst, prev1, prev2, stride, tbl, size))
750  return AVERROR_INVALIDDATA;
751  if (process_block(ctx, dst + size, prev1 + size, prev2 + size,
752  stride, tbl, size))
753  return AVERROR_INVALIDDATA;
754  }
755  break;
756  case 0xFE:
757  if (bytestream2_get_bytes_left(&ctx->gb) < 1)
758  return AVERROR_INVALIDDATA;
759 
760  t = bytestream2_get_byteu(&ctx->gb);
761  for (k = 0; k < size; k++)
762  memset(dst + k * stride, t, size);
763  break;
764  case 0xFD:
765  if (bytestream2_get_bytes_left(&ctx->gb) < 3)
766  return AVERROR_INVALIDDATA;
767 
768  code = bytestream2_get_byteu(&ctx->gb);
769  pglyph = (size == 8) ? ctx->p8x8glyphs[code] : ctx->p4x4glyphs[code];
770  bytestream2_get_bufferu(&ctx->gb, colors, 2);
771 
772  for (k = 0; k < size; k++)
773  for (t = 0; t < size; t++)
774  dst[t + k * stride] = colors[!*pglyph++];
775  break;
776  case 0xFC:
777  for (k = 0; k < size; k++)
778  memcpy(dst + k * stride, prev1 + k * stride, size);
779  break;
780  default:
781  k = bytestream2_tell(&ctx->gb);
782  bytestream2_seek(&ctx->gb, tbl + (code & 7), SEEK_SET);
783  t = bytestream2_get_byte(&ctx->gb);
784  bytestream2_seek(&ctx->gb, k, SEEK_SET);
785  for (k = 0; k < size; k++)
786  memset(dst + k * stride, t, size);
787  }
788  } else {
789  int mx = motion_vectors[code][0];
790  int my = motion_vectors[code][1];
791  int index = prev2 - (const uint8_t *)ctx->frm2;
792 
793  av_assert2(index >= 0 && index < (ctx->buf_size >> 1));
794 
795  if (index < -mx - my * stride ||
796  (ctx->buf_size >> 1) - index < mx + size + (my + size - 1) * stride) {
797  av_log(ctx->avctx, AV_LOG_ERROR, "MV is invalid.\n");
798  return AVERROR_INVALIDDATA;
799  }
800 
801  for (k = 0; k < size; k++)
802  memcpy(dst + k * stride, prev2 + mx + (my + k) * stride, size);
803  }
804 
805  return 0;
806 }
807 
808 static int old_codec47(SANMVideoContext *ctx, int top,
809  int left, int width, int height)
810 {
811  uint32_t decoded_size;
812  int i, j;
813  int stride = ctx->pitch;
814  uint8_t *dst = (uint8_t *)ctx->frm0 + left + top * stride;
815  uint8_t *prev1 = (uint8_t *)ctx->frm1;
816  uint8_t *prev2 = (uint8_t *)ctx->frm2;
817  int tbl_pos = bytestream2_tell(&ctx->gb);
818  int seq = bytestream2_get_le16(&ctx->gb);
819  int compr = bytestream2_get_byte(&ctx->gb);
820  int new_rot = bytestream2_get_byte(&ctx->gb);
821  int skip = bytestream2_get_byte(&ctx->gb);
822 
823  bytestream2_skip(&ctx->gb, 9);
824  decoded_size = bytestream2_get_le32(&ctx->gb);
825  bytestream2_skip(&ctx->gb, 8);
826 
827  if (decoded_size > ctx->height * stride - left - top * stride) {
828  decoded_size = ctx->height * stride - left - top * stride;
829  av_log(ctx->avctx, AV_LOG_WARNING, "Decoded size is too large.\n");
830  }
831 
832  if (skip & 1)
833  bytestream2_skip(&ctx->gb, 0x8080);
834  if (!seq) {
835  ctx->prev_seq = -1;
836  memset(prev1, 0, ctx->height * stride);
837  memset(prev2, 0, ctx->height * stride);
838  }
839 
840  switch (compr) {
841  case 0:
842  if (bytestream2_get_bytes_left(&ctx->gb) < width * height)
843  return AVERROR_INVALIDDATA;
844  for (j = 0; j < height; j++) {
845  bytestream2_get_bufferu(&ctx->gb, dst, width);
846  dst += stride;
847  }
848  break;
849  case 1:
850  if (bytestream2_get_bytes_left(&ctx->gb) < ((width + 1) >> 1) * ((height + 1) >> 1))
851  return AVERROR_INVALIDDATA;
852  for (j = 0; j < height; j += 2) {
853  for (i = 0; i < width; i += 2) {
854  dst[i] =
855  dst[i + 1] =
856  dst[stride + i] =
857  dst[stride + i + 1] = bytestream2_get_byteu(&ctx->gb);
858  }
859  dst += stride * 2;
860  }
861  break;
862  case 2:
863  if (seq == ctx->prev_seq + 1) {
864  for (j = 0; j < height; j += 8) {
865  for (i = 0; i < width; i += 8)
866  if (process_block(ctx, dst + i, prev1 + i, prev2 + i, stride,
867  tbl_pos + 8, 8))
868  return AVERROR_INVALIDDATA;
869  dst += stride * 8;
870  prev1 += stride * 8;
871  prev2 += stride * 8;
872  }
873  }
874  break;
875  case 3:
876  memcpy(ctx->frm0, ctx->frm2, ctx->pitch * ctx->height);
877  break;
878  case 4:
879  memcpy(ctx->frm0, ctx->frm1, ctx->pitch * ctx->height);
880  break;
881  case 5:
882  if (rle_decode(ctx, dst, decoded_size))
883  return AVERROR_INVALIDDATA;
884  break;
885  default:
887  "Subcodec 47 compression %d", compr);
888  return AVERROR_PATCHWELCOME;
889  }
890  if (seq == ctx->prev_seq + 1)
891  ctx->rotate_code = new_rot;
892  else
893  ctx->rotate_code = 0;
894  ctx->prev_seq = seq;
895 
896  return 0;
897 }
898 
900 {
901  uint16_t codec = bytestream2_get_le16u(&ctx->gb);
902  uint16_t left = bytestream2_get_le16u(&ctx->gb);
903  uint16_t top = bytestream2_get_le16u(&ctx->gb);
904  uint16_t w = bytestream2_get_le16u(&ctx->gb);
905  uint16_t h = bytestream2_get_le16u(&ctx->gb);
906 
907  if (!w || !h) {
908  av_log(ctx->avctx, AV_LOG_ERROR, "Dimensions are invalid.\n");
909  return AVERROR_INVALIDDATA;
910  }
911 
912  if (ctx->width < left + w || ctx->height < top + h) {
913  int ret = ff_set_dimensions(ctx->avctx, FFMAX(left + w, ctx->width),
914  FFMAX(top + h, ctx->height));
915  if (ret < 0)
916  return ret;
917  init_sizes(ctx, FFMAX(left + w, ctx->width),
918  FFMAX(top + h, ctx->height));
919  if (init_buffers(ctx)) {
920  av_log(ctx->avctx, AV_LOG_ERROR, "Error resizing buffers.\n");
921  return AVERROR(ENOMEM);
922  }
923  }
924  bytestream2_skip(&ctx->gb, 4);
925 
926  switch (codec) {
927  case 1:
928  case 3:
929  return old_codec1(ctx, top, left, w, h);
930  break;
931  case 37:
932  return old_codec37(ctx, top, left, w, h);
933  break;
934  case 47:
935  return old_codec47(ctx, top, left, w, h);
936  break;
937  default:
938  avpriv_request_sample(ctx->avctx, "Subcodec %d", codec);
939  return AVERROR_PATCHWELCOME;
940  }
941 }
942 
943 static int decode_0(SANMVideoContext *ctx)
944 {
945  uint16_t *frm = ctx->frm0;
946  int x, y;
947 
948  if (bytestream2_get_bytes_left(&ctx->gb) < ctx->width * ctx->height * 2) {
949  av_log(ctx->avctx, AV_LOG_ERROR, "Insufficient data for raw frame.\n");
950  return AVERROR_INVALIDDATA;
951  }
952  for (y = 0; y < ctx->height; y++) {
953  for (x = 0; x < ctx->width; x++)
954  frm[x] = bytestream2_get_le16u(&ctx->gb);
955  frm += ctx->pitch;
956  }
957  return 0;
958 }
959 
960 static int decode_nop(SANMVideoContext *ctx)
961 {
962  avpriv_request_sample(ctx->avctx, "Unknown/unsupported compression type");
963  return AVERROR_PATCHWELCOME;
964 }
965 
966 static void copy_block(uint16_t *pdest, uint16_t *psrc, int block_size, int pitch)
967 {
968  uint8_t *dst = (uint8_t *)pdest;
969  uint8_t *src = (uint8_t *)psrc;
970  int stride = pitch * 2;
971 
972  switch (block_size) {
973  case 2:
974  copy_block4(dst, src, stride, stride, 2);
975  break;
976  case 4:
977  copy_block8(dst, src, stride, stride, 4);
978  break;
979  case 8:
980  copy_block16(dst, src, stride, stride, 8);
981  break;
982  }
983 }
984 
985 static void fill_block(uint16_t *pdest, uint16_t color, int block_size, int pitch)
986 {
987  int x, y;
988 
989  pitch -= block_size;
990  for (y = 0; y < block_size; y++, pdest += pitch)
991  for (x = 0; x < block_size; x++)
992  *pdest++ = color;
993 }
994 
995 static int draw_glyph(SANMVideoContext *ctx, uint16_t *dst, int index,
996  uint16_t fg_color, uint16_t bg_color, int block_size,
997  int pitch)
998 {
999  int8_t *pglyph;
1000  uint16_t colors[2] = { fg_color, bg_color };
1001  int x, y;
1002 
1003  if (index >= NGLYPHS) {
1004  av_log(ctx->avctx, AV_LOG_ERROR, "Ignoring nonexistent glyph #%u.\n", index);
1005  return AVERROR_INVALIDDATA;
1006  }
1007 
1008  pglyph = block_size == 8 ? ctx->p8x8glyphs[index] : ctx->p4x4glyphs[index];
1009  pitch -= block_size;
1010 
1011  for (y = 0; y < block_size; y++, dst += pitch)
1012  for (x = 0; x < block_size; x++)
1013  *dst++ = colors[*pglyph++];
1014  return 0;
1015 }
1016 
1017 static int opcode_0xf7(SANMVideoContext *ctx, int cx, int cy, int block_size, int pitch)
1018 {
1019  uint16_t *dst = ctx->frm0 + cx + cy * ctx->pitch;
1020 
1021  if (block_size == 2) {
1022  uint32_t indices;
1023 
1024  if (bytestream2_get_bytes_left(&ctx->gb) < 4)
1025  return AVERROR_INVALIDDATA;
1026 
1027  indices = bytestream2_get_le32u(&ctx->gb);
1028  dst[0] = ctx->codebook[indices & 0xFF];
1029  indices >>= 8;
1030  dst[1] = ctx->codebook[indices & 0xFF];
1031  indices >>= 8;
1032  dst[pitch] = ctx->codebook[indices & 0xFF];
1033  indices >>= 8;
1034  dst[pitch + 1] = ctx->codebook[indices & 0xFF];
1035  } else {
1036  uint16_t fgcolor, bgcolor;
1037  int glyph;
1038 
1039  if (bytestream2_get_bytes_left(&ctx->gb) < 3)
1040  return AVERROR_INVALIDDATA;
1041 
1042  glyph = bytestream2_get_byteu(&ctx->gb);
1043  bgcolor = ctx->codebook[bytestream2_get_byteu(&ctx->gb)];
1044  fgcolor = ctx->codebook[bytestream2_get_byteu(&ctx->gb)];
1045 
1046  draw_glyph(ctx, dst, glyph, fgcolor, bgcolor, block_size, pitch);
1047  }
1048  return 0;
1049 }
1050 
1051 static int opcode_0xf8(SANMVideoContext *ctx, int cx, int cy, int block_size, int pitch)
1052 {
1053  uint16_t *dst = ctx->frm0 + cx + cy * ctx->pitch;
1054 
1055  if (block_size == 2) {
1056  if (bytestream2_get_bytes_left(&ctx->gb) < 8)
1057  return AVERROR_INVALIDDATA;
1058 
1059  dst[0] = bytestream2_get_le16u(&ctx->gb);
1060  dst[1] = bytestream2_get_le16u(&ctx->gb);
1061  dst[pitch] = bytestream2_get_le16u(&ctx->gb);
1062  dst[pitch + 1] = bytestream2_get_le16u(&ctx->gb);
1063  } else {
1064  uint16_t fgcolor, bgcolor;
1065  int glyph;
1066 
1067  if (bytestream2_get_bytes_left(&ctx->gb) < 5)
1068  return AVERROR_INVALIDDATA;
1069 
1070  glyph = bytestream2_get_byteu(&ctx->gb);
1071  bgcolor = bytestream2_get_le16u(&ctx->gb);
1072  fgcolor = bytestream2_get_le16u(&ctx->gb);
1073 
1074  draw_glyph(ctx, dst, glyph, fgcolor, bgcolor, block_size, pitch);
1075  }
1076  return 0;
1077 }
1078 
1079 static int good_mvec(SANMVideoContext *ctx, int cx, int cy, int mx, int my,
1080  int block_size)
1081 {
1082  int start_pos = cx + mx + (cy + my) * ctx->pitch;
1083  int end_pos = start_pos + (block_size - 1) * (ctx->pitch + 1);
1084 
1085  int good = start_pos >= 0 && end_pos < (ctx->buf_size >> 1);
1086 
1087  if (!good)
1088  av_log(ctx->avctx, AV_LOG_ERROR,
1089  "Ignoring invalid motion vector (%i, %i)->(%u, %u), block size = %u\n",
1090  cx + mx, cy + my, cx, cy, block_size);
1091 
1092  return good;
1093 }
1094 
1095 static int codec2subblock(SANMVideoContext *ctx, int cx, int cy, int blk_size)
1096 {
1097  int16_t mx, my, index;
1098  int opcode;
1099 
1100  if (bytestream2_get_bytes_left(&ctx->gb) < 1)
1101  return AVERROR_INVALIDDATA;
1102 
1103  opcode = bytestream2_get_byteu(&ctx->gb);
1104 
1105  switch (opcode) {
1106  default:
1107  mx = motion_vectors[opcode][0];
1108  my = motion_vectors[opcode][1];
1109 
1110  if (good_mvec(ctx, cx, cy, mx, my, blk_size)) {
1111  copy_block(ctx->frm0 + cx + ctx->pitch * cy,
1112  ctx->frm2 + cx + mx + ctx->pitch * (cy + my),
1113  blk_size, ctx->pitch);
1114  }
1115  break;
1116  case 0xF5:
1117  if (bytestream2_get_bytes_left(&ctx->gb) < 2)
1118  return AVERROR_INVALIDDATA;
1119  index = bytestream2_get_le16u(&ctx->gb);
1120 
1121  mx = index % ctx->width;
1122  my = index / ctx->width;
1123 
1124  if (good_mvec(ctx, cx, cy, mx, my, blk_size)) {
1125  copy_block(ctx->frm0 + cx + ctx->pitch * cy,
1126  ctx->frm2 + cx + mx + ctx->pitch * (cy + my),
1127  blk_size, ctx->pitch);
1128  }
1129  break;
1130  case 0xF6:
1131  copy_block(ctx->frm0 + cx + ctx->pitch * cy,
1132  ctx->frm1 + cx + ctx->pitch * cy,
1133  blk_size, ctx->pitch);
1134  break;
1135  case 0xF7:
1136  opcode_0xf7(ctx, cx, cy, blk_size, ctx->pitch);
1137  break;
1138 
1139  case 0xF8:
1140  opcode_0xf8(ctx, cx, cy, blk_size, ctx->pitch);
1141  break;
1142  case 0xF9:
1143  case 0xFA:
1144  case 0xFB:
1145  case 0xFC:
1146  fill_block(ctx->frm0 + cx + cy * ctx->pitch,
1147  ctx->small_codebook[opcode - 0xf9], blk_size, ctx->pitch);
1148  break;
1149  case 0xFD:
1150  if (bytestream2_get_bytes_left(&ctx->gb) < 1)
1151  return AVERROR_INVALIDDATA;
1152  fill_block(ctx->frm0 + cx + cy * ctx->pitch,
1153  ctx->codebook[bytestream2_get_byteu(&ctx->gb)], blk_size, ctx->pitch);
1154  break;
1155  case 0xFE:
1156  if (bytestream2_get_bytes_left(&ctx->gb) < 2)
1157  return AVERROR_INVALIDDATA;
1158  fill_block(ctx->frm0 + cx + cy * ctx->pitch,
1159  bytestream2_get_le16u(&ctx->gb), blk_size, ctx->pitch);
1160  break;
1161  case 0xFF:
1162  if (blk_size == 2) {
1163  opcode_0xf8(ctx, cx, cy, blk_size, ctx->pitch);
1164  } else {
1165  blk_size >>= 1;
1166  if (codec2subblock(ctx, cx, cy, blk_size))
1167  return AVERROR_INVALIDDATA;
1168  if (codec2subblock(ctx, cx + blk_size, cy, blk_size))
1169  return AVERROR_INVALIDDATA;
1170  if (codec2subblock(ctx, cx, cy + blk_size, blk_size))
1171  return AVERROR_INVALIDDATA;
1172  if (codec2subblock(ctx, cx + blk_size, cy + blk_size, blk_size))
1173  return AVERROR_INVALIDDATA;
1174  }
1175  break;
1176  }
1177  return 0;
1178 }
1179 
1180 static int decode_2(SANMVideoContext *ctx)
1181 {
1182  int cx, cy, ret;
1183 
1184  for (cy = 0; cy < ctx->aligned_height; cy += 8)
1185  for (cx = 0; cx < ctx->aligned_width; cx += 8)
1186  if (ret = codec2subblock(ctx, cx, cy, 8))
1187  return ret;
1188 
1189  return 0;
1190 }
1191 
1192 static int decode_3(SANMVideoContext *ctx)
1193 {
1194  memcpy(ctx->frm0, ctx->frm2, ctx->frm2_size);
1195  return 0;
1196 }
1197 
1198 static int decode_4(SANMVideoContext *ctx)
1199 {
1200  memcpy(ctx->frm0, ctx->frm1, ctx->frm1_size);
1201  return 0;
1202 }
1203 
1204 static int decode_5(SANMVideoContext *ctx)
1205 {
1206 #if HAVE_BIGENDIAN
1207  uint16_t *frm;
1208  int npixels;
1209 #endif
1210  uint8_t *dst = (uint8_t*)ctx->frm0;
1211 
1212  if (rle_decode(ctx, dst, ctx->buf_size))
1213  return AVERROR_INVALIDDATA;
1214 
1215 #if HAVE_BIGENDIAN
1216  npixels = ctx->npixels;
1217  frm = ctx->frm0;
1218  while (npixels--) {
1219  *frm = av_bswap16(*frm);
1220  frm++;
1221  }
1222 #endif
1223 
1224  return 0;
1225 }
1226 
1227 static int decode_6(SANMVideoContext *ctx)
1228 {
1229  int npixels = ctx->npixels;
1230  uint16_t *frm = ctx->frm0;
1231 
1232  if (bytestream2_get_bytes_left(&ctx->gb) < npixels) {
1233  av_log(ctx->avctx, AV_LOG_ERROR, "Insufficient data for frame.\n");
1234  return AVERROR_INVALIDDATA;
1235  }
1236  while (npixels--)
1237  *frm++ = ctx->codebook[bytestream2_get_byteu(&ctx->gb)];
1238 
1239  return 0;
1240 }
1241 
1242 static int decode_8(SANMVideoContext *ctx)
1243 {
1244  uint16_t *pdest = ctx->frm0;
1245  uint8_t *rsrc;
1246  long npixels = ctx->npixels;
1247 
1248  av_fast_malloc(&ctx->rle_buf, &ctx->rle_buf_size, npixels);
1249  if (!ctx->rle_buf) {
1250  av_log(ctx->avctx, AV_LOG_ERROR, "RLE buffer allocation failed.\n");
1251  return AVERROR(ENOMEM);
1252  }
1253  rsrc = ctx->rle_buf;
1254 
1255  if (rle_decode(ctx, rsrc, npixels))
1256  return AVERROR_INVALIDDATA;
1257 
1258  while (npixels--)
1259  *pdest++ = ctx->codebook[*rsrc++];
1260 
1261  return 0;
1262 }
1263 
1264 typedef int (*frm_decoder)(SANMVideoContext *ctx);
1265 
1266 static const frm_decoder v1_decoders[] = {
1269 };
1270 
1272 {
1273  int i, ret;
1274 
1275  if ((ret = bytestream2_get_bytes_left(&ctx->gb)) < 560) {
1276  av_log(ctx->avctx, AV_LOG_ERROR, "Input frame too short (%d bytes).\n",
1277  ret);
1278  return AVERROR_INVALIDDATA;
1279  }
1280  bytestream2_skip(&ctx->gb, 8); // skip pad
1281 
1282  hdr->width = bytestream2_get_le32u(&ctx->gb);
1283  hdr->height = bytestream2_get_le32u(&ctx->gb);
1284 
1285  if (hdr->width != ctx->width || hdr->height != ctx->height) {
1286  avpriv_report_missing_feature(ctx->avctx, "Variable size frames");
1287  return AVERROR_PATCHWELCOME;
1288  }
1289 
1290  hdr->seq_num = bytestream2_get_le16u(&ctx->gb);
1291  hdr->codec = bytestream2_get_byteu(&ctx->gb);
1292  hdr->rotate_code = bytestream2_get_byteu(&ctx->gb);
1293 
1294  bytestream2_skip(&ctx->gb, 4); // skip pad
1295 
1296  for (i = 0; i < 4; i++)
1297  ctx->small_codebook[i] = bytestream2_get_le16u(&ctx->gb);
1298  hdr->bg_color = bytestream2_get_le16u(&ctx->gb);
1299 
1300  bytestream2_skip(&ctx->gb, 2); // skip pad
1301 
1302  hdr->rle_output_size = bytestream2_get_le32u(&ctx->gb);
1303  for (i = 0; i < 256; i++)
1304  ctx->codebook[i] = bytestream2_get_le16u(&ctx->gb);
1305 
1306  bytestream2_skip(&ctx->gb, 8); // skip pad
1307 
1308  return 0;
1309 }
1310 
1311 static void fill_frame(uint16_t *pbuf, int buf_size, uint16_t color)
1312 {
1313  while (buf_size--)
1314  *pbuf++ = color;
1315 }
1316 
1318 {
1319  uint8_t *dst;
1320  const uint8_t *src = (uint8_t*) ctx->frm0;
1321  int ret, dstpitch, height = ctx->height;
1322  int srcpitch = ctx->pitch * (hdr ? sizeof(ctx->frm0[0]) : 1);
1323 
1324  if ((ret = ff_get_buffer(ctx->avctx, ctx->frame, 0)) < 0)
1325  return ret;
1326 
1327  dst = ctx->frame->data[0];
1328  dstpitch = ctx->frame->linesize[0];
1329 
1330  while (height--) {
1331  memcpy(dst, src, srcpitch);
1332  src += srcpitch;
1333  dst += dstpitch;
1334  }
1335 
1336  return 0;
1337 }
1338 
1339 static int decode_frame(AVCodecContext *avctx, void *data,
1340  int *got_frame_ptr, AVPacket *pkt)
1341 {
1342  SANMVideoContext *ctx = avctx->priv_data;
1343  int i, ret;
1344 
1345  ctx->frame = data;
1346  bytestream2_init(&ctx->gb, pkt->data, pkt->size);
1347 
1348  if (!ctx->version) {
1349  int to_store = 0;
1350 
1351  while (bytestream2_get_bytes_left(&ctx->gb) >= 8) {
1352  uint32_t sig, size;
1353  int pos;
1354 
1355  sig = bytestream2_get_be32u(&ctx->gb);
1356  size = bytestream2_get_be32u(&ctx->gb);
1357  pos = bytestream2_tell(&ctx->gb);
1358 
1359  if (bytestream2_get_bytes_left(&ctx->gb) < size) {
1360  av_log(avctx, AV_LOG_ERROR, "Incorrect chunk size %"PRIu32".\n", size);
1361  break;
1362  }
1363  switch (sig) {
1364  case MKBETAG('N', 'P', 'A', 'L'):
1365  if (size != PALETTE_SIZE * 3) {
1366  av_log(avctx, AV_LOG_ERROR,
1367  "Incorrect palette block size %"PRIu32".\n", size);
1368  return AVERROR_INVALIDDATA;
1369  }
1370  for (i = 0; i < PALETTE_SIZE; i++)
1371  ctx->pal[i] = 0xFFU << 24 | bytestream2_get_be24u(&ctx->gb);
1372  break;
1373  case MKBETAG('F', 'O', 'B', 'J'):
1374  if (size < 16)
1375  return AVERROR_INVALIDDATA;
1376  if (ret = process_frame_obj(ctx))
1377  return ret;
1378  break;
1379  case MKBETAG('X', 'P', 'A', 'L'):
1380  if (size == 6 || size == 4) {
1381  uint8_t tmp[3];
1382  int j;
1383 
1384  for (i = 0; i < PALETTE_SIZE; i++) {
1385  for (j = 0; j < 3; j++) {
1386  int t = (ctx->pal[i] >> (16 - j * 8)) & 0xFF;
1387  tmp[j] = av_clip_uint8((t * 129 + ctx->delta_pal[i * 3 + j]) >> 7);
1388  }
1389  ctx->pal[i] = 0xFFU << 24 | AV_RB24(tmp);
1390  }
1391  } else {
1392  if (size < PALETTE_DELTA * 2 + 4) {
1393  av_log(avctx, AV_LOG_ERROR,
1394  "Incorrect palette change block size %"PRIu32".\n",
1395  size);
1396  return AVERROR_INVALIDDATA;
1397  }
1398  bytestream2_skipu(&ctx->gb, 4);
1399  for (i = 0; i < PALETTE_DELTA; i++)
1400  ctx->delta_pal[i] = bytestream2_get_le16u(&ctx->gb);
1401  if (size >= PALETTE_DELTA * 5 + 4) {
1402  for (i = 0; i < PALETTE_SIZE; i++)
1403  ctx->pal[i] = 0xFFU << 24 | bytestream2_get_be24u(&ctx->gb);
1404  } else {
1405  memset(ctx->pal, 0, sizeof(ctx->pal));
1406  }
1407  }
1408  break;
1409  case MKBETAG('S', 'T', 'O', 'R'):
1410  to_store = 1;
1411  break;
1412  case MKBETAG('F', 'T', 'C', 'H'):
1413  memcpy(ctx->frm0, ctx->stored_frame, ctx->buf_size);
1414  break;
1415  default:
1416  bytestream2_skip(&ctx->gb, size);
1417  av_log(avctx, AV_LOG_DEBUG,
1418  "Unknown/unsupported chunk %"PRIx32".\n", sig);
1419  break;
1420  }
1421 
1422  bytestream2_seek(&ctx->gb, pos + size, SEEK_SET);
1423  if (size & 1)
1424  bytestream2_skip(&ctx->gb, 1);
1425  }
1426  if (to_store)
1427  memcpy(ctx->stored_frame, ctx->frm0, ctx->buf_size);
1428  if ((ret = copy_output(ctx, NULL)))
1429  return ret;
1430  memcpy(ctx->frame->data[1], ctx->pal, 1024);
1431  } else {
1432  SANMFrameHeader header;
1433 
1434  if ((ret = read_frame_header(ctx, &header)))
1435  return ret;
1436 
1437  ctx->rotate_code = header.rotate_code;
1438  if ((ctx->frame->key_frame = !header.seq_num)) {
1440  fill_frame(ctx->frm1, ctx->npixels, header.bg_color);
1441  fill_frame(ctx->frm2, ctx->npixels, header.bg_color);
1442  } else {
1444  }
1445 
1446  if (header.codec < FF_ARRAY_ELEMS(v1_decoders)) {
1447  if ((ret = v1_decoders[header.codec](ctx))) {
1448  av_log(avctx, AV_LOG_ERROR,
1449  "Subcodec %d: error decoding frame.\n", header.codec);
1450  return ret;
1451  }
1452  } else {
1453  avpriv_request_sample(avctx, "Subcodec %d", header.codec);
1454  return AVERROR_PATCHWELCOME;
1455  }
1456 
1457  if ((ret = copy_output(ctx, &header)))
1458  return ret;
1459  }
1460  if (ctx->rotate_code)
1461  rotate_bufs(ctx, ctx->rotate_code);
1462 
1463  *got_frame_ptr = 1;
1464 
1465  return pkt->size;
1466 }
1467 
1469  .name = "sanm",
1470  .long_name = NULL_IF_CONFIG_SMALL("LucasArts SANM video"),
1471  .type = AVMEDIA_TYPE_VIDEO,
1472  .id = AV_CODEC_ID_SANM,
1473  .priv_data_size = sizeof(SANMVideoContext),
1474  .init = decode_init,
1475  .close = decode_end,
1476  .decode = decode_frame,
1477  .capabilities = CODEC_CAP_DR1,
1478 };
Definition: sanm.c:264
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:54
static int process_frame_obj(SANMVideoContext *ctx)
Definition: sanm.c:899
int size
This structure describes decoded (raw) audio or video data.
Definition: frame.h:135
static int read_frame_header(SANMVideoContext *ctx, SANMFrameHeader *hdr)
Definition: sanm.c:1271
int8_t p4x4glyphs[NGLYPHS][16]
Definition: sanm.c:241
int aligned_height
Definition: sanm.c:222
misc image utilities
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:129
static void fill_frame(uint16_t *pbuf, int buf_size, uint16_t color)
Definition: sanm.c:1311
int ff_set_dimensions(AVCodecContext *s, int width, int height)
Check that the provided frame dimensions are valid and set them on the codec context.
Definition: utils.c:133
static void copy_block16(uint8_t *dst, const uint8_t *src, int dstStride, int srcStride, int h)
Definition: copy_block.h:57
Definition: vf_drawbox.c:37
uint32_t pal[PALETTE_SIZE]
Definition: sanm.c:217
int size
Definition: avcodec.h:974
static int opcode_0xf8(SANMVideoContext *ctx, int cx, int cy, int block_size, int pitch)
Definition: sanm.c:1051
static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame_ptr, AVPacket *pkt)
Definition: sanm.c:1339
uint8_t * stored_frame
Definition: sanm.c:227
#define av_bswap16
Definition: bswap.h:31
#define AV_RB24
Definition: intreadwrite.h:64
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1270
static av_always_inline void bytestream2_init(GetByteContext *g, const uint8_t *buf, int buf_size)
Definition: bytestream.h:132
GlyphEdge
Definition: sanm.c:252
#define AV_RL16
Definition: intreadwrite.h:42
void av_fast_padded_malloc(void *ptr, unsigned int *size, size_t min_size)
Allocate a buffer with padding, reusing the given one if large enough.
Definition: utils.c:59
static enum GlyphEdge which_edge(int x, int y, int edge_size)
Return enum GlyphEdge of box where point (x, y) lies.
Definition: sanm.c:275
Definition: sanm.c:261
Definition: sanm.c:257
#define FF_ARRAY_ELEMS(a)
static const int8_t glyph4_x[GLYPH_COORD_VECT_SIZE]
Definition: sanm.c:37
uint32_t width
Definition: sanm.c:249
uint16_t * frm0
Definition: sanm.c:226
int stride
Definition: mace.c:144
AVCodec.
Definition: avcodec.h:2812
static const int8_t glyph8_y[GLYPH_COORD_VECT_SIZE]
Definition: sanm.c:49
static av_always_inline unsigned int bytestream2_get_bufferu(GetByteContext *g, uint8_t *dst, unsigned int size)
Definition: bytestream.h:270
#define FFALIGN(x, a)
Definition: common.h:62
Definition: sanm.c:265
void av_freep(void *arg)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
Definition: mem.c:198
static const uint8_t run_len[7][16]
Definition: h264_cavlc.c:218
int rotate_code
Definition: sanm.c:246
static int decode(MimicContext *ctx, int quality, int num_coeffs, int is_iframe)
Definition: mimic.c:275
void void avpriv_request_sample(void *avc, const char *msg,...) av_printf_format(2
Log a generic warning message about a missing feature.
static int draw_glyph(SANMVideoContext *ctx, uint16_t *dst, int index, uint16_t fg_color, uint16_t bg_color, int block_size, int pitch)
Definition: sanm.c:995
uint8_t
#define av_cold
Definition: attributes.h:66
static av_cold int decode_init(AVCodecContext *avctx)
Definition: sanm.c:437
#define av_assert2(cond)
assert() equivalent, that does lie in speed critical code.
Definition: avassert.h:63
8 bit with PIX_FMT_RGB32 palette
Definition: pixfmt.h:76
static int good_mvec(SANMVideoContext *ctx, int cx, int cy, int mx, int my, int block_size)
Definition: sanm.c:1079
uint16_t bg_color
Definition: sanm.c:248
static void codec37_mv(uint8_t *dst, const uint8_t *src, int height, int stride, int x, int y)
Definition: sanm.c:553
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:1164
AVFrame * frame
Definition: sanm.c:225
static const int8_t glyph4_y[GLYPH_COORD_VECT_SIZE]
Definition: sanm.c:41
#define CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
Definition: avcodec.h:684
const char data[16]
Definition: mxf.c:70
uint8_t * data
Definition: avcodec.h:973
static int flags
Definition: log.c:44
static av_always_inline void bytestream2_skipu(GetByteContext *g, unsigned int size)
Definition: bytestream.h:167
GetByteContext gb
Definition: sanm.c:214
static const int8_t motion_vectors[256][2]
Definition: sanm.c:53
static int decode_5(SANMVideoContext *ctx)
Definition: sanm.c:1204
uint16_t small_codebook[4]
Definition: sanm.c:239
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:123
uint32_t height
Definition: sanm.c:249
static av_cold int decode_end(AVCodecContext *avctx)
Definition: sanm.c:471
#define AVERROR(e)
Definition: error.h:43
static av_always_inline void bytestream2_skip(GetByteContext *g, unsigned int size)
Definition: bytestream.h:161
static int rle_decode(SANMVideoContext *ctx, uint8_t *dst, const int out_size)
Definition: sanm.c:480
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:145
static av_always_inline unsigned int bytestream2_get_buffer(GetByteContext *g, uint8_t *dst, unsigned int size)
Definition: bytestream.h:260
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:144
int rle_output_size
Definition: sanm.c:246
int subversion
Definition: sanm.c:216
static av_always_inline unsigned int bytestream2_get_bytes_left(GetByteContext *g)
Definition: bytestream.h:151
uint16_t * frm1
Definition: sanm.c:226
long npixels
Definition: sanm.c:236
AVCodecContext * avctx
Definition: sanm.c:213
simple assert() macros that are a bit more flexible than ISO C assert().
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:169
static int decode_3(SANMVideoContext *ctx)
Definition: sanm.c:1192
const char * name
Name of the codec implementation.
Definition: avcodec.h:2819
static enum GlyphDir which_direction(enum GlyphEdge edge0, enum GlyphEdge edge1)
Definition: sanm.c:291
static void make_glyphs(int8_t *pglyphs, const int8_t *xvec, const int8_t *yvec, const int side_length)
Construct glyphs by iterating through vector coordinates.
Definition: sanm.c:334
uint32_t frm1_size
Definition: sanm.c:228
#define FFMAX(a, b)
Definition: common.h:55
uint32_t stored_frame_size
Definition: sanm.c:229
uint32_t frm2_size
Definition: sanm.c:228
int aligned_width
Definition: sanm.c:222
enum AVPictureType pict_type
Picture type of the frame.
Definition: frame.h:196
GlyphDir
Definition: sanm.c:260
#define PALETTE_DELTA
Definition: sanm.c:35
int width
picture width / height.
Definition: avcodec.h:1229
static void destroy_buffers(SANMVideoContext *ctx)
Definition: sanm.c:399
static void interp_point(int8_t *points, int x0, int y0, int x1, int y1, int pos, int npoints)
Definition: sanm.c:314
static void copy_block(uint16_t *pdest, uint16_t *psrc, int block_size, int pitch)
Definition: sanm.c:966
static int old_codec47(SANMVideoContext *ctx, int top, int left, int width, int height)
Definition: sanm.c:808
#define FFABS(a)
Definition: common.h:52
int seq_num
Definition: sanm.c:246
static int copy_output(SANMVideoContext *ctx, SANMFrameHeader *hdr)
Definition: sanm.c:1317
Definition: sanm.c:254
#define AV_RL32
Definition: intreadwrite.h:146
if(ac->has_optimized_func)
#define AVERROR_PATCHWELCOME
Not yet implemented in Libav, patches welcome.
Definition: error.h:57
static av_cold int init_buffers(SANMVideoContext *ctx)
Definition: sanm.c:412
static av_always_inline int bytestream2_tell(GetByteContext *g)
Definition: bytestream.h:185
NULL
Definition: eval.c:55
static int width
Definition: utils.c:156
static const int8_t c37_mv[]
Definition: sanm.c:107
Libavcodec external API header.
#define GLYPH_COORD_VECT_SIZE
Definition: sanm.c:33
static int decode_nop(SANMVideoContext *ctx)
Definition: sanm.c:960
static int decode_6(SANMVideoContext *ctx)
Definition: sanm.c:1227
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:153
main external API structure.
Definition: avcodec.h:1050
static int codec2subblock(SANMVideoContext *ctx, int cx, int cy, int blk_size)
Definition: sanm.c:1095
static void close(AVCodecParserContext *s)
Definition: h264_parser.c:490
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: utils.c:612
static int process_block(SANMVideoContext *ctx, uint8_t *dst, uint8_t *prev1, uint8_t *prev2, int stride, int tbl, int size)
Definition: sanm.c:718
static int opcode_0xf7(SANMVideoContext *ctx, int cx, int cy, int block_size, int pitch)
Definition: sanm.c:1017
int extradata_size
Definition: avcodec.h:1165
static int decode_2(SANMVideoContext *ctx)
Definition: sanm.c:1180
#define NGLYPHS
Definition: sanm.c:32
int index
Definition: gxfenc.c:72
byte swapping routines
static int decode_8(SANMVideoContext *ctx)
Definition: sanm.c:1242
static int decode_0(SANMVideoContext *ctx)
Definition: sanm.c:943
uint16_t * frm2
Definition: sanm.c:226
static void rotate_bufs(SANMVideoContext *ctx, int rotate_code)
Definition: sanm.c:430
void avpriv_report_missing_feature(void *avc, const char *msg,...) av_printf_format(2
Log a generic warning message about a missing feature.
static const frm_decoder v1_decoders[]
Definition: sanm.c:1266
Definition: sanm.c:262
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:141
static void fill_block(uint16_t *pdest, uint16_t color, int block_size, int pitch)
Definition: sanm.c:985
int height
Definition: gxfenc.c:72
#define PALETTE_SIZE
Definition: sanm.c:34
unsigned int rle_buf_size
Definition: sanm.c:232
void av_fast_malloc(void *ptr, unsigned int *size, size_t min_size)
Allocate a buffer, reusing the given one if large enough.
Definition: mem.c:388
static int old_codec37(SANMVideoContext *ctx, int top, int left, int width, int height)
Definition: sanm.c:572
uint16_t codebook[256]
Definition: sanm.c:238
common internal api header.
static void init_sizes(SANMVideoContext *ctx, int width, int height)
Definition: sanm.c:386
static const uint8_t color[]
Definition: log.c:55
static av_cold int init(AVCodecParserContext *s)
Definition: h264_parser.c:499
long buf_size
Definition: sanm.c:236
#define MKBETAG(a, b, c, d)
Definition: common.h:239
void * priv_data
Definition: avcodec.h:1092
static const int8_t glyph8_x[GLYPH_COORD_VECT_SIZE]
Definition: sanm.c:45
static int decode_4(SANMVideoContext *ctx)
Definition: sanm.c:1198
int16_t delta_pal[PALETTE_DELTA]
Definition: sanm.c:218
int len
static void copy_block4(uint8_t *dst, const uint8_t *src, int dstStride, int srcStride, int h)
Definition: copy_block.h:26
int key_frame
1 -> keyframe, 0-> not
Definition: frame.h:191
static av_always_inline int bytestream2_seek(GetByteContext *g, int offset, int whence)
Definition: bytestream.h:205
static void copy_block8(uint8_t *dst, const uint8_t *src, int dstStride, int srcStride, int h)
Definition: copy_block.h:36
AVCodec ff_sanm_decoder
Definition: sanm.c:1468
#define AV_PIX_FMT_RGB565
Definition: pixfmt.h:230
int prev_seq
Definition: sanm.c:223
static int old_codec1(SANMVideoContext *ctx, int top, int left, int width, int height)
Definition: sanm.c:506
int rotate_code
Definition: sanm.c:234
int8_t p8x8glyphs[NGLYPHS][64]
Definition: sanm.c:242
uint32_t frm0_size
Definition: sanm.c:228
#define FFSWAP(type, a, b)
Definition: common.h:60
uint8_t * rle_buf
Definition: sanm.c:231
This structure stores compressed data.
Definition: avcodec.h:950
Predicted.
Definition: avutil.h:254
int(* frm_decoder)(SANMVideoContext *ctx)
Definition: sanm.c:1264