]> git.sesse.net Git - ffmpeg/blob - libavcodec/sanm.c
lavu: Remove bit packing from AVComponentDescriptor
[ffmpeg] / libavcodec / sanm.c
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 {
213     AVCodecContext *avctx;
214     GetByteContext gb;
215
216     int version, subversion;
217     uint32_t pal[PALETTE_SIZE];
218     int16_t delta_pal[PALETTE_DELTA];
219
220     int pitch;
221     int width, height;
222     int aligned_width, aligned_height;
223     int prev_seq;
224
225     AVFrame *frame;
226     uint16_t *frm0, *frm1, *frm2;
227     uint8_t *stored_frame;
228     uint32_t frm0_size, frm1_size, frm2_size;
229     uint32_t stored_frame_size;
230
231     uint8_t *rle_buf;
232     unsigned int rle_buf_size;
233
234     int rotate_code;
235
236     long npixels, buf_size;
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];
243 } SANMVideoContext;
244
245 typedef struct SANMFrameHeader {
246     int seq_num, codec, rotate_code, rle_output_size;
247
248     uint16_t bg_color;
249     uint32_t width, height;
250 } SANMFrameHeader;
251
252 enum GlyphEdge {
253     LEFT_EDGE,
254     TOP_EDGE,
255     RIGHT_EDGE,
256     BOTTOM_EDGE,
257     NO_EDGE
258 };
259
260 enum GlyphDir {
261     DIR_LEFT,
262     DIR_UP,
263     DIR_RIGHT,
264     DIR_DOWN,
265     NO_DIR
266 };
267
268 /**
269  * Return enum GlyphEdge of box where point (x, y) lies.
270  *
271  * @param x x point coordinate
272  * @param y y point coordinate
273  * @param edge_size box width/height.
274  */
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
326 /**
327  * Construct glyphs by iterating through vector coordinates.
328  *
329  * @param pglyphs pointer to table where glyphs are stored
330  * @param xvec pointer to x component of vector coordinates
331  * @param yvec pointer to y component of vector coordinates
332  * @param side_length glyph width/height.
333  */
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
399 static void destroy_buffers(SANMVideoContext *ctx)
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 }
410
411 static av_cold int init_buffers(SANMVideoContext *ctx)
412 {
413     av_fast_padded_malloc(&ctx->frm0, &ctx->frm0_size, ctx->buf_size);
414     av_fast_padded_malloc(&ctx->frm1, &ctx->frm1_size, ctx->buf_size);
415     av_fast_padded_malloc(&ctx->frm2, &ctx->frm2_size, ctx->buf_size);
416     if (!ctx->version)
417         av_fast_padded_malloc(&ctx->stored_frame,
418                               &ctx->stored_frame_size, ctx->buf_size);
419
420     if (!ctx->frm0 || !ctx->frm1 || !ctx->frm2 ||
421         (!ctx->stored_frame && !ctx->version)) {
422         destroy_buffers(ctx);
423         return AVERROR(ENOMEM);
424     }
425
426     return 0;
427 }
428
429 static void rotate_bufs(SANMVideoContext *ctx, int rotate_code)
430 {
431     if (rotate_code == 2)
432         FFSWAP(uint16_t*, ctx->frm1, ctx->frm2);
433     FFSWAP(uint16_t*, ctx->frm2, ctx->frm0);
434 }
435
436 static av_cold int decode_init(AVCodecContext *avctx)
437 {
438     SANMVideoContext *ctx = avctx->priv_data;
439
440     ctx->avctx   = avctx;
441     ctx->version = !avctx->extradata_size;
442
443     avctx->pix_fmt = ctx->version ? AV_PIX_FMT_RGB565 : AV_PIX_FMT_PAL8;
444
445     init_sizes(ctx, avctx->width, avctx->height);
446     if (init_buffers(ctx)) {
447         av_log(avctx, AV_LOG_ERROR, "Error allocating buffers.\n");
448         return AVERROR(ENOMEM);
449     }
450
451     make_glyphs(ctx->p4x4glyphs[0], glyph4_x, glyph4_y, 4);
452     make_glyphs(ctx->p8x8glyphs[0], glyph8_x, glyph8_y, 8);
453
454     if (!ctx->version) {
455         int i;
456
457         if (avctx->extradata_size < 1026) {
458             av_log(avctx, AV_LOG_ERROR, "Not enough extradata.\n");
459             return AVERROR_INVALIDDATA;
460         }
461
462         ctx->subversion = AV_RL16(avctx->extradata);
463         for (i = 0; i < 256; i++)
464             ctx->pal[i] = 0xFFU << 24 | AV_RL32(avctx->extradata + 2 + i * 4);
465     }
466
467     return 0;
468 }
469
470 static av_cold int decode_end(AVCodecContext *avctx)
471 {
472     SANMVideoContext *ctx = avctx->priv_data;
473
474     destroy_buffers(ctx);
475
476     return 0;
477 }
478
479 static int rle_decode(SANMVideoContext *ctx, uint8_t *dst, const int out_size)
480 {
481     int opcode, color, run_len, left = out_size;
482
483     while (left > 0) {
484         opcode = bytestream2_get_byte(&ctx->gb);
485         run_len = (opcode >> 1) + 1;
486         if (run_len > left || bytestream2_get_bytes_left(&ctx->gb) <= 0)
487             return AVERROR_INVALIDDATA;
488
489         if (opcode & 1) {
490             color = bytestream2_get_byte(&ctx->gb);
491             memset(dst, color, run_len);
492         } else {
493             if (bytestream2_get_bytes_left(&ctx->gb) < run_len)
494                 return AVERROR_INVALIDDATA;
495             bytestream2_get_bufferu(&ctx->gb, dst, run_len);
496         }
497
498         dst  += run_len;
499         left -= run_len;
500     }
501
502     return 0;
503 }
504
505 static int old_codec1(SANMVideoContext *ctx, int top,
506                       int left, int width, int height)
507 {
508     uint8_t *dst = ((uint8_t *)ctx->frm0) + left + top * ctx->pitch;
509     int i, j, len, flag, code, val, pos, end;
510
511     for (i = 0; i < height; i++) {
512         pos = 0;
513
514         if (bytestream2_get_bytes_left(&ctx->gb) < 2)
515             return AVERROR_INVALIDDATA;
516
517         len = bytestream2_get_le16u(&ctx->gb);
518         end = bytestream2_tell(&ctx->gb) + len;
519
520         while (bytestream2_tell(&ctx->gb) < end) {
521             if (bytestream2_get_bytes_left(&ctx->gb) < 2)
522                 return AVERROR_INVALIDDATA;
523
524             code = bytestream2_get_byteu(&ctx->gb);
525             flag = code & 1;
526             code = (code >> 1) + 1;
527             if (pos + code > width)
528                 return AVERROR_INVALIDDATA;
529             if (flag) {
530                 val = bytestream2_get_byteu(&ctx->gb);
531                 if (val)
532                     memset(dst + pos, val, code);
533                 pos += code;
534             } else {
535                 if (bytestream2_get_bytes_left(&ctx->gb) < code)
536                     return AVERROR_INVALIDDATA;
537                 for (j = 0; j < code; j++) {
538                     val = bytestream2_get_byteu(&ctx->gb);
539                     if (val)
540                         dst[pos] = val;
541                     pos++;
542                 }
543             }
544         }
545         dst += ctx->pitch;
546     }
547     ctx->rotate_code = 0;
548
549     return 0;
550 }
551
552 static inline void codec37_mv(uint8_t *dst, const uint8_t *src,
553                               int height, int stride, int x, int y)
554 {
555     int pos, i, j;
556
557     pos = x + y * stride;
558     for (j = 0; j < 4; j++) {
559         for (i = 0; i < 4; i++) {
560             if ((pos + i) < 0 || (pos + i) >= height * stride)
561                 dst[i] = 0;
562             else
563                 dst[i] = src[i];
564         }
565         dst += stride;
566         src += stride;
567         pos += stride;
568     }
569 }
570
571 static int old_codec37(SANMVideoContext *ctx, int top,
572                        int left, int width, int height)
573 {
574     int stride = ctx->pitch;
575     int i, j, k, t;
576     uint8_t *dst, *prev;
577     int skip_run = 0;
578     int compr = bytestream2_get_byte(&ctx->gb);
579     int mvoff = bytestream2_get_byte(&ctx->gb);
580     int seq   = bytestream2_get_le16(&ctx->gb);
581     uint32_t decoded_size = bytestream2_get_le32(&ctx->gb);
582     int flags;
583
584     bytestream2_skip(&ctx->gb, 4);
585     flags = bytestream2_get_byte(&ctx->gb);
586     bytestream2_skip(&ctx->gb, 3);
587
588     if (decoded_size > ctx->height * stride - left - top * stride) {
589         decoded_size = ctx->height * stride - left - top * stride;
590         av_log(ctx->avctx, AV_LOG_WARNING, "Decoded size is too large.\n");
591     }
592
593     ctx->rotate_code = 0;
594
595     if (((seq & 1) || !(flags & 1)) && (compr && compr != 2))
596         rotate_bufs(ctx, 1);
597
598     dst  = ((uint8_t*)ctx->frm0) + left + top * stride;
599     prev = ((uint8_t*)ctx->frm2) + left + top * stride;
600
601     if (mvoff > 2) {
602         av_log(ctx->avctx, AV_LOG_ERROR, "Invalid motion base value %d.\n", mvoff);
603         return AVERROR_INVALIDDATA;
604     }
605
606     switch (compr) {
607     case 0:
608         for (i = 0; i < height; i++) {
609             bytestream2_get_buffer(&ctx->gb, dst, width);
610             dst += stride;
611         }
612         memset(ctx->frm1, 0, ctx->height * stride);
613         memset(ctx->frm2, 0, ctx->height * stride);
614         break;
615     case 2:
616         if (rle_decode(ctx, dst, decoded_size))
617             return AVERROR_INVALIDDATA;
618         memset(ctx->frm1, 0, ctx->frm1_size);
619         memset(ctx->frm2, 0, ctx->frm2_size);
620         break;
621     case 3:
622     case 4:
623         if (flags & 4) {
624             for (j = 0; j < height; j += 4) {
625                 for (i = 0; i < width; i += 4) {
626                     int code;
627                     if (skip_run) {
628                         skip_run--;
629                         copy_block4(dst + i, prev + i, stride, stride, 4);
630                         continue;
631                     }
632                     if (bytestream2_get_bytes_left(&ctx->gb) < 1)
633                         return AVERROR_INVALIDDATA;
634                     code = bytestream2_get_byteu(&ctx->gb);
635                     switch (code) {
636                     case 0xFF:
637                         if (bytestream2_get_bytes_left(&ctx->gb) < 16)
638                             return AVERROR_INVALIDDATA;
639                         for (k = 0; k < 4; k++)
640                             bytestream2_get_bufferu(&ctx->gb, dst + i + k * stride, 4);
641                         break;
642                     case 0xFE:
643                         if (bytestream2_get_bytes_left(&ctx->gb) < 4)
644                             return AVERROR_INVALIDDATA;
645                         for (k = 0; k < 4; k++)
646                             memset(dst + i + k * stride, bytestream2_get_byteu(&ctx->gb), 4);
647                         break;
648                     case 0xFD:
649                         if (bytestream2_get_bytes_left(&ctx->gb) < 1)
650                             return AVERROR_INVALIDDATA;
651                         t = bytestream2_get_byteu(&ctx->gb);
652                         for (k = 0; k < 4; k++)
653                             memset(dst + i + k * stride, t, 4);
654                         break;
655                     default:
656                         if (compr == 4 && !code) {
657                             if (bytestream2_get_bytes_left(&ctx->gb) < 1)
658                                 return AVERROR_INVALIDDATA;
659                             skip_run = bytestream2_get_byteu(&ctx->gb) + 1;
660                             i -= 4;
661                         } else {
662                             int mx, my;
663
664                             mx = c37_mv[(mvoff * 255 + code) * 2];
665                             my = c37_mv[(mvoff * 255 + code) * 2 + 1];
666                             codec37_mv(dst + i, prev + i + mx + my * stride,
667                                        ctx->height, stride, i + mx, j + my);
668                         }
669                     }
670                 }
671                 dst  += stride * 4;
672                 prev += stride * 4;
673             }
674         } else {
675             for (j = 0; j < height; j += 4) {
676                 for (i = 0; i < width; i += 4) {
677                     int code;
678                     if (skip_run) {
679                         skip_run--;
680                         copy_block4(dst + i, prev + i, stride, stride, 4);
681                         continue;
682                     }
683                     code = bytestream2_get_byte(&ctx->gb);
684                     if (code == 0xFF) {
685                         if (bytestream2_get_bytes_left(&ctx->gb) < 16)
686                             return AVERROR_INVALIDDATA;
687                         for (k = 0; k < 4; k++)
688                             bytestream2_get_bufferu(&ctx->gb, dst + i + k * stride, 4);
689                     } else if (compr == 4 && !code) {
690                         if (bytestream2_get_bytes_left(&ctx->gb) < 1)
691                             return AVERROR_INVALIDDATA;
692                         skip_run = bytestream2_get_byteu(&ctx->gb) + 1;
693                         i -= 4;
694                     } else {
695                         int mx, my;
696
697                         mx = c37_mv[(mvoff * 255 + code) * 2];
698                         my = c37_mv[(mvoff * 255 + code) * 2 + 1];
699                         codec37_mv(dst + i, prev + i + mx + my * stride,
700                                    ctx->height, stride, i + mx, j + my);
701                     }
702                 }
703                 dst  += stride * 4;
704                 prev += stride * 4;
705             }
706         }
707         break;
708     default:
709         avpriv_report_missing_feature(ctx->avctx,
710                                       "Subcodec 37 compression %d", compr);
711         return AVERROR_PATCHWELCOME;
712     }
713
714     return 0;
715 }
716
717 static int process_block(SANMVideoContext *ctx, uint8_t *dst, uint8_t *prev1,
718                          uint8_t *prev2, int stride, int tbl, int size)
719 {
720     int code, k, t;
721     uint8_t colors[2];
722     int8_t *pglyph;
723
724     if (bytestream2_get_bytes_left(&ctx->gb) < 1)
725         return AVERROR_INVALIDDATA;
726
727     code = bytestream2_get_byteu(&ctx->gb);
728     if (code >= 0xF8) {
729         switch (code) {
730         case 0xFF:
731             if (size == 2) {
732                 if (bytestream2_get_bytes_left(&ctx->gb) < 4)
733                     return AVERROR_INVALIDDATA;
734                 dst[0]          = bytestream2_get_byteu(&ctx->gb);
735                 dst[1]          = bytestream2_get_byteu(&ctx->gb);
736                 dst[0 + stride] = bytestream2_get_byteu(&ctx->gb);
737                 dst[1 + stride] = bytestream2_get_byteu(&ctx->gb);
738             } else {
739                 size >>= 1;
740                 if (process_block(ctx, dst, prev1, prev2, stride, tbl, size))
741                     return AVERROR_INVALIDDATA;
742                 if (process_block(ctx, dst + size, prev1 + size, prev2 + size,
743                                   stride, tbl, size))
744                     return AVERROR_INVALIDDATA;
745                 dst   += size * stride;
746                 prev1 += size * stride;
747                 prev2 += size * stride;
748                 if (process_block(ctx, dst, prev1, prev2, stride, tbl, size))
749                     return AVERROR_INVALIDDATA;
750                 if (process_block(ctx, dst + size, prev1 + size, prev2 + size,
751                                   stride, tbl, size))
752                     return AVERROR_INVALIDDATA;
753             }
754             break;
755         case 0xFE:
756             if (bytestream2_get_bytes_left(&ctx->gb) < 1)
757                 return AVERROR_INVALIDDATA;
758
759             t = bytestream2_get_byteu(&ctx->gb);
760             for (k = 0; k < size; k++)
761                 memset(dst + k * stride, t, size);
762             break;
763         case 0xFD:
764             if (bytestream2_get_bytes_left(&ctx->gb) < 3)
765                 return AVERROR_INVALIDDATA;
766
767             code = bytestream2_get_byteu(&ctx->gb);
768             pglyph = (size == 8) ? ctx->p8x8glyphs[code] : ctx->p4x4glyphs[code];
769             bytestream2_get_bufferu(&ctx->gb, colors, 2);
770
771             for (k = 0; k < size; k++)
772                 for (t = 0; t < size; t++)
773                     dst[t + k * stride] = colors[!*pglyph++];
774             break;
775         case 0xFC:
776             for (k = 0; k < size; k++)
777                 memcpy(dst + k * stride, prev1 + k * stride, size);
778             break;
779         default:
780             k = bytestream2_tell(&ctx->gb);
781             bytestream2_seek(&ctx->gb, tbl + (code & 7), SEEK_SET);
782             t = bytestream2_get_byte(&ctx->gb);
783             bytestream2_seek(&ctx->gb, k, SEEK_SET);
784             for (k = 0; k < size; k++)
785                 memset(dst + k * stride, t, size);
786         }
787     } else {
788         int mx = motion_vectors[code][0];
789         int my = motion_vectors[code][1];
790         int index = prev2 - (const uint8_t *)ctx->frm2;
791
792         av_assert2(index >= 0 && index < (ctx->buf_size >> 1));
793
794         if (index < -mx - my * stride ||
795             (ctx->buf_size >> 1) - index < mx + size + (my + size - 1) * stride) {
796             av_log(ctx->avctx, AV_LOG_ERROR, "MV is invalid.\n");
797             return AVERROR_INVALIDDATA;
798         }
799
800         for (k = 0; k < size; k++)
801             memcpy(dst + k * stride, prev2 + mx + (my + k) * stride, size);
802     }
803
804     return 0;
805 }
806
807 static int old_codec47(SANMVideoContext *ctx, int top,
808                        int left, int width, int height)
809 {
810     uint32_t decoded_size;
811     int i, j;
812     int stride     = ctx->pitch;
813     uint8_t *dst   = (uint8_t *)ctx->frm0 + left + top * stride;
814     uint8_t *prev1 = (uint8_t *)ctx->frm1;
815     uint8_t *prev2 = (uint8_t *)ctx->frm2;
816     int tbl_pos = bytestream2_tell(&ctx->gb);
817     int seq     = bytestream2_get_le16(&ctx->gb);
818     int compr   = bytestream2_get_byte(&ctx->gb);
819     int new_rot = bytestream2_get_byte(&ctx->gb);
820     int skip    = bytestream2_get_byte(&ctx->gb);
821
822     bytestream2_skip(&ctx->gb, 9);
823     decoded_size = bytestream2_get_le32(&ctx->gb);
824     bytestream2_skip(&ctx->gb, 8);
825
826     if (decoded_size > ctx->height * stride - left - top * stride) {
827         decoded_size = ctx->height * stride - left - top * stride;
828         av_log(ctx->avctx, AV_LOG_WARNING, "Decoded size is too large.\n");
829     }
830
831     if (skip & 1)
832         bytestream2_skip(&ctx->gb, 0x8080);
833     if (!seq) {
834         ctx->prev_seq = -1;
835         memset(prev1, 0, ctx->height * stride);
836         memset(prev2, 0, ctx->height * stride);
837     }
838
839     switch (compr) {
840     case 0:
841         if (bytestream2_get_bytes_left(&ctx->gb) < width * height)
842             return AVERROR_INVALIDDATA;
843         for (j = 0; j < height; j++) {
844             bytestream2_get_bufferu(&ctx->gb, dst, width);
845             dst += stride;
846         }
847         break;
848     case 1:
849         if (bytestream2_get_bytes_left(&ctx->gb) < ((width + 1) >> 1) * ((height + 1) >> 1))
850             return AVERROR_INVALIDDATA;
851         for (j = 0; j < height; j += 2) {
852             for (i = 0; i < width; i += 2) {
853                 dst[i] =
854                 dst[i + 1] =
855                 dst[stride + i] =
856                 dst[stride + i + 1] = bytestream2_get_byteu(&ctx->gb);
857             }
858             dst += stride * 2;
859         }
860         break;
861     case 2:
862         if (seq == ctx->prev_seq + 1) {
863             for (j = 0; j < height; j += 8) {
864                 for (i = 0; i < width; i += 8)
865                     if (process_block(ctx, dst + i, prev1 + i, prev2 + i, stride,
866                                       tbl_pos + 8, 8))
867                         return AVERROR_INVALIDDATA;
868                 dst   += stride * 8;
869                 prev1 += stride * 8;
870                 prev2 += stride * 8;
871             }
872         }
873         break;
874     case 3:
875         memcpy(ctx->frm0, ctx->frm2, ctx->pitch * ctx->height);
876         break;
877     case 4:
878         memcpy(ctx->frm0, ctx->frm1, ctx->pitch * ctx->height);
879         break;
880     case 5:
881         if (rle_decode(ctx, dst, decoded_size))
882             return AVERROR_INVALIDDATA;
883         break;
884     default:
885         avpriv_report_missing_feature(ctx->avctx,
886                                       "Subcodec 47 compression %d", compr);
887         return AVERROR_PATCHWELCOME;
888     }
889     if (seq == ctx->prev_seq + 1)
890         ctx->rotate_code = new_rot;
891     else
892         ctx->rotate_code = 0;
893     ctx->prev_seq = seq;
894
895     return 0;
896 }
897
898 static int process_frame_obj(SANMVideoContext *ctx)
899 {
900     uint16_t codec = bytestream2_get_le16u(&ctx->gb);
901     uint16_t left  = bytestream2_get_le16u(&ctx->gb);
902     uint16_t top   = bytestream2_get_le16u(&ctx->gb);
903     uint16_t w     = bytestream2_get_le16u(&ctx->gb);
904     uint16_t h     = bytestream2_get_le16u(&ctx->gb);
905
906     if (!w || !h) {
907         av_log(ctx->avctx, AV_LOG_ERROR, "Dimensions are invalid.\n");
908         return AVERROR_INVALIDDATA;
909     }
910
911     if (ctx->width < left + w || ctx->height < top + h) {
912         int ret = ff_set_dimensions(ctx->avctx, FFMAX(left + w, ctx->width),
913                                     FFMAX(top + h, ctx->height));
914         if (ret < 0)
915             return ret;
916         init_sizes(ctx, FFMAX(left + w, ctx->width),
917                    FFMAX(top + h, ctx->height));
918         if (init_buffers(ctx)) {
919             av_log(ctx->avctx, AV_LOG_ERROR, "Error resizing buffers.\n");
920             return AVERROR(ENOMEM);
921         }
922     }
923     bytestream2_skip(&ctx->gb, 4);
924
925     switch (codec) {
926     case 1:
927     case 3:
928         return old_codec1(ctx, top, left, w, h);
929         break;
930     case 37:
931         return old_codec37(ctx, top, left, w, h);
932         break;
933     case 47:
934         return old_codec47(ctx, top, left, w, h);
935         break;
936     default:
937         avpriv_request_sample(ctx->avctx, "Subcodec %d", codec);
938         return AVERROR_PATCHWELCOME;
939     }
940 }
941
942 static int decode_0(SANMVideoContext *ctx)
943 {
944     uint16_t *frm = ctx->frm0;
945     int x, y;
946
947     if (bytestream2_get_bytes_left(&ctx->gb) < ctx->width * ctx->height * 2) {
948         av_log(ctx->avctx, AV_LOG_ERROR, "Insufficient data for raw frame.\n");
949         return AVERROR_INVALIDDATA;
950     }
951     for (y = 0; y < ctx->height; y++) {
952         for (x = 0; x < ctx->width; x++)
953             frm[x] = bytestream2_get_le16u(&ctx->gb);
954         frm += ctx->pitch;
955     }
956     return 0;
957 }
958
959 static int decode_nop(SANMVideoContext *ctx)
960 {
961     avpriv_request_sample(ctx->avctx, "Unknown/unsupported compression type");
962     return AVERROR_PATCHWELCOME;
963 }
964
965 static void copy_block(uint16_t *pdest, uint16_t *psrc, int block_size, int pitch)
966 {
967     uint8_t *dst = (uint8_t *)pdest;
968     uint8_t *src = (uint8_t *)psrc;
969     int stride = pitch * 2;
970
971     switch (block_size) {
972     case 2:
973         copy_block4(dst, src, stride, stride, 2);
974         break;
975     case 4:
976         copy_block8(dst, src, stride, stride, 4);
977         break;
978     case 8:
979         copy_block16(dst, src, stride, stride, 8);
980         break;
981     }
982 }
983
984 static void fill_block(uint16_t *pdest, uint16_t color, int block_size, int pitch)
985 {
986     int x, y;
987
988     pitch -= block_size;
989     for (y = 0; y < block_size; y++, pdest += pitch)
990         for (x = 0; x < block_size; x++)
991             *pdest++ = color;
992 }
993
994 static int draw_glyph(SANMVideoContext *ctx, uint16_t *dst, int index,
995                       uint16_t fg_color, uint16_t bg_color, int block_size,
996                       int pitch)
997 {
998     int8_t *pglyph;
999     uint16_t colors[2] = { fg_color, bg_color };
1000     int x, y;
1001
1002     if (index >= NGLYPHS) {
1003         av_log(ctx->avctx, AV_LOG_ERROR, "Ignoring nonexistent glyph #%u.\n", index);
1004         return AVERROR_INVALIDDATA;
1005     }
1006
1007     pglyph = block_size == 8 ? ctx->p8x8glyphs[index] : ctx->p4x4glyphs[index];
1008     pitch -= block_size;
1009
1010     for (y = 0; y < block_size; y++, dst += pitch)
1011         for (x = 0; x < block_size; x++)
1012             *dst++ = colors[*pglyph++];
1013     return 0;
1014 }
1015
1016 static int opcode_0xf7(SANMVideoContext *ctx, int cx, int cy, int block_size, int pitch)
1017 {
1018     uint16_t *dst = ctx->frm0 + cx + cy * ctx->pitch;
1019
1020     if (block_size == 2) {
1021         uint32_t indices;
1022
1023         if (bytestream2_get_bytes_left(&ctx->gb) < 4)
1024             return AVERROR_INVALIDDATA;
1025
1026         indices        = bytestream2_get_le32u(&ctx->gb);
1027         dst[0]         = ctx->codebook[indices & 0xFF];
1028         indices      >>= 8;
1029         dst[1]         = ctx->codebook[indices & 0xFF];
1030         indices      >>= 8;
1031         dst[pitch]     = ctx->codebook[indices & 0xFF];
1032         indices      >>= 8;
1033         dst[pitch + 1] = ctx->codebook[indices & 0xFF];
1034     } else {
1035         uint16_t fgcolor, bgcolor;
1036         int glyph;
1037
1038         if (bytestream2_get_bytes_left(&ctx->gb) < 3)
1039             return AVERROR_INVALIDDATA;
1040
1041         glyph   = bytestream2_get_byteu(&ctx->gb);
1042         bgcolor = ctx->codebook[bytestream2_get_byteu(&ctx->gb)];
1043         fgcolor = ctx->codebook[bytestream2_get_byteu(&ctx->gb)];
1044
1045         draw_glyph(ctx, dst, glyph, fgcolor, bgcolor, block_size, pitch);
1046     }
1047     return 0;
1048 }
1049
1050 static int opcode_0xf8(SANMVideoContext *ctx, int cx, int cy, int block_size, int pitch)
1051 {
1052     uint16_t *dst = ctx->frm0 + cx + cy * ctx->pitch;
1053
1054     if (block_size == 2) {
1055         if (bytestream2_get_bytes_left(&ctx->gb) < 8)
1056             return AVERROR_INVALIDDATA;
1057
1058         dst[0]         = bytestream2_get_le16u(&ctx->gb);
1059         dst[1]         = bytestream2_get_le16u(&ctx->gb);
1060         dst[pitch]     = bytestream2_get_le16u(&ctx->gb);
1061         dst[pitch + 1] = bytestream2_get_le16u(&ctx->gb);
1062     } else {
1063         uint16_t fgcolor, bgcolor;
1064         int glyph;
1065
1066         if (bytestream2_get_bytes_left(&ctx->gb) < 5)
1067             return AVERROR_INVALIDDATA;
1068
1069         glyph   = bytestream2_get_byteu(&ctx->gb);
1070         bgcolor = bytestream2_get_le16u(&ctx->gb);
1071         fgcolor = bytestream2_get_le16u(&ctx->gb);
1072
1073         draw_glyph(ctx, dst, glyph, fgcolor, bgcolor, block_size, pitch);
1074     }
1075     return 0;
1076 }
1077
1078 static int good_mvec(SANMVideoContext *ctx, int cx, int cy, int mx, int my,
1079                      int block_size)
1080 {
1081     int start_pos = cx + mx + (cy + my) * ctx->pitch;
1082     int end_pos = start_pos + (block_size - 1) * (ctx->pitch + 1);
1083
1084     int good = start_pos >= 0 && end_pos < (ctx->buf_size >> 1);
1085
1086     if (!good)
1087         av_log(ctx->avctx, AV_LOG_ERROR,
1088                "Ignoring invalid motion vector (%i, %i)->(%u, %u), block size = %u\n",
1089                cx + mx, cy + my, cx, cy, block_size);
1090
1091     return good;
1092 }
1093
1094 static int codec2subblock(SANMVideoContext *ctx, int cx, int cy, int blk_size)
1095 {
1096     int16_t mx, my, index;
1097     int opcode;
1098
1099     if (bytestream2_get_bytes_left(&ctx->gb) < 1)
1100         return AVERROR_INVALIDDATA;
1101
1102     opcode = bytestream2_get_byteu(&ctx->gb);
1103
1104     switch (opcode) {
1105     default:
1106         mx = motion_vectors[opcode][0];
1107         my = motion_vectors[opcode][1];
1108
1109         if (good_mvec(ctx, cx, cy, mx, my, blk_size)) {
1110             copy_block(ctx->frm0 + cx      + ctx->pitch *  cy,
1111                        ctx->frm2 + cx + mx + ctx->pitch * (cy + my),
1112                        blk_size, ctx->pitch);
1113         }
1114         break;
1115     case 0xF5:
1116         if (bytestream2_get_bytes_left(&ctx->gb) < 2)
1117             return AVERROR_INVALIDDATA;
1118         index = bytestream2_get_le16u(&ctx->gb);
1119
1120         mx = index % ctx->width;
1121         my = index / ctx->width;
1122
1123         if (good_mvec(ctx, cx, cy, mx, my, blk_size)) {
1124             copy_block(ctx->frm0 + cx      + ctx->pitch *  cy,
1125                        ctx->frm2 + cx + mx + ctx->pitch * (cy + my),
1126                        blk_size, ctx->pitch);
1127         }
1128         break;
1129     case 0xF6:
1130         copy_block(ctx->frm0 + cx + ctx->pitch * cy,
1131                    ctx->frm1 + cx + ctx->pitch * cy,
1132                    blk_size, ctx->pitch);
1133         break;
1134     case 0xF7:
1135         opcode_0xf7(ctx, cx, cy, blk_size, ctx->pitch);
1136         break;
1137
1138     case 0xF8:
1139         opcode_0xf8(ctx, cx, cy, blk_size, ctx->pitch);
1140         break;
1141     case 0xF9:
1142     case 0xFA:
1143     case 0xFB:
1144     case 0xFC:
1145         fill_block(ctx->frm0 + cx + cy * ctx->pitch,
1146                    ctx->small_codebook[opcode - 0xf9], blk_size, ctx->pitch);
1147         break;
1148     case 0xFD:
1149         if (bytestream2_get_bytes_left(&ctx->gb) < 1)
1150             return AVERROR_INVALIDDATA;
1151         fill_block(ctx->frm0 + cx + cy * ctx->pitch,
1152                    ctx->codebook[bytestream2_get_byteu(&ctx->gb)], blk_size, ctx->pitch);
1153         break;
1154     case 0xFE:
1155         if (bytestream2_get_bytes_left(&ctx->gb) < 2)
1156             return AVERROR_INVALIDDATA;
1157         fill_block(ctx->frm0 + cx + cy * ctx->pitch,
1158                    bytestream2_get_le16u(&ctx->gb), blk_size, ctx->pitch);
1159         break;
1160     case 0xFF:
1161         if (blk_size == 2) {
1162             opcode_0xf8(ctx, cx, cy, blk_size, ctx->pitch);
1163         } else {
1164             blk_size >>= 1;
1165             if (codec2subblock(ctx, cx, cy, blk_size))
1166                 return AVERROR_INVALIDDATA;
1167             if (codec2subblock(ctx, cx + blk_size, cy, blk_size))
1168                 return AVERROR_INVALIDDATA;
1169             if (codec2subblock(ctx, cx, cy + blk_size, blk_size))
1170                 return AVERROR_INVALIDDATA;
1171             if (codec2subblock(ctx, cx + blk_size, cy + blk_size, blk_size))
1172                 return AVERROR_INVALIDDATA;
1173         }
1174         break;
1175     }
1176     return 0;
1177 }
1178
1179 static int decode_2(SANMVideoContext *ctx)
1180 {
1181     int cx, cy, ret;
1182
1183     for (cy = 0; cy < ctx->aligned_height; cy += 8)
1184         for (cx = 0; cx < ctx->aligned_width; cx += 8)
1185             if (ret = codec2subblock(ctx, cx, cy, 8))
1186                 return ret;
1187
1188     return 0;
1189 }
1190
1191 static int decode_3(SANMVideoContext *ctx)
1192 {
1193     memcpy(ctx->frm0, ctx->frm2, ctx->frm2_size);
1194     return 0;
1195 }
1196
1197 static int decode_4(SANMVideoContext *ctx)
1198 {
1199     memcpy(ctx->frm0, ctx->frm1, ctx->frm1_size);
1200     return 0;
1201 }
1202
1203 static int decode_5(SANMVideoContext *ctx)
1204 {
1205 #if HAVE_BIGENDIAN
1206     uint16_t *frm;
1207     int npixels;
1208 #endif
1209     uint8_t *dst = (uint8_t*)ctx->frm0;
1210
1211     if (rle_decode(ctx, dst, ctx->buf_size))
1212         return AVERROR_INVALIDDATA;
1213
1214 #if HAVE_BIGENDIAN
1215     npixels = ctx->npixels;
1216     frm = ctx->frm0;
1217     while (npixels--) {
1218         *frm = av_bswap16(*frm);
1219         frm++;
1220     }
1221 #endif
1222
1223     return 0;
1224 }
1225
1226 static int decode_6(SANMVideoContext *ctx)
1227 {
1228     int npixels = ctx->npixels;
1229     uint16_t *frm = ctx->frm0;
1230
1231     if (bytestream2_get_bytes_left(&ctx->gb) < npixels) {
1232         av_log(ctx->avctx, AV_LOG_ERROR, "Insufficient data for frame.\n");
1233         return AVERROR_INVALIDDATA;
1234     }
1235     while (npixels--)
1236         *frm++ = ctx->codebook[bytestream2_get_byteu(&ctx->gb)];
1237
1238     return 0;
1239 }
1240
1241 static int decode_8(SANMVideoContext *ctx)
1242 {
1243     uint16_t *pdest = ctx->frm0;
1244     uint8_t *rsrc;
1245     long npixels = ctx->npixels;
1246
1247     av_fast_malloc(&ctx->rle_buf, &ctx->rle_buf_size, npixels);
1248     if (!ctx->rle_buf) {
1249         av_log(ctx->avctx, AV_LOG_ERROR, "RLE buffer allocation failed.\n");
1250         return AVERROR(ENOMEM);
1251     }
1252     rsrc = ctx->rle_buf;
1253
1254     if (rle_decode(ctx, rsrc, npixels))
1255         return AVERROR_INVALIDDATA;
1256
1257     while (npixels--)
1258         *pdest++ = ctx->codebook[*rsrc++];
1259
1260     return 0;
1261 }
1262
1263 typedef int (*frm_decoder)(SANMVideoContext *ctx);
1264
1265 static const frm_decoder v1_decoders[] = {
1266     decode_0, decode_nop, decode_2, decode_3, decode_4, decode_5,
1267     decode_6, decode_nop, decode_8
1268 };
1269
1270 static int read_frame_header(SANMVideoContext *ctx, SANMFrameHeader *hdr)
1271 {
1272     int i, ret;
1273
1274     if ((ret = bytestream2_get_bytes_left(&ctx->gb)) < 560) {
1275         av_log(ctx->avctx, AV_LOG_ERROR, "Input frame too short (%d bytes).\n",
1276                ret);
1277         return AVERROR_INVALIDDATA;
1278     }
1279     bytestream2_skip(&ctx->gb, 8); // skip pad
1280
1281     hdr->width  = bytestream2_get_le32u(&ctx->gb);
1282     hdr->height = bytestream2_get_le32u(&ctx->gb);
1283
1284     if (hdr->width != ctx->width || hdr->height != ctx->height) {
1285         avpriv_report_missing_feature(ctx->avctx, "Variable size frames");
1286         return AVERROR_PATCHWELCOME;
1287     }
1288
1289     hdr->seq_num     = bytestream2_get_le16u(&ctx->gb);
1290     hdr->codec       = bytestream2_get_byteu(&ctx->gb);
1291     hdr->rotate_code = bytestream2_get_byteu(&ctx->gb);
1292
1293     bytestream2_skip(&ctx->gb, 4); // skip pad
1294
1295     for (i = 0; i < 4; i++)
1296         ctx->small_codebook[i] = bytestream2_get_le16u(&ctx->gb);
1297     hdr->bg_color = bytestream2_get_le16u(&ctx->gb);
1298
1299     bytestream2_skip(&ctx->gb, 2); // skip pad
1300
1301     hdr->rle_output_size = bytestream2_get_le32u(&ctx->gb);
1302     for (i = 0; i < 256; i++)
1303         ctx->codebook[i] = bytestream2_get_le16u(&ctx->gb);
1304
1305     bytestream2_skip(&ctx->gb, 8); // skip pad
1306
1307     return 0;
1308 }
1309
1310 static void fill_frame(uint16_t *pbuf, int buf_size, uint16_t color)
1311 {
1312     while (buf_size--)
1313         *pbuf++ = color;
1314 }
1315
1316 static int copy_output(SANMVideoContext *ctx, SANMFrameHeader *hdr)
1317 {
1318     uint8_t *dst;
1319     const uint8_t *src = (uint8_t*) ctx->frm0;
1320     int ret, dstpitch, height = ctx->height;
1321     int srcpitch = ctx->pitch * (hdr ? sizeof(ctx->frm0[0]) : 1);
1322
1323     if ((ret = ff_get_buffer(ctx->avctx, ctx->frame, 0)) < 0)
1324         return ret;
1325
1326     dst      = ctx->frame->data[0];
1327     dstpitch = ctx->frame->linesize[0];
1328
1329     while (height--) {
1330         memcpy(dst, src, srcpitch);
1331         src += srcpitch;
1332         dst += dstpitch;
1333     }
1334
1335     return 0;
1336 }
1337
1338 static int decode_frame(AVCodecContext *avctx, void *data,
1339                         int *got_frame_ptr, AVPacket *pkt)
1340 {
1341     SANMVideoContext *ctx = avctx->priv_data;
1342     int i, ret;
1343
1344     ctx->frame = data;
1345     bytestream2_init(&ctx->gb, pkt->data, pkt->size);
1346
1347     if (!ctx->version) {
1348         int to_store = 0;
1349
1350         while (bytestream2_get_bytes_left(&ctx->gb) >= 8) {
1351             uint32_t sig, size;
1352             int pos;
1353
1354             sig  = bytestream2_get_be32u(&ctx->gb);
1355             size = bytestream2_get_be32u(&ctx->gb);
1356             pos  = bytestream2_tell(&ctx->gb);
1357
1358             if (bytestream2_get_bytes_left(&ctx->gb) < size) {
1359                 av_log(avctx, AV_LOG_ERROR, "Incorrect chunk size %"PRIu32".\n", size);
1360                 break;
1361             }
1362             switch (sig) {
1363             case MKBETAG('N', 'P', 'A', 'L'):
1364                 if (size != PALETTE_SIZE * 3) {
1365                     av_log(avctx, AV_LOG_ERROR,
1366                            "Incorrect palette block size %"PRIu32".\n", size);
1367                     return AVERROR_INVALIDDATA;
1368                 }
1369                 for (i = 0; i < PALETTE_SIZE; i++)
1370                     ctx->pal[i] = 0xFFU << 24 | bytestream2_get_be24u(&ctx->gb);
1371                 break;
1372             case MKBETAG('F', 'O', 'B', 'J'):
1373                 if (size < 16)
1374                     return AVERROR_INVALIDDATA;
1375                 if (ret = process_frame_obj(ctx))
1376                     return ret;
1377                 break;
1378             case MKBETAG('X', 'P', 'A', 'L'):
1379                 if (size == 6 || size == 4) {
1380                     uint8_t tmp[3];
1381                     int j;
1382
1383                     for (i = 0; i < PALETTE_SIZE; i++) {
1384                         for (j = 0; j < 3; j++) {
1385                             int t = (ctx->pal[i] >> (16 - j * 8)) & 0xFF;
1386                             tmp[j] = av_clip_uint8((t * 129 + ctx->delta_pal[i * 3 + j]) >> 7);
1387                         }
1388                         ctx->pal[i] = 0xFFU << 24 | AV_RB24(tmp);
1389                     }
1390                 } else {
1391                     if (size < PALETTE_DELTA * 2 + 4) {
1392                         av_log(avctx, AV_LOG_ERROR,
1393                                "Incorrect palette change block size %"PRIu32".\n",
1394                                size);
1395                         return AVERROR_INVALIDDATA;
1396                     }
1397                     bytestream2_skipu(&ctx->gb, 4);
1398                     for (i = 0; i < PALETTE_DELTA; i++)
1399                         ctx->delta_pal[i] = bytestream2_get_le16u(&ctx->gb);
1400                     if (size >= PALETTE_DELTA * 5 + 4) {
1401                         for (i = 0; i < PALETTE_SIZE; i++)
1402                             ctx->pal[i] = 0xFFU << 24 | bytestream2_get_be24u(&ctx->gb);
1403                     } else {
1404                         memset(ctx->pal, 0, sizeof(ctx->pal));
1405                     }
1406                 }
1407                 break;
1408             case MKBETAG('S', 'T', 'O', 'R'):
1409                 to_store = 1;
1410                 break;
1411             case MKBETAG('F', 'T', 'C', 'H'):
1412                 memcpy(ctx->frm0, ctx->stored_frame, ctx->buf_size);
1413                 break;
1414             default:
1415                 bytestream2_skip(&ctx->gb, size);
1416                 av_log(avctx, AV_LOG_DEBUG,
1417                        "Unknown/unsupported chunk %"PRIx32".\n", sig);
1418                 break;
1419             }
1420
1421             bytestream2_seek(&ctx->gb, pos + size, SEEK_SET);
1422             if (size & 1)
1423                 bytestream2_skip(&ctx->gb, 1);
1424         }
1425         if (to_store)
1426             memcpy(ctx->stored_frame, ctx->frm0, ctx->buf_size);
1427         if ((ret = copy_output(ctx, NULL)))
1428             return ret;
1429         memcpy(ctx->frame->data[1], ctx->pal, 1024);
1430     } else {
1431         SANMFrameHeader header;
1432
1433         if ((ret = read_frame_header(ctx, &header)))
1434             return ret;
1435
1436         ctx->rotate_code = header.rotate_code;
1437         if ((ctx->frame->key_frame = !header.seq_num)) {
1438             ctx->frame->pict_type = AV_PICTURE_TYPE_I;
1439             fill_frame(ctx->frm1, ctx->npixels, header.bg_color);
1440             fill_frame(ctx->frm2, ctx->npixels, header.bg_color);
1441         } else {
1442             ctx->frame->pict_type = AV_PICTURE_TYPE_P;
1443         }
1444
1445         if (header.codec < FF_ARRAY_ELEMS(v1_decoders)) {
1446             if ((ret = v1_decoders[header.codec](ctx))) {
1447                 av_log(avctx, AV_LOG_ERROR,
1448                        "Subcodec %d: error decoding frame.\n", header.codec);
1449                 return ret;
1450             }
1451         } else {
1452             avpriv_request_sample(avctx, "Subcodec %d", header.codec);
1453             return AVERROR_PATCHWELCOME;
1454         }
1455
1456         if ((ret = copy_output(ctx, &header)))
1457             return ret;
1458     }
1459     if (ctx->rotate_code)
1460         rotate_bufs(ctx, ctx->rotate_code);
1461
1462     *got_frame_ptr = 1;
1463
1464     return pkt->size;
1465 }
1466
1467 AVCodec ff_sanm_decoder = {
1468     .name           = "sanm",
1469     .long_name      = NULL_IF_CONFIG_SMALL("LucasArts SANM video"),
1470     .type           = AVMEDIA_TYPE_VIDEO,
1471     .id             = AV_CODEC_ID_SANM,
1472     .priv_data_size = sizeof(SANMVideoContext),
1473     .init           = decode_init,
1474     .close          = decode_end,
1475     .decode         = decode_frame,
1476     .capabilities   = AV_CODEC_CAP_DR1,
1477 };