fraps.c 10.9 KB
Newer Older
1 2 3
/*
 * Fraps FPS1 decoder
 * Copyright (c) 2005 Roine Gustafsson
4
 * Copyright (c) 2006 Konstantin Shishkov
5
 *
6 7 8
 * This file is part of FFmpeg.
 *
 * FFmpeg 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
 * FFmpeg 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 FFmpeg; 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
#include "thread.h"
41

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

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


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

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

68
    ff_bswapdsp_init(&s->bdsp);
69 70 71 72

    return 0;
}

73 74 75 76
/**
 * Comparator - our nodes should ascend by count
 * but with preserved symbol order
 */
77 78
static int huff_cmp(const void *va, const void *vb)
{
79
    const Node *a = va, *b = vb;
80 81 82 83 84 85 86
    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,
87 88
                               int h, const uint8_t *src, int size, int Uoff,
                               const int step)
89
{
90
    int i, j, ret;
91 92
    GetBitContext gb;
    VLC vlc;
93
    Node nodes[512];
94

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

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

108 109 110
    if ((ret = init_get_bits8(&gb, s->tmpbuf, size)) < 0)
        return ret;

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

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

152 153 154 155
    if (buf_size < 4) {
        av_log(avctx, AV_LOG_ERROR, "Packet is too short\n");
        return AVERROR_INVALIDDATA;
    }
156

157 158
    header      = AV_RL32(buf);
    version     = header & 0xff;
159
    is_pal      = buf[1] == 2 && version == 1;
160 161
    header_size = (header & (1<<30))? 8 : 4; /* bit 30 means pad to 8 bytes */

162
    if (version > 5) {
163
        avpriv_report_missing_feature(avctx, "Fraps version %u", version);
164
        return AVERROR_PATCHWELCOME;
165 166
    }

167
    buf += header_size;
168

169 170 171 172 173 174 175 176 177 178
    if (is_pal) {
        unsigned needed_size = avctx->width * avctx->height + 1024;
        needed_size += header_size;
        if (buf_size != needed_size) {
            av_log(avctx, AV_LOG_ERROR,
                   "Invalid frame length %d (should be %d)\n",
                   buf_size, needed_size);
            return AVERROR_INVALIDDATA;
        }
    } else if (version < 2) {
179
        unsigned needed_size = avctx->width * avctx->height * 3;
180 181
        if (version == 0) needed_size /= 2;
        needed_size += header_size;
182 183
        /* bit 31 means same as previous pic */
        if (header & (1U<<31)) {
184
            *got_frame = 0;
185 186
            return buf_size;
        }
187 188 189 190
        if (buf_size != needed_size) {
            av_log(avctx, AV_LOG_ERROR,
                   "Invalid frame length %d (should be %d)\n",
                   buf_size, needed_size);
191
            return AVERROR_INVALIDDATA;
192
        }
193 194 195
    } else {
        /* skip frame */
        if (buf_size == 8) {
196
            *got_frame = 0;
197 198
            return buf_size;
        }
199
        if (AV_RL32(buf) != FPS_TAG || buf_size < planes*1024 + 24) {
200
            av_log(avctx, AV_LOG_ERROR, "error in data stream\n");
201
            return AVERROR_INVALIDDATA;
202
        }
203
        for (i = 0; i < planes; i++) {
204
            offs[i] = AV_RL32(buf + 4 + i * 4);
205
            if (offs[i] >= buf_size - header_size || (i && offs[i] <= offs[i - 1] + 1024)) {
206
                av_log(avctx, AV_LOG_ERROR, "plane %i offset is out of bounds\n", i);
207
                return AVERROR_INVALIDDATA;
208 209
            }
        }
210
        offs[planes] = buf_size - header_size;
211
        for (i = 0; i < planes; i++) {
212 213 214 215
            av_fast_padded_malloc(&s->tmpbuf, &s->tmpbuf_size, offs[i + 1] - offs[i] - 1024);
            if (!s->tmpbuf)
                return AVERROR(ENOMEM);
        }
216 217
    }

218 219
    f->pict_type = AV_PICTURE_TYPE_I;
    f->key_frame = 1;
220

221
    avctx->pix_fmt = version & 1 ? is_pal ? AV_PIX_FMT_PAL8 : AV_PIX_FMT_BGR24 : AV_PIX_FMT_YUVJ420P;
222 223
    avctx->color_range = version & 1 ? AVCOL_RANGE_UNSPECIFIED
                                     : AVCOL_RANGE_JPEG;
224
    avctx->colorspace = version & 1 ? AVCOL_SPC_UNSPECIFIED : AVCOL_SPC_BT709;
225

226
    if ((ret = ff_thread_get_buffer(avctx, &frame, 0)) < 0)
227
        return ret;
228

229
    switch (version) {
230 231 232
    case 0:
    default:
        /* Fraps v0 is a reordered YUV420 */
233
        if (((avctx->width % 8) != 0) || ((avctx->height % 2) != 0)) {
234
            av_log(avctx, AV_LOG_ERROR, "Invalid frame size %dx%d\n",
235
                   avctx->width, avctx->height);
236
            return AVERROR_INVALIDDATA;
237 238
        }

239 240 241 242 243 244 245
        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) {
246 247 248 249 250 251
                *luma1++ = *buf32++;
                *luma1++ = *buf32++;
                *luma2++ = *buf32++;
                *luma2++ = *buf32++;
                *cr++    = *buf32++;
                *cb++    = *buf32++;
252
            }
253
        }
