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
36
38 {
40
42 }
43
46 {
51 int offsets[8];
53 int outcnt = 0, maxcnt;
55
58
59 w = (buf[0] + 1) * 8;
60 h = (buf[1] + 1) * 8;
61 buf += 2;
62
65
69 }
70
71 maxcnt = w * h;
72
78 } else {
79 prev = NULL;
80 }
81
82 for (i = 0; i < 8; i++)
83 offsets[i] = -1;
84
85 while (outcnt < maxcnt && buf_end - 2 >= buf) {
87 buf += 2;
88
89 if (!(code & 0x8000)) {
90 AV_WN16A(&out[2 * outcnt], code);
// rgb555 pixel coded directly
91 outcnt++;
92 } else {
94
95 if ((code & 0x6000) == 0x6000) {
96 // copy from previous frame
97 int oidx = (code >> 10) & 7;
99
100 count = (code & 0x3FF) + 3;
101
102 if (offsets[oidx] < 0) {
103 if (buf_end - 3 < buf)
104 break;
106 buf += 3;
107 }
108
109 start = (outcnt + offsets[oidx]) % maxcnt;
110
111 if (maxcnt - start < count || maxcnt - outcnt < count)
112 break;
113
114 if (!prev) {
116 "Frame reference does not exist\n");
117 break;
118 }
119
120 memcpy(out + 2 * outcnt, prev + 2 * start, 2 * count);
121 } else {
122 // copy from earlier in this frame
123 int offset = (code & 0x1FFF) + 1;
124
125 if (!(code & 0x6000)) {
126 count = 2;
127 } else if ((code & 0x6000) == 0x2000) {
128 count = 3;
129 } else {
130 if (buf_end - 1 < buf)
131 break;
132 count = 4 + *buf++;
133 }
134
135 if (outcnt < offset || maxcnt - outcnt < count)
136 break;
137
139 }
141 }
142 }
143
144 if (outcnt - maxcnt)
146
150
151 *got_frame = 1;
152
154 }
155
157 {
159
163
166
167 return 0;
168 }
169
171 {
174 return 0;
175 }
176
188 };