fraps.c 12.5 KB
Newer Older
1 2 3
/*
 * Fraps FPS1 decoder
 * Copyright (c) 2005 Roine Gustafsson
4
 * Copyright (c) 2006 Konstantin Shishkov
5
 *
6
 * This file is part of Libav.
7
 *
8
 * Libav is free software; you can redistribute it and/or
9 10
 * modify it under the terms of the GNU Lesser General Public
 * 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
 * Libav is distributed in the hope that it will be useful,
14 15 16 17 18
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
19
 * License along with Libav; if not, write to the Free Software
20
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21
 */
22

23
/**
24
 * @file
25
 * Lossless Fraps 'FPS1' decoder
26
 * @author Roine Gustafsson (roine at users sf net)
27
 * @author Konstantin Shishkov
28
 *
29 30
 * Codec algorithm for version 0 is taken from Transcode <www.transcoding.org>
 *
31
 * Version 2 files support by Konstantin Shishkov
32
 */
33

34
#include "avcodec.h"
35
#include "get_bits.h"
36 37
#include "huffman.h"
#include "bytestream.h"
38
#include "bswapdsp.h"
39
#include "internal.h"
40

Mike Melanson's avatar
Mike Melanson committed
41
#define FPS_TAG MKTAG('F', 'P', 'S', 'x')
42 43 44 45

/**
 * local variable storage
 */
46
typedef struct FrapsContext {
47
    AVCodecContext *avctx;
48
    BswapDSPContext bdsp;
49
    AVFrame *frame;
50
    uint8_t *tmpbuf;
51
    int tmpbuf_size;
52 53 54 55 56 57 58 59
} FrapsContext;


/**
 * initializes decoder
 * @param avctx codec context
 * @return 0 on success or negative if fails
 */
60
static av_cold int decode_init(AVCodecContext *avctx)
61 62 63
{
    FrapsContext * const s = avctx->priv_data;

64
    avctx->pix_fmt     = AV_PIX_FMT_NONE; /* set in decode_frame */
65

66
    s->avctx  = avctx;
67 68
    s->tmpbuf = NULL;

69 70 71
    s->frame = av_frame_alloc();
    if (!s->frame)
        return AVERROR(ENOMEM);
72

73
    ff_bswapdsp_init(&s->bdsp);
74 75 76 77

    return 0;
}

78 79 80 81
/**
 * Comparator - our nodes should ascend by count
 * but with preserved symbol order
 */
82 83
static int huff_cmp(const void *va, const void *vb)
{
84
    const Node *a = va, *b = vb;
85 86 87 88 89 90 91
    return (a->count - b->count)*256 + a->sym - b->sym;
}

/**
 * decode Fraps v2 packed plane
 */
static int fraps2_decode_plane(FrapsContext *s, uint8_t *dst, int stride, int w,
92 93
                               int h, const uint8_t *src, int size, int Uoff,
                               const int step)
94
{
95
    int i, j, ret;
96 97
    GetBitContext gb;
    VLC vlc;
98
    Node nodes[512];
99

100
    for (i = 0; i < 256; i++)
101 102
        nodes[i].count = bytestream_get_le32(&src);
    size -= 1024;
103 104 105
    if ((ret = ff_huff_build_tree(s->avctx, &vlc, 256, nodes, huff_cmp,
                                  FF_HUFFMAN_FLAG_ZERO_COUNT)) < 0)
        return ret;
106 107 108
    /* we have built Huffman table and are ready to decode plane */

    /* convert bits so they may be used by standard bitreader */
109 110
    s->bdsp.bswap_buf((uint32_t *) s->tmpbuf,
                      (const uint32_t *) src, size >> 2);
111 112

    init_get_bits(&gb, s->tmpbuf, size * 8);
113 114
    for (j = 0; j < h; j++) {
        for (i = 0; i < w*step; i += step) {
115
            dst[i] = get_vlc2(&gb, vlc.table, 9, 3);
116 117 118
            /* lines are stored as deltas between previous lines
             * and we need to add 0x80 to the first lines of chroma planes
             */
119 120 121 122
            if (j)
                dst[i] += dst[i - stride];
            else if (Uoff)
                dst[i] += 0x80;
123
            if (get_bits_left(&gb) < 0) {
124
                ff_free_vlc(&vlc);
125 126
                return AVERROR_INVALIDDATA;
            }
127 128 129
        }
        dst += stride;
    }
130
    ff_free_vlc(&vlc);
131 132
    return 0;
}
133

