]> git.sesse.net Git - ffmpeg/blob - libavcodec/fraps.c
Use pointers to avoid copying AVFrame.
[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 /**
25  * @file fraps.c
26  * Lossless Fraps 'FPS1' decoder
27  * @author Roine Gustafsson <roine at users sf net>
28  * @author Konstantin Shishkov
29  *
30  * Codec algorithm for version 0 is taken from Transcode <www.transcoding.org>
31  *
32  * Version 2 files support by Konstantin Shishkov
33  */
34
35 #include "avcodec.h"
36 #include "bitstream.h"
37 #include "dsputil.h"
38
39 #define FPS_TAG MKTAG('F', 'P', 'S', 'x')
40
41 /* symbol for Huffman tree node */
42 #define HNODE -1
43
44 /**
45  * Huffman node
46  * FIXME one day this should belong to one general framework
47  */
48 typedef struct Node{
49     int16_t sym;
50     int16_t n0;
51     int count;
52 }Node;
53
54 /**
55  * local variable storage
56  */
57 typedef struct FrapsContext{
58     AVCodecContext *avctx;
59     AVFrame frame;
60     Node nodes[512];
61     uint8_t *tmpbuf;
62     DSPContext dsp;
63 } FrapsContext;
64
65
66 /**
67  * initializes decoder
68  * @param avctx codec context
69  * @return 0 on success or negative if fails
70  */
71 static int decode_init(AVCodecContext *avctx)
72 {
73     FrapsContext * const s = avctx->priv_data;
74
75     avctx->coded_frame = (AVFrame*)&s->frame;
76     avctx->has_b_frames = 0;
77     avctx->pix_fmt= PIX_FMT_NONE; /* set in decode_frame */
78
79     s->avctx = avctx;
80     s->frame.data[0] = NULL;
81     s->tmpbuf = NULL;
82
83     dsputil_init(&s->dsp, avctx);
84
85     return 0;
86 }
87
88 /**
89  * Comparator - our nodes should ascend by count
90  * but with preserved symbol order
91  */
92 static int huff_cmp(const Node *a, const Node *b){
93     return (a->count - b->count)*256 + a->sym - b->sym;
94 }
95
96 static void get_tree_codes(uint32_t *bits, int16_t *lens, uint8_t *xlat, Node *nodes, int node, uint32_t pfx, int pl, int *pos)
97 {
98     int s;
99
100     s = nodes[node].sym;
101     if(s != HNODE || !nodes[node].count){
102         bits[*pos] = pfx;
103         lens[*pos] = pl;
104         xlat[*pos] = s;
105         (*pos)++;
106     }else{
107         pfx <<= 1;
108         pl++;
109         get_tree_codes(bits, lens, xlat, nodes, nodes[node].n0, pfx, pl, pos);
110         pfx |= 1;
111         get_tree_codes(bits, lens, xlat, nodes, nodes[node].n0+1, pfx, pl, pos);
112     }
113 }
114
115 static int build_huff_tree(VLC *vlc, Node *nodes, uint8_t *xlat)
116 {
117     uint32_t bits[256];
118     int16_t lens[256];
119     int pos = 0;
120
121     get_tree_codes(bits, lens, xlat, nodes, 510, 0, 0, &pos);
122     return init_vlc(vlc, 9, pos, lens, 2, 2, bits, 4, 4, 0);
123 }
124
125
126 /**
127  * decode Fraps v2 packed plane
128  */
129 static int fraps2_decode_plane(FrapsContext *s, uint8_t *dst, int stride, int w,
130                                int h, uint8_t *src, int size, int Uoff)
131 {
132     int i, j;
133     int cur_node;
134     GetBitContext gb;
135     VLC vlc;
136     int64_t sum = 0;
137     uint8_t recode[256];
138
139     for(i = 0; i < 256; i++){
140         s->nodes[i].sym = i;
141         s->nodes[i].count = AV_RL32(src);
142         s->nodes[i].n0 = -2;
143         if(s->nodes[i].count < 0) {
144             av_log(s->avctx, AV_LOG_ERROR, "Symbol count < 0\n");
145             return -1;
146         }
147         src += 4;
148         sum += s->nodes[i].count;
149     }
150     size -= 1024;
151
152     if(sum >> 31) {
153         av_log(s->avctx, AV_LOG_ERROR, "Too high symbol frequencies. Tree construction is not possible\n");
154         return -1;
155     }
156     qsort(s->nodes, 256, sizeof(Node), huff_cmp);
157     cur_node = 256;
158     for(i = 0; i < 511; i += 2){
159         s->nodes[cur_node].sym = HNODE;
160         s->nodes[cur_node].count = s->nodes[i].count + s->nodes[i+1].count;
161         s->nodes[cur_node].n0 = i;
162         for(j = cur_node; j > 0; j--){
163             if(s->nodes[j].count >= s->nodes[j - 1].count) break;
164             FFSWAP(Node, s->nodes[j], s->nodes[j - 1]);
165         }
166         cur_node++;
167     }
168     if(build_huff_tree(&vlc, s->nodes, recode) < 0){
169         av_log(s->avctx, AV_LOG_ERROR, "Error building tree\n");
170         return -1;
171     }
172     /* we have built Huffman table and are ready to decode plane */
173
174     /* convert bits so they may be used by standard bitreader */
175     s->dsp.bswap_buf(s->tmpbuf, src, size >> 2);
176
177     init_get_bits(&gb, s->tmpbuf, size * 8);
178     for(j = 0; j < h; j++){
179         for(i = 0; i < w; i++){
180             dst[i] = recode[get_vlc2(&gb, vlc.table, 9, 3)];
181             /* lines are stored as deltas between previous lines
182              * and we need to add 0x80 to the first lines of chroma planes
183              */
184             if(j) dst[i] += dst[i - stride];
185             else if(Uoff) dst[i] += 0x80;
186         }
187         dst += stride;
188     }
189     free_vlc(&vlc);
190     return 0;
191 }
192
193 /**
194  * decode a frame
195  * @param avctx codec context
196  * @param data output AVFrame
197  * @param data_size size of output data or 0 if no picture is returned
198  * @param buf input data frame
199  * @param buf_size size of input data frame
200  * @return number of consumed bytes on success or negative if decode fails
201  */
202 static int decode_frame(AVCodecContext *avctx,
203                         void *data, int *data_size,
204                         uint8_t *buf, int buf_size)
205 {
206     FrapsContext * const s = avctx->priv_data;
207     AVFrame *frame = data;
208     AVFrame * const f = (AVFrame*)&s->frame;
209     uint32_t header;
210     unsigned int version,header_size;
211     unsigned int x, y;
212     uint32_t *buf32;
213     uint32_t *luma1,*luma2,*cb,*cr;
214     uint32_t offs[4];
215     int i, is_chroma, planes;
216
217
218     header = AV_RL32(buf);
219     version = header & 0xff;
220     header_size = (header & (1<<30))? 8 : 4; /* bit 30 means pad to 8 bytes */
221
222     if (version > 2 && version != 4) {
223         av_log(avctx, AV_LOG_ERROR,
224                "This file is encoded with Fraps version %d. " \
225                "This codec can only decode version 0, 1, 2 and 4.\n", version);
226         return -1;
227     }
228
229     buf+=4;
230     if (header_size == 8)
231         buf+=4;
232
233     switch(version) {
234     case 0:
235     default:
236         /* Fraps v0 is a reordered YUV420 */
237         avctx->pix_fmt = PIX_FMT_YUV420P;
238
239         if ( (buf_size != avctx->width*avctx->height*3/2+header_size) &&
240              (buf_size != header_size) ) {
241             av_log(avctx, AV_LOG_ERROR,
242                    "Invalid frame length %d (should be %d)\n",
243                    buf_size, avctx->width*avctx->height*3/2+header_size);
244             return -1;
245         }
246
247         if (( (avctx->width % 8) != 0) || ( (avctx->height % 2) != 0 )) {
248             av_log(avctx, AV_LOG_ERROR, "Invalid frame size %dx%d\n",
249                    avctx->width, avctx->height);
250             return -1;
251         }
252
253         f->reference = 1;
254         f->buffer_hints = FF_BUFFER_HINTS_VALID |
255                           FF_BUFFER_HINTS_PRESERVE |
256                           FF_BUFFER_HINTS_REUSABLE;
257         if (avctx->reget_buffer(avctx, f)) {
258             av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
259             return -1;
260         }
261         /* bit 31 means same as previous pic */
262         f->pict_type = (header & (1<<31))? FF_P_TYPE : FF_I_TYPE;
263         f->key_frame = f->pict_type == FF_I_TYPE;
264
265         if (f->pict_type == FF_I_TYPE) {
266             buf32=(uint32_t*)buf;
267             for(y=0; y<avctx->height/2; y++){
268                 luma1=(uint32_t*)&f->data[0][ y*2*f->linesize[0] ];
269                 luma2=(uint32_t*)&f->data[0][ (y*2+1)*f->linesize[0] ];
270                 cr=(uint32_t*)&f->data[1][ y*f->linesize[1] ];
271                 cb=(uint32_t*)&f->data[2][ y*f->linesize[2] ];
272                 for(x=0; x<avctx->width; x+=8){
273                     *(luma1++) = *(buf32++);
274                     *(luma1++) = *(buf32++);
275                     *(luma2++) = *(buf32++);
276                     *(luma2++) = *(buf32++);
277                     *(cr++) = *(buf32++);
278                     *(cb++) = *(buf32++);
279                 }
280             }
281         }
282         break;
283
284     case 1:
285         /* Fraps v1 is an upside-down BGR24 */
286         avctx->pix_fmt = PIX_FMT_BGR24;
287
288         if ( (buf_size != avctx->width*avctx->height*3+header_size) &&
289              (buf_size != header_size) ) {
290             av_log(avctx, AV_LOG_ERROR,
291                    "Invalid frame length %d (should be %d)\n",
292                    buf_size, avctx->width*avctx->height*3+header_size);
293             return -1;
294         }
295
296         f->reference = 1;
297         f->buffer_hints = FF_BUFFER_HINTS_VALID |
298                           FF_BUFFER_HINTS_PRESERVE |
299                           FF_BUFFER_HINTS_REUSABLE;
300         if (avctx->reget_buffer(avctx, f)) {
301             av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
302             return -1;
303         }
304         /* bit 31 means same as previous pic */
305         f->pict_type = (header & (1<<31))? FF_P_TYPE : FF_I_TYPE;
306         f->key_frame = f->pict_type == FF_I_TYPE;
307
308         if (f->pict_type == FF_I_TYPE) {
309             for(y=0; y<avctx->height; y++)
310                 memcpy(&f->data[0][ (avctx->height-y)*f->linesize[0] ],
311                        &buf[y*avctx->width*3],
312                        f->linesize[0]);
313         }
314         break;
315
316     case 2:
317     case 4:
318         /**
319          * Fraps v2 is Huffman-coded YUV420 planes
320          * Fraps v4 is virtually the same
321          */
322         avctx->pix_fmt = PIX_FMT_YUV420P;
323         planes = 3;
324         f->reference = 1;
325         f->buffer_hints = FF_BUFFER_HINTS_VALID |
326                           FF_BUFFER_HINTS_PRESERVE |
327                           FF_BUFFER_HINTS_REUSABLE;
328         if (avctx->reget_buffer(avctx, f)) {
329             av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
330             return -1;
331         }
332         /* skip frame */
333         if(buf_size == 8) {
334             f->pict_type = FF_P_TYPE;
335             f->key_frame = 0;
336             break;
337         }
338         f->pict_type = FF_I_TYPE;
339         f->key_frame = 1;
340         if ((AV_RL32(buf) != FPS_TAG)||(buf_size < (planes*1024 + 24))) {
341             av_log(avctx, AV_LOG_ERROR, "Fraps: error in data stream\n");
342             return -1;
343         }
344         for(i = 0; i < planes; i++) {
345             offs[i] = AV_RL32(buf + 4 + i * 4);
346             if(offs[i] >= buf_size || (i && offs[i] <= offs[i - 1] + 1024)) {
347                 av_log(avctx, AV_LOG_ERROR, "Fraps: plane %i offset is out of bounds\n", i);
348                 return -1;
349             }
350         }
351         offs[planes] = buf_size;
352         for(i = 0; i < planes; i++){
353             is_chroma = !!i;
354             s->tmpbuf = av_realloc(s->tmpbuf, offs[i + 1] - offs[i] - 1024 + FF_INPUT_BUFFER_PADDING_SIZE);
355             if(fraps2_decode_plane(s, f->data[i], f->linesize[i], avctx->width >> is_chroma,
356                     avctx->height >> is_chroma, buf + offs[i], offs[i + 1] - offs[i], is_chroma) < 0) {
357                 av_log(avctx, AV_LOG_ERROR, "Error decoding plane %i\n", i);
358                 return -1;
359             }
360         }
361         break;
362     }
363
364     *frame = *f;
365     *data_size = sizeof(AVFrame);
366
367     return buf_size;
368 }
369
370
371 /**
372  * closes decoder
373  * @param avctx codec context
374  * @return 0 on success or negative if fails
375  */
376 static int decode_end(AVCodecContext *avctx)
377 {
378     FrapsContext *s = (FrapsContext*)avctx->priv_data;
379
380     if (s->frame.data[0])
381         avctx->release_buffer(avctx, &s->frame);
382
383     av_freep(&s->tmpbuf);
384     return 0;
385 }
386
387
388 AVCodec fraps_decoder = {
389     "fraps",
390     CODEC_TYPE_VIDEO,
391     CODEC_ID_FRAPS,
392     sizeof(FrapsContext),
393     decode_init,
394     NULL,
395     decode_end,
396     decode_frame,
397     CODEC_CAP_DR1,
398 };