FFmpeg
Loading...
Searching...
No Matches
fraps.c
Go to the documentation of this file.
1/*
2 * Fraps FPS1 decoder
3 * Copyright (c) 2005 Roine Gustafsson
4 * Copyright (c) 2006 Konstantin Shishkov
5 *
6 * This file is part of FFmpeg.
7 *
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.
12 *
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.
17 *
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
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 * Codec algorithm for version 0 is taken from Transcode <www.transcoding.org>
30 *
31 * Version 2 files support by Konstantin Shishkov
32 */
33
34#include "config.h"
35
36#define CACHED_BITSTREAM_READER HAVE_FAST_64BIT
37#define UNCHECKED_BITSTREAM_READER 1
38#include "libavutil/mem.h"
39#include "avcodec.h"
40#include "get_bits.h"
41#include "huffman.h"
42#include "bytestream.h"
43#include "bswapdsp.h"
44#include "codec_internal.h"
45#include "thread.h"
46
47#define FPS_TAG MKTAG('F', 'P', 'S', 'x')
48#define VLC_BITS 11
49
50/**
51 * local variable storage
52 */
59
60/**
61 * initializes decoder
62 * @param avctx codec context
63 * @return 0 on success or negative if fails
64 */
66{
67 FrapsContext * const s = avctx->priv_data;
68
69 s->avctx = avctx;
70 s->tmpbuf = NULL;
71
72 ff_bswapdsp_init(&s->bdsp);
73
74 return 0;
75}
76
77/**
78 * Comparator - our nodes should ascend by count
79 * but with preserved symbol order
80 */
81static int huff_cmp(const void *va, const void *vb)
82{
83 const Node *a = va, *b = vb;
84 return (a->count - b->count)*256 + a->sym - b->sym;
85}
86
87/**
88 * decode Fraps v2 packed plane
89 */
90static int fraps2_decode_plane(FrapsContext *s, uint8_t *dst, int stride, int w,
91 int h, const uint8_t *src, int size, int Uoff,
92 const int step)
93{
94 int i, j, ret;
96 VLC vlc;
97 Node nodes[512];
98
99 for (i = 0; i < 256; i++)
100 nodes[i].count = bytestream_get_le32(&src);
101 size -= 1024;
102 if ((ret = ff_huff_build_tree(s->avctx, &vlc, 256, VLC_BITS,
103 nodes, huff_cmp,
105 return ret;
106 /* we have built Huffman table and are ready to decode plane */
107
108 /* convert bits so they may be used by standard bitreader */
109 s->bdsp.bswap_buf((uint32_t *) s->tmpbuf,
110 (const uint32_t *) src, size >> 2);
111
112 if ((ret = init_get_bits8(&gb, s->tmpbuf, size)) < 0)
113 return ret;
114
115 for (j = 0; j < h; j++) {
116 for (i = 0; i < w*step; i += step) {
117 dst[i] = get_vlc2(&gb, vlc.table, VLC_BITS, 3);
118 /* lines are stored as deltas between previous lines
119 * and we need to add 0x80 to the first lines of chroma planes
120 */
121 if (j)
122 dst[i] += dst[i - stride];
123 else if (Uoff)
124 dst[i] += 0x80;
125 if (get_bits_left(&gb) < 0) {
126 ff_vlc_free(&vlc);
127 return AVERROR_INVALIDDATA;
128 }
129 }
130 dst += stride;
131 }
132 ff_vlc_free(&vlc);
133 return 0;
134}
135
137 int *got_frame, AVPacket *avpkt)
138{
139 FrapsContext * const s = avctx->priv_data;
140 const uint8_t *buf = avpkt->data;
141 int buf_size = avpkt->size;
142 uint32_t header;
143 unsigned int version,header_size;
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 if (buf_size < 4) {
153 av_log(avctx, AV_LOG_ERROR, "Packet is too short\n");
154 return AVERROR_INVALIDDATA;
155 }
156
157 header = AV_RL32(buf);
158 version = header & 0xff;
159 is_pal = buf[1] == 2 && version == 1;
160 header_size = (header & (1<<30))? 8 : 4; /* bit 30 means pad to 8 bytes */
161
162 if (version > 5) {
163 avpriv_report_missing_feature(avctx, "Fraps version %u", version);
165 }
166
167 buf += header_size;
168
169 if (is_pal) {
170 unsigned needed_size = avctx->width * avctx->height + 1024;
171 needed_size += header_size;
172 if (buf_size != needed_size) {
173 av_log(avctx, AV_LOG_ERROR,
174 "Invalid frame length %d (should be %d)\n",
175 buf_size, needed_size);
176 return AVERROR_INVALIDDATA;
177 }
178 } else if (version < 2) {
179 unsigned needed_size = avctx->width * avctx->height * 3;
180 if (version == 0) needed_size /= 2;
181 needed_size += header_size;
182 /* bit 31 means same as previous pic */
183 if (header & (1U<<31)) {
184 *got_frame = 0;
185 return buf_size;
186 }
187 if (buf_size != needed_size) {
188 av_log(avctx, AV_LOG_ERROR,
189 "Invalid frame length %d (should be %d)\n",
190 buf_size, needed_size);
191 return AVERROR_INVALIDDATA;
192 }
193 } else {
194 /* skip frame */
195 if (buf_size == 8) {
196 *got_frame = 0;
197 return buf_size;
198 }
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 av_fast_padded_malloc(&s->tmpbuf, &s->tmpbuf_size, offs[i + 1] - offs[i] - 1024);
213 if (!s->tmpbuf)
214 return AVERROR(ENOMEM);
215 }
216 }
217
222
223 if ((ret = ff_thread_get_buffer(avctx, f, 0)) < 0)
224 return ret;
225
226 switch (version) {
227 case 0:
228 default:
229 /* Fraps v0 is a reordered YUV420 */
230 if (((avctx->width % 8) != 0) || ((avctx->height % 2) != 0)) {
231 av_log(avctx, AV_LOG_ERROR, "Invalid frame size %dx%d\n",
232 avctx->width, avctx->height);
233 return AVERROR_INVALIDDATA;
234 }
235
236 buf32 = (const uint32_t*)buf;
237 for (ptrdiff_t y = 0; y < avctx->height / 2; y++) {
238 luma1 = (uint32_t*)&f->data[0][ y * 2 * f->linesize[0] ];
239 luma2 = (uint32_t*)&f->data[0][ (y * 2 + 1) * f->linesize[0] ];
240 cr = (uint32_t*)&f->data[1][ y * f->linesize[1] ];
241 cb = (uint32_t*)&f->data[2][ y * f->linesize[2] ];
242 for (ptrdiff_t x = 0; x < avctx->width; x += 8) {
243 *luma1++ = *buf32++;
244 *luma1++ = *buf32++;
245 *luma2++ = *buf32++;
246 *luma2++ = *buf32++;
247 *cr++ = *buf32++;
248 *cb++ = *buf32++;
249 }
250 }
251 break;
252
253 case 1:
254 if (is_pal) {
255 uint32_t *pal = (uint32_t *)f->data[1];
256
257 for (unsigned y = 0; y < 256; y++) {
258 pal[y] = AV_RL32(buf) | 0xFF000000;
259 buf += 4;
260 }
261
262 for (ptrdiff_t y = 0; y < avctx->height; y++)
263 memcpy(&f->data[0][y * f->linesize[0]],
264 &buf[y * avctx->width],
265 avctx->width);
266 } else {
267 /* Fraps v1 is an upside-down BGR24 */
268 for (ptrdiff_t y = 0; y < avctx->height; y++)
269 memcpy(&f->data[0][(avctx->height - y - 1) * f->linesize[0]],
270 &buf[y * avctx->width * 3],
271 3 * avctx->width);
272 }
273 break;
274
275 case 2:
276 case 4:
277 /**
278 * Fraps v2 is Huffman-coded YUV420 planes
279 * Fraps v4 is virtually the same
280 */
281 for (i = 0; i < planes; i++) {
282 is_chroma = !!i;
283 if ((ret = fraps2_decode_plane(s, f->data[i], f->linesize[i],
284 avctx->width >> is_chroma,
285 avctx->height >> is_chroma,
286 buf + offs[i], offs[i + 1] - offs[i],
287 is_chroma, 1)) < 0) {
288 av_log(avctx, AV_LOG_ERROR, "Error decoding plane %i\n", i);
289 return ret;
290 }
291 }
292 break;
293 case 3:
294 case 5:
295 /* Virtually the same as version 4, but is for RGB24 */
296 for (i = 0; i < planes; i++) {
297 if ((ret = fraps2_decode_plane(s, f->data[0] + i + (f->linesize[0] * (avctx->height - 1)),
298 -f->linesize[0], avctx->width, avctx->height,
299 buf + offs[i], offs[i + 1] - offs[i], 0, 3)) < 0) {
300 av_log(avctx, AV_LOG_ERROR, "Error decoding plane %i\n", i);
301 return ret;
302 }
303 }
304 out = f->data[0];
305 // convert pseudo-YUV into real RGB
306 for (j = 0; j < avctx->height; j++) {
307 uint8_t *line_end = out + 3*avctx->width;
308 while (out < line_end) {
309 out[0] += out[1];
310 out[2] += out[1];
311 out += 3;
312 }
313 out += f->linesize[0] - 3*avctx->width;
314 }
315 break;
316 }
317
318 *got_frame = 1;
319
320 return buf_size;
321}
322
323/**
324 * closes decoder
325 * @param avctx codec context
326 * @return 0 on success or negative if fails
327 */
329{
330 FrapsContext *s = avctx->priv_data;
331
332 av_freep(&s->tmpbuf);
333 return 0;
334}
335
337 .p.name = "fraps",
338 CODEC_LONG_NAME("Fraps"),
339 .p.type = AVMEDIA_TYPE_VIDEO,
340 .p.id = AV_CODEC_ID_FRAPS,
341 .priv_data_size = sizeof(FrapsContext),
342 .init = decode_init,
343 .close = decode_end,
346};
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
Definition dsp.h:87
const FFCodec ff_fraps_decoder
Definition fraps.c:336
static av_cold void close(AVCodecParserContext *s)
Definition apv_parser.c:197
Libavcodec external API header.
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define f(width, name)
Definition cbs_vp8.c:236
#define s(width, name)
Definition cbs_vp9.c:198
#define VLC_BITS
Definition cfhd.h:103
#define FF_CODEC_DECODE_CB(func)
#define CODEC_LONG_NAME(str)
#define NULL
Definition coverity.c:32
int(* init)(AVBSFContext *ctx)
Definition dts2pts.c:608
static int huff_cmp(const void *va, const void *vb)
Comparator - our nodes should ascend by count but with preserved symbol order.
Definition fraps.c:81
static av_cold int decode_init(AVCodecContext *avctx)
initializes decoder
Definition fraps.c:65
static av_cold int decode_end(AVCodecContext *avctx)
closes decoder
Definition fraps.c:328
static int decode_frame(AVCodecContext *avctx, AVFrame *f, int *got_frame, AVPacket *avpkt)
Definition fraps.c:136
static int fraps2_decode_plane(FrapsContext *s, uint8_t *dst, int stride, int w, int h, const uint8_t *src, int size, int Uoff, const int step)
decode Fraps v2 packed plane
Definition fraps.c:90
#define FPS_TAG
Definition fraps.c:47
bitstream reader API header.
static av_always_inline int get_vlc2(GetBitContext *s, const VLCElem *table, int bits, int max_depth)
Parse a vlc code.
Definition get_bits.h:645
static int get_bits_left(GetBitContext *gb)
Definition get_bits.h:688
static int init_get_bits8(GetBitContext *s, const uint8_t *buffer, int byte_size)
Initialize GetBitContext.
Definition get_bits.h:544
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
Definition codec.h:49
#define AV_CODEC_CAP_FRAME_THREADS
Codec supports frame-level multithreading.
Definition codec.h:98
@ AV_CODEC_ID_FRAPS
Definition codec_id.h:126
void av_fast_padded_malloc(void *ptr, unsigned int *size, size_t min_size)
Same behaviour av_fast_malloc but the buffer has additional AV_INPUT_BUFFER_PADDING_SIZE at the end w...
Definition utils.c:53
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition error.h:64
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition error.h:61
#define AVERROR(e)
Definition error.h:45
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
@ AVMEDIA_TYPE_VIDEO
Definition avutil.h:200
int a
int ff_huff_build_tree(void *logctx, VLC *vlc, int nb_codes, int nb_bits, Node *nodes, HuffCmp cmp, int flags)
nodes size must be 2*nb_codes first nb_codes nodes.count must be set
Definition huffman.c:158
huffman tree builder and VLC generator
#define FF_HUFFMAN_FLAG_ZERO_COUNT
Definition huffman.h:40
#define b
Definition input.c:43
#define AV_RL32(p)
static av_cold int decode_init(AVCodecContext *avctx)
Definition 4xm.c:998
static av_cold int decode_end(AVCodecContext *avctx)
Definition 4xm.c:980
static int decode_frame(AVCodecContext *avctx, AVFrame *picture, int *got_frame, AVPacket *avpkt)
Definition 4xm.c:837
av_cold void ff_bswapdsp_init(BswapDSPContext *c)
Definition bswapdsp.c:37
Multithreading API for decoders.
#define av_cold
Definition attributes.h:117
void avpriv_report_missing_feature(void *avc, const char *msg,...) av_printf_format(2
Log a generic warning message about a missing feature.
version
Definition libkvazaar.c:313
static const struct @257111027162314367033347246032313251342043035002 planes[]
uint8_t w
Definition llvidencdsp.c:39
Memory handling functions.
@ AVCOL_RANGE_UNSPECIFIED
Definition pixfmt.h:749
@ AVCOL_RANGE_JPEG
Full range content.
Definition pixfmt.h:783
@ AV_PIX_FMT_PAL8
8 bits with AV_PIX_FMT_RGB32 palette
Definition pixfmt.h:84
@ AV_PIX_FMT_BGR24
packed RGB 8:8:8, 24bpp, BGRBGR...
Definition pixfmt.h:76
@ AV_PIX_FMT_YUVJ420P
planar YUV 4:2:0, 12bpp, full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV420P and setting col...
Definition pixfmt.h:85
@ AVCOL_SPC_BT709
also ITU-R BT1361 / IEC 61966-2-4 xvYCC709 / derived in SMPTE RP 177 Annex B
Definition pixfmt.h:708
@ AVCOL_SPC_UNSPECIFIED
Definition pixfmt.h:709
int ff_thread_get_buffer(AVCodecContext *avctx, AVFrame *f, int flags)
Wrapper around get_buffer() for frame-multithreaded codecs.
static const uint8_t header[24]
Definition sdr2.c:68
main external API structure.
Definition avcodec.h:443
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition avcodec.h:643
int width
picture width / height.
Definition avcodec.h:604
enum AVColorRange color_range
MPEG vs JPEG YUV range.
Definition avcodec.h:681
enum AVColorSpace colorspace
YUV colorspace type.
Definition avcodec.h:671
void * priv_data
Definition avcodec.h:470
This structure describes decoded (raw) audio or video data.
Definition frame.h:472
This structure stores compressed data.
Definition packet.h:580
int size
Definition packet.h:604
uint8_t * data
Definition packet.h:603
local variable storage
Definition fraps.c:53
int tmpbuf_size
Definition fraps.c:57
uint8_t * tmpbuf
Definition fraps.c:56
BswapDSPContext bdsp
Definition fraps.c:55
AVCodecContext * avctx
Definition fraps.c:54
Definition agm.c:903
Definition vlc.h:50
VLCElem * table
Definition vlc.h:52
#define stride
#define av_freep(p)
#define av_log(a,...)
uint32_t buf32[PRNG_CACHE_SIZE > > 2]
Definition utils.c:181
#define src
Definition vp8dsp.c:248
static FILE * out
Definition movenc.c:55
int size
static double cr(void *priv, double x, double y)
Definition vf_geq.c:248
static double cb(void *priv, double x, double y)
Definition vf_geq.c:247
void ff_vlc_free(VLC *vlc)
Definition vlc.c:580