FFmpeg
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 "avcodec.h"
39 #include "get_bits.h"
40 #include "huffman.h"
41 #include "bytestream.h"
42 #include "bswapdsp.h"
43 #include "codec_internal.h"
44 #include "thread.h"
45 
46 #define FPS_TAG MKTAG('F', 'P', 'S', 'x')
47 #define VLC_BITS 11
48 
49 /**
50  * local variable storage
51  */
52 typedef struct FrapsContext {
55  uint8_t *tmpbuf;
57 } FrapsContext;
58 
59 /**
60  * initializes decoder
61  * @param avctx codec context
62  * @return 0 on success or negative if fails
63  */
65 {
66  FrapsContext * const s = avctx->priv_data;
67 
68  s->avctx = avctx;
69  s->tmpbuf = NULL;
70 
71  ff_bswapdsp_init(&s->bdsp);
72 
73  return 0;
74 }
75 
76 /**
77  * Comparator - our nodes should ascend by count
78  * but with preserved symbol order
79  */
80 static int huff_cmp(const void *va, const void *vb)
81 {
82  const Node *a = va, *b = vb;
83  return (a->count - b->count)*256 + a->sym - b->sym;
84 }
85 
86 /**
87  * decode Fraps v2 packed plane
88  */
89 static int fraps2_decode_plane(FrapsContext *s, uint8_t *dst, int stride, int w,
90  int h, const uint8_t *src, int size, int Uoff,
91  const int step)
92 {
93  int i, j, ret;
94  GetBitContext gb;
95  VLC vlc;
96  Node nodes[512];
97 
98  for (i = 0; i < 256; i++)
99  nodes[i].count = bytestream_get_le32(&src);
100  size -= 1024;
101  if ((ret = ff_huff_build_tree(s->avctx, &vlc, 256, VLC_BITS,
102  nodes, huff_cmp,
104  return ret;
105  /* we have built Huffman table and are ready to decode plane */
106 
107  /* convert bits so they may be used by standard bitreader */
108  s->bdsp.bswap_buf((uint32_t *) s->tmpbuf,
109  (const uint32_t *) src, size >> 2);
110 
111  if ((ret = init_get_bits8(&gb, s->tmpbuf, size)) < 0)
112  return ret;
113 
114  for (j = 0; j < h; j++) {
115  for (i = 0; i < w*step; i += step) {
116  dst[i] = get_vlc2(&gb, vlc.table, VLC_BITS, 3);
117  /* lines are stored as deltas between previous lines
118  * and we need to add 0x80 to the first lines of chroma planes
119  */
120  if (j)
121  dst[i] += dst[i - stride];
122  else if (Uoff)
123  dst[i] += 0x80;
124  if (get_bits_left(&gb) < 0) {
125  ff_vlc_free(&vlc);
126  return AVERROR_INVALIDDATA;
127  }
128  }
129  dst += stride;
130  }
131  ff_vlc_free(&vlc);
132  return 0;
133 }
134 
135 static int decode_frame(AVCodecContext *avctx, AVFrame *f,
136  int *got_frame, AVPacket *avpkt)
137 {
138  FrapsContext * const s = avctx->priv_data;
139  const uint8_t *buf = avpkt->data;
140  int buf_size = avpkt->size;
141  uint32_t header;
142  unsigned int version,header_size;
143  const uint32_t *buf32;
144  uint32_t *luma1,*luma2,*cb,*cr;
145  uint32_t offs[4];
146  int i, j, ret, is_chroma;
147  const int planes = 3;
148  int is_pal;
149  uint8_t *out;
150 
151  if (buf_size < 4) {
152  av_log(avctx, AV_LOG_ERROR, "Packet is too short\n");
153  return AVERROR_INVALIDDATA;
154  }
155 
156  header = AV_RL32(buf);
157  version = header & 0xff;
158  is_pal = buf[1] == 2 && version == 1;
159  header_size = (header & (1<<30))? 8 : 4; /* bit 30 means pad to 8 bytes */
160 
161  if (version > 5) {
162  avpriv_report_missing_feature(avctx, "Fraps version %u", version);
163  return AVERROR_PATCHWELCOME;
164  }
165 
166  buf += header_size;
167 
168  if (is_pal) {
169  unsigned needed_size = avctx->width * avctx->height + 1024;
170  needed_size += header_size;
171  if (buf_size != needed_size) {
172  av_log(avctx, AV_LOG_ERROR,
173  "Invalid frame length %d (should be %d)\n",
174  buf_size, needed_size);
175  return AVERROR_INVALIDDATA;
176  }
177  } else if (version < 2) {
178  unsigned needed_size = avctx->width * avctx->height * 3;
179  if (version == 0) needed_size /= 2;
180  needed_size += header_size;
181  /* bit 31 means same as previous pic */
182  if (header & (1U<<31)) {
183  *got_frame = 0;
184  return buf_size;
185  }
186  if (buf_size != needed_size) {
187  av_log(avctx, AV_LOG_ERROR,
188  "Invalid frame length %d (should be %d)\n",
189  buf_size, needed_size);
190  return AVERROR_INVALIDDATA;
191  }
192  } else {
193  /* skip frame */
194  if (buf_size == 8) {
195  *got_frame = 0;
196  return buf_size;
197  }
198  if (AV_RL32(buf) != FPS_TAG || buf_size < planes*1024 + 24) {
199  av_log(avctx, AV_LOG_ERROR, "error in data stream\n");
200  return AVERROR_INVALIDDATA;
201  }
202  for (i = 0; i < planes; i++) {
203  offs[i] = AV_RL32(buf + 4 + i * 4);
204  if (offs[i] >= buf_size - header_size || (i && offs[i] <= offs[i - 1] + 1024)) {
205  av_log(avctx, AV_LOG_ERROR, "plane %i offset is out of bounds\n", i);
206  return AVERROR_INVALIDDATA;
207  }
208  }
209  offs[planes] = buf_size - header_size;
210  for (i = 0; i < planes; i++) {
211  av_fast_padded_malloc(&s->tmpbuf, &s->tmpbuf_size, offs[i + 1] - offs[i] - 1024);
212  if (!s->tmpbuf)
213  return AVERROR(ENOMEM);
214  }
215  }
216 
217  f->pict_type = AV_PICTURE_TYPE_I;
218  f->flags |= AV_FRAME_FLAG_KEY;
219 
224 
225  if ((ret = ff_thread_get_buffer(avctx, f, 0)) < 0)
226  return ret;
227 
228  switch (version) {
229  case 0:
230  default:
231  /* Fraps v0 is a reordered YUV420 */
232  if (((avctx->width % 8) != 0) || ((avctx->height % 2) != 0)) {
233  av_log(avctx, AV_LOG_ERROR, "Invalid frame size %dx%d\n",
234  avctx->width, avctx->height);
235  return AVERROR_INVALIDDATA;
236  }
237 
238  buf32 = (const uint32_t*)buf;
239  for (ptrdiff_t y = 0; y < avctx->height / 2; y++) {
240  luma1 = (uint32_t*)&f->data[0][ y * 2 * f->linesize[0] ];
241  luma2 = (uint32_t*)&f->data[0][ (y * 2 + 1) * f->linesize[0] ];
242  cr = (uint32_t*)&f->data[1][ y * f->linesize[1] ];
243  cb = (uint32_t*)&f->data[2][ y * f->linesize[2] ];
244  for (ptrdiff_t x = 0; x < avctx->width; x += 8) {
245  *luma1++ = *buf32++;
246  *luma1++ = *buf32++;
247  *luma2++ = *buf32++;
248  *luma2++ = *buf32++;
249  *cr++ = *buf32++;
250  *cb++ = *buf32++;
251  }
252  }
253  break;
254 
255  case 1:
256  if (is_pal) {
257  uint32_t *pal = (uint32_t *)f->data[1];
258 
259  for (unsigned y = 0; y < 256; y++) {
260  pal[y] = AV_RL32(buf) | 0xFF000000;
261  buf += 4;
262  }
263 
264  for (ptrdiff_t y = 0; y < avctx->height; y++)
265  memcpy(&f->data[0][y * f->linesize[0]],
266  &buf[y * avctx->width],
267  avctx->width);
268  } else {
269  /* Fraps v1 is an upside-down BGR24 */
270  for (ptrdiff_t y = 0; y < avctx->height; y++)
271  memcpy(&f->data[0][(avctx->height - y - 1) * f->linesize[0]],
272  &buf[y * avctx->width * 3],
273  3 * avctx->width);
274  }
275  break;
276 
277  case 2:
278  case 4:
279  /**
280  * Fraps v2 is Huffman-coded YUV420 planes
281  * Fraps v4 is virtually the same
282  */
283  for (i = 0; i < planes; i++) {
284  is_chroma = !!i;
285  if ((ret = fraps2_decode_plane(s, f->data[i], f->linesize[i],
286  avctx->width >> is_chroma,
287  avctx->height >> is_chroma,
288  buf + offs[i], offs[i + 1] - offs[i],
289  is_chroma, 1)) < 0) {
290  av_log(avctx, AV_LOG_ERROR, "Error decoding plane %i\n", i);
291  return ret;
292  }
293  }
294  break;
295  case 3:
296  case 5:
297  /* Virtually the same as version 4, but is for RGB24 */
298  for (i = 0; i < planes; i++) {
299  if ((ret = fraps2_decode_plane(s, f->data[0] + i + (f->linesize[0] * (avctx->height - 1)),
300  -f->linesize[0], avctx->width, avctx->height,
301  buf + offs[i], offs[i + 1] - offs[i], 0, 3)) < 0) {
302  av_log(avctx, AV_LOG_ERROR, "Error decoding plane %i\n", i);
303  return ret;
304  }
305  }
306  out = f->data[0];
307  // convert pseudo-YUV into real RGB
308  for (j = 0; j < avctx->height; j++) {
309  uint8_t *line_end = out + 3*avctx->width;
310  while (out < line_end) {
311  out[0] += out[1];
312  out[2] += out[1];
313  out += 3;
314  }
315  out += f->linesize[0] - 3*avctx->width;
316  }
317  break;
318  }
319 
320  *got_frame = 1;
321 
322  return buf_size;
323 }
324 
325 /**
326  * closes decoder
327  * @param avctx codec context
328  * @return 0 on success or negative if fails
329  */
331 {
332  FrapsContext *s = avctx->priv_data;
333 
334  av_freep(&s->tmpbuf);
335  return 0;
336 }
337 
339  .p.name = "fraps",
340  CODEC_LONG_NAME("Fraps"),
341  .p.type = AVMEDIA_TYPE_VIDEO,
342  .p.id = AV_CODEC_ID_FRAPS,
343  .priv_data_size = sizeof(FrapsContext),
344  .init = decode_init,
345  .close = decode_end,
347  .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS,
348 };
bswapdsp.h
FrapsContext::avctx
AVCodecContext * avctx
Definition: fraps.c:53
get_bits_left
static int get_bits_left(GetBitContext *gb)
Definition: get_bits.h:694
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
AVCodecContext::colorspace
enum AVColorSpace colorspace
YUV colorspace type.
Definition: avcodec.h:1029
out
FILE * out
Definition: movenc.c:54
Node
Definition: agm.c:897
cb
static double cb(void *priv, double x, double y)
Definition: vf_geq.c:241
ff_fraps_decoder
const FFCodec ff_fraps_decoder
Definition: fraps.c:338
FrapsContext::tmpbuf_size
int tmpbuf_size
Definition: fraps.c:56
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:340
step
trying all byte sequences megabyte in length and selecting the best looking sequence will yield cases to try But a word about which is also called distortion Distortion can be quantified by almost any quality measurement one chooses the sum of squared differences is used but more complex methods that consider psychovisual effects can be used as well It makes no difference in this discussion First step
Definition: rate_distortion.txt:58
w
uint8_t w
Definition: llviddspenc.c:38
AVCOL_RANGE_JPEG
@ AVCOL_RANGE_JPEG
Full range content.
Definition: pixfmt.h:673
AVPacket::data
uint8_t * data
Definition: packet.h:491
b
#define b
Definition: input.c:41
FFCodec
Definition: codec_internal.h:127
AV_PIX_FMT_BGR24
@ AV_PIX_FMT_BGR24
packed RGB 8:8:8, 24bpp, BGRBGR...
Definition: pixfmt.h:69
decode_end
static av_cold int decode_end(AVCodecContext *avctx)
closes decoder
Definition: fraps.c:330
thread.h
ff_thread_get_buffer
the pkt_dts and pkt_pts fields in AVFrame will work as usual Restrictions on codec whose streams don t reset across will not work because their bitstreams cannot be decoded in parallel *The contents of buffers must not be read before as well as code calling up to before the decode process starts Call have so the codec calls ff_thread_report set FF_CODEC_CAP_ALLOCATE_PROGRESS in FFCodec caps_internal and use ff_thread_get_buffer() to allocate frames. Otherwise decode directly into the user-supplied frames. Call ff_thread_report_progress() after some part of the current picture has decoded. A good place to put this is where draw_horiz_band() is called - add this if it isn 't called anywhere
FFCodec::p
AVCodec p
The public AVCodec.
Definition: codec_internal.h:131
FF_HUFFMAN_FLAG_ZERO_COUNT
#define FF_HUFFMAN_FLAG_ZERO_COUNT
Definition: huffman.h:40
GetBitContext
Definition: get_bits.h:108
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
av_cold
#define av_cold
Definition: attributes.h:90
init_get_bits8
static int init_get_bits8(GetBitContext *s, const uint8_t *buffer, int byte_size)
Initialize GetBitContext.
Definition: get_bits.h:545
AV_FRAME_FLAG_KEY
#define AV_FRAME_FLAG_KEY
A flag to mark frames that are keyframes.
Definition: frame.h:628
FF_CODEC_DECODE_CB
#define FF_CODEC_DECODE_CB(func)
Definition: codec_internal.h:306
s
#define s(width, name)
Definition: cbs_vp9.c:198
FrapsContext
local variable storage
Definition: fraps.c:52
init
int(* init)(AVBSFContext *ctx)
Definition: dts2pts_bsf.c:365
get_bits.h
CODEC_LONG_NAME
#define CODEC_LONG_NAME(str)
Definition: codec_internal.h:272
AV_CODEC_CAP_FRAME_THREADS
#define AV_CODEC_CAP_FRAME_THREADS
Codec supports frame-level multithreading.
Definition: codec.h:110
FPS_TAG
#define FPS_TAG
Definition: fraps.c:46
ff_bswapdsp_init
av_cold void ff_bswapdsp_init(BswapDSPContext *c)
Definition: bswapdsp.c:49
NULL
#define NULL
Definition: coverity.c:32
AVERROR_PATCHWELCOME
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition: error.h:64
AVCodecContext::color_range
enum AVColorRange color_range
MPEG vs JPEG YUV range.
Definition: avcodec.h:1039
fraps2_decode_plane
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:89
AV_PIX_FMT_YUVJ420P
@ 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:78
AV_PICTURE_TYPE_I
@ AV_PICTURE_TYPE_I
Intra.
Definition: avutil.h:279
get_vlc2
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:652
AVCOL_RANGE_UNSPECIFIED
@ AVCOL_RANGE_UNSPECIFIED
Definition: pixfmt.h:639
decode_frame
static int decode_frame(AVCodecContext *avctx, AVFrame *f, int *got_frame, AVPacket *avpkt)
Definition: fraps.c:135
AV_CODEC_ID_FRAPS
@ AV_CODEC_ID_FRAPS
Definition: codec_id.h:128
f
f
Definition: af_crystalizer.c:121
AV_CODEC_CAP_DR1
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
Definition: codec.h:52
AVPacket::size
int size
Definition: packet.h:492
codec_internal.h
for
for(k=2;k<=8;++k)
Definition: h264pred_template.c:425
size
int size
Definition: twinvq_data.h:10344
avpriv_report_missing_feature
void avpriv_report_missing_feature(void *avc, const char *msg,...) av_printf_format(2
Log a generic warning message about a missing feature.
header
static const uint8_t header[24]
Definition: sdr2.c:67
a
The reader does not expect b to be semantically here and if the code is changed by maybe adding a a division or other the signedness will almost certainly be mistaken To avoid this confusion a new type was SUINT is the C unsigned type but it holds a signed int to use the same example SUINT a
Definition: undefined.txt:41
FrapsContext::bdsp
BswapDSPContext bdsp
Definition: fraps.c:54
version
version
Definition: libkvazaar.c:321
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:255
av_fast_padded_malloc
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:52
planes
static const struct @363 planes[]
AVCodec::name
const char * name
Name of the codec implementation.
Definition: codec.h:194
AVCOL_SPC_UNSPECIFIED
@ AVCOL_SPC_UNSPECIFIED
Definition: pixfmt.h:602
AVCodecContext::height
int height
Definition: avcodec.h:621
AVCodecContext::pix_fmt
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:658
avcodec.h
stride
#define stride
Definition: h264pred_template.c:537
AV_PIX_FMT_PAL8
@ AV_PIX_FMT_PAL8
8 bits with AV_PIX_FMT_RGB32 palette
Definition: pixfmt.h:77
ff_vlc_free
void ff_vlc_free(VLC *vlc)
Definition: vlc.c:509
ret
ret
Definition: filter_design.txt:187
ff_huff_build_tree
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:159
AV_RL32
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_RL32
Definition: bytestream.h:92
U
#define U(x)
Definition: vpx_arith.h:37
FrapsContext::tmpbuf
uint8_t * tmpbuf
Definition: fraps.c:55
huff_cmp
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:80
AVCodecContext
main external API structure.
Definition: avcodec.h:441
VLC
Definition: vlc.h:33
huffman.h
decode_init
static av_cold int decode_init(AVCodecContext *avctx)
initializes decoder
Definition: fraps.c:64
VLC::table
VLCElem * table
Definition: vlc.h:35
VLC_BITS
#define VLC_BITS
Definition: fraps.c:47
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
AVPacket
This structure stores compressed data.
Definition: packet.h:468
AVCodecContext::priv_data
void * priv_data
Definition: avcodec.h:468
cr
static double cr(void *priv, double x, double y)
Definition: vf_geq.c:242
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:34
src
INIT_CLIP pixel * src
Definition: h264pred_template.c:418
AVCodecContext::width
int width
picture width / height.
Definition: avcodec.h:621
bytestream.h
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:61
BswapDSPContext
Definition: bswapdsp.h:24
h
h
Definition: vp9dsp_template.c:2038
AVCOL_SPC_BT709
@ AVCOL_SPC_BT709
also ITU-R BT1361 / IEC 61966-2-4 xvYCC709 / derived in SMPTE RP 177 Annex B
Definition: pixfmt.h:601