134
static int decode_frame(AVCodecContext *avctx,
135
                        void *data, int *got_frame,
136
                        AVPacket *avpkt)
137 138
{
    FrapsContext * const s = avctx->priv_data;
139 140 141
    const uint8_t *buf     = avpkt->data;
    int buf_size           = avpkt->size;
    AVFrame *frame         = data;
142
    AVFrame * const f      = s->frame;
143 144 145
    uint32_t header;
    unsigned int version,header_size;
    unsigned int x, y;
Michael Niedermayer's avatar
Michael Niedermayer committed
146
    const uint32_t *buf32;
147
    uint32_t *luma1,*luma2,*cb,*cr;
148
    uint32_t offs[4];
149
    int i, j, ret, is_chroma, planes;
150
    enum AVPixelFormat pix_fmt;
151 152 153 154 155 156
    int prev_pic_bit, expected_size;

    if (buf_size < 4) {
        av_log(avctx, AV_LOG_ERROR, "Packet is too short\n");
        return AVERROR_INVALIDDATA;
    }
157

158 159
    header      = AV_RL32(buf);
    version     = header & 0xff;
160
    header_size = (header & (1<<30))? 8 : 4; /* bit 30 means pad to 8 bytes */
161
    prev_pic_bit = header & (1U << 31); /* bit 31 means same as previous pic */
162

163
    if (version > 5) {
164
        av_log(avctx, AV_LOG_ERROR,
165
               "This file is encoded with Fraps version %d. " \
166
               "This codec can only decode versions <= 5.\n", version);
167
        return AVERROR_PATCHWELCOME;
168 169
    }

170
    buf += 4;
171
    if (header_size == 8)
172
        buf += 4;
173

174
    pix_fmt = version & 1 ? AV_PIX_FMT_BGR24 : AV_PIX_FMT_YUVJ420P;
175
    if (avctx->pix_fmt != pix_fmt && f->data[0]) {
176
        av_frame_unref(f);
177 178
    }
    avctx->pix_fmt = pix_fmt;
179 180
    avctx->color_range = version & 1 ? AVCOL_RANGE_UNSPECIFIED
                                     : AVCOL_RANGE_JPEG;
181

182 183
    expected_size = header_size;

184
    switch (version) {
185 186 187
    case 0:
    default:
        /* Fraps v0 is a reordered YUV420 */
188 189 190
        if (!prev_pic_bit)
            expected_size += avctx->width * avctx->height * 3 / 2;
        if (buf_size != expected_size) {
191
            av_log(avctx, AV_LOG_ERROR,
192
                   "Invalid frame length %d (should be %d)\n",
193
                   buf_size, expected_size);
194
            return AVERROR_INVALIDDATA;
195
        }
196

197
        if (((avctx->width % 8) != 0) || ((avctx->height % 2) != 0)) {
198
            av_log(avctx, AV_LOG_ERROR, "Invalid frame size %dx%d\n",
199
                   avctx->width, avctx->height);
200
            return AVERROR_INVALIDDATA;
201 202
        }

203
        if ((ret = ff_reget_buffer(avctx, f)) < 0) {
204
            av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
205
            return ret;
206
        }
207
        f->pict_type = prev_pic_bit ? AV_PICTURE_TYPE_P : AV_PICTURE_TYPE_I;
208
        f->key_frame = f->pict_type == AV_PICTURE_TYPE_I;
209

210
        if (f->pict_type == AV_PICTURE_TYPE_I) {
211 212 213 214 215 216 217
            buf32 = (const uint32_t*)buf;
            for (y = 0; y < avctx->height / 2; y++) {
                luma1 = (uint32_t*)&f->data[0][ y * 2      * f->linesize[0]];
                luma2 = (uint32_t*)&f->data[0][(y * 2 + 1) * f->linesize[0]];
                cr    = (uint32_t*)&f->data[1][ y          * f->linesize[1]];
                cb    = (uint32_t*)&f->data[2][ y          * f->linesize[2]];
                for (x = 0; x < avctx->width; x += 8) {
218 219 220 221 222 223 224 225 226 227 228 229 230
                    *(luma1++) = *(buf32++);
                    *(luma1++) = *(buf32++);
                    *(luma2++) = *(buf32++);
                    *(luma2++) = *(buf32++);
                    *(cr++) = *(buf32++);
                    *(cb++) = *(buf32++);
                }
            }
        }
        break;

    case 1:
        /* Fraps v1 is an upside-down BGR24 */
231 232 233
        if (!prev_pic_bit)
            expected_size += avctx->width * avctx->height * 3;
        if (buf_size != expected_size) {
234
            av_log(avctx, AV_LOG_ERROR,
235
                   "Invalid frame length %d (should be %d)\n",
236
                   buf_size, expected_size);
237
            return AVERROR_INVALIDDATA;
238 239
        }

240
        if ((ret = ff_reget_buffer(avctx, f)) < 0) {
241
            av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
242
            return ret;
243
        }
244
        f->pict_type = prev_pic_bit ? AV_PICTURE_TYPE_P : AV_PICTURE_TYPE_I;
245
        f->key_frame = f->pict_type == AV_PICTURE_TYPE_I;
246

247
        if (f->pict_type == AV_PICTURE_TYPE_I) {
248
            for (y = 0; y<avctx->height; y++)
249
                memcpy(&f->data[0][(avctx->height - y - 1) * f->linesize[0]],
250 251
                       &buf[y * avctx->width * 3],
                       3 * avctx->width);
252 253 254 255
        }
        break;

    case 2:
256
    case 4:
257
        /**
258
         * Fraps v2 is Huffman-coded YUV420 planes
259
         * Fraps v4 is virtually the same
260
         */
261
        planes = 3;
262
        if ((ret = ff_reget_buffer(avctx, f)) < 0) {
263
            av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
264
            return ret;
265 266
        }
        /* skip frame */
267
        if (buf_size == 8) {
268
            f->pict_type = AV_PICTURE_TYPE_P;
269 270 271
            f->key_frame = 0;
            break;
        }
272
        f->pict_type = AV_PICTURE_TYPE_I;
273
        f->key_frame = 1;
274
        if ((AV_RL32(buf) != FPS_TAG) || (buf_size < (planes * 1024 + 24))) {
275
            av_log(avctx, AV_LOG_ERROR, "Fraps: error in data stream\n");
276
            return AVERROR_INVALIDDATA;
277
        }
278
        for (i = 0; i < planes; i++) {
279
            offs[i] = AV_RL32(buf + 4 + i * 4);
280
            if (offs[i] >= buf_size || (i && offs[i] <= offs[i - 1] + 1024)) {
281
                av_log(avctx, AV_LOG_ERROR, "Fraps: plane %i offset is out of bounds\n", i);
282
                return AVERROR_INVALIDDATA;
283 284 285
            }
        }
        offs[planes] = buf_size;
286
        for (i = 0; i < planes; i++) {
287
            is_chroma = !!i;
288 289 290 291
            av_fast_padded_malloc(&s->tmpbuf, &s->tmpbuf_size,
                                  offs[i + 1] - offs[i] - 1024);
            if (!s->tmpbuf)
                return AVERROR(ENOMEM);
292 293 294 295 296
            if ((ret = fraps2_decode_plane(s, f->data[i], f->linesize[i],
                                           avctx->width  >> is_chroma,
                                           avctx->height >> is_chroma,
                                           buf + offs[i], offs[i + 1] - offs[i],
                                           is_chroma, 1)) < 0) {
297
                av_log(avctx, AV_LOG_ERROR, "Error decoding plane %i\n", i);
298
                return ret;
299 300
            }
        }
301
        break;
302
    case 3:
303 304 305
    case 5:
        /* Virtually the same as version 4, but is for RGB24 */
        planes = 3;
306
        if ((ret = ff_reget_buffer(avctx, f)) < 0) {
307
            av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
308
            return ret;
309 310
        }
        /* skip frame */
311
        if (buf_size == 8) {
312
            f->pict_type = AV_PICTURE_TYPE_P;
313 314 315
            f->key_frame = 0;
            break;
        }
316
        f->pict_type = AV_PICTURE_TYPE_I;
317 318 319
        f->key_frame = 1;
        if ((AV_RL32(buf) != FPS_TAG)||(buf_size < (planes*1024 + 24))) {
            av_log(avctx, AV_LOG_ERROR, "Fraps: error in data stream\n");
320
            return AVERROR_INVALIDDATA;
321
        }
322
        for (i = 0; i < planes; i++) {
323
            offs[i] = AV_RL32(buf + 4 + i * 4);
324
            if (offs[i] >= buf_size || (i && offs[i] <= offs[i - 1] + 1024)) {
325
                av_log(avctx, AV_LOG_ERROR, "Fraps: plane %i offset is out of bounds\n", i);
326
                return AVERROR_INVALIDDATA;
327 328 329
            }
        }
        offs[planes] = buf_size;
330
        for (i = 0; i < planes; i++) {
331 332 333 334
            av_fast_padded_malloc(&s->tmpbuf, &s->tmpbuf_size,
                                  offs[i + 1] - offs[i] - 1024);
            if (!s->tmpbuf)
                return AVERROR(ENOMEM);
335 336 337
            if ((ret = fraps2_decode_plane(s, f->data[0] + i + (f->linesize[0] * (avctx->height - 1)),
                                           -f->linesize[0], avctx->width, avctx->height,
                                           buf + offs[i], offs[i + 1] - offs[i], 0, 3)) < 0) {
338
                av_log(avctx, AV_LOG_ERROR, "Error decoding plane %i\n", i);
339
                return ret;
340 341
            }
        }
342
        // convert pseudo-YUV into real RGB
343 344
        for (j = 0; j < avctx->height; j++) {
            for (i = 0; i < avctx->width; i++) {
345 346
                f->data[0][0 + i*3 + j*f->linesize[0]] += f->data[0][1 + i*3 + j*f->linesize[0]];
                f->data[0][2 + i*3 + j*f->linesize[0]] += f->data[0][1 + i*3 + j*f->linesize[0]];
347 348
            }
        }
349
        break;
350 351
    }

352 353
    if ((ret = av_frame_ref(frame, f)) < 0)
        return ret;
354
    *got_frame = 1;
355 356 357 358 359 360 361 362 363 364

    return buf_size;
}


/**
 * closes decoder
 * @param avctx codec context
 * @return 0 on success or negative if fails
 */
365
static av_cold int decode_end(AVCodecContext *avctx)
366 367 368
{
    FrapsContext *s = (FrapsContext*)avctx->priv_data;

369
    av_frame_free(&s->frame);
370

371
    av_freep(&s->tmpbuf);
372 373 374 375
    return 0;
}


376
AVCodec ff_fraps_decoder = {
377
    .name           = "fraps",
378
    .long_name      = NULL_IF_CONFIG_SMALL("Fraps"),
379
    .type           = AVMEDIA_TYPE_VIDEO,
380
    .id             = AV_CODEC_ID_FRAPS,
381 382 383 384 385
    .priv_data_size = sizeof(FrapsContext),
    .init           = decode_init,
    .close          = decode_end,
    .decode         = decode_frame,
    .capabilities   = CODEC_CAP_DR1,
386
};