]> git.sesse.net Git - ffmpeg/blob - libavcodec/fraps.c
Replace Subversion revisions in comments by Git hashes.
[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 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     DSPContext dsp;
50 } FrapsContext;
51
52
53 /**
54  * initializes decoder
55  * @param avctx codec context
56  * @return 0 on success or negative if fails
57  */
58 static av_cold int decode_init(AVCodecContext *avctx)
59 {
60     FrapsContext * const s = avctx->priv_data;
61
62     avctx->coded_frame = (AVFrame*)&s->frame;
63     avctx->pix_fmt= PIX_FMT_NONE; /* set in decode_frame */
64
65     s->avctx = avctx;
66     s->tmpbuf = NULL;
67
68     dsputil_init(&s->dsp, avctx);
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     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;
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 (ff_huff_build_tree(s->avctx, &vlc, 256, nodes, huff_cmp,
98                            FF_HUFFMAN_FLAG_ZERO_COUNT) < 0)
99         return -1;
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) dst[i] += dst[i - stride];
113             else if(Uoff) dst[i] += 0x80;
114             if (get_bits_left(&gb) < 0) {
115                 free_vlc(&vlc);
116                 return AVERROR_INVALIDDATA;
117             }
118         }
119         dst += stride;
120     }
121     free_vlc(&vlc);
122     return 0;
123 }
124
125 static int decode_frame(AVCodecContext *avctx,
126                         void *data, int *data_size,
127                         AVPacket *avpkt)
128 {
129     const uint8_t *buf = avpkt->data;
130     int buf_size = avpkt->size;
131     FrapsContext * const s = avctx->priv_data;
132     AVFrame *frame = data;
133     AVFrame * const f = (AVFrame*)&s->frame;
134     uint32_t header;
135     unsigned int version,header_size;
136     unsigned int x, y;
137     const uint32_t *buf32;
138     uint32_t *luma1,*luma2,*cb,*cr;
139     uint32_t offs[4];
140     int i, j, is_chroma, planes;
141
142
143     header = AV_RL32(buf);
144     version = header & 0xff;
145     header_size = (header & (1<<30))? 8 : 4; /* bit 30 means pad to 8 bytes */
146
147     if (version > 5) {
148         av_log(avctx, AV_LOG_ERROR,
149                "This file is encoded with Fraps version %d. " \
150                "This codec can only decode versions <= 5.\n", version);
151         return -1;
152     }
153
154     buf+=4;
155     if (header_size == 8)
156         buf+=4;
157
158     switch(version) {
159     case 0:
160     default:
161         /* Fraps v0 is a reordered YUV420 */
162         avctx->pix_fmt = PIX_FMT_YUVJ420P;
163
164         if ( (buf_size != avctx->width*avctx->height*3/2+header_size) &&
165              (buf_size != header_size) ) {
166             av_log(avctx, AV_LOG_ERROR,
167                    "Invalid frame length %d (should be %d)\n",
168                    buf_size, avctx->width*avctx->height*3/2+header_size);
169             return -1;
170         }
171
172         if (( (avctx->width % 8) != 0) || ( (avctx->height % 2) != 0 )) {
173             av_log(avctx, AV_LOG_ERROR, "Invalid frame size %dx%d\n",
174                    avctx->width, avctx->height);
175             return -1;
176         }
177
178         f->reference = 1;
179         f->buffer_hints = FF_BUFFER_HINTS_VALID |
180                           FF_BUFFER_HINTS_PRESERVE |
181                           FF_BUFFER_HINTS_REUSABLE;
182         if (avctx->reget_buffer(avctx, f)) {
183             av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
184             return -1;
185         }
186         /* bit 31 means same as previous pic */
187         f->pict_type = (header & (1U<<31))? AV_PICTURE_TYPE_P : AV_PICTURE_TYPE_I;
188         f->key_frame = f->pict_type == AV_PICTURE_TYPE_I;
189
190         if (f->pict_type == AV_PICTURE_TYPE_I) {
191             buf32=(const uint32_t*)buf;
192             for(y=0; y<avctx->height/2; y++){
193                 luma1=(uint32_t*)&f->data[0][ y*2*f->linesize[0] ];
194                 luma2=(uint32_t*)&f->data[0][ (y*2+1)*f->linesize[0] ];
195                 cr=(uint32_t*)&f->data[1][ y*f->linesize[1] ];
196                 cb=(uint32_t*)&f->data[2][ y*f->linesize[2] ];
197                 for(x=0; x<avctx->width; x+=8){
198                     *(luma1++) = *(buf32++);
199                     *(luma1++) = *(buf32++);
200                     *(luma2++) = *(buf32++);
201                     *(luma2++) = *(buf32++);
202                     *(cr++) = *(buf32++);
203                     *(cb++) = *(buf32++);
204                 }
205             }
206         }
207         break;
208
209     case 1:
210         /* Fraps v1 is an upside-down BGR24 */
211         avctx->pix_fmt = PIX_FMT_BGR24;
212
213         if ( (buf_size != avctx->width*avctx->height*3+header_size) &&
214              (buf_size != header_size) ) {
215             av_log(avctx, AV_LOG_ERROR,
216                    "Invalid frame length %d (should be %d)\n",
217                    buf_size, avctx->width*avctx->height*3+header_size);
218             return -1;
219         }
220
221         f->reference = 1;
222         f->buffer_hints = FF_BUFFER_HINTS_VALID |
223                           FF_BUFFER_HINTS_PRESERVE |
224                           FF_BUFFER_HINTS_REUSABLE;
225         if (avctx->reget_buffer(avctx, f)) {
226             av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
227             return -1;
228         }
229         /* bit 31 means same as previous pic */
230         f->pict_type = (header & (1U<<31))? AV_PICTURE_TYPE_P : AV_PICTURE_TYPE_I;
231         f->key_frame = f->pict_type == AV_PICTURE_TYPE_I;
232
233         if (f->pict_type == AV_PICTURE_TYPE_I) {
234             for(y=0; y<avctx->height; y++)
235                 memcpy(&f->data[0][ (avctx->height-y)*f->linesize[0] ],
236                        &buf[y*avctx->width*3],
237                        3*avctx->width);
238         }
239         break;
240
241     case 2:
242     case 4:
243         /**
244          * Fraps v2 is Huffman-coded YUV420 planes
245          * Fraps v4 is virtually the same
246          */
247         avctx->pix_fmt = PIX_FMT_YUVJ420P;
248         planes = 3;
249         f->reference = 1;
250         f->buffer_hints = FF_BUFFER_HINTS_VALID |
251                           FF_BUFFER_HINTS_PRESERVE |
252                           FF_BUFFER_HINTS_REUSABLE;
253         if (avctx->reget_buffer(avctx, f)) {
254             av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
255             return -1;
256         }
257         /* skip frame */
258         if(buf_size == 8) {
259             f->pict_type = AV_PICTURE_TYPE_P;
260             f->key_frame = 0;
261             break;
262         }
263         f->pict_type = AV_PICTURE_TYPE_I;
264         f->key_frame = 1;
265         if ((AV_RL32(buf) != FPS_TAG)||(buf_size < (planes*1024 + 24))) {
266             av_log(avctx, AV_LOG_ERROR, "Fraps: error in data stream\n");
267             return -1;
268         }
269         for(i = 0; i < planes; i++) {
270             offs[i] = AV_RL32(buf + 4 + i * 4);
271             if(offs[i] >= buf_size || (i && offs[i] <= offs[i - 1] + 1024)) {
272                 av_log(avctx, AV_LOG_ERROR, "Fraps: plane %i offset is out of bounds\n", i);
273                 return -1;
274             }
275         }
276         offs[planes] = buf_size;
277         for(i = 0; i < planes; i++){
278             is_chroma = !!i;
279             s->tmpbuf = av_realloc(s->tmpbuf, offs[i + 1] - offs[i] - 1024 + FF_INPUT_BUFFER_PADDING_SIZE);
280             if(fraps2_decode_plane(s, f->data[i], f->linesize[i], avctx->width >> is_chroma,
281                     avctx->height >> is_chroma, buf + offs[i], offs[i + 1] - offs[i], is_chroma, 1) < 0) {
282                 av_log(avctx, AV_LOG_ERROR, "Error decoding plane %i\n", i);
283                 return -1;
284             }
285         }
286         break;
287     case 3:
288     case 5:
289         /* Virtually the same as version 4, but is for RGB24 */
290         avctx->pix_fmt = PIX_FMT_BGR24;
291         planes = 3;
292         f->reference = 1;
293         f->buffer_hints = FF_BUFFER_HINTS_VALID |
294                           FF_BUFFER_HINTS_PRESERVE |
295                           FF_BUFFER_HINTS_REUSABLE;
296         if (avctx->reget_buffer(avctx, f)) {
297             av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
298             return -1;
299         }
300         /* skip frame */
301         if(buf_size == 8) {
302             f->pict_type = AV_PICTURE_TYPE_P;
303             f->key_frame = 0;
304             break;
305         }
306         f->pict_type = AV_PICTURE_TYPE_I;
307         f->key_frame = 1;
308         if ((AV_RL32(buf) != FPS_TAG)||(buf_size < (planes*1024 + 24))) {
309             av_log(avctx, AV_LOG_ERROR, "Fraps: error in data stream\n");
310             return -1;
311         }
312         for(i = 0; i < planes; i++) {
313             offs[i] = AV_RL32(buf + 4 + i * 4);
314             if(offs[i] >= buf_size || (i && offs[i] <= offs[i - 1] + 1024)) {
315                 av_log(avctx, AV_LOG_ERROR, "Fraps: plane %i offset is out of bounds\n", i);
316                 return -1;
317             }
318         }
319         offs[planes] = buf_size;
320         for(i = 0; i < planes; i++){
321             s->tmpbuf = av_realloc(s->tmpbuf, offs[i + 1] - offs[i] - 1024 + FF_INPUT_BUFFER_PADDING_SIZE);
322             if(fraps2_decode_plane(s, f->data[0] + i + (f->linesize[0] * (avctx->height - 1)), -f->linesize[0],
323                     avctx->width, avctx->height, buf + offs[i], offs[i + 1] - offs[i], 0, 3) < 0) {
324                 av_log(avctx, AV_LOG_ERROR, "Error decoding plane %i\n", i);
325                 return -1;
326             }
327         }
328         // convert pseudo-YUV into real RGB
329         for(j = 0; j < avctx->height; j++){
330             for(i = 0; i < avctx->width; i++){
331                 f->data[0][0 + i*3 + j*f->linesize[0]] += f->data[0][1 + i*3 + j*f->linesize[0]];
332                 f->data[0][2 + i*3 + j*f->linesize[0]] += f->data[0][1 + i*3 + j*f->linesize[0]];
333             }
334         }
335         break;
336     }
337
338     *frame = *f;
339     *data_size = sizeof(AVFrame);
340
341     return buf_size;
342 }
343
344
345 /**
346  * closes decoder
347  * @param avctx codec context
348  * @return 0 on success or negative if fails
349  */
350 static av_cold int decode_end(AVCodecContext *avctx)
351 {
352     FrapsContext *s = (FrapsContext*)avctx->priv_data;
353
354     if (s->frame.data[0])
355         avctx->release_buffer(avctx, &s->frame);
356
357     av_freep(&s->tmpbuf);
358     return 0;
359 }
360
361
362 AVCodec ff_fraps_decoder = {
363     .name           = "fraps",
364     .type           = AVMEDIA_TYPE_VIDEO,
365     .id             = CODEC_ID_FRAPS,
366     .priv_data_size = sizeof(FrapsContext),
367     .init           = decode_init,
368     .close          = decode_end,
369     .decode         = decode_frame,
370     .capabilities   = CODEC_CAP_DR1,
371     .long_name = NULL_IF_CONFIG_SMALL("Fraps"),
372 };