3 * Copyright (c) 2005 Roine Gustafsson
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 * Lossless Fraps 'FPS1' decoder
24 * @author Roine Gustafsson <roine at users sf net>
26 * Only decodes version 0 and 1 files.
27 * Codec algorithm for version 0 is taken from Transcode <www.transcoding.org>
29 * Version 2 files, which are the most commonly found Fraps files, cannot be
35 #define FPS_TAG MKTAG('F', 'P', 'S', 'x')
38 * local variable storage
40 typedef struct FrapsContext{
41 AVCodecContext *avctx;
48 * @param avctx codec context
49 * @return 0 on success or negative if fails
51 static int decode_init(AVCodecContext *avctx)
53 FrapsContext * const s = avctx->priv_data;
55 avctx->coded_frame = (AVFrame*)&s->frame;
56 avctx->has_b_frames = 0;
57 avctx->pix_fmt= PIX_FMT_NONE; /* set in decode_frame */
60 s->frame.data[0] = NULL;
68 * @param avctx codec context
69 * @param data output AVFrame
70 * @param data_size size of output data or 0 if no picture is returned
71 * @param buf input data frame
72 * @param buf_size size of input data frame
73 * @return number of consumed bytes on success or negative if decode fails
75 static int decode_frame(AVCodecContext *avctx,
76 void *data, int *data_size,
77 uint8_t *buf, int buf_size)
79 FrapsContext * const s = avctx->priv_data;
80 AVFrame *frame = data;
81 AVFrame * const f = (AVFrame*)&s->frame;
83 unsigned int version,header_size;
86 uint32_t *luma1,*luma2,*cb,*cr;
90 version = header & 0xff;
91 header_size = (header & (1<<30))? 8 : 4; /* bit 30 means pad to 8 bytes */
94 av_log(avctx, AV_LOG_ERROR,
95 "This file is encoded with Fraps version %d. " \
96 "This codec can only decode version 0 and 1.\n", version);
101 if (header_size == 8)
107 /* Fraps v0 is a reordered YUV420 */
108 avctx->pix_fmt = PIX_FMT_YUV420P;
110 if ( (buf_size != avctx->width*avctx->height*3/2+header_size) &&
111 (buf_size != header_size) ) {
112 av_log(avctx, AV_LOG_ERROR,
113 "Invalid frame length %d (should be %d)\n",
114 buf_size, avctx->width*avctx->height*3/2+header_size);
118 if (( (avctx->width % 8) != 0) || ( (avctx->height % 2) != 0 )) {
119 av_log(avctx, AV_LOG_ERROR, "Invalid frame size %dx%d\n",
120 avctx->width, avctx->height);
125 f->buffer_hints = FF_BUFFER_HINTS_VALID |
126 FF_BUFFER_HINTS_PRESERVE |
127 FF_BUFFER_HINTS_REUSABLE;
128 if (avctx->reget_buffer(avctx, f)) {
129 av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
132 /* bit 31 means same as previous pic */
133 f->pict_type = (header & (1<<31))? FF_P_TYPE : FF_I_TYPE;
134 f->key_frame = f->pict_type == FF_I_TYPE;
136 if (f->pict_type == FF_I_TYPE) {
137 buf32=(uint32_t*)buf;
138 for(y=0; y<avctx->height/2; y++){
139 luma1=(uint32_t*)&f->data[0][ y*2*f->linesize[0] ];
140 luma2=(uint32_t*)&f->data[0][ (y*2+1)*f->linesize[0] ];
141 cr=(uint32_t*)&f->data[1][ y*f->linesize[1] ];
142 cb=(uint32_t*)&f->data[2][ y*f->linesize[2] ];
143 for(x=0; x<avctx->width; x+=8){
144 *(luma1++) = *(buf32++);
145 *(luma1++) = *(buf32++);
146 *(luma2++) = *(buf32++);
147 *(luma2++) = *(buf32++);
148 *(cr++) = *(buf32++);
149 *(cb++) = *(buf32++);
156 /* Fraps v1 is an upside-down BGR24 */
157 avctx->pix_fmt = PIX_FMT_BGR24;
159 if ( (buf_size != avctx->width*avctx->height*3+header_size) &&
160 (buf_size != header_size) ) {
161 av_log(avctx, AV_LOG_ERROR,
162 "Invalid frame length %d (should be %d)\n",
163 buf_size, avctx->width*avctx->height*3+header_size);
168 f->buffer_hints = FF_BUFFER_HINTS_VALID |
169 FF_BUFFER_HINTS_PRESERVE |
170 FF_BUFFER_HINTS_REUSABLE;
171 if (avctx->reget_buffer(avctx, f)) {
172 av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
175 /* bit 31 means same as previous pic */
176 f->pict_type = (header & (1<<31))? FF_P_TYPE : FF_I_TYPE;
177 f->key_frame = f->pict_type == FF_I_TYPE;
179 if (f->pict_type == FF_I_TYPE) {
180 for(y=0; y<avctx->height; y++)
181 memcpy(&f->data[0][ (avctx->height-y)*f->linesize[0] ],
182 &buf[y*avctx->width*3],
189 * Fraps v2 sub-header description. All numbers are little-endian:
190 * (this is all guesswork)
193 * 4: DWORD 0x00000010 unknown, perhaps flags
194 * 8: DWORD off_2 offset to plane 2
195 * 12: DWORD off_3 offset to plane 3
196 * 16: 256xDWORD freqtbl_1 frequency table for plane 1
199 * off_2: 256xDWORD freqtbl_2 frequency table for plane 2
202 * off_3: 256xDWORD freqtbl_3 frequency table for plane 3
205 if ((BE_32(buf) != FPS_TAG)||(buf_size < (3*1024 + 8))) {
206 av_log(avctx, AV_LOG_ERROR, "Fraps: error in data stream\n");
216 *data_size = sizeof(AVFrame);
224 * @param avctx codec context
225 * @return 0 on success or negative if fails
227 static int decode_end(AVCodecContext *avctx)
229 FrapsContext *s = (FrapsContext*)avctx->priv_data;
231 if (s->frame.data[0])
232 avctx->release_buffer(avctx, &s->frame);
238 AVCodec fraps_decoder = {
242 sizeof(FrapsContext),