|
808
|
1 /*
|
|
|
2 * Fraps FPS1 decoder
|
|
|
3 * Copyright (c) 2005 Roine Gustafsson
|
|
|
4 *
|
|
|
5 * This file is part of FFmpeg.
|
|
|
6 *
|
|
|
7 * FFmpeg is free software; you can redistribute it and/or
|
|
|
8 * modify it under the terms of the GNU Lesser General Public
|
|
|
9 * License as published by the Free Software Foundation; either
|
|
|
10 * version 2.1 of the License, or (at your option) any later version.
|
|
|
11 *
|
|
|
12 * FFmpeg is distributed in the hope that it will be useful,
|
|
|
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
15 * Lesser General Public License for more details.
|
|
|
16 *
|
|
|
17 * You should have received a copy of the GNU Lesser General Public
|
|
|
18 * License along with FFmpeg; if not, write to the Free Software
|
|
|
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
20 *
|
|
|
21 */
|
|
|
22
|
|
|
23 /**
|
|
|
24 * @file fraps.c
|
|
|
25 * Lossless Fraps 'FPS1' decoder
|
|
|
26 * @author Roine Gustafsson <roine at users sf net>
|
|
|
27 *
|
|
|
28 * Only decodes version 0 and 1 files.
|
|
|
29 * Codec algorithm for version 0 is taken from Transcode <www.transcoding.org>
|
|
|
30 *
|
|
|
31 * Version 2 files, which are the most commonly found Fraps files, cannot be
|
|
|
32 * decoded yet.
|
|
|
33 */
|
|
|
34
|
|
|
35 #include "avcodec.h"
|
|
|
36
|
|
|
37 #define FPS_TAG MKTAG('F', 'P', 'S', 'x')
|
|
|
38
|
|
|
39 /**
|
|
|
40 * local variable storage
|
|
|
41 */
|
|
|
42 typedef struct FrapsContext{
|
|
|
43 AVCodecContext *avctx;
|
|
|
44 AVFrame frame;
|
|
|
45 } FrapsContext;
|
|
|
46
|
|
|
47
|
|
|
48 /**
|
|
|
49 * initializes decoder
|
|
|
50 * @param avctx codec context
|
|
|
51 * @return 0 on success or negative if fails
|
|
|
52 */
|
|
|
53 static int decode_init(AVCodecContext *avctx)
|
|
|
54 {
|
|
|
55 FrapsContext * const s = avctx->priv_data;
|
|
|
56
|
|
|
57 avctx->coded_frame = (AVFrame*)&s->frame;
|
|
|
58 avctx->has_b_frames = 0;
|
|
|
59 avctx->pix_fmt= PIX_FMT_NONE; /* set in decode_frame */
|
|
|
60
|
|
|
61 s->avctx = avctx;
|
|
|
62 s->frame.data[0] = NULL;
|
|
|
63
|
|
|
64 return 0;
|
|
|
65 }
|
|
|
66
|
|
|
67
|
|
|
68 /**
|
|
|
69 * decode a frame
|
|
|
70 * @param avctx codec context
|
|
|
71 * @param data output AVFrame
|
|
|
72 * @param data_size size of output data or 0 if no picture is returned
|
|
|
73 * @param buf input data frame
|
|
|
74 * @param buf_size size of input data frame
|
|
|
75 * @return number of consumed bytes on success or negative if decode fails
|
|
|
76 */
|
|
|
77 static int decode_frame(AVCodecContext *avctx,
|
|
|
78 void *data, int *data_size,
|
|
|
79 uint8_t *buf, int buf_size)
|
|
|
80 {
|
|
|
81 FrapsContext * const s = avctx->priv_data;
|
|
|
82 AVFrame *frame = data;
|
|
|
83 AVFrame * const f = (AVFrame*)&s->frame;
|
|
|
84 uint32_t header;
|
|
|
85 unsigned int version,header_size;
|
|
|
86 unsigned int x, y;
|
|
|
87 uint32_t *buf32;
|
|
|
88 uint32_t *luma1,*luma2,*cb,*cr;
|
|
|
89
|
|
|
90
|
|
|
91 header = LE_32(buf);
|
|
|
92 version = header & 0xff;
|
|
|
93 header_size = (header & (1<<30))? 8 : 4; /* bit 30 means pad to 8 bytes */
|
|
|
94
|
|
|
95 if (version > 1) {
|
|
|
96 av_log(avctx, AV_LOG_ERROR,
|
|
|
97 "This file is encoded with Fraps version %d. " \
|
|
|
98 "This codec can only decode version 0 and 1.\n", version);
|
|
|
99 return -1;
|
|
|
100 }
|
|
|
101
|
|
|
102 buf+=4;
|
|
|
103 if (header_size == 8)
|
|
|
104 buf+=4;
|
|
|
105
|
|
|
106 switch(version) {
|
|
|
107 case 0:
|
|
|
108 default:
|
|
|
109 /* Fraps v0 is a reordered YUV420 */
|
|
|
110 avctx->pix_fmt = PIX_FMT_YUV420P;
|
|
|
111
|
|
|
112 if ( (buf_size != avctx->width*avctx->height*3/2+header_size) &&
|
|
|
113 (buf_size != header_size) ) {
|
|
|
114 av_log(avctx, AV_LOG_ERROR,
|
|
|
115 "Invalid frame length %d (should be %d)\n",
|
|
|
116 buf_size, avctx->width*avctx->height*3/2+header_size);
|
|
|
117 return -1;
|
|
|
118 }
|
|
|
119
|
|
|
120 if (( (avctx->width % 8) != 0) || ( (avctx->height % 2) != 0 )) {
|
|
|
121 av_log(avctx, AV_LOG_ERROR, "Invalid frame size %dx%d\n",
|
|
|
122 avctx->width, avctx->height);
|
|
|
123 return -1;
|
|
|
124 }
|
|
|
125
|
|
|
126 f->reference = 1;
|
|
|
127 f->buffer_hints = FF_BUFFER_HINTS_VALID |
|
|
|
128 FF_BUFFER_HINTS_PRESERVE |
|
|
|
129 FF_BUFFER_HINTS_REUSABLE;
|
|
|
130 if (avctx->reget_buffer(avctx, f)) {
|
|
|
131 av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
|
|
|
132 return -1;
|
|
|
133 }
|
|
|
134 /* bit 31 means same as previous pic */
|
|
|
135 f->pict_type = (header & (1<<31))? FF_P_TYPE : FF_I_TYPE;
|
|
|
136 f->key_frame = f->pict_type == FF_I_TYPE;
|
|
|
137
|
|
|
138 if (f->pict_type == FF_I_TYPE) {
|
|
|
139 buf32=(uint32_t*)buf;
|
|
|
140 for(y=0; y<avctx->height/2; y++){
|
|
|
141 luma1=(uint32_t*)&f->data[0][ y*2*f->linesize[0] ];
|
|
|
142 luma2=(uint32_t*)&f->data[0][ (y*2+1)*f->linesize[0] ];
|
|
|
143 cr=(uint32_t*)&f->data[1][ y*f->linesize[1] ];
|
|
|
144 cb=(uint32_t*)&f->data[2][ y*f->linesize[2] ];
|
|
|
145 for(x=0; x<avctx->width; x+=8){
|
|
|
146 *(luma1++) = *(buf32++);
|
|
|
147 *(luma1++) = *(buf32++);
|
|
|
148 *(luma2++) = *(buf32++);
|
|
|
149 *(luma2++) = *(buf32++);
|
|
|
150 *(cr++) = *(buf32++);
|
|
|
151 *(cb++) = *(buf32++);
|
|
|
152 }
|
|
|
153 }
|
|
|
154 }
|
|
|
155 break;
|
|
|
156
|
|
|
157 case 1:
|
|
|
158 /* Fraps v1 is an upside-down BGR24 */
|
|
|
159 avctx->pix_fmt = PIX_FMT_BGR24;
|
|
|
160
|
|
|
161 if ( (buf_size != avctx->width*avctx->height*3+header_size) &&
|
|
|
162 (buf_size != header_size) ) {
|
|
|
163 av_log(avctx, AV_LOG_ERROR,
|
|
|
164 "Invalid frame length %d (should be %d)\n",
|
|
|
165 buf_size, avctx->width*avctx->height*3+header_size);
|
|
|
166 return -1;
|
|
|
167 }
|
|
|
168
|
|
|
169 f->reference = 1;
|
|
|
170 f->buffer_hints = FF_BUFFER_HINTS_VALID |
|
|
|
171 FF_BUFFER_HINTS_PRESERVE |
|
|
|
172 FF_BUFFER_HINTS_REUSABLE;
|
|
|
173 if (avctx->reget_buffer(avctx, f)) {
|
|
|
174 av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
|
|
|
175 return -1;
|
|
|
176 }
|
|
|
177 /* bit 31 means same as previous pic */
|
|
|
178 f->pict_type = (header & (1<<31))? FF_P_TYPE : FF_I_TYPE;
|
|
|
179 f->key_frame = f->pict_type == FF_I_TYPE;
|
|
|
180
|
|
|
181 if (f->pict_type == FF_I_TYPE) {
|
|
|
182 for(y=0; y<avctx->height; y++)
|
|
|
183 memcpy(&f->data[0][ (avctx->height-y)*f->linesize[0] ],
|
|
|
184 &buf[y*avctx->width*3],
|
|
|
185 f->linesize[0]);
|
|
|
186 }
|
|
|
187 break;
|
|
|
188
|
|
|
189 case 2:
|
|
|
190 /**
|
|
|
191 * Fraps v2 sub-header description. All numbers are little-endian:
|
|
|
192 * (this is all guesswork)
|
|
|
193 *
|
|
|
194 * 0: DWORD 'FPSx'
|
|
|
195 * 4: DWORD 0x00000010 unknown, perhaps flags
|
|
|
196 * 8: DWORD off_2 offset to plane 2
|
|
|
197 * 12: DWORD off_3 offset to plane 3
|
|
|
198 * 16: 256xDWORD freqtbl_1 frequency table for plane 1
|
|
|
199 * 1040: plane_1
|
|
|
200 * ...
|
|
|
201 * off_2: 256xDWORD freqtbl_2 frequency table for plane 2
|
|
|
202 * plane_2
|
|
|
203 * ...
|
|
|
204 * off_3: 256xDWORD freqtbl_3 frequency table for plane 3
|
|
|
205 * plane_3
|
|
|
206 */
|
|
|
207 if ((BE_32(buf) != FPS_TAG)||(buf_size < (3*1024 + 8))) {
|
|
|
208 av_log(avctx, AV_LOG_ERROR, "Fraps: error in data stream\n");
|
|
|
209 return -1;
|
|
|
210 }
|
|
|
211
|
|
|
212 /* NOT FINISHED */
|
|
|
213
|
|
|
214 break;
|
|
|
215 }
|
|
|
216
|
|
|
217 *frame = *f;
|
|
|
218 *data_size = sizeof(AVFrame);
|
|
|
219
|
|
|
220 return buf_size;
|
|
|
221 }
|
|
|
222
|
|
|
223
|
|
|
224 /**
|
|
|
225 * closes decoder
|
|
|
226 * @param avctx codec context
|
|
|
227 * @return 0 on success or negative if fails
|
|
|
228 */
|
|
|
229 static int decode_end(AVCodecContext *avctx)
|
|
|
230 {
|
|
|
231 FrapsContext *s = (FrapsContext*)avctx->priv_data;
|
|
|
232
|
|
|
233 if (s->frame.data[0])
|
|
|
234 avctx->release_buffer(avctx, &s->frame);
|
|
|
235
|
|
|
236 return 0;
|
|
|
237 }
|
|
|
238
|
|
|
239
|
|
|
240 AVCodec fraps_decoder = {
|
|
|
241 "fraps",
|
|
|
242 CODEC_TYPE_VIDEO,
|
|
|
243 CODEC_ID_FRAPS,
|
|
|
244 sizeof(FrapsContext),
|
|
|
245 decode_init,
|
|
|
246 NULL,
|
|
|
247 decode_end,
|
|
|
248 decode_frame,
|
|
|
249 CODEC_CAP_DR1,
|
|
|
250 };
|