3 * Copyright (c) 2005 Roine Gustafsson
4 * Copyright (c) 2006 Konstantin Shishkov
6 * This file is part of FFmpeg.
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.
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.
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
26 * Lossless Fraps 'FPS1' decoder
27 * @author Roine Gustafsson <roine at users sf net>
28 * @author Konstantin Shishkov
30 * Codec algorithm for version 0 is taken from Transcode <www.transcoding.org>
32 * Version 2 files support by Konstantin Shishkov
36 #include "bitstream.h"
39 #define FPS_TAG MKTAG('F', 'P', 'S', 'x')
41 /* symbol for Huffman tree node */
46 * FIXME one day this should belong to one general framework
55 * local variable storage
57 typedef struct FrapsContext{
58 AVCodecContext *avctx;
68 * @param avctx codec context
69 * @return 0 on success or negative if fails
71 static int decode_init(AVCodecContext *avctx)
73 FrapsContext * const s = avctx->priv_data;
75 avctx->coded_frame = (AVFrame*)&s->frame;
76 avctx->has_b_frames = 0;
77 avctx->pix_fmt= PIX_FMT_NONE; /* set in decode_frame */
80 s->frame.data[0] = NULL;
83 dsputil_init(&s->dsp, avctx);
89 * Comparator - our nodes should ascend by count
90 * but with preserved symbol order
92 static int huff_cmp(const Node *a, const Node *b){
93 return (a->count - b->count)*256 + a->sym - b->sym;
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)
101 if(s != HNODE || !nodes[node].count){
109 get_tree_codes(bits, lens, xlat, nodes, nodes[node].n0, pfx, pl, pos);
111 get_tree_codes(bits, lens, xlat, nodes, nodes[node].n0+1, pfx, pl, pos);
115 static int build_huff_tree(VLC *vlc, Node *nodes, uint8_t *xlat)
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);
127 * decode Fraps v2 packed plane
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)
139 for(i = 0; i < 256; i++){
141 s->nodes[i].count = AV_RL32(src);
143 if(s->nodes[i].count < 0) {
144 av_log(s->avctx, AV_LOG_ERROR, "Symbol count < 0\n");
148 sum += s->nodes[i].count;
153 av_log(s->avctx, AV_LOG_ERROR, "Too high symbol frequencies. Tree construction is not possible\n");
156 qsort(s->nodes, 256, sizeof(Node), huff_cmp);
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]);
168 if(build_huff_tree(&vlc, s->nodes, recode) < 0){
169 av_log(s->avctx, AV_LOG_ERROR, "Error building tree\n");
172 /* we have built Huffman table and are ready to decode plane */
174 /* convert bits so they may be used by standard bitreader */
175 s->dsp.bswap_buf(s->tmpbuf, src, size >> 2);
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
184 if(j) dst[i] += dst[i - stride];
185 else if(Uoff) dst[i] += 0x80;
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
202 static int decode_frame(AVCodecContext *avctx,
203 void *data, int *data_size,
204 uint8_t *buf, int buf_size)
206 FrapsContext * const s = avctx->priv_data;
207 AVFrame *frame = data;
208 AVFrame * const f = (AVFrame*)&s->frame;
210 unsigned int version,header_size;
213 uint32_t *luma1,*luma2,*cb,*cr;
215 int i, is_chroma, planes;
218 header = AV_RL32(buf);
219 version = header & 0xff;
220 header_size = (header & (1<<30))? 8 : 4; /* bit 30 means pad to 8 bytes */
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);
230 if (header_size == 8)
236 /* Fraps v0 is a reordered YUV420 */
237 avctx->pix_fmt = PIX_FMT_YUV420P;
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);
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);
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");
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;
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++);
285 /* Fraps v1 is an upside-down BGR24 */
286 avctx->pix_fmt = PIX_FMT_BGR24;
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);
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");
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;
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],
319 * Fraps v2 is Huffman-coded YUV420 planes
320 * Fraps v4 is virtually the same
322 avctx->pix_fmt = PIX_FMT_YUV420P;
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");
334 f->pict_type = FF_P_TYPE;
338 f->pict_type = FF_I_TYPE;
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");
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);
351 offs[planes] = buf_size;
352 for(i = 0; i < planes; 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);
365 *data_size = sizeof(AVFrame);
373 * @param avctx codec context
374 * @return 0 on success or negative if fails
376 static int decode_end(AVCodecContext *avctx)
378 FrapsContext *s = (FrapsContext*)avctx->priv_data;
380 if (s->frame.data[0])
381 avctx->release_buffer(avctx, &s->frame);
383 av_freep(&s->tmpbuf);
388 AVCodec fraps_decoder = {
392 sizeof(FrapsContext),