237cb743f9255e56004069995eaae15a2a11f20a
[libav.git] / libavcodec / fraps.c
1 /*
2 * Fraps FPS1 decoder
3 * Copyright (c) 2005 Roine Gustafsson
4 * Copyright (c) 2006 Konstantin Shishkov
5 *
6 * This file is part of Libav.
7 *
8 * Libav 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 * Libav 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 Libav; 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 "avcodec.h"
35 #include "get_bits.h"
36 #include "huffman.h"
37 #include "bytestream.h"
38 #include "dsputil.h"
39
40 #define FPS_TAG MKTAG('F', 'P', 'S', 'x')
41
42 /**
43 * local variable storage
44 */
45 typedef struct FrapsContext {
46 AVCodecContext *avctx;
47 AVFrame frame;
48 uint8_t *tmpbuf;
49 int tmpbuf_size;
50 DSPContext dsp;
51 } FrapsContext;
52
53
54 /**
55 * initializes decoder
56 * @param avctx codec context
57 * @return 0 on success or negative if fails
58 */
59 static av_cold int decode_init(AVCodecContext *avctx)
60 {
61 FrapsContext * const s = avctx->priv_data;
62
63 avctx->coded_frame = &s->frame;
64 avctx->pix_fmt = AV_PIX_FMT_NONE; /* set in decode_frame */
65
66 s->avctx = avctx;
67 s->tmpbuf = NULL;
68
69 ff_dsputil_init(&s->dsp, avctx);
70
71 return 0;
72 }
73
74 /**
75 * Comparator - our nodes should ascend by count
76 * but with preserved symbol order
77 */
78 static int huff_cmp(const void *va, const void *vb)
79 {
80 const Node *a = va, *b = vb;
81 return (a->count - b->count)*256 + a->sym - b->sym;
82 }
83
84 /**
85 * decode Fraps v2 packed plane
86 */
87 static int fraps2_decode_plane(FrapsContext *s, uint8_t *dst, int stride, int w,
88 int h, const uint8_t *src, int size, int Uoff,
89 const int step)
90 {
91 int i, j, ret;
92 GetBitContext gb;
93 VLC vlc;
94 Node nodes[512];
95
96 for (i = 0; i < 256; i++)
97 nodes[i].count = bytestream_get_le32(&src);
98 size -= 1024;
99 if ((ret = ff_huff_build_tree(s->avctx, &vlc, 256, nodes, huff_cmp,
100 FF_HUFFMAN_FLAG_ZERO_COUNT)) < 0)
101 return ret;
102 /* we have built Huffman table and are ready to decode plane */
103
104 /* convert bits so they may be used by standard bitreader */
105 s->dsp.bswap_buf((uint32_t *)s->tmpbuf, (const uint32_t *)src, size >> 2);
106
107 init_get_bits(&gb, s->tmpbuf, size * 8);
108 for (j = 0; j < h; j++) {
109 for (i = 0; i < w*step; i += step) {
110 dst[i] = get_vlc2(&gb, vlc.table, 9, 3);
111 /* lines are stored as deltas between previous lines
112 * and we need to add 0x80 to the first lines of chroma planes
113 */
114 if (j)
115 dst[i] += dst[i - stride];
116 else if (Uoff)
117 dst[i] += 0x80;
118 if (get_bits_left(&gb) < 0) {
119 ff_free_vlc(&vlc);
120 return AVERROR_INVALIDDATA;
121 }
122 }
123 dst += stride;
124 }
125 ff_free_vlc(&vlc);
126 return 0;
127 }
128
129 static int decode_frame(AVCodecContext *avctx,
130 void *data, int *got_frame,
131 AVPacket *avpkt)
132 {
133 FrapsContext * const s = avctx->priv_data;
134 const uint8_t *buf = avpkt->data;
135 int buf_size = avpkt->size;
136 AVFrame *frame = data;
137 AVFrame * const f = &s->frame;
138 uint32_t header;
139 unsigned int version,header_size;
140 unsigned int x, y;
141 const uint32_t *buf32;
142 uint32_t *luma1,*luma2,*cb,*cr;
143 uint32_t offs[4];
144 int i, j, ret, is_chroma, planes;
145 enum AVPixelFormat pix_fmt;
146
147 header = AV_RL32(buf);
148 version = header & 0xff;
149 header_size = (header & (1<<30))? 8 : 4; /* bit 30 means pad to 8 bytes */
150
151 if (version > 5) {
152 av_log(avctx, AV_LOG_ERROR,
153 "This file is encoded with Fraps version %d. " \
154 "This codec can only decode versions <= 5.\n", version);
155 return AVERROR_PATCHWELCOME;
156 }
157
158 buf += 4;
159 if (header_size == 8)
160 buf += 4;
161
162 pix_fmt = version & 1 ? AV_PIX_FMT_BGR24 : AV_PIX_FMT_YUVJ420P;
163 if (avctx->pix_fmt != pix_fmt && f->data[0]) {
164 avctx->release_buffer(avctx, f);
165 }
166 avctx->pix_fmt = pix_fmt;
167
168 switch (version) {
169 case 0:
170 default:
171 /* Fraps v0 is a reordered YUV420 */
172 if ((buf_size != avctx->width * avctx->height * 3 / 2 + header_size) &&
173 (buf_size != header_size)) {
174 av_log(avctx, AV_LOG_ERROR,
175 "Invalid frame length %d (should be %d)\n",
176 buf_size,
177 avctx->width * avctx->height * 3 / 2 + header_size);
178 return AVERROR_INVALIDDATA;
179 }
180
181 if (((avctx->width % 8) != 0) || ((avctx->height % 2) != 0)) {
182 av_log(avctx, AV_LOG_ERROR, "Invalid frame size %dx%d\n",
183 avctx->width, avctx->height);
184 return AVERROR_INVALIDDATA;
185 }
186
187 f->reference = 1;
188 f->buffer_hints = FF_BUFFER_HINTS_VALID |
189 FF_BUFFER_HINTS_PRESERVE |
190 FF_BUFFER_HINTS_REUSABLE;
191 if ((ret = avctx->reget_buffer(avctx, f)) < 0) {
192 av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
193 return ret;
194 }
195 /* bit 31 means same as previous pic */
196 f->pict_type = (header & (1U << 31)) ? AV_PICTURE_TYPE_P : AV_PICTURE_TYPE_I;
197 f->key_frame = f->pict_type == AV_PICTURE_TYPE_I;
198
199 if (f->pict_type == AV_PICTURE_TYPE_I) {
200 buf32 = (const uint32_t*)buf;
201 for (y = 0; y < avctx->height / 2; y++) {
202 luma1 = (uint32_t*)&f->data[0][ y * 2 * f->linesize[0]];
203 luma2 = (uint32_t*)&f->data[0][(y * 2 + 1) * f->linesize[0]];
204 cr = (uint32_t*)&f->data[1][ y * f->linesize[1]];
205 cb = (uint32_t*)&f->data[2][ y * f->linesize[2]];
206 for (x = 0; x < avctx->width; x += 8) {
207 *(luma1++) = *(buf32++);
208 *(luma1++) = *(buf32++);
209 *(luma2++) = *(buf32++);
210 *(luma2++) = *(buf32++);
211 *(cr++) = *(buf32++);
212 *(cb++) = *(buf32++);
213 }
214 }
215 }
216 break;
217
218 case 1:
219 /* Fraps v1 is an upside-down BGR24 */
220 if ((buf_size != avctx->width * avctx->height * 3 + header_size) &&
221 (buf_size != header_size) ) {
222 av_log(avctx, AV_LOG_ERROR,
223 "Invalid frame length %d (should be %d)\n",
224 buf_size, avctx->width * avctx->height * 3 + header_size);
225 return AVERROR_INVALIDDATA;
226 }
227
228 f->reference = 1;
229 f->buffer_hints = FF_BUFFER_HINTS_VALID |
230 FF_BUFFER_HINTS_PRESERVE |
231 FF_BUFFER_HINTS_REUSABLE;
232 if ((ret = avctx->reget_buffer(avctx, f)) < 0) {
233 av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
234 return ret;
235 }
236 /* bit 31 means same as previous pic */
237 f->pict_type = (header & (1U<<31))? AV_PICTURE_TYPE_P : AV_PICTURE_TYPE_I;
238 f->key_frame = f->pict_type == AV_PICTURE_TYPE_I;
239
240 if (f->pict_type == AV_PICTURE_TYPE_I) {
241 for (y = 0; y<avctx->height; y++)
242 memcpy(&f->data[0][(avctx->height - y - 1) * f->linesize[0]],
243 &buf[y * avctx->width * 3],
244 3 * avctx->width);
245 }
246 break;
247
248 case 2:
249 case 4:
250 /**
251 * Fraps v2 is Huffman-coded YUV420 planes
252 * Fraps v4 is virtually the same
253 */
254 planes = 3;
255 f->reference = 1;
256 f->buffer_hints = FF_BUFFER_HINTS_VALID |
257 FF_BUFFER_HINTS_PRESERVE |
258 FF_BUFFER_HINTS_REUSABLE;
259 if ((ret = avctx->reget_buffer(avctx, f)) < 0) {
260 av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
261 return ret;
262 }
263 /* skip frame */
264 if (buf_size == 8) {
265 f->pict_type = AV_PICTURE_TYPE_P;
266 f->key_frame = 0;
267 break;
268 }
269 f->pict_type = AV_PICTURE_TYPE_I;
270 f->key_frame = 1;
271 if ((AV_RL32(buf) != FPS_TAG) || (buf_size < (planes * 1024 + 24))) {
272 av_log(avctx, AV_LOG_ERROR, "Fraps: error in data stream\n");
273 return AVERROR_INVALIDDATA;
274 }
275 for (i = 0; i < planes; i++) {
276 offs[i] = AV_RL32(buf + 4 + i * 4);
277 if (offs[i] >= buf_size || (i && offs[i] <= offs[i - 1] + 1024)) {
278 av_log(avctx, AV_LOG_ERROR, "Fraps: plane %i offset is out of bounds\n", i);
279 return AVERROR_INVALIDDATA;
280 }
281 }
282 offs[planes] = buf_size;
283 for (i = 0; i < planes; i++) {
284 is_chroma = !!i;
285 av_fast_padded_malloc(&s->tmpbuf, &s->tmpbuf_size,
286 offs[i + 1] - offs[i] - 1024);
287 if (!s->tmpbuf)
288 return AVERROR(ENOMEM);
289 if ((ret = fraps2_decode_plane(s, f->data[i], f->linesize[i],
290 avctx->width >> is_chroma,
291 avctx->height >> is_chroma,
292 buf + offs[i], offs[i + 1] - offs[i],
293 is_chroma, 1)) < 0) {
294 av_log(avctx, AV_LOG_ERROR, "Error decoding plane %i\n", i);
295 return ret;
296 }
297 }
298 break;
299 case 3:
300 case 5:
301 /* Virtually the same as version 4, but is for RGB24 */
302 planes = 3;
303 f->reference = 1;
304 f->buffer_hints = FF_BUFFER_HINTS_VALID |
305 FF_BUFFER_HINTS_PRESERVE |
306 FF_BUFFER_HINTS_REUSABLE;
307 if ((ret = avctx->reget_buffer(avctx, f)) < 0) {
308 av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
309 return ret;
310 }
311 /* skip frame */
312 if (buf_size == 8) {
313 f->pict_type = AV_PICTURE_TYPE_P;
314 f->key_frame = 0;
315 break;
316 }
317 f->pict_type = AV_PICTURE_TYPE_I;
318 f->key_frame = 1;
319 if ((AV_RL32(buf) != FPS_TAG)||(buf_size < (planes*1024 + 24))) {
320 av_log(avctx, AV_LOG_ERROR, "Fraps: error in data stream\n");
321 return AVERROR_INVALIDDATA;
322 }
323 for (i = 0; i < planes; i++) {
324 offs[i] = AV_RL32(buf + 4 + i * 4);
325 if (offs[i] >= buf_size || (i && offs[i] <= offs[i - 1] + 1024)) {
326 av_log(avctx, AV_LOG_ERROR, "Fraps: plane %i offset is out of bounds\n", i);
327 return AVERROR_INVALIDDATA;
328 }
329 }
330 offs[planes] = buf_size;
331 for (i = 0; i < planes; i++) {
332 av_fast_padded_malloc(&s->tmpbuf, &s->tmpbuf_size,
333 offs[i + 1] - offs[i] - 1024);
334 if (!s->tmpbuf)
335 return AVERROR(ENOMEM);
336 if ((ret = fraps2_decode_plane(s, f->data[0] + i + (f->linesize[0] * (avctx->height - 1)),
337 -f->linesize[0], avctx->width, avctx->height,
338 buf + offs[i], offs[i + 1] - offs[i], 0, 3)) < 0) {
339 av_log(avctx, AV_LOG_ERROR, "Error decoding plane %i\n", i);
340 return ret;
341 }
342 }
343 // convert pseudo-YUV into real RGB
344 for (j = 0; j < avctx->height; j++) {
345 for (i = 0; i < avctx->width; i++) {
346 f->data[0][0 + i*3 + j*f->linesize[0]] += f->data[0][1 + i*3 + j*f->linesize[0]];
347 f->data[0][2 + i*3 + j*f->linesize[0]] += f->data[0][1 + i*3 + j*f->linesize[0]];
348 }
349 }
350 break;
351 }
352
353 *frame = *f;
354 *got_frame = 1;
355
356 return buf_size;
357 }
358
359
360 /**
361 * closes decoder
362 * @param avctx codec context
363 * @return 0 on success or negative if fails
364 */
365 static av_cold int decode_end(AVCodecContext *avctx)
366 {
367 FrapsContext *s = (FrapsContext*)avctx->priv_data;
368
369 if (s->frame.data[0])
370 avctx->release_buffer(avctx, &s->frame);
371
372 av_freep(&s->tmpbuf);
373 return 0;
374 }
375
376
377 AVCodec ff_fraps_decoder = {
378 .name = "fraps",
379 .type = AVMEDIA_TYPE_VIDEO,
380 .id = AV_CODEC_ID_FRAPS,
381 .priv_data_size = sizeof(FrapsContext),
382 .init = decode_init,
383 .close = decode_end,
384 .decode = decode_frame,
385 .capabilities = CODEC_CAP_DR1,
386 .long_name = NULL_IF_CONFIG_SMALL("Fraps"),
387 };