b81f8949 |
/*
* Fraps FPS1 decoder
* Copyright (c) 2005 Roine Gustafsson |
08a4c4bf |
* Copyright (c) 2006 Konstantin Shishkov |
b81f8949 |
* |
b78e7197 |
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or |
b81f8949 |
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either |
b78e7197 |
* version 2.1 of the License, or (at your option) any later version. |
b81f8949 |
* |
b78e7197 |
* FFmpeg is distributed in the hope that it will be useful, |
b81f8949 |
* 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 |
b78e7197 |
* License along with FFmpeg; if not, write to the Free Software |
5509bffa |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
b81f8949 |
*/ |
115329f1 |
|
b81f8949 |
/** |
ba87f080 |
* @file |
b81f8949 |
* Lossless Fraps 'FPS1' decoder |
0baf34d8 |
* @author Roine Gustafsson (roine at users sf net) |
08a4c4bf |
* @author Konstantin Shishkov |
115329f1 |
* |
b81f8949 |
* Codec algorithm for version 0 is taken from Transcode <www.transcoding.org>
* |
08a4c4bf |
* Version 2 files support by Konstantin Shishkov |
b81f8949 |
*/ |
115329f1 |
|
b81f8949 |
#include "avcodec.h" |
9106a698 |
#include "get_bits.h" |
437c2079 |
#include "huffman.h"
#include "bytestream.h" |
08a4c4bf |
#include "dsputil.h" |
759001c5 |
#include "internal.h" |
cd3ced1b |
#include "thread.h" |
b81f8949 |
|
4935b1b9 |
#define FPS_TAG MKTAG('F', 'P', 'S', 'x') |
795eaf8d |
#define VLC_BITS 11 |
b81f8949 |
/**
* local variable storage
*/ |
80344261 |
typedef struct FrapsContext { |
b81f8949 |
AVCodecContext *avctx; |
08a4c4bf |
uint8_t *tmpbuf; |
80da2dcf |
int tmpbuf_size; |
08a4c4bf |
DSPContext dsp; |
b81f8949 |
} FrapsContext;
/**
* initializes decoder
* @param avctx codec context
* @return 0 on success or negative if fails
*/ |
98a6fff9 |
static av_cold int decode_init(AVCodecContext *avctx) |
b81f8949 |
{
FrapsContext * const s = avctx->priv_data;
|
80344261 |
s->avctx = avctx; |
08a4c4bf |
s->tmpbuf = NULL;
|
9cf0841e |
ff_dsputil_init(&s->dsp, avctx); |
b81f8949 |
return 0;
}
|
08a4c4bf |
/**
* Comparator - our nodes should ascend by count
* but with preserved symbol order
*/ |
80344261 |
static int huff_cmp(const void *va, const void *vb)
{ |
859cfdc0 |
const Node *a = va, *b = vb; |
08a4c4bf |
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, |
6a02cb82 |
int h, const uint8_t *src, int size, int Uoff,
const int step) |
08a4c4bf |
{ |
57d11e5e |
int i, j, ret; |
08a4c4bf |
GetBitContext gb;
VLC vlc; |
437c2079 |
Node nodes[512]; |
08a4c4bf |
|
80344261 |
for (i = 0; i < 256; i++) |
437c2079 |
nodes[i].count = bytestream_get_le32(&src);
size -= 1024; |
795eaf8d |
if ((ret = ff_huff_build_tree(s->avctx, &vlc, 256, VLC_BITS, |
2b834a06 |
nodes, huff_cmp, |
57d11e5e |
FF_HUFFMAN_FLAG_ZERO_COUNT)) < 0)
return ret; |
08a4c4bf |
/* we have built Huffman table and are ready to decode plane */
/* convert bits so they may be used by standard bitreader */ |
721052e9 |
s->dsp.bswap_buf((uint32_t *)s->tmpbuf, (const uint32_t *)src, size >> 2); |
08a4c4bf |
init_get_bits(&gb, s->tmpbuf, size * 8); |
80344261 |
for (j = 0; j < h; j++) {
for (i = 0; i < w*step; i += step) { |
795eaf8d |
dst[i] = get_vlc2(&gb, vlc.table, VLC_BITS, 3); |
08a4c4bf |
/* lines are stored as deltas between previous lines
* and we need to add 0x80 to the first lines of chroma planes
*/ |
80344261 |
if (j)
dst[i] += dst[i - stride];
else if (Uoff)
dst[i] += 0x80; |
5ace144f |
if (get_bits_left(&gb) < 0) { |
e96b4a53 |
ff_free_vlc(&vlc); |
5ace144f |
return AVERROR_INVALIDDATA; |
82a1d575 |
} |
08a4c4bf |
}
dst += stride;
} |
e96b4a53 |
ff_free_vlc(&vlc); |
08a4c4bf |
return 0;
} |
b81f8949 |
|
115329f1 |
static int decode_frame(AVCodecContext *avctx, |
df9b9567 |
void *data, int *got_frame, |
7a00bbad |
AVPacket *avpkt) |
b81f8949 |
{
FrapsContext * const s = avctx->priv_data; |
80344261 |
const uint8_t *buf = avpkt->data;
int buf_size = avpkt->size; |
80e9e63c |
ThreadFrame frame = { .f = data };
AVFrame * const f = data; |
b81f8949 |
uint32_t header;
unsigned int version,header_size;
unsigned int x, y; |
1855abe5 |
const uint32_t *buf32; |
b81f8949 |
uint32_t *luma1,*luma2,*cb,*cr; |
08a4c4bf |
uint32_t offs[4]; |
07f22d0b |
int i, j, ret, is_chroma; |
0a403588 |
const int planes = 3; |
7fabef1f |
uint8_t *out; |
b81f8949 |
|
3185a802 |
if (buf_size < 4) {
av_log(avctx, AV_LOG_ERROR, "Packet is too short\n");
return AVERROR_INVALIDDATA;
} |
b81f8949 |
|
80344261 |
header = AV_RL32(buf);
version = header & 0xff; |
b81f8949 |
header_size = (header & (1<<30))? 8 : 4; /* bit 30 means pad to 8 bytes */
|
675f7114 |
if (version > 5) { |
115329f1 |
av_log(avctx, AV_LOG_ERROR, |
b81f8949 |
"This file is encoded with Fraps version %d. " \ |
675f7114 |
"This codec can only decode versions <= 5.\n", version); |
04a585f0 |
return AVERROR_PATCHWELCOME; |
b81f8949 |
}
|
57608d90 |
buf += header_size; |
115329f1 |
|
b4ec645f |
if (version < 2) { |
d3066030 |
unsigned needed_size = avctx->width * avctx->height * 3; |
b4ec645f |
if (version == 0) needed_size /= 2;
needed_size += header_size; |
6a9b565e |
/* bit 31 means same as previous pic */
if (header & (1U<<31)) { |
fc1152de |
*got_frame = 0; |
6a9b565e |
return buf_size;
} |
0bae6661 |
if (buf_size != needed_size) {
av_log(avctx, AV_LOG_ERROR,
"Invalid frame length %d (should be %d)\n",
buf_size, needed_size); |
04a585f0 |
return AVERROR_INVALIDDATA; |
0bae6661 |
} |
6a9b565e |
} else {
/* skip frame */
if (buf_size == 8) { |
fc1152de |
*got_frame = 0; |
6a9b565e |
return buf_size;
} |
0efdb942 |
if (AV_RL32(buf) != FPS_TAG || buf_size < planes*1024 + 24) {
av_log(avctx, AV_LOG_ERROR, "Fraps: error in data stream\n"); |
04a585f0 |
return AVERROR_INVALIDDATA; |
0efdb942 |
} |
d3066030 |
for (i = 0; i < planes; i++) { |
0efdb942 |
offs[i] = AV_RL32(buf + 4 + i * 4); |
d3066030 |
if (offs[i] >= buf_size - header_size || (i && offs[i] <= offs[i - 1] + 1024)) { |
0efdb942 |
av_log(avctx, AV_LOG_ERROR, "Fraps: plane %i offset is out of bounds\n", i); |
04a585f0 |
return AVERROR_INVALIDDATA; |
0efdb942 |
}
} |
f9eb6229 |
offs[planes] = buf_size - header_size; |
d3066030 |
for (i = 0; i < planes; i++) { |
0efdb942 |
av_fast_padded_malloc(&s->tmpbuf, &s->tmpbuf_size, offs[i + 1] - offs[i] - 1024);
if (!s->tmpbuf)
return AVERROR(ENOMEM);
} |
b4ec645f |
}
|
b09c93d7 |
f->pict_type = AV_PICTURE_TYPE_I;
f->key_frame = 1; |
115329f1 |
|
80e9e63c |
avctx->pix_fmt = version & 1 ? AV_PIX_FMT_BGR24 : AV_PIX_FMT_YUVJ420P; |
79f452f4 |
avctx->color_range = version & 1 ? AVCOL_RANGE_UNSPECIFIED : AVCOL_RANGE_JPEG; |
1c4fa2aa |
avctx->colorspace = version & 1 ? AVCOL_SPC_UNSPECIFIED : AVCOL_SPC_BT709; |
830f7044 |
|
1ec94b0f |
if ((ret = ff_thread_get_buffer(avctx, &frame, 0)) < 0) |
04a585f0 |
return ret; |
b09c93d7 |
|
80344261 |
switch (version) { |
b81f8949 |
case 0:
default:
/* Fraps v0 is a reordered YUV420 */ |
80344261 |
if (((avctx->width % 8) != 0) || ((avctx->height % 2) != 0)) { |
115329f1 |
av_log(avctx, AV_LOG_ERROR, "Invalid frame size %dx%d\n", |
b81f8949 |
avctx->width, avctx->height); |
04a585f0 |
return AVERROR_INVALIDDATA; |
b81f8949 |
}
|
d3066030 |
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) { |
95e873bb |
*luma1++ = *buf32++;
*luma1++ = *buf32++;
*luma2++ = *buf32++;
*luma2++ = *buf32++;
*cr++ = *buf32++;
*cb++ = *buf32++; |
b81f8949 |
} |
95e873bb |
} |
b81f8949 |
break;
case 1:
/* Fraps v1 is an upside-down BGR24 */ |
80344261 |
for (y = 0; y<avctx->height; y++) |
2cd40680 |
memcpy(&f->data[0][(avctx->height - y - 1) * f->linesize[0]], |
80344261 |
&buf[y * avctx->width * 3],
3 * avctx->width); |
b81f8949 |
break;
case 2: |
08a4c4bf |
case 4: |
b81f8949 |
/** |
08a4c4bf |
* Fraps v2 is Huffman-coded YUV420 planes |
75a71b6c |
* Fraps v4 is virtually the same |
b81f8949 |
*/ |
80344261 |
for (i = 0; i < planes; i++) { |
08a4c4bf |
is_chroma = !!i; |
57d11e5e |
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) { |
08a4c4bf |
av_log(avctx, AV_LOG_ERROR, "Error decoding plane %i\n", i); |
57d11e5e |
return ret; |
08a4c4bf |
}
} |
b81f8949 |
break; |
675f7114 |
case 3: |
8c908d7d |
case 5:
/* Virtually the same as version 4, but is for RGB24 */ |
80344261 |
for (i = 0; i < planes; i++) { |
57d11e5e |
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) { |
8c908d7d |
av_log(avctx, AV_LOG_ERROR, "Error decoding plane %i\n", i); |
57d11e5e |
return ret; |
8c908d7d |
}
} |
7fabef1f |
out = f->data[0]; |
af176191 |
// convert pseudo-YUV into real RGB |
80344261 |
for (j = 0; j < avctx->height; j++) { |
7fabef1f |
uint8_t *line_end = out + 3*avctx->width;
while (out < line_end) {
out[0] += out[1];
out[2] += out[1];
out += 3; |
af176191 |
} |
7fabef1f |
out += f->linesize[0] - 3*avctx->width; |
af176191 |
} |
8c908d7d |
break; |
b81f8949 |
}
|
df9b9567 |
*got_frame = 1; |
b81f8949 |
return buf_size;
}
/**
* closes decoder
* @param avctx codec context
* @return 0 on success or negative if fails
*/ |
98a6fff9 |
static av_cold int decode_end(AVCodecContext *avctx) |
b81f8949 |
{
FrapsContext *s = (FrapsContext*)avctx->priv_data;
|
08a4c4bf |
av_freep(&s->tmpbuf); |
b81f8949 |
return 0;
}
|
e7e2df27 |
AVCodec ff_fraps_decoder = { |
ec6402b7 |
.name = "fraps", |
b2bed932 |
.long_name = NULL_IF_CONFIG_SMALL("Fraps"), |
ec6402b7 |
.type = AVMEDIA_TYPE_VIDEO, |
36ef5369 |
.id = AV_CODEC_ID_FRAPS, |
ec6402b7 |
.priv_data_size = sizeof(FrapsContext),
.init = decode_init,
.close = decode_end,
.decode = decode_frame, |
cd3ced1b |
.capabilities = CODEC_CAP_DR1 | CODEC_CAP_FRAME_THREADS, |
b81f8949 |
}; |