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