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