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