Line | Branch | Exec | Source |
---|---|---|---|
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 | ||
38 | #include "avcodec.h" | ||
39 | #include "get_bits.h" | ||
40 | #include "huffman.h" | ||
41 | #include "bytestream.h" | ||
42 | #include "bswapdsp.h" | ||
43 | #include "codec_internal.h" | ||
44 | #include "thread.h" | ||
45 | |||
46 | #define FPS_TAG MKTAG('F', 'P', 'S', 'x') | ||
47 | #define VLC_BITS 11 | ||
48 | |||
49 | /** | ||
50 | * local variable storage | ||
51 | */ | ||
52 | typedef struct FrapsContext { | ||
53 | AVCodecContext *avctx; | ||
54 | BswapDSPContext bdsp; | ||
55 | uint8_t *tmpbuf; | ||
56 | int tmpbuf_size; | ||
57 | } FrapsContext; | ||
58 | |||
59 | /** | ||
60 | * initializes decoder | ||
61 | * @param avctx codec context | ||
62 | * @return 0 on success or negative if fails | ||
63 | */ | ||
64 | 14 | static av_cold int decode_init(AVCodecContext *avctx) | |
65 | { | ||
66 | 14 | FrapsContext * const s = avctx->priv_data; | |
67 | |||
68 | 14 | s->avctx = avctx; | |
69 | 14 | s->tmpbuf = NULL; | |
70 | |||
71 | 14 | ff_bswapdsp_init(&s->bdsp); | |
72 | |||
73 | 14 | return 0; | |
74 | } | ||
75 | |||
76 | /** | ||
77 | * Comparator - our nodes should ascend by count | ||
78 | * but with preserved symbol order | ||
79 | */ | ||
80 | 924225 | static int huff_cmp(const void *va, const void *vb) | |
81 | { | ||
82 | 924225 | const Node *a = va, *b = vb; | |
83 | 924225 | return (a->count - b->count)*256 + a->sym - b->sym; | |
84 | } | ||
85 | |||
86 | /** | ||
87 | * decode Fraps v2 packed plane | ||
88 | */ | ||
89 | 417 | static int fraps2_decode_plane(FrapsContext *s, uint8_t *dst, int stride, int w, | |
90 | int h, const uint8_t *src, int size, int Uoff, | ||
91 | const int step) | ||
92 | { | ||
93 | int i, j, ret; | ||
94 | GetBitContext gb; | ||
95 | VLC vlc; | ||
96 | Node nodes[512]; | ||
97 | |||
98 |
2/2✓ Branch 0 taken 106752 times.
✓ Branch 1 taken 417 times.
|
107169 | for (i = 0; i < 256; i++) |
99 | 106752 | nodes[i].count = bytestream_get_le32(&src); | |
100 | 417 | size -= 1024; | |
101 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 417 times.
|
417 | if ((ret = ff_huff_build_tree(s->avctx, &vlc, 256, VLC_BITS, |
102 | nodes, huff_cmp, | ||
103 | FF_HUFFMAN_FLAG_ZERO_COUNT)) < 0) | ||
104 | ✗ | return ret; | |
105 | /* we have built Huffman table and are ready to decode plane */ | ||
106 | |||
107 | /* convert bits so they may be used by standard bitreader */ | ||
108 | 417 | s->bdsp.bswap_buf((uint32_t *) s->tmpbuf, | |
109 | (const uint32_t *) src, size >> 2); | ||
110 | |||
111 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 417 times.
|
417 | if ((ret = init_get_bits8(&gb, s->tmpbuf, size)) < 0) |
112 | ✗ | return ret; | |
113 | |||
114 |
2/2✓ Branch 0 taken 82192 times.
✓ Branch 1 taken 415 times.
|
82607 | for (j = 0; j < h; j++) { |
115 |
2/2✓ Branch 0 taken 29519934 times.
✓ Branch 1 taken 82190 times.
|
29602124 | for (i = 0; i < w*step; i += step) { |
116 | 29519934 | dst[i] = get_vlc2(&gb, vlc.table, VLC_BITS, 3); | |
117 | /* lines are stored as deltas between previous lines | ||
118 | * and we need to add 0x80 to the first lines of chroma planes | ||
119 | */ | ||
120 |
2/2✓ Branch 0 taken 29387582 times.
✓ Branch 1 taken 132352 times.
|
29519934 | if (j) |
121 | 29387582 | dst[i] += dst[i - stride]; | |
122 |
2/2✓ Branch 0 taken 8192 times.
✓ Branch 1 taken 124160 times.
|
132352 | else if (Uoff) |
123 | 8192 | dst[i] += 0x80; | |
124 |
2/2✓ Branch 1 taken 2 times.
✓ Branch 2 taken 29519932 times.
|
29519934 | if (get_bits_left(&gb) < 0) { |
125 | 2 | ff_vlc_free(&vlc); | |
126 | 2 | return AVERROR_INVALIDDATA; | |
127 | } | ||
128 | } | ||
129 | 82190 | dst += stride; | |
130 | } | ||
131 | 415 | ff_vlc_free(&vlc); | |
132 | 415 | return 0; | |
133 | } | ||
134 | |||
135 | 176 | static int decode_frame(AVCodecContext *avctx, AVFrame *f, | |
136 | int *got_frame, AVPacket *avpkt) | ||
137 | { | ||
138 | 176 | FrapsContext * const s = avctx->priv_data; | |
139 | 176 | const uint8_t *buf = avpkt->data; | |
140 | 176 | int buf_size = avpkt->size; | |
141 | uint32_t header; | ||
142 | unsigned int version,header_size; | ||
143 | const uint32_t *buf32; | ||
144 | uint32_t *luma1,*luma2,*cb,*cr; | ||
145 | uint32_t offs[4]; | ||
146 | int i, j, ret, is_chroma; | ||
147 | 176 | const int planes = 3; | |
148 | int is_pal; | ||
149 | uint8_t *out; | ||
150 | |||
151 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 176 times.
|
176 | if (buf_size < 4) { |
152 | ✗ | av_log(avctx, AV_LOG_ERROR, "Packet is too short\n"); | |
153 | ✗ | return AVERROR_INVALIDDATA; | |
154 | } | ||
155 | |||
156 | 176 | header = AV_RL32(buf); | |
157 | 176 | version = header & 0xff; | |
158 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 176 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
176 | is_pal = buf[1] == 2 && version == 1; |
159 |
1/2✓ Branch 0 taken 176 times.
✗ Branch 1 not taken.
|
176 | header_size = (header & (1<<30))? 8 : 4; /* bit 30 means pad to 8 bytes */ |
160 | |||
161 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 176 times.
|
176 | if (version > 5) { |
162 | ✗ | avpriv_report_missing_feature(avctx, "Fraps version %u", version); | |
163 | ✗ | return AVERROR_PATCHWELCOME; | |
164 | } | ||
165 | |||
166 | 176 | buf += header_size; | |
167 | |||
168 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 176 times.
|
176 | if (is_pal) { |
169 | ✗ | unsigned needed_size = avctx->width * avctx->height + 1024; | |
170 | ✗ | needed_size += header_size; | |
171 | ✗ | if (buf_size != needed_size) { | |
172 | ✗ | av_log(avctx, AV_LOG_ERROR, | |
173 | "Invalid frame length %d (should be %d)\n", | ||
174 | buf_size, needed_size); | ||
175 | ✗ | return AVERROR_INVALIDDATA; | |
176 | } | ||
177 |
2/2✓ Branch 0 taken 24 times.
✓ Branch 1 taken 152 times.
|
176 | } else if (version < 2) { |
178 | 24 | unsigned needed_size = avctx->width * avctx->height * 3; | |
179 |
2/2✓ Branch 0 taken 22 times.
✓ Branch 1 taken 2 times.
|
24 | if (version == 0) needed_size /= 2; |
180 | 24 | needed_size += header_size; | |
181 | /* bit 31 means same as previous pic */ | ||
182 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 24 times.
|
24 | if (header & (1U<<31)) { |
183 | ✗ | *got_frame = 0; | |
184 | ✗ | return buf_size; | |
185 | } | ||
186 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 23 times.
|
24 | if (buf_size != needed_size) { |
187 | 1 | av_log(avctx, AV_LOG_ERROR, | |
188 | "Invalid frame length %d (should be %d)\n", | ||
189 | buf_size, needed_size); | ||
190 | 1 | return AVERROR_INVALIDDATA; | |
191 | } | ||
192 | } else { | ||
193 | /* skip frame */ | ||
194 |
2/2✓ Branch 0 taken 10 times.
✓ Branch 1 taken 142 times.
|
152 | if (buf_size == 8) { |
195 | 10 | *got_frame = 0; | |
196 | 10 | return buf_size; | |
197 | } | ||
198 |
2/4✓ Branch 0 taken 142 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 142 times.
|
142 | if (AV_RL32(buf) != FPS_TAG || buf_size < planes*1024 + 24) { |
199 | ✗ | av_log(avctx, AV_LOG_ERROR, "error in data stream\n"); | |
200 | ✗ | return AVERROR_INVALIDDATA; | |
201 | } | ||
202 |
2/2✓ Branch 0 taken 424 times.
✓ Branch 1 taken 139 times.
|
563 | for (i = 0; i < planes; i++) { |
203 | 424 | offs[i] = AV_RL32(buf + 4 + i * 4); | |
204 |
5/6✓ Branch 0 taken 421 times.
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 279 times.
✓ Branch 3 taken 142 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 279 times.
|
424 | if (offs[i] >= buf_size - header_size || (i && offs[i] <= offs[i - 1] + 1024)) { |
205 | 3 | av_log(avctx, AV_LOG_ERROR, "plane %i offset is out of bounds\n", i); | |
206 | 3 | return AVERROR_INVALIDDATA; | |
207 | } | ||
208 | } | ||
209 | 139 | offs[planes] = buf_size - header_size; | |
210 |
2/2✓ Branch 0 taken 417 times.
✓ Branch 1 taken 139 times.
|
556 | for (i = 0; i < planes; i++) { |
211 | 417 | av_fast_padded_malloc(&s->tmpbuf, &s->tmpbuf_size, offs[i + 1] - offs[i] - 1024); | |
212 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 417 times.
|
417 | if (!s->tmpbuf) |
213 | ✗ | return AVERROR(ENOMEM); | |
214 | } | ||
215 | } | ||
216 | |||
217 | 162 | f->pict_type = AV_PICTURE_TYPE_I; | |
218 | 162 | f->flags |= AV_FRAME_FLAG_KEY; | |
219 | |||
220 |
3/4✓ Branch 0 taken 130 times.
✓ Branch 1 taken 32 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 130 times.
|
162 | avctx->pix_fmt = version & 1 ? is_pal ? AV_PIX_FMT_PAL8 : AV_PIX_FMT_BGR24 : AV_PIX_FMT_YUVJ420P; |
221 | 324 | avctx->color_range = version & 1 ? AVCOL_RANGE_UNSPECIFIED | |
222 |
2/2✓ Branch 0 taken 130 times.
✓ Branch 1 taken 32 times.
|
162 | : AVCOL_RANGE_JPEG; |
223 |
2/2✓ Branch 0 taken 130 times.
✓ Branch 1 taken 32 times.
|
162 | avctx->colorspace = version & 1 ? AVCOL_SPC_UNSPECIFIED : AVCOL_SPC_BT709; |
224 | |||
225 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 162 times.
|
162 | if ((ret = ff_thread_get_buffer(avctx, f, 0)) < 0) |
226 | ✗ | return ret; | |
227 | |||
228 |
4/4✓ Branch 0 taken 21 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 11 times.
✓ Branch 3 taken 128 times.
|
162 | switch (version) { |
229 | 21 | case 0: | |
230 | default: | ||
231 | /* Fraps v0 is a reordered YUV420 */ | ||
232 |
2/4✓ Branch 0 taken 21 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 21 times.
|
21 | if (((avctx->width % 8) != 0) || ((avctx->height % 2) != 0)) { |
233 | ✗ | av_log(avctx, AV_LOG_ERROR, "Invalid frame size %dx%d\n", | |
234 | avctx->width, avctx->height); | ||
235 | ✗ | return AVERROR_INVALIDDATA; | |
236 | } | ||
237 | |||
238 | 21 | buf32 = (const uint32_t*)buf; | |
239 |
2/2✓ Branch 0 taken 2268 times.
✓ Branch 1 taken 21 times.
|
2289 | for (ptrdiff_t y = 0; y < avctx->height / 2; y++) { |
240 | 2268 | luma1 = (uint32_t*)&f->data[0][ y * 2 * f->linesize[0] ]; | |
241 | 2268 | luma2 = (uint32_t*)&f->data[0][ (y * 2 + 1) * f->linesize[0] ]; | |
242 | 2268 | cr = (uint32_t*)&f->data[1][ y * f->linesize[1] ]; | |
243 | 2268 | cb = (uint32_t*)&f->data[2][ y * f->linesize[2] ]; | |
244 |
2/2✓ Branch 0 taken 88452 times.
✓ Branch 1 taken 2268 times.
|
90720 | for (ptrdiff_t x = 0; x < avctx->width; x += 8) { |
245 | 88452 | *luma1++ = *buf32++; | |
246 | 88452 | *luma1++ = *buf32++; | |
247 | 88452 | *luma2++ = *buf32++; | |
248 | 88452 | *luma2++ = *buf32++; | |
249 | 88452 | *cr++ = *buf32++; | |
250 | 88452 | *cb++ = *buf32++; | |
251 | } | ||
252 | } | ||
253 | 21 | break; | |
254 | |||
255 | 2 | case 1: | |
256 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | if (is_pal) { |
257 | ✗ | uint32_t *pal = (uint32_t *)f->data[1]; | |
258 | |||
259 | ✗ | for (unsigned y = 0; y < 256; y++) { | |
260 | ✗ | pal[y] = AV_RL32(buf) | 0xFF000000; | |
261 | ✗ | buf += 4; | |
262 | } | ||
263 | |||
264 | ✗ | for (ptrdiff_t y = 0; y < avctx->height; y++) | |
265 | ✗ | memcpy(&f->data[0][y * f->linesize[0]], | |
266 | ✗ | &buf[y * avctx->width], | |
267 | ✗ | avctx->width); | |
268 | } else { | ||
269 | /* Fraps v1 is an upside-down BGR24 */ | ||
270 |
2/2✓ Branch 0 taken 480 times.
✓ Branch 1 taken 2 times.
|
482 | for (ptrdiff_t y = 0; y < avctx->height; y++) |
271 | 480 | memcpy(&f->data[0][(avctx->height - y - 1) * f->linesize[0]], | |
272 | 480 | &buf[y * avctx->width * 3], | |
273 | 480 | 3 * avctx->width); | |
274 | } | ||
275 | 2 | break; | |
276 | |||
277 | 11 | case 2: | |
278 | case 4: | ||
279 | /** | ||
280 | * Fraps v2 is Huffman-coded YUV420 planes | ||
281 | * Fraps v4 is virtually the same | ||
282 | */ | ||
283 |
2/2✓ Branch 0 taken 33 times.
✓ Branch 1 taken 11 times.
|
44 | for (i = 0; i < planes; i++) { |
284 | 33 | is_chroma = !!i; | |
285 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 33 times.
|
33 | if ((ret = fraps2_decode_plane(s, f->data[i], f->linesize[i], |
286 | 33 | avctx->width >> is_chroma, | |
287 | 33 | avctx->height >> is_chroma, | |
288 | 33 | buf + offs[i], offs[i + 1] - offs[i], | |
289 | is_chroma, 1)) < 0) { | ||
290 | ✗ | av_log(avctx, AV_LOG_ERROR, "Error decoding plane %i\n", i); | |
291 | ✗ | return ret; | |
292 | } | ||
293 | } | ||
294 | 11 | break; | |
295 | 128 | case 3: | |
296 | case 5: | ||
297 | /* Virtually the same as version 4, but is for RGB24 */ | ||
298 |
2/2✓ Branch 0 taken 384 times.
✓ Branch 1 taken 126 times.
|
510 | for (i = 0; i < planes; i++) { |
299 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 382 times.
|
384 | if ((ret = fraps2_decode_plane(s, f->data[0] + i + (f->linesize[0] * (avctx->height - 1)), |
300 | 384 | -f->linesize[0], avctx->width, avctx->height, | |
301 | 384 | buf + offs[i], offs[i + 1] - offs[i], 0, 3)) < 0) { | |
302 | 2 | av_log(avctx, AV_LOG_ERROR, "Error decoding plane %i\n", i); | |
303 | 2 | return ret; | |
304 | } | ||
305 | } | ||
306 | 126 | out = f->data[0]; | |
307 | // convert pseudo-YUV into real RGB | ||
308 |
2/2✓ Branch 0 taken 22896 times.
✓ Branch 1 taken 126 times.
|
23022 | for (j = 0; j < avctx->height; j++) { |
309 | 22896 | uint8_t *line_end = out + 3*avctx->width; | |
310 |
2/2✓ Branch 0 taken 7282176 times.
✓ Branch 1 taken 22896 times.
|
7305072 | while (out < line_end) { |
311 | 7282176 | out[0] += out[1]; | |
312 | 7282176 | out[2] += out[1]; | |
313 | 7282176 | out += 3; | |
314 | } | ||
315 | 22896 | out += f->linesize[0] - 3*avctx->width; | |
316 | } | ||
317 | 126 | break; | |
318 | } | ||
319 | |||
320 | 160 | *got_frame = 1; | |
321 | |||
322 | 160 | return buf_size; | |
323 | } | ||
324 | |||
325 | /** | ||
326 | * closes decoder | ||
327 | * @param avctx codec context | ||
328 | * @return 0 on success or negative if fails | ||
329 | */ | ||
330 | 14 | static av_cold int decode_end(AVCodecContext *avctx) | |
331 | { | ||
332 | 14 | FrapsContext *s = avctx->priv_data; | |
333 | |||
334 | 14 | av_freep(&s->tmpbuf); | |
335 | 14 | return 0; | |
336 | } | ||
337 | |||
338 | const FFCodec ff_fraps_decoder = { | ||
339 | .p.name = "fraps", | ||
340 | CODEC_LONG_NAME("Fraps"), | ||
341 | .p.type = AVMEDIA_TYPE_VIDEO, | ||
342 | .p.id = AV_CODEC_ID_FRAPS, | ||
343 | .priv_data_size = sizeof(FrapsContext), | ||
344 | .init = decode_init, | ||
345 | .close = decode_end, | ||
346 | FF_CODEC_DECODE_CB(decode_frame), | ||
347 | .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS, | ||
348 | }; | ||
349 |