3 * Copyright (c) 2005 Roine Gustafsson
5 * This file is part of FFmpeg.
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.
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.
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
25 * Lossless Fraps 'FPS1' decoder
26 * @author Roine Gustafsson <roine at users sf net>
28 * Only decodes version 0 and 1 files.
29 * Codec algorithm for version 0 is taken from Transcode <www.transcoding.org>
31 * Version 2 files, which are the most commonly found Fraps files, cannot be
37 #define FPS_TAG MKTAG('F', 'P', 'S', 'x')
40 * local variable storage
42 typedef struct FrapsContext{
43 AVCodecContext *avctx;
50 * @param avctx codec context
51 * @return 0 on success or negative if fails
53 static int decode_init(AVCodecContext *avctx)
55 FrapsContext * const s = avctx->priv_data;
57 avctx->coded_frame = (AVFrame*)&s->frame;
58 avctx->has_b_frames = 0;
59 avctx->pix_fmt= PIX_FMT_NONE; /* set in decode_frame */
62 s->frame.data[0] = NULL;
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
77 static int decode_frame(AVCodecContext *avctx,
78 void *data, int *data_size,
79 uint8_t *buf, int buf_size)
81 FrapsContext * const s = avctx->priv_data;
82 AVFrame *frame = data;
83 AVFrame * const f = (AVFrame*)&s->frame;
85 unsigned int version,header_size;
88 uint32_t *luma1,*luma2,*cb,*cr;
92 version = header & 0xff;
93 header_size = (header & (1<<30))? 8 : 4; /* bit 30 means pad to 8 bytes */
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);
103 if (header_size == 8)
109 /* Fraps v0 is a reordered YUV420 */
110 avctx->pix_fmt = PIX_FMT_YUV420P;
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);
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);
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");
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;
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++);
158 /* Fraps v1 is an upside-down BGR24 */
159 avctx->pix_fmt = PIX_FMT_BGR24;
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);
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");
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;
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],
191 * Fraps v2 sub-header description. All numbers are little-endian:
192 * (this is all guesswork)
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
201 * off_2: 256xDWORD freqtbl_2 frequency table for plane 2
204 * off_3: 256xDWORD freqtbl_3 frequency table for plane 3
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");
218 *data_size = sizeof(AVFrame);
226 * @param avctx codec context
227 * @return 0 on success or negative if fails
229 static int decode_end(AVCodecContext *avctx)
231 FrapsContext *s = (FrapsContext*)avctx->priv_data;
233 if (s->frame.data[0])
234 avctx->release_buffer(avctx, &s->frame);
240 AVCodec fraps_decoder = {
244 sizeof(FrapsContext),