1 /*
2 * Fraps FPS1 decoder
3 * Copyright (c) 2005 Roine Gustafsson
4 * Copyright (c) 2006 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 /**
24 * @file
25 * Lossless Fraps 'FPS1' decoder
26 * @author Roine Gustafsson (roine at users sf net)
27 * @author Konstantin Shishkov
28 *
29 * Codec algorithm for version 0 is taken from Transcode <www.transcoding.org>
30 *
31 * Version 2 files support by Konstantin Shishkov
32 */
33
34 #include "config.h"
35
36 #define CACHED_BITSTREAM_READER HAVE_FAST_64BIT
37 #define UNCHECKED_BITSTREAM_READER 1
46
47 #define FPS_TAG MKTAG('F', 'P', 'S', 'x')
49
50 /**
51 * local variable storage
52 */
59
60 /**
61 * initializes decoder
62 * @param avctx codec context
63 * @return 0 on success or negative if fails
64 */
66 {
68
71
73
74 return 0;
75 }
76
77 /**
78 * Comparator - our nodes should ascend by count
79 * but with preserved symbol order
80 */
81 static int huff_cmp(
const void *va,
const void *vb)
82 {
83 const Node *
a = va, *
b = vb;
84 return (
a->count -
b->count)*256 +
a->sym -
b->sym;
85 }
86
87 /**
88 * decode Fraps v2 packed plane
89 */
91 int h,
const uint8_t *
src,
int size,
int Uoff,
93 {
98
99 for (
i = 0;
i < 256;
i++)
100 nodes[
i].count = bytestream_get_le32(&
src);
106 /* we have built Huffman table and are ready to decode plane */
107
108 /* convert bits so they may be used by standard bitreader */
109 s->bdsp.bswap_buf((uint32_t *)
s->tmpbuf,
110 (
const uint32_t *)
src,
size >> 2);
111
114
115 for (j = 0; j <
h; j++) {
118 /* lines are stored as deltas between previous lines
119 * and we need to add 0x80 to the first lines of chroma planes
120 */
121 if (j)
123 else if (Uoff)
128 }
129 }
131 }
133 return 0;
134 }
135
138 {
140 const uint8_t *buf = avpkt->
data;
141 int buf_size = avpkt->
size;
143 unsigned int version,header_size;
144 const uint32_t *buf32;
145 uint32_t *luma1,*luma2,*
cb,*
cr;
146 uint32_t offs[4];
147 int i, j,
ret, is_chroma;
149 int is_pal;
151
152 if (buf_size < 4) {
155 }
156
159 is_pal = buf[1] == 2 &&
version == 1;
160 header_size = (
header & (1<<30))? 8 : 4;
/* bit 30 means pad to 8 bytes */
161
165 }
166
167 buf += header_size;
168
169 if (is_pal) {
170 unsigned needed_size = avctx->
width * avctx->
height + 1024;
171 needed_size += header_size;
172 if (buf_size != needed_size) {
174 "Invalid frame length %d (should be %d)\n",
175 buf_size, needed_size);
177 }
179 unsigned needed_size = avctx->
width * avctx->
height * 3;
180 if (
version == 0) needed_size /= 2;
181 needed_size += header_size;
182 /* bit 31 means same as previous pic */
184 *got_frame = 0;
185 return buf_size;
186 }
187 if (buf_size != needed_size) {
189 "Invalid frame length %d (should be %d)\n",
190 buf_size, needed_size);
192 }
193 } else {
194 /* skip frame */
195 if (buf_size == 8) {
196 *got_frame = 0;
197 return buf_size;
198 }
202 }
205 if (offs[
i] >= buf_size - header_size || (
i && offs[
i] <= offs[
i - 1] + 1024)) {
208 }
209 }
210 offs[
planes] = buf_size - header_size;
215 }
216 }
217
222
225
227 case 0:
228 default:
229 /* Fraps v0 is a reordered YUV420 */
230 if (((avctx->
width % 8) != 0) || ((avctx->
height % 2) != 0)) {
234 }
235
236 buf32 = (const uint32_t*)buf;
237 for (ptrdiff_t y = 0; y < avctx->
height / 2; y++) {
238 luma1 = (uint32_t*)&
f->data[0][ y * 2 *
f->linesize[0] ];
239 luma2 = (uint32_t*)&
f->data[0][ (y * 2 + 1) *
f->linesize[0] ];
240 cr = (uint32_t*)&
f->data[1][ y *
f->linesize[1] ];
241 cb = (uint32_t*)&
f->data[2][ y *
f->linesize[2] ];
242 for (ptrdiff_t x = 0; x < avctx->
width; x += 8) {
243 *luma1++ = *buf32++;
244 *luma1++ = *buf32++;
245 *luma2++ = *buf32++;
246 *luma2++ = *buf32++;
249 }
250 }
251 break;
252
253 case 1:
254 if (is_pal) {
255 uint32_t *pal = (uint32_t *)
f->data[1];
256
257 for (
unsigned y = 0; y < 256; y++) {
258 pal[y] =
AV_RL32(buf) | 0xFF000000;
259 buf += 4;
260 }
261
262 for (ptrdiff_t y = 0; y < avctx->
height; y++)
263 memcpy(&
f->data[0][y *
f->linesize[0]],
264 &buf[y * avctx->
width],
266 } else {
267 /* Fraps v1 is an upside-down BGR24 */
268 for (ptrdiff_t y = 0; y < avctx->
height; y++)
269 memcpy(&
f->data[0][(avctx->
height - y - 1) *
f->linesize[0]],
270 &buf[y * avctx->
width * 3],
272 }
273 break;
274
275 case 2:
276 case 4:
277 /**
278 * Fraps v2 is Huffman-coded YUV420 planes
279 * Fraps v4 is virtually the same
280 */
284 avctx->
width >> is_chroma,
285 avctx->
height >> is_chroma,
286 buf + offs[
i], offs[
i + 1] - offs[
i],
287 is_chroma, 1)) < 0) {
290 }
291 }
292 break;
293 case 3:
294 case 5:
295 /* Virtually the same as version 4, but is for RGB24 */
299 buf + offs[
i], offs[
i + 1] - offs[
i], 0, 3)) < 0) {
302 }
303 }
305 // convert pseudo-YUV into real RGB
306 for (j = 0; j < avctx->
height; j++) {
307 uint8_t *line_end =
out + 3*avctx->
width;
308 while (
out < line_end) {
312 }
314 }
315 break;
316 }
317
318 *got_frame = 1;
319
320 return buf_size;
321 }
322
323 /**
324 * closes decoder
325 * @param avctx codec context
326 * @return 0 on success or negative if fails
327 */
329 {
331
333 return 0;
334 }
335
346 };