1 /*
2 * Kega Game Video (KGV1) decoder
3 * Copyright (c) 2010 Daniel Verkamp
4 *
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 /**
23 * @file
24 * Kega Game Video decoder
25 */
26
32
37
39 {
41
43 }
44
47 {
52 int offsets[8];
54 int outcnt = 0, maxcnt;
56
59
60 w = (buf[0] + 1) * 8;
61 h = (buf[1] + 1) * 8;
62 buf += 2;
63
66
70 }
71
72 maxcnt = w * h;
73
79 } else {
80 prev = NULL;
81 }
82
83 for (i = 0; i < 8; i++)
84 offsets[i] = -1;
85
86 while (outcnt < maxcnt && buf_end - 2 >= buf) {
88 buf += 2;
89
90 if (!(code & 0x8000)) {
91 AV_WN16A(&out[2 * outcnt], code);
// rgb555 pixel coded directly
92 outcnt++;
93 } else {
95
96 if ((code & 0x6000) == 0x6000) {
97 // copy from previous frame
98 int oidx = (code >> 10) & 7;
100
101 count = (code & 0x3FF) + 3;
102
103 if (offsets[oidx] < 0) {
104 if (buf_end - 3 < buf)
105 break;
107 buf += 3;
108 }
109
110 start = (outcnt + offsets[oidx]) % maxcnt;
111
112 if (maxcnt - start < count || maxcnt - outcnt < count)
113 break;
114
115 if (!prev) {
117 "Frame reference does not exist\n");
118 break;
119 }
120
121 memcpy(out + 2 * outcnt, prev + 2 * start, 2 * count);
122 } else {
123 // copy from earlier in this frame
124 int offset = (code & 0x1FFF) + 1;
125
126 if (!(code & 0x6000)) {
127 count = 2;
128 } else if ((code & 0x6000) == 0x2000) {
129 count = 3;
130 } else {
131 if (buf_end - 1 < buf)
132 break;
133 count = 4 + *buf++;
134 }
135
136 if (outcnt < offset || maxcnt - outcnt < count)
137 break;
138
140 }
142 }
143 }
144
145 if (outcnt - maxcnt)
147
151
152 *got_frame = 1;
153
155 }
156
158 {
160
164
165 return 0;
166 }
167
169 {
171 return 0;
172 }
173
185 };