254 255 256
        break;

    case 1:
257 258 259 260 261 262 263 264 265 266 267 268 269
        if (is_pal) {
            uint32_t *pal = (uint32_t *)f->data[1];

            for (y = 0; y < 256; y++) {
                pal[y] = AV_RL32(buf) | 0xFF000000;
                buf += 4;
            }

            for (y = 0; y <avctx->height; y++)
                memcpy(&f->data[0][y * f->linesize[0]],
                       &buf[y * avctx->width],
                       avctx->width);
        } else {
270
        /* Fraps v1 is an upside-down BGR24 */
271
            for (y = 0; y<avctx->height; y++)
272
                memcpy(&f->data[0][(avctx->height - y - 1) * f->linesize[0]],
273 274
                       &buf[y * avctx->width * 3],
                       3 * avctx->width);
275
        }
276 277 278
        break;

    case 2:
279
    case 4:
280
        /**
281
         * Fraps v2 is Huffman-coded YUV420 planes
282
         * Fraps v4 is virtually the same
283
         */
284
        for (i = 0; i < planes; i++) {
285
            is_chroma = !!i;
286 287 288 289 290
            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) {
291
                av_log(avctx, AV_LOG_ERROR, "Error decoding plane %i\n", i);
292
                return ret;
293 294
            }
        }
295
        break;
296
    case 3:
297 298
    case 5:
        /* Virtually the same as version 4, but is for RGB24 */
299
        for (i = 0; i < planes; i++) {
300 301 302
            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) {
303
                av_log(avctx, AV_LOG_ERROR, "Error decoding plane %i\n", i);
304
                return ret;
305 306
            }
        }
307
        out = f->data[0];
308
        // convert pseudo-YUV into real RGB
309
        for (j = 0; j < avctx->height; j++) {
310 311 312 313 314
            uint8_t *line_end = out + 3*avctx->width;
            while (out < line_end) {
                out[0]  += out[1];
                out[2]  += out[1];
                out += 3;
315
            }
316
            out += f->linesize[0] - 3*avctx->width;
317
        }
318
        break;
319 320
    }

321
    *got_frame = 1;
322 323 324 325 326 327 328 329 330 331

    return buf_size;
}


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

336
    av_freep(&s->tmpbuf);
337 338 339 340
    return 0;
}


341
AVCodec ff_fraps_decoder = {
342
    .name           = "fraps",
343
    .long_name      = NULL_IF_CONFIG_SMALL("Fraps"),
344
    .type           = AVMEDIA_TYPE_VIDEO,
345
    .id             = AV_CODEC_ID_FRAPS,
346 347 348 349
    .priv_data_size = sizeof(FrapsContext),
    .init           = decode_init,
    .close          = decode_end,
    .decode         = decode_frame,
350
    .capabilities   = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS,
351
    .caps_internal  = FF_CODEC_CAP_INIT_THREADSAFE,
352
};